Implement MachineKey.Protect and MachineKey.Unprotect
[mono.git] / mcs / class / System.Web.Mvc2 / System.Web.Mvc / ValueProviderUtil.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 ValueProviderUtil {\r
19 \r
20         // Given "foo.bar[baz].quux", this method will return:\r
21         // - "foo.bar[baz].quux"\r
22         // - "foo.bar[baz]"\r
23         // - "foo.bar"\r
24         // - "foo"\r
25         public static IEnumerable<string> GetPrefixes(string key) {\r
26             yield return key;\r
27             for (int i = key.Length - 1; i >= 0; i--) {\r
28                 switch (key[i]) {\r
29                     case '.':\r
30                     case '[':\r
31                         yield return key.Substring(0, i);\r
32                         break;\r
33                 }\r
34             }\r
35         }\r
36 \r
37         public static bool CollectionContainsPrefix(IEnumerable<string> collection, string prefix) {\r
38             foreach (string key in collection) {\r
39                 if (key != null) {\r
40                     if (prefix.Length == 0) {\r
41                         return true; // shortcut - non-null key matches empty prefix\r
42                     }\r
43 \r
44                     if (key.StartsWith(prefix, StringComparison.OrdinalIgnoreCase)) {\r
45                         if (key.Length == prefix.Length) {\r
46                             return true; // exact match\r
47                         }\r
48                         else {\r
49                             switch (key[prefix.Length]) {\r
50                                 case '.': // known separator characters\r
51                                 case '[':\r
52                                     return true;\r
53                             }\r
54                         }\r
55                     }\r
56                 }\r
57             }\r
58 \r
59             return false; // nothing found\r
60         }\r
61 \r
62     }\r
63 }\r