Skip to content
Permalink
Browse files

Add a BOM on open for utf16 filehandles

When we open a file for write we will write a BOM upon open. Since we
already truncate the file if it exists, this shouldn't cause any loss of
data aside from the previous functionality.

If we are appending, then only add a BOM if there is nothing yet written
to the file. If the file already has data in it, then don't add a byte
order mark.
  • Loading branch information...
samcv committed Sep 30, 2018
1 parent 4a96805 commit 959b89cf8ce49b0acf735aa2155df651a42bf3c2
Showing with 9 additions and 0 deletions.
  1. +9 −0 src/core/IO/Handle.pm6
@@ -177,8 +177,17 @@ my class IO::Handle {
$!decoder.set-line-separators(($!nl-in = $nl-in).list);
$!encoder := $encoding.encoder(:translate-nl);
$!encoding = $encoding.name;

# Add a byte order mark to the start of the file for utf16
nqp::if(nqp::iseq_s($!encoding, 'utf16'), (
if $create && !$exclusive && (!$append || $append && $!path.s == 0) {
self.write: Buf[uint16].new(0xFEFF);
})
);
}
self!set-out-buffer-size($out-buffer);


self;
}

0 comments on commit 959b89c

Please sign in to comment.
You can’t perform that action at this time.