Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
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{
19 Q_ASSERT(out);
20
21 int32_t size = str.size();
22 size += size >> 2; // add 25% for possible expansions
23 QString result(size, Qt::Uninitialized);
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 QLocalePrivate::toUpper(const QString &str, bool *ok) const
58{
59 Q_ASSERT(ok);
60 QString out;
61 *ok = qt_u_strToCase(str, &out, bcp47Name('_'), u_strToUpper);
62 return out;
63}
64
65QString QLocalePrivate::toLower(const QString &str, bool *ok) const
66{
67 Q_ASSERT(ok);
68 QString out;
69 *ok = qt_u_strToCase(str, &out, bcp47Name('_'), u_strToLower);
70 return out;
71}
72
73QT_END_NAMESPACE
static bool qt_u_strToCase(const QString &str, QString *out, const char *localeID, Ptr_u_strToCase caseFunc)
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)