• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "string_utils.h"
17 
18 #include <sstream>
19 #include <string_ex.h>
20 
21 #include "cstddef"
22 #include "cstdint"
23 #include "cstdlib"
24 #include "ostream"
25 #include "string"
26 #include "telephony_log_wrapper.h"
27 #include "vector"
28 
29 namespace OHOS {
30 namespace Telephony {
31 static constexpr uint8_t HEX_OFFSET = 4;
32 static constexpr uint8_t STEP_2BIT = 2;
33 static constexpr char HEX_TABLE[] = "0123456789ABCDEF";
34 
StringUtils()35 StringUtils::StringUtils() {}
36 
~StringUtils()37 StringUtils::~StringUtils() {}
38 
HexCharToInt(char c)39 uint16_t StringUtils::HexCharToInt(char c)
40 {
41     const uint8_t decimal = 10;
42     if (c >= '0' && c <= '9') {
43         return (c - '0');
44     }
45     if (c >= 'A' && c <= 'F') {
46         return (c - 'A' + decimal);
47     }
48     if (c >= 'a' && c <= 'f') {
49         return (c - 'a' + decimal);
50     }
51     return 0;
52 }
53 
StringToHex(const std::string & data)54 std::string StringUtils::StringToHex(const std::string &data)
55 {
56     std::stringstream ss;
57     for (std::string::size_type i = 0; i < data.size(); ++i) {
58         unsigned char temp = static_cast<unsigned char>(data[i]) >> HEX_OFFSET;
59         ss << HEX_TABLE[temp] << HEX_TABLE[static_cast<unsigned char>(data[i]) & 0xf];
60     }
61     return ss.str();
62 }
63 
StringToHex(const char * data,int byteLength)64 std::string StringUtils::StringToHex(const char *data, int byteLength)
65 {
66     std::stringstream ss;
67     for (int i = 0; i < byteLength; ++i) {
68         unsigned char temp = static_cast<unsigned char>(data[i]) >> HEX_OFFSET;
69         ss << HEX_TABLE[temp] << HEX_TABLE[static_cast<unsigned char>(data[i]) & 0xf];
70     }
71     return ss.str();
72 }
73 
StringToHex(const std::vector<uint8_t> & data)74 std::string StringUtils::StringToHex(const std::vector<uint8_t> &data)
75 {
76     std::stringstream ss;
77     for (std::size_t i = 0; i < data.size(); ++i) {
78         unsigned char temp = static_cast<unsigned char>(data[i]) >> HEX_OFFSET;
79         ss << HEX_TABLE[temp] << HEX_TABLE[static_cast<unsigned char>(data[i]) & 0xf];
80     }
81     return ss.str();
82 }
83 
HexToString(const std::string & str)84 std::string StringUtils::HexToString(const std::string &str)
85 {
86     std::string result;
87     uint8_t hexDecimal = 16;
88     uint8_t hexStep = 2;
89     if (str.length() <= 0) {
90         return result;
91     }
92     for (size_t i = 0; i < str.length() - 1; i += STEP_2BIT) {
93         std::string byte = str.substr(i, hexStep);
94         char chr = 0;
95         long strTemp = strtol(byte.c_str(), nullptr, hexDecimal);
96         if (strTemp > 0) {
97             chr = static_cast<char>(strTemp);
98         }
99         result.push_back(chr);
100     }
101     return result;
102 }
103 
HexToByteVector(const std::string & str)104 std::vector<uint8_t> StringUtils::HexToByteVector(const std::string &str)
105 {
106     std::vector<uint8_t> ret;
107     int sz = static_cast<int>(str.length());
108     if (sz <= 0) {
109         return ret;
110     }
111     for (int i = 0; i < (sz - 1); i += STEP_2BIT) {
112         auto temp = static_cast<uint8_t>((HexCharToInt(str.at(i)) << HEX_OFFSET) | HexCharToInt(str.at(i + 1)));
113         ret.push_back(temp);
114     }
115     return ret;
116 }
117 
ToUtf8(const std::u16string & str16)118 std::string StringUtils::ToUtf8(const std::u16string &str16)
119 {
120     if (str16.empty()) {
121         std::string ret;
122         return ret;
123     }
124     return Str16ToStr8(str16);
125 }
126 
ToUtf16(const std::string & str)127 std::u16string StringUtils::ToUtf16(const std::string &str)
128 {
129     if (str.empty()) {
130         std::u16string ret;
131         return ret;
132     }
133     return Str8ToStr16(str);
134 }
135 
IsEmpty(const std::string & str)136 bool StringUtils::IsEmpty(const std::string &str)
137 {
138     if (str.empty()) {
139         return true;
140     }
141     std::string strTemp = TrimStr(str);
142     return strTemp.empty() || strlen(strTemp.c_str()) == 0;
143 }
144 
GetPortFromURL(const std::string & url)145 std::string StringUtils::GetPortFromURL(const std::string &url)
146 {
147     std::string delimiter = "://";
148     std::string protocol = GetProtocolFromURL(url);
149     std::string hostname = GetHostnameFromURL(url);
150     size_t start = protocol.empty() ? hostname.size() : protocol.size() + delimiter.size() + hostname.size();
151     size_t posStart = url.find_first_of(':', start);
152     if (posStart == std::string::npos) {
153         return "";
154     }
155     size_t posEnd = std::min({url.find('/', start), url.find('?', start)});
156     if (posEnd == std::string::npos) {
157         return url.substr(posStart + 1);
158     }
159     if (posStart > posEnd) {
160         return "";
161     }
162     return url.substr(posStart + 1, posEnd - posStart - 1);
163 }
164 
GetHostnameFromURL(const std::string & url)165 std::string StringUtils::GetHostnameFromURL(const std::string &url)
166 {
167     if (url.empty()) {
168         return "";
169     }
170     std::string delimiter = "://";
171     std::string tempUrl = url;
172     std::replace(tempUrl.begin(), tempUrl.end(), '\\', '/');
173     size_t posStart = tempUrl.find(delimiter);
174     if (posStart != std::string::npos) {
175         posStart += delimiter.length();
176     } else {
177         posStart = 0;
178     }
179     size_t notSlash = tempUrl.find_first_not_of('/', posStart);
180     if (notSlash != std::string::npos) {
181         posStart = notSlash;
182     }
183     size_t posEnd = std::min({ tempUrl.find(':', posStart),
184                               tempUrl.find('/', posStart), tempUrl.find('?', posStart) });
185     if (posEnd != std::string::npos) {
186         return tempUrl.substr(posStart, posEnd - posStart);
187     }
188     return tempUrl.substr(posStart);
189 }
190 
GetHostnameWithPortFromURL(const std::string & url)191 std::string StringUtils::GetHostnameWithPortFromURL(const std::string& url)
192 {
193     std::string portDelimiter = ":";
194     auto hostname = GetHostnameFromURL(url);
195     if (!hostname.empty()) {
196         std::string port = GetPortFromURL(url);
197         if (!port.empty()) {
198             hostname += portDelimiter + port;
199         }
200     }
201     return hostname;
202 }
203 
GetProtocolFromURL(const std::string & url)204 std::string StringUtils::GetProtocolFromURL(const std::string &url)
205 {
206     std::string delimiter = "://";
207     size_t pos = url.find(delimiter);
208     if (pos != std::string::npos) {
209         return url.substr(0, pos);
210     }
211     return "";
212 }
213 } // namespace Telephony
214 } // namespace OHOS