| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef BASE_I18N_WORD_ITERATOR_H_ | |
| 6 #define BASE_I18N_WORD_ITERATOR_H_ | |
| 7 #pragma once | |
| 8 | |
| 9 #include <vector> | |
| 10 | |
| 11 #include "base/basictypes.h" | |
| 12 #include "base/string16.h" | |
| 13 | |
| 14 // The WordIterator class iterates through the words and word breaks | |
| 15 // in a UTF-16 string. | |
| 16 // | |
| 17 // It provides two modes, BREAK_WORD and BREAK_LINE, which modify how | |
| 18 // trailing non-word characters are aggregated into the returned word. | |
| 19 // | |
| 20 // Under BREAK_WORD mode (more common), the non-word characters are | |
| 21 // not included with a returned word (e.g. in the UTF-16 equivalent of | |
| 22 // the string " foo bar! ", the word breaks are at the periods in | |
| 23 // ". .foo. .bar.!. ."). | |
| 24 // | |
| 25 // Under BREAK_LINE mode (less common), the non-word characters are | |
| 26 // included in the word, breaking only when a space-equivalent character | |
| 27 // is encountered (e.g. in the UTF16-equivalent of the string " foo bar! ", | |
| 28 // the word breaks are at the periods in ". .foo .bar! ."). | |
| 29 // | |
| 30 // To extract the words from a string, move a BREAK_WORD WordIterator | |
| 31 // through the string and test whether IsWord() is true. E.g., | |
| 32 // WordIterator iter(&str, WordIterator::BREAK_WORD); | |
| 33 // if (!iter.Init()) return false; | |
| 34 // while (iter.Advance()) { | |
| 35 // if (iter.IsWord()) { | |
| 36 // // region [iter.prev(),iter.pos()) contains a word. | |
| 37 // VLOG(1) << "word: " << iter.GetWord(); | |
| 38 // } | |
| 39 // } | |
| 40 | |
| 41 | |
| 42 class WordIterator { | |
| 43 public: | |
| 44 enum BreakType { | |
| 45 BREAK_WORD, | |
| 46 BREAK_LINE | |
| 47 }; | |
| 48 | |
| 49 // Requires |str| to live as long as the WordIterator does. | |
| 50 WordIterator(const string16* str, BreakType break_type); | |
| 51 ~WordIterator(); | |
| 52 | |
| 53 // Init() must be called before any of the iterators are valid. | |
| 54 // Returns false if ICU failed to initialize. | |
| 55 bool Init(); | |
| 56 | |
| 57 // Return the current break position within the string, | |
| 58 // or WordIterator::npos when done. | |
| 59 size_t pos() const { return pos_; } | |
| 60 // Return the value of pos() returned before Advance() was last called. | |
| 61 size_t prev() const { return prev_; } | |
| 62 | |
| 63 // Advance to the next break. Returns false if we've run past the end of | |
| 64 // the string. (Note that the very last "word break" is after the final | |
| 65 // character in the string, and when we advance to that position it's the | |
| 66 // last time Advance() returns true.) | |
| 67 bool Advance(); | |
| 68 | |
| 69 // Returns true if the break we just hit is the end of a word. | |
| 70 // (Otherwise, the break iterator just skipped over e.g. whitespace | |
| 71 // or punctuation.) | |
| 72 bool IsWord() const; | |
| 73 | |
| 74 // Return the word between prev() and pos(). | |
| 75 // Advance() must have been called successfully at least once | |
| 76 // for pos() to have advanced to somewhere useful. | |
| 77 string16 GetWord() const; | |
| 78 | |
| 79 private: | |
| 80 // ICU iterator, avoiding ICU ubrk.h dependence. | |
| 81 // This is actually an ICU UBreakiterator* type, which turns out to be | |
| 82 // a typedef for a void* in the ICU headers. Using void* directly prevents | |
| 83 // callers from needing access to the ICU public headers directory. | |
| 84 void* iter_; | |
| 85 | |
| 86 // The string we're iterating over. | |
| 87 const string16* string_; | |
| 88 | |
| 89 // The breaking style (word/line). | |
| 90 BreakType break_type_; | |
| 91 | |
| 92 // Previous and current iterator positions. | |
| 93 size_t prev_, pos_; | |
| 94 | |
| 95 DISALLOW_COPY_AND_ASSIGN(WordIterator); | |
| 96 }; | |
| 97 | |
| 98 #endif // BASE_I18N_WORD_ITERATOR_H__ | |
| OLD | NEW |