py/repl: Refactor autocomplete to reduce nesting.
Originally at adafruit#4548
Updated for Pycopy.
@@ -161,15 +161,22 @@ size_t mp_repl_autocomplete(const char *str, size_t len, const mp_print_t *print
mp_obj_t obj = MP_OBJ_FROM_PTR(&mp_module___main__);
mp_obj_t dest[2];
+ const char *s_start;
+ size_t s_len;
+
for (;;) {
// get next word in string to complete
- const char *s_start = str;
+ s_start = str;
while (str < top && *str != '.') {
++str;
}
- size_t s_len = str - s_start;
+ s_len = str - s_start;
+
+ if (str == top) {
+ // end of string, do completion on this partial name
+ break;
+ }
- if (str < top) {
// a complete word, lookup in current object
qstr q = qstr_find_strn(s_start, s_len);
if (q == MP_QSTRnull) {
@@ -186,9 +193,7 @@ size_t mp_repl_autocomplete(const char *str, size_t len, const mp_print_t *print
// skip '.' to move to next word
++str;
-
- } else {
- // end of string, do completion on this partial name
+ }
// look for matches
const char *match_str = NULL;
@@ -281,7 +286,5 @@ size_t mp_repl_autocomplete(const char *str, size_t len, const mp_print_t *print
return (size_t)(-1); // indicate many matches
}
- }
-}
#endif // MICROPY_HELPER_REPL
