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
3 // found in the LICENSE file.
5 #include "base/i18n/break_iterator.h"
9 #include "base/logging.h"
10 #include "third_party/icu/source/common/unicode/ubrk.h"
11 #include "third_party/icu/source/common/unicode/uchar.h"
12 #include "third_party/icu/source/common/unicode/ustring.h"
17 const size_t npos = static_cast<size_t>(-1);
19 BreakIterator::BreakIterator(const StringPiece16& str, BreakType break_type)
22 break_type_(break_type),
26 BreakIterator::BreakIterator(const StringPiece16& str, const string16& rules)
30 break_type_(RULE_BASED),
34 BreakIterator::~BreakIterator() {
36 ubrk_close(static_cast<UBreakIterator*>(iter_));
39 bool BreakIterator::Init() {
40 UErrorCode status = U_ZERO_ERROR;
41 UParseError parse_error;
42 UBreakIteratorType break_type;
43 switch (break_type_) {
45 break_type = UBRK_CHARACTER;
48 break_type = UBRK_WORD;
52 case RULE_BASED: // (Keep compiler happy, break_type not used in this case)
53 break_type = UBRK_LINE;
56 NOTREACHED() << "invalid break_type_";
59 if (break_type_ == RULE_BASED) {
60 iter_ = ubrk_openRules(rules_.c_str(),
61 static_cast<int32_t>(rules_.length()),
63 static_cast<int32_t>(string_.size()),
66 if (U_FAILURE(status)) {
67 NOTREACHED() << "ubrk_openRules failed to parse rule string at line "
68 << parse_error.line << ", offset " << parse_error.offset;
71 iter_ = ubrk_open(break_type, nullptr, string_.data(),
72 static_cast<int32_t>(string_.size()), &status);
73 if (U_FAILURE(status)) {
74 NOTREACHED() << "ubrk_open failed for type " << break_type
75 << " with error " << status;
79 if (U_FAILURE(status)) {
83 // Move the iterator to the beginning of the string.
84 ubrk_first(static_cast<UBreakIterator*>(iter_));
88 bool BreakIterator::Advance() {
92 switch (break_type_) {
97 pos = ubrk_next(static_cast<UBreakIterator*>(iter_));
98 if (pos == UBRK_DONE) {
102 pos_ = static_cast<size_t>(pos);
106 pos = ubrk_next(static_cast<UBreakIterator*>(iter_));
107 if (pos == UBRK_DONE)
109 pos_ = static_cast<size_t>(pos);
110 status = ubrk_getRuleStatus(static_cast<UBreakIterator*>(iter_));
111 } while (status >= UBRK_LINE_SOFT && status < UBRK_LINE_SOFT_LIMIT);
112 if (pos == UBRK_DONE && prev_ == pos_) {
118 NOTREACHED() << "invalid break_type_";
123 bool BreakIterator::SetText(const base::char16* text, const size_t length) {
124 UErrorCode status = U_ZERO_ERROR;
125 ubrk_setText(static_cast<UBreakIterator*>(iter_),
126 text, length, &status);
127 pos_ = 0; // implicit when ubrk_setText is done
129 if (U_FAILURE(status)) {
130 NOTREACHED() << "ubrk_setText failed";
133 string_ = StringPiece16(text, length);
137 bool BreakIterator::IsWord() const {
138 return GetWordBreakStatus() == IS_WORD_BREAK;
141 BreakIterator::WordBreakStatus BreakIterator::GetWordBreakStatus() const {
142 int32_t status = ubrk_getRuleStatus(static_cast<UBreakIterator*>(iter_));
143 if (break_type_ != BREAK_WORD && break_type_ != RULE_BASED)
144 return IS_LINE_OR_CHAR_BREAK;
145 // In ICU 60, trying to advance past the end of the text does not change
146 // |status| so that |pos_| has to be checked as well as |status|.
147 // See http://bugs.icu-project.org/trac/ticket/13447 .
148 return (status == UBRK_WORD_NONE || pos_ == npos) ? IS_SKIPPABLE_WORD
152 bool BreakIterator::IsEndOfWord(size_t position) const {
153 if (break_type_ != BREAK_WORD && break_type_ != RULE_BASED)
156 UBreakIterator* iter = static_cast<UBreakIterator*>(iter_);
157 UBool boundary = ubrk_isBoundary(iter, static_cast<int32_t>(position));
158 int32_t status = ubrk_getRuleStatus(iter);
159 return (!!boundary && status != UBRK_WORD_NONE);
162 bool BreakIterator::IsStartOfWord(size_t position) const {
163 if (break_type_ != BREAK_WORD && break_type_ != RULE_BASED)
166 UBreakIterator* iter = static_cast<UBreakIterator*>(iter_);
167 UBool boundary = ubrk_isBoundary(iter, static_cast<int32_t>(position));
169 int32_t next_status = ubrk_getRuleStatus(iter);
170 return (!!boundary && next_status != UBRK_WORD_NONE);
173 bool BreakIterator::IsGraphemeBoundary(size_t position) const {
174 if (break_type_ != BREAK_CHARACTER)
177 UBreakIterator* iter = static_cast<UBreakIterator*>(iter_);
178 return !!ubrk_isBoundary(iter, static_cast<int32_t>(position));
181 string16 BreakIterator::GetString() const {
182 return GetStringPiece().as_string();
185 StringPiece16 BreakIterator::GetStringPiece() const {
186 DCHECK(prev_ != npos && pos_ != npos);
187 return string_.substr(prev_, pos_ - prev_);