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

UnoCore: strip Byte Order Mark for UTF-8 #251

Merged
merged 2 commits into from
Nov 10, 2019
Merged
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
19 changes: 15 additions & 4 deletions lib/UnoCore/Source/Uno/IO/Bundle.uno
Original file line number Diff line number Diff line change
Expand Up @@ -162,13 +162,24 @@ namespace Uno.IO

public string ReadAllText()
{
if defined(PREVIEW)
var bytes = defined(PREVIEW)
? _bytes ?? ReadAllBytes()
: ReadAllBytes();
var count = bytes.Length;
var index = 0;

// Strip Byte Order Mark for UTF-8.
// This is consistent with File.ReadAllText().
if (count > 2 &&
bytes[0] == 0xEF &&
bytes[1] == 0xBB &&
bytes[2] == 0xBF)
{
if (_bytes != null)
return Utf8.GetString(_bytes);
index = 3;
count -= 3;
}

return Utf8.GetString(ReadAllBytes());
return Utf8.GetString(bytes, index, count);
}

/** Must be called from Main-thread */
Expand Down
16 changes: 16 additions & 0 deletions lib/UnoCore/Tests/IO/BundleTests.uno
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Uno;
using Uno.IO;
using Uno.Testing;

namespace UnoCore_Uno_IO
{
public class BundleTests
{
[Test]
public static void LoadUtf8Bom()
{
var text = import("TestData/utf8-bom.txt").ReadAllText();
Assert.AreEqual(text, "hello");
}
}
}
1 change: 1 addition & 0 deletions lib/UnoCore/Tests/IO/TestData/utf8-bom.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello
2 changes: 2 additions & 0 deletions lib/UnoCore/Tests/UnoCore.Test.unoproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"Exceptions/InvalidCastException.Test.uno:Source",
"IO/BinaryReaderTests.uno:Source",
"IO/BinaryWriterTests.uno:Source",
"IO/BundleTests.uno:Source",
"IO/DirectoryTests.uno:Source",
"IO/FileTests.uno:Source",
"IO/MemoryStreamTests.uno:Source",
Expand All @@ -37,6 +38,7 @@
"IO/TestData/int4.bin:File",
"IO/TestData/long.bin:File",
"IO/TestData/string.bin:File",
"IO/TestData/utf8-bom.txt:File",
"Threading/ThreadTests.uno:Source",
"Time/CalendarTests.uno:Source",
"Time/DeviceTimeZoneMock.uno:Source",
Expand Down