corrade-vassal – Blame information for rev 16

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