Commit 24ec1cf6

24ec1cf637a4Merged

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

Description

codegen,runtime: clang-clean string literals + base-0 to_i + each_line newlines

Three independent fixes squashed because they share the runtime helper and codegen string-emit paths:

  • clang's CI build rejected raw high bytes (>=0x80) in source with -Winvalid-source-encoding. c_string_literal now emits each such byte as \xHH (with a string-concat break before any following hex digit) so the source is pure ASCII.

  • String#to_i(0) auto-detects the base from the literal prefix (0x → 16, 0b → 2, 0/0o → 8, 0d → 10, otherwise decimal). Fixes #927.

  • String#each_line yields each line WITH its trailing newline, matching CRuby. Pre-fix relied on sp_str_split which strips the delimiter. Fixes #922.

Also updates test/str_to_i_overflow expectations to the RangeError-raising semantics introduced by #842.

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

Details

Commit
24ec1cf637a4
Parent
7c38a7c82a6f

lib/sp_runtime.hmodifiedView file

@@ -266,15 +266,28 @@
266266 far. Issue #883. */
267267 static mrb_int sp_str_to_i_base(const char *s, mrb_int base) {
268268 if (!s) return 0;
269- if (base < 2 || base > 36) base = 10;
269+ /* base 0 = auto-detect from prefix (0x → 16, 0b → 2, 0/0o → 8,
270+ otherwise 10). Per CRuby, only base 0 enables prefix-based
271+ dispatch — explicit bases just *accept* the matching prefix. */
272+ if (base != 0 && (base < 2 || base > 36)) base = 10;
270273 const char *p = s;
271274 while (isspace((unsigned char)*p)) p++;
272275 int neg = 0;
273276 if (*p == '+') p++;
274277 else if (*p == '-') { neg = 1; p++; }
275- /* Optional `0x` / `0b` / `0o` prefix matching the explicit base.
276- CRuby's String#to_i(base) accepts these. */
277- if (*p == '0' && p[1] != 0) {
278+ if (base == 0) {
279+ if (*p == '0') {
280+ if (p[1] == 'x' || p[1] == 'X') { base = 16; p += 2; }
281+ else if (p[1] == 'b' || p[1] == 'B') { base = 2; p += 2; }
282+ else if (p[1] == 'o' || p[1] == 'O') { base = 8; p += 2; }
283+ else if (p[1] == 'd' || p[1] == 'D') { base = 10; p += 2; }
284+ else if (p[1] >= '0' && p[1] <= '7') { base = 8; p++; }
285+ else { base = 10; }
286+ } else {
287+ base = 10;
288+ }
289+ } else if (*p == '0' && p[1] != 0) {
290+ /* Explicit base accepts the matching prefix. */
278291 if ((base == 16) && (p[1] == 'x' || p[1] == 'X')) p += 2;
279292 else if ((base == 2) && (p[1] == 'b' || p[1] == 'B')) p += 2;
280293 else if ((base == 8) && (p[1] == 'o' || p[1] == 'O')) p += 2;