Skip to content

Commit 68a1d1d

Browse files
committed
[zapx] Fixed longestCommonPrefOffset() +tests
1 parent a26458e commit 68a1d1d

File tree

2 files changed

+67
-8
lines changed

2 files changed

+67
-8
lines changed

internal/zapx/slowlog_enc.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,25 +44,25 @@ func (sle *SlowlogEncoder) addDir(p string) bool {
4444
return true
4545
}
4646

47-
func (sle *SlowlogEncoder) longestCommonPrefOffset() int {
48-
if len(sle.strBuf) <= 0 {
47+
func (sle *SlowlogEncoder) longestCommonPrefOffset(ss []string) int {
48+
if len(ss) <= 0 {
4949
return 0
5050
}
5151

52-
if len(sle.strBuf) == 1 {
53-
return len(sle.strBuf[0]) + 1
52+
if len(ss) == 1 {
53+
return len(ss[0])
5454
}
5555

56-
first := sle.strBuf[0]
57-
last := sle.strBuf[len(sle.strBuf)-1]
56+
first := ss[0]
57+
last := ss[len(ss)-1]
5858

5959
for i := 0; i < len(first); i++ {
6060
if last[i] != first[i] {
6161
return i
6262
}
6363
}
6464

65-
return 0
65+
return len(first)
6666
}
6767

6868
func (sle *SlowlogEncoder) encodeStacktraceEntry(encoder zapcore.ObjectEncoder, entry phpfpm.SlowlogTraceEntry, pathOffset int) {
@@ -101,7 +101,7 @@ func (sle *SlowlogEncoder) Encode(entry phpfpm.SlowlogEntry) []zap.Field {
101101

102102
pathOffset := 0
103103
if cutPrefix {
104-
pathOffset = sle.longestCommonPrefOffset()
104+
pathOffset = sle.longestCommonPrefOffset(sle.strBuf)
105105
}
106106

107107
return []zap.Field{

internal/zapx/slowlog_enc_test.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package zapx
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestSlowlogEncoder_LongestCommonPrefOffset(t *testing.T) {
8+
tests := []struct {
9+
name string
10+
input []string
11+
expected int
12+
}{
13+
{
14+
name: "empty slice",
15+
input: []string{},
16+
expected: 0,
17+
},
18+
{
19+
name: "single string",
20+
input: []string{"test"},
21+
expected: 4,
22+
},
23+
{
24+
name: "no common prefix",
25+
input: []string{"abc", "def", "ghi"},
26+
expected: 0,
27+
},
28+
{
29+
name: "partial common prefix",
30+
input: []string{"prefix123", "prefix456", "prefix789"},
31+
expected: 6,
32+
},
33+
{
34+
name: "full common string",
35+
input: []string{"same", "same", "same"},
36+
expected: 4,
37+
},
38+
{
39+
name: "mixed length strings with common prefix",
40+
input: []string{"test123", "test", "test4567"},
41+
expected: 4,
42+
},
43+
{
44+
name: "strings with spaces",
45+
input: []string{"common prefix 1", "common prefix 2", "common prefix 3"},
46+
expected: 14,
47+
},
48+
}
49+
50+
sle := &SlowlogEncoder{}
51+
for _, tt := range tests {
52+
t.Run(tt.name, func(t *testing.T) {
53+
result := sle.longestCommonPrefOffset(tt.input)
54+
if result != tt.expected {
55+
t.Errorf("longestCommonPrefOffset() = %v, want %v", result, tt.expected)
56+
}
57+
})
58+
}
59+
}

0 commit comments

Comments
 (0)