[git commit] vi: code shrink

Denys Vlasenko vda.linux at googlemail.com
Mon Mar 29 10:05:53 UTC 2021


commit: https://git.busybox.net/busybox/commit/?id=776b56d774fec51e0ac3f9714adbdb2375c6bda7
branch: https://git.busybox.net/busybox/commit/?id=refs/heads/master

I was puzzled by code in find_range() which handles forward word
movement.  It included a test to see if we're at the start of a
word.  Since these are forward word movements surely we'd expect to
be at the start of a word?  In fact, the test was intended to fix a
problem with changes to the last word in a file, as discussed in the
thread starting here:

   http://lists.busybox.net/pipermail/busybox/2004-January/044552.html

The code can be simplified by testing directly for end of file instead
of indirectly for not being at the start of a word.  Since trailing
whitespace is now handled in do_cmd() the code to back up off a newline
is no longer required.

function                                             old     new   delta
find_range                                           619     514    -105
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 0/1 up/down: 0/-105)           Total: -105 bytes

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

diff --git a/editors/vi.c b/editors/vi.c
index 1bc2d08ad..7a7247c10 100644
--- a/editors/vi.c
+++ b/editors/vi.c
@@ -3021,15 +3021,9 @@ static int find_range(char **start, char **stop, char c)
 		q = dot;
 	} else if (strchr("wW", c)) {
 		do_cmd(c);		// execute movement cmd
-		// if we are at the next word's first char
-		// step back one char
-		// but check the possibilities when it is true
-		if (dot > text && ((isspace(dot[-1]) && !isspace(dot[0]))
-				|| (ispunct(dot[-1]) && !ispunct(dot[0]))
-				|| (isalnum(dot[-1]) && !isalnum(dot[0]))))
-			dot--;		// move back off of next word
-		if (dot > text && *dot == '\n')
-			dot--;		// stay off NL
+		// step back one char, but not if we're at end of file
+		if (dot > p && !((dot == end - 2 && end[-1] == '\n') || dot == end - 1))
+			dot--;
 		q = dot;
 	} else if (strchr("H-k{", c) || (c == 'G' && !forward)) {
 		// these operate on multi-lines backwards


More information about the busybox-cvs mailing list