Skip to content

Commit

Permalink
Few fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
linuscu committed Aug 19, 2024
1 parent 50ab886 commit c76822e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 19 deletions.
4 changes: 2 additions & 2 deletions packages/logging/include/logging/String.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::wstring_view> split(std::wstring_view text, std::wstring_view delim = L" \t\n", char escapeChar = '\"');
Expand Down
12 changes: 6 additions & 6 deletions packages/logging/source/common/String.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -308,25 +308,25 @@ namespace string {
return static_cast<size_t>(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;
}
}
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;
}
Expand Down
19 changes: 8 additions & 11 deletions packages/testing/tests/common/LoggingTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit c76822e

Please sign in to comment.