Skip to content

Commit

Permalink
Merge pull request #5 from Cysharp/hadashiA/null-with-align
Browse files Browse the repository at this point in the history
Support alignment with null parameter value
  • Loading branch information
neuecc committed Nov 16, 2023
2 parents f7eb625 + f9c7a8c commit 8f4b9f1
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 19 deletions.
153 changes: 136 additions & 17 deletions src/Utf8StringInterpolation/Utf8String.AppendFormatted.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,14 @@ namespace Utf8StringInterpolation;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AppendFormatted(bool? value, int alignment = 0, string? format = null)
{
if (value == null) return;
if (value == null)
{
if (alignment != 0)
{
AppendFormatted("", alignment); // write only alignment
}
return;
}
AppendFormatted(value.Value, alignment, format);
}

Expand Down Expand Up @@ -91,7 +98,14 @@ void AppendFormattedAlignment(bool value, int alignment, string? format)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AppendFormatted(byte? value, int alignment = 0, string? format = null)
{
if (value == null) return;
if (value == null)
{
if (alignment != 0)
{
AppendFormatted("", alignment); // write only alignment
}
return;
}
AppendFormatted(value.Value, alignment, format);
}

Expand Down Expand Up @@ -171,7 +185,14 @@ void AppendFormattedAlignment(byte value, int alignment, string? format)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AppendFormatted(Decimal? value, int alignment = 0, string? format = null)
{
if (value == null) return;
if (value == null)
{
if (alignment != 0)
{
AppendFormatted("", alignment); // write only alignment
}
return;
}
AppendFormatted(value.Value, alignment, format);
}

Expand Down Expand Up @@ -251,7 +272,14 @@ void AppendFormattedAlignment(Decimal value, int alignment, string? format)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AppendFormatted(Double? value, int alignment = 0, string? format = null)
{
if (value == null) return;
if (value == null)
{
if (alignment != 0)
{
AppendFormatted("", alignment); // write only alignment
}
return;
}
AppendFormatted(value.Value, alignment, format);
}

Expand Down Expand Up @@ -331,7 +359,14 @@ void AppendFormattedAlignment(Double value, int alignment, string? format)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AppendFormatted(Guid? value, int alignment = 0, string? format = null)
{
if (value == null) return;
if (value == null)
{
if (alignment != 0)
{
AppendFormatted("", alignment); // write only alignment
}
return;
}
AppendFormatted(value.Value, alignment, format);
}

Expand Down Expand Up @@ -411,7 +446,14 @@ void AppendFormattedAlignment(Guid value, int alignment, string? format)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AppendFormatted(Int16? value, int alignment = 0, string? format = null)
{
if (value == null) return;
if (value == null)
{
if (alignment != 0)
{
AppendFormatted("", alignment); // write only alignment
}
return;
}
AppendFormatted(value.Value, alignment, format);
}

Expand Down Expand Up @@ -491,7 +533,14 @@ void AppendFormattedAlignment(Int16 value, int alignment, string? format)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AppendFormatted(Int32? value, int alignment = 0, string? format = null)
{
if (value == null) return;
if (value == null)
{
if (alignment != 0)
{
AppendFormatted("", alignment); // write only alignment
}
return;
}
AppendFormatted(value.Value, alignment, format);
}

Expand Down Expand Up @@ -571,7 +620,14 @@ void AppendFormattedAlignment(Int32 value, int alignment, string? format)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AppendFormatted(Int64? value, int alignment = 0, string? format = null)
{
if (value == null) return;
if (value == null)
{
if (alignment != 0)
{
AppendFormatted("", alignment); // write only alignment
}
return;
}
AppendFormatted(value.Value, alignment, format);
}

Expand Down Expand Up @@ -651,7 +707,14 @@ void AppendFormattedAlignment(Int64 value, int alignment, string? format)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AppendFormatted(SByte? value, int alignment = 0, string? format = null)
{
if (value == null) return;
if (value == null)
{
if (alignment != 0)
{
AppendFormatted("", alignment); // write only alignment
}
return;
}
AppendFormatted(value.Value, alignment, format);
}

Expand Down Expand Up @@ -731,7 +794,14 @@ void AppendFormattedAlignment(SByte value, int alignment, string? format)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AppendFormatted(Single? value, int alignment = 0, string? format = null)
{
if (value == null) return;
if (value == null)
{
if (alignment != 0)
{
AppendFormatted("", alignment); // write only alignment
}
return;
}
AppendFormatted(value.Value, alignment, format);
}

Expand Down Expand Up @@ -811,7 +881,14 @@ void AppendFormattedAlignment(Single value, int alignment, string? format)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AppendFormatted(UInt16? value, int alignment = 0, string? format = null)
{
if (value == null) return;
if (value == null)
{
if (alignment != 0)
{
AppendFormatted("", alignment); // write only alignment
}
return;
}
AppendFormatted(value.Value, alignment, format);
}

