henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "rtc_base/stringencode.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 12 | |
Jonas Olsson | 6b1985d | 2018-07-05 11:59:48 +0200 | [diff] [blame] | 13 | #include "rtc_base/arraysize.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 14 | #include "rtc_base/checks.h" |
| 15 | #include "rtc_base/stringutils.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 16 | |
| 17 | namespace rtc { |
| 18 | |
| 19 | ///////////////////////////////////////////////////////////////////////////// |
| 20 | // String Encoding Utilities |
| 21 | ///////////////////////////////////////////////////////////////////////////// |
| 22 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 23 | size_t url_decode(char* buffer, |
| 24 | size_t buflen, |
| 25 | const char* source, |
| 26 | size_t srclen) { |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 27 | if (nullptr == buffer) |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 28 | return srclen + 1; |
| 29 | if (buflen <= 0) |
| 30 | return 0; |
| 31 | |
| 32 | unsigned char h1, h2; |
| 33 | size_t srcpos = 0, bufpos = 0; |
| 34 | while ((srcpos < srclen) && (bufpos + 1 < buflen)) { |
| 35 | unsigned char ch = source[srcpos++]; |
| 36 | if (ch == '+') { |
| 37 | buffer[bufpos++] = ' '; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 38 | } else if ((ch == '%') && (srcpos + 1 < srclen) && |
| 39 | hex_decode(source[srcpos], &h1) && |
| 40 | hex_decode(source[srcpos + 1], &h2)) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 41 | buffer[bufpos++] = (h1 << 4) | h2; |
| 42 | srcpos += 2; |
| 43 | } else { |
| 44 | buffer[bufpos++] = ch; |
| 45 | } |
| 46 | } |
| 47 | buffer[bufpos] = '\0'; |
| 48 | return bufpos; |
| 49 | } |
| 50 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 51 | static const char HEX[] = "0123456789abcdef"; |
| 52 | |
| 53 | char hex_encode(unsigned char val) { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 54 | RTC_DCHECK_LT(val, 16); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 55 | return (val < 16) ? HEX[val] : '!'; |
| 56 | } |
| 57 | |
| 58 | bool hex_decode(char ch, unsigned char* val) { |
| 59 | if ((ch >= '0') && (ch <= '9')) { |
| 60 | *val = ch - '0'; |
| 61 | } else if ((ch >= 'A') && (ch <= 'Z')) { |
| 62 | *val = (ch - 'A') + 10; |
| 63 | } else if ((ch >= 'a') && (ch <= 'z')) { |
| 64 | *val = (ch - 'a') + 10; |
| 65 | } else { |
| 66 | return false; |
| 67 | } |
| 68 | return true; |
| 69 | } |
| 70 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 71 | size_t hex_encode(char* buffer, |
| 72 | size_t buflen, |
| 73 | const char* csource, |
| 74 | size_t srclen) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 75 | return hex_encode_with_delimiter(buffer, buflen, csource, srclen, 0); |
| 76 | } |
| 77 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 78 | size_t hex_encode_with_delimiter(char* buffer, |
| 79 | size_t buflen, |
| 80 | const char* csource, |
| 81 | size_t srclen, |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 82 | char delimiter) { |
Henrik Grunell | 8487988 | 2018-03-23 15:33:03 +0100 | [diff] [blame] | 83 | RTC_DCHECK(buffer); // TODO(kwiberg): estimate output size |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 84 | if (buflen == 0) |
| 85 | return 0; |
| 86 | |
| 87 | // Init and check bounds. |
| 88 | const unsigned char* bsource = |
| 89 | reinterpret_cast<const unsigned char*>(csource); |
| 90 | size_t srcpos = 0, bufpos = 0; |
| 91 | size_t needed = delimiter ? (srclen * 3) : (srclen * 2 + 1); |
| 92 | if (buflen < needed) |
| 93 | return 0; |
| 94 | |
| 95 | while (srcpos < srclen) { |
| 96 | unsigned char ch = bsource[srcpos++]; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 97 | buffer[bufpos] = hex_encode((ch >> 4) & 0xF); |
| 98 | buffer[bufpos + 1] = hex_encode((ch)&0xF); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 99 | bufpos += 2; |
| 100 | |
| 101 | // Don't write a delimiter after the last byte. |
| 102 | if (delimiter && (srcpos < srclen)) { |
| 103 | buffer[bufpos] = delimiter; |
| 104 | ++bufpos; |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | // Null terminate. |
| 109 | buffer[bufpos] = '\0'; |
| 110 | return bufpos; |
| 111 | } |
| 112 | |
Peter Thatcher | 1cf6f81 | 2015-05-15 10:40:45 -0700 | [diff] [blame] | 113 | std::string hex_encode(const std::string& str) { |
| 114 | return hex_encode(str.c_str(), str.size()); |
| 115 | } |
| 116 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 117 | std::string hex_encode(const char* source, size_t srclen) { |
| 118 | return hex_encode_with_delimiter(source, srclen, 0); |
| 119 | } |
| 120 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 121 | std::string hex_encode_with_delimiter(const char* source, |
| 122 | size_t srclen, |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 123 | char delimiter) { |
| 124 | const size_t kBufferSize = srclen * 3; |
| 125 | char* buffer = STACK_ARRAY(char, kBufferSize); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 126 | size_t length = |
| 127 | hex_encode_with_delimiter(buffer, kBufferSize, source, srclen, delimiter); |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 128 | RTC_DCHECK(srclen == 0 || length > 0); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 129 | return std::string(buffer, length); |
| 130 | } |
| 131 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 132 | size_t hex_decode(char* cbuffer, |
| 133 | size_t buflen, |
| 134 | const char* source, |
| 135 | size_t srclen) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 136 | return hex_decode_with_delimiter(cbuffer, buflen, source, srclen, 0); |
| 137 | } |
| 138 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 139 | size_t hex_decode_with_delimiter(char* cbuffer, |
| 140 | size_t buflen, |
| 141 | const char* source, |
| 142 | size_t srclen, |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 143 | char delimiter) { |
Henrik Grunell | 8487988 | 2018-03-23 15:33:03 +0100 | [diff] [blame] | 144 | RTC_DCHECK(cbuffer); // TODO(kwiberg): estimate output size |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 145 | if (buflen == 0) |
| 146 | return 0; |
| 147 | |
| 148 | // Init and bounds check. |
| 149 | unsigned char* bbuffer = reinterpret_cast<unsigned char*>(cbuffer); |
| 150 | size_t srcpos = 0, bufpos = 0; |
| 151 | size_t needed = (delimiter) ? (srclen + 1) / 3 : srclen / 2; |
| 152 | if (buflen < needed) |
| 153 | return 0; |
| 154 | |
| 155 | while (srcpos < srclen) { |
| 156 | if ((srclen - srcpos) < 2) { |
| 157 | // This means we have an odd number of bytes. |
| 158 | return 0; |
| 159 | } |
| 160 | |
| 161 | unsigned char h1, h2; |
| 162 | if (!hex_decode(source[srcpos], &h1) || |
| 163 | !hex_decode(source[srcpos + 1], &h2)) |
| 164 | return 0; |
| 165 | |
| 166 | bbuffer[bufpos++] = (h1 << 4) | h2; |
| 167 | srcpos += 2; |
| 168 | |
| 169 | // Remove the delimiter if needed. |
| 170 | if (delimiter && (srclen - srcpos) > 1) { |
| 171 | if (source[srcpos] != delimiter) |
| 172 | return 0; |
| 173 | ++srcpos; |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | return bufpos; |
| 178 | } |
| 179 | |
| 180 | size_t hex_decode(char* buffer, size_t buflen, const std::string& source) { |
| 181 | return hex_decode_with_delimiter(buffer, buflen, source, 0); |
| 182 | } |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 183 | size_t hex_decode_with_delimiter(char* buffer, |
| 184 | size_t buflen, |
| 185 | const std::string& source, |
| 186 | char delimiter) { |
| 187 | return hex_decode_with_delimiter(buffer, buflen, source.c_str(), |
| 188 | source.length(), delimiter); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 189 | } |
| 190 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 191 | size_t transform(std::string& value, |
| 192 | size_t maxlen, |
| 193 | const std::string& source, |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 194 | Transform t) { |
| 195 | char* buffer = STACK_ARRAY(char, maxlen + 1); |
| 196 | size_t length = t(buffer, maxlen + 1, source.data(), source.length()); |
| 197 | value.assign(buffer, length); |
| 198 | return length; |
| 199 | } |
| 200 | |
| 201 | std::string s_transform(const std::string& source, Transform t) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 202 | // Ask transformation function to approximate the destination size (returns |
| 203 | // upper bound) |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 204 | size_t maxlen = t(nullptr, 0, source.data(), source.length()); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 205 | char* buffer = STACK_ARRAY(char, maxlen); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 206 | size_t len = t(buffer, maxlen, source.data(), source.length()); |
| 207 | std::string result(buffer, len); |
| 208 | return result; |
| 209 | } |
| 210 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 211 | size_t tokenize(const std::string& source, |
| 212 | char delimiter, |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 213 | std::vector<std::string>* fields) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 214 | fields->clear(); |
| 215 | size_t last = 0; |
| 216 | for (size_t i = 0; i < source.length(); ++i) { |
| 217 | if (source[i] == delimiter) { |
| 218 | if (i != last) { |
| 219 | fields->push_back(source.substr(last, i - last)); |
| 220 | } |
| 221 | last = i + 1; |
| 222 | } |
| 223 | } |
| 224 | if (last != source.length()) { |
| 225 | fields->push_back(source.substr(last, source.length() - last)); |
| 226 | } |
| 227 | return fields->size(); |
| 228 | } |
| 229 | |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 230 | size_t tokenize_with_empty_tokens(const std::string& source, |
| 231 | char delimiter, |
| 232 | std::vector<std::string>* fields) { |
| 233 | fields->clear(); |
| 234 | size_t last = 0; |
| 235 | for (size_t i = 0; i < source.length(); ++i) { |
| 236 | if (source[i] == delimiter) { |
| 237 | fields->push_back(source.substr(last, i - last)); |
| 238 | last = i + 1; |
| 239 | } |
| 240 | } |
| 241 | fields->push_back(source.substr(last, source.length() - last)); |
| 242 | return fields->size(); |
| 243 | } |
| 244 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 245 | size_t tokenize_append(const std::string& source, |
| 246 | char delimiter, |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 247 | std::vector<std::string>* fields) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 248 | if (!fields) |
| 249 | return 0; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 250 | |
| 251 | std::vector<std::string> new_fields; |
| 252 | tokenize(source, delimiter, &new_fields); |
| 253 | fields->insert(fields->end(), new_fields.begin(), new_fields.end()); |
| 254 | return fields->size(); |
| 255 | } |
| 256 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 257 | size_t tokenize(const std::string& source, |
| 258 | char delimiter, |
| 259 | char start_mark, |
| 260 | char end_mark, |
| 261 | std::vector<std::string>* fields) { |
| 262 | if (!fields) |
| 263 | return 0; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 264 | fields->clear(); |
| 265 | |
| 266 | std::string remain_source = source; |
| 267 | while (!remain_source.empty()) { |
| 268 | size_t start_pos = remain_source.find(start_mark); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 269 | if (std::string::npos == start_pos) |
| 270 | break; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 271 | std::string pre_mark; |
| 272 | if (start_pos > 0) { |
| 273 | pre_mark = remain_source.substr(0, start_pos - 1); |
| 274 | } |
| 275 | |
| 276 | ++start_pos; |
| 277 | size_t end_pos = remain_source.find(end_mark, start_pos); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 278 | if (std::string::npos == end_pos) |
| 279 | break; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 280 | |
| 281 | // We have found the matching marks. First tokenize the pre-mask. Then add |
| 282 | // the marked part as a single field. Finally, loop back for the post-mark. |
| 283 | tokenize_append(pre_mark, delimiter, fields); |
| 284 | fields->push_back(remain_source.substr(start_pos, end_pos - start_pos)); |
| 285 | remain_source = remain_source.substr(end_pos + 1); |
| 286 | } |
| 287 | |
| 288 | return tokenize_append(remain_source, delimiter, fields); |
| 289 | } |
| 290 | |
Donald Curtis | 144d018 | 2015-05-15 13:14:24 -0700 | [diff] [blame] | 291 | bool tokenize_first(const std::string& source, |
| 292 | const char delimiter, |
| 293 | std::string* token, |
| 294 | std::string* rest) { |
Donald Curtis | 0e07f92 | 2015-05-15 09:21:23 -0700 | [diff] [blame] | 295 | // Find the first delimiter |
| 296 | size_t left_pos = source.find(delimiter); |
| 297 | if (left_pos == std::string::npos) { |
| 298 | return false; |
| 299 | } |
| 300 | |
| 301 | // Look for additional occurrances of delimiter. |
| 302 | size_t right_pos = left_pos + 1; |
Donald Curtis | 144d018 | 2015-05-15 13:14:24 -0700 | [diff] [blame] | 303 | while (source[right_pos] == delimiter) { |
Donald Curtis | 0e07f92 | 2015-05-15 09:21:23 -0700 | [diff] [blame] | 304 | right_pos++; |
| 305 | } |
| 306 | |
| 307 | *token = source.substr(0, left_pos); |
| 308 | *rest = source.substr(right_pos); |
| 309 | return true; |
| 310 | } |
| 311 | |
Diogo Real | 7bd1f1b | 2017-09-08 12:50:41 -0700 | [diff] [blame] | 312 | std::string join(const std::vector<std::string>& source, char delimiter) { |
| 313 | if (source.size() == 0) { |
| 314 | return std::string(); |
| 315 | } |
| 316 | // Find length of the string to be returned to pre-allocate memory. |
| 317 | size_t source_string_length = 0; |
| 318 | for (size_t i = 0; i < source.size(); ++i) { |
| 319 | source_string_length += source[i].length(); |
| 320 | } |
| 321 | |
| 322 | // Build the joined string. |
| 323 | std::string joined_string; |
| 324 | joined_string.reserve(source_string_length + source.size() - 1); |
| 325 | for (size_t i = 0; i < source.size(); ++i) { |
| 326 | if (i != 0) { |
| 327 | joined_string += delimiter; |
| 328 | } |
| 329 | joined_string += source[i]; |
| 330 | } |
| 331 | return joined_string; |
| 332 | } |
| 333 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 334 | size_t split(const std::string& source, |
| 335 | char delimiter, |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 336 | std::vector<std::string>* fields) { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 337 | RTC_DCHECK(fields); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 338 | fields->clear(); |
| 339 | size_t last = 0; |
| 340 | for (size_t i = 0; i < source.length(); ++i) { |
| 341 | if (source[i] == delimiter) { |
| 342 | fields->push_back(source.substr(last, i - last)); |
| 343 | last = i + 1; |
| 344 | } |
| 345 | } |
| 346 | fields->push_back(source.substr(last, source.length() - last)); |
| 347 | return fields->size(); |
| 348 | } |
| 349 | |
Jonas Olsson | 6b1985d | 2018-07-05 11:59:48 +0200 | [diff] [blame] | 350 | std::string ToString(const bool b) { |
| 351 | return b ? "true" : "false"; |
| 352 | } |
| 353 | |
| 354 | std::string ToString(const char* const s) { |
| 355 | return std::string(s); |
| 356 | } |
| 357 | std::string ToString(const std::string s) { |
| 358 | return s; |
| 359 | } |
| 360 | |
| 361 | std::string ToString(const short s) { |
| 362 | char buf[32]; |
| 363 | const int len = std::snprintf(&buf[0], arraysize(buf), "%hd", s); |
| 364 | RTC_DCHECK_LE(len, arraysize(buf)); |
| 365 | return std::string(&buf[0], len); |
| 366 | } |
| 367 | std::string ToString(const unsigned short s) { |
| 368 | char buf[32]; |
| 369 | const int len = std::snprintf(&buf[0], arraysize(buf), "%hu", s); |
| 370 | RTC_DCHECK_LE(len, arraysize(buf)); |
| 371 | return std::string(&buf[0], len); |
| 372 | } |
| 373 | std::string ToString(const int s) { |
| 374 | char buf[32]; |
| 375 | const int len = std::snprintf(&buf[0], arraysize(buf), "%d", s); |
| 376 | RTC_DCHECK_LE(len, arraysize(buf)); |
| 377 | return std::string(&buf[0], len); |
| 378 | } |
| 379 | std::string ToString(const unsigned int s) { |
| 380 | char buf[32]; |
| 381 | const int len = std::snprintf(&buf[0], arraysize(buf), "%u", s); |
| 382 | RTC_DCHECK_LE(len, arraysize(buf)); |
| 383 | return std::string(&buf[0], len); |
| 384 | } |
| 385 | std::string ToString(const long int s) { |
| 386 | char buf[32]; |
| 387 | const int len = std::snprintf(&buf[0], arraysize(buf), "%ld", s); |
| 388 | RTC_DCHECK_LE(len, arraysize(buf)); |
| 389 | return std::string(&buf[0], len); |
| 390 | } |
| 391 | std::string ToString(const unsigned long int s) { |
| 392 | char buf[32]; |
| 393 | const int len = std::snprintf(&buf[0], arraysize(buf), "%lu", s); |
| 394 | RTC_DCHECK_LE(len, arraysize(buf)); |
| 395 | return std::string(&buf[0], len); |
| 396 | } |
| 397 | std::string ToString(const long long int s) { |
| 398 | char buf[32]; |
| 399 | const int len = std::snprintf(&buf[0], arraysize(buf), "%lld", s); |
| 400 | RTC_DCHECK_LE(len, arraysize(buf)); |
| 401 | return std::string(&buf[0], len); |
| 402 | } |
| 403 | std::string ToString(const unsigned long long int s) { |
| 404 | char buf[32]; |
| 405 | const int len = std::snprintf(&buf[0], arraysize(buf), "%llu", s); |
| 406 | RTC_DCHECK_LE(len, arraysize(buf)); |
| 407 | return std::string(&buf[0], len); |
| 408 | } |
| 409 | |
| 410 | std::string ToString(const double d) { |
| 411 | char buf[32]; |
| 412 | const int len = std::snprintf(&buf[0], arraysize(buf), "%g", d); |
| 413 | RTC_DCHECK_LE(len, arraysize(buf)); |
| 414 | return std::string(&buf[0], len); |
| 415 | } |
| 416 | |
Jonas Olsson | 88e1848 | 2018-09-03 10:15:08 +0200 | [diff] [blame] | 417 | std::string ToString(const long double d) { |
| 418 | char buf[32]; |
| 419 | const int len = std::snprintf(&buf[0], arraysize(buf), "%Lg", d); |
| 420 | RTC_DCHECK_LE(len, arraysize(buf)); |
| 421 | return std::string(&buf[0], len); |
| 422 | } |
| 423 | |
Jonas Olsson | 6b1985d | 2018-07-05 11:59:48 +0200 | [diff] [blame] | 424 | std::string ToString(const void* const p) { |
| 425 | char buf[32]; |
| 426 | const int len = std::snprintf(&buf[0], arraysize(buf), "%p", p); |
| 427 | RTC_DCHECK_LE(len, arraysize(buf)); |
| 428 | return std::string(&buf[0], len); |
| 429 | } |
| 430 | |
| 431 | bool FromString(const std::string& s, bool* b) { |
| 432 | if (s == "false") { |
| 433 | *b = false; |
| 434 | return true; |
| 435 | } |
| 436 | if (s == "true") { |
| 437 | *b = true; |
| 438 | return true; |
| 439 | } |
| 440 | return false; |
| 441 | } |
| 442 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 443 | } // namespace rtc |