author | Jan Vrany <jan.vrany@fit.cvut.cz> |
Wed, 19 Aug 2009 17:14:36 +0100 | |
branch | jv |
changeset 17728 | bbc5fa73dfab |
parent 17711 | 39faaaf888b4 |
child 17845 | 7e0cfaac936d |
permissions | -rw-r--r-- |
17711 | 1 |
" |
2 |
COPYRIGHT (c) 2006 by eXept Software AG |
|
3 |
All Rights Reserved |
|
4 |
||
5 |
This software is furnished under a license and may be used |
|
6 |
only in accordance with the terms of that license and with the |
|
7 |
inclusion of the above copyright notice. This software may not |
|
8 |
be provided or otherwise made available to, or used by, any |
|
9 |
other person. No title to or ownership of the software is |
|
10 |
hereby transferred. |
|
11 |
" |
|
12 |
"{ Package: 'stx:libbasic' }" |
|
13 |
||
14 |
"{ NameSpace: CharacterEncoderImplementations }" |
|
15 |
||
16 |
TwoByteEncoder subclass:#ISO10646_to_JavaText |
|
17 |
instanceVariableNames:'' |
|
18 |
classVariableNames:'' |
|
19 |
poolDictionaries:'' |
|
20 |
category:'Collections-Text-Encodings' |
|
21 |
! |
|
22 |
||
23 |
!ISO10646_to_JavaText class methodsFor:'documentation'! |
|
24 |
||
25 |
copyright |
|
26 |
" |
|
27 |
COPYRIGHT (c) 2006 by eXept Software AG |
|
28 |
All Rights Reserved |
|
29 |
||
30 |
This software is furnished under a license and may be used |
|
31 |
only in accordance with the terms of that license and with the |
|
32 |
inclusion of the above copyright notice. This software may not |
|
33 |
be provided or otherwise made available to, or used by, any |
|
34 |
other person. No title to or ownership of the software is |
|
35 |
hereby transferred. |
|
36 |
" |
|
37 |
! |
|
38 |
||
39 |
documentation |
|
40 |
" |
|
41 |
Translates \uXXXX-escapes in the text |
|
42 |
" |
|
43 |
! ! |
|
44 |
||
45 |
!ISO10646_to_JavaText methodsFor:'encoding & decoding'! |
|
46 |
||
47 |
decode:aCode |
|
48 |
self shouldNotImplement "/ no single byte conversion possible |
|
49 |
! |
|
50 |
||
51 |
decodeString:aStringOrByteCollection |
|
52 |
"given a string in JavaText encoding (i.e. with \uXXXX escaped characters), |
|
53 |
return a new string containing the same characters, in 16bit (or more) encoding. |
|
54 |
Returns either a normal String, a TwoByteString or a FourByteString instance. |
|
55 |
Only useful, when reading Java property and resource files. |
|
56 |
This only handles up-to 30bit characters." |
|
57 |
||
58 |
|nBits ch |
|
59 |
in out codePoint t| |
|
60 |
||
61 |
nBits := 8. |
|
62 |
in := aStringOrByteCollection readStream. |
|
63 |
out := WriteStream on:(String new:10). |
|
64 |
[in atEnd] whileFalse:[ |
|
65 |
ch := in next. |
|
66 |
ch == $\ ifTrue:[ |
|
67 |
in peekOrNil == $u ifTrue:[ |
|
68 |
in next. |
|
69 |
codePoint := 0. |
|
70 |
4 timesRepeat:[ |
|
71 |
ch := in peekOrNil. |
|
72 |
codePoint := (codePoint * 16) + ch digitValue. |
|
73 |
in next. |
|
74 |
]. |
|
75 |
codePoint > 16rFF ifTrue:[ |
|
76 |
codePoint > 16rFFFF ifTrue:[ |
|
77 |
nBits < 32 ifTrue:[ |
|
78 |
t := out contents. |
|
79 |
out := WriteStream on:(Unicode32String fromString:t). |
|
80 |
out position:t size. |
|
81 |
nBits := 32. |
|
82 |
] |
|
83 |
] ifFalse:[ |
|
84 |
nBits < 16 ifTrue:[ |
|
85 |
t := out contents. |
|
86 |
out := WriteStream on:(Unicode16String fromString:t). |
|
87 |
out position:t size. |
|
88 |
nBits := 16. |
|
89 |
] |
|
90 |
] |
|
91 |
]. |
|
92 |
out nextPut:(Character value:codePoint). |
|
93 |
] ifFalse:[ |
|
94 |
out nextPut:ch |
|
95 |
] |
|
96 |
] ifFalse:[ |
|
97 |
out nextPut:ch |
|
98 |
]. |
|
99 |
]. |
|
100 |
^ out contents |
|
101 |
||
102 |
" |
|
103 |
CharacterEncoderImplementations::ISO10646_to_JavaText |
|
104 |
decodeString:'AB\u1234CD' |
|
105 |
" |
|
106 |
||
107 |
"Modified: / 23-10-2006 / 13:23:18 / cg" |
|
108 |
! |
|
109 |
||
110 |
encode:aCode |
|
111 |
self shouldNotImplement "/ no single byte conversion possible |
|
112 |
! |
|
113 |
||
114 |
encodeString:aUnicodeString |
|
115 |
"return the JavaText representation of aUnicodeString. |
|
116 |
The resulting string is only useful to be stored on some external file, |
|
117 |
not for being used inside ST/X." |
|
118 |
||
119 |
|ch in out codePoint| |
|
120 |
||
121 |
in := aUnicodeString readStream. |
|
122 |
out := WriteStream on:(String new:10). |
|
123 |
[in atEnd] whileFalse:[ |
|
124 |
ch := in next. |
|
125 |
codePoint := ch codePoint. |
|
126 |
(codePoint between:16r20 and:16r7F) ifTrue:[ |
|
127 |
out nextPut:ch. |
|
128 |
] ifFalse:[ |
|
129 |
out nextPutAll:'\u'. |
|
130 |
out nextPutAll:((codePoint printStringRadix:16) leftPaddedTo:4 with:$0). |
|
131 |
]. |
|
132 |
]. |
|
133 |
^ out contents |
|
134 |
||
135 |
" |
|
136 |
CharacterEncoderImplementations::ISO10646_to_JavaText |
|
137 |
encodeString:'hello äöü' |
|
138 |
||
139 |
CharacterEncoderImplementations::ISO10646_to_JavaText |
|
140 |
decodeString:(CharacterEncoderImplementations::ISO10646_to_JavaText encodeString:'hello äöü') |
|
141 |
" |
|
142 |
||
143 |
"Modified: / 23-10-2006 / 13:25:03 / cg" |
|
144 |
! ! |
|
145 |
||
146 |
!ISO10646_to_JavaText class methodsFor:'documentation'! |
|
147 |
||
148 |
version |
|
17728
bbc5fa73dfab
Merged with trunk r10466
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17711
diff
changeset
|
149 |
^ '$Id: CharacterEncoderImplementations__ISO10646_to_JavaText.st 10467 2009-08-19 16:14:36Z vranyj1 $' |
17711 | 150 |
! ! |