Skip to content

Commit

Permalink
Normalize regex query patterns/values (fix beetbox#1482)
Browse files Browse the repository at this point in the history
  • Loading branch information
sampsyo authored and LordSputnik committed Jul 6, 2015
1 parent 941b613 commit e1d9170
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
11 changes: 10 additions & 1 deletion beets/dbcore/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from operator import mul
from beets import util
from datetime import datetime, timedelta
import unicodedata


class ParsingError(ValueError):
Expand Down Expand Up @@ -201,6 +202,7 @@ class RegexpQuery(StringFieldQuery):
"""
def __init__(self, field, pattern, fast=True):
super(RegexpQuery, self).__init__(field, pattern, fast)
pattern = self._normalize(pattern)
try:
self.pattern = re.compile(self.pattern)
except re.error as exc:
Expand All @@ -209,9 +211,16 @@ def __init__(self, field, pattern, fast=True):
"a regular expression",
format(exc))

@staticmethod
def _normalize(s):
"""Normalize a Unicode string's representation (used on both
patterns and matched values).
"""
return unicodedata.normalize('NFC', s)

@classmethod
def string_match(cls, pattern, value):
return pattern.search(value) is not None
return pattern.search(cls._normalize(value)) is not None


class BooleanQuery(MatchQuery):
Expand Down
2 changes: 2 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ Fixes:
:bug:`825`
* Fix some logging errors when filenames and other user-provided strings
contain curly braces. :bug:`1481`
* Regular expression queries over paths now work more reliably with non-ASCII
characters in filenames. :bug:`1482`


1.3.13 (April 24, 2015)
Expand Down

0 comments on commit e1d9170

Please sign in to comment.