corrade-vassal

Subversion Repositories:
Compare Path: Rev
With Path: Rev
?path1? @ 13  →  ?path2? @ 14
/wasSharp/CSV.cs
@@ -4,10 +4,11 @@
// rights of fair usage, the disclaimer and warranty conditions. //
///////////////////////////////////////////////////////////////////////////
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
 
namespace wasSharp
{
@@ -17,31 +18,32 @@
// Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 //
///////////////////////////////////////////////////////////////////////////
/// <summary>
/// Converts a list of string to a comma-separated values string.
/// Converts a list of strings to a comma-separated values string.
/// </summary>
/// <param name="l">a list of strings</param>
/// <returns>a commma-separated list of values</returns>
/// <remarks>compliant with RFC 4180</remarks>
public static string FromEnumerable(IEnumerable<string> l)
{
string[] csv = l.Select(o => o).ToArray();
char[] escapeCharacters = {'"', ' ', ',', '\r', '\n'};
Parallel.ForEach(csv.Select((v, i) => new {i, v}), o =>
{
string cell = o.v.Replace("\"", "\"\"");
public static Func<IEnumerable<string>, string> FromEnumerable =
((Expression<Func<IEnumerable<string>, string>>) (data => string.Join(",",
data
.Select(o => o.Replace("\"", "\"\""))
.Select(o => o.IndexOfAny(new[] {'"', ' ', ',', '\r', '\n'}).Equals(-1) ? o : "\"" + o + "\""))))
.Compile();
 
switch (cell.IndexOfAny(escapeCharacters))
{
case -1:
csv[o.i] = cell;
break;
default:
csv[o.i] = "\"" + cell + "\"";
break;
}
});
return string.Join(",", csv);
}
///////////////////////////////////////////////////////////////////////////
// Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 //
///////////////////////////////////////////////////////////////////////////
/// <summary>
/// Converts successive comma-separated values to key-value pairs.
/// </summary>
/// <returns>key-value pairs of successive comma-separate values</returns>
public static Func<string, IEnumerable<KeyValuePair<string, string>>> ToKeyValue =
((Expression<Func<string, IEnumerable<KeyValuePair<string, string>>>>)
(csv => ToEnumerable(csv).AsParallel().Select((o, p) => new {o, p})
.GroupBy(q => q.p/2, q => q.o)
.Select(o => o.ToArray())
.TakeWhile(o => o.Length%2 == 0)
.Where(o => !string.IsNullOrEmpty(o[0]) || !string.IsNullOrEmpty(o[1]))
.ToDictionary(o => o[0], p => p[1]).Select(o => o))).Compile();
 
///////////////////////////////////////////////////////////////////////////
// Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 //
/wasSharp/KeyValue.cs
@@ -7,6 +7,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
 
namespace wasSharp
{
@@ -18,24 +19,15 @@
/// <summary>
/// Returns the value of a key from a key-value data string.
/// </summary>
/// <param name="key">the key of the value</param>
/// <param name="data">the key-value data segment</param>
/// <returns>true if the key was found in data</returns>
public static string Get(string key, string data)
{
return data.Split('&')
public static Func<string, string, string> Get =
((Expression<Func<string, string, string>>) ((key, data) => data.Split('&')
.AsParallel()
.Select(o => o.Split('=').ToList())
.Where(o => o.Count.Equals(2))
.Select(o => new
{
k = o.First(),
v = o.Last()
})
.Where(o => o.k.Equals(key))
.Select(o => o.v)
.FirstOrDefault();
}
.Select(o => o.Split('='))
.Where(o => o.Length.Equals(2))
.Where(o => o[0].Equals(key))
.Select(o => o[1])
.FirstOrDefault())).Compile();
 
///////////////////////////////////////////////////////////////////////////
// Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 //
@@ -43,31 +35,18 @@
/// <summary>
/// Returns a key-value data string with a key set to a given value.
/// </summary>
/// <param name="key">the key of the value</param>
/// <param name="value">the value to set the key to</param>
/// <param name="data">the key-value data segment</param>
/// <returns>
/// a key-value data string or the empty string if either key or
/// value are empty
/// </returns>
public static string Set(string key, string value, string data)
{
HashSet<string> output = new HashSet<string>(data.Split('&')
.AsParallel()
.Select(o => o.Split('=').ToList())
.Where(o => o.Count.Equals(2))
.Select(o => new
{
k = o.First(),
v = !o.First().Equals(key) ? o.Last() : value
}).Select(o => string.Join("=", o.k, o.v)));
string append = string.Join("=", key, value);
if (!output.Contains(append))
{
output.Add(append);
}
return string.Join("&", output.ToArray());
}
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();
 
///////////////////////////////////////////////////////////////////////////
// Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 //
@@ -75,24 +54,15 @@
/// <summary>
/// Deletes a key-value pair from a string referenced by a key.
/// </summary>
/// <param name="key">the key to search for</param>
/// <param name="data">the key-value data segment</param>
/// <returns>a key-value pair string</returns>
public static string Delete(string key, string data)
{
return string.Join("&", data.Split('&')
public static Func<string, string, string> Delete =
((Expression<Func<string, string, string>>) ((key, data) => string.Join("&", data.Split('&')
.AsParallel()
.Select(o => o.Split('=').ToList())
.Where(o => o.Count.Equals(2))
.Select(o => new
{
k = o.First(),
v = o.Last()
})
.Where(o => !o.k.Equals(key))
.Select(o => string.Join("=", o.k, o.v))
.ToArray());
}
.Select(o => o.Split('='))
.Where(o => o.Length.Equals(2))
.Where(o => !o[0].Equals(key))
.Select(o => string.Join("=", o[0], o[1]))
.ToArray()))).Compile();
 
///////////////////////////////////////////////////////////////////////////
// Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 //
@@ -100,22 +70,19 @@
/// <summary>
/// Decodes key-value pair data to a dictionary.
/// </summary>
/// <param name="data">the key-value pair data</param>
/// <returns>a dictionary containing the keys and values</returns>
public static Dictionary<string, string> Decode(string data)
{
return data.Split('&')
public static Func<string, Dictionary<string, string>> Decode =
((Expression<Func<string, Dictionary<string, string>>>) (data => data.Split('&')
.AsParallel()
.Select(o => o.Split('=').ToList())
.Where(o => o.Count.Equals(2))
.Select(o => o.Split('='))
.Where(o => o.Length.Equals(2))
.Select(o => new
{
k = o.First(),
v = o.Last()
k = o[0],
v = o[1]
})
.GroupBy(o => o.k)
.ToDictionary(o => o.Key, p => p.First().v);
}
.ToDictionary(o => o.Key, p => p.First().v))).Compile();
 
///////////////////////////////////////////////////////////////////////////
// Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 //
@@ -123,23 +90,19 @@
/// <summary>
/// Serialises a dictionary to key-value data.
/// </summary>
/// <param name="data">a dictionary</param>
/// <returns>a key-value data encoded string</returns>
public static string Encode(Dictionary<string, string> data)
{
return string.Join("&", data.AsParallel().Select(o => string.Join("=", o.Key, o.Value)));
}
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();
 
///////////////////////////////////////////////////////////////////////////
// Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 //
///////////////////////////////////////////////////////////////////////////
/// <summary>Escapes a dictionary's keys and values for sending as POST data.</summary>
/// <param name="data">A dictionary containing keys and values to be escaped</param>
/// <param name="func">The function to use to escape the keys and values in the dictionary.</param>
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));
}
/// <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();
}
}
/wasSharp/Numerics.cs
@@ -4,6 +4,9 @@
// rights of fair usage, the disclaimer and warranty conditions. //
///////////////////////////////////////////////////////////////////////////
 
