From 36098ba2a93a17d4387fe8f9c73f56106ee0318b Mon Sep 17 00:00:00 2001 From: Adam Simon Date: Tue, 5 Mar 2024 18:26:32 +0100 Subject: [PATCH] Improve code formatting consistency --- include/configcat/keyvalue.h | 12 ++++++------ src/config.cpp | 2 +- src/configcatlogger.h | 2 +- src/configcatuser.cpp | 2 +- src/evaluatelogbuilder.cpp | 8 ++++---- src/rolloutevaluator.cpp | 16 ++++++++-------- src/utils.cpp | 12 ++++++------ src/utils.h | 14 +++++++------- test/test-utils.cpp | 8 ++++---- 9 files changed, 38 insertions(+), 38 deletions(-) diff --git a/include/configcat/keyvalue.h b/include/configcat/keyvalue.h index 52a956e..0d27b95 100644 --- a/include/configcat/keyvalue.h +++ b/include/configcat/keyvalue.h @@ -6,14 +6,14 @@ namespace configcat { struct KeyValue { - KeyValue(const std::string& key, const Value& value): - key(key), - value(value) { + KeyValue(const std::string& key, const Value& value) + : key(key) + , value(value) { } - KeyValue(const std::string& key, Value&& value) : - key(key), - value(value) { + KeyValue(const std::string& key, Value&& value) + : key(key) + , value(value) { } std::string key; diff --git a/src/config.cpp b/src/config.cpp index da07c80..e85c1c2 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -54,7 +54,7 @@ namespace configcat { return string_format("%d", alt); } else if constexpr (is_same_v) { - return numberToString(alt); + return number_to_string(alt); } else { static_assert(always_false_v, "Non-exhaustive visitor."); diff --git a/src/configcatlogger.h b/src/configcatlogger.h index 72da68b..f3dcafd 100644 --- a/src/configcatlogger.h +++ b/src/configcatlogger.h @@ -84,7 +84,7 @@ class LogEntry { LogEntry& operator<<(const std::vector& v) { if (logger->isEnabled(level)) { message += "["; - appendStringList(*this, v); + append_stringlist(*this, v); message += "]"; } return *this; diff --git a/src/configcatuser.cpp b/src/configcatuser.cpp index c115e80..00584b4 100644 --- a/src/configcatuser.cpp +++ b/src/configcatuser.cpp @@ -37,7 +37,7 @@ string ConfigCatUser::toJson() const { visit([&j, &nameRef = name] (auto&& alt) { // rebind reference to keep clang compiler happy (https://stackoverflow.com/a/74376436) using T = decay_t; if constexpr (is_same_v) { - j[nameRef] = formatDateTimeISO(alt); + j[nameRef] = datetime_to_isostring(alt); } else { j[nameRef] = alt; diff --git a/src/evaluatelogbuilder.cpp b/src/evaluatelogbuilder.cpp index c42d376..772ab9e 100644 --- a/src/evaluatelogbuilder.cpp +++ b/src/evaluatelogbuilder.cpp @@ -61,7 +61,7 @@ namespace configcat { comparisonAttribute.c_str(), getUserComparatorText(comparator)); - appendStringList(this->ss, *comparisonValuesPtr, kStringListMaxCount, [](size_t count) { + append_stringlist(this->ss, *comparisonValuesPtr, kStringListMaxCount, [](size_t count) { return string_format(", ... <%d more %s>", count, count ? kValueText : kValuesText); }); @@ -76,12 +76,12 @@ namespace configcat { } if (isDateTime) { - if (const auto tp = dateTimeFromUnixTimeSeconds(*comparisonValuePtr); tp) { + if (const auto tp = datetime_from_unixtimeseconds(*comparisonValuePtr); tp) { return appendFormat("User.%s %s '%g' (%s UTC)", comparisonAttribute.c_str(), getUserComparatorText(comparator), *comparisonValuePtr, - formatDateTimeISO(*tp).c_str()); + datetime_to_isostring(*tp).c_str()); } } @@ -331,7 +331,7 @@ namespace configcat { else if (const auto stringArrayPtr = get_if>(&comparisonValue)) { ostringstream ss; ss << "["; - appendStringList(ss, *stringArrayPtr); + append_stringlist(ss, *stringArrayPtr); ss << "]"; return str = ss.str(); } diff --git a/src/rolloutevaluator.cpp b/src/rolloutevaluator.cpp index 9d91706..51f2a66 100644 --- a/src/rolloutevaluator.cpp +++ b/src/rolloutevaluator.cpp @@ -629,7 +629,7 @@ namespace configcat { size_t sliceLength; if (index == string::npos - || (sliceLength = integerFromString(comparisonValue.substr(0, index)).value_or(-1)) < 0) { + || (sliceLength = integer_from_string(comparisonValue.substr(0, index)).value_or(-1)) < 0) { throw runtime_error("Comparison value is missing or invalid."); } @@ -805,7 +805,7 @@ namespace configcat { visitedFlags->push_back(prerequisiteFlagKey); ostringstream ss; ss << "Circular dependency detected between the following depending flags: "; - appendStringList(ss, *visitedFlags, 0, nullopt, " -> "); + append_stringlist(ss, *visitedFlags, 0, nullopt, " -> "); throw runtime_error(ss.str()); } @@ -944,11 +944,11 @@ namespace configcat { return alt; } else if constexpr (is_same_v) { - return numberToString(alt); + return number_to_string(alt); } else if constexpr (is_same_v) { - const auto unixTimeSeconds = dateTimeToUnixTimeSeconds(alt); - return numberToString(unixTimeSeconds ? *unixTimeSeconds : NAN); + const auto unixTimeSeconds = datetime_to_unixtimeseconds(alt); + return number_to_string(unixTimeSeconds ? *unixTimeSeconds : NAN); } else if constexpr (is_same_v>) { nlohmann::json j = alt; @@ -998,7 +998,7 @@ namespace configcat { return *numberPtr; } else if (const auto textPtr = get_if(&attributeValue); textPtr) { - auto number = numberFromString(*textPtr); + auto number = number_from_string(*textPtr); if (number) return *number; } @@ -1010,14 +1010,14 @@ namespace configcat { const UserCondition& condition, const std::string& key) const { if (const auto dateTimePtr = get_if(&attributeValue); dateTimePtr) { - const auto unixTimeSeconds = dateTimeToUnixTimeSeconds(*dateTimePtr); + const auto unixTimeSeconds = datetime_to_unixtimeseconds(*dateTimePtr); if (unixTimeSeconds) return *unixTimeSeconds; } else if (const auto numberPtr = get_if(&attributeValue); numberPtr) { return *numberPtr; } else if (const auto textPtr = get_if(&attributeValue); textPtr) { - auto number = numberFromString(*textPtr); + auto number = number_from_string(*textPtr); if (number) return *number; } diff --git a/src/utils.cpp b/src/utils.cpp index b6a2d73..1918050 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -26,7 +26,7 @@ using namespace std; namespace configcat { - std::optional dateTimeToUnixTimeSeconds(const std::chrono::system_clock::time_point& tp) { + std::optional datetime_to_unixtimeseconds(const std::chrono::system_clock::time_point& tp) { long long millisecondsSinceEpoch = tp.time_since_epoch() / std::chrono::milliseconds(1); auto timestamp = millisecondsSinceEpoch / 1000.0; @@ -34,7 +34,7 @@ namespace configcat { return timestamp < -62135596800 || 253402300800 <= timestamp ? nullopt : optional(timestamp); } - std::optional dateTimeFromUnixTimeSeconds(double timestamp) { + std::optional datetime_from_unixtimeseconds(double timestamp) { // Allow values only between 0001-01-01T00:00:00.000Z and 9999-12-31T23:59:59.999 if (timestamp < -62135596800 || 253402300800 <= timestamp) { return nullopt; @@ -43,7 +43,7 @@ namespace configcat { return chrono::system_clock::time_point{ chrono::milliseconds{ static_cast(timestamp) } }; } - std::string formatDateTimeISO(const std::chrono::system_clock::time_point& tp) { + std::string datetime_to_isostring(const std::chrono::system_clock::time_point& tp) { const auto secondsSinceEpoch = chrono::system_clock::to_time_t(tp); const auto remainder = (tp - chrono::system_clock::from_time_t(secondsSinceEpoch)); const auto milliseconds = chrono::duration_cast(remainder).count(); @@ -86,7 +86,7 @@ namespace configcat { return min(17, -exp); } - std::string numberToString(double number) { + std::string number_to_string(double number) { if (isnan(number)) { return "NaN"; } @@ -120,7 +120,7 @@ namespace configcat { return str; } - std::optional numberFromString(const std::string& str) { + std::optional number_from_string(const std::string& str) { if (str.empty()) return nullopt; auto strPtr = const_cast(&str); @@ -176,7 +176,7 @@ namespace configcat { return value; } - std::optional integerFromString(const std::string& str) { + std::optional integer_from_string(const std::string& str) { if (str.empty()) return nullopt; auto strPtr = const_cast(&str); diff --git a/src/utils.h b/src/utils.h index 1c00835..ec0c2bb 100644 --- a/src/utils.h +++ b/src/utils.h @@ -25,10 +25,10 @@ inline double getUtcNowSecondsSinceEpoch() { return std::chrono::duration(duration).count(); } -std::optional dateTimeToUnixTimeSeconds(const std::chrono::system_clock::time_point& tp); -std::optional dateTimeFromUnixTimeSeconds(double timestamp); +std::optional datetime_to_unixtimeseconds(const std::chrono::system_clock::time_point& tp); +std::optional datetime_from_unixtimeseconds(double timestamp); -std::string formatDateTimeISO(const std::chrono::system_clock::time_point& tp); +std::string datetime_to_isostring(const std::chrono::system_clock::time_point& tp); template std::string string_format(const std::string& format, Args&&... args) { @@ -50,7 +50,7 @@ std::string string_format(const std::string& format, Args&&... args) { } template -StreamType& appendStringList( +StreamType& append_stringlist( StreamType& stream, const std::vector& items, size_t maxLength = 0, @@ -110,10 +110,10 @@ inline bool contains(const std::string& str, const std::string& sub) { return str.find(sub) != std::string::npos; } -std::string numberToString(double number); +std::string number_to_string(double number); -std::optional numberFromString(const std::string& str); +std::optional number_from_string(const std::string& str); -std::optional integerFromString(const std::string& str); +std::optional integer_from_string(const std::string& str); } // namespace configcat diff --git a/test/test-utils.cpp b/test/test-utils.cpp index 48e581d..f2824f7 100644 --- a/test/test-utils.cpp +++ b/test/test-utils.cpp @@ -60,7 +60,7 @@ TEST(UtilsTest, trim_test_1) { } TEST(UtilsTest, formatDateTimeISOTest) { - auto s = formatDateTimeISO(*dateTimeFromUnixTimeSeconds(0)); + auto s = datetime_to_isostring(*datetime_from_unixtimeseconds(0)); ASSERT_EQ(string("1970-01-01T00:00:00.000Z"), s); } @@ -70,7 +70,7 @@ class numberToStringTestSuite : public ::testing::TestWithParam