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 | 95e8c2f | 2014-02-06 17:29:30 -0800 | [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 | */ |
| 23 | inline std::string |
| 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 ""; |
| 28 | |
| 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 | */ |
| 45 | inline void |
| 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 |
| 55 | str.clear(); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Modify str in place to erase whitespace on the right. |
| 60 | * @param str |
| 61 | */ |
| 62 | inline void |
| 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 | */ |
| 79 | inline void |
| 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 |
| 89 | * @return |
| 90 | */ |
| 91 | inline int |
| 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 | */ |
| 108 | inline std::string |
| 109 | unescape(const std::string& str) |
| 110 | { |
| 111 | std::ostringstream result; |
| 112 | |
| 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]); |
| 117 | |
| 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); |
| 123 | |
| 124 | // Skip ahead past the escaped value. |
| 125 | i += 2; |
| 126 | } |
| 127 | else |
| 128 | // Just copy through. |
| 129 | result << str[i]; |
| 130 | } |
| 131 | |
| 132 | return result.str(); |
| 133 | } |
| 134 | |
| 135 | } // namespace ndn |
| 136 | |
| 137 | #endif // NDN_STRING_HELPER_HPP |