fix number extraction ending with number
This commit is contained in:
1 parent
932e644c93
commit
225bd5b49c
2 files changed
+14
-5
No files matched your search
@@ -191,18 +191,15 @@ func extractNums(s string) (nums []int, rest []string, numFirst bool) {
|
||||
digit := unicode.IsDigit(r)
|
||||
numFirst = digit
|
||||
|
||||
for i, c := range s {
|
||||
for _, c := range s {
|
||||
if unicode.IsDigit(c) == digit {
|
||||
buf = append(buf, c)
|
||||
if i != len(s)-1 {
|
||||
continue
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
if digit {
|
||||
i, err := strconv.Atoi(string(buf))
|
||||
if err != nil {
|
||||
// TODO: handle error
|
||||
log.Printf("extracting numbers: %s", err)
|
||||
}
|
||||
nums = append(nums, i)
|
||||
@@ -215,6 +212,16 @@ func extractNums(s string) (nums []int, rest []string, numFirst bool) {
|
||||
digit = !digit
|
||||
}
|
||||
|
||||
if digit {
|
||||
i, err := strconv.Atoi(string(buf))
|
||||
if err != nil {
|
||||
log.Printf("extracting numbers: %s", err)
|
||||
}
|
||||
nums = append(nums, i)
|
||||
} else {
|
||||
rest = append(rest, string(buf))
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -210,6 +210,8 @@ func TestExtractNums(t *testing.T) {
|
||||
}{
|
||||
{"foo123bar456", []int{123, 456}, []string{"foo", "bar"}, false},
|
||||
{"123foo456bar", []int{123, 456}, []string{"foo", "bar"}, true},
|
||||
{"a-1-1", []int{1, 1}, []string{"a-", "-"}, false},
|
||||
{"a-1-1.", []int{1, 1}, []string{"a-", "-", "."}, false},
|
||||
{"a-1-1.txt", []int{1, 1}, []string{"a-", "-", ".txt"}, false},
|
||||
{"a-1-10.txt", []int{1, 10}, []string{"a-", "-", ".txt"}, false},
|
||||
{"a-10-1.txt", []int{10, 1}, []string{"a-", "-", ".txt"}, false},
|
||||
|
||||
Reference in new issue
Block a user