using System;
using System.Linq.Expressions;
 
namespace wasSharp
{
public class Numerics
@@ -15,26 +18,28 @@
/// Given a value in a source value range and a target range, map
/// the value from the source range into the target range.
/// </summary>
/// <param name="value">the value to map</param>
/// <param name="xMin">the lower bound of the source range</param>
/// <param name="xMax">the upper bound of the source range</param>
/// <param name="yMin">the lower bound of the target range</param>
/// <param name="yMax">the upper bound of the target range</param>
public static double MapValueToRange(double value, double xMin, double xMax, double yMin, double yMax)
{
return yMin + (
(
yMax - yMin
)
*
(
value - xMin
)
/
(
xMax - xMin
)
);
}
/// <remarks>
/// value - the value to map
/// xMin - the lower bound of the source range
/// xMax - the upper bound of the source range
/// yMin - the lower bound of the target range
/// yMax - the upper bound of the target range
/// </remarks>
/// <returns>a value in x mapped in the range of y</returns>
public static Func<double, double, double, double, double, double> MapValueToRange =
((Expression<Func<double, double, double, double, double, double>>)
((value, xMin, xMax, yMin, yMax) => yMin + (
(
yMax - yMin
)
*
(
value - xMin
)
/
(
xMax - xMin
)
))).Compile();
}
}
/wasSharp/Time.cs
@@ -18,6 +18,28 @@
{
public delegate void TimerCallback(object state);
 
/// <summary>
/// Convert an Unix timestamp to a DateTime structure.
/// </summary>
/// <param name="unixTimestamp">the Unix timestamp to convert</param>
/// <returns>the DateTime structure</returns>
/// <remarks>the function assumes UTC time</remarks>
public static DateTime UnixTimestampToDateTime(uint unixTimestamp)
{
return new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc).AddSeconds(unixTimestamp).ToUniversalTime();
}
 
