1
0
mirror of https://github.com/OpenRCT2/OpenRCT2 synced 2026-01-15 11:03:00 +01:00

trim string entries

This commit is contained in:
Ted John
2016-06-29 18:47:15 +01:00
parent 6c7bc9b158
commit 995bbffb84
3 changed files with 52 additions and 2 deletions

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;
}
}

View File

@@ -64,4 +64,6 @@ namespace String
codepoint_t GetNextCodepoint(utf8 * ptr, utf8 * * nextPtr = nullptr);
codepoint_t GetNextCodepoint(const utf8 * ptr, const utf8 * * nextPtr = nullptr);
utf8 * WriteCodepoint(utf8 * dst, codepoint_t codepoint);
utf8 * Trim(utf8 * str);
}

View File

@@ -52,13 +52,18 @@ void StringTable::Read(IStream * stream, uint8 id)
StringTableEntry entry;
entry.Id = id;
entry.LanguageId = languageId;
entry.Text = stream->ReadString();
if (StringIsBlank(entry.Text))
char * win1252 = stream->ReadString();
if (StringIsBlank(win1252))
{
entry.LanguageId = RCT2_LANGUAGE_ID_BLANK;
}
entry.Text = win1252_to_utf8_alloc(win1252);
Memory::Free(win1252);
String::Trim(entry.Text);
_strings.push_back(entry);
}
Sort();