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

Function TestSplitRegisteredEngineCapturesAndErrors

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

Source from the content-addressed store, hash-verified

182}
183
184func TestSplitRegisteredEngineCapturesAndErrors(t *testing.T) {
185 failure := errors.New("engine failure")
186 re := newEngineRegexp("split-engine", newCompileConfig(nil), RuntimeEngineData{
187 CapSize: 2, LeftContextKnown: true,
188 StringPrefixFilter: stringIndexPrefixFilter(",", false, 1),
189 FindFirstChar: func(r *Runner) bool {
190 for r.Runtextpos < r.Runtextend {
191 if r.Runtext[r.Runtextpos] == ',' || r.Runtext[r.Runtextpos] == '!' {
192 return true
193 }
194 r.Runtextpos++
195 }
196 return false
197 },
198 Execute: func(r *Runner) error {
199 if r.Runtext[r.Runtextpos] == '!' {
200 return failure
201 }
202 start := r.Runtextpos
203 r.Runtextpos++
204 r.Capture(1, start, r.Runtextpos)
205 r.Capture(0, start, r.Runtextpos)
206 return nil
207 },
208 ExecuteQuick: func(r *Runner) error { t.Fatal("Split must retain captures"); return nil },
209 })
210 got, err := re.Split("é,界,😀", -1)
211 if err != nil || !slices.Equal(got, []string{"é", ",", "界", ",", "😀"}) {
212 t.Fatal(got, err)
213 }
214 // Preserve error propagation, including the existing look-ahead scan after
215 // the last processed delimiter when count is exhausted.
216 for _, count := range []int{-1, 2} {
217 if got, err := re.Split("a,b,c!", count); got != nil || !errors.Is(err, failure) {
218 t.Fatal(got, err)
219 }
220 }
221 got, err = re.Split("x,y", -1)
222 if err != nil || !slices.Equal(got, []string{"x", ",", "y"}) {
223 t.Fatal(got, err)
224 }
225}
226
227func TestStringProcessingAllocations(t *testing.T) {
228 skipIfAllocsUnreliable(t)

Callers

nothing calls this directly

Calls 5

newEngineRegexpFunction · 0.85
newCompileConfigFunction · 0.85
stringIndexPrefixFilterFunction · 0.85
CaptureMethod · 0.80
SplitMethod · 0.80

Tested by

no test coverage detected