Skip to content

Commit 74bac81

Browse files
authored
Merge pull request dlclark#28 from dop251/fix-start-at-end
Fix start at end string-index-to-rune-index mapping problem.
2 parents 4a0a6ac + 104f7f5 commit 74bac81

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

regexp.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -235,17 +235,14 @@ func (re *Regexp) getRunesAndStart(s string, startAt int) ([]rune, int) {
235235
ret[i] = r
236236
i++
237237
}
238+
if startAt == len(s) {
239+
runeIdx = i
240+
}
238241
return ret[:i], runeIdx
239242
}
240243

241244
func getRunes(s string) []rune {
242-
ret := make([]rune, len(s))
243-
i := 0
244-
for _, r := range s {
245-
ret[i] = r
246-
i++
247-
}
248-
return ret[:i]
245+
return []rune(s)
249246
}
250247

251248
// MatchRunes return true if the runes matches the regex

regexp_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -877,6 +877,17 @@ func TestAlternationConstruct_Matches(t *testing.T) {
877877
}
878878
}
879879

880+
func TestStartAtEnd(t *testing.T) {
881+
re := MustCompile("(?:)", 0)
882+
m, err := re.FindStringMatchStartingAt("t", 1)
883+
if err != nil {
884+
t.Fatal(err)
885+
}
886+
if m == nil {
887+
t.Fatal("Expected match")
888+
}
889+
}
890+
880891
func TestParserFuzzCrashes(t *testing.T) {
881892
var crashes = []string{
882893
"(?'-", "(\\c0)", "(\\00(?())", "[\\p{0}", "(\x00?.*.()?(()?)?)*.x\xcb?&(\\s\x80)", "\\p{0}", "[0-[\\p{0}",

0 commit comments

Comments
 (0)