-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathq5_strip.pl
63 lines (53 loc) · 1.82 KB
/
q5_strip.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/perl
#
# Author : Harman Birdi
# Date : Sep 13, 2016
# Description : Program to preprocess JSON script to remove single line
# C-style comments from it.
#
use strict;
use warnings FATAL => 'all';
#
# This subroutine splits the original string into multiple lines and
# processes them line by line to check if it contains any // style comment
# It returns the lines without any comments. Any lines that were complete
# comments are replaced by an empty line.
#
sub decommentify {
my @inlines = split('\n', $_[0]);
my @outlines = ();
foreach my $line (@inlines) {
$line =~ s/(\/)\1+/\/\//g; # First collapse all 2+ occurences of // into one //
if ($line =~ /^(\w)*\/\/.*/) { # Remove line starting with // or optional whitespace before comment
push(@outlines, '');
} elsif ($line =~ /(.*)\/\/.*/) { # Remove lines containing comment //, but keeping the rest
push(@outlines, $1);
} elsif ($line =~ /[^\/].*/) { # Keep entire line if not // in it
push(@outlines, $line);
}
}
return join("\n", @outlines);
}
my $str = <<'EOSTR';
// this is a comment
/////// this is a comment too
{ // another comment
/////////////
true, "foo", // 3rd comment
"http://www.ariba.com" // comment after URL
"http://www.yahoo.com"// no space between text and comment after URL
"https://www.google.com"//no space between text and comment after URL
"https://www.google.com?ex=1" //no space between text and comment after URL
// This is a test http://www.pulse.com
"more than 2 slashes" ///// more than 2 slash comment
}
EOSTR
print "------------\n";
print "INPUT STRING\n";
print "------------\n";
print $str . "\n";
print "-------------\n";
print "OUTPUT STRING\n";
print "-------------\n";
print decommentify($str);
print "\n\n";