From c3754eee8014e15ab673f999986320058aeb7324 Mon Sep 17 00:00:00 2001 From: Dimitri van Heesch Date: Thu, 18 Feb 2021 22:22:49 +0100 Subject: Refactoring: replace QRegExp by std::regex in vhdldocgen.cpp --- src/util.cpp | 10 +++- src/util.h | 3 +- src/vhdlcode.l | 23 +++---- src/vhdldocgen.cpp | 172 +++++++++++++++++++++-------------------------------- src/vhdldocgen.h | 2 +- 5 files changed, 93 insertions(+), 117 deletions(-) diff --git a/src/util.cpp b/src/util.cpp index 36866dd..c9455d9 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -7518,9 +7518,17 @@ StringVector split(const std::string &s,const std::regex &delimiter) } /// find the index of a string in a vector of strings, returns -1 if the string could not be found -int findIndex(StringVector &sv,const std::string &s) +int findIndex(const StringVector &sv,const std::string &s) { auto it = std::find(sv.begin(),sv.end(),s); return it!=sv.end() ? (int)(it-sv.begin()) : -1; } +/// find the index of the first occurrence of pattern \a re in a string \a s +/// returns -1 if the pattern could not be found +int findIndex(const std::string &s,const std::regex &re) +{ + std::smatch match; + return (std::regex_search(s,match,re)) ? (int)match.position() : -1; +} + diff --git a/src/util.h b/src/util.h index f7f6171..d978c87 100644 --- a/src/util.h +++ b/src/util.h @@ -461,7 +461,8 @@ void writeLatexSpecialFormulaChars(FTextStream &t); StringVector split(const std::string &s,const std::string &delimiter); StringVector split(const std::string &s,const std::regex &delimiter); -int findIndex(StringVector &sv,const std::string &s); +int findIndex(const StringVector &sv,const std::string &s); +int findIndex(const std::string &s,const std::regex &re); bool recognizeFixedForm(const char* contents, FortranFormat format); FortranFormat convertFileNameFortranParserCode(QCString fn); diff --git a/src/vhdlcode.l b/src/vhdlcode.l index 37cc720..7c4b70b 100644 --- a/src/vhdlcode.l +++ b/src/vhdlcode.l @@ -324,7 +324,7 @@ XILINX "INST"|"NET"|"PIN"|"BLKNM"|"BUFG"|"COLLAPSE"|"CPLD"|"COMPGRP"|"CONFI QCString qcs(yytext); VhdlDocGen::deleteAllChars(qcs,'"'); VhdlDocGen::deleteAllChars(qcs,' '); - if (VhdlDocGen::isNumber(qcs)) + if (VhdlDocGen::isNumber(qcs.str())) { writeFont(yyscanner,"vhdllogic",yytext); } @@ -411,7 +411,7 @@ XILINX "INST"|"NET"|"PIN"|"BLKNM"|"BUFG"|"COLLAPSE"|"CPLD"|"COMPGRP"|"CONFI yyextra->vhdlKeyDict.find(yyextra->prevString.str())==yyextra->vhdlKeyDict.end()) { val=val.stripWhiteSpace(); - if (VhdlDocGen::isNumber(val)) + if (VhdlDocGen::isNumber(val.str())) { startFontClass(yyscanner,"vhdllogic"); codifyLines(yyscanner,yytext,yyextra->currClass.data()); @@ -846,7 +846,7 @@ XILINX "INST"|"NET"|"PIN"|"BLKNM"|"BUFG"|"COLLAPSE"|"CPLD"|"COMPGRP"|"CONFI VhdlDocGen::deleteAllChars(qcs,'"'); VhdlDocGen::deleteAllChars(qcs,' '); - if (VhdlDocGen::isNumber(qcs)) + if (VhdlDocGen::isNumber(qcs.str())) { writeFont(yyscanner,"vhdllogic",yytext); } @@ -978,7 +978,7 @@ static bool checkVhdlString(yyscan_t yyscanner,QCString &name) return true; } - if (VhdlDocGen::isNumber(name)) + if (VhdlDocGen::isNumber(name.str())) { startFontClass(yyscanner,"vhdllogic"); yyextra->code->codify(name.data()); @@ -1160,13 +1160,16 @@ static void writeWord(yyscan_t yyscanner,const char *word,const char* curr_class else { QCString qc(temp.data()); - if (VhdlDocGen::isNumber(qc)){ - startFontClass(yyscanner,"vhdllogic"); - yyextra->code->codify(temp.data()); - endFontClass(yyscanner); - } + if (VhdlDocGen::isNumber(qc.str())) + { + startFontClass(yyscanner,"vhdllogic"); + yyextra->code->codify(temp.data()); + endFontClass(yyscanner); + } else - yyextra->code->codify(temp.data()); + { + yyextra->code->codify(temp.data()); + } } } } diff --git a/src/vhdldocgen.cpp b/src/vhdldocgen.cpp index 299ae26..539caae 100644 --- a/src/vhdldocgen.cpp +++ b/src/vhdldocgen.cpp @@ -25,6 +25,9 @@ #include #include #include +#include +#include + #include #include #include @@ -271,23 +274,19 @@ static void writeVhdlDotLink(FTextStream &t, static QCString formatBriefNote(const QCString &brief,ClassDef * cd) { - QRegExp ep("[\n]"); QCString vForm; QCString repl("
"); QCString file=cd->getDefFileName(); int k=cd->briefLine(); - QCStringList qsl=QCStringList::split(ep,brief); - for(uint j=0;jtypeString(),"inout")==0)); if (md) @@ -325,10 +323,8 @@ static void writeColumn(FTextStream &t,const MemberDef *md,bool start) if (!toolTip.isEmpty()) { QCString largs = md->argsString(); - if (!largs.isEmpty()) - largs=largs.replace(reg," "); toolTip+=" ["; - toolTip+=largs; + toolTip+=substitute(largs,"%"," "); toolTip+="]"; } } @@ -355,7 +351,7 @@ static void writeColumn(FTextStream &t,const MemberDef *md,bool start) QCString largs = md->argsString(); if (!largs.isEmpty()) { - largs=largs.replace(reg," "); + largs=substitute(largs,"%"," "); codify(t,largs.data()); } } @@ -1013,8 +1009,9 @@ void VhdlDocGen::parseFuncProto(const char* text,QCString& name,QCString& ret,bo else { s1=s1.stripWhiteSpace(); - int i=s1.find("(",0,FALSE); - int s=s1.find(QRegExp("[ \\t]")); + int i=s1.find('('); + int s=s1.find(' '); + if (s==-1) s=s1.find('\t'); if (i==-1 || i (unsigned int)index) + if ((size_t)index < ql.size()) { return ql[index]; } @@ -1128,7 +1122,7 @@ QCString VhdlDocGen::getProcessNumber() void VhdlDocGen::writeFormatString(const QCString& s,OutputList&ol,const MemberDef* mdef) { - QRegExp reg("[\\[\\]\\.\\/\\:\\<\\>\\:\\s\\,\\;\\'\\+\\-\\*\\|\\&\\=\\(\\)\"]"); + static std::regex reg("[\\[\\]\\.\\/\\<\\>\\:\\s\\,\\;\\'\\+\\-\\*\\|\\&\\=\\(\\)\"]"); QCString qcs = s; qcs+=QCString(" ");// parsing the last sign QCString find=qcs; @@ -1136,9 +1130,7 @@ void VhdlDocGen::writeFormatString(const QCString& s,OutputList&ol,const MemberD char buf[2]; buf[1]='\0'; - int j; - int len; - j = reg.match(temp.data(),0,&len); + int j = findIndex(temp.str(),reg); ol.startBold(); if (j>=0) @@ -1148,7 +1140,7 @@ void VhdlDocGen::writeFormatString(const QCString& s,OutputList&ol,const MemberD find=find.left(j); buf[0]=temp[j]; const char *ss=VhdlDocGen::findKeyWord(find); - bool k=isNumber(find); // is this a number + bool k=isNumber(find.str()); // is this a number if (k) { ol.docify(" "); @@ -1184,7 +1176,7 @@ void VhdlDocGen::writeFormatString(const QCString& s,OutputList&ol,const MemberD { temp=st; } - j = reg.match(temp.data(),0,&len); + j = findIndex(temp.str(),reg); }//while }//if else @@ -1197,16 +1189,10 @@ void VhdlDocGen::writeFormatString(const QCString& s,OutputList&ol,const MemberD /*! * returns TRUE if this string is a number */ -bool VhdlDocGen::isNumber(const QCString& s) +bool VhdlDocGen::isNumber(const std::string& s) { - static QRegExp regg("[0-9][0-9eEfFbBcCdDaA_.#-+?xXzZ]*"); - - if (s.isEmpty()) return FALSE; - int j,len; - j = regg.match(s.data(),0,&len); - if ((j==0) && (len==(int)s.length())) return TRUE; - return FALSE; - + static std::regex regg("[0-9][0-9eEfFbBcCdDaA_.#-+?xXzZ]*"); + return std::regex_match(s,regg); }// isNumber @@ -1666,7 +1652,7 @@ bool VhdlDocGen::writeVHDLTypeDocumentation(const MemberDef* mdef, const Definit writeLink(mdef,ol); ol.docify(" "); - largs=largs.replace(QRegExp("#")," "); + largs=substitute(largs,"#"," "); VhdlDocGen::formatString(largs,ol,mdef); return hasParams; } @@ -2312,8 +2298,8 @@ void VhdlDocGen::parseUCF(const char* input, Entry* entity,QCString fileName,b } else { - QRegExp ee("[\\s=]"); - int in=temp.find(ee); + static std::regex ee("[[:space:]=]"); + int in=findIndex(temp.str(),ee); QCString ff=temp.left(in); temp.stripPrefix(ff.data()); ff.append("#"); @@ -2332,14 +2318,14 @@ void VhdlDocGen::parseUCF(const char* input, Entry* entity,QCString fileName,b static void initUCF(Entry* root,const char* type,QCString & qcs,int line,QCString & fileName,QCString & brief) { if (qcs.isEmpty())return; - QRegExp reg("[\\s=]"); QCString n; // bool bo=(qstricmp(type,qcs.data())==0); VhdlDocGen::deleteAllChars(qcs,';'); qcs=qcs.stripWhiteSpace(); - int i= qcs.find(reg); + static std::regex reg("[[:space:]=]"); + int i = findIndex(qcs.str(),reg); if (i<0) return; if (i==0) { @@ -2417,26 +2403,28 @@ static void writeUCFLink(const MemberDef* mdef,OutputList &ol) // for cell_inst : [entity] work.proto [ (label|expr) ] QCString VhdlDocGen::parseForConfig(QCString & entity,QCString & arch) { - int index; QCString label; if (!entity.contains(":")) return ""; - QRegExp exp("[:()\\s]"); - QCStringList ql=QCStringList::split(exp,entity); - //int ii=ql.findIndex(ent); - assert(ql.count()>=2); + static std::regex exp("[:()[:space:]]"); + auto ql=split(entity.str(),exp); + if (ql.size()<2) + { + return ""; + } label = ql[0]; entity = ql[1]; + int index; if ((index=entity.findRev("."))>=0) { entity.remove(0,index+1); } - if (ql.count()==3) + if (ql.size()==3) { - arch= ql[2]; - ql=QCStringList::split(exp,arch); - if (ql.count()>1) // expression + arch = ql[2]; + ql=split(arch.str(),exp); + if (ql.size()>1) // expression { arch=""; } @@ -2448,26 +2436,29 @@ QCString VhdlDocGen::parseForConfig(QCString & entity,QCString & arch) QCString VhdlDocGen::parseForBinding(QCString & entity,QCString & arch) { - int index; - QRegExp exp("[()\\s]"); + static std::regex exp("[()[:space:]]"); - QCString label=""; - QCStringList ql=QCStringList::split(exp,entity); + auto ql = split(entity.str(),exp); - if (ql.contains("open")) + if (findIndex(ql,"open")!=-1) { return "open"; } - label=ql[0]; + if (ql.size()<2) + { + return ""; + } + std::string label=ql[0]; entity = ql[1]; + int index; if ((index=entity.findRev("."))>=0) { entity.remove(0,index+1); } - if (ql.count()==3) + if (ql.size()==3) { arch=ql[2]; } @@ -2607,14 +2598,6 @@ ferr: md->setDocumentation(cur->doc.data(),cur->docFile.data(),cur->docLine); FileDef *fd=ar->getFileDef(); md->setBodyDef(fd); - //QCString info="Info: Elaborating entity "+n1; - //fd=ar->getFileDef(); - //info+=" for hierarchy "; - //QRegExp epr("[|]"); - //QCString label=cur->type+":"+cur->write+":"+cur->name; - //label.replace(epr,":"); - //info+=label; - //fprintf(stderr,"\n[%s:%d:%s]\n",fd->fileName().data(),cur->startLine,info.data()); ar->insertMember(md.get()); MemberName *mn = Doxygen::functionNameLinkedMap->add(uu); mn->push_back(std::move(md)); @@ -2721,15 +2704,15 @@ void VhdlDocGen::addBaseClass(ClassDef* cd,ClassDef *ent) bcd.usedName.append("(2)"); return; } - static QRegExp reg("[0-9]+"); + static std::regex reg("[[:digit:]]+"); QCString s=n.left(i); QCString r=n.right(n.length()-i); - QCString t=r; + std::string t=r.str(); VhdlDocGen::deleteAllChars(r,')'); VhdlDocGen::deleteAllChars(r,'('); r.setNum(r.toInt()+1); - t.replace(reg,r.data()); - s.append(t.data()); + std::regex_replace(t, reg, r.str()); + s.append(t.c_str()); bcd.usedName=s; bcd.templSpecifiers=t; } @@ -2913,13 +2896,12 @@ void alignText(QCString & q) q.append(" ..."); - QRegExp reg("[\\s|]"); QCString str(q.data()); QCString temp; while (str.length()>80) { - int j=str.findRev(reg,80); + int j=std::max(str.findRev(' ',80),str.findRev('|',80)); if (j<=0) { temp+=str; @@ -2942,8 +2924,8 @@ void alignText(QCString & q) void FlowChart::printNode(const FlowChart& flo) { QCString ui="-"; - QCString q,t; - QRegExp ep("[\t\n\r]"); + std::string q; + std::string t; ui.fill('-',255); @@ -2951,7 +2933,7 @@ void FlowChart::printNode(const FlowChart& flo) { if (flo.stamp>0) { - q=ui.left(2*flo.stamp); + q=ui.left(2*flo.stamp).str(); } else { @@ -2964,20 +2946,21 @@ void FlowChart::printNode(const FlowChart& flo) { if (flo.type & COMMENT_NO) { - t=flo.label; + t=flo.label.str(); } else { - t=flo.text; + t=flo.text.str(); } - t=t.replace(ep,""); - if (t.isEmpty()) + static std::regex ep("[[:space:]]"); + t = std::regex_replace(t,ep,std::string("")); + if (t.empty()) { t=" "; } if (flo.stamp>0) { - q=ui.left(2*flo.stamp); + q=ui.left(2*flo.stamp).str(); } else { @@ -2985,15 +2968,15 @@ void FlowChart::printNode(const FlowChart& flo) } if (flo.type & EMPTNODE) { - printf("\n NO: %s%s[%d,%d]",q.data(),FlowChart::getNodeType(flo.type),flo.stamp,flo.id); + printf("\n NO: %s%s[%d,%d]",q.c_str(),FlowChart::getNodeType(flo.type),flo.stamp,flo.id); } else if (flo.type & COMMENT_NO) { - printf("\n NO: %s%s[%d,%d]",t.data(),FlowChart::getNodeType(flo.type),flo.stamp,flo.id); + printf("\n NO: %s%s[%d,%d]",t.c_str(),FlowChart::getNodeType(flo.type),flo.stamp,flo.id); } else { - printf("\n NO: %s[%d,%d]",t.data(),flo.stamp,flo.id); + printf("\n NO: %s[%d,%d]",t.c_str(),flo.stamp,flo.id); } } } @@ -3247,9 +3230,6 @@ FlowChart::FlowChart(int typ,const char * t,const char* ex,const char* lab) void FlowChart::addFlowChart(int type,const char* text,const char* exp, const char *label) { - static QRegExp reg("[;]"); - static QRegExp reg1("[\"]"); - if (!VhdlDocGen::getFlowMember()) return; QCString typeString(text); @@ -3258,12 +3238,12 @@ void FlowChart::addFlowChart(int type,const char* text,const char* exp, const ch if (text) { - typeString=typeString.replace(reg,"\n"); + typeString=substitute(typeString,";","\n"); } if (exp) { - expression=expression.replace(reg1,"\\\""); + expression=substitute(expression,"\"","\\\""); } if (type & (START_NO | VARIABLE_NO)) @@ -3366,23 +3346,7 @@ void FlowChart::printUmlTree() QCString FlowChart::convertNameToFileName() { - static QRegExp exp ("[^][a-z_A-Z0-9]"); - QCString temp,qcs; - const MemberDef* md=VhdlDocGen::getFlowMember(); - - // temp.sprintf("%p",md); - qcs=md->name(); - - #if 0 - if (qcs.find(exp,0)>=0) - { - qcs.prepend("Z"); - qcs=qcs.replace(exp,"_"); - } - #endif - - //QCString tt= qcs;VhdlDocGen::getRecordNumber(); - return qcs; + return VhdlDocGen::getFlowMember()->name(); } const char* FlowChart::getNodeType(int c) diff --git a/src/vhdldocgen.h b/src/vhdldocgen.h index 46c23a8..3a55b45 100644 --- a/src/vhdldocgen.h +++ b/src/vhdldocgen.h @@ -207,7 +207,7 @@ class VhdlDocGen static QCString getRecordNumber(); static QCString getClassName(const ClassDef*); - static bool isNumber(const QCString& s); + static bool isNumber(const std::string& s); static QCString getProtectionName(int prot); static void parseUCF(const char* input,Entry* entity,QCString f,bool vendor); -- cgit v0.12