blob: 7042c4a4169d0c756fc767676299efb6916ca672 [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"
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.
150
151template <class T>
Yves Gerey665174f2018-06-19 15:03:05 +0200152static bool ToString(const T& t, std::string* s) {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200153 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 15:03:05 +0200170template <typename T>
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200171static inline std::string ToString(const T& val) {
Yves Gerey665174f2018-06-19 15:03:05 +0200172 std::string str;
173 ToString(val, &str);
174 return str;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200175}
176
Yves Gerey665174f2018-06-19 15:03:05 +0200177template <typename T>
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200178static inline T FromString(const std::string& str) {
Yves Gerey665174f2018-06-19 15:03:05 +0200179 T val;
180 FromString(str, &val);
181 return val;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200182}
183
Yves Gerey665174f2018-06-19 15:03:05 +0200184template <typename T>
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200185static inline T FromString(const T& defaultValue, const std::string& str) {
Yves Gerey665174f2018-06-19 15:03:05 +0200186 T val(defaultValue);
187 FromString(str, &val);
188 return val;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200189}
190
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200191//////////////////////////////////////////////////////////////////////
192
193} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000194
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200195#endif // RTC_BASE_STRINGENCODE_H__