summaryrefslogtreecommitdiff
path: root/src/strops.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/strops.cpp')
-rw-r--r--src/strops.cpp44
1 files changed, 29 insertions, 15 deletions
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 <stdlib.h>
-#include <string.h>
#include <ctype.h>
#include <stdio.h>
@@ -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);