- added Unicode aware MakeUpper/MakeLower functions to FString and ZScript's String and deprecated ToUpper/ToLower because their semantics did not allow fixing them.

This commit is contained in:
Christoph Oelckers 2019-04-13 10:12:33 +02:00
parent 325b744f4a
commit f7561f25d6
4 changed files with 50 additions and 16 deletions

View file

@ -263,6 +263,28 @@ DEFINE_ACTION_FUNCTION_NATIVE(FStringStruct, ToLower, StringToLower)
return 0;
}
static void StringMakeUpper(FString *self, FString *out)
{
*out = self->MakeUpper();
}
DEFINE_ACTION_FUNCTION_NATIVE(FStringStruct, MakeUpper, StringMakeUpper)
{
PARAM_SELF_STRUCT_PROLOGUE(FString);
ACTION_RETURN_STRING(self->MakeUpper());
}
static void StringMakeLower(FString *self, FString *out)
{
*out = self->MakeLower();
}
DEFINE_ACTION_FUNCTION_NATIVE(FStringStruct, MakeLower, StringMakeLower)
{
PARAM_SELF_STRUCT_PROLOGUE(FString);
ACTION_RETURN_STRING(self->MakeLower());
}
static int StringToInt(FString *self, int base)
{
return (int)self->ToLong(base);

View file

@ -40,6 +40,7 @@
#include "zstring.h"
#include "v_text.h"
#include "utf8.h"
#include "fontinternals.h"
FNullStringData FString::NullString =
{
@ -680,22 +681,30 @@ void FString::ToLower ()
UnlockBuffer();
}
void FString::SwapCase ()
FString FString::MakeLower()
{
LockBuffer();
size_t max = Len();
for (size_t i = 0; i < max; ++i)
TArray<uint8_t> builder(Len());
int pos = 0;
while (int c = GetNextCharacter(pos))
{
if (isupper(Chars[i]))
{
Chars[i] = (char)tolower(Chars[i]);
}
else
{
Chars[i] = (char)toupper(Chars[i]);
}
if (c < 65536) c = lowerforupper[c];
auto cp = MakeUTF8(c);
while (auto uc = *cp++) builder.Push(uc);
}
UnlockBuffer();
return FString(builder);
}
FString FString::MakeUpper()
{
TArray<uint8_t> builder(Len());
int pos = 0;
while (int c = GetNextCharacter(pos))
{
if (c < 65536) c = upperforlower[c];
auto cp = MakeUTF8(c);
while (auto uc = *cp++) builder.Push(uc);
}
return FString(builder);
}
void FString::StripLeft ()

View file

@ -238,7 +238,8 @@ public:
void ToUpper ();
void ToLower ();
void SwapCase ();
FString MakeUpper();
FString MakeLower();
void StripLeft ();
void StripLeft (const FString &charset);

View file

@ -905,8 +905,10 @@ struct StringStruct native
native int IndexOf(String substr, int startIndex = 0) const;
deprecated("3.5.1") native int LastIndexOf(String substr, int endIndex = 2147483647) const;
native int RightIndexOf(String substr, int endIndex = 2147483647) const;
native void ToUpper();
native void ToLower();
deprecated("4.1") native void ToUpper();
deprecated("4.1") native void ToLower();
native String MakeUpper();
native String MakeLower();
native int ToInt(int base = 0) const;
native double ToDouble() const;
native void Split(out Array<String> tokens, String delimiter, EmptyTokenType keepEmpty = TOK_KEEPEMPTY) const;