private static function consumePath()

in src/inlines/AutoLinkExtension.php [95:152]


  private static function consumePath(
    string $markdown,
    int $offset,
  ): (string, int) {
    $rest = Str\slice($markdown, $offset);
    $matches = Regex\first_match($rest, re"/^[^[\]<> ]+/");
    $match = $matches[0] ?? '';
    if ($match === '') {
      return tuple('', $offset);
    }

    $len = Str\length($match);
    $last = $match[$len - 1];

    if (C\contains_key(self::TRAILING_PUNCTUATION, $last)) {
      $match = Str\slice($match, 0, $len - 1);
      if ($match === '') {
        return tuple('', $offset);
      }
      $len--;
      $last = $match[$len - 1];
    }

    if ($last === ')') {
      $chars = Str\split($match, '');
      $opens = Vec\filter($chars, $c ==> $c === '(') |> C\count($$);
      $closes = Vec\filter($chars, $c ==> $c === ')') |> C\count($$);
      if ($closes > $opens) {
        $match = Str\slice($match, 0, $len - 1);
        if ($match === '') {
          return tuple('', $offset);
        }
        $len--;
        $last = $match[$len - 1];
      }
    }

    if ($last === ';') {
      $idx = Str\search_last($match, '&');
      if ($idx !== null) {
        $slice = Str\slice(
          $match,
          $idx + 1,
          $len -  ($idx + 2),
        );
        if (\ctype_alnum($slice)) {
          $match = Str\slice($match, 0, $idx);
          if ($match === '') {
            return tuple('', $offset);
          }
          $len = $idx;
          $last = $match[$len - 1];
        }
      }
    }

    return tuple($match, $offset + $len);
  }