分类: Python/Ruby
2010-02-10 09:33:11
') where nextchars contains 0-2 chars
# and unprocessed may contain more escapes. Pre match part contains
# no escapes can is appended directly to result.
result.append(unprocessed[:res.start()])
escapes = res.group(1)
nextchars = res.group(2)
unprocessed = unprocessed[res.end():]
# Append every second escape char to result
result.append('\\' * (len(escapes) / 2))
# Handle '\n', '\r' and '\t'. Note that both '\n' and '\n ' are
# converted to '\n'
if len(escapes) % 2 == 0 or len(nextchars) == 0 \
or nextchars[0] not in ['n','r','t']:
result.append(nextchars)
elif nextchars[0] == 'n':
if len(nextchars) == 1 or nextchars[1] == ' ':
result.append('\n')
else:
result.append('\n' + nextchars[1])
elif nextchars[0] == 'r':
result.append('\r' + nextchars[1:])
else:
result.append('\t' + nextchars[1:])
return ''.join(result)
def escape_file_name(filename):
"""Escapes filename.
Use only with actual file name and not with full path because possible
'/' and '\\' in the given name are also escaped!
"""
return ''.join([ _escape_char(c) for c in filename ])
_ok_chars = string.ascii_letters + string.digits + '-+.'
_replaced_chars = { u'\xe4' : 'a', u'\xe5' : 'a',
u'\xc4' : 'A', u'\xc5' : 'A',
u'\xf6' : 'o', u'\xd6' : 'O',
u'\xfc' : 'u', u'\xdc' : 'U', }
def _escape_char(char):
if char in _ok_chars:
return char
elif _replaced_chars.has_key(char):
return _replaced_chars[char]
else:
return '_'
文件路径:robotframework-2.1.2\src\robot\utils\escaping.py
功能:关于转义函数的定义。
_escape_re = re.compile(r'(\\+)([^\\]{0,2})') 为用正则表达式来匹配转义序列,这里奇怪的是只抓取反斜杠和后面2个字符,需要以后查看转义的表示方法。
escape对特殊符号多加一次转义。
unescape为对转义的处理
escape_file_name以及后面的好像没有地方调用,暂时不用理睬