OLD | NEW |
1 // Copyright (c) 2009 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 |
(...skipping 14 matching lines...) Expand all Loading... |
26 ubrk_close(static_cast<UBreakIterator*>(iter_)); | 26 ubrk_close(static_cast<UBreakIterator*>(iter_)); |
27 } | 27 } |
28 | 28 |
29 bool BreakIterator::Init() { | 29 bool BreakIterator::Init() { |
30 UErrorCode status = U_ZERO_ERROR; | 30 UErrorCode status = U_ZERO_ERROR; |
31 UBreakIteratorType break_type; | 31 UBreakIteratorType break_type; |
32 switch (break_type_) { | 32 switch (break_type_) { |
33 case BREAK_WORD: | 33 case BREAK_WORD: |
34 break_type = UBRK_WORD; | 34 break_type = UBRK_WORD; |
35 break; | 35 break; |
36 case BREAK_SPACE: | 36 case BREAK_LINE: |
37 case BREAK_NEWLINE: | 37 case BREAK_NEWLINE: |
38 break_type = UBRK_LINE; | 38 break_type = UBRK_LINE; |
39 break; | 39 break; |
40 default: | 40 default: |
41 NOTREACHED() << "invalid break_type_"; | 41 NOTREACHED() << "invalid break_type_"; |
42 return false; | 42 return false; |
43 } | 43 } |
44 iter_ = ubrk_open(break_type, NULL, | 44 iter_ = ubrk_open(break_type, NULL, |
45 string_->data(), static_cast<int32_t>(string_->size()), | 45 string_->data(), static_cast<int32_t>(string_->size()), |
46 &status); | 46 &status); |
47 if (U_FAILURE(status)) { | 47 if (U_FAILURE(status)) { |
48 NOTREACHED() << "ubrk_open failed"; | 48 NOTREACHED() << "ubrk_open failed"; |
49 return false; | 49 return false; |
50 } | 50 } |
51 // Move the iterator to the beginning of the string. | 51 // Move the iterator to the beginning of the string. |
52 ubrk_first(static_cast<UBreakIterator*>(iter_)); | 52 ubrk_first(static_cast<UBreakIterator*>(iter_)); |
53 return true; | 53 return true; |
54 } | 54 } |
55 | 55 |
56 bool BreakIterator::Advance() { | 56 bool BreakIterator::Advance() { |
57 int32_t pos; | 57 int32_t pos; |
58 int32_t status; | 58 int32_t status; |
59 prev_ = pos_; | 59 prev_ = pos_; |
60 switch (break_type_) { | 60 switch (break_type_) { |
61 case BREAK_WORD: | 61 case BREAK_WORD: |
62 case BREAK_SPACE: | 62 case BREAK_LINE: |
63 pos = ubrk_next(static_cast<UBreakIterator*>(iter_)); | 63 pos = ubrk_next(static_cast<UBreakIterator*>(iter_)); |
64 if (pos == UBRK_DONE) { | 64 if (pos == UBRK_DONE) { |
65 pos_ = npos; | 65 pos_ = npos; |
66 return false; | 66 return false; |
67 } | 67 } |
68 pos_ = static_cast<size_t>(pos); | 68 pos_ = static_cast<size_t>(pos); |
69 return true; | 69 return true; |
70 case BREAK_NEWLINE: | 70 case BREAK_NEWLINE: |
71 do { | 71 do { |
72 pos = ubrk_next(static_cast<UBreakIterator*>(iter_)); | 72 pos = ubrk_next(static_cast<UBreakIterator*>(iter_)); |
(...skipping 19 matching lines...) Expand all Loading... |
92 ubrk_getRuleStatus(static_cast<UBreakIterator*>(iter_)) != | 92 ubrk_getRuleStatus(static_cast<UBreakIterator*>(iter_)) != |
93 UBRK_WORD_NONE); | 93 UBRK_WORD_NONE); |
94 } | 94 } |
95 | 95 |
96 string16 BreakIterator::GetString() const { | 96 string16 BreakIterator::GetString() const { |
97 DCHECK(prev_ != npos && pos_ != npos); | 97 DCHECK(prev_ != npos && pos_ != npos); |
98 return string_->substr(prev_, pos_ - prev_); | 98 return string_->substr(prev_, pos_ - prev_); |
99 } | 99 } |
100 | 100 |
101 } // namespace base | 101 } // namespace base |
OLD | NEW |