Skip to content

Instantly share code, notes, and snippets.

@kriskowal
Created October 5, 2009 20:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kriskowal/202458 to your computer and use it in GitHub Desktop.
Save kriskowal/202458 to your computer and use it in GitHub Desktop.
golf () {
echo ip: $1;
echo -n "A: "
perl -e '$p=qr!(?:0|1\d{0,2}|2(?:[0-4]\d?|5[0-5]?|[6-9])?|[3-9]\d?)!;print((shift=~m/^$p\.$p\.$p\.$p$/)?1:0);' $1; echo
echo -n "B: "
perl -e '$p=qr!(?:0|1\d{0,2}|2(?:[0-4]\d?|5[0-5]?|[6-9])?|[3-9]\d?)!;exit shift !~ m/^$p\.$p\.$p\.$p$/;' $1 && echo pass || echo fail
echo -n "C: "
perl -e '$p=qr{(?:0|1\d{0,2}|2(?:[0-4]\d?|5[0-5]?|[6-9])?|[3-9]\d?)};exit shift !~ m/^$p\.$p\.$p\.$p$/;' $1 && echo pass || echo fail
echo -n "D: "
perl -e '$p=qr{(\d+)}; exit not (shift =~ m/^$p\.$p\.$p\.$p$/ and not grep {$_ < 0 or $_ >= 256} $1, $2, $3, $4);' $1 && echo pass || e
echo -n "E: "
perl -e '@_ = split /\./, shift; exit -1 if @_ != 4 or grep {!/^\d+$/ or $_ < 0 or $_ >= 256} @_' $1 && echo pass || echo fail
echo -n "F: "
perl -e '@_=split/\./,shift;exit -1 if @_!=4 or grep{!/^\d+$/ or $_< 0 or $_>=256}@_' $1 && echo pass || echo fail
}
golf "1.2.3.4"
golf "1.2.3"
golf "1.2.3.4.5"
golf "1.2.3.256"
golf "1.2.3.a"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment