Source-Changes-HG archive

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

[src/trunk]: src/games/cgram cgram: use ASCII-only implementation of <ctype.h...



details:   https://anonhg.NetBSD.org/src/rev/d0ba4c3ea0f4
branches:  trunk
changeset: 1020849:d0ba4c3ea0f4
user:      rillig <rillig%NetBSD.org@localhost>
date:      Sun Apr 25 20:38:03 2021 +0000

description:
cgram: use ASCII-only implementation of <ctype.h> functions

The function 'encode' already assumes that all letter characters are
contiguous and that there are only 26 letters of each case.  At the
moment, cgram cannot handle UTF-8 anyway since it reads the input
byte-wise, assuming that each byte is exacty one character.

diffstat:

 games/cgram/cgram.c |  55 ++++++++++++++++++++++++++--------------------------
 1 files changed, 27 insertions(+), 28 deletions(-)

diffs (85 lines):

diff -r eddd17c1e5e7 -r d0ba4c3ea0f4 games/cgram/cgram.c
--- a/games/cgram/cgram.c       Sun Apr 25 20:19:19 2021 +0000
+++ b/games/cgram/cgram.c       Sun Apr 25 20:38:03 2021 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: cgram.c,v 1.20 2021/04/25 20:19:19 rillig Exp $ */
+/* $NetBSD: cgram.c,v 1.21 2021/04/25 20:38:03 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.20 2021/04/25 20:19:19 rillig Exp $");
+__RCSID("$NetBSD: cgram.c,v 1.21 2021/04/25 20:38:03 rillig Exp $");
 #endif
 
 #include <assert.h>
@@ -46,31 +46,6 @@
 
 #include "pathnames.h"
 
-////////////////////////////////////////////////////////////
-
-static char
-ch_toupper(char ch)
-{
-       return (char)toupper((unsigned char)ch);
-}
-
-static char
-ch_tolower(char ch)
-{
-       return (char)tolower((unsigned char)ch);
-}
-
-static bool
-ch_isalpha(char ch)
-{
-       return isalpha((unsigned char)ch) != 0;
-}
-
-static bool
-ch_islower(char ch)
-{
-       return islower((unsigned char)ch) != 0;
-}
 
 static bool
 ch_isspace(char ch)
@@ -79,9 +54,33 @@
 }
 
 static bool
+ch_islower(char ch)
+{
+       return ch >= 'a' && ch <= 'z';
+}
+
+static bool
 ch_isupper(char ch)
 {
-       return isupper((unsigned char)ch) != 0;
+       return ch >= 'A' && ch <= 'Z';
+}
+
+static bool
+ch_isalpha(char ch)
+{
+       return ch_islower(ch) || ch_isupper(ch);
+}
+
+static char
+ch_toupper(char ch)
+{
+       return ch_islower(ch) ? (char)(ch - 'a' + 'A') : ch;
+}
+
+static char
+ch_tolower(char ch)
+{
+       return ch_isupper(ch) ? (char)(ch - 'A' + 'a') : ch;
 }
 
 static int



Home | Main Index | Thread Index | Old Index