Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(304)

Side by Side Diff: base/i18n/break_iterator.h

Issue 5796003: Refactor WordIterator into base::BreakIterator.... (Closed) Base URL: http://src.chromium.org/svn/trunk/src/
Patch Set: Created 10 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « base/base.gyp ('k') | base/i18n/break_iterator.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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_BREAK_ITERATOR_H_
6 #define BASE_I18N_WORD_ITERATOR_H_ 6 #define BASE_I18N_BREAK_ITERATOR_H_
7 #pragma once 7 #pragma once
8 8
9 #include <vector>
10
11 #include "base/basictypes.h" 9 #include "base/basictypes.h"
12 #include "base/string16.h" 10 #include "base/string16.h"
13 11
14 // The WordIterator class iterates through the words and word breaks 12 // The BreakIterator class iterates through the words and word breaks
15 // in a UTF-16 string. 13 // in a UTF-16 string.
16 // 14 //
17 // It provides two modes, BREAK_WORD and BREAK_LINE, which modify how 15 // It provides two modes, BREAK_WORD and BREAK_SPACE, which modify how
18 // trailing non-word characters are aggregated into the returned word. 16 // trailing non-word characters are aggregated into the returned word.
19 // 17 //
20 // Under BREAK_WORD mode (more common), the non-word characters are 18 // 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 19 // 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 20 // the string " foo bar! ", the word breaks are at the periods in
23 // ". .foo. .bar.!. ."). 21 // ". .foo. .bar.!. .").
24 // 22 //
25 // Under BREAK_LINE mode (less common), the non-word characters are 23 // Under BREAK_SPACE mode (less common), the non-word characters are
26 // included in the word, breaking only when a space-equivalent character 24 // 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! ", 25 // is encountered (e.g. in the UTF16-equivalent of the string " foo bar! ",
28 // the word breaks are at the periods in ". .foo .bar! ."). 26 // the word breaks are at the periods in ". .foo .bar! .").
29 // 27 //
30 // To extract the words from a string, move a BREAK_WORD WordIterator 28 // To extract the words from a string, move a BREAK_WORD BreakIterator
31 // through the string and test whether IsWord() is true. E.g., 29 // through the string and test whether IsWord() is true. E.g.,
32 // WordIterator iter(&str, WordIterator::BREAK_WORD); 30 // BreakIterator iter(&str, BreakIterator::BREAK_WORD);
33 // if (!iter.Init()) return false; 31 // if (!iter.Init()) return false;
34 // while (iter.Advance()) { 32 // while (iter.Advance()) {
35 // if (iter.IsWord()) { 33 // if (iter.IsWord()) {
36 // // region [iter.prev(),iter.pos()) contains a word. 34 // // region [iter.prev(),iter.pos()) contains a word.
37 // VLOG(1) << "word: " << iter.GetWord(); 35 // VLOG(1) << "word: " << iter.GetWord();
38 // } 36 // }
39 // } 37 // }
40 38
39 namespace base { // Prevent conflict with ICU BreakIterator
Evan Martin 2010/12/13 20:26:04 I guess this comment isn't necessary -- it's impli
41 40
42 class WordIterator { 41 class BreakIterator {
43 public: 42 public:
44 enum BreakType { 43 enum BreakType {
45 BREAK_WORD, 44 BREAK_WORD,
46 BREAK_LINE 45 BREAK_SPACE
47 }; 46 };
48 47
49 // Requires |str| to live as long as the WordIterator does. 48 // Requires |str| to live as long as the BreakIterator does.
50 WordIterator(const string16* str, BreakType break_type); 49 BreakIterator(const string16* str, BreakType break_type);
51 ~WordIterator(); 50 ~BreakIterator();
52 51
53 // Init() must be called before any of the iterators are valid. 52 // Init() must be called before any of the iterators are valid.
54 // Returns false if ICU failed to initialize. 53 // Returns false if ICU failed to initialize.
55 bool Init(); 54 bool Init();
56 55
57 // Return the current break position within the string, 56 // Return the current break position within the string,
58 // or WordIterator::npos when done. 57 // or BreakIterator::npos when done.
59 size_t pos() const { return pos_; } 58 size_t pos() const { return pos_; }
60 // Return the value of pos() returned before Advance() was last called. 59 // Return the value of pos() returned before Advance() was last called.
61 size_t prev() const { return prev_; } 60 size_t prev() const { return prev_; }
62 61
63 // Advance to the next break. Returns false if we've run past the end of 62 // 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 63 // 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 64 // character in the string, and when we advance to that position it's the
66 // last time Advance() returns true.) 65 // last time Advance() returns true.)
67 bool Advance(); 66 bool Advance();
68 67
69 // Returns true if the break we just hit is the end of a word. 68 // 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 69 // (Otherwise, the break iterator just skipped over e.g. whitespace
71 // or punctuation.) 70 // or punctuation.)
72 bool IsWord() const; 71 bool IsWord() const;
Evan Martin 2010/12/13 20:26:04 Maybe the comments on this function need to be upd
73 72
74 // Return the word between prev() and pos(). 73 // Return the word between prev() and pos().
75 // Advance() must have been called successfully at least once 74 // Advance() must have been called successfully at least once
76 // for pos() to have advanced to somewhere useful. 75 // for pos() to have advanced to somewhere useful.
77 string16 GetWord() const; 76 string16 GetWord() const;
78 77
79 private: 78 private:
80 // ICU iterator, avoiding ICU ubrk.h dependence. 79 // ICU iterator, avoiding ICU ubrk.h dependence.
81 // This is actually an ICU UBreakiterator* type, which turns out to be 80 // 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 81 // a typedef for a void* in the ICU headers. Using void* directly prevents
83 // callers from needing access to the ICU public headers directory. 82 // callers from needing access to the ICU public headers directory.
84 void* iter_; 83 void* iter_;
85 84
86 // The string we're iterating over. 85 // The string we're iterating over.
87 const string16* string_; 86 const string16* string_;
88 87
89 // The breaking style (word/line). 88 // The breaking style (word/line).
90 BreakType break_type_; 89 BreakType break_type_;
91 90
92 // Previous and current iterator positions. 91 // Previous and current iterator positions.
93 size_t prev_, pos_; 92 size_t prev_, pos_;
94 93
95 DISALLOW_COPY_AND_ASSIGN(WordIterator); 94 DISALLOW_COPY_AND_ASSIGN(BreakIterator);
96 }; 95 };
97 96
98 #endif // BASE_I18N_WORD_ITERATOR_H__ 97 } // namespace base
98
99 #endif // BASE_I18N_BREAK_ITERATOR_H__
OLDNEW
« no previous file with comments | « base/base.gyp ('k') | base/i18n/break_iterator.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698