diff --git a/README.md b/README.md index 30418f77..6378b7ea 100644 --- a/README.md +++ b/README.md @@ -278,6 +278,7 @@ Every `Deserialize` and `Serialize` overload takes an optional `KVSerializerOpti * `FileLoader` - Provider for files referenced by KV1 `#include` and `#base` directives. See [Includes](#includes). * `StringTable` - String table used by the KV1 binary format. See [Binary](#binary). * `SkipHeader` - Whether to omit the KV3 header comment when serializing and to not expect one when deserializing. +* `KeyComparer` - Comparer that keys of deserialized KV3 objects are looked up with, for example `StringComparer.OrdinalIgnoreCase`. KV1 objects keep their keys in a list and always compare them exactly. ```csharp var options = new KVSerializerOptions diff --git a/ValveKeyValue/ValveKeyValue.Test/KVObjectApiTestCase.cs b/ValveKeyValue/ValveKeyValue.Test/KVObjectApiTestCase.cs index 585e0a66..2a5265b1 100644 --- a/ValveKeyValue/ValveKeyValue.Test/KVObjectApiTestCase.cs +++ b/ValveKeyValue/ValveKeyValue.Test/KVObjectApiTestCase.cs @@ -325,6 +325,21 @@ public void DefaultConstructorCreatesEmptyCollection() } } + [Test] + public void ConstructorWithKeyComparerLooksUpKeysWithIt() + { + var obj = new KVObject(StringComparer.OrdinalIgnoreCase); + obj.Add("StartDisabled", true); + + using (Assert.EnterMultipleScope()) + { + Assert.That(obj.ContainsKey("startdisabled"), Is.True); + Assert.That((bool)obj["STARTDISABLED"], Is.True); + Assert.That(obj.TryAdd("startdisabled", false), Is.False); + Assert.That(obj.Keys.Single(), Is.EqualTo("StartDisabled")); + } + } + #endregion #region KVObject null @@ -702,6 +717,26 @@ public void KV3DeserializationContainsKeyWorks() } } + [Test] + public void KV3DeserializationLooksUpKeysWithKeyComparer() + { + var options = new KVSerializerOptions + { + SkipHeader = true, + KeyComparer = StringComparer.OrdinalIgnoreCase, + }; + + using var stream = new MemoryStream("{ Outer = { StartDisabled = true } }"u8.ToArray()); + var data = KVSerializer.Create(KVSerializationFormat.KeyValues3Text).Deserialize(stream, options).Root; + + using (Assert.EnterMultipleScope()) + { + Assert.That(data.ContainsKey("outer"), Is.True); + Assert.That((bool)data["OUTER"]["startdisabled"], Is.True); + Assert.That(data["outer"].Keys.Single(), Is.EqualTo("StartDisabled")); + } + } + [Test] public void KV3DeserializationChildrenIterable() { diff --git a/ValveKeyValue/ValveKeyValue.Test/Test Data/apisurface.txt b/ValveKeyValue/ValveKeyValue.Test/Test Data/apisurface.txt index 1003b497..dec71614 100644 --- a/ValveKeyValue/ValveKeyValue.Test/Test Data/apisurface.txt +++ b/ValveKeyValue/ValveKeyValue.Test/Test Data/apisurface.txt @@ -124,6 +124,7 @@ public class ValveKeyValue.KVObject : IConvertible, System.Collections.Generic.I public .ctor(long value); public .ctor(short value); public .ctor(string value); + public .ctor(System.Collections.Generic.IEqualityComparer`1[[string]] keyComparer); public .ctor(uint value); public .ctor(ulong value); public .ctor(ushort value); @@ -307,6 +308,7 @@ public sealed class ValveKeyValue.KVSerializerOptions public bool get_EnableValveNullByteBugBehavior(); public ValveKeyValue.IIncludedFileLoader get_FileLoader(); public bool get_HasEscapeSequences(); + public System.Collections.Generic.IEqualityComparer`1[[string]] get_KeyComparer(); public bool get_SkipHeader(); public ValveKeyValue.StringTable get_StringTable(); public int GetHashCode(); @@ -315,6 +317,7 @@ public sealed class ValveKeyValue.KVSerializerOptions public void set_EnableValveNullByteBugBehavior(bool value); public void set_FileLoader(ValveKeyValue.IIncludedFileLoader value); public void set_HasEscapeSequences(bool value); + public void set_KeyComparer(System.Collections.Generic.IEqualityComparer`1[[string]] value); public void set_SkipHeader(bool value); public void set_StringTable(ValveKeyValue.StringTable value); public string ToString(); diff --git a/ValveKeyValue/ValveKeyValue/Deserialization/KVObjectBuilder.cs b/ValveKeyValue/ValveKeyValue/Deserialization/KVObjectBuilder.cs index e241f402..e2086112 100644 --- a/ValveKeyValue/ValveKeyValue/Deserialization/KVObjectBuilder.cs +++ b/ValveKeyValue/ValveKeyValue/Deserialization/KVObjectBuilder.cs @@ -5,11 +5,13 @@ namespace ValveKeyValue.Deserialization class KVObjectBuilder : IParsingVisitationListener { readonly bool useDictionary; + readonly IEqualityComparer? keyComparer; readonly List associatedBuilders = new(); - public KVObjectBuilder(bool useDictionaryForCollections = false) + public KVObjectBuilder(bool useDictionaryForCollections = false, IEqualityComparer? keyComparer = null) { useDictionary = useDictionaryForCollections; + this.keyComparer = keyComparer; } public KeyValuePair GetObject() @@ -200,7 +202,7 @@ static KeyValuePair MakeResult(KVPartialState state, KVObject? } KVObject result = useDictionary - ? KVObject.Collection(state.Items) + ? KVObject.Collection(state.Items, keyComparer) : new KVObject(KVValueType.Collection, state.DetachItems()); if (state.Flag != KVFlag.None) diff --git a/ValveKeyValue/ValveKeyValue/KVObject.cs b/ValveKeyValue/ValveKeyValue/KVObject.cs index a3597f6e..02766677 100644 --- a/ValveKeyValue/ValveKeyValue/KVObject.cs +++ b/ValveKeyValue/ValveKeyValue/KVObject.cs @@ -83,6 +83,18 @@ public KVObject() _ref = new Dictionary(); } + /// + /// Initializes a new instance of the class as an empty dictionary-backed collection + /// whose keys are compared with the given comparer. + /// + /// The comparer keys are looked up with. + public KVObject(IEqualityComparer keyComparer) + { + ArgumentNullException.ThrowIfNull(keyComparer); + ValueType = KVValueType.Collection; + _ref = new Dictionary(keyComparer); + } + /// /// Initializes a new instance of the class with a string value. /// @@ -463,9 +475,9 @@ public static KVObject Collection(IEnumerable> ch } // Enumerates the list directly rather than through IEnumerable, which would box its enumerator. - internal static KVObject Collection(List> children) + internal static KVObject Collection(List> children, IEqualityComparer? keyComparer = null) { - var dict = new Dictionary(children.Count); + var dict = new Dictionary(children.Count, keyComparer); foreach (var (key, value) in children) { dict[key] = value; diff --git a/ValveKeyValue/ValveKeyValue/KVSerializer.cs b/ValveKeyValue/ValveKeyValue/KVSerializer.cs index 4dcc695b..6d201de0 100644 --- a/ValveKeyValue/ValveKeyValue/KVSerializer.cs +++ b/ValveKeyValue/ValveKeyValue/KVSerializer.cs @@ -39,9 +39,10 @@ public KVDocument Deserialize(Stream stream, KVSerializerOptions? options = null { ArgumentNullException.ThrowIfNull(stream); - var builder = new KVObjectBuilder(useDictionaryForCollections: format == KVSerializationFormat.KeyValues3Text); + options ??= KVSerializerOptions.DefaultOptions; + var builder = new KVObjectBuilder(useDictionaryForCollections: format == KVSerializationFormat.KeyValues3Text, options.KeyComparer); - using var reader = MakeReader(stream, builder, options ?? KVSerializerOptions.DefaultOptions); + using var reader = MakeReader(stream, builder, options); var header = reader.ReadHeader(); var result = builder.GetObject(); @@ -138,7 +139,7 @@ public void Serialize(Stream stream, KVDocument data, KVSerializerOptions? optio var resolvedOptions = options ?? KVSerializerOptions.DefaultOptions; var spans = new List(); - var builder = new KVObjectBuilder(useDictionaryForCollections: format == KVSerializationFormat.KeyValues3Text); + var builder = new KVObjectBuilder(useDictionaryForCollections: format == KVSerializationFormat.KeyValues3Text, resolvedOptions.KeyComparer); using var stringReader = new StringReader(text); using var reader = MakeSourceMapReader(stringReader, builder, resolvedOptions, spans); diff --git a/ValveKeyValue/ValveKeyValue/KVSerializerOptions.cs b/ValveKeyValue/ValveKeyValue/KVSerializerOptions.cs index 3062b715..43711fc7 100644 --- a/ValveKeyValue/ValveKeyValue/KVSerializerOptions.cs +++ b/ValveKeyValue/ValveKeyValue/KVSerializerOptions.cs @@ -37,6 +37,12 @@ public sealed class KVSerializerOptions /// public StringTable? StringTable { get; set; } + /// + /// Gets or sets the comparer that keys of deserialized dictionary-backed collections are looked up with. + /// KV3 text is the format that deserializes into dictionary-backed collections. + /// + public IEqualityComparer? KeyComparer { get; set; } + /// /// Gets the default options (used when none are specified). ///