Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 38a061d | 2018-02-15 22:45:48 -0500 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2013-2018 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 | /** |
| 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 Pesavento | 03115ce | 2017-01-27 01:17:55 -0500 | [diff] [blame] | 150 | shared_ptr<Buffer> |
Alexander Afanasyev | 8828ca6 | 2015-07-02 13:40:09 -0700 | [diff] [blame] | 151 | fromHex(const std::string& hexString); |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 152 | |
| 153 | /** |
Davide Pesavento | 38a061d | 2018-02-15 22:45:48 -0500 | [diff] [blame] | 154 | * @brief Convert (the least significant nibble of) @p n to the corresponding hex character |
| 155 | */ |
| 156 | constexpr char |
| 157 | toHexChar(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 | */ |
| 167 | constexpr int |
| 168 | fromHexChar(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 | */ |
| 192 | std::string |
| 193 | escape(const std::string& str); |
| 194 | |
| 195 | void |
| 196 | escape(std::ostream& os, const char* str, size_t len); |
| 197 | |
| 198 | /** |
Alexander Afanasyev | 8828ca6 | 2015-07-02 13:40:09 -0700 | [diff] [blame] | 199 | * @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 | * |
philo | 7b187f5 | 2015-08-24 14:07:08 -0700 | [diff] [blame] | 204 | * Examples: |
| 205 | * |
Davide Pesavento | e78eeca | 2017-02-23 23:22:32 -0500 | [diff] [blame] | 206 | * @code |
| 207 | * unescape("hello%20world") == "hello world" |
| 208 | * unescape("hello%20world%FooBar") == "hello world%FooBar" |
| 209 | * @endcode |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 210 | */ |
Alexander Afanasyev | 8828ca6 | 2015-07-02 13:40:09 -0700 | [diff] [blame] | 211 | std::string |
| 212 | unescape(const std::string& str); |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 213 | |
Davide Pesavento | 38a061d | 2018-02-15 22:45:48 -0500 | [diff] [blame] | 214 | void |
| 215 | unescape(std::ostream& os, const char* str, size_t len); |
| 216 | |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 217 | } // namespace ndn |
| 218 | |
Davide Pesavento | 03115ce | 2017-01-27 01:17:55 -0500 | [diff] [blame] | 219 | #endif // NDN_UTIL_STRING_HELPER_HPP |