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