| 1 | /* |
| 2 | Copyright (C) 2012-2018 Brazil |
| 3 | Copyright (C) 2018-2025 Sutou Kouhei <[email protected]> |
| 4 | Copyright (C) 2022 Horimoto Yasuhiro <[email protected]> |
| 5 | |
| 6 | This library is free software; you can redistribute it and/or |
| 7 | modify it under the terms of the GNU Lesser General Public |
| 8 | License as published by the Free Software Foundation; either |
| 9 | version 2.1 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 | Lesser General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU Lesser General Public |
| 17 | License along with this library; if not, write to the Free Software |
| 18 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 | */ |
| 20 | |
| 21 | #include <string.h> |
| 22 | |
| 23 | #include "grn_ii.h" |
| 24 | #include "grn_normalizer.h" |
| 25 | #include "grn_romaji.h" |
| 26 | #include <groonga/normalizer.h> |
| 27 | #include <groonga/tokenizer.h> |
| 28 | |
| 29 | grn_rc |
| 30 | grn_normalizer_register(grn_ctx *ctx, |
| 31 | const char *name_ptr, |
| 32 | int name_length, |
| 33 | grn_proc_func *init, |
| 34 | grn_proc_func *next, |
| 35 | grn_proc_func *fin) |
| 36 | { |
| 37 | grn_expr_var vars[1]; |
| 38 | memset(vars, 0, sizeof(vars)); |
| 39 | GRN_PTR_INIT(&vars[0].value, 0, GRN_ID_NIL); |
| 40 | |
| 41 | if (name_length < 0) { |
| 42 | name_length = (int)strlen(name_ptr); |
| 43 | } |
| 44 | |
| 45 | { |
| 46 | grn_obj *const normalizer = grn_proc_create(ctx, |
| 47 | name_ptr, |
| 48 | name_length, |
| 49 | GRN_PROC_NORMALIZER, |
| 50 | init, |
| 51 | next, |
| 52 | fin, |
| 53 | sizeof(*vars) / sizeof(vars), |
| 54 | vars); |
| 55 | if (!normalizer) { |
| 56 | GRN_PLUGIN_ERROR(ctx, |
| 57 | GRN_NORMALIZER_ERROR, |
| 58 | "[normalizer] failed to register normalizer: <%.*s>", |
| 59 | name_length, |
| 60 | name_ptr); |
| 61 | return ctx->rc; |
| 62 | } |
| 63 | } |
| 64 | return GRN_SUCCESS; |
| 65 | } |
| 66 | |
| 67 | grn_rc |
| 68 | grn_normalizer_init(void) |
| 69 | { |
| 70 | return GRN_SUCCESS; |
| 71 | } |
| 72 | |
| 73 | grn_rc |
| 74 | grn_normalizer_fin(void) |
| 75 | { |
| 76 | return GRN_SUCCESS; |
| 77 | } |
| 78 | |
| 79 | static unsigned char symbol[] = { |
| 80 | ',', '.', 0, ':', ';', '?', '!', 0, 0, 0, '`', 0, '^', '~', |
| 81 | '_', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, '-', '-', '/', |
| 82 | '\\', 0, 0, '|', 0, 0, 0, '\'', 0, '"', '(', ')', 0, 0, |
| 83 | '[', ']', '{', '}', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 84 | '+', '-', 0, 0, 0, '=', 0, '<', '>', 0, 0, 0, 0, 0, |
| 85 | 0, 0, 0, 0, 0, 0, '$', 0, 0, '%', '#', '&', '*', '@', |
| 86 | 0, 0, 0, 0, 0, 0, 0, 0}; |
| 87 | |
| 88 | static inline grn_obj * |
| 89 | eucjp_normalize(grn_ctx *ctx, grn_string *nstr) |
| 90 | { |
| 91 | static uint16_t hankana[] = { |
| 92 | 0xa1a1, 0xa1a3, 0xa1d6, 0xa1d7, 0xa1a2, 0xa1a6, 0xa5f2, 0xa5a1, |
| 93 | 0xa5a3, 0xa5a5, 0xa5a7, 0xa5a9, 0xa5e3, 0xa5e5, 0xa5e7, 0xa5c3, |
| 94 | 0xa1bc, 0xa5a2, 0xa5a4, 0xa5a6, 0xa5a8, 0xa5aa, 0xa5ab, 0xa5ad, |
| 95 | 0xa5af, 0xa5b1, 0xa5b3, 0xa5b5, 0xa5b7, 0xa5b9, 0xa5bb, 0xa5bd, |
| 96 | 0xa5bf, 0xa5c1, 0xa5c4, 0xa5c6, 0xa5c8, 0xa5ca, 0xa5cb, 0xa5cc, |
| 97 | 0xa5cd, 0xa5ce, 0xa5cf, 0xa5d2, 0xa5d5, 0xa5d8, 0xa5db, 0xa5de, |
| 98 | 0xa5df, 0xa5e0, 0xa5e1, 0xa5e2, 0xa5e4, 0xa5e6, 0xa5e8, 0xa5e9, |
| 99 | 0xa5ea, 0xa5eb, 0xa5ec, 0xa5ed, 0xa5ef, 0xa5f3, 0xa1ab, 0xa1eb}; |
| 100 | static unsigned char dakuten[] = { |
| 101 | 0xf4, 0, 0, 0, 0, 0xac, 0, 0xae, 0, 0xb0, 0, |
| 102 | 0xb2, 0, 0xb4, 0, 0xb6, 0, 0xb8, 0, 0xba, 0, 0xbc, |
| 103 | 0, 0xbe, 0, 0xc0, 0, 0xc2, 0, 0, 0xc5, 0, 0xc7, |
| 104 | 0, 0xc9, 0, 0, 0, 0, 0, 0, 0xd0, 0, 0, |
| 105 | 0xd3, 0, 0, 0xd6, 0, 0, 0xd9, 0, 0, 0xdc}; |
| 106 | static unsigned char handaku[] = |
| 107 | {0xd1, 0, 0, 0xd4, 0, 0, 0xd7, 0, 0, 0xda, 0, 0, 0xdd}; |
| 108 | int16_t *ch; |
| 109 | const unsigned char *s, *s_, *e; |
| 110 | unsigned char *d, *d0, *d_, b; |
| 111 | uint_least8_t *cp, *ctypes, ctype; |
| 112 | size_t size = nstr->original_length_in_bytes, length = 0; |
| 113 | int removeblankp = nstr->flags & GRN_STRING_REMOVE_BLANK; |
| 114 | if (!(nstr->normalized = GRN_MALLOC(size * 2 + 1))) { |
| 115 | ERR(GRN_NO_MEMORY_AVAILABLE, |
| 116 | "[string][eucjp] failed to allocate normalized text space"); |
| 117 | return NULL; |
| 118 | } |
| 119 | d0 = (unsigned char *)nstr->normalized; |
| 120 | if (nstr->flags & GRN_STRING_WITH_CHECKS) { |
| 121 | if (!(nstr->checks = GRN_MALLOC((size * 2 + 1) * sizeof(int16_t)))) { |
| 122 | GRN_FREE(nstr->normalized); |
| 123 | nstr->normalized = NULL; |
| 124 | ERR(GRN_NO_MEMORY_AVAILABLE, |
| 125 | "[string][eucjp] failed to allocate checks space"); |
| 126 | return NULL; |
| 127 | } |
| 128 | } |
| 129 | ch = nstr->checks; |
| 130 | if (nstr->flags & GRN_STRING_WITH_TYPES) { |
| 131 | if (!(nstr->ctypes = GRN_MALLOC(size + 1))) { |
| 132 | GRN_FREE(nstr->checks); |
| 133 | GRN_FREE(nstr->normalized); |
| 134 | nstr->checks = NULL; |
| 135 | nstr->normalized = NULL; |
| 136 | ERR(GRN_NO_MEMORY_AVAILABLE, |
| 137 | "[string][eucjp] failed to allocate character types space"); |
| 138 | return NULL; |
| 139 | } |
| 140 | } |
| 141 | cp = ctypes = nstr->ctypes; |
| 142 | e = (unsigned char *)nstr->original + size; |
| 143 | for (s = s_ = (unsigned char *)nstr->original, d = d_ = d0; s < e; s++) { |
| 144 | if ((*s & 0x80)) { |
| 145 | if (((s + 1) < e) && (*(s + 1) & 0x80)) { |
| 146 | unsigned char c1 = *s++, c2 = *s, c3 = 0; |
| 147 | switch (c1 >> 4) { |
| 148 | case 0x08: |
| 149 | if (c1 == 0x8e && 0xa0 <= c2 && c2 <= 0xdf) { |
| 150 | uint16_t c = hankana[c2 - 0xa0]; |
| 151 | switch (c) { |
| 152 | case 0xa1ab: |
| 153 | if (d > d0 + 1 && d[-2] == 0xa5 && 0xa6 <= d[-1] && |
| 154 | d[-1] <= 0xdb && (b = dakuten[d[-1] - 0xa6])) { |
| 155 | *(d - 1) = b; |
| 156 | if (ch) { |
| 157 | ch[-1] += 2; |
| 158 | s_ += 2; |
| 159 | } |
| 160 | continue; |
| 161 | } else { |
| 162 | *d++ = (unsigned char)(c >> 8); |
| 163 | *d = (unsigned char)(c & 0xff); |
| 164 | } |
| 165 | break; |
| 166 | case 0xa1eb: |
| 167 | if (d > d0 + 1 && d[-2] == 0xa5 && 0xcf <= d[-1] && |
| 168 | d[-1] <= 0xdb && (b = handaku[d[-1] - 0xcf])) { |
| 169 | *(d - 1) = b; |
| 170 | if (ch) { |
| 171 | ch[-1] += 2; |
| 172 | s_ += 2; |
| 173 | } |
| 174 | continue; |
| 175 | } else { |
| 176 | *d++ = (unsigned char)(c >> 8); |
| 177 | *d = (unsigned char)(c & 0xff); |
| 178 | } |
| 179 | break; |
| 180 | default: |
| 181 | *d++ = (unsigned char)(c >> 8); |
| 182 | *d = (unsigned char)(c & 0xff); |
| 183 | break; |
| 184 | } |
| 185 | ctype = GRN_CHAR_KATAKANA; |
| 186 | } else { |
| 187 | *d++ = c1; |
| 188 | *d = c2; |
| 189 | ctype = GRN_CHAR_OTHERS; |
| 190 | } |
| 191 | break; |
| 192 | case 0x09: |
| 193 | *d++ = c1; |
| 194 | *d = c2; |
| 195 | ctype = GRN_CHAR_OTHERS; |
| 196 | break; |
| 197 | case 0x0a: |
| 198 | switch (c1 & 0x0f) { |
| 199 | case 1: |
| 200 | switch (c2) { |
| 201 | case 0xbc: |
| 202 | *d++ = c1; |
| 203 | *d = c2; |
| 204 | ctype = GRN_CHAR_KATAKANA; |
| 205 | break; |
| 206 | case 0xb9: |
| 207 | *d++ = c1; |
| 208 | *d = c2; |
| 209 | ctype = GRN_CHAR_KANJI; |
| 210 | break; |
| 211 | case 0xa1: |
| 212 | if (removeblankp) { |
| 213 | if (cp > ctypes) { |
| 214 | *(cp - 1) |= GRN_CHAR_BLANK; |
| 215 | } |
| 216 | continue; |
| 217 | } else { |
| 218 | *d = ' '; |
| 219 | ctype = GRN_CHAR_BLANK | GRN_CHAR_SYMBOL; |
| 220 | } |
| 221 | break; |
| 222 | default: |
| 223 | if (c2 >= 0xa4 && (c3 = symbol[c2 - 0xa4])) { |
| 224 | *d = c3; |
| 225 | ctype = GRN_CHAR_SYMBOL; |
| 226 | } else { |
| 227 | *d++ = c1; |
| 228 | *d = c2; |
| 229 | ctype = GRN_CHAR_OTHERS; |
| 230 | } |
| 231 | break; |
| 232 | } |
| 233 | break; |
| 234 | case 2: |
| 235 | *d++ = c1; |
| 236 | *d = c2; |
| 237 | ctype = GRN_CHAR_SYMBOL; |
| 238 | break; |
| 239 | case 3: |
| 240 | c3 = c2 - 0x80; |
| 241 | if ('a' <= c3 && c3 <= 'z') { |
| 242 | ctype = GRN_CHAR_ALPHA; |
| 243 | *d = c3; |
| 244 | } else if ('A' <= c3 && c3 <= 'Z') { |
| 245 | ctype = GRN_CHAR_ALPHA; |
| 246 | *d = c3 + 0x20; |
| 247 | } else if ('0' <= c3 && c3 <= '9') { |
| 248 | ctype = GRN_CHAR_DIGIT; |
| 249 | *d = c3; |
| 250 | } else { |
| 251 | ctype = GRN_CHAR_OTHERS; |
| 252 | *d++ = c1; |
| 253 | *d = c2; |
| 254 | } |
| 255 | break; |
| 256 | case 4: |
| 257 | *d++ = c1; |
| 258 | *d = c2; |
| 259 | ctype = GRN_CHAR_HIRAGANA; |
| 260 | break; |
| 261 | case 5: |
| 262 | *d++ = c1; |
| 263 | *d = c2; |
| 264 | ctype = GRN_CHAR_KATAKANA; |
| 265 | break; |
| 266 | case 6: |
| 267 | case 7: |
| 268 | case 8: |
| 269 | *d++ = c1; |
| 270 | *d = c2; |
| 271 | ctype = GRN_CHAR_SYMBOL; |
| 272 | break; |
| 273 | default: |
| 274 | *d++ = c1; |
| 275 | *d = c2; |
| 276 | ctype = GRN_CHAR_OTHERS; |
| 277 | break; |
| 278 | } |
| 279 | break; |
| 280 | default: |
| 281 | *d++ = c1; |
| 282 | *d = c2; |
| 283 | ctype = GRN_CHAR_KANJI; |
| 284 | break; |
| 285 | } |
| 286 | } else { |
| 287 | /* skip invalid character */ |
| 288 | continue; |
| 289 | } |
| 290 | } else { |
| 291 | unsigned char c = *s; |
| 292 | switch (c >> 4) { |
| 293 | case 0: |
| 294 | case 1: |
| 295 | /* skip unprintable ascii */ |
| 296 | if (cp > ctypes) { |
| 297 | *(cp - 1) |= GRN_CHAR_BLANK; |
| 298 | } |
| 299 | continue; |
| 300 | case 2: |
| 301 | if (c == 0x20) { |
| 302 | if (removeblankp) { |
| 303 | if (cp > ctypes) { |
| 304 | *(cp - 1) |= GRN_CHAR_BLANK; |
| 305 | } |
| 306 | continue; |
| 307 | } else { |
| 308 | *d = ' '; |
| 309 | ctype = GRN_CHAR_BLANK | GRN_CHAR_SYMBOL; |
| 310 | } |
| 311 | } else { |
| 312 | *d = c; |
| 313 | ctype = GRN_CHAR_SYMBOL; |
| 314 | } |
| 315 | break; |
| 316 | case 3: |
| 317 | *d = c; |
| 318 | ctype = (c <= 0x39) ? GRN_CHAR_DIGIT : GRN_CHAR_SYMBOL; |
| 319 | break; |
| 320 | case 4: |
| 321 | *d = ('A' <= c) ? c + 0x20 : c; |
| 322 | ctype = (c == 0x40) ? GRN_CHAR_SYMBOL : GRN_CHAR_ALPHA; |
| 323 | break; |
| 324 | case 5: |
| 325 | *d = (c <= 'Z') ? c + 0x20 : c; |
| 326 | ctype = (c <= 0x5a) ? GRN_CHAR_ALPHA : GRN_CHAR_SYMBOL; |
| 327 | break; |
| 328 | case 6: |
| 329 | *d = c; |
| 330 | ctype = (c == 0x60) ? GRN_CHAR_SYMBOL : GRN_CHAR_ALPHA; |
| 331 | break; |
| 332 | case 7: |
| 333 | *d = c; |
| 334 | ctype = (c <= 0x7a) ? GRN_CHAR_ALPHA |
| 335 | : (c == 0x7f ? GRN_CHAR_OTHERS : GRN_CHAR_SYMBOL); |
| 336 | break; |
| 337 | default: |
| 338 | *d = c; |
| 339 | ctype = GRN_CHAR_OTHERS; |
| 340 | break; |
| 341 | } |
| 342 | } |
| 343 | d++; |
| 344 | length++; |
| 345 | if (cp) { |
| 346 | *cp++ = ctype; |
| 347 | } |
| 348 | if (ch) { |
| 349 | *ch++ = (int16_t)(s + 1 - s_); |
| 350 | s_ = s + 1; |
| 351 | while (++d_ < d) { |
| 352 | *ch++ = 0; |
| 353 | } |
| 354 | } |
| 355 | } |
| 356 | if (cp) { |
| 357 | *cp = GRN_CHAR_NULL; |
| 358 | } |
| 359 | *d = '\0'; |
| 360 | nstr->n_characters = (unsigned int)length; |
| 361 | nstr->normalized_length_in_bytes = |
| 362 | (unsigned int)(d - (unsigned char *)nstr->normalized); |
| 363 | return NULL; |
| 364 | } |
| 365 | |
| 366 | static inline grn_obj * |
| 367 | sjis_normalize(grn_ctx *ctx, grn_string *nstr) |
| 368 | { |
| 369 | static uint16_t hankana[] = { |
| 370 | 0x8140, 0x8142, 0x8175, 0x8176, 0x8141, 0x8145, 0x8392, 0x8340, |
| 371 | 0x8342, 0x8344, 0x8346, 0x8348, 0x8383, 0x8385, 0x8387, 0x8362, |
| 372 | 0x815b, 0x8341, 0x8343, 0x8345, 0x8347, 0x8349, 0x834a, 0x834c, |
| 373 | 0x834e, 0x8350, 0x8352, 0x8354, 0x8356, 0x8358, 0x835a, 0x835c, |
| 374 | 0x835e, 0x8360, 0x8363, 0x8365, 0x8367, 0x8369, 0x836a, 0x836b, |
| 375 | 0x836c, 0x836d, 0x836e, 0x8371, 0x8374, 0x8377, 0x837a, 0x837d, |
| 376 | 0x837e, 0x8380, 0x8381, 0x8382, 0x8384, 0x8386, 0x8388, 0x8389, |
| 377 | 0x838a, 0x838b, 0x838c, 0x838d, 0x838f, 0x8393, 0x814a, 0x814b}; |
| 378 | static unsigned char dakuten[] = { |
| 379 | 0x94, 0, 0, 0, 0, 0x4b, 0, 0x4d, 0, 0x4f, 0, |
| 380 | 0x51, 0, 0x53, 0, 0x55, 0, 0x57, 0, 0x59, 0, 0x5b, |
| 381 | 0, 0x5d, 0, 0x5f, 0, 0x61, 0, 0, 0x64, 0, 0x66, |
| 382 | 0, 0x68, 0, 0, 0, 0, 0, 0, 0x6f, 0, 0, |
| 383 | 0x72, 0, 0, 0x75, 0, 0, 0x78, 0, 0, 0x7b}; |
| 384 | static unsigned char handaku[] = |
| 385 | {0x70, 0, 0, 0x73, 0, 0, 0x76, 0, 0, 0x79, 0, 0, 0x7c}; |
| 386 | int16_t *ch; |
| 387 | const unsigned char *s, *s_; |
| 388 | unsigned char *d, *d0, *d_, b, *e; |
| 389 | uint_least8_t *cp, *ctypes, ctype; |
| 390 | size_t size = nstr->original_length_in_bytes, length = 0; |
| 391 | int removeblankp = nstr->flags & GRN_STRING_REMOVE_BLANK; |
| 392 | if (!(nstr->normalized = GRN_MALLOC(size * 2 + 1))) { |
| 393 | ERR(GRN_NO_MEMORY_AVAILABLE, |
| 394 | "[string][sjis] failed to allocate normalized text space"); |
| 395 | return NULL; |
| 396 | } |
| 397 | d0 = (unsigned char *)nstr->normalized; |
| 398 | if (nstr->flags & GRN_STRING_WITH_CHECKS) { |
| 399 | if (!(nstr->checks = GRN_MALLOC((size * 2 + 1) * sizeof(int16_t)))) { |
| 400 | GRN_FREE(nstr->normalized); |
| 401 | nstr->normalized = NULL; |
| 402 | ERR(GRN_NO_MEMORY_AVAILABLE, |
| 403 | "[string][sjis] failed to allocate checks space"); |
| 404 | return NULL; |
| 405 | } |
| 406 | } |
| 407 | ch = nstr->checks; |
| 408 | if (nstr->flags & GRN_STRING_WITH_TYPES) { |
| 409 | if (!(nstr->ctypes = GRN_MALLOC(size + 1))) { |
| 410 | GRN_FREE(nstr->checks); |
| 411 | GRN_FREE(nstr->normalized); |
| 412 | nstr->checks = NULL; |
| 413 | nstr->normalized = NULL; |
| 414 | ERR(GRN_NO_MEMORY_AVAILABLE, |
| 415 | "[string][sjis] failed to allocate character types space"); |
| 416 | return NULL; |
| 417 | } |
| 418 | } |
| 419 | cp = ctypes = nstr->ctypes; |
| 420 | e = (unsigned char *)nstr->original + size; |
| 421 | for (s = s_ = (unsigned char *)nstr->original, d = d_ = d0; s < e; s++) { |
| 422 | if ((*s & 0x80)) { |
| 423 | if (0xa0 <= *s && *s <= 0xdf) { |
| 424 | uint16_t c = hankana[*s - 0xa0]; |
| 425 | switch (c) { |
| 426 | case 0x814a: |
| 427 | if (d > d0 + 1 && d[-2] == 0x83 && 0x45 <= d[-1] && d[-1] <= 0x7a && |
| 428 | (b = dakuten[d[-1] - 0x45])) { |
| 429 | *(d - 1) = b; |
| 430 | if (ch) { |
| 431 | ch[-1]++; |
| 432 | s_++; |
| 433 | } |
| 434 | continue; |
| 435 | } else { |
| 436 | *d++ = (unsigned char)(c >> 8); |
| 437 | *d = (unsigned char)(c & 0xff); |
| 438 | } |
| 439 | break; |
| 440 | case 0x814b: |
| 441 | if (d > d0 + 1 && d[-2] == 0x83 && 0x6e <= d[-1] && d[-1] <= 0x7a && |
| 442 | (b = handaku[d[-1] - 0x6e])) { |
| 443 | *(d - 1) = b; |
| 444 | if (ch) { |
| 445 | ch[-1]++; |
| 446 | s_++; |
| 447 | } |
| 448 | continue; |
| 449 | } else { |
| 450 | *d++ = (unsigned char)(c >> 8); |
| 451 | *d = (unsigned char)(c & 0xff); |
| 452 | } |
| 453 | break; |
| 454 | default: |
| 455 | *d++ = (unsigned char)(c >> 8); |
| 456 | *d = (unsigned char)(c & 0xff); |
| 457 | break; |
| 458 | } |
| 459 | ctype = GRN_CHAR_KATAKANA; |
| 460 | } else { |
| 461 | if ((s + 1) < e && 0x40 <= *(s + 1) && *(s + 1) <= 0xfc) { |
| 462 | unsigned char c1 = *s++, c2 = *s, c3 = 0; |
| 463 | if (0x81 <= c1 && c1 <= 0x87) { |
| 464 | switch (c1 & 0x0f) { |
| 465 | case 1: |
| 466 | switch (c2) { |
| 467 | case 0x5b: |
| 468 | *d++ = c1; |
| 469 | *d = c2; |
| 470 | ctype = GRN_CHAR_KATAKANA; |
| 471 | break; |
| 472 | case 0x58: |
| 473 | *d++ = c1; |
| 474 | *d = c2; |
| 475 | ctype = GRN_CHAR_KANJI; |
| 476 | break; |
| 477 | case 0x40: |
| 478 | if (removeblankp) { |
| 479 | if (cp > ctypes) { |
| 480 | *(cp - 1) |= GRN_CHAR_BLANK; |
| 481 | } |
| 482 | continue; |
| 483 | } else { |
| 484 | *d = ' '; |
| 485 | ctype = GRN_CHAR_BLANK | GRN_CHAR_SYMBOL; |
| 486 | } |
| 487 | break; |
| 488 | default: |
| 489 | if (0x43 <= c2 && c2 <= 0x7e && (c3 = symbol[c2 - 0x43])) { |
| 490 | *d = c3; |
| 491 | ctype = GRN_CHAR_SYMBOL; |
| 492 | } else if (0x7f <= c2 && c2 <= 0x97 && |
| 493 | (c3 = symbol[c2 - 0x44])) { |
| 494 | *d = c3; |
| 495 | ctype = GRN_CHAR_SYMBOL; |
| 496 | } else { |
| 497 | *d++ = c1; |
| 498 | *d = c2; |
| 499 | ctype = GRN_CHAR_OTHERS; |
| 500 | } |
| 501 | break; |
| 502 | } |
| 503 | break; |
| 504 | case 2: |
| 505 | c3 = c2 - 0x1f; |
| 506 | if (0x4f <= c2 && c2 <= 0x58) { |
| 507 | ctype = GRN_CHAR_DIGIT; |
| 508 | *d = c2 - 0x1f; |
| 509 | } else if (0x60 <= c2 && c2 <= 0x79) { |
| 510 | ctype = GRN_CHAR_ALPHA; |
| 511 | *d = c2 + 0x01; |
| 512 | } else if (0x81 <= c2 && c2 <= 0x9a) { |
| 513 | ctype = GRN_CHAR_ALPHA; |
| 514 | *d = c2 - 0x20; |
| 515 | } else if (0x9f <= c2 && c2 <= 0xf1) { |
| 516 | *d++ = c1; |
| 517 | *d = c2; |
| 518 | ctype = GRN_CHAR_HIRAGANA; |
| 519 | } else { |
| 520 | *d++ = c1; |
| 521 | *d = c2; |
| 522 | ctype = GRN_CHAR_OTHERS; |
| 523 | } |
| 524 | break; |
| 525 | case 3: |
| 526 | if (0x40 <= c2 && c2 <= 0x96) { |
| 527 | *d++ = c1; |
| 528 | *d = c2; |
| 529 | ctype = GRN_CHAR_KATAKANA; |
| 530 | } else { |
| 531 | *d++ = c1; |
| 532 | *d = c2; |
| 533 | ctype = GRN_CHAR_SYMBOL; |
| 534 | } |
| 535 | break; |
| 536 | case 4: |
| 537 | case 7: |
| 538 | *d++ = c1; |
| 539 | *d = c2; |
| 540 | ctype = GRN_CHAR_SYMBOL; |
| 541 | break; |
| 542 | default: |
| 543 | *d++ = c1; |
| 544 | *d = c2; |
| 545 | ctype = GRN_CHAR_OTHERS; |
| 546 | break; |
| 547 | } |
| 548 | } else { |
| 549 | *d++ = c1; |
| 550 | *d = c2; |
| 551 | ctype = GRN_CHAR_KANJI; |
| 552 | } |
| 553 | } else { |
| 554 | /* skip invalid character */ |
| 555 | continue; |
| 556 | } |
| 557 | } |
| 558 | } else { |
| 559 | unsigned char c = *s; |
| 560 | switch (c >> 4) { |
| 561 | case 0: |
| 562 | case 1: |
| 563 | /* skip unprintable ascii */ |
| 564 | if (cp > ctypes) { |
| 565 | *(cp - 1) |= GRN_CHAR_BLANK; |
| 566 | } |
| 567 | continue; |
| 568 | case 2: |
| 569 | if (c == 0x20) { |
| 570 | if (removeblankp) { |
| 571 | if (cp > ctypes) { |
| 572 | *(cp - 1) |= GRN_CHAR_BLANK; |
| 573 | } |
| 574 | continue; |
| 575 | } else { |
| 576 | *d = ' '; |
| 577 | ctype = GRN_CHAR_BLANK | GRN_CHAR_SYMBOL; |
| 578 | } |
| 579 | } else { |
| 580 | *d = c; |
| 581 | ctype = GRN_CHAR_SYMBOL; |
| 582 | } |
| 583 | break; |
| 584 | case 3: |
| 585 | *d = c; |
| 586 | ctype = (c <= 0x39) ? GRN_CHAR_DIGIT : GRN_CHAR_SYMBOL; |
| 587 | break; |
| 588 | case 4: |
| 589 | *d = ('A' <= c) ? c + 0x20 : c; |
| 590 | ctype = (c == 0x40) ? GRN_CHAR_SYMBOL : GRN_CHAR_ALPHA; |
| 591 | break; |
| 592 | case 5: |
| 593 | *d = (c <= 'Z') ? c + 0x20 : c; |
| 594 | ctype = (c <= 0x5a) ? GRN_CHAR_ALPHA : GRN_CHAR_SYMBOL; |
| 595 | break; |
| 596 | case 6: |
| 597 | *d = c; |
| 598 | ctype = (c == 0x60) ? GRN_CHAR_SYMBOL : GRN_CHAR_ALPHA; |
| 599 | break; |
| 600 | case 7: |
| 601 | *d = c; |
| 602 | ctype = (c <= 0x7a) ? GRN_CHAR_ALPHA |
| 603 | : (c == 0x7f ? GRN_CHAR_OTHERS : GRN_CHAR_SYMBOL); |
| 604 | break; |
| 605 | default: |
| 606 | *d = c; |
| 607 | ctype = GRN_CHAR_OTHERS; |
| 608 | break; |
| 609 | } |
| 610 | } |
| 611 | d++; |
| 612 | length++; |
| 613 | if (cp) { |
| 614 | *cp++ = ctype; |
| 615 | } |
| 616 | if (ch) { |
| 617 | *ch++ = (int16_t)(s + 1 - s_); |
| 618 | s_ = s + 1; |
| 619 | while (++d_ < d) { |
| 620 | *ch++ = 0; |
| 621 | } |
| 622 | } |
| 623 | } |
| 624 | if (cp) { |
| 625 | *cp = GRN_CHAR_NULL; |
| 626 | } |
| 627 | *d = '\0'; |
| 628 | nstr->n_characters = (unsigned int)length; |
| 629 | nstr->normalized_length_in_bytes = |
| 630 | (unsigned int)(d - (unsigned char *)nstr->normalized); |
| 631 | return NULL; |
| 632 | } |
| 633 | |
| 634 | #ifdef GRN_WITH_NFKC |
| 635 | typedef struct { |
| 636 | size_t size; |
| 637 | |
| 638 | unsigned char *dest; |
| 639 | unsigned char *dest_end; |
| 640 | unsigned char *d; |
| 641 | unsigned char *d_; /* -1 */ |
| 642 | size_t n_characters; |
| 643 | |
| 644 | int16_t *checks; |
| 645 | int16_t *c; |
| 646 | |
| 647 | uint8_t *types; |
| 648 | uint8_t *t; |
| 649 | |
| 650 | uint64_t *offsets; |
| 651 | uint64_t *o; |
| 652 | } grn_nfkc_normalize_context; |
| 653 | |
| 654 | typedef struct { |
| 655 | grn_string *string; |
| 656 | grn_nfkc_normalize_options *options; |
| 657 | grn_nfkc_normalize_context context; |
| 658 | bool remove_blank_p; |
| 659 | bool remove_tokenized_delimiter_p; |
| 660 | } grn_nfkc_normalize_data; |
| 661 | |
| 662 | static inline void |
| 663 | grn_nfkc_normalize_context_init(grn_ctx *ctx, |
| 664 | grn_nfkc_normalize_context *context, |
| 665 | bool need_checks, |
| 666 | bool need_types, |
| 667 | bool need_offsets, |
| 668 | const char *context_tag) |
| 669 | { |
| 670 | if (!(context->dest = GRN_MALLOC(context->size + 1))) { |
| 671 | ERR(GRN_NO_MEMORY_AVAILABLE, |
| 672 | "[normalize][nfkc] failed to allocate normalized text space"); |
| 673 | return; |
| 674 | } |
| 675 | context->dest_end = context->dest + context->size; |
| 676 | context->d = context->dest; |
| 677 | context->d_ = NULL; |
| 678 | |
| 679 | if (need_checks) { |
| 680 | if (!(context->checks = |
| 681 | GRN_MALLOC(sizeof(int16_t) * (context->size + 1)))) { |
| 682 | ERR(GRN_NO_MEMORY_AVAILABLE, |
| 683 | "[normalize][nfkc] failed to allocate checks space"); |
| 684 | return; |
| 685 | } |
| 686 | context->checks[0] = 0; |
| 687 | } |
| 688 | context->c = context->checks; |
| 689 | |
| 690 | if (need_types) { |
| 691 | if (!(context->types = GRN_MALLOC(sizeof(uint8_t) * (context->size + 1)))) { |
| 692 | ERR(GRN_NO_MEMORY_AVAILABLE, |
| 693 | "[normalize][nfkc] failed to allocate character types space"); |
| 694 | return; |
| 695 | } |
| 696 | } |
| 697 | context->t = context->types; |
| 698 | |
| 699 | if (need_offsets) { |
| 700 | if (!(context->offsets = |
| 701 | GRN_MALLOC(sizeof(uint64_t) * (context->size + 1)))) { |
| 702 | ERR(GRN_NO_MEMORY_AVAILABLE, |
| 703 | "[normalize][nfkc] failed to allocate offsets space"); |
| 704 | return; |
| 705 | } |
| 706 | } |
| 707 | context->o = context->offsets; |
| 708 | } |
| 709 | |
| 710 | static inline void |
| 711 | grn_nfkc_normalize_context_fin(grn_ctx *ctx, |
| 712 | grn_nfkc_normalize_context *context) |
| 713 | { |
| 714 | if (context->dest) { |
| 715 | GRN_FREE(context->dest); |
| 716 | } |
| 717 | if (context->checks) { |
| 718 | GRN_FREE(context->checks); |
| 719 | } |
| 720 | if (context->types) { |
| 721 | GRN_FREE(context->types); |
| 722 | } |
| 723 | if (context->offsets) { |
| 724 | GRN_FREE(context->offsets); |
| 725 | } |
| 726 | } |
| 727 | |
| 728 | static inline void |
| 729 | grn_nfkc_normalize_context_swap(grn_ctx *ctx, |
| 730 | grn_nfkc_normalize_context *context1, |
| 731 | grn_nfkc_normalize_context *context2) |
| 732 | { |
| 733 | grn_nfkc_normalize_context tmp; |
| 734 | tmp = *context1; |
| 735 | *context1 = *context2; |
| 736 | *context2 = tmp; |
| 737 | } |
| 738 | |
| 739 | static inline void |
| 740 | grn_nfkc_normalize_context_rewind(grn_ctx *ctx, |
| 741 | grn_nfkc_normalize_context *context) |
| 742 | { |
| 743 | context->d = context->dest; |
| 744 | context->d_ = NULL; |
| 745 | context->n_characters = 0; |
| 746 | context->c = context->checks; |
| 747 | context->t = context->types; |
| 748 | context->o = context->offsets; |
| 749 | } |
| 750 | |
| 751 | static inline void |
| 752 | grn_nfkc_normalize_data_init(grn_ctx *ctx, |
| 753 | grn_nfkc_normalize_data *data, |
| 754 | grn_obj *string, |
| 755 | grn_nfkc_normalize_options *options) |
| 756 | { |
| 757 | size_t size; |
| 758 | |
| 759 | memset(data, 0, sizeof(grn_nfkc_normalize_data)); |
| 760 | data->string = (grn_string *)string; |
| 761 | data->options = options; |
| 762 | if (data->options->remove_blank_force_is_set) { |
| 763 | data->remove_blank_p = data->options->remove_blank_force; |
| 764 | } else { |
| 765 | data->remove_blank_p = (data->string->flags & GRN_STRING_REMOVE_BLANK) || |
| 766 | data->options->remove_blank; |
| 767 | } |
| 768 | data->remove_tokenized_delimiter_p = |
| 769 | (data->string->flags & GRN_STRING_REMOVE_TOKENIZED_DELIMITER); |
| 770 | |
| 771 | size = data->string->original_length_in_bytes; |
| 772 | data->context.size = size * 3; |
| 773 | |
| 774 | grn_nfkc_normalize_context_init(ctx, |
| 775 | &(data->context), |
| 776 | data->string->flags & GRN_STRING_WITH_CHECKS, |
| 777 | data->string->flags & GRN_STRING_WITH_TYPES, |
| 778 | data->options->report_source_offset, |
| 779 | ""); |
| 780 | } |
| 781 | |
| 782 | static inline void |
| 783 | grn_nfkc_normalize_context_expand(grn_ctx *ctx, |
| 784 | grn_nfkc_normalize_context *context, |
| 785 | size_t least_required_size, |
| 786 | const char *context_tag) |
| 787 | { |
| 788 | unsigned char *dest; |
| 789 | context->size += (context->size >> 1) + least_required_size; |
| 790 | dest = GRN_REALLOC(context->dest, context->size + 1); |
| 791 | if (!dest) { |
| 792 | ERR(GRN_NO_MEMORY_AVAILABLE, |
| 793 | "[normalize][nfkc]%s failed to expand destination text space", |
| 794 | context_tag); |
| 795 | return; |
| 796 | } |
| 797 | context->dest_end = dest + context->size; |
| 798 | context->d = dest + (context->d - context->dest); |
| 799 | context->dest = dest; |
| 800 | if (context->c) { |
| 801 | int16_t *checks; |
| 802 | if (!(checks = GRN_REALLOC(context->checks, |
| 803 | sizeof(int16_t) * (context->size + 1)))) { |
| 804 | ERR(GRN_NO_MEMORY_AVAILABLE, |
| 805 | "[normalize][nfkc]%s failed to expand checks space", |
| 806 | context_tag); |
| 807 | return; |
| 808 | } |
| 809 | context->c = checks + (context->c - context->checks); |
| 810 | context->checks = checks; |
| 811 | } |
| 812 | if (context->t) { |
| 813 | uint8_t *types; |
| 814 | if (!(types = GRN_REALLOC(context->types, |
| 815 | sizeof(uint8_t) * (context->size + 1)))) { |
| 816 | ERR(GRN_NO_MEMORY_AVAILABLE, |
| 817 | "[normalize][nfkc]%s failed to expand character types space", |
| 818 | context_tag); |
| 819 | return; |
| 820 | } |
| 821 | context->t = types + (context->t - context->types); |
| 822 | context->types = types; |
| 823 | } |
| 824 | if (context->o) { |
| 825 | uint64_t *offsets; |
| 826 | if (!(offsets = GRN_REALLOC(context->offsets, |
| 827 | sizeof(uint64_t) * (context->size + 1)))) { |
| 828 | ERR(GRN_NO_MEMORY_AVAILABLE, |
| 829 | "[normalize][nfkc]%s failed to expand offsets space", |
| 830 | context_tag); |
| 831 | return; |
| 832 | } |
| 833 | context->o = offsets + (context->o - context->offsets); |
| 834 | context->offsets = offsets; |
| 835 | } |
| 836 | } |
| 837 | |
| 838 | static inline void |
| 839 | grn_nfkc_normalize_expand(grn_ctx *ctx, |
| 840 | grn_nfkc_normalize_data *data, |
| 841 | size_t least_required_size) |
| 842 | { |
| 843 | grn_nfkc_normalize_context_expand(ctx, |
| 844 | &(data->context), |
| 845 | least_required_size, |
| 846 | ""); |
| 847 | } |
| 848 | |
| 849 | static inline bool |
| 850 | grn_nfkc_normalize_unify_latin_alphabet_with_is_a( |
| 851 | const unsigned char *utf8_char) |
| 852 | { |
| 853 | /* |
| 854 | * Latin-1 Supplement |
| 855 | * U+00E0 LATIN SMALL LETTER A WITH GRAVE .. |
| 856 | * U+00E5 LATIN SMALL LETTER A WITH RING ABOVE |
| 857 | */ |
| 858 | return (utf8_char[0] == 0xc3 && 0xa0 <= utf8_char[1] && |
| 859 | utf8_char[1] <= 0xa5) || |
| 860 | /* |
| 861 | * Latin Extended-A |
| 862 | * U+0101 LATIN SMALL LETTER A WITH MACRON |
| 863 | * U+0103 LATIN SMALL LETTER A WITH BREVE |
| 864 | * U+0105 LATIN SMALL LETTER A WITH OGONEK |
| 865 | * Uppercase counterparts (U+0102 and U+0104) are covered by the |
| 866 | * following condition but they are never appeared here. Because NFKC |
| 867 | * normalization converts them to their lowercase equivalents. |
| 868 | */ |
| 869 | (utf8_char[0] == 0xc4 && |
| 870 | (0x81 <= utf8_char[1] && utf8_char[1] <= 0x85)) || |
| 871 | /* |
| 872 | * Latin Extended-B |
| 873 | * U+01CE LATIN SMALL LETTER A WITH CARON |
| 874 | * U+01DF LATIN SMALL LETTER A WITH DIAERESIS AND MACRON |
| 875 | * U+01E1 LATIN SMALL LETTER A WITH DOT ABOVE AND MACRON |
| 876 | * U+01FB LATIN SMALL LETTER A WITH RING ABOVE AND ACUTE |
| 877 | */ |
| 878 | (utf8_char[0] == 0xc7 && |
| 879 | (utf8_char[1] == 0x8e || utf8_char[1] == 0x9f || |
| 880 | utf8_char[1] == 0xa1 || utf8_char[1] == 0xbb)) || |
| 881 | /* |
| 882 | * Latin Extended-B |
| 883 | * U+0201 LATIN SMALL LETTER A WITH DOUBLE GRAVE |
| 884 | * U+0203 LATIN SMALL LETTER A WITH INVERTED BREVE |
| 885 | * U+0227 LATIN SMALL LETTER A WITH DOT ABOVE |
| 886 | */ |
| 887 | (utf8_char[0] == 0xc8 && |
| 888 | (utf8_char[1] == 0x81 || utf8_char[1] == 0x83 || |
| 889 | utf8_char[1] == 0xa7)) || |
| 890 | /* |
| 891 | * Latin Extended Additional |
| 892 | * U+1E01 LATIN SMALL LETTER A WITH RING BELOW |
| 893 | */ |
| 894 | (utf8_char[0] == 0xe1 && utf8_char[1] == 0xb8 && |
| 895 | utf8_char[2] == 0x81) || |
| 896 | /* |
| 897 | * Latin Extended Additional |
| 898 | * U+1EA1 LATIN SMALL LETTER A WITH DOT BELOW |
| 899 | * U+1EA3 LATIN SMALL LETTER A WITH HOOK ABOVE |
| 900 | * U+1EA5 LATIN SMALL LETTER A WITH CIRCUMFLEX |
| 901 | * U+1EA7 LATIN SMALL LETTER A WITH CIRCUMFLEX AND GRAVE |
| 902 | * U+1EA9 LATIN SMALL LETTER A WITH CIRCUMFLEX AND HOOK ABOVE |
| 903 | * U+1EAB LATIN SMALL LETTER A WITH CIRCUMFLEX AND TILDE |
| 904 | * U+1EAD LATIN SMALL LETTER A WITH CIRCUMFLEX AND DOT BELOW |
| 905 | * U+1EAF LATIN SMALL LETTER A WITH BREVE AND ACUTE |
| 906 | * U+1EB1 LATIN SMALL LETTER A WITH BREVE AND GRAVE |
| 907 | * U+1EB3 LATIN SMALL LETTER A WITH BREVE AND HOOK ABOVE |
| 908 | * U+1EB5 LATIN SMALL LETTER A WITH BREVE AND TILDE |
| 909 | * U+1EB7 LATIN SMALL LETTER A WITH BREVE AND DOT BELOW |
| 910 | * Uppercase counterparts (U+1EA2, U+1EA4, U+1EA6, U+1EA8, U+1EAA, |
| 911 | * U+1EAC, U+1EAE, U+1EB0, U+1EB2, U+1EB4, and U+1EB6) are covered by |
| 912 | * the following condition but they are never appeared here. Because |
| 913 | * NFKC normalization converts them to their lowercase equivalents. |
| 914 | */ |
| 915 | (utf8_char[0] == 0xe1 && utf8_char[1] == 0xba && |
| 916 | (0xa1 <= utf8_char[2] && utf8_char[2] <= 0xb7)); |
| 917 | } |
| 918 | |
| 919 | static inline bool |
| 920 | grn_nfkc_normalize_unify_latin_alphabet_with_is_b( |
| 921 | const unsigned char *utf8_char) |
| 922 | { |
| 923 | /* |
| 924 | * Latin Extended Additional |
| 925 | * U+1E03 LATIN SMALL LETTER B WITH DOT ABOVE |
| 926 | * U+1E05 LATIN SMALL LETTER B WITH DOT BELOW |
| 927 | * U+1E07 LATIN SMALL LETTER B WITH LINE BELOW |
| 928 | * Uppercase counterparts (U+1E04, U+1E06) are covered by the |
| 929 | * following condition but they are never appeared here. Because NFKC |
| 930 | * normalization converts them to their lowercase equivalents. |
| 931 | */ |
| 932 | return utf8_char[0] == 0xe1 && utf8_char[1] == 0xb8 && |
| 933 | (0x83 <= utf8_char[2] && utf8_char[2] <= 0x87); |
| 934 | } |
| 935 | |
| 936 | static inline bool |
| 937 | grn_nfkc_normalize_unify_latin_alphabet_with_is_c( |
| 938 | const unsigned char *utf8_char) |
| 939 | { |
| 940 | return ( |
| 941 | /* |
| 942 | * Latin-1 Supplement |
| 943 | * U+00E7 LATIN SMALL LETTER C WITH CEDILLA |
| 944 | */ |
| 945 | (utf8_char[0] == 0xc3 && utf8_char[1] == 0xa7) || |
| 946 | /* |
| 947 | * Latin Extended-A |
| 948 | * U+0107 LATIN SMALL LETTER C WITH ACUTE |
| 949 | * U+0109 LATIN SMALL LETTER C WITH CIRCUMFLEX |
| 950 | * U+010B LATIN SMALL LETTER C WITH DOT ABOVE |
| 951 | * U+010D LATIN SMALL LETTER C WITH CARON |
| 952 | * Uppercase counterparts (U+0108, U+010A, U+010C) are covered by the |
| 953 | * following condition but they are never appeared here. Because NFKC |
| 954 | * normalization converts them to their lowercase equivalents. |
| 955 | */ |
| 956 | (utf8_char[0] == 0xc4 && 0x87 <= utf8_char[1] && utf8_char[1] <= 0x8d) || |
| 957 | /* |
| 958 | * Latin Extended Additional |
| 959 | * U+1E09 LATIN SMALL LETTER C WITH CEDILLA AND ACUTE |
| 960 | */ |
| 961 | (utf8_char[0] == 0xe1 && utf8_char[1] == 0xb8 && utf8_char[2] == 0x89)); |
| 962 | } |
| 963 | |
| 964 | static inline bool |
| 965 | grn_nfkc_normalize_unify_latin_alphabet_with_is_d( |
| 966 | const unsigned char *utf8_char) |
| 967 | { |
| 968 | return ( |
| 969 | /* |
| 970 | * Latin Extended-A |
| 971 | * U+010F LATIN SMALL LETTER D WITH CARON |
| 972 | */ |
| 973 | (utf8_char[0] == 0xc4 && utf8_char[1] == 0x8f) || |
| 974 | /* |
| 975 | * Latin Extended Additional |
| 976 | * U+1E0B LATIN SMALL LETTER D WITH DOT ABOVE |
| 977 | * U+1E0D LATIN SMALL LETTER D WITH DOT BELOW |
| 978 | * U+1E0F LATIN SMALL LETTER D WITH LINE BELOW |
| 979 | * U+1E11 LATIN SMALL LETTER D WITH CEDILLA |
| 980 | * U+1E13 LATIN SMALL LETTER D WITH CIRCUMFLEX BELOW |
| 981 | * Uppercase counterparts (U+1E0C, U+1E0E, U+1E10, U+1E12, U+1E14) are |
| 982 | * covered by the following condition but they are never appeared here. |
| 983 | * Because NFKC normalization converts them to their lowercase equivalents. |
| 984 | */ |
| 985 | (utf8_char[0] == 0xe1 && utf8_char[1] == 0xb8 && |
| 986 | (0x8b <= utf8_char[2] && utf8_char[2] <= 0x93))); |
| 987 | } |
| 988 | |
| 989 | static inline bool |
| 990 | grn_nfkc_normalize_unify_latin_alphabet_with_is_e( |
| 991 | const unsigned char *utf8_char) |
| 992 | { |
| 993 | return ( |
| 994 | /* |
| 995 | * Latin-1 Supplement |
| 996 | * U+00E8 LATIN SMALL LETTER E WITH GRAVE |
| 997 | * U+00E9 LATIN SMALL LETTER E WITH ACUTE |
| 998 | * U+00EA LATIN SMALL LETTER E WITH CIRCUMFLEX |
| 999 | * U+00EB LATIN SMALL LETTER E WITH DIAERESIS |
| 1000 | */ |
| 1001 | (utf8_char[0] == 0xc3 && 0xa8 <= utf8_char[1] && utf8_char[1] <= 0xab) || |
| 1002 | /* |
| 1003 | * Latin Extended-A |
| 1004 | * U+0113 LATIN SMALL LETTER E WITH MACRON |
| 1005 | * U+0115 LATIN SMALL LETTER E WITH BREVE |
| 1006 | * U+0117 LATIN SMALL LETTER E WITH DOT ABOVE |
| 1007 | * U+0119 LATIN SMALL LETTER E WITH OGONEK |
| 1008 | * U+011B LATIN SMALL LETTER E WITH CARON |
| 1009 | * Uppercase counterparts (U+0114, U+0116, U+0118, U+011A) are covered |
| 1010 | * by the following condition but they are never appeared here. Because |
| 1011 | * NFKC normalization converts them to their lowercase equivalents. |
| 1012 | */ |
| 1013 | (utf8_char[0] == 0xc4 && 0x93 <= utf8_char[1] && utf8_char[1] <= 0x9b) || |
| 1014 | /* |
| 1015 | * Latin Extended-B |
| 1016 | * U+0205 LATIN SMALL LETTER E WITH DOUBLE GRAVE |
| 1017 | * U+0207 LATIN SMALL LETTER E WITH INVERTED BREVE |
| 1018 | * U+0229 LATIN SMALL LETTER E WITH CEDILLA |
| 1019 | * Uppercase counterparts (U+0206) are covered by the following condition |
| 1020 | * but they are never appeared here. Because NFKC normalization converts |
| 1021 | * them to their lowercase equivalents. |
| 1022 | */ |
| 1023 | (utf8_char[0] == 0xc8 && ((0x85 <= utf8_char[1] && utf8_char[1] <= 0x87) || |
| 1024 | utf8_char[1] == 0xa9)) || |
| 1025 | /* |
| 1026 | * Latin Extended Additional |
| 1027 | * U+1E15 LATIN SMALL LETTER E WITH MACRON AND GRAVE |
| 1028 | * U+1E17 LATIN SMALL LETTER E WITH MACRON AND ACUTE |
| 1029 | * U+1E19 LATIN SMALL LETTER E WITH CIRCUMFLEX BELOW |
| 1030 | * U+1E1B LATIN SMALL LETTER E WITH TILDE BELOW |
| 1031 | * U+1E1D LATIN SMALL LETTER E WITH CEDILLA AND BREVE |
| 1032 | * |
| 1033 | * U+1EB9 LATIN SMALL LETTER E WITH DOT BELOW |
| 1034 | * U+1EBB LATIN SMALL LETTER E WITH HOOK ABOVE |
| 1035 | * U+1EBD LATIN SMALL LETTER E WITH TILDE |
| 1036 | * U+1EBF LATIN SMALL LETTER E WITH CIRCUMFLEX AND ACUTE |
| 1037 | * |
| 1038 | * U+1EC1 LATIN SMALL LETTER E WITH CIRCUMFLEX AND GRAVE |
| 1039 | * U+1EC3 LATIN SMALL LETTER E WITH CIRCUMFLEX AND HOOK ABOVE |
| 1040 | * U+1EC5 LATIN SMALL LETTER E WITH CIRCUMFLEX AND TILDE |
| 1041 | * U+1EC7 LATIN SMALL LETTER E WITH CIRCUMFLEX AND DOT BELOW |
| 1042 | * |
| 1043 | * Each missing one is an upper case. |
| 1044 | */ |
| 1045 | (utf8_char[0] == 0xe1 && utf8_char[1] == 0xb8 && |
| 1046 | (0x95 <= utf8_char[2] && utf8_char[2] <= 0x9d)) || |
| 1047 | (utf8_char[0] == 0xe1 && utf8_char[1] == 0xba && |
| 1048 | (0xb9 <= utf8_char[2] && utf8_char[2] <= 0xbf)) || |
| 1049 | (utf8_char[0] == 0xe1 && utf8_char[1] == 0xbb && |
| 1050 | (0x81 <= utf8_char[2] && utf8_char[2] <= 0x87))); |
| 1051 | } |
| 1052 | |
| 1053 | static inline bool |
| 1054 | grn_nfkc_normalize_unify_latin_alphabet_with_is_f( |
| 1055 | const unsigned char *utf8_char) |
| 1056 | { |
| 1057 | return ( |
| 1058 | /* |
| 1059 | * Latin Extended Additional |
| 1060 | * U+1E1F LATIN SMALL LETTER F WITH DOT ABOVE |
| 1061 | */ |
| 1062 | utf8_char[0] == 0xe1 && utf8_char[1] == 0xb8 && utf8_char[2] == 0x9f); |
| 1063 | } |
| 1064 | |
| 1065 | static inline bool |
| 1066 | grn_nfkc_normalize_unify_latin_alphabet_with_is_g( |
| 1067 | const unsigned char *utf8_char) |
| 1068 | { |
| 1069 | return ( |
| 1070 | /* |
| 1071 | * Latin Extended-A |
| 1072 | * U+011D LATIN SMALL LETTER G WITH CIRCUMFLEX |
| 1073 | * U+011F LATIN SMALL LETTER G WITH BREVE |
| 1074 | * U+0121 LATIN SMALL LETTER G WITH DOT ABOVE |
| 1075 | * U+0123 LATIN SMALL LETTER G WITH CEDILLA |
| 1076 | * Uppercase counterparts (U+011E, U+0120, U+0122) are covered by the |
| 1077 | * following condition but they are never appeared here. Because NFKC |
| 1078 | * normalization converts them to their lowercase equivalents. |
| 1079 | */ |
| 1080 | (utf8_char[0] == 0xc4 && 0x9d <= utf8_char[1] && utf8_char[1] <= 0xa3) || |
| 1081 | /* |
| 1082 | * Latin Extended-B |
| 1083 | * U+01E7 LATIN SMALL LETTER G WITH CARON |
| 1084 | * U+01F5 LATIN SMALL LETTER G WITH ACUTE |
| 1085 | */ |
| 1086 | (utf8_char[0] == 0xc7 && (utf8_char[1] == 0xa7 || utf8_char[1] == 0xb5)) || |
| 1087 | /* |
| 1088 | * Latin Extended Additional |
| 1089 | * U+1E21 LATIN SMALL LETTER G WITH MACRON |
| 1090 | */ |
| 1091 | (utf8_char[0] == 0xe1 && utf8_char[1] == 0xb8 && utf8_char[2] == 0xa1)); |
| 1092 | } |
| 1093 | |
| 1094 | static inline bool |
| 1095 | grn_nfkc_normalize_unify_latin_alphabet_with_is_h( |
| 1096 | const unsigned char *utf8_char) |
| 1097 | { |
| 1098 | return ( |
| 1099 | /* |
| 1100 | * Latin Extended-A |
| 1101 | * U+0125 LATIN SMALL LETTER H WITH CIRCUMFLEX |
| 1102 | */ |
| 1103 | (utf8_char[0] == 0xc4 && utf8_char[1] == 0xa5) || |
| 1104 | /* |
| 1105 | * Latin Extended-B |
| 1106 | * U+021F LATIN SMALL LETTER H WITH CARON |
| 1107 | */ |
| 1108 | (utf8_char[0] == 0xc8 && utf8_char[1] == 0x9f) || |
| 1109 | /* |
| 1110 | * Latin Extended Additional |
| 1111 | * U+1E23 LATIN SMALL LETTER H WITH DOT ABOVE |
| 1112 | * U+1E25 LATIN SMALL LETTER H WITH DOT BELOW |
| 1113 | * U+1E27 LATIN SMALL LETTER H WITH DIAERESIS |
| 1114 | * U+1E29 LATIN SMALL LETTER H WITH CEDILLA |
| 1115 | * U+1E2B LATIN SMALL LETTER H WITH BREVE BELOW |
| 1116 | * Uppercase counterparts (e.g. U+1E24) are covered by the following |
| 1117 | * condition but they are never appeared here. Because NFKC normalization |
| 1118 | * converts them to their lowercase equivalents. |
| 1119 | * |
| 1120 | * U+1E96 LATIN SMALL LETTER H WITH LINE BELOW |
| 1121 | */ |
| 1122 | (utf8_char[0] == 0xe1 && utf8_char[1] == 0xb8 && |
| 1123 | (0xa3 <= utf8_char[2] && utf8_char[2] <= 0xab)) || |
| 1124 | (utf8_char[0] == 0xe1 && utf8_char[1] == 0xba && utf8_char[2] == 0x96)); |
| 1125 | } |
| 1126 | |
| 1127 | static inline bool |
| 1128 | grn_nfkc_normalize_unify_latin_alphabet_with_is_i( |
| 1129 | const unsigned char *utf8_char) |
| 1130 | { |
| 1131 | return ( |
| 1132 | /* |
| 1133 | * Latin-1 Supplement |
| 1134 | * U+00EC LATIN SMALL LETTER I WITH GRAVE |
| 1135 | * U+00ED LATIN SMALL LETTER I WITH ACUTE |
| 1136 | * U+00EE LATIN SMALL LETTER I WITH CIRCUMFLEX |
| 1137 | * U+00EF LATIN SMALL LETTER I WITH DIAERESIS |
| 1138 | */ |
| 1139 | (utf8_char[0] == 0xc3 && 0xac <= utf8_char[1] && utf8_char[1] <= 0xaf) || |
| 1140 | /* |
| 1141 | * Latin Extended-A |
| 1142 | * U+0129 LATIN SMALL LETTER I WITH TILDE |
| 1143 | * U+012B LATIN SMALL LETTER I WITH MACRON |
| 1144 | * U+012D LATIN SMALL LETTER I WITH BREVE |
| 1145 | * U+012F LATIN SMALL LETTER I WITH OGONEK |
| 1146 | * Uppercase counterparts (e.g. U+012A) are covered by the following |
| 1147 | * condition but they are never appeared here. Because NFKC normalization |
| 1148 | * converts them to their lowercase equivalents. |
| 1149 | */ |
| 1150 | (utf8_char[0] == 0xc4 && 0xa9 <= utf8_char[1] && utf8_char[1] <= 0xaf) || |
| 1151 | /* |
| 1152 | * Latin Extended-B |
| 1153 | * U+01D0 LATIN SMALL LETTER I WITH CARON |
| 1154 | * U+0209 LATIN SMALL LETTER I WITH DOUBLE GRAVE |
| 1155 | * U+020B LATIN SMALL LETTER I WITH INVERTED BREVE |
| 1156 | */ |
| 1157 | (utf8_char[0] == 0xc7 && utf8_char[1] == 0x90) || |
| 1158 | (utf8_char[0] == 0xc8 && (utf8_char[1] == 0x89 || utf8_char[1] == 0x8b)) || |
| 1159 | /* |
| 1160 | * Latin Extended Additional |
| 1161 | * U+1E2D LATIN SMALL LETTER I WITH TILDE BELOW |
| 1162 | * U+1E2F LATIN SMALL LETTER I WITH DIAERESIS AND ACUTE |
| 1163 | * U+1EC9 LATIN SMALL LETTER I WITH HOOK ABOVE |
| 1164 | * U+1ECB LATIN SMALL LETTER I WITH DOT BELOW |
| 1165 | */ |
| 1166 | (utf8_char[0] == 0xe1 && utf8_char[1] == 0xb8 && |
| 1167 | (utf8_char[2] == 0xad || utf8_char[2] == 0xaf)) || |
| 1168 | (utf8_char[0] == 0xe1 && utf8_char[1] == 0xbb && |
| 1169 | (utf8_char[2] == 0x89 || utf8_char[2] == 0x8b))); |
| 1170 | } |
| 1171 | |
| 1172 | static inline bool |
| 1173 | grn_nfkc_normalize_unify_latin_alphabet_with_is_j( |
| 1174 | const unsigned char *utf8_char) |
| 1175 | { |
| 1176 | return ( |
| 1177 | /* |
| 1178 | * Latin Extended-A |
| 1179 | * U+0135 LATIN SMALL LETTER J WITH CIRCUMFLEX |
| 1180 | */ |
| 1181 | (utf8_char[0] == 0xc4 && utf8_char[1] == 0xb5) || |
| 1182 | /* |
| 1183 | * Latin Extended-B |
| 1184 | * U+01F0 LATIN SMALL LETTER J WITH CARON |
| 1185 | */ |
| 1186 | (utf8_char[0] == 0xc7 && utf8_char[1] == 0xb0)); |
| 1187 | } |
| 1188 | |
| 1189 | static inline bool |
| 1190 | grn_nfkc_normalize_unify_latin_alphabet_with_is_k( |
| 1191 | const unsigned char *utf8_char) |
| 1192 | { |
| 1193 | return ( |
| 1194 | /* |
| 1195 | * Latin Extended-A |
| 1196 | * U+0137 LATIN SMALL LETTER K WITH CEDILLA |
| 1197 | */ |
| 1198 | (utf8_char[0] == 0xc4 && utf8_char[1] == 0xb7) || |
| 1199 | /* |
| 1200 | * Latin Extended-B |
| 1201 | * U+01E9 LATIN SMALL LETTER K WITH CARON |
| 1202 | */ |
| 1203 | (utf8_char[0] == 0xc7 && utf8_char[1] == 0xa9) || |
| 1204 | /* |
| 1205 | * Latin Extended Additional |
| 1206 | * U+1E31 LATIN SMALL LETTER K WITH ACUTE |
| 1207 | * U+1E33 LATIN SMALL LETTER K WITH DOT BELOW |
| 1208 | * U+1E35 LATIN SMALL LETTER K WITH LINE BELOW |
| 1209 | * Uppercase counterparts (e.g. U+1E32) are covered by the following |
| 1210 | * condition but they are never appeared here. Because NFKC normalization |
| 1211 | * converts them to their lowercase equivalents. |
| 1212 | */ |
| 1213 | (utf8_char[0] == 0xe1 && utf8_char[1] == 0xb8 && |
| 1214 | (0xb1 <= utf8_char[2] && utf8_char[2] <= 0xb5))); |
| 1215 | } |
| 1216 | |
| 1217 | static inline bool |
| 1218 | grn_nfkc_normalize_unify_latin_alphabet_with_is_l( |
| 1219 | const unsigned char *utf8_char) |
| 1220 | { |
| 1221 | return ( |
| 1222 | /* |
| 1223 | * Latin Extended-A |
| 1224 | * U+013A LATIN SMALL LETTER L WITH ACUTE |
| 1225 | * U+013C LATIN SMALL LETTER L WITH CEDILLA |
| 1226 | * U+013E LATIN SMALL LETTER L WITH CARON |
| 1227 | * Uppercase counterparts (e.g. U+013B) are covered by the following |
| 1228 | * condition but they are never appeared here. Because NFKC normalization |
| 1229 | * converts them to their lowercase equivalents. |
| 1230 | */ |
| 1231 | (utf8_char[0] == 0xc4 && 0xba <= utf8_char[1] && utf8_char[1] <= 0xbe) || |
| 1232 | /* |
| 1233 | * Latin Extended Additional |
| 1234 | * U+1E37 LATIN SMALL LETTER L WITH DOT BELOW |
| 1235 | * U+1E39 LATIN SMALL LETTER L WITH DOT BELOW AND MACRON |
| 1236 | * U+1E3B LATIN SMALL LETTER L WITH LINE BELOW |
| 1237 | * U+1E3D LATIN SMALL LETTER L WITH CIRCUMFLEX BELOW |
| 1238 | * |
| 1239 | * Each missing one is an upper case. |
| 1240 | */ |
| 1241 | (utf8_char[0] == 0xe1 && utf8_char[1] == 0xb8 && |
| 1242 | (0xb7 <= utf8_char[2] && utf8_char[2] <= 0xbd))); |
| 1243 | } |
| 1244 | |
| 1245 | static inline bool |
| 1246 | grn_nfkc_normalize_unify_latin_alphabet_with_is_m( |
| 1247 | const unsigned char *utf8_char) |
| 1248 | { |
| 1249 | return ( |
| 1250 | /* |
| 1251 | * Latin Extended Additional |
| 1252 | * U+1E3F LATIN SMALL LETTER M WITH ACUTE |
| 1253 | * U+1E41 LATIN SMALL LETTER M WITH DOT ABOVE |
| 1254 | * U+1E43 LATIN SMALL LETTER M WITH DOT BELOW |
| 1255 | * Uppercase counterparts (e.g. U+1E42) are covered by the following |
| 1256 | * condition but they are never appeared here. Because NFKC normalization |
| 1257 | * converts them to their lowercase equivalents. |
| 1258 | */ |
| 1259 | (utf8_char[0] == 0xe1 && |
| 1260 | ((utf8_char[1] == 0xb8 && utf8_char[2] == 0xbf) || |
| 1261 | (utf8_char[1] == 0xb9 && 0x81 <= utf8_char[2] && utf8_char[2] <= 0x83)))); |
| 1262 | } |
| 1263 | |
| 1264 | static inline bool |
| 1265 | grn_nfkc_normalize_unify_latin_alphabet_with_is_n( |
| 1266 | const unsigned char *utf8_char) |
| 1267 | { |
| 1268 | return ( |
| 1269 | /* |
| 1270 | * Latin-1 Supplement |
| 1271 | * U+00F1 LATIN SMALL LETTER N WITH TILDE |
| 1272 | */ |
| 1273 | (utf8_char[0] == 0xc3 && utf8_char[1] == 0xb1) || |
| 1274 | /* |
| 1275 | * Latin Extended-A |
| 1276 | * U+0144 LATIN SMALL LETTER N WITH ACUTE |
| 1277 | * U+0146 LATIN SMALL LETTER N WITH CEDILLA |
| 1278 | * U+0148 LATIN SMALL LETTER N WITH CARON |
| 1279 | * Uppercase counterparts (e.g. U+0145) are covered by the following |
| 1280 | * condition but they are never appeared here. Because NFKC normalization |
| 1281 | * converts them to their lowercase equivalents. |
| 1282 | */ |
| 1283 | (utf8_char[0] == 0xc5 && 0x84 <= utf8_char[1] && utf8_char[1] <= 0x88) || |
| 1284 | /* |
| 1285 | * Latin Extended-B |
| 1286 | * U+01F9 LATIN SMALL LETTER N WITH GRAVE |
| 1287 | */ |
| 1288 | (utf8_char[0] == 0xc7 && utf8_char[1] == 0xb9) || |
| 1289 | /* |
| 1290 | * Latin Extended Additional |
| 1291 | * U+1E45 LATIN SMALL LETTER N WITH DOT ABOVE |
| 1292 | * U+1E47 LATIN SMALL LETTER N WITH DOT BELOW |
| 1293 | * U+1E49 LATIN SMALL LETTER N WITH LINE BELOW |
| 1294 | * U+1E4B LATIN SMALL LETTER N WITH CIRCUMFLEX BELOW |
| 1295 | * |
| 1296 | * Each missing one is an upper case. |
| 1297 | */ |
| 1298 | (utf8_char[0] == 0xe1 && utf8_char[1] == 0xb9 && |
| 1299 | (0x85 <= utf8_char[2] && utf8_char[2] <= 0x8b))); |
| 1300 | } |
| 1301 | |
| 1302 | static inline bool |
| 1303 | grn_nfkc_normalize_unify_latin_alphabet_with_is_o( |
| 1304 | const unsigned char *utf8_char) |
| 1305 | { |
| 1306 | return ( |
| 1307 | /* |
| 1308 | * Latin-1 Supplement |
| 1309 | * U+00F2 LATIN SMALL LETTER O WITH GRAVE |
| 1310 | * U+00F3 LATIN SMALL LETTER O WITH ACUTE |
| 1311 | * U+00F4 LATIN SMALL LETTER O WITH CIRCUMFLEX |
| 1312 | * U+00F5 LATIN SMALL LETTER O WITH TILDE |
| 1313 | * U+00F6 LATIN SMALL LETTER O WITH DIAERESIS |
| 1314 | */ |
| 1315 | (utf8_char[0] == 0xc3 && 0xb2 <= utf8_char[1] && utf8_char[1] <= 0xb6) || |
| 1316 | /* |
| 1317 | * Latin Extended-A |
| 1318 | * U+014D LATIN SMALL LETTER O WITH MACRON |
| 1319 | * U+014F LATIN SMALL LETTER O WITH BREVE |
| 1320 | * U+0151 LATIN SMALL LETTER O WITH DOUBLE ACUTE |
| 1321 | * Uppercase counterparts (U+014E) are covered by the following |
| 1322 | * condition but they are never appeared here. Because NFKC normalization |
| 1323 | * converts them to their lowercase equivalents. |
| 1324 | */ |
| 1325 | (utf8_char[0] == 0xc5 && ((0x8d <= utf8_char[1] && utf8_char[1] <= 0x8f) || |
| 1326 | utf8_char[1] == 0x91)) || |
| 1327 | /* |
| 1328 | * Latin Extended-B |
| 1329 | * U+01A1 LATIN SMALL LETTER O WITH HORN |
| 1330 | */ |
| 1331 | (utf8_char[0] == 0xc6 && utf8_char[1] == 0xa1) || |
| 1332 | /* |
| 1333 | * Latin Extended-B |
| 1334 | * U+01D2 LATIN SMALL LETTER O WITH CARON |
| 1335 | * U+01EB LATIN SMALL LETTER O WITH OGONEK |
| 1336 | * U+01ED LATIN SMALL LETTER O WITH OGONEK AND MACRON |
| 1337 | * Each missing one is an upper case. |
| 1338 | */ |
| 1339 | (utf8_char[0] == 0xc7 && |
| 1340 | ((utf8_char[1] == 0x92) || |
| 1341 | (0xab <= utf8_char[1] && utf8_char[1] <= 0xad))) || |
| 1342 | /* |
| 1343 | * Latin Extended-B |
| 1344 | * U+020D LATIN SMALL LETTER O WITH DOUBLE GRAVE |
| 1345 | * U+020F LATIN SMALL LETTER O WITH INVERTED BREVE |
| 1346 | * U+022B LATIN SMALL LETTER O WITH DIAERESIS AND MACRON |
| 1347 | * U+022D LATIN SMALL LETTER O WITH TILDE AND MACRON |
| 1348 | * U+022F LATIN SMALL LETTER O WITH DOT ABOVE |
| 1349 | * U+0231 LATIN SMALL LETTER O WITH DOT ABOVE AND MACRON |
| 1350 | * Each missing one is an upper case. |
| 1351 | */ |
| 1352 | (utf8_char[0] == 0xc8 && |
| 1353 | ((0x8d <= utf8_char[1] && utf8_char[1] <= 0x8f) || |
| 1354 | (0xab <= utf8_char[1] && utf8_char[1] <= 0xb1))) || |
| 1355 | /* |
| 1356 | * Latin Extended Additional |
| 1357 | * U+1E4D LATIN SMALL LETTER O WITH TILDE AND ACUTE |
| 1358 | * U+1E4F LATIN SMALL LETTER O WITH TILDE AND DIAERESIS |
| 1359 | * U+1E51 LATIN SMALL LETTER O WITH MACRON AND GRAVE |
| 1360 | * U+1E53 LATIN SMALL LETTER O WITH MACRON AND ACUTE |
| 1361 | * U+1ECD LATIN SMALL LETTER O WITH DOT BELOW |
| 1362 | * U+1ECF LATIN SMALL LETTER O WITH HOOK ABOVE |
| 1363 | * U+1ED1 LATIN SMALL LETTER O WITH CIRCUMFLEX AND ACUTE |
| 1364 | * U+1ED3 LATIN SMALL LETTER O WITH CIRCUMFLEX AND GRAVE |
| 1365 | * U+1ED5 LATIN SMALL LETTER O WITH CIRCUMFLEX AND HOOK ABOVE |
| 1366 | * U+1ED7 LATIN SMALL LETTER O WITH CIRCUMFLEX AND TILDE |
| 1367 | * U+1ED9 LATIN SMALL LETTER O WITH CIRCUMFLEX AND DOT BELOW |
| 1368 | * U+1EDB LATIN SMALL LETTER O WITH HORN AND ACUTE |
| 1369 | * U+1EDD LATIN SMALL LETTER O WITH HORN AND GRAVE |
| 1370 | * U+1EDF LATIN SMALL LETTER O WITH HORN AND HOOK ABOVE |
| 1371 | * U+1EE1 LATIN SMALL LETTER O WITH HORN AND TILDE |
| 1372 | * U+1EE3 LATIN SMALL LETTER O WITH HORN AND DOT BELOW |
| 1373 | * |
| 1374 | * Each missing one is an upper case. |
| 1375 | */ |
| 1376 | (utf8_char[0] == 0xe1 && |
| 1377 | ((utf8_char[1] == 0xb9 && 0x8d <= utf8_char[2] && utf8_char[2] <= 0x93) || |
| 1378 | (utf8_char[1] == 0xbb && 0x8d <= utf8_char[2] && utf8_char[2] <= 0xa3)))); |
| 1379 | } |
| 1380 | |
| 1381 | static inline bool |
| 1382 | grn_nfkc_normalize_unify_latin_alphabet_with_is_p( |
| 1383 | const unsigned char *utf8_char) |
| 1384 | { |
| 1385 | return ( |
| 1386 | /* |
| 1387 | * Latin Extended Additional |
| 1388 | * U+1E55 LATIN SMALL LETTER P WITH ACUTE |
| 1389 | * U+1E57 LATIN SMALL LETTER P WITH DOT ABOVE |
| 1390 | * Uppercase counterparts (U+1E56) are covered by the following |
| 1391 | * condition but they are never appeared here. Because NFKC normalization |
| 1392 | * converts them to their lowercase equivalents. |
| 1393 | */ |
| 1394 | utf8_char[0] == 0xe1 && utf8_char[1] == 0xb9 && |
| 1395 | (0x95 <= utf8_char[2] && utf8_char[2] <= 0x97)); |
| 1396 | } |
| 1397 | |
| 1398 | static inline bool |
| 1399 | grn_nfkc_normalize_unify_latin_alphabet_with_is_r( |
| 1400 | const unsigned char *utf8_char) |
| 1401 | { |
| 1402 | return ( |
| 1403 | /* |
| 1404 | * Latin Extended-A |
| 1405 | * U+0155 LATIN SMALL LETTER R WITH ACUTE |
| 1406 | * U+0157 LATIN SMALL LETTER R WITH CEDILLA |
| 1407 | * U+0159 LATIN SMALL LETTER R WITH CARON |
| 1408 | * Uppercase counterparts (e.g. U+0156) are covered by the following |
| 1409 | * condition but they are never appeared here. Because NFKC normalization |
| 1410 | * converts them to their lowercase equivalents. |
| 1411 | */ |
| 1412 | (utf8_char[0] == 0xc5 && 0x95 <= utf8_char[1] && utf8_char[1] <= 0x99) || |
| 1413 | /* |
| 1414 | * Latin Extended-B |
| 1415 | * U+0211 LATIN SMALL LETTER R WITH DOUBLE GRAVE |
| 1416 | * U+0213 LATIN SMALL LETTER R WITH INVERTED BREVE |
| 1417 | * Each missing one is an upper case. |
| 1418 | */ |
| 1419 | (utf8_char[0] == 0xc8 && 0x91 <= utf8_char[1] && utf8_char[1] <= 0x93) || |
| 1420 | /* |
| 1421 | * Latin Extended Additional |
| 1422 | * U+1E59 LATIN SMALL LETTER R WITH DOT ABOVE |
| 1423 | * U+1E5B LATIN SMALL LETTER R WITH DOT BELOW |
| 1424 | * U+1E5D LATIN SMALL LETTER R WITH DOT BELOW AND MACRON |
| 1425 | * U+1E5F LATIN SMALL LETTER R WITH LINE BELOW |
| 1426 | * Each missing one is an upper case. |
| 1427 | */ |
| 1428 | (utf8_char[0] == 0xe1 && utf8_char[1] == 0xb9 && |
| 1429 | (0x99 <= utf8_char[2] && utf8_char[2] <= 0x9f))); |
| 1430 | } |
| 1431 | |
| 1432 | static inline bool |
| 1433 | grn_nfkc_normalize_unify_latin_alphabet_with_is_s( |
| 1434 | const unsigned char *utf8_char) |
| 1435 | { |
| 1436 | return ( |
| 1437 | /* |
| 1438 | * Latin Extended-A |
| 1439 | * U+015B LATIN SMALL LETTER S WITH ACUTE |
| 1440 | * U+015D LATIN SMALL LETTER S WITH CIRCUMFLEX |
| 1441 | * U+015F LATIN SMALL LETTER S WITH CEDILLA |
| 1442 | * U+0161 LATIN SMALL LETTER S WITH CARON |
| 1443 | * Uppercase counterparts (e.g. U+015C) are covered by the following |
| 1444 | * condition but they are never appeared here. Because NFKC normalization |
| 1445 | * converts them to their lowercase equivalents. |
| 1446 | */ |
| 1447 | (utf8_char[0] == 0xc5 && 0x9b <= utf8_char[1] && utf8_char[1] <= 0xa1) || |
| 1448 | /* |
| 1449 | * Latin Extended-B |
| 1450 | * U+0219 LATIN SMALL LETTER S WITH COMMA BELOW |
| 1451 | */ |
| 1452 | (utf8_char[0] == 0xc8 && utf8_char[1] == 0x99) || |
| 1453 | /* |
| 1454 | * Latin Extended Additional |
| 1455 | * U+1E61 LATIN SMALL LETTER S WITH DOT ABOVE |
| 1456 | * U+1E63 LATIN SMALL LETTER S WITH DOT BELOW |
| 1457 | * U+1E65 LATIN SMALL LETTER S WITH ACUTE AND DOT ABOVE |
| 1458 | * U+1E67 LATIN SMALL LETTER S WITH CARON AND DOT ABOVE |
| 1459 | * U+1E69 LATIN SMALL LETTER S WITH DOT BELOW AND DOT ABOVE |
| 1460 | * |
| 1461 | * Each missing one is an upper case. |
| 1462 | */ |
| 1463 | (utf8_char[0] == 0xe1 && utf8_char[1] == 0xb9 && |
| 1464 | (0xa1 <= utf8_char[2] && utf8_char[2] <= 0xa9))); |
| 1465 | } |
| 1466 | |
| 1467 | static inline bool |
| 1468 | grn_nfkc_normalize_unify_latin_alphabet_with_is_t( |
| 1469 | const unsigned char *utf8_char) |
| 1470 | { |
| 1471 | return ( |
| 1472 | /* |
| 1473 | * Latin Extended-A |
| 1474 | * U+0163 LATIN SMALL LETTER T WITH CEDILLA |
| 1475 | * U+0165 LATIN SMALL LETTER T WITH CARON |
| 1476 | * Uppercase counterparts (e.g. U+0164) are covered by the following |
| 1477 | * condition but they are never appeared here. Because NFKC normalization |
| 1478 | * converts them to their lowercase equivalents. |
| 1479 | */ |
| 1480 | (utf8_char[0] == 0xc5 && 0xa3 <= utf8_char[1] && utf8_char[1] <= 0xa5) || |
| 1481 | /* |
| 1482 | * Latin Extended-B |
| 1483 | * U+021B LATIN SMALL LETTER T WITH COMMA BELOW |
| 1484 | */ |
| 1485 | (utf8_char[0] == 0xc8 && utf8_char[1] == 0x9b) || |
| 1486 | /* |
| 1487 | * Latin Extended Additional |
| 1488 | * U+1E6B LATIN SMALL LETTER T WITH DOT ABOVE |
| 1489 | * U+1E6D LATIN SMALL LETTER T WITH DOT BELOW |
| 1490 | * U+1E6F LATIN SMALL LETTER T WITH LINE BELOW |
| 1491 | * U+1E71 LATIN SMALL LETTER T WITH CIRCUMFLEX BELOW |
| 1492 | * |
| 1493 | * Each missing one is an upper case. |
| 1494 | */ |
| 1495 | (utf8_char[0] == 0xe1 && utf8_char[1] == 0xb9 && |
| 1496 | (0xab <= utf8_char[2] && utf8_char[2] <= 0xb1)) || |
| 1497 | /* |
| 1498 | * Latin Extended Additional |
| 1499 | * U+1E97 LATIN SMALL LETTER T WITH DIAERESIS |
| 1500 | */ |
| 1501 | (utf8_char[0] == 0xe1 && utf8_char[1] == 0xba && utf8_char[2] == 0x97)); |
| 1502 | } |
| 1503 | |
| 1504 | static inline bool |
| 1505 | grn_nfkc_normalize_unify_latin_alphabet_with_is_u( |
| 1506 | const unsigned char *utf8_char) |
| 1507 | { |
| 1508 | return ( |
| 1509 | /* |
| 1510 | * Latin-1 Supplement |
| 1511 | * U+00F9 LATIN SMALL LETTER U WITH GRAVE |
| 1512 | * U+00FA LATIN SMALL LETTER U WITH ACUTE |
| 1513 | * U+00FB LATIN SMALL LETTER U WITH CIRCUMFLEX |
| 1514 | * U+00FC LATIN SMALL LETTER U WITH DIAERESIS |
| 1515 | */ |
| 1516 | (utf8_char[0] == 0xc3 && 0xb9 <= utf8_char[1] && utf8_char[1] <= 0xbc) || |
| 1517 | /* |
| 1518 | * Latin Extended-A |
| 1519 | * U+0169 LATIN SMALL LETTER U WITH TILDE |
| 1520 | * U+016B LATIN SMALL LETTER U WITH MACRON |
| 1521 | * U+016D LATIN SMALL LETTER U WITH BREVE |
| 1522 | * U+016F LATIN SMALL LETTER U WITH RING ABOVE |
| 1523 | * U+0171 LATIN SMALL LETTER U WITH DOUBLE ACUTE |
| 1524 | * U+0173 LATIN SMALL LETTER U WITH OGONEK |
| 1525 | * Uppercase counterparts (e.g. #U+016A) are covered by the following |
| 1526 | * condition but they are never appeared here. Because NFKC normalization |
| 1527 | * converts them to their lowercase equivalents. |
| 1528 | */ |
| 1529 | (utf8_char[0] == 0xc5 && 0xa9 <= utf8_char[1] && utf8_char[1] <= 0xb3) || |
| 1530 | /* |
| 1531 | * Latin Extended-B |
| 1532 | * U+01B0 LATIN SMALL LETTER U WITH HORN |
| 1533 | */ |
| 1534 | (utf8_char[0] == 0xc6 && utf8_char[1] == 0xb0) || |
| 1535 | /* |
| 1536 | * Latin Extended-B |
| 1537 | * U+01D4 LATIN SMALL LETTER U WITH CARON |
| 1538 | * U+01D6 LATIN SMALL LETTER U WITH DIAERESIS AND MACRON |
| 1539 | * U+01D8 LATIN SMALL LETTER U WITH DIAERESIS AND ACUTE |
| 1540 | * U+01DA LATIN SMALL LETTER U WITH DIAERESIS AND CARON |
| 1541 | * U+01DC LATIN SMALL LETTER U WITH DIAERESIS AND GRAVE |
| 1542 | * |
| 1543 | * Each missing one is an upper case. |
| 1544 | */ |
| 1545 | (utf8_char[0] == 0xc7 && 0x94 <= utf8_char[1] && utf8_char[1] <= 0x9c) || |
| 1546 | /* |
| 1547 | * Latin Extended-B |
| 1548 | * U+0215 LATIN SMALL LETTER U WITH DOUBLE GRAVE |
| 1549 | * U+0217 LATIN SMALL LETTER U WITH INVERTED BREVE |
| 1550 | * |
| 1551 | * Each missing one is an upper case. |
| 1552 | */ |
| 1553 | (utf8_char[0] == 0xc8 && 0x95 <= utf8_char[1] && utf8_char[1] <= 0x97) || |
| 1554 | /* |
| 1555 | * Latin Extended Additional |
| 1556 | * U+1E73 LATIN SMALL LETTER U WITH DIAERESIS BELOW |
| 1557 | * U+1E75 LATIN SMALL LETTER U WITH TILDE BELOW |
| 1558 | * U+1E77 LATIN SMALL LETTER U WITH CIRCUMFLEX BELOW |
| 1559 | * U+1E79 LATIN SMALL LETTER U WITH TILDE AND ACUTE |
| 1560 | * U+1E7B LATIN SMALL LETTER U WITH MACRON AND DIAERESIS |
| 1561 | * |
| 1562 | * Each missing one is an upper case. |
| 1563 | */ |
| 1564 | (utf8_char[0] == 0xe1 && utf8_char[1] == 0xb9 && |
| 1565 | (0xb3 <= utf8_char[2] && utf8_char[2] <= 0xbb)) || |
| 1566 | /* |
| 1567 | * Latin Extended Additional |
| 1568 | * U+1EE5 LATIN SMALL LETTER U WITH DOT BELOW |
| 1569 | * U+1EE7 LATIN SMALL LETTER U WITH HOOK ABOVE |
| 1570 | * U+1EE9 LATIN SMALL LETTER U WITH HORN AND ACUTE |
| 1571 | * U+1EEB LATIN SMALL LETTER U WITH HORN AND GRAVE |
| 1572 | * U+1EED LATIN SMALL LETTER U WITH HORN AND HOOK ABOVE |
| 1573 | * U+1EEF LATIN SMALL LETTER U WITH HORN AND TILDE |
| 1574 | * U+1EF1 LATIN SMALL LETTER U WITH HORN AND DOT BELOW |
| 1575 | * |
| 1576 | * Each missing one is an upper case. |
| 1577 | */ |
| 1578 | (utf8_char[0] == 0xe1 && utf8_char[1] == 0xbb && |
| 1579 | (0xa5 <= utf8_char[2] && utf8_char[2] <= 0xb1))); |
| 1580 | } |
| 1581 | |
| 1582 | static inline bool |
| 1583 | grn_nfkc_normalize_unify_latin_alphabet_with_is_v( |
| 1584 | const unsigned char *utf8_char) |
| 1585 | { |
| 1586 | return ( |
| 1587 | /* |
| 1588 | * Latin Extended Additional |
| 1589 | * U+1E7D LATIN SMALL LETTER V WITH TILDE |
| 1590 | * U+1E7F LATIN SMALL LETTER V WITH DOT BELOW |
| 1591 | * Uppercase counterparts (U+1E7E) are covered by the following |
| 1592 | * condition but they are never appeared here. Because NFKC normalization |
| 1593 | * converts them to their lowercase equivalents. |
| 1594 | */ |
| 1595 | utf8_char[0] == 0xe1 && utf8_char[1] == 0xb9 && |
| 1596 | (0xbd <= utf8_char[2] && utf8_char[2] <= 0xbf)); |
| 1597 | } |
| 1598 | |
| 1599 | static inline bool |
| 1600 | grn_nfkc_normalize_unify_latin_alphabet_with_is_w( |
| 1601 | const unsigned char *utf8_char) |
| 1602 | { |
| 1603 | return ( |
| 1604 | /* |
| 1605 | * Latin Extended-A |
| 1606 | * U+0175 LATIN SMALL LETTER W WITH CIRCUMFLEX |
| 1607 | */ |
| 1608 | (utf8_char[0] == 0xc5 && utf8_char[1] == 0xb5) || |
| 1609 | /* |
| 1610 | * Latin Extended Additional |
| 1611 | * U+1E81 LATIN SMALL LETTER W WITH GRAVE |
| 1612 | * U+1E83 LATIN SMALL LETTER W WITH ACUTE |
| 1613 | * U+1E85 LATIN SMALL LETTER W WITH DIAERESIS |
| 1614 | * U+1E87 LATIN SMALL LETTER W WITH DOT ABOVE |
| 1615 | * U+1E89 LATIN SMALL LETTER W WITH DOT BELOW |
| 1616 | * Uppercase counterparts (e.g. U+1E82) are covered by the following |
| 1617 | * condition but they are never appeared here. Because NFKC normalization |
| 1618 | * converts them to their lowercase equivalents. |
| 1619 | */ |
| 1620 | (utf8_char[0] == 0xe1 && utf8_char[1] == 0xba && |
| 1621 | (0x81 <= utf8_char[2] && utf8_char[2] <= 0x89)) || |
| 1622 | /* |
| 1623 | * Latin Extended Additional |
| 1624 | * U+1E98 LATIN SMALL LETTER W WITH RING ABOVE |
| 1625 | */ |
| 1626 | (utf8_char[0] == 0xe1 && utf8_char[1] == 0xba && utf8_char[2] == 0x98)); |
| 1627 | } |
| 1628 | |
| 1629 | static inline bool |
| 1630 | grn_nfkc_normalize_unify_latin_alphabet_with_is_x( |
| 1631 | const unsigned char *utf8_char) |
| 1632 | { |
| 1633 | return ( |
| 1634 | /* |
| 1635 | * Latin Extended Additional |
| 1636 | * U+1E8B LATIN SMALL LETTER X WITH DOT ABOVE |
| 1637 | * U+1E8D LATIN SMALL LETTER X WITH DIAERESIS |
| 1638 | * Uppercase counterparts (U+1E8C) are covered by the following |
| 1639 | * condition but they are never appeared here. Because NFKC normalization |
| 1640 | * converts them to their lowercase equivalents. |
| 1641 | */ |
| 1642 | utf8_char[0] == 0xe1 && utf8_char[1] == 0xba && |
| 1643 | (0x8b <= utf8_char[2] && utf8_char[2] <= 0x8d)); |
| 1644 | } |
| 1645 | |
| 1646 | static inline bool |
| 1647 | grn_nfkc_normalize_unify_latin_alphabet_with_is_y( |
| 1648 | const unsigned char *utf8_char) |
| 1649 | { |
| 1650 | return ( |
| 1651 | /* |
| 1652 | * Latin-1 Supplement |
| 1653 | * U+00FD LATIN SMALL LETTER Y WITH ACUTE |
| 1654 | * U+00FF LATIN SMALL LETTER Y WITH DIAERESIS |
| 1655 | */ |
| 1656 | (utf8_char[0] == 0xc3 && utf8_char[1] == 0xbd) || |
| 1657 | (utf8_char[0] == 0xc3 && utf8_char[1] == 0xbf) || |
| 1658 | /* |
| 1659 | * Latin Extended-A |
| 1660 | * U+0177 LATIN SMALL LETTER Y WITH CIRCUMFLEX |
| 1661 | */ |
| 1662 | (utf8_char[0] == 0xc5 && utf8_char[1] == 0xb7) || |
| 1663 | /* |
| 1664 | * Latin Extended-B |
| 1665 | * U+0233 LATIN SMALL LETTER Y WITH MACRON |
| 1666 | */ |
| 1667 | (utf8_char[0] == 0xc8 && utf8_char[1] == 0xb3) || |
| 1668 | /* |
| 1669 | * Latin Extended Additional |
| 1670 | * U+1E8F LATIN SMALL LETTER Y WITH DOT ABOVE |
| 1671 | * U+1E99 LATIN SMALL LETTER Y WITH RING ABOVE |
| 1672 | */ |
| 1673 | (utf8_char[0] == 0xe1 && utf8_char[1] == 0xba && |
| 1674 | (utf8_char[2] == 0x8f || utf8_char[2] == 0x99)) || |
| 1675 | /* |
| 1676 | * Latin Extended Additional |
| 1677 | * U+1EF3 LATIN SMALL LETTER Y WITH GRAVE |
| 1678 | * U+1EF5 LATIN SMALL LETTER Y WITH DOT BELOW |
| 1679 | * U+1EF7 LATIN SMALL LETTER Y WITH HOOK ABOVE |
| 1680 | * U+1EF9 LATIN SMALL LETTER Y WITH TILDE |
| 1681 | * Uppercase counterparts (e.g. U+1EF4) are covered by the following |
| 1682 | * condition but they are never appeared here. Because NFKC normalization |
| 1683 | * converts them to their lowercase equivalents. |
| 1684 | */ |
| 1685 | (utf8_char[0] == 0xe1 && utf8_char[1] == 0xbb && |
| 1686 | (0xb3 <= utf8_char[2] && utf8_char[2] <= 0xb9))); |
| 1687 | } |
| 1688 | |
| 1689 | static inline bool |
| 1690 | grn_nfkc_normalize_unify_latin_alphabet_with_is_z( |
| 1691 | const unsigned char *utf8_char) |
| 1692 | { |
| 1693 | return ( |
| 1694 | /* |
| 1695 | * Latin Extended-A |
| 1696 | * U+017A LATIN SMALL LETTER Z WITH ACUTE |
| 1697 | * U+017C LATIN SMALL LETTER Z WITH DOT ABOVE |
| 1698 | * U+017E LATIN SMALL LETTER Z WITH CARON |
| 1699 | * Uppercase counterparts (e.g. U+017B) are covered by the following |
| 1700 | * condition but they are never appeared here. Because NFKC normalization |
| 1701 | * converts them to their lowercase equivalents. |
| 1702 | */ |
| 1703 | (utf8_char[0] == 0xc5 && 0xba <= utf8_char[1] && utf8_char[1] <= 0xbe) || |
| 1704 | /* |
| 1705 | * Latin Extended Additional |
| 1706 | * U+1E91 LATIN SMALL LETTER Z WITH CIRCUMFLEX |
| 1707 | * U+1E93 LATIN SMALL LETTER Z WITH DOT BELOW |
| 1708 | * U+1E95 LATIN SMALL LETTER Z WITH LINE BELOW |
| 1709 | * |
| 1710 | * Each missing one is an upper case. |
| 1711 | */ |
| 1712 | (utf8_char[0] == 0xe1 && utf8_char[1] == 0xba && |
| 1713 | (0x91 <= utf8_char[2] && utf8_char[2] <= 0x95))); |
| 1714 | } |
| 1715 | |
| 1716 | /* |
| 1717 | * This function assumes that the input utf8_char is a valid UTF-8 character. |
| 1718 | * It is the caller's responsibility to ensure that utf8_char is valid UTF-8 |
| 1719 | * before calling this function. This is a precondition in terms of contract |
| 1720 | * programming. |
| 1721 | */ |
| 1722 | static inline const unsigned char * |
| 1723 | grn_nfkc_normalize_unify_latin_alphabet_with(const unsigned char *utf8_char, |
| 1724 | unsigned char *unified) |
| 1725 | { |
| 1726 | if (grn_nfkc_normalize_unify_latin_alphabet_with_is_a(utf8_char)) { |
| 1727 | *unified = 'a'; |
| 1728 | return unified; |
| 1729 | } else if (grn_nfkc_normalize_unify_latin_alphabet_with_is_b(utf8_char)) { |
| 1730 | *unified = 'b'; |
| 1731 | return unified; |
| 1732 | } else if (grn_nfkc_normalize_unify_latin_alphabet_with_is_c(utf8_char)) { |
| 1733 | *unified = 'c'; |
| 1734 | return unified; |
| 1735 | } else if (grn_nfkc_normalize_unify_latin_alphabet_with_is_d(utf8_char)) { |
| 1736 | *unified = 'd'; |
| 1737 | return unified; |
| 1738 | } else if (grn_nfkc_normalize_unify_latin_alphabet_with_is_e(utf8_char)) { |
| 1739 | *unified = 'e'; |
| 1740 | return unified; |
| 1741 | } else if (grn_nfkc_normalize_unify_latin_alphabet_with_is_f(utf8_char)) { |
| 1742 | *unified = 'f'; |
| 1743 | return unified; |
| 1744 | } else if (grn_nfkc_normalize_unify_latin_alphabet_with_is_g(utf8_char)) { |
| 1745 | *unified = 'g'; |
| 1746 | return unified; |
| 1747 | } else if (grn_nfkc_normalize_unify_latin_alphabet_with_is_h(utf8_char)) { |
| 1748 | *unified = 'h'; |
| 1749 | return unified; |
| 1750 | } else if (grn_nfkc_normalize_unify_latin_alphabet_with_is_i(utf8_char)) { |
| 1751 | *unified = 'i'; |
| 1752 | return unified; |
| 1753 | } else if (grn_nfkc_normalize_unify_latin_alphabet_with_is_j(utf8_char)) { |
| 1754 | *unified = 'j'; |
| 1755 | return unified; |
| 1756 | } else if (grn_nfkc_normalize_unify_latin_alphabet_with_is_k(utf8_char)) { |
| 1757 | *unified = 'k'; |
| 1758 | return unified; |
| 1759 | } else if (grn_nfkc_normalize_unify_latin_alphabet_with_is_l(utf8_char)) { |
| 1760 | *unified = 'l'; |
| 1761 | return unified; |
| 1762 | } else if (grn_nfkc_normalize_unify_latin_alphabet_with_is_m(utf8_char)) { |
| 1763 | *unified = 'm'; |
| 1764 | return unified; |
| 1765 | } else if (grn_nfkc_normalize_unify_latin_alphabet_with_is_n(utf8_char)) { |
| 1766 | *unified = 'n'; |
| 1767 | return unified; |
| 1768 | } else if (grn_nfkc_normalize_unify_latin_alphabet_with_is_o(utf8_char)) { |
| 1769 | *unified = 'o'; |
| 1770 | return unified; |
| 1771 | } else if (grn_nfkc_normalize_unify_latin_alphabet_with_is_p(utf8_char)) { |
| 1772 | *unified = 'p'; |
| 1773 | return unified; |
| 1774 | /* 'q' has no "LATIN (SMALL|CAPITAL) LETTER X WITH XXX" */ |
| 1775 | } else if (grn_nfkc_normalize_unify_latin_alphabet_with_is_r(utf8_char)) { |
| 1776 | *unified = 'r'; |
| 1777 | return unified; |
| 1778 | } else if (grn_nfkc_normalize_unify_latin_alphabet_with_is_s(utf8_char)) { |
| 1779 | *unified = 's'; |
| 1780 | return unified; |
| 1781 | } else if (grn_nfkc_normalize_unify_latin_alphabet_with_is_t(utf8_char)) { |
| 1782 | *unified = 't'; |
| 1783 | return unified; |
| 1784 | } else if (grn_nfkc_normalize_unify_latin_alphabet_with_is_u(utf8_char)) { |
| 1785 | *unified = 'u'; |
| 1786 | return unified; |
| 1787 | } else if (grn_nfkc_normalize_unify_latin_alphabet_with_is_v(utf8_char)) { |
| 1788 | *unified = 'v'; |
| 1789 | return unified; |
| 1790 | } else if (grn_nfkc_normalize_unify_latin_alphabet_with_is_w(utf8_char)) { |
| 1791 | *unified = 'w'; |
| 1792 | return unified; |
| 1793 | } else if (grn_nfkc_normalize_unify_latin_alphabet_with_is_x(utf8_char)) { |
| 1794 | *unified = 'x'; |
| 1795 | return unified; |
| 1796 | } else if (grn_nfkc_normalize_unify_latin_alphabet_with_is_y(utf8_char)) { |
| 1797 | *unified = 'y'; |
| 1798 | return unified; |
| 1799 | } else if (grn_nfkc_normalize_unify_latin_alphabet_with_is_z(utf8_char)) { |
| 1800 | *unified = 'z'; |
| 1801 | return unified; |
| 1802 | } else { |
| 1803 | return utf8_char; |
| 1804 | } |
| 1805 | } |
| 1806 | |
| 1807 | static inline const unsigned char * |
| 1808 | grn_nfkc_normalize_unify_kana(const unsigned char *utf8_char, |
| 1809 | unsigned char *unified) |
| 1810 | { |
| 1811 | if (utf8_char[0] == 0xe3 && |
| 1812 | /* U+30A1 KATAKANA LETTER SMALL A .. |
| 1813 | * U+30F6 KATAKANA LETTER SMALL KE |
| 1814 | * |
| 1815 | * U+30FD KATAKANA ITERATION MARK .. |
| 1816 | * U+30F6 KATAKANA LETTER SMALL KE */ |
| 1817 | ((utf8_char[1] == 0x82 && 0xa1 <= utf8_char[2]) || |
| 1818 | (utf8_char[1] == 0x83 && utf8_char[2] <= 0xb6) || |
| 1819 | (utf8_char[1] == 0x83 && |
| 1820 | (0xbd <= utf8_char[2] && utf8_char[2] <= 0xbe)))) { |
| 1821 | unified[0] = utf8_char[0]; |
| 1822 | if (utf8_char[2] & 0x20) { |
| 1823 | unified[1] = utf8_char[1] - 1; |
| 1824 | } else { |
| 1825 | unified[1] = utf8_char[1] - 2; |
| 1826 | } |
| 1827 | unified[2] = utf8_char[2] ^ 0x20; |
| 1828 | return unified; |
| 1829 | } |
| 1830 | |
| 1831 | return utf8_char; |
| 1832 | } |
| 1833 | |
| 1834 | static inline const unsigned char * |
| 1835 | grn_nfkc_normalize_unify_hiragana_case(const unsigned char *utf8_char, |
| 1836 | unsigned char *unified) |
| 1837 | { |
| 1838 | if (utf8_char[0] == 0xe3) { |
| 1839 | if ((utf8_char[1] == 0x81 && |
| 1840 | (0x81 <= utf8_char[2] && utf8_char[2] <= 0x89)) || |
| 1841 | (utf8_char[1] == 0x81 && utf8_char[2] == 0xa3) || |
| 1842 | (utf8_char[1] == 0x82 && |
| 1843 | (0x83 <= utf8_char[2] && utf8_char[2] <= 0x87))) { |
| 1844 | /* U+3041 HIRAGANA LETTER SMALL A .. |
| 1845 | * U+3049 HIRAGANA LETTER SMALL O |
| 1846 | * |
| 1847 | * U+3063 HIRAGANA LETTER SMALL TU |
| 1848 | * |
| 1849 | * U+3083 HIRAGANA LETTER SMALL YA .. |
| 1850 | * U+3087 HIRAGANA LETTER SMALL YO */ |
| 1851 | if (utf8_char[2] & 0x1) { |
| 1852 | unified[0] = utf8_char[0]; |
| 1853 | unified[1] = utf8_char[1]; |
| 1854 | unified[2] = utf8_char[2] + 1; |
| 1855 | return unified; |
| 1856 | } |
| 1857 | } else if (utf8_char[1] == 0x82 && utf8_char[2] == 0x8e) { |
| 1858 | /* U+308E HIRAGANA LETTER SMALL WA */ |
| 1859 | unified[0] = utf8_char[0]; |
| 1860 | unified[1] = utf8_char[1]; |
| 1861 | unified[2] = utf8_char[2] + 1; |
| 1862 | return unified; |
| 1863 | } else if (utf8_char[1] == 0x82 && utf8_char[2] == 0x95) { |
| 1864 | /* U+3095 HIRAGANA LETTER SMALL KA */ |
| 1865 | unified[0] = utf8_char[0]; |
| 1866 | unified[1] = 0x81; |
| 1867 | unified[2] = 0x8b; |
| 1868 | return unified; |
| 1869 | } else if (utf8_char[1] == 0x82 && utf8_char[2] == 0x96) { |
| 1870 | /* U+3096 HIRAGANA LETTER SMALL KE */ |
| 1871 | unified[0] = utf8_char[0]; |
| 1872 | unified[1] = 0x81; |
| 1873 | unified[2] = 0x91; |
| 1874 | return unified; |
| 1875 | } |
| 1876 | } |
| 1877 | |
| 1878 | return utf8_char; |
| 1879 | } |
| 1880 | |
| 1881 | static inline const unsigned char * |
| 1882 | grn_nfkc_normalize_unify_katakana_case(const unsigned char *utf8_char, |
| 1883 | unsigned char *unified) |
| 1884 | { |
| 1885 | if (utf8_char[0] == 0xe3) { |
| 1886 | if ((utf8_char[1] == 0x82 && |
| 1887 | (0xa1 <= utf8_char[2] && utf8_char[2] <= 0xa9)) || |
| 1888 | (utf8_char[1] == 0x83 && utf8_char[2] == 0x83) || |
| 1889 | (utf8_char[1] == 0x83 && |
| 1890 | (0xa3 <= utf8_char[2] && utf8_char[2] <= 0xa7))) { |
| 1891 | /* U+30A1 KATAKANA LETTER SMALL A .. |
| 1892 | * U+30A9 KATAKANA LETTER SMALL O |
| 1893 | * |
| 1894 | * U+30C3 KATAKANA LETTER SMALL TU |
| 1895 | * |
| 1896 | * U+30E3 KATAKANA LETTER SMALL YA .. |
| 1897 | * U+30E7 KATAKANA LETTER SMALL YO */ |
| 1898 | if (utf8_char[2] & 0x1) { |
| 1899 | unified[0] = utf8_char[0]; |
| 1900 | unified[1] = utf8_char[1]; |
| 1901 | unified[2] = utf8_char[2] + 1; |
| 1902 | return unified; |
| 1903 | } |
| 1904 | } else if (utf8_char[1] == 0x83 && utf8_char[2] == 0xae) { |
| 1905 | /* U+30EE KATAKANA LETTER SMALL WA */ |
| 1906 | unified[0] = utf8_char[0]; |
| 1907 | unified[1] = utf8_char[1]; |
| 1908 | unified[2] = utf8_char[2] + 1; |
| 1909 | return unified; |
| 1910 | } else if (utf8_char[1] == 0x83 && utf8_char[2] == 0xb5) { |
| 1911 | /* U+3095 HIRAGANA LETTER SMALL KA */ |
| 1912 | unified[0] = utf8_char[0]; |
| 1913 | unified[1] = 0x82; |
| 1914 | unified[2] = 0xab; |
| 1915 | return unified; |
| 1916 | } else if (utf8_char[1] == 0x83 && utf8_char[2] == 0xb6) { |
| 1917 | /* U+3096 HIRAGANA LETTER SMALL KE */ |
| 1918 | unified[0] = utf8_char[0]; |
| 1919 | unified[1] = 0x82; |
| 1920 | unified[2] = 0xb1; |
| 1921 | return unified; |
| 1922 | } |
| 1923 | } |
| 1924 | |
| 1925 | return utf8_char; |
| 1926 | } |
| 1927 | |
| 1928 | static inline const unsigned char * |
| 1929 | grn_nfkc_normalize_unify_hiragana_voiced_sound_mark( |
| 1930 | const unsigned char *utf8_char, unsigned char *unified) |
| 1931 | { |
| 1932 | if (utf8_char[0] == 0xe3) { |
| 1933 | if ((utf8_char[1] == 0x81 && |
| 1934 | (0x8c <= utf8_char[2] && utf8_char[2] <= 0xa2))) { |
| 1935 | /* U+304C HIRAGANA LETTER GA .. |
| 1936 | * U+3062 HIRAGANA LETTER DI */ |
| 1937 | if (!(utf8_char[2] & 0x1)) { |
| 1938 | unified[0] = utf8_char[0]; |
| 1939 | unified[1] = utf8_char[1]; |
| 1940 | unified[2] = utf8_char[2] - 1; |
| 1941 | return unified; |
| 1942 | } |
| 1943 | } else if ((utf8_char[1] == 0x81 && |
| 1944 | (0xa5 <= utf8_char[2] && utf8_char[2] <= 0xa9))) { |
| 1945 | /* U+3065 HIRAGANA LETTER DU .. |
| 1946 | * U+3069 HIRAGANA LETTER DO */ |
| 1947 | if (utf8_char[2] & 0x1) { |
| 1948 | unified[0] = utf8_char[0]; |
| 1949 | unified[1] = utf8_char[1]; |
| 1950 | unified[2] = utf8_char[2] - 1; |
| 1951 | return unified; |
| 1952 | } |
| 1953 | } else if ((utf8_char[1] == 0x81 && |
| 1954 | (0xb0 <= utf8_char[2] && utf8_char[2] <= 0xbd))) { |
| 1955 | /* U+3070 HIRAGANA LETTER BA .. |
| 1956 | * U+307D HIRAGANA LETTER PO */ |
| 1957 | unsigned char mod3 = (unsigned char)((utf8_char[2] - 1) % 3); |
| 1958 | if (mod3 != 0) { |
| 1959 | unified[0] = utf8_char[0]; |
| 1960 | unified[1] = utf8_char[1]; |
| 1961 | unified[2] = utf8_char[2] - mod3; |
| 1962 | return unified; |
| 1963 | } |
| 1964 | } |
| 1965 | } |
| 1966 | |
| 1967 | return utf8_char; |
| 1968 | } |
| 1969 | |
| 1970 | static inline bool |
| 1971 | grn_nfkc_normalize_process_hiragana_voiced_iteration_mark( |
| 1972 | const unsigned char *utf8_char, unsigned char *voiced) |
| 1973 | { |
| 1974 | if (utf8_char[0] == 0xe3 && utf8_char[1] == 0x81) { |
| 1975 | if (0x8b <= utf8_char[2] && utf8_char[2] <= 0xa2) { |
| 1976 | /* U+304B HIRAGANA LETTER KA .. |
| 1977 | * U+3062 HIRAGANA LETTER DI */ |
| 1978 | voiced[0] = utf8_char[0]; |
| 1979 | voiced[1] = utf8_char[1]; |
| 1980 | voiced[2] = utf8_char[2]; |
| 1981 | if (utf8_char[2] & 0x1) { |
| 1982 | /* Unvoiced -> add voiced mark */ |
| 1983 | voiced[2] += 1; |
| 1984 | } |
| 1985 | return true; |
| 1986 | } else if (0xa4 <= utf8_char[2] && utf8_char[2] <= 0xa9) { |
| 1987 | /* U+3064 HIRAGANA LETTER TU .. |
| 1988 | * U+3069 HIRAGANA LETTER DO */ |
| 1989 | voiced[0] = utf8_char[0]; |
| 1990 | voiced[1] = utf8_char[1]; |
| 1991 | voiced[2] = utf8_char[2]; |
| 1992 | if (!(utf8_char[2] & 0x1)) { |
| 1993 | /* Unvoiced -> add voiced mark */ |
| 1994 | voiced[2] += 1; |
| 1995 | } |
| 1996 | return true; |
| 1997 | } else if (0xaf <= utf8_char[2] && utf8_char[2] <= 0xbd) { |
| 1998 | /* U+306F HIRAGANA LETTER HA .. |
| 1999 | * U+307D HIRAGANA LETTER PO */ |
| 2000 | unsigned char mod3 = (unsigned char)((utf8_char[2] - 0xaf) % 3); |
| 2001 | if (mod3 == 0) { |
| 2002 | /* Unvoiced -> add voiced mark */ |
| 2003 | voiced[0] = utf8_char[0]; |
| 2004 | voiced[1] = utf8_char[1]; |
| 2005 | voiced[2] = utf8_char[2] + 1; |
| 2006 | return true; |
| 2007 | } else if (mod3 == 1) { |
| 2008 | /* Already voiced -> no change */ |
| 2009 | voiced[0] = utf8_char[0]; |
| 2010 | voiced[1] = utf8_char[1]; |
| 2011 | voiced[2] = utf8_char[2]; |
| 2012 | return true; |
| 2013 | } else { |
| 2014 | /* Semi-voiced -> not normalized to voiced */ |
| 2015 | return false; |
| 2016 | } |
| 2017 | } |
| 2018 | } |
| 2019 | return false; |
| 2020 | } |
| 2021 | |
| 2022 | static inline const unsigned char * |
| 2023 | grn_nfkc_normalize_unify_katakana_voiced_sound_mark( |
| 2024 | const unsigned char *utf8_char, unsigned char *unified) |
| 2025 | { |
| 2026 | if (utf8_char[0] == 0xe3) { |
| 2027 | if (utf8_char[1] == 0x83 && utf8_char[2] == 0x80) { |
| 2028 | /* U+30C0 KATAKANA LETTER DA */ |
| 2029 | unified[0] = utf8_char[0]; |
| 2030 | unified[1] = 0x82; |
| 2031 | unified[2] = 0xbf; |
| 2032 | return unified; |
| 2033 | } else if ((utf8_char[1] == 0x82 && 0xac <= utf8_char[2]) || |
| 2034 | (utf8_char[1] == 0x83 && utf8_char[2] <= 0x82)) { |
| 2035 | /* U+30AC KATAKANA LETTER GA .. |
| 2036 | * U+30C2 KATAKANA LETTER DI */ |
| 2037 | if (!(utf8_char[2] & 0x1)) { |
| 2038 | unified[0] = utf8_char[0]; |
| 2039 | unified[1] = utf8_char[1]; |
| 2040 | unified[2] = utf8_char[2] - 1; |
| 2041 | return unified; |
| 2042 | } |
| 2043 | } else if ((utf8_char[1] == 0x83 && |
| 2044 | (0x85 <= utf8_char[2] && utf8_char[2] <= 0x89))) { |
| 2045 | /* U+30C5 KATAKANA LETTER DU .. |
| 2046 | * U+30C9 KATAKANA LETTER DO */ |
| 2047 | if (utf8_char[2] & 0x1) { |
| 2048 | unified[0] = utf8_char[0]; |
| 2049 | unified[1] = utf8_char[1]; |
| 2050 | unified[2] = utf8_char[2] - 1; |
| 2051 | return unified; |
| 2052 | } |
| 2053 | } else if ((utf8_char[1] == 0x83 && |
| 2054 | (0x90 <= utf8_char[2] && utf8_char[2] <= 0x9d))) { |
| 2055 | /* U+30D0 KATAKANA LETTER BA .. |
| 2056 | * U+30DD KATAKANA LETTER PO */ |
| 2057 | unsigned char mod3 = (unsigned char)((utf8_char[2] - 2) % 3); |
| 2058 | if (mod3 != 0) { |
| 2059 | unified[0] = utf8_char[0]; |
| 2060 | unified[1] = utf8_char[1]; |
| 2061 | unified[2] = utf8_char[2] - mod3; |
| 2062 | return unified; |
| 2063 | } |
| 2064 | } |
| 2065 | } |
| 2066 | |
| 2067 | return utf8_char; |
| 2068 | } |
| 2069 | |
| 2070 | static inline bool |
| 2071 | grn_nfkc_normalize_process_katakana_voiced_iteration_mark( |
| 2072 | const unsigned char *utf8_char, unsigned char *voiced) |
| 2073 | { |
| 2074 | if (utf8_char[0] == 0xe3) { |
| 2075 | if (utf8_char[1] == 0x82 && utf8_char[2] == 0xBF) { |
| 2076 | /* U+30BF KATAKANA LETTER TA => |
| 2077 | * U+30C0 KATAKANA LETTER DA */ |
| 2078 | voiced[0] = utf8_char[0]; |
| 2079 | voiced[1] = 0x83; |
| 2080 | voiced[2] = 0x80; |
| 2081 | return true; |
| 2082 | } else if ((utf8_char[1] == 0x82 && 0xab <= utf8_char[2]) || |
| 2083 | (utf8_char[1] == 0x83 && utf8_char[2] <= 0x82)) { |
| 2084 | /* U+30AB KATAKANA LETTER KA .. |
| 2085 | * U+30C2 KATAKANA LETTER DI */ |
| 2086 | voiced[0] = utf8_char[0]; |
| 2087 | voiced[1] = utf8_char[1]; |
| 2088 | voiced[2] = utf8_char[2]; |
| 2089 | if (utf8_char[2] & 0x1) { |
| 2090 | /* Unvoiced -> add voiced mark */ |
| 2091 | voiced[2] += 1; |
| 2092 | } |
| 2093 | return true; |
| 2094 | } else if ((utf8_char[1] == 0x83 && |
| 2095 | (0x84 <= utf8_char[2] && utf8_char[2] <= 0x89))) { |
| 2096 | /* U+30C4 KATAKANA LETTER TU .. |
| 2097 | * U+30C9 KATAKANA LETTER DO */ |
| 2098 | voiced[0] = utf8_char[0]; |
| 2099 | voiced[1] = utf8_char[1]; |
| 2100 | voiced[2] = utf8_char[2]; |
| 2101 | if (!(utf8_char[2] & 0x1)) { |
| 2102 | /* Unvoiced -> add voiced mark */ |
| 2103 | voiced[2] += 1; |
| 2104 | } |
| 2105 | return true; |
| 2106 | } else if ((utf8_char[1] == 0x83 && |
| 2107 | (0x8F <= utf8_char[2] && utf8_char[2] <= 0x9d))) { |
| 2108 | /* U+30D0 KATAKANA LETTER BA .. |
| 2109 | * U+30DD KATAKANA LETTER PO */ |
| 2110 | unsigned char mod3 = (unsigned char)((utf8_char[2] - 2) % 3); |
| 2111 | if (mod3 == 0) { |
| 2112 | /* Unvoiced -> add voiced mark */ |
| 2113 | voiced[0] = utf8_char[0]; |
| 2114 | voiced[1] = utf8_char[1]; |
| 2115 | voiced[2] = utf8_char[2] + 1; |
| 2116 | return true; |
| 2117 | } else if (mod3 == 1) { |
| 2118 | /* Already voiced -> no change */ |
| 2119 | voiced[0] = utf8_char[0]; |
| 2120 | voiced[1] = utf8_char[1]; |
| 2121 | voiced[2] = utf8_char[2]; |
| 2122 | return true; |
| 2123 | } else { |
| 2124 | /* Semi-voiced -> not normalized to voiced */ |
| 2125 | return false; |
| 2126 | } |
| 2127 | } |
| 2128 | } |
| 2129 | |
| 2130 | return false; |
| 2131 | } |
| 2132 | |
| 2133 | static inline bool |
| 2134 | grn_nfkc_normalize_is_hyphen(const unsigned char *utf8_char, size_t length) |
| 2135 | { |
| 2136 | /* U+002D HYPHEN-MINUS */ |
| 2137 | return (length == 1 && utf8_char[0] == '-'); |
| 2138 | } |
| 2139 | |
| 2140 | static inline bool |
| 2141 | grn_nfkc_normalize_is_prolonged_sound_mark(const unsigned char *utf8_char, |
| 2142 | size_t length) |
| 2143 | { |
| 2144 | /* U+30FC KATAKANA-HIRAGANA PROLONGED SOUND MARK */ |
| 2145 | return (length == 3 && utf8_char[0] == 0xe3 && utf8_char[1] == 0x83 && |
| 2146 | utf8_char[2] == 0xbc); |
| 2147 | } |
| 2148 | |
| 2149 | static inline bool |
| 2150 | grn_nfkc_normalize_is_hyphen_family(const unsigned char *utf8_char, |
| 2151 | size_t length) |
| 2152 | { |
| 2153 | if (length == 1) { |
| 2154 | if (utf8_char[0] == '-') { |
| 2155 | /* U+002D HYPHEN-MINUS */ |
| 2156 | return true; |
| 2157 | } |
| 2158 | } else if (length == 2) { |
| 2159 | switch (utf8_char[0]) { |
| 2160 | case 0xcb: |
| 2161 | if (utf8_char[1] == 0x97) { |
| 2162 | /* U+02D7 MODIFIER LETTER MINUS SIGN */ |
| 2163 | return true; |
| 2164 | } |
| 2165 | break; |
| 2166 | case 0xd6: |
| 2167 | if (utf8_char[1] == 0x8a) { |
| 2168 | /* U+058A ARMENIAN HYPHEN */ |
| 2169 | return true; |
| 2170 | } |
| 2171 | break; |
| 2172 | default: |
| 2173 | break; |
| 2174 | } |
| 2175 | } else if (length == 3) { |
| 2176 | if (utf8_char[0] == 0xe2) { |
| 2177 | if (utf8_char[1] == 0x80 && |
| 2178 | (0x90 <= utf8_char[2] && utf8_char[2] <= 0x93)) { |
| 2179 | /* U+2010 HYPHEN .. |
| 2180 | * U+2013 EN DASH */ |
| 2181 | return true; |
| 2182 | } else if (utf8_char[1] == 0x81 && |
| 2183 | (utf8_char[2] == 0x83 || utf8_char[2] == 0xbb)) { |
| 2184 | /* U+2043 HYPHEN BULLET */ |
| 2185 | /* U+207B SUPERSCRIPT MINUS */ |
| 2186 | return true; |
| 2187 | } else if (utf8_char[1] == 0x82 && utf8_char[2] == 0x8b) { |
| 2188 | /* U+208B SUBSCRIPT MINUS */ |
| 2189 | return true; |
| 2190 | } else if (utf8_char[1] == 0x88 && utf8_char[2] == 0x92) { |
| 2191 | /* U+2212 MINUS SIGN */ |
| 2192 | return true; |
| 2193 | } |
| 2194 | } |
| 2195 | } |
| 2196 | |
| 2197 | return false; |
| 2198 | } |
| 2199 | |
| 2200 | static inline bool |
| 2201 | grn_nfkc_normalize_is_prolonged_sound_mark_family( |
| 2202 | const unsigned char *utf8_char, size_t length) |
| 2203 | { |
| 2204 | if (length == 3) { |
| 2205 | if (utf8_char[0] == 0xe2) { |
| 2206 | if (utf8_char[1] == 0x80 && |
| 2207 | (0x94 <= utf8_char[2] && utf8_char[2] <= 0x95)) { |
| 2208 | /* U+2014 EM DASH .. |
| 2209 | * U+2015 HORIZONTAL BAR */ |
| 2210 | return true; |
| 2211 | } else if (utf8_char[1] == 0x94 && |
| 2212 | (0x80 <= utf8_char[2] && utf8_char[2] <= 0x81)) { |
| 2213 | /* U+2500 BOX DRAWINGS LIGHT HORIZONTAL .. |
| 2214 | * U+2501 BOX DRAWINGS HEAVY HORIZONTAL */ |
| 2215 | return true; |
| 2216 | } |
| 2217 | } else if (utf8_char[0] == 0xe3) { |
| 2218 | if (utf8_char[1] == 0x83 && utf8_char[2] == 0xbc) { |
| 2219 | /* U+30FC KATAKANA-HIRAGANA PROLONGED SOUND MARK */ |
| 2220 | return true; |
| 2221 | } |
| 2222 | } else if (utf8_char[0] == 0xef) { |
| 2223 | if (utf8_char[1] == 0xbd && utf8_char[2] == 0xb0) { |
| 2224 | /* U+FF70 HALFWIDTH KATAKANA-HIRAGANA PROLONGED SOUND MARK */ |
| 2225 | return true; |
| 2226 | } |
| 2227 | } |
| 2228 | } |
| 2229 | |
| 2230 | return false; |
| 2231 | } |
| 2232 | |
| 2233 | static inline bool |
| 2234 | grn_nfkc_normalize_is_middle_dot_family(const unsigned char *utf8_char, |
| 2235 | size_t length) |
| 2236 | { |
| 2237 | if (length == 3) { |
| 2238 | if (utf8_char[0] == 0xe1) { |
| 2239 | if (utf8_char[1] == 0x90 && utf8_char[2] == 0xa7) { |
| 2240 | /* U+1427 CANADIAN SYLLABICS FINAL MIDDLE DOT */ |
| 2241 | return true; |
| 2242 | } |
| 2243 | } else if (utf8_char[0] == 0xe2) { |
| 2244 | if (utf8_char[1] == 0x80 && utf8_char[2] == 0xa2) { |
| 2245 | /* U+2022 BULLET */ |
| 2246 | return true; |
| 2247 | } else if (utf8_char[1] == 0x88 && utf8_char[2] == 0x99) { |
| 2248 | /* U+2219 BULLET OPERATOR */ |
| 2249 | return true; |
| 2250 | } else if (utf8_char[1] == 0x8b && utf8_char[2] == 0x85) { |
| 2251 | /* U+22C5 DOT OPERATOR */ |
| 2252 | return true; |
| 2253 | } else if (utf8_char[1] == 0xb8 && utf8_char[2] == 0xb1) { |
| 2254 | /* U+2E31 WORD SEPARATOR MIDDLE DOT */ |
| 2255 | return true; |
| 2256 | } |
| 2257 | } else if (utf8_char[0] == 0xe3) { |
| 2258 | if (utf8_char[1] == 0x83 && utf8_char[2] == 0xbb) { |
| 2259 | /* U+30FB KATAKANA MIDDLE DOT */ |
| 2260 | return true; |
| 2261 | } |
| 2262 | } else if (utf8_char[0] == 0xef) { |
| 2263 | if (utf8_char[1] == 0xbd && utf8_char[2] == 0xa5) { |
| 2264 | /* U+FF65 HALFWIDTH KATAKANA MIDDLE DOT */ |
| 2265 | return true; |
| 2266 | } |
| 2267 | } |
| 2268 | } |
| 2269 | |
| 2270 | return false; |
| 2271 | } |
| 2272 | |
| 2273 | static inline const unsigned char * |
| 2274 | grn_nfkc_normalize_unify_to_katakana(const unsigned char *utf8_char, |
| 2275 | unsigned char *unified) |
| 2276 | { |
| 2277 | if (utf8_char[0] == 0xe3) { |
| 2278 | if (utf8_char[1] == 0x81 && utf8_char[2] >= 0x81 && utf8_char[2] <= 0x9f) { |
| 2279 | /* U+3041 HIRAGANA LETTER SMALL A .. |
| 2280 | * U+305F HIRAGANA LETTER TA */ |
| 2281 | unified[0] = utf8_char[0]; |
| 2282 | unified[1] = utf8_char[1] + 0x01; |
| 2283 | unified[2] = utf8_char[2] + 0x20; |
| 2284 | return unified; |
| 2285 | } else if (utf8_char[1] == 0x81 && utf8_char[2] >= 0xa0 && |
| 2286 | utf8_char[2] <= 0xbf) { |
| 2287 | /* U+3060 HIRAGANA LETTER DA .. |
| 2288 | * U+305F HIRAGANA LETTER MI */ |
| 2289 | unified[0] = utf8_char[0]; |
| 2290 | unified[1] = utf8_char[1] + 0x02; |
| 2291 | unified[2] = utf8_char[2] - 0x20; |
| 2292 | return unified; |
| 2293 | } else if (utf8_char[1] == 0x82) { |
| 2294 | if ((utf8_char[2] >= 0x80 && utf8_char[2] <= 0x96) || |
| 2295 | (utf8_char[2] >= 0x9d && utf8_char[2] <= 0x9e)) { |
| 2296 | /* U+3041 HIRAGANA LETTER MU .. |
| 2297 | * U+3096 HIRAGANA LETTER KE |
| 2298 | * U+309D HIRAGANA ITERATION MARK |
| 2299 | * U+309E HIRAGANA VOICED ITERATION MARK */ |
| 2300 | unified[0] = utf8_char[0]; |
| 2301 | unified[1] = utf8_char[1] + 0x01; |
| 2302 | unified[2] = utf8_char[2] + 0x20; |
| 2303 | return unified; |
| 2304 | } |
| 2305 | } |
| 2306 | } |
| 2307 | return utf8_char; |
| 2308 | } |
| 2309 | |
| 2310 | static void |
| 2311 | grn_nfkc_normalize_unify_stateless(grn_ctx *ctx, |
| 2312 | grn_nfkc_normalize_data *data, |
| 2313 | grn_nfkc_normalize_context *unify, |
| 2314 | bool before) |
| 2315 | { |
| 2316 | const unsigned char *current = data->context.dest; |
| 2317 | const unsigned char *end = data->context.d; |
| 2318 | size_t i_byte; |
| 2319 | size_t i_character; |
| 2320 | |
| 2321 | i_byte = 0; |
| 2322 | i_character = 0; |
| 2323 | while (current < end) { |
| 2324 | unsigned char unified_alphabet[1]; |
| 2325 | unsigned char unified_kana[3]; |
| 2326 | unsigned char unified_kana_case[3]; |
| 2327 | unsigned char unified_kana_voiced_sound_mark[3]; |
| 2328 | unsigned char unified_hyphen[] = {'-'}; |
| 2329 | /* U+30FC KATAKANA-HIRAGANA PROLONGED SOUND MARK */ |
| 2330 | unsigned char unified_prolonged_sound_mark[] = {0xe3, 0x83, 0xbc}; |
| 2331 | /* U+00B7 MIDDLE DOT */ |
| 2332 | unsigned char unified_middle_dot[] = {0xc2, 0xb7}; |
| 2333 | unsigned char unified_katakana[3]; |
| 2334 | const unsigned char *unifying = current; |
| 2335 | size_t char_length; |
| 2336 | size_t unified_char_length; |
| 2337 | grn_char_type char_type; |
| 2338 | |
| 2339 | char_length = (size_t)grn_charlen_(ctx, current, end, GRN_ENC_UTF8); |
| 2340 | unified_char_length = char_length; |
| 2341 | |
| 2342 | if (data->context.t) { |
| 2343 | char_type = data->context.types[i_character]; |
| 2344 | } else { |
| 2345 | char_type = data->options->char_type_func(current); |
| 2346 | } |
| 2347 | |
| 2348 | if (before && data->options->unify_latin_alphabet_with && |
| 2349 | GRN_CHAR_TYPE(char_type) == GRN_CHAR_ALPHA && |
| 2350 | (unified_char_length == 2 || unified_char_length == 3)) { |
| 2351 | unifying = grn_nfkc_normalize_unify_latin_alphabet_with(unifying, |
| 2352 | unified_alphabet); |
| 2353 | if (unifying == unified_alphabet) { |
| 2354 | unified_char_length = sizeof(unified_alphabet); |
| 2355 | } |
| 2356 | } |
| 2357 | |
| 2358 | if (before && data->options->unify_kana && |
| 2359 | GRN_CHAR_TYPE(char_type) == GRN_CHAR_KATAKANA && |
| 2360 | unified_char_length == 3) { |
| 2361 | unifying = grn_nfkc_normalize_unify_kana(unifying, unified_kana); |
| 2362 | if (unifying == unified_kana) { |
| 2363 | char_type = GRN_CHAR_HIRAGANA | (char_type & GRN_CHAR_BLANK); |
| 2364 | } |
| 2365 | } |
| 2366 | |
| 2367 | if (before && data->options->unify_to_katakana && |
| 2368 | GRN_CHAR_TYPE(char_type) == GRN_CHAR_HIRAGANA && |
| 2369 | unified_char_length == 3) { |
| 2370 | unifying = |
| 2371 | grn_nfkc_normalize_unify_to_katakana(unifying, unified_katakana); |
| 2372 | if (unifying == unified_katakana) { |
| 2373 | char_type = GRN_CHAR_KATAKANA | (char_type & GRN_CHAR_BLANK); |
| 2374 | } |
| 2375 | } |
| 2376 | |
| 2377 | if (!before && data->options->unify_kana_case) { |
| 2378 | switch (GRN_CHAR_TYPE(char_type)) { |
| 2379 | case GRN_CHAR_HIRAGANA: |
| 2380 | if (unified_char_length == 3) { |
| 2381 | unifying = |
| 2382 | grn_nfkc_normalize_unify_hiragana_case(unifying, unified_kana_case); |
| 2383 | } |
| 2384 | break; |
| 2385 | case GRN_CHAR_KATAKANA: |
| 2386 | if (unified_char_length == 3) { |
| 2387 | unifying = |
| 2388 | grn_nfkc_normalize_unify_katakana_case(unifying, unified_kana_case); |
| 2389 | } |
| 2390 | break; |
| 2391 | default: |
| 2392 | break; |
| 2393 | } |
| 2394 | } |
| 2395 | |
| 2396 | if (before && data->options->unify_kana_voiced_sound_mark) { |
| 2397 | switch (GRN_CHAR_TYPE(char_type)) { |
| 2398 | case GRN_CHAR_HIRAGANA: |
| 2399 | if (unified_char_length == 3) { |
| 2400 | unifying = grn_nfkc_normalize_unify_hiragana_voiced_sound_mark( |
| 2401 | unifying, |
| 2402 | unified_kana_voiced_sound_mark); |
| 2403 | } |
| 2404 | break; |
| 2405 | case GRN_CHAR_KATAKANA: |
| 2406 | if (unified_char_length == 3) { |
| 2407 | unifying = grn_nfkc_normalize_unify_katakana_voiced_sound_mark( |
| 2408 | unifying, |
| 2409 | unified_kana_voiced_sound_mark); |
| 2410 | } |
| 2411 | break; |
| 2412 | default: |
| 2413 | break; |
| 2414 | } |
| 2415 | } |
| 2416 | |
| 2417 | if (before && data->options->unify_hyphen) { |
| 2418 | if (grn_nfkc_normalize_is_hyphen_family(unifying, unified_char_length)) { |
| 2419 | unifying = unified_hyphen; |
| 2420 | unified_char_length = sizeof(unified_hyphen); |
| 2421 | char_type = GRN_CHAR_SYMBOL | (char_type & GRN_CHAR_BLANK); |
| 2422 | } |
| 2423 | } |
| 2424 | |
| 2425 | if (before && data->options->unify_prolonged_sound_mark) { |
| 2426 | if (grn_nfkc_normalize_is_prolonged_sound_mark_family( |
| 2427 | unifying, |
| 2428 | unified_char_length)) { |
| 2429 | unifying = unified_prolonged_sound_mark; |
| 2430 | unified_char_length = sizeof(unified_prolonged_sound_mark); |
| 2431 | char_type = GRN_CHAR_KATAKANA | (char_type & GRN_CHAR_BLANK); |
| 2432 | } |
| 2433 | } |
| 2434 | |
| 2435 | /* When both `unify_hyphen_and_prolonged_sound_mark` and `remove_symbol` are |
| 2436 | * enabled, the hyphen is handled as a symbol and has already been removed. |
| 2437 | * Therefore, when `unify_hyphen_and_prolonged_sound_mark` is enabled and |
| 2438 | * `remove_symbol` is disabled, the hyphen is unified. */ |
| 2439 | if (before && data->options->unify_hyphen_and_prolonged_sound_mark && |
| 2440 | !data->options->remove_symbol) { |
| 2441 | if (grn_nfkc_normalize_is_hyphen_family(unifying, unified_char_length) || |
| 2442 | grn_nfkc_normalize_is_prolonged_sound_mark_family( |
| 2443 | unifying, |
| 2444 | unified_char_length)) { |
| 2445 | unifying = unified_hyphen; |
| 2446 | unified_char_length = sizeof(unified_hyphen); |
| 2447 | char_type = GRN_CHAR_SYMBOL | (char_type & GRN_CHAR_BLANK); |
| 2448 | } |
| 2449 | } |
| 2450 | |
| 2451 | /* When both `unify_middle_dot` and `remove_symbol` are enabled, the middle |
| 2452 | * dot is handled as a symbol and has already been removed. Therefore, when |
| 2453 | * `unify_middle_dot` is enabled and `remove_symbol` is disabled, the middle |
| 2454 | * dot is unified. */ |
| 2455 | if (before && data->options->unify_middle_dot && |
| 2456 | !data->options->remove_symbol) { |
| 2457 | if (grn_nfkc_normalize_is_middle_dot_family(unifying, |
| 2458 | unified_char_length)) { |
| 2459 | unifying = unified_middle_dot; |
| 2460 | unified_char_length = sizeof(unified_middle_dot); |
| 2461 | char_type = GRN_CHAR_SYMBOL | (char_type & GRN_CHAR_BLANK); |
| 2462 | } |
| 2463 | } |
| 2464 | |
| 2465 | if (unify->d + unified_char_length >= unify->dest_end) { |
| 2466 | grn_nfkc_normalize_context_expand(ctx, |
| 2467 | unify, |
| 2468 | unified_char_length, |
| 2469 | "[unify]"); |
| 2470 | if (ctx->rc != GRN_SUCCESS) { |
| 2471 | return; |
| 2472 | } |
| 2473 | } |
| 2474 | |
| 2475 | grn_memcpy(unify->d, unifying, unified_char_length); |
| 2476 | unify->d += unified_char_length; |
| 2477 | unify->n_characters++; |
| 2478 | if (unify->t) { |
| 2479 | *(unify->t++) = (uint8_t)char_type; |
| 2480 | } |
| 2481 | if (unify->c) { |
| 2482 | size_t i; |
| 2483 | if (unifying == current) { |
| 2484 | memcpy(unify->c, |
| 2485 | data->context.checks + i_byte, |
| 2486 | sizeof(int16_t) * char_length); |
| 2487 | unify->c += char_length; |
| 2488 | } else { |
| 2489 | *(unify->c++) += data->context.checks[i_byte]; |
| 2490 | for (i = 1; i < unified_char_length; i++) { |
| 2491 | *(unify->c++) = 0; |
| 2492 | } |
| 2493 | } |
| 2494 | unify->c[0] = 0; |
| 2495 | } |
| 2496 | if (unify->o) { |
| 2497 | *(unify->o++) = data->context.offsets[i_character]; |
| 2498 | } |
| 2499 | |
| 2500 | i_byte += char_length; |
| 2501 | current += char_length; |
| 2502 | i_character++; |
| 2503 | } |
| 2504 | } |
| 2505 | |
| 2506 | typedef const unsigned char *(*grn_nfkc_normalize_unify_stateful_func)( |
| 2507 | grn_ctx *ctx, |
| 2508 | const unsigned char *start, |
| 2509 | const unsigned char *current, |
| 2510 | const unsigned char *end, |
| 2511 | size_t *n_used_bytes, |
| 2512 | size_t *n_used_characters, |
| 2513 | unsigned char *unified_buffer, |
| 2514 | size_t *n_unified_bytes, |
| 2515 | size_t *n_unified_characters, |
| 2516 | void *user_data); |
| 2517 | |
| 2518 | static const unsigned char * |
| 2519 | grn_nfkc_normalize_unify_katakana_v_sounds(grn_ctx *ctx, |
| 2520 | const unsigned char *start, |
| 2521 | const unsigned char *current, |
| 2522 | const unsigned char *end, |
| 2523 | size_t *n_used_bytes, |
| 2524 | size_t *n_used_characters, |
| 2525 | unsigned char *unified_buffer, |
| 2526 | size_t *n_unified_bytes, |
| 2527 | size_t *n_unified_characters, |
| 2528 | void *user_data) |
| 2529 | { |
| 2530 | size_t char_length; |
| 2531 | |
| 2532 | char_length = (size_t)grn_charlen_(ctx, current, end, GRN_ENC_UTF8); |
| 2533 | |
| 2534 | *n_used_bytes = char_length; |
| 2535 | *n_used_characters = 1; |
| 2536 | |
| 2537 | /* U+30F4 KATAKANA LETTER VU */ |
| 2538 | if (char_length == 3 && current[0] == 0xe3 && current[1] == 0x83 && |
| 2539 | current[2] == 0xb4) { |
| 2540 | const unsigned char *next = current + char_length; |
| 2541 | size_t next_char_length; |
| 2542 | |
| 2543 | next_char_length = (size_t)grn_charlen_(ctx, next, end, GRN_ENC_UTF8); |
| 2544 | if (next_char_length == 3 && next[0] == 0xe3 && next[1] == 0x82) { |
| 2545 | if (next[2] == 0xa1) { /* U+30A1 KATAKANA LETTER SMALL A */ |
| 2546 | /* U+30D0 KATAKANA LETTER BA */ |
| 2547 | unified_buffer[(*n_unified_bytes)++] = current[0]; |
| 2548 | unified_buffer[(*n_unified_bytes)++] = current[1]; |
| 2549 | unified_buffer[(*n_unified_bytes)++] = 0x90; |
| 2550 | (*n_unified_characters)++; |
| 2551 | (*n_used_bytes) += next_char_length; |
| 2552 | (*n_used_characters)++; |
| 2553 | return unified_buffer; |
| 2554 | } else if (next[2] == 0xa3) { /* U+30A3 KATAKANA LETTER SMALL I */ |
| 2555 | /* U+30D3 KATAKANA LETTER BI */ |
| 2556 | unified_buffer[(*n_unified_bytes)++] = current[0]; |
| 2557 | unified_buffer[(*n_unified_bytes)++] = current[1]; |
| 2558 | unified_buffer[(*n_unified_bytes)++] = 0x93; |
| 2559 | (*n_unified_characters)++; |
| 2560 | (*n_used_bytes) += next_char_length; |
| 2561 | (*n_used_characters)++; |
| 2562 | return unified_buffer; |
| 2563 | } else if (next[2] == 0xa5) { /* U+30A5 KATAKANA LETTER SMALL U */ |
| 2564 | /* U+30D6 KATAKANA LETTER BU */ |
| 2565 | unified_buffer[(*n_unified_bytes)++] = current[0]; |
| 2566 | unified_buffer[(*n_unified_bytes)++] = current[1]; |
| 2567 | unified_buffer[(*n_unified_bytes)++] = 0x96; |
| 2568 | (*n_unified_characters)++; |
| 2569 | (*n_used_bytes) += next_char_length; |
| 2570 | (*n_used_characters)++; |
| 2571 | return unified_buffer; |
| 2572 | } else if (next[2] == 0xa7) { /* U+30A7 KATAKANA LETTER SMALL E */ |
| 2573 | /* U+30D9 KATAKANA LETTER BE */ |
| 2574 | unified_buffer[(*n_unified_bytes)++] = current[0]; |
| 2575 | unified_buffer[(*n_unified_bytes)++] = current[1]; |
| 2576 | unified_buffer[(*n_unified_bytes)++] = 0x99; |
| 2577 | (*n_unified_characters)++; |
| 2578 | (*n_used_bytes) += next_char_length; |
| 2579 | (*n_used_characters)++; |
| 2580 | return unified_buffer; |
| 2581 | } else if (next[2] == 0xa9) { /* U+30A8 KATAKANA LETTER SMALL O */ |
| 2582 | /* U+30DC KATAKANA LETTER BO */ |
| 2583 | unified_buffer[(*n_unified_bytes)++] = current[0]; |
| 2584 | unified_buffer[(*n_unified_bytes)++] = current[1]; |
| 2585 | unified_buffer[(*n_unified_bytes)++] = 0x9c; |
| 2586 | (*n_unified_characters)++; |
| 2587 | (*n_used_bytes) += next_char_length; |
| 2588 | (*n_used_characters)++; |
| 2589 | return unified_buffer; |
| 2590 | } |
| 2591 | } |
| 2592 | /* U+30D6 KATAKANA LETTER BU */ |
| 2593 | unified_buffer[(*n_unified_bytes)++] = current[0]; |
| 2594 | unified_buffer[(*n_unified_bytes)++] = current[1]; |
| 2595 | unified_buffer[(*n_unified_bytes)++] = 0x96; |
| 2596 | (*n_unified_characters)++; |
| 2597 | return unified_buffer; |
| 2598 | } |
| 2599 | |
| 2600 | *n_unified_bytes = *n_used_bytes; |
| 2601 | *n_unified_characters = *n_used_characters; |
| 2602 | |
| 2603 | return current; |
| 2604 | } |
| 2605 | |
| 2606 | static const unsigned char * |
| 2607 | grn_nfkc_normalize_unify_katakana_bu_sound(grn_ctx *ctx, |
| 2608 | const unsigned char *start, |
| 2609 | const unsigned char *current, |
| 2610 | const unsigned char *end, |
| 2611 | size_t *n_used_bytes, |
| 2612 | size_t *n_used_characters, |
| 2613 | unsigned char *unified_buffer, |
| 2614 | size_t *n_unified_bytes, |
| 2615 | size_t *n_unified_characters, |
| 2616 | void *user_data) |
| 2617 | { |
| 2618 | size_t char_length; |
| 2619 | |
| 2620 | char_length = (size_t)grn_charlen_(ctx, current, end, GRN_ENC_UTF8); |
| 2621 | *n_used_bytes = char_length; |
| 2622 | *n_used_characters = 1; |
| 2623 | |
| 2624 | /* U+30F4 KATAKANA LETTER VU */ |
| 2625 | if (char_length == 3 && current[0] == 0xe3 && current[1] == 0x83 && |
| 2626 | current[2] == 0xb4) { |
| 2627 | const unsigned char *next = current + char_length; |
| 2628 | size_t next_char_length; |
| 2629 | |
| 2630 | /* U+30D6 KATAKANA LETTER BU */ |
| 2631 | unified_buffer[(*n_unified_bytes)++] = current[0]; |
| 2632 | unified_buffer[(*n_unified_bytes)++] = current[1]; |
| 2633 | unified_buffer[(*n_unified_bytes)++] = 0x96; |
| 2634 | (*n_unified_characters)++; |
| 2635 | |
| 2636 | next_char_length = (size_t)grn_charlen_(ctx, next, end, GRN_ENC_UTF8); |
| 2637 | if (next_char_length == 3 && next[0] == 0xe3 && next[1] == 0x82 && |
| 2638 | /* U+30A1 KATAKANA LETTER SMALL A */ |
| 2639 | /* U+30A3 KATAKANA LETTER SMALL I */ |
| 2640 | /* U+30A5 KATAKANA LETTER SMALL U */ |
| 2641 | /* U+30A7 KATAKANA LETTER SMALL E */ |
| 2642 | /* U+30A9 KATAKANA LETTER SMALL O */ |
| 2643 | (next[2] == 0xa1 || next[2] == 0xa3 || next[2] == 0xa5 || |
| 2644 | next[2] == 0xa7 || next[2] == 0xa9)) { |
| 2645 | (*n_used_bytes) += next_char_length; |
| 2646 | (*n_used_characters)++; |
| 2647 | } |
| 2648 | return unified_buffer; |
| 2649 | } |
| 2650 | |
| 2651 | *n_unified_bytes = *n_used_bytes; |
| 2652 | *n_unified_characters = *n_used_characters; |
| 2653 | |
| 2654 | return current; |
| 2655 | } |
| 2656 | |
| 2657 | static const unsigned char * |
| 2658 | grn_nfkc_normalize_unify_katakana_du_small_sounds(grn_ctx *ctx, |
| 2659 | const unsigned char *start, |
| 2660 | const unsigned char *current, |
| 2661 | const unsigned char *end, |
| 2662 | size_t *n_used_bytes, |
| 2663 | size_t *n_used_characters, |
| 2664 | unsigned char *unified_buffer, |
| 2665 | size_t *n_unified_bytes, |
| 2666 | size_t *n_unified_characters, |
| 2667 | void *user_data) |
| 2668 | { |
| 2669 | size_t char_length; |
| 2670 | |
| 2671 | char_length = (size_t)grn_charlen_(ctx, current, end, GRN_ENC_UTF8); |
| 2672 | |
| 2673 | *n_used_bytes = char_length; |
| 2674 | *n_used_characters = 1; |
| 2675 | |
| 2676 | if (char_length == 3 && |
| 2677 | /* U+30C5 KATAKANA LETTER DU */ |
| 2678 | current[0] == 0xe3 && current[1] == 0x83 && current[2] == 0x85) { |
| 2679 | const unsigned char *next = current + char_length; |
| 2680 | size_t next_char_length; |
| 2681 | |
| 2682 | next_char_length = (size_t)grn_charlen_(ctx, next, end, GRN_ENC_UTF8); |
| 2683 | if (next_char_length == 3 && next[0] == 0xe3 && next[1] == 0x82) { |
| 2684 | if (next[2] == 0xa1) { /* U+30A1 KATAKANA LETTER SMALL A */ |
| 2685 | /* U+30B6 KATAKANA LETTER ZA */ |
| 2686 | unified_buffer[(*n_unified_bytes)++] = current[0]; |
| 2687 | unified_buffer[(*n_unified_bytes)++] = 0x82; |
| 2688 | unified_buffer[(*n_unified_bytes)++] = 0xb6; |
| 2689 | (*n_unified_characters)++; |
| 2690 | (*n_used_bytes) += next_char_length; |
| 2691 | (*n_used_characters)++; |
| 2692 | return unified_buffer; |
| 2693 | } else if (next[2] == 0xa3) { /* U+30A3 KATAKANA LETTER SMALL I */ |
| 2694 | /* U+30B8 KATAKANA LETTER ZI */ |
| 2695 | unified_buffer[(*n_unified_bytes)++] = current[0]; |
| 2696 | unified_buffer[(*n_unified_bytes)++] = 0x82; |
| 2697 | unified_buffer[(*n_unified_bytes)++] = 0xb8; |
| 2698 | (*n_unified_characters)++; |
| 2699 | (*n_used_bytes) += next_char_length; |
| 2700 | (*n_used_characters)++; |
| 2701 | return unified_buffer; |
| 2702 | } else if (next[2] == 0xa7) { /* U+30A7 KATAKANA LETTER SMALL E */ |
| 2703 | /* U+30BC KATAKANA LETTER ZE */ |
| 2704 | unified_buffer[(*n_unified_bytes)++] = current[0]; |
| 2705 | unified_buffer[(*n_unified_bytes)++] = 0x82; |
| 2706 | unified_buffer[(*n_unified_bytes)++] = 0xbC; |
| 2707 | (*n_unified_characters)++; |
| 2708 | (*n_used_bytes) += next_char_length; |
| 2709 | (*n_used_characters)++; |
| 2710 | return unified_buffer; |
| 2711 | } else if (next[2] == 0xa9) { /* U+30A8 KATAKANA LETTER SMALL O */ |
| 2712 | /* U+30BE KATAKANA LETTER ZO */ |
| 2713 | unified_buffer[(*n_unified_bytes)++] = current[0]; |
| 2714 | unified_buffer[(*n_unified_bytes)++] = 0x82; |
| 2715 | unified_buffer[(*n_unified_bytes)++] = 0xbe; |
| 2716 | (*n_unified_characters)++; |
| 2717 | (*n_used_bytes) += next_char_length; |
| 2718 | (*n_used_characters)++; |
| 2719 | return unified_buffer; |
| 2720 | } |
| 2721 | } |
| 2722 | } |
| 2723 | |
| 2724 | *n_unified_bytes = *n_used_bytes; |
| 2725 | *n_unified_characters = *n_used_characters; |
| 2726 | |
| 2727 | return current; |
| 2728 | } |
| 2729 | |
| 2730 | static const unsigned char * |
| 2731 | grn_nfkc_normalize_unify_katakana_du_sound(grn_ctx *ctx, |
| 2732 | const unsigned char *start, |
| 2733 | const unsigned char *current, |
| 2734 | const unsigned char *end, |
| 2735 | size_t *n_used_bytes, |
| 2736 | size_t *n_used_characters, |
| 2737 | unsigned char *unified_buffer, |
| 2738 | size_t *n_unified_bytes, |
| 2739 | size_t *n_unified_characters, |
| 2740 | void *user_data) |
| 2741 | { |
| 2742 | size_t char_length; |
| 2743 | |
| 2744 | char_length = (size_t)grn_charlen_(ctx, current, end, GRN_ENC_UTF8); |
| 2745 | |
| 2746 | *n_used_bytes = char_length; |
| 2747 | *n_used_characters = 1; |
| 2748 | |
| 2749 | if (char_length == 3 && |
| 2750 | /* U+30C5 KATAKANA LETTER DU */ |
| 2751 | current[0] == 0xe3 && current[1] == 0x83 && current[2] == 0x85) { |
| 2752 | /* U+30BA KATAKANA LETTER ZU */ |
| 2753 | unified_buffer[(*n_unified_bytes)++] = current[0]; |
| 2754 | unified_buffer[(*n_unified_bytes)++] = 0x82; |
| 2755 | unified_buffer[(*n_unified_bytes)++] = 0xba; |
| 2756 | (*n_unified_characters)++; |
| 2757 | return unified_buffer; |
| 2758 | } |
| 2759 | |
| 2760 | *n_unified_bytes = *n_used_bytes; |
| 2761 | *n_unified_characters = *n_used_characters; |
| 2762 | |
| 2763 | return current; |
| 2764 | } |
| 2765 | |
| 2766 | static const unsigned char * |
| 2767 | grn_nfkc_normalize_unify_katakana_zu_small_sounds(grn_ctx *ctx, |
| 2768 | const unsigned char *start, |
| 2769 | const unsigned char *current, |
| 2770 | const unsigned char *end, |
| 2771 | size_t *n_used_bytes, |
| 2772 | size_t *n_used_characters, |
| 2773 | unsigned char *unified_buffer, |
| 2774 | size_t *n_unified_bytes, |
| 2775 | size_t *n_unified_characters, |
| 2776 | void *user_data) |
| 2777 | { |
| 2778 | size_t char_length; |
| 2779 | |
| 2780 | char_length = (size_t)grn_charlen_(ctx, current, end, GRN_ENC_UTF8); |
| 2781 | |
| 2782 | *n_used_bytes = char_length; |
| 2783 | *n_used_characters = 1; |
| 2784 | |
| 2785 | if (char_length == 3 && |
| 2786 | /* U+30BA KATAKANA LETTER ZU */ |
| 2787 | current[0] == 0xe3 && current[1] == 0x82 && current[2] == 0xba) { |
| 2788 | const unsigned char *next = current + char_length; |
| 2789 | size_t next_char_length; |
| 2790 | |
| 2791 | next_char_length = (size_t)grn_charlen_(ctx, next, end, GRN_ENC_UTF8); |
| 2792 | if (next_char_length == 3 && next[0] == 0xe3 && next[1] == 0x82) { |
| 2793 | if (next[2] == 0xa1) { /* U+30A1 KATAKANA LETTER SMALL A */ |
| 2794 | /* U+30B6 KATAKANA LETTER ZA */ |
| 2795 | unified_buffer[(*n_unified_bytes)++] = current[0]; |
| 2796 | unified_buffer[(*n_unified_bytes)++] = 0x82; |
| 2797 | unified_buffer[(*n_unified_bytes)++] = 0xb6; |
| 2798 | (*n_unified_characters)++; |
| 2799 | (*n_used_bytes) += next_char_length; |
| 2800 | (*n_used_characters)++; |
| 2801 | return unified_buffer; |
| 2802 | } else if (next[2] == 0xa3) { /* U+30A3 KATAKANA LETTER SMALL I */ |
| 2803 | /* U+30B8 KATAKANA LETTER ZI */ |
| 2804 | unified_buffer[(*n_unified_bytes)++] = current[0]; |
| 2805 | unified_buffer[(*n_unified_bytes)++] = 0x82; |
| 2806 | unified_buffer[(*n_unified_bytes)++] = 0xb8; |
| 2807 | (*n_unified_characters)++; |
| 2808 | (*n_used_bytes) += next_char_length; |
| 2809 | (*n_used_characters)++; |
| 2810 | return unified_buffer; |
| 2811 | } else if (next[2] == 0xa7) { /* U+30A7 KATAKANA LETTER SMALL E */ |
| 2812 | /* U+30BC KATAKANA LETTER ZE */ |
| 2813 | unified_buffer[(*n_unified_bytes)++] = current[0]; |
| 2814 | unified_buffer[(*n_unified_bytes)++] = 0x82; |
| 2815 | unified_buffer[(*n_unified_bytes)++] = 0xbC; |
| 2816 | (*n_unified_characters)++; |
| 2817 | (*n_used_bytes) += next_char_length; |
| 2818 | (*n_used_characters)++; |
| 2819 | return unified_buffer; |
| 2820 | } else if (next[2] == 0xa9) { /* U+30A8 KATAKANA LETTER SMALL O */ |
| 2821 | /* U+30BE KATAKANA LETTER ZO */ |
| 2822 | unified_buffer[(*n_unified_bytes)++] = current[0]; |
| 2823 | unified_buffer[(*n_unified_bytes)++] = 0x82; |
| 2824 | unified_buffer[(*n_unified_bytes)++] = 0xbe; |
| 2825 | (*n_unified_characters)++; |
| 2826 | (*n_used_bytes) += next_char_length; |
| 2827 | (*n_used_characters)++; |
| 2828 | return unified_buffer; |
| 2829 | } |
| 2830 | } |
| 2831 | } |
| 2832 | |
| 2833 | *n_unified_bytes = *n_used_bytes; |
| 2834 | *n_unified_characters = *n_used_characters; |
| 2835 | |
| 2836 | return current; |
| 2837 | } |
| 2838 | |
| 2839 | static const unsigned char * |
| 2840 | grn_nfkc_normalize_unify_katakana_wo_sound(grn_ctx *ctx, |
| 2841 | const unsigned char *start, |
| 2842 | const unsigned char *current, |
| 2843 | const unsigned char *end, |
| 2844 | size_t *n_used_bytes, |
| 2845 | size_t *n_used_characters, |
| 2846 | unsigned char *unified_buffer, |
| 2847 | size_t *n_unified_bytes, |
| 2848 | size_t *n_unified_characters, |
| 2849 | void *user_data) |
| 2850 | { |
| 2851 | size_t char_length; |
| 2852 | |
| 2853 | char_length = (size_t)grn_charlen_(ctx, current, end, GRN_ENC_UTF8); |
| 2854 | |
| 2855 | *n_used_bytes = char_length; |
| 2856 | *n_used_characters = 1; |
| 2857 | |
| 2858 | /* U+30F2 KATAKANA LETTER WO */ |
| 2859 | if (char_length == 3 && current[0] == 0xe3 && current[1] == 0x83 && |
| 2860 | current[2] == 0xb2) { |
| 2861 | (*n_unified_characters)++; |
| 2862 | /* U+30AA KATAKANA LETTER O */ |
| 2863 | unified_buffer[(*n_unified_bytes)++] = current[0]; |
| 2864 | unified_buffer[(*n_unified_bytes)++] = 0x82; |
| 2865 | unified_buffer[(*n_unified_bytes)++] = 0xaa; |
| 2866 | return unified_buffer; |
| 2867 | } |
| 2868 | |
| 2869 | *n_unified_bytes = *n_used_bytes; |
| 2870 | *n_unified_characters = *n_used_characters; |
| 2871 | |
| 2872 | return current; |
| 2873 | } |
| 2874 | |
| 2875 | static const unsigned char * |
| 2876 | grn_nfkc_normalize_unify_katakana_di_sound(grn_ctx *ctx, |
| 2877 | const unsigned char *start, |
| 2878 | const unsigned char *current, |
| 2879 | const unsigned char *end, |
| 2880 | size_t *n_used_bytes, |
| 2881 | size_t *n_used_characters, |
| 2882 | unsigned char *unified_buffer, |
| 2883 | size_t *n_unified_bytes, |
| 2884 | size_t *n_unified_characters, |
| 2885 | void *user_data) |
| 2886 | { |
| 2887 | size_t char_length; |
| 2888 | |
| 2889 | char_length = (size_t)grn_charlen_(ctx, current, end, GRN_ENC_UTF8); |
| 2890 | |
| 2891 | *n_used_bytes = char_length; |
| 2892 | *n_used_characters = 1; |
| 2893 | |
| 2894 | /* U+30C2 KATAKANA LETTER DI */ |
| 2895 | if (char_length == 3 && current[0] == 0xe3 && current[1] == 0x83 && |
| 2896 | current[2] == 0x82) { |
| 2897 | (*n_unified_characters)++; |
| 2898 | /* U+30B8 KATAKANA LETTER ZI */ |
| 2899 | unified_buffer[(*n_unified_bytes)++] = current[0]; |
| 2900 | unified_buffer[(*n_unified_bytes)++] = 0x82; |
| 2901 | unified_buffer[(*n_unified_bytes)++] = 0xb8; |
| 2902 | return unified_buffer; |
| 2903 | } |
| 2904 | |
| 2905 | *n_unified_bytes = *n_used_bytes; |
| 2906 | *n_unified_characters = *n_used_characters; |
| 2907 | |
| 2908 | return current; |
| 2909 | } |
| 2910 | |
| 2911 | static const unsigned char * |
| 2912 | grn_nfkc_normalize_unify_katakana_gu_small_sounds(grn_ctx *ctx, |
| 2913 | const unsigned char *start, |
| 2914 | const unsigned char *current, |
| 2915 | const unsigned char *end, |
| 2916 | size_t *n_used_bytes, |
| 2917 | size_t *n_used_characters, |
| 2918 | unsigned char *unified_buffer, |
| 2919 | size_t *n_unified_bytes, |
| 2920 | size_t *n_unified_characters, |
| 2921 | void *user_data) |
| 2922 | { |
| 2923 | size_t char_length; |
| 2924 | |
| 2925 | char_length = (size_t)grn_charlen_(ctx, current, end, GRN_ENC_UTF8); |
| 2926 | |
| 2927 | *n_used_bytes = char_length; |
| 2928 | *n_used_characters = 1; |
| 2929 | |
| 2930 | /* U+30B0 KATAKANA LETTER GU */ |
| 2931 | if (char_length == 3 && current[0] == 0xe3 && current[1] == 0x82 && |
| 2932 | current[2] == 0xb0) { |
| 2933 | const unsigned char *next = current + char_length; |
| 2934 | size_t next_char_length; |
| 2935 | |
| 2936 | next_char_length = (size_t)grn_charlen_(ctx, next, end, GRN_ENC_UTF8); |
| 2937 | if (next_char_length == 3 && next[0] == 0xe3 && next[1] == 0x82) { |
| 2938 | if (next[2] == 0xa1) { /* U+30A1 KATAKANA LETTER SMALL A */ |
| 2939 | /* U+30AC KATAKANA LETTER GA */ |
| 2940 | unified_buffer[(*n_unified_bytes)++] = current[0]; |
| 2941 | unified_buffer[(*n_unified_bytes)++] = current[1]; |
| 2942 | unified_buffer[(*n_unified_bytes)++] = 0xac; |
| 2943 | (*n_unified_characters)++; |
| 2944 | (*n_used_bytes) += next_char_length; |
| 2945 | (*n_used_characters)++; |
| 2946 | return unified_buffer; |
| 2947 | } else if (next[2] == 0xa3) { /* U+30A3 KATAKANA LETTER SMALL I */ |
| 2948 | /* U+30AE KATAKANA LETTER GI */ |
| 2949 | unified_buffer[(*n_unified_bytes)++] = current[0]; |
| 2950 | unified_buffer[(*n_unified_bytes)++] = current[1]; |
| 2951 | unified_buffer[(*n_unified_bytes)++] = 0xae; |
| 2952 | (*n_unified_characters)++; |
| 2953 | (*n_used_bytes) += next_char_length; |
| 2954 | (*n_used_characters)++; |
| 2955 | return unified_buffer; |
| 2956 | } else if (next[2] == 0xa7) { /* U+30A7 KATAKANA LETTER SMALL E */ |
| 2957 | /* U+30B2 KATAKANA LETTER GE */ |
| 2958 | unified_buffer[(*n_unified_bytes)++] = current[0]; |
| 2959 | unified_buffer[(*n_unified_bytes)++] = current[1]; |
| 2960 | unified_buffer[(*n_unified_bytes)++] = 0xb2; |
| 2961 | (*n_unified_characters)++; |
| 2962 | (*n_used_bytes) += next_char_length; |
| 2963 | (*n_used_characters)++; |
| 2964 | return unified_buffer; |
| 2965 | } else if (next[2] == 0xa9) { /* U+30A8 KATAKANA LETTER SMALL O */ |
| 2966 | /* U+30B4 KATAKANA LETTER GO */ |
| 2967 | unified_buffer[(*n_unified_bytes)++] = current[0]; |
| 2968 | unified_buffer[(*n_unified_bytes)++] = current[1]; |
| 2969 | unified_buffer[(*n_unified_bytes)++] = 0xb4; |
| 2970 | (*n_unified_characters)++; |
| 2971 | (*n_used_bytes) += next_char_length; |
| 2972 | (*n_used_characters)++; |
| 2973 | return unified_buffer; |
| 2974 | } |
| 2975 | } |
| 2976 | } |
| 2977 | |
| 2978 | *n_unified_bytes = *n_used_bytes; |
| 2979 | *n_unified_characters = *n_used_characters; |
| 2980 | |
| 2981 | return current; |
| 2982 | } |
| 2983 | |
| 2984 | static const unsigned char * |
| 2985 | grn_nfkc_normalize_unify_katakana_trailing_o(grn_ctx *ctx, |
| 2986 | const unsigned char *start, |
| 2987 | const unsigned char *current, |
| 2988 | const unsigned char *end, |
| 2989 | size_t *n_used_bytes, |
| 2990 | size_t *n_used_characters, |
| 2991 | unsigned char *unified_buffer, |
| 2992 | size_t *n_unified_bytes, |
| 2993 | size_t *n_unified_characters, |
| 2994 | void *user_data) |
| 2995 | { |
| 2996 | size_t char_length; |
| 2997 | bool *need_trailing_check = (bool *)user_data; |
| 2998 | |
| 2999 | char_length = (size_t)grn_charlen_(ctx, current, end, GRN_ENC_UTF8); |
| 3000 | |
| 3001 | *n_used_bytes = char_length; |
| 3002 | *n_used_characters = 1; |
| 3003 | |
| 3004 | if (*need_trailing_check && |
| 3005 | /* U+30AA KATAKANA LETTER O */ |
| 3006 | char_length == 3 && current[0] == 0xe3 && current[1] == 0x82 && |
| 3007 | current[2] == 0xaa) { |
| 3008 | /* U+30A6 KATAKANA LETTER U */ |
| 3009 | unified_buffer[(*n_unified_bytes)++] = current[0]; |
| 3010 | unified_buffer[(*n_unified_bytes)++] = current[1]; |
| 3011 | unified_buffer[(*n_unified_bytes)++] = 0xa6; |
| 3012 | (*n_unified_characters)++; |
| 3013 | *need_trailing_check = false; |
| 3014 | return unified_buffer; |
| 3015 | } |
| 3016 | |
| 3017 | *need_trailing_check = char_length == 3 && current[0] == 0xe3 && |
| 3018 | /* U+30A9 KATAKANA LETTER SMALL O */ |
| 3019 | ((current[1] == 0x82 && current[2] == 0xa9) || |
| 3020 | /* U+30AA KATAKANA LETTER O */ |
| 3021 | (current[1] == 0x82 && current[2] == 0xaa) || |
| 3022 | /* U+30B3 KATAKANA LETTER KO */ |
| 3023 | (current[1] == 0x82 && current[2] == 0xb3) || |
| 3024 | /* U+30BD KATAKANA LETTER SO */ |
| 3025 | (current[1] == 0x82 && current[2] == 0xbd) || |
| 3026 | /* U+30C8 KATAKANA LETTER TO */ |
| 3027 | (current[1] == 0x83 && current[2] == 0x88) || |
| 3028 | /* U+30CE KATAKANA LETTER NO */ |
| 3029 | (current[1] == 0x83 && current[2] == 0x8e) || |
| 3030 | /* U+30DB KATAKANA LETTER HO */ |
| 3031 | (current[1] == 0x83 && current[2] == 0x9b) || |
| 3032 | /* U+30E2 KATAKANA LETTER MO */ |
| 3033 | (current[1] == 0x83 && current[2] == 0xa2) || |
| 3034 | /* U+30E7 KATAKANA LETTER SMALL YO */ |
| 3035 | (current[1] == 0x83 && current[2] == 0xa7) || |
| 3036 | /* U+30E8 KATAKANA LETTER YO */ |
| 3037 | (current[1] == 0x83 && current[2] == 0xa8) || |
| 3038 | /* U+30ED KATAKANA LETTER RO */ |
| 3039 | (current[1] == 0x83 && current[2] == 0xad) || |
| 3040 | /* U+30B4 KATAKANA LETTER GO */ |
| 3041 | (current[1] == 0x82 && current[2] == 0xb4) || |
| 3042 | /* U+30BE KATAKANA LETTER ZO */ |
| 3043 | (current[1] == 0x82 && current[2] == 0xbe) || |
| 3044 | /* U+30C9 KATAKANA LETTER DO */ |
| 3045 | (current[1] == 0x83 && current[2] == 0x89) || |
| 3046 | /* U+30DC KATAKANA LETTER BO */ |
| 3047 | (current[1] == 0x83 && current[2] == 0x9c) || |
| 3048 | /* U+30DD KATAKANA LETTER PO */ |
| 3049 | (current[1] == 0x83 && current[2] == 0x9d) || |
| 3050 | /* U+30FA KATAKANA LETTER VO */ |
| 3051 | (current[1] == 0x83 && current[2] == 0xba)); |
| 3052 | *n_unified_bytes = *n_used_bytes; |
| 3053 | *n_unified_characters = *n_used_characters; |
| 3054 | |
| 3055 | return current; |
| 3056 | } |
| 3057 | |
| 3058 | typedef bool (*grn_nfkc_normalize_is_target_char_func)( |
| 3059 | const unsigned char *utf8_char, size_t length); |
| 3060 | |
| 3061 | typedef struct { |
| 3062 | grn_nfkc_normalize_is_target_char_func is_target_char; |
| 3063 | size_t previous_length; |
| 3064 | } grn_nfkc_normalize_prolonged_sound_mark_like_data; |
| 3065 | |
| 3066 | static const unsigned char * |
| 3067 | grn_nfkc_normalize_unify_kana_prolonged_sound_mark_like( |
| 3068 | grn_ctx *ctx, |
| 3069 | const unsigned char *start, |
| 3070 | const unsigned char *current, |
| 3071 | const unsigned char *end, |
| 3072 | size_t *n_used_bytes, |
| 3073 | size_t *n_used_characters, |
| 3074 | unsigned char *unified_buffer, |
| 3075 | size_t *n_unified_bytes, |
| 3076 | size_t *n_unified_characters, |
| 3077 | void *user_data) |
| 3078 | { |
| 3079 | size_t char_length; |
| 3080 | grn_nfkc_normalize_prolonged_sound_mark_like_data *data = user_data; |
| 3081 | size_t previous_length = data->previous_length; |
| 3082 | |
| 3083 | char_length = (size_t)grn_charlen_(ctx, current, end, GRN_ENC_UTF8); |
| 3084 | data->previous_length = char_length; |
| 3085 | |
| 3086 | *n_used_bytes = char_length; |
| 3087 | *n_used_characters = 1; |
| 3088 | |
| 3089 | if (previous_length == 3 && data->is_target_char(current, char_length)) { |
| 3090 | const unsigned char *previous = current - previous_length; |
| 3091 | if (previous[0] == 0xe3) { |
| 3092 | if (/* U+3041 HIRAGANA LETTER SMALL A */ |
| 3093 | (previous[1] == 0x81 && previous[2] == 0x81) || |
| 3094 | /* U+3042 HIRAGANA LETTER A */ |
| 3095 | (previous[1] == 0x81 && previous[2] == 0x82) || |
| 3096 | /* U+304B HIRAGANA LETTER KA */ |
| 3097 | (previous[1] == 0x81 && previous[2] == 0x8b) || |
| 3098 | /* U+304C HIRAGANA LETTER GA */ |
| 3099 | (previous[1] == 0x81 && previous[2] == 0x8c) || |
| 3100 | /* U+3055 HIRAGANA LETTER SA */ |
| 3101 | (previous[1] == 0x81 && previous[2] == 0x95) || |
| 3102 | /* U+3056 HIRAGANA LETTER ZA */ |
| 3103 | (previous[1] == 0x81 && previous[2] == 0x96) || |
| 3104 | /* U+305F HIRAGANA LETTER TA */ |
| 3105 | (previous[1] == 0x81 && previous[2] == 0x9f) || |
| 3106 | /* U+3060 HIRAGANA LETTER DA */ |
| 3107 | (previous[1] == 0x81 && previous[2] == 0xa0) || |
| 3108 | /* U+306A HIRAGANA LETTER NA */ |
| 3109 | (previous[1] == 0x81 && previous[2] == 0xaa) || |
| 3110 | /* U+306F HIRAGANA LETTER HA */ |
| 3111 | (previous[1] == 0x81 && previous[2] == 0xaf) || |
| 3112 | /* U+3070 HIRAGANA LETTER BA */ |
| 3113 | (previous[1] == 0x81 && previous[2] == 0xb0) || |
| 3114 | /* U+3071 HIRAGANA LETTER PA */ |
| 3115 | (previous[1] == 0x81 && previous[2] == 0xb1) || |
| 3116 | /* U+307E HIRAGANA LETTER MA */ |
| 3117 | (previous[1] == 0x81 && previous[2] == 0xbe) || |
| 3118 | /* U+3083 HIRAGANA LETTER SMALL YA */ |
| 3119 | (previous[1] == 0x82 && previous[2] == 0x83) || |
| 3120 | /* U+3084 HIRAGANA LETTER YA */ |
| 3121 | (previous[1] == 0x82 && previous[2] == 0x84) || |
| 3122 | /* U+3089 HIRAGANA LETTER RA */ |
| 3123 | (previous[1] == 0x82 && previous[2] == 0x89) || |
| 3124 | /* U+308E HIRAGANA LETTER SMALL WA */ |
| 3125 | (previous[1] == 0x82 && previous[2] == 0x8e) || |
| 3126 | /* U+308F HIRAGANA LETTER WA */ |
| 3127 | (previous[1] == 0x82 && previous[2] == 0x8f) || |
| 3128 | /* U+3095 HIRAGANA LETTER SMALL KA */ |
| 3129 | (previous[1] == 0x82 && previous[2] == 0x95)) { |
| 3130 | /* U+3042 HIRAGANA LETTER A */ |
| 3131 | unified_buffer[(*n_unified_bytes)++] = previous[0]; |
| 3132 | unified_buffer[(*n_unified_bytes)++] = 0x81; |
| 3133 | unified_buffer[(*n_unified_bytes)++] = 0x82; |
| 3134 | (*n_unified_characters)++; |
| 3135 | return unified_buffer; |
| 3136 | } else if (/* U+3043 HIRAGANA LETTER SMALL I */ |
| 3137 | (previous[1] == 0x81 && previous[2] == 0x83) || |
| 3138 | /* U+3044 HIRAGANA LETTER I */ |
| 3139 | (previous[1] == 0x81 && previous[2] == 0x84) || |
| 3140 | /* U+304D HIRAGANA LETTER KI */ |
| 3141 | (previous[1] == 0x81 && previous[2] == 0x8d) || |
| 3142 | /* U+304E HIRAGANA LETTER GI */ |
| 3143 | (previous[1] == 0x81 && previous[2] == 0x8e) || |
| 3144 | /* U+3057 HIRAGANA LETTER SI */ |
| 3145 | (previous[1] == 0x81 && previous[2] == 0x97) || |
| 3146 | /* U+3058 HIRAGANA LETTER ZI */ |
| 3147 | (previous[1] == 0x81 && previous[2] == 0x98) || |
| 3148 | /* U+3061 HIRAGANA LETTER TI */ |
| 3149 | (previous[1] == 0x81 && previous[2] == 0xa1) || |
| 3150 | /* U+3062 HIRAGANA LETTER DI */ |
| 3151 | (previous[1] == 0x81 && previous[2] == 0xa2) || |
| 3152 | /* U+306B HIRAGANA LETTER NI */ |
| 3153 | (previous[1] == 0x81 && previous[2] == 0xab) || |
| 3154 | /* U+3072 HIRAGANA LETTER HI */ |
| 3155 | (previous[1] == 0x81 && previous[2] == 0xb2) || |
| 3156 | /* U+3073 HIRAGANA LETTER BI */ |
| 3157 | (previous[1] == 0x81 && previous[2] == 0xb3) || |
| 3158 | /* U+3074 HIRAGANA LETTER PI */ |
| 3159 | (previous[1] == 0x81 && previous[2] == 0xb4) || |
| 3160 | /* U+307F HIRAGANA LETTER MI */ |
| 3161 | (previous[1] == 0x81 && previous[2] == 0xbf) || |
| 3162 | /* U+308A HIRAGANA LETTER RI */ |
| 3163 | (previous[1] == 0x82 && previous[2] == 0x8a) || |
| 3164 | /* U+3090 HIRAGANA LETTER WI */ |
| 3165 | (previous[1] == 0x82 && previous[2] == 0x90)) { |
| 3166 | /* U+3044 HIRAGANA LETTER I */ |
| 3167 | unified_buffer[(*n_unified_bytes)++] = previous[0]; |
| 3168 | unified_buffer[(*n_unified_bytes)++] = 0x81; |
| 3169 | unified_buffer[(*n_unified_bytes)++] = 0x84; |
| 3170 | (*n_unified_characters)++; |
| 3171 | return unified_buffer; |
| 3172 | } else if (/* U+3045 HIRAGANA LETTER SMALL U */ |
| 3173 | (previous[1] == 0x81 && previous[2] == 0x85) || |
| 3174 | /* U+3046 HIRAGANA LETTER U */ |
| 3175 | (previous[1] == 0x81 && previous[2] == 0x86) || |
| 3176 | /* U+304F HIRAGANA LETTER KU */ |
| 3177 | (previous[1] == 0x81 && previous[2] == 0x8f) || |
| 3178 | /* U+3050 HIRAGANA LETTER GU */ |
| 3179 | (previous[1] == 0x81 && previous[2] == 0x90) || |
| 3180 | /* U+3059 HIRAGANA LETTER SU */ |
| 3181 | (previous[1] == 0x81 && previous[2] == 0x99) || |
| 3182 | /* U+305A HIRAGANA LETTER ZU */ |
| 3183 | (previous[1] == 0x81 && previous[2] == 0x9a) || |
| 3184 | /* U+3063 HIRAGANA LETTER SMALL TU */ |
| 3185 | (previous[1] == 0x81 && previous[2] == 0xa3) || |
| 3186 | /* U+3064 HIRAGANA LETTER TU */ |
| 3187 | (previous[1] == 0x81 && previous[2] == 0xa4) || |
| 3188 | /* U+3065 HIRAGANA LETTER DU */ |
| 3189 | (previous[1] == 0x81 && previous[2] == 0xa5) || |
| 3190 | /* U+306C HIRAGANA LETTER NU */ |
| 3191 | (previous[1] == 0x81 && previous[2] == 0xac) || |
| 3192 | /* U+3075 HIRAGANA LETTER HU */ |
| 3193 | (previous[1] == 0x81 && previous[2] == 0xb5) || |
| 3194 | /* U+3076 HIRAGANA LETTER BU */ |
| 3195 | (previous[1] == 0x81 && previous[2] == 0xb6) || |
| 3196 | /* U+3077 HIRAGANA LETTER PU */ |
| 3197 | (previous[1] == 0x81 && previous[2] == 0xb7) || |
| 3198 | /* U+3080 HIRAGANA LETTER MU */ |
| 3199 | (previous[1] == 0x82 && previous[2] == 0x80) || |
| 3200 | /* U+3085 HIRAGANA LETTER SMALL YU */ |
| 3201 | (previous[1] == 0x82 && previous[2] == 0x85) || |
| 3202 | /* U+3086 HIRAGANA LETTER YU */ |
| 3203 | (previous[1] == 0x82 && previous[2] == 0x86) || |
| 3204 | /* U+308B HIRAGANA LETTER RU */ |
| 3205 | (previous[1] == 0x82 && previous[2] == 0x8b) || |
| 3206 | /* U+3094 HIRAGANA LETTER VU */ |
| 3207 | (previous[1] == 0x82 && previous[2] == 0x94)) { |
| 3208 | /* U+3046 HIRAGANA LETTER U */ |
| 3209 | unified_buffer[(*n_unified_bytes)++] = previous[0]; |
| 3210 | unified_buffer[(*n_unified_bytes)++] = 0x81; |
| 3211 | unified_buffer[(*n_unified_bytes)++] = 0x86; |
| 3212 | (*n_unified_characters)++; |
| 3213 | return unified_buffer; |
| 3214 | } else if (/* U+3047 HIRAGANA LETTER SMALL E */ |
| 3215 | (previous[1] == 0x81 && previous[2] == 0x87) || |
| 3216 | /* U+3048 HIRAGANA LETTER E */ |
| 3217 | (previous[1] == 0x81 && previous[2] == 0x88) || |
| 3218 | /* U+3051 HIRAGANA LETTER KE */ |
| 3219 | (previous[1] == 0x81 && previous[2] == 0x91) || |
| 3220 | /* U+3052 HIRAGANA LETTER GE */ |
| 3221 | (previous[1] == 0x81 && previous[2] == 0x92) || |
| 3222 | /* U+305B HIRAGANA LETTER SE */ |
| 3223 | (previous[1] == 0x81 && previous[2] == 0x9b) || |
| 3224 | /* U+305C HIRAGANA LETTER ZE */ |
| 3225 | (previous[1] == 0x81 && previous[2] == 0x9c) || |
| 3226 | /* U+3066 HIRAGANA LETTER TE */ |
| 3227 | (previous[1] == 0x81 && previous[2] == 0xa6) || |
| 3228 | /* U+3067 HIRAGANA LETTER DE */ |
| 3229 | (previous[1] == 0x81 && previous[2] == 0xa7) || |
| 3230 | /* U+306D HIRAGANA LETTER NE */ |
| 3231 | (previous[1] == 0x81 && previous[2] == 0xad) || |
| 3232 | /* U+3078 HIRAGANA LETTER HE */ |
| 3233 | (previous[1] == 0x81 && previous[2] == 0xb8) || |
| 3234 | /* U+3079 HIRAGANA LETTER BE */ |
| 3235 | (previous[1] == 0x81 && previous[2] == 0xb9) || |
| 3236 | /* U+307A HIRAGANA LETTER PE */ |
| 3237 | (previous[1] == 0x81 && previous[2] == 0xba) || |
| 3238 | /* U+3081 HIRAGANA LETTER ME */ |
| 3239 | (previous[1] == 0x82 && previous[2] == 0x81) || |
| 3240 | /* U+308C HIRAGANA LETTER RE */ |
| 3241 | (previous[1] == 0x82 && previous[2] == 0x8c) || |
| 3242 | /* U+3096 HIRAGANA LETTER SMALL KE */ |
| 3243 | (previous[1] == 0x82 && previous[2] == 0x96) || |
| 3244 | /* U+3091 HIRAGANA LETTER WE */ |
| 3245 | (previous[1] == 0x82 && previous[2] == 0x91)) { |
| 3246 | /* U+3048 HIRAGANA LETTER E */ |
| 3247 | unified_buffer[(*n_unified_bytes)++] = previous[0]; |
| 3248 | unified_buffer[(*n_unified_bytes)++] = 0x81; |
| 3249 | unified_buffer[(*n_unified_bytes)++] = 0x88; |
| 3250 | (*n_unified_characters)++; |
| 3251 | return unified_buffer; |
| 3252 | } else if (/* U+3049 HIRAGANA LETTER SMALL O */ |
| 3253 | (previous[1] == 0x81 && previous[2] == 0x89) || |
| 3254 | /* U+304A HIRAGANA LETTER O */ |
| 3255 | (previous[1] == 0x81 && previous[2] == 0x8a) || |
| 3256 | /* U+3053 HIRAGANA LETTER KO */ |
| 3257 | (previous[1] == 0x81 && previous[2] == 0x93) || |
| 3258 | /* U+3054 HIRAGANA LETTER GO */ |
| 3259 | (previous[1] == 0x81 && previous[2] == 0x94) || |
| 3260 | /* U+305D HIRAGANA LETTER SO */ |
| 3261 | (previous[1] == 0x81 && previous[2] == 0x9d) || |
| 3262 | /* U+305E HIRAGANA LETTER ZO */ |
| 3263 | (previous[1] == 0x81 && previous[2] == 0x9e) || |
| 3264 | /* U+3068 HIRAGANA LETTER TO */ |
| 3265 | (previous[1] == 0x81 && previous[2] == 0xa8) || |
| 3266 | /* U+3069 HIRAGANA LETTER DO */ |
| 3267 | (previous[1] == 0x81 && previous[2] == 0xa9) || |
| 3268 | /* U+306E HIRAGANA LETTER NO */ |
| 3269 | (previous[1] == 0x81 && previous[2] == 0xae) || |
| 3270 | /* U+307B HIRAGANA LETTER HO */ |
| 3271 | (previous[1] == 0x81 && previous[2] == 0xbb) || |
| 3272 | /* U+307C HIRAGANA LETTER BO */ |
| 3273 | (previous[1] == 0x81 && previous[2] == 0xbc) || |
| 3274 | /* U+307D HIRAGANA LETTER PO */ |
| 3275 | (previous[1] == 0x81 && previous[2] == 0xbd) || |
| 3276 | /* U+3082 HIRAGANA LETTER MO */ |
| 3277 | (previous[1] == 0x82 && previous[2] == 0x82) || |
| 3278 | /* U+3087 HIRAGANA LETTER SMALL YO */ |
| 3279 | (previous[1] == 0x82 && previous[2] == 0x87) || |
| 3280 | /* U+3088 HIRAGANA LETTER YO */ |
| 3281 | (previous[1] == 0x82 && previous[2] == 0x88) || |
| 3282 | /* U+308D HIRAGANA LETTER RO */ |
| 3283 | (previous[1] == 0x82 && previous[2] == 0x8d) || |
| 3284 | /* U+3092 HIRAGANA LETTER WO */ |
| 3285 | (previous[1] == 0x82 && previous[2] == 0x92)) { |
| 3286 | /* U+304A HIRAGANA LETTER O */ |
| 3287 | unified_buffer[(*n_unified_bytes)++] = previous[0]; |
| 3288 | unified_buffer[(*n_unified_bytes)++] = 0x81; |
| 3289 | unified_buffer[(*n_unified_bytes)++] = 0x8a; |
| 3290 | (*n_unified_characters)++; |
| 3291 | return unified_buffer; |
| 3292 | } else if (/* U+3093 HIRAGANA LETTER N */ |
| 3293 | previous[1] == 0x82 && previous[2] == 0x93) { |
| 3294 | /* U+3093 HIRAGANA LETTER N */ |
| 3295 | unified_buffer[(*n_unified_bytes)++] = previous[0]; |
| 3296 | unified_buffer[(*n_unified_bytes)++] = previous[1]; |
| 3297 | unified_buffer[(*n_unified_bytes)++] = previous[2]; |
| 3298 | (*n_unified_characters)++; |
| 3299 | return unified_buffer; |
| 3300 | } else if (/* U+30A1 KATAKANA LETTER SMALL A */ |
| 3301 | (previous[1] == 0x82 && previous[2] == 0xa1) || |
| 3302 | /* U+30A2 KATAKANA LETTER A */ |
| 3303 | (previous[1] == 0x82 && previous[2] == 0xa2) || |
| 3304 | /* U+30AB KATAKANA LETTER KA */ |
| 3305 | (previous[1] == 0x82 && previous[2] == 0xab) || |
| 3306 | /* U+30AC KATAKANA LETTER GA */ |
| 3307 | (previous[1] == 0x82 && previous[2] == 0xac) || |
| 3308 | /* U+30B5 KATAKANA LETTER SA */ |
| 3309 | (previous[1] == 0x82 && previous[2] == 0xb5) || |
| 3310 | /* U+30B6 KATAKANA LETTER ZA */ |
| 3311 | (previous[1] == 0x82 && previous[2] == 0xb6) || |
| 3312 | /* U+30BF KATAKANA LETTER TA */ |
| 3313 | (previous[1] == 0x82 && previous[2] == 0xbf) || |
| 3314 | /* U+30C0 KATAKANA LETTER DA */ |
| 3315 | (previous[1] == 0x83 && previous[2] == 0x80) || |
| 3316 | /* U+30CA KATAKANA LETTER NA */ |
| 3317 | (previous[1] == 0x83 && previous[2] == 0x8a) || |
| 3318 | /* U+30CF KATAKANA LETTER HA */ |
| 3319 | (previous[1] == 0x83 && previous[2] == 0x8f) || |
| 3320 | /* U+30D0 KATAKANA LETTER BA */ |
| 3321 | (previous[1] == 0x83 && previous[2] == 0x90) || |
| 3322 | /* U+30D1 KATAKANA LETTER PA */ |
| 3323 | (previous[1] == 0x83 && previous[2] == 0x91) || |
| 3324 | /* U+30DE KATAKANA LETTER MA */ |
| 3325 | (previous[1] == 0x83 && previous[2] == 0x9e) || |
| 3326 | /* U+30E9 KATAKANA LETTER RA */ |
| 3327 | (previous[1] == 0x83 && previous[2] == 0xa9) || |
| 3328 | /* U+30E3 KATAKANA LETTER SMALL YA */ |
| 3329 | (previous[1] == 0x83 && previous[2] == 0xa3) || |
| 3330 | /* U+30E4 KATAKANA LETTER YA */ |
| 3331 | (previous[1] == 0x83 && previous[2] == 0xa4) || |
| 3332 | /* U+30EE KATAKANA LETTER SMALL WA */ |
| 3333 | (previous[1] == 0x83 && previous[2] == 0xae) || |
| 3334 | /* U+30EF KATAKANA LETTER WA */ |
| 3335 | (previous[1] == 0x83 && previous[2] == 0xaf) || |
| 3336 | /* U+30F5 KATAKANA LETTER SMALL KA */ |
| 3337 | (previous[1] == 0x83 && previous[2] == 0xb5) || |
| 3338 | /* U+30F7 KATAKANA LETTER VA */ |
| 3339 | (previous[1] == 0x83 && previous[2] == 0xb7)) { |
| 3340 | /* U+30A2 KATAKANA LETTER A */ |
| 3341 | unified_buffer[(*n_unified_bytes)++] = previous[0]; |
| 3342 | unified_buffer[(*n_unified_bytes)++] = 0x82; |
| 3343 | unified_buffer[(*n_unified_bytes)++] = 0xa2; |
| 3344 | (*n_unified_characters)++; |
| 3345 | return unified_buffer; |
| 3346 | } else if (/* U+30A3 KATAKANA LETTER SMALL I */ |
| 3347 | (previous[1] == 0x82 && previous[2] == 0xa3) || |
| 3348 | /* U+30A4 KATAKANA LETTER I */ |
| 3349 | (previous[1] == 0x82 && previous[2] == 0xa4) || |
| 3350 | /* U+30AD KATAKANA LETTER KI */ |
| 3351 | (previous[1] == 0x82 && previous[2] == 0xad) || |
| 3352 | /* U+30AE KATAKANA LETTER GI */ |
| 3353 | (previous[1] == 0x82 && previous[2] == 0xae) || |
| 3354 | /* U+30B7 KATAKANA LETTER SI */ |
| 3355 | (previous[1] == 0x82 && previous[2] == 0xb7) || |
| 3356 | /* U+30B8 KATAKANA LETTER ZI */ |
| 3357 | (previous[1] == 0x82 && previous[2] == 0xb8) || |
| 3358 | /* U+30C1 KATAKANA LETTER TI */ |
| 3359 | (previous[1] == 0x83 && previous[2] == 0x81) || |
| 3360 | /* U+30C2 KATAKANA LETTER DI */ |
| 3361 | (previous[1] == 0x83 && previous[2] == 0x82) || |
| 3362 | /* U+30CB KATAKANA LETTER NI */ |
| 3363 | (previous[1] == 0x83 && previous[2] == 0x8b) || |
| 3364 | /* U+30D2 KATAKANA LETTER HI */ |
| 3365 | (previous[1] == 0x83 && previous[2] == 0x92) || |
| 3366 | /* U+30D3 KATAKANA LETTER BI */ |
| 3367 | (previous[1] == 0x83 && previous[2] == 0x93) || |
| 3368 | /* U+30D4 KATAKANA LETTER PI */ |
| 3369 | (previous[1] == 0x83 && previous[2] == 0x94) || |
| 3370 | /* U+30DF KATAKANA LETTER MI */ |
| 3371 | (previous[1] == 0x83 && previous[2] == 0x9f) || |
| 3372 | /* U+30EA KATAKANA LETTER RI */ |
| 3373 | (previous[1] == 0x83 && previous[2] == 0xaa) || |
| 3374 | /* U+30F0 KATAKANA LETTER WI */ |
| 3375 | (previous[1] == 0x83 && previous[2] == 0xb0) || |
| 3376 | /* U+30F8 KATAKANA LETTER VI */ |
| 3377 | (previous[1] == 0x83 && previous[2] == 0xb8)) { |
| 3378 | /* U+30A4 KATAKANA LETTER I */ |
| 3379 | unified_buffer[(*n_unified_bytes)++] = previous[0]; |
| 3380 | unified_buffer[(*n_unified_bytes)++] = 0x82; |
| 3381 | unified_buffer[(*n_unified_bytes)++] = 0xa4; |
| 3382 | (*n_unified_characters)++; |
| 3383 | return unified_buffer; |
| 3384 | } else if (/* U+30A5 KATAKANA LETTER SMALL U */ |
| 3385 | (previous[1] == 0x82 && previous[2] == 0xa5) || |
| 3386 | /* U+30A6 KATAKANA LETTER U */ |
| 3387 | (previous[1] == 0x82 && previous[2] == 0xa6) || |
| 3388 | /* U+30AF KATAKANA LETTER KU */ |
| 3389 | (previous[1] == 0x82 && previous[2] == 0xaf) || |
| 3390 | /* U+30B0 KATAKANA LETTER GU */ |
| 3391 | (previous[1] == 0x82 && previous[2] == 0xb0) || |
| 3392 | /* U+30B9 KATAKANA LETTER SU */ |
| 3393 | (previous[1] == 0x82 && previous[2] == 0xb9) || |
| 3394 | /* U+30BA KATAKANA LETTER ZU */ |
| 3395 | (previous[1] == 0x82 && previous[2] == 0xba) || |
| 3396 | /* U+30C4 KATAKANA LETTER TU */ |
| 3397 | (previous[1] == 0x83 && previous[2] == 0x84) || |
| 3398 | /* U+30C5 KATAKANA LETTER DU */ |
| 3399 | (previous[1] == 0x83 && previous[2] == 0x85) || |
| 3400 | /* U+30CC KATAKANA LETTER NU */ |
| 3401 | (previous[1] == 0x83 && previous[2] == 0x8c) || |
| 3402 | /* U+30D5 KATAKANA LETTER HU */ |
| 3403 | (previous[1] == 0x83 && previous[2] == 0x95) || |
| 3404 | /* U+30D6 KATAKANA LETTER BU */ |
| 3405 | (previous[1] == 0x83 && previous[2] == 0x96) || |
| 3406 | /* U+30D7 KATAKANA LETTER PU */ |
| 3407 | (previous[1] == 0x83 && previous[2] == 0x97) || |
| 3408 | /* U+30E0 KATAKANA LETTER MU */ |
| 3409 | (previous[1] == 0x83 && previous[2] == 0xa0) || |
| 3410 | /* U+30E5 KATAKANA LETTER SMALL YU */ |
| 3411 | (previous[1] == 0x83 && previous[2] == 0xa5) || |
| 3412 | /* U+30E6 KATAKANA LETTER YU */ |
| 3413 | (previous[1] == 0x83 && previous[2] == 0xa6) || |
| 3414 | /* U+30EB KATAKANA LETTER RU */ |
| 3415 | (previous[1] == 0x83 && previous[2] == 0xab) || |
| 3416 | /* U+30F4 KATAKANA LETTER VU */ |
| 3417 | (previous[1] == 0x83 && previous[2] == 0xb4)) { |
| 3418 | /* U+30A6 KATAKANA LETTER U */ |
| 3419 | unified_buffer[(*n_unified_bytes)++] = previous[0]; |
| 3420 | unified_buffer[(*n_unified_bytes)++] = 0x82; |
| 3421 | unified_buffer[(*n_unified_bytes)++] = 0xa6; |
| 3422 | (*n_unified_characters)++; |
| 3423 | return unified_buffer; |
| 3424 | } else if (/* U+30A7 KATAKANA LETTER SMALL E */ |
| 3425 | (previous[1] == 0x82 && previous[2] == 0xa7) || |
| 3426 | /* U+30A8 KATAKANA LETTER E */ |
| 3427 | (previous[1] == 0x82 && previous[2] == 0xa8) || |
| 3428 | /* U+30B1 KATAKANA LETTER KE */ |
| 3429 | (previous[1] == 0x82 && previous[2] == 0xb1) || |
| 3430 | /* U+30B2 KATAKANA LETTER GE */ |
| 3431 | (previous[1] == 0x82 && previous[2] == 0xb2) || |
| 3432 | /* U+30BB KATAKANA LETTER SE */ |
| 3433 | (previous[1] == 0x82 && previous[2] == 0xbb) || |
| 3434 | /* U+30BC KATAKANA LETTER ZE */ |
| 3435 | (previous[1] == 0x82 && previous[2] == 0xbc) || |
| 3436 | /* U+30C6 KATAKANA LETTER TE */ |
| 3437 | (previous[1] == 0x83 && previous[2] == 0x86) || |
| 3438 | /* U+30C7 KATAKANA LETTER DE */ |
| 3439 | (previous[1] == 0x83 && previous[2] == 0x87) || |
| 3440 | /* U+30CD KATAKANA LETTER NE */ |
| 3441 | (previous[1] == 0x83 && previous[2] == 0x8d) || |
| 3442 | /* U+30D8 KATAKANA LETTER HE */ |
| 3443 | (previous[1] == 0x83 && previous[2] == 0x98) || |
| 3444 | /* U+30D9 KATAKANA LETTER BE */ |
| 3445 | (previous[1] == 0x83 && previous[2] == 0x99) || |
| 3446 | /* U+30DA KATAKANA LETTER PE */ |
| 3447 | (previous[1] == 0x83 && previous[2] == 0x9a) || |
| 3448 | /* U+30E1 KATAKANA LETTER ME */ |
| 3449 | (previous[1] == 0x83 && previous[2] == 0xa1) || |
| 3450 | /* U+30EC KATAKANA LETTER RE */ |
| 3451 | (previous[1] == 0x83 && previous[2] == 0xac) || |
| 3452 | /* U+30F1 KATAKANA LETTER WE */ |
| 3453 | (previous[1] == 0x83 && previous[2] == 0xb1) || |
| 3454 | /* U+30F6 KATAKANA LETTER SMALL KE */ |
| 3455 | (previous[1] == 0x83 && previous[2] == 0xb6) || |
| 3456 | /* U+30F9 KATAKANA LETTER VE */ |
| 3457 | (previous[1] == 0x83 && previous[2] == 0xb9)) { |
| 3458 | /* U+30A8 KATAKANA LETTER E */ |
| 3459 | unified_buffer[(*n_unified_bytes)++] = previous[0]; |
| 3460 | unified_buffer[(*n_unified_bytes)++] = 0x82; |
| 3461 | unified_buffer[(*n_unified_bytes)++] = 0xa8; |
| 3462 | (*n_unified_characters)++; |
| 3463 | return unified_buffer; |
| 3464 | } else if (/* U+30A9 KATAKANA LETTER SMALL O */ |
| 3465 | (previous[1] == 0x82 && previous[2] == 0xa9) || |
| 3466 | /* U+30AA KATAKANA LETTER O */ |
| 3467 | (previous[1] == 0x82 && previous[2] == 0xaa) || |
| 3468 | /* U+30B3 KATAKANA LETTER KO */ |
| 3469 | (previous[1] == 0x82 && previous[2] == 0xb3) || |
| 3470 | /* U+30B4 KATAKANA LETTER GO */ |
| 3471 | (previous[1] == 0x82 && previous[2] == 0xb4) || |
| 3472 | /* U+30BD KATAKANA LETTER SO */ |
| 3473 | (previous[1] == 0x82 && previous[2] == 0xbd) || |
| 3474 | /* U+30BE KATAKANA LETTER ZO */ |
| 3475 | (previous[1] == 0x82 && previous[2] == 0xbe) || |
| 3476 | /* U+30C8 KATAKANA LETTER TO */ |
| 3477 | (previous[1] == 0x83 && previous[2] == 0x88) || |
| 3478 | /* U+30C9 KATAKANA LETTER DO */ |
| 3479 | (previous[1] == 0x83 && previous[2] == 0x89) || |
| 3480 | /* U+30CE KATAKANA LETTER NO */ |
| 3481 | (previous[1] == 0x83 && previous[2] == 0x8e) || |
| 3482 | /* U+30DB KATAKANA LETTER HO */ |
| 3483 | (previous[1] == 0x83 && previous[2] == 0x9b) || |
| 3484 | /* U+30DC KATAKANA LETTER BO */ |
| 3485 | (previous[1] == 0x83 && previous[2] == 0x9c) || |
| 3486 | /* U+30DD KATAKANA LETTER PO */ |
| 3487 | (previous[1] == 0x83 && previous[2] == 0x9d) || |
| 3488 | /* U+30E2 KATAKANA LETTER MO */ |
| 3489 | (previous[1] == 0x83 && previous[2] == 0xa2) || |
| 3490 | /* U+30E7 KATAKANA LETTER SMALL YO */ |
| 3491 | (previous[1] == 0x83 && previous[2] == 0xa7) || |
| 3492 | /* U+30E8 KATAKANA LETTER YO */ |
| 3493 | (previous[1] == 0x83 && previous[2] == 0xa8) || |
| 3494 | /* U+30ED KATAKANA LETTER RO */ |
| 3495 | (previous[1] == 0x83 && previous[2] == 0xad) || |
| 3496 | /* U+30F2 KATAKANA LETTER WO */ |
| 3497 | (previous[1] == 0x83 && previous[2] == 0xb2) || |
| 3498 | /* U+30FA KATAKANA LETTER VO */ |
| 3499 | (previous[1] == 0x83 && previous[2] == 0xba)) { |
| 3500 | /* U+30AA KATAKANA LETTER O */ |
| 3501 | unified_buffer[(*n_unified_bytes)++] = previous[0]; |
| 3502 | unified_buffer[(*n_unified_bytes)++] = 0x82; |
| 3503 | unified_buffer[(*n_unified_bytes)++] = 0xaa; |
| 3504 | (*n_unified_characters)++; |
| 3505 | return unified_buffer; |
| 3506 | } else if (/* U+30F3 KATAKANA LETTER N */ |
| 3507 | previous[1] == 0x83 && previous[2] == 0xb3) { |
| 3508 | /* U+30F3 KATAKANA LETTER N */ |
| 3509 | unified_buffer[(*n_unified_bytes)++] = previous[0]; |
| 3510 | unified_buffer[(*n_unified_bytes)++] = previous[1]; |
| 3511 | unified_buffer[(*n_unified_bytes)++] = previous[2]; |
| 3512 | (*n_unified_characters)++; |
| 3513 | return unified_buffer; |
| 3514 | } |
| 3515 | } |
| 3516 | } |
| 3517 | |
| 3518 | *n_unified_bytes = *n_used_bytes; |
| 3519 | *n_unified_characters = *n_used_characters; |
| 3520 | |
| 3521 | return current; |
| 3522 | } |
| 3523 | |
| 3524 | static const unsigned char * |
| 3525 | grn_nfkc_normalize_unify_romaji(grn_ctx *ctx, |
| 3526 | const unsigned char *start, |
| 3527 | const unsigned char *current, |
| 3528 | const unsigned char *end, |
| 3529 | size_t *n_used_bytes, |
| 3530 | size_t *n_used_characters, |
| 3531 | unsigned char *unified_buffer, |
| 3532 | size_t *n_unified_bytes, |
| 3533 | size_t *n_unified_characters, |
| 3534 | void *user_data) |
| 3535 | { |
| 3536 | return grn_romaji_hepburn_convert(ctx, |
| 3537 | current, |
| 3538 | end, |
| 3539 | n_used_bytes, |
| 3540 | n_used_characters, |
| 3541 | unified_buffer, |
| 3542 | n_unified_bytes, |
| 3543 | n_unified_characters); |
| 3544 | } |
| 3545 | |
| 3546 | typedef struct { |
| 3547 | size_t previous_length; |
| 3548 | } grn_nfkc_normalize_iteration_mark_data; |
| 3549 | |
| 3550 | static const unsigned char * |
| 3551 | grn_nfkc_normalize_unify_iteration_mark(grn_ctx *ctx, |
| 3552 | const unsigned char *start, |
| 3553 | const unsigned char *current, |
| 3554 | const unsigned char *end, |
| 3555 | size_t *n_used_bytes, |
| 3556 | size_t *n_used_characters, |
| 3557 | unsigned char *unified_buffer, |
| 3558 | size_t *n_unified_bytes, |
| 3559 | size_t *n_unified_characters, |
| 3560 | void *user_data) |
| 3561 | { |
| 3562 | grn_nfkc_normalize_iteration_mark_data *data = user_data; |
| 3563 | size_t previous_length = data->previous_length; |
| 3564 | size_t current_length = (size_t)grn_charlen_(ctx, current, end, GRN_ENC_UTF8); |
| 3565 | data->previous_length = current_length; |
| 3566 | |
| 3567 | *n_used_bytes = current_length; |
| 3568 | *n_used_characters = 1; |
| 3569 | |
| 3570 | const unsigned char *previous = current - previous_length; |
| 3571 | |
| 3572 | # define N_HIRAGANA_BYTES 3 |
| 3573 | if (current_length == 3 && current[0] == 0xe3 && current[1] == 0x82 && |
| 3574 | current[2] == 0x9d && previous_length == N_HIRAGANA_BYTES && |
| 3575 | GRN_CHAR_TYPE(grn_nfkc_char_type(previous)) == GRN_CHAR_HIRAGANA) { |
| 3576 | /* U+309D HIRAGANA ITERATION MARK */ |
| 3577 | unsigned char unvoiced_buffer[N_HIRAGANA_BYTES]; |
| 3578 | const unsigned char *unvoiced_char = |
| 3579 | grn_nfkc_normalize_unify_hiragana_voiced_sound_mark(previous, |
| 3580 | unvoiced_buffer); |
| 3581 | for (size_t i = 0; i < N_HIRAGANA_BYTES; i++) { |
| 3582 | unified_buffer[(*n_unified_bytes)++] = unvoiced_char[i]; |
| 3583 | } |
| 3584 | data->previous_length = N_HIRAGANA_BYTES; |
| 3585 | (*n_unified_characters)++; |
| 3586 | return unified_buffer; |
| 3587 | } else if (current_length == 3 && current[0] == 0xe3 && current[1] == 0x82 && |
| 3588 | current[2] == 0x9e && previous_length == N_HIRAGANA_BYTES && |
| 3589 | GRN_CHAR_TYPE(grn_nfkc_char_type(previous)) == GRN_CHAR_HIRAGANA) { |
| 3590 | /* U+309E HIRAGANA VOICED ITERATION MARK */ |
| 3591 | const bool processed = |
| 3592 | grn_nfkc_normalize_process_hiragana_voiced_iteration_mark(previous, |
| 3593 | unified_buffer); |
| 3594 | if (processed) { |
| 3595 | *n_unified_bytes += N_HIRAGANA_BYTES; |
| 3596 | data->previous_length = N_HIRAGANA_BYTES; |
| 3597 | (*n_unified_characters)++; |
| 3598 | return unified_buffer; |
| 3599 | } |
| 3600 | } |
| 3601 | # undef N_HIRAGANA_BYTES |
| 3602 | # define N_KATAKANA_BYTES 3 |
| 3603 | else if (current_length == 3 && current[0] == 0xe3 && current[1] == 0x83 && |
| 3604 | current[2] == 0xbd && previous_length == N_KATAKANA_BYTES && |
| 3605 | GRN_CHAR_TYPE(grn_nfkc_char_type(previous)) == GRN_CHAR_KATAKANA) { |
| 3606 | /* U+30FD KATAKANA ITERATION MARK */ |
| 3607 | unsigned char unvoiced_buffer[N_KATAKANA_BYTES]; |
| 3608 | const unsigned char *unvoiced_char = |
| 3609 | grn_nfkc_normalize_unify_katakana_voiced_sound_mark(previous, |
| 3610 | unvoiced_buffer); |
| 3611 | for (size_t i = 0; i < N_KATAKANA_BYTES; i++) { |
| 3612 | unified_buffer[(*n_unified_bytes)++] = unvoiced_char[i]; |
| 3613 | } |
| 3614 | data->previous_length = N_KATAKANA_BYTES; |
| 3615 | (*n_unified_characters)++; |
| 3616 | return unified_buffer; |
| 3617 | } else if (current_length == 3 && current[0] == 0xe3 && current[1] == 0x83 && |
| 3618 | current[2] == 0xbe && previous_length == N_KATAKANA_BYTES && |
| 3619 | GRN_CHAR_TYPE(grn_nfkc_char_type(previous)) == GRN_CHAR_KATAKANA) { |
| 3620 | /* U+30FE KATAKANA VOICED ITERATION MARK */ |
| 3621 | const bool processed = |
| 3622 | grn_nfkc_normalize_process_katakana_voiced_iteration_mark(previous, |
| 3623 | unified_buffer); |
| 3624 | if (processed) { |
| 3625 | *n_unified_bytes += N_KATAKANA_BYTES; |
| 3626 | data->previous_length = N_KATAKANA_BYTES; |
| 3627 | (*n_unified_characters)++; |
| 3628 | return unified_buffer; |
| 3629 | } |
| 3630 | } |
| 3631 | # undef N_KATAKANA_BYTES |
| 3632 | else if (current_length == 3 && current[0] == 0xe3 && current[1] == 0x80 && |
| 3633 | (current[2] == 0x85 || current[2] == 0xbb) && previous_length >= 3 && |
| 3634 | GRN_CHAR_TYPE(grn_nfkc_char_type(previous)) == GRN_CHAR_KANJI) { |
| 3635 | /** |
| 3636 | * U+3005 IDEOGRAPHIC ITERATION MARK |
| 3637 | * U+303B VERTICAL IDEOGRAPHIC ITERATION MARK |
| 3638 | * For kanji iteration mark, we simply repeat the previous kanji character. |
| 3639 | * This implementation only handles simple cases as follows. |
| 3640 | * - U+5404 U+3005 -> U+5404 U+5404 |
| 3641 | * ("CJK UNIFIED IDEOGRAPH-5404" "IDEOGRAPHIC ITERATION MARK" -> |
| 3642 | * "CJK UNIFIED IDEOGRAPH-5404" "CJK UNIFIED IDEOGRAPH-5404") |
| 3643 | * - U+2000B U+3005 -> U+2000B U+2000B |
| 3644 | * ("CJK UNIFIED IDEOGRAPH-2000B" "IDEOGRAPHIC ITERATION MARK" -> |
| 3645 | * "CJK UNIFIED IDEOGRAPH-2000B" "CJK UNIFIED IDEOGRAPH-2000B") |
| 3646 | * More complex patterns are not supported as follows. |
| 3647 | * - 2 or more characters are iterated: |
| 3648 | * U+90E8 U+5206 U+3005 U+3005 -> U+90E8 U+5206 U+90E8 U+5206 |
| 3649 | * ("CJK UNIFIED IDEOGRAPH-90E8" "CJK UNIFIED IDEOGRAPH-5206" |
| 3650 | * "IDEOGRAPHIC ITERATION MARK" |
| 3651 | * "IDEOGRAPHIC ITERATION MARK" -> |
| 3652 | * "CJK UNIFIED IDEOGRAPH-90E8" "CJK UNIFIED IDEOGRAPH-5206" |
| 3653 | * "CJK UNIFIED IDEOGRAPH-90E8" "CJK UNIFIED IDEOGRAPH-5206") |
| 3654 | * - The same character is iterated multiple times: |
| 3655 | * U+53E4 U+3005 U+3005 U+7C73 -> U+53E4 U+53E4 U+53E4 U+7C73 |
| 3656 | * ("CJK UNIFIED IDEOGRAPH-53E4" "IDEOGRAPHIC ITERATION MARK" |
| 3657 | * "IDEOGRAPHIC ITERATION MARK" "CJK UNIFIED IDEOGRAPH-7C73" -> |
| 3658 | * "CJK UNIFIED IDEOGRAPH-53E4" "CJK UNIFIED IDEOGRAPH-53E4" |
| 3659 | * "CJK UNIFIED IDEOGRAPH-53E4" "CJK UNIFIED IDEOGRAPH-7C73") |
| 3660 | */ |
| 3661 | for (size_t i = 0; i < previous_length; i++) { |
| 3662 | unified_buffer[(*n_unified_bytes)++] = previous[i]; |
| 3663 | } |
| 3664 | data->previous_length = previous_length; |
| 3665 | (*n_unified_characters)++; |
| 3666 | return unified_buffer; |
| 3667 | } |
| 3668 | |
| 3669 | *n_unified_bytes = *n_used_bytes; |
| 3670 | *n_unified_characters = *n_used_characters; |
| 3671 | |
| 3672 | return current; |
| 3673 | } |
| 3674 | |
| 3675 | static const unsigned char * |
| 3676 | grn_nfkc_normalize_strip(grn_ctx *ctx, |
| 3677 | const unsigned char *start, |
| 3678 | const unsigned char *current, |
| 3679 | const unsigned char *end, |
| 3680 | size_t *n_used_bytes, |
| 3681 | size_t *n_used_characters, |
| 3682 | unsigned char *unified_buffer, |
| 3683 | size_t *n_unified_bytes, |
| 3684 | size_t *n_unified_characters, |
| 3685 | void *user_data) |
| 3686 | { |
| 3687 | bool *striped_from_end = user_data; |
| 3688 | const bool from_start = (start == current); |
| 3689 | const unsigned char *current_keep = current; |
| 3690 | while (current < end) { |
| 3691 | size_t char_length = (size_t)grn_charlen_(ctx, current, end, GRN_ENC_UTF8); |
| 3692 | (*n_used_bytes) += char_length; |
| 3693 | (*n_used_characters)++; |
| 3694 | |
| 3695 | const bool is_blank = (char_length == 1 && current[0] == ' '); |
| 3696 | if (!is_blank) { |
| 3697 | if (from_start) { |
| 3698 | *n_unified_bytes = char_length; |
| 3699 | *n_unified_characters = 1; |
| 3700 | return current; |
| 3701 | } else { |
| 3702 | *n_unified_bytes = *n_used_bytes; |
| 3703 | *n_unified_characters = *n_used_characters; |
| 3704 | return current_keep; |
| 3705 | } |
| 3706 | } |
| 3707 | |
| 3708 | current += char_length; |
| 3709 | } |
| 3710 | *striped_from_end = true; |
| 3711 | return current; |
| 3712 | } |
| 3713 | |
| 3714 | static inline void |
| 3715 | grn_nfkc_normalize_unify_stateful(grn_ctx *ctx, |
| 3716 | grn_nfkc_normalize_data *data, |
| 3717 | grn_nfkc_normalize_context *unify, |
| 3718 | grn_nfkc_normalize_unify_stateful_func func, |
| 3719 | void *func_data, |
| 3720 | const char *context_tag) |
| 3721 | { |
| 3722 | const unsigned char *start = data->context.dest; |
| 3723 | const unsigned char *current = start; |
| 3724 | const unsigned char *end = data->context.d; |
| 3725 | size_t i_byte = 0; |
| 3726 | size_t i_character = 0; |
| 3727 | |
| 3728 | while (current < end) { |
| 3729 | size_t n_used_bytes = 0; |
| 3730 | size_t n_used_characters = 0; |
| 3731 | const unsigned char *unified; |
| 3732 | unsigned char unified_buffer[64]; /* Will be enough. */ |
| 3733 | size_t n_unified_bytes = 0; |
| 3734 | size_t n_unified_characters = 0; |
| 3735 | |
| 3736 | unified = func(ctx, |
| 3737 | start, |
| 3738 | current, |
| 3739 | end, |
| 3740 | &n_used_bytes, |
| 3741 | &n_used_characters, |
| 3742 | unified_buffer, |
| 3743 | &n_unified_bytes, |
| 3744 | &n_unified_characters, |
| 3745 | func_data); |
| 3746 | |
| 3747 | if (unify->d + n_unified_bytes >= unify->dest_end) { |
| 3748 | grn_nfkc_normalize_context_expand(ctx, |
| 3749 | unify, |
| 3750 | n_unified_bytes, |
| 3751 | context_tag); |
| 3752 | if (ctx->rc != GRN_SUCCESS) { |
| 3753 | return; |
| 3754 | } |
| 3755 | } |
| 3756 | |
| 3757 | if (unified == current && n_unified_bytes == n_used_bytes) { |
| 3758 | grn_memcpy(unify->d, unified, n_unified_bytes); |
| 3759 | unify->d += n_unified_bytes; |
| 3760 | unify->n_characters += n_unified_characters; |
| 3761 | if (unify->t) { |
| 3762 | memcpy(unify->t, |
| 3763 | data->context.types + i_character, |
| 3764 | sizeof(uint8_t) * n_unified_characters); |
| 3765 | unify->t += n_unified_characters; |
| 3766 | } |
| 3767 | if (unify->c) { |
| 3768 | memcpy(unify->c, |
| 3769 | data->context.checks + i_byte, |
| 3770 | sizeof(int16_t) * n_unified_bytes); |
| 3771 | unify->c += n_unified_bytes; |
| 3772 | unify->c[0] = 0; |
| 3773 | } |
| 3774 | if (unify->o) { |
| 3775 | memcpy(unify->o, |
| 3776 | data->context.offsets + i_character, |
| 3777 | sizeof(uint64_t) * n_unified_characters); |
| 3778 | unify->o += n_unified_characters; |
| 3779 | } |
| 3780 | } else { |
| 3781 | if (n_unified_bytes > 0) { |
| 3782 | grn_memcpy(unify->d, unified, n_unified_bytes); |
| 3783 | |
| 3784 | unify->d += n_unified_bytes; |
| 3785 | unify->n_characters += n_unified_characters; |
| 3786 | if (unify->t || unify->c || unify->o) { |
| 3787 | const unsigned char *unified_current = unified; |
| 3788 | const unsigned char *unified_end = unified + n_unified_bytes; |
| 3789 | |
| 3790 | while (unified_current < unified_end) { |
| 3791 | size_t unified_char_length; |
| 3792 | |
| 3793 | unified_char_length = (size_t) |
| 3794 | grn_charlen_(ctx, unified_current, unified_end, GRN_ENC_UTF8); |
| 3795 | if (unify->t) { |
| 3796 | grn_char_type unified_char_type = |
| 3797 | data->options->char_type_func(unified_current); |
| 3798 | *(unify->t++) = (uint8_t)unified_char_type; |
| 3799 | } |
| 3800 | if (unify->c) { |
| 3801 | size_t i; |
| 3802 | if (unified_current == unified) { |
| 3803 | unify->c[0] = data->context.checks[i_byte]; |
| 3804 | for (i = 1; i < n_used_bytes; i++) { |
| 3805 | if (data->context.checks[i_byte + i] > 0) { |
| 3806 | unify->c[0] += data->context.checks[i_byte + i]; |
| 3807 | } |
| 3808 | } |
| 3809 | unify->c++; |
| 3810 | } else { |
| 3811 | *(unify->c++) = -1; |
| 3812 | } |
| 3813 | for (i = 1; i < unified_char_length; i++) { |
| 3814 | *(unify->c++) = 0; |
| 3815 | } |
| 3816 | unify->c[0] = 0; |
| 3817 | } |
| 3818 | if (unify->o) { |
| 3819 | *(unify->o++) = data->context.offsets[i_character]; |
| 3820 | } |
| 3821 | unified_current += unified_char_length; |
| 3822 | } |
| 3823 | } |
| 3824 | } |
| 3825 | } |
| 3826 | |
| 3827 | i_byte += n_used_bytes; |
| 3828 | current += n_used_bytes; |
| 3829 | i_character += n_used_characters; |
| 3830 | } |
| 3831 | } |
| 3832 | |
| 3833 | static void |
| 3834 | grn_nfkc_normalize_unify(grn_ctx *ctx, grn_nfkc_normalize_data *data) |
| 3835 | { |
| 3836 | grn_nfkc_normalize_context unify; |
| 3837 | bool need_swap = false; |
| 3838 | /* If both `unify_hyphen_and_prolonged_sound_mark` and `remove_symbol` are |
| 3839 | * enabled, unification is not necessary because the hyphen has already been |
| 3840 | * removed. Same for `unify_middle_dot`. */ |
| 3841 | bool unify_hyphen_and_prolonged_sound_mark = |
| 3842 | (data->options->unify_hyphen_and_prolonged_sound_mark && |
| 3843 | !data->options->remove_symbol); |
| 3844 | bool unify_middle_dot = |
| 3845 | (data->options->unify_middle_dot && !data->options->remove_symbol); |
| 3846 | |
| 3847 | if (!(data->options->unify_latin_alphabet_with || data->options->unify_kana || |
| 3848 | data->options->unify_kana_case || |
| 3849 | data->options->unify_kana_voiced_sound_mark || |
| 3850 | data->options->unify_hyphen || |
| 3851 | data->options->unify_prolonged_sound_mark || |
| 3852 | unify_hyphen_and_prolonged_sound_mark || unify_middle_dot || |
| 3853 | data->options->unify_katakana_v_sounds || |
| 3854 | data->options->unify_katakana_bu_sound || |
| 3855 | data->options->unify_katakana_du_small_sounds || |
| 3856 | data->options->unify_katakana_du_sound || |
| 3857 | data->options->unify_katakana_zu_small_sounds || |
| 3858 | data->options->unify_katakana_wo_sound || |
| 3859 | data->options->unify_katakana_di_sound || |
| 3860 | data->options->unify_katakana_gu_small_sounds || |
| 3861 | data->options->unify_kana_hyphen || |
| 3862 | data->options->unify_kana_prolonged_sound_mark || |
| 3863 | data->options->unify_katakana_trailing_o || |
| 3864 | data->options->unify_iteration_mark || data->options->unify_to_romaji || |
| 3865 | data->options->unify_to_katakana || data->options->strip)) { |
| 3866 | return; |
| 3867 | } |
| 3868 | |
| 3869 | memset(&unify, 0, sizeof(grn_nfkc_normalize_context)); |
| 3870 | unify.size = data->context.size; |
| 3871 | grn_nfkc_normalize_context_init(ctx, |
| 3872 | &unify, |
| 3873 | data->context.checks != NULL, |
| 3874 | data->context.types != NULL, |
| 3875 | data->context.offsets != NULL, |
| 3876 | "[unify]"); |
| 3877 | if (ctx->rc != GRN_SUCCESS) { |
| 3878 | goto exit; |
| 3879 | } |
| 3880 | |
| 3881 | if (data->options->unify_latin_alphabet_with || data->options->unify_kana || |
| 3882 | data->options->unify_kana_voiced_sound_mark || |
| 3883 | data->options->unify_hyphen || |
| 3884 | data->options->unify_prolonged_sound_mark || |
| 3885 | unify_hyphen_and_prolonged_sound_mark || unify_middle_dot || |
| 3886 | data->options->unify_to_katakana) { |
| 3887 | grn_nfkc_normalize_unify_stateless(ctx, data, &unify, true); |
| 3888 | if (ctx->rc != GRN_SUCCESS) { |
| 3889 | goto exit; |
| 3890 | } |
| 3891 | need_swap = true; |
| 3892 | } |
| 3893 | |
| 3894 | if (data->options->unify_katakana_v_sounds) { |
| 3895 | if (need_swap) { |
| 3896 | grn_nfkc_normalize_context_swap(ctx, &(data->context), &unify); |
| 3897 | grn_nfkc_normalize_context_rewind(ctx, &unify); |
| 3898 | } |
| 3899 | grn_nfkc_normalize_unify_stateful( |
| 3900 | ctx, |
| 3901 | data, |
| 3902 | &unify, |
| 3903 | grn_nfkc_normalize_unify_katakana_v_sounds, |
| 3904 | NULL, |
| 3905 | "[unify][katakana-v-sounds]"); |
| 3906 | if (ctx->rc != GRN_SUCCESS) { |
| 3907 | goto exit; |
| 3908 | } |
| 3909 | need_swap = true; |
| 3910 | } |
| 3911 | |
| 3912 | if (data->options->unify_katakana_bu_sound) { |
| 3913 | if (need_swap) { |
| 3914 | grn_nfkc_normalize_context_swap(ctx, &(data->context), &unify); |
| 3915 | grn_nfkc_normalize_context_rewind(ctx, &unify); |
| 3916 | } |
| 3917 | grn_nfkc_normalize_unify_stateful( |
| 3918 | ctx, |
| 3919 | data, |
| 3920 | &unify, |
| 3921 | grn_nfkc_normalize_unify_katakana_bu_sound, |
| 3922 | NULL, |
| 3923 | "[unify][katakana-bu-sound]"); |
| 3924 | if (ctx->rc != GRN_SUCCESS) { |
| 3925 | goto exit; |
| 3926 | } |
| 3927 | need_swap = true; |
| 3928 | } |
| 3929 | |
| 3930 | if (data->options->unify_katakana_du_small_sounds) { |
| 3931 | if (need_swap) { |
| 3932 | grn_nfkc_normalize_context_swap(ctx, &(data->context), &unify); |
| 3933 | grn_nfkc_normalize_context_rewind(ctx, &unify); |
| 3934 | } |
| 3935 | grn_nfkc_normalize_unify_stateful( |
| 3936 | ctx, |
| 3937 | data, |
| 3938 | &unify, |
| 3939 | grn_nfkc_normalize_unify_katakana_du_small_sounds, |
| 3940 | NULL, |
| 3941 | "[unify][katakana-du-small-sounds]"); |
| 3942 | if (ctx->rc != GRN_SUCCESS) { |
| 3943 | goto exit; |
| 3944 | } |
| 3945 | need_swap = true; |
| 3946 | } |
| 3947 | |
| 3948 | if (data->options->unify_katakana_du_sound) { |
| 3949 | if (need_swap) { |
| 3950 | grn_nfkc_normalize_context_swap(ctx, &(data->context), &unify); |
| 3951 | grn_nfkc_normalize_context_rewind(ctx, &unify); |
| 3952 | } |
| 3953 | grn_nfkc_normalize_unify_stateful( |
| 3954 | ctx, |
| 3955 | data, |
| 3956 | &unify, |
| 3957 | grn_nfkc_normalize_unify_katakana_du_sound, |
| 3958 | NULL, |
| 3959 | "[unify][katakana-du-sound]"); |
| 3960 | if (ctx->rc != GRN_SUCCESS) { |
| 3961 | goto exit; |
| 3962 | } |
| 3963 | need_swap = true; |
| 3964 | } |
| 3965 | |
| 3966 | if (data->options->unify_katakana_zu_small_sounds) { |
| 3967 | if (need_swap) { |
| 3968 | grn_nfkc_normalize_context_swap(ctx, &(data->context), &unify); |
| 3969 | grn_nfkc_normalize_context_rewind(ctx, &unify); |
| 3970 | } |
| 3971 | grn_nfkc_normalize_unify_stateful( |
| 3972 | ctx, |
| 3973 | data, |
| 3974 | &unify, |
| 3975 | grn_nfkc_normalize_unify_katakana_zu_small_sounds, |
| 3976 | NULL, |
| 3977 | "[unify][katakana-zu-small-sounds]"); |
| 3978 | if (ctx->rc != GRN_SUCCESS) { |
| 3979 | goto exit; |
| 3980 | } |
| 3981 | need_swap = true; |
| 3982 | } |
| 3983 | |
| 3984 | if (data->options->unify_katakana_wo_sound) { |
| 3985 | if (need_swap) { |
| 3986 | grn_nfkc_normalize_context_swap(ctx, &(data->context), &unify); |
| 3987 | grn_nfkc_normalize_context_rewind(ctx, &unify); |
| 3988 | } |
| 3989 | grn_nfkc_normalize_unify_stateful( |
| 3990 | ctx, |
| 3991 | data, |
| 3992 | &unify, |
| 3993 | grn_nfkc_normalize_unify_katakana_wo_sound, |
| 3994 | NULL, |
| 3995 | "[unify][katakana-wo-sound]"); |
| 3996 | if (ctx->rc != GRN_SUCCESS) { |
| 3997 | goto exit; |
| 3998 | } |
| 3999 | need_swap = true; |
| 4000 | } |
| 4001 | |
| 4002 | if (data->options->unify_katakana_di_sound) { |
| 4003 | if (need_swap) { |
| 4004 | grn_nfkc_normalize_context_swap(ctx, &(data->context), &unify); |
| 4005 | grn_nfkc_normalize_context_rewind(ctx, &unify); |
| 4006 | } |
| 4007 | grn_nfkc_normalize_unify_stateful( |
| 4008 | ctx, |
| 4009 | data, |
| 4010 | &unify, |
| 4011 | grn_nfkc_normalize_unify_katakana_di_sound, |
| 4012 | NULL, |
| 4013 | "[unify][katakana-di-sound]"); |
| 4014 | if (ctx->rc != GRN_SUCCESS) { |
| 4015 | goto exit; |
| 4016 | } |
| 4017 | need_swap = true; |
| 4018 | } |
| 4019 | |
| 4020 | if (data->options->unify_katakana_gu_small_sounds) { |
| 4021 | if (need_swap) { |
| 4022 | grn_nfkc_normalize_context_swap(ctx, &(data->context), &unify); |
| 4023 | grn_nfkc_normalize_context_rewind(ctx, &unify); |
| 4024 | } |
| 4025 | grn_nfkc_normalize_unify_stateful( |
| 4026 | ctx, |
| 4027 | data, |
| 4028 | &unify, |
| 4029 | grn_nfkc_normalize_unify_katakana_gu_small_sounds, |
| 4030 | NULL, |
| 4031 | "[unify][katakana-gu-small-sounds]"); |
| 4032 | if (ctx->rc != GRN_SUCCESS) { |
| 4033 | goto exit; |
| 4034 | } |
| 4035 | need_swap = true; |
| 4036 | } |
| 4037 | |
| 4038 | if (data->options->unify_kana_hyphen) { |
| 4039 | if (need_swap) { |
| 4040 | grn_nfkc_normalize_context_swap(ctx, &(data->context), &unify); |
| 4041 | grn_nfkc_normalize_context_rewind(ctx, &unify); |
| 4042 | } |
| 4043 | grn_nfkc_normalize_prolonged_sound_mark_like_data subdata; |
| 4044 | subdata.is_target_char = grn_nfkc_normalize_is_hyphen; |
| 4045 | subdata.previous_length = 0; |
| 4046 | grn_nfkc_normalize_unify_stateful( |
| 4047 | ctx, |
| 4048 | data, |
| 4049 | &unify, |
| 4050 | grn_nfkc_normalize_unify_kana_prolonged_sound_mark_like, |
| 4051 | &subdata, |
| 4052 | "[unify][kana-hyphen]"); |
| 4053 | if (ctx->rc != GRN_SUCCESS) { |
| 4054 | goto exit; |
| 4055 | } |
| 4056 | need_swap = true; |
| 4057 | } |
| 4058 | |
| 4059 | if (data->options->unify_kana_prolonged_sound_mark) { |
| 4060 | if (need_swap) { |
| 4061 | grn_nfkc_normalize_context_swap(ctx, &(data->context), &unify); |
| 4062 | grn_nfkc_normalize_context_rewind(ctx, &unify); |
| 4063 | } |
| 4064 | grn_nfkc_normalize_prolonged_sound_mark_like_data subdata; |
| 4065 | subdata.is_target_char = grn_nfkc_normalize_is_prolonged_sound_mark; |
| 4066 | subdata.previous_length = 0; |
| 4067 | grn_nfkc_normalize_unify_stateful( |
| 4068 | ctx, |
| 4069 | data, |
| 4070 | &unify, |
| 4071 | grn_nfkc_normalize_unify_kana_prolonged_sound_mark_like, |
| 4072 | &subdata, |
| 4073 | "[unify][kana-prolonged-sound-mark]"); |
| 4074 | if (ctx->rc != GRN_SUCCESS) { |
| 4075 | goto exit; |
| 4076 | } |
| 4077 | need_swap = true; |
| 4078 | } |
| 4079 | |
| 4080 | if (data->options->unify_katakana_trailing_o) { |
| 4081 | if (need_swap) { |
| 4082 | grn_nfkc_normalize_context_swap(ctx, &(data->context), &unify); |
| 4083 | grn_nfkc_normalize_context_rewind(ctx, &unify); |
| 4084 | } |
| 4085 | bool need_trailing_check = false; |
| 4086 | grn_nfkc_normalize_unify_stateful( |
| 4087 | ctx, |
| 4088 | data, |
| 4089 | &unify, |
| 4090 | grn_nfkc_normalize_unify_katakana_trailing_o, |
| 4091 | &need_trailing_check, |
| 4092 | "[unify][katakana-trailing-o]"); |
| 4093 | if (ctx->rc != GRN_SUCCESS) { |
| 4094 | goto exit; |
| 4095 | } |
| 4096 | need_swap = true; |
| 4097 | } |
| 4098 | |
| 4099 | if (data->options->unify_iteration_mark) { |
| 4100 | if (need_swap) { |
| 4101 | grn_nfkc_normalize_context_swap(ctx, &(data->context), &unify); |
| 4102 | grn_nfkc_normalize_context_rewind(ctx, &unify); |
| 4103 | } |
| 4104 | grn_nfkc_normalize_iteration_mark_data subdata; |
| 4105 | subdata.previous_length = 0; |
| 4106 | grn_nfkc_normalize_unify_stateful(ctx, |
| 4107 | data, |
| 4108 | &unify, |
| 4109 | grn_nfkc_normalize_unify_iteration_mark, |
| 4110 | &subdata, |
| 4111 | "[unify][iteration-mark]"); |
| 4112 | if (ctx->rc != GRN_SUCCESS) { |
| 4113 | goto exit; |
| 4114 | } |
| 4115 | need_swap = true; |
| 4116 | } |
| 4117 | |
| 4118 | if (data->options->unify_kana_case) { |
| 4119 | if (need_swap) { |
| 4120 | grn_nfkc_normalize_context_swap(ctx, &(data->context), &unify); |
| 4121 | grn_nfkc_normalize_context_rewind(ctx, &unify); |
| 4122 | } |
| 4123 | grn_nfkc_normalize_unify_stateless(ctx, data, &unify, false); |
| 4124 | if (ctx->rc != GRN_SUCCESS) { |
| 4125 | goto exit; |
| 4126 | } |
| 4127 | need_swap = true; |
| 4128 | } |
| 4129 | |
| 4130 | if (data->options->unify_to_romaji) { |
| 4131 | if (need_swap) { |
| 4132 | grn_nfkc_normalize_context_swap(ctx, &(data->context), &unify); |
| 4133 | grn_nfkc_normalize_context_rewind(ctx, &unify); |
| 4134 | } |
| 4135 | grn_nfkc_normalize_unify_stateful(ctx, |
| 4136 | data, |
| 4137 | &unify, |
| 4138 | grn_nfkc_normalize_unify_romaji, |
| 4139 | NULL, |
| 4140 | "[unify][romaji]"); |
| 4141 | if (ctx->rc != GRN_SUCCESS) { |
| 4142 | goto exit; |
| 4143 | } |
| 4144 | need_swap = true; |
| 4145 | } |
| 4146 | |
| 4147 | if (data->options->strip) { |
| 4148 | if (need_swap) { |
| 4149 | grn_nfkc_normalize_context_swap(ctx, &(data->context), &unify); |
| 4150 | grn_nfkc_normalize_context_rewind(ctx, &unify); |
| 4151 | } |
| 4152 | bool striped_from_end = false; |
| 4153 | grn_nfkc_normalize_unify_stateful(ctx, |
| 4154 | data, |
| 4155 | &unify, |
| 4156 | grn_nfkc_normalize_strip, |
| 4157 | &striped_from_end, |
| 4158 | "[strip]"); |
| 4159 | if (ctx->rc != GRN_SUCCESS) { |
| 4160 | goto exit; |
| 4161 | } |
| 4162 | if (striped_from_end) { |
| 4163 | if (unify.types && unify.n_characters > 0) { |
| 4164 | unify.types[unify.n_characters - 1] |= GRN_CHAR_BLANK; |
| 4165 | } |
| 4166 | } |
| 4167 | need_swap = true; |
| 4168 | } |
| 4169 | |
| 4170 | grn_nfkc_normalize_context_fin(ctx, &(data->context)); |
| 4171 | |
| 4172 | data->context.size = unify.size; |
| 4173 | |
| 4174 | data->context.dest = unify.dest; |
| 4175 | data->context.d = unify.d; |
| 4176 | data->context.d_ = unify.d_; |
| 4177 | data->context.n_characters = unify.n_characters; |
| 4178 | unify.dest = NULL; |
| 4179 | |
| 4180 | data->context.checks = unify.checks; |
| 4181 | data->context.c = unify.c; |
| 4182 | unify.checks = NULL; |
| 4183 | |
| 4184 | data->context.types = unify.types; |
| 4185 | data->context.t = unify.t; |
| 4186 | unify.types = NULL; |
| 4187 | |
| 4188 | data->context.offsets = unify.offsets; |
| 4189 | data->context.o = unify.o; |
| 4190 | unify.offsets = NULL; |
| 4191 | |
| 4192 | exit: |
| 4193 | grn_nfkc_normalize_context_fin(ctx, &unify); |
| 4194 | } |
| 4195 | |
| 4196 | static inline bool |
| 4197 | grn_nfkc_normalize_remove_target_blank_character_p( |
| 4198 | grn_ctx *ctx, |
| 4199 | const grn_nfkc_normalize_data *data, |
| 4200 | const unsigned char *current, |
| 4201 | size_t current_length) |
| 4202 | { |
| 4203 | if (current_length != 1) { |
| 4204 | return false; |
| 4205 | } |
| 4206 | |
| 4207 | if (current[0] > ' ') { |
| 4208 | return false; |
| 4209 | } |
| 4210 | |
| 4211 | switch (current[0]) { |
| 4212 | case ' ': |
| 4213 | return data->remove_blank_p; |
| 4214 | case '\r': |
| 4215 | case '\n': |
| 4216 | return data->options->remove_new_line; |
| 4217 | default: |
| 4218 | /* skip unprintable ascii */ |
| 4219 | return true; |
| 4220 | } |
| 4221 | } |
| 4222 | |
| 4223 | static inline bool |
| 4224 | grn_nfkc_normalize_remove_target_non_blank_character_p( |
| 4225 | grn_ctx *ctx, |
| 4226 | const grn_nfkc_normalize_data *data, |
| 4227 | const unsigned char *current, |
| 4228 | size_t current_length) |
| 4229 | { |
| 4230 | if (!data->options->remove_symbol) { |
| 4231 | return false; |
| 4232 | } |
| 4233 | |
| 4234 | if (data->options->char_type_func(current) == GRN_CHAR_SYMBOL) { |
| 4235 | return true; |
| 4236 | } |
| 4237 | |
| 4238 | /* `unify_hyphen_and_prolonged_sound_mark` is `true` and the unified hyphen is |
| 4239 | * handled as a symbol. |
| 4240 | * |
| 4241 | * The best implementation is to normalize and then remove the symbols. |
| 4242 | * However, at the time of this change, that way had a large impact, so this |
| 4243 | * is the implementation. */ |
| 4244 | if (data->options->unify_hyphen_and_prolonged_sound_mark && |
| 4245 | (grn_nfkc_normalize_is_hyphen_family(current, current_length) || |
| 4246 | grn_nfkc_normalize_is_prolonged_sound_mark_family(current, |
| 4247 | current_length))) { |
| 4248 | return true; |
| 4249 | } |
| 4250 | /* If `unify_middle_dot` is `true`, middle dot is removed |
| 4251 | * as in `unify_hyphen_and_prolonged_sound_mark`. */ |
| 4252 | if (data->options->unify_middle_dot && |
| 4253 | grn_nfkc_normalize_is_middle_dot_family(current, current_length)) { |
| 4254 | return true; |
| 4255 | } |
| 4256 | |
| 4257 | return false; |
| 4258 | } |
| 4259 | |
| 4260 | grn_rc |
| 4261 | grn_nfkc_normalize(grn_ctx *ctx, |
| 4262 | grn_obj *string, |
| 4263 | grn_nfkc_normalize_options *options) |
| 4264 | { |
| 4265 | grn_nfkc_normalize_data data; |
| 4266 | const unsigned char *source; |
| 4267 | const unsigned char *source_ = NULL; /* -1 */ |
| 4268 | const unsigned char *source__ = NULL; /* -2 */ |
| 4269 | const unsigned char *source_end; |
| 4270 | size_t source_char_length; |
| 4271 | grn_nfkc_normalize_context *context; |
| 4272 | |
| 4273 | grn_nfkc_normalize_data_init(ctx, &data, string, options); |
| 4274 | context = &(data.context); |
| 4275 | if (ctx->rc != GRN_SUCCESS) { |
| 4276 | goto exit; |
| 4277 | } |
| 4278 | |
| 4279 | source = source_ = (unsigned char *)(data.string->original); |
| 4280 | source_end = source + data.string->original_length_in_bytes; |
| 4281 | for (; source < source_end; source += source_char_length) { |
| 4282 | source_char_length = |
| 4283 | (size_t)grn_charlen_(ctx, source, source_end, GRN_ENC_UTF8); |
| 4284 | if (source_char_length == 0) { |
| 4285 | break; |
| 4286 | } |
| 4287 | if (data.remove_tokenized_delimiter_p && |
| 4288 | grn_tokenizer_is_tokenized_delimiter(ctx, |
| 4289 | (const char *)source, |
| 4290 | (unsigned int)source_char_length, |
| 4291 | GRN_ENC_UTF8)) { |
| 4292 | continue; |
| 4293 | } |
| 4294 | { |
| 4295 | const char *decomposed; |
| 4296 | const unsigned char *current; |
| 4297 | const unsigned char *current_end; |
| 4298 | size_t current_length; |
| 4299 | |
| 4300 | decomposed = data.options->decompose_func(source); |
| 4301 | if (decomposed) { |
| 4302 | current = decomposed; |
| 4303 | current_end = current + strlen(decomposed); |
| 4304 | } else { |
| 4305 | current = source; |
| 4306 | current_end = current + source_char_length; |
| 4307 | } |
| 4308 | if (context->d_) { |
| 4309 | const char *composed = NULL; |
| 4310 | composed = options->compose_func(context->d_, current); |
| 4311 | if (composed) { |
| 4312 | current = composed; |
| 4313 | current_end = current + strlen(composed); |
| 4314 | if (context->t) { |
| 4315 | context->t--; |
| 4316 | } |
| 4317 | if (context->c) { |
| 4318 | context->c -= (context->d - context->d_); |
| 4319 | if (context->c[0] >= 0) { |
| 4320 | source_ = source__; |
| 4321 | } |
| 4322 | } |
| 4323 | if (context->o) { |
| 4324 | context->o--; |
| 4325 | } |
| 4326 | context->d = context->d_; |
| 4327 | context->n_characters--; |
| 4328 | } |
| 4329 | } |
| 4330 | bool have_ignored_character = false; |
| 4331 | bool have_used_character = false; |
| 4332 | for (; current < current_end; current += current_length) { |
| 4333 | current_length = |
| 4334 | (size_t)grn_charlen_(ctx, current, current_end, GRN_ENC_UTF8); |
| 4335 | if (current_length == 0) { |
| 4336 | break; |
| 4337 | } |
| 4338 | if (grn_nfkc_normalize_remove_target_blank_character_p( |
| 4339 | ctx, |
| 4340 | &data, |
| 4341 | current, |
| 4342 | current_length)) { |
| 4343 | if (context->t > context->types) { |
| 4344 | context->t[-1] |= GRN_CHAR_BLANK; |
| 4345 | } |
| 4346 | if (!data.options->include_removed_source_location) { |
| 4347 | have_ignored_character = true; |
| 4348 | } |
| 4349 | } else if (grn_nfkc_normalize_remove_target_non_blank_character_p( |
| 4350 | ctx, |
| 4351 | &data, |
| 4352 | current, |
| 4353 | current_length)) { |
| 4354 | if (!data.options->include_removed_source_location) { |
| 4355 | have_ignored_character = true; |
| 4356 | } |
| 4357 | } else { |
| 4358 | have_used_character = true; |
| 4359 | if (context->dest_end <= context->d + current_length) { |
| 4360 | grn_nfkc_normalize_expand(ctx, &data, current_length); |
| 4361 | if (ctx->rc != GRN_SUCCESS) { |
| 4362 | goto exit; |
| 4363 | } |
| 4364 | } |
| 4365 | |
| 4366 | grn_memcpy(context->d, current, current_length); |
| 4367 | context->d_ = context->d; |
| 4368 | context->d += current_length; |
| 4369 | context->n_characters++; |
| 4370 | if (context->t) { |
| 4371 | grn_char_type char_type; |
| 4372 | char_type = data.options->char_type_func(current); |
| 4373 | *(context->t++) = char_type; |
| 4374 | } |
| 4375 | if (context->c) { |
| 4376 | size_t i; |
| 4377 | if (source_ == source + source_char_length) { |
| 4378 | *(context->c++) = -1; |
| 4379 | } else { |
| 4380 | *(context->c++) = |
| 4381 | (int16_t)(source + source_char_length - source_); |
| 4382 | source__ = source_; |
| 4383 | source_ = source + source_char_length; |
| 4384 | } |
| 4385 | for (i = current_length; i > 1; i--) { |
| 4386 | *(context->c++) = 0; |
| 4387 | } |
| 4388 | } |
| 4389 | if (context->o) { |
| 4390 | *(context->o++) = |
| 4391 | (uint64_t)(source - |
| 4392 | (const unsigned char *)(data.string->original)); |
| 4393 | } |
| 4394 | } |
| 4395 | } |
| 4396 | if (have_ignored_character && !have_used_character) { |
| 4397 | source_ = source + source_char_length; |
| 4398 | } |
| 4399 | } |
| 4400 | } |
| 4401 | grn_nfkc_normalize_unify(ctx, &data); |
| 4402 | if (context->t) { |
| 4403 | *(context->t) = GRN_CHAR_NULL; |
| 4404 | } |
| 4405 | if (context->o) { |
| 4406 | *(context->o) = data.string->original_length_in_bytes; |
| 4407 | } |
| 4408 | *(context->d) = '\0'; |
| 4409 | data.string->n_characters = (unsigned int)(context->n_characters); |
| 4410 | data.string->normalized = context->dest; |
| 4411 | data.string->normalized_length_in_bytes = |
| 4412 | (unsigned int)(context->d - context->dest); |
| 4413 | data.string->checks = context->checks; |
| 4414 | data.string->ctypes = context->types; |
| 4415 | data.string->offsets = context->offsets; |
| 4416 | context->dest = NULL; |
| 4417 | context->checks = NULL; |
| 4418 | context->types = NULL; |
| 4419 | context->offsets = NULL; |
| 4420 | exit: |
| 4421 | if (ctx->rc != GRN_SUCCESS) { |
| 4422 | grn_nfkc_normalize_context_fin(ctx, context); |
| 4423 | } |
| 4424 | return ctx->rc; |
| 4425 | } |
| 4426 | #endif /* GRN_WITH_NFKC */ |
| 4427 | |
| 4428 | static inline grn_obj * |
| 4429 | ascii_normalize(grn_ctx *ctx, grn_string *nstr) |
| 4430 | { |
| 4431 | int16_t *ch; |
| 4432 | const unsigned char *s, *s_, *e; |
| 4433 | unsigned char *d, *d0, *d_; |
| 4434 | uint_least8_t *cp, *ctypes, ctype; |
| 4435 | size_t size = nstr->original_length_in_bytes, length = 0; |
| 4436 | int removeblankp = nstr->flags & GRN_STRING_REMOVE_BLANK; |
| 4437 | if (!(nstr->normalized = GRN_MALLOC(size + 1))) { |
| 4438 | ERR(GRN_NO_MEMORY_AVAILABLE, |
| 4439 | "[string][ascii] failed to allocate normalized text space"); |
| 4440 | return NULL; |
| 4441 | } |
| 4442 | d0 = (unsigned char *)nstr->normalized; |
| 4443 | if (nstr->flags & GRN_STRING_WITH_CHECKS) { |
| 4444 | if (!(nstr->checks = GRN_MALLOC((size + 1) * sizeof(int16_t)))) { |
| 4445 | GRN_FREE(nstr->normalized); |
| 4446 | nstr->normalized = NULL; |
| 4447 | ERR(GRN_NO_MEMORY_AVAILABLE, |
| 4448 | "[string][ascii] failed to allocate checks space"); |
| 4449 | return NULL; |
| 4450 | } |
| 4451 | } |
| 4452 | ch = nstr->checks; |
| 4453 | if (nstr->flags & GRN_STRING_WITH_TYPES) { |
| 4454 | if (!(nstr->ctypes = GRN_MALLOC(size + 1))) { |
| 4455 | GRN_FREE(nstr->checks); |
| 4456 | GRN_FREE(nstr->normalized); |
| 4457 | nstr->checks = NULL; |
| 4458 | nstr->normalized = NULL; |
| 4459 | ERR(GRN_NO_MEMORY_AVAILABLE, |
| 4460 | "[string][ascii] failed to allocate character types space"); |
| 4461 | return NULL; |
| 4462 | } |
| 4463 | } |
| 4464 | cp = ctypes = nstr->ctypes; |
| 4465 | e = (unsigned char *)nstr->original + size; |
| 4466 | for (s = s_ = (unsigned char *)nstr->original, d = d_ = d0; s < e; s++) { |
| 4467 | unsigned char c = *s; |
| 4468 | switch (c >> 4) { |
| 4469 | case 0: |
| 4470 | case 1: |
| 4471 | /* skip unprintable ascii */ |
| 4472 | if (cp > ctypes) { |
| 4473 | *(cp - 1) |= GRN_CHAR_BLANK; |
| 4474 | } |
| 4475 | continue; |
| 4476 | case 2: |
| 4477 | if (c == 0x20) { |
| 4478 | if (removeblankp) { |
| 4479 | if (cp > ctypes) { |
| 4480 | *(cp - 1) |= GRN_CHAR_BLANK; |
| 4481 | } |
| 4482 | continue; |
| 4483 | } else { |
| 4484 | *d = ' '; |
| 4485 | ctype = GRN_CHAR_BLANK | GRN_CHAR_SYMBOL; |
| 4486 | } |
| 4487 | } else { |
| 4488 | *d = c; |
| 4489 | ctype = GRN_CHAR_SYMBOL; |
| 4490 | } |
| 4491 | break; |
| 4492 | case 3: |
| 4493 | *d = c; |
| 4494 | ctype = (c <= 0x39) ? GRN_CHAR_DIGIT : GRN_CHAR_SYMBOL; |
| 4495 | break; |
| 4496 | case 4: |
| 4497 | *d = ('A' <= c) ? c + 0x20 : c; |
| 4498 | ctype = (c == 0x40) ? GRN_CHAR_SYMBOL : GRN_CHAR_ALPHA; |
| 4499 | break; |
| 4500 | case 5: |
| 4501 | *d = (c <= 'Z') ? c + 0x20 : c; |
| 4502 | ctype = (c <= 0x5a) ? GRN_CHAR_ALPHA : GRN_CHAR_SYMBOL; |
| 4503 | break; |
| 4504 | case 6: |
| 4505 | *d = c; |
| 4506 | ctype = (c == 0x60) ? GRN_CHAR_SYMBOL : GRN_CHAR_ALPHA; |
| 4507 | break; |
| 4508 | case 7: |
| 4509 | *d = c; |
| 4510 | ctype = (c <= 0x7a) ? GRN_CHAR_ALPHA |
| 4511 | : (c == 0x7f ? GRN_CHAR_OTHERS : GRN_CHAR_SYMBOL); |
| 4512 | break; |
| 4513 | default: |
| 4514 | *d = c; |
| 4515 | ctype = GRN_CHAR_OTHERS; |
| 4516 | break; |
| 4517 | } |
| 4518 | d++; |
| 4519 | length++; |
| 4520 | if (cp) { |
| 4521 | *cp++ = ctype; |
| 4522 | } |
| 4523 | if (ch) { |
| 4524 | *ch++ = (int16_t)(s + 1 - s_); |
| 4525 | s_ = s + 1; |
| 4526 | while (++d_ < d) { |
| 4527 | *ch++ = 0; |
| 4528 | } |
| 4529 | } |
| 4530 | } |
| 4531 | if (cp) { |
| 4532 | *cp = GRN_CHAR_NULL; |
| 4533 | } |
| 4534 | *d = '\0'; |
| 4535 | nstr->n_characters = (unsigned int)length; |
| 4536 | nstr->normalized_length_in_bytes = |
| 4537 | (unsigned int)(d - (unsigned char *)nstr->normalized); |
| 4538 | return NULL; |
| 4539 | } |
| 4540 | |
| 4541 | /* use cp1252 as latin1 */ |
| 4542 | static inline grn_obj * |
| 4543 | latin1_normalize(grn_ctx *ctx, grn_string *nstr) |
| 4544 | { |
| 4545 | int16_t *ch; |
| 4546 | const unsigned char *s, *s_, *e; |
| 4547 | unsigned char *d, *d0, *d_; |
| 4548 | uint_least8_t *cp, *ctypes, ctype; |
| 4549 | size_t size = nstr->original_length_in_bytes, length = 0; |
| 4550 | int removeblankp = nstr->flags & GRN_STRING_REMOVE_BLANK; |
| 4551 | if (!(nstr->normalized = GRN_MALLOC(size + 1))) { |
| 4552 | ERR(GRN_NO_MEMORY_AVAILABLE, |
| 4553 | "[string][latin1] failed to allocate normalized text space"); |
| 4554 | return NULL; |
| 4555 | } |
| 4556 | d0 = (unsigned char *)nstr->normalized; |
| 4557 | if (nstr->flags & GRN_STRING_WITH_CHECKS) { |
| 4558 | if (!(nstr->checks = GRN_MALLOC((size + 1) * sizeof(int16_t)))) { |
| 4559 | GRN_FREE(nstr->normalized); |
| 4560 | nstr->normalized = NULL; |
| 4561 | ERR(GRN_NO_MEMORY_AVAILABLE, |
| 4562 | "[string][latin1] failed to allocate checks space"); |
| 4563 | return NULL; |
| 4564 | } |
| 4565 | } |
| 4566 | ch = nstr->checks; |
| 4567 | if (nstr->flags & GRN_STRING_WITH_TYPES) { |
| 4568 | if (!(nstr->ctypes = GRN_MALLOC(size + 1))) { |
| 4569 | GRN_FREE(nstr->checks); |
| 4570 | GRN_FREE(nstr->normalized); |
| 4571 | nstr->checks = NULL; |
| 4572 | nstr->normalized = NULL; |
| 4573 | ERR(GRN_NO_MEMORY_AVAILABLE, |
| 4574 | "[normalizer][latin1] failed to allocate character types space"); |
| 4575 | return NULL; |
| 4576 | } |
| 4577 | } |
| 4578 | cp = ctypes = nstr->ctypes; |
| 4579 | e = (unsigned char *)nstr->original + size; |
| 4580 | for (s = s_ = (unsigned char *)nstr->original, d = d_ = d0; s < e; s++) { |
| 4581 | unsigned char c = *s; |
| 4582 | switch (c >> 4) { |
| 4583 | case 0: |
| 4584 | case 1: |
| 4585 | /* skip unprintable ascii */ |
| 4586 | if (cp > ctypes) { |
| 4587 | *(cp - 1) |= GRN_CHAR_BLANK; |
| 4588 | } |
| 4589 | continue; |
| 4590 | case 2: |
| 4591 | if (c == 0x20) { |
| 4592 | if (removeblankp) { |
| 4593 | if (cp > ctypes) { |
| 4594 | *(cp - 1) |= GRN_CHAR_BLANK; |
| 4595 | } |
| 4596 | continue; |
| 4597 | } else { |
| 4598 | *d = ' '; |
| 4599 | ctype = GRN_CHAR_BLANK | GRN_CHAR_SYMBOL; |
| 4600 | } |
| 4601 | } else { |
| 4602 | *d = c; |
| 4603 | ctype = GRN_CHAR_SYMBOL; |
| 4604 | } |
| 4605 | break; |
| 4606 | case 3: |
| 4607 | *d = c; |
| 4608 | ctype = (c <= 0x39) ? GRN_CHAR_DIGIT : GRN_CHAR_SYMBOL; |
| 4609 | break; |
| 4610 | case 4: |
| 4611 | *d = ('A' <= c) ? c + 0x20 : c; |
| 4612 | ctype = (c == 0x40) ? GRN_CHAR_SYMBOL : GRN_CHAR_ALPHA; |
| 4613 | break; |
| 4614 | case 5: |
| 4615 | *d = (c <= 'Z') ? c + 0x20 : c; |
| 4616 | ctype = (c <= 0x5a) ? GRN_CHAR_ALPHA : GRN_CHAR_SYMBOL; |
| 4617 | break; |
| 4618 | case 6: |
| 4619 | *d = c; |
| 4620 | ctype = (c == 0x60) ? GRN_CHAR_SYMBOL : GRN_CHAR_ALPHA; |
| 4621 | break; |
| 4622 | case 7: |
| 4623 | *d = c; |
| 4624 | ctype = (c <= 0x7a) ? GRN_CHAR_ALPHA |
| 4625 | : (c == 0x7f ? GRN_CHAR_OTHERS : GRN_CHAR_SYMBOL); |
| 4626 | break; |
| 4627 | case 8: |
| 4628 | if (c == 0x8a || c == 0x8c || c == 0x8e) { |
| 4629 | *d = c + 0x10; |
| 4630 | ctype = GRN_CHAR_ALPHA; |
| 4631 | } else { |
| 4632 | *d = c; |
| 4633 | ctype = GRN_CHAR_SYMBOL; |
| 4634 | } |
| 4635 | break; |
| 4636 | case 9: |
| 4637 | if (c == 0x9a || c == 0x9c || c == 0x9e || c == 0x9f) { |
| 4638 | *d = (c == 0x9f) ? c + 0x60 : c; |
| 4639 | ctype = GRN_CHAR_ALPHA; |
| 4640 | } else { |
| 4641 | *d = c; |
| 4642 | ctype = GRN_CHAR_SYMBOL; |
| 4643 | } |
| 4644 | break; |
| 4645 | case 0x0c: |
| 4646 | *d = c + 0x20; |
| 4647 | ctype = GRN_CHAR_ALPHA; |
| 4648 | break; |
| 4649 | case 0x0d: |
| 4650 | *d = (c == 0xd7 || c == 0xdf) ? c : c + 0x20; |
| 4651 | ctype = (c == 0xd7) ? GRN_CHAR_SYMBOL : GRN_CHAR_ALPHA; |
| 4652 | break; |
| 4653 | case 0x0e: |
| 4654 | *d = c; |
| 4655 | ctype = GRN_CHAR_ALPHA; |
| 4656 | break; |
| 4657 | case 0x0f: |
| 4658 | *d = c; |
| 4659 | ctype = (c == 0xf7) ? GRN_CHAR_SYMBOL : GRN_CHAR_ALPHA; |
| 4660 | break; |
| 4661 | default: |
| 4662 | *d = c; |
| 4663 | ctype = GRN_CHAR_OTHERS; |
| 4664 | break; |
| 4665 | } |
| 4666 | d++; |
| 4667 | length++; |
| 4668 | if (cp) { |
| 4669 | *cp++ = ctype; |
| 4670 | } |
| 4671 | if (ch) { |
| 4672 | *ch++ = (int16_t)(s + 1 - s_); |
| 4673 | s_ = s + 1; |
| 4674 | while (++d_ < d) { |
| 4675 | *ch++ = 0; |
| 4676 | } |
| 4677 | } |
| 4678 | } |
| 4679 | if (cp) { |
| 4680 | *cp = GRN_CHAR_NULL; |
| 4681 | } |
| 4682 | *d = '\0'; |
| 4683 | nstr->n_characters = (unsigned int)length; |
| 4684 | nstr->normalized_length_in_bytes = |
| 4685 | (unsigned int)(d - (unsigned char *)nstr->normalized); |
| 4686 | return NULL; |
| 4687 | } |
| 4688 | |
| 4689 | static inline grn_obj * |
| 4690 | koi8r_normalize(grn_ctx *ctx, grn_string *nstr) |
| 4691 | { |
| 4692 | int16_t *ch; |
| 4693 | const unsigned char *s, *s_, *e; |
| 4694 | unsigned char *d, *d0, *d_; |
| 4695 | uint_least8_t *cp, *ctypes, ctype; |
| 4696 | size_t size = nstr->original_length_in_bytes, length = 0; |
| 4697 | int removeblankp = nstr->flags & GRN_STRING_REMOVE_BLANK; |
| 4698 | if (!(nstr->normalized = GRN_MALLOC(size + 1))) { |
| 4699 | ERR(GRN_NO_MEMORY_AVAILABLE, |
| 4700 | "[string][koi8r] failed to allocate normalized text space"); |
| 4701 | return NULL; |
| 4702 | } |
| 4703 | d0 = (unsigned char *)nstr->normalized; |
| 4704 | if (nstr->flags & GRN_STRING_WITH_CHECKS) { |
| 4705 | if (!(nstr->checks = GRN_MALLOC((size + 1) * sizeof(int16_t)))) { |
| 4706 | GRN_FREE(nstr->normalized); |
| 4707 | nstr->normalized = NULL; |
| 4708 | ERR(GRN_NO_MEMORY_AVAILABLE, |
| 4709 | "[string][koi8r] failed to allocate checks space"); |
| 4710 | return NULL; |
| 4711 | } |
| 4712 | } |
| 4713 | ch = nstr->checks; |
| 4714 | if (nstr->flags & GRN_STRING_WITH_TYPES) { |
| 4715 | if (!(nstr->ctypes = GRN_MALLOC(size + 1))) { |
| 4716 | GRN_FREE(nstr->checks); |
| 4717 | GRN_FREE(nstr->normalized); |
| 4718 | nstr->checks = NULL; |
| 4719 | nstr->normalized = NULL; |
| 4720 | ERR(GRN_NO_MEMORY_AVAILABLE, |
| 4721 | "[string][koi8r] failed to allocate character types space"); |
| 4722 | return NULL; |
| 4723 | } |
| 4724 | } |
| 4725 | cp = ctypes = nstr->ctypes; |
| 4726 | e = (unsigned char *)nstr->original + size; |
| 4727 | for (s = s_ = (unsigned char *)nstr->original, d = d_ = d0; s < e; s++) { |
| 4728 | unsigned char c = *s; |
| 4729 | switch (c >> 4) { |
| 4730 | case 0: |
| 4731 | case 1: |
| 4732 | /* skip unprintable ascii */ |
| 4733 | if (cp > ctypes) { |
| 4734 | *(cp - 1) |= GRN_CHAR_BLANK; |
| 4735 | } |
| 4736 | continue; |
| 4737 | case 2: |
| 4738 | if (c == 0x20) { |
| 4739 | if (removeblankp) { |
| 4740 | if (cp > ctypes) { |
| 4741 | *(cp - 1) |= GRN_CHAR_BLANK; |
| 4742 | } |
| 4743 | continue; |
| 4744 | } else { |
| 4745 | *d = ' '; |
| 4746 | ctype = GRN_CHAR_BLANK | GRN_CHAR_SYMBOL; |
| 4747 | } |
| 4748 | } else { |
| 4749 | *d = c; |
| 4750 | ctype = GRN_CHAR_SYMBOL; |
| 4751 | } |
| 4752 | break; |
| 4753 | case 3: |
| 4754 | *d = c; |
| 4755 | ctype = (c <= 0x39) ? GRN_CHAR_DIGIT : GRN_CHAR_SYMBOL; |
| 4756 | break; |
| 4757 | case 4: |
| 4758 | *d = ('A' <= c) ? c + 0x20 : c; |
| 4759 | ctype = (c == 0x40) ? GRN_CHAR_SYMBOL : GRN_CHAR_ALPHA; |
| 4760 | break; |
| 4761 | case 5: |
| 4762 | *d = (c <= 'Z') ? c + 0x20 : c; |
| 4763 | ctype = (c <= 0x5a) ? GRN_CHAR_ALPHA : GRN_CHAR_SYMBOL; |
| 4764 | break; |
| 4765 | case 6: |
| 4766 | *d = c; |
| 4767 | ctype = (c == 0x60) ? GRN_CHAR_SYMBOL : GRN_CHAR_ALPHA; |
| 4768 | break; |
| 4769 | case 7: |
| 4770 | *d = c; |
| 4771 | ctype = (c <= 0x7a) ? GRN_CHAR_ALPHA |
| 4772 | : (c == 0x7f ? GRN_CHAR_OTHERS : GRN_CHAR_SYMBOL); |
| 4773 | break; |
| 4774 | case 0x0a: |
| 4775 | *d = c; |
| 4776 | ctype = (c == 0xa3) ? GRN_CHAR_ALPHA : GRN_CHAR_OTHERS; |
| 4777 | break; |
| 4778 | case 0x0b: |
| 4779 | if (c == 0xb3) { |
| 4780 | *d = c - 0x10; |
| 4781 | ctype = GRN_CHAR_ALPHA; |
| 4782 | } else { |
| 4783 | *d = c; |
| 4784 | ctype = GRN_CHAR_OTHERS; |
| 4785 | } |
| 4786 | break; |
| 4787 | case 0x0c: |
| 4788 | case 0x0d: |
| 4789 | *d = c; |
| 4790 | ctype = GRN_CHAR_ALPHA; |
| 4791 | break; |
| 4792 | case 0x0e: |
| 4793 | case 0x0f: |
| 4794 | *d = c - 0x20; |
| 4795 | ctype = GRN_CHAR_ALPHA; |
| 4796 | break; |
| 4797 | default: |
| 4798 | *d = c; |
| 4799 | ctype = GRN_CHAR_OTHERS; |
| 4800 | break; |
| 4801 | } |
| 4802 | d++; |
| 4803 | length++; |
| 4804 | if (cp) { |
| 4805 | *cp++ = ctype; |
| 4806 | } |
| 4807 | if (ch) { |
| 4808 | *ch++ = (int16_t)(s + 1 - s_); |
| 4809 | s_ = s + 1; |
| 4810 | while (++d_ < d) { |
| 4811 | *ch++ = 0; |
| 4812 | } |
| 4813 | } |
| 4814 | } |
| 4815 | if (cp) { |
| 4816 | *cp = GRN_CHAR_NULL; |
| 4817 | } |
| 4818 | *d = '\0'; |
| 4819 | nstr->n_characters = (unsigned int)length; |
| 4820 | nstr->normalized_length_in_bytes = |
| 4821 | (unsigned int)(d - (unsigned char *)nstr->normalized); |
| 4822 | return NULL; |
| 4823 | } |
| 4824 | |
| 4825 | static grn_obj * |
| 4826 | auto_next(grn_ctx *ctx, int nargs, grn_obj **args, grn_user_data *user_data) |
| 4827 | { |
| 4828 | grn_obj *string = args[0]; |
| 4829 | grn_string *string_ = (grn_string *)(string); |
| 4830 | switch (string_->encoding) { |
| 4831 | case GRN_ENC_EUC_JP: |
| 4832 | eucjp_normalize(ctx, string_); |
| 4833 | break; |
| 4834 | case GRN_ENC_UTF8: |
| 4835 | #ifdef GRN_WITH_NFKC |
| 4836 | { |
| 4837 | grn_nfkc_normalize_options options; |
| 4838 | grn_nfkc_normalize_options_init(ctx, |
| 4839 | &options, |
| 4840 | grn_nfkc_char_type, |
| 4841 | grn_nfkc_decompose, |
| 4842 | grn_nfkc_compose); |
| 4843 | grn_nfkc_normalize(ctx, string, &options); |
| 4844 | grn_nfkc_normalize_options_fin(ctx, &options); |
| 4845 | } |
| 4846 | #else /* GRN_WITH_NFKC */ |
| 4847 | ascii_normalize(ctx, string_); |
| 4848 | #endif /* GRN_WITH_NFKC */ |
| 4849 | break; |
| 4850 | case GRN_ENC_SJIS: |
| 4851 | sjis_normalize(ctx, string_); |
| 4852 | break; |
| 4853 | case GRN_ENC_LATIN1: |
| 4854 | latin1_normalize(ctx, string_); |
| 4855 | break; |
| 4856 | case GRN_ENC_KOI8R: |
| 4857 | koi8r_normalize(ctx, string_); |
| 4858 | break; |
| 4859 | default: |
| 4860 | ascii_normalize(ctx, string_); |
| 4861 | break; |
| 4862 | } |
| 4863 | return NULL; |
| 4864 | } |
| 4865 | |
| 4866 | #ifdef GRN_WITH_NFKC |
| 4867 | static grn_obj * |
| 4868 | nfkc51_next(grn_ctx *ctx, int nargs, grn_obj **args, grn_user_data *user_data) |
| 4869 | { |
| 4870 | grn_obj *string = args[0]; |
| 4871 | grn_nfkc_normalize_options options; |
| 4872 | |
| 4873 | grn_nfkc_normalize_options_init(ctx, |
| 4874 | &options, |
| 4875 | grn_nfkc50_char_type, |
| 4876 | grn_nfkc50_decompose, |
| 4877 | grn_nfkc50_compose); |
| 4878 | grn_nfkc_normalize(ctx, string, &options); |
| 4879 | grn_nfkc_normalize_options_fin(ctx, &options); |
| 4880 | return NULL; |
| 4881 | } |
| 4882 | |
| 4883 | static void * |
| 4884 | nfkc100_open_options(grn_ctx *ctx, |
| 4885 | grn_obj *normalizer, |
| 4886 | grn_obj *raw_options, |
| 4887 | void *user_data) |
| 4888 | { |
| 4889 | const char *tag = "[normalizer][nfkc100]"; |
| 4890 | grn_nfkc_normalize_options *options; |
| 4891 | |
| 4892 | options = GRN_CALLOC(sizeof(grn_nfkc_normalize_options)); |
| 4893 | if (!options) { |
| 4894 | ERR(GRN_NO_MEMORY_AVAILABLE, |
| 4895 | "%s failed to allocate memory for options", |
| 4896 | tag); |
| 4897 | return NULL; |
| 4898 | } |
| 4899 | |
| 4900 | grn_nfkc100_normalize_options_init(ctx, options); |
| 4901 | |
| 4902 | grn_nfkc_normalize_options_apply(ctx, options, raw_options, tag); |
| 4903 | |
| 4904 | return options; |
| 4905 | } |
| 4906 | |
| 4907 | static void |
| 4908 | nfkc100_close_options(grn_ctx *ctx, void *data) |
| 4909 | { |
| 4910 | grn_nfkc_normalize_options *options = data; |
| 4911 | grn_nfkc_normalize_options_fin(ctx, options); |
| 4912 | GRN_FREE(options); |
| 4913 | } |
| 4914 | |
| 4915 | static grn_obj * |
| 4916 | nfkc100_next(grn_ctx *ctx, int nargs, grn_obj **args, grn_user_data *user_data) |
| 4917 | { |
| 4918 | grn_obj *string = args[0]; |
| 4919 | grn_obj *table; |
| 4920 | grn_nfkc_normalize_options *options; |
| 4921 | grn_nfkc_normalize_options options_raw; |
| 4922 | |
| 4923 | table = grn_string_get_table(ctx, string); |
| 4924 | if (table) { |
| 4925 | uint32_t i = grn_string_get_normalizer_index(ctx, string); |
| 4926 | options = grn_table_cache_normalizers_options(ctx, |
| 4927 | table, |
| 4928 | i, |
| 4929 | nfkc100_open_options, |
| 4930 | nfkc100_close_options, |
| 4931 | NULL); |
| 4932 | if (ctx->rc != GRN_SUCCESS) { |
| 4933 | return NULL; |
| 4934 | } |
| 4935 | } else { |
| 4936 | grn_nfkc100_normalize_options_init(ctx, &options_raw); |
| 4937 | options = &options_raw; |
| 4938 | } |
| 4939 | |
| 4940 | grn_nfkc_normalize(ctx, string, options); |
| 4941 | if (!table) { |
| 4942 | grn_nfkc_normalize_options_fin(ctx, options); |
| 4943 | } |
| 4944 | return NULL; |
| 4945 | } |
| 4946 | |
| 4947 | static void * |
| 4948 | nfkc121_open_options(grn_ctx *ctx, |
| 4949 | grn_obj *normalizer, |
| 4950 | grn_obj *raw_options, |
| 4951 | void *user_data) |
| 4952 | { |
| 4953 | const char *tag = "[normalizer][nfkc121]"; |
| 4954 | grn_nfkc_normalize_options *options; |
| 4955 | |
| 4956 | options = GRN_CALLOC(sizeof(grn_nfkc_normalize_options)); |
| 4957 | if (!options) { |
| 4958 | ERR(GRN_NO_MEMORY_AVAILABLE, |
| 4959 | "%s failed to allocate memory for options", |
| 4960 | tag); |
| 4961 | return NULL; |
| 4962 | } |
| 4963 | |
| 4964 | grn_nfkc121_normalize_options_init(ctx, options); |
| 4965 | |
| 4966 | grn_nfkc_normalize_options_apply(ctx, options, raw_options, tag); |
| 4967 | |
| 4968 | return options; |
| 4969 | } |
| 4970 | |
| 4971 | static void |
| 4972 | nfkc121_close_options(grn_ctx *ctx, void *data) |
| 4973 | { |
| 4974 | grn_nfkc_normalize_options *options = data; |
| 4975 | grn_nfkc_normalize_options_fin(ctx, options); |
| 4976 | GRN_FREE(options); |
| 4977 | } |
| 4978 | |
| 4979 | static grn_obj * |
| 4980 | nfkc121_next(grn_ctx *ctx, int nargs, grn_obj **args, grn_user_data *user_data) |
| 4981 | { |
| 4982 | grn_obj *string = args[0]; |
| 4983 | grn_obj *table; |
| 4984 | grn_nfkc_normalize_options *options; |
| 4985 | grn_nfkc_normalize_options options_raw; |
| 4986 | |
| 4987 | table = grn_string_get_table(ctx, string); |
| 4988 | if (table) { |
| 4989 | uint32_t i = grn_string_get_normalizer_index(ctx, string); |
| 4990 | options = grn_table_cache_normalizers_options(ctx, |
| 4991 | table, |
| 4992 | i, |
| 4993 | nfkc121_open_options, |
| 4994 | nfkc121_close_options, |
| 4995 | NULL); |
| 4996 | if (ctx->rc != GRN_SUCCESS) { |
| 4997 | return NULL; |
| 4998 | } |
| 4999 | } else { |
| 5000 | grn_nfkc121_normalize_options_init(ctx, &options_raw); |
| 5001 | options = &options_raw; |
| 5002 | } |
| 5003 | |
| 5004 | grn_nfkc_normalize(ctx, string, options); |
| 5005 | if (!table) { |
| 5006 | grn_nfkc_normalize_options_fin(ctx, options); |
| 5007 | } |
| 5008 | return NULL; |
| 5009 | } |
| 5010 | |
| 5011 | static void * |
| 5012 | nfkc130_open_options(grn_ctx *ctx, |
| 5013 | grn_obj *normalizer, |
| 5014 | grn_obj *raw_options, |
| 5015 | void *user_data) |
| 5016 | { |
| 5017 | const char *tag = "[normalizer][nfkc130]"; |
| 5018 | grn_nfkc_normalize_options *options; |
| 5019 | |
| 5020 | options = GRN_CALLOC(sizeof(grn_nfkc_normalize_options)); |
| 5021 | if (!options) { |
| 5022 | ERR(GRN_NO_MEMORY_AVAILABLE, |
| 5023 | "%s failed to allocate memory for options", |
| 5024 | tag); |
| 5025 | return NULL; |
| 5026 | } |
| 5027 | |
| 5028 | grn_nfkc130_normalize_options_init(ctx, options); |
| 5029 | |
| 5030 | grn_nfkc_normalize_options_apply(ctx, options, raw_options, tag); |
| 5031 | |
| 5032 | return options; |
| 5033 | } |
| 5034 | |
| 5035 | static void |
| 5036 | nfkc130_close_options(grn_ctx *ctx, void *data) |
| 5037 | { |
| 5038 | grn_nfkc_normalize_options *options = data; |
| 5039 | grn_nfkc_normalize_options_fin(ctx, options); |
| 5040 | GRN_FREE(options); |
| 5041 | } |
| 5042 | |
| 5043 | static grn_obj * |
| 5044 | nfkc130_next(grn_ctx *ctx, int nargs, grn_obj **args, grn_user_data *user_data) |
| 5045 | { |
| 5046 | grn_obj *string = args[0]; |
| 5047 | grn_obj *table; |
| 5048 | grn_nfkc_normalize_options *options; |
| 5049 | grn_nfkc_normalize_options options_raw; |
| 5050 | |
| 5051 | table = grn_string_get_table(ctx, string); |
| 5052 | if (table) { |
| 5053 | uint32_t i = grn_string_get_normalizer_index(ctx, string); |
| 5054 | options = grn_table_cache_normalizers_options(ctx, |
| 5055 | table, |
| 5056 | i, |
| 5057 | nfkc130_open_options, |
| 5058 | nfkc130_close_options, |
| 5059 | NULL); |
| 5060 | if (ctx->rc != GRN_SUCCESS) { |
| 5061 | return NULL; |
| 5062 | } |
| 5063 | } else { |
| 5064 | grn_nfkc130_normalize_options_init(ctx, &options_raw); |
| 5065 | options = &options_raw; |
| 5066 | } |
| 5067 | |
| 5068 | grn_nfkc_normalize(ctx, string, options); |
| 5069 | if (!table) { |
| 5070 | grn_nfkc_normalize_options_fin(ctx, options); |
| 5071 | } |
| 5072 | return NULL; |
| 5073 | } |
| 5074 | |
| 5075 | static void * |
| 5076 | nfkc150_open_options(grn_ctx *ctx, |
| 5077 | grn_obj *normalizer, |
| 5078 | grn_obj *raw_options, |
| 5079 | void *user_data) |
| 5080 | { |
| 5081 | const char *tag = "[normalizer][nfkc150]"; |
| 5082 | grn_nfkc_normalize_options *options; |
| 5083 | |
| 5084 | options = GRN_CALLOC(sizeof(grn_nfkc_normalize_options)); |
| 5085 | if (!options) { |
| 5086 | ERR(GRN_NO_MEMORY_AVAILABLE, |
| 5087 | "%s failed to allocate memory for options", |
| 5088 | tag); |
| 5089 | return NULL; |
| 5090 | } |
| 5091 | |
| 5092 | grn_nfkc150_normalize_options_init(ctx, options); |
| 5093 | |
| 5094 | grn_nfkc_normalize_options_apply(ctx, options, raw_options, tag); |
| 5095 | |
| 5096 | return options; |
| 5097 | } |
| 5098 | |
| 5099 | static void |
| 5100 | nfkc150_close_options(grn_ctx *ctx, void *data) |
| 5101 | { |
| 5102 | grn_nfkc_normalize_options *options = data; |
| 5103 | grn_nfkc_normalize_options_fin(ctx, options); |
| 5104 | GRN_FREE(options); |
| 5105 | } |
| 5106 | |
| 5107 | static grn_obj * |
| 5108 | nfkc150_next(grn_ctx *ctx, int nargs, grn_obj **args, grn_user_data *user_data) |
| 5109 | { |
| 5110 | grn_obj *string = args[0]; |
| 5111 | grn_obj *table; |
| 5112 | grn_nfkc_normalize_options *options; |
| 5113 | grn_nfkc_normalize_options options_raw; |
| 5114 | |
| 5115 | table = grn_string_get_table(ctx, string); |
| 5116 | if (table) { |
| 5117 | uint32_t i = grn_string_get_normalizer_index(ctx, string); |
| 5118 | options = grn_table_cache_normalizers_options(ctx, |
| 5119 | table, |
| 5120 | i, |
| 5121 | nfkc150_open_options, |
| 5122 | nfkc150_close_options, |
| 5123 | NULL); |
| 5124 | if (ctx->rc != GRN_SUCCESS) { |
| 5125 | return NULL; |
| 5126 | } |
| 5127 | } else { |
| 5128 | grn_nfkc150_normalize_options_init(ctx, &options_raw); |
| 5129 | options = &options_raw; |
| 5130 | } |
| 5131 | |
| 5132 | grn_nfkc_normalize(ctx, string, options); |
| 5133 | if (!table) { |
| 5134 | grn_nfkc_normalize_options_fin(ctx, options); |
| 5135 | } |
| 5136 | return NULL; |
| 5137 | } |
| 5138 | |
| 5139 | static void * |
| 5140 | nfkc_open_options(grn_ctx *ctx, |
| 5141 | grn_obj *normalizer, |
| 5142 | grn_obj *raw_options, |
| 5143 | void *user_data) |
| 5144 | { |
| 5145 | const char *tag = "[normalizer][nfkc]"; |
| 5146 | grn_nfkc_normalize_options *options; |
| 5147 | |
| 5148 | options = GRN_CALLOC(sizeof(grn_nfkc_normalize_options)); |
| 5149 | if (!options) { |
| 5150 | ERR(GRN_NO_MEMORY_AVAILABLE, |
| 5151 | "%s failed to allocate memory for options", |
| 5152 | tag); |
| 5153 | return NULL; |
| 5154 | } |
| 5155 | |
| 5156 | grn_nfkc160_normalize_options_init(ctx, options); |
| 5157 | |
| 5158 | grn_nfkc_normalize_options_apply(ctx, options, raw_options, tag); |
| 5159 | |
| 5160 | return options; |
| 5161 | } |
| 5162 | |
| 5163 | static void |
| 5164 | nfkc_close_options(grn_ctx *ctx, void *data) |
| 5165 | { |
| 5166 | grn_nfkc_normalize_options *options = data; |
| 5167 | grn_nfkc_normalize_options_fin(ctx, options); |
| 5168 | GRN_FREE(options); |
| 5169 | } |
| 5170 | |
| 5171 | static grn_obj * |
| 5172 | nfkc_next(grn_ctx *ctx, int nargs, grn_obj **args, grn_user_data *user_data) |
| 5173 | { |
| 5174 | grn_obj *string = args[0]; |
| 5175 | grn_obj *table; |
| 5176 | grn_nfkc_normalize_options *options; |
| 5177 | grn_nfkc_normalize_options options_raw; |
| 5178 | |
| 5179 | table = grn_string_get_table(ctx, string); |
| 5180 | if (table) { |
| 5181 | uint32_t i = grn_string_get_normalizer_index(ctx, string); |
| 5182 | options = grn_table_cache_normalizers_options(ctx, |
| 5183 | table, |
| 5184 | i, |
| 5185 | nfkc_open_options, |
| 5186 | nfkc_close_options, |
| 5187 | NULL); |
| 5188 | if (ctx->rc != GRN_SUCCESS) { |
| 5189 | return NULL; |
| 5190 | } |
| 5191 | } else { |
| 5192 | grn_nfkc160_normalize_options_init(ctx, &options_raw); |
| 5193 | options = &options_raw; |
| 5194 | } |
| 5195 | |
| 5196 | grn_nfkc_normalize(ctx, string, options); |
| 5197 | if (!table) { |
| 5198 | grn_nfkc_normalize_options_fin(ctx, options); |
| 5199 | } |
| 5200 | return NULL; |
| 5201 | } |
| 5202 | #endif /* GRN_WITH_NFKC */ |
| 5203 | |
| 5204 | typedef grn_char_type (*grn_get_char_type_func)(const unsigned char *character); |
| 5205 | |
| 5206 | /* This is for general substitute based normalizers. */ |
| 5207 | typedef struct { |
| 5208 | const char *tag; |
| 5209 | grn_get_char_type_func get_char_type; |
| 5210 | grn_obj normalized; |
| 5211 | grn_obj checks; |
| 5212 | bool need_checks; |
| 5213 | grn_obj types; |
| 5214 | bool need_types; |
| 5215 | size_t current_source_offset; |
| 5216 | grn_obj offsets; |
| 5217 | bool need_offsets; |
| 5218 | size_t n_normalized_characters; |
| 5219 | bool count_n_normalized_characters_in_checks; |
| 5220 | bool count_n_normalized_characters_in_types; |
| 5221 | } substitutor_data; |
| 5222 | |
| 5223 | static void |
| 5224 | substitutor_data_init(grn_ctx *ctx, |
| 5225 | substitutor_data *data, |
| 5226 | const char *tag, |
| 5227 | grn_get_char_type_func get_char_type, |
| 5228 | grn_string *string, |
| 5229 | bool report_source_offset) |
| 5230 | { |
| 5231 | data->tag = tag; |
| 5232 | data->get_char_type = get_char_type; |
| 5233 | GRN_TEXT_INIT(&(data->normalized), 0); |
| 5234 | GRN_INT16_INIT(&(data->checks), GRN_OBJ_VECTOR); |
| 5235 | data->need_checks = (string->flags & GRN_STRING_WITH_CHECKS); |
| 5236 | GRN_UINT8_INIT(&(data->types), GRN_OBJ_VECTOR); |
| 5237 | data->need_types = |
| 5238 | (string->flags & GRN_STRING_WITH_TYPES) && (ctx->encoding == GRN_ENC_UTF8); |
| 5239 | data->current_source_offset = 0; |
| 5240 | GRN_UINT64_INIT(&(data->offsets), GRN_OBJ_VECTOR); |
| 5241 | data->need_offsets = report_source_offset; |
| 5242 | data->n_normalized_characters = 0; |
| 5243 | data->count_n_normalized_characters_in_checks = false; |
| 5244 | data->count_n_normalized_characters_in_types = false; |
| 5245 | if (data->need_checks) { |
| 5246 | data->count_n_normalized_characters_in_checks = true; |
| 5247 | } else if (data->need_types) { |
| 5248 | data->count_n_normalized_characters_in_types = true; |
| 5249 | } |
| 5250 | } |
| 5251 | |
| 5252 | static void |
| 5253 | substitutor_data_fin(grn_ctx *ctx, substitutor_data *data) |
| 5254 | { |
| 5255 | GRN_OBJ_FIN(ctx, &(data->normalized)); |
| 5256 | GRN_OBJ_FIN(ctx, &(data->checks)); |
| 5257 | GRN_OBJ_FIN(ctx, &(data->types)); |
| 5258 | GRN_OBJ_FIN(ctx, &(data->offsets)); |
| 5259 | } |
| 5260 | |
| 5261 | static void |
| 5262 | substitutor_add_checks_and_offsets(grn_ctx *ctx, |
| 5263 | substitutor_data *data, |
| 5264 | const char *source, |
| 5265 | size_t source_length, |
| 5266 | const char *normalized, |
| 5267 | size_t normalized_length) |
| 5268 | { |
| 5269 | grn_obj *checks = &(data->checks); |
| 5270 | grn_obj *offsets = &(data->offsets); |
| 5271 | |
| 5272 | size_t current_source_offset = data->current_source_offset; |
| 5273 | const char *source_current = source; |
| 5274 | const char *previous_source_current = source; |
| 5275 | const char *source_end = source_current + source_length; |
| 5276 | const char *normalized_current = normalized; |
| 5277 | const char *normalized_end = normalized_current + normalized_length; |
| 5278 | while (source_current < source_end && normalized_current < normalized_end) { |
| 5279 | int source_char_length = grn_charlen(ctx, source_current, source_end); |
| 5280 | if (source_char_length == 0) { |
| 5281 | ERR(GRN_INVALID_ARGUMENT, |
| 5282 | "%s invalid character in source text: <%.*s>(%d)", |
| 5283 | data->tag, |
| 5284 | (int)(source_end - source_current), |
| 5285 | source_current, |
| 5286 | (int)(source_end - source_current)); |
| 5287 | return; |
| 5288 | } |
| 5289 | int normalized_char_length; |
| 5290 | if (source_current == normalized_current) { |
| 5291 | normalized_char_length = source_char_length; |
| 5292 | } else { |
| 5293 | normalized_char_length = |
| 5294 | grn_charlen(ctx, normalized_current, normalized_end); |
| 5295 | if (normalized_char_length == 0) { |
| 5296 | ERR(GRN_INVALID_ARGUMENT, |
| 5297 | "%s invalid character in normalized text: <%.*s>(%d)", |
| 5298 | data->tag, |
| 5299 | (int)(normalized_end - normalized_current), |
| 5300 | normalized_current, |
| 5301 | (int)(normalized_end - normalized_current)); |
| 5302 | return; |
| 5303 | } |
| 5304 | } |
| 5305 | if (data->need_checks) { |
| 5306 | GRN_INT16_PUT(ctx, checks, source_char_length); |
| 5307 | int i; |
| 5308 | for (i = 1; i < normalized_char_length; i++) { |
| 5309 | GRN_INT16_PUT(ctx, checks, 0); |
| 5310 | } |
| 5311 | } |
| 5312 | if (data->need_offsets) { |
| 5313 | GRN_UINT64_PUT(ctx, |
| 5314 | offsets, |
| 5315 | (uint64_t)(source_current - source) + |
| 5316 | current_source_offset); |
| 5317 | } |
| 5318 | previous_source_current = source_current; |
| 5319 | source_current += source_char_length; |
| 5320 | normalized_current += normalized_char_length; |
| 5321 | if (data->count_n_normalized_characters_in_checks) { |
| 5322 | data->n_normalized_characters++; |
| 5323 | } |
| 5324 | } |
| 5325 | if (source_current < source_end) { |
| 5326 | size_t last_check_index = GRN_INT16_VECTOR_SIZE(checks); |
| 5327 | if (last_check_index == 0) { |
| 5328 | return; |
| 5329 | } |
| 5330 | for (last_check_index--; last_check_index > 0; last_check_index--) { |
| 5331 | if (GRN_INT16_VALUE_AT(checks, last_check_index) != 0) { |
| 5332 | break; |
| 5333 | } |
| 5334 | } |
| 5335 | while (source_current < source_end) { |
| 5336 | int source_char_length = grn_charlen(ctx, source_current, source_end); |
| 5337 | if (source_char_length == 0) { |
| 5338 | ERR(GRN_INVALID_ARGUMENT, |
| 5339 | "%s invalid character in source text: <%.*s>(%d)", |
| 5340 | data->tag, |
| 5341 | (int)(source_end - source_current), |
| 5342 | source_current, |
| 5343 | (int)(source_end - source_current)); |
| 5344 | return; |
| 5345 | } |
| 5346 | GRN_INT16_VALUE_AT(checks, last_check_index) += |
| 5347 | (int16_t)source_char_length; |
| 5348 | source_current += source_char_length; |
| 5349 | } |
| 5350 | } else if (normalized_current < normalized_end) { |
| 5351 | while (normalized_current < normalized_end) { |
| 5352 | int normalized_char_length = |
| 5353 | grn_charlen(ctx, normalized_current, normalized_end); |
| 5354 | if (normalized_char_length == 0) { |
| 5355 | ERR(GRN_INVALID_ARGUMENT, |
| 5356 | "%s invalid character in normalized text: <%.*s>(%d)", |
| 5357 | data->tag, |
| 5358 | (int)(normalized_end - normalized_current), |
| 5359 | normalized_current, |
| 5360 | (int)(normalized_end - normalized_current)); |
| 5361 | return; |
| 5362 | } |
| 5363 | if (data->need_checks) { |
| 5364 | GRN_INT16_PUT(ctx, checks, -1); |
| 5365 | int i; |
| 5366 | for (i = 1; i < normalized_char_length; i++) { |
| 5367 | GRN_INT16_PUT(ctx, checks, 0); |
| 5368 | } |
| 5369 | } |
| 5370 | if (data->need_offsets) { |
| 5371 | GRN_UINT64_PUT(ctx, |
| 5372 | offsets, |
| 5373 | (uint64_t)(previous_source_current - source) + |
| 5374 | current_source_offset); |
| 5375 | } |
| 5376 | normalized_current += normalized_char_length; |
| 5377 | if (data->count_n_normalized_characters_in_checks) { |
| 5378 | data->n_normalized_characters++; |
| 5379 | } |
| 5380 | } |
| 5381 | } |
| 5382 | } |
| 5383 | |
| 5384 | static void |
| 5385 | substitutor_add_types(grn_ctx *ctx, |
| 5386 | substitutor_data *data, |
| 5387 | const char *normalized, |
| 5388 | size_t normalized_length) |
| 5389 | { |
| 5390 | grn_obj *types = &(data->types); |
| 5391 | const char *normalized_end = normalized + normalized_length; |
| 5392 | while (normalized < normalized_end) { |
| 5393 | int normalized_char_length = grn_charlen(ctx, normalized, normalized_end); |
| 5394 | if (normalized_char_length == 0) { |
| 5395 | ERR(GRN_INVALID_ARGUMENT, |
| 5396 | "%s invalid character in normalized text: <%.*s>(%d)", |
| 5397 | data->tag, |
| 5398 | (int)(normalized_end - normalized), |
| 5399 | normalized, |
| 5400 | (int)(normalized_end - normalized)); |
| 5401 | return; |
| 5402 | } |
| 5403 | grn_char_type char_type = data->get_char_type(normalized); |
| 5404 | GRN_UINT8_PUT(ctx, types, char_type); |
| 5405 | normalized += normalized_char_length; |
| 5406 | if (data->count_n_normalized_characters_in_types) { |
| 5407 | data->n_normalized_characters++; |
| 5408 | } |
| 5409 | } |
| 5410 | } |
| 5411 | |
| 5412 | static void |
| 5413 | substitutor_normalized(grn_ctx *ctx, |
| 5414 | substitutor_data *data, |
| 5415 | const char *source, |
| 5416 | size_t source_length, |
| 5417 | const char *normalized, |
| 5418 | size_t normalized_length) |
| 5419 | { |
| 5420 | if (data->need_checks || data->need_offsets) { |
| 5421 | substitutor_add_checks_and_offsets(ctx, |
| 5422 | data, |
| 5423 | source, |
| 5424 | source_length, |
| 5425 | normalized, |
| 5426 | normalized_length); |
| 5427 | if (ctx->rc != GRN_SUCCESS) { |
| 5428 | return; |
| 5429 | } |
| 5430 | } |
| 5431 | |
| 5432 | if (data->need_types) { |
| 5433 | substitutor_add_types(ctx, data, normalized, normalized_length); |
| 5434 | if (ctx->rc != GRN_SUCCESS) { |
| 5435 | return; |
| 5436 | } |
| 5437 | } |
| 5438 | |
| 5439 | data->current_source_offset += source_length; |
| 5440 | } |
| 5441 | |
| 5442 | static void |
| 5443 | substitutor_finished(grn_ctx *ctx, substitutor_data *data, grn_string *string) |
| 5444 | { |
| 5445 | if (!data->count_n_normalized_characters_in_checks && |
| 5446 | !data->count_n_normalized_characters_in_types) { |
| 5447 | const char *current = GRN_TEXT_VALUE(&(data->normalized)); |
| 5448 | const char *end = current + GRN_TEXT_LEN(&(data->normalized)); |
| 5449 | while (current < end) { |
| 5450 | int char_length = grn_charlen(ctx, current, end); |
| 5451 | if (char_length == 0) { |
| 5452 | ERR(GRN_INVALID_ARGUMENT, |
| 5453 | "%s invalid character in normalized text: <%.*s>(%d)", |
| 5454 | data->tag, |
| 5455 | (int)(end - current), |
| 5456 | current, |
| 5457 | (int)(end - current)); |
| 5458 | return; |
| 5459 | } |
| 5460 | data->n_normalized_characters++; |
| 5461 | current += char_length; |
| 5462 | } |
| 5463 | } |
| 5464 | |
| 5465 | string->n_characters = (unsigned int)(data->n_normalized_characters); |
| 5466 | string->normalized_length_in_bytes = |
| 5467 | (unsigned int)GRN_TEXT_LEN(&(data->normalized)); |
| 5468 | string->normalized = grn_bulk_detach(ctx, &(data->normalized)); |
| 5469 | |
| 5470 | if (data->need_checks) { |
| 5471 | string->checks = (int16_t *)grn_bulk_detach(ctx, &(data->checks)); |
| 5472 | } |
| 5473 | |
| 5474 | if (data->need_types) { |
| 5475 | GRN_UINT64_PUT(ctx, &(data->types), GRN_CHAR_NULL); |
| 5476 | string->ctypes = (uint_least8_t *)grn_bulk_detach(ctx, &(data->types)); |
| 5477 | } |
| 5478 | |
| 5479 | if (data->need_offsets) { |
| 5480 | GRN_UINT64_PUT(ctx, &(data->offsets), string->original_length_in_bytes); |
| 5481 | string->offsets = (uint64_t *)grn_bulk_detach(ctx, &(data->offsets)); |
| 5482 | } |
| 5483 | } |
| 5484 | |
| 5485 | typedef struct { |
| 5486 | grn_obj *target_table; |
| 5487 | grn_obj *target_index_column; |
| 5488 | uint32_t target_section; |
| 5489 | grn_obj *normalized_column; |
| 5490 | grn_get_char_type_func get_char_type; |
| 5491 | bool report_source_offset; |
| 5492 | } table_options; |
| 5493 | |
| 5494 | static void |
| 5495 | table_options_init(grn_ctx *ctx, table_options *options) |
| 5496 | { |
| 5497 | options->target_table = NULL; |
| 5498 | options->target_index_column = NULL; |
| 5499 | options->target_section = 0; |
| 5500 | options->normalized_column = NULL; |
| 5501 | options->get_char_type = grn_nfkc_char_type; |
| 5502 | options->report_source_offset = false; |
| 5503 | } |
| 5504 | |
| 5505 | static void |
| 5506 | table_options_fin(grn_ctx *ctx, table_options *options) |
| 5507 | { |
| 5508 | /* We can't call grn_obj_unlink() safety here. Options are cached |
| 5509 | * and closed when the table that have this option is closed. It may |
| 5510 | * be occurred when DB is closed. In the case, |
| 5511 | * options->target_table, options->target_index_column or |
| 5512 | * options->normalized_column are already closed. If we call |
| 5513 | * grn_obj_unlink() with closed column/table, Groonga may be |
| 5514 | * crashed. */ |
| 5515 | |
| 5516 | if (options->normalized_column) { |
| 5517 | /* grn_obj_unlink(ctx, options->normalized_column); */ |
| 5518 | options->normalized_column = NULL; |
| 5519 | } |
| 5520 | if (options->target_index_column) { |
| 5521 | /* grn_obj_unlink(ctx, options->target_index_column); */ |
| 5522 | options->target_index_column = NULL; |
| 5523 | } |
| 5524 | if (options->target_table) { |
| 5525 | /* grn_obj_unlink(ctx, options->target_table); */ |
| 5526 | options->target_table = NULL; |
| 5527 | } |
| 5528 | } |
| 5529 | |
| 5530 | static void |
| 5531 | table_options_close(grn_ctx *ctx, void *data) |
| 5532 | { |
| 5533 | table_options *options = data; |
| 5534 | table_options_fin(ctx, options); |
| 5535 | GRN_FREE(options); |
| 5536 | } |
| 5537 | |
| 5538 | static void * |
| 5539 | table_options_open(grn_ctx *ctx, |
| 5540 | grn_obj *normalizer, |
| 5541 | grn_obj *raw_options, |
| 5542 | void *user_data) |
| 5543 | { |
| 5544 | const char *tag = "[normalizer][table]"; |
| 5545 | |
| 5546 | table_options *options = GRN_CALLOC(sizeof(table_options)); |
| 5547 | if (!options) { |
| 5548 | ERR(GRN_NO_MEMORY_AVAILABLE, |
| 5549 | "%s failed to allocate memory for options", |
| 5550 | tag); |
| 5551 | return NULL; |
| 5552 | } |
| 5553 | |
| 5554 | table_options_init(ctx, options); |
| 5555 | grn_obj target_name_buffer; |
| 5556 | GRN_TEXT_INIT(&target_name_buffer, 0); |
| 5557 | grn_obj *target_name = &target_name_buffer; |
| 5558 | GRN_OPTION_VALUES_EACH_BEGIN(ctx, raw_options, i, name, name_length) |
| 5559 | { |
| 5560 | grn_raw_string name_raw; |
| 5561 | name_raw.value = name; |
| 5562 | name_raw.length = name_length; |
| 5563 | |
| 5564 | if (GRN_RAW_STRING_EQUAL_CSTRING(name_raw, "column") || |
| 5565 | GRN_RAW_STRING_EQUAL_CSTRING(name_raw, "normalized")) { |
| 5566 | if (options->normalized_column) { |
| 5567 | ERR(GRN_INVALID_ARGUMENT, |
| 5568 | "%s[%.*s] must not specify " |
| 5569 | "both of <column> (deprecated) and <normalized>", |
| 5570 | tag, |
| 5571 | (int)(name_raw.length), |
| 5572 | name_raw.value); |
| 5573 | break; |
| 5574 | } |
| 5575 | const char *name; |
| 5576 | grn_id domain; |
| 5577 | unsigned int name_length = |
| 5578 | grn_vector_get_element(ctx, raw_options, i, &name, NULL, &domain); |
| 5579 | if (!grn_type_id_is_text_family(ctx, domain)) { |
| 5580 | grn_obj value; |
| 5581 | GRN_VALUE_FIX_SIZE_INIT(&value, GRN_OBJ_DO_SHALLOW_COPY, domain); |
| 5582 | GRN_TEXT_SET(ctx, &value, name, name_length); |
| 5583 | grn_obj inspected; |
| 5584 | grn_inspect(ctx, &inspected, &value); |
| 5585 | ERR(GRN_INVALID_ARGUMENT, |
| 5586 | "%s[%.*s] must be a text: <%.*s>", |
| 5587 | tag, |
| 5588 | (int)(name_raw.length), |
| 5589 | name_raw.value, |
| 5590 | (int)GRN_TEXT_LEN(&inspected), |
| 5591 | GRN_TEXT_VALUE(&inspected)); |
| 5592 | GRN_OBJ_FIN(ctx, &inspected); |
| 5593 | GRN_OBJ_FIN(ctx, &value); |
| 5594 | break; |
| 5595 | } |
| 5596 | if (name_length == 0) { |
| 5597 | ERR(GRN_INVALID_ARGUMENT, |
| 5598 | "%s[%.*s] must not be empty", |
| 5599 | tag, |
| 5600 | (int)(name_raw.length), |
| 5601 | name_raw.value); |
| 5602 | break; |
| 5603 | } |
| 5604 | options->normalized_column = grn_ctx_get(ctx, name, (int)name_length); |
| 5605 | if (!options->normalized_column) { |
| 5606 | ERR(GRN_INVALID_ARGUMENT, |
| 5607 | "%s[%.*s] nonexistent column: <%.*s>", |
| 5608 | tag, |
| 5609 | (int)(name_raw.length), |
| 5610 | name_raw.value, |
| 5611 | (int)name_length, |
| 5612 | name); |
| 5613 | break; |
| 5614 | } |
| 5615 | if (!grn_obj_is_text_family_scalar_column(ctx, |
| 5616 | options->normalized_column)) { |
| 5617 | grn_obj inspected; |
| 5618 | GRN_TEXT_INIT(&inspected, 0); |
| 5619 | grn_inspect_limited(ctx, &inspected, options->normalized_column); |
| 5620 | ERR(GRN_INVALID_ARGUMENT, |
| 5621 | "%s[%.*s] must be a text family scalar column: <%.*s>: <%.*s>", |
| 5622 | tag, |
| 5623 | (int)(name_raw.length), |
| 5624 | name_raw.value, |
| 5625 | (int)name_length, |
| 5626 | name, |
| 5627 | (int)GRN_TEXT_LEN(&inspected), |
| 5628 | GRN_TEXT_VALUE(&inspected)); |
| 5629 | GRN_OBJ_FIN(ctx, &inspected); |
| 5630 | break; |
| 5631 | } |
| 5632 | } else if (GRN_RAW_STRING_EQUAL_CSTRING(name_raw, "target")) { |
| 5633 | const char *name; |
| 5634 | grn_id domain; |
| 5635 | unsigned int name_length = |
| 5636 | grn_vector_get_element(ctx, raw_options, i, &name, NULL, &domain); |
| 5637 | if (!grn_type_id_is_text_family(ctx, domain)) { |
| 5638 | grn_obj value; |
| 5639 | GRN_VALUE_FIX_SIZE_INIT(&value, GRN_OBJ_DO_SHALLOW_COPY, domain); |
| 5640 | GRN_TEXT_SET(ctx, &value, name, name_length); |
| 5641 | grn_obj inspected; |
| 5642 | grn_inspect(ctx, &inspected, &value); |
| 5643 | ERR(GRN_INVALID_ARGUMENT, |
| 5644 | "%s[%.*s] must be a text: <%.*s>", |
| 5645 | tag, |
| 5646 | (int)(name_raw.length), |
| 5647 | name_raw.value, |
| 5648 | (int)GRN_TEXT_LEN(&inspected), |
| 5649 | GRN_TEXT_VALUE(&inspected)); |
| 5650 | GRN_OBJ_FIN(ctx, &inspected); |
| 5651 | GRN_OBJ_FIN(ctx, &value); |
| 5652 | break; |
| 5653 | } |
| 5654 | if (name_length == 0) { |
| 5655 | ERR(GRN_INVALID_ARGUMENT, |
| 5656 | "%s[%.*s] must not be empty", |
| 5657 | tag, |
| 5658 | (int)(name_raw.length), |
| 5659 | name_raw.value); |
| 5660 | break; |
| 5661 | } |
| 5662 | GRN_TEXT_SET(ctx, target_name, name, name_length); |
| 5663 | } else if (GRN_RAW_STRING_EQUAL_CSTRING(name_raw, "unicode_version")) { |
| 5664 | grn_nfkc_funcs funcs = |
| 5665 | grn_nfkc_version_option_process(ctx, raw_options, i, &name_raw, tag); |
| 5666 | if (ctx->rc != GRN_SUCCESS) { |
| 5667 | break; |
| 5668 | } |
| 5669 | options->get_char_type = funcs.char_type_func; |
| 5670 | } else if (GRN_RAW_STRING_EQUAL_CSTRING(name_raw, "report_source_offset")) { |
| 5671 | options->report_source_offset = |
| 5672 | grn_vector_get_element_bool(ctx, |
| 5673 | raw_options, |
| 5674 | i, |
| 5675 | options->report_source_offset); |
| 5676 | } |
| 5677 | } |
| 5678 | GRN_OPTION_VALUES_EACH_END(); |
| 5679 | if (ctx->rc != GRN_SUCCESS) { |
| 5680 | goto exit; |
| 5681 | } |
| 5682 | |
| 5683 | if (!options->normalized_column) { |
| 5684 | ERR(GRN_INVALID_ARGUMENT, "%s[normalized] not specified", tag); |
| 5685 | goto exit; |
| 5686 | } |
| 5687 | |
| 5688 | const char *target_table_option_name = ""; |
| 5689 | grn_raw_string raw_target_name; |
| 5690 | GRN_RAW_STRING_SET(raw_target_name, target_name); |
| 5691 | if (GRN_RAW_STRING_EQUAL_CSTRING(raw_target_name, "") || |
| 5692 | GRN_RAW_STRING_EQUAL_CSTRING(raw_target_name, "_key")) { |
| 5693 | target_table_option_name = "normalized"; |
| 5694 | options->target_table = |
| 5695 | grn_ctx_at(ctx, options->normalized_column->header.domain); |
| 5696 | /* clang-format off */ |
| 5697 | /* TODO: Enable this when we add support for |
| 5698 | "Lexicon.index_column['source_name']" syntax. */ |
| 5699 | /* |
| 5700 | } else if (grn_raw_string_have_sub_string_cstring(ctx, |
| 5701 | &raw_target_name, |
| 5702 | ".")) { |
| 5703 | target_table_option_name = "target"; |
| 5704 | options->target_index_column = |
| 5705 | grn_ctx_get(ctx, |
| 5706 | raw_target_name.value, |
| 5707 | raw_target_name.length); |
| 5708 | if (!grn_obj_is_index_column(ctx, options->target_index_column)) { |
| 5709 | grn_obj inspected; |
| 5710 | GRN_TEXT_INIT(&inspected, 0); |
| 5711 | grn_inspect_limited(ctx, &inspected, options->target_index_column); |
| 5712 | ERR(GRN_INVALID_ARGUMENT, |
| 5713 | "%s[target] must be a column " |
| 5714 | "in the same table as the normalized column or " |
| 5715 | "index column to resolve the table of the normalized column: <%.*s>", |
| 5716 | tag, |
| 5717 | (int)GRN_TEXT_LEN(&inspected), GRN_TEXT_VALUE(&inspected)); |
| 5718 | GRN_OBJ_FIN(ctx, &inspected); |
| 5719 | goto exit; |
| 5720 | } |
| 5721 | if (DB_OBJ(options->target_index_column)->range != |
| 5722 | options->normalized_column->header.domain) { |
| 5723 | grn_obj inspected_target; |
| 5724 | GRN_TEXT_INIT(&inspected_target, 0); |
| 5725 | grn_inspect_limited(ctx, &inspected_target, options->target_index_column); |
| 5726 | grn_obj inspected_normalized; |
| 5727 | GRN_TEXT_INIT(&inspected_normalized, 0); |
| 5728 | grn_obj *normalized_table = |
| 5729 | grn_ctx_at(ctx, options->normalized_column->header.domain); |
| 5730 | grn_inspect_limited(ctx, &inspected_normalized, normalized_table); |
| 5731 | grn_obj_unref(ctx, normalized_table); |
| 5732 | ERR(GRN_INVALID_ARGUMENT, |
| 5733 | "%s[target] index column must be " |
| 5734 | "for the table of the normalized column: <%.*s>: <%.*s>", |
| 5735 | tag, |
| 5736 | (int)GRN_TEXT_LEN(&inspected_target), |
| 5737 | GRN_TEXT_VALUE(&inspected_target), |
| 5738 | (int)GRN_TEXT_LEN(&inspected_normalized), |
| 5739 | GRN_TEXT_VALUE(&inspected_normalized)); |
| 5740 | GRN_OBJ_FIN(ctx, &inspected_normalized); |
| 5741 | GRN_OBJ_FIN(ctx, &inspected_target); |
| 5742 | goto exit; |
| 5743 | } |
| 5744 | options->target_table = |
| 5745 | grn_ctx_at(ctx, options->target_index_column->header.domain); |
| 5746 | */ |
| 5747 | /* clang-format on */ |
| 5748 | } else { |
| 5749 | target_table_option_name = "target"; |
| 5750 | grn_obj *normalized_table = |
| 5751 | grn_ctx_at(ctx, options->normalized_column->header.domain); |
| 5752 | grn_obj *target_column = |
| 5753 | grn_table_column(ctx, |
| 5754 | normalized_table, |
| 5755 | raw_target_name.value, |
| 5756 | (ssize_t)(raw_target_name.length)); |
| 5757 | grn_obj_unref(ctx, normalized_table); |
| 5758 | if (!target_column) { |
| 5759 | ERR(GRN_INVALID_ARGUMENT, |
| 5760 | "%s[%s] nonexistent column: <%.*s>", |
| 5761 | tag, |
| 5762 | target_table_option_name, |
| 5763 | (int)(raw_target_name.length), |
| 5764 | raw_target_name.value); |
| 5765 | goto exit; |
| 5766 | } |
| 5767 | grn_index_datum index_datum; |
| 5768 | unsigned int n_indexes = grn_column_find_index_data(ctx, |
| 5769 | target_column, |
| 5770 | GRN_OP_EQUAL, |
| 5771 | &index_datum, |
| 5772 | 1); |
| 5773 | grn_obj_unref(ctx, target_column); |
| 5774 | if (n_indexes == 0) { |
| 5775 | ERR(GRN_INVALID_ARGUMENT, |
| 5776 | "%s[target] no equal index: <%.*s>", |
| 5777 | tag, |
| 5778 | (int)(raw_target_name.length), |
| 5779 | raw_target_name.value); |
| 5780 | goto exit; |
| 5781 | } |
| 5782 | options->target_index_column = index_datum.index; |
| 5783 | options->target_section = index_datum.section; |
| 5784 | options->target_table = |
| 5785 | grn_ctx_at(ctx, options->target_index_column->header.domain); |
| 5786 | } |
| 5787 | |
| 5788 | if (options->target_table->header.type != GRN_TABLE_PAT_KEY) { |
| 5789 | grn_obj inspected; |
| 5790 | GRN_TEXT_INIT(&inspected, 0); |
| 5791 | grn_inspect_limited(ctx, &inspected, options->target_table); |
| 5792 | ERR(GRN_INVALID_ARGUMENT, |
| 5793 | "%s[%s] table must be a TABLE_PAT_KEY: <%.*s>", |
| 5794 | tag, |
| 5795 | target_table_option_name, |
| 5796 | (int)GRN_TEXT_LEN(&inspected), |
| 5797 | GRN_TEXT_VALUE(&inspected)); |
| 5798 | GRN_OBJ_FIN(ctx, &inspected); |
| 5799 | goto exit; |
| 5800 | } |
| 5801 | if (options->target_table->header.domain != GRN_DB_SHORT_TEXT) { |
| 5802 | grn_obj inspected; |
| 5803 | GRN_TEXT_INIT(&inspected, 0); |
| 5804 | grn_inspect_limited(ctx, &inspected, options->target_table); |
| 5805 | ERR(GRN_INVALID_ARGUMENT, |
| 5806 | "%s[%s] table's key must be ShortText: <%.*s>", |
| 5807 | tag, |
| 5808 | target_table_option_name, |
| 5809 | (int)GRN_TEXT_LEN(&inspected), |
| 5810 | GRN_TEXT_VALUE(&inspected)); |
| 5811 | GRN_OBJ_FIN(ctx, &inspected); |
| 5812 | goto exit; |
| 5813 | } |
| 5814 | |
| 5815 | exit: |
| 5816 | GRN_OBJ_FIN(ctx, &target_name_buffer); |
| 5817 | if (ctx->rc != GRN_SUCCESS) { |
| 5818 | table_options_fin(ctx, options); |
| 5819 | table_options_close(ctx, options); |
| 5820 | return NULL; |
| 5821 | } |
| 5822 | |
| 5823 | return options; |
| 5824 | } |
| 5825 | |
| 5826 | typedef struct { |
| 5827 | const char *tag; |
| 5828 | table_options *options; |
| 5829 | substitutor_data substitutor_data; |
| 5830 | } table_data; |
| 5831 | |
| 5832 | static void |
| 5833 | table_normalize(grn_ctx *ctx, grn_string *string, table_options *options) |
| 5834 | { |
| 5835 | table_data data; |
| 5836 | data.tag = "[normalizer][table]"; |
| 5837 | data.options = options; |
| 5838 | substitutor_data_init(ctx, |
| 5839 | &(data.substitutor_data), |
| 5840 | data.tag, |
| 5841 | options->get_char_type, |
| 5842 | string, |
| 5843 | options->report_source_offset); |
| 5844 | |
| 5845 | string->n_characters = 0; |
| 5846 | string->normalized = NULL; |
| 5847 | string->normalized_length_in_bytes = 0; |
| 5848 | string->checks = NULL; |
| 5849 | string->ctypes = NULL; |
| 5850 | string->offsets = NULL; |
| 5851 | |
| 5852 | grn_obj *normalized = &(data.substitutor_data.normalized); |
| 5853 | const char *source = string->original; |
| 5854 | const char *source_end = source + string->original_length_in_bytes; |
| 5855 | while (source < source_end) { |
| 5856 | #define MAX_N_HITS 16 |
| 5857 | grn_pat_scan_hit hits[MAX_N_HITS]; |
| 5858 | const char *rest; |
| 5859 | int i, n_hits; |
| 5860 | unsigned int previous = 0; |
| 5861 | size_t chunk_length; |
| 5862 | |
| 5863 | n_hits = grn_pat_scan(ctx, |
| 5864 | (grn_pat *)(options->target_table), |
| 5865 | source, |
| 5866 | (unsigned int)(source_end - source), |
| 5867 | hits, |
| 5868 | MAX_N_HITS, |
| 5869 | &rest); |
| 5870 | for (i = 0; i < n_hits; i++) { |
| 5871 | if (hits[i].offset > previous) { |
| 5872 | const char *source_previous = source + previous; |
| 5873 | size_t source_previous_length = hits[i].offset - previous; |
| 5874 | GRN_TEXT_PUT(ctx, normalized, source_previous, source_previous_length); |
| 5875 | substitutor_normalized(ctx, |
| 5876 | &(data.substitutor_data), |
| 5877 | source_previous, |
| 5878 | source_previous_length, |
| 5879 | source_previous, |
| 5880 | source_previous_length); |
| 5881 | if (ctx->rc != GRN_SUCCESS) { |
| 5882 | goto exit; |
| 5883 | } |
| 5884 | } |
| 5885 | size_t before_offset = GRN_TEXT_LEN(normalized); |
| 5886 | if (options->target_index_column) { |
| 5887 | bool found = false; |
| 5888 | grn_ii *ii = (grn_ii *)(options->target_index_column); |
| 5889 | grn_ii_cursor *cursor = grn_ii_cursor_open(ctx, |
| 5890 | ii, |
| 5891 | hits[i].id, |
| 5892 | GRN_ID_NIL, |
| 5893 | GRN_ID_MAX, |
| 5894 | (int)(ii->n_elements), |
| 5895 | 0); |
| 5896 | if (cursor) { |
| 5897 | grn_posting *ii_posting; |
| 5898 | while ((ii_posting = grn_ii_cursor_next(ctx, cursor))) { |
| 5899 | if (options->target_section == 0 || |
| 5900 | ii_posting->sid == options->target_section) { |
| 5901 | grn_obj_get_value(ctx, |
| 5902 | options->normalized_column, |
| 5903 | ii_posting->rid, |
| 5904 | normalized); |
| 5905 | found = true; |
| 5906 | break; |
| 5907 | } |
| 5908 | } |
| 5909 | grn_ii_cursor_close(ctx, cursor); |
| 5910 | } |
| 5911 | if (!found) { |
| 5912 | GRN_TEXT_PUT(ctx, |
| 5913 | normalized, |
| 5914 | source + hits[i].offset, |
| 5915 | hits[i].length); |
| 5916 | } |
| 5917 | } else { |
| 5918 | grn_obj_get_value(ctx, |
| 5919 | options->normalized_column, |
| 5920 | hits[i].id, |
| 5921 | normalized); |
| 5922 | } |
| 5923 | substitutor_normalized(ctx, |
| 5924 | &(data.substitutor_data), |
| 5925 | source + hits[i].offset, |
| 5926 | hits[i].length, |
| 5927 | GRN_TEXT_VALUE(normalized) + before_offset, |
| 5928 | GRN_TEXT_LEN(normalized) - before_offset); |
| 5929 | if (ctx->rc != GRN_SUCCESS) { |
| 5930 | goto exit; |
| 5931 | } |
| 5932 | previous = hits[i].offset + hits[i].length; |
| 5933 | } |
| 5934 | |
| 5935 | chunk_length = (size_t)(rest - source); |
| 5936 | if (chunk_length - previous > 0) { |
| 5937 | const char *source_previous = source + previous; |
| 5938 | size_t source_previous_length = (size_t)(source_end - source - previous); |
| 5939 | GRN_TEXT_PUT(ctx, normalized, source_previous, source_previous_length); |
| 5940 | substitutor_normalized(ctx, |
| 5941 | &(data.substitutor_data), |
| 5942 | source_previous, |
| 5943 | source_previous_length, |
| 5944 | source_previous, |
| 5945 | source_previous_length); |
| 5946 | if (ctx->rc != GRN_SUCCESS) { |
| 5947 | goto exit; |
| 5948 | } |
| 5949 | } |
| 5950 | source = rest; |
| 5951 | #undef MAX_N_HITS |
| 5952 | } |
| 5953 | |
| 5954 | substitutor_finished(ctx, &(data.substitutor_data), string); |
| 5955 | |
| 5956 | exit: |
| 5957 | substitutor_data_fin(ctx, &(data.substitutor_data)); |
| 5958 | } |
| 5959 | |
| 5960 | static grn_obj * |
| 5961 | table_next(grn_ctx *ctx, int nargs, grn_obj **args, grn_user_data *user_data) |
| 5962 | { |
| 5963 | grn_obj *string = args[0]; |
| 5964 | grn_obj *table; |
| 5965 | table_options *options; |
| 5966 | table_options options_raw; |
| 5967 | |
| 5968 | table = grn_string_get_table(ctx, string); |
| 5969 | if (table) { |
| 5970 | uint32_t i = grn_string_get_normalizer_index(ctx, string); |
| 5971 | options = grn_table_cache_normalizers_options(ctx, |
| 5972 | table, |
| 5973 | i, |
| 5974 | table_options_open, |
| 5975 | table_options_close, |
| 5976 | NULL); |
| 5977 | if (ctx->rc != GRN_SUCCESS) { |
| 5978 | return NULL; |
| 5979 | } |
| 5980 | } else { |
| 5981 | table_options_init(ctx, &options_raw); |
| 5982 | if (ctx->rc != GRN_SUCCESS) { |
| 5983 | table_options_fin(ctx, &options_raw); |
| 5984 | return NULL; |
| 5985 | } |
| 5986 | options = &options_raw; |
| 5987 | } |
| 5988 | |
| 5989 | table_normalize(ctx, (grn_string *)string, options); |
| 5990 | |
| 5991 | if (!table) { |
| 5992 | table_options_fin(ctx, options); |
| 5993 | } |
| 5994 | return NULL; |
| 5995 | } |
| 5996 | |
| 5997 | typedef struct { |
| 5998 | bool remove_tag; |
| 5999 | bool expand_character_reference; |
| 6000 | grn_nfkc_char_type_func get_char_type; |
| 6001 | bool report_source_offset; |
| 6002 | } html_options; |
| 6003 | |
| 6004 | static void |
| 6005 | html_options_init(grn_ctx *ctx, html_options *options) |
| 6006 | { |
| 6007 | options->remove_tag = true; |
| 6008 | options->expand_character_reference = true; |
| 6009 | options->get_char_type = grn_nfkc_char_type; |
| 6010 | options->report_source_offset = false; |
| 6011 | } |
| 6012 | |
| 6013 | static void |
| 6014 | html_options_fin(grn_ctx *ctx, html_options *options) |
| 6015 | { |
| 6016 | } |
| 6017 | |
| 6018 | static void |
| 6019 | html_options_close(grn_ctx *ctx, void *data) |
| 6020 | { |
| 6021 | html_options *options = data; |
| 6022 | html_options_fin(ctx, options); |
| 6023 | GRN_FREE(options); |
| 6024 | } |
| 6025 | |
| 6026 | static void * |
| 6027 | html_options_open(grn_ctx *ctx, |
| 6028 | grn_obj *normalizer, |
| 6029 | grn_obj *raw_options, |
| 6030 | void *user_data) |
| 6031 | { |
| 6032 | const char *tag = "[normalizer][html]"; |
| 6033 | |
| 6034 | html_options *options = GRN_CALLOC(sizeof(html_options)); |
| 6035 | if (!options) { |
| 6036 | ERR(GRN_NO_MEMORY_AVAILABLE, |
| 6037 | "%s failed to allocate memory for options", |
| 6038 | tag); |
| 6039 | return NULL; |
| 6040 | } |
| 6041 | |
| 6042 | html_options_init(ctx, options); |
| 6043 | GRN_OPTION_VALUES_EACH_BEGIN(ctx, raw_options, i, name, name_length) |
| 6044 | { |
| 6045 | grn_raw_string name_raw; |
| 6046 | name_raw.value = name; |
| 6047 | name_raw.length = name_length; |
| 6048 | |
| 6049 | if (GRN_RAW_STRING_EQUAL_CSTRING(name_raw, "remove_tag")) { |
| 6050 | options->remove_tag = |
| 6051 | grn_vector_get_element_bool(ctx, raw_options, i, options->remove_tag); |
| 6052 | } else if (GRN_RAW_STRING_EQUAL_CSTRING(name_raw, |
| 6053 | "expand_character_reference")) { |
| 6054 | options->expand_character_reference = |
| 6055 | grn_vector_get_element_bool(ctx, |
| 6056 | raw_options, |
| 6057 | i, |
| 6058 | options->expand_character_reference); |
| 6059 | } else if (GRN_RAW_STRING_EQUAL_CSTRING(name_raw, "unicode_version")) { |
| 6060 | grn_nfkc_funcs funcs = |
| 6061 | grn_nfkc_version_option_process(ctx, raw_options, i, &name_raw, tag); |
| 6062 | if (ctx->rc != GRN_SUCCESS) { |
| 6063 | break; |
| 6064 | } |
| 6065 | options->get_char_type = funcs.char_type_func; |
| 6066 | } else if (GRN_RAW_STRING_EQUAL_CSTRING(name_raw, "report_source_offset")) { |
| 6067 | options->report_source_offset = |
| 6068 | grn_vector_get_element_bool(ctx, |
| 6069 | raw_options, |
| 6070 | i, |
| 6071 | options->report_source_offset); |
| 6072 | } |
| 6073 | } |
| 6074 | GRN_OPTION_VALUES_EACH_END(); |
| 6075 | |
| 6076 | if (ctx->rc != GRN_SUCCESS) { |
| 6077 | html_options_fin(ctx, options); |
| 6078 | html_options_close(ctx, options); |
| 6079 | return NULL; |
| 6080 | } |
| 6081 | |
| 6082 | return options; |
| 6083 | } |
| 6084 | |
| 6085 | static bool |
| 6086 | html_expand_numeric_char_ref(grn_ctx *ctx, grn_obj *buffer, uint32_t code_point) |
| 6087 | { |
| 6088 | /* Special code points. |
| 6089 | * See also: |
| 6090 | * https://html.spec.whatwg.org/multipage/parsing.html#numeric-character-reference-end-state |
| 6091 | */ |
| 6092 | switch (code_point) { |
| 6093 | case 0x80: |
| 6094 | code_point = 0x20AC; |
| 6095 | break; |
| 6096 | case 0x82: |
| 6097 | code_point = 0x201A; |
| 6098 | break; |
| 6099 | case 0x83: |
| 6100 | code_point = 0x0192; |
| 6101 | break; |
| 6102 | case 0x84: |
| 6103 | code_point = 0x201E; |
| 6104 | break; |
| 6105 | case 0x85: |
| 6106 | code_point = 0x2026; |
| 6107 | break; |
| 6108 | case 0x86: |
| 6109 | code_point = 0x2020; |
| 6110 | break; |
| 6111 | case 0x87: |
| 6112 | code_point = 0x2021; |
| 6113 | break; |
| 6114 | case 0x88: |
| 6115 | code_point = 0x02C6; |
| 6116 | break; |
| 6117 | case 0x89: |
| 6118 | code_point = 0x2030; |
| 6119 | break; |
| 6120 | case 0x8A: |
| 6121 | code_point = 0x0160; |
| 6122 | break; |
| 6123 | case 0x8B: |
| 6124 | code_point = 0x2039; |
| 6125 | break; |
| 6126 | case 0x8C: |
| 6127 | code_point = 0x0152; |
| 6128 | break; |
| 6129 | case 0x8E: |
| 6130 | code_point = 0x017D; |
| 6131 | break; |
| 6132 | case 0x91: |
| 6133 | code_point = 0x2018; |
| 6134 | break; |
| 6135 | case 0x92: |
| 6136 | code_point = 0x2019; |
| 6137 | break; |
| 6138 | case 0x93: |
| 6139 | code_point = 0x201C; |
| 6140 | break; |
| 6141 | case 0x94: |
| 6142 | code_point = 0x201D; |
| 6143 | break; |
| 6144 | case 0x95: |
| 6145 | code_point = 0x2022; |
| 6146 | break; |
| 6147 | case 0x96: |
| 6148 | code_point = 0x2013; |
| 6149 | break; |
| 6150 | case 0x97: |
| 6151 | code_point = 0x2014; |
| 6152 | break; |
| 6153 | case 0x98: |
| 6154 | code_point = 0x02DC; |
| 6155 | break; |
| 6156 | case 0x99: |
| 6157 | code_point = 0x2122; |
| 6158 | break; |
| 6159 | case 0x9A: |
| 6160 | code_point = 0x0161; |
| 6161 | break; |
| 6162 | case 0x9B: |
| 6163 | code_point = 0x203A; |
| 6164 | break; |
| 6165 | case 0x9C: |
| 6166 | code_point = 0x0153; |
| 6167 | break; |
| 6168 | case 0x9E: |
| 6169 | code_point = 0x017E; |
| 6170 | break; |
| 6171 | case 0x9F: |
| 6172 | code_point = 0x0178; |
| 6173 | break; |
| 6174 | default: |
| 6175 | break; |
| 6176 | } |
| 6177 | grn_text_code_point(ctx, buffer, code_point); |
| 6178 | return true; |
| 6179 | } |
| 6180 | |
| 6181 | #include "normalizer_html_expand_named_char_ref.c" |
| 6182 | |
| 6183 | typedef enum { |
| 6184 | HTML_STATE_TEXT, |
| 6185 | HTML_STATE_IN_TAG, |
| 6186 | HTML_STATE_IN_CHAR_REF, |
| 6187 | } html_state; |
| 6188 | |
| 6189 | static void |
| 6190 | html_normalize(grn_ctx *ctx, grn_string *string, html_options *options) |
| 6191 | { |
| 6192 | const char *tag = "[normalizer][html]"; |
| 6193 | substitutor_data data; |
| 6194 | substitutor_data_init(ctx, |
| 6195 | &data, |
| 6196 | tag, |
| 6197 | options->get_char_type, |
| 6198 | string, |
| 6199 | options->report_source_offset); |
| 6200 | |
| 6201 | string->n_characters = 0; |
| 6202 | string->normalized = NULL; |
| 6203 | string->normalized_length_in_bytes = 0; |
| 6204 | string->checks = NULL; |
| 6205 | string->ctypes = NULL; |
| 6206 | string->offsets = NULL; |
| 6207 | |
| 6208 | grn_obj *normalized = &(data.normalized); |
| 6209 | const char *current = string->original; |
| 6210 | const char *end = current + string->original_length_in_bytes; |
| 6211 | |
| 6212 | html_state state = HTML_STATE_TEXT; |
| 6213 | const char *start_tag = NULL; |
| 6214 | const char *start_char_ref = NULL; |
| 6215 | |
| 6216 | int char_length = 0; |
| 6217 | for (; current < end; current += char_length) { |
| 6218 | char_length = grn_charlen(ctx, current, end); |
| 6219 | if (char_length == 0) { |
| 6220 | /* Set error? */ |
| 6221 | break; |
| 6222 | } |
| 6223 | |
| 6224 | if (char_length == 1) { |
| 6225 | bool processed = false; |
| 6226 | switch (state) { |
| 6227 | case HTML_STATE_IN_TAG: |
| 6228 | if (*current == '>') { |
| 6229 | substitutor_normalized(ctx, |
| 6230 | &data, |
| 6231 | start_tag, |
| 6232 | (size_t)(current - start_tag + 1), |
| 6233 | NULL, |
| 6234 | 0); |
| 6235 | state = HTML_STATE_TEXT; |
| 6236 | start_tag = NULL; |
| 6237 | processed = true; |
| 6238 | } |
| 6239 | break; |
| 6240 | case HTML_STATE_IN_CHAR_REF: |
| 6241 | if (*current == ';') { |
| 6242 | bool valid = false; |
| 6243 | size_t normalized_length_before = GRN_TEXT_LEN(normalized); |
| 6244 | if (start_char_ref[1] == '#') { |
| 6245 | long code_point = -1; |
| 6246 | if (start_char_ref[2] == 'x' || start_char_ref[2] == 'X') { |
| 6247 | /* "&#xH...;" */ |
| 6248 | const char *code_point_end = NULL; |
| 6249 | code_point = |
| 6250 | grn_htoui(start_char_ref + 3, current, &code_point_end); |
| 6251 | if (code_point_end != current) { |
| 6252 | code_point = -1; |
| 6253 | } |
| 6254 | } else { |
| 6255 | /* "&#D...;" */ |
| 6256 | const char *code_point_end = NULL; |
| 6257 | code_point = |
| 6258 | grn_atoll(start_char_ref + 2, current, &code_point_end); |
| 6259 | if (code_point_end != current) { |
| 6260 | code_point = -1; |
| 6261 | } |
| 6262 | } |
| 6263 | if (code_point >= 0) { |
| 6264 | valid = html_expand_numeric_char_ref(ctx, |
| 6265 | normalized, |
| 6266 | (uint32_t)code_point); |
| 6267 | } |
| 6268 | } else { |
| 6269 | /* "&NAME;" */ |
| 6270 | valid = html_expand_named_char_ref( |
| 6271 | ctx, |
| 6272 | normalized, |
| 6273 | start_char_ref + 1, |
| 6274 | (size_t)(current - start_char_ref - 1)); |
| 6275 | } |
| 6276 | if (valid) { |
| 6277 | const char *current_normalized = |
| 6278 | GRN_TEXT_VALUE(normalized) + normalized_length_before; |
| 6279 | size_t current_normalized_length = |
| 6280 | GRN_TEXT_LEN(normalized) - normalized_length_before; |
| 6281 | substitutor_normalized(ctx, |
| 6282 | &data, |
| 6283 | start_char_ref, |
| 6284 | (size_t)(current - start_char_ref + 1), |
| 6285 | current_normalized, |
| 6286 | current_normalized_length); |
| 6287 | if (ctx->rc != GRN_SUCCESS) { |
| 6288 | goto exit; |
| 6289 | } |
| 6290 | state = HTML_STATE_TEXT; |
| 6291 | start_char_ref = NULL; |
| 6292 | processed = true; |
| 6293 | } |
| 6294 | } else if (((current - start_char_ref) == 1 && *current == '#') || |
| 6295 | ('A' <= *current && *current <= 'Z') || |
| 6296 | ('a' <= *current && *current <= 'z') || |
| 6297 | ('0' <= *current && *current <= '9')) { |
| 6298 | processed = true; |
| 6299 | } |
| 6300 | default: |
| 6301 | if (options->remove_tag && *current == '<') { |
| 6302 | state = HTML_STATE_IN_TAG; |
| 6303 | start_tag = current; |
| 6304 | processed = true; |
| 6305 | } else if (options->expand_character_reference && *current == '&') { |
| 6306 | state = HTML_STATE_IN_CHAR_REF; |
| 6307 | start_char_ref = current; |
| 6308 | processed = true; |
| 6309 | } |
| 6310 | break; |
| 6311 | } |
| 6312 | if (processed) { |
| 6313 | continue; |
| 6314 | } |
| 6315 | } |
| 6316 | |
| 6317 | switch (state) { |
| 6318 | case HTML_STATE_IN_TAG: |
| 6319 | break; |
| 6320 | case HTML_STATE_IN_CHAR_REF: |
| 6321 | { |
| 6322 | /* "&...(any multibyte character)...;" case: Write it as-is. */ |
| 6323 | size_t normalized_length = |
| 6324 | (size_t)(current - start_char_ref) + (size_t)char_length; |
| 6325 | grn_bulk_write(ctx, normalized, start_char_ref, normalized_length); |
| 6326 | substitutor_normalized(ctx, |
| 6327 | &data, |
| 6328 | start_char_ref, |
| 6329 | normalized_length, |
| 6330 | start_char_ref, |
| 6331 | normalized_length); |
| 6332 | state = HTML_STATE_TEXT; |
| 6333 | start_char_ref = NULL; |
| 6334 | } |
| 6335 | default: |
| 6336 | grn_bulk_write(ctx, normalized, current, (size_t)char_length); |
| 6337 | substitutor_normalized(ctx, |
| 6338 | &data, |
| 6339 | current, |
| 6340 | (size_t)char_length, |
| 6341 | current, |
| 6342 | (size_t)char_length); |
| 6343 | break; |
| 6344 | } |
| 6345 | } |
| 6346 | if (state == HTML_STATE_IN_TAG) { |
| 6347 | substitutor_normalized(ctx, |
| 6348 | &data, |
| 6349 | start_tag, |
| 6350 | (size_t)(current - start_tag), |
| 6351 | NULL, |
| 6352 | 0); |
| 6353 | } |
| 6354 | |
| 6355 | substitutor_finished(ctx, &data, string); |
| 6356 | |
| 6357 | exit: |
| 6358 | substitutor_data_fin(ctx, &data); |
| 6359 | } |
| 6360 | |
| 6361 | static grn_obj * |
| 6362 | html_next(grn_ctx *ctx, int nargs, grn_obj **args, grn_user_data *user_data) |
| 6363 | { |
| 6364 | grn_obj *string = args[0]; |
| 6365 | grn_obj *table; |
| 6366 | html_options *options; |
| 6367 | html_options options_raw; |
| 6368 | |
| 6369 | table = grn_string_get_table(ctx, string); |
| 6370 | if (table) { |
| 6371 | uint32_t i = grn_string_get_normalizer_index(ctx, string); |
| 6372 | options = grn_table_cache_normalizers_options(ctx, |
| 6373 | table, |
| 6374 | i, |
| 6375 | html_options_open, |
| 6376 | html_options_close, |
| 6377 | NULL); |
| 6378 | if (ctx->rc != GRN_SUCCESS) { |
| 6379 | return NULL; |
| 6380 | } |
| 6381 | } else { |
| 6382 | html_options_init(ctx, &options_raw); |
| 6383 | if (ctx->rc != GRN_SUCCESS) { |
| 6384 | html_options_fin(ctx, &options_raw); |
| 6385 | return NULL; |
| 6386 | } |
| 6387 | options = &options_raw; |
| 6388 | } |
| 6389 | |
| 6390 | html_normalize(ctx, (grn_string *)string, options); |
| 6391 | |
| 6392 | if (!table) { |
| 6393 | html_options_fin(ctx, options); |
| 6394 | } |
| 6395 | return NULL; |
| 6396 | } |
| 6397 | |
| 6398 | grn_rc |
| 6399 | grn_normalizer_normalize(grn_ctx *ctx, grn_obj *normalizer, grn_obj *string) |
| 6400 | { |
| 6401 | grn_rc rc; |
| 6402 | int nargs = 0; |
| 6403 | |
| 6404 | grn_ctx_push(ctx, string); |
| 6405 | nargs++; |
| 6406 | rc = grn_proc_call(ctx, normalizer, nargs, NULL); |
| 6407 | grn_ctx_pop(ctx); |
| 6408 | |
| 6409 | return rc; |
| 6410 | } |
| 6411 | |
| 6412 | grn_rc |
| 6413 | grn_db_init_builtin_normalizers(grn_ctx *ctx) |
| 6414 | { |
| 6415 | const char *normalizer_nfkc51_name = "NormalizerNFKC51"; |
| 6416 | const char *normalizer_nfkc100_name = "NormalizerNFKC100"; |
| 6417 | const char *normalizer_nfkc121_name = "NormalizerNFKC121"; |
| 6418 | const char *normalizer_nfkc130_name = "NormalizerNFKC130"; |
| 6419 | const char *normalizer_nfkc150_name = "NormalizerNFKC150"; |
| 6420 | const char *normalizer_nfkc_name = "NormalizerNFKC"; |
| 6421 | |
| 6422 | grn_normalizer_register(ctx, |
| 6423 | GRN_NORMALIZER_AUTO_NAME, |
| 6424 | -1, |
| 6425 | NULL, |
| 6426 | auto_next, |
| 6427 | NULL); |
| 6428 | |
| 6429 | #ifdef GRN_WITH_NFKC |
| 6430 | grn_normalizer_register(ctx, |
| 6431 | normalizer_nfkc51_name, |
| 6432 | -1, |
| 6433 | NULL, |
| 6434 | nfkc51_next, |
| 6435 | NULL); |
| 6436 | grn_normalizer_register(ctx, |
| 6437 | normalizer_nfkc100_name, |
| 6438 | -1, |
| 6439 | NULL, |
| 6440 | nfkc100_next, |
| 6441 | NULL); |
| 6442 | grn_normalizer_register(ctx, |
| 6443 | normalizer_nfkc121_name, |
| 6444 | -1, |
| 6445 | NULL, |
| 6446 | nfkc121_next, |
| 6447 | NULL); |
| 6448 | grn_normalizer_register(ctx, |
| 6449 | normalizer_nfkc130_name, |
| 6450 | -1, |
| 6451 | NULL, |
| 6452 | nfkc130_next, |
| 6453 | NULL); |
| 6454 | grn_normalizer_register(ctx, |
| 6455 | normalizer_nfkc150_name, |
| 6456 | -1, |
| 6457 | NULL, |
| 6458 | nfkc150_next, |
| 6459 | NULL); |
| 6460 | grn_normalizer_register(ctx, normalizer_nfkc_name, -1, NULL, nfkc_next, NULL); |
| 6461 | #else /* GRN_WITH_NFKC */ |
| 6462 | grn_normalizer_register(ctx, normalizer_nfkc51_name, -1, NULL, NULL, NULL); |
| 6463 | grn_normalizer_register(ctx, normalizer_nfkc100_name, -1, NULL, NULL, NULL); |
| 6464 | grn_normalizer_register(ctx, normalizer_nfkc121_name, -1, NULL, NULL, NULL); |
| 6465 | grn_normalizer_register(ctx, normalizer_nfkc130_name, -1, NULL, NULL, NULL); |
| 6466 | grn_normalizer_register(ctx, normalizer_nfkc150_name, -1, NULL, NULL, NULL); |
| 6467 | grn_normalizer_register(ctx, normalizer_nfkc_name, -1, NULL, NULL, NULL); |
| 6468 | #endif /* GRN_WITH_NFKC */ |
| 6469 | |
| 6470 | grn_normalizer_register(ctx, "NormalizerTable", -1, NULL, table_next, NULL); |
| 6471 | |
| 6472 | grn_normalizer_register(ctx, "NormalizerHTML", -1, NULL, html_next, NULL); |
| 6473 | |
| 6474 | /* |
| 6475 | grn_normalizer_register(ctx, "NormalizerUCA", -1, |
| 6476 | NULL, uca_next, NULL); |
| 6477 | */ |
| 6478 | |
| 6479 | return GRN_SUCCESS; |
| 6480 | } |