Skip to content

Commit

Permalink
Fix pattern normalization form.
Browse files Browse the repository at this point in the history
  • Loading branch information
oldnomad committed Oct 14, 2023
1 parent 672b7b4 commit 89e8103
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
12 changes: 8 additions & 4 deletions lib/Calibre/CalibreSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
* Search implementation.
*/
class CalibreSearch {
/**
* Default Unicode form used for search.
*/
private const DEFAULT_FORM = Normalizer::NFKC;

/**
* Construct an instance.
*
Expand All @@ -33,7 +38,7 @@ private function __construct(private string $pattern) {
private static function removeDiacritics(string $text): string {
$text = normalizer_normalize($text, Normalizer::NFD);
$text = preg_replace('/[\p{M}]/u', '', $text);
return normalizer_normalize($text, Normalizer::NFKC);
return normalizer_normalize($text, self::DEFAULT_FORM);
}

/**
Expand All @@ -48,7 +53,7 @@ private static function appendHaystack(array &$haystack, mixed $text): void {
if (!is_string($text) || $text === '') {
return;
}
$text = normalizer_normalize($text, Normalizer::NFKC);
$text = normalizer_normalize($text, self::DEFAULT_FORM);
array_push($haystack, $text);
$textNoMarks = self::removeDiacritics($text);
if ($textNoMarks !== $text) {
Expand Down Expand Up @@ -94,9 +99,8 @@ private static function createPattern(string $terms): ?string {
if ($terms === '') {
return null;
}
/* @var string */
$dataEsc = str_replace('/', '\/', $terms);
return '/'.$dataEsc.'/inS';
return '/'.normalizer_normalize($dataEsc, self::DEFAULT_FORM).'/inS';
}

/**
Expand Down
10 changes: 10 additions & 0 deletions tests/unit/SearchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,16 @@ public function testSimple(): void {
...self::BOOK_DEFAULTS,
'title' => 'This is a Símple Tést',
])), 'Simple search in title -- diacritics-insensitive');
$filter = CalibreSearch::searchBooks(normalizer_normalize('símple', Normalizer::NFC));
$this->assertTrue(call_user_func($filter, $this->createCalibreItem(CalibreBook::class, $this->db, [
...self::BOOK_DEFAULTS,
'title' => 'This is a Símple Tést',
])), 'Simple search in title -- diacritics-insensitive (NFC)');
$filter = CalibreSearch::searchBooks(normalizer_normalize('símple', Normalizer::NFD));
$this->assertTrue(call_user_func($filter, $this->createCalibreItem(CalibreBook::class, $this->db, [
...self::BOOK_DEFAULTS,
'title' => 'This is a Símple Tést',
])), 'Simple search in title -- diacritics-insensitive (NFD)');
}

public function testRegex(): void {
Expand Down

0 comments on commit 89e8103

Please sign in to comment.