Skip to content

Commit

Permalink
Add ignore_case option to String pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
Toilal committed Oct 18, 2015
1 parent d214392 commit 7a9a7b8
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
9 changes: 8 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,20 @@ If multiple ``Match`` objects are found at the same position, only the longer on
String Patterns
---------------
String patterns are based on `str.find`_ method to find matches, but returns all matches in the string.
String patterns are based on `str.find`_ method to find matches, but returns all matches in the string. ``ignore_case``
can be enabled to ignore case.

.. code-block:: python
>>> Rebulk().string('la').matches("lalalilala")
[<la:(0, 2)>, <la:(2, 4)>, <la:(6, 8)>, <la:(8, 10)>]
>>> Rebulk().string('la').matches("LalAlilAla")
[<la:(8, 10)>]
>>> Rebulk().string('la', ignore_case=True).matches("LalAlilAla")
[<La:(0, 2)>, <lA:(2, 4)>, <lA:(6, 8)>, <la:(8, 10)>]
You can define several patterns with a single ``string`` method call.

.. code-block:: python
Expand Down
13 changes: 13 additions & 0 deletions rebulk/test/test_pattern.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,19 @@ def test_single(self):
assert matches[0].span == (28, 34)
assert matches[0].value == "Celtic"

def test_ignore_case(self):
pattern = StringPattern("celtic", label="test", ignore_case=False)

matches = list(pattern.matches(self.input_string))
assert len(matches) == 0

pattern = StringPattern("celtic", label="test", ignore_case=True)

matches = list(pattern.matches(self.input_string))
assert len(matches) == 1
assert matches[0].value == "Celtic"


def test_no_match(self):
pattern = StringPattern("Python")

Expand Down
5 changes: 4 additions & 1 deletion rebulk/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from types import GeneratorType


def find_all(string, sub, start=None, end=None):
def find_all(string, sub, start=None, end=None, ignore_case=False):
"""
Return all indices in string s where substring sub is
found, such that sub is contained in the slice s[start:end].
Expand Down Expand Up @@ -39,6 +39,9 @@ def find_all(string, sub, start=None, end=None):
:return: all indices in the input string
:rtype: __generator[str]
"""
if ignore_case:
sub = sub.lower()
string = string.lower()
while True:
start = string.find(sub, start, end)
if start == -1:
Expand Down

0 comments on commit 7a9a7b8

Please sign in to comment.