-
Notifications
You must be signed in to change notification settings - Fork 7
/
HostAliases.cs
85 lines (72 loc) · 2.09 KB
/
HostAliases.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Hosts;
public class HostAliases : List<HostName>
{
public HostAliases() { }
public HostAliases(string line) { Set(line); }
public HostAliases(string[] hosts) { Set(hosts); }
public HostAliases(HostName host) { Set(host); }
public HostAliases(HostName[] hosts) { Set(hosts); }
public HostAliases(HostAliases hosts) { Set(hosts); }
public int Set(string line) { Clear(); return Add(line); }
public int Set(string[] hosts) { Clear(); return Add(hosts); }
public int Set(HostName host) { Clear(); return Add(host); }
public int Set(HostName[] hosts) { Clear(); return Add(hosts); }
public int Set(HostAliases hosts) { Clear(); return Add(hosts); }
public new int Add(HostName host)
{
if (Contains(host)) return 0;
RemoveAll(item => String.IsNullOrWhiteSpace(item));
base.Add(host);
return 1;
}
public int Add(HostName[] hosts)
{
int result = 0;
foreach (HostName host in hosts) result += Add(host);
return result;
}
public int Add(string line)
{
return Add(line.Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries));
}
public int Add(string[] hosts)
{
int result = 0;
foreach (string host in hosts) result += Add(new HostName(host));
return result;
}
public int Add(HostAliases hosts)
{
return Add(hosts.ToArray());
}
public override string ToString()
{
return ToString(true);
}
public string ToString(bool idn)
{
List<string> hosts = new List<string>(this.Count);
foreach (HostName hn in this) hosts.Add(idn ? hn.Unicode : hn.Ascii);
return String.Join(" ", hosts.ToArray());
}
public bool IsMatch(WildcardPattern pattern)
{
return this.Exists(item => pattern.IsMatch(item.Unicode));
}
public bool ContainsAny(List<HostName> hosts)
{
foreach (HostName host in hosts)
{
if (this.Contains(host)) { return true; }
}
return false;
}
public int RemoveAll(List<HostName> hosts)
{
return this.RemoveAll(host => hosts.Contains(host));
}
}