Skip to content

Commit

Permalink
Another SearchParser update for an unlikely case
Browse files Browse the repository at this point in the history
  • Loading branch information
kohler committed Sep 23, 2024
1 parent fd41943 commit 9ff849b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/searchoperatorset.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,19 @@ function regex() {
$br[] = "[{$ch}]";
}
if (!empty($alnum)) {
$br[] = '(?:' . join("|", $alnum) . ')(?=[\s\(\)]|\z)';
$br[] = '(?:' . join("|", $alnum) . ')(?=[\s\(\)\[\]\{\}]|\z)';
}
$this->regex = '/\G(?:' . join("|", $br) . ')/s';
return $this->regex;
}

/** @param string $str
* @param int $pos
* @return bool */
static function safe_terminator($str, $pos = 0) {
return preg_match('/\G[\s\(\)\[\]\{\}]/', $str, $m, 0, $pos);
}


/** @return SearchOperatorSet */
static function simple_operators() {
Expand Down
8 changes: 8 additions & 0 deletions src/searchparser.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ function __construct($str, $pos1 = 0, $pos2 = null) {
$this->str = $str;
$this->pos = $pos1;
$this->len = $pos2 ?? strlen($str);

// unlikely: passed a substring that ends mid-word; need to be careful
if ($this->len < strlen($str)
&& !SearchOperatorSet::safe_terminator($str, $this->len)
&& ($this->len === 0 || !SearchOperatorSet::safe_terminator($str, $this->len - 1))) {
$this->str = substr($str, 0, $this->len);
}

$this->utf8q = strpos($str, chr(0xE2)) !== false && is_valid_utf8($str);
$this->set_span_and_pos(0);
}
Expand Down
12 changes: 12 additions & 0 deletions test/t_unit.php
Original file line number Diff line number Diff line change
Expand Up @@ -802,6 +802,18 @@ function test_span_balanced_parens() {
$sp = new SearchParser("a(b(c(d(e", 1, 5);
$x = $sp->shift_balanced_parens();
xassert_eqq($x, "(b(c");

$sp = new SearchParser("HIGHLIGHT:foobar");
$pe = $sp->parse_expression();
xassert_neqq($pe->op, null);
xassert_eqq($pe->op->type, "highlight");
xassert_eqq($pe->op->subtype, "foobar");

$sp = new SearchParser("HIGHLIGHT:foobar", 0, 11);
$pe = $sp->parse_expression();
xassert_neqq($pe->op, null);
xassert_eqq($pe->op->type, "highlight");
xassert_eqq($pe->op->subtype, "f");
}

function test_unpack_comparison() {
Expand Down

0 comments on commit 9ff849b

Please sign in to comment.