Feature request (patch attached)
I wanted to be able to see the unicode flag for strings. (utf8::is_utf8)
I also wanted to be able to render control characters and unicode as pure
ascii escapes, for debugging.
I added new options
show_unicode
escape_non_ascii
I also re-implemented _escape_chars to be more efficient on average (I
think, not tested) in addition to supporting these new features.
Subject: | unicode_and_extra_escapes.diff |
29,53c29,55
< 'name' => 'var',
< 'indent' => 4,
< 'index' => 1,
< 'max_depth' => 0,
< 'multiline' => 1,
< 'sort_keys' => 1,
< 'deparse' => 0,
< 'hash_separator' => ' ',
< 'separator' => ',',
< 'end_separator' => 0,
< 'show_tied' => 1,
< 'show_tainted' => 1,
< 'show_weak' => 1,
< 'show_readonly' => 0,
< 'show_lvalue' => 1,
< 'print_escapes' => 0,
< 'quote_keys' => 'auto',
< 'use_prototypes' => 1,
< 'output' => 'stderr',
< 'return_value' => 'dump', # also 'void' or 'pass'
< 'colored' => 'auto', # also 0 or 1
< 'caller_info' => 0,
< 'caller_message' => 'Printing in line __LINE__ of __FILENAME__:',
< 'class_method' => '_data_printer', # use a specific dump method, if available
< 'color' => {
---
> 'name' => 'var',
> 'indent' => 4,
> 'index' => 1,
> 'max_depth' => 0,
> 'multiline' => 1,
> 'sort_keys' => 1,
> 'deparse' => 0,
> 'hash_separator' => ' ',
> 'separator' => ',',
> 'end_separator' => 0,
> 'show_tied' => 1,
> 'show_unicode' => 0,
> 'show_tainted' => 1,
> 'show_weak' => 1,
> 'show_readonly' => 0,
> 'show_lvalue' => 1,
> 'print_escapes' => 0,
> 'escape_non_ascii' => 1,
> 'quote_keys' => 'auto',
> 'use_prototypes' => 1,
> 'output' => 'stderr',
> 'return_value' => 'dump', # also 'void' or 'pass'
> 'colored' => 'auto', # also 0 or 1
> 'caller_info' => 0,
> 'caller_message' => 'Printing in line __LINE__ of __FILENAME__:',
> 'class_method' => '_data_printer', # use a specific dump method, if available
> 'color' => {
329a332,334
> $string .= ' ' . colored('(U)', $p->{color}->{'unicode'})
> if $p->{show_unicode} and utf8::is_utf8($$item);
>
341a347,361
> my %_escape_mapping= (
> "\0" => '\0',
> "\n" => '\n',
> "\r" => '\r',
> "\t" => '\t',
> "\f" => '\f',
> "\b" => '\b',
> "\a" => '\a',
> "\e" => '\e',
> );
> sub _escape_char {
> exists $_escape_mapping{$_[0]}?
> $_escape_mapping{$_[0]}
> : sprintf((ord $_[0] <= 0xFF)? "\\x%02X" : "\\x{%X}", ord $_[0]);
> }
349,360c369,378
< $str =~ s/\e/$esc_color\\e$orig_color/g;
<
< my %escaped = (
< "\n" => '\n',
< "\r" => '\r',
< "\t" => '\t',
< "\f" => '\f',
< "\b" => '\b',
< "\a" => '\a',
< );
< foreach my $k ( keys %escaped ) {
< $str =~ s/$k/$esc_color$escaped{$k}$orig_color/g;
---
> if ($p->{escape_non_ascii}) {
> if (utf8::is_utf8($str)) {
> $str =~ s/([\0-\x1F\x7F-\x{10FFFF}])/ $esc_color . _escape_char($1) . $orig_color /eg;
> }
> else {
> $str =~ s/([\0-\x1F\x7F-\xFF])/ $esc_color . _escape_char($1) . $orig_color /eg;
> }
> }
> else {
> $str =~ s/([\0\a\b\e\f\n\r\t])/ $esc_color . _escape_char($1) . $orig_color /eg;
363,364c381,384
< # always escape the null character
< $str =~ s/\0/$esc_color\\0$orig_color/g;
---
> else {
> # always escape the null character
> $str =~ s/\0/$esc_color\\0$orig_color/g;
> }