Beautify your code vb (VBS) code format implementation Code _c language

Source: Internet
Author: User
Tags chr tagname

But VB.net does have a number of new features that VB6 doesn't have, and the automatic layout of the code is one of the features that we're going to implement today--VB code formatting.
First look at the effect of the implementation:

Before formatting:

Copy Code code 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 for
End If
Next

After formatting:
Copy Code code 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 for
End If
Next

C + + level has been very bad, so chose C + + as the practicing language to write this simple VB code formatting tool. The code is not long, only more than 200 lines, the standard C + + implementation. In addition, in order to completely eliminate some people continue to use the idea of VC6, using the Auto keyword hehe. Okay, cut the crap, go straight to the code:
Copy Code code as follows:

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <fstream>
using namespace Std;

To determine whether a space
BOOL Isspace (const char CHR)
{
return CHR = = ';
}

Remove 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 Right blank
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 both sides of white space
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;
}

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");
}

Format a given line and mark related information
int Formatandmarkline (string& strcodeline)
{
Initial 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 empty
String _s[] = {"Static"}; Can be empty
String _sfpt[] = {"Sub", "Function", "Property", "type"};
_PFP _s are 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]);

Terminate the keyword "End If", "Next", "[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]);

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 quote Status
/*
Rules:
Double quotes inside single quotes do not consider, otherwise the following content becomes a comment
*/
Auto Space = true; Whitespace status false indicates that no whitespace has been 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") ' single quotation mark has a space before
while ((c=strcodeline[n++))//Direct append single quotation mark back content
Line + = C;
Continue
}
Break
Case ' _ '://Line continuation character
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 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 ())///Last act empty then go
Veccodelines.push_back (line);
}

Format given code
void Formatcode (string& strcode,const string& strindentstring)
{
Vector<string> Veclines; All lines of code
Splittolines (Strcode,veclines); Get all lines of code
if (Veclines.size () ==0)
{
Return
}
Auto IndentLevel = 0; Indent level
Auto Incompleteline = false; is not an end 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;
}

Read the code should know how the basic implementation of it, a lot of details are not to be carefully processed, such as not considering the colon connection multiple lines, so if you want to use this tool, please do not write multi-line statements to a line Oh!

Finally provide a compiler I compile the exe download: puvbformatter

Update:
Add select Case...end Select keyword, thank jjww2999 Netizen's feedback.
This article from: Itianda ' s blog

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.