wasSharp – Diff between revs 52 and 55

Subversion Repositories:
Rev:
Only display areas with differencesIgnore whitespace
Rev 52 Rev 55
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.Languages
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) && string.Equals(o[0], key, StringComparison.Ordinal)) 27 .Where(o => o.Length.Equals(2) && string.Equals(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) && !string.Equals(o[0], key, StringComparison.Ordinal)) 47 .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)); 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) && !string.Equals(o[0], key, StringComparison.Ordinal)) 63 .Where(o => o.Length.Equals(2) && !string.Equals(o[0], key, StringComparison.Ordinal))
64 .Select(o => string.Join("=", o[0], o[1]))); 64 .Select(o => string.Join("=", o[0], o[1])));
65 } 65 }
66   66  
67 /////////////////////////////////////////////////////////////////////////// 67 ///////////////////////////////////////////////////////////////////////////
68 // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // 68 // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 //
69 /////////////////////////////////////////////////////////////////////////// 69 ///////////////////////////////////////////////////////////////////////////
70 /// <summary> 70 /// <summary>
71 /// Decodes key-value pair data to a dictionary. 71 /// Decodes key-value pair data to a dictionary.
72 /// </summary> 72 /// </summary>
73 /// <returns>a dictionary containing the keys and values</returns> 73 /// <returns>a dictionary containing the keys and values</returns>
74 public static Dictionary<string, string> Decode(string data) 74 public static Dictionary<string, string> Decode(string data)
75 { 75 {
76 return data.Split('&') 76 return data.Split('&')
77 .AsParallel() 77 .AsParallel()
78 .Select(o => o.Split('=')) 78 .Select(o => o.Split('='))
79 .Where(o => o.Length.Equals(2)) 79 .Where(o => o.Length.Equals(2))
80 .Select(o => new 80 .Select(o => new
81 { 81 {
82 k = o[0], 82 k = o[0],
83 v = o[1] 83 v = o[1]
84 }) 84 })
85 .GroupBy(o => o.k) 85 .GroupBy(o => o.k)
86 .ToDictionary(o => o.Key, p => p.FirstOrDefault()?.v); 86 .ToDictionary(o => o.Key, p => p.FirstOrDefault()?.v);
87 } 87 }
88   88  
89 /////////////////////////////////////////////////////////////////////////// 89 ///////////////////////////////////////////////////////////////////////////
90 // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // 90 // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 //
91 /////////////////////////////////////////////////////////////////////////// 91 ///////////////////////////////////////////////////////////////////////////
92 /// <summary> 92 /// <summary>
93 /// Serialises a dictionary to key-value data. 93 /// Serialises a dictionary to key-value data.
94 /// </summary> 94 /// </summary>
95 /// <returns>a key-value data encoded string</returns> 95 /// <returns>a key-value data encoded string</returns>
96 public static string Encode(Dictionary<string, string> data) 96 public static string Encode(Dictionary<string, string> data)
97 { 97 {
98 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)));
99 } 99 }
100   100  
101 /////////////////////////////////////////////////////////////////////////// 101 ///////////////////////////////////////////////////////////////////////////
102 // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // 102 // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 //
103 /////////////////////////////////////////////////////////////////////////// 103 ///////////////////////////////////////////////////////////////////////////
104 /// <summary> 104 /// <summary>
105 /// Serialises a dictionary to key-value data. 105 /// Serialises a dictionary to key-value data.
106 /// </summary> 106 /// </summary>
107 /// <returns>a key-value data encoded string</returns> 107 /// <returns>a key-value data encoded string</returns>
108 public static string Encode(IEnumerable<KeyValuePair<string, string>> data) 108 public static string Encode(IEnumerable<KeyValuePair<string, string>> data)
109 { 109 {
110 return string.Join("&", data.AsParallel().Select(o => string.Join("=", o.Key, o.Value))); 110 return string.Join("&", data.AsParallel().Select(o => string.Join("=", o.Key, o.Value)));
111 } 111 }
112   112  
113 /////////////////////////////////////////////////////////////////////////// 113 ///////////////////////////////////////////////////////////////////////////
114 // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // 114 // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 //
115 /////////////////////////////////////////////////////////////////////////// 115 ///////////////////////////////////////////////////////////////////////////
116 /// <summary> 116 /// <summary>
117 /// Escapes a dictionary's keys and values for sending as POST data. 117 /// Escapes a dictionary's keys and values for sending as POST data.
118 /// </summary> 118 /// </summary>
119 public static IEnumerable<KeyValuePair<string, string>> Escape(IEnumerable<KeyValuePair<string, string>> data, 119 public static IEnumerable<KeyValuePair<string, string>> Escape(IEnumerable<KeyValuePair<string, string>> data,
120 Func<string, string> func) 120 Func<string, string> func)
121 { 121 {
122 return data.AsParallel().ToDictionary(o => func(o.Key), p => func(p.Value)); 122 return data.AsParallel().ToDictionary(o => func(o.Key), p => func(p.Value));
123 } 123 }
124 } 124 }
125 } 125 }
126   126