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