blob: 2ea09e25d4a8d24f193861ce113484f531b8d042 [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 +020029std::string hex_encode(const std::string& str);
30std::string hex_encode(const char* source, size_t srclen);
Yves Gerey665174f2018-06-19 15:03:05 +020031std::string hex_encode_with_delimiter(const char* source,
32 size_t srclen,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020033 char delimiter);
34
35// hex_decode converts ascii hex to binary.
Yves Gerey665174f2018-06-19 15:03:05 +020036size_t hex_decode(char* buffer,
37 size_t buflen,
38 const char* source,
39 size_t srclen);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020040
41// hex_decode, assuming that there is a delimiter between every byte
42// pair.
43// |delimiter| == 0 means no delimiter
44// If the buffer is too short or the data is invalid, we return 0.
Yves Gerey665174f2018-06-19 15:03:05 +020045size_t hex_decode_with_delimiter(char* buffer,
46 size_t buflen,
47 const char* source,
48 size_t srclen,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020049 char delimiter);
50
51// Helper functions for hex_decode.
52size_t hex_decode(char* buffer, size_t buflen, const std::string& source);
Yves Gerey665174f2018-06-19 15:03:05 +020053size_t hex_decode_with_delimiter(char* buffer,
54 size_t buflen,
55 const std::string& source,
56 char delimiter);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020057
Diogo Real7bd1f1b2017-09-08 12:50:41 -070058// Joins the source vector of strings into a single string, with each
59// field in source being separated by delimiter. No trailing delimiter is added.
60std::string join(const std::vector<std::string>& source, char delimiter);
61
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020062// Splits the source string into multiple fields separated by delimiter,
63// with duplicates of delimiter creating empty fields.
Yves Gerey665174f2018-06-19 15:03:05 +020064size_t split(const std::string& source,
65 char delimiter,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020066 std::vector<std::string>* fields);
67
68// Splits the source string into multiple fields separated by delimiter,
69// with duplicates of delimiter ignored. Trailing delimiter ignored.
Yves Gerey665174f2018-06-19 15:03:05 +020070size_t tokenize(const std::string& source,
71 char delimiter,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020072 std::vector<std::string>* fields);
73
74// Tokenize, including the empty tokens.
75size_t tokenize_with_empty_tokens(const std::string& source,
76 char delimiter,
77 std::vector<std::string>* fields);
78
79// Tokenize and append the tokens to fields. Return the new size of fields.
Yves Gerey665174f2018-06-19 15:03:05 +020080size_t tokenize_append(const std::string& source,
81 char delimiter,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020082 std::vector<std::string>* fields);
83
84// Splits the source string into multiple fields separated by delimiter, with
85// duplicates of delimiter ignored. Trailing delimiter ignored. A substring in
86// between the start_mark and the end_mark is treated as a single field. Return
87// the size of fields. For example, if source is "filename
88// \"/Library/Application Support/media content.txt\"", delimiter is ' ', and
89// the start_mark and end_mark are '"', this method returns two fields:
90// "filename" and "/Library/Application Support/media content.txt".
Yves Gerey665174f2018-06-19 15:03:05 +020091size_t tokenize(const std::string& source,
92 char delimiter,
93 char start_mark,
94 char end_mark,
95 std::vector<std::string>* fields);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020096
97// Extract the first token from source as separated by delimiter, with
98// duplicates of delimiter ignored. Return false if the delimiter could not be
99// found, otherwise return true.
100bool tokenize_first(const std::string& source,
101 const char delimiter,
102 std::string* token,
103 std::string* rest);
104
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200105// Convert arbitrary values to/from a string.
Jonas Olsson6b1985d2018-07-05 11:59:48 +0200106// TODO(jonasolsson): Remove these when absl::StrCat becomes available.
107std::string ToString(bool b);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200108
Jonas Olsson6b1985d2018-07-05 11:59:48 +0200109std::string ToString(const char* s);
110std::string ToString(std::string t);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200111
Jonas Olsson6b1985d2018-07-05 11:59:48 +0200112std::string ToString(short s);
113std::string ToString(unsigned short s);
114std::string ToString(int s);
115std::string ToString(unsigned int s);
116std::string ToString(long int s);
117std::string ToString(unsigned long int s);
118std::string ToString(long long int s);
119std::string ToString(unsigned long long int s);
120
121std::string ToString(double t);
Jonas Olsson88e18482018-09-03 10:15:08 +0200122std::string ToString(long double t);
Jonas Olsson6b1985d2018-07-05 11:59:48 +0200123
124std::string ToString(const void* p);
125
126template <typename T,
127 typename std::enable_if<std::is_arithmetic<T>::value &&
128 !std::is_same<T, bool>::value,
129 int>::type = 0>
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200130static bool FromString(const std::string& s, T* t) {
131 RTC_DCHECK(t);
Jonas Olsson6b1985d2018-07-05 11:59:48 +0200132 absl::optional<T> result = StringToNumber<T>(s);
133
134 if (result)
135 *t = *result;
136
137 return result.has_value();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200138}
139
Jonas Olsson6b1985d2018-07-05 11:59:48 +0200140bool FromString(const std::string& s, bool* b);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200141
Yves Gerey665174f2018-06-19 15:03:05 +0200142template <typename T>
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200143static inline T FromString(const std::string& str) {
Yves Gerey665174f2018-06-19 15:03:05 +0200144 T val;
145 FromString(str, &val);
146 return val;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200147}
148
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200149//////////////////////////////////////////////////////////////////////
150
151} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000152
Steve Anton10542f22019-01-11 09:11:00 -0800153#endif // RTC_BASE_STRING_ENCODE_H__