blob: 577c45f9f074d671992d84dc79fa6c09e3a1a27d [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.
Artem Titov96e3b992021-07-26 16:03:14 +020044// `delimiter` == 0 means no delimiter
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020045// 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
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020075// Extract the first token from source as separated by delimiter, with
76// duplicates of delimiter ignored. Return false if the delimiter could not be
77// found, otherwise return true.
78bool tokenize_first(const std::string& source,
79 const char delimiter,
80 std::string* token,
81 std::string* rest);
82
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020083// Convert arbitrary values to/from a string.
Jonas Olsson6b1985d2018-07-05 11:59:48 +020084// TODO(jonasolsson): Remove these when absl::StrCat becomes available.
85std::string ToString(bool b);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020086
Jonas Olsson6b1985d2018-07-05 11:59:48 +020087std::string ToString(const char* s);
88std::string ToString(std::string t);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020089
Jonas Olsson6b1985d2018-07-05 11:59:48 +020090std::string ToString(short s);
91std::string ToString(unsigned short s);
92std::string ToString(int s);
93std::string ToString(unsigned int s);
94std::string ToString(long int s);
95std::string ToString(unsigned long int s);
96std::string ToString(long long int s);
97std::string ToString(unsigned long long int s);
98
99std::string ToString(double t);
Jonas Olsson88e18482018-09-03 10:15:08 +0200100std::string ToString(long double t);
Jonas Olsson6b1985d2018-07-05 11:59:48 +0200101
102std::string ToString(const void* p);
103
104template <typename T,
105 typename std::enable_if<std::is_arithmetic<T>::value &&
106 !std::is_same<T, bool>::value,
107 int>::type = 0>
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200108static bool FromString(const std::string& s, T* t) {
109 RTC_DCHECK(t);
Jonas Olsson6b1985d2018-07-05 11:59:48 +0200110 absl::optional<T> result = StringToNumber<T>(s);
111
112 if (result)
113 *t = *result;
114
115 return result.has_value();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200116}
117
Jonas Olsson6b1985d2018-07-05 11:59:48 +0200118bool FromString(const std::string& s, bool* b);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200119
Yves Gerey665174f2018-06-19 15:03:05 +0200120template <typename T>
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200121static inline T FromString(const std::string& str) {
Yves Gerey665174f2018-06-19 15:03:05 +0200122 T val;
123 FromString(str, &val);
124 return val;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200125}
126
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200127//////////////////////////////////////////////////////////////////////
128
129} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000130
Steve Anton10542f22019-01-11 09:11:00 -0800131#endif // RTC_BASE_STRING_ENCODE_H__