wasSharp – Blame information for rev
?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; |
||
10 | |||
11 | namespace wasSharp |
||
12 | { |
||
7 | office | 13 | public static class KeyValue |
1 | zed | 14 | { |
15 | /////////////////////////////////////////////////////////////////////////// |
||
16 | // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // |
||
17 | /////////////////////////////////////////////////////////////////////////// |
||
18 | /// <summary> |
||
19 | /// Returns the value of a key from a key-value data string. |
||
20 | /// </summary> |
||
21 | /// <returns>true if the key was found in data</returns> |
||
7 | office | 22 | public static string Get(string key, string data) |
23 | { |
||
24 | return data.Split('&') |
||
1 | zed | 25 | .AsParallel() |
6 | eva | 26 | .Select(o => o.Split('=')) |
7 | office | 27 | .Where(o => o.Length.Equals(2) && Strings.StringEquals(o[0], key, StringComparison.Ordinal)) |
6 | eva | 28 | .Select(o => o[1]) |
7 | office | 29 | .FirstOrDefault(); |
30 | } |
||
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> |
||
7 | office | 42 | public static string Set(string key, string value, string data) |
43 | { |
||
44 | return string.Join("&", string.Join("&", data.Split('&') |
||
45 | .AsParallel() |
||
46 | .Select(o => o.Split('=')) |
||
47 | .Where(o => o.Length.Equals(2) && !Strings.StringEquals(o[0], key, StringComparison.Ordinal)) |
||
48 | .Select(o => string.Join("=", o[0], o[1]))), string.Join("=", key, value)); |
||
49 | } |
||
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> |
||
7 | office | 58 | public static string Delete(string key, string data) |
59 | { |
||
60 | return string.Join("&", data.Split('&') |
||
1 | zed | 61 | .AsParallel() |
6 | eva | 62 | .Select(o => o.Split('=')) |
7 | office | 63 | .Where(o => o.Length.Equals(2) && !Strings.StringEquals(o[0], key, StringComparison.Ordinal)) |
6 | eva | 64 | .Select(o => string.Join("=", o[0], o[1])) |
7 | office | 65 | .ToArray()); |
66 | } |
||
1 | zed | 67 | |
68 | /////////////////////////////////////////////////////////////////////////// |
||
69 | // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // |
||
70 | /////////////////////////////////////////////////////////////////////////// |
||
71 | /// <summary> |
||
72 | /// Decodes key-value pair data to a dictionary. |
||
73 | /// </summary> |
||
74 | /// <returns>a dictionary containing the keys and values</returns> |
||
7 | office | 75 | public static Dictionary<string, string> Decode(string data) |
76 | { |
||
77 | return data.Split('&') |
||
1 | zed | 78 | .AsParallel() |
6 | eva | 79 | .Select(o => o.Split('=')) |
80 | .Where(o => o.Length.Equals(2)) |
||
1 | zed | 81 | .Select(o => new |
82 | { |
||
6 | eva | 83 | k = o[0], |
84 | v = o[1] |
||
1 | zed | 85 | }) |
86 | .GroupBy(o => o.k) |
||
7 | office | 87 | .ToDictionary(o => o.Key, p => p.First().v); |
88 | } |
||
1 | zed | 89 | |
90 | /////////////////////////////////////////////////////////////////////////// |
||
91 | // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // |
||
92 | /////////////////////////////////////////////////////////////////////////// |
||
93 | /// <summary> |
||
94 | /// Serialises a dictionary to key-value data. |
||
95 | /// </summary> |
||
96 | /// <returns>a key-value data encoded string</returns> |
||
7 | office | 97 | public static string Encode(Dictionary<string, string> data) |
98 | { |
||
99 | return string.Join("&", data.AsParallel().Select(o => string.Join("=", o.Key, o.Value))); |
||
100 | } |
||
1 | zed | 101 | |
102 | /////////////////////////////////////////////////////////////////////////// |
||
103 | // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // |
||
104 | /////////////////////////////////////////////////////////////////////////// |
||
6 | eva | 105 | /// <summary> |
106 | /// Escapes a dictionary's keys and values for sending as POST data. |
||
107 | /// </summary> |
||
7 | office | 108 | public static Dictionary<string, string> Escape(Dictionary<string, string> data, Func<string, string> func) |
109 | { |
||
110 | return data.AsParallel().ToDictionary(o => func(o.Key), p => func(p.Value)); |
||
111 | } |
||
1 | zed | 112 | } |
113 | } |