- added some UTF-8 utilities to FString.

This deprecated CharAt and CharCodeAt for being unsuitable for text processing and in the case of CharCodeAt also for being buggy.
A new replacement, ByteAt has been added that reads a string byte by byte, as well as CodePointCount, which counts the amount of Unicode code points in a string and GetNextCodePoint which reads the string code point by code point.
Note that while this woll work as intended with the currently supported languages as a means to read single characters, there is no guarantee that this will remain so if Unicode support gets extended to things which break the "one code point == one character" assumption.
This commit is contained in:
Christoph Oelckers 2019-04-13 09:31:36 +02:00
commit bcf7bc8d34
6 changed files with 55 additions and 10 deletions

View file

@ -176,6 +176,19 @@ DEFINE_ACTION_FUNCTION_NATIVE(FStringStruct, CharCodeAt, StringCharCodeAt)
ACTION_RETURN_INT(StringCharCodeAt(self, pos));
}
static int StringByteAt(FString *self, int pos)
{
if ((unsigned)pos >= self->Len()) return 0;
else return (uint8_t)((*self)[pos]);
}
DEFINE_ACTION_FUNCTION_NATIVE(FStringStruct, ByteAt, StringByteAt)
{
PARAM_SELF_STRUCT_PROLOGUE(FString);
PARAM_INT(pos);
ACTION_RETURN_INT(StringByteAt(self, pos));
}
static void StringFilter(FString *self, FString *result)
{
*result = strbin1(*self);
@ -288,6 +301,34 @@ DEFINE_ACTION_FUNCTION_NATIVE(FStringStruct, Split, StringSplit)
return 0;
}
static int StringCodePointCount(FString *self)
{
return (int)self->CharacterCount();
}
DEFINE_ACTION_FUNCTION_NATIVE(FStringStruct, CodePointCount, StringCodePointCount)
{
PARAM_SELF_STRUCT_PROLOGUE(FString);
ACTION_RETURN_INT(StringCodePointCount(self));
}
static int StringNextCodePoint(FString *self, int inposition, int *position)
{
int codepoint = self->GetNextCharacter(inposition);
if (position) *position = inposition;
return codepoint;
}
DEFINE_ACTION_FUNCTION_NATIVE(FStringStruct, GetNextCodePoint, StringNextCodePoint)
{
PARAM_SELF_STRUCT_PROLOGUE(FString);
PARAM_INT(pos);
if (numret > 0) ret[0].SetInt(self->GetNextCharacter(pos));
if (numret > 1) ret[1].SetInt(pos);
return numret;
}
//=====================================================================================
//
// sector_t exports