diff options
Diffstat (limited to 'deluge/config.py')
-rw-r--r-- | deluge/config.py | 43 |
1 files changed, 19 insertions, 24 deletions
diff --git a/deluge/config.py b/deluge/config.py index c85299682..90944a38f 100644 --- a/deluge/config.py +++ b/deluge/config.py @@ -74,38 +74,33 @@ def prop(func): return property(doc=func.__doc__, **func()) -def find_json_objects(s): - """Find json objects in a string. +def find_json_objects(text, decoder=json.JSONDecoder()): + """Find json objects in text. Args: - s (str): the string to find json objects in + text (str): The text to find json objects within. Returns: list: A list of tuples containing start and end locations of json - objects in string `s`. e.g. [(start, end), ...] + objects in the text. e.g. [(start, end), ...] + """ objects = [] - opens = 0 - start = s.find('{') - offset = start - - if start < 0: - return [] - - quoted = False - for index, c in enumerate(s[offset:]): - if c == '"': - quoted = not quoted - elif quoted: - continue - elif c == '{': - opens += 1 - elif c == '}': - opens -= 1 - if opens == 0: - objects.append((start, index + offset + 1)) - start = index + offset + 1 + offset = 0 + while True: + try: + start = text.index('{', offset) + except ValueError: + break + + try: + __, index = decoder.raw_decode(text[start:]) + except json.decoder.JSONDecodeError: + offset = start + 1 + else: + offset = start + index + objects.append((start, offset)) return objects |