Skip to content

Commit

Permalink
eliminate parse_result
Browse files Browse the repository at this point in the history
  • Loading branch information
chadaustin committed Apr 22, 2017
1 parent b4aa78a commit 193b183
Showing 1 changed file with 27 additions and 55 deletions.
82 changes: 27 additions & 55 deletions include/sajson.h
Expand Up @@ -548,24 +548,6 @@ namespace sajson {
} }
}; };


struct parse_result {
parse_result(error_result)
: success(false)
{}

parse_result(type t)
: success(true)
, value_type(t)
{}

bool operator!() const {
return !success;
}

bool success;
type value_type;
};

bool at_eof(const char* p) { bool at_eof(const char* p) {
return p == input_end; return p == input_end;
} }
Expand Down Expand Up @@ -650,7 +632,7 @@ namespace sajson {
size_t* current_base = temp; size_t* current_base = temp;
*temp++ = make_element(current_structure_type, ROOT_MARKER); *temp++ = make_element(current_structure_type, ROOT_MARKER);


parse_result result = error_result(); type value_type_result;


for (;;) { for (;;) {
const char closing_bracket = (current_structure_type == TYPE_OBJECT ? '}' : ']'); const char closing_bracket = (current_structure_type == TYPE_OBJECT ? '}' : ']');
Expand Down Expand Up @@ -701,21 +683,21 @@ namespace sajson {
if (!p) { if (!p) {
return false; return false;
} }
result = TYPE_NULL; value_type_result = TYPE_NULL;
break; break;
case 'f': case 'f':
p = parse_false(p); p = parse_false(p);
if (!p) { if (!p) {
return false; return false;
} }
result = TYPE_FALSE; value_type_result = TYPE_FALSE;
break; break;
case 't': case 't':
p = parse_true(p); p = parse_true(p);
if (!p) { if (!p) {
return false; return false;
} }
result = TYPE_TRUE; value_type_result = TYPE_TRUE;
break; break;
case '0': case '0':
case '1': case '1':
Expand All @@ -727,16 +709,22 @@ namespace sajson {
case '7': case '7':
case '8': case '8':
case '9': case '9':
case '-': case '-': {
result = parse_number(p); auto result = parse_number(p);
p = result.first;
if (!p) {
return false;
}
value_type_result = result.second;
break; break;
}
case '"': case '"':
out -= 2; out -= 2;
p = parse_string(p, out); p = parse_string(p, out);
if (!p) { if (!p) {
return false; return false;
} }
result = TYPE_STRING; value_type_result = TYPE_STRING;
break; break;


case '[': case '[':
Expand Down Expand Up @@ -784,7 +772,7 @@ namespace sajson {
} }
temp = current_base; temp = current_base;
current_base = structure + parent; current_base = structure + parent;
result = current_structure_type; value_type_result = current_structure_type;
current_structure_type = get_element_type(element); current_structure_type = get_element_type(element);
break; break;
} }
Expand All @@ -794,11 +782,7 @@ namespace sajson {
return error(p, "cannot parse unknown value"); return error(p, "cannot parse unknown value");
} }


if (!result) { *temp++ = make_element(value_type_result, out - current_base - 1);
return result.success;
}

*temp++ = make_element(result.value_type, out - current_base - 1);
} }


done: done:
Expand Down Expand Up @@ -922,16 +906,14 @@ namespace sajson {
return constants[exponent + 323]; return constants[exponent + 323];
} }


