add latin-1 wrapper for html_entities
[libi2ncommon] / src / stringfunc.hxx
index 407b726..06d85f9 100644 (file)
@@ -34,7 +34,11 @@ on this file might be covered by the GNU General Public License.
 #ifndef __STRINGFUNC_HXX
 #define __STRINGFUNC_HXX
 
+#include <stdio.h>
 #include <list>
+#include <numeric>
+#include <set>
+#include <vector>
 #include <string>
 #include <sstream>
 #include <stdexcept>
@@ -113,6 +117,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",
@@ -120,13 +132,52 @@ std::list< std::string > split_string(
    const std::string& trim_list= std::string()
 );
 
+struct concatenator {
+    std::string delim;
+
+    concatenator (const std::string &delim) : delim (delim) { }
+
+    inline std::string operator() (const std::string &acc, const std::string &elt) const
+    { return acc + delim + elt; }
+};
+
+template<typename Iter>
+std::string
+join_string (
+    Iter first,
+    Iter last,
+    const std::string &delimiter = "\n"
+)
+{
+    if (first == last) { return ""; }
+
+    const std::string &init = *first++;
+    if (first == last) { return init; }
+
+    return std::accumulate (first, last, init, concatenator (delimiter));
+}
+
+/**
+ * @brief joins a container of strings into a single string.
+ *
+ * This funtion is (basically) the reverse operation of @a split_string.
+ *
+ * @param parts         the container of strings.
+ * @param delimiter     the delimiter to insert between the strings.
+ * @return              the joined string.
+ */
+template<typename Cont>
+inline std::string join_string(
+   const Cont& parts,
+   const std::string& delimiter = "\n"
+)
+{ return join_string (parts.begin (), parts.end (), delimiter); }
 
 std::string join_string(
-   const std::list< std::string >& parts,
+   const char *const parts [],
    const std::string& delimiter = "\n"
 );
 
-
 /*
 ** conversions:
 */
@@ -137,7 +188,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 +196,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 +237,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.
@@ -198,6 +293,20 @@ std::string to_string(const T& v)
 } // eo to_string(const T&)
 
 
+/**
+ * Create a string with types shortened in texts describing C++ types
+ *
+ * for example: std::list<some_long_type, std::allocator<some_long_type> >
+ * -->  std::list<some_long_type, _alloc_>
+ *
+ * and std::basic_string<char, std::char_traits<char>, std::allocator<char> >
+ * --> std::string
+ */
+std::string shorten_stl_types(const std::string &input);
+
+std::string base64_encode(const std::string &input, bool one_line=true);
+std::string base64_decode(const std::string &input, bool one_line=true);
+
 } // eo namespace I2n
 
 
@@ -249,6 +358,16 @@ std::string smart_html_entities(const std::string &input);
 std::string html_entities(std::string str);
 std::string html_entities_to_console(std::string str);
 
+inline std::string html_entities_iso (const std::string &str)
+{
+    return html_entities (iso_to_utf8 (str));
+}
+
+typedef std::pair<std::string::size_type, std::string::size_type> CommentZone;
+std::vector<CommentZone> find_html_comments(const std::string &str);
+void remove_html_comments(std::string &str);
+void remove_html_comments(std::string &str, const std::vector<CommentZone> &comments);
+
 std::string sanitize_for_logging(const std::string &str, const char replace_with='?');
 
 std::string escape(const std::string &s);