function consume_unbracketed_link_destination()

in src/_Private/consume_link_destination.php [56:104]


function consume_unbracketed_link_destination(
  string $input,
): ?(string, int) {
  $len = Str\length($input);

  $paren_depth = 0;
  $destination = '';
  for ($idx = 0; $idx < $len; ++$idx) {
    $chr = $input[$idx];
    if ($chr === ' ') {
      break;
    }
    if ($chr === "\n") {
      break;
    }

    if ($chr === '\\') {
      if ($idx + 1 < $len) {
        $next = $input[$idx + 1];
        if (C\contains_key(ASCII_PUNCTUATION, $next)) {
          $destination .= $next;
          $idx++;
          continue;
        }
      }
    }

    if ($chr === '(') {
      $destination .= $chr;
      $paren_depth++;
      continue;
    }
    if ($chr === ')') {
      if ($paren_depth === 0) {
        break;
      }
      $destination .= $chr;
      --$paren_depth;
      continue;
    }
    $destination .= $chr;
  }

  if ($destination === '') {
    return null;
  }

  return tuple($destination, $idx);
}