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