KTextTemplate

safestring.cpp
1/*
2 This file is part of the KTextTemplate library
3
4 SPDX-FileCopyrightText: 2009, 2010 Stephen Kelly <steveire@gmail.com>
5
6 SPDX-License-Identifier: LGPL-2.1-or-later
7
8*/
9
10#include "safestring.h"
11
12#include <QStringList>
13
14using namespace KTextTemplate;
15
17 : m_nestedString(this)
18 , m_safety(IsNotSafe)
19 , m_needsescape(false)
20{
21}
22
24 : m_nestedString(safeString.get(), this)
25 , m_safety(safeString.m_safety)
26 , m_needsescape(safeString.m_needsescape)
27{
28}
29
30SafeString::SafeString(const QString &str, bool safe)
31 : m_nestedString(str, this)
32 , m_safety(safe ? IsSafe : IsNotSafe)
33 , m_needsescape(false)
34{
35}
36
38 : m_nestedString(str, this)
39 , m_safety(safety)
40 , m_needsescape(false)
41{
42}
43
44SafeString::~SafeString() = default;
45
46void SafeString::setNeedsEscape(bool needsEscape)
47{
48 m_needsescape = needsEscape;
49}
50
52{
53 return m_needsescape;
54}
55
56void SafeString::setSafety(KTextTemplate::SafeString::Safety safety)
57{
58 m_safety = safety;
59}
60
62{
63 return m_safety == IsSafe;
64}
65
66SafeString::NestedString::NestedString(SafeString *safeString)
67 : m_safeString(safeString)
68{
69}
70
71SafeString::NestedString::NestedString(const QString &content, SafeString *safeString)
72 : QString(content)
73 , m_safeString(safeString)
74{
75}
76
78{
79 if (&str == this)
80 return *this;
81 m_nestedString = str.m_nestedString;
82 m_nestedString.m_safeString = this;
83 m_safety = str.m_safety;
84 m_needsescape = str.m_needsescape;
85 return *this;
86}
87
89{
90 return {static_cast<QString>(m_nestedString) + str, IsNotSafe};
91}
92
94{
95 if (!str.isSafe())
96 return {static_cast<QString>(m_nestedString) + static_cast<QString>(str.get()), IsNotSafe};
97 return {static_cast<QString>(m_nestedString) + static_cast<QString>(str.get()), m_safety};
98}
99
101{
102 m_safety = IsNotSafe;
103
104 m_nestedString += str;
105 return *this;
106}
107
109{
110 if (!str.isSafe())
111 m_safety = IsNotSafe;
112
113 m_nestedString += str.get();
114 return *this;
115}
116
118{
119 // Don't compare safety or account for future escaping here.
120 // See TestBuiltins testEscaping
121 return m_nestedString == other.get();
122}
123
124bool SafeString::operator==(const QString &other) const
125{
126 return m_nestedString == other;
127}
128
130{
131 QString::append(str.get());
132 if (!str.isSafe())
133 m_safeString->m_safety = IsNotSafe;
134
135 return *m_safeString;
136}
137
139{
140 QString::append(str);
141 m_safeString->m_safety = IsNotSafe;
142 return *m_safeString;
143}
144
146{
147 QString::append(str);
148 m_safeString->m_safety = IsNotSafe;
149 return *m_safeString;
150}
151
153{
154 QString::append(ch);
155 m_safeString->m_safety = IsNotSafe;
156 return *m_safeString;
157}
158
160{
161 QString::chop(n);
162 m_safeString->m_safety = IsNotSafe;
163}
164
166{
167 QString::fill(ch, size);
168 m_safeString->m_safety = IsNotSafe;
169 return *m_safeString;
170}
171
173{
174 QString::insert(position, str.get());
175 if (!str.isSafe())
176 m_safeString->m_safety = IsNotSafe;
177
178 return *m_safeString;
179}
180
181SafeString &SafeString::NestedString::insert(int position, const QString &str)
182{
183 QString::insert(position, str);
184 return *m_safeString;
185}
186
188{
189 QString::insert(position, str);
190 return *m_safeString;
191}
192
193SafeString &SafeString::NestedString::insert(int position, const QChar *unicode, int size)
194{
195 QString::insert(position, unicode, size);
196 return *m_safeString;
197}
198
200{
201 QString::insert(position, ch);
202 return *m_safeString;
203}
204
206{
207 return {QString::left(n), m_safeString->m_safety};
208}
209
210SafeString SafeString::NestedString::leftJustified(int width, QChar fill, bool truncate) const
211{
212 return {QString::leftJustified(width, fill, truncate), m_safeString->m_safety};
213}
214
215SafeString SafeString::NestedString::mid(int position, int n) const
216{
217 return {QString::mid(position, n), m_safeString->m_safety};
218}
219
221{
222 return {QString::normalized(mode), m_safeString->m_safety};
223}
224
226{
227 return {QString::normalized(mode, version), m_safeString->m_safety};
228}
229
231{
232 QString::prepend(str.get());
233 if (!str.isSafe())
234 m_safeString->m_safety = IsNotSafe;
235
236 return *m_safeString;
237}
238
240{
241 QString::prepend(str);
242 m_safeString->m_safety = IsNotSafe;
243 return *m_safeString;
244}
245
247{
248 QString::prepend(str);
249 m_safeString->m_safety = IsNotSafe;
250 return *m_safeString;
251}
252
254{
256 m_safeString->m_safety = IsNotSafe;
257 return *m_safeString;
258}
259
261{
262 QString::push_back(other.get());
263 m_safeString->m_safety = other.m_safety;
264}
265
267{
268 QString::push_front(other.get());
269 m_safeString->m_safety = other.m_safety;
270}
271
273{
274 QString::remove(position, n);
275 m_safeString->m_safety = IsNotSafe;
276 return *m_safeString;
277}
278
280{
281 QString::remove(ch, cs);
282 m_safeString->m_safety = IsNotSafe;
283 return *m_safeString;
284}
285
287{
288 QString::remove(str, cs);
289 m_safeString->m_safety = IsNotSafe;
290 return *m_safeString;
291}
292
294{
295 QString::remove(str, cs);
296 m_safeString->m_safety = IsNotSafe;
297 return *m_safeString;
298}
299
301{
302 QString::remove(rx);
303 m_safeString->m_safety = IsNotSafe;
304 return *m_safeString;
305}
306
308{
309 return {QString::repeated(times), m_safeString->m_safety};
310}
311
313{
314 QString::replace(position, n, after.get());
315 m_safeString->m_safety = after.m_safety;
316 return *m_safeString;
317}
318
319SafeString &SafeString::NestedString::replace(int position, int n, const QString &after)
320{
321 QString::replace(position, n, after);
322 m_safeString->m_safety = IsNotSafe;
323 return *m_safeString;
324}
325
326SafeString &SafeString::NestedString::replace(int position, int n, const QChar *unicode, int size)
327{
328 QString::replace(position, n, unicode, size);
329 m_safeString->m_safety = IsNotSafe;
330 return *m_safeString;
331}
332
333SafeString &SafeString::NestedString::replace(int position, int n, QChar after)
334{
335 QString::replace(position, n, after);
336 m_safeString->m_safety = IsNotSafe;
337 return *m_safeString;
338}
339
341{
342 QString::replace(before.get(), after.get(), cs);
343 m_safeString->m_safety = after.m_safety;
344 return *m_safeString;
345}
346
348{
349 QString::replace(before, after.get(), cs);
350 m_safeString->m_safety = IsNotSafe;
351 return *m_safeString;
352}
353
355{
356 QString::replace(before.get(), after, cs);
357 m_safeString->m_safety = IsNotSafe;
358 return *m_safeString;
359}
360
362{
363 QString::replace(before, after, cs);
364 m_safeString->m_safety = IsNotSafe;
365 return *m_safeString;
366}
367
368SafeString &SafeString::NestedString::replace(const QChar *before, int blen, const QChar *after, int alen, Qt::CaseSensitivity cs)
369{
370 QString::replace(before, blen, after, alen, cs);
371 m_safeString->m_safety = IsNotSafe;
372 return *m_safeString;
373}
374
376{
377 QString::replace(ch, after.get(), cs);
378 m_safeString->m_safety = after.m_safety;
379 return *m_safeString;
380}
381
383{
384 QString::replace(ch, after, cs);
385 m_safeString->m_safety = IsNotSafe;
386 return *m_safeString;
387}
388
390{
391 QString::replace(before, after, cs);
392 m_safeString->m_safety = IsNotSafe;
393 return *m_safeString;
394}
395
397{
398 QString::replace(before, after, cs);
399 m_safeString->m_safety = IsNotSafe;
400 return *m_safeString;
401}
402
404{
405 QString::replace(before, after.get(), cs);
406 m_safeString->m_safety = after.m_safety;
407 return *m_safeString;
408}
409
411{
412 QString::replace(before, after, cs);
413 m_safeString->m_safety = IsNotSafe;
414 return *m_safeString;
415}
416
418{
419 QString::replace(before.get(), after, cs);
420 m_safeString->m_safety = before.m_safety;
421 return *m_safeString;
422}
423
425{
426 QString::replace(before, after, cs);
427 m_safeString->m_safety = IsNotSafe;
428 return *m_safeString;
429}
430
432{
433 QString::replace(c, after, cs);
434 m_safeString->m_safety = IsNotSafe;
435 return *m_safeString;
436}
437
439{
440 QString::replace(rx, after.get());
441 m_safeString->m_safety = after.m_safety;
442 return *m_safeString;
443}
444
446{
447 QString::replace(rx, after);
448 m_safeString->m_safety = IsNotSafe;
449 return *m_safeString;
450}
451
453{
454 return {QString::right(n), m_safeString->m_safety};
455}
456
457SafeString SafeString::NestedString::rightJustified(int width, QChar fill, bool truncate) const
458{
459 return {QString::rightJustified(width, fill, truncate), m_safeString->m_safety};
460}
461
463{
464 return {QString::section(reg, start, end, flags), m_safeString->m_safety};
465}
466
468{
469 return {QString::section(sep, start, end, flags), m_safeString->m_safety};
470}
471
473{
474 return {QString::section(sep, start, end, flags), m_safeString->m_safety};
475}
476
478{
479 return {QString::section(sep, start, end, flags), m_safeString->m_safety};
480}
481
483{
484 QString::setNum(n, base);
485 m_safeString->m_safety = IsNotSafe;
486 return *m_safeString;
487}
488
490{
491 QString::setNum(n, base);
492 m_safeString->m_safety = IsNotSafe;
493 return *m_safeString;
494}
495
496SafeString &SafeString::NestedString::setNum(long int n, int base)
497{
498 QString::setNum(n, base);
499 m_safeString->m_safety = IsNotSafe;
500 return *m_safeString;
501}
502
504{
505 QString::setNum(n, base);
506 m_safeString->m_safety = IsNotSafe;
507 return *m_safeString;
508}
509
510SafeString &SafeString::NestedString::setNum(qlonglong n, int base)
511{
512 QString::setNum(n, base);
513 m_safeString->m_safety = IsNotSafe;
514 return *m_safeString;
515}
516
517SafeString &SafeString::NestedString::setNum(qulonglong n, int base)
518{
519 QString::setNum(n, base);
520 m_safeString->m_safety = IsNotSafe;
521 return *m_safeString;
522}
523
524SafeString &SafeString::NestedString::setNum(short int n, int base)
525{
526 QString::setNum(n, base);
527 m_safeString->m_safety = IsNotSafe;
528 return *m_safeString;
529}
530
532{
533 QString::setNum(n, base);
534 m_safeString->m_safety = IsNotSafe;
535 return *m_safeString;
536}
537
538SafeString &SafeString::NestedString::setNum(double n, char format, int precision)
539{
540 QString::setNum(n, format, precision);
541 m_safeString->m_safety = IsNotSafe;
542 return *m_safeString;
543}
544
545SafeString &SafeString::NestedString::setNum(float n, char format, int precision)
546{
547 QString::setNum(n, format, precision);
548 m_safeString->m_safety = IsNotSafe;
549 return *m_safeString;
550}
551
553{
554 QString::setUnicode(unicode, size);
555 m_safeString->m_safety = IsNotSafe;
556 return *m_safeString;
557}
558
559SafeString &SafeString::NestedString::setUtf16(const ushort *unicode, int size)
560{
561 QString::setUtf16(unicode, size);
562 m_safeString->m_safety = IsNotSafe;
563 return *m_safeString;
564}
565
567{
568 return {QString::simplified(), m_safeString->m_safety};
569}
570
572{
573 return QString::split(sep.get(), behavior, cs);
574}
575
577{
578 return QString::split(sep, behavior, cs);
579}
580
582{
583 return QString::split(sep, behavior, cs);
584}
585
587{
588 return QString::split(rx, behavior);
589}
590
592{
593 return {QString::toLower(), IsNotSafe};
594}
595
597{
598 return {QString::toUpper(), IsNotSafe};
599}
600
602{
603 return {QString::trimmed(), m_safeString->m_safety};
604}
A QString wrapper class for containing whether a string is safe or needs to be escaped.
Definition safestring.h:81
SafeString & operator+=(const QString &str)
Appends the content of str to this.
bool needsEscape() const
Whether the string needs to be escaped.
bool isSafe() const
Whether the string is safe.
Safety
Possible safety states of a SafeString
Definition safestring.h:86
@ IsNotSafe
The string is not safe.
Definition safestring.h:88
@ IsSafe
The string is safe and requires no further escaping.
Definition safestring.h:87
SafeString operator+(const QString &str)
Returns a concatenation of this with str.
SafeString()
Constructs an empty SafeString.
SafeString & operator=(const SafeString &str)
Assignment operator.
const NestedString & get() const
Returns the String held by this SafeString
Definition safestring.h:283
bool operator==(const SafeString &other) const
Returns true if the content of other matches the content of this.
Q_SCRIPTABLE Q_NOREPLY void start()
The KTextTemplate namespace holds all public KTextTemplate API.
Definition Mainpage.dox:8
UnicodeVersion
QString & append(QChar ch)
void chop(qsizetype n)
QString & fill(QChar ch, qsizetype size)
QString & insert(qsizetype position, QChar ch)
QString left(qsizetype n) const const
QString leftJustified(qsizetype width, QChar fill, bool truncate) const const
QString mid(qsizetype position, qsizetype n) const const
QString normalized(NormalizationForm mode, QChar::UnicodeVersion version) const const
QString & prepend(QChar ch)
void push_back(QChar ch)
void push_front(QChar ch)
QString & remove(QChar ch, Qt::CaseSensitivity cs)
QString repeated(qsizetype times) const const
QString & replace(QChar before, QChar after, Qt::CaseSensitivity cs)
QString right(qsizetype n) const const
QString rightJustified(qsizetype width, QChar fill, bool truncate) const const
QString section(QChar sep, qsizetype start, qsizetype end, SectionFlags flags) const const
QString & setNum(double n, char format, int precision)
QString & setUnicode(const QChar *unicode, qsizetype size)
QString & setUtf16(const ushort *unicode, qsizetype size)
QString simplified() const const
QStringList split(QChar sep, Qt::SplitBehavior behavior, Qt::CaseSensitivity cs) const const
QString toLower() const const
QString toUpper() const const
QString trimmed() const const
CaseSensitivity
typedef SplitBehavior
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 22 2024 12:12:30 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.