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