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

Function TestReplaceStringFastPaths

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

Source from the content-addressed store, hash-verified

76}
77
78func TestReplaceStringFastPaths(t *testing.T) {
79 for _, option := range []RegexOptions{None, RightToLeft} {
80 for _, input := range []string{"xxneedlexxneedlexx", "éneedle界needle😀", "\xffneedle\x80needle\xe2\x82"} {
81 re := MustCompile(`needle`, option)
82 got, err := re.Replace(input, "pin", -1, -1)
83 // Pattern replacement has always re-encoded unmatched invalid UTF-8.
84 want := strings.ReplaceAll(string([]rune(input)), "needle", "pin")
85 if err != nil || got != want {
86 t.Fatalf("RTL=%v input=%q: got %q,%v; want %q", re.RightToLeft(), input, got, err, want)
87 }
88 }
89 }
90 for _, tc := range []struct {
91 pattern, input, replacement, want string
92 start, count int
93 }{
94 {`needle`, "prefixneedleTAIL", "$`|$&|$'|$_", "prefixprefix|needle|TAIL|prefixneedleTAILTAIL", -1, -1},
95 {`(?<=prefix)(needle)`, "prefixneedleTAIL", "[$1]", "prefix[needle]TAIL", -1, -1},
96 {`\Gneedle`, "xxneedleneedleTAIL", "pin", "xxpinpinTAIL", 2, -1},
97 {`needle`, "xxneedleneedleTAIL", "pin", "xxpinneedleTAIL", 0, 1},
98 {`needle`, "\xffnone\x80", "pin", "\xffnone\x80", -1, -1},
99 {`aaba|aaca|bada`, strings.Repeat("a", 80) + "ca?bada!", "[$&]", strings.Repeat("a", 78) + "[aaca]?[bada]!", -1, -1},
100 {`(?i)ab`, "aX AB!ab?", "[$&]", "aX [AB]![ab]?", -1, -1},
101 {`[ACEGIK]{2,4}`, "A!ACE?IK", "[$&]", "A![ACE]?[IK]", -1, -1},
102 {`aa[ \n]$`, "aaa\n", "[$&]", "a[aa\n]", -1, -1},
103 {`aaba|aaca|bada`, strings.Repeat("a", 80), "[$&]", strings.Repeat("a", 80), -1, -1},
104 } {
105 re := MustCompile(tc.pattern)
106 got, err := re.Replace(tc.input, tc.replacement, tc.start, tc.count)
107 if err != nil || got != tc.want {
108 t.Fatalf("%s on %q: got %q,%v; want %q", tc.pattern, tc.input, got, err, tc.want)
109 }
110 }
111 re := MustCompile(`needle`)
112 for _, input := range []string{"é-no-match", strings.Repeat("é", 64)} {
113 for _, start := range []int{1, len(input) + 1} {
114 if _, err := re.Replace(input, "pin", start, -1); err == nil {
115 t.Fatalf("startAt=%d: expected validation error before rejecting miss", start)
116 }
117 }
118 }
119}
120
121func TestSplitReusableCaptures(t *testing.T) {
122 for _, tc := range []struct {

Callers

nothing calls this directly

Calls 3

ReplaceMethod · 0.80
RightToLeftMethod · 0.80
MustCompileFunction · 0.70

Tested by

no test coverage detected