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
16 #include "str_util.h"
17 #include "i18n_memory_adapter.h"
18 #include "securec.h"
19 #include "types.h"
20
21 namespace OHOS {
22 namespace I18N {
23 static int g_i18nMaxMalloc = 4096;
24
ReplaceAndCountOff(std::string & content,const int index,const char * sign,const int off)25 int ReplaceAndCountOff(std::string &content, const int index, const char *sign, const int off)
26 {
27 if ((sign == nullptr) || (index < 0) || (static_cast<unsigned int>(index) > strlen(content.data()))) {
28 return off;
29 }
30 content = content.replace(index, 1, sign);
31 int signLen = strlen(sign);
32 return off + signLen - 1;
33 }
34
ArrayCopy(std::string * target,const int targetSize,const std::string * source,const int sourceSize)35 void ArrayCopy(std::string *target, const int targetSize, const std::string *source, const int sourceSize)
36 {
37 if (target == nullptr || source == nullptr || (sourceSize > targetSize)) {
38 return;
39 }
40 for (int i = 0; i < sourceSize; i++) {
41 target[i] = source[i];
42 }
43 }
44
NewArrayAndCopy(const char * source,const int len)45 char *NewArrayAndCopy(const char *source, const int len)
46 {
47 if ((source == nullptr) || (len <= 0) || (len > g_i18nMaxMalloc)) { // 4096 is the max size that we could use
48 return nullptr;
49 }
50 char *out = reinterpret_cast<char *>(I18nMalloc(len + 1));
51 if (out == nullptr) {
52 return nullptr;
53 }
54 errno_t rc = strcpy_s(out, len + 1, source);
55 if (rc != EOK) {
56 I18nFree(static_cast<void *>(out));
57 return nullptr;
58 }
59 out[len] = '\0';
60 return out;
61 }
62
I18nNewCharString(const char * source,const int len)63 char *I18nNewCharString(const char *source, const int len)
64 {
65 if ((source == nullptr) || (len <= 0) || len > g_i18nMaxMalloc) { // 4096 is the max size of allocation
66 return nullptr;
67 }
68 char *out = reinterpret_cast<char *>(I18nMalloc(len + 1));
69 if (out == nullptr) {
70 return nullptr;
71 }
72 errno_t rc = memcpy_s(out, len + 1, source, len);
73 if (rc != EOK) {
74 I18nFree(static_cast<void *>(out));
75 return nullptr;
76 }
77 out[len] = '\0';
78 return out;
79 }
80
CleanCharArray(char * target,const int len)81 bool CleanCharArray(char *target, const int len)
82 {
83 errno_t ret = memset_s(target, len, 0, len);
84 if (ret != EOK) {
85 return false;
86 }
87 return true;
88 }
89
LenCharArray(const char * target)90 int LenCharArray(const char *target)
91 {
92 if (target == nullptr) {
93 return 0;
94 }
95 return strlen(target);
96 }
97
Split(const std::string & src,std::string * dst,const int32_t size,const char & sep)98 void Split(const std::string &src, std::string *dst, const int32_t size, const char &sep)
99 {
100 if (dst == nullptr || size <= 0) {
101 return;
102 }
103 int32_t current = 0;
104
105 std::string::size_type begin = 0;
106 std::string::size_type end = 0;
107 std::string::size_type srcSize = src.size();
108 while (current < size) {
109 while ((end < srcSize) && (src[end] != sep)) {
110 ++end;
111 }
112 if (end >= srcSize) {
113 break;
114 }
115 dst[current++] = std::string(src, begin, end - begin);
116 ++end; // pass the sep
117 begin = end;
118 }
119 if (current < size && end > begin) {
120 dst[current++] = std::string(src, begin, end - begin);
121 }
122 }
123
CompareLocaleItem(const char * item,const char * other)124 bool CompareLocaleItem(const char *item, const char *other)
125 {
126 if ((item == nullptr) && (other == nullptr)) {
127 return true;
128 }
129 if ((item != nullptr) && (other != nullptr) && (strcmp(item, other) == 0)) {
130 return true;
131 }
132 return false;
133 }
134
135 /**
136 * split str with "_"
137 */
Parse(const char * str,int32_t count)138 std::string Parse(const char *str, int32_t count)
139 {
140 if (str == nullptr || count < 0) {
141 return "";
142 }
143 size_t length = strlen(str);
144 if (length == 0 || length > I18N_STRING_LENGTH_MAX) {
145 return "";
146 }
147 int32_t tempCount = 0;
148 size_t ind = 0;
149 while ((ind < length) && (tempCount < count)) {
150 if (str[ind] == '_') {
151 ++tempCount;
152 }
153 ++ind;
154 }
155 if (tempCount < count) {
156 return "";
157 }
158 size_t last = ind;
159 while (last < length) {
160 if (str[last] == '_') {
161 break;
162 }
163 ++last;
164 }
165 if (last == ind) {
166 return "";
167 }
168 return std::string(str + ind, last - ind);
169 }
170 } // namespace I18N
171 } // namespace OHOS
172