Path: blob/jdk8u272-b10-aarch32-20201026/jdk/src/share/native/common/unicode/brkiter.h
48729 views
// © 2016 and later: Unicode, Inc. and others.1// License & terms of use: http://www.unicode.org/copyright.html2/*3********************************************************************************4* Copyright (C) 1997-2016, International Business Machines5* Corporation and others. All Rights Reserved.6********************************************************************************7*8* File brkiter.h9*10* Modification History:11*12* Date Name Description13* 02/18/97 aliu Added typedef for TextCount. Made DONE const.14* 05/07/97 aliu Fixed DLL declaration.15* 07/09/97 jfitz Renamed BreakIterator and interface synced with JDK16* 08/11/98 helena Sync-up JDK1.2.17* 01/13/2000 helena Added UErrorCode parameter to createXXXInstance methods.18********************************************************************************19*/2021#ifndef BRKITER_H22#define BRKITER_H2324#include "unicode/utypes.h"2526/**27* \file28* \brief C++ API: Break Iterator.29*/3031#if UCONFIG_NO_BREAK_ITERATION3233U_NAMESPACE_BEGIN3435/*36* Allow the declaration of APIs with pointers to BreakIterator37* even when break iteration is removed from the build.38*/39class BreakIterator;4041U_NAMESPACE_END4243#else4445#include "unicode/uobject.h"46#include "unicode/unistr.h"47#include "unicode/chariter.h"48#include "unicode/locid.h"49#include "unicode/ubrk.h"50#include "unicode/strenum.h"51#include "unicode/utext.h"52#include "unicode/umisc.h"5354U_NAMESPACE_BEGIN5556/**57* The BreakIterator class implements methods for finding the location58* of boundaries in text. BreakIterator is an abstract base class.59* Instances of BreakIterator maintain a current position and scan over60* text returning the index of characters where boundaries occur.61* <p>62* Line boundary analysis determines where a text string can be broken63* when line-wrapping. The mechanism correctly handles punctuation and64* hyphenated words.65* <p>66* Sentence boundary analysis allows selection with correct67* interpretation of periods within numbers and abbreviations, and68* trailing punctuation marks such as quotation marks and parentheses.69* <p>70* Word boundary analysis is used by search and replace functions, as71* well as within text editing applications that allow the user to72* select words with a double click. Word selection provides correct73* interpretation of punctuation marks within and following74* words. Characters that are not part of a word, such as symbols or75* punctuation marks, have word-breaks on both sides.76* <p>77* Character boundary analysis allows users to interact with78* characters as they expect to, for example, when moving the cursor79* through a text string. Character boundary analysis provides correct80* navigation of through character strings, regardless of how the81* character is stored. For example, an accented character might be82* stored as a base character and a diacritical mark. What users83* consider to be a character can differ between languages.84* <p>85* The text boundary positions are found according to the rules86* described in Unicode Standard Annex #29, Text Boundaries, and87* Unicode Standard Annex #14, Line Breaking Properties. These88* are available at http://www.unicode.org/reports/tr14/ and89* http://www.unicode.org/reports/tr29/.90* <p>91* In addition to the C++ API defined in this header file, a92* plain C API with equivalent functionality is defined in the93* file ubrk.h94* <p>95* Code snippets illustrating the use of the Break Iterator APIs96* are available in the ICU User Guide,97* http://icu-project.org/userguide/boundaryAnalysis.html98* and in the sample program icu/source/samples/break/break.cpp99*100*/101class U_COMMON_API BreakIterator : public UObject {102public:103/**104* destructor105* @stable ICU 2.0106*/107virtual ~BreakIterator();108109/**110* Return true if another object is semantically equal to this111* one. The other object should be an instance of the same subclass of112* BreakIterator. Objects of different subclasses are considered113* unequal.114* <P>115* Return true if this BreakIterator is at the same position in the116* same text, and is the same class and type (word, line, etc.) of117* BreakIterator, as the argument. Text is considered the same if118* it contains the same characters, it need not be the same119* object, and styles are not considered.120* @stable ICU 2.0121*/122virtual UBool operator==(const BreakIterator&) const = 0;123124/**125* Returns the complement of the result of operator==126* @param rhs The BreakIterator to be compared for inequality127* @return the complement of the result of operator==128* @stable ICU 2.0129*/130UBool operator!=(const BreakIterator& rhs) const { return !operator==(rhs); }131132/**133* Return a polymorphic copy of this object. This is an abstract134* method which subclasses implement.135* @stable ICU 2.0136*/137virtual BreakIterator* clone(void) const = 0;138139/**140* Return a polymorphic class ID for this object. Different subclasses141* will return distinct unequal values.142* @stable ICU 2.0143*/144virtual UClassID getDynamicClassID(void) const = 0;145146/**147* Return a CharacterIterator over the text being analyzed.148* @stable ICU 2.0149*/150virtual CharacterIterator& getText(void) const = 0;151152153/**154* Get a UText for the text being analyzed.155* The returned UText is a shallow clone of the UText used internally156* by the break iterator implementation. It can safely be used to157* access the text without impacting any break iterator operations,158* but the underlying text itself must not be altered.159*160* @param fillIn A UText to be filled in. If NULL, a new UText will be161* allocated to hold the result.162* @param status receives any error codes.163* @return The current UText for this break iterator. If an input164* UText was provided, it will always be returned.165* @stable ICU 3.4166*/167virtual UText *getUText(UText *fillIn, UErrorCode &status) const = 0;168169/**170* Change the text over which this operates. The text boundary is171* reset to the start.172*173* The BreakIterator will retain a reference to the supplied string.174* The caller must not modify or delete the text while the BreakIterator175* retains the reference.176*177* @param text The UnicodeString used to change the text.178* @stable ICU 2.0179*/180virtual void setText(const UnicodeString &text) = 0;181182/**183* Reset the break iterator to operate over the text represented by184* the UText. The iterator position is reset to the start.185*186* This function makes a shallow clone of the supplied UText. This means187* that the caller is free to immediately close or otherwise reuse the188* Utext that was passed as a parameter, but that the underlying text itself189* must not be altered while being referenced by the break iterator.190*191* All index positions returned by break iterator functions are192* native indices from the UText. For example, when breaking UTF-8193* encoded text, the break positions returned by next(), previous(), etc.194* will be UTF-8 string indices, not UTF-16 positions.195*196* @param text The UText used to change the text.197* @param status receives any error codes.198* @stable ICU 3.4199*/200virtual void setText(UText *text, UErrorCode &status) = 0;201202/**203* Change the text over which this operates. The text boundary is204* reset to the start.205* Note that setText(UText *) provides similar functionality to this function,206* and is more efficient.207* @param it The CharacterIterator used to change the text.208* @stable ICU 2.0209*/210virtual void adoptText(CharacterIterator* it) = 0;211212enum {213/**214* DONE is returned by previous() and next() after all valid215* boundaries have been returned.216* @stable ICU 2.0217*/218DONE = (int32_t)-1219};220221/**222* Sets the current iteration position to the beginning of the text, position zero.223* @return The offset of the beginning of the text, zero.224* @stable ICU 2.0225*/226virtual int32_t first(void) = 0;227228/**229* Set the iterator position to the index immediately BEYOND the last character in the text being scanned.230* @return The index immediately BEYOND the last character in the text being scanned.231* @stable ICU 2.0232*/233virtual int32_t last(void) = 0;234235/**236* Set the iterator position to the boundary preceding the current boundary.237* @return The character index of the previous text boundary or DONE if all238* boundaries have been returned.239* @stable ICU 2.0240*/241virtual int32_t previous(void) = 0;242243/**244* Advance the iterator to the boundary following the current boundary.245* @return The character index of the next text boundary or DONE if all246* boundaries have been returned.247* @stable ICU 2.0248*/249virtual int32_t next(void) = 0;250251/**252* Return character index of the current iterator position within the text.253* @return The boundary most recently returned.254* @stable ICU 2.0255*/256virtual int32_t current(void) const = 0;257258/**259* Advance the iterator to the first boundary following the specified offset.260* The value returned is always greater than the offset or261* the value BreakIterator.DONE262* @param offset the offset to begin scanning.263* @return The first boundary after the specified offset.264* @stable ICU 2.0265*/266virtual int32_t following(int32_t offset) = 0;267268/**269* Set the iterator position to the first boundary preceding the specified offset.270* The value returned is always smaller than the offset or271* the value BreakIterator.DONE272* @param offset the offset to begin scanning.273* @return The first boundary before the specified offset.274* @stable ICU 2.0275*/276virtual int32_t preceding(int32_t offset) = 0;277278/**279* Return true if the specified position is a boundary position.280* As a side effect, the current position of the iterator is set281* to the first boundary position at or following the specified offset.282* @param offset the offset to check.283* @return True if "offset" is a boundary position.284* @stable ICU 2.0285*/286virtual UBool isBoundary(int32_t offset) = 0;287288/**289* Set the iterator position to the nth boundary from the current boundary290* @param n the number of boundaries to move by. A value of 0291* does nothing. Negative values move to previous boundaries292* and positive values move to later boundaries.293* @return The new iterator position, or294* DONE if there are fewer than |n| boundaries in the specified direction.295* @stable ICU 2.0296*/297virtual int32_t next(int32_t n) = 0;298299/**300* For RuleBasedBreakIterators, return the status tag from the break rule301* that determined the boundary at the current iteration position.302* <p>303* For break iterator types that do not support a rule status,304* a default value of 0 is returned.305* <p>306* @return the status from the break rule that determined the boundary at307* the current iteration position.308* @see RuleBaseBreakIterator::getRuleStatus()309* @see UWordBreak310* @stable ICU 52311*/312virtual int32_t getRuleStatus() const;313314/**315* For RuleBasedBreakIterators, get the status (tag) values from the break rule(s)316* that determined the boundary at the current iteration position.317* <p>318* For break iterator types that do not support rule status,319* no values are returned.320* <p>321* The returned status value(s) are stored into an array provided by the caller.322* The values are stored in sorted (ascending) order.323* If the capacity of the output array is insufficient to hold the data,324* the output will be truncated to the available length, and a325* U_BUFFER_OVERFLOW_ERROR will be signaled.326* <p>327* @see RuleBaseBreakIterator::getRuleStatusVec328*329* @param fillInVec an array to be filled in with the status values.330* @param capacity the length of the supplied vector. A length of zero causes331* the function to return the number of status values, in the332* normal way, without attempting to store any values.333* @param status receives error codes.334* @return The number of rule status values from rules that determined335* the boundary at the current iteration position.336* In the event of a U_BUFFER_OVERFLOW_ERROR, the return value337* is the total number of status values that were available,338* not the reduced number that were actually returned.339* @see getRuleStatus340* @stable ICU 52341*/342virtual int32_t getRuleStatusVec(int32_t *fillInVec, int32_t capacity, UErrorCode &status);343344/**345* Create BreakIterator for word-breaks using the given locale.346* Returns an instance of a BreakIterator implementing word breaks.347* WordBreak is useful for word selection (ex. double click)348* @param where the locale.349* @param status the error code350* @return A BreakIterator for word-breaks. The UErrorCode& status351* parameter is used to return status information to the user.352* To check whether the construction succeeded or not, you should check353* the value of U_SUCCESS(err). If you wish more detailed information, you354* can check for informational error results which still indicate success.355* U_USING_FALLBACK_WARNING indicates that a fall back locale was used. For356* example, 'de_CH' was requested, but nothing was found there, so 'de' was357* used. U_USING_DEFAULT_WARNING indicates that the default locale data was358* used; neither the requested locale nor any of its fall back locales359* could be found.360* The caller owns the returned object and is responsible for deleting it.361* @stable ICU 2.0362*/363static BreakIterator* U_EXPORT2364createWordInstance(const Locale& where, UErrorCode& status);365366/**367* Create BreakIterator for line-breaks using specified locale.368* Returns an instance of a BreakIterator implementing line breaks. Line369* breaks are logically possible line breaks, actual line breaks are370* usually determined based on display width.371* LineBreak is useful for word wrapping text.372* @param where the locale.373* @param status The error code.374* @return A BreakIterator for line-breaks. The UErrorCode& status375* parameter is used to return status information to the user.376* To check whether the construction succeeded or not, you should check377* the value of U_SUCCESS(err). If you wish more detailed information, you378* can check for informational error results which still indicate success.379* U_USING_FALLBACK_WARNING indicates that a fall back locale was used. For380* example, 'de_CH' was requested, but nothing was found there, so 'de' was381* used. U_USING_DEFAULT_WARNING indicates that the default locale data was382* used; neither the requested locale nor any of its fall back locales383* could be found.384* The caller owns the returned object and is responsible for deleting it.385* @stable ICU 2.0386*/387static BreakIterator* U_EXPORT2388createLineInstance(const Locale& where, UErrorCode& status);389390/**391* Create BreakIterator for character-breaks using specified locale392* Returns an instance of a BreakIterator implementing character breaks.393* Character breaks are boundaries of combining character sequences.394* @param where the locale.395* @param status The error code.396* @return A BreakIterator for character-breaks. The UErrorCode& status397* parameter is used to return status information to the user.398* To check whether the construction succeeded or not, you should check399* the value of U_SUCCESS(err). If you wish more detailed information, you400* can check for informational error results which still indicate success.401* U_USING_FALLBACK_WARNING indicates that a fall back locale was used. For402* example, 'de_CH' was requested, but nothing was found there, so 'de' was403* used. U_USING_DEFAULT_WARNING indicates that the default locale data was404* used; neither the requested locale nor any of its fall back locales405* could be found.406* The caller owns the returned object and is responsible for deleting it.407* @stable ICU 2.0408*/409static BreakIterator* U_EXPORT2410createCharacterInstance(const Locale& where, UErrorCode& status);411412/**413* Create BreakIterator for sentence-breaks using specified locale414* Returns an instance of a BreakIterator implementing sentence breaks.415* @param where the locale.416* @param status The error code.417* @return A BreakIterator for sentence-breaks. The UErrorCode& status418* parameter is used to return status information to the user.419* To check whether the construction succeeded or not, you should check420* the value of U_SUCCESS(err). If you wish more detailed information, you421* can check for informational error results which still indicate success.422* U_USING_FALLBACK_WARNING indicates that a fall back locale was used. For423* example, 'de_CH' was requested, but nothing was found there, so 'de' was424* used. U_USING_DEFAULT_WARNING indicates that the default locale data was425* used; neither the requested locale nor any of its fall back locales426* could be found.427* The caller owns the returned object and is responsible for deleting it.428* @stable ICU 2.0429*/430static BreakIterator* U_EXPORT2431createSentenceInstance(const Locale& where, UErrorCode& status);432433#ifndef U_HIDE_DEPRECATED_API434/**435* Create BreakIterator for title-casing breaks using the specified locale436* Returns an instance of a BreakIterator implementing title breaks.437* The iterator returned locates title boundaries as described for438* Unicode 3.2 only. For Unicode 4.0 and above title boundary iteration,439* please use a word boundary iterator. See {@link #createWordInstance }.440*441* @param where the locale.442* @param status The error code.443* @return A BreakIterator for title-breaks. The UErrorCode& status444* parameter is used to return status information to the user.445* To check whether the construction succeeded or not, you should check446* the value of U_SUCCESS(err). If you wish more detailed information, you447* can check for informational error results which still indicate success.448* U_USING_FALLBACK_WARNING indicates that a fall back locale was used. For449* example, 'de_CH' was requested, but nothing was found there, so 'de' was450* used. U_USING_DEFAULT_WARNING indicates that the default locale data was451* used; neither the requested locale nor any of its fall back locales452* could be found.453* The caller owns the returned object and is responsible for deleting it.454* @deprecated ICU 64 Use createWordInstance instead.455*/456static BreakIterator* U_EXPORT2457createTitleInstance(const Locale& where, UErrorCode& status);458#endif /* U_HIDE_DEPRECATED_API */459460/**461* Get the set of Locales for which TextBoundaries are installed.462* <p><b>Note:</b> this will not return locales added through the register463* call. To see the registered locales too, use the getAvailableLocales464* function that returns a StringEnumeration object </p>465* @param count the output parameter of number of elements in the locale list466* @return available locales467* @stable ICU 2.0468*/469static const Locale* U_EXPORT2 getAvailableLocales(int32_t& count);470471/**472* Get name of the object for the desired Locale, in the desired language.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 value476* Uses best match.477* @return user-displayable name478* @stable ICU 2.0479*/480static UnicodeString& U_EXPORT2 getDisplayName(const Locale& objectLocale,481const Locale& displayLocale,482UnicodeString& name);483484/**485* Get name of the object for the desired Locale, in the language of the486* default locale.487* @param objectLocale must be from getMatchingLocales488* @param name the fill-in parameter of the return value489* @return user-displayable name490* @stable ICU 2.0491*/492static UnicodeString& U_EXPORT2 getDisplayName(const Locale& objectLocale,493UnicodeString& name);494495/**496* Deprecated functionality. Use clone() instead.497*498* Thread safe client-buffer-based cloning operation499* 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 will504* 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 errors508* An informational status value, U_SAFECLONE_ALLOCATED_ERROR, is used if any allocations were509* necessary.510* @return pointer to the new clone511*512* @deprecated ICU 52. Use clone() instead.513*/514virtual BreakIterator * createBufferClone(void *stackBuffer,515int32_t &BufferSize,516UErrorCode &status) = 0;517518#ifndef U_HIDE_DEPRECATED_API519520/**521* Determine whether the BreakIterator was created in user memory by522* createBufferClone(), and thus should not be deleted. Such objects523* must be closed by an explicit call to the destructor (not delete).524* @deprecated ICU 52. Always delete the BreakIterator.525*/526inline UBool isBufferClone(void);527528#endif /* U_HIDE_DEPRECATED_API */529530#if !UCONFIG_NO_SERVICE531/**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 returned534* if a request for a break iterator of the given kind matches or falls back to535* this locale.536* Because ICU may choose to cache BreakIterators internally, this must537* be called at application startup, prior to any calls to538* BreakIterator::createXXXInstance to avoid undefined behavior.539* @param toAdopt the BreakIterator instance to be adopted540* @param locale the Locale for which this instance is to be registered541* @param kind the type of iterator for which this instance is to be registered542* @param status the in/out status code, no special meanings are assigned543* @return a registry key that can be used to unregister this instance544* @stable ICU 2.4545*/546static URegistryKey U_EXPORT2 registerInstance(BreakIterator* toAdopt,547const Locale& locale,548UBreakIteratorType kind,549UErrorCode& status);550551/**552* Unregister a previously-registered BreakIterator using the key returned from the553* 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 should556* be called during application shutdown, after all calls to557* BreakIterator::createXXXInstance to avoid undefined behavior.558* @param key the registry key returned by a previous call to registerInstance559* @param status the in/out status code, no special meanings are assigned560* @return TRUE if the iterator for the key was successfully unregistered561* @stable ICU 2.4562*/563static UBool U_EXPORT2 unregister(URegistryKey key, UErrorCode& status);564565/**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 call569* @stable ICU 2.4570*/571static StringEnumeration* U_EXPORT2 getAvailableLocales(void);572#endif573574/**575* Returns the locale for this break iterator. Two flavors are available: valid and576* actual locale.577* @stable ICU 2.8578*/579Locale getLocale(ULocDataLocaleType type, UErrorCode& status) const;580581#ifndef U_HIDE_INTERNAL_API582/** 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 operation585* @return the locale586* @internal587*/588const char *getLocaleID(ULocDataLocaleType type, UErrorCode& status) const;589#endif /* U_HIDE_INTERNAL_API */590591/**592* Set the subject text string upon which the break iterator is operating593* without changing any other aspect of the matching state.594* The new and previous text strings must have the same content.595*596* This function is intended for use in environments where ICU is operating on597* strings that may move around in memory. It provides a mechanism for notifying598* ICU that the string has been relocated, and providing a new UText to access the599* string in its new position.600*601* Note that the break iterator implementation never copies the underlying text602* of a string being processed, but always operates directly on the original text603* provided by the user. Refreshing simply drops the references to the old text604* and replaces them with references to the new.605*606* Caution: this function is normally used only by very specialized,607* system-level code. One example use case is with garbage collection that moves608* the text in memory.609*610* @param input The new (moved) text string.611* @param status Receives errors detected by this function.612* @return *this613*614* @stable ICU 49615*/616virtual BreakIterator &refreshInputText(UText *input, UErrorCode &status) = 0;617618private:619static BreakIterator* buildInstance(const Locale& loc, const char *type, UErrorCode& status);620static BreakIterator* createInstance(const Locale& loc, int32_t kind, UErrorCode& status);621static BreakIterator* makeInstance(const Locale& loc, int32_t kind, UErrorCode& status);622623friend class ICUBreakIteratorFactory;624friend class ICUBreakIteratorService;625626protected:627// Do not enclose protected default/copy constructors with #ifndef U_HIDE_INTERNAL_API628// or else the compiler will create a public ones.629/** @internal */630BreakIterator();631/** @internal */632BreakIterator (const BreakIterator &other);633#ifndef U_HIDE_INTERNAL_API634/** @internal */635BreakIterator (const Locale& valid, const Locale &actual);636/** @internal. Assignment Operator, used by RuleBasedBreakIterator. */637BreakIterator &operator = (const BreakIterator &other);638#endif /* U_HIDE_INTERNAL_API */639640private:641642/** @internal (private) */643char actualLocale[ULOC_FULLNAME_CAPACITY];644char validLocale[ULOC_FULLNAME_CAPACITY];645};646647#ifndef U_HIDE_DEPRECATED_API648649inline UBool BreakIterator::isBufferClone()650{651return FALSE;652}653654#endif /* U_HIDE_DEPRECATED_API */655656U_NAMESPACE_END657658#endif /* #if !UCONFIG_NO_BREAK_ITERATION */659660#endif // BRKITER_H661//eof662663664