| 6 | #define BASE_I18N_BREAK_ITERATOR_H_ |
| 7 |
| 8 | #include <stddef.h> |
| 9 |
| 10 | #include "base/i18n/base_i18n_export.h" |
| 11 | #include "base/macros.h" |
| 12 | #include "base/strings/string16.h" |
| 13 | #include "base/strings/string_piece.h" |
| 14 |
| 15 | // The BreakIterator class iterates through the words, word breaks, and |
| 16 | // line breaks in a UTF-16 string. |
| 17 | // |
| 18 | // It provides several modes, BREAK_WORD, BREAK_LINE, BREAK_NEWLINE, and |
| 19 | // BREAK_SENTENCE which modify how characters are aggregated into the returned |
| 20 | // string. |
| 21 | // |
| 22 | // Under BREAK_WORD mode, once a word is encountered any non-word |
| 23 | // characters are not included in the returned string (e.g. in the |
| 24 | // UTF-16 equivalent of the string " foo bar! ", the word breaks are at |
| 25 | // the periods in ". .foo. .bar.!. ."). |
| 26 | // Note that Chinese/Japanese/Thai do not use spaces between words so that |
| 27 | // boundaries can fall in the middle of a continuous run of non-space / |
| 28 | // non-punctuation characters. |
| 29 | // |
| 30 | // Under BREAK_LINE mode, once a line breaking opportunity is encountered, |
| 31 | // any non-word characters are included in the returned string, breaking |
| 32 | // only when a space-equivalent character or a line breaking opportunity |
| 33 | // is encountered (e.g. in the UTF16-equivalent of the string " foo bar! ", |
| 34 | // the breaks are at the periods in ". .foo .bar! ."). |
| 35 | // |
| 36 | // Note that lines can be broken at any character/syllable/grapheme cluster |
| 37 | // boundary in Chinese/Japanese/Korean and at word boundaries in Thai |
| 38 | // (Thai does not use spaces between words). Therefore, this is NOT the same |
| 39 | // as breaking only at space-equivalent characters where its former |
| 40 | // name (BREAK_SPACE) implied. |
| 41 | // |
| 42 | // Under BREAK_NEWLINE mode, all characters are included in the returned |
| 43 | // string, breaking only when a newline-equivalent character is encountered |
| 44 | // (eg. in the UTF-16 equivalent of the string "foo\nbar!\n\n", the line |
| 45 | // breaks are at the periods in ".foo\n.bar\n.\n."). |
| 46 | // |
| 47 | // Under BREAK_SENTENCE mode, all characters are included in the returned |
| 48 | // string, breaking only on sentence boundaries defined in "Unicode Standard |
| 49 | // Annex #29: Text Segmentation." Whitespace immediately following the sentence |
| 50 | // is also included. For example, in the UTF-16 equivalent of the string |
| 51 | // "foo bar! baz qux?" the breaks are at the periods in ".foo bar! .baz quz?." |
| 52 | // |
| 53 | // To extract the words from a string, move a BREAK_WORD BreakIterator |
| 54 | // through the string and test whether IsWord() is true. E.g., |
| 55 | // BreakIterator iter(str, BreakIterator::BREAK_WORD); |
| 56 | // if (!iter.Init()) |
| 57 | // return false; |
| 58 | // while (iter.Advance()) { |
| 59 | // if (iter.IsWord()) { |
| 60 | // // Region [iter.prev(), iter.pos()) contains a word. |
| 61 | // VLOG(1) << "word: " << iter.GetString(); |
| 62 | // } |
| 63 | // } |
| 64 |
| 65 | namespace base { |
| 66 | namespace i18n { |
| 67 |
| 68 | class BASE_I18N_EXPORT BreakIterator { |
| 69 | public: |
| 70 | enum BreakType { |
| 71 | BREAK_WORD, |
| 72 | BREAK_LINE, |
| 73 | // TODO(jshin): Remove this after reviewing call sites. |
| 74 | // If call sites really need break only on space-like characters |
| 75 | // implement it separately. |
| 76 | BREAK_SPACE = BREAK_LINE, |
| 77 | BREAK_NEWLINE, |
| 78 | BREAK_CHARACTER, |
| 79 | // But don't remove this one! |
| 80 | RULE_BASED, |
| 81 | BREAK_SENTENCE, |
| 82 | }; |
| 83 |
| 84 | enum WordBreakStatus { |
| 85 | // The end of text that the iterator recognizes as word characters. |
| 86 | // Non-word characters are things like punctuation and spaces. |
| 87 | IS_WORD_BREAK, |
| 88 | // Characters that the iterator can skip past, such as punctuation, |
| 89 | // whitespace, and, if using RULE_BASED mode, characters from another |
| 90 | // character set. |
| 91 | IS_SKIPPABLE_WORD, |
| 92 | // Only used if not in BREAK_WORD or RULE_BASED mode. This is returned for |
| 93 | // newlines, line breaks, and character breaks. |
| 94 | IS_LINE_OR_CHAR_BREAK |
| 95 | }; |
| 96 |
| 97 | // Requires |str| to live as long as the BreakIterator does. |
| 98 | BreakIterator(const StringPiece16& str, BreakType break_type); |
| 99 | // Make a rule-based iterator. BreakType == RULE_BASED is implied. |
| 100 | // TODO(andrewhayden): This signature could easily be misinterpreted as |
| 101 | // "(const string16& str, const string16& locale)". We should do something |
| 102 | // better. |
| 103 | BreakIterator(const StringPiece16& str, const string16& rules); |
| 104 | ~BreakIterator(); |
| 105 |
| 106 | // Init() must be called before any of the iterators are valid. |
| 107 | // Returns false if ICU failed to initialize. |
| 108 | bool Init(); |
| 109 |
| 110 | // Advance to the next break. Returns false if we've run past the end of |
| 111 | // the string. (Note that the very last "break" is after the final |
| 112 | // character in the string, and when we advance to that position it's the |
| 113 | // last time Advance() returns true.) |
| 114 | bool Advance(); |
| 115 |
| 116 | // Updates the text used by the iterator, resetting the iterator as if |
| 117 | // if Init() had been called again. Any old state is lost. Returns true |
| 118 | // unless there is an error setting the text. |
| 119 | bool SetText(const base::char16* text, const size_t length); |
| 120 |
| 121 | // Under BREAK_WORD mode, returns true if the break we just hit is the |
| 122 | // end of a word. (Otherwise, the break iterator just skipped over e.g. |
| 123 | // whitespace or punctuation.) Under BREAK_LINE and BREAK_NEWLINE modes, |
| 124 | // this distinction doesn't apply and it always returns false. |
| 125 | bool IsWord() const; |
| 126 |
| 127 | // Under BREAK_WORD mode: |
| 128 | // - Returns IS_SKIPPABLE_WORD if non-word characters, such as punctuation or |
| 129 | // spaces, are found. |
| 130 | // - Returns IS_WORD_BREAK if the break we just hit is the end of a sequence |
| 131 | // of word characters. |
| 132 | // Under RULE_BASED mode: |
| 133 | // - Returns IS_SKIPPABLE_WORD if characters outside the rules' character set |
| 134 | // or non-word characters, such as punctuation or spaces, are found. |
| 135 | // - Returns IS_WORD_BREAK if the break we just hit is the end of a sequence |
| 136 | // of word characters that are in the rules' character set. |
| 137 | // Not under BREAK_WORD or RULE_BASED mode: |
| 138 | // - Returns IS_LINE_OR_CHAR_BREAK. |
| 139 | BreakIterator::WordBreakStatus GetWordBreakStatus() const; |
| 140 |
| 141 | // Under BREAK_WORD mode, returns true if |position| is at the end of word or |
| 142 | // at the start of word. It always returns false under modes that are not |
| 143 | // BREAK_WORD or RULE_BASED. |
| 144 | bool IsEndOfWord(size_t position) const; |
| 145 | bool IsStartOfWord(size_t position) const; |
| 146 |
| 147 | // Under BREAK_SENTENCE mode, returns true if |position| is at a sentence |
| 148 | // boundary. It always returns false under modes that are not BREAK_SENTENCE |
| 149 | // or RULE_BASED. |
| 150 | bool IsSentenceBoundary(size_t position) const; |
| 151 |
| 152 | // Under BREAK_CHARACTER mode, returns whether |position| is a Unicode |
| 153 | // grapheme boundary. |
| 154 | bool IsGraphemeBoundary(size_t position) const; |
| 155 |
| 156 | // Returns the string between prev() and pos(). |
| 157 | // Advance() must have been called successfully at least once for pos() to |
| 158 | // have advanced to somewhere useful. |
| 159 | string16 GetString() const; |
| 160 |
| 161 | StringPiece16 GetStringPiece() const; |
| 162 |
| 163 | // Returns the value of pos() returned before Advance() was last called. |
| 164 | size_t prev() const { return prev_; } |
| 165 |
| 166 | // Returns the current break position within the string, |
| 167 | // or BreakIterator::npos when done. |
| 168 | size_t pos() const { return pos_; } |
| 169 |
| 170 | private: |
| 171 | // ICU iterator, avoiding ICU ubrk.h dependence. |
| 172 | // This is actually an ICU UBreakiterator* type, which turns out to be |
| 173 | // a typedef for a void* in the ICU headers. Using void* directly prevents |
| 174 | // callers from needing access to the ICU public headers directory. |
| 175 | void* iter_; |
| 176 |
| 177 | // The string we're iterating over. Can be changed with SetText(...) |
| 178 | StringPiece16 string_; |
| 179 |
| 180 | // Rules for our iterator. Mutually exclusive with break_type_. |
| 181 | const string16 rules_; |
| 182 |
| 183 | // The breaking style (word/space/newline). Mutually exclusive with rules_ |
| 184 | BreakType break_type_; |
| 185 |
| 186 | // Previous and current iterator positions. |
| 187 | size_t prev_, pos_; |
| 188 |
| 189 | DISALLOW_COPY_AND_ASSIGN(BreakIterator); |
| 190 | }; |
| 191 |
| 192 | } // namespace i18n |
| 193 | } // namespace base |
| 194 |
| 195 | #endif // BASE_I18N_BREAK_ITERATOR_H_ |
| 196 |