1 // -*- c-basic-offset: 2 -*-
3 * This file is part of the KDE libraries
4 * Copyright (C) 2006 George Staikos <staikos@kde.org>
5 * Copyright (C) 2006 Alexey Proskuryakov <ap@nypop.com>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public License
18 * along with this library; see the file COPYING.LIB. If not, write to
19 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
24 #ifndef KJS_UNICODE_ICU_H
25 #define KJS_UNICODE_ICU_H
27 #include <unicode/uchar.h>
28 #include <unicode/ustring.h>
30 #include "../UnicodeCategory.h"
35 inline int toLower(uint16_t* str, int strLength, uint16_t*& destIfNeeded)
37 UErrorCode err = U_ZERO_ERROR;
41 resultLength = u_strToLower(0, 0, reinterpret_cast< ::UChar*>(str), strLength, "", &err);
43 if (resultLength <= strLength) {
45 u_strToLower(reinterpret_cast< ::UChar*>(str), resultLength, reinterpret_cast< ::UChar*>(str), strLength, "", &err);
48 destIfNeeded = (uint16_t*)malloc(resultLength * sizeof(uint16_t));
49 u_strToLower(reinterpret_cast< ::UChar*>(destIfNeeded), resultLength, reinterpret_cast< ::UChar*>(str), strLength, "", &err);
52 return U_FAILURE(err) ? -1 : resultLength;
55 inline int toUpper(uint16_t* str, int strLength, uint16_t*& destIfNeeded)
57 UErrorCode err = U_ZERO_ERROR;
61 resultLength = u_strToUpper(0, 0, reinterpret_cast< ::UChar*>(str), strLength, "", &err);
63 if (resultLength <= strLength) {
65 u_strToUpper(reinterpret_cast< ::UChar*>(str), resultLength, reinterpret_cast< ::UChar*>(str), strLength, "", &err);
68 destIfNeeded = (uint16_t*)malloc(resultLength * sizeof(uint16_t));
69 u_strToUpper(reinterpret_cast< ::UChar*>(destIfNeeded), resultLength, reinterpret_cast< ::UChar*>(str), strLength, "", &err);
72 return U_FAILURE(err) ? -1 : resultLength;
75 inline bool isFormatChar(int32_t c)
77 return u_charType(c) == U_FORMAT_CHAR;
80 inline bool isSeparatorSpace(int32_t c)
82 return u_charType(c) == U_SPACE_SEPARATOR;
85 inline bool isPrintableChar(int32_t c)
90 inline CharCategory category(int32_t c)
92 switch (u_charType(c)) {
93 case U_NON_SPACING_MARK:
94 return Mark_NonSpacing;
95 case U_COMBINING_SPACING_MARK:
96 return Mark_SpacingCombining;
97 case U_ENCLOSING_MARK:
98 return Mark_Enclosing;
99 case U_DECIMAL_DIGIT_NUMBER:
100 return Number_DecimalDigit;
101 case U_LETTER_NUMBER:
102 return Number_Letter;
105 case U_SPACE_SEPARATOR:
106 return Separator_Space;
107 case U_LINE_SEPARATOR:
108 return Separator_Line;
109 case U_PARAGRAPH_SEPARATOR:
110 return Separator_Paragraph;
112 return Other_Control;
116 return Other_Surrogate;
117 case U_PRIVATE_USE_CHAR:
118 return Other_PrivateUse;
119 case U_GENERAL_OTHER_TYPES:
120 return Other_NotAssigned;
121 case U_UPPERCASE_LETTER:
122 return Letter_Uppercase;
123 case U_LOWERCASE_LETTER:
124 return Letter_Lowercase;
125 case U_TITLECASE_LETTER:
126 return Letter_Titlecase;
127 case U_MODIFIER_LETTER:
128 return Letter_Modifier;
131 case U_CONNECTOR_PUNCTUATION:
132 return Punctuation_Connector;
133 case U_DASH_PUNCTUATION:
134 return Punctuation_Dash;
135 case U_START_PUNCTUATION:
136 return Punctuation_Open;
137 case U_END_PUNCTUATION:
138 return Punctuation_Close;
139 case U_INITIAL_PUNCTUATION:
140 return Punctuation_InitialQuote;
141 case U_FINAL_PUNCTUATION:
142 return Punctuation_FinalQuote;
143 case U_OTHER_PUNCTUATION:
144 return Punctuation_Other;
147 case U_CURRENCY_SYMBOL:
148 return Symbol_Currency;
149 case U_MODIFIER_SYMBOL:
150 return Symbol_Modifier;