ip范围生成 C#

#region ip
/// <summary>
/// ip rang ,ip
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
bool Get_IPRangExist(string str, string ip)
{
bool val = false;
if (string.IsNullOrEmpty(str))
{
return val;
}
if (str.Contains(ip))
{
return true;
}
string[] sp = str.Split(new string[] { "|" }, StringSplitOptions.RemoveEmptyEntries);
if (sp.Length > 0)
{
foreach (var item in sp)
{

if (item.Contains("-"))
{
string[] sp2 = item.Split(new string[] { "-" }, StringSplitOptions.RemoveEmptyEntries);
if (sp2.Length == 2)
{
val = GetIPInfo2(sp2[0], sp2[1], ip);
if (val)
{
break;
}
}
}
else if (item.Contains("*")) {
string[] spsynctlist = item.Split(new string[]{"*"},StringSplitOptions.RemoveEmptyEntries);
foreach (var item_sp in spsynctlist)
{
if (ip.Contains(item_sp))
{
val = true; break;
}

}

}
if(item == ip)
{
val = true; break;
}
}
}
return val;
}


bool GetIPInfo2(string source, string des, string ip)
{
bool B_flag = false;
if (source == des)
{
return source == ip;
}


string[] p1 = source.Split(new char[] { '.' });
string[] p2 = des.Split(new char[] { '.' });
List<Tuple<int,int>> tupeIPList=new System.Collections.Generic.List<Tuple<int,int>>();

for (int i = 0; i < 4; i++)
{
string a = p1[i];string b = p2[i];
int start = Math.Min(int.Parse(a), int.Parse(b));
int end = Math.Max(int.Parse(a), int.Parse(b));
tupeIPList.Add(new Tuple<int, int>(start, end));
}
#region MyRegion
for (int a =tupeIPList[0].Item1; a <= tupeIPList[0].Item2; a++)
{
for (int b =tupeIPList[1].Item1; b <= tupeIPList[1].Item2; b++)
{
for (int c = tupeIPList[2].Item1; c <= tupeIPList[2].Item2; c++)
{
for (int d = tupeIPList[3].Item1; d <= tupeIPList[3].Item2;d++)
{
string template = string.Format("{0}.{1}.{2}.{3}",a,b,c,d);
if (template==ip)
{
B_flag = true;
break;
}
}
}
}
}
#endregion
return B_flag;

}


#endregion
public void test() {
Console.WriteLine("start");
List<Tuple<string, string>> a = new List<Tuple<string, string>>();
a.Add(new Tuple<string, string>("10.0.0.0-10.255.255.255", "10.19.48.27"));
a.Add(new Tuple<string, string>("10.*.*.*", "10.19.48.27"));
a.Add(new Tuple<string, string>("10.1.25.13-10.255.25.13", "10.19.48.27"));
a.Add(new Tuple<string, string>("10.1.25.13-10.255.25.13", "10.19.25.13"));
foreach (var item in a)
{
var start1 = System.Diagnostics.Stopwatch.StartNew(); start1.Start();
Console.WriteLine(Get_IPRangExist(item.Item1,item.Item2));
start1.Stop();
Console.WriteLine(item.Item1+" "+item.Item2+" ------- "+start1.ElapsedMilliseconds.ToString());
}
}

原文地址:https://www.cnblogs.com/window5549-accp/p/3954287.html