1 /*
2 * Copyright (c) 2023 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
16 #include "word_breaker.h"
17
18 #include "texgine_exception.h"
19 #include "texgine/utils/exlog.h"
20
21 namespace OHOS {
22 namespace Rosen {
23 namespace TextEngine {
SetLocale(const icu::Locale & locale)24 void WordBreaker::SetLocale(const icu::Locale &locale)
25 {
26 locale_ = locale;
27 }
28
SetRange(size_t start,size_t end)29 void WordBreaker::SetRange(size_t start, size_t end)
30 {
31 startIndex_ = start;
32 endIndex_ = end;
33 }
34
GetBoundary(const std::vector<uint16_t> & u16str,bool isWordInstance)35 std::vector<Boundary> WordBreaker::GetBoundary(const std::vector<uint16_t> &u16str, bool isWordInstance)
36 {
37 UErrorCode status = U_ZERO_ERROR;
38 icu::BreakIterator *wbi = nullptr;
39 if (isWordInstance) {
40 wbi = icu::BreakIterator::createWordInstance(locale_, status);
41 } else {
42 wbi = icu::BreakIterator::createLineInstance(locale_, status);
43 }
44
45 if (wbi == nullptr) {
46 LOGEX_FUNC_LINE(ERROR) << "create BreakIterator failed";
47 throw TEXGINE_EXCEPTION(API_FAILED);
48 }
49
50 // > U_ZERO_ERROR: error, < U_ZERO_ERROR: warning
51 if (status > U_ZERO_ERROR) {
52 LOGEX_FUNC_LINE(ERROR) << "status is error";
53 throw TEXGINE_EXCEPTION(API_FAILED);
54 }
55
56 if (endIndex_ <= startIndex_) {
57 LOGEX_FUNC_LINE(ERROR) << "endIndex_ <= startIndex_";
58 throw TEXGINE_EXCEPTION(INVALID_ARGUMENT);
59 }
60
61 if (endIndex_ > u16str.size()) {
62 LOGEX_FUNC_LINE(ERROR) << "endIndex_ > u16str.size()";
63 throw TEXGINE_EXCEPTION(INVALID_ARGUMENT);
64 }
65
66 wbi->setText({u16str.data() + startIndex_, static_cast<int32_t>(endIndex_ - startIndex_)});
67
68 std::vector<Boundary> boundaries;
69 auto beg = wbi->first();
70 for (auto end = wbi->next(); end != wbi->DONE; end = wbi->next()) {
71 boundaries.emplace_back(beg, end);
72 beg = end;
73 }
74
75 delete wbi;
76 wbi = nullptr;
77 return boundaries;
78 }
79 } // namespace TextEngine
80 } // namespace Rosen
81 } // namespace OHOS
82