Indent json/indent.c
@@ -15,11 +15,17 @@ SDB_API char *sdb_json_indent(const char *s, const char* tab) {
int instr = 0;
int osz;
char *o, *O, *OE, *tmp;
- if (!s) return NULL;
+ if (!s) {
+ return NULL;
+ }
osz = (1 + strlen (s)) * 20;
- if (osz<1) return NULL;
+ if (osz < 1) {
+ return NULL;
+ }
O = malloc (osz);
- if (!O) return NULL;
+ if (!O) {
+ return NULL;
+ }
OE = O + osz;
for (o = O; *s; s++) {
if (o + indent + 10 > OE) {
@@ -39,17 +45,21 @@ SDB_API char *sdb_json_indent(const char *s, const char* tab) {
o = O + delta;
}
if (instr) {
- if (s[0] == '"') instr = 0;
- else if (s[0] == '\\' && s[1] == '"')
+ if (s[0] == '"') {
+ instr = 0;
+ } else if (s[0] == '\\' && s[1] == '"') {
*o++ = *s;
+ }
*o++ = *s;
continue;
} else {
- if (s[0] == '"')
+ if (s[0] == '"') {
instr = 1;
}
- if (*s == '\n'|| *s == '\r' || *s == '\t' || *s == ' ')
+ }
+ if (*s == '\n' || *s == '\r' || *s == '\t' || *s == ' ') {
continue;
+ }
switch (*s) {
case ':':
*o++ = *s;
@@ -88,19 +98,27 @@ SDB_API char *sdb_json_unindent(const char *s) {
int instr = 0;
int len = strlen (s);
char *o, *O = malloc (len + 1);
- if (!O) return NULL;
+ if (!O) {
+ return NULL;
+ }
memset (O, 0, len);
for (o = O; *s; s++) {
if (instr) {
if (s[0] != '"') {
- if (s[0] == '\\' && s[1] == '"')
+ if (s[0] == '\\' && s[1] == '"') {
*o++ = *s;
- } else instr = 0;
+ }
+ } else {
+ instr = 0;
+ }
*o++ = *s;
continue;
- } else if (s[0] == '"') instr = 1;
- if (*s == '\n'|| *s == '\r' || *s == '\t' || *s == ' ')
+ } else if (s[0] == '"') {
+ instr = 1;
+ }
+ if (*s == '\n' || *s == '\r' || *s == '\t' || *s == ' ') {
continue;
+ }
*o++ = *s;
}
*o = 0;
