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