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
qstringalgorithms_p.h
Go to the documentation of this file.
1// Copyright (C) 2016 Intel Corporation.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef QSTRINGALGORITHMS_P_H
5#define QSTRINGALGORITHMS_P_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists for the convenience
12// of internal files. This header file may change from version to version
13// without notice, or even be removed.
14//
15// We mean it.
16//
17
18#include "qstring.h"
19#include "qlocale_p.h" // for ascii_isspace
20
22
23template <typename StringType> struct QStringAlgorithms
24{
25 typedef typename StringType::value_type Char;
26 typedef typename StringType::size_type size_type;
27 typedef typename std::remove_cv<StringType>::type NakedStringType;
28 static const bool isConst = std::is_const<StringType>::value;
29
30 static inline bool isSpace(char ch) { return ascii_isspace(ch); }
31 static inline bool isSpace(QChar ch) { return ch.isSpace(); }
32
33 // Surrogate pairs are not handled in either of the functions below. That is
34 // not a problem because there are no space characters (Zs, Zl, Zp) outside the
35 // Basic Multilingual Plane.
36
37 static inline StringType trimmed_helper_inplace(NakedStringType &str, const Char *begin, const Char *end)
38 {
39 // in-place trimming:
40 Char *data = const_cast<Char *>(str.cbegin());
41 if (begin != data)
42 memmove(data, begin, (end - begin) * sizeof(Char));
44 return std::move(str);
45 }
46
47 static inline StringType trimmed_helper_inplace(const NakedStringType &, const Char *, const Char *)
48 {
49 // can't happen
50 Q_UNREACHABLE_RETURN(StringType());
51 }
52
54 const Char *begin;
55 const Char *end;
56 };
57 // Returns {begin, end} where:
58 // - "begin" refers to the first non-space character
59 // - if there is a sequence of one or more space chacaters at the end,
60 // "end" refers to the first character in that sequence, otherwise
61 // "end" is str.cend()
62 [[nodiscard]] static TrimPositions trimmed_helper_positions(const StringType &str)
63 {
64 const Char *begin = str.cbegin();
65 const Char *end = str.cend();
66 // skip white space from end
67 while (begin < end && isSpace(end[-1]))
68 --end;
69 // skip white space from start
70 while (begin < end && isSpace(*begin))
71 begin++;
72 return {begin, end};
73 }
74
75 static inline StringType trimmed_helper(StringType &str)
76 {
77 const auto [begin, end] = trimmed_helper_positions(str);
78 if (begin == str.cbegin() && end == str.cend())
79 return str;
80 if (!isConst && str.isDetached())
82 return StringType(begin, end - begin);
83 }
84
85 static inline StringType simplified_helper(StringType &str)
86 {
87 if (str.isEmpty())
88 return str;
89 const Char *src = str.cbegin();
90 const Char *end = str.cend();
92 StringType(str.size(), Qt::Uninitialized) :
93 std::move(str);
94
95 Char *dst = const_cast<Char *>(result.cbegin());
96 Char *ptr = dst;
97 bool unmodified = true;
98 forever {
99 while (src != end && isSpace(*src))
100 ++src;
101 while (src != end && !isSpace(*src))
102 *ptr++ = *src++;
103 if (src == end)
104 break;
105 if (*src != QChar::Space)
106 unmodified = false;
107 *ptr++ = QChar::Space;
108 }
109 if (ptr != dst && ptr[-1] == QChar::Space)
110 --ptr;
111
112 qsizetype newlen = ptr - dst;
113 if (isConst && newlen == str.size() && unmodified) {
114 // nothing happened, return the original
115 return str;
116 }
117 result.resize(newlen);
118 return result;
119 }
120};
121
123
124#endif // QSTRINGALGORITHMS_P_H
\inmodule QtCore
bool isDetached() const
Definition qstring.h:1250
bool isEmpty() const noexcept
Returns true if the string has no characters; otherwise returns false.
Definition qstring.h:192
const_iterator cbegin() const
Definition qstring.h:1353
qsizetype size() const noexcept
Returns the number of characters in this string.
Definition qstring.h:186
const_iterator cend() const
Definition qstring.h:1361
void resize(qsizetype size)
Sets the size of the string to size characters.
Definition qstring.cpp:2668
QString str
[2]
Combined button and popup list for selecting options.
constexpr Initialization Uninitialized
#define forever
Definition qforeach.h:78
constexpr bool ascii_isspace(uchar c)
Definition qlocale_p.h:556
static ControlElement< T > * ptr(QWidget *widget)
GLuint GLuint end
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLenum src
GLenum GLenum dst
GLuint64EXT * result
[6]
QtPrivate::QRegularExpressionMatchIteratorRangeBasedForIterator begin(const QRegularExpressionMatchIterator &iterator)
ptrdiff_t qsizetype
Definition qtypes.h:165
static TrimPositions trimmed_helper_positions(const StringType &str)
static StringType trimmed_helper(StringType &str)
static StringType trimmed_helper_inplace(NakedStringType &str, const Char *begin, const Char *end)
std::remove_cv< StringType >::type NakedStringType
StringType::size_type size_type
static bool isSpace(QChar ch)
static StringType simplified_helper(StringType &str)
static bool isSpace(char ch)
StringType::value_type Char
static const bool isConst
static StringType trimmed_helper_inplace(const NakedStringType &, const Char *, const Char *)