Add some useful string functions

This commit is contained in:
Ted John 2016-11-12 02:19:54 +00:00
parent 2e37d6e427
commit 5a850cd155
2 changed files with 105 additions and 0 deletions

View File

@ -15,6 +15,7 @@
#pragma endregion
#include <cwctype>
#include <vector>
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<utf8>(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<utf8 *> 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<utf8 *>(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);

View File

@ -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);