From 998ce1e07772dab0c289f8dd661c649dad9b69df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Boh=C3=A1=C4=8Dek?= Date: Sun, 15 Jan 2023 14:02:13 +0100 Subject: [PATCH] feat(packets): add possibility to increment read tokens for arrays using capture and increment read tokens --- .../PacketStringEnumerator.cs | 39 +++++++++++++++++++ .../Special/Converters/ListStringConverter.cs | 3 +- .../ListInlineConverterGenerator.cs | 2 + 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/Packets/NosSmooth.PacketSerializer.Abstractions/PacketStringEnumerator.cs b/Packets/NosSmooth.PacketSerializer.Abstractions/PacketStringEnumerator.cs index 1b69717..407da61 100644 --- a/Packets/NosSmooth.PacketSerializer.Abstractions/PacketStringEnumerator.cs +++ b/Packets/NosSmooth.PacketSerializer.Abstractions/PacketStringEnumerator.cs @@ -53,6 +53,43 @@ public ref struct PacketStringEnumerator _currentLevel.SeparatorOnce = separator; } + /// + /// Capture current read tokens number. + /// + /// + /// Usable for lists that may have indeterminate separators (same multiple separators). + /// The list converter may capture read tokens before reading an element and increment after token is read. + /// + public void CaptureReadTokens() + { + _currentLevel.CapturedTokensRead = _currentLevel.TokensRead; + } + + /// + /// Increment read tokens from the captured state taht was captured using . + /// + /// + /// Usable for lists that may have indeterminate separators (same multiple separators). + /// The list converter may capture read tokens before reading an element and increment after token is read. + /// + /// Thrown in case was not called before. + public void IncrementReadTokens() + { + if (_currentLevel.CapturedTokensRead is null) + { + throw new InvalidOperationException + ("The read tokens cannot be incremented as CaptureReadTokens was not called."); + } + + _currentLevel.TokensRead = _currentLevel.CapturedTokensRead.Value + 1; + _currentLevel.CapturedTokensRead = null; + + if (_currentLevel.TokensRead >= _currentLevel.MaxTokens) + { + _currentLevel.ReachedEnd = true; + } + } + /// /// Sets that the next token should be read to the last entry in the level. /// @@ -367,5 +404,7 @@ public ref struct PacketStringEnumerator public bool? ReachedEnd { get; set; } public (char Separator, uint? MaxTokens)? PreparedLevel { get; set; } + + public uint? CapturedTokensRead { get; set; } } } \ No newline at end of file diff --git a/Packets/NosSmooth.PacketSerializer/Converters/Special/Converters/ListStringConverter.cs b/Packets/NosSmooth.PacketSerializer/Converters/Special/Converters/ListStringConverter.cs index 3555515..3ec3db7 100644 --- a/Packets/NosSmooth.PacketSerializer/Converters/Special/Converters/ListStringConverter.cs +++ b/Packets/NosSmooth.PacketSerializer/Converters/Special/Converters/ListStringConverter.cs @@ -26,7 +26,6 @@ public class ListStringConverter : BaseStringConverter @@ -63,6 +62,7 @@ public class ListStringConverter : BaseStringConverter : BaseStringConverter?>.FromError(new ListSerializerError(result, list.Count), result); diff --git a/Packets/NosSmooth.PacketSerializersGenerator/InlineConverterGenerators/ListInlineConverterGenerator.cs b/Packets/NosSmooth.PacketSerializersGenerator/InlineConverterGenerators/ListInlineConverterGenerator.cs index dc14aae..2a9610a 100644 --- a/Packets/NosSmooth.PacketSerializersGenerator/InlineConverterGenerators/ListInlineConverterGenerator.cs +++ b/Packets/NosSmooth.PacketSerializersGenerator/InlineConverterGenerators/ListInlineConverterGenerator.cs @@ -112,6 +112,7 @@ public static Result> {GetMethodName(type, while (!(stringEnumerator.IsOnLastToken() ?? false)) {{ + stringEnumerator.CaptureReadTokens(); if (!stringEnumerator.PushPreparedLevel()) {{ return new ArgumentInvalidError(nameof(stringEnumerator), ""The string enumerator has to have a prepared level for all lists.""); @@ -132,6 +133,7 @@ public static Result> {GetMethodName(type, }} stringEnumerator.PopLevel(); + stringEnumerator.IncrementReadTokens(); if (!result.IsSuccess) {{ return Result>.FromError(new ListSerializerError(result, data.Count), result); -- 2.48.1