Rebaseline svg/custom/foreignObject-crash-on-hover.xml.
[chromium-blink-merge.git] / base / i18n / word_iterator.h
blobc9648ca759781a97295578e346aeab455271262a
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_
8 #include <string>
9 #include <vector>
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();
27 // }
28 // }
31 class WordIterator {
32 public:
33 enum BreakType {
34 BREAK_WORD,
35 BREAK_LINE
38 // Requires |str| to live as long as the WordIterator does.
39 WordIterator(const std::wstring& str, BreakType break_type);
40 ~WordIterator();
42 // Init() must be called before any of the iterators are valid.
43 // Returns false if ICU failed to initialize.
44 bool Init();
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.)
56 bool Advance();
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
60 // or punctuation.)
61 bool IsWord() const;
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;
68 private:
69 // ICU iterator.
70 void* iter_;
71 #if !defined(WCHAR_T_IS_UTF16)
72 std::vector<UChar> chars_;
73 #endif
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.
82 size_t prev_, pos_;
84 DISALLOW_COPY_AND_ASSIGN(WordIterator);
87 #endif // BASE_I18N_WORD_ITERATOR_H__