corrade-vassal – Blame information for rev 14

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;
14 zed 10 using System.Linq.Expressions;
13 eva 11  
12 namespace wasSharp
13 {
14 public class KeyValue
15 {
16 ///////////////////////////////////////////////////////////////////////////
17 // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 //
18 ///////////////////////////////////////////////////////////////////////////
19 /// <summary>
20 /// Returns the value of a key from a key-value data string.
21 /// </summary>
22 /// <returns>true if the key was found in data</returns>
14 zed 23 public static Func<string, string, string> Get =
24 ((Expression<Func<string, string, string>>) ((key, data) => 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])
30 .FirstOrDefault())).Compile();
13 eva 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>
14 zed 42 public static Func<string, string, string, string> Set =
43 ((Expression<Func<string, string, string, string>>)
44 ((key, value, data) => string.Join("&", string.Join("&", data.Split('&')
45 .AsParallel()
46 .Select(o => o.Split('='))
47 .Where(o => o.Length.Equals(2))
48 .Where(o => !o[0].Equals(key))
49 .Select(o => string.Join("=", o[0], o[1]))), string.Join("=", key, value)))).Compile();
13 eva 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>
14 zed 58 public static Func<string, string, string> Delete =
59 ((Expression<Func<string, string, string>>) ((key, data) => string.Join("&", data.Split('&')
13 eva 60 .AsParallel()
14 zed 61 .Select(o => o.Split('='))
62 .Where(o => o.Length.Equals(2))
63 .Where(o => !o[0].Equals(key))
64 .Select(o => string.Join("=", o[0], o[1]))
65 .ToArray()))).Compile();
13 eva 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>
14 zed 74 public static Func<string, Dictionary<string, string>> Decode =
75 ((Expression<Func<string, Dictionary<string, string>>>) (data => data.Split('&')
13 eva 76 .AsParallel()
14 zed 77 .Select(o => o.Split('='))
78 .Where(o => o.Length.Equals(2))
13 eva 79 .Select(o => new
80 {
14 zed 81 k = o[0],
82 v = o[1]
13 eva 83 })
84 .GroupBy(o => o.k)
14 zed 85 .ToDictionary(o => o.Key, p => p.First().v))).Compile();
13 eva 86  
87 ///////////////////////////////////////////////////////////////////////////
88 // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 //
89 ///////////////////////////////////////////////////////////////////////////
90 /// <summary>
91 /// Serialises a dictionary to key-value data.
92 /// </summary>
93 /// <returns>a key-value data encoded string</returns>
14 zed 94 public static Func<Dictionary<string, string>, string> Encode =
95 ((Expression<Func<Dictionary<string, string>, string>>)
96 (data => string.Join("&", data.AsParallel().Select(o => string.Join("=", o.Key, o.Value))))).Compile();
13 eva 97  
98 ///////////////////////////////////////////////////////////////////////////
99 // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 //
100 ///////////////////////////////////////////////////////////////////////////
14 zed 101 /// <summary>
102 /// Escapes a dictionary's keys and values for sending as POST data.
103 /// </summary>
104 public static Func<Dictionary<string, string>, Func<string, string>, Dictionary<string, string>> Escape =
105 ((Expression<Func<Dictionary<string, string>, Func<string, string>, Dictionary<string, string>>>)
106 ((data, func) => data.AsParallel().ToDictionary(o => func(o.Key), p => func(p.Value)))).Compile();
13 eva 107 }
108 }