+
+ /** Deletes a object and zeroes the memory location that pointed to it.
+ * @param pr A reference to the pointer that contains the object to delete.
+ */
+ template<typename T>
+ void delete_zero(T*& pr)
+ {
+ T* p = pr;
+ pr = NULL;
+ delete p;
+ }
+
+ /**
+ * Remove an element from a container
+ * @param cont Container to remove the element from
+ * @param val Value of the element to look for and remove
+ * @return True if the element was found and removed, false otherwise
+ */
+ template <template<typename, typename> class Cont, typename T, typename Alloc>
+ inline bool erase(Cont<T, Alloc>& cont, const T& val)
+ {
+ const typename Cont<T, Alloc>::iterator it = std::find(cont.begin(), cont.end(), val);
+ if (it != cont.end())
+ {
+ cont.erase(it);
+ return true;
+ }
+ return false;
+ }
+
+ /**
+ * Check if an element with the given value is in a container. Equivalent to (std::find(cont.begin(), cont.end(), val) != cont.end()).
+ * @param cont Container to find the element in
+ * @param val Value of the element to look for
+ * @return True if the element was found in the container, false otherwise
+ */
+ template <template<typename, typename> class Cont, typename T, typename Alloc>
+ inline bool isin(const Cont<T, Alloc>& cont, const T& val)
+ {
+ return (std::find(cont.begin(), cont.end(), val) != cont.end());
+ }
+
+ namespace string
+ {
+ /**
+ * Escape a string
+ * @param str String to escape
+ * @param out Output, must not be the same string as str
+ */
+ template <char from, char to, char esc>
+ inline void escape(const std::string& str, std::string& out)
+ {
+ for (std::string::const_iterator i = str.begin(); i != str.end(); ++i)
+ {
+ char c = *i;
+ if (c == esc)
+ out.append(2, esc);
+ else
+ {
+ if (c == from)
+ {
+ out.push_back(esc);
+ c = to;
+ }
+ out.push_back(c);
+ }
+ }
+ }
+
+ /**
+ * Escape a string using the backslash character as the escape character
+ * @param str String to escape
+ * @param out Output, must not be the same string as str
+ */
+ template <char from, char to>
+ inline void escape(const std::string& str, std::string& out)
+ {
+ escape<from, to, '\\'>(str, out);
+ }
+
+ /**
+ * Unescape a string
+ * @param str String to unescape
+ * @param out Output, must not be the same string as str
+ * @return True if the string was unescaped, false if an invalid escape sequence is present in the input in which case out will contain a partially unescaped string
+ */
+ template<char from, char to, char esc>
+ inline bool unescape(const std::string& str, std::string& out)
+ {
+ for (std::string::const_iterator i = str.begin(); i != str.end(); ++i)
+ {
+ char c = *i;
+ if (c == '\\')
+ {
+ ++i;
+ if (i == str.end())
+ return false;
+
+ char nextc = *i;
+ if (nextc == esc)
+ c = esc;
+ else if (nextc != to)
+ return false; // Invalid escape sequence
+ else
+ c = from;
+ }
+ out.push_back(c);
+ }
+ return true;
+ }
+
+ /**
+ * Unescape a string using the backslash character as the escape character
+ * @param str String to unescape
+ * @param out Output, must not be the same string as str
+ * @return True if the string was unescaped, false if an invalid escape sequence is present in the input in which case out will contain a partially unescaped string
+ */
+ template <char from, char to>
+ inline bool unescape(const std::string& str, std::string& out)
+ {
+ return unescape<from, to, '\\'>(str, out);
+ }
+ }