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