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