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
|
/*
* Copyright (C) 2008 Jürg Billeter <j@bitron.ch>
* Copyright (C) 2008 Dominik Röttsches <dominik.roettsches@access-company.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*
*/
#include "config.h"
#include "UnicodeGLib.h"
namespace WTF {
namespace Unicode {
UChar32 foldCase(UChar32 ch)
{
GOwnPtr<GError> gerror;
GOwnPtr<char> utf8char;
utf8char.set(g_ucs4_to_utf8(reinterpret_cast<gunichar*>(&ch), 1, 0, 0, &gerror.outPtr()));
if (gerror)
return ch;
GOwnPtr<char> utf8caseFolded;
utf8caseFolded.set(g_utf8_casefold(utf8char.get(), -1));
GOwnPtr<gunichar> ucs4Result;
ucs4Result.set(g_utf8_to_ucs4_fast(utf8caseFolded.get(), -1, 0));
return *ucs4Result;
}
int foldCase(UChar* result, int resultLength, const UChar* src, int srcLength, bool* error)
{
*error = false;
GOwnPtr<GError> gerror;
GOwnPtr<char> utf8src;
utf8src.set(g_utf16_to_utf8(src, srcLength, 0, 0, &gerror.outPtr()));
if (gerror) {
*error = true;
return -1;
}
GOwnPtr<char> utf8result;
utf8result.set(g_utf8_casefold(utf8src.get(), -1));
long utf16resultLength = -1;
GOwnPtr<UChar> utf16result;
utf16result.set(g_utf8_to_utf16(utf8result.get(), -1, 0, &utf16resultLength, &gerror.outPtr()));
if (gerror) {
*error = true;
return -1;
}
if (utf16resultLength > resultLength) {
*error = true;
return utf16resultLength;
}
memcpy(result, utf16result.get(), utf16resultLength * sizeof(UChar));
return utf16resultLength;
}
int toLower(UChar* result, int resultLength, const UChar* src, int srcLength, bool* error)
{
*error = false;
GOwnPtr<GError> gerror;
GOwnPtr<char> utf8src;
utf8src.set(g_utf16_to_utf8(src, srcLength, 0, 0, &gerror.outPtr()));
if (gerror) {
*error = true;
return -1;
}
GOwnPtr<char> utf8result;
utf8result.set(g_utf8_strdown(utf8src.get(), -1));
long utf16resultLength = -1;
GOwnPtr<UChar> utf16result;
utf16result.set(g_utf8_to_utf16(utf8result.get(), -1, 0, &utf16resultLength, &gerror.outPtr()));
if (gerror) {
*error = true;
return -1;
}
if (utf16resultLength > resultLength) {
*error = true;
return utf16resultLength;
}
memcpy(result, utf16result.get(), utf16resultLength * sizeof(UChar));
return utf16resultLength;
}
int toUpper(UChar* result, int resultLength, const UChar* src, int srcLength, bool* error)
{
*error = false;
GOwnPtr<GError> gerror;
GOwnPtr<char> utf8src;
utf8src.set(g_utf16_to_utf8(src, srcLength, 0, 0, &gerror.outPtr()));
if (gerror) {
*error = true;
return -1;
}
GOwnPtr<char> utf8result;
utf8result.set(g_utf8_strup(utf8src.get(), -1));
long utf16resultLength = -1;
GOwnPtr<UChar> utf16result;
utf16result.set(g_utf8_to_utf16(utf8result.get(), -1, 0, &utf16resultLength, &gerror.outPtr()));
if (gerror) {
*error = true;
return -1;
}
if (utf16resultLength > resultLength) {
*error = true;
return utf16resultLength;
}
memcpy(result, utf16result.get(), utf16resultLength * sizeof(UChar));
return utf16resultLength;
}
Direction direction(UChar32 c)
{
PangoBidiType type = pango_bidi_type_for_unichar(c);
switch (type) {
case PANGO_BIDI_TYPE_L:
return LeftToRight;
case PANGO_BIDI_TYPE_R:
return RightToLeft;
case PANGO_BIDI_TYPE_AL:
return RightToLeftArabic;
case PANGO_BIDI_TYPE_LRE:
return LeftToRightEmbedding;
case PANGO_BIDI_TYPE_RLE:
return RightToLeftEmbedding;
case PANGO_BIDI_TYPE_LRO:
return LeftToRightOverride;
case PANGO_BIDI_TYPE_RLO:
return RightToLeftOverride;
case PANGO_BIDI_TYPE_PDF:
return PopDirectionalFormat;
case PANGO_BIDI_TYPE_EN:
return EuropeanNumber;
case PANGO_BIDI_TYPE_AN:
return ArabicNumber;
case PANGO_BIDI_TYPE_ES:
return EuropeanNumberSeparator;
case PANGO_BIDI_TYPE_ET:
return EuropeanNumberTerminator;
case PANGO_BIDI_TYPE_CS:
return CommonNumberSeparator;
case PANGO_BIDI_TYPE_NSM:
return NonSpacingMark;
case PANGO_BIDI_TYPE_BN:
return BoundaryNeutral;
case PANGO_BIDI_TYPE_B:
return BlockSeparator;
case PANGO_BIDI_TYPE_S:
return SegmentSeparator;
case PANGO_BIDI_TYPE_WS:
return WhiteSpaceNeutral;
default:
return OtherNeutral;
}
}
int umemcasecmp(const UChar* a, const UChar* b, int len)
{
GOwnPtr<char> utf8a;
GOwnPtr<char> utf8b;
utf8a.set(g_utf16_to_utf8(a, len, 0, 0, 0));
utf8b.set(g_utf16_to_utf8(b, len, 0, 0, 0));
GOwnPtr<char> foldedA;
GOwnPtr<char> foldedB;
foldedA.set(g_utf8_casefold(utf8a.get(), -1));
foldedB.set(g_utf8_casefold(utf8b.get(), -1));
// FIXME: umemcasecmp needs to mimic u_memcasecmp of icu
// from the ICU docs:
// "Compare two strings case-insensitively using full case folding.
// his is equivalent to u_strcmp(u_strFoldCase(s1, n, options), u_strFoldCase(s2, n, options))."
//
// So it looks like we don't need the full g_utf8_collate here,
// but really a bitwise comparison of casefolded unicode chars (not utf-8 bytes).
// As there is no direct equivalent to this icu function in GLib, for now
// we'll use g_utf8_collate():
return g_utf8_collate(foldedA.get(), foldedB.get());
}
}
}
|