Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qlocale_icu.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qglobal.h"
5#include "qdebug.h"
6#include "qlocale_p.h"
7#include "qmutex.h"
8
9#include "unicode/uloc.h"
10#include "unicode/ustring.h"
11
13
14typedef int32_t (*Ptr_u_strToCase)(UChar *dest, int32_t destCapacity, const UChar *src, int32_t srcLength, const char *locale, UErrorCode *pErrorCode);
15
16// caseFunc can either be u_strToUpper or u_strToLower
17static bool qt_u_strToCase(const QString &str, QString *out, const char *localeID, Ptr_u_strToCase caseFunc)
18{
20
21 int32_t size = str.size();
22 size += size >> 2; // add 25% for possible expansions
24
25 UErrorCode status = U_ZERO_ERROR;
26
27 size = caseFunc(reinterpret_cast<UChar *>(result.data()), result.size(),
28 reinterpret_cast<const UChar *>(str.constData()), str.size(),
29 localeID, &status);
30
31 if (U_FAILURE(status) && status != U_BUFFER_OVERFLOW_ERROR)
32 return false;
33
34 if (size < result.size()) {
35 result.resize(size);
36 } else if (size > result.size()) {
37 // the resulting string is larger than our source string
38 result.resize(size);
39
40 status = U_ZERO_ERROR;
41 size = caseFunc(reinterpret_cast<UChar *>(result.data()), result.size(),
42 reinterpret_cast<const UChar *>(str.constData()), str.size(),
43 localeID, &status);
44
45 if (U_FAILURE(status))
46 return false;
47
48 // if the sizes don't match now, we give up.
49 if (size != result.size())
50 return false;
51 }
52
53 *out = result;
54 return true;
55}
56
57QString QIcu::toUpper(const QByteArray &localeID, const QString &str, bool *ok)
58{
60 bool err = qt_u_strToCase(str, &out, localeID, u_strToUpper);
61 if (ok)
62 *ok = err;
63 return out;
64}
65
66QString QIcu::toLower(const QByteArray &localeID, const QString &str, bool *ok)
67{
69 bool err = qt_u_strToCase(str, &out, localeID, u_strToLower);
70 if (ok)
71 *ok = err;
72 return out;
73}
74
\inmodule QtCore
Definition qbytearray.h:57
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
const QChar * constData() const
Returns a pointer to the data stored in the QString.
Definition qstring.h:1246
qsizetype size() const noexcept
Returns the number of characters in this string.
Definition qstring.h:186
QString str
[2]
Combined button and popup list for selecting options.
constexpr Initialization Uninitialized
QT_BEGIN_NAMESPACE typedef int32_t(* Ptr_u_strToCase)(UChar *dest, int32_t destCapacity, const UChar *src, int32_t srcLength, const char *locale, UErrorCode *pErrorCode)
static bool qt_u_strToCase(const QString &str, QString *out, const char *localeID, Ptr_u_strToCase caseFunc)
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLenum src
GLuint64EXT * result
[6]
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
QTextStream out(stdout)
[7]