| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 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 | 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/word_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 { |
| 13 |
| 12 const size_t npos = -1; | 14 const size_t npos = -1; |
| 13 | 15 |
| 14 WordIterator::WordIterator(const string16* str, BreakType break_type) | 16 BreakIterator::BreakIterator(const string16* str, BreakType break_type) |
| 15 : iter_(NULL), | 17 : iter_(NULL), |
| 16 string_(str), | 18 string_(str), |
| 17 break_type_(break_type), | 19 break_type_(break_type), |
| 18 prev_(npos), | 20 prev_(npos), |
| 19 pos_(0) { | 21 pos_(0) { |
| 20 } | 22 } |
| 21 | 23 |
| 22 WordIterator::~WordIterator() { | 24 BreakIterator::~BreakIterator() { |
| 23 if (iter_) | 25 if (iter_) |
| 24 ubrk_close(iter_); | 26 ubrk_close(iter_); |
| 25 } | 27 } |
| 26 | 28 |
| 27 bool WordIterator::Init() { | 29 bool BreakIterator::Init() { |
| 28 UErrorCode status = U_ZERO_ERROR; | 30 UErrorCode status = U_ZERO_ERROR; |
| 29 UBreakIteratorType break_type; | 31 UBreakIteratorType break_type; |
| 30 switch (break_type_) { | 32 switch (break_type_) { |
| 31 case BREAK_WORD: | 33 case BREAK_WORD: |
| 32 break_type = UBRK_WORD; | 34 break_type = UBRK_WORD; |
| 33 break; | 35 break; |
| 34 case BREAK_LINE: | 36 case BREAK_SPACE: |
| 35 break_type = UBRK_LINE; | 37 break_type = UBRK_LINE; |
| 36 break; | 38 break; |
| 37 default: | 39 default: |
| 38 NOTREACHED(); | 40 NOTREACHED(); |
| 39 break_type = UBRK_LINE; | 41 break_type = UBRK_LINE; |
| 40 } | 42 } |
| 41 iter_ = ubrk_open(break_type, NULL, | 43 iter_ = ubrk_open(break_type, NULL, |
| 42 string_->data(), static_cast<int32_t>(string_->size()), | 44 string_->data(), static_cast<int32_t>(string_->size()), |
| 43 &status); | 45 &status); |
| 44 if (U_FAILURE(status)) { | 46 if (U_FAILURE(status)) { |
| 45 NOTREACHED() << "ubrk_open failed"; | 47 NOTREACHED() << "ubrk_open failed"; |
| 46 return false; | 48 return false; |
| 47 } | 49 } |
| 48 ubrk_first(iter_); // Move the iterator to the beginning of the string. | 50 ubrk_first(iter_); // Move the iterator to the beginning of the string. |
| 49 return true; | 51 return true; |
| 50 } | 52 } |
| 51 | 53 |
| 52 bool WordIterator::Advance() { | 54 bool BreakIterator::Advance() { |
| 53 prev_ = pos_; | 55 prev_ = pos_; |
| 54 const int32_t pos = ubrk_next(iter_); | 56 const int32_t pos = ubrk_next(iter_); |
| 55 if (pos == UBRK_DONE) { | 57 if (pos == UBRK_DONE) { |
| 56 pos_ = npos; | 58 pos_ = npos; |
| 57 return false; | 59 return false; |
| 58 } else { | 60 } else { |
| 59 pos_ = static_cast<size_t>(pos); | 61 pos_ = static_cast<size_t>(pos); |
| 60 return true; | 62 return true; |
| 61 } | 63 } |
| 62 } | 64 } |
| 63 | 65 |
| 64 bool WordIterator::IsWord() const { | 66 bool BreakIterator::IsWord() const { |
| 65 return (ubrk_getRuleStatus(iter_) != UBRK_WORD_NONE); | 67 return (ubrk_getRuleStatus(iter_) != UBRK_WORD_NONE); |
| 66 } | 68 } |
| 67 | 69 |
| 68 string16 WordIterator::GetWord() const { | 70 string16 BreakIterator::GetWord() const { |
| 69 DCHECK(prev_ != npos && pos_ != npos); | 71 DCHECK(prev_ != npos && pos_ != npos); |
| 70 return string_->substr(prev_, pos_ - prev_); | 72 return string_->substr(prev_, pos_ - prev_); |
| 71 } | 73 } |
| 74 |
| 75 } // namespace base |
| OLD | NEW |