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

upgraded strings confuse Load() #60

Open
FGasper opened this issue Feb 19, 2021 · 3 comments
Open

upgraded strings confuse Load() #60

FGasper opened this issue Feb 19, 2021 · 3 comments

Comments

@FGasper
Copy link

FGasper commented Feb 19, 2021

String stored internally as bytes:

> perl -Mstrict -w -MYAML::Syck -e'my $foo = "---\népée"; my $parse = YAML::Syck::LoadYAML($foo); print $parse;'
épée

String stored internally as UTF-8:

> perl -Mstrict -w -MYAML::Syck -e'my $foo = "---\népée"; utf8::upgrade($foo); my $parse = YAML::Syck::LoadYAML($foo); print $parse;'
épée

Since Perl applications don’t (normally) control a string’s internal representation, this library should define separate LoadBytes() and LoadUTF8() functions.

(This is the same problem as with CDB_File.)

@FGasper
Copy link
Author

FGasper commented Feb 19, 2021

Also separate DumpBytes and DumpUTF8.

@FGasper
Copy link
Author

FGasper commented Feb 19, 2021

Provisional workaround DumpBytes implementation:

sub DumpBytes {
    Data::Rmap::rmap_all(
        sub {
            if (ref) {
                if ('HASH' eq ref) {
                    %$_ = map { $_ && utf8::downgrade($_) && $_ } %$_;
                }
                elsif ('ARRAY' eq ref) {
                    $_ && utf8::downgrade($_) for @$_;
                }
            }
            elsif ($_) {
                utf8::downgrade($_);
            }
        },
        $_[0],
    );

    return YAML::Syck::Dump($_[0]);
}

@FGasper
Copy link
Author

FGasper commented Feb 19, 2021

In fact … shouldn’t Load() always assume its input is bytes?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant