blob: b4878ae29d09fab3230f666e27b2ade12c498234 [file] [log] [blame]
Alexander Afanasyevaf283d82014-01-03 13:23:34 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/**
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07003 * 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 Afanasyevaf283d82014-01-03 13:23:34 -080013 */
14
15#ifndef NDN_STRING_HELPER_HPP
16#define NDN_STRING_HELPER_HPP
17
18#include <string>
19#include <sstream>
20
21namespace ndn {
22
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070023static const char* WHITESPACE_CHARS = " \n\r\t";
Alexander Afanasyevaf283d82014-01-03 13:23:34 -080024
25/**
Alexander Afanasyev809805d2014-02-17 17:20:33 -080026 * Return the hex representation of the bytes in array.
27 * @param array The array of bytes.
28 * @return Hex string.
29 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070030inline std::string
Alexander Afanasyev809805d2014-02-17 17:20:33 -080031toHex(const uint8_t* array, size_t arraySize)
32{
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -070033 if (array == 0 || arraySize == 0)
Alexander Afanasyev809805d2014-02-17 17:20:33 -080034 return "";
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070035
Alexander Afanasyev809805d2014-02-17 17:20:33 -080036 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 Afanasyevaf283d82014-01-03 13:23:34 -080049 * Modify str in place to erase whitespace on the left.
50 * @param str
51 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070052inline void
Alexander Afanasyevaf283d82014-01-03 13:23:34 -080053trimLeft(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 Afanasyevfdbfc6d2014-04-14 15:12:11 -070062 str.clear();
Alexander Afanasyevaf283d82014-01-03 13:23:34 -080063}
64
65/**
66 * Modify str in place to erase whitespace on the right.
67 * @param str
68 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070069inline void
Alexander Afanasyevaf283d82014-01-03 13:23:34 -080070trimRight(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 Afanasyevfdbfc6d2014-04-14 15:12:11 -070086inline void
Alexander Afanasyevaf283d82014-01-03 13:23:34 -080087trim(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 Afanasyevfdbfc6d2014-04-14 15:12:11 -070096 * @return
Alexander Afanasyevaf283d82014-01-03 13:23:34 -080097 */
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070098inline int
Alexander Afanasyevaf283d82014-01-03 13:23:34 -080099fromHexChar(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 Afanasyevfdbfc6d2014-04-14 15:12:11 -0700115inline std::string
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800116unescape(const std::string& str)
117{
118 std::ostringstream result;
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700119
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800120 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 Afanasyevfdbfc6d2014-04-14 15:12:11 -0700124
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800125 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 Afanasyevfdbfc6d2014-04-14 15:12:11 -0700130
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800131 // Skip ahead past the escaped value.
132 i += 2;
133 }
134 else
135 // Just copy through.
136 result << str[i];
137 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700138
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800139 return result.str();
140}
141
142} // namespace ndn
143
144#endif // NDN_STRING_HELPER_HPP