Skip to content

Commit

Permalink
Item13893: Help out standard escapes
Browse files Browse the repository at this point in the history
  • Loading branch information
gac410 committed Jan 22, 2016
1 parent 3a6d2ed commit 409b0f4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 24 deletions.
3 changes: 3 additions & 0 deletions UnitTestContrib/test/unit/FuncTests.pm
Expand Up @@ -2188,6 +2188,7 @@ $gt embed$gtembed$gt()embed
$n$n!$n()gnnnnh
$amp embed$ampembed$amp()embed
$dollarlt
$lt()()$n()()$gt()()
TEST
my $expected = <<'TEST';
Expand All @@ -2207,6 +2208,8 @@ $ embed$embed$embed
gnnnnh
& embed&embed&embed
$lt
<()
()>()
TEST

#"
Expand Down
41 changes: 17 additions & 24 deletions core/lib/Foswiki.pm
Expand Up @@ -3782,37 +3782,30 @@ System.FormatTokens for a full list of supported tokens.
sub expandStandardEscapes {
my $text = shift;

# expand '$n()' and $n! to new line
$text =~ s/\$n\(\)/\n/gs;
$text =~ s/\$n(?=[^[:alpha:]]|$)/\n/gs;

# filler, useful for nested search
$text =~ s/\$nop(\(\))?//gs;

# $quot -> "
$text =~ s/\$quot(\(\))?/\"/gs;

# $comma -> ,
$text =~ s/\$comma(\(\))?/,/gs;

# $percent -> %
$text =~ s/\$perce?nt(\(\))?/\%/gs;

# $lt -> <
$text =~ s/\$lt(\(\))?/\</gs;

# $gt -> >
$text =~ s/\$gt(\(\))?/\>/gs;

# $amp -> &
$text =~ s/\$amp(\(\))?/\&/gs;
$text =~
s/\$((n(?=[^[:alpha:]]|$))|n\(\)|nop|quot|comma|perce?nt|lt|gt|amp)(?:\(\))?/&mysubst($1)/gse;

# $dollar -> $, done last to avoid creating the above tokens
$text =~ s/\$dollar(\(\))?/\$/gs;

return $text;
}

sub mysubst {
return '"' if $_[0] eq 'quot';
return '<' if $_[0] eq 'lt';
return '>' if $_[0] eq 'gt';
return '%' if ( $_[0] eq 'percent' | $_[0] eq 'percnt' );
return ',' if $_[0] eq 'comma';
return '&' if $_[0] eq 'amp';
return '' if $_[0] eq 'nop';
return "\n" if ( $_[0] eq 'n' );

# SMELL. Since $n, the trailing () are required, the regex might match $n()()
# so this requires an exception.
return "\n()" if ( $_[0] eq 'n()' );
}

=begin TML
---++ ObjectMethod webExists( $web ) -> $boolean
Expand Down

2 comments on commit 409b0f4

@MichaelDaum
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is a bad patch. I can't picture this being a performance improvement: (1) there's a sub being called as part of the regex match - best practice is to avoid them as good as possible (2) the same string is checked multiple times - as a regex and then using one or more EQs inside mysubst(). Last but not least a rather simple task has resulted in now overly complex code just to map some strings.

@gac410
Copy link
Member Author

@gac410 gac410 commented on 409b0f4 Jan 25, 2016 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.