diff options
author | Marc Mutz <marc.mutz@kdab.com> | 2020-04-29 11:36:19 +0200 |
---|---|---|
committer | Marc Mutz <marc.mutz@kdab.com> | 2020-05-09 06:26:08 +0000 |
commit | 928d57d8da5e89d2dd7fb3be8c992422538f47d0 (patch) | |
tree | 0915a02f6788300fd2d4d41bf1433ce7836021f8 /src/corelib/text/qstringview.h | |
parent | 19e7c0d2b5a807e194be1d65b16041f48136c9be (diff) |
QChar: add fromUcs{2,4}()
The fromUcs2() named ctor is designed to replace all the non-char
integral-type constructors of QChar which make it very hard to control
the implicit QChar conversions, which have caused a few bugs in Qt
itself. As a classical named contructor, it simply returns QChar.
The fromUcs4() named "ctor", however, needs to expand surrogate pairs,
and thus can't just return QChar. Instead, it returns a small struct
that contains one or two char16_t's, can be iterated over and be
implicitly converted to QStringView. To avoid bikeshedding the name
(FromUcs4Result, of course :), it's defined inline and thus can't be
named outside the function. This function replaces most uses of
QChar::requiresSurrogates() in QtBase.
[ChangeLog][QtCore][QChar] Added fromUcs2(), fromUcs4().
Change-Id: I803708c14001040f75cb599e33c24a3fb8d2579c
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src/corelib/text/qstringview.h')
-rw-r--r-- | src/corelib/text/qstringview.h | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/src/corelib/text/qstringview.h b/src/corelib/text/qstringview.h index 2dd933284b..7e70238cb2 100644 --- a/src/corelib/text/qstringview.h +++ b/src/corelib/text/qstringview.h @@ -351,6 +351,22 @@ template <typename QStringLike, typename std::enable_if< inline QStringView qToStringViewIgnoringNull(const QStringLike &s) noexcept { return QStringView(s.data(), s.size()); } +// QChar inline functions: + +Q_REQUIRED_RESULT constexpr auto QChar::fromUcs4(char32_t c) noexcept +{ + struct R { + char16_t chars[2]; + Q_REQUIRED_RESULT constexpr operator QStringView() const noexcept { return {begin(), end()}; } + Q_REQUIRED_RESULT constexpr qsizetype size() const noexcept { return chars[1] ? 2 : 1; } + Q_REQUIRED_RESULT constexpr const char16_t *begin() const noexcept { return chars; } + Q_REQUIRED_RESULT constexpr const char16_t *end() const noexcept { return begin() + size(); } + }; + return requiresSurrogates(c) ? R{{QChar::highSurrogate(c), + QChar::lowSurrogate(c)}} : + R{{char16_t(c), u'\0'}} ; +} + QT_END_NAMESPACE #endif /* QSTRINGVIEW_H */ |