corrade-vassal – Diff between revs 14 and 16

Subversion Repositories:
Rev:
Only display areas with differencesIgnore whitespace
Rev 14 Rev 16
1 /////////////////////////////////////////////////////////////////////////// 1 ///////////////////////////////////////////////////////////////////////////
2 // Copyright (C) Wizardry and Steamworks 2013 - License: GNU GPLv3 // 2 // Copyright (C) Wizardry and Steamworks 2013 - License: GNU GPLv3 //
3 // Please see: http://www.gnu.org/licenses/gpl.html for legal details, // 3 // Please see: http://www.gnu.org/licenses/gpl.html for legal details, //
4 // rights of fair usage, the disclaimer and warranty conditions. // 4 // rights of fair usage, the disclaimer and warranty conditions. //
5 /////////////////////////////////////////////////////////////////////////// 5 ///////////////////////////////////////////////////////////////////////////
6   6  
7 using System; 7 using System;
8 using System.Collections.Generic; 8 using System.Collections.Generic;
9 using System.Linq; 9 using System.Linq;
10 using System.Linq.Expressions; -  
11   10  
12 namespace wasSharp 11 namespace wasSharp
13 { 12 {
14 public class KeyValue 13 public static class KeyValue
15 { 14 {
16 /////////////////////////////////////////////////////////////////////////// 15 ///////////////////////////////////////////////////////////////////////////
17 // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // 16 // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 //
18 /////////////////////////////////////////////////////////////////////////// 17 ///////////////////////////////////////////////////////////////////////////
19 /// <summary> 18 /// <summary>
20 /// Returns the value of a key from a key-value data string. 19 /// Returns the value of a key from a key-value data string.
21 /// </summary> 20 /// </summary>
22 /// <returns>true if the key was found in data</returns> 21 /// <returns>true if the key was found in data</returns>
23 public static Func<string, string, string> Get = 22 public static string Get(string key, string data)
-   23 {
24 ((Expression<Func<string, string, string>>) ((key, data) => data.Split('&') 24 return data.Split('&')
25 .AsParallel() 25 .AsParallel()
26 .Select(o => o.Split('=')) 26 .Select(o => o.Split('='))
27 .Where(o => o.Length.Equals(2)) 27 .Where(o => o.Length.Equals(2))
28 .Where(o => o[0].Equals(key)) 28 .Where(o => o[0].Equals(key))
29 .Select(o => o[1]) 29 .Select(o => o[1])
30 .FirstOrDefault())).Compile(); 30 .FirstOrDefault();
-   31 }
31   32  
32 /////////////////////////////////////////////////////////////////////////// 33 ///////////////////////////////////////////////////////////////////////////
33 // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // 34 // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 //
34 /////////////////////////////////////////////////////////////////////////// 35 ///////////////////////////////////////////////////////////////////////////
35 /// <summary> 36 /// <summary>
36 /// Returns a key-value data string with a key set to a given value. 37 /// Returns a key-value data string with a key set to a given value.
37 /// </summary> 38 /// </summary>
38 /// <returns> 39 /// <returns>
39 /// a key-value data string or the empty string if either key or 40 /// a key-value data string or the empty string if either key or
40 /// value are empty 41 /// value are empty
41 /// </returns> 42 /// </returns>
42 public static Func<string, string, string, string> Set = 43 public static string Set(string key, string value, string data)
43 ((Expression<Func<string, string, string, string>>) 44 {
44 ((key, value, data) => string.Join("&", string.Join("&", data.Split('&') 45 return string.Join("&", string.Join("&", data.Split('&')
45 .AsParallel() 46 .AsParallel()
46 .Select(o => o.Split('=')) 47 .Select(o => o.Split('='))
47 .Where(o => o.Length.Equals(2)) 48 .Where(o => o.Length.Equals(2))
48 .Where(o => !o[0].Equals(key)) 49 .Where(o => !o[0].Equals(key))
49 .Select(o => string.Join("=", o[0], o[1]))), string.Join("=", key, value)))).Compile(); 50 .Select(o => string.Join("=", o[0], o[1]))), string.Join("=", key, value));
-   51 }
50   52  
51 /////////////////////////////////////////////////////////////////////////// 53 ///////////////////////////////////////////////////////////////////////////
52 // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // 54 // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 //
53 /////////////////////////////////////////////////////////////////////////// 55 ///////////////////////////////////////////////////////////////////////////
54 /// <summary> 56 /// <summary>
55 /// Deletes a key-value pair from a string referenced by a key. 57 /// Deletes a key-value pair from a string referenced by a key.
56 /// </summary> 58 /// </summary>
57 /// <returns>a key-value pair string</returns> 59 /// <returns>a key-value pair string</returns>
58 public static Func<string, string, string> Delete = 60 public static string Delete(string key, string data)
-   61 {
59 ((Expression<Func<string, string, string>>) ((key, data) => string.Join("&", data.Split('&') 62 return string.Join("&", data.Split('&')
60 .AsParallel() 63 .AsParallel()
61 .Select(o => o.Split('=')) 64 .Select(o => o.Split('='))
62 .Where(o => o.Length.Equals(2)) 65 .Where(o => o.Length.Equals(2))
63 .Where(o => !o[0].Equals(key)) 66 .Where(o => !o[0].Equals(key))
64 .Select(o => string.Join("=", o[0], o[1])) 67 .Select(o => string.Join("=", o[0], o[1]))
65 .ToArray()))).Compile(); 68 .ToArray());
-   69 }
66   70  
67 /////////////////////////////////////////////////////////////////////////// 71 ///////////////////////////////////////////////////////////////////////////
68 // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // 72 // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 //
69 /////////////////////////////////////////////////////////////////////////// 73 ///////////////////////////////////////////////////////////////////////////
70 /// <summary> 74 /// <summary>
71 /// Decodes key-value pair data to a dictionary. 75 /// Decodes key-value pair data to a dictionary.
72 /// </summary> 76 /// </summary>
73 /// <returns>a dictionary containing the keys and values</returns> 77 /// <returns>a dictionary containing the keys and values</returns>
74 public static Func<string, Dictionary<string, string>> Decode = 78 public static Dictionary<string, string> Decode(string data)
-   79 {
75 ((Expression<Func<string, Dictionary<string, string>>>) (data => data.Split('&') 80 return data.Split('&')
76 .AsParallel() 81 .AsParallel()
77 .Select(o => o.Split('=')) 82 .Select(o => o.Split('='))
78 .Where(o => o.Length.Equals(2)) 83 .Where(o => o.Length.Equals(2))
79 .Select(o => new 84 .Select(o => new
80 { 85 {
81 k = o[0], 86 k = o[0],
82 v = o[1] 87 v = o[1]
83 }) 88 })
84 .GroupBy(o => o.k) 89 .GroupBy(o => o.k)
85 .ToDictionary(o => o.Key, p => p.First().v))).Compile(); 90 .ToDictionary(o => o.Key, p => p.First().v);
-   91 }
86   92  
87 /////////////////////////////////////////////////////////////////////////// 93 ///////////////////////////////////////////////////////////////////////////
88 // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // 94 // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 //
89 /////////////////////////////////////////////////////////////////////////// 95 ///////////////////////////////////////////////////////////////////////////
90 /// <summary> 96 /// <summary>
91 /// Serialises a dictionary to key-value data. 97 /// Serialises a dictionary to key-value data.
92 /// </summary> 98 /// </summary>
93 /// <returns>a key-value data encoded string</returns> 99 /// <returns>a key-value data encoded string</returns>
94 public static Func<Dictionary<string, string>, string> Encode = 100 public static string Encode(Dictionary<string, string> data)
95 ((Expression<Func<Dictionary<string, string>, string>>) 101 {
96 (data => string.Join("&", data.AsParallel().Select(o => string.Join("=", o.Key, o.Value))))).Compile(); 102 return string.Join("&", data.AsParallel().Select(o => string.Join("=", o.Key, o.Value)));
-   103 }
97   104  
98 /////////////////////////////////////////////////////////////////////////// 105 ///////////////////////////////////////////////////////////////////////////
99 // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // 106 // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 //
100 /////////////////////////////////////////////////////////////////////////// 107 ///////////////////////////////////////////////////////////////////////////
101 /// <summary> 108 /// <summary>
102 /// Escapes a dictionary's keys and values for sending as POST data. 109 /// Escapes a dictionary's keys and values for sending as POST data.
103 /// </summary> 110 /// </summary>
104 public static Func<Dictionary<string, string>, Func<string, string>, Dictionary<string, string>> Escape = 111 public static Dictionary<string, string> Escape(Dictionary<string, string> data, Func<string, string> func)
105 ((Expression<Func<Dictionary<string, string>, Func<string, string>, Dictionary<string, string>>>) -  
-   112 {
106 ((data, func) => data.AsParallel().ToDictionary(o => func(o.Key), p => func(p.Value)))).Compile(); 113 return data.AsParallel().ToDictionary(o => func(o.Key), p => func(p.Value));
-   114 }
107 } 115 }
108 } 116 }
109   117  
110
Generated by GNU Enscript 1.6.5.90.
118
Generated by GNU Enscript 1.6.5.90.
111   119  
112   120  
113   121