wasSharp – Diff between revs 6 and 7

Subversion Repositories:
Rev:
Show entire fileIgnore whitespace
Rev 6 Rev 7
Line 5... Line 5...
5 /////////////////////////////////////////////////////////////////////////// 5 ///////////////////////////////////////////////////////////////////////////
Line 6... Line 6...
6   6  
7 using System; 7 using System;
8 using System.Collections.Generic; 8 using System.Collections.Generic;
9 using System.Linq; -  
Line 10... Line 9...
10 using System.Linq.Expressions; 9 using System.Linq;
11   10  
12 namespace wasSharp 11 namespace wasSharp
13 { 12 {
14 public class KeyValue 13 public static class KeyValue
15 { 14 {
16 /////////////////////////////////////////////////////////////////////////// 15 ///////////////////////////////////////////////////////////////////////////
17 // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // 16 // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 //
18 /////////////////////////////////////////////////////////////////////////// 17 ///////////////////////////////////////////////////////////////////////////
19 /// <summary> 18 /// <summary>
20 /// 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.
21 /// </summary> 20 /// </summary>
-   21 /// <returns>true if the key was found in data</returns>
22 /// <returns>true if the key was found in data</returns> 22 public static string Get(string key, string data)
23 public static Func<string, string, string> Get = 23 {
24 ((Expression<Func<string, string, string>>) ((key, data) => data.Split('&') 24 return data.Split('&')
25 .AsParallel() 25 .AsParallel()
26 .Select(o => o.Split('=')) -  
27 .Where(o => o.Length.Equals(2)) 26 .Select(o => o.Split('='))
28 .Where(o => o[0].Equals(key)) 27 .Where(o => o.Length.Equals(2) && Strings.StringEquals(o[0], key, StringComparison.Ordinal))
-   28 .Select(o => o[1])
Line 29... Line 29...
29 .Select(o => o[1]) 29 .FirstOrDefault();
30 .FirstOrDefault())).Compile(); 30 }
31   31  
32 /////////////////////////////////////////////////////////////////////////// 32 ///////////////////////////////////////////////////////////////////////////
Line 37... Line 37...
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 Func<string, string, string, string> Set = 42 public static string Set(string key, string value, string data)
43 ((Expression<Func<string, string, string, string>>) 43 {
44 ((key, value, data) => 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)) 47 .Where(o => o.Length.Equals(2) && !Strings.StringEquals(o[0], key, StringComparison.Ordinal))
48 .Where(o => !o[0].Equals(key)) -  
49 .Select(o => string.Join("=", o[0], o[1]))), string.Join("=", key, value)))).Compile(); 48 .Select(o => string.Join("=", o[0], o[1]))), string.Join("=", key, value));
-   49 }
Line 50... Line 50...
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 Func<string, string, string> Delete = 59 {
59 ((Expression<Func<string, string, string>>) ((key, data) => string.Join("&", data.Split('&') 60 return string.Join("&", data.Split('&')
60 .AsParallel() 61 .AsParallel()
61 .Select(o => o.Split('=')) 62 .Select(o => o.Split('='))
62 .Where(o => o.Length.Equals(2)) -  
63 .Where(o => !o[0].Equals(key)) 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());
Line 65... Line 66...
65 .ToArray()))).Compile(); 66 }
66   67  
67 /////////////////////////////////////////////////////////////////////////// 68 ///////////////////////////////////////////////////////////////////////////
68 // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // 69 // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 //
69 /////////////////////////////////////////////////////////////////////////// 70 ///////////////////////////////////////////////////////////////////////////
70 /// <summary> 71 /// <summary>
71 /// Decodes key-value pair data to a dictionary. 72 /// Decodes key-value pair data to a dictionary.
72 /// </summary> 73 /// </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 Func<string, Dictionary<string, string>> Decode = 76 {
75 ((Expression<Func<string, Dictionary<string, string>>>) (data => data.Split('&') 77 return data.Split('&')
76 .AsParallel() 78 .AsParallel()
77 .Select(o => o.Split('=')) 79 .Select(o => o.Split('='))
78 .Where(o => o.Length.Equals(2)) 80 .Where(o => o.Length.Equals(2))
79 .Select(o => new 81 .Select(o => new
80 { 82 {
81 k = o[0], 83 k = o[0],
82 v = o[1] 84 v = o[1]
83 }) 85 })
-   86 .GroupBy(o => o.k)
Line 84... Line 87...
84 .GroupBy(o => o.k) 87 .ToDictionary(o => o.Key, p => p.First().v);
85 .ToDictionary(o => o.Key, p => p.First().v))).Compile(); 88 }
86   89  
87 /////////////////////////////////////////////////////////////////////////// 90 ///////////////////////////////////////////////////////////////////////////
88 // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // 91 // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 //
89 /////////////////////////////////////////////////////////////////////////// 92 ///////////////////////////////////////////////////////////////////////////
90 /// <summary> 93 /// <summary>
91 /// Serialises a dictionary to key-value data. 94 /// Serialises a dictionary to key-value data.
92 /// </summary> 95 /// </summary>
93 /// <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)
Line 94... Line 98...
94 public static Func<Dictionary<string, string>, string> Encode = 98 {
95 ((Expression<Func<Dictionary<string, string>, string>>) 99 return string.Join("&", data.AsParallel().Select(o => string.Join("=", o.Key, o.Value)));
96 (data => string.Join("&", data.AsParallel().Select(o => string.Join("=", o.Key, o.Value))))).Compile(); 100 }
97   101  
98 /////////////////////////////////////////////////////////////////////////// 102 ///////////////////////////////////////////////////////////////////////////
99 // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // 103 // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 //
100 /////////////////////////////////////////////////////////////////////////// 104 ///////////////////////////////////////////////////////////////////////////
101 /// <summary> -  
-   105 /// <summary>
102 /// Escapes a dictionary's keys and values for sending as POST data. 106 /// Escapes a dictionary's keys and values for sending as POST data.
-   107 /// </summary>
103 /// </summary> 108 public static Dictionary<string, string> Escape(Dictionary<string, string> data, Func<string, string> func)
104 public static Func<Dictionary<string, string>, Func<string, string>, Dictionary<string, string>> Escape = 109 {
105 ((Expression<Func<Dictionary<string, string>, Func<string, string>, Dictionary<string, string>>>) 110 return data.AsParallel().ToDictionary(o => func(o.Key), p => func(p.Value));