OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/i18n/break_iterator.h" | 5 #include "base/i18n/break_iterator.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "unicode/ubrk.h" | 8 #include "unicode/ubrk.h" |
9 #include "unicode/uchar.h" | 9 #include "unicode/uchar.h" |
10 #include "unicode/ustring.h" | 10 #include "unicode/ustring.h" |
11 | 11 |
12 namespace base { | 12 namespace base { |
13 namespace i18n { | 13 namespace i18n { |
14 | 14 |
15 const size_t npos = -1; | 15 const size_t npos = -1; |
16 | 16 |
17 BreakIterator::BreakIterator(const string16* str, BreakType break_type) | 17 BreakIterator::BreakIterator(const string16& str, BreakType break_type) |
18 : iter_(NULL), | 18 : iter_(NULL), |
19 string_(str), | 19 string_(str), |
20 break_type_(break_type), | 20 break_type_(break_type), |
21 prev_(npos), | 21 prev_(npos), |
22 pos_(0) { | 22 pos_(0) { |
23 } | 23 } |
24 | 24 |
25 BreakIterator::~BreakIterator() { | 25 BreakIterator::~BreakIterator() { |
26 if (iter_) | 26 if (iter_) |
27 ubrk_close(static_cast<UBreakIterator*>(iter_)); | 27 ubrk_close(static_cast<UBreakIterator*>(iter_)); |
28 } | 28 } |
29 | 29 |
30 bool BreakIterator::Init() { | 30 bool BreakIterator::Init() { |
31 UErrorCode status = U_ZERO_ERROR; | 31 UErrorCode status = U_ZERO_ERROR; |
32 UBreakIteratorType break_type; | 32 UBreakIteratorType break_type; |
33 switch (break_type_) { | 33 switch (break_type_) { |
34 case BREAK_WORD: | 34 case BREAK_WORD: |
35 break_type = UBRK_WORD; | 35 break_type = UBRK_WORD; |
36 break; | 36 break; |
37 case BREAK_LINE: | 37 case BREAK_LINE: |
38 case BREAK_NEWLINE: | 38 case BREAK_NEWLINE: |
39 break_type = UBRK_LINE; | 39 break_type = UBRK_LINE; |
40 break; | 40 break; |
41 default: | 41 default: |
42 NOTREACHED() << "invalid break_type_"; | 42 NOTREACHED() << "invalid break_type_"; |
43 return false; | 43 return false; |
44 } | 44 } |
45 iter_ = ubrk_open(break_type, NULL, | 45 iter_ = ubrk_open(break_type, NULL, |
46 string_->data(), static_cast<int32_t>(string_->size()), | 46 string_.data(), static_cast<int32_t>(string_.size()), |
47 &status); | 47 &status); |
48 if (U_FAILURE(status)) { | 48 if (U_FAILURE(status)) { |
49 NOTREACHED() << "ubrk_open failed"; | 49 NOTREACHED() << "ubrk_open failed"; |
50 return false; | 50 return false; |
51 } | 51 } |
52 // Move the iterator to the beginning of the string. | 52 // Move the iterator to the beginning of the string. |
53 ubrk_first(static_cast<UBreakIterator*>(iter_)); | 53 ubrk_first(static_cast<UBreakIterator*>(iter_)); |
54 return true; | 54 return true; |
55 } | 55 } |
56 | 56 |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
89 } | 89 } |
90 | 90 |
91 bool BreakIterator::IsWord() const { | 91 bool BreakIterator::IsWord() const { |
92 return (break_type_ == BREAK_WORD && | 92 return (break_type_ == BREAK_WORD && |
93 ubrk_getRuleStatus(static_cast<UBreakIterator*>(iter_)) != | 93 ubrk_getRuleStatus(static_cast<UBreakIterator*>(iter_)) != |
94 UBRK_WORD_NONE); | 94 UBRK_WORD_NONE); |
95 } | 95 } |
96 | 96 |
97 string16 BreakIterator::GetString() const { | 97 string16 BreakIterator::GetString() const { |
98 DCHECK(prev_ != npos && pos_ != npos); | 98 DCHECK(prev_ != npos && pos_ != npos); |
99 return string_->substr(prev_, pos_ - prev_); | 99 return string_.substr(prev_, pos_ - prev_); |
100 } | 100 } |
101 | 101 |
102 } // namespace i18n | 102 } // namespace i18n |
103 } // namespace base | 103 } // namespace base |
OLD | NEW |