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