1 /* 2 * Copyright (C) 2006 George Staikos <staikos (at) kde.org> 3 * Copyright (C) 2006 Alexey Proskuryakov <ap (at) nypop.com> 4 * Copyright (C) 2006, 2007, 2008, 2009 Apple Inc. All rights reserved. 5 * 6 * This library is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU Library General Public 8 * License as published by the Free Software Foundation; either 9 * version 2 of the License, or (at your option) any later version. 10 * 11 * This library is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 * Library General Public License for more details. 15 * 16 * You should have received a copy of the GNU Library General Public License 17 * along with this library; see the file COPYING.LIB. If not, write to 18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 19 * Boston, MA 02110-1301, USA. 20 * 21 */ 22 23 #ifndef WTF_UNICODE_ICU_H 24 #define WTF_UNICODE_ICU_H 25 26 #include <stdlib.h> 27 #include <unicode/uchar.h> 28 #include <unicode/uscript.h> 29 #include <unicode/ustring.h> 30 #include <unicode/utf16.h> 31 32 namespace WTF { 33 34 namespace Unicode { 35 36 enum Direction { 37 LeftToRight = U_LEFT_TO_RIGHT, 38 RightToLeft = U_RIGHT_TO_LEFT, 39 EuropeanNumber = U_EUROPEAN_NUMBER, 40 EuropeanNumberSeparator = U_EUROPEAN_NUMBER_SEPARATOR, 41 EuropeanNumberTerminator = U_EUROPEAN_NUMBER_TERMINATOR, 42 ArabicNumber = U_ARABIC_NUMBER, 43 CommonNumberSeparator = U_COMMON_NUMBER_SEPARATOR, 44 BlockSeparator = U_BLOCK_SEPARATOR, 45 SegmentSeparator = U_SEGMENT_SEPARATOR, 46 WhiteSpaceNeutral = U_WHITE_SPACE_NEUTRAL, 47 OtherNeutral = U_OTHER_NEUTRAL, 48 LeftToRightEmbedding = U_LEFT_TO_RIGHT_EMBEDDING, 49 LeftToRightOverride = U_LEFT_TO_RIGHT_OVERRIDE, 50 RightToLeftArabic = U_RIGHT_TO_LEFT_ARABIC, 51 RightToLeftEmbedding = U_RIGHT_TO_LEFT_EMBEDDING, 52 RightToLeftOverride = U_RIGHT_TO_LEFT_OVERRIDE, 53 PopDirectionalFormat = U_POP_DIRECTIONAL_FORMAT, 54 NonSpacingMark = U_DIR_NON_SPACING_MARK, 55 BoundaryNeutral = U_BOUNDARY_NEUTRAL 56 }; 57 58 enum DecompositionType { 59 DecompositionNone = U_DT_NONE, 60 DecompositionCanonical = U_DT_CANONICAL, 61 DecompositionCompat = U_DT_COMPAT, 62 DecompositionCircle = U_DT_CIRCLE, 63 DecompositionFinal = U_DT_FINAL, 64 DecompositionFont = U_DT_FONT, 65 DecompositionFraction = U_DT_FRACTION, 66 DecompositionInitial = U_DT_INITIAL, 67 DecompositionIsolated = U_DT_ISOLATED, 68 DecompositionMedial = U_DT_MEDIAL, 69 DecompositionNarrow = U_DT_NARROW, 70 DecompositionNoBreak = U_DT_NOBREAK, 71 DecompositionSmall = U_DT_SMALL, 72 DecompositionSquare = U_DT_SQUARE, 73 DecompositionSub = U_DT_SUB, 74 DecompositionSuper = U_DT_SUPER, 75 DecompositionVertical = U_DT_VERTICAL, 76 DecompositionWide = U_DT_WIDE, 77 }; 78 79 enum CharCategory { 80 NoCategory = 0, 81 Other_NotAssigned = U_MASK(U_GENERAL_OTHER_TYPES), 82 Letter_Uppercase = U_MASK(U_UPPERCASE_LETTER), 83 Letter_Lowercase = U_MASK(U_LOWERCASE_LETTER), 84 Letter_Titlecase = U_MASK(U_TITLECASE_LETTER), 85 Letter_Modifier = U_MASK(U_MODIFIER_LETTER), 86 Letter_Other = U_MASK(U_OTHER_LETTER), 87 88 Mark_NonSpacing = U_MASK(U_NON_SPACING_MARK), 89 Mark_Enclosing = U_MASK(U_ENCLOSING_MARK), 90 Mark_SpacingCombining = U_MASK(U_COMBINING_SPACING_MARK), 91 92 Number_DecimalDigit = U_MASK(U_DECIMAL_DIGIT_NUMBER), 93 Number_Letter = U_MASK(U_LETTER_NUMBER), 94 Number_Other = U_MASK(U_OTHER_NUMBER), 95 96 Separator_Space = U_MASK(U_SPACE_SEPARATOR), 97 Separator_Line = U_MASK(U_LINE_SEPARATOR), 98 Separator_Paragraph = U_MASK(U_PARAGRAPH_SEPARATOR), 99 100 Other_Control = U_MASK(U_CONTROL_CHAR), 101 Other_Format = U_MASK(U_FORMAT_CHAR), 102 Other_PrivateUse = U_MASK(U_PRIVATE_USE_CHAR), 103 Other_Surrogate = U_MASK(U_SURROGATE), 104 105 Punctuation_Dash = U_MASK(U_DASH_PUNCTUATION), 106 Punctuation_Open = U_MASK(U_START_PUNCTUATION), 107 Punctuation_Close = U_MASK(U_END_PUNCTUATION), 108 Punctuation_Connector = U_MASK(U_CONNECTOR_PUNCTUATION), 109 Punctuation_Other = U_MASK(U_OTHER_PUNCTUATION), 110 111 Symbol_Math = U_MASK(U_MATH_SYMBOL), 112 Symbol_Currency = U_MASK(U_CURRENCY_SYMBOL), 113 Symbol_Modifier = U_MASK(U_MODIFIER_SYMBOL), 114 Symbol_Other = U_MASK(U_OTHER_SYMBOL), 115 116 Punctuation_InitialQuote = U_MASK(U_INITIAL_PUNCTUATION), 117 Punctuation_FinalQuote = U_MASK(U_FINAL_PUNCTUATION) 118 }; 119 120 inline UChar32 foldCase(UChar32 c) 121 { 122 return u_foldCase(c, U_FOLD_CASE_DEFAULT); 123 } 124 125 inline int foldCase(UChar* result, int resultLength, const UChar* src, int srcLength, bool* error) 126 { 127 UErrorCode status = U_ZERO_ERROR; 128 int realLength = u_strFoldCase(result, resultLength, src, srcLength, U_FOLD_CASE_DEFAULT, &status); 129 *error = !U_SUCCESS(status); 130 return realLength; 131 } 132 133 inline int toLower(UChar* result, int resultLength, const UChar* src, int srcLength, bool* error) 134 { 135 UErrorCode status = U_ZERO_ERROR; 136 int realLength = u_strToLower(result, resultLength, src, srcLength, "", &status); 137 *error = !!U_FAILURE(status); 138 return realLength; 139 } 140 141 inline UChar32 toLower(UChar32 c) 142 { 143 return u_tolower(c); 144 } 145 146 inline UChar32 toUpper(UChar32 c) 147 { 148 return u_toupper(c); 149 } 150 151 inline int toUpper(UChar* result, int resultLength, const UChar* src, int srcLength, bool* error) 152 { 153 UErrorCode status = U_ZERO_ERROR; 154 int realLength = u_strToUpper(result, resultLength, src, srcLength, "", &status); 155 *error = !!U_FAILURE(status); 156 return realLength; 157 } 158 159 inline UChar32 toTitleCase(UChar32 c) 160 { 161 return u_totitle(c); 162 } 163 164 inline bool isArabicChar(UChar32 c) 165 { 166 return ublock_getCode(c) == UBLOCK_ARABIC; 167 } 168 169 inline bool isAlphanumeric(UChar32 c) 170 { 171 return u_isalnum(c); 172 } 173 174 inline bool isSeparatorSpace(UChar32 c) 175 { 176 return u_charType(c) == U_SPACE_SEPARATOR; 177 } 178 179 inline bool isPrintableChar(UChar32 c) 180 { 181 return !!u_isprint(c); 182 } 183 184 inline bool isPunct(UChar32 c) 185 { 186 return !!u_ispunct(c); 187 } 188 189 inline bool hasLineBreakingPropertyComplexContext(UChar32 c) 190 { 191 return u_getIntPropertyValue(c, UCHAR_LINE_BREAK) == U_LB_COMPLEX_CONTEXT; 192 } 193 194 inline UChar32 mirroredChar(UChar32 c) 195 { 196 return u_charMirror(c); 197 } 198 199 inline CharCategory category(UChar32 c) 200 { 201 return static_cast<CharCategory>(U_GET_GC_MASK(c)); 202 } 203 204 inline Direction direction(UChar32 c) 205 { 206 return static_cast<Direction>(u_charDirection(c)); 207 } 208 209 inline bool isLower(UChar32 c) 210 { 211 return !!u_islower(c); 212 } 213 214 inline uint8_t combiningClass(UChar32 c) 215 { 216 return u_getCombiningClass(c); 217 } 218 219 inline DecompositionType decompositionType(UChar32 c) 220 { 221 return static_cast<DecompositionType>(u_getIntPropertyValue(c, UCHAR_DECOMPOSITION_TYPE)); 222 } 223 224 inline int umemcasecmp(const UChar* a, const UChar* b, int len) 225 { 226 return u_memcasecmp(a, b, len, U_FOLD_CASE_DEFAULT); 227 } 228 229 } // namespace Unicode 230 231 } // namespace WTF 232 233 #endif // WTF_UNICODE_ICU_H 234