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))