Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
perlpunk committed Dec 31, 2023
1 parent 79511dd commit 48f4dde
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 10 deletions.
18 changes: 16 additions & 2 deletions LibYAML/XS.xs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ emit_string_events(AV *perl_events, HV *options)
yaml_emitter_t emitter;
SV **val;
SV *yaml = newSVpvn("", 0);
int unicode = 1;

XCPT_TRY_START
{
Expand All @@ -140,7 +141,14 @@ emit_string_events(AV *perl_events, HV *options)
}
yaml_emitter_set_output(&emitter, &append_output, (void *) yaml);
yaml_emitter_set_canonical(&emitter, 0);
yaml_emitter_set_unicode(&emitter, 0);
yaml_emitter_set_unicode(&emitter, 1);
val = hv_fetch(options, "unicode", 7, TRUE);
if (val && SvOK(*val) && SvTRUE(*val)) {
unicode = 1;
}
else if (val && SvOK(*val) && ! SvTRUE(*val)) {
unicode = 0;
}

emit_events(&emitter, perl_events);

Expand All @@ -155,7 +163,13 @@ emit_string_events(AV *perl_events, HV *options)
}

if (yaml) {
SvUTF8_off(yaml);
if (unicode) {
SvUTF8_off(yaml);
}
else {
(void)sv_utf8_decode(yaml);
SvUTF8_on(yaml);
}
}
RETVAL = yaml;

Expand Down
5 changes: 3 additions & 2 deletions LibYAML/etc/perl_libyaml.c
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ perl_to_libyaml_event(yaml_emitter_t *emitter, HV *perl_event)
}

if (strEQ(type, "stream_start_event")) {
ok = yaml_stream_start_event_initialize(&event, 0);
ok = yaml_stream_start_event_initialize(&event, YAML_UTF8_ENCODING);
}
else if (strEQ(type, "stream_end_event")) {
ok = yaml_stream_end_event_initialize(&event);
Expand Down Expand Up @@ -385,9 +385,10 @@ perl_to_libyaml_event(yaml_emitter_t *emitter, HV *perl_event)
else {
croak("%s\n", "scalar value not defined");
}

ok = yaml_scalar_event_initialize(
&event, anchor_name, tag_name,
(unsigned char *) scalar_value, strlen(scalar_value), plain_implicit, quoted_implicit, style);
(unsigned char *) scalar_value, len, plain_implicit, quoted_implicit, style);
}
else if (strEQ(type, "alias_event")) {
val = hv_fetch(perl_event, "value", 5, TRUE);
Expand Down
16 changes: 10 additions & 6 deletions t/10.basic.t
Original file line number Diff line number Diff line change
Expand Up @@ -269,16 +269,20 @@ subtest unicode => sub {
my $ev = [];
$yaml = "- ö";
YAML::LibYAML::API::parse_string_events($yaml, $ev);
use Devel::Peek;
my $value = encode_utf8 $ev->[3]->{value};
cmp_ok($value, 'eq', "ö", "utf8 parse");

$ev->[3]->{value} = decode_utf8 "ä";
my $dump = YAML::LibYAML::API::emit_string_events($ev);
cmp_ok($dump, '=~', qr{- "\\xE4"}i, "utf8 emit");

$ev->[3]->{value} = "\303\274 \303\300";
$dump = YAML::LibYAML::API::emit_string_events($ev);
cmp_ok($dump, '=~', qr{- "\\xC3\\xBC \\xC3\\xC0"}i, "binary emit");
Dump $ev->[3]->{value};
my $dump = YAML::LibYAML::API::emit_string_events($ev, { unicode => 0 });
Dump $dump;
cmp_ok($dump, '=~', qr{- \xe4}i, "utf8 decoded emit");

# $ev->[3]->{value} = "\303\274 \303\300";
$dump = YAML::LibYAML::API::emit_string_events($ev, { unicode => 1 });
cmp_ok($dump, '=~', qr{- ä}i, "utf8 emit");
# cmp_ok($dump, '=~', qr{- "\\xC3\\xBC \\xC3\\xC0"}i, "binary emit");
};

subtest indent => sub {
Expand Down

0 comments on commit 48f4dde

Please sign in to comment.