int rootLength()

in lib/src/style/url.dart [47:73]


  int rootLength(String path, {bool withDrive = false}) {
    if (path.isEmpty) return 0;
    if (isSeparator(path.codeUnitAt(0))) return 1;

    for (var i = 0; i < path.length; i++) {
      final codeUnit = path.codeUnitAt(i);
      if (isSeparator(codeUnit)) return 0;
      if (codeUnit == chars.colon) {
        if (i == 0) return 0;

        // The root part is up until the next '/', or the full path. Skip ':'
        // (and '//' if it exists) and search for '/' after that.
        if (path.startsWith('//', i + 1)) i += 3;
        final index = path.indexOf('/', i);
        if (index <= 0) return path.length;

        // file: URLs sometimes consider Windows drive letters part of the root.
        // See https://url.spec.whatwg.org/#file-slash-state.
        if (!withDrive || path.length < index + 3) return index;
        if (!path.startsWith('file://')) return index;
        if (!isDriveLetter(path, index + 1)) return index;
        return path.length == index + 3 ? index + 3 : index + 4;
      }
    }

    return 0;
  }