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 | /** |
Alexander Afanasyev | 73e3004 | 2015-09-17 17:09:51 -0700 | [diff] [blame] | 3 | * Copyright (c) 2013-2015 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 | |
| 22 | #ifndef NDN_STRING_HELPER_HPP |
| 23 | #define NDN_STRING_HELPER_HPP |
| 24 | |
Alexander Afanasyev | 8828ca6 | 2015-07-02 13:40:09 -0700 | [diff] [blame] | 25 | #include "../common.hpp" |
| 26 | #include "../encoding/buffer.hpp" |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 27 | |
| 28 | namespace ndn { |
| 29 | |
Alexander Afanasyev | 8828ca6 | 2015-07-02 13:40:09 -0700 | [diff] [blame] | 30 | class StringHelperError : public std::invalid_argument |
| 31 | { |
| 32 | public: |
| 33 | explicit |
| 34 | StringHelperError(const std::string& what) |
| 35 | : std::invalid_argument(what) |
| 36 | { |
| 37 | } |
| 38 | }; |
| 39 | |
| 40 | /** |
| 41 | * @brief Output the hex representation of the bytes in array to the output stream @p os |
| 42 | * |
| 43 | * @param os Output stream |
| 44 | * @param buffer The array of bytes |
| 45 | * @param length Size of the array |
| 46 | * @param isUpperCase if true (default) output use uppercase for hex values |
| 47 | * |
philo | 7b187f5 | 2015-08-24 14:07:08 -0700 | [diff] [blame] | 48 | * Examples: |
| 49 | * |
Alexander Afanasyev | 8828ca6 | 2015-07-02 13:40:09 -0700 | [diff] [blame] | 50 | * printHex(std::cout, "Hello, World!") outputs "48656C6C6F2C20776F726C6421" |
| 51 | * printHex(std::cout, "Hello, World!", false) outputs "48656c6c6f2c20776f726c6421" |
| 52 | * |
| 53 | * Each octet is always represented as two hex characters ("00" for octet==0). |
| 54 | * |
| 55 | * The output string is a continuous sequence of hex characters without any whitespace separators. |
| 56 | */ |
| 57 | void |
| 58 | printHex(std::ostream& os, const uint8_t* buffer, size_t length, bool isUpperCase = true); |
| 59 | |
| 60 | /** |
| 61 | * @brief Output the hex representation of the bytes in the @p buffer to the output stream @p os |
| 62 | * |
| 63 | * @param os Output stream |
| 64 | * @param buffer The array of bytes |
| 65 | * @param isUpperCase if true (default) output use uppercase for hex values |
| 66 | */ |
| 67 | void |
| 68 | printHex(std::ostream& os, const Buffer& buffer, bool isUpperCase = true); |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 69 | |
| 70 | /** |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 71 | * @brief Return the hex representation of the bytes in array |
| 72 | * |
Alexander Afanasyev | 8828ca6 | 2015-07-02 13:40:09 -0700 | [diff] [blame] | 73 | * @param buffer The array of bytes |
| 74 | * @param length Size of the array |
| 75 | * @param isUpperCase if true (default) output use uppercase for hex values |
| 76 | * |
philo | 7b187f5 | 2015-08-24 14:07:08 -0700 | [diff] [blame] | 77 | * Examples: |
| 78 | * |
Alexander Afanasyev | 8828ca6 | 2015-07-02 13:40:09 -0700 | [diff] [blame] | 79 | * toHex("Hello, World!") == "48656C6C6F2C20776F726C6421" |
| 80 | * toHex("Hello, World!", false) == "48656c6c6f2c20776f726c6421" |
| 81 | * |
| 82 | * Each octet is always represented as two hex characters ("00" for octet==0). |
| 83 | * |
| 84 | * 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] | 85 | */ |
Alexander Afanasyev | 8828ca6 | 2015-07-02 13:40:09 -0700 | [diff] [blame] | 86 | std::string |
| 87 | toHex(const uint8_t* buffer, size_t length, bool isUpperCase = true); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 88 | |
Alexander Afanasyev | 8828ca6 | 2015-07-02 13:40:09 -0700 | [diff] [blame] | 89 | /** |
| 90 | * @brief Return the hex representation of the bytes in the @p buffer to the output stream @p os |
| 91 | * |
| 92 | * @param buffer The array of bytes |
| 93 | * @param isUpperCase if true (default) output use uppercase for hex values |
| 94 | */ |
| 95 | std::string |
| 96 | toHex(const Buffer& buffer, bool isUpperCase = true); |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 97 | |
Alexander Afanasyev | 8828ca6 | 2015-07-02 13:40:09 -0700 | [diff] [blame] | 98 | /** |
| 99 | * @brief Convert the hex string to buffer |
| 100 | * @param hexString sequence of pairs of hex numbers (lower and upper case can be mixed) |
| 101 | * without any whitespace separators (e.g., "48656C6C6F2C20776F726C6421") |
| 102 | * @throw StringHelperError if input is invalid |
| 103 | */ |
| 104 | shared_ptr<const Buffer> |
| 105 | fromHex(const std::string& hexString); |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 106 | |
| 107 | /** |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 108 | * @brief Modify str in place to erase whitespace on the left |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 109 | */ |
Alexander Afanasyev | 8828ca6 | 2015-07-02 13:40:09 -0700 | [diff] [blame] | 110 | void |
| 111 | trimLeft(std::string& str); |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 112 | |
| 113 | /** |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 114 | * @brief Modify str in place to erase whitespace on the right |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 115 | */ |
Alexander Afanasyev | 8828ca6 | 2015-07-02 13:40:09 -0700 | [diff] [blame] | 116 | void |
| 117 | trimRight(std::string& str); |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 118 | |
| 119 | /** |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 120 | * @brief Modify str in place to erase whitespace on the left and right |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 121 | */ |
Alexander Afanasyev | 8828ca6 | 2015-07-02 13:40:09 -0700 | [diff] [blame] | 122 | void |
| 123 | trim(std::string& str); |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 124 | |
| 125 | /** |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 126 | * @brief Convert the hex character to an integer from 0 to 15, or -1 if not a hex character |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 127 | */ |
Alexander Afanasyev | 8828ca6 | 2015-07-02 13:40:09 -0700 | [diff] [blame] | 128 | int |
| 129 | fromHexChar(uint8_t c); |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 130 | |
| 131 | /** |
Alexander Afanasyev | 8828ca6 | 2015-07-02 13:40:09 -0700 | [diff] [blame] | 132 | * @brief Decode a percent-encoded string |
| 133 | * @see RFC 3986 section 2 |
| 134 | * |
| 135 | * When % is not followed by two hex characters, the output is not transformed. |
| 136 | * |
philo | 7b187f5 | 2015-08-24 14:07:08 -0700 | [diff] [blame] | 137 | * Examples: |
| 138 | * |
| 139 | * unescape("hello%20world") == "hello world" |
| 140 | * unescape("hello%20world%FooBar") == "hello world%FooBar" |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 141 | */ |
Alexander Afanasyev | 8828ca6 | 2015-07-02 13:40:09 -0700 | [diff] [blame] | 142 | std::string |
| 143 | unescape(const std::string& str); |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 144 | |
| 145 | } // namespace ndn |
| 146 | |
| 147 | #endif // NDN_STRING_HELPER_HPP |