File: profilerstring.h

package info (click to toggle)
procdump 2.2-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 12,900 kB
  • sloc: cpp: 42,512; ansic: 4,610; sh: 1,447; makefile: 107; cs: 76
file content (379 lines) | stat: -rw-r--r-- 7,838 bytes parent folder | download
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#pragma once

#include <iostream>
#include <assert.h>
#include <cstring>
#include <string>
#include <algorithm>

#ifdef _WIN32
#define WCHAR(str) L##str
#define CAST_CHAR(ch) ch

#else // defined(_WIN32)

// Definitely won't work for non-ascii characters so hopefully we never start using
// them in the tests
#define CAST_CHAR(ch) static_cast<wchar_t>(ch)

// On linux the runtime uses 16 bit strings but the native platform wchar_t is 32 bit.
// This means there aren't c runtime functions like wcslen for 16 bit strings. The idea
// here is to provide the easy ones to avoid all the copying and transforming. If more complex
// string operations become necessary we should either write them in C++ or convert the string to
// 32 bit and call the c runtime ones.

#define WCHAR(str) u##str
inline size_t wcslen(const char16_t *str)
{
    if (str == NULL)
    {
        return 0;
    }

    size_t len = 0;
    while (*str != 0)
    {
        ++str;
        ++len;
    }

    return len;
}

inline int wcscmp(const char16_t *lhs, const char16_t *rhs)
{
    int i = 0;
    while (true)
    {
        if (lhs[i] != rhs[i] || (lhs[i] == 0 && rhs[i] == 0))
        {
            break;
        }

        ++i;
    }

    return lhs[i] - rhs[i];
}

inline int wcscpy(char16_t *dst, size_t dmax,const char16_t *src)
{
    while (dmax > 0) {
        *dst = *src;
        if (*dst == u'\0') {
                return 0;
        }

        dmax--;
        dst++;
        src++;
    }
    return 0;
}
inline int wcslwr(char16_t *str, size_t len)
{
    for (size_t i = 0; i < len; ++i)
    {
        if ((str[i] >= u'A') && (str[i] <= u'Z'))
        str[i] = str[i] + (u'a' - u'A');   
    }
    return 0;
}

#endif // defined(__WIN32)

template<typename TARGET, typename SOURCE>
TARGET convertString(const SOURCE &s)
{
    TARGET result;
    result.assign(s.begin(), s.end());
    return result;
}

// 16 bit string type that works cross plat and doesn't require changing widths
// on non-windows platforms
class String
{
    friend std::wostream& operator<<(std::wostream& os, const String& obj);
private:
    WCHAR *buffer;
    size_t bufferLen;
    wchar_t *printBuffer;
    size_t printBufferLen;
    const size_t DefaultStringLength = 1024;

    void CopyBuffer(const WCHAR *other)
    {
        assert(other != nullptr);

        size_t otherLen = wcslen(other) + 1;
        if (buffer == nullptr || otherLen > bufferLen)
        {
            bufferLen = std::max(DefaultStringLength, otherLen);
            if (buffer != nullptr)
            {
                delete[] buffer;
            }

            buffer = new WCHAR[bufferLen];
        }

        memcpy(buffer, other, otherLen * sizeof(WCHAR));
    }

public:
    String(const WCHAR *s = WCHAR("")) :
        buffer(nullptr),
        bufferLen(0),
        printBuffer(nullptr),
        printBufferLen(0)
    {
        CopyBuffer(s);
    }

    ~String()
    {
        if (buffer != nullptr)
        {
            bufferLen = 0;
            delete[] buffer;
        }

        if (printBuffer != nullptr)
        {
            printBufferLen = 0;
            delete[] printBuffer;
        }
    }

    String(const String& other) :
        buffer(nullptr),
        bufferLen(0),
        printBuffer(nullptr),
        printBufferLen(0)
    {
        CopyBuffer(other.buffer);
    }

    String(String&& other) noexcept :
        buffer(nullptr),
        bufferLen(0),
        printBuffer(nullptr),
        printBufferLen(0)
    {
        std::swap(buffer, other.buffer);
        std::swap(bufferLen, other.bufferLen);
    }

    String& operator=(const String& other)
    {
        if(this != &other)
        {
            if (other.buffer != nullptr)
            {
                CopyBuffer(other.buffer);
            }
        }

        printBuffer = nullptr;
        printBufferLen = 0;

        return *this;
    }

    String& operator=(String&& other) noexcept
    {
        std::swap(buffer, other.buffer);
        std::swap(bufferLen, other.bufferLen);

        printBuffer = nullptr;
        printBufferLen = 0;

        return *this;
    }

    bool operator==(const String& other) const
    {
        if (buffer == nullptr)
        {
            return buffer == other.buffer;
        }

        return wcscmp(buffer, other.buffer) == 0;
    }

    bool operator!=(const String& other) const
    {
        return !(*this == other);
    }

    String& operator+=(const String& other)
    {
        size_t currLen = wcslen(buffer);
        size_t otherLen = wcslen(other.buffer);
        size_t candidateLen = currLen + otherLen + 1;

        if (candidateLen > bufferLen)
        {
            WCHAR *newBuffer = new WCHAR[candidateLen];
            memcpy(newBuffer, buffer, currLen * sizeof(WCHAR));
            delete[] buffer;
            buffer = newBuffer;
        }

        memcpy(buffer + currLen, other.buffer, otherLen * sizeof(WCHAR));
        buffer[candidateLen - 1] = 0;
        return *this;
    }

    WCHAR& operator[] (size_t pos)
    {
        return buffer[pos];
    }

    const WCHAR& operator[] (size_t pos) const
    {
        return buffer[pos];
    }

    void Clear()
    {
        if (buffer != nullptr)
        {
            buffer[0] = 0;
        }
    }

    const wchar_t *ToCStr()
    {
        if (bufferLen == 0 || buffer == nullptr)
        {
            // Nothing to convert
            return nullptr;
        }

        if (bufferLen > printBufferLen)
        {
            if (printBuffer != nullptr)
            {
                delete[] printBuffer;
            }

            printBuffer = new wchar_t[bufferLen];
            printBufferLen = bufferLen;
        }

        for (size_t i = 0; i < bufferLen; ++i)
        {
            printBuffer[i] = CAST_CHAR(buffer[i]);
        }

        // Make sure it's null terminated
        printBuffer[bufferLen - 1] = '\0';

        return printBuffer;
    }

    std::wstring ToWString()
    {
        std::wstring temp;
        for (size_t i = 0; i < bufferLen; ++i)
        {
            if (buffer[i] == 0)
            {
                break;
            }

            temp.push_back(CAST_CHAR(buffer[i]));
        }

        return temp;
    }

    size_t Length() const
    {
        return wcslen(buffer);
    }
};

inline std::wostream& operator<<(std::wostream& os, const String& obj)
{
    for (size_t i = 0; i < obj.bufferLen; ++i)
    {
        if (obj.buffer[i] == 0)
        {
            break;
        }

        os << CAST_CHAR(obj.buffer[i]);
    }

    return os;
}

inline bool EndsWith(const char *lhs, const char *rhs)
{
    size_t lhsLen = strlen(lhs);
    size_t rhsLen = strlen(rhs);
    if (lhsLen < rhsLen)
    {
        return false;
    }

    size_t lhsPos = lhsLen - rhsLen;
    size_t rhsPos = 0;

    while (rhsPos < rhsLen)
    {
        if (lhs[lhsPos] != rhs[rhsPos])
        {
            return false;
        }

        ++lhsPos;
        ++rhsPos;
    }

    return true;
}

inline bool EndsWith(const String &lhs, const String &rhs)
{
    size_t lhsLength = lhs.Length();
    size_t rhsLength = rhs.Length();
    if (lhsLength < rhsLength)
    {
        return false;
    }

    size_t lhsPos = lhsLength - rhsLength;
    size_t rhsPos = 0;

    while (rhsPos < rhsLength)
    {
        if (std::tolower(lhs[lhsPos]) != std::tolower(rhs[rhsPos]))
        {
            return false;
        }

        ++lhsPos;
        ++rhsPos;
    }

    return true;
}

inline WCHAR *getWCHARs(const String &src)
{
    size_t len = src.Length();
    WCHAR* tempbuff = new WCHAR[len+1];
    for (size_t i = 0; i < len; ++i)
    {
        tempbuff[i] = src[i];
    }
    tempbuff[len] = u'\0';
    return tempbuff;
}