wasSharp – Diff between revs 7 and 23
?pathlinks?
Rev 7 | Rev 23 | |||
---|---|---|---|---|
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.Linq; |
9 | using System.Linq; |
|
10 | |
10 | |
|
11 | namespace wasSharp |
11 | namespace wasSharp |
|
12 | { |
12 | { |
|
13 | public static class KeyValue |
13 | public static class KeyValue |
|
14 | { |
14 | { |
|
15 | /////////////////////////////////////////////////////////////////////////// |
15 | /////////////////////////////////////////////////////////////////////////// |
|
16 | // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // |
16 | // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // |
|
17 | /////////////////////////////////////////////////////////////////////////// |
17 | /////////////////////////////////////////////////////////////////////////// |
|
18 | /// <summary> |
18 | /// <summary> |
|
19 | /// Returns the value of a key from a key-value data string. |
19 | /// Returns the value of a key from a key-value data string. |
|
20 | /// </summary> |
20 | /// </summary> |
|
21 | /// <returns>true if the key was found in data</returns> |
21 | /// <returns>true if the key was found in data</returns> |
|
22 | public static string Get(string key, string data) |
22 | public static string Get(string key, string data) |
|
23 | { |
23 | { |
|
24 | return data.Split('&') |
24 | return data.Split('&') |
|
25 | .AsParallel() |
25 | .AsParallel() |
|
26 | .Select(o => o.Split('=')) |
26 | .Select(o => o.Split('=')) |
|
27 | .Where(o => o.Length.Equals(2) && Strings.StringEquals(o[0], key, StringComparison.Ordinal)) |
27 | .Where(o => o.Length.Equals(2) && Strings.StringEquals(o[0], key, StringComparison.Ordinal)) |
|
28 | .Select(o => o[1]) |
28 | .Select(o => o[1]) |
|
29 | .FirstOrDefault(); |
29 | .FirstOrDefault(); |
|
30 | } |
30 | } |
|
31 | |
31 | |
|
32 | /////////////////////////////////////////////////////////////////////////// |
32 | /////////////////////////////////////////////////////////////////////////// |
|
33 | // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // |
33 | // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // |
|
34 | /////////////////////////////////////////////////////////////////////////// |
34 | /////////////////////////////////////////////////////////////////////////// |
|
35 | /// <summary> |
35 | /// <summary> |
|
36 | /// Returns a key-value data string with a key set to a given value. |
36 | /// Returns a key-value data string with a key set to a given value. |
|
37 | /// </summary> |
37 | /// </summary> |
|
38 | /// <returns> |
38 | /// <returns> |
|
39 | /// a key-value data string or the empty string if either key or |
39 | /// a key-value data string or the empty string if either key or |
|
40 | /// value are empty |
40 | /// value are empty |
|
41 | /// </returns> |
41 | /// </returns> |
|
42 | public static string Set(string key, string value, string data) |
42 | public static string Set(string key, string value, string data) |
|
43 | { |
43 | { |
|
44 | return string.Join("&", string.Join("&", data.Split('&') |
44 | return string.Join("&", string.Join("&", data.Split('&') |
|
45 | .AsParallel() |
45 | .AsParallel() |
|
46 | .Select(o => o.Split('=')) |
46 | .Select(o => o.Split('=')) |
|
47 | .Where(o => o.Length.Equals(2) && !Strings.StringEquals(o[0], key, StringComparison.Ordinal)) |
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)); |
48 | .Select(o => string.Join("=", o[0], o[1]))), string.Join("=", key, value)); |
|
49 | } |
49 | } |
|
50 | |
50 | |
|
51 | /////////////////////////////////////////////////////////////////////////// |
51 | /////////////////////////////////////////////////////////////////////////// |
|
52 | // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // |
52 | // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // |
|
53 | /////////////////////////////////////////////////////////////////////////// |
53 | /////////////////////////////////////////////////////////////////////////// |
|
54 | /// <summary> |
54 | /// <summary> |
|
55 | /// Deletes a key-value pair from a string referenced by a key. |
55 | /// Deletes a key-value pair from a string referenced by a key. |
|
56 | /// </summary> |
56 | /// </summary> |
|
57 | /// <returns>a key-value pair string</returns> |
57 | /// <returns>a key-value pair string</returns> |
|
58 | public static string Delete(string key, string data) |
58 | public static string Delete(string key, string data) |
|
59 | { |
59 | { |
|
60 | return string.Join("&", data.Split('&') |
60 | return string.Join("&", data.Split('&') |
|
61 | .AsParallel() |
61 | .AsParallel() |
|
62 | .Select(o => o.Split('=')) |
62 | .Select(o => o.Split('=')) |
|
63 | .Where(o => o.Length.Equals(2) && !Strings.StringEquals(o[0], key, StringComparison.Ordinal)) |
63 | .Where(o => o.Length.Equals(2) && !Strings.StringEquals(o[0], key, StringComparison.Ordinal)) |
|
64 | .Select(o => string.Join("=", o[0], o[1])) |
64 | .Select(o => string.Join("=", o[0], o[1]))); |
|
65 | .ToArray()); |
- | ||
66 | } |
65 | } |
|
67 | |
66 | |
|
68 | /////////////////////////////////////////////////////////////////////////// |
67 | /////////////////////////////////////////////////////////////////////////// |
|
69 | // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // |
68 | // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // |
|
70 | /////////////////////////////////////////////////////////////////////////// |
69 | /////////////////////////////////////////////////////////////////////////// |
|
71 | /// <summary> |
70 | /// <summary> |
|
72 | /// Decodes key-value pair data to a dictionary. |
71 | /// Decodes key-value pair data to a dictionary. |
|
73 | /// </summary> |
72 | /// </summary> |
|
74 | /// <returns>a dictionary containing the keys and values</returns> |
73 | /// <returns>a dictionary containing the keys and values</returns> |
|
75 | public static Dictionary<string, string> Decode(string data) |
74 | public static Dictionary<string, string> Decode(string data) |
|
76 | { |
75 | { |
|
77 | return data.Split('&') |
76 | return data.Split('&') |
|
78 | .AsParallel() |
77 | .AsParallel() |
|
79 | .Select(o => o.Split('=')) |
78 | .Select(o => o.Split('=')) |
|
80 | .Where(o => o.Length.Equals(2)) |
79 | .Where(o => o.Length.Equals(2)) |
|
81 | .Select(o => new |
80 | .Select(o => new |
|
82 | { |
81 | { |
|
83 | k = o[0], |
82 | k = o[0], |
|
84 | v = o[1] |
83 | v = o[1] |
|
85 | }) |
84 | }) |
|
86 | .GroupBy(o => o.k) |
85 | .GroupBy(o => o.k) |
|
87 | .ToDictionary(o => o.Key, p => p.First().v); |
86 | .ToDictionary(o => o.Key, p => p.FirstOrDefault()?.v); |
|
88 | } |
87 | } |
|
89 | |
88 | |
|
90 | /////////////////////////////////////////////////////////////////////////// |
89 | /////////////////////////////////////////////////////////////////////////// |
|
91 | // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // |
90 | // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // |
|
92 | /////////////////////////////////////////////////////////////////////////// |
91 | /////////////////////////////////////////////////////////////////////////// |
|
93 | /// <summary> |
92 | /// <summary> |
|
94 | /// Serialises a dictionary to key-value data. |
93 | /// Serialises a dictionary to key-value data. |
|
95 | /// </summary> |
94 | /// </summary> |
|
96 | /// <returns>a key-value data encoded string</returns> |
95 | /// <returns>a key-value data encoded string</returns> |
|
97 | public static string Encode(Dictionary<string, string> data) |
96 | public static string Encode(Dictionary<string, string> data) |
|
98 | { |
97 | { |
|
99 | return string.Join("&", data.AsParallel().Select(o => string.Join("=", o.Key, o.Value))); |
98 | return string.Join("&", data.AsParallel().Select(o => string.Join("=", o.Key, o.Value))); |
|
100 | } |
99 | } |
|
101 | |
100 | |
|
102 | /////////////////////////////////////////////////////////////////////////// |
101 | /////////////////////////////////////////////////////////////////////////// |
|
103 | // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // |
102 | // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // |
|
104 | /////////////////////////////////////////////////////////////////////////// |
103 | /////////////////////////////////////////////////////////////////////////// |
|
105 | /// <summary> |
104 | /// <summary> |
|
106 | /// Escapes a dictionary's keys and values for sending as POST data. |
105 | /// Escapes a dictionary's keys and values for sending as POST data. |
|
107 | /// </summary> |
106 | /// </summary> |
|
108 | public static Dictionary<string, string> Escape(Dictionary<string, string> data, Func<string, string> func) |
107 | public static Dictionary<string, string> Escape(Dictionary<string, string> data, Func<string, string> func) |
|
109 | { |
108 | { |
|
110 | return data.AsParallel().ToDictionary(o => func(o.Key), p => func(p.Value)); |
109 | return data.AsParallel().ToDictionary(o => func(o.Key), p => func(p.Value)); |
|
111 | } |
110 | } |
|
112 | } |
111 | } |
|
113 | } |
112 | } |
|
114 | |
113 | |
|
115 |
|
114 |
|
|
116 | |
115 | |
|
117 | |
116 | |
|
118 | |
117 | |