Skip to content

Commit dd8a1fb

Browse files
committed
Remove AsSpan, add AsArraySpan
1 parent 17edcc7 commit dd8a1fb

File tree

7 files changed

+46
-44
lines changed

7 files changed

+46
-44
lines changed

ValveKeyValue/ValveKeyValue.Test/Test Data/apisurface.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ public class ValveKeyValue.KVDocument
3737
public .ctor(ValveKeyValue.KVHeader header, string name, ValveKeyValue.KVObject root);
3838
public void Add(string key, ValveKeyValue.KVObject value);
3939
public void Add(ValveKeyValue.KVObject value);
40+
public Span`1[[ValveKeyValue.KVObject]] AsArraySpan();
4041
public byte[] AsBlob();
41-
public ReadOnlySpan`1[[byte]] AsSpan();
4242
public void Clear();
4343
public bool ContainsKey(string name);
4444
public bool Equals(object obj);
@@ -169,8 +169,8 @@ public class ValveKeyValue.KVObject
169169
public static ValveKeyValue.KVObject Array();
170170
public static ValveKeyValue.KVObject Array(int capacity);
171171
public static ValveKeyValue.KVObject Array(System.Collections.Generic.IEnumerable`1[[ValveKeyValue.KVObject]] elements);
172+
public Span`1[[ValveKeyValue.KVObject]] AsArraySpan();
172173
public byte[] AsBlob();
173-
public ReadOnlySpan`1[[byte]] AsSpan();
174174
public static ValveKeyValue.KVObject Blob(byte[] data);
175175
public void Clear();
176176
public static ValveKeyValue.KVObject Collection();

ValveKeyValue/ValveKeyValue.Test/TextKV3/ValveFormatTestCase.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -132,13 +132,13 @@ public void SerializesBinaryBlobsRoundTrip()
132132

133133
var blob32 = data2["blob32"];
134134
Assert.That(blob32.AsBlob().Length, Is.EqualTo(32));
135-
Assert.That(blob32.AsSpan()[0], Is.EqualTo(0x00));
136-
Assert.That(blob32.AsSpan()[31], Is.EqualTo(0x1F));
135+
Assert.That(blob32.AsBlob()[0], Is.EqualTo(0x00));
136+
Assert.That(blob32.AsBlob()[31], Is.EqualTo(0x1F));
137137

138138
var blob100 = data2["blob100"];
139139
Assert.That(blob100.AsBlob().Length, Is.EqualTo(100));
140-
Assert.That(blob100.AsSpan()[0], Is.EqualTo(0x00));
141-
Assert.That(blob100.AsSpan()[99], Is.EqualTo(0x63));
140+
Assert.That(blob100.AsBlob()[0], Is.EqualTo(0x00));
141+
Assert.That(blob100.AsBlob()[99], Is.EqualTo(0x63));
142142
}
143143

144144
[Test]
@@ -345,8 +345,8 @@ public void BinaryBlobInlineRoundTrip()
345345
Assert.That(data2["small"].AsBlob(), Is.EqualTo(new byte[] { 0x11, 0xFF }));
346346
var exact32 = data2["exact32"];
347347
Assert.That(exact32.AsBlob().Length, Is.EqualTo(32));
348-
Assert.That(exact32.AsSpan()[0], Is.EqualTo(0x00));
349-
Assert.That(exact32.AsSpan()[31], Is.EqualTo(0x1F));
348+
Assert.That(exact32.AsBlob()[0], Is.EqualTo(0x00));
349+
Assert.That(exact32.AsBlob()[31], Is.EqualTo(0x1F));
350350
});
351351
}
352352

ValveKeyValue/ValveKeyValue/Abstraction/KVObjectVisitor.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ void VisitObject(string? name, KVObject obj, bool isArray)
3030
break;
3131

3232
case KVValueType.Array:
33-
var arrayList = obj.GetArrayList();
33+
var arrayList = obj.AsArraySpan();
3434
var allSimple = true;
3535
foreach (var element in arrayList)
3636
{
@@ -40,7 +40,7 @@ void VisitObject(string? name, KVObject obj, bool isArray)
4040
break;
4141
}
4242
}
43-
listener.OnArrayStart(name, obj.Flag, arrayList.Count, allSimple);
43+
listener.OnArrayStart(name, obj.Flag, arrayList.Length, allSimple);
4444
foreach (var element in arrayList)
4545
{
4646
VisitObject(null, element, true);

ValveKeyValue/ValveKeyValue/KVObject.cs

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Diagnostics.CodeAnalysis;
33
using System.Globalization;
44
using System.Linq;
5+
using System.Runtime.InteropServices;
56

67
namespace ValveKeyValue
78
{
@@ -55,7 +56,7 @@ public partial class KVObject : IReadOnlyDictionary<string, KVObject>
5556
public int Count => ValueType switch
5657
{
5758
KVValueType.Collection => GetCollectionCount(),
58-
KVValueType.Array => GetArrayList().Count,
59+
KVValueType.Array => ((List<KVObject>)_ref!).Count,
5960
_ => 0,
6061
};
6162

@@ -223,10 +224,11 @@ public KVObject this[int index]
223224
{
224225
if (ValueType == KVValueType.Array)
225226
{
226-
return GetArrayList()[index];
227+
var listArray = (List<KVObject>)_ref!;
228+
return listArray[index];
227229
}
228230

229-
if (_ref is List<KeyValuePair<string, KVObject>> list && ValueType == KVValueType.Collection)
231+
if (ValueType == KVValueType.Collection && _ref is List<KeyValuePair<string, KVObject>> list)
230232
{
231233
return list[index].Value;
232234
}
@@ -280,7 +282,7 @@ public bool ContainsKey(string name)
280282
/// </summary>
281283
public IEnumerable<KeyValuePair<string, KVObject>> Children => ValueType switch
282284
{
283-
KVValueType.Collection => GetCollectionChildren(),
285+
KVValueType.Collection => EnumerateCollection(),
284286
KVValueType.Array => EnumerateArray(),
285287
_ => [],
286288
};
@@ -345,7 +347,8 @@ public void Add(KVObject value)
345347
throw new InvalidOperationException($"Cannot add an array element to a {ValueType} value.");
346348
}
347349

348-
GetArrayList().Add(value ?? Null());
350+
var list = (List<KVObject>)_ref!;
351+
list.Add(value ?? Null());
349352
}
350353

351354
/// <summary>
@@ -374,7 +377,8 @@ public void RemoveAt(int index)
374377
throw new InvalidOperationException($"Cannot remove by index from a {ValueType} value.");
375378
}
376379

377-
GetArrayList().RemoveAt(index);
380+
var list = (List<KVObject>)_ref!;
381+
list.RemoveAt(index);
378382
}
379383

380384
/// <summary>
@@ -492,7 +496,7 @@ public static KVObject Blob(byte[] data)
492496

493497
#endregion
494498

495-
#region Blob access
499+
#region Blob and list access
496500

497501
/// <summary>Gets the binary blob data as a byte array.</summary>
498502
public byte[] AsBlob()
@@ -505,26 +509,19 @@ public byte[] AsBlob()
505509
return (byte[])_ref!;
506510
}
507511

508-
/// <summary>Gets the binary blob data as a span.</summary>
509-
public ReadOnlySpan<byte> AsSpan()
512+
/// <summary>Gets the array elements as a span.</summary>
513+
public Span<KVObject> AsArraySpan()
510514
{
511-
if (ValueType != KVValueType.BinaryBlob)
515+
if (ValueType != KVValueType.Array)
512516
{
513-
throw new InvalidOperationException($"Cannot get blob span from a {ValueType} value.");
517+
throw new InvalidOperationException($"Cannot get list from a {ValueType} value.");
514518
}
515519

516-
return ((byte[])_ref!).AsSpan();
520+
return CollectionsMarshal.AsSpan((List<KVObject>)_ref!);
517521
}
518522

519523
#endregion
520524

521-
#region Internal accessors
522-
523-
internal List<KVObject> GetArrayList()
524-
=> (List<KVObject>)_ref!;
525-
526-
#endregion
527-
528525
#region Private helpers
529526

530527
private enum InsertionBehavior
@@ -593,7 +590,7 @@ private bool TryInsert(string key, KVObject value, InsertionBehavior behavior)
593590
_ => 0,
594591
};
595592

596-
private IEnumerable<KeyValuePair<string, KVObject>> GetCollectionChildren() => _ref switch
593+
private IEnumerable<KeyValuePair<string, KVObject>> EnumerateCollection() => _ref switch
597594
{
598595
Dictionary<string, KVObject> dict => dict,
599596
List<KeyValuePair<string, KVObject>> list => list,
@@ -602,10 +599,10 @@ private bool TryInsert(string key, KVObject value, InsertionBehavior behavior)
602599

603600
private IEnumerable<KeyValuePair<string, KVObject>> EnumerateArray()
604601
{
605-
var list = GetArrayList();
606-
for (var i = 0; i < list.Count; i++)
602+
var list = (List<KVObject>)_ref!;
603+
foreach (var item in list)
607604
{
608-
yield return new KeyValuePair<string, KVObject>(null!, list[i]);
605+
yield return new KeyValuePair<string, KVObject>(null!, item);
609606
}
610607
}
611608

@@ -628,8 +625,8 @@ private static bool TryFindInList(List<KeyValuePair<string, KVObject>> list, str
628625
{
629626
KVValueType.String => $"\"{_ref}\"",
630627
KVValueType.Null => "null",
631-
KVValueType.Collection => $"Collection ({GetCollectionCount()} items)",
632-
KVValueType.Array => $"Array ({GetArrayList().Count} items)",
628+
KVValueType.Collection => $"Collection ({Count} items)",
629+
KVValueType.Array => $"Array ({Count} items)",
633630
_ => $"{ToString(CultureInfo.InvariantCulture)} ({ValueType})",
634631
};
635632

ValveKeyValue/ValveKeyValue/KVObject_IConvertible.cs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -271,19 +271,24 @@ private T ConvertFromString<T>(IFormatProvider? provider) where T : IParsable<T>
271271
private string FormatBlob()
272272
{
273273
var bytes = ((byte[])_ref!).AsSpan();
274-
var builder = new StringBuilder(bytes.Length * 3);
274+
275+
if (bytes.Length == 0)
276+
{
277+
return string.Empty;
278+
}
279+
280+
var builder = new StringBuilder(bytes.Length * 3 - 1);
275281

276282
for (var i = 0; i < bytes.Length; i++)
277283
{
284+
if (i > 0)
285+
{
286+
builder.Append(' ');
287+
}
288+
278289
var b = bytes[i];
279290
builder.Append(HexStringHelper.HexToCharUpper(b >> 4));
280291
builder.Append(HexStringHelper.HexToCharUpper(b));
281-
builder.Append(' ');
282-
}
283-
284-
if (builder.Length > 1)
285-
{
286-
builder.Length -= 1;
287292
}
288293

289294
return builder.ToString();

ValveKeyValue/ValveKeyValue/ObjectCopier.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public static object MakeObject(
5252
}
5353
else if (keyValueObject.ValueType == KVValueType.Array)
5454
{
55-
var arrayValues = keyValueObject.GetArrayList().Select(c => (object)c).ToArray();
55+
var arrayValues = keyValueObject.Values.Select(c => (object)c).ToArray();
5656
if (ConstructTypedEnumerable(typeof(TObject), arrayValues, reflector, out var enumerable))
5757
{
5858
return (TObject)enumerable;

ValveKeyValue/ValveKeyValue/Serialization/KeyValues3/KV3TextSerializer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ void WriteFloatTrimmed(string formatted)
304304

305305
void WriteBinaryBlob(KVObject value)
306306
{
307-
var bytes = value.AsSpan();
307+
var bytes = value.AsBlob();
308308

309309
if (bytes.Length <= 32)
310310
{

0 commit comments

Comments
 (0)