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

Side by Side Diff: base/i18n/break_iterator.cc

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/i18n/break_iterator.h ('k') | base/i18n/break_iterator_unittest.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) 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.
4
5 #include "base/i18n/break_iterator.h"
6
7 #include "base/logging.h"
8 #include "unicode/ubrk.h"
9 #include "unicode/uchar.h"
10 #include "unicode/ustring.h"
11
12 namespace base {
13
14 const size_t npos = -1;
15
16 BreakIterator::BreakIterator(const string16* str, BreakType break_type)
17 : iter_(NULL),
18 string_(str),
19 break_type_(break_type),
20 prev_(npos),
21 pos_(0) {
22 }
23
24 BreakIterator::~BreakIterator() {
25 if (iter_)
26 ubrk_close(iter_);
27 }
28
29 bool BreakIterator::Init() {
30 UErrorCode status = U_ZERO_ERROR;
31 UBreakIteratorType break_type;
32 switch (break_type_) {
33 case BREAK_WORD:
34 break_type = UBRK_WORD;
35 break;
36 case BREAK_SPACE:
37 break_type = UBRK_LINE;
38 break;
39 default:
40 NOTREACHED();
41 break_type = UBRK_LINE;
42 }
43 iter_ = ubrk_open(break_type, NULL,
44 string_->data(), static_cast<int32_t>(string_->size()),
45 &status);
46 if (U_FAILURE(status)) {
47 NOTREACHED() << "ubrk_open failed";
48 return false;
49 }
50 ubrk_first(iter_); // Move the iterator to the beginning of the string.
51 return true;
52 }
53
54 bool BreakIterator::Advance() {
55 prev_ = pos_;
56 const int32_t pos = ubrk_next(iter_);
57 if (pos == UBRK_DONE) {
58 pos_ = npos;
59 return false;
60 } else {
61 pos_ = static_cast<size_t>(pos);
62 return true;
63 }
64 }
65
66 bool BreakIterator::IsWord() const {
67 return (break_type_ == BREAK_WORD &&
68 ubrk_getRuleStatus(iter_) != UBRK_WORD_NONE);
69 }
70
71 string16 BreakIterator::GetString() const {
72 DCHECK(prev_ != npos && pos_ != npos);
73 return string_->substr(prev_, pos_ - prev_);
74 }
75
76 } // namespace base
OLDNEW
« no previous file with comments | « base/i18n/break_iterator.h ('k') | base/i18n/break_iterator_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698