Skip to content

Commit

Permalink
support nullable
Browse files Browse the repository at this point in the history
  • Loading branch information
neuecc committed Oct 19, 2023
1 parent 76125df commit 0acb4ca
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 4 deletions.
9 changes: 5 additions & 4 deletions sandbox/ConsoleApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@

//Console.WriteLine($"Foo{100,5}Bar");

var a= Utf8String.Format($"Double value is {123.456789:.###}");
var b = Utf8String.Format($"Today is {DateTime.Now:yyyy-MM-dd}");
Console.WriteLine( Encoding.UTF8.GetString(a));
Console.WriteLine( Encoding.UTF8.GetString(b));
int? foo = 23;

// var a = foo.GetType();

var test = Utf8String.Format($"foo{foo}");

//var actual = Utf8String.Format($"Prime numbers less than 10: {2,01}, {3,02}, {5,3:D}, {7,4: X}");
//actual.Should().Be(expected);
Expand Down
119 changes: 119 additions & 0 deletions src/Utf8StringInterpolation/Utf8String.AppendFormatted.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ public ref partial struct Utf8StringWriter<TBufferWriter>
{
# if true

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AppendFormatted(bool? value, int alignment = 0, string? format = null)
{
if (value == null) return;
AppendFormatted(value.Value, alignment, format);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AppendFormatted(bool value, int alignment = 0, string? format = null)
{
Expand Down Expand Up @@ -81,6 +88,13 @@ void AppendFormattedAlignment(bool value, int alignment, string? format)

#if !NET8_0_OR_GREATER

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AppendFormatted(byte? value, int alignment = 0, string? format = null)
{
if (value == null) return;
AppendFormatted(value.Value, alignment, format);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AppendFormatted(byte value, int alignment = 0, string? format = null)
{
Expand Down Expand Up @@ -154,6 +168,13 @@ void AppendFormattedAlignment(byte value, int alignment, string? format)

#if !NET8_0_OR_GREATER

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AppendFormatted(Decimal? value, int alignment = 0, string? format = null)
{
if (value == null) return;
AppendFormatted(value.Value, alignment, format);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AppendFormatted(Decimal value, int alignment = 0, string? format = null)
{
Expand Down Expand Up @@ -227,6 +248,13 @@ void AppendFormattedAlignment(Decimal value, int alignment, string? format)

#if !NET8_0_OR_GREATER

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AppendFormatted(Double? value, int alignment = 0, string? format = null)
{
if (value == null) return;
AppendFormatted(value.Value, alignment, format);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AppendFormatted(Double value, int alignment = 0, string? format = null)
{
Expand Down Expand Up @@ -300,6 +328,13 @@ void AppendFormattedAlignment(Double value, int alignment, string? format)

#if !NET8_0_OR_GREATER

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AppendFormatted(Guid? value, int alignment = 0, string? format = null)
{
if (value == null) return;
AppendFormatted(value.Value, alignment, format);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AppendFormatted(Guid value, int alignment = 0, string? format = null)
{
Expand Down Expand Up @@ -373,6 +408,13 @@ void AppendFormattedAlignment(Guid value, int alignment, string? format)

#if !NET8_0_OR_GREATER

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AppendFormatted(Int16? value, int alignment = 0, string? format = null)
{
if (value == null) return;
AppendFormatted(value.Value, alignment, format);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AppendFormatted(Int16 value, int alignment = 0, string? format = null)
{
Expand Down Expand Up @@ -446,6 +488,13 @@ void AppendFormattedAlignment(Int16 value, int alignment, string? format)

#if !NET8_0_OR_GREATER

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AppendFormatted(Int32? value, int alignment = 0, string? format = null)
{
if (value == null) return;
AppendFormatted(value.Value, alignment, format);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AppendFormatted(Int32 value, int alignment = 0, string? format = null)
{
Expand Down Expand Up @@ -519,6 +568,13 @@ void AppendFormattedAlignment(Int32 value, int alignment, string? format)

#if !NET8_0_OR_GREATER

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AppendFormatted(Int64? value, int alignment = 0, string? format = null)
{
if (value == null) return;
AppendFormatted(value.Value, alignment, format);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AppendFormatted(Int64 value, int alignment = 0, string? format = null)
{
Expand Down Expand Up @@ -592,6 +648,13 @@ void AppendFormattedAlignment(Int64 value, int alignment, string? format)

#if !NET8_0_OR_GREATER

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AppendFormatted(SByte? value, int alignment = 0, string? format = null)
{
if (value == null) return;
AppendFormatted(value.Value, alignment, format);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AppendFormatted(SByte value, int alignment = 0, string? format = null)
{
Expand Down Expand Up @@ -665,6 +728,13 @@ void AppendFormattedAlignment(SByte value, int alignment, string? format)

#if !NET8_0_OR_GREATER

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AppendFormatted(Single? value, int alignment = 0, string? format = null)
{
if (value == null) return;
AppendFormatted(value.Value, alignment, format);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AppendFormatted(Single value, int alignment = 0, string? format = null)
{
Expand Down Expand Up @@ -738,6 +808,13 @@ void AppendFormattedAlignment(Single value, int alignment, string? format)

#if !NET8_0_OR_GREATER

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AppendFormatted(UInt16? value, int alignment = 0, string? format = null)
{
if (value == null) return;
AppendFormatted(value.Value, alignment, format);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AppendFormatted(UInt16 value, int alignment = 0, string? format = null)
{
Expand Down Expand Up @@ -811,6 +888,13 @@ void AppendFormattedAlignment(UInt16 value, int alignment, string? format)

#if !NET8_0_OR_GREATER

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AppendFormatted(UInt32? value, int alignment = 0, string? format = null)
{
if (value == null) return;
AppendFormatted(value.Value, alignment, format);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AppendFormatted(UInt32 value, int alignment = 0, string? format = null)
{
Expand Down Expand Up @@ -884,6 +968,13 @@ void AppendFormattedAlignment(UInt32 value, int alignment, string? format)

#if !NET8_0_OR_GREATER

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AppendFormatted(UInt64? value, int alignment = 0, string? format = null)
{
if (value == null) return;
AppendFormatted(value.Value, alignment, format);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AppendFormatted(UInt64 value, int alignment = 0, string? format = null)
{
Expand Down Expand Up @@ -957,6 +1048,13 @@ void AppendFormattedAlignment(UInt64 value, int alignment, string? format)

#if !NET8_0_OR_GREATER

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AppendFormatted(DateTime? value, int alignment = 0, string? format = null)
{
if (value == null) return;
AppendFormatted(value.Value, alignment, format);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AppendFormatted(DateTime value, int alignment = 0, string? format = null)
{
Expand Down Expand Up @@ -1030,6 +1128,13 @@ void AppendFormattedAlignment(DateTime value, int alignment, string? format)

#if !NET8_0_OR_GREATER

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AppendFormatted(DateTimeOffset? value, int alignment = 0, string? format = null)
{
if (value == null) return;
AppendFormatted(value.Value, alignment, format);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AppendFormatted(DateTimeOffset value, int alignment = 0, string? format = null)
{
Expand Down Expand Up @@ -1103,6 +1208,13 @@ void AppendFormattedAlignment(DateTimeOffset value, int alignment, string? forma

#if !NET8_0_OR_GREATER

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AppendFormatted(TimeSpan? value, int alignment = 0, string? format = null)
{
if (value == null) return;
AppendFormatted(value.Value, alignment, format);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AppendFormatted(TimeSpan value, int alignment = 0, string? format = null)
{
Expand Down Expand Up @@ -1176,6 +1288,13 @@ void AppendFormattedAlignment(TimeSpan value, int alignment, string? format)

#if !NET8_0_OR_GREATER

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AppendFormatted(char? value, int alignment = 0, string? format = null)
{
if (value == null) return;
AppendFormatted(value.Value, alignment, format);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AppendFormatted(char value, int alignment = 0, string? format = null)
{
Expand Down
7 changes: 7 additions & 0 deletions src/Utf8StringInterpolation/Utf8String.AppendFormatted.tt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ public ref partial struct Utf8StringWriter<TBufferWriter>
<# foreach(var x in generateTypes) { #>
<#= (x.type is not "bool" or "char") ? "#if !NET8_0_OR_GREATER" : "# if true" #>

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AppendFormatted(<#= x.type #>? value, int alignment = 0, string? format = null)
{
if (value == null) return;
AppendFormatted(value.Value, alignment, format);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AppendFormatted(<#= x.type #> value, int alignment = 0, string? format = null)
{
Expand Down
7 changes: 7 additions & 0 deletions src/Utf8StringInterpolation/Utf8StringWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,13 @@ public void AppendFormatted(string value, int alignment = 0, string? format = nu
}
}

public void AppendFormatted<T>(Nullable<T> value, int alignment = 0, string? format = null)
where T : struct
{
if (value == null) return;
AppendFormatted(value.Value, alignment, format);
}

public void AppendFormatted<T>(T value, int alignment = 0, string? format = null)
{
// no alignment or add right whitespace
Expand Down

0 comments on commit 0acb4ca

Please sign in to comment.