Strip block comments: Difference between revisions

m
m (added whitespace before the TOC (table of contents), added a ;Task: (bold) header, added bullet points.)
Line 1,997:
 
=={{header|zkl}}==
<lang zkl>fcn stripper(text, a="/*", b="*/"){
while(xy:=text.span(a,b,True)){ x,y:=xy; text=text[0,x] + text[x+y,*]} }
text
}</lang>
The span method takes two tokens and matches the shortest or longest balanced match (if True). It assumes there are no escape characters (such as \ or ""). So we just repeatedly strip out the longest balanced comments until there aren't any left (span returns the empty list). If a comment was unbalanced, span would fail but this code doesn't check that and just assumes no more matches.
{{out}}
The input (from the task description) is in a file because I'm too lazy to type it in:
<pre>
stripper(File("text.txt").read().text);
function subroutine() {
a = b + c ;
}
 
function subroutine() {
a = b + c ;
function something() {
}
}
 
function something() {
}
</pre>
 
Anonymous user