Anonymous user
Strip block comments: Difference between revisions
m
→{{header|zkl}}: text
m (added whitespace before the TOC (table of contents), added a ;Task: (bold) header, added bullet points.) |
m (→{{header|zkl}}: text) |
||
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>
function subroutine() {▼
a = b + c ;▼
function something() {▼
}
▲ }
</pre>
|