From 5a850cd1558792ec76db03e2a2370a394fc0f0e1 Mon Sep 17 00:00:00 2001 From: Ted John Date: Sat, 12 Nov 2016 02:19:54 +0000 Subject: [PATCH] Add some useful string functions --- src/openrct2/core/String.cpp | 88 ++++++++++++++++++++++++++++++++++++ src/openrct2/core/String.hpp | 17 +++++++ 2 files changed, 105 insertions(+) diff --git a/src/openrct2/core/String.cpp b/src/openrct2/core/String.cpp index 5f3fafd435..15e80d2490 100644 --- a/src/openrct2/core/String.cpp +++ b/src/openrct2/core/String.cpp @@ -15,6 +15,7 @@ #pragma endregion #include +#include extern "C" { @@ -116,6 +117,19 @@ namespace String } } + size_t IndexOf(const utf8 * str, utf8 match, size_t startIndex) + { + const utf8 * ch = str + startIndex; + for (; *ch != '\0'; ch++) + { + if (*ch == match) + { + return (size_t)(ch - str); + } + } + return SIZE_MAX; + } + size_t LastIndexOf(const utf8 * str, utf8 match) { const utf8 * lastOccurance = nullptr; @@ -300,6 +314,80 @@ namespace String return DiscardUse(ptr, String::Duplicate(replacement)); } + utf8 * Substring(const utf8 * buffer, size_t index) + { + size_t bufferSize = String::SizeOf(buffer); + bool goodSubstring = index <= bufferSize; + Guard::Assert(goodSubstring, "Substring past end of input string."); + + // If assertion continues, return empty string to avoid crash + if (!goodSubstring) + { + return String::Duplicate(""); + } + + return String::Duplicate(buffer + index); + } + + utf8 * Substring(const utf8 * buffer, size_t index, size_t size) + { + size_t bufferSize = String::SizeOf(buffer); + bool goodSubstring = index + size <= bufferSize; + Guard::Assert(goodSubstring, "Substring past end of input string."); + + // If assertion continues, cap the substring to avoid crash + if (!goodSubstring) + { + if (index >= bufferSize) + { + size = 0; + } + else + { + size = bufferSize - index; + } + } + + utf8 * result = Memory::Allocate(size + 1); + Memory::Copy(result, buffer + index, size); + result[size] = '\0'; + return result; + } + + size_t Split(utf8 * * * values, const utf8 * buffer, utf8 delimiter) + { + std::vector valuesList; + size_t index = 0; + size_t nextIndex; + do + { + nextIndex = String::IndexOf(buffer, '/', index); + utf8 * value; + if (nextIndex == SIZE_MAX) + { + value = String::Substring(buffer, index); + } + else + { + value = String::Substring(buffer, index, nextIndex - index); + } + valuesList.push_back(value); + index = nextIndex + 1; + } while (nextIndex != SIZE_MAX); + + *values = nullptr; + if (valuesList.size() > 0) + { + utf8 * * valuesArray = Memory::AllocateArray(valuesList.size()); + for (size_t i = 0; i < valuesList.size(); i++) + { + valuesArray[i] = valuesList[i]; + } + *values = valuesArray; + } + return valuesList.size(); + } + utf8 * SkipBOM(utf8 * buffer) { return (utf8*)SkipBOM((const utf8 *)buffer); diff --git a/src/openrct2/core/String.hpp b/src/openrct2/core/String.hpp index 427374b2af..1416764dce 100644 --- a/src/openrct2/core/String.hpp +++ b/src/openrct2/core/String.hpp @@ -32,6 +32,7 @@ namespace String bool Equals(const std::string &a, const std::string &b, bool ignoreCase = false); bool Equals(const utf8 * a, const utf8 * b, bool ignoreCase = false); bool StartsWith(const utf8 * str, const utf8 * match, bool ignoreCase = false); + size_t IndexOf(const utf8 * str, utf8 match, size_t startIndex = 0); size_t LastIndexOf(const utf8 * str, utf8 match); /** @@ -64,6 +65,22 @@ namespace String */ utf8 * DiscardDuplicate(utf8 * * ptr, const utf8 * replacement); + /** + * Creates a new string containing the characters between index and and end of the input string. + */ + utf8 * Substring(const utf8 * buffer, size_t index); + + /** + * Creates a new string containing the characters between index and index + size of the input string. + */ + utf8 * Substring(const utf8 * buffer, size_t index, size_t size); + + /** + * Splits the given string by a delimiter and returns the values as a new string array. + * @returns the number of values. + */ + size_t Split(utf8 * * * values, const utf8 * buffer, utf8 delimiter); + utf8 * SkipBOM(utf8 * buffer); const utf8 * SkipBOM(const utf8 * buffer);