Expand Down Expand Up @@ -891,7 +968,14 @@ void AppendFormattedAlignment(UInt16 value, int alignment, string? format)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AppendFormatted(UInt32? value, int alignment = 0, string? format = null)
{
if (value == null) return;
if (value == null)
{
if (alignment != 0)
{
AppendFormatted("", alignment); // write only alignment
}
return;
}
AppendFormatted(value.Value, alignment, format);
}

Expand Down Expand Up @@ -971,7 +1055,14 @@ void AppendFormattedAlignment(UInt32 value, int alignment, string? format)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AppendFormatted(UInt64? value, int alignment = 0, string? format = null)
{
if (value == null) return;
if (value == null)
{
if (alignment != 0)
{
AppendFormatted("", alignment); // write only alignment
}
return;
}
AppendFormatted(value.Value, alignment, format);
}

Expand Down Expand Up @@ -1051,7 +1142,14 @@ void AppendFormattedAlignment(UInt64 value, int alignment, string? format)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AppendFormatted(DateTime? value, int alignment = 0, string? format = null)
{
if (value == null) return;
if (value == null)
{
if (alignment != 0)
{
AppendFormatted("", alignment); // write only alignment
}
return;
}
AppendFormatted(value.Value, alignment, format);
}

Expand Down Expand Up @@ -1131,7 +1229,14 @@ void AppendFormattedAlignment(DateTime value, int alignment, string? format)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AppendFormatted(DateTimeOffset? value, int alignment = 0, string? format = null)
{
if (value == null) return;
if (value == null)
{
if (alignment != 0)
{
AppendFormatted("", alignment); // write only alignment
}
return;
}
AppendFormatted(value.Value, alignment, format);
}

Expand Down Expand Up @@ -1211,7 +1316,14 @@ void AppendFormattedAlignment(DateTimeOffset value, int alignment, string? forma
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AppendFormatted(TimeSpan? value, int alignment = 0, string? format = null)
{
if (value == null) return;
if (value == null)
{
if (alignment != 0)
{
AppendFormatted("", alignment); // write only alignment
}
return;
}
AppendFormatted(value.Value, alignment, format);
}

Expand Down Expand Up @@ -1291,7 +1403,14 @@ void AppendFormattedAlignment(TimeSpan value, int alignment, string? format)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AppendFormatted(char? value, int alignment = 0, string? format = null)
{
if (value == null) return;
if (value == null)
{
if (alignment != 0)
{
AppendFormatted("", alignment); // write only alignment
}
return;
}
AppendFormatted(value.Value, alignment, format);
}

Expand Down
9 changes: 8 additions & 1 deletion src/Utf8StringInterpolation/Utf8String.AppendFormatted.tt
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,14 @@ public ref partial struct Utf8StringWriter<TBufferWriter>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AppendFormatted(<#= x.type #>? value, int alignment = 0, string? format = null)
{
if (value == null) return;
if (value == null)
{
if (alignment != 0)
{
AppendFormatted("", alignment); // write only alignment
}
return;
}
AppendFormatted(value.Value, alignment, format);
}

Expand Down
9 changes: 8 additions & 1 deletion src/Utf8StringInterpolation/Utf8StringWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,14 @@ 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;
if (value == null)
{
if (alignment != 0)
{
AppendFormatted("", alignment);
}
return;
}
AppendFormatted(value.Value, alignment, format);
}

Expand Down
10 changes: 10 additions & 0 deletions tests/Utf8StringInterpolation.Tests/FormatTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@ public void Nullable()
Utf8String.Format($"abc{guid}def{(Guid?)null}ghi").Should().Be($"abc{guid}def{(Guid?)null}ghi");
Utf8String.Format($"abc{(double?)Math.PI:e}def{(double?)null:e}ghi").Should().Be($"abc{(double?)Math.PI:e}def{(double?)null:e}ghi");
}

[Fact]
public void NullableWithAlignment()
{
var guid = (Guid?)Guid.NewGuid();
Utf8String.Format($"abc{(int?)100,10}def{(int?)null,10}ghi").Should().Be($"abc{(int?)100,10}def{(int?)null,10}ghi");
Utf8String.Format($"abc{(int?)100,-5:X}def{(int?)null,-5:X}ghi").Should().Be($"abc{(int?)100,-5:X}def{(int?)null,-5:X}ghi");
Utf8String.Format($"abc{guid}def{(Guid?)null,10}ghi").Should().Be($"abc{guid}def{(Guid?)null,10}ghi");
Utf8String.Format($"abc{(double?)null,5}def{(double?)null,5:e}ghi").Should().Be($"abc{(double?)null,5}def{(double?)null,5:e}ghi");
}

[Fact]
public void Comment()
Expand Down

0 comments on commit 8f4b9f1

Please sign in to comment.