1 // Copyright (c) 2009 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.
5 #ifndef BASE_I18N_WORD_ITERATOR_H_
6 #define BASE_I18N_WORD_ITERATOR_H_
11 #include "unicode/uchar.h"
13 #include "base/basictypes.h"
15 // The WordIterator class iterates through the words and word breaks
16 // in a string. (In the string " foo bar! ", the word breaks are at the
17 // periods in ". .foo. .bar.!. .".)
19 // To extract the words from a string, move a WordIterator through the
20 // string and test whether IsWord() is true. E.g.,
21 // WordIterator iter(str, WordIterator::BREAK_WORD);
22 // if (!iter.Init()) return false;
23 // while (iter.Advance()) {
24 // if (iter.IsWord()) {
25 // // region [iter.prev(),iter.pos()) contains a word.
26 // LOG(INFO) << "word: " << iter.GetWord();
38 // Requires |str| to live as long as the WordIterator does.
39 WordIterator(const std::wstring
& str
, BreakType break_type
);
42 // Init() must be called before any of the iterators are valid.
43 // Returns false if ICU failed to initialize.
46 // Return the current break position within the string,
47 // or WordIterator::npos when done.
48 size_t pos() const { return pos_
; }
49 // Return the value of pos() returned before Advance() was last called.
50 size_t prev() const { return prev_
; }
52 // Advance to the next break. Returns false if we've run past the end of
53 // the string. (Note that the very last "word break" is after the final
54 // character in the string, and when we advance to that position it's the
55 // last time Advance() returns true.)
58 // Returns true if the break we just hit is the end of a word.
59 // (Otherwise, the break iterator just skipped over e.g. whitespace
63 // Return the word between prev() and pos().
64 // Advance() must have been called successfully at least once
65 // for pos() to have advanced to somewhere useful.
66 std::wstring
GetWord() const;
71 #if !defined(WCHAR_T_IS_UTF16)
72 std::vector
<UChar
> chars_
;
75 // The string we're iterating over.
76 const std::wstring
& string_
;
78 // The breaking style (word/line).
79 BreakType break_type_
;
81 // Previous and current iterator positions.
84 DISALLOW_COPY_AND_ASSIGN(WordIterator
);
87 #endif // BASE_I18N_WORD_ITERATOR_H__