OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef BASE_I18N_WORD_ITERATOR_H_ | 5 #ifndef BASE_I18N_WORD_ITERATOR_H_ |
6 #define BASE_I18N_WORD_ITERATOR_H_ | 6 #define BASE_I18N_WORD_ITERATOR_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "unicode/ubrk.h" | |
12 #include "unicode/uchar.h" | |
13 | |
14 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
15 #include "base/string16.h" | 12 #include "base/string16.h" |
16 | 13 |
17 // The WordIterator class iterates through the words and word breaks | 14 // The WordIterator class iterates through the words and word breaks |
18 // in a string. (In the string " foo bar! ", the word breaks are at the | 15 // in a UTF-16 string. |
19 // periods in ". .foo. .bar.!. .".) | |
20 // | 16 // |
21 // To extract the words from a string, move a WordIterator through the | 17 // It provides two modes, BREAK_WORD and BREAK_LINE, which modify how |
22 // string and test whether IsWord() is true. E.g., | 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., |
23 // WordIterator iter(&str, WordIterator::BREAK_WORD); | 32 // WordIterator iter(&str, WordIterator::BREAK_WORD); |
24 // if (!iter.Init()) return false; | 33 // if (!iter.Init()) return false; |
25 // while (iter.Advance()) { | 34 // while (iter.Advance()) { |
26 // if (iter.IsWord()) { | 35 // if (iter.IsWord()) { |
27 // // region [iter.prev(),iter.pos()) contains a word. | 36 // // region [iter.prev(),iter.pos()) contains a word. |
28 // VLOG(1) << "word: " << iter.GetWord(); | 37 // VLOG(1) << "word: " << iter.GetWord(); |
29 // } | 38 // } |
30 // } | 39 // } |
31 | 40 |
32 | 41 |
(...skipping 28 matching lines...) Expand all Loading... |
61 // (Otherwise, the break iterator just skipped over e.g. whitespace | 70 // (Otherwise, the break iterator just skipped over e.g. whitespace |
62 // or punctuation.) | 71 // or punctuation.) |
63 bool IsWord() const; | 72 bool IsWord() const; |
64 | 73 |
65 // Return the word between prev() and pos(). | 74 // Return the word between prev() and pos(). |
66 // Advance() must have been called successfully at least once | 75 // Advance() must have been called successfully at least once |
67 // for pos() to have advanced to somewhere useful. | 76 // for pos() to have advanced to somewhere useful. |
68 string16 GetWord() const; | 77 string16 GetWord() const; |
69 | 78 |
70 private: | 79 private: |
71 // ICU iterator. | 80 // ICU iterator, avoiding ICU ubrk.h dependence. |
72 UBreakIterator* iter_; | 81 // This is actually an ICU UBreakiterator* type, which turns out to be |
73 #if !defined(WCHAR_T_IS_UTF16) | 82 // a typedef for a void* in the ICU headers. Using void* directly prevents |
74 std::vector<UChar> chars_; | 83 // callers from needing access to the ICU public headers directory. |
75 #endif | 84 void* iter_; |
76 | 85 |
77 // The string we're iterating over. | 86 // The string we're iterating over. |
78 const string16* string_; | 87 const string16* string_; |
79 | 88 |
80 // The breaking style (word/line). | 89 // The breaking style (word/line). |
81 BreakType break_type_; | 90 BreakType break_type_; |
82 | 91 |
83 // Previous and current iterator positions. | 92 // Previous and current iterator positions. |
84 size_t prev_, pos_; | 93 size_t prev_, pos_; |
85 | 94 |
86 DISALLOW_COPY_AND_ASSIGN(WordIterator); | 95 DISALLOW_COPY_AND_ASSIGN(WordIterator); |
87 }; | 96 }; |
88 | 97 |
89 #endif // BASE_I18N_WORD_ITERATOR_H__ | 98 #endif // BASE_I18N_WORD_ITERATOR_H__ |
OLD | NEW |