Jump to content

Strip block comments: Difference between revisions

Add Python
(Add Python)
Line 308:
 
</pre>
 
=={{header|Python}}==
The code has comment delimeters as an argument and will also strip ''nested'' block comments.
 
<lang python>def _commentstripper(txt, delim):
'Strips first nest of block comments'
deliml, delimr = delim
out = ''
if deliml in txt:
indx = txt.index(deliml)
out += txt[:indx]
txt = txt[indx+len(deliml):]
txt = _commentstripper(txt, delim)
assert delimr in txt, 'Cannot find closing comment delimiter in ' + txt
indx = txt.index(delimr)
out += txt[(indx+len(delimr)):]
else:
out = txt
return out
 
def commentstripper(txt, delim=('/*', '*/')):
'Strips nests of block comments'
deliml, delimr = delim
while deliml in txt:
txt = _commentstripper(txt, delim)
return txt</lang>
 
'''Tests and sample output'''
<lang python>def test():
print('\nNON-NESTED BLOCK COMMENT EXAMPLE:')
sample = ''' /**
* Some comments
* longer comments here that we can parse.
*
* Rahoo
*/
function subroutine() {
a = /* inline comment */ b + c ;
}
/*/ <-- tricky comments */
 
/**
* Another comment.
*/
function something() {
}'''
print(commentstripper(sample))
 
print('\nNESTED BLOCK COMMENT EXAMPLE:')
sample = ''' /**
* Some comments
* longer comments here that we can parse.
*
* Rahoo
*//*
function subroutine() {
a = /* inline comment */ b + c ;
}
/*/ <-- tricky comments */
*/
/**
* Another comment.
*/
function something() {
}'''
print(commentstripper(sample))
if __name__ == '__main__':
test()</lang>
 
<pre>
NON-NESTED BLOCK COMMENT EXAMPLE:
function subroutine() {
a = b + c ;
}
 
function something() {
}
 
NESTED BLOCK COMMENT EXAMPLE:
function something() {
}</pre>
 
=={{header|Tcl}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.