[PATCH harec 1/1] lex: split `consume` into 2 functions.
Export this patch
From: Alona Enraght-Moony <code@alona.page>
The codepath is completely different when `n == -1` so there's little
point in merging that case into the same function. Seperating it out
makes the it clearer at the callsite what's actually being done.
Signed-off-by: Alona Enraght-Moony <code@alona.page>
---
src/lex.c | 23 ++++++++++++ -----------
1 file changed, 12 insertions(+), 11 deletions(-)
diff --git a/src/lex.c b/src/lex.c
index 1779e9f..b8033df 100644
--- a/src/lex.c
+++ b/src/lex.c
@@ -245,14 +245,15 @@ wgetc(struct lexer *lexer, struct location *loc)
}
static void
- consume(struct lexer *lexer, ssize_t n)
+ clearbuf(struct lexer *lexer) {
+ lexer->buflen = 0;
+ lexer->buf[0] = 0;
+ }
+
+ static void
+ consume(struct lexer *lexer, size_t n)
{
- if (n == -1) {
- lexer->buflen = 0;
- lexer->buf[0] = 0;
- return;
- }
- for (ssize_t i = 0; i < n; i++) {
+ for (size_t i = 0; i < n; i++) {
while ((lexer->buf[--lexer->buflen] & 0xC0) == 0x80) ;
}
lexer->buf[lexer->buflen] = 0;
@@ -298,7 +299,7 @@ lex_name(struct lexer *lexer, struct token *out)
} else {
out->token = (const char **)token - tokens;
}
- consume(lexer, -1);
+ clearbuf(lexer);
return out->token;
}
@@ -477,7 +478,7 @@ want_int:
error(out->loc, "Unexpected decimal point in integer literal");
}
out->fval = strtod(lexer->buf, NULL);
- consume(lexer, -1);
+ clearbuf(lexer);
return;
}
@@ -503,7 +504,7 @@ want_int:
} else if (kind != UNSIGNED) {
out->ival = (int64_t)out->uval;
}
- consume(lexer, -1);
+ clearbuf(lexer);
}
static uint32_t
@@ -613,7 +614,7 @@ lex_string(struct lexer *lexer, struct token *out)
out->storage = STORAGE_STRING;
out->string.len = lexer->buflen;
out->string.value = buf;
- consume(lexer, -1);
+ clearbuf(lexer);
return out->token;
case '\'':
c = next(lexer, NULL, false);
--
2.38.5
harec/patches: SUCCESS in 1m12s
[lex: split `consume` into 2 functions.][0] from [~adot][1]
[0]: https://lists.sr.ht/~sircmpwn/hare-dev/patches/44807
[1]: mailto:nixon.emoony@gmail.com
✓ #1058856 SUCCESS harec/patches/freebsd.yml https://builds.sr.ht/~sircmpwn/job/1058856
✓ #1058857 SUCCESS harec/patches/netbsd.yml https://builds.sr.ht/~sircmpwn/job/1058857
✓ #1058855 SUCCESS harec/patches/alpine.yml https://builds.sr.ht/~sircmpwn/job/1058855
Thanks!
To gitsrht:~sircmpwn/harec
edd7b87a..6151bd66 master -> master