From e2795d1a1b096b4846270941c4a202fb82b325f0 Mon Sep 17 00:00:00 2001 From: Rex Zeng Date: Mon, 3 Mar 2025 22:56:55 +0800 Subject: [PATCH] fix: no empty string matches Close #47 --- rules/rules.go | 2 +- rules/zh/exact_month_date.go | 41 +++++++++++++++++++++++++----------- rules/zh/weekday_test.go | 2 +- 3 files changed, 31 insertions(+), 14 deletions(-) diff --git a/rules/rules.go b/rules/rules.go index 2011d3f..e03f035 100644 --- a/rules/rules.go +++ b/rules/rules.go @@ -74,7 +74,7 @@ func (f *F) Find(text string) *Match { } } - if len(m.Captures) == 0 { + if len(m.Captures) == 0 || m.Left == -1 { return nil } diff --git a/rules/zh/exact_month_date.go b/rules/zh/exact_month_date.go index 3bfccb5..79bfe21 100644 --- a/rules/zh/exact_month_date.go +++ b/rules/zh/exact_month_date.go @@ -19,13 +19,17 @@ func ExactMonthDate(s rules.Strategy) rules.Rule { RegExp: regexp.MustCompile("" + "(?:\\b|^)" + // can't use \W here due to Chinese characters "(?:" + - "(1[0-2]|[1-9]|" + MON_WORDS_PATTERN + ")" + "(?:\\s*)" + - "(月|-|/|\\.)" + "(?:\\s*)" + + "(1[0-2]|[1-9])" + "\\s*" + "(?:-|/|\\.)" + "\\s*" + "(1[0-9]|2[0-9]|3[0-1]|[1-9])" + + "|" + + "(?:" + + "(1[0-2]|[1-9]|" + MON_WORDS_PATTERN + ")" + "\\s*" + + "(月)" + "\\s*" + ")?" + "(?:" + - "(1[0-9]|2[0-9]|3[0-1]|[1-9]|" + DAY_WORDS_PATTERN + ")" + "(?:\\s*)" + - "(日|号)?" + - ")?", + "(1[0-9]|2[0-9]|3[0-1]|[1-9]|" + DAY_WORDS_PATTERN + ")" + "\\s*" + + "(日|号)" + + ")?" + + ")", ), Applier: func(m *rules.Match, c *rules.Context, o *rules.Options, ref time.Time) (bool, error) { @@ -38,14 +42,14 @@ func ExactMonthDate(s rules.Strategy) rules.Rule { var dayInt = 1 var exist bool - if m.Captures[1] == "" && m.Captures[3] == "" { + if m.Captures[0] == "" && m.Captures[2] == "" && m.Captures[4] == "" { return false, nil } - if m.Captures[0] != "" { - monInt, exist = MON_WORDS[compressStr(m.Captures[0])] + if m.Captures[2] != "" { + monInt, exist = MON_WORDS[compressStr(m.Captures[2])] if !exist { - mon, err := strconv.Atoi(m.Captures[0]) + mon, err := strconv.Atoi(m.Captures[2]) if err != nil { return false, nil } @@ -53,10 +57,10 @@ func ExactMonthDate(s rules.Strategy) rules.Rule { } } - if m.Captures[2] != "" { - dayInt, exist = DAY_WORDS[compressStr(m.Captures[2])] + if m.Captures[4] != "" { + dayInt, exist = DAY_WORDS[compressStr(m.Captures[4])] if !exist { - day, err := strconv.Atoi(m.Captures[2]) + day, err := strconv.Atoi(m.Captures[4]) if err != nil { return false, nil } @@ -64,6 +68,19 @@ func ExactMonthDate(s rules.Strategy) rules.Rule { } } + if m.Captures[0] != "" && m.Captures[1] != "" { + mon, err := strconv.Atoi(m.Captures[0]) + if err != nil { + return false, nil + } + day, err := strconv.Atoi(m.Captures[1]) + if err != nil { + return false, nil + } + monInt = mon + dayInt = day + } + c.Month = &monInt c.Day = &dayInt diff --git a/rules/zh/weekday_test.go b/rules/zh/weekday_test.go index e8718a2..b301808 100644 --- a/rules/zh/weekday_test.go +++ b/rules/zh/weekday_test.go @@ -1,12 +1,12 @@ package zh_test import ( - "github.com/olebedev/when/rules/zh" "testing" "time" "github.com/olebedev/when" "github.com/olebedev/when/rules" + "github.com/olebedev/when/rules/zh" ) func TestWeekday(t *testing.T) {