1// © 2016 and later: Unicode, Inc. and others.
2// License & terms of use: http://www.unicode.org/copyright.html
3/*
4******************************************************************************
5* Copyright (C) 1996-2015, International Business Machines Corporation and others.
6* All Rights Reserved.
7******************************************************************************
8*/
9
10#ifndef UBRK_H
11#define UBRK_H
12
13#include "unicode/utypes.h"
14#include "unicode/uloc.h"
15
16#if U_SHOW_CPLUSPLUS_API
17#include "unicode/localpointer.h"
18#endif   // U_SHOW_CPLUSPLUS_API
19
20/**
21 * A text-break iterator.
22 *  For usage in C programs.
23 */
24#ifndef UBRK_TYPEDEF_UBREAK_ITERATOR
25#   define UBRK_TYPEDEF_UBREAK_ITERATOR
26    /**
27     *  Opaque type representing an ICU Break iterator object.
28     *  @stable ICU 2.0
29     */
30    typedef struct UBreakIterator UBreakIterator;
31#endif
32
33#include "unicode/parseerr.h"
34
35#if !UCONFIG_NO_BREAK_ITERATION
36/**
37 * \file
38 * \brief C API: BreakIterator
39 *
40 * <h2> BreakIterator C API </h2>
41 *
42 * The BreakIterator C API defines  methods for finding the location
43 * of boundaries in text. Pointer to a UBreakIterator maintain a
44 * current position and scan over text returning the index of characters
45 * where boundaries occur.
46 * <p>
47 * Line boundary analysis determines where a text string can be broken
48 * when line-wrapping. The mechanism correctly handles punctuation and
49 * hyphenated words.
50 * <p>
51 * Note: The locale keyword "lb" can be used to modify line break
52 * behavior according to the CSS level 3 line-break options, see
53 * <http://dev.w3.org/csswg/css-text/#line-breaking>. For example:
54 * "ja@lb=strict", "zh@lb=loose".
55 * <p>
56 * Sentence boundary analysis allows selection with correct
57 * interpretation of periods within numbers and abbreviations, and
58 * trailing punctuation marks such as quotation marks and parentheses.
59 * <p>
60 * Note: The locale keyword "ss" can be used to enable use of
61 * segmentation suppression data (preventing breaks in English after
62 * abbreviations such as "Mr." or "Est.", for example), as follows:
63 * "en@ss=standard".
64 * <p>
65 * Word boundary analysis is used by search and replace functions, as
66 * well as within text editing applications that allow the user to
67 * select words with a double click. Word selection provides correct
68 * interpretation of punctuation marks within and following
69 * words. Characters that are not part of a word, such as symbols or
70 * punctuation marks, have word-breaks on both sides.
71 * <p>
72 * Character boundary analysis identifies the boundaries of
73 * "Extended Grapheme Clusters", which are groupings of codepoints
74 * that should be treated as character-like units for many text operations.
75 * Please see Unicode Standard Annex #29, Unicode Text Segmentation,
76 * http://www.unicode.org/reports/tr29/ for additional information
77 * on grapheme clusters and guidelines on their use.
78 * <p>
79 * Title boundary analysis locates all positions,
80 * typically starts of words, that should be set to Title Case
81 * when title casing the text.
82 * <p>
83 * The text boundary positions are found according to the rules
84 * described in Unicode Standard Annex #29, Text Boundaries, and
85 * Unicode Standard Annex #14, Line Breaking Properties.  These
86 * are available at http://www.unicode.org/reports/tr14/ and
87 * http://www.unicode.org/reports/tr29/.
88 * <p>
89 * In addition to the plain C API defined in this header file, an
90 * object oriented C++ API with equivalent functionality is defined in the
91 * file brkiter.h.
92 * <p>
93 * Code snippets illustrating the use of the Break Iterator APIs
94 * are available in the ICU User Guide,
95 * https://unicode-org.github.io/icu/userguide/boundaryanalysis/
96 * and in the sample program icu/source/samples/break/break.cpp
97 */
98
99/** The possible types of text boundaries.  @stable ICU 2.0 */
100typedef enum UBreakIteratorType {
101  /** Character breaks  @stable ICU 2.0 */
102  UBRK_CHARACTER = 0,
103  /** Word breaks @stable ICU 2.0 */
104  UBRK_WORD = 1,
105  /** Line breaks @stable ICU 2.0 */
106  UBRK_LINE = 2,
107  /** Sentence breaks @stable ICU 2.0 */
108  UBRK_SENTENCE = 3,
109} UBreakIteratorType;
110
111/** Value indicating all text boundaries have been returned.
112 *  @stable ICU 2.0
113 */
114#define UBRK_DONE ((int32_t) -1)
115
116
117/**
118 *  Enum constants for the word break tags returned by
119 *  getRuleStatus().  A range of values is defined for each category of
120 *  word, to allow for further subdivisions of a category in future releases.
121 *  Applications should check for tag values falling within the range, rather
122 *  than for single individual values.
123 *
124 * The numeric values of all of these constants are stable (will not change).
125 *
126 * @stable ICU 2.2
127*/
128typedef enum UWordBreak {
129    /** Tag value for "words" that do not fit into any of other categories.
130     *  Includes spaces and most punctuation. */
131    UBRK_WORD_NONE           = 0,
132    /** Upper bound for tags for uncategorized words. */
133    UBRK_WORD_NONE_LIMIT     = 100,
134    /** Tag value for words that appear to be numbers, lower limit.    */
135    UBRK_WORD_NUMBER         = 100,
136    /** Tag value for words that appear to be numbers, upper limit.    */
137    UBRK_WORD_NUMBER_LIMIT   = 200,
138    /** Tag value for words that contain letters, excluding
139     *  hiragana, katakana or ideographic characters, lower limit.    */
140    UBRK_WORD_LETTER         = 200,
141    /** Tag value for words containing letters, upper limit  */
142    UBRK_WORD_LETTER_LIMIT   = 300,
143    /** Tag value for words containing kana characters, lower limit */
144    UBRK_WORD_KANA           = 300,
145    /** Tag value for words containing kana characters, upper limit */
146    UBRK_WORD_KANA_LIMIT     = 400,
147    /** Tag value for words containing ideographic characters, lower limit */
148    UBRK_WORD_IDEO           = 400,
149    /** Tag value for words containing ideographic characters, upper limit */
150    UBRK_WORD_IDEO_LIMIT     = 500
151} UWordBreak;
152
153/**
154 *  Enum constants for the line break tags returned by getRuleStatus().
155 *  A range of values is defined for each category of
156 *  word, to allow for further subdivisions of a category in future releases.
157 *  Applications should check for tag values falling within the range, rather
158 *  than for single individual values.
159 *
160 * The numeric values of all of these constants are stable (will not change).
161 *
162 * @stable ICU 2.8
163*/
164typedef enum ULineBreakTag {
165    /** Tag value for soft line breaks, positions at which a line break
166      *  is acceptable but not required                */
167    UBRK_LINE_SOFT            = 0,
168    /** Upper bound for soft line breaks.              */
169    UBRK_LINE_SOFT_LIMIT      = 100,
170    /** Tag value for a hard, or mandatory line break  */
171    UBRK_LINE_HARD            = 100,
172    /** Upper bound for hard line breaks.              */
173    UBRK_LINE_HARD_LIMIT      = 200
174} ULineBreakTag;
175
176
177
178/**
179 *  Enum constants for the sentence break tags returned by getRuleStatus().
180 *  A range of values is defined for each category of
181 *  sentence, to allow for further subdivisions of a category in future releases.
182 *  Applications should check for tag values falling within the range, rather
183 *  than for single individual values.
184 *
185 * The numeric values of all of these constants are stable (will not change).
186 *
187 * @stable ICU 2.8
188*/
189typedef enum USentenceBreakTag {
190    /** Tag value for for sentences  ending with a sentence terminator
191      * ('.', '?', '!', etc.) character, possibly followed by a
192      * hard separator (CR, LF, PS, etc.)
193      */
194    UBRK_SENTENCE_TERM       = 0,
195    /** Upper bound for tags for sentences ended by sentence terminators.    */
196    UBRK_SENTENCE_TERM_LIMIT = 100,
197    /** Tag value for for sentences that do not contain an ending
198      * sentence terminator ('.', '?', '!', etc.) character, but
199      * are ended only by a hard separator (CR, LF, PS, etc.) or end of input.
200      */
201    UBRK_SENTENCE_SEP        = 100,
202    /** Upper bound for tags for sentences ended by a separator.              */
203    UBRK_SENTENCE_SEP_LIMIT  = 200
204    /** Tag value for a hard, or mandatory line break  */
205} USentenceBreakTag;
206
207
208/**
209 * Open a new UBreakIterator for locating text boundaries for a specified locale.
210 * A UBreakIterator may be used for detecting character, line, word,
211 * and sentence breaks in text.
212 * @param type The type of UBreakIterator to open: one of UBRK_CHARACTER, UBRK_WORD,
213 * UBRK_LINE, UBRK_SENTENCE
214 * @param locale The locale specifying the text-breaking conventions. Note that
215 * locale keys such as "lb" and "ss" may be used to modify text break behavior,
216 * see general discussion of BreakIterator C API.
217 * @param text The text to be iterated over. May be null, in which case ubrk_setText() is
218 *        used to specify the text to be iterated.
219 * @param textLength The number of characters in text, or -1 if null-terminated.
220 * @param status A UErrorCode to receive any errors.
221 * @return A UBreakIterator for the specified locale.
222 * @see ubrk_openRules
223 * @stable ICU 2.0
224 */
225U_CAPI UBreakIterator* U_EXPORT2
226ubrk_open(UBreakIteratorType type,
227      const char *locale,
228      const UChar *text,
229      int32_t textLength,
230      UErrorCode *status);
231
232/**
233 * Open a new UBreakIterator for locating text boundaries using specified breaking rules.
234 * The rule syntax is ... (TBD)
235 * @param rules A set of rules specifying the text breaking conventions.
236 * @param rulesLength The number of characters in rules, or -1 if null-terminated.
237 * @param text The text to be iterated over.  May be null, in which case ubrk_setText() is
238 *        used to specify the text to be iterated.
239 * @param textLength The number of characters in text, or -1 if null-terminated.
240 * @param parseErr   Receives position and context information for any syntax errors
241 *                   detected while parsing the rules.
242 * @param status A UErrorCode to receive any errors.
243 * @return A UBreakIterator for the specified rules.
244 * @see ubrk_open
245 * @stable ICU 2.2
246 */
247U_CAPI UBreakIterator* U_EXPORT2
248ubrk_openRules(const UChar     *rules,
249               int32_t         rulesLength,
250               const UChar     *text,
251               int32_t          textLength,
252               UParseError     *parseErr,
253               UErrorCode      *status);
254
255/**
256 * Open a new UBreakIterator for locating text boundaries using precompiled binary rules.
257 * Opening a UBreakIterator this way is substantially faster than using ubrk_openRules.
258 * Binary rules may be obtained using ubrk_getBinaryRules. The compiled rules are not
259 * compatible across different major versions of ICU, nor across platforms of different
260 * endianness or different base character set family (ASCII vs EBCDIC).
261 * @param binaryRules A set of compiled binary rules specifying the text breaking
262 *                    conventions. Ownership of the storage containing the compiled
263 *                    rules remains with the caller of this function. The compiled
264 *                    rules must not be modified or deleted during the life of the
265 *                    break iterator.
266 * @param rulesLength The length of binaryRules in bytes; must be >= 0.
267 * @param text        The text to be iterated over.  May be null, in which case
268 *                    ubrk_setText() is used to specify the text to be iterated.
269 * @param textLength  The number of characters in text, or -1 if null-terminated.
270 * @param status      Pointer to UErrorCode to receive any errors.
271 * @return            UBreakIterator for the specified rules.
272 * @see ubrk_getBinaryRules
273 * @stable ICU 59
274 */
275U_CAPI UBreakIterator* U_EXPORT2
276ubrk_openBinaryRules(const uint8_t *binaryRules, int32_t rulesLength,
277                     const UChar *  text, int32_t textLength,
278                     UErrorCode *   status);
279
280#ifndef U_HIDE_DEPRECATED_API
281
282#endif /* U_HIDE_DEPRECATED_API */
283
284/**
285 * Thread safe cloning operation.
286 * @param bi iterator to be cloned
287 * @param status to indicate whether the operation went on smoothly or there were errors
288 * @return pointer to the new clone
289 * @stable ICU 69
290 */
291U_CAPI UBreakIterator * U_EXPORT2
292ubrk_clone(const UBreakIterator *bi,
293           UErrorCode *status);
294
295/**
296* Close a UBreakIterator.
297* Once closed, a UBreakIterator may no longer be used.
298* @param bi The break iterator to close.
299 * @stable ICU 2.0
300*/
301U_CAPI void U_EXPORT2
302ubrk_close(UBreakIterator *bi);
303
304#if U_SHOW_CPLUSPLUS_API
305
306U_NAMESPACE_BEGIN
307
308/**
309 * \class LocalUBreakIteratorPointer
310 * "Smart pointer" class, closes a UBreakIterator via ubrk_close().
311 * For most methods see the LocalPointerBase base class.
312 *
313 * @see LocalPointerBase
314 * @see LocalPointer
315 * @stable ICU 4.4
316 */
317U_DEFINE_LOCAL_OPEN_POINTER(LocalUBreakIteratorPointer, UBreakIterator, ubrk_close);
318
319U_NAMESPACE_END
320
321#endif
322
323/**
324 * Sets an existing iterator to point to a new piece of text.
325 * The break iterator retains a pointer to the supplied text.
326 * The caller must not modify or delete the text while the BreakIterator
327 * retains the reference.
328 *
329 * @param bi The iterator to use
330 * @param text The text to be set
331 * @param textLength The length of the text
332 * @param status The error code
333 * @stable ICU 2.0
334 */
335U_CAPI void U_EXPORT2
336ubrk_setText(UBreakIterator* bi,
337             const UChar*    text,
338             int32_t         textLength,
339             UErrorCode*     status);
340
341/**
342 * Determine the most recently-returned text boundary.
343 *
344 * @param bi The break iterator to use.
345 * @return The character index most recently returned by \ref ubrk_next, \ref ubrk_previous,
346 * \ref ubrk_first, or \ref ubrk_last.
347 * @stable ICU 2.0
348 */
349U_CAPI int32_t U_EXPORT2
350ubrk_current(const UBreakIterator *bi);
351
352/**
353 * Advance the iterator to the boundary following the current boundary.
354 *
355 * @param bi The break iterator to use.
356 * @return The character index of the next text boundary, or UBRK_DONE
357 * if all text boundaries have been returned.
358 * @see ubrk_previous
359 * @stable ICU 2.0
360 */
361U_CAPI int32_t U_EXPORT2
362ubrk_next(UBreakIterator *bi);
363
364/**
365 * Set the iterator position to the boundary preceding the current boundary.
366 *
367 * @param bi The break iterator to use.
368 * @return The character index of the preceding text boundary, or UBRK_DONE
369 * if all text boundaries have been returned.
370 * @see ubrk_next
371 * @stable ICU 2.0
372 */
373U_CAPI int32_t U_EXPORT2
374ubrk_previous(UBreakIterator *bi);
375
376/**
377 * Set the iterator position to zero, the start of the text being scanned.
378 * @param bi The break iterator to use.
379 * @return The new iterator position (zero).
380 * @see ubrk_last
381 * @stable ICU 2.0
382 */
383U_CAPI int32_t U_EXPORT2
384ubrk_first(UBreakIterator *bi);
385
386/**
387 * Set the iterator position to the index immediately <EM>beyond</EM> the last character in the text being scanned.
388 * This is not the same as the last character.
389 * @param bi The break iterator to use.
390 * @return The character offset immediately <EM>beyond</EM> the last character in the
391 * text being scanned.
392 * @see ubrk_first
393 * @stable ICU 2.0
394 */
395U_CAPI int32_t U_EXPORT2
396ubrk_last(UBreakIterator *bi);
397
398/**
399 * Set the iterator position to the first boundary preceding the specified offset.
400 * The new position is always smaller than offset, or UBRK_DONE.
401 * @param bi The break iterator to use.
402 * @param offset The offset to begin scanning.
403 * @return The text boundary preceding offset, or UBRK_DONE.
404 * @see ubrk_following
405 * @stable ICU 2.0
406 */
407U_CAPI int32_t U_EXPORT2
408ubrk_preceding(UBreakIterator *bi,
409           int32_t offset);
410
411/**
412 * Advance the iterator to the first boundary following the specified offset.
413 * The value returned is always greater than offset, or UBRK_DONE.
414 * @param bi The break iterator to use.
415 * @param offset The offset to begin scanning.
416 * @return The text boundary following offset, or UBRK_DONE.
417 * @see ubrk_preceding
418 * @stable ICU 2.0
419 */
420U_CAPI int32_t U_EXPORT2
421ubrk_following(UBreakIterator *bi,
422           int32_t offset);
423
424/**
425* Get a locale for which text breaking information is available.
426* A UBreakIterator in a locale returned by this function will perform the correct
427* text breaking for the locale.
428* @param index The index of the desired locale.
429* @return A locale for which number text breaking information is available, or 0 if none.
430* @see ubrk_countAvailable
431* @stable ICU 2.0
432*/
433U_CAPI const char* U_EXPORT2
434ubrk_getAvailable(int32_t index);
435
436/**
437* Determine how many locales have text breaking information available.
438* This function is most useful as determining the loop ending condition for
439* calls to \ref ubrk_getAvailable.
440* @return The number of locales for which text breaking information is available.
441* @see ubrk_getAvailable
442* @stable ICU 2.0
443*/
444U_CAPI int32_t U_EXPORT2
445ubrk_countAvailable(void);
446
447
448/**
449* Returns true if the specified position is a boundary position.  As a side
450* effect, leaves the iterator pointing to the first boundary position at
451* or after "offset".
452* @param bi The break iterator to use.
453* @param offset the offset to check.
454* @return True if "offset" is a boundary position.
455* @stable ICU 2.0
456*/
457U_CAPI  UBool U_EXPORT2
458ubrk_isBoundary(UBreakIterator *bi, int32_t offset);
459
460/**
461 * Return the status from the break rule that determined the most recently
462 * returned break position.  The values appear in the rule source
463 * within brackets, {123}, for example.  For rules that do not specify a
464 * status, a default value of 0 is returned.
465 * <p>
466 * For word break iterators, the possible values are defined in enum UWordBreak.
467 * @stable ICU 2.2
468 */
469U_CAPI  int32_t U_EXPORT2
470ubrk_getRuleStatus(UBreakIterator *bi);
471
472/**
473 * Get the statuses from the break rules that determined the most recently
474 * returned break position.  The values appear in the rule source
475 * within brackets, {123}, for example.  The default status value for rules
476 * that do not explicitly provide one is zero.
477 * <p>
478 * For word break iterators, the possible values are defined in enum UWordBreak.
479 * @param bi        The break iterator to use
480 * @param fillInVec an array to be filled in with the status values.
481 * @param capacity  the length of the supplied vector.  A length of zero causes
482 *                  the function to return the number of status values, in the
483 *                  normal way, without attempting to store any values.
484 * @param status    receives error codes.
485 * @return          The number of rule status values from rules that determined
486 *                  the most recent boundary returned by the break iterator.
487 * @stable ICU 3.0
488 */
489U_CAPI  int32_t U_EXPORT2
490ubrk_getRuleStatusVec(UBreakIterator *bi, int32_t *fillInVec, int32_t capacity, UErrorCode *status);
491
492/**
493 * Return the locale of the break iterator. You can choose between the valid and
494 * the actual locale.
495 * @param bi break iterator
496 * @param type locale type (valid or actual)
497 * @param status error code
498 * @return locale string
499 * @stable ICU 2.8
500 */
501U_CAPI const char* U_EXPORT2
502ubrk_getLocaleByType(const UBreakIterator *bi, ULocDataLocaleType type, UErrorCode* status);
503
504/**
505 * Get a compiled binary version of the rules specifying the behavior of a UBreakIterator.
506 * The binary rules may be used with ubrk_openBinaryRules to open a new UBreakIterator
507 * more quickly than using ubrk_openRules. The compiled rules are not compatible across
508 * different major versions of ICU, nor across platforms of different endianness or
509 * different base character set family (ASCII vs EBCDIC). Supports preflighting (with
510 * binaryRules=NULL and rulesCapacity=0) to get the rules length without copying them to
511 * the binaryRules buffer. However, whether preflighting or not, if the actual length
512 * is greater than INT32_MAX, then the function returns 0 and sets *status to
513 * U_INDEX_OUTOFBOUNDS_ERROR.
514
515 * @param bi            The break iterator to use.
516 * @param binaryRules   Buffer to receive the compiled binary rules; set to NULL for
517 *                      preflighting.
518 * @param rulesCapacity Capacity (in bytes) of the binaryRules buffer; set to 0 for
519 *                      preflighting. Must be >= 0.
520 * @param status        Pointer to UErrorCode to receive any errors, such as
521 *                      U_BUFFER_OVERFLOW_ERROR, U_INDEX_OUTOFBOUNDS_ERROR, or
522 *                      U_ILLEGAL_ARGUMENT_ERROR.
523 * @return              The actual byte length of the binary rules, if <= INT32_MAX;
524 *                      otherwise 0. If not preflighting and this is larger than
525 *                      rulesCapacity, *status will be set to an error.
526 * @see ubrk_openBinaryRules
527 * @stable ICU 59
528 */
529U_CAPI int32_t U_EXPORT2
530ubrk_getBinaryRules(UBreakIterator *bi,
531                    uint8_t *       binaryRules, int32_t rulesCapacity,
532                    UErrorCode *    status);
533
534#endif /* #if !UCONFIG_NO_BREAK_ITERATION */
535
536#endif
537