22477
|
1 |
"{ Encoding: utf8 }"
|
|
2 |
|
8171
|
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 |
"
|
8170
|
14 |
"{ Package: 'stx:libbasic' }"
|
|
15 |
|
|
16 |
"{ NameSpace: CharacterEncoderImplementations }"
|
|
17 |
|
22477
|
18 |
VariableBytesEncoder subclass:#ISO10646_to_SGML
|
8170
|
19 |
instanceVariableNames:''
|
|
20 |
classVariableNames:''
|
|
21 |
poolDictionaries:''
|
|
22 |
category:'Collections-Text-Encodings'
|
|
23 |
!
|
|
24 |
|
8171
|
25 |
!ISO10646_to_SGML class methodsFor:'documentation'!
|
|
26 |
|
|
27 |
copyright
|
|
28 |
"
|
|
29 |
COPYRIGHT (c) 2004 by eXept Software AG
|
|
30 |
All Rights Reserved
|
|
31 |
|
|
32 |
This software is furnished under a license and may be used
|
|
33 |
only in accordance with the terms of that license and with the
|
|
34 |
inclusion of the above copyright notice. This software may not
|
|
35 |
be provided or otherwise made available to, or used by, any
|
|
36 |
other person. No title to or ownership of the software is
|
|
37 |
hereby transferred.
|
|
38 |
"
|
|
39 |
!
|
|
40 |
|
|
41 |
documentation
|
|
42 |
"
|
|
43 |
Incomplete - only knows how to encode/decode escaped decimal-code characters
|
|
44 |
(i.e. &#nnnn; )
|
|
45 |
|
|
46 |
TODO:
|
|
47 |
add all other characters
|
|
48 |
reuse this code in XML and HTML processing code.
|
|
49 |
"
|
|
50 |
! !
|
8170
|
51 |
|
|
52 |
!ISO10646_to_SGML methodsFor:'encoding & decoding'!
|
|
53 |
|
|
54 |
decodeString:aStringOrByteCollection
|
|
55 |
"given a string in SGML encoding (i.e. with SGML escaped characters),
|
|
56 |
return a new string containing the same characters, in 16bit (or more) encoding.
|
|
57 |
Returns either a normal String, a TwoByteString or a FourByteString instance.
|
|
58 |
Only useful, when reading from external sources.
|
|
59 |
This only handles up-to 30bit characters."
|
|
60 |
|
|
61 |
|nBits ch
|
22477
|
62 |
in out codePoint|
|
8170
|
63 |
|
|
64 |
nBits := 8.
|
|
65 |
in := aStringOrByteCollection readStream.
|
22477
|
66 |
out := CharacterWriteStream on:(String new:10).
|
8170
|
67 |
[in atEnd] whileFalse:[
|
|
68 |
ch := in next.
|
|
69 |
ch == $& ifTrue:[
|
|
70 |
in peekOrNil == $# ifTrue:[
|
|
71 |
in next.
|
|
72 |
codePoint := 0.
|
|
73 |
[ch := in peekOrNil.
|
|
74 |
ch notNil and:[ch isDigit]
|
|
75 |
] whileTrue:[
|
|
76 |
codePoint := (codePoint * 10) + ch digitValue.
|
|
77 |
in next.
|
|
78 |
].
|
22477
|
79 |
out nextPut:(Character codePoint:codePoint).
|
8170
|
80 |
in peekOrNil == $; ifTrue:[
|
|
81 |
in next.
|
|
82 |
]
|
|
83 |
] ifFalse:[
|
|
84 |
out nextPut:ch
|
|
85 |
]
|
|
86 |
] ifFalse:[
|
|
87 |
out nextPut:ch
|
|
88 |
].
|
|
89 |
].
|
|
90 |
^ out contents
|
|
91 |
|
|
92 |
"
|
|
93 |
CharacterEncoderImplementations::ISO10646_to_SGML
|
|
94 |
decodeString:'Файл'
|
|
95 |
|
|
96 |
CharacterEncoderImplementations::ISO10646_to_SGML
|
|
97 |
decodeString:'#197;&bn...'
|
|
98 |
"
|
|
99 |
|
22477
|
100 |
"Modified: / 17-01-2018 / 18:35:52 / stefan"
|
8170
|
101 |
!
|
|
102 |
|
|
103 |
encodeString:aUnicodeString
|
10108
|
104 |
"return the SGML representation of aUnicodeString.
|
8170
|
105 |
The resulting string is only useful to be stored on some external file,
|
10108
|
106 |
not for being used inside ST/X."
|
8170
|
107 |
|
22477
|
108 |
|in out|
|
8170
|
109 |
|
|
110 |
in := aUnicodeString readStream.
|
22477
|
111 |
out := WriteStream on:(String new:aUnicodeString size + 10).
|
8170
|
112 |
[in atEnd] whileFalse:[
|
22477
|
113 |
|ch codePoint|
|
|
114 |
|
8170
|
115 |
ch := in next.
|
|
116 |
codePoint := ch codePoint.
|
|
117 |
(codePoint between:16r20 and:16r7F) ifTrue:[
|
|
118 |
out nextPut:ch.
|
|
119 |
] ifFalse:[
|
|
120 |
out nextPutAll:'&#'.
|
22477
|
121 |
codePoint printOn:out.
|
|
122 |
out nextPut:$;.
|
8170
|
123 |
].
|
|
124 |
].
|
|
125 |
^ out contents
|
|
126 |
|
|
127 |
"
|
|
128 |
CharacterEncoderImplementations::ISO10646_to_SGML
|
22477
|
129 |
encodeString:'hello äöü'
|
8170
|
130 |
"
|
10108
|
131 |
|
|
132 |
"Modified: / 23-10-2006 / 13:25:27 / cg"
|
22477
|
133 |
"Modified (format): / 17-01-2018 / 18:41:16 / stefan"
|
|
134 |
! !
|
|
135 |
|
|
136 |
!ISO10646_to_SGML methodsFor:'queries'!
|
|
137 |
|
|
138 |
characterSize:aCharacter
|
|
139 |
|codePoint|
|
|
140 |
|
|
141 |
codePoint := aCharacter codePoint.
|
|
142 |
(codePoint between:16r20 and:16r7F) ifTrue:[
|
|
143 |
^ 1.
|
|
144 |
].
|
|
145 |
^ codePoint printString size + 3 "#&1234;"
|
|
146 |
|
|
147 |
"Created: / 17-01-2018 / 18:01:40 / stefan"
|
|
148 |
! !
|
|
149 |
|
|
150 |
!ISO10646_to_SGML methodsFor:'stream support'!
|
|
151 |
|
|
152 |
readNextCharacterFrom:aStream
|
|
153 |
|char codePoint|
|
|
154 |
|
|
155 |
char := aStream next.
|
|
156 |
(char ~~ $# and:[aStream peek ~~ $&]) ifTrue:[
|
|
157 |
^ char.
|
|
158 |
].
|
|
159 |
aStream next.
|
|
160 |
|
|
161 |
codePoint := 0.
|
|
162 |
[char := aStream peekOrNil.
|
|
163 |
char notNil and:[char isDigit]
|
|
164 |
] whileTrue:[
|
|
165 |
codePoint := (codePoint * 10) + char digitValue.
|
|
166 |
aStream next.
|
|
167 |
].
|
|
168 |
aStream peekOrNil == $; ifTrue:[
|
|
169 |
aStream next.
|
|
170 |
].
|
|
171 |
^ Character codePoint:codePoint.
|
|
172 |
|
|
173 |
"Created: / 17-01-2018 / 18:37:40 / stefan"
|
8170
|
174 |
! !
|
|
175 |
|
|
176 |
!ISO10646_to_SGML class methodsFor:'documentation'!
|
|
177 |
|
|
178 |
version
|
22477
|
179 |
^ '$Header$'
|
8170
|
180 |
! !
|
22477
|
181 |
|