1 /*
2 * Copyright (c) 2020-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 "ace_mem_base.h"
17 #include "ctype.h"
18 #include "js_config.h"
19 #include "securec.h"
20 #include "string_util.h"
21
22 namespace OHOS {
23 namespace ACELite {
Copy(const char * sequence)24 char *StringUtil::Copy(const char *sequence)
25 {
26 if (sequence == nullptr) {
27 return nullptr;
28 }
29 size_t size = strlen(sequence);
30 if (size >= UINT16_MAX) {
31 return nullptr;
32 }
33 char *buffer = StringUtil::Malloc(size);
34 if (buffer == nullptr) {
35 return nullptr;
36 }
37 if (strncpy_s(buffer, size + 1, sequence, size) == 0) {
38 return buffer;
39 }
40 ace_free(buffer);
41 return nullptr;
42 }
Malloc(const uint32_t size)43 char *StringUtil::Malloc(const uint32_t size)
44 {
45 char *buffer = static_cast<char *>(ace_malloc(size + 1));
46 if (buffer == nullptr) {
47 return nullptr;
48 }
49 buffer[size] = '\0';
50 return buffer;
51 }
Slice(const char * sequence,const int32_t start)52 char *StringUtil::Slice(const char *sequence, const int32_t start)
53 {
54 return StringUtil::Slice(sequence, start, strlen(sequence));
55 }
Slice(const char * sequence,const int32_t start,const int32_t end)56 char *StringUtil::Slice(const char *sequence, const int32_t start, const int32_t end)
57 {
58 if (sequence == nullptr) {
59 return nullptr;
60 }
61 uint32_t size = strlen(sequence);
62 if (size == 0 || size >= UINT16_MAX) {
63 return nullptr;
64 }
65 int32_t startIdx = (start < 0) ? (start + size) : start;
66 startIdx = (startIdx < 0) ? 0 : startIdx;
67 int32_t endIdx = (end < 0) ? (end + size) : end;
68 endIdx = (endIdx > static_cast<int32_t>(size)) ? size : endIdx;
69 if (startIdx > endIdx || endIdx < 0) {
70 return nullptr;
71 }
72 int32_t diffSize = endIdx - startIdx;
73 if (diffSize < 0) {
74 return nullptr;
75 }
76 char *buffer = StringUtil::Malloc(diffSize);
77 if (buffer == nullptr) {
78 return nullptr;
79 }
80 if (strncpy_s(buffer, diffSize + 1, sequence + startIdx, diffSize) == 0) {
81 return buffer;
82 }
83 ace_free(buffer);
84 return nullptr;
85 }
StartsWith(const char * sequence,const char * subsequence)86 bool StringUtil::StartsWith(const char *sequence, const char *subsequence)
87 {
88 if (sequence == nullptr) {
89 return subsequence == nullptr;
90 }
91 if (subsequence == nullptr) {
92 return true;
93 }
94 return strncmp(sequence, subsequence, strlen(subsequence)) == 0;
95 }
96
Trim(char * sequence)97 char *StringUtil::Trim(char *sequence)
98 {
99 if (sequence == nullptr) {
100 return nullptr;
101 }
102 if (strlen(sequence) == 0) {
103 return sequence;
104 }
105 char *leftP = sequence;
106 char *rightP = sequence;
107 char *endP = sequence;
108 // find the first no-space position
109 while (*leftP != '\0' && isspace(*leftP)) {
110 leftP++;
111 }
112 // copy all charaters one by one from the first no-space position to the start of the buffer
113 while (*leftP != '\0') {
114 *rightP = *leftP;
115 if (!isspace(*rightP)) {
116 // record the next position of the last no-space character
117 endP = rightP + 1;
118 }
119 leftP++;
120 rightP++;
121 }
122 *endP = '\0';
123 return sequence;
124 }
125 } // namespace ACELite
126 } // namespace OHOS
127