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