1 // Copyright (C) 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 ********************************************************************************
5 * Copyright (C) 1997-2016, International Business Machines
6 * Corporation and others. All Rights Reserved.
7 ********************************************************************************
11 * Modification History:
13 * Date Name Description
14 * 02/18/97 aliu Added typedef for TextCount. Made DONE const.
15 * 05/07/97 aliu Fixed DLL declaration.
16 * 07/09/97 jfitz Renamed BreakIterator and interface synced with JDK
17 * 08/11/98 helena Sync-up JDK1.2.
18 * 01/13/2000 helena Added UErrorCode parameter to createXXXInstance methods.
19 ********************************************************************************
25 #include "unicode/utypes.h"
29 * \brief C++ API: Break Iterator.
32 #if UCONFIG_NO_BREAK_ITERATION
37 * Allow the declaration of APIs with pointers to BreakIterator
38 * even when break iteration is removed from the build.
46 #include "unicode/uobject.h"
47 #include "unicode/unistr.h"
48 #include "unicode/chariter.h"
49 #include "unicode/locid.h"
50 #include "unicode/ubrk.h"
51 #include "unicode/strenum.h"
52 #include "unicode/utext.h"
53 #include "unicode/umisc.h"
58 * The BreakIterator class implements methods for finding the location
59 * of boundaries in text. BreakIterator is an abstract base class.
60 * Instances of BreakIterator maintain a current position and scan over
61 * text returning the index of characters where boundaries occur.
63 * Line boundary analysis determines where a text string can be broken
64 * when line-wrapping. The mechanism correctly handles punctuation and
67 * Sentence boundary analysis allows selection with correct
68 * interpretation of periods within numbers and abbreviations, and
69 * trailing punctuation marks such as quotation marks and parentheses.
71 * Word boundary analysis is used by search and replace functions, as
72 * well as within text editing applications that allow the user to
73 * select words with a double click. Word selection provides correct
74 * interpretation of punctuation marks within and following
75 * words. Characters that are not part of a word, such as symbols or
76 * punctuation marks, have word-breaks on both sides.
78 * Character boundary analysis allows users to interact with
79 * characters as they expect to, for example, when moving the cursor
80 * through a text string. Character boundary analysis provides correct
81 * navigation of through character strings, regardless of how the
82 * character is stored. For example, an accented character might be
83 * stored as a base character and a diacritical mark. What users
84 * consider to be a character can differ between languages.
86 * The text boundary positions are found according to the rules
87 * described in Unicode Standard Annex #29, Text Boundaries, and
88 * Unicode Standard Annex #14, Line Breaking Properties. These
89 * are available at http://www.unicode.org/reports/tr14/ and
90 * http://www.unicode.org/reports/tr29/.
92 * In addition to the C++ API defined in this header file, a
93 * plain C API with equivalent functionality is defined in the
96 * Code snippets illustrating the use of the Break Iterator APIs
97 * are available in the ICU User Guide,
98 * http://icu-project.org/userguide/boundaryAnalysis.html
99 * and in the sample program icu/source/samples/break/break.cpp
102 class U_COMMON_API BreakIterator : public UObject {
108 virtual ~BreakIterator();
111 * Return true if another object is semantically equal to this
112 * one. The other object should be an instance of the same subclass of
113 * BreakIterator. Objects of different subclasses are considered
116 * Return true if this BreakIterator is at the same position in the
117 * same text, and is the same class and type (word, line, etc.) of
118 * BreakIterator, as the argument. Text is considered the same if
119 * it contains the same characters, it need not be the same
120 * object, and styles are not considered.
123 virtual UBool operator==(const BreakIterator&) const = 0;
126 * Returns the complement of the result of operator==
127 * @param rhs The BreakIterator to be compared for inequality
128 * @return the complement of the result of operator==
131 UBool operator!=(const BreakIterator& rhs) const { return !operator==(rhs); }
134 * Return a polymorphic copy of this object. This is an abstract
135 * method which subclasses implement.
138 virtual BreakIterator* clone(void) const = 0;
141 * Return a polymorphic class ID for this object. Different subclasses
142 * will return distinct unequal values.
145 virtual UClassID getDynamicClassID(void) const = 0;
148 * Return a CharacterIterator over the text being analyzed.
151 virtual CharacterIterator& getText(void) const = 0;
155 * Get a UText for the text being analyzed.
156 * The returned UText is a shallow clone of the UText used internally
157 * by the break iterator implementation. It can safely be used to
158 * access the text without impacting any break iterator operations,
159 * but the underlying text itself must not be altered.
161 * @param fillIn A UText to be filled in. If NULL, a new UText will be
162 * allocated to hold the result.
163 * @param status receives any error codes.
164 * @return The current UText for this break iterator. If an input
165 * UText was provided, it will always be returned.
168 virtual UText *getUText(UText *fillIn, UErrorCode &status) const = 0;
171 * Change the text over which this operates. The text boundary is
172 * reset to the start.
174 * The BreakIterator will retain a reference to the supplied string.
175 * The caller must not modify or delete the text while the BreakIterator
176 * retains the reference.
178 * @param text The UnicodeString used to change the text.
181 virtual void setText(const UnicodeString &text) = 0;
184 * Reset the break iterator to operate over the text represented by
185 * the UText. The iterator position is reset to the start.
187 * This function makes a shallow clone of the supplied UText. This means
188 * that the caller is free to immediately close or otherwise reuse the
189 * Utext that was passed as a parameter, but that the underlying text itself
190 * must not be altered while being referenced by the break iterator.
192 * All index positions returned by break iterator functions are
193 * native indices from the UText. For example, when breaking UTF-8
194 * encoded text, the break positions returned by next(), previous(), etc.
195 * will be UTF-8 string indices, not UTF-16 positions.
197 * @param text The UText used to change the text.
198 * @param status receives any error codes.
201 virtual void setText(UText *text, UErrorCode &status) = 0;
204 * Change the text over which this operates. The text boundary is
205 * reset to the start.
206 * Note that setText(UText *) provides similar functionality to this function,
207 * and is more efficient.
208 * @param it The CharacterIterator used to change the text.
211 virtual void adoptText(CharacterIterator* it) = 0;
215 * DONE is returned by previous() and next() after all valid
216 * boundaries have been returned.
223 * Sets the current iteration position to the beginning of the text, position zero.
224 * @return The offset of the beginning of the text, zero.
227 virtual int32_t first(void) = 0;
230 * Set the iterator position to the index immediately BEYOND the last character in the text being scanned.
231 * @return The index immediately BEYOND the last character in the text being scanned.
234 virtual int32_t last(void) = 0;
237 * Set the iterator position to the boundary preceding the current boundary.
238 * @return The character index of the previous text boundary or DONE if all
239 * boundaries have been returned.
242 virtual int32_t previous(void) = 0;
245 * Advance the iterator to the boundary following the current boundary.
246 * @return The character index of the next text boundary or DONE if all
247 * boundaries have been returned.
250 virtual int32_t next(void) = 0;
253 * Return character index of the current interator position within the text.
254 * @return The boundary most recently returned.
257 virtual int32_t current(void) const = 0;
260 * Advance the iterator to the first boundary following the specified offset.
261 * The value returned is always greater than the offset or
262 * the value BreakIterator.DONE
263 * @param offset the offset to begin scanning.
264 * @return The first boundary after the specified offset.
267 virtual int32_t following(int32_t offset) = 0;
270 * Set the iterator position to the first boundary preceding the specified offset.
271 * The value returned is always smaller than the offset or
272 * the value BreakIterator.DONE
273 * @param offset the offset to begin scanning.
274 * @return The first boundary before the specified offset.
277 virtual int32_t preceding(int32_t offset) = 0;
280 * Return true if the specfied position is a boundary position.
281 * As a side effect, the current position of the iterator is set
282 * to the first boundary position at or following the specified offset.
283 * @param offset the offset to check.
284 * @return True if "offset" is a boundary position.
287 virtual UBool isBoundary(int32_t offset) = 0;
290 * Set the iterator position to the nth boundary from the current boundary
291 * @param n the number of boundaries to move by. A value of 0
292 * does nothing. Negative values move to previous boundaries
293 * and positive values move to later boundaries.
294 * @return The new iterator position, or
295 * DONE if there are fewer than |n| boundaries in the specfied direction.
298 virtual int32_t next(int32_t n) = 0;
301 * For RuleBasedBreakIterators, return the status tag from the
302 * break rule that determined the most recently
303 * returned break position.
305 * For break iterator types that do not support a rule status,
306 * a default value of 0 is returned.
308 * @return the status from the break rule that determined the most recently
309 * returned break position.
310 * @see RuleBaseBreakIterator::getRuleStatus()
314 virtual int32_t getRuleStatus() const;
317 * For RuleBasedBreakIterators, get the status (tag) values from the break rule(s)
318 * that determined the most recently returned break position.
320 * For break iterator types that do not support rule status,
321 * no values are returned.
323 * The returned status value(s) are stored into an array provided by the caller.
324 * The values are stored in sorted (ascending) order.
325 * If the capacity of the output array is insufficient to hold the data,
326 * the output will be truncated to the available length, and a
327 * U_BUFFER_OVERFLOW_ERROR will be signaled.
329 * @see RuleBaseBreakIterator::getRuleStatusVec
331 * @param fillInVec an array to be filled in with the status values.
332 * @param capacity the length of the supplied vector. A length of zero causes
333 * the function to return the number of status values, in the
334 * normal way, without attemtping to store any values.
335 * @param status receives error codes.
336 * @return The number of rule status values from rules that determined
337 * the most recent boundary returned by the break iterator.
338 * In the event of a U_BUFFER_OVERFLOW_ERROR, the return value
339 * is the total number of status values that were available,
340 * not the reduced number that were actually returned.
344 virtual int32_t getRuleStatusVec(int32_t *fillInVec, int32_t capacity, UErrorCode &status);
347 * Create BreakIterator for word-breaks using the given locale.
348 * Returns an instance of a BreakIterator implementing word breaks.
349 * WordBreak is useful for word selection (ex. double click)
350 * @param where the locale.
351 * @param status the error code
352 * @return A BreakIterator for word-breaks. The UErrorCode& status
353 * parameter is used to return status information to the user.
354 * To check whether the construction succeeded or not, you should check
355 * the value of U_SUCCESS(err). If you wish more detailed information, you
356 * can check for informational error results which still indicate success.
357 * U_USING_FALLBACK_WARNING indicates that a fall back locale was used. For
358 * example, 'de_CH' was requested, but nothing was found there, so 'de' was
359 * used. U_USING_DEFAULT_WARNING indicates that the default locale data was
360 * used; neither the requested locale nor any of its fall back locales
362 * The caller owns the returned object and is responsible for deleting it.
365 static BreakIterator* U_EXPORT2
366 createWordInstance(const Locale& where, UErrorCode& status);
369 * Create BreakIterator for line-breaks using specified locale.
370 * Returns an instance of a BreakIterator implementing line breaks. Line
371 * breaks are logically possible line breaks, actual line breaks are
372 * usually determined based on display width.
373 * LineBreak is useful for word wrapping text.
374 * @param where the locale.
375 * @param status The error code.
376 * @return A BreakIterator for line-breaks. The UErrorCode& status
377 * parameter is used to return status information to the user.
378 * To check whether the construction succeeded or not, you should check
379 * the value of U_SUCCESS(err). If you wish more detailed information, you
380 * can check for informational error results which still indicate success.
381 * U_USING_FALLBACK_WARNING indicates that a fall back locale was used. For
382 * example, 'de_CH' was requested, but nothing was found there, so 'de' was
383 * used. U_USING_DEFAULT_WARNING indicates that the default locale data was
384 * used; neither the requested locale nor any of its fall back locales
386 * The caller owns the returned object and is responsible for deleting it.
389 static BreakIterator* U_EXPORT2
390 createLineInstance(const Locale& where, UErrorCode& status);
393 * Create BreakIterator for character-breaks using specified locale
394 * Returns an instance of a BreakIterator implementing character breaks.
395 * Character breaks are boundaries of combining character sequences.
396 * @param where the locale.
397 * @param status The error code.
398 * @return A BreakIterator for character-breaks. The UErrorCode& status
399 * parameter is used to return status information to the user.
400 * To check whether the construction succeeded or not, you should check
401 * the value of U_SUCCESS(err). If you wish more detailed information, you
402 * can check for informational error results which still indicate success.
403 * U_USING_FALLBACK_WARNING indicates that a fall back locale was used. For
404 * example, 'de_CH' was requested, but nothing was found there, so 'de' was
405 * used. U_USING_DEFAULT_WARNING indicates that the default locale data was
406 * used; neither the requested locale nor any of its fall back locales
408 * The caller owns the returned object and is responsible for deleting it.
411 static BreakIterator* U_EXPORT2
412 createCharacterInstance(const Locale& where, UErrorCode& status);
415 * Create BreakIterator for sentence-breaks using specified locale
416 * Returns an instance of a BreakIterator implementing sentence breaks.
417 * @param where the locale.
418 * @param status The error code.
419 * @return A BreakIterator for sentence-breaks. The UErrorCode& status
420 * parameter is used to return status information to the user.
421 * To check whether the construction succeeded or not, you should check
422 * the value of U_SUCCESS(err). If you wish more detailed information, you
423 * can check for informational error results which still indicate success.
424 * U_USING_FALLBACK_WARNING indicates that a fall back locale was used. For
425 * example, 'de_CH' was requested, but nothing was found there, so 'de' was
426 * used. U_USING_DEFAULT_WARNING indicates that the default locale data was
427 * used; neither the requested locale nor any of its fall back locales
429 * The caller owns the returned object and is responsible for deleting it.
432 static BreakIterator* U_EXPORT2
433 createSentenceInstance(const Locale& where, UErrorCode& status);
436 * Create BreakIterator for title-casing breaks using the specified locale
437 * Returns an instance of a BreakIterator implementing title breaks.
438 * The iterator returned locates title boundaries as described for
439 * Unicode 3.2 only. For Unicode 4.0 and above title boundary iteration,
440 * please use Word Boundary iterator.{@link #createWordInstance }
442 * @param where the locale.
443 * @param status The error code.
444 * @return A BreakIterator for title-breaks. The UErrorCode& status
445 * parameter is used to return status information to the user.
446 * To check whether the construction succeeded or not, you should check
447 * the value of U_SUCCESS(err). If you wish more detailed information, you
448 * can check for informational error results which still indicate success.
449 * U_USING_FALLBACK_WARNING indicates that a fall back locale was used. For
450 * example, 'de_CH' was requested, but nothing was found there, so 'de' was
451 * used. U_USING_DEFAULT_WARNING indicates that the default locale data was
452 * used; neither the requested locale nor any of its fall back locales
454 * The caller owns the returned object and is responsible for deleting it.
457 static BreakIterator* U_EXPORT2
458 createTitleInstance(const Locale& where, UErrorCode& status);
461 * Get the set of Locales for which TextBoundaries are installed.
462 * <p><b>Note:</b> this will not return locales added through the register
463 * call. To see the registered locales too, use the getAvailableLocales
464 * function that returns a StringEnumeration object </p>
465 * @param count the output parameter of number of elements in the locale list
466 * @return available locales
469 static const Locale* U_EXPORT2 getAvailableLocales(int32_t& count);
472 * Get name of the object for the desired Locale, in the desired langauge.
473 * @param objectLocale must be from getAvailableLocales.
474 * @param displayLocale specifies the desired locale for output.
475 * @param name the fill-in parameter of the return value
477 * @return user-displayable name
480 static UnicodeString& U_EXPORT2 getDisplayName(const Locale& objectLocale,
481 const Locale& displayLocale,
482 UnicodeString& name);
485 * Get name of the object for the desired Locale, in the langauge of the
487 * @param objectLocale must be from getMatchingLocales
488 * @param name the fill-in parameter of the return value
489 * @return user-displayable name
492 static UnicodeString& U_EXPORT2 getDisplayName(const Locale& objectLocale,
493 UnicodeString& name);
496 * Deprecated functionality. Use clone() instead.
498 * Thread safe client-buffer-based cloning operation
499 * Do NOT call delete on a safeclone, since 'new' is not used to create it.
500 * @param stackBuffer user allocated space for the new clone. If NULL new memory will be allocated.
501 * If buffer is not large enough, new memory will be allocated.
502 * @param BufferSize reference to size of allocated space.
503 * If BufferSize == 0, a sufficient size for use in cloning will
504 * be returned ('pre-flighting')
505 * If BufferSize is not enough for a stack-based safe clone,
506 * new memory will be allocated.
507 * @param status to indicate whether the operation went on smoothly or there were errors
508 * An informational status value, U_SAFECLONE_ALLOCATED_ERROR, is used if any allocations were
510 * @return pointer to the new clone
512 * @deprecated ICU 52. Use clone() instead.
514 virtual BreakIterator * createBufferClone(void *stackBuffer,
516 UErrorCode &status) = 0;
518 #ifndef U_HIDE_DEPRECATED_API
521 * Determine whether the BreakIterator was created in user memory by
522 * createBufferClone(), and thus should not be deleted. Such objects
523 * must be closed by an explicit call to the destructor (not delete).
524 * @deprecated ICU 52. Always delete the BreakIterator.
526 inline UBool isBufferClone(void);
528 #endif /* U_HIDE_DEPRECATED_API */
530 #if !UCONFIG_NO_SERVICE
532 * Register a new break iterator of the indicated kind, to use in the given locale.
533 * The break iterator will be adopted. Clones of the iterator will be returned
534 * if a request for a break iterator of the given kind matches or falls back to
536 * Because ICU may choose to cache BreakIterators internally, this must
537 * be called at application startup, prior to any calls to
538 * BreakIterator::createXXXInstance to avoid undefined behavior.
539 * @param toAdopt the BreakIterator instance to be adopted
540 * @param locale the Locale for which this instance is to be registered
541 * @param kind the type of iterator for which this instance is to be registered
542 * @param status the in/out status code, no special meanings are assigned
543 * @return a registry key that can be used to unregister this instance
546 static URegistryKey U_EXPORT2 registerInstance(BreakIterator* toAdopt,
547 const Locale& locale,
548 UBreakIteratorType kind,
552 * Unregister a previously-registered BreakIterator using the key returned from the
553 * register call. Key becomes invalid after a successful call and should not be used again.
554 * The BreakIterator corresponding to the key will be deleted.
555 * Because ICU may choose to cache BreakIterators internally, this should
556 * be called during application shutdown, after all calls to
557 * BreakIterator::createXXXInstance to avoid undefined behavior.
558 * @param key the registry key returned by a previous call to registerInstance
559 * @param status the in/out status code, no special meanings are assigned
560 * @return TRUE if the iterator for the key was successfully unregistered
563 static UBool U_EXPORT2 unregister(URegistryKey key, UErrorCode& status);
566 * Return a StringEnumeration over the locales available at the time of the call,
567 * including registered locales.
568 * @return a StringEnumeration over the locales available at the time of the call
571 static StringEnumeration* U_EXPORT2 getAvailableLocales(void);
575 * Returns the locale for this break iterator. Two flavors are available: valid and
579 Locale getLocale(ULocDataLocaleType type, UErrorCode& status) const;
581 #ifndef U_HIDE_INTERNAL_API
582 /** Get the locale for this break iterator object. You can choose between valid and actual locale.
583 * @param type type of the locale we're looking for (valid or actual)
584 * @param status error code for the operation
588 const char *getLocaleID(ULocDataLocaleType type, UErrorCode& status) const;
589 #endif /* U_HIDE_INTERNAL_API */
592 * Set the subject text string upon which the break iterator is operating
593 * without changing any other aspect of the matching state.
594 * The new and previous text strings must have the same content.
596 * This function is intended for use in environments where ICU is operating on
597 * strings that may move around in memory. It provides a mechanism for notifying
598 * ICU that the string has been relocated, and providing a new UText to access the
599 * string in its new position.
601 * Note that the break iterator implementation never copies the underlying text
602 * of a string being processed, but always operates directly on the original text
603 * provided by the user. Refreshing simply drops the references to the old text
604 * and replaces them with references to the new.
606 * Caution: this function is normally used only by very specialized,
607 * system-level code. One example use case is with garbage collection that moves
608 * the text in memory.
610 * @param input The new (moved) text string.
611 * @param status Receives errors detected by this function.
616 virtual BreakIterator &refreshInputText(UText *input, UErrorCode &status) = 0;
619 static BreakIterator* buildInstance(const Locale& loc, const char *type, int32_t kind, UErrorCode& status);
620 static BreakIterator* createInstance(const Locale& loc, int32_t kind, UErrorCode& status);
621 static BreakIterator* makeInstance(const Locale& loc, int32_t kind, UErrorCode& status);
623 friend class ICUBreakIteratorFactory;
624 friend class ICUBreakIteratorService;
627 // Do not enclose protected default/copy constructors with #ifndef U_HIDE_INTERNAL_API
628 // or else the compiler will create a public ones.
632 BreakIterator (const BreakIterator &other) : UObject(other) {}
633 #ifndef U_HIDE_INTERNAL_API
635 BreakIterator (const Locale& valid, const Locale& actual);
636 #endif /* U_HIDE_INTERNAL_API */
641 char actualLocale[ULOC_FULLNAME_CAPACITY];
642 char validLocale[ULOC_FULLNAME_CAPACITY];
645 * The assignment operator has no real implementation.
646 * It's provided to make the compiler happy. Do not call.
648 BreakIterator& operator=(const BreakIterator&);
651 #ifndef U_HIDE_DEPRECATED_API
653 inline UBool BreakIterator::isBufferClone()
658 #endif /* U_HIDE_DEPRECATED_API */
662 #endif /* #if !UCONFIG_NO_BREAK_ITERATION */