Skip to content

Commit 7ad6554

Browse files
committed
Add ability to read dataitem without advancing
1 parent c3d6319 commit 7ad6554

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

src/Dahomey.Cbor.Tests/CborReaderTests.cs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using Dahomey.Cbor.Serialization;
22
using Xunit;
33
using System;
4-
using Dahomey.Cbor.Util;
54
using System.Globalization;
65
using System.Buffers;
76
using Dahomey.Cbor.Tests.Extensions;
@@ -337,6 +336,37 @@ public void ReadDataItem(string hexBuffer, string expectedIdValue, string expect
337336
Assert.Equal(expectedResultValue, resultValueItem.BytesToHex());
338337
}
339338

339+
[Theory]
340+
[InlineData("a262696468366534306233336266726573756c74c6f6", "683665343062333362", "c6f6")]
341+
[InlineData("a262696468366534306233336266726573756c74182a", "683665343062333362", "182a")]
342+
[InlineData("A262696468376235356365363566726573756C7463657965", "683762353563653635", "63657965")]
343+
public void ReadDataItemButWithoutAdvance(string hexBuffer, string expectedIdValue, string expectedResultValue)
344+
{
345+
var reader = new CborReader(hexBuffer.HexToBytes());
346+
347+
reader.ReadBeginMap();
348+
349+
int remainingItemCount = reader.ReadSize();
350+
351+
reader.MoveNextMapItem(ref remainingItemCount);
352+
var key1 = reader.ReadString();
353+
Assert.Equal("id", key1);
354+
355+
var idValueItem1 = reader.ReadDataItem(false);
356+
var idValueItem2 = reader.ReadDataItem();
357+
Assert.Equal(expectedIdValue, idValueItem1.BytesToHex());
358+
Assert.Equal(expectedIdValue, idValueItem2.BytesToHex());
359+
360+
reader.MoveNextMapItem(ref remainingItemCount);
361+
var key2 = reader.ReadString();
362+
Assert.Equal("result", key2);
363+
364+
var resultValueItem1 = reader.ReadDataItem(false);
365+
var resultValueItem2 = reader.ReadDataItem();
366+
Assert.Equal(expectedResultValue, resultValueItem1.BytesToHex());
367+
Assert.Equal(expectedResultValue, resultValueItem2.BytesToHex());
368+
}
369+
340370
[Theory]
341371
[InlineData("a262696468366534306233336266726573756c74c6f6", CborDataItemType.Null, "f6")]
342372
[InlineData("a262696468366534306233336266726573756c74182a", CborDataItemType.Unsigned, "182a")]

src/Dahomey.Cbor/Serialization/CborReader.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
using System.Buffers;
44
using System.Buffers.Binary;
55
using System.Buffers.Text;
6-
using System.Globalization;
76
using System.Runtime.CompilerServices;
87
using System.Runtime.InteropServices;
98
using System.Text;
@@ -799,15 +798,24 @@ private ReadOnlySpan<byte> ReadSizeAndBytes(bool allowScratchBuffer)
799798
}
800799

801800
[MethodImpl(MethodImplOptions.AggressiveInlining)]
802-
public ReadOnlySpan<byte> ReadDataItem()
801+
public ReadOnlySpan<byte> ReadDataItem(bool advance = true)
803802
{
803+
int positionBefore = _currentPos;
804+
var stateBefore = _state;
805+
804806
int headerOffset = _state == CborReaderState.Header ? 1 : 0;
805807
int currentDataItemPos = _currentPos - headerOffset;
806808

807809
SkipDataItem();
808810

809811
int size = _currentPos - currentDataItemPos;
810812

813+
if (!advance)
814+
{
815+
_currentPos = positionBefore;
816+
_state = stateBefore;
817+
}
818+
811819
return _buffer.Slice(currentDataItemPos, size);
812820
}
813821

0 commit comments

Comments
 (0)