2 *******************************************************************************
4 * Copyright (C) 2002-2004, 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.
62 USET_CASE_INSENSITIVE
= 2,
65 * Bitmask for UnicodeSet::closeOver() indicating letter case.
66 * This may be ORed together with other selectors.
72 * Enable case insensitive matching. E.g., "[ab]" with this flag
73 * will match 'a', 'A', 'b', and 'B'. "[^ab]" with this flag will
74 * match all except 'a', 'A', 'b', and 'B'. This adds the lower-,
75 * title-, and uppercase mappings as well as the case folding
76 * of each existing element in the set.
79 USET_ADD_CASE_MAPPINGS
= 4,
82 * Enough for any single-code point set
85 USET_SERIALIZED_STATIC_ARRAY_CAPACITY
=8
89 * A serialized form of a Unicode set. Limited manipulations are
90 * possible directly on a serialized set. See below.
93 typedef struct USerializedSet
{
95 * The serialized Unicode Set.
98 const uint16_t *array
;
100 * The length of the array that contains BMP characters.
105 * The total length of the array.
110 * A small buffer for the array to reduce memory allocations.
113 uint16_t staticArray
[USET_SERIALIZED_STATIC_ARRAY_CAPACITY
];
116 /*********************************************************************
118 *********************************************************************/
121 * Creates a USet object that contains the range of characters
122 * start..end, inclusive.
123 * @param start first character of the range, inclusive
124 * @param end last character of the range, inclusive
125 * @return a newly created USet. The caller must call uset_close() on
129 U_STABLE USet
* U_EXPORT2
130 uset_open(UChar32 start
, UChar32 end
);
133 * Creates a set from the given pattern. See the UnicodeSet class
134 * description for the syntax of the pattern language.
135 * @param pattern a string specifying what characters are in the set
136 * @param patternLength the length of the pattern, or -1 if null
138 * @param ec the error code
141 U_STABLE USet
* U_EXPORT2
142 uset_openPattern(const UChar
* pattern
, int32_t patternLength
,
146 * Creates a set from the given pattern. See the UnicodeSet class
147 * description for the syntax of the pattern language.
148 * @param pattern a string specifying what characters are in the set
149 * @param patternLength the length of the pattern, or -1 if null
151 * @param options bitmask for options to apply to the pattern.
152 * Valid options are USET_IGNORE_SPACE and USET_CASE_INSENSITIVE.
153 * @param ec the error code
156 U_STABLE USet
* U_EXPORT2
157 uset_openPatternOptions(const UChar
* pattern
, int32_t patternLength
,
162 * Disposes of the storage used by a USet object. This function should
163 * be called exactly once for objects returned by uset_open().
164 * @param set the object to dispose of
167 U_STABLE
void U_EXPORT2
168 uset_close(USet
* set
);
171 * Causes the USet object to represent the range <code>start - end</code>.
172 * If <code>start > end</code> then this USet is set to an empty range.
173 * @param set the object to set to the given range
174 * @param start first character in the set, inclusive
175 * @param end last character in the set, inclusive
178 U_DRAFT
void U_EXPORT2
180 UChar32 start
, UChar32 end
);
183 * Modifies the set to represent the set specified by the given
184 * pattern. See the UnicodeSet class description for the syntax of
185 * the pattern language. See also the User Guide chapter about UnicodeSet.
186 * <em>Empties the set passed before applying the pattern.</em>
187 * @param set The set to which the pattern is to be applied.
188 * @param pattern A pointer to UChar string specifying what characters are in the set.
189 * The character at pattern[0] must be a '['.
190 * @param patternLength The length of the UChar string. -1 if NUL terminated.
191 * @param options A bitmask for options to apply to the pattern.
192 * Valid options are USET_IGNORE_SPACE and USET_CASE_INSENSITIVE.
193 * @param status Returns an error if the pattern cannot be parsed.
194 * @return Upon successful parse, the value is either
195 * the index of the character after the closing ']'
196 * of the parsed pattern.
197 * If the status code indicates failure, then the return value
198 * is the index of the error in the source.
202 U_DRAFT
int32_t U_EXPORT2
203 uset_applyPattern(USet
*set
,
204 const UChar
*pattern
, int32_t patternLength
,
209 * Modifies the set to contain those code points which have the given value
210 * for the given binary or enumerated property, as returned by
211 * u_getIntPropertyValue. Prior contents of this set are lost.
213 * @param set the object to contain the code points defined by the property
215 * @param prop a property in the range UCHAR_BIN_START..UCHAR_BIN_LIMIT-1
216 * or UCHAR_INT_START..UCHAR_INT_LIMIT-1
217 * or UCHAR_MASK_START..UCHAR_MASK_LIMIT-1.
219 * @param value a value in the range u_getIntPropertyMinValue(prop)..
220 * u_getIntPropertyMaxValue(prop), with one exception. If prop is
221 * UCHAR_GENERAL_CATEGORY_MASK, then value should not be a UCharCategory, but
222 * rather a mask value produced by U_GET_GC_MASK(). This allows grouped
223 * categories such as [:L:] to be represented.
225 * @param ec error code input/output parameter
229 U_DRAFT
void U_EXPORT2
230 uset_applyIntPropertyValue(USet
* set
,
231 UProperty prop
, int32_t value
, UErrorCode
* ec
);
234 * Modifies the set to contain those code points which have the
235 * given value for the given property. Prior contents of this
238 * @param set the object to contain the code points defined by the given
239 * property and value alias
241 * @param prop a string specifying a property alias, either short or long.
242 * The name is matched loosely. See PropertyAliases.txt for names and a
243 * description of loose matching. If the value string is empty, then this
244 * string is interpreted as either a General_Category value alias, a Script
245 * value alias, a binary property alias, or a special ID. Special IDs are
246 * matched loosely and correspond to the following sets:
248 * "ANY" = [\\u0000-\\U0010FFFF],
249 * "ASCII" = [\\u0000-\\u007F].
251 * @param propLength the length of the prop, or -1 if NULL
253 * @param value a string specifying a value alias, either short or long.
254 * The name is matched loosely. See PropertyValueAliases.txt for names
255 * and a description of loose matching. In addition to aliases listed,
256 * numeric values and canonical combining classes may be expressed
257 * numerically, e.g., ("nv", "0.5") or ("ccc", "220"). The value string
260 * @param valueLength the length of the value, or -1 if NULL
262 * @param ec error code input/output parameter
266 U_DRAFT
void U_EXPORT2
267 uset_applyPropertyAlias(USet
* set
,
268 const UChar
*prop
, int32_t propLength
,
269 const UChar
*value
, int32_t valueLength
,
273 * Return true if the given position, in the given pattern, appears
274 * to be the start of a UnicodeSet pattern.
276 * @param pattern a string specifying the pattern
277 * @param patternLength the length of the pattern, or -1 if NULL
278 * @param pos the given position
281 U_DRAFT UBool U_EXPORT2
282 uset_resemblesPattern(const UChar
*pattern
, int32_t patternLength
,
286 * Returns a string representation of this set. If the result of
287 * calling this function is passed to a uset_openPattern(), it
288 * will produce another set that is equal to this one.
290 * @param result the string to receive the rules, may be NULL
291 * @param resultCapacity the capacity of result, may be 0 if result is NULL
292 * @param escapeUnprintable if TRUE then convert unprintable
293 * character to their hex escape representations, \\uxxxx or
294 * \\Uxxxxxxxx. Unprintable characters are those other than
295 * U+000A, U+0020..U+007E.
296 * @param ec error code.
297 * @return length of string, possibly larger than resultCapacity
300 U_STABLE
int32_t U_EXPORT2
301 uset_toPattern(const USet
* set
,
302 UChar
* result
, int32_t resultCapacity
,
303 UBool escapeUnprintable
,
307 * Adds the given character to the given USet. After this call,
308 * uset_contains(set, c) will return TRUE.
309 * @param set the object to which to add the character
310 * @param c the character to add
313 U_STABLE
void U_EXPORT2
314 uset_add(USet
* set
, UChar32 c
);
317 * Adds all of the elements in the specified set to this set if
318 * they're not already present. This operation effectively
319 * modifies this set so that its value is the <i>union</i> of the two
320 * sets. The behavior of this operation is unspecified if the specified
321 * collection is modified while the operation is in progress.
323 * @param set the object to which to add the set
324 * @param additionalSet the source set whose elements are to be added to this set.
327 U_STABLE
void U_EXPORT2
328 uset_addAll(USet
* set
, const USet
*additionalSet
);
331 * Adds the given range of characters to the given USet. After this call,
332 * uset_contains(set, start, end) will return TRUE.
333 * @param set the object to which to add the character
334 * @param start the first character of the range to add, inclusive
335 * @param end the last character of the range to add, inclusive
338 U_STABLE
void U_EXPORT2
339 uset_addRange(USet
* set
, UChar32 start
, UChar32 end
);
342 * Adds the given string to the given USet. After this call,
343 * uset_containsString(set, str, strLen) will return TRUE.
344 * @param set the object to which to add the character
345 * @param str the string to add
346 * @param strLen the length of the string or -1 if null terminated.
349 U_STABLE
void U_EXPORT2
350 uset_addString(USet
* set
, const UChar
* str
, int32_t strLen
);
353 * Removes the given character from the given USet. After this call,
354 * uset_contains(set, c) will return FALSE.
355 * @param set the object from which to remove the character
356 * @param c the character to remove
359 U_STABLE
void U_EXPORT2
360 uset_remove(USet
* set
, UChar32 c
);
363 * Removes the given range of characters from the given USet. After this call,
364 * uset_contains(set, start, end) will return FALSE.
365 * @param set the object to which to add the character
366 * @param start the first character of the range to remove, inclusive
367 * @param end the last character of the range to remove, inclusive
370 U_STABLE
void U_EXPORT2
371 uset_removeRange(USet
* set
, UChar32 start
, UChar32 end
);
374 * Removes the given string to the given USet. After this call,
375 * uset_containsString(set, str, strLen) will return FALSE.
376 * @param set the object to which to add the character
377 * @param str the string to remove
378 * @param strLen the length of the string or -1 if null terminated.
381 U_STABLE
void U_EXPORT2
382 uset_removeString(USet
* set
, const UChar
* str
, int32_t strLen
);
385 * Removes from this set all of its elements that are contained in the
386 * specified set. This operation effectively modifies this
387 * set so that its value is the <i>asymmetric set difference</i> of
389 * @param set the object from which the elements are to be removed
390 * @param removeSet the object that defines which elements will be
391 * removed from this set
394 U_DRAFT
void U_EXPORT2
395 uset_removeAll(USet
* set
, const USet
* removeSet
);
398 * Retain only the elements in this set that are contained in the
399 * specified range. If <code>start > end</code> then an empty range is
400 * retained, leaving the set empty. This is equivalent to
401 * a boolean logic AND, or a set INTERSECTION.
403 * @param set the object for which to retain only the specified range
404 * @param start first character, inclusive, of range to be retained
406 * @param end last character, inclusive, of range to be retained
410 U_DRAFT
void U_EXPORT2
411 uset_retain(USet
* set
, UChar32 start
, UChar32 end
);
414 * Retains only the elements in this set that are contained in the
415 * specified set. In other words, removes from this set all of
416 * its elements that are not contained in the specified set. This
417 * operation effectively modifies this set so that its value is
418 * the <i>intersection</i> of the two sets.
420 * @param set the object on which to perform the retain
421 * @param retain set that defines which elements this set will retain
424 U_DRAFT
void U_EXPORT2
425 uset_retainAll(USet
* set
, const USet
* retain
);
428 * Reallocate this objects internal structures to take up the least
429 * possible space, without changing this object's value.
431 * @param set the object on which to perfrom the compact
434 U_DRAFT
void U_EXPORT2
435 uset_compact(USet
* set
);
438 * Inverts this set. This operation modifies this set so that
439 * its value is its complement. This operation does not affect
440 * the multicharacter strings, if any.
444 U_STABLE
void U_EXPORT2
445 uset_complement(USet
* set
);
448 * Complements in this set all elements contained in the specified
449 * set. Any character in the other set will be removed if it is
450 * in this set, or will be added if it is not in this set.
452 * @param set the set with which to complement
453 * @param complement set that defines which elements will be xor'ed
457 U_DRAFT
void U_EXPORT2
458 uset_complementAll(USet
* set
, const USet
* complement
);
461 * Removes all of the elements from this set. This set will be
462 * empty after this call returns.
466 U_STABLE
void U_EXPORT2
467 uset_clear(USet
* set
);
470 * Returns TRUE if the given USet contains no characters and no
473 * @return true if set is empty
476 U_STABLE UBool U_EXPORT2
477 uset_isEmpty(const USet
* set
);
480 * Returns TRUE if the given USet contains the given character.
482 * @param c The codepoint to check for within the set
483 * @return true if set contains c
486 U_STABLE UBool U_EXPORT2
487 uset_contains(const USet
* set
, UChar32 c
);
490 * Returns TRUE if the given USet contains all characters c
491 * where start <= c && c <= end.
493 * @param start the first character of the range to test, inclusive
494 * @param end the last character of the range to test, inclusive
495 * @return TRUE if set contains the range
498 U_STABLE UBool U_EXPORT2
499 uset_containsRange(const USet
* set
, UChar32 start
, UChar32 end
);
502 * Returns TRUE if the given USet contains the given string.
504 * @param str the string
505 * @param strLen the length of the string or -1 if null terminated.
506 * @return true if set contains str
509 U_STABLE UBool U_EXPORT2
510 uset_containsString(const USet
* set
, const UChar
* str
, int32_t strLen
);
513 * Returns the index of the given character within this set, where
514 * the set is ordered by ascending code point. If the character
515 * is not in this set, return -1. The inverse of this method is
516 * <code>charAt()</code>.
518 * @param c the character to obtain the index for
519 * @return an index from 0..size()-1, or -1
522 U_DRAFT
int32_t U_EXPORT2
523 uset_indexOf(const USet
* set
, UChar32 c
);
526 * Returns the character at the given index within this set, where
527 * the set is ordered by ascending code point. If the index is
528 * out of range, return (UChar32)-1. The inverse of this method is
529 * <code>indexOf()</code>.
531 * @param index an index from 0..size()-1 to obtain the char for
532 * @return the character at the given index, or (UChar32)-1.
535 U_DRAFT UChar32 U_EXPORT2
536 uset_charAt(const USet
* set
, int32_t index
);
539 * Returns the number of characters and strings contained in the given
542 * @return a non-negative integer counting the characters and strings
546 U_STABLE
int32_t U_EXPORT2
547 uset_size(const USet
* set
);
550 * Returns the number of items in this set. An item is either a range
551 * of characters or a single multicharacter string.
553 * @return a non-negative integer counting the character ranges
554 * and/or strings contained in set
557 U_STABLE
int32_t U_EXPORT2
558 uset_getItemCount(const USet
* set
);
561 * Returns an item of this set. An item is either a range of
562 * characters or a single multicharacter string.
564 * @param itemIndex a non-negative integer in the range 0..
565 * uset_getItemCount(set)-1
566 * @param start pointer to variable to receive first character
567 * in range, inclusive
568 * @param end pointer to variable to receive last character in range,
570 * @param str buffer to receive the string, may be NULL
571 * @param strCapacity capacity of str, or 0 if str is NULL
572 * @param ec error code
573 * @return the length of the string (>= 2), or 0 if the item is a
574 * range, in which case it is the range *start..*end, or -1 if
575 * itemIndex is out of range
578 U_STABLE
int32_t U_EXPORT2
579 uset_getItem(const USet
* set
, int32_t itemIndex
,
580 UChar32
* start
, UChar32
* end
,
581 UChar
* str
, int32_t strCapacity
,
585 * Returns true if set1 contains all the characters and strings
586 * of set2. It answers the question, 'Is set1 a subset of set2?'
587 * @param set1 set to be checked for containment
588 * @param set2 set to be checked for containment
589 * @return true if the test condition is met
592 U_DRAFT UBool U_EXPORT2
593 uset_containsAll(const USet
* set1
, const USet
* set2
);
596 * Returns true if set1 contains none of the characters and strings
597 * of set2. It answers the question, 'Is set1 a disjoint set of set2?'
598 * @param set1 set to be checked for containment
599 * @param set2 set to be checked for containment
600 * @return true if the test condition is met
603 U_DRAFT UBool U_EXPORT2
604 uset_containsNone(const USet
* set1
, const USet
* set2
);
607 * Returns true if set1 contains some of the characters and strings
608 * of set2. It answers the question, 'Does set1 and set2 have an intersection?'
609 * @param set1 set to be checked for containment
610 * @param set2 set to be checked for containment
611 * @return true if the test condition is met
614 U_DRAFT UBool U_EXPORT2
615 uset_containsSome(const USet
* set1
, const USet
* set2
);
618 * Returns true if set1 contains all of the characters and strings
619 * of set2, and vis versa. It answers the question, 'Is set1 equal to set2?'
620 * @param set1 set to be checked for containment
621 * @param set2 set to be checked for containment
622 * @return true if the test condition is met
625 U_DRAFT UBool U_EXPORT2
626 uset_equals(const USet
* set1
, const USet
* set2
);
628 /*********************************************************************
630 *********************************************************************/
633 * Serializes this set into an array of 16-bit integers. Serialization
634 * (currently) only records the characters in the set; multicharacter
635 * strings are ignored.
638 * has following format (each line is one 16-bit integer):
640 * length = (n+2*m) | (m!=0?0x8000:0)
641 * bmpLength = n; present if m!=0
654 * The array starts with a header. After the header are n bmp
655 * code points, then m supplementary code points. Either n or m
656 * or both may be zero. n+2*m is always <= 0x7FFF.
658 * If there are no supplementary characters (if m==0) then the
659 * header is one 16-bit integer, 'length', with value n.
661 * If there are supplementary characters (if m!=0) then the header
662 * is two 16-bit integers. The first, 'length', has value
663 * (n+2*m)|0x8000. The second, 'bmpLength', has value n.
665 * After the header the code points are stored in ascending order.
666 * Supplementary code points are stored as most significant 16
667 * bits followed by least significant 16 bits.
670 * @param dest pointer to buffer of destCapacity 16-bit integers.
671 * May be NULL only if destCapacity is zero.
672 * @param destCapacity size of dest, or zero. Must not be negative.
673 * @param pErrorCode pointer to the error code. Will be set to
674 * U_INDEX_OUTOFBOUNDS_ERROR if n+2*m > 0x7FFF. Will be set to
675 * U_BUFFER_OVERFLOW_ERROR if n+2*m+(m!=0?2:1) > destCapacity.
676 * @return the total length of the serialized format, including
677 * the header, that is, n+2*m+(m!=0?2:1), or 0 on error other
678 * than U_BUFFER_OVERFLOW_ERROR.
681 U_STABLE
int32_t U_EXPORT2
682 uset_serialize(const USet
* set
, uint16_t* dest
, int32_t destCapacity
, UErrorCode
* pErrorCode
);
685 * Given a serialized array, fill in the given serialized set object.
686 * @param fillSet pointer to result
687 * @param src pointer to start of array
688 * @param srcLength length of array
689 * @return true if the given array is valid, otherwise false
692 U_STABLE UBool U_EXPORT2
693 uset_getSerializedSet(USerializedSet
* fillSet
, const uint16_t* src
, int32_t srcLength
);
696 * Set the USerializedSet to contain the given character (and nothing
698 * @param fillSet pointer to result
699 * @param c The codepoint to set
702 U_STABLE
void U_EXPORT2
703 uset_setSerializedToOne(USerializedSet
* fillSet
, UChar32 c
);
706 * Returns TRUE if the given USerializedSet contains the given
708 * @param set the serialized set
709 * @param c The codepoint to check for within the set
710 * @return true if set contains c
713 U_STABLE UBool U_EXPORT2
714 uset_serializedContains(const USerializedSet
* set
, UChar32 c
);
717 * Returns the number of disjoint ranges of characters contained in
718 * the given serialized set. Ignores any strings contained in the
720 * @param set the serialized set
721 * @return a non-negative integer counting the character ranges
725 U_STABLE
int32_t U_EXPORT2
726 uset_getSerializedRangeCount(const USerializedSet
* set
);
729 * Returns a range of characters contained in the given serialized
731 * @param set the serialized set
732 * @param rangeIndex a non-negative integer in the range 0..
733 * uset_getSerializedRangeCount(set)-1
734 * @param pStart pointer to variable to receive first character
735 * in range, inclusive
736 * @param pEnd pointer to variable to receive last character in range,
738 * @return true if rangeIndex is valid, otherwise false
741 U_STABLE UBool U_EXPORT2
742 uset_getSerializedRange(const USerializedSet
* set
, int32_t rangeIndex
,
743 UChar32
* pStart
, UChar32
* pEnd
);