Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ private void WriteStartArrayDefiniteLength(int definiteLength)

private void WriteStartArrayIndefiniteLength()
{
if (!ConvertIndefiniteLengthEncodings && CborConformanceModeHelpers.RequiresDefiniteLengthItems(ConformanceMode))
if (!ConvertIndefiniteLengthEncodings && _requiresDefiniteLengthItems)
{
throw new InvalidOperationException(SR.Format(SR.Cbor_ConformanceMode_IndefiniteLengthItemsNotSupported, ConformanceMode));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ private void WriteStartMapDefiniteLength(int definiteLength)

private void WriteStartMapIndefiniteLength()
{
if (!ConvertIndefiniteLengthEncodings && CborConformanceModeHelpers.RequiresDefiniteLengthItems(ConformanceMode))
if (!ConvertIndefiniteLengthEncodings && _requiresDefiniteLengthItems)
Comment thread
bartonjs marked this conversation as resolved.
Resolved
{
throw new InvalidOperationException(SR.Format(SR.Cbor_ConformanceMode_IndefiniteLengthItemsNotSupported, ConformanceMode));
}
Expand All @@ -96,7 +96,7 @@ private void HandleMapKeyWritten()
{
Debug.Assert(_currentKeyOffset != null && _currentValueOffset == null);

if (CborConformanceModeHelpers.RequiresUniqueKeys(ConformanceMode))
if (_requiresUniqueKeys)
{
HashSet<(int Offset, int Length)> keyEncodingRanges = GetKeyEncodingRanges();

Expand All @@ -120,7 +120,7 @@ private void HandleMapValueWritten()
{
Debug.Assert(_currentKeyOffset != null && _currentValueOffset != null);

if (CborConformanceModeHelpers.RequiresSortedKeys(ConformanceMode))
if (_requiresSortedKeys)
{
List<KeyValuePairEncodingRange> keyValuePairEncodingRanges = GetKeyValueEncodingRanges();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public void WriteByteString(ReadOnlySpan<byte> value)
/// </remarks>
public void WriteStartIndefiniteLengthByteString()
{
if (!ConvertIndefiniteLengthEncodings && CborConformanceModeHelpers.RequiresDefiniteLengthItems(ConformanceMode))
if (!ConvertIndefiniteLengthEncodings && _requiresDefiniteLengthItems)
{
throw new InvalidOperationException(SR.Format(SR.Cbor_ConformanceMode_IndefiniteLengthItemsNotSupported, ConformanceMode));
}
Expand Down Expand Up @@ -122,7 +122,7 @@ public void WriteTextString(string value)
/// <para>The written data is not accepted under the current conformance mode.</para></exception>
public void WriteTextString(ReadOnlySpan<char> value)
{
Encoding utf8Encoding = CborConformanceModeHelpers.GetUtf8Encoding(ConformanceMode);
Encoding utf8Encoding = _encoding;

int length;
try
Expand Down Expand Up @@ -164,7 +164,7 @@ public void WriteTextString(ReadOnlySpan<char> value)
/// </remarks>
public void WriteStartIndefiniteLengthTextString()
{
if (!ConvertIndefiniteLengthEncodings && CborConformanceModeHelpers.RequiresDefiniteLengthItems(ConformanceMode))
if (!ConvertIndefiniteLengthEncodings && _requiresDefiniteLengthItems)
{
throw new InvalidOperationException(SR.Format(SR.Cbor_ConformanceMode_IndefiniteLengthItemsNotSupported, ConformanceMode));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.ComponentModel;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Text;

namespace System.Formats.Cbor
{
Expand All @@ -16,6 +17,13 @@ public partial class CborWriter
private const int DefaultCapacitySentinel = -1;
private static readonly ArrayPool<byte> s_bufferPool = ArrayPool<byte>.Create();

// Conformance-mode behaviors
private readonly bool _requiresUniqueKeys;
private readonly bool _requiresSortedKeys;
private readonly bool _requiresDefiniteLengthItems;
private readonly bool _tracksMapKeys; // map key and value offsets are only needed by the unique and sorted key checks
private readonly Encoding _encoding;

private byte[] _buffer;
private int _offset;

Expand Down Expand Up @@ -84,6 +92,11 @@ public CborWriter(CborWriterOptions? options)
}

ConformanceMode = conformanceMode;
_requiresUniqueKeys = CborConformanceModeHelpers.RequiresUniqueKeys(conformanceMode);
_requiresSortedKeys = CborConformanceModeHelpers.RequiresSortedKeys(conformanceMode);
_requiresDefiniteLengthItems = CborConformanceModeHelpers.RequiresDefiniteLengthItems(conformanceMode);
_tracksMapKeys = _requiresUniqueKeys || _requiresSortedKeys;
_encoding = CborConformanceModeHelpers.GetUtf8Encoding(conformanceMode);
ConvertIndefiniteLengthEncodings = convertIndefiniteLengthEncodings;
AllowMultipleRootLevelValues = allowMultipleRootLevelValues;
MaxDepth = maxDepth < 0 ? DefaultMaxDepth : maxDepth;
Expand Down Expand Up @@ -124,6 +137,11 @@ public CborWriter(
CborConformanceModeHelpers.Validate(conformanceMode);

ConformanceMode = conformanceMode;
_requiresUniqueKeys = CborConformanceModeHelpers.RequiresUniqueKeys(conformanceMode);
_requiresSortedKeys = CborConformanceModeHelpers.RequiresSortedKeys(conformanceMode);
_requiresDefiniteLengthItems = CborConformanceModeHelpers.RequiresDefiniteLengthItems(conformanceMode);
_tracksMapKeys = _requiresUniqueKeys || _requiresSortedKeys;
_encoding = CborConformanceModeHelpers.GetUtf8Encoding(conformanceMode);
ConvertIndefiniteLengthEncodings = convertIndefiniteLengthEncodings;
AllowMultipleRootLevelValues = allowMultipleRootLevelValues;
MaxDepth = DefaultMaxDepth;
Expand Down Expand Up @@ -368,7 +386,7 @@ private void PopDataItem(CborMajorType typeToPop)
// Perform encoding fixups that require the current context and must be done before popping
// NB map key sorting must happen _before_ indefinite-length patching

if (typeToPop == CborMajorType.Map)
if (_tracksMapKeys && typeToPop == CborMajorType.Map)
{
CompleteMapWrite();
}
Expand All @@ -394,7 +412,7 @@ private void PopDataItem(CborMajorType typeToPop)
// Advance writer state after a data item has been written to the buffer
private void AdvanceDataItemCounters()
{
if (_currentMajorType == CborMajorType.Map)
if (_tracksMapKeys && _currentMajorType == CborMajorType.Map)
{
if (_itemsWritten % 2 == 0)
{
Expand Down
Loading