Strip block comments: Difference between revisions

no edit summary
No edit summary
No edit summary
Line 27:
 
C.f: [[Strip comments from a string]]
 
=={{header|C++}}==
<lang C++>#include <string>
#include <iostream>
#include <iterator>
#include <fstream>
#include <boost/regex.hpp>
 
void stripComments( std::string & , const std::string , const std::string ) ;
 
int main( ) {
std::ifstream codeFile( "samplecode.txt" ) ;
if ( codeFile ) {
boost::regex commentre( "/\\*.*?\\*/" ) ;//comment start and end, and as few characters in between as possible
std::string my_erase( "" ) ; //erase them
std::string stripped ;
std::string code( (std::istreambuf_iterator<char>( codeFile ) ) ,
std::istreambuf_iterator<char>( ) ) ;
codeFile.close( ) ;
stripped = boost::regex_replace( code , commentre , my_erase ) ;
std::cout << "Code unstripped:\n" << stripped << std::endl ;
return 0 ;
}
else {
std::cout << "Could not find code file!" << std::endl ;
return 1 ;
}
}</lang>
Output:
<pre>
Code unstripped:
 
function subroutine() {
a = b + c ;
}
 
function something() {
}
</pre>
 
=={{header|D}}==
262

edits