summaryrefslogtreecommitdiffstats
path: root/source/core/slang-string.cpp
blob: e9804eaa8cd33aba33163b756a4ebd342afde2b3 (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
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
#include "slang-string.h"

#include "slang-char-util.h"
#include "slang-text-io.h"

namespace Slang
{
// HACK!
// JS: Many of the inlined functions of CharUtil just access a global map. That referencing this
// global is *NOT* enough to link correctly with CharUtil on linux for a shared library. The
// following call exists to try and force linkage of CharUtil for anything that uses core
static const auto s_charUtilLink = CharUtil::_ensureLink();


// StringRepresentation

void StringRepresentation::setContents(const UnownedStringSlice& slice)
{
    const auto sliceLength = slice.getLength();
    SLANG_ASSERT(sliceLength <= capacity);

    char* chars = getData();

    // Use move (rather than memcpy), because the slice *could* be contained in the
    // StringRepresentation
    ::memmove(chars, slice.begin(), sliceLength * sizeof(char));
    // Zero terminate.
    chars[sliceLength] = 0;
    // Set the length
    length = sliceLength;
}


/* static */ StringRepresentation* StringRepresentation::create(const UnownedStringSlice& slice)
{
    const auto sliceLength = slice.getLength();

    if (sliceLength)
    {
        StringRepresentation* rep = StringRepresentation::createWithLength(sliceLength);

        char* chars = rep->getData();
        ::memcpy(chars, slice.begin(), sizeof(char) * sliceLength);
        chars[sliceLength] = 0;

        return rep;
    }
    else
    {
        return nullptr;
    }
}

/* static */ StringRepresentation* StringRepresentation::createWithReference(
    const UnownedStringSlice& slice)
{
    const auto sliceLength = slice.getLength();

    if (sliceLength)
    {
        StringRepresentation* rep = StringRepresentation::createWithLength(sliceLength);
        rep->addReference();

        char* chars = rep->getData();
        ::memcpy(chars, slice.begin(), sizeof(char) * sliceLength);
        chars[sliceLength] = 0;

        return rep;
    }
    else
    {
        return nullptr;
    }
}

// OSString

OSString::OSString()
    : m_begin(nullptr), m_end(nullptr)
{
}

OSString::OSString(wchar_t* begin, wchar_t* end)
    : m_begin(begin), m_end(end)
{
}

void OSString::_releaseBuffer()
{
    if (m_begin)
    {
        delete[] m_begin;
    }
}

void OSString::set(const wchar_t* begin, const wchar_t* end)
{
    if (m_begin)
    {
        delete[] m_begin;
        m_begin = nullptr;
        m_end = nullptr;
    }
    const size_t len = end - begin;
    if (len > 0)
    {
        // TODO(JS): The allocation is only done this way to be compatible with the buffer being
        // detached from an array This is unfortunate, because it means that the allocation stores
        // the size (and alignment fix), which is a shame because we know the size
        m_begin = new wchar_t[len + 1];
        memcpy(m_begin, begin, len * sizeof(wchar_t));
        // Zero terminate
        m_begin[len] = 0;
        m_end = m_begin + len;
    }
}

static const wchar_t kEmptyOSString[] = {0};

wchar_t const* OSString::begin() const
{
    return m_begin ? m_begin : kEmptyOSString;
}

wchar_t const* OSString::end() const
{
    return m_end ? m_end : kEmptyOSString;
}

// UnownedStringSlice

bool UnownedStringSlice::startsWith(UnownedStringSlice const& other) const
{
    UInt thisSize = getLength();
    UInt otherSize = other.getLength();

    if (otherSize > thisSize)
        return false;

    return head(otherSize) == other;
}

bool UnownedStringSlice::startsWith(char const* str) const
{
    return startsWith(UnownedTerminatedStringSlice(str));
}

bool UnownedStringSlice::startsWithCaseInsensitive(UnownedStringSlice const& other) const
{
    UInt thisSize = getLength();
    UInt otherSize = other.getLength();

    if (otherSize > thisSize)
        return false;

    return head(otherSize).caseInsensitiveEquals(other);
}


bool UnownedStringSlice::endsWith(UnownedStringSlice const& other) const
{
    UInt thisSize = getLength();
    UInt otherSize = other.getLength();

    if (otherSize > thisSize)
        return false;

    return UnownedStringSlice(end() - otherSize, end()) == other;
}

bool UnownedStringSlice::endsWithCaseInsensitive(UnownedStringSlice const& other) const
{
    UInt thisSize = getLength();
    UInt otherSize = other.getLength();

    if (otherSize > thisSize)
        return false;

    return UnownedStringSlice(end() - otherSize, end()).caseInsensitiveEquals(other);
}

bool UnownedStringSlice::endsWith(char const* str) const
{
    return endsWith(UnownedTerminatedStringSlice(str));
}

bool UnownedStringSlice::endsWithCaseInsensitive(char const* str) const
{
    return endsWithCaseInsensitive(UnownedTerminatedStringSlice(str));
}

UnownedStringSlice UnownedStringSlice::trim() const
{
    const char* start = m_begin;
    const char* end = m_end;

    while (start < end && CharUtil::isHorizontalWhitespace(*start))
        start++;
    while (end > start && CharUtil::isHorizontalWhitespace(end[-1]))
        end--;
    return UnownedStringSlice(start, end);
}

UnownedStringSlice UnownedStringSlice::trimStart() const
{
    const char* start = m_begin;

    while (start < m_end && CharUtil::isHorizontalWhitespace(*start))
        start++;
    return UnownedStringSlice(start, m_end);
}

UnownedStringSlice UnownedStringSlice::trim(char c) const
{
    const char* start = m_begin;
    const char* end = m_end;

    while (start < end && *start == c)
        start++;
    while (end > start && end[-1] == c)
        end--;
    return UnownedStringSlice(start, end);
}

// StringSlice

StringSlice::StringSlice()
    : representation(0), beginIndex(0), endIndex(0)
{
}

StringSlice::StringSlice(String const& str)
    : representation(str.m_buffer), beginIndex(0), endIndex(str.getLength())
{
}

StringSlice::StringSlice(String const& str, UInt beginIndex, UInt endIndex)
    : representation(str.m_buffer), beginIndex(beginIndex), endIndex(endIndex)
{
}


//

_EndLine EndLine;

String operator+(const char* op1, const String& op2)
{
    String result(op1);
    result.append(op2);
    return result;
}

String operator+(const String& op1, const char* op2)
{
    String result(op1);
    result.append(op2);
    return result;
}

String operator+(const String& op1, const String& op2)
{
    String result(op1);
    result.append(op2);
    return result;
}

int stringToInt(const String& str, int radix)
{
    if (str.startsWith("0x"))
        return (int)strtoll(str.getBuffer(), NULL, 16);
    else
        return (int)strtoll(str.getBuffer(), NULL, radix);
}
unsigned int stringToUInt(const String& str, int radix)
{
    if (str.startsWith("0x"))
        return (unsigned int)strtoull(str.getBuffer(), NULL, 16);
    else
        return (unsigned int)strtoull(str.getBuffer(), NULL, radix);
}
double stringToDouble(const String& str)
{
    return (double)strtod(str.getBuffer(), NULL);
}
float stringToFloat(const String& str)
{
    return strtof(str.getBuffer(), NULL);
}

#if 0
    String String::ReplaceAll(String src, String dst) const
    {
        String rs = *this;
        int index = 0;
        int srcLen = src.length;
        int len = rs.length;
        while ((index = rs.IndexOf(src, index)) != -1)
        {
            rs = rs.SubString(0, index) + dst + rs.SubString(index + srcLen, len - index - srcLen);
            len = rs.length;
        }
        return rs;
    }
#endif

String String::fromWString(const wchar_t* wstr)
{
    List<char> buf;
#ifdef _WIN32
    Slang::CharEncoding::UTF16->decode(
        (const Byte*)wstr,
        (int)(wcslen(wstr) * sizeof(wchar_t)),
        buf);
#else
    Slang::CharEncoding::UTF32->decode(
        (const Byte*)wstr,
        (int)(wcslen(wstr) * sizeof(wchar_t)),
        buf);
#endif
    return String(buf.begin(), buf.end());
}

String String::fromWString(const wchar_t* wstr, const wchar_t* wend)
{
    List<char> buf;
#ifdef _WIN32
    Slang::CharEncoding::UTF16->decode(
        (const Byte*)wstr,
        (int)((wend - wstr) * sizeof(wchar_t)),
        buf);
#else
    Slang::CharEncoding::UTF32->decode(
        (const Byte*)wstr,
        (int)((wend - wstr) * sizeof(wchar_t)),
        buf);
#endif
    return String(buf.begin(), buf.end());
}

String String::fromWChar(const wchar_t ch)
{
    List<char> buf;
#ifdef _WIN32
    Slang::CharEncoding::UTF16->decode((const Byte*)&ch, (int)(sizeof(wchar_t)), buf);
#else
    Slang::CharEncoding::UTF32->decode((const Byte*)&ch, (int)(sizeof(wchar_t)), buf);
#endif
    return String(buf.begin(), buf.end());
}

/* static */ String String::fromUnicodePoint(Char32 codePoint)
{
    char buf[6];
    int len = Slang::encodeUnicodePointToUTF8(codePoint, buf);
    return String(buf, buf + len);
}

OSString String::toWString(Index* outLength) const
{
    if (!m_buffer)
    {
        return OSString();
    }
    else
    {
        List<Byte> buf;
        switch (sizeof(wchar_t))
        {
        case 2:
            Slang::CharEncoding::UTF16->encode(getUnownedSlice(), buf);
            break;

        case 4:
            Slang::CharEncoding::UTF32->encode(getUnownedSlice(), buf);
            break;

        default:
            break;
        }

        auto length = Index(buf.getCount() / sizeof(wchar_t));
        if (outLength)
            *outLength = length;

        for (size_t ii = 0; ii < sizeof(wchar_t); ++ii)
            buf.add(0);

        wchar_t* beginData = (wchar_t*)buf.getBuffer();
        wchar_t* endData = beginData + length;

        OSString ret;
        ret.set(beginData, endData);
        return ret;
    }
}

//

void String::ensureUniqueStorageWithCapacity(Index requiredCapacity)
{
    if (m_buffer && m_buffer->isUniquelyReferenced() && m_buffer->capacity >= requiredCapacity)
        return;

    Index newCapacity = m_buffer ? 2 * m_buffer->capacity : 16;
    if (newCapacity < requiredCapacity)
    {
        newCapacity = requiredCapacity;
    }

    Index length = getLength();
    StringRepresentation* newRepresentation =
        StringRepresentation::createWithCapacityAndLength(newCapacity, length);

    if (m_buffer)
    {
        memcpy(newRepresentation->getData(), m_buffer->getData(), length + 1);
    }

    m_buffer = newRepresentation;
}

char* String::prepareForAppend(Index count)
{
    auto oldLength = getLength();
    auto newLength = oldLength + count;
    ensureUniqueStorageWithCapacity(newLength);
    return getData() + oldLength;
}
void String::appendInPlace(const char* chars, Index count)
{
    SLANG_UNUSED(chars);

    if (count > 0)
    {
        SLANG_ASSERT(m_buffer && m_buffer->isUniquelyReferenced());

        auto oldLength = getLength();
        auto newLength = oldLength + count;

        char* dst = m_buffer->getData();

        // Make sure the input buffer is the same one returned from prepareForAppend
        SLANG_ASSERT(chars == dst + oldLength);
        // It has to fit within the capacity
        SLANG_ASSERT(newLength <= m_buffer->capacity);

        // We just need to modify the length
        m_buffer->length = newLength;

        // And mark with a terminating 0
        dst[newLength] = 0;
    }
}

void String::reduceLength(Index newLength)
{
    Index oldLength = getLength();
    SLANG_ASSERT(newLength <= oldLength);
    if (oldLength == newLength)
    {
        return;
    }

    // It must have a buffer, because only 0 length allows for nullptr
    // and being 0 sized is already covered
    SLANG_ASSERT(m_buffer);

    if (m_buffer->isUniquelyReferenced())
    {
        m_buffer->length = newLength;
        m_buffer->getData()[newLength] = 0;
    }
    else
    {
        // If 0 length is wanted we can just free
        if (newLength == 0)
        {
            m_buffer.setNull();
        }
        else
        {
            // We need to make a new copy, that we will shrink

            // We'll just go with capacity enough for the new length
            const Index newCapacity = newLength;
            StringRepresentation* newRepresentation =
                StringRepresentation::createWithCapacityAndLength(newCapacity, newLength);

            // Copy
            char* dst = newRepresentation->getData();
            memcpy(dst, m_buffer->getData(), sizeof(char) * newLength);
            // Zero terminate
            dst[newLength] = 0;

            // Set the new rep
            m_buffer = newRepresentation;
        }
    }
}

void String::append(char const* str, size_t len)
{
    append(str, str + len);
}

void String::append(const char* textBegin, char const* textEnd)
{
    auto oldLength = getLength();
    auto textLength = textEnd - textBegin;
    if (textLength <= 0)
        return;

    auto newLength = oldLength + textLength;

    ensureUniqueStorageWithCapacity(newLength);

    memcpy(getData() + oldLength, textBegin, textLength);
    getData()[newLength] = 0;
    m_buffer->length = newLength;
}

void String::append(char const* str)
{
    if (str)
    {
        append(str, str + strlen(str));
    }
}

void String::appendRepeatedChar(char chr, Index count)
{
    SLANG_ASSERT(count >= 0);
    if (count > 0)
    {
        char* chars = prepareForAppend(count);
        // Set all space to repeated chr.
        ::memset(chars, chr, sizeof(char) * count);
        appendInPlace(chars, count);
    }
}

void String::appendChar(char c)
{
    const auto oldLength = getLength();
    const auto newLength = oldLength + 1;

    ensureUniqueStorageWithCapacity(newLength);

    // Since there must be space for at least one character, m_buffer cannot be nullptr
    SLANG_ASSERT(m_buffer);
    char* data = m_buffer->getData();
    data[oldLength] = c;
    data[newLength] = 0;

    m_buffer->length = newLength;
}

void String::append(char chr)
{
    appendChar(chr);
}

void String::append(String const& str)
{
    if (!m_buffer)
    {
        m_buffer = str.m_buffer;
        return;
    }

    append(str.begin(), str.end());
}

void String::append(StringSlice const& slice)
{
    append(slice.begin(), slice.end());
}

void String::append(UnownedStringSlice const& slice)
{
    append(slice.begin(), slice.end());
}

void String::append(int32_t value, int radix)
{
    enum
    {
        kCount = 33
    };
    char* data = prepareForAppend(kCount);
    const auto count = intToAscii(data, value, radix);
    m_buffer->length += count;
}

void String::append(uint32_t value, int radix)
{
    enum
    {
        kCount = 33
    };
    char* data = prepareForAppend(kCount);
    const auto count = intToAscii(data, value, radix);
    m_buffer->length += count;
}

void String::append(int64_t value, int radix)
{
    enum
    {
        kCount = 65
    };
    char* data = prepareForAppend(kCount);
    auto count = intToAscii(data, value, radix);
    m_buffer->length += count;
}

void String::append(uint64_t value, int radix)
{
    enum
    {
        kCount = 65
    };
    char* data = prepareForAppend(kCount);
    auto count = intToAscii(data, value, radix);
    m_buffer->length += count;
}

void String::append(float val, const char* format)
{
    enum
    {
        kCount = 128
    };
    char* data = prepareForAppend(kCount);
    sprintf_s(data, kCount, format, val);
    m_buffer->length += strnlen_s(data, kCount);
}

void String::append(double val, const char* format)
{
    enum
    {
        kCount = 128
    };
    char* data = prepareForAppend(kCount);
    sprintf_s(data, kCount, format, val);
    m_buffer->length += strnlen_s(data, kCount);
}

void String::append(StableHashCode32 value)
{
    const Index digits = 8;
    // + null terminator
    char* data = prepareForAppend(digits + 1);
    auto count = intToAscii(data, value.hash, 16, digits);
    m_buffer->length += count;
}

void String::append(StableHashCode64 value)
{
    const Index digits = 16;
    // + null terminator
    char* data = prepareForAppend(digits + 1);
    auto count = intToAscii(data, value.hash, 16, digits);
    m_buffer->length += count;
}

// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! UnownedStringSlice !!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Index UnownedStringSlice::indexOf(char c) const
{
    const Index size = Index(m_end - m_begin);
    for (Index i = 0; i < size; ++i)
    {
        if (m_begin[i] == c)
        {
            return i;
        }
    }
    return -1;
}

Index UnownedStringSlice::indexOf(const UnownedStringSlice& in) const
{
    const Index len = getLength();
    const Index inLen = in.getLength();
    if (inLen > len)
    {
        return -1;
    }

    const char* inChars = in.m_begin;
    switch (inLen)
    {
    case 0:
        return 0;
    case 1:
        return indexOf(inChars[0]);
    default:
        break;
    }

    const char* chars = m_begin;
    const char firstChar = inChars[0];

    for (Int i = 0; i <= len - inLen; ++i)
    {
        if (chars[i] == firstChar && in == UnownedStringSlice(chars + i, inLen))
        {
            return i;
        }
    }

    return -1;
}

UnownedStringSlice UnownedStringSlice::subString(Index idx, Index len) const
{
    const Index totalLen = getLength();
    SLANG_ASSERT(idx >= 0 && len >= 0 && idx <= totalLen);

    // If too large, we truncate
    len = (idx + len > totalLen) ? (totalLen - idx) : len;

    // Return the substring
    return UnownedStringSlice(m_begin + idx, m_begin + idx + len);
}

bool UnownedStringSlice::operator==(ThisType const& other) const
{
    // Note that memcmp is undefined when passed in null ptrs, so if we want to handle
    // we need to cover that case.
    // Can only be nullptr if size is 0.
    auto thisSize = getLength();
    auto otherSize = other.getLength();

    if (thisSize != otherSize)
    {
        return false;
    }

    const char* const thisChars = begin();
    const char* const otherChars = other.begin();
    if (thisChars == otherChars || thisSize == 0)
    {
        return true;
    }
    SLANG_ASSERT(thisChars && otherChars);
    return memcmp(thisChars, otherChars, thisSize) == 0;
}

bool UnownedStringSlice::caseInsensitiveEquals(const ThisType& rhs) const
{
    const auto length = getLength();
    if (length != rhs.getLength())
    {
        return false;
    }

    const char* a = m_begin;
    const char* b = rhs.m_begin;

    // Assuming this is a faster test
    if (memcmp(a, b, length) != 0)
    {
        // They aren't identical so compare character by character
        for (Index i = 0; i < length; ++i)
        {
            if (CharUtil::toLower(a[i]) != CharUtil::toLower(b[i]))
            {
                return false;
            }
        }
    }

    return true;
}
} // namespace Slang

std::ostream& operator<<(std::ostream& stream, const Slang::String& s)
{
    stream << s.getBuffer();
    return stream;
}