namespace Deppton.Model
{
using System;
using System.Text;
using System.Text.RegularExpressions;
///
/// Utility functions class.
///
public static class Utilities
{
///
/// Quote in comma separated value string lists.
///
private const string Quote = "\"";
///
/// Escaped quote in comma separated value string lists.
///
private const string EscapedQuote = "\"\"";
///
/// Characters required to be quoted for comma separated value string lists.
///
private static char[] charactersRequiredToBeQuoted = { ',', '\"' };
///
/// Regular expression to split comma separated value string lists.
///
private static Regex splitterRegex = new Regex(@",(?=(?:[^""]*""[^""]*"")*(?![^""]*""))");
///
/// Escapes the string list into a comma separated value string.
///
/// The values to escape.
/// The escaped comma separated value string.
public static string EscapeList(params string[] values)
{
StringBuilder builder = new StringBuilder();
for (var index = 0; index < values.Length; index++)
{
var value = values[index];
builder.Append('\"' + Escape(value) + '\"');
if (index < values.Length - 1)
{
builder.Append(',');
}
}
return builder.ToString();
}
///
/// Unescapes a comma separated value string list.
///
/// The comma separated value string.
/// The unescaped values.
public static string[] UnescapeList(string value)
{
string[] values = splitterRegex.Split(value);
for (int i = 0; i < values.Length; i++)
{
values[i] = Unescape(values[i]);
}
return values;
}
///
/// Determines whether a location is within a expected radius.
///
/// The latitude.
/// The longitude.
/// The expected latitude.
/// The expected longitude.
/// The tolerance in decimal degrees.
///
/// true if location is within the expected radius, otherwise, false .
///
public static bool IsWithinRange(double latitude, double longitude, double expectedLatitude, double expectedLongitude, double tolerance)
{
return GetDelta(latitude, longitude, expectedLatitude, expectedLongitude) <= Math.Abs(tolerance);
}
///
/// Gets the absolute delta between to locations.
///
/// The latitude.
/// The longitude.
/// The expected latitude.
/// The expected longitude.
/// The absolute delta in decimal degrees.
public static double GetDelta(double latitude, double longitude, double expectedLatitude, double expectedLongitude)
{
return Math.Abs(Math.Pow(Math.Pow(expectedLatitude - latitude, 2) + Math.Pow(expectedLongitude - longitude, 2), 0.5));
}
///
/// Escapes the specified string to create comma separated value string list.
///
/// The string to escape.
/// The escaped string.
private static string Escape(string value)
{
if (value.Contains(Quote))
{
value = value.Replace(Quote, EscapedQuote);
}
if (value.IndexOfAny(charactersRequiredToBeQuoted) > -1)
{
value = Quote + value + Quote;
}
return value;
}
///
/// Unescapes the specified string coming from create comma separated value string list.
///
/// The string to unescape.
/// The unescaped string.
private static string Unescape(string value)
{
if (value.StartsWith(Quote) && value.EndsWith(Quote))
{
value = value.Substring(1, value.Length - 2);
if (value.Contains(EscapedQuote))
{
value = value.Replace(EscapedQuote, Quote);
}
}
return value;
}
}
}