00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 inline
00024 std::string StringUtils::ToLower(const std::string &s) {
00025 std::string t;
00026
00027 t.reserve(s.size());
00028
00029 for(std::string::const_iterator i = s.begin(), e = s.end(); i != e; ++i)
00030 t.push_back(tolower(*i));
00031
00032 return t;
00033 }
00034
00035 inline
00036 std::string StringUtils::ToUpper(const std::string &s) {
00037 std::string t;
00038
00039 t.reserve(s.size());
00040
00041 for(std::string::const_iterator i = s.begin(), e = s.end(); i != e; ++i)
00042 t.push_back(toupper(*i));
00043
00044 return t;
00045 }
00046
00047 inline
00048 int StringUtils::CompareNoCase(const std::string &s1, const std::string &s2) {
00049 std::string::const_iterator p1 = s1.begin();
00050 std::string::const_iterator p2 = s2.begin();
00051
00052 while(p1 != s1.end() && p2 != s2.end()) {
00053 if(toupper(*p1) != toupper(*p2))
00054 return (toupper(*p1) < toupper(*p2)) ? -1 : 1;
00055
00056 ++p1;
00057 ++p2;
00058 }
00059
00060 return (s1.size() == s2.size()) ? 0
00061 : (s1.size() < s2.size()) ? -1 : 1;
00062 }
00063
00064 template<typename T> inline
00065 T StringUtils::FromString(const std::string &s) {
00066 stringstream ss;
00067 T temp;
00068
00069 ss << s;
00070 ss >> temp;
00071
00072 if(ss.fail())
00073 throw ConversionErrorException();
00074
00075 return temp;
00076 }
00077
00078 template<typename T> inline
00079 std::string StringUtils::ToString(const T &t) {
00080 stringstream ss;
00081
00082 ss << t;
00083
00084 return ss.str();
00085 }
00086
00087 inline
00088 char *StringUtils::Replicate(const char *s) {
00089 if(!s)
00090 return 0;
00091
00092 char *t = new char[strlen(s) + 1];
00093
00094 return t ? strcpy(t, s) : 0;
00095 }
00096
00097 inline
00098 std::string StringUtils::GetPath(const std::string &s) {
00099 const std::string::size_type i = s.find_last_of("\\/");
00100
00101 return (i == std::string::npos) ? "./" :
00102 s.substr(0, i + 1);
00103 }
00104
00105 inline
00106 std::string StringUtils::GetFilename(const std::string &s) {
00107 const std::string::size_type i = s.find_last_of("\\/");
00108
00109 return (i == std::string::npos) ? s :
00110 s.substr(i + 1, s.length() - i - 1);
00111 }
00112
00113 inline
00114 std::string StringUtils::GetExtension(const std::string &s) {
00115 const std::string::size_type i = s.find_last_of("\\/.");
00116
00117 if(i == std::string::npos)
00118 return std::string();
00119
00120 return (s[i] == '.') ? s.substr(i + 1, s.length() - i - 1) : "";
00121 }
00122
00123 inline
00124 std::string StringUtils::RemoveExtension(const std::string &s) {
00125 const std::string::size_type i = s.find_last_of("\\/.");
00126
00127 if(i == std::string::npos || s[i] != '.')
00128 return s;
00129
00130 return s.substr(0, i);
00131 }