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