From 1108dde1ce7aac7bfb2461d024380f2e5dbee190 Mon Sep 17 00:00:00 2001 From: Dimitri van Heesch Date: Mon, 18 Sep 2023 21:25:29 +0200 Subject: [PATCH] Reduce code duplication --- src/util.cpp | 102 +++++++++++++++++++-------------------------------- 1 file changed, 37 insertions(+), 65 deletions(-) diff --git a/src/util.cpp b/src/util.cpp index ba6d9ff9ac2..ab2971ef3b4 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -585,37 +585,21 @@ QCString removeRedundantWhiteSpace(const QCString &s) c=src[i]; char nc=i0 || // inside search string - i==0 || // if it is the first character - !isId(pc) // the previous may not be a digit - ) - ) - csp++; - else // reset counter - csp=0; - - if (vosp<9 && c==volatileScope[vosp] && // character matches substring "volatile" - (vosp>0 || // inside search string - i==0 || // if it is the first character - !isId(pc) // the previous may not be a digit - ) - ) - vosp++; - else // reset counter - vosp=0; - - // search for "virtual" - if (vsp<8 && c==virtualScope[vsp] && // character matches substring "virtual" - (vsp>0 || // inside search string - i==0 || // if it is the first character - !isId(pc) // the previous may not be a digit + auto searchForKeyword = [&](const char *kw,uint32_t &matchLen,uint32_t totalLen) + { + if (matchLen<=totalLen && c==kw[matchLen] && // character matches substring kw + (matchLen>0 || // inside search string + i==0 || // if it is the first character + !isId(pc) // the previous may not be a digit + ) ) - ) - vsp++; - else // reset counter - vsp=0; + matchLen++; + else // reset counter + matchLen=0; + }; + searchForKeyword(constScope, csp, 5); // keyword: const + searchForKeyword(volatileScope, vosp, 8); // keyword: volatile + searchForKeyword(virtualScope, vsp, 7); // keyword: virtual // search for "operator" if (osp<11 && (osp>=8 || c==operatorScope[osp]) && // character matches substring "operator" followed by 3 arbitrary characters @@ -782,43 +766,31 @@ QCString removeRedundantWhiteSpace(const QCString &s) break; default: *dst++=c; - if (c=='t' && csp==5) - { - if (i csp && src[i-csp] == ':' && src[i-csp-1] == ':') csp = 0; - } - else if (c=='e' && vosp==8) - { - if (i vosp && src[i-vosp] == ':' && src[i-vosp-1] == ':') vosp = 0; - } - else if (c=='l' && vsp==7) - { - if (i vsp && src[i-vsp] == ':' && src[i-vsp-1] == ':') vsp = 0; - } + auto correctKeywordAllowedInsideScope = [&](char cc,uint32_t &matchLen,uint32_t totalLen) { + if (c==cc && matchLen==totalLen) + { + if ((imatchLen && src[i-matchLen] == ':' && src[i-matchLen-1] == ':')) // ::keyword + ) matchLen = 0; + }; + }; + correctKeywordAllowedInsideScope('t',csp, 5); // keyword: const + correctKeywordAllowedInsideScope('e',vosp,8); // keyword: volatile + correctKeywordAllowedInsideScope('l',vsp, 7); // keyword: virtual - if (c=='t' && csp==5 && i(nc))) - ) // prevent const ::A from being converted to const::A + auto correctKeywordNotPartOfScope = [&](char cc,uint32_t &matchLen,uint32_t totalLen) { - *dst++=' '; - csp=0; - } - else if (c=='e' && vosp==8 && i(nc))) - ) // prevent volatile ::A from being converted to volatile::A - { - *dst++=' '; - vosp=0; - } - else if (c=='l' && vsp==7 && i(nc))) - ) // prevent virtual ::A from being converted to virtual::A - { - *dst++=' '; - vsp=0; - } + if (c==cc && matchLen==totalLen && i