diff --git a/storm/String.cpp b/storm/String.cpp index b714ae4..6afd66e 100644 --- a/storm/String.cpp +++ b/storm/String.cpp @@ -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) { diff --git a/storm/String.hpp b/storm/String.hpp index 0fb1a77..8078095 100644 --- a/storm/String.hpp +++ b/storm/String.hpp @@ -1,6 +1,7 @@ #ifndef STORM_STRING_HPP #define STORM_STRING_HPP +#include #include #include @@ -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); diff --git a/test/String.cpp b/test/String.cpp index 19c1680..5a508b0 100644 --- a/test/String.cpp +++ b/test/String.cpp @@ -3,6 +3,7 @@ #include "test/Test.hpp" #include +#include #include @@ -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";