| 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 |
| 11 | #ifndef RTC_BASE_STRINGENCODE_H_ |
| 12 | #define RTC_BASE_STRINGENCODE_H_ |
| 13 |
| 14 | #include <stddef.h> |
| 15 | #include <string> |
| 16 | #include <type_traits> |
| 17 | #include <vector> |
| 18 |
| 19 | #include "absl/types/optional.h" |
| 20 | #include "rtc_base/checks.h" |
| 21 | #include "rtc_base/string_to_number.h" |
| 22 |
| 23 | namespace rtc |
| 24 | { |
| 25 |
| 26 | ////////////////////////////////////////////////////////////////////// |
| 27 | // String Encoding Utilities |
| 28 | ////////////////////////////////////////////////////////////////////// |
| 29 |
| 30 | // Note: in-place decoding (buffer == source) is allowed. |
| 31 | size_t url_decode(char* buffer, |
| 32 | size_t buflen, |
| 33 | const char* source, |
| 34 | size_t srclen); |
| 35 |
| 36 | // Convert an unsigned value from 0 to 15 to the hex character equivalent... |
| 37 | char hex_encode(unsigned char val); |
| 38 | // ...and vice-versa. |
| 39 | bool hex_decode(char ch, unsigned char* val); |
| 40 |
| 41 | // hex_encode shows the hex representation of binary data in ascii. |
| 42 | size_t hex_encode(char* buffer, |
| 43 | size_t buflen, |
| 44 | const char* source, |
| 45 | size_t srclen); |
| 46 |
| 47 | // hex_encode, but separate each byte representation with a delimiter. |
| 48 | // |delimiter| == 0 means no delimiter |
| 49 | // If the buffer is too short, we return 0 |
| 50 | size_t hex_encode_with_delimiter(char* buffer, |
| 51 | size_t buflen, |
| 52 | const char* source, |
| 53 | size_t srclen, |
| 54 | char delimiter); |
| 55 |
| 56 | // Helper functions for hex_encode. |
| 57 | std::string hex_encode(const std::string& str); |
| 58 | std::string hex_encode(const char* source, size_t srclen); |
| 59 | std::string hex_encode_with_delimiter(const char* source, |
| 60 | size_t srclen, |
| 61 | char delimiter); |
| 62 |
| 63 | // hex_decode converts ascii hex to binary. |
| 64 | size_t hex_decode(char* buffer, |
| 65 | size_t buflen, |
| 66 | const char* source, |
| 67 | size_t srclen); |
| 68 |
| 69 | // hex_decode, assuming that there is a delimiter between every byte |
| 70 | // pair. |
| 71 | // |delimiter| == 0 means no delimiter |
| 72 | // If the buffer is too short or the data is invalid, we return 0. |
| 73 | size_t hex_decode_with_delimiter(char* buffer, |
| 74 | size_t buflen, |
| 75 | const char* source, |
| 76 | size_t srclen, |
| 77 | char delimiter); |
| 78 |
| 79 | // Helper functions for hex_decode. |
| 80 | size_t hex_decode(char* buffer, size_t buflen, const std::string& source); |
| 81 | size_t hex_decode_with_delimiter(char* buffer, |
| 82 | size_t buflen, |
| 83 | const std::string& source, |
| 84 | char delimiter); |
| 85 |
| 86 | // Apply any suitable string transform (including the ones above) to an STL |
| 87 | // string. Stack-allocated temporary space is used for the transformation, |
| 88 | // so value and source may refer to the same string. |
| 89 | typedef size_t (*Transform)(char* buffer, |
| 90 | size_t buflen, |
| 91 | const char* source, |
| 92 | size_t srclen); |
| 93 | size_t transform(std::string& value, |
| 94 | size_t maxlen, |
| 95 | const std::string& source, |
| 96 | Transform t); |
| 97 |
| 98 | // Return the result of applying transform t to source. |
| 99 | std::string s_transform(const std::string& source, Transform t); |
| 100 |
| 101 | // Convenience wrappers. |
| 102 | inline std::string s_url_decode(const std::string& source) |
| 103 | { |
| 104 | return s_transform(source, url_decode); |
| 105 | } |
| 106 |
| 107 | // Joins the source vector of strings into a single string, with each |
| 108 | // field in source being separated by delimiter. No trailing delimiter is added. |
| 109 | std::string join(const std::vector<std::string>& source, char delimiter); |
| 110 |
| 111 | // Splits the source string into multiple fields separated by delimiter, |
| 112 | // with duplicates of delimiter creating empty fields. |
| 113 | size_t split(const std::string& source, |
| 114 | char delimiter, |
| 115 | std::vector<std::string>* fields); |
| 116 |
| 117 | // Splits the source string into multiple fields separated by delimiter, |
| 118 | // with duplicates of delimiter ignored. Trailing delimiter ignored. |
| 119 | size_t tokenize(const std::string& source, |
| 120 | char delimiter, |
| 121 | std::vector<std::string>* fields); |
| 122 |
| 123 | // Tokenize, including the empty tokens. |
| 124 | size_t tokenize_with_empty_tokens(const std::string& source, |
| 125 | char delimiter, |
| 126 | std::vector<std::string>* fields); |
| 127 |
| 128 | // Tokenize and append the tokens to fields. Return the new size of fields. |
| 129 | size_t tokenize_append(const std::string& source, |
| 130 | char delimiter, |
| 131 | std::vector<std::string>* fields); |
| 132 |
| 133 | // Splits the source string into multiple fields separated by delimiter, with |
| 134 | // duplicates of delimiter ignored. Trailing delimiter ignored. A substring in |
| 135 | // between the start_mark and the end_mark is treated as a single field. Return |
| 136 | // the size of fields. For example, if source is "filename |
| 137 | // \"/Library/Application Support/media content.txt\"", delimiter is ' ', and |
| 138 | // the start_mark and end_mark are '"', this method returns two fields: |
| 139 | // "filename" and "/Library/Application Support/media content.txt". |
| 140 | size_t tokenize(const std::string& source, |
| 141 | char delimiter, |
| 142 | char start_mark, |
| 143 | char end_mark, |
| 144 | std::vector<std::string>* fields); |
| 145 |
| 146 | // Extract the first token from source as separated by delimiter, with |
| 147 | // duplicates of delimiter ignored. Return false if the delimiter could not be |
| 148 | // found, otherwise return true. |
| 149 | bool tokenize_first(const std::string& source, |
| 150 | const char delimiter, |
| 151 | std::string* token, |
| 152 | std::string* rest); |
| 153 |
| 154 | // Convert arbitrary values to/from a string. |
| 155 | // TODO(jonasolsson): Remove these when absl::StrCat becomes available. |
| 156 | std::string ToString(bool b); |
| 157 |
| 158 | std::string ToString(const char* s); |
| 159 | std::string ToString(std::string t); |
| 160 |
| 161 | std::string ToString(short s); |
| 162 | std::string ToString(unsigned short s); |
| 163 | std::string ToString(int s); |
| 164 | std::string ToString(unsigned int s); |
| 165 | std::string ToString(long int s); |
| 166 | std::string ToString(unsigned long int s); |
| 167 | std::string ToString(long long int s); |
| 168 | std::string ToString(unsigned long long int s); |
| 169 |
| 170 | std::string ToString(double t); |
| 171 | std::string ToString(long double t); |
| 172 |
| 173 | std::string ToString(const void* p); |
| 174 |
| 175 | template <typename T, |
| 176 | typename std::enable_if<std::is_arithmetic<T>::value && !std::is_same<T, bool>::value, |
| 177 | int>::type |
| 178 | = 0> |
| 179 | static bool FromString(const std::string& s, T* t) |
| 180 | { |
| 181 | RTC_DCHECK(t); |
| 182 | absl::optional<T> result = StringToNumber<T>(s); |
| 183 |
| 184 | if (result) |
| 185 | *t = *result; |
| 186 |
| 187 | return result.has_value(); |
| 188 | } |
| 189 |
| 190 | bool FromString(const std::string& s, bool* b); |
| 191 |
| 192 | template <typename T> |
| 193 | static inline T FromString(const std::string& str) |
| 194 | { |
| 195 | T val; |
| 196 | FromString(str, &val); |
| 197 | return val; |
| 198 | } |
| 199 |
| 200 | ////////////////////////////////////////////////////////////////////// |
| 201 |
| 202 | } // namespace rtc |
| 203 |
| 204 | #endif // RTC_BASE_STRINGENCODE_H__ |
| 205 |