• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 ********************************************************************************
3 *   Copyright (C) 1997-2012, International Business Machines
4 *   Corporation and others.  All Rights Reserved.
5 ********************************************************************************
6 *
7 * File brkiter.h
8 *
9 * Modification History:
10 *
11 *   Date        Name        Description
12 *   02/18/97    aliu        Added typedef for TextCount.  Made DONE const.
13 *   05/07/97    aliu        Fixed DLL declaration.
14 *   07/09/97    jfitz       Renamed BreakIterator and interface synced with JDK
15 *   08/11/98    helena      Sync-up JDK1.2.
16 *   01/13/2000  helena      Added UErrorCode parameter to createXXXInstance methods.
17 ********************************************************************************
18 */
19 
20 #ifndef BRKITER_H
21 #define BRKITER_H
22 
23 #include "unicode/utypes.h"
24 
25 /**
26  * \file
27  * \brief C++ API: Break Iterator.
28  */
29 
30 #if UCONFIG_NO_BREAK_ITERATION
31 
32 U_NAMESPACE_BEGIN
33 
34 /*
35  * Allow the declaration of APIs with pointers to BreakIterator
36  * even when break iteration is removed from the build.
37  */
38 class BreakIterator;
39 
40 U_NAMESPACE_END
41 
42 #else
43 
44 #include "unicode/uobject.h"
45 #include "unicode/unistr.h"
46 #include "unicode/chariter.h"
47 #include "unicode/locid.h"
48 #include "unicode/ubrk.h"
49 #include "unicode/strenum.h"
50 #include "unicode/utext.h"
51 #include "unicode/umisc.h"
52 
53 U_NAMESPACE_BEGIN
54 
55 /**
56  * The BreakIterator class implements methods for finding the location
57  * of boundaries in text. BreakIterator is an abstract base class.
58  * Instances of BreakIterator maintain a current position and scan over
59  * text returning the index of characters where boundaries occur.
60  * <p>
61  * Line boundary analysis determines where a text string can be broken
62  * when line-wrapping. The mechanism correctly handles punctuation and
63  * hyphenated words.
64  * <p>
65  * Sentence boundary analysis allows selection with correct
66  * interpretation of periods within numbers and abbreviations, and
67  * trailing punctuation marks such as quotation marks and parentheses.
68  * <p>
69  * Word boundary analysis is used by search and replace functions, as
70  * well as within text editing applications that allow the user to
71  * select words with a double click. Word selection provides correct
72  * interpretation of punctuation marks within and following
73  * words. Characters that are not part of a word, such as symbols or
74  * punctuation marks, have word-breaks on both sides.
75  * <p>
76  * Character boundary analysis allows users to interact with
77  * characters as they expect to, for example, when moving the cursor
78  * through a text string. Character boundary analysis provides correct
79  * navigation of through character strings, regardless of how the
80  * character is stored.  For example, an accented character might be
81  * stored as a base character and a diacritical mark. What users
82  * consider to be a character can differ between languages.
83  * <p>
84  * The text boundary positions are found according to the rules
85  * described in Unicode Standard Annex #29, Text Boundaries, and
86  * Unicode Standard Annex #14, Line Breaking Properties.  These
87  * are available at http://www.unicode.org/reports/tr14/ and
88  * http://www.unicode.org/reports/tr29/.
89  * <p>
90  * In addition to the C++ API defined in this header file, a
91  * plain C API with equivalent functionality is defined in the
92  * file ubrk.h
93  * <p>
94  * Code snippets illustrating the use of the Break Iterator APIs
95  * are available in the ICU User Guide,
96  * http://icu-project.org/userguide/boundaryAnalysis.html
97  * and in the sample program icu/source/samples/break/break.cpp
98  *
99  */
100 class U_COMMON_API BreakIterator : public UObject {
101 public:
102     /**
103      *  destructor
104      *  @stable ICU 2.0
105      */
106     virtual ~BreakIterator();
107 
108     /**
109      * Return true if another object is semantically equal to this
110      * one. The other object should be an instance of the same subclass of
111      * BreakIterator. Objects of different subclasses are considered
112      * unequal.
113      * <P>
114      * Return true if this BreakIterator is at the same position in the
115      * same text, and is the same class and type (word, line, etc.) of
116      * BreakIterator, as the argument.  Text is considered the same if
117      * it contains the same characters, it need not be the same
118      * object, and styles are not considered.
119      * @stable ICU 2.0
120      */
121     virtual UBool operator==(const BreakIterator&) const = 0;
122 
123     /**
124      * Returns the complement of the result of operator==
125      * @param rhs The BreakIterator to be compared for inequality
126      * @return the complement of the result of operator==
127      * @stable ICU 2.0
128      */
129     UBool operator!=(const BreakIterator& rhs) const { return !operator==(rhs); }
130 
131     /**
132      * Return a polymorphic copy of this object.  This is an abstract
133      * method which subclasses implement.
134      * @stable ICU 2.0
135      */
136     virtual BreakIterator* clone(void) const = 0;
137 
138     /**
139      * Return a polymorphic class ID for this object. Different subclasses
140      * will return distinct unequal values.
141      * @stable ICU 2.0
142      */
143     virtual UClassID getDynamicClassID(void) const = 0;
144 
145     /**
146      * Return a CharacterIterator over the text being analyzed.
147      * @stable ICU 2.0
148      */
149     virtual CharacterIterator& getText(void) const = 0;
150 
151 
152     /**
153       *  Get a UText for the text being analyzed.
154       *  The returned UText is a shallow clone of the UText used internally
155       *  by the break iterator implementation.  It can safely be used to
156       *  access the text without impacting any break iterator operations,
157       *  but the underlying text itself must not be altered.
158       *
159       * @param fillIn A UText to be filled in.  If NULL, a new UText will be
160       *           allocated to hold the result.
161       * @param status receives any error codes.
162       * @return   The current UText for this break iterator.  If an input
163       *           UText was provided, it will always be returned.
164       * @stable ICU 3.4
165       */
166      virtual UText *getUText(UText *fillIn, UErrorCode &status) const = 0;
167 
168     /**
169      * Change the text over which this operates. The text boundary is
170      * reset to the start.
171      * @param text The UnicodeString used to change the text.
172      * @stable ICU 2.0
173      */
174     virtual void  setText(const UnicodeString &text) = 0;
175 
176     /**
177      * Reset the break iterator to operate over the text represented by
178      * the UText.  The iterator position is reset to the start.
179      *
180      * This function makes a shallow clone of the supplied UText.  This means
181      * that the caller is free to immediately close or otherwise reuse the
182      * Utext that was passed as a parameter, but that the underlying text itself
183      * must not be altered while being referenced by the break iterator.
184      *
185      * @param text The UText used to change the text.
186      * @param status receives any error codes.
187      * @stable ICU 3.4
188      */
189     virtual void  setText(UText *text, UErrorCode &status) = 0;
190 
191     /**
192      * Change the text over which this operates. The text boundary is
193      * reset to the start.
194      * Note that setText(UText *) provides similar functionality to this function,
195      * and is more efficient.
196      * @param it The CharacterIterator used to change the text.
197      * @stable ICU 2.0
198      */
199     virtual void  adoptText(CharacterIterator* it) = 0;
200 
201     enum {
202         /**
203          * DONE is returned by previous() and next() after all valid
204          * boundaries have been returned.
205          * @stable ICU 2.0
206          */
207         DONE = (int32_t)-1
208     };
209 
210     /**
211      * Set the iterator position to the index of the first character in the text being scanned.
212      * @return The index of the first character in the text being scanned.
213      * @stable ICU 2.0
214      */
215     virtual int32_t first(void) = 0;
216 
217     /**
218      * Set the iterator position to the index immediately BEYOND the last character in the text being scanned.
219      * @return The index immediately BEYOND the last character in the text being scanned.
220      * @stable ICU 2.0
221      */
222     virtual int32_t last(void) = 0;
223 
224     /**
225      * Set the iterator position to the boundary preceding the current boundary.
226      * @return The character index of the previous text boundary or DONE if all
227      * boundaries have been returned.
228      * @stable ICU 2.0
229      */
230     virtual int32_t previous(void) = 0;
231 
232     /**
233      * Advance the iterator to the boundary following the current boundary.
234      * @return The character index of the next text boundary or DONE if all
235      * boundaries have been returned.
236      * @stable ICU 2.0
237      */
238     virtual int32_t next(void) = 0;
239 
240     /**
241      * Return character index of the current interator position within the text.
242      * @return The boundary most recently returned.
243      * @stable ICU 2.0
244      */
245     virtual int32_t current(void) const = 0;
246 
247     /**
248      * Advance the iterator to the first boundary following the specified offset.
249      * The value returned is always greater than the offset or
250      * the value BreakIterator.DONE
251      * @param offset the offset to begin scanning.
252      * @return The first boundary after the specified offset.
253      * @stable ICU 2.0
254      */
255     virtual int32_t following(int32_t offset) = 0;
256 
257     /**
258      * Set the iterator position to the first boundary preceding the specified offset.
259      * The value returned is always smaller than the offset or
260      * the value BreakIterator.DONE
261      * @param offset the offset to begin scanning.
262      * @return The first boundary before the specified offset.
263      * @stable ICU 2.0
264      */
265     virtual int32_t preceding(int32_t offset) = 0;
266 
267     /**
268      * Return true if the specfied position is a boundary position.
269      * As a side effect, the current position of the iterator is set
270      * to the first boundary position at or following the specified offset.
271      * @param offset the offset to check.
272      * @return True if "offset" is a boundary position.
273      * @stable ICU 2.0
274      */
275     virtual UBool isBoundary(int32_t offset) = 0;
276 
277     /**
278      * Set the iterator position to the nth boundary from the current boundary
279      * @param n the number of boundaries to move by.  A value of 0
280      * does nothing.  Negative values move to previous boundaries
281      * and positive values move to later boundaries.
282      * @return The new iterator position, or
283      * DONE if there are fewer than |n| boundaries in the specfied direction.
284      * @stable ICU 2.0
285      */
286     virtual int32_t next(int32_t n) = 0;
287 
288     /**
289      * Create BreakIterator for word-breaks using the given locale.
290      * Returns an instance of a BreakIterator implementing word breaks.
291      * WordBreak is useful for word selection (ex. double click)
292      * @param where the locale.
293      * @param status the error code
294      * @return A BreakIterator for word-breaks.  The UErrorCode& status
295      * parameter is used to return status information to the user.
296      * To check whether the construction succeeded or not, you should check
297      * the value of U_SUCCESS(err).  If you wish more detailed information, you
298      * can check for informational error results which still indicate success.
299      * U_USING_FALLBACK_WARNING indicates that a fall back locale was used.  For
300      * example, 'de_CH' was requested, but nothing was found there, so 'de' was
301      * used.  U_USING_DEFAULT_WARNING indicates that the default locale data was
302      * used; neither the requested locale nor any of its fall back locales
303      * could be found.
304      * The caller owns the returned object and is responsible for deleting it.
305      * @stable ICU 2.0
306      */
307     static BreakIterator* U_EXPORT2
308     createWordInstance(const Locale& where, UErrorCode& status);
309 
310     /**
311      * Create BreakIterator for line-breaks using specified locale.
312      * Returns an instance of a BreakIterator implementing line breaks. Line
313      * breaks are logically possible line breaks, actual line breaks are
314      * usually determined based on display width.
315      * LineBreak is useful for word wrapping text.
316      * @param where the locale.
317      * @param status The error code.
318      * @return A BreakIterator for line-breaks.  The UErrorCode& status
319      * parameter is used to return status information to the user.
320      * To check whether the construction succeeded or not, you should check
321      * the value of U_SUCCESS(err).  If you wish more detailed information, you
322      * can check for informational error results which still indicate success.
323      * U_USING_FALLBACK_WARNING indicates that a fall back locale was used.  For
324      * example, 'de_CH' was requested, but nothing was found there, so 'de' was
325      * used.  U_USING_DEFAULT_WARNING indicates that the default locale data was
326      * used; neither the requested locale nor any of its fall back locales
327      * could be found.
328      * The caller owns the returned object and is responsible for deleting it.
329      * @stable ICU 2.0
330      */
331     static BreakIterator* U_EXPORT2
332     createLineInstance(const Locale& where, UErrorCode& status);
333 
334     /**
335      * Create BreakIterator for character-breaks using specified locale
336      * Returns an instance of a BreakIterator implementing character breaks.
337      * Character breaks are boundaries of combining character sequences.
338      * @param where the locale.
339      * @param status The error code.
340      * @return A BreakIterator for character-breaks.  The UErrorCode& status
341      * parameter is used to return status information to the user.
342      * To check whether the construction succeeded or not, you should check
343      * the value of U_SUCCESS(err).  If you wish more detailed information, you
344      * can check for informational error results which still indicate success.
345      * U_USING_FALLBACK_WARNING indicates that a fall back locale was used.  For
346      * example, 'de_CH' was requested, but nothing was found there, so 'de' was
347      * used.  U_USING_DEFAULT_WARNING indicates that the default locale data was
348      * used; neither the requested locale nor any of its fall back locales
349      * could be found.
350      * The caller owns the returned object and is responsible for deleting it.
351      * @stable ICU 2.0
352      */
353     static BreakIterator* U_EXPORT2
354     createCharacterInstance(const Locale& where, UErrorCode& status);
355 
356     /**
357      * Create BreakIterator for sentence-breaks using specified locale
358      * Returns an instance of a BreakIterator implementing sentence breaks.
359      * @param where the locale.
360      * @param status The error code.
361      * @return A BreakIterator for sentence-breaks.  The UErrorCode& status
362      * parameter is used to return status information to the user.
363      * To check whether the construction succeeded or not, you should check
364      * the value of U_SUCCESS(err).  If you wish more detailed information, you
365      * can check for informational error results which still indicate success.
366      * U_USING_FALLBACK_WARNING indicates that a fall back locale was used.  For
367      * example, 'de_CH' was requested, but nothing was found there, so 'de' was
368      * used.  U_USING_DEFAULT_WARNING indicates that the default locale data was
369      * used; neither the requested locale nor any of its fall back locales
370      * could be found.
371      * The caller owns the returned object and is responsible for deleting it.
372      * @stable ICU 2.0
373      */
374     static BreakIterator* U_EXPORT2
375     createSentenceInstance(const Locale& where, UErrorCode& status);
376 
377     /**
378      * Create BreakIterator for title-casing breaks using the specified locale
379      * Returns an instance of a BreakIterator implementing title breaks.
380      * The iterator returned locates title boundaries as described for
381      * Unicode 3.2 only. For Unicode 4.0 and above title boundary iteration,
382      * please use Word Boundary iterator.{@link #createWordInstance }
383      *
384      * @param where the locale.
385      * @param status The error code.
386      * @return A BreakIterator for title-breaks.  The UErrorCode& status
387      * parameter is used to return status information to the user.
388      * To check whether the construction succeeded or not, you should check
389      * the value of U_SUCCESS(err).  If you wish more detailed information, you
390      * can check for informational error results which still indicate success.
391      * U_USING_FALLBACK_WARNING indicates that a fall back locale was used.  For
392      * example, 'de_CH' was requested, but nothing was found there, so 'de' was
393      * used.  U_USING_DEFAULT_WARNING indicates that the default locale data was
394      * used; neither the requested locale nor any of its fall back locales
395      * could be found.
396      * The caller owns the returned object and is responsible for deleting it.
397      * @stable ICU 2.1
398      */
399     static BreakIterator* U_EXPORT2
400     createTitleInstance(const Locale& where, UErrorCode& status);
401 
402     /**
403      * Get the set of Locales for which TextBoundaries are installed.
404      * <p><b>Note:</b> this will not return locales added through the register
405      * call. To see the registered locales too, use the getAvailableLocales
406      * function that returns a StringEnumeration object </p>
407      * @param count the output parameter of number of elements in the locale list
408      * @return available locales
409      * @stable ICU 2.0
410      */
411     static const Locale* U_EXPORT2 getAvailableLocales(int32_t& count);
412 
413     /**
414      * Get name of the object for the desired Locale, in the desired langauge.
415      * @param objectLocale must be from getAvailableLocales.
416      * @param displayLocale specifies the desired locale for output.
417      * @param name the fill-in parameter of the return value
418      * Uses best match.
419      * @return user-displayable name
420      * @stable ICU 2.0
421      */
422     static UnicodeString& U_EXPORT2 getDisplayName(const Locale& objectLocale,
423                                          const Locale& displayLocale,
424                                          UnicodeString& name);
425 
426     /**
427      * Get name of the object for the desired Locale, in the langauge of the
428      * default locale.
429      * @param objectLocale must be from getMatchingLocales
430      * @param name the fill-in parameter of the return value
431      * @return user-displayable name
432      * @stable ICU 2.0
433      */
434     static UnicodeString& U_EXPORT2 getDisplayName(const Locale& objectLocale,
435                                          UnicodeString& name);
436 
437     /**
438      * Thread safe client-buffer-based cloning operation
439      *    Do NOT call delete on a safeclone, since 'new' is not used to create it.
440      * @param stackBuffer user allocated space for the new clone. If NULL new memory will be allocated.
441      * If buffer is not large enough, new memory will be allocated.
442      * @param BufferSize reference to size of allocated space.
443      * If BufferSize == 0, a sufficient size for use in cloning will
444      * be returned ('pre-flighting')
445      * If BufferSize is not enough for a stack-based safe clone,
446      * new memory will be allocated.
447      * @param status to indicate whether the operation went on smoothly or there were errors
448      *  An informational status value, U_SAFECLONE_ALLOCATED_ERROR, is used if any allocations were
449      *  necessary.
450      * @return pointer to the new clone
451      *
452      * @stable ICU 2.0
453      */
454     virtual BreakIterator *  createBufferClone(void *stackBuffer,
455                                                int32_t &BufferSize,
456                                                UErrorCode &status) = 0;
457 
458     /**
459      *   Determine whether the BreakIterator was created in user memory by
460      *   createBufferClone(), and thus should not be deleted.  Such objects
461      *   must be closed by an explicit call to the destructor (not delete).
462      *  @stable ICU 2.0
463      */
464     inline UBool isBufferClone(void);
465 
466 #if !UCONFIG_NO_SERVICE
467     /**
468      * Register a new break iterator of the indicated kind, to use in the given locale.
469      * The break iterator will be adopted.  Clones of the iterator will be returned
470      * if a request for a break iterator of the given kind matches or falls back to
471      * this locale.
472      * @param toAdopt the BreakIterator instance to be adopted
473      * @param locale the Locale for which this instance is to be registered
474      * @param kind the type of iterator for which this instance is to be registered
475      * @param status the in/out status code, no special meanings are assigned
476      * @return a registry key that can be used to unregister this instance
477      * @stable ICU 2.4
478      */
479     static URegistryKey U_EXPORT2 registerInstance(BreakIterator* toAdopt,
480                                         const Locale& locale,
481                                         UBreakIteratorType kind,
482                                         UErrorCode& status);
483 
484     /**
485      * Unregister a previously-registered BreakIterator using the key returned from the
486      * register call.  Key becomes invalid after a successful call and should not be used again.
487      * The BreakIterator corresponding to the key will be deleted.
488      * @param key the registry key returned by a previous call to registerInstance
489      * @param status the in/out status code, no special meanings are assigned
490      * @return TRUE if the iterator for the key was successfully unregistered
491      * @stable ICU 2.4
492      */
493     static UBool U_EXPORT2 unregister(URegistryKey key, UErrorCode& status);
494 
495     /**
496      * Return a StringEnumeration over the locales available at the time of the call,
497      * including registered locales.
498      * @return a StringEnumeration over the locales available at the time of the call
499      * @stable ICU 2.4
500      */
501     static StringEnumeration* U_EXPORT2 getAvailableLocales(void);
502 #endif
503 
504     /**
505      * Returns the locale for this break iterator. Two flavors are available: valid and
506      * actual locale.
507      * @stable ICU 2.8
508      */
509     Locale getLocale(ULocDataLocaleType type, UErrorCode& status) const;
510 
511 #ifndef U_HIDE_INTERNAL_API
512     /** Get the locale for this break iterator object. You can choose between valid and actual locale.
513      *  @param type type of the locale we're looking for (valid or actual)
514      *  @param status error code for the operation
515      *  @return the locale
516      *  @internal
517      */
518     const char *getLocaleID(ULocDataLocaleType type, UErrorCode& status) const;
519 #endif  /* U_HIDE_INTERNAL_API */
520 
521     /**
522      *  Set the subject text string upon which the break iterator is operating
523      *  without changing any other aspect of the matching state.
524      *  The new and previous text strings must have the same content.
525      *
526      *  This function is intended for use in environments where ICU is operating on
527      *  strings that may move around in memory.  It provides a mechanism for notifying
528      *  ICU that the string has been relocated, and providing a new UText to access the
529      *  string in its new position.
530      *
531      *  Note that the break iterator implementation never copies the underlying text
532      *  of a string being processed, but always operates directly on the original text
533      *  provided by the user. Refreshing simply drops the references to the old text
534      *  and replaces them with references to the new.
535      *
536      *  Caution:  this function is normally used only by very specialized,
537      *  system-level code.  One example use case is with garbage collection that moves
538      *  the text in memory.
539      *
540      * @param input      The new (moved) text string.
541      * @param status     Receives errors detected by this function.
542      * @return           *this
543      *
544      * @draft ICU 49
545      */
546     virtual BreakIterator &refreshInputText(UText *input, UErrorCode &status) = 0;
547 
548  private:
549     static BreakIterator* buildInstance(const Locale& loc, const char *type, int32_t kind, UErrorCode& status);
550     static BreakIterator* createInstance(const Locale& loc, int32_t kind, UErrorCode& status);
551     static BreakIterator* makeInstance(const Locale& loc, int32_t kind, UErrorCode& status);
552 
553     friend class ICUBreakIteratorFactory;
554     friend class ICUBreakIteratorService;
555 
556 protected:
557     // Do not enclose protected default/copy constructors with #ifndef U_HIDE_INTERNAL_API
558     // or else the compiler will create a public ones.
559     /** @internal */
560     BreakIterator();
561     /** @internal */
562     UBool fBufferClone;
563     /** @internal */
564     BreakIterator (const BreakIterator &other) : UObject(other), fBufferClone(FALSE) {}
565 
566 private:
567 
568     /** @internal */
569     char actualLocale[ULOC_FULLNAME_CAPACITY];
570     char validLocale[ULOC_FULLNAME_CAPACITY];
571 
572     /**
573      * The assignment operator has no real implementation.
574      * It's provided to make the compiler happy. Do not call.
575      */
576     BreakIterator& operator=(const BreakIterator&);
577 };
578 
579 inline UBool BreakIterator::isBufferClone()
580 {
581     return fBufferClone;
582 }
583 
584 U_NAMESPACE_END
585 
586 #endif /* #if !UCONFIG_NO_BREAK_ITERATION */
587 
588 #endif // _BRKITER
589 //eof
590 
591