Map parse()

in lib/src/packages_file.dart [32:96]


Map<String, Uri> parse(List<int> source, Uri baseLocation,
    {bool allowDefaultPackage = false}) {
  var index = 0;
  var result = <String, Uri>{};
  while (index < source.length) {
    var isComment = false;
    var start = index;
    var separatorIndex = -1;
    var end = source.length;
    var char = source[index++];
    if (char == $cr || char == $lf) {
      continue;
    }
    if (char == $colon) {
      if (!allowDefaultPackage) {
        throw FormatException("Missing package name", source, index - 1);
      }
      separatorIndex = index - 1;
    }
    isComment = char == $hash;
    while (index < source.length) {
      char = source[index++];
      if (char == $colon && separatorIndex < 0) {
        separatorIndex = index - 1;
      } else if (char == $cr || char == $lf) {
        end = index - 1;
        break;
      }
    }
    if (isComment) continue;
    if (separatorIndex < 0) {
      throw FormatException("No ':' on line", source, index - 1);
    }
    var packageName = String.fromCharCodes(source, start, separatorIndex);
    if (packageName.isEmpty
        ? !allowDefaultPackage
        : !isValidPackageName(packageName)) {
      throw FormatException("Not a valid package name", packageName, 0);
    }
    var packageValue = String.fromCharCodes(source, separatorIndex + 1, end);
    Uri packageLocation;
    if (packageName.isEmpty) {
      if (!isValidPackageName(packageValue)) {
        throw FormatException(
            "Default package entry value is not a valid package name");
      }
      packageLocation = Uri(path: packageValue);
    } else {
      packageLocation = baseLocation.resolve(packageValue);
      if (!packageLocation.path.endsWith('/')) {
        packageLocation =
            packageLocation.replace(path: packageLocation.path + "/");
      }
    }
    if (result.containsKey(packageName)) {
      if (packageName.isEmpty) {
        throw FormatException(
            "More than one default package entry", source, start);
      }
      throw FormatException("Same package name occured twice", source, start);
    }
    result[packageName] = packageLocation;
  }
  return result;
}