From eb26539b33acb1823cb175a38a4dc7e5bb8f0efc Mon Sep 17 00:00:00 2001 From: superp00t Date: Tue, 24 Dec 2024 14:08:06 -0500 Subject: [PATCH] feat(string): implement SStrToUnsigned --- storm/String.cpp | 14 ++++++++++++++ storm/String.hpp | 2 ++ test/String.cpp | 50 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 66 insertions(+) diff --git a/storm/String.cpp b/storm/String.cpp index 4696719..d9f21b9 100644 --- a/storm/String.cpp +++ b/storm/String.cpp @@ -669,6 +669,20 @@ int32_t SStrToInt(const char* string) { return result; } +uint32_t SStrToUnsigned(const char* string) { + STORM_ASSERT(string); + + uint32_t result = 0; + + uint32_t digit; + while ((digit = *string - '0') < 10) { + result = digit + (10 * result); + string++; + } + + return result; +} + void SStrUpper(char* string) { while (*string) { *string = static_cast(toupper(*string)); diff --git a/storm/String.hpp b/storm/String.hpp index 572db5a..657ef9d 100644 --- a/storm/String.hpp +++ b/storm/String.hpp @@ -41,6 +41,8 @@ float SStrToFloat(const char* string); int32_t SStrToInt(const char* string); +uint32_t SStrToUnsigned(const char* string); + void SStrUpper(char* string); #endif diff --git a/test/String.cpp b/test/String.cpp index dd0b343..470ccdb 100644 --- a/test/String.cpp +++ b/test/String.cpp @@ -405,6 +405,56 @@ TEST_CASE("SStrToInt", "[string]") { } } +TEST_CASE("SStrToUnsigned", "[string]") { + SECTION("converts empty string to unsigned") { + auto string = ""; + auto result = SStrToUnsigned(string); + REQUIRE(result == 0); + } + + SECTION("converts whitespace string to unsigned") { + auto string = " "; + auto result = SStrToUnsigned(string); + REQUIRE(result == 0); + } + + SECTION("converts string with positive number to unsigned") { + auto string = "123"; + auto result = SStrToUnsigned(string); + REQUIRE(result == 123); + } + + SECTION("converts string with negative number to unsigned") { + auto string = "-123"; + auto result = SStrToUnsigned(string); + REQUIRE(result == 0); + } + + SECTION("converts string with zero to unsigned") { + auto string = "0"; + auto result = SStrToUnsigned(string); + REQUIRE(result == 0); + } + + SECTION("converts string with leading zero to unsigned") { + auto string = "01"; + auto result = SStrToUnsigned(string); + REQUIRE(result == 1); + } + + SECTION("converts string with two whitespace-separated numbers to unsigned") { + auto string = "123 456"; + auto result = SStrToUnsigned(string); + REQUIRE(result == 123); + } + + SECTION("converts string to max unsigned value") { + auto string = "4294967295"; + auto result = SStrToUnsigned(string); + REQUIRE(result == 0xFFFFFFFF); + } +} + TEST_CASE("SStrUpper", "[string]") { SECTION("rewrites lowercase string to uppercase correctly") { auto lower = "foobar";