blob: 9e2329c72c3d7667357cd281ff9627c992c30f12 [file] [log] [blame]
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
/**
* Copyright (C) 2013 Regents of the University of California.
* @author: Jeff Thompson <jefft0@remap.ucla.edu>
* See COPYING for copyright and distribution information.
*/
#ifndef NDN_STRING_HELPER_HPP
#define NDN_STRING_HELPER_HPP
#include <string>
#include <sstream>
namespace ndn {
static const char *WHITESPACE_CHARS = " \n\r\t";
/**
* Return the hex representation of the bytes in array.
* @param array The array of bytes.
* @return Hex string.
*/
inline std::string
toHex(const uint8_t* array, size_t arraySize)
{
if (array == 0 || arraySize == 0)
return "";
std::ostringstream result;
result.flags(std::ios::hex | std::ios::uppercase);
for (size_t i = 0; i < arraySize; ++i) {
uint8_t x = array[i];
if (x < 16)
result << '0';
result << (unsigned int)x;
}
return result.str();
}
/**
* Modify str in place to erase whitespace on the left.
* @param str
*/
inline void
trimLeft(std::string& str)
{
size_t found = str.find_first_not_of(WHITESPACE_CHARS);
if (found != std::string::npos) {
if (found > 0)
str.erase(0, found);
}
else
// All whitespace
str.clear();
}
/**
* Modify str in place to erase whitespace on the right.
* @param str
*/
inline void
trimRight(std::string& str)
{
size_t found = str.find_last_not_of(WHITESPACE_CHARS);
if (found != std::string::npos) {
if (found + 1 < str.size())
str.erase(found + 1);
}
else
// All whitespace
str.clear();
}
/**
* Modify str in place to erase whitespace on the left and right.
* @param str
*/
inline void
trim(std::string& str)
{
trimLeft(str);
trimRight(str);
}
/**
* Convert the hex character to an integer from 0 to 15, or -1 if not a hex character.
* @param c
* @return
*/
inline int
fromHexChar(uint8_t c)
{
if (c >= '0' && c <= '9')
return (int)c - (int)'0';
else if (c >= 'A' && c <= 'F')
return (int)c - (int)'A' + 10;
else if (c >= 'a' && c <= 'f')
return (int)c - (int)'a' + 10;
else
return -1;
}
/**
* Return a copy of str, converting each escaped "%XX" to the char value.
* @param str
*/
inline std::string
unescape(const std::string& str)
{
std::ostringstream result;
for (size_t i = 0; i < str.size(); ++i) {
if (str[i] == '%' && i + 2 < str.size()) {
int hi = fromHexChar(str[i + 1]);
int lo = fromHexChar(str[i + 2]);
if (hi < 0 || lo < 0)
// Invalid hex characters, so just keep the escaped string.
result << str[i] << str[i + 1] << str[i + 2];
else
result << (uint8_t)(16 * hi + lo);
// Skip ahead past the escaped value.
i += 2;
}
else
// Just copy through.
result << str[i];
}
return result.str();
}
} // namespace ndn
#endif // NDN_STRING_HELPER_HPP