[aquaskk-changes 366] CVS update: AquaSKK

Back to archive index

t-suw****@users***** t-suw****@users*****
2007年 9月 11日 (火) 23:08:20 JST


Index: AquaSKK/jconv.cpp
diff -u AquaSKK/jconv.cpp:1.1.2.1 AquaSKK/jconv.cpp:1.1.2.2
--- AquaSKK/jconv.cpp:1.1.2.1	Mon Nov 27 15:16:27 2006
+++ AquaSKK/jconv.cpp	Tue Sep 11 23:08:19 2007
@@ -46,4 +46,152 @@
     void convert_eucj_to_utf8(const std::string& from, std::string& to) {
 	std::for_each(from.begin(), from.end(), eucj_to_utf8(to));
     }
+
+    struct kana {
+	const char* hirakana;
+	const char* katakana;
+	const char* jisx0201_kana;
+    };
+
+    // data member pointer
+    typedef const char* kana::*kana_member;
+
+    static kana primary_kana_table[] = {
+	{ "が", "ガ", "ガ" }, { "ぎ", "ギ", "ギ" }, { "ぐ", "グ", "グ" }, { "げ", "ゲ", "ゲ" }, { "ご", "ゴ", "ゴ" },
+	{ "ざ", "ザ", "ザ" }, { "じ", "ジ", "ジ" }, { "ず", "ズ", "ズ" }, { "ぜ", "ゼ", "ゼ" }, { "ぞ", "ゾ", "ゾ" },
+	{ "だ", "ダ", "ダ" }, { "ぢ", "ヂ", "ヂ" }, { "づ", "ヅ", "ヅ" }, { "で", "デ", "デ" }, { "ど", "ド", "ド" },
+	{ "ば", "バ", "バ" }, { "び", "ビ", "ビ" }, { "ぶ", "ブ", "ブ" }, { "べ", "ベ", "ベ" }, { "ぼ", "ボ", "ボ" },
+	{ "ぱ", "パ", "パ" }, { "ぴ", "ピ", "ピ" }, { "ぷ", "プ", "プ" }, { "ぺ", "ペ", "ペ" }, { "ぽ", "ポ", "ポ" },
+	{ "う゛", "ヴ", "ヴ" }, { 0, 0, 0 }
+    };
+
+    static kana secondary_kana_table[] = {
+	{ "あ", "ア", "ア" }, { "い", "イ", "イ" }, { "う", "ウ", "ウ" }, { "え", "エ", "エ" }, { "お", "オ", "オ" },
+	{ "か", "カ", "カ" }, { "き", "キ", "キ" }, { "く", "ク", "ク" }, { "け", "ケ", "ケ" }, { "こ", "コ", "コ" },
+	{ "さ", "サ", "サ" }, { "し", "シ", "シ" }, { "す", "ス", "ス" }, { "せ", "セ", "セ" }, { "そ", "ソ", "ソ" },
+	{ "た", "タ", "タ" }, { "ち", "チ", "チ" }, { "つ", "ツ", "ツ" }, { "て", "テ", "テ" }, { "と", "ト", "ト" },
+	{ "な", "ナ", "ナ" }, { "に", "ニ", "ニ" }, { "ぬ", "ヌ", "ヌ" }, { "ね", "ネ", "ネ" }, { "の", "ノ", "ノ" },
+	{ "は", "ハ", "ハ" }, { "ひ", "ヒ", "ヒ" }, { "ふ", "フ", "フ" }, { "へ", "ヘ", "ヘ" }, { "ほ", "ホ", "ホ" },
+	{ "ま", "マ", "マ" }, { "み", "ミ", "ミ" }, { "む", "ム", "ム" }, { "め", "メ", "メ" }, { "も", "モ", "モ" },
+	{ "や", "ヤ", "ヤ" }, { "ゐ", "ヰ", "イ" }, { "ゆ", "ユ", "ユ" }, { "ゑ", "ヱ", "エ" }, { "よ", "ヨ", "ヨ" },
+	{ "ら", "ラ", "ラ" }, { "り", "リ", "リ" }, { "る", "ル", "ル" }, { "れ", "レ", "レ" }, { "ろ", "ロ", "ロ" },
+	{ "わ", "ワ", "ワ" }, { "を", "ヲ", "ヲ" }, { "ん", "ン", "ン" },
+	{ "ぁ", "ァ", "ァ" }, { "ぃ", "ィ", "ィ" }, { "ぅ", "ゥ", "ゥ" }, { "ぇ", "ェ", "ェ" }, { "ぉ", "ォ", "ォ" },
+	{ "っ", "ッ", "ッ" }, { "ゃ", "ャ", "ャ" }, { "ゅ", "ュ", "ュ" }, { "ょ", "ョ", "ョ" }, { " ", " ", " " },
+	{ "。", "。", "。" }, { "、", "、", "、" }, { "ー", "ー", "ー" }, { "「", "「", "「" }, { "」", "」", "」" },
+	{ "゛", "゛", "゙" }, { "゜", "゜", "゚" }, { 0, 0, 0 }
+    };
+
+    static kana* kana_tables[] = { primary_kana_table, secondary_kana_table, 0 };
+
+    class translate {
+	std::string from_;
+	std::string to_;
+
+    public:
+	translate(const char* from, const char* to) : from_(from), to_(to) {}
+
+	unsigned operator()(std::string& str, unsigned remain, unsigned offset = 0) {
+	    if(!remain) return 0;
+
+	    unsigned pos = str.find(from_, offset);
+	    if(pos == std::string::npos) return remain;
+
+	    str.replace(pos, from_.size(), to_);
+
+	    return this->operator()(str, remain - from_.size(), pos + to_.size());
+	}
+    };
+
+    static void kana_convert(kana_member target, kana_member replacement, std::string& str) {
+	unsigned remain = str.size();
+
+	for(kana** tbl = kana_tables; *tbl != 0; ++ tbl) {
+	    for(kana* ptr = *tbl; remain && ptr->hirakana; ++ ptr) {
+		remain = translate(ptr->*target, ptr->*replacement)(str, remain);
+	    }
+	}
+    }
+
+    void hirakana_to_katakana(const std::string& from, std::string& to) {
+	to = from;
+	kana_convert(&kana::hirakana, &kana::katakana, to);
+    }
+
+    void hirakana_to_jisx0201_kana(const std::string& from, std::string& to) {
+	to = from;
+	kana_convert(&kana::hirakana, &kana::jisx0201_kana, to);
+    }
+
+    void katakana_to_hirakana(const std::string& from, std::string& to) {
+	to = from;
+	kana_convert(&kana::katakana, &kana::hirakana, to);
+    }
+
+    void katakana_to_jisx0201_kana(const std::string& from, std::string& to) {
+	to = from;
+	kana_convert(&kana::katakana, &kana::jisx0201_kana, to);
+    }
+    
+    void jisx0201_kana_to_hirakana(const std::string& from, std::string& to) {
+	to = from;
+	kana_convert(&kana::jisx0201_kana, &kana::hirakana, to);
+    }
+    
+    void jisx0201_kana_to_katakana(const std::string& from, std::string& to) {
+	to = from;
+	kana_convert(&kana::jisx0201_kana, &kana::katakana, to);
+    }
+
+    // latin record
+    struct latin {
+	const char* ascii;
+	const char* jisx0208_latin;
+    };
+
+    static latin latin_table[] = {
+	{ " ", " " }, { "!", "!" }, { "\"", "”" }, { "#", "#" },
+	{ "$", "$" }, { "%", "%" }, { "&", "&" }, { "'", "’" },
+	{ "(", "(" }, { ")", ")" }, { "*", "*" }, { "+", "+" },
+	{ ",", "," }, { "-", "−" }, { ".", "." }, { "/", "/" },
+	{ "0", "0" }, { "1", "1" }, { "2", "2" }, { "3", "3" },
+	{ "4", "4" }, { "5", "5" }, { "6", "6" }, { "7", "7" },
+	{ "8", "8" }, { "9", "9" }, { ":", ":" }, { ";", ";" },
+	{ "<", "<" }, { "=", "=" }, { ">", ">" }, { "?", "?" },
+	{ "@", "@" }, { "A", "A" }, { "B", "B" }, { "C", "C" },
+	{ "D", "D" }, { "E", "E" }, { "F", "F" }, { "G", "G" },
+	{ "H", "H" }, { "I", "I" }, { "J", "J" }, { "K", "K" },
+	{ "L", "L" }, { "M", "M" }, { "N", "N" }, { "O", "O" },
+	{ "P", "ï¼°" }, { "Q", "ï¼±" }, { "R", "ï¼²" }, { "S", "ï¼³" },
+	{ "T", "T" }, { "U", "U" }, { "V", "V" }, { "W", "W" },
+	{ "X", "X" }, { "Y", "Y" }, { "Z", "Z" }, { "[", "[" },
+	{ "\\", "\" }, { "]", "]" }, { "^", "^" }, { "_", "_" },
+	{ "`", "‘" }, { "a", "a" }, { "b", "b" }, { "c", "c" },
+	{ "d", "d" }, { "e", "e" }, { "f", "f" }, { "g", "g" },
+	{ "h", "h" }, { "i", "i" }, { "j", "j" }, { "k", "k" },
+	{ "l", "l" }, { "m", "m" }, { "n", "n" }, { "o", "o" },
+	{ "p", "p" }, { "q", "q" }, { "r", "r" }, { "s", "s" },
+	{ "t", "t" }, { "u", "u" }, { "v", "v" }, { "w", "w" },
+	{ "x", "x" }, { "y", "y" }, { "z", "z" }, { "{", "{" },
+	{ "|", "|" }, { "}", "}" }, { "~", "〜" }, { 0, 0 }
+    };
+
+    void ascii_to_jisx0208_latin(const std::string& from, std::string& to) {
+	to.clear();
+	for(unsigned i = 0; i < from.size(); ++ i) {
+	    if(from[i] < 0x20 || 0x7e < from[i]) {
+		to += from[i];
+	    } else {
+		to += latin_table[from[i] - 0x20].jisx0208_latin;
+	    }
+	}
+    }
+
+    void jisx0208_latin_to_ascii(const std::string& from, std::string& to) {
+	to = from;
+	unsigned remain = to.size();
+	for(latin* ptr = latin_table; remain && ptr->ascii; ++ ptr) {
+	    remain = translate(ptr->jisx0208_latin, ptr->ascii)(to, remain);
+	}
+    }
 }
