blob: d0f323deed771bbb1a8cdab69d16f0d869e997f4 [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>
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020015#include <string>
Yves Gerey988cc082018-10-23 12:03:01 +020016#include <type_traits>
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020017#include <vector>
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000018
Yves Gerey988cc082018-10-23 12:03:01 +020019#include "absl/types/optional.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "rtc_base/checks.h"
Jonas Olsson6b1985d2018-07-05 11:59:48 +020021#include "rtc_base/string_to_number.h"
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020022
23namespace rtc {
24
25//////////////////////////////////////////////////////////////////////
26// String Encoding Utilities
27//////////////////////////////////////////////////////////////////////
28
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020029// Convert an unsigned value from 0 to 15 to the hex character equivalent...
30char hex_encode(unsigned char val);
31// ...and vice-versa.
32bool hex_decode(char ch, unsigned char* val);
33
34// hex_encode shows the hex representation of binary data in ascii.
Yves Gerey665174f2018-06-19 15:03:05 +020035size_t hex_encode(char* buffer,
36 size_t buflen,
37 const char* source,
38 size_t srclen);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020039
40// hex_encode, but separate each byte representation with a delimiter.
41// |delimiter| == 0 means no delimiter
42// If the buffer is too short, we return 0
Yves Gerey665174f2018-06-19 15:03:05 +020043size_t hex_encode_with_delimiter(char* buffer,
44 size_t buflen,
45 const char* source,
46 size_t srclen,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020047 char delimiter);
48
49// Helper functions for hex_encode.
50std::string hex_encode(const std::string& str);
51std::string hex_encode(const char* source, size_t srclen);
Yves Gerey665174f2018-06-19 15:03:05 +020052std::string hex_encode_with_delimiter(const char* source,
53 size_t srclen,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020054 char delimiter);
55
56// hex_decode converts ascii hex to binary.
Yves Gerey665174f2018-06-19 15:03:05 +020057size_t hex_decode(char* buffer,
58 size_t buflen,
59 const char* source,
60 size_t srclen);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020061
62// hex_decode, assuming that there is a delimiter between every byte
63// pair.
64// |delimiter| == 0 means no delimiter
65// If the buffer is too short or the data is invalid, we return 0.
Yves Gerey665174f2018-06-19 15:03:05 +020066size_t hex_decode_with_delimiter(char* buffer,
67 size_t buflen,
68 const char* source,
69 size_t srclen,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020070 char delimiter);
71
72// Helper functions for hex_decode.
73size_t hex_decode(char* buffer, size_t buflen, const std::string& source);
Yves Gerey665174f2018-06-19 15:03:05 +020074size_t hex_decode_with_delimiter(char* buffer,
75 size_t buflen,
76 const std::string& source,
77 char delimiter);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020078
Diogo Real7bd1f1b2017-09-08 12:50:41 -070079// Joins the source vector of strings into a single string, with each
80// field in source being separated by delimiter. No trailing delimiter is added.
81std::string join(const std::vector<std::string>& source, char delimiter);
82
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020083// Splits the source string into multiple fields separated by delimiter,
84// with duplicates of delimiter creating empty fields.
Yves Gerey665174f2018-06-19 15:03:05 +020085size_t split(const std::string& source,
86 char delimiter,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020087 std::vector<std::string>* fields);
88
89// Splits the source string into multiple fields separated by delimiter,
90// with duplicates of delimiter ignored. Trailing delimiter ignored.
Yves Gerey665174f2018-06-19 15:03:05 +020091size_t tokenize(const std::string& source,
92 char delimiter,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020093 std::vector<std::string>* fields);
94
95// Tokenize, including the empty tokens.
96size_t tokenize_with_empty_tokens(const std::string& source,
97 char delimiter,
98 std::vector<std::string>* fields);
99
100// Tokenize and append the tokens to fields. Return the new size of fields.
Yves Gerey665174f2018-06-19 15:03:05 +0200101size_t tokenize_append(const std::string& source,
102 char delimiter,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200103 std::vector<std::string>* fields);
104
105// Splits the source string into multiple fields separated by delimiter, with
106// duplicates of delimiter ignored. Trailing delimiter ignored. A substring in
107// between the start_mark and the end_mark is treated as a single field. Return
108// the size of fields. For example, if source is "filename
109// \"/Library/Application Support/media content.txt\"", delimiter is ' ', and
110// the start_mark and end_mark are '"', this method returns two fields:
111// "filename" and "/Library/Application Support/media content.txt".
Yves Gerey665174f2018-06-19 15:03:05 +0200112size_t tokenize(const std::string& source,
113 char delimiter,
114 char start_mark,
115 char end_mark,
116 std::vector<std::string>* fields);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200117
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.
Jonas Olsson6b1985d2018-07-05 11:59:48 +0200127// TODO(jonasolsson): Remove these when absl::StrCat becomes available.
128std::string ToString(bool b);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200129
Jonas Olsson6b1985d2018-07-05 11:59:48 +0200130std::string ToString(const char* s);
131std::string ToString(std::string t);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200132
Jonas Olsson6b1985d2018-07-05 11:59:48 +0200133std::string ToString(short s);
134std::string ToString(unsigned short s);
135std::string ToString(int s);
136std::string ToString(unsigned int s);
137std::string ToString(long int s);
138std::string ToString(unsigned long int s);
139std::string ToString(long long int s);
140std::string ToString(unsigned long long int s);
141
142std::string ToString(double t);
Jonas Olsson88e18482018-09-03 10:15:08 +0200143std::string ToString(long double t);
Jonas Olsson6b1985d2018-07-05 11:59:48 +0200144
145std::string ToString(const void* p);
146
147template <typename T,
148 typename std::enable_if<std::is_arithmetic<T>::value &&
149 !std::is_same<T, bool>::value,
150 int>::type = 0>
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200151static bool FromString(const std::string& s, T* t) {
152 RTC_DCHECK(t);
Jonas Olsson6b1985d2018-07-05 11:59:48 +0200153 absl::optional<T> result = StringToNumber<T>(s);
154
155 if (result)
156 *t = *result;
157
158 return result.has_value();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200159}
160
Jonas Olsson6b1985d2018-07-05 11:59:48 +0200161bool FromString(const std::string& s, bool* b);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200162
Yves Gerey665174f2018-06-19 15:03:05 +0200163template <typename T>
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200164static inline T FromString(const std::string& str) {
Yves Gerey665174f2018-06-19 15:03:05 +0200165 T val;
166 FromString(str, &val);
167 return val;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200168}
169
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200170//////////////////////////////////////////////////////////////////////
171
172} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000173
Steve Anton10542f22019-01-11 09:11:00 -0800174#endif // RTC_BASE_STRING_ENCODE_H__