PEPTool/AV-ToolV3/Helper.cs
2019-07-26 07:36:50 +02:00

141 lines
5.2 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PEP_Tool
{
public static class Helper
{
public static string RemoveSpecialCharacters(string str)
{
StringBuilder sb = new StringBuilder();
foreach (char c in str)
{
if ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || c == '.' || c == '_')
{
sb.Append(c);
}
}
return sb.ToString();
}
public static bool keyIsSpecialKey(System.Windows.Input.Key key)
{
var b = false;
if (key == System.Windows.Input.Key.Escape || key == System.Windows.Input.Key.Tab ||
key == System.Windows.Input.Key.CapsLock || key == System.Windows.Input.Key.LWin ||
key == System.Windows.Input.Key.Left || key == System.Windows.Input.Key.Up ||
key == System.Windows.Input.Key.Down || key == System.Windows.Input.Key.Right ||
key == System.Windows.Input.Key.End || key == System.Windows.Input.Key.Home ||
key == System.Windows.Input.Key.PageUp || key == System.Windows.Input.Key.PageDown ||
key == System.Windows.Input.Key.PrintScreen || key == System.Windows.Input.Key.Print ||
(key >= System.Windows.Input.Key.F1 && key <= System.Windows.Input.Key.LaunchApplication2) ||
key == System.Windows.Input.Key.System || key == System.Windows.Input.Key.Play)
b = true;
else
b = false; // the key will sappressed
return b;
}
public static bool isTelNumber(System.Windows.Input.Key Char)
{
return ((Char >= System.Windows.Input.Key.D0 && Char <= System.Windows.Input.Key.D9) ||
(Char >= System.Windows.Input.Key.NumPad0 && Char <= System.Windows.Input.Key.NumPad9) ||
(Char == System.Windows.Input.Key.Delete ||
Char == System.Windows.Input.Key.Divide ||
Char == System.Windows.Input.Key.Space ||
Char == System.Windows.Input.Key.Back ||
Char == System.Windows.Input.Key.Add));
}
public static bool IsBase64String(this string s)
{
s = s.Trim();
return (s.Length % 4 == 0) && System.Text.RegularExpressions.Regex.IsMatch(s, @"^[a-zA-Z0-9\+/]*={0,3}$", System.Text.RegularExpressions.RegexOptions.None);
}
public static T FindVisualChild<T>(System.Windows.DependencyObject depObj) where T : System.Windows.DependencyObject
{
if (depObj != null)
{
for (int i = 0; i < System.Windows.Media.VisualTreeHelper.GetChildrenCount(depObj); i++)
{
System.Windows.DependencyObject child = System.Windows.Media.VisualTreeHelper.GetChild(depObj, i);
if (child != null && child is T)
{
return (T)child;
}
T childItem = FindVisualChild<T>(child);
if (childItem != null) return childItem;
}
}
return null;
}
public static int IndexOfNth(this string str, string value, int nth = 1)
{
if (nth <= 0)
throw new ArgumentException("Can not find the zeroth index of substring in string. Must start with 1");
int offset = str.IndexOf(value);
for (int i = 1; i < nth; i++)
{
if (offset == -1) return -1;
offset = str.IndexOf(value, offset + 1);
}
return offset;
}
public static int CountSubstring(this string text, string value)
{
int count = 0, minIndex = text.IndexOf(value, 0);
while (minIndex != -1)
{
minIndex = text.IndexOf(value, minIndex + value.Length);
count++;
}
return count;
}
public static string AddLineNumbers(this string text, int startLine = 1)
{
var i = text.CountSubstring("\n");
var strArr = text.Split("\n".ToCharArray());
for(var j = startLine; j <= i; j++)
{
strArr[j] = $"{j - startLine + 1}. " + strArr[j].TrimStart("1234567890. ".ToCharArray());
}
text = string.Join("\n", strArr);
return text;
}
}
public class ReplaceDotConverter : System.Windows.Data.IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value != null)
{
string original = value.ToString();
return original.Replace("l, ", "");
}
else return "";
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
}