Gameloop.Vdf icon indicating copy to clipboard operation
Gameloop.Vdf copied to clipboard

Case insensitive option for ContainsKey and getter?

Open a6874453 opened this issue 3 years ago • 1 comments

Hello, i would like to ask if i can retrieve a value by a key name ignoring the case?

In source engine KeyValues class, this code:

KeyValues *pKV = new KeyValues("Test");
pKV->SetInt("Apple", 5);
Msg("%d %d", pKV->GetInt("apple"), pKV->GetInt("APPLE"));

it prints "5 5", which means the cases are ignored

a6874453 avatar Jul 08 '21 13:07 a6874453

Json.NET appears to have a GetValue method that this library doesn't yet support. I'll tag this issue as a feature request.

In the meantime, you should be able to filter children manually

// returns 5
pKV.Children<VProperty>().First(x => x.Key.Equals("apple", StringComparison.InvariantCultureIgnoreCase)).Value

You could also write an extension method that does this:

public static class VdfExtensions
{
    public static VToken GetValueIgnoreCase(this VObject obj, string key)
    {
        return obj.Children<VProperty>().First(x => x.Key.Equals(key, StringComparison.InvariantCultureIgnoreCase)).Value;
    }
}

// returns 5
pKV.GetValueIgnoreCase("apple")

shravan2x avatar Jul 08 '21 14:07 shravan2x