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

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
(Empty)
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
3 // found in the LICENSE file.
4
5 #ifndef BASE_I18N_BREAK_ITERATOR_H_
6 #define BASE_I18N_BREAK_ITERATOR_H_
7 #pragma once
8
9 #include "base/basictypes.h"
10 #include "base/string16.h"
11
12 // The BreakIterator class iterates through the words and word breaks
13 // in a UTF-16 string.
14 //
15 // It provides two modes, BREAK_WORD and BREAK_SPACE, which modify how
16 // trailing non-word characters are aggregated into the returned word.
17 //
18 // Under BREAK_WORD mode (more common), the non-word characters are
19 // not included with a returned word (e.g. in the UTF-16 equivalent of
20 // the string " foo bar! ", the word breaks are at the periods in
21 // ". .foo. .bar.!. .").
22 //
23 // Under BREAK_SPACE mode (less common), the non-word characters are
24 // included in the word, breaking only when a space-equivalent character
25 // is encountered (e.g. in the UTF16-equivalent of the string " foo bar! ",
26 // the word breaks are at the periods in ". .foo .bar! .").
27 //
28 // To extract the words from a string, move a BREAK_WORD BreakIterator
29 // through the string and test whether IsWord() is true. E.g.,
30 // BreakIterator iter(&str, BreakIterator::BREAK_WORD);
31 // if (!iter.Init()) return false;
32 // while (iter.Advance()) {
33 // if (iter.IsWord()) {
34 // // region [iter.prev(),iter.pos()) contains a word.
35 // VLOG(1) << "word: " << iter.GetString();
36 // }
37 // }
38
39 namespace base {
40
41 class BreakIterator {
42 public:
43 enum BreakType {
44 BREAK_WORD,
45 BREAK_SPACE
46 };
47
48 // Requires |str| to live as long as the BreakIterator does.
49 BreakIterator(const string16* str, BreakType break_type);
50 ~BreakIterator();
51
52 // Init() must be called before any of the iterators are valid.
53 // Returns false if ICU failed to initialize.
54 bool Init();
55
56 // Return the current break position within the string,
57 // or BreakIterator::npos when done.
58 size_t pos() const { return pos_; }
59 // Return the value of pos() returned before Advance() was last called.
60 size_t prev() const { return prev_; }
61
62 // Advance to the next break. Returns false if we've run past the end of
63 // the string. (Note that the very last "word break" is after the final
64 // character in the string, and when we advance to that position it's the
65 // last time Advance() returns true.)
66 bool Advance();
67
68 // Under BREAK_WORD mode, returns true if the break we just hit is the
69 // end of a word. (Otherwise, the break iterator just skipped over e.g.
70 // whitespace or punctuation.) Under BREAK_SPACE mode, this distinction
71 // doesn't apply and it always retuns false.
72 bool IsWord() const;
73
74 // Return the string between prev() and pos().
75 // Advance() must have been called successfully at least once
76 // for pos() to have advanced to somewhere useful.
77 string16 GetString() const;
78
79 private:
80 // ICU iterator, avoiding ICU ubrk.h dependence.
81 // 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
83 // callers from needing access to the ICU public headers directory.
84 void* iter_;
85
86 // The string we're iterating over.
87 const string16* string_;
88
89 // The breaking style (word/line).
90 BreakType break_type_;
91
92 // Previous and current iterator positions.
93 size_t prev_, pos_;
94
95 DISALLOW_COPY_AND_ASSIGN(BreakIterator);
96 };
97
98 } // namespace base
99
100 #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