最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

美化你的代碼 vb(VBS)代碼格式化的實現(xiàn)代碼

 更新時間:2012年05月18日 17:39:47   作者:  
雖然VB.NET出現(xiàn)很久了,但還有好多人仍然在使用VB6。我在實現(xiàn)一些小功能的時候也喜歡用VB6,畢竟誰都不想每天的美好心情被VS那烏龜般的啟動速度影響

不過VB.NET確實有許多VB6沒有的新功能,代碼的自動排版就是一項,這也正是我們今天要實現(xiàn)的功能——VB代碼格式化。
先看下實現(xiàn)的效果:

格式化前:

復(fù)制代碼 代碼如下:

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

格式化后:
復(fù)制代碼 代碼如下:

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++作為練手語言寫了這個簡陋的VB代碼格式化工具。代碼不長,才200多行,標(biāo)準(zhǔn)C++實現(xiàn)。另外,為了徹底打消某些人繼續(xù)使用VC6的念頭,使用了auto關(guān)鍵字嘿嘿。好吧,廢話少說,直接上代碼:
復(fù)制代碼 代碼如下:

#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)換成小寫
string toLower(const string& str)
{
string strRet(str);
transform(strRet.begin(),strRet.end(),strRet.begin(),(int (*)(int))tolower);
return strRet;
}

//判斷是否以給定關(guān)鍵字開頭
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; //雙引號狀態(tài)
/*
規(guī)則:
雙引號內(nèi)單引號不考慮,否則后面內(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") '單引號前有一個空格
while((c=strCodeLine[n++])) //直接附加單引號后面內(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; //縮進級別
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;
}

看過代碼應(yīng)該知道這是多么基本的實現(xiàn)了吧,好多細節(jié)都沒有去仔細處理,比如沒有考慮冒號連接多行的情況,所以如果你希望使用此工具,請不要把多行語句寫到一行哦!

最后提供一個我編譯好的EXE下載:PUVBFormatter

更新:
增加select case…end select關(guān)鍵字,感謝jjww2999網(wǎng)友的反饋。
本文來自: itianda's blog

相關(guān)文章

  • 生成隨機數(shù)rand函數(shù)的用法詳解

    生成隨機數(shù)rand函數(shù)的用法詳解

    本篇文章是對生成隨機數(shù)rand函數(shù)的用法進行了詳細的分析介紹,需要的朋友參考下
    2013-05-05
  • C語言實現(xiàn)的猴子吃桃問題算法解決方案

    C語言實現(xiàn)的猴子吃桃問題算法解決方案

    這篇文章主要介紹了C語言實現(xiàn)的猴子吃桃問題解決方案,較為詳細的分析了猴子吃桃問題并給出了C語言算法的實現(xiàn)方法,具有一定參考借鑒價值,需要的朋友可以參考下
    2016-10-10
  • C++編程語言實現(xiàn)單鏈表詳情

    C++編程語言實現(xiàn)單鏈表詳情

    這篇文章主要介紹的是利用C語言實現(xiàn)單鏈表,實現(xiàn)的是鏈表中最簡單的一種單鏈表且每個結(jié)點中只含有一個指針域,下面將詳細舉例說明,需要的朋友可以參考一下
    2021-10-10
  • C++中delete和delete[]的區(qū)別

    C++中delete和delete[]的區(qū)別

    這篇文章主要介紹了C++中delete和delete[]的區(qū)別的相關(guān)資料,需要的朋友可以參考下
    2016-03-03
  • C語言實現(xiàn)無頭單向鏈表的示例代碼

    C語言實現(xiàn)無頭單向鏈表的示例代碼

    本文主要介紹了C語言實現(xiàn)無頭單向鏈表的示例代碼,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • c++中的兩種getline用法詳解

    c++中的兩種getline用法詳解

    c++中有2種getline函數(shù),一種在頭文件 <istream> 中,是istream類的成員函數(shù);另一種是在頭文件 <string> 中,是普通函數(shù)。這篇文章主要介紹了c++中的兩種getline用法,需要的朋友可以參考下
    2020-02-02
  • Qt信號與槽知識點總結(jié)歸納

    Qt信號與槽知識點總結(jié)歸納

    信號和槽是一種高級接口,應(yīng)用于對象之間的通信,它是QT的核心特性,下面這篇文章主要給大家介紹了關(guān)于Qt信號與槽知識點總結(jié)歸納的相關(guān)資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2022-12-12
  • C++實現(xiàn)CreatThread函數(shù)主線程與工作線程交互的方法

    C++實現(xiàn)CreatThread函數(shù)主線程與工作線程交互的方法

    這篇文章主要介紹了C++實現(xiàn)CreatThread函數(shù)主線程與工作線程交互的方法,是Windows應(yīng)用程序設(shè)計中非常實用的方法,需要的朋友可以參考下
    2014-10-10
  • opencv3/C++ 將圖片轉(zhuǎn)換為視頻的實例

    opencv3/C++ 將圖片轉(zhuǎn)換為視頻的實例

    今天小編就為大家分享一篇opencv3/C++ 將圖片轉(zhuǎn)換為視頻的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-12-12
  • C++各種數(shù)據(jù)類型所占內(nèi)存大小詳解

    C++各種數(shù)據(jù)類型所占內(nèi)存大小詳解

    這篇文章主要介紹了C++各種數(shù)據(jù)類型所占內(nèi)存大小,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-08-08

最新評論

会宁县| 阳江市| 镇原县| 绥中县| 长春市| 遵化市| 华坪县| 武威市| 施甸县| 韶关市| 延寿县| 盈江县| 同心县| 五莲县| 博客| 小金县| 建宁县| 乐至县| 兰溪市| 吴堡县| 灵台县| 喀喇沁旗| 东平县| 富川| 福鼎市| 桃园市| 浙江省| 鹤山市| 陇西县| 安泽县| 开化县| 登封市| 武定县| 宜兰市| 苏尼特右旗| 蒙山县| 中江县| 扎囊县| 毕节市| 棋牌| 长子县|