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