summaryrefslogtreecommitdiff
path: root/Source/WTF/wtf/unicode/wchar/UnicodeWchar.h
blob: 10c2026c5faf879b45250859c7231e52fef09430 (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
/*
 * Copyright (C) 2012 Patrick Gansterer <paroga@paroga.com>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef WTF_UnicodeWchar_h
#define WTF_UnicodeWchar_h

#include <stdint.h>
#include <wchar.h>
#include <wtf/unicode/ScriptCodesFromICU.h>
#include <wtf/unicode/UnicodeMacrosFromICU.h>

typedef wchar_t UChar;
typedef uint32_t UChar32;

namespace WTF {
namespace Unicode {

enum Direction {
    LeftToRight,
    RightToLeft,
    EuropeanNumber,
    EuropeanNumberSeparator,
    EuropeanNumberTerminator,
    ArabicNumber,
    CommonNumberSeparator,
    BlockSeparator,
    SegmentSeparator,
    WhiteSpaceNeutral,
    OtherNeutral,
    LeftToRightEmbedding,
    LeftToRightOverride,
    RightToLeftArabic,
    RightToLeftEmbedding,
    RightToLeftOverride,
    PopDirectionalFormat,
    NonSpacingMark,
    BoundaryNeutral
};

enum DecompositionType {
    DecompositionNone,
    DecompositionCanonical,
    DecompositionCompat,
    DecompositionCircle,
    DecompositionFinal,
    DecompositionFont,
    DecompositionFraction,
    DecompositionInitial,
    DecompositionIsolated,
    DecompositionMedial,
    DecompositionNarrow,
    DecompositionNoBreak,
    DecompositionSmall,
    DecompositionSquare,
    DecompositionSub,
    DecompositionSuper,
    DecompositionVertical,
    DecompositionWide
};

enum CharCategory {
    NoCategory = 0,
    Other_NotAssigned = U_MASK(0),
    Letter_Uppercase = U_MASK(1),
    Letter_Lowercase = U_MASK(2),
    Letter_Titlecase = U_MASK(3),
    Letter_Modifier = U_MASK(4),
    Letter_Other = U_MASK(5),

    Mark_NonSpacing = U_MASK(6),
    Mark_Enclosing = U_MASK(7),
    Mark_SpacingCombining = U_MASK(8),

    Number_DecimalDigit = U_MASK(9),
    Number_Letter = U_MASK(10),
    Number_Other = U_MASK(11),

    Separator_Space = U_MASK(12),
    Separator_Line = U_MASK(13),
    Separator_Paragraph = U_MASK(14),

    Other_Control = U_MASK(15),
    Other_Format = U_MASK(16),
    Other_PrivateUse = U_MASK(17),
    Other_Surrogate = U_MASK(18),

    Punctuation_Dash = U_MASK(19),
    Punctuation_Open = U_MASK(20),
    Punctuation_Close = U_MASK(21),
    Punctuation_Connector = U_MASK(22),
    Punctuation_Other = U_MASK(23),

    Symbol_Math = U_MASK(24),
    Symbol_Currency = U_MASK(25),
    Symbol_Modifier = U_MASK(26),
    Symbol_Other = U_MASK(27),

    Punctuation_InitialQuote = U_MASK(28),
    Punctuation_FinalQuote = U_MASK(29)
};


WTF_EXPORT_PRIVATE CharCategory category(UChar32);
WTF_EXPORT_PRIVATE unsigned char combiningClass(UChar32);
WTF_EXPORT_PRIVATE Direction direction(UChar32);
WTF_EXPORT_PRIVATE DecompositionType decompositionType(UChar32);
WTF_EXPORT_PRIVATE bool hasLineBreakingPropertyComplexContext(UChar32);
WTF_EXPORT_PRIVATE UChar32 mirroredChar(UChar32);

inline bool isAlphanumeric(UChar c) { return !!iswalnum(c); }
inline bool isDigit(UChar c) { return !!iswdigit(c); }
inline bool isLetter(UChar c) { return !!iswalpha(c); }
inline bool isLower(UChar c) { return !!iswlower(c); }
inline bool isPrintableChar(UChar c) { return !!iswprint(c); }
inline bool isPunct(UChar c) { return !!iswpunct(c); }
inline bool isSpace(UChar c) { return !!iswspace(c); }
inline bool isUpper(UChar c) { return !!iswupper(c); }

inline bool isArabicChar(UChar32 c) { return c >= 0x0600 && c <= 0x06ff; }
inline bool isSeparatorSpace(UChar32 c) { return category(c) == Separator_Space; }

inline UChar foldCase(UChar c) { return towlower(c); }
inline UChar toLower(UChar c) { return towlower(c); }
inline UChar toUpper(UChar c) { return towupper(c); }
inline UChar toTitleCase(UChar c) { return towupper(c); }

WTF_EXPORT_PRIVATE int foldCase(UChar* result, int resultLength, const UChar* source, int sourceLength, bool* isError);
WTF_EXPORT_PRIVATE int toLower(UChar* result, int resultLength, const UChar* source, int sourceLength, bool* isError);
WTF_EXPORT_PRIVATE int toUpper(UChar* result, int resultLength, const UChar* source, int sourceLength, bool* isError);

inline int umemcasecmp(const UChar* a, const UChar* b, int len)
{
    for (int i = 0; i < len; ++i) {
        UChar c1 = foldCase(a[i]);
        UChar c2 = foldCase(b[i]);
        if (c1 != c2)
            return c1 - c2;
    }

    return 0;
}

} // namespace Unicode
} // namespace WTF

#endif // WTF_UnicodeWchar_h