Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Soreepeong committed Aug 26, 2024
1 parent a478966 commit ab65104
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 38 deletions.
28 changes: 24 additions & 4 deletions src/Lumina.Tests/SeStringBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -431,18 +431,38 @@ public void AddonIsParsedCorrectly()
}

[Fact]
public unsafe void InterpolationHandlerTest()
public unsafe void InterpolationHandlerTest1()
{
const string test = "asdf";
Assert.Equal(
"Left:1234 \nRight: 1234\nasdf\nint*: 0x0000000012345678",
new SeStringBuilder()
.Append( $"Left:{0x1234,-8:X}\nRight:{0x1234,8:X}\n{test}\nint*: 0x{(void*) 0x12345678:X16}" )
.ToReadOnlySeString()
.ToString() );
}

[Fact]
public void InterpolationHandlerTest2()
{
var boldHello = new SeStringBuilder().AppendBold( "Hello" ).ToReadOnlySeString();
Assert.Equal(
"Hex:0x 1234\nasdf\nint*: 0x0000000012345678\n|Left|\n<bold(1)>Hello<bold(0)>" ,
"|Left |\n| Right|\n<bold(1)>Hello<bold(0)>\nnull",
new SeStringBuilder()
.Append( $"Hex:0x{0x1234,8:X}\n{test}\nint*: 0x{(void*) 0x12345678:X16}\n|{"Left",-8}|\n{boldHello}" )
.Append( $"|{"Left",-8}|\n|{"Right"u8,8}|\n{boldHello}\n{(object) null}" )
.ToReadOnlySeString()
.ToString());
.ToString() );
}

[Fact]
public void InterpolationHandlerTest3() =>
Assert.Equal(
"<italic(1)>test<italic(0)>",
new SeStringBuilder()
.Append( $"{"<italic(1)>test<italic(0)>":m}" )
.ToReadOnlySeString()
.ToString() );

[Fact]
public void ThrowsOnInvalidMacroStrings1() =>
Assert.Throws< MacroStringParseException >( () => new SeStringBuilder().AppendMacroString( "<bad_payload>"u8 ) );
Expand Down
2 changes: 1 addition & 1 deletion src/Lumina/Text/Parse/MacroStringParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ private MacroCode ParseMacroCode( ref int offset )

macroCodeName = macroCodeName[ ..macroCodeNameLength ];

