• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/types/optional.h"
21 #include "rtc_base/checks.h"
22 #include "rtc_base/string_to_number.h"
23 
24 namespace rtc {
25 
26 //////////////////////////////////////////////////////////////////////
27 // String Encoding Utilities
28 //////////////////////////////////////////////////////////////////////
29 
30 std::string hex_encode(const std::string& str);
31 std::string hex_encode(const char* source, size_t srclen);
32 std::string hex_encode_with_delimiter(const char* source,
33                                       size_t srclen,
34                                       char delimiter);
35 
36 // hex_decode converts ascii hex to binary.
37 size_t hex_decode(char* buffer,
38                   size_t buflen,
39                   const char* source,
40                   size_t srclen);
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.
46 size_t hex_decode_with_delimiter(char* buffer,
47                                  size_t buflen,
48                                  const char* source,
49                                  size_t srclen,
50                                  char delimiter);
51 
52 // Helper functions for hex_decode.
53 size_t hex_decode(char* buffer, size_t buflen, const std::string& source);
54 size_t hex_decode_with_delimiter(char* buffer,
55                                  size_t buflen,
56                                  const std::string& source,
57                                  char delimiter);
58 
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 
63 // Splits the source string into multiple fields separated by delimiter,
64 // with duplicates of delimiter creating empty fields.
65 size_t split(const std::string& source,
66              char delimiter,
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.
71 size_t tokenize(const std::string& source,
72                 char delimiter,
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.
81 size_t tokenize_append(const std::string& source,
82                        char delimiter,
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".
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);
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 
106 // Convert arbitrary values to/from a string.
107 // TODO(jonasolsson): Remove these when absl::StrCat becomes available.
108 std::string ToString(bool b);
109 
110 std::string ToString(const char* s);
111 std::string ToString(std::string t);
112 
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);
123 std::string ToString(long double t);
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>
FromString(const std::string & s,T * t)131 static bool FromString(const std::string& s, T* t) {
132   RTC_DCHECK(t);
133   absl::optional<T> result = StringToNumber<T>(s);
134 
135   if (result)
136     *t = *result;
137 
138   return result.has_value();
139 }
140 
141 bool FromString(const std::string& s, bool* b);
142 
143 template <typename T>
FromString(const std::string & str)144 static inline T FromString(const std::string& str) {
145   T val;
146   FromString(str, &val);
147   return val;
148 }
149 
150 //////////////////////////////////////////////////////////////////////
151 
152 }  // namespace rtc
153 
154 #endif  // RTC_BASE_STRING_ENCODE_H__
155