blob: 14f499e5a67c2d04a6bac14104c185257fd48e3e [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 Afanasyev95e8c2f2014-02-06 17:29:30 -080016static 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 */
23inline std::string
24toHex(const uint8_t* array, size_t arraySize)
25{
26 if (!&array)
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 Afanasyevaf283d82014-01-03 13:23:34 -080042 * Modify str in place to erase whitespace on the left.
43 * @param str
44 */
45inline void
46trimLeft(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 */
62inline void
63trimRight(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 */
79inline void
80trim(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 */
91inline int
92fromHexChar(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 */
108inline std::string
109unescape(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