| 1 | /* |
| 2 | Copyright (C) 2010-2016 Brazil |
| 3 | Copyright (C) 2018-2025 Sutou Kouhei <[email protected]> |
| 4 | |
| 5 | This library is free software; you can redistribute it and/or |
| 6 | modify it under the terms of the GNU Lesser General Public |
| 7 | License as published by the Free Software Foundation; either |
| 8 | version 2.1 of the License, or (at your option) any later version. |
| 9 | |
| 10 | This library is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | Lesser General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU Lesser General Public |
| 16 | License along with this library; if not, write to the Free Software |
| 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | */ |
| 19 | |
| 20 | #include "grn.h" |
| 21 | #include "grn_error.h" |
| 22 | #include "grn_nfkc.h" |
| 23 | #include <groonga/nfkc.h> |
| 24 | |
| 25 | #ifdef GRN_WITH_NFKC |
| 26 | |
| 27 | grn_char_type |
| 28 | grn_nfkc_char_type(const unsigned char *utf8) |
| 29 | { |
| 30 | return grn_nfkc50_char_type(utf8); |
| 31 | } |
| 32 | |
| 33 | grn_char_type |
| 34 | grn_nfkc_latest_char_type(const unsigned char *utf8) |
| 35 | { |
| 36 | /* This must be updated when we add the latest Unicode support. */ |
| 37 | return grn_nfkc170_char_type(utf8); |
| 38 | } |
| 39 | |
| 40 | const char * |
| 41 | grn_nfkc_decompose(const unsigned char *utf8) |
| 42 | { |
| 43 | return grn_nfkc50_decompose(utf8); |
| 44 | } |
| 45 | |
| 46 | const char * |
| 47 | grn_nfkc_compose(const unsigned char *prefix_utf8, |
| 48 | const unsigned char *suffix_utf8) |
| 49 | { |
| 50 | return grn_nfkc50_compose(prefix_utf8, suffix_utf8); |
| 51 | } |
| 52 | |
| 53 | void |
| 54 | grn_nfkc_normalize_options_init(grn_ctx *ctx, |
| 55 | grn_nfkc_normalize_options *options, |
| 56 | grn_nfkc_char_type_func char_type_func, |
| 57 | grn_nfkc_decompose_func decompose_func, |
| 58 | grn_nfkc_compose_func compose_func) |
| 59 | { |
| 60 | options->char_type_func = char_type_func; |
| 61 | options->decompose_func = decompose_func; |
| 62 | options->compose_func = compose_func; |
| 63 | options->include_removed_source_location = true; |
| 64 | options->report_source_offset = false; |
| 65 | options->unify_latin_alphabet_with = false; |
| 66 | options->unify_kana = false; |
| 67 | options->unify_kana_case = false; |
| 68 | options->unify_kana_voiced_sound_mark = false; |
| 69 | options->unify_hyphen = false; |
| 70 | options->unify_prolonged_sound_mark = false; |
| 71 | options->unify_hyphen_and_prolonged_sound_mark = false; |
| 72 | options->unify_middle_dot = false; |
| 73 | options->unify_katakana_v_sounds = false; |
| 74 | options->unify_katakana_bu_sound = false; |
| 75 | options->unify_katakana_du_small_sounds = false; |
| 76 | options->unify_katakana_du_sound = false; |
| 77 | options->unify_katakana_zu_small_sounds = false; |
| 78 | options->unify_katakana_wo_sound = false; |
| 79 | options->unify_katakana_di_sound = false; |
| 80 | options->unify_katakana_gu_small_sounds = false; |
| 81 | options->unify_kana_hyphen = false; |
| 82 | options->unify_kana_prolonged_sound_mark = false; |
| 83 | options->unify_katakana_trailing_o = false; |
| 84 | options->unify_iteration_mark = false; |
| 85 | options->unify_to_romaji = false; |
| 86 | options->unify_to_katakana = false; |
| 87 | options->remove_blank = false; |
| 88 | options->remove_blank_force = false; |
| 89 | options->remove_blank_force_is_set = false; |
| 90 | options->remove_new_line = true; |
| 91 | options->remove_symbol = false; |
| 92 | options->strip = false; |
| 93 | } |
| 94 | |
| 95 | void |
| 96 | grn_nfkc100_normalize_options_init(grn_ctx *ctx, |
| 97 | grn_nfkc_normalize_options *options) |
| 98 | { |
| 99 | grn_nfkc_normalize_options_init(ctx, |
| 100 | options, |
| 101 | grn_nfkc100_char_type, |
| 102 | grn_nfkc100_decompose, |
| 103 | grn_nfkc100_compose); |
| 104 | } |
| 105 | |
| 106 | void |
| 107 | grn_nfkc121_normalize_options_init(grn_ctx *ctx, |
| 108 | grn_nfkc_normalize_options *options) |
| 109 | { |
| 110 | grn_nfkc_normalize_options_init(ctx, |
| 111 | options, |
| 112 | grn_nfkc121_char_type, |
| 113 | grn_nfkc121_decompose, |
| 114 | grn_nfkc121_compose); |
| 115 | } |
| 116 | |
| 117 | void |
| 118 | grn_nfkc130_normalize_options_init(grn_ctx *ctx, |
| 119 | grn_nfkc_normalize_options *options) |
| 120 | { |
| 121 | grn_nfkc_normalize_options_init(ctx, |
| 122 | options, |
| 123 | grn_nfkc130_char_type, |
| 124 | grn_nfkc130_decompose, |
| 125 | grn_nfkc130_compose); |
| 126 | } |
| 127 | |
| 128 | void |
| 129 | grn_nfkc150_normalize_options_init(grn_ctx *ctx, |
| 130 | grn_nfkc_normalize_options *options) |
| 131 | { |
| 132 | grn_nfkc_normalize_options_init(ctx, |
| 133 | options, |
| 134 | grn_nfkc150_char_type, |
| 135 | grn_nfkc150_decompose, |
| 136 | grn_nfkc150_compose); |
| 137 | } |
| 138 | |
| 139 | void |
| 140 | grn_nfkc160_normalize_options_init(grn_ctx *ctx, |
| 141 | grn_nfkc_normalize_options *options) |
| 142 | { |
| 143 | grn_nfkc_normalize_options_init(ctx, |
| 144 | options, |
| 145 | grn_nfkc160_char_type, |
| 146 | grn_nfkc160_decompose, |
| 147 | grn_nfkc160_compose); |
| 148 | } |
| 149 | |
| 150 | void |
| 151 | grn_nfkc170_normalize_options_init(grn_ctx *ctx, |
| 152 | grn_nfkc_normalize_options *options) |
| 153 | { |
| 154 | grn_nfkc_normalize_options_init(ctx, |
| 155 | options, |
| 156 | grn_nfkc170_char_type, |
| 157 | grn_nfkc170_decompose, |
| 158 | grn_nfkc170_compose); |
| 159 | } |
| 160 | |
| 161 | static const grn_nfkc_funcs grn_nfkc_funcs_null = { |
| 162 | NULL, |
| 163 | NULL, |
| 164 | NULL, |
| 165 | }; |
| 166 | |
| 167 | static const grn_nfkc_funcs grn_nfkc50_funcs = { |
| 168 | grn_nfkc50_char_type, |
| 169 | grn_nfkc50_decompose, |
| 170 | grn_nfkc50_compose, |
| 171 | }; |
| 172 | |
| 173 | static const grn_nfkc_funcs grn_nfkc100_funcs = { |
| 174 | grn_nfkc100_char_type, |
| 175 | grn_nfkc100_decompose, |
| 176 | grn_nfkc100_compose, |
| 177 | }; |
| 178 | |
| 179 | static const grn_nfkc_funcs grn_nfkc121_funcs = { |
| 180 | grn_nfkc121_char_type, |
| 181 | grn_nfkc121_decompose, |
| 182 | grn_nfkc121_compose, |
| 183 | }; |
| 184 | |
| 185 | static const grn_nfkc_funcs grn_nfkc130_funcs = { |
| 186 | grn_nfkc130_char_type, |
| 187 | grn_nfkc130_decompose, |
| 188 | grn_nfkc130_compose, |
| 189 | }; |
| 190 | |
| 191 | static const grn_nfkc_funcs grn_nfkc150_funcs = { |
| 192 | grn_nfkc150_char_type, |
| 193 | grn_nfkc150_decompose, |
| 194 | grn_nfkc150_compose, |
| 195 | }; |
| 196 | |
| 197 | static const grn_nfkc_funcs grn_nfkc160_funcs = { |
| 198 | grn_nfkc160_char_type, |
| 199 | grn_nfkc160_decompose, |
| 200 | grn_nfkc160_compose, |
| 201 | }; |
| 202 | |
| 203 | static const grn_nfkc_funcs grn_nfkc170_funcs = { |
| 204 | grn_nfkc170_char_type, |
| 205 | grn_nfkc170_decompose, |
| 206 | grn_nfkc170_compose, |
| 207 | }; |
| 208 | |
| 209 | grn_nfkc_funcs |
| 210 | grn_nfkc_version_option_process(grn_ctx *ctx, |
| 211 | grn_obj *raw_options, |
| 212 | unsigned int i, |
| 213 | grn_raw_string *name_raw, |
| 214 | const char *tag) |
| 215 | { |
| 216 | const char *version; |
| 217 | grn_id domain; |
| 218 | unsigned int version_length = |
| 219 | grn_vector_get_element(ctx, raw_options, i, &version, NULL, &domain); |
| 220 | if (!grn_type_id_is_text_family(ctx, domain)) { |
| 221 | grn_obj value; |
| 222 | GRN_VALUE_FIX_SIZE_INIT(&value, GRN_OBJ_DO_SHALLOW_COPY, domain); |
| 223 | GRN_TEXT_SET(ctx, &value, version, version_length); |
| 224 | grn_obj inspected; |
| 225 | GRN_TEXT_INIT(&inspected, 0); |
| 226 | grn_inspect(ctx, &inspected, &value); |
| 227 | ERR(GRN_INVALID_ARGUMENT, |
| 228 | "%s[%.*s] must be a text: <%.*s>", |
| 229 | tag, |
| 230 | (int)(name_raw->length), |
| 231 | name_raw->value, |
| 232 | (int)GRN_TEXT_LEN(&inspected), |
| 233 | GRN_TEXT_VALUE(&inspected)); |
| 234 | GRN_OBJ_FIN(ctx, &inspected); |
| 235 | GRN_OBJ_FIN(ctx, &value); |
| 236 | return grn_nfkc_funcs_null; |
| 237 | } |
| 238 | if (version_length == 0) { |
| 239 | ERR(GRN_INVALID_ARGUMENT, |
| 240 | "%s[%.*s] must not be empty", |
| 241 | tag, |
| 242 | (int)(name_raw->length), |
| 243 | name_raw->value); |
| 244 | return grn_nfkc_funcs_null; |
| 245 | } |
| 246 | grn_raw_string version_raw; |
| 247 | version_raw.value = version; |
| 248 | version_raw.length = version_length; |
| 249 | if (GRN_RAW_STRING_EQUAL_CSTRING(version_raw, "5.0.0")) { |
| 250 | return grn_nfkc50_funcs; |
| 251 | } else if (GRN_RAW_STRING_EQUAL_CSTRING(version_raw, "10.0.0")) { |
| 252 | return grn_nfkc100_funcs; |
| 253 | } else if (GRN_RAW_STRING_EQUAL_CSTRING(version_raw, "12.1.0")) { |
| 254 | return grn_nfkc121_funcs; |
| 255 | } else if (GRN_RAW_STRING_EQUAL_CSTRING(version_raw, "13.0.0")) { |
| 256 | return grn_nfkc130_funcs; |
| 257 | } else if (GRN_RAW_STRING_EQUAL_CSTRING(version_raw, "15.0.0")) { |
| 258 | return grn_nfkc150_funcs; |
| 259 | } else if (GRN_RAW_STRING_EQUAL_CSTRING(version_raw, "16.0.0")) { |
| 260 | return grn_nfkc160_funcs; |
| 261 | } else if (GRN_RAW_STRING_EQUAL_CSTRING(version_raw, "17.0.0")) { |
| 262 | return grn_nfkc170_funcs; |
| 263 | } else { |
| 264 | ERR(GRN_INVALID_ARGUMENT, |
| 265 | "%s[%.*s] must be one of \"5.0.0\", \"10.0.0\", \"12.1.0\", " |
| 266 | "\"13.0.0\", \"15.0.0\", \"16.0.0\" or \"17.0.0\": <%.*s>", |
| 267 | tag, |
| 268 | (int)(name_raw->length), |
| 269 | name_raw->value, |
| 270 | (int)(version_raw.length), |
| 271 | version_raw.value); |
| 272 | return grn_nfkc_funcs_null; |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | grn_rc |
| 277 | grn_nfkc_normalize_options_apply(grn_ctx *ctx, |
| 278 | grn_nfkc_normalize_options *options, |
| 279 | grn_obj *raw_options, |
| 280 | const char *tag) |
| 281 | { |
| 282 | GRN_OPTION_VALUES_EACH_BEGIN(ctx, raw_options, i, name, name_length) |
| 283 | { |
| 284 | grn_raw_string name_raw; |
| 285 | name_raw.value = name; |
| 286 | name_raw.length = name_length; |
| 287 | |
| 288 | if (GRN_RAW_STRING_EQUAL_CSTRING(name_raw, |
| 289 | "include_removed_source_location")) { |
| 290 | options->include_removed_source_location = |
| 291 | grn_vector_get_element_bool(ctx, |
| 292 | raw_options, |
| 293 | i, |
| 294 | options->include_removed_source_location); |
| 295 | } else if (GRN_RAW_STRING_EQUAL_CSTRING(name_raw, "report_source_offset")) { |
| 296 | options->report_source_offset = |
| 297 | grn_vector_get_element_bool(ctx, |
| 298 | raw_options, |
| 299 | i, |
| 300 | options->report_source_offset); |
| 301 | } else if (GRN_RAW_STRING_EQUAL_CSTRING(name_raw, |
| 302 | "unify_latin_alphabet_with")) { |
| 303 | options->unify_latin_alphabet_with = |
| 304 | grn_vector_get_element_bool(ctx, |
| 305 | raw_options, |
| 306 | i, |
| 307 | options->unify_latin_alphabet_with); |
| 308 | } else if (GRN_RAW_STRING_EQUAL_CSTRING(name_raw, "unify_kana")) { |
| 309 | options->unify_kana = |
| 310 | grn_vector_get_element_bool(ctx, raw_options, i, options->unify_kana); |
| 311 | } else if (GRN_RAW_STRING_EQUAL_CSTRING(name_raw, "unify_kana_case")) { |
| 312 | options->unify_kana_case = |
| 313 | grn_vector_get_element_bool(ctx, |
| 314 | raw_options, |
| 315 | i, |
| 316 | options->unify_kana_case); |
| 317 | } else if (GRN_RAW_STRING_EQUAL_CSTRING(name_raw, |
| 318 | "unify_kana_voiced_sound_mark")) { |
| 319 | options->unify_kana_voiced_sound_mark = |
| 320 | grn_vector_get_element_bool(ctx, |
| 321 | raw_options, |
| 322 | i, |
| 323 | options->unify_kana_voiced_sound_mark); |
| 324 | } else if (GRN_RAW_STRING_EQUAL_CSTRING(name_raw, "unify_hyphen")) { |
| 325 | options->unify_hyphen = |
| 326 | grn_vector_get_element_bool(ctx, raw_options, i, options->unify_hyphen); |
| 327 | } else if (GRN_RAW_STRING_EQUAL_CSTRING(name_raw, |
| 328 | "unify_prolonged_sound_mark")) { |
| 329 | options->unify_prolonged_sound_mark = |
| 330 | grn_vector_get_element_bool(ctx, |
| 331 | raw_options, |
| 332 | i, |
| 333 | options->unify_prolonged_sound_mark); |
| 334 | } else if (GRN_RAW_STRING_EQUAL_CSTRING( |
| 335 | name_raw, |
| 336 | "unify_hyphen_and_prolonged_sound_mark")) { |
| 337 | options->unify_hyphen_and_prolonged_sound_mark = |
| 338 | grn_vector_get_element_bool( |
| 339 | ctx, |
| 340 | raw_options, |
| 341 | i, |
| 342 | options->unify_hyphen_and_prolonged_sound_mark); |
| 343 | } else if (GRN_RAW_STRING_EQUAL_CSTRING(name_raw, "unify_middle_dot")) { |
| 344 | options->unify_middle_dot = |
| 345 | grn_vector_get_element_bool(ctx, |
| 346 | raw_options, |
| 347 | i, |
| 348 | options->unify_middle_dot); |
| 349 | } else if (GRN_RAW_STRING_EQUAL_CSTRING(name_raw, |
| 350 | "unify_katakana_v_sounds")) { |
| 351 | options->unify_katakana_v_sounds = |
| 352 | grn_vector_get_element_bool(ctx, |
| 353 | raw_options, |
| 354 | i, |
| 355 | options->unify_katakana_v_sounds); |
| 356 | } else if (GRN_RAW_STRING_EQUAL_CSTRING(name_raw, |
| 357 | "unify_katakana_bu_sound")) { |
| 358 | options->unify_katakana_bu_sound = |
| 359 | grn_vector_get_element_bool(ctx, |
| 360 | raw_options, |
| 361 | i, |
| 362 | options->unify_katakana_bu_sound); |
| 363 | } else if (GRN_RAW_STRING_EQUAL_CSTRING(name_raw, |
| 364 | "unify_katakana_du_small_sounds")) { |
| 365 | options->unify_katakana_du_small_sounds = |
| 366 | grn_vector_get_element_bool(ctx, |
| 367 | raw_options, |
| 368 | i, |
| 369 | options->unify_katakana_du_small_sounds); |
| 370 | } else if (GRN_RAW_STRING_EQUAL_CSTRING(name_raw, |
| 371 | "unify_katakana_du_sound")) { |
| 372 | options->unify_katakana_du_sound = |
| 373 | grn_vector_get_element_bool(ctx, |
| 374 | raw_options, |
| 375 | i, |
| 376 | options->unify_katakana_du_sound); |
| 377 | } else if (GRN_RAW_STRING_EQUAL_CSTRING(name_raw, |
| 378 | "unify_katakana_zu_small_sounds")) { |
| 379 | options->unify_katakana_zu_small_sounds = |
| 380 | grn_vector_get_element_bool(ctx, |
| 381 | raw_options, |
| 382 | i, |
| 383 | options->unify_katakana_zu_small_sounds); |
| 384 | } else if (GRN_RAW_STRING_EQUAL_CSTRING(name_raw, |
| 385 | "unify_katakana_wo_sound")) { |
| 386 | options->unify_katakana_wo_sound = |
| 387 | grn_vector_get_element_bool(ctx, |
| 388 | raw_options, |
| 389 | i, |
| 390 | options->unify_katakana_wo_sound); |
| 391 | } else if (GRN_RAW_STRING_EQUAL_CSTRING(name_raw, |
| 392 | "unify_katakana_di_sound")) { |
| 393 | options->unify_katakana_di_sound = |
| 394 | grn_vector_get_element_bool(ctx, |
| 395 | raw_options, |
| 396 | i, |
| 397 | options->unify_katakana_di_sound); |
| 398 | } else if (GRN_RAW_STRING_EQUAL_CSTRING(name_raw, |
| 399 | "unify_katakana_gu_small_sounds")) { |
| 400 | options->unify_katakana_gu_small_sounds = |
| 401 | grn_vector_get_element_bool(ctx, |
| 402 | raw_options, |
| 403 | i, |
| 404 | options->unify_katakana_gu_small_sounds); |
| 405 | } else if (GRN_RAW_STRING_EQUAL_CSTRING(name_raw, "unify_kana_hyphen")) { |
| 406 | options->unify_kana_hyphen = |
| 407 | grn_vector_get_element_bool(ctx, |
| 408 | raw_options, |
| 409 | i, |
| 410 | options->unify_kana_hyphen); |
| 411 | } else if (GRN_RAW_STRING_EQUAL_CSTRING( |
| 412 | name_raw, |
| 413 | "unify_kana_prolonged_sound_mark")) { |
| 414 | options->unify_kana_prolonged_sound_mark = |
| 415 | grn_vector_get_element_bool(ctx, |
| 416 | raw_options, |
| 417 | i, |
| 418 | options->unify_kana_prolonged_sound_mark); |
| 419 | } else if (GRN_RAW_STRING_EQUAL_CSTRING(name_raw, |
| 420 | "unify_katakana_trailing_o")) { |
| 421 | options->unify_katakana_trailing_o = |
| 422 | grn_vector_get_element_bool(ctx, |
| 423 | raw_options, |
| 424 | i, |
| 425 | options->unify_katakana_trailing_o); |
| 426 | } else if (GRN_RAW_STRING_EQUAL_CSTRING(name_raw, "unify_iteration_mark")) { |
| 427 | options->unify_iteration_mark = |
| 428 | grn_vector_get_element_bool(ctx, |
| 429 | raw_options, |
| 430 | i, |
| 431 | options->unify_iteration_mark); |
| 432 | } else if (GRN_RAW_STRING_EQUAL_CSTRING(name_raw, "unify_to_romaji")) { |
| 433 | options->unify_to_romaji = |
| 434 | grn_vector_get_element_bool(ctx, |
| 435 | raw_options, |
| 436 | i, |
| 437 | options->unify_to_romaji); |
| 438 | } else if (GRN_RAW_STRING_EQUAL_CSTRING(name_raw, "unify_to_katakana")) { |
| 439 | options->unify_to_katakana = |
| 440 | grn_vector_get_element_bool(ctx, |
| 441 | raw_options, |
| 442 | i, |
| 443 | options->unify_to_katakana); |
| 444 | } else if (GRN_RAW_STRING_EQUAL_CSTRING(name_raw, "remove_blank")) { |
| 445 | options->remove_blank = |
| 446 | grn_vector_get_element_bool(ctx, raw_options, i, options->remove_blank); |
| 447 | } else if (GRN_RAW_STRING_EQUAL_CSTRING(name_raw, "remove_blank_force")) { |
| 448 | options->remove_blank_force = |
| 449 | grn_vector_get_element_bool(ctx, |
| 450 | raw_options, |
| 451 | i, |
| 452 | options->remove_blank_force); |
| 453 | options->remove_blank_force_is_set = true; |
| 454 | } else if (GRN_RAW_STRING_EQUAL_CSTRING(name_raw, "remove_new_line")) { |
| 455 | options->remove_new_line = |
| 456 | grn_vector_get_element_bool(ctx, |
| 457 | raw_options, |
| 458 | i, |
| 459 | options->remove_new_line); |
| 460 | } else if (GRN_RAW_STRING_EQUAL_CSTRING(name_raw, "remove_symbol")) { |
| 461 | options->remove_symbol = |
| 462 | grn_vector_get_element_bool(ctx, |
| 463 | raw_options, |
| 464 | i, |
| 465 | options->remove_symbol); |
| 466 | } else if (GRN_RAW_STRING_EQUAL_CSTRING(name_raw, "strip")) { |
| 467 | options->strip = |
| 468 | grn_vector_get_element_bool(ctx, raw_options, i, options->strip); |
| 469 | } else if (GRN_RAW_STRING_EQUAL_CSTRING(name_raw, "version")) { |
| 470 | grn_nfkc_funcs funcs = |
| 471 | grn_nfkc_version_option_process(ctx, raw_options, i, &name_raw, tag); |
| 472 | if (ctx->rc != GRN_SUCCESS) { |
| 473 | break; |
| 474 | } |
| 475 | options->char_type_func = funcs.char_type_func; |
| 476 | options->decompose_func = funcs.decompose_func; |
| 477 | options->compose_func = funcs.compose_func; |
| 478 | } |
| 479 | } |
| 480 | GRN_OPTION_VALUES_EACH_END(); |
| 481 | |
| 482 | return ctx->rc; |
| 483 | } |
| 484 | |
| 485 | void |
| 486 | grn_nfkc_normalize_options_fin(grn_ctx *ctx, |
| 487 | grn_nfkc_normalize_options *options) |
| 488 | { |
| 489 | } |
| 490 | |
| 491 | #endif /* GRN_WITH_NFKC */ |