diff options
| author | Stuart Skelton <stuarts@broadbean.com> | 2017-08-08 21:00:12 +0100 |
|---|---|---|
| committer | Stuart Skelton <stuarts@broadbean.com> | 2017-08-12 17:59:23 +0100 |
| commit | 180dd57d27f8d7b0add8005ee46026e6f89fabbb (patch) | |
| tree | c95d976d0480fe60114ca4a0f3932fd46cc2b1a3 /todolist/app.go | |
| parent | 8f8dcb6def8ef78c6b144f96e61dc627dd1cc3a8 (diff) | |
Refactor getIds so the parsing can be reused.
Diffstat (limited to 'todolist/app.go')
| -rw-r--r-- | todolist/app.go | 36 |
1 files changed, 22 insertions, 14 deletions
diff --git a/todolist/app.go b/todolist/app.go index ffac17f..d4513ac 100644 --- a/todolist/app.go +++ b/todolist/app.go @@ -190,23 +190,16 @@ func (a *App) getId(input string) int { return -1 } -func (a *App) getIds(input string) []int { - var ids []int - idGroups := strings.Split(input, ",") - - rangeNumberRE, _ := regexp.Compile("(\\d+)-(\\d+)") +func (a *App) getIds(input string) (ids []int) { + idGroups := strings.Split(input, ",") for _, idGroup := range idGroups { - if matches := rangeNumberRE.FindStringSubmatch(idGroup); len(matches) > 0 { - lowerID, _ := strconv.Atoi(matches[1]) - upperID, _ := strconv.Atoi(matches[2]) - if lowerID >= upperID { - fmt.Printf("Invalid id group: %s.\n", idGroup) - break - } - for id := lowerID; id <= upperID; id++ { - ids = append(ids, id) + if rangedIds, err := a.parseRangedIds(idGroup); len(rangedIds) > 0 || err != nil { + if err != nil { + fmt.Printf("Invalid id group: %s.\n", input) + continue } + ids = append(ids, rangedIds...) } else if id := a.getId(idGroup); id != -1 { ids = append(ids, id) } else { @@ -216,6 +209,21 @@ func (a *App) getIds(input string) []int { return ids } +func (a *App) parseRangedIds(input string) (ids []int, err error) { + rangeNumberRE, _ := regexp.Compile("(\\d+)-(\\d+)") + if matches := rangeNumberRE.FindStringSubmatch(input); len(matches) > 0 { + lowerID, _ := strconv.Atoi(matches[1]) + upperID, _ := strconv.Atoi(matches[2]) + if lowerID >= upperID { + return ids, fmt.Errorf("Invalid id group: %s.\n", input) + } + for id := lowerID; id <= upperID; id++ { + ids = append(ids, id) + } + } + return ids, err +} + func (a *App) getGroups(input string, todos []*Todo) *GroupedTodos { grouper := &Grouper{} contextRegex, _ := regexp.Compile(`by c.*$`) |
