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