blob: 403c25ef4685510f201d525273ff7dc289fcb347 [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Alexander Afanasyevaf283d82014-01-03 13:23:34 -08002/**
Davide Pesavento03115ce2017-01-27 01:17:55 -05003 * Copyright (c) 2013-2017 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/**
Davide Pesavento03115ce2017-01-27 01:17:55 -0500145 * @brief Convert the hex character to an integer from 0 to 15, or -1 if not a hex character
146 */
147int
148fromHexChar(char c);
149
150/**
Alexander Afanasyev8828ca62015-07-02 13:40:09 -0700151 * @brief Convert the hex string to buffer
152 * @param hexString sequence of pairs of hex numbers (lower and upper case can be mixed)
153 * without any whitespace separators (e.g., "48656C6C6F2C20776F726C6421")
154 * @throw StringHelperError if input is invalid
155 */
Davide Pesavento03115ce2017-01-27 01:17:55 -0500156shared_ptr<Buffer>
Alexander Afanasyev8828ca62015-07-02 13:40:09 -0700157fromHex(const std::string& hexString);
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800158
159/**
Alexander Afanasyev8828ca62015-07-02 13:40:09 -0700160 * @brief Decode a percent-encoded string
161 * @see RFC 3986 section 2
162 *
163 * When % is not followed by two hex characters, the output is not transformed.
164 *
philo7b187f52015-08-24 14:07:08 -0700165 * Examples:
166 *
Davide Pesaventoe78eeca2017-02-23 23:22:32 -0500167 * @code
168 * unescape("hello%20world") == "hello world"
169 * unescape("hello%20world%FooBar") == "hello world%FooBar"
170 * @endcode
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800171 */
Alexander Afanasyev8828ca62015-07-02 13:40:09 -0700172std::string
173unescape(const std::string& str);
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800174
175} // namespace ndn
176
Davide Pesavento03115ce2017-01-27 01:17:55 -0500177#endif // NDN_UTIL_STRING_HELPER_HPP