Frans Bouma's blog

Generator.CreateCoolTool();

Syndication

News




    Add to Technorati Favorites

About me

Fun stuff I created

My work

CommentStripper: strip out xml comments and/or normal comments from C# code

A customer of ours asked me if there was an option in LLBLGen Pro to not generate XML comments into the generated code. As there isn't such an option, I though it would be cool to write a little command line app to strip out any xml comment and/or normal comment from C# sourcecode. It might sound to you like the most worthless piece of code ever cooked up, I mean: why would you remove comments from your code, right? . There are situations however, where you have to distribute sourcecode to your client, but you don't want your client to have all the comments, so in a way you give the client the feeling they have the sourcecode, but they also will have a hard time reading it.

My attempt to get such a command line app is available here: http://www.xs4all.nl/~perseus/CommentStripper.zip (5KB). It's C# sourcecode, and does basic comment stripping. It's not able to strip out multi-line /* */ comments, I leave that regexp voodoo requiring code to the reader . I tested it on a large pack of code and it's very fast and the code was even compilable afterwards .

Happy strippin'!

Published Friday, August 19, 2005 11:40 AM by FransBouma

Filed under:

Comments

# re: CommentStripper: strip out xml comments and/or normal comments from C# code@ Friday, August 19, 2005 12:15 PM

This regex will work:

(?<comment>/\*(?>[^/\*\*/]+|/\*(?<depth>)
|
\*/(?<-depth>))*(?(depth)(?!))\*/)

Cheers.

Michael K. Campbell

# re: CommentStripper: strip out xml comments and/or normal comments from C# code@ Friday, August 19, 2005 9:15 PM

So wait a minute, you're telling us that your customer asked for a stripper?

Sahil Malik

# re: CommentStripper: strip out xml comments and/or normal comments from C# code@ Saturday, August 20, 2005 4:45 AM

Thanks for the regex! :) Will see how I can hack that in :)

Sahil: Yeah! so as a geek, the first thing I thought he meant was a stripprogram, so I wrote it. Haven't heard yet if he's happy with it ;)

Frans Bouma