-
-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create and use FormattingStreamWriter
Prevents bugs causes by system cultures with different formatting
- Loading branch information
Showing
5 changed files
with
64 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
using System; | ||
using System.IO; | ||
|
||
namespace Jellyfin.Extensions; | ||
|
||
/// <summary> | ||
/// A custom StreamWriter which supports setting a IFormatProvider. | ||
/// </summary> | ||
public class FormattingStreamWriter : StreamWriter | ||
{ | ||
private readonly IFormatProvider _formatProvider; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="FormattingStreamWriter"/> class. | ||
/// </summary> | ||
/// <param name="stream">The stream to write to.</param> | ||
/// <param name="formatProvider">The format provider to use.</param> | ||
public FormattingStreamWriter(Stream stream, IFormatProvider formatProvider) | ||
: base(stream) | ||
{ | ||
_formatProvider = formatProvider; | ||
} | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="FormattingStreamWriter"/> class. | ||
/// </summary> | ||
/// <param name="path">The complete file path to write to.</param> | ||
/// <param name="formatProvider">The format provider to use.</param> | ||
public FormattingStreamWriter(string path, IFormatProvider formatProvider) | ||
: base(path) | ||
{ | ||
_formatProvider = formatProvider; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public override IFormatProvider FormatProvider | ||
=> _formatProvider; | ||
} |
23 changes: 23 additions & 0 deletions
23
tests/Jellyfin.Extensions.Tests/FormattingStreamWriterTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using System.Globalization; | ||
using System.IO; | ||
using System.Text; | ||
using System.Threading; | ||
using Xunit; | ||
|
||
namespace Jellyfin.Extensions.Tests; | ||
|
||
public static class FormattingStreamWriterTests | ||
{ | ||
[Fact] | ||
public static void Shuffle_Valid_Correct() | ||
{ | ||
Thread.CurrentThread.CurrentCulture = new CultureInfo("de-DE", false); | ||
using (var ms = new MemoryStream()) | ||
using (var txt = new FormattingStreamWriter(ms, CultureInfo.InvariantCulture)) | ||
{ | ||
txt.Write("{0}", 3.14159); | ||
txt.Close(); | ||
Assert.Equal("3.14159", Encoding.UTF8.GetString(ms.ToArray())); | ||
} | ||
} | ||
} |