Korero – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 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.Collections.Generic;
8 using System.Linq;
9 using System.Threading.Tasks;
10  
11 namespace Korero.Serialization
12 {
13 public static class KeyValue
14 {
15 #region Public Methods
16  
17 ///////////////////////////////////////////////////////////////////////////
18 // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 //
19 ///////////////////////////////////////////////////////////////////////////
20 /// <summary>
21 /// Returns the value of a key from a key-value data string.
22 /// </summary>
23 /// <returns>true if the key was found in data</returns>
24 public static string Get(string key, string data)
25 {
26 return data.Split('&')
27 .Select(o => o.Split('='))
28 .Where(o => o.Length.Equals(2) && o[0] == key)
29 .Select(o => o[1])
30 .FirstOrDefault();
31 }
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>
43 public static string Set(string key, string value, string data)
44 {
45 return string.Join("&",
46 string.Join("&",
47 data.Split('&')
48 .Select(o => o.Split('='))
49 .Where(o => o.Length.Equals(2) && o[0] == key)
50 .Select(o => o[0] + "=" + o[1])),
51 key + "=" + value);
52 }
53  
54 ///////////////////////////////////////////////////////////////////////////
55 // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 //
56 ///////////////////////////////////////////////////////////////////////////
57 /// <summary>
58 /// Deletes a key-value pair from a string referenced by a key.
59 /// </summary>
60 /// <returns>a key-value pair string</returns>
61 public static string Delete(string key, string data)
62 {
63 return string.Join("&",
64 data.Split('&')
65 .Select(o => o.Split('='))
66 .Where(o => o.Length.Equals(2) && o[0] == key)
67 .Select(o => o[0] + "=" + o[1]));
68 }
69  
70 ///////////////////////////////////////////////////////////////////////////
71 // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 //
72 ///////////////////////////////////////////////////////////////////////////
73 /// <summary>
74 /// Decodes key-value pair data to a dictionary.
75 /// </summary>
76 /// <returns>a dictionary containing the keys and values</returns>
77 public static IEnumerable<KeyValuePair<string, string>> Decode(string data)
78 {
79 return data.Split('&')
80 .Select(o => o.Split('='))
81 .Where(o => o.Length.Equals(2))
82 .Select(o => new
83 {
84 k = o[0],
85 v = o[1]
86 })
87 .GroupBy(o => o.k)
88 .ToDictionary(o => o.Key,
89 p => p.FirstOrDefault()
90 ?.v);
91 }
92  
93 ///////////////////////////////////////////////////////////////////////////
94 // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 //
95 ///////////////////////////////////////////////////////////////////////////
96 /// <summary>
97 /// Serializes a dictionary to key-value data.
98 /// </summary>
99 /// <returns>a key-value data encoded string</returns>
100 public static string Encode(Dictionary<string, string> data)
101 {
102 return string.Join("&", data.Select(o => $"{o.Key}={o.Value}"));
103 }
104  
105 ///////////////////////////////////////////////////////////////////////////
106 // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 //
107 ///////////////////////////////////////////////////////////////////////////
108 /// <summary>
109 /// Serializes a dictionary to key-value data.
110 /// </summary>
111 /// <param name="data">an input string</param>
112 /// <returns>a key-value data encoded string</returns>
113 public static string Encode(IEnumerable<KeyValuePair<string, string>> data)
114 {
115 return string.Join("&", data.Select(o => $"{o.Key}={o.Value}"));
116 }
117  
118 ///////////////////////////////////////////////////////////////////////////
119 // Copyright (C) 2018 Wizardry and Steamworks - License: GNU GPLv3 //
120 ///////////////////////////////////////////////////////////////////////////
121 /// <summary>
122 /// Asynchronously serializes a dictionary to key-value data.
123 /// </summary>
124 /// <param name="data">an input string</param>
125 /// <returns>a key-value data encoded string</returns>
126 public static string Encode(IEnumerable<KeyValuePair<Task<string>, Task<string>>> data)
127 {
128 return string.Join("&", data.Select(o => $"{o.Key.Result}={o.Value.Result}"));
129 }
130  
131 #endregion
132 }
133 }