wasSharp – Diff between revs 58 and 59

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