Skip to content

Commit e2795d1

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

File tree

3 files changed

+31
-14
lines changed

3 files changed

+31
-14
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: 29 additions & 12 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,45 @@ func ExactMonthDate(s rules.Strategy) rules.Rule {
3842
var dayInt = 1
3943
var exist bool
4044

41-
if m.Captures[1] == "" && m.Captures[3] == "" {
45+
if m.Captures[0] == "" && m.Captures[2] == "" && m.Captures[4] == "" {
4246
return false, nil
4347
}
4448

45-
if m.Captures[0] != "" {
46-
monInt, exist = MON_WORDS[compressStr(m.Captures[0])]
49+
if m.Captures[2] != "" {
50+
monInt, exist = MON_WORDS[compressStr(m.Captures[2])]
4751
if !exist {
48-
mon, err := strconv.Atoi(m.Captures[0])
52+
mon, err := strconv.Atoi(m.Captures[2])
4953
if err != nil {
5054
return false, nil
5155
}
5256
monInt = mon
5357
}
5458
}
5559

56-
if m.Captures[2] != "" {
57-
dayInt, exist = DAY_WORDS[compressStr(m.Captures[2])]
60+
if m.Captures[4] != "" {
61+
dayInt, exist = DAY_WORDS[compressStr(m.Captures[4])]
5862
if !exist {
59-
day, err := strconv.Atoi(m.Captures[2])
63+
day, err := strconv.Atoi(m.Captures[4])
6064
if err != nil {
6165
return false, nil
6266
}
6367
dayInt = day
6468
}
6569
}
6670

71+
if m.Captures[0] != "" && m.Captures[1] != "" {
72+
mon, err := strconv.Atoi(m.Captures[0])
73+
if err != nil {
74+
return false, nil
75+
}
76+
day, err := strconv.Atoi(m.Captures[1])
77+
if err != nil {
78+
return false, nil
79+
}
80+
monInt = mon
81+
dayInt = day
82+
}
83+
6784
c.Month = &monInt
6885
c.Day = &dayInt
6986

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)