Skip to content

Commit

Permalink
Use LangVersion:11 and omit AppendFormatted feature only for Unity
Browse files Browse the repository at this point in the history
  • Loading branch information
hadashiA committed Dec 8, 2023
1 parent 27eb8fb commit dc03d89
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 13 deletions.
6 changes: 3 additions & 3 deletions sandbox/ConsoleApp/ReadMeSample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ void Sample(int id, string name)
// like StringBuilder
var writer = Utf8String.CreateWriter(bufferWriter);
writer.Append("My Name...");
// writer.AppendFormat($"is...? {name}");
writer.AppendFormat($"is...? {name}");
writer.AppendLine();
writer.Flush();

Expand Down Expand Up @@ -115,7 +115,7 @@ void WriterSample()

// call each append methods.
writer.Append("foo");
// writer.AppendFormat($"bar {Guid.NewGuid()}");
writer.AppendFormat($"bar {Guid.NewGuid()}");

// finally call Flush(or Dispose)
writer.Flush();
Expand All @@ -131,7 +131,7 @@ void WriterSample2(IBufferWriter<byte> otherBufferWriter)

// call each append methods.
writer.Append("foo");
// writer.AppendFormat($"bar {Guid.NewGuid()}");
writer.AppendFormat($"bar {Guid.NewGuid()}");

// finally call Flush(or Dispose)
writer.Flush();
Expand Down
6 changes: 1 addition & 5 deletions src/Utf8StringInterpolation/Utf8StringInterpolation.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,14 @@
<PropertyGroup>
<TargetFrameworks>netstandard2.0;netstandard2.1;net6.0;net8.0</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<LangVersion>10</LangVersion>
<LangVersion>11</LangVersion>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<NoWarn>$(NoWarn);CS1591;CA2255</NoWarn>
<PackageTags>string</PackageTags>
<Description>Successor of ZString; UTF8 based zero allocation high-peformance String Interpolation and StringBuilder.</Description>
</PropertyGroup>

<PropertyGroup Condition="'$(TargetFramework)'=='netstandard2.0'">
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>

<ItemGroup Condition="'$(TargetFramework)'=='netstandard2.0'">
<PackageReference Include="System.Memory" Version="4.5.5" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ void AppendFormattedAlignment(bool value, int alignment, string? format)
buffer.Slice(0, bytesWritten).CopyTo(destination);
destination = destination.Slice(bytesWritten);
currentWritten += bytesWritten;
return;
}
else
{
Expand Down
14 changes: 10 additions & 4 deletions src/Utf8StringInterpolation/Utf8StringWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,10 @@ public Utf8StringWriter(int literalLength, int formattedCount, Span<byte> destin
this.bufferWriter.GetSpan(destination.Length); // allocate dummy
}

#if !UNITY_2022_2_OR_NEWER
// from AppendFormat extension methods.
public Utf8StringWriter(int literalLength, int formattedCount, ref Utf8StringWriter<TBufferWriter> parent)
// This feature required C# 11 or higher, because it needs scoped ref.
public Utf8StringWriter(int literalLength, int formattedCount, scoped ref Utf8StringWriter<TBufferWriter> parent)
{
parent.ClearState();
this.bufferWriter = parent.bufferWriter;
Expand All @@ -104,6 +106,7 @@ public Utf8StringWriter(int literalLength, int formattedCount, ref Utf8StringWri
var initialSize = literalLength + (formattedCount * GuessedLengthPerHole);
TryGrow(initialSize);
}
#endif

public void AppendLiteral(string s)
{
Expand Down Expand Up @@ -485,24 +488,27 @@ public void Dispose()
}
}

// This feature requires C# 11 or newer, because it needs scoped ref.
#if !UNITY_2022_2_OR_NEWER
public static class Utf8StringExtensions
{
// hack for use nested InterpolatedStringHandler.

public static void AppendFormat<TBufferWriter>(
this ref Utf8StringWriter<TBufferWriter> parent,
[InterpolatedStringHandlerArgument("parent")] ref Utf8StringWriter<TBufferWriter> format)
[InterpolatedStringHandlerArgument("parent")] scoped ref Utf8StringWriter<TBufferWriter> format)
where TBufferWriter : IBufferWriter<byte>
{
format.Flush();
}

public static void AppendLine<TBufferWriter>(
this ref Utf8StringWriter<TBufferWriter> parent,
[InterpolatedStringHandlerArgument("parent")] ref Utf8StringWriter<TBufferWriter> format)
[InterpolatedStringHandlerArgument("parent")] scoped ref Utf8StringWriter<TBufferWriter> format)
where TBufferWriter : IBufferWriter<byte>
{
format.Flush();
parent.AppendLine();
}
}
}
#endif

0 comments on commit dc03d89

Please sign in to comment.