diff --git a/bc/String.cpp b/bc/String.cpp new file mode 100644 index 0000000..20c0599 --- /dev/null +++ b/bc/String.cpp @@ -0,0 +1,54 @@ +#include "bc/String.hpp" +#include + +int32_t Blizzard::String::Copy(char* dst, const char* src, size_t len) { + if (!len || !dst) { + return 0; + } + + if (!src) { + *dst = 0; + return 0; + } + + char* v3 = dst + len - 1; + + char v4; + const char* v5; + char* v6; + + int32_t result; + + if (dst < v3 && (v4 = *src, v5 = src, v6 = dst, *src)) { + do { + *v6++ = v4; + + if (v3 <= v6) { + break; + } + + v4 = (v5++)[1]; + } while ( v4 ); + + result = v6 - dst; + } else { + v6 = dst; + result = 0; + } + + *v6 = 0; + + return result; +} + +uint32_t Blizzard::String::Length(const char* str) { + if (str) { + return strlen(str); + } + + return 0; +} + +void Blizzard::String::MemFill(void* dst, uint32_t len, uint8_t fill) { + memset(dst, fill, len); +} diff --git a/bc/String.hpp b/bc/String.hpp new file mode 100644 index 0000000..aea8132 --- /dev/null +++ b/bc/String.hpp @@ -0,0 +1,18 @@ +#ifndef BC_STRING_HPP +#define BC_STRING_HPP + +#include +#include + +namespace Blizzard { +namespace String { + +// Functions +int32_t Copy(char* dst, const char* src, size_t len); +uint32_t Length(const char* str); +void MemFill(void* dst, uint32_t len, uint8_t fill); + +} // namespace String +} // namespace Blizzard + +#endif diff --git a/test/String.cpp b/test/String.cpp new file mode 100644 index 0000000..e7cd84a --- /dev/null +++ b/test/String.cpp @@ -0,0 +1,36 @@ +#include "bc/String.hpp" +#include "test/Test.hpp" + +TEST_CASE("Blizzard::String::Copy", "[string]") { + SECTION("copies src to dst") { + auto src = "abC"; + char dst[4] = { 0x11, 0x22, 0x33, 0x44 }; + + Blizzard::String::Copy(dst, src, sizeof(dst)); + + REQUIRE(dst[0] == 'a'); + REQUIRE(dst[1] == 'b'); + REQUIRE(dst[2] == 'C'); + REQUIRE(dst[3] == '\0'); + } +} + +TEST_CASE("Blizzard::String::Length", "[string]") { + SECTION("correctly determines length of 'abc'") { + auto str = "abC"; + + REQUIRE(Blizzard::String::Length(str) == 3); + } +} + +TEST_CASE("Blizzard::String::MemFill", "[string]") { + SECTION("fills destination with given value") { + char dst[3] = { 0x11, 0x22, 0x33 }; + + Blizzard::String::MemFill(dst, sizeof(dst), 0x66); + + REQUIRE(dst[0] == 0x66); + REQUIRE(dst[1] == 0x66); + REQUIRE(dst[2] == 0x66); + } +}