[Templates-cvs] cvs commit: Template2/lib/Template Stash.pm

cvs@template-toolkit.org cvs@template-toolkit.org


cvs         06/02/02 11:45:38

  Modified:    lib/Template Stash.pm
  Log:
  * final (hopefully) version of the text.replace method
  
  Revision  Changes    Path
  2.98      +22 -39    Template2/lib/Template/Stash.pm
  
  Index: Stash.pm
  ===================================================================
  RCS file: /template-toolkit/Template2/lib/Template/Stash.pm,v
  retrieving revision 2.97
  retrieving revision 2.98
  diff -u -r2.97 -r2.98
  --- Stash.pm	2006/02/01 18:39:26	2.97
  +++ Stash.pm	2006/02/02 11:45:38	2.98
  @@ -18,7 +18,7 @@
   #
   #----------------------------------------------------------------------------
   #
  -# $Id: Stash.pm,v 2.97 2006/02/01 18:39:26 abw Exp $
  +# $Id: Stash.pm,v 2.98 2006/02/02 11:45:38 abw Exp $
   #
   #============================================================================
   
  @@ -29,7 +29,7 @@
   use strict;
   use vars qw( $VERSION $DEBUG $ROOT_OPS $SCALAR_OPS $HASH_OPS $LIST_OPS );
   
  -$VERSION = sprintf("%d.%02d", q$Revision: 2.97 $ =~ /(\d+)\.(\d+)/);
  +$VERSION = sprintf("%d.%02d", q$Revision: 2.98 $ =~ /(\d+)\.(\d+)/);
   
   
   #========================================================================
  @@ -78,43 +78,26 @@
       },
       'replace' => sub {
           my ($text, $pattern, $replace, $global) = @_;
  -        my ($matched, $after, $backref, @start, @end);
  -        my $result = '';
  -
  -        $global = 1 unless defined $global;
  -        
  -        while ($text =~ m/$pattern/) {
  -            if($#- == 0) {  
  -                # no captured groups so do a simple search and replace
  -                if($global) { $text =~ s/$pattern/$replace/g }
  -                else        { $text =~ s/$pattern/$replace/  }
  -                last;
  -            }
  -
  -            # extract the bit before the match, the match itself, the 
  -            # bit after and the positions of all subgroups
  -            $result .= substr($text, 0, $-[0]) if $-[0];
  -            $matched = substr($text, $-[0], $+[0] - $-[0]);
  -            $after   = substr($text, $+[0]);
  -            @start   = @-;
  -            @end     = @+;
  -
  -            # do the s/// leaving the placeholders (literally '$1' etc) in place
  -            $matched =~ s/$pattern/$replace/;
  -
  -            # then replace the $1, $2, etc., placeholders in reverse order 
  -            # to ensure we do $10 before $1
  -            for (my $i = $#start; $i; $i--) {
  -                $backref = substr( $text, $start[$i], $end[$i] - $start[$i] );
  -                $matched =~ s/\$$i/$backref/g;
  -            }
  -
  -            # add the modified $matched output to the result and loop if global
  -            $result .= $matched;
  -            $text    = $after;
  -            last unless $global && length $text;
  +        $text    = '' unless defined $text;
  +        $pattern = '' unless defined $pattern;
  +        $replace = '' unless defined $replace;
  +        $global  = 1  unless defined $global;
  +        my $expand = sub {
  +            my ($chunk, $start, $end) = @_;
  +            $chunk =~ s{ \\(\\|\$) | \$ (\d+) }{
  +                $1 ? $1
  +                    : ($2 > $#$start || $2 == 0) ? '' 
  +                    : substr($text, $start->[$2], $end->[$2] - $start->[$2]);
  +            }exg;
  +            $chunk;
  +        };
  +        if ($global) {
  +            $text =~ s{$pattern}{ &$expand($replace, [@-], [@+]) }eg;
  +        } 
  +        else {
  +            $text =~ s{$pattern}{ &$expand($replace, , [@-], [@+]) }e;
           }
  -        return $result . $text;
  +        return $text;
       },
       'remove'  => sub { 
           my ($str, $search) = @_;
  @@ -1094,7 +1077,7 @@
   
   =head1 VERSION
   
  -2.96, distributed as part of the
  +2.97, distributed as part of the
   Template Toolkit version 2.14a, released on 01 February 2006.
   
   =head1 COPYRIGHT