Bulk remove annotations from C++/C

Source: Internet
Author: User

Often open the previously written code to be submitted as a job to find either Chinese comments become garbled, or the previous comments too detailed caused the impact of normal reading ....

In short, I want to delete the notes to refresh the time ... Hundred lines within the manual operation is good to say, a little longer is very tiring, and the program to solve must be fast.

Since this is obviously a file io+ string operation, it is better to do it in Python, mainly using the RE module, which uses regular expressions.

1. The file is read line by row and concatenated into a string (too long a file should be selected by block, but this time the block annotation needs to set a marker, otherwise do not know whether the current line is a block comment inside or outside);

2. Write two pattern to identify the annotation and replace the matched string with an empty string.

The simple version probably is, the core is the regular expression matching and replacement, very simple:

2 ImportOS3 ImportRe4 5Hasblockcomment=False6 7 defParse (string):8Pattern1=re.compile (R'//.*')9result = Re.sub (PATTERN1,"", String)Ten  OnePattern2=re.compile (R'(/\*)((\*[^/])*|( [^\*]) *)(\*/)') AResult=re.sub (PATTERN2,"", result) -  -     returnresult the  -  - defMain (): -Filename=input ("input file path and name:") +name,ext=Os.path.splitext (fileName) -File=open (FileName,"R") +Result=open ("Result.txt","W") A  atContent="" -      forEachlineinchFile: -content+=Eachline -L=(str) (Parse (content)) - Result.write (L) -Main ()


For very large files, we need to find the first/* after recording its number of lines and location, and then after the read in the encounter * * When the content is all deleted, because C + +/C comments can not be nested, so for example

/*///..../* should be the first match instead of the second and the end match.

Of course, comments should not be nested if you use them yourself.

In C + + to do it is also very simple, do not call some library functions, you can manually simulate a simple state machine, read the characters one after another, encountered/* When the record location, continue to read, know to encounter */, Mark this position, the output ignores this paragraph of the content, line comment is very simple, No contextual interference, no longer repeat it.

Before Python did the scheme interpreter tired of playing the words can be replaced with Chinese, of course, is playing, no meaning ...

The effect is like this ... It is too boring, in order to enter the convenience also deliberately to the English () replaced () ...

http://user.qzone.qq.com/414445050/blog/1416846214

Bulk remove annotations from C++/C

Related Article

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.