Commit bd7a6cef

bd7a6cef24f8Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 8 months ago · 7 files · +3 added · ~4 modified

Description

feat: add StringIO as built-in type (SPINEL_TYPE_STRINGIO)

Implement StringIO as a native Spinel type with inline C functions, following the same pattern as sp_File. Methods supported: new, string, pos, tell, size, length, lineno, write, puts, print, putc, read, read(n), gets, getc, getbyte, rewind, seek, eof?, truncate, close, closed?, flush, sync, isatty, tty?, fileno, <<

Also fix: fill default values for missing optional positional params in class method calls (previously only keyword args were handled).

60/60 tests, 39/39 benchmarks pass.

Co-Authored-By: Claude Opus 4.6 (1M context) [email protected]

Details

Commit
bd7a6cef24f8
Parent
21664a724919

lib/stringio.caddedView file

1+/*
2+ * stringio.c — StringIO C implementation for Spinel
3+ *
4+ * Provides sp_StringIO_* functions that the generated C code calls.
5+ * In-memory IO object with a growable string buffer.
6+ *
7+ * Usage: cc -O2 app.c lib/stringio.c -lm -o app
8+ */
9+
10+#include <stdio.h>
11+#include <stdlib.h>
12+#include <string.h>
13+#include <stdint.h>
14+#include <stdarg.h>
15+
16+typedef struct {
17+ char *buf; /* heap-allocated buffer */
18+ int64_t len; /* current string length */
19+ int64_t cap; /* buffer capacity */
20+ int64_t pos; /* current read/write position */
21+ int64_t lineno; /* line number counter (for gets) */
22+ int closed; /* 1 if closed */
23+} sp_StringIO;
24+
25+/* Internal: ensure buffer has room for `need` more bytes */
26+static void sio_grow(sp_StringIO *sio, int64_t need) {
27+ int64_t required = sio->pos + need;
28+ if (required <= sio->cap) return;
29+ int64_t new_cap = sio->cap ? sio->cap : 64;
30+ while (new_cap < required) new_cap *= 2;
31+ sio->buf = (char *)realloc(sio->buf, new_cap + 1);
32+ sio->cap = new_cap;
33+}
34+
35+/* Internal: write bytes at current position, extending buffer as needed */
36+static int64_t sio_write(sp_StringIO *sio, const char *data, int64_t data_len) {
37+ sio_grow(sio, data_len);
38+ /* Fill gap with zeros if pos > len */
39+ if (sio->pos > sio->len) {
40+ memset(sio->buf + sio->len, 0, sio->pos - sio->len);
41+ }
42+ memcpy(sio->buf + sio->pos, data, data_len);
43+ sio->pos += data_len;
44+ if (sio->pos > sio->len) sio->len = sio->pos;
45+ sio->buf[sio->len] = '\0';
46+ return data_len;
47+}
48+
49+/* Constructor: StringIO.new or StringIO.new("initial") */
50+sp_StringIO *sp_StringIO_new(void) {
51+ sp_StringIO *sio = (sp_StringIO *)calloc(1, sizeof(sp_StringIO));
52+ sio->buf = (char *)calloc(1, 64);
53+ sio->cap = 63;
54+ return sio;
55+}
56+
57+sp_StringIO *sp_StringIO_new_s(const char *initial) {
58+ sp_StringIO *sio = (sp_StringIO *)calloc(1, sizeof(sp_StringIO));
59+ int64_t len = (int64_t)strlen(initial);
60+ int64_t cap = len < 63 ? 63 : len;
61+ sio->buf = (char *)malloc(cap + 1);
62+ memcpy(sio->buf, initial, len);
63+ sio->buf[len] = '\0';
64+ sio->len = len;
65+ sio->cap = cap;
66+ sio->pos = 0;
67+ return sio;
68+}
69+
70+/* string — return the buffer contents as a C string */
71+const char *sp_StringIO_string(sp_StringIO *sio) {
72+ return sio->buf ? sio->buf : "";
73+}
74+
75+/* pos / tell — current position */
76+int64_t sp_StringIO_pos(sp_StringIO *sio) {
77+ return sio->pos;
78+}
79+
80+/* pos= — set position */
81+int64_t sp_StringIO_set_pos(sp_StringIO *sio, int64_t new_pos) {
82+ if (new_pos < 0) new_pos = 0;
83+ sio->pos = new_pos;
84+ return sio->pos;
85+}
86+
87+/* lineno */
88+int64_t sp_StringIO_lineno(sp_StringIO *sio) {
89+ return sio->lineno;
90+}
91+
92+/* size / length — string length */
93+int64_t sp_StringIO_size(sp_StringIO *sio) {
94+ return sio->len;
95+}
96+
97+/* write(str) — write string at current position, return bytes written */
98+int64_t sp_StringIO_write(sp_StringIO *sio, const char *str) {
99+ return sio_write(sio, str, (int64_t)strlen(str));
100+}
101+
102+/* puts(str) — write string + newline */
103+int64_t sp_StringIO_puts(sp_StringIO *sio, const char *str) {
104+ int64_t slen = (int64_t)strlen(str);
105+ sio_write(sio, str, slen);
106+ /* Add newline unless string already ends with one */
107+ if (slen == 0 || str[slen - 1] != '\n')
108+ sio_write(sio, "\n", 1);
109+ return 0; /* Ruby puts returns nil */
110+}
111+
112+/* puts() with no args — just a newline */
113+int64_t sp_StringIO_puts_empty(sp_StringIO *sio) {
114+ sio_write(sio, "\n", 1);
115+ return 0;
116+}
117+
118+/* print(str) — write string without newline */
119+int64_t sp_StringIO_print(sp_StringIO *sio, const char *str) {
120+ return sio_write(sio, str, (int64_t)strlen(str));
121+}
122+
123+/* putc(ch) — write a single character (integer or first char of string) */
124+int64_t sp_StringIO_putc(sp_StringIO *sio, int64_t ch) {
125+ char c = (char)(ch & 0xFF);
126+ sio_write(sio, &c, 1);
127+ return ch;
128+}
129+
130+/* << operator — append, return self (for chaining) */
131+sp_StringIO *sp_StringIO_append(sp_StringIO *sio, const char *str) {
132+ sio_write(sio, str, (int64_t)strlen(str));
133+ return sio;
134+}
135+
136+/* read() — read from pos to end */
137+const char *sp_StringIO_read(sp_StringIO *sio) {
138+ if (sio->pos >= sio->len) return "";
139+ const char *result = sio->buf + sio->pos;
140+ sio->pos = sio->len;
141+ return result;
142+}
143+
144+/* read(length) — read up to length bytes */
145+const char *sp_StringIO_read_n(sp_StringIO *sio, int64_t length) {
146+ if (sio->pos >= sio->len) return NULL;
147+ int64_t remaining = sio->len - sio->pos;
148+ if (length > remaining) length = remaining;
149+ /* Return a temporary substring — caller should copy if needed */
150+ static char read_buf[65536];
151+ if (length >= (int64_t)sizeof(read_buf)) length = sizeof(read_buf) - 1;
152+ memcpy(read_buf, sio->buf + sio->pos, length);
153+ read_buf[length] = '\0';
154+ sio->pos += length;
155+ return read_buf;
156+}
157+
158+/* gets — read one line (up to and including \n), return NULL at EOF */
159+const char *sp_StringIO_gets(sp_StringIO *sio) {
160+ if (sio->pos >= sio->len) return NULL;
161+ const char *start = sio->buf + sio->pos;
162+ const char *nl = memchr(start, '\n', sio->len - sio->pos);
163+ int64_t line_len;
164+ if (nl) {
165+ line_len = (nl - start) + 1;
166+ } else {
167+ line_len = sio->len - sio->pos;
168+ }
169+ /* Return a temporary buffer */
170+ static char gets_buf[65536];
171+ if (line_len >= (int64_t)sizeof(gets_buf)) line_len = sizeof(gets_buf) - 1;
172+ memcpy(gets_buf, start, line_len);
173+ gets_buf[line_len] = '\0';
174+ sio->pos += line_len;
175+ sio->lineno++;
176+ return gets_buf;
177+}
178+
179+/* getc — read one character, return NULL at EOF */
180+const char *sp_StringIO_getc(sp_StringIO *sio) {
181+ if (sio->pos >= sio->len) return NULL;
182+ static char getc_buf[2];
183+ getc_buf[0] = sio->buf[sio->pos++];
184+ getc_buf[1] = '\0';
185+ return getc_buf;
186+}
187+
188+/* getbyte — read one byte as integer, return -1 at EOF */
189+int64_t sp_StringIO_getbyte(sp_StringIO *sio) {
190+ if (sio->pos >= sio->len) return -1;
191+ return (int64_t)(unsigned char)sio->buf[sio->pos++];
192+}
193+
194+/* rewind — reset position and lineno to 0 */
195+int64_t sp_StringIO_rewind(sp_StringIO *sio) {
196+ sio->pos = 0;
197+ sio->lineno = 0;
198+ return 0;
199+}
200+
201+/* seek(offset, whence) — set position, whence: 0=SET, 1=CUR, 2=END */
202+int64_t sp_StringIO_seek(sp_StringIO *sio, int64_t offset, int64_t whence) {
203+ int64_t new_pos;
204+ switch (whence) {
205+ case 1: new_pos = sio->pos + offset; break; /* SEEK_CUR */
206+ case 2: new_pos = sio->len + offset; break; /* SEEK_END */
207+ default: new_pos = offset; break; /* SEEK_SET */
208+ }
209+ if (new_pos < 0) new_pos = 0;
210+ sio->pos = new_pos;
211+ return 0;
212+}
213+
214+/* eof? — true if at or past end */
215+int sp_StringIO_eof_p(sp_StringIO *sio) {
216+ return sio->pos >= sio->len;
217+}
218+
219+/* truncate(length) — truncate buffer to given length */
220+int64_t sp_StringIO_truncate(sp_StringIO *sio, int64_t length) {
221+ if (length < 0) length = 0;
222+ if (length < sio->len) {
223+ sio->len = length;
224+ sio->buf[length] = '\0';
225+ }
226+ return 0;
227+}
228+
229+/* close — mark as closed */
230+int64_t sp_StringIO_close(sp_StringIO *sio) {
231+ sio->closed = 1;
232+ return 0;
233+}
234+
235+/* closed? */
236+int sp_StringIO_closed_p(sp_StringIO *sio) {
237+ return sio->closed;
238+}
239+
240+/* flush — no-op for StringIO, return self */
241+sp_StringIO *sp_StringIO_flush(sp_StringIO *sio) {
242+ return sio;
243+}
244+
245+/* sync — always true */
246+int sp_StringIO_sync(sp_StringIO *sio) {
247+ (void)sio;
248+ return 1;
249+}
250+
251+/* isatty / tty? — always false */
252+int sp_StringIO_isatty(sp_StringIO *sio) {
253+ (void)sio;
254+ return 0;
255+}
256+
257+/* fileno — nil (returns -1 for C) */
258+int64_t sp_StringIO_fileno(sp_StringIO *sio) {
259+ (void)sio;
260+ return -1;
261+}
262+
263+/* printf(fmt, ...) — formatted print to buffer */
264+int64_t sp_StringIO_printf(sp_StringIO *sio, const char *fmt, ...) {
265+ char tmp[8192];
266+ va_list ap;
267+ va_start(ap, fmt);
268+ int n = vsnprintf(tmp, sizeof(tmp), fmt, ap);
269+ va_end(ap);
270+ if (n > 0) sio_write(sio, tmp, n);
271+ return n;
272+}
273+
274+/* string= — replace buffer contents */
275+void sp_StringIO_set_string(sp_StringIO *sio, const char *str) {
276+ int64_t len = (int64_t)strlen(str);
277+ if (len > sio->cap) {
278+ sio->cap = len;
279+ sio->buf = (char *)realloc(sio->buf, sio->cap + 1);
280+ }
281+ memcpy(sio->buf, str, len);
282+ sio->buf[len] = '\0';
283+ sio->len = len;
284+ sio->pos = 0;
285+}