mirror of
https://github.com/thunderbrewhq/squall.git
synced 2025-12-12 02:22:30 +00:00
feat(string): add SStrPrintf
This commit is contained in:
parent
d9e7f05c15
commit
3e24b01fe1
3 changed files with 55 additions and 0 deletions
|
|
@ -4,6 +4,8 @@
|
|||
#include "storm/string/bjhash.hpp"
|
||||
#include <cctype>
|
||||
#include <cmath>
|
||||
#include <cstdarg>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <strings.h>
|
||||
|
||||
|
|
@ -167,6 +169,29 @@ void InitializeFloatDigits() {
|
|||
}
|
||||
}
|
||||
|
||||
size_t ISStrVPrintf(const char* format, va_list va, char* dest, size_t maxchars) {
|
||||
if (!maxchars) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
size_t result;
|
||||
|
||||
if (maxchars == STORM_MAX_STR) {
|
||||
// TODO conditional vsoprintf;
|
||||
result = vsprintf(dest, format, va);
|
||||
} else {
|
||||
// TODO conditional vsnoprintf;
|
||||
result = vsnprintf(dest, maxchars, format, va);
|
||||
|
||||
if (result >= maxchars) {
|
||||
result = maxchars - 1;
|
||||
dest[result] = '\0';
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void SStrInitialize() {
|
||||
if (!s_initialized) {
|
||||
InitializeFloatDigits();
|
||||
|
|
@ -347,6 +372,16 @@ uint32_t SStrPack(char* dest, const char* source, uint32_t destsize) {
|
|||
return i - dest;
|
||||
}
|
||||
|
||||
size_t SStrPrintf(char* dest, size_t maxchars, const char* format, ...) {
|
||||
va_list va;
|
||||
va_start(va, format);
|
||||
|
||||
STORM_ASSERT(dest);
|
||||
STORM_ASSERT(format);
|
||||
|
||||
return ISStrVPrintf(format, va, dest, maxchars);
|
||||
}
|
||||
|
||||
const char* SStrStr(const char* string, const char* search) {
|
||||
STORM_ASSERT(string);
|
||||
STORM_ASSERT(search);
|
||||
|
|
|
|||
|
|
@ -27,6 +27,8 @@ void SStrLower(char* string);
|
|||
|
||||
uint32_t SStrPack(char* dest, const char* source, uint32_t destsize);
|
||||
|
||||
size_t SStrPrintf(char* dest, size_t maxchars, const char* format, ...);
|
||||
|
||||
const char* SStrStr(const char* string, const char* search);
|
||||
|
||||
void SStrTokenize(const char** string, char* buffer, size_t bufferchars, const char* whitespace, int32_t* quoted);
|
||||
|
|
|
|||
|
|
@ -207,6 +207,24 @@ TEST_CASE("SStrPack", "[string]") {
|
|||
}
|
||||
}
|
||||
|
||||
TEST_CASE("SStrPrintf", "[string]") {
|
||||
SECTION("fills dest with formatted string") {
|
||||
char dest[100] = { 0 };
|
||||
auto format = "%s - %s";
|
||||
auto length = SStrPrintf(dest, 100, format, "foo", "bar");
|
||||
REQUIRE(length == 9);
|
||||
REQUIRE(!SStrCmp(dest, "foo - bar", SStrLen("foo - bar")));
|
||||
}
|
||||
|
||||
SECTION("fills dest with empty string") {
|
||||
char dest[100] = { 0 };
|
||||
auto format = "";
|
||||
auto length = SStrPrintf(dest, 100, format);
|
||||
REQUIRE(length == 0);
|
||||
REQUIRE(!SStrCmp(dest, "", SStrLen("")));
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("SStrStr", "[string]") {
|
||||
SECTION("finds substring when it exists at end of string") {
|
||||
auto string = "foobar";
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue