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