State machine programming ideas (2): Delete Code comments (currently C/C ++ and Java are supported), programming ideas java

Source: Internet
Author: User

State machine programming ideas (2): Delete Code comments (currently C/C ++ and Java are supported), programming ideas java
Preface

Sometimes we need to delete comments to keep the information confidential or simply read the code.

I have considered regular expressions before, but it is quite troublesome to implement them. The state machine can classify multiple situations into one type of State for further decomposition, greatly simplifying the problem. This article is based on the state machine.

Directory
 
 
  • Delete C/C ++ code comments
  • Delete Java code comments
  • Program
  • Reference
Considerations for deleting C/C ++ code comments
  • //
  • /**/
  • // And/**/nesting (note that no/**/AND/**/nesting exists)
  • Line comments (separated)
  • Characters in/and *
  • // And /**/
  • Line code in the string (separated)
  • Header file may exist/
Status transfer description

For more information, see the blog http://www.cnblogs.com/zhanghaiba/p/3569928.html?#7.

This article makes the following modifications or optimizations based on the above blog:

  • The original blog does not consider/***/(where * is an odd number) and has been corrected.
  • Switch to windows platform and support windows newline \ r \ n (and Note: if the original file does not end with a carriage return, it will be automatically inserted)
  • Optimized status to enumeration Constants
  • Status transition is changed from the if... else... elseif structure to the switch... case structure, which is clearer and more efficient for large code.

 

 

Except NOTE_MULTILINE_STAR, character (string) processing is required in other States to ensure correct output. For details, refer to the code at the end of the article.

 

Considerations for deleting Java code annotations
  • //
  • /**/
  • /***/
  • // And/**/nesting (note that/**/AND/**/nesting do not exist, and/**/AND/**/nesting does not exist, does not exist/**/AND/**/nesting)
  • // And/***/nesting
  • Characters in/and *
  • //,/**/, And /***/
Status transfer description

It can be seen that the annotation rules in java are simpler, and the/***/can be completely covered by the/***/state. There will be no LINE folding comments or string folding, so the status is simpler. If you are interested, you can draw a picture. In other words, the program that deletes C/C ++ comments can be used to delete java comments.

 

Program

 

