00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #ifndef _MIMETIC_STRINGUTILS_H_
00017 #define _MIMETIC_STRINGUTILS_H_
00018 #include <string>
00019 #include <iostream>
00020 #include <algorithm>
00021
00022 namespace mimetic
00023 {
00024
00025 extern const std::string nullstring;
00026
00027 struct ichar_traits : public std::char_traits<char>
00028 {
00029 static bool eq (const char_type & c1, const char_type& c2)
00030 { return (toupper(c1) == toupper(c2)); }
00031 static bool ne (const char_type& c1, const char_type& c2)
00032 { return (toupper(c1) != toupper(c2)); }
00033 static bool lt (const char_type& c1, const char_type& c2)
00034 { return (toupper(c1) < toupper(c2)); }
00035 static int compare (const char_type* s1, const char_type* s2, size_t n)
00036 {
00037 for(size_t i=0; i < n; ++i)
00038 if(toupper(s1[i]) != toupper(s2[i]))
00039 return (toupper(s1[i]) < toupper(s2[i])) ?-1: 1;
00040 return 0;
00041 }
00042 static const char* find( const char* s, int n, char a )
00043 {
00044 while( n-- > 0 && tolower(*s) != tolower(a) )
00045 ++s;
00046 return s;
00047 }
00048 };
00049
00050
00051 using std::string;
00052
00053 struct istring: public string
00054 {
00055 istring()
00056 {}
00057
00058 istring(const std::string& right)
00059 : string(right)
00060 {}
00061 explicit istring(const allocator_type& al)
00062 : string(al)
00063 {}
00064 istring(const istring& right)
00065 : string(right)
00066 {}
00067 istring(const istring& right, size_type roff, size_type count = npos)
00068 : string(right, roff, count)
00069 {}
00070 istring(const istring& right, size_type roff, size_type count,
00071 const allocator_type& al)
00072 : string(right, roff, count, al)
00073 {}
00074 istring(const value_type *ptr, size_type count)
00075 : string(ptr, count)
00076 {}
00077 istring(const value_type *ptr, size_type count,const allocator_type& al)
00078 : string(ptr, count, al)
00079 {}
00080 istring(const value_type *ptr)
00081 : string(ptr)
00082 {}
00083 istring(const value_type *ptr,const allocator_type& al)
00084 : string(ptr, al)
00085 {}
00086 istring(size_type count, value_type ch)
00087 : string(count,ch)
00088 {}
00089 istring(size_type count, value_type ch,const allocator_type& al)
00090 : string(count,ch,al)
00091 {}
00092 template <class InIt>
00093 istring(InIt first, InIt last)
00094 : string(first, last)
00095 {}
00096 template <class InIt>
00097 istring(InIt first, InIt last,const allocator_type& al)
00098 : string(first, last, al)
00099 {}
00100 };
00101
00102
00103 inline bool operator==(const istring& is, const std::string& s)
00104 {
00105 return (0 == ichar_traits::compare(is.c_str(),s.c_str(),
00106 std::max(is.length(),s.length())) );
00107 }
00108
00109 inline bool operator!=(const istring& is, const std::string& s)
00110 {
00111 return (0 != ichar_traits::compare(is.c_str(),s.c_str(),
00112 std::max(is.length(),s.length())) );
00113 }
00114
00115 inline bool operator!=(const istring& is, const char* str)
00116 {
00117 return (0 != ichar_traits::compare(is.c_str(),str,
00118 std::max(is.length(),::strlen(str))) );
00119 }
00120
00121 inline bool operator==(const istring& is, const char* str)
00122 {
00123 return (0 == ichar_traits::compare(is.c_str(),str,
00124 std::max(is.length(),::strlen(str))) );
00125 }
00126
00127 inline std::string dquoted(const std::string& s)
00128 {
00129 return "\"" + s + "\"";
00130 }
00131
00132 inline std::string parenthed(const std::string& s)
00133 {
00134 return "(" + s + ")";
00135 }
00136
00137
00138 inline std::string remove_dquote(const std::string& s)
00139 {
00140 int len = s.length();
00141 if( len < 2)
00142 return s;
00143 if(s[0] == '"' && s[len-1] == '"')
00144 return std::string(s, 1, len-2);
00145 return s;
00146 }
00147
00148
00149
00150
00151
00152 std::string canonical(const std::string& s, bool no_ws = false);
00153
00154
00155 inline std::string remove_external_blanks(const std::string& in)
00156 {
00157 if(!in.length())
00158 return in;
00159 std::string s = in;
00160 int beg = 0, end = s.length();
00161 for(; beg < end; ++beg)
00162 if(s[beg] != ' ' && s[beg] != '\t')
00163 break;
00164 end = s.length() - 1;
00165 for(; end > beg; --end)
00166 if(s[end] != ' ' && s[end] != '\t')
00167 break;
00168 s.assign(std::string(s, beg, end - beg + 1));
00169 return s;
00170 }
00171
00172 }
00173
00174 #endif
00175