blob: 09bf77f4461525a3bafc1fa1faca69c296e2d2c2 [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:261/*
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 04:47:3111#ifndef RTC_BASE_STRINGENCODE_H_
12#define RTC_BASE_STRINGENCODE_H_
henrike@webrtc.orgf0488722014-05-13 18:00:2613
Yves Gerey988cc082018-10-23 10:03:0114#include <stddef.h>
Henrik Kjellanderec78f1c2017-06-29 05:52:5015#include <string>
Yves Gerey988cc082018-10-23 10:03:0116#include <type_traits>
Henrik Kjellanderec78f1c2017-06-29 05:52:5017#include <vector>
henrike@webrtc.orgf0488722014-05-13 18:00:2618
Yves Gerey988cc082018-10-23 10:03:0119#include "absl/types/optional.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3120#include "rtc_base/checks.h"
Jonas Olsson6b1985d2018-07-05 09:59:4821#include "rtc_base/string_to_number.h"
Henrik Kjellanderec78f1c2017-06-29 05:52:5022
23namespace rtc {
24
25//////////////////////////////////////////////////////////////////////
26// String Encoding Utilities
27//////////////////////////////////////////////////////////////////////
28
Henrik Kjellanderec78f1c2017-06-29 05:52:5029// Note: in-place decoding (buffer == source) is allowed.
Yves Gerey665174f2018-06-19 13:03:0530size_t url_decode(char* buffer,
31 size_t buflen,
32 const char* source,
33 size_t srclen);
Henrik Kjellanderec78f1c2017-06-29 05:52:5034
Henrik Kjellanderec78f1c2017-06-29 05:52:5035// Convert an unsigned value from 0 to 15 to the hex character equivalent...
36char hex_encode(unsigned char val);
37// ...and vice-versa.
38bool hex_decode(char ch, unsigned char* val);
39
40// hex_encode shows the hex representation of binary data in ascii.
Yves Gerey665174f2018-06-19 13:03:0541size_t hex_encode(char* buffer,
42 size_t buflen,
43 const char* source,
44 size_t srclen);
Henrik Kjellanderec78f1c2017-06-29 05:52:5045
46// hex_encode, but separate each byte representation with a delimiter.
47// |delimiter| == 0 means no delimiter
48// If the buffer is too short, we return 0
Yves Gerey665174f2018-06-19 13:03:0549size_t hex_encode_with_delimiter(char* buffer,
50 size_t buflen,
51 const char* source,
52 size_t srclen,
Henrik Kjellanderec78f1c2017-06-29 05:52:5053 char delimiter);
54
55// Helper functions for hex_encode.
56std::string hex_encode(const std::string& str);
57std::string hex_encode(const char* source, size_t srclen);
Yves Gerey665174f2018-06-19 13:03:0558std::string hex_encode_with_delimiter(const char* source,
59 size_t srclen,
Henrik Kjellanderec78f1c2017-06-29 05:52:5060 char delimiter);
61
62// hex_decode converts ascii hex to binary.
Yves Gerey665174f2018-06-19 13:03:0563size_t hex_decode(char* buffer,
64 size_t buflen,
65 const char* source,
66 size_t srclen);
Henrik Kjellanderec78f1c2017-06-29 05:52:5067
68// hex_decode, assuming that there is a delimiter between every byte
69// pair.
70// |delimiter| == 0 means no delimiter
71// If the buffer is too short or the data is invalid, we return 0.
Yves Gerey665174f2018-06-19 13:03:0572size_t hex_decode_with_delimiter(char* buffer,
73 size_t buflen,
74 const char* source,
75 size_t srclen,
Henrik Kjellanderec78f1c2017-06-29 05:52:5076 char delimiter);
77
78// Helper functions for hex_decode.
79size_t hex_decode(char* buffer, size_t buflen, const std::string& source);
Yves Gerey665174f2018-06-19 13:03:0580size_t hex_decode_with_delimiter(char* buffer,
81 size_t buflen,
82 const std::string& source,
83 char delimiter);
Henrik Kjellanderec78f1c2017-06-29 05:52:5084
85// Apply any suitable string transform (including the ones above) to an STL
86// string. Stack-allocated temporary space is used for the transformation,
87// so value and source may refer to the same string.
Yves Gerey665174f2018-06-19 13:03:0588typedef size_t (*Transform)(char* buffer,
89 size_t buflen,
90 const char* source,
91 size_t srclen);
92size_t transform(std::string& value,
93 size_t maxlen,
94 const std::string& source,
Henrik Kjellanderec78f1c2017-06-29 05:52:5095 Transform t);
96
97// Return the result of applying transform t to source.
98std::string s_transform(const std::string& source, Transform t);
99
100// Convenience wrappers.
Henrik Kjellanderec78f1c2017-06-29 05:52:50101inline std::string s_url_decode(const std::string& source) {
102 return s_transform(source, url_decode);
103}
104
Diogo Real7bd1f1b2017-09-08 19:50:41105// Joins the source vector of strings into a single string, with each
106// field in source being separated by delimiter. No trailing delimiter is added.
107std::string join(const std::vector<std::string>& source, char delimiter);
108
Henrik Kjellanderec78f1c2017-06-29 05:52:50109// Splits the source string into multiple fields separated by delimiter,
110// with duplicates of delimiter creating empty fields.
Yves Gerey665174f2018-06-19 13:03:05111size_t split(const std::string& source,
112 char delimiter,
Henrik Kjellanderec78f1c2017-06-29 05:52:50113 std::vector<std::string>* fields);
114
115// Splits the source string into multiple fields separated by delimiter,
116// with duplicates of delimiter ignored. Trailing delimiter ignored.
Yves Gerey665174f2018-06-19 13:03:05117size_t tokenize(const std::string& source,
118 char delimiter,
Henrik Kjellanderec78f1c2017-06-29 05:52:50119 std::vector<std::string>* fields);
120
121// Tokenize, including the empty tokens.
122size_t tokenize_with_empty_tokens(const std::string& source,
123 char delimiter,
124 std::vector<std::string>* fields);
125
126// Tokenize and append the tokens to fields. Return the new size of fields.
Yves Gerey665174f2018-06-19 13:03:05127size_t tokenize_append(const std::string& source,
128 char delimiter,
Henrik Kjellanderec78f1c2017-06-29 05:52:50129 std::vector<std::string>* fields);
130
131// Splits the source string into multiple fields separated by delimiter, with
132// duplicates of delimiter ignored. Trailing delimiter ignored. A substring in
133// between the start_mark and the end_mark is treated as a single field. Return
134// the size of fields. For example, if source is "filename
135// \"/Library/Application Support/media content.txt\"", delimiter is ' ', and
136// the start_mark and end_mark are '"', this method returns two fields:
137// "filename" and "/Library/Application Support/media content.txt".
Yves Gerey665174f2018-06-19 13:03:05138size_t tokenize(const std::string& source,
139 char delimiter,
140 char start_mark,
141 char end_mark,
142 std::vector<std::string>* fields);
Henrik Kjellanderec78f1c2017-06-29 05:52:50143
144// Extract the first token from source as separated by delimiter, with
145// duplicates of delimiter ignored. Return false if the delimiter could not be
146// found, otherwise return true.
147bool tokenize_first(const std::string& source,
148 const char delimiter,
149 std::string* token,
150 std::string* rest);
151
Henrik Kjellanderec78f1c2017-06-29 05:52:50152// Convert arbitrary values to/from a string.
Jonas Olsson6b1985d2018-07-05 09:59:48153// TODO(jonasolsson): Remove these when absl::StrCat becomes available.
154std::string ToString(bool b);
Henrik Kjellanderec78f1c2017-06-29 05:52:50155
Jonas Olsson6b1985d2018-07-05 09:59:48156std::string ToString(const char* s);
157std::string ToString(std::string t);
Henrik Kjellanderec78f1c2017-06-29 05:52:50158
Jonas Olsson6b1985d2018-07-05 09:59:48159std::string ToString(short s);
160std::string ToString(unsigned short s);
161std::string ToString(int s);
162std::string ToString(unsigned int s);
163std::string ToString(long int s);
164std::string ToString(unsigned long int s);
165std::string ToString(long long int s);
166std::string ToString(unsigned long long int s);
167
168std::string ToString(double t);
Jonas Olsson88e18482018-09-03 08:15:08169std::string ToString(long double t);
Jonas Olsson6b1985d2018-07-05 09:59:48170
171std::string ToString(const void* p);
172
173template <typename T,
174 typename std::enable_if<std::is_arithmetic<T>::value &&
175 !std::is_same<T, bool>::value,
176 int>::type = 0>
Henrik Kjellanderec78f1c2017-06-29 05:52:50177static bool FromString(const std::string& s, T* t) {
178 RTC_DCHECK(t);
Jonas Olsson6b1985d2018-07-05 09:59:48179 absl::optional<T> result = StringToNumber<T>(s);
180
181 if (result)
182 *t = *result;
183
184 return result.has_value();
Henrik Kjellanderec78f1c2017-06-29 05:52:50185}
186
Jonas Olsson6b1985d2018-07-05 09:59:48187bool FromString(const std::string& s, bool* b);
Henrik Kjellanderec78f1c2017-06-29 05:52:50188
Yves Gerey665174f2018-06-19 13:03:05189template <typename T>
Henrik Kjellanderec78f1c2017-06-29 05:52:50190static inline T FromString(const std::string& str) {
Yves Gerey665174f2018-06-19 13:03:05191 T val;
192 FromString(str, &val);
193 return val;
Henrik Kjellanderec78f1c2017-06-29 05:52:50194}
195
Henrik Kjellanderec78f1c2017-06-29 05:52:50196//////////////////////////////////////////////////////////////////////
197
198} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26199
Mirko Bonadei92ea95e2017-09-15 04:47:31200#endif // RTC_BASE_STRINGENCODE_H__