Skip to content

Commit 285548e

Browse files
committed
Apply the joinLines transform to the _result_ of the regex match. That
way, we get even more consistent behavior.
1 parent 2075edf commit 285548e

1 file changed

Lines changed: 14 additions & 2 deletions

File tree

keepsorted/line_group.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,14 @@ func (lg *lineGroup) regexTokens() []regexToken {
429429
regexMatches = []regexMatch{{lg.internalJoinedLines()}}
430430
} else {
431431
regexMatches = lg.opts.matchRegexes(lg.regexJoinedLines())
432+
// We still want to apply the joinLines transform so that we get
433+
// "reasonable human" comparisons if the regex intentionally
434+
// (or accidentally!) matches more than one line.
435+
for _, match := range regexMatches {
436+
for i, s := range match {
437+
match[i] = joinLines(strings.Split(s, "\n"))
438+
}
439+
}
432440
}
433441

434442
ret := make([]regexToken, len(regexMatches))
@@ -473,15 +481,19 @@ func (lg *lineGroup) regexTokens() []regexToken {
473481
// Unlike joinedLines, this method does not record that it was used in the
474482
// accessRecorder.
475483
func (lg *lineGroup) internalJoinedLines() string {
476-
if len(lg.lines) == 0 {
484+
return joinLines(lg.lines)
485+
}
486+
487+
func joinLines(lines []string) string {
488+
if len(lines) == 0 {
477489
return ""
478490
}
479491

480492
endsWithWordChar := regexp.MustCompile(`\w$`)
481493
startsWithWordChar := regexp.MustCompile(`^\w`)
482494
var s strings.Builder
483495
var last string
484-
for _, l := range lg.lines {
496+
for _, l := range lines {
485497
l := strings.TrimLeftFunc(l, unicode.IsSpace)
486498
if len(last) > 0 && len(l) > 0 && endsWithWordChar.MatchString(last) && startsWithWordChar.MatchString(l) {
487499
s.WriteString(" ")

0 commit comments

Comments
 (0)