New tests.
[mono.git] / mcs / class / System.Web.Mvc2 / System.Web.Mvc / DictionaryHelpers.cs
1 /* ****************************************************************************\r
2  *\r
3  * Copyright (c) Microsoft Corporation. All rights reserved.\r
4  *\r
5  * This software is subject to the Microsoft Public License (Ms-PL). \r
6  * A copy of the license can be found in the license.htm file included \r
7  * in this distribution.\r
8  *\r
9  * You must not remove this notice, or any other, from this software.\r
10  *\r
11  * ***************************************************************************/\r
12 \r
13 namespace System.Web.Mvc {\r
14     using System;\r
15     using System.Collections.Generic;\r
16     using System.Linq;\r
17 \r
18     internal static class DictionaryHelpers {\r
19 \r
20         public static IEnumerable<KeyValuePair<string, TValue>> FindKeysWithPrefix<TValue>(IDictionary<string, TValue> dictionary, string prefix) {\r
21             TValue exactMatchValue;\r
22             if (dictionary.TryGetValue(prefix, out exactMatchValue)) {\r
23                 yield return new KeyValuePair<string, TValue>(prefix, exactMatchValue);\r
24             }\r
25 \r
26             foreach (var entry in dictionary) {\r
27                 string key = entry.Key;\r
28 \r
29                 if (key.Length <= prefix.Length) {\r
30                     continue;\r
31                 }\r
32 \r
33                 if (!key.StartsWith(prefix, StringComparison.OrdinalIgnoreCase)) {\r
34                     continue;\r
35                 }\r
36 \r
37                 char charAfterPrefix = key[prefix.Length];\r
38                 switch (charAfterPrefix) {\r
39                     case '[':\r
40                     case '.':\r
41                         yield return entry;\r
42                         break;\r
43                 }\r
44             }\r
45         }\r
46 \r
47         public static bool DoesAnyKeyHavePrefix<TValue>(IDictionary<string, TValue> dictionary, string prefix) {\r
48             return FindKeysWithPrefix(dictionary, prefix).Any();\r
49         }\r
50 \r
51     }\r
52 }\r