FirteX-高性能全文索引和检索平台API Documentation |
00001 // 00002 // Copyright(C) 2005--2006 Institute of Computing Tech, Chinese Academy of Sciences. 00003 // All rights reserved. 00004 // This file is part of FirteX (www.firtex.org) 00005 // 00006 // Use of the FirteX is subject to the terms of the software license set forth in 00007 // the LICENSE file included with this software, and also available at 00008 // http://www.firtex.org/license.html 00009 // 00010 // Author : 郭瑞杰(GuoRuijie) 00011 // Email : ruijieguo@software.ict.ac.cn,ruijieguo@gmail.com 00012 // Created : 2006/5/20 00013 // 00014 #ifndef _STRINGUTILS_H 00015 #define _STRINGUTILS_H 00016 00017 #include <vector> 00018 #include <string> 00019 #include <cctype> 00020 00021 using namespace std; 00022 00023 namespace firtex 00024 { 00025 namespace string_utils 00026 { 00027 template<typename TString, typename TSep> 00028 std::vector<TString> split(const TString& szText, TSep Separator = " ") 00029 { 00030 std::vector<TString> vec; 00031 TString str(szText); 00032 TString sep(Separator); 00033 size_t n = 0, nOld=0; 00034 while (n != TString::npos) 00035 { 00036 n = str.find(sep,n); 00037 if (n != TString::npos) 00038 { 00039 if (n != nOld) 00040 vec.push_back(str.substr(nOld, n-nOld)); 00041 n += sep.length(); 00042 nOld = n; 00043 } 00044 } 00045 vec.push_back(str.substr(nOld, str.length()-nOld)); 00046 00047 return vec; 00048 } 00049 00050 template<typename TString> 00051 std::vector<TString> split(const TString& szText, char Separator = ' ') 00052 { 00053 TString sep(" "); 00054 sep[0] = Separator; 00055 return split<TString, TString>(szText, sep); 00056 } 00057 00058 template<typename TString> 00059 std::vector<TString> split(const TString& szText, wchar_t Separator = ' ') 00060 { 00061 TString sep(" "); 00062 sep[0] = Separator; 00063 return split<TString, TString>(szText, sep); 00064 } 00065 00066 template <typename TCont, typename TGlue> 00067 TCont join(const TCont& container, const TGlue& glue) 00068 { 00069 TCont szResp = container; 00070 szResp += glue; 00071 return szResp; 00072 /*TCont szResp(""); 00073 TCont::const_iterator iter; 00074 size_t s = container.size(); 00075 for (iter = container.begin(); iter != container.end(); iter++) 00076 { 00077 szResp += (*iter); 00078 if (--s) 00079 szResp += glue; 00080 } 00081 00082 return szResp;*/ 00083 } 00084 00085 template <typename TCont> 00086 TCont join(const TCont& container) 00087 { 00088 return join(container, ' '); 00089 } 00090 00091 template <typename TString, typename TSearch, typename TReplace> 00092 TString replace(const TString& baseStr, const TSearch& searchStr, const TReplace& replaceStr) 00093 { 00094 TString szResp(baseStr); 00095 TString szSearch(searchStr); 00096 TString szReplace(replaceStr); 00097 if (searchStr == replaceStr) 00098 return szResp; 00099 00100 size_t searchSize = szSearch.size(); 00101 size_t replaceSize = szReplace.size(); 00102 size_t n = 0; 00103 00104 while (n != TString::npos) 00105 { 00106 n = szResp.find(szSearch, n); 00107 if (n != TString::npos) 00108 { 00109 szResp.replace(n, searchSize, szReplace); 00110 n += replaceSize; 00111 } 00112 } 00113 return szResp; 00114 } 00115 00116 00117 template <typename TString, typename TSearch,typename CharT> 00118 TString replace(const TString& baseStr, const TSearch& searchStr, CharT replaceChar) 00119 { 00120 TString stdReplaceStr(" "); 00121 stdReplaceStr[0] = replaceChar; 00122 return replace(baseStr, searchStr, stdReplaceStr); 00123 } 00124 00125 template <typename TString, typename TReplace,typename CharT> 00126 TString replace(const TString& baseStr, CharT searchChar, const TReplace& replaceStr) 00127 { 00128 TString stdSearchStr(" "); 00129 stdSearchStr[0] = searchChar; 00130 return replace(baseStr, stdSearchStr, replaceStr); 00131 } 00132 00133 template <typename TString,typename CharT> 00134 TString replace(const TString& baseStr, CharT searchChar, CharT replaceChar) 00135 { 00136 TString stdSearchStr(" "); 00137 TString stdReplaceStr(" "); 00138 stdSearchStr[0] = searchChar; 00139 stdReplaceStr[0] = replaceChar; 00140 return replace(baseStr, stdSearchStr, stdReplaceStr); 00141 } 00142 00143 template <typename TString> 00144 TString properCase(const TString& str) 00145 { 00146 TString szResp(str); 00147 szResp[0] = std::toupper(szResp[0]); 00148 for (size_t i = 1; i < szResp.size(); i++) 00149 if (std::isspace(szResp[i-1])) 00150 szResp[i] = std::toupper(szResp[i]); 00151 else 00152 szResp[i] = std::tolower(szResp[i]); 00153 00154 return szResp; 00155 } 00156 00157 00158 template <typename TString> 00159 TString toUpper(const TString& str) 00160 { 00161 TString szResp(str); 00162 std::transform(szResp.begin(),szResp.end(), szResp.begin(), (int(*)(int))std::toupper); 00163 00164 return szResp; 00165 } 00166 template <typename TString> 00167 TString toLower(const TString& str) 00168 { 00169 TString szResp(str); 00170 std::transform(szResp.begin(),szResp.end(), szResp.begin(), (int(*)(int))std::tolower); 00171 00172 return szResp; 00173 } 00174 00175 /*template <typename TString> 00176 void trimleft( TString& s ) 00177 { 00178 TString::iterator it; 00179 00180 for( it = s.begin(); it != s.end(); it++ ) 00181 if( !std::isspace(*it)) 00182 break; 00183 00184 s.erase( s.begin(), it ); 00185 } 00186 00187 template <typename TString> 00188 void trimright( TString& s ) 00189 { 00190 TString::difference_type dt; 00191 TString::reverse_iterator it; 00192 00193 for( it = s.rbegin(); it != s.rend(); it++ ) 00194 if( !std::isspace( *it ) ) 00195 break; 00196 00197 dt = s.rend() - it; 00198 00199 s.erase( s.begin() + dt, s.end() ); 00200 } 00201 00202 template <typename TString> 00203 void trim( TString& s ) 00204 { 00205 trimleft( s ); 00206 trimright( s ); 00207 } 00208 00209 template <typename TString> 00210 TString trim( const TString& s ) 00211 { 00212 TString t = s; 00213 trim( t ); 00214 return t; 00215 }*/ 00216 00217 void trimleft(string& s ); 00218 00219 void trimright( string& s ); 00220 00221 void trim( string& s ); 00222 inline string trim( const string& s ); 00223 00224 void trimleft(wstring& s ); 00225 00226 void trimright( wstring& s ); 00227 00228 void trim( wstring& s ); 00229 00230 wstring trim( const wstring& s ); 00231 } 00232 } 00233 00234 #endif
http://www.firtex.org http://www.sourceforge.net/projects/firtex