Commit 9ea43fe9

9ea43fe98698Merged

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

Description

Honour the mode argument in StringIO.new(str, mode)

StringIO.new(str, mode) ignored the mode: the codegen arm only ever passed the first arg to sp_StringIO_new_s, so the content was always kept and writable regardless of mode.

Add sp_StringIO_new_sm(init, mode), keyed on the mode's first char: "w"/"w+" truncate to empty, "a"/"a+" keep the content and seek to the end (writes append), "r"/"r+" and anything else keep the content at position 0. Codegen factors the StringIO construction into compile_stringio_construct, which routes the two-positional-arg form (when the second arg is a string mode) to the new helper.

StringIO.new("existing", "w").string # => "" (was "existing") io = StringIO.new("head", "a"); io.print("tail") io.string # => "headtail" (was "tail")

Read-only enforcement ("r" rejecting writes) is not modelled, and the init string is copied rather than shared by reference (so the passed-in variable is not mutated as in CRuby) -- both pre-existing and out of scope here.

Fixes #1138

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

Details

Commit
9ea43fe98698
Parent
b43af50740fb

lib/sp_runtime.hmodifiedView file

@@ -3769,6 +3769,20 @@
37693769 static int64_t sio_write(sp_StringIO *sio, const char *d, int64_t dl) { sio_grow(sio, dl); if (sio->pos > sio->len) memset(sio->buf + sio->len, 0, sio->pos - sio->len); memcpy(sio->buf + sio->pos, d, dl); sio->pos += dl; if (sio->pos > sio->len) sio->len = sio->pos; sio->buf[sio->len] = '\0'; return dl; }
37703770 static sp_StringIO *sp_StringIO_new(void) { sp_StringIO *s = (sp_StringIO *)calloc(1, sizeof(sp_StringIO)); s->buf = (char *)calloc(1, 64); s->cap = 63; return s; }
37713771 static sp_StringIO *sp_StringIO_new_s(const char *init) { if (!init) sp_raise_cls("TypeError", "no implicit conversion of nil into String"); sp_StringIO *s = (sp_StringIO *)calloc(1, sizeof(sp_StringIO)); int64_t l = (int64_t)strlen(init); int64_t c = l < 63 ? 63 : l; s->buf = (char*)malloc(c+1); memcpy(s->buf, init, l); s->buf[l]='\0'; s->len = l; s->cap = c; return s; }
3772+/* StringIO.new(str, mode): the mode's first char selects the initial
3773+ content/position. "w"/"w+" truncate to empty; "a"/"a+" keep the
3774+ content and seek to the end (subsequent writes append); "r"/"r+" and
3775+ anything else keep the content at position 0. Read-only enforcement
3776+ ("r" rejecting writes) is not modelled. */
3777+static sp_StringIO *sp_StringIO_new_sm(const char *init, const char *mode) {
3778+ if (!init) sp_raise_cls("TypeError", "no implicit conversion of nil into String");
3779+ if (!mode || !mode[0]) return sp_StringIO_new_s(init);
3780+ char m0 = mode[0];
3781+ if (m0 == 'w') return sp_StringIO_new();
3782+ sp_StringIO *s = sp_StringIO_new_s(init);
3783+ if (m0 == 'a') s->pos = s->len;
3784+ return s;
3785+}
37723786 static const char *sp_StringIO_string(sp_StringIO *s) { return s->buf ? s->buf : sp_str_empty; }
37733787 static int64_t sp_StringIO_pos(sp_StringIO *s) { return s->pos; }
37743788 static int64_t sp_StringIO_size(sp_StringIO *s) { return s->len; }