Strip block comments: Difference between revisions

m
apply polish
(Added Groovy version)
m (apply polish)
Line 1:
{{task|Text processing}}[[Category:String manipulation]]
A block comment begins with a ''beginning delimiter'' and ends with a ''ending delimiter'', including the delimiters. These delimiters are often multi-character sequences.
 
Line 123:
Ada.Text_IO.Close (File => File);
end Strip;</lang>
 
output:
<pre>
Line 191 ⟶ 190:
pause();
return 0;
}</lang>
}
;Usage:
</lang>
Usage:
Specify an input file via the first command line argument, and optionally specify comment opening and closing delimiters with the next two args, or defaults of /* and */ are assumed.
;Output:<pre>
<pre>
 
function subroutine() {
Line 247 ⟶ 246:
</pre>
 
=={{header|C sharp|C#}}==
<lang C sharpCsharp>using System;
using System;
 
class Program
Line 266 ⟶ 264:
return sampleText;
}
}</lang>
</lang>
 
=={{header|Clojure}}==
Line 282 ⟶ 279:
(= delim-count 0)(recur (str out (first txt)) (rest txt) delim-count)
true (recur out (rest txt) delim-count))))))</lang>
 
<pre>user> (comment-strip "This /* is */ some /* /* /* */ funny */ */ text")
hdtxt= Th resttxt=is /* is */ some /* /* /* */ funny */ */ text out= txt=This /* is */ some /* /* /* */ funny */ */ text delim-count=0
Line 414 ⟶ 410:
* Another comment.
*/</pre>
 
=={{header|Go}}==
For the extra credit: No optional parameters in Go, but documented below is an efficient technique for letting the caller specify the delimiters.
Line 495 ⟶ 492:
=={{header|Haskell}}==
Comment delimiters can be changed by calling stripComments with different start and end parameters.
 
<lang Haskell>import Data.List
 
Line 526 ⟶ 522:
 
=={{header|Icon}} and {{header|Unicon}}==
If one is willing to concede that the program file will fit in memory, then the following code works:
 
If one is willing to concede that the program file will fit in memory,
then the following code works:
 
<lang Icon>procedure main()
every (unstripped := "") ||:= !&input || "\n" # Load file as one string
Line 546 ⟶ 539:
}
end</lang>
 
Otherwise, the following handles an arbitrary length input:
 
<lang Icon>procedure main()
every writes(stripBlockComment(!&input,"/*","*/"))
Line 565 ⟶ 556:
 
=={{header|J}}==
 
<lang j>strip=:#~1 0 _1*./@:(|."0 1)2>4{"1(5;(0,"0~".;._2]0 :0);'/*'i.a.)&;:
1 0 0
Line 572 ⟶ 562:
0 2 2
)</lang>
 
Example data:
 
<lang j>example=: 0 :0
/**
Line 593 ⟶ 581:
}
)</lang>
 
Example use:
 
<lang j> strip example
Line 606 ⟶ 592:
function something() {
}</lang>
 
Here is a version which allows the delimiters to be passed as an optional left argument as a pair of strings:
 
<lang j>stripp=:3 :0
('/*';'*/') stripp y
Line 616 ⟶ 600:
y #~ -. (+._1&|.) (1 <. 0 >. +)/\.&.|. marks
)</lang>
 
=={{header|Java}}==
<lang java>import java.io.*;
Line 621 ⟶ 606:
 
public class StripBlockComments{
public static void main( String[] args) ){
if( (args.length < 3 ) {
System.out.println("Usage: BeginToken EndToken StringToProcess");
} else {
else{
String beginToken = args[0];
String endToken = args[1];
String inputFile = args[2];
String input = "";
try {
BufferedReader reader = new BufferedReader( new FileReader( inputFile ) );
StringBuilder fileContents = new StringBuilder();
char[] buffer = new char[4096];
while( (reader.read(buffer, 0, 4096) > 0 ) {
fileContents.append( buffer );
}
input = fileContents.toString();
} catch (Exception e) {
catch( Exception e ){
e.printStackTrace();
System.exit(1);
}
StringBuilder output = new StringBuilder();
while (true) {
int begin = input.indexOf(beginToken);
int end = input.indexOf(endToken, begin+beginToken.length());
if( (begin == -1 ) {
output.append( input );
break;
}
output.append( input.substring( 0, begin ) );
input = input.substring( end + endToken.length(), input.length() );
}
System.out.println( output.toString() );
}
}
Line 660 ⟶ 643:
 
=={{header|Liberty BASIC}}==
<lang lb>global CRLF$
global CRLF$
CRLF$ =chr$( 13) +chr$( 10)
 
Line 705 ⟶ 687:
end if
next i
end function</lang>
</lang>
<pre>
 
Line 726 ⟶ 707:
</pre>
 
=={{header|Lua}}==
Line 751 ⟶ 730:
function something() {
}</lang>
</lang>
 
 
=={{header|MATLAB}} / {{header|Octave}}==
Line 852 ⟶ 829:
}
'}</lang>
Output:
 
Output:<pre>
function subroutine() {
a = b + c ;
Line 881 ⟶ 858:
 
=={{header|PL/I}}==
<lang PL/I>/* A program to remove comments from text. */
<lang PL/I>
/* A program to remove comments from text. */
strip: procedure options (main); /* 8/1/2011 */
declare text character (80) varying;
Line 915 ⟶ 891:
end;
 
end strip;</lang>
</lang>
 
=={{header|PureBasic}}==
Line 1,014 ⟶ 989:
=={{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'
Line 1,039 ⟶ 1,013:
txt = _commentstripper(txt, delim)
return txt</lang>
''';Tests and sample output'''
 
'''Tests and sample output'''
<lang python>def test():
print('\nNON-NESTED BLOCK COMMENT EXAMPLE:')
Line 1,134 ⟶ 1,107:
 
puts remove_comments example</lang>
 
outputs
<pre>
Line 1,191 ⟶ 1,163:
{{omit from|Openscad}}
{{omit from|PARI/GP|No real capacity for string manipulation}}
 
[[Category:String manipulation]]
Anonymous user