Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion rules/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
41 changes: 29 additions & 12 deletions rules/zh/exact_month_date.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -38,32 +42,45 @@ 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
}
monInt = mon
}
}

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
}
dayInt = day
}
}

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

Expand Down
2 changes: 1 addition & 1 deletion rules/zh/weekday_test.go
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down