shithub: nime

Download patch

ref: ec893e82921b997b373697147db0df327352a850
parent: 6775c0a9a046673bb6e472bd286271725013968a
author: Ethan Long <ethandavidlong@gmail.com>
date: Mon Jun 28 03:06:15 EDT 2021

Added ability to use katakana.

--- a/src/test.c
+++ b/src/test.c
@@ -57,5 +57,11 @@
 	Rune tesuto[4] = L"テスト";
 	int fp = fileno(stdout);
 	fprint(fp, "%s\n%S\n", lol, tesuto);
+	
+	print("%d , %d\n", L'あ', L'ア');
+	print("%d , %d\n", L'か', L'カ');
+	for(int i = 0; i < 100; i++){
+		print("%C , %C\n", (Rune)(12353 + i), (Rune)(12353 + 96 + i));
+	}
 	exits(nil);
 }
--- a/src/to-kana.c
+++ b/src/to-kana.c
@@ -1,10 +1,10 @@
 #include <u.h>
-#include <stdio.h>
 #include <libc.h>
+#include <stdio.h>
 
 void eval(int, int);
 void kanafill(Rune, char*, int, Rune*);
-void kanalook(char*, Rune*);
+void kanalook(char*, Rune*, int);
 void printhelp(void);
 int strappend(char*, char);
 
@@ -18,6 +18,7 @@
 		fpin = fileno(stdin);
 		fpout = fileno(stdout);
 		eval(fpin, fpout);
+		print("\n");
 		break;
 	case 'h':
 		printhelp();
@@ -34,7 +35,7 @@
 printhelp(void)
 {
 	print("options:\n");
-	print(" [-s] - use stdin stream\n");
+	print(" [-s] - use stdin/out streams\n");
 	print(" [-h] - show this help\n");
 	return;
 }
