- 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; 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) static int StringToInt(FString *self, int base)
{ {
return (int)self->ToLong(base); return (int)self->ToLong(base);

View file

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

View file

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

View file

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