aboutsummaryrefslogtreecommitdiff
path: root/challenge-021
diff options
context:
space:
mode:
authorJaldhar H. Vyas <jaldhar@braincells.com>2019-08-18 18:58:45 -0400
committerJaldhar H. Vyas <jaldhar@braincells.com>2019-08-18 18:58:45 -0400
commitf3f99794ffacb9bdba5bcb1d93be0b2d71aa6e56 (patch)
tree95a944b32dfe20321dcdf53cf5040d32a6534055 /challenge-021
parentf997fb18f9f7949b181fa5a445235ce32303e0d0 (diff)
downloadperlweeklychallenge-club-f3f99794ffacb9bdba5bcb1d93be0b2d71aa6e56.tar.gz
perlweeklychallenge-club-f3f99794ffacb9bdba5bcb1d93be0b2d71aa6e56.tar.bz2
perlweeklychallenge-club-f3f99794ffacb9bdba5bcb1d93be0b2d71aa6e56.zip
Challenge 21 by Jaldhar H. Vyas (except blog and p6/ch-2)
Diffstat (limited to 'challenge-021')
-rwxr-xr-xchallenge-021/jaldhar-h-vyas/perl5/ch-1.sh1
-rwxr-xr-xchallenge-021/jaldhar-h-vyas/perl5/ch-2.pl127
-rwxr-xr-xchallenge-021/jaldhar-h-vyas/perl6/ch-1.sh1
3 files changed, 129 insertions, 0 deletions
diff --git a/challenge-021/jaldhar-h-vyas/perl5/ch-1.sh b/challenge-021/jaldhar-h-vyas/perl5/ch-1.sh
new file mode 100755
index 0000000000..04d5a3f8df
--- /dev/null
+++ b/challenge-021/jaldhar-h-vyas/perl5/ch-1.sh
@@ -0,0 +1 @@
+perl -Mbigrat=e -e 'printf("%.15f\n", e);' \ No newline at end of file
diff --git a/challenge-021/jaldhar-h-vyas/perl5/ch-2.pl b/challenge-021/jaldhar-h-vyas/perl5/ch-2.pl
new file mode 100755
index 0000000000..6eacf013aa
--- /dev/null
+++ b/challenge-021/jaldhar-h-vyas/perl5/ch-2.pl
@@ -0,0 +1,127 @@
+#!/usr/bin/perl
+use warnings;
+use strict;
+use 5.010;
+
+my $dec_octet = qr / \d | [1-9]\d | 1\d\d | 2[0-4]\d | 25[0-5] /x;
+my $hex_octet = qr / [ 0-9 A-F a-f ]{2} /x;
+my $pct_encoded = qr/ % $hex_octet /x;
+my $sub_delim = qr/ [ ! $ & ' ( ) * + , ; = ] /x;
+my $scheme = qr/ [ A-Z a-z 0-9 + - . ] /x;
+my $unreserved = qr/ [ A-Z a-z 0-9 - . _ ~ ] /x;
+
+my $ipv4 = qr/$dec_octet \. $dec_octet \. $dec_octet \. $dec_octet /x;
+my $userinfo = qr/ $unreserved | $pct_encoded | $sub_delim | : /x;
+my $pchar = qr/ $userinfo | @ /x;
+my $path = qr / $pchar | \/ /x;
+my $query_or_fragment = qr/ $pchar | \/ | \? /x;
+
+sub parse {
+ my ($url) = @_;
+
+ $url =~ /
+ \A
+ (?<scheme>$scheme+)
+ :
+ (?:
+ (?<slashes>\/\/)?
+ (?: (?<userinfo> $userinfo+? ) @ )?
+ (?<host> $ipv4 | $unreserved+ )
+ (?: : (?<port> \d+ ) )?
+ )?
+ (?<path> $path+ )?
+ (?: \? (?<query> $query_or_fragment+ ) )?
+ (?: \# (?<fragment> $query_or_fragment+ ) )?
+ # \z
+ /msx;
+
+ my $parsed = {};
+ for my $part (qw/ scheme slashes userinfo host port path query fragment /) {
+ $parsed->{$part} = $+{$part} // undef;
+ }
+ return $parsed;
+}
+
+sub lowerCase {
+ my ($url) = @_;
+ my $result = $url;
+
+ $result->{scheme} =~ tr/A-Z/a-z/;
+ $result->{host} =~ tr/A-Z/a-z/;
+
+ return $result;
+}
+
+sub capitalizeEscape {
+ my ($url) = @_;
+ my $result = $url;
+
+ for my $part (qw/ userinfo path query fragment /) {
+ $result->{$part} =~ s/($pct_encoded)/\U$1/g;
+ }
+
+ return $result;
+}
+
+sub decodeUnreserved {
+ my ($url) = @_;
+ my $result = $url;
+
+ for my $part (qw/ userinfo path query fragment /) {
+ while ($result->{$part} =~ /($pct_encoded)/g) {
+ my $pct = $1;
+ my $hex = $pct;
+ $hex =~ s/%//;
+ if (hex($hex) =~ /$unreserved/) {
+ my $decoded = chr(hex($hex));
+ $result->{$part} =~ s/$pct/$decoded/;
+ }
+ }
+ }
+
+ return $result;
+}
+
+sub removeDefaultPort {
+ my ($url) = @_;
+ my $result = $url;
+
+ if ($result->{port} eq '80') {
+ $result->{port} = undef;
+ }
+
+ return $result;
+}
+sub reassemble {
+ my ($url) = @_;
+
+ my $result = "$url->{scheme}:";
+ $result .= $url->{slashes} // q{};
+ $result .= $url->{userinfo} // q{};
+ if (defined $url->{userinfo}) {
+ $result .= '@';
+ }
+ $result .= $url->{host} // q{};
+ if (defined $url->{port}) {
+ $result .= ":$url->{port}";
+ }
+ $result .= $url->{path} // q{};
+ $result .= "?$url->{query}" // q{};
+ $result .= "#$url->{fragment}" // q{};
+
+ return $result;
+}
+
+say reassemble(
+ decodeUnreserved(
+ capitalizeEscape(
+ removeDefaultPort(
+ lowerCase(
+ parse(
+ shift
+ )
+ )
+ )
+ )
+ )
+);
diff --git a/challenge-021/jaldhar-h-vyas/perl6/ch-1.sh b/challenge-021/jaldhar-h-vyas/perl6/ch-1.sh
new file mode 100755
index 0000000000..4b750cd42a
--- /dev/null
+++ b/challenge-021/jaldhar-h-vyas/perl6/ch-1.sh
@@ -0,0 +1 @@
+perl6 -e '𝑒.say;' \ No newline at end of file