corrade-vassal – Diff between revs 14 and 16
?pathlinks?
Rev 14 | Rev 16 | |||
---|---|---|---|---|
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 10... | Line 9... | |||
10 | using System.Linq.Expressions; |
9 | using System.Linq; |
|
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> |
|
- | 21 | /// <returns>true if the key was found in data</returns> |
||
22 | /// <returns>true if the key was found in data</returns> |
22 | public static string Get(string key, string data) |
|
23 | public static Func<string, string, string> Get = |
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]) |
||
Line 29... | Line 30... | |||
29 | .Select(o => o[1]) |
30 | .FirstOrDefault(); |
|
30 | .FirstOrDefault())).Compile(); |
31 | } |
|
31 | |
32 | |
|
32 | /////////////////////////////////////////////////////////////////////////// |
33 | /////////////////////////////////////////////////////////////////////////// |
|
Line 37... | Line 38... | |||
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 | } |
||
Line 50... | Line 52... | |||
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> |
|
- | 60 | public static string Delete(string key, string data) |
||
58 | public static Func<string, string, string> Delete = |
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])) |
|
- | 68 | .ToArray()); |
||
Line 65... | Line 69... | |||
65 | .ToArray()))).Compile(); |
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> |
|
- | 77 | /// <returns>a dictionary containing the keys and values</returns> |
||
73 | /// <returns>a dictionary containing the keys and values</returns> |
78 | public static Dictionary<string, string> Decode(string data) |
|
74 | public static Func<string, Dictionary<string, string>> Decode = |
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 | }) |
|
- | 89 | .GroupBy(o => o.k) |
||
Line 84... | Line 90... | |||
84 | .GroupBy(o => o.k) |
90 | .ToDictionary(o => o.Key, p => p.First().v); |
|
85 | .ToDictionary(o => o.Key, p => p.First().v))).Compile(); |
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> |
|
- | 100 | public static string Encode(Dictionary<string, string> data) |
||
Line 94... | Line 101... | |||
94 | public static Func<Dictionary<string, string>, string> Encode = |
101 | { |
|
95 | ((Expression<Func<Dictionary<string, string>, string>>) |
102 | return string.Join("&", data.AsParallel().Select(o => string.Join("=", o.Key, o.Value))); |
|
96 | (data => string.Join("&", data.AsParallel().Select(o => string.Join("=", o.Key, o.Value))))).Compile(); |
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. |
|
- | 110 | /// </summary> |
||
103 | /// </summary> |
111 | public static Dictionary<string, string> Escape(Dictionary<string, string> data, Func<string, string> func) |
|
104 | public static Func<Dictionary<string, string>, Func<string, string>, Dictionary<string, string>> Escape = |
112 | { |
|
105 | ((Expression<Func<Dictionary<string, string>, Func<string, string>, Dictionary<string, string>>>) |
113 | return data.AsParallel().ToDictionary(o => func(o.Key), p => func(p.Value)); |