feat(str): add SStrVPrintf

This commit is contained in:
Adam Heinermann 2025-08-31 17:00:56 -07:00 committed by fallenoak
parent 75dc4e57a6
commit 4b1904ad4e
3 changed files with 65 additions and 2 deletions

View file

@ -176,7 +176,7 @@ void InitializeFloatDigits() {
}
}
size_t ISStrVPrintf(const char* format, va_list va, char* dest, size_t maxchars) {
size_t ISStrVPrintf(char* dest, size_t maxchars, const char* format, va_list va) {
if (!maxchars) {
return 0;
}
@ -454,7 +454,16 @@ size_t SStrPrintf(char* dest, size_t maxchars, const char* format, ...) {
STORM_VALIDATE(format);
STORM_VALIDATE_END;
return ISStrVPrintf(format, va, dest, maxchars);
return ISStrVPrintf(dest, maxchars, format, va);
}
size_t SStrVPrintf(char* dest, size_t maxchars, const char* format, va_list arglist) {
STORM_VALIDATE_BEGIN;
STORM_VALIDATE(dest);
STORM_VALIDATE(format);
STORM_VALIDATE_END;
return ISStrVPrintf(dest, maxchars, format, arglist);
}
const char* SStrStr(const char* string, const char* search) {

View file

@ -1,6 +1,7 @@
#ifndef STORM_STRING_HPP
#define STORM_STRING_HPP
#include <cstdarg>
#include <cstdint>
#include <cstdlib>
@ -33,6 +34,8 @@ uint32_t SStrPack(char* dest, const char* source, uint32_t destsize);
size_t SStrPrintf(char* dest, size_t maxchars, const char* format, ...);
size_t SStrVPrintf(char* dest, size_t maxchars, const char* format, va_list arglist);
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);

View file

@ -3,6 +3,7 @@
#include "test/Test.hpp"
#include <cfloat>
#include <cstdarg>
#include <type_traits>
@ -384,6 +385,56 @@ TEST_CASE("SStrPrintf", "[string]") {
}
}
static size_t CallSStrVPrintf(char* dest, size_t maxchars, const char* format, ...) {
va_list va;
va_start(va, format);
auto length = SStrVPrintf(dest, maxchars, format, va);
va_end(va);
return length;
}
TEST_CASE("SStrVPrintf", "[string]") {
SECTION("fills dest with formatted string") {
char dest[100] = {};
auto length = CallSStrVPrintf(dest, sizeof(dest), "%s - %s", "foo", "bar");
REQUIRE(length == 9);
REQUIRE(!SStrCmp(dest, "foo - bar"));
}
SECTION("fills dest with int") {
char dest[100] = {};
auto length = CallSStrVPrintf(dest, sizeof(dest), "%d", 69);
REQUIRE(length == 2);
REQUIRE(!SStrCmp(dest, "69"));
}
SECTION("fills dest with empty string") {
char dest[100] = {};
auto length = CallSStrVPrintf(dest, sizeof(dest), "");
REQUIRE(length == 0);
REQUIRE(!SStrCmp(dest, ""));
}
SECTION("truncates when dest size is not large enough") {
char dest[4] = {};
auto length = CallSStrVPrintf(dest, sizeof(dest), "%s", "wowzers");
REQUIRE(length == 3);
REQUIRE(!SStrCmp(dest, "wow"));
}
SECTION("does nothing if maxchars is 0") {
char dest[10] = {};
auto length = CallSStrVPrintf(dest, 0, "%d", 69);
REQUIRE(length == 0);
REQUIRE(!SStrCmp(dest, ""));
}
}
TEST_CASE("SStrStr", "[string]") {
auto string = "foobar";