string_encode.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. #ifndef RTC_BASE_STRING_ENCODE_H_
  11. #define RTC_BASE_STRING_ENCODE_H_
  12. #include <stddef.h>
  13. #include <string>
  14. #include <type_traits>
  15. #include <vector>
  16. #include "absl/types/optional.h"
  17. #include "rtc_base/checks.h"
  18. #include "rtc_base/string_to_number.h"
  19. namespace rtc {
  20. //////////////////////////////////////////////////////////////////////
  21. // String Encoding Utilities
  22. //////////////////////////////////////////////////////////////////////
  23. std::string hex_encode(const std::string& str);
  24. std::string hex_encode(const char* source, size_t srclen);
  25. std::string hex_encode_with_delimiter(const char* source,
  26. size_t srclen,
  27. char delimiter);
  28. // hex_decode converts ascii hex to binary.
  29. size_t hex_decode(char* buffer,
  30. size_t buflen,
  31. const char* source,
  32. size_t srclen);
  33. // hex_decode, assuming that there is a delimiter between every byte
  34. // pair.
  35. // |delimiter| == 0 means no delimiter
  36. // If the buffer is too short or the data is invalid, we return 0.
  37. size_t hex_decode_with_delimiter(char* buffer,
  38. size_t buflen,
  39. const char* source,
  40. size_t srclen,
  41. char delimiter);
  42. // Helper functions for hex_decode.
  43. size_t hex_decode(char* buffer, size_t buflen, const std::string& source);
  44. size_t hex_decode_with_delimiter(char* buffer,
  45. size_t buflen,
  46. const std::string& source,
  47. char delimiter);
  48. // Joins the source vector of strings into a single string, with each
  49. // field in source being separated by delimiter. No trailing delimiter is added.
  50. std::string join(const std::vector<std::string>& source, char delimiter);
  51. // Splits the source string into multiple fields separated by delimiter,
  52. // with duplicates of delimiter creating empty fields.
  53. size_t split(const std::string& source,
  54. char delimiter,
  55. std::vector<std::string>* fields);
  56. // Splits the source string into multiple fields separated by delimiter,
  57. // with duplicates of delimiter ignored. Trailing delimiter ignored.
  58. size_t tokenize(const std::string& source,
  59. char delimiter,
  60. std::vector<std::string>* fields);
  61. // Tokenize, including the empty tokens.
  62. size_t tokenize_with_empty_tokens(const std::string& source,
  63. char delimiter,
  64. std::vector<std::string>* fields);
  65. // Tokenize and append the tokens to fields. Return the new size of fields.
  66. size_t tokenize_append(const std::string& source,
  67. char delimiter,
  68. std::vector<std::string>* fields);
  69. // Splits the source string into multiple fields separated by delimiter, with
  70. // duplicates of delimiter ignored. Trailing delimiter ignored. A substring in
  71. // between the start_mark and the end_mark is treated as a single field. Return
  72. // the size of fields. For example, if source is "filename
  73. // \"/Library/Application Support/media content.txt\"", delimiter is ' ', and
  74. // the start_mark and end_mark are '"', this method returns two fields:
  75. // "filename" and "/Library/Application Support/media content.txt".
  76. size_t tokenize(const std::string& source,
  77. char delimiter,
  78. char start_mark,
  79. char end_mark,
  80. std::vector<std::string>* fields);
  81. // Extract the first token from source as separated by delimiter, with
  82. // duplicates of delimiter ignored. Return false if the delimiter could not be
  83. // found, otherwise return true.
  84. bool tokenize_first(const std::string& source,
  85. const char delimiter,
  86. std::string* token,
  87. std::string* rest);
  88. // Convert arbitrary values to/from a string.
  89. // TODO(jonasolsson): Remove these when absl::StrCat becomes available.
  90. std::string ToString(bool b);
  91. std::string ToString(const char* s);
  92. std::string ToString(std::string t);
  93. std::string ToString(short s);
  94. std::string ToString(unsigned short s);
  95. std::string ToString(int s);
  96. std::string ToString(unsigned int s);
  97. std::string ToString(long int s);
  98. std::string ToString(unsigned long int s);
  99. std::string ToString(long long int s);
  100. std::string ToString(unsigned long long int s);
  101. std::string ToString(double t);
  102. std::string ToString(long double t);
  103. std::string ToString(const void* p);
  104. template <typename T,
  105. typename std::enable_if<std::is_arithmetic<T>::value &&
  106. !std::is_same<T, bool>::value,
  107. int>::type = 0>
  108. static bool FromString(const std::string& s, T* t) {
  109. RTC_DCHECK(t);
  110. absl::optional<T> result = StringToNumber<T>(s);
  111. if (result)
  112. *t = *result;
  113. return result.has_value();
  114. }
  115. bool FromString(const std::string& s, bool* b);
  116. template <typename T>
  117. static inline T FromString(const std::string& str) {
  118. T val;
  119. FromString(str, &val);
  120. return val;
  121. }
  122. //////////////////////////////////////////////////////////////////////
  123. } // namespace rtc
  124. #endif // RTC_BASE_STRING_ENCODE_H__