mirror of https://github.com/jesopo/irctokens
prevent tag replacements overlapping each other
This commit is contained in:
parent
a82105e09f
commit
e523f03d4f
|
@ -1,15 +1,17 @@
|
|||
from typing import Dict, List, Optional
|
||||
|
||||
TAG_ESCAPE = ["\\", " ", ";", "\r", "\n"]
|
||||
TAG_UNESCAPE = ["\\\\", "\s", "\:", r"\r", r"\n"]
|
||||
TAG_UNESCAPED = ["\\", " ", ";", "\r", "\n"]
|
||||
TAG_ESCAPED = ["\\\\", "\\s", "\\:", "\\r", "\\n"]
|
||||
|
||||
def _unescape_tag(value: str):
|
||||
for i, char in enumerate(TAG_UNESCAPE):
|
||||
value = value.replace(char, TAG_ESCAPE[i])
|
||||
return value
|
||||
parts = value.split("\\\\")
|
||||
for i, piece in enumerate(TAG_ESCAPED):
|
||||
for j, part in enumerate(parts):
|
||||
parts[j] = part.replace(piece, TAG_UNESCAPED[i])
|
||||
return "\\".join(parts)
|
||||
def _escape_tag(value: str):
|
||||
for i, char in enumerate(TAG_ESCAPE):
|
||||
value = value.replace(char, TAG_UNESCAPE[i])
|
||||
for i, char in enumerate(TAG_UNESCAPED):
|
||||
value = value.replace(char, TAG_ESCAPED[i])
|
||||
return value
|
||||
|
||||
class Hostmask(object):
|
||||
|
|
|
@ -5,7 +5,7 @@ class FormatTestTags(unittest.TestCase):
|
|||
def test(self):
|
||||
line = irctokens.format("PRIVMSG", ["#channel", "hello"],
|
||||
tags={"id": "\\" + " " + ";" + "\r\n"})
|
||||
self.assertEqual(line, r"@id=\\\s\:\r\n PRIVMSG #channel hello")
|
||||
self.assertEqual(line, "@id=\\\\\\s\\:\\r\\n PRIVMSG #channel hello")
|
||||
|
||||
def test_missing(self):
|
||||
line = irctokens.format("PRIVMSG", ["#channel", "hello"])
|
||||
|
|
|
@ -18,6 +18,10 @@ class TokenTestTags(unittest.TestCase):
|
|||
line = irctokens.tokenise(r"@id=1\\\:\r\n\s2 PRIVMSG #channel")
|
||||
self.assertEqual(line.tags["id"], "1\\;\r\n 2")
|
||||
|
||||
def test_overlap(self):
|
||||
line = irctokens.tokenise(r"@id=1\\\s\\s PRIVMSG #channel")
|
||||
self.assertEqual(line.tags["id"], "1\\ \\s")
|
||||
|
||||
class TokenTestSource(unittest.TestCase):
|
||||
def test_without_tags(self):
|
||||
line = irctokens.tokenise(":nick!user@host PRIVMSG #channel")
|
||||
|
|
Loading…
Reference in New Issue