blob: c1401b9f52f83f456fc81454494af696bf35b20d [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>
Jonas Olssona4d87372019-07-05 19:08:33 +020015
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020016#include <string>
Yves Gerey988cc082018-10-23 12:03:01 +020017#include <type_traits>
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020018#include <vector>
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000019
Yves Gerey988cc082018-10-23 12:03:01 +020020#include "absl/types/optional.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "rtc_base/checks.h"
Jonas Olsson6b1985d2018-07-05 11:59:48 +020022#include "rtc_base/string_to_number.h"
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020023
24namespace rtc {
25
26//////////////////////////////////////////////////////////////////////
27// String Encoding Utilities
28//////////////////////////////////////////////////////////////////////
29
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020030std::string hex_encode(const std::string& str);
31std::string hex_encode(const char* source, size_t srclen);
Yves Gerey665174f2018-06-19 15:03:05 +020032std::string hex_encode_with_delimiter(const char* source,
33 size_t srclen,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020034 char delimiter);
35
36// hex_decode converts ascii hex to binary.
Yves Gerey665174f2018-06-19 15:03:05 +020037size_t hex_decode(char* buffer,
38 size_t buflen,
39 const char* source,
40 size_t srclen);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020041
42// hex_decode, assuming that there is a delimiter between every byte
43// pair.
44// |delimiter| == 0 means no delimiter
45// If the buffer is too short or the data is invalid, we return 0.
Yves Gerey665174f2018-06-19 15:03:05 +020046size_t hex_decode_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_decode.
53size_t hex_decode(char* buffer, size_t buflen, const std::string& source);
Yves Gerey665174f2018-06-19 15:03:05 +020054size_t hex_decode_with_delimiter(char* buffer,
55 size_t buflen,
56 const std::string& source,
57 char delimiter);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020058
Diogo Real7bd1f1b2017-09-08 12:50:41 -070059// Joins the source vector of strings into a single string, with each
60// field in source being separated by delimiter. No trailing delimiter is added.
61std::string join(const std::vector<std::string>& source, char delimiter);
62
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020063// Splits the source string into multiple fields separated by delimiter,
64// with duplicates of delimiter creating empty fields.
Yves Gerey665174f2018-06-19 15:03:05 +020065size_t split(const std::string& source,
66 char delimiter,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020067 std::vector<std::string>* fields);
68
69// Splits the source string into multiple fields separated by delimiter,
70// with duplicates of delimiter ignored. Trailing delimiter ignored.
Yves Gerey665174f2018-06-19 15:03:05 +020071size_t tokenize(const std::string& source,
72 char delimiter,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020073 std::vector<std::string>* fields);
74
75// Tokenize, including the empty tokens.
76size_t tokenize_with_empty_tokens(const std::string& source,
77 char delimiter,
78 std::vector<std::string>* fields);
79
80// Tokenize and append the tokens to fields. Return the new size of fields.
Yves Gerey665174f2018-06-19 15:03:05 +020081size_t tokenize_append(const std::string& source,
82 char delimiter,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020083 std::vector<std::string>* fields);
84
85// Splits the source string into multiple fields separated by delimiter, with
86// duplicates of delimiter ignored. Trailing delimiter ignored. A substring in
87// between the start_mark and the end_mark is treated as a single field. Return
88// the size of fields. For example, if source is "filename
89// \"/Library/Application Support/media content.txt\"", delimiter is ' ', and
90// the start_mark and end_mark are '"', this method returns two fields:
91// "filename" and "/Library/Application Support/media content.txt".
Yves Gerey665174f2018-06-19 15:03:05 +020092size_t tokenize(const std::string& source,
93 char delimiter,
94 char start_mark,
95 char end_mark,
96 std::vector<std::string>* fields);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020097
98// Extract the first token from source as separated by delimiter, with
99// duplicates of delimiter ignored. Return false if the delimiter could not be
100// found, otherwise return true.
101bool tokenize_first(const std::string& source,
102 const char delimiter,
103 std::string* token,
104 std::string* rest);
105
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200106// Convert arbitrary values to/from a string.
Jonas Olsson6b1985d2018-07-05 11:59:48 +0200107// TODO(jonasolsson): Remove these when absl::StrCat becomes available.
108std::string ToString(bool b);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200109
Jonas Olsson6b1985d2018-07-05 11:59:48 +0200110std::string ToString(const char* s);
111std::string ToString(std::string t);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200112
Jonas Olsson6b1985d2018-07-05 11:59:48 +0200113std::string ToString(short s);
114std::string ToString(unsigned short s);
115std::string ToString(int s);
116std::string ToString(unsigned int s);
117std::string ToString(long int s);
118std::string ToString(unsigned long int s);
119std::string ToString(long long int s);
120std::string ToString(unsigned long long int s);
121
122std::string ToString(double t);
Jonas Olsson88e18482018-09-03 10:15:08 +0200123std::string ToString(long double t);
Jonas Olsson6b1985d2018-07-05 11:59:48 +0200124
125std::string ToString(const void* p);
126
127template <typename T,
128 typename std::enable_if<std::is_arithmetic<T>::value &&
129 !std::is_same<T, bool>::value,
130 int>::type = 0>
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200131static bool FromString(const std::string& s, T* t) {
132 RTC_DCHECK(t);
Jonas Olsson6b1985d2018-07-05 11:59:48 +0200133 absl::optional<T> result = StringToNumber<T>(s);
134
135 if (result)
136 *t = *result;
137
138 return result.has_value();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200139}
140
Jonas Olsson6b1985d2018-07-05 11:59:48 +0200141bool FromString(const std::string& s, bool* b);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200142
Yves Gerey665174f2018-06-19 15:03:05 +0200143template <typename T>
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200144static inline T FromString(const std::string& str) {
Yves Gerey665174f2018-06-19 15:03:05 +0200145 T val;
146 FromString(str, &val);
147 return val;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200148}
149
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200150//////////////////////////////////////////////////////////////////////
151
152} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000153
Steve Anton10542f22019-01-11 09:11:00 -0800154#endif // RTC_BASE_STRING_ENCODE_H__