/// <summary>
/// Convert a DateTime structure to a Unix timestamp.
/// </summary>
/// <param name="dateTime">the DateTime structure to convert</param>
/// <returns>the Unix timestamp</returns>
/// <remarks>the function assumes UTC time</remarks>
public static uint DateTimeToUnixTimestamp(DateTime dateTime)
{
return (uint) (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc)).TotalSeconds;
}
 
public sealed class Timer : IDisposable
{
private static readonly Task CompletedTask = Task.FromResult(false);
/wasSharp/Web.cs
@@ -6,6 +6,7 @@
 
using System;
using System.Linq;
using System.Linq.Expressions;
using System.Net;
 
namespace wasSharp
@@ -16,29 +17,30 @@
// Copyright (C) Wizardry and Steamworks 2014 - License: GNU GPLv3 //
///////////////////////////////////////////////////////////////////////////
/// <summary>RFC3986 URI Escapes a string</summary>
/// <param name="data">a string to escape</param>
/// <remarks>
/// data - a string to escape
/// </remarks>
/// <returns>an RFC3986 escaped string</returns>
public static string URIEscapeDataString(string data)
{
// Uri.EscapeDataString can only handle 32766 characters at a time
return string.Join("", Enumerable.Range(0, (data.Length + 32765)/32766)
.Select(o => Uri.EscapeDataString(data.Substring(o*32766, Math.Min(32766, data.Length - (o*32766)))))
.ToArray());
}
public static Func<string, string> URIEscapeDataString =
((Expression<Func<string, string>>)
(data => string.Join("", Enumerable.Range(0, (data.Length + 32765)/32766)
.Select(o => Uri.EscapeDataString(data.Substring(o*32766, Math.Min(32766, data.Length - (o*32766)))))
.ToArray()))).Compile();
 
///////////////////////////////////////////////////////////////////////////
// Copyright (C) Wizardry and Steamworks 2014 - License: GNU GPLv3 //
///////////////////////////////////////////////////////////////////////////
/// <summary>URI unescapes an RFC3986 URI escaped string</summary>
/// <param name="data">a string to unescape</param>
/// <remarks>
/// data - a string to unescape
/// </remarks>
/// <returns>the resulting string</returns>
public static string URIUnescapeDataString(string data)
{
// Uri.UnescapeDataString can only handle 32766 characters at a time
return string.Join("", Enumerable.Range(0, (data.Length + 32765)/32766)
.Select(o => Uri.UnescapeDataString(data.Substring(o*32766, Math.Min(32766, data.Length - (o*32766)))))
.ToArray());
}
public static Func<string, string> URIUnescapeDataString =
((Expression<Func<string, string>>)
(data => string.Join("", Enumerable.Range(0, (data.Length + 32765)/32766)
.Select(
o => Uri.UnescapeDataString(data.Substring(o*32766, Math.Min(32766, data.Length - (o*32766)))))
.ToArray()))).Compile();
 
///////////////////////////////////////////////////////////////////////////
// Copyright (C) Wizardry and Steamworks 2015 - License: GNU GPLv3 //