All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
string-helper.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
8 #ifndef NDN_STRING_HELPER_HPP
9 #define NDN_STRING_HELPER_HPP
10 
11 #include <string>
12 #include <sstream>
13 
14 namespace ndn {
15 
16 static const char* WHITESPACE_CHARS = " \n\r\t";
17 
23 inline std::string
24 toHex(const uint8_t* array, size_t arraySize)
25 {
26  if (array == 0 || arraySize == 0)
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 
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 
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 
79 inline void
80 trim(std::string& str)
81 {
82  trimLeft(str);
83  trimRight(str);
84 }
85 
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 
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
static const char * WHITESPACE_CHARS
std::string unescape(const std::string &str)
Return a copy of str, converting each escaped &quot;%XX&quot; to the char value.
void trimRight(std::string &str)
Modify str in place to erase whitespace on the right.
std::string toHex(const uint8_t *array, size_t arraySize)
Return the hex representation of the bytes in array.
void trim(std::string &str)
Modify str in place to erase whitespace on the left and right.
void trimLeft(std::string &str)
Modify str in place to erase whitespace on the left.
int fromHexChar(uint8_t c)
Convert the hex character to an integer from 0 to 15, or -1 if not a hex character.