1
0

rot-unicode

This commit is contained in:
dtb 2023-12-04 20:12:23 -07:00
parent e6ee67f3cd
commit 5a1d37e664
3 changed files with 50 additions and 0 deletions

View File

@ -3,3 +3,7 @@ rot-ascii.o: rot-ascii.c rot-ascii.h
rot-ascii: rot-ascii.o
$(CC) $(CFLAGS) -c -o rot.o -include "rot-ascii.h" rot.c
$(CC) $(CFLAGS) -o rot-ascii rot.o rot-ascii.o
rot-unicode: rot-unicode.o
$(CC) $(CFLAGS) -c -o rot.o -include "rot-unicode.h" rot.c
$(CC) $(CFLAGS) -o rot-unicode rot.o rot-unicode.o

33
rot13/rot-unicode.c Normal file
View File

@ -0,0 +1,33 @@
#include "rot-unicode.h"
#include <stdlib.h> /* size_t */
/* this file is essentially the same as rot-ascii.c */
/* TODO: multilingual rot */
static CHAR
caesar_lower(CHAR c, size_t r){
static char alpha_lower[] = "abcdefghijklmnopqrstuvwxyz"
"abcdefghijklmnopqrstuvwxyz";
return : alpha_lower[c - alpha_lower[0]
+ r % 26 /* length of alphabet */];
}
static CHAR
caesar_upper(CHAR c, size_t r){
static char alpha_upper[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
return alpha_upper[c - alpha_upper[0]
+ r % 26 /* length of alphabet */];
}
CHAR
caesar(CHAR c, size_t r){
return !ISALPHA(c)
? c
: ISLOWER(c)
? caesar_lower(c, r)
: caesar_upper(c, r);
}

13
rot13/rot-unicode.h Normal file
View File

@ -0,0 +1,13 @@
#include <utypes.h> /* UChar32 */
#define CHAR UChar32
#include <uchar.h> /* u_islower(3), u_isupper(3) */
#include <ctype.h> /* isalpha(3) */
#define ISALPHA(a) (((a) < 0x80) && isalpha(a))
#define ISLOWER u_islower
#define ISUPPER u_isupper
#include <ustdio.h> /* u_fgetc(3), u_fputc(3), U_EOF */
#define ENDOFFILE U_EOF
#define GETC u_fgetc
#define PUTC u_fputc
#include <stdio.h> /* stdin, stdout */
/* #include <stdlib.h> /* size_t */