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