mirror of
https://github.com/OpenRCT2/OpenRCT2
synced 2026-01-16 19:43:06 +01:00
new-argparse: implement most of the root commands, fix issues
This commit is contained in:
106
src/core/String.cpp
Normal file
106
src/core/String.cpp
Normal file
@@ -0,0 +1,106 @@
|
||||
extern "C"
|
||||
{
|
||||
#include "../localisation/localisation.h"
|
||||
#include "../util/util.h"
|
||||
}
|
||||
|
||||
#include "Math.hpp"
|
||||
#include "Memory.hpp"
|
||||
#include "String.hpp"
|
||||
|
||||
namespace String
|
||||
{
|
||||
bool Equals(const utf8 * a, const utf8 * b, bool ignoreCase)
|
||||
{
|
||||
if (a == b) return true;
|
||||
if (a == nullptr || b == nullptr) return false;
|
||||
|
||||
if (ignoreCase)
|
||||
{
|
||||
return _strcmpi(a, b) == 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
return strcmp(a, b) == 0;
|
||||
}
|
||||
}
|
||||
|
||||
size_t LengthOf(const utf8 * str)
|
||||
{
|
||||
return utf8_length(str);
|
||||
}
|
||||
|
||||
size_t SizeOf(const utf8 * str)
|
||||
{
|
||||
return strlen(str);
|
||||
}
|
||||
|
||||
utf8 * Set(utf8 * buffer, size_t bufferSize, const utf8 * src)
|
||||
{
|
||||
return safe_strncpy(buffer, src, bufferSize);
|
||||
}
|
||||
|
||||
utf8 * Set(utf8 * buffer, size_t bufferSize, const utf8 * src, size_t srcSize)
|
||||
{
|
||||
utf8 * dst = buffer;
|
||||
size_t minSize = Math::Min(bufferSize - 1, srcSize);
|
||||
for (size_t i = 0; i < minSize; i++)
|
||||
{
|
||||
*dst++ = *src;
|
||||
if (*src == '\0') break;
|
||||
*src++;
|
||||
}
|
||||
*dst = '\0';
|
||||
return buffer;
|
||||
}
|
||||
|
||||
utf8 * Append(utf8 * buffer, size_t bufferSize, const utf8 * src)
|
||||
{
|
||||
return safe_strcat(buffer, src, bufferSize);
|
||||
}
|
||||
|
||||
utf8 * Format(utf8 * buffer, size_t bufferSize, const utf8 * format, ...)
|
||||
{
|
||||
va_list args;
|
||||
|
||||
va_start(args, format);
|
||||
vsnprintf(buffer, bufferSize, format, args);
|
||||
va_end(args);
|
||||
|
||||
// Terminate buffer in case formatted string overflowed
|
||||
buffer[bufferSize - 1] = '\0';
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
utf8 * AppendFormat(utf8 * buffer, size_t bufferSize, const utf8 * format, ...)
|
||||
{
|
||||
va_list args;
|
||||
|
||||
utf8 * dst = buffer;
|
||||
size_t i;
|
||||
for (i = 0; i < bufferSize; i++)
|
||||
{
|
||||
if (*dst == '\0') break;
|
||||
dst++;
|
||||
}
|
||||
|
||||
if (i < bufferSize - 1)
|
||||
{
|
||||
va_start(args, format);
|
||||
vsnprintf(buffer, bufferSize - i - 1, format, args);
|
||||
va_end(args);
|
||||
|
||||
// Terminate buffer in case formatted string overflowed
|
||||
buffer[bufferSize - 1] = '\0';
|
||||
}
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
utf8 * Duplicate(const utf8 * src)
|
||||
{
|
||||
size_t srcSize = SizeOf(src);
|
||||
return Memory::DuplicateArray(src, srcSize + 1);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user