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 | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 20 | * |
| 21 | * @author Jeff Thompson <jefft0@remap.ucla.edu> |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 22 | */ |
| 23 | |
| 24 | #ifndef NDN_STRING_HELPER_HPP |
| 25 | #define NDN_STRING_HELPER_HPP |
| 26 | |
| 27 | #include <string> |
| 28 | #include <sstream> |
| 29 | |
| 30 | namespace ndn { |
| 31 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 32 | static const char* WHITESPACE_CHARS = " \n\r\t"; |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 33 | |
| 34 | /** |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 35 | * @brief Return the hex representation of the bytes in array |
| 36 | * |
| 37 | * @param array The array of bytes |
| 38 | * @param arraySize Size of the array |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 39 | */ |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 40 | inline std::string |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 41 | toHex(const uint8_t* array, size_t arraySize) |
| 42 | { |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 43 | if (array == 0 || arraySize == 0) |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 44 | return ""; |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 45 | |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 46 | std::ostringstream result; |
| 47 | result.flags(std::ios::hex | std::ios::uppercase); |
| 48 | for (size_t i = 0; i < arraySize; ++i) { |
| 49 | uint8_t x = array[i]; |
| 50 | if (x < 16) |
| 51 | result << '0'; |
| 52 | result << (unsigned int)x; |
| 53 | } |
| 54 | |
| 55 | return result.str(); |
| 56 | } |
| 57 | |
| 58 | /** |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 59 | * @brief Modify str in place to erase whitespace on the left |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 60 | */ |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 61 | inline void |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 62 | trimLeft(std::string& str) |
| 63 | { |
| 64 | size_t found = str.find_first_not_of(WHITESPACE_CHARS); |
| 65 | if (found != std::string::npos) { |
| 66 | if (found > 0) |
| 67 | str.erase(0, found); |
| 68 | } |
| 69 | else |
| 70 | // All whitespace |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 71 | str.clear(); |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | /** |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 75 | * @brief Modify str in place to erase whitespace on the right |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 76 | */ |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 77 | inline void |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 78 | trimRight(std::string& str) |
| 79 | { |
| 80 | size_t found = str.find_last_not_of(WHITESPACE_CHARS); |
| 81 | if (found != std::string::npos) { |
| 82 | if (found + 1 < str.size()) |
| 83 | str.erase(found + 1); |
| 84 | } |
| 85 | else |
| 86 | // All whitespace |
| 87 | str.clear(); |
| 88 | } |
| 89 | |
| 90 | /** |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 91 | * @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] | 92 | */ |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 93 | inline void |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 94 | trim(std::string& str) |
| 95 | { |
| 96 | trimLeft(str); |
| 97 | trimRight(str); |
| 98 | } |
| 99 | |
| 100 | /** |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 101 | * @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] | 102 | */ |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 103 | inline int |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 104 | fromHexChar(uint8_t c) |
| 105 | { |
| 106 | if (c >= '0' && c <= '9') |
| 107 | return (int)c - (int)'0'; |
| 108 | else if (c >= 'A' && c <= 'F') |
| 109 | return (int)c - (int)'A' + 10; |
| 110 | else if (c >= 'a' && c <= 'f') |
| 111 | return (int)c - (int)'a' + 10; |
| 112 | else |
| 113 | return -1; |
| 114 | } |
| 115 | |
| 116 | /** |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 117 | * @brief Return a copy of str, converting each escaped "%XX" to the char value |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 118 | */ |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 119 | inline std::string |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 120 | unescape(const std::string& str) |
| 121 | { |
| 122 | std::ostringstream result; |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 123 | |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 124 | for (size_t i = 0; i < str.size(); ++i) { |
| 125 | if (str[i] == '%' && i + 2 < str.size()) { |
| 126 | int hi = fromHexChar(str[i + 1]); |
| 127 | int lo = fromHexChar(str[i + 2]); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 128 | |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 129 | if (hi < 0 || lo < 0) |
| 130 | // Invalid hex characters, so just keep the escaped string. |
| 131 | result << str[i] << str[i + 1] << str[i + 2]; |
| 132 | else |
| 133 | result << (uint8_t)(16 * hi + lo); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 134 | |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 135 | // Skip ahead past the escaped value. |
| 136 | i += 2; |
| 137 | } |
| 138 | else |
| 139 | // Just copy through. |
| 140 | result << str[i]; |
| 141 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 142 | |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 143 | return result.str(); |
| 144 | } |
| 145 | |
| 146 | } // namespace ndn |
| 147 | |
| 148 | #endif // NDN_STRING_HELPER_HPP |