美化你的代碼 vb(VBS)代碼格式化的實(shí)現(xiàn)代碼
不過(guò)VB.NET確實(shí)有許多VB6沒(méi)有的新功能,代碼的自動(dòng)排版就是一項(xiàng),這也正是我們今天要實(shí)現(xiàn)的功能——VB代碼格式化。
先看下實(shí)現(xiàn)的效果:
格式化前:
For i = 0 To WebBrowser1.Document.All.length - 1
If WebBrowser1.Document.All(i).tagName = "HTML" Then
strContent = strContent & WebBrowser1.Document.All(i).innerHTML
Exit For
End If
Next
格式化后:
For i = 0 To WebBrowser1.Document.All.length - 1
If WebBrowser1.Document.All(i).tagName = "HTML" Then
strContent = strContent & WebBrowser1.Document.All(i).innerHTML
Exit For
End If
Next
C++水平一直很爛,所以選擇了C++作為練手語(yǔ)言寫(xiě)了這個(gè)簡(jiǎn)陋的VB代碼格式化工具。代碼不長(zhǎng),才200多行,標(biāo)準(zhǔn)C++實(shí)現(xiàn)。另外,為了徹底打消某些人繼續(xù)使用VC6的念頭,使用了auto關(guān)鍵字嘿嘿。好吧,廢話少說(shuō),直接上代碼:
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<fstream>
using namespace std;
//判斷是否為空格
bool isSpace(const char chr)
{
return chr == ' ';
}
//去除左側(cè)空白
void leftTrim(string &str)
{
string::iterator p=find_if(str.begin(),str.end(),not1(ptr_fun(isSpace)));
str.erase(str.begin(),p);
}
//去除右側(cè)空白
void rightTrim(string& str)
{
string::reverse_iterator p=find_if(str.rbegin(),str.rend(),not1(ptr_fun(isSpace)));
str.erase(p.base(),str.end());
}
//去除兩側(cè)空白
string trim(const string& str)
{
string strRet(str);
leftTrim(strRet);
rightTrim(strRet);
return strRet;
}
//轉(zhuǎn)換成小寫(xiě)
string toLower(const string& str)
{
string strRet(str);
transform(strRet.begin(),strRet.end(),strRet.begin(),(int (*)(int))tolower);
return strRet;
}
//判斷是否以給定關(guān)鍵字開(kāi)頭
bool startWith(const vector<string>& vecCodeKeywords,const string& strCodeLine)
{
string line(toLower(strCodeLine));
for(auto keyword=vecCodeKeywords.begin(); keyword!=vecCodeKeywords.end(); keyword++)
if(line.find(*keyword + " ")==0 || line== *keyword)
return true;
return false;
}
//IF...Then...特殊檢查
bool checkForIfThen(const string& strCodeLine)
{
vector<string> vecIf;
vecIf.push_back("if");
if(!startWith(vecIf,strCodeLine))
return false;
if(toLower(strCodeLine).find("then")==string::npos)
return false;
string line(trim(toLower(strCodeLine)));
if(line.length()<7)
return false;
return !(line.substr(line.length()-4,4) == "then");
}
//格式化給定行并標(biāo)記相關(guān)信息
int formatAndMarkLine(string& strCodeLine)
{
//起始關(guān)鍵字 "if","for","[Private | Friend | Public] [Static] [Sub | Function | Property | Type]","with","do","select"
vector<string> vecStartKeywords;
vecStartKeywords.push_back("if");
vecStartKeywords.push_back("for");
vecStartKeywords.push_back("with");
vecStartKeywords.push_back("do");
vecStartKeywords.push_back("select");
string _pfp[] = {"private","friend","public"}; //可空
string _s[] = {"static"}; //可空
string _sfpt[] = {"sub","function","property","type"};
//_pfp _s 都為空
for(auto i=0; i<4; i++)
vecStartKeywords.push_back(_sfpt[i]);
//_pfp 為空
for(auto i=0; i<4; i++)
vecStartKeywords.push_back(_s[0] + " " + _sfpt[i]);
//_s 為空
for(auto i=0; i<4; i++)
for(auto j=0; j<3; j++)
vecStartKeywords.push_back(_pfp[j] + " " + _sfpt[i]);
//_pfp _s 都不為空
for(auto i=0; i<4; i++)
for(auto j=0; j<3; j++)
vecStartKeywords.push_back(_pfp[j] + " " + _s[0] + " " + _sfpt[i]);
//終止關(guān)鍵字 "end if","next","End [Sub | Function | Property | Type]","end with","loop","end select"
vector<string> vecEndKeywords;
vecEndKeywords.push_back("end if");
vecEndKeywords.push_back("next");
vecEndKeywords.push_back("end with");
vecEndKeywords.push_back("loop");
vecEndKeywords.push_back("end select");
for(auto i=0; i<4; i++)
vecEndKeywords.push_back("end " + _sfpt[i]);
//中間關(guān)鍵字 "else","elseif","case"
vector<string> vecMiddleKeywords;
vecMiddleKeywords.push_back("else");
vecMiddleKeywords.push_back("elseif");
vecMiddleKeywords.push_back("case");
auto mark = 0;
char c;
auto n=0;
string line;
auto quote = false; //雙引號(hào)狀態(tài)
/*
規(guī)則:
雙引號(hào)內(nèi)單引號(hào)不考慮,否則后面內(nèi)容成為注釋
*/
auto space = true; //空白符狀態(tài) false 表示未遇到任何空白符
while((c=strCodeLine[n++]))
{
switch(c)
{
case ' ':
case '\t':
if(quote)
{
line += c;
}
else
{
if(!space)
{
line += c;
space = true;
}
}
break;
case '"':
space = false;
quote = !quote;
line += c;
break;
case '\'':
space = false;
if(quote)
line += c;
else
{
line += " '"; //MsgBox("itianda") '單引號(hào)前有一個(gè)空格
while((c=strCodeLine[n++])) //直接附加單引號(hào)后面內(nèi)容
line += c;
continue;
}
break;
case '_': //續(xù)行符
space = false;
line += c;
if(!quote && n==(int)strCodeLine.length() && n-2>=0 && strCodeLine[n-2]==' ')
mark |= 0x80; //10000000
break;
default:
space = false;
line += c;
}
}
strCodeLine = line;
if(startWith(vecStartKeywords,line) && !checkForIfThen(line))
mark += 1;
if(startWith(vecEndKeywords,line))
mark += 2;
if(startWith(vecMiddleKeywords,line))
mark += 3;
return mark;
}
//將代碼按行分割
void splitToLines(const string& strCode, vector<string>& vecCodeLines)
{
vecCodeLines.clear();
char c;
auto n=0;
string line;
while((c=strCode[n++]))
{
if(c!='\n')
line += c;
else
{
vecCodeLines.push_back(line);
line.clear();
}
}
if(line.length()) //最后一行為空則舍去
vecCodeLines.push_back(line);
}
//格式化給定代碼
void formatCode(string& strCode,const string& strIndentString)
{
vector<string> vecLines; //所有代碼行
splitToLines(strCode,vecLines); //獲取所有代碼行
if(vecLines.size()==0)
{
return;
}
auto indentLevel = 0; //縮進(jìn)級(jí)別
auto incompleteLine = false; //是否是未結(jié)束行
for(auto line=vecLines.begin(); line!=vecLines.end(); line++)
{
auto indent = indentLevel;
auto mask = formatAndMarkLine(*line);
switch(mask & ~0x80)
{
case 0:
break;
case 1:
indentLevel++;
break;
case 2:
indent--;
indentLevel--;
break;
case 3:
indent--;
break;
}
if(incompleteLine)
indent++;
incompleteLine = mask & 0x80;
if(indent<0)
indent = 0;
if(indentLevel<0)
indentLevel = 0;
string strIndent;
for(auto i=0; i<indent; i++)
strIndent += strIndentString;
*line = strIndent + *line;
}
strCode.clear();
for(auto line=vecLines.begin(); line!=vecLines.end(); line++)
strCode+= trim(*line).length() ? "\n" + *line : "";
}
int main()
{
string indentString = " ";
string code;
ifstream inputFile("in.txt");
string line;
while(getline(inputFile,line))
{
code += line + "\n";
}
formatCode(code,indentString);
ofstream outputFile("out.txt");
outputFile<<"Your beautiful code:"<<endl<<
"-------------------------------------------------------------------"
<<endl<<code<<endl<<endl<<
"-------------------------------------------------------------------"
<<endl<<
" Formatted by itianda's PUVBFormatter"
<<endl<<
" http://www.programup.com/blog"
<<endl;
return 0;
}
看過(guò)代碼應(yīng)該知道這是多么基本的實(shí)現(xiàn)了吧,好多細(xì)節(jié)都沒(méi)有去仔細(xì)處理,比如沒(méi)有考慮冒號(hào)連接多行的情況,所以如果你希望使用此工具,請(qǐng)不要把多行語(yǔ)句寫(xiě)到一行哦!
最后提供一個(gè)我編譯好的EXE下載:PUVBFormatter
更新:
增加select case…end select關(guān)鍵字,感謝jjww2999網(wǎng)友的反饋。
本文來(lái)自: itianda's blog
相關(guān)文章
C語(yǔ)言修煉之路靈根孕育源流出?初識(shí)C言大道生上篇
C語(yǔ)言是一門(mén)面向過(guò)程、抽象化的通用程序設(shè)計(jì)語(yǔ)言,廣泛應(yīng)用于底層開(kāi)發(fā)。C語(yǔ)言能以簡(jiǎn)易的方式編譯、處理低級(jí)存儲(chǔ)器。C語(yǔ)言是僅產(chǎn)生少量的機(jī)器語(yǔ)言以及不需要任何運(yùn)行環(huán)境支持便能運(yùn)行的高效率程序設(shè)計(jì)語(yǔ)言2022-03-03C++ 基本算法 冒泡法、交換法、選擇法、實(shí)現(xiàn)代碼集合
大家在學(xué)習(xí)C語(yǔ)言的時(shí)候,老師可能都會(huì)講的幾個(gè)算法,這里簡(jiǎn)單整理下,方便需要的朋友2013-04-04C++ API功能設(shè)計(jì)的實(shí)現(xiàn)
C++ API中看似很小的修改,都可能會(huì)影響到生成的對(duì)象和庫(kù)文件的二進(jìn)制表示,如果客戶想替換共享庫(kù)使之工作,就不能簡(jiǎn)單的替換庫(kù)文件了事,而往往需要重新編譯2022-08-08C語(yǔ)言中調(diào)用Swift函數(shù)實(shí)例詳解
這篇文章主要介紹了C語(yǔ)言中調(diào)用Swift函數(shù)實(shí)例詳解的相關(guān)資料,實(shí)現(xiàn)該功能可以通過(guò)定義全局的指向Blocks的對(duì)象指針來(lái)實(shí)現(xiàn),需要的朋友可以參考下2017-07-07C語(yǔ)言實(shí)現(xiàn)電影管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)電影管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08C++利用多態(tài)實(shí)現(xiàn)職工管理系統(tǒng)(項(xiàng)目開(kāi)發(fā))
這篇文章主要介紹了C++利用多態(tài)實(shí)現(xiàn)職工管理系統(tǒng)(項(xiàng)目開(kāi)發(fā)),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01C++數(shù)據(jù)封裝以及定義結(jié)構(gòu)的詳細(xì)講解
這篇文章主要詳細(xì)講解了C++數(shù)據(jù)封裝以及定義結(jié)構(gòu),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-05-05