Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9f845ec

Browse files
committed
More changes by Jeffrey.
1 parent 23b8d4c commit 9f845ec

1 file changed

Lines changed: 51 additions & 23 deletions

File tree

Lib/re.py

Lines changed: 51 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -688,34 +688,62 @@ def expand_escape(pattern, index, context=NORMAL):
688688
return CHAR, 'D', index + 1
689689

690690
elif pattern[index] in '0123456789':
691-
end = index
692-
while (end < len(pattern)) and (pattern[end] in string.digits):
693-
end = end + 1
694-
value = pattern[index:end]
695691

696-
if (len(value) == 3) or ((len(value) == 2) and (value[0] == '0')):
697-
# octal character value
698-
value = string.atoi(value, 8)
699-
if value > 255:
700-
raise error, 'octal char out of range'
701-
return CHAR, chr(value), end
692+
if pattern[index] == '0':
693+
if (index + 1 < len(pattern)) and \
694+
(pattern[index + 1] in string.octdigits):
695+
if (index + 2 < len(pattern)) and \
696+
(pattern[index + 2] in string.octdigits):
697+
value = string.atoi(pattern[index:index + 3], 8)
698+
index = index + 3
699+
700+
else:
701+
value = string.atoi(pattern[index:index + 2], 8)
702+
index = index + 2
702703

703-
elif value == '0':
704-
return CHAR, chr(0), end
704+
else:
705+
value = 0
706+
index = index + 1
705707

706-
elif len(value) > 3:
707-
raise error, ('\\' + value + ' has too many digits')
708+
if value > 255:
709+
raise error, 'octal value out of range'
708710

711+
return CHAR, chr(value), index
712+
709713
else:
710-
# \1-\99 - reference a register
711-
if context == CHARCLASS:
712-
raise error, ('cannot reference a register from '
713-
'inside a character class')
714-
value = string.atoi(value)
715-
if value == 0:
716-
raise error, ('register 0 cannot be used '
717-
'during match')
718-
return MEMORY_REFERENCE, value, end
714+
if (index + 1 < len(pattern)) and \
715+
(pattern[index + 1] in string.digits):
716+
if (index + 2 < len(pattern)) and \
717+
(pattern[index + 2] in string.octdigits) and \
718+
(pattern[index + 1] in string.octdigits) and \
719+
(pattern[index] in string.octdigits):
720+
value = string.atoi(pattern[index:index + 3], 8)
721+
if value > 255:
722+
raise error, 'octal value out of range'
723+
724+
return CHAR, chr(value), index + 3
725+
726+
else:
727+
value = string.atoi(pattern[index:index + 2])
728+
if (value < 1) or (value > 99):
729+
raise error, 'memory reference out of range'
730+
731+
if context == CHARCLASS:
732+
raise error, ('cannot reference a register from '
733+
'inside a character class')
734+
return MEMORY_REFERENCE, value, index + 2
735+
736+
else:
737+
if context == CHARCLASS:
738+
raise error, ('cannot reference a register from '
739+
'inside a character class')
740+
741+
value = string.atoi(pattern[index])
742+
return MEMORY_REFERENCE, value, index + 1
743+
744+
while (end < len(pattern)) and (pattern[end] in string.digits):
745+
end = end + 1
746+
value = pattern[index:end]
719747

720748
else:
721749
return CHAR, pattern[index], index + 1

0 commit comments

Comments
 (0)