summaryrefslogtreecommitdiff
path: root/zen/string_base.h
blob: 96d46fc47cdb6d68a60a9f903c357342ad716c43 (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
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
// **************************************************************************
// * This file is part of the FreeFileSync project. It is distributed under *
// * GNU General Public License: http://www.gnu.org/licenses/gpl-3.0        *
// * Copyright (C) Zenju (zenju AT gmx DOT de) - All Rights Reserved        *
// **************************************************************************

#ifndef STRING_BASE_H_083217454562342526
#define STRING_BASE_H_083217454562342526

#include <algorithm>
#include <cassert>
#include <cstdint>
#include <atomic>
#include "string_tools.h"

//Zbase - a policy based string class optimizing performance and flexibility

namespace zen
{
/*
Allocator Policy:
-----------------
    void* allocate(size_t size) //throw std::bad_alloc
    void deallocate(void* ptr)
    size_t calcCapacity(size_t length)
*/
class AllocatorOptimalSpeed //exponential growth + min size
{
public:
    //::operator new/ ::operator delete show same performance characterisics like malloc()/free()!
    static void* allocate(size_t size) { return ::malloc(size); } //throw std::bad_alloc
    static void  deallocate(void* ptr) { ::free(ptr); }
    static size_t calcCapacity(size_t length) { return std::max<size_t>(16, std::max(length + length / 2, length)); }
    //- size_t might overflow! => better catch here than return a too small size covering up the real error: a way too large length!
    //- any growth rate should not exceed golden ratio: 1.618033989
};


class AllocatorOptimalMemory //no wasted memory, but more reallocations required when manipulating string
{
public:
    static void* allocate(size_t size) { return ::malloc(size); } //throw std::bad_alloc
    static void  deallocate(void* ptr) { ::free(ptr); }
    static size_t calcCapacity(size_t length) { return length; }
};

/*
Storage Policy:
---------------
template <typename Char, //Character Type
         class AP>       //Allocator Policy

    Char* create(size_t size)
    Char* create(size_t size, size_t minCapacity)
    Char* clone(Char* ptr)
    void destroy(Char* ptr) //must handle "destroy(nullptr)"!
    bool canWrite(const Char* ptr, size_t minCapacity) //needs to be checked before writing to "ptr"
    size_t length(const Char* ptr)
    void setLength(Char* ptr, size_t newLength)
*/

template <class Char, //Character Type
          class AP>   //Allocator Policy
class StorageDeepCopy : public AP
{
protected:
    ~StorageDeepCopy() {}

    Char* create(size_t size) { return create(size, size); }
    Char* create(size_t size, size_t minCapacity)
    {
        assert(size <= minCapacity);
        const size_t newCapacity = AP::calcCapacity(minCapacity);
        assert(newCapacity >= minCapacity);

        Descriptor* const newDescr = static_cast<Descriptor*>(this->allocate(sizeof(Descriptor) + (newCapacity + 1) * sizeof(Char))); //throw std::bad_alloc
        new (newDescr) Descriptor(size, newCapacity);

        return reinterpret_cast<Char*>(newDescr + 1); //alignment note: "newDescr + 1" is Descriptor-aligned, which is larger than alignment for Char-array! => no problem!
    }

    Char* clone(Char* ptr)
    {
        Char* newData = create(length(ptr)); //throw std::bad_alloc
        std::copy(ptr, ptr + length(ptr) + 1, newData);
        return newData;
    }

    void destroy(Char* ptr)
    {
        if (!ptr) return; //support "destroy(nullptr)"

        Descriptor* const d = descr(ptr);
        d->~Descriptor();
        this->deallocate(d);
    }

    //this needs to be checked before writing to "ptr"
    static bool canWrite(const Char* ptr, size_t minCapacity) { return minCapacity <= descr(ptr)->capacity; }
    static size_t length(const Char* ptr) { return descr(ptr)->length; }

    static void setLength(Char* ptr, size_t newLength)
    {
        assert(canWrite(ptr, newLength));
        descr(ptr)->length = newLength;
    }

private:
    struct Descriptor
    {
        Descriptor(size_t len, size_t cap) :
            length  (static_cast<std::uint32_t>(len)),
            capacity(static_cast<std::uint32_t>(cap)) {}

        std::uint32_t length;
        std::uint32_t capacity; //allocated size without null-termination
    };

    static       Descriptor* descr(      Char* ptr) { return reinterpret_cast<      Descriptor*>(ptr) - 1; }
    static const Descriptor* descr(const Char* ptr) { return reinterpret_cast<const Descriptor*>(ptr) - 1; }
};


template <class Char, //Character Type
          class AP>   //Allocator Policy
class StorageRefCountThreadSafe : public AP
{
protected:
    ~StorageRefCountThreadSafe() {}

    Char* create(size_t size) { return create(size, size); }
    Char* create(size_t size, size_t minCapacity)
    {
        assert(size <= minCapacity);

        const size_t newCapacity = AP::calcCapacity(minCapacity);
        assert(newCapacity >= minCapacity);

        Descriptor* const newDescr = static_cast<Descriptor*>(this->allocate(sizeof(Descriptor) + (newCapacity + 1) * sizeof(Char))); //throw std::bad_alloc
        new (newDescr) Descriptor(size, newCapacity);

        return reinterpret_cast<Char*>(newDescr + 1);
    }

    static Char* clone(Char* ptr)
    {
        ++descr(ptr)->refCount;
        return ptr;
    }

#ifdef NDEBUG
    void destroy(Char* ptr)
#else
    void destroy(Char*& ptr)
#endif
    {
        assert(ptr != reinterpret_cast<Char*>(0x1)); //detect double-deletion

        if (!ptr) //support "destroy(nullptr)"
        {
#ifndef NDEBUG
            ptr = reinterpret_cast<Char*>(0x1);
#endif
            return;
        }

        Descriptor* const d = descr(ptr);

        if (--(d->refCount) == 0) //operator--() is overloaded to decrement and evaluate in a single atomic operation!
        {
            d->~Descriptor();
            this->deallocate(d);
#ifndef NDEBUG
            ptr = reinterpret_cast<Char*>(0x1);
#endif
        }
    }

    static bool canWrite(const Char* ptr, size_t minCapacity) //needs to be checked before writing to "ptr"
    {
        const Descriptor* const d = descr(ptr);
        assert(d->refCount > 0);
        return d->refCount == 1 && minCapacity <= d->capacity;
    }

    static size_t length(const Char* ptr) { return descr(ptr)->length; }

    static void setLength(Char* ptr, size_t newLength)
    {
        assert(canWrite(ptr, newLength));
        descr(ptr)->length = static_cast<std::uint32_t>(newLength);
    }

private:
    struct Descriptor
    {
        Descriptor(size_t len, size_t cap) :
            length  (static_cast<std::uint32_t>(len)),
            capacity(static_cast<std::uint32_t>(cap)) { static_assert(ATOMIC_INT_LOCK_FREE == 2, ""); } //2: "the types are always lock-free"

        std::atomic<unsigned int> refCount { 1 }; //std:atomic is uninitialized by default!
        std::uint32_t length;
        std::uint32_t capacity; //allocated size without null-termination
    };

    static       Descriptor* descr(      Char* ptr) { return reinterpret_cast<      Descriptor*>(ptr) - 1; }
    static const Descriptor* descr(const Char* ptr) { return reinterpret_cast<const Descriptor*>(ptr) - 1; }
};

//################################################################################################################################################################

//perf note: interestingly StorageDeepCopy and StorageRefCountThreadSafe show same performance in FFS comparison

template <class Char,                                                   //Character Type
          template <class, class> class SP = StorageRefCountThreadSafe, //Storage Policy
          class AP = AllocatorOptimalSpeed>                             //Allocator Policy
class Zbase : public SP<Char, AP>
{
public:
    Zbase();
    Zbase(const Char* source); //implicit conversion from a C-string
    Zbase(const Char* source, size_t length);
    Zbase(const Zbase& source);
    Zbase(Zbase&& tmp) noexcept;
    //explicit Zbase(Char source); //dangerous if implicit: Char buffer[]; return buffer[0]; ups... forgot &, but not a compiler error! //-> non-standard extension!!!

    //allow explicit construction from different string type, prevent ambiguity via SFINAE
    //template <class S> explicit Zbase(const S& other, typename S::value_type = 0);

    ~Zbase();

    //operator const Char* () const; //NO implicit conversion to a C-string!! Many problems... one of them: if we forget to provide operator overloads, it'll just work with a Char*...

    //STL accessors
    typedef       Char*       iterator;
    typedef const Char* const_iterator;
    typedef       Char&       reference;
    typedef const Char& const_reference;
    typedef       Char value_type;

    Zbase(const_iterator first, const_iterator last);
    Char*       begin();
    Char*       end  ();
    const Char* begin() const;
    const Char* end  () const;
    const Char* cbegin() const { return begin(); }
    const Char* cend  () const { return end(); }

    //std::string functions
    size_t length() const;
    size_t size  () const { return length(); }
    const Char* c_str() const { return rawStr; } //C-string format with 0-termination
    const Char* data()  const { return rawStr; } //internal representation, 0-termination not guaranteed
    const Char operator[](size_t pos) const;
    bool empty() const { return length() == 0; }
    void clear();
    size_t find (const Zbase& str, size_t pos = 0)    const; //
    size_t find (const Char* str,  size_t pos = 0)    const; //
    size_t find (Char  ch,         size_t pos = 0)    const; //returns "npos" if not found
    size_t rfind(Char  ch,         size_t pos = npos) const; //
    size_t rfind(const Char* str,  size_t pos = npos) const; //
    //Zbase& replace(size_t pos1, size_t n1, const Zbase& str);
    void reserve(size_t minCapacity);
    Zbase& assign(const Char* source, size_t len);
    Zbase& append(const Char* source, size_t len);
    void resize(size_t newSize, Char fillChar = 0);
    void swap(Zbase& other);
    void push_back(Char val) { operator+=(val); } //STL access

    Zbase& operator=(const Zbase& source);
    Zbase& operator=(Zbase&& tmp) noexcept;
    Zbase& operator=(const Char* source);
    Zbase& operator=(Char source);
    Zbase& operator+=(const Zbase& other);
    Zbase& operator+=(const Char* other);
    Zbase& operator+=(Char ch);

    static const size_t npos = static_cast<size_t>(-1);

private:
    Zbase            (int) = delete; //
    Zbase& operator= (int) = delete; //detect usage errors by creating an intentional ambiguity with "Char"
    Zbase& operator+=(int) = delete; //
    void   push_back (int) = delete; //

    Char* rawStr;
};

template <class Char, template <class, class> class SP, class AP>        bool operator==(const Zbase<Char, SP, AP>& lhs, const Zbase<Char, SP, AP>& rhs);
template <class Char, template <class, class> class SP, class AP>        bool operator==(const Zbase<Char, SP, AP>& lhs, const Char*                rhs);
template <class Char, template <class, class> class SP, class AP> inline bool operator==(const Char*                lhs, const Zbase<Char, SP, AP>& rhs) { return operator==(rhs, lhs); }

template <class Char, template <class, class> class SP, class AP> inline bool operator!=(const Zbase<Char, SP, AP>& lhs, const Zbase<Char, SP, AP>& rhs) { return !operator==(lhs, rhs); }
template <class Char, template <class, class> class SP, class AP> inline bool operator!=(const Zbase<Char, SP, AP>& lhs, const Char*                rhs) { return !operator==(lhs, rhs); }
template <class Char, template <class, class> class SP, class AP> inline bool operator!=(const Char*                lhs, const Zbase<Char, SP, AP>& rhs) { return !operator==(lhs, rhs); }

template <class Char, template <class, class> class SP, class AP> bool operator<(const Zbase<Char, SP, AP>& lhs, const Zbase<Char, SP, AP>& rhs);
template <class Char, template <class, class> class SP, class AP> bool operator<(const Zbase<Char, SP, AP>& lhs, const Char*                rhs);
template <class Char, template <class, class> class SP, class AP> bool operator<(const Char*                lhs, const Zbase<Char, SP, AP>& rhs);

template <class Char, template <class, class> class SP, class AP> inline Zbase<Char, SP, AP> operator+(const Zbase<Char, SP, AP>& lhs, const Zbase<Char, SP, AP>& rhs) { return Zbase<Char, SP, AP>(lhs) += rhs; }
template <class Char, template <class, class> class SP, class AP> inline Zbase<Char, SP, AP> operator+(const Zbase<Char, SP, AP>& lhs, const Char*                rhs) { return Zbase<Char, SP, AP>(lhs) += rhs; }
template <class Char, template <class, class> class SP, class AP> inline Zbase<Char, SP, AP> operator+(const Zbase<Char, SP, AP>& lhs,       Char                 rhs) { return Zbase<Char, SP, AP>(lhs) += rhs; }

//don't use unified first argument but save one move-construction in the r-value case instead!
template <class Char, template <class, class> class SP, class AP> inline Zbase<Char, SP, AP> operator+(Zbase<Char, SP, AP>&& lhs, const Zbase<Char, SP, AP>& rhs) { return std::move(lhs += rhs); } //the move *is* needed!!!
template <class Char, template <class, class> class SP, class AP> inline Zbase<Char, SP, AP> operator+(Zbase<Char, SP, AP>&& lhs, const Char*                rhs) { return std::move(lhs += rhs); } //lhs, is an l-value parameter...
template <class Char, template <class, class> class SP, class AP> inline Zbase<Char, SP, AP> operator+(Zbase<Char, SP, AP>&& lhs,       Char                 rhs) { return std::move(lhs += rhs); } //and not a local variable => no copy elision

template <class Char, template <class, class> class SP, class AP> inline Zbase<Char, SP, AP> operator+(      Char          lhs, const Zbase<Char, SP, AP>& rhs) { return Zbase<Char, SP, AP>(&lhs, 1) += rhs; }
template <class Char, template <class, class> class SP, class AP> inline Zbase<Char, SP, AP> operator+(const Char*         lhs, const Zbase<Char, SP, AP>& rhs) { return Zbase<Char, SP, AP>(lhs    ) += rhs; }













//################################# implementation ########################################
template <class Char, template <class, class> class SP, class AP> inline
Zbase<Char, SP, AP>::Zbase()
{
    //resist the temptation to avoid this allocation by referening a static global: NO performance advantage, MT issues!
    rawStr    = this->create(0);
    rawStr[0] = 0;
}


template <class Char, template <class, class> class SP, class AP> inline
Zbase<Char, SP, AP>::Zbase(const Char* source)
{
    const size_t sourceLen = strLength(source);
    rawStr = this->create(sourceLen);
    std::copy(source, source + sourceLen + 1, rawStr); //include null-termination
}


template <class Char, template <class, class> class SP, class AP> inline
Zbase<Char, SP, AP>::Zbase(const Char* source, size_t sourceLen)
{
    rawStr = this->create(sourceLen);
    std::copy(source, source + sourceLen, rawStr);
    rawStr[sourceLen] = 0;
}


template <class Char, template <class, class> class SP, class AP>
Zbase<Char, SP, AP>::Zbase(const_iterator first, const_iterator last)
{
    assert(first <= last);
    const size_t sourceLen = last - first;
    rawStr = this->create(sourceLen);
    std::copy(first, last, rawStr);
    rawStr[sourceLen] = 0;
}


template <class Char, template <class, class> class SP, class AP> inline
Zbase<Char, SP, AP>::Zbase(const Zbase<Char, SP, AP>& source)
{
    rawStr = this->clone(source.rawStr);
}


template <class Char, template <class, class> class SP, class AP> inline
Zbase<Char, SP, AP>::Zbase(Zbase<Char, SP, AP>&& tmp) noexcept
{
    rawStr = tmp.rawStr;
    tmp.rawStr = nullptr; //usually nullptr would violate the class invarants, but it is good enough for the destructor!
    //caveat: do not increment ref-count of an unshared string! We'd lose optimization opportunity of reusing its memory!
}

/*
template <class Char, template <class, class> class SP, class AP>
template <class S> inline
Zbase<Char, SP, AP>::Zbase(const S& other, typename S::value_type)
{
    const size_t sourceLen = other.size();
    rawStr = this->create(sourceLen);
    std::copy(other.c_str(), other.c_str() + sourceLen, rawStr);
    rawStr[sourceLen] = 0;
}
*/

template <class Char, template <class, class> class SP, class AP> inline
Zbase<Char, SP, AP>::~Zbase()
{
    static_assert(noexcept(this->~Zbase()), ""); //has exception spec of compiler-generated destructor by default

    this->destroy(rawStr); //rawStr may be nullptr; see move constructor!
}


template <class Char, template <class, class> class SP, class AP> inline
size_t Zbase<Char, SP, AP>::find(const Zbase& str, size_t pos) const
{
    assert(pos <= length());
    const size_t len = length();
    const Char* thisEnd = begin() + len; //respect embedded 0
    const Char* it = std::search(begin() + std::min(pos, len), thisEnd,
                                 str.begin(), str.end());
    return it == thisEnd ? npos : it - begin();
}


template <class Char, template <class, class> class SP, class AP> inline
size_t Zbase<Char, SP, AP>::find(const Char* str, size_t pos) const
{
    assert(pos <= length());
    const size_t len = length();
    const Char* thisEnd = begin() + len; //respect embedded 0
    const Char* it = std::search(begin() + std::min(pos, len), thisEnd,
                                 str, str + strLength(str));
    return it == thisEnd ? npos : it - begin();
}


template <class Char, template <class, class> class SP, class AP> inline
size_t Zbase<Char, SP, AP>::find(Char ch, size_t pos) const
{
    assert(pos <= length());
    const size_t len = length();
    const Char* thisEnd = begin() + len; //respect embedded 0
    const Char* it = std::find(begin() + std::min(pos, len), thisEnd, ch);
    return it == thisEnd ? npos : it - begin();
}


template <class Char, template <class, class> class SP, class AP> inline
size_t Zbase<Char, SP, AP>::rfind(Char ch, size_t pos) const
{
    assert(pos == npos || pos <= length());
    const size_t len = length();
    const Char* currEnd = begin() + (pos == npos ? len : std::min(pos + 1, len));
    const Char* it = find_last(begin(), currEnd, ch);
    return it == currEnd ? npos : it - begin();
}


template <class Char, template <class, class> class SP, class AP> inline
size_t Zbase<Char, SP, AP>::rfind(const Char* str, size_t pos) const
{
    assert(pos == npos || pos <= length());
    const size_t strLen = strLength(str);
    const size_t len = length();
    const Char* currEnd = begin() + (pos == npos ? len : std::min(pos + strLen, len));
    const Char* it = search_last(begin(), currEnd,
                                 str, str + strLen);
    return it == currEnd ? npos : it - begin();
}


template <class Char, template <class, class> class SP, class AP> inline
void Zbase<Char, SP, AP>::resize(size_t newSize, Char fillChar)
{
    const size_t oldSize = length();
    if (this->canWrite(rawStr, newSize))
    {
        if (oldSize < newSize)
            std::fill(rawStr + oldSize, rawStr + newSize, fillChar);
        rawStr[newSize] = 0;
        this->setLength(rawStr, newSize);
    }
    else
    {
        Char* newStr = this->create(newSize);
        if (oldSize < newSize)
        {
            std::copy(rawStr, rawStr + oldSize, newStr);
            std::fill(newStr + oldSize, newStr + newSize, fillChar);
        }
        else
            std::copy(rawStr, rawStr + newSize, newStr);
        newStr[newSize] = 0;

        this->destroy(rawStr);
        rawStr = newStr;
    }
}


template <class Char, template <class, class> class SP, class AP> inline
bool operator==(const Zbase<Char, SP, AP>& lhs, const Zbase<Char, SP, AP>& rhs)
{
    return lhs.length() == rhs.length() && std::equal(lhs.begin(), lhs.end(), rhs.begin()); //respect embedded 0
}


template <class Char, template <class, class> class SP, class AP> inline
bool operator==(const Zbase<Char, SP, AP>& lhs, const Char* rhs)
{
    return lhs.length() == strLength(rhs) && std::equal(lhs.begin(), lhs.end(), rhs); //respect embedded 0
}


template <class Char, template <class, class> class SP, class AP> inline
bool operator<(const Zbase<Char, SP, AP>& lhs, const Zbase<Char, SP, AP>& rhs)
{
    return std::lexicographical_compare(lhs.begin(), lhs.end(), //respect embedded 0
                                        rhs.begin(), rhs.end());
}


template <class Char, template <class, class> class SP, class AP> inline
bool operator<(const Zbase<Char, SP, AP>& lhs, const Char* rhs)
{
    return std::lexicographical_compare(lhs.begin(), lhs.end(), //respect embedded 0
                                        rhs, rhs + strLength(rhs));
}


template <class Char, template <class, class> class SP, class AP> inline
bool operator<(const Char* lhs, const Zbase<Char, SP, AP>& rhs)
{
    return std::lexicographical_compare(lhs, lhs + strLength(lhs), //respect embedded 0
                                        rhs.begin(), rhs.end());
}


template <class Char, template <class, class> class SP, class AP> inline
size_t Zbase<Char, SP, AP>::length() const
{
    return SP<Char, AP>::length(rawStr);
}


template <class Char, template <class, class> class SP, class AP> inline
const Char Zbase<Char, SP, AP>::operator[](size_t pos) const
{
    assert(pos < length()); //design by contract! no runtime check!
    return rawStr[pos];
}


template <class Char, template <class, class> class SP, class AP> inline
const Char* Zbase<Char, SP, AP>::begin() const
{
    return rawStr;
}


template <class Char, template <class, class> class SP, class AP> inline
const Char* Zbase<Char, SP, AP>::end() const
{
    return rawStr + length();
}


template <class Char, template <class, class> class SP, class AP> inline
Char* Zbase<Char, SP, AP>::begin()
{
    reserve(length()); //make unshared!
    return rawStr;
}


template <class Char, template <class, class> class SP, class AP> inline
Char* Zbase<Char, SP, AP>::end()
{
    return begin() + length();
}


template <class Char, template <class, class> class SP, class AP> inline
void Zbase<Char, SP, AP>::clear()
{
    if (!empty())
    {
        if (this->canWrite(rawStr, 0))
        {
            rawStr[0] = 0;              //keep allocated memory
            this->setLength(rawStr, 0); //
        }
        else
            *this = Zbase();
    }
}


template <class Char, template <class, class> class SP, class AP> inline
void Zbase<Char, SP, AP>::swap(Zbase<Char, SP, AP>& other)
{
    std::swap(rawStr, other.rawStr);
}


template <class Char, template <class, class> class SP, class AP> inline
void Zbase<Char, SP, AP>::reserve(size_t minCapacity) //make unshared and check capacity
{
    if (!this->canWrite(rawStr, minCapacity))
    {
        //allocate a new string
        const size_t len = length();
        Char* newStr = this->create(len, std::max(len, minCapacity)); //reserve() must NEVER shrink the string: logical const!
        std::copy(rawStr, rawStr + len + 1, newStr); //include 0-termination

        this->destroy(rawStr);
        rawStr = newStr;
    }
}


template <class Char, template <class, class> class SP, class AP> inline
Zbase<Char, SP, AP>& Zbase<Char, SP, AP>::assign(const Char* source, size_t len)
{
    if (this->canWrite(rawStr, len))
    {
        std::copy(source, source + len, rawStr);
        rawStr[len] = 0; //include null-termination
        this->setLength(rawStr, len);
    }
    else
        *this = Zbase(source, len);

    return *this;
}


template <class Char, template <class, class> class SP, class AP> inline
Zbase<Char, SP, AP>& Zbase<Char, SP, AP>::append(const Char* source, size_t len)
{
    const size_t thisLen = length();
    reserve(thisLen + len); //make unshared and check capacity

    std::copy(source, source + len, rawStr + thisLen);
    rawStr[thisLen + len] = 0;
    this->setLength(rawStr, thisLen + len);
    return *this;
}


template <class Char, template <class, class> class SP, class AP> inline
Zbase<Char, SP, AP>& Zbase<Char, SP, AP>::operator=(const Zbase<Char, SP, AP>& other)
{
    Zbase<Char, SP, AP>(other).swap(*this);
    return *this;
}


template <class Char, template <class, class> class SP, class AP> inline
Zbase<Char, SP, AP>& Zbase<Char, SP, AP>::operator=(Zbase<Char, SP, AP>&& tmp) noexcept
{
    swap(tmp); //don't use unifying assignment but save one move-construction in the r-value case instead!
    return *this;
}


template <class Char, template <class, class> class SP, class AP> inline
Zbase<Char, SP, AP>& Zbase<Char, SP, AP>::operator=(const Char* source)
{
    return assign(source, strLength(source));
}


template <class Char, template <class, class> class SP, class AP> inline
Zbase<Char, SP, AP>& Zbase<Char, SP, AP>::operator=(Char ch)
{
    return assign(&ch, 1);
}


template <class Char, template <class, class> class SP, class AP> inline
Zbase<Char, SP, AP>& Zbase<Char, SP, AP>::operator+=(const Zbase<Char, SP, AP>& other)
{
    return append(other.c_str(), other.length());
}


template <class Char, template <class, class> class SP, class AP> inline
Zbase<Char, SP, AP>& Zbase<Char, SP, AP>::operator+=(const Char* other)
{
    return append(other, strLength(other));
}


template <class Char, template <class, class> class SP, class AP> inline
Zbase<Char, SP, AP>& Zbase<Char, SP, AP>::operator+=(Char ch)
{
    return append(&ch, 1);
}
}

#endif //STRING_BASE_H_083217454562342526
bgstack15