/*
* Author, Copyright: Oleg Borodin <onborodin@gmail.com>
*/
package main
import (
"fmt"
"regexp"
"strings"
"strconv"
)
func main() {
max := 23
field := expander("1-2,5-6,,*//5,,", max)
for i := 0; i < max; i++ {
if field[i] {
fmt.Print(i, " ")
}
}
fmt.Println("")
}
/* Expand comma-separated list records like * /N, N-M,* */
func expander(items string, max int) map[int]bool {
items = strings.ReplaceAll(items, "--", "-")
items = strings.ReplaceAll(items, ",,", ",")
items = strings.ReplaceAll(items, "//", "/")
fmt.Println("items:",items)
field := make(map[int]bool)
for i := 0; i < max; i++ {
field[i] = false
}
for _, item := range strings.Split(items, ",") {
if len(item) == 0 {
continue
}
var match bool
match, _ = regexp.MatchString(`^\*$`, item)
if match {
for i := 0; i < max; i++ {
field[i] = true
}
}
match, _ = regexp.MatchString(`^\*/[0-9]+$`, item)
if match {
arr := strings.Split(item, "/")
period, _ := strconv.Atoi(arr[1])
num := 0
for i := 0; i < max; i++ {
num = num + period
if num > max {
break
}
field[num] = true
}
}
match, _ = regexp.MatchString(`^[0-9]+-[0-9]+$`, item)
if match {
arr := strings.Split(item, "-")
begin, _ := strconv.Atoi(arr[0])
end, _ := strconv.Atoi(arr[1])
for i := begin; i < end + 1 && i < max; i++ {
field[i] = true
}
}
}
return field
}