Skip to content

Commit 5dba5a8

Browse files
committed
Improve string reversal
1 parent 7570bba commit 5dba5a8

File tree

2 files changed

+5
-34
lines changed

2 files changed

+5
-34
lines changed

modules/yup_core/text/yup_String.cpp

Lines changed: 4 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2035,41 +2035,12 @@ String String::reversed() const
20352035
if (numChars <= 0)
20362036
return *this;
20372037

2038-
auto isCombiningMark = [] (yup_wchar c)
2039-
{
2040-
// Combining Diacritical Marks
2041-
if (c >= 0x0300 && c <= 0x036F)
2042-
return true;
2043-
// Combining Diacritical Marks Extended
2044-
if (c >= 0x1AB0 && c <= 0x1AFF)
2045-
return true;
2046-
// Combining Diacritical Marks Supplement
2047-
if (c >= 0x1DC0 && c <= 0x1DFF)
2048-
return true;
2049-
// Combining Half Marks
2050-
if (c >= 0xFE20 && c <= 0xFE2F)
2051-
return true;
2052-
// Combining Diacritical Marks for Symbols
2053-
if (c >= 0x20D0 && c <= 0x20FF)
2054-
return true;
2055-
return false;
2056-
};
2057-
2058-
std::vector<String> clusters;
2038+
std::vector<yup_wchar> clusters;
20592039
clusters.reserve (numChars);
20602040

2061-
for (int i = 0; i < numChars; ++i)
2062-
{
2063-
String cluster = substring (i, i + 1);
2064-
2065-
while (i + 1 < numChars && isCombiningMark ((*this)[i + 1]))
2066-
{
2067-
++i;
2068-
cluster += substring (i, i + 1);
2069-
}
2070-
2071-
clusters.emplace_back (std::move (cluster));
2072-
}
2041+
CharPointerType p{ text };
2042+
while (! p.isEmpty())
2043+
clusters.push_back (p.getAndAdvance());
20732044

20742045
String result;
20752046
result.preallocateBytes (numChars);

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)