parse_result parse_number(char*& p_) { std::pair<char*, type> parse_number(char* p) {
char* p = p_;
bool negative = false; bool negative = false;
if ('-' == *p) { if ('-' == *p) {
++p; ++p;
negative = true; negative = true;


if (SAJSON_UNLIKELY(at_eof(p))) { if (SAJSON_UNLIKELY(at_eof(p))) {
p_ = p; return std::make_pair(error(p, "unexpected end of input"), TYPE_NULL);
return error(p, "unexpected end of input");
} }
} }


Expand All @@ -949,8 +931,7 @@ namespace sajson {


++p; ++p;
if (SAJSON_UNLIKELY(at_eof(p))) { if (SAJSON_UNLIKELY(at_eof(p))) {
p_ = p; return std::make_pair(error(p, "unexpected end of input"), TYPE_NULL);
return error(p, "unexpected end of input");
} }


unsigned char digit = c - '0'; unsigned char digit = c - '0';
Expand All @@ -976,8 +957,7 @@ namespace sajson {
} }
++p; ++p;
if (SAJSON_UNLIKELY(at_eof(p))) { if (SAJSON_UNLIKELY(at_eof(p))) {
p_ = p; return std::make_pair(error(p, "unexpected end of input"), TYPE_NULL);
return error(p, "unexpected end of input");
} }
for (;;) { for (;;) {
char c = *p; char c = *p;
Expand All @@ -987,8 +967,7 @@ namespace sajson {


++p; ++p;
if (SAJSON_UNLIKELY(at_eof(p))) { if (SAJSON_UNLIKELY(at_eof(p))) {
p_ = p; return std::make_pair(error(p, "unexpected end of input"), TYPE_NULL);
return error(p, "unexpected end of input");
} }
d = d * 10 + (c - '0'); d = d * 10 + (c - '0');
--exponent; --exponent;
Expand All @@ -1003,40 +982,35 @@ namespace sajson {
} }
++p; ++p;
if (SAJSON_UNLIKELY(at_eof(p))) { if (SAJSON_UNLIKELY(at_eof(p))) {
p_ = p; return std::make_pair(error(p, "unexpected end of input"), TYPE_NULL);
return error(p, "unexpected end of input");
} }


bool negativeExponent = false; bool negativeExponent = false;
if ('-' == *p) { if ('-' == *p) {
negativeExponent = true; negativeExponent = true;
++p; ++p;
if (SAJSON_UNLIKELY(at_eof(p))) { if (SAJSON_UNLIKELY(at_eof(p))) {
p_ = p; return std::make_pair(error(p, "unexpected end of input"), TYPE_NULL);
return error(p, "unexpected end of input");
} }
} else if ('+' == *p) { } else if ('+' == *p) {
++p; ++p;
if (SAJSON_UNLIKELY(at_eof(p))) { if (SAJSON_UNLIKELY(at_eof(p))) {
p_ = p; return std::make_pair(error(p, "unexpected end of input"), TYPE_NULL);
return error(p, "unexpected end of input");
} }
} }


int exp = 0; int exp = 0;


char c = *p; char c = *p;
if (SAJSON_UNLIKELY(c < '0' || c > '9')) { if (SAJSON_UNLIKELY(c < '0' || c > '9')) {
p_ = p; return std::make_pair(error(p, "missing exponent"), TYPE_NULL);
return error(p, "missing exponent");
} }
for (;;) { for (;;) {
exp = 10 * exp + (c - '0'); exp = 10 * exp + (c - '0');


++p; ++p;
if (SAJSON_UNLIKELY(at_eof(p))) { if (SAJSON_UNLIKELY(at_eof(p))) {
p_ = p; return std::make_pair(error(p, "unexpected end of input"), TYPE_NULL);
return error(p, "unexpected end of input");
} }


c = *p; c = *p;
Expand All @@ -1062,15 +1036,13 @@ namespace sajson {
if (try_double) { if (try_double) {
out -= double_storage::word_length; out -= double_storage::word_length;
double_storage::store(out, d); double_storage::store(out, d);
p_ = p; return std::make_pair(p, TYPE_DOUBLE);
return TYPE_DOUBLE;
} else { } else {
integer_storage is; integer_storage is;
is.i = i; is.i = i;


*--out = is.u; *--out = is.u;
p_ = p; return std::make_pair(p, TYPE_INTEGER);
return TYPE_INTEGER;
} }
} }


Expand Down

0 comments on commit 193b183

Please sign in to comment.