📄 wycseg.cpp
字号:
/* Copyright is licensed by GNU LGPL. by I.J.Wang 2004*/#define WYLIB_SOURCE#include "wycseg.h"const WyCSeg::value_type wy__default_seg[]= { WyCSeg::value_type(0), WyCSeg::value_type(1) };const WyCSeg::value_type* WyCSeg::DefaultAddr=wy__default_seg;bool WyCSeg::find(size_type* res,size_type idx,const WyCSeg& cs) const WY__TSPC(){ const size_type csize( cs.size() ); signed int llen( (signed int)(_pz-_pa)-((signed int)idx-(signed int)csize) ); const value_type* ptr( _pa+idx ); const value_type* cbgn( cs.begin() ); for( ; llen>0; --llen,++ptr) { if(std::memcmp(ptr,cbgn,csize)==0) { if(csize==0) { return(false); // memcmp find "" } if(res!=NULL) { *res=ptr-_pa; } return(true); } } return(false);};bool WyCSeg::rfind(size_type* res,size_type idx,const WyCSeg& cs) const WY__TSPC(){ const size_type csize( cs.size() ); if(csize<=0) { return(false); // note: memcmp indicates "" is found } signed int fidx= (signed int)(_pz-_pa)-(signed int)csize; if(fidx>(signed int)idx) { fidx=idx; } // note: fidx may be negative const char* const cs_bgn( cs.begin() ); for( ; fidx>=0; --fidx) { if(std::memcmp(_pa+fidx,cs_bgn,csize)==0) { if(res!=NULL) { *res=fidx; } return(true); } } return(false);};bool WyCSeg::findc(size_type* res,size_type idx,value_type ch) const WY__TSPC(){ const size_type msize(_pz-_pa); if(idx>=msize) { return(false); } const value_type * const ptr( static_cast<const value_type*>( std::memchr(_pa+idx,(unsigned char)ch,msize)) ); if(ptr==NULL) { return(false); } if(res!=NULL) { *res=ptr-_pa; } return(true);};bool WyCSeg::findc_not(size_type* res,size_type idx,value_type ch) const WY__TSPC(){ if(idx>=size_type(_pz-_pa)) { return false; } for(const value_type *ptr(_pa+idx) ; ptr<_pz; ++ptr) { if(*ptr!=ch) { if(res!=NULL) { *res=ptr-_pa; } return(true); } } return(false);};bool WyCSeg::findc_not(size_type* res,size_type idx,const WyCSeg& cs) const WY__TSPC(){ if(idx>=size_type(_pz-_pa)) { return false; } const value_type *cs_begin(cs.begin()); const size_type cs_size(cs.size()); for(const value_type *ptr(_pa+idx) ; ptr<_pz; ++ptr) { if(std::memchr(cs_begin,*(unsigned char*)ptr,cs_size)==NULL) { if(res!=NULL) { *res=ptr-_pa; } return(true); } } return(false);};bool WyCSeg::findc(size_type* res,size_type idx,const WyCSeg& cs) const WY__TSPC(){ if(idx>=size_type(_pz-_pa)) { return false; } const value_type *cs_begin(cs.begin()); const size_type cs_size(cs.size()); for(const value_type *ptr(_pa+idx) ; ptr<_pz; ++ptr) { if(std::memchr(cs_begin,*(unsigned char*)ptr,cs_size)!=NULL) { if(res!=NULL) { *res=ptr-_pa; } return(true); } } return(false);};bool WyCSeg::rfindc(size_type* res,size_type idx,value_type ch) const WY__TSPC(){ const size_type msize( _pz-_pa ); if(msize==0) { return(false); } const value_type * const ptr( static_cast<const value_type*> ( memrchr(_pa,(unsigned char)ch, (idx>=msize)? msize: idx+1)) ); if(ptr==NULL) { return(false); } if(res!=NULL) { *res=ptr-_pa; } return(true);};bool WyCSeg::rfindc_not(size_type* res,size_type idx,value_type ch) const WY__TSPC(){ const value_type* ptr; if(idx>=size_type(_pz-_pa)) { if(_pz==_pa) { return(false); } ptr=_pz-1; } else { ptr=_pa+idx; } for(; ptr>=_pa; --ptr) { if(*ptr!=ch) { if(res!=NULL) { *res=ptr-_pa; } return(true); } } return(false);};bool WyCSeg::rfindc_not(size_type* res,size_type idx, const WyCSeg& cs) const WY__TSPC(){ const value_type* ptr; if(idx>=size_type(_pz-_pa)) { if(_pz==_pa) { return(false); } ptr=_pz-1; } else { ptr=_pa+idx; } const value_type *cs_begin( cs.begin() ); const size_type cs_size( cs.size() ); for(; ptr>=_pa; --ptr) { if(std::memchr(cs_begin,*(unsigned char*)ptr,cs_size)==NULL) { if(res!=NULL) { *res=ptr-_pa; } return(true); } } return(false);};bool WyCSeg::rfindc(size_type* res,size_type idx, const WyCSeg& cs) const WY__TSPC(){ const value_type *ptr; if(idx>=size_type(_pz-_pa)) { if(_pz==_pa) { return(false); } ptr=_pz-1; } else { ptr=_pa+idx; } const value_type *cs_begin( cs.begin() ); const size_type cs_size( cs.size() ); for(; ptr>=_pa; --ptr) { if(std::memchr(cs_begin,*(unsigned char*)ptr,cs_size)!=NULL) { if(res!=NULL) { *res=ptr-_pa; } return(true); } } return(false);};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -