From 19e7c0d2b5a807e194be1d65b16041f48136c9be Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Fri, 24 Apr 2020 12:54:20 +0200 Subject: QChar/QString: centralize case folding in qchar.cpp There are (at least) two implementations of the low-level case-folding algorithm, one of which (for QChar::toLower()) seems to be wrong (it doesn't deal with special cases which expand to more than one code point). The algoithm hidden in QString and entangled with the QString detaching code makes reusing the code much harder. At the same time, the dependency of the algorithm on the unicode tables makes exposing a non-allocating result type in the public API hard. std::u16string would be an alternative if we can assure that all implementations use SSO with at least four characters. So, for the time being, leave this as internal API for use in an upcoming QStringView::toLower() as well as case-insensitive hashing. Change-Id: Iabb2611846f6176776aa20e634f44d8464f3305c Reviewed-by: Thiago Macieira --- src/corelib/text/qchar.cpp | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'src/corelib/text/qchar.cpp') diff --git a/src/corelib/text/qchar.cpp b/src/corelib/text/qchar.cpp index 6b1bb33949..1b8cfb642e 100644 --- a/src/corelib/text/qchar.cpp +++ b/src/corelib/text/qchar.cpp @@ -1550,6 +1550,29 @@ QChar::UnicodeVersion QChar::currentUnicodeVersion() noexcept return UNICODE_DATA_VERSION; } +using FullConvertCaseResult = std::array; +static FullConvertCaseResult fullConvertCase(char32_t uc, QUnicodeTables::Case which) noexcept +{ + FullConvertCaseResult result = {}; + auto pp = result.begin(); + + const auto fold = qGetProp(uc)->cases[which]; + const auto caseDiff = fold.diff; + + if (Q_UNLIKELY(fold.special)) { + const auto *specialCase = specialCaseMap + caseDiff; + auto length = *specialCase++; + while (length--) + *pp++ = *specialCase++; + } else if (Q_UNLIKELY(QChar::requiresSurrogates(uc))) { + // so far, case convertion never changes planes (guaranteed by the qunicodetables generator) + *pp++ = QChar::highSurrogate(uc); + *pp++ = QChar::lowSurrogate(uc + caseDiff); + } else { + *pp++ = uc + caseDiff; + } + return result; +} template Q_DECL_CONST_FUNCTION static inline T convertCase_helper(T uc, QUnicodeTables::Case which) noexcept -- cgit v1.2.3