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