summaryrefslogtreecommitdiffstats
path: root/src/foundation/StringTools.h
blob: 43324b46e7b202753ef61c8e322f2471885ea9b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
/****************************************************************************
**
** Copyright (C) 2008-2012 NVIDIA Corporation.
** Copyright (C) 2017 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt 3D Studio.
**
** $QT_BEGIN_LICENSE:GPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 or (at your option) any later version
** approved by the KDE Free Qt Foundation. The licenses are as published by
** the Free Software Foundation and appearing in the file LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#pragma once
#ifndef QT3DS_RENDER_STRING_H
#define QT3DS_RENDER_STRING_H
#include <QtCore/qstring.h>
#include <QtCore/qbytearray.h>
#include <QtCore/qhash.h>
#include <string>

#include "Qt3DSRender.h"
#include "EASTL/string.h"

namespace qt3ds {
namespace foundation {

class Qt3DSStringUtils {
public:
    static inline QString ConvertUTFtoQString(const char16_t *string)
    {
        return QString::fromUtf16(string);
    }

    static inline QString ConvertUTFtoQString(const char32_t *string)
    {
        return QString::fromUcs4(string);
    }

    static inline QString ConvertUTFtoQString(const wchar_t *string)
    {
        return QString::fromWCharArray(string);
    }

    static inline QString &append(QString &target, QString::iterator begin,
                                  QString::iterator end)
    {
        for (QString::iterator iter = begin;iter != end;++iter)
            target.append(*iter);
        return target;
    }

    static inline QString &append(QString &target, QString::const_iterator begin,
                                  QString::const_iterator end)
    {
        for (QString::const_iterator iter = begin;iter != end;++iter)
            target.append(*iter);
        return target;
    }

    /**
     * @brief replace Replaces specified portion of a string.
     * @param source Source text that will be used. Note: This is const and will
     * not be directly modified.
     * @param first First character in the range that will be replaced.
     * @param last Last character in the range that will be replaced.
     * @param replaceText Pointer to the character string to use for replacement.
     * @param len Length of the replacement character string.
     * @return Returns a new QString containing the modified result.
     */
    static inline QString replace(const QString &source,
                                   QString::const_iterator first,
                                   QString::const_iterator last,
                                   const char *replaceText, int len)
    {
        return replace(source, first, last, QString::fromUtf8(replaceText, len));
    }

    /**
     * @brief replace Replaces specified portion of a string.
     * @param source Source text that will be used. Note: This is const and will
     * not be directly modified.
     * @param first First character in the range that will be replaced.
     * @param last Last character in the range that will be replaced.
     * @param replaceText String to use for replacement.
     * @return Returns a new QString containing the modified result.
     */
    static inline QString replace(const QString &source,
                                   QString::const_iterator first,
                                   QString::const_iterator last,
                                   const QString &replaceText)
    {
        QString newStr;
        for (QString::const_iterator iter = source.constBegin(); iter != first;
             iter++)
            newStr.append(*iter);

        newStr.append(replaceText);

        for (QString::const_iterator iter = last; iter != source.constEnd();
             iter++)
            newStr.append(*iter);
        return newStr;
    }

    /**
     * @brief replace Replaces specified portion of a string.
     * @param source Source text that will be used. Note: This is const and
     * will not be directly modified.
     * @param first First character in the range that will be replaced.
     * @param last Last character in the range that will be replaced.
     * @param replaceText Pointer to the character string to use for replacement.
     * @param len Length of the replacement character string.
     * @return Returns a temporary object allocated on the stack containing
     * the modified result.
     */
    static inline QString &replace(QString &target,
                                   QString::iterator first,
                                   QString::iterator last,
                                   const char *replaceText, int len)
    {
        return replace(target, first, last, QString::fromUtf8(replaceText, len));
    }

    /**
     * @brief replace Replaces specified portion of a string.
     * @param target Target QString that will be modified.
     * @param first First character in the range that will be replaced.
     * @param last Last character in the range that will be replaced.
     * @param replaceText String to use for replacement.
     * @return Returns the string given in target, containing the modified result.
     */
    static inline QString &replace(QString &target,
                                   QString::iterator first,
                                   QString::iterator last,
                                   const QString &replaceText)
    {
        QString newStr;
        for (QString::iterator iter = target.begin(); iter != first; iter++)
            newStr.append(*iter);

        newStr.append(replaceText);

        for (QString::iterator iter = last; iter != target.end(); iter++)
            newStr.append(*iter);

        target = newStr;
        return target;
    }
};

