From 592cdf1cdbdf70e8135b2fac28ff225be74f1811 Mon Sep 17 00:00:00 2001 From: Alexander Borisov Date: Mon, 22 Dec 2025 00:39:52 +0300 Subject: [PATCH v8 1/4] Add Perl module (PrettyLine) to a common module. Perl module for generating lines for C tables that do not exceed a certain number of columns (default: 80). The module is necessary for compact and attractive data packaging so that C tables generated take up less space in the file. Append to src/tools/PrettyLine.pm. This is necessary for further use of this code to build Unicode normalization, case, and Encoding C tables. --- src/tools/PrettyLine.pm | 132 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 132 insertions(+) create mode 100644 src/tools/PrettyLine.pm diff --git a/src/tools/PrettyLine.pm b/src/tools/PrettyLine.pm new file mode 100644 index 00000000000..ef071333acd --- /dev/null +++ b/src/tools/PrettyLine.pm @@ -0,0 +1,132 @@ +#---------------------------------------------------------------------- +# +# PrettyLine.pm +# Perl module for generating lines for C tables that do not exceed a certain +# number of columns (default: 80). +# +# The module is necessary for compact and attractive data packaging so that +# C tables generated take up less space in the file. +# +# Default parameters: +# line_limit => 80 - 4: maximum characters per line (80 columns minus +# 4 columns for tab indentation). +# indent => "\t": indentation string prepended to each output line. +# +# Parameters can be passed to the constructor to change them: +# my $pretty = new PrettyLine(indent => " ", line_limit => 80 - 2); +# +# For example: +# my $pretty = new PrettyLine(); +# +# foreach my $key (70..80, 110..115, 1100..1110, 100000..100010, 10..50) +# { +# $pretty->push($key); +# } +# +# print $pretty->result(), "\n"; +# +# Result: +# 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 110, 111, 112, 113, 114, 115, +# 1100, 1101, 1102, 1103, 1104, 1105, 1106, 1107, 1108, 1109, 1110, 100000, +# 100001, 100002, 100003, 100004, 100005, 100006, 100007, 100008, 100009, +# 100010, 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 +# +# Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group +# Portions Copyright (c) 1994, Regents of the University of California +# +# src/tools/PrettyLine.pm +# +#---------------------------------------------------------------------- + +package PrettyLine; + +use strict; +use warnings FATAL => 'all'; + +# Constructor. +# Creates and returns a new PrettyLine object that formats elements +# into lines of limited length with a fixed indentation. +sub new +{ + my ($class, %params) = @_; + + # Default parameters. + my %defaults = ( + # Calculate the line size as 80 columns minus 4 columns for tabulation. + line_limit => 80 - 4, + indent => "\t"); + + return bless { + %defaults, + %params, + ( lines => [], + line => [], + line_len => 0, + length => 0) + }, $class; +} + +# Adds elements to the formatted output. +# Splits content into multiple lines when the limit is reached. +# Each completed line is pushed into the lines array. +sub push +{ + my $pretty = shift; + my $lines = $pretty->{lines}; + my $line_limit = $pretty->{line_limit}; + my $indent = $pretty->{indent}; + + foreach (@_) + { + my $value = $_; + my $length = length($value) + 2; # 2 == ", " + my $line_len = $pretty->{line_len} + $length; + + # Group the characters into a single line until the line size limit + # is reached. This will avoid unnecessary line breaks and indent. + if (@{ $pretty->{line} } && $line_len > $line_limit) + { + # Add a comma at the end of the last line, as it is not the last one. + $lines->[-1] .= "," if @$lines; + + CORE::push @$lines, $indent . join(", ", @{ $pretty->{line} }); + + $pretty->{line} = []; + $pretty->{line_len} = 0; + } + + CORE::push @{ $pretty->{line} }, $value; + $pretty->{line_len} += $length; + } + + $pretty->{length} += scalar @_; +} + +# Returns the total number of elements added so far. +sub length +{ + return $_[0]->{length}; +} + +# Produces the final formatted string. +sub result +{ + my $pretty = shift; + my $lines = $pretty->{lines}; + my $indent = $pretty->{indent}; + + # The line may not be complete (it has not reached the limit), so we add + # the remaining line to the total result. + if (scalar(@{ $pretty->{line} })) + { + $lines->[-1] .= "," if @$lines; + + CORE::push @$lines, $indent . join(", ", @{ $pretty->{line} }); + } + + return \@$lines; +} + +1; -- 2.39.5 (Apple Git-154)