~ruther/NosSmooth

ed1280ef5b7eeac883cf6ec9c3b308bdc91aea5f — František Boháček 3 years ago 3812133
feat: use StringBuilder Append overloads
M Core/NosSmooth.PacketSerializersGenerator/InlineConverterGenerators/BasicInlineConverterGenerator.cs => Core/NosSmooth.PacketSerializersGenerator/InlineConverterGenerators/BasicInlineConverterGenerator.cs +11 -1
@@ 30,8 30,18 @@ public class BasicInlineConverterGenerator : IInlineConverterGenerator
    public IError? GenerateSerializerPart(IndentedTextWriter textWriter, PacketInfo packet)
    {
        var parameter = packet.Parameters.Current;
        if (parameter.Nullable)
        {
            textWriter.WriteLine("if (obj is null)");
            textWriter.WriteLine("{");
            textWriter.WriteLine("builder.Append('-');");
            textWriter.WriteLine("}");
            textWriter.WriteLine("else");
        }
        textWriter.WriteLine("{");
        textWriter.WriteLine
            ($"builder.Append(obj.{parameter.Name}{(parameter.Nullable ? "?" : string.Empty)}.ToString() ?? \"-\");");
            ($"builder.Append(obj.{parameter.Name});");
        textWriter.WriteLine("}");
        return null;
    }


M Core/NosSmooth.PacketSerializersGenerator/InlineConverterGenerators/BoolInlineConverterGenerator.cs => Core/NosSmooth.PacketSerializersGenerator/InlineConverterGenerators/BoolInlineConverterGenerator.cs +2 -2
@@ 27,14 27,14 @@ public class BoolInlineConverterGenerator : IInlineConverterGenerator
            textWriter.WriteLine($"if (obj.{parameter.Name} is null)");
            textWriter.WriteLine("{");
            textWriter.Indent++;
            textWriter.WriteLine("builder.Append(\"-\");");
            textWriter.WriteLine("builder.Append('-');");
            textWriter.Indent--;
            textWriter.WriteLine("}");
            textWriter.WriteLine("else");
        }
        textWriter.WriteLine("{");
        textWriter.Indent++;
        textWriter.WriteLine($"builder.Append(obj.{parameter.Name} ? \"1\" : \"0\");");
        textWriter.WriteLine($"builder.Append(obj.{parameter.Name} ? '1' : '0');");
        textWriter.Indent--;
        textWriter.WriteLine("}");
        return null;

M Core/NosSmooth.PacketSerializersGenerator/InlineConverterGenerators/EnumInlineConverterGenerator.cs => Core/NosSmooth.PacketSerializersGenerator/InlineConverterGenerators/EnumInlineConverterGenerator.cs +13 -4
@@ 34,10 34,19 @@ public class EnumInlineConverterGenerator : IInlineConverterGenerator
    {
        var parameter = packet.Parameters.Current;
        var underlyingType = ((INamedTypeSymbol)parameter.Type).EnumUnderlyingType!.ToString();
        if (parameter.Nullable)
        {
            textWriter.WriteLine("if (obj is null)");
            textWriter.WriteLine("{");
            textWriter.WriteLine("builder.Append('-');");
            textWriter.WriteLine("}");
            textWriter.WriteLine("else");
        }
        textWriter.WriteLine("{");
        textWriter.WriteLine
        (
            $"builder.Append((({underlyingType}?)obj.{parameter.Name}{(parameter.Nullable ? "?" : string.Empty)}).ToString() ?? \"-\");"
        );
            ($"builder.Append(({underlyingType})obj.{parameter.Name});");
        textWriter.WriteLine("}");

        return null;
    }



@@ 89,4 98,4 @@ public static Result<{type}?> ParseEnum{type.ToString().Replace('.', '_')}(IType
            );
        }
    }
}
\ No newline at end of file
}

M Core/NosSmooth.Packets/Converters/Basic/BoolTypeConverter.cs => Core/NosSmooth.Packets/Converters/Basic/BoolTypeConverter.cs +1 -1
@@ 17,7 17,7 @@ public class BoolTypeConverter : BaseTypeConverter<bool>
    /// <inheritdoc />
    public override Result Serialize(bool obj, PacketStringBuilder builder)
    {
        builder.Append(obj ? "1" : "0");
        builder.Append(obj ? '1' : '0');
        return Result.FromSuccess();
    }


M Core/NosSmooth.Packets/Converters/Common/NameStringConverter.cs => Core/NosSmooth.Packets/Converters/Common/NameStringConverter.cs +1 -1
@@ 20,7 20,7 @@ public class NameStringConverter : BaseTypeConverter<NameString>
    {
        if (obj is null)
        {
            builder.Append("-");
            builder.Append('-');
            return Result.FromSuccess();
        }


M Core/NosSmooth.Packets/Converters/Packets/UpgradeRareSubPacketConverter.cs => Core/NosSmooth.Packets/Converters/Packets/UpgradeRareSubPacketConverter.cs +1 -1
@@ 20,7 20,7 @@ public class UpgradeRareSubPacketConverter : BaseTypeConverter<UpgradeRareSubPac
    {
        if (obj is null)
        {
            builder.Append("-");
            builder.Append('-');
            return Result.FromSuccess();
        }
        builder.Append($"{obj.Upgrade}{obj.Rare}");

M Core/NosSmooth.Packets/Converters/Special/EnumTypeConverter.cs => Core/NosSmooth.Packets/Converters/Special/EnumTypeConverter.cs +2 -2
@@ 35,11 35,11 @@ public class EnumTypeConverter : ISpecialTypeConverter
    {
        if (obj is null)
        {
            builder.Append("-");
            builder.Append('-');
            return Result.FromSuccess();
        }

        builder.Append(Convert.ToInt64(obj).ToString());
        builder.Append(Convert.ToInt64(obj));
        return Result.FromSuccess();
    }
}
\ No newline at end of file

M Core/NosSmooth.Packets/Converters/Special/ListTypeConverter.cs => Core/NosSmooth.Packets/Converters/Special/ListTypeConverter.cs +1 -3
@@ 79,7 79,7 @@ public class ListTypeConverter : ISpecialTypeConverter
    {
        if (obj is null)
        {
            builder.Append("-");
            builder.Append('-');
            return Result.FromSuccess();
        }



@@ 94,14 94,12 @@ public class ListTypeConverter : ISpecialTypeConverter
            }

            var serializeResult = _typeConverterRepository.Serialize(genericType, item, builder);
            builder.ReplaceWithParentSeparator();
            builder.PopLevel();
            if (!serializeResult.IsSuccess)
            {
                return serializeResult;
            }
        }
        builder.ReplaceWithParentSeparator();

        return Result.FromSuccess();
    }

M Core/NosSmooth.Packets/PacketStringBuilder.cs => Core/NosSmooth.Packets/PacketStringBuilder.cs +143 -1
@@ 4,6 4,7 @@
//  Copyright (c) František Boháček. All rights reserved.
//  Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Collections.Generic;
using System.Text;
using Remora.Results;


@@ 121,13 122,154 @@ public class PacketStringBuilder
    /// <param name="value">The value to append.</param>
    public void Append(string value)
    {
        BeforeAppend();
        _builder.Append(value);
        AfterAppend();
    }

    /// <summary>
    /// Appends the value to the string.
    /// </summary>
    /// <param name="value">The value to append.</param>
    public void Append(int value)
    {
        BeforeAppend();
        _builder.Append(value);
        AfterAppend();
    }

    /// <summary>
    /// Appends the value to the string.
    /// </summary>
    /// <param name="value">The value to append.</param>
    public void Append(uint value)
    {
        BeforeAppend();
        _builder.Append(value);
        AfterAppend();
    }

    /// <summary>
    /// Appends the value to the string.
    /// </summary>
    /// <param name="value">The value to append.</param>
    public void Append(short value)
    {
        BeforeAppend();
        _builder.Append(value);
        AfterAppend();
    }

    /// <summary>
    /// Appends the value to the string.
    /// </summary>
    /// <param name="value">The value to append.</param>
    public void Append(char value)
    {
        BeforeAppend();
        _builder.Append(value);
        AfterAppend();
    }

    /// <summary>
    /// Appends the value to the string.
    /// </summary>
    /// <param name="value">The value to append.</param>
    public void Append(ushort value)
    {
        BeforeAppend();
        _builder.Append(value);
        AfterAppend();
    }

    /// <summary>
    /// Appends the value to the string.
    /// </summary>
    /// <param name="value">The value to append.</param>
    public void Append(long value)
    {
        BeforeAppend();
        _builder.Append(value);
        AfterAppend();
    }

    /// <summary>
    /// Appends the value to the string.
    /// </summary>
    /// <param name="value">The value to append.</param>
    public void Append(ulong value)
    {
        BeforeAppend();
        _builder.Append(value);
        AfterAppend();
    }

    /// <summary>
    /// Appends the value to the string.
    /// </summary>
    /// <param name="value">The value to append.</param>
    public void Append(byte value)
    {
        BeforeAppend();
        _builder.Append(value);
        AfterAppend();
    }

    /// <summary>
    /// Appends the value to the string.
    /// </summary>
    /// <param name="value">The value to append.</param>
    public void Append(sbyte value)
    {
        BeforeAppend();
        _builder.Append(value);
        AfterAppend();
    }

    /// <summary>
    /// Appends the value to the string.
    /// </summary>
    /// <param name="value">The value to append.</param>
    public void Append(float value)
    {
        BeforeAppend();
        _builder.Append(value);
        AfterAppend();
    }

    /// <summary>
    /// Appends the value to the string.
    /// </summary>
    /// <param name="value">The value to append.</param>
    public void Append(double value)
    {
        BeforeAppend();
        _builder.Append(value);
        AfterAppend();
    }

    /// <summary>
    /// Appends the value to the string.
    /// </summary>
    /// <param name="value">The value to append.</param>
    public void Append(decimal value)
    {
        BeforeAppend();
        _builder.Append(value);
        AfterAppend();
    }

    private void BeforeAppend()
    {
        if (_insertSeparator is not null)
        {
            _builder.Append(_insertSeparator);
            _insertSeparator = null;
        }
    }

        _builder.Append(value);
    private void AfterAppend()
    {
        _insertSeparator = _currentLevel.SeparatorOnce ?? _currentLevel.Separator;
        _currentLevel.SeparatorOnce = null;
    }