class Qt3DSString {
public:
    inline Qt3DSString() {}

    Qt3DSString(QChar c) : m_string(QString(c)) {}

    Qt3DSString(int size, QChar c) : m_string(size, c) {}

    inline Qt3DSString(QLatin1String latin1)
        : m_string(latin1) {}

    explicit Qt3DSString(const QChar *unicode, int size = -1)
        : m_string(unicode,size) {}

    inline Qt3DSString(const QString &str) Q_DECL_NOTHROW
        : m_string(str) {}

    inline Qt3DSString(const Qt3DSString &str) Q_DECL_NOTHROW
        : m_string(str.m_string) {}

    ~Qt3DSString() {}

    inline operator QString() const
    {
        return m_string;
    }

    inline void operator=(const Qt3DSString &text)
    {
        m_string = text.m_string;
        m_isDirty = true;
    }

    inline void operator=(const QString &text)
    {
        m_string = text;
        m_isDirty = true;
    }

    // QString method wrappers
    static inline Qt3DSString fromUtf8(const char *str, int size = -1)
    {
        return Qt3DSString(QString::fromUtf8(str, size));
    }

    typedef int size_type;
    typedef qptrdiff difference_type;
    typedef const QChar & const_reference;
    typedef QChar & reference;
    typedef QChar *pointer;
    typedef const QChar *const_pointer;
    typedef QChar value_type;
    typedef QChar *iterator;
    typedef const QChar *const_iterator;
    typedef iterator Iterator;
    typedef const_iterator ConstIterator;
    typedef std::reverse_iterator<iterator> reverse_iterator;
    typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
    inline iterator begin() { return m_string.begin(); }
    inline const_iterator begin() const { return m_string.begin(); }
    inline const_iterator cbegin() const { return m_string.cbegin(); }
    inline const_iterator constBegin() const  { return m_string.constBegin(); }
    inline iterator end()  { return m_string.end(); }
    inline const_iterator end() const  { return m_string.end(); }
    inline const_iterator cend() const  { return m_string.cend(); }
    inline const_iterator constEnd() const  { return m_string.constEnd(); }
    reverse_iterator rbegin() { return reverse_iterator(end()); }
    reverse_iterator rend() { return reverse_iterator(begin()); }
    const_reverse_iterator rbegin() const { return const_reverse_iterator(end()); }
    const_reverse_iterator rend() const { return const_reverse_iterator(begin()); }
    const_reverse_iterator crbegin() const { return const_reverse_iterator(end()); }
    const_reverse_iterator crend() const { return const_reverse_iterator(begin()); }

    inline void push_back(QChar c)
    {
        m_string.push_back(c);
        m_isDirty = true;
    }

    inline void push_back(const QString &s)
    {
        m_string.push_back(s);
        m_isDirty = true;
    }

    inline void push_front(QChar c)
    {
        m_string.push_front(c);
        m_isDirty = true;
    }

    inline void push_front(const QString &s)
    {
        m_string.push_front(s);
        m_isDirty = true;
    }

    inline bool operator==(const QString &rhs)
    {
        return rhs == m_string;
    }

    inline bool operator<(const QString &rhs)
    {
        return m_string < rhs;
    }

    inline bool operator!=(const QString &rhs)
    {
        return !(*this == rhs);
    }

