Skip to content

Commit

Permalink
updated to handle utf8-bom string properly. (#378)
Browse files Browse the repository at this point in the history
  • Loading branch information
btiteux authored Feb 13, 2022
1 parent 0bb688a commit 5510f52
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System;
using System.Collections.Generic;
using System.Composition;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DevToys.Api.Core;
Expand Down Expand Up @@ -207,7 +208,16 @@ private async Task<string> DecodeBase64DataAsync(string? data)
{
byte[]? decodedData = Convert.FromBase64String(data);
Encoding encoder = GetEncoder();
decoded = encoder.GetString(decodedData);
if (encoder is UTF8Encoding && decodedData != null)
{
byte[] preamble = encoder.GetPreamble();
if (decodedData.Take(preamble.Length).SequenceEqual(preamble))
{
// need to keep it this way to have the dom char
decoded += Encoding.Unicode.GetString(preamble, 0, 1);
}
}
decoded += encoder.GetString(decodedData);
}
catch (FormatException)
{
Expand All @@ -226,7 +236,7 @@ private Encoding GetEncoder()
{
if (string.Equals(EncodingMode, DefaultEncoding, StringComparison.Ordinal))
{
return Encoding.UTF8;
return new UTF8Encoding(true);
}
return Encoding.ASCII;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
Grid.Row="1"
MinHeight="150"
AcceptsReturn="True"
IsRichTextEdit="True"
Header="{x:Bind ViewModel.Strings.InputTitle}"
Text="{x:Bind ViewModel.InputValue, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>

Expand All @@ -78,6 +79,7 @@
MinHeight="150"
IsReadOnly="True"
AcceptsReturn="True"
IsRichTextEdit="True"
Header="{x:Bind ViewModel.Strings.OutputTitle}"
Text="{x:Bind ViewModel.OutputValue, Mode=OneWay}"/>

Expand Down

0 comments on commit 5510f52

Please sign in to comment.