mirror of
https://github.com/ICDSystems/ICD.Common.Utils.git
synced 2026-01-11 19:44:55 +00:00
feat: Added util method for removing BOM characters from UTF8 data
This commit is contained in:
@@ -6,6 +6,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
|
||||
|
||||
## [Unreleased]
|
||||
- Added Yield extension to return a single-item enumerable for an object.
|
||||
- Added util method for removing BOM characters from UTF8 data
|
||||
|
||||
## [3.0.0] - 2018-04-23
|
||||
### Added
|
||||
|
||||
23
ICD.Common.Utils.Tests/EncodingUtilsTest.cs
Normal file
23
ICD.Common.Utils.Tests/EncodingUtilsTest.cs
Normal file
@@ -0,0 +1,23 @@
|
||||
using System;
|
||||
using System.Text;
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace ICD.Common.Utils.Tests
|
||||
{
|
||||
[TestFixture]
|
||||
public sealed class EncodingUtilsTest
|
||||
{
|
||||
[Test]
|
||||
public void StripUtf8BomTest()
|
||||
{
|
||||
Assert.Throws<ArgumentNullException>(() => EncodingUtils.StripUtf8Bom(null));
|
||||
|
||||
byte[] preamble = Encoding.UTF8.GetPreamble();
|
||||
string preambleString = Encoding.UTF8.GetString(preamble, 0, preamble.Length);
|
||||
|
||||
Assert.AreEqual(string.Empty, EncodingUtils.StripUtf8Bom(string.Empty));
|
||||
Assert.AreEqual("test", EncodingUtils.StripUtf8Bom("test"));
|
||||
Assert.AreEqual("test", EncodingUtils.StripUtf8Bom(preambleString + "test"));
|
||||
}
|
||||
}
|
||||
}
|
||||
30
ICD.Common.Utils/EncodingUtils.cs
Normal file
30
ICD.Common.Utils/EncodingUtils.cs
Normal file
@@ -0,0 +1,30 @@
|
||||
using System;
|
||||
using System.Text;
|
||||
using ICD.Common.Properties;
|
||||
|
||||
namespace ICD.Common.Utils
|
||||
{
|
||||
public static class EncodingUtils
|
||||
{
|
||||
/// <summary>
|
||||
/// Strips leading Byte Order Mark characters from the given UTF8 data.
|
||||
/// </summary>
|
||||
/// <param name="data">Input string to remove leading BOM chars from.</param>
|
||||
/// <returns>Input string with leading BOM chars removed.</returns>
|
||||
/// <exception cref="ArgumentNullException">Data is null.</exception>
|
||||
[PublicAPI]
|
||||
public static string StripUtf8Bom(string data)
|
||||
{
|
||||
if (data == null)
|
||||
throw new ArgumentNullException("data");
|
||||
|
||||
byte[] preamble = Encoding.UTF8.GetPreamble();
|
||||
string preambleString = Encoding.UTF8.GetString(preamble, 0, preamble.Length);
|
||||
|
||||
if (data.StartsWith(preambleString, StringComparison.Ordinal))
|
||||
data = data.Remove(0, preambleString.Length);
|
||||
|
||||
return data;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -77,6 +77,7 @@
|
||||
<Compile Include="Collections\WeakKeyDictionary.cs" />
|
||||
<Compile Include="Comparers\PredicateComparer.cs" />
|
||||
<Compile Include="ConsoleColor.cs" />
|
||||
<Compile Include="EncodingUtils.cs" />
|
||||
<Compile Include="EventArguments\BoolEventArgs.cs" />
|
||||
<Compile Include="EventArguments\CharEventArgs.cs" />
|
||||
<Compile Include="EventArguments\DateTimeEventArgs.cs" />
|
||||
|
||||
Reference in New Issue
Block a user