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
11 #ifndef RTC_BASE_STRING_ENCODE_H_
12 #define RTC_BASE_STRING_ENCODE_H_
13
14 #include <stddef.h>
15
16 #include <string>
17 #include <type_traits>
18 #include <vector>
19
20 #include "absl/strings/string_view.h"
21 #include "absl/types/optional.h"
22 #include "api/array_view.h"
23 #include "rtc_base/checks.h"
24 #include "rtc_base/string_to_number.h"
25
26 namespace rtc {
27
28 //////////////////////////////////////////////////////////////////////
29 // String Encoding Utilities
30 //////////////////////////////////////////////////////////////////////
31
32 std::string hex_encode(absl::string_view str);
33 std::string hex_encode_with_delimiter(absl::string_view source, char delimiter);
34
35 // hex_decode converts ascii hex to binary.
36 size_t hex_decode(ArrayView<char> buffer, absl::string_view source);
37
38 // hex_decode, assuming that there is a delimiter between every byte
39 // pair.
40 // `delimiter` == 0 means no delimiter
41 // If the buffer is too short or the data is invalid, we return 0.
42 size_t hex_decode_with_delimiter(ArrayView<char> buffer,
43 absl::string_view source,
44 char delimiter);
45
46 // Splits the source string into multiple fields separated by delimiter,
47 // with duplicates of delimiter creating empty fields. Empty input produces a
48 // single, empty, field.
49 std::vector<absl::string_view> split(absl::string_view source, char delimiter);
50
51 // Splits the source string into multiple fields separated by delimiter,
52 // with duplicates of delimiter ignored. Trailing delimiter ignored.
53 size_t tokenize(absl::string_view source,
54 char delimiter,
55 std::vector<std::string>* fields);
56
57 // Extract the first token from source as separated by delimiter, with
58 // duplicates of delimiter ignored. Return false if the delimiter could not be
59 // found, otherwise return true.
60 bool tokenize_first(absl::string_view source,
61 char delimiter,
62 std::string* token,
63 std::string* rest);
64
65 // Convert arbitrary values to/from a string.
66 // TODO(jonasolsson): Remove these when absl::StrCat becomes available.
67 std::string ToString(bool b);
68
69 std::string ToString(absl::string_view s);
70 // The const char* overload is needed for correct overload resolution because of
71 // the const void* version of ToString() below.
72 std::string ToString(const char* s);
73
74 std::string ToString(short s);
75 std::string ToString(unsigned short s);
76 std::string ToString(int s);
77 std::string ToString(unsigned int s);
78 std::string ToString(long int s);
79 std::string ToString(unsigned long int s);
80 std::string ToString(long long int s);
81 std::string ToString(unsigned long long int s);
82
83 std::string ToString(double t);
84 std::string ToString(long double t);
85
86 std::string ToString(const void* p);
87
88 template <typename T,
89 typename std::enable_if<std::is_arithmetic<T>::value &&
90 !std::is_same<T, bool>::value,
91 int>::type = 0>
FromString(absl::string_view s,T * t)92 static bool FromString(absl::string_view s, T* t) {
93 RTC_DCHECK(t);
94 absl::optional<T> result = StringToNumber<T>(s);
95
96 if (result)
97 *t = *result;
98
99 return result.has_value();
100 }
101
102 bool FromString(absl::string_view s, bool* b);
103
104 template <typename T>
FromString(absl::string_view str)105 static inline T FromString(absl::string_view str) {
106 T val;
107 FromString(str, &val);
108 return val;
109 }
110
111 //////////////////////////////////////////////////////////////////////
112
113 } // namespace rtc
114
115 #endif // RTC_BASE_STRING_ENCODE_H__
116