@@ -47,14 +48,17 @@
 eval(int fpin, int fpout)
 {
 	int reading = 1;
+	int katakana = 0;
 	char charin;
 	char buf[10];
 	Rune kana[10];
 	while(reading){
 		if(read(fpin, &charin, 1)){
-			if(strappend(buf, charin)){
-				kanalook(buf, kana);
-				fprint(fpout, "%s %S\n", buf, kana);
+			if(charin == '!')
+				katakana = (0 == katakana);
+			else if(strappend(buf, charin)){
+				kanalook(buf, kana, katakana);
+				fprint(fpout, "%S", kana);
 				for(int i=0; i<10; i++){
 					kana[i] = 0;
 					buf[i] = 0;
@@ -65,7 +69,7 @@
 			reading = 0;
 		}
 	}
-	fprint(fpout, "%s\n", buf);
+	fprint(fpout, "%s", buf);
 	return;
 }
 
@@ -90,6 +94,133 @@
 }
 
 /*
+ * kanalook takes in a pointer to a string and a pointer to a string of Rune
+ * and converts the string in romaji to a string of runes of kana.
+ */
+void
+kanalook(char* buf, Rune* str, int katakana)
+{
+	int index;
+	char* end;
+	
+	katakana = katakana * 96;
+	
+	index = 0;
+	if(buf[0] == buf[1]){
+		str[index] = L'っ';
+		index ++;
+	}
+	end = buf + 1 + index;
+	
+	switch(buf[0]){
+	/* あ family */
+	case 'a':
+		str[0] = (Rune)((int)(L'あ') + katakana);
+		break;
+	case 'i':
+		str[0] = (Rune)((int)(L'い') + katakana);
+		break;
+	case 'u':
+		str[0] = (Rune)((int)(L'う') + katakana);
+		break;
+	case 'e':
+		str[0] = (Rune)((int)(L'え') + katakana);
+		break;
+	case 'o':
+		str[0] = (Rune)((int)(L'お') + katakana);
+		break;
+	
+	/* か family */
+	case 'k':
+		kanafill((Rune)((int)(L'か') + katakana), end, 0, str + index);
+		break;
+	case 'g':
+		kanafill((Rune)((int)(L'か') + katakana), end, 1, str + index);
+		break;
+	
+	/* さ family */
+	case 's':
+		kanafill((Rune)((int)(L'さ') + katakana), end, 0, str + index);
+		break;
+	case 'z':
+		kanafill((Rune)((int)(L'さ') + katakana), end, 1, str + index);
+		break;
+	case 'j':
+		if(buf[index + 1] != 'y' && buf[index + 1] != 'i'){
+			end[0] = 'y';
+			end[1] = buf[index + 1];
+			kanafill((Rune)((int)(L'さ') + katakana), end, 1, str + index);
+		} else
+			kanafill((Rune)((int)(L'さ') + katakana), end, 1, str + index);
+		break;
+	
+	/* た family */
+	case 't':
+		kanafill((Rune)((int)(L'た') + katakana), end, 0, str + index);
+		break;
+	case 'c':
+		if(buf[index + 1] != 'y' && buf[index + 1] != 'i'){
+			end[0] = 'y';
+			end[1] = buf[index + 1];
+			kanafill((Rune)((int)(L'た') + katakana), end, 0, str + index);
+		} else
+			kanafill((Rune)((int)(L'た') + katakana), end, 0, str + index);
+		break;
+	case 'd':
+		kanafill((Rune)((int)(L'た') + katakana), end, 1, str + index);
+		break;
+	
+	/* な family (and ん) */
+	case 'n':
+		if(buf[1] == 'n')
+			str[0] = (Rune)((int)(L'ん') + katakana);
+		else
+			kanafill((Rune)((int)(L'な') + katakana), end, 0, str + index);
+		break;
+	
+	/* は family */
+	case 'h':
+		kanafill((Rune)((int)(L'は') + katakana), end, 0, str + index);
+		break;
+	case 'f':
+		end[0] = 'f';
+		end[1] = buf[index + 1];
+		kanafill((Rune)((int)(L'は') + katakana), end, 0, str + index);
+		break;
+	case 'b':
+		kanafill((Rune)((int)(L'は') + katakana), end, 1, str + index);
+		break;
+	case 'p':
+		kanafill((Rune)((int)(L'は') + katakana), end, 2, str + index);
+		break;
+	
+	/* ま family */
+	case 'm':
+		kanafill((Rune)((int)(L'ま') + katakana), end, 0, str + index);
+		break;
+	
+	/* や family */
+	case 'y':
+		kanafill((Rune)((int)(L'や') + katakana), end, 0, str + index);
+		break;
+	
+	/* ら family */
+	case 'r':
+		kanafill((Rune)((int)(L'ら') + katakana), end, 0, str + index);
+		break;
+	
+	/* わ family */
+	case 'w':
+		kanafill((Rune)((int)(L'わ') + katakana), end, 0, str + index);
+		break;
+	
+	default:
+		break;
+	}
+	return;
+}
+
+/*
  * The kanafill function takes in the base kana of the family to be output,
  * the character representation of the ending syllabaries, an integer
  * representing the type of dakuten (1 for regular, 2 for maru), and a pointer
@@ -101,6 +232,7 @@
 	switch(base){
 	/* Special Boys */
 	case L'や':
+	case L'ヤ':
 		switch(in[0]){
 		case 'a':
 			out[0] = L'や';
@@ -117,11 +249,13 @@
 		break;
 
 	case L'ん':
+	case L'ン':
 		out[0] = L'ん';
 		break;
 	
 	/* は family has marudakuten */
 	case L'は':
+	case L'ハ':
 		switch(in[0]){
 		case 'a':
 			out[0] = (Rune)((int)base + dakuten);
@@ -160,9 +294,13 @@
 	
 	/* families with no dakuten or small forms */
 	case L'ら':
+	case L'ラ':
 	case L'ま':
+	case L'マ':
 	case L'な':
+	case L'ナ':
 	case L'わ':
+	case L'ワ':
 		switch(in[0]){
 		case 'a':
 			out[0] = base;
@@ -201,7 +339,9 @@
 	
 	/* families with just dakuten */
 	case L'か':
+	case L'カ':
 	case L'さ':
+	case L'サ':
 		switch(in[0]){
 		case 'a':
 			out[0] = (Rune)((int)base + dakuten);
@@ -240,6 +380,7 @@
 	
 	/* た family has small つ and dakuten */
 	case L'た':
+	case L'タ':
 		switch(in[0]){
 		case 'a':
 			out[0] = (Rune)((int)base + dakuten);
@@ -282,121 +423,6 @@
 	return;
 }
 
-/*
- * kanalook takes in a pointer to a string and a pointer to a string of Rune
- * and converts the string in romaji to a string of runes of kana.
- */
-void
-kanalook(char* buf, Rune* str)
-{
-	char* end;
-	end = buf + 1;
-	switch(buf[0]){
-	/* あ family */
-	case 'a':
-		str[0] = L'あ';
-		break;
-	case 'i':
-		str[0] = L'い';
-		break;
-	case 'u':
-		str[0] = L'う';
-		break;
-	case 'e':
-		str[0] = L'え';
-		break;
-	case 'o':
-		str[0] = L'お';
-		break;
-	
-	/* か family */
-	case 'k':
-		kanafill(L'か', end, 0, str);
-		break;
-	case 'g':
-		kanafill(L'か', end, 1, str);
-		break;
-	
-	/* さ family */
-	case 's':
-		kanafill(L'さ', end, 0, str);
-		break;
-	case 'z':
-		kanafill(L'さ', end, 1, str);
-		break;
-	case 'j':
-		if(buf[1] != 'y' && buf[1] != 'i'){
-			end[0] = 'y';
-			end[1] = buf[1];
-			kanafill(L'さ', end, 1, str);
-		} else
-			kanafill(L'さ', end, 1, str);
-		break;
-	
-	/* た family */
-	case 't':
-		kanafill(L'た', end, 0, str);
-		break;
-	case 'c':
-		if(buf[1] != 'y' && buf[1] != 'i'){
-			end[0] = 'y';
-			end[1] = buf[1];
-			kanafill(L'た', end, 0, str);
-		} else
-			kanafill(L'た', end, 0, str);
-		break;
-	case 'd':
-		kanafill(L'た', end, 1, str);
-		break;
-	
-	/* な family (and ん) */
-	case 'n':
-		if(buf[1] == 'n')
-			str[0] = L'ん';
-		else
-			kanafill(L'な', end, 0, str);
-		break;
-	
-	/* は family */
-	case 'h':
-		kanafill(L'は', end, 0, str);
-		break;
-	case 'f':
-		end[0] = 'f';
-		end[1] = buf[1];
-		kanafill(L'は', end, 0, str);
-		break;
-	case 'b':
-		kanafill(L'は', end, 1, str);
-		break;
-	case 'p':
-		kanafill(L'は', end, 2, str);
-		break;
-	
-	/* ま family */
-	case 'm':
-		kanafill(L'ま', end, 0, str);
-		break;
-	
-	/* や family */
-	case 'y':
-		kanafill(L'や', end, 0, str);
-		break;
-	
-	/* ら family */
-	case 'r':
-		kanafill(L'ら', end, 0, str);
-		break;
-	
-	/* わ family */
-	case 'w':
-		kanafill(L'わ', end, 0, str);
-		break;
-	
-	default:
-		break;
-	}
-	return;
-}
+