author | Stefan Vogel <sv@exept.de> |
Tue, 19 Jul 2016 17:29:33 +0200 | |
changeset 20160 | 3e9b5d2aa65e |
parent 20155 | 6ab108dfcb3f |
child 20182 | f8625bd2a98c |
permissions | -rw-r--r-- |
17600 | 1 |
"{ Encoding: utf8 }" |
2 |
||
64 | 3 |
" |
4 |
COPYRIGHT (c) 1994 by Claus Gittinger |
|
20012 | 5 |
All Rights Reserved |
64 | 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 |
" |
|
5342
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
14 |
"{ Package: 'stx:libbasic' }" |
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
15 |
|
17364 | 16 |
"{ NameSpace: Smalltalk }" |
17 |
||
14137
1eb1b4a3f364
Inherit from UninterpretedBytes instead of ByteArray
Stefan Vogel <sv@exept.de>
parents:
14122
diff
changeset
|
18 |
UninterpretedBytes variableByteSubclass:#CharacterArray |
20012 | 19 |
instanceVariableNames:'' |
20 |
classVariableNames:'PreviousMatch DecoderTables EncoderTables DecodingFailedSignal |
|
21 |
EncodingFailedSignal UnicodeNormalizationMap |
|
22 |
UnicodeDenormalizationMap' |
|
23 |
poolDictionaries:'' |
|
24 |
category:'Collections-Text' |
|
64 | 25 |
! |
26 |
||
1849
9f35f5934ec6
added msdos-latin1 en/decoder (code = #msdos850)
Claus Gittinger <cg@exept.de>
parents:
1669
diff
changeset
|
27 |
!CharacterArray class methodsFor:'documentation'! |
64 | 28 |
|
82 | 29 |
copyright |
30 |
" |
|
31 |
COPYRIGHT (c) 1994 by Claus Gittinger |
|
20012 | 32 |
All Rights Reserved |
82 | 33 |
|
34 |
This software is furnished under a license and may be used |
|
35 |
only in accordance with the terms of that license and with the |
|
36 |
inclusion of the above copyright notice. This software may not |
|
37 |
be provided or otherwise made available to, or used by, any |
|
38 |
other person. No title to or ownership of the software is |
|
39 |
hereby transferred. |
|
40 |
" |
|
41 |
! |
|
42 |
||
64 | 43 |
documentation |
44 |
" |
|
255 | 45 |
CharacterArray is a superclass for all kinds of Strings (i.e. |
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
46 |
(singleByte-)Strings, TwoByteStrings, UnicodeStrings |
18318 | 47 |
and whatever my come in the future. |
48 |
||
49 |
This class is abstract, meaning that there are no instances of it; |
|
50 |
concrete subclasses define how the characters are stored (i.e. either as |
|
51 |
single byte, two-byte or four byte strings). |
|
52 |
||
2537 | 53 |
All this class does is provide common protocol for concrete subclasses. |
1290 | 54 |
|
18318 | 55 |
Notice: |
20012 | 56 |
internally, ST/X uses a unicode encoding for ALL characters - both |
57 |
for individual chatacter entities and for strings of characters. |
|
58 |
When reading/writing files in different encodings, the conversion is |
|
59 |
done at read/write time by use of a CharacterEncoder instance. |
|
60 |
These know how to convert to a wide range of encodings. |
|
18318 | 61 |
|
62 |
Also notice: |
|
20012 | 63 |
UTF8 and UTF16 are external encodings of a Unicode string; they are never |
64 |
used internally. When interacting with a UTF8 interface (OS-API or files), |
|
65 |
you should convert UTF8 into the internal full Unicode right at the interface. |
|
66 |
Do not keep UTF8 around internally as String instances. |
|
67 |
The reason is that UTF8 makes it harder to manipulate strings (for example |
|
68 |
to insert/extract substrings or to get its size. Of such operations would |
|
69 |
require a scan of the UTF8, which would complicate them). |
|
70 |
Of course, there may be rare exceptions to this, for example if a file's contents |
|
71 |
is treated as raw data, and the strings have to be copied/shuffled around only, |
|
72 |
without any real processing on it. |
|
18318 | 73 |
|
1290 | 74 |
[author:] |
20012 | 75 |
Claus Gittinger |
1290 | 76 |
|
77 |
[see also:] |
|
20012 | 78 |
String TwoByteString Unicode16String Uniode32String |
79 |
StringCollection |
|
64 | 80 |
" |
81 |
! ! |
|
82 |
||
2728
7569e45a4d15
raise error signal if anything goes wrong while decoding.
Claus Gittinger <cg@exept.de>
parents:
2721
diff
changeset
|
83 |
!CharacterArray class methodsFor:'initialization'! |
7569e45a4d15
raise error signal if anything goes wrong while decoding.
Claus Gittinger <cg@exept.de>
parents:
2721
diff
changeset
|
84 |
|
7569e45a4d15
raise error signal if anything goes wrong while decoding.
Claus Gittinger <cg@exept.de>
parents:
2721
diff
changeset
|
85 |
initialize |
8874 | 86 |
DecodingFailedSignal isNil ifTrue:[ |
20012 | 87 |
DecodingFailedSignal := DecodingError. |
88 |
DecodingFailedSignal notifierString:'error during decode'. |
|
89 |
||
90 |
EncodingFailedSignal :=EncodingError. |
|
91 |
EncodingFailedSignal notifierString:'error during encode'. |
|
5440 | 92 |
] |
2728
7569e45a4d15
raise error signal if anything goes wrong while decoding.
Claus Gittinger <cg@exept.de>
parents:
2721
diff
changeset
|
93 |
|
7569e45a4d15
raise error signal if anything goes wrong while decoding.
Claus Gittinger <cg@exept.de>
parents:
2721
diff
changeset
|
94 |
" |
7569e45a4d15
raise error signal if anything goes wrong while decoding.
Claus Gittinger <cg@exept.de>
parents:
2721
diff
changeset
|
95 |
CharacterArray initialize |
7569e45a4d15
raise error signal if anything goes wrong while decoding.
Claus Gittinger <cg@exept.de>
parents:
2721
diff
changeset
|
96 |
" |
7569e45a4d15
raise error signal if anything goes wrong while decoding.
Claus Gittinger <cg@exept.de>
parents:
2721
diff
changeset
|
97 |
|
2833 | 98 |
"Modified: 3.8.1997 / 18:15:59 / cg" |
2728
7569e45a4d15
raise error signal if anything goes wrong while decoding.
Claus Gittinger <cg@exept.de>
parents:
2721
diff
changeset
|
99 |
! ! |
7569e45a4d15
raise error signal if anything goes wrong while decoding.
Claus Gittinger <cg@exept.de>
parents:
2721
diff
changeset
|
100 |
|
1849
9f35f5934ec6
added msdos-latin1 en/decoder (code = #msdos850)
Claus Gittinger <cg@exept.de>
parents:
1669
diff
changeset
|
101 |
!CharacterArray class methodsFor:'instance creation'! |
64 | 102 |
|
103 |
basicNew |
|
104 |
"return a new empty string" |
|
105 |
||
106 |
^ self basicNew:0 |
|
107 |
! |
|
108 |
||
2734 | 109 |
fromBytes:aByteCollection |
110 |
"return an instance of the receiver class, |
|
19757 | 111 |
taking untranslated bytes from the argument, aByteCollection |
112 |
in most-significant first order. |
|
2734 | 113 |
Only useful, when reading twoByteStrings from external sources." |
114 |
||
19757 | 115 |
^ self fromBytes:aByteCollection MSB:true |
116 |
||
117 |
" |
|
118 |
Unicode16String fromBytes:#[16r02 16r20] |
|
119 |
Unicode16String fromBytes:#[16r02 16r20] MSB:true |
|
120 |
Unicode16String fromBytes:#[16r02 16r20] MSB:false |
|
121 |
" |
|
122 |
||
123 |
"Modified: 30.6.1997 / 20:08:37 / cg" |
|
124 |
! |
|
125 |
||
126 |
fromBytes:aByteCollection MSB:msb |
|
127 |
"return an instance of the receiver class, |
|
128 |
taking untranslated bytes from the argument, aByteCollection |
|
129 |
in the given byte order. |
|
130 |
Only useful, when reading twoByteStrings from external sources." |
|
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
131 |
|
2734 | 132 |
|mySize nBytes newString dstIdx| |
133 |
||
19757 | 134 |
self assert:(self ~~ CharacterArray). "/ only works for concrete subclasses. |
135 |
||
136 |
"/ the following is a quite inefficient implementation. |
|
137 |
"/ consider rewriting, if heavily used. |
|
2734 | 138 |
nBytes := aByteCollection size. |
139 |
mySize := self basicNew bitsPerCharacter. |
|
140 |
mySize == 16 ifTrue:[ |
|
20012 | 141 |
newString := self uninitializedNew:(nBytes // 2). |
142 |
dstIdx := 1. |
|
143 |
msb ifTrue:[ |
|
144 |
aByteCollection pairWiseDo:[:hi :lo | |
|
145 |
newString at:dstIdx put:(Character value:(hi bitShift:8)+lo). |
|
146 |
dstIdx := dstIdx + 1 |
|
147 |
]. |
|
148 |
] ifFalse:[ |
|
149 |
aByteCollection pairWiseDo:[:lo :hi | |
|
150 |
newString at:dstIdx put:(Character value:(hi bitShift:8)+lo). |
|
151 |
dstIdx := dstIdx + 1 |
|
152 |
]. |
|
153 |
]. |
|
154 |
^ newString. |
|
2734 | 155 |
]. |
156 |
||
17625 | 157 |
^ (self uninitializedNew:nBytes) replaceFrom:1 with:aByteCollection |
158 |
||
159 |
" |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
160 |
Unicode16String fromBytes:#[16r02 16r20] |
2734 | 161 |
" |
162 |
||
163 |
"Modified: 30.6.1997 / 20:08:37 / cg" |
|
164 |
! |
|
165 |
||
64 | 166 |
fromString:aString |
167 |
"return a copy of the argument, aString" |
|
168 |
||
18967 | 169 |
|sz| |
170 |
||
171 |
sz := aString size. |
|
172 |
^ (self uninitializedNew:sz) replaceFrom:1 to:sz with:aString startingAt:1 |
|
173 |
||
174 |
" |
|
20012 | 175 |
Unicode16String fromString:'hello' |
17625 | 176 |
" |
581 | 177 |
! |
178 |
||
179 |
fromStringCollection:aCollectionOfStrings |
|
180 |
"return new string formed by concatenating a copy of the argument, aString" |
|
181 |
||
182 |
^ self fromStringCollection:aCollectionOfStrings separatedBy:'' |
|
183 |
||
184 |
" |
|
185 |
String fromStringCollection:#('hello' 'world' 'how' 'about' 'this') |
|
186 |
" |
|
187 |
||
188 |
"Created: 20.11.1995 / 15:26:59 / cg" |
|
189 |
! |
|
190 |
||
191 |
fromStringCollection:aCollectionOfStrings separatedBy:aSeparatorString |
|
192 |
"return new string formed by concatenating a copy of the argument, aString" |
|
193 |
||
17625 | 194 |
|stream| |
195 |
||
196 |
aCollectionOfStrings do:[:eachString | |
|
20012 | 197 |
stream isNil ifTrue:[ |
198 |
stream := self writeStreamClass with:eachString. |
|
199 |
] ifFalse:[ |
|
200 |
stream |
|
201 |
nextPutAll:aSeparatorString; |
|
202 |
nextPutAll:eachString. |
|
203 |
]. |
|
581 | 204 |
]. |
17700 | 205 |
stream isNil ifTrue:[^ '']. |
17625 | 206 |
^ stream contents |
581 | 207 |
|
208 |
" |
|
209 |
String fromStringCollection:#('hello' 'world' 'how' 'about' 'this') separatedBy:' ' |
|
17625 | 210 |
String fromStringCollection:#('hello' 'world' 'how' 'about' 'this') separatedBy:'Ƞ' |
211 |
Text fromStringCollection:{'hello'. 'world'. 'how' allBold. 'about'. 'this'. 'äöü'} separatedBy:'Ƞ' |
|
581 | 212 |
" |
213 |
||
214 |
"Created: 20.11.1995 / 15:32:17 / cg" |
|
608 | 215 |
! |
216 |
||
5757 | 217 |
fromUTF8Bytes:aByteCollection |
5758 | 218 |
"return a new string which represents the characters as decoded |
219 |
from the utf8 encoded bytes, aByteCollection. |
|
220 |
Returns either a normal String, or a TwoByteString instance. |
|
6810 | 221 |
Only useful, when reading twoByteStrings from external sources. |
222 |
This only handles up-to 16bit characters" |
|
5757 | 223 |
|
7876 | 224 |
^ self decodeFromUTF8:aByteCollection. |
5757 | 225 |
|
226 |
" |
|
8874 | 227 |
CharacterArray fromUTF8Bytes:#[ 16r41 16r42 ] |
228 |
CharacterArray fromUTF8Bytes:#[ 16rC1 16r02 ] |
|
229 |
CharacterArray fromUTF8Bytes:#[ 16rE0 16r81 16r02 ] |
|
6810 | 230 |
CharacterArray fromUTF8Bytes:#[ 16rEF 16rBF 16rBF ] |
231 |
||
232 |
rfc2279 examples: |
|
8874 | 233 |
CharacterArray fromUTF8Bytes:#[ 16r41 16rE2 16r89 16rA2 16rCE 16r91 16r2E ] |
234 |
CharacterArray fromUTF8Bytes:#[ 16rED 16r95 16r9C 16rEA 16rB5 16rAD 16rEC 16r96 16rB4 ] |
|
235 |
CharacterArray fromUTF8Bytes:#[ 16rE6 16r97 16rA5 16rE6 16r9C 16rAC 16rE8 16rAA 16r9E ] |
|
6810 | 236 |
|
237 |
invalid: |
|
8874 | 238 |
CharacterArray fromUTF8Bytes:#[ 16rC0 16r80 ] |
239 |
CharacterArray fromUTF8Bytes:#[ 16rE0 16r80 16r80 ] |
|
5757 | 240 |
" |
241 |
! |
|
242 |
||
608 | 243 |
new |
244 |
"return a new empty string" |
|
245 |
||
246 |
^ self basicNew:0 |
|
9425 | 247 |
! |
248 |
||
249 |
readSmalltalkStringFrom:aStreamOrString onError:exceptionBlock |
|
250 |
"read & return the next String from the (character-)stream aStream; |
|
251 |
skipping all whitespace first; return the value of exceptionBlock, |
|
252 |
if no string can be read. The sequence of characters as read from the |
|
253 |
stream must be one as stored via storeOn: or storeString." |
|
254 |
||
255 |
|str collected char| |
|
256 |
||
257 |
str := aStreamOrString readStream. |
|
258 |
||
259 |
"skip whiteSpace" |
|
260 |
str skipSeparators. |
|
11320 | 261 |
|
14452
ef3591302afa
changed: #readSmalltalkStringFrom:onError:
Stefan Vogel <sv@exept.de>
parents:
14451
diff
changeset
|
262 |
(str peekOrNil == $') ifTrue:[ |
20012 | 263 |
str next. |
264 |
collected := self writeStream. |
|
265 |
[str atEnd] whileFalse:[ |
|
266 |
char := str next. |
|
267 |
char == $' ifTrue:[ |
|
268 |
"/ look for another quote |
|
269 |
str peekOrNil ~~ $' ifTrue:[ |
|
270 |
"end of string reached" |
|
271 |
^ collected contents. |
|
272 |
]. |
|
273 |
"eat doubled quote" |
|
274 |
str next. |
|
275 |
]. |
|
276 |
((char ~~ Character return) or:[str peekOrNil ~~ Character lf]) ifTrue:[ |
|
277 |
"compress CRLF to LF, but keep a single CR" |
|
278 |
collected nextPut:char. |
|
279 |
]. |
|
280 |
]. |
|
281 |
"if we come here, we reached the end without finding a closing $'" |
|
9425 | 282 |
]. |
283 |
^ exceptionBlock value |
|
284 |
||
285 |
" |
|
286 |
String readFrom:('''hello world''' readStream) |
|
287 |
String readFrom:('''hello '''' world''' readStream) |
|
288 |
String readFrom:('1 ''hello'' ' readStream) |
|
289 |
String readFrom:('1 ''hello'' ' readStream) onError:['foobar'] |
|
290 |
" |
|
291 |
||
292 |
"Created: / 05-07-2006 / 16:41:04 / cg" |
|
10045
bbd328940fb4
care for cr-lf when reading a smalltalk string
Claus Gittinger <cg@exept.de>
parents:
9845
diff
changeset
|
293 |
"Modified: / 06-10-2006 / 14:05:32 / cg" |
11243
4bf099f5bb56
CharacterArray class>>#writeStream - answer a CharacterWriteStrem
Stefan Vogel <sv@exept.de>
parents:
11229
diff
changeset
|
294 |
! |
4bf099f5bb56
CharacterArray class>>#writeStream - answer a CharacterWriteStrem
Stefan Vogel <sv@exept.de>
parents:
11229
diff
changeset
|
295 |
|
13201 | 296 |
writeStreamClass |
17213 | 297 |
"the class used by writeStream. |
298 |
Here, we return CharacterWriteStream, which automatically changes |
|
299 |
the underlying collection to a multiByte string (i.e. UnicodeString). |
|
300 |
So you can stream wide characters into it." |
|
13201 | 301 |
|
302 |
^ CharacterWriteStream |
|
11243
4bf099f5bb56
CharacterArray class>>#writeStream - answer a CharacterWriteStrem
Stefan Vogel <sv@exept.de>
parents:
11229
diff
changeset
|
303 |
|
4bf099f5bb56
CharacterArray class>>#writeStream - answer a CharacterWriteStrem
Stefan Vogel <sv@exept.de>
parents:
11229
diff
changeset
|
304 |
" |
4bf099f5bb56
CharacterArray class>>#writeStream - answer a CharacterWriteStrem
Stefan Vogel <sv@exept.de>
parents:
11229
diff
changeset
|
305 |
String writeStream |
4bf099f5bb56
CharacterArray class>>#writeStream - answer a CharacterWriteStrem
Stefan Vogel <sv@exept.de>
parents:
11229
diff
changeset
|
306 |
" |
13201 | 307 |
|
308 |
"Created: / 09-01-2011 / 10:37:57 / cg" |
|
64 | 309 |
! ! |
310 |
||
9265 | 311 |
!CharacterArray class methodsFor:'Compatibility-VW'! |
312 |
||
313 |
fromIntegerArray: anArray |
|
314 |
"Answer a new instance of the receiver that is created from the argument, anArray." |
|
315 |
||
316 |
| new | |
|
317 |
||
318 |
new := self new: anArray size. |
|
319 |
1 to: anArray size do:[:index | |
|
20012 | 320 |
new at: index put: (anArray at: index) asCharacter |
9265 | 321 |
]. |
322 |
^new |
|
323 |
||
324 |
" |
|
325 |
String fromIntegerArray: #[8 127] |
|
326 |
" |
|
327 |
! ! |
|
328 |
||
2728
7569e45a4d15
raise error signal if anything goes wrong while decoding.
Claus Gittinger <cg@exept.de>
parents:
2721
diff
changeset
|
329 |
!CharacterArray class methodsFor:'Signal constants'! |
7569e45a4d15
raise error signal if anything goes wrong while decoding.
Claus Gittinger <cg@exept.de>
parents:
2721
diff
changeset
|
330 |
|
7569e45a4d15
raise error signal if anything goes wrong while decoding.
Claus Gittinger <cg@exept.de>
parents:
2721
diff
changeset
|
331 |
decodingFailedSignal |
2833 | 332 |
"return the signal, raised when decoding of a string is not possible |
2728
7569e45a4d15
raise error signal if anything goes wrong while decoding.
Claus Gittinger <cg@exept.de>
parents:
2721
diff
changeset
|
333 |
due to invalid characters contained in the source. |
7569e45a4d15
raise error signal if anything goes wrong while decoding.
Claus Gittinger <cg@exept.de>
parents:
2721
diff
changeset
|
334 |
This may happen for example, if a non EUC coded 8-bit string |
7569e45a4d15
raise error signal if anything goes wrong while decoding.
Claus Gittinger <cg@exept.de>
parents:
2721
diff
changeset
|
335 |
is attempted to be decoded into a JIS string." |
7569e45a4d15
raise error signal if anything goes wrong while decoding.
Claus Gittinger <cg@exept.de>
parents:
2721
diff
changeset
|
336 |
|
7569e45a4d15
raise error signal if anything goes wrong while decoding.
Claus Gittinger <cg@exept.de>
parents:
2721
diff
changeset
|
337 |
^ DecodingFailedSignal |
7569e45a4d15
raise error signal if anything goes wrong while decoding.
Claus Gittinger <cg@exept.de>
parents:
2721
diff
changeset
|
338 |
|
2833 | 339 |
"Created: 28.6.1997 / 20:09:55 / cg" |
340 |
"Modified: 3.8.1997 / 18:16:47 / cg" |
|
341 |
! |
|
342 |
||
343 |
encodingFailedSignal |
|
344 |
"return the (query-) signal, raised when encoding of a string is not possible |
|
345 |
due to invalid characters contained in the source." |
|
346 |
||
347 |
^ EncodingFailedSignal |
|
348 |
||
2728
7569e45a4d15
raise error signal if anything goes wrong while decoding.
Claus Gittinger <cg@exept.de>
parents:
2721
diff
changeset
|
349 |
"Modified: 28.6.1997 / 20:09:35 / cg" |
2833 | 350 |
"Created: 3.8.1997 / 18:16:40 / cg" |
2728
7569e45a4d15
raise error signal if anything goes wrong while decoding.
Claus Gittinger <cg@exept.de>
parents:
2721
diff
changeset
|
351 |
! ! |
7569e45a4d15
raise error signal if anything goes wrong while decoding.
Claus Gittinger <cg@exept.de>
parents:
2721
diff
changeset
|
352 |
|
20155 | 353 |
|
1849
9f35f5934ec6
added msdos-latin1 en/decoder (code = #msdos850)
Claus Gittinger <cg@exept.de>
parents:
1669
diff
changeset
|
354 |
!CharacterArray class methodsFor:'cleanup'! |
991
f0b45a4b82d2
first attempt in encoding/decoding for different character representations (mac/msdos etc.)
Claus Gittinger <cg@exept.de>
parents:
986
diff
changeset
|
355 |
|
f0b45a4b82d2
first attempt in encoding/decoding for different character representations (mac/msdos etc.)
Claus Gittinger <cg@exept.de>
parents:
986
diff
changeset
|
356 |
lowSpaceCleanup |
f0b45a4b82d2
first attempt in encoding/decoding for different character representations (mac/msdos etc.)
Claus Gittinger <cg@exept.de>
parents:
986
diff
changeset
|
357 |
"cleanup in low-memory situations" |
f0b45a4b82d2
first attempt in encoding/decoding for different character representations (mac/msdos etc.)
Claus Gittinger <cg@exept.de>
parents:
986
diff
changeset
|
358 |
|
f0b45a4b82d2
first attempt in encoding/decoding for different character representations (mac/msdos etc.)
Claus Gittinger <cg@exept.de>
parents:
986
diff
changeset
|
359 |
DecoderTables := EncoderTables := nil |
f0b45a4b82d2
first attempt in encoding/decoding for different character representations (mac/msdos etc.)
Claus Gittinger <cg@exept.de>
parents:
986
diff
changeset
|
360 |
|
f0b45a4b82d2
first attempt in encoding/decoding for different character representations (mac/msdos etc.)
Claus Gittinger <cg@exept.de>
parents:
986
diff
changeset
|
361 |
" |
f0b45a4b82d2
first attempt in encoding/decoding for different character representations (mac/msdos etc.)
Claus Gittinger <cg@exept.de>
parents:
986
diff
changeset
|
362 |
CharacterArray lowSpaceCleanup |
f0b45a4b82d2
first attempt in encoding/decoding for different character representations (mac/msdos etc.)
Claus Gittinger <cg@exept.de>
parents:
986
diff
changeset
|
363 |
" |
f0b45a4b82d2
first attempt in encoding/decoding for different character representations (mac/msdos etc.)
Claus Gittinger <cg@exept.de>
parents:
986
diff
changeset
|
364 |
|
f0b45a4b82d2
first attempt in encoding/decoding for different character representations (mac/msdos etc.)
Claus Gittinger <cg@exept.de>
parents:
986
diff
changeset
|
365 |
"Created: 22.2.1996 / 16:30:30 / cg" |
f0b45a4b82d2
first attempt in encoding/decoding for different character representations (mac/msdos etc.)
Claus Gittinger <cg@exept.de>
parents:
986
diff
changeset
|
366 |
"Modified: 22.2.1996 / 17:58:05 / cg" |
f0b45a4b82d2
first attempt in encoding/decoding for different character representations (mac/msdos etc.)
Claus Gittinger <cg@exept.de>
parents:
986
diff
changeset
|
367 |
! ! |
f0b45a4b82d2
first attempt in encoding/decoding for different character representations (mac/msdos etc.)
Claus Gittinger <cg@exept.de>
parents:
986
diff
changeset
|
368 |
|
7421 | 369 |
!CharacterArray class methodsFor:'encoding & decoding'! |
1202
200f7cd0a457
moved encode/decode stuff into class methods / added big5/gb support
Claus Gittinger <cg@exept.de>
parents:
1145
diff
changeset
|
370 |
|
7876 | 371 |
decodeFromUTF8:aStringOrByteCollection |
19837 | 372 |
"given a string in UTF8 encoding, |
373 |
return a new string containing the same characters, in Unicode encoding. |
|
374 |
Returns either a normal String, a Unicode16String or a Unicode32String instance. |
|
375 |
This is only useful, when reading from external sources or communicating with |
|
376 |
other systems |
|
377 |
(ST/X never uses utf8 internally, but always uses strings of fully decoded unicode characters). |
|
378 |
This only handles up-to 30bit characters." |
|
379 |
||
380 |
|sz anyAbove7BitAscii nBitsRequired |
|
381 |
ascii "{ Class: SmallInteger }" |
|
382 |
byte "{ Class: SmallInteger }" |
|
383 |
lastIdx "{ Class: SmallInteger }" |
|
384 |
srcIdx "{ Class: SmallInteger }" |
|
385 |
idx "{ Class: SmallInteger }" |
|
386 |
nFollowBytes "{ Class: SmallInteger }" |
|
387 |
minValue "{ Class: SmallInteger }" |
|
388 |
newString| |
|
389 |
||
390 |
"/ fast track, also avoid creation of new strings if aStringOrByteCollection is already a 7-bit string |
|
391 |
aStringOrByteCollection containsNon7BitAscii ifFalse:[ |
|
392 |
^ aStringOrByteCollection asSingleByteString |
|
393 |
]. |
|
394 |
||
395 |
nBitsRequired := 8. |
|
396 |
anyAbove7BitAscii := false. |
|
397 |
sz := 0. |
|
398 |
||
399 |
lastIdx := aStringOrByteCollection size. |
|
400 |
srcIdx := 1. |
|
401 |
||
402 |
"first determine the string size and max element size. |
|
403 |
Check for UTF-8 confomance on the fly." |
|
404 |
[srcIdx <= lastIdx] whileTrue:[ |
|
405 |
byte := ascii := aStringOrByteCollection byteAt:srcIdx. |
|
406 |
srcIdx := srcIdx + 1. |
|
407 |
(byte bitAnd:16r80) ~~ 0 ifTrue:[ |
|
408 |
anyAbove7BitAscii := true. |
|
409 |
(byte bitAnd:2r11100000) == 2r11000000 ifTrue:[ |
|
410 |
"/ 80 .. 7FF |
|
411 |
ascii := byte bitAnd:2r00011111. |
|
412 |
nFollowBytes := 1. |
|
413 |
minValue := 16r80. |
|
414 |
] ifFalse:[(byte bitAnd:2r11110000) == 2r11100000 ifTrue:[ |
|
415 |
"/ 800 .. FFFF |
|
416 |
ascii := byte bitAnd:2r00001111. |
|
417 |
nFollowBytes := 2. |
|
418 |
minValue := 16r800. |
|
419 |
] ifFalse:[(byte bitAnd:2r11111000) == 2r11110000 ifTrue:[ |
|
420 |
"/ 10000 .. 1FFFFF |
|
421 |
ascii := byte bitAnd:2r00000111. |
|
422 |
nFollowBytes := 3. |
|
423 |
minValue := 16r10000. |
|
424 |
] ifFalse:[(byte bitAnd:2r11111100) == 2r11111000 ifTrue:[ |
|
425 |
"/ 200000 .. 3FFFFFF |
|
426 |
ascii := byte bitAnd:2r00000011. |
|
427 |
nFollowBytes := 4. |
|
428 |
minValue := 16r200000. |
|
429 |
] ifFalse:[(byte bitAnd:2r11111110) == 2r11111100 ifTrue:[ |
|
430 |
"/ 4000000 .. 7FFFFFFF |
|
431 |
ascii := byte bitAnd:2r00000001. |
|
432 |
ascii ~~ 0 ifTrue:[ |
|
433 |
"/ ST/X can only represent 30 bit unicode characters. |
|
434 |
"/ but the max unicode character is defined as 16r10 FFFF anyway |
|
435 |
^ DecodingError newException |
|
436 |
defaultValue:aStringOrByteCollection; |
|
437 |
raiseRequestWith:aStringOrByteCollection errorString:'unicode character out of range'. |
|
438 |
]. |
|
439 |
nFollowBytes := 5. |
|
440 |
minValue := 16r4000000. |
|
441 |
] ifFalse:[ |
|
442 |
^ DecodingError newException |
|
443 |
defaultValue:aStringOrByteCollection; |
|
444 |
raiseRequestWith:aStringOrByteCollection errorString:'invalid utf8 encoding'. |
|
445 |
]]]]]. |
|
446 |
||
447 |
nFollowBytes timesRepeat:[ |
|
448 |
byte := aStringOrByteCollection byteAt:srcIdx. |
|
449 |
srcIdx := srcIdx + 1. |
|
450 |
(byte bitAnd:2r11000000) ~~ 2r10000000 ifTrue:[ |
|
451 |
^ DecodingError newException |
|
452 |
defaultValue:aStringOrByteCollection; |
|
453 |
raiseRequestWith:aStringOrByteCollection errorString:'illegal followbyte'. |
|
454 |
]. |
|
455 |
ascii := (ascii bitShift:6) bitOr:(byte bitAnd:2r00111111). |
|
456 |
]. |
|
457 |
ascii > 16rFFFF ifTrue:[ |
|
458 |
nBitsRequired < 32 ifTrue:[nBitsRequired := 32]. |
|
459 |
] ifFalse:[ascii > 16rFF ifTrue:[ |
|
460 |
nBitsRequired < 16 ifTrue:[nBitsRequired := 16]. |
|
461 |
]]. |
|
462 |
ascii < minValue ifTrue:[ |
|
463 |
"encoding a value in a longer utf8-sequence than required is not allowed |
|
464 |
and imposes a security risk" |
|
465 |
^ DecodingError newException |
|
466 |
defaultValue:aStringOrByteCollection; |
|
467 |
raiseRequestWith:aStringOrByteCollection errorString:'overlong utf8 sequence'. |
|
468 |
]. |
|
469 |
]. |
|
470 |
sz := sz + 1. |
|
471 |
]. |
|
472 |
||
473 |
nBitsRequired == 8 ifTrue:[ |
|
474 |
anyAbove7BitAscii ifFalse:[ |
|
475 |
"/ can return the original string |
|
476 |
^ aStringOrByteCollection asSingleByteString. |
|
477 |
]. |
|
478 |
newString := String uninitializedNew:sz |
|
479 |
] ifFalse:[nBitsRequired <= 16 ifTrue:[ |
|
480 |
newString := Unicode16String new:sz |
|
481 |
] ifFalse:[ |
|
482 |
newString := Unicode32String new:sz |
|
483 |
]]. |
|
484 |
||
485 |
idx := srcIdx := 1. |
|
486 |
||
487 |
"now fill the string" |
|
488 |
[srcIdx <= lastIdx] whileTrue:[ |
|
489 |
byte := ascii := aStringOrByteCollection byteAt:srcIdx. |
|
490 |
srcIdx := srcIdx + 1. |
|
491 |
(byte bitAnd:2r10000000) ~~ 0 ifTrue:[ |
|
492 |
(byte bitAnd:2r11100000) == 2r11000000 ifTrue:[ |
|
493 |
ascii := byte bitAnd:2r00011111. |
|
494 |
nFollowBytes := 1. |
|
495 |
] ifFalse:[(byte bitAnd:2r11110000) == 2r11100000 ifTrue:[ |
|
496 |
ascii := byte bitAnd:2r00001111. |
|
497 |
nFollowBytes := 2. |
|
498 |
] ifFalse:[(byte bitAnd:2r11111000) == 2r11110000 ifTrue:[ |
|
499 |
ascii := byte bitAnd:2r00000111. |
|
500 |
nFollowBytes := 3. |
|
501 |
] ifFalse:[(byte bitAnd:2r11111100) == 2r11111000 ifTrue:[ |
|
502 |
"5 byte sequences do not result in valid unicode" |
|
503 |
ascii := byte bitAnd:2r00000011. |
|
504 |
nFollowBytes := 4. |
|
505 |
] ifFalse:[(byte bitAnd:2r11111110) == 2r11111100 ifTrue:[ |
|
506 |
"6 byte sequences do not result in valid unicode" |
|
507 |
ascii := byte bitAnd:2r00000001. |
|
508 |
nFollowBytes := 5. |
|
509 |
]]]]]. |
|
510 |
nFollowBytes timesRepeat:[ |
|
511 |
byte := aStringOrByteCollection byteAt:srcIdx. |
|
512 |
srcIdx := srcIdx + 1. |
|
513 |
ascii := (ascii bitShift:6) bitOr:(byte bitAnd:2r00111111). |
|
514 |
]. |
|
515 |
]. |
|
516 |
newString at:idx put:(Character value:ascii). |
|
517 |
idx := idx + 1. |
|
518 |
]. |
|
519 |
^ newString |
|
520 |
||
521 |
" |
|
522 |
CharacterArray decodeFromUTF8:#[ 16r41 16r42 ] |
|
523 |
CharacterArray decodeFromUTF8:#[ 16rC6 16r8F ] |
|
524 |
CharacterArray decodeFromUTF8:#[ 16rE0 16r81 16r02 ] |
|
525 |
CharacterArray decodeFromUTF8:#[ 16rEF 16rBF 16rBF ] |
|
526 |
||
527 |
rfc2279 (deprecated) examples: |
|
528 |
CharacterArray decodeFromUTF8:#[ 16r41 16rE2 16r89 16rA2 16rCE 16r91 16r2E ] |
|
529 |
CharacterArray decodeFromUTF8:#[ 16rED 16r95 16r9C 16rEA 16rB5 16rAD 16rEC 16r96 16rB4 ] |
|
530 |
CharacterArray decodeFromUTF8:#[ 16rE6 16r97 16rA5 16rE6 16r9C 16rAC 16rE8 16rAA 16r9E ] |
|
531 |
||
532 |
invalid: |
|
533 |
CharacterArray decodeFromUTF8:#[ 16rC0 16r80 ] |
|
534 |
CharacterArray decodeFromUTF8:#[ 16rE0 16r80 16r80 ] |
|
535 |
" |
|
536 |
||
537 |
" |
|
538 |
CharacterArray decodeFromUTF8:#[ 16r41 16r42 ] |
|
539 |
CharacterArray decodeFromUTF8:#[ 16rC1 16r02 ] |
|
540 |
CharacterArray decodeFromUTF8:#[ 16rE0 16r81 16r02 ] |
|
541 |
CharacterArray decodeFromUTF8:#[ 16rEF 16rBF 16rBF ] |
|
7876 | 542 |
|
543 |
rfc2279 examples: |
|
19837 | 544 |
CharacterArray decodeFromUTF8:#[ 16r41 16rE2 16r89 16rA2 16rCE 16r91 16r2E ] |
545 |
CharacterArray decodeFromUTF8:#[ 16rED 16r95 16r9C 16rEA 16rB5 16rAD 16rEC 16r96 16rB4 ] |
|
546 |
CharacterArray decodeFromUTF8:#[ 16rE6 16r97 16rA5 16rE6 16r9C 16rAC 16rE8 16rAA 16r9E ] |
|
7876 | 547 |
|
548 |
invalid: |
|
19837 | 549 |
CharacterArray decodeFromUTF8:#[ 16rC0 16r80 ] |
550 |
CharacterArray decodeFromUTF8:#[ 16rE0 16r80 16r80 ] |
|
7876 | 551 |
" |
20010 | 552 |
! |
553 |
||
20011 | 554 |
setupNormalizationMaps |
555 |
"returns a 2-stage map from ch2 -> ch1 -> mappedChar |
|
556 |
for unicode normalization (i.e. for making combining chars regular ones)" |
|
557 |
||
558 |
|def| |
|
559 |
||
560 |
UnicodeNormalizationMap := Dictionary new. |
|
20013 | 561 |
UnicodeDenormalizationMap := Dictionary new. |
562 |
||
563 |
def := [:combiner :chars :mappedChars | |
|
20011 | 564 |
|d| |
565 |
||
20013 | 566 |
d := UnicodeNormalizationMap at:combiner ifAbsentPut:[Dictionary new]. |
567 |
chars with:mappedChars do:[:ch1 :mappedChar | |
|
568 |
d at:ch1 put:mappedChar. |
|
569 |
UnicodeDenormalizationMap at:mappedChar put:(Unicode16String with:ch1 with:combiner). |
|
570 |
]. |
|
20011 | 571 |
]. |
20014 | 572 |
def value:(Character codePoint:16r0300) "grave" value:'AEIOUaeiouWw' value:'ÀÈÌÒÙàèìòùẀẁ'. |
573 |
def value:(Character codePoint:16r0301) "acute" value:'AEIOUYaeiouyÇçKkMmPpWw' value:'ÁÉÍÓÚÝáéíóúýḈḉḰḱḾḿṔṕẂẃ'. |
|
574 |
def value:(Character codePoint:16r0302) "circumflex" value:'AEIOUaeiouZz' value:'ÂÊÎÔÛâêîôûẐẑ'. |
|
20011 | 575 |
def value:(Character codePoint:16r0303) "tilde" value:'AONaon' value:'ÃÕÑãõñ'. |
20014 | 576 |
def value:(Character codePoint:16r0307) "dot over" value:'BbDdFfHhMmNnPpRrSsTtWwXxYyṢṣ' value:'ḂḃḊḋḞḟḢḣṀṁṄṅṖṗṘṙṠṡṪṫẆẇẊẋẎẏṨṩ'. |
577 |
def value:(Character codePoint:16r0308) "diaresis" value:'AEIOUaeiouyHhWwXxt' value:'ÄËÏÖÜäëïöüÿḦḧẄẅẌẍẗ'. |
|
578 |
def value:(Character codePoint:16r030A) "ring" value:'Aawy' value:'Ååẘẙ'. |
|
579 |
def value:(Character codePoint:16r0323) "dot under" value:'AaBbDdEeIiHhKkLlMmNnOoRrSsTtUuVvWwZzṠṡ' value:'ẠạḄḅḌḍẸẹỊịḤḥḲḳḶḷṂṃṆṇỌọṚṛṢṣṬṭỤụṾṿẈẉẒẓṨṩ'. |
|
20011 | 580 |
def value:(Character codePoint:16r0327) "cedilla" value:'Cc' value:'Çç'. |
581 |
||
582 |
" |
|
20014 | 583 |
self setupNormalizationMaps |
20011 | 584 |
" |
585 |
! |
|
586 |
||
587 |
unicodeDenormalizationMap |
|
588 |
"returns a 2-stage map from ch2 -> ch1 -> mappedChar |
|
589 |
for unicode normalization (i.e. for making combining chars regular ones)" |
|
590 |
||
591 |
UnicodeDenormalizationMap isNil ifTrue:[ |
|
592 |
self setupNormalizationMaps |
|
593 |
]. |
|
594 |
^ UnicodeDenormalizationMap |
|
595 |
||
596 |
" |
|
597 |
self unicodeDenormalizationMap |
|
598 |
" |
|
599 |
! |
|
600 |
||
20010 | 601 |
unicodeNormalizationMap |
602 |
"returns a 2-stage map from ch2 -> ch1 -> mappedChar |
|
603 |
for unicode normalization (i.e. for making combining chars regular ones)" |
|
604 |
||
605 |
UnicodeNormalizationMap isNil ifTrue:[ |
|
20011 | 606 |
self setupNormalizationMaps |
20010 | 607 |
]. |
608 |
^ UnicodeNormalizationMap |
|
609 |
||
610 |
" |
|
611 |
self unicodeNormalizationMap |
|
612 |
" |
|
443 | 613 |
! ! |
614 |
||
20155 | 615 |
|
1849
9f35f5934ec6
added msdos-latin1 en/decoder (code = #msdos850)
Claus Gittinger <cg@exept.de>
parents:
1669
diff
changeset
|
616 |
!CharacterArray class methodsFor:'pattern matching'! |
64 | 617 |
|
5342
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
618 |
matchEscapeCharacter |
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
619 |
"return the character used to escape a matchCharacter |
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
620 |
(i.e. make it a regular character in a matchPattern)" |
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
621 |
|
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
622 |
^ $\ |
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
623 |
! |
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
624 |
|
15734 | 625 |
matchScan:matchScanArray from:matchStart to:matchStop with:aString from:start to:stop caseSensitive:caseSensitive |
327 | 626 |
"helper for match; return true if the characters from start to stop in |
627 |
aString are matching the scan in matchScan from matchStart to matchStop. |
|
328 | 628 |
The matchScan is as created by asMatchScanArray. |
327 | 629 |
|
8874 | 630 |
This algorithm is not at all the most efficient; |
631 |
for heavy duty pattern matching, an interface (primitive) to the regex |
|
328 | 632 |
pattern matching package should be added." |
327 | 633 |
|
8874 | 634 |
|matchEntry |
327 | 635 |
mStart "{ Class: SmallInteger }" |
636 |
mStop "{ Class: SmallInteger }" |
|
637 |
sStart "{ Class: SmallInteger }" |
|
638 |
sStop "{ Class: SmallInteger }" |
|
639 |
mSize "{ Class: SmallInteger }" |
|
640 |
sSize "{ Class: SmallInteger }" |
|
641 |
index "{ Class: SmallInteger }" |
|
2561 | 642 |
quickCheck matchLast nextMatchEntry |
986 | 643 |
checkChar included| |
327 | 644 |
|
645 |
mStart := matchStart. |
|
646 |
mStop := matchStop. |
|
647 |
sStart := start. |
|
648 |
sStop := stop. |
|
649 |
||
16245 | 650 |
[ |
8874 | 651 |
"/ Transcript showCR:('match: ''' , (aString copyFrom:sStart to:sStop) , |
3537 | 652 |
"/ ''' against:' , (matchScanArray copyFrom:mStart to:mStop) printString). |
653 |
||
20012 | 654 |
mSize := mStop - mStart + 1. |
655 |
sSize := sStop - sStart + 1. |
|
656 |
||
657 |
"empty strings match" |
|
658 |
(mSize == 0) ifTrue:[^ (sSize == 0)]. |
|
659 |
||
660 |
matchEntry := matchScanArray at:mStart. |
|
661 |
||
662 |
"/ the most common case first: |
|
663 |
(sSize ~~ 0 |
|
664 |
and:[(checkChar := (aString at:sStart)) = matchEntry]) ifTrue:[ |
|
665 |
"advance by one and continue" |
|
666 |
mStart := mStart + 1. |
|
667 |
sStart := sStart + 1 |
|
668 |
] ifFalse:[ |
|
669 |
(matchEntry == #any) ifTrue:[ |
|
670 |
"restString empty -> no match" |
|
671 |
(sSize == 0) ifTrue:[^ false]. |
|
672 |
"# matches single character" |
|
673 |
((sSize == 1) and:[mSize == 1]) ifTrue:[^ true]. |
|
674 |
"advance by one and continue" |
|
675 |
mStart := mStart + 1. |
|
676 |
sStart := sStart + 1 |
|
677 |
] ifFalse:[ |
|
678 |
(matchEntry == #anyString) ifTrue:[ |
|
679 |
"* alone matches anything" |
|
680 |
(mSize == 1) ifTrue:[^ true]. |
|
681 |
"restString empty & matchString not empty -> no match" |
|
682 |
(sSize == 0) ifTrue:[^ false]. |
|
683 |
||
684 |
" |
|
685 |
try to avoid some of the recursion by checking last |
|
686 |
character and continue with shortened strings if possible |
|
687 |
" |
|
688 |
quickCheck := false. |
|
689 |
(mStop >= mStart) ifTrue:[ |
|
690 |
matchLast := matchScanArray at:mStop. |
|
691 |
(matchLast ~~ #anyString) ifTrue:[ |
|
692 |
(matchLast == #any) ifTrue:[ |
|
693 |
quickCheck := true |
|
694 |
] ifFalse:[ |
|
695 |
matchLast == (aString at:sStop) ifTrue:[ |
|
696 |
quickCheck := true |
|
697 |
] ifFalse:[ |
|
698 |
matchLast isString ifTrue:[ |
|
699 |
quickCheck := matchLast includes:(aString at:sStop) |
|
700 |
] |
|
701 |
] |
|
702 |
] |
|
703 |
] |
|
704 |
]. |
|
705 |
quickCheck ifTrue:[ |
|
706 |
" |
|
707 |
quickCheck ok, advance from the right |
|
708 |
" |
|
709 |
mStop := mStop - 1. |
|
710 |
sStop := sStop - 1 |
|
711 |
] ifFalse:[ |
|
712 |
"/ no quick check; |
|
713 |
"/ look for the next character(s) |
|
714 |
"/ and try matching there |
|
715 |
"/ (to avoid recursion) |
|
716 |
||
717 |
mStart < mStop ifTrue:[ |
|
718 |
nextMatchEntry := matchScanArray at:mStart+1. |
|
719 |
nextMatchEntry isCharacter ifTrue:[ |
|
720 |
sStart <= sStop ifTrue:[ |
|
721 |
[ |
|
722 |
caseSensitive ifTrue:[ |
|
723 |
index := aString indexOf:nextMatchEntry startingAt:sStart |
|
724 |
] ifFalse:[ |
|
725 |
index := aString findFirst:[:c | c asLowercase = nextMatchEntry asLowercase] |
|
726 |
startingAt:sStart. |
|
727 |
]. |
|
728 |
(index == 0 or:[index > sStop]) ifTrue:[ |
|
729 |
^ false |
|
730 |
]. |
|
731 |
(self matchScan:matchScanArray |
|
732 |
from:(mStart + 1) |
|
733 |
to:mStop |
|
734 |
with:aString |
|
735 |
from:index |
|
736 |
to:sStop |
|
737 |
caseSensitive:caseSensitive |
|
738 |
) ifTrue:[ |
|
739 |
^ true |
|
740 |
]. |
|
741 |
sStart := index + 1. |
|
742 |
] loop. |
|
743 |
] |
|
744 |
] |
|
745 |
]. |
|
746 |
||
747 |
" |
|
748 |
no quick check possible; |
|
749 |
loop over all possible substrings |
|
750 |
" |
|
751 |
index := sStart. |
|
752 |
[index <= sStop] whileTrue:[ |
|
753 |
(self matchScan:matchScanArray |
|
754 |
from:(mStart + 1) |
|
755 |
to:mStop |
|
756 |
with:aString |
|
757 |
from:index |
|
758 |
to:sStop |
|
759 |
caseSensitive:caseSensitive |
|
760 |
) ifTrue:[ |
|
761 |
^ true |
|
762 |
]. |
|
763 |
index := index + 1 |
|
764 |
]. |
|
765 |
^ false |
|
766 |
]. |
|
767 |
] ifFalse:[ |
|
768 |
(matchEntry isString) ifTrue:[ |
|
769 |
"testString empty -> no match" |
|
770 |
(sSize == 0) ifTrue:[^ false]. |
|
771 |
||
772 |
included := false. |
|
773 |
"/ checkChar := aString at:sStart. |
|
774 |
included := matchEntry includes:checkChar. |
|
775 |
included ifFalse:[ |
|
776 |
caseSensitive ifFalse:[ |
|
777 |
checkChar isUppercase ifTrue:[ |
|
778 |
included := matchEntry includes:checkChar asLowercase. |
|
779 |
] ifFalse:[ |
|
780 |
included := matchEntry includes:checkChar asUppercase. |
|
781 |
] |
|
782 |
]. |
|
783 |
]. |
|
784 |
mStart := mStart + 1. |
|
785 |
mSize := mSize - 1. |
|
786 |
included ifFalse:[^ false]. |
|
787 |
||
788 |
((sSize == 1) and:[mSize == 0]) ifTrue:[^ true]. |
|
789 |
] ifFalse:[ |
|
790 |
"/ must be single character |
|
791 |
||
792 |
"testString empty ?" |
|
793 |
(sSize == 0) ifTrue:[^ false]. |
|
794 |
||
795 |
"first characters equal ?" |
|
796 |
"/ checkChar := aString at:sStart. |
|
797 |
caseSensitive ifTrue:[^ false]. |
|
798 |
(checkChar asUppercase ~= matchEntry asUppercase) ifTrue:[^ false]. |
|
799 |
||
800 |
"advance and continue" |
|
801 |
mStart := mStart + 1. |
|
802 |
]. |
|
803 |
"cut off 1st char and continue" |
|
804 |
sStart := sStart + 1 |
|
805 |
] |
|
806 |
] |
|
807 |
] |
|
16245 | 808 |
] loop. |
2519
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
809 |
|
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
810 |
" |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
811 |
|scanArray s| |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
812 |
|
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
813 |
scanArray := self matchScanArrayFrom:'*hello'. |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
814 |
s := 'foo bar hello world'. |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
815 |
CharacterArray |
20012 | 816 |
matchScan:scanArray |
817 |
from:1 |
|
818 |
to:scanArray size |
|
819 |
with:s |
|
820 |
from:1 |
|
821 |
to:s size |
|
822 |
caseSensitive:true |
|
2519
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
823 |
" |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
824 |
" |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
825 |
|scanArray s| |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
826 |
|
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
827 |
scanArray := self matchScanArrayFrom:'*hello*'. |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
828 |
s := 'foo bar hello world'. |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
829 |
CharacterArray |
20012 | 830 |
matchScan:scanArray |
831 |
from:1 |
|
832 |
to:scanArray size |
|
833 |
with:s |
|
834 |
from:1 |
|
835 |
to:s size |
|
836 |
caseSensitive:true |
|
15734 | 837 |
" |
838 |
||
839 |
"Modified: / 24-07-2011 / 07:17:03 / cg" |
|
840 |
"Modified (comment): / 24-07-2011 / 08:55:14 / cg" |
|
841 |
! |
|
842 |
||
843 |
matchScan:matchScanArray from:matchStart to:matchStop with:aString from:start to:stop ignoreCase:ignoreCase |
|
844 |
<resource: #obsolete> |
|
845 |
"helper for match; return true if the characters from start to stop in |
|
846 |
aString are matching the scan in matchScan from matchStart to matchStop. |
|
847 |
The matchScan is as created by asMatchScanArray. |
|
848 |
||
849 |
This algorithm is not at all the most efficient; |
|
850 |
for heavy duty pattern matching, an interface (primitive) to the regex |
|
851 |
pattern matching package should be added." |
|
852 |
||
853 |
^ self |
|
20012 | 854 |
matchScan:matchScanArray from:matchStart to:matchStop with:aString from:start to:stop caseSensitive:ignoreCase not |
15734 | 855 |
|
856 |
" |
|
857 |
|scanArray s| |
|
858 |
||
859 |
scanArray := self matchScanArrayFrom:'*hello'. |
|
860 |
s := 'foo bar hello world'. |
|
861 |
CharacterArray |
|
20012 | 862 |
matchScan:scanArray |
863 |
from:1 |
|
864 |
to:scanArray size |
|
865 |
with:s |
|
866 |
from:1 |
|
867 |
to:s size |
|
868 |
ignoreCase:false |
|
15734 | 869 |
" |
870 |
" |
|
871 |
|scanArray s| |
|
872 |
||
873 |
scanArray := self matchScanArrayFrom:'*hello*'. |
|
874 |
s := 'foo bar hello world'. |
|
875 |
CharacterArray |
|
20012 | 876 |
matchScan:scanArray |
877 |
from:1 |
|
878 |
to:scanArray size |
|
879 |
with:s |
|
880 |
from:1 |
|
881 |
to:s size |
|
882 |
ignoreCase:false |
|
13520 | 883 |
" |
884 |
||
885 |
"Modified: / 24-07-2011 / 07:17:03 / cg" |
|
886 |
"Modified (comment): / 24-07-2011 / 08:55:14 / cg" |
|
608 | 887 |
! |
888 |
||
889 |
matchScanArrayFrom:aString |
|
890 |
"scan a pattern string and decompose it into a scanArray. |
|
891 |
This is processed faster (especially with character ranges), and |
|
892 |
can also be reused later. (if the same pattern is to be searched again)" |
|
893 |
||
9282
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
894 |
^ self matchScanArrayFrom:aString escapeCharacter:(self matchEscapeCharacter) |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
895 |
|
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
896 |
" |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
897 |
String matchScanArrayFrom:'*ute*' |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
898 |
String matchScanArrayFrom:'**ute**' |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
899 |
String matchScanArrayFrom:'*uter' |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
900 |
String matchScanArrayFrom:'\*uter' |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
901 |
String matchScanArrayFrom:'[cC]#mpute[rR]' |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
902 |
String matchScanArrayFrom:'[abcd]*' |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
903 |
String matchScanArrayFrom:'[a-k]*' |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
904 |
String matchScanArrayFrom:'*some*compl*ern*' |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
905 |
String matchScanArrayFrom:'[a-' |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
906 |
String matchScanArrayFrom:'[a-zA-Z]' |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
907 |
String matchScanArrayFrom:'[a-z01234A-Z]' |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
908 |
" |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
909 |
|
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
910 |
"Modified: 2.4.1997 / 16:20:29 / cg" |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
911 |
! |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
912 |
|
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
913 |
matchScanArrayFrom:aString escapeCharacter:escape |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
914 |
"scan a pattern string and decompose it into a scanArray. |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
915 |
This is processed faster (especially with character ranges), and |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
916 |
can also be reused later. (if the same pattern is to be searched again)" |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
917 |
|
8874 | 918 |
|coll |
608 | 919 |
idx "{ Class: SmallInteger }" |
9282
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
920 |
end c1 c2 matchSet previous| |
5342
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
921 |
|
5487 | 922 |
previous := nil. |
608 | 923 |
|
924 |
coll := OrderedCollection new. |
|
925 |
idx := 1. end := aString size. |
|
926 |
[idx <= end] whileTrue:[ |
|
20012 | 927 |
|char this| |
928 |
||
929 |
char := aString at:idx. |
|
930 |
char == $* ifTrue:[ |
|
931 |
previous ~~ #anyString ifTrue:[ |
|
932 |
this := #anyString |
|
933 |
] |
|
934 |
] ifFalse:[ |
|
935 |
char == $# ifTrue:[ |
|
936 |
previous ~~ #anyString ifTrue:[ |
|
937 |
this := #any |
|
938 |
] |
|
939 |
] ifFalse:[ |
|
940 |
char == $[ ifTrue:[ |
|
941 |
matchSet := IdentitySet new. |
|
942 |
idx := idx + 1. |
|
943 |
idx > end ifTrue:[^ nil]. |
|
944 |
char := aString at:idx. |
|
945 |
c1 := nil. |
|
946 |
[char ~~ $]] whileTrue:[ |
|
947 |
((char == $-) and:[c1 notNil]) ifTrue:[ |
|
948 |
idx := idx + 1. |
|
949 |
idx > end ifTrue:[^ nil]. |
|
950 |
c2 := aString at:idx. |
|
951 |
c1 to:c2 do:[:c | matchSet add:c]. |
|
952 |
c1 := nil. |
|
953 |
idx := idx + 1. |
|
954 |
] ifFalse:[ |
|
955 |
(char ~~ $]) ifTrue:[ |
|
956 |
matchSet add:char. |
|
957 |
c1 := char. |
|
958 |
idx := idx + 1 |
|
959 |
] |
|
960 |
]. |
|
961 |
idx > end ifTrue:[^ nil]. |
|
962 |
char := aString at:idx |
|
963 |
]. |
|
964 |
this := matchSet asString |
|
965 |
] ifFalse:[ |
|
966 |
char == escape ifTrue:[ |
|
967 |
idx := idx + 1. |
|
968 |
idx > end ifTrue:[ |
|
969 |
"/ mhmh - what should we do here ? |
|
970 |
this := char |
|
971 |
] ifFalse:[ |
|
972 |
this := aString at:idx. |
|
973 |
] |
|
974 |
] ifFalse:[ |
|
975 |
this := char |
|
976 |
] |
|
977 |
] |
|
978 |
] |
|
979 |
]. |
|
980 |
this notNil ifTrue:[coll add:this. previous := this]. |
|
981 |
idx := idx + 1 |
|
608 | 982 |
]. |
983 |
||
984 |
^ coll asArray |
|
985 |
||
986 |
" |
|
8874 | 987 |
String matchScanArrayFrom:'*ute*' |
988 |
String matchScanArrayFrom:'**ute**' |
|
989 |
String matchScanArrayFrom:'*uter' |
|
990 |
String matchScanArrayFrom:'\*uter' |
|
991 |
String matchScanArrayFrom:'[cC]#mpute[rR]' |
|
992 |
String matchScanArrayFrom:'[abcd]*' |
|
993 |
String matchScanArrayFrom:'[a-k]*' |
|
994 |
String matchScanArrayFrom:'*some*compl*ern*' |
|
995 |
String matchScanArrayFrom:'[a-' |
|
996 |
String matchScanArrayFrom:'[a-zA-Z]' |
|
997 |
String matchScanArrayFrom:'[a-z01234A-Z]' |
|
608 | 998 |
" |
2519
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
999 |
|
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
1000 |
"Modified: 2.4.1997 / 16:20:29 / cg" |
9192 | 1001 |
! ! |
1002 |
||
1003 |
!CharacterArray class methodsFor:'queries'! |
|
1004 |
||
1005 |
isAbstract |
|
11229 | 1006 |
"Return if this class is an abstract class. |
1007 |
True is returned for CharacterArray here; false for subclasses. |
|
19464 | 1008 |
Abstract subclasses must redefine this again." |
11229 | 1009 |
|
9192 | 1010 |
^ self == CharacterArray |
18597 | 1011 |
! |
1012 |
||
1013 |
speciesForCharacterSize:characterSize |
|
1014 |
"answer the class, that is able to hold characters of size characterSize" |
|
1015 |
||
1016 |
characterSize <= 8 ifTrue:[^ String]. |
|
1017 |
characterSize <= 16 ifTrue:[^ Unicode16String]. |
|
1018 |
^ Unicode32String |
|
9192 | 1019 |
! ! |
1020 |
||
20155 | 1021 |
|
9192 | 1022 |
!CharacterArray methodsFor:'Compatibility-ANSI'! |
1023 |
||
1024 |
addLineDelimiters |
|
1025 |
"Ansi compatibility - same as withCRs" |
|
1026 |
||
1027 |
^ self withCRs |
|
1028 |
||
1029 |
"Modified: / 13.11.2001 / 19:16:25 / cg" |
|
17543 | 1030 |
! ! |
1031 |
||
1032 |
!CharacterArray methodsFor:'Compatibility-Dolphin'! |
|
9192 | 1033 |
|
1034 |
copyExpanding:expandTable |
|
1035 |
"return a copy of myself, with translations from the expandTable sliced in. |
|
18392 | 1036 |
The argument is supposed to map from characters to either characters or strings. |
1037 |
Added for Dolphin compatibility" |
|
9192 | 1038 |
|
1039 |
|ds| |
|
1040 |
||
1041 |
ds := WriteStream on:(self species new). |
|
1042 |
self do:[:eachChar | |
|
20012 | 1043 |
|repl| |
1044 |
||
1045 |
repl := expandTable at:eachChar ifAbsent:[nil]. |
|
1046 |
repl isNil ifTrue:[ |
|
1047 |
ds nextPut:eachChar |
|
1048 |
] ifFalse:[ |
|
1049 |
repl size == 0 ifTrue:[ |
|
1050 |
ds nextPut:repl |
|
1051 |
] ifFalse:[ |
|
1052 |
ds nextPutAll:repl |
|
1053 |
] |
|
1054 |
]. |
|
9192 | 1055 |
]. |
6870 | 1056 |
^ ds contents. |
1057 |
! |
|
1058 |
||
1059 |
formatWith:aString |
|
11955 | 1060 |
"Compatibility method - do not use in new code. |
18392 | 1061 |
same as #bindWith: for Dolphin compatibility" |
6870 | 1062 |
|
1063 |
^ self bindWith:aString |
|
1064 |
||
1065 |
" |
|
1066 |
'hello%1world' formatWith:'123' |
|
1067 |
" |
|
1068 |
! |
|
1069 |
||
1070 |
formatWith:arg1 with:arg2 |
|
11955 | 1071 |
"Compatibility method - do not use in new code. |
18392 | 1072 |
same as #bindWith: for Dolphin compatibility" |
6870 | 1073 |
|
1074 |
^ self bindWith:arg1 with:arg2 |
|
1075 |
||
1076 |
" |
|
1077 |
'hello%1 %2world' formatWith:'123' with:234 |
|
1078 |
" |
|
1079 |
! |
|
1080 |
||
1081 |
formatWith:arg1 with:arg2 with:arg3 |
|
11955 | 1082 |
"Compatibility method - do not use in new code. |
18392 | 1083 |
same as #bindWith: for Dolphin compatibility" |
6870 | 1084 |
|
1085 |
^ self bindWith:arg1 with:arg2 with:arg3 |
|
1086 |
||
1087 |
" |
|
1088 |
'hello%1 %2 %3world' formatWith:'123' with:234 with:345 |
|
1089 |
" |
|
6391 | 1090 |
! ! |
1091 |
||
14681 | 1092 |
!CharacterArray methodsFor:'Compatibility-GNU'! |
1093 |
||
1094 |
% anArrayOfOperands |
|
1095 |
"return a copy of the receiver, where a '%i' escape |
|
1096 |
is replaced by the coresponding string from the argument array. |
|
1097 |
'i' may be between 1 and 9 (i.e. a maximum of 9 placeholders is allowed). |
|
1098 |
Added for GNU-ST compatibility." |
|
1099 |
||
1100 |
^ self expandPlaceholdersWith:anArrayOfOperands |
|
1101 |
||
1102 |
" |
|
1103 |
'do you prefer %1 or rather %2 (not talking about %3) ?' |
|
20012 | 1104 |
% #('smalltalk' 'c++' 'c') |
14681 | 1105 |
|
1106 |
'do you %(what) ?' |
|
20012 | 1107 |
% (Dictionary new at:#'what' put:'understand'; yourself) |
14681 | 1108 |
" |
1109 |
! ! |
|
1110 |
||
7261 | 1111 |
!CharacterArray methodsFor:'Compatibility-ST/V'! |
608 | 1112 |
|
17517 | 1113 |
asArrayOfSubstrings |
1114 |
"return an array of substrings from the receiver, interpreting |
|
1115 |
separators (i.e. spaces & newlines) as word-delimiters. |
|
18390 | 1116 |
This has been added for ST/V compatibility |
1117 |
- the actual work is done in asCollectionOfWords. |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
1118 |
(sigh: it is called #'subStrings' in V'Age, #'substrings' in Squeak |
17517 | 1119 |
and #'asCollectionOfWords' in ST/X) " |
1120 |
||
1121 |
^ self asCollectionOfWords asArray |
|
1122 |
||
1123 |
" |
|
1124 |
'1 one two three four 5 five' asArrayOfSubstrings |
|
1125 |
'1 |
|
1126 |
one |
|
20012 | 1127 |
two three four 5 five' asArrayOfSubstrings |
17517 | 1128 |
" |
1129 |
! |
|
1130 |
||
608 | 1131 |
byteAt:index put:aByte |
1132 |
"store a byte at given index. |
|
1133 |
This is an ST/V compatibility method." |
|
1134 |
||
1333
d3fdca956f0d
allow storing of 0-bytes (be careful with printing then ...)
Claus Gittinger <cg@exept.de>
parents:
1312
diff
changeset
|
1135 |
"/ (aByte == 0) ifTrue:[ |
d3fdca956f0d
allow storing of 0-bytes (be careful with printing then ...)
Claus Gittinger <cg@exept.de>
parents:
1312
diff
changeset
|
1136 |
"/ "store a space instead" |
d3fdca956f0d
allow storing of 0-bytes (be careful with printing then ...)
Claus Gittinger <cg@exept.de>
parents:
1312
diff
changeset
|
1137 |
"/ ^ super basicAt:index put:(Character space) |
d3fdca956f0d
allow storing of 0-bytes (be careful with printing then ...)
Claus Gittinger <cg@exept.de>
parents:
1312
diff
changeset
|
1138 |
"/ ]. |
11754
5132248cfd6e
common definition of #byteAt:put between CharacterArray and Symbol
Stefan Vogel <sv@exept.de>
parents:
11632
diff
changeset
|
1139 |
^ self basicAt:index put:(Character value:aByte) |
9192 | 1140 |
|
1141 |
" |
|
1142 |
'hello' copy at:1 put:$H asciiValue; yourself |
|
1143 |
'hello' copy byteAt:1 put:72; yourself |
|
1144 |
'hello' copy byteAt:1 put:0; yourself |
|
1145 |
" |
|
1146 |
||
1147 |
"Modified: 6.5.1996 / 10:35:26 / cg" |
|
1148 |
! |
|
608 | 1149 |
|
19497 | 1150 |
equalsIgnoreCase:aString |
1151 |
"This is an ST/V compatibility method and an alias for sameAs:." |
|
1152 |
||
1153 |
^ self sameAs:aString |
|
1154 |
||
1155 |
" |
|
1156 |
'abc' equalsIgnoreCase: 'aBC' |
|
1157 |
" |
|
1158 |
! |
|
1159 |
||
608 | 1160 |
replChar:oldChar with:newChar |
19497 | 1161 |
"return a copy of the receiver, with all oldChars replaced by newChar. |
1162 |
This is an ST/V compatibility method and an alias for copyReplaceAll." |
|
608 | 1163 |
|
3656 | 1164 |
^ self copyReplaceAll:oldChar with:newChar |
608 | 1165 |
|
1166 |
" |
|
8874 | 1167 |
'12345678901234567890' replChar:$0 with:$* |
608 | 1168 |
" |
3656 | 1169 |
|
1170 |
"Modified: / 18.7.1998 / 22:52:57 / cg" |
|
608 | 1171 |
! |
1172 |
||
1173 |
replChar:oldChar withString:newString |
|
1174 |
"return a copy of the receiver, with all oldChars replaced |
|
1175 |
by newString (i.e. slice in the newString in place of the oldChar). |
|
1176 |
This is an ST/V compatibility method." |
|
1177 |
||
1178 |
|tmpStream| |
|
1179 |
||
15523 | 1180 |
tmpStream := self species writeStream. |
608 | 1181 |
self do:[:element | |
20012 | 1182 |
element = oldChar ifTrue:[ |
1183 |
tmpStream nextPutAll:newString |
|
1184 |
] ifFalse:[ |
|
1185 |
tmpStream nextPut:element |
|
1186 |
]. |
|
608 | 1187 |
]. |
1188 |
^ tmpStream contents |
|
1189 |
||
1190 |
" |
|
8874 | 1191 |
'12345678901234567890' replChar:$0 withString:'foo' |
1192 |
'a string with spaces' replChar:$ withString:' foo ' |
|
608 | 1193 |
" |
9192 | 1194 |
! |
1195 |
||
1196 |
replString:subString withString:newString |
|
4244
5deefa3144fb
added #replString:withString:
Claus Gittinger <cg@exept.de>
parents:
4156
diff
changeset
|
1197 |
"return a copy of the receiver, with all sequences of subString replaced |
18390 | 1198 |
by newString (i.e. slice in the newString in place of the oldString). |
19497 | 1199 |
This is an ST/V compatibility method and an alias for copyReplaceString." |
4244
5deefa3144fb
added #replString:withString:
Claus Gittinger <cg@exept.de>
parents:
4156
diff
changeset
|
1200 |
|
8347 | 1201 |
^ self copyReplaceString:subString withString:newString |
4244
5deefa3144fb
added #replString:withString:
Claus Gittinger <cg@exept.de>
parents:
4156
diff
changeset
|
1202 |
|
5deefa3144fb
added #replString:withString:
Claus Gittinger <cg@exept.de>
parents:
4156
diff
changeset
|
1203 |
" |
8874 | 1204 |
'12345678901234567890' replString:'123' withString:'OneTwoThree' |
1205 |
'12345678901234567890' replString:'123' withString:'*' |
|
1206 |
'12345678901234567890' replString:'234' withString:'foo' |
|
4244
5deefa3144fb
added #replString:withString:
Claus Gittinger <cg@exept.de>
parents:
4156
diff
changeset
|
1207 |
|
5deefa3144fb
added #replString:withString:
Claus Gittinger <cg@exept.de>
parents:
4156
diff
changeset
|
1208 |
('a string with spaces' replChar:$ withString:' foo ') |
20012 | 1209 |
replString:'foo' withString:'bar' |
4244
5deefa3144fb
added #replString:withString:
Claus Gittinger <cg@exept.de>
parents:
4156
diff
changeset
|
1210 |
" |
5deefa3144fb
added #replString:withString:
Claus Gittinger <cg@exept.de>
parents:
4156
diff
changeset
|
1211 |
|
8347 | 1212 |
"Modified: / 12-05-2004 / 12:00:27 / cg" |
9192 | 1213 |
! |
1214 |
||
9275 | 1215 |
subString:start to:end |
18390 | 1216 |
"same as copyFrom:to: |
19497 | 1217 |
This is an ST/V compatibility method and an alias for copyFrom:to:." |
18390 | 1218 |
|
9275 | 1219 |
^ self copyFrom:start to:end |
1220 |
||
1221 |
" |
|
1222 |
'12345678901234567890' subString:3 to:8 |
|
1223 |
" |
|
1224 |
! |
|
1225 |
||
9192 | 1226 |
trimBlanks |
14992 | 1227 |
"return a copy of the receiver without leading and trailing spaces. |
18464 | 1228 |
This is an ST/V compatibility method and an alias for withoutSpaces." |
9192 | 1229 |
|
1230 |
^ self withoutSpaces |
|
1231 |
||
1232 |
" |
|
1233 |
' spaces at beginning' trimBlanks |
|
1234 |
'spaces at end ' trimBlanks |
|
1235 |
' spaces at beginning and end ' trimBlanks |
|
1236 |
'no spaces' trimBlanks |
|
1237 |
" |
|
1238 |
! ! |
|
1239 |
||
1240 |
!CharacterArray methodsFor:'Compatibility-Squeak'! |
|
4409
86e049b21f8d
added #includesSubstring:caseSensitive:
Claus Gittinger <cg@exept.de>
parents:
4389
diff
changeset
|
1241 |
|
19171 | 1242 |
asBoldText |
1243 |
"return self as a bold text" |
|
1244 |
||
1245 |
^Text string: self emphasis: #bold |
|
1246 |
! |
|
1247 |
||
6148 | 1248 |
asDate |
1249 |
"Many allowed forms, see Date.readFrom:" |
|
1250 |
||
1251 |
^ Date readFrom: (ReadStream on: self) |
|
1252 |
||
1253 |
" |
|
8874 | 1254 |
'30 Apr 1999' asDate dayName capitalized |
6148 | 1255 |
" |
1256 |
! |
|
1257 |
||
16051 | 1258 |
asOneByteString |
1259 |
"return the receiver converted to a 'normal' string. |
|
18391 | 1260 |
Same as asSingleByteString - for Squeak/Pharo compatibility." |
16051 | 1261 |
|
1262 |
^ self asSingleByteString |
|
1263 |
! |
|
1264 |
||
8971 | 1265 |
asUrl |
18391 | 1266 |
"Same as asURL - for Squeak/Pharo compatibility." |
1267 |
||
8971 | 1268 |
^ self asURL |
1269 |
! |
|
1270 |
||
10840 | 1271 |
asWideString |
18391 | 1272 |
"return a two-byte string containing the same characters as the receiver. |
1273 |
Same as asTwoByteString - for Squeak/Pharo compatibility." |
|
14405
1d0d99a95bba
comment/format in:7 methods
Claus Gittinger <cg@exept.de>
parents:
14168
diff
changeset
|
1274 |
|
10840 | 1275 |
^ self asTwoByteString |
1276 |
||
1277 |
" |
|
1278 |
'abc' asWideString |
|
1279 |
" |
|
1280 |
! |
|
1281 |
||
4989 | 1282 |
capitalized |
18391 | 1283 |
"same as asUppercaseFirst for Squeak/Pharo compatibility" |
14405
1d0d99a95bba
comment/format in:7 methods
Claus Gittinger <cg@exept.de>
parents:
14168
diff
changeset
|
1284 |
|
4989 | 1285 |
^ self asUppercaseFirst |
1286 |
||
1287 |
" |
|
1288 |
'hello' capitalized |
|
1289 |
" |
|
1290 |
! |
|
1291 |
||
10841 | 1292 |
caseInsensitiveLessOrEqual:aString |
18391 | 1293 |
"compare the receiver against the argument ignoring case differences |
1294 |
For Squeak/Pharo compatibility" |
|
1295 |
||
10841 | 1296 |
^ (self compareCaselessWith:aString) <= 0 |
1297 |
! |
|
1298 |
||
1299 |
caseSensitiveLessOrEqual:aString |
|
18391 | 1300 |
"compare the receiver against the argument caring for case differences |
1301 |
For Squeak/Pharo compatibility" |
|
1302 |
||
10841 | 1303 |
^ self <= aString |
1304 |
! |
|
1305 |
||
1306 |
charactersExactlyMatching:aString |
|
14405
1d0d99a95bba
comment/format in:7 methods
Claus Gittinger <cg@exept.de>
parents:
14168
diff
changeset
|
1307 |
"return the number of characters I share as a prefix with the argument, aString" |
1d0d99a95bba
comment/format in:7 methods
Claus Gittinger <cg@exept.de>
parents:
14168
diff
changeset
|
1308 |
|
10841 | 1309 |
|idx nMax| |
1310 |
||
1311 |
nMax :=(self size) min:(aString size). |
|
1312 |
idx := 1. |
|
1313 |
[idx <= nMax] whileTrue:[ |
|
20012 | 1314 |
(self at:idx) = (aString at:idx) ifFalse:[ |
1315 |
^ idx - 1 |
|
1316 |
]. |
|
1317 |
idx := idx + 1. |
|
10841 | 1318 |
]. |
1319 |
^ nMax |
|
1320 |
||
1321 |
" |
|
12177 | 1322 |
'abc' charactersExactlyMatching:'abc' |
1323 |
'abc' charactersExactlyMatching:'abcd' |
|
1324 |
'abcd' charactersExactlyMatching:'abc' |
|
1325 |
'abc' charactersExactlyMatching:'abd' |
|
1326 |
'abc' charactersExactlyMatching:'xxx' |
|
10841 | 1327 |
" |
1328 |
! |
|
1329 |
||
9192 | 1330 |
endsWithDigit |
1331 |
"Answer whether the receiver's final character represents a digit. 3/11/96 sw" |
|
1332 |
||
18391 | 1333 |
|len| |
1334 |
||
1335 |
^ (len := self size) > 0 and: [(self at:len) isDigit] |
|
9192 | 1336 |
! |
1337 |
||
10703
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
1338 |
findDelimiters:delimiters startingAt:start |
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
1339 |
"Answer the index of the character within the receiver, starting at start, |
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
1340 |
that matches one of the delimiters. |
18391 | 1341 |
If the receiver does not contain any of the delimiters, answer size + 1." |
9192 | 1342 |
|
1343 |
|idx| |
|
1344 |
||
1345 |
idx := self indexOfAny:delimiters startingAt:start. |
|
1346 |
idx == 0 ifTrue:[^ self size + 1]. |
|
1347 |
^ idx. |
|
1348 |
"/start to: self size do: [:i | |
|
1349 |
"/ |char| |
|
1350 |
"/ |
|
1351 |
"/ char := self at: i. |
|
1352 |
"/ delimiters do: [:delim | delim = char ifTrue: [^ i]] |
|
8971 | 1353 |
"/ ]. |
1354 |
"/ ^ self size + 1 |
|
1355 |
! |
|
1356 |
||
13624 | 1357 |
findTokens:delimiterOrDelimiters |
7270 | 1358 |
"cg: I am not sure, if this is really the squeak semantics (w.r.t. empty fields)" |
1359 |
||
13624 | 1360 |
delimiterOrDelimiters size == 0 ifTrue:[ |
20012 | 1361 |
^ self asCollectionOfSubstringsSeparatedBy:delimiterOrDelimiters |
13624 | 1362 |
] ifFalse:[ |
20012 | 1363 |
^ self asCollectionOfSubstringsSeparatedByAny:delimiterOrDelimiters |
13624 | 1364 |
]. |
7270 | 1365 |
|
1366 |
" |
|
8874 | 1367 |
'a|b#c||e' findTokens:#($# $|) |
13624 | 1368 |
'a|b#c||e' findTokens:$| |
1369 |
" |
|
1370 |
||
1371 |
"Modified: / 04-09-2011 / 20:31:21 / cg" |
|
1372 |
"Modified (comment): / 05-09-2011 / 04:25:28 / cg" |
|
4981 | 1373 |
! |
1374 |
||
4974 | 1375 |
includesSubString:aString |
1376 |
"return true, if a substring is contained in the receiver. |
|
1377 |
The compare is case sensitive." |
|
1378 |
||
1379 |
^ self includesString:aString |
|
1380 |
||
1381 |
" |
|
8874 | 1382 |
'hello world' includesSubString:'Hel' |
9192 | 1383 |
'hello world' includesSubString:'hel' |
1384 |
'hello world' includesSubString:'llo' |
|
1385 |
" |
|
1386 |
||
1387 |
||
1388 |
||
1389 |
! |
|
1390 |
||
11178 | 1391 |
includesSubString:aString caseSensitive:caseSensitive |
18391 | 1392 |
"sigh - an alias; added for Squeak/Pharo compatibility" |
1393 |
||
11179 | 1394 |
^ self includesSubstring:aString caseSensitive:caseSensitive |
11178 | 1395 |
! |
1396 |
||
16875
32e61bb3a742
class: CharacterArray>>#includesSubstring: (for Squeak compatibility)
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
16863
diff
changeset
|
1397 |
includesSubstring: aString |
18391 | 1398 |
"sigh - an alias; added for Squeak/Pharo compatibility" |
1399 |
||
16875
32e61bb3a742
class: CharacterArray>>#includesSubstring: (for Squeak compatibility)
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
16863
diff
changeset
|
1400 |
^ self includesSubString: aString |
32e61bb3a742
class: CharacterArray>>#includesSubstring: (for Squeak compatibility)
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
16863
diff
changeset
|
1401 |
|
32e61bb3a742
class: CharacterArray>>#includesSubstring: (for Squeak compatibility)
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
16863
diff
changeset
|
1402 |
"Created: / 03-10-2014 / 02:47:52 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
32e61bb3a742
class: CharacterArray>>#includesSubstring: (for Squeak compatibility)
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
16863
diff
changeset
|
1403 |
! |
32e61bb3a742
class: CharacterArray>>#includesSubstring: (for Squeak compatibility)
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
16863
diff
changeset
|
1404 |
|
9192 | 1405 |
includesSubstring:aString caseSensitive:caseSensitive |
9190 | 1406 |
"return true, if a substring is contained in the receiver. |
9192 | 1407 |
The argument, caseSensitive controls if case is ignored in the compare." |
1408 |
||
1409 |
"/ for now, a q&d hack ... |
|
1410 |
||
1411 |
caseSensitive ifFalse:[ |
|
20012 | 1412 |
^ self asLowercase includesString:aString asLowercase |
9192 | 1413 |
]. |
9190 | 1414 |
^ self includesString:aString |
1415 |
||
1416 |
" |
|
9192 | 1417 |
'hello world' includesSubstring:'Hel' caseSensitive:true |
1418 |
'hello world' includesSubstring:'Hel' caseSensitive:false |
|
1419 |
" |
|
1420 |
||
1421 |
||
1422 |
||
1423 |
! |
|
9190 | 1424 |
|
6979 | 1425 |
isAllDigits |
1426 |
"Answer whether the receiver's characters are all digits" |
|
1427 |
||
1428 |
^ self conform:[:eachChar | eachChar isDigit] |
|
1429 |
||
1430 |
" |
|
1431 |
'hello world' isAllDigits |
|
1432 |
'12344' isAllDigits |
|
1433 |
" |
|
1434 |
! |
|
1435 |
||
4890 | 1436 |
lastSpacePosition |
18391 | 1437 |
"return the index of the last space character; 0 if there is none. |
1438 |
Added for Squeak/Pharo compatibility" |
|
14405
1d0d99a95bba
comment/format in:7 methods
Claus Gittinger <cg@exept.de>
parents:
14168
diff
changeset
|
1439 |
|
4890 | 1440 |
^ self lastIndexOfSeparator |
1441 |
! |
|
1442 |
||
7022 | 1443 |
padded:leftOrRight to:paddedSize with:padCharacter |
20155 | 1444 |
"pad left (leftOrRight == #left) or right" |
14053
acbb97913c30
comment/format in: #padded:to:with:
Claus Gittinger <cg@exept.de>
parents:
14050
diff
changeset
|
1445 |
|
7022 | 1446 |
leftOrRight == #left ifTrue:[ |
20155 | 1447 |
^ self leftPaddedTo:paddedSize with:padCharacter |
9190 | 1448 |
]. |
1449 |
^ self paddedTo:paddedSize with:padCharacter |
|
1450 |
||
1451 |
" |
|
8874 | 1452 |
'hello' padded:#right to:10 with:$. |
9192 | 1453 |
'hello' padded:#left to:10 with:$. |
1454 |
" |
|
14053
acbb97913c30
comment/format in: #padded:to:with:
Claus Gittinger <cg@exept.de>
parents:
14050
diff
changeset
|
1455 |
|
acbb97913c30
comment/format in: #padded:to:with:
Claus Gittinger <cg@exept.de>
parents:
14050
diff
changeset
|
1456 |
"Modified (comment): / 07-03-2012 / 16:31:33 / cg" |
9192 | 1457 |
! |
1458 |
||
1459 |
skipDelimiters:delimiters startingAt:start |
|
1460 |
"Answer the index of the character within the receiver, starting at start, |
|
1461 |
that does NOT match one of the delimiters. |
|
1462 |
If the receiver does not contain any of the delimiters, answer size + 1. |
|
1463 |
Assumes the delimiters to be a non-empty string." |
|
1464 |
||
1465 |
start to:self size do:[:i | |
|
20012 | 1466 |
delimiters detect:[:delim | delim = (self at:i) ] ifNone:[ ^ i ] |
9192 | 1467 |
]. |
1468 |
^ self size + 1 |
|
1469 |
||
1470 |
" |
|
1471 |
'123***7890' skipDelimiters:'*' startingAt:4 |
|
1472 |
'123***7890' skipDelimiters:'*' startingAt:3 |
|
9190 | 1473 |
'123***7890' skipDelimiters:'*' startingAt:10 |
1474 |
'123*******' skipDelimiters:'*' startingAt:10 |
|
8874 | 1475 |
" |
1476 |
! |
|
1477 |
||
9190 | 1478 |
substrings |
18921 | 1479 |
"return a collection consisting of all words contained in the receiver. |
17517 | 1480 |
Words are separated by whitespace. |
1481 |
This has been added for Squeak compatibility. |
|
1482 |
(sigh: it is called #'subStrings' in V'Age, and #'asCollectionOfWords' in ST/X) " |
|
1483 |
||
9190 | 1484 |
^ self asCollectionOfWords |
1485 |
||
1486 |
" |
|
1487 |
'foo bar baz' substrings |
|
1488 |
" |
|
1489 |
! |
|
1490 |
||
12665
b027fc5aec4e
added: #substringsSeparatedBy:
Claus Gittinger <cg@exept.de>
parents:
12664
diff
changeset
|
1491 |
substringsSeparatedBy:separatorCharacter |
18921 | 1492 |
"return a collection consisting of all words contained in the receiver. |
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
1493 |
Words are separated by the given separator character. |
18921 | 1494 |
This has been added for Squeak/Pharo compatibility. |
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
1495 |
(sigh: it is called #'subStrings:' in V'Age, |
18921 | 1496 |
and #'asCollectionOfSubstringsSeparatedBy' in ST/X) " |
18391 | 1497 |
|
12665
b027fc5aec4e
added: #substringsSeparatedBy:
Claus Gittinger <cg@exept.de>
parents:
12664
diff
changeset
|
1498 |
^ self asCollectionOfSubstringsSeparatedBy:separatorCharacter |
b027fc5aec4e
added: #substringsSeparatedBy:
Claus Gittinger <cg@exept.de>
parents:
12664
diff
changeset
|
1499 |
|
b027fc5aec4e
added: #substringsSeparatedBy:
Claus Gittinger <cg@exept.de>
parents:
12664
diff
changeset
|
1500 |
" |
b027fc5aec4e
added: #substringsSeparatedBy:
Claus Gittinger <cg@exept.de>
parents:
12664
diff
changeset
|
1501 |
'foo bar, baz' substringsSeparatedBy:$, |
b027fc5aec4e
added: #substringsSeparatedBy:
Claus Gittinger <cg@exept.de>
parents:
12664
diff
changeset
|
1502 |
" |
b027fc5aec4e
added: #substringsSeparatedBy:
Claus Gittinger <cg@exept.de>
parents:
12664
diff
changeset
|
1503 |
! |
b027fc5aec4e
added: #substringsSeparatedBy:
Claus Gittinger <cg@exept.de>
parents:
12664
diff
changeset
|
1504 |
|
9190 | 1505 |
truncateTo:smallSize |
9192 | 1506 |
"return myself or a copy shortened to smallSize. 1/18/96 sw" |
1507 |
||
1508 |
self size <= smallSize ifTrue:[^ self]. |
|
1509 |
^ self copyFrom: 1 to: smallSize |
|
1510 |
||
1511 |
" |
|
1512 |
'hello world' truncateTo:5 |
|
1513 |
'hello' truncateTo:10 |
|
1514 |
||
1515 |
'hello world' copyTo:5 |
|
1516 |
'hello' copyTo:10 |
|
1517 |
" |
|
1518 |
! |
|
5795 | 1519 |
|
4890 | 1520 |
withBlanksTrimmed |
18464 | 1521 |
"Return a copy of the receiver from which leading and trailing whitespace have been trimmed. |
1522 |
Notice the bad naming - it is trimming separators, not just blanks. |
|
1523 |
Added for Squeak compatibility and an alias for withoutSeparators" |
|
4890 | 1524 |
|
14865 | 1525 |
^ self withoutSeparators "withoutSpaces" "/ cg: it seems that squeak cares for any whitespace |
4890 | 1526 |
|
1527 |
" |
|
8874 | 1528 |
' hello world ' withBlanksTrimmed |
4890 | 1529 |
" |
1530 |
! |
|
1531 |
||
1532 |
withNoLineLongerThan: aNumber |
|
1533 |
"Answer a string with the same content as receiver, but rewrapped so that no line has more characters than the given number" |
|
1534 |
||
9192 | 1535 |
| listOfLines currentLast currentStart resultString putativeLast putativeLine crPosition | |
1536 |
||
14451
c06e7885b675
changed: #withNoLineLongerThan:
Claus Gittinger <cg@exept.de>
parents:
14420
diff
changeset
|
1537 |
(aNumber isNumber not or:[ aNumber < 1] ) ifTrue: [self error: 'too narrow']. |
9192 | 1538 |
listOfLines _ OrderedCollection new. |
1539 |
currentLast _ 0. |
|
1540 |
[currentLast < self size] whileTrue: |
|
20012 | 1541 |
[currentStart _ currentLast + 1. |
1542 |
putativeLast _ (currentStart + aNumber - 1) min: self size. |
|
1543 |
putativeLine _ self copyFrom: currentStart to: putativeLast. |
|
1544 |
(crPosition _ putativeLine indexOf: Character cr) > 0 ifTrue: |
|
1545 |
[putativeLast _ currentStart + crPosition - 1. |
|
1546 |
putativeLine _ self copyFrom: currentStart to: putativeLast]. |
|
1547 |
currentLast _ putativeLast == self size |
|
1548 |
ifTrue: |
|
1549 |
[putativeLast] |
|
1550 |
ifFalse: |
|
1551 |
[currentStart + putativeLine lastSpacePosition - 1]. |
|
1552 |
currentLast <= currentStart ifTrue: |
|
1553 |
["line has NO spaces; baleout!!" |
|
1554 |
currentLast _ putativeLast]. |
|
1555 |
listOfLines add: (self copyFrom: currentStart to: currentLast) withBlanksTrimmed]. |
|
9192 | 1556 |
|
1557 |
listOfLines size > 0 ifFalse: [^ '']. |
|
1558 |
resultString _ listOfLines first. |
|
1559 |
2 to: listOfLines size do: |
|
20012 | 1560 |
[:i | resultString _ resultString, Character cr asString, (listOfLines at: i)]. |
9192 | 1561 |
^ resultString |
1562 |
||
1563 |
" |
|
1564 |
#(5 7 20) collect: |
|
20012 | 1565 |
[:i | 'Fred the bear went down to the brook to read his book in silence' withNoLineLongerThan: i] |
14451
c06e7885b675
changed: #withNoLineLongerThan:
Claus Gittinger <cg@exept.de>
parents:
14420
diff
changeset
|
1566 |
" |
10883 | 1567 |
! |
1568 |
||
12343
0a93eee37d65
added: #withSqueakLineEndings
Claus Gittinger <cg@exept.de>
parents:
12330
diff
changeset
|
1569 |
withSqueakLineEndings |
13882
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
1570 |
"assume the string is textual, and that CR, LF, and CRLF are all |
18391 | 1571 |
valid line endings. Replace each occurence with a single CR" |
12343
0a93eee37d65
added: #withSqueakLineEndings
Claus Gittinger <cg@exept.de>
parents:
12330
diff
changeset
|
1572 |
|
0a93eee37d65
added: #withSqueakLineEndings
Claus Gittinger <cg@exept.de>
parents:
12330
diff
changeset
|
1573 |
^ self asStringCollection asStringWith:Character cr. |
0a93eee37d65
added: #withSqueakLineEndings
Claus Gittinger <cg@exept.de>
parents:
12330
diff
changeset
|
1574 |
! |
0a93eee37d65
added: #withSqueakLineEndings
Claus Gittinger <cg@exept.de>
parents:
12330
diff
changeset
|
1575 |
|
12188 | 1576 |
withoutLeading: char |
1577 |
"return a copy of myself without leading a char. |
|
1578 |
Returns an empty string, if the receiver consist only of a char." |
|
1579 |
||
1580 |
^ self withoutLeadingForWhich:[:ch | ch = char] |
|
1581 |
||
1582 |
" |
|
13882
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
1583 |
'****foo****' withoutLeading: $* |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
1584 |
'foo****' withoutLeading: $* |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
1585 |
'*' withoutLeading: $* |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
1586 |
'' withoutLeading: $* |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
1587 |
'****foo' withoutLeading: $* |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
1588 |
'*******' withoutLeading: $* |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
1589 |
'foo' withoutLeading: $* |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
1590 |
'f***o***o' withoutLeading: $* |
12188 | 1591 |
('**' , Character tab asString , '*foo***') withoutLeading: $* inspect |
1592 |
" |
|
1593 |
! |
|
1594 |
||
11565 | 1595 |
withoutTrailing:char |
1596 |
"return a copy of myself without trailing char. |
|
1597 |
Returns an empty string, if the receiver consist only of char." |
|
1598 |
||
12188 | 1599 |
^ self withoutTrailingForWhich:[:ch | ch = char] |
1600 |
||
1601 |
" |
|
13882
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
1602 |
' foo....' withoutTrailing:$. |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
1603 |
'foo....' withoutTrailing:$. |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
1604 |
' foo' withoutTrailing:$. |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
1605 |
'.......' withoutTrailing:$. |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
1606 |
'foo' withoutTrailing:$. |
11565 | 1607 |
" |
9190 | 1608 |
! ! |
1609 |
||
1610 |
!CharacterArray methodsFor:'Compatibility-V''Age'! |
|
1611 |
||
1612 |
addLineDelimiter |
|
1613 |
"replace all '\'-characters by line delimiter (cr) - characters. |
|
9192 | 1614 |
This has been added for VisualAge compatibility." |
1615 |
||
1616 |
^ self withCRs |
|
1617 |
! |
|
1618 |
||
1619 |
bindWith:aString |
|
1620 |
"return a copy of the receiver, where a '%1' escape is |
|
1621 |
replaced by aString. |
|
1622 |
This has been added for VisualAge compatibility." |
|
1623 |
||
1624 |
^ self expandPlaceholdersWith:(Array with:aString) |
|
1625 |
||
1626 |
" |
|
1627 |
'do you like %1 ?' bindWith:'smalltalk' |
|
19785 | 1628 |
'do you like %(foo) ?' bindWithArguments:(Dictionary new at:'foo' put:'smalltalk'; yourself) |
9192 | 1629 |
" |
1630 |
! |
|
608 | 1631 |
|
1632 |
bindWith:string1 with:string2 |
|
1633 |
"return a copy of the receiver, where a '%1' escape is |
|
1634 |
replaced by string1 and '%2' is replaced by string2. |
|
1635 |
This has been added for VisualAge compatibility." |
|
1636 |
||
1637 |
^ self expandPlaceholdersWith:(Array with:string1 with:string2) |
|
1638 |
||
1639 |
" |
|
1640 |
'do you prefer %1 or rather %2 ?' |
|
20012 | 1641 |
bindWith:'smalltalk' with:'c++' |
9192 | 1642 |
" |
1643 |
! |
|
9190 | 1644 |
|
1645 |
bindWith:str1 with:str2 with:str3 |
|
608 | 1646 |
"return a copy of the receiver, where a '%1', '%2' and '%3' escapes |
1647 |
are replaced by str1, str2 and str3 respectively. |
|
1648 |
This has been added for VisualAge compatibility." |
|
1649 |
||
1650 |
^ self expandPlaceholdersWith:(Array with:str1 with:str2 with:str3) |
|
1651 |
||
1652 |
" |
|
9192 | 1653 |
'do you prefer %1 or rather %2 (not talking about %3) ?' |
20012 | 1654 |
bindWith:'smalltalk' with:'c++' with:'c' |
9192 | 1655 |
" |
1656 |
! |
|
608 | 1657 |
|
1658 |
bindWith:str1 with:str2 with:str3 with:str4 |
|
1659 |
"return a copy of the receiver, where a '%1', '%2', '%3' and '%4' escapes |
|
1660 |
are replaced by str1, str2, str3 and str4 respectively. |
|
1661 |
This has been added for VisualAge compatibility." |
|
1662 |
||
1663 |
^ self expandPlaceholdersWith:(Array with:str1 with:str2 with:str3 with:str4) |
|
1664 |
||
1665 |
" |
|
9192 | 1666 |
'do you prefer %1 or rather %2 (not talking about %3 or even %4) ?' |
20012 | 1667 |
bindWith:'smalltalk' with:'c++' with:'c' with:'assembler' |
9192 | 1668 |
" |
1669 |
! |
|
608 | 1670 |
|
2344 | 1671 |
bindWith:str1 with:str2 with:str3 with:str4 with:str5 |
1672 |
"return a copy of the receiver, where a '%1' .. '%5' escapes |
|
1673 |
are replaced by str1 .. str5 respectively. |
|
1674 |
This has been added for VisualAge compatibility." |
|
1675 |
||
1676 |
^ self expandPlaceholdersWith:(Array with:str1 with:str2 with:str3 with:str4 with:str5) |
|
1677 |
||
1678 |
"Created: 31.1.1997 / 16:25:42 / cg" |
|
1679 |
! |
|
1680 |
||
5343 | 1681 |
bindWith:str1 with:str2 with:str3 with:str4 with:str5 with:str6 |
1682 |
"return a copy of the receiver, where a '%1' .. '%6' escapes |
|
11524 | 1683 |
are replaced by str1 .. str6 respectively. |
5343 | 1684 |
This has been added for VisualAge compatibility." |
1685 |
||
8874 | 1686 |
^ self expandPlaceholdersWith:(Array with:str1 with:str2 |
20012 | 1687 |
with:str3 with:str4 |
1688 |
with:str5 with:str6) |
|
11524 | 1689 |
! |
1690 |
||
1691 |
bindWith:str1 with:str2 with:str3 with:str4 with:str5 with:str6 with:str7 |
|
1692 |
"return a copy of the receiver, where a '%1' .. '%7' escapes |
|
1693 |
are replaced by str1 .. str7 respectively. |
|
1694 |
This has been added for VisualAge compatibility." |
|
1695 |
||
1696 |
^ self expandPlaceholdersWith:(Array with:str1 with:str2 |
|
20012 | 1697 |
with:str3 with:str4 |
1698 |
with:str5 with:str6 |
|
1699 |
with:str7) |
|
5343 | 1700 |
! |
1701 |
||
13996
a86faa4f17b8
added: #bindWith:with:with:with:with:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
13987
diff
changeset
|
1702 |
bindWith:str1 with:str2 with:str3 with:str4 with:str5 with:str6 with:str7 with:str8 |
a86faa4f17b8
added: #bindWith:with:with:with:with:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
13987
diff
changeset
|
1703 |
"return a copy of the receiver, where a '%1' .. '%8' escapes |
a86faa4f17b8
added: #bindWith:with:with:with:with:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
13987
diff
changeset
|
1704 |
are replaced by str1 .. str8 respectively. |
a86faa4f17b8
added: #bindWith:with:with:with:with:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
13987
diff
changeset
|
1705 |
This has been added for VisualAge compatibility." |
a86faa4f17b8
added: #bindWith:with:with:with:with:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
13987
diff
changeset
|
1706 |
|
a86faa4f17b8
added: #bindWith:with:with:with:with:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
13987
diff
changeset
|
1707 |
^ self expandPlaceholdersWith:(Array with:str1 with:str2 |
20012 | 1708 |
with:str3 with:str4 |
1709 |
with:str5 with:str6 |
|
1710 |
with:str7 with:str8) |
|
13996
a86faa4f17b8
added: #bindWith:with:with:with:with:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
13987
diff
changeset
|
1711 |
|
a86faa4f17b8
added: #bindWith:with:with:with:with:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
13987
diff
changeset
|
1712 |
"Created: / 06-02-2012 / 10:33:18 / cg" |
a86faa4f17b8
added: #bindWith:with:with:with:with:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
13987
diff
changeset
|
1713 |
! |
a86faa4f17b8
added: #bindWith:with:with:with:with:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
13987
diff
changeset
|
1714 |
|
14014
3aff2d7c41c8
added: #bindWith:with:with:with:with:with:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
13996
diff
changeset
|
1715 |
bindWith:str1 with:str2 with:str3 with:str4 with:str5 with:str6 with:str7 with:str8 with:str9 |
3aff2d7c41c8
added: #bindWith:with:with:with:with:with:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
13996
diff
changeset
|
1716 |
"return a copy of the receiver, where a '%1' .. '%9' escapes |
3aff2d7c41c8
added: #bindWith:with:with:with:with:with:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
13996
diff
changeset
|
1717 |
are replaced by str1 .. str9 respectively. |
3aff2d7c41c8
added: #bindWith:with:with:with:with:with:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
13996
diff
changeset
|
1718 |
This has been added for VisualAge compatibility." |
3aff2d7c41c8
added: #bindWith:with:with:with:with:with:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
13996
diff
changeset
|
1719 |
|
3aff2d7c41c8
added: #bindWith:with:with:with:with:with:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
13996
diff
changeset
|
1720 |
^ self expandPlaceholdersWith:(Array with:str1 with:str2 |
20012 | 1721 |
with:str3 with:str4 |
1722 |
with:str5 with:str6 |
|
1723 |
with:str7 with:str8 |
|
1724 |
with:str9) |
|
14014
3aff2d7c41c8
added: #bindWith:with:with:with:with:with:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
13996
diff
changeset
|
1725 |
|
3aff2d7c41c8
added: #bindWith:with:with:with:with:with:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
13996
diff
changeset
|
1726 |
"Created: / 14-02-2012 / 17:42:31 / cg" |
3aff2d7c41c8
added: #bindWith:with:with:with:with:with:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
13996
diff
changeset
|
1727 |
! |
3aff2d7c41c8
added: #bindWith:with:with:with:with:with:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
13996
diff
changeset
|
1728 |
|
608 | 1729 |
bindWithArguments:anArrayOfStrings |
1730 |
"return a copy of the receiver, where a '%i' escape |
|
1731 |
is replaced by the coresponding string from the argument array. |
|
1732 |
'i' may be between 1 and 9 (i.e. a maximum of 9 placeholders is allowed). |
|
1733 |
This has been added for VisualAge compatibility." |
|
1734 |
||
9192 | 1735 |
^ self expandPlaceholdersWith:anArrayOfStrings |
1736 |
||
1737 |
" |
|
1738 |
'do you prefer %1 or rather %2 (not talking about %3) ?' |
|
20012 | 1739 |
bindWithArguments:#('smalltalk' 'c++' 'c') |
12196 | 1740 |
|
1741 |
'do you %(what) ?' |
|
20012 | 1742 |
bindWithArguments:(Dictionary new at:#'what' put:'understand'; yourself) |
9192 | 1743 |
" |
1744 |
! |
|
608 | 1745 |
|
1746 |
subStrings |
|
18921 | 1747 |
"return a collection consisting of all words contained in the receiver. |
608 | 1748 |
Words are separated by whitespace. |
17517 | 1749 |
This has been added for VisualAge compatibility. |
1750 |
(sigh: it is called #'subbtrings' in Squeak, and #'asCollectionOfWords' in ST/X) " |
|
608 | 1751 |
|
1752 |
^ self asCollectionOfWords |
|
1753 |
||
1754 |
" |
|
1755 |
'hello world, this is smalltalk' subStrings |
|
1756 |
" |
|
1757 |
! |
|
1758 |
||
16181 | 1759 |
subStrings:separatorCharacterOrString |
18921 | 1760 |
"return a collection consisting of all words contained in the receiver. |
608 | 1761 |
Words are separated by separatorCharacter. |
18921 | 1762 |
This is similar to split: (squeak) and asCollectionOfSubstringsSeparatedBy: (st/x) |
1763 |
and has been added for VisualAge compatibility." |
|
608 | 1764 |
|
16181 | 1765 |
separatorCharacterOrString isCharacter ifTrue:[ |
20012 | 1766 |
^ self asCollectionOfSubstringsSeparatedBy:separatorCharacterOrString |
16181 | 1767 |
]. |
1768 |
^ self asCollectionOfSubstringsSeparatedByAny:separatorCharacterOrString |
|
1769 |
||
1770 |
" |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
1771 |
'foo:bar:baz:smalltalk' subStrings:$: |
16181 | 1772 |
'foo:bar:baz:smalltalk' subStrings:':' |
1773 |
'foo.bar,baz-smalltalk' subStrings:'.,-' |
|
9192 | 1774 |
" |
1775 |
! |
|
1776 |
||
1777 |
trimSeparators |
|
18392 | 1778 |
"return a copy of the receiver without leading and trailing whiteSpace. |
18921 | 1779 |
Added for VisualAge compatibility (an alias for withoutSeparators)" |
9192 | 1780 |
|
1781 |
^ self withoutSeparators |
|
1782 |
! ! |
|
1783 |
||
1784 |
!CharacterArray methodsFor:'Compatibility-VW'! |
|
5964 | 1785 |
|
6139 | 1786 |
asLogicalFileSpecification |
1787 |
^ self asFilename |
|
1788 |
||
1789 |
"Created: / 30.10.2001 / 17:29:53 / cg" |
|
1790 |
! |
|
1791 |
||
5964 | 1792 |
expandMacros |
1793 |
"ST80 compatibility - expand '<..>' macros with |
|
1794 |
argument strings. Similar to #bindWith:. |
|
1795 |
Read the comment in #expandMacrosWithArguments: about |
|
1796 |
limited compatibility issues." |
|
1797 |
||
1798 |
^ self expandMacrosWithArguments:#() |
|
1799 |
||
1800 |
" |
|
1801 |
'hello<n>foo' expandMacros |
|
1802 |
" |
|
1803 |
||
9192 | 1804 |
"Modified: / 18.6.1998 / 16:03:02 / cg" |
1805 |
! |
|
1806 |
||
1807 |
expandMacrosWith:arg |
|
5964 | 1808 |
"ST80 compatibility - expand '<..>' macros with |
1809 |
argument strings. Similar to #bindWith:. |
|
1810 |
Read the comment in #expandMacrosWithArguments: about |
|
1811 |
limited compatibility issues." |
|
1812 |
||
9192 | 1813 |
^ self expandMacrosWithArguments:(Array with:arg) |
1814 |
||
1815 |
"Created: / 1.11.1997 / 13:01:28 / cg" |
|
1816 |
"Modified: / 1.11.1997 / 13:30:50 / cg" |
|
1817 |
! |
|
5964 | 1818 |
|
1819 |
expandMacrosWith:arg1 with:arg2 |
|
1820 |
"ST80 compatibility - expand '<..>' macros with |
|
1821 |
argument strings. Similar to #bindWith:. |
|
1822 |
Read the comment in #expandMacrosWithArguments: about |
|
1823 |
limited compatibility issues." |
|
1824 |
||
1825 |
^ self expandMacrosWithArguments:(Array with:arg1 with:arg2) |
|
1826 |
||
1827 |
"Modified: / 6.7.1998 / 21:58:14 / cg" |
|
1828 |
! |
|
1829 |
||
1830 |
expandMacrosWith:arg1 with:arg2 with:arg3 |
|
1831 |
"ST80 compatibility - expand '<..>' macros with |
|
1832 |
argument strings. Similar to #bindWith:. |
|
1833 |
Read the comment in #expandMacrosWithArguments: about |
|
1834 |
limited compatibility issues." |
|
1835 |
||
1836 |
^ self expandMacrosWithArguments:(Array with:arg1 with:arg2 with:arg3) |
|
1837 |
! |
|
1838 |
||
10353
65fb7aa2c6c2
added #expandMacrosWith:with:with:with:
Stefan Vogel <sv@exept.de>
parents:
10134
diff
changeset
|
1839 |
expandMacrosWith:arg1 with:arg2 with:arg3 with:arg4 |
65fb7aa2c6c2
added #expandMacrosWith:with:with:with:
Stefan Vogel <sv@exept.de>
parents:
10134
diff
changeset
|
1840 |
"ST80 compatibility - expand '<..>' macros with |
65fb7aa2c6c2
added #expandMacrosWith:with:with:with:
Stefan Vogel <sv@exept.de>
parents:
10134
diff
changeset
|
1841 |
argument strings. Similar to #bindWith:. |
65fb7aa2c6c2
added #expandMacrosWith:with:with:with:
Stefan Vogel <sv@exept.de>
parents:
10134
diff
changeset
|
1842 |
Read the comment in #expandMacrosWithArguments: about |
65fb7aa2c6c2
added #expandMacrosWith:with:with:with:
Stefan Vogel <sv@exept.de>
parents:
10134
diff
changeset
|
1843 |
limited compatibility issues." |
65fb7aa2c6c2
added #expandMacrosWith:with:with:with:
Stefan Vogel <sv@exept.de>
parents:
10134
diff
changeset
|
1844 |
|
65fb7aa2c6c2
added #expandMacrosWith:with:with:with:
Stefan Vogel <sv@exept.de>
parents:
10134
diff
changeset
|
1845 |
^ self expandMacrosWithArguments:(Array with:arg1 with:arg2 with:arg3 with:arg4) |
65fb7aa2c6c2
added #expandMacrosWith:with:with:with:
Stefan Vogel <sv@exept.de>
parents:
10134
diff
changeset
|
1846 |
! |
65fb7aa2c6c2
added #expandMacrosWith:with:with:with:
Stefan Vogel <sv@exept.de>
parents:
10134
diff
changeset
|
1847 |
|
9192 | 1848 |
expandMacrosWithArguments:argArray |
5964 | 1849 |
"ST80 compatibility - expand '<..>' macros with |
1850 |
argument strings. Similar to #bindWith:. |
|
1851 |
WARNING: possibly not all ST80 expansions are supported here." |
|
1852 |
||
1853 |
"/ supported expansions: |
|
1854 |
"/ |
|
1855 |
"/ <#p> # is arg Number; slice in the args printString |
|
8222
f2c454a9a038
replaced '' writeStream by String writeStream
Claus Gittinger <cg@exept.de>
parents:
8124
diff
changeset
|
1856 |
"/ <#s> # is arg Number; slice in the arg itself (must know asString) |
5964 | 1857 |
"/ <#?s1:s2> # is arg Number; slice in s1 if the arg is true, s2 otherwise |
7643 | 1858 |
"/ use first arg if # is not given (i.e. no number before s,p ...) |
5964 | 1859 |
"/ <n> replace by a newLine character |
1860 |
"/ <t> replace by a tab character |
|
6834
eba4b58b8692
fixed expandMacros (%-escape)
Claus Gittinger <cg@exept.de>
parents:
6810
diff
changeset
|
1861 |
"/ %X the X character itself |
5964 | 1862 |
|
8999
f3fda66ea51e
#expandMacrosWithArguments: - be mor friendly to xml strings.
Stefan Vogel <sv@exept.de>
parents:
8971
diff
changeset
|
1863 |
|in out c fmt nr arg s1 s2 peekc| |
5964 | 1864 |
|
1865 |
in := self readStream. |
|
10721
58e6950264cb
care for bitsPerCharacter when expanding macros
Claus Gittinger <cg@exept.de>
parents:
10703
diff
changeset
|
1866 |
out := CharacterWriteStream on:(self species uninitializedNew:self size). |
5964 | 1867 |
|
1868 |
[in atEnd] whileFalse:[ |
|
20012 | 1869 |
c := in next. |
1870 |
c == $% ifTrue:[ |
|
1871 |
c := in next. |
|
1872 |
out nextPut:c |
|
1873 |
] ifFalse:[c ~~ $< ifTrue:[ |
|
1874 |
out nextPut:c. |
|
1875 |
] ifFalse:[ |
|
1876 |
peekc := in peek. |
|
1877 |
[peekc == $<] whileTrue:[ |
|
1878 |
out nextPut:$<. |
|
1879 |
peekc := in nextPeek. |
|
1880 |
]. |
|
1881 |
peekc == $n ifTrue:[ |
|
1882 |
peekc := in nextPeek. |
|
1883 |
peekc == $> ifTrue:[ |
|
1884 |
in next. |
|
1885 |
out cr. |
|
1886 |
] ifFalse:[ |
|
1887 |
out nextPutAll:'<n'. |
|
1888 |
] |
|
1889 |
] ifFalse:[peekc == $t ifTrue:[ |
|
1890 |
peekc := in nextPeek. |
|
1891 |
peekc == $> ifTrue:[ |
|
1892 |
in next. |
|
1893 |
out tab. |
|
1894 |
] ifFalse:[ |
|
1895 |
out nextPutAll:'<t'. |
|
1896 |
] |
|
1897 |
] ifFalse:[ |
|
1898 |
peekc isDigit ifTrue:[ |
|
1899 |
"start an argument expansion ..." |
|
1900 |
nr := Integer readFrom:in onError:nil. |
|
1901 |
nr isNil ifTrue:[ |
|
1902 |
"this cannot happen (there is at least one digit)" |
|
1903 |
self error:'invalid format' mayProceed:true. |
|
1904 |
^ self |
|
1905 |
]. |
|
1906 |
fmt := in next. |
|
1907 |
(fmt ~~ $? and:[in peek ~~ $>]) ifTrue:[ |
|
1908 |
out nextPut:$<. |
|
1909 |
nr printOn:out. |
|
1910 |
out nextPut:fmt. |
|
1911 |
] ifFalse:[ |
|
1912 |
(nr between:1 and:argArray size) ifTrue:[ |
|
1913 |
arg := argArray at:nr. |
|
1914 |
] ifFalse:[ |
|
1915 |
arg := '' |
|
1916 |
]. |
|
1917 |
||
1918 |
fmt == $p ifTrue:[ |
|
1919 |
"expand with args printString" |
|
1920 |
arg printOn:out. |
|
1921 |
] ifFalse:[fmt == $s ifTrue:[ |
|
1922 |
"expand with arg itself" |
|
1923 |
arg isText ifTrue:[ |
|
1924 |
out contentsSpecies isText ifFalse:[ |
|
1925 |
out := (TextStream ? CharacterWriteStream on:Text new) nextPutAll:out contents; yourself. |
|
1926 |
]. |
|
1927 |
out nextPutAll:arg. |
|
1928 |
] ifFalse:[ |
|
1929 |
out nextPutAll:arg asString string. "see method comment: arg must know #asString" |
|
1930 |
] |
|
1931 |
] ifFalse:[fmt == $? ifTrue:[ |
|
1932 |
s1 := in upTo:$:. |
|
1933 |
s2 := in nextUpTo:$>. |
|
1934 |
arg == true ifTrue:[ |
|
1935 |
out nextPutAll:s1 |
|
1936 |
] ifFalse:[ |
|
1937 |
out nextPutAll:s2 |
|
1938 |
]. |
|
1939 |
] ifFalse:[ |
|
1940 |
"what does VW do here ?" |
|
1941 |
self error:'invalid format' mayProceed:true. |
|
1942 |
^ self |
|
1943 |
]]]. |
|
1944 |
c := in next. |
|
1945 |
c ~~ $> ifTrue:[ |
|
1946 |
"what does VW do here ?" |
|
1947 |
self error:'invalid format' mayProceed:true. |
|
1948 |
^ self |
|
1949 |
] |
|
1950 |
||
1951 |
]. |
|
1952 |
] ifFalse:[ |
|
1953 |
out nextPut:$<. |
|
1954 |
]. |
|
1955 |
]]. |
|
1956 |
]]. |
|
5964 | 1957 |
]. |
1958 |
^ out contents |
|
1959 |
||
1960 |
" |
|
8874 | 1961 |
'hello <1s> how are you' expandMacrosWith:(OperatingSystem getLoginName) |
1962 |
'one plus one is <1p>' expandMacrosWith:2 |
|
5964 | 1963 |
" |
1964 |
||
10721
58e6950264cb
care for bitsPerCharacter when expanding macros
Claus Gittinger <cg@exept.de>
parents:
10703
diff
changeset
|
1965 |
"Modified: / 18-09-2007 / 22:50:43 / cg" |
9192 | 1966 |
! |
1967 |
||
1968 |
isCharacters |
|
1969 |
"added for visual works compatibility" |
|
1970 |
^ true |
|
1971 |
! ! |
|
1972 |
||
16295 | 1973 |
!CharacterArray methodsFor:'JavaScript support'! |
1974 |
||
1975 |
unquote |
|
17000 | 1976 |
"removes double quotes from the receiver. |
19698 | 1977 |
This is the JavaScript standard unquote function." |
1978 |
||
1979 |
^ self unquote:$" |
|
16295 | 1980 |
|
1981 |
" |
|
17000 | 1982 |
'hello' quote unquote |
1983 |
||
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
1984 |
JavaScriptParser evaluate:'''hello''.quote.unquote' |
16295 | 1985 |
" |
19698 | 1986 |
! |
1987 |
||
1988 |
unquote:quoteCharacter |
|
1989 |
"removes quoteCharacter from either end of the receiver." |
|
1990 |
||
1991 |
|mySize| |
|
1992 |
||
1993 |
(mySize := self size) >= 2 ifTrue:[ |
|
20012 | 1994 |
((self first == quoteCharacter) and:[self last == quoteCharacter]) ifTrue:[ |
1995 |
^ self copyFrom:2 to:mySize-1 |
|
1996 |
]. |
|
19698 | 1997 |
]. |
1998 |
^ self |
|
1999 |
||
2000 |
" |
|
2001 |
'*hello*' unquote:$* |
|
2002 |
" |
|
16295 | 2003 |
! ! |
13510 | 2004 |
|
16175 | 2005 |
|
9192 | 2006 |
!CharacterArray methodsFor:'character searching'! |
2007 |
||
2008 |
includesMatchCharacters |
|
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
2009 |
"return true if the receiver includes any GLOB meta-match characters (i.e. $* or $#) |
2519
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2010 |
for match operations; false if not. |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2011 |
Here, do not care for $\ escapes" |
608 | 2012 |
|
2013 |
^ self includesAny:'*#[' |
|
2519
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2014 |
|
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2015 |
" |
8874 | 2016 |
'*foo' includesMatchCharacters |
2017 |
'\*foo' includesMatchCharacters |
|
2018 |
'\*foo' includesUnescapedMatchCharacters |
|
2019 |
'*foo' includesMatchCharacters |
|
2519
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2020 |
'\\*foo' includesMatchCharacters |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2021 |
'foo*' includesMatchCharacters |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2022 |
'foo\*' includesMatchCharacters |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2023 |
'foo\' includesMatchCharacters |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2024 |
" |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2025 |
|
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2026 |
"Modified: 2.4.1997 / 18:12:34 / cg" |
9192 | 2027 |
! |
2028 |
||
2029 |
includesSeparator |
|
2030 |
"return true, if the receiver contains any whitespace characters" |
|
2031 |
||
2032 |
^ (self indexOfSeparator ~~ 0) |
|
2033 |
||
2034 |
" |
|
2035 |
'hello world' includesSeparator |
|
2036 |
'helloworld' includesSeparator |
|
2037 |
" |
|
2038 |
! |
|
2039 |
||
2040 |
includesUnescapedMatchCharacters |
|
8874 | 2041 |
"return true if the receiver really includes any meta characters (i.e. $* or $#) |
2519
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2042 |
for match operations; false if not. |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2043 |
Here, care for $\ escapes" |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2044 |
|
5342
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
2045 |
|idx sz specialChars escape| |
2519
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2046 |
|
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2047 |
idx := 1. |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2048 |
sz := self size. |
5342
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
2049 |
specialChars := '*#[\'. |
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
2050 |
(escape := self class matchEscapeCharacter) ~~ $\ ifTrue:[ |
20012 | 2051 |
specialChars := specialChars copy. |
2052 |
specialChars at:specialChars size put:escape |
|
5342
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
2053 |
]. |
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
2054 |
|
16245 | 2055 |
[ |
20012 | 2056 |
idx := self indexOfAny:specialChars startingAt:idx. |
2057 |
idx == 0 ifTrue:[^ false]. |
|
2058 |
(self at:idx) == escape ifFalse:[^ true]. |
|
2059 |
idx := idx + 2. |
|
2060 |
idx > sz ifTrue:[^ false]. |
|
16245 | 2061 |
] loop. |
8874 | 2062 |
|
2063 |
" |
|
2064 |
'*foo' includesUnescapedMatchCharacters |
|
2065 |
'\*foo' includesUnescapedMatchCharacters |
|
2066 |
'\\foo' includesUnescapedMatchCharacters |
|
2067 |
'\\\$foo' includesUnescapedMatchCharacters |
|
2519
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2068 |
'*foo' includesUnescapedMatchCharacters |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2069 |
'\\*foo' includesUnescapedMatchCharacters |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2070 |
'foo*' includesUnescapedMatchCharacters |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2071 |
'foo\*' includesUnescapedMatchCharacters |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2072 |
'foo\' includesUnescapedMatchCharacters |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2073 |
" |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2074 |
|
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2075 |
"Modified: 2.4.1997 / 17:08:52 / cg" |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2076 |
"Created: 2.4.1997 / 17:23:26 / cg" |
9192 | 2077 |
! |
2078 |
||
2079 |
indexOfControlCharacterStartingAt:startIndex |
|
3668
76271941f044
added #indexOfControlCharacterStartingAt:
Claus Gittinger <cg@exept.de>
parents:
3656
diff
changeset
|
2080 |
"return the index of the next control character; |
76271941f044
added #indexOfControlCharacterStartingAt:
Claus Gittinger <cg@exept.de>
parents:
3656
diff
changeset
|
2081 |
that is a character with asciiValue < 32. |
15605 | 2082 |
Start the search at startIndex, searching forward. |
3668
76271941f044
added #indexOfControlCharacterStartingAt:
Claus Gittinger <cg@exept.de>
parents:
3656
diff
changeset
|
2083 |
Return 0 if none is found." |
76271941f044
added #indexOfControlCharacterStartingAt:
Claus Gittinger <cg@exept.de>
parents:
3656
diff
changeset
|
2084 |
|
76271941f044
added #indexOfControlCharacterStartingAt:
Claus Gittinger <cg@exept.de>
parents:
3656
diff
changeset
|
2085 |
|start "{ Class: SmallInteger }" |
9192 | 2086 |
mySize "{ Class: SmallInteger }"| |
2087 |
||
2088 |
start := startIndex. |
|
2089 |
mySize := self size. |
|
2090 |
||
2091 |
start to:mySize do:[:index | |
|
20012 | 2092 |
(self at:index) isControlCharacter ifTrue:[^ index] |
9192 | 2093 |
]. |
2094 |
^ 0 |
|
2095 |
||
2096 |
" |
|
2097 |
'hello world' asTwoByteString indexOfControlCharacterStartingAt:1 |
|
2098 |
'hello world\foo' withCRsasTwoByteString indexOfControlCharacterStartingAt:1 |
|
2099 |
" |
|
2100 |
||
2101 |
"Modified: / 21.7.1998 / 17:25:07 / cg" |
|
2102 |
! |
|
3668
76271941f044
added #indexOfControlCharacterStartingAt:
Claus Gittinger <cg@exept.de>
parents:
3656
diff
changeset
|
2103 |
|
8541 | 2104 |
indexOfNonSeparator |
2105 |
"return the index of the first non-whitespace character. |
|
2106 |
return 0 if no non-separator was found" |
|
2107 |
||
2108 |
^ self indexOfNonSeparatorStartingAt:1. |
|
2109 |
||
2110 |
" |
|
8874 | 2111 |
' hello world' indexOfNonSeparator |
2112 |
' ' indexOfNonSeparator |
|
2113 |
'a ' indexOfNonSeparator |
|
2114 |
'abc' indexOfNonSeparator |
|
2115 |
' ' indexOfNonSeparator |
|
9192 | 2116 |
'' indexOfNonSeparator |
2117 |
" |
|
2118 |
! |
|
2119 |
||
2120 |
indexOfNonSeparatorStartingAt:startIndex |
|
4117 | 2121 |
"return the index of the next non-whitespace character, |
2122 |
starting the search at startIndex, searching forward; |
|
2123 |
return 0 if no non-separator was found" |
|
608 | 2124 |
|
2125 |
|start "{ Class: SmallInteger }" |
|
2126 |
mySize "{ Class: SmallInteger }"| |
|
2127 |
||
2128 |
start := startIndex. |
|
2129 |
mySize := self size. |
|
2130 |
||
2131 |
start to:mySize do:[:index | |
|
20012 | 2132 |
(self at:index) isSeparator ifFalse:[^ index] |
608 | 2133 |
]. |
2134 |
^ 0 |
|
2135 |
||
2136 |
" |
|
8874 | 2137 |
' hello world' indexOfNonSeparatorStartingAt:1 |
2138 |
' ' indexOfNonSeparatorStartingAt:1 |
|
2139 |
'a ' indexOfNonSeparatorStartingAt:2 |
|
4117 | 2140 |
" |
2141 |
||
608 | 2142 |
" |
2143 |
|s index1 index2| |
|
2144 |
s := ' foo bar baz'. |
|
2145 |
index1 := s indexOfNonSeparatorStartingAt:1. |
|
2146 |
index2 := s indexOfSeparatorStartingAt:index1. |
|
2147 |
s copyFrom:index1 to:index2 - 1 |
|
2148 |
" |
|
9192 | 2149 |
! |
2150 |
||
2151 |
indexOfSeparator |
|
2152 |
"return the index of the first whitespace character; |
|
2153 |
starting the search at the beginning, searching forward; |
|
2154 |
return 0 if no separator was found" |
|
2155 |
||
2156 |
^ self indexOfSeparatorStartingAt:1 |
|
2157 |
||
2158 |
" |
|
2159 |
'hello world' indexOfSeparator |
|
2160 |
'helloworld' indexOfSeparator |
|
2161 |
'hello ' indexOfSeparator |
|
2162 |
' hello' indexOfSeparator |
|
2163 |
" |
|
2164 |
! |
|
2165 |
||
15912 | 2166 |
indexOfSeparatorOrEndStartingAt:startIndex |
2167 |
"return the index of the next whitespace character, |
|
2168 |
starting the search at startIndex, searching forward; |
|
19737 | 2169 |
return the index of one beyond the end of the receiver if no separator was found. |
15912 | 2170 |
To extract the word, copy from startIndex to the returned index-1" |
2171 |
||
2172 |
|idx| |
|
2173 |
||
2174 |
idx := self indexOfSeparatorStartingAt:startIndex. |
|
2175 |
idx == 0 ifTrue:[^ self size + 1]. |
|
2176 |
^ idx. |
|
2177 |
||
2178 |
" |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2179 |
'hello world' indexOfSeparatorOrEndStartingAt:3 |
15912 | 2180 |
' hello world' indexOfSeparatorOrEndStartingAt:3 |
2181 |
'hello world ' indexOfSeparatorOrEndStartingAt:3 |
|
2182 |
'hello world ' indexOfSeparatorOrEndStartingAt:6 |
|
2183 |
'hello world ' indexOfSeparatorOrEndStartingAt:7 |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2184 |
'helloworld ' indexOfSeparatorOrEndStartingAt:7 |
15912 | 2185 |
'helloworld' indexOfSeparatorOrEndStartingAt:7 |
19737 | 2186 |
'helloworld' indexOfSeparatorStartingAt:7 |
15912 | 2187 |
" |
2188 |
! |
|
2189 |
||
9192 | 2190 |
indexOfSeparatorStartingAt:startIndex |
2191 |
"return the index of the next whitespace character, |
|
2192 |
starting the search at startIndex, searching forward; |
|
2193 |
return 0 if no separator was found" |
|
2194 |
||
2195 |
|start "{ Class: SmallInteger }" |
|
2196 |
mySize "{ Class: SmallInteger }"| |
|
2197 |
||
2198 |
start := startIndex. |
|
2199 |
mySize := self size. |
|
2200 |
||
2201 |
start to:mySize do:[:index | |
|
20012 | 2202 |
(self at:index) isSeparator ifTrue:[^ index] |
9192 | 2203 |
]. |
2204 |
^ 0 |
|
2205 |
||
2206 |
" |
|
2207 |
'hello world' indexOfSeparatorStartingAt:3 |
|
2208 |
' hello world' indexOfSeparatorStartingAt:3 |
|
8874 | 2209 |
'hello world ' indexOfSeparatorStartingAt:3 |
2210 |
'hello world ' indexOfSeparatorStartingAt:6 |
|
2211 |
'hello world ' indexOfSeparatorStartingAt:7 |
|
2212 |
'helloworld ' indexOfSeparatorStartingAt:7 |
|
2213 |
'helloworld' indexOfSeparatorStartingAt:7 |
|
4117 | 2214 |
" |
2519
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2215 |
! |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2216 |
|
4117 | 2217 |
lastIndexOfSeparator |
14150
e13160aa321a
comment/format in: #lastIndexOfSeparator
Claus Gittinger <cg@exept.de>
parents:
14141
diff
changeset
|
2218 |
"return the last index of a whitespace character (space or tab). |
4117 | 2219 |
(i.e. start the search at the end and search backwards); |
2220 |
Returns 0 if no separator is found." |
|
2221 |
||
2222 |
^ self lastIndexOfSeparatorStartingAt:(self size) |
|
2223 |
||
2224 |
" |
|
8874 | 2225 |
'hello world' lastIndexOfSeparator |
2226 |
'helloworld' lastIndexOfSeparator |
|
2227 |
'hel lo wor ld' lastIndexOfSeparator |
|
2228 |
'hel ' lastIndexOfSeparator 6 |
|
4117 | 2229 |
" |
14150
e13160aa321a
comment/format in: #lastIndexOfSeparator
Claus Gittinger <cg@exept.de>
parents:
14141
diff
changeset
|
2230 |
|
e13160aa321a
comment/format in: #lastIndexOfSeparator
Claus Gittinger <cg@exept.de>
parents:
14141
diff
changeset
|
2231 |
"Modified (comment): / 01-06-2012 / 13:10:30 / cg" |
4117 | 2232 |
! |
2233 |
||
2234 |
lastIndexOfSeparatorStartingAt:startIndex |
|
9192 | 2235 |
"return the index of the previous whitespace character, |
2236 |
starting the search at startIndex (and searching backwards); |
|
2237 |
returns 0 if no separator was found" |
|
2238 |
||
2239 |
|start "{ Class: SmallInteger }"| |
|
2240 |
||
2241 |
start := startIndex. |
|
2242 |
||
2243 |
start to:1 by:-1 do:[:index | |
|
20012 | 2244 |
(self at:index) isSeparator ifTrue:[^ index] |
9192 | 2245 |
]. |
2246 |
^ 0 |
|
2247 |
||
2248 |
" |
|
2249 |
'hello world' lastIndexOfSeparatorStartingAt:3 |
|
2250 |
'hello world' lastIndexOfSeparatorStartingAt:7 |
|
2251 |
'helloworld' lastIndexOfSeparatorStartingAt:7 |
|
8874 | 2252 |
' helloworld' lastIndexOfSeparatorStartingAt:7 |
4117 | 2253 |
" |
608 | 2254 |
! ! |
2255 |
||
2256 |
!CharacterArray methodsFor:'comparing'! |
|
2257 |
||
6742
292775fda600
avoid endless recursion when comparing int < string
Claus Gittinger <cg@exept.de>
parents:
6655
diff
changeset
|
2258 |
< aString |
608 | 2259 |
"Compare the receiver with the argument and return true if the |
6742
292775fda600
avoid endless recursion when comparing int < string
Claus Gittinger <cg@exept.de>
parents:
6655
diff
changeset
|
2260 |
receiver is greater than the argument. Otherwise return false. |
8874 | 2261 |
This comparison is based on the elements ascii code - |
9192 | 2262 |
i.e. upper/lowercase & upper/lowercase & national characters are NOT treated specially." |
2263 |
||
2264 |
|mySize "{ Class: SmallInteger }" |
|
2265 |
otherSize "{ Class: SmallInteger }" |
|
2266 |
n "{ Class: SmallInteger }" |
|
2267 |
c1 c2| |
|
2268 |
||
2269 |
mySize := self size. |
|
2270 |
otherSize := aString string size. |
|
6742
292775fda600
avoid endless recursion when comparing int < string
Claus Gittinger <cg@exept.de>
parents:
6655
diff
changeset
|
2271 |
n := mySize min:otherSize. |
292775fda600
avoid endless recursion when comparing int < string
Claus Gittinger <cg@exept.de>
parents:
6655
diff
changeset
|
2272 |
|
292775fda600
avoid endless recursion when comparing int < string
Claus Gittinger <cg@exept.de>
parents:
6655
diff
changeset
|
2273 |
1 to:n do:[:index | |
20012 | 2274 |
c1 := self at:index. |
2275 |
c2 := aString at:index. |
|
2276 |
(c1 == c2 or:[c1 = c2]) ifFalse:[^ c1 < c2]. |
|
6742
292775fda600
avoid endless recursion when comparing int < string
Claus Gittinger <cg@exept.de>
parents:
6655
diff
changeset
|
2277 |
]. |
292775fda600
avoid endless recursion when comparing int < string
Claus Gittinger <cg@exept.de>
parents:
6655
diff
changeset
|
2278 |
^ mySize < otherSize |
608 | 2279 |
! |
2280 |
||
2281 |
= aString |
|
2282 |
"Compare the receiver with the argument and return true if the |
|
2283 |
receiver is equal to the argument. Otherwise return false. |
|
1252 | 2284 |
|
9191 | 2285 |
This compare does NOT ignore case differences, |
608 | 2286 |
therefore 'foo' = 'Foo' will return false. |
2287 |
Since this is incompatible to ST-80 (at least, V2.x) , this may change." |
|
2288 |
||
14137
1eb1b4a3f364
Inherit from UninterpretedBytes instead of ByteArray
Stefan Vogel <sv@exept.de>
parents:
14122
diff
changeset
|
2289 |
|mySize "{ Class: SmallInteger }"| |
608 | 2290 |
|
5963
23462e06dcff
compare with Texts fixed (should compare equal if strings are)
Claus Gittinger <cg@exept.de>
parents:
5946
diff
changeset
|
2291 |
(aString isString or:[aString species == self species]) ifFalse:[ |
20012 | 2292 |
^ false |
5963
23462e06dcff
compare with Texts fixed (should compare equal if strings are)
Claus Gittinger <cg@exept.de>
parents:
5946
diff
changeset
|
2293 |
]. |
608 | 2294 |
mySize := self size. |
14137
1eb1b4a3f364
Inherit from UninterpretedBytes instead of ByteArray
Stefan Vogel <sv@exept.de>
parents:
14122
diff
changeset
|
2295 |
mySize ~~ (aString size) ifTrue:[^ false]. |
608 | 2296 |
|
2297 |
1 to:mySize do:[:index | |
|
20012 | 2298 |
(self at:index) = (aString at:index) ifFalse:[^ false]. |
608 | 2299 |
]. |
2300 |
^ true |
|
2301 |
||
2302 |
" |
|
8874 | 2303 |
'foo' = 'Foo' |
2304 |
'foo' = 'bar' |
|
2305 |
'foo' = 'foo' |
|
2306 |
'foo' = 'foo' asText |
|
2307 |
'foo' asText = 'foo' |
|
2308 |
'foo' asText = 'foo' asText |
|
608 | 2309 |
" |
1252 | 2310 |
|
2311 |
"Modified: 22.4.1996 / 15:53:58 / cg" |
|
608 | 2312 |
! |
9192 | 2313 |
|
9191 | 2314 |
> aString |
608 | 2315 |
"Compare the receiver with the argument and return true if the |
2316 |
receiver is greater than the argument. Otherwise return false. |
|
8874 | 2317 |
This comparison is based on the elements ascii code - |
1252 | 2318 |
i.e. upper/lowercase & upper/lowercase & national characters are NOT treated specially." |
608 | 2319 |
|
2320 |
|mySize "{ Class: SmallInteger }" |
|
8874 | 2321 |
otherSize "{ Class: SmallInteger }" |
9192 | 2322 |
n "{ Class: SmallInteger }" |
2323 |
c1 c2| |
|
2324 |
||
2325 |
mySize := self size. |
|
5929 | 2326 |
otherSize := aString string size. |
2327 |
n := mySize min:otherSize. |
|
2328 |
||
2329 |
1 to:n do:[:index | |
|
20012 | 2330 |
c1 := self at:index. |
2331 |
c2 := aString at:index. |
|
2332 |
(c1 == c2 or:[c1 = c2]) ifFalse:[^ c1 > c2]. |
|
5929 | 2333 |
]. |
9192 | 2334 |
^ mySize > otherSize |
2335 |
||
2336 |
"Modified: 22.4.1996 / 15:55:00 / cg" |
|
2337 |
! |
|
2338 |
||
14660 | 2339 |
after:aString |
2340 |
"Compare the receiver with the argument and return true if the |
|
2341 |
receiver should come after the argument in a sorted list. |
|
2342 |
Otherwise return false. |
|
2343 |
NOTE: The comparison should be language specific, depending on the value of |
|
20012 | 2344 |
LC_COLLATE, which is initialized from the environment. |
2345 |
||
2346 |
Currently it is for Strings, but not for UnicodeStrings... |
|
14660 | 2347 |
|
2348 |
STUPID: |
|
20012 | 2349 |
#after has a completely different meaning in SeqColl ..." |
14660 | 2350 |
|
2351 |
^ (self compareCollatingWith:aString) > 0 |
|
2352 |
! |
|
2353 |
||
15814 | 2354 |
compareAsVersionNumberWith:aStringOrCollection |
2355 |
"Compare the receiver with the argument and return 1 if the receiver is |
|
2356 |
greater, 0 if equal and -1 if less than the argument in a sorted list. |
|
2357 |
Compare as version numbers in the form a.b.c... ." |
|
2358 |
||
2359 |
|rev1 rev2| |
|
2360 |
||
2361 |
rev1 := self asCollectionOfSubstringsSeparatedBy:$.. |
|
2362 |
aStringOrCollection isString ifTrue:[ |
|
20012 | 2363 |
rev2 := aStringOrCollection asCollectionOfSubstringsSeparatedBy:$.. |
15814 | 2364 |
]. |
2365 |
rev1 := rev1 collect:[:each| each asInteger]. |
|
2366 |
rev2 := rev2 collect:[:each| each asInteger]. |
|
2367 |
||
2368 |
^ rev1 compareWith:rev2 |
|
2369 |
||
2370 |
" |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2371 |
self assert:('1' compareAsVersionNumberWith:'2') < 0. |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2372 |
self assert:('2' compareAsVersionNumberWith:'1') > 0. |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2373 |
self assert:('1.1' compareAsVersionNumberWith:'2.1.2') < 0. |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2374 |
self assert:('2.1' compareAsVersionNumberWith:'1.2.3') > 0. |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2375 |
self assert:('1' compareAsVersionNumberWith:'1.1') < 0. |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2376 |
self assert:('1.1' compareAsVersionNumberWith:'1') > 0. |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2377 |
self assert:('1.1' compareAsVersionNumberWith:'1.2') < 0. |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2378 |
self assert:('1.10' compareAsVersionNumberWith:'1.2') > 0. |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2379 |
self assert:('1.2.3.4' compareAsVersionNumberWith:'1.2.3.5') < 0. |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2380 |
self assert:('1.2.3.4' compareAsVersionNumberWith:'1.2.3.3') > 0. |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2381 |
self assert:('1.2.3.4' compareAsVersionNumberWith:'1.2.3') > 0. |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2382 |
self assert:('1.2.3.4' compareAsVersionNumberWith:'1.2.3.4') = 0. |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2383 |
self assert:('1.2.3.4' compareAsVersionNumberWith:'01.002.03.004') = 0. |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2384 |
self assert:('1.2.3.4' compareAsVersionNumberWith:#(1 2 3 4)) = 0. |
15814 | 2385 |
" |
2386 |
! |
|
2387 |
||
9192 | 2388 |
compareCaselessWith:aString |
9190 | 2389 |
"Compare the receiver against the argument, ignoreing case. |
2390 |
Return 1 if the receiver is greater, 0 if equal and -1 if less than the argument. |
|
2391 |
||
8874 | 2392 |
This comparison is based on the elements ascii code - |
9190 | 2393 |
i.e. national characters are NOT treated specially. |
2394 |
'foo' compareWith: 'Foo' will return 0" |
|
608 | 2395 |
|
2396 |
|mySize "{ Class: SmallInteger }" |
|
8874 | 2397 |
otherSize "{ Class: SmallInteger }" |
9192 | 2398 |
n "{ Class: SmallInteger }" |
608 | 2399 |
c1 c2| |
2400 |
||
2401 |
mySize := self size. |
|
5739
4fd3be7ccc75
oops some relational ops did accept a non-string arg
Claus Gittinger <cg@exept.de>
parents:
5732
diff
changeset
|
2402 |
otherSize := aString string size. |
608 | 2403 |
n := mySize min:otherSize. |
2404 |
||
2405 |
1 to:n do:[:index | |
|
20012 | 2406 |
c1 := (self at:index) asLowercase. |
2407 |
c2 := (aString at:index) asLowercase. |
|
2408 |
c1 > c2 ifTrue:[^ 1]. |
|
2409 |
c1 < c2 ifTrue:[^ -1]. |
|
608 | 2410 |
]. |
2411 |
mySize > otherSize ifTrue:[^ 1]. |
|
2412 |
mySize < otherSize ifTrue:[^ -1]. |
|
2413 |
^ 0 |
|
1252 | 2414 |
|
2415 |
"Modified: 22.4.1996 / 15:56:07 / cg" |
|
608 | 2416 |
! |
2417 |
||
14660 | 2418 |
compareCollatingWith:aString |
2419 |
"Compare the receiver with the argument and return 1 if the receiver is |
|
2420 |
greater, 0 if equal and -1 if less than the argument in a sorted list. |
|
2421 |
The comparison is language specific, depending on the value of |
|
2422 |
LC_COLLATE, which is in the shell environment." |
|
2423 |
||
2424 |
"TODO not yet defined for unicode" |
|
2425 |
<resource: #todo> |
|
2426 |
||
2427 |
|s| |
|
2428 |
||
2429 |
(s := self string) ~~ self ifTrue:[ |
|
20012 | 2430 |
^ s compareCollatingWith:aString |
14660 | 2431 |
]. |
2432 |
^ self compareWith:aString |
|
2433 |
! |
|
2434 |
||
9192 | 2435 |
compareWith:aString |
9190 | 2436 |
"Compare the receiver with the argument and return 1 if the receiver is |
2437 |
greater, 0 if equal and -1 if less than the argument. |
|
11861 | 2438 |
This comparison is based on the elements' codepoints - |
9190 | 2439 |
i.e. upper/lowercase & national characters are NOT treated specially. |
2440 |
'foo' compareWith: 'Foo' will return 1. |
|
2441 |
while 'foo' sameAs:'Foo' will return true" |
|
2442 |
||
15814 | 2443 |
|s| |
2444 |
||
2445 |
s := self string. |
|
2446 |
s ~~ self ifTrue:[ |
|
20012 | 2447 |
^ s compareWith:aString string. |
9192 | 2448 |
]. |
15814 | 2449 |
^ super compareWith:aString string. |
9192 | 2450 |
! |
2451 |
||
11861 | 2452 |
endsWith:aStringOrCharacter |
16175 | 2453 |
"return true, if the receiver ends with something, aStringOrCharacter. |
2454 |
If aStringOrCharacter is empty, true is returned" |
|
11861 | 2455 |
|
2456 |
|s| |
|
2457 |
||
2458 |
(s := self string) ~~ self ifTrue:[ |
|
20012 | 2459 |
^ s endsWith:aStringOrCharacter |
11861 | 2460 |
]. |
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2461 |
(self size > 0 and:[aStringOrCharacter isCharacter]) ifTrue:[ |
20012 | 2462 |
^ self last = aStringOrCharacter |
11861 | 2463 |
]. |
2464 |
^ super endsWith:aStringOrCharacter |
|
2465 |
||
2466 |
" |
|
2467 |
'hello world' endsWith:'world' |
|
2468 |
'hello world' asText allBold endsWith:'world' |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2469 |
'hello world' endsWith:'' |
16175 | 2470 |
'hello world' asText allBold endsWith:'' |
11861 | 2471 |
" |
2472 |
||
2473 |
"Modified: 12.5.1996 / 15:49:18 / cg" |
|
2474 |
! |
|
2475 |
||
11838 | 2476 |
hammingDistanceTo:aString |
2477 |
"return the hamming distance (the number of characters which are different). |
|
12177 | 2478 |
In information theory, the Hamming distance between two strings of equal length |
2479 |
is the number of positions for which the corresponding symbols are different. |
|
2480 |
Put another way, it measures the minimum number of substitutions required to change |
|
11838 | 2481 |
one into the other, or the number of errors that transformed one string into the other." |
2482 |
||
2483 |
self assert:(aString size == self size). |
|
2484 |
^ 1 to:self size count:[:idx | (self at:idx) ~= (aString at:idx)] |
|
2485 |
||
2486 |
" |
|
2487 |
'roses' hammingDistanceTo:'toned' |
|
2488 |
" |
|
2489 |
! |
|
2490 |
||
9192 | 2491 |
hash |
2492 |
"return an integer useful as a hash-key" |
|
2493 |
||
16898 | 2494 |
"/ whenever changing, also care for String>>hash. |
13879 | 2495 |
"/ immediately after any change, execute (maybe in a debugger): |
2496 |
"/ Set allSubInstancesDo:[:s | s rehash] |
|
16898 | 2497 |
^ self hash_fnv1a |
2498 |
||
2499 |
" |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2500 |
'a' hash |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2501 |
'a' asUnicode16String hash |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2502 |
'a' asUnicode32String hash |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2503 |
'aa' hash |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2504 |
'aa' asUnicode16String hash |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2505 |
'aa' asUnicode32String hash |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2506 |
'ab' hash |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2507 |
'ab' asUnicode16String hash |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2508 |
'ab' asUnicode32String hash |
13879 | 2509 |
'ab' hash |
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2510 |
'ab' asArray hash |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2511 |
" |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2512 |
|
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2513 |
" |
20012 | 2514 |
|syms ms| |
2515 |
||
2516 |
syms := Symbol allInstances. |
|
2517 |
Transcript show:'syms: '; showCR:syms size. |
|
2518 |
Transcript show:'sdbm hashes: '; showCR:(syms collect:[:s| s hash]) asSet size. |
|
2519 |
Transcript show:'dragonBook hashes: '; showCR:(syms collect:[:s| s hash_dragonBook]) asSet size. |
|
2520 |
||
2521 |
ms := Time millisecondsToRun:[ |
|
2522 |
10 timesRepeat:[ |
|
2523 |
syms do:[:each| each hash]. |
|
2524 |
]. |
|
2525 |
]. |
|
2526 |
Transcript show:'sdbm hash: '; showCR:ms. |
|
2527 |
||
2528 |
ms := Time millisecondsToRun:[ |
|
2529 |
10 timesRepeat:[ |
|
2530 |
syms do:[:each| each hash_dragonBook]. |
|
2531 |
]. |
|
2532 |
]. |
|
2533 |
Transcript show:'dragonBook: '; showCR:ms. |
|
2534 |
||
2535 |
syms := syms collect:[:each| each asUnicode16String]. |
|
2536 |
ms := Time millisecondsToRun:[ |
|
2537 |
10 timesRepeat:[ |
|
2538 |
syms do:[:each| each hash]. |
|
2539 |
]. |
|
2540 |
]. |
|
2541 |
Transcript show:'unicode sdbm hash: '; showCR:ms. |
|
2542 |
||
2543 |
ms := Time millisecondsToRun:[ |
|
2544 |
10 timesRepeat:[ |
|
2545 |
syms do:[:each| each hash_dragonBook]. |
|
2546 |
]. |
|
2547 |
]. |
|
2548 |
Transcript show:'unicode dragonBook:'; showCR:ms. |
|
14641 | 2549 |
" |
2550 |
||
13879 | 2551 |
"Modified: / 26-12-2011 / 14:09:07 / cg" |
2552 |
! |
|
2553 |
||
2554 |
hash_dragonBook |
|
2555 |
"return an integer useful as a hash-key" |
|
2556 |
||
10698
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2557 |
|h g| |
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2558 |
|
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2559 |
"/ |
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2560 |
"/ this is the dragon-book algorithm |
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2561 |
"/ |
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2562 |
h := 0. |
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2563 |
self reverseDo:[:char | |
10703
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
2564 |
"/ Sorry, stc cannot compile this (as of 10.9.2007) |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
2565 |
"/ h := (h bitShift:4) + char asciiValue. |
20012 | 2566 |
h := (h bitShift:4). |
2567 |
h := h + char codePoint. |
|
2568 |
h := h bitAnd:16rFFFFFFFF. |
|
2569 |
g := (h bitAnd: 16rF0000000). |
|
2570 |
g ~~ 0 ifTrue:[ |
|
2571 |
h := h bitXor:(g bitShift:-24). |
|
2572 |
h := h bitXor:g. |
|
2573 |
]. |
|
10698
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2574 |
]. |
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2575 |
"/ |
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2576 |
"/ multiply by large prime to spread values |
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2577 |
"/ This speeds up Set and Dictionary by a factor of 10!! |
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2578 |
"/ |
14137
1eb1b4a3f364
Inherit from UninterpretedBytes instead of ByteArray
Stefan Vogel <sv@exept.de>
parents:
14122
diff
changeset
|
2579 |
h := h times:31415821. |
10698
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2580 |
^ h |
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2581 |
|
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2582 |
" |
10703
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
2583 |
'a' hash |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
2584 |
'a' asUnicode16String hash |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
2585 |
'aa' hash |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
2586 |
'aa' asUnicode16String hash |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
2587 |
'ab' hash |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
2588 |
'ab' asUnicode16String hash |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
2589 |
'ab' hash |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
2590 |
'ab' asArray hash |
10698
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2591 |
" |
13879 | 2592 |
|
2593 |
"Created: / 26-12-2011 / 13:46:06 / cg" |
|
2594 |
! |
|
2595 |
||
16887 | 2596 |
hash_fnv1a |
2597 |
"return an integer useful as a hash-key. |
|
2598 |
This method uses the fnv-1a algorithm |
|
19743 | 2599 |
(which is actually a very good one). |
2600 |
Attention: stops when a 0-codepoint is encountered |
|
20012 | 2601 |
(for compatibility with the hash used by the VM) |
19743 | 2602 |
Also: on 64bit CPUs, only small 4-byte hashvalues are returned, |
20012 | 2603 |
(so hash values are independent from the architecture)" |
19743 | 2604 |
|
2605 |
|h byte| |
|
16887 | 2606 |
|
2607 |
h := 2166136261. |
|
2608 |
self do:[:eachChar | |
|
20012 | 2609 |
byte := eachChar codePoint. |
2610 |
byte == 0 ifTrue:[ |
|
2611 |
"/ stop |
|
2612 |
^ (h bitXor: (h >> 30)) bitAnd: 16r3FFFFFFF. |
|
2613 |
]. |
|
2614 |
h := h bitXor:byte. |
|
2615 |
h := (h * 16777619) bitAnd:16rFFFFFFFF. |
|
16887 | 2616 |
]. |
16898 | 2617 |
"/ make sure, it fits into a smallInt |
2618 |
h := (h bitXor: (h >> 30)) bitAnd: 16r3FFFFFFF. |
|
16887 | 2619 |
^ h |
2620 |
||
2621 |
" |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2622 |
'abc' hash_fnv1a |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2623 |
'abc' asUnicode16String hash_fnv1a |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2624 |
'abc' asUnicode32String hash_fnv1a |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2625 |
|
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2626 |
'foofooHelloWorld' hash_fnv1a |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2627 |
'foofooHelloWorld' asUnicode16String hash_fnv1a |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2628 |
'foofooHelloWorld' asUnicode32String hash_fnv1a |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2629 |
|
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2630 |
'blablaHelloWorld' hash_fnv1a |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2631 |
'blablaHelloWorld' asUnicode16String hash_fnv1a |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2632 |
'blablaHelloWorld' asUnicode32String hash_fnv1a |
16887 | 2633 |
" |
2634 |
! |
|
2635 |
||
2636 |
hash_java |
|
2637 |
"return an integer useful as a hash-key. |
|
2638 |
This method uses the same algorithm as used in |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2639 |
the java virtual machine |
16887 | 2640 |
(which is actually not a very good one)." |
2641 |
||
2642 |
|h| |
|
2643 |
||
2644 |
h := 0. |
|
2645 |
self do:[:eachChar | |
|
20012 | 2646 |
h := (h * 31) + (eachChar codePoint). |
2647 |
h := h bitAnd:16rFFFFFFFF. |
|
16887 | 2648 |
]. |
2649 |
^ h |
|
2650 |
||
2651 |
" |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2652 |
'abc' hash_java |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2653 |
'foofooHelloWorld' hash_java |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2654 |
'blablaHelloWorld' hash_java |
16887 | 2655 |
" |
2656 |
! |
|
2657 |
||
16898 | 2658 |
hash_sdbm |
2659 |
"return an integer useful as a hash-key. |
|
2660 |
This method implements the sdbm algorithm." |
|
2661 |
||
2662 |
|h| |
|
2663 |
||
2664 |
"/ |
|
2665 |
"/ this is the sdbm algorithm |
|
2666 |
"/ |
|
2667 |
h := 0. |
|
2668 |
self do:[:char | |
|
20012 | 2669 |
h := (65599 times:h) plus:char codePoint. |
16898 | 2670 |
]. |
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2671 |
^ h |
16898 | 2672 |
|
2673 |
" |
|
2674 |
'a' hash |
|
2675 |
'a' asUnicode16String hash |
|
2676 |
'aa' hash |
|
2677 |
'aa' asUnicode16String hash |
|
2678 |
'ab' hash |
|
2679 |
'ab' asUnicode16String hash |
|
2680 |
'ab' hash |
|
2681 |
'ab' asArray hash |
|
2682 |
" |
|
2683 |
||
2684 |
" |
|
20012 | 2685 |
|syms ms| |
2686 |
||
2687 |
syms := Symbol allInstances. |
|
2688 |
Transcript show:'syms: '; showCR:syms size. |
|
2689 |
Transcript show:'sdbm hashes: '; showCR:(syms collect:[:s| s hash]) asSet size. |
|
2690 |
Transcript show:'dragonBook hashes: '; showCR:(syms collect:[:s| s hash_dragonBook]) asSet size. |
|
2691 |
||
2692 |
ms := Time millisecondsToRun:[ |
|
2693 |
10 timesRepeat:[ |
|
2694 |
syms do:[:each| each hash]. |
|
2695 |
]. |
|
2696 |
]. |
|
2697 |
Transcript show:'sdbm hash: '; showCR:ms. |
|
2698 |
||
2699 |
ms := Time millisecondsToRun:[ |
|
2700 |
10 timesRepeat:[ |
|
2701 |
syms do:[:each| each hash_dragonBook]. |
|
2702 |
]. |
|
2703 |
]. |
|
2704 |
Transcript show:'dragonBook: '; showCR:ms. |
|
2705 |
||
2706 |
syms := syms collect:[:each| each asUnicode16String]. |
|
2707 |
ms := Time millisecondsToRun:[ |
|
2708 |
10 timesRepeat:[ |
|
2709 |
syms do:[:each| each hash]. |
|
2710 |
]. |
|
2711 |
]. |
|
2712 |
Transcript show:'unicode sdbm hash: '; showCR:ms. |
|
2713 |
||
2714 |
ms := Time millisecondsToRun:[ |
|
2715 |
10 timesRepeat:[ |
|
2716 |
syms do:[:each| each hash_dragonBook]. |
|
2717 |
]. |
|
2718 |
]. |
|
2719 |
Transcript show:'unicode dragonBook:'; showCR:ms. |
|
16898 | 2720 |
" |
2721 |
||
2722 |
"Modified: / 26-12-2011 / 14:09:07 / cg" |
|
2723 |
! |
|
2724 |
||
11861 | 2725 |
levenshteinTo:aString |
2726 |
"return the levenshtein distance to the argument, aString; |
|
2727 |
this value corresponds to the number of replacements that have to be |
|
2728 |
made to get aString from the receiver. |
|
2729 |
See IEEE transactions on Computers 1976 Pg 172 ff." |
|
2730 |
||
2731 |
" |
|
2732 |
in the following, we assume that ommiting a character |
|
2733 |
is less of an error than inserting an extra character. |
|
2734 |
Therefore the different insertion (i) and deletion (d) values. |
|
20012 | 2735 |
s: substitution weight (4) |
2736 |
k: keyboard weight (k) (typing a nearby key) - or nil (then use s) |
|
2737 |
c: case weight (4) - or nil (then use s) |
|
2738 |
e: exchange weight (8) - or nil (then use s*2) |
|
2739 |
i: insertion of extra character weight (2) |
|
2740 |
d: delete of a character weight (6) |
|
11861 | 2741 |
" |
2742 |
||
2743 |
^ StringUtilities |
|
20012 | 2744 |
levenshteinDistanceFrom:self |
2745 |
to:aString |
|
2746 |
s:4 k:4 c:4 e:nil i:2 d:6 |
|
12177 | 2747 |
|
2748 |
" |
|
2749 |
'computer' levenshteinTo:'computer' |
|
11861 | 2750 |
'cOmputer' levenshteinTo:'computer' |
12177 | 2751 |
'cOmpuTer' levenshteinTo:'computer' |
11861 | 2752 |
'cimputer' levenshteinTo:'computer' |
2753 |
'cumputer' levenshteinTo:'computer' |
|
2754 |
||
2755 |
'cmputer' levenshteinTo:'computer' |
|
2756 |
'coomputer' levenshteinTo:'computer' |
|
2757 |
||
2758 |
'ocmprt' levenshteinTo:'computer' |
|
2759 |
'computer' levenshteinTo:'computer' |
|
2760 |
'ocmputer' levenshteinTo:'computer' |
|
2761 |
'cmputer' levenshteinTo:'computer' |
|
2762 |
'computer' levenshteinTo:'cmputer' |
|
2763 |
'Computer' levenshteinTo:'computer' |
|
2764 |
||
12177 | 2765 |
'compiter' levenshteinTo:'computer' |
2766 |
'compoter' levenshteinTo:'computer' |
|
2767 |
||
2768 |
'comptuer' levenshteinTo:'computer' |
|
11861 | 2769 |
" |
2770 |
! |
|
2771 |
||
2772 |
levenshteinTo:aString s:substWeight k:kbdTypoWeight c:caseWeight i:insrtWeight d:deleteWeight |
|
2773 |
"parametrized levenshtein. |
|
2774 |
return the levenshtein distance to the argument, aString; |
|
2775 |
this value corrensponds to the number of replacements that have to be |
|
2776 |
made to get aString from the receiver. |
|
2777 |
The arguments are the costs for |
|
20012 | 2778 |
s:substitution, |
2779 |
k:keyboard type (substitution), |
|
2780 |
c:case-change, |
|
2781 |
i:insertion |
|
2782 |
d:deletion |
|
11861 | 2783 |
of a character. |
2784 |
See IEEE transactions on Computers 1976 Pg 172 ff" |
|
2785 |
||
2786 |
^ StringUtilities |
|
20012 | 2787 |
levenshteinDistanceFrom:self |
2788 |
to:aString |
|
2789 |
s:substWeight k:kbdTypoWeight c:caseWeight e:nil i:insrtWeight d:deleteWeight |
|
11861 | 2790 |
! |
2791 |
||
9190 | 2792 |
sameAs:aString |
2793 |
"Compare the receiver with the argument like =, but ignore case differences. |
|
2794 |
Return true or false." |
|
2795 |
||
2796 |
|mySize "{ Class: SmallInteger }" |
|
2797 |
otherSize c1 c2| |
|
2798 |
||
9192 | 2799 |
self == aString ifTrue:[^ true]. |
2800 |
||
2801 |
mySize := self size. |
|
2802 |
otherSize := aString string size. |
|
18884 | 2803 |
mySize ~~ otherSize ifTrue:[^ false]. |
9192 | 2804 |
|
2805 |
1 to:mySize do:[:index | |
|
20012 | 2806 |
c1 := self at:index. |
2807 |
c2 := aString at:index. |
|
2808 |
c1 ~~ c2 ifTrue:[ |
|
2809 |
(c1 sameAs:c2) ifFalse:[^ false]. |
|
2810 |
] |
|
9192 | 2811 |
]. |
2812 |
^ true |
|
2813 |
||
608 | 2814 |
" |
8874 | 2815 |
'foo' sameAs: 'Foo' |
2816 |
'foo' sameAs: 'bar' |
|
2817 |
'foo' sameAs: 'foo' |
|
9192 | 2818 |
" |
2819 |
||
2820 |
"Modified: 22.4.1996 / 15:56:17 / cg" |
|
2821 |
! |
|
608 | 2822 |
|
15734 | 2823 |
sameAs:aString caseSensitive:caseSensitive |
2824 |
"Compare the receiver with the argument. |
|
2825 |
If caseSensitive is false, this is the same as #sameAs:, |
|
2826 |
if false, this is the same as #=." |
|
2827 |
||
2828 |
caseSensitive ifFalse:[ |
|
20012 | 2829 |
^ self sameAs:aString |
15734 | 2830 |
]. |
2831 |
^ self = aString |
|
2832 |
||
2833 |
" |
|
2834 |
'foo' sameAs:'Foo' caseSensitive:false |
|
2835 |
'foo' sameAs:'foo' caseSensitive:true |
|
2836 |
" |
|
2837 |
! |
|
2838 |
||
5242 | 2839 |
sameAs:aString ignoreCase:ignoreCase |
15734 | 2840 |
<resource: #obsolete> |
5242 | 2841 |
"Compare the receiver with the argument. |
2842 |
If ignoreCase is true, this is the same as #sameAs:, |
|
2843 |
if false, this is the same as #=." |
|
2844 |
||
2845 |
ignoreCase ifTrue:[ |
|
20012 | 2846 |
^ self sameAs:aString |
5242 | 2847 |
]. |
2848 |
^ self = aString |
|
2849 |
||
2850 |
" |
|
8874 | 2851 |
'foo' sameAs:'Foo' ignoreCase:false |
2852 |
'foo' sameAs:'foo' ignoreCase:true |
|
5242 | 2853 |
" |
2854 |
||
2855 |
! |
|
2856 |
||
608 | 2857 |
sameCharacters:aString |
9192 | 2858 |
"count & return the number of characters which are the same |
2859 |
(ignoring case and emphasis) in the receiver and the argument, aString." |
|
2860 |
||
2861 |
|n "{ Class: SmallInteger }" |
|
2862 |
c1 c2 cnt| |
|
2863 |
||
2864 |
n := self size. |
|
2865 |
n := n min:(aString string size). |
|
2866 |
||
2867 |
cnt := 0. |
|
2868 |
1 to:n do:[:index | |
|
20012 | 2869 |
c1 := self at:index. |
2870 |
c2 := aString at:index. |
|
2871 |
((c1 == c2) |
|
2872 |
or:[c1 asLowercase = c2 asLowercase]) ifTrue:[ |
|
2873 |
cnt := cnt + 1 |
|
2874 |
] |
|
9192 | 2875 |
]. |
2876 |
^ cnt |
|
2877 |
||
2878 |
" |
|
2879 |
'foobarbaz' sameCharacters: 'foo' |
|
8874 | 2880 |
'foobarbaz' sameCharacters: 'Foo' |
2881 |
'foobarbaz' sameCharacters: 'baz' |
|
608 | 2882 |
" |
5510
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
2883 |
! |
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
2884 |
|
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
2885 |
sameEmphasisAs:aStringOrText |
17215 | 2886 |
"compare the receiver's and the argument's emphasis" |
5510
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
2887 |
|
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
2888 |
^ self emphasis = aStringOrText emphasis |
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
2889 |
|
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
2890 |
" |
8874 | 2891 |
'hello' asText sameEmphasisAs: 'hello' |
2892 |
'hello' asText sameEmphasisAs: 'hello' asText |
|
2893 |
'hello' asText allBold sameEmphasisAs: 'hello' |
|
2894 |
'hello' asText allBold sameEmphasisAs: 'fooba' asText allBold |
|
9192 | 2895 |
'hello' asText allBold sameEmphasisAs: 'fooba' asText allItalic |
2896 |
" |
|
2897 |
! |
|
2898 |
||
9191 | 2899 |
sameStringAndEmphasisAs:aStringOrText |
5739
4fd3be7ccc75
oops some relational ops did accept a non-string arg
Claus Gittinger <cg@exept.de>
parents:
5732
diff
changeset
|
2900 |
"compare both emphasis and string of the receiver and the argument" |
5510
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
2901 |
|
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
2902 |
aStringOrText isString ifFalse:[^ false]. |
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
2903 |
(self string = aStringOrText string) ifFalse:[^ false]. |
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
2904 |
self hasChangeOfEmphasis = aStringOrText hasChangeOfEmphasis ifFalse:[^ false]. |
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
2905 |
^ self emphasis = aStringOrText emphasis |
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
2906 |
|
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
2907 |
" |
8874 | 2908 |
'hello' asText sameEmphasisAs: 'hello' |
2909 |
'hello' asText sameEmphasisAs: 'hello' asText |
|
2910 |
'hello' asText allBold sameEmphasisAs: 'hello' |
|
2911 |
'hello' asText allBold sameEmphasisAs: 'fooba' asText allBold |
|
2912 |
'hello' asText allBold sameEmphasisAs: 'fooba' asText allItalic |
|
2913 |
||
2914 |
'hello' sameEmphasisAs: 'hello' asText |
|
2915 |
'hello' sameEmphasisAs: 'hello' asText allBold |
|
2916 |
'hello' sameEmphasisAs: 'fooba' |
|
2917 |
'hello' sameEmphasisAs: 'fooba' asText |
|
2918 |
'hello' sameEmphasisAs: 'fooba' asText allBold |
|
2919 |
'hello' sameEmphasisAs: 'fooba' asText allItalic |
|
2920 |
||
2921 |
'hello' asText sameStringAndEmphasisAs: 'hello' |
|
2922 |
'hello' asText sameStringAndEmphasisAs: 'hello' asText |
|
2923 |
'hello' asText allBold sameStringAndEmphasisAs: 'hello' |
|
2924 |
'hello' asText allBold sameStringAndEmphasisAs: 'fooba' asText allBold |
|
2925 |
'hello' asText allBold sameStringAndEmphasisAs: 'fooba' asText allItalic |
|
2926 |
||
2927 |
'hello' sameStringAndEmphasisAs: 'hello' asText |
|
2928 |
'hello' sameStringAndEmphasisAs: 'hello' asText allBold |
|
2929 |
'hello' sameStringAndEmphasisAs: 'fooba' |
|
2930 |
'hello' sameStringAndEmphasisAs: 'fooba' asText |
|
2931 |
'hello' sameStringAndEmphasisAs: 'fooba' asText allBold |
|
2932 |
'hello' sameStringAndEmphasisAs: 'fooba' asText allItalic |
|
5510
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
2933 |
" |
11861 | 2934 |
! |
2935 |
||
2936 |
spellAgainst: aString |
|
2937 |
"return an integer between 0 and 100 indicating how similar |
|
2938 |
the argument is to the receiver. No case conversion is done. |
|
2939 |
This algorithm is much simpler (but also less exact) than the |
|
2940 |
levenshtein distance. Experiment which is better for your |
|
2941 |
application." |
|
2942 |
||
2943 |
| i1 "{ Class: SmallInteger }" |
|
2944 |
i2 "{ Class: SmallInteger }" |
|
2945 |
next1 "{ Class: SmallInteger }" |
|
2946 |
next2 "{ Class: SmallInteger }" |
|
2947 |
size1 "{ Class: SmallInteger }" |
|
2948 |
size2 "{ Class: SmallInteger }" |
|
2949 |
score "{ Class: SmallInteger }" |
|
2950 |
maxLen "{ Class: SmallInteger }" | |
|
2951 |
||
2952 |
size1 := self size. |
|
2953 |
size2 := aString size. |
|
2954 |
maxLen := size1 max:size2. |
|
2955 |
score := 0. |
|
2956 |
i1 := i2 := 1. |
|
2957 |
[i1 <= size1 and: [i2 <= size2]] whileTrue:[ |
|
20012 | 2958 |
next1 := i1 + 1. |
2959 |
next2 := i2 + 1. |
|
2960 |
(self at:i1) == (aString at:i2) ifTrue: [ |
|
2961 |
score := score+1. |
|
2962 |
i1 := next1. |
|
2963 |
i2 := next2 |
|
2964 |
] ifFalse: [ |
|
2965 |
(i2 < size2 and: [(self at:i1) == (aString at:next2)]) ifTrue: [ |
|
2966 |
i2 := next2 |
|
2967 |
] ifFalse: [ |
|
2968 |
(i1 < size1 and: [(self at:next1) == (aString at:i2)]) ifTrue: [ |
|
2969 |
i1 := next1 |
|
2970 |
] ifFalse: [ |
|
2971 |
i1 := next1. |
|
2972 |
i2 := next2 |
|
2973 |
] |
|
2974 |
] |
|
2975 |
] |
|
11861 | 2976 |
]. |
2977 |
||
2978 |
score == maxLen ifTrue: [^ 100]. |
|
2979 |
^ 100 * score // maxLen |
|
2980 |
||
2981 |
" |
|
2982 |
'Smalltalk' spellAgainst: 'Smalltlak' |
|
2983 |
'Smalltalk' spellAgainst: 'smalltlak' |
|
2984 |
'Smalltalk' spellAgainst: 'smalltalk' |
|
2985 |
'Smalltalk' spellAgainst: 'smalltlk' |
|
2986 |
'Smalltalk' spellAgainst: 'Smalltolk' |
|
2987 |
" |
|
2988 |
! |
|
2989 |
||
16553 | 2990 |
startsWith:aStringOrCharacter |
2991 |
"return true, if the receiver starts with something, aStringOrCharacter. |
|
17371 | 2992 |
If the argument is empty, true is returned. |
2993 |
Notice, that this is similar to, but slightly different from VW's and Squeak's beginsWith:, |
|
2994 |
which are both inconsistent w.r.t. an empty argument." |
|
11861 | 2995 |
|
2996 |
|s| |
|
2997 |
||
16553 | 2998 |
aStringOrCharacter isCharacter ifTrue:[ |
20012 | 2999 |
^ (self size > 0) and:[ (self at:1) == aStringOrCharacter ] |
16553 | 3000 |
]. |
11861 | 3001 |
(s := self string) ~~ self ifTrue:[ |
20012 | 3002 |
^ s startsWith:aStringOrCharacter |
11861 | 3003 |
]. |
16553 | 3004 |
^ super startsWith:aStringOrCharacter |
11861 | 3005 |
|
3006 |
" |
|
17364 | 3007 |
'abcde' startsWith:#($a $b $c) |
3008 |
'abcde' startsWith:'abc' |
|
3009 |
'abcd' startsWith:'abcde' |
|
3010 |
'abcde' startsWith:'abd' |
|
11861 | 3011 |
'hello world' startsWith:'hello' |
3012 |
'hello world' asText allBold startsWith:'hello' |
|
3013 |
'hello world' asText allBold startsWith:'' |
|
3014 |
" |
|
3015 |
||
3016 |
"Created: 12.5.1996 / 15:46:40 / cg" |
|
3017 |
"Modified: 12.5.1996 / 15:49:24 / cg" |
|
9192 | 3018 |
! ! |
3019 |
||
3020 |
!CharacterArray methodsFor:'converting'! |
|
3021 |
||
11881 | 3022 |
asAsciiZ |
3023 |
"if the receiver does not end with a 0-valued character, return a copy of it, |
|
3024 |
with an additional 0-character. Otherwise return the receiver. This is sometimes |
|
3025 |
needed when a string has to be passed to C, which needs 0-terminated strings. |
|
3026 |
Notice, that all singleByte strings are already 0-terminated in ST/X, whereas wide |
|
3027 |
strings are not." |
|
3028 |
||
12658 | 3029 |
(self notEmpty and:[self last codePoint == 0]) ifTrue:[^ self ]. |
11881 | 3030 |
^ self copyWith:(Character value:0). |
3031 |
||
3032 |
" |
|
3033 |
'abc' asAsciiZ |
|
3034 |
'abc' asWideString asAsciiZ |
|
3035 |
" |
|
3036 |
! |
|
3037 |
||
13524 | 3038 |
asByteArray |
15936 | 3039 |
"depending on the size of the characters in the receiver, |
3040 |
return a byteArray containing single-, double- or even 4-bytewise values. |
|
3041 |
The size of the returned byteArray will be the strings size multiplied by the |
|
3042 |
size required for the largest character. |
|
19233 | 3043 |
Attention: The bytes are in native byte order. |
15936 | 3044 |
Caveat: better use utf8Encoded, to get reproducable results" |
3045 |
||
14555 | 3046 |
|bytes sz bytesPerCharacter idx str| |
3047 |
||
3048 |
str := self string. |
|
3049 |
str ~~ self ifTrue:[ |
|
20012 | 3050 |
"/ for text and other wrappers |
3051 |
^ str asByteArray |
|
13611
ba8c7372218a
variable renamed in: #inspector2TabBytes
Claus Gittinger <cg@exept.de>
parents:
13524
diff
changeset
|
3052 |
]. |
ba8c7372218a
variable renamed in: #inspector2TabBytes
Claus Gittinger <cg@exept.de>
parents:
13524
diff
changeset
|
3053 |
|
ba8c7372218a
variable renamed in: #inspector2TabBytes
Claus Gittinger <cg@exept.de>
parents:
13524
diff
changeset
|
3054 |
"/ for real strings, a fallback |
13524 | 3055 |
sz := self size. |
19757 | 3056 |
bytesPerCharacter := self bytesPerCharacter. |
13524 | 3057 |
bytes := ByteArray new:(sz * bytesPerCharacter). |
3058 |
idx := 1. |
|
3059 |
self do:[:char | |
|
20012 | 3060 |
|code| |
3061 |
||
3062 |
code := char codePoint. |
|
3063 |
bytesPerCharacter == 2 ifTrue:[ |
|
3064 |
bytes unsignedInt16At:idx put:code |
|
3065 |
] ifFalse:[ |
|
3066 |
bytesPerCharacter == 4 ifTrue:[ |
|
3067 |
bytes unsignedInt32At:idx put:code |
|
3068 |
] ifFalse:[ |
|
3069 |
bytes at:idx put:code |
|
3070 |
]. |
|
3071 |
]. |
|
3072 |
idx := idx + bytesPerCharacter. |
|
13524 | 3073 |
]. |
3074 |
^ bytes |
|
3075 |
||
3076 |
"Created: / 27-07-2011 / 00:56:17 / cg" |
|
3077 |
! |
|
3078 |
||
19233 | 3079 |
asByteArrayMSB:msb |
3080 |
"depending on the size of the characters in the receiver, |
|
3081 |
return a byteArray containing single-, double- or even 4-bytewise values. |
|
3082 |
The size of the returned byteArray will be the strings size multiplied by the |
|
3083 |
size required for the largest character. |
|
3084 |
Caveat: better use utf8Encoded, to get reproducable results" |
|
3085 |
||
3086 |
|ba| |
|
3087 |
||
3088 |
ba := self asByteArray. "/ native order |
|
3089 |
UninterpretedBytes isBigEndian ~~ msb ifTrue:[ |
|
20012 | 3090 |
ba swapBytes |
19233 | 3091 |
]. |
3092 |
^ ba |
|
3093 |
! |
|
3094 |
||
608 | 3095 |
asCollectionOfLines |
8874 | 3096 |
"return a collection containing the lines (separated by cr) |
608 | 3097 |
of the receiver. If multiple cr's occur in a row, the result will |
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
3098 |
contain empty strings. |
14456
d7c3e67647f1
comment/format in: #asCollectionOfLines
Claus Gittinger <cg@exept.de>
parents:
14455
diff
changeset
|
3099 |
If the string ends with a cr, an empty line will be found as last element of the resulting collection. |
d7c3e67647f1
comment/format in: #asCollectionOfLines
Claus Gittinger <cg@exept.de>
parents:
14455
diff
changeset
|
3100 |
See also #asCollectionOfLinesWithReturn |
d7c3e67647f1
comment/format in: #asCollectionOfLines
Claus Gittinger <cg@exept.de>
parents:
14455
diff
changeset
|
3101 |
(would have rather changed this method instead of adding another one, but a lot of code already uses |
d7c3e67647f1
comment/format in: #asCollectionOfLines
Claus Gittinger <cg@exept.de>
parents:
14455
diff
changeset
|
3102 |
this method and we did not want to risk any incompatibilities)" |
14455
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3103 |
|
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3104 |
^ self asCollectionOfSubstringsSeparatedBy:Character cr. |
608 | 3105 |
|
3106 |
" |
|
3107 |
'1 one\2 two\3 three\4 four\5 five' withCRs asCollectionOfLines |
|
8874 | 3108 |
'1 one\\\\2 two\3 three' withCRs asCollectionOfLines |
608 | 3109 |
" |
3110 |
! |
|
3111 |
||
14455
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3112 |
asCollectionOfLinesWithReturn |
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3113 |
"return a collection containing the lines (separated by cr) |
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3114 |
of the receiver. If multiple cr's occur in a row, the result will |
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3115 |
contain empty strings." |
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3116 |
|
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3117 |
|lines| |
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3118 |
|
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3119 |
lines := self asCollectionOfSubstringsSeparatedBy:Character cr. |
18194 | 3120 |
(lines notEmpty and:[lines last isEmpty]) ifTrue:[ |
20012 | 3121 |
^ lines copyButLast:1 |
14455
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3122 |
]. |
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3123 |
^ lines |
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3124 |
|
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3125 |
" |
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3126 |
'1\2\3' withCRs asCollectionOfLines |
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3127 |
'1\2\3\' withCRs asCollectionOfLines |
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3128 |
'1\2\3' withCRs asCollectionOfLinesWithReturn |
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3129 |
'1\2\3\' withCRs asCollectionOfLinesWithReturn |
18194 | 3130 |
'' withCRs asCollectionOfLinesWithReturn |
14455
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3131 |
" |
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3132 |
! |
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3133 |
|
608 | 3134 |
asCollectionOfSubstringsSeparatedBy:aCharacter |
15740 | 3135 |
"return a collection containing substrings (separated by aCharacter) |
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
3136 |
of the receiver. |
15740 | 3137 |
If aCharacter occurs multiple times in a row, the result will contain empty strings. |
3138 |
If the receiver ends with aCharacter, an empty string with be the last result element." |
|
608 | 3139 |
|
9192 | 3140 |
^ self asCollectionOfSubCollectionsSeparatedBy:aCharacter |
3141 |
||
3142 |
" |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
3143 |
'1 one:2 two:3 three:4 four:5 five' asCollectionOfSubstringsSeparatedBy:$: |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
3144 |
'1 one:2 two:3 three:4 four:5 five:' asCollectionOfSubstringsSeparatedBy:$: |
15740 | 3145 |
'1 one 2 two 3 three 4 four 5 five' asCollectionOfSubstringsSeparatedBy:Character space |
9192 | 3146 |
" |
3147 |
! |
|
608 | 3148 |
|
11828 | 3149 |
asCollectionOfSubstringsSeparatedBy:aCharacter exceptIn:ch |
15740 | 3150 |
"return a collection containing the substrings (separated by aCharacter) |
12177 | 3151 |
of the receiver. If aCharacter occurs multiple times in a row, |
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
3152 |
the result will contain empty strings. |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
3153 |
The separation is not done, inside a matching pair of ch-substrings. |
15740 | 3154 |
Can be used to tokenize csv-like strings, which may or may not be enclosed in quotes." |
11828 | 3155 |
|
3156 |
|lines myClass except i c |
|
3157 |
startIndex "{ Class:SmallInteger }" |
|
3158 |
stopIndex "{ Class:SmallInteger }" | |
|
3159 |
||
11905
cbc2191cfe2d
return stringCollections from asCollectionOfXXX
Claus Gittinger <cg@exept.de>
parents:
11904
diff
changeset
|
3160 |
lines := StringCollection new. |
11828 | 3161 |
myClass := self species. |
3162 |
||
3163 |
startIndex := 1. |
|
3164 |
except := false. |
|
3165 |
[ |
|
20012 | 3166 |
i := startIndex-1. |
3167 |
[ |
|
3168 |
i := i+1. |
|
3169 |
c := self at:i. |
|
3170 |
c = ch ifTrue:[ except := except not. ]. |
|
3171 |
i < self size and:[except or:[c ~= aCharacter]] |
|
3172 |
] whileTrue. |
|
3173 |
||
3174 |
c = aCharacter ifTrue:[ |
|
3175 |
stopIndex := i -1. |
|
3176 |
] ifFalse: [ |
|
3177 |
stopIndex := i. |
|
3178 |
]. |
|
3179 |
(stopIndex < startIndex) ifTrue: [ |
|
3180 |
lines add:(myClass new:0) |
|
3181 |
] ifFalse: [ |
|
3182 |
lines add:(self copyFrom:startIndex to:stopIndex) |
|
3183 |
]. |
|
3184 |
startIndex := stopIndex + 2. |
|
3185 |
startIndex <= self size |
|
11828 | 3186 |
] whileTrue. |
3187 |
^ lines |
|
3188 |
||
3189 |
" |
|
12177 | 3190 |
'asd''f;d''dd;s' asCollectionOfSubstringsSeparatedBy:$; exceptIn:$' |
3191 |
" |
|
3192 |
"/ 'asd "hello bla" foo "bla bla" bar' asCollectionOfSubstringsSeparatedBy:$ exceptIn:$" |
|
11828 | 3193 |
! |
3194 |
||
13344
ec6860c49990
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13343
diff
changeset
|
3195 |
asCollectionOfSubstringsSeparatedBy:aFieldSeparatorString textSeparator:aTextSeparatorOrNil |
13343
3f058ad0d2f1
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13332
diff
changeset
|
3196 |
"return a collection containing the words (separated by aFieldSeparatorString) of the receiver. |
13327
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3197 |
Individual words might be enclosed in aTextSeparator characters, in case they contain blanks or fieldSeparators. |
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3198 |
Typically used for CSV line parsing, with a $; as aFieldSeparator and $'' (dquote) as textSeparator." |
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3199 |
|
13344
ec6860c49990
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13343
diff
changeset
|
3200 |
|aTextSeparatorChar items scanningWord inStream element lastIsFieldSeparator sz| |
ec6860c49990
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13343
diff
changeset
|
3201 |
|
ec6860c49990
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13343
diff
changeset
|
3202 |
aTextSeparatorOrNil isNil ifTrue:[ |
19961 | 3203 |
^ self asCollectionOfSubstringsSeparatedByAll: aFieldSeparatorString |
13327
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3204 |
]. |
13344
ec6860c49990
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13343
diff
changeset
|
3205 |
sz := aTextSeparatorOrNil size. |
ec6860c49990
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13343
diff
changeset
|
3206 |
sz = 0 ifTrue:[ |
19961 | 3207 |
aTextSeparatorChar := aTextSeparatorOrNil |
13344
ec6860c49990
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13343
diff
changeset
|
3208 |
] ifFalse:[sz = 1 ifTrue:[ |
19961 | 3209 |
"this is a String. Fetch the first character - compatibility to older expecco libs" |
3210 |
aTextSeparatorChar := aTextSeparatorOrNil first. |
|
13344
ec6860c49990
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13343
diff
changeset
|
3211 |
] ifFalse:[ |
19961 | 3212 |
self error:'textSeparatorSize > 1'. |
13344
ec6860c49990
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13343
diff
changeset
|
3213 |
]]. |
13327
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3214 |
|
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3215 |
items := OrderedCollection new. |
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3216 |
|
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3217 |
inStream := ReadStream on:self. |
13882
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
3218 |
[ |
19961 | 3219 |
inStream skipSeparators. |
3220 |
inStream atEnd |
|
13327
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3221 |
] whileFalse:[ |
19961 | 3222 |
lastIsFieldSeparator := false. |
3223 |
inStream peek == aTextSeparatorChar ifTrue:[ |
|
3224 |
inStream next. |
|
3225 |
element := ''. |
|
3226 |
scanningWord := true. |
|
3227 |
[ scanningWord and:[inStream atEnd not] ] whileTrue:[ |
|
3228 |
element := element , (inStream upTo:aTextSeparatorChar). |
|
3229 |
(inStream peek == aTextSeparatorChar) ifTrue:[ |
|
3230 |
element := element , aTextSeparatorChar . |
|
3231 |
inStream next. |
|
3232 |
] ifFalse:[ |
|
3233 |
scanningWord := false. |
|
3234 |
]. |
|
3235 |
]. |
|
3236 |
inStream upToAll_positionBefore:aFieldSeparatorString. |
|
3237 |
] ifFalse:[ |
|
3238 |
element := inStream upToAll_positionBefore:aFieldSeparatorString |
|
3239 |
]. |
|
3240 |
items add:element. |
|
3241 |
lastIsFieldSeparator := (inStream skipThroughAll:aFieldSeparatorString) notNil. |
|
13343
3f058ad0d2f1
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13332
diff
changeset
|
3242 |
]. |
3f058ad0d2f1
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13332
diff
changeset
|
3243 |
lastIsFieldSeparator ifTrue:[ |
19961 | 3244 |
"empty element at the end of the line" |
3245 |
items add:''. |
|
13327
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3246 |
]. |
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3247 |
|
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3248 |
^ items |
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3249 |
|
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3250 |
" |
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3251 |
self assert:(('#First#, #Second,SecondAdd#, #Third#' asCollectionOfSubstringsSeparatedBy:',' textSeparator: $#) |
19961 | 3252 |
sameContentsAs:#('First' 'Second,SecondAdd' 'Third')). |
13327
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3253 |
self assert:(('#Fir##st#, #Second,SecondAdd#, #Third#' asCollectionOfSubstringsSeparatedBy:',' textSeparator: $#) |
19961 | 3254 |
sameContentsAs:#('Fir#st' 'Second,SecondAdd' 'Third')). |
13327
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3255 |
self assert:(('#Fir##st#, Second,SecondAdd, #Third#' asCollectionOfSubstringsSeparatedBy:',' textSeparator: $#) |
19961 | 3256 |
sameContentsAs:#('Fir#st' 'Second' 'SecondAdd' 'Third')). |
13343
3f058ad0d2f1
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13332
diff
changeset
|
3257 |
self assert:(('First,Second,Third,,' asCollectionOfSubstringsSeparatedBy:',' textSeparator:nil) |
19961 | 3258 |
sameContentsAs:#('First' 'Second' 'Third' '' '')). |
13344
ec6860c49990
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13343
diff
changeset
|
3259 |
self assert:(('First,Second,Third,,' asCollectionOfSubstringsSeparatedBy:',' textSeparator:'#') |
19961 | 3260 |
sameContentsAs:#('First' 'Second' 'Third' '' '')). |
13327
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3261 |
" |
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3262 |
|
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3263 |
"Modified: / 07-04-2011 / 13:23:19 / cg" |
10501
999589445aa7
add new method for splitting a csv string into csv elements
ab
parents:
10437
diff
changeset
|
3264 |
! |
999589445aa7
add new method for splitting a csv string into csv elements
ab
parents:
10437
diff
changeset
|
3265 |
|
7339
20969b5770c9
asCollectionOfSubstringsSeparatedByAll:
Claus Gittinger <cg@exept.de>
parents:
7318
diff
changeset
|
3266 |
asCollectionOfSubstringsSeparatedByAll:aSeparatorString |
8874 | 3267 |
"return a collection containing the lines (separated by aSeparatorString) |
3268 |
of the receiver. If aSeparatorString occurs multiple times in a row, |
|
7339
20969b5770c9
asCollectionOfSubstringsSeparatedByAll:
Claus Gittinger <cg@exept.de>
parents:
7318
diff
changeset
|
3269 |
the result will contain empty strings." |
20969b5770c9
asCollectionOfSubstringsSeparatedByAll:
Claus Gittinger <cg@exept.de>
parents:
7318
diff
changeset
|
3270 |
|
8852
9f5984eff2c2
use asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8794
diff
changeset
|
3271 |
^ self asCollectionOfSubCollectionsSeparatedByAll:aSeparatorString |
9f5984eff2c2
use asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8794
diff
changeset
|
3272 |
|
9f5984eff2c2
use asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8794
diff
changeset
|
3273 |
" |
8874 | 3274 |
'1::2::3::4::5::' asCollectionOfSubstringsSeparatedByAll:'::' |
7339
20969b5770c9
asCollectionOfSubstringsSeparatedByAll:
Claus Gittinger <cg@exept.de>
parents:
7318
diff
changeset
|
3275 |
" |
20969b5770c9
asCollectionOfSubstringsSeparatedByAll:
Claus Gittinger <cg@exept.de>
parents:
7318
diff
changeset
|
3276 |
! |
20969b5770c9
asCollectionOfSubstringsSeparatedByAll:
Claus Gittinger <cg@exept.de>
parents:
7318
diff
changeset
|
3277 |
|
608 | 3278 |
asCollectionOfSubstringsSeparatedByAny:aCollectionOfSeparators |
3279 |
"return a collection containing the words (separated by any character |
|
3280 |
from aCollectionOfSeparators) of the receiver. |
|
3281 |
This allows breaking up strings using any character as separator." |
|
3282 |
||
8852
9f5984eff2c2
use asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8794
diff
changeset
|
3283 |
^ self asCollectionOfSubCollectionsSeparatedByAny:aCollectionOfSeparators |
608 | 3284 |
|
3285 |
" |
|
3286 |
'hello:world:isnt:this nice' asCollectionOfSubstringsSeparatedByAny:#($:) |
|
8874 | 3287 |
'hello:world:isnt:this nice' asCollectionOfSubstringsSeparatedByAny:':' |
3288 |
'hello:world:isnt:this nice' asCollectionOfSubstringsSeparatedByAny:(Array with:$: with:Character space) |
|
3289 |
'hello:world:isnt:this nice' asCollectionOfSubstringsSeparatedByAny:': ' |
|
3290 |
'h1e2l3l4o' asCollectionOfSubstringsSeparatedByAny:($1 to: $9) |
|
608 | 3291 |
" |
3292 |
! |
|
3293 |
||
3294 |
asCollectionOfWords |
|
8874 | 3295 |
"return a collection containing the words (separated by whitespace) |
3296 |
of the receiver. Multiple occurrences of whitespace characters will |
|
17517 | 3297 |
be treated like one - i.e. whitespace is skipped. |
3298 |
(sigh: it is called #'subStrings' in V'Age, and #'substrings' in Squeak) " |
|
608 | 3299 |
|
7797 | 3300 |
|words| |
608 | 3301 |
|
11905
cbc2191cfe2d
return stringCollections from asCollectionOfXXX
Claus Gittinger <cg@exept.de>
parents:
11904
diff
changeset
|
3302 |
words := StringCollection new. |
7797 | 3303 |
self asCollectionOfWordsDo:[:w | words add:w]. |
608 | 3304 |
^ words |
3305 |
||
3306 |
" |
|
3307 |
'hello world isnt this nice' asCollectionOfWords |
|
3308 |
' hello world isnt this nice ' asCollectionOfWords |
|
3309 |
'hello' asCollectionOfWords |
|
3310 |
'' asCollectionOfWords |
|
3311 |
' ' asCollectionOfWords |
|
10703
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
3312 |
' foo bar__baz__bla__ bar ' asCollectionOfWords |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
3313 |
' foo __bar__baz__bla__ bar ' asCollectionOfWords |
608 | 3314 |
" |
9192 | 3315 |
! |
3316 |
||
3317 |
asCollectionOfWordsDo:aBlock |
|
8874 | 3318 |
"evaluate aBlock for each word (separated by whitespace) of the receiver. |
3319 |
Multiple occurrences of whitespace characters will be treated like one |
|
7797 | 3320 |
- i.e. whitespace is skipped. |
3321 |
Returns the number of words (i.e. the number of invocations of aBlock)." |
|
3322 |
||
3323 |
|count "{ Class:SmallInteger }" |
|
8874 | 3324 |
start "{ Class:SmallInteger }" |
3325 |
stop "{ Class:SmallInteger }" |
|
7797 | 3326 |
mySize "{ Class:SmallInteger }"| |
3327 |
||
3328 |
count := 0. |
|
3329 |
start := 1. |
|
3330 |
mySize := self size. |
|
3331 |
[start <= mySize] whileTrue:[ |
|
20012 | 3332 |
start := self indexOfNonSeparatorStartingAt:start. |
3333 |
start == 0 ifTrue:[ |
|
3334 |
^ count |
|
3335 |
]. |
|
3336 |
stop := self indexOfSeparatorStartingAt:start. |
|
3337 |
stop == 0 ifTrue:[ |
|
3338 |
aBlock value:(self copyFrom:start to:mySize). |
|
3339 |
^ count + 1 |
|
3340 |
]. |
|
3341 |
aBlock value:(self copyFrom:start to:(stop - 1)). |
|
3342 |
start := stop. |
|
3343 |
count := count + 1 |
|
7797 | 3344 |
]. |
3345 |
^ count |
|
3346 |
||
3347 |
" |
|
3348 |
'hello world isnt this nice' asCollectionOfWordsDo:[:w | Transcript showCR:w] |
|
3349 |
' hello world isnt this nice ' asCollectionOfWordsDo:[:w | Transcript showCR:w] |
|
3350 |
'hello' asCollectionOfWordsDo:[:w | Transcript showCR:w] |
|
3351 |
'' asCollectionOfWordsDo:[:w | Transcript showCR:w] |
|
3352 |
' ' asCollectionOfWordsDo:[:w | Transcript showCR:w] |
|
3353 |
" |
|
9192 | 3354 |
! |
3355 |
||
3356 |
asFilename |
|
3357 |
"return a Filename with pathname taken from the receiver" |
|
3358 |
||
3359 |
^ Filename named:self "(self asSingleByteStringReplaceInvalidWith:$?)" |
|
3360 |
||
3361 |
"Modified: 20.5.1996 / 09:38:15 / cg" |
|
3362 |
! |
|
608 | 3363 |
|
3060 | 3364 |
asFixedPoint |
3365 |
"read a fixedPoint number from the receiver. |
|
8874 | 3366 |
Notice, that errors may occur during the read, |
7423 | 3367 |
so you better setup some signal handler when using this method." |
3060 | 3368 |
|
3369 |
^ FixedPoint readFromString:self |
|
3370 |
||
3371 |
" |
|
8874 | 3372 |
'0.123' asFixedPoint |
3373 |
'12345' asFixedPoint |
|
3060 | 3374 |
'(1/5)' asFixedPoint |
3375 |
'foo' asFixedPoint |
|
9192 | 3376 |
Object errorSignal handle:[:ex | ex return:0] do:['foo' asFixedPoint] |
3377 |
" |
|
3378 |
||
3379 |
"Modified: / 25.10.1997 / 15:19:00 / cg" |
|
3380 |
! |
|
3381 |
||
3382 |
asFixedPoint:scale |
|
3060 | 3383 |
"read a fixedPoint number with scale number of post-decimal digits |
3384 |
from the receiver. Scale controls the number of displayed digits, |
|
3385 |
not the number of actually valid digits. |
|
8874 | 3386 |
Notice, that errors may occur during the read, |
7423 | 3387 |
so you better setup some signal handler when using this method." |
3060 | 3388 |
|
3389 |
^ (FixedPoint readFromString:self) scale:scale |
|
3390 |
||
3391 |
" |
|
8874 | 3392 |
'0.123' asFixedPoint:2 |
3393 |
'123456' asFixedPoint:2 |
|
3394 |
('3.14157' asFixedPoint:1) asFixedPoint:5 |
|
3395 |
'3.14157' asFixedPoint:2 |
|
3396 |
'foo' asFixedPoint:2 |
|
3060 | 3397 |
" |
3398 |
||
3399 |
"Modified: / 25.10.1997 / 15:21:57 / cg" |
|
9192 | 3400 |
! |
3401 |
||
3402 |
asFloat |
|
608 | 3403 |
"read a float number from the receiver. |
8874 | 3404 |
Notice, that errors may occur during the read, |
7422 | 3405 |
so you better setup some exception handler when using this method." |
608 | 3406 |
|
3407 |
^ (Number readFromString:self) asFloat |
|
3408 |
||
3409 |
" |
|
8874 | 3410 |
'0.123' asFloat |
608 | 3411 |
'12345' asFloat |
3412 |
'(1/5)' asFloat |
|
8874 | 3413 |
Object errorSignal handle:[:ex | ex return:0] do:['foo' asFloat] |
608 | 3414 |
" |
3415 |
! |
|
3416 |
||
3417 |
asInteger |
|
9190 | 3418 |
"convert the receiver into an integer. |
8874 | 3419 |
Notice, that errors may occur during the read, |
9190 | 3420 |
so you better setup some exception handler when using this method. |
3421 |
Also notice, that this method here is more strict than the code found |
|
10703
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
3422 |
in other smalltalks. |
9190 | 3423 |
For less strict integer reading, use Integer readFrom:aString" |
608 | 3424 |
|
3425 |
^ Integer readFromString:self |
|
3426 |
||
3427 |
" |
|
3428 |
'12345678901234567890' asInteger |
|
3429 |
'-1234' asInteger |
|
9190 | 3430 |
|
3431 |
The following raises an error: |
|
20012 | 3432 |
'0.123' asInteger <- reader finds more after reading 0 |
9190 | 3433 |
|
3434 |
whereas the less strict readFrom does not: |
|
20012 | 3435 |
Integer readFrom:'0.123' <- reader stops at ., returning 0 |
10703
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
3436 |
|
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
3437 |
'0.123' asInteger |
608 | 3438 |
'0.123' asNumber <- returns what you expect |
8874 | 3439 |
Object errorSignal handle:[:ex | ex return:0] do:['foo' asInteger] |
9190 | 3440 |
|
3441 |
" |
|
9192 | 3442 |
! |
3443 |
||
3444 |
asLowercase |
|
608 | 3445 |
"return a copy of myself in lowercase letters" |
3446 |
||
8003
6e690f4b79f1
asLowercase/asUppercase and friends care for 16-bit chars
Claus Gittinger <cg@exept.de>
parents:
7995
diff
changeset
|
3447 |
|newStr c bitsPerCharacter |
9192 | 3448 |
mySize "{ Class: SmallInteger }" | |
3449 |
||
3450 |
mySize := self size. |
|
3451 |
mySize == 0 ifTrue:[^ self]. |
|
18657 | 3452 |
|
9192 | 3453 |
newStr := self species new:mySize. |
3454 |
bitsPerCharacter := newStr bitsPerCharacter. |
|
3455 |
||
18657 | 3456 |
"/ handle the very seldom case of an uppercase char which needs |
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
3457 |
"/ more bits in its lowercase variant |
18657 | 3458 |
"/ (there are only a few of them) |
3459 |
||
9192 | 3460 |
1 to:mySize do:[:i | |
20012 | 3461 |
c := (self at:i) asLowercase. |
3462 |
(c bitsPerCharacter > bitsPerCharacter |
|
3463 |
and:[c stringSpecies ~= newStr stringSpecies]) ifTrue:[ |
|
3464 |
newStr := c stringSpecies fromString:newStr. |
|
3465 |
bitsPerCharacter := newStr bitsPerCharacter. |
|
3466 |
]. |
|
3467 |
newStr at:i put:c |
|
9192 | 3468 |
]. |
3469 |
^ newStr |
|
3470 |
||
3471 |
" |
|
3472 |
'HelloWorld' asLowercase |
|
18662 | 3473 |
'HelloWorld' asUnicode16String asLowercase |
9192 | 3474 |
'HelloWorld' asLowercaseFirst |
18657 | 3475 |
'HelloWorld' asUppercase |
9192 | 3476 |
" |
3477 |
! |
|
3478 |
||
3479 |
asLowercaseFirst |
|
17508 | 3480 |
"return a copy of myself where the first character is converted to lowercase. |
3481 |
If the first character is already lowercase, or there is no uppercase for it, return the |
|
3482 |
receiver." |
|
9192 | 3483 |
|
3484 |
|newString firstChar firstCharAsLowercase| |
|
3485 |
||
11525 | 3486 |
self isEmpty ifTrue:[^ self]. |
9192 | 3487 |
firstChar := (self at:1). |
3488 |
firstCharAsLowercase := firstChar asLowercase. |
|
3489 |
firstChar == firstCharAsLowercase ifTrue:[ ^ self]. |
|
3490 |
||
3491 |
firstCharAsLowercase bitsPerCharacter > self bitsPerCharacter ifTrue:[ |
|
20012 | 3492 |
newString := firstCharAsLowercase stringSpecies fromString:self. |
9192 | 3493 |
] ifFalse:[ |
20012 | 3494 |
newString := self stringSpecies fromString:self. |
9192 | 3495 |
]. |
9190 | 3496 |
newString at:1 put:firstCharAsLowercase. |
3497 |
^ newString |
|
608 | 3498 |
|
3499 |
" |
|
8874 | 3500 |
'HelloWorld' asLowercase |
3501 |
'HelloWorld' asLowercaseFirst |
|
608 | 3502 |
" |
3503 |
! |
|
3504 |
||
11828 | 3505 |
asLowercaseLast |
3506 |
"return a copy of myself where the last character is |
|
3507 |
converted to lowercase." |
|
3508 |
||
3509 |
|newString sz| |
|
3510 |
||
3511 |
sz := self size. |
|
3512 |
newString := self copyFrom:1 to:sz. |
|
3513 |
sz > 0 ifTrue:[ |
|
20012 | 3514 |
newString at:sz put:(newString at:sz) asLowercase |
11828 | 3515 |
]. |
3516 |
^ newString |
|
3517 |
||
3518 |
" |
|
12177 | 3519 |
'HelloWorld' asLowercase |
3520 |
'HelloWorlD' asLowercaseLast |
|
11828 | 3521 |
" |
3522 |
! |
|
3523 |
||
17644 | 3524 |
asMutator |
19473 | 3525 |
"return a corresponding setter method's selector. |
3526 |
I.e. #foo asMutator returns #foo:" |
|
17644 | 3527 |
|
3528 |
^ (self asOneByteString,':') asSymbol |
|
3529 |
! |
|
3530 |
||
9190 | 3531 |
asNumber |
3532 |
"read a number from the receiver. |
|
3533 |
Notice, that (in contrast to ST-80) errors may occur during the read, |
|
3534 |
so you better setup some signal handler when using this method. |
|
3535 |
Also notice, that this is meant to read end-user numbers from a string; |
|
9135 | 3536 |
it does not handle smalltalk numbers (i.e. radix). |
3537 |
To read a smalltalk number, use Number >> readSmalltalkFrom:. |
|
608 | 3538 |
This may change if ANSI specifies it." |
3539 |
||
3540 |
"/ ST-80 behavior: |
|
3541 |
"/ ^ Number readFromString:self onError:0 |
|
3542 |
||
17061 | 3543 |
^ Number fromString:self |
3544 |
||
3545 |
" |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
3546 |
'123' asNumber |
608 | 3547 |
'123.567' asNumber |
3548 |
'(5/6)' asNumber |
|
3549 |
'foo' asNumber |
|
17061 | 3550 |
'123a' asNumber |
8874 | 3551 |
Object errorSignal handle:[:ex | ex returnWith:0] do:['foo' asNumber] |
608 | 3552 |
" |
9192 | 3553 |
! |
3554 |
||
3555 |
asNumberFromFormatString:ignored |
|
608 | 3556 |
"read a number from the receiver, ignoring any nonDigit characters. |
3557 |
This is typically used to convert from strings which include |
|
3558 |
dollar-signs or millenium digits. However, this method also ignores |
|
3559 |
the decimal point (if any) and therefore should be used with care." |
|
3560 |
||
3561 |
|tempString| |
|
3562 |
||
3563 |
tempString := self collect:[:char | char isDigit]. |
|
3564 |
^ Number readFromString:tempString onError:0 |
|
3565 |
||
3566 |
" |
|
3567 |
'USD 123' asNumberFromFormatString:'foo' |
|
3568 |
'DM 123' asNumberFromFormatString:'foo' |
|
3569 |
'123' asNumberFromFormatString:'foo' |
|
3570 |
'123.567' asNumberFromFormatString:'foo' |
|
3571 |
'(5/6)' asNumberFromFormatString:'foo' |
|
3572 |
'foo' asNumberFromFormatString:'foo' |
|
3573 |
" |
|
9192 | 3574 |
! |
3575 |
||
7977 | 3576 |
asSingleByteStringIfPossible |
3577 |
"if possible, return the receiver converted to a 'normal' string. |
|
18181 | 3578 |
It is only possible, if there are no characters with codePoints above 255 in the receiver. |
3579 |
If not possible, the (wideString) receiver is returned." |
|
7977 | 3580 |
|
14555 | 3581 |
self isWideString ifFalse:[^ self]. |
18967 | 3582 |
self containsNon8BitElements ifTrue:[^ self]. |
3583 |
^ self asSingleByteString |
|
9192 | 3584 |
|
3585 |
" |
|
3586 |
'hello' asSingleByteStringIfPossible |
|
18967 | 3587 |
'hello' asText asSingleByteStringIfPossible |
3588 |
'hello' asUnicodeString asText asSingleByteStringIfPossible |
|
9192 | 3589 |
'hello' asUnicodeString asSingleByteStringIfPossible |
3590 |
" |
|
3591 |
! |
|
3592 |
||
3593 |
asSingleByteStringReplaceInvalidWith:replacementCharacter |
|
3594 |
"return the receiver converted to a 'normal' string, |
|
3595 |
with invalid characters replaced by replacementCharacter. |
|
3596 |
Can be used to convert from 16-bit strings to 8-bit strings |
|
3597 |
and replace characters above code-255 with some replacement." |
|
3598 |
||
3599 |
|newString| |
|
3600 |
||
3601 |
newString := String new:(self size). |
|
3602 |
1 to:self size do:[:idx | |
|
20012 | 3603 |
|char| |
3604 |
||
3605 |
char := self at:idx. |
|
3606 |
char codePoint <= 16rFF ifTrue:[ |
|
3607 |
newString at:idx put:char |
|
3608 |
] ifFalse:[ |
|
3609 |
newString at:idx put:replacementCharacter |
|
3610 |
]. |
|
9190 | 3611 |
]. |
3612 |
^ newString |
|
3613 |
||
3614 |
"Created: 30.6.1997 / 13:02:14 / cg" |
|
7977 | 3615 |
! |
3616 |
||
9192 | 3617 |
asString |
3618 |
"return myself - I am a string" |
|
3619 |
||
3620 |
^ self |
|
3621 |
! |
|
3622 |
||
3623 |
asStringCollection |
|
3624 |
"return a collection of lines from myself." |
|
3625 |
||
3626 |
^ StringCollection fromString:self "string" |
|
3627 |
||
3628 |
"Modified: 13.5.1996 / 20:36:59 / cg" |
|
3629 |
! |
|
1375
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
3630 |
|
1434 | 3631 |
asSymbol |
19039 | 3632 |
"Return a unique symbol with the name taken from the receiver's characters. |
8049
ccd0e8f26439
printing - migrating towards unicodeSupport in ST-code
Claus Gittinger <cg@exept.de>
parents:
8040
diff
changeset
|
3633 |
The receiver must be a singleByte-String. |
ccd0e8f26439
printing - migrating towards unicodeSupport in ST-code
Claus Gittinger <cg@exept.de>
parents:
8040
diff
changeset
|
3634 |
TwoByte- and FourByteSymbols are (currently ?) not allowed." |
ccd0e8f26439
printing - migrating towards unicodeSupport in ST-code
Claus Gittinger <cg@exept.de>
parents:
8040
diff
changeset
|
3635 |
|
ccd0e8f26439
printing - migrating towards unicodeSupport in ST-code
Claus Gittinger <cg@exept.de>
parents:
8040
diff
changeset
|
3636 |
|str| |
ccd0e8f26439
printing - migrating towards unicodeSupport in ST-code
Claus Gittinger <cg@exept.de>
parents:
8040
diff
changeset
|
3637 |
|
9192 | 3638 |
str := self string. |
3639 |
str ~~ self ifTrue:[ ^ str asSymbol ]. |
|
3640 |
^ self asSingleByteString asSymbol |
|
3641 |
! |
|
3642 |
||
3643 |
asSymbolIfInterned |
|
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
3644 |
"If a symbol with the receiver's characters is already known, return it. |
19692 | 3645 |
Otherwise, return nil. |
9230 | 3646 |
This can be used to query for an existing symbol and is the same as: |
20012 | 3647 |
self knownAsSymbol ifTrue:[self asSymbol] ifFalse:[nil] |
8049
ccd0e8f26439
printing - migrating towards unicodeSupport in ST-code
Claus Gittinger <cg@exept.de>
parents:
8040
diff
changeset
|
3648 |
but slightly faster, since the symbol lookup operation is only performed once. |
ccd0e8f26439
printing - migrating towards unicodeSupport in ST-code
Claus Gittinger <cg@exept.de>
parents:
8040
diff
changeset
|
3649 |
The receiver must be a singleByte-String. |
ccd0e8f26439
printing - migrating towards unicodeSupport in ST-code
Claus Gittinger <cg@exept.de>
parents:
8040
diff
changeset
|
3650 |
TwoByte- and FourByteSymbols are (currently ?) not allowed." |
1435
3ee1fde2aa58
allow asSymbolIfInterned for Texts
Claus Gittinger <cg@exept.de>
parents:
1434
diff
changeset
|
3651 |
|
18967 | 3652 |
|str| |
3653 |
||
3654 |
str := self string. |
|
3655 |
str == self ifTrue:[ |
|
20012 | 3656 |
"must be some kind of N-ByteString" |
3657 |
str := self asSingleByteStringIfPossible. |
|
3658 |
str == self ifTrue:[ |
|
3659 |
"single byte string conversion is not possible" |
|
3660 |
^ nil. |
|
3661 |
]. |
|
6642 | 3662 |
]. |
18967 | 3663 |
^ str asSymbolIfInterned |
3664 |
||
3665 |
" |
|
3666 |
(Unicode16String with:(Character value:16rFFFF)) asSymbolIfInterned |
|
3667 |
'new' asUnicodeString asSymbolIfInterned |
|
3668 |
'new' asText asSymbolIfInterned |
|
3669 |
'new' asUnicodeString asText asSymbolIfInterned |
|
3670 |
" |
|
1435
3ee1fde2aa58
allow asSymbolIfInterned for Texts
Claus Gittinger <cg@exept.de>
parents:
1434
diff
changeset
|
3671 |
|
3ee1fde2aa58
allow asSymbolIfInterned for Texts
Claus Gittinger <cg@exept.de>
parents:
1434
diff
changeset
|
3672 |
"Created: 22.5.1996 / 16:37:04 / cg" |
9192 | 3673 |
! |
3674 |
||
20160 | 3675 |
asSymbolIfInternedOrSelf |
3676 |
"If a symbol with the receiver's characters is already known, return it. |
|
3677 |
Otherwise, return self. |
|
3678 |
This can be used to query for an existing symbol and is the same as: |
|
3679 |
self knownAsSymbol ifTrue:[self asSymbol] ifFalse:[self] |
|
3680 |
but slightly faster, since the symbol lookup operation is only performed once. |
|
3681 |
The receiver must be a singleByte-String. |
|
3682 |
TwoByte- and FourByteSymbols are (currently ?) not allowed." |
|
3683 |
||
3684 |
|sym| |
|
3685 |
||
3686 |
sym := self asSymbolIfInterned. |
|
3687 |
^ sym isNil ifTrue:[self] ifFalse:[sym]. |
|
3688 |
||
3689 |
||
3690 |
" |
|
3691 |
(Unicode16String with:(Character value:16rFFFF)) asSymbolIfInternedOrSelf |
|
3692 |
'new' asUnicodeString asSymbolIfInternedOrSelf |
|
3693 |
'new' asText asSymbolIfInternedOrSelf |
|
3694 |
'new' asUnicodeString asText asSymbolIfInternedOrSelf |
|
3695 |
" |
|
3696 |
||
3697 |
"Created: 22.5.1996 / 16:37:04 / cg" |
|
3698 |
! |
|
3699 |
||
9192 | 3700 |
asText |
9403 | 3701 |
"return a Text-object (string with emphasis) from myself." |
9192 | 3702 |
|
3703 |
"this test allows for small non-gui apps to be built without libbasic2 (where Text is)" |
|
3704 |
Text isNil ifTrue:[^ self]. |
|
17065 | 3705 |
^ Text string:self |
9192 | 3706 |
|
3707 |
"Created: 12.5.1996 / 10:41:14 / cg" |
|
3708 |
! |
|
3709 |
||
17021 | 3710 |
asTimestamp |
3711 |
"convert the receiver into an Timestamp. |
|
3712 |
Notice, that errors may occur during the read, |
|
3713 |
so you better setup some exception handler when using this method." |
|
3714 |
||
3715 |
^ Timestamp readFromString:self |
|
3716 |
||
3717 |
" |
|
3718 |
'2014-11-10 21:30:22.444' asTimestamp |
|
3719 |
'2014-11-10 21:30:22.444Z' asTimestamp |
|
3720 |
'2014-11-10 21:30:22.444+0200' asTimestamp |
|
3721 |
" |
|
3722 |
! |
|
3723 |
||
9192 | 3724 |
asTitlecase |
8027 | 3725 |
"return a version of the receiver, where the first character is converted to titlecase, |
3726 |
and everything else to lowercase. |
|
3727 |
See the comment in Character on what titlecase is." |
|
3728 |
||
3729 |
|newStr c bitsPerCharacter |
|
3730 |
mySize "{ Class: SmallInteger }" | |
|
3731 |
||
3732 |
mySize := self size. |
|
3733 |
newStr := self species new:mySize. |
|
3734 |
bitsPerCharacter := newStr bitsPerCharacter. |
|
3735 |
||
3736 |
1 to:mySize do:[:i | |
|
20012 | 3737 |
i == 1 ifTrue:[ |
3738 |
c := (self at:i) asTitlecase. |
|
3739 |
] ifFalse:[ |
|
3740 |
c := (self at:i) asLowercase. |
|
3741 |
]. |
|
3742 |
c bitsPerCharacter > bitsPerCharacter ifTrue:[ |
|
3743 |
newStr := c stringSpecies fromString:newStr. |
|
3744 |
bitsPerCharacter := newStr bitsPerCharacter. |
|
3745 |
]. |
|
3746 |
newStr at:i put:c |
|
8027 | 3747 |
]. |
3748 |
^ newStr |
|
3749 |
||
3750 |
" |
|
8874 | 3751 |
'helloWorld' asTitlecase |
3752 |
'HelloWorld' asTitlecase |
|
3753 |
'HELLOWORLD' asTitlecase |
|
3754 |
'helloworld' asTitlecase |
|
8027 | 3755 |
" |
9192 | 3756 |
! |
3757 |
||
3758 |
asTitlecaseFirst |
|
8023 | 3759 |
"return a version of the receiver, where the first character is converted to titlecase. |
8024 | 3760 |
Titlecase is much like uppercase for most characters, with the exception of some combined |
3761 |
(2-character glyphs), which consist of an upper- and lower-case characters. |
|
3762 |
If the first character is already titlecase, or there is no titlecasepercase for it, return the |
|
8023 | 3763 |
receiver." |
3764 |
||
8026 | 3765 |
" |
8874 | 3766 |
For example, in Unicode, character U+01F3 is LATIN SMALL LETTER DZ. |
3767 |
(Let us write this compound character using ASCII as 'dz'.) |
|
3768 |
This character uppercases to character U+01F1, LATIN CAPITAL LETTER DZ. |
|
3769 |
(Which is basically 'DZ'.) |
|
3770 |
But it titlecases to to character U+01F2, LATIN CAPITAL LETTER D WITH SMALL LETTER Z. |
|
8026 | 3771 |
(Which we can write 'Dz'.) |
3772 |
||
3773 |
character uppercase titlecase |
|
3774 |
--------- --------- --------- |
|
3775 |
dz DZ Dz |
|
3776 |
" |
|
3777 |
||
8023 | 3778 |
|newString firstChar firstCharAsTitlecase| |
3779 |
||
3780 |
firstChar := (self at:1). |
|
8025 | 3781 |
firstCharAsTitlecase := firstChar asTitlecase. |
8023 | 3782 |
firstChar == firstCharAsTitlecase ifTrue:[ ^ self]. |
8037
8cce25b1059c
care for wide chars in asXXXFirst
Claus Gittinger <cg@exept.de>
parents:
8032
diff
changeset
|
3783 |
|
8cce25b1059c
care for wide chars in asXXXFirst
Claus Gittinger <cg@exept.de>
parents:
8032
diff
changeset
|
3784 |
firstCharAsTitlecase bitsPerCharacter > self bitsPerCharacter ifTrue:[ |
20012 | 3785 |
newString := firstCharAsTitlecase stringSpecies fromString:self. |
8037
8cce25b1059c
care for wide chars in asXXXFirst
Claus Gittinger <cg@exept.de>
parents:
8032
diff
changeset
|
3786 |
] ifFalse:[ |
20012 | 3787 |
newString := self stringSpecies fromString:self. |
8037
8cce25b1059c
care for wide chars in asXXXFirst
Claus Gittinger <cg@exept.de>
parents:
8032
diff
changeset
|
3788 |
]. |
8023 | 3789 |
newString at:1 put:firstCharAsTitlecase. |
3790 |
^ newString |
|
3791 |
||
3792 |
" |
|
8874 | 3793 |
'helloWorld' asTitlecaseFirst |
3794 |
'HelloWorld' asTitlecaseFirst |
|
8023 | 3795 |
" |
9192 | 3796 |
! |
3797 |
||
3798 |
asTwoByteString |
|
3799 |
"return the receiver converted to a two-byte string. |
|
16051 | 3800 |
Will be obsolete soon - use asUnicode16String." |
9192 | 3801 |
|
3802 |
^ TwoByteString fromString:self |
|
3803 |
! |
|
3804 |
||
3805 |
asURI |
|
3806 |
"return an URI with string taken from the receiver" |
|
3807 |
||
3808 |
^ URI fromString:self |
|
3809 |
! |
|
8710 | 3810 |
|
5776 | 3811 |
asURL |
3812 |
"return an URL-object from myself." |
|
3813 |
||
3814 |
^ URL fromString:self |
|
3815 |
||
3816 |
" |
|
8874 | 3817 |
'http://www.exept.de:80/index.html' asURL host |
3818 |
'http://www.exept.de:80/index.html' asURL port |
|
9192 | 3819 |
'http://www.exept.de:80/index.html' asURL method |
3820 |
'http://www.exept.de:80/index.html' asURL path |
|
3821 |
" |
|
3822 |
! |
|
3823 |
||
18901 | 3824 |
asUUID |
3825 |
"return self as a UUID" |
|
3826 |
||
3827 |
^ UUID fromString:self |
|
3828 |
||
3829 |
" |
|
3830 |
UUID genUUID asString asUUID |
|
3831 |
'{EAB22AC0-30C1-11CF-A7EB-0000C05BAE0B}' asUUID |
|
3832 |
'{EAB22AC0-30C1-11CF-A7EB-0000C05BAE0B}' asUnicodeString asUUID |
|
3833 |
'EAB22AC0-30C1-11CF-A7EB-0000C05BAE0B' asUUID |
|
3834 |
'EAB22AC0-30C1-11CF-A7EB-0000C05BAE0B quatsch' asUUID |
|
3835 |
'quark EAB22AC0-30C1-11CF-A7EB-0000C05BAE0B' asUUID |
|
3836 |
" |
|
3837 |
||
3838 |
"Modified: / 02-08-2007 / 16:43:29 / cg" |
|
3839 |
! |
|
3840 |
||
9192 | 3841 |
asUnicode16String |
15035 | 3842 |
"return the receiver in a two-byte per character representation. |
3843 |
Normally, the internal ST/X representation should be transparent and not |
|
3844 |
of the programmer's concern; except when strings are passed to the outside world, |
|
3845 |
such as wide-string ffi calls or file contents." |
|
3846 |
||
17600 | 3847 |
|sz| |
3848 |
||
3849 |
sz := self size. |
|
3850 |
||
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
3851 |
^ (Unicode16String new:sz) |
20012 | 3852 |
replaceFrom:1 to:sz with:self startingAt:1; |
3853 |
yourself. |
|
3854 |
||
3855 |
" |
|
3856 |
'abc' asUnicode16String |
|
17600 | 3857 |
" |
3858 |
! |
|
3859 |
||
3860 |
asUnicode16StringZ |
|
3861 |
"return the receiver in a two-byte per character representation. |
|
17625 | 3862 |
Make sure that is zero terminated (last char is 16r0000). |
17600 | 3863 |
Normally, the internal ST/X representation should be transparent and not |
3864 |
of the programmer's concern; except when strings are passed to the outside world, |
|
3865 |
such as wide-string ffi calls or file contents." |
|
3866 |
||
3867 |
|sz| |
|
3868 |
||
3869 |
sz := self size. |
|
3870 |
(self at:sz) == (Character codePoint:0) ifTrue:[ |
|
20012 | 3871 |
^ self asUnicode16String. |
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
3872 |
]. |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
3873 |
|
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
3874 |
^ (Unicode16String new:sz+1) |
20012 | 3875 |
replaceFrom:1 to:sz with:self startingAt:1; |
3876 |
at:sz+1 put:(Character codePoint:0); |
|
3877 |
yourself. |
|
17600 | 3878 |
|
3879 |
" |
|
20012 | 3880 |
'abc' asUnicode16StringZ |
3881 |
'abc' asUnicode16String asUnicode16StringZ |
|
17600 | 3882 |
" |
9192 | 3883 |
! |
8295
8433d819226b
Define #asUnicode16String and #asUnicode32String in CharacterArray
Stefan Vogel <sv@exept.de>
parents:
8222
diff
changeset
|
3884 |
|
8433d819226b
Define #asUnicode16String and #asUnicode32String in CharacterArray
Stefan Vogel <sv@exept.de>
parents:
8222
diff
changeset
|
3885 |
asUnicode32String |
16051 | 3886 |
"return the receiver in a four-byte per character representation. |
3887 |
Normally, the internal ST/X representation should be transparent and not |
|
3888 |
of the programmer's concern; except when strings are passed to the outside world, |
|
3889 |
such as wide-string ffi calls or file contents." |
|
3890 |
||
3891 |
^ self as:Unicode32String |
|
8295
8433d819226b
Define #asUnicode16String and #asUnicode32String in CharacterArray
Stefan Vogel <sv@exept.de>
parents:
8222
diff
changeset
|
3892 |
! |
8433d819226b
Define #asUnicode16String and #asUnicode32String in CharacterArray
Stefan Vogel <sv@exept.de>
parents:
8222
diff
changeset
|
3893 |
|
8433d819226b
Define #asUnicode16String and #asUnicode32String in CharacterArray
Stefan Vogel <sv@exept.de>
parents:
8222
diff
changeset
|
3894 |
asUnicodeString |
16051 | 3895 |
"return the receiver in a two-byte per character representation. |
3896 |
Normally, the internal ST/X representation should be transparent and not |
|
3897 |
of the programmer's concern; except when strings are passed to the outside world, |
|
3898 |
such as wide-string ffi calls or file contents." |
|
3899 |
||
3900 |
^ self asUnicode16String |
|
3901 |
" |
|
3902 |
'Hello World' asUnicodeString |
|
14141
e86d245213b0
comment/format in: #asUnicodeString
Stefan Vogel <sv@exept.de>
parents:
14137
diff
changeset
|
3903 |
" |
8295
8433d819226b
Define #asUnicode16String and #asUnicode32String in CharacterArray
Stefan Vogel <sv@exept.de>
parents:
8222
diff
changeset
|
3904 |
! |
8433d819226b
Define #asUnicode16String and #asUnicode32String in CharacterArray
Stefan Vogel <sv@exept.de>
parents:
8222
diff
changeset
|
3905 |
|
13073 | 3906 |
asUnixFilename |
3907 |
"return a UnixFilename with pathname taken from the receiver" |
|
3908 |
||
3909 |
^ UnixFilename named:self |
|
3910 |
||
3911 |
"Created: / 20-09-2010 / 21:42:55 / cg" |
|
3912 |
! |
|
3913 |
||
608 | 3914 |
asUppercase |
3915 |
"return a copy of myself in uppercase letters" |
|
3916 |
||
8003
6e690f4b79f1
asLowercase/asUppercase and friends care for 16-bit chars
Claus Gittinger <cg@exept.de>
parents:
7995
diff
changeset
|
3917 |
|newStr c bitsPerCharacter |
608 | 3918 |
mySize "{ Class: SmallInteger }" | |
3919 |
||
3920 |
mySize := self size. |
|
8954
12e701d7b5ac
allow for receiver to be empty in asLowercase and asUppercase.
Claus Gittinger <cg@exept.de>
parents:
8938
diff
changeset
|
3921 |
mySize == 0 ifTrue:[^ self]. |
608 | 3922 |
newStr := self species new:mySize. |
8003
6e690f4b79f1
asLowercase/asUppercase and friends care for 16-bit chars
Claus Gittinger <cg@exept.de>
parents:
7995
diff
changeset
|
3923 |
bitsPerCharacter := newStr bitsPerCharacter. |
6e690f4b79f1
asLowercase/asUppercase and friends care for 16-bit chars
Claus Gittinger <cg@exept.de>
parents:
7995
diff
changeset
|
3924 |
|
18657 | 3925 |
"/ handle the very seldom case of a lowercase char which needs |
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
3926 |
"/ more bits in its uppercase variant |
18657 | 3927 |
"/ (there are only a few of them) |
3928 |
||
608 | 3929 |
1 to:mySize do:[:i | |
20012 | 3930 |
c := (self at:i) asUppercase. |
3931 |
c bitsPerCharacter > bitsPerCharacter ifTrue:[ |
|
3932 |
newStr := c stringSpecies fromString:newStr. |
|
3933 |
bitsPerCharacter := newStr bitsPerCharacter. |
|
3934 |
]. |
|
3935 |
newStr at:i put:c |
|
608 | 3936 |
]. |
3937 |
^ newStr |
|
3938 |
||
3939 |
" |
|
8874 | 3940 |
'helloWorld' asUppercase |
3941 |
'helloWorld' asUppercaseFirst |
|
9192 | 3942 |
(Character value:16rB5) asString asUppercase -- needs 16 bits !! |
18657 | 3943 |
(Character value:16rFF) asString asUppercase -- needs 16 bits !! |
9192 | 3944 |
" |
3945 |
! |
|
3946 |
||
3947 |
asUppercaseFirst |
|
8003
6e690f4b79f1
asLowercase/asUppercase and friends care for 16-bit chars
Claus Gittinger <cg@exept.de>
parents:
7995
diff
changeset
|
3948 |
"return a version of the receiver, where the first character is converted to uppercase. |
6e690f4b79f1
asLowercase/asUppercase and friends care for 16-bit chars
Claus Gittinger <cg@exept.de>
parents:
7995
diff
changeset
|
3949 |
If the first character is already uppercase, or there is no uppercase for it, return the |
6e690f4b79f1
asLowercase/asUppercase and friends care for 16-bit chars
Claus Gittinger <cg@exept.de>
parents:
7995
diff
changeset
|
3950 |
receiver." |
6e690f4b79f1
asLowercase/asUppercase and friends care for 16-bit chars
Claus Gittinger <cg@exept.de>
parents:
7995
diff
changeset
|
3951 |
|
6e690f4b79f1
asLowercase/asUppercase and friends care for 16-bit chars
Claus Gittinger <cg@exept.de>
parents:
7995
diff
changeset
|
3952 |
|newString firstChar firstCharAsUppercase| |
6e690f4b79f1
asLowercase/asUppercase and friends care for 16-bit chars
Claus Gittinger <cg@exept.de>
parents:
7995
diff
changeset
|
3953 |
|
12679 | 3954 |
self isEmpty ifTrue:[^ self]. |
13882
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
3955 |
firstChar := self at:1. |
8003
6e690f4b79f1
asLowercase/asUppercase and friends care for 16-bit chars
Claus Gittinger <cg@exept.de>
parents:
7995
diff
changeset
|
3956 |
firstCharAsUppercase := firstChar asUppercase. |
6e690f4b79f1
asLowercase/asUppercase and friends care for 16-bit chars
Claus Gittinger <cg@exept.de>
parents:
7995
diff
changeset
|
3957 |
firstChar == firstCharAsUppercase ifTrue:[ ^ self]. |
6e690f4b79f1
asLowercase/asUppercase and friends care for 16-bit chars
Claus Gittinger <cg@exept.de>
parents:
7995
diff
changeset
|
3958 |
|
12679 | 3959 |
"/ very seldom, the uppercase-char needs more bits than the lowercase one (turkish y-diaresis) |
8037
8cce25b1059c
care for wide chars in asXXXFirst
Claus Gittinger <cg@exept.de>
parents:
8032
diff
changeset
|
3960 |
firstCharAsUppercase bitsPerCharacter > self bitsPerCharacter ifTrue:[ |
20012 | 3961 |
newString := firstCharAsUppercase stringSpecies fromString:self. |
8037
8cce25b1059c
care for wide chars in asXXXFirst
Claus Gittinger <cg@exept.de>
parents:
8032
diff
changeset
|
3962 |
] ifFalse:[ |
20012 | 3963 |
newString := self stringSpecies fromString:self. |
8037
8cce25b1059c
care for wide chars in asXXXFirst
Claus Gittinger <cg@exept.de>
parents:
8032
diff
changeset
|
3964 |
]. |
8003
6e690f4b79f1
asLowercase/asUppercase and friends care for 16-bit chars
Claus Gittinger <cg@exept.de>
parents:
7995
diff
changeset
|
3965 |
newString at:1 put:firstCharAsUppercase. |
608 | 3966 |
^ newString |
3967 |
||
3968 |
" |
|
8874 | 3969 |
'helloWorld' asUppercase |
3970 |
'helloWorld' asUppercaseFirst |
|
3971 |
'HelloWorld' asUppercaseFirst |
|
608 | 3972 |
" |
9192 | 3973 |
! |
3974 |
||
11828 | 3975 |
asUppercaseLast |
3976 |
"return a copy of myself where the last character is |
|
3977 |
converted to uppercase." |
|
3978 |
||
3979 |
|newString sz| |
|
3980 |
||
3981 |
sz := self size. |
|
3982 |
newString := self copyFrom:1 to:sz. |
|
3983 |
sz > 0 ifTrue:[ |
|
20012 | 3984 |
newString at:sz put:(newString at:sz) asUppercase |
11828 | 3985 |
]. |
3986 |
^ newString |
|
3987 |
||
3988 |
" |
|
12177 | 3989 |
'HelloWorld' asUppercase |
3990 |
'HelloWorld' asUppercaseLast |
|
11828 | 3991 |
" |
3992 |
! |
|
3993 |
||
17021 | 3994 |
asUtcTimestamp |
3995 |
"convert the receiver into an UtcTimestamp (Time is interpreted in the UTC timezone). |
|
3996 |
Notice, that errors may occur during the read, |
|
3997 |
so you better setup some exception handler when using this method." |
|
3998 |
||
3999 |
^ UtcTimestamp readFromString:self |
|
4000 |
||
4001 |
" |
|
4002 |
'2014-11-10 21:30:22.444' asUtcTimestamp |
|
4003 |
" |
|
4004 |
! |
|
4005 |
||
11062 | 4006 |
literalArrayEncoding |
4007 |
"encode myself as an array literal, from which a copy of the receiver |
|
4008 |
can be reconstructed with #decodeAsLiteralArray." |
|
4009 |
||
4010 |
^ self |
|
4011 |
||
4012 |
"Modified: 1.9.1995 / 02:25:45 / claus" |
|
4013 |
"Modified: 22.4.1996 / 13:00:50 / cg" |
|
4014 |
! |
|
4015 |
||
9192 | 4016 |
string |
4017 |
"return the receiver - for ST-80 compatibility" |
|
4018 |
||
4019 |
^ self |
|
4020 |
||
4021 |
"Modified: 27.4.1996 / 13:29:30 / cg" |
|
4022 |
! ! |
|
4023 |
||
4024 |
!CharacterArray methodsFor:'copying'! |
|
4025 |
||
4026 |
, aStringOrCharacter |
|
2720
98c21d9a07c0
allow mixed strings to be concatenated using comma
Claus Gittinger <cg@exept.de>
parents:
2692
diff
changeset
|
4027 |
"redefined to allow characters and mixed strings to be appended. |
1050
2f14bbd792a9
, now accepts a character-argument
Claus Gittinger <cg@exept.de>
parents:
1028
diff
changeset
|
4028 |
This is nonStandard, but convenient" |
2f14bbd792a9
, now accepts a character-argument
Claus Gittinger <cg@exept.de>
parents:
1028
diff
changeset
|
4029 |
|
2720
98c21d9a07c0
allow mixed strings to be concatenated using comma
Claus Gittinger <cg@exept.de>
parents:
2692
diff
changeset
|
4030 |
|myWidth otherWidth| |
98c21d9a07c0
allow mixed strings to be concatenated using comma
Claus Gittinger <cg@exept.de>
parents:
2692
diff
changeset
|
4031 |
|
1050
2f14bbd792a9
, now accepts a character-argument
Claus Gittinger <cg@exept.de>
parents:
1028
diff
changeset
|
4032 |
aStringOrCharacter isCharacter ifTrue:[ |
20012 | 4033 |
^ self copyWith:aStringOrCharacter |
1050
2f14bbd792a9
, now accepts a character-argument
Claus Gittinger <cg@exept.de>
parents:
1028
diff
changeset
|
4034 |
]. |
1396
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4035 |
aStringOrCharacter isText ifTrue:[ |
20012 | 4036 |
^ aStringOrCharacter concatenateFromString:self |
1396
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4037 |
]. |
2720
98c21d9a07c0
allow mixed strings to be concatenated using comma
Claus Gittinger <cg@exept.de>
parents:
2692
diff
changeset
|
4038 |
aStringOrCharacter isString ifTrue:[ |
20012 | 4039 |
(otherWidth := aStringOrCharacter bitsPerCharacter) ~~ (myWidth := self bitsPerCharacter) ifTrue:[ |
4040 |
otherWidth > myWidth ifTrue:[ |
|
4041 |
^ (aStringOrCharacter species fromString:self) , aStringOrCharacter |
|
4042 |
]. |
|
4043 |
^ self , (self species fromString:aStringOrCharacter) |
|
4044 |
]. |
|
2720
98c21d9a07c0
allow mixed strings to be concatenated using comma
Claus Gittinger <cg@exept.de>
parents:
2692
diff
changeset
|
4045 |
]. |
1050
2f14bbd792a9
, now accepts a character-argument
Claus Gittinger <cg@exept.de>
parents:
1028
diff
changeset
|
4046 |
^ super , aStringOrCharacter |
2f14bbd792a9
, now accepts a character-argument
Claus Gittinger <cg@exept.de>
parents:
1028
diff
changeset
|
4047 |
|
2f14bbd792a9
, now accepts a character-argument
Claus Gittinger <cg@exept.de>
parents:
1028
diff
changeset
|
4048 |
" |
8874 | 4049 |
'hello' , $1 |
4050 |
'hello' , '1' |
|
4051 |
'hello' , (' world' asText allBold) |
|
4052 |
'hello' , (JISEncodedString fromString:' world') |
|
2720
98c21d9a07c0
allow mixed strings to be concatenated using comma
Claus Gittinger <cg@exept.de>
parents:
2692
diff
changeset
|
4053 |
(JISEncodedString fromString:'hello') , ' world' |
1411 | 4054 |
|
1422 | 4055 |
Transcript showCR: |
20012 | 4056 |
(Text string:'hello' emphasis:#italic) , (Text string:' world' emphasis:#bold) |
1050
2f14bbd792a9
, now accepts a character-argument
Claus Gittinger <cg@exept.de>
parents:
1028
diff
changeset
|
4057 |
" |
2f14bbd792a9
, now accepts a character-argument
Claus Gittinger <cg@exept.de>
parents:
1028
diff
changeset
|
4058 |
|
2720
98c21d9a07c0
allow mixed strings to be concatenated using comma
Claus Gittinger <cg@exept.de>
parents:
2692
diff
changeset
|
4059 |
"Modified: 28.6.1997 / 00:13:17 / cg" |
9192 | 4060 |
! |
4061 |
||
4062 |
,, aString |
|
7020 | 4063 |
"concatenate with a newLine in between" |
6965 | 4064 |
|
4065 |
^ (self copyWith:Character cr) , aString |
|
4066 |
||
4067 |
" |
|
4068 |
hello ,, world |
|
7020 | 4069 |
'hello' ,, 'world' |
6965 | 4070 |
" |
4071 |
! |
|
4072 |
||
11329 | 4073 |
chopTo:maxLen |
19464 | 4074 |
"if the receiver's size is less or equal to maxLen, return it. |
11329 | 4075 |
Otherwise, return a copy of the receiver, where some characters |
4076 |
in the middle have been removed for a total string length |
|
4077 |
of maxLen." |
|
4078 |
||
4079 |
|sz n1 n2| |
|
4080 |
||
4081 |
(sz := self size) <= maxLen ifTrue:[ ^ self ]. |
|
4082 |
||
4083 |
n1 := n2 := maxLen // 2. |
|
4084 |
maxLen odd ifTrue:[ |
|
20012 | 4085 |
n2 := n1 + 1 |
11329 | 4086 |
]. |
4087 |
^ (self copyFrom:1 to:n1) , (self copyFrom:sz - n2 + 1) |
|
4088 |
||
4089 |
" |
|
4090 |
'12345678901234' chopTo:15 |
|
4091 |
'123456789012345' chopTo:15 |
|
4092 |
'1234567890123456' chopTo:15 |
|
4093 |
'aShortString' chopTo:15 |
|
4094 |
'aVeryLongNameForAStringThatShouldBeShortened' chopTo:15 |
|
4095 |
" |
|
4096 |
||
4097 |
"Modified: / 24-10-2006 / 12:32:01 / cg" |
|
4098 |
! |
|
4099 |
||
4100 |
contractAtBeginningTo:maxLen |
|
19464 | 4101 |
"if the receiver's size is less or equal to maxLen, return it. |
11329 | 4102 |
Otherwise, return a copy of the receiver, where some characters |
4103 |
at the beginning have been replaced by '...' for a total string length |
|
4104 |
of maxLen. Can be used to abbreviate long entries in tables." |
|
4105 |
||
4106 |
|sz| |
|
4107 |
||
4108 |
(sz := self size) <= maxLen ifTrue:[ ^ self ]. |
|
4109 |
||
4110 |
^ '...' , (self copyFrom:(sz - (maxLen - 4))) |
|
4111 |
||
4112 |
" |
|
4113 |
'12345678901234' contractAtBeginningTo:15 |
|
4114 |
'123456789012345' contractAtBeginningTo:15 |
|
4115 |
'1234567890123456' contractAtBeginningTo:15 |
|
4116 |
'aShortString' contractAtBeginningTo:15 |
|
4117 |
'aVeryLongNameForAStringThatShouldBeShortened' contractAtBeginningTo:15 |
|
4118 |
" |
|
4119 |
||
4120 |
"Modified: / 24-10-2006 / 12:32:13 / cg" |
|
4121 |
! |
|
4122 |
||
4123 |
contractAtEndTo:maxLen |
|
19464 | 4124 |
"if the receiver's size is less or equal to maxLen, return it. |
11329 | 4125 |
Otherwise, return a copy of the receiver, where some characters |
4126 |
at the end have been replaced by '...' for a total string length |
|
4127 |
of maxLen. Can be used to abbreviate long entries in tables." |
|
4128 |
||
4129 |
|sz| |
|
4130 |
||
4131 |
(sz := self size) <= maxLen ifTrue:[ ^ self ]. |
|
4132 |
||
4133 |
^ (self copyTo:maxLen-3),'...' |
|
4134 |
||
4135 |
" |
|
4136 |
'12345678901234' contractAtEndTo:15 |
|
4137 |
'123456789012345' contractAtEndTo:15 |
|
4138 |
'1234567890123456' contractAtEndTo:15 |
|
4139 |
'aShortString' contractAtEndTo:15 |
|
4140 |
'aVeryLongNameForAStringThatShouldBeShortened' contractAtEndTo:15 |
|
4141 |
" |
|
4142 |
||
4143 |
"Modified: / 24-10-2006 / 12:32:26 / cg" |
|
4144 |
! |
|
4145 |
||
4146 |
contractLeftTo:maxLen |
|
19464 | 4147 |
"if the receiver's size is less or equal to maxLen, return it. |
11329 | 4148 |
Otherwise, return a copy of the receiver, where some characters |
4149 |
near the first quarter have been replaced by '...' for a total string length |
|
12177 | 4150 |
of maxLen. |
4151 |
Very similar to contractTo:, but better to abbreviate long filename entries, |
|
11329 | 4152 |
where the right part is of more use than the left." |
4153 |
||
4154 |
|sz "{ SmallInteger }" |
|
4155 |
halfSize quarterSize "{ SmallInteger }" |
|
4156 |
leftEnd rightEnd rightStart| |
|
4157 |
||
4158 |
(sz := self size) <= maxLen ifTrue:[ ^ self ]. |
|
4159 |
||
4160 |
halfSize := maxLen // 2. |
|
4161 |
quarterSize := maxLen // 4. |
|
4162 |
leftEnd := quarterSize-1. |
|
4163 |
rightEnd := maxLen - leftEnd - 3. |
|
4164 |
rightStart := sz - rightEnd + 1. |
|
4165 |
^ (self copyTo:leftEnd),'...',(self copyFrom:rightStart) |
|
4166 |
||
4167 |
" |
|
12177 | 4168 |
'12345678901234' contractLeftTo:15 |
4169 |
'123456789012345' contractLeftTo:15 |
|
4170 |
'1234567890123456' contractLeftTo:15 |
|
4171 |
'aShortString' contractLeftTo:15 |
|
4172 |
'aVeryLongNameForAStringThatShouldBeShortened' contractLeftTo:15 |
|
11329 | 4173 |
'C:\Dokumente und Einstellungen\cg\work\bosch\dapas\hw_schnittstellen\DAPAS__HpibDLL.st' contractLeftTo:40 |
4174 |
" |
|
4175 |
! |
|
4176 |
||
4177 |
contractTo:maxLen |
|
19464 | 4178 |
"if the receiver's size is less or equal to maxLen, return it. |
11329 | 4179 |
Otherwise, return a copy of the receiver, where some characters |
4180 |
in the middle have been replaced by '...' for a total string length |
|
4181 |
of maxLen. Can be used to abbreviate long entries in tables." |
|
4182 |
||
13362 | 4183 |
|sz "{ SmallInteger }" leftSize rightSize| |
11329 | 4184 |
|
4185 |
(sz := self size) <= maxLen ifTrue:[ ^ self ]. |
|
4186 |
||
13362 | 4187 |
rightSize := maxLen // 2. |
4188 |
leftSize := maxLen - rightSize. |
|
13822 | 4189 |
leftSize := leftSize - 2. |
4190 |
rightSize := rightSize - 1. |
|
13362 | 4191 |
^ (self copyTo:leftSize),'...',(self copyFrom:(sz+1-rightSize)) |
4192 |
||
4193 |
" |
|
13882
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
4194 |
'12345678901234' contractTo:15 |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
4195 |
'123456789012345' contractTo:15 |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
4196 |
'1234567890123456' contractTo:15 |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
4197 |
'12345678901234567' contractTo:15 |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
4198 |
'123456789012345678' contractTo:15 |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
4199 |
'aShortString' contractTo:15 |
11329 | 4200 |
'aVeryLongNameForAStringThatShouldBeShortened' contractTo:15 |
4201 |
'C:\Dokumente und Einstellungen\cg\work\bosch\dapas\hw_schnittstellen\DAPAS__HpibDLL.st' contractTo:40 |
|
13882
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
4202 |
('1234567890123456789012345678901234567' contractTo:30) size |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
4203 |
('1234567890123456789012345678901234567' contractTo:29) size |
13362 | 4204 |
" |
4205 |
||
13822 | 4206 |
"Modified (comment): / 24-11-2011 / 19:17:46 / cg" |
11329 | 4207 |
! |
4208 |
||
16202 | 4209 |
copyReplaceAll:oldElement with:newElement |
4210 |
"return a copy of the receiver as a string, where all elements equal to oldElement |
|
4211 |
have been replaced by newElement." |
|
4212 |
||
4213 |
"/ ANSI seems to allow a sequence to be replaced by another sequence, |
|
4214 |
"/ whereas the old ST80 meant replace all occurrences... - sigh. |
|
4215 |
oldElement isByteCollection ifTrue:[ |
|
20012 | 4216 |
newElement isByteCollection ifTrue:[ |
4217 |
^ self copyReplaceString:oldElement withString:newElement. |
|
4218 |
]. |
|
4219 |
self halt:'check if this is legal'. |
|
16202 | 4220 |
]. |
4221 |
newElement isByteCollection ifTrue:[ |
|
20012 | 4222 |
self halt:'check if this is legal'. |
16202 | 4223 |
]. |
16212 | 4224 |
^ super copyReplaceAll:oldElement with:newElement |
16202 | 4225 |
! |
4226 |
||
8347 | 4227 |
copyReplaceString:subString withString:newString |
4228 |
"return a copy of the receiver, with all sequences of subString replaced |
|
4229 |
by newString (i.e. slice in the newString in place of the oldString)." |
|
4230 |
||
4231 |
|tmpStream idx idx1| |
|
4232 |
||
15523 | 4233 |
tmpStream := self species writeStream. |
8347 | 4234 |
idx := 1. |
4235 |
[idx ~~ 0] whileTrue:[ |
|
20012 | 4236 |
idx1 := idx. |
4237 |
idx := self indexOfSubCollection:subString startingAt:idx. |
|
4238 |
idx ~~ 0 ifTrue:[ |
|
4239 |
tmpStream nextPutAll:(self copyFrom:idx1 to:idx-1). |
|
4240 |
tmpStream nextPutAll:newString. |
|
4241 |
idx := idx + subString size |
|
4242 |
] |
|
8347 | 4243 |
]. |
4244 |
tmpStream nextPutAll:(self copyFrom:idx1). |
|
4245 |
^ tmpStream contents |
|
4246 |
||
4247 |
" |
|
9306 | 4248 |
'12345678901234567890' copyReplaceString:'123' withString:'OneTwoThree' |
4249 |
'12345678901234567890' copyReplaceString:'123' withString:'*' |
|
4250 |
'12345678901234567890' copyReplaceString:'234' withString:'foo' |
|
4251 |
||
4252 |
('a string with spaces' copyReplaceAll:$ withAll:' foo ') |
|
20012 | 4253 |
copyReplaceString:'foo' withString:'bar' |
8347 | 4254 |
" |
4255 |
||
4256 |
"Modified: / 31-05-1999 / 12:33:59 / cg" |
|
4257 |
"Created: / 12-05-2004 / 12:00:00 / cg" |
|
4258 |
! |
|
4259 |
||
8040
f10cca439066
copyWith for mixed-width string/character
Claus Gittinger <cg@exept.de>
parents:
8037
diff
changeset
|
4260 |
copyWith:aCharacter |
19464 | 4261 |
"return a new string containing the receiver's characters |
8874 | 4262 |
and the single new character, aCharacter. |
8040
f10cca439066
copyWith for mixed-width string/character
Claus Gittinger <cg@exept.de>
parents:
8037
diff
changeset
|
4263 |
This is different from concatentation, which expects another string |
f10cca439066
copyWith for mixed-width string/character
Claus Gittinger <cg@exept.de>
parents:
8037
diff
changeset
|
4264 |
as argument, but equivalent to copy-and-addLast. |
8874 | 4265 |
The code below cares for different width characters |
8040
f10cca439066
copyWith for mixed-width string/character
Claus Gittinger <cg@exept.de>
parents:
8037
diff
changeset
|
4266 |
(i.e. when appending a 16bit char to an 8bit string)" |
f10cca439066
copyWith for mixed-width string/character
Claus Gittinger <cg@exept.de>
parents:
8037
diff
changeset
|
4267 |
|
f10cca439066
copyWith for mixed-width string/character
Claus Gittinger <cg@exept.de>
parents:
8037
diff
changeset
|
4268 |
|sz newString| |
f10cca439066
copyWith for mixed-width string/character
Claus Gittinger <cg@exept.de>
parents:
8037
diff
changeset
|
4269 |
|
f10cca439066
copyWith for mixed-width string/character
Claus Gittinger <cg@exept.de>
parents:
8037
diff
changeset
|
4270 |
aCharacter bitsPerCharacter > self bitsPerCharacter ifTrue:[ |
20012 | 4271 |
sz := self size. |
4272 |
newString := aCharacter stringSpecies new:sz + 1. |
|
4273 |
newString replaceFrom:1 to:sz with:self startingAt:1. |
|
4274 |
newString at:sz+1 put:aCharacter. |
|
4275 |
^ newString. |
|
8040
f10cca439066
copyWith for mixed-width string/character
Claus Gittinger <cg@exept.de>
parents:
8037
diff
changeset
|
4276 |
]. |
f10cca439066
copyWith for mixed-width string/character
Claus Gittinger <cg@exept.de>
parents:
8037
diff
changeset
|
4277 |
^ super copyWith:aCharacter |
17241 | 4278 |
! |
4279 |
||
4280 |
restAfter:keyword withoutSeparators:strip |
|
4281 |
"compare the left of the receiver with keyword, |
|
4282 |
if it matches return the right. |
|
4283 |
Finally, if strip is true, remove whiteSpace. |
|
4284 |
This method is used to match and extract lines of the form: |
|
20012 | 4285 |
something: rest |
17241 | 4286 |
where we are interested in rest, but only if the receiver string |
4287 |
begins with something. |
|
4288 |
||
4289 |
You may wonder why such a specialized method exists here |
|
4290 |
- this is so common when processing mailboxes, |
|
4291 |
rcs files, nntp/pop3 responses, that is was considered worth |
|
4292 |
a special method here to avoid having the code below a hundred |
|
4293 |
times in variuos places." |
|
4294 |
||
4295 |
|rest| |
|
4296 |
||
4297 |
(self startsWith:keyword) ifTrue:[ |
|
20012 | 4298 |
rest := self copyFrom:(keyword size + 1). |
4299 |
strip ifTrue:[ |
|
4300 |
rest := rest withoutSeparators |
|
4301 |
]. |
|
4302 |
^ rest |
|
17241 | 4303 |
]. |
4304 |
^ nil |
|
4305 |
||
4306 |
" |
|
4307 |
'foo: hello world' restAfter:'foo:' withoutSeparators:true |
|
4308 |
'funny: something' restAfter:'foo:' withoutSeparators:true |
|
4309 |
||
4310 |
'foo: hello world ' restAfter:'foo:' withoutSeparators:true |
|
4311 |
'foo: hello world ' restAfter:'foo:' withoutSeparators:false |
|
4312 |
" |
|
4313 |
||
4314 |
"Created: 25.11.1995 / 11:04:18 / cg" |
|
4315 |
! |
|
4316 |
||
4317 |
splitAtString:subString withoutSeparators:strip |
|
4318 |
"If the receiver is of the form: |
|
20012 | 4319 |
<left><subString><right> |
17241 | 4320 |
return a collection containing left and right only. |
4321 |
If strip is true, remove whiteSpace in the returned substrings." |
|
4322 |
||
4323 |
|idx left right| |
|
4324 |
||
4325 |
(idx := self indexOfSubCollection:subString) ~~ 0 ifTrue:[ |
|
20012 | 4326 |
left := self copyTo:(idx - 1). |
4327 |
right := self copyFrom:(idx + subString size). |
|
4328 |
strip ifTrue:[ |
|
4329 |
left := left withoutSeparators. |
|
4330 |
right := right withoutSeparators. |
|
4331 |
]. |
|
4332 |
^ StringCollection with:left with:right |
|
17241 | 4333 |
]. |
4334 |
self error:'substring not present in receiver' mayProceed:true. |
|
4335 |
^ self |
|
4336 |
||
4337 |
" |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
4338 |
'hello -> world' splitAtString:'->' withoutSeparators:false |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
4339 |
'hello -> world' splitAtString:'->' withoutSeparators:true |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
4340 |
'hello -> ' splitAtString:'->' withoutSeparators:true |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
4341 |
'hello > error' splitAtString:'->' withoutSeparators:true |
17241 | 4342 |
" |
4343 |
||
4344 |
"Created: 25.11.1995 / 11:04:18 / cg" |
|
608 | 4345 |
! ! |
4346 |
||
4347 |
!CharacterArray methodsFor:'displaying'! |
|
4348 |
||
2762 | 4349 |
displayOn:aGC x:x y:y from:start to:stop |
4350 |
"display the receiver on a GC" |
|
4351 |
||
12171 | 4352 |
self displayOn:aGC x:x y:y from:start to:stop opaque:false |
4353 |
||
4354 |
"Modified: 12.5.1996 / 12:49:33 / cg" |
|
4355 |
! |
|
4356 |
||
4357 |
displayOn:aGC x:x y:y from:start to:stop opaque:opaque |
|
4358 |
"display the receiver on a GC" |
|
4359 |
||
2762 | 4360 |
"q&d hack" |
4361 |
||
12177 | 4362 |
(start == 1 and:[stop == self size]) ifTrue:[ |
20012 | 4363 |
self displayOn:aGC x:x y:y opaque:opaque. |
12177 | 4364 |
] ifFalse:[ |
20012 | 4365 |
(self copyFrom:start to:stop) displayOn:aGC x:x y:y opaque:opaque. |
12177 | 4366 |
]. |
2762 | 4367 |
! |
4368 |
||
7082 | 4369 |
displayOn:aGc x:x y:y opaque:opaque |
2762 | 4370 |
"display the receiver in a graphicsContext - this method allows |
4371 |
strings to be used like DisplayObjects." |
|
4372 |
||
4373 |
|s| |
|
4374 |
||
4375 |
s := self string. |
|
7082 | 4376 |
opaque ifTrue:[ |
20012 | 4377 |
aGc displayOpaqueString:s x:x y:y. |
2762 | 4378 |
] ifFalse:[ |
20012 | 4379 |
aGc displayString:s x:x y:y. |
2762 | 4380 |
]. |
1381 | 4381 |
|
4382 |
"Modified: 11.5.1996 / 14:42:48 / cg" |
|
4383 |
! |
|
4384 |
||
2762 | 4385 |
displayOpaqueOn:aGC x:x y:y from:start to:stop |
4386 |
"display the receiver on a GC" |
|
4387 |
||
12171 | 4388 |
self displayOn:aGC x:x y:y from:start to:stop opaque:true |
608 | 4389 |
! ! |
4390 |
||
1375
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
4391 |
!CharacterArray methodsFor:'emphasis'! |
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
4392 |
|
11632
b82e9f3cabd1
new: #actionForAll: #emphasisAtPoint:on:
Stefan Vogel <sv@exept.de>
parents:
11630
diff
changeset
|
4393 |
actionForAll:aBlock |
12326
c67f12213ddb
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
12319
diff
changeset
|
4394 |
"change the action block of all characters. |
c67f12213ddb
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
12319
diff
changeset
|
4395 |
Some widgets use this like a href if clicked onto the text." |
11632
b82e9f3cabd1
new: #actionForAll: #emphasisAtPoint:on:
Stefan Vogel <sv@exept.de>
parents:
11630
diff
changeset
|
4396 |
|
b82e9f3cabd1
new: #actionForAll: #emphasisAtPoint:on:
Stefan Vogel <sv@exept.de>
parents:
11630
diff
changeset
|
4397 |
^ self asText actionForAll:aBlock |
b82e9f3cabd1
new: #actionForAll: #emphasisAtPoint:on:
Stefan Vogel <sv@exept.de>
parents:
11630
diff
changeset
|
4398 |
! |
b82e9f3cabd1
new: #actionForAll: #emphasisAtPoint:on:
Stefan Vogel <sv@exept.de>
parents:
11630
diff
changeset
|
4399 |
|
5732 | 4400 |
allBold |
12326
c67f12213ddb
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
12319
diff
changeset
|
4401 |
"return a text object representing the receiver, but all boldified" |
5732 | 4402 |
|
8874 | 4403 |
"this test allows for small non-gui apps to be built without libbasic2 (where Text is)" |
6187 | 4404 |
Text isNil ifTrue:[^ self]. |
5732 | 4405 |
^ self asText allBold |
4406 |
||
4407 |
" |
|
4408 |
Transcript showCR:'hello' asText allBold |
|
4409 |
Transcript showCR:'hello' allBold |
|
4410 |
" |
|
4411 |
! |
|
4412 |
||
5898
7bc4f1593b4e
allItalic now understood by CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5848
diff
changeset
|
4413 |
allItalic |
12326
c67f12213ddb
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
12319
diff
changeset
|
4414 |
"return a text object representing the receiver, but all in italic" |
5898
7bc4f1593b4e
allItalic now understood by CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5848
diff
changeset
|
4415 |
|
8874 | 4416 |
"this test allows for small non-gui apps to be built without libbasic2 (where Text is)" |
6187 | 4417 |
Text isNil ifTrue:[^ self]. |
5898
7bc4f1593b4e
allItalic now understood by CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5848
diff
changeset
|
4418 |
^ self asText allItalic |
7bc4f1593b4e
allItalic now understood by CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5848
diff
changeset
|
4419 |
|
7bc4f1593b4e
allItalic now understood by CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5848
diff
changeset
|
4420 |
" |
7bc4f1593b4e
allItalic now understood by CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5848
diff
changeset
|
4421 |
Transcript showCR:'hello' asText allItalic |
7bc4f1593b4e
allItalic now understood by CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5848
diff
changeset
|
4422 |
Transcript showCR:'hello' allItalic |
7bc4f1593b4e
allItalic now understood by CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5848
diff
changeset
|
4423 |
" |
7bc4f1593b4e
allItalic now understood by CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5848
diff
changeset
|
4424 |
! |
7bc4f1593b4e
allItalic now understood by CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5848
diff
changeset
|
4425 |
|
11345 | 4426 |
allStrikedOut |
12326
c67f12213ddb
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
12319
diff
changeset
|
4427 |
"return a text object representing the receiver, but all in strikeout" |
11345 | 4428 |
|
4429 |
"this test allows for small non-gui apps to be built without libbasic2 (where Text is)" |
|
4430 |
Text isNil ifTrue:[^ self]. |
|
4431 |
^ self asText allStrikedOut |
|
4432 |
||
4433 |
" |
|
4434 |
Transcript showCR:'hello' asText allStrikedOut |
|
4435 |
Transcript showCR:'hello' allStrikedOut |
|
4436 |
" |
|
4437 |
! |
|
4438 |
||
10862 | 4439 |
allUnderlined |
12326
c67f12213ddb
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
12319
diff
changeset
|
4440 |
"return a text object representing the receiver, but all with underline" |
10862 | 4441 |
|
4442 |
"this test allows for small non-gui apps to be built without libbasic2 (where Text is)" |
|
4443 |
Text isNil ifTrue:[^ self]. |
|
4444 |
^ self asText allUnderlined |
|
4445 |
||
4446 |
" |
|
4447 |
Transcript showCR:'hello' asText allUnderlined |
|
4448 |
Transcript showCR:'hello' allUnderlined |
|
4449 |
" |
|
4450 |
! |
|
4451 |
||
19774 | 4452 |
asActionLinkTo:aBlock |
4453 |
"change the action block of all characters and colorize as a link. |
|
4454 |
Some widgets use this like a href if clicked onto the text |
|
4455 |
(for example, the system-browser's info at the bottom is such a widget)." |
|
4456 |
||
4457 |
"/ the blue may become a user-setting some time later.. |
|
4458 |
^ (self actionForAll:aBlock) withColor:(Color blue) |
|
4459 |
! |
|
4460 |
||
6228 | 4461 |
colorizeAllWith:aColor |
12326
c67f12213ddb
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
12319
diff
changeset
|
4462 |
"return a text object representing the receiver, but all colorized" |
c67f12213ddb
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
12319
diff
changeset
|
4463 |
|
c67f12213ddb
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
12319
diff
changeset
|
4464 |
"this test allows for small non-gui apps to be built without libbasic2 (where Text is)" |
c67f12213ddb
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
12319
diff
changeset
|
4465 |
Text isNil ifTrue:[^ self]. |
6228 | 4466 |
^ self asText colorizeAllWith:aColor |
4467 |
||
4468 |
" |
|
4469 |
Transcript showCR:('hello' colorizeAllWith:Color red) |
|
4470 |
Transcript showCR:('world' colorizeAllWith:Color green darkened) |
|
4471 |
" |
|
4472 |
! |
|
4473 |
||
12330
f9b57b0b8a8b
added: #colorizeAllWith:on:
Claus Gittinger <cg@exept.de>
parents:
12326
diff
changeset
|
4474 |
colorizeAllWith:fgColor on:bgColor |
f9b57b0b8a8b
added: #colorizeAllWith:on:
Claus Gittinger <cg@exept.de>
parents:
12326
diff
changeset
|
4475 |
"return a text object representing the receiver, but all colorized with |
f9b57b0b8a8b
added: #colorizeAllWith:on:
Claus Gittinger <cg@exept.de>
parents:
12326
diff
changeset
|
4476 |
both fg and background color" |
f9b57b0b8a8b
added: #colorizeAllWith:on:
Claus Gittinger <cg@exept.de>
parents:
12326
diff
changeset
|
4477 |
|
f9b57b0b8a8b
added: #colorizeAllWith:on:
Claus Gittinger <cg@exept.de>
parents:
12326
diff
changeset
|
4478 |
"this test allows for small non-gui apps to be built without libbasic2 (where Text is)" |
f9b57b0b8a8b
added: #colorizeAllWith:on:
Claus Gittinger <cg@exept.de>
parents:
12326
diff
changeset
|
4479 |
Text isNil ifTrue:[^ self]. |
f9b57b0b8a8b
added: #colorizeAllWith:on:
Claus Gittinger <cg@exept.de>
parents:
12326
diff
changeset
|
4480 |
^ self asText colorizeAllWith:fgColor on:bgColor |
f9b57b0b8a8b
added: #colorizeAllWith:on:
Claus Gittinger <cg@exept.de>
parents:
12326
diff
changeset
|
4481 |
|
f9b57b0b8a8b
added: #colorizeAllWith:on:
Claus Gittinger <cg@exept.de>
parents:
12326
diff
changeset
|
4482 |
" |
f9b57b0b8a8b
added: #colorizeAllWith:on:
Claus Gittinger <cg@exept.de>
parents:
12326
diff
changeset
|
4483 |
Transcript showCR:('hello' colorizeAllWith:Color red on:Color yellow) |
f9b57b0b8a8b
added: #colorizeAllWith:on:
Claus Gittinger <cg@exept.de>
parents:
12326
diff
changeset
|
4484 |
Transcript showCR:('world' colorizeAllWith:Color red) |
f9b57b0b8a8b
added: #colorizeAllWith:on:
Claus Gittinger <cg@exept.de>
parents:
12326
diff
changeset
|
4485 |
" |
f9b57b0b8a8b
added: #colorizeAllWith:on:
Claus Gittinger <cg@exept.de>
parents:
12326
diff
changeset
|
4486 |
! |
f9b57b0b8a8b
added: #colorizeAllWith:on:
Claus Gittinger <cg@exept.de>
parents:
12326
diff
changeset
|
4487 |
|
1396
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4488 |
emphasis |
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4489 |
"return the emphasis. |
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4490 |
Since characterArrays do not hold any emphasis information, |
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4491 |
nil (no emphasis) is returned here." |
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4492 |
|
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4493 |
^ RunArray new:self size withAll:nil |
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4494 |
|
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4495 |
"Created: 14.5.1996 / 13:58:58 / cg" |
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4496 |
! |
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4497 |
|
6655 | 4498 |
emphasis:emphasisCollection |
4499 |
^ self asText emphasis:emphasisCollection |
|
4500 |
||
4501 |
" |
|
4502 |
Transcript showCR:('hello' emphasis:#(bold bold bold bold bold)) |
|
4503 |
" |
|
4504 |
! |
|
4505 |
||
1375
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
4506 |
emphasisAt:characterIndex |
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
4507 |
"return the emphasis at some index. |
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
4508 |
Since characterArrays do not hold any emphasis information, |
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
4509 |
nil (no emphasis) is returned here." |
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
4510 |
|
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
4511 |
^ nil |
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
4512 |
|
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
4513 |
"Created: 11.5.1996 / 14:13:27 / cg" |
1396
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4514 |
! |
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4515 |
|
11632
b82e9f3cabd1
new: #actionForAll: #emphasisAtPoint:on:
Stefan Vogel <sv@exept.de>
parents:
11630
diff
changeset
|
4516 |
emphasisAtPoint:aPoint on:aGC |
b82e9f3cabd1
new: #actionForAll: #emphasisAtPoint:on:
Stefan Vogel <sv@exept.de>
parents:
11630
diff
changeset
|
4517 |
"return the emphasis at a given point, or nil if there is none" |
b82e9f3cabd1
new: #actionForAll: #emphasisAtPoint:on:
Stefan Vogel <sv@exept.de>
parents:
11630
diff
changeset
|
4518 |
|
b82e9f3cabd1
new: #actionForAll: #emphasisAtPoint:on:
Stefan Vogel <sv@exept.de>
parents:
11630
diff
changeset
|
4519 |
^ nil |
b82e9f3cabd1
new: #actionForAll: #emphasisAtPoint:on:
Stefan Vogel <sv@exept.de>
parents:
11630
diff
changeset
|
4520 |
! |
b82e9f3cabd1
new: #actionForAll: #emphasisAtPoint:on:
Stefan Vogel <sv@exept.de>
parents:
11630
diff
changeset
|
4521 |
|
1396
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4522 |
emphasisCollection |
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4523 |
"return the emphasis. |
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4524 |
Since characterArrays do not hold any emphasis information, |
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4525 |
nil (no emphasis) is returned here." |
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4526 |
|
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4527 |
^ RunArray new:(self size) |
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4528 |
|
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4529 |
"Created: 14.5.1996 / 13:58:58 / cg" |
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4530 |
"Modified: 14.5.1996 / 15:02:29 / cg" |
3404 | 4531 |
! |
4532 |
||
11919 | 4533 |
emphasiseFrom:start to:stop with:newEmphasis |
4534 |
"set to the emphasis within some range. return the receiver" |
|
4535 |
||
4536 |
^ self asText emphasiseFrom:start to:stop with:newEmphasis |
|
4537 |
||
4538 |
" |
|
12177 | 4539 |
'hello' emphasiseFrom:2 with:#italic |
11919 | 4540 |
|
4541 |
Transcript showCR:('hello' emphasiseFrom:2 with:#italic) |
|
4542 |
" |
|
4543 |
! |
|
4544 |
||
4545 |
emphasiseFrom:start with:newEmphasis |
|
4546 |
"set to the emphasis within some range. return the receiver" |
|
4547 |
||
4548 |
^ self emphasiseFrom:start to:(self size) with:newEmphasis |
|
4549 |
||
4550 |
" |
|
12177 | 4551 |
'hello' emphasiseFrom:2 with:#italic |
11919 | 4552 |
|
4553 |
Transcript showCR:('hello' emphasiseFrom:2 with:#italic) |
|
4554 |
" |
|
4555 |
! |
|
4556 |
||
3404 | 4557 |
emphasizeAllWith:emphasis |
12326
c67f12213ddb
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
12319
diff
changeset
|
4558 |
"return a text object representing the receiver, but all emphasized" |
c67f12213ddb
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
12319
diff
changeset
|
4559 |
|
c67f12213ddb
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
12319
diff
changeset
|
4560 |
"this test allows for small non-gui apps to be built without libbasic2 (where Text is)" |
c67f12213ddb
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
12319
diff
changeset
|
4561 |
Text isNil ifTrue:[^ self]. |
3404 | 4562 |
^ self asText emphasizeAllWith:emphasis |
4563 |
||
4564 |
" |
|
4565 |
Transcript showCR:('hello' emphasizeAllWith:#bold) |
|
3599 | 4566 |
Transcript showCR:('hello' emphasizeAllWith:(#color -> Color red)) |
4567 |
Transcript showCR:('hello' emphasizeAllWith:(#color -> Color red)) |
|
4568 |
" |
|
4569 |
||
4570 |
"Modified: / 17.6.1998 / 12:51:44 / cg" |
|
5097 | 4571 |
! |
4572 |
||
4573 |
makeSelectorBoldIn:aClass |
|
14050 | 4574 |
"the receiver represents some source code for a method in aClass. |
5097 | 4575 |
Change myself to boldify the selector. |
14050 | 4576 |
Not yet implemented (could easily use the syntaxHighlighter for this ...). |
4577 |
For protocol compatibility with other smalltalks" |
|
5097 | 4578 |
|
4579 |
^ self |
|
4580 |
||
14050 | 4581 |
"Created: / 13-12-1999 / 21:49:24 / cg" |
4582 |
! |
|
4583 |
||
19762 | 4584 |
withColor:aColorOrColorSymbol |
16697 | 4585 |
"return a text object representing the receiver, but all colorized" |
4586 |
||
19762 | 4587 |
aColorOrColorSymbol isSymbol ifTrue:[ |
20012 | 4588 |
^ self colorizeAllWith:(Color perform:aColorOrColorSymbol) |
19762 | 4589 |
]. |
4590 |
^ self colorizeAllWith:aColorOrColorSymbol |
|
4591 |
||
4592 |
" |
|
4593 |
Transcript showCR:('hello' withColor:#red) |
|
16697 | 4594 |
Transcript showCR:('hello' withColor:Color red) |
4595 |
Transcript showCR:('world' withColor:Color green darkened) |
|
4596 |
" |
|
4597 |
! |
|
4598 |
||
14050 | 4599 |
withoutAnyColorEmphasis |
4600 |
"for protocol compatibility with Text" |
|
4601 |
||
4602 |
^ self |
|
4603 |
||
4604 |
"Created: / 06-03-2012 / 18:15:38 / cg" |
|
6653 | 4605 |
! |
4606 |
||
12396 | 4607 |
withoutBackgroundColorEmphasis |
14050 | 4608 |
"for protocol compatibility with Text" |
4609 |
||
12396 | 4610 |
^ self |
14050 | 4611 |
|
4612 |
"Modified (comment): / 06-03-2012 / 18:14:27 / cg" |
|
12396 | 4613 |
! |
4614 |
||
6653 | 4615 |
withoutEmphasis:emphasisToRemove |
14050 | 4616 |
"for protocol compatibility with Text" |
4617 |
||
6653 | 4618 |
^ self |
14050 | 4619 |
|
4620 |
"Modified (comment): / 06-03-2012 / 18:14:29 / cg" |
|
12396 | 4621 |
! |
4622 |
||
4623 |
withoutForegroundColorEmphasis |
|
14050 | 4624 |
"for protocol compatibility with Text" |
4625 |
||
12396 | 4626 |
^ self |
14050 | 4627 |
|
4628 |
"Modified (comment): / 06-03-2012 / 18:14:31 / cg" |
|
1375
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
4629 |
! ! |
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
4630 |
|
7265 | 4631 |
!CharacterArray methodsFor:'encoding & decoding'! |
991
f0b45a4b82d2
first attempt in encoding/decoding for different character representations (mac/msdos etc.)
Claus Gittinger <cg@exept.de>
parents:
986
diff
changeset
|
4632 |
|
20013 | 4633 |
asDenormalizedUnicodeString |
4634 |
"return a new string containing the same characters, as a denormalized Unicode string. |
|
4635 |
This replaces diacritical chars (umlauts, accented chars etc) by |
|
4636 |
a sequence with combination characters. |
|
4637 |
(i.e. a plain character followed by a combining diacritical in the 0x03xx range)" |
|
4638 |
||
20014 | 4639 |
|map outStream mapChar| |
20013 | 4640 |
|
4641 |
map := self class unicodeDenormalizationMap. |
|
4642 |
||
4643 |
self containsNon7BitAscii ifFalse:[^ self]. "/ I cannot contain any |
|
4644 |
||
20014 | 4645 |
mapChar := |
4646 |
[:char | |
|
4647 |
|mappedChars| |
|
4648 |
||
4649 |
(mappedChars := map at:char ifAbsent:nil) notNil ifTrue:[ |
|
4650 |
mappedChars do:mapChar. |
|
4651 |
] ifFalse:[ |
|
4652 |
outStream nextPut:char. |
|
4653 |
]. |
|
4654 |
]. |
|
20013 | 4655 |
|
20014 | 4656 |
outStream := WriteStream on:(Unicode16String new:self size). |
4657 |
self do:mapChar. |
|
20013 | 4658 |
^ outStream contents asSingleByteStringIfPossible. |
4659 |
||
4660 |
" |
|
4661 |
'Ö' asDenormalizedUnicodeString |
|
4662 |
'aÖÄx' asDenormalizedUnicodeString |
|
4663 |
'abc' asDenormalizedUnicodeString |
|
20014 | 4664 |
'ṩ' asString asDenormalizedUnicodeString |
20013 | 4665 |
" |
4666 |
! |
|
4667 |
||
20011 | 4668 |
asNormalizedUnicodeString |
20010 | 4669 |
"return a new string containing the same characters, as a normalized Unicode string. |
4670 |
This replaces combination characters by corresponding single characters. |
|
4671 |
(i.e. diaresis and other combining diacriticals in the 0x03xx range)" |
|
4672 |
||
4673 |
|outStream prevChar map mapEntries mappedChar| |
|
4674 |
||
4675 |
map := self class unicodeNormalizationMap. |
|
20017 | 4676 |
|
20010 | 4677 |
self bitsPerCharacter <= 8 ifTrue:[^ self]. "/ I cannot contain any overtypes |
20017 | 4678 |
(self contains:[:ch | ch unicodeBlock == #'COMBINING_DIACRITICAL_MARKS']) ifFalse:[^ self]. "/ I do not contain any overtypes |
20010 | 4679 |
|
4680 |
outStream := WriteStream on:(self species new:self size). |
|
4681 |
self do:[:char | |
|
20017 | 4682 |
((char unicodeBlock == #'COMBINING_DIACRITICAL_MARKS') |
20010 | 4683 |
and:[ (mapEntries := map at:char ifAbsent:nil) notNil |
4684 |
and:[ (mappedChar := mapEntries at:prevChar ifAbsent:nil) notNil ]]) ifTrue:[ |
|
20014 | 4685 |
prevChar := mappedChar. |
20010 | 4686 |
] ifFalse:[ |
4687 |
prevChar notNil ifTrue:[ outStream nextPut:prevChar]. |
|
4688 |
prevChar := char |
|
4689 |
]. |
|
4690 |
]. |
|
20011 | 4691 |
prevChar notNil ifTrue:[ outStream nextPut:prevChar]. |
20010 | 4692 |
^ outStream contents asSingleByteStringIfPossible. |
4693 |
||
4694 |
" |
|
4695 |
self unicodeNormalizationMap |
|
20012 | 4696 |
('O' , (Character value:16r0308)) asNormalizedUnicodeString -> 'Ö' |
20010 | 4697 |
('O' , (Character value:16r0308) , |
20012 | 4698 |
'A' , (Character value:16r0308)) asNormalizedUnicodeString -> 'ÖÄ' |
20014 | 4699 |
('s' , (Character value:16r0323) , (Character value:16r0307)) asNormalizedUnicodeString -> 'ṩ' |
4700 |
('s' , (Character value:16r0307) , (Character value:16r0323)) asNormalizedUnicodeString -> 'ṩ' |
|
20010 | 4701 |
" |
4702 |
! |
|
4703 |
||
12767
17d2c7268065
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12679
diff
changeset
|
4704 |
decodeAsLiteralArray |
17d2c7268065
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12679
diff
changeset
|
4705 |
"given a literalEncoding in the receiver, |
17d2c7268065
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12679
diff
changeset
|
4706 |
create & return the corresponding object. |
17d2c7268065
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12679
diff
changeset
|
4707 |
The inverse operation to #literalArrayEncoding." |
17d2c7268065
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12679
diff
changeset
|
4708 |
|
17d2c7268065
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12679
diff
changeset
|
4709 |
^ self |
17d2c7268065
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12679
diff
changeset
|
4710 |
! |
17d2c7268065
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12679
diff
changeset
|
4711 |
|
991
f0b45a4b82d2
first attempt in encoding/decoding for different character representations (mac/msdos etc.)
Claus Gittinger <cg@exept.de>
parents:
986
diff
changeset
|
4712 |
decodeFrom:encodingSymbol |
8874 | 4713 |
"given the receiver encoded as described by encodingSymbol, |
8124
ca774158b317
code cleanup (removed old obsolete encoding stuff)
Claus Gittinger <cg@exept.de>
parents:
8102
diff
changeset
|
4714 |
convert it into internal ST/X (unicode) encoding and return a corresponding CharacterArray." |
ca774158b317
code cleanup (removed old obsolete encoding stuff)
Claus Gittinger <cg@exept.de>
parents:
8102
diff
changeset
|
4715 |
|
ca774158b317
code cleanup (removed old obsolete encoding stuff)
Claus Gittinger <cg@exept.de>
parents:
8102
diff
changeset
|
4716 |
|myEncoding encoder| |
7950 | 4717 |
|
4718 |
encodingSymbol isNil ifTrue:[^ self]. |
|
4719 |
myEncoding := self encoding. |
|
4720 |
encodingSymbol == myEncoding ifTrue:[^ self]. |
|
4721 |
||
4722 |
encoder := CharacterEncoder encoderToEncodeFrom:(self encoding) into:encodingSymbol. |
|
8124
ca774158b317
code cleanup (removed old obsolete encoding stuff)
Claus Gittinger <cg@exept.de>
parents:
8102
diff
changeset
|
4723 |
encoder isNil ifTrue:[^ self]. |
ca774158b317
code cleanup (removed old obsolete encoding stuff)
Claus Gittinger <cg@exept.de>
parents:
8102
diff
changeset
|
4724 |
^ encoder decodeString:self. |
991
f0b45a4b82d2
first attempt in encoding/decoding for different character representations (mac/msdos etc.)
Claus Gittinger <cg@exept.de>
parents:
986
diff
changeset
|
4725 |
! |
f0b45a4b82d2
first attempt in encoding/decoding for different character representations (mac/msdos etc.)
Claus Gittinger <cg@exept.de>
parents:
986
diff
changeset
|
4726 |
|
9192 | 4727 |
encodeFrom:oldEncoding into:newEncoding |
4728 |
^ CharacterEncoder encodeString:self from:oldEncoding into:newEncoding |
|
14405
1d0d99a95bba
comment/format in:7 methods
Claus Gittinger <cg@exept.de>
parents:
14168
diff
changeset
|
4729 |
|
1d0d99a95bba
comment/format in:7 methods
Claus Gittinger <cg@exept.de>
parents:
14168
diff
changeset
|
4730 |
" |
17600 | 4731 |
'äüö' encodeFrom:#iso8859 into:#utf8 |
4732 |
('äüö' encodeFrom:#iso8859 into:#utf8) encodeFrom:#utf8 into:#unicode |
|
14405
1d0d99a95bba
comment/format in:7 methods
Claus Gittinger <cg@exept.de>
parents:
14168
diff
changeset
|
4733 |
" |
9192 | 4734 |
! |
4735 |
||
4736 |
rot13 |
|
8874 | 4737 |
"Usenet: from `rotate alphabet 13 places'] |
6835
bdb2cb03200c
added utf8Decode with 8-bit chars only
Claus Gittinger <cg@exept.de>
parents:
6834
diff
changeset
|
4738 |
The simple Caesar-cypher encryption that replaces each English |
8874 | 4739 |
letter with the one 13 places forward or back along the alphabet, |
6835
bdb2cb03200c
added utf8Decode with 8-bit chars only
Claus Gittinger <cg@exept.de>
parents:
6834
diff
changeset
|
4740 |
so that 'The butler did it!!' becomes 'Gur ohgyre qvq vg!!' |
8874 | 4741 |
Most Usenet news reading and posting programs include a rot13 feature. |
6835
bdb2cb03200c
added utf8Decode with 8-bit chars only
Claus Gittinger <cg@exept.de>
parents:
6834
diff
changeset
|
4742 |
It is used to enclose the text in a sealed wrapper that the reader must choose |
8874 | 4743 |
to open -- e.g., for posting things that might offend some readers, or spoilers. |
6835
bdb2cb03200c
added utf8Decode with 8-bit chars only
Claus Gittinger <cg@exept.de>
parents:
6834
diff
changeset
|
4744 |
A major advantage of rot13 over rot(N) for other N is that it |
bdb2cb03200c
added utf8Decode with 8-bit chars only
Claus Gittinger <cg@exept.de>
parents:
6834
diff
changeset
|
4745 |
is self-inverse, so the same code can be used for encoding and decoding." |
bdb2cb03200c
added utf8Decode with 8-bit chars only
Claus Gittinger <cg@exept.de>
parents:
6834
diff
changeset
|
4746 |
|
11865 | 4747 |
^ self rot:13 |
8874 | 4748 |
|
4749 |
" |
|
4750 |
'hello world' rot13 |
|
4751 |
'hello world' rot13 rot13 |
|
6835
bdb2cb03200c
added utf8Decode with 8-bit chars only
Claus Gittinger <cg@exept.de>
parents:
6834
diff
changeset
|
4752 |
" |
bdb2cb03200c
added utf8Decode with 8-bit chars only
Claus Gittinger <cg@exept.de>
parents:
6834
diff
changeset
|
4753 |
! |
bdb2cb03200c
added utf8Decode with 8-bit chars only
Claus Gittinger <cg@exept.de>
parents:
6834
diff
changeset
|
4754 |
|
11865 | 4755 |
rot:n |
4756 |
"Usenet: from `rotate alphabet N places'] |
|
4757 |
The simple Caesar-cypher encryption that replaces each English |
|
4758 |
letter with the one N places forward or back along the alphabet, |
|
4759 |
so that 'The butler did it!!' becomes 'Gur ohgyre qvq vg!!' by rot 13 |
|
4760 |
Most Usenet news reading and posting programs include a rot13 feature. |
|
4761 |
It is used to enclose the text in a sealed wrapper that the reader must choose |
|
4762 |
to open -- e.g., for posting things that might offend some readers, or spoilers. |
|
4763 |
A major advantage of rot13 over rot(N) for other N is that it |
|
4764 |
is self-inverse, so the same code can be used for encoding and decoding." |
|
4765 |
||
4766 |
^ self species |
|
20012 | 4767 |
streamContents:[:aStream | |
4768 |
self do:[:char | |
|
4769 |
aStream nextPut:(char rot:n) ]] |
|
11865 | 4770 |
|
4771 |
" |
|
4772 |
'hello world' rot:13 |
|
4773 |
('hello world' rot:13) rot:13 |
|
4774 |
" |
|
4775 |
! |
|
4776 |
||
15656 | 4777 |
utf16Encoded |
4778 |
"Return my UTF-16 representation as a new TwoByteString" |
|
4779 |
||
4780 |
|s| |
|
4781 |
||
4782 |
s := WriteStream on:(TwoByteString uninitializedNew:self size). |
|
4783 |
s nextPutAllUtf16:self. |
|
4784 |
^ s contents |
|
4785 |
||
4786 |
" |
|
4787 |
'abcde1234' utf16Encoded |
|
17600 | 4788 |
'abcdeäöüß' utf16Encoded |
15656 | 4789 |
" |
4790 |
||
4791 |
"Modified: / 11-05-2010 / 19:12:37 / cg" |
|
4792 |
! |
|
4793 |
||
9192 | 4794 |
utf8Encoded |
19837 | 4795 |
"return the UTF-8 representation of a Unicode string. |
4796 |
The resulting string is only useful to be stored on some external file, |
|
4797 |
not for being used inside ST/X." |
|
4798 |
||
4799 |
|string stream| |
|
17625 | 4800 |
|
4801 |
string := self string. |
|
19837 | 4802 |
|
4803 |
"/ avoid creation of new strings if possible |
|
4804 |
string containsNon7BitAscii ifFalse:[ |
|
4805 |
^ string asSingleByteString |
|
4806 |
]. |
|
4807 |
||
4808 |
"make it size 2 for 1-byte strings" |
|
4809 |
stream := WriteStream on:(String uninitializedNew:(1 + (string size * 3 // 2))). |
|
4810 |
string utf8EncodedOn:stream. |
|
4811 |
^ stream contents. |
|
4812 |
||
4813 |
" |
|
4814 |
'hello' utf8Encoded asByteArray #[104 101 108 108 111] |
|
4815 |
(Character value:16r40) asString utf8Encoded asByteArray #[64] |
|
4816 |
(Character value:16r7F) asString utf8Encoded asByteArray #[127] |
|
4817 |
(Character value:16r80) asString utf8Encoded asByteArray #[194 128] |
|
4818 |
(Character value:16rFF) asString utf8Encoded asByteArray #[195 191] |
|
4819 |
(Character value:16r100) asString utf8Encoded asByteArray #[196 128] |
|
4820 |
(Character value:16r200) asString utf8Encoded asByteArray #[200 128] |
|
4821 |
(Character value:16r400) asString utf8Encoded asByteArray #[208 128] |
|
4822 |
(Character value:16r800) asString utf8Encoded asByteArray #[224 160 128] |
|
4823 |
(Character value:16r1000) asString utf8Encoded asByteArray #[225 128 128] |
|
4824 |
(Character value:16r2000) asString utf8Encoded asByteArray #[226 128 128] |
|
4825 |
(Character value:16r4000) asString utf8Encoded asByteArray #[228 128 128] |
|
4826 |
(Character value:16r8000) asString utf8Encoded asByteArray #[232 128 128] |
|
4827 |
(Character value:16rFFFF) asString utf8Encoded asByteArray #[239 191 191] |
|
4828 |
||
18597 | 4829 |
'abcde1234' asUnicode32String utf8Encoded |
4830 |
'abcdeäöüß' asUnicode32String utf8Encoded |
|
12925
5c0a9a633ca1
comment/format in: #utf8Encoded
Claus Gittinger <cg@exept.de>
parents:
12912
diff
changeset
|
4831 |
" |
17625 | 4832 |
! |
4833 |
||
4834 |
utf8EncodedOn:aStream |
|
19837 | 4835 |
"return the UTF-8 representation of a Unicode string. |
4836 |
The resulting string is only useful to be stored on some external file, |
|
4837 |
not for being used inside ST/X." |
|
4838 |
||
4839 |
|string |
|
4840 |
stringSize "{ Class: SmallInteger }" |
|
4841 |
codePoint "{Class: SmallInteger }" |
|
4842 |
v "{Class: SmallInteger }" |
|
4843 |
character b1 b2 b3 b4 b5| |
|
17625 | 4844 |
|
4845 |
string := self string. |
|
19837 | 4846 |
|
4847 |
"/ avoid creation of new strings if possible |
|
4848 |
string containsNon7BitAscii ifFalse:[ |
|
4849 |
aStream nextPutAll:string asSingleByteString. |
|
4850 |
^ self. |
|
4851 |
]. |
|
4852 |
||
4853 |
stringSize := string size. |
|
4854 |
||
4855 |
1 to:stringSize do:[:idx | |
|
4856 |
character := string at:idx. |
|
4857 |
codePoint := character codePoint. |
|
4858 |
codePoint < 16r80 ifTrue:[ |
|
4859 |
aStream nextPutByte:codePoint. |
|
4860 |
] ifFalse:[ |
|
4861 |
b1 := (codePoint bitAnd:16r3F) bitOr:2r10000000. |
|
4862 |
v := codePoint bitShift:-6. |
|
4863 |
v <= 16r1F ifTrue:[ |
|
4864 |
aStream nextPutByte:(v bitOr:2r11000000). |
|
4865 |
aStream nextPutByte:b1. |
|
4866 |
] ifFalse:[ |
|
4867 |
b2 := (v bitAnd:16r3F) bitOr:2r10000000. |
|
4868 |
v := v bitShift:-6. |
|
4869 |
v <= 16r0F ifTrue:[ |
|
4870 |
aStream nextPutByte:(v bitOr:2r11100000). |
|
4871 |
aStream nextPutByte:b2; nextPutByte:b1. |
|
4872 |
] ifFalse:[ |
|
4873 |
b3 := (v bitAnd:16r3F) bitOr:2r10000000. |
|
4874 |
v := v bitShift:-6. |
|
4875 |
v <= 16r07 ifTrue:[ |
|
4876 |
aStream nextPutByte:(v bitOr:2r11110000). |
|
4877 |
aStream nextPutByte:b3; nextPutByte:b2; nextPutByte:b1. |
|
4878 |
] ifFalse:[ |
|
4879 |
b4 := (v bitAnd:16r3F) bitOr:2r10000000. |
|
4880 |
v := v bitShift:-6. |
|
4881 |
v <= 16r03 ifTrue:[ |
|
4882 |
aStream nextPutByte:(v bitOr:2r11111000). |
|
4883 |
aStream nextPutByte:b4; nextPutByte:b3; nextPutByte:b2; nextPutByte:b1. |
|
4884 |
] ifFalse:[ |
|
4885 |
b5 := (v bitAnd:16r3F) bitOr:2r10000000. |
|
4886 |
v := v bitShift:-6. |
|
4887 |
v <= 16r01 ifTrue:[ |
|
4888 |
aStream nextPutByte:(v bitOr:2r11111100). |
|
4889 |
aStream nextPutByte:b5; nextPutByte:b4; nextPutByte:b3; nextPutByte:b2; nextPutByte:b1. |
|
4890 |
] ifFalse:[ |
|
4891 |
"/ cannot happen - we only support up to 30 bit characters |
|
4892 |
EncodingError raiseWith:character errorString:'codePoint > 31bit in #utf8Encode'. |
|
4893 |
] |
|
4894 |
]. |
|
4895 |
]. |
|
4896 |
]. |
|
4897 |
]. |
|
4898 |
]. |
|
4899 |
]. |
|
4900 |
||
4901 |
" |
|
4902 |
String streamContents:[:s| |
|
4903 |
'hallo' utf8EncodedOn:s |
|
4904 |
]. |
|
4905 |
||
4906 |
ByteArray streamContents:[:s| |
|
4907 |
'hallo' utf8EncodedOn:s |
|
4908 |
]. |
|
4909 |
||
4910 |
String streamContents:[:s| |
|
4911 |
'abcdeäöüß' asUnicode32String utf8EncodedOn:s |
|
4912 |
]. |
|
4913 |
. |
|
4914 |
ByteArray streamContents:[:s| |
|
4915 |
'abcdeäöüß' asUnicode32String utf8EncodedOn:s |
|
4916 |
]. |
|
4917 |
||
4918 |
'/tmp/bytes' asFilename writingFileDo:[:s| |
|
4919 |
'abcdeäöüß' utf8EncodedOn:s |
|
4920 |
]. |
|
17625 | 4921 |
" |
9192 | 4922 |
! ! |
4923 |
||
11345 | 4924 |
|
20155 | 4925 |
|
4926 |
||
12111 | 4927 |
!CharacterArray methodsFor:'matching - glob expressions'! |
4928 |
||
4929 |
compoundMatch:aString |
|
4930 |
"like match, but the receiver may be a compound match pattern, |
|
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
4931 |
consisting of multiple simple GLOB patterns, separated by semicolons. |
12111 | 4932 |
This is usable with fileName pattern fields. |
4933 |
||
4934 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20012 | 4935 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
4936 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 4937 |
NOTICE: the receiver is the match pattern" |
4938 |
||
15890 | 4939 |
^ self compoundMatch:aString caseSensitive:true withoutSeparators:false |
12111 | 4940 |
|
4941 |
" |
|
4942 |
'f*' match:'foo' |
|
4943 |
'b*' match:'foo' |
|
4944 |
'f*;b*' match:'foo' |
|
4945 |
'f*;b*' match:'bar' |
|
4946 |
'f*;b*' compoundMatch:'foo' |
|
4947 |
'f*;b*' compoundMatch:'bar' |
|
4948 |
" |
|
4949 |
||
4950 |
"Modified: / 30.1.1998 / 11:40:18 / stefan" |
|
4951 |
"Modified: / 16.12.1999 / 01:22:08 / cg" |
|
4952 |
! |
|
4953 |
||
15734 | 4954 |
compoundMatch:aString caseSensitive:caseSensitive |
12111 | 4955 |
"like match, but the receiver may be a compound match pattern, |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
4956 |
consisting of multiple simple GLOB patterns, separated by semicolons. |
12111 | 4957 |
This is usable with fileName pattern fields. |
4958 |
||
4959 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20012 | 4960 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
4961 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 4962 |
NOTICE: the receiver is the match pattern" |
4963 |
||
15890 | 4964 |
^self compoundMatch:aString caseSensitive:caseSensitive withoutSeparators:false |
4965 |
||
4966 |
" |
|
4967 |
'f*' match:'foo' |
|
4968 |
'b*' match:'foo' |
|
4969 |
'f*;b*' match:'foo' |
|
4970 |
'f*;b*' match:'bar' |
|
4971 |
'f*;b*' compoundMatch:'foo' |
|
4972 |
'f*;b*' compoundMatch:'bar' |
|
4973 |
'f*;b*' compoundMatch:'Foo' caseSensitive:false |
|
4974 |
'f*;b*' compoundMatch:'Bar' caseSensitive:false |
|
4975 |
'f*;b*' compoundMatch:'ccc' caseSensitive:false |
|
4976 |
" |
|
4977 |
||
4978 |
"Modified: / 15.4.1997 / 15:50:33 / cg" |
|
4979 |
"Modified: / 30.1.1998 / 11:40:18 / stefan" |
|
4980 |
"Created: / 16.12.1999 / 01:21:35 / cg" |
|
4981 |
! |
|
4982 |
||
4983 |
compoundMatch:aString caseSensitive:caseSensitive withoutSeparators:withoutSeparators |
|
4984 |
"like match, but the receiver may be a compound match pattern, |
|
4985 |
consisting of multiple simple GLOB patterns, separated by semicolons. |
|
4986 |
If withoutSeparators is true, spaces around individual patterns are stripped off. |
|
4987 |
This is usable with fileName pattern fields. |
|
4988 |
||
4989 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20012 | 4990 |
NOT the ST-80 meaning. |
15890 | 4991 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
4992 |
NOTICE: the receiver is the match pattern" |
|
4993 |
||
12111 | 4994 |
|matchers| |
4995 |
||
4996 |
matchers := self asCollectionOfSubstringsSeparatedBy:$;. |
|
15890 | 4997 |
withoutSeparators ifTrue:[ matchers := matchers collect:[:each | each withoutSeparators] ]. |
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
4998 |
^ matchers |
20012 | 4999 |
contains:[:aPattern | |
5000 |
aPattern match:aString caseSensitive:caseSensitive escapeCharacter:nil |
|
5001 |
]. |
|
15734 | 5002 |
|
5003 |
" |
|
5004 |
'f*' match:'foo' |
|
5005 |
'b*' match:'foo' |
|
5006 |
'f*;b*' match:'foo' |
|
5007 |
'f*;b*' match:'bar' |
|
5008 |
'f*;b*' compoundMatch:'foo' |
|
5009 |
'f*;b*' compoundMatch:'bar' |
|
5010 |
'f*;b*' compoundMatch:'Foo' caseSensitive:false |
|
5011 |
'f*;b*' compoundMatch:'Bar' caseSensitive:false |
|
5012 |
'f*;b*' compoundMatch:'ccc' caseSensitive:false |
|
15890 | 5013 |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
5014 |
'f* ; b*' compoundMatch:'foo' |
15890 | 5015 |
'f* ; b*' compoundMatch:'foo' caseSensitive:true withoutSeparators:true |
15734 | 5016 |
" |
5017 |
||
5018 |
"Modified: / 15.4.1997 / 15:50:33 / cg" |
|
5019 |
"Modified: / 30.1.1998 / 11:40:18 / stefan" |
|
5020 |
"Created: / 16.12.1999 / 01:21:35 / cg" |
|
5021 |
! |
|
5022 |
||
5023 |
compoundMatch:aString ignoreCase:ignoreCase |
|
5024 |
<resource: #obsolete> |
|
5025 |
||
5026 |
"like match, but the receiver may be a compound match pattern, |
|
5027 |
consisting of multiple simple GLOB patterns, separated by semicolons. |
|
5028 |
This is usable with fileName pattern fields. |
|
5029 |
||
5030 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20012 | 5031 |
NOT the ST-80 meaning. |
15734 | 5032 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
5033 |
NOTICE: the receiver is the match pattern" |
|
5034 |
||
5035 |
^ self compoundMatch:aString caseSensitive:ignoreCase not |
|
12111 | 5036 |
|
5037 |
" |
|
5038 |
'f*' match:'foo' |
|
5039 |
'b*' match:'foo' |
|
5040 |
'f*;b*' match:'foo' |
|
5041 |
'f*;b*' match:'bar' |
|
5042 |
'f*;b*' compoundMatch:'foo' |
|
5043 |
'f*;b*' compoundMatch:'bar' |
|
5044 |
'f*;b*' compoundMatch:'Foo' ignoreCase:true |
|
5045 |
'f*;b*' compoundMatch:'Bar' ignoreCase:true |
|
5046 |
'f*;b*' compoundMatch:'ccc' ignoreCase:true |
|
5047 |
" |
|
5048 |
||
5049 |
"Modified: / 15.4.1997 / 15:50:33 / cg" |
|
5050 |
"Modified: / 30.1.1998 / 11:40:18 / stefan" |
|
5051 |
"Created: / 16.12.1999 / 01:21:35 / cg" |
|
5052 |
! |
|
5053 |
||
5054 |
findMatchString:matchString |
|
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5055 |
"like findString/indexOfSubCollection, but allowing GLOB match patterns. |
12111 | 5056 |
find matchstring; if found, return the index; |
5057 |
if not found, return 0. |
|
5058 |
||
5059 |
NOTICE: match-meta character interpretation is like in unix-matching, |
|
20012 | 5060 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5061 |
NOTICE: this GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 5062 |
NOTICE: the argument is the match pattern" |
5063 |
||
19033 | 5064 |
^ self findMatchString:matchString startingAt:1 caseSensitive:true ifAbsent:0 |
5065 |
||
5066 |
" |
|
5067 |
1234567890123 |
|
5068 |
'hello world bla foo baz' findMatchString:'b* ' |
|
5069 |
" |
|
12111 | 5070 |
! |
5071 |
||
5072 |
findMatchString:matchString startingAt:index |
|
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5073 |
"like findString, but allowing GLOB match patterns. |
12111 | 5074 |
find matchstring, starting at index. if found, return the index; |
5075 |
if not found, return 0. |
|
5076 |
||
5077 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20012 | 5078 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5079 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 5080 |
NOTICE: the argument is the match pattern" |
5081 |
||
19033 | 5082 |
^ self findMatchString:matchString startingAt:index caseSensitive:true ifAbsent:0 |
12111 | 5083 |
! |
5084 |
||
15734 | 5085 |
findMatchString:matchString startingAt:index caseSensitive:caseSensitive ifAbsent:exceptionBlock |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5086 |
"like findString, but allowing GLOB match patterns. |
12111 | 5087 |
find matchstring, starting at index. if found, return the index; |
5088 |
if not found, return the result of evaluating exceptionBlock. |
|
5089 |
This is a q&d hack - not very efficient. |
|
5090 |
||
5091 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20012 | 5092 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5093 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 5094 |
NOTICE: the argument is the match pattern" |
5095 |
||
5096 |
|firstChar firstSet |
|
5097 |
startIndex "{ Class: SmallInteger }" |
|
5098 |
matchSize "{ Class: SmallInteger }" |
|
5099 |
mySize "{ Class: SmallInteger }" |
|
5100 |
realMatchString lcChar ucChar| |
|
5101 |
||
5102 |
matchSize := matchString size. |
|
5103 |
matchSize == 0 ifTrue:[^ index]. "empty string matches" |
|
5104 |
||
5105 |
realMatchString := matchString. |
|
5106 |
(realMatchString endsWith:$*) ifFalse:[ |
|
20012 | 5107 |
realMatchString := realMatchString , '*'. |
5108 |
matchSize := matchSize + 1 |
|
12111 | 5109 |
]. |
5110 |
||
5111 |
mySize := self size. |
|
5112 |
firstChar := realMatchString at:1. |
|
5113 |
firstChar == self class matchEscapeCharacter ifTrue:[ |
|
20012 | 5114 |
firstChar := realMatchString at:2. |
12111 | 5115 |
]. |
5116 |
||
5117 |
firstChar asString includesMatchCharacters ifTrue:[ |
|
20012 | 5118 |
index to:mySize do:[:col | |
5119 |
(realMatchString match:self from:col to:mySize caseSensitive:caseSensitive) |
|
5120 |
ifTrue:[^ col] |
|
5121 |
]. |
|
5122 |
^ exceptionBlock value. |
|
12111 | 5123 |
]. |
5124 |
||
5125 |
lcChar := firstChar asLowercase. |
|
5126 |
ucChar := firstChar asUppercase. |
|
15734 | 5127 |
(caseSensitive not and:[ lcChar ~= ucChar ]) ifTrue:[ |
20012 | 5128 |
firstSet := Array with:ucChar with:lcChar. |
5129 |
startIndex := self indexOfAny:firstSet startingAt:index. |
|
12111 | 5130 |
] ifFalse:[ |
20012 | 5131 |
startIndex := self indexOf:firstChar startingAt:index. |
12111 | 5132 |
]. |
5133 |
[startIndex == 0] whileFalse:[ |
|
20012 | 5134 |
(realMatchString match:self from:startIndex to:mySize caseSensitive:caseSensitive) |
5135 |
ifTrue:[^ startIndex]. |
|
5136 |
||
5137 |
firstSet notNil ifTrue:[ |
|
5138 |
startIndex := self indexOfAny:firstSet startingAt:(startIndex + 1). |
|
5139 |
] ifFalse:[ |
|
5140 |
startIndex := self indexOf:firstChar startingAt:(startIndex + 1). |
|
5141 |
]. |
|
12111 | 5142 |
]. |
5143 |
^ exceptionBlock value |
|
5144 |
||
5145 |
" |
|
5146 |
'one two three four' findMatchString:'o[nu]' |
|
5147 |
'one two three four' findMatchString:'o[nu]' startingAt:3 |
|
15734 | 5148 |
'one two three four one' findMatchString:'ONE' startingAt:3 caseSensitive:false ifAbsent:0 |
5149 |
'one two three four one' findMatchString:'ONE' startingAt:3 caseSensitive:true ifAbsent:0 |
|
5150 |
'one two three four ONE' findMatchString:'O#E' startingAt:1 caseSensitive:false ifAbsent:0 |
|
5151 |
'one two three four ONE' findMatchString:'O#E' startingAt:1 caseSensitive:true ifAbsent:0 |
|
5152 |
12345678901234567890 |
|
5153 |
" |
|
5154 |
||
5155 |
"Modified: 13.9.1997 / 06:31:22 / cg" |
|
5156 |
! |
|
5157 |
||
5158 |
findMatchString:matchString startingAt:index ignoreCase:ignoreCase ifAbsent:exceptionBlock |
|
5159 |
<resource: #obsolete> |
|
5160 |
"like findString, but allowing GLOB match patterns. |
|
5161 |
find matchstring, starting at index. if found, return the index; |
|
5162 |
if not found, return the result of evaluating exceptionBlock. |
|
5163 |
This is a q&d hack - not very efficient. |
|
5164 |
||
5165 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20012 | 5166 |
NOT the ST-80 meaning. |
15734 | 5167 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
5168 |
NOTICE: the argument is the match pattern" |
|
5169 |
||
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
5170 |
^ self |
20012 | 5171 |
findMatchString:matchString startingAt:index caseSensitive:ignoreCase not ifAbsent:exceptionBlock |
15734 | 5172 |
|
5173 |
" |
|
5174 |
'one two three four' findMatchString:'o[nu]' |
|
5175 |
'one two three four' findMatchString:'o[nu]' startingAt:3 |
|
12111 | 5176 |
'one two three four one' findMatchString:'ONE' startingAt:3 ignoreCase:true ifAbsent:0 |
5177 |
" |
|
5178 |
||
5179 |
"Modified: 13.9.1997 / 06:31:22 / cg" |
|
5180 |
! |
|
5181 |
||
19746 | 5182 |
globPatternAsRegexPattern |
5183 |
"taking the receiver as a GLOB pattern, |
|
5184 |
return a corresponding regex pattern. |
|
5185 |
As regex does provide information about the matching substring, |
|
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
5186 |
it may be useful to apply a regex after a GLOB match, |
19746 | 5187 |
in order to highlight matching substrings (eg. in a CodeView after a search). |
5188 |
If it turns out to be better, we may convert all GLOB searches to regex and use it right away. |
|
5189 |
(currently, it is not sure, if GLOB is not better for most simple searches, as they are encountered |
|
5190 |
in typical real life)" |
|
5191 |
||
5192 |
^ self species streamContents:[:s | |
|
20012 | 5193 |
|addCharacter| |
5194 |
||
5195 |
addCharacter := |
|
5196 |
[:ch | |
|
5197 |
ch isLetterOrDigit ifFalse:[ |
|
5198 |
s nextPut:$\. |
|
5199 |
]. |
|
5200 |
s nextPut:ch |
|
5201 |
]. |
|
5202 |
||
5203 |
(String matchScanArrayFrom:self) do:[:matchEntry | |
|
5204 |
matchEntry isCharacter ifTrue:[ |
|
5205 |
addCharacter value:matchEntry |
|
5206 |
] ifFalse:[ |
|
5207 |
matchEntry == #anyString ifTrue:[ |
|
5208 |
s nextPutAll:'.*' |
|
5209 |
] ifFalse:[ |
|
5210 |
matchEntry == #any ifTrue:[ |
|
5211 |
s nextPut:$. |
|
5212 |
] ifFalse:[ |
|
5213 |
matchEntry isString ifTrue:[ |
|
5214 |
|set min max| |
|
5215 |
||
5216 |
s nextPut:$[. |
|
5217 |
set := matchEntry copy sort. |
|
5218 |
min := set min. |
|
5219 |
max := set max. |
|
5220 |
set asSet = (min to:max) asSet ifTrue:[ |
|
5221 |
addCharacter value:min. |
|
5222 |
s nextPut:$-. |
|
5223 |
addCharacter value:max. |
|
5224 |
] ifFalse:[ |
|
5225 |
set do:addCharacter. |
|
5226 |
]. |
|
5227 |
s nextPut:$]. |
|
5228 |
] ifFalse:[ |
|
5229 |
self halt. |
|
5230 |
]. |
|
5231 |
]. |
|
5232 |
]. |
|
5233 |
] |
|
5234 |
]. |
|
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
5235 |
]. |
19746 | 5236 |
|
5237 |
" |
|
5238 |
'hello' globPatternAsRegexPattern |
|
5239 |
'hello*' globPatternAsRegexPattern |
|
5240 |
'*hello*' globPatternAsRegexPattern |
|
5241 |
'h###' globPatternAsRegexPattern |
|
5242 |
'h[0-9]' globPatternAsRegexPattern |
|
5243 |
'h[0-9][0-9][0-9]' globPatternAsRegexPattern |
|
5244 |
'h[0-9]*' globPatternAsRegexPattern |
|
19747 | 5245 |
'h[-+]*' globPatternAsRegexPattern |
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
5246 |
|
19747 | 5247 |
'hello world' matches:'h*w' |
5248 |
'hello world' matchesRegex:('h*w' globPatternAsRegexPattern) |
|
5249 |
'hello world' matches:'h*d' |
|
5250 |
'hello world' matchesRegex:('h*d' globPatternAsRegexPattern) |
|
19746 | 5251 |
" |
5252 |
! |
|
5253 |
||
12111 | 5254 |
includesMatchString:matchString |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5255 |
"like includesString, but allowing GLOB match patterns. |
12111 | 5256 |
find matchstring; if found, return true, otherwise return false. |
5257 |
||
5258 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20012 | 5259 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5260 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 5261 |
NOTICE: the argument is the match pattern" |
5262 |
||
5263 |
^ (self findMatchString:matchString) ~~ 0 |
|
5264 |
||
5265 |
" |
|
5266 |
'hello world' includesMatchString:'h*' |
|
5267 |
'hello world' includesMatchString:'h[aeiou]llo' |
|
5268 |
'hello world' includesMatchString:'wor*' |
|
5269 |
'hello world' includesMatchString:'woR*' |
|
5270 |
" |
|
5271 |
! |
|
5272 |
||
5273 |
includesMatchString:matchString caseSensitive:caseSensitive |
|
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5274 |
"like includesString, but allowing GLOB match patterns. |
12111 | 5275 |
find matchstring; if found, return true, otherwise return false. |
5276 |
||
5277 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20012 | 5278 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5279 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 5280 |
NOTICE: the argument is the match pattern" |
5281 |
||
19033 | 5282 |
^ (self findMatchString:matchString startingAt:1 caseSensitive:caseSensitive ifAbsent:0) ~~ 0 |
12111 | 5283 |
|
5284 |
" |
|
5285 |
'hello world' includesMatchString:'h*' caseSensitive:true |
|
5286 |
'hello world' includesMatchString:'h*' caseSensitive:false |
|
5287 |
'Hello world' includesMatchString:'h*' caseSensitive:true |
|
5288 |
'Hello world' includesMatchString:'h*' caseSensitive:false |
|
5289 |
||
5290 |
'hello world' includesMatchString:'h[aeiou]llo' caseSensitive:true |
|
5291 |
'hello world' includesMatchString:'h[aeiou]llo' caseSensitive:false |
|
5292 |
||
5293 |
'hello world' includesMatchString:'wor*' caseSensitive:true |
|
5294 |
'hello world' includesMatchString:'wor*' caseSensitive:false |
|
5295 |
||
5296 |
'hello world' includesMatchString:'woR*' caseSensitive:true |
|
5297 |
'hello world' includesMatchString:'woR*' caseSensitive:false |
|
5298 |
" |
|
5299 |
! |
|
5300 |
||
5301 |
match:aString |
|
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5302 |
"return true if aString matches self, where self may contain GLOB meta-match |
12111 | 5303 |
characters $* (to match any string) or $# (to match any character). |
5304 |
or [...] to match a set of characters. |
|
5305 |
Lower/uppercase are considered different. |
|
5306 |
The escape character is the backQuote. |
|
5307 |
||
5308 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20012 | 5309 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5310 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 5311 |
NOTICE: the receiver is the match pattern" |
5312 |
||
15734 | 5313 |
^ self match:aString from:1 to:aString size caseSensitive:true |
12111 | 5314 |
|
5315 |
" |
|
5316 |
'\*f*' match:'f' |
|
5317 |
'\*f*' match:'*f' |
|
5318 |
'*\*f*' match:'*f' |
|
5319 |
'*f*' match:'*f' |
|
5320 |
'*ute*' match:'computer' |
|
5321 |
'*uter' match:'computer' |
|
5322 |
'uter*' match:'computer' |
|
5323 |
'*ute*' match:'' |
|
5324 |
'[abcd]*' match:'computer' |
|
5325 |
'[abcd]*' match:'komputer' |
|
5326 |
'*some*compl*ern*' match:'this is some more complicated pattern match' |
|
5327 |
'*some*compl*ern*' match:'this is another complicated pattern match' |
|
5328 |
'*-hh' match:'anton-h' |
|
5329 |
" |
|
5330 |
||
5331 |
"Modified: / 9.6.1998 / 18:50:00 / cg" |
|
5332 |
! |
|
5333 |
||
15734 | 5334 |
match:aString caseSensitive:caseSensitive |
5335 |
"return true if aString matches self, where self may contain GLOB meta-match |
|
5336 |
characters $* (to match any string) or $# (to match any character) |
|
5337 |
or [...] to match a set of characters. |
|
5338 |
If caseSensitive is false, lower/uppercase are considered the same. |
|
5339 |
The escape character is the backQuote. |
|
5340 |
||
5341 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20012 | 5342 |
NOT the ST-80 meaning. |
15734 | 5343 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
5344 |
NOTICE: the receiver is the match pattern" |
|
5345 |
||
5346 |
^ self match:aString from:1 to:aString size caseSensitive:caseSensitive |
|
5347 |
||
5348 |
" |
|
5349 |
'*ute*' match:'COMPUTER' caseSensitive:false |
|
5350 |
'*uter' match:'COMPUTER' caseSensitive:true |
|
5351 |
'[abcd]*' match:'computer' caseSensitive:true |
|
5352 |
'[abcd]*' match:'Computer' caseSensitive:true |
|
5353 |
'[a-k]*' match:'komputer' caseSensitive:true |
|
5354 |
'[a-k]*' match:'zomputer' caseSensitive:true |
|
5355 |
'[a-k]*' match:'Komputer' caseSensitive:true |
|
5356 |
'[a-k]*' match:'Komputer' caseSensitive:false |
|
5357 |
'*some*compl*ern*' match:'this is some more complicated pattern match' caseSensitive:false |
|
5358 |
'*some*compl*ern*' match:'this is another complicated pattern match' caseSensitive:false |
|
5359 |
||
5360 |
Time millisecondsToRun:[ |
|
20012 | 5361 |
Symbol allInstancesDo:[:sym | |
5362 |
'[ab]*' match:sym caseSensitive:true |
|
5363 |
] |
|
15734 | 5364 |
]. |
5365 |
Time millisecondsToRun:[ |
|
20012 | 5366 |
Symbol allInstancesDo:[:sym | |
5367 |
'*at:*' match:sym caseSensitive:true |
|
5368 |
] |
|
15734 | 5369 |
]. |
5370 |
" |
|
5371 |
||
5372 |
"Modified: 2.4.1997 / 17:28:58 / cg" |
|
5373 |
! |
|
5374 |
||
5375 |
match:aString caseSensitive:caseSensitive escapeCharacter:escape |
|
5376 |
"return true if aString matches self, where self may contain GLOB meta-match |
|
5377 |
characters $* (to match any string) or $# (to match any character) |
|
5378 |
or [...] to match a set of characters. |
|
5379 |
If caseSensitive is false, lower/uppercase are considered the same. |
|
5380 |
||
5381 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20012 | 5382 |
NOT the ST-80 meaning. |
15734 | 5383 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
5384 |
NOTICE: the receiver is the match pattern" |
|
5385 |
||
5386 |
^ self match:aString from:1 to:aString size caseSensitive:caseSensitive escapeCharacter:escape |
|
5387 |
||
5388 |
" |
|
5389 |
'*ute*' match:'COMPUTER' caseSensitive:false |
|
5390 |
'*uter' match:'COMPUTER' caseSensitive:true |
|
5391 |
'[abcd]*' match:'computer' caseSensitive:true |
|
5392 |
'[abcd]*' match:'Computer' caseSensitive:true |
|
5393 |
'[a-k]*' match:'komputer' caseSensitive:true |
|
5394 |
'[a-k]*' match:'zomputer' caseSensitive:true |
|
5395 |
'[a-k]*' match:'Komputer' caseSensitive:true |
|
5396 |
'[a-k]*' match:'Komputer' caseSensitive:false |
|
5397 |
'*some*compl*ern*' match:'this is some more complicated pattern match' caseSensitive:false |
|
5398 |
'*some*compl*ern*' match:'this is another complicated pattern match' caseSensitive:false |
|
5399 |
||
5400 |
Time millisecondsToRun:[ |
|
20012 | 5401 |
Symbol allInstancesDo:[:sym | |
5402 |
'[ab]*' match:sym caseSensitive:true |
|
5403 |
] |
|
15734 | 5404 |
]. |
5405 |
Time millisecondsToRun:[ |
|
20012 | 5406 |
Symbol allInstancesDo:[:sym | |
5407 |
'*at:*' match:sym caseSensitive:true |
|
5408 |
] |
|
15734 | 5409 |
]. |
5410 |
" |
|
5411 |
||
5412 |
"Modified: 2.4.1997 / 17:28:58 / cg" |
|
5413 |
! |
|
5414 |
||
12111 | 5415 |
match:aString escapeCharacter:escape |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5416 |
"return true if aString matches self, where self may contain GLOB meta-match |
12111 | 5417 |
characters $* (to match any string) or $# (to match any character). |
5418 |
or [...] to match a set of characters. |
|
5419 |
Lower/uppercase are considered different. |
|
5420 |
||
5421 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20012 | 5422 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5423 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 5424 |
NOTICE: the receiver is the match pattern" |
5425 |
||
19033 | 5426 |
^ self match:aString from:1 to:aString size caseSensitive:true escapeCharacter:escape |
12111 | 5427 |
|
5428 |
" |
|
5429 |
'a\b\c\*' match:'a\b\c\d' |
|
5430 |
'a\b\c\*' match:'a\b\c\d' escapeCharacter:nil |
|
5431 |
" |
|
5432 |
! |
|
5433 |
||
15734 | 5434 |
match:aString from:start to:stop caseSensitive:caseSensitive |
5435 |
"return true if part of aString matches myself, |
|
5436 |
where self may contain GLOB meta-match |
|
5437 |
characters $* (to match any string) or $# (to match any character) |
|
5438 |
or [...] to match a set of characters. |
|
5439 |
If caseSensitive is false, lower/uppercase are considered the same. |
|
5440 |
The escape character is the backQuote. |
|
5441 |
||
5442 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20012 | 5443 |
NOT the ST-80 meaning. |
15734 | 5444 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
5445 |
NOTICE: the receiver is the match pattern" |
|
5446 |
||
5447 |
^ self |
|
20012 | 5448 |
match:aString from:start to:stop caseSensitive:caseSensitive |
5449 |
escapeCharacter:(self class matchEscapeCharacter) |
|
15734 | 5450 |
|
5451 |
" |
|
5452 |
'*ute*' match:'12345COMPUTER' from:1 to:5 caseSensitive:false |
|
5453 |
'*ute*' match:'12345COMPUTER' from:6 to:13 caseSensitive:false |
|
5454 |
" |
|
5455 |
||
5456 |
"Modified: / 10.11.1998 / 21:43:46 / cg" |
|
5457 |
! |
|
5458 |
||
5459 |
match:aString from:start to:stop caseSensitive:caseSensitive escapeCharacter:escape |
|
5460 |
"return true if part of aString matches myself, |
|
5461 |
where self may contain GLOB meta-match |
|
5462 |
characters $* (to match any string) or $# (to match any character) |
|
5463 |
or [...] to match a set of characters. |
|
5464 |
If caseSensitive is false, lower/uppercase are considered the same. |
|
5465 |
||
5466 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20012 | 5467 |
NOT the ST-80 meaning. |
15734 | 5468 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
5469 |
NOTICE: the receiver is the match pattern" |
|
5470 |
||
5471 |
|matchScanArray| |
|
5472 |
||
5473 |
" |
|
5474 |
keep the matchScanArray from the most recent match - |
|
5475 |
avoids parsing the pattern over-and over if multiple searches |
|
5476 |
are done with the same pattern. |
|
5477 |
" |
|
5478 |
(PreviousMatch notNil |
|
5479 |
and:[PreviousMatch key = self]) ifTrue:[ |
|
20012 | 5480 |
matchScanArray := PreviousMatch value |
15734 | 5481 |
] ifFalse:[ |
20012 | 5482 |
matchScanArray := self class matchScanArrayFrom:self escapeCharacter:escape. |
5483 |
matchScanArray isNil ifTrue:[ |
|
5484 |
'CharacterArray [info]: invalid matchpattern:''' infoPrint. self infoPrint. ''' comparing for equality.' infoPrintCR. |
|
5485 |
^ self = aString |
|
15734 | 5486 |
"/ ^ false |
20012 | 5487 |
]. |
5488 |
PreviousMatch := self -> matchScanArray. |
|
15734 | 5489 |
]. |
5490 |
||
5491 |
^ self class |
|
20012 | 5492 |
matchScan:matchScanArray |
5493 |
from:1 to:matchScanArray size |
|
5494 |
with:aString |
|
5495 |
from:start to:stop |
|
5496 |
caseSensitive:caseSensitive |
|
15734 | 5497 |
|
5498 |
" |
|
5499 |
'*ute*' match:'12345COMPUTER' from:1 to:5 caseSensitive:false |
|
5500 |
'*ute*' match:'12345COMPUTER' from:6 to:13 caseSensitive:false |
|
5501 |
" |
|
5502 |
||
5503 |
"Modified: / 10.11.1998 / 21:43:46 / cg" |
|
5504 |
! |
|
5505 |
||
12111 | 5506 |
match:aString from:start to:stop ignoreCase:ignoreCase |
15734 | 5507 |
<resource: #obsolete> |
12111 | 5508 |
"return true if part of aString matches myself, |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5509 |
where self may contain GLOB meta-match |
12111 | 5510 |
characters $* (to match any string) or $# (to match any character) |
5511 |
or [...] to match a set of characters. |
|
5512 |
If ignoreCase is true, lower/uppercase are considered the same. |
|
5513 |
The escape character is the backQuote. |
|
5514 |
||
5515 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20012 | 5516 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5517 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 5518 |
NOTICE: the receiver is the match pattern" |
5519 |
||
5520 |
^ self |
|
20012 | 5521 |
match:aString from:start to:stop caseSensitive:ignoreCase not |
5522 |
escapeCharacter:(self class matchEscapeCharacter) |
|
12111 | 5523 |
|
5524 |
" |
|
5525 |
'*ute*' match:'12345COMPUTER' from:1 to:5 ignoreCase:true |
|
5526 |
'*ute*' match:'12345COMPUTER' from:6 to:13 ignoreCase:true |
|
5527 |
" |
|
5528 |
||
5529 |
"Modified: / 10.11.1998 / 21:43:46 / cg" |
|
5530 |
! |
|
5531 |
||
5532 |
match:aString from:start to:stop ignoreCase:ignoreCase escapeCharacter:escape |
|
15734 | 5533 |
<resource: #obsolete> |
12111 | 5534 |
"return true if part of aString matches myself, |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5535 |
where self may contain GLOB meta-match |
12111 | 5536 |
characters $* (to match any string) or $# (to match any character) |
5537 |
or [...] to match a set of characters. |
|
5538 |
If ignoreCase is true, lower/uppercase are considered the same. |
|
5539 |
||
5540 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20012 | 5541 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5542 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 5543 |
NOTICE: the receiver is the match pattern" |
5544 |
||
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
5545 |
^ self |
20012 | 5546 |
match:aString from:start to:stop |
5547 |
caseSensitive:ignoreCase not |
|
5548 |
escapeCharacter:escape |
|
12111 | 5549 |
|
5550 |
" |
|
5551 |
'*ute*' match:'12345COMPUTER' from:1 to:5 ignoreCase:true |
|
5552 |
'*ute*' match:'12345COMPUTER' from:6 to:13 ignoreCase:true |
|
5553 |
" |
|
5554 |
||
5555 |
"Modified: / 10.11.1998 / 21:43:46 / cg" |
|
5556 |
! |
|
5557 |
||
5558 |
match:aString ignoreCase:ignoreCase |
|
15734 | 5559 |
<resource: #obsolete> |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5560 |
"return true if aString matches self, where self may contain GLOB meta-match |
12111 | 5561 |
characters $* (to match any string) or $# (to match any character) |
5562 |
or [...] to match a set of characters. |
|
5563 |
If ignoreCase is true, lower/uppercase are considered the same. |
|
5564 |
The escape character is the backQuote. |
|
5565 |
||
5566 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20012 | 5567 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5568 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 5569 |
NOTICE: the receiver is the match pattern" |
5570 |
||
15734 | 5571 |
"/ leave it this way, in case a customer has redefined it for performance (for a while) |
19033 | 5572 |
^ self match:aString from:1 to:aString size caseSensitive:ignoreCase not |
12111 | 5573 |
|
5574 |
" |
|
5575 |
'*ute*' match:'COMPUTER' ignoreCase:true |
|
5576 |
'*uter' match:'COMPUTER' ignoreCase:false |
|
5577 |
'[abcd]*' match:'computer' ignoreCase:false |
|
5578 |
'[abcd]*' match:'Computer' ignoreCase:false |
|
5579 |
'[a-k]*' match:'komputer' ignoreCase:false |
|
5580 |
'[a-k]*' match:'zomputer' ignoreCase:false |
|
5581 |
'[a-k]*' match:'Komputer' ignoreCase:false |
|
5582 |
'[a-k]*' match:'Komputer' ignoreCase:true |
|
5583 |
'*some*compl*ern*' match:'this is some more complicated pattern match' ignoreCase:true |
|
5584 |
'*some*compl*ern*' match:'this is another complicated pattern match' ignoreCase:true |
|
5585 |
||
5586 |
Time millisecondsToRun:[ |
|
20012 | 5587 |
Symbol allInstancesDo:[:sym | |
5588 |
'[ab]*' match:sym ignoreCase:false |
|
5589 |
] |
|
12111 | 5590 |
]. |
5591 |
Time millisecondsToRun:[ |
|
20012 | 5592 |
Symbol allInstancesDo:[:sym | |
5593 |
'*at:*' match:sym ignoreCase:false |
|
5594 |
] |
|
12111 | 5595 |
]. |
5596 |
" |
|
5597 |
||
5598 |
"Modified: 2.4.1997 / 17:28:58 / cg" |
|
5599 |
! |
|
5600 |
||
5601 |
match:aString ignoreCase:ignoreCase escapeCharacter:escape |
|
15734 | 5602 |
<resource: #obsolete> |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5603 |
"return true if aString matches self, where self may contain GLOB meta-match |
12111 | 5604 |
characters $* (to match any string) or $# (to match any character) |
5605 |
or [...] to match a set of characters. |
|
5606 |
If ignoreCase is true, lower/uppercase are considered the same. |
|
5607 |
||
5608 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20012 | 5609 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5610 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 5611 |
NOTICE: the receiver is the match pattern" |
5612 |
||
19033 | 5613 |
^ self match:aString from:1 to:aString size caseSensitive:ignoreCase not escapeCharacter:escape |
12111 | 5614 |
|
5615 |
" |
|
5616 |
'*ute*' match:'COMPUTER' ignoreCase:true |
|
5617 |
'*uter' match:'COMPUTER' ignoreCase:false |
|
5618 |
'[abcd]*' match:'computer' ignoreCase:false |
|
5619 |
'[abcd]*' match:'Computer' ignoreCase:false |
|
5620 |
'[a-k]*' match:'komputer' ignoreCase:false |
|
5621 |
'[a-k]*' match:'zomputer' ignoreCase:false |
|
5622 |
'[a-k]*' match:'Komputer' ignoreCase:false |
|
5623 |
'[a-k]*' match:'Komputer' ignoreCase:true |
|
5624 |
'*some*compl*ern*' match:'this is some more complicated pattern match' ignoreCase:true |
|
5625 |
'*some*compl*ern*' match:'this is another complicated pattern match' ignoreCase:true |
|
5626 |
||
5627 |
Time millisecondsToRun:[ |
|
20012 | 5628 |
Symbol allInstancesDo:[:sym | |
5629 |
'[ab]*' match:sym ignoreCase:false |
|
5630 |
] |
|
12111 | 5631 |
]. |
5632 |
Time millisecondsToRun:[ |
|
20012 | 5633 |
Symbol allInstancesDo:[:sym | |
5634 |
'*at:*' match:sym ignoreCase:false |
|
5635 |
] |
|
12111 | 5636 |
]. |
5637 |
" |
|
5638 |
||
5639 |
"Modified: 2.4.1997 / 17:28:58 / cg" |
|
5640 |
! |
|
5641 |
||
5642 |
matches:aPatternString |
|
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5643 |
"return true if the receiver matches aString, where aPatternString may contain GLOB meta-match |
12111 | 5644 |
characters $* (to match any string) or $# (to match any character). |
5645 |
or [...] to match a set of characters. |
|
5646 |
Lower/uppercase are considered different. |
|
5647 |
||
5648 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20012 | 5649 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5650 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 5651 |
NOTICE: the receiver is the match pattern" |
5652 |
||
5653 |
^ aPatternString match:self |
|
14054
48d3fab77bd9
added: #matches:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
14053
diff
changeset
|
5654 |
! |
48d3fab77bd9
added: #matches:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
14053
diff
changeset
|
5655 |
|
15734 | 5656 |
matches:aPatternString caseSensitive:caseSensitive |
5657 |
"return true if the receiver matches aString, where aPatternString may contain GLOB meta-match |
|
5658 |
characters $* (to match any string) or $# (to match any character). |
|
5659 |
or [...] to match a set of characters. |
|
5660 |
||
5661 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20012 | 5662 |
NOT the ST-80 meaning. |
15734 | 5663 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
5664 |
NOTICE: the receiver is the match pattern" |
|
5665 |
||
5666 |
^ aPatternString match:self caseSensitive:caseSensitive |
|
5667 |
! |
|
5668 |
||
14054
48d3fab77bd9
added: #matches:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
14053
diff
changeset
|
5669 |
matches:aPatternString ignoreCase:ignoreCase |
15734 | 5670 |
<resource: #obsolete> |
14054
48d3fab77bd9
added: #matches:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
14053
diff
changeset
|
5671 |
"return true if the receiver matches aString, where aPatternString may contain GLOB meta-match |
48d3fab77bd9
added: #matches:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
14053
diff
changeset
|
5672 |
characters $* (to match any string) or $# (to match any character). |
48d3fab77bd9
added: #matches:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
14053
diff
changeset
|
5673 |
or [...] to match a set of characters. |
48d3fab77bd9
added: #matches:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
14053
diff
changeset
|
5674 |
Lower/uppercase are considered different. |
48d3fab77bd9
added: #matches:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
14053
diff
changeset
|
5675 |
|
48d3fab77bd9
added: #matches:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
14053
diff
changeset
|
5676 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
20012 | 5677 |
NOT the ST-80 meaning. |
14054
48d3fab77bd9
added: #matches:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
14053
diff
changeset
|
5678 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
48d3fab77bd9
added: #matches:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
14053
diff
changeset
|
5679 |
NOTICE: the receiver is the match pattern" |
48d3fab77bd9
added: #matches:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
14053
diff
changeset
|
5680 |
|
15734 | 5681 |
^ aPatternString match:self caseSensitive:ignoreCase not |
14054
48d3fab77bd9
added: #matches:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
14053
diff
changeset
|
5682 |
|
48d3fab77bd9
added: #matches:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
14053
diff
changeset
|
5683 |
"Created: / 08-03-2012 / 03:11:11 / cg" |
12111 | 5684 |
! ! |
5685 |
||
5686 |
||
18722 | 5687 |
!CharacterArray methodsFor:'matching - regex'! |
5688 |
||
5689 |
matchesRegex:regexString caseSensitive:aBoolean |
|
5690 |
"Test if the receiver matches a regex. |
|
5691 |
May raise RxParser>>regexErrorSignal or child signals. |
|
5692 |
This is a part of the Regular Expression Matcher package, |
|
20012 | 5693 |
(c) 1996, 1999 Vassili Bykov. |
18722 | 5694 |
Refer to `documentation' protocol of RxParser class for details." |
5695 |
||
5696 |
aBoolean ifFalse:[ |
|
20012 | 5697 |
^ self matchesRegexIgnoringCase:regexString |
18722 | 5698 |
] ifTrue:[ |
20012 | 5699 |
^ self matchesRegex:regexString |
18722 | 5700 |
]. |
5701 |
||
5702 |
" |
|
5703 |
'hello world' matchesRegex:'h.*d'. |
|
5704 |
" |
|
5705 |
||
5706 |
"Created: / 13-12-2010 / 11:01:49 / cg" |
|
5707 |
! ! |
|
17625 | 5708 |
|
9192 | 5709 |
!CharacterArray methodsFor:'padded copying'! |
5710 |
||
5711 |
centerPaddedTo:newSize |
|
19464 | 5712 |
"return a new string consisting of the receiver's characters, |
5713 |
plus spaces up to length and center the receiver's characters in |
|
5714 |
the resulting string. |
|
5715 |
If the receiver's size is equal or greater than the length argument, |
|
5716 |
the original receiver is returned unchanged." |
|
9192 | 5717 |
|
5718 |
^ self centerPaddedTo:newSize with:(Character space) |
|
5719 |
||
5720 |
" |
|
5721 |
'foo' centerPaddedTo:10 |
|
5722 |
123 printString centerPaddedTo:10 |
|
5723 |
" |
|
5724 |
||
5725 |
"Created: 25.11.1995 / 10:53:57 / cg" |
|
5726 |
! |
|
9190 | 5727 |
|
5728 |
centerPaddedTo:size with:padCharacter |
|
5729 |
"return a new string of length size, which contains the receiver |
|
5730 |
centered (i.e. padded on both sides). |
|
5731 |
Characters are filled with padCharacter. |
|
19464 | 5732 |
If the receiver's size is equal or greater than the length argument, |
815
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
5733 |
the original receiver is returned unchanged." |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
5734 |
|
9190 | 5735 |
|len s| |
5736 |
||
5737 |
len := self size. |
|
9192 | 5738 |
(len < size) ifTrue:[ |
20012 | 5739 |
s := self species new:size withAll:padCharacter. |
5740 |
s replaceFrom:(size - len) // 2 + 1 with:self. |
|
5741 |
^ s |
|
9192 | 5742 |
] |
5743 |
||
9190 | 5744 |
" |
5745 |
'foo' centerPaddedTo:11 with:$. |
|
5746 |
'fooBar' centerPaddedTo:5 with:$. |
|
5747 |
123 printString centerPaddedTo:10 with:$. |
|
5748 |
(' ' , 123 printString) centerPaddedTo:10 with:$. |
|
5749 |
(Float pi printString) centerPaddedTo:15 with:(Character space) |
|
5750 |
(Float pi printString) centerPaddedTo:15 with:$- |
|
5751 |
(' ' , Float pi class name) centerPaddedTo:15 with:$. |
|
5752 |
" |
|
815
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
5753 |
! |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
5754 |
|
9190 | 5755 |
decimalPaddedTo:size and:afterPeriod at:decimalCharacter |
815
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
5756 |
"return a new string of overall length size, which contains the receiver |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
5757 |
aligned at the decimal-period column and afterPeriod characters to the right |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
5758 |
of the period. The periodCharacter is passed as arguments (allowing for US and European formats |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
5759 |
to be padded). |
19464 | 5760 |
If the receiver's size is equal or greater than the length argument, |
815
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
5761 |
the original receiver is returned unchanged. |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
5762 |
(sounds complicated ? -> see examples below)." |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
5763 |
|
8874 | 5764 |
^ self |
20012 | 5765 |
decimalPaddedTo:size |
5766 |
and:afterPeriod |
|
5767 |
at:decimalCharacter |
|
5768 |
withLeft:(Character space) |
|
5769 |
right:$0 |
|
815
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
5770 |
|
8874 | 5771 |
" |
5772 |
'123' decimalPaddedTo:10 and:3 at:$. -> ' 123 ' |
|
5773 |
'123' decimalPaddedTo:10 and:3 at:$. -> ' 123.000' |
|
5774 |
'123.' decimalPaddedTo:10 and:3 at:$. -> ' 123.000' |
|
815
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
5775 |
'123.1' decimalPaddedTo:10 and:3 at:$. -> ' 123.100' |
8874 | 5776 |
'123.1' decimalPaddedTo:10 and:3 at:$. -> ' 123.1 ' |
815
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
5777 |
'123.123' decimalPaddedTo:10 and:3 at:$. -> ' 123.123' |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
5778 |
" |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
5779 |
|
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
5780 |
"Created: 23.12.1995 / 13:11:52 / cg" |
9192 | 5781 |
! |
5782 |
||
5783 |
decimalPaddedTo:size and:afterPeriod at:decimalCharacter withLeft:leftPadChar right:rightPadChar |
|
815
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
5784 |
"return a new string of overall length size, which contains the receiver |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
5785 |
aligned at the decimal-period column and afterPeriod characters to the right |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
5786 |
of the period. |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
5787 |
Characters on the left are filled with leftPadChar. |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
5788 |
If rightPadChar is nil, characters on the right are filled with leftPadCharacter too; |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
5789 |
otherwise, if missing, a decimal point is added and right characters filled with this. |
19464 | 5790 |
If the receiver's size is equal or greater than the length argument, |
815
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
5791 |
the original receiver is returned unchanged. |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
5792 |
(sounds complicated ? -> see examples below)." |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
5793 |
|
5230
655c21ffe6cc
care for negative size of rest-string when decimalPadding
Claus Gittinger <cg@exept.de>
parents:
5221
diff
changeset
|
5794 |
|s idx n rest| |
815
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
5795 |
|
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
5796 |
idx := self indexOf:decimalCharacter. |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
5797 |
idx == 0 ifTrue:[ |
20012 | 5798 |
"/ |
5799 |
"/ no decimal point found; adjust string to the left of the period column |
|
5800 |
"/ |
|
5801 |
rightPadChar isNil ifTrue:[ |
|
5802 |
s := self , (self species new:afterPeriod + 1 withAll:leftPadChar) |
|
5803 |
] ifFalse:[ |
|
5804 |
s:= self , decimalCharacter asString , (self species new:afterPeriod withAll:rightPadChar). |
|
5805 |
]. |
|
815
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
5806 |
] ifFalse:[ |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
5807 |
|
20012 | 5808 |
"/ the number of after-decimalPoint characters |
5809 |
n := self size - idx. |
|
5810 |
rest := afterPeriod - n. |
|
5811 |
rest > 0 ifTrue:[ |
|
5812 |
s := (self species new:rest withAll:(rightPadChar ? leftPadChar)). |
|
5813 |
] ifFalse:[ |
|
5814 |
s := '' |
|
5815 |
]. |
|
5816 |
s := self , s. |
|
815
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
5817 |
]. |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
5818 |
|
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
5819 |
^ s leftPaddedTo:size with:leftPadChar |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
5820 |
|
8874 | 5821 |
" |
5822 |
'123' decimalPaddedTo:10 and:3 at:$. withLeft:(Character space) right:nil -> ' 123 ' |
|
5823 |
'123' decimalPaddedTo:10 and:3 at:$. withLeft:(Character space) right:$0 -> ' 123.000' |
|
5824 |
'123.' decimalPaddedTo:10 and:3 at:$. withLeft:(Character space) right:$0 -> ' 123.000' |
|
815
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
5825 |
'123.1' decimalPaddedTo:10 and:3 at:$. withLeft:(Character space) right:$0 -> ' 123.100' |
8874 | 5826 |
'123.1' decimalPaddedTo:10 and:3 at:$. withLeft:(Character space) right:nil -> ' 123.1 ' |
815
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
5827 |
'123.123' decimalPaddedTo:10 and:3 at:$. withLeft:(Character space) right:$0 -> ' 123.123' |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
5828 |
" |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
5829 |
|
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
5830 |
"Modified: 23.12.1995 / 13:08:18 / cg" |
9192 | 5831 |
! |
5832 |
||
5833 |
leftPaddedTo:size |
|
5834 |
"return a new string of length size, which contains the receiver |
|
5835 |
right-adjusted (i.e. padded on the left). |
|
5836 |
Characters on the left are filled with spaces. |
|
19464 | 5837 |
If the receiver's size is equal or greater than the length argument, |
9192 | 5838 |
the original receiver is returned unchanged." |
5839 |
||
5840 |
^ self leftPaddedTo:size with:(Character space) |
|
5841 |
||
5842 |
" |
|
5843 |
'foo' leftPaddedTo:10 |
|
5844 |
'fooBar' leftPaddedTo:5 |
|
5845 |
123 printString leftPaddedTo:10 |
|
5846 |
" |
|
5847 |
! |
|
608 | 5848 |
|
5849 |
paddedTo:newSize |
|
19753 | 5850 |
"return a new string consisting of the receiver's characters, |
608 | 5851 |
plus spaces up to length. |
19464 | 5852 |
If the receiver's size is equal or greater than the length argument, |
608 | 5853 |
the original receiver is returned unchanged." |
5854 |
||
5855 |
^ self paddedTo:newSize with:(Character space) |
|
5856 |
||
5857 |
" |
|
8874 | 5858 |
'foo' paddedTo:10 |
5859 |
123 printString paddedTo:10 |
|
15734 | 5860 |
'12345678901234' paddedTo:10 |
608 | 5861 |
" |
64 | 5862 |
! ! |
5863 |
||
9192 | 5864 |
!CharacterArray methodsFor:'printing & storing'! |
608 | 5865 |
|
5866 |
article |
|
17137 | 5867 |
"return an article string for the receiver. |
5868 |
The original code only looked at the first character being a vowel; |
|
5869 |
this has been enhanced by some heuristics - not perfect, still." |
|
608 | 5870 |
|
17241 | 5871 |
|firstChar secondChar thirdChar| |
608 | 5872 |
|
8874 | 5873 |
firstChar := (self at:1) asLowercase. |
5848 | 5874 |
((firstChar isVowel and:[firstChar ~~ $u]) or:[firstChar == $x]) ifTrue:[ |
20012 | 5875 |
^ 'an' |
608 | 5876 |
]. |
17043 | 5877 |
|
5878 |
(self size >= 3) ifTrue:[ |
|
20012 | 5879 |
secondChar := (self at:2) asLowercase. |
5880 |
||
5881 |
"/ may need more here... |
|
5882 |
( #('rb') includes:(String with:firstChar with:secondChar)) ifTrue:[ |
|
5883 |
^ 'an' |
|
5884 |
]. |
|
5885 |
||
5886 |
thirdChar := (self at:3) asLowercase. |
|
5887 |
||
5888 |
(firstChar isVowel not |
|
5889 |
and:[(secondChar isVowel or:[secondChar == $y]) not |
|
5890 |
and:[thirdChar isVowel not ]]) ifTrue:[ |
|
5891 |
"/ exceptions: 3 non-vowels in a row: looks like an abbreviation |
|
5892 |
(self size > 4) ifTrue:[ |
|
5893 |
(firstChar == $s) ifTrue:[ |
|
5894 |
((secondChar == $c and:[thirdChar == $r]) |
|
5895 |
or:[ (secondChar == $t and:[thirdChar == $r]) ]) ifTrue:[ |
|
5896 |
(self at:4) isVowel ifTrue:[ |
|
5897 |
^ 'a' |
|
5898 |
] |
|
5899 |
] |
|
5900 |
]. |
|
5901 |
]. |
|
5902 |
"/ an abbreviation; treat x, s as vowels |
|
5903 |
(firstChar == $x or:[ firstChar == $s ]) ifTrue:[^ 'an']. |
|
5904 |
] |
|
17043 | 5905 |
]. |
608 | 5906 |
^ 'a' |
5848 | 5907 |
|
5908 |
" |
|
20012 | 5909 |
'uboot' article. |
5910 |
'xmas' article. |
|
5911 |
'alarm' article. |
|
5912 |
'baby' article. |
|
5913 |
'sql' article. |
|
5914 |
'scr' article. |
|
5915 |
'screen' article. |
|
5916 |
'scrollbar' article. |
|
5917 |
'scrs' article. |
|
5918 |
'cvs' article. |
|
5919 |
'cvssource' article. |
|
5920 |
'symbol' article. |
|
5921 |
'string' article. |
|
5922 |
'rbparser' article. |
|
19683 | 5923 |
" |
5924 |
||
5925 |
"Modified (comment): / 01-05-2016 / 10:57:25 / cg" |
|
608 | 5926 |
! |
5927 |
||
8049
ccd0e8f26439
printing - migrating towards unicodeSupport in ST-code
Claus Gittinger <cg@exept.de>
parents:
8040
diff
changeset
|
5928 |
basicStoreString |
ccd0e8f26439
printing - migrating towards unicodeSupport in ST-code
Claus Gittinger <cg@exept.de>
parents:
8040
diff
changeset
|
5929 |
"return a String for storing myself" |
ccd0e8f26439
printing - migrating towards unicodeSupport in ST-code
Claus Gittinger <cg@exept.de>
parents:
8040
diff
changeset
|
5930 |
|
ccd0e8f26439
printing - migrating towards unicodeSupport in ST-code
Claus Gittinger <cg@exept.de>
parents:
8040
diff
changeset
|
5931 |
|s n index| |
ccd0e8f26439
printing - migrating towards unicodeSupport in ST-code
Claus Gittinger <cg@exept.de>
parents:
8040
diff
changeset
|
5932 |
|
ccd0e8f26439
printing - migrating towards unicodeSupport in ST-code
Claus Gittinger <cg@exept.de>
parents:
8040
diff
changeset
|
5933 |
n := self occurrencesOf:$'. |
11320 | 5934 |
n ~~ 0 ifTrue:[ |
20012 | 5935 |
s := self species new:(n + 2 + self size). |
5936 |
s at:1 put:$'. |
|
5937 |
index := 2. |
|
5938 |
self do:[:thisChar | |
|
5939 |
(thisChar == $') ifTrue:[ |
|
5940 |
s at:index put:thisChar. |
|
5941 |
index := index + 1. |
|
5942 |
]. |
|
5943 |
s at:index put:thisChar. |
|
5944 |
index := index + 1. |
|
5945 |
]. |
|
5946 |
s at:index put:$'. |
|
5947 |
^ s |
|
9192 | 5948 |
]. |
11320 | 5949 |
|
9192 | 5950 |
^ '''' , self , '''' |
15523 | 5951 |
|
5952 |
" |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
5953 |
'''immutable'' string' asImmutableString basicStoreString |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
5954 |
'immutable string' asImmutableString basicStoreString |
15523 | 5955 |
" |
5956 |
||
5957 |
"Modified: / 14-07-2013 / 19:20:49 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
9192 | 5958 |
! |
8049
ccd0e8f26439
printing - migrating towards unicodeSupport in ST-code
Claus Gittinger <cg@exept.de>
parents:
8040
diff
changeset
|
5959 |
|
11774
6f6e2b71b607
displayOn (inspector's interface) was broken
Claus Gittinger <cg@exept.de>
parents:
11754
diff
changeset
|
5960 |
displayOn:aGCOrStream |
6f6e2b71b607
displayOn (inspector's interface) was broken
Claus Gittinger <cg@exept.de>
parents:
11754
diff
changeset
|
5961 |
"Compatibility |
6f6e2b71b607
displayOn (inspector's interface) was broken
Claus Gittinger <cg@exept.de>
parents:
11754
diff
changeset
|
5962 |
append a printed desription on some stream (Dolphin, Squeak) |
6f6e2b71b607
displayOn (inspector's interface) was broken
Claus Gittinger <cg@exept.de>
parents:
11754
diff
changeset
|
5963 |
OR: |
6f6e2b71b607
displayOn (inspector's interface) was broken
Claus Gittinger <cg@exept.de>
parents:
11754
diff
changeset
|
5964 |
display the receiver in a graphicsContext at 0@0 (ST80). |
6f6e2b71b607
displayOn (inspector's interface) was broken
Claus Gittinger <cg@exept.de>
parents:
11754
diff
changeset
|
5965 |
This method allows for any object to be displayed in some view |
6f6e2b71b607
displayOn (inspector's interface) was broken
Claus Gittinger <cg@exept.de>
parents:
11754
diff
changeset
|
5966 |
(although the fallBack is to display its printString ...)" |
6f6e2b71b607
displayOn (inspector's interface) was broken
Claus Gittinger <cg@exept.de>
parents:
11754
diff
changeset
|
5967 |
|
6f6e2b71b607
displayOn (inspector's interface) was broken
Claus Gittinger <cg@exept.de>
parents:
11754
diff
changeset
|
5968 |
"/ what a kludge - Dolphin and Squeak mean: printOn: a stream; |
6f6e2b71b607
displayOn (inspector's interface) was broken
Claus Gittinger <cg@exept.de>
parents:
11754
diff
changeset
|
5969 |
"/ ST/X (and some old ST80's) mean: draw-yourself on a GC. |
16738 | 5970 |
(aGCOrStream isStream) ifTrue:[ |
20012 | 5971 |
self storeOn:aGCOrStream. |
5972 |
^ self |
|
11774
6f6e2b71b607
displayOn (inspector's interface) was broken
Claus Gittinger <cg@exept.de>
parents:
11754
diff
changeset
|
5973 |
]. |
6f6e2b71b607
displayOn (inspector's interface) was broken
Claus Gittinger <cg@exept.de>
parents:
11754
diff
changeset
|
5974 |
^ super displayOn:aGCOrStream |
6f6e2b71b607
displayOn (inspector's interface) was broken
Claus Gittinger <cg@exept.de>
parents:
11754
diff
changeset
|
5975 |
! |
6f6e2b71b607
displayOn (inspector's interface) was broken
Claus Gittinger <cg@exept.de>
parents:
11754
diff
changeset
|
5976 |
|
608 | 5977 |
printOn:aStream |
5978 |
"print the receiver on aStream" |
|
5979 |
||
5980 |
aStream nextPutAll:self |
|
5981 |
! |
|
5982 |
||
5983 |
printString |
|
13079
111e7e456648
Implement display with #displayOn: instead of #displayString
Stefan Vogel <sv@exept.de>
parents:
13073
diff
changeset
|
5984 |
"return a string for printing - that's myself" |
608 | 5985 |
|
5986 |
^ self |
|
3578 | 5987 |
! |
5988 |
||
5989 |
printWithQuotesDoubledOn:aStream |
|
5990 |
"put the raw storeString of myself on aStream" |
|
5991 |
||
5992 |
self do:[:thisChar | |
|
20012 | 5993 |
(thisChar == $') ifTrue:[aStream nextPut:thisChar]. |
5994 |
aStream nextPut:thisChar |
|
3578 | 5995 |
] |
5996 |
||
5997 |
"Modified: / 15.6.1998 / 17:21:17 / cg" |
|
5998 |
"Created: / 15.6.1998 / 17:22:13 / cg" |
|
12171 | 5999 |
! |
6000 |
||
15788 | 6001 |
printXmlQuotedOn:aStream |
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
6002 |
"convert aString to a valid XML string |
15788 | 6003 |
that can be used for attributes, text, comments an PIs |
6004 |
TODO: care for 16bit UNICODE string and escape chars ..." |
|
6005 |
||
6006 |
self do:[:eachChar | |
|
20012 | 6007 |
eachChar == $< ifTrue:[ |
6008 |
aStream nextPutAll:'<' "mapping needed for xml text" |
|
6009 |
] ifFalse:[ eachChar == $& ifTrue:[ |
|
6010 |
aStream nextPutAll:'&' "mapping needed for all" |
|
6011 |
] ifFalse:[ eachChar == $> ifTrue:[ |
|
6012 |
aStream nextPutAll:'>' "mapping needed for comments" |
|
6013 |
] ifFalse:[ eachChar == $' ifTrue:[ |
|
6014 |
aStream nextPutAll:''' "mapping needed for attributes" |
|
6015 |
] ifFalse:[ eachChar == $" ifTrue:[ |
|
6016 |
aStream nextPutAll:'"' "mapping needed for attributes" |
|
6017 |
] ifFalse:[ |
|
6018 |
|codePoint| |
|
6019 |
codePoint := eachChar codePoint. |
|
6020 |
(codePoint < 16r20 or:[codePoint >= 16r7F]) ifTrue:[ |
|
6021 |
aStream nextPutAll:'&#'. |
|
6022 |
codePoint printOn:aStream. |
|
6023 |
aStream nextPut:$;. |
|
6024 |
] ifFalse:[ |
|
6025 |
aStream nextPut:eachChar |
|
6026 |
]]]]]] |
|
15788 | 6027 |
]. |
6028 |
! |
|
6029 |
||
6030 |
printXmlTextQuotedOn:aStream |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
6031 |
"convert aString to a valid XML string |
15788 | 6032 |
that can be used for XML text. |
6033 |
Here line formatting characters are not escaped. |
|
6034 |
TODO: care for 16bit UNICODE string and escape chars ..." |
|
6035 |
||
6036 |
self do:[:eachChar | |
|
20012 | 6037 |
eachChar == $< ifTrue:[ |
6038 |
aStream nextPutAll:'<' "mapping needed for xml text" |
|
6039 |
] ifFalse:[ eachChar == $& ifTrue:[ |
|
6040 |
aStream nextPutAll:'&' "mapping needed for all" |
|
15788 | 6041 |
"/ ] ifFalse:[ eachChar == $> ifTrue:[ |
6042 |
"/ aStream nextPutAll:'>' "mapping needed for comments" |
|
6043 |
"/ ] ifFalse:[ eachChar == $' ifTrue:[ |
|
6044 |
"/ aStream nextPutAll:''' "mapping needed for attributes" |
|
6045 |
"/ ] ifFalse:[ eachChar == $" ifTrue:[ |
|
6046 |
"/ aStream nextPutAll:'"' "mapping needed for attributes" |
|
20012 | 6047 |
] ifFalse:[ |
6048 |
|codePoint| |
|
6049 |
codePoint := eachChar codePoint. |
|
6050 |
((codePoint < 16r20 and:[codePoint ~~ 9 and:[codePoint ~~ 10 and:[codePoint ~~ 13]]]) |
|
6051 |
or:[codePoint >= 16r7F]) ifTrue:[ |
|
6052 |
aStream nextPutAll:'&#'. |
|
6053 |
codePoint printOn:aStream. |
|
6054 |
aStream nextPut:$;. |
|
6055 |
] ifFalse:[ |
|
6056 |
aStream nextPut:eachChar |
|
6057 |
]]]"/]]] |
|
15788 | 6058 |
]. |
6059 |
! |
|
6060 |
||
12171 | 6061 |
printfWith:arg1 with:arg2 with:arg3 with:arg4 with:arg5 |
12177 | 6062 |
"Format and print the receiver with <argI> formatted in C style, |
12171 | 6063 |
as specified in the Unix C-language manual page for printf(3). |
15712 | 6064 |
Return the resulting string (i.e actually, this is more like an sprintf)." |
12177 | 6065 |
|
12171 | 6066 |
^ self printf:(Array with:arg1 with:arg2 with:arg3 with:arg4 with:arg5) |
6067 |
||
6068 |
" |
|
12177 | 6069 |
Transcript showCR:('%d %05x %08o %b' printfWith:123 with:234 with:345 with:123) |
12171 | 6070 |
" |
15788 | 6071 |
! |
6072 |
||
6073 |
xmlQuotedPrintString |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
6074 |
"convert aString to a valid XML string |
15788 | 6075 |
that can be used for attributes, text, comments an PIs |
6076 |
TODO: care for 16bit UNICODE string and escape chars ..." |
|
6077 |
||
6078 |
^ String streamContents:[:s| |
|
20012 | 6079 |
self printXmlQuotedOn:s |
15788 | 6080 |
]. |
9192 | 6081 |
! ! |
6082 |
||
11955 | 6083 |
|
9192 | 6084 |
!CharacterArray methodsFor:'queries'! |
608 | 6085 |
|
4429 | 6086 |
bitsPerCharacter |
18662 | 6087 |
"return the underlying string's bitsPerCharacter |
4429 | 6088 |
(i.e. is it a regular String or a TwoByteString)" |
6089 |
||
8457 | 6090 |
|string max| |
6091 |
||
6092 |
(string := self string) ~~ self ifTrue:[ |
|
20012 | 6093 |
^ string bitsPerCharacter |
8457 | 6094 |
]. |
6095 |
||
6096 |
max := 8. |
|
6097 |
self do:[:eachCharacter | |
|
20012 | 6098 |
max := max max:(eachCharacter bitsPerCharacter) |
9192 | 6099 |
]. |
6100 |
^ max |
|
6101 |
||
6102 |
" |
|
6103 |
'hello' bitsPerCharacter |
|
6104 |
'hello' asText allBold bitsPerCharacter |
|
6105 |
" |
|
6106 |
! |
|
8457 | 6107 |
|
19757 | 6108 |
bytesPerCharacter |
6109 |
"return the underlying string's required |
|
6110 |
bytesPerCharacter |
|
6111 |
(i.e. is it a regular String or a TwoByteString)" |
|
6112 |
||
6113 |
|string max| |
|
6114 |
||
6115 |
(string := self string) ~~ self ifTrue:[ |
|
20012 | 6116 |
^ string bytesPerCharacter |
19757 | 6117 |
]. |
6118 |
||
6119 |
max := 1. |
|
6120 |
self do:[:eachCharacter | |
|
20012 | 6121 |
max := max max:(eachCharacter bytesPerCharacter) |
19757 | 6122 |
]. |
6123 |
^ max |
|
6124 |
||
6125 |
" |
|
6126 |
'hello' bytesPerCharacter |
|
6127 |
'hello' asUnicode16String bytesPerCharacter |
|
6128 |
'hello' asText allBold bytesPerCharacter |
|
6129 |
" |
|
6130 |
! |
|
6131 |
||
18967 | 6132 |
containsNon8BitElements |
6133 |
"return true, if the underlying string contains elements larger than a single byte" |
|
6134 |
||
6135 |
|sz "{ Class:SmallInteger }"| |
|
6136 |
||
6137 |
sz := self size. |
|
6138 |
1 to:sz do:[:idx| |
|
20012 | 6139 |
(self at:idx) codePoint > 16rFF ifTrue:[ |
6140 |
^ true. |
|
6141 |
]. |
|
18967 | 6142 |
]. |
6143 |
^ false. |
|
6144 |
! |
|
6145 |
||
10903 | 6146 |
continuesWith:aString startingAt:startIndex |
6147 |
"return true, if the receiver beginning at startIndex |
|
6148 |
contains the characters in aString." |
|
6149 |
||
6150 |
|sz "{Class: SmallInteger }" |
|
6151 |
idx "{Class: SmallInteger }"| |
|
6152 |
||
6153 |
sz := aString size. |
|
6154 |
idx := startIndex. |
|
6155 |
||
6156 |
1 to:sz do:[:i | |
|
20012 | 6157 |
(self at:idx) ~~ (aString at:i) ifTrue:[^ false]. |
6158 |
idx := idx + 1 |
|
10903 | 6159 |
]. |
6160 |
^ true |
|
6161 |
||
6162 |
" |
|
6163 |
'hello world' continuesWith:'world' startingAt:6 |
|
6164 |
'hello world' continuesWith:'world' startingAt:7 |
|
6165 |
" |
|
6166 |
||
6167 |
"Created: 12.5.1996 / 15:46:40 / cg" |
|
6168 |
"Modified: 26.7.1996 / 19:08:36 / cg" |
|
6169 |
! |
|
6170 |
||
6171 |
countWords |
|
6172 |
"return the number of words, which are separated by separators" |
|
6173 |
||
6174 |
|tally "{ Class: SmallInteger }" |
|
6175 |
start "{ Class: SmallInteger }" |
|
6176 |
mySize "{ Class: SmallInteger }" |
|
6177 |
stop ch| |
|
6178 |
||
6179 |
tally := 0. |
|
6180 |
start := 1. |
|
6181 |
mySize := self size. |
|
6182 |
[start <= mySize] whileTrue:[ |
|
20012 | 6183 |
ch := self at:start. |
6184 |
ch isSeparator ifTrue:[ |
|
6185 |
start := start + 1 |
|
6186 |
] ifFalse:[ |
|
6187 |
stop := self indexOfSeparatorStartingAt:start. |
|
6188 |
(stop == 0) ifTrue:[ |
|
6189 |
stop := mySize + 1 |
|
6190 |
]. |
|
6191 |
tally := tally + 1. |
|
6192 |
start := stop |
|
6193 |
] |
|
10903 | 6194 |
]. |
6195 |
^ tally |
|
6196 |
||
6197 |
" |
|
6198 |
'hello world isnt this nice' countWords' |
|
6199 |
" |
|
6200 |
! |
|
6201 |
||
11788 | 6202 |
defaultElement |
6203 |
^ Character space |
|
6204 |
! |
|
6205 |
||
608 | 6206 |
encoding |
1311 | 6207 |
"return the strings encoding, as a symbol. |
7982 | 6208 |
Here, by default, we assume unicode-encoding. |
20006 | 6209 |
Notice, that ISO-8859-1 is a true subset of unicode, |
8874 | 6210 |
and that singleByteStrings are therefore both unicode AND |
7982 | 6211 |
8859-1 encoded." |
6212 |
||
14168 | 6213 |
^ #unicode |
608 | 6214 |
! |
6215 |
||
1375
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
6216 |
hasChangeOfEmphasis |
14405
1d0d99a95bba
comment/format in:7 methods
Claus Gittinger <cg@exept.de>
parents:
14168
diff
changeset
|
6217 |
"return true, if the receiver contains non-empty emphasis information |
1d0d99a95bba
comment/format in:7 methods
Claus Gittinger <cg@exept.de>
parents:
14168
diff
changeset
|
6218 |
i.e. any non-normal (=emphasized) characters" |
1d0d99a95bba
comment/format in:7 methods
Claus Gittinger <cg@exept.de>
parents:
14168
diff
changeset
|
6219 |
|
1375
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
6220 |
^ false |
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
6221 |
|
1381 | 6222 |
"Created: 12.5.1996 / 12:31:39 / cg" |
1375
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
6223 |
! |
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
6224 |
|
7995
8ddbb403d67b
+hasIcon and hasImage for LabelAndIcon compatibility
Claus Gittinger <cg@exept.de>
parents:
7991
diff
changeset
|
6225 |
hasIcon |
9191 | 6226 |
"for LabelAndIcon compatibility" |
6227 |
||
9192 | 6228 |
^ false |
6229 |
! |
|
6230 |
||
6231 |
hasImage |
|
6232 |
"for LabelAndIcon compatibility" |
|
6233 |
||
6234 |
^ false |
|
6235 |
! |
|
7995
8ddbb403d67b
+hasIcon and hasImage for LabelAndIcon compatibility
Claus Gittinger <cg@exept.de>
parents:
7991
diff
changeset
|
6236 |
|
1388 | 6237 |
heightOn:aGC |
19464 | 6238 |
"return the size of the receiver in device units if displayed on aGC" |
1388 | 6239 |
|
6528 | 6240 |
^ (aGC font onDevice:aGC device) heightOf:self |
1388 | 6241 |
|
6242 |
" |
|
8874 | 6243 |
'hello world' heightOn:(View new) |
1388 | 6244 |
" |
6245 |
||
6246 |
"Created: 12.5.1996 / 20:09:29 / cg" |
|
6247 |
"Modified: 12.5.1996 / 20:32:05 / cg" |
|
6248 |
! |
|
6249 |
||
11828 | 6250 |
isLowercaseFirst |
6251 |
"return true, if the first character is a lowercase character." |
|
6252 |
||
6253 |
^ self size ~~ 0 and:[ self first isLowercase ] |
|
6254 |
||
6255 |
" |
|
12177 | 6256 |
'helloWorld' isLowercaseFirst |
6257 |
'HelloWorld' isLowercaseFirst |
|
11828 | 6258 |
" |
6259 |
! |
|
6260 |
||
6261 |
isUppercaseFirst |
|
6262 |
"return true, if the first character is an uppercase character." |
|
6263 |
||
6264 |
^ self size ~~ 0 and:[ self first isUppercase ] |
|
6265 |
||
6266 |
" |
|
12177 | 6267 |
'helloWorld' isUppercaseFirst |
6268 |
'HelloWorld' isUppercaseFirst |
|
11828 | 6269 |
" |
6270 |
! |
|
6271 |
||
15773 | 6272 |
keywords |
6273 |
"assuming the receiver is a keyword message selector, |
|
6274 |
return the individual keywords (i.e. break it up at colons) |
|
6275 |
and return these as a collection. |
|
6276 |
For binary and unary selectors, the result may be nonsense (an array containing the receiver)." |
|
6277 |
||
6278 |
|coll s part| |
|
6279 |
||
6280 |
coll := OrderedCollection new. |
|
6281 |
s := ReadStream on:self. |
|
6282 |
[s atEnd] whileFalse:[ |
|
20012 | 6283 |
part := s through:$:. |
6284 |
coll add:part |
|
15773 | 6285 |
]. |
6286 |
^ coll asArray |
|
6287 |
||
6288 |
" |
|
6289 |
#at:put: keywords |
|
6290 |
#at: keywords |
|
6291 |
#+ keywords |
|
6292 |
#size keywords |
|
6293 |
" |
|
6294 |
||
6295 |
"Modified (Comment): / 30-06-2011 / 17:46:21 / cg" |
|
6296 |
! |
|
6297 |
||
9190 | 6298 |
knownAsSymbol |
6299 |
"for now, only single character strings are allowed as symbols. |
|
6300 |
This method is redefined in String." |
|
6301 |
||
6302 |
^ false |
|
6303 |
! |
|
6304 |
||
6305 |
leftIndent |
|
6306 |
"if the receiver starts with spaces, return the number of spaces |
|
6307 |
at the left - otherwise, return 0. |
|
19464 | 6308 |
If the receiver consists of spaces only, return the receiver's size." |
9192 | 6309 |
|
6310 |
|index "{Class: SmallInteger }" |
|
6311 |
end "{Class: SmallInteger }"| |
|
6312 |
||
6313 |
index := 1. |
|
6314 |
end := self size. |
|
6315 |
[index <= end] whileTrue:[ |
|
20012 | 6316 |
(self at:index) isSeparator ifFalse:[^ index - 1]. |
6317 |
index := index + 1 |
|
8350 | 6318 |
]. |
9192 | 6319 |
^ end |
6320 |
||
6321 |
" |
|
6322 |
' hello' leftIndent |
|
6323 |
'foo ' leftIndent |
|
9190 | 6324 |
' ' leftIndent |
6325 |
" |
|
6326 |
||
6327 |
"Modified: 20.4.1996 / 19:28:43 / cg" |
|
8350 | 6328 |
! |
6329 |
||
10437 | 6330 |
nameSpaceSelectorParts |
6331 |
"Answer the namespace and baseSelector parts of a namespace selector. |
|
6332 |
Namespace selectors are those generated by sends from a method in a different |
|
6333 |
namespace; they are prefixed by ':'<ns>'::'. |
|
6334 |
You cannot easily change this algorithm here, as it is also known by the VM's lookup function. |
|
6335 |
Experimental" |
|
6336 |
||
6337 |
|nsPart selPart idx| |
|
6338 |
||
6339 |
(self at:1) == $: ifFalse:[^ Array with:'' with:self]. |
|
6340 |
idx := self indexOf:$: startingAt:3. |
|
6341 |
idx == 0 ifTrue:[^ Array with:'' with:self]. |
|
6342 |
||
6343 |
(idx+2 > self size) ifTrue:[^ Array with:'' with:self]. |
|
6344 |
(self at:idx+1) == $: ifFalse:[^ Array with:'' with:self]. |
|
6345 |
nsPart := self copyFrom:2 to:idx-1. |
|
6346 |
selPart := self copyFrom:idx+2. |
|
6347 |
^ Array with:nsPart with:selPart |
|
6348 |
||
6349 |
"test: |
|
10703
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
6350 |
self assert:('+' nameSpaceSelectorParts) = #('' '+'). |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
6351 |
self assert:(':+' nameSpaceSelectorParts) = #('' ':+'). |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
6352 |
self assert:(':Foo:+' nameSpaceSelectorParts) = #('' ':Foo:+'). |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
6353 |
|
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
6354 |
self assert:(':Foo::+' nameSpaceSelectorParts) = #('Foo' '+'). |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
6355 |
self assert:(':Foo::bar:baz:' nameSpaceSelectorParts) = #('Foo' 'bar:baz:'). |
10437 | 6356 |
" |
6357 |
||
6358 |
"Created: / 05-03-2007 / 17:16:58 / cg" |
|
6359 |
"Modified: / 06-03-2007 / 11:51:15 / cg" |
|
6360 |
! |
|
6361 |
||
9190 | 6362 |
stringSpecies |
8874 | 6363 |
"return the underlying strings bitsPerCharacter |
8037
8cce25b1059c
care for wide chars in asXXXFirst
Claus Gittinger <cg@exept.de>
parents:
8032
diff
changeset
|
6364 |
(i.e. is it a regular String or a TwoByteString)" |
8cce25b1059c
care for wide chars in asXXXFirst
Claus Gittinger <cg@exept.de>
parents:
8032
diff
changeset
|
6365 |
|
8cce25b1059c
care for wide chars in asXXXFirst
Claus Gittinger <cg@exept.de>
parents:
8032
diff
changeset
|
6366 |
|string| |
8cce25b1059c
care for wide chars in asXXXFirst
Claus Gittinger <cg@exept.de>
parents:
8032
diff
changeset
|
6367 |
|
15523 | 6368 |
string := self string. |
6369 |
string == self ifTrue:[^ self species]. |
|
8037
8cce25b1059c
care for wide chars in asXXXFirst
Claus Gittinger <cg@exept.de>
parents:
8032
diff
changeset
|
6370 |
^ string stringSpecies |
8cce25b1059c
care for wide chars in asXXXFirst
Claus Gittinger <cg@exept.de>
parents:
8032
diff
changeset
|
6371 |
|
8cce25b1059c
care for wide chars in asXXXFirst
Claus Gittinger <cg@exept.de>
parents:
8032
diff
changeset
|
6372 |
" |
8874 | 6373 |
'hello' stringSpecies |
9192 | 6374 |
'hello' asText allBold stringSpecies |
6375 |
" |
|
6376 |
! |
|
8037
8cce25b1059c
care for wide chars in asXXXFirst
Claus Gittinger <cg@exept.de>
parents:
8032
diff
changeset
|
6377 |
|
6653 | 6378 |
widthFrom:startIndex to:endIndex on:aGC |
19464 | 6379 |
"return ths size of part of the receiver in device units if displayed on aGC" |
6653 | 6380 |
|
6381 |
^ (aGC font onDevice:aGC device) widthOf:self from:startIndex to:endIndex |
|
6382 |
||
6383 |
" |
|
8874 | 6384 |
'hello world' widthFrom:1 to:5 on:(View new) |
6385 |
'hello' widthOn:(View new) |
|
6653 | 6386 |
" |
9192 | 6387 |
! |
6653 | 6388 |
|
1388 | 6389 |
widthOn:aGC |
19464 | 6390 |
"return ths size of the receiver in device units if displayed on aGC" |
1388 | 6391 |
|
6528 | 6392 |
^ (aGC font onDevice:aGC device) widthOf:self |
1388 | 6393 |
|
6394 |
" |
|
8874 | 6395 |
'hello world' widthOn:(View new) |
1388 | 6396 |
" |
6397 |
||
6398 |
"Created: 12.5.1996 / 20:09:29 / cg" |
|
2554 | 6399 |
"Modified: 17.4.1997 / 12:50:23 / cg" |
608 | 6400 |
! ! |
6401 |
||
20155 | 6402 |
|
608 | 6403 |
!CharacterArray methodsFor:'special string converting'! |
6404 |
||
16759 | 6405 |
asUnixFilenameString |
6406 |
"return a new string consisting of receiver's characters |
|
6407 |
with all \-characters replaced by /-characters. |
|
6408 |
If there are no backslashes, return the original" |
|
6409 |
||
6410 |
^ self copyReplaceAll:$\ with:$/ ifNone:self |
|
6411 |
||
6412 |
" |
|
6413 |
'hello\world' asUnixFilenameString |
|
6414 |
" |
|
6415 |
||
6416 |
"Modified: / 18.7.1998 / 22:53:02 / cg" |
|
6417 |
! |
|
6418 |
||
14768 | 6419 |
expandPlaceholders:escapeCharacter with:argArrayOrDictionary |
6420 |
"this is the generic version of the old %-escaping method, allowing for an arbitrary |
|
6421 |
escape character to be used (typically $$ or $% are effectively used). |
|
6422 |
Return a copy of the receiver, where all %i escapes are |
|
6423 |
replaced by corresponding arguments' printStrings from the argArrayOrDictionary. |
|
6424 |
I.e. 'hello %1; how is %2' expandPlaceholdersWith:#('world' 'this') results |
|
6425 |
in the new string 'hello world; how is this'. |
|
6426 |
As an extension, the argument may also be a dictionary, providing |
|
6427 |
values for symbolic keys. |
|
6428 |
In this case, %a .. %z and %(...) are also allowed. |
|
6429 |
(%1..%9 require a numeric key in the dictionary, however) |
|
6430 |
To get a '%' character, use a '%%'-escape. |
|
6431 |
To get an integer-indexed placeHolder followed by another digit, |
|
6432 |
or an index > 9, you must use %(digit). |
|
6433 |
See also bindWith:... for VisualAge compatibility." |
|
6434 |
||
6435 |
|stream| |
|
6436 |
||
18269 | 6437 |
stream := (TextStream ? CharacterWriteStream) on:(self species uninitializedNew:self size + 20). |
14768 | 6438 |
self expandPlaceholders:escapeCharacter with:argArrayOrDictionary on:stream. |
6439 |
^ stream contents. |
|
6440 |
||
6441 |
||
6442 |
" |
|
6443 |
'hello %1' expandPlaceholdersWith:#('world') |
|
6444 |
'hello %1; how is %2' expandPlaceholdersWith:#('world' 'this') |
|
6445 |
'hello %2; how is %1' expandPlaceholdersWith:#('world' 'this') |
|
6446 |
'%1 plus %2 gives %3 ' expandPlaceholdersWith:#(4 5 9) |
|
6447 |
'%%(1)0 gives %(1)0' expandPlaceholdersWith:#(123) |
|
6448 |
'%%10 gives %10' expandPlaceholdersWith:#(123) |
|
6449 |
'%%(10) gives %(10)' expandPlaceholdersWith:#(123) |
|
6450 |
'%test gives %1' expandPlaceholdersWith:#(123) |
|
6451 |
'bla %1 bla' expandPlaceholdersWith:{ 'hello' allBold } |
|
6452 |
'bla %1 bla' expandPlaceholdersWith:{ 'hello' } |
|
6453 |
" |
|
6454 |
||
6455 |
" |
|
6456 |
|dict| |
|
6457 |
||
6458 |
dict := Dictionary new. |
|
6459 |
dict at:1 put:'one'. |
|
6460 |
dict at:$a put:'AAAAA'. |
|
6461 |
dict at:$b put:[ Time now ]. |
|
6462 |
dict at:'foo' put:[ Date today ]. |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
6463 |
'hello $1 %a $b %(foo) $foo ' expandPlaceholders:$$ with:dict. |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
6464 |
'hello $1 %a $b %(foo) $foo ' expandPlaceholders:$% with:dict. |
14768 | 6465 |
" |
6466 |
||
6467 |
"Modified: 1.7.1997 / 00:53:24 / cg" |
|
6468 |
! |
|
6469 |
||
14765 | 6470 |
expandPlaceholders:escapeCharacter with:argArrayOrDictionary on:aStream |
6471 |
"this is the generic version of the old %-escaping method, allowing for an arbitrary |
|
6472 |
escape character to be used (typically $$ or $% are effectively used). |
|
18425 | 6473 |
|
14765 | 6474 |
Write the receiver to aStream, where all %i escapes are |
6475 |
replaced by corresponding arguments' printStrings from the argArrayOrDictionary. |
|
6476 |
I.e. 'hello %1; how is %2' expandPlaceholdersWith:#('world' 'this') results |
|
6477 |
in the new string 'hello world; how is this'. |
|
18425 | 6478 |
|
17192 | 6479 |
As an extension, the argument may also be a dictionary, providing values for symbolic keys. |
14765 | 6480 |
In this case, %a .. %z and %(...) are also allowed. |
6481 |
(%1..%9 require a numeric key in the dictionary, however) |
|
17192 | 6482 |
Also, the values in argArrayOrDictionary may be blocks. |
18425 | 6483 |
|
14765 | 6484 |
To get a '%' character, use a '%%'-escape. |
6485 |
To get an integer-indexed placeHolder followed by another digit, |
|
6486 |
or an index > 9, you must use %(digit). |
|
18425 | 6487 |
|
14765 | 6488 |
See also bindWith:... for VisualAge compatibility. |
6489 |
Use %<cr> to insert a CR and %<tab> to insert a TAB." |
|
6490 |
||
6491 |
|next v key |
|
6492 |
idx "{ SmallInteger }" |
|
6493 |
idx2 "{ SmallInteger }" |
|
6494 |
start "{ SmallInteger }" |
|
6495 |
stop "{ SmallInteger }"| |
|
6496 |
||
6497 |
stop := self size. |
|
6498 |
start := 1. |
|
6499 |
[start <= stop] whileTrue:[ |
|
20012 | 6500 |
idx := self indexOf:escapeCharacter startingAt:start. |
6501 |
(idx == 0 or:[idx == stop]) ifTrue:[ |
|
6502 |
aStream nextPutAll:self startingAt:start to:stop. |
|
6503 |
^ self. |
|
6504 |
]. |
|
6505 |
"found an escapeCharacter" |
|
6506 |
aStream nextPutAll:self startingAt:start to:(idx - 1). |
|
6507 |
next := self at:(idx + 1). |
|
6508 |
(next == escapeCharacter) ifTrue:[ |
|
6509 |
aStream nextPut:escapeCharacter. |
|
6510 |
] ifFalse:[ |
|
6511 |
next == $< ifTrue:[ |
|
6512 |
idx2 := self indexOf:$> startingAt:idx+2. |
|
6513 |
key := self copyFrom:idx+2 to:idx2-1. |
|
6514 |
idx := idx2 - 1. |
|
6515 |
key := key asSymbolIfInterned. |
|
6516 |
(#(cr tab nl return lf ff null) includesIdentical:key) ifTrue:[ |
|
6517 |
aStream nextPut:(Character perform:key). |
|
6518 |
]. |
|
6519 |
] ifFalse:[ |
|
6520 |
next isDigit ifTrue:[ |
|
6521 |
v := argArrayOrDictionary at:(next digitValue) ifAbsent:'' |
|
6522 |
] ifFalse:[ |
|
6523 |
next == $( ifTrue:[ |
|
6524 |
idx2 := self indexOf:$) startingAt:idx+2. |
|
6525 |
key := self copyFrom:idx+2 to:idx2-1. |
|
6526 |
idx := idx2 - 1. |
|
6527 |
(argArrayOrDictionary includesKey:key) ifTrue:[ |
|
6528 |
v := argArrayOrDictionary at:key |
|
6529 |
] ifFalse:[ |
|
6530 |
key := key asSymbolIfInterned ? key. |
|
6531 |
(argArrayOrDictionary includesKey:key) ifTrue:[ |
|
6532 |
v := argArrayOrDictionary at:key |
|
6533 |
] ifFalse:[ |
|
6534 |
(key size == 1 and:[ argArrayOrDictionary includesKey:(key at:1)]) ifTrue:[ |
|
6535 |
v := argArrayOrDictionary at:(key at:1) |
|
6536 |
] ifFalse:[ |
|
6537 |
key isNumeric ifTrue:[ |
|
6538 |
key := Integer readFrom:key onError:nil. |
|
6539 |
]. |
|
6540 |
v := argArrayOrDictionary at:key ifAbsent:'' |
|
6541 |
] |
|
6542 |
]. |
|
6543 |
]. |
|
6544 |
] ifFalse:[ |
|
6545 |
(next isLetter and:[argArrayOrDictionary isSequenceable not "is a Dictionary"]) ifTrue:[ |
|
6546 |
"so next is a non-numeric single character." |
|
6547 |
v := argArrayOrDictionary |
|
6548 |
at:next |
|
6549 |
ifAbsent:[ |
|
6550 |
"try symbol instead of character" |
|
6551 |
argArrayOrDictionary |
|
6552 |
at:next asSymbol |
|
6553 |
ifAbsent:[String with:escapeCharacter with:next]. |
|
6554 |
]. |
|
6555 |
] ifFalse:[ |
|
6556 |
v := String with:$% with:next. |
|
6557 |
]. |
|
6558 |
] |
|
6559 |
]. |
|
6560 |
"/ v notNil ifTrue:[ |
|
6561 |
v isBlock ifTrue:[ |
|
6562 |
v := v value |
|
6563 |
]. |
|
6564 |
||
6565 |
v printOn:aStream. |
|
6566 |
"/ ]. |
|
6567 |
] |
|
6568 |
]. |
|
6569 |
start := idx + 2 |
|
14765 | 6570 |
]. |
6571 |
||
6572 |
" |
|
6573 |
String streamContents:[:s| |
|
20012 | 6574 |
'hello %1' expandPlaceholders:$% with:#('world') on:s. |
6575 |
s cr. |
|
6576 |
'hello $1; how is $2' expandPlaceholders:$$ with:#('world' 'this') on:s. |
|
6577 |
s cr. |
|
6578 |
'hello %2; how is %1' expandPlaceholders:$% with:#('world' 'this') on:s. |
|
6579 |
s cr. |
|
6580 |
'%1 plus %2 gives %3 ' expandPlaceholders:$% with:#(4 5 9) on:s. |
|
6581 |
s cr. |
|
6582 |
'%%(1)0 gives %(1)0' expandPlaceholders:$% with:#(123) on:s. |
|
6583 |
s cr. |
|
6584 |
'%%10 gives %10' expandPlaceholders:$% with:#(123) on:s. |
|
6585 |
s cr. |
|
6586 |
'%%(10) gives %(10) %<cr>%<tab>next line' expandPlaceholders:$% with:#(123) on:s. |
|
6587 |
s cr. |
|
6588 |
'%%test gives %test' expandPlaceholders:$% with:#(123) on:s. |
|
6589 |
s cr. |
|
6590 |
'|%%<tab>|%%1|%%<cr>| gives |%<tab>|%1|%<cr>|' expandPlaceholders:$% with:#(foo) on:s. |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
6591 |
] |
14765 | 6592 |
" |
6593 |
||
6594 |
" |
|
6595 |
|dict| |
|
6596 |
||
6597 |
dict := Dictionary new. |
|
6598 |
dict at:1 put:'one'. |
|
6599 |
dict at:$a put:'AAAAA'. |
|
6600 |
dict at:$b put:[ Time now ]. |
|
6601 |
String streamContents:[:s| |
|
20012 | 6602 |
'hello $1 $a $b' expandPlaceholders:$$ with:dict on:s. |
14765 | 6603 |
]. |
6604 |
" |
|
6605 |
||
18425 | 6606 |
"using blocks: |
6607 |
|dict| |
|
6608 |
||
6609 |
dict := Dictionary new. |
|
6610 |
dict at:'time' put:[Time now printString]. |
|
6611 |
dict at:'date' put:[Date today printString]. |
|
6612 |
String streamContents:[:s| |
|
20012 | 6613 |
'it is $(time) $(date)' expandPlaceholders:$$ with:dict on:s. |
18425 | 6614 |
]. |
6615 |
" |
|
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
6616 |
|
14765 | 6617 |
"Modified: / 18-11-2010 / 15:43:28 / cg" |
6618 |
! |
|
6619 |
||
9192 | 6620 |
expandPlaceholdersWith:argArrayOrDictionary |
608 | 6621 |
"return a copy of the receiver, where all %i escapes are |
4410
2bdc0c89a42e
enhanced #exapndPlaceHolders, to allow for a dictionary arg
Claus Gittinger <cg@exept.de>
parents:
4409
diff
changeset
|
6622 |
replaced by corresponding arguments' printStrings from the argArrayOrDictionary. |
608 | 6623 |
I.e. 'hello %1; how is %2' expandPlaceholdersWith:#('world' 'this') results |
6624 |
in the new string 'hello world; how is this'. |
|
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
6625 |
|
4410
2bdc0c89a42e
enhanced #exapndPlaceHolders, to allow for a dictionary arg
Claus Gittinger <cg@exept.de>
parents:
4409
diff
changeset
|
6626 |
As an extension, the argument may also be a dictionary, providing |
7313 | 6627 |
values for symbolic keys. |
4842 | 6628 |
In this case, %a .. %z and %(...) are also allowed. |
4410
2bdc0c89a42e
enhanced #exapndPlaceHolders, to allow for a dictionary arg
Claus Gittinger <cg@exept.de>
parents:
4409
diff
changeset
|
6629 |
(%1..%9 require a numeric key in the dictionary, however) |
18425 | 6630 |
Also, the values in argArrayOrDictionary may be blocks. |
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
6631 |
|
608 | 6632 |
To get a '%' character, use a '%%'-escape. |
7313 | 6633 |
To get an integer-indexed placeHolder followed by another digit, |
6634 |
or an index > 9, you must use %(digit). |
|
608 | 6635 |
See also bindWith:... for VisualAge compatibility." |
6636 |
||
10906
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6637 |
|stream| |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6638 |
|
18269 | 6639 |
stream := (TextStream ? CharacterWriteStream) on:(self species uninitializedNew:self size + 20). |
10906
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6640 |
self expandPlaceholdersWith:argArrayOrDictionary on:stream. |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6641 |
^ stream contents. |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6642 |
|
608 | 6643 |
" |
8874 | 6644 |
'hello %1' expandPlaceholdersWith:#('world') |
6645 |
'hello %1; how is %2' expandPlaceholdersWith:#('world' 'this') |
|
6646 |
'hello %2; how is %1' expandPlaceholdersWith:#('world' 'this') |
|
6647 |
'%1 plus %2 gives %3 ' expandPlaceholdersWith:#(4 5 9) |
|
6648 |
'%%(1)0 gives %(1)0' expandPlaceholdersWith:#(123) |
|
6649 |
'%%10 gives %10' expandPlaceholdersWith:#(123) |
|
6650 |
'%%(10) gives %(10)' expandPlaceholdersWith:#(123) |
|
8938
860bbcbd3bd8
Fix #expandPlaceHoldersWith:, when a %a is encountered and and an Array
Stefan Vogel <sv@exept.de>
parents:
8913
diff
changeset
|
6651 |
'%test gives %1' expandPlaceholdersWith:#(123) |
11028
cb1ee3ddd91a
expandPlaceHolders: care fr Text with emphasis
Claus Gittinger <cg@exept.de>
parents:
11018
diff
changeset
|
6652 |
'bla %1 bla' expandPlaceholdersWith:{ 'hello' allBold } |
cb1ee3ddd91a
expandPlaceHolders: care fr Text with emphasis
Claus Gittinger <cg@exept.de>
parents:
11018
diff
changeset
|
6653 |
'bla %1 bla' expandPlaceholdersWith:{ 'hello' } |
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
6654 |
('bla %1 bla' withColor:Color red) |
20012 | 6655 |
expandPlaceholdersWith:{ 'hello' } |
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
6656 |
('bla %1 bla' withColor:Color red) |
20012 | 6657 |
expandPlaceholdersWith:{ 'hello' withColor:Color blue } |
608 | 6658 |
" |
1026
455c9f609f4d
care for 16bit strings in expandPlaceHolders
Claus Gittinger <cg@exept.de>
parents:
1025
diff
changeset
|
6659 |
|
4410
2bdc0c89a42e
enhanced #exapndPlaceHolders, to allow for a dictionary arg
Claus Gittinger <cg@exept.de>
parents:
4409
diff
changeset
|
6660 |
" |
2bdc0c89a42e
enhanced #exapndPlaceHolders, to allow for a dictionary arg
Claus Gittinger <cg@exept.de>
parents:
4409
diff
changeset
|
6661 |
|dict| |
2bdc0c89a42e
enhanced #exapndPlaceHolders, to allow for a dictionary arg
Claus Gittinger <cg@exept.de>
parents:
4409
diff
changeset
|
6662 |
|
2bdc0c89a42e
enhanced #exapndPlaceHolders, to allow for a dictionary arg
Claus Gittinger <cg@exept.de>
parents:
4409
diff
changeset
|
6663 |
dict := Dictionary new. |
2bdc0c89a42e
enhanced #exapndPlaceHolders, to allow for a dictionary arg
Claus Gittinger <cg@exept.de>
parents:
4409
diff
changeset
|
6664 |
dict at:1 put:'one'. |
2bdc0c89a42e
enhanced #exapndPlaceHolders, to allow for a dictionary arg
Claus Gittinger <cg@exept.de>
parents:
4409
diff
changeset
|
6665 |
dict at:$a put:'AAAAA'. |
8794
a5df33935142
nindWith supports blocks in the value dictionary
Claus Gittinger <cg@exept.de>
parents:
8710
diff
changeset
|
6666 |
dict at:$b put:[ Time now ]. |
12316
3e78905d70a9
comment/format in: #expandPlaceholdersWith:
Claus Gittinger <cg@exept.de>
parents:
12196
diff
changeset
|
6667 |
dict at:'foo' put:[ Date today ]. |
13882
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
6668 |
'hello %1 %a %b %(foo)' expandPlaceholdersWith:dict |
4410
2bdc0c89a42e
enhanced #exapndPlaceHolders, to allow for a dictionary arg
Claus Gittinger <cg@exept.de>
parents:
4409
diff
changeset
|
6669 |
" |
2bdc0c89a42e
enhanced #exapndPlaceHolders, to allow for a dictionary arg
Claus Gittinger <cg@exept.de>
parents:
4409
diff
changeset
|
6670 |
|
2737
9f29aecea39d
care for % at end in expandPlaceHolders.
Claus Gittinger <cg@exept.de>
parents:
2736
diff
changeset
|
6671 |
"Modified: 1.7.1997 / 00:53:24 / cg" |
9192 | 6672 |
! |
6673 |
||
10906
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6674 |
expandPlaceholdersWith:argArrayOrDictionary on:aStream |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6675 |
"write the receiver to aStream, where all %i escapes are |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6676 |
replaced by corresponding arguments' printStrings from the argArrayOrDictionary. |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6677 |
I.e. 'hello %1; how is %2' expandPlaceholdersWith:#('world' 'this') results |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6678 |
in the new string 'hello world; how is this'. |
18425 | 6679 |
|
10906
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6680 |
As an extension, the argument may also be a dictionary, providing |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6681 |
values for symbolic keys. |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6682 |
In this case, %a .. %z and %(...) are also allowed. |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6683 |
(%1..%9 require a numeric key in the dictionary, however) |
18425 | 6684 |
Also, the values in argArrayOrDictionary may be blocks. |
6685 |
||
10906
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6686 |
To get a '%' character, use a '%%'-escape. |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6687 |
To get an integer-indexed placeHolder followed by another digit, |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6688 |
or an index > 9, you must use %(digit). |
18425 | 6689 |
|
10935 | 6690 |
See also bindWith:... for VisualAge compatibility. |
10939
d0ca31c1d1e9
#expandPlaceHoldersWith: handle %<cr> %<tab> etc
Stefan Vogel <sv@exept.de>
parents:
10935
diff
changeset
|
6691 |
Use %<cr> to insert a CR and %<tab> to insert a TAB." |
10906
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6692 |
|
14765 | 6693 |
^ self expandPlaceholders:$% with:argArrayOrDictionary on:aStream |
10906
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6694 |
|
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6695 |
" |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6696 |
String streamContents:[:s| |
20012 | 6697 |
'hello %1' expandPlaceholdersWith:#('world') on:s. |
6698 |
s cr. |
|
6699 |
'hello %1; how is %2' expandPlaceholdersWith:#('world' 'this') on:s. |
|
6700 |
s cr. |
|
6701 |
'hello %2; how is %1' expandPlaceholdersWith:#('world' 'this') on:s. |
|
6702 |
s cr. |
|
6703 |
'%1 plus %2 gives %3 ' expandPlaceholdersWith:#(4 5 9) on:s. |
|
6704 |
s cr. |
|
6705 |
'%%(1)0 gives %(1)0' expandPlaceholdersWith:#(123) on:s. |
|
6706 |
s cr. |
|
6707 |
'%%10 gives %10' expandPlaceholdersWith:#(123) on:s. |
|
6708 |
s cr. |
|
6709 |
'%%(10) gives %(10) %<cr>%<tab>next line' expandPlaceholdersWith:#(123) on:s. |
|
6710 |
s cr. |
|
6711 |
'%test gives %1' expandPlaceholdersWith:#(123) on:s. |
|
10906
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6712 |
] |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6713 |
" |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6714 |
|
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6715 |
" |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6716 |
|dict| |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6717 |
|
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6718 |
dict := Dictionary new. |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6719 |
dict at:1 put:'one'. |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6720 |
dict at:$a put:'AAAAA'. |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6721 |
dict at:$b put:[ Time now ]. |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6722 |
String streamContents:[:s| |
20012 | 6723 |
'hello %1 %a %b' expandPlaceholdersWith:dict on:s. |
10906
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6724 |
]. |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6725 |
" |
13008
a27647c36b31
changed: #expandPlaceholdersWith:on:
Claus Gittinger <cg@exept.de>
parents:
12925
diff
changeset
|
6726 |
|
13138
d2eb0eba5907
changed: #expandPlaceholdersWith:on:
Claus Gittinger <cg@exept.de>
parents:
13137
diff
changeset
|
6727 |
"Modified: / 18-11-2010 / 15:43:28 / cg" |
10906
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6728 |
! |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6729 |
|
9230 | 6730 |
firstLine |
14405
1d0d99a95bba
comment/format in:7 methods
Claus Gittinger <cg@exept.de>
parents:
14168
diff
changeset
|
6731 |
"return the first line of a multiline string" |
1d0d99a95bba
comment/format in:7 methods
Claus Gittinger <cg@exept.de>
parents:
14168
diff
changeset
|
6732 |
|
1d0d99a95bba
comment/format in:7 methods
Claus Gittinger <cg@exept.de>
parents:
14168
diff
changeset
|
6733 |
^ self asCollectionOfSubCollectionsSeparatedBy:(Character cr) do:[:line | ^ line]. |
9230 | 6734 |
|
6735 |
" |
|
10703
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
6736 |
'hello' firstLine |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
6737 |
'1\2\3' withCRs firstLine |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
6738 |
'\1\2\3' withCRs firstLine |
9230 | 6739 |
" |
6740 |
! |
|
6741 |
||
13987 | 6742 |
tokensBasedOn:aCharacter |
6743 |
"this is an ST-80 alias for the ST/X method |
|
20012 | 6744 |
asCollectionOfSubstringsSeparatedBy:" |
13987 | 6745 |
|
6746 |
^ self asCollectionOfSubstringsSeparatedBy:aCharacter |
|
6747 |
||
6748 |
" |
|
6749 |
'hello:world:isnt:this nice' tokensBasedOn:$: |
|
6750 |
'foo,bar,baz' tokensBasedOn:$, |
|
6751 |
'/etc/passwd' asFilename readStream nextLine tokensBasedOn:$: |
|
6752 |
" |
|
6753 |
! |
|
6754 |
||
6755 |
withCEscapes |
|
16863 | 6756 |
"return a new string consisting of receiver's characters |
13987 | 6757 |
with all special and unprintable characters replaced by \X-character escapes. |
6758 |
(similar to the way C-language literal Strings are represented). |
|
6759 |
The resulting string will contain only 7-bit ascii characters. |
|
6760 |
Emphasis is not supported. |
|
6761 |
The following escapes are generated: |
|
20012 | 6762 |
\' single quote character |
6763 |
\dQuote double quote character |
|
6764 |
\r return character |
|
6765 |
\r return character |
|
6766 |
\n newline character |
|
6767 |
\t tab character |
|
6768 |
\\ the \ character itself |
|
6769 |
\xnn two digit hex number defining the characters ascii value |
|
6770 |
\unnnn four digit hex number defining the characters ascii value |
|
6771 |
\Unnnnnnnn eight digit hex number defining the characters ascii value |
|
16863 | 6772 |
This is the opposite of withoutCEscapes. |
17000 | 6773 |
|
6774 |
Sigh: this is named completely wrong (opposite naming of withCRs/witoutCRs), |
|
20012 | 6775 |
but it cannot be changed easily, as these methods are already used heavily |
13987 | 6776 |
" |
6777 |
||
6778 |
|anyEscapeNeeded out seq| |
|
6779 |
||
6780 |
" |
|
6781 |
first, check if any escape is needed and return the receiver unchanged if not |
|
6782 |
" |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
6783 |
anyEscapeNeeded := self |
20012 | 6784 |
contains:[:ch | |
6785 |
((ch codePoint between:32 and:126) not |
|
6786 |
or:[ch == $' or:[ch == $"]]) |
|
6787 |
]. |
|
13987 | 6788 |
anyEscapeNeeded ifFalse:[ ^ self ]. |
6789 |
||
6790 |
self hasChangeOfEmphasis ifTrue:[ self error:'emphasis not supported' ]. |
|
6791 |
||
18269 | 6792 |
out := WriteStream on:(String uninitializedNew:self size-1). |
13987 | 6793 |
|
6794 |
self do:[:ch | |
|
20012 | 6795 |
|cp| |
6796 |
||
6797 |
(ch == $' or:[ch == $"]) ifTrue:[ |
|
6798 |
out nextPut:$\. |
|
6799 |
out nextPut:ch. |
|
6800 |
] ifFalse:[ |
|
6801 |
(ch codePoint between:32 and:126) ifTrue:[ |
|
6802 |
out nextPut:ch |
|
6803 |
] ifFalse:[ |
|
6804 |
ch == Character return ifTrue:[ |
|
6805 |
seq := '\r' |
|
6806 |
] ifFalse:[ ch == Character nl ifTrue:[ |
|
6807 |
seq := '\n' |
|
6808 |
] ifFalse:[ ch == Character tab ifTrue:[ |
|
6809 |
seq := '\t' |
|
6810 |
] ifFalse:[ ch == $\ ifTrue:[ |
|
6811 |
seq := '\\' |
|
6812 |
] ifFalse:[ |
|
6813 |
cp := ch codePoint. |
|
6814 |
cp <= 16rFF ifTrue:[ |
|
6815 |
seq := '\x' , (cp printStringRadix:16 padTo:2) |
|
6816 |
] ifFalse:[ |
|
6817 |
cp <= 16rFFFF ifTrue:[ |
|
6818 |
seq := '\u' , (cp printStringRadix:16 padTo:4) |
|
6819 |
] ifFalse:[ |
|
6820 |
seq := '\U',(cp printStringRadix:16 padTo:8) |
|
6821 |
] |
|
6822 |
] |
|
6823 |
]]]]. |
|
6824 |
out nextPutAll:seq |
|
6825 |
]. |
|
6826 |
]. |
|
13987 | 6827 |
]. |
6828 |
^ out contents |
|
6829 |
||
6830 |
" |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
6831 |
'hello\n\tworld' withoutCEscapes. |
17000 | 6832 |
'hello\nworld\na\n\tnice\n\t\tstring' withoutCEscapes withCEscapes. |
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
6833 |
('hello ',(Character value:16r1234),' world') withCEscapes |
13987 | 6834 |
" |
6835 |
||
6836 |
"Created: / 25-01-2012 / 11:08:16 / cg" |
|
6837 |
! |
|
6838 |
||
9192 | 6839 |
withCRs |
17205 | 6840 |
"return a new string consisting of the receiver's characters |
16757 | 6841 |
with all \-characters replaced by cr-characters. |
6842 |
If there are no backslashes, return the original" |
|
6843 |
||
16759 | 6844 |
^ self copyReplaceAll:$\ with:(Character cr) ifNone:self |
9192 | 6845 |
|
6846 |
" |
|
6847 |
'hello\world' withCRs |
|
6848 |
" |
|
6849 |
||
6850 |
"Modified: / 18.7.1998 / 22:53:02 / cg" |
|
6851 |
! |
|
6852 |
||
6853 |
withEscapes |
|
15405 | 6854 |
<resource: #obsolete> |
13987 | 6855 |
"has been renamed; the name withEscapes is misleading" |
6856 |
||
6857 |
self obsoleteMethodWarning:'use withoutCEscapes'. |
|
6858 |
^ self withoutCEscapes. |
|
6859 |
||
6860 |
"Modified: / 25-01-2012 / 10:42:30 / cg" |
|
9192 | 6861 |
! |
6862 |
||
6863 |
withMatchEscapes |
|
4117 | 6864 |
"return a copy of the receiver with all match characters escaped |
6865 |
by $\ characters (to be usable as a match string). |
|
6866 |
Return the receiver, if there are none." |
|
6867 |
||
5342
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
6868 |
|in out c escape| |
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
6869 |
|
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
6870 |
escape := self class matchEscapeCharacter. |
4117 | 6871 |
|
6872 |
in := self readStream. |
|
7986 | 6873 |
out := WriteStream on:(self species new:self size). |
4117 | 6874 |
[in atEnd] whileFalse:[ |
20012 | 6875 |
c := in next. |
6876 |
(c == escape or:['*[#' includes:c]) ifTrue:[ |
|
6877 |
out nextPut:$\. |
|
6878 |
]. |
|
6879 |
out nextPut:c. |
|
4117 | 6880 |
]. |
6881 |
^ out contents. |
|
6882 |
||
6883 |
" |
|
8874 | 6884 |
'*foo' withMatchEscapes |
6885 |
'\*foo' withMatchEscapes |
|
6886 |
'*foo' withMatchEscapes |
|
6887 |
'\\*foo' withMatchEscapes |
|
6888 |
'foo*' withMatchEscapes |
|
6889 |
'foo\*' withMatchEscapes |
|
6890 |
'foo\' withMatchEscapes |
|
6891 |
'f*o*o' withMatchEscapes |
|
4117 | 6892 |
" |
6893 |
||
6894 |
"Modified: 2.4.1997 / 18:13:04 / cg" |
|
9192 | 6895 |
! |
6896 |
||
14992 | 6897 |
withSeparatorsCompacted |
6898 |
"return a new string with each sequence of whiteSpace replaced by a single space character. |
|
6899 |
Preserves a leading/trailing space." |
|
6900 |
||
6901 |
^ self species streamContents:[:s | |
|
20012 | 6902 |
|skipping| |
6903 |
||
6904 |
skipping := false. |
|
6905 |
1 to:self size do:[:idx | |
|
6906 |
|char| |
|
6907 |
||
6908 |
char := self at:idx. |
|
6909 |
char isSeparator ifFalse:[ |
|
6910 |
s nextPut:char. |
|
6911 |
skipping := false. |
|
6912 |
] ifTrue:[ |
|
6913 |
skipping ifFalse:[ |
|
6914 |
s nextPut:(Character space). |
|
6915 |
skipping := true |
|
6916 |
]. |
|
6917 |
] |
|
6918 |
] |
|
14992 | 6919 |
] |
6920 |
||
6921 |
" |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
6922 |
'hello wwww' withSeparatorsCompacted |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
6923 |
'hello wwww' withSeparatorsCompacted |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
6924 |
' hello wwww' withSeparatorsCompacted |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
6925 |
' hello wwww ' withSeparatorsCompacted |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
6926 |
' hello wwww ' withSeparatorsCompacted |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
6927 |
'hel lo www w' withSeparatorsCompacted |
14992 | 6928 |
" |
6929 |
! |
|
6930 |
||
18464 | 6931 |
withSeparatorsReplacedBy:replacementCharacter |
6932 |
"return a new string with each separator (whitespace) replaced by replacementCharacter. |
|
6933 |
Typically used with space as replacementCharacter" |
|
6934 |
||
6935 |
^ self species streamContents:[:s | |
|
20012 | 6936 |
self do:[:ch | |
6937 |
ch isSeparator ifTrue:[ |
|
6938 |
s nextPut:replacementCharacter |
|
6939 |
] ifFalse:[ |
|
6940 |
s nextPut:ch. |
|
6941 |
] |
|
6942 |
] |
|
18464 | 6943 |
] |
6944 |
||
6945 |
" |
|
6946 |
'hello wwww' withSeparatorsReplacedBy:$* |
|
6947 |
'hello ww ww' withSeparatorsReplacedBy:$* |
|
6948 |
' hello wwww' withSeparatorsReplacedBy:$* |
|
6949 |
'hel lo www w' withSeparatorsReplacedBy:$* |
|
6950 |
'hel |
|
6951 |
lo www |
|
6952 |
w' withSeparatorsReplacedBy:$* |
|
6953 |
" |
|
6954 |
! |
|
6955 |
||
9192 | 6956 |
withTabs |
19464 | 6957 |
"return a string consisting of the receiver's characters |
735
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
678
diff
changeset
|
6958 |
where leading spaces are replaced by tabulator characters (assuming 8-col tabs). |
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
678
diff
changeset
|
6959 |
Notice: if the receiver does not contain any tabs, it is returned unchanged; |
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
678
diff
changeset
|
6960 |
otherwise a new string is returned. |
8874 | 6961 |
Limitation: only the very first spaces are replaced |
20012 | 6962 |
(i.e. if the receiver contains newLine characters, |
6963 |
no tabs are inserted after those lineBreaks)" |
|
8874 | 6964 |
|
6965 |
|idx "{ SmallInteger }" |
|
6966 |
nTabs "{ SmallInteger }" |
|
608 | 6967 |
newString| |
6968 |
||
6969 |
idx := self findFirst:[:c | (c ~~ Character space)]. |
|
6970 |
nTabs := (idx-1) // 8. |
|
5815
4acdd87a77d4
// can return negative numbers.
Stefan Vogel <sv@exept.de>
parents:
5795
diff
changeset
|
6971 |
nTabs <= 0 ifTrue:[^ self]. |
608 | 6972 |
|
6973 |
"any tabs" |
|
15523 | 6974 |
newString := self species new:(self size - (nTabs * 7)). |
608 | 6975 |
newString atAll:(1 to:nTabs) put:(Character tab). |
6976 |
newString replaceFrom:(nTabs + 1) with:self startingAt:(nTabs * 8 + 1). |
|
6977 |
^ newString |
|
6978 |
||
6979 |
" |
|
8874 | 6980 |
'12345678901234567890' withTabs |
6981 |
' 8901234567890' withTabs |
|
6982 |
' 901234567890' withTabs |
|
608 | 6983 |
' 67890' withTabs |
6984 |
' 7890' withTabs |
|
6985 |
' 890' withTabs |
|
6986 |
" |
|
9192 | 6987 |
! |
6988 |
||
6989 |
withTabsExpanded |
|
19464 | 6990 |
"return a string consisting of the receiver's characters, |
8874 | 6991 |
where all tabulator characters are expanded into spaces (assuming 8-col tabs). |
735
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
678
diff
changeset
|
6992 |
Notice: if the receiver does not contain any tabs, it is returned unchanged; |
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
678
diff
changeset
|
6993 |
otherwise a new string is returned. |
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
678
diff
changeset
|
6994 |
This does handle multiline strings." |
608 | 6995 |
|
5741
3a6086774d38
added #withTabsExpanded: (i.e. arg to specify tab-column width)
Claus Gittinger <cg@exept.de>
parents:
5739
diff
changeset
|
6996 |
^ self withTabsExpanded:8 |
3a6086774d38
added #withTabsExpanded: (i.e. arg to specify tab-column width)
Claus Gittinger <cg@exept.de>
parents:
5739
diff
changeset
|
6997 |
|
3a6086774d38
added #withTabsExpanded: (i.e. arg to specify tab-column width)
Claus Gittinger <cg@exept.de>
parents:
5739
diff
changeset
|
6998 |
" |
8874 | 6999 |
('1' , Character tab asString , 'x') withTabsExpanded |
7000 |
('12345' , Character tab asString , 'x') withTabsExpanded |
|
7001 |
('123456' , Character tab asString , 'x') withTabsExpanded |
|
7002 |
('1234567' , Character tab asString , 'x') withTabsExpanded |
|
7003 |
('12345678' , Character tab asString , 'x') withTabsExpanded |
|
7004 |
('123456789' , Character tab asString , 'x') withTabsExpanded |
|
5741
3a6086774d38
added #withTabsExpanded: (i.e. arg to specify tab-column width)
Claus Gittinger <cg@exept.de>
parents:
5739
diff
changeset
|
7005 |
|
3a6086774d38
added #withTabsExpanded: (i.e. arg to specify tab-column width)
Claus Gittinger <cg@exept.de>
parents:
5739
diff
changeset
|
7006 |
(String with:Character tab |
20012 | 7007 |
with:Character tab |
7008 |
with:$1) withTabsExpanded |
|
5741
3a6086774d38
added #withTabsExpanded: (i.e. arg to specify tab-column width)
Claus Gittinger <cg@exept.de>
parents:
5739
diff
changeset
|
7009 |
|
3a6086774d38
added #withTabsExpanded: (i.e. arg to specify tab-column width)
Claus Gittinger <cg@exept.de>
parents:
5739
diff
changeset
|
7010 |
(String with:Character tab |
20012 | 7011 |
with:$1 |
7012 |
with:Character tab |
|
7013 |
with:$2) withTabsExpanded |
|
5741
3a6086774d38
added #withTabsExpanded: (i.e. arg to specify tab-column width)
Claus Gittinger <cg@exept.de>
parents:
5739
diff
changeset
|
7014 |
|
3a6086774d38
added #withTabsExpanded: (i.e. arg to specify tab-column width)
Claus Gittinger <cg@exept.de>
parents:
5739
diff
changeset
|
7015 |
(String with:Character tab |
20012 | 7016 |
with:$1 |
7017 |
with:Character cr |
|
7018 |
with:Character tab |
|
7019 |
with:$2) withTabsExpanded |
|
5741
3a6086774d38
added #withTabsExpanded: (i.e. arg to specify tab-column width)
Claus Gittinger <cg@exept.de>
parents:
5739
diff
changeset
|
7020 |
" |
3a6086774d38
added #withTabsExpanded: (i.e. arg to specify tab-column width)
Claus Gittinger <cg@exept.de>
parents:
5739
diff
changeset
|
7021 |
|
3a6086774d38
added #withTabsExpanded: (i.e. arg to specify tab-column width)
Claus Gittinger <cg@exept.de>
parents:
5739
diff
changeset
|
7022 |
"Modified: 12.5.1996 / 13:05:10 / cg" |
9192 | 7023 |
! |
7024 |
||
7025 |
withTabsExpanded:numSpaces |
|
19464 | 7026 |
"return a string consisting of the receiver's characters, |
8874 | 7027 |
where all tabulator characters are expanded into spaces (assuming numSpaces-col tabs). |
5741
3a6086774d38
added #withTabsExpanded: (i.e. arg to specify tab-column width)
Claus Gittinger <cg@exept.de>
parents:
5739
diff
changeset
|
7028 |
Notice: if the receiver does not contain any tabs, it is returned unchanged; |
3a6086774d38
added #withTabsExpanded: (i.e. arg to specify tab-column width)
Claus Gittinger <cg@exept.de>
parents:
5739
diff
changeset
|
7029 |
otherwise a new string is returned. |
3a6086774d38
added #withTabsExpanded: (i.e. arg to specify tab-column width)
Claus Gittinger <cg@exept.de>
parents:
5739
diff
changeset
|
7030 |
This does handle multiline strings." |
3a6086774d38
added #withTabsExpanded: (i.e. arg to specify tab-column width)
Claus Gittinger <cg@exept.de>
parents:
5739
diff
changeset
|
7031 |
|
8874 | 7032 |
|col "{ SmallInteger }" |
1381 | 7033 |
str ch |
7034 |
dstIdx "{ SmallInteger }" |
|
7035 |
newSz "{ SmallInteger }" |
|
7036 |
sz "{ SmallInteger }" |
|
7037 |
hasEmphasis e| |
|
608 | 7038 |
|
7039 |
(self includes:(Character tab)) ifFalse:[^ self]. |
|
1381 | 7040 |
|
7041 |
sz := self size. |
|
7042 |
||
7043 |
"/ count the new size first, instead of |
|
7044 |
"/ multiple resizing (better for large strings) |
|
7045 |
||
7046 |
col := 1. newSz := 0. |
|
7047 |
1 to:sz do:[:srcIdx | |
|
20012 | 7048 |
ch := self at:srcIdx. |
7049 |
ch == Character tab ifFalse:[ |
|
7050 |
col := col + 1. |
|
7051 |
newSz := newSz + 1. |
|
7052 |
ch == Character cr ifTrue:[ |
|
7053 |
col := 1 |
|
7054 |
]. |
|
7055 |
] ifTrue:[ |
|
7056 |
(col \\ numSpaces) to:numSpaces do:[:ii | |
|
7057 |
newSz := newSz + 1. |
|
7058 |
col := col + 1 |
|
7059 |
]. |
|
7060 |
] |
|
608 | 7061 |
]. |
1381 | 7062 |
|
7063 |
str := self species new:newSz. |
|
7064 |
||
7065 |
hasEmphasis := self hasChangeOfEmphasis. |
|
7066 |
||
7067 |
col := 1. dstIdx := 1. |
|
7068 |
1 to:sz do:[:srcIdx | |
|
20012 | 7069 |
ch := self at:srcIdx. |
7070 |
||
7071 |
ch == Character tab ifFalse:[ |
|
7072 |
col := col + 1. |
|
7073 |
ch == Character cr ifTrue:[ |
|
7074 |
col := 1 |
|
7075 |
]. |
|
7076 |
hasEmphasis ifTrue:[ |
|
7077 |
e := self emphasisAt:srcIdx. |
|
7078 |
str emphasisAt:dstIdx put:e |
|
7079 |
]. |
|
7080 |
str at:dstIdx put:ch. |
|
7081 |
dstIdx := dstIdx + 1 |
|
7082 |
] ifTrue:[ |
|
7083 |
(col \\ numSpaces) to:numSpaces do:[:ii | |
|
7084 |
str at:dstIdx put:Character space. |
|
7085 |
dstIdx := dstIdx + 1. |
|
7086 |
col := col + 1 |
|
7087 |
]. |
|
7088 |
] |
|
1381 | 7089 |
]. |
7090 |
^ str |
|
608 | 7091 |
|
7092 |
" |
|
8874 | 7093 |
('1' , Character tab asString , 'x') withTabsExpanded |
7094 |
('1' , Character tab asString , 'x') withTabsExpanded:4 |
|
7095 |
('12345' , Character tab asString , 'x') withTabsExpanded |
|
7096 |
('123456' , Character tab asString , 'x') withTabsExpanded |
|
7097 |
('1234567' , Character tab asString , 'x') withTabsExpanded |
|
7098 |
('12345678' , Character tab asString , 'x') withTabsExpanded |
|
7099 |
('123456789' , Character tab asString , 'x') withTabsExpanded |
|
735
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
678
diff
changeset
|
7100 |
|
608 | 7101 |
(String with:Character tab |
20012 | 7102 |
with:Character tab |
7103 |
with:$1) withTabsExpanded |
|
608 | 7104 |
|
7105 |
(String with:Character tab |
|
20012 | 7106 |
with:$1 |
7107 |
with:Character tab |
|
7108 |
with:$2) withTabsExpanded |
|
735
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
678
diff
changeset
|
7109 |
|
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
678
diff
changeset
|
7110 |
(String with:Character tab |
20012 | 7111 |
with:$1 |
7112 |
with:Character cr |
|
7113 |
with:Character tab |
|
7114 |
with:$2) withTabsExpanded |
|
608 | 7115 |
" |
735
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
678
diff
changeset
|
7116 |
|
1381 | 7117 |
"Modified: 12.5.1996 / 13:05:10 / cg" |
9192 | 7118 |
! |
7119 |
||
13987 | 7120 |
withoutAllSpaces |
16855 | 7121 |
"return a copy of the receiver with all whitespace removed" |
7122 |
||
13987 | 7123 |
^ self asCollectionOfWords asStringWith:''. |
7124 |
"/ |col string| |
|
7125 |
"/ |
|
7126 |
"/ col := self asCollectionOfWords. |
|
7127 |
"/ string := String new. |
|
7128 |
"/ col do:[:el | |
|
7129 |
"/ string := string,el |
|
7130 |
"/ ]. |
|
7131 |
"/ ^string |
|
7132 |
||
7133 |
" |
|
7134 |
'hello wwww' withoutAllSpaces |
|
7135 |
'hel lo www w' withoutAllSpaces |
|
7136 |
" |
|
7137 |
||
7138 |
"Modified: / 18.7.1998 / 22:53:08 / cg" |
|
7139 |
! |
|
7140 |
||
7141 |
withoutCEscapes |
|
15628 | 7142 |
"return a new string consisting of receiver's characters |
13987 | 7143 |
with all \X-character escapes replaced by corresponding-characters. |
7144 |
(similar to the way C-language Strings are converted). |
|
7145 |
The following escapes are supported: |
|
20012 | 7146 |
\r return character |
7147 |
\n newline character |
|
7148 |
\b backspace character |
|
7149 |
\f formfeed character |
|
7150 |
\t tab character |
|
7151 |
\e escape character |
|
7152 |
\\ the \ character itself |
|
7153 |
\nnn three digit octal number defining the characters ascii value |
|
7154 |
\xnn two digit hex number defining the characters ascii value |
|
7155 |
\unnnn four digit hex number defining the characters unicode value |
|
7156 |
\Unnnnnnnn eight digit hex number defining the characters unicode value |
|
7157 |
\other other |
|
13987 | 7158 |
|
7159 |
Notice, that \' is NOT a valid escape, since the general syntax of |
|
7160 |
string constants is not affected by this method. |
|
7161 |
||
7162 |
Although easily implementable, this is NOT done automatically |
|
7163 |
by the compiler (due to a lack of a language standard for this). |
|
7164 |
However, the compiler may detect sends ot #withEscapes to string literals |
|
7165 |
and place a modified string constant into the binary/byte-code. |
|
7166 |
Therefore, no runtime penalty will be payed for using these escapes. |
|
7167 |
(not in pre 2.11 versions) |
|
16863 | 7168 |
|
7169 |
This is the opposite of withCEscapes. |
|
17000 | 7170 |
|
7171 |
Sigh: this is named completely wrong (opposite naming of withCRs/witoutCRs), |
|
20012 | 7172 |
but it cannot be changed easily, as these methods are already used heavily |
13987 | 7173 |
" |
7174 |
||
7175 |
|val "{ SmallInteger }" |
|
7176 |
in out nextChar nDigits| |
|
7177 |
||
7178 |
" |
|
7179 |
first, see if there is any escape; if not, return the receiver unchanged |
|
7180 |
" |
|
7181 |
(self includes:$\) ifFalse:[^ self ]. |
|
7182 |
||
7183 |
self hasChangeOfEmphasis ifTrue:[ self error:'emphasis not supported' ]. |
|
7184 |
out := CharacterWriteStream on:(String new:self size - 1). |
|
7185 |
||
7186 |
in := ReadStream on:self. |
|
7187 |
[in atEnd] whileFalse:[ |
|
20012 | 7188 |
nextChar := in next. |
7189 |
nextChar == $\ ifTrue:[ |
|
7190 |
in atEnd ifTrue:[ |
|
7191 |
] ifFalse:[ |
|
7192 |
nextChar := in next. |
|
7193 |
nextChar == $r ifTrue:[ |
|
7194 |
nextChar := Character return |
|
7195 |
] ifFalse:[ nextChar == $n ifTrue:[ |
|
7196 |
nextChar := Character nl |
|
7197 |
] ifFalse:[ nextChar == $b ifTrue:[ |
|
7198 |
nextChar := Character backspace |
|
7199 |
] ifFalse:[ nextChar == $f ifTrue:[ |
|
7200 |
nextChar := Character newPage |
|
7201 |
] ifFalse:[ nextChar == $t ifTrue:[ |
|
7202 |
nextChar := Character tab |
|
7203 |
] ifFalse:[ nextChar == $e ifTrue:[ |
|
7204 |
nextChar := Character esc |
|
7205 |
] ifFalse:[ |
|
7206 |
nextChar == $0 ifTrue:[ |
|
7207 |
val := 0. |
|
7208 |
nextChar := in peek. |
|
7209 |
nDigits := 1. |
|
7210 |
[nextChar notNil and:[nextChar isDigit and:[nDigits <= 3]]] whileTrue:[ |
|
7211 |
val := (val * 8) + nextChar digitValue. |
|
7212 |
nextChar := in nextPeek. |
|
7213 |
nDigits := nDigits + 1. |
|
7214 |
]. |
|
7215 |
nextChar := Character value:val. |
|
7216 |
] ifFalse:[ |
|
7217 |
val := 0. |
|
7218 |
nextChar == $x ifTrue:[ |
|
7219 |
2 timesRepeat:[ |
|
7220 |
nextChar := in next. |
|
7221 |
val := (val * 16) + nextChar digitValue. |
|
7222 |
]. |
|
7223 |
nextChar := Character value:val. |
|
7224 |
] ifFalse:[ |
|
7225 |
nextChar == $u ifTrue:[ |
|
7226 |
4 timesRepeat:[ |
|
7227 |
nextChar := in next. |
|
7228 |
val := (val * 16) + nextChar digitValue. |
|
7229 |
]. |
|
7230 |
nextChar := Character value:val. |
|
7231 |
] ifFalse:[ |
|
7232 |
nextChar == $U ifTrue:[ |
|
7233 |
8 timesRepeat:[ |
|
7234 |
nextChar := in next. |
|
7235 |
val := (val * 16) + nextChar digitValue. |
|
7236 |
]. |
|
7237 |
nextChar := Character value:val. |
|
7238 |
] |
|
7239 |
] |
|
7240 |
] |
|
7241 |
] |
|
7242 |
]]]]]]. |
|
7243 |
]. |
|
7244 |
]. |
|
7245 |
out nextPut:nextChar. |
|
13987 | 7246 |
]. |
7247 |
^ out contents |
|
7248 |
||
7249 |
" |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
7250 |
'hello world' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
7251 |
'hello\world' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
7252 |
'hello\world\' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
7253 |
'hello world\' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
7254 |
'hello\tworld' withoutCEscapes |
13987 | 7255 |
'hello\nworld\na\n\tnice\n\t\tstring' withoutCEscapes |
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
7256 |
'hello\tworld\n' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
7257 |
'hello\010world' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
7258 |
'hello\r\nworld' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
7259 |
'hello\r\n\x08world' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
7260 |
'0\x080' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
7261 |
'0\u12340' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
7262 |
'0\U123456780' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
7263 |
'0\0a' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
7264 |
'0\00a' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
7265 |
'0\000a' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
7266 |
'0\0000a' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
7267 |
'0\00000a' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
7268 |
'0\03770' withoutCEscapes |
13987 | 7269 |
" |
7270 |
||
7271 |
"Created: / 25-01-2012 / 10:41:44 / cg" |
|
7272 |
! |
|
7273 |
||
9192 | 7274 |
withoutCRs |
19464 | 7275 |
"return a new collection consisting of receiver's elements |
9192 | 7276 |
with all cr-characters replaced by \-characters. |
7277 |
This is the reverse operation of withCRs." |
|
7278 |
||
16759 | 7279 |
^ self copyReplaceAll:(Character cr) with:$\ ifNone:self |
12319
18b80c2ce1e3
oops #withoutSeparators #withoutSpaces were noops
Claus Gittinger <cg@exept.de>
parents:
12317
diff
changeset
|
7280 |
|
9192 | 7281 |
" |
7282 |
'hello |
|
7283 |
world' withoutCRs |
|
7284 |
" |
|
7285 |
||
7286 |
"Modified: / 18.7.1998 / 22:53:08 / cg" |
|
7287 |
! |
|
7288 |
||
7289 |
withoutLeadingSeparators |
|
608 | 7290 |
"return a copy of myself without leading separators. |
7291 |
Notice: this does remove tabs, newline or any other whitespace. |
|
7292 |
Returns an empty string, if the receiver consist only of whitespace." |
|
7293 |
||
7294 |
|index| |
|
7295 |
||
7296 |
index := self indexOfNonSeparatorStartingAt:1. |
|
7297 |
index ~~ 0 ifTrue:[ |
|
20012 | 7298 |
index == 1 ifTrue:[ |
7299 |
^ self |
|
7300 |
]. |
|
7301 |
^ self copyFrom:index |
|
608 | 7302 |
]. |
7303 |
^ '' |
|
7304 |
||
7305 |
" |
|
8874 | 7306 |
' foo ' withoutLeadingSeparators |
7307 |
'foo ' withoutLeadingSeparators |
|
7308 |
' foo' withoutLeadingSeparators |
|
7309 |
' ' withoutLeadingSeparators |
|
7310 |
'foo' withoutLeadingSeparators |
|
7311 |
(' ' , Character tab asString , ' foo ') withoutLeadingSeparators inspect |
|
608 | 7312 |
" |
9192 | 7313 |
! |
7314 |
||
7315 |
withoutMatchEscapes |
|
4117 | 7316 |
"return a copy of the receiver with all $\ removed or |
7317 |
the receiver, if there are none." |
|
7318 |
||
5342
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
7319 |
|in out c escape| |
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
7320 |
|
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
7321 |
escape := self class matchEscapeCharacter. |
4117 | 7322 |
|
7323 |
in := self readStream. |
|
8222
f2c454a9a038
replaced '' writeStream by String writeStream
Claus Gittinger <cg@exept.de>
parents:
8124
diff
changeset
|
7324 |
out := self species writeStream. |
4117 | 7325 |
[in atEnd] whileFalse:[ |
20012 | 7326 |
c := in next. |
7327 |
c == escape ifTrue:[ |
|
7328 |
in atEnd ifFalse:[ |
|
7329 |
c := in next. |
|
7330 |
] |
|
7331 |
]. |
|
7332 |
out nextPut:c. |
|
4117 | 7333 |
]. |
7334 |
^ out contents. |
|
7335 |
||
7336 |
" |
|
8874 | 7337 |
'*foo' withoutMatchEscapes |
7338 |
'\*foo' withoutMatchEscapes |
|
7339 |
'*foo' withoutMatchEscapes |
|
7340 |
'\\*foo' withoutMatchEscapes |
|
7341 |
'foo*' withoutMatchEscapes |
|
7342 |
'foo\*' withoutMatchEscapes |
|
7343 |
'foo\' withoutMatchEscapes |
|
7344 |
'f\*o\*o' withoutMatchEscapes |
|
4117 | 7345 |
" |
7346 |
||
7347 |
"Modified: 30.6.1997 / 13:40:23 / cg" |
|
9192 | 7348 |
! |
7349 |
||
7350 |
withoutPrefix:aString |
|
19677 | 7351 |
"if the receiver startsWith aString, return a copy without it. |
9192 | 7352 |
Otherwise return the receiver" |
7353 |
||
7354 |
(self startsWith:aString) ifTrue:[ |
|
20012 | 7355 |
^ self copyFrom:aString size+1 |
9192 | 7356 |
]. |
7357 |
^ self |
|
7358 |
||
7359 |
" |
|
7360 |
'helloworld' withoutPrefix:'hello' |
|
7361 |
'helloworld' withoutPrefix:'foo' |
|
7362 |
" |
|
19677 | 7363 |
|
7364 |
"Modified: / 30-04-2016 / 10:01:00 / cg" |
|
9192 | 7365 |
! |
7366 |
||
10671 | 7367 |
withoutQuotes |
17000 | 7368 |
"/ remove quotes ($" and $') from the front and end of myself (if matching)" |
7369 |
||
7370 |
|firstChar| |
|
7371 |
||
17024 | 7372 |
self isEmpty ifTrue:[^ self]. |
7373 |
||
17000 | 7374 |
firstChar := self first. |
7375 |
((firstChar == $") or:[firstChar == $']) ifFalse:[^ self]. |
|
7376 |
||
7377 |
self last == firstChar ifTrue:[ |
|
20012 | 7378 |
^ self copyFrom:2 to:(self size-1) |
10671 | 7379 |
]. |
17000 | 7380 |
^ self |
7381 |
||
7382 |
"/ |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
7383 |
"/ '"hello"' withoutQuotes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
7384 |
"/ '''hello''' withoutQuotes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
7385 |
"/ 'hello' withoutQuotes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
7386 |
"/ '"hello' withoutQuotes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
7387 |
"/ 'hello"' withoutQuotes |
17000 | 7388 |
"/ |
10671 | 7389 |
! |
7390 |
||
9192 | 7391 |
withoutSeparators |
608 | 7392 |
"return a copy of myself without leading and trailing whitespace. |
4156 | 7393 |
(but whiteSpace in-between is preserved) |
608 | 7394 |
Whitespace is space, tab, newline, formfeed. |
7395 |
Use withoutSpaces, if you want to remove spaces only." |
|
7396 |
||
12319
18b80c2ce1e3
oops #withoutSeparators #withoutSpaces were noops
Claus Gittinger <cg@exept.de>
parents:
12317
diff
changeset
|
7397 |
^ self trimForWhich:[:ch | ch isSeparator] |
608 | 7398 |
|
7399 |
" |
|
8874 | 7400 |
' foo ' withoutSeparators |
7401 |
' foo' withoutSeparators |
|
7402 |
'foo ' withoutSeparators |
|
7403 |
' ' withoutSeparators |
|
7404 |
(' foo' , Character tab asString , ' ') withoutSeparators inspect |
|
12319
18b80c2ce1e3
oops #withoutSeparators #withoutSpaces were noops
Claus Gittinger <cg@exept.de>
parents:
12317
diff
changeset
|
7405 |
' foo ' asUnicode16String withoutSeparators |
608 | 7406 |
" |
9192 | 7407 |
! |
7408 |
||
7409 |
withoutSpaces |
|
608 | 7410 |
"return a copy of myself without leading and trailing spaces. |
4156 | 7411 |
(but spaces in-between are preserved) |
608 | 7412 |
Notice: this does NOT remove tabs, newline or any other whitespace. |
7413 |
Use withoutSeparators for this." |
|
7414 |
||
12319
18b80c2ce1e3
oops #withoutSeparators #withoutSpaces were noops
Claus Gittinger <cg@exept.de>
parents:
12317
diff
changeset
|
7415 |
^ self trimForWhich:[:ch | ch == Character space] |
608 | 7416 |
|
7417 |
" |
|
8874 | 7418 |
' foo ' withoutSpaces |
7419 |
'foo ' withoutSpaces |
|
7420 |
' foo' withoutSpaces |
|
7421 |
' ' withoutSpaces |
|
7422 |
'a b' withoutSpaces |
|
7423 |
(' foo' , Character tab asString , ' ') withoutSpaces inspect |
|
608 | 7424 |
" |
9192 | 7425 |
! |
7426 |
||
7427 |
withoutTrailingSeparators |
|
915
1ac1c7e942f4
added withoutTrailingSeparators
Claus Gittinger <cg@exept.de>
parents:
828
diff
changeset
|
7428 |
"return a copy of myself without trailing separators. |
1ac1c7e942f4
added withoutTrailingSeparators
Claus Gittinger <cg@exept.de>
parents:
828
diff
changeset
|
7429 |
Notice: this does remove tabs, newline or any other whitespace. |
1ac1c7e942f4
added withoutTrailingSeparators
Claus Gittinger <cg@exept.de>
parents:
828
diff
changeset
|
7430 |
Returns an empty string, if the receiver consist only of whitespace." |
1ac1c7e942f4
added withoutTrailingSeparators
Claus Gittinger <cg@exept.de>
parents:
828
diff
changeset
|
7431 |
|
12188 | 7432 |
^ self withoutTrailingForWhich:[:ch | ch isSeparator] |
915
1ac1c7e942f4
added withoutTrailingSeparators
Claus Gittinger <cg@exept.de>
parents:
828
diff
changeset
|
7433 |
|
1ac1c7e942f4
added withoutTrailingSeparators
Claus Gittinger <cg@exept.de>
parents:
828
diff
changeset
|
7434 |
" |
8874 | 7435 |
' foo ' withoutTrailingSeparators |
7436 |
'foo ' withoutTrailingSeparators |
|
7437 |
' foo' withoutTrailingSeparators |
|
7438 |
' ' withoutTrailingSeparators |
|
7439 |
'foo' withoutTrailingSeparators |
|
7440 |
(' ' , Character tab asString , ' foo ') withoutTrailingSeparators inspect |
|
7441 |
(' foo' , Character tab asString) withoutTrailingSeparators inspect |
|
915
1ac1c7e942f4
added withoutTrailingSeparators
Claus Gittinger <cg@exept.de>
parents:
828
diff
changeset
|
7442 |
" |
9192 | 7443 |
! ! |
7444 |
||
20155 | 7445 |
|
9192 | 7446 |
!CharacterArray methodsFor:'substring searching'! |
7447 |
||
15055 | 7448 |
findRangeOfString:subString |
7449 |
"find a substring. if found, return the start- and endIndex; |
|
7450 |
if not found, return an empty interval." |
|
7451 |
||
7452 |
^ self rangeOfSubCollection:subString startingAt:1 ifAbsent:[0 to:-1] caseSensitive:true |
|
7453 |
||
7454 |
" |
|
7455 |
'hello world' findRangeOfString:'llo' |
|
7456 |
'hello world' findRangeOfString:'ole' |
|
7457 |
" |
|
7458 |
! |
|
7459 |
||
9192 | 7460 |
findString:subString |
7461 |
"find a substring. if found, return the index; |
|
7462 |
if not found, return 0." |
|
7463 |
||
7464 |
^ self indexOfSubCollection:subString startingAt:1 ifAbsent:0 caseSensitive:true |
|
7465 |
||
7466 |
" |
|
7467 |
'hello world' findString:'llo' |
|
7468 |
'hello world' findString:'ole' |
|
7469 |
" |
|
7470 |
! |
|
608 | 7471 |
|
15734 | 7472 |
findString:subString caseSensitive:caseSensitive |
7473 |
"find a substring. if found, return the index; |
|
7474 |
if not found, return 0." |
|
7475 |
||
7476 |
^ self indexOfSubCollection:subString startingAt:1 ifAbsent:0 caseSensitive:caseSensitive |
|
7477 |
||
7478 |
" |
|
7479 |
'hello world' findString:'LLo' caseSensitive:true |
|
7480 |
'hello world' findString:'LLo' caseSensitive:false |
|
7481 |
" |
|
7482 |
! |
|
7483 |
||
608 | 7484 |
findString:subString ifAbsent:exceptionBlock |
7485 |
"find a substring. If found, return the index; |
|
7486 |
if not found, return the result of evaluating exceptionBlock." |
|
7487 |
||
9087
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
7488 |
^ self indexOfSubCollection:subString startingAt:1 ifAbsent:exceptionBlock caseSensitive:true |
608 | 7489 |
! |
7490 |
||
11018 | 7491 |
findString:subString ignoreCase:ignoreCase |
15734 | 7492 |
<resource: #obsolete> |
11018 | 7493 |
"find a substring. if found, return the index; |
7494 |
if not found, return 0." |
|
7495 |
||
7496 |
^ self indexOfSubCollection:subString startingAt:1 ifAbsent:0 caseSensitive:ignoreCase not |
|
7497 |
||
7498 |
" |
|
7499 |
'hello world' findString:'LLo' ignoreCase:false |
|
7500 |
'hello world' findString:'LLo' ignoreCase:true |
|
7501 |
" |
|
7502 |
! |
|
7503 |
||
608 | 7504 |
findString:subString startingAt:index |
7505 |
"find a substring, starting at index. if found, return the index; |
|
7506 |
if not found, return 0." |
|
7507 |
||
9087
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
7508 |
^ self indexOfSubCollection:subString startingAt:index ifAbsent:0 caseSensitive:true |
608 | 7509 |
|
7510 |
" |
|
9192 | 7511 |
'hello yello' findString:'llo' startingAt:1 |
7512 |
'hello yello' findString:'llo' startingAt:5 |
|
7513 |
'hello yello' findString:'llo' startingAt:15 |
|
7514 |
" |
|
7515 |
! |
|
608 | 7516 |
|
7517 |
findString:subString startingAt:index ifAbsent:exceptionBlock |
|
7518 |
"find a substring, starting at index. if found, return the index; |
|
7519 |
if not found, return the result of evaluating exceptionBlock." |
|
7520 |
||
9087
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
7521 |
^ self indexOfSubCollection:subString startingAt:index ifAbsent:exceptionBlock caseSensitive:true |
608 | 7522 |
! |
7523 |
||
12177 | 7524 |
findString:subString startingAt:index ifAbsent:exceptionBlock caseSensitive:caseSensitive |
10929 | 7525 |
"find a substring, starting at index. if found, return the index; |
7526 |
if not found, return the result of evaluating exceptionBlock." |
|
7527 |
||
7528 |
^ self indexOfSubCollection:subString startingAt:index ifAbsent:exceptionBlock caseSensitive:caseSensitive |
|
7529 |
! |
|
7530 |
||
608 | 7531 |
includesString:aString |
7532 |
"return true, if a substring is contained in the receiver" |
|
7533 |
||
9087
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
7534 |
^ (self indexOfSubCollection:aString startingAt:1 ifAbsent:0 caseSensitive:true) ~~ 0 |
608 | 7535 |
|
7536 |
" |
|
12177 | 7537 |
'hello world' includesString:'hel' |
8874 | 7538 |
'hello world' includesString:'rld' |
9192 | 7539 |
'hello world' includesString:'llo' |
7540 |
'hello world' includesString:'LLO' |
|
12177 | 7541 |
'hello world' includesString:'' |
9192 | 7542 |
" |
7543 |
! |
|
9191 | 7544 |
|
7545 |
includesString:aString caseSensitive:caseSensitive |
|
9087
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
7546 |
"return true, if a substring is contained in the receiver" |
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
7547 |
|
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
7548 |
^ (self indexOfSubCollection:aString startingAt:1 ifAbsent:0 caseSensitive:caseSensitive) ~~ 0 |
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
7549 |
|
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
7550 |
" |
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
7551 |
'hello world' includesString:'hel' caseSensitive:true |
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
7552 |
'hello world' includesString:'HEL' caseSensitive:true |
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
7553 |
'hello world' includesString:'HEL' caseSensitive:false |
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
7554 |
|
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
7555 |
'hello world' includesString:'wor' caseSensitive:true |
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
7556 |
'hello world' includesString:'WOR' caseSensitive:true |
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
7557 |
'hello world' includesString:'WOR' caseSensitive:false |
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
7558 |
" |
9192 | 7559 |
! |
7560 |
||
17486 | 7561 |
indexOfString: aString |
7562 |
"VSE and V'age compatibility" |
|
7563 |
"find a substring. If found, return the index; if not found, return 0." |
|
7564 |
||
18939 | 7565 |
^ self indexOfSubCollection: aString startingAt:1 ifAbsent:[0] |
7566 |
! |
|
7567 |
||
7568 |
indexOfString:aString ifAbsent:exceptionValue |
|
7569 |
"VSE and V'age compatibility" |
|
7570 |
"find a substring. If found, return the index; if not found, return 0." |
|
7571 |
||
7572 |
^ self indexOfSubCollection: aString startingAt:1 ifAbsent:exceptionValue |
|
7573 |
||
7574 |
" |
|
7575 |
'abcdefabcdef' indexOfString:'fab' ifAbsent:[999] |
|
7576 |
'abcdefabcdef' indexOfString:'xxx' ifAbsent:[999] |
|
7577 |
" |
|
17486 | 7578 |
! |
7579 |
||
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
7580 |
indexOfString:aString startingAt:startIndex |
18721 | 7581 |
"VSE and V'age compatibility" |
7582 |
"find a substring. If found, return the index; if not found, return 0." |
|
7583 |
||
7584 |
^ self indexOfSubCollection:aString startingAt:startIndex ifAbsent:[0] |
|
7585 |
! |
|
7586 |
||
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
7587 |
indexOfString:aString startingAt:startIndex ifAbsent:exceptionalValue |
18721 | 7588 |
"VSE and V'age compatibility" |
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
7589 |
"find a substring. |
18721 | 7590 |
If found, return the index; if not found, the value from exceptionalValue." |
7591 |
||
7592 |
^ self indexOfSubCollection:aString startingAt:startIndex ifAbsent:exceptionalValue |
|
7593 |
! |
|
7594 |
||
18671 | 7595 |
indexOfSubCollection:subString caseSensitive:caseSensitive |
7596 |
"find a substring, starting at index. if found, return the index; |
|
7597 |
if not found, return the result of evaluating exceptionBlock. |
|
7598 |
This is a q&d hack - not very efficient" |
|
7599 |
||
7600 |
^ self indexOfSubCollection:subString startingAt:1 ifAbsent:[0] caseSensitive:caseSensitive. |
|
7601 |
! |
|
7602 |
||
9192 | 7603 |
indexOfSubCollection:subString startingAt:index ifAbsent:exceptionBlock |
7604 |
"find a substring, starting at index. if found, return the index; |
|
7605 |
if not found, return the result of evaluating exceptionBlock. |
|
7606 |
This is a q&d hack - not very efficient" |
|
7607 |
||
7608 |
^ self indexOfSubCollection:subString startingAt:index ifAbsent:exceptionBlock caseSensitive:true. |
|
7609 |
! |
|
7610 |
||
7611 |
indexOfSubCollection:subString startingAt:index ifAbsent:exceptionBlock caseSensitive:caseSensitive |
|
9087
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
7612 |
"find a substring, starting at index. if found, return the index; |
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
7613 |
if not found, return the result of evaluating exceptionBlock. |
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
7614 |
This is a q&d hack - not very efficient" |
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
7615 |
|
608 | 7616 |
|firstChar found |
7617 |
startIndex "{ Class: SmallInteger }" |
|
7618 |
subSize "{ Class: SmallInteger }" |
|
7619 |
mySize "{ Class: SmallInteger }" |
|
10703
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
7620 |
runIdx "{ Class: SmallInteger }" |
9087
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
7621 |
tester| |
608 | 7622 |
|
7623 |
subSize := subString size. |
|
12177 | 7624 |
subSize == 0 ifTrue:[ |
20012 | 7625 |
subString isString ifFalse:[ |
7626 |
self error:'non string argument' mayProceed:true. |
|
7627 |
]. |
|
7628 |
"empty string does not match" |
|
7629 |
^ 0. |
|
7630 |
"empty string matches" |
|
15433 | 7631 |
"/ ^ index |
6122
a98958f1c85f
handle non-string arg to indexOfSubString:
Claus Gittinger <cg@exept.de>
parents:
6119
diff
changeset
|
7632 |
]. |
9087
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
7633 |
|
608 | 7634 |
mySize := self size. |
7635 |
firstChar := subString at:1. |
|
9087
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
7636 |
caseSensitive ifTrue:[ |
20012 | 7637 |
tester := [:c1 :c2 | c1 = c2 ]. |
7638 |
startIndex := self indexOf:firstChar startingAt:index. |
|
9087
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
7639 |
] ifFalse:[ |
20012 | 7640 |
tester := [:c1 :c2 | c1 sameAs: c2 ]. |
7641 |
startIndex := self findFirst:[:c | c sameAs:firstChar] startingAt:index. |
|
9087
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
7642 |
]. |
608 | 7643 |
[startIndex == 0] whileFalse:[ |
20012 | 7644 |
runIdx := startIndex. |
7645 |
found := true. |
|
7646 |
1 to:subSize do:[:i | |
|
7647 |
runIdx > mySize ifTrue:[ |
|
7648 |
found := false |
|
7649 |
] ifFalse:[ |
|
7650 |
(tester value:(subString at:i) value:(self at:runIdx)) ifFalse:[ |
|
7651 |
found := false |
|
7652 |
] |
|
7653 |
]. |
|
7654 |
runIdx := runIdx + 1 |
|
7655 |
]. |
|
7656 |
found ifTrue:[ |
|
7657 |
^ startIndex |
|
7658 |
]. |
|
7659 |
caseSensitive ifTrue:[ |
|
7660 |
startIndex := self indexOf:firstChar startingAt:(startIndex + 1) |
|
7661 |
] ifFalse:[ |
|
7662 |
startIndex := self findFirst:[:c | c sameAs:firstChar] startingAt:(startIndex + 1). |
|
7663 |
]. |
|
608 | 7664 |
]. |
7665 |
^ exceptionBlock value |
|
1005
4507487e0be6
more on encoding/decoding (still unfinished)
Claus Gittinger <cg@exept.de>
parents:
997
diff
changeset
|
7666 |
|
4507487e0be6
more on encoding/decoding (still unfinished)
Claus Gittinger <cg@exept.de>
parents:
997
diff
changeset
|
7667 |
"Modified: 23.2.1996 / 15:35:15 / cg" |
9192 | 7668 |
! |
7669 |
||
17191 | 7670 |
indexOfSubCollection:subString startingAt:index ifAbsent:exceptionBlock caseSensitive:caseSensitive ignoreDiacritics:ignoreDiacritics |
7671 |
"find a substring, starting at index. if found, return the index; |
|
7672 |
if not found, return the result of evaluating exceptionBlock. |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
7673 |
This is a q&d hack - not very efficient |
17191 | 7674 |
(see implementation in string, for a much faster algorithm)" |
7675 |
||
7676 |
|firstChar found |
|
7677 |
startIndex "{ Class: SmallInteger }" |
|
7678 |
subSize "{ Class: SmallInteger }" |
|
7679 |
mySize "{ Class: SmallInteger }" |
|
7680 |
runIdx "{ Class: SmallInteger }" |
|
7681 |
tester charMap firstCharMapped findNextIndex| |
|
7682 |
||
7683 |
subSize := subString size. |
|
7684 |
subSize == 0 ifTrue:[ |
|
20012 | 7685 |
subString isString ifFalse:[ |
7686 |
self error:'non string argument' mayProceed:true. |
|
7687 |
]. |
|
7688 |
"empty string does not match" |
|
7689 |
^ 0. |
|
7690 |
"empty string matches" |
|
17191 | 7691 |
"/ ^ index |
7692 |
]. |
|
7693 |
||
7694 |
mySize := self size. |
|
7695 |
firstChar := subString at:1. |
|
7696 |
ignoreDiacritics ifTrue:[ |
|
20012 | 7697 |
caseSensitive ifTrue:[ |
7698 |
charMap := [:ch | ch asLowercase withoutDiacritics]. |
|
7699 |
] ifFalse:[ |
|
7700 |
charMap := [:ch | ch withoutDiacritics]. |
|
7701 |
]. |
|
7702 |
tester := [:c1 :c2 | (charMap value:c1) = (charMap value:c2) ]. |
|
7703 |
firstCharMapped := (charMap value:firstChar). |
|
7704 |
findNextIndex := [:index | self findFirst:[:ch | (charMap value:ch) = firstCharMapped] startingAt:index]. |
|
17191 | 7705 |
] ifFalse:[ |
20012 | 7706 |
caseSensitive ifTrue:[ |
7707 |
tester := [:c1 :c2 | c1 = c2 ]. |
|
7708 |
findNextIndex := [:index | self indexOf:firstChar startingAt:index]. |
|
7709 |
] ifFalse:[ |
|
7710 |
tester := [:c1 :c2 | c1 sameAs: c2 ]. |
|
7711 |
findNextIndex := [:index | self findFirst:[:c | c sameAs:firstChar] startingAt:index]. |
|
7712 |
]. |
|
17191 | 7713 |
]. |
7714 |
startIndex := findNextIndex value:index. |
|
7715 |
[startIndex == 0] whileFalse:[ |
|
20012 | 7716 |
runIdx := startIndex. |
7717 |
found := true. |
|
7718 |
1 to:subSize do:[:i | |
|
7719 |
runIdx > mySize ifTrue:[ |
|
7720 |
found := false |
|
7721 |
] ifFalse:[ |
|
7722 |
(tester value:(subString at:i) value:(self at:runIdx)) ifFalse:[ |
|
7723 |
found := false |
|
7724 |
] |
|
7725 |
]. |
|
7726 |
runIdx := runIdx + 1 |
|
7727 |
]. |
|
7728 |
found ifTrue:[ |
|
7729 |
^ startIndex |
|
7730 |
]. |
|
7731 |
startIndex := findNextIndex value:(startIndex + 1) |
|
17191 | 7732 |
]. |
7733 |
^ exceptionBlock value |
|
7734 |
||
7735 |
" |
|
17600 | 7736 |
'bla depot bla dépots' indexOfSubCollection:'dep' startingAt:1 ifAbsent:0 caseSensitive:false ignoreDiacritics:true. 5 |
7737 |
'bla depot bla dépots' indexOfSubCollection:'dep' startingAt:6 ifAbsent:0 caseSensitive:false ignoreDiacritics:true. 0 |
|
7738 |
||
7739 |
'bla depot bla dépots' indexOfSubCollection:'dep' startingAt:1 ifAbsent:0 caseSensitive:false ignoreDiacritics:false. 5 |
|
7740 |
'bla depot bla dépots' indexOfSubCollection:'dep' startingAt:6 ifAbsent:0 caseSensitive:false ignoreDiacritics:false. 0 |
|
17191 | 7741 |
" |
7742 |
! |
|
7743 |
||
18939 | 7744 |
lastIndexOfString:aString |
7745 |
"VSE and V'age compatibility" |
|
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
7746 |
"find the last occurrence of substring. |
18939 | 7747 |
If found, return the index; if not found, return 0." |
7748 |
||
7749 |
^ self lastIndexOfSubCollection:aString startingAt:(self size-aString size+1) ifAbsent:[0] |
|
7750 |
||
7751 |
" 123456789012 |
|
7752 |
'abcdefabcdef' lastIndexOfString:'abc' |
|
7753 |
'abcdefabcdef' lastIndexOfString:'abc' startingAt:6 |
|
7754 |
'abcdefabcdef' lastIndexOfString:'xxx' startingAt:6 |
|
7755 |
" |
|
7756 |
! |
|
7757 |
||
7758 |
lastIndexOfString:aString ifAbsent:exceptionValue |
|
7759 |
"VSE and V'age compatibility" |
|
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
7760 |
"find the last occurrence of substring. |
18939 | 7761 |
If found, return the index; if not found, return 0." |
7762 |
||
7763 |
^ self lastIndexOfSubCollection:aString startingAt:(self size-aString size+1) ifAbsent:exceptionValue |
|
7764 |
||
7765 |
" 123456789012 |
|
7766 |
'abcdefabcdef' lastIndexOfString:'abc' ifAbsent:[999] |
|
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
7767 |
'abcdefabcdef' lastIndexOfString:'xxx' ifAbsent:[999] |
18939 | 7768 |
" |
7769 |
! |
|
7770 |
||
7771 |
lastIndexOfString:aString startingAt:startIndex |
|
7772 |
"VSE and V'age compatibility" |
|
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
7773 |
"find the last occurrence of a substring. |
18939 | 7774 |
If found, return the index; if not found, return 0." |
7775 |
||
7776 |
^ self lastIndexOfSubCollection:aString startingAt:startIndex ifAbsent:[0] |
|
7777 |
||
7778 |
" 123456789012 |
|
7779 |
'abcdefabcdef' lastIndexOfString:'abc' |
|
7780 |
'abcdefabcdef' lastIndexOfString:'abc' startingAt:6 |
|
7781 |
" |
|
7782 |
! |
|
7783 |
||
7784 |
lastIndexOfString:aString startingAt:startIndex ifAbsent:exceptionValue |
|
7785 |
"VSE and V'age compatibility" |
|
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
7786 |
"find the last occurrence of a substring. |
18939 | 7787 |
If found, return the index; if not found, return 0." |
7788 |
||
7789 |
^ self lastIndexOfSubCollection:aString startingAt:startIndex ifAbsent:exceptionValue |
|
7790 |
||
7791 |
" 123456789012 |
|
7792 |
'abcdefabcdef' lastIndexOfString:'abc' |
|
7793 |
'abcdefabcdef' lastIndexOfString:'abc' startingAt:6 |
|
7794 |
'abcdefabcdef' lastIndexOfString:'xxx' startingAt:6 ifAbsent:999 |
|
7795 |
" |
|
7796 |
! |
|
7797 |
||
15055 | 7798 |
rangeOfSubCollection:subString startingAt:start ifAbsent:exceptionValue caseSensitive:caseSensitive |
7799 |
"find a substring. if found, return the start- and endIndex; |
|
7800 |
if not found, return the value of exceptionValue." |
|
7801 |
||
7802 |
|i| |
|
7803 |
||
7804 |
i := self indexOfSubCollection:subString startingAt:start ifAbsent:0 caseSensitive:caseSensitive. |
|
7805 |
i == 0 ifTrue:[ |
|
20012 | 7806 |
^ exceptionValue value |
15055 | 7807 |
]. |
7808 |
^ i to:(i + subString size - 1) |
|
7809 |
||
7810 |
" |
|
7811 |
'hello world' findRangeOfString:'llo' |
|
7812 |
'hello world' findRangeOfString:'ole' |
|
7813 |
" |
|
9192 | 7814 |
! ! |
7815 |
||
7816 |
!CharacterArray methodsFor:'testing'! |
|
7817 |
||
18463 | 7818 |
argumentCount |
7819 |
"treating the receiver as a message selector, return how many arguments would it take (ANSI)" |
|
7820 |
||
7821 |
|binopChars| |
|
7822 |
||
7823 |
(self size <= Method maxBinarySelectorSize) ifTrue:[ |
|
20012 | 7824 |
binopChars := Method binarySelectorCharacters. |
7825 |
(self conform:[:eachChar | (binopChars includes:eachChar)]) ifTrue:[ |
|
7826 |
^ 1 |
|
7827 |
]. |
|
18463 | 7828 |
]. |
7829 |
^ self occurrencesOf:$: |
|
7830 |
||
7831 |
" |
|
7832 |
'foo:bar:' argumentCount |
|
7833 |
#foo:bar: argumentCount |
|
7834 |
'hello' argumentCount |
|
7835 |
'+' argumentCount |
|
7836 |
'++' argumentCount |
|
7837 |
'+++' argumentCount |
|
7838 |
'|' argumentCount |
|
7839 |
'?' argumentCount |
|
7840 |
'_' argumentCount |
|
7841 |
'_:' argumentCount |
|
7842 |
'_:_:' argumentCount |
|
7843 |
'<->' argumentCount |
|
7844 |
" |
|
7845 |
! |
|
7846 |
||
18597 | 7847 |
characterSize |
7848 |
"answer the size in bits of my largest character (actually only 7, 8, 16 or 32)" |
|
7849 |
||
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
7850 |
|string max |
18597 | 7851 |
sz "{ Class:SmallInteger}" | |
15829 | 7852 |
|
7853 |
(string := self string) ~~ self ifTrue:[ |
|
20012 | 7854 |
^ string characterSize. |
18597 | 7855 |
]. |
7856 |
||
7857 |
sz := self size. |
|
7858 |
max := 7. |
|
7859 |
1 to:sz do:[:idx | |
|
20012 | 7860 |
|thisSize| |
7861 |
||
7862 |
thisSize := (self at:idx) characterSize. |
|
7863 |
thisSize > max ifTrue:[ |
|
7864 |
max := thisSize. |
|
7865 |
max == 32 ifTrue:[ |
|
7866 |
"shortcut: we know, that max size is 32" |
|
7867 |
^ 32. |
|
7868 |
]. |
|
7869 |
]. |
|
15829 | 7870 |
]. |
18597 | 7871 |
|
7872 |
^ max. |
|
7873 |
||
7874 |
" |
|
7875 |
'hello' characterSize |
|
7876 |
'hello' asUnicode32String characterSize |
|
7877 |
'helloü' asUnicode32String characterSize |
|
7878 |
'hello' asText allBold characterSize |
|
7879 |
" |
|
7880 |
! |
|
7881 |
||
7882 |
contains8BitCharacters |
|
7883 |
<resource: #obsolete> |
|
7884 |
||
7885 |
^ self containsNon7BitAscii |
|
7886 |
! |
|
7887 |
||
7888 |
containsNon7BitAscii |
|
7889 |
"return true, if the underlying string contains 8BitCharacters (or widers) |
|
7890 |
(i.e. if it is non-ascii)" |
|
7891 |
||
7892 |
|string |
|
7893 |
sz "{ Class:SmallInteger }"| |
|
7894 |
||
7895 |
(string := self string) ~~ self ifTrue:[ |
|
20012 | 7896 |
^ string containsNon7BitAscii |
18597 | 7897 |
]. |
7898 |
sz := self size. |
|
7899 |
1 to:sz do:[:idx| |
|
20012 | 7900 |
(self at:idx) codePoint > 16r7F ifTrue:[ |
7901 |
^ true. |
|
7902 |
]. |
|
18597 | 7903 |
]. |
7904 |
^ false. |
|
7905 |
||
7906 |
" |
|
7907 |
'hello' asUnicode32String containsNon7BitAscii |
|
7908 |
'hello üöä' asUnicode32String containsNon7BitAscii |
|
7909 |
'hello' asUnicode32String asText allBold containsNon7BitAscii |
|
7910 |
'hello üö' asUnicode32String asText allBold containsNon7BitAscii |
|
15829 | 7911 |
" |
7912 |
! |
|
7913 |
||
9192 | 7914 |
isAlphaNumeric |
7915 |
"return true, if the receiver is some alphanumeric word; |
|
7916 |
i.e. consists of a letter followed by letters or digits." |
|
7917 |
||
7918 |
self size == 0 ifTrue:[ |
|
20012 | 7919 |
"mhmh what is this ?" |
7920 |
^ false |
|
9192 | 7921 |
]. |
7922 |
(self at:1) isLetter ifFalse:[^ false]. |
|
10093
8cff5eca1c6f
code cleanup: use #contains/#conform instead of explicit loop
Claus Gittinger <cg@exept.de>
parents:
10088
diff
changeset
|
7923 |
^ self conform:[:char | char isLetterOrDigit]. |
8cff5eca1c6f
code cleanup: use #contains/#conform instead of explicit loop
Claus Gittinger <cg@exept.de>
parents:
10088
diff
changeset
|
7924 |
|
8cff5eca1c6f
code cleanup: use #contains/#conform instead of explicit loop
Claus Gittinger <cg@exept.de>
parents:
10088
diff
changeset
|
7925 |
" |
10703
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
7926 |
'helloWorld' isAlphaNumeric |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
7927 |
'foo1234' isAlphaNumeric |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
7928 |
'f1234' isAlphaNumeric |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
7929 |
'1234' isAlphaNumeric |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
7930 |
'+' isAlphaNumeric |
10093
8cff5eca1c6f
code cleanup: use #contains/#conform instead of explicit loop
Claus Gittinger <cg@exept.de>
parents:
10088
diff
changeset
|
7931 |
" |
8cff5eca1c6f
code cleanup: use #contains/#conform instead of explicit loop
Claus Gittinger <cg@exept.de>
parents:
10088
diff
changeset
|
7932 |
|
8cff5eca1c6f
code cleanup: use #contains/#conform instead of explicit loop
Claus Gittinger <cg@exept.de>
parents:
10088
diff
changeset
|
7933 |
"Modified: / 13-10-2006 / 12:53:49 / cg" |
9192 | 7934 |
! |
9190 | 7935 |
|
7936 |
isBinarySelector |
|
10903 | 7937 |
"treating the receiver as a message selector, return true if its a binary selector. |
7938 |
Notice, that st/x does not have a size <= 2 limit for unaries" |
|
9190 | 7939 |
|
7940 |
|binopChars| |
|
7941 |
||
18463 | 7942 |
(self size > Method maxBinarySelectorSize) ifTrue:[^ false]. |
7943 |
||
7944 |
binopChars := Method binarySelectorCharacters. |
|
9190 | 7945 |
^ (self conform:[:char | (binopChars includes:char)]) |
7946 |
||
7947 |
" |
|
7948 |
'foo:bar:' isBinarySelector |
|
7949 |
#foo:bar: isBinarySelector |
|
7950 |
'hello' isBinarySelector |
|
7951 |
'+' isBinarySelector |
|
7952 |
'|' isBinarySelector |
|
8874 | 7953 |
'?' isBinarySelector |
7954 |
':' isBinarySelector |
|
7955 |
'a:' isBinarySelector |
|
7956 |
'->' isBinarySelector |
|
7957 |
'<->' isBinarySelector |
|
7958 |
'::' isBinarySelector |
|
7482 | 7959 |
" |
7960 |
||
7961 |
"Modified: 4.1.1997 / 14:16:14 / cg" |
|
9192 | 7962 |
! |
7963 |
||
7964 |
isBlank |
|
7965 |
"return true, if the receiver contains spaces only" |
|
7966 |
||
9230 | 7967 |
^ (self contains:[:char | char ~~ Character space]) not |
7968 |
||
7969 |
" |
|
10703
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
7970 |
'' isBlank |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
7971 |
' a ' isBlank |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
7972 |
' ' isBlank |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
7973 |
' ' asUnicode16String isBlank |
9230 | 7974 |
" |
9192 | 7975 |
! |
7976 |
||
10903 | 7977 |
isKeywordSelector |
7978 |
"return true, iff there are only alphanumeric or underline characters separated by colons. |
|
19682 | 7979 |
Must end with a colon. |
7980 |
You can use this to check an arbitrary string for being valid as a keyword. |
|
7981 |
If you have a valid selector at hand, and need to know if it is a keyword or not, |
|
7982 |
use #isKeyword, which is much faster." |
|
10903 | 7983 |
|
7984 |
|state| |
|
7985 |
||
19682 | 7986 |
(self size == 0) ifTrue:[^ false]. |
7987 |
(self last == $:) ifFalse:[^ false]. |
|
7988 |
||
12177 | 7989 |
state := #initial. |
10903 | 7990 |
self do:[:char | |
20012 | 7991 |
(state == #initial or:[ state == #gotColon]) ifTrue:[ |
7992 |
(char isLetterOrUnderline) ifFalse:[^ false]. |
|
7993 |
state := #gotCharacter. |
|
7994 |
] ifFalse:[ |
|
7995 |
char == $: ifTrue:[ |
|
7996 |
state := #gotColon. |
|
7997 |
] ifFalse:[ |
|
7998 |
(char isLetterOrDigit or:[char == $_]) ifFalse:[^ false]. |
|
7999 |
]. |
|
8000 |
]. |
|
10903 | 8001 |
]. |
8002 |
^ state == #gotColon. |
|
8003 |
||
8004 |
" |
|
12177 | 8005 |
self assert:(':' isKeywordSelector not). |
10903 | 8006 |
self assert:(':a' isKeywordSelector not). |
8007 |
self assert:('1:' isKeywordSelector not). |
|
8008 |
self assert:('a:' isKeywordSelector). |
|
8009 |
self assert:('_:' isKeywordSelector). |
|
8010 |
self assert:('_a:' isKeywordSelector). |
|
8011 |
self assert:('_1:' isKeywordSelector). |
|
8012 |
self assert:('_1::' isKeywordSelector not). |
|
8013 |
self assert:('_:_:' isKeywordSelector). |
|
8014 |
self assert:('a:b:' isKeywordSelector). |
|
8015 |
self assert:('aa:bb:' isKeywordSelector). |
|
8016 |
self assert:('aa:bb:a' isKeywordSelector not). |
|
8017 |
self assert:('1:2:' isKeywordSelector not). |
|
8018 |
" |
|
19682 | 8019 |
|
8020 |
"Modified (comment): / 30-04-2016 / 18:20:14 / cg" |
|
10903 | 8021 |
! |
8022 |
||
10431 | 8023 |
isNameSpaceSelector |
8024 |
"Answer true if the receiver contains chars which form a nameSpace selector name. |
|
8025 |
These are of the form ':<ns>::<sel>', where ns is the NameSpace and sel is the regular selector. |
|
8026 |
For example, the #+ selector as seen by the Foo namespace would be actually #':Foo::+'. |
|
17511 | 8027 |
This special format (a symbol starting with a colon) was chosen, because almost every other selector |
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
8028 |
is legal, and this can be checked quickly by just looking at the first character. |
10431 | 8029 |
You cannot easily change this algorithm here, as it is also known by the VM's lookup function." |
8030 |
||
8031 |
|i| |
|
8032 |
||
16733 | 8033 |
(self at:1) == $: ifFalse:[^ false]. |
17511 | 8034 |
|
10431 | 8035 |
i := self indexOf:$: startingAt:2. |
8036 |
i == 0 ifTrue:[^ false]. |
|
8037 |
self size <= (i+1) ifTrue:[^ false]. |
|
8038 |
(self at:i+1) == $: ifFalse:[^ false]. |
|
8039 |
(self at:i+2) == $: ifTrue:[^ false]. |
|
8040 |
"/ could check if the rest after the ns-prefix and colons is a valid selector... |
|
8041 |
^ true |
|
8042 |
||
8043 |
"test: |
|
10703
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
8044 |
self assert:('+' isNameSpaceSelector) not. |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
8045 |
self assert:(':+' isNameSpaceSelector) not. |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
8046 |
self assert:(':Foo:+' isNameSpaceSelector) not. |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
8047 |
|
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
8048 |
self assert:(':Foo::+' isNameSpaceSelector). |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
8049 |
self assert:(':Foo::bar:baz:' isNameSpaceSelector). |
10431 | 8050 |
" |
8051 |
||
8052 |
"Created: / 05-03-2007 / 11:35:31 / cg" |
|
8053 |
! |
|
8054 |
||
9192 | 8055 |
isNumeric |
8056 |
"return true, if the receiver is some numeric word; |
|
8057 |
i.e. consists only of digits." |
|
8058 |
||
8059 |
self size == 0 ifTrue:[ |
|
20012 | 8060 |
^ false |
9192 | 8061 |
]. |
10093
8cff5eca1c6f
code cleanup: use #contains/#conform instead of explicit loop
Claus Gittinger <cg@exept.de>
parents:
10088
diff
changeset
|
8062 |
^ self conform:[:char | char isDigit] |
9192 | 8063 |
|
8064 |
" |
|
8065 |
'helloWorld' isNumeric |
|
8066 |
'foo1234' isNumeric |
|
8067 |
'f1234' isNumeric |
|
8068 |
'1234' isNumeric |
|
8069 |
'+' isNumeric |
|
8070 |
" |
|
10093
8cff5eca1c6f
code cleanup: use #contains/#conform instead of explicit loop
Claus Gittinger <cg@exept.de>
parents:
10088
diff
changeset
|
8071 |
|
8cff5eca1c6f
code cleanup: use #contains/#conform instead of explicit loop
Claus Gittinger <cg@exept.de>
parents:
10088
diff
changeset
|
8072 |
"Modified: / 13-10-2006 / 12:54:12 / cg" |
9192 | 8073 |
! |
8074 |
||
18893 | 8075 |
isPlainString |
8076 |
"return true, if the receiver is a plain string (without attributes); |
|
8077 |
true is returned here - redefinition of Object>>isPlainString." |
|
8078 |
||
8079 |
^ true |
|
8080 |
! |
|
8081 |
||
9230 | 8082 |
isString |
8083 |
"return true, if the receiver is some kind of string; |
|
8084 |
true is returned here - redefinition of Object>>isString." |
|
8085 |
||
8086 |
^ true |
|
8087 |
! |
|
8088 |
||
8089 |
isUnarySelector |
|
8090 |
"Answer true if the receiver contains only chars in an ANSI unary method selector, false otherwise." |
|
8091 |
||
17442 | 8092 |
^ (self first isLetterOrUnderline) |
9845 | 8093 |
and:[ self conform: [ :chr | chr isLetterOrDigit or:[chr == $_] ]] |
8094 |
||
8095 |
"Modified: / 13-09-2006 / 11:35:15 / cg" |
|
9230 | 8096 |
! |
8097 |
||
14122 | 8098 |
isUnicode16String |
8099 |
^ false |
|
8100 |
! |
|
8101 |
||
8102 |
isUnicode32String |
|
19939 | 8103 |
"true if this is a 4-byte unicode string" |
8104 |
||
14122 | 8105 |
^ false |
8106 |
! |
|
8107 |
||
8108 |
isUnicodeString |
|
19938 | 8109 |
"true if this is a 2- or 4-byte unicode string |
8110 |
(i.e. not a single byte string). |
|
8111 |
Notice, that the name is misleading: |
|
8112 |
all strings are use unicode encoding" |
|
8113 |
||
14122 | 8114 |
^ false |
8115 |
! |
|
8116 |
||
9230 | 8117 |
isValidSmalltalkIdentifier |
19464 | 8118 |
"return true, if the receiver's characters make up a valid smalltalk identifier" |
9230 | 8119 |
|
8120 |
|scanner tok| |
|
8121 |
||
8122 |
scanner := Compiler new. |
|
8123 |
scanner source:(self readStream). |
|
11630
6db5956f188f
isValidSmalltalkIdentifier: return true/false (catch parseError)
Claus Gittinger <cg@exept.de>
parents:
11565
diff
changeset
|
8124 |
Parser parseErrorSignal handle:[:ex | |
20012 | 8125 |
tok := nil. |
11630
6db5956f188f
isValidSmalltalkIdentifier: return true/false (catch parseError)
Claus Gittinger <cg@exept.de>
parents:
11565
diff
changeset
|
8126 |
] do:[ |
20012 | 8127 |
tok := scanner nextToken. |
11630
6db5956f188f
isValidSmalltalkIdentifier: return true/false (catch parseError)
Claus Gittinger <cg@exept.de>
parents:
11565
diff
changeset
|
8128 |
]. |
9230 | 8129 |
tok ~~ #Identifier ifTrue:[ |
20012 | 8130 |
^ false |
9230 | 8131 |
]. |
8132 |
scanner tokenPosition == 1 ifFalse:[^ false]. |
|
8133 |
^ scanner sourceStream atEnd. |
|
8134 |
||
8135 |
" |
|
8136 |
'foo' isValidSmalltalkIdentifier |
|
8137 |
'1foo' isValidSmalltalkIdentifier |
|
8138 |
'_foo' isValidSmalltalkIdentifier |
|
8139 |
'_foo_bar_' isValidSmalltalkIdentifier |
|
8140 |
'foo ' isValidSmalltalkIdentifier |
|
8141 |
' foo' isValidSmalltalkIdentifier |
|
8142 |
" |
|
8143 |
! |
|
8144 |
||
14555 | 8145 |
isWideString |
19580 | 8146 |
"true if I require more than one byte per character" |
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
8147 |
|
15829 | 8148 |
|string| |
8149 |
||
8150 |
(string := self string) ~~ self ifTrue:[ |
|
20012 | 8151 |
^ string isWideString. |
15829 | 8152 |
]. |
8153 |
^ self contains:[:aCharacter | aCharacter codePoint > 16rFF]. |
|
14555 | 8154 |
! |
8155 |
||
9192 | 8156 |
numArgs |
18463 | 8157 |
<resource: #obsolete> |
608 | 8158 |
"treating the receiver as a message selector, return how many arguments would it take" |
8159 |
||
18463 | 8160 |
^ self argumentCount |
9192 | 8161 |
! |
8162 |
||
8163 |
partsIfSelector |
|
14629 | 8164 |
"treat the receiver as a message selector, return a collection of parts. |
8165 |
Notice: this is more tolerant than Smalltalk's syntax would suggest; |
|
8166 |
especially it allows for empty keyword parts between colons. |
|
8167 |
This is not (and should not be checked here), to allow parsing of |
|
8168 |
degenerate selectors as appearing with objectiveC." |
|
608 | 8169 |
|
9192 | 8170 |
|idx1 "{ Class: SmallInteger }" |
8171 |
coll idx2 sz| |
|
8172 |
||
8173 |
coll := OrderedCollection new. |
|
8174 |
idx1 := 1. |
|
8175 |
sz := self size. |
|
16245 | 8176 |
[ |
20012 | 8177 |
idx2 := self indexOf:$: startingAt:idx1. |
8178 |
(idx2 == 0 or:[idx2 == sz]) ifTrue:[ |
|
8179 |
coll add:(self copyFrom:idx1). |
|
8180 |
^ coll |
|
8181 |
]. |
|
8182 |
coll add:(self copyFrom:idx1 to:idx2). |
|
8183 |
idx1 := idx2 + 1 |
|
16245 | 8184 |
] loop. |
9192 | 8185 |
|
8186 |
" |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
8187 |
'foo:' partsIfSelector |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
8188 |
'foo:bar:' partsIfSelector |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
8189 |
'foo::::' partsIfSelector |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
8190 |
#foo:bar: partsIfSelector |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
8191 |
'hello' partsIfSelector |
9192 | 8192 |
'+' partsIfSelector |
8193 |
" |
|
64 | 8194 |
! ! |
8195 |
||
9192 | 8196 |
!CharacterArray methodsFor:'tracing'! |
8197 |
||
8198 |
traceInto:aRequestor level:level from:referrer |
|
8199 |
"double dispatch into tracer, passing my type implicitely in the selector" |
|
8200 |
||
8201 |
^ aRequestor traceCharacterArray:self level:level from:referrer |
|
8202 |
||
8203 |
||
8204 |
! ! |
|
8205 |
||
8206 |
!CharacterArray methodsFor:'visiting'! |
|
8395
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8376
diff
changeset
|
8207 |
|
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8376
diff
changeset
|
8208 |
acceptVisitor:aVisitor with:aParameter |
16733 | 8209 |
"dispatch for visitor pattern; send #visitString:with: to aVisitor" |
8395
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8376
diff
changeset
|
8210 |
|
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8376
diff
changeset
|
8211 |
^ aVisitor visitString:self with:aParameter |
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8376
diff
changeset
|
8212 |
! ! |
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8376
diff
changeset
|
8213 |
|
20155 | 8214 |
|
1849
9f35f5934ec6
added msdos-latin1 en/decoder (code = #msdos850)
Claus Gittinger <cg@exept.de>
parents:
1669
diff
changeset
|
8215 |
!CharacterArray class methodsFor:'documentation'! |
629 | 8216 |
|
8217 |
version |
|
18476 | 8218 |
^ '$Header$' |
12058 | 8219 |
! |
8220 |
||
8221 |
version_CVS |
|
18476 | 8222 |
^ '$Header$' |
629 | 8223 |
! ! |
6809 | 8224 |
|
14606 | 8225 |
|
2728
7569e45a4d15
raise error signal if anything goes wrong while decoding.
Claus Gittinger <cg@exept.de>
parents:
2721
diff
changeset
|
8226 |
CharacterArray initialize! |