Files Classes Functions Hierarchy
00001 #ifndef STRINGSPACE 00002 #define STRINGSPACE 00003 00004 #include <cassert> 00005 #include <string> 00006 using namespace std; 00007 00008 #include <typedefs.h> 00009 00013 class myisspace 00014 { 00015 public: 00016 00018 boolc operator ()(charc ch) const; 00019 }; 00020 00024 template< typename SPC=myisspace > 00025 class spacertrim 00026 { 00027 public: 00028 00030 SPC spacer; 00031 00033 void operator () ( string & s ) const; 00034 }; 00035 00037 stringc stringtrim(stringc& str); 00038 00051 template< typename SPC=myisspace > 00052 class spacerdelete 00053 { 00054 public: 00055 00057 SPC spacer; 00058 00061 boolc operator () ( stringc & s ) const; 00062 00064 boolc operator [] (string& str); 00065 00066 }; 00067 00068 //--------------------------------------------------------- 00069 // Implementation. 00070 00071 template< typename SPC > 00072 boolc spacerdelete<SPC>::operator [] (string& str) 00073 { 00074 if (spacerdelete<SPC>()(str)) 00075 { 00076 str=""; 00077 return true; 00078 } 00079 return false; 00080 } 00081 00082 template< typename SPC > 00083 boolc spacerdelete<SPC>::operator () ( stringc & s ) const 00084 { 00085 if (s.empty()) 00086 return true; 00087 uintc sz = s.size(); 00088 uint i=0; 00089 for ( ; i<sz; ++i) 00090 { 00091 if (spacer(s[i])==false) 00092 return false; 00093 } 00094 00095 return true; 00096 } 00097 00098 template< typename SPC > 00099 void spacertrim<SPC>::operator () ( string & s ) const 00100 { 00101 uintc imax=s.size(); 00102 00103 if (imax==0) 00104 return; 00105 00106 uint i(0); 00107 00108 for ( ; i<imax; ++i ) 00109 if (spacer(s[i])==false) 00110 break; 00111 uintc i0(i); 00112 00113 for ( i=imax-1; i>0; --i) 00114 if (spacer(s[i])==false) 00115 break; 00116 uintc i1(i); 00117 00118 // One character in string case. 00119 if (i0==i1) 00120 { 00121 if (spacer(s[i0])) 00122 { 00123 s.clear(); 00124 return; 00125 } 00126 00127 s = s[i0]; 00128 return; 00129 } 00130 00131 if (i0==0) 00132 if (i1==(imax-1)) 00133 return; 00134 00135 s = s.substr(i0,i1-i0+1); 00136 } 00137 00138 ; 00139 00140 #endif 00141 00142
1.5.8