diff --git a/include/znc/ZNCString.h b/include/znc/ZNCString.h index 88ef0d5d..03fd91bb 100644 --- a/include/znc/ZNCString.h +++ b/include/znc/ZNCString.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2004-2014 ZNC, see the NOTICE file for details. + * Copyright (C) 2004-2013 ZNC, see the NOTICE file for details. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,6 +22,7 @@ #include #include #include +#include #include #define _SQL(s) CString("'" + CString(s).Escape_n(CString::ESQL) + "'") @@ -94,7 +95,41 @@ public: CString(const std::string& s) : std::string(s) {} CString(size_t n, char c) : std::string(n, c) {} ~CString() {} - + + /** + * Casts a CString to another type. Implemented via std::stringstream, you use this + * for any class that has an operator<<(std::ostream, YourClass). + * @param target The object to cast into. If the cast fails, its state is unspecified. + * @return True if the cast succeeds, and false if it fails. + */ + template bool Convert(T *target) const + { + std::stringstream ss(*this); + ss >> *target; + return (bool) ss; // we don't care why it failed, only whether it failed + } + + /** + * Joins a collection of objects together, using 'this' as a delimiter. + * You can pass either pointers to arrays, or iterators to collections. + * @param i_begin An iterator pointing to the beginning of a group of objects. + * @param i_end An iterator pointing past the end of a group of objects. + * @return The joined string + */ + template CString Join(Iterator i_start, const Iterator &i_end) const + { + if (i_start == i_end) return CString(""); + std::ostringstream output; + output << *i_start; + while (true) + { + ++i_start; + if (i_start == i_end) return CString(output.str()); + output << *this; + output << *i_start; + } + } + /** * Compare this string caselessly to some other string. * @param s The string to compare to.