feat(str): add SStrToUnsigned

This commit is contained in:
Adam Heinermann 2025-08-31 16:28:04 -07:00 committed by fallenoak
parent e19e394a79
commit 2408166d5d
3 changed files with 72 additions and 0 deletions

View file

@ -678,6 +678,22 @@ int32_t SStrToInt(const char* string) {
return result; 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) { void SStrUpper(char* string) {
while (*string) { while (*string) {
*string = static_cast<char>(toupper(*string)); *string = static_cast<char>(toupper(*string));

View file

@ -41,6 +41,8 @@ float SStrToFloat(const char* string);
int32_t SStrToInt(const char* string); int32_t SStrToInt(const char* string);
uint32_t SStrToUnsigned(const char* string);
void SStrUpper(char* string); void SStrUpper(char* string);
#endif #endif

View file

@ -374,6 +374,13 @@ TEST_CASE("SStrPrintf", "[string]") {
REQUIRE(!SStrCmp(dest, "wow")); 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]") { 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]") { TEST_CASE("SStrUpper", "[string]") {
SECTION("rewrites lowercase string to uppercase correctly") { SECTION("rewrites lowercase string to uppercase correctly") {
char string[] = "foobar"; char string[] = "foobar";