blob: f9f37e4cfdb1242d21fd35da4025b144b67413a6 [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Alexander Afanasyevaf283d82014-01-03 13:23:34 -08002/**
Alexander Afanasyevc169a812014-05-20 20:37:29 -04003 * Copyright (c) 2013-2014 Regents of the University of California.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -070020 *
21 * @author Jeff Thompson <jefft0@remap.ucla.edu>
Alexander Afanasyevaf283d82014-01-03 13:23:34 -080022 */
23
24#ifndef NDN_STRING_HELPER_HPP
25#define NDN_STRING_HELPER_HPP
26
27#include <string>
28#include <sstream>
29
30namespace ndn {
31
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070032static const char* WHITESPACE_CHARS = " \n\r\t";
Alexander Afanasyevaf283d82014-01-03 13:23:34 -080033
34/**
Alexander Afanasyev770827c2014-05-13 17:42:55 -070035 * @brief Return the hex representation of the bytes in array
36 *
37 * @param array The array of bytes
38 * @param arraySize Size of the array
Alexander Afanasyev809805d2014-02-17 17:20:33 -080039 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070040inline std::string
Alexander Afanasyev809805d2014-02-17 17:20:33 -080041toHex(const uint8_t* array, size_t arraySize)
42{
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -070043 if (array == 0 || arraySize == 0)
Alexander Afanasyev809805d2014-02-17 17:20:33 -080044 return "";
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070045
Alexander Afanasyev809805d2014-02-17 17:20:33 -080046 std::ostringstream result;
47 result.flags(std::ios::hex | std::ios::uppercase);
48 for (size_t i = 0; i < arraySize; ++i) {
49 uint8_t x = array[i];
50 if (x < 16)
51 result << '0';
52 result << (unsigned int)x;
53 }
54
55 return result.str();
56}
57
58/**
Alexander Afanasyev770827c2014-05-13 17:42:55 -070059 * @brief Modify str in place to erase whitespace on the left
Alexander Afanasyevaf283d82014-01-03 13:23:34 -080060 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070061inline void
Alexander Afanasyevaf283d82014-01-03 13:23:34 -080062trimLeft(std::string& str)
63{
64 size_t found = str.find_first_not_of(WHITESPACE_CHARS);
65 if (found != std::string::npos) {
66 if (found > 0)
67 str.erase(0, found);
68 }
69 else
70 // All whitespace
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070071 str.clear();
Alexander Afanasyevaf283d82014-01-03 13:23:34 -080072}
73
74/**
Alexander Afanasyev770827c2014-05-13 17:42:55 -070075 * @brief Modify str in place to erase whitespace on the right
Alexander Afanasyevaf283d82014-01-03 13:23:34 -080076 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070077inline void
Alexander Afanasyevaf283d82014-01-03 13:23:34 -080078trimRight(std::string& str)
79{
80 size_t found = str.find_last_not_of(WHITESPACE_CHARS);
81 if (found != std::string::npos) {
82 if (found + 1 < str.size())
83 str.erase(found + 1);
84 }
85 else
86 // All whitespace
87 str.clear();
88}
89
90/**
Alexander Afanasyev770827c2014-05-13 17:42:55 -070091 * @brief Modify str in place to erase whitespace on the left and right
Alexander Afanasyevaf283d82014-01-03 13:23:34 -080092 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070093inline void
Alexander Afanasyevaf283d82014-01-03 13:23:34 -080094trim(std::string& str)
95{
96 trimLeft(str);
97 trimRight(str);
98}
99
100/**
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700101 * @brief Convert the hex character to an integer from 0 to 15, or -1 if not a hex character
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800102 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700103inline int
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800104fromHexChar(uint8_t c)
105{
106 if (c >= '0' && c <= '9')
107 return (int)c - (int)'0';
108 else if (c >= 'A' && c <= 'F')
109 return (int)c - (int)'A' + 10;
110 else if (c >= 'a' && c <= 'f')
111 return (int)c - (int)'a' + 10;
112 else
113 return -1;
114}
115
116/**
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700117 * @brief Return a copy of str, converting each escaped "%XX" to the char value
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800118 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700119inline std::string
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800120unescape(const std::string& str)
121{
122 std::ostringstream result;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700123
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800124 for (size_t i = 0; i < str.size(); ++i) {
125 if (str[i] == '%' && i + 2 < str.size()) {
126 int hi = fromHexChar(str[i + 1]);
127 int lo = fromHexChar(str[i + 2]);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700128
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800129 if (hi < 0 || lo < 0)
130 // Invalid hex characters, so just keep the escaped string.
131 result << str[i] << str[i + 1] << str[i + 2];
132 else
133 result << (uint8_t)(16 * hi + lo);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700134
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800135 // Skip ahead past the escaped value.
136 i += 2;
137 }
138 else
139 // Just copy through.
140 result << str[i];
141 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700142
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800143 return result.str();
144}
145
146} // namespace ndn
147
148#endif // NDN_STRING_HELPER_HPP