corrade-vassal – Diff between revs 13 and 14

Subversion Repositories:
Rev:
Show entire fileIgnore whitespace
Rev 13 Rev 14
Line 5... Line 5...
5 /////////////////////////////////////////////////////////////////////////// 5 ///////////////////////////////////////////////////////////////////////////
Line 6... Line 6...
6   6  
7 using System; 7 using System;
8 using System.Collections.Generic; 8 using System.Collections.Generic;
-   9 using System.Linq;
Line 9... Line 10...
9 using System.Linq; 10 using System.Linq.Expressions;
10   11  
11 namespace wasSharp 12 namespace wasSharp
12 { 13 {
Line 16... Line 17...
16 // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // 17 // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 //
17 /////////////////////////////////////////////////////////////////////////// 18 ///////////////////////////////////////////////////////////////////////////
18 /// <summary> 19 /// <summary>
19 /// Returns the value of a key from a key-value data string. 20 /// Returns the value of a key from a key-value data string.
20 /// </summary> 21 /// </summary>
21 /// <param name="key">the key of the value</param> -  
22 /// <param name="data">the key-value data segment</param> -  
23 /// <returns>true if the key was found in data</returns> 22 /// <returns>true if the key was found in data</returns>
24 public static string Get(string key, string data) 23 public static Func<string, string, string> Get =
25 { -  
26 return data.Split('&') 24 ((Expression<Func<string, string, string>>) ((key, data) => data.Split('&')
27 .AsParallel() 25 .AsParallel()
28 .Select(o => o.Split('=').ToList()) 26 .Select(o => o.Split('='))
29 .Where(o => o.Count.Equals(2)) 27 .Where(o => o.Length.Equals(2))
30 .Select(o => new -  
31 { -  
32 k = o.First(), -  
33 v = o.Last() -  
34 }) -  
35 .Where(o => o.k.Equals(key)) 28 .Where(o => o[0].Equals(key))
36 .Select(o => o.v) 29 .Select(o => o[1])
37 .FirstOrDefault(); 30 .FirstOrDefault())).Compile();
38 } -  
Line 39... Line 31...
39   31  
40 /////////////////////////////////////////////////////////////////////////// 32 ///////////////////////////////////////////////////////////////////////////
41 // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // 33 // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 //
42 /////////////////////////////////////////////////////////////////////////// 34 ///////////////////////////////////////////////////////////////////////////
43 /// <summary> 35 /// <summary>
44 /// Returns a key-value data string with a key set to a given value. 36 /// Returns a key-value data string with a key set to a given value.
45 /// </summary> -  
46 /// <param name="key">the key of the value</param> -  
47 /// <param name="value">the value to set the key to</param> -  
48 /// <param name="data">the key-value data segment</param> 37 /// </summary>
49 /// <returns> 38 /// <returns>
50 /// a key-value data string or the empty string if either key or 39 /// a key-value data string or the empty string if either key or
51 /// value are empty 40 /// value are empty
52 /// </returns> 41 /// </returns>
53 public static string Set(string key, string value, string data) 42 public static Func<string, string, string, string> Set =
54 { 43 ((Expression<Func<string, string, string, string>>)
55 HashSet<string> output = new HashSet<string>(data.Split('&') 44 ((key, value, data) => string.Join("&", string.Join("&", data.Split('&')
56 .AsParallel() 45 .AsParallel()
57 .Select(o => o.Split('=').ToList()) 46 .Select(o => o.Split('='))
58 .Where(o => o.Count.Equals(2)) -  
59 .Select(o => new -  
60 { 47 .Where(o => o.Length.Equals(2))
61 k = o.First(), -  
62 v = !o.First().Equals(key) ? o.Last() : value 48 .Where(o => !o[0].Equals(key))
63 }).Select(o => string.Join("=", o.k, o.v))); -  
64 string append = string.Join("=", key, value); -  
65 if (!output.Contains(append)) -  
66 { -  
67 output.Add(append); -  
68 } -  
69 return string.Join("&", output.ToArray()); -  
Line 70... Line 49...
70 } 49 .Select(o => string.Join("=", o[0], o[1]))), string.Join("=", key, value)))).Compile();
71   50  
72 /////////////////////////////////////////////////////////////////////////// 51 ///////////////////////////////////////////////////////////////////////////
73 // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // 52 // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 //
74 /////////////////////////////////////////////////////////////////////////// 53 ///////////////////////////////////////////////////////////////////////////
75 /// <summary> 54 /// <summary>
76 /// Deletes a key-value pair from a string referenced by a key. -  
77 /// </summary> -  
78 /// <param name="key">the key to search for</param> 55 /// Deletes a key-value pair from a string referenced by a key.
79 /// <param name="data">the key-value data segment</param> 56 /// </summary>
80 /// <returns>a key-value pair string</returns> -  
81 public static string Delete(string key, string data) 57 /// <returns>a key-value pair string</returns>
82 { 58 public static Func<string, string, string> Delete =
83 return string.Join("&", data.Split('&') 59 ((Expression<Func<string, string, string>>) ((key, data) => string.Join("&", data.Split('&')
84 .AsParallel() 60 .AsParallel()
85 .Select(o => o.Split('=').ToList()) -  
86 .Where(o => o.Count.Equals(2)) -  
87 .Select(o => new -  
88 { -  
89 k = o.First(), -  
90 v = o.Last() 61 .Select(o => o.Split('='))
91 }) 62 .Where(o => o.Length.Equals(2))
92 .Where(o => !o.k.Equals(key)) 63 .Where(o => !o[0].Equals(key))
93 .Select(o => string.Join("=", o.k, o.v)) -  
Line 94... Line 64...
94 .ToArray()); 64 .Select(o => string.Join("=", o[0], o[1]))
95 } 65 .ToArray()))).Compile();
96   66  
97 /////////////////////////////////////////////////////////////////////////// 67 ///////////////////////////////////////////////////////////////////////////
98 // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // 68 // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 //
99 /////////////////////////////////////////////////////////////////////////// 69 ///////////////////////////////////////////////////////////////////////////
100 /// <summary> -  
101 /// Decodes key-value pair data to a dictionary. 70 /// <summary>
102 /// </summary> 71 /// Decodes key-value pair data to a dictionary.
103 /// <param name="data">the key-value pair data</param> -  
104 /// <returns>a dictionary containing the keys and values</returns> 72 /// </summary>
105 public static Dictionary<string, string> Decode(string data) 73 /// <returns>a dictionary containing the keys and values</returns>
106 { 74 public static Func<string, Dictionary<string, string>> Decode =
107 return data.Split('&') 75 ((Expression<Func<string, Dictionary<string, string>>>) (data => data.Split('&')
108 .AsParallel() 76 .AsParallel()
109 .Select(o => o.Split('=').ToList()) 77 .Select(o => o.Split('='))
110 .Where(o => o.Count.Equals(2)) 78 .Where(o => o.Length.Equals(2))
111 .Select(o => new 79 .Select(o => new
112 { 80 {
113 k = o.First(), 81 k = o[0],
114 v = o.Last() 82 v = o[1]
115 }) -  
Line 116... Line 83...
116 .GroupBy(o => o.k) 83 })
117 .ToDictionary(o => o.Key, p => p.First().v); 84 .GroupBy(o => o.k)
118 } 85 .ToDictionary(o => o.Key, p => p.First().v))).Compile();
119   86  
120 /////////////////////////////////////////////////////////////////////////// 87 ///////////////////////////////////////////////////////////////////////////
121 // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // 88 // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 //
122 /////////////////////////////////////////////////////////////////////////// -  
123 /// <summary> 89 ///////////////////////////////////////////////////////////////////////////
124 /// Serialises a dictionary to key-value data. 90 /// <summary>
125 /// </summary> 91 /// Serialises a dictionary to key-value data.
126 /// <param name="data">a dictionary</param> 92 /// </summary>
127 /// <returns>a key-value data encoded string</returns> -  
Line 128... Line 93...
128 public static string Encode(Dictionary<string, string> data) 93 /// <returns>a key-value data encoded string</returns>
129 { 94 public static Func<Dictionary<string, string>, string> Encode =
130 return string.Join("&", data.AsParallel().Select(o => string.Join("=", o.Key, o.Value))); 95 ((Expression<Func<Dictionary<string, string>, string>>)
-   96 (data => string.Join("&", data.AsParallel().Select(o => string.Join("=", o.Key, o.Value))))).Compile();
131 } 97  
132   98 ///////////////////////////////////////////////////////////////////////////
133 /////////////////////////////////////////////////////////////////////////// 99 // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 //
134 // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // 100 ///////////////////////////////////////////////////////////////////////////
135 /////////////////////////////////////////////////////////////////////////// -  
136 /// <summary>Escapes a dictionary's keys and values for sending as POST data.</summary> -  
137 /// <param name="data">A dictionary containing keys and values to be escaped</param> 101 /// <summary>
138 /// <param name="func">The function to use to escape the keys and values in the dictionary.</param> -  
139 public static Dictionary<string, string> Escape(Dictionary<string, string> data, 102 /// Escapes a dictionary's keys and values for sending as POST data.
140 Func<string, string> func) 103 /// </summary>
141 { 104 public static Func<Dictionary<string, string>, Func<string, string>, Dictionary<string, string>> Escape =