blob: 434d1e6139293b3bcbab7371b759053b743cba63 [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:261/*
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
Steve Anton10542f22019-01-11 17:11:0011#include "rtc_base/string_encode.h"
henrike@webrtc.orgf0488722014-05-13 18:00:2612
Yves Gerey988cc082018-10-23 10:03:0113#include <cstdio>
14
Ali Tofigh7fa90572022-03-17 14:47:4915#include "absl/strings/string_view.h"
Ali Tofighfd6a4d62022-03-31 08:36:4816#include "api/array_view.h"
Jonas Olsson6b1985d2018-07-05 09:59:4817#include "rtc_base/arraysize.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3118#include "rtc_base/checks.h"
henrike@webrtc.orgf0488722014-05-13 18:00:2619
20namespace rtc {
21
22/////////////////////////////////////////////////////////////////////////////
23// String Encoding Utilities
24/////////////////////////////////////////////////////////////////////////////
25
Niels Möllere7e36012019-05-23 10:10:2626namespace {
27const char HEX[] = "0123456789abcdef";
henrike@webrtc.orgf0488722014-05-13 18:00:2628
Niels Möllere7e36012019-05-23 10:10:2629// Convert an unsigned value from 0 to 15 to the hex character equivalent...
henrike@webrtc.orgf0488722014-05-13 18:00:2630char hex_encode(unsigned char val) {
henrikg91d6ede2015-09-17 07:24:3431 RTC_DCHECK_LT(val, 16);
henrike@webrtc.orgf0488722014-05-13 18:00:2632 return (val < 16) ? HEX[val] : '!';
33}
34
Niels Möllere7e36012019-05-23 10:10:2635// ...and vice-versa.
henrike@webrtc.orgf0488722014-05-13 18:00:2636bool hex_decode(char ch, unsigned char* val) {
37 if ((ch >= '0') && (ch <= '9')) {
38 *val = ch - '0';
Niels Möller12048c72018-10-29 11:58:4839 } else if ((ch >= 'A') && (ch <= 'F')) {
henrike@webrtc.orgf0488722014-05-13 18:00:2640 *val = (ch - 'A') + 10;
Niels Möller12048c72018-10-29 11:58:4841 } else if ((ch >= 'a') && (ch <= 'f')) {
henrike@webrtc.orgf0488722014-05-13 18:00:2642 *val = (ch - 'a') + 10;
43 } else {
44 return false;
45 }
46 return true;
47}
48
Niels Möllerf25df352019-05-24 07:14:1349size_t hex_encode_output_length(size_t srclen, char delimiter) {
50 return delimiter && srclen > 0 ? (srclen * 3 - 1) : (srclen * 2);
51}
52
53// hex_encode shows the hex representation of binary data in ascii, with
Artem Titov96e3b992021-07-26 14:03:1454// `delimiter` between bytes, or none if `delimiter` == 0.
Niels Möllerf25df352019-05-24 07:14:1355void hex_encode_with_delimiter(char* buffer,
Ali Tofighfd6a4d62022-03-31 08:36:4856 absl::string_view source,
Niels Möllerf25df352019-05-24 07:14:1357 char delimiter) {
58 RTC_DCHECK(buffer);
henrike@webrtc.orgf0488722014-05-13 18:00:2659
60 // Init and check bounds.
61 const unsigned char* bsource =
Ali Tofighfd6a4d62022-03-31 08:36:4862 reinterpret_cast<const unsigned char*>(source.data());
henrike@webrtc.orgf0488722014-05-13 18:00:2663 size_t srcpos = 0, bufpos = 0;
henrike@webrtc.orgf0488722014-05-13 18:00:2664
Ali Tofighfd6a4d62022-03-31 08:36:4865 size_t srclen = source.length();
henrike@webrtc.orgf0488722014-05-13 18:00:2666 while (srcpos < srclen) {
67 unsigned char ch = bsource[srcpos++];
Yves Gerey665174f2018-06-19 13:03:0568 buffer[bufpos] = hex_encode((ch >> 4) & 0xF);
69 buffer[bufpos + 1] = hex_encode((ch)&0xF);
henrike@webrtc.orgf0488722014-05-13 18:00:2670 bufpos += 2;
71
72 // Don't write a delimiter after the last byte.
73 if (delimiter && (srcpos < srclen)) {
74 buffer[bufpos] = delimiter;
75 ++bufpos;
76 }
77 }
henrike@webrtc.orgf0488722014-05-13 18:00:2678}
79
Niels Möllere7e36012019-05-23 10:10:2680} // namespace
81
Ali Tofigh7fa90572022-03-17 14:47:4982std::string hex_encode(absl::string_view str) {
Ali Tofighfd6a4d62022-03-31 08:36:4883 return hex_encode_with_delimiter(str, 0);
Peter Thatcher1cf6f812015-05-15 17:40:4584}
85
Ali Tofighfd6a4d62022-03-31 08:36:4886std::string hex_encode_with_delimiter(absl::string_view source,
henrike@webrtc.orgf0488722014-05-13 18:00:2687 char delimiter) {
Ali Tofighfd6a4d62022-03-31 08:36:4888 std::string s(hex_encode_output_length(source.length(), delimiter), 0);
89 hex_encode_with_delimiter(&s[0], source, delimiter);
Niels Möllerf25df352019-05-24 07:14:1390 return s;
henrike@webrtc.orgf0488722014-05-13 18:00:2691}
92
Ali Tofighfd6a4d62022-03-31 08:36:4893size_t hex_decode_with_delimiter(ArrayView<char> cbuffer,
94 absl::string_view source,
henrike@webrtc.orgf0488722014-05-13 18:00:2695 char delimiter) {
Ali Tofighfd6a4d62022-03-31 08:36:4896 if (cbuffer.empty())
henrike@webrtc.orgf0488722014-05-13 18:00:2697 return 0;
98
99 // Init and bounds check.
Ali Tofighfd6a4d62022-03-31 08:36:48100 unsigned char* bbuffer = reinterpret_cast<unsigned char*>(cbuffer.data());
henrike@webrtc.orgf0488722014-05-13 18:00:26101 size_t srcpos = 0, bufpos = 0;
Ali Tofighfd6a4d62022-03-31 08:36:48102 size_t srclen = source.length();
103
henrike@webrtc.orgf0488722014-05-13 18:00:26104 size_t needed = (delimiter) ? (srclen + 1) / 3 : srclen / 2;
Ali Tofighfd6a4d62022-03-31 08:36:48105 if (cbuffer.size() < needed)
henrike@webrtc.orgf0488722014-05-13 18:00:26106 return 0;
107
108 while (srcpos < srclen) {
109 if ((srclen - srcpos) < 2) {
110 // This means we have an odd number of bytes.
111 return 0;
112 }
113
114 unsigned char h1, h2;
115 if (!hex_decode(source[srcpos], &h1) ||
116 !hex_decode(source[srcpos + 1], &h2))
117 return 0;
118
119 bbuffer[bufpos++] = (h1 << 4) | h2;
120 srcpos += 2;
121
122 // Remove the delimiter if needed.
123 if (delimiter && (srclen - srcpos) > 1) {
124 if (source[srcpos] != delimiter)
125 return 0;
126 ++srcpos;
127 }
128 }
129
130 return bufpos;
131}
132
Ali Tofighfd6a4d62022-03-31 08:36:48133size_t hex_decode(ArrayView<char> buffer, absl::string_view source) {
134 return hex_decode_with_delimiter(buffer, source, 0);
henrike@webrtc.orgf0488722014-05-13 18:00:26135}
136
Niels Möller44203802021-09-16 13:39:16137size_t tokenize(absl::string_view source,
Yves Gerey665174f2018-06-19 13:03:05138 char delimiter,
henrike@webrtc.orgf0488722014-05-13 18:00:26139 std::vector<std::string>* fields) {
henrike@webrtc.orgf0488722014-05-13 18:00:26140 fields->clear();
141 size_t last = 0;
142 for (size_t i = 0; i < source.length(); ++i) {
143 if (source[i] == delimiter) {
144 if (i != last) {
Niels Möller44203802021-09-16 13:39:16145 fields->emplace_back(source.substr(last, i - last));
henrike@webrtc.orgf0488722014-05-13 18:00:26146 }
147 last = i + 1;
148 }
149 }
150 if (last != source.length()) {
Niels Möller44203802021-09-16 13:39:16151 fields->emplace_back(source.substr(last, source.length() - last));
henrike@webrtc.orgf0488722014-05-13 18:00:26152 }
153 return fields->size();
154}
155
Niels Möller44203802021-09-16 13:39:16156bool tokenize_first(absl::string_view source,
Donald Curtis144d0182015-05-15 20:14:24157 const char delimiter,
158 std::string* token,
159 std::string* rest) {
Donald Curtis0e07f922015-05-15 16:21:23160 // Find the first delimiter
161 size_t left_pos = source.find(delimiter);
Ali Tofigh7fa90572022-03-17 14:47:49162 if (left_pos == absl::string_view::npos) {
Donald Curtis0e07f922015-05-15 16:21:23163 return false;
164 }
165
166 // Look for additional occurrances of delimiter.
167 size_t right_pos = left_pos + 1;
Niels Möller44203802021-09-16 13:39:16168 while (right_pos < source.size() && source[right_pos] == delimiter) {
Donald Curtis0e07f922015-05-15 16:21:23169 right_pos++;
170 }
171
Niels Möller44203802021-09-16 13:39:16172 *token = std::string(source.substr(0, left_pos));
173 *rest = std::string(source.substr(right_pos));
Donald Curtis0e07f922015-05-15 16:21:23174 return true;
175}
176
Niels Möller2d3186e2022-01-24 13:15:03177std::vector<absl::string_view> split(absl::string_view source, char delimiter) {
178 std::vector<absl::string_view> fields;
179 size_t last = 0;
180 for (size_t i = 0; i < source.length(); ++i) {
181 if (source[i] == delimiter) {
182 fields.push_back(source.substr(last, i - last));
183 last = i + 1;
184 }
185 }
186 fields.push_back(source.substr(last));
187 return fields;
188}
189
Jonas Olsson6b1985d2018-07-05 09:59:48190std::string ToString(const bool b) {
191 return b ? "true" : "false";
192}
193
Ali Tofighfd6a4d62022-03-31 08:36:48194std::string ToString(absl::string_view s) {
Jonas Olsson6b1985d2018-07-05 09:59:48195 return std::string(s);
196}
Ali Tofigh7fa90572022-03-17 14:47:49197
Ali Tofighfd6a4d62022-03-31 08:36:48198std::string ToString(const char* s) {
Ali Tofigh7fa90572022-03-17 14:47:49199 return std::string(s);
Jonas Olsson6b1985d2018-07-05 09:59:48200}
201
202std::string ToString(const short s) {
203 char buf[32];
204 const int len = std::snprintf(&buf[0], arraysize(buf), "%hd", s);
205 RTC_DCHECK_LE(len, arraysize(buf));
206 return std::string(&buf[0], len);
207}
208std::string ToString(const unsigned short s) {
209 char buf[32];
210 const int len = std::snprintf(&buf[0], arraysize(buf), "%hu", s);
211 RTC_DCHECK_LE(len, arraysize(buf));
212 return std::string(&buf[0], len);
213}
214std::string ToString(const int s) {
215 char buf[32];
216 const int len = std::snprintf(&buf[0], arraysize(buf), "%d", s);
217 RTC_DCHECK_LE(len, arraysize(buf));
218 return std::string(&buf[0], len);
219}
220std::string ToString(const unsigned int s) {
221 char buf[32];
222 const int len = std::snprintf(&buf[0], arraysize(buf), "%u", s);
223 RTC_DCHECK_LE(len, arraysize(buf));
224 return std::string(&buf[0], len);
225}
226std::string ToString(const long int s) {
227 char buf[32];
228 const int len = std::snprintf(&buf[0], arraysize(buf), "%ld", s);
229 RTC_DCHECK_LE(len, arraysize(buf));
230 return std::string(&buf[0], len);
231}
232std::string ToString(const unsigned long int s) {
233 char buf[32];
234 const int len = std::snprintf(&buf[0], arraysize(buf), "%lu", s);
235 RTC_DCHECK_LE(len, arraysize(buf));
236 return std::string(&buf[0], len);
237}
238std::string ToString(const long long int s) {
239 char buf[32];
240 const int len = std::snprintf(&buf[0], arraysize(buf), "%lld", s);
241 RTC_DCHECK_LE(len, arraysize(buf));
242 return std::string(&buf[0], len);
243}
244std::string ToString(const unsigned long long int s) {
245 char buf[32];
246 const int len = std::snprintf(&buf[0], arraysize(buf), "%llu", s);
247 RTC_DCHECK_LE(len, arraysize(buf));
248 return std::string(&buf[0], len);
249}
250
251std::string ToString(const double d) {
252 char buf[32];
253 const int len = std::snprintf(&buf[0], arraysize(buf), "%g", d);
254 RTC_DCHECK_LE(len, arraysize(buf));
255 return std::string(&buf[0], len);
256}
257
Jonas Olsson88e18482018-09-03 08:15:08258std::string ToString(const long double d) {
259 char buf[32];
260 const int len = std::snprintf(&buf[0], arraysize(buf), "%Lg", d);
261 RTC_DCHECK_LE(len, arraysize(buf));
262 return std::string(&buf[0], len);
263}
264
Jonas Olsson6b1985d2018-07-05 09:59:48265std::string ToString(const void* const p) {
266 char buf[32];
267 const int len = std::snprintf(&buf[0], arraysize(buf), "%p", p);
268 RTC_DCHECK_LE(len, arraysize(buf));
269 return std::string(&buf[0], len);
270}
271
Ali Tofighfd6a4d62022-03-31 08:36:48272bool FromString(absl::string_view s, bool* b) {
Jonas Olsson6b1985d2018-07-05 09:59:48273 if (s == "false") {
274 *b = false;
275 return true;
276 }
277 if (s == "true") {
278 *b = true;
279 return true;
280 }
281 return false;
282}
283
henrike@webrtc.orgf0488722014-05-13 18:00:26284} // namespace rtc