From 180dd57d27f8d7b0add8005ee46026e6f89fabbb Mon Sep 17 00:00:00 2001 From: Stuart Skelton Date: Tue, 8 Aug 2017 21:00:12 +0100 Subject: Refactor getIds so the parsing can be reused. --- todolist/app.go | 36 ++++++++++++++++++++++-------------- 1 file changed, 22 insertions(+), 14 deletions(-) (limited to 'todolist') 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.*$`) -- cgit v1.3