blob: 7059f0a05ca4b42ee458a451264d89f36856ff1d [file] [log] [blame]
Alexander Afanasyevaf283d82014-01-03 13:23:34 -08001/* -*- 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
14namespace ndn {
15
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070016static const char* WHITESPACE_CHARS = " \n\r\t";
Alexander Afanasyevaf283d82014-01-03 13:23:34 -080017
18/**
Alexander Afanasyev809805d2014-02-17 17:20:33 -080019 * Return the hex representation of the bytes in array.
20 * @param array The array of bytes.
21 * @return Hex string.
22 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070023inline std::string
Alexander Afanasyev809805d2014-02-17 17:20:33 -080024toHex(const uint8_t* array, size_t arraySize)
25{
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -070026 if (array == 0 || arraySize == 0)
Alexander Afanasyev809805d2014-02-17 17:20:33 -080027 return "";
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070028
Alexander Afanasyev809805d2014-02-17 17:20:33 -080029 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 Afanasyevaf283d82014-01-03 13:23:34 -080042 * Modify str in place to erase whitespace on the left.
43 * @param str
44 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070045inline void
Alexander Afanasyevaf283d82014-01-03 13:23:34 -080046trimLeft(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 Afanasyevfdbfc6d2014-04-14 15:12:11 -070055 str.clear();
Alexander Afanasyevaf283d82014-01-03 13:23:34 -080056}
57
58/**
59 * Modify str in place to erase whitespace on the right.
60 * @param str
61 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070062inline void
Alexander Afanasyevaf283d82014-01-03 13:23:34 -080063trimRight(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 Afanasyevfdbfc6d2014-04-14 15:12:11 -070079inline void
Alexander Afanasyevaf283d82014-01-03 13:23:34 -080080trim(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 Afanasyevfdbfc6d2014-04-14 15:12:11 -070089 * @return
Alexander Afanasyevaf283d82014-01-03 13:23:34 -080090 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070091inline int
Alexander Afanasyevaf283d82014-01-03 13:23:34 -080092fromHexChar(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 Afanasyevfdbfc6d2014-04-14 15:12:11 -0700108inline std::string
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800109unescape(const std::string& str)
110{
111 std::ostringstream result;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700112
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800113 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 Afanasyevfdbfc6d2014-04-14 15:12:11 -0700117
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800118 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 Afanasyevfdbfc6d2014-04-14 15:12:11 -0700123
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800124 // Skip ahead past the escaped value.
125 i += 2;
126 }
127 else
128 // Just copy through.
129 result << str[i];
130 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700131
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800132 return result.str();
133}
134
135} // namespace ndn
136
137#endif // NDN_STRING_HELPER_HPP