TextGlob.pm 5.82 KB
 Stefan Tauner committed May 04, 2018 1 2 3 #----------------------------------------------------------------------- # Fault InJection Instrumenter (FIJI) # https://embsys.technikum-wien.at/projects/vecs/fiji  Christian Fibich committed May 04, 2018 4 #  Stefan Tauner committed May 04, 2018 5 6 7 8 # The creation of this file has been supported by the publicly funded # R&D project Josef Ressel Center for Verification of Embedded Computing # Systems (VECS) managed by the Christian Doppler Gesellschaft (CDG). #  Stefan Tauner committed May 04, 2018 9 10 11 # Authors: # Christian Fibich # Stefan Tauner  Christian Fibich committed May 04, 2018 12 #  Stefan Tauner committed May 04, 2018 13 14 # This module is free software; you can redistribute it and/or modify # it under the same terms as Perl itself.  Christian Fibich committed May 04, 2018 15 #  Stefan Tauner committed May 04, 2018 16 17 18 # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  Christian Fibich committed May 04, 2018 19 #  Stefan Tauner committed May 04, 2018 20 21 22 23 24 25 26 27 28 # See the LICENSE file for more details. #----------------------------------------------------------------------- ## @file Tests.pm # @brief Contains class \ref FIJI::TextGlob ## @class FIJI::Tests # # @brief Glob strings to regexes based on Text::Glob by Richard Clamp  Christian Fibich committed May 04, 2018 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46  package FIJI::TextGlob; use strict; use Exporter; use vars qw/$VERSION @ISA @EXPORT_OK$strict_leading_dot $strict_wildcard_slash/;$VERSION = '0.11'; @ISA = 'Exporter'; @EXPORT_OK = qw( glob_to_regex glob_to_regex_string match_glob ); $strict_leading_dot = 1;$strict_wildcard_slash = 1; use constant debug => 0; sub glob_to_regex { my $glob = shift; my$regex = glob_to_regex_string($glob);  Stefan Tauner committed May 04, 2018 47  return "^$regex\$";  Christian Fibich committed May 04, 2018 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 } sub glob_to_regex_string { my$glob = shift; my $seperator =$Text::Glob::seperator; $seperator = "/" unless defined$seperator; $seperator = quotemeta($seperator); my ($regex,$in_curlies, $escaping); local$_; my $first_byte = 1; for ($glob =~ m/(.)/gs) { if ($first_byte) { if ($strict_leading_dot) { $regex .= '(?=[^\.])' unless$_ eq '.'; } $first_byte = 0; } if ($_ eq '/') { $first_byte = 1; } if ($_ eq '.' || $_ eq '(' ||$_ eq ')' || $_ eq '|' ||$_ eq '+' || $_ eq '^' ||$_ eq '$' ||$_ eq '@' || $_ eq '%' ) {$regex .= "\\$_"; } elsif ($_ eq '*') { $regex .=$escaping ? "\\*" : $strict_wildcard_slash ? "(?:(?!$seperator).)*" : ".*"; } elsif ($_ eq '?') {$regex .= $escaping ? "\\?" :$strict_wildcard_slash ? "(?!$seperator)." : "."; } elsif ($_ eq '{') { $regex .=$escaping ? "\\{" : "("; ++$in_curlies unless$escaping; } elsif ($_ eq '}' &&$in_curlies) { $regex .=$escaping ? "}" : ")"; --$in_curlies unless$escaping; } elsif ($_ eq ',' &&$in_curlies) { $regex .=$escaping ? "," : "|"; } elsif ($_ eq "\\") { if ($escaping) { $regex .= "\\\\";$escaping = 0; } else { $escaping = 1; } next; } elsif ($_ eq "[") { $regex .= "\\["; } else {$regex .= $_;$escaping = 0; } $escaping = 0; } print "#$glob $regex\n" if debug; return$regex; } sub match_glob { print "# ", join(', ', map { "'$_'" } @_), "\n" if debug; my$glob = shift; my $regex = glob_to_regex$glob; local $_; grep {$_ =~ $regex } @_; } 1; __END__ =head1 NAME Text::Glob - match globbing patterns against text =head1 SYNOPSIS use FIJI::TextGlob qw( match_glob glob_to_regex ); print "matched\n" if match_glob( "foo.*", "foo.bar" ); # prints foo.bar and foo.baz my$regex = glob_to_regex( "foo.*" ); for ( qw( foo.bar foo.baz foo bar ) ) { print "matched: $_\n" if /$regex/; } =head1 DESCRIPTION Text::Glob implements glob(3) style matching that can be used to match against text, rather than fetching names from a filesystem. If you want to do full file globbing use the File::Glob module instead. =head2 Routines =over =item match_glob( $glob, @things_to_test ) Returns the list of things which match the glob from the source list. =item glob_to_regex($glob ) Returns a compiled regex which is the equivalent of the globbing pattern. =item glob_to_regex_string( $glob ) Returns a regex string which is the equivalent of the globbing pattern. =back =head1 SYNTAX The following metacharacters and rules are respected. =over =item C<*> - match zero or more characters C matches C, C, C and many many more. =item C - match exactly one character C matches C, but not C, or C =item Character sets/ranges C matches C and C C matches C, C, and C =item alternation C matches C, C, and C =item leading . must be explicitly matched C<*.foo> does not match C<.bar.foo>. For this you must either specify the leading . in the glob pattern (C<.*.foo>), or set C<$Text::Glob::strict_leading_dot> to a false value while compiling the regex. =item C<*> and C do not match the seperator (i.e. do not match C) C<*.foo> does not match C. For this you must either explicitly match the / in the glob (C<*/*.foo>), or set C<$Text::Glob::strict_wildcard_slash> to a false value while compiling the regex, or change the seperator that Text::Glob uses by setting C<$Text::Glob::seperator> to an alternative value while compiling the the regex. =back =head1 COPYRIGHT Based on Text::Glob by Richard Clamp Original Copyright notice: Copyright (C) 2002, 2003, 2006, 2007 Richard Clamp. All Rights Reserved. This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. =cut