From f8c600df9158b19157f23d150e504c7fed26f7d9 Mon Sep 17 00:00:00 2001 From: Kaustav Das Modak Date: Mon, 21 Jan 2013 03:17:25 +0530 Subject: [PATCH] Implement validate.is_exact for exact search of needle in haystack --- pyavrophonetic/utils/validate.py | 5 +++++ tests/test_utils_validate.py | 7 +++++++ 2 files changed, 12 insertions(+) diff --git a/pyavrophonetic/utils/validate.py b/pyavrophonetic/utils/validate.py index 0b74d4b..6c88d90 100755 --- a/pyavrophonetic/utils/validate.py +++ b/pyavrophonetic/utils/validate.py @@ -66,6 +66,11 @@ def is_case_sensitive(text): else: return False +def is_exact(needle, haystack, start, end, matchnot): + """Check exact occurrence of needle in haystack""" + return (start >= 0 and end < len(haystack) and + (haystack[start:end] == needle) ^ matchnot) + def fix_string_case(text): """Converts case-insensitive characters to lower case diff --git a/tests/test_utils_validate.py b/tests/test_utils_validate.py index 82ecc4f..24916ab 100755 --- a/tests/test_utils_validate.py +++ b/tests/test_utils_validate.py @@ -100,3 +100,10 @@ def test_fix_string_case(self): # 'raMgoRurer Chana' should become 'ramgoRurer chana' self.assertEquals(validate.fix_string_case('raMgoRurer Chana'), 'ramgoRurer chana') + + def test_is_exact(self): + """Test exact search response of needle in haystack""" + self.assertTrue(validate.is_exact('abcd', 'abcdefgh', 0, 4, False)) + self.assertFalse(validate.is_exact('abcd', 'abcdefgh', 0, 4, True)) + self.assertFalse(validate.is_exact('bcd', 'abcdefgh', 0, 4, False)) + self.assertTrue(validate.is_exact('bcd', 'abcdefgh', 0, 4, True))