-
Notifications
You must be signed in to change notification settings - Fork 0
/
basic_char.gs
168 lines (165 loc) · 4.95 KB
/
basic_char.gs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*! (c) 2014-2017 みらい研究室実行委員会
* Released under the MIT license.
*/
/**
* @brief detect type.
* @param obj {any} target variable
* @return {string}
*/
function decltype(obj){
return (typeof(obj) === "undefined") ? "undefined" : Object.prototype.toString.call(obj).slice(8, -1);
}
/**
* @brief check type.
* @param type {string} typename(String,Number,Boolean,Date,Error,Array,Function,RegExp,Object, ...)
* @param obj {any} target variable
* @return {boolean}
*/
function is(type, obj) {
return obj !== undefined && obj !== null && decltype(obj) === type;
}
/**
* @brief concat all element as string.
* @param args {onject} array-like object.
*/
function concat_all_as_string(args) {
var s = "";
for (var i = 0; i < args.length; i++) {
s += args[i];
}
return s;
}
/**
* @brief ひらがな⇒全角カタカナ, 全角英数⇒半角英数
* @param args {string|string[]|string[][]} target string
* @return {string|string[]|string[][]}
*/
function phonetic(args) {
var type = decltype(args);
switch (type) {
case "String":
// ひらがな⇒カタカナ
var s = [];
for (var i = 0; i < args.length; i++) {
var c = args[i].charCodeAt();
s[i] = (0x3041 <= c && c <= 0x3096) ? c + 0x0060 : c;
}
var katakana = String.fromCharCode.apply(null, s);
// 全角英数⇒半角英数
return katakana.replace(/[A-Za-z0-9]/g, function(s) {
return String.fromCharCode(s.charCodeAt(0) - 0xFEE0);
});
case "Array":
return function(){
var i;
var re = [];
for(i = 0; i < args.length; ++i) re.push(phonetic(args[i]));
return re;
}();
default:
return "error. unexpected type.";
}
}
function asc(args){
var type = decltype(args);
switch (type) {
case "String":
// 全角カタカナ⇒半角カタカナ
//変換テーブル:2つの配列の要素数が一致しない場合空白文字が戻り値になる、かならず揃えること
//replace_table_base:変換対象(検索)文字(wchar_t)
//replace_table_to:置換文字(wchar_t)
//ッ, ヮ, ワ, ヰ, ヱ, ヲ, ン, ヵ, ヶ
var replace_table_base = [
0x30c3, 0x30ee, 0x30ef, 0x30f0, 0x30f1, 0x30f1, 0x30f3, 0x30f5, 0x30f6
]
var replace_table_to = [
0xff6f, 0xff9c, 0xff9c, 0xff72, 0xff74, 0xff66, 0xff9d, 0xff76, 0xff79
]
if(replace_table_base.length != replace_table_to.length) return "";//プログラミングミス防止
// 変換テーブルによる変換
for(var i = 0; i < replace_table_to.length; i++){
args = args.replace(new RegExp(String.fromCharCode(replace_table_base[i]), "g"), String.fromCharCode(replace_table_to[i]));
}
args = args
.replace(/ヴ/g, "ヴ")
.replace(/[ァ-オ]/g, function(s){
var c1 = s.charCodeAt(0);
var c2 = c1 / 2;
if(c1 % 2){
return String.fromCharCode(c2 + 0xe717);//小さいカナ
}
else{
return String.fromCharCode(c2 + 0xe720);//普通のかな
}
})
.replace(/[カ-ヂ]/g, function(s){
var c1 = s.charCodeAt(0);
var c2 = c1 / 2;
if(c1 % 2){
return String.fromCharCode(c2 + 0xe721);//無濁点
}
else{
return String.fromCharCode(c2 + 0xe720, 0xff9f);//有濁点
}
})
.replace(/[ツ-ド]/g, function(s){
var c1 = s.charCodeAt(0);
var c2 = c1 / 2;
if(c1 % 2){
return String.fromCharCode(c2 + 0xe720, 0xff9f);//有濁点
}
return String.fromCharCode(c2 + 0xe720);
})
.replace(/[ナ-ハ]/g, function(s){
return String.fromCharCode(s.charCodeAt(0) + 0xcebb);
})
.replace(/[バ-ポ]/g, function(s){
var c1 = s.charCodeAt(0);
var c2 = c1 / 3;
switch(c1 % 3){
case 2:
return String.fromCharCode(c2 + 0xEF45, 0xff9f);//゚
case 1:
return String.fromCharCode(c2 + 0xEF45, 0xff9e);//゙
default:
return String.fromCharCode(c2 + 0xEF45);
}
})
.replace(/[マ-モ]/g, function(s){
return String.fromCharCode(s.charCodeAt(0) + 0xceb1);
})
.replace(/[ャ-ョ]/g, function(s){
var c1 = s.charCodeAt(0);
var c2 = c1 / 2;
if(c1 % 2){
return String.fromCharCode(c2 + 0xe6fb);//小さいカナ
}
else{
return String.fromCharCode(c2 + 0xe722);//普通のかな
}
})
.replace(/[ヨ-ロ]/g, function(s){
return String.fromCharCode(s.charCodeAt(0) + 0xceae);
});
return args;
case "Array":
return function(){
var i;
var re = [];
for(i = 0; i < args.length; ++i) re.push(asc(args[i]));
return re;
}();
default:
return "error. unexpected type.";
}
}
/**
* @brief remove chars.
* @param args {string} target string.
* @return {string} replaced string.
*/
function remove_by_chars(args, chars){
if(!is("string", chars)) return args;
if(!is("string", args)) return args;
return args.replace(new RegExp("[" + chars + "]", "g"), "");
}