// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; using System.Collections.Generic; using System.Linq; using Microsoft.Framework.Internal; namespace Microsoft.AspNet.Mvc.ModelBinding.Internal { public static class DictionaryHelper { public static IEnumerable> FindKeysWithPrefix( [NotNull] IDictionary dictionary, [NotNull] string prefix) { TValue exactMatchValue; if (dictionary.TryGetValue(prefix, out exactMatchValue)) { yield return new KeyValuePair(prefix, exactMatchValue); } foreach (var entry in dictionary) { var key = entry.Key; if (key.Length <= prefix.Length) { continue; } if (key.StartsWith("[", StringComparison.OrdinalIgnoreCase)) { key = key.Substring(key.IndexOf('.') + 1); if (string.Equals(prefix, key, StringComparison.Ordinal)) { yield return entry; continue; } } if (!key.StartsWith(prefix, StringComparison.OrdinalIgnoreCase)) { continue; } // Everything is prefixed by the empty string if (prefix.Length == 0) { yield return entry; } else { var charAfterPrefix = key[prefix.Length]; switch (charAfterPrefix) { case '[': case '.': yield return entry; break; } } } } } }