From 5494ec571f08fced702c334c6cafacd628ca2786 Mon Sep 17 00:00:00 2001 From: Cnly Date: Sun, 19 Dec 2021 15:12:27 +0800 Subject: [PATCH 1/3] Improve evaluation experience for large values --- autoload/iced/nrepl.vim | 2 +- message/iced/en.txt | 4 +- python/bencode.py | 111 +++++++++++++++++++--------------------- 3 files changed, 56 insertions(+), 61 deletions(-) diff --git a/autoload/iced/nrepl.vim b/autoload/iced/nrepl.vim index 0923ac84d..935b95939 100644 --- a/autoload/iced/nrepl.vim +++ b/autoload/iced/nrepl.vim @@ -249,7 +249,7 @@ function! s:dispatcher(ch, resp) abort "" To avoid freezing vim/nvim when too large values are returned. "" c.f. https://github.com/liquidz/vim-iced/issues/219 if text_len > g:iced#nrepl#buffer_size - call iced#message#error('too_large_values') + call iced#message#warning('too_large_values') if g:iced#nrepl#skip_evaluation_when_buffer_size_is_exceeded let s:response_buffer = '' let s:messages = {} diff --git a/message/iced/en.txt b/message/iced/en.txt index f96273332..55f5ee7ae 100644 --- a/message/iced/en.txt +++ b/message/iced/en.txt @@ -103,7 +103,7 @@ 'timeout': 'Timed out.', 'toggle_warn_on_reflection': 'Toggled *warn-on-reflection* to %s.', 'too_deep_to_slurp': 'Too deep to slurp.', - 'too_large_values': 'Timed out because evaluated values are too large.', + 'too_large_values': 'Evaluating values too large may freeze vim. See: g:iced#nrepl#skip_evaluation_when_buffer_size_is_exceeded', 'try_connect': 'Not connected. Try `:IcedConnect `', 'unaliased': 'Unaliased "%s".', 'undefined': 'Undefined %s.', @@ -117,4 +117,4 @@ '___EOF___': '' } -#vim:ft=ruby +# vim:ft=ruby diff --git a/python/bencode.py b/python/bencode.py index 569fff847..89cda3075 100644 --- a/python/bencode.py +++ b/python/bencode.py @@ -1,76 +1,72 @@ #!/usr/bin/python3 -def __decode_string(s): +def __decode_string(b, start=0): """ - >>> __decode_string('3:foo3:bar') - {'value': 'foo', 'rest': '3:bar'} - >>> __decode_string('0:3:bar') - {'value': '', 'rest': '3:bar'} + >>> __decode_string(b'3:foo3:bar') + {'value': 'foo', 'start': 5} + >>> __decode_string(b'0:3:bar') + {'value': '', 'start': 2} """ - i = s.find(':') - if i == -1: - raise Exception('failed to decode string token', s) - else: - l = int(s[:i]) - b = s[i+1:].encode() - return {'value': b[:l].decode(encoding='utf-8'), 'rest': b[l:].decode(encoding='utf-8')} + cpos = b.find(b':', start) + if cpos == -1: + raise Exception('failed to decode string token', b[start:]) + l = int(b[start:cpos]) + return {'value': b[cpos+1:cpos+1+l].decode(encoding='utf-8'), 'start': cpos+1+l} -def __decode_integer(s): +def __decode_integer(b, start=0): """ - >>> __decode_integer('i123e3:foo') - {'value': 123, 'rest': '3:foo'} + >>> __decode_integer(b'i123e3:foo') + {'value': 123, 'start': 5} """ - i = s.find('e') - if i == -1: + epos = b.find(b'e', start) + if epos == -1: raise Exception('failed to decode integer token') - else: - return {'value': int(s[1:i]), 'rest': s[i+1:]} + return {'value': int(b[start+1:epos]), 'start': epos+1} -def __decode_list(s): +def __decode_list(b, start=0): """ - >>> __decode_list('li123e3:foo0:e3:bar') - {'value': [123, 'foo', ''], 'rest': '3:bar'} - >>> __decode_list('le3:bar') - {'value': [], 'rest': '3:bar'} - >>> __decode_list('llee3:bar') - {'value': [[]], 'rest': '3:bar'} + >>> __decode_list(b'li123e3:foo0:e3:bar') + {'value': [123, 'foo', ''], 'start': 14} + >>> __decode_list(b'le3:bar') + {'value': [], 'start': 2} + >>> __decode_list(b'llee3:bar') + {'value': [[]], 'start': 4} """ - rest = s[1:] + start += 1 result = [] - while rest[0] != 'e': - decoded = __decode(rest) + while chr(b[start]) != 'e': + decoded = __decode(b, start) result.append(decoded['value']) - rest = decoded['rest'] - return {'value': result, 'rest': rest[1:]} + start = decoded['start'] + return {'value': result, 'start': start+1} -def __decode_dict(s): +def __decode_dict(b, start=0): """ - >>> __decode_dict('d3:fooi123ee3:bar') - {'value': {'foo': 123}, 'rest': '3:bar'} - >>> __decode_dict('de3:bar') - {'value': {}, 'rest': '3:bar'} + >>> __decode_dict(b'd3:fooi123ee3:bar') + {'value': {'foo': 123}, 'start': 12} + >>> __decode_dict(b'de3:bar') + {'value': {}, 'start': 2} """ - rest = s[1:] + start += 1 result = {} - while rest[0] != 'e': - k = __decode(rest) - v = __decode(k['rest']) + while chr(b[start]) != 'e': + k = __decode(b, start) + v = __decode(b, k['start']) result[k['value']] = v['value'] - rest = v['rest'] - return {'value': result, 'rest': rest[1:]} + start = v['start'] + return {'value': result, 'start': start+1} -def __decode(s): - c = s[0] +def __decode(b, start=0): + c = chr(b[start]) if c in {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}: - return __decode_string(s) + return __decode_string(b, start) elif c == 'i': - return __decode_integer(s) + return __decode_integer(b, start) elif c == 'l': - return __decode_list(s) + return __decode_list(b, start) elif c == 'd': - return __decode_dict(s) - else: - raise Exception('invalid token') + return __decode_dict(b, start) + raise Exception('invalid token ' + c) def iced_bencode_decode(s): """ @@ -92,16 +88,16 @@ def iced_bencode_decode(s): '__FAILED__' """ result = [] - rest = s + s = s.encode('utf-8') + start = 0 try: - while rest != '': - ret = __decode(rest) + while start < len(s): + ret = __decode(s, start) result.append(ret['value']) - rest = ret['rest'] + start = ret['start'] if len(result) == 1: return result[0] - else: - return result + return result except Exception: return '__FAILED__' @@ -135,5 +131,4 @@ def iced_vim_repr(x): elif t is dict: ret = [iced_vim_repr(k) + ':' + iced_vim_repr(v) for k,v in x.items()] return '{' + ','.join(ret) + '}' - else: - raise Exception + raise Exception From 7310173de59bdb47dfa8d422d9210b6bd39043ef Mon Sep 17 00:00:00 2001 From: Cnly Date: Tue, 21 Dec 2021 10:54:14 +0800 Subject: [PATCH 2/3] Improve docs and naming --- python/bencode.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/python/bencode.py b/python/bencode.py index 89cda3075..6e2f18ff3 100644 --- a/python/bencode.py +++ b/python/bencode.py @@ -4,6 +4,8 @@ def __decode_string(b, start=0): """ >>> __decode_string(b'3:foo3:bar') {'value': 'foo', 'start': 5} + >> __decode_string(b'3:foo3:bar', start=5) + {'value': 'bar', 'start': 10} >>> __decode_string(b'0:3:bar') {'value': '', 'start': 2} """ @@ -17,6 +19,8 @@ def __decode_integer(b, start=0): """ >>> __decode_integer(b'i123e3:foo') {'value': 123, 'start': 5} + >>> __decode_integer(b'i123ei4567e', start=5) + {'value': 4567, 'start': 11} """ epos = b.find(b'e', start) if epos == -1: @@ -27,8 +31,10 @@ def __decode_list(b, start=0): """ >>> __decode_list(b'li123e3:foo0:e3:bar') {'value': [123, 'foo', ''], 'start': 14} - >>> __decode_list(b'le3:bar') + >>> __decode_list(b'lel3:bare') {'value': [], 'start': 2} + >>> __decode_list(b'lel3:bare', start=2) + {'value': ['bar'], 'start': 9} >>> __decode_list(b'llee3:bar') {'value': [[]], 'start': 4} """ @@ -46,6 +52,8 @@ def __decode_dict(b, start=0): {'value': {'foo': 123}, 'start': 12} >>> __decode_dict(b'de3:bar') {'value': {}, 'start': 2} + >>> __decode_dict(b'ded3:bari1ee', start=2) + {'value': {'bar': 1}, 'start': 12} """ start += 1 result = {} @@ -88,11 +96,11 @@ def iced_bencode_decode(s): '__FAILED__' """ result = [] - s = s.encode('utf-8') + b = s.encode('utf-8') start = 0 try: - while start < len(s): - ret = __decode(s, start) + while start < len(b): + ret = __decode(b, start) result.append(ret['value']) start = ret['start'] if len(result) == 1: From dd9c3234e460f467c38146469f6a11c213eb1d2c Mon Sep 17 00:00:00 2001 From: Cnly Date: Tue, 21 Dec 2021 10:55:52 +0800 Subject: [PATCH 3/3] Fix typo --- python/bencode.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/bencode.py b/python/bencode.py index 6e2f18ff3..27f7f9af8 100644 --- a/python/bencode.py +++ b/python/bencode.py @@ -4,7 +4,7 @@ def __decode_string(b, start=0): """ >>> __decode_string(b'3:foo3:bar') {'value': 'foo', 'start': 5} - >> __decode_string(b'3:foo3:bar', start=5) + >>> __decode_string(b'3:foo3:bar', start=5) {'value': 'bar', 'start': 10} >>> __decode_string(b'0:3:bar') {'value': '', 'start': 2}