From 2dbe3a72a9b248ec967612fe617888a1297ea202 Mon Sep 17 00:00:00 2001 From: rillig Date: Sun, 25 Apr 2021 20:38:03 +0000 Subject: cgram: use ASCII-only implementation of 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. --- cgram/cgram.c | 37 ++++++++++++++++++------------------- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/cgram/cgram.c b/cgram/cgram.c index 00701ab7..54b6429f 100644 --- a/cgram/cgram.c +++ b/cgram/cgram.c @@ -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 #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 @@ -46,42 +46,41 @@ __RCSID("$NetBSD: cgram.c,v 1.20 2021/04/25 20:19:19 rillig Exp $"); #include "pathnames.h" -//////////////////////////////////////////////////////////// -static char -ch_toupper(char ch) +static bool +ch_isspace(char ch) { - return (char)toupper((unsigned char)ch); + return isspace((unsigned char)ch) != 0; } -static char -ch_tolower(char ch) +static bool +ch_islower(char ch) { - return (char)tolower((unsigned char)ch); + return ch >= 'a' && ch <= 'z'; } static bool -ch_isalpha(char ch) +ch_isupper(char ch) { - return isalpha((unsigned char)ch) != 0; + return ch >= 'A' && ch <= 'Z'; } static bool -ch_islower(char ch) +ch_isalpha(char ch) { - return islower((unsigned char)ch) != 0; + return ch_islower(ch) || ch_isupper(ch); } -static bool -ch_isspace(char ch) +static char +ch_toupper(char ch) { - return isspace((unsigned char)ch) != 0; + return ch_islower(ch) ? (char)(ch - 'a' + 'A') : ch; } -static bool -ch_isupper(char ch) +static char +ch_tolower(char ch) { - return isupper((unsigned char)ch) != 0; + return ch_isupper(ch) ? (char)(ch - 'A' + 'a') : ch; } static int -- cgit v1.2.3-56-ge451