aboutsummaryrefslogtreecommitdiff
path: root/src/stdlib/simpleparse.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/stdlib/simpleparse.c')
-rw-r--r--src/stdlib/simpleparse.c62
1 files changed, 33 insertions, 29 deletions
diff --git a/src/stdlib/simpleparse.c b/src/stdlib/simpleparse.c
index 1ee64036..3929c32b 100644
--- a/src/stdlib/simpleparse.c
+++ b/src/stdlib/simpleparse.c
@@ -26,41 +26,42 @@ static bool _match_word(const char **str, const char *target) {
return false;
}
-public const char *simpleparse(const char *str, int n, parse_type_e types[n], void *destinations[n])
-{
+public
+const char *simpleparse(const char *str, int n, parse_type_e types[n], void *destinations[n]) {
for (int i = 0; i < n; i++) {
switch (types[i]) {
case PARSE_SOME_OF: {
- if (destinations[i]) str += strspn(str, (char*)destinations[i]);
+ if (destinations[i]) str += strspn(str, (char *)destinations[i]);
break;
}
case PARSE_LITERAL: {
- const char *target = (const char*)destinations[i];
+ const char *target = (const char *)destinations[i];
if (target) {
- if (strncmp(str, target, strlen(target)) != 0)
- return str;
+ if (strncmp(str, target, strlen(target)) != 0) return str;
str += strlen(target);
}
break;
}
case PARSE_STRING: {
size_t len;
- static const char matching_pair[256] = {[(int)'(']=')', [(int)'{']='}', [(int)'[']=']',
- [(int)'"']='"', [(int)'\'']='\'', [(int)'`']='`', [(int)'<']='>'};
- if (i > 0 && i + 1 < n && types[i-1] == PARSE_LITERAL && types[i+1] == PARSE_LITERAL
- && destinations[i-1] && destinations[i+1]
- && strlen((char*)destinations[i-1]) == 1 && strlen((char*)destinations[i+1]) == 1
- && *(char*)destinations[i+1] == matching_pair[(int)*(char*)destinations[i-1]]) {
+ static const char matching_pair[256] = {[(int)'('] = ')', [(int)'{'] = '}', [(int)'['] = ']',
+ [(int)'"'] = '"', [(int)'\''] = '\'', [(int)'`'] = '`',
+ [(int)'<'] = '>'};
+ if (i > 0 && i + 1 < n && types[i - 1] == PARSE_LITERAL && types[i + 1] == PARSE_LITERAL
+ && destinations[i - 1] && destinations[i + 1] && strlen((char *)destinations[i - 1]) == 1
+ && strlen((char *)destinations[i + 1]) == 1
+ && *(char *)destinations[i + 1] == matching_pair[(int)*(char *)destinations[i - 1]]) {
len = 0;
- char special_characters[4] = {'\\', *(char*)destinations[i-1], *(char*)destinations[i+1], 0};
- for (int depth = 1; depth > 0; ) {
+ char special_characters[4] = {'\\', *(char *)destinations[i - 1], *(char *)destinations[i + 1], 0};
+ for (int depth = 1; depth > 0;) {
len += strcspn(str + len, special_characters);
if (str[len] == '\0') {
return str;
} else if (str[len] == '\\'
- && (special_characters[1] == '"' || special_characters[1] == '\'' || special_characters[1] == '`')) {
- if (str[len+1] == '\0') return str;
- len += 2;
+ && (special_characters[1] == '"' || special_characters[1] == '\''
+ || special_characters[1] == '`')) {
+ if (str[len + 1] == '\0') return str;
+ len += 2;
} else if (str[len] == special_characters[2]) { // Check for closing quotes before opening quotes
depth -= 1;
if (depth > 0) len += 1;
@@ -70,8 +71,8 @@ public const char *simpleparse(const char *str, int n, parse_type_e types[n], vo
len += 1;
}
}
- } else if (i + 1 < n && types[i+1] == PARSE_LITERAL) {
- const char *terminator = (const char*)destinations[i+1];
+ } else if (i + 1 < n && types[i + 1] == PARSE_LITERAL) {
+ const char *terminator = (const char *)destinations[i + 1];
if (terminator) {
const char *end = strstr(str, terminator);
if (!end) return str;
@@ -79,16 +80,17 @@ public const char *simpleparse(const char *str, int n, parse_type_e types[n], vo
} else {
len = strlen(str);
}
- } else if (i + 1 < n && types[i+1] == PARSE_SOME_OF) {
- len = destinations[i+1] ? strcspn(str, (char*)destinations[i+1]) : strlen(str);;
+ } else if (i + 1 < n && types[i + 1] == PARSE_SOME_OF) {
+ len = destinations[i + 1] ? strcspn(str, (char *)destinations[i + 1]) : strlen(str);
+ ;
} else {
len = strlen(str);
}
if (destinations[i]) {
- char *matched = GC_MALLOC_ATOMIC(len+1);
+ char *matched = GC_MALLOC_ATOMIC(len + 1);
memcpy(matched, str, len);
matched[len] = '\0';
- *(const char**)destinations[i] = matched;
+ *(const char **)destinations[i] = matched;
}
str += len;
break;
@@ -97,7 +99,7 @@ public const char *simpleparse(const char *str, int n, parse_type_e types[n], vo
char *end = NULL;
double val = strtod(str, &end);
if (end == str) return str;
- if (destinations[i]) *(double*)destinations[i] = val;
+ if (destinations[i]) *(double *)destinations[i] = val;
str = end;
break;
}
@@ -105,15 +107,17 @@ public const char *simpleparse(const char *str, int n, parse_type_e types[n], vo
char *end = NULL;
long val = strtol(str, &end, 10);
if (end == str) return str;
- if (destinations[i]) *(long*)destinations[i] = val;
+ if (destinations[i]) *(long *)destinations[i] = val;
str = end;
break;
}
case PARSE_BOOL: {
- if (_match_word(&str, "true") || _match_word(&str, "yes") || _match_word(&str, "on") || _match_word(&str, "1")) {
- if (destinations[i]) *(bool*)destinations[i] = true;
- } else if (_match_word(&str, "false") || _match_word(&str, "no") || _match_word(&str, "off") || _match_word(&str, "0")) {
- if (destinations[i]) *(bool*)destinations[i] = false;
+ if (_match_word(&str, "true") || _match_word(&str, "yes") || _match_word(&str, "on")
+ || _match_word(&str, "1")) {
+ if (destinations[i]) *(bool *)destinations[i] = true;
+ } else if (_match_word(&str, "false") || _match_word(&str, "no") || _match_word(&str, "off")
+ || _match_word(&str, "0")) {
+ if (destinations[i]) *(bool *)destinations[i] = false;
} else {
return str;
}