• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 "string_utils.h"
17 
18 #include <iomanip>
19 #include <sstream>
20 
21 #include "unicode/ustring.h"
22 #include "unicode/unistr.h"
23 
24 #include "global.h"
25 
26 namespace OHOS {
27 namespace MiscServices {
28 constexpr int32_t HEX_BYTE_WIDTH = 2;
29 
ToHex(const std::string & in)30 std::string StringUtils::ToHex(const std::string &in)
31 {
32     std::stringstream ss;
33     if (in.size() < 1) {
34         return "";
35     }
36     for (size_t i = 0; i < in.size(); i++) {
37         ss << std::uppercase << std::hex << std::setw(sizeof(uint8_t) * HEX_BYTE_WIDTH)
38         << std::setfill('0') << static_cast<char16_t>(in.at(i));
39     }
40     return ss.str();
41 }
42 
ToHex(const std::u16string & in)43 std::string StringUtils::ToHex(const std::u16string &in)
44 {
45     std::stringstream ss;
46     if (in.size() < 1) {
47         return "";
48     }
49     for (size_t i = 0; i < in.size(); i++) {
50         ss << std::uppercase << std::hex << std::setw(sizeof(char16_t) * HEX_BYTE_WIDTH)
51         << std::setfill('0') << in.at(i);
52     }
53     return ss.str();
54 }
55 
CountUtf16Chars(const std::u16string & in)56 int32_t StringUtils::CountUtf16Chars(const std::u16string &in)
57 {
58     int32_t ret = u_countChar32(in.data(), in.size());
59     IMSA_HILOGD("size:%{public}zu,ret:%{public}d", in.size(), ret);
60     return ret;
61 }
62 
TruncateUtf16String(std::u16string & in,int32_t maxChars)63 void StringUtils::TruncateUtf16String(std::u16string &in, int32_t maxChars)
64 {
65     const UChar* src = in.data();
66     size_t srcLen = in.size();
67     size_t offset = 0;
68     int32_t count = 0;
69     if (maxChars < 0  || static_cast<int32_t>(srcLen) <= maxChars) {
70         IMSA_HILOGD("srcLen:%{public}zu,maxChars:%{public}d", srcLen, maxChars);
71         return;
72     }
73     while (offset < srcLen && count < maxChars) {
74         UChar32 c;
75         U16_NEXT(src, offset, srcLen, c);
76         if (c == U_SENTINEL) {
77             break;
78         }
79         count++;
80     }
81     IMSA_HILOGD("srcLen:%{public}zu,maxChars:%{public}d,resultLen:%{public}zu,count:%{public}d",
82         srcLen, maxChars, offset, count);
83     if (offset < srcLen) {
84         IMSA_HILOGI("chars length exceeds limit,maxChars:%{public}d,offset:%{public}zu", maxChars, offset);
85         in.resize(offset);
86     }
87     return;
88 }
89 } // namespace MiscServices
90 } // namespace OHOS