From 638be8c52e1a21bbf2a5c818b1e8d7a0a80e3a0f Mon Sep 17 00:00:00 2001 From: Loretta Date: Fri, 15 May 2026 10:51:05 +0200 Subject: [PATCH] Refactor AcBinaryDeserializer for conciseness Refactored several methods to use ternary operators and single-line returns for early-exit and exception cases. Improved code readability by condensing multi-line if/else and null checks into concise expressions. No changes to functionality. --- .../Binaries/AcBinaryDeserializer.cs | 27 ++++++------------- 1 file changed, 8 insertions(+), 19 deletions(-) diff --git a/AyCode.Core/Serializers/Binaries/AcBinaryDeserializer.cs b/AyCode.Core/Serializers/Binaries/AcBinaryDeserializer.cs index 4c3e66b..c123d9f 100644 --- a/AyCode.Core/Serializers/Binaries/AcBinaryDeserializer.cs +++ b/AyCode.Core/Serializers/Binaries/AcBinaryDeserializer.cs @@ -1129,14 +1129,7 @@ public static partial class AcBinaryDeserializer // H2Q6: non-ASCII short strings now use StringSmall tier (handled below via TypeReaderTable dispatch). var reader = TypeReaderTable.Readers[typeCode]; - if (reader != null) - { - return reader(context, targetType); - } - - throw new AcBinaryDeserializationException( - $"Unknown type code: {typeCode}", - context.Position, targetType); + return reader != null ? reader(context, targetType) : throw new AcBinaryDeserializationException($"Unknown type code: {typeCode}", context.Position, targetType); } /// @@ -1151,8 +1144,8 @@ public static partial class AcBinaryDeserializer where TInput : struct, IBinaryInputBase { var length = (int)context.ReadVarUInt(); - if (length == 0) return string.Empty; - return context.ReadStringUtf8(length); + + return length == 0 ? string.Empty : context.ReadStringUtf8(length); } // ReadStringSmall / Medium / Big / PlainStringAscii and ReadAndRegisterInternedStringSmall / Medium @@ -1191,8 +1184,7 @@ public static partial class AcBinaryDeserializer private static object ConvertToTargetType(int value, Type targetType) { var info = GetConversionInfo(targetType); - if (info.IsEnum) - return Enum.ToObject(info.UnderlyingType, value); + if (info.IsEnum) return Enum.ToObject(info.UnderlyingType, value); return info.TypeCode switch { @@ -1281,12 +1273,10 @@ public static partial class AcBinaryDeserializer where TInput : struct, IBinaryInputBase { var length = (int)context.ReadVarUInt(); - if (length == 0) return []; - return context.ReadBytes(length); + + return length == 0 ? [] : context.ReadBytes(length); } - - #endregion #region Object Reading @@ -1376,9 +1366,8 @@ public static partial class AcBinaryDeserializer { var typeName = ReadPlainString(context); var resolvedType = AcSerializerCommon.ResolveTypeName(typeName) - ?? throw new AcBinaryDeserializationException( - $"Cannot resolve type '{typeName}' for ObjectWithTypeName at position {context.Position}.", - context.Position, null); + ?? throw new AcBinaryDeserializationException($"Cannot resolve type '{typeName}' for ObjectWithTypeName at position {context.Position}.", context.Position, null); + var wrapper = context.GetWrapper(resolvedType); context.RegisterPolymorphicWrapper(wrapper); // Next byte is the actual inner marker (Object/Array/Dict/etc.) — read it via ReadValue