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