However, VB. NET does have many new functions that VB6 does not have. The Automatic Code formatting is one of the functions that we want to implement today-VB Code formatting.
Let's take a look at the effect:
Before formatting:
Copy codeThe Code is as follows: 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
End If
Next
After formatting:Copy codeThe Code is as follows: 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
End If
Next
C ++ has been very bad, so I chose C ++ as the trainer language to write this simple VB Code formatting tool. The code is not long, only more than 200 lines, standard C ++ implementation. In addition, in order to completely eliminate some people's idea of continuing to use VC6, the auto keyword is used. Okay, let's just talk about the Code:Copy codeThe Code is as follows: # 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 the left blank
Void leftTrim (string & str)
{
String: iterator p = find_if (str. begin (), str. end (), not1 (ptr_fun (isSpace )));
Str. erase (str. begin (), p );
}
// Remove the white space on the right
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 the blank spaces 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;
}
// Determine 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;
}
// IF... Then... special check
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 ");
}
// Format the given row and mark the related information
Int formatAndMarkLine (string & strCodeLine)
{
// Start keyword "if", "for", "[Private | Friend | Public] [Static] [Sub | Function | Property | Type]", "", "do", "select"
Vector <string> vecStartKeywords;
VecStartKeywords. push_back ("if ");
VecStartKeywords. push_back ("");
VecStartKeywords. push_back ("");
VecStartKeywords. push_back ("do ");
VecStartKeywords. push_back ("select ");
String _ pfp [] = {"private", "friend", "public"}; // can be empty
String _ s [] = {"static"}; // 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 are 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]);
// Keyword "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 ");
VecEndKeywords. push_back ("loop ");
VecEndKeywords. push_back ("end select ");
For (auto I = 0; I <4; I ++)
VecEndKeywords. push_back ("end" + _ sfpt [I]);
// Intermediate 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 status
/*
Rules:
Single quotation marks in double quotation marks are not considered; otherwise, the following content is annotated.
*/
Auto space = true; // the blank space status is false, indicating that no blank space is 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")' There is a space before the single quotation mark
While (c = strCodeLine [n ++]) // directly append the content following the single quotes
Line + = c;
Continue;
}
Break;
Case '_': // The line feed.
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;
}
// Separate the code by line
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 () // if the last row is null, it is left empty.
VecCodeLines. push_back (line );
}
// Format the given Code
Void formatCode (string & strCode, const string & strIndentString)
{
Vector <string> vecLines; // All code lines
SplitToLines (strCode, vecLines); // obtain all code lines
If (vecLines. size () = 0)
{
Return;
}
Auto indentLevel = 0; // indent level
Auto incompleteLine = false; // whether the row is not ended
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 <
"Formatted by itianda's PUVBFormatter"
<Endl <
Http://www.programup.com/blog"
<Endl;
Return 0;
}
After reading the code, you should know how basic the implementation is. A lot of details are not carefully handled. For example, if you do not consider connecting multiple rows with colons, if you want to use this tool, please do not write multi-line statements into one line!
Finally, we provide a compiled EXE download: PUVBFormatter.
Update:
Add select case... End select keyword. Thanks to feedback from jjww2999.
This article is from: itianda's blog