FirteX-高性能全文索引和检索平台

API Documentation


首页 | 名字空间列表 | 类继承关系 | 组合类型列表 | $(BL\录(B | 文件列表 | 名字空间成员 | 组合类型成员 | 文件成员

FXString.h

浏览该文件的文档。
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 _FXSTRING_H
00015 #define _FXSTRING_H
00016 
00017 #include "typedefs.h"
00018 
00019 #if _MSC_VER > 1000
00020 #pragma once
00021 #endif // _MSC_VER > 1000
00022 
00023 #include <string>
00024 
00025 using namespace firtex;
00026 
00027 namespace firtex
00028 {
00029         namespace utility
00030         {
00031                 template <typename CharT>
00032                 class CFXString_base
00033                 {
00034                 public:
00035                         typedef  CharT char_type;
00036                 public:
00037                         CFXString_base(void);
00038                         CFXString_base(const char_type* str);                                                           //will copy the string
00039                         CFXString_base(char_type* str,bool bNoCopy);                                            //will or will NOT copy the string,depends on bNoCopy
00040                         CFXString_base(const char_type* str,size_t len);                                        //will copy the string
00041                         CFXString_base(char_type* str,size_t len,bool bNoCopy);                         //will or will NOT copy the string,depends on bNoCopy
00042                         CFXString_base(const _str<char_type>& str);                                                     //will copy the string
00043                         CFXString_base(const _str<char_type>& str,bool bNoCopy);                        //will or will NOT copy the string,depends on bNoCopy
00044                         CFXString_base(const basic_string<char_type>& str);                                     //will copy the string
00045                         CFXString_base(const CFXString_base& src);                                                      
00046                         ~CFXString_base(void);                  
00047                 public:                 
00048                         CFXString_base& operator=(const _str<char_type>& str);                          //will copy the string
00049                         CFXString_base& operator=(const char_type* str);                                        //will copy the string
00050                         CFXString_base& operator=(const CFXString_base<char_type>& str);                //will copy the string
00051                         CFXString_base& operator=(const basic_string<char_type>& str);          //will copy the string
00052 
00053                         operator char_type*();
00054                         operator _str<char_type>();
00055                         operator basic_string<char_type>();
00056 
00057                         bool operator==(const char_type* str)const;                                             
00058                         bool operator==(const basic_string<char_type>& str)const;
00059                         bool operator==(const CFXString_base<char_type>& right)const;
00060 
00061                         bool operator!=(const char_type* str)const;
00062                         bool operator!=(const basic_string<char_type>& str)const;
00063                         bool operator!=(const CFXString_base<char_type>& right)const;
00064                 public:
00068                         const char_type*        c_str()const;
00069 
00073                         char_type*      data();
00074 
00078                         size_t          length()const;
00079 
00083                         size_t          maxLength()const{return m_maxLen;}
00084 
00089                         void            reserve(size_t len);
00090 
00095                         void            resize(size_t len);
00096 
00103                         void            assign(char_type* data,size_t len,bool bNoCopy);
00104 
00105                         void            assign (const char_type* str);
00106                         void            assign (const char_type* str,size_t len);
00107                         void            assign (const _str<char_type>& str);
00108                 public:
00109                         void            append(char_type* str,size_t len);
00110                 public:
00111                         int     compare(const CFXString_base<char_type>& right)const;
00112                         int     compare(const char_type* str)const;
00113                         int     compare(const basic_string<char_type>& str)const;
00114                         int     compare(const _str<char_type>& str)const;
00115 
00116                         void    clear();
00117 
00118                 protected:              
00119                         inline void     init();
00120                 private:
00121                         _str<char_type> m_str;
00122                         size_t                  m_maxLen;
00123                         bool                    m_bOwner;
00124                 };
00125 
00127                 //Inline Functions
00128                 template <class CharT>
00129                         inline CFXString_base<CharT>::CFXString_base(void)
00130                 {
00131                         init();
00132                 }
00133                 template <class CharT>
00134                         inline CFXString_base<CharT>::CFXString_base(const CharT* str)
00135                 {
00136                         init();
00137                         assign(str);
00138                 }
00139                 template <class CharT>
00140                         inline CFXString_base<CharT>::CFXString_base(CharT* str,bool bNoCopy)
00141                 {
00142                         if(bNoCopy)
00143                         {
00144                                 m_str.str = str;
00145                                 m_maxLen = m_str.length = (sizeof(CharT)==sizeof(char))?strlen(str):wcslen(str);                                 
00146                                 m_bOwner = false;
00147                         }
00148                         else 
00149                         {
00150                                 init();
00151                                 assign(str);
00152                         }
00153                         
00154                 }
00155                 template <class CharT>
00156                         inline CFXString_base<CharT>::CFXString_base(const CharT* str,size_t len)
00157                 {                       
00158                         init();
00159                         assign(str,len);
00160                 }
00161                 template <class CharT>
00162                         inline CFXString_base<CharT>::CFXString_base(CharT* str,size_t len,bool bNoCopy)
00163                 {
00164                         init();
00165                         if(bNoCopy)
00166                         {
00167                                 m_str.str = str;
00168                                 m_maxLen = m_str.length = len;                           
00169                                 m_bOwner = false;
00170                         }
00171                         else assign(str,len);
00172                 }
00173                 template <class CharT>
00174                         inline CFXString_base<CharT>::CFXString_base(const basic_string<CharT>& str)
00175                 {
00176                         init();
00177                         assign(str.c_str(),str.length());                       
00178                 }
00179                 template <class CharT>
00180                         inline CFXString_base<CharT>::CFXString_base(const CFXString_base& src)
00181                 {
00182                         init();
00183                         if(src.m_bOwner)
00184                                 assign(src.m_str.str,src.m_str.length);
00185                         else 
00186                         {
00187                                 m_str = src.m_str;                              
00188                         }
00189                         m_maxLen = src.m_maxLen;
00190                 }
00191                 template <class CharT>
00192                         inline CFXString_base<CharT>::~CFXString_base(void)
00193                 {                       
00194                         clear();                        
00195                 }
00196                 template <class CharT>
00197                         inline void CFXString_base<CharT>::init()
00198                 {
00199                         m_str.str = NULL;
00200                         m_str.length = 0;
00201                         m_bOwner = false;
00202                         m_maxLen = 0;
00203                 }
00204 
00205                 template<class CharT>
00206                         inline void CFXString_base<CharT>::clear()
00207                 {
00208                         if(m_bOwner)
00209                         {
00210                                 if(m_str.str)
00211                                         delete[] m_str.str;
00212                         }
00213                         m_str.str = NULL;
00214                         m_str.length = 0;
00215                         m_maxLen = 0;
00216                         m_bOwner = false;
00217                 }
00218 
00219                 template <>
00220                         inline void CFXString_base<char>::assign (const char* str)
00221                 {                       
00222                         m_str.length = strlen(str);
00223                         if(!m_bOwner)
00224                         {
00225                                 m_maxLen = m_str.length;
00226                                 m_str.str = new char[m_str.length + 1];
00227                         }
00228                         else 
00229                         {
00230                                 if((m_str.length > m_maxLen))
00231                                 {
00232                                         if(m_str.str)
00233                                                 delete[] m_str.str;
00234                                         m_str.str = new char[m_str.length + 1];
00235                                         m_maxLen = m_str.length;
00236                                 }
00237                         }                       
00238                         strcpy(m_str.str,str);
00239                         m_bOwner = true;
00240                 }
00241                 template <>
00242                         inline void CFXString_base<wchar_t>::assign (const wchar_t* str)
00243                 {
00244                         m_str.length = wcslen(str);
00245                         if(!m_bOwner)
00246                         {
00247                                 m_maxLen = m_str.length;
00248                                 m_str.str = new wchar_t[m_str.length + 1];
00249                         }
00250                         else 
00251                         {
00252                                 if((m_str.length > m_maxLen))
00253                                 {
00254                                         if(m_str.str)
00255                                                 delete[] m_str.str;
00256                                         m_str.str = new wchar_t[m_str.length + 1];
00257                                         m_maxLen = m_str.length;
00258                                 }
00259                         }                       
00260                         wcscpy(m_str.str,str);
00261                         m_bOwner = true;
00262                 }
00263 
00264                 template <>
00265                         inline void CFXString_base<char>::assign (const char* str,size_t len)
00266                 {                       
00267                         if(len == 0)
00268                                 len = strlen(str);
00269                         m_str.length = len;
00270                         if(!m_bOwner)
00271                         {
00272                                 m_maxLen = len;
00273                                 m_str.str = new char[len + 1];
00274                         }
00275                         else 
00276                         {
00277                                 if((len > m_maxLen))
00278                                 {
00279                                         if(m_str.str)
00280                                                 delete[] m_str.str;
00281                                         m_str.str = new char[len + 1];
00282                                         m_maxLen = len;
00283                                 }
00284                         }                       
00285                         strncpy(m_str.str,str,len);
00286                         m_str.str[len] = 0;
00287                         m_bOwner = true;
00288                         
00289                 }
00290                 template <>
00291                         inline void CFXString_base<wchar_t>::assign (const wchar_t* str,size_t len)
00292                 {
00293                         if(len == 0)
00294                                 len = wcslen(str);
00295                         m_str.length = len;
00296                         if(!m_bOwner)
00297                         {
00298                                 m_maxLen = len;
00299                                 m_str.str = new wchar_t[len + 1];
00300                         }
00301                         else 
00302                         {
00303                                 if((len > m_maxLen))
00304                                 {
00305                                         if(m_str.str)
00306                                                 delete[] m_str.str;
00307                                         m_str.str = new wchar_t[len + 1];
00308                                         m_maxLen = len;
00309                                 }
00310                         }                       
00311                         wcsncpy(m_str.str,str,len);
00312                         m_str.str[len] = 0;
00313                         m_bOwner = true;
00314                 }
00315 
00316                 template <class CharT>
00317                         inline CFXString_base<CharT>& CFXString_base<CharT>::operator=(const _str<CharT>& str)
00318                 {
00319                         assign(str.str,str.length);                     
00320                         return (*this);
00321                 }
00322                 template <class CharT>
00323                         inline CFXString_base<CharT>& CFXString_base<CharT>::operator=(const CharT* str)
00324                 {
00325                         assign(str);
00326                         return (*this);
00327                 }
00328                 template <class CharT>
00329                         inline CFXString_base<CharT>& CFXString_base<CharT>::operator=(const CFXString_base<CharT>& str)
00330                 {
00331             assign(str.m_str.str,str.m_str.length);                     
00332                         m_maxLen = str.m_maxLen;
00333                         
00334                         return (*this);
00335                 }
00336                 template <class CharT>
00337                         inline CFXString_base<CharT>& CFXString_base<CharT>::operator=(const basic_string<CharT>& str)
00338                 {
00339                         assign(str.c_str(),str.length());                       
00340                         return (*this);
00341                 }
00342                 template <class CharT>
00343                         inline CFXString_base<CharT>::operator CharT*()
00344                 {
00345                         return m_str.str;
00346                 }
00347                 template <class CharT>
00348                         inline CFXString_base<CharT>::operator _str<CharT>()
00349                 {
00350                         return m_str;
00351                 }
00352                 template <class CharT>
00353                         inline CFXString_base<CharT>::operator basic_string<CharT>()
00354                 {
00355                         basic_string<CharT> s;
00356                         s.append(m_str.str,m_str.length);
00357                         return s;
00358                 }
00359 
00360 
00361                 template <class CharT>
00362                         inline void CFXString_base<CharT>::assign(CharT* data,size_t len,bool bNoCopy)
00363                 {
00364                         m_str.length = len;
00365                         if(bNoCopy)
00366                         {
00367                                 if(m_bOwner)
00368                                         delete[] m_str.str;
00369                                 m_str.str = data;
00370                                 m_bOwner = false;
00371                         }
00372                         else 
00373                         {
00374                                 if(!m_bOwner)
00375                     m_str.str = new CharT[len + 1];
00376                                 else 
00377                                 {
00378                                         if((len > m_maxLen))
00379                                         {
00380                                                 if(m_str.str)
00381                                                         delete[] m_str.str;
00382                                                 m_str.str = new CharT[len + 1];
00383                                                 m_maxLen = len;
00384                                         }
00385                                 }
00386                                 memcpy(m_str.str,data,len*sizeof(CharT));
00387                                 m_str.str[len] = 0;
00388                                 m_bOwner = true;
00389                         }
00390                 }
00391 
00392                 template <class CharT>
00393                         inline bool CFXString_base<CharT>::operator==(const CharT* str)const
00394                 {
00395                         return (compare(str)==0);
00396                 }
00397                 template <class CharT>
00398                         inline bool CFXString_base<CharT>::operator==(const basic_string<CharT>& str)const
00399                 {
00400                         return (compare(str)==0);
00401                 }
00402                 template <class CharT>
00403                         inline bool CFXString_base<CharT>::operator==(const CFXString_base<CharT>& right)const
00404                 {
00405                         return (compare(right)==0);
00406                 }
00407 
00408                 template <class CharT>
00409                         inline bool CFXString_base<CharT>::operator!=(const CharT* str)const
00410                 {
00411                         return (compare(str) != 0);
00412                 }
00413                 template <class CharT>
00414                         inline bool CFXString_base<CharT>::operator!=(const basic_string<CharT>& str)const
00415                 {
00416                         return (compare(str) != 0);
00417                 }
00418                 template <class CharT>
00419                         inline bool CFXString_base<CharT>::operator!=(const CFXString_base<CharT>& right)const
00420                 {
00421                         return (compare(right)!=0);
00422                 }
00423 
00424                 template<>
00425                         inline int CFXString_base<char>::compare(const CFXString_base<char>& right)const
00426                 {
00427                         if(m_str.length == 0 || right.m_str.length == 0)
00428                         {
00429                                 return (int)(m_str.length - right.m_str.length);
00430                         }
00431                         int ret = strncmp(m_str.str,right.m_str.str,(m_str.length > right.m_str.length)?right.m_str.length:m_str.length);
00432                         if (ret != 0) return ret;
00433                         else return (int)(m_str.length - right.m_str.length);
00434                 }
00435 
00436                 template<>
00437                         inline int CFXString_base<wchar_t>::compare(const CFXString_base<wchar_t>& right)const
00438                 {
00439                         if(m_str.length == 0 || right.m_str.length == 0)
00440                         {
00441                                 return (int)(m_str.length - right.m_str.length);
00442                         }
00443                         int ret = wcsncmp(m_str.str,right.m_str.str,(m_str.length > right.m_str.length)?right.m_str.length:m_str.length);
00444                         if (ret != 0) return ret;
00445                         else return (int)(m_str.length - right.m_str.length);
00446                 }
00447 
00448                 template<>
00449                         inline int CFXString_base<char>::compare(const char* str)const
00450                 {
00451                         size_t rlen = strlen(str);
00452                         if(m_str.length == 0 || rlen == 0)
00453                         {
00454                                 return (int)(m_str.length - rlen);
00455                         }
00456                         int ret = strncmp(m_str.str,str,(m_str.length > rlen)?rlen:m_str.length);
00457                         if (ret != 0) return ret;
00458                         else return (int)(m_str.length - rlen);
00459                 }
00460 
00461                 template<>
00462                         inline int CFXString_base<wchar_t>::compare(const wchar_t* str)const
00463                 {
00464                         size_t rlen = wcslen(str);
00465                         if(m_str.length == 0 || rlen == 0)
00466                         {
00467                                 return (int)(m_str.length - rlen);
00468                         }
00469                         int ret = wcsncmp(m_str.str,str,(m_str.length > rlen)?rlen:m_str.length);
00470                         if (ret != 0) return ret;
00471                         else return (int)(m_str.length - rlen);
00472                 }
00473                 template<>
00474                         inline int CFXString_base<char>::compare(const basic_string<char>& str)const
00475                 {
00476                         size_t rlen = str.length();
00477                         if(m_str.length == 0 || rlen == 0)
00478                         {
00479                                 return (int)(m_str.length - rlen);
00480                         }
00481                         int ret = strncmp(m_str.str,str.c_str(),(m_str.length > rlen)?rlen:m_str.length);
00482                         if (ret != 0) return ret;
00483                         else return (int)(m_str.length - rlen);
00484                 }
00485                 template<>
00486                         inline int CFXString_base<wchar_t>::compare(const basic_string<wchar_t>& str)const
00487                 {
00488                         size_t rlen = str.length();
00489                         if(m_str.length == 0 || rlen == 0)
00490                         {
00491                                 return (int)(m_str.length - rlen);
00492                         }
00493                         int ret = wcsncmp(m_str.str,str.c_str(),(m_str.length > rlen)?rlen:m_str.length);
00494                         if (ret != 0) return ret;
00495                         else return (int)(m_str.length - rlen);
00496                 }
00497                 template<>
00498                         inline int CFXString_base<wchar_t>::compare(const _str<wchar_t>& str)const
00499                 {
00500                         size_t rlen = str.length;
00501                         if(m_str.length == 0 || rlen == 0)
00502                         {
00503                                 return (int)(m_str.length - rlen);
00504                         }
00505                         int ret = wcsncmp(m_str.str,str.str,(m_str.length > rlen)?rlen:m_str.length);
00506                         if (ret != 0) return ret;
00507                         else return (int)(m_str.length - rlen);
00508                 }
00509                 template <class CharT>
00510                         inline const CharT* CFXString_base<CharT>::c_str()const
00511                 {
00512                         return m_str.str;
00513                 }
00514 
00515                 template <class CharT>
00516                         inline CharT* CFXString_base<CharT>::data()
00517                 {
00518                         return m_str.str;
00519                 }
00520 
00521                 template <class CharT>
00522                         inline size_t CFXString_base<CharT>::length()const
00523                 {
00524                         return m_str.length;
00525                 }
00526                 
00527                 template <class CharT>
00528                         void CFXString_base<CharT>::reserve(size_t len)
00529                 {
00530                         if(len > m_maxLen)
00531                         {                               
00532                                 CharT* tmp = new CharT[len + 1];
00533                                 memset(m_str.str,0,sizeof(CharT)*(len+1));
00534                                 if(m_str.length > 0)
00535                                 {
00536                                         memcpy(tmp,m_str.str,(m_str.length>len)?len:m_str.length);                                      
00537                                 }
00538                                 if(m_bOwner && m_str.str)
00539                                         delete[] m_str.str;
00540                                 m_str.str = tmp;
00541                                 m_str.length = len;
00542                                 m_bOwner = true;
00543                                 m_maxLen = len;
00544                         }                       
00545                 }
00546 
00547                 template <class CharT>
00548                         void CFXString_base<CharT>::resize(size_t len)
00549                 {
00550                         if(len > m_maxLen)
00551                         {
00552                                 if(m_bOwner)
00553                                         reserve(len);
00554                                 else 
00555                                         FIRTEX_THROW3(BAD_PARAMETER_ERROR,"void CFXString_base<CharT>::resize(size_t len):can't resize string,maybe the string is not owned by CFXString.");
00556                         }
00557                         m_str.str[len] = 0;
00558                         m_str.length = len;
00559                 }
00560                 template <class CharT>
00561                         void CFXString_base<CharT>::append(CharT* str,size_t len)
00562                 {
00563                         if((m_str.length + len) > m_maxLen)
00564                         {
00565                                 if(m_bOwner)
00566                                         reserve(m_str.length + len);
00567                                 else 
00568                                         FIRTEX_THROW3(BAD_PARAMETER_ERROR,"void CFXString_base<CharT>::append(CharT* str,size_t len):can't append string,maybe the string is not owned by CFXString.");
00569                         }
00570                         memcpy(m_str.str+m_str.length,str,len*sizeof(CharT));
00571                         m_str.length += len;
00572                         m_str.str[m_str.length] = 0;
00573                 }
00574                 
00575         
00576                 typedef CFXString_base<char>    CFXStringA;
00577                 typedef CFXString_base<wchar_t> CFXStringW;
00578                  
00579 #ifdef _UNICODE
00580                 typedef CFXStringW CFXString;
00581 #else
00582                 typedef CFXStringA CFXString;
00583 #endif
00584                 
00585         }
00586 }
00587 
00588 #endif

http://www.firtex.org http://www.sourceforge.net/projects/firtex