Commit aa76e864
aa76e864e890Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 8 months ago · 1 file · ~1 modified
Description
style: apply 2-space indent and newline before else to lib/stringio.c
Co-Authored-By: Claude Opus 4.6 (1M context) [email protected]
Details
- Commit
- aa76e864e890
- Parent
- 03bb7b5964af
lib/stringio.cmodifiedView file
@@ -14,272 +14,273 @@
1414
#include <stdarg.h>1515
1616
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 */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 */2323
} sp_StringIO;2424
2525
/* Internal: ensure buffer has room for `need` more bytes */2626
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;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;3333
}3434
3535
/* Internal: write bytes at current position, extending buffer as needed */3636
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;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;4747
}4848
4949
/* Constructor: StringIO.new or StringIO.new("initial") */5050
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;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;5555
}5656
5757
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;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;6868
}6969
7070
/* string — return the buffer contents as a C string */7171
const char *sp_StringIO_string(sp_StringIO *sio) {72-
return sio->buf ? sio->buf : "";72+
return sio->buf ? sio->buf : "";7373
}7474
7575
/* pos / tell — current position */7676
int64_t sp_StringIO_pos(sp_StringIO *sio) {77-
return sio->pos;77+
return sio->pos;7878
}7979
8080
/* pos= — set position */8181
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;82+
if (new_pos < 0) new_pos = 0;83+
sio->pos = new_pos;84+
return sio->pos;8585
}8686
8787
/* lineno */8888
int64_t sp_StringIO_lineno(sp_StringIO *sio) {89-
return sio->lineno;89+
return sio->lineno;9090
}9191
9292
/* size / length — string length */9393
int64_t sp_StringIO_size(sp_StringIO *sio) {94-
return sio->len;94+
return sio->len;9595
}9696
9797
/* write(str) — write string at current position, return bytes written */9898
int64_t sp_StringIO_write(sp_StringIO *sio, const char *str) {99-
return sio_write(sio, str, (int64_t)strlen(str));99+
return sio_write(sio, str, (int64_t)strlen(str));100100
}101101
102102
/* puts(str) — write string + newline */103103
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 */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 */110110
}111111
112112
/* puts() with no args — just a newline */113113
int64_t sp_StringIO_puts_empty(sp_StringIO *sio) {114-
sio_write(sio, "\n", 1);115-
return 0;114+
sio_write(sio, "\n", 1);115+
return 0;116116
}117117
118118
/* print(str) — write string without newline */119119
int64_t sp_StringIO_print(sp_StringIO *sio, const char *str) {120-
return sio_write(sio, str, (int64_t)strlen(str));120+
return sio_write(sio, str, (int64_t)strlen(str));121121
}122122
123123
/* putc(ch) — write a single character (integer or first char of string) */124124
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;125+
char c = (char)(ch & 0xFF);126+
sio_write(sio, &c, 1);127+
return ch;128128
}129129
130130
/* << operator — append, return self (for chaining) */131131
sp_StringIO *sp_StringIO_append(sp_StringIO *sio, const char *str) {132-
sio_write(sio, str, (int64_t)strlen(str));133-
return sio;132+
sio_write(sio, str, (int64_t)strlen(str));133+
return sio;134134
}135135
136136
/* read() — read from pos to end */137137
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;138+
if (sio->pos >= sio->len) return "";139+
const char *result = sio->buf + sio->pos;140+
sio->pos = sio->len;141+
return result;142142
}143143
144144
/* read(length) — read up to length bytes */145145
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;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;156156
}157157
158158
/* gets — read one line (up to and including \n), return NULL at EOF */159159
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;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+
}167+
else {168+
line_len = sio->len - sio->pos;169+
}170+
/* Return a temporary buffer */171+
static char gets_buf[65536];172+
if (line_len >= (int64_t)sizeof(gets_buf)) line_len = sizeof(gets_buf) - 1;173+
memcpy(gets_buf, start, line_len);174+
gets_buf[line_len] = '\0';175+
sio->pos += line_len;176+
sio->lineno++;177+
return gets_buf;177178
}178179
179180
/* getc — read one character, return NULL at EOF */180181
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;182+
if (sio->pos >= sio->len) return NULL;183+
static char getc_buf[2];184+
getc_buf[0] = sio->buf[sio->pos++];185+
getc_buf[1] = '\0';186+
return getc_buf;186187
}187188
188189
/* getbyte — read one byte as integer, return -1 at EOF */189190
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++];191+
if (sio->pos >= sio->len) return -1;192+
return (int64_t)(unsigned char)sio->buf[sio->pos++];192193
}193194
194195
/* rewind — reset position and lineno to 0 */195196
int64_t sp_StringIO_rewind(sp_StringIO *sio) {196-
sio->pos = 0;197-
sio->lineno = 0;198-
return 0;197+
sio->pos = 0;198+
sio->lineno = 0;199+
return 0;199200
}200201
201202
/* seek(offset, whence) — set position, whence: 0=SET, 1=CUR, 2=END */202203
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;204+
int64_t new_pos;205+
switch (whence) {206+
case 1: new_pos = sio->pos + offset; break; /* SEEK_CUR */207+
case 2: new_pos = sio->len + offset; break; /* SEEK_END */208+
default: new_pos = offset; break; /* SEEK_SET */209+
}210+
if (new_pos < 0) new_pos = 0;211+
sio->pos = new_pos;212+
return 0;212213
}213214
214215
/* eof? — true if at or past end */215216
int sp_StringIO_eof_p(sp_StringIO *sio) {216-
return sio->pos >= sio->len;217+
return sio->pos >= sio->len;217218
}218219
219220
/* truncate(length) — truncate buffer to given length */220221
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;222+
if (length < 0) length = 0;223+
if (length < sio->len) {224+
sio->len = length;225+
sio->buf[length] = '\0';226+
}227+
return 0;227228
}228229
229230
/* close — mark as closed */230231
int64_t sp_StringIO_close(sp_StringIO *sio) {231-
sio->closed = 1;232-
return 0;232+
sio->closed = 1;233+
return 0;233234
}234235
235236
/* closed? */236237
int sp_StringIO_closed_p(sp_StringIO *sio) {237-
return sio->closed;238+
return sio->closed;238239
}239240
240241
/* flush — no-op for StringIO, return self */241242
sp_StringIO *sp_StringIO_flush(sp_StringIO *sio) {242-
return sio;243+
return sio;243244
}244245
245246
/* sync — always true */246247
int sp_StringIO_sync(sp_StringIO *sio) {247-
(void)sio;248-
return 1;248+
(void)sio;249+
return 1;249250
}250251
251252
/* isatty / tty? — always false */252253
int sp_StringIO_isatty(sp_StringIO *sio) {253-
(void)sio;254-
return 0;254+
(void)sio;255+
return 0;255256
}256257
257258
/* fileno — nil (returns -1 for C) */258259
int64_t sp_StringIO_fileno(sp_StringIO *sio) {259-
(void)sio;260-
return -1;260+
(void)sio;261+
return -1;261262
}262263
263264
/* printf(fmt, ...) — formatted print to buffer */264265
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;266+
char tmp[8192];267+
va_list ap;268+
va_start(ap, fmt);269+
int n = vsnprintf(tmp, sizeof(tmp), fmt, ap);270+
va_end(ap);271+
if (n > 0) sio_write(sio, tmp, n);272+
return n;272273
}273274
274275
/* string= — replace buffer contents */275276
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;277+
int64_t len = (int64_t)strlen(str);278+
if (len > sio->cap) {279+
sio->cap = len;280+
sio->buf = (char *)realloc(sio->buf, sio->cap + 1);281+
}282+
memcpy(sio->buf, str, len);283+
sio->buf[len] = '\0';284+
sio->len = len;285+
sio->pos = 0;285286
}