From 8e73c43d9a8730725fc8970a1daf9d8fdb67242b Mon Sep 17 00:00:00 2001 From: Alvin Wan Date: Sun, 19 Nov 2017 00:24:26 -0800 Subject: [PATCH] convert to str type --- TexSoup/utils.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/TexSoup/utils.py b/TexSoup/utils.py index 66fbb69..a8b0bc1 100644 --- a/TexSoup/utils.py +++ b/TexSoup/utils.py @@ -50,16 +50,24 @@ def wrap(*args, **kwargs): ######################### -class TokenWithPosition(object): - def __init__(self, text, position): +class TokenWithPosition(str): + """Enhanced string object with knowledge of global position.""" + + def __new__(cls, text, position): + """Initializer for pseudo-string object. + + :param text: The original string + :param position: Position in the original buffer + """ + self = str.__new__(cls, text) if isinstance(text, TokenWithPosition): self.text, self.position = text.text, text.position else: self.text = text self.position = position + return self def __repr__(self): -# return '{}:{}'.format(self.position, repr(self.text)) return repr(self.text) def __str__(self):