Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug that prevented roundtrip of number followed by newline. #47

Open
wants to merge 1 commit into
base: devel
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions lib/YAML/Tiny.pm
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,17 @@ sub _has_internal_string_value {
return $b_obj->FLAGS & B::SVf_POK();
}

sub _escape_string {
my $string = shift;
$string =~ s/\\/\\\\/g;
$string =~ s/"/\\"/g;
$string =~ s/\n/\\n/g;
$string =~ s/[\x85]/\\N/g;
$string =~ s/([\x00-\x1f])/\\$UNPRINTABLE[ord($1)]/g;
$string =~ s/([\x7f-\x9f])/'\x' . sprintf("%X",ord($1))/ge;
return qq|"$string"|;
}

sub _dump_scalar {
my $string = $_[1];
my $is_key = $_[2];
Expand All @@ -666,20 +677,17 @@ sub _dump_scalar {
if (Scalar::Util::looks_like_number($string)) {
# keys and values that have been used as strings get quoted
if ( $is_key || $has_string_flag ) {
if ( $string =~ /[\x00-\x09\x0b-\x0d\x0e-\x1f\x7f-\x9f\'\n]/ ) {
return _escape_string($string);
}
return qq['$string'];
}
else {
return $string;
}
}
if ( $string =~ /[\x00-\x09\x0b-\x0d\x0e-\x1f\x7f-\x9f\'\n]/ ) {
$string =~ s/\\/\\\\/g;
$string =~ s/"/\\"/g;
$string =~ s/\n/\\n/g;
$string =~ s/[\x85]/\\N/g;
$string =~ s/([\x00-\x1f])/\\$UNPRINTABLE[ord($1)]/g;
$string =~ s/([\x7f-\x9f])/'\x' . sprintf("%X",ord($1))/ge;
return qq|"$string"|;
return _escape_string($string);
}
if ( $string =~ /(?:^[~!@#%&*|>?:,'"`{}\[\]]|^-+$|\s|:\z)/ or
$QUOTE{$string}
Expand Down
7 changes: 7 additions & 0 deletions t/tml-local/yaml-roundtrip/quoting.tml
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,10 @@ slash3: '\\foo\\\\'
foo: '-'
--- perl
[ { foo => '-' } ]

=== Number followed by a newline
--- yaml
---
foo: "0\n"
--- perl
[ { foo => "0\n" } ]