added a few function to str_istream_t

This commit is contained in:
alessandrobason 2022-08-22 18:27:41 +01:00
parent 8ddd9186b8
commit ae4da6d73d
2 changed files with 29 additions and 0 deletions

View file

@ -22,6 +22,17 @@ str_istream_t istrInitLen(const char *str, usize len) {
return res; return res;
} }
void istrScanf(str_istream_t *ctx, const char *fmt, ...) {
va_list va;
va_start(va, fmt);
istrScanfV(ctx, fmt, va);
va_end(va);
}
void istrScanfV(str_istream_t *ctx, const char *fmt, va_list args) {
vsscanf(ctx->cur, fmt, args);
}
char istrGet(str_istream_t *ctx) { char istrGet(str_istream_t *ctx) {
return *ctx->cur++; return *ctx->cur++;
} }
@ -34,6 +45,11 @@ void istrIgnore(str_istream_t *ctx, char delim) {
++i, ++ctx->cur); ++i, ++ctx->cur);
} }
void istrIgnoreAndSkip(str_istream_t *ctx, char delim) {
istrIgnore(ctx, delim);
istrSkip(ctx, 1);
}
char istrPeek(str_istream_t *ctx) { char istrPeek(str_istream_t *ctx) {
return *ctx->cur; return *ctx->cur;
} }
@ -75,6 +91,12 @@ void istrRewind(str_istream_t *ctx) {
ctx->cur = ctx->start; ctx->cur = ctx->start;
} }
void istrRewindN(str_istream_t *ctx, usize amount) {
usize remaining = ctx->size - (ctx->cur - ctx->start);
if (amount > remaining) amount = remaining;
ctx->cur -= amount;
}
usize istrTell(str_istream_t ctx) { usize istrTell(str_istream_t ctx) {
return ctx.cur - ctx.start; return ctx.cur - ctx.start;
} }

View file

@ -23,12 +23,17 @@ typedef struct {
str_istream_t istrInit(const char *str); str_istream_t istrInit(const char *str);
str_istream_t istrInitLen(const char *str, usize len); str_istream_t istrInitLen(const char *str, usize len);
void istrScanf(str_istream_t *ctx, const char *fmt, ...);
void istrScanfV(str_istream_t *ctx, const char *fmt, va_list args);
// get the current character and advance // get the current character and advance
char istrGet(str_istream_t *ctx); char istrGet(str_istream_t *ctx);
// get the current character but don't advance // get the current character but don't advance
char istrPeek(str_istream_t *ctx); char istrPeek(str_istream_t *ctx);
// ignore characters until the delimiter // ignore characters until the delimiter
void istrIgnore(str_istream_t *ctx, char delim); void istrIgnore(str_istream_t *ctx, char delim);
// ignore characters until the delimiter and skip it
void istrIgnoreAndSkip(str_istream_t *ctx, char delim);
// skip n characters // skip n characters
void istrSkip(str_istream_t *ctx, usize n); void istrSkip(str_istream_t *ctx, usize n);
// skips whitespace (' ', '\n', '\t', '\r') // skips whitespace (' ', '\n', '\t', '\r')
@ -40,6 +45,8 @@ void istrRead(str_istream_t *ctx, char *buf, usize len);
usize istrReadMax(str_istream_t *ctx, char *buf, usize len); usize istrReadMax(str_istream_t *ctx, char *buf, usize len);
// returns to the beginning of the stream // returns to the beginning of the stream
void istrRewind(str_istream_t *ctx); void istrRewind(str_istream_t *ctx);
// returns back <amount> characters
void istrRewindN(str_istream_t *ctx, usize amount);
// returns the number of bytes read from beginning of stream // returns the number of bytes read from beginning of stream
usize istrTell(str_istream_t ctx); usize istrTell(str_istream_t ctx);
// returns the number of bytes left to read in the stream // returns the number of bytes left to read in the stream