Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/games/cgram cgram: fix assertion when substituting at the en...



details:   https://anonhg.NetBSD.org/src/rev/f7c96991a3eb
branches:  trunk
changeset: 980931:f7c96991a3eb
user:      rillig <rillig%NetBSD.org@localhost>
date:      Mon Feb 22 19:34:07 2021 +0000

description:
cgram: fix assertion when substituting at the end of the longest line

Reported by Weitian LI via GitHub.

Contrary to the patch suggested in [1], still allow the cursor to be
placed to the very right of the text in a line since that is the usual
behavior of text editors.

Split the function substitute() into two parts:  one that handles the
curses-specific part of checking whether a substitution is possible at
the current cursor position, and one that performs the actual
substitution.  Only the latter is kept in the code section for the
string manipulation functions, the other is moved to the section for
curses code.  Having all the curses code in one place reduces the places
that call beep().  Previously, as well as now, there is a single beep
per invalid key before, but that was not obvious from the previous code.

[1]: https://github.com/DragonFlyBSD/DragonFlyBSD/commit/18d09f18cf4c

diffstat:

 games/cgram/cgram.c |  69 +++++++++++++++++++++++-----------------------------
 1 files changed, 30 insertions(+), 39 deletions(-)

diffs (107 lines):

diff -r ffd6d021a953 -r f7c96991a3eb games/cgram/cgram.c
--- a/games/cgram/cgram.c       Mon Feb 22 17:36:42 2021 +0000
+++ b/games/cgram/cgram.c       Mon Feb 22 19:34:07 2021 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: cgram.c,v 1.13 2021/02/22 17:36:42 rillig Exp $ */
+/* $NetBSD: cgram.c,v 1.14 2021/02/22 19:34:07 rillig Exp $ */
 
 /*-
  * Copyright (c) 2013, 2021 The NetBSD Foundation, Inc.
@@ -31,7 +31,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__RCSID) && !defined(lint)
-__RCSID("$NetBSD: cgram.c,v 1.13 2021/02/22 17:36:42 rillig Exp $");
+__RCSID("$NetBSD: cgram.c,v 1.14 2021/02/22 19:34:07 rillig Exp $");
 #endif
 
 #include <assert.h>
@@ -279,40 +279,26 @@
        }
 }
 
-static bool
-substitute(char ch)
+static void
+substitute(char a, char b)
 {
-       assert(cursor_x >= 0 && cursor_x < extent_x);
-       assert(cursor_y >= 0 && cursor_y < extent_y);
-       if (cursor_x >= cur_max_x()) {
-               beep();
-               return false;
-       }
-
-       char och = char_at_cursor();
-       if (!ch_isalpha(och)) {
-               beep();
-               return false;
-       }
-
-       char loch = ch_tolower(och);
-       char uoch = ch_toupper(och);
-       char lch = ch_tolower(ch);
-       char uch = ch_toupper(ch);
+       char la = ch_tolower(a);
+       char ua = ch_toupper(a);
+       char lb = ch_tolower(b);
+       char ub = ch_toupper(b);
 
        for (int y = 0; y < (int)lines.num; y++) {
                for (char *p = lines.v[y].s; *p != '\0'; p++) {
-                       if (*p == loch)
-                               *p = lch;
-                       else if (*p == uoch)
-                               *p = uch;
-                       else if (*p == lch)
-                               *p = loch;
-                       else if (*p == uch)
-                               *p = uoch;
+                       if (*p == la)
+                               *p = lb;
+                       else if (*p == ua)
+                               *p = ub;
+                       else if (*p == lb)
+                               *p = la;
+                       else if (*p == ub)
+                               *p = ua;
                }
        }
-       return true;
 }
 
 static bool
@@ -457,19 +443,24 @@
                go_right();
 }
 
+static bool
+can_substitute_here(int ch)
+{
+       return isascii(ch) &&
+           ch_isalpha((char)ch) &&
+           cursor_x < cur_max_x() &&
+           ch_isalpha(char_at_cursor());
+}
+
 static void
 handle_char_input(int ch)
 {
-       if (isascii(ch) && ch_isalpha((char)ch)) {
-               if (substitute((char)ch)) {
-                       if (cursor_x < cur_max_x())
-                               cursor_x++;
-                       if (cursor_x == cur_max_x())
-                               go_to_next_line();
-               }
-       } else if (ch == char_at_cursor())
+       if (ch == char_at_cursor())
                go_right();
-       else
+       else if (can_substitute_here(ch)) {
+               substitute(char_at_cursor(), (char)ch);
+               go_right();
+       } else
                beep();
 }
 



Home | Main Index | Thread Index | Old Index