From b278d242d03ba614779243ec9e9495fc95abea3d Mon Sep 17 00:00:00 2001 From: Aldrik Ramaekers Date: Sun, 5 Oct 2025 15:15:55 +0200 Subject: strops format --- src/strops.cpp | 44 +++++++++++++++++++++++++++++--------------- 1 file changed, 29 insertions(+), 15 deletions(-) (limited to 'src/strops.cpp') diff --git a/src/strops.cpp b/src/strops.cpp index 356ce5b..6142c01 100644 --- a/src/strops.cpp +++ b/src/strops.cpp @@ -15,7 +15,6 @@ */ #include -#include #include #include @@ -51,17 +50,32 @@ namespace strops { char* contains(char* haystack, char* needle) { do { - const char* h = haystack; - const char* n = needle; - while (tolower((unsigned char) *h) == tolower((unsigned char ) *n) && *n) { - h++; - n++; - } - if (*n == 0) { - return (char *) haystack; + const char* h = haystack; + const char* n = needle; + while (tolower((unsigned char) *h) == tolower((unsigned char ) *n) && *n) { + h++; + n++; + } + if (*n == 0) { + return (char *) haystack; } - } while (*haystack++); - return 0; + } while (*haystack++); + return 0; + } + + s32 format_va(char* s, size_t n, const char* format, va_list args) + { + s32 result = vsnprintf(s, n, format, args); + return result; + } + + s32 format(char* s, size_t n, const char* format, ...) + { + va_list args; + va_start (args, format); + s32 result = vsnprintf(s, n, format, args); + va_end (args); + return result; } void replace(char *buf, size_t buf_size, const char *search, const char *replace) @@ -99,28 +113,28 @@ namespace strops { void replace_int32(char *buf, size_t buf_size, const char *search, s32 number) { char num_buf[200]; - snprintf(num_buf, 200, "%d", number); + strops::format(num_buf, 200, "%d", number); strops::replace(buf, buf_size, search, num_buf); } void replace_int64(char *buf, size_t buf_size, const char *search, s64 number) { char num_buf[200]; - snprintf(num_buf, 200, "%lld", number); + strops::format(num_buf, 200, "%lld", number); strops::replace(buf, buf_size, search, num_buf); } void replace_float(char *buf, size_t buf_size, const char *search, float number, int decimals) { char num_buf[200]; - snprintf(num_buf, 200, "%.*f", decimals, number); + strops::format(num_buf, 200, "%.*f", decimals, number); strops::replace(buf, buf_size, search, num_buf); } char* get_json_value(const char *json, const char *key, char *out, size_t out_size, int skip) { char pattern[128]; - snprintf(pattern, sizeof(pattern), "\"%s\"", key); + strops::format(pattern, sizeof(pattern), "\"%s\"", key); const char *pos = strstr(json, pattern); while(skip > 0) { pos = strstr(pos+1, pattern); -- cgit v1.2.3-70-g09d2