1// © 2016 and later: Unicode, Inc. and others.
2// License & terms of use: http://www.unicode.org/copyright.html
3/*
4*******************************************************************************
5*
6*   Copyright (C) 2002-2014, International Business Machines
7*   Corporation and others.  All Rights Reserved.
8*
9*******************************************************************************
10*   file name:  uset.h
11*   encoding:   UTF-8
12*   tab size:   8 (not used)
13*   indentation:4
14*
15*   created on: 2002mar07
16*   created by: Markus W. Scherer
17*
18*   C version of UnicodeSet.
19*/
20
21
22/**
23 * \file
24 * \brief C API: Unicode Set
25 *
26 * <p>This is a C wrapper around the C++ UnicodeSet class.</p>
27 */
28
29#ifndef __USET_H__
30#define __USET_H__
31
32#include "unicode/utypes.h"
33#include "unicode/uchar.h"
34
35#if U_SHOW_CPLUSPLUS_API
36#include "unicode/localpointer.h"
37#endif   // U_SHOW_CPLUSPLUS_API
38
39#ifndef USET_DEFINED
40
41#ifndef U_IN_DOXYGEN
42#define USET_DEFINED
43#endif
44/**
45 * USet is the C API type corresponding to C++ class UnicodeSet.
46 * Use the uset_* API to manipulate.  Create with
47 * uset_open*, and destroy with uset_close.
48 * @stable ICU 2.4
49 */
50typedef struct USet USet;
51#endif
52
53/**
54 * Bitmask values to be passed to uset_openPatternOptions() or
55 * uset_applyPattern() taking an option parameter.
56 * @stable ICU 2.4
57 */
58enum {
59    /**
60     * Ignore white space within patterns unless quoted or escaped.
61     * @stable ICU 2.4
62     */
63    USET_IGNORE_SPACE = 1,
64
65    /**
66     * Enable case insensitive matching.  E.g., "[ab]" with this flag
67     * will match 'a', 'A', 'b', and 'B'.  "[^ab]" with this flag will
68     * match all except 'a', 'A', 'b', and 'B'. This performs a full
69     * closure over case mappings, e.g. U+017F for s.
70     *
71     * The resulting set is a superset of the input for the code points but
72     * not for the strings.
73     * It performs a case mapping closure of the code points and adds
74     * full case folding strings for the code points, and reduces strings of
75     * the original set to their full case folding equivalents.
76     *
77     * This is designed for case-insensitive matches, for example
78     * in regular expressions. The full code point case closure allows checking of
79     * an input character directly against the closure set.
80     * Strings are matched by comparing the case-folded form from the closure
81     * set with an incremental case folding of the string in question.
82     *
83     * The closure set will also contain single code points if the original
84     * set contained case-equivalent strings (like U+00DF for "ss" or "Ss" etc.).
85     * This is not necessary (that is, redundant) for the above matching method
86     * but results in the same closure sets regardless of whether the original
87     * set contained the code point or a string.
88     *
89     * @stable ICU 2.4
90     */
91    USET_CASE_INSENSITIVE = 2,
92
93    /**
94     * Enable case insensitive matching.  E.g., "[ab]" with this flag
95     * will match 'a', 'A', 'b', and 'B'.  "[^ab]" with this flag will
96     * match all except 'a', 'A', 'b', and 'B'. This adds the lower-,
97     * title-, and uppercase mappings as well as the case folding
98     * of each existing element in the set.
99     * @stable ICU 3.2
100     */
101    USET_ADD_CASE_MAPPINGS = 4
102};
103
104/**
105 * Argument values for whether span() and similar functions continue while
106 * the current character is contained vs. not contained in the set.
107 *
108 * The functionality is straightforward for sets with only single code points,
109 * without strings (which is the common case):
110 * - USET_SPAN_CONTAINED and USET_SPAN_SIMPLE work the same.
111 * - USET_SPAN_CONTAINED and USET_SPAN_SIMPLE are inverses of USET_SPAN_NOT_CONTAINED.
112 * - span() and spanBack() partition any string the same way when
113 *   alternating between span(USET_SPAN_NOT_CONTAINED) and
114 *   span(either "contained" condition).
115 * - Using a complemented (inverted) set and the opposite span conditions
116 *   yields the same results.
117 *
118 * When a set contains multi-code point strings, then these statements may not
119 * be true, depending on the strings in the set (for example, whether they
120 * overlap with each other) and the string that is processed.
121 * For a set with strings:
122 * - The complement of the set contains the opposite set of code points,
123 *   but the same set of strings.
124 *   Therefore, complementing both the set and the span conditions
125 *   may yield different results.
126 * - When starting spans at different positions in a string
127 *   (span(s, ...) vs. span(s+1, ...)) the ends of the spans may be different
128 *   because a set string may start before the later position.
129 * - span(USET_SPAN_SIMPLE) may be shorter than
130 *   span(USET_SPAN_CONTAINED) because it will not recursively try
131 *   all possible paths.
132 *   For example, with a set which contains the three strings "xy", "xya" and "ax",
133 *   span("xyax", USET_SPAN_CONTAINED) will return 4 but
134 *   span("xyax", USET_SPAN_SIMPLE) will return 3.
135 *   span(USET_SPAN_SIMPLE) will never be longer than
136 *   span(USET_SPAN_CONTAINED).
137 * - With either "contained" condition, span() and spanBack() may partition
138 *   a string in different ways.
139 *   For example, with a set which contains the two strings "ab" and "ba",
140 *   and when processing the string "aba",
141 *   span() will yield contained/not-contained boundaries of { 0, 2, 3 }
142 *   while spanBack() will yield boundaries of { 0, 1, 3 }.
143 *
144 * Note: If it is important to get the same boundaries whether iterating forward
145 * or backward through a string, then either only span() should be used and
146 * the boundaries cached for backward operation, or an ICU BreakIterator
147 * could be used.
148 *
149 * Note: Unpaired surrogates are treated like surrogate code points.
150 * Similarly, set strings match only on code point boundaries,
151 * never in the middle of a surrogate pair.
152 * Illegal UTF-8 sequences are treated like U+FFFD.
153 * When processing UTF-8 strings, malformed set strings
154 * (strings with unpaired surrogates which cannot be converted to UTF-8)
155 * are ignored.
156 *
157 * @stable ICU 3.8
158 */
159typedef enum USetSpanCondition {
160    /**
161     * Continues a span() while there is no set element at the current position.
162     * Increments by one code point at a time.
163     * Stops before the first set element (character or string).
164     * (For code points only, this is like while contains(current)==false).
165     *
166     * When span() returns, the substring between where it started and the position
167     * it returned consists only of characters that are not in the set,
168     * and none of its strings overlap with the span.
169     *
170     * @stable ICU 3.8
171     */
172    USET_SPAN_NOT_CONTAINED = 0,
173    /**
174     * Spans the longest substring that is a concatenation of set elements (characters or strings).
175     * (For characters only, this is like while contains(current)==true).
176     *
177     * When span() returns, the substring between where it started and the position
178     * it returned consists only of set elements (characters or strings) that are in the set.
179     *
180     * If a set contains strings, then the span will be the longest substring for which there
181     * exists at least one non-overlapping concatenation of set elements (characters or strings).
182     * This is equivalent to a POSIX regular expression for <code>(OR of each set element)*</code>.
183     * (Java/ICU/Perl regex stops at the first match of an OR.)
184     *
185     * @stable ICU 3.8
186     */
187    USET_SPAN_CONTAINED = 1,
188    /**
189     * Continues a span() while there is a set element at the current position.
190     * Increments by the longest matching element at each position.
191     * (For characters only, this is like while contains(current)==true).
192     *
193     * When span() returns, the substring between where it started and the position
194     * it returned consists only of set elements (characters or strings) that are in the set.
195     *
196     * If a set only contains single characters, then this is the same
197     * as USET_SPAN_CONTAINED.
198     *
199     * If a set contains strings, then the span will be the longest substring
200     * with a match at each position with the longest single set element (character or string).
201     *
202     * Use this span condition together with other longest-match algorithms,
203     * such as ICU converters (ucnv_getUnicodeSet()).
204     *
205     * @stable ICU 3.8
206     */
207    USET_SPAN_SIMPLE = 2,
208} USetSpanCondition;
209
210enum {
211    /**
212     * Capacity of USerializedSet::staticArray.
213     * Enough for any single-code point set.
214     * Also provides padding for nice sizeof(USerializedSet).
215     * @stable ICU 2.4
216     */
217    USET_SERIALIZED_STATIC_ARRAY_CAPACITY=8
218};
219
220/**
221 * A serialized form of a Unicode set.  Limited manipulations are
222 * possible directly on a serialized set.  See below.
223 * @stable ICU 2.4
224 */
225typedef struct USerializedSet {
226    /**
227     * The serialized Unicode Set.
228     * @stable ICU 2.4
229     */
230    const uint16_t *array;
231    /**
232     * The length of the array that contains BMP characters.
233     * @stable ICU 2.4
234     */
235    int32_t bmpLength;
236    /**
237     * The total length of the array.
238     * @stable ICU 2.4
239     */
240    int32_t length;
241    /**
242     * A small buffer for the array to reduce memory allocations.
243     * @stable ICU 2.4
244     */
245    uint16_t staticArray[USET_SERIALIZED_STATIC_ARRAY_CAPACITY];
246} USerializedSet;
247
248/*********************************************************************
249 * USet API
250 *********************************************************************/
251
252/**
253 * Creates a USet object that contains the range of characters
254 * start..end, inclusive.  If <code>start > end</code>
255 * then an empty set is created (same as using uset_openEmpty()).
256 * @param start first character of the range, inclusive
257 * @param end last character of the range, inclusive
258 * @return a newly created USet.  The caller must call uset_close() on
259 * it when done.
260 * @stable ICU 2.4
261 */
262U_CAPI USet* U_EXPORT2
263uset_open(UChar32 start, UChar32 end);
264
265/**
266 * Creates a set from the given pattern.  See the UnicodeSet class
267 * description for the syntax of the pattern language.
268 * @param pattern a string specifying what characters are in the set
269 * @param patternLength the length of the pattern, or -1 if null
270 * terminated
271 * @param ec the error code
272 * @stable ICU 2.4
273 */
274U_CAPI USet* U_EXPORT2
275uset_openPattern(const UChar* pattern, int32_t patternLength,
276                 UErrorCode* ec);
277
278/**
279 * Creates a set from the given pattern.  See the UnicodeSet class
280 * description for the syntax of the pattern language.
281 * @param pattern a string specifying what characters are in the set
282 * @param patternLength the length of the pattern, or -1 if null
283 * terminated
284 * @param options bitmask for options to apply to the pattern.
285 * Valid options are USET_IGNORE_SPACE and USET_CASE_INSENSITIVE.
286 * @param ec the error code
287 * @stable ICU 2.4
288 */
289U_CAPI USet* U_EXPORT2
290uset_openPatternOptions(const UChar* pattern, int32_t patternLength,
291                 uint32_t options,
292                 UErrorCode* ec);
293
294/**
295 * Disposes of the storage used by a USet object.  This function should
296 * be called exactly once for objects returned by uset_open().
297 * @param set the object to dispose of
298 * @stable ICU 2.4
299 */
300U_CAPI void U_EXPORT2
301uset_close(USet* set);
302
303#if U_SHOW_CPLUSPLUS_API
304
305U_NAMESPACE_BEGIN
306
307/**
308 * \class LocalUSetPointer
309 * "Smart pointer" class, closes a USet via uset_close().
310 * For most methods see the LocalPointerBase base class.
311 *
312 * @see LocalPointerBase
313 * @see LocalPointer
314 * @stable ICU 4.4
315 */
316U_DEFINE_LOCAL_OPEN_POINTER(LocalUSetPointer, USet, uset_close);
317
318U_NAMESPACE_END
319
320#endif
321
322/**
323 * Returns a string representation of this set.  If the result of
324 * calling this function is passed to a uset_openPattern(), it
325 * will produce another set that is equal to this one.
326 * @param set the set
327 * @param result the string to receive the rules, may be NULL
328 * @param resultCapacity the capacity of result, may be 0 if result is NULL
329 * @param escapeUnprintable if true then convert unprintable
330 * character to their hex escape representations, \\uxxxx or
331 * \\Uxxxxxxxx.  Unprintable characters are those other than
332 * U+000A, U+0020..U+007E.
333 * @param ec error code.
334 * @return length of string, possibly larger than resultCapacity
335 * @stable ICU 2.4
336 */
337U_CAPI int32_t U_EXPORT2
338uset_toPattern(const USet* set,
339               UChar* result, int32_t resultCapacity,
340               UBool escapeUnprintable,
341               UErrorCode* ec);
342
343/**
344 * Adds the given character to the given USet.  After this call,
345 * uset_contains(set, c) will return true.
346 * A frozen set will not be modified.
347 * @param set the object to which to add the character
348 * @param c the character to add
349 * @stable ICU 2.4
350 */
351U_CAPI void U_EXPORT2
352uset_add(USet* set, UChar32 c);
353
354/**
355 * Adds the given string to the given USet.  After this call,
356 * uset_containsString(set, str, strLen) will return true.
357 * A frozen set will not be modified.
358 * @param set the object to which to add the character
359 * @param str the string to add
360 * @param strLen the length of the string or -1 if null terminated.
361 * @stable ICU 2.4
362 */
363U_CAPI void U_EXPORT2
364uset_addString(USet* set, const UChar* str, int32_t strLen);
365
366/**
367 * Removes the given character from the given USet.  After this call,
368 * uset_contains(set, c) will return false.
369 * A frozen set will not be modified.
370 * @param set the object from which to remove the character
371 * @param c the character to remove
372 * @stable ICU 2.4
373 */
374U_CAPI void U_EXPORT2
375uset_remove(USet* set, UChar32 c);
376
377/**
378 * Removes the given string to the given USet.  After this call,
379 * uset_containsString(set, str, strLen) will return false.
380 * A frozen set will not be modified.
381 * @param set the object to which to add the character
382 * @param str the string to remove
383 * @param strLen the length of the string or -1 if null terminated.
384 * @stable ICU 2.4
385 */
386U_CAPI void U_EXPORT2
387uset_removeString(USet* set, const UChar* str, int32_t strLen);
388
389/**
390 * This is equivalent to
391 * <code>uset_complementRange(set, 0, 0x10FFFF)</code>.
392 *
393 * <strong>Note:</strong> This performs a symmetric difference with all code points
394 * <em>and thus retains all multicharacter strings</em>.
395 * In order to achieve a “code point complement” (all code points minus this set),
396 * the easiest is to <code>uset_complement(set); uset_removeAllStrings(set);</code>.
397 *
398 * A frozen set will not be modified.
399 * @param set the set
400 * @stable ICU 2.4
401 */
402U_CAPI void U_EXPORT2
403uset_complement(USet* set);
404
405/**
406 * Removes all of the elements from this set.  This set will be
407 * empty after this call returns.
408 * A frozen set will not be modified.
409 * @param set the set
410 * @stable ICU 2.4
411 */
412U_CAPI void U_EXPORT2
413uset_clear(USet* set);
414
415/**
416 * Returns true if the given USet contains no characters and no
417 * strings.
418 * @param set the set
419 * @return true if set is empty
420 * @stable ICU 2.4
421 */
422U_CAPI UBool U_EXPORT2
423uset_isEmpty(const USet* set);
424
425/**
426 * Returns true if the given USet contains the given character.
427 * This function works faster with a frozen set.
428 * @param set the set
429 * @param c The codepoint to check for within the set
430 * @return true if set contains c
431 * @stable ICU 2.4
432 */
433U_CAPI UBool U_EXPORT2
434uset_contains(const USet* set, UChar32 c);
435
436/**
437 * Returns true if the given USet contains the given string.
438 * @param set the set
439 * @param str the string
440 * @param strLen the length of the string or -1 if null terminated.
441 * @return true if set contains str
442 * @stable ICU 2.4
443 */
444U_CAPI UBool U_EXPORT2
445uset_containsString(const USet* set, const UChar* str, int32_t strLen);
446/**
447 * Returns the number of characters and strings contained in this set.
448 * The last (uset_getItemCount() - uset_getRangeCount()) items are strings.
449 *
450 * This is slower than uset_getRangeCount() and uset_getItemCount() because
451 * it counts the code points of all ranges.
452 *
453 * @param set the set
454 * @return a non-negative integer counting the characters and strings
455 * contained in set
456 * @stable ICU 2.4
457 * @see uset_getRangeCount
458 */
459U_CAPI int32_t U_EXPORT2
460uset_size(const USet* set);
461
462/**
463 * @param set the set
464 * @return the number of ranges in this set.
465 * @stable ICU 70
466 * @see uset_getItemCount
467 * @see uset_getItem
468 * @see uset_size
469 */
470U_CAPI int32_t U_EXPORT2
471uset_getRangeCount(const USet *set);
472
473/**
474 * Returns the number of items in this set.  An item is either a range
475 * of characters or a single multicharacter string.
476 * @param set the set
477 * @return a non-negative integer counting the character ranges
478 * and/or strings contained in set
479 * @stable ICU 2.4
480 */
481U_CAPI int32_t U_EXPORT2
482uset_getItemCount(const USet* set);
483
484/**
485 * Returns an item of this set.  An item is either a range of
486 * characters or a single multicharacter string (which can be the empty string).
487 *
488 * If <code>itemIndex</code> is less than uset_getRangeCount(), then this function returns 0,
489 * and the range is <code>*start</code>..<code>*end</code>.
490 *
491 * If <code>itemIndex</code> is at least uset_getRangeCount() and less than uset_getItemCount(), then
492 * this function copies the string into <code>str[strCapacity]</code> and
493 * returns the length of the string (0 for the empty string).
494 *
495 * If <code>itemIndex</code> is out of range, then this function returns -1.
496 *
497 * Note that 0 is returned for each range as well as for the empty string.
498 *
499 * @param set the set
500 * @param itemIndex a non-negative integer in the range 0..uset_getItemCount(set)-1
501 * @param start pointer to variable to receive first character in range, inclusive;
502 *              can be NULL for a string item
503 * @param end pointer to variable to receive last character in range, inclusive;
504 *            can be NULL for a string item
505 * @param str buffer to receive the string, may be NULL
506 * @param strCapacity capacity of str, or 0 if str is NULL
507 * @param ec error code; U_INDEX_OUTOFBOUNDS_ERROR if the itemIndex is out of range
508 * @return the length of the string (0 or >= 2), or 0 if the item is a range,
509 *         or -1 if the itemIndex is out of range
510 * @stable ICU 2.4
511 */
512U_CAPI int32_t U_EXPORT2
513uset_getItem(const USet* set, int32_t itemIndex,
514             UChar32* start, UChar32* end,
515             UChar* str, int32_t strCapacity,
516             UErrorCode* ec);
517#endif
518