Skip to content

Commit

Permalink
Improve code formatting consistency
Browse files Browse the repository at this point in the history
  • Loading branch information
adams85 committed Mar 5, 2024
1 parent cab5939 commit 36098ba
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 38 deletions.
12 changes: 6 additions & 6 deletions include/configcat/keyvalue.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ namespace configcat {
return string_format("%d", alt);
}
else if constexpr (is_same_v<T, double>) {
return numberToString(alt);
return number_to_string(alt);
}
else {
static_assert(always_false_v<T>, "Non-exhaustive visitor.");
Expand Down
2 changes: 1 addition & 1 deletion src/configcatlogger.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class LogEntry {
LogEntry& operator<<(const std::vector<std::string>& v) {
if (logger->isEnabled(level)) {
message += "[";
appendStringList(*this, v);
append_stringlist(*this, v);
message += "]";
}
return *this;
Expand Down
2 changes: 1 addition & 1 deletion src/configcatuser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<decltype(alt)>;
if constexpr (is_same_v<T, date_time_t>) {
j[nameRef] = formatDateTimeISO(alt);
j[nameRef] = datetime_to_isostring(alt);
}
else {
j[nameRef] = alt;
Expand Down
8 changes: 4 additions & 4 deletions src/evaluatelogbuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

Expand All @@ -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());
}
}

Expand Down Expand Up @@ -331,7 +331,7 @@ namespace configcat {
else if (const auto stringArrayPtr = get_if<vector<string>>(&comparisonValue)) {
ostringstream ss;
ss << "[";
appendStringList(ss, *stringArrayPtr);
append_stringlist(ss, *stringArrayPtr);
ss << "]";
return str = ss.str();
}
Expand Down
16 changes: 8 additions & 8 deletions src/rolloutevaluator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
}

Expand Down Expand Up @@ -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());
}

Expand Down Expand Up @@ -944,11 +944,11 @@ namespace configcat {
return alt;
}
else if constexpr (is_same_v<T, double>) {
return numberToString(alt);
return number_to_string(alt);
}
else if constexpr (is_same_v<T, date_time_t>) {
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<T, vector<string>>) {
nlohmann::json j = alt;
Expand Down Expand Up @@ -998,7 +998,7 @@ namespace configcat {
return *numberPtr;
}
else if (const auto textPtr = get_if<string>(&attributeValue); textPtr) {
auto number = numberFromString(*textPtr);
auto number = number_from_string(*textPtr);
if (number) return *number;
}

Expand All @@ -1010,14 +1010,14 @@ namespace configcat {
const UserCondition& condition, const std::string& key) const {

if (const auto dateTimePtr = get_if<date_time_t>(&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<double>(&attributeValue); numberPtr) {
return *numberPtr;
}
else if (const auto textPtr = get_if<string>(&attributeValue); textPtr) {
auto number = numberFromString(*textPtr);
auto number = number_from_string(*textPtr);
if (number) return *number;
}

Expand Down
12 changes: 6 additions & 6 deletions src/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ using namespace std;

namespace configcat {

std::optional<double> dateTimeToUnixTimeSeconds(const std::chrono::system_clock::time_point& tp) {
std::optional<double> 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;

// Allow values only between 0001-01-01T00:00:00.000Z and 9999-12-31T23:59:59.999
return timestamp < -62135596800 || 253402300800 <= timestamp ? nullopt : optional<double>(timestamp);
}

std::optional<std::chrono::system_clock::time_point> dateTimeFromUnixTimeSeconds(double timestamp) {
std::optional<std::chrono::system_clock::time_point> 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;
Expand All @@ -43,7 +43,7 @@ namespace configcat {
return chrono::system_clock::time_point{ chrono::milliseconds{ static_cast<long long>(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<chrono::milliseconds>(remainder).count();
Expand Down Expand Up @@ -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";
}
Expand Down Expand Up @@ -120,7 +120,7 @@ namespace configcat {
return str;
}

std::optional<double> numberFromString(const std::string& str) {
std::optional<double> number_from_string(const std::string& str) {
if (str.empty()) return nullopt;

auto strPtr = const_cast<string*>(&str);
Expand Down Expand Up @@ -176,7 +176,7 @@ namespace configcat {
return value;
}

std::optional<long long> integerFromString(const std::string& str) {
std::optional<long long> integer_from_string(const std::string& str) {
if (str.empty()) return nullopt;

auto strPtr = const_cast<string*>(&str);
Expand Down
14 changes: 7 additions & 7 deletions src/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ inline double getUtcNowSecondsSinceEpoch() {
return std::chrono::duration<double>(duration).count();
}

std::optional<double> dateTimeToUnixTimeSeconds(const std::chrono::system_clock::time_point& tp);
std::optional<std::chrono::system_clock::time_point> dateTimeFromUnixTimeSeconds(double timestamp);
std::optional<double> datetime_to_unixtimeseconds(const std::chrono::system_clock::time_point& tp);
std::optional<std::chrono::system_clock::time_point> 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<typename... Args>
std::string string_format(const std::string& format, Args&&... args) {
Expand All @@ -50,7 +50,7 @@ std::string string_format(const std::string& format, Args&&... args) {
}

template<typename StreamType>
StreamType& appendStringList(
StreamType& append_stringlist(
StreamType& stream,
const std::vector<std::string>& items,
size_t maxLength = 0,
Expand Down Expand Up @@ -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<double> numberFromString(const std::string& str);
std::optional<double> number_from_string(const std::string& str);

std::optional<long long> integerFromString(const std::string& str);
std::optional<long long> integer_from_string(const std::string& str);

} // namespace configcat
8 changes: 4 additions & 4 deletions test/test-utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -70,7 +70,7 @@ class numberToStringTestSuite : public ::testing::TestWithParam<std::tuple<doubl

TEST_P(numberToStringTestSuite, numberToStringTest) {
auto [input, expectedOutput] = GetParam();
auto actualOutput = numberToString(input);
auto actualOutput = number_to_string(input);

ASSERT_EQ(expectedOutput, actualOutput);
}
Expand Down Expand Up @@ -119,7 +119,7 @@ class numberFromStringTestSuite : public ::testing::TestWithParam<std::tuple<std
TEST_P(numberFromStringTestSuite, numberFromStringTest) {
auto [input, expectedOutput] = GetParam();
string inputCopy(input);
auto actualOutput = numberFromString(input);
auto actualOutput = number_from_string(input);

ASSERT_EQ(input, inputCopy);
ASSERT_EQ(expectedOutput.has_value(), actualOutput.has_value());
Expand Down Expand Up @@ -175,7 +175,7 @@ class integerFromStringTestSuite : public ::testing::TestWithParam<std::tuple<st
TEST_P(integerFromStringTestSuite, integerFromStringTest) {
auto [input, expectedOutput] = GetParam();
string inputCopy(input);
auto actualOutput = integerFromString(input);
auto actualOutput = integer_from_string(input);

ASSERT_EQ(input, inputCopy);
ASSERT_EQ(expectedOutput.has_value(), actualOutput.has_value());
Expand Down

0 comments on commit 36098ba

Please sign in to comment.