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