/usr/share/perl5/SHARYANTO/String/Util.pm is in libsharyanto-string-util-perl 0.26-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 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 | package SHARYANTO::String::Util;
use 5.010001;
use strict;
use warnings;
our $VERSION = '0.26'; # VERSION
use Exporter;
our @ISA = qw(Exporter);
our @EXPORT_OK = qw(
ltrim
rtrim
trim
ltrim_lines
rtrim_lines
trim_lines
trim_blank_lines
ellipsis
indent
linenum
pad
qqquote
);
sub ltrim {
my $str = shift;
$str =~ s/\A\s+//s;
$str;
}
sub rtrim {
my $str = shift;
$str =~ s/\s+\z//s;
$str;
}
sub trim {
my $str = shift;
$str =~ s/\A\s+//s;
$str =~ s/\s+\z//s;
$str;
}
sub ltrim_lines {
my $str = shift;
$str =~ s/^[ \t]+//mg; # XXX other unicode non-newline spaces
$str;
}
sub rtrim_lines {
my $str = shift;
$str =~ s/[ \t]+$//mg;
$str;
}
sub trim_lines {
my $str = shift;
$str =~ s/^[ \t]+//mg;
$str =~ s/[ \t]+$//mg;
$str;
}
sub trim_blank_lines {
local $_ = shift;
return $_ unless defined;
s/\A(?:\n\s*)+//;
s/(?:\n\s*){2,}\z/\n/;
$_;
}
sub ellipsis {
my ($str, $maxlen, $ellipsis) = @_;
$maxlen //= 80;
$ellipsis //= "...";
if (length($str) <= $maxlen) {
return $str;
} else {
return substr($str, 0, $maxlen-length($ellipsis)) . $ellipsis;
}
}
sub indent {
my ($indent, $str, $opts) = @_;
$opts //= {};
if ($opts->{indent_blank_lines} // 1) {
$str =~ s/^/$indent/mg;
} else {
$str =~ s/^([^\r\n]*\S[^\r\n]*)/$indent$1/mg;
}
$str;
}
sub linenum {
my ($str, $opts) = @_;
$opts //= {};
$opts->{width} //= 4;
$opts->{zeropad} //= 0;
$opts->{skip_empty} //= 1;
my $i = 0;
$str =~ s/^(([\t ]*\S)?.*)/
sprintf(join("",
"%",
($opts->{zeropad} && !($opts->{skip_empty}
&& !defined($2)) ? "0" : ""),
$opts->{width}, "s",
"|%s"),
++$i && $opts->{skip_empty} && !defined($2) ? "" : $i,
$1)/meg;
$str;
}
sub pad {
my ($text, $width, $which, $padchar, $is_trunc) = @_;
if ($which) {
$which = substr($which, 0, 1);
} else {
$which = "r";
}
$padchar //= " ";
my $w = length($text);
if ($is_trunc && $w > $width) {
$text = substr($text, 0, $width, 1);
} else {
if ($which eq 'l') {
$text = ($padchar x ($width-$w)) . $text;
} elsif ($which eq 'c') {
my $n = int(($width-$w)/2);
$text = ($padchar x $n) . $text . ($padchar x ($width-$w-$n));
} else {
$text .= ($padchar x ($width-$w));
}
}
$text;
}
# BEGIN COPY PASTE FROM Data::Dump
my %esc = (
"\a" => "\\a",
"\b" => "\\b",
"\t" => "\\t",
"\n" => "\\n",
"\f" => "\\f",
"\r" => "\\r",
"\e" => "\\e",
);
# put a string value in double quotes
sub qqquote {
local($_) = $_[0];
# If there are many '"' we might want to use qq() instead
s/([\\\"\@\$])/\\$1/g;
return qq("$_") unless /[^\040-\176]/; # fast exit
s/([\a\b\t\n\f\r\e])/$esc{$1}/g;
# no need for 3 digits in escape for these
s/([\0-\037])(?!\d)/sprintf('\\%o',ord($1))/eg;
s/([\0-\037\177-\377])/sprintf('\\x%02X',ord($1))/eg;
s/([^\040-\176])/sprintf('\\x{%X}',ord($1))/eg;
return qq("$_");
}
# END COPY PASTE FROM Data::Dump
1;
# ABSTRACT: String utilities
__END__
=pod
=encoding utf-8
=head1 NAME
SHARYANTO::String::Util - String utilities
=head1 DESCRIPTION
=head1 FUNCTIONS
=head2 ltrim($str) => STR
Trim whitespaces (including newlines) at the beginning of string. Equivalent to:
$str =~ s/\A\s+//s;
=head2 ltrim_lines($str) => STR
Trim whitespaces (not including newlines) at the beginning of each line of
string. Equivalent to:
$str =~ s/^\s+//mg;
=head2 rtrim($str) => STR
Trim whitespaces (including newlines) at the end of string. Equivalent to:
$str =~ s/[ \t]+\z//s;
=head2 rtrim_lines($str) => STR
Trim whitespaces (not including newlines) at the end of each line of
string. Equivalent to:
$str =~ s/[ \t]+$//mg;
=head2 trim($str) => STR
ltrim + rtrim.
=head2 trim_lines($str) => STR
ltrim_lines + rtrim_lines.
=head2 trim_blank_lines($str) => STR
Trim blank lines at the beginning and the end. Won't trim blank lines in the
middle. Blank lines include lines with only whitespaces in them.
=head2 ellipsis($str[, $maxlen, $ellipsis]) => STR
Return $str unmodified if $str's length is less than $maxlen (default 80).
Otherwise cut $str to ($maxlen - length($ellipsis)) and append $ellipsis
(default '...') at the end.
=head2 indent($indent, $str, \%opts) => STR
Indent every line in $str with $indent. Example:
indent(' ', "one\ntwo\nthree") # " one\n two\n three"
%opts is optional. Known options:
=over 4
=item * indent_blank_lines => BOOL (default 1)
If set to false, does not indent blank lines (i.e., lines containing only zero
or more whitespaces).
=back
=head2 linenum($str, \%opts) => STR
Add line numbers. For example:
1|line1
2|line2
|
4|line4
Known options:
=over 4
=item * width => INT (default: 4)
=item * zeropad => BOOL (default: 0)
If turned on, will output something like:
0001|line1
0002|line2
|
0004|line4
=item * skip_empty => BOOL (default: 1)
If set to false, keep printing line number even if line is empty:
1|line1
2|line2
3|
4|line4
=back
=head2 pad($text, $width[, $which[, $padchar[, $truncate]]]) => STR
Return C<$text> padded with C<$padchar> to C<$width> columns. C<$which> is
either "r" or "right" for padding on the right (the default if not specified),
"l" or "left" for padding on the right, or "c" or "center" or "centre" for
left+right padding to center the text.
C<$padchar> is whitespace if not specified. It should be string having the width
of 1 column.
=head2 qqquote($str) => STR
Quote or encode C<$str> to the Perl double quote (C<qq>) literal representation
of the string. Example:
say qqquote("a"); # => "a"
say qqquote("a\n"); # => "a\n"
say qqquote('"'); # => "\""
say qqquote('$foo'); # => "\$foo"
This code is taken from C<quote()> in L<Data::Dump>. Maybe I didn't look more
closely, but I couldn't a module that provides a function to do something like
this. L<String::Escape>, for example, provides C<qqbackslash> but it does not
escape C<$>.
None are exported by default, but they are exportable.
=head1 SEE ALSO
L<SHARYANTO>
=head1 HOMEPAGE
Please visit the project's homepage at L<https://metacpan.org/release/SHARYANTO-String-Util>.
=head1 SOURCE
Source repository is at L<HASH(0x353ed90)>.
=head1 BUGS
Please report any bugs or feature requests on the bugtracker website
https://rt.cpan.org/Public/Dist/Display.html?Name=SHARYANTO-String-Util
When submitting a bug or request, please include a test-file or a
patch to an existing test-file that illustrates the bug or desired
feature.
=head1 AUTHOR
Steven Haryanto <stevenharyanto@gmail.com>
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2013 by Steven Haryanto.
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
|