github.com/dolthub/go-mysql-server@v0.18.0/sql/planbuilder/dateparse/combinators.go (about) 1 package dateparse 2 3 import ( 4 "strconv" 5 "strings" 6 ) 7 8 type predicate func(char rune) bool 9 10 func isChar(a rune) predicate { return func(b rune) bool { return a == b } } 11 12 func takeAtMost(count int, str string, match predicate) (captured, rest string) { 13 var result strings.Builder 14 for i, ch := range str { 15 if i == count { 16 rest = trimPrefix(i, str) 17 break 18 } 19 if !match(ch) { 20 return result.String(), trimPrefix(i, str) 21 } 22 result.WriteRune(ch) 23 } 24 return result.String(), rest 25 } 26 27 func takeAll(str string, match predicate) (captured, rest string) { 28 return takeAtMost(-1, str, match) 29 } 30 31 func takeNumberAtMostNChars(n int, chars string) (num uint, rest string, err error) { 32 numChars, rest := takeAtMost(n, chars, isNumeral) 33 parsedNum, err := strconv.ParseUint(numChars, 10, 32) 34 if err != nil { 35 return 0, "", err 36 } 37 return uint(parsedNum), rest, nil 38 } 39 40 func takeAllSpaces(str string) (rest string) { 41 _, rest = takeAll(str, isChar(' ')) 42 return rest 43 } 44 45 func takeNumber(chars string) (num uint, rest string, err error) { 46 numChars, rest := takeAll(chars, isNumeral) 47 parsedNum, err := strconv.ParseUint(numChars, 10, 32) 48 if err != nil { 49 return 0, "", err 50 } 51 return uint(parsedNum), rest, nil 52 } 53 54 func isNumeral(r rune) bool { 55 switch r { 56 case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9': 57 return true 58 } 59 return false 60 }