1 import java. io. fileInputStream; 2 import java. io. inputStreamReader; 3 import java. io. bufferedReader; 4 5 import java. io. fileOutputStream; 6 import java. io. outputStreamWriter; 7 import java. io. bufferedWriter; 8 9 import java. io. IOException; 10 11 import java. util. listen; 12 13/** 14 * @ author xiaoxi666 15 * @ version 1.0.0 2017.12.01 16 */17 18 public class deleteCAndCplusplusAndJavaNote {19 20 /** 21 * Status 22 */23 enum State {24 CODE, // normal CODE 25 SLASH, // SLASH 26 NOTE_MULTILINE, // comment 27 NOTE_MULTILINE_STAR on multiple lines, // multiple-line comments encounter * 28 NOTE_SINGLELINE, // single-line Comments 29 BACKSLASH, // line comments 30 CODE_CHAR, // character 31 CHAR_ESCAPE_SEQUENCE, // escape character 32 CODE_STRING, // string 33 STRING_ESCAPE_SEQUENCE // Escape Character 34} in the string; 35 36/** 37 * @ function delete comments in the code, return 38 * @ param strToHandle code 39 * @ return deleted note in String format Release code. String format: 40 */41 public static String delete_C_Cplusplus_Java_Note (String strToHandle) {42 StringBuilder builder = new StringBuilder (); 43 44 State state State = State. CODE; // Initiate 45 for (int I = 0; I <strToHandle. length (); ++ I) {46 char c = strToHandle. charAt (I); 47 switch (state) {48 case CODE: 49 if (c = '/') {50 state = State. SLASH; 51} else {52 builder. append (c); 53 if (c = '\'') {54 state = State. CODE_CHAR; 55} else if (c = '\ "') {56 state = State. CODE_STRING; 57} 58} 59 break; 60 case SLASH: 61 if (c = '*') {62 state = State. NOTE_MULTILINE; 63} else if (c = '/') {64 state = State. NOTE_SINGLELINE; 65} else {66 builder. append ('/'); 67 builder. append (c); 68 state = State. CODE; 69} 70 break; 71 case NOTE_MULTILINE: 72 if (c = '*') {73 state = State. NOTE_MULTILINE_STAR; 7 4} else {75 if (c = '\ n') {76 builder. append ("\ r \ n"); // retain empty rows. Of course, you can also remove 77} 78 state = State. NOTE_MULTILINE; // keep current status 79} 80 break; 81 case NOTE_MULTILINE_STAR: 82 if (c = '/') {83 state = State. CODE; 84} else if (c = '*') {85 state = State. NOTE_MULTILINE_STAR; // keep current status 86} 87 else {88 state = State. NOTE_MULTILINE; 89} 90 break; 91 case NOTE_SINGLELINE: 92 if (c = '\') {93 state = State. BACKSLASH; 94} else if (C = '\ n') {95 builder. append ("\ r \ n"); 96 state = State. CODE; 97} else {98 state = State. NOTE_SINGLELINE; // keep current status 99} 100 break; 101 case BACKSLASH: 102 if (c = '\' | c = '\ R' | c =' \ n ') {// the windows system line break is \ r \ n103 if (c = '\ n') {104 builder. append ("\ r \ n"); // retain empty rows. Of course, you can also remove 105} 106 state = State. BACKSLASH; // keep the current status 107} else {108 state = State. NOTE_SINGLELINE; 109} 110 break; 111 case CODE_CHAR: 112 builder. append (c); 113 If (c = '\') {114 state = State. CHAR_ESCAPE_SEQUENCE; 115} else if (c = '\ '') {116 state = State. CODE; 117} else {118 state = State. CODE_CHAR; // keep current status 119} 120 break; 121 case CHAR_ESCAPE_SEQUENCE: 122 builder. append (c); 123 state = State. CODE_CHAR; 124 break; 125 case CODE_STRING: 126 builder. append (c); 127 if (c = '\') {128 state = State. STRING_ESCAPE_SEQUENCE; 129} else if (c = '\ "') {130 state = State. CODE; 131} else {13 2 state = State. CODE_STRING; // keep current status 133} 134 break; 135 case STRING_ESCAPE_SEQUENCE: 136 builder. append (c); 137 state = State. CODE_STRING; 138 break; 139 default: 140 break; 141} 142 return builder. toString (); 144} 145 146/** 147 * @ function reads code from a specified file, return the code content of the 148 * @ param inputFileName file to be deleted as a String in the form of 149 * @ return in the file to be deleted, string format 150 * @ note input file format default: UTF-8151 */152 public static String readFile (String inputFileName) {153 StringBuilder builder = new StringBuilder (); 154 try {155 FileInputStream FCM = new FileInputStream (inputFileName); 156 InputStreamReader dis = new InputStreamReader (FCM ); 157 BufferedReader reader = new BufferedReader (dis); 158 String s; 159 // each time a row is read, 160 while (s = reader. readLine ())! = Null) {161 builder. append (s); 162 builder. append ("\ r \ n"); // windows system line break 163} 164 reader. close (); 165 dis. close (); 166 fi. close (); 167} catch (IOException e) {168 e. printStackTrace (); 169 System. exit (1); 170} 171 return builder. toString (); 172} 173 174/** 175 * @ function Save the code after the annotation is deleted to the specified new file 176 * @ param outputFileName file name 177 of the file for storing the "Code after the annotation is deleted "* @ param strHandled Code 178 */179 public static void writeFile (String outputFileName, string strHandled) {180 try {181 FileOutputStream fos = new FileOutputStream (outputFileName); 182 OutputStreamWriter dos = new OutputStreamWriter (fos); 183 BufferedWriter writer = new BufferedWriter (dos); 184 writer. write (strHandled); 185 writer. close (); 186 dos. close (); 187 fos. close (); 188 System. out. println ("code that without note has been saved successfully in" + outputFileName); 189} catch (IOException e) {190 e. printStackTrace (); 191} 192} 193 194/** 195 * @ function reads the file to be processed, deletes comments, the processed code is written to the new file 196 * @ param args197 */198 public static void main (String [] args) {199 bytes in = new partition (System. in); 200 // The 201 System file to be deleted. out. println ("The fileName that will be delete note:"); 202 String inputFileName = in. nextLine (); 203 // save the "delete comments code" file 204 System. out. println ("The fileName that will save code without note:"); 205 String outputFileName = in. nextLine (); 206 207 String strToHandle = readFile (inputFileName); 208 String strHandled = delete_C_Cplusplus_Java_Note (strToHandle); 209 writeFile (outputFileName, strHandled); 210 211} 212}

 

Description
  • In this program, the Comment occupies the line, that is, the Code except the comment is kept as is (the number of lines will not change), and the comment line becomes blank.
  • If the file suffix is not detected, it means that the code can be written in the. txt file.) You can add the code as needed.
  • This program is applicable to windows. For other platforms such as linux and mac, replace the "\ r \ n" line break. The default file format is UTF.
  • If you are interested, you can encapsulate it into a graphical interface and drag it into the file for processing, which is more useful.
  • This program has passed a lot of tests and has not found any bugs. If you have found any bugs, please submit them.

 

Reference
 
 
  • How to delete all comments in C/C ++ code? The programming thought of state machine: http://www.cnblogs.com/zhanghaiba/p/3569928.html#3853787
  • Who can write a regular expression for deleting comments: http://bbs.csdn.net/topics/380183706
  • Regular Expressions delete comments of code: http://blog.csdn.net/conquer0715/article/details/14446463

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.