Make the string_value of mvt_value std::optional

This commit is contained in:
Erica Fischer
2024-01-25 11:12:23 -08:00
parent 09f271646d
commit a5eabab8bf
5 changed files with 21 additions and 20 deletions
+7 -7
View File
@@ -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:
+6 -6
View File
@@ -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:
+3 -2
View File
@@ -6,6 +6,7 @@
#include <unordered_map>
#include <set>
#include <vector>
#include <optional>
#include "errors.hpp"
@@ -74,7 +75,7 @@ enum mvt_value_type {
struct mvt_value {
mvt_value_type type;
std::string string_value;
std::optional<std::string> string_value;
union {
float float_value;
double double_value;
@@ -104,7 +105,7 @@ struct std::hash<mvt_value> {
switch (k.type) {
case mvt_string:
return std::hash<string>()(k.string_value);
return std::hash<string>()(*(k.string_value));
case mvt_float:
return std::hash<float>()(k.numeric_value.float_value);
+4 -4
View File
@@ -217,7 +217,7 @@ void decode_meta(std::vector<long long> const &metakeys, std::vector<long long>
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<long long> &keys1, const std::vector<long l
mvt_value key1 = retrieve_string(keys1[i], stringpool1, NULL);
mvt_value key2 = retrieve_string(keys2[i], stringpool2, NULL);
if (key1.string_value < key2.string_value) {
if (*(key1.string_value) < *(key2.string_value)) {
return -1;
} else if (key1.string_value > 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);
}
}
+1 -1
View File
@@ -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);