/usr/share/perl5/NetSDS/Util/String.pm is in libnetsds-util-perl 1.044-2.
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 347 348 349 350 | #===============================================================================
#
# FILE: String.pm
#
# DESCRIPTION: Utilities for easy string processing
#
# NOTE: This module ported from Wono framework
# AUTHOR: Michael Bochkaryov (Rattler), <misha@rattler.kiev.ua>
# COMPANY: Net.Style
# VERSION: 1.044
# CREATED: 03.08.2008 15:04:22 EEST
#===============================================================================
=head1 NAME
NetSDS::Util::String - string prcessing routines
=head1 SYNOPSIS
use NetSDS::Util::String qw();
# Read from standard input
my $string = <STDIN>;
# Encode string to internal structure
$string = string_encode($tring);
=head1 DESCRIPTION
C<NetSDS::Util::String> module contains functions may be used to quickly solve
string processing tasks like parsing, recoding, formatting.
As in other NetSDS modules standard encoding is UTF-8.
=cut
package NetSDS::Util::String;
use 5.8.0;
use warnings 'all';
use strict;
use base 'Exporter';
use version; our $VERSION = '1.044';
our @EXPORT = qw(
str_encode
str_decode
str_recode
str_trim
str_trim_left
str_trim_right
str_clean
str_camelize
str_decamelize
);
use POSIX;
use Encode qw(
encode
decode
encode_utf8
decode_utf8
from_to
is_utf8
);
my $BLANK = "[:blank:][:space:][:cntrl:]";
use constant DEFAULT_ENCODING => 'UTF-8';
#***********************************************************************
#
# ENCODING/DECODING/RECODING FUNCTIONS
#
#***********************************************************************
=head1 EXPORTED FUNCTIONS
=over
=item B<str_encode($str[, $encoding])> - encode string to internal UTF-8
By default this function treat first argument as byte string in UTF-8
and return it's internal Unicode representation.
In case of external character set isn't UTF-8 it should be added as second
argument of function.
# Convert UTF-8 byte string to internal Unicode representation
$uni_string = str_encode($byte_string);
# Convert KOI8-U byte string to internal
$uni_string = str_encode($koi8_string, 'KOI8-U');
After C<str_encode()> it's possible to process this string correctly
including regular expressions. All characters will be understood
as UTF-8 symbols instead of byte sequences.
=cut
#-----------------------------------------------------------------------
sub str_encode {
my ( $txt, $enc ) = @_;
if ( defined($txt) and ( $txt ne '' ) ) {
unless ( is_utf8($txt) ) {
$txt = decode( $enc || DEFAULT_ENCODING, $txt );
}
}
return $txt;
}
#***********************************************************************
=item B<str_decode($str[, $encoding])> - decode internal UTF-8 to byte string
By default this function treat first argument as string in internal UTF-8
and return it in byte string (external) representation.
In case of external character set isn't UTF-8 it should be added as second
argument of function.
# Get UTF-8 byte string from internal Unicode representation
$byte_string = str_decode($uni_string);
# Convert to KOI8-U byte string from internal Unicode
$koi8_string = str_encode($uni_string, 'KOI8-U');
It's recommended to use C<str_encode()> when preparing data for
communication with external systems (especially networking).
=cut
#-----------------------------------------------------------------------
sub str_decode {
my ( $txt, $enc ) = @_;
if ( defined($txt) and ( $txt ne '' ) ) {
if ( is_utf8($txt) ) {
$txt = encode( $enc || DEFAULT_ENCODING, $txt );
}
}
return $txt;
}
#***********************************************************************
=item B<str_recode($str, $FROM_ENC[, $TO_ENC])> - recode string
Translate string between different encodings.
If target encoding is not set UTF-8 used as default one.
=cut
#-----------------------------------------------------------------------
sub str_recode {
my ( $txt, $enc, $trg ) = @_;
if ( defined($txt) and ( $txt ne '' ) ) {
if ($enc) {
my $len = from_to( $txt, $enc, $trg || DEFAULT_ENCODING );
unless ( defined($len) ) {
$txt = undef;
}
}
}
return $txt;
}
#***********************************************************************
#
# CLEANING STRINGS
#
#***********************************************************************
=item B<str_trim($str)> - remove leading/trailing space characters
$orig_str = " string with spaces ";
$new_str = str_trim($orig_str);
# Output: "string with spaces"
print $new_str;
=cut
#-----------------------------------------------------------------------
sub str_trim {
my ($s) = @_;
if ( defined($s) and ( $s ne '' ) ) {
$s =~ s/^[$BLANK]+//s;
$s =~ s/[$BLANK]+$//s;
}
return $s;
}
#***********************************************************************
=item B<str_trim_left($str)> - removes leading whitespaces
This function is similar to C<str_trim()> except of it removes only
leading space characters and leave trailing ones.
=cut
#-----------------------------------------------------------------------
sub str_trim_left {
my ($s) = @_ ? @_ : $_;
if ( defined($s) and ( $s ne '' ) ) {
$s =~ s/^[$BLANK]+//s;
}
return $s;
}
#***********************************************************************
=item B<str_trim_right($str)> - removes trailing whitespaces
This function is similar to C<str_trim()> except of it removes only
trailing space characters and leave leading ones.
=cut
#-----------------------------------------------------------------------
sub str_trim_right {
my ($s) = @_ ? @_ : $_;
if ( defined($s) and ( $s ne '' ) ) {
$s =~ s/[$BLANK]+$//s;
}
return $s;
}
#***********************************************************************
=item B<str_clean($str)> - clean string from extra spaces
Function is similar to C<str_trim()> but also changes all spacing chains
inside string to single spaces.
=cut
#-----------------------------------------------------------------------
sub str_clean {
my ($txt) = @_;
if ( defined($txt) and ( $txt ne '' ) ) {
$txt =~ s/^[$BLANK]+//s;
$txt =~ s/[$BLANK]+$//s;
$txt =~ s/[$BLANK]+/ /gs;
}
return $txt;
}
#**************************************************************************
=item B<str_camelize($strin)>
If pass undef - return undef.
If pass '' - return ''.
Examples:
# returns 'getValue'
str_camelize( 'get_value' )
# returns 'addUserAction'
str_camelize( 'ADD_User_actION' )
=cut
#-----------------------------------------------------------------------
sub str_camelize {
my $s = shift;
if ( defined($s) and ( $s ne '' ) ) {
$s = lc($s);
$s =~ s/_([0-9a-z])/\U$1/g;
}
return $s;
}
#**************************************************************************
=item B<str_decamelize(...)>
If pass undef - return undef.
If pass '' - return ''.
Examples:
# returns 'get_value'
str_decamelize( 'getValue' )
=cut
#-----------------------------------------------------------------------
sub str_decamelize {
my $s = shift;
$s =~ s/([A-Z])/_\L$1/g;
return lc($s);
}
1;
__END__
=back
=head1 EXAMPLES
None yet
=head1 BUGS
Unknown yet
=head1 TODO
Implement examples and tests.
=head1 SEE ALSO
L<Encode>, L<perlunicode>
=head1 AUTHORS
Valentyn Solomko <pere@pere.org.ua>
Michael Bochkaryov <misha@rattler.kiev.ua>
=cut
|