File Coverage

blib/lib/Regexp/CharClasses/Helper.pm
Criterion Covered Total %
statement 30 30 100.0
branch 13 14 92.8
condition 5 6 83.3
subroutine 7 7 100.0
pod 1 1 100.0
total 56 58 96.5


line stmt bran cond sub pod time code
1 3     3   52433 use strict;
  3         6  
  3         77  
2 3     3   12 use warnings;
  3         4  
  3         140  
3             package Regexp::CharClasses::Helper;
4              
5             # ABSTRACT: Helper for creating user defined character class strings
6             our $VERSION = '0.003'; # VERSION
7              
8 3     3   10 use Carp;
  3         3  
  3         138  
9 3     3   1388 use charnames ':full';
  3         74922  
  3         19  
10 3     3   683 use utf8;
  3         6  
  3         15  
11              
12             =pod
13              
14             =encoding utf8
15              
16             =head1 NAME
17              
18             Regexp::CharClasses::Helper - User defined character class strings by unicode name or literals
19              
20              
21             =head1 SYNOPSIS
22              
23             use Regexp::CharClasses::Helper;
24              
25             sub IsAorPlus {
26             return Regexp::CharClasses::Helper::fmt(
27             'A',
28             'LATIN SMALL LETTER A'
29             '+',
30             );
31             }
32             say 'matches' if 'A+' =~ /^\p{IsAorPlus}+$/;
33              
34             sub InCapitalsButNot42 {
35             return Regexp::CharClasses::Helper::fmt(
36             "+A\tZ", # from A till Z
37             "-\x42" # except \x42
38             );
39             }
40             say "doesn't" if 'ABC' =~ /\p{InCapitalsButNot42}+/;
41            
42              
43             =head1 METHODS AND ARGUMENTS
44              
45             =over 4
46              
47             =cut
48              
49             sub _parse {
50 1112   66 1112   1763 my $in = $_[0] || $_;
51 1112 100       1846 croak "Unknown charname '$in'" if $in =~ /^U\+/i;
52 1111 50       6105 return $in if $in =~ /In|Is|::/;
53 1111 100       1985 my $code = length $in == 1 ? ord $in
54             : charnames::vianame $in;
55 1111 100       51463 croak "Unknown charname '$in'" unless defined $code;
56 1089         5625 return sprintf '%04x', $code;
57             }
58              
59             =item fmt()
60              
61             Takes in a list and turns it into the format specified by L
62              
63             =cut
64              
65             sub fmt {
66             return join '', map {
67 584 100   584 1 216328 croak 'undef unexpected' unless defined $_;
  604         1275  
68 602         1123 my ($prefix, @out) = ('', $_);
69 602 100 100     3612 if (/^[-+!&]/ && !/^[-+!&]($|\t[^\t]|\t\t$)/) {
70 457         1164 $out[0] = s/^([-+!&])//r;
71 457         828 $prefix = $1;
72             }
73 602 100       2839 @out = ($1, $2) if $out[0] =~ /^(.+)\t(.+)$/;
74 602         627 $prefix . join("\t", map {_parse $_} @out) . "\n";
  1112         1354  
75             } @_;
76             }
77              
78             1;
79             __END__