22483
|
1 |
"{ Encoding: utf8 }"
|
|
2 |
|
8148
|
3 |
"
|
|
4 |
COPYRIGHT (c) 2004 by eXept Software AG
|
|
5 |
All Rights Reserved
|
|
6 |
|
|
7 |
This software is furnished under a license and may be used
|
|
8 |
only in accordance with the terms of that license and with the
|
|
9 |
inclusion of the above copyright notice. This software may not
|
|
10 |
be provided or otherwise made available to, or used by, any
|
|
11 |
other person. No title to or ownership of the software is
|
|
12 |
hereby transferred.
|
|
13 |
"
|
8123
|
14 |
"{ Package: 'stx:libbasic' }"
|
|
15 |
|
|
16 |
"{ NameSpace: CharacterEncoderImplementations }"
|
|
17 |
|
22483
|
18 |
VariableBytesEncoder subclass:#JIS0208_to_EUC
|
8123
|
19 |
instanceVariableNames:''
|
|
20 |
classVariableNames:'Jis7KanjiEscapeSequence Jis7RomanEscapeSequence
|
|
21 |
JisISO2022EscapeSequence Jis7KanjiOldEscapeSequence'
|
|
22 |
poolDictionaries:''
|
|
23 |
category:'Collections-Text-Encodings'
|
|
24 |
!
|
|
25 |
|
|
26 |
!JIS0208_to_EUC class methodsFor:'documentation'!
|
|
27 |
|
8148
|
28 |
copyright
|
|
29 |
"
|
|
30 |
COPYRIGHT (c) 2004 by eXept Software AG
|
|
31 |
All Rights Reserved
|
|
32 |
|
|
33 |
This software is furnished under a license and may be used
|
|
34 |
only in accordance with the terms of that license and with the
|
|
35 |
inclusion of the above copyright notice. This software may not
|
|
36 |
be provided or otherwise made available to, or used by, any
|
|
37 |
other person. No title to or ownership of the software is
|
|
38 |
hereby transferred.
|
|
39 |
"
|
|
40 |
!
|
|
41 |
|
22483
|
42 |
documentation
|
|
43 |
"
|
|
44 |
documentation to be added.
|
|
45 |
|
|
46 |
[author:]
|
|
47 |
stefan
|
|
48 |
|
|
49 |
[instance variables:]
|
|
50 |
|
|
51 |
[class variables:]
|
|
52 |
|
|
53 |
[see also:]
|
|
54 |
|
|
55 |
"
|
|
56 |
!
|
|
57 |
|
8123
|
58 |
examples
|
|
59 |
"
|
|
60 |
Encoding (jis0208 to jis-7)
|
|
61 |
|t|
|
|
62 |
|
|
63 |
t := JIS0208_to_JIS7 decodeString:'hello'.
|
|
64 |
JIS0208_to_JIS7 encodeString:t.
|
|
65 |
|
|
66 |
|
|
67 |
|
|
68 |
Decoding (jis-7 to jis0208):
|
|
69 |
|
|
70 |
JIS0208_to_JIS7 decodeString:'hello'
|
|
71 |
|
|
72 |
ending with a crippled escape:
|
|
73 |
|
|
74 |
|s|
|
|
75 |
s := 'hello' copyWith:Character esc.
|
|
76 |
JIS0208_to_JIS7 decodeString:s
|
|
77 |
|
|
78 |
|s|
|
|
79 |
s := 'hello' copyWith:Character esc.
|
|
80 |
s := s copyWith:$A.
|
|
81 |
JIS0208_to_JIS7 decodeString:s
|
|
82 |
|
|
83 |
|s|
|
|
84 |
s := 'hello' copyWith:Character esc.
|
|
85 |
s := s copyWith:$$.
|
|
86 |
JIS0208_to_JIS7 decodeString:s
|
|
87 |
|
|
88 |
|s|
|
|
89 |
s := 'hello' copyWith:Character esc.
|
|
90 |
s := s copyWith:$$.
|
|
91 |
s := s copyWith:$A.
|
|
92 |
JIS0208_to_JIS7 decodeString:s
|
|
93 |
|
|
94 |
ending with a KANJI-in, but no more chars:
|
|
95 |
|
|
96 |
|s|
|
|
97 |
s := 'hello' copyWith:Character esc.
|
|
98 |
s := s copyWith:$$.
|
|
99 |
s := s copyWith:$B.
|
|
100 |
JIS0208_to_JIS7 decodeString:s
|
|
101 |
|
|
102 |
ending with a KANJI-in, followed by $3 (KO):
|
|
103 |
|
|
104 |
|s|
|
|
105 |
s := 'hello' copyWith:Character esc.
|
|
106 |
s := s copyWith:$$.
|
|
107 |
s := s copyWith:$B.
|
|
108 |
s := s , '$3'.
|
|
109 |
JIS0208_to_JIS7 decodeString:s
|
|
110 |
|
|
111 |
ending with a KANJI-in, followed by $3$l$OF| (KO RE HA NI):
|
|
112 |
|
|
113 |
|s|
|
|
114 |
s := 'hello' copyWith:Character esc.
|
|
115 |
s := s copyWith:$$.
|
|
116 |
s := s copyWith:$B.
|
|
117 |
s := s , '$3$l$OF|'.
|
|
118 |
JIS0208_to_JIS7 decodeString:s
|
|
119 |
|
|
120 |
a KO in between:
|
|
121 |
|
|
122 |
|s|
|
|
123 |
s := 'hello' copyWith:Character esc.
|
|
124 |
s := s copyWith:$$.
|
|
125 |
s := s copyWith:$B.
|
|
126 |
s := s , '$3'.
|
|
127 |
s := s copyWith:Character esc.
|
|
128 |
s := s copyWith:$(.
|
|
129 |
s := s copyWith:$B.
|
|
130 |
s := s , 'hello'.
|
|
131 |
JIS0208_to_JIS7 decodeString:s
|
|
132 |
|
|
133 |
I dont know what that means ;-):
|
|
134 |
|
|
135 |
|s t l|
|
|
136 |
s := 'kterm ' copyWith:Character esc.
|
|
137 |
s := s copyWith:$$.
|
|
138 |
s := s copyWith:$B.
|
|
139 |
s := s , '$N4A;zC<Kv%(%_%e%l!!<%?'.
|
|
140 |
s := s copyWith:Character esc.
|
|
141 |
s := s copyWith:$(.
|
|
142 |
s := s copyWith:$B.
|
|
143 |
s := s , ' kterm'.
|
|
144 |
t := JIS0208_to_JIS7 decodeString:s.
|
|
145 |
|
|
146 |
l := Label new.
|
|
147 |
l label:t.
|
|
148 |
l font:(Font family:'k14' face:nil style:nil size:nil).
|
|
149 |
l font:(Font family:'gothic' size:17).
|
|
150 |
l font:(Font family:'mincho' size:23).
|
|
151 |
l realize
|
|
152 |
"
|
|
153 |
! !
|
|
154 |
|
|
155 |
!JIS0208_to_EUC class methodsFor:'queries'!
|
|
156 |
|
|
157 |
nameOfDecodedCode
|
|
158 |
"I encode jis0208 into jis7 and vice versa"
|
|
159 |
|
|
160 |
^ #'jis0208'
|
|
161 |
! !
|
|
162 |
|
|
163 |
!JIS0208_to_EUC methodsFor:'encoding & decoding'!
|
|
164 |
|
|
165 |
decodeString:aString
|
|
166 |
"return a new string containing the characters from aString,
|
|
167 |
which is interpreted as an EUC encoded singleByte string.
|
|
168 |
|
|
169 |
There are various JIS encodings around (New-JIS, Old-JIS and NEC-JIS);
|
|
170 |
this one only understands New-JIS.
|
|
171 |
The result is a JISEncodedString (you need a JIS font to display that ...).
|
|
172 |
|
|
173 |
This is a first-class candidate for a primitive"
|
|
174 |
|
|
175 |
|newString
|
|
176 |
sz "{ Class: SmallInteger }"
|
|
177 |
dstIdx "{ Class: SmallInteger }"
|
|
178 |
srcIdx "{ Class: SmallInteger }"
|
|
179 |
b1 "{ Class: SmallInteger }"
|
|
180 |
b2 "{ Class: SmallInteger }"
|
|
181 |
val "{ Class: SmallInteger }"
|
|
182 |
c c2|
|
|
183 |
|
|
184 |
sz := aString size.
|
|
185 |
newString := TwoByteString new:sz.
|
|
186 |
sz ~~ 0 ifTrue:[
|
|
187 |
dstIdx := 1.
|
|
188 |
srcIdx := 1.
|
|
189 |
|
|
190 |
%{
|
|
191 |
if (__isString(aString)
|
|
192 |
&& (__Class(newString) == @global(JISEncodedString))) {
|
|
193 |
int _dstIdx = 1, _srcIdx = 1;
|
|
194 |
int _sz = __intVal(sz);
|
|
195 |
unsigned char *_cp = __stringVal(aString);
|
|
196 |
unsigned char _c1;
|
|
197 |
unsigned short *_jcp = (unsigned short *)__stringVal(newString);
|
|
198 |
|
|
199 |
while (_srcIdx <= _sz) {
|
|
200 |
_c1 = _cp[_srcIdx-1];
|
|
201 |
if (_c1 < 161) {
|
|
202 |
_jcp[_dstIdx-1] = _c1;
|
|
203 |
} else {
|
|
204 |
_srcIdx++;
|
|
205 |
if (_srcIdx <= _sz) {
|
|
206 |
unsigned char _c2;
|
|
207 |
int _val;
|
|
208 |
int _b1, _b2;
|
|
209 |
|
|
210 |
_b1 = _c1 - 128;
|
|
211 |
_c2 = _cp[_srcIdx-1];
|
|
212 |
_b2 = _c2 - 128;
|
|
213 |
_val = (_b1<<8) + _b2;
|
|
214 |
if (_val < 0) {
|
|
215 |
/* decoder errors are handled in smalltalk */
|
|
216 |
_srcIdx--;
|
|
217 |
goto getOutOfHere;
|
|
218 |
}
|
|
219 |
_jcp[_dstIdx-1] = _val;
|
|
220 |
} else {
|
|
221 |
_jcp[_dstIdx-1] = _c1;
|
|
222 |
}
|
|
223 |
}
|
|
224 |
_dstIdx++;
|
|
225 |
_srcIdx++;
|
|
226 |
}
|
|
227 |
getOutOfHere:
|
8913
|
228 |
srcIdx = __mkSmallInteger(_srcIdx);
|
|
229 |
dstIdx = __mkSmallInteger(_dstIdx);
|
8123
|
230 |
}
|
|
231 |
%}.
|
|
232 |
|
|
233 |
[srcIdx <= sz] whileTrue:[
|
|
234 |
c := aString at:srcIdx.
|
|
235 |
b1 := c codePoint.
|
|
236 |
b1 < 161 ifTrue:[
|
|
237 |
"/ characters below 16rA1 are left untranslated
|
|
238 |
"/ (control character or roman).
|
|
239 |
newString at:dstIdx put:c.
|
|
240 |
] ifFalse:[
|
|
241 |
srcIdx := srcIdx + 1.
|
|
242 |
srcIdx <= sz ifTrue:[
|
|
243 |
b1 := b1 - 128.
|
|
244 |
b2 := (c2 := aString at:srcIdx) codePoint.
|
|
245 |
b2 := b2 - 128.
|
|
246 |
val := (b1 bitShift:8) bitOr:b2.
|
|
247 |
val <= 0 ifTrue:[
|
|
248 |
DecodingError
|
|
249 |
raiseWith:aString
|
|
250 |
errorString:'EUC decoding failed (not EUC encoded ?)'.
|
|
251 |
newString at:dstIdx put:c.
|
|
252 |
dstIdx := dstIdx + 1.
|
|
253 |
newString at:dstIdx put:c2.
|
|
254 |
] ifFalse:[
|
|
255 |
newString at:dstIdx put:(Character value:val).
|
|
256 |
].
|
|
257 |
] ifFalse:[
|
|
258 |
newString at:dstIdx put:c.
|
|
259 |
].
|
|
260 |
].
|
|
261 |
dstIdx := dstIdx + 1.
|
|
262 |
srcIdx := srcIdx + 1.
|
|
263 |
].
|
|
264 |
|
|
265 |
(dstIdx-1) ~~ sz ifTrue:[
|
|
266 |
newString := newString copyFrom:1 to:dstIdx-1.
|
|
267 |
].
|
|
268 |
].
|
|
269 |
^ newString
|
|
270 |
|
|
271 |
"simple:
|
|
272 |
|
|
273 |
'hello' decodeFrom:#euc
|
|
274 |
"
|
|
275 |
|
22483
|
276 |
"Created: / 17-04-1996 / 16:10:22 / cg"
|
|
277 |
"Modified: / 04-07-1997 / 11:06:05 / cg"
|
|
278 |
"Modified: / 19-01-2018 / 14:47:19 / stefan"
|
8123
|
279 |
!
|
|
280 |
|
|
281 |
encodeString:aJISString
|
|
282 |
"return a new string with aJISStrings characters as EUC encoded 8bit string.
|
|
283 |
The argument must be a JIS 16 bit character string.
|
|
284 |
The resulting string is only useful to be stored on some external file,
|
|
285 |
not for being displayed in an ST/X view.
|
|
286 |
|
|
287 |
This is a first-class candidate for a primitive"
|
|
288 |
|
|
289 |
|sz "{ Class: SmallInteger }"
|
|
290 |
b1 "{ Class: SmallInteger }"
|
|
291 |
val "{ Class: SmallInteger }"
|
|
292 |
c romans out|
|
|
293 |
|
|
294 |
romans := CharacterEncoderImplementations::JIS0208 romanTable.
|
|
295 |
|
|
296 |
sz := aJISString size.
|
|
297 |
sz == 0 ifTrue:[^ ''].
|
|
298 |
|
|
299 |
out := WriteStream on:(String new:(sz * 2)).
|
|
300 |
|
|
301 |
1 to:sz do:[:srcIndex |
|
|
302 |
b1 := (c := aJISString at:srcIndex) codePoint.
|
|
303 |
b1 < 161 ifTrue:[
|
|
304 |
"/ a control or roman character
|
|
305 |
out nextPut:c.
|
|
306 |
] ifFalse:[
|
|
307 |
"/
|
|
308 |
"/ check for a roman character
|
|
309 |
"/ the two numbers below are romanTable min and romanTable max
|
|
310 |
"/
|
|
311 |
(b1 between:16r2121 and:16r2573) ifTrue:[
|
|
312 |
val := romans indexOf:b1.
|
|
313 |
(val ~~ 0 and:[val <= 127]) ifTrue:[
|
|
314 |
out nextPut:(Character value:(val - 1 + 32))
|
|
315 |
] ifFalse:[
|
|
316 |
out nextPut:(Character value:(b1 bitShift:-8) + 128).
|
|
317 |
out nextPut:(Character value:(b1 bitAnd:16rFF) + 128).
|
|
318 |
].
|
|
319 |
] ifFalse:[
|
|
320 |
out nextPut:(Character value:(b1 bitShift:-8) + 128).
|
|
321 |
out nextPut:(Character value:(b1 bitAnd:16rFF) + 128).
|
|
322 |
]
|
|
323 |
].
|
|
324 |
].
|
|
325 |
^ out contents
|
|
326 |
|
|
327 |
"simple:
|
|
328 |
|
|
329 |
('hello' decodeFrom:#euc) encodeInto:#euc
|
|
330 |
"
|
|
331 |
|
|
332 |
"Created: 17.4.1996 / 16:13:33 / cg"
|
|
333 |
"Modified: 4.7.1997 / 11:03:43 / cg"
|
|
334 |
! !
|
|
335 |
|
8163
|
336 |
!JIS0208_to_EUC methodsFor:'queries'!
|
|
337 |
|
|
338 |
nameOfEncoding
|
|
339 |
^ #'euc'
|
|
340 |
! !
|
|
341 |
|
8123
|
342 |
!JIS0208_to_EUC class methodsFor:'documentation'!
|
|
343 |
|
|
344 |
version
|
22483
|
345 |
^ '$Header$'
|
8123
|
346 |
! !
|
22483
|
347 |
|