mirror of
https://github.com/EQEmu/Server.git
synced 2025-12-11 21:01:29 +00:00
Basic string tests, plus fix for StringFormat returning a std::string that was just very subtley malformed.
This commit is contained in:
parent
b36cc3ab08
commit
412835d7fa
@ -57,6 +57,7 @@ const std::string vStringFormat(const char* format, va_list args)
|
|||||||
va_copy(tmpargs,args);
|
va_copy(tmpargs,args);
|
||||||
characters_used = vsnprintf(&output[0], output.capacity(), format, tmpargs);
|
characters_used = vsnprintf(&output[0], output.capacity(), format, tmpargs);
|
||||||
va_end(tmpargs);
|
va_end(tmpargs);
|
||||||
|
output.resize(characters_used);
|
||||||
|
|
||||||
if (characters_used < 0) {
|
if (characters_used < 0) {
|
||||||
// We shouldn't have a format error by this point, but I can't imagine what error we
|
// We shouldn't have a format error by this point, but I can't imagine what error we
|
||||||
@ -72,6 +73,8 @@ const std::string vStringFormat(const char* format, va_list args)
|
|||||||
characters_used = vsnprintf(&output[0], output.capacity(), format, tmpargs);
|
characters_used = vsnprintf(&output[0], output.capacity(), format, tmpargs);
|
||||||
va_end(tmpargs);
|
va_end(tmpargs);
|
||||||
|
|
||||||
|
output.resize(characters_used);
|
||||||
|
|
||||||
if (characters_used < 0) {
|
if (characters_used < 0) {
|
||||||
// We shouldn't have a format error by this point, but I can't imagine what error we
|
// We shouldn't have a format error by this point, but I can't imagine what error we
|
||||||
// could have by this point. Still, return empty string;
|
// could have by this point. Still, return empty string;
|
||||||
@ -380,6 +383,42 @@ std::string EscapeString(const std::string &s) {
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::string EscapeString(const char *src, size_t sz) {
|
||||||
|
std::string ret;
|
||||||
|
|
||||||
|
for(size_t i = 0; i < sz; ++i) {
|
||||||
|
char c = src[i];
|
||||||
|
switch(c) {
|
||||||
|
case '\x00':
|
||||||
|
ret += "\\x00";
|
||||||
|
break;
|
||||||
|
case '\n':
|
||||||
|
ret += "\\n";
|
||||||
|
break;
|
||||||
|
case '\r':
|
||||||
|
ret += "\\r";
|
||||||
|
break;
|
||||||
|
case '\\':
|
||||||
|
ret += "\\\\";
|
||||||
|
break;
|
||||||
|
case '\'':
|
||||||
|
ret += "\\'";
|
||||||
|
break;
|
||||||
|
case '\"':
|
||||||
|
ret += "\\\"";
|
||||||
|
break;
|
||||||
|
case '\x1a':
|
||||||
|
ret += "\\x1a";
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
ret.push_back(c);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
bool isAlphaNumeric(const char *text)
|
bool isAlphaNumeric(const char *text)
|
||||||
{
|
{
|
||||||
for (unsigned int charIndex=0; charIndex<strlen(text); charIndex++) {
|
for (unsigned int charIndex=0; charIndex<strlen(text); charIndex++) {
|
||||||
|
|||||||
@ -26,6 +26,7 @@
|
|||||||
const std::string vStringFormat(const char* format, va_list args);
|
const std::string vStringFormat(const char* format, va_list args);
|
||||||
const std::string StringFormat(const char* format, ...);
|
const std::string StringFormat(const char* format, ...);
|
||||||
std::string EscapeString(const std::string &s);
|
std::string EscapeString(const std::string &s);
|
||||||
|
std::string EscapeString(const char *src, size_t sz);
|
||||||
|
|
||||||
const char *MakeLowerString(const char *source);
|
const char *MakeLowerString(const char *source);
|
||||||
|
|
||||||
|
|||||||
@ -7,12 +7,13 @@ SET(tests_sources
|
|||||||
)
|
)
|
||||||
|
|
||||||
SET(tests_headers
|
SET(tests_headers
|
||||||
|
atobool_test.h
|
||||||
fixed_memory_test.h
|
fixed_memory_test.h
|
||||||
fixed_memory_variable_test.h
|
fixed_memory_variable_test.h
|
||||||
|
hextoi_32_64_test.h
|
||||||
ipc_mutex_test.h
|
ipc_mutex_test.h
|
||||||
memory_mapped_file_test.h
|
memory_mapped_file_test.h
|
||||||
atobool_test.h
|
string_util_test.h
|
||||||
hextoi_32_64_test.h
|
|
||||||
)
|
)
|
||||||
|
|
||||||
ADD_EXECUTABLE(tests ${tests_sources} ${tests_headers})
|
ADD_EXECUTABLE(tests ${tests_sources} ${tests_headers})
|
||||||
|
|||||||
@ -26,6 +26,7 @@
|
|||||||
#include "fixed_memory_variable_test.h"
|
#include "fixed_memory_variable_test.h"
|
||||||
#include "atobool_test.h"
|
#include "atobool_test.h"
|
||||||
#include "hextoi_32_64_test.h"
|
#include "hextoi_32_64_test.h"
|
||||||
|
#include "string_util_test.h"
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
try {
|
try {
|
||||||
@ -38,6 +39,7 @@ int main() {
|
|||||||
tests.add(new FixedMemoryVariableHashTest());
|
tests.add(new FixedMemoryVariableHashTest());
|
||||||
tests.add(new atoboolTest());
|
tests.add(new atoboolTest());
|
||||||
tests.add(new hextoi_32_64_Test());
|
tests.add(new hextoi_32_64_Test());
|
||||||
|
tests.add(new StringUtilTest());
|
||||||
tests.run(*output, true);
|
tests.run(*output, true);
|
||||||
} catch(...) {
|
} catch(...) {
|
||||||
return -1;
|
return -1;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user