Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

remove some ugly and dubiously-useful "optimised" switch statements

  • Loading branch information...
commit 7951a3a26f1525bc8014f7a71b3530eec8a62b3f 1 parent 83fd4ad
Ryan Kelly authored
Showing with 21 additions and 59 deletions.
  1. +17 −43 tnetstring/_tnetstring.c
  2. +4 −16 tnetstring/tns_core.c
60 tnetstring/_tnetstring.c
View
@@ -288,9 +288,9 @@ static void* tns_parse_integer(const char *data, size_t len)
const char *pos, *eod;
PyObject *v = NULL;
+ // Anything with less than 10 digits, we can fit into a long.
+ // Hand-parsing, as we need tighter error-checking than strtol.
if (len < 10) {
- // Anything with less than 10 digits, we can fit into a long.
- // Hand-parsing, as we need tighter error-checking than strtol.
pos = data;
eod = data + len;
c = *pos++;
@@ -317,27 +317,14 @@ static void* tns_parse_integer(const char *data, size_t len)
}
while(pos < eod) {
c = *pos++;
- switch(c) {
- case '0':
- case '1':
- case '2':
- case '3':
- case '4':
- case '5':
- case '6':
- case '7':
- case '8':
- case '9':
- l = (l * 10) + (c - '0');
- break;
- default:
- sentinel("invalid integer literal");
- }
+ check(c >= '0' && c <= '9', "invalid integer literal");
+ l = (l * 10) + (c - '0');
}
return PyLong_FromLong(l * sign);
- } else if(len < 19) {
- // Anything with less than 19 digits fits in a long long.
- // Hand-parsing, as we need tighter error-checking than strtoll.
+ }
+ // Anything with less than 19 digits fits in a long long.
+ // Hand-parsing, as we need tighter error-checking than strtoll.
+ else if(len < 19) {
pos = data;
eod = data + len;
c = *pos++;
@@ -364,30 +351,17 @@ static void* tns_parse_integer(const char *data, size_t len)
}
while(pos < eod) {
c = *pos++;
- switch(c) {
- case '0':
- case '1':
- case '2':
- case '3':
- case '4':
- case '5':
- case '6':
- case '7':
- case '8':
- case '9':
- ll = (ll * 10) + (c - '0');
- break;
- default:
- sentinel("invalid integer literal");
- }
+ check(c >= '0' && c <= '9', "invalid integer literal");
+ ll = (ll * 10) + (c - '0');
}
return PyLong_FromLongLong(ll * sign);
- } else {
- // Really big numbers must be parsed by python.
- // Technically this allows whitespace around the number, which
- // isn't valid in a tnetstring. But I don't want to waste the
- // time checking and I am *not* reimplementing arbitrary-precision
- // strtod for python.
+ }
+ // Really big numbers must be parsed by python.
+ // Technically this allows whitespace around the number, which
+ // isn't valid in a tnetstring. But I don't want to waste the
+ // time checking and I am *not* reimplementing arbitrary-precision
+ // strtod for python.
+ else {
v = PyLong_FromString((char *)data, &dataend, 10);
check(dataend == data + len, "invalid integer literal");
return v;
20 tnetstring/tns_core.c
View
@@ -39,12 +39,12 @@ void* tns_parse(const tns_ops *ops, const char *data, size_t len, char **remain)
// Read the length of the value, and verify that it ends in a colon.
check(tns_strtosz(data, len, &vallen, &valstr) != -1,
- "Not a tnetstring: invalid length prefix.");
+ "Not a tnetstring: invalid length prefix1.");
check(*valstr == ':',
- "Not a tnetstring: invalid length prefix.");
+ "Not a tnetstring: invalid length prefix2. %s");
valstr++;
check((valstr+vallen) < (data+len),
- "Not a tnetstring: invalid length prefix.");
+ "Not a tnetstring: invalid length prefix3.");
// Grab the type tag from the end of the value.
type = valstr[vallen];
@@ -310,19 +310,7 @@ tns_strtosz(const char *data, size_t len, size_t *sz, char **end)
// Consume all other digits.
while(pos < eod) {
c = *pos;
- switch(c) {
- case '0':
- case '1':
- case '2':
- case '3':
- case '4':
- case '5':
- case '6':
- case '7':
- case '8':
- case '9':
- break;
- default:
+ if(c < '0' || c > '9') {
*sz = value;
*end = (char*) pos;
return 0;
Please sign in to comment.
Something went wrong with that request. Please try again.