Skip to content

Commit 350bd3f

Browse files
authored
Fix bom trimming (#543)
1 parent 791c0b8 commit 350bd3f

File tree

3 files changed

+16
-20
lines changed

3 files changed

+16
-20
lines changed

src/Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<Project>
33
<PropertyGroup>
44
<NoWarn>CS1591;CS0649;xUnit1026;xUnit1013;msb3277;CS0436</NoWarn>
5-
<Version>17.1.3</Version>
5+
<Version>17.1.4</Version>
66
<ImplicitUsings>enable</ImplicitUsings>
77
<LangVersion>10</LangVersion>
88
<SuppressTfmSupportBuildWarnings>true</SuppressTfmSupportBuildWarnings>

src/Verify/IoHelpers.cs

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
{
33
static readonly UTF8Encoding Utf8 = new(true, true);
44

5-
static readonly string UTF8Preamble = Encoding.UTF8.GetString(Encoding.UTF8.GetPreamble());
6-
75
public static void DeleteIfEmpty(string path)
86
{
97
var info = new FileInfo(path);
@@ -45,19 +43,9 @@ static async Task<string> ReadStringWithFixedLines(this Stream stream)
4543
return builder.ToString();
4644
}
4745

48-
static string TrimPreamble(string text)
49-
{
50-
if (text.StartsWith(UTF8Preamble))
51-
{
52-
return text.Substring(UTF8Preamble.Length);
53-
}
54-
55-
return text;
56-
}
57-
5846
#if NETSTANDARD2_1 || NET5_0_OR_GREATER
5947
public static Task WriteText(string path, string text) =>
60-
File.WriteAllTextAsync(path, TrimPreamble(text), Utf8);
48+
File.WriteAllTextAsync(path, text, Utf8);
6149

6250
public static async Task<string> ReadStringWithFixedLines(string path)
6351
{
@@ -80,7 +68,7 @@ public static async Task WriteStream(string path, Stream stream)
8068
#else
8169
public static async Task WriteText(string path, string text)
8270
{
83-
var encodedText = Utf8.GetBytes(TrimPreamble(text));
71+
var encodedText = Utf8.GetBytes(text);
8472

8573
using var stream = OpenWrite(path);
8674
await stream.WriteAsync(encodedText, 0, encodedText.Length);

src/Verify/Verifier/InnerVerifier_Inner.cs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,17 @@ bool TryGetTargetBuilder(object? target, [NotNullWhen(true)] out StringBuilder?
5858
return true;
5959
}
6060

61-
if (!hasAppends && target is string stringTarget)
61+
if (target is string stringTarget)
6262
{
63-
builder = new(stringTarget);
64-
builder.FixNewlines();
65-
extension = settings.ExtensionOrTxt();
66-
return true;
63+
target = stringTarget = TrimPreamble(stringTarget);
64+
65+
if (!hasAppends)
66+
{
67+
builder = new(stringTarget);
68+
builder.FixNewlines();
69+
extension = settings.ExtensionOrTxt();
70+
return true;
71+
}
6772
}
6873

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

7883
return true;
7984
}
85+
86+
static string TrimPreamble(string text) =>
87+
text.TrimStart('\uFEFF');
8088
}

0 commit comments

Comments
 (0)