📄 wystr.cpp
字号:
return(*this);};const WyStr& WyStr::operator =(const WyCSeg& rhs) WY__TSPC(Reply){ const WyRet r(_imp->set_string(rhs)); if(r==Ok) { return(*this); } if(r!=Wym_EFBIG) { WY_THROW( Reply(r) ); } Wy__HStr* hptr(NULL); try { hptr= Wy__HStr::alloc(rhs); } catch(const Wy__HStr::Reply& e) { WY_THROW( Reply(e) ); }; Wy__HStr::free(_imp); _imp=hptr; return(*this);};void WyStr::swap(WyStr& str) WY__TSPC(){ Wy__Base::vswap(_imp,str._imp);};WyRet WyStr::insert(size_type idx,const WyStr& str) WY__TSPC(){ const WyRet r(_imp->insert(idx,*str._imp)); if(r==Ok) { return(Ok); } if(r!=Wym_EFBIG) { WY_RETURN(r); } Wy__HStr* hptr(NULL); try { hptr= Wy__HStr::alloc(_imp->cseg(0,idx),str._imp->cseg(),_imp->cseg(idx)); } catch(const Wy__HStr::Reply& e) { WY_RETURN( WyRet(e) ); }; Wy__HStr::free(_imp); _imp=hptr; return(Ok);};WyRet WyStr::insert(size_type idx,const WyCSeg& cseg) WY__TSPC(){ const WyRet r(_imp->insert(idx,cseg)); if(r==Ok) { return(Ok); } if(r!=Wym_EFBIG) { WY_RETURN(r); } Wy__HStr* hptr(NULL); try { hptr= Wy__HStr::alloc(_imp->cseg(0,idx),cseg,_imp->cseg(idx)); } catch(const Wy__HStr::Reply& e) { WY_RETURN( WyRet(e) ); }; Wy__HStr::free(_imp); _imp=hptr; return(Ok);};WyRet WyStr::insert(size_type idx,size_type cnum,value_type ch) WY__TSPC(){ const WyRet r(_imp->insert(idx,cnum,ch)); if(r==Ok) { return(Ok); } if(r!=Wym_EFBIG) { WY_RETURN(r); } Wy__HStr* hptr(NULL); try { hptr= Wy__HStr::alloc(_imp->cseg(0,idx),cnum,ch,_imp->cseg(idx)); } catch(const Wy__HStr::Reply& e) { WY_RETURN( WyRet(e) ); }; Wy__HStr::free(_imp); _imp=hptr; return(Ok);};WyRet WyStr::erase(size_type idx) WY__TSPC(){ WY_RETURN( _imp->erase(idx) );};WyRet WyStr::erase(size_type idx,size_type len) WY__TSPC(){ WY_RETURN( _imp->erase(idx,len) );};WyRet WyStr::resize(size_type num) WY__TSPC(){ // Note: Asserted no error if num<_capacity() const WyRet r(_imp->resize(num)); if(r==Ok) { return(Ok); } if(r!=Wym_EFBIG) { WY_RETURN(r); } Wy__HStr* hptr(NULL); try { hptr= Wy__HStr::alloc(_imp->cseg(),num-_imp->size(),value_type(0)); } catch(const Wy__HStr::Reply& e) { WY_RETURN( WyRet(e) ); }; Wy__HStr::free(_imp); _imp=hptr; return(Ok);};WyRet WyStr::resize(size_type num,value_type ch) WY__TSPC(){ // Note: Asserted no error if num<_capacity() const WyRet r(_imp->resize(num,ch)); if(r==Ok) { return(Ok); } if(r!=Wym_EFBIG) { WY_RETURN(r); } Wy__HStr* hptr(NULL); try { hptr= Wy__HStr::alloc(_imp->cseg(),num-_imp->size(),ch); } catch(const Wy__HStr::Reply& e) { WY_RETURN( WyRet(e) ); }; Wy__HStr::free(_imp); _imp=hptr; return(Ok);};WyRet WyStr::replace(size_type idx,size_type num,const WyStr& str) WY__TSPC(){ const WyRet r(_imp->replace(idx,num,*str._imp)); if(r==Ok) { return(Ok); } if(r!=Wym_EFBIG) { WY_RETURN(r); } Wy__HStr* hptr(NULL); try { hptr= Wy__HStr::alloc(_imp->cseg(0,idx),str.cseg(), _imp->cseg(idx+_imp->cseg(idx,num).size())); } catch(const Wy__HStr::Reply& e) { WY_RETURN( WyRet(e) ); }; Wy__HStr::free(_imp); _imp=hptr; return(Ok);};WyRet WyStr::replace(size_type idx,size_type num,const WyCSeg& cseg) WY__TSPC(){ const WyRet r(_imp->replace(idx,num,cseg)); if(r==Ok) { return(Ok); } if(r!=Wym_EFBIG) { WY_RETURN(r); } Wy__HStr* hptr(NULL); try { hptr= Wy__HStr::alloc(_imp->cseg(0,idx),cseg, _imp->cseg(idx+_imp->cseg(idx,num).size())); } catch(const Wy__HStr::Reply& e) { WY_RETURN( WyRet(e) ); }; Wy__HStr::free(_imp); _imp=hptr; return(Ok);};WyRet WyStr::replace(size_type idx,size_type num,size_type cnum,value_type ch) WY__TSPC(){ const WyRet r(_imp->replace(idx,num,cnum,ch)); if(r==Ok) { return(Ok); } if(r!=Wym_EFBIG) { WY_RETURN(r); } Wy__HStr* hptr(NULL); try { hptr= Wy__HStr::alloc(_imp->cseg(0,idx),cnum,ch, _imp->cseg(idx+_imp->cseg(idx,num).size())); } catch(const Wy__HStr::Reply& e) { WY_RETURN( WyRet(e) ); }; Wy__HStr::free(_imp); _imp=hptr; return(Ok);};WyStr& WyStr::_fill_end(size_t wlen, char ch) WY__TSPC(WyStr::Reply){ if(wlen<=this->size()) { return(*this); } const WyRet r( this->append(wlen-this->size(),ch) ); if(r!=Ok) { WY_THROW( WyStr::Reply(r) ); } return(*this);};WyStr& WyStr::_fill_begin(size_t wlen, char ch) WY__TSPC(WyStr::Reply){ if(wlen<=this->size()) { return(*this); } const WyRet r( this->insert(0,wlen-this->size(),ch) ); if(r!=Ok) { WY_THROW( WyStr::Reply(r) ); } return(*this);};int WyStr::_strcmp(const WyStr& str) const WY__TSPC(){ return _imp->_strcmp(*str._imp);};int WyStr::_strcmp(const WyCSeg& cseg) const WY__TSPC(){ return _imp->_strcmp(cseg);};int WyStr::_strcmp(const char* cstr) const WY__TSPC(WyCSeg::Reply){ return _imp->_strcmp( WyCSeg(cstr) );};WyStr::size_type WyStr::_capacity(void) const WY__TSPC(){ return(_imp->_capacity());};WyRet WyStr::copyto(size_type* rlen,value_type* buf,size_type blen,size_type idx) const WY__TSPC(){ WY_RETURN( _imp->copyto(rlen,buf,blen,idx) );};WyRet WyStr::_reserve(size_type num) WY__TSPC(){ if(num<=_capacity()) { return(Ok); } Wy__HStr* hptr(NULL); try { hptr= Wy__HStr::alloc(num); } catch(const Wy__HStr::Reply& e) { WY_RETURN( WyRet(e) ); }; const WyRet r( hptr->set_string(*_imp) ); if(r!=Ok) { // for now, this version of set_string only fails on Wym_ELOOP // it should always succeed. Wy__HStr::free(hptr); #ifdef WY_DEBUG WY_TERMINATE(""); #else WY_RETURN(Wym_ENOMEM); // Hide bug as normal failure #endif } Wy__HStr::free(_imp); _imp=hptr; return(Ok);};WyRet WyStr::_setsize(size_t nsize) WY__TSPC(){ return _imp->_setsize(nsize);};WyStr operator +(const WyStr& lhs,const WyStr& rhs) WY__TSPC(WyStr::Reply)try { return WyStr( Wy__HStr::alloc(lhs.cseg(),rhs.cseg()) );}catch(const Wy__HStr::Reply& e) { WY_THROW( WyStr::Reply(e) );};WyStr operator +(const WyStr& lhs,const WyCSeg& rhs) WY__TSPC(WyStr::Reply)try { return WyStr( Wy__HStr::alloc(lhs.cseg(),rhs) );}catch(const Wy__HStr::Reply& e) { WY_THROW( WyStr::Reply(e) );};WyStr operator +(const WyStr& str,WyStr::value_type ch) WY__TSPC(WyStr::Reply)try { return WyStr( Wy__HStr::alloc(str.cseg(),ch) );}catch(const Wy__HStr::Reply& e) { WY_THROW( WyStr::Reply(e) );};WyStr operator +(const WyCSeg& cs,const WyStr& str) WY__TSPC(WyStr::Reply)try { return WyStr( Wy__HStr::alloc(cs,str.cseg()) );}catch(const Wy__HStr::Reply& e) { WY_THROW( WyStr::Reply(e) );};WyStr operator +(WyStr::value_type ch,const WyStr& str) WY__TSPC(WyStr::Reply)try { return WyStr( Wy__HStr::alloc(ch,str.cseg()) );}catch(const Wy__HStr::Reply& e) { WY_THROW( WyStr::Reply(e) );};//----------------------------------------------------------------// [Syn] Make string representation of the given argument file_name/line_num.//// [Ret] WyStr, string representation of the arguments// in the form: file_name '(' line_num ')'//typedef int wy__line_num_type;static WyStr mkstr_sli(const char *file_name,wy__line_num_type line_num) WY__TSPC(WyStr::Reply)try { WyStr nstr; if(file_name!=NULL) {#ifndef WY_SHORT_SLI // personal use for my application nstr=file_name;#else // Sample the first MaxFlen characters of file_name or until the dot // character as the source file name information. // const int MaxFlen=8; int flen; for(flen=0; flen<=MaxFlen; ++flen) { const char ch=file_name[flen]; if((ch==0)||(ch=='.')) { nstr.reset(WyCSeg(file_name,flen)); break; } }#endif };#ifndef WY_SHORT_SLI nstr+='(';#endif // Convert line_num number // char dbuf[2+3*sizeof(wy__line_num_type)]; std::div_t dt; int n; if(line_num<0) { dt.quot=-line_num; nstr+='-'; } else { dt.quot=line_num; } n=0; do { dt=std::div(dt.quot,10); dbuf[n++]=(char)(dt.rem); } while(dt.quot>0); for(--n; n>=0; --n) { const char ch=dbuf[n]; nstr+= (ch>9)? char(ch+'7'):char(ch+'0'); }#ifndef WY_SHORT_SLI nstr+=')';#endif return(nstr);}catch(const WyStr::Reply&) { throw;};//---------------------------------------------------------------WyStr& Wy::_toupper(WyStr& str) WY__TSPC(){ const WyStr::value_type *pend( str.data()+str.size() ); for(WyStr::value_type *p=&str[0]; p!=pend; ++p) { if((*p>='a')&&(*p<='z')) { *p-=WyStr::value_type(0x20); } } return str;};WyStr& Wy::_tolower(WyStr& str) WY__TSPC(){ const WyStr::value_type *pend( str.data()+str.size() ); for(WyStr::value_type *p=&str[0]; p!=pend; ++p) { if((*p>='A')&&(*p<='Z')) { *p+=WyStr::value_type(0x20); } } return str;};WyStr Wy::wrd(const WyReply& em) WY__TSPC(WyStr::Reply)try { WyRet r; WyStr str; if((r=str._reserve( em->get_reply(NULL,0) ))!=Ok) { WY_THROW( WyStr::Reply(r) ); } if((r=str._setsize( em->get_reply(&str[0],str._capacity()) ))!=Ok) { WY_THROW(r); } return(str);}catch(const WyStr::Reply& e) { if(e==Wym_ENOMEM) { throw; } WY_TERMINATE("");}; WyStr Wy::wrd(const Wy_SrcLoc& s) WY__TSPC(WyStr::Reply)try { return mkstr_sli(s.src_file(),s.src_line());}catch(const WyStr::Reply& e) { if(e==Wym_ENOMEM) { throw; } WY_TERMINATE("");};WyStr Wy::wrd(const WyRet& arg) WY__TSPC(WyStr::Reply)try {#ifndef WYLIB_CONFIG_NORSLI WyRet r; WyStr str; if((r=str._reserve( arg->get_reply(NULL,0) ))!=Ok) { WY_THROW( WyStr::Reply(r) ); } if((r=str._setsize( arg->get_reply(&str[0],str._capacity()) ))!=Ok) { WY_THROW(r); } if(arg.sli_ptr()!=NULL) { str+=", "; str+=Wy::wrd(*arg.sli_ptr()); } return(str);#else return Wy::wrd( static_cast<WyReply>(arg) );#endif}catch(const WyStr::Reply& e) { throw;};//---------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -