blob: e5395b7edf2e8f6399d48f19da99e9d38f4329c9 [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.
27size_t url_decode(char * buffer, size_t buflen,
28 const char * source, size_t srclen);
29
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020030// Convert an unsigned value from 0 to 15 to the hex character equivalent...
31char hex_encode(unsigned char val);
32// ...and vice-versa.
33bool hex_decode(char ch, unsigned char* val);
34
35// hex_encode shows the hex representation of binary data in ascii.
36size_t hex_encode(char* buffer, size_t buflen,
37 const char* source, size_t srclen);
38
39// hex_encode, but separate each byte representation with a delimiter.
40// |delimiter| == 0 means no delimiter
41// If the buffer is too short, we return 0
42size_t hex_encode_with_delimiter(char* buffer, size_t buflen,
43 const char* source, size_t srclen,
44 char delimiter);
45
46// Helper functions for hex_encode.
47std::string hex_encode(const std::string& str);
48std::string hex_encode(const char* source, size_t srclen);
49std::string hex_encode_with_delimiter(const char* source, size_t srclen,
50 char delimiter);
51
52// hex_decode converts ascii hex to binary.
53size_t hex_decode(char* buffer, size_t buflen,
54 const char* source, size_t srclen);
55
56// hex_decode, assuming that there is a delimiter between every byte
57// pair.
58// |delimiter| == 0 means no delimiter
59// If the buffer is too short or the data is invalid, we return 0.
60size_t hex_decode_with_delimiter(char* buffer, size_t buflen,
61 const char* source, size_t srclen,
62 char delimiter);
63
64// Helper functions for hex_decode.
65size_t hex_decode(char* buffer, size_t buflen, const std::string& source);
66size_t hex_decode_with_delimiter(char* buffer, size_t buflen,
67 const std::string& source, char delimiter);
68
69// Apply any suitable string transform (including the ones above) to an STL
70// string. Stack-allocated temporary space is used for the transformation,
71// so value and source may refer to the same string.
72typedef size_t (*Transform)(char * buffer, size_t buflen,
73 const char * source, size_t srclen);
74size_t transform(std::string& value, size_t maxlen, const std::string& source,
75 Transform t);
76
77// Return the result of applying transform t to source.
78std::string s_transform(const std::string& source, Transform t);
79
80// Convenience wrappers.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020081inline std::string s_url_decode(const std::string& source) {
82 return s_transform(source, url_decode);
83}
84
Diogo Real7bd1f1b2017-09-08 12:50:41 -070085// Joins the source vector of strings into a single string, with each
86// field in source being separated by delimiter. No trailing delimiter is added.
87std::string join(const std::vector<std::string>& source, char delimiter);
88
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020089// Splits the source string into multiple fields separated by delimiter,
90// with duplicates of delimiter creating empty fields.
91size_t split(const std::string& source, char delimiter,
92 std::vector<std::string>* fields);
93
94// Splits the source string into multiple fields separated by delimiter,
95// with duplicates of delimiter ignored. Trailing delimiter ignored.
96size_t tokenize(const std::string& source, char delimiter,
97 std::vector<std::string>* fields);
98
99// Tokenize, including the empty tokens.
100size_t tokenize_with_empty_tokens(const std::string& source,
101 char delimiter,
102 std::vector<std::string>* fields);
103
104// Tokenize and append the tokens to fields. Return the new size of fields.
105size_t tokenize_append(const std::string& source, char delimiter,
106 std::vector<std::string>* fields);
107
108// Splits the source string into multiple fields separated by delimiter, with
109// duplicates of delimiter ignored. Trailing delimiter ignored. A substring in
110// between the start_mark and the end_mark is treated as a single field. Return
111// the size of fields. For example, if source is "filename
112// \"/Library/Application Support/media content.txt\"", delimiter is ' ', and
113// the start_mark and end_mark are '"', this method returns two fields:
114// "filename" and "/Library/Application Support/media content.txt".
115size_t tokenize(const std::string& source, char delimiter, char start_mark,
116 char end_mark, std::vector<std::string>* fields);
117
118// Extract the first token from source as separated by delimiter, with
119// duplicates of delimiter ignored. Return false if the delimiter could not be
120// found, otherwise return true.
121bool tokenize_first(const std::string& source,
122 const char delimiter,
123 std::string* token,
124 std::string* rest);
125
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200126// Convert arbitrary values to/from a string.
127
128template <class T>
129static bool ToString(const T &t, std::string* s) {
130 RTC_DCHECK(s);
131 std::ostringstream oss;
132 oss << std::boolalpha << t;
133 *s = oss.str();
134 return !oss.fail();
135}
136
137template <class T>
138static bool FromString(const std::string& s, T* t) {
139 RTC_DCHECK(t);
140 std::istringstream iss(s);
141 iss >> std::boolalpha >> *t;
142 return !iss.fail();
143}
144
145// Inline versions of the string conversion routines.
146
147template<typename T>
148static inline std::string ToString(const T& val) {
149 std::string str; ToString(val, &str); return str;
150}
151
152template<typename T>
153static inline T FromString(const std::string& str) {
154 T val; FromString(str, &val); return val;
155}
156
157template<typename T>
158static inline T FromString(const T& defaultValue, const std::string& str) {
159 T val(defaultValue); FromString(str, &val); return val;
160}
161
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200162//////////////////////////////////////////////////////////////////////
163
164} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000165
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200166#endif // RTC_BASE_STRINGENCODE_H__