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