mirror of
https://github.com/thunderbrewhq/squall.git
synced 2025-12-12 10:32:29 +00:00
feat(string): add SStrToInt
This commit is contained in:
parent
e51d4cdaa1
commit
3d59699e1b
3 changed files with 70 additions and 0 deletions
|
|
@ -288,3 +288,27 @@ const char* SStrStr(const char* string, const char* search) {
|
||||||
|
|
||||||
return substring;
|
return substring;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int32_t SStrToInt(const char* string) {
|
||||||
|
STORM_ASSERT(string);
|
||||||
|
|
||||||
|
int32_t result = 0;
|
||||||
|
bool negative = false;
|
||||||
|
|
||||||
|
if (*string == '-') {
|
||||||
|
negative = true;
|
||||||
|
string++;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint32_t digit;
|
||||||
|
while ((digit = *string - '0') < 10) {
|
||||||
|
result = digit + (10 * result);
|
||||||
|
string++;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (negative) {
|
||||||
|
result = -result;
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -27,4 +27,6 @@ void SStrLower(char* string);
|
||||||
|
|
||||||
const char* SStrStr(const char* string, const char* search);
|
const char* SStrStr(const char* string, const char* search);
|
||||||
|
|
||||||
|
int32_t SStrToInt(const char* string);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
|
|
@ -217,3 +217,47 @@ TEST_CASE("SStrStr", "[string]") {
|
||||||
REQUIRE(substring == nullptr);
|
REQUIRE(substring == nullptr);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_CASE("SStrToInt", "[string]") {
|
||||||
|
SECTION("converts empty string to int") {
|
||||||
|
auto string = "";
|
||||||
|
auto result = SStrToInt(string);
|
||||||
|
REQUIRE(result == 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("converts whitespace string to int") {
|
||||||
|
auto string = " ";
|
||||||
|
auto result = SStrToInt(string);
|
||||||
|
REQUIRE(result == 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("converts string with positive number to int") {
|
||||||
|
auto string = "123";
|
||||||
|
auto result = SStrToInt(string);
|
||||||
|
REQUIRE(result == 123);
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("converts string with negative number to int") {
|
||||||
|
auto string = "-123";
|
||||||
|
auto result = SStrToInt(string);
|
||||||
|
REQUIRE(result == -123);
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("converts string with zero to int") {
|
||||||
|
auto string = "0";
|
||||||
|
auto result = SStrToInt(string);
|
||||||
|
REQUIRE(result == 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("converts string with leading zero to int") {
|
||||||
|
auto string = "01";
|
||||||
|
auto result = SStrToInt(string);
|
||||||
|
REQUIRE(result == 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("converts string with two whitespace-separated numbers to int") {
|
||||||
|
auto string = "123 456";
|
||||||
|
auto result = SStrToInt(string);
|
||||||
|
REQUIRE(result == 123);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue