mirror of
https://github.com/thunderbrewhq/squall.git
synced 2025-12-12 02:22:30 +00:00
feat(string): add SStrChr
This commit is contained in:
parent
87cb6feb94
commit
0dafa3a70a
3 changed files with 57 additions and 0 deletions
|
|
@ -132,6 +132,24 @@ void GetNextTextUpper(uint32_t* orig, const char** string, uint32_t* upper) {
|
|||
}
|
||||
}
|
||||
|
||||
const char* SStrChr(const char* string, char search) {
|
||||
STORM_ASSERT(string);
|
||||
|
||||
if (!*string) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
while (*string != search) {
|
||||
string++;
|
||||
|
||||
if (!*string) {
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
return string;
|
||||
}
|
||||
|
||||
int32_t SStrCmp(const char* string1, const char* string2, size_t maxchars) {
|
||||
return strncmp(string1, string2, maxchars);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,6 +7,8 @@
|
|||
#define STORM_MAX_PATH 260
|
||||
#define STORM_MAX_STR 0x7FFFFFFF
|
||||
|
||||
const char* SStrChr(const char* string, char search);
|
||||
|
||||
int32_t SStrCmp(const char* string1, const char* string2, size_t maxchars);
|
||||
|
||||
int32_t SStrCmpI(const char* string1, const char* string2, size_t maxchars);
|
||||
|
|
|
|||
|
|
@ -2,6 +2,43 @@
|
|||
#include "storm/Memory.hpp"
|
||||
#include "test/Test.hpp"
|
||||
|
||||
TEST_CASE("SStrChr", "[string]") {
|
||||
SECTION("finds first character when it exists at start of string") {
|
||||
auto string = "foobar";
|
||||
auto search = 'f';
|
||||
auto result = SStrChr(string, search);
|
||||
REQUIRE(result == string);
|
||||
}
|
||||
|
||||
SECTION("finds first character when it exists in middle of string") {
|
||||
auto string = "foobar";
|
||||
auto search = 'b';
|
||||
auto result = SStrChr(string, search);
|
||||
REQUIRE(result == string + 3);
|
||||
}
|
||||
|
||||
SECTION("finds first character when it exists at end of string") {
|
||||
auto string = "foobar";
|
||||
auto search = 'r';
|
||||
auto result = SStrChr(string, search);
|
||||
REQUIRE(result == string + 5);
|
||||
}
|
||||
|
||||
SECTION("returns nullptr when character does not exist in string") {
|
||||
auto string = "foobar";
|
||||
auto search = 'z';
|
||||
auto result = SStrChr(string, search);
|
||||
REQUIRE(result == nullptr);
|
||||
}
|
||||
|
||||
SECTION("returns nullptr when string is empty") {
|
||||
auto string = "";
|
||||
auto search = 'z';
|
||||
auto result = SStrChr(string, search);
|
||||
REQUIRE(result == nullptr);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("SStrCmp", "[string]") {
|
||||
SECTION("compares two strings that exactly match correctly") {
|
||||
auto compare = SStrCmp("foo", "foo", STORM_MAX_STR);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue