From ed1280ef5b7eeac883cf6ec9c3b308bdc91aea5f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Boh=C3=A1=C4=8Dek?= Date: Sun, 2 Jan 2022 00:22:14 +0100 Subject: [PATCH] feat: use StringBuilder Append overloads --- .../BasicInlineConverterGenerator.cs | 12 +- .../BoolInlineConverterGenerator.cs | 4 +- .../EnumInlineConverterGenerator.cs | 17 ++- .../Converters/Basic/BoolTypeConverter.cs | 2 +- .../Converters/Common/NameStringConverter.cs | 2 +- .../Packets/UpgradeRareSubPacketConverter.cs | 2 +- .../Converters/Special/EnumTypeConverter.cs | 4 +- .../Converters/Special/ListTypeConverter.cs | 4 +- Core/NosSmooth.Packets/PacketStringBuilder.cs | 144 +++++++++++++++++- 9 files changed, 175 insertions(+), 16 deletions(-) diff --git a/Core/NosSmooth.PacketSerializersGenerator/InlineConverterGenerators/BasicInlineConverterGenerator.cs b/Core/NosSmooth.PacketSerializersGenerator/InlineConverterGenerators/BasicInlineConverterGenerator.cs index 70660a63f07a760697d2e609eedd4adcbf86d843..82bb0e01cfeacc8c7578c1bffb1f46ae3f501f2c 100644 --- a/Core/NosSmooth.PacketSerializersGenerator/InlineConverterGenerators/BasicInlineConverterGenerator.cs +++ b/Core/NosSmooth.PacketSerializersGenerator/InlineConverterGenerators/BasicInlineConverterGenerator.cs @@ -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; } diff --git a/Core/NosSmooth.PacketSerializersGenerator/InlineConverterGenerators/BoolInlineConverterGenerator.cs b/Core/NosSmooth.PacketSerializersGenerator/InlineConverterGenerators/BoolInlineConverterGenerator.cs index de418e20f143a26c9a3f2174ea2c98528758688f..a04bba428ad1867503680e04d1dbbc2eb18158bd 100644 --- a/Core/NosSmooth.PacketSerializersGenerator/InlineConverterGenerators/BoolInlineConverterGenerator.cs +++ b/Core/NosSmooth.PacketSerializersGenerator/InlineConverterGenerators/BoolInlineConverterGenerator.cs @@ -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; diff --git a/Core/NosSmooth.PacketSerializersGenerator/InlineConverterGenerators/EnumInlineConverterGenerator.cs b/Core/NosSmooth.PacketSerializersGenerator/InlineConverterGenerators/EnumInlineConverterGenerator.cs index 551d373ca6a9a2bb6b6ef99f9bc19b786daf336d..e7de90508669801aa6866978866bd5ee7ec8ad8e 100644 --- a/Core/NosSmooth.PacketSerializersGenerator/InlineConverterGenerators/EnumInlineConverterGenerator.cs +++ b/Core/NosSmooth.PacketSerializersGenerator/InlineConverterGenerators/EnumInlineConverterGenerator.cs @@ -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 +} diff --git a/Core/NosSmooth.Packets/Converters/Basic/BoolTypeConverter.cs b/Core/NosSmooth.Packets/Converters/Basic/BoolTypeConverter.cs index f154666f42a90a081790acd4601ad8e6d7f02459..d87a5cd139b73df0746c505df668d51235c8959e 100644 --- a/Core/NosSmooth.Packets/Converters/Basic/BoolTypeConverter.cs +++ b/Core/NosSmooth.Packets/Converters/Basic/BoolTypeConverter.cs @@ -17,7 +17,7 @@ public class BoolTypeConverter : BaseTypeConverter /// public override Result Serialize(bool obj, PacketStringBuilder builder) { - builder.Append(obj ? "1" : "0"); + builder.Append(obj ? '1' : '0'); return Result.FromSuccess(); } diff --git a/Core/NosSmooth.Packets/Converters/Common/NameStringConverter.cs b/Core/NosSmooth.Packets/Converters/Common/NameStringConverter.cs index 030a90b42533bb8d69ecbdb009ee0bf9622e6593..7fe3940261b4e9ec7c449216addf538d9a614a7b 100644 --- a/Core/NosSmooth.Packets/Converters/Common/NameStringConverter.cs +++ b/Core/NosSmooth.Packets/Converters/Common/NameStringConverter.cs @@ -20,7 +20,7 @@ public class NameStringConverter : BaseTypeConverter { if (obj is null) { - builder.Append("-"); + builder.Append('-'); return Result.FromSuccess(); } diff --git a/Core/NosSmooth.Packets/Converters/Packets/UpgradeRareSubPacketConverter.cs b/Core/NosSmooth.Packets/Converters/Packets/UpgradeRareSubPacketConverter.cs index f35a579f526779400d110ad66d540ebe116ccb01..e325bedd5b97ce10c3a8b0b28eff6339acb237af 100644 --- a/Core/NosSmooth.Packets/Converters/Packets/UpgradeRareSubPacketConverter.cs +++ b/Core/NosSmooth.Packets/Converters/Packets/UpgradeRareSubPacketConverter.cs @@ -20,7 +20,7 @@ public class UpgradeRareSubPacketConverter : BaseTypeConverter /// The value to append. public void Append(string value) + { + BeforeAppend(); + _builder.Append(value); + AfterAppend(); + } + + /// + /// Appends the value to the string. + /// + /// The value to append. + public void Append(int value) + { + BeforeAppend(); + _builder.Append(value); + AfterAppend(); + } + + /// + /// Appends the value to the string. + /// + /// The value to append. + public void Append(uint value) + { + BeforeAppend(); + _builder.Append(value); + AfterAppend(); + } + + /// + /// Appends the value to the string. + /// + /// The value to append. + public void Append(short value) + { + BeforeAppend(); + _builder.Append(value); + AfterAppend(); + } + + /// + /// Appends the value to the string. + /// + /// The value to append. + public void Append(char value) + { + BeforeAppend(); + _builder.Append(value); + AfterAppend(); + } + + /// + /// Appends the value to the string. + /// + /// The value to append. + public void Append(ushort value) + { + BeforeAppend(); + _builder.Append(value); + AfterAppend(); + } + + /// + /// Appends the value to the string. + /// + /// The value to append. + public void Append(long value) + { + BeforeAppend(); + _builder.Append(value); + AfterAppend(); + } + + /// + /// Appends the value to the string. + /// + /// The value to append. + public void Append(ulong value) + { + BeforeAppend(); + _builder.Append(value); + AfterAppend(); + } + + /// + /// Appends the value to the string. + /// + /// The value to append. + public void Append(byte value) + { + BeforeAppend(); + _builder.Append(value); + AfterAppend(); + } + + /// + /// Appends the value to the string. + /// + /// The value to append. + public void Append(sbyte value) + { + BeforeAppend(); + _builder.Append(value); + AfterAppend(); + } + + /// + /// Appends the value to the string. + /// + /// The value to append. + public void Append(float value) + { + BeforeAppend(); + _builder.Append(value); + AfterAppend(); + } + + /// + /// Appends the value to the string. + /// + /// The value to append. + public void Append(double value) + { + BeforeAppend(); + _builder.Append(value); + AfterAppend(); + } + + /// + /// Appends the value to the string. + /// + /// The value to append. + 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; }