blob: 190150599f965098826d5644f3a429da247e3cd4 [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
Niels Möller44203802021-09-16 15:39:16 +020020#include "absl/strings/string_view.h"
Yves Gerey988cc082018-10-23 12:03:01 +020021#include "absl/types/optional.h"
Ali Tofighfd6a4d62022-03-31 10:36:48 +020022#include "api/array_view.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020023#include "rtc_base/checks.h"
Jonas Olsson6b1985d2018-07-05 11:59:48 +020024#include "rtc_base/string_to_number.h"
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020025
26namespace rtc {
27
28//////////////////////////////////////////////////////////////////////
29// String Encoding Utilities
30//////////////////////////////////////////////////////////////////////
31
Ali Tofigh7fa90572022-03-17 15:47:49 +010032std::string hex_encode(absl::string_view str);
Ali Tofighfd6a4d62022-03-31 10:36:48 +020033std::string hex_encode_with_delimiter(absl::string_view source, char delimiter);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020034
35// hex_decode converts ascii hex to binary.
Ali Tofighfd6a4d62022-03-31 10:36:48 +020036size_t hex_decode(ArrayView<char> buffer, absl::string_view source);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020037
38// hex_decode, assuming that there is a delimiter between every byte
39// pair.
Artem Titov96e3b992021-07-26 16:03:14 +020040// `delimiter` == 0 means no delimiter
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020041// If the buffer is too short or the data is invalid, we return 0.
Ali Tofighfd6a4d62022-03-31 10:36:48 +020042size_t hex_decode_with_delimiter(ArrayView<char> buffer,
43 absl::string_view source,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020044 char delimiter);
Yves Gerey665174f2018-06-19 15:03:05 +020045size_t hex_decode_with_delimiter(char* buffer,
46 size_t buflen,
Ali Tofigh7fa90572022-03-17 15:47:49 +010047 absl::string_view source,
Yves Gerey665174f2018-06-19 15:03:05 +020048 char delimiter);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020049
Diogo Real7bd1f1b2017-09-08 12:50:41 -070050// Joins the source vector of strings into a single string, with each
51// field in source being separated by delimiter. No trailing delimiter is added.
52std::string join(const std::vector<std::string>& source, char delimiter);
53
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020054// Splits the source string into multiple fields separated by delimiter,
Niels Möller2d3186e2022-01-24 14:15:03 +010055// with duplicates of delimiter creating empty fields. Empty input produces a
56// single, empty, field.
57std::vector<absl::string_view> split(absl::string_view source, char delimiter);
58
Niels Möller44203802021-09-16 15:39:16 +020059size_t split(absl::string_view source,
Yves Gerey665174f2018-06-19 15:03:05 +020060 char delimiter,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020061 std::vector<std::string>* fields);
62
63// Splits the source string into multiple fields separated by delimiter,
64// with duplicates of delimiter ignored. Trailing delimiter ignored.
Niels Möller44203802021-09-16 15:39:16 +020065size_t tokenize(absl::string_view source,
Yves Gerey665174f2018-06-19 15:03:05 +020066 char delimiter,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020067 std::vector<std::string>* fields);
68
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020069// Extract the first token from source as separated by delimiter, with
70// duplicates of delimiter ignored. Return false if the delimiter could not be
71// found, otherwise return true.
Niels Möller44203802021-09-16 15:39:16 +020072bool tokenize_first(absl::string_view source,
Ali Tofigh62238092022-01-25 13:27:19 +010073 char delimiter,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020074 std::string* token,
75 std::string* rest);
76
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020077// Convert arbitrary values to/from a string.
Jonas Olsson6b1985d2018-07-05 11:59:48 +020078// TODO(jonasolsson): Remove these when absl::StrCat becomes available.
79std::string ToString(bool b);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020080
Ali Tofigh7fa90572022-03-17 15:47:49 +010081std::string ToString(absl::string_view s);
Ali Tofighfd6a4d62022-03-31 10:36:48 +020082// The const char* overload is needed for correct overload resolution because of
83// the const void* version of ToString() below.
84std::string ToString(const char* s);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020085
Jonas Olsson6b1985d2018-07-05 11:59:48 +020086std::string ToString(short s);
87std::string ToString(unsigned short s);
88std::string ToString(int s);
89std::string ToString(unsigned int s);
90std::string ToString(long int s);
91std::string ToString(unsigned long int s);
92std::string ToString(long long int s);
93std::string ToString(unsigned long long int s);
94
95std::string ToString(double t);
Jonas Olsson88e18482018-09-03 10:15:08 +020096std::string ToString(long double t);
Jonas Olsson6b1985d2018-07-05 11:59:48 +020097
98std::string ToString(const void* p);
99
100template <typename T,
101 typename std::enable_if<std::is_arithmetic<T>::value &&
102 !std::is_same<T, bool>::value,
103 int>::type = 0>
Ali Tofighfd6a4d62022-03-31 10:36:48 +0200104static bool FromString(absl::string_view s, T* t) {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200105 RTC_DCHECK(t);
Jonas Olsson6b1985d2018-07-05 11:59:48 +0200106 absl::optional<T> result = StringToNumber<T>(s);
107
108 if (result)
109 *t = *result;
110
111 return result.has_value();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200112}
113
Ali Tofighfd6a4d62022-03-31 10:36:48 +0200114bool FromString(absl::string_view s, bool* b);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200115
Yves Gerey665174f2018-06-19 15:03:05 +0200116template <typename T>
Ali Tofighfd6a4d62022-03-31 10:36:48 +0200117static inline T FromString(absl::string_view str) {
Yves Gerey665174f2018-06-19 15:03:05 +0200118 T val;
119 FromString(str, &val);
120 return val;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200121}
122
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200123//////////////////////////////////////////////////////////////////////
124
125} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000126
Steve Anton10542f22019-01-11 09:11:00 -0800127#endif // RTC_BASE_STRING_ENCODE_H__