PortRange.parse constructor

PortRange.parse(
  1. String spec
)

Parses a start-end (or single port) string, e.g. 20000-20100.

Throws a FormatException if spec is not a number/range, or if the range is invalid (ports outside 1..65535, or end < start). The bounds are checked explicitly because the constructor's asserts are disabled in release/AOT builds (such as a compiled CLI).

Implementation

factory PortRange.parse(String spec) {
  final dash = spec.indexOf('-');
  final int start;
  final int end;
  if (dash < 0) {
    start = end = int.parse(spec.trim());
  } else {
    start = int.parse(spec.substring(0, dash).trim());
    end = int.parse(spec.substring(dash + 1).trim());
  }
  if (start < 1 || start > 65535 || end < start || end > 65535) {
    throw FormatException(
      'Invalid port range (expected 1-65535 and start <= end)',
      spec,
    );
  }
  return PortRange(start, end);
}