Merge pull request #249 from pcc/xgetinputfocus
[mono.git] / mcs / class / System.Web.Mvc3 / Mvc / DictionaryHelpers.cs
1 namespace System.Web.Mvc {
2     using System;
3     using System.Collections.Generic;
4     using System.Linq;
5
6     internal static class DictionaryHelpers {
7
8         public static IEnumerable<KeyValuePair<string, TValue>> FindKeysWithPrefix<TValue>(IDictionary<string, TValue> dictionary, string prefix) {
9             TValue exactMatchValue;
10             if (dictionary.TryGetValue(prefix, out exactMatchValue)) {
11                 yield return new KeyValuePair<string, TValue>(prefix, exactMatchValue);
12             }
13
14             foreach (var entry in dictionary) {
15                 string key = entry.Key;
16
17                 if (key.Length <= prefix.Length) {
18                     continue;
19                 }
20
21                 if (!key.StartsWith(prefix, StringComparison.OrdinalIgnoreCase)) {
22                     continue;
23                 }
24
25                 char charAfterPrefix = key[prefix.Length];
26                 switch (charAfterPrefix) {
27                     case '[':
28                     case '.':
29                         yield return entry;
30                         break;
31                 }
32             }
33         }
34
35         public static bool DoesAnyKeyHavePrefix<TValue>(IDictionary<string, TValue> dictionary, string prefix) {
36             return FindKeysWithPrefix(dictionary, prefix).Any();
37         }
38
39     }
40 }