📄 wy__num.h
字号:
} r=Wy::_strnum(res2,&cptr,WyCSeg(cptr,clft),Radix); if(endptr!=NULL) { *endptr=cptr; } fdig=FracPtr-cptr; if(r!=Ok) { WY_RETURN(r); // buf=.ddd??? } return(Ok); // buf=.ddd } if(Wy::_charnum(cptr[-1])<0) { // buf=(+-).X ++cptr; clft=blen-(cptr-buf); if(clft<=0) { if(endptr!=NULL) { *endptr=buf; } WY_RETURN(Wym_ENOENT); // buf="(+-)." } // buf=(+-).xxx const int tmp(Wy::_charnum(*cptr)); if((tmp<0)||(tmp>=Radix)) { if(endptr!=NULL) { *endptr=buf; } WY_RETURN(Wym_ENOENT); // fraction does not start with a digit } r=Wy::_strnum(res2,&cptr,WyCSeg(cptr,clft),Radix); if(res1!=0) { WY_THROW( WyRet() ); // assertion fail } if(buf[0]=='-') { const ResType2 tmp(res2); res2=-res2; if((tmp!=0)&&(res2>tmp)) { cptr=FracPtr+1; clft=blen-(cptr-buf); r=Wy::_strnum(res2,&cptr,WyCSeg(cptr,clft),Radix); if(r!=Ok) { WY_THROW(r); } if(endptr!=NULL) { *endptr=cptr; } fdig=FracPtr-cptr; WY_RETURN(Wym_ERANGE); } } if(endptr!=NULL) { *endptr=cptr; } fdig=FracPtr-cptr; if(r!=Ok) { WY_RETURN(r); } return(Ok); } else { // buf=[+-]ddd.X ++cptr; clft=blen-(cptr-buf); if(clft<=0) { if(endptr!=NULL) { *endptr=cptr; } fdig=FracPtr-cptr; return(Ok); // buf=[+-]ddd." } // buf=[+-]ddd.xxx const int tmp(Wy::_charnum(*cptr)); if((tmp<0)||(tmp>=Radix)) { if(endptr!=NULL) { *endptr=cptr; } WY_RETURN(Wym_EBADMSG); // fraction does not start with a digit } r=Wy::_strnum(res2,&cptr,WyCSeg(cptr,clft),Radix); if(buf[0]=='-') { const ResType2 tmp(res2); res2=-res2; if((tmp!=0)&&(res2>tmp)) { cptr=FracPtr+1; clft=blen-(cptr-buf); r=Wy::_strnum(res2,&cptr,WyCSeg(cptr,clft),Radix); if(r!=Ok) { WY_THROW(r); } if(endptr!=NULL) { *endptr=cptr; } fdig=FracPtr-cptr; WY_RETURN(Wym_ERANGE); } } if(endptr!=NULL) { *endptr=cptr; } fdig=FracPtr-cptr; if(r!=Ok) { WY_RETURN(r); } return(Ok); }}catch(const WyCSeg::Reply& e) { throw WyRet(e);};// [Syn] Convert float number string notation in the form:// S1::= [+-]ddd[(Ee)[+-]ddd]// S2::= [+-].ddd[(Ee)[+-]ddd]// S3::= [+-]ddd.[ggg][(Ee)[+-]ddd]// Conversion rule is the same as for wy__strnum2 except that// this function accept trailing expression "e[+-]ddd" and// the extened number (expontient) is stored into res3//// [Ret] Ok// Wym_EBADMSG// Wym_ERANGE// Wym_ENOENT//template <typename ResType1, typename ResType2>WyRet wy__strnum3(ResType1& res1, ResType2& res2, int& fdig, int& res3,const char** endptr, const char* buf,size_t blen){ const int Radix=10; WyRet r; const char* cptr; size_t clft; res3=0; r=Wy::wy_strnum2(res1,res2,fdig,&cptr,buf,blen); if(r!=Wym_EBADMSG) { if(endptr!=NULL) { *endptr=cptr; } WY_RETURN(r); } clft=blen-(cptr-buf); if(clft<2) { if(endptr!=NULL) { *endptr=cptr; } WY_RETURN(Wym_EBADMSG); } if((*cptr!='e')&&(*cptr!='E')) { if(endptr!=NULL) { *endptr=cptr; } WY_RETURN(Wym_EBADMSG); } const char* const CharE_Ptr(cptr); --clft; ++cptr; r=Wy::_strnum(res3,&cptr,WyCSeg(cptr,clft),Radix); if(r==Wym_ENOENT) { if(endptr!=NULL) { *endptr=CharE_Ptr; } WY_RETURN(Wym_EBADMSG); } if(endptr!=NULL) { *endptr=cptr; } if(r!=Ok) { WY_RETURN(r); } return(Ok);};// [Internal] Convert digit characters in buf[blen] denoting a number// and store result to res//// [Ret] 1. *endptr-buf = number of characters converted// 2. res = stored with the resultant number (0, if no valid digits)// 3. Characters between buf and *endptr are all valid// digits if blen!=0 and return code is Wym_EBADMSG // Ok// Wym_EFAULT buf is zero// Wym_EINVAL radix invalid [0 |2-36]// Wym_ENOENT No number notation recognized// Wym_EBADMSG No digit or invalid digit// Wym_ERANGE Result not representable//template <typename NumType>WyRet wy__reset_num(NumType& res, const char** endptr, const char* buf, size_t blen, const int& radix){ const char* sptr(buf); res=0; if(sptr==0) { if(endptr!=NULL) { *endptr=buf; } WY_RETURN(Wym_EFAULT); } if(blen==0) { if(endptr!=NULL) { *endptr=buf; } WY_RETURN(Wym_ENOENT); } int e_radix=radix; bool is_neg= !std::numeric_limits<NumType>::is_signed; if(std::numeric_limits<NumType>::is_signed) { if(*sptr=='+') { ++sptr; --blen; } else if(*sptr=='-') { ++sptr; --blen; is_neg=true; }; } else { if(*sptr=='+') { ++sptr; --blen; }; } // resolve radix if(radix>36) { if(endptr!=NULL) { *endptr=buf; } WY_RETURN(Wym_EINVAL); } else if(radix<2) { if(radix!=0) { if(endptr!=NULL) { *endptr=buf; } WY_RETURN(Wym_EINVAL); } // assert radix==0 if((blen>=1)&&(sptr[0]=='0')) { if((blen>=2)&&((sptr[1]=='x')||(sptr[1]=='X'))) { if(blen==2) { if(endptr!=NULL) { *endptr=&sptr[1]; } WY_RETURN(Wym_EBADMSG); } const int tmp( Wy::_charnum(sptr[2]) ); if((tmp<0)||(tmp>=16)) { if(endptr!=NULL) { *endptr=&sptr[1]; } WY_RETURN(Wym_EBADMSG); } e_radix=16; sptr+=2; blen-=2; // FALL_THROUGH } else { e_radix=8; } } else { e_radix=10; } } else { if(radix==16) { if(blen>=2) { if((sptr[0]=='0')&&((sptr[1]=='x')||(sptr[1]=='X'))) { if(blen==2) { if(endptr!=NULL) { *endptr=&sptr[1]; } WY_RETURN(Wym_EBADMSG); } const int tmp( Wy::_charnum(sptr[2]) ); if((tmp<0)||(tmp>=16)) { if(endptr!=NULL) { *endptr=&sptr[1]; } WY_RETURN(Wym_EBADMSG); } sptr+=2; blen-=2; // FALL_THROUGH } }; } else { } } if(blen<=0) { if(endptr!=NULL) { *endptr=buf; } WY_RETURN(Wym_ENOENT); } if(std::numeric_limits<NumType>::is_signed) { if(is_neg) { return( wy__strnum_neg(res,endptr,sptr,blen,e_radix) ); } else { return( wy__strnum_pos(res,endptr,sptr,blen,e_radix) ); } } else { return( wy__strnum_pos(res,endptr,sptr,blen,e_radix) ); }};template <typename NumType>WyRet wy__reset_num(NumType& res, const char** endptr, const char* buf, size_t blen, int& radix){ const char* sptr(buf); res=0; if(sptr==0) { if(endptr!=NULL) { *endptr=buf; } WY_RETURN(Wym_EFAULT); } if(blen==0) { if(endptr!=NULL) { *endptr=buf; } WY_RETURN(Wym_ENOENT); } bool is_neg= !std::numeric_limits<NumType>::is_signed; if(std::numeric_limits<NumType>::is_signed) { if(*sptr=='+') { ++sptr; --blen; } else if(*sptr=='-') { ++sptr; --blen; is_neg=true; }; } else { if(*sptr=='+') { ++sptr; --blen; }; } // resolve radix if(radix>36) { if(endptr!=NULL) { *endptr=buf; } WY_RETURN(Wym_EINVAL); } else if(radix<2) { if(radix!=0) { if(endptr!=NULL) { *endptr=buf; } WY_RETURN(Wym_EINVAL); } // assert radix==0 if((blen>=1)&&(sptr[0]=='0')) { if((blen>=2)&&(sptr[1]=='x')||(sptr[1]=='X')) { if(blen==2) { if(endptr!=NULL) { *endptr=&sptr[1]; } WY_RETURN(Wym_EBADMSG); } const int tmp( Wy::_charnum(sptr[2]) ); if((tmp<0)||(tmp>=16)) { if(endptr!=NULL) { *endptr=&sptr[1]; } WY_RETURN(Wym_EBADMSG); } radix=16; sptr+=2; blen-=2; } else if(blen==1) { radix=10; // "0" is radix 10 } else { radix=8; } } else { radix=10; } } else { if(radix==16) { if(blen>=2) { if((sptr[0]=='0')&&((sptr[1]=='x')||(sptr[1]=='X'))) { if(blen==2) { if(endptr!=NULL) { *endptr=&sptr[1]; } WY_RETURN(Wym_EBADMSG); } const int tmp( Wy::_charnum(sptr[2]) ); if((tmp<0)||(tmp>=16)) { if(endptr!=NULL) { *endptr=&sptr[1]; } WY_RETURN(Wym_EBADMSG); } sptr+=2; blen-=2; // FALL_THROUGH } }; } } if(blen<=0) { if(endptr!=NULL) { *endptr=sptr; } WY_RETURN(Wym_ENOENT); } if(std::numeric_limits<NumType>::is_signed) { if(is_neg) { return( wy__strnum_neg(res,endptr,sptr,blen,radix) ); } else { return( wy__strnum_pos(res,endptr,sptr,blen,radix) ); } } else { return( wy__strnum_pos(res,endptr,sptr,blen,radix) ); }};#endif // End of Wy__Num
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -