Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 2 | /** |
Davide Pesavento | 03115ce | 2017-01-27 01:17:55 -0500 | [diff] [blame] | 3 | * Copyright (c) 2013-2017 Regents of the University of California. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 6 | * |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 7 | * 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 Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 20 | */ |
| 21 | |
Davide Pesavento | 03115ce | 2017-01-27 01:17:55 -0500 | [diff] [blame] | 22 | #ifndef NDN_UTIL_STRING_HELPER_HPP |
| 23 | #define NDN_UTIL_STRING_HELPER_HPP |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 24 | |
Alexander Afanasyev | 8828ca6 | 2015-07-02 13:40:09 -0700 | [diff] [blame] | 25 | #include "../common.hpp" |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 26 | |
| 27 | namespace ndn { |
| 28 | |
Davide Pesavento | dd46143 | 2017-01-28 21:47:26 -0500 | [diff] [blame] | 29 | class Buffer; |
| 30 | |
Alexander Afanasyev | 8828ca6 | 2015-07-02 13:40:09 -0700 | [diff] [blame] | 31 | class StringHelperError : public std::invalid_argument |
| 32 | { |
| 33 | public: |
| 34 | explicit |
| 35 | StringHelperError(const std::string& what) |
| 36 | : std::invalid_argument(what) |
| 37 | { |
| 38 | } |
| 39 | }; |
| 40 | |
| 41 | /** |
Davide Pesavento | e78eeca | 2017-02-23 23:22:32 -0500 | [diff] [blame] | 42 | * @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 | */ |
| 52 | class AsHex |
| 53 | { |
| 54 | public: |
| 55 | constexpr explicit |
| 56 | AsHex(uint64_t val) noexcept |
| 57 | : m_value(val) |
| 58 | { |
| 59 | } |
| 60 | |
| 61 | private: |
| 62 | uint64_t m_value; |
| 63 | |
| 64 | friend std::ostream& operator<<(std::ostream&, const AsHex&); |
| 65 | }; |
| 66 | |
| 67 | std::ostream& |
| 68 | operator<<(std::ostream& os, const AsHex& hex); |
| 69 | |
| 70 | /** |
| 71 | * @brief Output the hex representation of @p num to the output stream @p os |
Alexander Afanasyev | 8828ca6 | 2015-07-02 13:40:09 -0700 | [diff] [blame] | 72 | * |
| 73 | * @param os Output stream |
Davide Pesavento | e78eeca | 2017-02-23 23:22:32 -0500 | [diff] [blame] | 74 | * @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 | */ |
| 79 | void |
| 80 | printHex(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 Afanasyev | 8828ca6 | 2015-07-02 13:40:09 -0700 | [diff] [blame] | 87 | * @param length Size of the array |
Davide Pesavento | e78eeca | 2017-02-23 23:22:32 -0500 | [diff] [blame] | 88 | * @param wantUpperCase if true (the default) print uppercase hex chars |
Alexander Afanasyev | 8828ca6 | 2015-07-02 13:40:09 -0700 | [diff] [blame] | 89 | * |
philo | 7b187f5 | 2015-08-24 14:07:08 -0700 | [diff] [blame] | 90 | * Examples: |
| 91 | * |
Davide Pesavento | e78eeca | 2017-02-23 23:22:32 -0500 | [diff] [blame] | 92 | * @code |
| 93 | * printHex(std::cout, "Hello, World!"); // outputs "48656C6C6F2C20776F726C6421" |
| 94 | * printHex(std::cout, "Hello, World!", false); // outputs "48656c6c6f2c20776f726c6421" |
| 95 | * @endcode |
Alexander Afanasyev | 8828ca6 | 2015-07-02 13:40:09 -0700 | [diff] [blame] | 96 | * |
| 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 | */ |
| 101 | void |
Davide Pesavento | fdda241 | 2017-01-28 18:53:51 -0500 | [diff] [blame] | 102 | printHex(std::ostream& os, const uint8_t* buffer, size_t length, bool wantUpperCase = true); |
Alexander Afanasyev | 8828ca6 | 2015-07-02 13:40:09 -0700 | [diff] [blame] | 103 | |
| 104 | /** |
Davide Pesavento | e78eeca | 2017-02-23 23:22:32 -0500 | [diff] [blame] | 105 | * @brief Output the hex representation of the bytes in @p buffer to the output stream @p os |
Alexander Afanasyev | 8828ca6 | 2015-07-02 13:40:09 -0700 | [diff] [blame] | 106 | * |
| 107 | * @param os Output stream |
Davide Pesavento | e78eeca | 2017-02-23 23:22:32 -0500 | [diff] [blame] | 108 | * @param buffer Buffer of bytes to print in hexadecimal format |
| 109 | * @param wantUpperCase if true (the default) print uppercase hex chars |
Alexander Afanasyev | 8828ca6 | 2015-07-02 13:40:09 -0700 | [diff] [blame] | 110 | */ |
| 111 | void |
Davide Pesavento | fdda241 | 2017-01-28 18:53:51 -0500 | [diff] [blame] | 112 | printHex(std::ostream& os, const Buffer& buffer, bool wantUpperCase = true); |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 113 | |
| 114 | /** |
Davide Pesavento | e78eeca | 2017-02-23 23:22:32 -0500 | [diff] [blame] | 115 | * @brief Return a string containing the hex representation of the bytes in @p buffer |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 116 | * |
Davide Pesavento | e78eeca | 2017-02-23 23:22:32 -0500 | [diff] [blame] | 117 | * @param buffer Pointer to an array of bytes |
Alexander Afanasyev | 8828ca6 | 2015-07-02 13:40:09 -0700 | [diff] [blame] | 118 | * @param length Size of the array |
Davide Pesavento | e78eeca | 2017-02-23 23:22:32 -0500 | [diff] [blame] | 119 | * @param wantUpperCase if true (the default) use uppercase hex chars |
Alexander Afanasyev | 8828ca6 | 2015-07-02 13:40:09 -0700 | [diff] [blame] | 120 | * |
philo | 7b187f5 | 2015-08-24 14:07:08 -0700 | [diff] [blame] | 121 | * Examples: |
| 122 | * |
Davide Pesavento | e78eeca | 2017-02-23 23:22:32 -0500 | [diff] [blame] | 123 | * @code |
| 124 | * toHex("Hello, World!") == "48656C6C6F2C20776F726C6421" |
| 125 | * toHex("Hello, World!", false) == "48656c6c6f2c20776f726c6421" |
| 126 | * @endcode |
Alexander Afanasyev | 8828ca6 | 2015-07-02 13:40:09 -0700 | [diff] [blame] | 127 | * |
| 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 Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 131 | */ |
Alexander Afanasyev | 8828ca6 | 2015-07-02 13:40:09 -0700 | [diff] [blame] | 132 | std::string |
Davide Pesavento | fdda241 | 2017-01-28 18:53:51 -0500 | [diff] [blame] | 133 | toHex(const uint8_t* buffer, size_t length, bool wantUpperCase = true); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 134 | |
Alexander Afanasyev | 8828ca6 | 2015-07-02 13:40:09 -0700 | [diff] [blame] | 135 | /** |
Davide Pesavento | e78eeca | 2017-02-23 23:22:32 -0500 | [diff] [blame] | 136 | * @brief Return a string containing the hex representation of the bytes in @p buffer |
Alexander Afanasyev | 8828ca6 | 2015-07-02 13:40:09 -0700 | [diff] [blame] | 137 | * |
Davide Pesavento | e78eeca | 2017-02-23 23:22:32 -0500 | [diff] [blame] | 138 | * @param buffer Buffer of bytes to convert to hexadecimal format |
| 139 | * @param wantUpperCase if true (the default) use uppercase hex chars |
Alexander Afanasyev | 8828ca6 | 2015-07-02 13:40:09 -0700 | [diff] [blame] | 140 | */ |
| 141 | std::string |
Davide Pesavento | fdda241 | 2017-01-28 18:53:51 -0500 | [diff] [blame] | 142 | toHex(const Buffer& buffer, bool wantUpperCase = true); |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 143 | |
Alexander Afanasyev | 8828ca6 | 2015-07-02 13:40:09 -0700 | [diff] [blame] | 144 | /** |
Davide Pesavento | 03115ce | 2017-01-27 01:17:55 -0500 | [diff] [blame] | 145 | * @brief Convert the hex character to an integer from 0 to 15, or -1 if not a hex character |
| 146 | */ |
| 147 | int |
| 148 | fromHexChar(char c); |
| 149 | |
| 150 | /** |
Alexander Afanasyev | 8828ca6 | 2015-07-02 13:40:09 -0700 | [diff] [blame] | 151 | * @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 Pesavento | 03115ce | 2017-01-27 01:17:55 -0500 | [diff] [blame] | 156 | shared_ptr<Buffer> |
Alexander Afanasyev | 8828ca6 | 2015-07-02 13:40:09 -0700 | [diff] [blame] | 157 | fromHex(const std::string& hexString); |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 158 | |
| 159 | /** |
Alexander Afanasyev | 8828ca6 | 2015-07-02 13:40:09 -0700 | [diff] [blame] | 160 | * @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 | * |
philo | 7b187f5 | 2015-08-24 14:07:08 -0700 | [diff] [blame] | 165 | * Examples: |
| 166 | * |
Davide Pesavento | e78eeca | 2017-02-23 23:22:32 -0500 | [diff] [blame] | 167 | * @code |
| 168 | * unescape("hello%20world") == "hello world" |
| 169 | * unescape("hello%20world%FooBar") == "hello world%FooBar" |
| 170 | * @endcode |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 171 | */ |
Alexander Afanasyev | 8828ca6 | 2015-07-02 13:40:09 -0700 | [diff] [blame] | 172 | std::string |
| 173 | unescape(const std::string& str); |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 174 | |
| 175 | } // namespace ndn |
| 176 | |
Davide Pesavento | 03115ce | 2017-01-27 01:17:55 -0500 | [diff] [blame] | 177 | #endif // NDN_UTIL_STRING_HELPER_HPP |