1 /* 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 #include "i18n_break_iterator.h" 16 17 #include "unicode/brkiter.h" 18 #include "string" 19 #include "unicode/utypes.h" 20 21 namespace OHOS { 22 namespace Global { 23 namespace I18n { I18nBreakIterator(std::string localeTag)24I18nBreakIterator::I18nBreakIterator(std::string localeTag) 25 { 26 UErrorCode status = U_ZERO_ERROR; 27 iter = icu::BreakIterator::createLineInstance(localeTag.c_str(), status); 28 if (!U_SUCCESS(status)) { 29 if (iter != nullptr) { 30 delete iter; 31 } 32 iter = nullptr; 33 } 34 } 35 ~I18nBreakIterator()36I18nBreakIterator::~I18nBreakIterator() 37 { 38 if (iter != nullptr) { 39 delete iter; 40 } 41 } 42 Current()43int32_t I18nBreakIterator::Current() 44 { 45 icu::BreakIterator* breakIter = GetBreakIterator(); 46 if (breakIter != nullptr) { 47 return breakIter->current(); 48 } 49 return OFF_BOUND; 50 } 51 First()52int32_t I18nBreakIterator::First() 53 { 54 icu::BreakIterator* breakIter = GetBreakIterator(); 55 if (breakIter != nullptr) { 56 return breakIter->first(); 57 } 58 return OFF_BOUND; 59 } 60 Last()61int32_t I18nBreakIterator::Last() 62 { 63 icu::BreakIterator* breakIter = GetBreakIterator(); 64 if (breakIter != nullptr) { 65 return breakIter->last(); 66 } 67 return OFF_BOUND; 68 } 69 Previous()70int32_t I18nBreakIterator::Previous() 71 { 72 icu::BreakIterator* breakIter = GetBreakIterator(); 73 if (breakIter != nullptr) { 74 return breakIter->previous(); 75 } 76 return OFF_BOUND; 77 } 78 Next(int32_t number)79int32_t I18nBreakIterator::Next(int32_t number) 80 { 81 icu::BreakIterator* breakIter = GetBreakIterator(); 82 if (breakIter != nullptr) { 83 return breakIter->next(number); 84 } 85 return OFF_BOUND; 86 } 87 Next()88int32_t I18nBreakIterator::Next() 89 { 90 icu::BreakIterator* breakIter = GetBreakIterator(); 91 if (breakIter != nullptr) { 92 return breakIter->next(); 93 } 94 return OFF_BOUND; 95 } 96 Following(int32_t offset)97int32_t I18nBreakIterator::Following(int32_t offset) 98 { 99 icu::BreakIterator* breakIter = GetBreakIterator(); 100 if (breakIter != nullptr) { 101 return breakIter->following(offset); 102 } 103 return OFF_BOUND; 104 } 105 SetText(const char * text)106void I18nBreakIterator::SetText(const char *text) 107 { 108 icu::BreakIterator* breakIter = GetBreakIterator(); 109 if (breakIter != nullptr) { 110 ftext = text; 111 breakIter->setText(ftext); 112 } 113 } 114 GetText(std::string & str)115void I18nBreakIterator::GetText(std::string &str) 116 { 117 icu::BreakIterator* breakIter = GetBreakIterator(); 118 if (breakIter != nullptr) { 119 ftext.toUTF8String(str); 120 } 121 } 122 IsBoundary(int32_t offset)123bool I18nBreakIterator::IsBoundary(int32_t offset) 124 { 125 icu::BreakIterator* breakIter = GetBreakIterator(); 126 if (breakIter != nullptr) { 127 return breakIter->isBoundary(offset); 128 } 129 return false; 130 } 131 GetBreakIterator()132icu::BreakIterator* I18nBreakIterator::GetBreakIterator() 133 { 134 return this->iter; 135 } 136 } // namespace I18n 137 } // namespace Global 138 } // namespace OHOS