prevent tag replacements overlapping each other

This commit is contained in:
jesopo 2020-03-11 23:32:52 +00:00
parent a82105e09f
commit e523f03d4f
3 changed files with 14 additions and 8 deletions

View File

@ -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):

View File

@ -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"])

View File

@ -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")