Skip to content

Commit

Permalink
Support escaping with quote
Browse files Browse the repository at this point in the history
  • Loading branch information
nanaya committed Nov 8, 2023
1 parent e6ce7e4 commit 08e8f56
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
14 changes: 11 additions & 3 deletions app/Libraries/Search/BeatmapsetQueryParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ public static function parse(?string $query): array
$options = [];

// reference: https://github.com/ppy/osu/blob/f6baf49ad6b42c662a729ad05e18bd99bc48b4c7/osu.Game/Screens/Select/FilterQueryParser.cs
$keywords = preg_replace_callback('#\b(?<key>\w+)(?<op>(:|=|(>|<)(:|=)?))(?<value>(".*")|(\S*))#i', function ($m) use (&$options) {
// (modified to allow escaping quotes)
$keywords = preg_replace_callback('#\b(?<key>\w+)(?<op>(:|=|(>|<)(:|=)?))(?<value>("(?:\\"|[^"])*")|(\S*))#i', function ($m) use (&$options) {
$key = strtolower($m['key']);
$op = str_replace(':', '=', $m['op']);
switch ($key) {
Expand Down Expand Up @@ -226,9 +227,16 @@ private static function makeIntRangeOption($operator, $value)

private static function makeTextOption($operator, $value)
{
if ($operator === '=') {
return presence(trim($value, '"'));
if ($operator !== '=') {
return null;
}

$len = strlen($value);
if ($len > 1 && $value[0] === '"' && $value[$len - 1] === '"') {
$value = substr($value, 1, $len - 2);
}

return presence(strtr($value, ['\"' => '"']));
}

private static function statePrefixSearch($value): ?int
Expand Down
1 change: 1 addition & 0 deletions tests/Libraries/Search/BeatmapsetQueryParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ public function queryDataProvider()
['find me songs by artist=singer please', ['keywords' => 'find me songs by please', 'options' => ['artist' => 'singer']]],
['really like artist="name with space" yes', ['keywords' => 'really like yes', 'options' => ['artist' => 'name with space']]],
['weird artist=double"quote', ['keywords' => 'weird', 'options' => ['artist' => 'double"quote']]],
['weird artist="escaped \"quote\"" thing', ['keywords' => 'weird thing', 'options' => ['artist' => 'escaped "quote"']]],
['artist=><something', ['keywords' => null, 'options' => ['artist' => '><something']]],
['unrecognised=keyword', ['keywords' => 'unrecognised=keyword', 'options' => []]],
['cs=nope', ['keywords' => 'cs=nope', 'options' => []]],
Expand Down

0 comments on commit 08e8f56

Please sign in to comment.