added a few function to str_istream_t
This commit is contained in:
parent
8ddd9186b8
commit
ae4da6d73d
2 changed files with 29 additions and 0 deletions
|
|
@ -22,6 +22,17 @@ str_istream_t istrInitLen(const char *str, usize len) {
|
|||
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) {
|
||||
return *ctx->cur++;
|
||||
}
|
||||
|
|
@ -34,6 +45,11 @@ void istrIgnore(str_istream_t *ctx, char delim) {
|
|||
++i, ++ctx->cur);
|
||||
}
|
||||
|
||||
void istrIgnoreAndSkip(str_istream_t *ctx, char delim) {
|
||||
istrIgnore(ctx, delim);
|
||||
istrSkip(ctx, 1);
|
||||
}
|
||||
|
||||
char istrPeek(str_istream_t *ctx) {
|
||||
return *ctx->cur;
|
||||
}
|
||||
|
|
@ -75,6 +91,12 @@ void istrRewind(str_istream_t *ctx) {
|
|||
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) {
|
||||
return ctx.cur - ctx.start;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -23,12 +23,17 @@ typedef struct {
|
|||
str_istream_t istrInit(const char *str);
|
||||
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
|
||||
char istrGet(str_istream_t *ctx);
|
||||
// get the current character but don't advance
|
||||
char istrPeek(str_istream_t *ctx);
|
||||
// ignore characters until the delimiter
|
||||
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
|
||||
void istrSkip(str_istream_t *ctx, usize n);
|
||||
// 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);
|
||||
// returns to the beginning of the stream
|
||||
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
|
||||
usize istrTell(str_istream_t ctx);
|
||||
// returns the number of bytes left to read in the stream
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue