mirror of
https://github.com/Mikaela/Limnoria.git
synced 2025-03-03 21:10:40 +01:00
Removed some XXXes and allowed other separators than /.
This commit is contained in:
parent
5d37d71afb
commit
cc084d2535
29
src/utils.py
29
src/utils.py
@ -214,16 +214,33 @@ def quoted(s):
|
|||||||
"""Returns a quoted s."""
|
"""Returns a quoted s."""
|
||||||
return '"%s"' % s
|
return '"%s"' % s
|
||||||
|
|
||||||
nonEscapedSlashes = re.compile(r'(?<!\\)/')
|
def _getSep(s):
|
||||||
|
assert len(s) >= 2
|
||||||
|
if s.startswith('m') or s.startswith('s'):
|
||||||
|
separator = s[1]
|
||||||
|
else:
|
||||||
|
separator = s[0]
|
||||||
|
if separator.isalnum() or separator in '{}[]()<>':
|
||||||
|
raise ValueError, \
|
||||||
|
'Invalid separator: separator must not be alphanumeric or in ' \
|
||||||
|
'"{}[]()<>"'
|
||||||
|
return separator
|
||||||
|
|
||||||
|
def _getSplitterRe(s):
|
||||||
|
separator = _getSep(s)
|
||||||
|
return re.compile(r'(?<!\\)%s' % re.escape(separator))
|
||||||
|
|
||||||
def perlReToPythonRe(s):
|
def perlReToPythonRe(s):
|
||||||
"""Converts a string representation of a Perl regular expression (i.e.,
|
"""Converts a string representation of a Perl regular expression (i.e.,
|
||||||
m/^foo$/i or /foo|bar/) to a Python regular expression.
|
m/^foo$/i or /foo|bar/) to a Python regular expression.
|
||||||
"""
|
"""
|
||||||
|
sep = _getSep(s)
|
||||||
|
splitter = _getSplitterRe(s)
|
||||||
try:
|
try:
|
||||||
(kind, regexp, flags) = nonEscapedSlashes.split(s)
|
(kind, regexp, flags) = splitter.split(s)
|
||||||
except ValueError: # Unpack list of wrong size.
|
except ValueError: # Unpack list of wrong size.
|
||||||
raise ValueError, 'Must be of the form m/.../ or /.../'
|
raise ValueError, 'Must be of the form m/.../ or /.../'
|
||||||
regexp = regexp.replace('\\/', '/')
|
regexp = regexp.replace('\\'+sep, sep)
|
||||||
if kind not in ('', 'm'):
|
if kind not in ('', 'm'):
|
||||||
raise ValueError, 'Invalid kind: must be in ("", "m")'
|
raise ValueError, 'Invalid kind: must be in ("", "m")'
|
||||||
flag = 0
|
flag = 0
|
||||||
@ -242,12 +259,14 @@ def perlReToReplacer(s):
|
|||||||
s/foo/bar/g or s/foo/bar/i) to a Python function doing the equivalent
|
s/foo/bar/g or s/foo/bar/i) to a Python function doing the equivalent
|
||||||
replacement.
|
replacement.
|
||||||
"""
|
"""
|
||||||
|
sep = _getSep(s)
|
||||||
|
splitter = _getSplitterRe(s)
|
||||||
try:
|
try:
|
||||||
(kind, regexp, replace, flags) = nonEscapedSlashes.split(s)
|
(kind, regexp, replace, flags) = splitter.split(s)
|
||||||
except ValueError: # Unpack list of wrong size.
|
except ValueError: # Unpack list of wrong size.
|
||||||
raise ValueError, 'Must be of the form s/.../.../'
|
raise ValueError, 'Must be of the form s/.../.../'
|
||||||
regexp = regexp.replace('\x08', r'\b')
|
regexp = regexp.replace('\x08', r'\b')
|
||||||
replace = replace.replace('\\/', '/')
|
replace = replace.replace('\\'+sep, sep)
|
||||||
for i in xrange(10):
|
for i in xrange(10):
|
||||||
replace = replace.replace(chr(i), r'\%s' % i)
|
replace = replace.replace(chr(i), r'\%s' % i)
|
||||||
if kind != 's':
|
if kind != 's':
|
||||||
|
@ -177,6 +177,10 @@ class UtilsTest(SupyTestCase):
|
|||||||
self.failUnless(r.search('CAT'))
|
self.failUnless(r.search('CAT'))
|
||||||
self.assertRaises(ValueError, utils.perlReToPythonRe, 'm/?/')
|
self.assertRaises(ValueError, utils.perlReToPythonRe, 'm/?/')
|
||||||
|
|
||||||
|
def testP2PReDifferentSeparator(self):
|
||||||
|
r = utils.perlReToPythonRe('m!foo!')
|
||||||
|
self.failUnless(r.search('foo'))
|
||||||
|
|
||||||
def testPerlReToReplacer(self):
|
def testPerlReToReplacer(self):
|
||||||
f = utils.perlReToReplacer('s/foo/bar/')
|
f = utils.perlReToReplacer('s/foo/bar/')
|
||||||
self.assertEqual(f('foobarbaz'), 'barbarbaz')
|
self.assertEqual(f('foobarbaz'), 'barbarbaz')
|
||||||
@ -197,6 +201,10 @@ class UtilsTest(SupyTestCase):
|
|||||||
f = utils.perlReToReplacer('s/^/foo/')
|
f = utils.perlReToReplacer('s/^/foo/')
|
||||||
self.assertEqual(f('bar'), 'foobar')
|
self.assertEqual(f('bar'), 'foobar')
|
||||||
|
|
||||||
|
def testPReToReplacerDifferentSeparator(self):
|
||||||
|
f = utils.perlReToReplacer('s#foo#bar#')
|
||||||
|
self.assertEqual(f('foobarbaz'), 'barbarbaz')
|
||||||
|
|
||||||
def testPerlReToReplacerBug850931(self):
|
def testPerlReToReplacerBug850931(self):
|
||||||
f = utils.perlReToReplacer('s/\b(\w+)\b/\1./g')
|
f = utils.perlReToReplacer('s/\b(\w+)\b/\1./g')
|
||||||
self.assertEqual(f('foo bar baz'), 'foo. bar. baz.')
|
self.assertEqual(f('foo bar baz'), 'foo. bar. baz.')
|
||||||
|
Loading…
x
Reference in New Issue
Block a user