summaryrefslogtreecommitdiffstats
path: root/src/Runtime/Source/Qt3DSFoundation/Include/foundation/StrConvertUTF.h
blob: 965867c9f16096e060cd4ce743afa3a46d9653f4 (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
/****************************************************************************
**
** Copyright (C) 1993-2009 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_STR_CONVERT_UTF_H
#define QT3DS_RENDER_STR_CONVERT_UTF_H
#include <EASTL/string.h>
#include "foundation/ConvertUTF.h"
#include "foundation/Qt3DSAssert.h"
#include "foundation/Qt3DSSimpleTypes.h"

namespace qt3ds {
namespace foundation {

    using namespace qt3ds;
    template <typename TSrcType>
    struct ConverterType
    {
    };
    template <>
    struct ConverterType<char8_t>
    {
        typedef UTF8 TConverterType;
    };
    template <>
    struct ConverterType<char16_t>
    {
        typedef UTF16 TConverterType;
    };
    template <>
    struct ConverterType<char32_t>
    {
        typedef UTF32 TConverterType;
    };

    template <QT3DSU32 TWCharSize>
    struct WCharType
    {
    };

    template <>
    struct WCharType<2>
    {
        typedef char16_t TCharType;
    };

    template <>
    struct WCharType<4>
    {
        typedef char32_t TCharType;
    };

    typedef WCharType<sizeof(wchar_t)> TWCharEASTLConverter;

    template <typename TSrcType, typename TDestType>
    struct UTFConversionSelector
    {
    };

    template <>
    struct UTFConversionSelector<char8_t, char16_t>
    {
        static ConversionResult Convert(const UTF8 **sourceStart, const UTF8 *sourceEnd,
                                        UTF16 **targetStart, UTF16 *targetEnd,
                                        ConversionFlags flags)
        {
            return Q3DSConvertUTF8toUTF16(sourceStart, sourceEnd, targetStart, targetEnd, flags);
        }
    };

    template <>
    struct UTFConversionSelector<char8_t, char32_t>
    {
        static ConversionResult Convert(const UTF8 **sourceStart, const UTF8 *sourceEnd,
                                        UTF32 **targetStart, UTF32 *targetEnd,
                                        ConversionFlags flags)
        {
            return Q3DSConvertUTF8toUTF32(sourceStart, sourceEnd, targetStart, targetEnd, flags);
        }
    };
    template <>
    struct UTFConversionSelector<char16_t, char8_t>
    {
        static ConversionResult Convert(const UTF16 **sourceStart, const UTF16 *sourceEnd,
                                        UTF8 **targetStart, UTF8 *targetEnd, ConversionFlags flags)
        {
            return Q3DSConvertUTF16toUTF8(sourceStart, sourceEnd, targetStart, targetEnd, flags);
        }
    };
    template <>
    struct UTFConversionSelector<char16_t, char32_t>
    {
        static ConversionResult Convert(const UTF16 **sourceStart, const UTF16 *sourceEnd,
                                        UTF32 **targetStart, UTF32 *targetEnd,
                                        ConversionFlags flags)
        {
            return Q3DSConvertUTF16toUTF32(sourceStart, sourceEnd, targetStart, targetEnd, flags);
        }
    };
    template <>
    struct UTFConversionSelector<char32_t, char8_t>
    {
        static ConversionResult Convert(const UTF32 **sourceStart, const UTF32 *sourceEnd,
                                        UTF8 **targetStart, UTF8 *targetEnd, ConversionFlags flags)
        {
            return Q3DSConvertUTF32toUTF8(sourceStart, sourceEnd, targetStart, targetEnd, flags);
        }
    };
    template <>
    struct UTFConversionSelector<char32_t, char16_t>
    {
        static ConversionResult Convert(const UTF32 **sourceStart, const UTF32 *sourceEnd,
                                        UTF16 **targetStart, UTF16 *targetEnd,
                                        ConversionFlags flags)
        {
            return Q3DSConvertUTF32toUTF16(sourceStart, sourceEnd, targetStart, targetEnd, flags);
        }
    };

    // Convert into an EASTL string type.
    // inSrcLen may be zero in which case we analyze the string looking for the zero element.
    template <typename TSrcType, typename TDestType, typename TAllocType>
    bool ConvertUTF(const TSrcType *inSrc, size_t inSrcLen,
                    eastl::basic_string<TDestType, TAllocType> &outString)
    {
        typedef typename ConverterType<TDestType>::TConverterType TDestUTFType;
        typedef typename ConverterType<TSrcType>::TConverterType TSrcUTFType;
        if (inSrc == 0 || *inSrc == 0) {
            outString.clear();
            return true;
        }

        if (inSrcLen == 0) {
            // empty loop intentional.
            for (const TSrcType *ptr = inSrc; ptr && *ptr; ++ptr, ++inSrcLen) {
            }
        }

        typename eastl::basic_string<TDestType, TAllocType>::size_type capacity =
            outString.capacity();
        if (capacity == 0)
            outString.resize((QT3DSU32)inSrcLen * 2);
        else
            outString.resize(capacity);

        ConversionResult theConversionResult(conversionOK);
        TDestUTFType *writePtr = NULL;
        do {
            writePtr = reinterpret_cast<TDestUTFType *>(const_cast<TDestType *>(outString.data()));
            TDestUTFType *writeEnd(writePtr + outString.size());
            const TSrcUTFType *readPtr(reinterpret_cast<const TSrcUTFType *>(inSrc));
            const TSrcUTFType *readEnd = readPtr + inSrcLen;
            theConversionResult = UTFConversionSelector<TSrcType, TDestType>::Convert(
                &readPtr, readEnd, &writePtr, writeEnd, lenientConversion);
            if (theConversionResult == targetExhausted) {
                capacity = outString.capacity() * 2;
                outString.resize(capacity);
            }
        } while (theConversionResult == targetExhausted);

        if (theConversionResult == conversionOK) {
            TDestUTFType *writeStart =
                reinterpret_cast<TDestUTFType *>(const_cast<TDestType *>(outString.data()));
            outString.resize((QT3DSU32)(writePtr - writeStart));
            return true;
        } else {
            outString.clear();
            QT3DS_ASSERT(false);
            return false;
        }
    }

#ifdef WIDE_IS_DIFFERENT_TYPE_THAN_CHAR16_T

    template <typename TDestType, typename TAllocType>
    bool ConvertWideUTF(const wchar_t *inSrc, size_t inSrcLen,
                        eastl::basic_string<TDestType, TAllocType> &outString)
    {
        return ConvertUTF((const TWCharEASTLConverter::TCharType *)inSrc, inSrcLen, outString);
    }

#endif
}
}

#endif