2 package CharClass::Matcher;
6 use warnings FATAL => 'all';
7 use Text::Wrap qw(wrap);
9 $Data::Dumper::Useqq= 1;
10 our $hex_fmt= "0x%02X";
12 sub ASCII_PLATFORM { (ord('A') == 65) }
14 require 'regen/regen_lib.pl';
18 CharClass::Matcher -- Generate C macros that match character classes efficiently
22 perl Porting/regcharclass.pl
26 Dynamically generates macros for detecting special charclasses
27 in latin-1, utf8, and codepoint forms. Macros can be set to return
28 the length (in bytes) of the matched codepoint, and/or the codepoint itself.
30 To regenerate F<regcharclass.h>, run this script from perl-root. No arguments
33 Using WHATEVER as an example the following macros can be produced, depending
34 on the input parameters (how to get each is described by internal comments at
35 the C<__DATA__> line):
39 =item C<is_WHATEVER(s,is_utf8)>
41 =item C<is_WHATEVER_safe(s,e,is_utf8)>
43 Do a lookup as appropriate based on the C<is_utf8> flag. When possible
44 comparisons involving octect<128 are done before checking the C<is_utf8>
45 flag, hopefully saving time.
47 The version without the C<_safe> suffix should be used only when the input is
48 known to be well-formed.
50 =item C<is_WHATEVER_utf8(s)>
52 =item C<is_WHATEVER_utf8_safe(s,e)>
54 Do a lookup assuming the string is encoded in (normalized) UTF8.
56 The version without the C<_safe> suffix should be used only when the input is
57 known to be well-formed.
59 =item C<is_WHATEVER_latin1(s)>
61 =item C<is_WHATEVER_latin1_safe(s,e)>
63 Do a lookup assuming the string is encoded in latin-1 (aka plan octets).
65 The version without the C<_safe> suffix should be used only when it is known
66 that C<s> contains at least one character.
68 =item C<is_WHATEVER_cp(cp)>
70 Check to see if the string matches a given codepoint (hypothetically a
71 U32). The condition is constructed as as to "break out" as early as
72 possible if the codepoint is out of range of the condition.
76 (cp==X || (cp>X && (cp==Y || (cp>Y && ...))))
78 Thus if the character is X+1 only two comparisons will be done. Making
79 matching lookups slower, but non-matching faster.
81 =item C<what_len_WHATEVER_FOO(arg1, ..., len)>
83 A variant form of each of the macro types described above can be generated, in
84 which the code point is returned by the macro, and an extra parameter (in the
85 final position) is added, which is a pointer for the macro to set the byte
86 length of the returned code point.
88 These forms all have a C<what_len> prefix instead of the C<is_>, for example
89 C<what_len_WHATEVER_safe(s,e,is_utf8,len)> and
90 C<what_len_WHATEVER_utf8(s,len)>.
92 These forms should not be used I<except> on small sets of mostly widely
93 separated code points; otherwise the code generated is inefficient. For these
94 cases, it is best to use the C<is_> forms, and then find the code point with
95 C<utf8_to_uvchr_buf>(). This program can fail with a "deep recursion"
96 message on the worst of the inappropriate sets. Examine the generated macro
97 to see if it is acceptable.
99 =item C<what_WHATEVER_FOO(arg1, ...)>
101 A variant form of each of the C<is_> macro types described above can be generated, in
102 which the code point and not the length is returned by the macro. These have
103 the same caveat as L</what_len_WHATEVER_FOO(arg1, ..., len)>, plus they should
104 not be used where the set contains a NULL, as 0 is returned for two different
105 cases: a) the set doesn't include the input code point; b) the set does
106 include it, and it is a NULL.
112 perltidy -st -bt=1 -bbt=0 -pt=0 -sbt=1 -ce -nwls== "%f"
117 Author: Yves Orton (demerphq) 2007. Maintained by Perl5 Porters.
121 No tests directly here (although the regex engine will fail tests
122 if this code is broken). Insufficient documentation and no Getopts
123 handler for using the module as a script.
127 You may distribute under the terms of either the GNU General Public
128 License or the Artistic License, as specified in the README file.
132 # Sub naming convention:
133 # __func : private subroutine, can not be called as a method
134 # _func : private method, not meant for external use
135 # func : public method.
138 #-------------------------------------------------------------------------------
140 # ($cp,$n,$l,$u)=__uni_latin($str);
142 # Return a list of arrays, each of which when interpreted correctly
143 # represent the string in some given encoding with specific conditions.
145 # $cp - list of codepoints that make up the string.
146 # $n - list of octets that make up the string if all codepoints are invariant
147 # regardless of if the string is in UTF-8 or not.
148 # $l - list of octets that make up the string in latin1 encoding if all
149 # codepoints < 256, and at least one codepoint is UTF-8 variant.
150 # $u - list of octets that make up the string in utf8 if any codepoint is
154 #-----------+----------
155 # 0 - 127 : $n (127/128 are the values for ASCII platforms)
164 my $only_has_invariants = 1;
165 for my $ch ( split //, $str ) {
168 $max= $cp if $max < $cp;
169 if (! ASCII_PLATFORM && $only_has_invariants) {
171 $only_has_invariants = 0;
175 utf8::upgrade($temp);
176 my @utf8 = unpack "U0C*", $temp;
177 $only_has_invariants = (@utf8 == 1 && $utf8[0] == $cp);
182 $only_has_invariants = $max < 128 if ASCII_PLATFORM;
183 if ($only_has_invariants) {
186 $l= [@cp] if $max && $max < 256;
190 $u= [ unpack "U0C*", $u ] if defined $u;
192 return ( \@cp, $n, $l, $u );
196 # $clean= __clean($expr);
198 # Cleanup a ternary expression, removing unnecessary parens and apply some
199 # simplifications using regexes.
208 $parens= qr/ (?> \( (?> (?: (?> [^()]+ ) | (??{ $parens }) )* ) \) ) /x;
210 ## remove redundant parens
211 1 while $expr =~ s/ \( \s* ( $parens ) \s* \) /$1/gx;
214 # repeatedly simplify conditions like
215 # ( (cond1) ? ( (cond2) ? X : Y ) : Y )
217 # ( ( (cond1) && (cond2) ) ? X : Y )
218 # Also similarly handles expressions like:
219 # : (cond1) ? ( (cond2) ? X : Y ) : Y )
220 # Note the inclusion of the close paren in ([:()]) and the open paren in ([()]) is
221 # purely to ensure we have a balanced set of parens in the expression which makes
222 # it easier to understand the pattern in an editor that understands paren's, we do
223 # not expect either of these cases to actually fire. - Yves
229 \? \s* ($parens|[^()?:\s]+?) \s*
230 : \s* ($parens|[^()?:\s]+?) \s*
234 /$1 ( $2 && $3 ) ? $4 : $5 $6/gx;
240 # $text= __macro(@args);
241 # Join args together by newlines, and then neatly add backslashes to the end
242 # of every line as expected by the C pre-processor for #define's.
246 my $str= join "\n", @_;
248 my @lines= map { s/\s+$//; s/\t/ /g; $_ } split /\n/, $str;
249 my $last= pop @lines;
250 $str= join "\n", ( map { sprintf "%-76s\\", $_ } @lines ), $last;
251 1 while $str =~ s/^(\t*) {8}/$1\t/gm;
256 # my $op=__incrdepth($op);
258 # take an 'op' hashref and add one to it and all its childrens depths.
263 return unless ref $op;
265 __incrdepth( $op->{yes} );
266 __incrdepth( $op->{no} );
270 # join two branches of an opcode together with a condition, incrementing
271 # the depth on the yes branch when we do so.
272 # returns the new root opcode of the tree.
274 my ( $cond, $yes, $no )= @_;
277 yes => __incrdepth( $yes ),
287 # my $obj=CLASS->new(op=>'SOMENAME',title=>'blah',txt=>[..]);
289 # Create a new CharClass::Matcher object by parsing the text in
290 # the txt array. Currently applies the following rules:
292 # Element starts with C<0x>, line is evaled the result treated as
293 # a number which is passed to chr().
295 # Element starts with C<">, line is evaled and the result treated
298 # Each string is then stored in the 'strs' subhash as a hash record
299 # made up of the results of __uni_latin1, using the keynames
300 # 'low','latin1','utf8', as well as the synthesized 'LATIN1', 'high', and
301 # 'UTF8' which hold a merge of 'low' and their lowercase equivelents.
303 # Size data is tracked per type in the 'size' subhash.
311 die "in " . __PACKAGE__ . " constructor '$_;' is a mandatory field"
317 title => $opt{title} || '',
319 foreach my $txt ( @{ $opt{txt} } ) {
321 if ( $str =~ /^[""]/ ) {
323 } elsif ($str =~ / - /x ) { # A range: Replace this element on the
324 # list with its expansion
325 my ($lower, $upper) = $str =~ / 0x (.+?) \s* - \s* 0x (.+) /x;
326 die "Format must be like '0xDEAD - 0xBEAF'; instead was '$str'" if ! defined $lower || ! defined $upper;
327 foreach my $cp (hex $lower .. hex $upper) {
328 push @{$opt{txt}}, sprintf "0x%X", $cp;
331 } elsif ($str =~ s/ ^ N (?= 0x ) //x ) {
332 # Otherwise undocumented, a leading N means is already in the
333 # native character set; don't convert.
335 } elsif ( $str =~ /^0x/ ) {
338 # Convert from Unicode/ASCII to native, if necessary
339 $str = utf8::unicode_to_native($str) if ! ASCII_PLATFORM
342 } elsif ( $str =~ / \s* \\p \{ ( .*? ) \} /x) {
344 use Unicode::UCD qw(prop_invlist);
346 my @invlist = prop_invlist($property, '_perl_core_internal_ok');
349 # An empty return could mean an unknown property, or merely
350 # that it is empty. Call in scalar context to differentiate
351 my $count = prop_invlist($property, '_perl_core_internal_ok');
352 die "$property not found" unless defined $count;
355 # Replace this element on the list with the property's expansion
356 for (my $i = 0; $i < @invlist; $i += 2) {
357 foreach my $cp ($invlist[$i] .. $invlist[$i+1] - 1) {
359 # prop_invlist() returns native values; add leading 'N'
361 push @{$opt{txt}}, sprintf "N0x%X", $cp;
365 } elsif ($str =~ / ^ do \s+ ( .* ) /x) {
366 die "do '$1' failed: $!$@" if ! do $1 or $@;
368 } elsif ($str =~ / ^ & \s* ( .* ) /x) { # user-furnished sub() call
369 my @results = eval "$1";
370 die "eval '$1' failed: $@" if $@;
371 push @{$opt{txt}}, @results;
374 die "Unparsable line: $txt\n";
376 my ( $cp, $low, $latin1, $utf8 )= __uni_latin1( $str );
377 my $UTF8= $low || $utf8;
378 my $LATIN1= $low || $latin1;
379 my $high = (scalar grep { $_ < 256 } @$cp) ? 0 : $utf8;
380 #die Dumper($txt,$cp,$low,$latin1,$utf8)
381 # if $txt=~/NEL/ or $utf8 and @$utf8>3;
383 @{ $self->{strs}{$str} }{qw( str txt low utf8 latin1 high cp UTF8 LATIN1 )}=
384 ( $str, $txt, $low, $utf8, $latin1, $high, $cp, $UTF8, $LATIN1 );
385 my $rec= $self->{strs}{$str};
386 foreach my $key ( qw(low utf8 latin1 high cp UTF8 LATIN1) ) {
387 $self->{size}{$key}{ 0 + @{ $self->{strs}{$str}{$key} } }++
388 if $self->{strs}{$str}{$key};
390 $self->{has_multi} ||= @$cp > 1;
391 $self->{has_ascii} ||= $latin1 && @$latin1;
392 $self->{has_low} ||= $low && @$low;
393 $self->{has_high} ||= !$low && !$latin1;
395 $self->{val_fmt}= $hex_fmt;
396 $self->{count}= 0 + keys %{ $self->{strs} };
400 # my $trie = make_trie($type,$maxlen);
402 # using the data stored in the object build a trie of a specific type,
403 # and with specific maximum depth. The trie is made up the elements of
404 # the given types array for each string in the object (assuming it is
407 # returns the trie, or undef if there was no relevant data in the object.
411 my ( $self, $type, $maxlen )= @_;
413 my $strs= $self->{strs};
415 foreach my $rec ( values %$strs ) {
416 die "panic: unknown type '$type'"
417 if !exists $rec->{$type};
418 my $dat= $rec->{$type};
420 next if $maxlen && @$dat > $maxlen;
422 foreach my $elem ( @$dat ) {
423 $node->{$elem} ||= {};
424 $node= $node->{$elem};
426 $node->{''}= $rec->{str};
428 return 0 + keys( %trie ) ? \%trie : undef;
434 # This returns a list of the positions of the bits in the input word that
440 push @positions, $position if $word & 1;
447 # my $optree= _optree()
449 # recursively convert a trie to an optree where every node represents
455 my ( $self, $trie, $test_type, $ret_type, $else, $depth )= @_;
456 return unless defined $trie;
457 if ( $self->{has_multi} and $ret_type =~ /cp|both/ ) {
458 die "Can't do 'cp' optree from multi-codepoint strings";
461 $else= 0 unless defined $else;
462 $depth= 0 unless defined $depth;
464 # if we have an emptry string as a key it means we are in an
465 # accepting state and unless we can match further on should
466 # return the value of the '' key.
467 if (exists $trie->{''} ) {
468 # we can now update the "else" value, anything failing to match
469 # after this point should return the value from this.
470 if ( $ret_type eq 'cp' ) {
471 $else= $self->{strs}{ $trie->{''} }{cp}[0];
472 $else= sprintf "$self->{val_fmt}", $else if $else > 9;
473 } elsif ( $ret_type eq 'len' ) {
475 } elsif ( $ret_type eq 'both') {
476 $else= $self->{strs}{ $trie->{''} }{cp}[0];
477 $else= sprintf "$self->{val_fmt}", $else if $else > 9;
478 $else= "len=$depth, $else";
481 # extract the meaningful keys from the trie, filter out '' as
482 # it means we are an accepting state (end of sequence).
483 my @conds= sort { $a <=> $b } grep { length $_ } keys %$trie;
485 # if we havent any keys there is no further we can match and we
486 # can return the "else" value.
487 return $else if !@conds;
490 my $test= $test_type eq 'cp' ? "cp" : "((U8*)s)[$depth]";
491 # first we loop over the possible keys/conditions and find out what they look like
492 # we group conditions with the same optree together.
495 local $Data::Dumper::Sortkeys=1;
496 foreach my $cond ( @conds ) {
498 # get the optree for this child/condition
499 my $res= $self->_optree( $trie->{$cond}, $test_type, $ret_type, $else, $depth + 1 );
500 # convert it to a string with Dumper
501 my $res_code= Dumper( $res );
503 push @{$dmp_res{$res_code}{vals}}, $cond;
504 if (!$dmp_res{$res_code}{optree}) {
505 $dmp_res{$res_code}{optree}= $res;
506 push @res_order, $res_code;
510 # now that we have deduped the optrees we construct a new optree containing the merged
514 foreach my $res_code_idx (0 .. $#res_order) {
515 my $res_code= $res_order[$res_code_idx];
516 $node->{vals}= $dmp_res{$res_code}{vals};
517 $node->{test}= $test;
518 $node->{yes}= $dmp_res{$res_code}{optree};
519 $node->{depth}= $depth;
520 if ($res_code_idx < $#res_order) {
521 $node= $node->{no}= {};
531 # my $optree= optree(%opts);
533 # Convert a trie to an optree, wrapper for _optree
538 my $trie= $self->make_trie( $opt{type}, $opt{max_depth} );
539 $opt{ret_type} ||= 'len';
540 my $test_type= $opt{type} eq 'cp' ? 'cp' : 'depth';
541 return $self->_optree( $trie, $test_type, $opt{ret_type}, $opt{else}, 0 );
544 # my $optree= generic_optree(%opts);
546 # build a "generic" optree out of the three 'low', 'latin1', 'utf8'
547 # sets of strings, including a branch for handling the string type check.
554 $opt{ret_type} ||= 'len';
555 my $test_type= 'depth';
556 my $else= $opt{else} || 0;
558 my $latin1= $self->make_trie( 'latin1', $opt{max_depth} );
559 my $utf8= $self->make_trie( 'utf8', $opt{max_depth} );
561 $_= $self->_optree( $_, $test_type, $opt{ret_type}, $else, 0 )
565 $else= __cond_join( "( is_utf8 )", $utf8, $latin1 || $else );
566 } elsif ( $latin1 ) {
567 $else= __cond_join( "!( is_utf8 )", $latin1, $else );
569 my $low= $self->make_trie( 'low', $opt{max_depth} );
571 $else= $self->_optree( $low, $test_type, $opt{ret_type}, $else, 0 );
579 # create a string length guarded optree.
585 my $type= $opt{type};
587 die "Can't do a length_optree on type 'cp', makes no sense."
590 my ( @size, $method );
592 if ( $type eq 'generic' ) {
593 $method= 'generic_optree';
595 %{ $self->{size}{low} || {} },
596 %{ $self->{size}{latin1} || {} },
597 %{ $self->{size}{utf8} || {} }
599 @size= sort { $a <=> $b } keys %sizes;
602 @size= sort { $a <=> $b } keys %{ $self->{size}{$type} };
605 my $else= ( $opt{else} ||= 0 );
606 for my $size ( @size ) {
607 my $optree= $self->$method( %opt, type => $type, max_depth => $size );
608 my $cond= "((e)-(s) > " . ( $size - 1 ).")";
609 $else= __cond_join( $cond, $optree, $else );
614 sub calculate_mask(@) {
616 my $list_count = @list;
618 # Look at the input list of byte values. This routine sees if the set
619 # consisting of those bytes is exactly determinable by using a
620 # mask/compare operation. If not, it returns an empty list; if so, it
621 # returns a list consisting of (mask, compare). For example, consider a
622 # set consisting of the numbers 0xF0, 0xF1, 0xF2, and 0xF3. If we want to
623 # know if a number 'c' is in the set, we could write:
624 # 0xF0 <= c && c <= 0xF4
625 # But the following mask/compare also works, and has just one test:
627 # The reason it works is that the set consists of exactly those numbers
628 # whose first 4 bits are 1, and the next two are 0. (The value of the
629 # other 2 bits is immaterial in determining if a number is in the set or
630 # not.) The mask masks out those 2 irrelevant bits, and the comparison
631 # makes sure that the result matches all bytes that which match those 6
632 # material bits exactly. In other words, the set of numbers contains
633 # exactly those whose bottom two bit positions are either 0 or 1. The
634 # same principle applies to bit positions that are not necessarily
635 # adjacent. And it can be applied to bytes that differ in 1 through all 8
636 # bit positions. In order to be a candidate for this optimization, the
637 # number of numbers in the test must be a power of 2. Based on this
638 # count, we know the number of bit positions that must differ.
639 my $bit_diff_count = 0;
640 my $compare = $list[0];
641 if ($list_count == 2) {
644 elsif ($list_count == 4) {
647 elsif ($list_count == 8) {
650 elsif ($list_count == 16) {
653 elsif ($list_count == 32) {
656 elsif ($list_count == 64) {
659 elsif ($list_count == 128) {
662 elsif ($list_count == 256) {
666 # If the count wasn't a power of 2, we can't apply this optimization
667 return if ! $bit_diff_count;
671 # For each byte in the list, find the bit positions in it whose value
672 # differs from the first byte in the set.
673 for (my $i = 1; $i < @list; $i++) {
674 my @positions = pop_count($list[0] ^ $list[$i]);
676 # If the number of differing bits is greater than those permitted by
677 # the set size, this optimization doesn't apply.
678 return if @positions > $bit_diff_count;
680 # Save the bit positions that differ.
681 foreach my $bit (@positions) {
685 # If the total so far is greater than those permitted by the set size,
686 # this optimization doesn't apply.
687 return if keys %bit_map > $bit_diff_count;
690 # The value to compare against is the AND of all the members of the
691 # set. The bit positions that are the same in all will be correct in
692 # the AND, and the bit positions that differ will be 0.
693 $compare &= $list[$i];
696 # To get to here, we have gone through all bytes in the set,
697 # and determined that they all differ from each other in at most
698 # the number of bits allowed for the set's quantity. And since we have
699 # tested all 2**N possibilities, we know that the set includes no fewer
700 # elements than we need,, so the optimization applies.
701 die "panic: internal logic error" if keys %bit_map != $bit_diff_count;
703 # The mask is the bit positions where things differ, complemented.
705 foreach my $position (keys %bit_map) {
706 $mask |= 1 << $position;
708 $mask = ~$mask & 0xFF;
710 return ($mask, $compare);
714 # turn a list of conditions into a text expression
715 # - merges ranges of conditions, and joins the result with ||
717 my ( $self, $op, $combine, $opts_ref )= @_;
718 my $cond= $op->{vals};
719 my $test= $op->{test};
720 my $is_cp_ret = $opts_ref->{ret_type} eq "cp";
721 return "( $test )" if !defined $cond;
726 # We skip this if there are optimizations that
727 # we can apply (below) to the individual ranges
728 if ( ($is_cp_ret || $combine) && @ranges && ref $ranges[-1]) {
729 if ( $ranges[-1][0] == $ranges[-1][1] ) {
730 $ranges[-1]= $ranges[-1][0];
731 } elsif ( $ranges[-1][0] + 1 == $ranges[-1][1] ) {
732 $ranges[-1]= $ranges[-1][0];
733 push @ranges, $ranges[-1] + 1;
737 for my $condition ( @$cond ) {
738 if ( !@ranges || $condition != $ranges[-1][1] + 1 ) {
740 push @ranges, [ $condition, $condition ];
747 return $self->_combine( $test, @ranges )
754 "( $self->{val_fmt} <= $test && $test <= $self->{val_fmt} )",
756 : sprintf( "$self->{val_fmt} == $test", $_ );
760 # If the input set has certain characteristics, we can optimize tests
761 # for it. This doesn't apply if returning the code point, as we want
762 # each element of the set individually. The code above is for this
765 return 1 if @$cond == 256; # If all bytes match, is trivially true
768 # See if the entire set shares optimizable characterstics, and if
769 # so, return the optimization. We delay checking for this on sets
770 # with just a single range, as there may be better optimizations
771 # available in that case.
772 my ($mask, $base) = calculate_mask(@$cond);
773 if (defined $mask && defined $base) {
774 return sprintf "( ( $test & $self->{val_fmt} ) == $self->{val_fmt} )", $mask, $base;
778 # Here, there was no entire-class optimization. Look at each range.
779 for (my $i = 0; $i < @ranges; $i++) {
780 if (! ref $ranges[$i]) { # Trivial case: no range
781 $ranges[$i] = sprintf "$self->{val_fmt} == $test", $ranges[$i];
783 elsif ($ranges[$i]->[0] == $ranges[$i]->[1]) {
784 $ranges[$i] = # Trivial case: single element range
785 sprintf "$self->{val_fmt} == $test", $ranges[$i]->[0];
790 # Well-formed UTF-8 continuation bytes on ascii platforms must
791 # be in the range 0x80 .. 0xBF. If we know that the input is
792 # well-formed (indicated by not trying to be 'safe'), we can
793 # omit tests that verify that the input is within either of
794 # these bounds. (No legal UTF-8 character can begin with
795 # anything in this range, so we don't have to worry about this
796 # being a continuation byte or not.)
798 && ! $opts_ref->{safe}
799 && $opts_ref->{type} =~ / ^ (?: utf8 | high ) $ /xi)
801 my $lower_limit_is_80 = ($ranges[$i]->[0] == 0x80);
802 my $upper_limit_is_BF = ($ranges[$i]->[1] == 0xBF);
804 # If the range is the entire legal range, it matches any
805 # legal byte, so we can omit both tests. (This should
806 # happen only if the number of ranges is 1.)
807 if ($lower_limit_is_80 && $upper_limit_is_BF) {
810 elsif ($lower_limit_is_80) { # Just use the upper limit test
811 $output = sprintf("( $test <= $self->{val_fmt} )",
814 elsif ($upper_limit_is_BF) { # Just use the lower limit test
815 $output = sprintf("( $test >= $self->{val_fmt} )",
820 # If we didn't change to omit a test above, see if the number
821 # of elements is a power of 2 (only a single bit in the
822 # representation of its count will be set) and if so, it may
823 # be that a mask/compare optimization is possible.
825 && pop_count($ranges[$i]->[1] - $ranges[$i]->[0] + 1) == 1)
828 push @list, $_ for ($ranges[$i]->[0] .. $ranges[$i]->[1]);
829 my ($mask, $base) = calculate_mask(@list);
830 if (defined $mask && defined $base) {
831 $output = sprintf "( $test & $self->{val_fmt} ) == $self->{val_fmt}", $mask, $base;
835 if ($output ne "") { # Prefer any optimization
836 $ranges[$i] = $output;
838 elsif ($ranges[$i]->[0] + 1 == $ranges[$i]->[1]) {
839 # No optimization happened. We need a test that the code
840 # point is within both bounds. But, if the bounds are
841 # adjacent code points, it is cleaner to say
842 # 'first == test || second == test'
844 # 'first <= test && test <= second'
846 . join( " || ", ( map
847 { sprintf "$self->{val_fmt} == $test", $_ }
851 else { # Full bounds checking
852 $ranges[$i] = sprintf("( $self->{val_fmt} <= $test && $test <= $self->{val_fmt} )", $ranges[$i]->[0], $ranges[$i]->[1]);
858 return "( " . join( " || ", @ranges ) . " )";
863 # recursively turn a list of conditions into a fast break-out condition
864 # used by _cond_as_str() for 'cp' type macros.
866 my ( $self, $test, @cond )= @_;
868 my $item= shift @cond;
872 sprintf( "( $self->{val_fmt} <= $test && $test <= $self->{val_fmt} )",
874 $gtv= sprintf "$self->{val_fmt}", $item->[1];
876 $cstr= sprintf( "$self->{val_fmt} == $test", $item );
877 $gtv= sprintf "$self->{val_fmt}", $item;
880 return "( $cstr || ( $gtv < $test &&\n"
881 . $self->_combine( $test, @cond ) . " ) )";
888 # recursively convert an optree to text with reasonably neat formatting
890 my ( $self, $op, $combine, $brace, $opts_ref )= @_;
891 return 0 if ! defined $op; # The set is empty
895 my $cond= $self->_cond_as_str( $op, $combine, $opts_ref );
896 #no warnings 'recursion'; # This would allow really really inefficient
897 # code to be generated. See pod
898 my $yes= $self->_render( $op->{yes}, $combine, 1, $opts_ref );
899 return $yes if $cond eq '1';
901 my $no= $self->_render( $op->{no}, $combine, 0, $opts_ref );
902 return "( $cond )" if $yes eq '1' and $no eq '0';
903 my ( $lb, $rb )= $brace ? ( "( ", " )" ) : ( "", "" );
904 return "$lb$cond ? $yes : $no$rb"
905 if !ref( $op->{yes} ) && !ref( $op->{no} );
907 my $ind= "\n" . ( $ind1 x $op->{depth} );
909 if ( ref $op->{yes} ) {
910 $yes= $ind . $ind1 . $yes;
915 return "$lb$cond ?$yes$ind: $no$rb";
918 # $expr=render($op,$combine)
920 # convert an optree to text with reasonably neat formatting. If $combine
921 # is true then the condition is created using "fast breakouts" which
922 # produce uglier expressions that are more efficient for common case,
923 # longer lists such as that resulting from type 'cp' output.
924 # Currently only used for type 'cp' macros.
926 my ( $self, $op, $combine, $opts_ref )= @_;
927 my $str= "( " . $self->_render( $op, $combine, 0, $opts_ref ) . " )";
928 return __clean( $str );
932 # make a macro of a given type.
933 # calls into make_trie and (generic_|length_)optree as needed
935 # type : 'cp','generic','high','low','latin1','utf8','LATIN1','UTF8'
936 # ret_type : 'cp' or 'len'
937 # safe : add length guards to macro
939 # type defaults to 'generic', and ret_type to 'len' unless type is 'cp'
940 # in which case it defaults to 'cp' as well.
942 # it is illegal to do a type 'cp' macro on a pattern with multi-codepoint
943 # sequences in it, as the generated macro will accept only a single codepoint
952 my $type= $opts{type} || 'generic';
953 die "Can't do a 'cp' on multi-codepoint character class '$self->{op}'"
955 and $self->{has_multi};
956 my $ret_type= $opts{ret_type} || ( $opts{type} eq 'cp' ? 'cp' : 'len' );
959 $method= 'length_optree';
960 } elsif ( $type eq 'generic' ) {
961 $method= 'generic_optree';
965 my $optree= $self->$method( %opts, type => $type, ret_type => $ret_type );
966 my $text= $self->render( $optree, $type eq 'cp', \%opts );
967 my @args= $type eq 'cp' ? 'cp' : 's';
968 push @args, "e" if $opts{safe};
969 push @args, "is_utf8" if $type eq 'generic';
970 push @args, "len" if $ret_type eq 'both';
971 my $pfx= $ret_type eq 'both' ? 'what_len_' :
972 $ret_type eq 'cp' ? 'what_' : 'is_';
973 my $ext= $type eq 'generic' ? '' : '_' . lc( $type );
974 $ext .= "_safe" if $opts{safe};
975 my $argstr= join ",", @args;
976 return "/*** GENERATED CODE ***/\n"
977 . __macro( "#define $pfx$self->{op}$ext($argstr)\n$text" );
980 # if we arent being used as a module (highly likely) then process
981 # the __DATA__ below and produce macros in regcharclass.h
982 # if an argument is provided to the script then it is assumed to
983 # be the path of the file to output to, if the arg is '-' outputs
987 my $path= shift @ARGV || "regcharclass.h";
989 if ( $path eq '-' ) {
992 $out_fh = open_new( $path );
994 print $out_fh read_only_top( lang => 'C', by => $0,
995 file => 'regcharclass.h', style => '*',
996 copyright => [2007, 2011] );
997 print $out_fh "\n#ifndef H_REGCHARCLASS /* Guard against nested #includes */\n#define H_REGCHARCLASS 1\n\n";
999 my ( $op, $title, @txt, @types, %mods );
1003 # Skip if to compile on a different platform.
1004 return if delete $mods{only_ascii_platform} && ! ASCII_PLATFORM;
1005 return if delete $mods{only_ebcdic_platform} && ord 'A' != 193;
1007 print $out_fh "/*\n\t$op: $title\n\n";
1008 print $out_fh join "\n", ( map { "\t$_" } @txt ), "*/", "";
1009 my $obj= __PACKAGE__->new( op => $op, title => $title, txt => \@txt );
1011 #die Dumper(\@types,\%mods);
1014 push @mods, 'safe' if delete $mods{safe};
1015 unshift @mods, 'fast' if delete $mods{fast} || ! @mods; # Default to 'fast'
1020 die "Unknown modifiers: ", join ", ", map { "'$_'" } keys %mods;
1023 foreach my $type_spec ( @types ) {
1024 my ( $type, $ret )= split /-/, $type_spec;
1026 foreach my $mod ( @mods ) {
1027 next if $mod eq 'safe' and $type eq 'cp';
1029 my $macro= $obj->make_macro(
1032 safe => $mod eq 'safe'
1034 print $out_fh $macro, "\n";
1040 s/^ \s* (?: \# .* ) ? $ //x; # squeeze out comment and blanks
1044 $doit->(); # This starts a new definition; do the previous one
1045 ( $op, $title )= split /\s*:\s*/, $_, 2;
1047 } elsif ( s/^=>// ) {
1048 my ( $type, $modifier )= split /:/, $_;
1049 @types= split ' ', $type;
1051 map { $mods{$_} = 1 } split ' ', $modifier;
1058 print $out_fh "\n#endif /* H_REGCHARCLASS */\n";
1061 print $out_fh "/* ex: set ro: */\n";
1063 read_only_bottom_close_and_rename($out_fh)
1067 # The form of the input is a series of definitions to make macros for.
1068 # The first line gives the base name of the macro, followed by a colon, and
1069 # then text to be used in comments associated with the macro that are its
1070 # title or description. In all cases the first (perhaps only) parameter to
1071 # the macro is a pointer to the first byte of the code point it is to test to
1072 # see if it is in the class determined by the macro. In the case of non-UTF8,
1073 # the code point consists only of a single byte.
1075 # The second line must begin with a '=>' and be followed by the types of
1076 # macro(s) to be generated; these are specified below. A colon follows the
1077 # types, followed by the modifiers, also specified below. At least one
1078 # modifier is required.
1080 # The subsequent lines give what code points go into the class defined by the
1081 # macro. Multiple characters may be specified via a string like "\x0D\x0A",
1082 # enclosed in quotes. Otherwise the lines consist of one of:
1083 # 1) a single Unicode code point, prefaced by 0x
1084 # 2) a single range of Unicode code points separated by a minus (and
1086 # 3) a single Unicode property specified in the standard Perl form
1088 # 4) a line like 'do path'. This will do a 'do' on the file given by
1089 # 'path'. It is assumed that this does nothing but load subroutines
1090 # (See item 5 below). The reason 'require path' is not used instead is
1091 # because 'do' doesn't assume that path is in @INC.
1092 # 5) a subroutine call
1093 # &pkg::foo(arg1, ...)
1094 # where pkg::foo was loaded by a 'do' line (item 4). The subroutine
1095 # returns an array of entries of forms like items 1-3 above. This
1096 # allows more complex inputs than achievable from the other input types.
1098 # A blank line or one whose first non-blank character is '#' is a comment.
1099 # The definition of the macro is terminated by a line unlike those described.
1102 # low generate a macro whose name is 'is_BASE_low' and defines a
1103 # class that includes only ASCII-range chars. (BASE is the
1104 # input macro base name.)
1105 # latin1 generate a macro whose name is 'is_BASE_latin1' and defines a
1106 # class that includes only upper-Latin1-range chars. It is not
1107 # designed to take a UTF-8 input parameter.
1108 # high generate a macro whose name is 'is_BASE_high' and defines a
1109 # class that includes all relevant code points that are above
1110 # the Latin1 range. This is for very specialized uses only.
1111 # It is designed to take only an input UTF-8 parameter.
1112 # utf8 generate a macro whose name is 'is_BASE_utf8' and defines a
1113 # class that includes all relevant characters that aren't ASCII.
1114 # It is designed to take only an input UTF-8 parameter.
1115 # LATIN1 generate a macro whose name is 'is_BASE_latin1' and defines a
1116 # class that includes both ASCII and upper-Latin1-range chars.
1117 # It is not designed to take a UTF-8 input parameter.
1118 # UTF8 generate a macro whose name is 'is_BASE_utf8' and defines a
1119 # class that can include any code point, adding the 'low' ones
1120 # to what 'utf8' works on. It is designed to take only an input
1122 # generic generate a macro whose name is 'is_BASE". It has a 2nd,
1123 # boolean, parameter which indicates if the first one points to
1124 # a UTF-8 string or not. Thus it works in all circumstances.
1125 # cp generate a macro whose name is 'is_BASE_cp' and defines a
1126 # class that returns true if the UV parameter is a member of the
1127 # class; false if not.
1128 # A macro of the given type is generated for each type listed in the input.
1129 # The default return value is the number of octets read to generate the match.
1130 # Append "-cp" to the type to have it instead return the matched codepoint.
1131 # The macro name is changed to 'what_BASE...'. See pod for
1133 # Appending '-both" instead adds an extra parameter to the end of the argument
1134 # list, which is a pointer as to where to store the number of
1135 # bytes matched, while also returning the code point. The macro
1136 # name is changed to 'what_len_BASE...'. See pod for caveats
1139 # safe The input string is not necessarily valid UTF-8. In
1140 # particular an extra parameter (always the 2nd) to the macro is
1141 # required, which points to one beyond the end of the string.
1142 # The macro will make sure not to read off the end of the
1143 # string. In the case of non-UTF8, it makes sure that the
1144 # string has at least one byte in it. The macro name has
1145 # '_safe' appended to it.
1146 # fast The input string is valid UTF-8. No bounds checking is done,
1147 # and the macro can make assumptions that lead to faster
1149 # only_ascii_platform Skip this definition if this program is being run on
1150 # a non-ASCII platform.
1151 # only_ebcdic_platform Skip this definition if this program is being run on
1152 # a non-EBCDIC platform.
1153 # No modifier need be specified; fast is assumed for this case. If both
1154 # 'fast', and 'safe' are specified, two macros will be created for each
1157 # If run on a non-ASCII platform will automatically convert the Unicode input
1158 # to native. The documentation above is slightly wrong in this case. 'low'
1159 # actually refers to code points whose UTF-8 representation is the same as the
1160 # non-UTF-8 version (invariants); and 'latin1' refers to all the rest of the
1161 # code points less than 256.
1163 1; # in the unlikely case we are being used as a module
1166 # This is no longer used, but retained in case it is needed some day.
1167 # TRICKYFOLD: Problematic fold case letters. When adding to this list, also should add them to regcomp.c and fold_grind.t
1168 # => generic cp generic-cp generic-both :fast safe
1169 # 0x00DF # LATIN SMALL LETTER SHARP S
1170 # 0x0390 # GREEK SMALL LETTER IOTA WITH DIALYTIKA AND TONOS
1171 # 0x03B0 # GREEK SMALL LETTER UPSILON WITH DIALYTIKA AND TONOS
1172 # 0x1E9E # LATIN CAPITAL LETTER SHARP S, because maps to same as 00DF
1173 # 0x1FD3 # GREEK SMALL LETTER IOTA WITH DIALYTIKA AND OXIA; maps same as 0390
1174 # 0x1FE3 # GREEK SMALL LETTER UPSILON WITH DIALYTIKA AND OXIA; maps same as 03B0
1176 LNBREAK: Line Break: \R
1177 => generic UTF8 LATIN1 :fast safe
1178 "\x0D\x0A" # CRLF - Network (Windows) line ending
1181 HORIZWS: Horizontal Whitespace: \h \H
1182 => generic UTF8 LATIN1 cp :fast safe
1185 VERTWS: Vertical Whitespace: \v \V
1186 => generic UTF8 LATIN1 cp :fast safe
1189 REPLACEMENT: Unicode REPLACEMENT CHARACTER
1193 NONCHAR: Non character code points
1197 SURROGATE: Surrogate characters
1201 GCB_L: Grapheme_Cluster_Break=L
1205 GCB_LV_LVT_V: Grapheme_Cluster_Break=(LV or LVT or V)
1209 GCB_Prepend: Grapheme_Cluster_Break=Prepend
1213 GCB_RI: Grapheme_Cluster_Break=RI
1217 GCB_SPECIAL_BEGIN: Grapheme_Cluster_Break=special_begins
1219 \p{_X_Special_Begin}
1221 GCB_T: Grapheme_Cluster_Break=T
1225 GCB_V: Grapheme_Cluster_Break=V
1229 # This program was run with this enabled, and the results copied to utf8.h;
1230 # then this was commented out because it takes so long to figure out these 2
1231 # million code points. The results would not change unless utf8.h decides it
1232 # wants a maximum other than 4 bytes, or this program creates better
1234 #UTF8_CHAR: Matches utf8 from 1 to 4 bytes
1235 #=> UTF8 :safe only_ascii_platform
1238 # This hasn't been commented out, because we haven't an EBCDIC platform to run
1239 # it on, and the 3 types of EBCDIC allegedly supported by Perl would have
1241 UTF8_CHAR: Matches utf8 from 1 to 5 bytes
1242 => UTF8 :safe only_ebcdic_platform
1245 QUOTEMETA: Meta-characters that \Q should quote