Skip to content

Commit

Permalink
Fix bom trimming (#543)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonCropp authored Jun 8, 2022
1 parent 791c0b8 commit 350bd3f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<Project>
<PropertyGroup>
<NoWarn>CS1591;CS0649;xUnit1026;xUnit1013;msb3277;CS0436</NoWarn>
<Version>17.1.3</Version>
<Version>17.1.4</Version>
<ImplicitUsings>enable</ImplicitUsings>
<LangVersion>10</LangVersion>
<SuppressTfmSupportBuildWarnings>true</SuppressTfmSupportBuildWarnings>
Expand Down
16 changes: 2 additions & 14 deletions src/Verify/IoHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
{
static readonly UTF8Encoding Utf8 = new(true, true);

static readonly string UTF8Preamble = Encoding.UTF8.GetString(Encoding.UTF8.GetPreamble());

public static void DeleteIfEmpty(string path)
{
var info = new FileInfo(path);
Expand Down Expand Up @@ -45,19 +43,9 @@ static async Task<string> ReadStringWithFixedLines(this Stream stream)
return builder.ToString();
}

static string TrimPreamble(string text)
{
if (text.StartsWith(UTF8Preamble))
{
return text.Substring(UTF8Preamble.Length);
}

return text;
}

#if NETSTANDARD2_1 || NET5_0_OR_GREATER
public static Task WriteText(string path, string text) =>
File.WriteAllTextAsync(path, TrimPreamble(text), Utf8);
File.WriteAllTextAsync(path, text, Utf8);

public static async Task<string> ReadStringWithFixedLines(string path)
{
Expand All @@ -80,7 +68,7 @@ public static async Task WriteStream(string path, Stream stream)
#else
public static async Task WriteText(string path, string text)
{
var encodedText = Utf8.GetBytes(TrimPreamble(text));
var encodedText = Utf8.GetBytes(text);

using var stream = OpenWrite(path);
await stream.WriteAsync(encodedText, 0, encodedText.Length);
Expand Down
18 changes: 13 additions & 5 deletions src/Verify/Verifier/InnerVerifier_Inner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,17 @@ bool TryGetTargetBuilder(object? target, [NotNullWhen(true)] out StringBuilder?
return true;
}

if (!hasAppends && target is string stringTarget)
if (target is string stringTarget)
{
builder = new(stringTarget);
builder.FixNewlines();
extension = settings.ExtensionOrTxt();
return true;
target = stringTarget = TrimPreamble(stringTarget);

if (!hasAppends)
{
builder = new(stringTarget);
builder.FixNewlines();
extension = settings.ExtensionOrTxt();
return true;
}
}

extension = "txt";
Expand All @@ -77,4 +82,7 @@ bool TryGetTargetBuilder(object? target, [NotNullWhen(true)] out StringBuilder?

return true;
}

static string TrimPreamble(string text) =>
text.TrimStart('\uFEFF');
}

0 comments on commit 350bd3f

Please sign in to comment.