blob: 830ea77c7b985e9c99e0a7cdddd131690928d278 [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 <sstream>
15#include <string>
16#include <vector>
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000017
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "rtc_base/checks.h"
Jonas Olsson6b1985d2018-07-05 11:59:48 +020019#include "rtc_base/string_to_number.h"
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020020
21namespace rtc {
22
23//////////////////////////////////////////////////////////////////////
24// String Encoding Utilities
25//////////////////////////////////////////////////////////////////////
26
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020027// Note: in-place decoding (buffer == source) is allowed.
Yves Gerey665174f2018-06-19 15:03:05 +020028size_t url_decode(char* buffer,
29 size_t buflen,
30 const char* source,
31 size_t srclen);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020032
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020033// Convert an unsigned value from 0 to 15 to the hex character equivalent...
34char hex_encode(unsigned char val);
35// ...and vice-versa.
36bool hex_decode(char ch, unsigned char* val);
37
38// hex_encode shows the hex representation of binary data in ascii.
Yves Gerey665174f2018-06-19 15:03:05 +020039size_t hex_encode(char* buffer,
40 size_t buflen,
41 const char* source,
42 size_t srclen);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020043
44// hex_encode, but separate each byte representation with a delimiter.
45// |delimiter| == 0 means no delimiter
46// If the buffer is too short, we return 0
Yves Gerey665174f2018-06-19 15:03:05 +020047size_t hex_encode_with_delimiter(char* buffer,
48 size_t buflen,
49 const char* source,
50 size_t srclen,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020051 char delimiter);
52
53// Helper functions for hex_encode.
54std::string hex_encode(const std::string& str);
55std::string hex_encode(const char* source, size_t srclen);
Yves Gerey665174f2018-06-19 15:03:05 +020056std::string hex_encode_with_delimiter(const char* source,
57 size_t srclen,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020058 char delimiter);
59
60// hex_decode converts ascii hex to binary.
Yves Gerey665174f2018-06-19 15:03:05 +020061size_t hex_decode(char* buffer,
62 size_t buflen,
63 const char* source,
64 size_t srclen);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020065
66// hex_decode, assuming that there is a delimiter between every byte
67// pair.
68// |delimiter| == 0 means no delimiter
69// If the buffer is too short or the data is invalid, we return 0.
Yves Gerey665174f2018-06-19 15:03:05 +020070size_t hex_decode_with_delimiter(char* buffer,
71 size_t buflen,
72 const char* source,
73 size_t srclen,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020074 char delimiter);
75
76// Helper functions for hex_decode.
77size_t hex_decode(char* buffer, size_t buflen, const std::string& source);
Yves Gerey665174f2018-06-19 15:03:05 +020078size_t hex_decode_with_delimiter(char* buffer,
79 size_t buflen,
80 const std::string& source,
81 char delimiter);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020082
83// Apply any suitable string transform (including the ones above) to an STL
84// string. Stack-allocated temporary space is used for the transformation,
85// so value and source may refer to the same string.
Yves Gerey665174f2018-06-19 15:03:05 +020086typedef size_t (*Transform)(char* buffer,
87 size_t buflen,
88 const char* source,
89 size_t srclen);
90size_t transform(std::string& value,
91 size_t maxlen,
92 const std::string& source,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020093 Transform t);
94
95// Return the result of applying transform t to source.
96std::string s_transform(const std::string& source, Transform t);
97
98// Convenience wrappers.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020099inline std::string s_url_decode(const std::string& source) {
100 return s_transform(source, url_decode);
101}
102
Diogo Real7bd1f1b2017-09-08 12:50:41 -0700103// Joins the source vector of strings into a single string, with each
104// field in source being separated by delimiter. No trailing delimiter is added.
105std::string join(const std::vector<std::string>& source, char delimiter);
106
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200107// Splits the source string into multiple fields separated by delimiter,
108// with duplicates of delimiter creating empty fields.
Yves Gerey665174f2018-06-19 15:03:05 +0200109size_t split(const std::string& source,
110 char delimiter,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200111 std::vector<std::string>* fields);
112
113// Splits the source string into multiple fields separated by delimiter,
114// with duplicates of delimiter ignored. Trailing delimiter ignored.
Yves Gerey665174f2018-06-19 15:03:05 +0200115size_t tokenize(const std::string& source,
116 char delimiter,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200117 std::vector<std::string>* fields);
118
119// Tokenize, including the empty tokens.
120size_t tokenize_with_empty_tokens(const std::string& source,
121 char delimiter,
122 std::vector<std::string>* fields);
123
124// Tokenize and append the tokens to fields. Return the new size of fields.
Yves Gerey665174f2018-06-19 15:03:05 +0200125size_t tokenize_append(const std::string& source,
126 char delimiter,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200127 std::vector<std::string>* fields);
128
129// Splits the source string into multiple fields separated by delimiter, with
130// duplicates of delimiter ignored. Trailing delimiter ignored. A substring in
131// between the start_mark and the end_mark is treated as a single field. Return
132// the size of fields. For example, if source is "filename
133// \"/Library/Application Support/media content.txt\"", delimiter is ' ', and
134// the start_mark and end_mark are '"', this method returns two fields:
135// "filename" and "/Library/Application Support/media content.txt".
Yves Gerey665174f2018-06-19 15:03:05 +0200136size_t tokenize(const std::string& source,
137 char delimiter,
138 char start_mark,
139 char end_mark,
140 std::vector<std::string>* fields);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200141
142// Extract the first token from source as separated by delimiter, with
143// duplicates of delimiter ignored. Return false if the delimiter could not be
144// found, otherwise return true.
145bool tokenize_first(const std::string& source,
146 const char delimiter,
147 std::string* token,
148 std::string* rest);
149
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200150// Convert arbitrary values to/from a string.
Jonas Olsson6b1985d2018-07-05 11:59:48 +0200151// TODO(jonasolsson): Remove these when absl::StrCat becomes available.
152std::string ToString(bool b);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200153
Jonas Olsson6b1985d2018-07-05 11:59:48 +0200154std::string ToString(const char* s);
155std::string ToString(std::string t);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200156
Jonas Olsson6b1985d2018-07-05 11:59:48 +0200157std::string ToString(short s);
158std::string ToString(unsigned short s);
159std::string ToString(int s);
160std::string ToString(unsigned int s);
161std::string ToString(long int s);
162std::string ToString(unsigned long int s);
163std::string ToString(long long int s);
164std::string ToString(unsigned long long int s);
165
166std::string ToString(double t);
167
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__