diff --git a/rot13/Makefile b/rot13/Makefile index 1b3d83a..1a7991f 100644 --- a/rot13/Makefile +++ b/rot13/Makefile @@ -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 diff --git a/rot13/rot-unicode.c b/rot13/rot-unicode.c new file mode 100644 index 0000000..79fe29e --- /dev/null +++ b/rot13/rot-unicode.c @@ -0,0 +1,33 @@ +#include "rot-unicode.h" + +#include /* 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); +} diff --git a/rot13/rot-unicode.h b/rot13/rot-unicode.h new file mode 100644 index 0000000..d95d028 --- /dev/null +++ b/rot13/rot-unicode.h @@ -0,0 +1,13 @@ +#include /* UChar32 */ +#define CHAR UChar32 +#include /* u_islower(3), u_isupper(3) */ +#include /* isalpha(3) */ +#define ISALPHA(a) (((a) < 0x80) && isalpha(a)) +#define ISLOWER u_islower +#define ISUPPER u_isupper +#include /* u_fgetc(3), u_fputc(3), U_EOF */ +#define ENDOFFILE U_EOF +#define GETC u_fgetc +#define PUTC u_fputc +#include /* stdin, stdout */ +/* #include /* size_t */