MCPcopy Create free account
hub / github.com/dlclark/regexp2 / TestStringCaptureByteOffsets

Function TestStringCaptureByteOffsets

string_processing_test.go:48–76  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

46}
47
48func TestStringCaptureByteOffsets(t *testing.T) {
49 for _, input := range []string{
50 strings.Repeat("a", 128), strings.Repeat("界😀", 64),
51 strings.Repeat("\xff\x80", 64), "a\xffé\xc0\x80界\xe2\x82",
52 } {
53 t.Run(fmt.Sprintf("%x", input[:min(8, len(input))]), func(t *testing.T) {
54 offsets := make([]int, 0, len(input)+1)
55 for i := range input {
56 offsets = append(offsets, i)
57 }
58 offsets = append(offsets, len(input))
59 re := MustCompile(`(.)+`)
60 m, err := re.FindStringMatch(input)
61 if err != nil || m == nil {
62 t.Fatal(m, err)
63 }
64 captures := m.GroupByNumber(1).Captures
65 if len(captures) != len(offsets)-1 {
66 t.Fatal(len(captures), offsets)
67 }
68 for i := len(captures) - 1; i >= 0; i-- {
69 start, length := captures[i].ByteRange()
70 if start != offsets[i] || length != offsets[i+1]-offsets[i] || captures[i].String() != input[offsets[i]:offsets[i+1]] {
71 t.Fatalf("capture %d: %d,%d %q", i, start, length, captures[i].String())
72 }
73 }
74 })
75 }
76}
77
78func TestReplaceStringFastPaths(t *testing.T) {
79 for _, option := range []RegexOptions{None, RightToLeft} {

Callers

nothing calls this directly

Calls 6

minFunction · 0.85
FindStringMatchMethod · 0.80
GroupByNumberMethod · 0.80
MustCompileFunction · 0.70
ByteRangeMethod · 0.65
StringMethod · 0.45

Tested by

no test coverage detected