Skip to content

Commit

Permalink
Merge branch 'axuno:version/v3.0' into version/v3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
axunonb authored Nov 5, 2021
2 parents e46513c + 5530353 commit 450dbd1
Show file tree
Hide file tree
Showing 3 changed files with 231 additions and 4 deletions.
4 changes: 2 additions & 2 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ for:
}
test_script:
- cmd: nuget install Appveyor.TestLogger
- cmd: dotnet test --no-build --framework net5.0 --test-adapter-path:. --logger:Appveyor SmartFormat.sln /p:configuration=release /p:AltCover=true /p:AltCoverXmlReport="coverage.xml" /p:AltCover=true /p:AltCoverStrongNameKey="..\SmartFormat\SmartFormat.snk" /p:AltCoverAssemblyExcludeFilter="SmartFormat.Tests|NUnit3.TestAdapter" /p:AltCoverLineCover="true"
- cmd: dotnet test --no-build --framework net5.0 --test-adapter-path:. --logger:Appveyor SmartFormat.sln /p:configuration=release /p:AltCover=true /p:AltCoverXmlReport="coverage.xml" /p:AltCover=true /p:AltCoverStrongNameKey="..\SmartFormat\SmartFormat.snk" /p:AltCoverAssemblyExcludeFilter="SmartFormat.Tests|SmartFormat.ZString|NUnit3.TestAdapter" /p:AltCoverLineCover="true"
- cmd: nuget install codecov -excludeversion
- cmd: .\Codecov\Tools\win7-x86\codecov.exe -f ".\SmartFormat.Tests\coverage.net5.0.xml" -n net5.0win
artifacts:
Expand All @@ -60,5 +60,5 @@ for:
- dotnet add ./SmartFormat.Tests/SmartFormat.Tests.csproj package AltCover
- dotnet build SmartFormat.sln /verbosity:minimal /t:rebuild /p:configuration=release /nowarn:CS1591,CS0618
test_script:
- dotnet test --no-build --framework net5.0 SmartFormat.sln /p:configuration=release /p:AltCover=true /p:AltCoverXmlReport="coverage.xml" /p:AltCover=true /p:AltCoverStrongNameKey="..\SmartFormat\SmartFormat.snk" /p:AltCoverAssemblyExcludeFilter="SmartFormat.Tests|NUnit3.TestAdapter" /p:AltCoverLineCover="true"
- dotnet test --no-build --framework net5.0 SmartFormat.sln /p:configuration=release /p:AltCover=true /p:AltCoverXmlReport="coverage.xml" /p:AltCover=true /p:AltCoverStrongNameKey="..\SmartFormat\SmartFormat.snk" /p:AltCoverAssemblyExcludeFilter="SmartFormat.Tests|SmartFormat.ZString|NUnit3.TestAdapter" /p:AltCoverLineCover="true"
- bash <(curl -s https://codecov.io/bash) -f ./SmartFormat.Tests/coverage.net5.0.xml -n net5.0linux
8 changes: 6 additions & 2 deletions src/SmartFormat.ZString/SmartFormat.ZString.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.0;netstandard2.1</TargetFrameworks>
<TargetFrameworks>netstandard2.0;netstandard2.1;net461</TargetFrameworks>
<RootNamespace>Cysharp.Text</RootNamespace>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
Expand Down Expand Up @@ -34,16 +34,20 @@
</PropertyGroup>

<ItemGroup>
<!-- repo\src\ZString\ZStringWriter.cs replaced for net461 compatibility -->
<Compile Remove="repo\src\ZString\ZStringWriter.cs" />
<Compile Remove="repo\docs\**" />
<Compile Remove="repo\sandbox\**" />
<Compile Remove="repo\src\ZString.Unity\**" />
<Compile Remove="repo\src\ZString\Unity\**" />
<Compile Remove="repo\tests\**" />
<EmbeddedResource Remove="repo\src\ZString\ZStringWriter.cs" />
<EmbeddedResource Remove="repo\docs\**" />
<EmbeddedResource Remove="repo\sandbox\**" />
<EmbeddedResource Remove="repo\src\ZString.Unity\**" />
<EmbeddedResource Remove="repo\src\ZString\Unity\**" />
<EmbeddedResource Remove="repo\tests\**" />
<None Remove="repo\src\ZString\ZStringWriter.cs" />
<None Remove="repo\docs\**" />
<None Remove="repo\sandbox\**" />
<None Remove="repo\src\ZString.Unity\**" />
Expand All @@ -52,7 +56,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="System.Memory" Version="4.5.3" />
<PackageReference Include="System.Memory" Version="4.5.4" />
</ItemGroup>

</Project>
223 changes: 223 additions & 0 deletions src/SmartFormat.ZString/ZStringWriter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
using System;
using System.Globalization;
using System.IO;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Cysharp.Text
{
/// <summary>
/// A <see cref="TextWriter"/> implementation that is backed with <see cref="Utf16ValueStringBuilder"/>.
/// </summary>
/// <remarks>
/// It's important to make sure the writer is always properly disposed.
/// </remarks>
public sealed class ZStringWriter : TextWriter
{
private Utf16ValueStringBuilder sb;
private bool isOpen;
private UnicodeEncoding encoding;

/// <summary>
/// Creates a new instance using <see cref="CultureInfo.CurrentCulture"/> as format provider.
/// </summary>
public ZStringWriter() : this(CultureInfo.CurrentCulture)
{
}

/// <summary>
/// Creates a new instance with given format provider.
/// </summary>
public ZStringWriter(IFormatProvider formatProvider) : base(formatProvider)
{
sb = ZString.CreateStringBuilder();
isOpen = true;
}

/// <summary>
/// Disposes this instance, operations are no longer allowed.
/// </summary>
public override void Close()
{
Dispose(true);
}

protected override void Dispose(bool disposing)
{
sb.Dispose();
isOpen = false;
base.Dispose(disposing);
}

public override Encoding Encoding => encoding = encoding ?? new UnicodeEncoding(false, false);

public override void Write(char value)
{
AssertNotDisposed();

sb.Append(value);
}

public override void Write(char[] buffer, int index, int count)
{
if (buffer == null)
{
throw new ArgumentNullException(nameof(buffer));
}
if (index < 0)
{
throw new ArgumentOutOfRangeException(nameof(index));
}
if (count < 0)
{
throw new ArgumentOutOfRangeException(nameof(count));
}
if (buffer.Length - index < count)
{
throw new ArgumentException();
}
AssertNotDisposed();

sb.Append(buffer.AsSpan(index, count));
}

public override void Write(string value)
{
AssertNotDisposed();

if (value != null)
{
sb.Append(value);
}
}

public override Task WriteAsync(char value)
{
Write(value);
return Task.CompletedTask;
}

public override Task WriteAsync(string value)
{
Write(value);
return Task.CompletedTask;
}

public override Task WriteAsync(char[] buffer, int index, int count)
{
Write(buffer, index, count);
return Task.CompletedTask;
}

public override Task WriteLineAsync(char value)
{
WriteLine(value);
return Task.CompletedTask;
}

public override Task WriteLineAsync(string value)
{
WriteLine(value);
return Task.CompletedTask;
}

public override Task WriteLineAsync(char[] buffer, int index, int count)
{
WriteLine(buffer, index, count);
return Task.CompletedTask;
}

public override void Write(bool value)
{
AssertNotDisposed();
sb.Append(value);
}

public override void Write(decimal value)
{
AssertNotDisposed();
sb.Append(value);
}

/// <summary>
/// No-op.
/// </summary>
public override Task FlushAsync()
{
return Task.CompletedTask;
}

/// <summary>
/// Materializes the current state from underlying string builder.
/// </summary>
public override string ToString()
{
return sb.ToString();
}

#if !UNITY_2018_3_OR_NEWER

public
#if !NETSTANDARD2_0 && !NETFRAMEWORK
override
#endif
void Write(ReadOnlySpan<char> buffer)
{
AssertNotDisposed();

sb.Append(buffer);
}

public
#if !NETSTANDARD2_0 && !NETFRAMEWORK
override
#endif
void WriteLine(ReadOnlySpan<char> buffer)
{
AssertNotDisposed();

sb.Append(buffer);
WriteLine();
}

public
#if !NETSTANDARD2_0 && !NETFRAMEWORK
override
#endif
Task WriteAsync(ReadOnlyMemory<char> buffer, CancellationToken cancellationToken = default)
{
if (cancellationToken.IsCancellationRequested)
{
return Task.FromCanceled(cancellationToken);
}

Write(buffer.Span);
return Task.CompletedTask;
}

public
#if !NETSTANDARD2_0 && !NETFRAMEWORK
override
#endif
Task WriteLineAsync(ReadOnlyMemory<char> buffer, CancellationToken cancellationToken = default)
{
if (cancellationToken.IsCancellationRequested)
{
return Task.FromCanceled(cancellationToken);
}

WriteLine(buffer.Span);
return Task.CompletedTask;
}
#endif

private void AssertNotDisposed()
{
if (!isOpen)
{
throw new ObjectDisposedException(nameof(sb));
}
}
}
}

0 comments on commit 450dbd1

Please sign in to comment.