From a5eabab8bfe179995bf2e0de969b502ba9134638 Mon Sep 17 00:00:00 2001 From: Erica Fischer Date: Thu, 25 Jan 2024 11:12:23 -0800 Subject: [PATCH] Make the string_value of mvt_value std::optional --- evaluator.cpp | 14 +++++++------- mvt.cpp | 12 ++++++------ mvt.hpp | 5 +++-- tile.cpp | 8 ++++---- write_json.cpp | 2 +- 5 files changed, 21 insertions(+), 20 deletions(-) diff --git a/evaluator.cpp b/evaluator.cpp index c8d81e13..791d2962 100644 --- a/evaluator.cpp +++ b/evaluator.cpp @@ -8,10 +8,10 @@ #include "errors.hpp" #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 const &one, bool &fail) { switch (one.type) { case mvt_string: - return one.string_value; + return *(one.string_value); case mvt_float: return milo::dtoa_milo(one.numeric_value.float_value); case mvt_double: @@ -34,7 +34,7 @@ static std::string mvt_value_to_string(mvt_value one, bool &fail) { } } -int compare_fsl(mvt_value one, json_object *two, bool &fail) { +int compare_fsl(mvt_value const &one, json_object *two, bool &fail) { // In FSL expressions, the attribute value is coerced to the type // of the JSON literal value it is being compared to. // @@ -53,7 +53,7 @@ int compare_fsl(mvt_value one, json_object *two, bool &fail) { switch (one.type) { case mvt_string: { char *endptr = NULL; - const char *s = one.string_value.c_str(); + const char *s = one.string_value->c_str(); lhs = strtod(s, &endptr); if (endptr == s) { fail = true; // non-numeric-string op number => null @@ -106,7 +106,7 @@ int compare_fsl(mvt_value one, json_object *two, bool &fail) { switch (one.type) { case mvt_string: - lhs = one.string_value.size() > 0; + lhs = one.string_value->size() > 0; break; case mvt_float: lhs = one.numeric_value.float_value != 0; @@ -144,7 +144,7 @@ int compare_fsl(mvt_value one, json_object *two, bool &fail) { exit(EXIT_IMPOSSIBLE); } -int compare(mvt_value one, json_object *two, bool &fail) { +int compare(mvt_value const &one, json_object *two, bool &fail) { switch (one.type) { case mvt_string: if (two->type != JSON_STRING) { @@ -152,7 +152,7 @@ int compare(mvt_value one, json_object *two, bool &fail) { 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); case mvt_double: case mvt_float: diff --git a/mvt.cpp b/mvt.cpp index 6b0d2bdc..416f1fb3 100644 --- a/mvt.cpp +++ b/mvt.cpp @@ -329,7 +329,7 @@ std::string mvt_tile::encode() { switch (pbv.type) { case mvt_string: - value_writer.add_string(1, pbv.string_value); + value_writer.add_string(1, *(pbv.string_value)); break; case mvt_float: value_writer.add_float(2, pbv.numeric_value.float_value); @@ -458,7 +458,7 @@ bool mvt_value::operator<(const mvt_value &o) const { if (type == o.type) { switch (type) { case mvt_string: - return string_value < o.string_value; + return *string_value < *(o.string_value); case mvt_float: return numeric_value.float_value < o.numeric_value.float_value; @@ -494,7 +494,7 @@ bool mvt_value::operator==(const mvt_value &o) const { if (type == o.type) { switch (type) { case mvt_string: - return string_value == o.string_value; + return *string_value == *(o.string_value); case mvt_float: return numeric_value.float_value == o.numeric_value.float_value; @@ -550,7 +550,7 @@ static std::string quote(std::string const &s) { std::string mvt_value::toString() const { switch (type) { case mvt_string: - return quote(string_value); + return quote(*string_value); case mvt_int: return std::to_string(numeric_value.int_value); case mvt_double: { @@ -767,7 +767,7 @@ serial_val mvt_value_to_serial_val(mvt_value const &v) { switch (v.type) { case mvt_string: sv.type = mvt_string; - sv.s = v.string_value; + sv.s = *(v.string_value); break; case mvt_float: sv.type = mvt_double; @@ -809,7 +809,7 @@ serial_val mvt_value_to_serial_val(mvt_value const &v) { long long mvt_value_to_long_long(mvt_value const &v) { switch (v.type) { case mvt_string: - return atoll(v.string_value.c_str()); + return atoll(v.string_value->c_str()); case mvt_float: return v.numeric_value.float_value; case mvt_double: diff --git a/mvt.hpp b/mvt.hpp index a0b3c123..b3611998 100644 --- a/mvt.hpp +++ b/mvt.hpp @@ -6,6 +6,7 @@ #include #include #include +#include #include "errors.hpp" @@ -74,7 +75,7 @@ enum mvt_value_type { struct mvt_value { mvt_value_type type; - std::string string_value; + std::optional string_value; union { float float_value; double double_value; @@ -104,7 +105,7 @@ struct std::hash { switch (k.type) { case mvt_string: - return std::hash()(k.string_value); + return std::hash()(*(k.string_value)); case mvt_float: return std::hash()(k.numeric_value.float_value); diff --git a/tile.cpp b/tile.cpp index 8166a672..46875fd6 100644 --- a/tile.cpp +++ b/tile.cpp @@ -217,7 +217,7 @@ void decode_meta(std::vector const &metakeys, std::vector mvt_value key = retrieve_string(metakeys[i], stringpool, NULL); mvt_value value = retrieve_string(metavals[i], stringpool, &otype); - layer.tag(feature, key.string_value, value); + layer.tag(feature, *(key.string_value), value); } } @@ -227,9 +227,9 @@ static int metacmp(const std::vector &keys1, const std::vector key2.string_value) { + } else if (*(key1.string_value) > *(key2.string_value)) { return 1; } @@ -273,7 +273,7 @@ static mvt_value find_attribute_value(const struct coalesce *c1, std::string key for (size_t i = 0; i < keys1.size(); i++) { mvt_value key1 = retrieve_string(keys1[i], stringpool1, NULL); - if (key == key1.string_value) { + if (key == *(key1.string_value)) { return retrieve_string(values1[i], stringpool1, NULL); } } diff --git a/write_json.cpp b/write_json.cpp index b859926b..c3624a4d 100644 --- a/write_json.cpp +++ b/write_json.cpp @@ -339,7 +339,7 @@ void layer_to_geojson(mvt_layer const &layer, unsigned z, unsigned x, unsigned y switch (val.type) { case mvt_string: state.json_write_string(key); - state.json_write_string(val.string_value); + state.json_write_string(*(val.string_value)); break; case mvt_int: state.json_write_string(key);