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 | /** |
| 42 | * @brief Output the hex representation of the bytes in array to the output stream @p os |
| 43 | * |
| 44 | * @param os Output stream |
| 45 | * @param buffer The array of bytes |
| 46 | * @param length Size of the array |
Davide Pesavento | fdda241 | 2017-01-28 18:53:51 -0500 | [diff] [blame] | 47 | * @param wantUpperCase if true (default) output use uppercase for hex values |
Alexander Afanasyev | 8828ca6 | 2015-07-02 13:40:09 -0700 | [diff] [blame] | 48 | * |
philo | 7b187f5 | 2015-08-24 14:07:08 -0700 | [diff] [blame] | 49 | * Examples: |
| 50 | * |
Alexander Afanasyev | 8828ca6 | 2015-07-02 13:40:09 -0700 | [diff] [blame] | 51 | * printHex(std::cout, "Hello, World!") outputs "48656C6C6F2C20776F726C6421" |
| 52 | * printHex(std::cout, "Hello, World!", false) outputs "48656c6c6f2c20776f726c6421" |
| 53 | * |
| 54 | * Each octet is always represented as two hex characters ("00" for octet==0). |
| 55 | * |
| 56 | * The output string is a continuous sequence of hex characters without any whitespace separators. |
| 57 | */ |
| 58 | void |
Davide Pesavento | fdda241 | 2017-01-28 18:53:51 -0500 | [diff] [blame] | 59 | 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] | 60 | |
| 61 | /** |
| 62 | * @brief Output the hex representation of the bytes in the @p buffer to the output stream @p os |
| 63 | * |
| 64 | * @param os Output stream |
| 65 | * @param buffer The array of bytes |
Davide Pesavento | fdda241 | 2017-01-28 18:53:51 -0500 | [diff] [blame] | 66 | * @param wantUpperCase if true (default) output use uppercase for hex values |
Alexander Afanasyev | 8828ca6 | 2015-07-02 13:40:09 -0700 | [diff] [blame] | 67 | */ |
| 68 | void |
Davide Pesavento | fdda241 | 2017-01-28 18:53:51 -0500 | [diff] [blame] | 69 | printHex(std::ostream& os, const Buffer& buffer, bool wantUpperCase = true); |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 70 | |
| 71 | /** |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 72 | * @brief Return the hex representation of the bytes in array |
| 73 | * |
Alexander Afanasyev | 8828ca6 | 2015-07-02 13:40:09 -0700 | [diff] [blame] | 74 | * @param buffer The array of bytes |
| 75 | * @param length Size of the array |
Davide Pesavento | fdda241 | 2017-01-28 18:53:51 -0500 | [diff] [blame] | 76 | * @param wantUpperCase if true (default) output use uppercase for hex values |
Alexander Afanasyev | 8828ca6 | 2015-07-02 13:40:09 -0700 | [diff] [blame] | 77 | * |
philo | 7b187f5 | 2015-08-24 14:07:08 -0700 | [diff] [blame] | 78 | * Examples: |
| 79 | * |
Alexander Afanasyev | 8828ca6 | 2015-07-02 13:40:09 -0700 | [diff] [blame] | 80 | * toHex("Hello, World!") == "48656C6C6F2C20776F726C6421" |
| 81 | * toHex("Hello, World!", false) == "48656c6c6f2c20776f726c6421" |
| 82 | * |
| 83 | * Each octet is always represented as two hex characters ("00" for octet==0). |
| 84 | * |
| 85 | * 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] | 86 | */ |
Alexander Afanasyev | 8828ca6 | 2015-07-02 13:40:09 -0700 | [diff] [blame] | 87 | std::string |
Davide Pesavento | fdda241 | 2017-01-28 18:53:51 -0500 | [diff] [blame] | 88 | toHex(const uint8_t* buffer, size_t length, bool wantUpperCase = true); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 89 | |
Alexander Afanasyev | 8828ca6 | 2015-07-02 13:40:09 -0700 | [diff] [blame] | 90 | /** |
| 91 | * @brief Return the hex representation of the bytes in the @p buffer to the output stream @p os |
| 92 | * |
| 93 | * @param buffer The array of bytes |
Davide Pesavento | fdda241 | 2017-01-28 18:53:51 -0500 | [diff] [blame] | 94 | * @param wantUpperCase if true (default) output use uppercase for hex values |
Alexander Afanasyev | 8828ca6 | 2015-07-02 13:40:09 -0700 | [diff] [blame] | 95 | */ |
| 96 | std::string |
Davide Pesavento | fdda241 | 2017-01-28 18:53:51 -0500 | [diff] [blame] | 97 | toHex(const Buffer& buffer, bool wantUpperCase = true); |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 98 | |
Alexander Afanasyev | 8828ca6 | 2015-07-02 13:40:09 -0700 | [diff] [blame] | 99 | /** |
Davide Pesavento | 03115ce | 2017-01-27 01:17:55 -0500 | [diff] [blame] | 100 | * @brief Convert the hex character to an integer from 0 to 15, or -1 if not a hex character |
| 101 | */ |
| 102 | int |
| 103 | fromHexChar(char c); |
| 104 | |
| 105 | /** |
Alexander Afanasyev | 8828ca6 | 2015-07-02 13:40:09 -0700 | [diff] [blame] | 106 | * @brief Convert the hex string to buffer |
| 107 | * @param hexString sequence of pairs of hex numbers (lower and upper case can be mixed) |
| 108 | * without any whitespace separators (e.g., "48656C6C6F2C20776F726C6421") |
| 109 | * @throw StringHelperError if input is invalid |
| 110 | */ |
Davide Pesavento | 03115ce | 2017-01-27 01:17:55 -0500 | [diff] [blame] | 111 | shared_ptr<Buffer> |
Alexander Afanasyev | 8828ca6 | 2015-07-02 13:40:09 -0700 | [diff] [blame] | 112 | fromHex(const std::string& hexString); |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 113 | |
| 114 | /** |
Alexander Afanasyev | 8828ca6 | 2015-07-02 13:40:09 -0700 | [diff] [blame] | 115 | * @brief Decode a percent-encoded string |
| 116 | * @see RFC 3986 section 2 |
| 117 | * |
| 118 | * When % is not followed by two hex characters, the output is not transformed. |
| 119 | * |
philo | 7b187f5 | 2015-08-24 14:07:08 -0700 | [diff] [blame] | 120 | * Examples: |
| 121 | * |
| 122 | * unescape("hello%20world") == "hello world" |
| 123 | * unescape("hello%20world%FooBar") == "hello world%FooBar" |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 124 | */ |
Alexander Afanasyev | 8828ca6 | 2015-07-02 13:40:09 -0700 | [diff] [blame] | 125 | std::string |
| 126 | unescape(const std::string& str); |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 127 | |
| 128 | } // namespace ndn |
| 129 | |
Davide Pesavento | 03115ce | 2017-01-27 01:17:55 -0500 | [diff] [blame] | 130 | #endif // NDN_UTIL_STRING_HELPER_HPP |