Fix indentation
[jansson.git] / src / load.c
index 5175f35..f004525 100644 (file)
@@ -13,7 +13,6 @@
 #include <stdlib.h>
 #include <string.h>
 #include <stdarg.h>
-#include <unistd.h>
 #include <assert.h>
 
 #include <jansson.h>
@@ -135,7 +134,7 @@ static char stream_get(stream_t *stream, json_error_t *error)
 
         c = stream->buffer[0];
 
-        if(c < 0 && c != EOF)
+        if((unsigned char)c >= 0x80 && c != (char)EOF)
         {
             /* multi-byte UTF-8 sequence */
             int i, count;
@@ -258,14 +257,14 @@ static void lex_scan_string(lex_t *lex, json_error_t *error)
     c = lex_get_save(lex, error);
 
     while(c != '"') {
-        if(c == EOF) {
+        if(c == (char)EOF) {
             lex_unget_unsave(lex, c);
             if(lex_eof(lex))
                 error_set(error, lex, "premature end of input");
             goto out;
         }
 
-        else if(0 <= c && c <= 0x1F) {
+        else if((unsigned char)c <= 0x1F) {
             /* control character */
             lex_unget_unsave(lex, c);
             if(c == '\n')
@@ -339,9 +338,10 @@ static void lex_scan_string(lex_t *lex, json_error_t *error)
 
                         if(0xDC00 <= value2 && value2 <= 0xDFFF) {
                             /* valid second surrogate */
-                            value = ((value - 0xD800) << 10) +
-                                    (value2 - 0xDC00) +
-                                    0x10000;
+                            value =
+                                ((value - 0xD800) << 10) +
+                                (value2 - 0xDC00) +
+                                0x10000;
                         }
                         else {
                             /* invalid second surrogate */
@@ -506,8 +506,8 @@ static int lex_scan(lex_t *lex, json_error_t *error)
     strbuffer_clear(&lex->saved_text);
 
     if(lex->token == TOKEN_STRING) {
-      free(lex->value.string);
-      lex->value.string = NULL;
+        free(lex->value.string);
+        lex->value.string = NULL;
     }
 
     c = lex_get(lex, error);
@@ -519,7 +519,7 @@ static int lex_scan(lex_t *lex, json_error_t *error)
         c = lex_get(lex, error);
     }
 
-    if(c == EOF) {
+    if(c == (char)EOF) {
         if(lex_eof(lex))
             lex->token = TOKEN_EOF;
         else
@@ -572,6 +572,17 @@ out:
     return lex->token;
 }
 
+static char *lex_steal_string(lex_t *lex)
+{
+    char *result = NULL;
+    if(lex->token == TOKEN_STRING)
+    {
+        result = lex->value.string;
+        lex->value.string = NULL;
+    }
+    return result;
+}
+
 static int lex_init(lex_t *lex, get_func get, eof_func eof, void *data)
 {
     stream_init(&lex->stream, get, eof, data);
@@ -615,7 +626,7 @@ static json_t *parse_object(lex_t *lex, json_error_t *error)
             goto error;
         }
 
-        key = strdup(lex->value.string);
+        key = lex_steal_string(lex);
         if(!key)
             return NULL;
 
@@ -734,7 +745,7 @@ static json_t *parse_value(lex_t *lex, json_error_t *error)
             break;
 
         case '{':
-          json = parse_object(lex, error);
+            json = parse_object(lex, error);
             break;
 
         case '[':