Strip block comments: Difference between revisions
Content added Content deleted
m (added whitespace before the TOC (table of contents), added a ;Task: (bold) header, added bullet points.) |
m (→{{header|zkl}}: text) |
||
Line 1,997: | Line 1,997: | ||
=={{header|zkl}}== |
=={{header|zkl}}== |
||
<lang zkl>fcn stripper(text,a="/*",b="*/"){ |
<lang zkl>fcn stripper(text, a="/*", b="*/"){ |
||
while(xy:=text.span(a,b,True)){ x,y:=xy; text=text[0,x]+text[x+y,*] |
while(xy:=text.span(a,b,True)){ x,y:=xy; text=text[0,x] + text[x+y,*] } |
||
text |
text |
||
}</lang> |
}</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. |
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}} |
{{out}} |
||
The input is in a file because I'm too lazy to type it in: |
The input (from the task description) is in a file because I'm too lazy to type it in: |
||
<pre> |
<pre> |
||
stripper(File("text.txt").read().text); |
|||
⚫ | |||
⚫ | |||
⚫ | |||
} |
|||
⚫ | |||
⚫ | |||
⚫ | |||
} |
|||
} |
|||
⚫ | |||
⚫ | |||
</pre> |
</pre> |
||