public bool Contains(PortRange range) { if (this._specificLocalPort.HasValue) { return(this.Equals(range)); } if (range._isSinglePort) { return(range.Begin == this.Begin); } return(this.Begin <= range.Begin && this.End >= range.End); }
public static bool TryParse(string rangeString, out PortRange range) { range = null; if (string.IsNullOrWhiteSpace(rangeString)) { return(false); } var from = -1; var to = default(int); for (int i = 0; i < rangeString.Length; i++) { var c = rangeString[i]; if (c >= '0' && c <= '9') { to = to * 10 + (c - '0'); } else if (c == '-' && to >= 0 && from == -1) { from = to; to ^= to; } else if (char.IsWhiteSpace(c)) { continue; } else { if (TryParseSpecificLocalPort(rangeString, out var sp)) { range = new PortRange(sp.Value); return(true); } return(false); } } if (from == -1) { if (to > ushort.MaxValue || to < ushort.MinValue) { return(false); } range = new PortRange((ushort)to); } else { if (from > ushort.MaxValue || from < ushort.MinValue) { return(false); } if (to > ushort.MaxValue || to < ushort.MinValue) { return(false); } if (from > to) { from ^= to ^= from ^= to; } range = new PortRange((ushort)from, (ushort)to); } return(true); }