You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

387 lines
11 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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. #include "rtc_base/string_encode.h"
  11. #include <cstdio>
  12. #include "rtc_base/arraysize.h"
  13. #include "rtc_base/checks.h"
  14. namespace rtc {
  15. /////////////////////////////////////////////////////////////////////////////
  16. // String Encoding Utilities
  17. /////////////////////////////////////////////////////////////////////////////
  18. namespace {
  19. const char HEX[] = "0123456789abcdef";
  20. // Convert an unsigned value from 0 to 15 to the hex character equivalent...
  21. char hex_encode(unsigned char val) {
  22. RTC_DCHECK_LT(val, 16);
  23. return (val < 16) ? HEX[val] : '!';
  24. }
  25. // ...and vice-versa.
  26. bool hex_decode(char ch, unsigned char* val) {
  27. if ((ch >= '0') && (ch <= '9')) {
  28. *val = ch - '0';
  29. } else if ((ch >= 'A') && (ch <= 'F')) {
  30. *val = (ch - 'A') + 10;
  31. } else if ((ch >= 'a') && (ch <= 'f')) {
  32. *val = (ch - 'a') + 10;
  33. } else {
  34. return false;
  35. }
  36. return true;
  37. }
  38. size_t hex_encode_output_length(size_t srclen, char delimiter) {
  39. return delimiter && srclen > 0 ? (srclen * 3 - 1) : (srclen * 2);
  40. }
  41. // hex_encode shows the hex representation of binary data in ascii, with
  42. // |delimiter| between bytes, or none if |delimiter| == 0.
  43. void hex_encode_with_delimiter(char* buffer,
  44. const char* csource,
  45. size_t srclen,
  46. char delimiter) {
  47. RTC_DCHECK(buffer);
  48. // Init and check bounds.
  49. const unsigned char* bsource =
  50. reinterpret_cast<const unsigned char*>(csource);
  51. size_t srcpos = 0, bufpos = 0;
  52. while (srcpos < srclen) {
  53. unsigned char ch = bsource[srcpos++];
  54. buffer[bufpos] = hex_encode((ch >> 4) & 0xF);
  55. buffer[bufpos + 1] = hex_encode((ch)&0xF);
  56. bufpos += 2;
  57. // Don't write a delimiter after the last byte.
  58. if (delimiter && (srcpos < srclen)) {
  59. buffer[bufpos] = delimiter;
  60. ++bufpos;
  61. }
  62. }
  63. }
  64. } // namespace
  65. std::string hex_encode(const std::string& str) {
  66. return hex_encode(str.c_str(), str.size());
  67. }
  68. std::string hex_encode(const char* source, size_t srclen) {
  69. return hex_encode_with_delimiter(source, srclen, 0);
  70. }
  71. std::string hex_encode_with_delimiter(const char* source,
  72. size_t srclen,
  73. char delimiter) {
  74. std::string s(hex_encode_output_length(srclen, delimiter), 0);
  75. hex_encode_with_delimiter(&s[0], source, srclen, delimiter);
  76. return s;
  77. }
  78. size_t hex_decode(char* cbuffer,
  79. size_t buflen,
  80. const char* source,
  81. size_t srclen) {
  82. return hex_decode_with_delimiter(cbuffer, buflen, source, srclen, 0);
  83. }
  84. size_t hex_decode_with_delimiter(char* cbuffer,
  85. size_t buflen,
  86. const char* source,
  87. size_t srclen,
  88. char delimiter) {
  89. RTC_DCHECK(cbuffer); // TODO(kwiberg): estimate output size
  90. if (buflen == 0)
  91. return 0;
  92. // Init and bounds check.
  93. unsigned char* bbuffer = reinterpret_cast<unsigned char*>(cbuffer);
  94. size_t srcpos = 0, bufpos = 0;
  95. size_t needed = (delimiter) ? (srclen + 1) / 3 : srclen / 2;
  96. if (buflen < needed)
  97. return 0;
  98. while (srcpos < srclen) {
  99. if ((srclen - srcpos) < 2) {
  100. // This means we have an odd number of bytes.
  101. return 0;
  102. }
  103. unsigned char h1, h2;
  104. if (!hex_decode(source[srcpos], &h1) ||
  105. !hex_decode(source[srcpos + 1], &h2))
  106. return 0;
  107. bbuffer[bufpos++] = (h1 << 4) | h2;
  108. srcpos += 2;
  109. // Remove the delimiter if needed.
  110. if (delimiter && (srclen - srcpos) > 1) {
  111. if (source[srcpos] != delimiter)
  112. return 0;
  113. ++srcpos;
  114. }
  115. }
  116. return bufpos;
  117. }
  118. size_t hex_decode(char* buffer, size_t buflen, const std::string& source) {
  119. return hex_decode_with_delimiter(buffer, buflen, source, 0);
  120. }
  121. size_t hex_decode_with_delimiter(char* buffer,
  122. size_t buflen,
  123. const std::string& source,
  124. char delimiter) {
  125. return hex_decode_with_delimiter(buffer, buflen, source.c_str(),
  126. source.length(), delimiter);
  127. }
  128. size_t tokenize(const std::string& source,
  129. char delimiter,
  130. std::vector<std::string>* fields) {
  131. fields->clear();
  132. size_t last = 0;
  133. for (size_t i = 0; i < source.length(); ++i) {
  134. if (source[i] == delimiter) {
  135. if (i != last) {
  136. fields->push_back(source.substr(last, i - last));
  137. }
  138. last = i + 1;
  139. }
  140. }
  141. if (last != source.length()) {
  142. fields->push_back(source.substr(last, source.length() - last));
  143. }
  144. return fields->size();
  145. }
  146. size_t tokenize_with_empty_tokens(const std::string& source,
  147. char delimiter,
  148. std::vector<std::string>* fields) {
  149. fields->clear();
  150. size_t last = 0;
  151. for (size_t i = 0; i < source.length(); ++i) {
  152. if (source[i] == delimiter) {
  153. fields->push_back(source.substr(last, i - last));
  154. last = i + 1;
  155. }
  156. }
  157. fields->push_back(source.substr(last, source.length() - last));
  158. return fields->size();
  159. }
  160. size_t tokenize_append(const std::string& source,
  161. char delimiter,
  162. std::vector<std::string>* fields) {
  163. if (!fields)
  164. return 0;
  165. std::vector<std::string> new_fields;
  166. tokenize(source, delimiter, &new_fields);
  167. fields->insert(fields->end(), new_fields.begin(), new_fields.end());
  168. return fields->size();
  169. }
  170. size_t tokenize(const std::string& source,
  171. char delimiter,
  172. char start_mark,
  173. char end_mark,
  174. std::vector<std::string>* fields) {
  175. if (!fields)
  176. return 0;
  177. fields->clear();
  178. std::string remain_source = source;
  179. while (!remain_source.empty()) {
  180. size_t start_pos = remain_source.find(start_mark);
  181. if (std::string::npos == start_pos)
  182. break;
  183. std::string pre_mark;
  184. if (start_pos > 0) {
  185. pre_mark = remain_source.substr(0, start_pos - 1);
  186. }
  187. ++start_pos;
  188. size_t end_pos = remain_source.find(end_mark, start_pos);
  189. if (std::string::npos == end_pos)
  190. break;
  191. // We have found the matching marks. First tokenize the pre-mask. Then add
  192. // the marked part as a single field. Finally, loop back for the post-mark.
  193. tokenize_append(pre_mark, delimiter, fields);
  194. fields->push_back(remain_source.substr(start_pos, end_pos - start_pos));
  195. remain_source = remain_source.substr(end_pos + 1);
  196. }
  197. return tokenize_append(remain_source, delimiter, fields);
  198. }
  199. bool tokenize_first(const std::string& source,
  200. const char delimiter,
  201. std::string* token,
  202. std::string* rest) {
  203. // Find the first delimiter
  204. size_t left_pos = source.find(delimiter);
  205. if (left_pos == std::string::npos) {
  206. return false;
  207. }
  208. // Look for additional occurrances of delimiter.
  209. size_t right_pos = left_pos + 1;
  210. while (source[right_pos] == delimiter) {
  211. right_pos++;
  212. }
  213. *token = source.substr(0, left_pos);
  214. *rest = source.substr(right_pos);
  215. return true;
  216. }
  217. std::string join(const std::vector<std::string>& source, char delimiter) {
  218. if (source.size() == 0) {
  219. return std::string();
  220. }
  221. // Find length of the string to be returned to pre-allocate memory.
  222. size_t source_string_length = 0;
  223. for (size_t i = 0; i < source.size(); ++i) {
  224. source_string_length += source[i].length();
  225. }
  226. // Build the joined string.
  227. std::string joined_string;
  228. joined_string.reserve(source_string_length + source.size() - 1);
  229. for (size_t i = 0; i < source.size(); ++i) {
  230. if (i != 0) {
  231. joined_string += delimiter;
  232. }
  233. joined_string += source[i];
  234. }
  235. return joined_string;
  236. }
  237. size_t split(const std::string& source,
  238. char delimiter,
  239. std::vector<std::string>* fields) {
  240. RTC_DCHECK(fields);
  241. fields->clear();
  242. size_t last = 0;
  243. for (size_t i = 0; i < source.length(); ++i) {
  244. if (source[i] == delimiter) {
  245. fields->push_back(source.substr(last, i - last));
  246. last = i + 1;
  247. }
  248. }
  249. fields->push_back(source.substr(last, source.length() - last));
  250. return fields->size();
  251. }
  252. std::string ToString(const bool b) {
  253. return b ? "true" : "false";
  254. }
  255. std::string ToString(const char* const s) {
  256. return std::string(s);
  257. }
  258. std::string ToString(const std::string s) {
  259. return s;
  260. }
  261. std::string ToString(const short s) {
  262. char buf[32];
  263. const int len = std::snprintf(&buf[0], arraysize(buf), "%hd", s);
  264. RTC_DCHECK_LE(len, arraysize(buf));
  265. return std::string(&buf[0], len);
  266. }
  267. std::string ToString(const unsigned short s) {
  268. char buf[32];
  269. const int len = std::snprintf(&buf[0], arraysize(buf), "%hu", s);
  270. RTC_DCHECK_LE(len, arraysize(buf));
  271. return std::string(&buf[0], len);
  272. }
  273. std::string ToString(const int s) {
  274. char buf[32];
  275. const int len = std::snprintf(&buf[0], arraysize(buf), "%d", s);
  276. RTC_DCHECK_LE(len, arraysize(buf));
  277. return std::string(&buf[0], len);
  278. }
  279. std::string ToString(const unsigned int s) {
  280. char buf[32];
  281. const int len = std::snprintf(&buf[0], arraysize(buf), "%u", s);
  282. RTC_DCHECK_LE(len, arraysize(buf));
  283. return std::string(&buf[0], len);
  284. }
  285. std::string ToString(const long int s) {
  286. char buf[32];
  287. const int len = std::snprintf(&buf[0], arraysize(buf), "%ld", s);
  288. RTC_DCHECK_LE(len, arraysize(buf));
  289. return std::string(&buf[0], len);
  290. }
  291. std::string ToString(const unsigned long int s) {
  292. char buf[32];
  293. const int len = std::snprintf(&buf[0], arraysize(buf), "%lu", s);
  294. RTC_DCHECK_LE(len, arraysize(buf));
  295. return std::string(&buf[0], len);
  296. }
  297. std::string ToString(const long long int s) {
  298. char buf[32];
  299. const int len = std::snprintf(&buf[0], arraysize(buf), "%lld", s);
  300. RTC_DCHECK_LE(len, arraysize(buf));
  301. return std::string(&buf[0], len);
  302. }
  303. std::string ToString(const unsigned long long int s) {
  304. char buf[32];
  305. const int len = std::snprintf(&buf[0], arraysize(buf), "%llu", s);
  306. RTC_DCHECK_LE(len, arraysize(buf));
  307. return std::string(&buf[0], len);
  308. }
  309. std::string ToString(const double d) {
  310. char buf[32];
  311. const int len = std::snprintf(&buf[0], arraysize(buf), "%g", d);
  312. RTC_DCHECK_LE(len, arraysize(buf));
  313. return std::string(&buf[0], len);
  314. }
  315. std::string ToString(const long double d) {
  316. char buf[32];
  317. const int len = std::snprintf(&buf[0], arraysize(buf), "%Lg", d);
  318. RTC_DCHECK_LE(len, arraysize(buf));
  319. return std::string(&buf[0], len);
  320. }
  321. std::string ToString(const void* const p) {
  322. char buf[32];
  323. const int len = std::snprintf(&buf[0], arraysize(buf), "%p", p);
  324. RTC_DCHECK_LE(len, arraysize(buf));
  325. return std::string(&buf[0], len);
  326. }
  327. bool FromString(const std::string& s, bool* b) {
  328. if (s == "false") {
  329. *b = false;
  330. return true;
  331. }
  332. if (s == "true") {
  333. *b = true;
  334. return true;
  335. }
  336. return false;
  337. }
  338. } // namespace rtc