    inline bool operator>(const QString& rhs)
    {
        return (rhs < m_string);
    }

    inline bool operator<=(const QString& rhs)
    {
        return !operator> (rhs);
    }

    inline bool operator>=(const QString& rhs)
    {
        return !operator< (rhs);
    }

    inline  Qt3DSString& operator+=(const QString &rhs)
    {
        m_string += rhs;
        m_isDirty = true;
        return *this;
    }

    inline  Qt3DSString& operator+=(const char *s)
    {
        m_string += s;
        m_isDirty = true;
        return *this;
    }

    inline bool isEmpty() const
    {
        return m_string.isEmpty();
    }

    int indexOf(const QString &str, int from = 0,
                Qt::CaseSensitivity cs = Qt::CaseSensitive) const
    {
        return m_string.indexOf(str, from, cs);
    }

    int indexOf(QChar ch, int from = 0,
                Qt::CaseSensitivity cs = Qt::CaseSensitive) const
    {
        return m_string.indexOf(ch, from, cs);
    }

    int indexOf(QLatin1String str, int from = 0,
                Qt::CaseSensitivity cs = Qt::CaseSensitive) const
    {
        return m_string.indexOf(str, from, cs);
    }

    int indexOf(const QStringRef &str, int from = 0,
                Qt::CaseSensitivity cs = Qt::CaseSensitive) const
    {
        return m_string.indexOf(str, from, cs);
    }

    int lastIndexOf(const QString &str, int from = -1,
                    Qt::CaseSensitivity cs = Qt::CaseSensitive) const
    {
        return m_string.lastIndexOf(str, from, cs);
    }

    int lastIndexOf(QChar ch, int from = -1,
                    Qt::CaseSensitivity cs = Qt::CaseSensitive) const
    {
        return m_string.lastIndexOf(ch, from, cs);
    }

    int lastIndexOf(QLatin1String str, int from = -1,
                    Qt::CaseSensitivity cs = Qt::CaseSensitive) const
    {
        return m_string.lastIndexOf(str, from, cs);
    }

    int lastIndexOf(const QStringRef &str, int from = -1,
                    Qt::CaseSensitivity cs = Qt::CaseSensitive) const
    {
        return m_string.lastIndexOf(str, from, cs);
    }

    inline void clear()
    {
        m_string.clear();
        m_isDirty = true;
    }

    inline Qt3DSString &insert(int i, QChar c)
    {
        m_string.insert(i,c);
        m_isDirty = true;
        return *this;
    }
    inline Qt3DSString &insert(int i, const QChar *uc, int len)
    {
        m_string.insert(i,uc, len);
        m_isDirty = true;
        return *this;
    }
    inline Qt3DSString &insert(int i, const QString &s)
    {
        m_string.insert(i,s);
        m_isDirty = true;
        return *this;
    }
    inline Qt3DSString &insert(int i, const QStringRef &s)
    {
        m_string.insert(i,s);
        m_isDirty = true;
        return *this;
    }

    inline Qt3DSString &insert(int i, QLatin1String s)
    {
        m_string.insert(i,s);
        m_isDirty = true;
        return *this;
    }

    inline int size() const
    {
        return m_string.size();
    }

    inline int length() const {
        return m_string.length();
    }

    inline Qt3DSString &append(QChar c)
    {
        m_string.append(c);
        m_isDirty = true;
        return *this;
    }

    inline Qt3DSString &append(const QChar *uc, int len)
    {
        m_string.append(uc, len);
        m_isDirty = true;
        return *this;
    }

    inline Qt3DSString &append(const QString &s)
    {
        m_string.append(s);
        m_isDirty = true;
        return *this;
    }

    inline Qt3DSString &append(const QStringRef &s)
    {
        m_string.append(s);
        m_isDirty = true;
        return *this;
    }

