Strip block comments: Difference between revisions

Added zkl
(Added zkl)
Line 1,669:
4 =
5 = function something() { }
</pre>
 
=={{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 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 something() {
}
 
</pre>
 
Anonymous user