Skip to content

Commit f600d34

Browse files
kvpp: support reading hex strings
1 parent 5e52688 commit f600d34

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

include/kvpp/KV1.h

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ namespace kvpp {
1515
template<typename V>
1616
concept KV1ValueType = std::convertible_to<V, std::string_view>
1717
|| std::same_as<V, bool>
18-
|| std::same_as<V, int>
18+
|| std::same_as<V, int32_t>
19+
|| std::same_as<V, int64_t>
1920
|| std::same_as<V, float>;
2021

2122
template<typename S, typename K>
@@ -38,9 +39,17 @@ class KV1ElementBase {
3839
if constexpr (std::convertible_to<V, std::string_view>) {
3940
return this->value;
4041
} else if constexpr (std::same_as<V, bool>) {
41-
return static_cast<bool>(this->getValue<int>());
42-
} else if constexpr (std::same_as<V, int>) {
42+
return static_cast<bool>(this->getValue<int32_t>());
43+
} else if constexpr (std::same_as<V, int32_t>) {
44+
if (this->value.length() == 10 && this->value.starts_with("0x") && sourcepp::parser::text::isNumber(this->value.substr(2))) {
45+
return std::stoi(std::string{this->value.substr(2)}, nullptr, 16);
46+
}
4347
return std::stoi(std::string{this->value});
48+
} else if constexpr (std::same_as<V, int64_t>) {
49+
if (this->value.length() == 18 && this->value.starts_with("0x") && sourcepp::parser::text::isNumber(this->value.substr(2))) {
50+
return std::stoll(std::string{this->value.substr(2)}, nullptr, 16);
51+
}
52+
return std::stoll(std::string{this->value});
4453
} else if constexpr (std::same_as<V, float>) {
4554
return std::stof(std::string{this->value});
4655
}
@@ -206,7 +215,7 @@ class KV1ElementWritable : public KV1ElementBase<S, KV1ElementWritable<S>> {
206215
void setValue(V value_) {
207216
if constexpr (std::convertible_to<V, std::string_view>) {
208217
this->value = std::string_view{value_};
209-
} else if constexpr (std::same_as<V, bool> || std::same_as<V, int> || std::same_as<V, float>) {
218+
} else {
210219
this->setValue(std::to_string(value_));
211220
}
212221
}

0 commit comments

Comments
 (0)