blob: a34fdb7b05bde786d287b49b6e01cb87d7ba96cf [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento38a061d2018-02-15 22:45:48 -05002/*
3 * Copyright (c) 2013-2018 Regents of the University of California.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
Alexander Afanasyevaf283d82014-01-03 13:23:34 -080020 */
21
Davide Pesavento03115ce2017-01-27 01:17:55 -050022#ifndef NDN_UTIL_STRING_HELPER_HPP
23#define NDN_UTIL_STRING_HELPER_HPP
Alexander Afanasyevaf283d82014-01-03 13:23:34 -080024
Alexander Afanasyev8828ca62015-07-02 13:40:09 -070025#include "../common.hpp"
Alexander Afanasyevaf283d82014-01-03 13:23:34 -080026
27namespace ndn {
28
Davide Pesaventodd461432017-01-28 21:47:26 -050029class Buffer;
30
Alexander Afanasyev8828ca62015-07-02 13:40:09 -070031class StringHelperError : public std::invalid_argument
32{
33public:
34 explicit
35 StringHelperError(const std::string& what)
36 : std::invalid_argument(what)
37 {
38 }
39};
40
41/**
Davide Pesaventoe78eeca2017-02-23 23:22:32 -050042 * @brief Helper class to convert a number to hexadecimal
43 * format, for use with stream insertion operators
44 *
45 * Example usage:
46 *
47 * @code
48 * std::cout << AsHex{42}; // outputs "0x2a"
49 * std::cout << std::uppercase << AsHex{42}; // outputs "0x2A"
50 * @endcode
51 */
52class AsHex
53{
54public:
55 constexpr explicit
56 AsHex(uint64_t val) noexcept
57 : m_value(val)
58 {
59 }
60
61private:
62 uint64_t m_value;
63
64 friend std::ostream& operator<<(std::ostream&, const AsHex&);
65};
66
67std::ostream&
68operator<<(std::ostream& os, const AsHex& hex);
69
70/**
71 * @brief Output the hex representation of @p num to the output stream @p os
Alexander Afanasyev8828ca62015-07-02 13:40:09 -070072 *
73 * @param os Output stream
Davide Pesaventoe78eeca2017-02-23 23:22:32 -050074 * @param num Number to print in hexadecimal format
75 * @param wantUpperCase if true, print uppercase hex chars; the default is to use lowercase
76 *
77 * The output string is a continuous sequence of hex characters without any whitespace separators.
78 */
79void
80printHex(std::ostream& os, uint64_t num, bool wantUpperCase = false);
81
82/**
83 * @brief Output the hex representation of the bytes in @p buffer to the output stream @p os
84 *
85 * @param os Output stream
86 * @param buffer Pointer to an array of bytes
Alexander Afanasyev8828ca62015-07-02 13:40:09 -070087 * @param length Size of the array
Davide Pesaventoe78eeca2017-02-23 23:22:32 -050088 * @param wantUpperCase if true (the default) print uppercase hex chars
Alexander Afanasyev8828ca62015-07-02 13:40:09 -070089 *
philo7b187f52015-08-24 14:07:08 -070090 * Examples:
91 *
Davide Pesaventoe78eeca2017-02-23 23:22:32 -050092 * @code
93 * printHex(std::cout, "Hello, World!"); // outputs "48656C6C6F2C20776F726C6421"
94 * printHex(std::cout, "Hello, World!", false); // outputs "48656c6c6f2c20776f726c6421"
95 * @endcode
Alexander Afanasyev8828ca62015-07-02 13:40:09 -070096 *
97 * Each octet is always represented as two hex characters ("00" for octet==0).
98 *
99 * The output string is a continuous sequence of hex characters without any whitespace separators.
100 */
101void
Davide Pesaventofdda2412017-01-28 18:53:51 -0500102printHex(std::ostream& os, const uint8_t* buffer, size_t length, bool wantUpperCase = true);
Alexander Afanasyev8828ca62015-07-02 13:40:09 -0700103
104/**
Davide Pesaventoe78eeca2017-02-23 23:22:32 -0500105 * @brief Output the hex representation of the bytes in @p buffer to the output stream @p os
Alexander Afanasyev8828ca62015-07-02 13:40:09 -0700106 *
107 * @param os Output stream
Davide Pesaventoe78eeca2017-02-23 23:22:32 -0500108 * @param buffer Buffer of bytes to print in hexadecimal format
109 * @param wantUpperCase if true (the default) print uppercase hex chars
Alexander Afanasyev8828ca62015-07-02 13:40:09 -0700110 */
111void
Davide Pesaventofdda2412017-01-28 18:53:51 -0500112printHex(std::ostream& os, const Buffer& buffer, bool wantUpperCase = true);
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800113
114/**
Davide Pesaventoe78eeca2017-02-23 23:22:32 -0500115 * @brief Return a string containing the hex representation of the bytes in @p buffer
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700116 *
Davide Pesaventoe78eeca2017-02-23 23:22:32 -0500117 * @param buffer Pointer to an array of bytes
Alexander Afanasyev8828ca62015-07-02 13:40:09 -0700118 * @param length Size of the array
Davide Pesaventoe78eeca2017-02-23 23:22:32 -0500119 * @param wantUpperCase if true (the default) use uppercase hex chars
Alexander Afanasyev8828ca62015-07-02 13:40:09 -0700120 *
philo7b187f52015-08-24 14:07:08 -0700121 * Examples:
122 *
Davide Pesaventoe78eeca2017-02-23 23:22:32 -0500123 * @code
124 * toHex("Hello, World!") == "48656C6C6F2C20776F726C6421"
125 * toHex("Hello, World!", false) == "48656c6c6f2c20776f726c6421"
126 * @endcode
Alexander Afanasyev8828ca62015-07-02 13:40:09 -0700127 *
128 * Each octet is always represented as two hex characters ("00" for octet==0).
129 *
130 * The output string is a continuous sequence of hex characters without any whitespace separators.
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800131 */
Alexander Afanasyev8828ca62015-07-02 13:40:09 -0700132std::string
Davide Pesaventofdda2412017-01-28 18:53:51 -0500133toHex(const uint8_t* buffer, size_t length, bool wantUpperCase = true);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700134
Alexander Afanasyev8828ca62015-07-02 13:40:09 -0700135/**
Davide Pesaventoe78eeca2017-02-23 23:22:32 -0500136 * @brief Return a string containing the hex representation of the bytes in @p buffer
Alexander Afanasyev8828ca62015-07-02 13:40:09 -0700137 *
Davide Pesaventoe78eeca2017-02-23 23:22:32 -0500138 * @param buffer Buffer of bytes to convert to hexadecimal format
139 * @param wantUpperCase if true (the default) use uppercase hex chars
Alexander Afanasyev8828ca62015-07-02 13:40:09 -0700140 */
141std::string
Davide Pesaventofdda2412017-01-28 18:53:51 -0500142toHex(const Buffer& buffer, bool wantUpperCase = true);
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800143
Alexander Afanasyev8828ca62015-07-02 13:40:09 -0700144/**
145 * @brief Convert the hex string to buffer
146 * @param hexString sequence of pairs of hex numbers (lower and upper case can be mixed)
147 * without any whitespace separators (e.g., "48656C6C6F2C20776F726C6421")
148 * @throw StringHelperError if input is invalid
149 */
Davide Pesavento03115ce2017-01-27 01:17:55 -0500150shared_ptr<Buffer>
Alexander Afanasyev8828ca62015-07-02 13:40:09 -0700151fromHex(const std::string& hexString);
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800152
153/**
Davide Pesavento38a061d2018-02-15 22:45:48 -0500154 * @brief Convert (the least significant nibble of) @p n to the corresponding hex character
155 */
156constexpr char
157toHexChar(unsigned int n, bool wantUpperCase = true) noexcept
158{
159 return wantUpperCase ?
160 "0123456789ABCDEF"[n & 0xf] :
161 "0123456789abcdef"[n & 0xf];
162}
163
164/**
165 * @brief Convert the hex character @p c to an integer in [0, 15], or -1 if it's not a hex character
166 */
167constexpr int
168fromHexChar(char c) noexcept
169{
170 return (c >= '0' && c <= '9') ? int(c - '0') :
171 (c >= 'A' && c <= 'F') ? int(c - 'A' + 10) :
172 (c >= 'a' && c <= 'f') ? int(c - 'a' + 10) :
173 -1;
174}
175
176/**
177 * @brief Percent-encode a string
178 * @see RFC 3986 section 2
179 *
180 * This function will encode all characters that are not one of the following:
181 * ALPHA ("a" to "z" and "A" to "Z") / DIGIT (0 to 9) / "-" / "." / "_" / "~"
182 *
183 * The hex encoding uses the numbers 0-9 and the uppercase letters A-F.
184 *
185 * Examples:
186 *
187 * @code
188 * escape("hello world") == "hello%20world"
189 * escape("100%") == "100%25"
190 * @endcode
191 */
192std::string
193escape(const std::string& str);
194
195void
196escape(std::ostream& os, const char* str, size_t len);
197
198/**
Alexander Afanasyev8828ca62015-07-02 13:40:09 -0700199 * @brief Decode a percent-encoded string
200 * @see RFC 3986 section 2
201 *
202 * When % is not followed by two hex characters, the output is not transformed.
203 *
philo7b187f52015-08-24 14:07:08 -0700204 * Examples:
205 *
Davide Pesaventoe78eeca2017-02-23 23:22:32 -0500206 * @code
207 * unescape("hello%20world") == "hello world"
208 * unescape("hello%20world%FooBar") == "hello world%FooBar"
209 * @endcode
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800210 */
Alexander Afanasyev8828ca62015-07-02 13:40:09 -0700211std::string
212unescape(const std::string& str);
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800213
Davide Pesavento38a061d2018-02-15 22:45:48 -0500214void
215unescape(std::ostream& os, const char* str, size_t len);
216
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800217} // namespace ndn
218
Davide Pesavento03115ce2017-01-27 01:17:55 -0500219#endif // NDN_UTIL_STRING_HELPER_HPP