Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions sandbox/ConsoleApp/ConsoleApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net9.0</TargetFramework>
<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>../../opensource.snk</AssemblyOriginatorKeyFile>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.12.1" />
<PackageReference Include="System.Text.Json" Version="8.0.4" />
<PackageReference Include="BenchmarkDotNet" Version="0.15.2" />
<PackageReference Include="System.Text.Json" Version="9.0.6" />
<ProjectReference Include="..\..\src\ZString\ZString.csproj">
<SetTargetFramework>TargetFramework=netstandard2.1</SetTargetFramework>
</ProjectReference>
Expand Down
4 changes: 2 additions & 2 deletions sandbox/PerfBenchmark/PerfBenchmark.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net9.0</TargetFramework>
<LangVersion>8.0</LangVersion>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.12.1" />
<PackageReference Include="BenchmarkDotNet" Version="0.15.2" />
<PackageReference Include="StringFormatter" Version="1.0.0.13" />
</ItemGroup>

Expand Down
6 changes: 3 additions & 3 deletions src/ZString.Unity/Assets/Scripts/ZString/EnumUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
namespace Cysharp.Text
{
internal static class EnumUtil<T>
// where T : Enum
where T : notnull
{
const string InvalidName = "$";

Expand Down Expand Up @@ -45,7 +45,7 @@ public static bool TryFormatUtf16(T value, Span<char> dest, out int written, Rea
{
if (!names.TryGetValue(value, out var v) || v == InvalidName)
{
v = value!.ToString(); // T is Enum, not null always
v = value!.ToString()!; // T is Enum, not null always
}

written = v.Length;
Expand All @@ -56,7 +56,7 @@ public static bool TryFormatUtf8(T value, Span<byte> dest, out int written, Stan
{
if (!utf8names.TryGetValue(value, out var v) || v.Length == 0)
{
v = Encoding.UTF8.GetBytes(value!.ToString());
v = Encoding.UTF8.GetBytes(value.ToString()!);
}

written = v.Length;
Expand Down
6 changes: 3 additions & 3 deletions src/ZString.Unity/Assets/Scripts/ZString/FormatHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ private static void FormatToRightJustify<TBufferWriter, T>(ref TBufferWriter sb,
{
if (typeof(T) == typeof(string))
{
var s = Unsafe.As<string>(arg);
var s = Unsafe.As<string>(arg) ?? "";
int padding = width - s.Length;
if (padding > 0)
{
Expand Down Expand Up @@ -130,7 +130,7 @@ private static void FormatToRightJustify<TBufferWriter, T>(ref TBufferWriter sb,
{
if (typeof(T) == typeof(string))
{
var s = Unsafe.As<string>(arg);
var s = Unsafe.As<string>(arg) ?? "";
int padding = width - s.Length;
if (padding > 0)
{
Expand Down Expand Up @@ -169,4 +169,4 @@ private static void FormatToRightJustify<TBufferWriter, T>(ref TBufferWriter sb,
}
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

#pragma warning disable CS9080,CS9082,CS9084,CS9091,CS9094

namespace System
{
internal static partial class Number
Expand Down Expand Up @@ -1219,4 +1221,4 @@ private static uint DivRem32(uint value, out uint remainder)
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
using System.Numerics;
using System.Runtime.CompilerServices;

#pragma warning disable CS9080
#pragma warning disable CS9091

namespace System
{
// This is a port of the `Dragon4` implementation here: http://www.ryanjuckett.com/programming/printing-floating-point-numbers/part-2/
Expand Down Expand Up @@ -522,4 +525,4 @@ private static unsafe uint Dragon4(ulong mantissa, int exponent, uint mantissaHi
return outputLen;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using System.Diagnostics;
using System.Runtime.CompilerServices;

#pragma warning disable CS9080

namespace System
{
internal unsafe partial class Number
Expand Down Expand Up @@ -634,4 +636,4 @@ private static bool ShouldRoundUp(bool lsbBit, bool roundBit, bool hasTailBits)
return roundBit && (hasTailBits || lsbBit);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ static Utf16ValueStringBuilder()
[ThreadStatic]
internal static bool scratchBufferUsed;

char[]? buffer;
char[] buffer;
int index;
bool disposeImmediately;

Expand Down Expand Up @@ -103,7 +103,7 @@ public void Dispose()
{
ArrayPool<char>.Shared.Return(buffer);
}
buffer = null;
buffer = null!;
index = 0;
if (disposeImmediately)
{
Expand Down Expand Up @@ -642,7 +642,7 @@ void AppendFormatInternal<T>(T arg, int width, ReadOnlySpan<char> format, string
{
if (typeof(T) == typeof(string))
{
var s = Unsafe.As<string>(arg);
var s = Unsafe.As<string>(arg) ?? "";
int padding = width - s.Length;
if (padding > 0)
{
Expand Down Expand Up @@ -719,7 +719,9 @@ static FormatterCache()
{
if (typeof(T).IsEnum)
{
#pragma warning disable CS8714
formatter = new TryFormat<T>(EnumUtil<T>.TryFormatUtf16);
#pragma warning restore CS8714
}
else if (typeof(T) == typeof(string))
{
Expand Down Expand Up @@ -759,7 +761,7 @@ static bool TryFormatDefault(T value, Span<char> dest, out int written, ReadOnly

var s = (value is IFormattable formattable && format.Length != 0) ?
formattable.ToString(format.ToString(), null) :
value.ToString();
value.ToString()!;

// also use this length when result is false.
written = s.Length;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Buffers;
using System.IO;
using System.Runtime.CompilerServices;
Expand Down Expand Up @@ -44,7 +44,7 @@ static Utf8ValueStringBuilder()
[ThreadStatic]
internal static bool scratchBufferUsed;

byte[]? buffer;
byte[] buffer;
int index;
bool disposeImmediately;

Expand Down Expand Up @@ -110,7 +110,7 @@ public void Dispose()
{
ArrayPool<byte>.Shared.Return(buffer);
}
buffer = null;
buffer = null!;
index = 0;
if (disposeImmediately)
{
Expand Down Expand Up @@ -432,7 +432,7 @@ private void AppendFormatInternal<T>(T arg, int width, StandardFormat format, st
{
if (typeof(T) == typeof(string))
{
var s = Unsafe.As<string>(arg);
var s = Unsafe.As<string>(arg) ?? "";
int padding = width - s.Length;
if (padding > 0)
{
Expand Down Expand Up @@ -504,8 +504,10 @@ static FormatterCache()
if (formatter == null)
{
if (typeof(T).IsEnum)
{
formatter = new TryFormat<T>(EnumUtil<T>.TryFormatUtf8);
{
#pragma warning disable CS8714
formatter = new TryFormat<T>(EnumUtil<T>.TryFormatUtf8);
#pragma warning restore CS8714
}
else
{
Expand All @@ -526,7 +528,7 @@ static bool TryFormatDefault(T value, Span<byte> dest, out int written, Standard

var s = typeof(T) == typeof(string) ? Unsafe.As<string>(value) :
(value is IFormattable formattable && format != default) ? formattable.ToString(format.ToString(), null) :
value.ToString();
value.ToString()!;

// also use this length when result is false.
written = UTF8NoBom.GetMaxByteCount(s.Length);
Expand Down
2 changes: 1 addition & 1 deletion src/ZString.Unity/Assets/Scripts/ZString/ZString.cs
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ static string JoinInternal<T>(ReadOnlySpan<char> separator, ReadOnlySpan<T> valu
}
else if (typeof(T) == typeof(string) && values.Length == 1)
{
return Unsafe.As<string>(values[0]);
return Unsafe.As<string>(values[0])!;
}

var sb = new Utf16ValueStringBuilder(true);
Expand Down
8 changes: 4 additions & 4 deletions src/ZString.Unity/Assets/Scripts/ZString/ZStringWriter.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Globalization;
using System.IO;
using System.Text;
Expand Down Expand Up @@ -82,7 +82,7 @@ public override void Write(char[] buffer, int index, int count)
sb.Append(buffer, index, count);
}

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

Expand All @@ -98,7 +98,7 @@ public override Task WriteAsync(char value)
return Task.CompletedTask;
}

public override Task WriteAsync(string value)
public override Task WriteAsync(string? value)
{
Write(value);
return Task.CompletedTask;
Expand All @@ -116,7 +116,7 @@ public override Task WriteLineAsync(char value)
return Task.CompletedTask;
}

public override Task WriteLineAsync(string value)
public override Task WriteLineAsync(string? value)
{
WriteLine(value);
return Task.CompletedTask;
Expand Down
6 changes: 3 additions & 3 deletions src/ZString/EnumUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
namespace Cysharp.Text
{
internal static class EnumUtil<T>
// where T : Enum
where T : notnull
{
const string InvalidName = "$";

Expand Down Expand Up @@ -45,7 +45,7 @@ public static bool TryFormatUtf16(T value, Span<char> dest, out int written, Rea
{
if (!names.TryGetValue(value, out var v) || v == InvalidName)
{
v = value!.ToString(); // T is Enum, not null always
v = value!.ToString()!; // T is Enum, not null always
}

written = v.Length;
Expand All @@ -56,7 +56,7 @@ public static bool TryFormatUtf8(T value, Span<byte> dest, out int written, Stan
{
if (!utf8names.TryGetValue(value, out var v) || v.Length == 0)
{
v = Encoding.UTF8.GetBytes(value!.ToString());
v = Encoding.UTF8.GetBytes(value.ToString()!);
}

written = v.Length;
Expand Down
6 changes: 3 additions & 3 deletions src/ZString/FormatHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ private static void FormatToRightJustify<TBufferWriter, T>(ref TBufferWriter sb,
{
if (typeof(T) == typeof(string))
{
var s = Unsafe.As<string>(arg);
var s = Unsafe.As<string>(arg) ?? "";
int padding = width - s.Length;
if (padding > 0)
{
Expand Down Expand Up @@ -130,7 +130,7 @@ private static void FormatToRightJustify<TBufferWriter, T>(ref TBufferWriter sb,
{
if (typeof(T) == typeof(string))
{
var s = Unsafe.As<string>(arg);
var s = Unsafe.As<string>(arg) ?? "";
int padding = width - s.Length;
if (padding > 0)
{
Expand Down Expand Up @@ -169,4 +169,4 @@ private static void FormatToRightJustify<TBufferWriter, T>(ref TBufferWriter sb,
}
}

}
}
6 changes: 3 additions & 3 deletions src/ZString/FormatHelper.tt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
Expand Down Expand Up @@ -55,7 +55,7 @@ namespace Cysharp.Text
{
if (typeof(T) == typeof(string))
{
var s = Unsafe.As<string>(arg);
var s = Unsafe.As<string>(arg) ?? "";
int padding = width - s.Length;
if (padding > 0)
{
Expand Down Expand Up @@ -101,4 +101,4 @@ namespace Cysharp.Text
}

<# } // foreach(utf) #>
}
}
4 changes: 3 additions & 1 deletion src/ZString/Number/Number.BigInteger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

#pragma warning disable CS9080,CS9082,CS9084,CS9091,CS9094

namespace System
{
internal static partial class Number
Expand Down Expand Up @@ -1219,4 +1221,4 @@ private static uint DivRem32(uint value, out uint remainder)
}
}
}
}
}
5 changes: 4 additions & 1 deletion src/ZString/Number/Number.Dragon4.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
using System.Numerics;
using System.Runtime.CompilerServices;

#pragma warning disable CS9080
#pragma warning disable CS9091

namespace System
{
// This is a port of the `Dragon4` implementation here: http://www.ryanjuckett.com/programming/printing-floating-point-numbers/part-2/
Expand Down Expand Up @@ -522,4 +525,4 @@ private static unsafe uint Dragon4(ulong mantissa, int exponent, uint mantissaHi
return outputLen;
}
}
}
}
Loading