-
Notifications
You must be signed in to change notification settings - Fork 1
/
finder.go
56 lines (48 loc) · 1.3 KB
/
finder.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package date
import (
"strings"
"unicode"
"unicode/utf8"
"github.com/domonda/go-types/language"
)
func NewFinder(lang ...language.Code) *Finder {
return &Finder{LangHint: getLangHint(lang)}
}
type Finder struct {
LangHint language.Code
}
func (df *Finder) FindAllIndex(str []byte, n int) (indices [][]int) {
if len(str) < MinLength {
return nil
}
s := string(str)
// Find all spaces, also treat string bounds as spaces
spacePos := make([]int, 1, 16)
spacePos[0] = -1
for i, r := range s {
if unicode.IsSpace(r) {
spacePos = append(spacePos, i)
}
}
spacePos = append(spacePos, len(str))
for begSpace := 0; begSpace < len(spacePos)-1; begSpace++ {
for endSpace := begSpace + 1; endSpace < begSpace+4 && endSpace < len(spacePos); endSpace++ {
beg := spacePos[begSpace] + 1
end := spacePos[endSpace]
for r, n := utf8.DecodeRune(str[beg:end]); r != utf8.RuneError && isDateTrimRune(r); {
beg += n
r, n = utf8.DecodeRune(str[beg:end])
}
for r, n := utf8.DecodeLastRune(str[beg:end]); r != utf8.RuneError && isDateTrimRune(r); {
end -= n
r, n = utf8.DecodeLastRune(str[beg:end])
}
_, _, err := normalizeAndCheckDate(strings.ToLower(s[beg:end]), df.LangHint)
if err == nil {
indices = append(indices, []int{beg, end})
break
}
}
}
return indices
}