break_iterator.cc 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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.
  4. #include "base/i18n/break_iterator.h"
  5. #include <stdint.h>
  6. #include <ostream>
  7. #include "base/check.h"
  8. #include "base/lazy_instance.h"
  9. #include "base/memory/raw_ptr.h"
  10. #include "base/notreached.h"
  11. #include "base/synchronization/lock.h"
  12. #include "third_party/icu/source/common/unicode/ubrk.h"
  13. #include "third_party/icu/source/common/unicode/uchar.h"
  14. #include "third_party/icu/source/common/unicode/ustring.h"
  15. namespace base {
  16. namespace i18n {
  17. const size_t npos = static_cast<size_t>(-1);
  18. BreakIterator::BreakIterator(const StringPiece16& str, BreakType break_type)
  19. : iter_(nullptr),
  20. string_(str),
  21. break_type_(break_type),
  22. prev_(npos),
  23. pos_(0) {}
  24. BreakIterator::BreakIterator(const StringPiece16& str,
  25. const std::u16string& rules)
  26. : iter_(nullptr),
  27. string_(str),
  28. rules_(rules),
  29. break_type_(RULE_BASED),
  30. prev_(npos),
  31. pos_(0) {}
  32. namespace {
  33. // We found the usage pattern of break iterator is to create, use and destroy.
  34. // The following cache support multiple break iterator in the same thread and
  35. // also optimize to not create break iterator many time. For each kind of break
  36. // iterator (character, word, line and sentence, but NOT rule), we keep one of
  37. // them in the main_ and lease it out. If some other code request a lease
  38. // before |main_| is returned, we create a new instance of the iterator.
  39. // This will keep at most 4 break iterators (one for each kind) unreleased until
  40. // the program destruction time.
  41. template <UBreakIteratorType break_type>
  42. class DefaultLocaleBreakIteratorCache {
  43. public:
  44. DefaultLocaleBreakIteratorCache()
  45. : main_status_(U_ZERO_ERROR),
  46. main_(nullptr),
  47. main_could_be_leased_(true) {
  48. main_ = ubrk_open(break_type, nullptr, nullptr, 0, &main_status_);
  49. if (U_FAILURE(main_status_)) {
  50. NOTREACHED() << "ubrk_open failed for type " << break_type
  51. << " with error " << main_status_;
  52. }
  53. }
  54. virtual ~DefaultLocaleBreakIteratorCache() { ubrk_close(main_); }
  55. UBreakIterator* Lease(UErrorCode& status) {
  56. if (U_FAILURE(status)) {
  57. return nullptr;
  58. }
  59. if (U_FAILURE(main_status_)) {
  60. status = main_status_;
  61. return nullptr;
  62. }
  63. {
  64. AutoLock scoped_lock(lock_);
  65. if (main_could_be_leased_) {
  66. // Just lease the main_ out.
  67. main_could_be_leased_ = false;
  68. return main_;
  69. }
  70. }
  71. // The main_ is already leased out to some other places, return a new
  72. // object instead.
  73. UBreakIterator* result =
  74. ubrk_open(break_type, nullptr, nullptr, 0, &status);
  75. if (U_FAILURE(status)) {
  76. NOTREACHED() << "ubrk_open failed for type " << break_type
  77. << " with error " << status;
  78. }
  79. return result;
  80. }
  81. void Return(UBreakIterator* item) {
  82. // If the return item is the main_, just remember we can lease it out
  83. // next time.
  84. if (item == main_) {
  85. AutoLock scoped_lock(lock_);
  86. main_could_be_leased_ = true;
  87. } else {
  88. // Close the item if it is not main_.
  89. ubrk_close(item);
  90. }
  91. }
  92. private:
  93. UErrorCode main_status_;
  94. raw_ptr<UBreakIterator> main_;
  95. bool main_could_be_leased_ GUARDED_BY(lock_);
  96. Lock lock_;
  97. };
  98. static LazyInstance<DefaultLocaleBreakIteratorCache<UBRK_CHARACTER>>::Leaky
  99. char_break_cache = LAZY_INSTANCE_INITIALIZER;
  100. static LazyInstance<DefaultLocaleBreakIteratorCache<UBRK_WORD>>::Leaky
  101. word_break_cache = LAZY_INSTANCE_INITIALIZER;
  102. static LazyInstance<DefaultLocaleBreakIteratorCache<UBRK_SENTENCE>>::Leaky
  103. sentence_break_cache = LAZY_INSTANCE_INITIALIZER;
  104. static LazyInstance<DefaultLocaleBreakIteratorCache<UBRK_LINE>>::Leaky
  105. line_break_cache = LAZY_INSTANCE_INITIALIZER;
  106. } // namespace
  107. BreakIterator::~BreakIterator() {
  108. if (iter_) {
  109. UBreakIterator* iter = static_cast<UBreakIterator*>(iter_);
  110. switch (break_type_) {
  111. // Free the iter if it is RULE_BASED
  112. case RULE_BASED:
  113. ubrk_close(iter);
  114. break;
  115. // Otherwise, return the iter to the cache it leased from.`
  116. case BREAK_CHARACTER:
  117. char_break_cache.Pointer()->Return(iter);
  118. break;
  119. case BREAK_WORD:
  120. word_break_cache.Pointer()->Return(iter);
  121. break;
  122. case BREAK_SENTENCE:
  123. sentence_break_cache.Pointer()->Return(iter);
  124. break;
  125. case BREAK_LINE:
  126. case BREAK_NEWLINE:
  127. line_break_cache.Pointer()->Return(iter);
  128. break;
  129. default:
  130. NOTREACHED() << "invalid break_type_";
  131. break;
  132. }
  133. }
  134. }
  135. bool BreakIterator::Init() {
  136. UErrorCode status = U_ZERO_ERROR;
  137. UParseError parse_error;
  138. switch (break_type_) {
  139. case BREAK_CHARACTER:
  140. iter_ = char_break_cache.Pointer()->Lease(status);
  141. break;
  142. case BREAK_WORD:
  143. iter_ = word_break_cache.Pointer()->Lease(status);
  144. break;
  145. case BREAK_SENTENCE:
  146. iter_ = sentence_break_cache.Pointer()->Lease(status);
  147. break;
  148. case BREAK_LINE:
  149. case BREAK_NEWLINE:
  150. iter_ = line_break_cache.Pointer()->Lease(status);
  151. break;
  152. case RULE_BASED:
  153. iter_ =
  154. ubrk_openRules(rules_.c_str(), static_cast<int32_t>(rules_.length()),
  155. nullptr, 0, &parse_error, &status);
  156. if (U_FAILURE(status)) {
  157. NOTREACHED() << "ubrk_openRules failed to parse rule string at line "
  158. << parse_error.line << ", offset " << parse_error.offset;
  159. }
  160. break;
  161. default:
  162. NOTREACHED() << "invalid break_type_";
  163. return false;
  164. }
  165. if (U_FAILURE(status) || iter_ == nullptr) {
  166. return false;
  167. }
  168. if (string_.data() != nullptr) {
  169. ubrk_setText(static_cast<UBreakIterator*>(iter_), string_.data(),
  170. static_cast<int32_t>(string_.size()), &status);
  171. if (U_FAILURE(status)) {
  172. return false;
  173. }
  174. }
  175. // Move the iterator to the beginning of the string.
  176. ubrk_first(static_cast<UBreakIterator*>(iter_));
  177. return true;
  178. }
  179. bool BreakIterator::Advance() {
  180. int32_t pos;
  181. int32_t status;
  182. prev_ = pos_;
  183. switch (break_type_) {
  184. case BREAK_CHARACTER:
  185. case BREAK_WORD:
  186. case BREAK_LINE:
  187. case BREAK_SENTENCE:
  188. case RULE_BASED:
  189. pos = ubrk_next(static_cast<UBreakIterator*>(iter_));
  190. if (pos == UBRK_DONE) {
  191. pos_ = npos;
  192. return false;
  193. }
  194. pos_ = static_cast<size_t>(pos);
  195. return true;
  196. case BREAK_NEWLINE:
  197. do {
  198. pos = ubrk_next(static_cast<UBreakIterator*>(iter_));
  199. if (pos == UBRK_DONE)
  200. break;
  201. pos_ = static_cast<size_t>(pos);
  202. status = ubrk_getRuleStatus(static_cast<UBreakIterator*>(iter_));
  203. } while (status >= UBRK_LINE_SOFT && status < UBRK_LINE_SOFT_LIMIT);
  204. if (pos == UBRK_DONE && prev_ == pos_) {
  205. pos_ = npos;
  206. return false;
  207. }
  208. return true;
  209. default:
  210. NOTREACHED() << "invalid break_type_";
  211. return false;
  212. }
  213. }
  214. bool BreakIterator::SetText(const char16_t* text, const size_t length) {
  215. UErrorCode status = U_ZERO_ERROR;
  216. ubrk_setText(static_cast<UBreakIterator*>(iter_), text, length, &status);
  217. pos_ = 0; // implicit when ubrk_setText is done
  218. prev_ = npos;
  219. if (U_FAILURE(status)) {
  220. NOTREACHED() << "ubrk_setText failed";
  221. return false;
  222. }
  223. string_ = StringPiece16(text, length);
  224. return true;
  225. }
  226. bool BreakIterator::IsWord() const {
  227. return GetWordBreakStatus() == IS_WORD_BREAK;
  228. }
  229. BreakIterator::WordBreakStatus BreakIterator::GetWordBreakStatus() const {
  230. int32_t status = ubrk_getRuleStatus(static_cast<UBreakIterator*>(iter_));
  231. if (break_type_ != BREAK_WORD && break_type_ != RULE_BASED)
  232. return IS_LINE_OR_CHAR_BREAK;
  233. // In ICU 60, trying to advance past the end of the text does not change
  234. // |status| so that |pos_| has to be checked as well as |status|.
  235. // See http://bugs.icu-project.org/trac/ticket/13447 .
  236. return (status == UBRK_WORD_NONE || pos_ == npos) ? IS_SKIPPABLE_WORD
  237. : IS_WORD_BREAK;
  238. }
  239. bool BreakIterator::IsEndOfWord(size_t position) const {
  240. if (break_type_ != BREAK_WORD && break_type_ != RULE_BASED)
  241. return false;
  242. UBreakIterator* iter = static_cast<UBreakIterator*>(iter_);
  243. UBool boundary = ubrk_isBoundary(iter, static_cast<int32_t>(position));
  244. int32_t status = ubrk_getRuleStatus(iter);
  245. return (!!boundary && status != UBRK_WORD_NONE);
  246. }
  247. bool BreakIterator::IsStartOfWord(size_t position) const {
  248. if (break_type_ != BREAK_WORD && break_type_ != RULE_BASED)
  249. return false;
  250. UBreakIterator* iter = static_cast<UBreakIterator*>(iter_);
  251. UBool boundary = ubrk_isBoundary(iter, static_cast<int32_t>(position));
  252. ubrk_next(iter);
  253. int32_t next_status = ubrk_getRuleStatus(iter);
  254. return (!!boundary && next_status != UBRK_WORD_NONE);
  255. }
  256. bool BreakIterator::IsSentenceBoundary(size_t position) const {
  257. if (break_type_ != BREAK_SENTENCE && break_type_ != RULE_BASED)
  258. return false;
  259. UBreakIterator* iter = static_cast<UBreakIterator*>(iter_);
  260. return !!ubrk_isBoundary(iter, static_cast<int32_t>(position));
  261. }
  262. bool BreakIterator::IsGraphemeBoundary(size_t position) const {
  263. if (break_type_ != BREAK_CHARACTER)
  264. return false;
  265. UBreakIterator* iter = static_cast<UBreakIterator*>(iter_);
  266. return !!ubrk_isBoundary(iter, static_cast<int32_t>(position));
  267. }
  268. std::u16string BreakIterator::GetString() const {
  269. return std::u16string(GetStringPiece());
  270. }
  271. StringPiece16 BreakIterator::GetStringPiece() const {
  272. DCHECK(prev_ != npos && pos_ != npos);
  273. return string_.substr(prev_, pos_ - prev_);
  274. }
  275. } // namespace i18n
  276. } // namespace base