diff options
author | Yves Orton <demerphq@gmail.com> | 2023-01-31 04:36:41 +0100 |
---|---|---|
committer | Yves Orton <demerphq@gmail.com> | 2023-02-19 11:27:50 +0800 |
commit | b8837dad4db9806bc61659d5230a35b90655c07b (patch) | |
tree | 2a2967ce500245445febb3b9e98c6baf5e689ebc /regen | |
parent | 481446ecbbb0078e33ae072401d5dc207d7a60e0 (diff) | |
download | perl-b8837dad4db9806bc61659d5230a35b90655c07b.tar.gz |
embed.fnc - sort entries alphabetically by function name.
This is actually a library sort (lc with underbars removed), followed by a
lexicographical sort. Comment lines are sticky to the line that follows
them.
Somehow the original version of this patch was missed in my earlier work on
tidy_embed.pl, I think I messed up a rebase somehow. I noticed it was missing
when I realized that new entries werent being sorted into place correctly.
While this patch creates a fair bit of churn in the file right now, long
term it will make it easier to use. Also note that the *output* files
have not changed, which validates that the patch did not break anything.
Diffstat (limited to 'regen')
-rw-r--r-- | regen/tidy_embed.pl | 36 |
1 files changed, 27 insertions, 9 deletions
diff --git a/regen/tidy_embed.pl b/regen/tidy_embed.pl index bafe9de082..b0b7182b4f 100644 --- a/regen/tidy_embed.pl +++ b/regen/tidy_embed.pl @@ -10,12 +10,25 @@ my $parser= HeaderParser->new( my $embed= $line_data->{embed} or return; }, - _post_process_grouped_content => sub { - my ($self,$group_ary)= @_; + post_process_grouped_content => sub { + my ($self, $group_ary)= @_; + my $last=chr(0x10FFFF); + for(my $i= $#$group_ary; $i>=0; $i--) { + my $entry= $group_ary->[$i]; + if ($entry->{embed}) { + $last = $entry->{embed}{name}; + } + $entry->{sort}{klc}= lc($last)=~s/[^a-z]+//gr; + $entry->{sort}{key}= $last; + $entry->{sort}{idx}= $i; + } @{$group_ary}= sort { - $a->{embed}{name} cmp $b->{embed}{name} + $a->{sort}{klc} cmp $b->{sort}{klc} || + $a->{sort}{key} cmp $b->{sort}{key} || + $a->{sort}{idx} <=> $b->{sort}{idx} } @{$group_ary}; + delete $_->{sort} for @$group_ary; }, ); my $tap; @@ -30,16 +43,21 @@ my $new= "$file.new"; my $bak= "$file.bak"; $parser->read_file($file); my $lines= $parser->lines; -my @tail; -while ($lines->[-1]{type} eq "content" and - ($lines->[-1]{line} eq "\n" or $lines->[-1]{line}=~/^\s*:/) -) { +my (@head, @tail); +# strip off comments at the start of the file +while ($lines->[0]{type} eq "content" and !$lines->[0]{embed}) { + push @head, shift @$lines; +} + +# strip off comments at the bottom of the file +while ($lines->[-1]{type} eq "content" and !$lines->[-1]{embed}) +{ unshift @tail, pop @$lines; } my $grouped_content_ary= $parser->group_content(); -push @$grouped_content_ary, @tail; -my $grouped_content_txt= $parser->lines_as_str($grouped_content_ary); +my $grouped_content_txt= $parser->lines_as_str( + [ @head, @$grouped_content_ary, @tail ]); if ($grouped_content_txt ne $parser->{orig_content}) { if ($tap) { print "not ok - $0 $file\n"; |