foreach( var n in Enum.GetValues< MacroCode >() )
foreach( var n in MacroCodeExtensions.GetDefinedMacroCodes())
{
if( macroCodeName.SequenceEqual( n.GetEncodeName() ) )
{
Expand Down
3 changes: 3 additions & 0 deletions src/Lumina/Text/Payloads/MacroCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ namespace Lumina.Text.Payloads;
/// <remarks>A <c>terminator</c> argument does not mean that an argument is required.</remarks>
public enum MacroCode : byte
{
/// <summary>Not a valid macro code. Representation for <c>default(MacroCode)</c>.</summary>
Invalid = 0,

/// <summary>Sets the reset time to the contextual time storage.</summary>
/// <remarks>Parameters: weekday, hour, terminator.</remarks>
[MacroCodeData( null, "n N x" )] SetResetTime = 0x06,
Expand Down
7 changes: 7 additions & 0 deletions src/Lumina/Text/Payloads/MacroCodeExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
using System;
using System.Linq;
using System.Reflection;

namespace Lumina.Text.Payloads;

/// <summary>Extension methods for <see cref="MacroCode"/>.</summary>
public static class MacroCodeExtensions
{
private static readonly MacroCode[] _definedMacroCodes = Enum.GetValues< MacroCode >().Where( x => x != MacroCode.Invalid ).ToArray();
private static readonly string?[] EncodedNames;

static MacroCodeExtensions()
Expand All @@ -20,6 +23,10 @@ static MacroCodeExtensions()
}
}

/// <summary>Gets all the defined macro codes.</summary>
/// <returns>Read-only span of macro codes.</returns>
public static ReadOnlySpan< MacroCode > GetDefinedMacroCodes() => _definedMacroCodes;

/// <summary>Gets the encoded name for an macro code, if available.</summary>
/// <param name="v">The macro code.</param>
/// <returns>The native name of the macro code, or <c>null</c> if not available.</returns>
Expand Down
27 changes: 15 additions & 12 deletions src/Lumina/Text/SeStringBuilder.Expressions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,7 @@ public SeStringBuilder AppendUIntExpression( uint value )
/// <returns>A reference of this instance after the append operation is completed.</returns>
public SeStringBuilder AppendNullaryExpression( ExpressionType expressionType )
{
if( _mss[ ^1 ].Type is not StackType.Payload and not StackType.Expression )
throw new InvalidOperationException( "Expression cannot be appended in current state." );
if( _mss[ ^1 ].Type == StackType.Expression && _mss[ ^1 ].Ident <= 0 )
throw new InvalidOperationException( $"No more expressions may be written. Call {nameof( EndExpression )}." );
EnsureExpressionWritableStateOrThrow();
if( expressionType.GetArity() != ExpressionArity.Nullary )
throw new ArgumentOutOfRangeException( nameof( expressionType ), expressionType, "Only nullary expression types are allowed." );
AllocateExpressionSpan( 1 )[ 0 ] = (byte) expressionType;
Expand All @@ -42,10 +39,7 @@ public SeStringBuilder AppendNullaryExpression( ExpressionType expressionType )
/// <returns>A reference of this instance after the operation is completed.</returns>
public SeStringBuilder BeginUnaryExpression( ExpressionType expressionType )
{
if( _mss[ ^1 ].Type is not StackType.Payload and not StackType.Expression )
throw new InvalidOperationException( "Expression cannot be appended in current state." );
if( _mss[ ^1 ].Type == StackType.Expression && _mss[ ^1 ].Ident <= 0 )
throw new InvalidOperationException( $"No more expressions may be written. Call {nameof( EndExpression )}." );
EnsureExpressionWritableStateOrThrow();
if( expressionType.GetArity() != ExpressionArity.Unary )
throw new ArgumentOutOfRangeException( nameof( expressionType ), expressionType, "Only unary expression types are allowed." );
if( _mssFree.Count == 0 )
Expand All @@ -67,10 +61,7 @@ public SeStringBuilder BeginUnaryExpression( ExpressionType expressionType )
/// <returns>A reference of this instance after the operation is completed.</returns>
public SeStringBuilder BeginBinaryExpression( ExpressionType expressionType )
{
if( _mss[ ^1 ].Type is not StackType.Payload and not StackType.Expression )
throw new InvalidOperationException( "Expression cannot be appended in current state." );
if( _mss[ ^1 ].Type == StackType.Expression && _mss[ ^1 ].Ident <= 0 )
throw new InvalidOperationException( $"No more expressions may be written. Call {nameof( EndExpression )}." );
EnsureExpressionWritableStateOrThrow();
if( expressionType.GetArity() != ExpressionArity.Binary )
throw new ArgumentOutOfRangeException( nameof( expressionType ), expressionType, "Only binary expression types are allowed." );
if( _mssFree.Count == 0 )
Expand Down Expand Up @@ -174,4 +165,16 @@ public SeStringBuilder AbortExpression()
_mssFree.Add( stream );
return this;
}

private bool IsExpressionWritableState() =>
_mss[ ^1 ].Type == StackType.Payload
|| ( _mss[ ^1 ].Type == StackType.Expression && _mss[ ^1 ].Ident > 0 );

private void EnsureExpressionWritableStateOrThrow()
{
if( _mss[ ^1 ].Type is not StackType.Payload and not StackType.Expression )
throw new InvalidOperationException( "Expression cannot be appended in current state." );
if( _mss[ ^1 ].Type == StackType.Expression && _mss[ ^1 ].Ident <= 0 )
throw new InvalidOperationException( $"No more expressions may be written. Call {nameof( EndExpression )}." );
}
}
56 changes: 35 additions & 21 deletions src/Lumina/Text/SeStringBuilder.InterpolatedStringHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,20 +71,28 @@ public void AppendFormatted< T >( T value, int alignment, string? format )
var prevLen = _builder.GetStringStream().Length;
switch( value )
{
case string s:
AppendFormatted( s.AsSpan(), format );
break;

#if NET8_0_OR_GREATER
case IUtf8SpanFormattable f:
AppendSpanFormattableUtf8( f, format );
break;
#endif

case ISpanFormattable f:
AppendSpanFormattableUtf16( f, format );
break;

case IFormattable f:
_builder.Append( f.ToString( format, _provider ) );
break;

case null:
_builder.Append( "null"u8 );
break;

default:
_builder.Append( value.ToString() );
break;
Expand Down Expand Up @@ -574,24 +582,6 @@ public void AppendFormatted( System.Net.IPNetwork value, int alignment, string?
}
#endif

/// <inheritdoc cref="AppendFormatted{T}(T)"/>
public void AppendFormatted( ReadOnlySpan< byte > value ) => AppendFormatted( value, 0, null );

/// <inheritdoc cref="AppendFormatted{T}(T, string?)"/>
public void AppendFormatted( ReadOnlySpan< byte > value, string? format ) => AppendFormatted( value, 0, format );

/// <inheritdoc cref="AppendFormatted{T}(T, int)"/>
public void AppendFormatted( ReadOnlySpan< byte > value, int alignment ) => AppendFormatted( value, alignment, null );

/// <inheritdoc cref="AppendFormatted{T}(T, int, string?)"/>
public void AppendFormatted( ReadOnlySpan< byte > value, int alignment, string? format )
{
_ = format;
var prevLen = _builder.GetStringStream().Length;
_builder.Append( value );
FixAlignment( prevLen, alignment );
}

/// <inheritdoc cref="AppendFormatted{T}(T)"/>
public void AppendFormatted( ReadOnlySeString value ) => AppendFormatted( value, 0, null );

Expand Down Expand Up @@ -682,6 +672,27 @@ public void AppendFormatted( SeString value, int alignment, string? format )
FixAlignment( prevLen, alignment );
}

/// <inheritdoc cref="AppendFormatted{T}(T)"/>
public void AppendFormatted( ReadOnlySpan< byte > value ) => AppendFormatted( value, 0, null );

/// <inheritdoc cref="AppendFormatted{T}(T, string?)"/>
public void AppendFormatted( ReadOnlySpan< byte > value, string? format ) => AppendFormatted( value, 0, format );

/// <inheritdoc cref="AppendFormatted{T}(T, int)"/>
public void AppendFormatted( ReadOnlySpan< byte > value, int alignment ) => AppendFormatted( value, alignment, null );

/// <inheritdoc cref="AppendFormatted{T}(T, int, string?)"/>
public void AppendFormatted( ReadOnlySpan< byte > value, int alignment, string? format )
{
_ = format;
var prevLen = _builder.GetStringStream().Length;
if( format?.StartsWith( 'm' ) is true )
_builder.AppendMacroString( value );
else
_builder.Append( value );
FixAlignment( prevLen, alignment );
}

/// <inheritdoc cref="AppendFormatted{T}(T)"/>
public void AppendFormatted( ReadOnlySpan< char > value ) => AppendFormatted( value, 0, null );

Expand All @@ -696,7 +707,10 @@ public void AppendFormatted( ReadOnlySpan< char > value, int alignment, string?
{
_ = format;
var prevLen = _builder.GetStringStream().Length;
_builder.Append( value );
if( format?.StartsWith( 'm' ) is true )
_builder.AppendMacroString( value );
else
_builder.Append( value );
FixAlignment( prevLen, alignment );
}

Expand All @@ -706,13 +720,13 @@ private void FixAlignment( long prevLen, int alignment )
return;

var len = (int) ( _builder.GetStringStream().Length - prevLen );
if( len <= -alignment || len >= alignment )
if( len >= Math.Abs( alignment ) )
return;

if( alignment < 0 )
{
// left align
_builder.AllocateStringSpan( len + alignment ).Fill( (byte) ' ' );
_builder.AllocateStringSpan( -( len + alignment ) ).Fill( (byte) ' ' );
}
else
{
Expand Down

0 comments on commit ab65104

Please sign in to comment.