using CommonUtil.Top;
using System;
using System.Linq;
using System.Text.RegularExpressions;
namespace CommonUtil
{
///
/// 扩展方法工具类
///
public static partial class ExtendUtil
{
private static readonly Regex REG_CIDR = new Regex("^(\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3})/(\\d{1,2})$");
///
/// 是否为null
///
/// 数据
/// bool
public static bool IsNull(this object data)
{
return data == null;
}
///
/// 是否为空或仅有空白字符组成
///
/// 数据
/// bool
public static bool IsNullOrEmpty(this string data)
{
return string.IsNullOrEmpty(data);
}
///
/// 是否为空或仅有空白字符组成
///
/// 数据
/// bool
public static bool IsNullOrWhiteSpace(this string data)
{
return string.IsNullOrWhiteSpace(data);
}
///
/// 是否为空,null也算作空
///
/// 数据
/// bool
public static bool IsNullOrEmpty(this Guid? data)
{
if (!data.HasValue)
{
return true;
}
return IsNullOrEmpty(data.Value);
}
///
/// 是否为空
///
/// 数据
/// bool
public static bool IsNullOrEmpty(this Guid data)
{
if (data == Guid.Empty)
{
return true;
}
return false;
}
//public static bool IsNumber(this string str)
//{
// string pattern = @"^[+-]?\d*[.]?\d*$";
// return Regex.IsMatch(str, pattern);
//}
///
/// 判断是否是正确的手机号码格式
///
/// 手机号码
/// bool
public static bool IsPhone(this string txtPhone)
{
string rule = @"^(1(([3457968][0-9])|(47)|[8][01236789]))\d{8}$";
return Regex.IsMatch(txtPhone, rule);
}
///
/// 判断是否是正确的邮箱格式
///
/// 邮箱号码
/// bool
public static bool IsEmail(this string txtEmail)
{
Regex rule = new Regex(@"^\s*([A-Za-z0-9_-]+(\.\w+)*@(\w+\.)+\w{2,5})\s*$");
return rule.IsMatch(txtEmail);
}
///
/// 验证是否为url
///
/// 数据
/// bool
public static bool IsUrl(this string str)
{
return Regex.IsMatch(str, @"^(http|https|ftp)\://[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(:[a-zA-Z0-9]*)?/?([a-zA-Z0-9\-\._\?\,\'/\\\+&$%\$#\=~])*$");
}
///
/// 验证是否为IP地址
///
/// 数据
/// bool
public static bool IsIP(this string str)
{
return Regex.IsMatch(str, @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$");
}
///
/// 验证ip是否在范围
///
///
///
///
public static bool IsIpInRange(string ipAddr, string cidrAddr)
{
Match match = REG_CIDR.Match(cidrAddr);
if (!match.Success)
{
throw new TopException("Invalid CIDR address: " + cidrAddr);
}
int[] minIpParts = new int[4];
int[] maxIpParts = new int[4];
string[] ipParts = match.Groups[1].Value.Split(new string[1] { "." }, StringSplitOptions.None);
int intMask = int.Parse(match.Groups[2].Value);
for (int i = 0; i < ipParts.Length; i++)
{
int ipPart = int.Parse(ipParts[i]);
if (intMask > 8)
{
minIpParts[i] = ipPart;
maxIpParts[i] = ipPart;
intMask -= 8;
}
else if (intMask > 0)
{
minIpParts[i] = ipPart >> intMask;
maxIpParts[i] = ipPart | (0xFF >> intMask);
intMask = 0;
}
else
{
minIpParts[i] = 1;
maxIpParts[i] = 0xFF - 1;
}
}
string[] realIpParts = ipAddr.Split(new string[1] { "." }, StringSplitOptions.None);
for (int i = 0; i < realIpParts.Length; i++)
{
int realIp = int.Parse(realIpParts[i]);
if (realIp < minIpParts[i] || realIp > maxIpParts[i])
{
return false;
}
}
return true;
}
///
/// 值在的范围?
///
/// 数据
/// 大于等于begin
/// 小于等于end
/// bool
public static bool IsInRange(this int thisValue, int begin, int end)
{
return thisValue >= begin && thisValue <= end;
}
///
/// 值在的范围?
///
/// 数据
/// 大于等于begin
/// 小于等于end
/// bool
public static bool IsInRange(this DateTime thisValue, DateTime begin, DateTime end)
{
return thisValue >= begin && thisValue <= end;
}
///
/// 长度在范围?
///
/// 数据
/// 大于等于最小长度
/// 小于等于最大长度
///
public static bool IsInRange(this string thisValue, long minLength, long maxLength)
{
return thisValue.Length >= minLength && thisValue.Length <= maxLength;
}
///
/// 在里面吗?
///
/// 泛型
/// 数据
/// 匹配范围
/// bool
public static bool IsContains(this T thisValue, params T[] inValues) where T : struct
{
return inValues.Contains(thisValue);
}
///
/// 在里面吗?
///
/// 数据
/// 匹配范围
/// bool
public static bool IsContains(this string thisValue, params string[] inValues)
{
return inValues.Any(it => thisValue.Contains(it));
}
///
/// 是否包含空白
///
/// 数据
///
public static bool IsContainsEmpty(this string thisValue)
{
if (thisValue.IsNullOrWhiteSpace()) return true;
return Regex.IsMatch(thisValue, @"\s+");
}
///
/// 是零?
///
/// 数据
/// bool
public static bool IsZero(this int thisValue)
{
return thisValue == 0;
}
///
/// 是零?
///
/// 数据
/// bool
public static bool IsZero(this decimal thisValue)
{
return thisValue == 0;
}
///
/// 是零?
///
/// 数据
/// bool
public static bool IsZero(this double thisValue)
{
return thisValue == 0;
}
///
/// 是零?
///
/// 数据
/// bool
public static bool IsZero(this float thisValue)
{
return thisValue == 0;
}
///
/// 是INT?
///
/// 数据
/// bool
public static bool IsInt(this string thisValue)
{
return Regex.IsMatch(thisValue, @"^\d+$");
}
///
/// 不是INT?
///
/// 数据
/// bool
public static bool IsNoInt(this string thisValue)
{
return !Regex.IsMatch(thisValue, @"^\d+$");
}
///
/// 是时间?
///
/// 数据
/// bool
public static bool IsDateTime(this string thisValue)
{
DateTime outValue = DateTime.MinValue;
return DateTime.TryParse(thisValue, out outValue);
}
///
/// 是身份证?
///
/// 数据
/// bool
public static bool IsIDcard(this string thisValue)
{
return Regex.IsMatch(thisValue, @"^(\d{15}$|^\d{18}$|^\d{17}(\d|X|x))$");
}
///
/// 是传真?
///
/// 数据
/// bool
public static bool IsFax(this string thisValue)
{
return Regex.IsMatch(thisValue, @"^[+]{0,1}(\d){1,3}[ ]?([-]?((\d)|[ ]){1,12})+$");
}
///
/// 是适合正则匹配?
///
/// 数据
/// 正则表达式
/// bool
public static bool IsMatch(this string thisValue, string pattern)
{
Regex reg = new Regex(pattern);
return reg.IsMatch(thisValue);
}
///
/// 是适合正则匹配?
///
/// 数据
/// 正则表达式
/// bool
public static bool IsMatch(this string thisValue, Regex reg)
{
return reg.IsMatch(thisValue);
}
}
}