| 6 |
| 7 | #include <stdint.h> |
| 8 |
| 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" |
| 13 |
| 14 | namespace base { |
| 15 | namespace i18n { |
| 16 |
| 17 | const size_t npos = static_cast<size_t>(-1); |
| 18 |
| 19 | BreakIterator::BreakIterator(const StringPiece16& str, BreakType break_type) |
| 20 | : iter_(nullptr), |
| 21 | string_(str), |
| 22 | break_type_(break_type), |
| 23 | prev_(npos), |
| 24 | pos_(0) {} |
| 25 |
| 26 | BreakIterator::BreakIterator(const StringPiece16& str, const string16& rules) |
| 27 | : iter_(nullptr), |
| 28 | string_(str), |
| 29 | rules_(rules), |
| 30 | break_type_(RULE_BASED), |
| 31 | prev_(npos), |
| 32 | pos_(0) {} |
| 33 |
| 34 | BreakIterator::~BreakIterator() { |
| 35 | if (iter_) |
| 36 | ubrk_close(static_cast<UBreakIterator*>(iter_)); |
| 37 | } |
| 38 |
| 39 | bool BreakIterator::Init() { |
| 40 | UErrorCode status = U_ZERO_ERROR; |
| 41 | UParseError parse_error; |
| 42 | UBreakIteratorType break_type; |
| 43 | switch (break_type_) { |
| 44 | case BREAK_CHARACTER: |
| 45 | break_type = UBRK_CHARACTER; |
| 46 | break; |
| 47 | case BREAK_WORD: |
| 48 | break_type = UBRK_WORD; |
| 49 | break; |
| 50 | case BREAK_SENTENCE: |
| 51 | break_type = UBRK_SENTENCE; |
| 52 | break; |
| 53 | case BREAK_LINE: |
| 54 | case BREAK_NEWLINE: |
| 55 | case RULE_BASED: // (Keep compiler happy, break_type not used in this case) |
| 56 | break_type = UBRK_LINE; |
| 57 | break; |
| 58 | default: |
| 59 | NOTREACHED() << "invalid break_type_"; |
| 60 | return false; |
| 61 | } |
| 62 | if (break_type_ == RULE_BASED) { |
| 63 | iter_ = ubrk_openRules(rules_.c_str(), |
| 64 | static_cast<int32_t>(rules_.length()), |
| 65 | string_.data(), |
| 66 | static_cast<int32_t>(string_.size()), |
| 67 | &parse_error, |
| 68 | &status); |
| 69 | if (U_FAILURE(status)) { |
| 70 | NOTREACHED() << "ubrk_openRules failed to parse rule string at line " |
| 71 | << parse_error.line << ", offset " << parse_error.offset; |
| 72 | } |
| 73 | } else { |
| 74 | iter_ = ubrk_open(break_type, nullptr, string_.data(), |
| 75 | static_cast<int32_t>(string_.size()), &status); |
| 76 | if (U_FAILURE(status)) { |
| 77 | NOTREACHED() << "ubrk_open failed for type " << break_type |
| 78 | << " with error " << status; |
| 79 | } |
| 80 | } |
| 81 |
| 82 | if (U_FAILURE(status)) { |
| 83 | return false; |
| 84 | } |
| 85 |
| 86 | // Move the iterator to the beginning of the string. |
| 87 | ubrk_first(static_cast<UBreakIterator*>(iter_)); |
| 88 | return true; |
| 89 | } |
| 90 |
| 91 | bool BreakIterator::Advance() { |
| 92 | int32_t pos; |
| 93 | int32_t status; |
| 94 | prev_ = pos_; |
| 95 | switch (break_type_) { |
| 96 | case BREAK_CHARACTER: |
| 97 | case BREAK_WORD: |
| 98 | case BREAK_LINE: |
| 99 | case BREAK_SENTENCE: |
| 100 | case RULE_BASED: |
| 101 | pos = ubrk_next(static_cast<UBreakIterator*>(iter_)); |
| 102 | if (pos == UBRK_DONE) { |
| 103 | pos_ = npos; |
| 104 | return false; |
| 105 | } |
| 106 | pos_ = static_cast<size_t>(pos); |
| 107 | return true; |
| 108 | case BREAK_NEWLINE: |
| 109 | do { |
| 110 | pos = ubrk_next(static_cast<UBreakIterator*>(iter_)); |
| 111 | if (pos == UBRK_DONE) |
| 112 | break; |
| 113 | pos_ = static_cast<size_t>(pos); |
| 114 | status = ubrk_getRuleStatus(static_cast<UBreakIterator*>(iter_)); |
| 115 | } while (status >= UBRK_LINE_SOFT && status < UBRK_LINE_SOFT_LIMIT); |
| 116 | if (pos == UBRK_DONE && prev_ == pos_) { |
| 117 | pos_ = npos; |
| 118 | return false; |
| 119 | } |
| 120 | return true; |
| 121 | default: |
| 122 | NOTREACHED() << "invalid break_type_"; |
| 123 | return false; |
| 124 | } |
| 125 | } |
| 126 |
| 127 | bool BreakIterator::SetText(const base::char16* text, const size_t length) { |
| 128 | UErrorCode status = U_ZERO_ERROR; |
| 129 | ubrk_setText(static_cast<UBreakIterator*>(iter_), |
| 130 | text, length, &status); |
| 131 | pos_ = 0; // implicit when ubrk_setText is done |
| 132 | prev_ = npos; |
| 133 | if (U_FAILURE(status)) { |
| 134 | NOTREACHED() << "ubrk_setText failed"; |
| 135 | return false; |
| 136 | } |
| 137 | string_ = StringPiece16(text, length); |
| 138 | return true; |
| 139 | } |
| 140 |
| 141 | bool BreakIterator::IsWord() const { |
| 142 | return GetWordBreakStatus() == IS_WORD_BREAK; |
| 143 | } |
| 144 |
| 145 | BreakIterator::WordBreakStatus BreakIterator::GetWordBreakStatus() const { |
| 146 | int32_t status = ubrk_getRuleStatus(static_cast<UBreakIterator*>(iter_)); |
| 147 | if (break_type_ != BREAK_WORD && break_type_ != RULE_BASED) |
| 148 | return IS_LINE_OR_CHAR_BREAK; |
| 149 | // In ICU 60, trying to advance past the end of the text does not change |
| 150 | // |status| so that |pos_| has to be checked as well as |status|. |
| 151 | // See http://bugs.icu-project.org/trac/ticket/13447 . |
| 152 | return (status == UBRK_WORD_NONE || pos_ == npos) ? IS_SKIPPABLE_WORD |
| 153 | : IS_WORD_BREAK; |
| 154 | } |
| 155 |
| 156 | bool BreakIterator::IsEndOfWord(size_t position) const { |
| 157 | if (break_type_ != BREAK_WORD && break_type_ != RULE_BASED) |
| 158 | return false; |
| 159 |
| 160 | UBreakIterator* iter = static_cast<UBreakIterator*>(iter_); |
| 161 | UBool boundary = ubrk_isBoundary(iter, static_cast<int32_t>(position)); |
| 162 | int32_t status = ubrk_getRuleStatus(iter); |
| 163 | return (!!boundary && status != UBRK_WORD_NONE); |
| 164 | } |
| 165 |
| 166 | bool BreakIterator::IsStartOfWord(size_t position) const { |
| 167 | if (break_type_ != BREAK_WORD && break_type_ != RULE_BASED) |
| 168 | return false; |
| 169 |
| 170 | UBreakIterator* iter = static_cast<UBreakIterator*>(iter_); |
| 171 | UBool boundary = ubrk_isBoundary(iter, static_cast<int32_t>(position)); |
| 172 | ubrk_next(iter); |
| 173 | int32_t next_status = ubrk_getRuleStatus(iter); |
| 174 | return (!!boundary && next_status != UBRK_WORD_NONE); |
| 175 | } |
| 176 |
| 177 | bool BreakIterator::IsSentenceBoundary(size_t position) const { |
| 178 | if (break_type_ != BREAK_SENTENCE && break_type_ != RULE_BASED) |
| 179 | return false; |
| 180 |
| 181 | UBreakIterator* iter = static_cast<UBreakIterator*>(iter_); |
| 182 | return !!ubrk_isBoundary(iter, static_cast<int32_t>(position)); |
| 183 | } |
| 184 |
| 185 | bool BreakIterator::IsGraphemeBoundary(size_t position) const { |
| 186 | if (break_type_ != BREAK_CHARACTER) |
| 187 | return false; |
| 188 |
| 189 | UBreakIterator* iter = static_cast<UBreakIterator*>(iter_); |
| 190 | return !!ubrk_isBoundary(iter, static_cast<int32_t>(position)); |
| 191 | } |
| 192 |
| 193 | string16 BreakIterator::GetString() const { |
| 194 | return GetStringPiece().as_string(); |
| 195 | } |
| 196 |
| 197 | StringPiece16 BreakIterator::GetStringPiece() const { |
| 198 | DCHECK(prev_ != npos && pos_ != npos); |
| 199 | return string_.substr(prev_, pos_ - prev_); |
| 200 | } |
| 201 |
| 202 | } // namespace i18n |
| 203 | } // namespace base |
| 204 |