Skip to content

Commit 691b318

Browse files
authored
Merge pull request #1160 from VXNCXNX/fix/search-skips-quoted-section
fix: plain text search skips a quoted section after a match
2 parents febdc0a + ed913c4 commit 691b318

3 files changed

Lines changed: 61 additions & 9 deletions

File tree

oviewer/search_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -798,6 +798,35 @@ func Test_sensitive_FindAll(t *testing.T) {
798798
},
799799
want: [][]int{{0, 3}},
800800
},
801+
{
802+
// A double quote is an ordinary character in a plain text search,
803+
// so a match immediately before one must not hide the matches
804+
// inside the quoted section.
805+
name: "testQuoted",
806+
fields: fields{
807+
searchWord: "=",
808+
searchReg: regexpCompile("=", true),
809+
caseSensitive: true,
810+
regexpSearch: false,
811+
},
812+
args: args{
813+
s: `msg="a=b" level=info`,
814+
},
815+
want: [][]int{{3, 4}, {6, 7}, {15, 16}},
816+
},
817+
{
818+
name: "testQuotedInsensitive",
819+
fields: fields{
820+
searchWord: "A",
821+
searchReg: regexpCompile("A", false),
822+
caseSensitive: false,
823+
regexpSearch: false,
824+
},
825+
args: args{
826+
s: `a"a"a`,
827+
},
828+
want: [][]int{{0, 1}, {2, 3}, {4, 5}},
829+
},
801830
}
802831
for _, tt := range tests {
803832
t.Run(tt.name, func(t *testing.T) {

oviewer/utils.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -102,24 +102,18 @@ func skipQuoted(s string, offSet int) (string, int) {
102102
}
103103

104104
// allStringIndex returns all matching string positions.
105+
// It is used by the plain text search, where a double quote is an ordinary
106+
// character, so every occurrence is reported.
105107
func allStringIndex(s string, substr string) [][]int {
106108
if len(substr) == 0 {
107109
return nil
108110
}
109111
var result [][]int
110112
width := len(substr)
111-
for pos, offSet := strings.Index(s, substr), 0; pos != -1; {
113+
for pos, offSet := strings.Index(s, substr), 0; pos != -1; pos = strings.Index(s, substr) {
112114
s = s[pos+width:]
113115
result = append(result, []int{pos + offSet, pos + offSet + width})
114116
offSet += pos + width
115-
116-
if len(s) > 0 && s[0] == '"' {
117-
qpos := strings.Index(s[1:], `"`)
118-
s = s[qpos+2:]
119-
offSet += qpos + 2
120-
}
121-
122-
pos = strings.Index(s, substr)
123117
}
124118
return result
125119
}

oviewer/utils_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,13 +481,16 @@ func Test_allStringIndex(t *testing.T) {
481481
want: nil,
482482
},
483483
{
484+
// A double quote has no special meaning here, so the delimiter
485+
// inside the quoted field is reported as well.
484486
name: "testDoubleQuote",
485487
args: args{
486488
s: `a,"b,c",d`,
487489
substr: ",",
488490
},
489491
want: [][]int{
490492
{1, 2},
493+
{4, 5},
491494
{7, 8},
492495
},
493496
},
@@ -502,6 +505,32 @@ func Test_allStringIndex(t *testing.T) {
502505
{9, 10},
503506
},
504507
},
508+
{
509+
// A match immediately followed by a double quote must not hide the
510+
// matches inside the quoted section.
511+
name: "testMatchBeforeQuote",
512+
args: args{
513+
s: `msg="a=b" level=info`,
514+
substr: "=",
515+
},
516+
want: [][]int{
517+
{3, 4},
518+
{6, 7},
519+
{15, 16},
520+
},
521+
},
522+
{
523+
name: "testMatchInsideQuote",
524+
args: args{
525+
s: `a"a"a`,
526+
substr: "a",
527+
},
528+
want: [][]int{
529+
{0, 1},
530+
{2, 3},
531+
{4, 5},
532+
},
533+
},
505534
}
506535
for _, tt := range tests {
507536
t.Run(tt.name, func(t *testing.T) {

0 commit comments

Comments
 (0)