Update contrib.
2 *******************************************************************************
4 * Copyright (C) 2002-2005, International Business Machines
5 * Corporation and others. All Rights Reserved.
7 *******************************************************************************
10 * tab size: 8 (not used)
13 * created on: 2002mar07
14 * created by: Markus W. Scherer
16 * C version of UnicodeSet.
22 * \brief C API: Unicode Set
24 * <p>This is a C wrapper around the C++ UnicodeSet class.</p>
30 #include "unicode/utypes.h"
31 #include "unicode/uchar.h"
36 * A UnicodeSet. Use the uset_* API to manipulate. Create with
37 * uset_open*, and destroy with uset_close.
40 typedef struct USet USet;
44 * Bitmask values to be passed to uset_openPatternOptions() or
45 * uset_applyPattern() taking an option parameter.
50 * Ignore white space within patterns unless quoted or escaped.
53 USET_IGNORE_SPACE = 1,
56 * Enable case insensitive matching. E.g., "[ab]" with this flag
57 * will match 'a', 'A', 'b', and 'B'. "[^ab]" with this flag will
58 * match all except 'a', 'A', 'b', and 'B'. This performs a full
59 * closure over case mappings, e.g. U+017F for s.
61 * The resulting set is a superset of the input for the code points but
62 * not for the strings.
63 * It performs a case mapping closure of the code points and adds
64 * full case folding strings for the code points, and reduces strings of
65 * the original set to their full case folding equivalents.
67 * This is designed for case-insensitive matches, for example
68 * in regular expressions. The full code point case closure allows checking of
69 * an input character directly against the closure set.
70 * Strings are matched by comparing the case-folded form from the closure
71 * set with an incremental case folding of the string in question.
73 * The closure set will also contain single code points if the original
74 * set contained case-equivalent strings (like U+00DF for "ss" or "Ss" etc.).
75 * This is not necessary (that is, redundant) for the above matching method
76 * but results in the same closure sets regardless of whether the original
77 * set contained the code point or a string.
81 USET_CASE_INSENSITIVE = 2,
84 * Bitmask for UnicodeSet::closeOver() indicating letter case.
85 * This may be ORed together with other selectors.
91 * Enable case insensitive matching. E.g., "[ab]" with this flag
92 * will match 'a', 'A', 'b', and 'B'. "[^ab]" with this flag will
93 * match all except 'a', 'A', 'b', and 'B'. This adds the lower-,
94 * title-, and uppercase mappings as well as the case folding
95 * of each existing element in the set.
98 USET_ADD_CASE_MAPPINGS = 4,
101 * Enough for any single-code point set
104 USET_SERIALIZED_STATIC_ARRAY_CAPACITY=8
108 * A serialized form of a Unicode set. Limited manipulations are
109 * possible directly on a serialized set. See below.
112 typedef struct USerializedSet {
114 * The serialized Unicode Set.
117 const uint16_t *array;
119 * The length of the array that contains BMP characters.
124 * The total length of the array.
129 * A small buffer for the array to reduce memory allocations.
132 uint16_t staticArray[USET_SERIALIZED_STATIC_ARRAY_CAPACITY];
135 /*********************************************************************
137 *********************************************************************/
140 * Creates a USet object that contains the range of characters
141 * start..end, inclusive.
142 * @param start first character of the range, inclusive
143 * @param end last character of the range, inclusive
144 * @return a newly created USet. The caller must call uset_close() on
148 U_STABLE USet* U_EXPORT2
149 uset_open(UChar32 start, UChar32 end);
152 * Creates a set from the given pattern. See the UnicodeSet class
153 * description for the syntax of the pattern language.
154 * @param pattern a string specifying what characters are in the set
155 * @param patternLength the length of the pattern, or -1 if null
157 * @param ec the error code
160 U_STABLE USet* U_EXPORT2
161 uset_openPattern(const UChar* pattern, int32_t patternLength,
165 * Creates a set from the given pattern. See the UnicodeSet class
166 * description for the syntax of the pattern language.
167 * @param pattern a string specifying what characters are in the set
168 * @param patternLength the length of the pattern, or -1 if null
170 * @param options bitmask for options to apply to the pattern.
171 * Valid options are USET_IGNORE_SPACE and USET_CASE_INSENSITIVE.
172 * @param ec the error code
175 U_STABLE USet* U_EXPORT2
176 uset_openPatternOptions(const UChar* pattern, int32_t patternLength,
181 * Disposes of the storage used by a USet object. This function should
182 * be called exactly once for objects returned by uset_open().
183 * @param set the object to dispose of
186 U_STABLE void U_EXPORT2
187 uset_close(USet* set);
190 * Causes the USet object to represent the range <code>start - end</code>.
191 * If <code>start > end</code> then this USet is set to an empty range.
192 * @param set the object to set to the given range
193 * @param start first character in the set, inclusive
194 * @param end last character in the set, inclusive
197 U_DRAFT void U_EXPORT2
199 UChar32 start, UChar32 end);
202 * Modifies the set to represent the set specified by the given
203 * pattern. See the UnicodeSet class description for the syntax of
204 * the pattern language. See also the User Guide chapter about UnicodeSet.
205 * <em>Empties the set passed before applying the pattern.</em>
206 * @param set The set to which the pattern is to be applied.
207 * @param pattern A pointer to UChar string specifying what characters are in the set.
208 * The character at pattern[0] must be a '['.
209 * @param patternLength The length of the UChar string. -1 if NUL terminated.
210 * @param options A bitmask for options to apply to the pattern.
211 * Valid options are USET_IGNORE_SPACE and USET_CASE_INSENSITIVE.
212 * @param status Returns an error if the pattern cannot be parsed.
213 * @return Upon successful parse, the value is either
214 * the index of the character after the closing ']'
215 * of the parsed pattern.
216 * If the status code indicates failure, then the return value
217 * is the index of the error in the source.
221 U_STABLE int32_t U_EXPORT2
222 uset_applyPattern(USet *set,
223 const UChar *pattern, int32_t patternLength,
228 * Modifies the set to contain those code points which have the given value
229 * for the given binary or enumerated property, as returned by
230 * u_getIntPropertyValue. Prior contents of this set are lost.
232 * @param set the object to contain the code points defined by the property
234 * @param prop a property in the range UCHAR_BIN_START..UCHAR_BIN_LIMIT-1
235 * or UCHAR_INT_START..UCHAR_INT_LIMIT-1
236 * or UCHAR_MASK_START..UCHAR_MASK_LIMIT-1.
238 * @param value a value in the range u_getIntPropertyMinValue(prop)..
239 * u_getIntPropertyMaxValue(prop), with one exception. If prop is
240 * UCHAR_GENERAL_CATEGORY_MASK, then value should not be a UCharCategory, but
241 * rather a mask value produced by U_GET_GC_MASK(). This allows grouped
242 * categories such as [:L:] to be represented.
244 * @param ec error code input/output parameter
248 U_DRAFT void U_EXPORT2
249 uset_applyIntPropertyValue(USet* set,
250 UProperty prop, int32_t value, UErrorCode* ec);
253 * Modifies the set to contain those code points which have the
254 * given value for the given property. Prior contents of this
257 * @param set the object to contain the code points defined by the given
258 * property and value alias
260 * @param prop a string specifying a property alias, either short or long.
261 * The name is matched loosely. See PropertyAliases.txt for names and a
262 * description of loose matching. If the value string is empty, then this
263 * string is interpreted as either a General_Category value alias, a Script
264 * value alias, a binary property alias, or a special ID. Special IDs are
265 * matched loosely and correspond to the following sets:
267 * "ANY" = [\\u0000-\\U0010FFFF],
268 * "ASCII" = [\\u0000-\\u007F],
269 * "Assigned" = [:^Cn:].
271 * @param propLength the length of the prop, or -1 if NULL
273 * @param value a string specifying a value alias, either short or long.
274 * The name is matched loosely. See PropertyValueAliases.txt for names
275 * and a description of loose matching. In addition to aliases listed,
276 * numeric values and canonical combining classes may be expressed
277 * numerically, e.g., ("nv", "0.5") or ("ccc", "220"). The value string
280 * @param valueLength the length of the value, or -1 if NULL
282 * @param ec error code input/output parameter
286 U_DRAFT void U_EXPORT2
287 uset_applyPropertyAlias(USet* set,
288 const UChar *prop, int32_t propLength,
289 const UChar *value, int32_t valueLength,
293 * Return true if the given position, in the given pattern, appears
294 * to be the start of a UnicodeSet pattern.
296 * @param pattern a string specifying the pattern
297 * @param patternLength the length of the pattern, or -1 if NULL
298 * @param pos the given position
301 U_DRAFT UBool U_EXPORT2
302 uset_resemblesPattern(const UChar *pattern, int32_t patternLength,
306 * Returns a string representation of this set. If the result of
307 * calling this function is passed to a uset_openPattern(), it
308 * will produce another set that is equal to this one.
310 * @param result the string to receive the rules, may be NULL
311 * @param resultCapacity the capacity of result, may be 0 if result is NULL
312 * @param escapeUnprintable if TRUE then convert unprintable
313 * character to their hex escape representations, \\uxxxx or
314 * \\Uxxxxxxxx. Unprintable characters are those other than
315 * U+000A, U+0020..U+007E.
316 * @param ec error code.
317 * @return length of string, possibly larger than resultCapacity
320 U_STABLE int32_t U_EXPORT2
321 uset_toPattern(const USet* set,
322 UChar* result, int32_t resultCapacity,
323 UBool escapeUnprintable,
327 * Adds the given character to the given USet. After this call,
328 * uset_contains(set, c) will return TRUE.
329 * @param set the object to which to add the character
330 * @param c the character to add
333 U_STABLE void U_EXPORT2
334 uset_add(USet* set, UChar32 c);
337 * Adds all of the elements in the specified set to this set if
338 * they're not already present. This operation effectively
339 * modifies this set so that its value is the <i>union</i> of the two
340 * sets. The behavior of this operation is unspecified if the specified
341 * collection is modified while the operation is in progress.
343 * @param set the object to which to add the set
344 * @param additionalSet the source set whose elements are to be added to this set.
347 U_STABLE void U_EXPORT2
348 uset_addAll(USet* set, const USet *additionalSet);
351 * Adds the given range of characters to the given USet. After this call,
352 * uset_contains(set, start, end) will return TRUE.
353 * @param set the object to which to add the character
354 * @param start the first character of the range to add, inclusive
355 * @param end the last character of the range to add, inclusive
358 U_STABLE void U_EXPORT2
359 uset_addRange(USet* set, UChar32 start, UChar32 end);
362 * Adds the given string to the given USet. After this call,
363 * uset_containsString(set, str, strLen) will return TRUE.
364 * @param set the object to which to add the character
365 * @param str the string to add
366 * @param strLen the length of the string or -1 if null terminated.
369 U_STABLE void U_EXPORT2
370 uset_addString(USet* set, const UChar* str, int32_t strLen);
373 * Adds each of the characters in this string to the set. Thus "ch" => {"c", "h"}
374 * If this set already any particular character, it has no effect on that character.
375 * @param set the object to which to add the character
376 * @param str the source string
377 * @param strLen the length of the string or -1 if null terminated.
380 U_DRAFT void U_EXPORT2
381 uset_addAllCodePoints(USet* set, const UChar *str, int32_t strLen);
384 * Removes the given character from the given USet. After this call,
385 * uset_contains(set, c) will return FALSE.
386 * @param set the object from which to remove the character
387 * @param c the character to remove
390 U_STABLE void U_EXPORT2
391 uset_remove(USet* set, UChar32 c);
394 * Removes the given range of characters from the given USet. After this call,
395 * uset_contains(set, start, end) will return FALSE.
396 * @param set the object to which to add the character
397 * @param start the first character of the range to remove, inclusive
398 * @param end the last character of the range to remove, inclusive
401 U_STABLE void U_EXPORT2
402 uset_removeRange(USet* set, UChar32 start, UChar32 end);
405 * Removes the given string to the given USet. After this call,
406 * uset_containsString(set, str, strLen) will return FALSE.
407 * @param set the object to which to add the character
408 * @param str the string to remove
409 * @param strLen the length of the string or -1 if null terminated.
412 U_STABLE void U_EXPORT2
413 uset_removeString(USet* set, const UChar* str, int32_t strLen);
416 * Removes from this set all of its elements that are contained in the
417 * specified set. This operation effectively modifies this
418 * set so that its value is the <i>asymmetric set difference</i> of
420 * @param set the object from which the elements are to be removed
421 * @param removeSet the object that defines which elements will be
422 * removed from this set
425 U_DRAFT void U_EXPORT2
426 uset_removeAll(USet* set, const USet* removeSet);
429 * Retain only the elements in this set that are contained in the
430 * specified range. If <code>start > end</code> then an empty range is
431 * retained, leaving the set empty. This is equivalent to
432 * a boolean logic AND, or a set INTERSECTION.
434 * @param set the object for which to retain only the specified range
435 * @param start first character, inclusive, of range to be retained
437 * @param end last character, inclusive, of range to be retained
441 U_DRAFT void U_EXPORT2
442 uset_retain(USet* set, UChar32 start, UChar32 end);
445 * Retains only the elements in this set that are contained in the
446 * specified set. In other words, removes from this set all of
447 * its elements that are not contained in the specified set. This
448 * operation effectively modifies this set so that its value is
449 * the <i>intersection</i> of the two sets.
451 * @param set the object on which to perform the retain
452 * @param retain set that defines which elements this set will retain
455 U_DRAFT void U_EXPORT2
456 uset_retainAll(USet* set, const USet* retain);
459 * Reallocate this objects internal structures to take up the least
460 * possible space, without changing this object's value.
462 * @param set the object on which to perfrom the compact
465 U_DRAFT void U_EXPORT2
466 uset_compact(USet* set);
469 * Inverts this set. This operation modifies this set so that
470 * its value is its complement. This operation does not affect
471 * the multicharacter strings, if any.
475 U_STABLE void U_EXPORT2
476 uset_complement(USet* set);
479 * Complements in this set all elements contained in the specified
480 * set. Any character in the other set will be removed if it is
481 * in this set, or will be added if it is not in this set.
483 * @param set the set with which to complement
484 * @param complement set that defines which elements will be xor'ed
488 U_DRAFT void U_EXPORT2
489 uset_complementAll(USet* set, const USet* complement);
492 * Removes all of the elements from this set. This set will be
493 * empty after this call returns.
497 U_STABLE void U_EXPORT2
498 uset_clear(USet* set);
501 * Returns TRUE if the given USet contains no characters and no
504 * @return true if set is empty
507 U_STABLE UBool U_EXPORT2
508 uset_isEmpty(const USet* set);
511 * Returns TRUE if the given USet contains the given character.
513 * @param c The codepoint to check for within the set
514 * @return true if set contains c
517 U_STABLE UBool U_EXPORT2
518 uset_contains(const USet* set, UChar32 c);
521 * Returns TRUE if the given USet contains all characters c
522 * where start <= c && c <= end.
524 * @param start the first character of the range to test, inclusive
525 * @param end the last character of the range to test, inclusive
526 * @return TRUE if set contains the range
529 U_STABLE UBool U_EXPORT2
530 uset_containsRange(const USet* set, UChar32 start, UChar32 end);
533 * Returns TRUE if the given USet contains the given string.
535 * @param str the string
536 * @param strLen the length of the string or -1 if null terminated.
537 * @return true if set contains str
540 U_STABLE UBool U_EXPORT2
541 uset_containsString(const USet* set, const UChar* str, int32_t strLen);
544 * Returns the index of the given character within this set, where
545 * the set is ordered by ascending code point. If the character
546 * is not in this set, return -1. The inverse of this method is
547 * <code>charAt()</code>.
549 * @param c the character to obtain the index for
550 * @return an index from 0..size()-1, or -1
553 U_DRAFT int32_t U_EXPORT2
554 uset_indexOf(const USet* set, UChar32 c);
557 * Returns the character at the given index within this set, where
558 * the set is ordered by ascending code point. If the index is
559 * out of range, return (UChar32)-1. The inverse of this method is
560 * <code>indexOf()</code>.
562 * @param index an index from 0..size()-1 to obtain the char for
563 * @return the character at the given index, or (UChar32)-1.
566 U_DRAFT UChar32 U_EXPORT2
567 uset_charAt(const USet* set, int32_t index);
570 * Returns the number of characters and strings contained in the given
573 * @return a non-negative integer counting the characters and strings
577 U_STABLE int32_t U_EXPORT2
578 uset_size(const USet* set);
581 * Returns the number of items in this set. An item is either a range
582 * of characters or a single multicharacter string.
584 * @return a non-negative integer counting the character ranges
585 * and/or strings contained in set
588 U_STABLE int32_t U_EXPORT2
589 uset_getItemCount(const USet* set);
592 * Returns an item of this set. An item is either a range of
593 * characters or a single multicharacter string.
595 * @param itemIndex a non-negative integer in the range 0..
596 * uset_getItemCount(set)-1
597 * @param start pointer to variable to receive first character
598 * in range, inclusive
599 * @param end pointer to variable to receive last character in range,
601 * @param str buffer to receive the string, may be NULL
602 * @param strCapacity capacity of str, or 0 if str is NULL
603 * @param ec error code
604 * @return the length of the string (>= 2), or 0 if the item is a
605 * range, in which case it is the range *start..*end, or -1 if
606 * itemIndex is out of range
609 U_STABLE int32_t U_EXPORT2
610 uset_getItem(const USet* set, int32_t itemIndex,
611 UChar32* start, UChar32* end,
612 UChar* str, int32_t strCapacity,
616 * Returns true if set1 contains all the characters and strings
617 * of set2. It answers the question, 'Is set1 a subset of set2?'
618 * @param set1 set to be checked for containment
619 * @param set2 set to be checked for containment
620 * @return true if the test condition is met
623 U_DRAFT UBool U_EXPORT2
624 uset_containsAll(const USet* set1, const USet* set2);
627 * Returns true if this set contains all the characters
628 * of the given string. This is does not check containment of grapheme
629 * clusters, like uset_containsString.
630 * @param set set of characters to be checked for containment
631 * @param str string containing codepoints to be checked for containment
632 * @param strLen the length of the string or -1 if null terminated.
633 * @return true if the test condition is met
636 U_DRAFT UBool U_EXPORT2
637 uset_containsAllCodePoints(const USet* set, const UChar *str, int32_t strLen);
640 * Returns true if set1 contains none of the characters and strings
641 * of set2. It answers the question, 'Is set1 a disjoint set of set2?'
642 * @param set1 set to be checked for containment
643 * @param set2 set to be checked for containment
644 * @return true if the test condition is met
647 U_DRAFT UBool U_EXPORT2
648 uset_containsNone(const USet* set1, const USet* set2);
651 * Returns true if set1 contains some of the characters and strings
652 * of set2. It answers the question, 'Does set1 and set2 have an intersection?'
653 * @param set1 set to be checked for containment
654 * @param set2 set to be checked for containment
655 * @return true if the test condition is met
658 U_DRAFT UBool U_EXPORT2
659 uset_containsSome(const USet* set1, const USet* set2);
662 * Returns true if set1 contains all of the characters and strings
663 * of set2, and vis versa. It answers the question, 'Is set1 equal to set2?'
664 * @param set1 set to be checked for containment
665 * @param set2 set to be checked for containment
666 * @return true if the test condition is met
669 U_DRAFT UBool U_EXPORT2
670 uset_equals(const USet* set1, const USet* set2);
672 /*********************************************************************
674 *********************************************************************/
677 * Serializes this set into an array of 16-bit integers. Serialization
678 * (currently) only records the characters in the set; multicharacter
679 * strings are ignored.
682 * has following format (each line is one 16-bit integer):
684 * length = (n+2*m) | (m!=0?0x8000:0)
685 * bmpLength = n; present if m!=0
698 * The array starts with a header. After the header are n bmp
699 * code points, then m supplementary code points. Either n or m
700 * or both may be zero. n+2*m is always <= 0x7FFF.
702 * If there are no supplementary characters (if m==0) then the
703 * header is one 16-bit integer, 'length', with value n.
705 * If there are supplementary characters (if m!=0) then the header
706 * is two 16-bit integers. The first, 'length', has value
707 * (n+2*m)|0x8000. The second, 'bmpLength', has value n.
709 * After the header the code points are stored in ascending order.
710 * Supplementary code points are stored as most significant 16
711 * bits followed by least significant 16 bits.
714 * @param dest pointer to buffer of destCapacity 16-bit integers.
715 * May be NULL only if destCapacity is zero.
716 * @param destCapacity size of dest, or zero. Must not be negative.
717 * @param pErrorCode pointer to the error code. Will be set to
718 * U_INDEX_OUTOFBOUNDS_ERROR if n+2*m > 0x7FFF. Will be set to
719 * U_BUFFER_OVERFLOW_ERROR if n+2*m+(m!=0?2:1) > destCapacity.
720 * @return the total length of the serialized format, including
721 * the header, that is, n+2*m+(m!=0?2:1), or 0 on error other
722 * than U_BUFFER_OVERFLOW_ERROR.
725 U_STABLE int32_t U_EXPORT2
726 uset_serialize(const USet* set, uint16_t* dest, int32_t destCapacity, UErrorCode* pErrorCode);
729 * Given a serialized array, fill in the given serialized set object.
730 * @param fillSet pointer to result
731 * @param src pointer to start of array
732 * @param srcLength length of array
733 * @return true if the given array is valid, otherwise false
736 U_STABLE UBool U_EXPORT2
737 uset_getSerializedSet(USerializedSet* fillSet, const uint16_t* src, int32_t srcLength);
740 * Set the USerializedSet to contain the given character (and nothing
742 * @param fillSet pointer to result
743 * @param c The codepoint to set
746 U_STABLE void U_EXPORT2
747 uset_setSerializedToOne(USerializedSet* fillSet, UChar32 c);
750 * Returns TRUE if the given USerializedSet contains the given
752 * @param set the serialized set
753 * @param c The codepoint to check for within the set
754 * @return true if set contains c
757 U_STABLE UBool U_EXPORT2
758 uset_serializedContains(const USerializedSet* set, UChar32 c);
761 * Returns the number of disjoint ranges of characters contained in
762 * the given serialized set. Ignores any strings contained in the
764 * @param set the serialized set
765 * @return a non-negative integer counting the character ranges
769 U_STABLE int32_t U_EXPORT2
770 uset_getSerializedRangeCount(const USerializedSet* set);
773 * Returns a range of characters contained in the given serialized
775 * @param set the serialized set
776 * @param rangeIndex a non-negative integer in the range 0..
777 * uset_getSerializedRangeCount(set)-1
778 * @param pStart pointer to variable to receive first character
779 * in range, inclusive
780 * @param pEnd pointer to variable to receive last character in range,
782 * @return true if rangeIndex is valid, otherwise false
785 U_STABLE UBool U_EXPORT2
786 uset_getSerializedRange(const USerializedSet* set, int32_t rangeIndex,
787 UChar32* pStart, UChar32* pEnd);