blob: 5d436dfd57ba08a57307f5ac3d3103e3fce6e081 [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef RTC_BASE_STRINGENCODE_H_
12#define RTC_BASE_STRINGENCODE_H_
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000013
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020014#include <string>
15#include <vector>
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000016
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "rtc_base/checks.h"
Jonas Olsson6b1985d2018-07-05 11:59:48 +020018#include "rtc_base/string_to_number.h"
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020019
20namespace rtc {
21
22//////////////////////////////////////////////////////////////////////
23// String Encoding Utilities
24//////////////////////////////////////////////////////////////////////
25
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020026// Note: in-place decoding (buffer == source) is allowed.
Yves Gerey665174f2018-06-19 15:03:05 +020027size_t url_decode(char* buffer,
28 size_t buflen,
29 const char* source,
30 size_t srclen);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020031
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020032// Convert an unsigned value from 0 to 15 to the hex character equivalent...
33char hex_encode(unsigned char val);
34// ...and vice-versa.
35bool hex_decode(char ch, unsigned char* val);
36
37// hex_encode shows the hex representation of binary data in ascii.
Yves Gerey665174f2018-06-19 15:03:05 +020038size_t hex_encode(char* buffer,
39 size_t buflen,
40 const char* source,
41 size_t srclen);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020042
43// hex_encode, but separate each byte representation with a delimiter.
44// |delimiter| == 0 means no delimiter
45// If the buffer is too short, we return 0
Yves Gerey665174f2018-06-19 15:03:05 +020046size_t hex_encode_with_delimiter(char* buffer,
47 size_t buflen,
48 const char* source,
49 size_t srclen,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020050 char delimiter);
51
52// Helper functions for hex_encode.
53std::string hex_encode(const std::string& str);
54std::string hex_encode(const char* source, size_t srclen);
Yves Gerey665174f2018-06-19 15:03:05 +020055std::string hex_encode_with_delimiter(const char* source,
56 size_t srclen,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020057 char delimiter);
58
59// hex_decode converts ascii hex to binary.
Yves Gerey665174f2018-06-19 15:03:05 +020060size_t hex_decode(char* buffer,
61 size_t buflen,
62 const char* source,
63 size_t srclen);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020064
65// hex_decode, assuming that there is a delimiter between every byte
66// pair.
67// |delimiter| == 0 means no delimiter
68// If the buffer is too short or the data is invalid, we return 0.
Yves Gerey665174f2018-06-19 15:03:05 +020069size_t hex_decode_with_delimiter(char* buffer,
70 size_t buflen,
71 const char* source,
72 size_t srclen,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020073 char delimiter);
74
75// Helper functions for hex_decode.
76size_t hex_decode(char* buffer, size_t buflen, const std::string& source);
Yves Gerey665174f2018-06-19 15:03:05 +020077size_t hex_decode_with_delimiter(char* buffer,
78 size_t buflen,
79 const std::string& source,
80 char delimiter);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020081
82// Apply any suitable string transform (including the ones above) to an STL
83// string. Stack-allocated temporary space is used for the transformation,
84// so value and source may refer to the same string.
Yves Gerey665174f2018-06-19 15:03:05 +020085typedef size_t (*Transform)(char* buffer,
86 size_t buflen,
87 const char* source,
88 size_t srclen);
89size_t transform(std::string& value,
90 size_t maxlen,
91 const std::string& source,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020092 Transform t);
93
94// Return the result of applying transform t to source.
95std::string s_transform(const std::string& source, Transform t);
96
97// Convenience wrappers.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020098inline std::string s_url_decode(const std::string& source) {
99 return s_transform(source, url_decode);
100}
101
Diogo Real7bd1f1b2017-09-08 12:50:41 -0700102// Joins the source vector of strings into a single string, with each
103// field in source being separated by delimiter. No trailing delimiter is added.
104std::string join(const std::vector<std::string>& source, char delimiter);
105
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200106// Splits the source string into multiple fields separated by delimiter,
107// with duplicates of delimiter creating empty fields.
Yves Gerey665174f2018-06-19 15:03:05 +0200108size_t split(const std::string& source,
109 char delimiter,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200110 std::vector<std::string>* fields);
111
112// Splits the source string into multiple fields separated by delimiter,
113// with duplicates of delimiter ignored. Trailing delimiter ignored.
Yves Gerey665174f2018-06-19 15:03:05 +0200114size_t tokenize(const std::string& source,
115 char delimiter,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200116 std::vector<std::string>* fields);
117
118// Tokenize, including the empty tokens.
119size_t tokenize_with_empty_tokens(const std::string& source,
120 char delimiter,
121 std::vector<std::string>* fields);
122
123// Tokenize and append the tokens to fields. Return the new size of fields.
Yves Gerey665174f2018-06-19 15:03:05 +0200124size_t tokenize_append(const std::string& source,
125 char delimiter,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200126 std::vector<std::string>* fields);
127
128// Splits the source string into multiple fields separated by delimiter, with
129// duplicates of delimiter ignored. Trailing delimiter ignored. A substring in
130// between the start_mark and the end_mark is treated as a single field. Return
131// the size of fields. For example, if source is "filename
132// \"/Library/Application Support/media content.txt\"", delimiter is ' ', and
133// the start_mark and end_mark are '"', this method returns two fields:
134// "filename" and "/Library/Application Support/media content.txt".
Yves Gerey665174f2018-06-19 15:03:05 +0200135size_t tokenize(const std::string& source,
136 char delimiter,
137 char start_mark,
138 char end_mark,
139 std::vector<std::string>* fields);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200140
141// Extract the first token from source as separated by delimiter, with
142// duplicates of delimiter ignored. Return false if the delimiter could not be
143// found, otherwise return true.
144bool tokenize_first(const std::string& source,
145 const char delimiter,
146 std::string* token,
147 std::string* rest);
148
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200149// Convert arbitrary values to/from a string.
Jonas Olsson6b1985d2018-07-05 11:59:48 +0200150// TODO(jonasolsson): Remove these when absl::StrCat becomes available.
151std::string ToString(bool b);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200152
Jonas Olsson6b1985d2018-07-05 11:59:48 +0200153std::string ToString(const char* s);
154std::string ToString(std::string t);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200155
Jonas Olsson6b1985d2018-07-05 11:59:48 +0200156std::string ToString(short s);
157std::string ToString(unsigned short s);
158std::string ToString(int s);
159std::string ToString(unsigned int s);
160std::string ToString(long int s);
161std::string ToString(unsigned long int s);
162std::string ToString(long long int s);
163std::string ToString(unsigned long long int s);
164
165std::string ToString(double t);
Jonas Olsson88e18482018-09-03 10:15:08 +0200166std::string ToString(long double t);
Jonas Olsson6b1985d2018-07-05 11:59:48 +0200167
168std::string ToString(const void* p);
169
170template <typename T,
171 typename std::enable_if<std::is_arithmetic<T>::value &&
172 !std::is_same<T, bool>::value,
173 int>::type = 0>
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200174static bool FromString(const std::string& s, T* t) {
175 RTC_DCHECK(t);
Jonas Olsson6b1985d2018-07-05 11:59:48 +0200176 absl::optional<T> result = StringToNumber<T>(s);
177
178 if (result)
179 *t = *result;
180
181 return result.has_value();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200182}
183
Jonas Olsson6b1985d2018-07-05 11:59:48 +0200184bool FromString(const std::string& s, bool* b);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200185
Yves Gerey665174f2018-06-19 15:03:05 +0200186template <typename T>
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200187static inline T FromString(const std::string& str) {
Yves Gerey665174f2018-06-19 15:03:05 +0200188 T val;
189 FromString(str, &val);
190 return val;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200191}
192
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200193//////////////////////////////////////////////////////////////////////
194
195} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000196
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200197#endif // RTC_BASE_STRINGENCODE_H__