201 lines
6.5 KiB
C#
201 lines
6.5 KiB
C#
//
|
|
// Class: Extensions
|
|
// Current CLR: 4.0.30319.34209
|
|
// System: Microsoft Visual Studio 10.0
|
|
// Author: dani
|
|
// Created: 5/13/2015 7:37:03 PM
|
|
//
|
|
// Copyright (c) 2015 Informatikbüro Daniel Schick. All rights reserved.
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Collections.ObjectModel;
|
|
using System.Globalization;
|
|
using System.Text.RegularExpressions;
|
|
using log4net;
|
|
using System.Linq;
|
|
using System.Collections;
|
|
|
|
namespace bsmd.database
|
|
{
|
|
/// <summary>
|
|
/// sensible helpers
|
|
/// </summary>
|
|
public static class Extensions
|
|
{
|
|
private static readonly ILog _log = LogManager.GetLogger(typeof(Extensions));
|
|
private static readonly Regex splitRegex = new Regex(@"(""((\\"")|([^""]))*"")|('((\\')|([^']))*')|(\S+)");
|
|
private static readonly char[] splitDelimiter = new[] { ',', ';', ' ' };
|
|
|
|
public static bool IsNullOrEmpty(this String str)
|
|
{
|
|
if (str == null) return true;
|
|
if (str.Trim().Length == 0) return true;
|
|
if (str.Equals("00000000-0000-0000-0000-000000000000")) return true; // for null guids
|
|
return false;
|
|
}
|
|
|
|
public static bool IsNullOrEmpty<T>(this List<T> items)
|
|
{
|
|
return (items == null) || (items.Count == 0);
|
|
}
|
|
|
|
public static bool IsNullOrEmpty<T>(this ObservableCollection<T> items)
|
|
{
|
|
return (items == null) || (items.Count == 0);
|
|
}
|
|
|
|
public static bool IsNullOrEmpty<T>(this Dictionary<T, T> items)
|
|
{
|
|
return (items == null) || (items.Count == 0);
|
|
}
|
|
|
|
public static bool IsNumber(this String str)
|
|
{
|
|
string pattern = @"^[1-9]\d*(\.\d+)?$";
|
|
if (str == null) return false;
|
|
if (str.Trim().Length == 0) return false;
|
|
return new Regex(pattern).IsMatch(str);
|
|
}
|
|
|
|
public static bool IsDigitsOnly(this String str)
|
|
{
|
|
foreach (char c in str)
|
|
{
|
|
if (c < '0' || c > '9')
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
public static bool IsBoolean(this String str)
|
|
{
|
|
if (str == null) return false;
|
|
if (str.Trim().Length == 0) return false;
|
|
if (str.Trim().Equals("true", StringComparison.InvariantCultureIgnoreCase)) return true;
|
|
if (str.Trim().Equals("false", StringComparison.InvariantCultureIgnoreCase)) return true;
|
|
return false;
|
|
}
|
|
|
|
public static bool Contains(this string source, string toCheck, StringComparison comp)
|
|
{
|
|
return source.IndexOf(toCheck, comp) >= 0;
|
|
}
|
|
|
|
public static uint ToUnixTimeStamp(this DateTime source)
|
|
{
|
|
System.DateTime rootDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc);
|
|
return (uint) (source - rootDateTime).TotalSeconds;
|
|
}
|
|
|
|
public static DateTime FromUnixTimeStamp(this UInt32 source)
|
|
{
|
|
System.DateTime rootDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc);
|
|
return rootDateTime.AddSeconds(source);
|
|
}
|
|
|
|
public static DateTime? TryParseDateTime(string text)
|
|
{
|
|
if (DateTime.TryParse(text, DateTimeFormatInfo.InvariantInfo, DateTimeStyles.AdjustToUniversal, out DateTime date))
|
|
{
|
|
return date;
|
|
}
|
|
else
|
|
{
|
|
_log.WarnFormat("{0} cannot be parsed as datetime", text);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public static bool IsTimeEmpty(this DateTime datetime)
|
|
{
|
|
return ((datetime.Hour == 0) && (datetime.Minute == 0) && (datetime.Second == 0));
|
|
}
|
|
|
|
public static double? TryParseDouble(string text)
|
|
{
|
|
if (Double.TryParse(text, NumberStyles.Any, NumberFormatInfo.InvariantInfo, out double d))
|
|
{
|
|
return d;
|
|
}
|
|
else
|
|
{
|
|
_log.WarnFormat("{0} cannot be parsed as double", text);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public static string ToDBHDateString(this DateTime source)
|
|
{
|
|
return source.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'");
|
|
}
|
|
|
|
public static List<string> RegexSplit(this string source)
|
|
{
|
|
List<string> result = new List<string>();
|
|
|
|
foreach(Match match in splitRegex.Matches(source))
|
|
{
|
|
result.Add(match.Value.Trim());
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
public static List<string> SimpleSplit(this string source)
|
|
{
|
|
List<string> result = new List<string>(source.Split(splitDelimiter, StringSplitOptions.RemoveEmptyEntries));
|
|
return result;
|
|
}
|
|
|
|
public static string Truncate(this string str, int length)
|
|
{
|
|
if (length < 0) throw new ArgumentOutOfRangeException("length", "length must be >= 0");
|
|
if (str == null) return null;
|
|
int maxLen = Math.Min(str.Length, length);
|
|
return str.Substring(0, maxLen);
|
|
}
|
|
|
|
/// <summary>
|
|
/// returns maximum size of following non-whitespace characters (aka size of "longest" word)
|
|
/// </summary>
|
|
public static int MaxLenNoWS(this string str)
|
|
{
|
|
int max = 0;
|
|
int currentMax = 0;
|
|
for(int i = 0; i < str.Length; i++)
|
|
{
|
|
if(Char.IsWhiteSpace(str[i]))
|
|
{
|
|
if (currentMax > max) max = currentMax;
|
|
currentMax = 0;
|
|
}
|
|
else
|
|
{
|
|
currentMax++;
|
|
}
|
|
}
|
|
if (currentMax > max) max = currentMax;
|
|
return max;
|
|
}
|
|
|
|
public static void BubbleSort(this IList o)
|
|
{
|
|
for (int i = o.Count - 1; i >= 0; i--)
|
|
{
|
|
for (int j = 1; j <= i; j++)
|
|
{
|
|
object o1 = o[j - 1];
|
|
object o2 = o[j];
|
|
if (((IComparable)o1).CompareTo(o2) > 0)
|
|
{
|
|
o.Remove(o1);
|
|
o.Insert(j, o1);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|