Index: AquaSKK/jconv.h
diff -u AquaSKK/jconv.h:1.1.2.1 AquaSKK/jconv.h:1.1.2.2
--- AquaSKK/jconv.h:1.1.2.1	Mon Nov 27 15:16:27 2006
+++ AquaSKK/jconv.h	Tue Sep 11 23:08:19 2007
@@ -355,6 +355,18 @@
     void convert_utf8_to_eucj(const std::string& from, std::string& to);
     void convert_eucj_to_utf8(const std::string& from, std::string& to);
 
+    // translator(UTF-8 only)
+    void hirakana_to_katakana(const std::string& from, std::string& to);
+    void hirakana_to_jisx0201_kana(const std::string& from, std::string& to);
+
+    void katakana_to_hirakana(const std::string& from, std::string& to);
+    void katakana_to_jisx0201_kana(const std::string& from, std::string& to);
+
+    void jisx0201_kana_to_hirakana(const std::string& from, std::string& to);
+    void jisx0201_kana_to_katakana(const std::string& from, std::string& to);
+
+    void ascii_to_jisx0208_latin(const std::string& from, std::string& to);
+    void jisx0208_latin_to_ascii(const std::string& from, std::string& to);
 } // namespace jconv
 
 #endif // INC__jconv__


aquaskk-changes メーリングリストの案内
Back to archive index