Qt 6.x
The Qt SDK
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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
53 static inline void trimmed_helper_positions(const Char *&begin, const Char *&end)
54 {
55 // skip white space from end
56 while (begin < end && isSpace(end[-1]))
57 --end;
58 // skip white space from start
59 while (begin < end && isSpace(*begin))
60 begin++;
61 }
62
63 static inline StringType trimmed_helper(StringType &str)
64 {
65 const Char *begin = str.cbegin();
66 const Char *end = str.cend();
68
69 if (begin == str.cbegin() && end == str.cend())
70 return str;
71 if (!isConst && str.isDetached())
73 return StringType(begin, end - begin);
74 }
75
76 static inline StringType simplified_helper(StringType &str)
77 {
78 if (str.isEmpty())
79 return str;
80 const Char *src = str.cbegin();
81 const Char *end = str.cend();
83 StringType(str.size(), Qt::Uninitialized) :
84 std::move(str);
85
86 Char *dst = const_cast<Char *>(result.cbegin());
87 Char *ptr = dst;
88 bool unmodified = true;
89 forever {
90 while (src != end && isSpace(*src))
91 ++src;
92 while (src != end && !isSpace(*src))
93 *ptr++ = *src++;
94 if (src == end)
95 break;
96 if (*src != QChar::Space)
97 unmodified = false;
98 *ptr++ = QChar::Space;
99 }
100 if (ptr != dst && ptr[-1] == QChar::Space)
101 --ptr;
102
103 qsizetype newlen = ptr - dst;
104 if (isConst && newlen == str.size() && unmodified) {
105 // nothing happened, return the original
106 return str;
107 }
108 result.resize(newlen);
109 return result;
110 }
111};
112
114
115#endif // QSTRINGALGORITHMS_P_H
\inmodule QtCore
Definition qchar.h:48
@ Space
Definition qchar.h:56
bool isDetached() const
Definition qstring.h:1105
const_iterator cbegin() const
Definition qstring.h:1201
qsizetype size() const
Returns the number of characters in this string.
Definition qstring.h:182
const_iterator cend() const
Definition qstring.h:1209
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition qstring.h:1083
void resize(qsizetype size)
Sets the size of the string to size characters.
Definition qstring.cpp:2654
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:539
static ControlElement< T > * ptr(QWidget *widget)
GLuint GLuint end
GLenum src
GLenum GLenum dst
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLuint64EXT * result
[6]
QtPrivate::QRegularExpressionMatchIteratorRangeBasedForIterator begin(const QRegularExpressionMatchIterator &iterator)
ptrdiff_t qsizetype
Definition qtypes.h:70
static StringType trimmed_helper(StringType &str)
static void trimmed_helper_positions(const Char *&begin, const Char *&end)
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 *)