From 2408166d5de35edd3c40bd6e614cc961e34b00cb Mon Sep 17 00:00:00 2001 From: Adam Heinermann Date: Sun, 31 Aug 2025 16:28:04 -0700 Subject: [PATCH] feat(str): add SStrToUnsigned --- storm/String.cpp | 16 ++++++++++++++ storm/String.hpp | 2 ++ test/String.cpp | 54 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 72 insertions(+) diff --git a/storm/String.cpp b/storm/String.cpp index 610fc65..241ab04 100644 --- a/storm/String.cpp +++ b/storm/String.cpp @@ -678,6 +678,22 @@ int32_t SStrToInt(const char* string) { return result; } +uint32_t SStrToUnsigned(const char* string) { + STORM_VALIDATE_BEGIN; + STORM_VALIDATE(string); + STORM_VALIDATE_END; + + 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 83e8c2b..c185041 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 60dd41a..ad27a2e 100644 --- a/test/String.cpp +++ b/test/String.cpp @@ -374,6 +374,13 @@ TEST_CASE("SStrPrintf", "[string]") { REQUIRE(!SStrCmp(dest, "wow")); } + SECTION("does nothing if maxchars is 0") { + char dest[10] = {}; + auto length = SStrPrintf(dest, 0, "%d", 69); + + REQUIRE(length == 0); + REQUIRE(!SStrCmp(dest, "")); + } } TEST_CASE("SStrStr", "[string]") { @@ -594,6 +601,53 @@ TEST_CASE("SStrToInt", "[string]") { } } +TEST_CASE("SStrToUnsigned", "[string]") { + SECTION("converts empty string to int") { + auto result = SStrToUnsigned(""); + REQUIRE(result == 0); + } + + SECTION("converts whitespace string to int") { + auto result = SStrToUnsigned(" "); + REQUIRE(result == 0); + } + + SECTION("converts random characters to int") { + auto result = SStrToUnsigned("abcd"); + REQUIRE(result == 0); + } + + SECTION("converts string with positive number to int") { + auto result = SStrToUnsigned("123"); + REQUIRE(result == 123); + } + + SECTION("returns 0 if number is negative") { + auto result = SStrToUnsigned("-123"); + REQUIRE(result == 0); + } + + SECTION("converts string with zero to int") { + auto result = SStrToUnsigned("0"); + REQUIRE(result == 0); + } + + SECTION("converts string with leading zero to int") { + auto result = SStrToUnsigned("01"); + REQUIRE(result == 1); + } + + SECTION("converts string with two whitespace-separated numbers to int") { + auto result = SStrToUnsigned("123 456"); + REQUIRE(result == 123); + } + + SECTION("converts max unsigned int") { + auto result = SStrToUnsigned("4294967295"); + REQUIRE(result == 4294967295u); + } +} + TEST_CASE("SStrUpper", "[string]") { SECTION("rewrites lowercase string to uppercase correctly") { char string[] = "foobar";