Skip to content

Commit 27bab4b

Browse files
committed
Try again
1 parent 3dad9cb commit 27bab4b

2 files changed

Lines changed: 33 additions & 2 deletions

File tree

modules/yup_core/text/yup_String.cpp

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2042,10 +2042,41 @@ String String::reversed() const
20422042
while (! p.isEmpty())
20432043
clusters.push_back (p.getAndAdvance());
20442044

2045+
auto appendUTF32CodepointAsUTF8 = [](String& dest, yup_wchar cp)
2046+
{
2047+
char utf8[5] = { 0 };
2048+
size_t len = 0;
2049+
2050+
if (cp <= 0x7F)
2051+
{
2052+
utf8[len++] = static_cast<char> (cp);
2053+
}
2054+
else if (cp <= 0x7FF)
2055+
{
2056+
utf8[len++] = static_cast<char> (0xC0 | (cp >> 6));
2057+
utf8[len++] = static_cast<char> (0x80 | (cp & 0x3F));
2058+
}
2059+
else if (cp <= 0xFFFF)
2060+
{
2061+
utf8[len++] = static_cast<char> (0xE0 | (cp >> 12));
2062+
utf8[len++] = static_cast<char> (0x80 | ((cp >> 6) & 0x3F));
2063+
utf8[len++] = static_cast<char> (0x80 | (cp & 0x3F));
2064+
}
2065+
else
2066+
{
2067+
utf8[len++] = static_cast<char> (0xF0 | (cp >> 18));
2068+
utf8[len++] = static_cast<char> (0x80 | ((cp >> 12) & 0x3F));
2069+
utf8[len++] = static_cast<char> (0x80 | ((cp >> 6) & 0x3F));
2070+
utf8[len++] = static_cast<char> (0x80 | (cp & 0x3F));
2071+
}
2072+
2073+
dest.append (CharPointer_UTF8 (utf8), len);
2074+
};
2075+
20452076
String result;
20462077
result.preallocateBytes (numChars);
20472078
for (auto it = clusters.rbegin(); it != clusters.rend(); ++it)
2048-
result += *it;
2079+
appendUTF32CodepointAsUTF8 (result, *it);
20492080

20502081
return result;
20512082
}

tests/yup_core/yup_String.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -764,7 +764,7 @@ TEST_F (StringTests, StringReversing)
764764
EXPECT_EQ (String().reversed(), String());
765765
EXPECT_EQ (String ("12345").reversed(), String ("54321"));
766766

767-
#if ! YUP_WINDOWS
767+
#if 1 // ! YUP_WINDOWS
768768
// Test with Unicode characters - this is the critical test for UTF-8 handling
769769
String unicode_str (L"café");
770770
String reversed_unicode = unicode_str.reversed();

0 commit comments

Comments
 (0)