Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / base / i18n / break_iterator.h
blob8120d4729a22491dc9cdc1421d6aa1d4d1d5912d
1 // Copyright (c) 2011 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_BREAK_ITERATOR_H_
6 #define BASE_I18N_BREAK_ITERATOR_H_
8 #include "base/basictypes.h"
9 #include "base/i18n/base_i18n_export.h"
10 #include "base/strings/string16.h"
12 // The BreakIterator class iterates through the words, word breaks, and
13 // line breaks in a UTF-16 string.
15 // It provides several modes, BREAK_WORD, BREAK_LINE, and BREAK_NEWLINE,
16 // which modify how characters are aggregated into the returned string.
18 // Under BREAK_WORD mode, once a word is encountered any non-word
19 // characters are not included in the returned string (e.g. in the
20 // UTF-16 equivalent of the string " foo bar! ", the word breaks are at
21 // the periods in ". .foo. .bar.!. .").
22 // Note that Chinese/Japanese/Thai do not use spaces between words so that
23 // boundaries can fall in the middle of a continuous run of non-space /
24 // non-punctuation characters.
26 // Under BREAK_LINE mode, once a line breaking opportunity is encountered,
27 // any non-word characters are included in the returned string, breaking
28 // only when a space-equivalent character or a line breaking opportunity
29 // is encountered (e.g. in the UTF16-equivalent of the string " foo bar! ",
30 // the breaks are at the periods in ". .foo .bar! .").
32 // Note that lines can be broken at any character/syllable/grapheme cluster
33 // boundary in Chinese/Japanese/Korean and at word boundaries in Thai
34 // (Thai does not use spaces between words). Therefore, this is NOT the same
35 // as breaking only at space-equivalent characters where its former
36 // name (BREAK_SPACE) implied.
38 // Under BREAK_NEWLINE mode, all characters are included in the returned
39 // string, breaking only when a newline-equivalent character is encountered
40 // (eg. in the UTF-16 equivalent of the string "foo\nbar!\n\n", the line
41 // breaks are at the periods in ".foo\n.bar\n.\n.").
43 // To extract the words from a string, move a BREAK_WORD BreakIterator
44 // through the string and test whether IsWord() is true. E.g.,
45 // BreakIterator iter(str, BreakIterator::BREAK_WORD);
46 // if (!iter.Init())
47 // return false;
48 // while (iter.Advance()) {
49 // if (iter.IsWord()) {
50 // // Region [iter.prev(), iter.pos()) contains a word.
51 // VLOG(1) << "word: " << iter.GetString();
52 // }
53 // }
55 namespace base {
56 namespace i18n {
58 class BASE_I18N_EXPORT BreakIterator {
59 public:
60 enum BreakType {
61 BREAK_WORD,
62 BREAK_LINE,
63 // TODO(jshin): Remove this after reviewing call sites.
64 // If call sites really need break only on space-like characters
65 // implement it separately.
66 BREAK_SPACE = BREAK_LINE,
67 BREAK_NEWLINE,
68 BREAK_CHARACTER,
69 // But don't remove this one!
70 RULE_BASED,
73 // Requires |str| to live as long as the BreakIterator does.
74 BreakIterator(const string16& str, BreakType break_type);
75 // Make a rule-based iterator. BreakType == RULE_BASED is implied.
76 // TODO(andrewhayden): This signature could easily be misinterpreted as
77 // "(const string16& str, const string16& locale)". We should do something
78 // better.
79 BreakIterator(const string16& str, const string16& rules);
80 ~BreakIterator();
82 // Init() must be called before any of the iterators are valid.
83 // Returns false if ICU failed to initialize.
84 bool Init();
86 // Advance to the next break. Returns false if we've run past the end of
87 // the string. (Note that the very last "break" is after the final
88 // character in the string, and when we advance to that position it's the
89 // last time Advance() returns true.)
90 bool Advance();
92 // Updates the text used by the iterator, resetting the iterator as if
93 // if Init() had been called again. Any old state is lost. Returns true
94 // unless there is an error setting the text.
95 bool SetText(const base::char16* text, const size_t length);
97 // Under BREAK_WORD mode, returns true if the break we just hit is the
98 // end of a word. (Otherwise, the break iterator just skipped over e.g.
99 // whitespace or punctuation.) Under BREAK_LINE and BREAK_NEWLINE modes,
100 // this distinction doesn't apply and it always returns false.
101 bool IsWord() const;
103 // Under BREAK_WORD mode, returns true if |position| is at the end of word or
104 // at the start of word. It always returns false under BREAK_LINE and
105 // BREAK_NEWLINE modes.
106 bool IsEndOfWord(size_t position) const;
107 bool IsStartOfWord(size_t position) const;
109 // Under BREAK_CHARACTER mode, returns whether |position| is a Unicode
110 // grapheme boundary.
111 bool IsGraphemeBoundary(size_t position) const;
113 // Returns the string between prev() and pos().
114 // Advance() must have been called successfully at least once for pos() to
115 // have advanced to somewhere useful.
116 string16 GetString() const;
118 // Returns the value of pos() returned before Advance() was last called.
119 size_t prev() const { return prev_; }
121 // Returns the current break position within the string,
122 // or BreakIterator::npos when done.
123 size_t pos() const { return pos_; }
125 private:
126 // ICU iterator, avoiding ICU ubrk.h dependence.
127 // This is actually an ICU UBreakiterator* type, which turns out to be
128 // a typedef for a void* in the ICU headers. Using void* directly prevents
129 // callers from needing access to the ICU public headers directory.
130 void* iter_;
132 // The string we're iterating over. Can be changed with SetText(...)
133 const string16& string_;
135 // Rules for our iterator. Mutually exclusive with break_type_.
136 const string16 rules_;
138 // The breaking style (word/space/newline). Mutually exclusive with rules_
139 BreakType break_type_;
141 // Previous and current iterator positions.
142 size_t prev_, pos_;
144 DISALLOW_COPY_AND_ASSIGN(BreakIterator);
147 } // namespace i18n
148 } // namespace base
150 #endif // BASE_I18N_BREAK_ITERATOR_H_