1
0

vastly simplify code

This commit is contained in:
dtb 2023-12-23 08:46:43 -07:00
parent 106effb923
commit 89d72e95f2
3 changed files with 25 additions and 42 deletions

8
lowercase/case-ascii.h Normal file
View File

@ -0,0 +1,8 @@
#define CHARACTER int
#define ISVALID(c) ((c) <= 0x127 /* ASCII DEL; 0b 0111 111 */ && (c) >= 0)
#include <stdio.h> /* getc(3), putc(3) */
#define GETC getc
#define PUTC putc
#include <ctype.h> /* tolower(3), toupper(3) */
#define TOLOWER tolower
#define TOUPPER toupper

9
lowercase/case-unicode.h Normal file
View File

@ -0,0 +1,9 @@
#include <unicode/umachine.h> /* UChar32 */
#define CHARACTER UChar32
#include <unicode/uchar.h> /* u_tolower(3), u_toupper(3), UCHAR_MAX_VALUE */
#define ISVALID(c) ((c) <= UCHAR_MAX_VALUE && (c) >= 0)
#define TOLOWER u_tolower
#define TOUPPER u_toupper
#include <unicode/ustdio.h> /* u_fgetc(3), u_fputc(3) */
#define GETC u_fgetc
#define PUTC u_fputc

View File

@ -1,27 +1,10 @@
#include <stdio.h> /* fprintf(3), getc(3), putc(3), stderr, stdin, stdout */
#include <stdlib.h> /* exit(3) */
#include <sysexits.h> /* EX_OK, EX_USAGE, EX_SOFTWARE */
#include <unistd.h> /* getopt(3) */
/* Enable Unicode support using official Unicode library. */
#ifdef USE_ICU
# include <unicode/uchar.h>
/* UCHAR_MAX_VALUE, u_tolower(3), u_toupper(3) */
# include <unicode/umachine.h> /* UChar32 */
# include <unicode/ustdio.h> /* u_fgetc(3) */
#else
# include <ctype.h> /* tolower(3), toupper(3) */
#endif /* USE_ICU */
#define ASCII_MAX_VALUE 0x7F
#include <stdio.h> /* fprintf(3), stderr, stdin, stdout */
#if !defined EX_OK || !defined EX_USAGE
# include <sysexits.h>
#endif
int main(int argc, char *argv[]){
#ifdef USE_ICU
UChar32
#else
int
#endif
c; /* iterating over character stream */
CHARACTER c; /* iterating over character stream */
if(argc > 1){
fprintf(stderr, "Usage: %s\n", argv[0]);
@ -29,25 +12,8 @@ int main(int argc, char *argv[]){
}
#ifdef USE_ICU
while((c = u_fgetc(stdin)) != U_EOF){
if(c <= UCHAR_MAX_VALUE && c >= 0)
# ifdef LOWERCASE
c = u_tolower(c);
# else
c = u_toupper(c);
# endif /* ifdef LOWERCASE */
u_fputc(c, stdout);
#else
while((c = getc(stdin)) != EOF){
if(c <= ASCII_MAX_VALUE && c >= 0)
# ifdef LOWERCASE
c = tolower(c);
# else
c = toupper(c);
# endif /* ifdef LOWERCASE */
putc(c, stdout);
#endif /* ifdef USE_ICU */
}
while((c = GETC(stdin)) != ENDOFFILE)
PUTC(ISVALID(c) ? CONV(c) : c, stdout);
return EX_OK;
}