/KeyValue.cs |
@@ -7,11 +7,10 @@ |
using System; |
using System.Collections.Generic; |
using System.Linq; |
using System.Linq.Expressions; |
|
namespace wasSharp |
{ |
public class KeyValue |
public static class KeyValue |
{ |
/////////////////////////////////////////////////////////////////////////// |
// Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // |
@@ -20,14 +19,15 @@ |
/// Returns the value of a key from a key-value data string. |
/// </summary> |
/// <returns>true if the key was found in data</returns> |
public static Func<string, string, string> Get = |
((Expression<Func<string, string, string>>) ((key, data) => data.Split('&') |
public static string Get(string key, string data) |
{ |
return data.Split('&') |
.AsParallel() |
.Select(o => o.Split('=')) |
.Where(o => o.Length.Equals(2)) |
.Where(o => o[0].Equals(key)) |
.Where(o => o.Length.Equals(2) && Strings.StringEquals(o[0], key, StringComparison.Ordinal)) |
.Select(o => o[1]) |
.FirstOrDefault())).Compile(); |
.FirstOrDefault(); |
} |
|
/////////////////////////////////////////////////////////////////////////// |
// Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // |
@@ -39,14 +39,14 @@ |
/// a key-value data string or the empty string if either key or |
/// value are empty |
/// </returns> |
public static Func<string, string, string, string> Set = |
((Expression<Func<string, string, string, string>>) |
((key, value, data) => string.Join("&", string.Join("&", data.Split('&') |
.AsParallel() |
.Select(o => o.Split('=')) |
.Where(o => o.Length.Equals(2)) |
.Where(o => !o[0].Equals(key)) |
.Select(o => string.Join("=", o[0], o[1]))), string.Join("=", key, value)))).Compile(); |
public static string Set(string key, string value, string data) |
{ |
return string.Join("&", string.Join("&", data.Split('&') |
.AsParallel() |
.Select(o => o.Split('=')) |
.Where(o => o.Length.Equals(2) && !Strings.StringEquals(o[0], key, StringComparison.Ordinal)) |
.Select(o => string.Join("=", o[0], o[1]))), string.Join("=", key, value)); |
} |
|
/////////////////////////////////////////////////////////////////////////// |
// Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // |
@@ -55,14 +55,15 @@ |
/// Deletes a key-value pair from a string referenced by a key. |
/// </summary> |
/// <returns>a key-value pair string</returns> |
public static Func<string, string, string> Delete = |
((Expression<Func<string, string, string>>) ((key, data) => string.Join("&", data.Split('&') |
public static string Delete(string key, string data) |
{ |
return string.Join("&", data.Split('&') |
.AsParallel() |
.Select(o => o.Split('=')) |
.Where(o => o.Length.Equals(2)) |
.Where(o => !o[0].Equals(key)) |
.Where(o => o.Length.Equals(2) && !Strings.StringEquals(o[0], key, StringComparison.Ordinal)) |
.Select(o => string.Join("=", o[0], o[1])) |
.ToArray()))).Compile(); |
.ToArray()); |
} |
|
/////////////////////////////////////////////////////////////////////////// |
// Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // |
@@ -71,8 +72,9 @@ |
/// Decodes key-value pair data to a dictionary. |
/// </summary> |
/// <returns>a dictionary containing the keys and values</returns> |
public static Func<string, Dictionary<string, string>> Decode = |
((Expression<Func<string, Dictionary<string, string>>>) (data => data.Split('&') |
public static Dictionary<string, string> Decode(string data) |
{ |
return data.Split('&') |
.AsParallel() |
.Select(o => o.Split('=')) |
.Where(o => o.Length.Equals(2)) |
@@ -82,7 +84,8 @@ |
v = o[1] |
}) |
.GroupBy(o => o.k) |
.ToDictionary(o => o.Key, p => p.First().v))).Compile(); |
.ToDictionary(o => o.Key, p => p.First().v); |
} |
|
/////////////////////////////////////////////////////////////////////////// |
// Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // |
@@ -91,9 +94,10 @@ |
/// Serialises a dictionary to key-value data. |
/// </summary> |
/// <returns>a key-value data encoded string</returns> |
public static Func<Dictionary<string, string>, string> Encode = |
((Expression<Func<Dictionary<string, string>, string>>) |
(data => string.Join("&", data.AsParallel().Select(o => string.Join("=", o.Key, o.Value))))).Compile(); |
public static string Encode(Dictionary<string, string> data) |
{ |
return string.Join("&", data.AsParallel().Select(o => string.Join("=", o.Key, o.Value))); |
} |
|
/////////////////////////////////////////////////////////////////////////// |
// Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // |
@@ -101,8 +105,9 @@ |
/// <summary> |
/// Escapes a dictionary's keys and values for sending as POST data. |
/// </summary> |
public static Func<Dictionary<string, string>, Func<string, string>, Dictionary<string, string>> Escape = |
((Expression<Func<Dictionary<string, string>, Func<string, string>, Dictionary<string, string>>>) |
((data, func) => data.AsParallel().ToDictionary(o => func(o.Key), p => func(p.Value)))).Compile(); |
public static Dictionary<string, string> Escape(Dictionary<string, string> data, Func<string, string> func) |
{ |
return data.AsParallel().ToDictionary(o => func(o.Key), p => func(p.Value)); |
} |
} |
} |