Adjust code spacing, tweak comments, do only (de)serialize tests with database, give tests names.

This commit is contained in:
Daniel Nichter
2011-12-28 15:47:50 -07:00
parent 88e82d665d
commit 4197b09f53
2 changed files with 55 additions and 51 deletions

View File

@@ -144,11 +144,10 @@ sub join_quote {
return $db ? "$db.$tbl" : $tbl;
}
# Nothing much going on here. Return the list passed in,
# with the elements passed through quotemeta, and the results
# concatenated with ','
# Return the list passed in, with the elements passed through quotemeta,
# and the results concatenated with ','.
sub serialize_list {
my @args = @_;
my ( $self, @args ) = @_;
if ( @args && $args[-1] eq '' ) {
# If the last element is an empty string, it conflicts
# with the assumptions of the somewhat lax regex below,
@@ -163,49 +162,49 @@ sub serialize_list {
}
sub deserialize_list {
my ( $string ) = @_;
my @escaped_parts = $string =~ /
my ( $self, $string ) = @_;
my @escaped_parts = $string =~ /
\G # Start of string, or end of previous match.
( # Each of these is an element in the original list.
[^\\,]* # Anything not a backslash or a comma
(?: # When we get here, we found one of the above.
\\. # A backslash followed by something means we can continue
\\. # A backslash followed by something so we can continue
[^\\,]* # Same as above.
)* # Repeat zero of more times.
)
(?:,|\z) # Comma dividing elements, or absolute end of the string.
(?:,|\z) # Comma dividing elements or absolute end of the string.
/sxg;
pop @escaped_parts; # Last element will always be empty. Flaw in the regex.
# But easier to fix this way. Faster, too.
my @unescaped_parts = map {
# Undo the quotemeta().
my $part = $_;
# Here be weirdness. Unfortunately quotemeta() is broken, and exposes
# the internal representation of scalars. Namely, the latin-1 range,
# \128-\377 (\p{Latin1} in newer Perls) is all escaped in downgraded
# strings, but left alone in UTF-8 strings. Thus, this.
# TODO: quotemeta() might change in 5.16 to mean
# qr/(?=\p{ASCII})\W|\p{Pattern_Syntax}/
# And also fix this whole weird behavior under
# use feature 'unicode_strings' -- If/once that's
# implemented, this will have to change.
my $char_class = utf8::is_utf8($part) # If it's a UTF-8 string,
? qr/(?=\p{ASCII})\W/ # We only care about non-word
# characters in the ASCII range
: qr/(?=\p{ASCII})\W|[\x{80}-\x{FF}]/; # Otherwise,
# same as above, but also
# unescape the latin-1 range.
$part =~ s/\\($char_class)/$1/g;
# As a somewhat uplifting note, all of the above is more
# or less fixed in newer Perls! quotemeta() is still
# broken, but regexen can deal with it more naturally.
$part;
} @escaped_parts;
# Last element will always be empty. Flaw in the regex.
# But easier to fix this way. Faster, too.
pop @escaped_parts;
# Undo the quotemeta().
my @unescaped_parts = map {
my $part = $_;
# Here be weirdness. Unfortunately quotemeta() is broken, and exposes
# the internal representation of scalars. Namely, the latin-1 range,
# \128-\377 (\p{Latin1} in newer Perls) is all escaped in downgraded
# strings, but left alone in UTF-8 strings. Thus, this.
# TODO: quotemeta() might change in 5.16 to mean
# qr/(?=\p{ASCII})\W|\p{Pattern_Syntax}/
# And also fix this whole weird behavior under
# use feature 'unicode_strings' -- If/once that's
# implemented, this will have to change.
my $char_class = utf8::is_utf8($part) # If it's a UTF-8 string,
? qr/(?=\p{ASCII})\W/ # We only care about non-word
# characters in the ASCII range
: qr/(?=\p{ASCII})\W|[\x{80}-\x{FF}]/; # Otherwise,
# same as above, but also
# unescape the latin-1 range.
$part =~ s/\\($char_class)/$1/g;
$part;
} @escaped_parts;
return @unescaped_parts;
}
1;
}
# ###########################################################################