    inline Qt3DSString &append(QLatin1String s)
    {
        m_string.append(s);
        m_isDirty = true;
        return *this;
    }

    inline int compare(const QString &s,
                       Qt::CaseSensitivity cs = Qt::CaseSensitive) const
    {
        return m_string.compare(s, cs);
    }

    // Compatibility wrappers for std::basic_string and eastl::basic_string
    // Required for now for the Qt 3D Studio's template classes.
    static const int npos = -1;

    inline char operator[](int idx) const
    {
        return m_string[idx].toLatin1();
    }

    inline void assign(const char *text)
    {
        m_string = QString::fromUtf8(text);
        m_isDirty = true;
    }

    inline void assign(const char16_t *text)
    {
        m_string = QString::fromUtf16(text);
        m_isDirty = true;
    }

    inline void assign(const char32_t *text)
    {
        m_string = QString::fromUcs4(text);
        m_isDirty = true;
    }

    inline void assign(const wchar_t *text)
    {
        m_string = QString::fromWCharArray(text);
        m_isDirty = true;
    }

    inline void assign(const eastl::string &text)
    {
        m_string = QString::fromUtf8(text.c_str());
        m_isDirty = true;
    }

    inline void operator=(const char *text)
    {
        assign(text);
    }

    inline void operator=(const char16_t *text)
    {
        assign(text);
    }

    inline void operator=(const char32_t *text)
    {
        assign(text);
    }

    inline void operator=(const wchar_t *text)
    {
        assign(text);
    }

    inline void operator=(const eastl::string &text)
    {
        assign(text);
    }

    inline void operator=(const eastl::basic_string<char*> &text)
    {
        assign(*text.c_str());
    }

    inline Qt3DSString &append(QString::iterator first,
                                 QString::iterator last)
    {
        Qt3DSStringUtils::append(m_string, first, last);
        m_isDirty = true;
        return *this;
    }

    inline Qt3DSString &append(QString::const_iterator first,
                                 QString::const_iterator last)
    {
        Qt3DSStringUtils::append(m_string, first, last);
        m_isDirty = true;
        return *this;
    }

    inline Qt3DSString &append(const char *text, int size)
    {
        m_string.append(QString::fromUtf8(text, size));
        m_isDirty = true;
        return *this;
    }

    inline Qt3DSString &append(const char *text)
    {
        m_string.append(QString::fromUtf8(text));
        m_isDirty = true;
        return *this;
    }

    inline Qt3DSString &replace(QString::iterator replaceBegin,
                                  QString::iterator replaceEnd,
                                  const char *replaceText, int len)
    {
        return replace(replaceBegin, replaceEnd,
                       QString::fromUtf8(replaceText, len));
    }

    inline Qt3DSString &replace(QString::iterator replaceBegin,
                                  QString::iterator replaceEnd,
                                  const QString &replaceText)
    {
        m_string = Qt3DSStringUtils::replace(this->m_string,
                                             replaceBegin, replaceEnd,
                                             replaceText);
        m_isDirty = true;
        return *this;
    }

    inline Qt3DSString &replace(QString::const_iterator replaceBegin,
                                  QString::const_iterator replaceEnd,
                                  const QString &replaceText)
    {
        m_string = Qt3DSStringUtils::replace(this->m_string,
                                             replaceBegin, replaceEnd,
                                             replaceText);
        m_isDirty = true;
        return *this;
    }

    inline QByteArray toUtf8() const
    {
        updateCache();
        return m_array;
    }

    inline const char *c_str() const
    {
        updateCache();
        return m_array.constData();
    }

    inline const char *c_str()
    {
        updateCache();
        return m_array.constData();
    }

private:
    inline void updateCache() const
    {
        if (m_isDirty) {
            m_array = m_string.toUtf8();
            m_isDirty = false;
        }
    }

    QString m_string;
    mutable bool m_isDirty = true;
    mutable QByteArray m_array;
};

}
}

#endif