wasSharp – Blame information for rev 59
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
58 | office | 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 | using System.Threading.Tasks; |
||
11 | |||
12 | namespace wasSharp.Languages |
||
13 | { |
||
14 | public static class KeyValue |
||
15 | { |
||
16 | /////////////////////////////////////////////////////////////////////////// |
||
17 | // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // |
||
1 | zed | 18 | /////////////////////////////////////////////////////////////////////////// |
19 | /// <summary> |
||
58 | office | 20 | /// Returns the value of a key from a key-value data string. |
1 | zed | 21 | /// </summary> |
22 | /// <returns>true if the key was found in data</returns> |
||
58 | office | 23 | public static string Get(string key, string data) |
24 | { |
||
25 | return data.Split('&') |
||
26 | .AsParallel() |
||
27 | .Select(o => o.Split('=')) |
||
59 | office | 28 | .Where(o => o.Length.Equals(2) && o[0] == key) |
58 | office | 29 | .Select(o => o[1]) |
30 | .FirstOrDefault(); |
||
31 | } |
||
32 | |||
33 | /////////////////////////////////////////////////////////////////////////// |
||
34 | // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // |
||
1 | zed | 35 | /////////////////////////////////////////////////////////////////////////// |
36 | /// <summary> |
||
58 | office | 37 | /// Returns a key-value data string with a key set to a given value. |
1 | zed | 38 | /// </summary> |
39 | /// <returns> |
||
58 | office | 40 | /// a key-value data string or the empty string if either key or |
41 | /// value are empty |
||
1 | zed | 42 | /// </returns> |
58 | office | 43 | public static string Set(string key, string value, string data) |
44 | { |
||
45 | return string.Join("&", string.Join("&", data.Split('&') |
||
46 | .AsParallel() |
||
47 | .Select(o => o.Split('=')) |
||
59 | office | 48 | .Where(o => o.Length.Equals(2) && o[0] == key) |
49 | .Select(o => o[0] + "=" + o[1])), key + "=" + value); |
||
58 | office | 50 | } |
51 | |||
52 | /////////////////////////////////////////////////////////////////////////// |
||
53 | // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // |
||
1 | zed | 54 | /////////////////////////////////////////////////////////////////////////// |
55 | /// <summary> |
||
58 | office | 56 | /// Deletes a key-value pair from a string referenced by a key. |
1 | zed | 57 | /// </summary> |
58 | /// <returns>a key-value pair string</returns> |
||
58 | office | 59 | public static string Delete(string key, string data) |
60 | { |
||
61 | return string.Join("&", data.Split('&') |
||
62 | .AsParallel() |
||
63 | .Select(o => o.Split('=')) |
||
59 | office | 64 | .Where(o => o.Length.Equals(2) && o[0] == key) |
65 | .Select(o => o[0] + "=" + o[1])); |
||
58 | office | 66 | } |
67 | |||
68 | /////////////////////////////////////////////////////////////////////////// |
||
69 | // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // |
||
1 | zed | 70 | /////////////////////////////////////////////////////////////////////////// |
71 | /// <summary> |
||
58 | office | 72 | /// Decodes key-value pair data to a dictionary. |
1 | zed | 73 | /// </summary> |
74 | /// <returns>a dictionary containing the keys and values</returns> |
||
58 | office | 75 | public static Dictionary<string, string> Decode(string data) |
76 | { |
||
77 | return data.Split('&') |
||
78 | .AsParallel() |
||
79 | .Select(o => o.Split('=')) |
||
80 | .Where(o => o.Length.Equals(2)) |
||
81 | .Select(o => new |
||
82 | { |
||
83 | k = o[0], |
||
84 | v = o[1] |
||
85 | }) |
||
86 | .GroupBy(o => o.k) |
||
87 | .ToDictionary(o => o.Key, p => p.FirstOrDefault()?.v); |
||
88 | } |
||
89 | |||
90 | /////////////////////////////////////////////////////////////////////////// |
||
91 | // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // |
||
1 | zed | 92 | /////////////////////////////////////////////////////////////////////////// |
93 | /// <summary> |
||
58 | office | 94 | /// Serialises a dictionary to key-value data. |
1 | zed | 95 | /// </summary> |
96 | /// <returns>a key-value data encoded string</returns> |
||
58 | office | 97 | public static string Encode(Dictionary<string, string> data) |
98 | { |
||
59 | office | 99 | return string.Join("&", data.Select(o => o.Key + "=" + o.Value)); |
58 | office | 100 | } |
101 | |||
102 | /////////////////////////////////////////////////////////////////////////// |
||
103 | // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // |
||
1 | zed | 104 | /////////////////////////////////////////////////////////////////////////// |
6 | eva | 105 | /// <summary> |
58 | office | 106 | /// Serializes a dictionary to key-value data. |
24 | office | 107 | /// </summary> |
56 | office | 108 | /// <param name="data">an input string</param> |
24 | office | 109 | /// <returns>a key-value data encoded string</returns> |
58 | office | 110 | public static string Encode(IEnumerable<KeyValuePair<string, string>> data) |
111 | { |
||
59 | office | 112 | return string.Join("&", data.Select(o => o.Key + "=" + o.Value)); |
58 | office | 113 | } |
114 | |||
115 | /////////////////////////////////////////////////////////////////////////// |
||
116 | // Copyright (C) 2018 Wizardry and Steamworks - License: GNU GPLv3 // |
||
24 | office | 117 | /////////////////////////////////////////////////////////////////////////// |
56 | office | 118 | /// <summary> |
58 | office | 119 | /// Asynchronously serializes a dictionary to key-value data. |
56 | office | 120 | /// </summary> |
121 | /// <param name="data">an input string</param> |
||
122 | /// <returns>a key-value data encoded string</returns> |
||
58 | office | 123 | public static string Encode(IEnumerable<KeyValuePair<Task<string>, Task<string>>> data) |
124 | { |
||
59 | office | 125 | return string.Join("&", data.Select(o => o.Key.Result + "=" + o.Value.Result)); |
58 | office | 126 | } |
127 | |||
128 | /////////////////////////////////////////////////////////////////////////// |
||
129 | // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // |
||
56 | office | 130 | /////////////////////////////////////////////////////////////////////////// |
24 | office | 131 | /// <summary> |
58 | office | 132 | /// Escapes a dictionary's keys and values for sending as POST data. |
6 | eva | 133 | /// </summary> |
58 | office | 134 | public static IEnumerable<KeyValuePair<string, string>> Escape(IEnumerable<KeyValuePair<string, string>> data, |
135 | Func<string, string> func) |
||
136 | { |
||
137 | return data.ToDictionary(o => func(o.Key), p => func(p.Value)); |
||
138 | } |
||
139 | |||
140 | /////////////////////////////////////////////////////////////////////////// |
||
141 | // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // |
||
56 | office | 142 | /////////////////////////////////////////////////////////////////////////// |
143 | /// <summary> |
||
58 | office | 144 | /// Escapes a dictionary's keys and values for sending as POST data. |
56 | office | 145 | /// </summary> |
58 | office | 146 | public static IEnumerable<KeyValuePair<Task<string>, Task<string>>> EscapeAsync( |
147 | IEnumerable<KeyValuePair<string, string>> data, |
||
148 | Func<string, Task<string>> func) |
||
149 | { |
||
150 | return data.ToDictionary(async o => await func(o.Key), async p => await func(p.Value)); |
||
151 | } |
||
152 | } |
||
153 | } |