Create vector-result-version of split_string with unit test
[libi2ncommon] / src / stringfunc.hxx
index 407b726..0d9e363 100644 (file)
@@ -35,6 +35,7 @@ on this file might be covered by the GNU General Public License.
 #define __STRINGFUNC_HXX
 
 #include <list>
+#include <vector>
 #include <string>
 #include <sstream>
 #include <stdexcept>
@@ -113,6 +114,14 @@ void split_string(
    const std::string& trim_list= std::string()
 );
 
+void split_string(
+   const std::string& str,
+   std::vector< std::string >& result,
+   const std::string& delimiter= "\n",
+   bool omit_empty= false,
+   const std::string& trim_list= std::string()
+);
+
 std::list< std::string > split_string(
    const std::string& str,
    const std::string& delimiter = "\n",
@@ -126,6 +135,11 @@ std::string join_string(
    const std::string& delimiter = "\n"
 );
 
+std::string join_string(
+   const std::vector< std::string >& parts,
+   const std::string& delimiter = "\n"
+);
+
 
 /*
 ** conversions:
@@ -137,7 +151,6 @@ std::string convert_binary_to_hex(const std::string&str, bool upper_case_digits=
 std::string convert_hex_to_binary(const std::string& str) throw(std::runtime_error);
 
 
-
 /*
 ** "type conversions":
 */
@@ -146,6 +159,11 @@ std::string convert_hex_to_binary(const std::string& str) throw(std::runtime_err
 /**
  * convert a datatype @a T to a string via string stream.
  *
+ * This will not report trouble in conversion; for example:
+ *     string_to<int>("christian")
+ * will return 0 and not throw an error.
+ * Use boost::lexical_cast<T>(string) to get error-checked results.
+ *
  * @param s the string which should be converted to @a T.
  * @return the value of type T.
  */
@@ -182,6 +200,46 @@ bool string_to(const std::string& s, T& result)
 
 
 /**
+ * convert string in hexadecimal notation to a datatype @a T
+ * supports strings with and without "0x" notation, e.g. 0xff and FF are both valid
+ * 
+ * @param s the hex string which should be converted to @a T.
+ * @return the value of type T.
+ */
+template<
+class T
+>
+T hex_string_to(const std::string& s)
+{
+   std::istringstream istr(s);
+   T result;
+   istr >> std::hex >> result;
+   return result;
+} // eo string_to(const std::string&)
+
+
+/**
+ * convert string in hexadecimal notation to a datatype @a T
+ * supports strings with and without "0x" notation, e.g. 0xff and FF are both valid
+ *
+ * @param s the hex string which should be converted to @a T.
+ * @param result the resulting value of type @a T.
+ * @return @a true iff the internal string stream was EOF after the conversion.
+ *
+ * @attention: does not return if the conversion was successful. So check for empty strings before.
+ */
+template<
+class T
+>
+bool hex_string_to(const std::string& s, T& result)
+{
+   std::istringstream istr(s);
+   istr >> std::hex >> result;
+   return istr.eof();
+} // eo string_to(const std::string&)
+
+
+/**
  * convert a string to another datatype @a T via string stream.
  *
  * @param v the value (of type @a T) which should be converted to a string.