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