2 * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include <unicode/usearch.h>
18 #include <unicode/uchar.h>
19 #include <utils_i18n_uset.h>
20 #include <utils_i18n_private.h>
22 int i18n_uset_create_empty ( i18n_uset_h* set )
26 return I18N_ERROR_INVALID_PARAMETER;
28 *set = (i18n_uset_h)uset_openEmpty();
29 return I18N_ERROR_NONE;
32 int i18n_uset_create ( i18n_uchar32 start, i18n_uchar32 end, i18n_uset_h *set)
36 return I18N_ERROR_INVALID_PARAMETER;
38 *set = (i18n_uset_h)uset_open(start, end);
39 return I18N_ERROR_NONE;
42 int i18n_uset_create_pattern ( const i18n_uchar *pattern, int32_t pattern_length, i18n_uset_h *set )
44 UErrorCode icu_error = U_ZERO_ERROR;
45 i18n_error_code_e i18n_error;
47 if (pattern == NULL || pattern_length < -1 || set == NULL) {
48 return I18N_ERROR_INVALID_PARAMETER;
51 *set = (i18n_uset_h)uset_openPattern(pattern, pattern_length, &icu_error);
52 ERR("Error code: %d", icu_error);
53 ERR_MAPPING(icu_error, i18n_error);
57 int i18n_uset_create_pattern_options ( const i18n_uchar *pattern, int32_t pattern_length, uint32_t options, i18n_uset_h *set )
59 UErrorCode icu_error = U_ZERO_ERROR;
60 i18n_error_code_e i18n_error;
62 if (pattern == NULL || pattern_length < -1 || set == NULL) {
63 return I18N_ERROR_INVALID_PARAMETER;
65 if ((options != I18N_USET_IGNORE_SPACE) && (options != I18N_USET_CASE_INSENSITIVE)) {
66 return I18N_ERROR_INVALID_PARAMETER;
69 *set = (i18n_uset_h)uset_openPatternOptions(pattern, pattern_length, options, &icu_error);
70 ERR("Error code: %d", icu_error);
71 ERR_MAPPING(icu_error, i18n_error);
75 int i18n_uset_destroy ( i18n_uset_h set )
78 return I18N_ERROR_INVALID_PARAMETER;
80 uset_close((USet*)set);
81 return I18N_ERROR_NONE;
84 int i18n_uset_clone ( const i18n_uset_h set, i18n_uset_h *set_clone )
86 if (set == NULL || set_clone == NULL) {
87 return I18N_ERROR_INVALID_PARAMETER;
89 *set_clone = (i18n_uset_h)uset_clone((const USet*)set);
90 return I18N_ERROR_NONE;
93 i18n_ubool i18n_uset_is_frozen ( const i18n_uset_h set )
95 set_last_result(I18N_ERROR_NONE);
97 set_last_result(I18N_ERROR_INVALID_PARAMETER);
100 return uset_isFrozen((const USet*)set);
103 int i18n_uset_freeze ( i18n_uset_h set )
106 return I18N_ERROR_INVALID_PARAMETER;
108 uset_freeze((USet*)set);
109 return I18N_ERROR_NONE;
112 int i18n_uset_clone_as_thawed ( const i18n_uset_h set, i18n_uset_h *set_copy )
114 if (set == NULL || set_copy == NULL) {
115 return I18N_ERROR_INVALID_PARAMETER;
117 *set_copy = (i18n_uset_h)uset_cloneAsThawed((const USet*)set);
118 return I18N_ERROR_NONE;
121 int i18n_uset_set ( i18n_uset_h set, i18n_uchar32 start, i18n_uchar32 end )
124 return I18N_ERROR_INVALID_PARAMETER;
126 uset_set((USet*)set, start, end);
127 return I18N_ERROR_NONE;
130 int32_t i18n_uset_apply_pattern ( i18n_uset_h set, const i18n_uchar *pattern, int32_t pattern_length, uint32_t options )
133 set_last_result(I18N_ERROR_INVALID_PARAMETER);
136 if (pattern == NULL || pattern_length < -1) {
137 set_last_result(I18N_ERROR_INVALID_PARAMETER);
140 if (options != I18N_USET_IGNORE_SPACE && options != I18N_USET_CASE_INSENSITIVE) {
141 set_last_result(I18N_ERROR_INVALID_PARAMETER);
145 UErrorCode icu_error = U_ZERO_ERROR;
146 i18n_error_code_e i18n_error;
148 int32_t result_uset_applyPattern = uset_applyPattern ((USet*)set, pattern, pattern_length, options, &icu_error);
149 ERR("Error code: %d", icu_error);
150 ERR_MAPPING(icu_error, i18n_error);
152 set_last_result(i18n_error);
153 return result_uset_applyPattern;
156 int i18n_uset_apply_int_property_value ( i18n_uset_h set, i18n_uchar_uproperty_e prop, int32_t value )
159 return I18N_ERROR_INVALID_PARAMETER;
161 if ((prop < I18N_UCHAR_INT_START || prop > I18N_UCHAR_INT_LIMIT-1) &&
162 (prop < I18N_UCHAR_MASK_START || prop > I18N_UCHAR_MASK_LIMIT-1)) {
163 return I18N_ERROR_INVALID_PARAMETER;
165 if (prop != I18N_UCHAR_GENERAL_CATEGORY_MASK &&
166 (value <= I18N_UCHAR_INVALID_CODE ||
167 value >= I18N_UCHAR_OTHER_PROPERTY_LIMIT)) {
168 return I18N_ERROR_INVALID_PARAMETER;
171 UErrorCode icu_error = U_ZERO_ERROR;
172 i18n_error_code_e i18n_error;
174 uset_applyIntPropertyValue((USet *)set, prop, value, &icu_error);
175 ERR("Error code: %d", icu_error);
176 ERR_MAPPING(icu_error, i18n_error);
181 int i18n_uset_apply_property_alias ( i18n_uset_h set, const i18n_uchar *prop, int32_t prop_length, const i18n_uchar *value, int32_t value_length )
184 return I18N_ERROR_INVALID_PARAMETER;
186 if (prop == NULL || prop_length < -1) {
187 return I18N_ERROR_INVALID_PARAMETER;
189 if (value == NULL || value_length < -1) {
190 return I18N_ERROR_INVALID_PARAMETER;
193 UErrorCode icu_error = U_ZERO_ERROR;
194 i18n_error_code_e i18n_error;
196 uset_applyPropertyAlias ((USet*)set, prop, prop_length, value, value_length, &icu_error);
197 ERR("Error code: %d", icu_error);
198 ERR_MAPPING(icu_error, i18n_error);
204 i18n_ubool i18n_uset_resembles_pattern ( const i18n_uchar *pattern, int32_t pattern_length, int32_t pos )
206 if (pattern == NULL || pattern_length < -1 || pos < 0) {
207 set_last_result(I18N_ERROR_INVALID_PARAMETER);
210 set_last_result(I18N_ERROR_NONE);
211 return uset_resemblesPattern(pattern, pattern_length, pos);
214 int32_t i18n_uset_to_pattern ( const i18n_uset_h set, i18n_uchar *result, int32_t result_capacity, i18n_ubool escape_unprintable )
216 if (set == NULL || result_capacity < 0) {
217 set_last_result(I18N_ERROR_INVALID_PARAMETER);
221 UErrorCode icu_error = U_ZERO_ERROR;
222 i18n_error_code_e i18n_error;
224 int32_t result_uset_toPattern = uset_toPattern((const USet*)set, result, result_capacity, escape_unprintable, &icu_error);
225 ERR("Error code: %d", icu_error);
226 ERR_MAPPING(icu_error, i18n_error);
228 set_last_result(i18n_error);
229 return result_uset_toPattern;
232 int i18n_uset_add ( i18n_uset_h set, i18n_uchar32 character )
235 return I18N_ERROR_INVALID_PARAMETER;
237 uset_add ((USet*)set, character);
238 return I18N_ERROR_NONE;
241 int i18n_uset_add_all ( i18n_uset_h set, const i18n_uset_h additional_set )
243 if (set == NULL || additional_set == NULL) {
244 return I18N_ERROR_INVALID_PARAMETER;
246 uset_addAll((USet *)set, (const USet*)additional_set);
247 return I18N_ERROR_NONE;
250 int i18n_uset_add_range ( i18n_uset_h set, i18n_uchar32 start, i18n_uchar32 end )
253 return I18N_ERROR_INVALID_PARAMETER;
255 uset_addRange((USet*)set, start, end);
256 return I18N_ERROR_NONE;
259 int i18n_uset_add_string ( i18n_uset_h set, const i18n_uchar *str, int32_t str_len )
262 return I18N_ERROR_INVALID_PARAMETER;
264 if (str == NULL || str_len < -1) {
265 return I18N_ERROR_INVALID_PARAMETER;
267 uset_addString((USet*)set, str, str_len);
268 return I18N_ERROR_NONE;
271 int i18n_uset_add_all_code_points ( i18n_uset_h set, const i18n_uchar *str, int32_t str_len )
274 return I18N_ERROR_INVALID_PARAMETER;
276 if (str == NULL || str_len < -1) {
277 return I18N_ERROR_INVALID_PARAMETER;
279 uset_addAllCodePoints((USet*)set, str, str_len);
280 return I18N_ERROR_NONE;
283 int i18n_uset_remove ( i18n_uset_h set, i18n_uchar32 character )
286 return I18N_ERROR_INVALID_PARAMETER;
288 uset_remove ((USet*)set, character);
289 return I18N_ERROR_NONE;
292 int i18n_uset_remove_range ( i18n_uset_h set, i18n_uchar32 start, i18n_uchar32 end )
295 return I18N_ERROR_INVALID_PARAMETER;
297 uset_removeRange ((USet*)set, start, end);
298 return I18N_ERROR_NONE;
301 int i18n_uset_remove_string ( i18n_uset_h set, const i18n_uchar *str, int32_t str_len )
304 return I18N_ERROR_INVALID_PARAMETER;
306 if (str == NULL || str_len < -1) {
307 return I18N_ERROR_INVALID_PARAMETER;
309 uset_removeString((USet*)set, str, str_len);
310 return I18N_ERROR_NONE;
313 int i18n_uset_remove_all ( i18n_uset_h set, const i18n_uset_h remove_set )
315 if (set == NULL || remove_set == NULL) {
316 return I18N_ERROR_INVALID_PARAMETER;
318 uset_removeAll((USet*)set, (const USet*)remove_set);
319 return I18N_ERROR_NONE;
322 int i18n_uset_retain ( i18n_uset_h set, i18n_uchar32 start, i18n_uchar32 end )
325 return I18N_ERROR_INVALID_PARAMETER;
327 uset_retain((USet*)set, start, end);
328 return I18N_ERROR_NONE;
331 int i18n_uset_retain_all ( i18n_uset_h set, const i18n_uset_h retain )
333 if (set == NULL || retain == NULL) {
334 return I18N_ERROR_INVALID_PARAMETER;
336 uset_retainAll ((USet*)set, (const USet*)retain);
337 return I18N_ERROR_NONE;
340 int i18n_uset_compact ( i18n_uset_h set )
343 return I18N_ERROR_INVALID_PARAMETER;
345 uset_compact((USet*)set);
346 return I18N_ERROR_NONE;
349 int i18n_uset_complement ( i18n_uset_h set )
352 return I18N_ERROR_INVALID_PARAMETER;
354 uset_complement((USet*)set);
355 return I18N_ERROR_NONE;
358 int i18n_uset_complement_all ( i18n_uset_h set, const i18n_uset_h complement )
360 if (set == NULL || set == complement || complement == NULL) {
361 return I18N_ERROR_INVALID_PARAMETER;
363 uset_complementAll((USet*)set, (const USet*)complement);
364 return I18N_ERROR_NONE;
367 int i18n_uset_clear ( i18n_uset_h set )
370 return I18N_ERROR_INVALID_PARAMETER;
372 uset_clear((USet*)set);
373 return I18N_ERROR_NONE;
376 int i18n_uset_destroy_over ( i18n_uset_h set, int32_t attributes )
379 return I18N_ERROR_INVALID_PARAMETER;
381 uset_closeOver((USet*)set, attributes);
382 return I18N_ERROR_NONE;
385 int i18n_uset_remove_all_strings ( i18n_uset_h set )
388 return I18N_ERROR_INVALID_PARAMETER;
390 uset_removeAllStrings((USet*)set);
391 return I18N_ERROR_NONE;
394 i18n_ubool i18n_uset_is_empty ( const i18n_uset_h set )
397 set_last_result(I18N_ERROR_INVALID_PARAMETER);
400 set_last_result(I18N_ERROR_NONE);
401 return uset_isEmpty((const USet*)set);
404 i18n_ubool i18n_uset_contains ( const i18n_uset_h set, i18n_uchar32 character )
407 set_last_result(I18N_ERROR_INVALID_PARAMETER);
410 set_last_result(I18N_ERROR_NONE);
411 return uset_contains((const USet*)set, character);
414 i18n_ubool i18n_uset_contains_range ( const i18n_uset_h set, i18n_uchar32 start, i18n_uchar32 end )
417 set_last_result(I18N_ERROR_INVALID_PARAMETER);
420 set_last_result(I18N_ERROR_NONE);
421 return uset_containsRange((const USet*)set, start, end);
424 i18n_ubool i18n_uset_contains_string ( const i18n_uset_h set, const i18n_uchar *str, int32_t str_len )
427 set_last_result(I18N_ERROR_INVALID_PARAMETER);
430 if (str == NULL || str_len < -1) {
431 set_last_result(I18N_ERROR_INVALID_PARAMETER);
434 set_last_result(I18N_ERROR_NONE);
435 return uset_containsString((const USet*)set, str, str_len);
438 int32_t i18n_uset_index_of ( const i18n_uset_h set, i18n_uchar32 character )
441 set_last_result(I18N_ERROR_INVALID_PARAMETER);
444 set_last_result(I18N_ERROR_NONE);
445 return uset_indexOf((const USet*)set, character);
448 i18n_uchar32 i18n_uset_char_at ( const i18n_uset_h set, int32_t char_index )
450 if (set == NULL || (char_index < 0 || char_index > uset_size((const USet*)set)-1)) {
451 set_last_result(I18N_ERROR_INVALID_PARAMETER);
454 set_last_result(I18N_ERROR_NONE);
455 return uset_charAt((const USet*)set, char_index);
458 int32_t i18n_uset_size ( const i18n_uset_h set )
461 set_last_result(I18N_ERROR_INVALID_PARAMETER);
464 set_last_result(I18N_ERROR_NONE);
465 return uset_size((const USet*)set);
468 int32_t i18n_uset_get_item_count ( const i18n_uset_h set )
471 set_last_result(I18N_ERROR_INVALID_PARAMETER);
474 set_last_result(I18N_ERROR_NONE);
475 return uset_getItemCount((const USet*)set);
478 int32_t i18n_uset_get_item ( const i18n_uset_h set, int32_t item_index, i18n_uchar32 *start, i18n_uchar32 *end,
479 i18n_uchar *str, int32_t str_capacity )
481 if (set == NULL || (item_index < 0 || item_index > uset_getItemCount((const USet*)set)-1) || start == NULL || end == NULL) {
482 set_last_result(I18N_ERROR_INVALID_PARAMETER);
485 if ((str == NULL && str_capacity != 0) || (str != NULL && str_capacity < 0)) {
486 set_last_result(I18N_ERROR_INVALID_PARAMETER);
490 UErrorCode icu_error = U_ZERO_ERROR;
491 i18n_error_code_e i18n_error;
493 int32_t result_uset_getItem = uset_getItem((const USet*)set, item_index, start, end, str, str_capacity, &icu_error);
494 ERR("Error code: %d", icu_error);
495 ERR_MAPPING(icu_error, i18n_error);
497 set_last_result(i18n_error);
498 return result_uset_getItem;
501 i18n_ubool i18n_uset_contains_all ( const i18n_uset_h set1, const i18n_uset_h set2 )
503 if (set1 == NULL || set2 == NULL) {
504 set_last_result(I18N_ERROR_INVALID_PARAMETER);
507 set_last_result(I18N_ERROR_NONE);
508 return uset_containsAll((const USet*)set1, (const USet*)set2);
511 i18n_ubool i18n_uset_contains_all_code_points ( const i18n_uset_h set, const i18n_uchar *str, int32_t str_len )
514 set_last_result(I18N_ERROR_INVALID_PARAMETER);
517 if (str == NULL || str_len < -1) {
518 set_last_result(I18N_ERROR_INVALID_PARAMETER);
521 set_last_result(I18N_ERROR_NONE);
522 return uset_containsAllCodePoints((const USet*)set, str, str_len);
525 i18n_ubool i18n_uset_contains_none ( const i18n_uset_h set1, const i18n_uset_h set2 )
527 if (set1 == NULL || set2 == NULL) {
528 set_last_result(I18N_ERROR_INVALID_PARAMETER);
531 set_last_result(I18N_ERROR_NONE);
532 return uset_containsNone((const USet*)set1, (const USet*)set2);
535 i18n_ubool i18n_uset_contains_some ( const i18n_uset_h set1, const i18n_uset_h set2 )
537 if (set1 == NULL || set2 == NULL) {
538 set_last_result(I18N_ERROR_INVALID_PARAMETER);
541 set_last_result(I18N_ERROR_NONE);
542 return uset_containsSome((const USet*)set1, (const USet*)set2);
545 int32_t i18n_uset_span ( const i18n_uset_h set, const i18n_uchar *str, int32_t length, i18n_uset_span_condition_e span_condition )
548 set_last_result(I18N_ERROR_INVALID_PARAMETER);
551 if (str == NULL || length < -1) {
552 set_last_result(I18N_ERROR_INVALID_PARAMETER);
555 set_last_result(I18N_ERROR_NONE);
556 return uset_span((const USet*)set, str, length, (USetSpanCondition) span_condition);
559 int32_t i18n_uset_span_back ( const i18n_uset_h set, const i18n_uchar *str, int32_t length, i18n_uset_span_condition_e span_condition )
562 set_last_result(I18N_ERROR_INVALID_PARAMETER);
565 if (str == NULL || length < -1) {
566 set_last_result(I18N_ERROR_INVALID_PARAMETER);
569 if (span_condition < I18N_USET_SPAN_NOT_CONTAINED ||
570 span_condition > I18N_USET_SPAN_CONDITION_COUNT) {
571 set_last_result(I18N_ERROR_INVALID_PARAMETER);
574 set_last_result(I18N_ERROR_NONE);
575 return uset_spanBack((const USet*)set, str, length, (USetSpanCondition) span_condition);
578 int32_t i18n_uset_span_utf8 ( const i18n_uset_h set, const char *str, int32_t length, i18n_uset_span_condition_e span_condition )
581 set_last_result(I18N_ERROR_INVALID_PARAMETER);
584 if (str == NULL || length < -1) {
585 set_last_result(I18N_ERROR_INVALID_PARAMETER);
588 if (span_condition < I18N_USET_SPAN_NOT_CONTAINED ||
589 span_condition > I18N_USET_SPAN_CONDITION_COUNT) {
590 set_last_result(I18N_ERROR_INVALID_PARAMETER);
593 set_last_result(I18N_ERROR_NONE);
594 return uset_spanUTF8((const USet*)set, str, length, (USetSpanCondition) span_condition);
597 int32_t i18n_uset_span_back_utf8 ( const i18n_uset_h set, const char *str, int32_t length, i18n_uset_span_condition_e span_condition )
600 set_last_result(I18N_ERROR_INVALID_PARAMETER);
603 if (str == NULL || length < -1) {
604 set_last_result(I18N_ERROR_INVALID_PARAMETER);
607 if (span_condition < I18N_USET_SPAN_NOT_CONTAINED ||
608 span_condition > I18N_USET_SPAN_CONDITION_COUNT) {
609 set_last_result(I18N_ERROR_INVALID_PARAMETER);
612 set_last_result(I18N_ERROR_NONE);
613 return uset_spanBackUTF8((const USet*)set, str, length, (USetSpanCondition) span_condition);
616 i18n_ubool i18n_uset_equals ( const i18n_uset_h set1, const i18n_uset_h set2 )
618 if (set1 == NULL || set2 == NULL) {
619 set_last_result(I18N_ERROR_INVALID_PARAMETER);
622 set_last_result(I18N_ERROR_NONE);
623 return uset_equals((const USet*)set1, (const USet*)set2);
626 int32_t i18n_uset_serialize ( const i18n_uset_h set, uint16_t *dest, int32_t dest_capacity )
628 i18n_error_code_e i18n_error;
631 set_last_result(I18N_ERROR_INVALID_PARAMETER);
634 if (dest == NULL || dest_capacity < 0) {
635 set_last_result(I18N_ERROR_INVALID_PARAMETER);
639 UErrorCode icu_error = U_ZERO_ERROR;
640 int32_t result_uset_serialize = uset_serialize((const USet*)set, dest, dest_capacity, &icu_error);
641 ERR("Error code: %d", icu_error);
642 ERR_MAPPING(icu_error, i18n_error);
644 set_last_result(i18n_error);
645 return result_uset_serialize;
648 i18n_ubool i18n_uset_get_serialized_set ( const uint16_t *src, int32_t src_length, i18n_userialized_set_s *fill_set )
650 if (src == NULL || src_length < 0) {
651 set_last_result(I18N_ERROR_INVALID_PARAMETER);
654 set_last_result(I18N_ERROR_NONE);
655 return uset_getSerializedSet((USerializedSet*)fill_set, src, src_length);
658 int i18n_uset_set_serialized_to_one ( i18n_uchar32 character, i18n_userialized_set_s *fill_set )
660 if (fill_set == NULL) {
661 return I18N_ERROR_INVALID_PARAMETER;
663 uset_setSerializedToOne((USerializedSet*)fill_set, character);
664 return I18N_ERROR_NONE;
667 i18n_ubool i18n_uset_serialized_contains ( const i18n_userialized_set_s *set, i18n_uchar32 character )
670 set_last_result(I18N_ERROR_INVALID_PARAMETER);
673 set_last_result(I18N_ERROR_NONE);
674 return uset_serializedContains((const USerializedSet*)set, character);
677 int32_t i18n_uset_get_serialized_range_count ( const i18n_userialized_set_s *set )
680 set_last_result(I18N_ERROR_INVALID_PARAMETER);
683 set_last_result(I18N_ERROR_NONE);
684 return uset_getSerializedRangeCount((const USerializedSet*)set);
687 i18n_ubool i18n_uset_get_serialized_range ( const i18n_userialized_set_s *set, int32_t range_index, i18n_uchar32 *p_start, i18n_uchar32 *p_end )
690 set_last_result(I18N_ERROR_INVALID_PARAMETER);
693 if (range_index < 0 || range_index > uset_getSerializedRangeCount((const USerializedSet*)set)-1) {
694 set_last_result(I18N_ERROR_INVALID_PARAMETER);
697 set_last_result(I18N_ERROR_NONE);
698 return uset_getSerializedRange((const USerializedSet*)set, range_index, p_start, p_end);