Commit 7a753885

7a753885870fMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 2 files · +1 added · ~1 modified

Description

Save last_pos in StringScanner#check for correct unscan

CRuby's check matches at the current position without advancing, but it still records the pre-match position, so a following unscan rewinds to just before the check (and pre_match/post_match reflect the check match). Spinel's check skipped sc->last_pos = sc->pos, so after a scan + check an unscan jumped back to the position saved by the earlier scan, often the start of the string.

Set last_pos on a successful check match, mirroring scan. A failed check leaves last_pos untouched, matching CRuby (where unscan after a failed match raises rather than rewinding).

Fixes #1266

Co-Authored-By: Claude Opus 4.8 [email protected]

Details

Commit
7a753885870f
Parent
831c2911d34e

lib/sp_strscan.cmodifiedView file

@@ -135,6 +135,7 @@
135135 return sp_ext_str_empty();
136136 }
137137 char *m = sc_substr(sc->source, sc->pos, mlen);
138+ sc->last_pos = sc->pos;
138139 sc->matched = m;
139140 sc->matched_p = 1;
140141 return m;