blob: 7042c4a4169d0c756fc767676299efb6916ca672 [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
Henrik Kjellanderec78f1c2017-06-29 05:52:5014#include <sstream>
15#include <string>
16#include <vector>
henrike@webrtc.orgf0488722014-05-13 18:00:2617
Mirko Bonadei92ea95e2017-09-15 04:47:3118#include "rtc_base/checks.h"
Henrik Kjellanderec78f1c2017-06-29 05:52:5019
20namespace rtc {
21
22//////////////////////////////////////////////////////////////////////
23// String Encoding Utilities
24//////////////////////////////////////////////////////////////////////
25
Henrik Kjellanderec78f1c2017-06-29 05:52:5026// Note: in-place decoding (buffer == source) is allowed.
Yves Gerey665174f2018-06-19 13:03:0527size_t url_decode(char* buffer,
28 size_t buflen,
29 const char* source,
30 size_t srclen);
Henrik Kjellanderec78f1c2017-06-29 05:52:5031
Henrik Kjellanderec78f1c2017-06-29 05:52:5032// 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 13:03:0538size_t hex_encode(char* buffer,
39 size_t buflen,
40 const char* source,
41 size_t srclen);
Henrik Kjellanderec78f1c2017-06-29 05:52:5042
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 13:03:0546size_t hex_encode_with_delimiter(char* buffer,
47 size_t buflen,
48 const char* source,
49 size_t srclen,
Henrik Kjellanderec78f1c2017-06-29 05:52:5050 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 13:03:0555std::string hex_encode_with_delimiter(const char* source,
56 size_t srclen,
Henrik Kjellanderec78f1c2017-06-29 05:52:5057 char delimiter);
58
59// hex_decode converts ascii hex to binary.
Yves Gerey665174f2018-06-19 13:03:0560size_t hex_decode(char* buffer,
61 size_t buflen,
62 const char* source,
63 size_t srclen);
Henrik Kjellanderec78f1c2017-06-29 05:52:5064
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 13:03:0569size_t hex_decode_with_delimiter(char* buffer,
70 size_t buflen,
71 const char* source,
72 size_t srclen,
Henrik Kjellanderec78f1c2017-06-29 05:52:5073 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 13:03:0577size_t hex_decode_with_delimiter(char* buffer,
78 size_t buflen,
79 const std::string& source,
80 char delimiter);
Henrik Kjellanderec78f1c2017-06-29 05:52:5081
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 13:03:0585typedef 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 05:52:5092 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 05:52:5098inline std::string s_url_decode(const std::string& source) {
99 return s_transform(source, url_decode);
100}
101
Diogo Real7bd1f1b2017-09-08 19:50:41102// 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 05:52:50106// Splits the source string into multiple fields separated by delimiter,
107// with duplicates of delimiter creating empty fields.
Yves Gerey665174f2018-06-19 13:03:05108size_t split(const std::string& source,
109 char delimiter,
Henrik Kjellanderec78f1c2017-06-29 05:52:50110 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 13:03:05114size_t tokenize(const std::string& source,
115 char delimiter,
Henrik Kjellanderec78f1c2017-06-29 05:52:50116 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 13:03:05124size_t tokenize_append(const std::string& source,
125 char delimiter,
Henrik Kjellanderec78f1c2017-06-29 05:52:50126 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 13:03:05135size_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 05:52:50140
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 05:52:50149// Convert arbitrary values to/from a string.
150
151template <class T>
Yves Gerey665174f2018-06-19 13:03:05152static bool ToString(const T& t, std::string* s) {
Henrik Kjellanderec78f1c2017-06-29 05:52:50153 RTC_DCHECK(s);
154 std::ostringstream oss;
155 oss << std::boolalpha << t;
156 *s = oss.str();
157 return !oss.fail();
158}
159
160template <class T>
161static bool FromString(const std::string& s, T* t) {
162 RTC_DCHECK(t);
163 std::istringstream iss(s);
164 iss >> std::boolalpha >> *t;
165 return !iss.fail();
166}
167
168// Inline versions of the string conversion routines.
169
Yves Gerey665174f2018-06-19 13:03:05170template <typename T>
Henrik Kjellanderec78f1c2017-06-29 05:52:50171static inline std::string ToString(const T& val) {
Yves Gerey665174f2018-06-19 13:03:05172 std::string str;
173 ToString(val, &str);
174 return str;
Henrik Kjellanderec78f1c2017-06-29 05:52:50175}
176
Yves Gerey665174f2018-06-19 13:03:05177template <typename T>
Henrik Kjellanderec78f1c2017-06-29 05:52:50178static inline T FromString(const std::string& str) {
Yves Gerey665174f2018-06-19 13:03:05179 T val;
180 FromString(str, &val);
181 return val;
Henrik Kjellanderec78f1c2017-06-29 05:52:50182}
183
Yves Gerey665174f2018-06-19 13:03:05184template <typename T>
Henrik Kjellanderec78f1c2017-06-29 05:52:50185static inline T FromString(const T& defaultValue, const std::string& str) {
Yves Gerey665174f2018-06-19 13:03:05186 T val(defaultValue);
187 FromString(str, &val);
188 return val;
Henrik Kjellanderec78f1c2017-06-29 05:52:50189}
190
Henrik Kjellanderec78f1c2017-06-29 05:52:50191//////////////////////////////////////////////////////////////////////
192
193} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26194
Mirko Bonadei92ea95e2017-09-15 04:47:31195#endif // RTC_BASE_STRINGENCODE_H__