Factor out isBinDigit and parseHexDigit utility functions

This commit is contained in:
Rangi42
2025-09-04 13:14:27 -04:00
parent 891e6f98df
commit 1dfc1d3231
5 changed files with 32 additions and 48 deletions

View File

@@ -5,6 +5,8 @@
#include <stdint.h>
#include <stdio.h>
#include "helpers.hpp" // assume
bool isNewline(int c) {
return c == '\r' || c == '\n';
}
@@ -51,6 +53,17 @@ bool continuesIdentifier(int c) {
return startsIdentifier(c) || isDigit(c) || c == '#' || c == '$' || c == '@';
}
uint8_t parseHexDigit(int c) {
if (c >= 'A' && c <= 'F') {
return c - 'A' + 10;
} else if (c >= 'a' && c <= 'f') {
return c - 'a' + 10;
} else {
assume(isDigit(c));
return c - '0';
}
}
char const *printChar(int c) {
// "'A'" + '\0': 4 bytes
// "'\\n'" + '\0': 5 bytes