| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
require 'nkf' |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
module Kconv |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
AUTO = NKF::AUTO |
| 27 |
|
| 28 |
JIS = NKF::JIS |
| 29 |
|
| 30 |
EUC = NKF::EUC |
| 31 |
|
| 32 |
SJIS = NKF::SJIS |
| 33 |
|
| 34 |
BINARY = NKF::BINARY |
| 35 |
|
| 36 |
NOCONV = NKF::NOCONV |
| 37 |
|
| 38 |
ASCII = NKF::ASCII |
| 39 |
|
| 40 |
UTF8 = NKF::UTF8 |
| 41 |
|
| 42 |
UTF16 = NKF::UTF16 |
| 43 |
|
| 44 |
UTF32 = NKF::UTF32 |
| 45 |
|
| 46 |
UNKNOWN = NKF::UNKNOWN |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
def kconv(str, to_enc, from_enc=nil) |
| 58 |
opt = '' |
| 59 |
opt += ' --ic=' + from_enc.to_s if from_enc |
| 60 |
opt += ' --oc=' + to_enc.to_s if to_enc |
| 61 |
|
| 62 |
::NKF::nkf(opt, str) |
| 63 |
end |
| 64 |
module_function :kconv |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
def tojis(str) |
| 75 |
kconv(str, JIS) |
| 76 |
end |
| 77 |
module_function :tojis |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
def toeuc(str) |
| 84 |
kconv(str, EUC) |
| 85 |
end |
| 86 |
module_function :toeuc |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
def tosjis(str) |
| 93 |
kconv(str, SJIS) |
| 94 |
end |
| 95 |
module_function :tosjis |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
def toutf8(str) |
| 102 |
kconv(str, UTF8) |
| 103 |
end |
| 104 |
module_function :toutf8 |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
def toutf16(str) |
| 111 |
kconv(str, UTF16) |
| 112 |
end |
| 113 |
module_function :toutf16 |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
def toutf32(str) |
| 120 |
kconv(str, UTF32) |
| 121 |
end |
| 122 |
module_function :toutf32 |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
def tolocale(str) |
| 129 |
kconv(str, Encoding.locale_charmap) |
| 130 |
end |
| 131 |
module_function :tolocale |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
def guess(str) |
| 142 |
::NKF::guess(str) |
| 143 |
end |
| 144 |
module_function :guess |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
def iseuc(str) |
| 157 |
str.dup.force_encoding(EUC).valid_encoding? |
| 158 |
end |
| 159 |
module_function :iseuc |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
def issjis(str) |
| 166 |
str.dup.force_encoding(SJIS).valid_encoding? |
| 167 |
end |
| 168 |
module_function :issjis |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
def isjis(str) |
| 175 |
/\A [\t\n\r\x20-\x7E]* |
| 176 |
(?: |
| 177 |
(?:\x1b \x28 I [\x21-\x7E]* |
| 178 |
|\x1b \x28 J [\x21-\x7E]* |
| 179 |
|\x1b \x24 @ (?:[\x21-\x7E]{2})* |
| 180 |
|\x1b \x24 B (?:[\x21-\x7E]{2})* |
| 181 |
|\x1b \x24 \x28 D (?:[\x21-\x7E]{2})* |
| 182 |
)* |
| 183 |
\x1b \x28 B [\t\n\r\x20-\x7E]* |
| 184 |
)* |
| 185 |
\z/nox =~ str.dup.force_encoding('BINARY') ? true : false |
| 186 |
end |
| 187 |
module_function :isjis |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
def isutf8(str) |
| 194 |
str.dup.force_encoding(UTF8).valid_encoding? |
| 195 |
end |
| 196 |
module_function :isutf8 |
| 197 |
end |
| 198 |
|
| 199 |
class String |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
def kconv(to_enc, from_enc=nil) |
| 206 |
from_enc = self.encoding if !from_enc && self.encoding != Encoding.list[0] |
| 207 |
Kconv::kconv(self, to_enc, from_enc) |
| 208 |
end |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
def tojis; Kconv.tojis(self) end |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
def toeuc; Kconv.toeuc(self) end |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
def tosjis; Kconv.tosjis(self) end |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
def toutf8; Kconv.toutf8(self) end |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
def toutf16; Kconv.toutf16(self) end |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
def toutf32; Kconv.toutf32(self) end |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
def tolocale; Kconv.tolocale(self) end |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
def iseuc; Kconv.iseuc(self) end |
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
def issjis; Kconv.issjis(self) end |
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
def isjis; Kconv.isjis(self) end |
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
def isutf8; Kconv.isutf8(self) end |
| 283 |
end |