[git commit] vi: reset command count when specifying '0' range

Denys Vlasenko vda.linux at googlemail.com
Thu Apr 15 11:09:12 UTC 2021


commit: https://git.busybox.net/busybox/commit/?id=38ae0f3e3e9490c5b8cc3b917c2d896404afddbe
branch: https://git.busybox.net/busybox/commit/?id=refs/heads/master

Since commit a54450248 (vi: allow the '.' command to have a
repetition count) using '0' to specify a range doesn't work with
a non-zero repeat count, e.g. '1d0'.  Users wouldn't normally try
to do that but the '.' command does.

Add a special case in get_motion_char() to handle this.

function                                             old     new   delta
find_range                                           737     746      +9
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 1/0 up/down: 9/0)                 Total: 9 bytes

Signed-off-by: Ron Yorston <rmy at pobox.com>
Signed-off-by: Denys Vlasenko <vda.linux at googlemail.com>
---
 editors/vi.c | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)

diff --git a/editors/vi.c b/editors/vi.c
index d37357edd..d20481fbd 100644
--- a/editors/vi.c
+++ b/editors/vi.c
@@ -1128,11 +1128,16 @@ static int get_motion_char(void)
 	int c, cnt;
 
 	c = get_one_char();
-	if (c != '0' && isdigit(c)) {
-		// get any non-zero motion count
-		for (cnt = 0; isdigit(c); c = get_one_char())
-			cnt = cnt * 10 + (c - '0');
-		cmdcnt = (cmdcnt ?: 1) * cnt;
+	if (isdigit(c)) {
+		if (c != '0') {
+			// get any non-zero motion count
+			for (cnt = 0; isdigit(c); c = get_one_char())
+				cnt = cnt * 10 + (c - '0');
+			cmdcnt = (cmdcnt ?: 1) * cnt;
+		} else {
+			// ensure standalone '0' works
+			cmdcnt = 0;
+		}
 	}
 
 	return c;


More information about the busybox-cvs mailing list