trim string entries

This commit is contained in:
Ted John committed 2016-07-08 22:58:38 +01:00
1 parent 6c7bc9b158
commit 995bbffb84
3 files changed
+52 -2

No files matched your search

+43
View File
@@ -14,6 +14,8 @@
*****************************************************************************/
#pragma endregion
#include <cwctype>
extern "C"
{
#include "../localisation/localisation.h"
@@ -218,4 +220,45 @@ namespace String
{
return utf8_write_codepoint(dst, codepoint);
}
utf8 * Trim(utf8 * str)
{
utf8 * firstNonWhitespace = nullptr;
utf8 * lastNonWhitespace = nullptr;
codepoint_t codepoint;
utf8 * ch = str;
utf8 * nextCh;
while ((codepoint = GetNextCodepoint(ch, &nextCh)) != '\0')
{
if (codepoint <= WCHAR_MAX && !iswspace((wchar_t)codepoint))
{
if (firstNonWhitespace == nullptr)
{
firstNonWhitespace = ch;
}
lastNonWhitespace = ch;
}
ch = nextCh;
}
if (firstNonWhitespace != nullptr &&
firstNonWhitespace != str)
{
size_t newStringSize = ch - firstNonWhitespace;
#if DEBUG
size_t currentStringSize = String::SizeOf(str);
assert(newStringSize < currentStringSize);
#endif
Memory::Copy(str, firstNonWhitespace, newStringSize);
str[newStringSize] = '\0';
}
else
{
*ch = '\0';
}
return str;
}
}