Beautify your code vb of VBS code format code

  • 2020-04-01 21:26:58
  • OfStack

However, VB.NET does have a lot of new features that VB6 does not have, code automatic typesetting is one of the functions, which is what we are going to achieve today - VB code format.
Let's look at the implementation first:

Before formatting:

 
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 

After formatting:
The same code at the page code block index 0
C++ level has been very bad, so the choice of C++ as a training language to write this simple VB code format tool. The code is not long, just 200 lines, standard C++ implementation. In addition, in order to completely eliminate the idea of some people continue to use VC6, the use of the auto keyword hey hey. Well, cut the crap and go straight to the code:
 
#include<iostream> 
#include<string> 
#include<vector> 
#include<algorithm> 
#include<fstream> 
using namespace std; 

//Determines whether it is a space
bool isSpace(const char chr) 
{ 
return chr == ' '; 
} 

//Remove left margin
void leftTrim(string &str) 
{ 
string::iterator p=find_if(str.begin(),str.end(),not1(ptr_fun(isSpace))); 
str.erase(str.begin(),p); 
} 

//Remove right margin
void rightTrim(string& str) 
{ 
string::reverse_iterator p=find_if(str.rbegin(),str.rend(),not1(ptr_fun(isSpace))); 
str.erase(p.base(),str.end()); 
} 

//Remove margin on both sides
string trim(const string& str) 
{ 
string strRet(str); 
leftTrim(strRet); 
rightTrim(strRet); 
return strRet; 
} 

//Convert to lowercase
string toLower(const string& str) 
{ 
string strRet(str); 
transform(strRet.begin(),strRet.end(),strRet.begin(),(int (*)(int))tolower); 
return strRet; 
} 

//Determines whether to start with a given keyword
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; 
} 

//The IF... Then... Special inspection
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"); 
} 

//Formats the given row and marks the relevant information
int formatAndMarkLine(string& strCodeLine) 
{ 
//Start keyword "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"}; //Can be null
string _s[] = {"static"}; //Can be null
string _sfpt[] = {"sub","function","property","type"}; 
//_pfp _s is empty
for(auto i=0; i<4; i++) 
vecStartKeywords.push_back(_sfpt[i]); 
//_pfp is empty
for(auto i=0; i<4; i++) 
vecStartKeywords.push_back(_s[0] + " " + _sfpt[i]); 
//_s is empty
for(auto i=0; i<4; i++) 
for(auto j=0; j<3; j++) 
vecStartKeywords.push_back(_pfp[j] + " " + _sfpt[i]); 
//_pfp _s is not empty
for(auto i=0; i<4; i++) 
for(auto j=0; j<3; j++) 
vecStartKeywords.push_back(_pfp[j] + " " + _s[0] + " " + _sfpt[i]); 

//"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]); 

//The middle keyword "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; //Double quotation mark state
 
auto space = true; //The blank status false indicates that no blank character was encountered
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") 'has a space before the single quote
while((c=strCodeLine[n++])) //Append the single quotation mark directly
line += c; 
continue; 
} 
break; 
case '_': //Line continuation operator
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; 
} 

//Split the code into lines
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()) //The last act is empty
vecCodeLines.push_back(line); 
} 

//Formatting the given code
void formatCode(string& strCode,const string& strIndentString) 
{ 
vector<string> vecLines; //All lines of code
splitToLines(strCode,vecLines); // To obtain All lines of code
if(vecLines.size()==0) 
{ 
return; 
} 
auto indentLevel = 0; //Indentation level
auto incompleteLine = false; //Is it an unended line
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; 
} 

As you can see from the code, this is a very basic implementation. Many details are not taken into account, such as the colons linking multiple lines, so if you want to use this tool, please do not write multiple lines to one line.

Finally, I compile a good EXE download: (link: #)

Update:
Increase the select case... End select keyword, thank jjww2999 user feedback.
This article is from: itianda's blog


Related articles: