mirror of
https://github.com/felt/tippecanoe.git
synced 2026-10-05 18:05:42 +02:00
Converting repetitive ifs to cases
This commit is contained in:
+69
-42
@@ -8,24 +8,25 @@
|
|||||||
#include "milo/dtoa_milo.h"
|
#include "milo/dtoa_milo.h"
|
||||||
|
|
||||||
static std::string mvt_value_to_string(mvt_value one, bool &fail) {
|
static std::string mvt_value_to_string(mvt_value one, bool &fail) {
|
||||||
if (one.type == mvt_string) {
|
switch (one.type) {
|
||||||
|
case mvt_string:
|
||||||
return one.string_value;
|
return one.string_value;
|
||||||
} else if (one.type == mvt_float) {
|
case mvt_float:
|
||||||
return milo::dtoa_milo(one.numeric_value.float_value);
|
return milo::dtoa_milo(one.numeric_value.float_value);
|
||||||
} else if (one.type == mvt_double) {
|
case mvt_double:
|
||||||
return milo::dtoa_milo(one.numeric_value.double_value);
|
return milo::dtoa_milo(one.numeric_value.double_value);
|
||||||
} else if (one.type == mvt_int) {
|
case mvt_int:
|
||||||
return std::to_string(one.numeric_value.int_value);
|
return std::to_string(one.numeric_value.int_value);
|
||||||
} else if (one.type == mvt_uint) {
|
case mvt_uint:
|
||||||
return std::to_string(one.numeric_value.uint_value);
|
return std::to_string(one.numeric_value.uint_value);
|
||||||
} else if (one.type == mvt_sint) {
|
case mvt_sint:
|
||||||
return std::to_string(one.numeric_value.sint_value);
|
return std::to_string(one.numeric_value.sint_value);
|
||||||
} else if (one.type == mvt_bool) {
|
case mvt_bool:
|
||||||
return one.numeric_value.bool_value ? "true" : "false";
|
return one.numeric_value.bool_value ? "true" : "false";
|
||||||
} else if (one.type == mvt_null) {
|
case mvt_null:
|
||||||
fail = true; // null op string => null
|
fail = true; // null op string => null
|
||||||
return "";
|
return "";
|
||||||
} else {
|
default:
|
||||||
fprintf(stderr, "unhandled mvt_type %d\n", one.type);
|
fprintf(stderr, "unhandled mvt_type %d\n", one.type);
|
||||||
exit(EXIT_IMPOSSIBLE);
|
exit(EXIT_IMPOSSIBLE);
|
||||||
}
|
}
|
||||||
@@ -47,7 +48,8 @@ int compare_fsl(mvt_value one, json_object *two, bool &fail) {
|
|||||||
if (two->type == JSON_NUMBER) {
|
if (two->type == JSON_NUMBER) {
|
||||||
double lhs;
|
double lhs;
|
||||||
|
|
||||||
if (one.type == mvt_string) {
|
switch (one.type) {
|
||||||
|
case mvt_string: {
|
||||||
char *endptr = NULL;
|
char *endptr = NULL;
|
||||||
const char *s = one.string_value.c_str();
|
const char *s = one.string_value.c_str();
|
||||||
lhs = strtod(s, &endptr);
|
lhs = strtod(s, &endptr);
|
||||||
@@ -55,22 +57,29 @@ int compare_fsl(mvt_value one, json_object *two, bool &fail) {
|
|||||||
fail = true; // non-numeric-string op number => null
|
fail = true; // non-numeric-string op number => null
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
} else if (one.type == mvt_float) {
|
} break;
|
||||||
|
case mvt_float:
|
||||||
lhs = one.numeric_value.float_value;
|
lhs = one.numeric_value.float_value;
|
||||||
} else if (one.type == mvt_double) {
|
break;
|
||||||
|
case mvt_double:
|
||||||
lhs = one.numeric_value.double_value;
|
lhs = one.numeric_value.double_value;
|
||||||
} else if (one.type == mvt_int) {
|
break;
|
||||||
|
case mvt_int:
|
||||||
lhs = one.numeric_value.int_value;
|
lhs = one.numeric_value.int_value;
|
||||||
} else if (one.type == mvt_uint) {
|
break;
|
||||||
|
case mvt_uint:
|
||||||
lhs = one.numeric_value.uint_value;
|
lhs = one.numeric_value.uint_value;
|
||||||
} else if (one.type == mvt_sint) {
|
break;
|
||||||
|
case mvt_sint:
|
||||||
lhs = one.numeric_value.sint_value;
|
lhs = one.numeric_value.sint_value;
|
||||||
} else if (one.type == mvt_bool) {
|
break;
|
||||||
|
case mvt_bool:
|
||||||
lhs = one.numeric_value.bool_value;
|
lhs = one.numeric_value.bool_value;
|
||||||
} else if (one.type == mvt_null) {
|
break;
|
||||||
|
case mvt_null:
|
||||||
fail = true; // null op number => null
|
fail = true; // null op number => null
|
||||||
return 0;
|
return 0;
|
||||||
} else {
|
default:
|
||||||
fprintf(stderr, "unhandled mvt_type %d\n", one.type);
|
fprintf(stderr, "unhandled mvt_type %d\n", one.type);
|
||||||
exit(EXIT_IMPOSSIBLE);
|
exit(EXIT_IMPOSSIBLE);
|
||||||
}
|
}
|
||||||
@@ -93,24 +102,32 @@ int compare_fsl(mvt_value one, json_object *two, bool &fail) {
|
|||||||
if (two->type == JSON_TRUE || two->type == JSON_FALSE) {
|
if (two->type == JSON_TRUE || two->type == JSON_FALSE) {
|
||||||
bool lhs;
|
bool lhs;
|
||||||
|
|
||||||
if (one.type == mvt_string) {
|
switch (one.type) {
|
||||||
|
case mvt_string:
|
||||||
lhs = one.string_value.size() > 0;
|
lhs = one.string_value.size() > 0;
|
||||||
} else if (one.type == mvt_float) {
|
break;
|
||||||
|
case mvt_float:
|
||||||
lhs = one.numeric_value.float_value != 0;
|
lhs = one.numeric_value.float_value != 0;
|
||||||
} else if (one.type == mvt_double) {
|
break;
|
||||||
|
case mvt_double:
|
||||||
lhs = one.numeric_value.double_value != 0;
|
lhs = one.numeric_value.double_value != 0;
|
||||||
} else if (one.type == mvt_int) {
|
break;
|
||||||
|
case mvt_int:
|
||||||
lhs = one.numeric_value.int_value != 0;
|
lhs = one.numeric_value.int_value != 0;
|
||||||
} else if (one.type == mvt_uint) {
|
break;
|
||||||
|
case mvt_uint:
|
||||||
lhs = one.numeric_value.uint_value != 0;
|
lhs = one.numeric_value.uint_value != 0;
|
||||||
} else if (one.type == mvt_sint) {
|
break;
|
||||||
|
case mvt_sint:
|
||||||
lhs = one.numeric_value.sint_value != 0;
|
lhs = one.numeric_value.sint_value != 0;
|
||||||
} else if (one.type == mvt_bool) {
|
break;
|
||||||
|
case mvt_bool:
|
||||||
lhs = one.numeric_value.bool_value;
|
lhs = one.numeric_value.bool_value;
|
||||||
} else if (one.type == mvt_null) {
|
break;
|
||||||
|
case mvt_null:
|
||||||
fail = true; // null op bool => null
|
fail = true; // null op bool => null
|
||||||
return 0;
|
return 0;
|
||||||
} else {
|
default:
|
||||||
fprintf(stderr, "unhandled mvt_type %d\n", one.type);
|
fprintf(stderr, "unhandled mvt_type %d\n", one.type);
|
||||||
exit(EXIT_IMPOSSIBLE);
|
exit(EXIT_IMPOSSIBLE);
|
||||||
}
|
}
|
||||||
@@ -124,33 +141,43 @@ int compare_fsl(mvt_value one, json_object *two, bool &fail) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int compare(mvt_value one, json_object *two, bool &fail) {
|
int compare(mvt_value one, json_object *two, bool &fail) {
|
||||||
if (one.type == mvt_string) {
|
switch (one.type) {
|
||||||
|
case mvt_string:
|
||||||
if (two->type != JSON_STRING) {
|
if (two->type != JSON_STRING) {
|
||||||
fail = true;
|
fail = true;
|
||||||
return false; // string vs non-string
|
return false; // string vs non-string
|
||||||
}
|
}
|
||||||
|
|
||||||
return strcmp(one.string_value.c_str(), two->value.string.string);
|
return strcmp(one.string_value.c_str(), two->value.string.string);
|
||||||
}
|
|
||||||
|
|
||||||
if (one.type == mvt_double || one.type == mvt_float || one.type == mvt_int || one.type == mvt_uint || one.type == mvt_sint) {
|
case mvt_double:
|
||||||
|
case mvt_float:
|
||||||
|
case mvt_int:
|
||||||
|
case mvt_uint:
|
||||||
|
case mvt_sint:
|
||||||
if (two->type != JSON_NUMBER) {
|
if (two->type != JSON_NUMBER) {
|
||||||
fail = true;
|
fail = true;
|
||||||
return false; // number vs non-number
|
return false; // number vs non-number
|
||||||
}
|
}
|
||||||
|
|
||||||
double v;
|
double v;
|
||||||
if (one.type == mvt_double) {
|
switch (one.type) {
|
||||||
|
case mvt_double:
|
||||||
v = one.numeric_value.double_value;
|
v = one.numeric_value.double_value;
|
||||||
} else if (one.type == mvt_float) {
|
break;
|
||||||
|
case mvt_float:
|
||||||
v = one.numeric_value.float_value;
|
v = one.numeric_value.float_value;
|
||||||
} else if (one.type == mvt_int) {
|
break;
|
||||||
|
case mvt_int:
|
||||||
v = one.numeric_value.int_value;
|
v = one.numeric_value.int_value;
|
||||||
} else if (one.type == mvt_uint) {
|
break;
|
||||||
|
case mvt_uint:
|
||||||
v = one.numeric_value.uint_value;
|
v = one.numeric_value.uint_value;
|
||||||
} else if (one.type == mvt_sint) {
|
break;
|
||||||
|
case mvt_sint:
|
||||||
v = one.numeric_value.sint_value;
|
v = one.numeric_value.sint_value;
|
||||||
} else {
|
break;
|
||||||
|
default:
|
||||||
fprintf(stderr, "Internal error: bad mvt type %d\n", one.type);
|
fprintf(stderr, "Internal error: bad mvt type %d\n", one.type);
|
||||||
exit(EXIT_IMPOSSIBLE);
|
exit(EXIT_IMPOSSIBLE);
|
||||||
}
|
}
|
||||||
@@ -162,19 +189,19 @@ int compare(mvt_value one, json_object *two, bool &fail) {
|
|||||||
} else {
|
} else {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (one.type == mvt_bool) {
|
case mvt_bool:
|
||||||
if (two->type != JSON_TRUE && two->type != JSON_FALSE) {
|
if (two->type != JSON_TRUE && two->type != JSON_FALSE) {
|
||||||
fail = true;
|
fail = true;
|
||||||
return false; // bool vs non-bool
|
return false; // bool vs non-bool
|
||||||
}
|
}
|
||||||
|
|
||||||
bool b = two->type != JSON_FALSE;
|
{
|
||||||
return one.numeric_value.bool_value > b;
|
bool b = two->type != JSON_FALSE;
|
||||||
}
|
return one.numeric_value.bool_value > b;
|
||||||
|
}
|
||||||
|
|
||||||
if (one.type == mvt_null) {
|
case mvt_null:
|
||||||
if (two->type != JSON_NULL) {
|
if (two->type != JSON_NULL) {
|
||||||
fail = true;
|
fail = true;
|
||||||
return false; // null vs non-null
|
return false; // null vs non-null
|
||||||
|
|||||||
Reference in New Issue
Block a user