Skip to content

Commit

Permalink
Merge pull request #63 from cake-contrib/feature/gh-62
Browse files Browse the repository at this point in the history
(GH-62) Ignore if preamble should be skipped but does not exist
  • Loading branch information
pascalberger authored Sep 4, 2018
2 parents ee55d9c + c0ccd1f commit 171a0e4
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 10 deletions.
12 changes: 6 additions & 6 deletions src/Cake.Issues.Tests/ByteArrayExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,17 @@ public void Should_Throw_If_Value_Is_Null()
}

[Fact]
public void Should_Throw_If_Preamble_Should_Be_Skipped_But_No_Preamble_Passed()
public void Should_Ignore_If_Preamble_Should_Be_Skipped_But_No_Preamble_Passed()
{
// Given
var stringValue = "foo🐱bar";
var byteArrayValue = stringValue.ToByteArray();

// When
var result = Record.Exception(() => byteArrayValue.ToStringUsingEncoding(true));
var result = byteArrayValue.ToStringUsingEncoding(true);

// Then
result.IsArgumentException("value");
result.ShouldBe(stringValue);
}

[Theory]
Expand Down Expand Up @@ -131,18 +131,18 @@ public void Should_Throw_If_Value_Is_Null()
}

[Fact]
public void Should_Throw_If_Preamble_Should_Be_Skipped_But_No_Preamble_Passed()
public void Should_Ignore_If_Preamble_Should_Be_Skipped_But_No_Preamble_Passed()
{
// Given
var encoding = Encoding.UTF32;
var stringValue = "foo🐱bar";
var byteArrayValue = stringValue.ToByteArray(encoding);

// When
var result = Record.Exception(() => byteArrayValue.ToStringUsingEncoding(encoding, true));
var result = byteArrayValue.ToStringUsingEncoding(encoding, true);

// Then
result.IsArgumentException("value");
result.ShouldBe(stringValue);
}

[Theory]
Expand Down
6 changes: 2 additions & 4 deletions src/Cake.Issues/ByteArrayExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,10 @@ public static string ToStringUsingEncoding(this byte[] value, Encoding encoding,
{
var preamble = encoding.GetPreamble();

if (preamble.Where((p, i) => p != value[i]).Any())
if (value.Zip(preamble, (x, y) => x == y).All(x => x))
{
throw new ArgumentException("Value contains no preamble.", nameof(value));
value = value.Skip(preamble.Length).ToArray();
}

value = value.Skip(preamble.Length).ToArray();
}

return encoding.GetString(value);
Expand Down

0 comments on commit 171a0e4

Please sign in to comment.