Skip to content

Commit 6bed25a

Browse files
committed
fix: no empty string matches
Close #47
1 parent 987a8ee commit 6bed25a

File tree

3 files changed

+30
-17
lines changed

3 files changed

+30
-17
lines changed

rules/rules.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func (f *F) Find(text string) *Match {
7474
}
7575
}
7676

77-
if len(m.Captures) == 0 {
77+
if len(m.Captures) == 0 || m.Left == -1 {
7878
return nil
7979
}
8080

rules/zh/exact_month_date.go

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,17 @@ func ExactMonthDate(s rules.Strategy) rules.Rule {
1919
RegExp: regexp.MustCompile("" +
2020
"(?:\\b|^)" + // can't use \W here due to Chinese characters
2121
"(?:" +
22-
"(1[0-2]|[1-9]|" + MON_WORDS_PATTERN + ")" + "(?:\\s*)" +
23-
"(月|-|/|\\.)" + "(?:\\s*)" +
22+
"(1[0-2]|[1-9])" + "\\s*" + "(?:-|/|\\.)" + "\\s*" + "(1[0-9]|2[0-9]|3[0-1]|[1-9])" +
23+
"|" +
24+
"(?:" +
25+
"(1[0-2]|[1-9]|" + MON_WORDS_PATTERN + ")" + "\\s*" +
26+
"(月)" + "\\s*" +
2427
")?" +
2528
"(?:" +
26-
"(1[0-9]|2[0-9]|3[0-1]|[1-9]|" + DAY_WORDS_PATTERN + ")" + "(?:\\s*)" +
27-
"(日|号)?" +
28-
")?",
29+
"(1[0-9]|2[0-9]|3[0-1]|[1-9]|" + DAY_WORDS_PATTERN + ")" + "\\s*" +
30+
"(日|号)" +
31+
")?" +
32+
")",
2933
),
3034

3135
Applier: func(m *rules.Match, c *rules.Context, o *rules.Options, ref time.Time) (bool, error) {
@@ -38,32 +42,41 @@ func ExactMonthDate(s rules.Strategy) rules.Rule {
3842
var dayInt = 1
3943
var exist bool
4044

41-
if m.Captures[1] == "" && m.Captures[3] == "" {
42-
return false, nil
43-
}
44-
45-
if m.Captures[0] != "" {
46-
monInt, exist = MON_WORDS[compressStr(m.Captures[0])]
45+
if m.Captures[2] != "" {
46+
monInt, exist = MON_WORDS[compressStr(m.Captures[2])]
4747
if !exist {
48-
mon, err := strconv.Atoi(m.Captures[0])
48+
mon, err := strconv.Atoi(m.Captures[2])
4949
if err != nil {
5050
return false, nil
5151
}
5252
monInt = mon
5353
}
5454
}
5555

56-
if m.Captures[2] != "" {
57-
dayInt, exist = DAY_WORDS[compressStr(m.Captures[2])]
56+
if m.Captures[4] != "" {
57+
dayInt, exist = DAY_WORDS[compressStr(m.Captures[4])]
5858
if !exist {
59-
day, err := strconv.Atoi(m.Captures[2])
59+
day, err := strconv.Atoi(m.Captures[4])
6060
if err != nil {
6161
return false, nil
6262
}
6363
dayInt = day
6464
}
6565
}
6666

67+
if m.Captures[0] != "" && m.Captures[1] != "" {
68+
mon, err := strconv.Atoi(m.Captures[0])
69+
if err != nil {
70+
return false, nil
71+
}
72+
day, err := strconv.Atoi(m.Captures[1])
73+
if err != nil {
74+
return false, nil
75+
}
76+
monInt = mon
77+
dayInt = day
78+
}
79+
6780
c.Month = &monInt
6881
c.Day = &dayInt
6982

rules/zh/weekday_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package zh_test
22

33
import (
4-
"github.com/olebedev/when/rules/zh"
54
"testing"
65
"time"
76

87
"github.com/olebedev/when"
98
"github.com/olebedev/when/rules"
9+
"github.com/olebedev/when/rules/zh"
1010
)
1111

1212
func TestWeekday(t *testing.T) {

0 commit comments

Comments
 (0)