Aion-Format
view release on metacpan or search on metacpan
lib/Aion/Format/Url.pm view on Meta::CPAN
package Aion::Format::Url;
use common::sense;
use List::Util qw//;
use Encode qw//;
use Exporter qw/import/;
our @EXPORT = our @EXPORT_OK = grep {
ref \$Aion::Format::Url::{$_} eq "GLOB"
&& *{$Aion::Format::Url::{$_}}{CODE} && !/^(_|(NaN|import)\z)/n
} keys %Aion::Format::Url::;
#@category escape url
use constant UNSAFE_RFC3986 => qr/[^A-Za-z0-9\-\._~]/;
# ÐÑÐºÐµÐ¹Ð¿Ð¸Ñ Ð·Ð½Ð°Ñение
sub to_url_param(;$) {
my ($param) = @_ == 0? $_: @_;
use bytes;
$param =~ s/${\ UNSAFE_RFC3986}/$& eq " "? "+": sprintf "%%%02X", ord $&/ge;
$param
}
# ÐÑеобÑазÑÐµÑ Ð² ÑоÑÐ¼Ð°Ñ url-паÑамеÑÑов
sub to_url_params(;$) {
my ($param) = @_ == 0? $_: @_;
my @R;
my @S = [$param];
while(@S) {
my $u = pop @S;
my ($x, $key) = @$u;
if(ref $x eq "HASH") {
push @S, defined($key)
? (map [$x->{$_}, "$key\[${\to_url_param}]"], sort keys %$x)
: (map [$x->{$_}, to_url_param], sort keys %$x)
;
}
elsif(ref $x eq "ARRAY") {
my $i = '';
push @S, map [$_, "$key\[${\($i++)}]"], @$x;
}
elsif(!defined $x) {}
elsif($x eq 1) { unshift @R, $key }
else {
unshift @R, join "=", $key, to_url_param $x;
}
}
join "&", @R
}
# ÐпÑеделÑÐµÑ ÐºÐ¾Ð´Ð¸ÑовкÑ. Ð koi8-r и в cp1251 болÑÑие и малÑе бÑÐºÐ²Ñ ÐºÐ°Ðº Ð±Ñ Ð¿Ð¾Ð¼ÐµÐ½ÑлиÑÑ Ð¼ÐµÑÑами, поÑÑÐ¾Ð¼Ñ Ñ Ð¿ÑавилÑной кодиÑовки Ð²ÐµÑ Ð±ÑÐ´ÐµÑ Ð±Ð¾Ð»ÑÑе
sub _bohemy {
my ($s) = @_;
my $c = 0;
while($s =~ /[а-ÑÑ]+/gi) {
my $x = $&;
if($x =~ /^[Ð-ЯÐа-ÑÑ][а-ÑÑ]*$/) { $c += length $x } else { $c -= length $x }
}
$c
}
sub from_url_param(;$) {
my ($param) = @_ == 0? $_: @_;
utf8::encode($param) if utf8::is_utf8($param);
{
no utf8;
use bytes;
( run in 0.859 second using v1.01-cache-2.11-cpan-99c4e6809bf )