Pcore
view release on metacpan or search on metacpan
lib/Pcore/Util/Text.pm view on Meta::CPAN
package Pcore::Util::Text;
use Pcore -ansi, -export;
use Encode qw[]; ## no critic qw[Modules::ProhibitEvilModules]
use Text::Xslate qw[mark_raw unmark_raw];
our $ENC_CACHE = {};
our $EXPORT = [ qw[
cut
cut_all
decode_eol
decode_html_entities
decode_utf8
encode_hex
encode_html
encode_html_attr
encode_js_string
encode_utf8
escape_perl
expand_num
add_num_sep
fullchomp
lcut
lcut_all
ltrim
ltrim_multi
mark_raw
rcut
rcut_all
remove_ansi
remove_bom
rtrim
rtrim_multi
table
to_camel_case
to_snake_case
trim
trim_multi
unmark_raw
wrap
] ];
# TODO
# - crunch - ?;
# - P->text - clear trim functions names, eg, P->text->rcut_all -> P->text->trim_trailing_hs
# - autogenerated functions should always return ScalarRef if wantarray;
our $CODE = {
decode_eol => <<'PERL', # convert EOL to internal \n representation
s/\x0D?\x0A/\n/smg;
PERL
remove_bom => <<'PERL', # remove BOM
s/\A(?:\x00\x00\xFE\xFF|\xFF\xFE\x00\x00|\xFE\xFF|\xFF\xFE|\xEF\xBB\xBF)//sm;
PERL
fullchomp => <<'PERL',
s/(?:\x0D|\x0A)+\z//sm;
PERL
# "trim" functions removes spaces and tabs
trim => <<'PERL',
s/\A\h+//sm; # ltrim
s/\h+\z//sm; # rtrim
PERL
ltrim => <<'PERL', # treats string as single-line, remove all \h (space, tab) before first \n, non-space or non-tab character)
s/\A\h+//sm;
PERL
rtrim => <<'PERL', # treats string as single-line, remove all \h (space, tab) after last \n, non-space or non-tab character
s/\h+\z//sm;
PERL
trim_multi => <<'PERL',
s/^\h+//smg; # ltrim_multi
s/\h+$//smg; # rtrim_multi
PERL
ltrim_multi => <<'PERL', # treats string as multi-line, remove \h just after each \n or string begin
s/^\h+//smg;
PERL
rtrim_multi => <<'PERL', # treats string as multi-line, remove \h before each \n
s/\h+$//smg;
PERL
# "cut" functions compress several \n to one \n
cut => <<'PERL', # replace all \n series with single \n
s/\A\v+//sm; # lcut
s/\v+\z//sm; # rcut
s/\v+/\n/smg;
PERL
lcut => <<'PERL', # treats string as single-line, cut all \n before first character
s/\A\v+//sm;
PERL
rcut => <<'PERL', # treats string as single-line, remove all \n after last character, including last \n
s/\v+\z//sm;
PERL
# "cut_all" functions combines trim and cut functionality together
cut_all => <<'PERL', # trim_multi + cut
# trim_multi
s/^\h+//smg; # ltrim_multi
s/\h+$//smg; # rtrim_multi
# cut
s/\A\v+//sm; # lcut
s/\v+\z//sm; # rcut
s/\v+/\n/smg;
PERL
lcut_all => <<'PERL', # remove empty lines and lines, consisting only of spaces and tabs, from string start
s/\A\s+//sm;
PERL
rcut_all => <<'PERL', # remove empty lines and lines, consisting only of spaces and tabs, from string end, including last \n
s/\s+\z//sm;
PERL
( run in 1.685 second using v1.01-cache-2.11-cpan-39bf76dae61 )