File Coverage

blib/lib/Regexp/CharClasses/Helper.pm
Criterion Covered Total %
statement 32 32 100.0
branch 13 14 92.8
condition 5 6 83.3
subroutine 8 8 100.0
pod 1 1 100.0
total 59 61 96.7


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