From a40d249fd6a93c0c59de94de631c288259085fd5 Mon Sep 17 00:00:00 2001 From: Mercier Pierre-Olivier Date: Thu, 26 Sep 2013 08:58:49 +0200 Subject: [PATCH] Add a tinyglobing package --- ACU/Tinyglob.pm | 52 ++++++++++++++++++++++++++++++++++++++++++++++++ ACU/t/tinyglob.t | 22 ++++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 ACU/Tinyglob.pm create mode 100644 ACU/t/tinyglob.t diff --git a/ACU/Tinyglob.pm b/ACU/Tinyglob.pm new file mode 100644 index 0000000..d400ead --- /dev/null +++ b/ACU/Tinyglob.pm @@ -0,0 +1,52 @@ +#! /usr/bin/env perl + +package Tinyglob; + +use v5.10.1; +use strict; +use warnings; +use Carp; +use Exporter 'import'; + +our @EXPORT = qw(tinyglob); + +sub tinyglob +{ + my @str = split("", quotemeta(shift)); + my $res = ""; + + my $metaescape = 0; + + for (my $i = 0; $i <= $#str; $i++) + { + if ($str[$i] eq '\\') + { + $i += 1; + if ($str[$i] eq '\\') + { + $metaescape = ! $metaescape; + $res .= $str[$i]; + } + elsif ($metaescape && ($str[$i] eq '*' || $str[$i] eq '?')) { + $res .= $str[$i]; + $metaescape = 0; + } + elsif ($str[$i] eq '?') { + $res .= '.'; + } + elsif ($str[$i] eq '*') { + $res .= '.*'; + } + else { + croak "Invalid number of \\"; + } + } + else { + $res .= $str[$i]; + } + } + + return $res; +} + +1; diff --git a/ACU/t/tinyglob.t b/ACU/t/tinyglob.t new file mode 100644 index 0000000..9d497c5 --- /dev/null +++ b/ACU/t/tinyglob.t @@ -0,0 +1,22 @@ +use v5.10.1; +use strict; +use warnings; + +use Test::More; + +use lib "../"; + +BEGIN { + diag("Testing Tinyglob on perl $]"); + use_ok('ACU::Tinyglob'); +} + +use ACU::Tinyglob; + +is(Tinyglob::tinyglob("test"), "test"); +is(Tinyglob::tinyglob("\\*"), "\\*"); +is(Tinyglob::tinyglob("\\\\*"), "\\\\.*"); +is(Tinyglob::tinyglob("\\?"), "\\?"); +is(Tinyglob::tinyglob("\\\\?"), "\\\\."); + +done_testing();