summaryrefslogtreecommitdiff
path: root/src/utilcode/fstring.cpp
blob: 1165ecaac007a82301b06e717ab86890030db81b (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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
// ---------------------------------------------------------------------------
// FString.cpp
// 

// ---------------------------------------------------------------------------

#include "stdafx.h"
#include "ex.h"
#include "holder.h"

#include "fstring.h"


namespace FString
{

#ifdef _MSC_VER
#pragma optimize("t", on)
#endif // _MSC_VER

#define MAX_LENGTH 0x1fffff00


HRESULT Unicode_Utf8_Length(__in_z LPCWSTR pString, __out bool * pAllAscii, __out DWORD * pLength)
{
    CONTRACTL
    {
        NOTHROW;
        GC_NOTRIGGER;
    }
    CONTRACTL_END;

    * pAllAscii = true;

    LPCWSTR p = pString;

    while (true)
    {
        WCHAR ch = * p;

        // Single check for termination and non ASCII
        if (((unsigned) (ch - 1)) >= 0x7F)
        {
            if (ch != 0)
            {
                * pAllAscii = false;
            }

            break;
        }

        p ++;
    }

    if (* pAllAscii)
    {
        if ((p - pString) > MAX_LENGTH)
        {
            return COR_E_OVERFLOW;
        }

        * pLength = (DWORD) (p - pString);
    }
    else // use WideCharToMultiByte to calculate result length
    {
        * pLength = WszWideCharToMultiByte(CP_UTF8, 0, pString, -1, NULL, 0, NULL, NULL);

        if (*pLength == 0)
        {
            return HRESULT_FROM_GetLastError();
        }

        // Remove the count of null terminator, to be consistent with the all-ASCII case.
        --*pLength;

        if (*pLength > MAX_LENGTH)
        {
            return COR_E_OVERFLOW;
        }
    }

    return S_OK;
}


// UNICODE to UTF8
HRESULT Unicode_Utf8(__in_z LPCWSTR pString, bool allAscii, __out_z LPSTR pBuffer, DWORD length)
{
    CONTRACTL
    {
        NOTHROW;
        GC_NOTRIGGER;
    }
    CONTRACTL_END;

    pBuffer[length] = 0;

    if (allAscii)
    {
        LPCWSTR p = pString;

        LPSTR q = pBuffer;

        LPCWSTR endP = p + length - 8;

        // Unfold to optimize for long string: 8 chars per iteration
        while (p < endP)
        {
            q[0] = (char) p[0];
            q[1] = (char) p[1];
            q[2] = (char) p[2];
            q[3] = (char) p[3];

            q[4] = (char) p[4];
            q[5] = (char) p[5];
            q[6] = (char) p[6];
            q[7] = (char) p[7];
            
            q += 8;
            p += 8;
        }

        endP += 8;

        while (p < endP)
        {
            * q ++ = (char) * p ++;
        }
    }
    else
    {
        length = WszWideCharToMultiByte(CP_UTF8, 0, pString, -1, pBuffer, (int) length + 1, NULL, NULL);

        if (length == 0)
        {
            return HRESULT_FROM_GetLastError();
        }
    }

    return S_OK;
}


HRESULT Utf8_Unicode_Length(__in_z LPCSTR pString, __out bool * pAllAscii, __out DWORD * pLength)
{
    CONTRACTL
    {
        NOTHROW;
        GC_NOTRIGGER;
    }
    CONTRACTL_END;

    * pAllAscii = true;

    LPCSTR p = pString;

    while (true)
    {
        char ch = * p;

        // Single check for termination and non ASCII
        if (((unsigned) (ch - 1)) >= 0x7F)
        {
            if (ch != 0)
            {
                * pAllAscii = false;
            }

            break;
        }

        p ++;
    }

    if (* pAllAscii)
    {
        if ((p - pString) > MAX_LENGTH)
        {
            return COR_E_OVERFLOW;
        }

        * pLength = (DWORD)(p - pString);
    }
    else
    {
        * pLength = WszMultiByteToWideChar(CP_UTF8, 0, pString, -1, NULL, 0);

        if (* pLength == 0)
        {
            return HRESULT_FROM_GetLastError();
        }

        // Remove the count of null terminator, to be consistent with the all-ASCII case.
        --*pLength;

        if (* pLength > MAX_LENGTH)
        {
            return COR_E_OVERFLOW;
        }
    }

    return S_OK;
}


// UTF8 to Unicode

HRESULT Utf8_Unicode(__in_z LPCSTR pString, bool allAscii, __out_z LPWSTR pBuffer, DWORD length)
{
    CONTRACTL
    {
        NOTHROW;
        GC_NOTRIGGER;
    }
    CONTRACTL_END;

    pBuffer[length] = 0;

    if (allAscii)
    {
        LPCSTR p = pString;

        LPWSTR q = pBuffer;

        LPCSTR endP = p + length - 8;

        // Unfold to optimize for long string: 4 chars per iteration
        while (p < endP)
        {
            q[0] = (WCHAR) p[0];
            q[1] = (WCHAR) p[1];
            q[2] = (WCHAR) p[2];
            q[3] = (WCHAR) p[3];

            q[4] = (WCHAR) p[4];
            q[5] = (WCHAR) p[5];
            q[6] = (WCHAR) p[6];
            q[7] = (WCHAR) p[7];
            
            q += 8;
            p += 8;
        }

        endP += 8;

        while (p < endP)
        {
            * q ++ = (WCHAR) * p ++;
        }
    }
    else
    {
        length = WszMultiByteToWideChar(CP_UTF8, 0, pString, -1, pBuffer, (int) length + 1);

        if (length == 0)
        {
            return HRESULT_FROM_GetLastError();
        }
    }

    return S_OK;
}


HRESULT ConvertUnicode_Utf8(__in_z LPCWSTR pString, __out_z LPSTR * pBuffer)
{
    bool  allAscii;
    DWORD length;

    HRESULT hr = Unicode_Utf8_Length(pString, & allAscii, & length);

    if (SUCCEEDED(hr))
    {
        * pBuffer = new (nothrow) char[length + 1];

        if (* pBuffer == NULL)
        {
            hr = E_OUTOFMEMORY;
        }
        else
        {
            hr = Unicode_Utf8(pString, allAscii, * pBuffer, length);
        }
    }

    return hr;
}


HRESULT ConvertUtf8_Unicode(__in_z LPCSTR pString, __out_z LPWSTR * pBuffer)
{
    bool  allAscii;
    DWORD length;

    HRESULT hr = Utf8_Unicode_Length(pString, & allAscii, & length);

    if (SUCCEEDED(hr))
    {
        * pBuffer = new (nothrow) WCHAR[length + 1];

        if (* pBuffer == NULL)
        {
            hr = E_OUTOFMEMORY;
        }
        else
        {
            hr = Utf8_Unicode(pString, allAscii, * pBuffer, length);
        }
    }

    return hr;
}


#ifdef _MSC_VER
#pragma optimize("", on)
#endif // _MSC_VER

} // namespace FString