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