00001
00020 #ifndef COIL_STRINGUTIL_H
00021 #define COIL_STRINGUTIL_H
00022
00023 #include <string>
00024 #include <vector>
00025 #include <sstream>
00026
00027 #if defined (_MSC_VER) && (_MSC_VER <=1500) // VC2008(VC9.0) or before
00028 #else
00029 #include <stdint.h>
00030 #endif
00031
00032 #if defined(Cygwin) && ( __GNUC__ < 4 )
00033 namespace std
00034 {
00035 typedef basic_string<wchar_t> wstring;
00036 }
00037 #endif
00038
00039 namespace coil
00040 {
00041 typedef std::vector<std::string> vstring;
00042
00062 std::wstring string2wstring(std::string str);
00063
00083 std::string wstring2string(std::wstring wstr);
00084
00102 void toUpper(std::string& str);
00103
00121 void toLower(std::string& str);
00122
00149 int getlinePortable(std::istream& istr, std::string& line);
00150
00174 bool isEscaped(const std::string& str, std::string::size_type pos);
00175
00208 std::string escape(const std::string str);
00209
00246 std::string unescape(const std::string str);
00247
00267 void eraseBlank(std::string& str);
00268
00288 void eraseHeadBlank(std::string& str);
00289
00310 void eraseTailBlank(std::string& str);
00311
00332 void eraseBothEndsBlank(std::string& str);
00333
00355 std::string normalize(std::string& str);
00356
00378 unsigned int replaceString(std::string& str, const std::string from,
00379 const std::string to);
00380
00404 vstring split(const std::string& input,
00405 const std::string& delimiter,
00406 bool ignore_empty = false);
00407
00436 bool toBool(std::string str, std::string yes, std::string no,
00437 bool default_value = true);
00461 bool includes(const vstring& list, std::string value,
00462 bool ignore_case = true);
00463
00487 bool includes(const std::string& list, std::string value,
00488 bool ignore_case = true);
00489
00519 bool isAbsolutePath(const std::string& str);
00520
00546 bool isURL(const std::string& str);
00547
00569 template <class Printable>
00570 std::string otos(Printable n)
00571 {
00572 std::stringstream str_stream;
00573 str_stream << n;
00574 return str_stream.str();
00575 };
00576
00600 template <typename To>
00601 bool stringTo(To& val, const char* str)
00602 {
00603 if (str == 0) { return false; }
00604
00605 std::stringstream s;
00606 if ((s << str).fail()) { return false; }
00607 if ((s >> val).fail()) { return false; }
00608 return true;
00609 }
00610
00634 template<>
00635 bool stringTo<std::string>(std::string& val, const char* str);
00636
00669 template <>
00670 bool stringTo<bool>(bool& val, const char* str);
00671
00695 template<class T>
00696 std::string ptrToHex(T* n)
00697 {
00698 std::stringstream ss;
00699 ss << std::hex << std::showbase;
00700 ss << reinterpret_cast<uintptr_t>(n);
00701 return ss.str();
00702 };
00703
00728 template <class T>
00729 bool hexToPtr(T*& ptr, const std::string str)
00730 {
00731 std::stringstream s;
00732 if ((s << std::hex << str).fail()) { return false; }
00733 uintptr_t intval;
00734 if ((s >> intval).fail()) { return false; }
00735 ptr = reinterpret_cast<T*>(intval);
00736 if (ptr == NULL) { return false; }
00737 return true;
00738 }
00739
00762 vstring unique_sv(vstring sv);
00763
00790 std::string flatten(vstring sv, std::string delimiter = ", ");
00791
00815 char** toArgv(const vstring& args);
00816
00817
00839 std::string sprintf(char const * __restrict fmt, ...);
00840
00841 };
00842 #endif // COIL_STRINGUTIL_H