diff --git a/packages/logging/include/logging/String.h b/packages/logging/include/logging/String.h index a4ceaf80..137fed96 100644 --- a/packages/logging/include/logging/String.h +++ b/packages/logging/include/logging/String.h @@ -58,8 +58,8 @@ namespace string { } } - bool cstring_equal(const char* a, const char* b, size_t a_offset = 0, size_t b_offset = 0); - bool partial_equality(const char* a, const char* b, size_t a_offset = 0, size_t b_offset = 0); + bool cstring_equal(const char* a, const char* b, size_t a_offset = 0, size_t b_offset = 0, size_t maxCount = 20); + bool partial_equality(const char* a, const char* b, size_t a_offset = 0, size_t b_offset = 0, size_t maxCount = 20); bool partial_equality(std::string_view a, std::string_view b, size_t a_offset = 0, size_t b_offset = 0); std::vector split(std::wstring_view text, std::wstring_view delim = L" \t\n", char escapeChar = '\"'); diff --git a/packages/logging/source/common/String.cpp b/packages/logging/source/common/String.cpp index 3cc19928..2db0bb6f 100644 --- a/packages/logging/source/common/String.cpp +++ b/packages/logging/source/common/String.cpp @@ -308,17 +308,17 @@ namespace string { return static_cast(count); } - bool cstring_equal(const char* a, const char* b, size_t a_offset, size_t b_offset) { - return !strcmp(a + a_offset, b + b_offset); + bool cstring_equal(const char* a, const char* b, size_t a_offset, size_t b_offset, size_t maxCount) { + return !strncmp(a + a_offset, b + b_offset, maxCount); } - bool partial_equality(const char* a, const char* b, size_t a_offset, size_t b_offset) { - for (size_t i = 0; i < a_offset; i++) { // check for missed null terminators before a_offset + bool partial_equality(const char* a, const char* b, size_t a_offset, size_t b_offset, size_t maxCount) { + for (size_t i = 0; i < a_offset && i < maxCount; i++) { // check for missed null terminators before a_offset if (a[i] == 0) { return false; } } - for (size_t i = 0; i < b_offset; i++) { // check for missed null terminators before b_offset + for (size_t i = 0; i < b_offset && i < maxCount; i++) { // check for missed null terminators before b_offset if (b[i] == 0) { return false; } @@ -326,7 +326,7 @@ namespace string { const char* a1 = a + a_offset; const char* b1 = b + b_offset; int i = 0; - for (i = 0; a1[i] != 0 && b1[i] != 0; i++) { + for (i = 0; a1[i] != 0 && b1[i] != 0 && i < maxCount; i++) { if (a1[i] != b1[i]) { return false; } diff --git a/packages/testing/tests/common/LoggingTest.cpp b/packages/testing/tests/common/LoggingTest.cpp index deb7a9f7..c2ab17ee 100644 --- a/packages/testing/tests/common/LoggingTest.cpp +++ b/packages/testing/tests/common/LoggingTest.cpp @@ -109,6 +109,11 @@ TEST(Logging, StringComparisons) { TEST_TRUE(l::string::partial_equality(d, e, 2, 0), ""); TEST_TRUE(l::string::partial_equality(d, f, 1, 1), ""); } + { + std::string a = "asdgkösd"; + TEST_TRUE(l::string::cstring_equal(a.c_str(), "asdg34643", 0, 0, 4), ""); + TEST_FALSE(l::string::cstring_equal(a.c_str(), "asdg34643", 0, 0, 5), ""); + } return 0; } @@ -142,33 +147,25 @@ TEST(Logging, TimeConversions) { } { auto unixtime = l::string::get_unix_epoch(); - auto localtime = l::string::convert_to_local_time_from_utc_time(unixtime); struct tm timeinfolocal; l::string::convert_to_tm(localtime, &timeinfolocal, true); - struct tm timeinfo; l::string::convert_to_local_tm_from_utc_time(unixtime, &timeinfo, true); - TEST_EQ(timeinfo.tm_hour, timeinfolocal.tm_hour, ""); } { auto unixtime = l::string::get_unix_epoch(); - int32_t fullDate[6]; l::string::to_local_time(unixtime, fullDate); - auto unixtime2 = l::string::to_unix_time_from_local(fullDate); - TEST_EQ(unixtime, unixtime2, ""); } { - auto unixtime = l::string::get_unix_epoch(); - int32_t fullDate[6]; - l::string::to_local_time(unixtime, fullDate); - auto unixtimelocal = l::string::to_unix_time_from_local(fullDate); - + auto unixtime = 1724097382; + auto timeString = l::string::get_local_time_string(unixtime); + TEST_TRUE(l::string::cstring_equal(timeString.c_str(), "2024-08-19 21:56:22"), ""); } return 0;