name: Converting Name to TLV

Change-Id: Idc44608d3f0610f7f6b07204a00a3510e8041393
diff --git a/src/util/string-helper.hpp b/src/util/string-helper.hpp
new file mode 100644
index 0000000..ee3f5bd
--- /dev/null
+++ b/src/util/string-helper.hpp
@@ -0,0 +1,114 @@
+/* -*- 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 {
+
+const char *WHITESPACE_CHARS = " \n\r\t";
+
+/**
+ * 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