StringUtil.cpp
Go to the documentation of this file.00001 #include <cctype>
00002 #include "StringUtil.h"
00003
00004
00005
00006 namespace StringUtil
00007 {
00008
00009
00010 bool IsPrefix(const std::string & str, const std::string & prefix)
00011 {
00012 #if (__GNUC__ > 2)
00013 return (str.length() >= prefix.length() &&
00014 str.compare(0, prefix.length(), prefix) == 0);
00015 #else
00016 return (str.length() >= prefix.length() &&
00017 str.compare(prefix, 0, prefix.length()) == 0);
00018 #endif
00019 }
00020
00021
00022 bool IsSuffix(const std::string & str, const std::string & suffix)
00023 {
00024 #if (__GNUC__ > 2)
00025 return (str.length() >= suffix.length() &&
00026 str.compare(str.length() - suffix.length(), suffix.length(), suffix) == 0);
00027 #else
00028 return (str.length() >= suffix.length() &&
00029 str.compare(suffix, str.length() - suffix.length(), suffix.length()) == 0);
00030 #endif
00031 }
00032
00033
00034 void ToLower(std::string & str)
00035 {
00036 ToLower(str.begin(), str.end());
00037 }
00038
00039 void ToLower(std::string::iterator start, std::string::iterator end)
00040 {
00041 std::string::iterator p = start;
00042 for (;p != end; ++p)
00043 {
00044 *p = tolower(*p);
00045 }
00046 }
00047
00048 std::string ToLowerCopy (const std::string & str)
00049 {
00050 std::string local(str);
00051 ToLower(local);
00052 return local;
00053 }
00054
00055 void Trim(std::string & str)
00056 {
00057 if (str.empty())
00058 return;
00059
00060 int count = 0;
00061 std::string::size_type pos = 0;
00062 while (pos < str.length() && isspace(str[pos]))
00063 {
00064 ++count;
00065 ++pos;
00066 }
00067
00068 if (0 < count)
00069 str.erase(0, count);
00070
00071 if (str.empty())
00072 return;
00073
00074 count = 0;
00075 pos = str.length() - 1;
00076 while (isspace(str[pos]))
00077 {
00078 ++count;
00079 if (pos == 0)
00080 break;
00081 else
00082 --pos;
00083 }
00084
00085 if (0 < count)
00086 str.erase(str.length() - count);
00087 }
00088
00089 std::string TrimCopy (const std::string & str)
00090 {
00091 std::string local(str);
00092 Trim(local);
00093 return local;
00094 }
00095
00096
00097
00098 void EncodeToXml (std::string & str)
00099 {
00100 str = EncodeToXmlCopy(str);
00101 return;
00102 }
00103
00104 std::string EncodeToXmlCopy (const std::string & str)
00105 {
00106 std::string convertedString;
00107
00108 int length = str.length();
00109 for(int i=0;i<length;i++)
00110 {
00111 switch(str.at(i))
00112 {
00113 case '&':
00114 convertedString.append("&");
00115 break;
00116 case '\'':
00117 convertedString.append("'");
00118 break;
00119 case '\"':
00120 convertedString.append(""");
00121 break;
00122 case '<':
00123 convertedString.append("<");
00124 break;
00125 case '>':
00126 convertedString.append(">");
00127 break;
00128 default:
00129 convertedString.push_back(str.at(i));
00130 }
00131 }
00132 return convertedString;
00133
00134 }
00135
00136 }
00137
00138
00139
00140