-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUtils.cs
127 lines (112 loc) · 3.86 KB
/
Utils.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
using System;
using System.Collections.Generic;
using MiscUtil;
using System.Text.RegularExpressions;
using System.Globalization;
namespace RevStack.Mvc
{
public static partial class Utils
{
/// <summary>
///
/// </summary>
/// <param name="msg"></param>
/// <returns></returns>
public static string StripNewLineChars(string msg)
{
msg = msg.Replace('\r', ' ');
msg = msg.Replace('\n', ' ');
return msg;
}
/// <summary>
///
/// </summary>
/// <param name="length"></param>
/// <returns></returns>
public static string GenerateRandomString(int length)
{
var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
return generateRandom(chars, length);
}
/// <summary>
///
/// </summary>
/// <param name="length"></param>
/// <returns></returns>
public static string GenerateRandomNumberString(int length)
{
var chars = "0123456789";
return generateRandom(chars, length);
}
/// <summary>
/// </summary>
/// <param name="source"></param>
/// <returns></returns>
public static bool IsNumeric(this string source)
{
return Regex.IsMatch(source, @"^\d+$");
}
/// <summary>
///
/// </summary>
/// <param name="strIn"></param>
/// <returns></returns>
public static bool IsValidEmail(string strIn)
{
if (String.IsNullOrEmpty(strIn))
return false;
// Use IdnMapping class to convert Unicode domain names.
try
{
strIn = Regex.Replace(strIn, @"(@)(.+)$", DomainMapper,
RegexOptions.None, TimeSpan.FromMilliseconds(200));
}
catch (RegexMatchTimeoutException)
{
return false;
}
// Return true if strIn is in valid e-mail format.
try
{
return Regex.IsMatch(strIn,
@"^(?("")(""[^""]+?""@)|(([0-9a-z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-z])@))" +
@"(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-z][-\w]*[0-9a-z]*\.)+[a-z0-9]{2,17}))$",
RegexOptions.IgnoreCase, TimeSpan.FromMilliseconds(250));
}
catch (RegexMatchTimeoutException)
{
return false;
}
}
/// <summary>
/// Uses EqualityComparer<T>.Default as an equality comparison for generics not constrained by a reference type
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="x"></param>
/// <param name="y"></param>
/// <returns></returns>
public static bool Compare<T>(T x, T y)
{
return EqualityComparer<T>.Default.Equals(x, y);
}
private static string DomainMapper(Match match)
{
// IdnMapping class with default property values.
IdnMapping idn = new IdnMapping();
string domainName = match.Groups[2].Value;
domainName = idn.GetAscii(domainName);
return match.Groups[1].Value + domainName;
}
private static string generateRandom(string chars, int length)
{
var stringChars = new char[length];
//var random = new Random();
for (int i = 0; i < stringChars.Length; i++)
{
stringChars[i] = chars[StaticRandom.Next(chars.Length)];
}
var finalString = new String(stringChars);
return finalString;
}
}
}