Skip to content

Commit

Permalink
[impr/performance] Replace regex number validations MVP
Browse files Browse the repository at this point in the history
  • Loading branch information
lnquy committed Apr 29, 2020
1 parent 0509c4b commit 7c291c5
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions parser.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cron

import (
"bytes"
"errors"
"fmt"
"regexp"
Expand Down Expand Up @@ -267,22 +268,25 @@ func (p *cronParser) normalize(exprParts []string) (err error) {
return nil
}

// TODO: Regex is really expensive here. Improve it
func (p *cronParser) validate(exprParts []string) (err error) {
// Second
buf := bytes.NewBuffer(make([]byte, 0, 8))
getNumbersFunc := func(s string) (numbers []string) {
runes := []rune(s)
var num []rune
for _, r := range runes {
if r >= '0' && r <= '9' {
num = append(num, r)
for _, b := range s {
if b >= '0' && b <= '9' {
_, _ = buf.WriteRune(b)
} else {
if len(num) > 0 {
numbers = append(numbers, string(num))
num = make([]rune, 0)
if buf.Len() > 0 {
numbers = append(numbers, buf.String())
buf.Reset()
}
}
}

if buf.Len() > 0 {
numbers = append(numbers, buf.String())
buf.Reset()
}
return numbers
}

Expand Down

0 comments on commit 7c291c5

Please sign in to comment.