aboutsummaryrefslogtreecommitdiff
path: root/utils.c
diff options
context:
space:
mode:
Diffstat (limited to 'utils.c')
-rw-r--r--utils.c81
1 files changed, 45 insertions, 36 deletions
diff --git a/utils.c b/utils.c
index 78b0a25..ab74bce 100644
--- a/utils.c
+++ b/utils.c
@@ -15,21 +15,24 @@
// Helper function to skip past all spaces (and comments)
// Returns a pointer to the first non-space character.
//
-public const char *after_spaces(const char *str, bool skip_nl, const char *end)
-{
+public
+const char *after_spaces(const char *str, bool skip_nl, const char *end) {
// Skip whitespace and comments:
- skip_whitespace:
+skip_whitespace:
if (str >= end) return str;
switch (*str) {
- case '\r': case '\n':
+ case '\r':
+ case '\n':
if (!skip_nl) break;
- __attribute__ ((fallthrough));
- case ' ': case '\t': {
+ __attribute__((fallthrough));
+ case ' ':
+ case '\t': {
++str;
goto skip_whitespace;
}
case '#': {
- while (str < end && *str != '\n') ++str;
+ while (str < end && *str != '\n')
+ ++str;
goto skip_whitespace;
}
default: break;
@@ -41,8 +44,8 @@ public const char *after_spaces(const char *str, bool skip_nl, const char *end)
// Return the first character after a valid BP name, or NULL if none is
// found.
//
-public const char *after_name(const char *str, const char *end)
-{
+public
+const char *after_name(const char *str, const char *end) {
if (str >= end) return end;
if (*str == '|') return &str[1];
if (*str == '^' || *str == '_' || *str == '$') {
@@ -50,8 +53,7 @@ public const char *after_name(const char *str, const char *end)
}
if (!isalpha(*str)) return NULL;
for (++str; str < end; ++str) {
- if (!(isalnum(*str) || *str == '-'))
- break;
+ if (!(isalnum(*str) || *str == '-')) break;
}
return str;
}
@@ -59,8 +61,8 @@ public const char *after_name(const char *str, const char *end)
//
// Check if a character is found and if so, move past it.
//
-public bool matchchar(const char **str, char c, bool skip_nl, const char *end)
-{
+public
+bool matchchar(const char **str, char c, bool skip_nl, const char *end) {
const char *next = after_spaces(*str, skip_nl, end);
if (next >= end) return false;
if (*next == c) {
@@ -73,8 +75,8 @@ public bool matchchar(const char **str, char c, bool skip_nl, const char *end)
//
// Check if a string is found and if so, move past it.
//
-public bool matchstr(const char **str, const char *target, bool skip_nl, const char *end)
-{
+public
+bool matchstr(const char **str, const char *target, bool skip_nl, const char *end) {
const char *next = after_spaces(*str, skip_nl, end);
if (next + strlen(target) > end) return false;
if (strncmp(next, target, strlen(target)) == 0) {
@@ -89,24 +91,27 @@ public bool matchstr(const char **str, const char *target, bool skip_nl, const c
// character that was escaped.
// Set *end = the first character past the end of the escape sequence.
//
-public char unescapechar(const char *escaped, const char **after, const char *end)
-{
+public
+char unescapechar(const char *escaped, const char **after, const char *end) {
size_t len = 0;
unsigned char ret = '\\';
if (escaped >= end) goto finished;
ret = (unsigned char)*escaped;
++len;
switch (*escaped) {
- case 'a': ret = '\a'; break; case 'b': ret = '\b'; break;
- case 'n': ret = '\n'; break; case 'r': ret = '\r'; break;
- case 't': ret = '\t'; break; case 'v': ret = '\v'; break;
- case 'e': ret = '\033'; break; case '\\': ret = '\\'; break;
+ case 'a': ret = '\a'; break;
+ case 'b': ret = '\b'; break;
+ case 'n': ret = '\n'; break;
+ case 'r': ret = '\r'; break;
+ case 't': ret = '\t'; break;
+ case 'v': ret = '\v'; break;
+ case 'e': ret = '\033'; break;
+ case '\\': ret = '\\'; break;
case 'x': { // Hex
static const unsigned char hextable[255] = {
- ['0']=0x10, ['1']=0x1, ['2']=0x2, ['3']=0x3, ['4']=0x4,
- ['5']=0x5, ['6']=0x6, ['7']=0x7, ['8']=0x8, ['9']=0x9,
- ['a']=0xa, ['b']=0xb, ['c']=0xc, ['d']=0xd, ['e']=0xe, ['f']=0xf,
- ['A']=0xa, ['B']=0xb, ['C']=0xc, ['D']=0xd, ['E']=0xe, ['F']=0xf,
+ ['0'] = 0x10, ['1'] = 0x1, ['2'] = 0x2, ['3'] = 0x3, ['4'] = 0x4, ['5'] = 0x5, ['6'] = 0x6, ['7'] = 0x7,
+ ['8'] = 0x8, ['9'] = 0x9, ['a'] = 0xa, ['b'] = 0xb, ['c'] = 0xc, ['d'] = 0xd, ['e'] = 0xe, ['f'] = 0xf,
+ ['A'] = 0xa, ['B'] = 0xb, ['C'] = 0xc, ['D'] = 0xd, ['E'] = 0xe, ['F'] = 0xf,
};
if (escaped + 2 >= end) {
len = 0;
@@ -117,7 +122,14 @@ public char unescapechar(const char *escaped, const char **after, const char *en
}
break;
}
- case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': { // Octal
+ case '0':
+ case '1':
+ case '2':
+ case '3':
+ case '4':
+ case '5':
+ case '6':
+ case '7': { // Octal
ret = (unsigned char)(escaped[0] - '0');
if (escaped + 2 >= end) {
len = 0;
@@ -132,11 +144,9 @@ public char unescapechar(const char *escaped, const char **after, const char *en
}
break;
}
- default:
- len = 0;
- goto finished;
+ default: len = 0; goto finished;
}
- finished:
+finished:
if (after) *after = &escaped[len];
return (char)ret;
}
@@ -144,12 +154,11 @@ public char unescapechar(const char *escaped, const char **after, const char *en
//
// Free memory, but also set the pointer to NULL for safety
//
-public void delete(void *p)
-{
- if (*(void**)p == NULL)
- errx(EXIT_FAILURE, "attempt to free(NULL)");
- free(*(void**)p);
- *((void**)p) = NULL;
+public
+void delete(void *p) {
+ if (*(void **)p == NULL) errx(EXIT_FAILURE, "attempt to free(NULL)");
+ free(*(void **)p);
+ *((void **)p) = NULL;
}
// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0