blob: d3338d952620666b0dba543ad4b08cf555e601a1 [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
Steve Anton10542f22019-01-11 09:11:00 -080011#ifndef RTC_BASE_STRING_ENCODE_H_
12#define RTC_BASE_STRING_ENCODE_H_
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000013
Yves Gerey988cc082018-10-23 12:03:01 +020014#include <stddef.h>
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020015#include <string>
Yves Gerey988cc082018-10-23 12:03:01 +020016#include <type_traits>
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020017#include <vector>
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000018
Yves Gerey988cc082018-10-23 12:03:01 +020019#include "absl/types/optional.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "rtc_base/checks.h"
Jonas Olsson6b1985d2018-07-05 11:59:48 +020021#include "rtc_base/string_to_number.h"
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020022
23namespace rtc {
24
25//////////////////////////////////////////////////////////////////////
26// String Encoding Utilities
27//////////////////////////////////////////////////////////////////////
28
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020029// Note: in-place decoding (buffer == source) is allowed.
Yves Gerey665174f2018-06-19 15:03:05 +020030size_t url_decode(char* buffer,
31 size_t buflen,
32 const char* source,
33 size_t srclen);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020034
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020035// 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 15:03:05 +020041size_t hex_encode(char* buffer,
42 size_t buflen,
43 const char* source,
44 size_t srclen);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020045
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 15:03:05 +020049size_t hex_encode_with_delimiter(char* buffer,
50 size_t buflen,
51 const char* source,
52 size_t srclen,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020053 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 15:03:05 +020058std::string hex_encode_with_delimiter(const char* source,
59 size_t srclen,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020060 char delimiter);
61
62// hex_decode converts ascii hex to binary.
Yves Gerey665174f2018-06-19 15:03:05 +020063size_t hex_decode(char* buffer,
64 size_t buflen,
65 const char* source,
66 size_t srclen);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020067
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 15:03:05 +020072size_t hex_decode_with_delimiter(char* buffer,
73 size_t buflen,
74 const char* source,
75 size_t srclen,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020076 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 15:03:05 +020080size_t hex_decode_with_delimiter(char* buffer,
81 size_t buflen,
82 const std::string& source,
83 char delimiter);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020084
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 15:03:05 +020088typedef 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 07:52:50 +020095 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 07:52:50 +0200101inline std::string s_url_decode(const std::string& source) {
102 return s_transform(source, url_decode);
103}
104
Diogo Real7bd1f1b2017-09-08 12:50:41 -0700105// 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 07:52:50 +0200109// Splits the source string into multiple fields separated by delimiter,
110// with duplicates of delimiter creating empty fields.
Yves Gerey665174f2018-06-19 15:03:05 +0200111size_t split(const std::string& source,
112 char delimiter,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200113 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 15:03:05 +0200117size_t tokenize(const std::string& source,
118 char delimiter,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200119 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 15:03:05 +0200127size_t tokenize_append(const std::string& source,
128 char delimiter,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200129 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 15:03:05 +0200138size_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 07:52:50 +0200143
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 07:52:50 +0200152// Convert arbitrary values to/from a string.
Jonas Olsson6b1985d2018-07-05 11:59:48 +0200153// TODO(jonasolsson): Remove these when absl::StrCat becomes available.
154std::string ToString(bool b);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200155
Jonas Olsson6b1985d2018-07-05 11:59:48 +0200156std::string ToString(const char* s);
157std::string ToString(std::string t);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200158
Jonas Olsson6b1985d2018-07-05 11:59:48 +0200159std::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 10:15:08 +0200169std::string ToString(long double t);
Jonas Olsson6b1985d2018-07-05 11:59:48 +0200170
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 07:52:50 +0200177static bool FromString(const std::string& s, T* t) {
178 RTC_DCHECK(t);
Jonas Olsson6b1985d2018-07-05 11:59:48 +0200179 absl::optional<T> result = StringToNumber<T>(s);
180
181 if (result)
182 *t = *result;
183
184 return result.has_value();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200185}
186
Jonas Olsson6b1985d2018-07-05 11:59:48 +0200187bool FromString(const std::string& s, bool* b);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200188
Yves Gerey665174f2018-06-19 15:03:05 +0200189template <typename T>
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200190static inline T FromString(const std::string& str) {
Yves Gerey665174f2018-06-19 15:03:05 +0200191 T val;
192 FromString(str, &val);
193 return val;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200194}
195
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200196//////////////////////////////////////////////////////////////////////
197
198} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000199
Steve Anton10542f22019-01-11 09:11:00 -0800200#endif // RTC_BASE_STRING_ENCODE_H__