File: align-comments.pl

package info (click to toggle)
xindy 2.3-2
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 51,492 kB
  • ctags: 16,667
  • sloc: lisp: 82,455; ansic: 43,027; xml: 25,207; asm: 22,166; sh: 20,913; perl: 15,725; fortran: 6,951; makefile: 3,644; cpp: 2,636; lex: 503; sed: 97
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__