File: align-comments.pl

package info (click to toggle)
clisp 1:2.33.2-10
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 36,532 kB
  • ctags: 13,235
  • sloc: lisp: 65,550; ansic: 35,636; asm: 23,001; sh: 14,153; xml: 13,470; fortran: 6,490; objc: 2,481; makefile: 2,069; perl: 164; sed: 55
file content (61 lines) | stat: -rw-r--r-- 1,255 bytes parent folder | download | duplicates (7)
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
#!/usr/bin/perl -w

# Tidy up lines with consecutive end-of-line comments.

use strict;

my @line_buffer = ();

while (<>) {
  process_line($_);
}
dump_line_buffer();


sub process_line {
  my ($line) = @_;

  if ($line =~ m!^(.*?\*/)(/\*.*$)!) {
    process_line("$1\n");
    process_line(" $2\n");
    return;
  }

  if ($line =~ m!^.*\S.*/\*.*\*/!) {
    push @line_buffer, $line;
  } else {
    dump_line_buffer();
    print $line;
  }
}


sub dump_line_buffer {
  if (@line_buffer) {
    if ($#line_buffer == 0) {
      print $line_buffer[0];
    } else {
      my @parts_buffer = ();
      my @max = (0, 0);
      foreach my $line (@line_buffer) {
        my @parts = $line =~ m!^(.*?)\s*/\*\s*(.*?)\s*\*/\s*(.*?)\s*$!;
        $parts[1] = "/* $parts[1] */";
        push @parts_buffer, \@parts;
        my @l = map { length } @parts;
        for (my $i = 0; $i <= $#max; $i++) {
          $max[$i] < $l[$i] and $max[$i] = $l[$i];
        }
      }
      foreach my $line (@parts_buffer) {
        if ($line->[2]) {
          printf("%-*s %-*s %s\n", $max[0], $line->[0], $max[1], $line->[1], $line->[2]);
        } else {
          printf("%-*s %s\n", $max[0], $line->[0], $line->[1]);
        }
      }
    }
    @line_buffer = ();
  }
}

__END__