/usr/share/perl5/String/Truncate.pm is in libstring-truncate-perl 1.100600-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 | use strict;
use warnings;
package String::Truncate;
BEGIN {
$String::Truncate::VERSION = '1.100600';
}
# ABSTRACT: a module for when strings are too long to be displayed in...
use Carp qw(croak);
use Sub::Install 0.03 qw(install_sub);
my %elider_for = (
right => \&_elide_right,
left => \&_elide_left,
middle => \&_elide_middle,
ends => \&_elide_ends,
);
sub _elide_right {
&_assert_1ML; ## no critic Ampersand
my ($string, $length, $marker, $at_space) = @_;
my $keep = $length - length($marker);
if ($at_space) {
my ($substr) = $string =~ /\A(.{0,$keep})\s/s;
$substr = substr($string, 0, $keep)
unless defined $substr and length $substr;
return $substr . $marker;
} else {
return substr($string, 0, $keep) . $marker;
}
}
sub _elide_left {
&_assert_1ML; ## no critic Ampersand
my ($string, $length, $marker, $at_space) = @_;
my $keep = $length - length($marker);
return $marker
. reverse(_elide_right(scalar reverse($string), $keep, q{}, $at_space));
}
sub _elide_middle {
&_assert_1ML; ## no critic Ampersand
my ($string, $length, $marker, $at_space) = @_;
my $keep = $length - length($marker);
my ($keep_left, $keep_right) = (int($keep / 2)) x 2;
$keep_left +=1 if ($keep_left + $keep_right) < $keep;
return _elide_right($string, $keep_left, q{}, $at_space)
. $marker
. _elide_left($string, $keep_right, q{}, $at_space)
}
sub _elide_ends {
&_assert_2ML; ## no critic Ampersand
my ($string, $length, $marker, $at_space) = @_;
my $midpoint = int(length($string) / 2);
my $each = int($length / 2);
return _elide_left(substr($string, 0, $midpoint), $each, $marker, $at_space)
. _elide_right(substr($string, -$midpoint), $each, $marker, $at_space)
}
sub _assert_1ML {
my ($string, $length, $marker) = @_;
croak "elision marker <$marker> is longer than allowed length $length!"
if length($marker) > $length;
}
sub _assert_2ML {
my ($string, $length, $marker) = @_;
# this should only complain if needed: elide('foobar', 3, {marker=>'...'})
# should be ok -- rjbs, 2006-02-24
croak "two elision markers <$marker> are longer than allowed length $length!"
if (length($marker) * 2) > $length;
}
sub elide {
my ($string, $length, $arg) = @_;
$arg = {} unless $arg;
my $truncate = $arg->{truncate} || 'right';
croak "invalid value for truncate argument: $truncate"
unless my $elider = $elider_for{ $truncate };
# hey, this might be really easy:
return $string if length($string) <= $length;
my $marker = defined $arg->{marker} ? $arg->{marker} : '...';
my $at_space = defined $arg->{at_space} ? $arg->{at_space} : 0;
return $elider->($string, $length, $marker, $at_space);
}
sub trunc {
my ($string, $length, $arg) = @_;
$arg = {} unless $arg;
croak "marker may not be passed to trunc()" if exists $arg->{marker};
$arg->{marker} = q{};
return elide($string, $length, $arg);
}
use Sub::Exporter::Util ();
use Sub::Exporter 0.953 -setup => {
exports => {
Sub::Exporter::Util::merge_col(defaults => {
trunc => sub { trunc_with_defaults($_[2]) },
elide => sub { elide_with_defaults($_[2]) },
})
},
collectors => [ qw(defaults) ]
};
sub _code_with_defaults {
my ($code, $skip_defaults) = @_;
sub {
my $defaults = shift || {};
my %defaults = %$defaults;
delete $defaults{$_} for @$skip_defaults;
my $length = delete $defaults{length};
sub {
my $string = $_[0];
my $length = defined $_[1] ? $_[1] : $length;
my $arg = { %defaults, (defined $_[2] ? %{ $_[2] } : ()) };
return $code->($string, $length, $arg);
}
}
}
BEGIN {
install_sub({
code => _code_with_defaults(\&elide),
as => 'elide_with_defaults',
});
}
BEGIN {
install_sub({
code => _code_with_defaults(\&trunc, ['marker']),
as => 'trunc_with_defaults',
});
}
1; # End of String::Truncate
__END__
=pod
=head1 NAME
String::Truncate - a module for when strings are too long to be displayed in...
=head1 VERSION
version 1.100600
=head1 SYNOPSIS
This module handles the simple but common problem of long strings and finite
terminal width. It can convert:
"this is your brain" -> "this is your ..."
or "...is your brain"
or "this is... brain"
or "... is your b..."
It's simple:
use String::Truncate qw(elide);
my $brain = "this is your brain";
elide($brain, 16); # first option
elide($brain, 16, { truncate => 'left' }); # second option
elide($brain, 16, { truncate => 'middle' }); # third option
elide($brain, 16, { truncate => 'ends' }); # fourth option
String::Trunc::trunc($brain, 16); # => "this is your bra"
=head1 FUNCTIONS
=head2 elide
elide($string, $length, \%arg)
This function returns the string, if it is less than or equal to C<$length>
characters long. If it is longer, it truncates the string and marks the
elision.
Valid arguments are:
truncate - elide at left, right, middle, or ends? (default: right)
marker - how to mark the elision (default: ...)
at_space - if true, strings will be broken at whitespace if possible
=head2 trunc
trunc($string, $length, \%arg)
This acts just like C<elide>, but assumes an empty marker, so it actually
truncates the string normally.
=head1 IMPORTING
String::Truncate exports both C<elide> and C<trunc>, and also supports the
Exporter-style ":all" tag.
use String::Truncate (); # export nothing
use String::Truncate qw(elide); # export just elide()
use String::Truncate qw(:all); # export both elide() and trunc()
use String::Truncate qw(-all); # export both elide() and trunc()
When exporting, you may also supply default values:
use String::Truncate -all => defaults => { length => 10, marker => '--' };
# or
use String::Truncate -all => { length => 10, marker => '--' };
These values affect only the imported version of the functions. You may pass
arguments as usual to override them, and you may call the subroutine by its
fully-qualified name to get the standard behavior.
=head1 BUILDING CODEREFS
The imported builds and installs lexical closures (code references) that merge
in given values to the defaults. You can build your own closures without
importing them into your namespace. To do this, use the C<elide_with_defaults>
and C<trunc_with_defaults> routines.
=head2 elide_with_defaults
my $elider = String::Truncate::elide_with_defaults(\%arg);
This routine, never exported, builds a coderef which behaves like C<elide>, but
uses default values when needed. All the valud arguments to C<elide> are valid
here, as well as C<length>.
=head2 trunc_with_defaults
This routine behaves exactly like elide_with_defaults, with one obvious
exception: it retuns code that works like C<trunc> rather than C<elide>. If a
C<marker> argument is passed, it is ignored.
=head1 SEE ALSO
L<Text::Truncate> does a very similar thing. So does L<Text::Elide>.
=head1 BUGS
Please report any bugs or feature requests through the web interface at
L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=String-Truncate>. I will be
notified, and then you'll automatically be notified of progress on your bug as
I make changes.
=head1 ACKNOWLEDGEMENTS
Ian Langworth gave me some good advice about naming things. (Also some bad
jokes. Nobody wants String::ETOOLONG, Ian.) Hans Dieter Pearcey suggested
allowing defaults just in time for a long bus ride, and I was rescued from
boredom by that suggestion
=head1 AUTHOR
Ricardo Signes <rjbs@cpan.org>
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2011 by Ricardo Signes.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
=cut
|