Commit 4d7c5ef3
4d7c5ef3aafbMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 2 files · +1 added · ~1 modified
Description
runtime: String#split with capture group splices captures into result
"hello world".split(/(\s+)/) used to drop the captured space.
After each match, push each captured group between the
surrounding segments to match CRuby's documented behaviour.
Fixes #856
Co-Authored-By: Claude Opus 4.7 [email protected]
Details
- Commit
- 4d7c5ef3aafb
- Parent
- fea6561a6e40
lib/sp_runtime.hmodifiedView file
@@ -2141,6 +2141,20 @@
21412141
}21422142
int len = caps[0] - pos; char *m = sp_str_alloc_raw(len+1);21432143
memcpy(m, str+pos, len); m[len] = 0; sp_StrArray_push(arr, m);2144+
/* Issue #856: when the splitter regex has capture groups,2145+
Ruby splices each captured substring into the result2146+
between the surrounding segments (caps[2..n-1] hold groups2147+
1..(n/2-1); group 0 is the whole match). */2148+
for (int gi = 1; gi * 2 + 1 < n; gi++) {2149+
if (caps[gi*2] >= 0 && caps[gi*2+1] >= 0) {2150+
int glen = caps[gi*2+1] - caps[gi*2];2151+
char *gm = sp_str_alloc_raw(glen+1);2152+
memcpy(gm, str + caps[gi*2], glen); gm[glen] = 0;2153+
sp_StrArray_push(arr, gm);2154+
} else {2155+
sp_StrArray_push(arr, sp_str_empty);2156+
}2157+
}21442158
pos = caps[1]; if (caps[0] == caps[1]) pos++;21452159
}21462160
return arr;test/string_split_capture.rbaddedView file
Loading diff…