Skip to content

Commit f938af4

Browse files
committed
Merge pull request #32 from alpha0010/master
Improve ability of halfEscapeMatch() to fix potential negation and alternation errors
2 parents 771114e + 563b5a8 commit f938af4

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

src/SphinxQL.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1368,9 +1368,15 @@ public function halfEscapeMatch($string)
13681368
$string .= '"';
13691369
}
13701370

1371+
$string = preg_replace('/-[\s-]*-/u', '-', $string);
1372+
13711373
$from_to_preg = array(
1372-
"'\"([^\s]+)-([^\s]*)\"'" => "\\1\-\\2",
1373-
"'([^\s]+)-([^\s]*)'" => "\"\\1\-\\2\""
1374+
'/([-|])\s*$/u' => '\\\\\1',
1375+
'/\|[\s|]*\|/u' => '|',
1376+
1377+
// prevent accidental negation in natural language
1378+
'/(\S+)-(\S+)/u' => '\1\-\2',
1379+
'/(\S+)\s+-\s+(\S+)/u' => '\1 \- \2',
13741380
);
13751381

13761382
$string = mb_strtolower(preg_replace(array_keys($from_to_preg), array_values($from_to_preg), $string));

tests/classes/SphinxQLTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,7 @@ public function testEscapeMatch()
393393
public function testHalfEscapeMatch()
394394
{
395395
$this->assertSame('this maybe that\^32 and | hi', SphinxQL::create($this->conn)->halfEscapeMatch('this MAYBE that^32 and | hi'));
396+
$this->assertSame('this \- not -that | hi \-', SphinxQL::create($this->conn)->halfEscapeMatch('this -- not -that | | hi -'));
396397
$this->assertSame('stärkergradig | mb', SphinxQL::create($this->conn)->halfEscapeMatch('stärkergradig | mb'));
397398
}
398399

0 commit comments

Comments
 (0)