Repository: apple/pkl
File: pkl-core/src/main/java/org/pkl/core/util/IoUtils.java (Java)

Code before:
    }
  }

  /** Checks whether the given string is "URI-like", i.e. matches a pattern like {@code foo:bar}. */
  public static boolean isUriLike(String str) {
    return uriLike.matcher(str).matches();
  }

  public static boolean isWindowsAbsolutePath(String str) {
    if (!isWindows()) return false;
    return windowsPathLike.matcher(str).matches();
  }

  /**
   * Converts the given string to a {@link URI}. This method MUST be used for constructing module
   * and resource URIs. Unlike {@code new URI(str)}, it correctly escapes paths of relative URIs.
   */
  public static URI toUri(String str) throws URISyntaxException {
    if (isUriLike(str)) {
      return new URI(str);
    }
    return new URI(null, null, str, null);
  }

  /** Converts a URI to a Path, normalizing any non-ASCII characters. */
  public static Path pathOfURI(URI uri) {
    // Path.of(URI) throws on non-ASCII characters so the module URI here must be normalized to
    // ASCII
    // Unfortunately there's no way to go from URI -> ASCII URI directly
    // so this must transform URI -> ASCII String -> ASCII URI
    try {
      return Path.of(new URI(uri.toASCIIString()));
    } catch (URISyntaxException e) {
      // impossible to get here; we started from a valid URI to begin with
      throw PklBugException.unreachableCode();
    }
  }

  /** Like {@link #toUri(String)}, except without checked exceptions. */
  public static URI createUri(String str) {
    try {
      return toUri(str);
    } catch (URISyntaxException e) {
      throw new IllegalArgumentException(e.getMessage(), e);
    }
  }

  public static URI stripFragment(URI uri) {
    try {
      return new URI(
          uri.getScheme(),

Review comment (suggestion):
```suggestion
  public static Path pathOfUri(URI uri) {
```

(I think `pathOf` would be even better.)

Code after:
    }
  }

  /** Checks whether the given string is "URI-like", i.e. matches a pattern like {@code foo:bar}. */
  public static boolean isUriLike(String str) {
    return uriLike.matcher(str).matches();
  }

  public static boolean isWindowsAbsolutePath(String str) {
    if (!isWindows()) return false;
    return windowsPathLike.matcher(str).matches();
  }

  /**
   * Converts the given string to a {@link URI}. This method MUST be used for constructing module
   * and resource URIs. Unlike {@code new URI(str)}, it correctly escapes paths of relative URIs.
   */
  public static URI toUri(String str) throws URISyntaxException {
    if (isUriLike(str)) {
      return new URI(str);
    }
    return new URI(null, null, str, null);
  }

  /** Converts a URI to a Path, normalizing any non-ASCII characters. */
  public static Path pathOf(URI uri) {
    // Path.of(URI) throws on non-ASCII characters so the module URI here must be normalized to
    // ASCII
    // Unfortunately there's no way to go from URI -> ASCII URI directly
    // so this must transform URI -> ASCII String -> ASCII URI
    try {
      return Path.of(new URI(uri.toASCIIString()));
    } catch (URISyntaxException e) {
      // impossible to get here; we started from a valid URI to begin with
      throw PklBugException.unreachableCode();
    }
  }

  /** Like {@link #toUri(String)}, except without checked exceptions. */
  public static URI createUri(String str) {
    try {
      return toUri(str);
    } catch (URISyntaxException e) {
      throw new IllegalArgumentException(e.getMessage(), e);
    }
  }

  public static URI stripFragment(URI uri) {
    try {
      return new URI(
          uri.getScheme(),