package Cpanel::CommentKiller; # cpanel - Cpanel/CommentKiller.pm Copyright(c) 2009 cPanel, Inc. # All rights Reserved. # copyright@cpanel.net http://cpanel.net # This code is subject to the cPanel license. Unauthorized copying is prohibited use strict; sub new { my ( $obj, %OPTS ) = @_; my $self = {}; bless $self; $self->{'in_c_comment'} = 0; return $self; } sub parse { my ( $self, $rline ) = @_; if ( $self->{'in_c_comment'} ) { if ( index( $rline, '*/' ) > -1 ) { $rline = substr( $rline, index( $rline, '*/' ) + 2 ); $self->{'in_c_comment'} = 0; } else { return; #still in comment } } # If it does not contain *, #, or // it cannot be a comment if ( $rline !~ tr/\*\#\//\*\#\// || ( $rline !~ tr/\*\#/\*\#/ && index( $rline, '//' ) == -1 ) ) { return $rline; } # strip one liner c++ style comments if ( index( $rline, '//' ) > -1 && $rline !~ /(['"])[^\1]*\/\/[^\1]*\1/ ) { $rline =~ s/\/\/.*//g; } # strip one liner perl/shell style comments if ( $rline =~ tr/#/#/ ) { $rline =~ s/\#.*//g; } # strip one liner c style comments if ( index( $rline, '/*' ) > -1 ) { $rline =~ s/\/\*.*\*\///g; } # no need to check to see if we are on a partial as there are no comment starts else { return $rline; } # still in comment if ( index( $rline, '/*' ) > -1 && $rline !~ /(['"])[^\1]*\/\*[^\1]*\1/ ) { $rline =~ s/\/\*.*//; $self->{'in_c_comment'} = 1; } return $rline; } 1;