]> granicus.if.org Git - python/commitdiff
More changes by Jeffrey.
authorGuido van Rossum <guido@python.org>
Tue, 15 Jul 1997 18:11:42 +0000 (18:11 +0000)
committerGuido van Rossum <guido@python.org>
Tue, 15 Jul 1997 18:11:42 +0000 (18:11 +0000)
Lib/re.py

index 2e1b9205f07dd27515e17c1cec456a7d4fc84962..e9b20c5c86b301e790c0a2b56945c4f74a64a0cf 100644 (file)
--- a/Lib/re.py
+++ b/Lib/re.py
@@ -688,34 +688,62 @@ def expand_escape(pattern, index, context=NORMAL):
            return CHAR, 'D', index + 1
 
     elif pattern[index] in '0123456789':
-       end = index
-       while (end < len(pattern)) and (pattern[end] in string.digits):
-           end = end + 1
-       value = pattern[index:end]
 
-       if (len(value) == 3) or ((len(value) == 2) and (value[0] == '0')):
-           # octal character value
-           value = string.atoi(value, 8)
-           if value > 255:
-               raise error, 'octal char out of range'
-           return CHAR, chr(value), end
+       if pattern[index] == '0':
+           if (index + 1 < len(pattern)) and \
+              (pattern[index + 1] in string.octdigits):
+               if (index + 2 < len(pattern)) and \
+                  (pattern[index + 2] in string.octdigits):
+                   value = string.atoi(pattern[index:index + 3], 8)
+                   index = index + 3
+
+               else:
+                   value = string.atoi(pattern[index:index + 2], 8)
+                   index = index + 2
 
-       elif value == '0':
-           return CHAR, chr(0), end
+           else:
+               value = 0
+               index = index + 1
 
-       elif len(value) > 3:
-           raise error, ('\\' + value + ' has too many digits')
+           if value > 255:
+               raise error, 'octal value out of range'
 
+           return CHAR, chr(value), index
+       
        else:
-           # \1-\99 - reference a register
-           if context == CHARCLASS:
-               raise error, ('cannot reference a register from '
-                             'inside a character class')
-           value = string.atoi(value)
-           if value == 0:
-               raise error, ('register 0 cannot be used '
-                             'during match')
-           return MEMORY_REFERENCE, value, end
+           if (index + 1 < len(pattern)) and \
+              (pattern[index + 1] in string.digits):
+               if (index + 2 < len(pattern)) and \
+                  (pattern[index + 2] in string.octdigits) and \
+                  (pattern[index + 1] in string.octdigits) and \
+                  (pattern[index] in string.octdigits):
+                   value = string.atoi(pattern[index:index + 3], 8)
+                   if value > 255:
+                       raise error, 'octal value out of range'
+
+                   return CHAR, chr(value), index + 3
+
+               else:
+                   value = string.atoi(pattern[index:index + 2])
+                   if (value < 1) or (value > 99):
+                       raise error, 'memory reference out of range'
+
+                   if context == CHARCLASS:
+                       raise error, ('cannot reference a register from '
+                                     'inside a character class')
+                   return MEMORY_REFERENCE, value, index + 2
+
+           else:
+               if context == CHARCLASS:
+                   raise error, ('cannot reference a register from '
+                                 'inside a character class')
+
+               value = string.atoi(pattern[index])
+               return MEMORY_REFERENCE, value, index + 1
+           
+           while (end < len(pattern)) and (pattern[end] in string.digits):
+               end = end + 1
+           value = pattern[index:end]
 
     else:
        return CHAR, pattern[index], index + 1