Skip to content
Browse files

Some doc tweaks and cleanups

  • Loading branch information...
1 parent 101cdb4 commit 2f7bdb25ec2898a680c39819137fabfcf239272f @mnunberg committed Apr 5, 2012
Showing with 50 additions and 29 deletions.
  1. BIN json_samples.tgz
  2. +34 −22 jsonsl.c
  3. +14 −5 jsonsl.h
  4. +2 −2 tests/Makefile
View
BIN json_samples.tgz
Binary file not shown.
View
56 jsonsl.c
@@ -73,6 +73,18 @@ JSONSL_API
void jsonsl_dump_global_metrics(void) { }
#endif /* JSONSL_USE_METRICS */
+#define CASE_DIGITS \
+case '1': \
+case '2': \
+case '3': \
+case '4': \
+case '5': \
+case '6': \
+case '7': \
+case '8': \
+case '9': \
+case '0':
+
/**
* This table (predeclared) contains characters which are recognized
@@ -213,8 +225,10 @@ jsonsl_feed(jsonsl_t jsn, const jsonsl_char_t *bytes, size_t nbytes)
INVOKE_ERROR(HKEY_EXPECTED); \
}
-#define EXTRACT_NUMERIC \
-
+#define VERIFY_SPECIAL(lit) \
+ if (CUR_CHAR != (lit)[jsn->pos - state->pos_begin]) { \
+ INVOKE_ERROR(SPECIAL_EXPECTED); \
+ }
const jsonsl_uchar_t *c = (jsonsl_uchar_t*)bytes;
size_t levels_max = jsn->levels_max;
@@ -223,6 +237,7 @@ jsonsl_feed(jsonsl_t jsn, const jsonsl_char_t *bytes, size_t nbytes)
jsn->base = bytes;
for (; nbytes; nbytes--, jsn->pos++, c++) {
+ register jsonsl_type_t state_type;
INCR_METRIC(TOTAL);
/* Special escape handling for some stuff */
if (jsn->in_escape) {
@@ -241,7 +256,8 @@ jsonsl_feed(jsonsl_t jsn, const jsonsl_char_t *bytes, size_t nbytes)
/**
* Several fast-tracks for common cases:
*/
- if (state->type & JSONSL_Tf_STRINGY) {
+ state_type = state->type;
+ if (state_type & JSONSL_Tf_STRINGY) {
/* check if our character cannot ever change our current string state
* or throw an error
*/
@@ -261,19 +277,10 @@ jsonsl_feed(jsonsl_t jsn, const jsonsl_char_t *bytes, size_t nbytes)
}
INCR_METRIC(STRINGY_SLOWPATH);
- } else if (state->type == JSONSL_T_SPECIAL) {
+ } else if (state_type == JSONSL_T_SPECIAL) {
if (state->special_flags & (JSONSL_SPECIALf_SIGNED|JSONSL_SPECIALf_UNSIGNED)) {
switch (CUR_CHAR) {
- case '1':
- case '2':
- case '3':
- case '4':
- case '5':
- case '6':
- case '7':
- case '8':
- case '9':
- case '0':
+ CASE_DIGITS
state->nelem = (state->nelem*10) + (CUR_CHAR-0x30);
goto GT_NEXT;
@@ -288,23 +295,26 @@ jsonsl_feed(jsonsl_t jsn, const jsonsl_char_t *bytes, size_t nbytes)
goto GT_NEXT;
default:
if (is_special_end(CUR_CHAR)) {
- SPECIAL_POP;
- jsn->expecting = ',';
- if (is_allowed_whitespace(CUR_CHAR)) {
- goto GT_NEXT;
- }
- goto GT_STRUCTURAL_TOKEN;
+ goto GT_SPECIAL_POP;
}
INVOKE_ERROR(INVALID_NUMBER);
break;
}
}
if (!is_special_end(CUR_CHAR)) {
+ /* Verify TRUE, FALSE, NULL */
+ if (state->special_flags == JSONSL_SPECIALf_TRUE) {
+ VERIFY_SPECIAL("true");
+ } else if (state->special_flags == JSONSL_SPECIALf_FALSE) {
+ VERIFY_SPECIAL("false");
+ } else if (state->special_flags == JSONSL_SPECIALf_NULL) {
+ VERIFY_SPECIAL("null");
+ }
INCR_METRIC(SPECIAL_FASTPATH);
- /* Most common case. Inside a number */
goto GT_NEXT;
}
+ GT_SPECIAL_POP:
SPECIAL_POP;
jsn->expecting = ',';
if (is_allowed_whitespace(CUR_CHAR)) {
@@ -334,7 +344,7 @@ jsonsl_feed(jsonsl_t jsn, const jsonsl_char_t *bytes, size_t nbytes)
if (CUR_CHAR == '"') {
GT_QUOTE:
jsn->can_insert = 0;
- switch (state->type) {
+ switch (state_type) {
/* the end of a string or hash key */
case JSONSL_T_STRING:
@@ -1222,3 +1232,5 @@ static unsigned char _escape_maps[0x100] = {
};
static unsigned char *Escape_Maps = _escape_maps;
+
+
View
19 jsonsl.h
@@ -57,7 +57,14 @@ typedef struct jsonsl_jpr_st* jsonsl_jpr_t;
/**
* Constant representing the special JSON types.
* The values are special and aid in speed (the OBJECT and LIST
- * values are the char literals of their openings)
+ * values are the char literals of their openings).
+ *
+ * Their actual value is a character which attempts to resemble
+ * some mnemonic reference to the actual type.
+ *
+ * If new types are added, they must fit into the ASCII printable
+ * range (so they should be AND'd with 0x7f and yield something
+ * meaningful)
*/
#define JSONSL_XTYPE \
X(STRING, '"'|JSONSL_Tf_STRINGY) \
@@ -79,9 +86,7 @@ typedef enum {
/**
* Subtypes for T_SPECIAL. We define them as flags
* because more than one type can be applied to a
- * given object, though in practice we only scan the first
- * character of the 'special' type, so it's only possible
- * to infer a single flag.
+ * given object.
*/
#define JSONSL_XSPECIAL \
@@ -99,13 +104,17 @@ typedef enum {
JSONSL_SPECIALf_##o = b,
JSONSL_XSPECIAL
#undef X
- JSONSL_SPECIALf_UNKNOWN
+ /* Handy flags for checking */
+ JSONSL_SPECIALf_UNKNOWN = 1 << 8,
+ JSONSL_SPECIALf_NUMERIC = (JSONSL_SPECIALf_SIGNED|JSONSL_SPECIALf_UNSIGNED),
+ JSONSL_SPECIALf_BOOLEAN = (JSONSL_SPECIALf_TRUE|JSONSL_SPECIALf_FALSE)
} jsonsl_special_t;
/**
* These are the various types of stack (or other) events
* which will trigger a callback.
+ * Like the type constants, this are also mnemonic
*/
#define JSONSL_XACTION \
X(PUSH, '+') \
View
4 tests/Makefile
@@ -6,9 +6,9 @@ all: $(TESTMODS)
./unescape
./json_test ../share/jsc/pass*.json
JSONSL_FAIL_TESTS=1 ./json_test ../share/jsc/fail*.json
+ @echo " Some tests were skipped."
+ @echo " See share/jsc/nyi_fail/README.skipped for details"
@echo "All Tests OK"
- @echo "These files were skipped. See tests/jsc/nyi_fail for more info"
- @ls ../share/jsc/nyi_fail/
%: %.c
$(CC) $(CFLAGS) $^ -o $@ $(LDFLAGS)

0 comments on commit 2f7bdb2

Please sign in to comment.
Something went wrong with that request. Please try again.