wasSharp – Blame information for rev 27

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