author | Stefan Vogel <sv@exept.de> |
Wed, 30 Nov 2016 18:24:33 +0100 | |
changeset 21083 | 3b7b02336cbb |
parent 21030 | 4dba22c05301 |
child 21089 | d50f93ca9622 |
child 21122 | a75ae33b406f |
permissions | -rw-r--r-- |
17600 | 1 |
"{ Encoding: utf8 }" |
2 |
||
64 | 3 |
" |
4 |
COPYRIGHT (c) 1994 by Claus Gittinger |
|
20874 | 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 |
21030 | 19 |
instanceVariableNames:'' |
20 |
classVariableNames:'DecoderTables DecodingFailedSignal EncoderTables |
|
21 |
EncodingFailedSignal PreviousMatches UnicodeDenormalizationMap |
|
22 |
UnicodeNormalizationMap' |
|
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 |
|
20874 | 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 |
20194 | 47 |
and whatever may come in the future. |
18318 | 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: |
20194 | 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: |
|
20194 | 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:] |
20194 | 75 |
Claus Gittinger |
1290 | 76 |
|
77 |
[see also:] |
|
20194 | 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:[ |
20874 | 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:[ |
|
20874 | 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 |
" |
|
20874 | 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 | |
|
20874 | 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:[ |
20874 | 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 | |
|
20874 | 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 |
|
20873 | 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 |
20864 | 555 |
"returns a 2-stage map from ch2 -> ch1 -> mappedChar. |
556 |
for unicode normalization |
|
557 |
(i.e. for replacing combining char-sequences with regular characters). |
|
558 |
ch2 is the combining charCode (eg. 0x0308), ch1 is the previous character (eg. $A), |
|
559 |
mappedChar is the result (eg. $Ä). |
|
560 |
Caveat: |
|
561 |
possibly incomplete: only COMBINING_DIACRITICAL_MARKS are cared for. |
|
562 |
Does not care for COMBINING_DIACRITICAL_MARKS_EXTENDED |
|
563 |
and COMBINING_DIACRITICAL_MARKS_SUPPLEMENT. |
|
564 |
However; those are used for German dialectology, ancient Greek and other similar |
|
565 |
exotic uses. Probably noone will ever even notice that they are missing..." |
|
20011 | 566 |
|
567 |
|def| |
|
568 |
||
569 |
UnicodeNormalizationMap := Dictionary new. |
|
20013 | 570 |
UnicodeDenormalizationMap := Dictionary new. |
571 |
||
572 |
def := [:combiner :chars :mappedChars | |
|
20011 | 573 |
|d| |
574 |
||
20013 | 575 |
d := UnicodeNormalizationMap at:combiner ifAbsentPut:[Dictionary new]. |
576 |
chars with:mappedChars do:[:ch1 :mappedChar | |
|
577 |
d at:ch1 put:mappedChar. |
|
578 |
UnicodeDenormalizationMap at:mappedChar put:(Unicode16String with:ch1 with:combiner). |
|
579 |
]. |
|
20011 | 580 |
]. |
20014 | 581 |
def value:(Character codePoint:16r0300) "grave" value:'AEIOUaeiouWw' value:'ÀÈÌÒÙàèìòùẀẁ'. |
582 |
def value:(Character codePoint:16r0301) "acute" value:'AEIOUYaeiouyÇçKkMmPpWw' value:'ÁÉÍÓÚÝáéíóúýḈḉḰḱḾḿṔṕẂẃ'. |
|
583 |
def value:(Character codePoint:16r0302) "circumflex" value:'AEIOUaeiouZz' value:'ÂÊÎÔÛâêîôûẐẑ'. |
|
20011 | 584 |
def value:(Character codePoint:16r0303) "tilde" value:'AONaon' value:'ÃÕÑãõñ'. |
20014 | 585 |
def value:(Character codePoint:16r0307) "dot over" value:'BbDdFfHhMmNnPpRrSsTtWwXxYyṢṣ' value:'ḂḃḊḋḞḟḢḣṀṁṄṅṖṗṘṙṠṡṪṫẆẇẊẋẎẏṨṩ'. |
586 |
def value:(Character codePoint:16r0308) "diaresis" value:'AEIOUaeiouyHhWwXxt' value:'ÄËÏÖÜäëïöüÿḦḧẄẅẌẍẗ'. |
|
587 |
def value:(Character codePoint:16r030A) "ring" value:'Aawy' value:'Ååẘẙ'. |
|
588 |
def value:(Character codePoint:16r0323) "dot under" value:'AaBbDdEeIiHhKkLlMmNnOoRrSsTtUuVvWwZzṠṡ' value:'ẠạḄḅḌḍẸẹỊịḤḥḲḳḶḷṂṃṆṇỌọṚṛṢṣṬṭỤụṾṿẈẉẒẓṨṩ'. |
|
20011 | 589 |
def value:(Character codePoint:16r0327) "cedilla" value:'Cc' value:'Çç'. |
590 |
||
591 |
" |
|
20014 | 592 |
self setupNormalizationMaps |
20011 | 593 |
" |
594 |
! |
|
595 |
||
596 |
unicodeDenormalizationMap |
|
597 |
"returns a 2-stage map from ch2 -> ch1 -> mappedChar |
|
20864 | 598 |
for unicode normalization (i.e. for making combining chars regular ones). |
599 |
Caveat: |
|
600 |
possibly incomplete: only COMBINING_DIACRITICAL_MARKS are cared for. |
|
601 |
Does not care for COMBINING_DIACRITICAL_MARKS_EXTENDED |
|
602 |
and COMBINING_DIACRITICAL_MARKS_SUPPLEMENT. |
|
603 |
However; those are used for German dialectology, ancient Greek and other similar |
|
604 |
exotic uses. Probably noone will ever even notice that they are missing..." |
|
20011 | 605 |
|
606 |
UnicodeDenormalizationMap isNil ifTrue:[ |
|
607 |
self setupNormalizationMaps |
|
608 |
]. |
|
609 |
^ UnicodeDenormalizationMap |
|
610 |
||
611 |
" |
|
612 |
self unicodeDenormalizationMap |
|
613 |
" |
|
614 |
! |
|
615 |
||
20010 | 616 |
unicodeNormalizationMap |
617 |
"returns a 2-stage map from ch2 -> ch1 -> mappedChar |
|
20864 | 618 |
for unicode normalization (i.e. for making combining chars regular ones). |
619 |
Caveat: |
|
620 |
possibly incomplete: only COMBINING_DIACRITICAL_MARKS are cared for. |
|
621 |
Does not care for COMBINING_DIACRITICAL_MARKS_EXTENDED |
|
622 |
and COMBINING_DIACRITICAL_MARKS_SUPPLEMENT. |
|
623 |
However; those are used for German dialectology, ancient Greek and other similar |
|
624 |
exotic uses. Probably noone will ever even notice that they are missing..." |
|
20010 | 625 |
|
626 |
UnicodeNormalizationMap isNil ifTrue:[ |
|
20011 | 627 |
self setupNormalizationMaps |
20010 | 628 |
]. |
629 |
^ UnicodeNormalizationMap |
|
630 |
||
631 |
" |
|
632 |
self unicodeNormalizationMap |
|
633 |
" |
|
443 | 634 |
! ! |
635 |
||
20873 | 636 |
|
1849
9f35f5934ec6
added msdos-latin1 en/decoder (code = #msdos850)
Claus Gittinger <cg@exept.de>
parents:
1669
diff
changeset
|
637 |
!CharacterArray class methodsFor:'pattern matching'! |
64 | 638 |
|
5342
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
639 |
matchEscapeCharacter |
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
640 |
"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
|
641 |
(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
|
642 |
|
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
643 |
^ $\ |
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
644 |
! |
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
645 |
|
15734 | 646 |
matchScan:matchScanArray from:matchStart to:matchStop with:aString from:start to:stop caseSensitive:caseSensitive |
327 | 647 |
"helper for match; return true if the characters from start to stop in |
648 |
aString are matching the scan in matchScan from matchStart to matchStop. |
|
328 | 649 |
The matchScan is as created by asMatchScanArray. |
327 | 650 |
|
8874 | 651 |
This algorithm is not at all the most efficient; |
652 |
for heavy duty pattern matching, an interface (primitive) to the regex |
|
328 | 653 |
pattern matching package should be added." |
327 | 654 |
|
8874 | 655 |
|matchEntry |
327 | 656 |
mStart "{ Class: SmallInteger }" |
657 |
mStop "{ Class: SmallInteger }" |
|
658 |
sStart "{ Class: SmallInteger }" |
|
659 |
sStop "{ Class: SmallInteger }" |
|
660 |
mSize "{ Class: SmallInteger }" |
|
661 |
sSize "{ Class: SmallInteger }" |
|
662 |
index "{ Class: SmallInteger }" |
|
2561 | 663 |
quickCheck matchLast nextMatchEntry |
986 | 664 |
checkChar included| |
327 | 665 |
|
666 |
mStart := matchStart. |
|
667 |
mStop := matchStop. |
|
668 |
sStart := start. |
|
669 |
sStop := stop. |
|
670 |
||
16245 | 671 |
[ |
8874 | 672 |
"/ Transcript showCR:('match: ''' , (aString copyFrom:sStart to:sStop) , |
3537 | 673 |
"/ ''' against:' , (matchScanArray copyFrom:mStart to:mStop) printString). |
674 |
||
20874 | 675 |
mSize := mStop - mStart + 1. |
676 |
sSize := sStop - sStart + 1. |
|
677 |
||
678 |
"empty strings match" |
|
679 |
(mSize == 0) ifTrue:[^ (sSize == 0)]. |
|
680 |
||
681 |
matchEntry := matchScanArray at:mStart. |
|
682 |
||
683 |
"/ the most common case first: |
|
684 |
(sSize ~~ 0 |
|
685 |
and:[(checkChar := (aString at:sStart)) = matchEntry]) ifTrue:[ |
|
686 |
"advance by one and continue" |
|
687 |
mStart := mStart + 1. |
|
688 |
sStart := sStart + 1 |
|
689 |
] ifFalse:[ |
|
690 |
(matchEntry == #any) ifTrue:[ |
|
691 |
"restString empty -> no match" |
|
692 |
(sSize == 0) ifTrue:[^ false]. |
|
693 |
"# matches single character" |
|
694 |
((sSize == 1) and:[mSize == 1]) ifTrue:[^ true]. |
|
695 |
"advance by one and continue" |
|
696 |
mStart := mStart + 1. |
|
697 |
sStart := sStart + 1 |
|
698 |
] ifFalse:[ |
|
699 |
(matchEntry == #anyString) ifTrue:[ |
|
700 |
"* alone matches anything" |
|
701 |
(mSize == 1) ifTrue:[^ true]. |
|
702 |
"restString empty & matchString not empty -> no match" |
|
703 |
(sSize == 0) ifTrue:[^ false]. |
|
704 |
||
705 |
" |
|
706 |
try to avoid some of the recursion by checking last |
|
707 |
character and continue with shortened strings if possible |
|
708 |
" |
|
709 |
quickCheck := false. |
|
710 |
(mStop >= mStart) ifTrue:[ |
|
711 |
matchLast := matchScanArray at:mStop. |
|
712 |
(matchLast ~~ #anyString) ifTrue:[ |
|
713 |
(matchLast == #any) ifTrue:[ |
|
714 |
quickCheck := true |
|
715 |
] ifFalse:[ |
|
716 |
matchLast == (aString at:sStop) ifTrue:[ |
|
717 |
quickCheck := true |
|
718 |
] ifFalse:[ |
|
719 |
matchLast isString ifTrue:[ |
|
720 |
quickCheck := matchLast includes:(aString at:sStop) |
|
721 |
] |
|
722 |
] |
|
723 |
] |
|
724 |
] |
|
725 |
]. |
|
726 |
quickCheck ifTrue:[ |
|
727 |
" |
|
728 |
quickCheck ok, advance from the right |
|
729 |
" |
|
730 |
mStop := mStop - 1. |
|
731 |
sStop := sStop - 1 |
|
732 |
] ifFalse:[ |
|
733 |
"/ no quick check; |
|
734 |
"/ look for the next character(s) |
|
735 |
"/ and try matching there |
|
736 |
"/ (to avoid recursion) |
|
737 |
||
738 |
mStart < mStop ifTrue:[ |
|
739 |
nextMatchEntry := matchScanArray at:mStart+1. |
|
740 |
nextMatchEntry isCharacter ifTrue:[ |
|
741 |
sStart <= sStop ifTrue:[ |
|
742 |
[ |
|
743 |
caseSensitive ifTrue:[ |
|
744 |
index := aString indexOf:nextMatchEntry startingAt:sStart |
|
745 |
] ifFalse:[ |
|
746 |
index := aString findFirst:[:c | c asLowercase = nextMatchEntry asLowercase] |
|
747 |
startingAt:sStart. |
|
748 |
]. |
|
749 |
(index == 0 or:[index > sStop]) ifTrue:[ |
|
750 |
^ false |
|
751 |
]. |
|
752 |
(self matchScan:matchScanArray |
|
753 |
from:(mStart + 1) |
|
754 |
to:mStop |
|
755 |
with:aString |
|
756 |
from:index |
|
757 |
to:sStop |
|
758 |
caseSensitive:caseSensitive |
|
759 |
) ifTrue:[ |
|
760 |
^ true |
|
761 |
]. |
|
762 |
sStart := index + 1. |
|
763 |
] loop. |
|
764 |
] |
|
765 |
] |
|
766 |
]. |
|
767 |
||
768 |
" |
|
769 |
no quick check possible; |
|
770 |
loop over all possible substrings |
|
771 |
" |
|
772 |
index := sStart. |
|
773 |
[index <= sStop] whileTrue:[ |
|
774 |
(self matchScan:matchScanArray |
|
775 |
from:(mStart + 1) |
|
776 |
to:mStop |
|
777 |
with:aString |
|
778 |
from:index |
|
779 |
to:sStop |
|
780 |
caseSensitive:caseSensitive |
|
781 |
) ifTrue:[ |
|
782 |
^ true |
|
783 |
]. |
|
784 |
index := index + 1 |
|
785 |
]. |
|
786 |
^ false |
|
787 |
]. |
|
788 |
] ifFalse:[ |
|
789 |
(matchEntry isString) ifTrue:[ |
|
790 |
"testString empty -> no match" |
|
791 |
(sSize == 0) ifTrue:[^ false]. |
|
792 |
||
793 |
included := false. |
|
794 |
"/ checkChar := aString at:sStart. |
|
795 |
included := matchEntry includes:checkChar. |
|
796 |
included ifFalse:[ |
|
797 |
caseSensitive ifFalse:[ |
|
798 |
checkChar isUppercase ifTrue:[ |
|
799 |
included := matchEntry includes:checkChar asLowercase. |
|
800 |
] ifFalse:[ |
|
801 |
included := matchEntry includes:checkChar asUppercase. |
|
802 |
] |
|
803 |
]. |
|
804 |
]. |
|
805 |
mStart := mStart + 1. |
|
806 |
mSize := mSize - 1. |
|
807 |
included ifFalse:[^ false]. |
|
808 |
||
809 |
((sSize == 1) and:[mSize == 0]) ifTrue:[^ true]. |
|
810 |
] ifFalse:[ |
|
811 |
"/ must be single character |
|
812 |
||
813 |
"testString empty ?" |
|
814 |
(sSize == 0) ifTrue:[^ false]. |
|
815 |
||
816 |
"first characters equal ?" |
|
817 |
"/ checkChar := aString at:sStart. |
|
818 |
caseSensitive ifTrue:[^ false]. |
|
819 |
(checkChar asUppercase ~= matchEntry asUppercase) ifTrue:[^ false]. |
|
820 |
||
821 |
"advance and continue" |
|
822 |
mStart := mStart + 1. |
|
823 |
]. |
|
824 |
"cut off 1st char and continue" |
|
825 |
sStart := sStart + 1 |
|
826 |
] |
|
827 |
] |
|
828 |
] |
|
16245 | 829 |
] loop. |
2519
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
830 |
|
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
831 |
" |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
832 |
|scanArray s| |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
833 |
|
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
834 |
scanArray := self matchScanArrayFrom:'*hello'. |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
835 |
s := 'foo bar hello world'. |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
836 |
CharacterArray |
20874 | 837 |
matchScan:scanArray |
838 |
from:1 |
|
839 |
to:scanArray size |
|
840 |
with:s |
|
841 |
from:1 |
|
842 |
to:s size |
|
843 |
caseSensitive:true |
|
2519
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
844 |
" |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
845 |
" |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
846 |
|scanArray s| |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
847 |
|
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
848 |
scanArray := self matchScanArrayFrom:'*hello*'. |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
849 |
s := 'foo bar hello world'. |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
850 |
CharacterArray |
20874 | 851 |
matchScan:scanArray |
852 |
from:1 |
|
853 |
to:scanArray size |
|
854 |
with:s |
|
855 |
from:1 |
|
856 |
to:s size |
|
857 |
caseSensitive:true |
|
15734 | 858 |
" |
859 |
||
860 |
"Modified: / 24-07-2011 / 07:17:03 / cg" |
|
861 |
"Modified (comment): / 24-07-2011 / 08:55:14 / cg" |
|
862 |
! |
|
863 |
||
864 |
matchScan:matchScanArray from:matchStart to:matchStop with:aString from:start to:stop ignoreCase:ignoreCase |
|
865 |
<resource: #obsolete> |
|
866 |
"helper for match; return true if the characters from start to stop in |
|
867 |
aString are matching the scan in matchScan from matchStart to matchStop. |
|
868 |
The matchScan is as created by asMatchScanArray. |
|
869 |
||
870 |
This algorithm is not at all the most efficient; |
|
871 |
for heavy duty pattern matching, an interface (primitive) to the regex |
|
872 |
pattern matching package should be added." |
|
873 |
||
874 |
^ self |
|
20874 | 875 |
matchScan:matchScanArray from:matchStart to:matchStop with:aString from:start to:stop caseSensitive:ignoreCase not |
15734 | 876 |
|
877 |
" |
|
878 |
|scanArray s| |
|
879 |
||
880 |
scanArray := self matchScanArrayFrom:'*hello'. |
|
881 |
s := 'foo bar hello world'. |
|
882 |
CharacterArray |
|
20874 | 883 |
matchScan:scanArray |
884 |
from:1 |
|
885 |
to:scanArray size |
|
886 |
with:s |
|
887 |
from:1 |
|
888 |
to:s size |
|
889 |
ignoreCase:false |
|
15734 | 890 |
" |
891 |
" |
|
892 |
|scanArray s| |
|
893 |
||
894 |
scanArray := self matchScanArrayFrom:'*hello*'. |
|
895 |
s := 'foo bar hello world'. |
|
896 |
CharacterArray |
|
20874 | 897 |
matchScan:scanArray |
898 |
from:1 |
|
899 |
to:scanArray size |
|
900 |
with:s |
|
901 |
from:1 |
|
902 |
to:s size |
|
903 |
ignoreCase:false |
|
13520 | 904 |
" |
905 |
||
906 |
"Modified: / 24-07-2011 / 07:17:03 / cg" |
|
907 |
"Modified (comment): / 24-07-2011 / 08:55:14 / cg" |
|
608 | 908 |
! |
909 |
||
910 |
matchScanArrayFrom:aString |
|
911 |
"scan a pattern string and decompose it into a scanArray. |
|
912 |
This is processed faster (especially with character ranges), and |
|
913 |
can also be reused later. (if the same pattern is to be searched again)" |
|
914 |
||
9282
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
915 |
^ self matchScanArrayFrom:aString escapeCharacter:(self matchEscapeCharacter) |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
916 |
|
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
917 |
" |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
918 |
String matchScanArrayFrom:'*ute*' |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
919 |
String matchScanArrayFrom:'**ute**' |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
920 |
String matchScanArrayFrom:'*uter' |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
921 |
String matchScanArrayFrom:'\*uter' |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
922 |
String matchScanArrayFrom:'[cC]#mpute[rR]' |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
923 |
String matchScanArrayFrom:'[abcd]*' |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
924 |
String matchScanArrayFrom:'[a-k]*' |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
925 |
String matchScanArrayFrom:'*some*compl*ern*' |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
926 |
String matchScanArrayFrom:'[a-' |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
927 |
String matchScanArrayFrom:'[a-zA-Z]' |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
928 |
String matchScanArrayFrom:'[a-z01234A-Z]' |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
929 |
" |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
930 |
|
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
931 |
"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
|
932 |
! |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
933 |
|
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
934 |
matchScanArrayFrom:aString escapeCharacter:escape |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
935 |
"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
|
936 |
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
|
937 |
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
|
938 |
|
8874 | 939 |
|coll |
608 | 940 |
idx "{ Class: SmallInteger }" |
9282
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
941 |
end c1 c2 matchSet previous| |
5342
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
942 |
|
5487 | 943 |
previous := nil. |
608 | 944 |
|
945 |
coll := OrderedCollection new. |
|
946 |
idx := 1. end := aString size. |
|
947 |
[idx <= end] whileTrue:[ |
|
20874 | 948 |
|char this| |
949 |
||
950 |
char := aString at:idx. |
|
951 |
char == $* ifTrue:[ |
|
952 |
previous ~~ #anyString ifTrue:[ |
|
953 |
this := #anyString |
|
954 |
] |
|
955 |
] ifFalse:[ |
|
956 |
char == $# ifTrue:[ |
|
957 |
previous ~~ #anyString ifTrue:[ |
|
958 |
this := #any |
|
959 |
] |
|
960 |
] ifFalse:[ |
|
961 |
char == $[ ifTrue:[ |
|
962 |
matchSet := IdentitySet new. |
|
963 |
idx := idx + 1. |
|
964 |
idx > end ifTrue:[^ nil]. |
|
965 |
char := aString at:idx. |
|
966 |
c1 := nil. |
|
967 |
[char ~~ $]] whileTrue:[ |
|
968 |
((char == $-) and:[c1 notNil]) ifTrue:[ |
|
969 |
idx := idx + 1. |
|
970 |
idx > end ifTrue:[^ nil]. |
|
971 |
c2 := aString at:idx. |
|
972 |
c1 to:c2 do:[:c | matchSet add:c]. |
|
973 |
c1 := nil. |
|
974 |
idx := idx + 1. |
|
975 |
] ifFalse:[ |
|
976 |
(char ~~ $]) ifTrue:[ |
|
977 |
matchSet add:char. |
|
978 |
c1 := char. |
|
979 |
idx := idx + 1 |
|
980 |
] |
|
981 |
]. |
|
982 |
idx > end ifTrue:[^ nil]. |
|
983 |
char := aString at:idx |
|
984 |
]. |
|
985 |
this := matchSet asString |
|
986 |
] ifFalse:[ |
|
987 |
char == escape ifTrue:[ |
|
988 |
idx := idx + 1. |
|
989 |
idx > end ifTrue:[ |
|
990 |
"/ mhmh - what should we do here ? |
|
991 |
this := char |
|
992 |
] ifFalse:[ |
|
993 |
this := aString at:idx. |
|
994 |
] |
|
995 |
] ifFalse:[ |
|
996 |
this := char |
|
997 |
] |
|
998 |
] |
|
999 |
] |
|
1000 |
]. |
|
1001 |
this notNil ifTrue:[coll add:this. previous := this]. |
|
1002 |
idx := idx + 1 |
|
608 | 1003 |
]. |
1004 |
||
1005 |
^ coll asArray |
|
1006 |
||
1007 |
" |
|
8874 | 1008 |
String matchScanArrayFrom:'*ute*' |
1009 |
String matchScanArrayFrom:'**ute**' |
|
1010 |
String matchScanArrayFrom:'*uter' |
|
1011 |
String matchScanArrayFrom:'\*uter' |
|
1012 |
String matchScanArrayFrom:'[cC]#mpute[rR]' |
|
1013 |
String matchScanArrayFrom:'[abcd]*' |
|
1014 |
String matchScanArrayFrom:'[a-k]*' |
|
1015 |
String matchScanArrayFrom:'*some*compl*ern*' |
|
1016 |
String matchScanArrayFrom:'[a-' |
|
1017 |
String matchScanArrayFrom:'[a-zA-Z]' |
|
1018 |
String matchScanArrayFrom:'[a-z01234A-Z]' |
|
608 | 1019 |
" |
2519
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
1020 |
|
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
1021 |
"Modified: 2.4.1997 / 16:20:29 / cg" |
9192 | 1022 |
! ! |
1023 |
||
1024 |
!CharacterArray class methodsFor:'queries'! |
|
1025 |
||
1026 |
isAbstract |
|
11229 | 1027 |
"Return if this class is an abstract class. |
1028 |
True is returned for CharacterArray here; false for subclasses. |
|
19464 | 1029 |
Abstract subclasses must redefine this again." |
11229 | 1030 |
|
9192 | 1031 |
^ self == CharacterArray |
18597 | 1032 |
! |
1033 |
||
1034 |
speciesForCharacterSize:characterSize |
|
1035 |
"answer the class, that is able to hold characters of size characterSize" |
|
1036 |
||
1037 |
characterSize <= 8 ifTrue:[^ String]. |
|
1038 |
characterSize <= 16 ifTrue:[^ Unicode16String]. |
|
1039 |
^ Unicode32String |
|
9192 | 1040 |
! ! |
1041 |
||
20873 | 1042 |
|
9192 | 1043 |
!CharacterArray methodsFor:'Compatibility-ANSI'! |
1044 |
||
1045 |
addLineDelimiters |
|
1046 |
"Ansi compatibility - same as withCRs" |
|
1047 |
||
1048 |
^ self withCRs |
|
1049 |
||
1050 |
"Modified: / 13.11.2001 / 19:16:25 / cg" |
|
17543 | 1051 |
! ! |
1052 |
||
1053 |
!CharacterArray methodsFor:'Compatibility-Dolphin'! |
|
9192 | 1054 |
|
1055 |
copyExpanding:expandTable |
|
1056 |
"return a copy of myself, with translations from the expandTable sliced in. |
|
18392 | 1057 |
The argument is supposed to map from characters to either characters or strings. |
1058 |
Added for Dolphin compatibility" |
|
9192 | 1059 |
|
1060 |
|ds| |
|
1061 |
||
1062 |
ds := WriteStream on:(self species new). |
|
1063 |
self do:[:eachChar | |
|
20874 | 1064 |
|repl| |
1065 |
||
1066 |
repl := expandTable at:eachChar ifAbsent:[nil]. |
|
1067 |
repl isNil ifTrue:[ |
|
1068 |
ds nextPut:eachChar |
|
1069 |
] ifFalse:[ |
|
1070 |
repl size == 0 ifTrue:[ |
|
1071 |
ds nextPut:repl |
|
1072 |
] ifFalse:[ |
|
1073 |
ds nextPutAll:repl |
|
1074 |
] |
|
1075 |
]. |
|
9192 | 1076 |
]. |
6870 | 1077 |
^ ds contents. |
1078 |
! |
|
1079 |
||
1080 |
formatWith:aString |
|
11955 | 1081 |
"Compatibility method - do not use in new code. |
18392 | 1082 |
same as #bindWith: for Dolphin compatibility" |
6870 | 1083 |
|
1084 |
^ self bindWith:aString |
|
1085 |
||
1086 |
" |
|
1087 |
'hello%1world' formatWith:'123' |
|
1088 |
" |
|
1089 |
! |
|
1090 |
||
1091 |
formatWith:arg1 with:arg2 |
|
11955 | 1092 |
"Compatibility method - do not use in new code. |
18392 | 1093 |
same as #bindWith: for Dolphin compatibility" |
6870 | 1094 |
|
1095 |
^ self bindWith:arg1 with:arg2 |
|
1096 |
||
1097 |
" |
|
1098 |
'hello%1 %2world' formatWith:'123' with:234 |
|
1099 |
" |
|
1100 |
! |
|
1101 |
||
1102 |
formatWith:arg1 with:arg2 with:arg3 |
|
11955 | 1103 |
"Compatibility method - do not use in new code. |
18392 | 1104 |
same as #bindWith: for Dolphin compatibility" |
6870 | 1105 |
|
1106 |
^ self bindWith:arg1 with:arg2 with:arg3 |
|
1107 |
||
1108 |
" |
|
1109 |
'hello%1 %2 %3world' formatWith:'123' with:234 with:345 |
|
1110 |
" |
|
6391 | 1111 |
! ! |
1112 |
||
14681 | 1113 |
!CharacterArray methodsFor:'Compatibility-GNU'! |
1114 |
||
1115 |
% anArrayOfOperands |
|
1116 |
"return a copy of the receiver, where a '%i' escape |
|
1117 |
is replaced by the coresponding string from the argument array. |
|
1118 |
'i' may be between 1 and 9 (i.e. a maximum of 9 placeholders is allowed). |
|
1119 |
Added for GNU-ST compatibility." |
|
1120 |
||
1121 |
^ self expandPlaceholdersWith:anArrayOfOperands |
|
1122 |
||
1123 |
" |
|
1124 |
'do you prefer %1 or rather %2 (not talking about %3) ?' |
|
20874 | 1125 |
% #('smalltalk' 'c++' 'c') |
14681 | 1126 |
|
1127 |
'do you %(what) ?' |
|
20874 | 1128 |
% (Dictionary new at:#'what' put:'understand'; yourself) |
14681 | 1129 |
" |
1130 |
! ! |
|
1131 |
||
7261 | 1132 |
!CharacterArray methodsFor:'Compatibility-ST/V'! |
608 | 1133 |
|
17517 | 1134 |
asArrayOfSubstrings |
1135 |
"return an array of substrings from the receiver, interpreting |
|
1136 |
separators (i.e. spaces & newlines) as word-delimiters. |
|
18390 | 1137 |
This has been added for ST/V compatibility |
1138 |
- the actual work is done in asCollectionOfWords. |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
1139 |
(sigh: it is called #'subStrings' in V'Age, #'substrings' in Squeak |
17517 | 1140 |
and #'asCollectionOfWords' in ST/X) " |
1141 |
||
1142 |
^ self asCollectionOfWords asArray |
|
1143 |
||
1144 |
" |
|
1145 |
'1 one two three four 5 five' asArrayOfSubstrings |
|
1146 |
'1 |
|
1147 |
one |
|
20874 | 1148 |
two three four 5 five' asArrayOfSubstrings |
17517 | 1149 |
" |
1150 |
! |
|
1151 |
||
608 | 1152 |
byteAt:index put:aByte |
1153 |
"store a byte at given index. |
|
1154 |
This is an ST/V compatibility method." |
|
1155 |
||
1333
d3fdca956f0d
allow storing of 0-bytes (be careful with printing then ...)
Claus Gittinger <cg@exept.de>
parents:
1312
diff
changeset
|
1156 |
"/ (aByte == 0) ifTrue:[ |
d3fdca956f0d
allow storing of 0-bytes (be careful with printing then ...)
Claus Gittinger <cg@exept.de>
parents:
1312
diff
changeset
|
1157 |
"/ "store a space instead" |
d3fdca956f0d
allow storing of 0-bytes (be careful with printing then ...)
Claus Gittinger <cg@exept.de>
parents:
1312
diff
changeset
|
1158 |
"/ ^ 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
|
1159 |
"/ ]. |
11754
5132248cfd6e
common definition of #byteAt:put between CharacterArray and Symbol
Stefan Vogel <sv@exept.de>
parents:
11632
diff
changeset
|
1160 |
^ self basicAt:index put:(Character value:aByte) |
9192 | 1161 |
|
1162 |
" |
|
1163 |
'hello' copy at:1 put:$H asciiValue; yourself |
|
1164 |
'hello' copy byteAt:1 put:72; yourself |
|
1165 |
'hello' copy byteAt:1 put:0; yourself |
|
1166 |
" |
|
1167 |
||
1168 |
"Modified: 6.5.1996 / 10:35:26 / cg" |
|
1169 |
! |
|
608 | 1170 |
|
19497 | 1171 |
equalsIgnoreCase:aString |
1172 |
"This is an ST/V compatibility method and an alias for sameAs:." |
|
1173 |
||
1174 |
^ self sameAs:aString |
|
1175 |
||
1176 |
" |
|
1177 |
'abc' equalsIgnoreCase: 'aBC' |
|
1178 |
" |
|
1179 |
! |
|
1180 |
||
608 | 1181 |
replChar:oldChar with:newChar |
19497 | 1182 |
"return a copy of the receiver, with all oldChars replaced by newChar. |
1183 |
This is an ST/V compatibility method and an alias for copyReplaceAll." |
|
608 | 1184 |
|
3656 | 1185 |
^ self copyReplaceAll:oldChar with:newChar |
608 | 1186 |
|
1187 |
" |
|
8874 | 1188 |
'12345678901234567890' replChar:$0 with:$* |
608 | 1189 |
" |
3656 | 1190 |
|
1191 |
"Modified: / 18.7.1998 / 22:52:57 / cg" |
|
608 | 1192 |
! |
1193 |
||
1194 |
replChar:oldChar withString:newString |
|
1195 |
"return a copy of the receiver, with all oldChars replaced |
|
1196 |
by newString (i.e. slice in the newString in place of the oldChar). |
|
1197 |
This is an ST/V compatibility method." |
|
1198 |
||
1199 |
|tmpStream| |
|
1200 |
||
15523 | 1201 |
tmpStream := self species writeStream. |
608 | 1202 |
self do:[:element | |
20874 | 1203 |
element = oldChar ifTrue:[ |
1204 |
tmpStream nextPutAll:newString |
|
1205 |
] ifFalse:[ |
|
1206 |
tmpStream nextPut:element |
|
1207 |
]. |
|
608 | 1208 |
]. |
1209 |
^ tmpStream contents |
|
1210 |
||
1211 |
" |
|
8874 | 1212 |
'12345678901234567890' replChar:$0 withString:'foo' |
1213 |
'a string with spaces' replChar:$ withString:' foo ' |
|
608 | 1214 |
" |
9192 | 1215 |
! |
1216 |
||
1217 |
replString:subString withString:newString |
|
4244
5deefa3144fb
added #replString:withString:
Claus Gittinger <cg@exept.de>
parents:
4156
diff
changeset
|
1218 |
"return a copy of the receiver, with all sequences of subString replaced |
18390 | 1219 |
by newString (i.e. slice in the newString in place of the oldString). |
19497 | 1220 |
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
|
1221 |
|
8347 | 1222 |
^ self copyReplaceString:subString withString:newString |
4244
5deefa3144fb
added #replString:withString:
Claus Gittinger <cg@exept.de>
parents:
4156
diff
changeset
|
1223 |
|
5deefa3144fb
added #replString:withString:
Claus Gittinger <cg@exept.de>
parents:
4156
diff
changeset
|
1224 |
" |
8874 | 1225 |
'12345678901234567890' replString:'123' withString:'OneTwoThree' |
1226 |
'12345678901234567890' replString:'123' withString:'*' |
|
1227 |
'12345678901234567890' replString:'234' withString:'foo' |
|
4244
5deefa3144fb
added #replString:withString:
Claus Gittinger <cg@exept.de>
parents:
4156
diff
changeset
|
1228 |
|
5deefa3144fb
added #replString:withString:
Claus Gittinger <cg@exept.de>
parents:
4156
diff
changeset
|
1229 |
('a string with spaces' replChar:$ withString:' foo ') |
20874 | 1230 |
replString:'foo' withString:'bar' |
4244
5deefa3144fb
added #replString:withString:
Claus Gittinger <cg@exept.de>
parents:
4156
diff
changeset
|
1231 |
" |
5deefa3144fb
added #replString:withString:
Claus Gittinger <cg@exept.de>
parents:
4156
diff
changeset
|
1232 |
|
8347 | 1233 |
"Modified: / 12-05-2004 / 12:00:27 / cg" |
9192 | 1234 |
! |
1235 |
||
9275 | 1236 |
subString:start to:end |
18390 | 1237 |
"same as copyFrom:to: |
19497 | 1238 |
This is an ST/V compatibility method and an alias for copyFrom:to:." |
18390 | 1239 |
|
9275 | 1240 |
^ self copyFrom:start to:end |
1241 |
||
1242 |
" |
|
1243 |
'12345678901234567890' subString:3 to:8 |
|
1244 |
" |
|
1245 |
! |
|
1246 |
||
9192 | 1247 |
trimBlanks |
14992 | 1248 |
"return a copy of the receiver without leading and trailing spaces. |
18464 | 1249 |
This is an ST/V compatibility method and an alias for withoutSpaces." |
9192 | 1250 |
|
1251 |
^ self withoutSpaces |
|
1252 |
||
1253 |
" |
|
1254 |
' spaces at beginning' trimBlanks |
|
1255 |
'spaces at end ' trimBlanks |
|
1256 |
' spaces at beginning and end ' trimBlanks |
|
1257 |
'no spaces' trimBlanks |
|
1258 |
" |
|
1259 |
! ! |
|
1260 |
||
1261 |
!CharacterArray methodsFor:'Compatibility-Squeak'! |
|
4409
86e049b21f8d
added #includesSubstring:caseSensitive:
Claus Gittinger <cg@exept.de>
parents:
4389
diff
changeset
|
1262 |
|
19171 | 1263 |
asBoldText |
1264 |
"return self as a bold text" |
|
1265 |
||
1266 |
^Text string: self emphasis: #bold |
|
1267 |
! |
|
1268 |
||
6148 | 1269 |
asDate |
1270 |
"Many allowed forms, see Date.readFrom:" |
|
1271 |
||
1272 |
^ Date readFrom: (ReadStream on: self) |
|
1273 |
||
1274 |
" |
|
8874 | 1275 |
'30 Apr 1999' asDate dayName capitalized |
6148 | 1276 |
" |
1277 |
! |
|
1278 |
||
10841 | 1279 |
caseInsensitiveLessOrEqual:aString |
18391 | 1280 |
"compare the receiver against the argument ignoring case differences |
1281 |
For Squeak/Pharo compatibility" |
|
1282 |
||
10841 | 1283 |
^ (self compareCaselessWith:aString) <= 0 |
1284 |
! |
|
1285 |
||
1286 |
caseSensitiveLessOrEqual:aString |
|
18391 | 1287 |
"compare the receiver against the argument caring for case differences |
1288 |
For Squeak/Pharo compatibility" |
|
1289 |
||
10841 | 1290 |
^ self <= aString |
1291 |
! |
|
1292 |
||
1293 |
charactersExactlyMatching:aString |
|
14405
1d0d99a95bba
comment/format in:7 methods
Claus Gittinger <cg@exept.de>
parents:
14168
diff
changeset
|
1294 |
"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
|
1295 |
|
10841 | 1296 |
|idx nMax| |
1297 |
||
1298 |
nMax :=(self size) min:(aString size). |
|
1299 |
idx := 1. |
|
1300 |
[idx <= nMax] whileTrue:[ |
|
20874 | 1301 |
(self at:idx) = (aString at:idx) ifFalse:[ |
1302 |
^ idx - 1 |
|
1303 |
]. |
|
1304 |
idx := idx + 1. |
|
10841 | 1305 |
]. |
1306 |
^ nMax |
|
1307 |
||
1308 |
" |
|
12177 | 1309 |
'abc' charactersExactlyMatching:'abc' |
1310 |
'abc' charactersExactlyMatching:'abcd' |
|
1311 |
'abcd' charactersExactlyMatching:'abc' |
|
1312 |
'abc' charactersExactlyMatching:'abd' |
|
1313 |
'abc' charactersExactlyMatching:'xxx' |
|
10841 | 1314 |
" |
1315 |
! |
|
1316 |
||
9192 | 1317 |
endsWithDigit |
1318 |
"Answer whether the receiver's final character represents a digit. 3/11/96 sw" |
|
1319 |
||
18391 | 1320 |
|len| |
1321 |
||
20873 | 1322 |
^ (len := self size) ~~ 0 and:[(self at:len) isDigit] |
9192 | 1323 |
! |
1324 |
||
10703
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
1325 |
findDelimiters:delimiters startingAt:start |
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
1326 |
"Answer the index of the character within the receiver, starting at start, |
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
1327 |
that matches one of the delimiters. |
18391 | 1328 |
If the receiver does not contain any of the delimiters, answer size + 1." |
9192 | 1329 |
|
1330 |
|idx| |
|
1331 |
||
1332 |
idx := self indexOfAny:delimiters startingAt:start. |
|
1333 |
idx == 0 ifTrue:[^ self size + 1]. |
|
1334 |
^ idx. |
|
1335 |
"/start to: self size do: [:i | |
|
1336 |
"/ |char| |
|
1337 |
"/ |
|
1338 |
"/ char := self at: i. |
|
1339 |
"/ delimiters do: [:delim | delim = char ifTrue: [^ i]] |
|
8971 | 1340 |
"/ ]. |
1341 |
"/ ^ self size + 1 |
|
1342 |
! |
|
1343 |
||
13624 | 1344 |
findTokens:delimiterOrDelimiters |
7270 | 1345 |
"cg: I am not sure, if this is really the squeak semantics (w.r.t. empty fields)" |
1346 |
||
13624 | 1347 |
delimiterOrDelimiters size == 0 ifTrue:[ |
20874 | 1348 |
^ self asCollectionOfSubstringsSeparatedBy:delimiterOrDelimiters |
13624 | 1349 |
] ifFalse:[ |
20874 | 1350 |
^ self asCollectionOfSubstringsSeparatedByAny:delimiterOrDelimiters |
13624 | 1351 |
]. |
7270 | 1352 |
|
1353 |
" |
|
8874 | 1354 |
'a|b#c||e' findTokens:#($# $|) |
13624 | 1355 |
'a|b#c||e' findTokens:$| |
1356 |
" |
|
1357 |
||
1358 |
"Modified: / 04-09-2011 / 20:31:21 / cg" |
|
1359 |
"Modified (comment): / 05-09-2011 / 04:25:28 / cg" |
|
4981 | 1360 |
! |
1361 |
||
4974 | 1362 |
includesSubString:aString |
1363 |
"return true, if a substring is contained in the receiver. |
|
1364 |
The compare is case sensitive." |
|
1365 |
||
1366 |
^ self includesString:aString |
|
1367 |
||
1368 |
" |
|
8874 | 1369 |
'hello world' includesSubString:'Hel' |
9192 | 1370 |
'hello world' includesSubString:'hel' |
1371 |
'hello world' includesSubString:'llo' |
|
1372 |
" |
|
1373 |
||
1374 |
||
1375 |
||
1376 |
! |
|
1377 |
||
11178 | 1378 |
includesSubString:aString caseSensitive:caseSensitive |
18391 | 1379 |
"sigh - an alias; added for Squeak/Pharo compatibility" |
1380 |
||
11179 | 1381 |
^ self includesSubstring:aString caseSensitive:caseSensitive |
11178 | 1382 |
! |
1383 |
||
16875
32e61bb3a742
class: CharacterArray>>#includesSubstring: (for Squeak compatibility)
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
16863
diff
changeset
|
1384 |
includesSubstring: aString |
18391 | 1385 |
"sigh - an alias; added for Squeak/Pharo compatibility" |
1386 |
||
16875
32e61bb3a742
class: CharacterArray>>#includesSubstring: (for Squeak compatibility)
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
16863
diff
changeset
|
1387 |
^ self includesSubString: aString |
32e61bb3a742
class: CharacterArray>>#includesSubstring: (for Squeak compatibility)
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
16863
diff
changeset
|
1388 |
|
32e61bb3a742
class: CharacterArray>>#includesSubstring: (for Squeak compatibility)
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
16863
diff
changeset
|
1389 |
"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
|
1390 |
! |
32e61bb3a742
class: CharacterArray>>#includesSubstring: (for Squeak compatibility)
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
16863
diff
changeset
|
1391 |
|
9192 | 1392 |
includesSubstring:aString caseSensitive:caseSensitive |
9190 | 1393 |
"return true, if a substring is contained in the receiver. |
9192 | 1394 |
The argument, caseSensitive controls if case is ignored in the compare." |
1395 |
||
1396 |
"/ for now, a q&d hack ... |
|
1397 |
||
1398 |
caseSensitive ifFalse:[ |
|
20874 | 1399 |
^ self asLowercase includesString:aString asLowercase |
9192 | 1400 |
]. |
9190 | 1401 |
^ self includesString:aString |
1402 |
||
1403 |
" |
|
9192 | 1404 |
'hello world' includesSubstring:'Hel' caseSensitive:true |
1405 |
'hello world' includesSubstring:'Hel' caseSensitive:false |
|
1406 |
" |
|
1407 |
||
1408 |
||
1409 |
||
1410 |
! |
|
9190 | 1411 |
|
6979 | 1412 |
isAllDigits |
1413 |
"Answer whether the receiver's characters are all digits" |
|
1414 |
||
1415 |
^ self conform:[:eachChar | eachChar isDigit] |
|
1416 |
||
1417 |
" |
|
1418 |
'hello world' isAllDigits |
|
1419 |
'12344' isAllDigits |
|
1420 |
" |
|
1421 |
! |
|
1422 |
||
4890 | 1423 |
lastSpacePosition |
18391 | 1424 |
"return the index of the last space character; 0 if there is none. |
1425 |
Added for Squeak/Pharo compatibility" |
|
14405
1d0d99a95bba
comment/format in:7 methods
Claus Gittinger <cg@exept.de>
parents:
14168
diff
changeset
|
1426 |
|
20739 | 1427 |
"/ CG: is this correct? separator or space only??? |
4890 | 1428 |
^ self lastIndexOfSeparator |
1429 |
! |
|
1430 |
||
20738 | 1431 |
linesDo:aBlock |
1432 |
"evaluate the argument, aBlock for all lines, |
|
1433 |
up to the end" |
|
1434 |
||
1435 |
^ self readStream linesDo:aBlock |
|
1436 |
! |
|
1437 |
||
7022 | 1438 |
padded:leftOrRight to:paddedSize with:padCharacter |
20155 | 1439 |
"pad left (leftOrRight == #left) or right" |
14053
acbb97913c30
comment/format in: #padded:to:with:
Claus Gittinger <cg@exept.de>
parents:
14050
diff
changeset
|
1440 |
|
7022 | 1441 |
leftOrRight == #left ifTrue:[ |
20155 | 1442 |
^ self leftPaddedTo:paddedSize with:padCharacter |
9190 | 1443 |
]. |
1444 |
^ self paddedTo:paddedSize with:padCharacter |
|
1445 |
||
1446 |
" |
|
8874 | 1447 |
'hello' padded:#right to:10 with:$. |
9192 | 1448 |
'hello' padded:#left to:10 with:$. |
1449 |
" |
|
14053
acbb97913c30
comment/format in: #padded:to:with:
Claus Gittinger <cg@exept.de>
parents:
14050
diff
changeset
|
1450 |
|
acbb97913c30
comment/format in: #padded:to:with:
Claus Gittinger <cg@exept.de>
parents:
14050
diff
changeset
|
1451 |
"Modified (comment): / 07-03-2012 / 16:31:33 / cg" |
9192 | 1452 |
! |
1453 |
||
1454 |
skipDelimiters:delimiters startingAt:start |
|
1455 |
"Answer the index of the character within the receiver, starting at start, |
|
1456 |
that does NOT match one of the delimiters. |
|
1457 |
If the receiver does not contain any of the delimiters, answer size + 1. |
|
1458 |
Assumes the delimiters to be a non-empty string." |
|
1459 |
||
1460 |
start to:self size do:[:i | |
|
20874 | 1461 |
delimiters detect:[:delim | delim = (self at:i) ] ifNone:[ ^ i ] |
9192 | 1462 |
]. |
1463 |
^ self size + 1 |
|
1464 |
||
1465 |
" |
|
1466 |
'123***7890' skipDelimiters:'*' startingAt:4 |
|
1467 |
'123***7890' skipDelimiters:'*' startingAt:3 |
|
9190 | 1468 |
'123***7890' skipDelimiters:'*' startingAt:10 |
1469 |
'123*******' skipDelimiters:'*' startingAt:10 |
|
8874 | 1470 |
" |
1471 |
! |
|
1472 |
||
9190 | 1473 |
substrings |
18921 | 1474 |
"return a collection consisting of all words contained in the receiver. |
17517 | 1475 |
Words are separated by whitespace. |
1476 |
This has been added for Squeak compatibility. |
|
1477 |
(sigh: it is called #'subStrings' in V'Age, and #'asCollectionOfWords' in ST/X) " |
|
1478 |
||
9190 | 1479 |
^ self asCollectionOfWords |
1480 |
||
1481 |
" |
|
1482 |
'foo bar baz' substrings |
|
1483 |
" |
|
1484 |
! |
|
1485 |
||
12665
b027fc5aec4e
added: #substringsSeparatedBy:
Claus Gittinger <cg@exept.de>
parents:
12664
diff
changeset
|
1486 |
substringsSeparatedBy:separatorCharacter |
18921 | 1487 |
"return a collection consisting of all words contained in the receiver. |
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
1488 |
Words are separated by the given separator character. |
18921 | 1489 |
This has been added for Squeak/Pharo compatibility. |
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
1490 |
(sigh: it is called #'subStrings:' in V'Age, |
18921 | 1491 |
and #'asCollectionOfSubstringsSeparatedBy' in ST/X) " |
18391 | 1492 |
|
12665
b027fc5aec4e
added: #substringsSeparatedBy:
Claus Gittinger <cg@exept.de>
parents:
12664
diff
changeset
|
1493 |
^ self asCollectionOfSubstringsSeparatedBy:separatorCharacter |
b027fc5aec4e
added: #substringsSeparatedBy:
Claus Gittinger <cg@exept.de>
parents:
12664
diff
changeset
|
1494 |
|
b027fc5aec4e
added: #substringsSeparatedBy:
Claus Gittinger <cg@exept.de>
parents:
12664
diff
changeset
|
1495 |
" |
b027fc5aec4e
added: #substringsSeparatedBy:
Claus Gittinger <cg@exept.de>
parents:
12664
diff
changeset
|
1496 |
'foo bar, baz' substringsSeparatedBy:$, |
b027fc5aec4e
added: #substringsSeparatedBy:
Claus Gittinger <cg@exept.de>
parents:
12664
diff
changeset
|
1497 |
" |
b027fc5aec4e
added: #substringsSeparatedBy:
Claus Gittinger <cg@exept.de>
parents:
12664
diff
changeset
|
1498 |
! |
b027fc5aec4e
added: #substringsSeparatedBy:
Claus Gittinger <cg@exept.de>
parents:
12664
diff
changeset
|
1499 |
|
9190 | 1500 |
truncateTo:smallSize |
9192 | 1501 |
"return myself or a copy shortened to smallSize. 1/18/96 sw" |
1502 |
||
1503 |
self size <= smallSize ifTrue:[^ self]. |
|
1504 |
^ self copyFrom: 1 to: smallSize |
|
1505 |
||
1506 |
" |
|
1507 |
'hello world' truncateTo:5 |
|
1508 |
'hello' truncateTo:10 |
|
1509 |
||
1510 |
'hello world' copyTo:5 |
|
1511 |
'hello' copyTo:10 |
|
1512 |
" |
|
1513 |
! |
|
5795 | 1514 |
|
4890 | 1515 |
withBlanksTrimmed |
18464 | 1516 |
"Return a copy of the receiver from which leading and trailing whitespace have been trimmed. |
1517 |
Notice the bad naming - it is trimming separators, not just blanks. |
|
1518 |
Added for Squeak compatibility and an alias for withoutSeparators" |
|
4890 | 1519 |
|
14865 | 1520 |
^ self withoutSeparators "withoutSpaces" "/ cg: it seems that squeak cares for any whitespace |
4890 | 1521 |
|
1522 |
" |
|
8874 | 1523 |
' hello world ' withBlanksTrimmed |
4890 | 1524 |
" |
1525 |
! |
|
1526 |
||
1527 |
withNoLineLongerThan: aNumber |
|
1528 |
"Answer a string with the same content as receiver, but rewrapped so that no line has more characters than the given number" |
|
1529 |
||
9192 | 1530 |
| listOfLines currentLast currentStart resultString putativeLast putativeLine crPosition | |
1531 |
||
14451
c06e7885b675
changed: #withNoLineLongerThan:
Claus Gittinger <cg@exept.de>
parents:
14420
diff
changeset
|
1532 |
(aNumber isNumber not or:[ aNumber < 1] ) ifTrue: [self error: 'too narrow']. |
9192 | 1533 |
listOfLines _ OrderedCollection new. |
1534 |
currentLast _ 0. |
|
1535 |
[currentLast < self size] whileTrue: |
|
20874 | 1536 |
[currentStart _ currentLast + 1. |
1537 |
putativeLast _ (currentStart + aNumber - 1) min: self size. |
|
1538 |
putativeLine _ self copyFrom: currentStart to: putativeLast. |
|
1539 |
(crPosition _ putativeLine indexOf: Character cr) > 0 ifTrue: |
|
1540 |
[putativeLast _ currentStart + crPosition - 1. |
|
1541 |
putativeLine _ self copyFrom: currentStart to: putativeLast]. |
|
1542 |
currentLast _ putativeLast == self size |
|
1543 |
ifTrue: |
|
1544 |
[putativeLast] |
|
1545 |
ifFalse: |
|
1546 |
[currentStart + putativeLine lastSpacePosition - 1]. |
|
1547 |
currentLast <= currentStart ifTrue: |
|
1548 |
["line has NO spaces; baleout!!" |
|
1549 |
currentLast _ putativeLast]. |
|
1550 |
listOfLines add: (self copyFrom: currentStart to: currentLast) withBlanksTrimmed]. |
|
9192 | 1551 |
|
1552 |
listOfLines size > 0 ifFalse: [^ '']. |
|
1553 |
resultString _ listOfLines first. |
|
1554 |
2 to: listOfLines size do: |
|
20874 | 1555 |
[:i | resultString _ resultString, Character cr asString, (listOfLines at: i)]. |
9192 | 1556 |
^ resultString |
1557 |
||
1558 |
" |
|
1559 |
#(5 7 20) collect: |
|
20874 | 1560 |
[: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
|
1561 |
" |
10883 | 1562 |
! |
1563 |
||
12343
0a93eee37d65
added: #withSqueakLineEndings
Claus Gittinger <cg@exept.de>
parents:
12330
diff
changeset
|
1564 |
withSqueakLineEndings |
13882
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
1565 |
"assume the string is textual, and that CR, LF, and CRLF are all |
20258 | 1566 |
valid line endings. Replace each occurrence with a single CR" |
12343
0a93eee37d65
added: #withSqueakLineEndings
Claus Gittinger <cg@exept.de>
parents:
12330
diff
changeset
|
1567 |
|
0a93eee37d65
added: #withSqueakLineEndings
Claus Gittinger <cg@exept.de>
parents:
12330
diff
changeset
|
1568 |
^ self asStringCollection asStringWith:Character cr. |
0a93eee37d65
added: #withSqueakLineEndings
Claus Gittinger <cg@exept.de>
parents:
12330
diff
changeset
|
1569 |
! |
0a93eee37d65
added: #withSqueakLineEndings
Claus Gittinger <cg@exept.de>
parents:
12330
diff
changeset
|
1570 |
|
12188 | 1571 |
withoutLeading: char |
1572 |
"return a copy of myself without leading a char. |
|
1573 |
Returns an empty string, if the receiver consist only of a char." |
|
1574 |
||
1575 |
^ self withoutLeadingForWhich:[:ch | ch = char] |
|
1576 |
||
1577 |
" |
|
13882
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
1578 |
'****foo****' withoutLeading: $* |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
1579 |
'foo****' withoutLeading: $* |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
1580 |
'*' withoutLeading: $* |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
1581 |
'' withoutLeading: $* |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
1582 |
'****foo' withoutLeading: $* |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
1583 |
'*******' 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 |
'f***o***o' withoutLeading: $* |
12188 | 1586 |
('**' , Character tab asString , '*foo***') withoutLeading: $* inspect |
1587 |
" |
|
1588 |
! |
|
1589 |
||
11565 | 1590 |
withoutTrailing:char |
1591 |
"return a copy of myself without trailing char. |
|
1592 |
Returns an empty string, if the receiver consist only of char." |
|
1593 |
||
12188 | 1594 |
^ self withoutTrailingForWhich:[:ch | ch = char] |
1595 |
||
1596 |
" |
|
13882
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
1597 |
' foo....' withoutTrailing:$. |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
1598 |
'foo....' withoutTrailing:$. |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
1599 |
' foo' withoutTrailing:$. |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
1600 |
'.......' withoutTrailing:$. |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
1601 |
'foo' withoutTrailing:$. |
11565 | 1602 |
" |
9190 | 1603 |
! ! |
1604 |
||
1605 |
!CharacterArray methodsFor:'Compatibility-V''Age'! |
|
1606 |
||
1607 |
addLineDelimiter |
|
1608 |
"replace all '\'-characters by line delimiter (cr) - characters. |
|
9192 | 1609 |
This has been added for VisualAge compatibility." |
1610 |
||
1611 |
^ self withCRs |
|
1612 |
! |
|
1613 |
||
1614 |
bindWith:aString |
|
1615 |
"return a copy of the receiver, where a '%1' escape is |
|
1616 |
replaced by aString. |
|
1617 |
This has been added for VisualAge compatibility." |
|
1618 |
||
1619 |
^ self expandPlaceholdersWith:(Array with:aString) |
|
1620 |
||
1621 |
" |
|
1622 |
'do you like %1 ?' bindWith:'smalltalk' |
|
19785 | 1623 |
'do you like %(foo) ?' bindWithArguments:(Dictionary new at:'foo' put:'smalltalk'; yourself) |
9192 | 1624 |
" |
1625 |
! |
|
608 | 1626 |
|
1627 |
bindWith:string1 with:string2 |
|
1628 |
"return a copy of the receiver, where a '%1' escape is |
|
1629 |
replaced by string1 and '%2' is replaced by string2. |
|
1630 |
This has been added for VisualAge compatibility." |
|
1631 |
||
1632 |
^ self expandPlaceholdersWith:(Array with:string1 with:string2) |
|
1633 |
||
1634 |
" |
|
1635 |
'do you prefer %1 or rather %2 ?' |
|
20874 | 1636 |
bindWith:'smalltalk' with:'c++' |
9192 | 1637 |
" |
1638 |
! |
|
9190 | 1639 |
|
1640 |
bindWith:str1 with:str2 with:str3 |
|
608 | 1641 |
"return a copy of the receiver, where a '%1', '%2' and '%3' escapes |
1642 |
are replaced by str1, str2 and str3 respectively. |
|
1643 |
This has been added for VisualAge compatibility." |
|
1644 |
||
1645 |
^ self expandPlaceholdersWith:(Array with:str1 with:str2 with:str3) |
|
1646 |
||
1647 |
" |
|
9192 | 1648 |
'do you prefer %1 or rather %2 (not talking about %3) ?' |
20874 | 1649 |
bindWith:'smalltalk' with:'c++' with:'c' |
9192 | 1650 |
" |
1651 |
! |
|
608 | 1652 |
|
1653 |
bindWith:str1 with:str2 with:str3 with:str4 |
|
1654 |
"return a copy of the receiver, where a '%1', '%2', '%3' and '%4' escapes |
|
1655 |
are replaced by str1, str2, str3 and str4 respectively. |
|
1656 |
This has been added for VisualAge compatibility." |
|
1657 |
||
1658 |
^ self expandPlaceholdersWith:(Array with:str1 with:str2 with:str3 with:str4) |
|
1659 |
||
1660 |
" |
|
9192 | 1661 |
'do you prefer %1 or rather %2 (not talking about %3 or even %4) ?' |
20874 | 1662 |
bindWith:'smalltalk' with:'c++' with:'c' with:'assembler' |
9192 | 1663 |
" |
1664 |
! |
|
608 | 1665 |
|
2344 | 1666 |
bindWith:str1 with:str2 with:str3 with:str4 with:str5 |
1667 |
"return a copy of the receiver, where a '%1' .. '%5' escapes |
|
1668 |
are replaced by str1 .. str5 respectively. |
|
1669 |
This has been added for VisualAge compatibility." |
|
1670 |
||
1671 |
^ self expandPlaceholdersWith:(Array with:str1 with:str2 with:str3 with:str4 with:str5) |
|
1672 |
||
1673 |
"Created: 31.1.1997 / 16:25:42 / cg" |
|
1674 |
! |
|
1675 |
||
5343 | 1676 |
bindWith:str1 with:str2 with:str3 with:str4 with:str5 with:str6 |
1677 |
"return a copy of the receiver, where a '%1' .. '%6' escapes |
|
11524 | 1678 |
are replaced by str1 .. str6 respectively. |
5343 | 1679 |
This has been added for VisualAge compatibility." |
1680 |
||
8874 | 1681 |
^ self expandPlaceholdersWith:(Array with:str1 with:str2 |
20874 | 1682 |
with:str3 with:str4 |
1683 |
with:str5 with:str6) |
|
11524 | 1684 |
! |
1685 |
||
1686 |
bindWith:str1 with:str2 with:str3 with:str4 with:str5 with:str6 with:str7 |
|
1687 |
"return a copy of the receiver, where a '%1' .. '%7' escapes |
|
1688 |
are replaced by str1 .. str7 respectively. |
|
1689 |
This has been added for VisualAge compatibility." |
|
1690 |
||
1691 |
^ self expandPlaceholdersWith:(Array with:str1 with:str2 |
|
20874 | 1692 |
with:str3 with:str4 |
1693 |
with:str5 with:str6 |
|
1694 |
with:str7) |
|
5343 | 1695 |
! |
1696 |
||
13996
a86faa4f17b8
added: #bindWith:with:with:with:with:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
13987
diff
changeset
|
1697 |
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
|
1698 |
"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
|
1699 |
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
|
1700 |
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
|
1701 |
|
a86faa4f17b8
added: #bindWith:with:with:with:with:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
13987
diff
changeset
|
1702 |
^ self expandPlaceholdersWith:(Array with:str1 with:str2 |
20874 | 1703 |
with:str3 with:str4 |
1704 |
with:str5 with:str6 |
|
1705 |
with:str7 with:str8) |
|
13996
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 |
"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
|
1708 |
! |
a86faa4f17b8
added: #bindWith:with:with:with:with:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
13987
diff
changeset
|
1709 |
|
14014
3aff2d7c41c8
added: #bindWith:with:with:with:with:with:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
13996
diff
changeset
|
1710 |
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
|
1711 |
"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
|
1712 |
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
|
1713 |
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
|
1714 |
|
3aff2d7c41c8
added: #bindWith:with:with:with:with:with:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
13996
diff
changeset
|
1715 |
^ self expandPlaceholdersWith:(Array with:str1 with:str2 |
20874 | 1716 |
with:str3 with:str4 |
1717 |
with:str5 with:str6 |
|
1718 |
with:str7 with:str8 |
|
1719 |
with:str9) |
|
14014
3aff2d7c41c8
added: #bindWith:with:with:with:with:with:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
13996
diff
changeset
|
1720 |
|
3aff2d7c41c8
added: #bindWith:with:with:with:with:with:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
13996
diff
changeset
|
1721 |
"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
|
1722 |
! |
3aff2d7c41c8
added: #bindWith:with:with:with:with:with:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
13996
diff
changeset
|
1723 |
|
608 | 1724 |
bindWithArguments:anArrayOfStrings |
1725 |
"return a copy of the receiver, where a '%i' escape |
|
1726 |
is replaced by the coresponding string from the argument array. |
|
1727 |
'i' may be between 1 and 9 (i.e. a maximum of 9 placeholders is allowed). |
|
1728 |
This has been added for VisualAge compatibility." |
|
1729 |
||
9192 | 1730 |
^ self expandPlaceholdersWith:anArrayOfStrings |
1731 |
||
1732 |
" |
|
1733 |
'do you prefer %1 or rather %2 (not talking about %3) ?' |
|
20874 | 1734 |
bindWithArguments:#('smalltalk' 'c++' 'c') |
12196 | 1735 |
|
1736 |
'do you %(what) ?' |
|
20874 | 1737 |
bindWithArguments:(Dictionary new at:#'what' put:'understand'; yourself) |
9192 | 1738 |
" |
1739 |
! |
|
608 | 1740 |
|
1741 |
subStrings |
|
18921 | 1742 |
"return a collection consisting of all words contained in the receiver. |
608 | 1743 |
Words are separated by whitespace. |
17517 | 1744 |
This has been added for VisualAge compatibility. |
1745 |
(sigh: it is called #'subbtrings' in Squeak, and #'asCollectionOfWords' in ST/X) " |
|
608 | 1746 |
|
1747 |
^ self asCollectionOfWords |
|
1748 |
||
1749 |
" |
|
1750 |
'hello world, this is smalltalk' subStrings |
|
1751 |
" |
|
1752 |
! |
|
1753 |
||
16181 | 1754 |
subStrings:separatorCharacterOrString |
18921 | 1755 |
"return a collection consisting of all words contained in the receiver. |
608 | 1756 |
Words are separated by separatorCharacter. |
18921 | 1757 |
This is similar to split: (squeak) and asCollectionOfSubstringsSeparatedBy: (st/x) |
1758 |
and has been added for VisualAge compatibility." |
|
608 | 1759 |
|
16181 | 1760 |
separatorCharacterOrString isCharacter ifTrue:[ |
20874 | 1761 |
^ self asCollectionOfSubstringsSeparatedBy:separatorCharacterOrString |
16181 | 1762 |
]. |
1763 |
^ self asCollectionOfSubstringsSeparatedByAny:separatorCharacterOrString |
|
1764 |
||
1765 |
" |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
1766 |
'foo:bar:baz:smalltalk' subStrings:$: |
16181 | 1767 |
'foo:bar:baz:smalltalk' subStrings:':' |
1768 |
'foo.bar,baz-smalltalk' subStrings:'.,-' |
|
9192 | 1769 |
" |
1770 |
! |
|
1771 |
||
1772 |
trimSeparators |
|
18392 | 1773 |
"return a copy of the receiver without leading and trailing whiteSpace. |
18921 | 1774 |
Added for VisualAge compatibility (an alias for withoutSeparators)" |
9192 | 1775 |
|
1776 |
^ self withoutSeparators |
|
1777 |
! ! |
|
1778 |
||
1779 |
!CharacterArray methodsFor:'Compatibility-VW'! |
|
5964 | 1780 |
|
6139 | 1781 |
asLogicalFileSpecification |
1782 |
^ self asFilename |
|
1783 |
||
1784 |
"Created: / 30.10.2001 / 17:29:53 / cg" |
|
1785 |
! |
|
1786 |
||
5964 | 1787 |
expandMacros |
1788 |
"ST80 compatibility - expand '<..>' macros with |
|
1789 |
argument strings. Similar to #bindWith:. |
|
1790 |
Read the comment in #expandMacrosWithArguments: about |
|
1791 |
limited compatibility issues." |
|
1792 |
||
1793 |
^ self expandMacrosWithArguments:#() |
|
1794 |
||
1795 |
" |
|
1796 |
'hello<n>foo' expandMacros |
|
1797 |
" |
|
1798 |
||
9192 | 1799 |
"Modified: / 18.6.1998 / 16:03:02 / cg" |
1800 |
! |
|
1801 |
||
1802 |
expandMacrosWith:arg |
|
5964 | 1803 |
"ST80 compatibility - expand '<..>' macros with |
1804 |
argument strings. Similar to #bindWith:. |
|
1805 |
Read the comment in #expandMacrosWithArguments: about |
|
1806 |
limited compatibility issues." |
|
1807 |
||
9192 | 1808 |
^ self expandMacrosWithArguments:(Array with:arg) |
1809 |
||
1810 |
"Created: / 1.11.1997 / 13:01:28 / cg" |
|
1811 |
"Modified: / 1.11.1997 / 13:30:50 / cg" |
|
1812 |
! |
|
5964 | 1813 |
|
1814 |
expandMacrosWith:arg1 with:arg2 |
|
1815 |
"ST80 compatibility - expand '<..>' macros with |
|
1816 |
argument strings. Similar to #bindWith:. |
|
1817 |
Read the comment in #expandMacrosWithArguments: about |
|
1818 |
limited compatibility issues." |
|
1819 |
||
1820 |
^ self expandMacrosWithArguments:(Array with:arg1 with:arg2) |
|
1821 |
||
1822 |
"Modified: / 6.7.1998 / 21:58:14 / cg" |
|
1823 |
! |
|
1824 |
||
1825 |
expandMacrosWith:arg1 with:arg2 with:arg3 |
|
1826 |
"ST80 compatibility - expand '<..>' macros with |
|
1827 |
argument strings. Similar to #bindWith:. |
|
1828 |
Read the comment in #expandMacrosWithArguments: about |
|
1829 |
limited compatibility issues." |
|
1830 |
||
1831 |
^ self expandMacrosWithArguments:(Array with:arg1 with:arg2 with:arg3) |
|
1832 |
! |
|
1833 |
||
10353
65fb7aa2c6c2
added #expandMacrosWith:with:with:with:
Stefan Vogel <sv@exept.de>
parents:
10134
diff
changeset
|
1834 |
expandMacrosWith:arg1 with:arg2 with:arg3 with:arg4 |
65fb7aa2c6c2
added #expandMacrosWith:with:with:with:
Stefan Vogel <sv@exept.de>
parents:
10134
diff
changeset
|
1835 |
"ST80 compatibility - expand '<..>' macros with |
65fb7aa2c6c2
added #expandMacrosWith:with:with:with:
Stefan Vogel <sv@exept.de>
parents:
10134
diff
changeset
|
1836 |
argument strings. Similar to #bindWith:. |
65fb7aa2c6c2
added #expandMacrosWith:with:with:with:
Stefan Vogel <sv@exept.de>
parents:
10134
diff
changeset
|
1837 |
Read the comment in #expandMacrosWithArguments: about |
65fb7aa2c6c2
added #expandMacrosWith:with:with:with:
Stefan Vogel <sv@exept.de>
parents:
10134
diff
changeset
|
1838 |
limited compatibility issues." |
65fb7aa2c6c2
added #expandMacrosWith:with:with:with:
Stefan Vogel <sv@exept.de>
parents:
10134
diff
changeset
|
1839 |
|
65fb7aa2c6c2
added #expandMacrosWith:with:with:with:
Stefan Vogel <sv@exept.de>
parents:
10134
diff
changeset
|
1840 |
^ 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
|
1841 |
! |
65fb7aa2c6c2
added #expandMacrosWith:with:with:with:
Stefan Vogel <sv@exept.de>
parents:
10134
diff
changeset
|
1842 |
|
9192 | 1843 |
expandMacrosWithArguments:argArray |
5964 | 1844 |
"ST80 compatibility - expand '<..>' macros with |
1845 |
argument strings. Similar to #bindWith:. |
|
1846 |
WARNING: possibly not all ST80 expansions are supported here." |
|
1847 |
||
1848 |
"/ supported expansions: |
|
1849 |
"/ |
|
1850 |
"/ <#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
|
1851 |
"/ <#s> # is arg Number; slice in the arg itself (must know asString) |
5964 | 1852 |
"/ <#?s1:s2> # is arg Number; slice in s1 if the arg is true, s2 otherwise |
7643 | 1853 |
"/ use first arg if # is not given (i.e. no number before s,p ...) |
5964 | 1854 |
"/ <n> replace by a newLine character |
1855 |
"/ <t> replace by a tab character |
|
6834
eba4b58b8692
fixed expandMacros (%-escape)
Claus Gittinger <cg@exept.de>
parents:
6810
diff
changeset
|
1856 |
"/ %X the X character itself |
5964 | 1857 |
|
8999
f3fda66ea51e
#expandMacrosWithArguments: - be mor friendly to xml strings.
Stefan Vogel <sv@exept.de>
parents:
8971
diff
changeset
|
1858 |
|in out c fmt nr arg s1 s2 peekc| |
5964 | 1859 |
|
1860 |
in := self readStream. |
|
10721
58e6950264cb
care for bitsPerCharacter when expanding macros
Claus Gittinger <cg@exept.de>
parents:
10703
diff
changeset
|
1861 |
out := CharacterWriteStream on:(self species uninitializedNew:self size). |
5964 | 1862 |
|
1863 |
[in atEnd] whileFalse:[ |
|
20874 | 1864 |
c := in next. |
1865 |
c == $% ifTrue:[ |
|
1866 |
c := in next. |
|
1867 |
out nextPut:c |
|
1868 |
] ifFalse:[c ~~ $< ifTrue:[ |
|
1869 |
out nextPut:c. |
|
1870 |
] ifFalse:[ |
|
1871 |
peekc := in peek. |
|
1872 |
[peekc == $<] whileTrue:[ |
|
1873 |
out nextPut:$<. |
|
1874 |
peekc := in nextPeek. |
|
1875 |
]. |
|
1876 |
peekc == $n ifTrue:[ |
|
1877 |
peekc := in nextPeek. |
|
1878 |
peekc == $> ifTrue:[ |
|
1879 |
in next. |
|
1880 |
out cr. |
|
1881 |
] ifFalse:[ |
|
1882 |
out nextPutAll:'<n'. |
|
1883 |
] |
|
1884 |
] ifFalse:[peekc == $t ifTrue:[ |
|
1885 |
peekc := in nextPeek. |
|
1886 |
peekc == $> ifTrue:[ |
|
1887 |
in next. |
|
1888 |
out tab. |
|
1889 |
] ifFalse:[ |
|
1890 |
out nextPutAll:'<t'. |
|
1891 |
] |
|
1892 |
] ifFalse:[ |
|
1893 |
peekc isDigit ifTrue:[ |
|
1894 |
"start an argument expansion ..." |
|
1895 |
nr := Integer readFrom:in onError:nil. |
|
1896 |
nr isNil ifTrue:[ |
|
1897 |
"this cannot happen (there is at least one digit)" |
|
1898 |
self error:'invalid format' mayProceed:true. |
|
1899 |
^ self |
|
1900 |
]. |
|
1901 |
fmt := in next. |
|
1902 |
(fmt ~~ $? and:[in peek ~~ $>]) ifTrue:[ |
|
1903 |
out nextPut:$<. |
|
1904 |
nr printOn:out. |
|
1905 |
out nextPut:fmt. |
|
1906 |
] ifFalse:[ |
|
1907 |
(nr between:1 and:argArray size) ifTrue:[ |
|
1908 |
arg := argArray at:nr. |
|
1909 |
] ifFalse:[ |
|
1910 |
arg := '' |
|
1911 |
]. |
|
1912 |
||
1913 |
fmt == $p ifTrue:[ |
|
1914 |
"expand with args printString" |
|
1915 |
arg printOn:out. |
|
1916 |
] ifFalse:[fmt == $s ifTrue:[ |
|
1917 |
"expand with arg itself" |
|
1918 |
arg isText ifTrue:[ |
|
1919 |
out contentsSpecies isText ifFalse:[ |
|
1920 |
out := (TextStream ? CharacterWriteStream on:Text new) nextPutAll:out contents; yourself. |
|
1921 |
]. |
|
1922 |
out nextPutAll:arg. |
|
1923 |
] ifFalse:[ |
|
1924 |
out nextPutAll:arg asString string. "see method comment: arg must know #asString" |
|
1925 |
] |
|
1926 |
] ifFalse:[fmt == $? ifTrue:[ |
|
1927 |
s1 := in upTo:$:. |
|
1928 |
s2 := in nextUpTo:$>. |
|
1929 |
arg == true ifTrue:[ |
|
1930 |
out nextPutAll:s1 |
|
1931 |
] ifFalse:[ |
|
1932 |
out nextPutAll:s2 |
|
1933 |
]. |
|
1934 |
] ifFalse:[ |
|
1935 |
"what does VW do here ?" |
|
1936 |
self error:'invalid format' mayProceed:true. |
|
1937 |
^ self |
|
1938 |
]]]. |
|
1939 |
c := in next. |
|
1940 |
c ~~ $> ifTrue:[ |
|
1941 |
"what does VW do here ?" |
|
1942 |
self error:'invalid format' mayProceed:true. |
|
1943 |
^ self |
|
1944 |
] |
|
1945 |
||
1946 |
]. |
|
1947 |
] ifFalse:[ |
|
1948 |
out nextPut:$<. |
|
1949 |
]. |
|
1950 |
]]. |
|
1951 |
]]. |
|
5964 | 1952 |
]. |
1953 |
^ out contents |
|
1954 |
||
1955 |
" |
|
8874 | 1956 |
'hello <1s> how are you' expandMacrosWith:(OperatingSystem getLoginName) |
1957 |
'one plus one is <1p>' expandMacrosWith:2 |
|
5964 | 1958 |
" |
1959 |
||
10721
58e6950264cb
care for bitsPerCharacter when expanding macros
Claus Gittinger <cg@exept.de>
parents:
10703
diff
changeset
|
1960 |
"Modified: / 18-09-2007 / 22:50:43 / cg" |
9192 | 1961 |
! |
1962 |
||
1963 |
isCharacters |
|
1964 |
"added for visual works compatibility" |
|
1965 |
^ true |
|
1966 |
! ! |
|
1967 |
||
16295 | 1968 |
!CharacterArray methodsFor:'JavaScript support'! |
1969 |
||
1970 |
unquote |
|
17000 | 1971 |
"removes double quotes from the receiver. |
19698 | 1972 |
This is the JavaScript standard unquote function." |
1973 |
||
1974 |
^ self unquote:$" |
|
16295 | 1975 |
|
1976 |
" |
|
17000 | 1977 |
'hello' quote unquote |
1978 |
||
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
1979 |
JavaScriptParser evaluate:'''hello''.quote.unquote' |
16295 | 1980 |
" |
19698 | 1981 |
! |
1982 |
||
1983 |
unquote:quoteCharacter |
|
1984 |
"removes quoteCharacter from either end of the receiver." |
|
1985 |
||
1986 |
|mySize| |
|
1987 |
||
1988 |
(mySize := self size) >= 2 ifTrue:[ |
|
20874 | 1989 |
((self first == quoteCharacter) and:[self last == quoteCharacter]) ifTrue:[ |
1990 |
^ self copyFrom:2 to:mySize-1 |
|
1991 |
]. |
|
19698 | 1992 |
]. |
1993 |
^ self |
|
1994 |
||
1995 |
" |
|
1996 |
'*hello*' unquote:$* |
|
1997 |
" |
|
16295 | 1998 |
! ! |
13510 | 1999 |
|
16175 | 2000 |
|
9192 | 2001 |
!CharacterArray methodsFor:'character searching'! |
2002 |
||
2003 |
includesMatchCharacters |
|
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
2004 |
"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
|
2005 |
for match operations; false if not. |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2006 |
Here, do not care for $\ escapes" |
608 | 2007 |
|
2008 |
^ self includesAny:'*#[' |
|
2519
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2009 |
|
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2010 |
" |
8874 | 2011 |
'*foo' includesMatchCharacters |
2012 |
'\*foo' includesMatchCharacters |
|
2013 |
'\*foo' includesUnescapedMatchCharacters |
|
2014 |
'*foo' includesMatchCharacters |
|
2519
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2015 |
'\\*foo' includesMatchCharacters |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2016 |
'foo*' includesMatchCharacters |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2017 |
'foo\*' includesMatchCharacters |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2018 |
'foo\' includesMatchCharacters |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2019 |
" |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2020 |
|
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2021 |
"Modified: 2.4.1997 / 18:12:34 / cg" |
9192 | 2022 |
! |
2023 |
||
2024 |
includesSeparator |
|
2025 |
"return true, if the receiver contains any whitespace characters" |
|
2026 |
||
2027 |
^ (self indexOfSeparator ~~ 0) |
|
2028 |
||
2029 |
" |
|
2030 |
'hello world' includesSeparator |
|
2031 |
'helloworld' includesSeparator |
|
2032 |
" |
|
2033 |
! |
|
2034 |
||
2035 |
includesUnescapedMatchCharacters |
|
8874 | 2036 |
"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
|
2037 |
for match operations; false if not. |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2038 |
Here, care for $\ escapes" |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2039 |
|
5342
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
2040 |
|idx sz specialChars escape| |
2519
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2041 |
|
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2042 |
idx := 1. |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2043 |
sz := self size. |
5342
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
2044 |
specialChars := '*#[\'. |
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
2045 |
(escape := self class matchEscapeCharacter) ~~ $\ ifTrue:[ |
20874 | 2046 |
specialChars := specialChars copy. |
2047 |
specialChars at:specialChars size put:escape |
|
5342
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
2048 |
]. |
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
2049 |
|
16245 | 2050 |
[ |
20874 | 2051 |
idx := self indexOfAny:specialChars startingAt:idx. |
2052 |
idx == 0 ifTrue:[^ false]. |
|
2053 |
(self at:idx) == escape ifFalse:[^ true]. |
|
2054 |
idx := idx + 2. |
|
2055 |
idx > sz ifTrue:[^ false]. |
|
16245 | 2056 |
] loop. |
8874 | 2057 |
|
2058 |
" |
|
2059 |
'*foo' includesUnescapedMatchCharacters |
|
2060 |
'\*foo' includesUnescapedMatchCharacters |
|
2061 |
'\\foo' includesUnescapedMatchCharacters |
|
2062 |
'\\\$foo' includesUnescapedMatchCharacters |
|
2519
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2063 |
'*foo' includesUnescapedMatchCharacters |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2064 |
'\\*foo' includesUnescapedMatchCharacters |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2065 |
'foo*' includesUnescapedMatchCharacters |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2066 |
'foo\*' includesUnescapedMatchCharacters |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2067 |
'foo\' includesUnescapedMatchCharacters |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2068 |
" |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2069 |
|
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2070 |
"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
|
2071 |
"Created: 2.4.1997 / 17:23:26 / cg" |
9192 | 2072 |
! |
2073 |
||
2074 |
indexOfControlCharacterStartingAt:startIndex |
|
3668
76271941f044
added #indexOfControlCharacterStartingAt:
Claus Gittinger <cg@exept.de>
parents:
3656
diff
changeset
|
2075 |
"return the index of the next control character; |
76271941f044
added #indexOfControlCharacterStartingAt:
Claus Gittinger <cg@exept.de>
parents:
3656
diff
changeset
|
2076 |
that is a character with asciiValue < 32. |
15605 | 2077 |
Start the search at startIndex, searching forward. |
3668
76271941f044
added #indexOfControlCharacterStartingAt:
Claus Gittinger <cg@exept.de>
parents:
3656
diff
changeset
|
2078 |
Return 0 if none is found." |
76271941f044
added #indexOfControlCharacterStartingAt:
Claus Gittinger <cg@exept.de>
parents:
3656
diff
changeset
|
2079 |
|
76271941f044
added #indexOfControlCharacterStartingAt:
Claus Gittinger <cg@exept.de>
parents:
3656
diff
changeset
|
2080 |
|start "{ Class: SmallInteger }" |
9192 | 2081 |
mySize "{ Class: SmallInteger }"| |
2082 |
||
2083 |
start := startIndex. |
|
2084 |
mySize := self size. |
|
2085 |
||
2086 |
start to:mySize do:[:index | |
|
20874 | 2087 |
(self at:index) isControlCharacter ifTrue:[^ index] |
9192 | 2088 |
]. |
2089 |
^ 0 |
|
2090 |
||
2091 |
" |
|
2092 |
'hello world' asTwoByteString indexOfControlCharacterStartingAt:1 |
|
2093 |
'hello world\foo' withCRsasTwoByteString indexOfControlCharacterStartingAt:1 |
|
2094 |
" |
|
2095 |
||
2096 |
"Modified: / 21.7.1998 / 17:25:07 / cg" |
|
2097 |
! |
|
3668
76271941f044
added #indexOfControlCharacterStartingAt:
Claus Gittinger <cg@exept.de>
parents:
3656
diff
changeset
|
2098 |
|
8541 | 2099 |
indexOfNonSeparator |
2100 |
"return the index of the first non-whitespace character. |
|
2101 |
return 0 if no non-separator was found" |
|
2102 |
||
2103 |
^ self indexOfNonSeparatorStartingAt:1. |
|
2104 |
||
2105 |
" |
|
8874 | 2106 |
' hello world' indexOfNonSeparator |
2107 |
' ' indexOfNonSeparator |
|
2108 |
'a ' indexOfNonSeparator |
|
2109 |
'abc' indexOfNonSeparator |
|
2110 |
' ' indexOfNonSeparator |
|
9192 | 2111 |
'' indexOfNonSeparator |
2112 |
" |
|
2113 |
! |
|
2114 |
||
2115 |
indexOfNonSeparatorStartingAt:startIndex |
|
4117 | 2116 |
"return the index of the next non-whitespace character, |
2117 |
starting the search at startIndex, searching forward; |
|
2118 |
return 0 if no non-separator was found" |
|
608 | 2119 |
|
2120 |
|start "{ Class: SmallInteger }" |
|
2121 |
mySize "{ Class: SmallInteger }"| |
|
2122 |
||
2123 |
start := startIndex. |
|
2124 |
mySize := self size. |
|
2125 |
||
2126 |
start to:mySize do:[:index | |
|
20874 | 2127 |
(self at:index) isSeparator ifFalse:[^ index] |
608 | 2128 |
]. |
2129 |
^ 0 |
|
2130 |
||
2131 |
" |
|
8874 | 2132 |
' hello world' indexOfNonSeparatorStartingAt:1 |
2133 |
' ' indexOfNonSeparatorStartingAt:1 |
|
2134 |
'a ' indexOfNonSeparatorStartingAt:2 |
|
4117 | 2135 |
" |
2136 |
||
608 | 2137 |
" |
2138 |
|s index1 index2| |
|
2139 |
s := ' foo bar baz'. |
|
2140 |
index1 := s indexOfNonSeparatorStartingAt:1. |
|
2141 |
index2 := s indexOfSeparatorStartingAt:index1. |
|
2142 |
s copyFrom:index1 to:index2 - 1 |
|
2143 |
" |
|
9192 | 2144 |
! |
2145 |
||
2146 |
indexOfSeparator |
|
2147 |
"return the index of the first whitespace character; |
|
2148 |
starting the search at the beginning, searching forward; |
|
2149 |
return 0 if no separator was found" |
|
2150 |
||
2151 |
^ self indexOfSeparatorStartingAt:1 |
|
2152 |
||
2153 |
" |
|
2154 |
'hello world' indexOfSeparator |
|
2155 |
'helloworld' indexOfSeparator |
|
2156 |
'hello ' indexOfSeparator |
|
2157 |
' hello' indexOfSeparator |
|
2158 |
" |
|
2159 |
! |
|
2160 |
||
15912 | 2161 |
indexOfSeparatorOrEndStartingAt:startIndex |
2162 |
"return the index of the next whitespace character, |
|
2163 |
starting the search at startIndex, searching forward; |
|
19737 | 2164 |
return the index of one beyond the end of the receiver if no separator was found. |
15912 | 2165 |
To extract the word, copy from startIndex to the returned index-1" |
2166 |
||
2167 |
|idx| |
|
2168 |
||
2169 |
idx := self indexOfSeparatorStartingAt:startIndex. |
|
2170 |
idx == 0 ifTrue:[^ self size + 1]. |
|
2171 |
^ idx. |
|
2172 |
||
2173 |
" |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2174 |
'hello world' indexOfSeparatorOrEndStartingAt:3 |
15912 | 2175 |
' hello world' indexOfSeparatorOrEndStartingAt:3 |
2176 |
'hello world ' indexOfSeparatorOrEndStartingAt:3 |
|
2177 |
'hello world ' indexOfSeparatorOrEndStartingAt:6 |
|
2178 |
'hello world ' indexOfSeparatorOrEndStartingAt:7 |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2179 |
'helloworld ' indexOfSeparatorOrEndStartingAt:7 |
15912 | 2180 |
'helloworld' indexOfSeparatorOrEndStartingAt:7 |
19737 | 2181 |
'helloworld' indexOfSeparatorStartingAt:7 |
15912 | 2182 |
" |
2183 |
! |
|
2184 |
||
9192 | 2185 |
indexOfSeparatorStartingAt:startIndex |
2186 |
"return the index of the next whitespace character, |
|
2187 |
starting the search at startIndex, searching forward; |
|
2188 |
return 0 if no separator was found" |
|
2189 |
||
2190 |
|start "{ Class: SmallInteger }" |
|
2191 |
mySize "{ Class: SmallInteger }"| |
|
2192 |
||
2193 |
start := startIndex. |
|
2194 |
mySize := self size. |
|
2195 |
||
2196 |
start to:mySize do:[:index | |
|
20874 | 2197 |
(self at:index) isSeparator ifTrue:[^ index] |
9192 | 2198 |
]. |
2199 |
^ 0 |
|
2200 |
||
2201 |
" |
|
2202 |
'hello world' indexOfSeparatorStartingAt:3 |
|
2203 |
' hello world' indexOfSeparatorStartingAt:3 |
|
8874 | 2204 |
'hello world ' indexOfSeparatorStartingAt:3 |
2205 |
'hello world ' indexOfSeparatorStartingAt:6 |
|
2206 |
'hello world ' indexOfSeparatorStartingAt:7 |
|
2207 |
'helloworld ' indexOfSeparatorStartingAt:7 |
|
2208 |
'helloworld' indexOfSeparatorStartingAt:7 |
|
4117 | 2209 |
" |
2519
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2210 |
! |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2211 |
|
4117 | 2212 |
lastIndexOfSeparator |
14150
e13160aa321a
comment/format in: #lastIndexOfSeparator
Claus Gittinger <cg@exept.de>
parents:
14141
diff
changeset
|
2213 |
"return the last index of a whitespace character (space or tab). |
4117 | 2214 |
(i.e. start the search at the end and search backwards); |
2215 |
Returns 0 if no separator is found." |
|
2216 |
||
2217 |
^ self lastIndexOfSeparatorStartingAt:(self size) |
|
2218 |
||
2219 |
" |
|
8874 | 2220 |
'hello world' lastIndexOfSeparator |
2221 |
'helloworld' lastIndexOfSeparator |
|
2222 |
'hel lo wor ld' lastIndexOfSeparator |
|
2223 |
'hel ' lastIndexOfSeparator 6 |
|
4117 | 2224 |
" |
14150
e13160aa321a
comment/format in: #lastIndexOfSeparator
Claus Gittinger <cg@exept.de>
parents:
14141
diff
changeset
|
2225 |
|
e13160aa321a
comment/format in: #lastIndexOfSeparator
Claus Gittinger <cg@exept.de>
parents:
14141
diff
changeset
|
2226 |
"Modified (comment): / 01-06-2012 / 13:10:30 / cg" |
4117 | 2227 |
! |
2228 |
||
2229 |
lastIndexOfSeparatorStartingAt:startIndex |
|
9192 | 2230 |
"return the index of the previous whitespace character, |
2231 |
starting the search at startIndex (and searching backwards); |
|
2232 |
returns 0 if no separator was found" |
|
2233 |
||
2234 |
|start "{ Class: SmallInteger }"| |
|
2235 |
||
2236 |
start := startIndex. |
|
2237 |
||
2238 |
start to:1 by:-1 do:[:index | |
|
20874 | 2239 |
(self at:index) isSeparator ifTrue:[^ index] |
9192 | 2240 |
]. |
2241 |
^ 0 |
|
2242 |
||
2243 |
" |
|
2244 |
'hello world' lastIndexOfSeparatorStartingAt:3 |
|
2245 |
'hello world' lastIndexOfSeparatorStartingAt:7 |
|
2246 |
'helloworld' lastIndexOfSeparatorStartingAt:7 |
|
8874 | 2247 |
' helloworld' lastIndexOfSeparatorStartingAt:7 |
4117 | 2248 |
" |
608 | 2249 |
! ! |
2250 |
||
2251 |
!CharacterArray methodsFor:'comparing'! |
|
2252 |
||
6742
292775fda600
avoid endless recursion when comparing int < string
Claus Gittinger <cg@exept.de>
parents:
6655
diff
changeset
|
2253 |
< aString |
608 | 2254 |
"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
|
2255 |
receiver is greater than the argument. Otherwise return false. |
8874 | 2256 |
This comparison is based on the elements ascii code - |
9192 | 2257 |
i.e. upper/lowercase & upper/lowercase & national characters are NOT treated specially." |
2258 |
||
2259 |
|mySize "{ Class: SmallInteger }" |
|
2260 |
otherSize "{ Class: SmallInteger }" |
|
2261 |
n "{ Class: SmallInteger }" |
|
2262 |
c1 c2| |
|
2263 |
||
2264 |
mySize := self size. |
|
2265 |
otherSize := aString string size. |
|
6742
292775fda600
avoid endless recursion when comparing int < string
Claus Gittinger <cg@exept.de>
parents:
6655
diff
changeset
|
2266 |
n := mySize min:otherSize. |
292775fda600
avoid endless recursion when comparing int < string
Claus Gittinger <cg@exept.de>
parents:
6655
diff
changeset
|
2267 |
|
292775fda600
avoid endless recursion when comparing int < string
Claus Gittinger <cg@exept.de>
parents:
6655
diff
changeset
|
2268 |
1 to:n do:[:index | |
20874 | 2269 |
c1 := self at:index. |
2270 |
c2 := aString at:index. |
|
2271 |
(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
|
2272 |
]. |
292775fda600
avoid endless recursion when comparing int < string
Claus Gittinger <cg@exept.de>
parents:
6655
diff
changeset
|
2273 |
^ mySize < otherSize |
608 | 2274 |
! |
2275 |
||
2276 |
= aString |
|
2277 |
"Compare the receiver with the argument and return true if the |
|
2278 |
receiver is equal to the argument. Otherwise return false. |
|
1252 | 2279 |
|
9191 | 2280 |
This compare does NOT ignore case differences, |
608 | 2281 |
therefore 'foo' = 'Foo' will return false. |
2282 |
Since this is incompatible to ST-80 (at least, V2.x) , this may change." |
|
2283 |
||
14137
1eb1b4a3f364
Inherit from UninterpretedBytes instead of ByteArray
Stefan Vogel <sv@exept.de>
parents:
14122
diff
changeset
|
2284 |
|mySize "{ Class: SmallInteger }"| |
608 | 2285 |
|
5963
23462e06dcff
compare with Texts fixed (should compare equal if strings are)
Claus Gittinger <cg@exept.de>
parents:
5946
diff
changeset
|
2286 |
(aString isString or:[aString species == self species]) ifFalse:[ |
20874 | 2287 |
^ false |
5963
23462e06dcff
compare with Texts fixed (should compare equal if strings are)
Claus Gittinger <cg@exept.de>
parents:
5946
diff
changeset
|
2288 |
]. |
608 | 2289 |
mySize := self size. |
14137
1eb1b4a3f364
Inherit from UninterpretedBytes instead of ByteArray
Stefan Vogel <sv@exept.de>
parents:
14122
diff
changeset
|
2290 |
mySize ~~ (aString size) ifTrue:[^ false]. |
608 | 2291 |
|
2292 |
1 to:mySize do:[:index | |
|
20874 | 2293 |
(self at:index) = (aString at:index) ifFalse:[^ false]. |
608 | 2294 |
]. |
2295 |
^ true |
|
2296 |
||
2297 |
" |
|
8874 | 2298 |
'foo' = 'Foo' |
2299 |
'foo' = 'bar' |
|
2300 |
'foo' = 'foo' |
|
2301 |
'foo' = 'foo' asText |
|
2302 |
'foo' asText = 'foo' |
|
2303 |
'foo' asText = 'foo' asText |
|
608 | 2304 |
" |
1252 | 2305 |
|
2306 |
"Modified: 22.4.1996 / 15:53:58 / cg" |
|
608 | 2307 |
! |
9192 | 2308 |
|
9191 | 2309 |
> aString |
608 | 2310 |
"Compare the receiver with the argument and return true if the |
2311 |
receiver is greater than the argument. Otherwise return false. |
|
8874 | 2312 |
This comparison is based on the elements ascii code - |
1252 | 2313 |
i.e. upper/lowercase & upper/lowercase & national characters are NOT treated specially." |
608 | 2314 |
|
2315 |
|mySize "{ Class: SmallInteger }" |
|
8874 | 2316 |
otherSize "{ Class: SmallInteger }" |
9192 | 2317 |
n "{ Class: SmallInteger }" |
2318 |
c1 c2| |
|
2319 |
||
2320 |
mySize := self size. |
|
5929 | 2321 |
otherSize := aString string size. |
2322 |
n := mySize min:otherSize. |
|
2323 |
||
2324 |
1 to:n do:[:index | |
|
20874 | 2325 |
c1 := self at:index. |
2326 |
c2 := aString at:index. |
|
2327 |
(c1 == c2 or:[c1 = c2]) ifFalse:[^ c1 > c2]. |
|
5929 | 2328 |
]. |
9192 | 2329 |
^ mySize > otherSize |
2330 |
||
2331 |
"Modified: 22.4.1996 / 15:55:00 / cg" |
|
2332 |
! |
|
2333 |
||
14660 | 2334 |
after:aString |
2335 |
"Compare the receiver with the argument and return true if the |
|
2336 |
receiver should come after the argument in a sorted list. |
|
2337 |
Otherwise return false. |
|
2338 |
NOTE: The comparison should be language specific, depending on the value of |
|
20874 | 2339 |
LC_COLLATE, which is initialized from the environment. |
2340 |
||
2341 |
Currently it is for Strings, but not for UnicodeStrings... |
|
14660 | 2342 |
|
2343 |
STUPID: |
|
20874 | 2344 |
#after has a completely different meaning in SeqColl ..." |
14660 | 2345 |
|
2346 |
^ (self compareCollatingWith:aString) > 0 |
|
2347 |
! |
|
2348 |
||
15814 | 2349 |
compareAsVersionNumberWith:aStringOrCollection |
2350 |
"Compare the receiver with the argument and return 1 if the receiver is |
|
2351 |
greater, 0 if equal and -1 if less than the argument in a sorted list. |
|
2352 |
Compare as version numbers in the form a.b.c... ." |
|
2353 |
||
20374 | 2354 |
|rev1 rev2 removeTrailingZerosBlock trailingZerosCount| |
15814 | 2355 |
|
2356 |
rev1 := self asCollectionOfSubstringsSeparatedBy:$.. |
|
2357 |
aStringOrCollection isString ifTrue:[ |
|
20374 | 2358 |
rev2 := aStringOrCollection asCollectionOfSubstringsSeparatedBy:$.. |
15814 | 2359 |
]. |
2360 |
rev1 := rev1 collect:[:each| each asInteger]. |
|
2361 |
rev2 := rev2 collect:[:each| each asInteger]. |
|
2362 |
||
20374 | 2363 |
removeTrailingZerosBlock := |
2364 |
[:numbers | |
|
2365 |
trailingZerosCount := 0. |
|
2366 |
numbers reversed doWithExit:[:each :exit | |
|
2367 |
each == 0 ifTrue:[ |
|
2368 |
trailingZerosCount := trailingZerosCount + 1. |
|
2369 |
] ifFalse:[ |
|
2370 |
exit value:nil. |
|
2371 |
]. |
|
2372 |
]. |
|
2373 |
||
2374 |
trailingZerosCount > 0 ifTrue:[ |
|
2375 |
numbers copyTo:numbers size - trailingZerosCount |
|
2376 |
] ifFalse:[ |
|
2377 |
numbers |
|
2378 |
] |
|
2379 |
]. |
|
2380 |
||
2381 |
rev1 := removeTrailingZerosBlock value:rev1. |
|
2382 |
rev2 := removeTrailingZerosBlock value:rev2. |
|
2383 |
||
15814 | 2384 |
^ rev1 compareWith:rev2 |
2385 |
||
2386 |
" |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2387 |
self assert:('1' compareAsVersionNumberWith:'2') < 0. |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2388 |
self assert:('2' compareAsVersionNumberWith:'1') > 0. |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2389 |
self assert:('1.1' compareAsVersionNumberWith:'2.1.2') < 0. |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2390 |
self assert:('2.1' compareAsVersionNumberWith:'1.2.3') > 0. |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2391 |
self assert:('1' compareAsVersionNumberWith:'1.1') < 0. |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2392 |
self assert:('1.1' compareAsVersionNumberWith:'1') > 0. |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2393 |
self assert:('1.1' compareAsVersionNumberWith:'1.2') < 0. |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2394 |
self assert:('1.10' compareAsVersionNumberWith:'1.2') > 0. |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2395 |
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
|
2396 |
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
|
2397 |
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
|
2398 |
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
|
2399 |
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
|
2400 |
self assert:('1.2.3.4' compareAsVersionNumberWith:#(1 2 3 4)) = 0. |
15814 | 2401 |
" |
2402 |
! |
|
2403 |
||
9192 | 2404 |
compareCaselessWith:aString |
9190 | 2405 |
"Compare the receiver against the argument, ignoreing case. |
2406 |
Return 1 if the receiver is greater, 0 if equal and -1 if less than the argument. |
|
2407 |
||
8874 | 2408 |
This comparison is based on the elements ascii code - |
9190 | 2409 |
i.e. national characters are NOT treated specially. |
2410 |
'foo' compareWith: 'Foo' will return 0" |
|
608 | 2411 |
|
2412 |
|mySize "{ Class: SmallInteger }" |
|
8874 | 2413 |
otherSize "{ Class: SmallInteger }" |
9192 | 2414 |
n "{ Class: SmallInteger }" |
608 | 2415 |
c1 c2| |
2416 |
||
2417 |
mySize := self size. |
|
5739
4fd3be7ccc75
oops some relational ops did accept a non-string arg
Claus Gittinger <cg@exept.de>
parents:
5732
diff
changeset
|
2418 |
otherSize := aString string size. |
608 | 2419 |
n := mySize min:otherSize. |
2420 |
||
2421 |
1 to:n do:[:index | |
|
20874 | 2422 |
c1 := (self at:index) asLowercase. |
2423 |
c2 := (aString at:index) asLowercase. |
|
2424 |
c1 > c2 ifTrue:[^ 1]. |
|
2425 |
c1 < c2 ifTrue:[^ -1]. |
|
608 | 2426 |
]. |
2427 |
mySize > otherSize ifTrue:[^ 1]. |
|
2428 |
mySize < otherSize ifTrue:[^ -1]. |
|
2429 |
^ 0 |
|
1252 | 2430 |
|
2431 |
"Modified: 22.4.1996 / 15:56:07 / cg" |
|
608 | 2432 |
! |
2433 |
||
14660 | 2434 |
compareCollatingWith:aString |
2435 |
"Compare the receiver with the argument and return 1 if the receiver is |
|
2436 |
greater, 0 if equal and -1 if less than the argument in a sorted list. |
|
2437 |
The comparison is language specific, depending on the value of |
|
2438 |
LC_COLLATE, which is in the shell environment." |
|
2439 |
||
2440 |
"TODO not yet defined for unicode" |
|
2441 |
<resource: #todo> |
|
2442 |
||
2443 |
|s| |
|
2444 |
||
2445 |
(s := self string) ~~ self ifTrue:[ |
|
20874 | 2446 |
^ s compareCollatingWith:aString |
14660 | 2447 |
]. |
2448 |
^ self compareWith:aString |
|
2449 |
! |
|
2450 |
||
9192 | 2451 |
compareWith:aString |
9190 | 2452 |
"Compare the receiver with the argument and return 1 if the receiver is |
2453 |
greater, 0 if equal and -1 if less than the argument. |
|
11861 | 2454 |
This comparison is based on the elements' codepoints - |
9190 | 2455 |
i.e. upper/lowercase & national characters are NOT treated specially. |
2456 |
'foo' compareWith: 'Foo' will return 1. |
|
2457 |
while 'foo' sameAs:'Foo' will return true" |
|
2458 |
||
15814 | 2459 |
|s| |
2460 |
||
2461 |
s := self string. |
|
2462 |
s ~~ self ifTrue:[ |
|
20874 | 2463 |
^ s compareWith:aString string. |
9192 | 2464 |
]. |
15814 | 2465 |
^ super compareWith:aString string. |
9192 | 2466 |
! |
2467 |
||
11861 | 2468 |
endsWith:aStringOrCharacter |
16175 | 2469 |
"return true, if the receiver ends with something, aStringOrCharacter. |
2470 |
If aStringOrCharacter is empty, true is returned" |
|
11861 | 2471 |
|
2472 |
|s| |
|
2473 |
||
2474 |
(s := self string) ~~ self ifTrue:[ |
|
20874 | 2475 |
^ s endsWith:aStringOrCharacter |
11861 | 2476 |
]. |
21083 | 2477 |
(self size ~~ 0 and:[aStringOrCharacter isCharacter]) ifTrue:[ |
20874 | 2478 |
^ self last = aStringOrCharacter |
11861 | 2479 |
]. |
2480 |
^ super endsWith:aStringOrCharacter |
|
2481 |
||
2482 |
" |
|
2483 |
'hello world' endsWith:'world' |
|
2484 |
'hello world' asText allBold endsWith:'world' |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2485 |
'hello world' endsWith:'' |
16175 | 2486 |
'hello world' asText allBold endsWith:'' |
11861 | 2487 |
" |
2488 |
||
2489 |
"Modified: 12.5.1996 / 15:49:18 / cg" |
|
2490 |
! |
|
2491 |
||
11838 | 2492 |
hammingDistanceTo:aString |
2493 |
"return the hamming distance (the number of characters which are different). |
|
12177 | 2494 |
In information theory, the Hamming distance between two strings of equal length |
2495 |
is the number of positions for which the corresponding symbols are different. |
|
2496 |
Put another way, it measures the minimum number of substitutions required to change |
|
11838 | 2497 |
one into the other, or the number of errors that transformed one string into the other." |
2498 |
||
2499 |
self assert:(aString size == self size). |
|
2500 |
^ 1 to:self size count:[:idx | (self at:idx) ~= (aString at:idx)] |
|
2501 |
||
2502 |
" |
|
2503 |
'roses' hammingDistanceTo:'toned' |
|
2504 |
" |
|
2505 |
! |
|
2506 |
||
9192 | 2507 |
hash |
2508 |
"return an integer useful as a hash-key" |
|
2509 |
||
16898 | 2510 |
"/ whenever changing, also care for String>>hash. |
13879 | 2511 |
"/ immediately after any change, execute (maybe in a debugger): |
2512 |
"/ Set allSubInstancesDo:[:s | s rehash] |
|
16898 | 2513 |
^ self hash_fnv1a |
2514 |
||
2515 |
" |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2516 |
'a' hash |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2517 |
'a' asUnicode16String hash |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2518 |
'a' asUnicode32String hash |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2519 |
'aa' hash |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2520 |
'aa' asUnicode16String hash |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2521 |
'aa' asUnicode32String hash |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2522 |
'ab' hash |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2523 |
'ab' asUnicode16String hash |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2524 |
'ab' asUnicode32String hash |
13879 | 2525 |
'ab' hash |
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2526 |
'ab' asArray hash |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2527 |
" |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2528 |
|
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2529 |
" |
20874 | 2530 |
|syms ms| |
2531 |
||
2532 |
syms := Symbol allInstances. |
|
2533 |
Transcript show:'syms: '; showCR:syms size. |
|
2534 |
Transcript show:'sdbm hashes: '; showCR:(syms collect:[:s| s hash]) asSet size. |
|
2535 |
Transcript show:'dragonBook hashes: '; showCR:(syms collect:[:s| s hash_dragonBook]) asSet size. |
|
2536 |
||
2537 |
ms := Time millisecondsToRun:[ |
|
2538 |
10 timesRepeat:[ |
|
2539 |
syms do:[:each| each hash]. |
|
2540 |
]. |
|
2541 |
]. |
|
2542 |
Transcript show:'sdbm hash: '; showCR:ms. |
|
2543 |
||
2544 |
ms := Time millisecondsToRun:[ |
|
2545 |
10 timesRepeat:[ |
|
2546 |
syms do:[:each| each hash_dragonBook]. |
|
2547 |
]. |
|
2548 |
]. |
|
2549 |
Transcript show:'dragonBook: '; showCR:ms. |
|
2550 |
||
2551 |
syms := syms collect:[:each| each asUnicode16String]. |
|
2552 |
ms := Time millisecondsToRun:[ |
|
2553 |
10 timesRepeat:[ |
|
2554 |
syms do:[:each| each hash]. |
|
2555 |
]. |
|
2556 |
]. |
|
2557 |
Transcript show:'unicode sdbm hash: '; showCR:ms. |
|
2558 |
||
2559 |
ms := Time millisecondsToRun:[ |
|
2560 |
10 timesRepeat:[ |
|
2561 |
syms do:[:each| each hash_dragonBook]. |
|
2562 |
]. |
|
2563 |
]. |
|
2564 |
Transcript show:'unicode dragonBook:'; showCR:ms. |
|
14641 | 2565 |
" |
2566 |
||
13879 | 2567 |
"Modified: / 26-12-2011 / 14:09:07 / cg" |
2568 |
! |
|
2569 |
||
2570 |
hash_dragonBook |
|
2571 |
"return an integer useful as a hash-key" |
|
2572 |
||
10698
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2573 |
|h g| |
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 |
"/ this is the dragon-book algorithm |
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2577 |
"/ |
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2578 |
h := 0. |
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2579 |
self reverseDo:[:char | |
10703
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
2580 |
"/ 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
|
2581 |
"/ h := (h bitShift:4) + char asciiValue. |
20874 | 2582 |
h := (h bitShift:4). |
2583 |
h := h + char codePoint. |
|
2584 |
h := h bitAnd:16rFFFFFFFF. |
|
2585 |
g := (h bitAnd: 16rF0000000). |
|
2586 |
g ~~ 0 ifTrue:[ |
|
2587 |
h := h bitXor:(g bitShift:-24). |
|
2588 |
h := h bitXor:g. |
|
2589 |
]. |
|
10698
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2590 |
]. |
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2591 |
"/ |
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2592 |
"/ multiply by large prime to spread values |
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2593 |
"/ This speeds up Set and Dictionary by a factor of 10!! |
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2594 |
"/ |
14137
1eb1b4a3f364
Inherit from UninterpretedBytes instead of ByteArray
Stefan Vogel <sv@exept.de>
parents:
14122
diff
changeset
|
2595 |
h := h times:31415821. |
10698
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2596 |
^ h |
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2597 |
|
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2598 |
" |
10703
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
2599 |
'a' hash |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
2600 |
'a' asUnicode16String hash |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
2601 |
'aa' hash |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
2602 |
'aa' asUnicode16String hash |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
2603 |
'ab' hash |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
2604 |
'ab' asUnicode16String hash |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
2605 |
'ab' hash |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
2606 |
'ab' asArray hash |
10698
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2607 |
" |
13879 | 2608 |
|
2609 |
"Created: / 26-12-2011 / 13:46:06 / cg" |
|
2610 |
! |
|
2611 |
||
16887 | 2612 |
hash_fnv1a |
2613 |
"return an integer useful as a hash-key. |
|
2614 |
This method uses the fnv-1a algorithm |
|
19743 | 2615 |
(which is actually a very good one). |
2616 |
Attention: stops when a 0-codepoint is encountered |
|
20874 | 2617 |
(for compatibility with the hash used by the VM) |
19743 | 2618 |
Also: on 64bit CPUs, only small 4-byte hashvalues are returned, |
20874 | 2619 |
(so hash values are independent from the architecture)" |
19743 | 2620 |
|
2621 |
|h byte| |
|
16887 | 2622 |
|
2623 |
h := 2166136261. |
|
2624 |
self do:[:eachChar | |
|
20874 | 2625 |
byte := eachChar codePoint. |
2626 |
byte == 0 ifTrue:[ |
|
2627 |
"/ stop |
|
2628 |
^ (h bitXor: (h >> 30)) bitAnd: 16r3FFFFFFF. |
|
2629 |
]. |
|
2630 |
h := h bitXor:byte. |
|
2631 |
h := (h * 16777619) bitAnd:16rFFFFFFFF. |
|
16887 | 2632 |
]. |
16898 | 2633 |
"/ make sure, it fits into a smallInt |
2634 |
h := (h bitXor: (h >> 30)) bitAnd: 16r3FFFFFFF. |
|
16887 | 2635 |
^ h |
2636 |
||
2637 |
" |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2638 |
'abc' hash_fnv1a |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2639 |
'abc' asUnicode16String hash_fnv1a |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2640 |
'abc' asUnicode32String hash_fnv1a |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2641 |
|
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2642 |
'foofooHelloWorld' hash_fnv1a |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2643 |
'foofooHelloWorld' asUnicode16String hash_fnv1a |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2644 |
'foofooHelloWorld' asUnicode32String hash_fnv1a |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2645 |
|
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2646 |
'blablaHelloWorld' hash_fnv1a |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2647 |
'blablaHelloWorld' asUnicode16String hash_fnv1a |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2648 |
'blablaHelloWorld' asUnicode32String hash_fnv1a |
16887 | 2649 |
" |
2650 |
! |
|
2651 |
||
2652 |
hash_java |
|
2653 |
"return an integer useful as a hash-key. |
|
2654 |
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
|
2655 |
the java virtual machine |
16887 | 2656 |
(which is actually not a very good one)." |
2657 |
||
2658 |
|h| |
|
2659 |
||
2660 |
h := 0. |
|
2661 |
self do:[:eachChar | |
|
20874 | 2662 |
h := (h * 31) + (eachChar codePoint). |
2663 |
h := h bitAnd:16rFFFFFFFF. |
|
16887 | 2664 |
]. |
2665 |
^ h |
|
2666 |
||
2667 |
" |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2668 |
'abc' hash_java |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2669 |
'foofooHelloWorld' hash_java |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2670 |
'blablaHelloWorld' hash_java |
16887 | 2671 |
" |
2672 |
! |
|
2673 |
||
16898 | 2674 |
hash_sdbm |
2675 |
"return an integer useful as a hash-key. |
|
2676 |
This method implements the sdbm algorithm." |
|
2677 |
||
2678 |
|h| |
|
2679 |
||
2680 |
"/ |
|
2681 |
"/ this is the sdbm algorithm |
|
2682 |
"/ |
|
2683 |
h := 0. |
|
2684 |
self do:[:char | |
|
20874 | 2685 |
h := (65599 times:h) plus:char codePoint. |
16898 | 2686 |
]. |
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2687 |
^ h |
16898 | 2688 |
|
2689 |
" |
|
2690 |
'a' hash |
|
2691 |
'a' asUnicode16String hash |
|
2692 |
'aa' hash |
|
2693 |
'aa' asUnicode16String hash |
|
2694 |
'ab' hash |
|
2695 |
'ab' asUnicode16String hash |
|
2696 |
'ab' hash |
|
2697 |
'ab' asArray hash |
|
2698 |
" |
|
2699 |
||
2700 |
" |
|
20874 | 2701 |
|syms ms| |
2702 |
||
2703 |
syms := Symbol allInstances. |
|
2704 |
Transcript show:'syms: '; showCR:syms size. |
|
2705 |
Transcript show:'sdbm hashes: '; showCR:(syms collect:[:s| s hash]) asSet size. |
|
2706 |
Transcript show:'dragonBook hashes: '; showCR:(syms collect:[:s| s hash_dragonBook]) asSet size. |
|
2707 |
||
2708 |
ms := Time millisecondsToRun:[ |
|
2709 |
10 timesRepeat:[ |
|
2710 |
syms do:[:each| each hash]. |
|
2711 |
]. |
|
2712 |
]. |
|
2713 |
Transcript show:'sdbm hash: '; showCR:ms. |
|
2714 |
||
2715 |
ms := Time millisecondsToRun:[ |
|
2716 |
10 timesRepeat:[ |
|
2717 |
syms do:[:each| each hash_dragonBook]. |
|
2718 |
]. |
|
2719 |
]. |
|
2720 |
Transcript show:'dragonBook: '; showCR:ms. |
|
2721 |
||
2722 |
syms := syms collect:[:each| each asUnicode16String]. |
|
2723 |
ms := Time millisecondsToRun:[ |
|
2724 |
10 timesRepeat:[ |
|
2725 |
syms do:[:each| each hash]. |
|
2726 |
]. |
|
2727 |
]. |
|
2728 |
Transcript show:'unicode sdbm hash: '; showCR:ms. |
|
2729 |
||
2730 |
ms := Time millisecondsToRun:[ |
|
2731 |
10 timesRepeat:[ |
|
2732 |
syms do:[:each| each hash_dragonBook]. |
|
2733 |
]. |
|
2734 |
]. |
|
2735 |
Transcript show:'unicode dragonBook:'; showCR:ms. |
|
16898 | 2736 |
" |
2737 |
||
2738 |
"Modified: / 26-12-2011 / 14:09:07 / cg" |
|
2739 |
! |
|
2740 |
||
11861 | 2741 |
levenshteinTo:aString |
2742 |
"return the levenshtein distance to the argument, aString; |
|
2743 |
this value corresponds to the number of replacements that have to be |
|
2744 |
made to get aString from the receiver. |
|
2745 |
See IEEE transactions on Computers 1976 Pg 172 ff." |
|
2746 |
||
2747 |
" |
|
2748 |
in the following, we assume that ommiting a character |
|
2749 |
is less of an error than inserting an extra character. |
|
2750 |
Therefore the different insertion (i) and deletion (d) values. |
|
20874 | 2751 |
s: substitution weight (4) |
2752 |
k: keyboard weight (k) (typing a nearby key) - or nil (then use s) |
|
2753 |
c: case weight (4) - or nil (then use s) |
|
2754 |
e: exchange weight (8) - or nil (then use s*2) |
|
2755 |
i: insertion of extra character weight (2) |
|
2756 |
d: delete of a character weight (6) |
|
11861 | 2757 |
" |
2758 |
||
2759 |
^ StringUtilities |
|
20874 | 2760 |
levenshteinDistanceFrom:self |
2761 |
to:aString |
|
2762 |
s:4 k:4 c:4 e:nil i:2 d:6 |
|
12177 | 2763 |
|
2764 |
" |
|
2765 |
'computer' levenshteinTo:'computer' |
|
11861 | 2766 |
'cOmputer' levenshteinTo:'computer' |
12177 | 2767 |
'cOmpuTer' levenshteinTo:'computer' |
11861 | 2768 |
'cimputer' levenshteinTo:'computer' |
2769 |
'cumputer' levenshteinTo:'computer' |
|
2770 |
||
2771 |
'cmputer' levenshteinTo:'computer' |
|
2772 |
'coomputer' levenshteinTo:'computer' |
|
2773 |
||
2774 |
'ocmprt' levenshteinTo:'computer' |
|
2775 |
'computer' levenshteinTo:'computer' |
|
2776 |
'ocmputer' levenshteinTo:'computer' |
|
2777 |
'cmputer' levenshteinTo:'computer' |
|
2778 |
'computer' levenshteinTo:'cmputer' |
|
2779 |
'Computer' levenshteinTo:'computer' |
|
2780 |
||
12177 | 2781 |
'compiter' levenshteinTo:'computer' |
2782 |
'compoter' levenshteinTo:'computer' |
|
2783 |
||
2784 |
'comptuer' levenshteinTo:'computer' |
|
11861 | 2785 |
" |
2786 |
! |
|
2787 |
||
2788 |
levenshteinTo:aString s:substWeight k:kbdTypoWeight c:caseWeight i:insrtWeight d:deleteWeight |
|
2789 |
"parametrized levenshtein. |
|
2790 |
return the levenshtein distance to the argument, aString; |
|
2791 |
this value corrensponds to the number of replacements that have to be |
|
2792 |
made to get aString from the receiver. |
|
2793 |
The arguments are the costs for |
|
20874 | 2794 |
s:substitution, |
2795 |
k:keyboard type (substitution), |
|
2796 |
c:case-change, |
|
2797 |
i:insertion |
|
2798 |
d:deletion |
|
11861 | 2799 |
of a character. |
2800 |
See IEEE transactions on Computers 1976 Pg 172 ff" |
|
2801 |
||
2802 |
^ StringUtilities |
|
20874 | 2803 |
levenshteinDistanceFrom:self |
2804 |
to:aString |
|
2805 |
s:substWeight k:kbdTypoWeight c:caseWeight e:nil i:insrtWeight d:deleteWeight |
|
11861 | 2806 |
! |
2807 |
||
9190 | 2808 |
sameAs:aString |
2809 |
"Compare the receiver with the argument like =, but ignore case differences. |
|
2810 |
Return true or false." |
|
2811 |
||
2812 |
|mySize "{ Class: SmallInteger }" |
|
2813 |
otherSize c1 c2| |
|
2814 |
||
9192 | 2815 |
self == aString ifTrue:[^ true]. |
2816 |
||
2817 |
mySize := self size. |
|
2818 |
otherSize := aString string size. |
|
18884 | 2819 |
mySize ~~ otherSize ifTrue:[^ false]. |
9192 | 2820 |
|
2821 |
1 to:mySize do:[:index | |
|
20874 | 2822 |
c1 := self at:index. |
2823 |
c2 := aString at:index. |
|
2824 |
c1 ~~ c2 ifTrue:[ |
|
2825 |
(c1 sameAs:c2) ifFalse:[^ false]. |
|
2826 |
] |
|
9192 | 2827 |
]. |
2828 |
^ true |
|
2829 |
||
608 | 2830 |
" |
8874 | 2831 |
'foo' sameAs: 'Foo' |
2832 |
'foo' sameAs: 'bar' |
|
2833 |
'foo' sameAs: 'foo' |
|
9192 | 2834 |
" |
2835 |
||
2836 |
"Modified: 22.4.1996 / 15:56:17 / cg" |
|
2837 |
! |
|
608 | 2838 |
|
15734 | 2839 |
sameAs:aString caseSensitive:caseSensitive |
2840 |
"Compare the receiver with the argument. |
|
2841 |
If caseSensitive is false, this is the same as #sameAs:, |
|
2842 |
if false, this is the same as #=." |
|
2843 |
||
2844 |
caseSensitive ifFalse:[ |
|
20874 | 2845 |
^ self sameAs:aString |
15734 | 2846 |
]. |
2847 |
^ self = aString |
|
2848 |
||
2849 |
" |
|
2850 |
'foo' sameAs:'Foo' caseSensitive:false |
|
2851 |
'foo' sameAs:'foo' caseSensitive:true |
|
2852 |
" |
|
2853 |
! |
|
2854 |
||
5242 | 2855 |
sameAs:aString ignoreCase:ignoreCase |
15734 | 2856 |
<resource: #obsolete> |
5242 | 2857 |
"Compare the receiver with the argument. |
2858 |
If ignoreCase is true, this is the same as #sameAs:, |
|
2859 |
if false, this is the same as #=." |
|
2860 |
||
2861 |
ignoreCase ifTrue:[ |
|
20874 | 2862 |
^ self sameAs:aString |
5242 | 2863 |
]. |
2864 |
^ self = aString |
|
2865 |
||
2866 |
" |
|
8874 | 2867 |
'foo' sameAs:'Foo' ignoreCase:false |
2868 |
'foo' sameAs:'foo' ignoreCase:true |
|
5242 | 2869 |
" |
2870 |
||
2871 |
! |
|
2872 |
||
608 | 2873 |
sameCharacters:aString |
9192 | 2874 |
"count & return the number of characters which are the same |
2875 |
(ignoring case and emphasis) in the receiver and the argument, aString." |
|
2876 |
||
2877 |
|n "{ Class: SmallInteger }" |
|
2878 |
c1 c2 cnt| |
|
2879 |
||
2880 |
n := self size. |
|
2881 |
n := n min:(aString string size). |
|
2882 |
||
2883 |
cnt := 0. |
|
2884 |
1 to:n do:[:index | |
|
20874 | 2885 |
c1 := self at:index. |
2886 |
c2 := aString at:index. |
|
2887 |
((c1 == c2) |
|
2888 |
or:[c1 asLowercase = c2 asLowercase]) ifTrue:[ |
|
2889 |
cnt := cnt + 1 |
|
2890 |
] |
|
9192 | 2891 |
]. |
2892 |
^ cnt |
|
2893 |
||
2894 |
" |
|
2895 |
'foobarbaz' sameCharacters: 'foo' |
|
8874 | 2896 |
'foobarbaz' sameCharacters: 'Foo' |
2897 |
'foobarbaz' sameCharacters: 'baz' |
|
608 | 2898 |
" |
5510
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
2899 |
! |
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
2900 |
|
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
2901 |
sameEmphasisAs:aStringOrText |
17215 | 2902 |
"compare the receiver's and the argument's emphasis" |
5510
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
2903 |
|
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
2904 |
^ self emphasis = aStringOrText emphasis |
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
2905 |
|
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
2906 |
" |
8874 | 2907 |
'hello' asText sameEmphasisAs: 'hello' |
2908 |
'hello' asText sameEmphasisAs: 'hello' asText |
|
2909 |
'hello' asText allBold sameEmphasisAs: 'hello' |
|
2910 |
'hello' asText allBold sameEmphasisAs: 'fooba' asText allBold |
|
9192 | 2911 |
'hello' asText allBold sameEmphasisAs: 'fooba' asText allItalic |
2912 |
" |
|
2913 |
! |
|
2914 |
||
9191 | 2915 |
sameStringAndEmphasisAs:aStringOrText |
5739
4fd3be7ccc75
oops some relational ops did accept a non-string arg
Claus Gittinger <cg@exept.de>
parents:
5732
diff
changeset
|
2916 |
"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
|
2917 |
|
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
2918 |
aStringOrText isString ifFalse:[^ false]. |
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
2919 |
(self string = aStringOrText string) ifFalse:[^ false]. |
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
2920 |
self hasChangeOfEmphasis = aStringOrText hasChangeOfEmphasis ifFalse:[^ false]. |
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
2921 |
^ self emphasis = aStringOrText emphasis |
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
2922 |
|
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
2923 |
" |
8874 | 2924 |
'hello' asText sameEmphasisAs: 'hello' |
2925 |
'hello' asText sameEmphasisAs: 'hello' asText |
|
2926 |
'hello' asText allBold sameEmphasisAs: 'hello' |
|
2927 |
'hello' asText allBold sameEmphasisAs: 'fooba' asText allBold |
|
2928 |
'hello' asText allBold sameEmphasisAs: 'fooba' asText allItalic |
|
2929 |
||
2930 |
'hello' sameEmphasisAs: 'hello' asText |
|
2931 |
'hello' sameEmphasisAs: 'hello' asText allBold |
|
2932 |
'hello' sameEmphasisAs: 'fooba' |
|
2933 |
'hello' sameEmphasisAs: 'fooba' asText |
|
2934 |
'hello' sameEmphasisAs: 'fooba' asText allBold |
|
2935 |
'hello' sameEmphasisAs: 'fooba' asText allItalic |
|
2936 |
||
2937 |
'hello' asText sameStringAndEmphasisAs: 'hello' |
|
2938 |
'hello' asText sameStringAndEmphasisAs: 'hello' asText |
|
2939 |
'hello' asText allBold sameStringAndEmphasisAs: 'hello' |
|
2940 |
'hello' asText allBold sameStringAndEmphasisAs: 'fooba' asText allBold |
|
2941 |
'hello' asText allBold sameStringAndEmphasisAs: 'fooba' asText allItalic |
|
2942 |
||
2943 |
'hello' sameStringAndEmphasisAs: 'hello' asText |
|
2944 |
'hello' sameStringAndEmphasisAs: 'hello' asText allBold |
|
2945 |
'hello' sameStringAndEmphasisAs: 'fooba' |
|
2946 |
'hello' sameStringAndEmphasisAs: 'fooba' asText |
|
2947 |
'hello' sameStringAndEmphasisAs: 'fooba' asText allBold |
|
2948 |
'hello' sameStringAndEmphasisAs: 'fooba' asText allItalic |
|
5510
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
2949 |
" |
11861 | 2950 |
! |
2951 |
||
2952 |
spellAgainst: aString |
|
2953 |
"return an integer between 0 and 100 indicating how similar |
|
2954 |
the argument is to the receiver. No case conversion is done. |
|
2955 |
This algorithm is much simpler (but also less exact) than the |
|
2956 |
levenshtein distance. Experiment which is better for your |
|
2957 |
application." |
|
2958 |
||
2959 |
| i1 "{ Class: SmallInteger }" |
|
2960 |
i2 "{ Class: SmallInteger }" |
|
2961 |
next1 "{ Class: SmallInteger }" |
|
2962 |
next2 "{ Class: SmallInteger }" |
|
2963 |
size1 "{ Class: SmallInteger }" |
|
2964 |
size2 "{ Class: SmallInteger }" |
|
2965 |
score "{ Class: SmallInteger }" |
|
2966 |
maxLen "{ Class: SmallInteger }" | |
|
2967 |
||
2968 |
size1 := self size. |
|
2969 |
size2 := aString size. |
|
2970 |
maxLen := size1 max:size2. |
|
2971 |
score := 0. |
|
2972 |
i1 := i2 := 1. |
|
2973 |
[i1 <= size1 and: [i2 <= size2]] whileTrue:[ |
|
20874 | 2974 |
next1 := i1 + 1. |
2975 |
next2 := i2 + 1. |
|
2976 |
(self at:i1) == (aString at:i2) ifTrue: [ |
|
2977 |
score := score+1. |
|
2978 |
i1 := next1. |
|
2979 |
i2 := next2 |
|
2980 |
] ifFalse: [ |
|
2981 |
(i2 < size2 and: [(self at:i1) == (aString at:next2)]) ifTrue: [ |
|
2982 |
i2 := next2 |
|
2983 |
] ifFalse: [ |
|
2984 |
(i1 < size1 and: [(self at:next1) == (aString at:i2)]) ifTrue: [ |
|
2985 |
i1 := next1 |
|
2986 |
] ifFalse: [ |
|
2987 |
i1 := next1. |
|
2988 |
i2 := next2 |
|
2989 |
] |
|
2990 |
] |
|
2991 |
] |
|
11861 | 2992 |
]. |
2993 |
||
2994 |
score == maxLen ifTrue: [^ 100]. |
|
2995 |
^ 100 * score // maxLen |
|
2996 |
||
2997 |
" |
|
2998 |
'Smalltalk' spellAgainst: 'Smalltlak' |
|
2999 |
'Smalltalk' spellAgainst: 'smalltlak' |
|
3000 |
'Smalltalk' spellAgainst: 'smalltalk' |
|
3001 |
'Smalltalk' spellAgainst: 'smalltlk' |
|
3002 |
'Smalltalk' spellAgainst: 'Smalltolk' |
|
3003 |
" |
|
3004 |
! |
|
3005 |
||
16553 | 3006 |
startsWith:aStringOrCharacter |
3007 |
"return true, if the receiver starts with something, aStringOrCharacter. |
|
17371 | 3008 |
If the argument is empty, true is returned. |
3009 |
Notice, that this is similar to, but slightly different from VW's and Squeak's beginsWith:, |
|
3010 |
which are both inconsistent w.r.t. an empty argument." |
|
11861 | 3011 |
|
3012 |
|s| |
|
3013 |
||
16553 | 3014 |
aStringOrCharacter isCharacter ifTrue:[ |
21083 | 3015 |
^ (self size ~~ 0) and:[ (self at:1) == aStringOrCharacter ] |
16553 | 3016 |
]. |
11861 | 3017 |
(s := self string) ~~ self ifTrue:[ |
20874 | 3018 |
^ s startsWith:aStringOrCharacter |
11861 | 3019 |
]. |
16553 | 3020 |
^ super startsWith:aStringOrCharacter |
11861 | 3021 |
|
3022 |
" |
|
17364 | 3023 |
'abcde' startsWith:#($a $b $c) |
3024 |
'abcde' startsWith:'abc' |
|
3025 |
'abcd' startsWith:'abcde' |
|
3026 |
'abcde' startsWith:'abd' |
|
11861 | 3027 |
'hello world' startsWith:'hello' |
3028 |
'hello world' asText allBold startsWith:'hello' |
|
3029 |
'hello world' asText allBold startsWith:'' |
|
3030 |
" |
|
3031 |
||
3032 |
"Created: 12.5.1996 / 15:46:40 / cg" |
|
3033 |
"Modified: 12.5.1996 / 15:49:24 / cg" |
|
9192 | 3034 |
! ! |
3035 |
||
3036 |
!CharacterArray methodsFor:'converting'! |
|
3037 |
||
11881 | 3038 |
asAsciiZ |
3039 |
"if the receiver does not end with a 0-valued character, return a copy of it, |
|
3040 |
with an additional 0-character. Otherwise return the receiver. This is sometimes |
|
3041 |
needed when a string has to be passed to C, which needs 0-terminated strings. |
|
3042 |
Notice, that all singleByte strings are already 0-terminated in ST/X, whereas wide |
|
3043 |
strings are not." |
|
3044 |
||
12658 | 3045 |
(self notEmpty and:[self last codePoint == 0]) ifTrue:[^ self ]. |
11881 | 3046 |
^ self copyWith:(Character value:0). |
3047 |
||
3048 |
" |
|
3049 |
'abc' asAsciiZ |
|
3050 |
'abc' asWideString asAsciiZ |
|
3051 |
" |
|
3052 |
! |
|
3053 |
||
13524 | 3054 |
asByteArray |
15936 | 3055 |
"depending on the size of the characters in the receiver, |
3056 |
return a byteArray containing single-, double- or even 4-bytewise values. |
|
3057 |
The size of the returned byteArray will be the strings size multiplied by the |
|
3058 |
size required for the largest character. |
|
19233 | 3059 |
Attention: The bytes are in native byte order. |
15936 | 3060 |
Caveat: better use utf8Encoded, to get reproducable results" |
3061 |
||
14555 | 3062 |
|bytes sz bytesPerCharacter idx str| |
3063 |
||
3064 |
str := self string. |
|
3065 |
str ~~ self ifTrue:[ |
|
20874 | 3066 |
"/ for text and other wrappers |
3067 |
^ str asByteArray |
|
13611
ba8c7372218a
variable renamed in: #inspector2TabBytes
Claus Gittinger <cg@exept.de>
parents:
13524
diff
changeset
|
3068 |
]. |
ba8c7372218a
variable renamed in: #inspector2TabBytes
Claus Gittinger <cg@exept.de>
parents:
13524
diff
changeset
|
3069 |
|
ba8c7372218a
variable renamed in: #inspector2TabBytes
Claus Gittinger <cg@exept.de>
parents:
13524
diff
changeset
|
3070 |
"/ for real strings, a fallback |
13524 | 3071 |
sz := self size. |
19757 | 3072 |
bytesPerCharacter := self bytesPerCharacter. |
13524 | 3073 |
bytes := ByteArray new:(sz * bytesPerCharacter). |
3074 |
idx := 1. |
|
3075 |
self do:[:char | |
|
20874 | 3076 |
|code| |
3077 |
||
3078 |
code := char codePoint. |
|
3079 |
bytesPerCharacter == 2 ifTrue:[ |
|
3080 |
bytes unsignedInt16At:idx put:code |
|
3081 |
] ifFalse:[ |
|
3082 |
bytesPerCharacter == 4 ifTrue:[ |
|
3083 |
bytes unsignedInt32At:idx put:code |
|
3084 |
] ifFalse:[ |
|
3085 |
bytes at:idx put:code |
|
3086 |
]. |
|
3087 |
]. |
|
3088 |
idx := idx + bytesPerCharacter. |
|
13524 | 3089 |
]. |
3090 |
^ bytes |
|
3091 |
||
3092 |
"Created: / 27-07-2011 / 00:56:17 / cg" |
|
3093 |
! |
|
3094 |
||
19233 | 3095 |
asByteArrayMSB:msb |
3096 |
"depending on the size of the characters in the receiver, |
|
3097 |
return a byteArray containing single-, double- or even 4-bytewise values. |
|
3098 |
The size of the returned byteArray will be the strings size multiplied by the |
|
3099 |
size required for the largest character. |
|
3100 |
Caveat: better use utf8Encoded, to get reproducable results" |
|
3101 |
||
3102 |
|ba| |
|
3103 |
||
3104 |
ba := self asByteArray. "/ native order |
|
3105 |
UninterpretedBytes isBigEndian ~~ msb ifTrue:[ |
|
20874 | 3106 |
ba swapBytes |
19233 | 3107 |
]. |
3108 |
^ ba |
|
3109 |
! |
|
3110 |
||
20480 | 3111 |
asCanonicalizedFilename |
3112 |
"return a Filename with pathname taken from the receiver. |
|
3113 |
The filename is canonicalized, meaning that it cares for trailing directory separators, |
|
3114 |
'.' components etc." |
|
3115 |
||
3116 |
^ self asFilename asCanonicalizedFilename |
|
3117 |
||
3118 |
"on windows: |
|
3119 |
'c:\foo\bar' asFilename |
|
3120 |
'c:\foo\bar\' asFilename |
|
3121 |
'c:\foo\bar\..\baz' asFilename |
|
3122 |
'c:\foo\bar\..\baz\.' asFilename |
|
3123 |
'c:\foo\bar' asCanonicalizedFilename |
|
3124 |
'c:\foo\bar\' asCanonicalizedFilename |
|
3125 |
'c:\foo\bar\..\baz' asCanonicalizedFilename |
|
3126 |
'c:\foo\bar\..\baz\.' asCanonicalizedFilename |
|
3127 |
||
3128 |
on unix: |
|
3129 |
'/foo/bar' asFilename |
|
3130 |
'/foo/bar/' asFilename |
|
3131 |
'/foo/bar/../baz' asFilename |
|
3132 |
'/foo/bar/../baz/.' asFilename |
|
3133 |
'/foo/bar' asCanonicalizedFilename |
|
3134 |
'/foo/bar/' asCanonicalizedFilename |
|
3135 |
'/foo/bar/../baz' asCanonicalizedFilename |
|
3136 |
'/foo/bar/../baz/.' asCanonicalizedFilename |
|
3137 |
" |
|
3138 |
! |
|
3139 |
||
608 | 3140 |
asCollectionOfLines |
8874 | 3141 |
"return a collection containing the lines (separated by cr) |
608 | 3142 |
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
|
3143 |
contain empty strings. |
14456
d7c3e67647f1
comment/format in: #asCollectionOfLines
Claus Gittinger <cg@exept.de>
parents:
14455
diff
changeset
|
3144 |
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
|
3145 |
See also #asCollectionOfLinesWithReturn |
d7c3e67647f1
comment/format in: #asCollectionOfLines
Claus Gittinger <cg@exept.de>
parents:
14455
diff
changeset
|
3146 |
(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
|
3147 |
this method and we did not want to risk any incompatibilities)" |
14455
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3148 |
|
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3149 |
^ self asCollectionOfSubstringsSeparatedBy:Character cr. |
608 | 3150 |
|
3151 |
" |
|
3152 |
'1 one\2 two\3 three\4 four\5 five' withCRs asCollectionOfLines |
|
8874 | 3153 |
'1 one\\\\2 two\3 three' withCRs asCollectionOfLines |
20182 | 3154 |
|
3155 |
('foo \r\nbar\nbaz\t\r\n\r\nbla' printf:#()) |
|
3156 |
asCollectionOfLines collect:[:l | (l endsWith:Character return) ifTrue:[l copyButLast:1] ifFalse:l] |
|
608 | 3157 |
" |
3158 |
! |
|
3159 |
||
14455
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3160 |
asCollectionOfLinesWithReturn |
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3161 |
"return a collection containing the lines (separated by cr) |
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3162 |
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
|
3163 |
contain empty strings." |
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3164 |
|
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3165 |
|lines| |
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3166 |
|
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3167 |
lines := self asCollectionOfSubstringsSeparatedBy:Character cr. |
18194 | 3168 |
(lines notEmpty and:[lines last isEmpty]) ifTrue:[ |
20874 | 3169 |
^ lines copyButLast:1 |
14455
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3170 |
]. |
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3171 |
^ lines |
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3172 |
|
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3173 |
" |
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3174 |
'1\2\3' withCRs asCollectionOfLines |
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3175 |
'1\2\3\' withCRs asCollectionOfLines |
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3176 |
'1\2\3' withCRs asCollectionOfLinesWithReturn |
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3177 |
'1\2\3\' withCRs asCollectionOfLinesWithReturn |
18194 | 3178 |
'' withCRs asCollectionOfLinesWithReturn |
14455
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3179 |
" |
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3180 |
! |
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3181 |
|
608 | 3182 |
asCollectionOfSubstringsSeparatedBy:aCharacter |
15740 | 3183 |
"return a collection containing substrings (separated by aCharacter) |
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
3184 |
of the receiver. |
15740 | 3185 |
If aCharacter occurs multiple times in a row, the result will contain empty strings. |
3186 |
If the receiver ends with aCharacter, an empty string with be the last result element." |
|
608 | 3187 |
|
9192 | 3188 |
^ self asCollectionOfSubCollectionsSeparatedBy:aCharacter |
3189 |
||
3190 |
" |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
3191 |
'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
|
3192 |
'1 one:2 two:3 three:4 four:5 five:' asCollectionOfSubstringsSeparatedBy:$: |
15740 | 3193 |
'1 one 2 two 3 three 4 four 5 five' asCollectionOfSubstringsSeparatedBy:Character space |
9192 | 3194 |
" |
3195 |
! |
|
608 | 3196 |
|
11828 | 3197 |
asCollectionOfSubstringsSeparatedBy:aCharacter exceptIn:ch |
15740 | 3198 |
"return a collection containing the substrings (separated by aCharacter) |
12177 | 3199 |
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
|
3200 |
the result will contain empty strings. |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
3201 |
The separation is not done, inside a matching pair of ch-substrings. |
15740 | 3202 |
Can be used to tokenize csv-like strings, which may or may not be enclosed in quotes." |
11828 | 3203 |
|
3204 |
|lines myClass except i c |
|
3205 |
startIndex "{ Class:SmallInteger }" |
|
3206 |
stopIndex "{ Class:SmallInteger }" | |
|
3207 |
||
11905
cbc2191cfe2d
return stringCollections from asCollectionOfXXX
Claus Gittinger <cg@exept.de>
parents:
11904
diff
changeset
|
3208 |
lines := StringCollection new. |
11828 | 3209 |
myClass := self species. |
3210 |
||
3211 |
startIndex := 1. |
|
3212 |
except := false. |
|
3213 |
[ |
|
20874 | 3214 |
i := startIndex-1. |
3215 |
[ |
|
3216 |
i := i+1. |
|
3217 |
c := self at:i. |
|
3218 |
c = ch ifTrue:[ except := except not. ]. |
|
3219 |
i < self size and:[except or:[c ~= aCharacter]] |
|
3220 |
] whileTrue. |
|
3221 |
||
3222 |
c = aCharacter ifTrue:[ |
|
3223 |
stopIndex := i -1. |
|
3224 |
] ifFalse: [ |
|
3225 |
stopIndex := i. |
|
3226 |
]. |
|
3227 |
(stopIndex < startIndex) ifTrue: [ |
|
3228 |
lines add:(myClass new:0) |
|
3229 |
] ifFalse: [ |
|
3230 |
lines add:(self copyFrom:startIndex to:stopIndex) |
|
3231 |
]. |
|
3232 |
startIndex := stopIndex + 2. |
|
3233 |
startIndex <= self size |
|
11828 | 3234 |
] whileTrue. |
3235 |
^ lines |
|
3236 |
||
3237 |
" |
|
12177 | 3238 |
'asd''f;d''dd;s' asCollectionOfSubstringsSeparatedBy:$; exceptIn:$' |
3239 |
" |
|
3240 |
"/ 'asd "hello bla" foo "bla bla" bar' asCollectionOfSubstringsSeparatedBy:$ exceptIn:$" |
|
11828 | 3241 |
! |
3242 |
||
13344
ec6860c49990
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13343
diff
changeset
|
3243 |
asCollectionOfSubstringsSeparatedBy:aFieldSeparatorString textSeparator:aTextSeparatorOrNil |
13343
3f058ad0d2f1
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13332
diff
changeset
|
3244 |
"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
|
3245 |
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
|
3246 |
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
|
3247 |
|
13344
ec6860c49990
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13343
diff
changeset
|
3248 |
|aTextSeparatorChar items scanningWord inStream element lastIsFieldSeparator sz| |
ec6860c49990
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13343
diff
changeset
|
3249 |
|
ec6860c49990
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13343
diff
changeset
|
3250 |
aTextSeparatorOrNil isNil ifTrue:[ |
19961 | 3251 |
^ self asCollectionOfSubstringsSeparatedByAll: aFieldSeparatorString |
13327
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3252 |
]. |
13344
ec6860c49990
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13343
diff
changeset
|
3253 |
sz := aTextSeparatorOrNil size. |
ec6860c49990
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13343
diff
changeset
|
3254 |
sz = 0 ifTrue:[ |
19961 | 3255 |
aTextSeparatorChar := aTextSeparatorOrNil |
13344
ec6860c49990
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13343
diff
changeset
|
3256 |
] ifFalse:[sz = 1 ifTrue:[ |
19961 | 3257 |
"this is a String. Fetch the first character - compatibility to older expecco libs" |
3258 |
aTextSeparatorChar := aTextSeparatorOrNil first. |
|
13344
ec6860c49990
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13343
diff
changeset
|
3259 |
] ifFalse:[ |
19961 | 3260 |
self error:'textSeparatorSize > 1'. |
13344
ec6860c49990
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13343
diff
changeset
|
3261 |
]]. |
13327
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 |
items := OrderedCollection new. |
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3264 |
|
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3265 |
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
|
3266 |
[ |
19961 | 3267 |
inStream skipSeparators. |
3268 |
inStream atEnd |
|
13327
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3269 |
] whileFalse:[ |
19961 | 3270 |
lastIsFieldSeparator := false. |
3271 |
inStream peek == aTextSeparatorChar ifTrue:[ |
|
3272 |
inStream next. |
|
3273 |
element := ''. |
|
3274 |
scanningWord := true. |
|
3275 |
[ scanningWord and:[inStream atEnd not] ] whileTrue:[ |
|
3276 |
element := element , (inStream upTo:aTextSeparatorChar). |
|
3277 |
(inStream peek == aTextSeparatorChar) ifTrue:[ |
|
3278 |
element := element , aTextSeparatorChar . |
|
3279 |
inStream next. |
|
3280 |
] ifFalse:[ |
|
3281 |
scanningWord := false. |
|
3282 |
]. |
|
3283 |
]. |
|
3284 |
inStream upToAll_positionBefore:aFieldSeparatorString. |
|
3285 |
] ifFalse:[ |
|
3286 |
element := inStream upToAll_positionBefore:aFieldSeparatorString |
|
3287 |
]. |
|
3288 |
items add:element. |
|
3289 |
lastIsFieldSeparator := (inStream skipThroughAll:aFieldSeparatorString) notNil. |
|
13343
3f058ad0d2f1
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13332
diff
changeset
|
3290 |
]. |
3f058ad0d2f1
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13332
diff
changeset
|
3291 |
lastIsFieldSeparator ifTrue:[ |
19961 | 3292 |
"empty element at the end of the line" |
3293 |
items add:''. |
|
13327
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3294 |
]. |
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3295 |
|
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3296 |
^ items |
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3297 |
|
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3298 |
" |
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3299 |
self assert:(('#First#, #Second,SecondAdd#, #Third#' asCollectionOfSubstringsSeparatedBy:',' textSeparator: $#) |
19961 | 3300 |
sameContentsAs:#('First' 'Second,SecondAdd' 'Third')). |
13327
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3301 |
self assert:(('#Fir##st#, #Second,SecondAdd#, #Third#' asCollectionOfSubstringsSeparatedBy:',' textSeparator: $#) |
19961 | 3302 |
sameContentsAs:#('Fir#st' 'Second,SecondAdd' 'Third')). |
13327
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3303 |
self assert:(('#Fir##st#, Second,SecondAdd, #Third#' asCollectionOfSubstringsSeparatedBy:',' textSeparator: $#) |
19961 | 3304 |
sameContentsAs:#('Fir#st' 'Second' 'SecondAdd' 'Third')). |
13343
3f058ad0d2f1
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13332
diff
changeset
|
3305 |
self assert:(('First,Second,Third,,' asCollectionOfSubstringsSeparatedBy:',' textSeparator:nil) |
19961 | 3306 |
sameContentsAs:#('First' 'Second' 'Third' '' '')). |
13344
ec6860c49990
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13343
diff
changeset
|
3307 |
self assert:(('First,Second,Third,,' asCollectionOfSubstringsSeparatedBy:',' textSeparator:'#') |
19961 | 3308 |
sameContentsAs:#('First' 'Second' 'Third' '' '')). |
13327
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3309 |
" |
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3310 |
|
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3311 |
"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
|
3312 |
! |
999589445aa7
add new method for splitting a csv string into csv elements
ab
parents:
10437
diff
changeset
|
3313 |
|
7339
20969b5770c9
asCollectionOfSubstringsSeparatedByAll:
Claus Gittinger <cg@exept.de>
parents:
7318
diff
changeset
|
3314 |
asCollectionOfSubstringsSeparatedByAll:aSeparatorString |
8874 | 3315 |
"return a collection containing the lines (separated by aSeparatorString) |
3316 |
of the receiver. If aSeparatorString occurs multiple times in a row, |
|
7339
20969b5770c9
asCollectionOfSubstringsSeparatedByAll:
Claus Gittinger <cg@exept.de>
parents:
7318
diff
changeset
|
3317 |
the result will contain empty strings." |
20969b5770c9
asCollectionOfSubstringsSeparatedByAll:
Claus Gittinger <cg@exept.de>
parents:
7318
diff
changeset
|
3318 |
|
8852
9f5984eff2c2
use asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8794
diff
changeset
|
3319 |
^ self asCollectionOfSubCollectionsSeparatedByAll:aSeparatorString |
9f5984eff2c2
use asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8794
diff
changeset
|
3320 |
|
9f5984eff2c2
use asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8794
diff
changeset
|
3321 |
" |
8874 | 3322 |
'1::2::3::4::5::' asCollectionOfSubstringsSeparatedByAll:'::' |
7339
20969b5770c9
asCollectionOfSubstringsSeparatedByAll:
Claus Gittinger <cg@exept.de>
parents:
7318
diff
changeset
|
3323 |
" |
20969b5770c9
asCollectionOfSubstringsSeparatedByAll:
Claus Gittinger <cg@exept.de>
parents:
7318
diff
changeset
|
3324 |
! |
20969b5770c9
asCollectionOfSubstringsSeparatedByAll:
Claus Gittinger <cg@exept.de>
parents:
7318
diff
changeset
|
3325 |
|
608 | 3326 |
asCollectionOfSubstringsSeparatedByAny:aCollectionOfSeparators |
3327 |
"return a collection containing the words (separated by any character |
|
3328 |
from aCollectionOfSeparators) of the receiver. |
|
3329 |
This allows breaking up strings using any character as separator." |
|
3330 |
||
8852
9f5984eff2c2
use asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8794
diff
changeset
|
3331 |
^ self asCollectionOfSubCollectionsSeparatedByAny:aCollectionOfSeparators |
608 | 3332 |
|
3333 |
" |
|
3334 |
'hello:world:isnt:this nice' asCollectionOfSubstringsSeparatedByAny:#($:) |
|
8874 | 3335 |
'hello:world:isnt:this nice' asCollectionOfSubstringsSeparatedByAny:':' |
3336 |
'hello:world:isnt:this nice' asCollectionOfSubstringsSeparatedByAny:(Array with:$: with:Character space) |
|
3337 |
'hello:world:isnt:this nice' asCollectionOfSubstringsSeparatedByAny:': ' |
|
3338 |
'h1e2l3l4o' asCollectionOfSubstringsSeparatedByAny:($1 to: $9) |
|
608 | 3339 |
" |
3340 |
! |
|
3341 |
||
3342 |
asCollectionOfWords |
|
8874 | 3343 |
"return a collection containing the words (separated by whitespace) |
3344 |
of the receiver. Multiple occurrences of whitespace characters will |
|
17517 | 3345 |
be treated like one - i.e. whitespace is skipped. |
3346 |
(sigh: it is called #'subStrings' in V'Age, and #'substrings' in Squeak) " |
|
608 | 3347 |
|
7797 | 3348 |
|words| |
608 | 3349 |
|
11905
cbc2191cfe2d
return stringCollections from asCollectionOfXXX
Claus Gittinger <cg@exept.de>
parents:
11904
diff
changeset
|
3350 |
words := StringCollection new. |
7797 | 3351 |
self asCollectionOfWordsDo:[:w | words add:w]. |
608 | 3352 |
^ words |
3353 |
||
3354 |
" |
|
3355 |
'hello world isnt this nice' asCollectionOfWords |
|
3356 |
' hello world isnt this nice ' asCollectionOfWords |
|
3357 |
'hello' asCollectionOfWords |
|
3358 |
'' asCollectionOfWords |
|
3359 |
' ' asCollectionOfWords |
|
10703
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
3360 |
' foo bar__baz__bla__ bar ' asCollectionOfWords |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
3361 |
' foo __bar__baz__bla__ bar ' asCollectionOfWords |
608 | 3362 |
" |
9192 | 3363 |
! |
3364 |
||
3365 |
asCollectionOfWordsDo:aBlock |
|
8874 | 3366 |
"evaluate aBlock for each word (separated by whitespace) of the receiver. |
3367 |
Multiple occurrences of whitespace characters will be treated like one |
|
7797 | 3368 |
- i.e. whitespace is skipped. |
3369 |
Returns the number of words (i.e. the number of invocations of aBlock)." |
|
3370 |
||
3371 |
|count "{ Class:SmallInteger }" |
|
8874 | 3372 |
start "{ Class:SmallInteger }" |
3373 |
stop "{ Class:SmallInteger }" |
|
7797 | 3374 |
mySize "{ Class:SmallInteger }"| |
3375 |
||
3376 |
count := 0. |
|
3377 |
start := 1. |
|
3378 |
mySize := self size. |
|
3379 |
[start <= mySize] whileTrue:[ |
|
20874 | 3380 |
start := self indexOfNonSeparatorStartingAt:start. |
3381 |
start == 0 ifTrue:[ |
|
3382 |
^ count |
|
3383 |
]. |
|
3384 |
stop := self indexOfSeparatorStartingAt:start. |
|
3385 |
stop == 0 ifTrue:[ |
|
3386 |
aBlock value:(self copyFrom:start to:mySize). |
|
3387 |
^ count + 1 |
|
3388 |
]. |
|
3389 |
aBlock value:(self copyFrom:start to:(stop - 1)). |
|
3390 |
start := stop. |
|
3391 |
count := count + 1 |
|
7797 | 3392 |
]. |
3393 |
^ count |
|
3394 |
||
3395 |
" |
|
3396 |
'hello world isnt this nice' asCollectionOfWordsDo:[:w | Transcript showCR:w] |
|
3397 |
' hello world isnt this nice ' asCollectionOfWordsDo:[:w | Transcript showCR:w] |
|
3398 |
'hello' asCollectionOfWordsDo:[:w | Transcript showCR:w] |
|
3399 |
'' asCollectionOfWordsDo:[:w | Transcript showCR:w] |
|
3400 |
' ' asCollectionOfWordsDo:[:w | Transcript showCR:w] |
|
3401 |
" |
|
9192 | 3402 |
! |
3403 |
||
3404 |
asFilename |
|
3405 |
"return a Filename with pathname taken from the receiver" |
|
3406 |
||
3407 |
^ Filename named:self "(self asSingleByteStringReplaceInvalidWith:$?)" |
|
3408 |
||
3409 |
"Modified: 20.5.1996 / 09:38:15 / cg" |
|
3410 |
! |
|
608 | 3411 |
|
3060 | 3412 |
asFixedPoint |
3413 |
"read a fixedPoint number from the receiver. |
|
8874 | 3414 |
Notice, that errors may occur during the read, |
7423 | 3415 |
so you better setup some signal handler when using this method." |
3060 | 3416 |
|
3417 |
^ FixedPoint readFromString:self |
|
3418 |
||
3419 |
" |
|
8874 | 3420 |
'0.123' asFixedPoint |
3421 |
'12345' asFixedPoint |
|
3060 | 3422 |
'(1/5)' asFixedPoint |
3423 |
'foo' asFixedPoint |
|
9192 | 3424 |
Object errorSignal handle:[:ex | ex return:0] do:['foo' asFixedPoint] |
3425 |
" |
|
3426 |
||
3427 |
"Modified: / 25.10.1997 / 15:19:00 / cg" |
|
3428 |
! |
|
3429 |
||
3430 |
asFixedPoint:scale |
|
3060 | 3431 |
"read a fixedPoint number with scale number of post-decimal digits |
3432 |
from the receiver. Scale controls the number of displayed digits, |
|
3433 |
not the number of actually valid digits. |
|
8874 | 3434 |
Notice, that errors may occur during the read, |
7423 | 3435 |
so you better setup some signal handler when using this method." |
3060 | 3436 |
|
3437 |
^ (FixedPoint readFromString:self) scale:scale |
|
3438 |
||
3439 |
" |
|
8874 | 3440 |
'0.123' asFixedPoint:2 |
3441 |
'123456' asFixedPoint:2 |
|
3442 |
('3.14157' asFixedPoint:1) asFixedPoint:5 |
|
3443 |
'3.14157' asFixedPoint:2 |
|
3444 |
'foo' asFixedPoint:2 |
|
3060 | 3445 |
" |
3446 |
||
3447 |
"Modified: / 25.10.1997 / 15:21:57 / cg" |
|
9192 | 3448 |
! |
3449 |
||
3450 |
asFloat |
|
608 | 3451 |
"read a float number from the receiver. |
8874 | 3452 |
Notice, that errors may occur during the read, |
7422 | 3453 |
so you better setup some exception handler when using this method." |
608 | 3454 |
|
3455 |
^ (Number readFromString:self) asFloat |
|
3456 |
||
3457 |
" |
|
8874 | 3458 |
'0.123' asFloat |
608 | 3459 |
'12345' asFloat |
3460 |
'(1/5)' asFloat |
|
8874 | 3461 |
Object errorSignal handle:[:ex | ex return:0] do:['foo' asFloat] |
608 | 3462 |
" |
3463 |
! |
|
3464 |
||
3465 |
asInteger |
|
9190 | 3466 |
"convert the receiver into an integer. |
8874 | 3467 |
Notice, that errors may occur during the read, |
9190 | 3468 |
so you better setup some exception handler when using this method. |
3469 |
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
|
3470 |
in other smalltalks. |
9190 | 3471 |
For less strict integer reading, use Integer readFrom:aString" |
608 | 3472 |
|
3473 |
^ Integer readFromString:self |
|
3474 |
||
3475 |
" |
|
3476 |
'12345678901234567890' asInteger |
|
3477 |
'-1234' asInteger |
|
9190 | 3478 |
|
3479 |
The following raises an error: |
|
20874 | 3480 |
'0.123' asInteger <- reader finds more after reading 0 |
9190 | 3481 |
|
3482 |
whereas the less strict readFrom does not: |
|
20874 | 3483 |
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
|
3484 |
|
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
3485 |
'0.123' asInteger |
608 | 3486 |
'0.123' asNumber <- returns what you expect |
8874 | 3487 |
Object errorSignal handle:[:ex | ex return:0] do:['foo' asInteger] |
9190 | 3488 |
|
3489 |
" |
|
9192 | 3490 |
! |
3491 |
||
3492 |
asLowercase |
|
608 | 3493 |
"return a copy of myself in lowercase letters" |
3494 |
||
8003
6e690f4b79f1
asLowercase/asUppercase and friends care for 16-bit chars
Claus Gittinger <cg@exept.de>
parents:
7995
diff
changeset
|
3495 |
|newStr c bitsPerCharacter |
9192 | 3496 |
mySize "{ Class: SmallInteger }" | |
3497 |
||
3498 |
mySize := self size. |
|
3499 |
mySize == 0 ifTrue:[^ self]. |
|
18657 | 3500 |
|
9192 | 3501 |
newStr := self species new:mySize. |
3502 |
bitsPerCharacter := newStr bitsPerCharacter. |
|
3503 |
||
18657 | 3504 |
"/ handle the very seldom case of an uppercase char which needs |
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
3505 |
"/ more bits in its lowercase variant |
18657 | 3506 |
"/ (there are only a few of them) |
3507 |
||
9192 | 3508 |
1 to:mySize do:[:i | |
20705 | 3509 |
c := (self at:i) asLowercase. |
3510 |
(c bitsPerCharacter > bitsPerCharacter |
|
3511 |
and:[c stringSpecies ~= newStr stringSpecies]) ifTrue:[ |
|
3512 |
newStr := c stringSpecies fromString:newStr. |
|
3513 |
bitsPerCharacter := newStr bitsPerCharacter. |
|
3514 |
]. |
|
3515 |
newStr at:i put:c |
|
9192 | 3516 |
]. |
3517 |
^ newStr |
|
3518 |
||
3519 |
" |
|
3520 |
'HelloWorld' asLowercase |
|
18662 | 3521 |
'HelloWorld' asUnicode16String asLowercase |
9192 | 3522 |
'HelloWorld' asLowercaseFirst |
18657 | 3523 |
'HelloWorld' asUppercase |
20705 | 3524 |
'HelloWorldÿ' asUppercase |
3525 |
'HelloWorldŸ' asLowercase - currently returns an U16 string; should this be u8? |
|
9192 | 3526 |
" |
3527 |
! |
|
3528 |
||
3529 |
asLowercaseFirst |
|
17508 | 3530 |
"return a copy of myself where the first character is converted to lowercase. |
3531 |
If the first character is already lowercase, or there is no uppercase for it, return the |
|
3532 |
receiver." |
|
9192 | 3533 |
|
3534 |
|newString firstChar firstCharAsLowercase| |
|
3535 |
||
11525 | 3536 |
self isEmpty ifTrue:[^ self]. |
9192 | 3537 |
firstChar := (self at:1). |
3538 |
firstCharAsLowercase := firstChar asLowercase. |
|
3539 |
firstChar == firstCharAsLowercase ifTrue:[ ^ self]. |
|
3540 |
||
3541 |
firstCharAsLowercase bitsPerCharacter > self bitsPerCharacter ifTrue:[ |
|
20874 | 3542 |
newString := firstCharAsLowercase stringSpecies fromString:self. |
9192 | 3543 |
] ifFalse:[ |
20874 | 3544 |
newString := self stringSpecies fromString:self. |
9192 | 3545 |
]. |
9190 | 3546 |
newString at:1 put:firstCharAsLowercase. |
3547 |
^ newString |
|
608 | 3548 |
|
3549 |
" |
|
8874 | 3550 |
'HelloWorld' asLowercase |
3551 |
'HelloWorld' asLowercaseFirst |
|
608 | 3552 |
" |
3553 |
! |
|
3554 |
||
11828 | 3555 |
asLowercaseLast |
3556 |
"return a copy of myself where the last character is |
|
3557 |
converted to lowercase." |
|
3558 |
||
3559 |
|newString sz| |
|
3560 |
||
3561 |
sz := self size. |
|
3562 |
newString := self copyFrom:1 to:sz. |
|
3563 |
sz > 0 ifTrue:[ |
|
20874 | 3564 |
newString at:sz put:(newString at:sz) asLowercase |
11828 | 3565 |
]. |
3566 |
^ newString |
|
3567 |
||
3568 |
" |
|
12177 | 3569 |
'HelloWorld' asLowercase |
3570 |
'HelloWorlD' asLowercaseLast |
|
11828 | 3571 |
" |
3572 |
! |
|
3573 |
||
17644 | 3574 |
asMutator |
19473 | 3575 |
"return a corresponding setter method's selector. |
3576 |
I.e. #foo asMutator returns #foo:" |
|
17644 | 3577 |
|
20873 | 3578 |
^ (self asSingleByteString,':') asSymbol |
17644 | 3579 |
! |
3580 |
||
9190 | 3581 |
asNumber |
3582 |
"read a number from the receiver. |
|
3583 |
Notice, that (in contrast to ST-80) errors may occur during the read, |
|
3584 |
so you better setup some signal handler when using this method. |
|
3585 |
Also notice, that this is meant to read end-user numbers from a string; |
|
9135 | 3586 |
it does not handle smalltalk numbers (i.e. radix). |
3587 |
To read a smalltalk number, use Number >> readSmalltalkFrom:. |
|
608 | 3588 |
This may change if ANSI specifies it." |
3589 |
||
3590 |
"/ ST-80 behavior: |
|
3591 |
"/ ^ Number readFromString:self onError:0 |
|
3592 |
||
17061 | 3593 |
^ Number fromString:self |
3594 |
||
3595 |
" |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
3596 |
'123' asNumber |
608 | 3597 |
'123.567' asNumber |
3598 |
'(5/6)' asNumber |
|
3599 |
'foo' asNumber |
|
17061 | 3600 |
'123a' asNumber |
8874 | 3601 |
Object errorSignal handle:[:ex | ex returnWith:0] do:['foo' asNumber] |
608 | 3602 |
" |
9192 | 3603 |
! |
3604 |
||
3605 |
asNumberFromFormatString:ignored |
|
608 | 3606 |
"read a number from the receiver, ignoring any nonDigit characters. |
3607 |
This is typically used to convert from strings which include |
|
3608 |
dollar-signs or millenium digits. However, this method also ignores |
|
3609 |
the decimal point (if any) and therefore should be used with care." |
|
3610 |
||
3611 |
|tempString| |
|
3612 |
||
3613 |
tempString := self collect:[:char | char isDigit]. |
|
3614 |
^ Number readFromString:tempString onError:0 |
|
3615 |
||
3616 |
" |
|
3617 |
'USD 123' asNumberFromFormatString:'foo' |
|
3618 |
'DM 123' asNumberFromFormatString:'foo' |
|
3619 |
'123' asNumberFromFormatString:'foo' |
|
3620 |
'123.567' asNumberFromFormatString:'foo' |
|
3621 |
'(5/6)' asNumberFromFormatString:'foo' |
|
3622 |
'foo' asNumberFromFormatString:'foo' |
|
3623 |
" |
|
9192 | 3624 |
! |
3625 |
||
7977 | 3626 |
asSingleByteStringIfPossible |
3627 |
"if possible, return the receiver converted to a 'normal' string. |
|
18181 | 3628 |
It is only possible, if there are no characters with codePoints above 255 in the receiver. |
3629 |
If not possible, the (wideString) receiver is returned." |
|
7977 | 3630 |
|
14555 | 3631 |
self isWideString ifFalse:[^ self]. |
18967 | 3632 |
self containsNon8BitElements ifTrue:[^ self]. |
3633 |
^ self asSingleByteString |
|
9192 | 3634 |
|
3635 |
" |
|
3636 |
'hello' asSingleByteStringIfPossible |
|
18967 | 3637 |
'hello' asText asSingleByteStringIfPossible |
3638 |
'hello' asUnicodeString asText asSingleByteStringIfPossible |
|
9192 | 3639 |
'hello' asUnicodeString asSingleByteStringIfPossible |
3640 |
" |
|
3641 |
! |
|
3642 |
||
3643 |
asSingleByteStringReplaceInvalidWith:replacementCharacter |
|
3644 |
"return the receiver converted to a 'normal' string, |
|
3645 |
with invalid characters replaced by replacementCharacter. |
|
3646 |
Can be used to convert from 16-bit strings to 8-bit strings |
|
3647 |
and replace characters above code-255 with some replacement." |
|
3648 |
||
3649 |
|newString| |
|
3650 |
||
3651 |
newString := String new:(self size). |
|
3652 |
1 to:self size do:[:idx | |
|
20874 | 3653 |
|char| |
3654 |
||
3655 |
char := self at:idx. |
|
3656 |
char codePoint <= 16rFF ifTrue:[ |
|
3657 |
newString at:idx put:char |
|
3658 |
] ifFalse:[ |
|
3659 |
newString at:idx put:replacementCharacter |
|
3660 |
]. |
|
9190 | 3661 |
]. |
3662 |
^ newString |
|
3663 |
||
3664 |
"Created: 30.6.1997 / 13:02:14 / cg" |
|
7977 | 3665 |
! |
3666 |
||
9192 | 3667 |
asString |
3668 |
"return myself - I am a string" |
|
3669 |
||
3670 |
^ self |
|
3671 |
! |
|
3672 |
||
3673 |
asStringCollection |
|
3674 |
"return a collection of lines from myself." |
|
3675 |
||
3676 |
^ StringCollection fromString:self "string" |
|
3677 |
||
3678 |
"Modified: 13.5.1996 / 20:36:59 / cg" |
|
3679 |
! |
|
1375
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
3680 |
|
1434 | 3681 |
asSymbol |
19039 | 3682 |
"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
|
3683 |
The receiver must be a singleByte-String. |
ccd0e8f26439
printing - migrating towards unicodeSupport in ST-code
Claus Gittinger <cg@exept.de>
parents:
8040
diff
changeset
|
3684 |
TwoByte- and FourByteSymbols are (currently ?) not allowed." |
ccd0e8f26439
printing - migrating towards unicodeSupport in ST-code
Claus Gittinger <cg@exept.de>
parents:
8040
diff
changeset
|
3685 |
|
ccd0e8f26439
printing - migrating towards unicodeSupport in ST-code
Claus Gittinger <cg@exept.de>
parents:
8040
diff
changeset
|
3686 |
|str| |
ccd0e8f26439
printing - migrating towards unicodeSupport in ST-code
Claus Gittinger <cg@exept.de>
parents:
8040
diff
changeset
|
3687 |
|
9192 | 3688 |
str := self string. |
3689 |
str ~~ self ifTrue:[ ^ str asSymbol ]. |
|
3690 |
^ self asSingleByteString asSymbol |
|
3691 |
! |
|
3692 |
||
3693 |
asSymbolIfInterned |
|
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
3694 |
"If a symbol with the receiver's characters is already known, return it. |
19692 | 3695 |
Otherwise, return nil. |
9230 | 3696 |
This can be used to query for an existing symbol and is the same as: |
20874 | 3697 |
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
|
3698 |
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
|
3699 |
The receiver must be a singleByte-String. |
ccd0e8f26439
printing - migrating towards unicodeSupport in ST-code
Claus Gittinger <cg@exept.de>
parents:
8040
diff
changeset
|
3700 |
TwoByte- and FourByteSymbols are (currently ?) not allowed." |
1435
3ee1fde2aa58
allow asSymbolIfInterned for Texts
Claus Gittinger <cg@exept.de>
parents:
1434
diff
changeset
|
3701 |
|
18967 | 3702 |
|str| |
3703 |
||
3704 |
str := self string. |
|
3705 |
str == self ifTrue:[ |
|
20874 | 3706 |
"must be some kind of N-ByteString" |
3707 |
str := self asSingleByteStringIfPossible. |
|
3708 |
str == self ifTrue:[ |
|
3709 |
"single byte string conversion is not possible" |
|
3710 |
^ nil. |
|
3711 |
]. |
|
6642 | 3712 |
]. |
18967 | 3713 |
^ str asSymbolIfInterned |
3714 |
||
3715 |
" |
|
3716 |
(Unicode16String with:(Character value:16rFFFF)) asSymbolIfInterned |
|
3717 |
'new' asUnicodeString asSymbolIfInterned |
|
3718 |
'new' asText asSymbolIfInterned |
|
3719 |
'new' asUnicodeString asText asSymbolIfInterned |
|
3720 |
" |
|
1435
3ee1fde2aa58
allow asSymbolIfInterned for Texts
Claus Gittinger <cg@exept.de>
parents:
1434
diff
changeset
|
3721 |
|
3ee1fde2aa58
allow asSymbolIfInterned for Texts
Claus Gittinger <cg@exept.de>
parents:
1434
diff
changeset
|
3722 |
"Created: 22.5.1996 / 16:37:04 / cg" |
9192 | 3723 |
! |
3724 |
||
20160 | 3725 |
asSymbolIfInternedOrSelf |
3726 |
"If a symbol with the receiver's characters is already known, return it. |
|
3727 |
Otherwise, return self. |
|
3728 |
This can be used to query for an existing symbol and is the same as: |
|
3729 |
self knownAsSymbol ifTrue:[self asSymbol] ifFalse:[self] |
|
3730 |
but slightly faster, since the symbol lookup operation is only performed once. |
|
3731 |
The receiver must be a singleByte-String. |
|
3732 |
TwoByte- and FourByteSymbols are (currently ?) not allowed." |
|
3733 |
||
3734 |
|sym| |
|
3735 |
||
3736 |
sym := self asSymbolIfInterned. |
|
3737 |
^ sym isNil ifTrue:[self] ifFalse:[sym]. |
|
3738 |
||
3739 |
||
3740 |
" |
|
3741 |
(Unicode16String with:(Character value:16rFFFF)) asSymbolIfInternedOrSelf |
|
3742 |
'new' asUnicodeString asSymbolIfInternedOrSelf |
|
3743 |
'new' asText asSymbolIfInternedOrSelf |
|
3744 |
'new' asUnicodeString asText asSymbolIfInternedOrSelf |
|
3745 |
" |
|
3746 |
||
3747 |
"Created: 22.5.1996 / 16:37:04 / cg" |
|
3748 |
! |
|
3749 |
||
9192 | 3750 |
asText |
9403 | 3751 |
"return a Text-object (string with emphasis) from myself." |
9192 | 3752 |
|
3753 |
"this test allows for small non-gui apps to be built without libbasic2 (where Text is)" |
|
3754 |
Text isNil ifTrue:[^ self]. |
|
17065 | 3755 |
^ Text string:self |
9192 | 3756 |
|
3757 |
"Created: 12.5.1996 / 10:41:14 / cg" |
|
3758 |
! |
|
3759 |
||
17021 | 3760 |
asTimestamp |
3761 |
"convert the receiver into an Timestamp. |
|
3762 |
Notice, that errors may occur during the read, |
|
3763 |
so you better setup some exception handler when using this method." |
|
3764 |
||
21030 | 3765 |
^ Timestamp readFrom:self |
17021 | 3766 |
|
3767 |
" |
|
3768 |
'2014-11-10 21:30:22.444' asTimestamp |
|
3769 |
'2014-11-10 21:30:22.444Z' asTimestamp |
|
3770 |
'2014-11-10 21:30:22.444+0200' asTimestamp |
|
3771 |
" |
|
3772 |
! |
|
3773 |
||
9192 | 3774 |
asTitlecase |
8027 | 3775 |
"return a version of the receiver, where the first character is converted to titlecase, |
3776 |
and everything else to lowercase. |
|
3777 |
See the comment in Character on what titlecase is." |
|
3778 |
||
3779 |
|newStr c bitsPerCharacter |
|
3780 |
mySize "{ Class: SmallInteger }" | |
|
3781 |
||
3782 |
mySize := self size. |
|
3783 |
newStr := self species new:mySize. |
|
3784 |
bitsPerCharacter := newStr bitsPerCharacter. |
|
3785 |
||
3786 |
1 to:mySize do:[:i | |
|
20874 | 3787 |
i == 1 ifTrue:[ |
3788 |
c := (self at:i) asTitlecase. |
|
3789 |
] ifFalse:[ |
|
3790 |
c := (self at:i) asLowercase. |
|
3791 |
]. |
|
3792 |
c bitsPerCharacter > bitsPerCharacter ifTrue:[ |
|
3793 |
newStr := c stringSpecies fromString:newStr. |
|
3794 |
bitsPerCharacter := newStr bitsPerCharacter. |
|
3795 |
]. |
|
3796 |
newStr at:i put:c |
|
8027 | 3797 |
]. |
3798 |
^ newStr |
|
3799 |
||
3800 |
" |
|
8874 | 3801 |
'helloWorld' asTitlecase |
3802 |
'HelloWorld' asTitlecase |
|
3803 |
'HELLOWORLD' asTitlecase |
|
3804 |
'helloworld' asTitlecase |
|
8027 | 3805 |
" |
9192 | 3806 |
! |
3807 |
||
3808 |
asTitlecaseFirst |
|
8023 | 3809 |
"return a version of the receiver, where the first character is converted to titlecase. |
8024 | 3810 |
Titlecase is much like uppercase for most characters, with the exception of some combined |
3811 |
(2-character glyphs), which consist of an upper- and lower-case characters. |
|
3812 |
If the first character is already titlecase, or there is no titlecasepercase for it, return the |
|
8023 | 3813 |
receiver." |
3814 |
||
8026 | 3815 |
" |
8874 | 3816 |
For example, in Unicode, character U+01F3 is LATIN SMALL LETTER DZ. |
3817 |
(Let us write this compound character using ASCII as 'dz'.) |
|
3818 |
This character uppercases to character U+01F1, LATIN CAPITAL LETTER DZ. |
|
3819 |
(Which is basically 'DZ'.) |
|
3820 |
But it titlecases to to character U+01F2, LATIN CAPITAL LETTER D WITH SMALL LETTER Z. |
|
8026 | 3821 |
(Which we can write 'Dz'.) |
3822 |
||
3823 |
character uppercase titlecase |
|
3824 |
--------- --------- --------- |
|
3825 |
dz DZ Dz |
|
3826 |
" |
|
3827 |
||
8023 | 3828 |
|newString firstChar firstCharAsTitlecase| |
3829 |
||
3830 |
firstChar := (self at:1). |
|
8025 | 3831 |
firstCharAsTitlecase := firstChar asTitlecase. |
8023 | 3832 |
firstChar == firstCharAsTitlecase ifTrue:[ ^ self]. |
8037
8cce25b1059c
care for wide chars in asXXXFirst
Claus Gittinger <cg@exept.de>
parents:
8032
diff
changeset
|
3833 |
|
8cce25b1059c
care for wide chars in asXXXFirst
Claus Gittinger <cg@exept.de>
parents:
8032
diff
changeset
|
3834 |
firstCharAsTitlecase bitsPerCharacter > self bitsPerCharacter ifTrue:[ |
20874 | 3835 |
newString := firstCharAsTitlecase stringSpecies fromString:self. |
8037
8cce25b1059c
care for wide chars in asXXXFirst
Claus Gittinger <cg@exept.de>
parents:
8032
diff
changeset
|
3836 |
] ifFalse:[ |
20874 | 3837 |
newString := self stringSpecies fromString:self. |
8037
8cce25b1059c
care for wide chars in asXXXFirst
Claus Gittinger <cg@exept.de>
parents:
8032
diff
changeset
|
3838 |
]. |
8023 | 3839 |
newString at:1 put:firstCharAsTitlecase. |
3840 |
^ newString |
|
3841 |
||
3842 |
" |
|
8874 | 3843 |
'helloWorld' asTitlecaseFirst |
3844 |
'HelloWorld' asTitlecaseFirst |
|
8023 | 3845 |
" |
9192 | 3846 |
! |
3847 |
||
3848 |
asTwoByteString |
|
3849 |
"return the receiver converted to a two-byte string. |
|
16051 | 3850 |
Will be obsolete soon - use asUnicode16String." |
9192 | 3851 |
|
3852 |
^ TwoByteString fromString:self |
|
3853 |
! |
|
3854 |
||
3855 |
asURI |
|
3856 |
"return an URI with string taken from the receiver" |
|
3857 |
||
3858 |
^ URI fromString:self |
|
3859 |
! |
|
8710 | 3860 |
|
5776 | 3861 |
asURL |
3862 |
"return an URL-object from myself." |
|
3863 |
||
3864 |
^ URL fromString:self |
|
3865 |
||
3866 |
" |
|
8874 | 3867 |
'http://www.exept.de:80/index.html' asURL host |
3868 |
'http://www.exept.de:80/index.html' asURL port |
|
9192 | 3869 |
'http://www.exept.de:80/index.html' asURL method |
3870 |
'http://www.exept.de:80/index.html' asURL path |
|
3871 |
" |
|
3872 |
! |
|
3873 |
||
18901 | 3874 |
asUUID |
3875 |
"return self as a UUID" |
|
3876 |
||
3877 |
^ UUID fromString:self |
|
3878 |
||
3879 |
" |
|
3880 |
UUID genUUID asString asUUID |
|
3881 |
'{EAB22AC0-30C1-11CF-A7EB-0000C05BAE0B}' asUUID |
|
3882 |
'{EAB22AC0-30C1-11CF-A7EB-0000C05BAE0B}' asUnicodeString asUUID |
|
3883 |
'EAB22AC0-30C1-11CF-A7EB-0000C05BAE0B' asUUID |
|
3884 |
'EAB22AC0-30C1-11CF-A7EB-0000C05BAE0B quatsch' asUUID |
|
3885 |
'quark EAB22AC0-30C1-11CF-A7EB-0000C05BAE0B' asUUID |
|
3886 |
" |
|
3887 |
||
3888 |
"Modified: / 02-08-2007 / 16:43:29 / cg" |
|
3889 |
! |
|
3890 |
||
9192 | 3891 |
asUnicode16String |
15035 | 3892 |
"return the receiver in a two-byte per character representation. |
3893 |
Normally, the internal ST/X representation should be transparent and not |
|
3894 |
of the programmer's concern; except when strings are passed to the outside world, |
|
3895 |
such as wide-string ffi calls or file contents." |
|
3896 |
||
17600 | 3897 |
|sz| |
3898 |
||
3899 |
sz := self size. |
|
3900 |
||
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
3901 |
^ (Unicode16String new:sz) |
20874 | 3902 |
replaceFrom:1 to:sz with:self startingAt:1; |
3903 |
yourself. |
|
3904 |
||
3905 |
" |
|
3906 |
'abc' asUnicode16String |
|
17600 | 3907 |
" |
3908 |
! |
|
3909 |
||
3910 |
asUnicode16StringZ |
|
3911 |
"return the receiver in a two-byte per character representation. |
|
17625 | 3912 |
Make sure that is zero terminated (last char is 16r0000). |
17600 | 3913 |
Normally, the internal ST/X representation should be transparent and not |
3914 |
of the programmer's concern; except when strings are passed to the outside world, |
|
3915 |
such as wide-string ffi calls or file contents." |
|
3916 |
||
3917 |
|sz| |
|
3918 |
||
3919 |
sz := self size. |
|
3920 |
(self at:sz) == (Character codePoint:0) ifTrue:[ |
|
20874 | 3921 |
^ self asUnicode16String. |
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
3922 |
]. |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
3923 |
|
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
3924 |
^ (Unicode16String new:sz+1) |
20874 | 3925 |
replaceFrom:1 to:sz with:self startingAt:1; |
3926 |
at:sz+1 put:(Character codePoint:0); |
|
3927 |
yourself. |
|
17600 | 3928 |
|
3929 |
" |
|
20874 | 3930 |
'abc' asUnicode16StringZ |
3931 |
'abc' asUnicode16String asUnicode16StringZ |
|
17600 | 3932 |
" |
9192 | 3933 |
! |
8295
8433d819226b
Define #asUnicode16String and #asUnicode32String in CharacterArray
Stefan Vogel <sv@exept.de>
parents:
8222
diff
changeset
|
3934 |
|
8433d819226b
Define #asUnicode16String and #asUnicode32String in CharacterArray
Stefan Vogel <sv@exept.de>
parents:
8222
diff
changeset
|
3935 |
asUnicode32String |
16051 | 3936 |
"return the receiver in a four-byte per character representation. |
3937 |
Normally, the internal ST/X representation should be transparent and not |
|
3938 |
of the programmer's concern; except when strings are passed to the outside world, |
|
3939 |
such as wide-string ffi calls or file contents." |
|
3940 |
||
3941 |
^ self as:Unicode32String |
|
8295
8433d819226b
Define #asUnicode16String and #asUnicode32String in CharacterArray
Stefan Vogel <sv@exept.de>
parents:
8222
diff
changeset
|
3942 |
! |
8433d819226b
Define #asUnicode16String and #asUnicode32String in CharacterArray
Stefan Vogel <sv@exept.de>
parents:
8222
diff
changeset
|
3943 |
|
8433d819226b
Define #asUnicode16String and #asUnicode32String in CharacterArray
Stefan Vogel <sv@exept.de>
parents:
8222
diff
changeset
|
3944 |
asUnicodeString |
16051 | 3945 |
"return the receiver in a two-byte per character representation. |
3946 |
Normally, the internal ST/X representation should be transparent and not |
|
3947 |
of the programmer's concern; except when strings are passed to the outside world, |
|
3948 |
such as wide-string ffi calls or file contents." |
|
3949 |
||
3950 |
^ self asUnicode16String |
|
3951 |
" |
|
3952 |
'Hello World' asUnicodeString |
|
14141
e86d245213b0
comment/format in: #asUnicodeString
Stefan Vogel <sv@exept.de>
parents:
14137
diff
changeset
|
3953 |
" |
8295
8433d819226b
Define #asUnicode16String and #asUnicode32String in CharacterArray
Stefan Vogel <sv@exept.de>
parents:
8222
diff
changeset
|
3954 |
! |
8433d819226b
Define #asUnicode16String and #asUnicode32String in CharacterArray
Stefan Vogel <sv@exept.de>
parents:
8222
diff
changeset
|
3955 |
|
13073 | 3956 |
asUnixFilename |
3957 |
"return a UnixFilename with pathname taken from the receiver" |
|
3958 |
||
3959 |
^ UnixFilename named:self |
|
3960 |
||
3961 |
"Created: / 20-09-2010 / 21:42:55 / cg" |
|
3962 |
! |
|
3963 |
||
608 | 3964 |
asUppercase |
3965 |
"return a copy of myself in uppercase letters" |
|
3966 |
||
8003
6e690f4b79f1
asLowercase/asUppercase and friends care for 16-bit chars
Claus Gittinger <cg@exept.de>
parents:
7995
diff
changeset
|
3967 |
|newStr c bitsPerCharacter |
608 | 3968 |
mySize "{ Class: SmallInteger }" | |
3969 |
||
3970 |
mySize := self size. |
|
8954
12e701d7b5ac
allow for receiver to be empty in asLowercase and asUppercase.
Claus Gittinger <cg@exept.de>
parents:
8938
diff
changeset
|
3971 |
mySize == 0 ifTrue:[^ self]. |
608 | 3972 |
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
|
3973 |
bitsPerCharacter := newStr bitsPerCharacter. |
6e690f4b79f1
asLowercase/asUppercase and friends care for 16-bit chars
Claus Gittinger <cg@exept.de>
parents:
7995
diff
changeset
|
3974 |
|
18657 | 3975 |
"/ handle the very seldom case of a lowercase char which needs |
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
3976 |
"/ more bits in its uppercase variant |
18657 | 3977 |
"/ (there are only a few of them) |
3978 |
||
608 | 3979 |
1 to:mySize do:[:i | |
20874 | 3980 |
c := (self at:i) asUppercase. |
3981 |
c bitsPerCharacter > bitsPerCharacter ifTrue:[ |
|
3982 |
newStr := c stringSpecies fromString:newStr. |
|
3983 |
bitsPerCharacter := newStr bitsPerCharacter. |
|
3984 |
]. |
|
3985 |
newStr at:i put:c |
|
608 | 3986 |
]. |
3987 |
^ newStr |
|
3988 |
||
3989 |
" |
|
8874 | 3990 |
'helloWorld' asUppercase |
3991 |
'helloWorld' asUppercaseFirst |
|
9192 | 3992 |
(Character value:16rB5) asString asUppercase -- needs 16 bits !! |
18657 | 3993 |
(Character value:16rFF) asString asUppercase -- needs 16 bits !! |
9192 | 3994 |
" |
3995 |
! |
|
3996 |
||
3997 |
asUppercaseFirst |
|
8003
6e690f4b79f1
asLowercase/asUppercase and friends care for 16-bit chars
Claus Gittinger <cg@exept.de>
parents:
7995
diff
changeset
|
3998 |
"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
|
3999 |
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
|
4000 |
receiver." |
6e690f4b79f1
asLowercase/asUppercase and friends care for 16-bit chars
Claus Gittinger <cg@exept.de>
parents:
7995
diff
changeset
|
4001 |
|
6e690f4b79f1
asLowercase/asUppercase and friends care for 16-bit chars
Claus Gittinger <cg@exept.de>
parents:
7995
diff
changeset
|
4002 |
|newString firstChar firstCharAsUppercase| |
6e690f4b79f1
asLowercase/asUppercase and friends care for 16-bit chars
Claus Gittinger <cg@exept.de>
parents:
7995
diff
changeset
|
4003 |
|
12679 | 4004 |
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
|
4005 |
firstChar := self at:1. |
8003
6e690f4b79f1
asLowercase/asUppercase and friends care for 16-bit chars
Claus Gittinger <cg@exept.de>
parents:
7995
diff
changeset
|
4006 |
firstCharAsUppercase := firstChar asUppercase. |
6e690f4b79f1
asLowercase/asUppercase and friends care for 16-bit chars
Claus Gittinger <cg@exept.de>
parents:
7995
diff
changeset
|
4007 |
firstChar == firstCharAsUppercase ifTrue:[ ^ self]. |
6e690f4b79f1
asLowercase/asUppercase and friends care for 16-bit chars
Claus Gittinger <cg@exept.de>
parents:
7995
diff
changeset
|
4008 |
|
12679 | 4009 |
"/ 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
|
4010 |
firstCharAsUppercase bitsPerCharacter > self bitsPerCharacter ifTrue:[ |
20874 | 4011 |
newString := firstCharAsUppercase stringSpecies fromString:self. |
8037
8cce25b1059c
care for wide chars in asXXXFirst
Claus Gittinger <cg@exept.de>
parents:
8032
diff
changeset
|
4012 |
] ifFalse:[ |
20874 | 4013 |
newString := self stringSpecies fromString:self. |
8037
8cce25b1059c
care for wide chars in asXXXFirst
Claus Gittinger <cg@exept.de>
parents:
8032
diff
changeset
|
4014 |
]. |
8003
6e690f4b79f1
asLowercase/asUppercase and friends care for 16-bit chars
Claus Gittinger <cg@exept.de>
parents:
7995
diff
changeset
|
4015 |
newString at:1 put:firstCharAsUppercase. |
608 | 4016 |
^ newString |
4017 |
||
4018 |
" |
|
8874 | 4019 |
'helloWorld' asUppercase |
4020 |
'helloWorld' asUppercaseFirst |
|
4021 |
'HelloWorld' asUppercaseFirst |
|
608 | 4022 |
" |
9192 | 4023 |
! |
4024 |
||
11828 | 4025 |
asUppercaseLast |
4026 |
"return a copy of myself where the last character is |
|
4027 |
converted to uppercase." |
|
4028 |
||
4029 |
|newString sz| |
|
4030 |
||
4031 |
sz := self size. |
|
4032 |
newString := self copyFrom:1 to:sz. |
|
4033 |
sz > 0 ifTrue:[ |
|
20874 | 4034 |
newString at:sz put:(newString at:sz) asUppercase |
11828 | 4035 |
]. |
4036 |
^ newString |
|
4037 |
||
4038 |
" |
|
12177 | 4039 |
'HelloWorld' asUppercase |
4040 |
'HelloWorld' asUppercaseLast |
|
11828 | 4041 |
" |
4042 |
! |
|
4043 |
||
17021 | 4044 |
asUtcTimestamp |
4045 |
"convert the receiver into an UtcTimestamp (Time is interpreted in the UTC timezone). |
|
4046 |
Notice, that errors may occur during the read, |
|
4047 |
so you better setup some exception handler when using this method." |
|
4048 |
||
21030 | 4049 |
^ UtcTimestamp readFrom:self |
17021 | 4050 |
|
4051 |
" |
|
4052 |
'2014-11-10 21:30:22.444' asUtcTimestamp |
|
4053 |
" |
|
4054 |
! |
|
4055 |
||
11062 | 4056 |
literalArrayEncoding |
4057 |
"encode myself as an array literal, from which a copy of the receiver |
|
4058 |
can be reconstructed with #decodeAsLiteralArray." |
|
4059 |
||
4060 |
^ self |
|
4061 |
||
4062 |
"Modified: 1.9.1995 / 02:25:45 / claus" |
|
4063 |
"Modified: 22.4.1996 / 13:00:50 / cg" |
|
4064 |
! |
|
4065 |
||
9192 | 4066 |
string |
4067 |
"return the receiver - for ST-80 compatibility" |
|
4068 |
||
4069 |
^ self |
|
4070 |
||
4071 |
"Modified: 27.4.1996 / 13:29:30 / cg" |
|
4072 |
! ! |
|
4073 |
||
4074 |
!CharacterArray methodsFor:'copying'! |
|
4075 |
||
4076 |
, aStringOrCharacter |
|
2720
98c21d9a07c0
allow mixed strings to be concatenated using comma
Claus Gittinger <cg@exept.de>
parents:
2692
diff
changeset
|
4077 |
"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
|
4078 |
This is nonStandard, but convenient" |
2f14bbd792a9
, now accepts a character-argument
Claus Gittinger <cg@exept.de>
parents:
1028
diff
changeset
|
4079 |
|
2720
98c21d9a07c0
allow mixed strings to be concatenated using comma
Claus Gittinger <cg@exept.de>
parents:
2692
diff
changeset
|
4080 |
|myWidth otherWidth| |
98c21d9a07c0
allow mixed strings to be concatenated using comma
Claus Gittinger <cg@exept.de>
parents:
2692
diff
changeset
|
4081 |
|
1050
2f14bbd792a9
, now accepts a character-argument
Claus Gittinger <cg@exept.de>
parents:
1028
diff
changeset
|
4082 |
aStringOrCharacter isCharacter ifTrue:[ |
20874 | 4083 |
^ self copyWith:aStringOrCharacter |
1050
2f14bbd792a9
, now accepts a character-argument
Claus Gittinger <cg@exept.de>
parents:
1028
diff
changeset
|
4084 |
]. |
1396
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4085 |
aStringOrCharacter isText ifTrue:[ |
20874 | 4086 |
^ aStringOrCharacter concatenateFromString:self |
1396
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4087 |
]. |
2720
98c21d9a07c0
allow mixed strings to be concatenated using comma
Claus Gittinger <cg@exept.de>
parents:
2692
diff
changeset
|
4088 |
aStringOrCharacter isString ifTrue:[ |
20874 | 4089 |
(otherWidth := aStringOrCharacter bitsPerCharacter) ~~ (myWidth := self bitsPerCharacter) ifTrue:[ |
4090 |
otherWidth > myWidth ifTrue:[ |
|
4091 |
^ (aStringOrCharacter species fromString:self) , aStringOrCharacter |
|
4092 |
]. |
|
4093 |
^ self , (self species fromString:aStringOrCharacter) |
|
4094 |
]. |
|
2720
98c21d9a07c0
allow mixed strings to be concatenated using comma
Claus Gittinger <cg@exept.de>
parents:
2692
diff
changeset
|
4095 |
]. |
1050
2f14bbd792a9
, now accepts a character-argument
Claus Gittinger <cg@exept.de>
parents:
1028
diff
changeset
|
4096 |
^ super , aStringOrCharacter |
2f14bbd792a9
, now accepts a character-argument
Claus Gittinger <cg@exept.de>
parents:
1028
diff
changeset
|
4097 |
|
2f14bbd792a9
, now accepts a character-argument
Claus Gittinger <cg@exept.de>
parents:
1028
diff
changeset
|
4098 |
" |
8874 | 4099 |
'hello' , $1 |
4100 |
'hello' , '1' |
|
4101 |
'hello' , (' world' asText allBold) |
|
4102 |
'hello' , (JISEncodedString fromString:' world') |
|
2720
98c21d9a07c0
allow mixed strings to be concatenated using comma
Claus Gittinger <cg@exept.de>
parents:
2692
diff
changeset
|
4103 |
(JISEncodedString fromString:'hello') , ' world' |
1411 | 4104 |
|
1422 | 4105 |
Transcript showCR: |
20874 | 4106 |
(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
|
4107 |
" |
2f14bbd792a9
, now accepts a character-argument
Claus Gittinger <cg@exept.de>
parents:
1028
diff
changeset
|
4108 |
|
2720
98c21d9a07c0
allow mixed strings to be concatenated using comma
Claus Gittinger <cg@exept.de>
parents:
2692
diff
changeset
|
4109 |
"Modified: 28.6.1997 / 00:13:17 / cg" |
9192 | 4110 |
! |
4111 |
||
4112 |
,, aString |
|
7020 | 4113 |
"concatenate with a newLine in between" |
6965 | 4114 |
|
4115 |
^ (self copyWith:Character cr) , aString |
|
4116 |
||
4117 |
" |
|
4118 |
hello ,, world |
|
7020 | 4119 |
'hello' ,, 'world' |
6965 | 4120 |
" |
4121 |
! |
|
4122 |
||
11329 | 4123 |
chopTo: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 |
in the middle have been removed for a total string length |
|
4127 |
of maxLen." |
|
4128 |
||
4129 |
|sz n1 n2| |
|
4130 |
||
4131 |
(sz := self size) <= maxLen ifTrue:[ ^ self ]. |
|
4132 |
||
4133 |
n1 := n2 := maxLen // 2. |
|
4134 |
maxLen odd ifTrue:[ |
|
20874 | 4135 |
n2 := n1 + 1 |
11329 | 4136 |
]. |
4137 |
^ (self copyFrom:1 to:n1) , (self copyFrom:sz - n2 + 1) |
|
4138 |
||
4139 |
" |
|
4140 |
'12345678901234' chopTo:15 |
|
4141 |
'123456789012345' chopTo:15 |
|
4142 |
'1234567890123456' chopTo:15 |
|
4143 |
'aShortString' chopTo:15 |
|
4144 |
'aVeryLongNameForAStringThatShouldBeShortened' chopTo:15 |
|
4145 |
" |
|
4146 |
||
4147 |
"Modified: / 24-10-2006 / 12:32:01 / cg" |
|
4148 |
! |
|
4149 |
||
4150 |
contractAtBeginningTo:maxLen |
|
19464 | 4151 |
"if the receiver's size is less or equal to maxLen, return it. |
11329 | 4152 |
Otherwise, return a copy of the receiver, where some characters |
4153 |
at the beginning have been replaced by '...' for a total string length |
|
4154 |
of maxLen. Can be used to abbreviate long entries in tables." |
|
4155 |
||
4156 |
|sz| |
|
4157 |
||
4158 |
(sz := self size) <= maxLen ifTrue:[ ^ self ]. |
|
4159 |
||
4160 |
^ '...' , (self copyFrom:(sz - (maxLen - 4))) |
|
4161 |
||
4162 |
" |
|
4163 |
'12345678901234' contractAtBeginningTo:15 |
|
4164 |
'123456789012345' contractAtBeginningTo:15 |
|
4165 |
'1234567890123456' contractAtBeginningTo:15 |
|
4166 |
'aShortString' contractAtBeginningTo:15 |
|
4167 |
'aVeryLongNameForAStringThatShouldBeShortened' contractAtBeginningTo:15 |
|
4168 |
" |
|
4169 |
||
4170 |
"Modified: / 24-10-2006 / 12:32:13 / cg" |
|
4171 |
! |
|
4172 |
||
4173 |
contractAtEndTo:maxLen |
|
19464 | 4174 |
"if the receiver's size is less or equal to maxLen, return it. |
11329 | 4175 |
Otherwise, return a copy of the receiver, where some characters |
4176 |
at the end have been replaced by '...' for a total string length |
|
4177 |
of maxLen. Can be used to abbreviate long entries in tables." |
|
4178 |
||
4179 |
|sz| |
|
4180 |
||
4181 |
(sz := self size) <= maxLen ifTrue:[ ^ self ]. |
|
4182 |
||
4183 |
^ (self copyTo:maxLen-3),'...' |
|
4184 |
||
4185 |
" |
|
4186 |
'12345678901234' contractAtEndTo:15 |
|
4187 |
'123456789012345' contractAtEndTo:15 |
|
4188 |
'1234567890123456' contractAtEndTo:15 |
|
4189 |
'aShortString' contractAtEndTo:15 |
|
4190 |
'aVeryLongNameForAStringThatShouldBeShortened' contractAtEndTo:15 |
|
4191 |
" |
|
4192 |
||
4193 |
"Modified: / 24-10-2006 / 12:32:26 / cg" |
|
4194 |
! |
|
4195 |
||
4196 |
contractLeftTo:maxLen |
|
19464 | 4197 |
"if the receiver's size is less or equal to maxLen, return it. |
11329 | 4198 |
Otherwise, return a copy of the receiver, where some characters |
4199 |
near the first quarter have been replaced by '...' for a total string length |
|
12177 | 4200 |
of maxLen. |
4201 |
Very similar to contractTo:, but better to abbreviate long filename entries, |
|
11329 | 4202 |
where the right part is of more use than the left." |
4203 |
||
4204 |
|sz "{ SmallInteger }" |
|
4205 |
halfSize quarterSize "{ SmallInteger }" |
|
4206 |
leftEnd rightEnd rightStart| |
|
4207 |
||
4208 |
(sz := self size) <= maxLen ifTrue:[ ^ self ]. |
|
4209 |
||
4210 |
halfSize := maxLen // 2. |
|
4211 |
quarterSize := maxLen // 4. |
|
4212 |
leftEnd := quarterSize-1. |
|
4213 |
rightEnd := maxLen - leftEnd - 3. |
|
4214 |
rightStart := sz - rightEnd + 1. |
|
4215 |
^ (self copyTo:leftEnd),'...',(self copyFrom:rightStart) |
|
4216 |
||
4217 |
" |
|
12177 | 4218 |
'12345678901234' contractLeftTo:15 |
4219 |
'123456789012345' contractLeftTo:15 |
|
4220 |
'1234567890123456' contractLeftTo:15 |
|
4221 |
'aShortString' contractLeftTo:15 |
|
4222 |
'aVeryLongNameForAStringThatShouldBeShortened' contractLeftTo:15 |
|
11329 | 4223 |
'C:\Dokumente und Einstellungen\cg\work\bosch\dapas\hw_schnittstellen\DAPAS__HpibDLL.st' contractLeftTo:40 |
4224 |
" |
|
4225 |
! |
|
4226 |
||
4227 |
contractTo:maxLen |
|
19464 | 4228 |
"if the receiver's size is less or equal to maxLen, return it. |
11329 | 4229 |
Otherwise, return a copy of the receiver, where some characters |
4230 |
in the middle have been replaced by '...' for a total string length |
|
4231 |
of maxLen. Can be used to abbreviate long entries in tables." |
|
4232 |
||
13362 | 4233 |
|sz "{ SmallInteger }" leftSize rightSize| |
11329 | 4234 |
|
4235 |
(sz := self size) <= maxLen ifTrue:[ ^ self ]. |
|
4236 |
||
13362 | 4237 |
rightSize := maxLen // 2. |
4238 |
leftSize := maxLen - rightSize. |
|
13822 | 4239 |
leftSize := leftSize - 2. |
4240 |
rightSize := rightSize - 1. |
|
13362 | 4241 |
^ (self copyTo:leftSize),'...',(self copyFrom:(sz+1-rightSize)) |
4242 |
||
4243 |
" |
|
13882
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
4244 |
'12345678901234' contractTo:15 |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
4245 |
'123456789012345' contractTo:15 |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
4246 |
'1234567890123456' contractTo:15 |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
4247 |
'12345678901234567' contractTo:15 |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
4248 |
'123456789012345678' contractTo:15 |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
4249 |
'aShortString' contractTo:15 |
11329 | 4250 |
'aVeryLongNameForAStringThatShouldBeShortened' contractTo:15 |
4251 |
'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
|
4252 |
('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
|
4253 |
('1234567890123456789012345678901234567' contractTo:29) size |
13362 | 4254 |
" |
4255 |
||
13822 | 4256 |
"Modified (comment): / 24-11-2011 / 19:17:46 / cg" |
11329 | 4257 |
! |
4258 |
||
16202 | 4259 |
copyReplaceAll:oldElement with:newElement |
4260 |
"return a copy of the receiver as a string, where all elements equal to oldElement |
|
4261 |
have been replaced by newElement." |
|
4262 |
||
4263 |
"/ ANSI seems to allow a sequence to be replaced by another sequence, |
|
4264 |
"/ whereas the old ST80 meant replace all occurrences... - sigh. |
|
4265 |
oldElement isByteCollection ifTrue:[ |
|
20874 | 4266 |
newElement isByteCollection ifTrue:[ |
4267 |
^ self copyReplaceString:oldElement withString:newElement. |
|
4268 |
]. |
|
4269 |
self halt:'check if this is legal'. |
|
16202 | 4270 |
]. |
4271 |
newElement isByteCollection ifTrue:[ |
|
20874 | 4272 |
self halt:'check if this is legal'. |
16202 | 4273 |
]. |
16212 | 4274 |
^ super copyReplaceAll:oldElement with:newElement |
16202 | 4275 |
! |
4276 |
||
8347 | 4277 |
copyReplaceString:subString withString:newString |
4278 |
"return a copy of the receiver, with all sequences of subString replaced |
|
4279 |
by newString (i.e. slice in the newString in place of the oldString)." |
|
4280 |
||
4281 |
|tmpStream idx idx1| |
|
4282 |
||
15523 | 4283 |
tmpStream := self species writeStream. |
8347 | 4284 |
idx := 1. |
4285 |
[idx ~~ 0] whileTrue:[ |
|
20874 | 4286 |
idx1 := idx. |
4287 |
idx := self indexOfSubCollection:subString startingAt:idx. |
|
4288 |
idx ~~ 0 ifTrue:[ |
|
4289 |
tmpStream nextPutAll:(self copyFrom:idx1 to:idx-1). |
|
4290 |
tmpStream nextPutAll:newString. |
|
4291 |
idx := idx + subString size |
|
4292 |
] |
|
8347 | 4293 |
]. |
4294 |
tmpStream nextPutAll:(self copyFrom:idx1). |
|
4295 |
^ tmpStream contents |
|
4296 |
||
4297 |
" |
|
9306 | 4298 |
'12345678901234567890' copyReplaceString:'123' withString:'OneTwoThree' |
4299 |
'12345678901234567890' copyReplaceString:'123' withString:'*' |
|
4300 |
'12345678901234567890' copyReplaceString:'234' withString:'foo' |
|
4301 |
||
4302 |
('a string with spaces' copyReplaceAll:$ withAll:' foo ') |
|
20874 | 4303 |
copyReplaceString:'foo' withString:'bar' |
8347 | 4304 |
" |
4305 |
||
4306 |
"Modified: / 31-05-1999 / 12:33:59 / cg" |
|
4307 |
"Created: / 12-05-2004 / 12:00:00 / cg" |
|
4308 |
! |
|
4309 |
||
8040
f10cca439066
copyWith for mixed-width string/character
Claus Gittinger <cg@exept.de>
parents:
8037
diff
changeset
|
4310 |
copyWith:aCharacter |
19464 | 4311 |
"return a new string containing the receiver's characters |
8874 | 4312 |
and the single new character, aCharacter. |
8040
f10cca439066
copyWith for mixed-width string/character
Claus Gittinger <cg@exept.de>
parents:
8037
diff
changeset
|
4313 |
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
|
4314 |
as argument, but equivalent to copy-and-addLast. |
8874 | 4315 |
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
|
4316 |
(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
|
4317 |
|
f10cca439066
copyWith for mixed-width string/character
Claus Gittinger <cg@exept.de>
parents:
8037
diff
changeset
|
4318 |
|sz newString| |
f10cca439066
copyWith for mixed-width string/character
Claus Gittinger <cg@exept.de>
parents:
8037
diff
changeset
|
4319 |
|
f10cca439066
copyWith for mixed-width string/character
Claus Gittinger <cg@exept.de>
parents:
8037
diff
changeset
|
4320 |
aCharacter bitsPerCharacter > self bitsPerCharacter ifTrue:[ |
20874 | 4321 |
sz := self size. |
4322 |
newString := aCharacter stringSpecies new:sz + 1. |
|
4323 |
newString replaceFrom:1 to:sz with:self startingAt:1. |
|
4324 |
newString at:sz+1 put:aCharacter. |
|
4325 |
^ newString. |
|
8040
f10cca439066
copyWith for mixed-width string/character
Claus Gittinger <cg@exept.de>
parents:
8037
diff
changeset
|
4326 |
]. |
f10cca439066
copyWith for mixed-width string/character
Claus Gittinger <cg@exept.de>
parents:
8037
diff
changeset
|
4327 |
^ super copyWith:aCharacter |
17241 | 4328 |
! |
4329 |
||
4330 |
restAfter:keyword withoutSeparators:strip |
|
4331 |
"compare the left of the receiver with keyword, |
|
4332 |
if it matches return the right. |
|
4333 |
Finally, if strip is true, remove whiteSpace. |
|
4334 |
This method is used to match and extract lines of the form: |
|
20874 | 4335 |
something: rest |
17241 | 4336 |
where we are interested in rest, but only if the receiver string |
4337 |
begins with something. |
|
4338 |
||
4339 |
You may wonder why such a specialized method exists here |
|
4340 |
- this is so common when processing mailboxes, |
|
4341 |
rcs files, nntp/pop3 responses, that is was considered worth |
|
4342 |
a special method here to avoid having the code below a hundred |
|
4343 |
times in variuos places." |
|
4344 |
||
4345 |
|rest| |
|
4346 |
||
4347 |
(self startsWith:keyword) ifTrue:[ |
|
20874 | 4348 |
rest := self copyFrom:(keyword size + 1). |
4349 |
strip ifTrue:[ |
|
4350 |
rest := rest withoutSeparators |
|
4351 |
]. |
|
4352 |
^ rest |
|
17241 | 4353 |
]. |
4354 |
^ nil |
|
4355 |
||
4356 |
" |
|
4357 |
'foo: hello world' restAfter:'foo:' withoutSeparators:true |
|
4358 |
'funny: something' restAfter:'foo:' withoutSeparators:true |
|
4359 |
||
4360 |
'foo: hello world ' restAfter:'foo:' withoutSeparators:true |
|
4361 |
'foo: hello world ' restAfter:'foo:' withoutSeparators:false |
|
4362 |
" |
|
4363 |
||
4364 |
"Created: 25.11.1995 / 11:04:18 / cg" |
|
4365 |
! |
|
4366 |
||
4367 |
splitAtString:subString withoutSeparators:strip |
|
4368 |
"If the receiver is of the form: |
|
20874 | 4369 |
<left><subString><right> |
17241 | 4370 |
return a collection containing left and right only. |
4371 |
If strip is true, remove whiteSpace in the returned substrings." |
|
4372 |
||
4373 |
|idx left right| |
|
4374 |
||
4375 |
(idx := self indexOfSubCollection:subString) ~~ 0 ifTrue:[ |
|
20874 | 4376 |
left := self copyTo:(idx - 1). |
4377 |
right := self copyFrom:(idx + subString size). |
|
4378 |
strip ifTrue:[ |
|
4379 |
left := left withoutSeparators. |
|
4380 |
right := right withoutSeparators. |
|
4381 |
]. |
|
4382 |
^ StringCollection with:left with:right |
|
17241 | 4383 |
]. |
4384 |
self error:'substring not present in receiver' mayProceed:true. |
|
4385 |
^ self |
|
4386 |
||
4387 |
" |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
4388 |
'hello -> world' splitAtString:'->' withoutSeparators:false |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
4389 |
'hello -> world' splitAtString:'->' withoutSeparators:true |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
4390 |
'hello -> ' splitAtString:'->' withoutSeparators:true |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
4391 |
'hello > error' splitAtString:'->' withoutSeparators:true |
17241 | 4392 |
" |
4393 |
||
4394 |
"Created: 25.11.1995 / 11:04:18 / cg" |
|
608 | 4395 |
! ! |
4396 |
||
4397 |
!CharacterArray methodsFor:'displaying'! |
|
4398 |
||
2762 | 4399 |
displayOn:aGC x:x y:y from:start to:stop |
4400 |
"display the receiver on a GC" |
|
4401 |
||
12171 | 4402 |
self displayOn:aGC x:x y:y from:start to:stop opaque:false |
4403 |
||
4404 |
"Modified: 12.5.1996 / 12:49:33 / cg" |
|
4405 |
! |
|
4406 |
||
4407 |
displayOn:aGC x:x y:y from:start to:stop opaque:opaque |
|
4408 |
"display the receiver on a GC" |
|
4409 |
||
2762 | 4410 |
"q&d hack" |
4411 |
||
12177 | 4412 |
(start == 1 and:[stop == self size]) ifTrue:[ |
20874 | 4413 |
self displayOn:aGC x:x y:y opaque:opaque. |
12177 | 4414 |
] ifFalse:[ |
20874 | 4415 |
(self copyFrom:start to:stop) displayOn:aGC x:x y:y opaque:opaque. |
12177 | 4416 |
]. |
2762 | 4417 |
! |
4418 |
||
7082 | 4419 |
displayOn:aGc x:x y:y opaque:opaque |
2762 | 4420 |
"display the receiver in a graphicsContext - this method allows |
4421 |
strings to be used like DisplayObjects." |
|
4422 |
||
4423 |
|s| |
|
4424 |
||
4425 |
s := self string. |
|
7082 | 4426 |
opaque ifTrue:[ |
20874 | 4427 |
aGc displayOpaqueString:s x:x y:y. |
2762 | 4428 |
] ifFalse:[ |
20874 | 4429 |
aGc displayString:s x:x y:y. |
2762 | 4430 |
]. |
1381 | 4431 |
|
4432 |
"Modified: 11.5.1996 / 14:42:48 / cg" |
|
4433 |
! |
|
4434 |
||
2762 | 4435 |
displayOpaqueOn:aGC x:x y:y from:start to:stop |
4436 |
"display the receiver on a GC" |
|
4437 |
||
12171 | 4438 |
self displayOn:aGC x:x y:y from:start to:stop opaque:true |
608 | 4439 |
! ! |
4440 |
||
1375
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
4441 |
!CharacterArray methodsFor:'emphasis'! |
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
4442 |
|
11632
b82e9f3cabd1
new: #actionForAll: #emphasisAtPoint:on:
Stefan Vogel <sv@exept.de>
parents:
11630
diff
changeset
|
4443 |
actionForAll:aBlock |
12326
c67f12213ddb
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
12319
diff
changeset
|
4444 |
"change the action block of all characters. |
c67f12213ddb
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
12319
diff
changeset
|
4445 |
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
|
4446 |
|
b82e9f3cabd1
new: #actionForAll: #emphasisAtPoint:on:
Stefan Vogel <sv@exept.de>
parents:
11630
diff
changeset
|
4447 |
^ self asText actionForAll:aBlock |
b82e9f3cabd1
new: #actionForAll: #emphasisAtPoint:on:
Stefan Vogel <sv@exept.de>
parents:
11630
diff
changeset
|
4448 |
! |
b82e9f3cabd1
new: #actionForAll: #emphasisAtPoint:on:
Stefan Vogel <sv@exept.de>
parents:
11630
diff
changeset
|
4449 |
|
5732 | 4450 |
allBold |
12326
c67f12213ddb
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
12319
diff
changeset
|
4451 |
"return a text object representing the receiver, but all boldified" |
5732 | 4452 |
|
8874 | 4453 |
"this test allows for small non-gui apps to be built without libbasic2 (where Text is)" |
6187 | 4454 |
Text isNil ifTrue:[^ self]. |
5732 | 4455 |
^ self asText allBold |
4456 |
||
4457 |
" |
|
4458 |
Transcript showCR:'hello' asText allBold |
|
4459 |
Transcript showCR:'hello' allBold |
|
4460 |
" |
|
4461 |
! |
|
4462 |
||
5898
7bc4f1593b4e
allItalic now understood by CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5848
diff
changeset
|
4463 |
allItalic |
12326
c67f12213ddb
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
12319
diff
changeset
|
4464 |
"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
|
4465 |
|
8874 | 4466 |
"this test allows for small non-gui apps to be built without libbasic2 (where Text is)" |
6187 | 4467 |
Text isNil ifTrue:[^ self]. |
5898
7bc4f1593b4e
allItalic now understood by CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5848
diff
changeset
|
4468 |
^ self asText allItalic |
7bc4f1593b4e
allItalic now understood by CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5848
diff
changeset
|
4469 |
|
7bc4f1593b4e
allItalic now understood by CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5848
diff
changeset
|
4470 |
" |
7bc4f1593b4e
allItalic now understood by CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5848
diff
changeset
|
4471 |
Transcript showCR:'hello' asText allItalic |
7bc4f1593b4e
allItalic now understood by CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5848
diff
changeset
|
4472 |
Transcript showCR:'hello' allItalic |
7bc4f1593b4e
allItalic now understood by CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5848
diff
changeset
|
4473 |
" |
7bc4f1593b4e
allItalic now understood by CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5848
diff
changeset
|
4474 |
! |
7bc4f1593b4e
allItalic now understood by CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5848
diff
changeset
|
4475 |
|
11345 | 4476 |
allStrikedOut |
12326
c67f12213ddb
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
12319
diff
changeset
|
4477 |
"return a text object representing the receiver, but all in strikeout" |
11345 | 4478 |
|
4479 |
"this test allows for small non-gui apps to be built without libbasic2 (where Text is)" |
|
4480 |
Text isNil ifTrue:[^ self]. |
|
4481 |
^ self asText allStrikedOut |
|
4482 |
||
4483 |
" |
|
4484 |
Transcript showCR:'hello' asText allStrikedOut |
|
4485 |
Transcript showCR:'hello' allStrikedOut |
|
4486 |
" |
|
4487 |
! |
|
4488 |
||
10862 | 4489 |
allUnderlined |
12326
c67f12213ddb
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
12319
diff
changeset
|
4490 |
"return a text object representing the receiver, but all with underline" |
10862 | 4491 |
|
4492 |
"this test allows for small non-gui apps to be built without libbasic2 (where Text is)" |
|
4493 |
Text isNil ifTrue:[^ self]. |
|
4494 |
^ self asText allUnderlined |
|
4495 |
||
4496 |
" |
|
4497 |
Transcript showCR:'hello' asText allUnderlined |
|
4498 |
Transcript showCR:'hello' allUnderlined |
|
4499 |
" |
|
4500 |
! |
|
4501 |
||
19774 | 4502 |
asActionLinkTo:aBlock |
4503 |
"change the action block of all characters and colorize as a link. |
|
4504 |
Some widgets use this like a href if clicked onto the text |
|
4505 |
(for example, the system-browser's info at the bottom is such a widget)." |
|
4506 |
||
4507 |
"/ the blue may become a user-setting some time later.. |
|
4508 |
^ (self actionForAll:aBlock) withColor:(Color blue) |
|
4509 |
! |
|
4510 |
||
6228 | 4511 |
colorizeAllWith:aColor |
12326
c67f12213ddb
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
12319
diff
changeset
|
4512 |
"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
|
4513 |
|
c67f12213ddb
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
12319
diff
changeset
|
4514 |
"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
|
4515 |
Text isNil ifTrue:[^ self]. |
6228 | 4516 |
^ self asText colorizeAllWith:aColor |
4517 |
||
4518 |
" |
|
4519 |
Transcript showCR:('hello' colorizeAllWith:Color red) |
|
4520 |
Transcript showCR:('world' colorizeAllWith:Color green darkened) |
|
4521 |
" |
|
4522 |
! |
|
4523 |
||
12330
f9b57b0b8a8b
added: #colorizeAllWith:on:
Claus Gittinger <cg@exept.de>
parents:
12326
diff
changeset
|
4524 |
colorizeAllWith:fgColor on:bgColor |
f9b57b0b8a8b
added: #colorizeAllWith:on:
Claus Gittinger <cg@exept.de>
parents:
12326
diff
changeset
|
4525 |
"return a text object representing the receiver, but all colorized with |
f9b57b0b8a8b
added: #colorizeAllWith:on:
Claus Gittinger <cg@exept.de>
parents:
12326
diff
changeset
|
4526 |
both fg and background color" |
f9b57b0b8a8b
added: #colorizeAllWith:on:
Claus Gittinger <cg@exept.de>
parents:
12326
diff
changeset
|
4527 |
|
f9b57b0b8a8b
added: #colorizeAllWith:on:
Claus Gittinger <cg@exept.de>
parents:
12326
diff
changeset
|
4528 |
"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
|
4529 |
Text isNil ifTrue:[^ self]. |
f9b57b0b8a8b
added: #colorizeAllWith:on:
Claus Gittinger <cg@exept.de>
parents:
12326
diff
changeset
|
4530 |
^ self asText colorizeAllWith:fgColor on:bgColor |
f9b57b0b8a8b
added: #colorizeAllWith:on:
Claus Gittinger <cg@exept.de>
parents:
12326
diff
changeset
|
4531 |
|
f9b57b0b8a8b
added: #colorizeAllWith:on:
Claus Gittinger <cg@exept.de>
parents:
12326
diff
changeset
|
4532 |
" |
f9b57b0b8a8b
added: #colorizeAllWith:on:
Claus Gittinger <cg@exept.de>
parents:
12326
diff
changeset
|
4533 |
Transcript showCR:('hello' colorizeAllWith:Color red on:Color yellow) |
f9b57b0b8a8b
added: #colorizeAllWith:on:
Claus Gittinger <cg@exept.de>
parents:
12326
diff
changeset
|
4534 |
Transcript showCR:('world' colorizeAllWith:Color red) |
f9b57b0b8a8b
added: #colorizeAllWith:on:
Claus Gittinger <cg@exept.de>
parents:
12326
diff
changeset
|
4535 |
" |
f9b57b0b8a8b
added: #colorizeAllWith:on:
Claus Gittinger <cg@exept.de>
parents:
12326
diff
changeset
|
4536 |
! |
f9b57b0b8a8b
added: #colorizeAllWith:on:
Claus Gittinger <cg@exept.de>
parents:
12326
diff
changeset
|
4537 |
|
1396
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4538 |
emphasis |
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4539 |
"return the emphasis. |
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4540 |
Since characterArrays do not hold any emphasis information, |
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4541 |
nil (no emphasis) is returned here." |
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4542 |
|
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4543 |
^ RunArray new:self size withAll:nil |
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4544 |
|
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4545 |
"Created: 14.5.1996 / 13:58:58 / cg" |
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4546 |
! |
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4547 |
|
6655 | 4548 |
emphasis:emphasisCollection |
4549 |
^ self asText emphasis:emphasisCollection |
|
4550 |
||
4551 |
" |
|
4552 |
Transcript showCR:('hello' emphasis:#(bold bold bold bold bold)) |
|
4553 |
" |
|
4554 |
! |
|
4555 |
||
1375
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
4556 |
emphasisAt:characterIndex |
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
4557 |
"return the emphasis at some index. |
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
4558 |
Since characterArrays do not hold any emphasis information, |
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
4559 |
nil (no emphasis) is returned here." |
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
4560 |
|
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
4561 |
^ nil |
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
4562 |
|
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
4563 |
"Created: 11.5.1996 / 14:13:27 / cg" |
1396
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4564 |
! |
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4565 |
|
11632
b82e9f3cabd1
new: #actionForAll: #emphasisAtPoint:on:
Stefan Vogel <sv@exept.de>
parents:
11630
diff
changeset
|
4566 |
emphasisAtPoint:aPoint on:aGC |
b82e9f3cabd1
new: #actionForAll: #emphasisAtPoint:on:
Stefan Vogel <sv@exept.de>
parents:
11630
diff
changeset
|
4567 |
"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
|
4568 |
|
b82e9f3cabd1
new: #actionForAll: #emphasisAtPoint:on:
Stefan Vogel <sv@exept.de>
parents:
11630
diff
changeset
|
4569 |
^ nil |
b82e9f3cabd1
new: #actionForAll: #emphasisAtPoint:on:
Stefan Vogel <sv@exept.de>
parents:
11630
diff
changeset
|
4570 |
! |
b82e9f3cabd1
new: #actionForAll: #emphasisAtPoint:on:
Stefan Vogel <sv@exept.de>
parents:
11630
diff
changeset
|
4571 |
|
1396
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4572 |
emphasisCollection |
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4573 |
"return the emphasis. |
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4574 |
Since characterArrays do not hold any emphasis information, |
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4575 |
nil (no emphasis) is returned here." |
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4576 |
|
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4577 |
^ RunArray new:(self size) |
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4578 |
|
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4579 |
"Created: 14.5.1996 / 13:58:58 / cg" |
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4580 |
"Modified: 14.5.1996 / 15:02:29 / cg" |
3404 | 4581 |
! |
4582 |
||
11919 | 4583 |
emphasiseFrom:start to:stop with:newEmphasis |
4584 |
"set to the emphasis within some range. return the receiver" |
|
4585 |
||
4586 |
^ self asText emphasiseFrom:start to:stop with:newEmphasis |
|
4587 |
||
4588 |
" |
|
12177 | 4589 |
'hello' emphasiseFrom:2 with:#italic |
11919 | 4590 |
|
4591 |
Transcript showCR:('hello' emphasiseFrom:2 with:#italic) |
|
4592 |
" |
|
4593 |
! |
|
4594 |
||
4595 |
emphasiseFrom:start with:newEmphasis |
|
4596 |
"set to the emphasis within some range. return the receiver" |
|
4597 |
||
4598 |
^ self emphasiseFrom:start to:(self size) with:newEmphasis |
|
4599 |
||
4600 |
" |
|
12177 | 4601 |
'hello' emphasiseFrom:2 with:#italic |
11919 | 4602 |
|
4603 |
Transcript showCR:('hello' emphasiseFrom:2 with:#italic) |
|
4604 |
" |
|
4605 |
! |
|
4606 |
||
3404 | 4607 |
emphasizeAllWith:emphasis |
12326
c67f12213ddb
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
12319
diff
changeset
|
4608 |
"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
|
4609 |
|
c67f12213ddb
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
12319
diff
changeset
|
4610 |
"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
|
4611 |
Text isNil ifTrue:[^ self]. |
3404 | 4612 |
^ self asText emphasizeAllWith:emphasis |
4613 |
||
4614 |
" |
|
4615 |
Transcript showCR:('hello' emphasizeAllWith:#bold) |
|
3599 | 4616 |
Transcript showCR:('hello' emphasizeAllWith:(#color -> Color red)) |
4617 |
Transcript showCR:('hello' emphasizeAllWith:(#color -> Color red)) |
|
4618 |
" |
|
4619 |
||
4620 |
"Modified: / 17.6.1998 / 12:51:44 / cg" |
|
5097 | 4621 |
! |
4622 |
||
4623 |
makeSelectorBoldIn:aClass |
|
14050 | 4624 |
"the receiver represents some source code for a method in aClass. |
5097 | 4625 |
Change myself to boldify the selector. |
14050 | 4626 |
Not yet implemented (could easily use the syntaxHighlighter for this ...). |
4627 |
For protocol compatibility with other smalltalks" |
|
5097 | 4628 |
|
4629 |
^ self |
|
4630 |
||
14050 | 4631 |
"Created: / 13-12-1999 / 21:49:24 / cg" |
4632 |
! |
|
4633 |
||
19762 | 4634 |
withColor:aColorOrColorSymbol |
20697 | 4635 |
"return a text object representing the receiver, but all colorized. |
4636 |
Usage of a colorSymbol is considered bad style (provided for backward compatibility); |
|
4637 |
please change to pass a proper color |
|
4638 |
(makes it easier to find color uses)" |
|
4639 |
||
4640 |
|color| |
|
4641 |
||
19762 | 4642 |
aColorOrColorSymbol isSymbol ifTrue:[ |
20697 | 4643 |
color := (Color perform:aColorOrColorSymbol) |
4644 |
] ifFalse:[ |
|
4645 |
color := aColorOrColorSymbol |
|
4646 |
]. |
|
4647 |
^ self colorizeAllWith:color |
|
19762 | 4648 |
|
4649 |
" |
|
4650 |
Transcript showCR:('hello' withColor:#red) |
|
20697 | 4651 |
Transcript showCR:('world' withColor:#blue) |
16697 | 4652 |
Transcript showCR:('hello' withColor:Color red) |
4653 |
Transcript showCR:('world' withColor:Color green darkened) |
|
4654 |
" |
|
4655 |
! |
|
4656 |
||
14050 | 4657 |
withoutAnyColorEmphasis |
4658 |
"for protocol compatibility with Text" |
|
4659 |
||
4660 |
^ self |
|
4661 |
||
4662 |
"Created: / 06-03-2012 / 18:15:38 / cg" |
|
6653 | 4663 |
! |
4664 |
||
12396 | 4665 |
withoutBackgroundColorEmphasis |
14050 | 4666 |
"for protocol compatibility with Text" |
4667 |
||
12396 | 4668 |
^ self |
14050 | 4669 |
|
4670 |
"Modified (comment): / 06-03-2012 / 18:14:27 / cg" |
|
12396 | 4671 |
! |
4672 |
||
6653 | 4673 |
withoutEmphasis:emphasisToRemove |
14050 | 4674 |
"for protocol compatibility with Text" |
4675 |
||
6653 | 4676 |
^ self |
14050 | 4677 |
|
4678 |
"Modified (comment): / 06-03-2012 / 18:14:29 / cg" |
|
12396 | 4679 |
! |
4680 |
||
4681 |
withoutForegroundColorEmphasis |
|
14050 | 4682 |
"for protocol compatibility with Text" |
4683 |
||
12396 | 4684 |
^ self |
14050 | 4685 |
|
4686 |
"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
|
4687 |
! ! |
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
4688 |
|
7265 | 4689 |
!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
|
4690 |
|
20013 | 4691 |
asDenormalizedUnicodeString |
4692 |
"return a new string containing the same characters, as a denormalized Unicode string. |
|
4693 |
This replaces diacritical chars (umlauts, accented chars etc) by |
|
4694 |
a sequence with combination characters. |
|
4695 |
(i.e. a plain character followed by a combining diacritical in the 0x03xx range)" |
|
4696 |
||
20014 | 4697 |
|map outStream mapChar| |
20013 | 4698 |
|
4699 |
map := self class unicodeDenormalizationMap. |
|
4700 |
||
4701 |
self containsNon7BitAscii ifFalse:[^ self]. "/ I cannot contain any |
|
4702 |
||
20014 | 4703 |
mapChar := |
4704 |
[:char | |
|
4705 |
|mappedChars| |
|
4706 |
||
4707 |
(mappedChars := map at:char ifAbsent:nil) notNil ifTrue:[ |
|
4708 |
mappedChars do:mapChar. |
|
4709 |
] ifFalse:[ |
|
4710 |
outStream nextPut:char. |
|
4711 |
]. |
|
4712 |
]. |
|
20013 | 4713 |
|
20014 | 4714 |
outStream := WriteStream on:(Unicode16String new:self size). |
4715 |
self do:mapChar. |
|
20013 | 4716 |
^ outStream contents asSingleByteStringIfPossible. |
4717 |
||
4718 |
" |
|
4719 |
'Ö' asDenormalizedUnicodeString |
|
4720 |
'aÖÄx' asDenormalizedUnicodeString |
|
4721 |
'abc' asDenormalizedUnicodeString |
|
20014 | 4722 |
'ṩ' asString asDenormalizedUnicodeString |
20013 | 4723 |
" |
4724 |
! |
|
4725 |
||
20011 | 4726 |
asNormalizedUnicodeString |
20010 | 4727 |
"return a new string containing the same characters, as a normalized Unicode string. |
4728 |
This replaces combination characters by corresponding single characters. |
|
20863 | 4729 |
(i.e. diaresis and other combining diacriticals in the 0x03xx range). |
4730 |
Caveat: |
|
4731 |
possibly incomplete: only COMBINING_DIACRITICAL_MARKS are cared for. |
|
4732 |
Does not care for COMBINING_DIACRITICAL_MARKS_EXTENDED |
|
20864 | 4733 |
and COMBINING_DIACRITICAL_MARKS_SUPPLEMENT. |
4734 |
However; those are used for German dialectology, ancient Greek and other similar |
|
4735 |
exotic uses. Probably noone will ever even notice that they are missing..." |
|
20010 | 4736 |
|
4737 |
|outStream prevChar map mapEntries mappedChar| |
|
4738 |
||
4739 |
map := self class unicodeNormalizationMap. |
|
20017 | 4740 |
|
20010 | 4741 |
self bitsPerCharacter <= 8 ifTrue:[^ self]. "/ I cannot contain any overtypes |
20017 | 4742 |
(self contains:[:ch | ch unicodeBlock == #'COMBINING_DIACRITICAL_MARKS']) ifFalse:[^ self]. "/ I do not contain any overtypes |
20010 | 4743 |
|
4744 |
outStream := WriteStream on:(self species new:self size). |
|
4745 |
self do:[:char | |
|
20017 | 4746 |
((char unicodeBlock == #'COMBINING_DIACRITICAL_MARKS') |
20010 | 4747 |
and:[ (mapEntries := map at:char ifAbsent:nil) notNil |
4748 |
and:[ (mappedChar := mapEntries at:prevChar ifAbsent:nil) notNil ]]) ifTrue:[ |
|
20014 | 4749 |
prevChar := mappedChar. |
20010 | 4750 |
] ifFalse:[ |
4751 |
prevChar notNil ifTrue:[ outStream nextPut:prevChar]. |
|
4752 |
prevChar := char |
|
4753 |
]. |
|
4754 |
]. |
|
20011 | 4755 |
prevChar notNil ifTrue:[ outStream nextPut:prevChar]. |
20010 | 4756 |
^ outStream contents asSingleByteStringIfPossible. |
4757 |
||
4758 |
" |
|
4759 |
self unicodeNormalizationMap |
|
20012 | 4760 |
('O' , (Character value:16r0308)) asNormalizedUnicodeString -> 'Ö' |
20010 | 4761 |
('O' , (Character value:16r0308) , |
20012 | 4762 |
'A' , (Character value:16r0308)) asNormalizedUnicodeString -> 'ÖÄ' |
20014 | 4763 |
('s' , (Character value:16r0323) , (Character value:16r0307)) asNormalizedUnicodeString -> 'ṩ' |
4764 |
('s' , (Character value:16r0307) , (Character value:16r0323)) asNormalizedUnicodeString -> 'ṩ' |
|
20010 | 4765 |
" |
4766 |
! |
|
4767 |
||
12767
17d2c7268065
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12679
diff
changeset
|
4768 |
decodeAsLiteralArray |
17d2c7268065
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12679
diff
changeset
|
4769 |
"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
|
4770 |
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
|
4771 |
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
|
4772 |
|
17d2c7268065
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12679
diff
changeset
|
4773 |
^ self |
17d2c7268065
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12679
diff
changeset
|
4774 |
! |
17d2c7268065
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12679
diff
changeset
|
4775 |
|
991
f0b45a4b82d2
first attempt in encoding/decoding for different character representations (mac/msdos etc.)
Claus Gittinger <cg@exept.de>
parents:
986
diff
changeset
|
4776 |
decodeFrom:encodingSymbol |
8874 | 4777 |
"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
|
4778 |
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
|
4779 |
|
ca774158b317
code cleanup (removed old obsolete encoding stuff)
Claus Gittinger <cg@exept.de>
parents:
8102
diff
changeset
|
4780 |
|myEncoding encoder| |
7950 | 4781 |
|
4782 |
encodingSymbol isNil ifTrue:[^ self]. |
|
4783 |
myEncoding := self encoding. |
|
4784 |
encodingSymbol == myEncoding ifTrue:[^ self]. |
|
4785 |
||
4786 |
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
|
4787 |
encoder isNil ifTrue:[^ self]. |
ca774158b317
code cleanup (removed old obsolete encoding stuff)
Claus Gittinger <cg@exept.de>
parents:
8102
diff
changeset
|
4788 |
^ 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
|
4789 |
! |
f0b45a4b82d2
first attempt in encoding/decoding for different character representations (mac/msdos etc.)
Claus Gittinger <cg@exept.de>
parents:
986
diff
changeset
|
4790 |
|
9192 | 4791 |
encodeFrom:oldEncoding into:newEncoding |
4792 |
^ CharacterEncoder encodeString:self from:oldEncoding into:newEncoding |
|
14405
1d0d99a95bba
comment/format in:7 methods
Claus Gittinger <cg@exept.de>
parents:
14168
diff
changeset
|
4793 |
|
1d0d99a95bba
comment/format in:7 methods
Claus Gittinger <cg@exept.de>
parents:
14168
diff
changeset
|
4794 |
" |
17600 | 4795 |
'äüö' encodeFrom:#iso8859 into:#utf8 |
4796 |
('äüö' encodeFrom:#iso8859 into:#utf8) encodeFrom:#utf8 into:#unicode |
|
14405
1d0d99a95bba
comment/format in:7 methods
Claus Gittinger <cg@exept.de>
parents:
14168
diff
changeset
|
4797 |
" |
9192 | 4798 |
! |
4799 |
||
4800 |
rot13 |
|
8874 | 4801 |
"Usenet: from `rotate alphabet 13 places'] |
6835
bdb2cb03200c
added utf8Decode with 8-bit chars only
Claus Gittinger <cg@exept.de>
parents:
6834
diff
changeset
|
4802 |
The simple Caesar-cypher encryption that replaces each English |
8874 | 4803 |
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
|
4804 |
so that 'The butler did it!!' becomes 'Gur ohgyre qvq vg!!' |
8874 | 4805 |
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
|
4806 |
It is used to enclose the text in a sealed wrapper that the reader must choose |
8874 | 4807 |
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
|
4808 |
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
|
4809 |
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
|
4810 |
|
11865 | 4811 |
^ self rot:13 |
8874 | 4812 |
|
4813 |
" |
|
4814 |
'hello world' rot13 |
|
4815 |
'hello world' rot13 rot13 |
|
6835
bdb2cb03200c
added utf8Decode with 8-bit chars only
Claus Gittinger <cg@exept.de>
parents:
6834
diff
changeset
|
4816 |
" |
bdb2cb03200c
added utf8Decode with 8-bit chars only
Claus Gittinger <cg@exept.de>
parents:
6834
diff
changeset
|
4817 |
! |
bdb2cb03200c
added utf8Decode with 8-bit chars only
Claus Gittinger <cg@exept.de>
parents:
6834
diff
changeset
|
4818 |
|
11865 | 4819 |
rot:n |
4820 |
"Usenet: from `rotate alphabet N places'] |
|
4821 |
The simple Caesar-cypher encryption that replaces each English |
|
4822 |
letter with the one N places forward or back along the alphabet, |
|
4823 |
so that 'The butler did it!!' becomes 'Gur ohgyre qvq vg!!' by rot 13 |
|
4824 |
Most Usenet news reading and posting programs include a rot13 feature. |
|
4825 |
It is used to enclose the text in a sealed wrapper that the reader must choose |
|
4826 |
to open -- e.g., for posting things that might offend some readers, or spoilers. |
|
4827 |
A major advantage of rot13 over rot(N) for other N is that it |
|
4828 |
is self-inverse, so the same code can be used for encoding and decoding." |
|
4829 |
||
4830 |
^ self species |
|
20874 | 4831 |
streamContents:[:aStream | |
4832 |
self do:[:char | |
|
4833 |
aStream nextPut:(char rot:n) ]] |
|
11865 | 4834 |
|
4835 |
" |
|
4836 |
'hello world' rot:13 |
|
4837 |
('hello world' rot:13) rot:13 |
|
4838 |
" |
|
4839 |
! |
|
4840 |
||
15656 | 4841 |
utf16Encoded |
4842 |
"Return my UTF-16 representation as a new TwoByteString" |
|
4843 |
||
4844 |
|s| |
|
4845 |
||
4846 |
s := WriteStream on:(TwoByteString uninitializedNew:self size). |
|
4847 |
s nextPutAllUtf16:self. |
|
4848 |
^ s contents |
|
4849 |
||
4850 |
" |
|
4851 |
'abcde1234' utf16Encoded |
|
17600 | 4852 |
'abcdeäöüß' utf16Encoded |
15656 | 4853 |
" |
4854 |
||
4855 |
"Modified: / 11-05-2010 / 19:12:37 / cg" |
|
4856 |
! |
|
4857 |
||
9192 | 4858 |
utf8Encoded |
19837 | 4859 |
"return the UTF-8 representation of a Unicode string. |
4860 |
The resulting string is only useful to be stored on some external file, |
|
4861 |
not for being used inside ST/X." |
|
4862 |
||
4863 |
|string stream| |
|
17625 | 4864 |
|
4865 |
string := self string. |
|
19837 | 4866 |
|
4867 |
"/ avoid creation of new strings if possible |
|
4868 |
string containsNon7BitAscii ifFalse:[ |
|
4869 |
^ string asSingleByteString |
|
4870 |
]. |
|
4871 |
||
4872 |
"make it size 2 for 1-byte strings" |
|
4873 |
stream := WriteStream on:(String uninitializedNew:(1 + (string size * 3 // 2))). |
|
4874 |
string utf8EncodedOn:stream. |
|
4875 |
^ stream contents. |
|
4876 |
||
4877 |
" |
|
4878 |
'hello' utf8Encoded asByteArray #[104 101 108 108 111] |
|
4879 |
(Character value:16r40) asString utf8Encoded asByteArray #[64] |
|
4880 |
(Character value:16r7F) asString utf8Encoded asByteArray #[127] |
|
4881 |
(Character value:16r80) asString utf8Encoded asByteArray #[194 128] |
|
4882 |
(Character value:16rFF) asString utf8Encoded asByteArray #[195 191] |
|
4883 |
(Character value:16r100) asString utf8Encoded asByteArray #[196 128] |
|
4884 |
(Character value:16r200) asString utf8Encoded asByteArray #[200 128] |
|
4885 |
(Character value:16r400) asString utf8Encoded asByteArray #[208 128] |
|
4886 |
(Character value:16r800) asString utf8Encoded asByteArray #[224 160 128] |
|
4887 |
(Character value:16r1000) asString utf8Encoded asByteArray #[225 128 128] |
|
4888 |
(Character value:16r2000) asString utf8Encoded asByteArray #[226 128 128] |
|
4889 |
(Character value:16r4000) asString utf8Encoded asByteArray #[228 128 128] |
|
4890 |
(Character value:16r8000) asString utf8Encoded asByteArray #[232 128 128] |
|
4891 |
(Character value:16rFFFF) asString utf8Encoded asByteArray #[239 191 191] |
|
4892 |
||
18597 | 4893 |
'abcde1234' asUnicode32String utf8Encoded |
4894 |
'abcdeäöüß' asUnicode32String utf8Encoded |
|
12925
5c0a9a633ca1
comment/format in: #utf8Encoded
Claus Gittinger <cg@exept.de>
parents:
12912
diff
changeset
|
4895 |
" |
17625 | 4896 |
! |
4897 |
||
4898 |
utf8EncodedOn:aStream |
|
19837 | 4899 |
"return the UTF-8 representation of a Unicode string. |
4900 |
The resulting string is only useful to be stored on some external file, |
|
4901 |
not for being used inside ST/X." |
|
4902 |
||
4903 |
|string |
|
4904 |
stringSize "{ Class: SmallInteger }" |
|
4905 |
codePoint "{Class: SmallInteger }" |
|
4906 |
v "{Class: SmallInteger }" |
|
4907 |
character b1 b2 b3 b4 b5| |
|
17625 | 4908 |
|
4909 |
string := self string. |
|
19837 | 4910 |
|
4911 |
"/ avoid creation of new strings if possible |
|
4912 |
string containsNon7BitAscii ifFalse:[ |
|
4913 |
aStream nextPutAll:string asSingleByteString. |
|
4914 |
^ self. |
|
4915 |
]. |
|
4916 |
||
4917 |
stringSize := string size. |
|
4918 |
||
4919 |
1 to:stringSize do:[:idx | |
|
4920 |
character := string at:idx. |
|
4921 |
codePoint := character codePoint. |
|
4922 |
codePoint < 16r80 ifTrue:[ |
|
4923 |
aStream nextPutByte:codePoint. |
|
4924 |
] ifFalse:[ |
|
4925 |
b1 := (codePoint bitAnd:16r3F) bitOr:2r10000000. |
|
4926 |
v := codePoint bitShift:-6. |
|
4927 |
v <= 16r1F ifTrue:[ |
|
4928 |
aStream nextPutByte:(v bitOr:2r11000000). |
|
4929 |
aStream nextPutByte:b1. |
|
4930 |
] ifFalse:[ |
|
4931 |
b2 := (v bitAnd:16r3F) bitOr:2r10000000. |
|
4932 |
v := v bitShift:-6. |
|
4933 |
v <= 16r0F ifTrue:[ |
|
4934 |
aStream nextPutByte:(v bitOr:2r11100000). |
|
4935 |
aStream nextPutByte:b2; nextPutByte:b1. |
|
4936 |
] ifFalse:[ |
|
4937 |
b3 := (v bitAnd:16r3F) bitOr:2r10000000. |
|
4938 |
v := v bitShift:-6. |
|
4939 |
v <= 16r07 ifTrue:[ |
|
4940 |
aStream nextPutByte:(v bitOr:2r11110000). |
|
4941 |
aStream nextPutByte:b3; nextPutByte:b2; nextPutByte:b1. |
|
4942 |
] ifFalse:[ |
|
4943 |
b4 := (v bitAnd:16r3F) bitOr:2r10000000. |
|
4944 |
v := v bitShift:-6. |
|
4945 |
v <= 16r03 ifTrue:[ |
|
4946 |
aStream nextPutByte:(v bitOr:2r11111000). |
|
4947 |
aStream nextPutByte:b4; nextPutByte:b3; nextPutByte:b2; nextPutByte:b1. |
|
4948 |
] ifFalse:[ |
|
4949 |
b5 := (v bitAnd:16r3F) bitOr:2r10000000. |
|
4950 |
v := v bitShift:-6. |
|
4951 |
v <= 16r01 ifTrue:[ |
|
4952 |
aStream nextPutByte:(v bitOr:2r11111100). |
|
4953 |
aStream nextPutByte:b5; nextPutByte:b4; nextPutByte:b3; nextPutByte:b2; nextPutByte:b1. |
|
4954 |
] ifFalse:[ |
|
4955 |
"/ cannot happen - we only support up to 30 bit characters |
|
4956 |
EncodingError raiseWith:character errorString:'codePoint > 31bit in #utf8Encode'. |
|
4957 |
] |
|
4958 |
]. |
|
4959 |
]. |
|
4960 |
]. |
|
4961 |
]. |
|
4962 |
]. |
|
4963 |
]. |
|
4964 |
||
4965 |
" |
|
4966 |
String streamContents:[:s| |
|
4967 |
'hallo' utf8EncodedOn:s |
|
4968 |
]. |
|
4969 |
||
4970 |
ByteArray streamContents:[:s| |
|
4971 |
'hallo' utf8EncodedOn:s |
|
4972 |
]. |
|
4973 |
||
4974 |
String streamContents:[:s| |
|
4975 |
'abcdeäöüß' asUnicode32String utf8EncodedOn:s |
|
4976 |
]. |
|
4977 |
. |
|
4978 |
ByteArray streamContents:[:s| |
|
4979 |
'abcdeäöüß' asUnicode32String utf8EncodedOn:s |
|
4980 |
]. |
|
4981 |
||
4982 |
'/tmp/bytes' asFilename writingFileDo:[:s| |
|
4983 |
'abcdeäöüß' utf8EncodedOn:s |
|
4984 |
]. |
|
17625 | 4985 |
" |
9192 | 4986 |
! ! |
4987 |
||
11345 | 4988 |
|
20873 | 4989 |
|
4990 |
||
12111 | 4991 |
!CharacterArray methodsFor:'matching - glob expressions'! |
4992 |
||
4993 |
compoundMatch:aString |
|
4994 |
"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
|
4995 |
consisting of multiple simple GLOB patterns, separated by semicolons. |
12111 | 4996 |
This is usable with fileName pattern fields. |
4997 |
||
4998 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20874 | 4999 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5000 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 5001 |
NOTICE: the receiver is the match pattern" |
5002 |
||
15890 | 5003 |
^ self compoundMatch:aString caseSensitive:true withoutSeparators:false |
12111 | 5004 |
|
5005 |
" |
|
5006 |
'f*' match:'foo' |
|
5007 |
'b*' match:'foo' |
|
5008 |
'f*;b*' match:'foo' |
|
5009 |
'f*;b*' match:'bar' |
|
5010 |
'f*;b*' compoundMatch:'foo' |
|
5011 |
'f*;b*' compoundMatch:'bar' |
|
5012 |
" |
|
5013 |
||
5014 |
"Modified: / 30.1.1998 / 11:40:18 / stefan" |
|
5015 |
"Modified: / 16.12.1999 / 01:22:08 / cg" |
|
5016 |
! |
|
5017 |
||
15734 | 5018 |
compoundMatch:aString caseSensitive:caseSensitive |
12111 | 5019 |
"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
|
5020 |
consisting of multiple simple GLOB patterns, separated by semicolons. |
12111 | 5021 |
This is usable with fileName pattern fields. |
5022 |
||
5023 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20874 | 5024 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5025 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 5026 |
NOTICE: the receiver is the match pattern" |
5027 |
||
15890 | 5028 |
^self compoundMatch:aString caseSensitive:caseSensitive withoutSeparators:false |
5029 |
||
5030 |
" |
|
5031 |
'f*' match:'foo' |
|
5032 |
'b*' match:'foo' |
|
5033 |
'f*;b*' match:'foo' |
|
5034 |
'f*;b*' match:'bar' |
|
5035 |
'f*;b*' compoundMatch:'foo' |
|
5036 |
'f*;b*' compoundMatch:'bar' |
|
5037 |
'f*;b*' compoundMatch:'Foo' caseSensitive:false |
|
5038 |
'f*;b*' compoundMatch:'Bar' caseSensitive:false |
|
5039 |
'f*;b*' compoundMatch:'ccc' caseSensitive:false |
|
5040 |
" |
|
5041 |
||
5042 |
"Modified: / 15.4.1997 / 15:50:33 / cg" |
|
5043 |
"Modified: / 30.1.1998 / 11:40:18 / stefan" |
|
5044 |
"Created: / 16.12.1999 / 01:21:35 / cg" |
|
5045 |
! |
|
5046 |
||
5047 |
compoundMatch:aString caseSensitive:caseSensitive withoutSeparators:withoutSeparators |
|
5048 |
"like match, but the receiver may be a compound match pattern, |
|
5049 |
consisting of multiple simple GLOB patterns, separated by semicolons. |
|
5050 |
If withoutSeparators is true, spaces around individual patterns are stripped off. |
|
5051 |
This is usable with fileName pattern fields. |
|
5052 |
||
5053 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20874 | 5054 |
NOT the ST-80 meaning. |
15890 | 5055 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
5056 |
NOTICE: the receiver is the match pattern" |
|
5057 |
||
12111 | 5058 |
|matchers| |
5059 |
||
5060 |
matchers := self asCollectionOfSubstringsSeparatedBy:$;. |
|
15890 | 5061 |
withoutSeparators ifTrue:[ matchers := matchers collect:[:each | each withoutSeparators] ]. |
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
5062 |
^ matchers |
20874 | 5063 |
contains:[:aPattern | |
5064 |
aPattern match:aString caseSensitive:caseSensitive escapeCharacter:nil |
|
5065 |
]. |
|
15734 | 5066 |
|
5067 |
" |
|
5068 |
'f*' match:'foo' |
|
5069 |
'b*' match:'foo' |
|
5070 |
'f*;b*' match:'foo' |
|
5071 |
'f*;b*' match:'bar' |
|
5072 |
'f*;b*' compoundMatch:'foo' |
|
5073 |
'f*;b*' compoundMatch:'bar' |
|
5074 |
'f*;b*' compoundMatch:'Foo' caseSensitive:false |
|
5075 |
'f*;b*' compoundMatch:'Bar' caseSensitive:false |
|
5076 |
'f*;b*' compoundMatch:'ccc' caseSensitive:false |
|
15890 | 5077 |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
5078 |
'f* ; b*' compoundMatch:'foo' |
15890 | 5079 |
'f* ; b*' compoundMatch:'foo' caseSensitive:true withoutSeparators:true |
15734 | 5080 |
" |
5081 |
||
5082 |
"Modified: / 15.4.1997 / 15:50:33 / cg" |
|
5083 |
"Modified: / 30.1.1998 / 11:40:18 / stefan" |
|
5084 |
"Created: / 16.12.1999 / 01:21:35 / cg" |
|
5085 |
! |
|
5086 |
||
5087 |
compoundMatch:aString ignoreCase:ignoreCase |
|
5088 |
<resource: #obsolete> |
|
5089 |
||
5090 |
"like match, but the receiver may be a compound match pattern, |
|
5091 |
consisting of multiple simple GLOB patterns, separated by semicolons. |
|
5092 |
This is usable with fileName pattern fields. |
|
5093 |
||
5094 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20874 | 5095 |
NOT the ST-80 meaning. |
15734 | 5096 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
5097 |
NOTICE: the receiver is the match pattern" |
|
5098 |
||
5099 |
^ self compoundMatch:aString caseSensitive:ignoreCase not |
|
12111 | 5100 |
|
5101 |
" |
|
5102 |
'f*' match:'foo' |
|
5103 |
'b*' match:'foo' |
|
5104 |
'f*;b*' match:'foo' |
|
5105 |
'f*;b*' match:'bar' |
|
5106 |
'f*;b*' compoundMatch:'foo' |
|
5107 |
'f*;b*' compoundMatch:'bar' |
|
5108 |
'f*;b*' compoundMatch:'Foo' ignoreCase:true |
|
5109 |
'f*;b*' compoundMatch:'Bar' ignoreCase:true |
|
5110 |
'f*;b*' compoundMatch:'ccc' ignoreCase:true |
|
5111 |
" |
|
5112 |
||
5113 |
"Modified: / 15.4.1997 / 15:50:33 / cg" |
|
5114 |
"Modified: / 30.1.1998 / 11:40:18 / stefan" |
|
5115 |
"Created: / 16.12.1999 / 01:21:35 / cg" |
|
5116 |
! |
|
5117 |
||
5118 |
findMatchString:matchString |
|
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5119 |
"like findString/indexOfSubCollection, but allowing GLOB match patterns. |
12111 | 5120 |
find matchstring; if found, return the index; |
5121 |
if not found, return 0. |
|
5122 |
||
5123 |
NOTICE: match-meta character interpretation is like in unix-matching, |
|
20874 | 5124 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5125 |
NOTICE: this GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 5126 |
NOTICE: the argument is the match pattern" |
5127 |
||
19033 | 5128 |
^ self findMatchString:matchString startingAt:1 caseSensitive:true ifAbsent:0 |
5129 |
||
5130 |
" |
|
5131 |
1234567890123 |
|
5132 |
'hello world bla foo baz' findMatchString:'b* ' |
|
5133 |
" |
|
12111 | 5134 |
! |
5135 |
||
5136 |
findMatchString:matchString startingAt:index |
|
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5137 |
"like findString, but allowing GLOB match patterns. |
12111 | 5138 |
find matchstring, starting at index. if found, return the index; |
5139 |
if not found, return 0. |
|
5140 |
||
5141 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20874 | 5142 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5143 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 5144 |
NOTICE: the argument is the match pattern" |
5145 |
||
19033 | 5146 |
^ self findMatchString:matchString startingAt:index caseSensitive:true ifAbsent:0 |
12111 | 5147 |
! |
5148 |
||
15734 | 5149 |
findMatchString:matchString startingAt:index caseSensitive:caseSensitive ifAbsent:exceptionBlock |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5150 |
"like findString, but allowing GLOB match patterns. |
12111 | 5151 |
find matchstring, starting at index. if found, return the index; |
5152 |
if not found, return the result of evaluating exceptionBlock. |
|
5153 |
This is a q&d hack - not very efficient. |
|
5154 |
||
5155 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20874 | 5156 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5157 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 5158 |
NOTICE: the argument is the match pattern" |
5159 |
||
5160 |
|firstChar firstSet |
|
5161 |
startIndex "{ Class: SmallInteger }" |
|
5162 |
matchSize "{ Class: SmallInteger }" |
|
5163 |
mySize "{ Class: SmallInteger }" |
|
5164 |
realMatchString lcChar ucChar| |
|
5165 |
||
5166 |
matchSize := matchString size. |
|
5167 |
matchSize == 0 ifTrue:[^ index]. "empty string matches" |
|
5168 |
||
5169 |
realMatchString := matchString. |
|
5170 |
(realMatchString endsWith:$*) ifFalse:[ |
|
20874 | 5171 |
realMatchString := realMatchString , '*'. |
5172 |
matchSize := matchSize + 1 |
|
12111 | 5173 |
]. |
5174 |
||
5175 |
mySize := self size. |
|
5176 |
firstChar := realMatchString at:1. |
|
5177 |
firstChar == self class matchEscapeCharacter ifTrue:[ |
|
20874 | 5178 |
firstChar := realMatchString at:2. |
12111 | 5179 |
]. |
5180 |
||
5181 |
firstChar asString includesMatchCharacters ifTrue:[ |
|
20874 | 5182 |
index to:mySize do:[:col | |
5183 |
(realMatchString match:self from:col to:mySize caseSensitive:caseSensitive) |
|
5184 |
ifTrue:[^ col] |
|
5185 |
]. |
|
5186 |
^ exceptionBlock value. |
|
12111 | 5187 |
]. |
5188 |
||
5189 |
lcChar := firstChar asLowercase. |
|
5190 |
ucChar := firstChar asUppercase. |
|
15734 | 5191 |
(caseSensitive not and:[ lcChar ~= ucChar ]) ifTrue:[ |
20874 | 5192 |
firstSet := Array with:ucChar with:lcChar. |
5193 |
startIndex := self indexOfAny:firstSet startingAt:index. |
|
12111 | 5194 |
] ifFalse:[ |
20874 | 5195 |
startIndex := self indexOf:firstChar startingAt:index. |
12111 | 5196 |
]. |
5197 |
[startIndex == 0] whileFalse:[ |
|
20874 | 5198 |
(realMatchString match:self from:startIndex to:mySize caseSensitive:caseSensitive) |
5199 |
ifTrue:[^ startIndex]. |
|
5200 |
||
5201 |
firstSet notNil ifTrue:[ |
|
5202 |
startIndex := self indexOfAny:firstSet startingAt:(startIndex + 1). |
|
5203 |
] ifFalse:[ |
|
5204 |
startIndex := self indexOf:firstChar startingAt:(startIndex + 1). |
|
5205 |
]. |
|
12111 | 5206 |
]. |
5207 |
^ exceptionBlock value |
|
5208 |
||
5209 |
" |
|
5210 |
'one two three four' findMatchString:'o[nu]' |
|
5211 |
'one two three four' findMatchString:'o[nu]' startingAt:3 |
|
15734 | 5212 |
'one two three four one' findMatchString:'ONE' startingAt:3 caseSensitive:false ifAbsent:0 |
5213 |
'one two three four one' findMatchString:'ONE' startingAt:3 caseSensitive:true ifAbsent:0 |
|
5214 |
'one two three four ONE' findMatchString:'O#E' startingAt:1 caseSensitive:false ifAbsent:0 |
|
5215 |
'one two three four ONE' findMatchString:'O#E' startingAt:1 caseSensitive:true ifAbsent:0 |
|
5216 |
12345678901234567890 |
|
5217 |
" |
|
5218 |
||
5219 |
"Modified: 13.9.1997 / 06:31:22 / cg" |
|
5220 |
! |
|
5221 |
||
5222 |
findMatchString:matchString startingAt:index ignoreCase:ignoreCase ifAbsent:exceptionBlock |
|
5223 |
<resource: #obsolete> |
|
5224 |
"like findString, but allowing GLOB match patterns. |
|
5225 |
find matchstring, starting at index. if found, return the index; |
|
5226 |
if not found, return the result of evaluating exceptionBlock. |
|
5227 |
This is a q&d hack - not very efficient. |
|
5228 |
||
5229 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20874 | 5230 |
NOT the ST-80 meaning. |
15734 | 5231 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
5232 |
NOTICE: the argument is the match pattern" |
|
5233 |
||
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
5234 |
^ self |
20874 | 5235 |
findMatchString:matchString startingAt:index caseSensitive:ignoreCase not ifAbsent:exceptionBlock |
15734 | 5236 |
|
5237 |
" |
|
5238 |
'one two three four' findMatchString:'o[nu]' |
|
5239 |
'one two three four' findMatchString:'o[nu]' startingAt:3 |
|
12111 | 5240 |
'one two three four one' findMatchString:'ONE' startingAt:3 ignoreCase:true ifAbsent:0 |
5241 |
" |
|
5242 |
||
5243 |
"Modified: 13.9.1997 / 06:31:22 / cg" |
|
5244 |
! |
|
5245 |
||
19746 | 5246 |
globPatternAsRegexPattern |
5247 |
"taking the receiver as a GLOB pattern, |
|
5248 |
return a corresponding regex pattern. |
|
5249 |
As regex does provide information about the matching substring, |
|
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
5250 |
it may be useful to apply a regex after a GLOB match, |
19746 | 5251 |
in order to highlight matching substrings (eg. in a CodeView after a search). |
5252 |
If it turns out to be better, we may convert all GLOB searches to regex and use it right away. |
|
5253 |
(currently, it is not sure, if GLOB is not better for most simple searches, as they are encountered |
|
5254 |
in typical real life)" |
|
5255 |
||
5256 |
^ self species streamContents:[:s | |
|
20874 | 5257 |
|addCharacter| |
5258 |
||
5259 |
addCharacter := |
|
5260 |
[:ch | |
|
5261 |
ch isLetterOrDigit ifFalse:[ |
|
5262 |
s nextPut:$\. |
|
5263 |
]. |
|
5264 |
s nextPut:ch |
|
5265 |
]. |
|
5266 |
||
5267 |
(String matchScanArrayFrom:self) do:[:matchEntry | |
|
5268 |
matchEntry isCharacter ifTrue:[ |
|
5269 |
addCharacter value:matchEntry |
|
5270 |
] ifFalse:[ |
|
5271 |
matchEntry == #anyString ifTrue:[ |
|
5272 |
s nextPutAll:'.*' |
|
5273 |
] ifFalse:[ |
|
5274 |
matchEntry == #any ifTrue:[ |
|
5275 |
s nextPut:$. |
|
5276 |
] ifFalse:[ |
|
5277 |
matchEntry isString ifTrue:[ |
|
5278 |
|set min max| |
|
5279 |
||
5280 |
s nextPut:$[. |
|
5281 |
set := matchEntry copy sort. |
|
5282 |
min := set min. |
|
5283 |
max := set max. |
|
5284 |
set asSet = (min to:max) asSet ifTrue:[ |
|
5285 |
addCharacter value:min. |
|
5286 |
s nextPut:$-. |
|
5287 |
addCharacter value:max. |
|
5288 |
] ifFalse:[ |
|
5289 |
set do:addCharacter. |
|
5290 |
]. |
|
5291 |
s nextPut:$]. |
|
5292 |
] ifFalse:[ |
|
5293 |
self halt. |
|
5294 |
]. |
|
5295 |
]. |
|
5296 |
]. |
|
5297 |
] |
|
5298 |
]. |
|
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
5299 |
]. |
19746 | 5300 |
|
5301 |
" |
|
5302 |
'hello' globPatternAsRegexPattern |
|
5303 |
'hello*' globPatternAsRegexPattern |
|
5304 |
'*hello*' globPatternAsRegexPattern |
|
5305 |
'h###' globPatternAsRegexPattern |
|
5306 |
'h[0-9]' globPatternAsRegexPattern |
|
5307 |
'h[0-9][0-9][0-9]' globPatternAsRegexPattern |
|
5308 |
'h[0-9]*' globPatternAsRegexPattern |
|
19747 | 5309 |
'h[-+]*' globPatternAsRegexPattern |
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
5310 |
|
19747 | 5311 |
'hello world' matches:'h*w' |
5312 |
'hello world' matchesRegex:('h*w' globPatternAsRegexPattern) |
|
5313 |
'hello world' matches:'h*d' |
|
5314 |
'hello world' matchesRegex:('h*d' globPatternAsRegexPattern) |
|
19746 | 5315 |
" |
5316 |
! |
|
5317 |
||
12111 | 5318 |
includesMatchString:matchString |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5319 |
"like includesString, but allowing GLOB match patterns. |
12111 | 5320 |
find matchstring; if found, return true, otherwise return false. |
5321 |
||
5322 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20874 | 5323 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5324 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 5325 |
NOTICE: the argument is the match pattern" |
5326 |
||
5327 |
^ (self findMatchString:matchString) ~~ 0 |
|
5328 |
||
5329 |
" |
|
5330 |
'hello world' includesMatchString:'h*' |
|
5331 |
'hello world' includesMatchString:'h[aeiou]llo' |
|
5332 |
'hello world' includesMatchString:'wor*' |
|
5333 |
'hello world' includesMatchString:'woR*' |
|
5334 |
" |
|
5335 |
! |
|
5336 |
||
5337 |
includesMatchString:matchString caseSensitive:caseSensitive |
|
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5338 |
"like includesString, but allowing GLOB match patterns. |
12111 | 5339 |
find matchstring; if found, return true, otherwise return false. |
5340 |
||
5341 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20874 | 5342 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5343 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 5344 |
NOTICE: the argument is the match pattern" |
5345 |
||
19033 | 5346 |
^ (self findMatchString:matchString startingAt:1 caseSensitive:caseSensitive ifAbsent:0) ~~ 0 |
12111 | 5347 |
|
5348 |
" |
|
5349 |
'hello world' includesMatchString:'h*' caseSensitive:true |
|
5350 |
'hello world' includesMatchString:'h*' caseSensitive:false |
|
5351 |
'Hello world' includesMatchString:'h*' caseSensitive:true |
|
5352 |
'Hello world' includesMatchString:'h*' caseSensitive:false |
|
5353 |
||
5354 |
'hello world' includesMatchString:'h[aeiou]llo' caseSensitive:true |
|
5355 |
'hello world' includesMatchString:'h[aeiou]llo' caseSensitive:false |
|
5356 |
||
5357 |
'hello world' includesMatchString:'wor*' caseSensitive:true |
|
5358 |
'hello world' includesMatchString:'wor*' caseSensitive:false |
|
5359 |
||
5360 |
'hello world' includesMatchString:'woR*' caseSensitive:true |
|
5361 |
'hello world' includesMatchString:'woR*' caseSensitive:false |
|
5362 |
" |
|
5363 |
! |
|
5364 |
||
5365 |
match:aString |
|
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5366 |
"return true if aString matches self, where self may contain GLOB meta-match |
12111 | 5367 |
characters $* (to match any string) or $# (to match any character). |
5368 |
or [...] to match a set of characters. |
|
5369 |
Lower/uppercase are considered different. |
|
5370 |
The escape character is the backQuote. |
|
5371 |
||
5372 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20874 | 5373 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5374 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 5375 |
NOTICE: the receiver is the match pattern" |
5376 |
||
15734 | 5377 |
^ self match:aString from:1 to:aString size caseSensitive:true |
12111 | 5378 |
|
5379 |
" |
|
5380 |
'\*f*' match:'f' |
|
5381 |
'\*f*' match:'*f' |
|
5382 |
'*\*f*' match:'*f' |
|
5383 |
'*f*' match:'*f' |
|
5384 |
'*ute*' match:'computer' |
|
5385 |
'*uter' match:'computer' |
|
5386 |
'uter*' match:'computer' |
|
5387 |
'*ute*' match:'' |
|
5388 |
'[abcd]*' match:'computer' |
|
5389 |
'[abcd]*' match:'komputer' |
|
5390 |
'*some*compl*ern*' match:'this is some more complicated pattern match' |
|
5391 |
'*some*compl*ern*' match:'this is another complicated pattern match' |
|
5392 |
'*-hh' match:'anton-h' |
|
5393 |
" |
|
5394 |
||
5395 |
"Modified: / 9.6.1998 / 18:50:00 / cg" |
|
5396 |
! |
|
5397 |
||
15734 | 5398 |
match:aString caseSensitive:caseSensitive |
5399 |
"return true if aString matches self, where self may contain GLOB meta-match |
|
5400 |
characters $* (to match any string) or $# (to match any character) |
|
5401 |
or [...] to match a set of characters. |
|
5402 |
If caseSensitive is false, lower/uppercase are considered the same. |
|
5403 |
The escape character is the backQuote. |
|
5404 |
||
5405 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20874 | 5406 |
NOT the ST-80 meaning. |
15734 | 5407 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
5408 |
NOTICE: the receiver is the match pattern" |
|
5409 |
||
5410 |
^ self match:aString from:1 to:aString size caseSensitive:caseSensitive |
|
5411 |
||
5412 |
" |
|
5413 |
'*ute*' match:'COMPUTER' caseSensitive:false |
|
5414 |
'*uter' match:'COMPUTER' caseSensitive:true |
|
5415 |
'[abcd]*' match:'computer' caseSensitive:true |
|
5416 |
'[abcd]*' match:'Computer' caseSensitive:true |
|
5417 |
'[a-k]*' match:'komputer' caseSensitive:true |
|
5418 |
'[a-k]*' match:'zomputer' caseSensitive:true |
|
5419 |
'[a-k]*' match:'Komputer' caseSensitive:true |
|
5420 |
'[a-k]*' match:'Komputer' caseSensitive:false |
|
5421 |
'*some*compl*ern*' match:'this is some more complicated pattern match' caseSensitive:false |
|
5422 |
'*some*compl*ern*' match:'this is another complicated pattern match' caseSensitive:false |
|
5423 |
||
5424 |
Time millisecondsToRun:[ |
|
20874 | 5425 |
Symbol allInstancesDo:[:sym | |
5426 |
'[ab]*' match:sym caseSensitive:true |
|
5427 |
] |
|
15734 | 5428 |
]. |
5429 |
Time millisecondsToRun:[ |
|
20874 | 5430 |
Symbol allInstancesDo:[:sym | |
5431 |
'*at:*' match:sym caseSensitive:true |
|
5432 |
] |
|
15734 | 5433 |
]. |
5434 |
" |
|
5435 |
||
5436 |
"Modified: 2.4.1997 / 17:28:58 / cg" |
|
5437 |
! |
|
5438 |
||
5439 |
match:aString caseSensitive:caseSensitive escapeCharacter:escape |
|
5440 |
"return true if aString matches self, where self may contain GLOB meta-match |
|
5441 |
characters $* (to match any string) or $# (to match any character) |
|
5442 |
or [...] to match a set of characters. |
|
5443 |
If caseSensitive is false, lower/uppercase are considered the same. |
|
5444 |
||
5445 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20874 | 5446 |
NOT the ST-80 meaning. |
15734 | 5447 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
5448 |
NOTICE: the receiver is the match pattern" |
|
5449 |
||
5450 |
^ self match:aString from:1 to:aString size caseSensitive:caseSensitive escapeCharacter:escape |
|
5451 |
||
5452 |
" |
|
5453 |
'*ute*' match:'COMPUTER' caseSensitive:false |
|
5454 |
'*uter' match:'COMPUTER' caseSensitive:true |
|
5455 |
'[abcd]*' match:'computer' caseSensitive:true |
|
5456 |
'[abcd]*' match:'Computer' caseSensitive:true |
|
5457 |
'[a-k]*' match:'komputer' caseSensitive:true |
|
5458 |
'[a-k]*' match:'zomputer' caseSensitive:true |
|
5459 |
'[a-k]*' match:'Komputer' caseSensitive:true |
|
5460 |
'[a-k]*' match:'Komputer' caseSensitive:false |
|
5461 |
'*some*compl*ern*' match:'this is some more complicated pattern match' caseSensitive:false |
|
5462 |
'*some*compl*ern*' match:'this is another complicated pattern match' caseSensitive:false |
|
5463 |
||
5464 |
Time millisecondsToRun:[ |
|
20874 | 5465 |
Symbol allInstancesDo:[:sym | |
5466 |
'[ab]*' match:sym caseSensitive:true |
|
5467 |
] |
|
15734 | 5468 |
]. |
5469 |
Time millisecondsToRun:[ |
|
20874 | 5470 |
Symbol allInstancesDo:[:sym | |
5471 |
'*at:*' match:sym caseSensitive:true |
|
5472 |
] |
|
15734 | 5473 |
]. |
5474 |
" |
|
5475 |
||
5476 |
"Modified: 2.4.1997 / 17:28:58 / cg" |
|
5477 |
! |
|
5478 |
||
12111 | 5479 |
match:aString escapeCharacter:escape |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5480 |
"return true if aString matches self, where self may contain GLOB meta-match |
12111 | 5481 |
characters $* (to match any string) or $# (to match any character). |
5482 |
or [...] to match a set of characters. |
|
5483 |
Lower/uppercase are considered different. |
|
5484 |
||
5485 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20874 | 5486 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5487 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 5488 |
NOTICE: the receiver is the match pattern" |
5489 |
||
19033 | 5490 |
^ self match:aString from:1 to:aString size caseSensitive:true escapeCharacter:escape |
12111 | 5491 |
|
5492 |
" |
|
5493 |
'a\b\c\*' match:'a\b\c\d' |
|
5494 |
'a\b\c\*' match:'a\b\c\d' escapeCharacter:nil |
|
5495 |
" |
|
5496 |
! |
|
5497 |
||
15734 | 5498 |
match:aString from:start to:stop caseSensitive:caseSensitive |
5499 |
"return true if part of aString matches myself, |
|
5500 |
where self may contain GLOB meta-match |
|
5501 |
characters $* (to match any string) or $# (to match any character) |
|
5502 |
or [...] to match a set of characters. |
|
5503 |
If caseSensitive is false, lower/uppercase are considered the same. |
|
5504 |
The escape character is the backQuote. |
|
5505 |
||
5506 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20874 | 5507 |
NOT the ST-80 meaning. |
15734 | 5508 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
5509 |
NOTICE: the receiver is the match pattern" |
|
5510 |
||
5511 |
^ self |
|
20874 | 5512 |
match:aString from:start to:stop caseSensitive:caseSensitive |
5513 |
escapeCharacter:(self class matchEscapeCharacter) |
|
15734 | 5514 |
|
5515 |
" |
|
5516 |
'*ute*' match:'12345COMPUTER' from:1 to:5 caseSensitive:false |
|
5517 |
'*ute*' match:'12345COMPUTER' from:6 to:13 caseSensitive:false |
|
5518 |
" |
|
5519 |
||
5520 |
"Modified: / 10.11.1998 / 21:43:46 / cg" |
|
5521 |
! |
|
5522 |
||
5523 |
match:aString from:start to:stop caseSensitive:caseSensitive escapeCharacter:escape |
|
5524 |
"return true if part of aString matches myself, |
|
5525 |
where self may contain GLOB meta-match |
|
5526 |
characters $* (to match any string) or $# (to match any character) |
|
5527 |
or [...] to match a set of characters. |
|
5528 |
If caseSensitive is false, lower/uppercase are considered the same. |
|
5529 |
||
5530 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20667 | 5531 |
NOT the ST-80 meaning. |
15734 | 5532 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
5533 |
NOTICE: the receiver is the match pattern" |
|
5534 |
||
5535 |
|matchScanArray| |
|
5536 |
||
5537 |
" |
|
5538 |
keep the matchScanArray from the most recent match - |
|
5539 |
avoids parsing the pattern over-and over if multiple searches |
|
5540 |
are done with the same pattern. |
|
5541 |
" |
|
20667 | 5542 |
|
5543 |
(PreviousMatches isNil |
|
5544 |
or:[(matchScanArray := PreviousMatches at: self ifAbsent:[nil]) isNil]) ifTrue:[ |
|
5545 |
matchScanArray := self class matchScanArrayFrom:self escapeCharacter:escape. |
|
5546 |
matchScanArray isNil ifTrue:[ |
|
5547 |
'CharacterArray [info]: invalid matchpattern:''' infoPrint. self infoPrint. ''' comparing for equality.' infoPrintCR. |
|
5548 |
^ self = aString |
|
15734 | 5549 |
"/ ^ false |
20667 | 5550 |
]. |
5551 |
PreviousMatches isNil ifTrue:[ |
|
5552 |
PreviousMatches := CacheDictionary new:10 |
|
5553 |
]. |
|
5554 |
PreviousMatches at:self put:matchScanArray. |
|
15734 | 5555 |
]. |
5556 |
||
5557 |
^ self class |
|
20667 | 5558 |
matchScan:matchScanArray |
5559 |
from:1 to:matchScanArray size |
|
5560 |
with:aString |
|
5561 |
from:start to:stop |
|
5562 |
caseSensitive:caseSensitive |
|
15734 | 5563 |
|
5564 |
" |
|
5565 |
'*ute*' match:'12345COMPUTER' from:1 to:5 caseSensitive:false |
|
5566 |
'*ute*' match:'12345COMPUTER' from:6 to:13 caseSensitive:false |
|
5567 |
" |
|
5568 |
||
5569 |
"Modified: / 10.11.1998 / 21:43:46 / cg" |
|
5570 |
! |
|
5571 |
||
12111 | 5572 |
match:aString from:start to:stop ignoreCase:ignoreCase |
15734 | 5573 |
<resource: #obsolete> |
12111 | 5574 |
"return true if part of aString matches myself, |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5575 |
where self may contain GLOB meta-match |
12111 | 5576 |
characters $* (to match any string) or $# (to match any character) |
5577 |
or [...] to match a set of characters. |
|
5578 |
If ignoreCase is true, lower/uppercase are considered the same. |
|
5579 |
The escape character is the backQuote. |
|
5580 |
||
5581 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20874 | 5582 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5583 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 5584 |
NOTICE: the receiver is the match pattern" |
5585 |
||
5586 |
^ self |
|
20874 | 5587 |
match:aString from:start to:stop caseSensitive:ignoreCase not |
5588 |
escapeCharacter:(self class matchEscapeCharacter) |
|
12111 | 5589 |
|
5590 |
" |
|
5591 |
'*ute*' match:'12345COMPUTER' from:1 to:5 ignoreCase:true |
|
5592 |
'*ute*' match:'12345COMPUTER' from:6 to:13 ignoreCase:true |
|
5593 |
" |
|
5594 |
||
5595 |
"Modified: / 10.11.1998 / 21:43:46 / cg" |
|
5596 |
! |
|
5597 |
||
5598 |
match:aString from:start to:stop ignoreCase:ignoreCase escapeCharacter:escape |
|
15734 | 5599 |
<resource: #obsolete> |
12111 | 5600 |
"return true if part of aString matches myself, |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5601 |
where self may contain GLOB meta-match |
12111 | 5602 |
characters $* (to match any string) or $# (to match any character) |
5603 |
or [...] to match a set of characters. |
|
5604 |
If ignoreCase is true, lower/uppercase are considered the same. |
|
5605 |
||
5606 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20874 | 5607 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5608 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 5609 |
NOTICE: the receiver is the match pattern" |
5610 |
||
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
5611 |
^ self |
20874 | 5612 |
match:aString from:start to:stop |
5613 |
caseSensitive:ignoreCase not |
|
5614 |
escapeCharacter:escape |
|
12111 | 5615 |
|
5616 |
" |
|
5617 |
'*ute*' match:'12345COMPUTER' from:1 to:5 ignoreCase:true |
|
5618 |
'*ute*' match:'12345COMPUTER' from:6 to:13 ignoreCase:true |
|
5619 |
" |
|
5620 |
||
5621 |
"Modified: / 10.11.1998 / 21:43:46 / cg" |
|
5622 |
! |
|
5623 |
||
5624 |
match:aString ignoreCase:ignoreCase |
|
15734 | 5625 |
<resource: #obsolete> |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5626 |
"return true if aString matches self, where self may contain GLOB meta-match |
12111 | 5627 |
characters $* (to match any string) or $# (to match any character) |
5628 |
or [...] to match a set of characters. |
|
5629 |
If ignoreCase is true, lower/uppercase are considered the same. |
|
5630 |
The escape character is the backQuote. |
|
5631 |
||
5632 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20874 | 5633 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5634 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 5635 |
NOTICE: the receiver is the match pattern" |
5636 |
||
15734 | 5637 |
"/ leave it this way, in case a customer has redefined it for performance (for a while) |
19033 | 5638 |
^ self match:aString from:1 to:aString size caseSensitive:ignoreCase not |
12111 | 5639 |
|
5640 |
" |
|
5641 |
'*ute*' match:'COMPUTER' ignoreCase:true |
|
5642 |
'*uter' match:'COMPUTER' ignoreCase:false |
|
5643 |
'[abcd]*' match:'computer' ignoreCase:false |
|
5644 |
'[abcd]*' match:'Computer' ignoreCase:false |
|
5645 |
'[a-k]*' match:'komputer' ignoreCase:false |
|
5646 |
'[a-k]*' match:'zomputer' ignoreCase:false |
|
5647 |
'[a-k]*' match:'Komputer' ignoreCase:false |
|
5648 |
'[a-k]*' match:'Komputer' ignoreCase:true |
|
5649 |
'*some*compl*ern*' match:'this is some more complicated pattern match' ignoreCase:true |
|
5650 |
'*some*compl*ern*' match:'this is another complicated pattern match' ignoreCase:true |
|
5651 |
||
5652 |
Time millisecondsToRun:[ |
|
20874 | 5653 |
Symbol allInstancesDo:[:sym | |
5654 |
'[ab]*' match:sym ignoreCase:false |
|
5655 |
] |
|
12111 | 5656 |
]. |
5657 |
Time millisecondsToRun:[ |
|
20874 | 5658 |
Symbol allInstancesDo:[:sym | |
5659 |
'*at:*' match:sym ignoreCase:false |
|
5660 |
] |
|
12111 | 5661 |
]. |
5662 |
" |
|
5663 |
||
5664 |
"Modified: 2.4.1997 / 17:28:58 / cg" |
|
5665 |
! |
|
5666 |
||
5667 |
match:aString ignoreCase:ignoreCase escapeCharacter:escape |
|
15734 | 5668 |
<resource: #obsolete> |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5669 |
"return true if aString matches self, where self may contain GLOB meta-match |
12111 | 5670 |
characters $* (to match any string) or $# (to match any character) |
5671 |
or [...] to match a set of characters. |
|
5672 |
If ignoreCase is true, lower/uppercase are considered the same. |
|
5673 |
||
5674 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20874 | 5675 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5676 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 5677 |
NOTICE: the receiver is the match pattern" |
5678 |
||
19033 | 5679 |
^ self match:aString from:1 to:aString size caseSensitive:ignoreCase not escapeCharacter:escape |
12111 | 5680 |
|
5681 |
" |
|
5682 |
'*ute*' match:'COMPUTER' ignoreCase:true |
|
5683 |
'*uter' match:'COMPUTER' ignoreCase:false |
|
5684 |
'[abcd]*' match:'computer' ignoreCase:false |
|
5685 |
'[abcd]*' match:'Computer' ignoreCase:false |
|
5686 |
'[a-k]*' match:'komputer' ignoreCase:false |
|
5687 |
'[a-k]*' match:'zomputer' ignoreCase:false |
|
5688 |
'[a-k]*' match:'Komputer' ignoreCase:false |
|
5689 |
'[a-k]*' match:'Komputer' ignoreCase:true |
|
5690 |
'*some*compl*ern*' match:'this is some more complicated pattern match' ignoreCase:true |
|
5691 |
'*some*compl*ern*' match:'this is another complicated pattern match' ignoreCase:true |
|
5692 |
||
5693 |
Time millisecondsToRun:[ |
|
20874 | 5694 |
Symbol allInstancesDo:[:sym | |
5695 |
'[ab]*' match:sym ignoreCase:false |
|
5696 |
] |
|
12111 | 5697 |
]. |
5698 |
Time millisecondsToRun:[ |
|
20874 | 5699 |
Symbol allInstancesDo:[:sym | |
5700 |
'*at:*' match:sym ignoreCase:false |
|
5701 |
] |
|
12111 | 5702 |
]. |
5703 |
" |
|
5704 |
||
5705 |
"Modified: 2.4.1997 / 17:28:58 / cg" |
|
5706 |
! |
|
5707 |
||
5708 |
matches:aPatternString |
|
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5709 |
"return true if the receiver matches aString, where aPatternString may contain GLOB meta-match |
12111 | 5710 |
characters $* (to match any string) or $# (to match any character). |
5711 |
or [...] to match a set of characters. |
|
5712 |
Lower/uppercase are considered different. |
|
5713 |
||
5714 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20874 | 5715 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5716 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 5717 |
NOTICE: the receiver is the match pattern" |
5718 |
||
5719 |
^ aPatternString match:self |
|
14054
48d3fab77bd9
added: #matches:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
14053
diff
changeset
|
5720 |
! |
48d3fab77bd9
added: #matches:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
14053
diff
changeset
|
5721 |
|
15734 | 5722 |
matches:aPatternString caseSensitive:caseSensitive |
5723 |
"return true if the receiver matches aString, where aPatternString may contain GLOB meta-match |
|
5724 |
characters $* (to match any string) or $# (to match any character). |
|
5725 |
or [...] to match a set of characters. |
|
5726 |
||
5727 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20874 | 5728 |
NOT the ST-80 meaning. |
15734 | 5729 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
5730 |
NOTICE: the receiver is the match pattern" |
|
5731 |
||
5732 |
^ aPatternString match:self caseSensitive:caseSensitive |
|
5733 |
! |
|
5734 |
||
14054
48d3fab77bd9
added: #matches:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
14053
diff
changeset
|
5735 |
matches:aPatternString ignoreCase:ignoreCase |
15734 | 5736 |
<resource: #obsolete> |
14054
48d3fab77bd9
added: #matches:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
14053
diff
changeset
|
5737 |
"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
|
5738 |
characters $* (to match any string) or $# (to match any character). |
48d3fab77bd9
added: #matches:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
14053
diff
changeset
|
5739 |
or [...] to match a set of characters. |
48d3fab77bd9
added: #matches:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
14053
diff
changeset
|
5740 |
Lower/uppercase are considered different. |
48d3fab77bd9
added: #matches:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
14053
diff
changeset
|
5741 |
|
48d3fab77bd9
added: #matches:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
14053
diff
changeset
|
5742 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
20874 | 5743 |
NOT the ST-80 meaning. |
14054
48d3fab77bd9
added: #matches:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
14053
diff
changeset
|
5744 |
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
|
5745 |
NOTICE: the receiver is the match pattern" |
48d3fab77bd9
added: #matches:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
14053
diff
changeset
|
5746 |
|
15734 | 5747 |
^ aPatternString match:self caseSensitive:ignoreCase not |
14054
48d3fab77bd9
added: #matches:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
14053
diff
changeset
|
5748 |
|
48d3fab77bd9
added: #matches:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
14053
diff
changeset
|
5749 |
"Created: / 08-03-2012 / 03:11:11 / cg" |
12111 | 5750 |
! ! |
5751 |
||
5752 |
||
18722 | 5753 |
!CharacterArray methodsFor:'matching - regex'! |
5754 |
||
5755 |
matchesRegex:regexString caseSensitive:aBoolean |
|
5756 |
"Test if the receiver matches a regex. |
|
5757 |
May raise RxParser>>regexErrorSignal or child signals. |
|
5758 |
This is a part of the Regular Expression Matcher package, |
|
20874 | 5759 |
(c) 1996, 1999 Vassili Bykov. |
18722 | 5760 |
Refer to `documentation' protocol of RxParser class for details." |
5761 |
||
5762 |
aBoolean ifFalse:[ |
|
20874 | 5763 |
^ self matchesRegexIgnoringCase:regexString |
18722 | 5764 |
] ifTrue:[ |
20874 | 5765 |
^ self matchesRegex:regexString |
18722 | 5766 |
]. |
5767 |
||
5768 |
" |
|
5769 |
'hello world' matchesRegex:'h.*d'. |
|
5770 |
" |
|
5771 |
||
5772 |
"Created: / 13-12-2010 / 11:01:49 / cg" |
|
5773 |
! ! |
|
17625 | 5774 |
|
9192 | 5775 |
!CharacterArray methodsFor:'padded copying'! |
5776 |
||
5777 |
centerPaddedTo:newSize |
|
19464 | 5778 |
"return a new string consisting of the receiver's characters, |
5779 |
plus spaces up to length and center the receiver's characters in |
|
5780 |
the resulting string. |
|
5781 |
If the receiver's size is equal or greater than the length argument, |
|
5782 |
the original receiver is returned unchanged." |
|
9192 | 5783 |
|
5784 |
^ self centerPaddedTo:newSize with:(Character space) |
|
5785 |
||
5786 |
" |
|
5787 |
'foo' centerPaddedTo:10 |
|
5788 |
123 printString centerPaddedTo:10 |
|
5789 |
" |
|
5790 |
||
5791 |
"Created: 25.11.1995 / 10:53:57 / cg" |
|
5792 |
! |
|
9190 | 5793 |
|
5794 |
centerPaddedTo:size with:padCharacter |
|
5795 |
"return a new string of length size, which contains the receiver |
|
5796 |
centered (i.e. padded on both sides). |
|
5797 |
Characters are filled with padCharacter. |
|
19464 | 5798 |
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
|
5799 |
the original receiver is returned unchanged." |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
5800 |
|
9190 | 5801 |
|len s| |
5802 |
||
5803 |
len := self size. |
|
9192 | 5804 |
(len < size) ifTrue:[ |
20874 | 5805 |
s := self species new:size withAll:padCharacter. |
5806 |
s replaceFrom:(size - len) // 2 + 1 with:self. |
|
5807 |
^ s |
|
9192 | 5808 |
] |
5809 |
||
9190 | 5810 |
" |
5811 |
'foo' centerPaddedTo:11 with:$. |
|
5812 |
'fooBar' centerPaddedTo:5 with:$. |
|
5813 |
123 printString centerPaddedTo:10 with:$. |
|
5814 |
(' ' , 123 printString) centerPaddedTo:10 with:$. |
|
5815 |
(Float pi printString) centerPaddedTo:15 with:(Character space) |
|
5816 |
(Float pi printString) centerPaddedTo:15 with:$- |
|
5817 |
(' ' , Float pi class name) centerPaddedTo:15 with:$. |
|
5818 |
" |
|
815
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
5819 |
! |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
5820 |
|
9190 | 5821 |
decimalPaddedTo:size and:afterPeriod at:decimalCharacter |
815
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
5822 |
"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
|
5823 |
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
|
5824 |
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
|
5825 |
to be padded). |
19464 | 5826 |
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
|
5827 |
the original receiver is returned unchanged. |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
5828 |
(sounds complicated ? -> see examples below)." |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
5829 |
|
8874 | 5830 |
^ self |
20874 | 5831 |
decimalPaddedTo:size |
5832 |
and:afterPeriod |
|
5833 |
at:decimalCharacter |
|
5834 |
withLeft:(Character space) |
|
5835 |
right:$0 |
|
815
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
5836 |
|
8874 | 5837 |
" |
5838 |
'123' decimalPaddedTo:10 and:3 at:$. -> ' 123 ' |
|
5839 |
'123' decimalPaddedTo:10 and:3 at:$. -> ' 123.000' |
|
5840 |
'123.' decimalPaddedTo:10 and:3 at:$. -> ' 123.000' |
|
815
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
5841 |
'123.1' decimalPaddedTo:10 and:3 at:$. -> ' 123.100' |
8874 | 5842 |
'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
|
5843 |
'123.123' decimalPaddedTo:10 and:3 at:$. -> ' 123.123' |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
5844 |
" |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
5845 |
|
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
5846 |
"Created: 23.12.1995 / 13:11:52 / cg" |
9192 | 5847 |
! |
5848 |
||
5849 |
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
|
5850 |
"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
|
5851 |
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
|
5852 |
of the period. |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
5853 |
Characters on the left are filled with leftPadChar. |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
5854 |
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
|
5855 |
otherwise, if missing, a decimal point is added and right characters filled with this. |
19464 | 5856 |
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
|
5857 |
the original receiver is returned unchanged. |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
5858 |
(sounds complicated ? -> see examples below)." |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
5859 |
|
5230
655c21ffe6cc
care for negative size of rest-string when decimalPadding
Claus Gittinger <cg@exept.de>
parents:
5221
diff
changeset
|
5860 |
|s idx n rest| |
815
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
5861 |
|
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
5862 |
idx := self indexOf:decimalCharacter. |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
5863 |
idx == 0 ifTrue:[ |
20874 | 5864 |
"/ |
5865 |
"/ no decimal point found; adjust string to the left of the period column |
|
5866 |
"/ |
|
5867 |
rightPadChar isNil ifTrue:[ |
|
5868 |
s := self , (self species new:afterPeriod + 1 withAll:leftPadChar) |
|
5869 |
] ifFalse:[ |
|
5870 |
s:= self , decimalCharacter asString , (self species new:afterPeriod withAll:rightPadChar). |
|
5871 |
]. |
|
815
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
5872 |
] ifFalse:[ |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
5873 |
|
20874 | 5874 |
"/ the number of after-decimalPoint characters |
5875 |
n := self size - idx. |
|
5876 |
rest := afterPeriod - n. |
|
5877 |
rest > 0 ifTrue:[ |
|
5878 |
s := (self species new:rest withAll:(rightPadChar ? leftPadChar)). |
|
5879 |
] ifFalse:[ |
|
5880 |
s := '' |
|
5881 |
]. |
|
5882 |
s := self , s. |
|
815
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
5883 |
]. |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
5884 |
|
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
5885 |
^ s leftPaddedTo:size with:leftPadChar |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
5886 |
|
8874 | 5887 |
" |
5888 |
'123' decimalPaddedTo:10 and:3 at:$. withLeft:(Character space) right:nil -> ' 123 ' |
|
5889 |
'123' decimalPaddedTo:10 and:3 at:$. withLeft:(Character space) right:$0 -> ' 123.000' |
|
5890 |
'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
|
5891 |
'123.1' decimalPaddedTo:10 and:3 at:$. withLeft:(Character space) right:$0 -> ' 123.100' |
8874 | 5892 |
'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
|
5893 |
'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
|
5894 |
" |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
5895 |
|
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
5896 |
"Modified: 23.12.1995 / 13:08:18 / cg" |
9192 | 5897 |
! |
5898 |
||
5899 |
leftPaddedTo:size |
|
5900 |
"return a new string of length size, which contains the receiver |
|
5901 |
right-adjusted (i.e. padded on the left). |
|
5902 |
Characters on the left are filled with spaces. |
|
19464 | 5903 |
If the receiver's size is equal or greater than the length argument, |
9192 | 5904 |
the original receiver is returned unchanged." |
5905 |
||
5906 |
^ self leftPaddedTo:size with:(Character space) |
|
5907 |
||
5908 |
" |
|
5909 |
'foo' leftPaddedTo:10 |
|
5910 |
'fooBar' leftPaddedTo:5 |
|
5911 |
123 printString leftPaddedTo:10 |
|
5912 |
" |
|
5913 |
! |
|
608 | 5914 |
|
5915 |
paddedTo:newSize |
|
19753 | 5916 |
"return a new string consisting of the receiver's characters, |
608 | 5917 |
plus spaces up to length. |
19464 | 5918 |
If the receiver's size is equal or greater than the length argument, |
608 | 5919 |
the original receiver is returned unchanged." |
5920 |
||
5921 |
^ self paddedTo:newSize with:(Character space) |
|
5922 |
||
5923 |
" |
|
8874 | 5924 |
'foo' paddedTo:10 |
5925 |
123 printString paddedTo:10 |
|
15734 | 5926 |
'12345678901234' paddedTo:10 |
608 | 5927 |
" |
64 | 5928 |
! ! |
5929 |
||
9192 | 5930 |
!CharacterArray methodsFor:'printing & storing'! |
608 | 5931 |
|
5932 |
article |
|
17137 | 5933 |
"return an article string for the receiver. |
5934 |
The original code only looked at the first character being a vowel; |
|
5935 |
this has been enhanced by some heuristics - not perfect, still." |
|
608 | 5936 |
|
17241 | 5937 |
|firstChar secondChar thirdChar| |
608 | 5938 |
|
8874 | 5939 |
firstChar := (self at:1) asLowercase. |
5848 | 5940 |
((firstChar isVowel and:[firstChar ~~ $u]) or:[firstChar == $x]) ifTrue:[ |
20874 | 5941 |
^ 'an' |
608 | 5942 |
]. |
17043 | 5943 |
|
5944 |
(self size >= 3) ifTrue:[ |
|
20874 | 5945 |
secondChar := (self at:2) asLowercase. |
5946 |
||
5947 |
"/ may need more here... |
|
5948 |
( #('rb') includes:(String with:firstChar with:secondChar)) ifTrue:[ |
|
5949 |
^ 'an' |
|
5950 |
]. |
|
5951 |
||
5952 |
thirdChar := (self at:3) asLowercase. |
|
5953 |
||
5954 |
(firstChar isVowel not |
|
5955 |
and:[(secondChar isVowel or:[secondChar == $y]) not |
|
5956 |
and:[thirdChar isVowel not ]]) ifTrue:[ |
|
5957 |
"/ exceptions: 3 non-vowels in a row: looks like an abbreviation |
|
5958 |
(self size > 4) ifTrue:[ |
|
5959 |
(firstChar == $s) ifTrue:[ |
|
5960 |
((secondChar == $c and:[thirdChar == $r]) |
|
5961 |
or:[ (secondChar == $t and:[thirdChar == $r]) ]) ifTrue:[ |
|
5962 |
(self at:4) isVowel ifTrue:[ |
|
5963 |
^ 'a' |
|
5964 |
] |
|
5965 |
] |
|
5966 |
]. |
|
5967 |
]. |
|
5968 |
"/ an abbreviation; treat x, s as vowels |
|
5969 |
(firstChar == $x or:[ firstChar == $s ]) ifTrue:[^ 'an']. |
|
5970 |
] |
|
17043 | 5971 |
]. |
608 | 5972 |
^ 'a' |
5848 | 5973 |
|
5974 |
" |
|
20874 | 5975 |
'uboot' article. |
5976 |
'xmas' article. |
|
5977 |
'alarm' article. |
|
5978 |
'baby' article. |
|
5979 |
'sql' article. |
|
5980 |
'scr' article. |
|
5981 |
'screen' article. |
|
5982 |
'scrollbar' article. |
|
5983 |
'scrs' article. |
|
5984 |
'cvs' article. |
|
5985 |
'cvssource' article. |
|
5986 |
'symbol' article. |
|
5987 |
'string' article. |
|
5988 |
'rbparser' article. |
|
19683 | 5989 |
" |
5990 |
||
5991 |
"Modified (comment): / 01-05-2016 / 10:57:25 / cg" |
|
608 | 5992 |
! |
5993 |
||
8049
ccd0e8f26439
printing - migrating towards unicodeSupport in ST-code
Claus Gittinger <cg@exept.de>
parents:
8040
diff
changeset
|
5994 |
basicStoreString |
ccd0e8f26439
printing - migrating towards unicodeSupport in ST-code
Claus Gittinger <cg@exept.de>
parents:
8040
diff
changeset
|
5995 |
"return a String for storing myself" |
ccd0e8f26439
printing - migrating towards unicodeSupport in ST-code
Claus Gittinger <cg@exept.de>
parents:
8040
diff
changeset
|
5996 |
|
ccd0e8f26439
printing - migrating towards unicodeSupport in ST-code
Claus Gittinger <cg@exept.de>
parents:
8040
diff
changeset
|
5997 |
|s n index| |
ccd0e8f26439
printing - migrating towards unicodeSupport in ST-code
Claus Gittinger <cg@exept.de>
parents:
8040
diff
changeset
|
5998 |
|
ccd0e8f26439
printing - migrating towards unicodeSupport in ST-code
Claus Gittinger <cg@exept.de>
parents:
8040
diff
changeset
|
5999 |
n := self occurrencesOf:$'. |
11320 | 6000 |
n ~~ 0 ifTrue:[ |
20874 | 6001 |
s := self species new:(n + 2 + self size). |
6002 |
s at:1 put:$'. |
|
6003 |
index := 2. |
|
6004 |
self do:[:thisChar | |
|
6005 |
(thisChar == $') ifTrue:[ |
|
6006 |
s at:index put:thisChar. |
|
6007 |
index := index + 1. |
|
6008 |
]. |
|
6009 |
s at:index put:thisChar. |
|
6010 |
index := index + 1. |
|
6011 |
]. |
|
6012 |
s at:index put:$'. |
|
6013 |
^ s |
|
9192 | 6014 |
]. |
11320 | 6015 |
|
9192 | 6016 |
^ '''' , self , '''' |
15523 | 6017 |
|
6018 |
" |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
6019 |
'''immutable'' string' asImmutableString basicStoreString |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
6020 |
'immutable string' asImmutableString basicStoreString |
15523 | 6021 |
" |
6022 |
||
6023 |
"Modified: / 14-07-2013 / 19:20:49 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
9192 | 6024 |
! |
8049
ccd0e8f26439
printing - migrating towards unicodeSupport in ST-code
Claus Gittinger <cg@exept.de>
parents:
8040
diff
changeset
|
6025 |
|
11774
6f6e2b71b607
displayOn (inspector's interface) was broken
Claus Gittinger <cg@exept.de>
parents:
11754
diff
changeset
|
6026 |
displayOn:aGCOrStream |
6f6e2b71b607
displayOn (inspector's interface) was broken
Claus Gittinger <cg@exept.de>
parents:
11754
diff
changeset
|
6027 |
"Compatibility |
6f6e2b71b607
displayOn (inspector's interface) was broken
Claus Gittinger <cg@exept.de>
parents:
11754
diff
changeset
|
6028 |
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
|
6029 |
OR: |
6f6e2b71b607
displayOn (inspector's interface) was broken
Claus Gittinger <cg@exept.de>
parents:
11754
diff
changeset
|
6030 |
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
|
6031 |
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
|
6032 |
(although the fallBack is to display its printString ...)" |
6f6e2b71b607
displayOn (inspector's interface) was broken
Claus Gittinger <cg@exept.de>
parents:
11754
diff
changeset
|
6033 |
|
6f6e2b71b607
displayOn (inspector's interface) was broken
Claus Gittinger <cg@exept.de>
parents:
11754
diff
changeset
|
6034 |
"/ 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
|
6035 |
"/ ST/X (and some old ST80's) mean: draw-yourself on a GC. |
16738 | 6036 |
(aGCOrStream isStream) ifTrue:[ |
20874 | 6037 |
self storeOn:aGCOrStream. |
6038 |
^ self |
|
11774
6f6e2b71b607
displayOn (inspector's interface) was broken
Claus Gittinger <cg@exept.de>
parents:
11754
diff
changeset
|
6039 |
]. |
6f6e2b71b607
displayOn (inspector's interface) was broken
Claus Gittinger <cg@exept.de>
parents:
11754
diff
changeset
|
6040 |
^ super displayOn:aGCOrStream |
6f6e2b71b607
displayOn (inspector's interface) was broken
Claus Gittinger <cg@exept.de>
parents:
11754
diff
changeset
|
6041 |
! |
6f6e2b71b607
displayOn (inspector's interface) was broken
Claus Gittinger <cg@exept.de>
parents:
11754
diff
changeset
|
6042 |
|
608 | 6043 |
printOn:aStream |
6044 |
"print the receiver on aStream" |
|
6045 |
||
6046 |
aStream nextPutAll:self |
|
6047 |
! |
|
6048 |
||
6049 |
printString |
|
13079
111e7e456648
Implement display with #displayOn: instead of #displayString
Stefan Vogel <sv@exept.de>
parents:
13073
diff
changeset
|
6050 |
"return a string for printing - that's myself" |
608 | 6051 |
|
6052 |
^ self |
|
3578 | 6053 |
! |
6054 |
||
6055 |
printWithQuotesDoubledOn:aStream |
|
6056 |
"put the raw storeString of myself on aStream" |
|
6057 |
||
6058 |
self do:[:thisChar | |
|
20874 | 6059 |
(thisChar == $') ifTrue:[aStream nextPut:thisChar]. |
6060 |
aStream nextPut:thisChar |
|
3578 | 6061 |
] |
6062 |
||
6063 |
"Modified: / 15.6.1998 / 17:21:17 / cg" |
|
6064 |
"Created: / 15.6.1998 / 17:22:13 / cg" |
|
12171 | 6065 |
! |
6066 |
||
15788 | 6067 |
printXmlQuotedOn:aStream |
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
6068 |
"convert aString to a valid XML string |
15788 | 6069 |
that can be used for attributes, text, comments an PIs |
6070 |
TODO: care for 16bit UNICODE string and escape chars ..." |
|
6071 |
||
6072 |
self do:[:eachChar | |
|
20874 | 6073 |
eachChar == $< ifTrue:[ |
6074 |
aStream nextPutAll:'<' "mapping needed for xml text" |
|
6075 |
] ifFalse:[ eachChar == $& ifTrue:[ |
|
6076 |
aStream nextPutAll:'&' "mapping needed for all" |
|
6077 |
] ifFalse:[ eachChar == $> ifTrue:[ |
|
6078 |
aStream nextPutAll:'>' "mapping needed for comments" |
|
6079 |
] ifFalse:[ eachChar == $' ifTrue:[ |
|
6080 |
aStream nextPutAll:''' "mapping needed for attributes" |
|
6081 |
] ifFalse:[ eachChar == $" ifTrue:[ |
|
6082 |
aStream nextPutAll:'"' "mapping needed for attributes" |
|
6083 |
] ifFalse:[ |
|
6084 |
|codePoint| |
|
6085 |
codePoint := eachChar codePoint. |
|
6086 |
(codePoint < 16r20 or:[codePoint >= 16r7F]) ifTrue:[ |
|
6087 |
aStream nextPutAll:'&#'. |
|
6088 |
codePoint printOn:aStream. |
|
6089 |
aStream nextPut:$;. |
|
6090 |
] ifFalse:[ |
|
6091 |
aStream nextPut:eachChar |
|
6092 |
]]]]]] |
|
15788 | 6093 |
]. |
6094 |
! |
|
6095 |
||
6096 |
printXmlTextQuotedOn:aStream |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
6097 |
"convert aString to a valid XML string |
15788 | 6098 |
that can be used for XML text. |
6099 |
Here line formatting characters are not escaped. |
|
6100 |
TODO: care for 16bit UNICODE string and escape chars ..." |
|
6101 |
||
6102 |
self do:[:eachChar | |
|
20874 | 6103 |
eachChar == $< ifTrue:[ |
6104 |
aStream nextPutAll:'<' "mapping needed for xml text" |
|
6105 |
] ifFalse:[ eachChar == $& ifTrue:[ |
|
6106 |
aStream nextPutAll:'&' "mapping needed for all" |
|
15788 | 6107 |
"/ ] ifFalse:[ eachChar == $> ifTrue:[ |
6108 |
"/ aStream nextPutAll:'>' "mapping needed for comments" |
|
6109 |
"/ ] ifFalse:[ eachChar == $' ifTrue:[ |
|
6110 |
"/ aStream nextPutAll:''' "mapping needed for attributes" |
|
6111 |
"/ ] ifFalse:[ eachChar == $" ifTrue:[ |
|
6112 |
"/ aStream nextPutAll:'"' "mapping needed for attributes" |
|
20874 | 6113 |
] ifFalse:[ |
6114 |
|codePoint| |
|
6115 |
codePoint := eachChar codePoint. |
|
6116 |
((codePoint < 16r20 and:[codePoint ~~ 9 and:[codePoint ~~ 10 and:[codePoint ~~ 13]]]) |
|
6117 |
or:[codePoint >= 16r7F]) ifTrue:[ |
|
6118 |
aStream nextPutAll:'&#'. |
|
6119 |
codePoint printOn:aStream. |
|
6120 |
aStream nextPut:$;. |
|
6121 |
] ifFalse:[ |
|
6122 |
aStream nextPut:eachChar |
|
6123 |
]]]"/]]] |
|
15788 | 6124 |
]. |
6125 |
! |
|
6126 |
||
12171 | 6127 |
printfWith:arg1 with:arg2 with:arg3 with:arg4 with:arg5 |
12177 | 6128 |
"Format and print the receiver with <argI> formatted in C style, |
12171 | 6129 |
as specified in the Unix C-language manual page for printf(3). |
15712 | 6130 |
Return the resulting string (i.e actually, this is more like an sprintf)." |
12177 | 6131 |
|
12171 | 6132 |
^ self printf:(Array with:arg1 with:arg2 with:arg3 with:arg4 with:arg5) |
6133 |
||
6134 |
" |
|
12177 | 6135 |
Transcript showCR:('%d %05x %08o %b' printfWith:123 with:234 with:345 with:123) |
12171 | 6136 |
" |
15788 | 6137 |
! |
6138 |
||
6139 |
xmlQuotedPrintString |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
6140 |
"convert aString to a valid XML string |
15788 | 6141 |
that can be used for attributes, text, comments an PIs |
6142 |
TODO: care for 16bit UNICODE string and escape chars ..." |
|
6143 |
||
6144 |
^ String streamContents:[:s| |
|
20874 | 6145 |
self printXmlQuotedOn:s |
15788 | 6146 |
]. |
9192 | 6147 |
! ! |
6148 |
||
11955 | 6149 |
|
9192 | 6150 |
!CharacterArray methodsFor:'queries'! |
608 | 6151 |
|
4429 | 6152 |
bitsPerCharacter |
18662 | 6153 |
"return the underlying string's bitsPerCharacter |
4429 | 6154 |
(i.e. is it a regular String or a TwoByteString)" |
6155 |
||
8457 | 6156 |
|string max| |
6157 |
||
6158 |
(string := self string) ~~ self ifTrue:[ |
|
20874 | 6159 |
^ string bitsPerCharacter |
8457 | 6160 |
]. |
6161 |
||
6162 |
max := 8. |
|
6163 |
self do:[:eachCharacter | |
|
20874 | 6164 |
max := max max:(eachCharacter bitsPerCharacter) |
9192 | 6165 |
]. |
6166 |
^ max |
|
6167 |
||
6168 |
" |
|
6169 |
'hello' bitsPerCharacter |
|
6170 |
'hello' asText allBold bitsPerCharacter |
|
6171 |
" |
|
6172 |
! |
|
8457 | 6173 |
|
19757 | 6174 |
bytesPerCharacter |
6175 |
"return the underlying string's required |
|
6176 |
bytesPerCharacter |
|
6177 |
(i.e. is it a regular String or a TwoByteString)" |
|
6178 |
||
6179 |
|string max| |
|
6180 |
||
6181 |
(string := self string) ~~ self ifTrue:[ |
|
20874 | 6182 |
^ string bytesPerCharacter |
19757 | 6183 |
]. |
6184 |
||
6185 |
max := 1. |
|
6186 |
self do:[:eachCharacter | |
|
20874 | 6187 |
max := max max:(eachCharacter bytesPerCharacter) |
19757 | 6188 |
]. |
6189 |
^ max |
|
6190 |
||
6191 |
" |
|
6192 |
'hello' bytesPerCharacter |
|
6193 |
'hello' asUnicode16String bytesPerCharacter |
|
6194 |
'hello' asText allBold bytesPerCharacter |
|
6195 |
" |
|
6196 |
! |
|
6197 |
||
18967 | 6198 |
containsNon8BitElements |
6199 |
"return true, if the underlying string contains elements larger than a single byte" |
|
6200 |
||
6201 |
|sz "{ Class:SmallInteger }"| |
|
6202 |
||
6203 |
sz := self size. |
|
6204 |
1 to:sz do:[:idx| |
|
20874 | 6205 |
(self at:idx) codePoint > 16rFF ifTrue:[ |
6206 |
^ true. |
|
6207 |
]. |
|
18967 | 6208 |
]. |
6209 |
^ false. |
|
6210 |
! |
|
6211 |
||
10903 | 6212 |
continuesWith:aString startingAt:startIndex |
6213 |
"return true, if the receiver beginning at startIndex |
|
6214 |
contains the characters in aString." |
|
6215 |
||
6216 |
|sz "{Class: SmallInteger }" |
|
6217 |
idx "{Class: SmallInteger }"| |
|
6218 |
||
6219 |
sz := aString size. |
|
6220 |
idx := startIndex. |
|
6221 |
||
6222 |
1 to:sz do:[:i | |
|
20874 | 6223 |
(self at:idx) ~~ (aString at:i) ifTrue:[^ false]. |
6224 |
idx := idx + 1 |
|
10903 | 6225 |
]. |
6226 |
^ true |
|
6227 |
||
6228 |
" |
|
6229 |
'hello world' continuesWith:'world' startingAt:6 |
|
6230 |
'hello world' continuesWith:'world' startingAt:7 |
|
6231 |
" |
|
6232 |
||
6233 |
"Created: 12.5.1996 / 15:46:40 / cg" |
|
6234 |
"Modified: 26.7.1996 / 19:08:36 / cg" |
|
6235 |
! |
|
6236 |
||
6237 |
countWords |
|
6238 |
"return the number of words, which are separated by separators" |
|
6239 |
||
6240 |
|tally "{ Class: SmallInteger }" |
|
6241 |
start "{ Class: SmallInteger }" |
|
6242 |
mySize "{ Class: SmallInteger }" |
|
6243 |
stop ch| |
|
6244 |
||
6245 |
tally := 0. |
|
6246 |
start := 1. |
|
6247 |
mySize := self size. |
|
6248 |
[start <= mySize] whileTrue:[ |
|
20874 | 6249 |
ch := self at:start. |
6250 |
ch isSeparator ifTrue:[ |
|
6251 |
start := start + 1 |
|
6252 |
] ifFalse:[ |
|
6253 |
stop := self indexOfSeparatorStartingAt:start. |
|
6254 |
(stop == 0) ifTrue:[ |
|
6255 |
stop := mySize + 1 |
|
6256 |
]. |
|
6257 |
tally := tally + 1. |
|
6258 |
start := stop |
|
6259 |
] |
|
10903 | 6260 |
]. |
6261 |
^ tally |
|
6262 |
||
6263 |
" |
|
6264 |
'hello world isnt this nice' countWords' |
|
6265 |
" |
|
6266 |
! |
|
6267 |
||
11788 | 6268 |
defaultElement |
6269 |
^ Character space |
|
6270 |
! |
|
6271 |
||
608 | 6272 |
encoding |
1311 | 6273 |
"return the strings encoding, as a symbol. |
7982 | 6274 |
Here, by default, we assume unicode-encoding. |
20006 | 6275 |
Notice, that ISO-8859-1 is a true subset of unicode, |
8874 | 6276 |
and that singleByteStrings are therefore both unicode AND |
7982 | 6277 |
8859-1 encoded." |
6278 |
||
14168 | 6279 |
^ #unicode |
608 | 6280 |
! |
6281 |
||
1375
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
6282 |
hasChangeOfEmphasis |
14405
1d0d99a95bba
comment/format in:7 methods
Claus Gittinger <cg@exept.de>
parents:
14168
diff
changeset
|
6283 |
"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
|
6284 |
i.e. any non-normal (=emphasized) characters" |
1d0d99a95bba
comment/format in:7 methods
Claus Gittinger <cg@exept.de>
parents:
14168
diff
changeset
|
6285 |
|
1375
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
6286 |
^ false |
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
6287 |
|
1381 | 6288 |
"Created: 12.5.1996 / 12:31:39 / cg" |
1375
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
6289 |
! |
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
6290 |
|
7995
8ddbb403d67b
+hasIcon and hasImage for LabelAndIcon compatibility
Claus Gittinger <cg@exept.de>
parents:
7991
diff
changeset
|
6291 |
hasIcon |
9191 | 6292 |
"for LabelAndIcon compatibility" |
6293 |
||
9192 | 6294 |
^ false |
6295 |
! |
|
6296 |
||
6297 |
hasImage |
|
6298 |
"for LabelAndIcon compatibility" |
|
6299 |
||
6300 |
^ false |
|
6301 |
! |
|
7995
8ddbb403d67b
+hasIcon and hasImage for LabelAndIcon compatibility
Claus Gittinger <cg@exept.de>
parents:
7991
diff
changeset
|
6302 |
|
1388 | 6303 |
heightOn:aGC |
19464 | 6304 |
"return the size of the receiver in device units if displayed on aGC" |
1388 | 6305 |
|
20221 | 6306 |
^ aGC deviceFont heightOf:self "font is already a device font" |
1388 | 6307 |
|
6308 |
" |
|
8874 | 6309 |
'hello world' heightOn:(View new) |
1388 | 6310 |
" |
6311 |
||
6312 |
"Created: 12.5.1996 / 20:09:29 / cg" |
|
6313 |
"Modified: 12.5.1996 / 20:32:05 / cg" |
|
6314 |
! |
|
6315 |
||
11828 | 6316 |
isLowercaseFirst |
6317 |
"return true, if the first character is a lowercase character." |
|
6318 |
||
6319 |
^ self size ~~ 0 and:[ self first isLowercase ] |
|
6320 |
||
6321 |
" |
|
12177 | 6322 |
'helloWorld' isLowercaseFirst |
6323 |
'HelloWorld' isLowercaseFirst |
|
11828 | 6324 |
" |
6325 |
! |
|
6326 |
||
6327 |
isUppercaseFirst |
|
6328 |
"return true, if the first character is an uppercase character." |
|
6329 |
||
6330 |
^ self size ~~ 0 and:[ self first isUppercase ] |
|
6331 |
||
6332 |
" |
|
12177 | 6333 |
'helloWorld' isUppercaseFirst |
6334 |
'HelloWorld' isUppercaseFirst |
|
11828 | 6335 |
" |
6336 |
! |
|
6337 |
||
15773 | 6338 |
keywords |
6339 |
"assuming the receiver is a keyword message selector, |
|
6340 |
return the individual keywords (i.e. break it up at colons) |
|
6341 |
and return these as a collection. |
|
6342 |
For binary and unary selectors, the result may be nonsense (an array containing the receiver)." |
|
6343 |
||
6344 |
|coll s part| |
|
6345 |
||
6346 |
coll := OrderedCollection new. |
|
6347 |
s := ReadStream on:self. |
|
6348 |
[s atEnd] whileFalse:[ |
|
20874 | 6349 |
part := s through:$:. |
6350 |
coll add:part |
|
15773 | 6351 |
]. |
6352 |
^ coll asArray |
|
6353 |
||
6354 |
" |
|
6355 |
#at:put: keywords |
|
6356 |
#at: keywords |
|
6357 |
#+ keywords |
|
6358 |
#size keywords |
|
6359 |
" |
|
6360 |
||
6361 |
"Modified (Comment): / 30-06-2011 / 17:46:21 / cg" |
|
6362 |
! |
|
6363 |
||
9190 | 6364 |
knownAsSymbol |
6365 |
"for now, only single character strings are allowed as symbols. |
|
6366 |
This method is redefined in String." |
|
6367 |
||
6368 |
^ false |
|
6369 |
! |
|
6370 |
||
6371 |
leftIndent |
|
6372 |
"if the receiver starts with spaces, return the number of spaces |
|
6373 |
at the left - otherwise, return 0. |
|
19464 | 6374 |
If the receiver consists of spaces only, return the receiver's size." |
9192 | 6375 |
|
6376 |
|index "{Class: SmallInteger }" |
|
6377 |
end "{Class: SmallInteger }"| |
|
6378 |
||
6379 |
index := 1. |
|
6380 |
end := self size. |
|
6381 |
[index <= end] whileTrue:[ |
|
20874 | 6382 |
(self at:index) isSeparator ifFalse:[^ index - 1]. |
6383 |
index := index + 1 |
|
8350 | 6384 |
]. |
9192 | 6385 |
^ end |
6386 |
||
6387 |
" |
|
6388 |
' hello' leftIndent |
|
6389 |
'foo ' leftIndent |
|
9190 | 6390 |
' ' leftIndent |
6391 |
" |
|
6392 |
||
6393 |
"Modified: 20.4.1996 / 19:28:43 / cg" |
|
8350 | 6394 |
! |
6395 |
||
10437 | 6396 |
nameSpaceSelectorParts |
6397 |
"Answer the namespace and baseSelector parts of a namespace selector. |
|
6398 |
Namespace selectors are those generated by sends from a method in a different |
|
6399 |
namespace; they are prefixed by ':'<ns>'::'. |
|
6400 |
You cannot easily change this algorithm here, as it is also known by the VM's lookup function. |
|
6401 |
Experimental" |
|
6402 |
||
6403 |
|nsPart selPart idx| |
|
6404 |
||
6405 |
(self at:1) == $: ifFalse:[^ Array with:'' with:self]. |
|
6406 |
idx := self indexOf:$: startingAt:3. |
|
6407 |
idx == 0 ifTrue:[^ Array with:'' with:self]. |
|
6408 |
||
6409 |
(idx+2 > self size) ifTrue:[^ Array with:'' with:self]. |
|
6410 |
(self at:idx+1) == $: ifFalse:[^ Array with:'' with:self]. |
|
6411 |
nsPart := self copyFrom:2 to:idx-1. |
|
6412 |
selPart := self copyFrom:idx+2. |
|
6413 |
^ Array with:nsPart with:selPart |
|
6414 |
||
6415 |
"test: |
|
10703
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
6416 |
self assert:('+' nameSpaceSelectorParts) = #('' '+'). |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
6417 |
self assert:(':+' nameSpaceSelectorParts) = #('' ':+'). |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
6418 |
self assert:(':Foo:+' nameSpaceSelectorParts) = #('' ':Foo:+'). |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
6419 |
|
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
6420 |
self assert:(':Foo::+' nameSpaceSelectorParts) = #('Foo' '+'). |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
6421 |
self assert:(':Foo::bar:baz:' nameSpaceSelectorParts) = #('Foo' 'bar:baz:'). |
10437 | 6422 |
" |
6423 |
||
6424 |
"Created: / 05-03-2007 / 17:16:58 / cg" |
|
6425 |
"Modified: / 06-03-2007 / 11:51:15 / cg" |
|
6426 |
! |
|
6427 |
||
9190 | 6428 |
stringSpecies |
8874 | 6429 |
"return the underlying strings bitsPerCharacter |
8037
8cce25b1059c
care for wide chars in asXXXFirst
Claus Gittinger <cg@exept.de>
parents:
8032
diff
changeset
|
6430 |
(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
|
6431 |
|
8cce25b1059c
care for wide chars in asXXXFirst
Claus Gittinger <cg@exept.de>
parents:
8032
diff
changeset
|
6432 |
|string| |
8cce25b1059c
care for wide chars in asXXXFirst
Claus Gittinger <cg@exept.de>
parents:
8032
diff
changeset
|
6433 |
|
15523 | 6434 |
string := self string. |
6435 |
string == self ifTrue:[^ self species]. |
|
8037
8cce25b1059c
care for wide chars in asXXXFirst
Claus Gittinger <cg@exept.de>
parents:
8032
diff
changeset
|
6436 |
^ string stringSpecies |
8cce25b1059c
care for wide chars in asXXXFirst
Claus Gittinger <cg@exept.de>
parents:
8032
diff
changeset
|
6437 |
|
8cce25b1059c
care for wide chars in asXXXFirst
Claus Gittinger <cg@exept.de>
parents:
8032
diff
changeset
|
6438 |
" |
8874 | 6439 |
'hello' stringSpecies |
9192 | 6440 |
'hello' asText allBold stringSpecies |
6441 |
" |
|
6442 |
! |
|
8037
8cce25b1059c
care for wide chars in asXXXFirst
Claus Gittinger <cg@exept.de>
parents:
8032
diff
changeset
|
6443 |
|
6653 | 6444 |
widthFrom:startIndex to:endIndex on:aGC |
19464 | 6445 |
"return ths size of part of the receiver in device units if displayed on aGC" |
6653 | 6446 |
|
20221 | 6447 |
^ aGC deviceFont widthOf:self from:startIndex to:endIndex |
6653 | 6448 |
|
6449 |
" |
|
8874 | 6450 |
'hello world' widthFrom:1 to:5 on:(View new) |
6451 |
'hello' widthOn:(View new) |
|
6653 | 6452 |
" |
9192 | 6453 |
! |
6653 | 6454 |
|
1388 | 6455 |
widthOn:aGC |
19464 | 6456 |
"return ths size of the receiver in device units if displayed on aGC" |
1388 | 6457 |
|
20221 | 6458 |
^ aGC deviceFont widthOf:self "font is already a device font" |
1388 | 6459 |
|
6460 |
" |
|
8874 | 6461 |
'hello world' widthOn:(View new) |
1388 | 6462 |
" |
6463 |
||
6464 |
"Created: 12.5.1996 / 20:09:29 / cg" |
|
2554 | 6465 |
"Modified: 17.4.1997 / 12:50:23 / cg" |
608 | 6466 |
! ! |
6467 |
||
20873 | 6468 |
|
608 | 6469 |
!CharacterArray methodsFor:'special string converting'! |
6470 |
||
16759 | 6471 |
asUnixFilenameString |
6472 |
"return a new string consisting of receiver's characters |
|
6473 |
with all \-characters replaced by /-characters. |
|
6474 |
If there are no backslashes, return the original" |
|
6475 |
||
6476 |
^ self copyReplaceAll:$\ with:$/ ifNone:self |
|
6477 |
||
6478 |
" |
|
6479 |
'hello\world' asUnixFilenameString |
|
6480 |
" |
|
6481 |
||
6482 |
"Modified: / 18.7.1998 / 22:53:02 / cg" |
|
6483 |
! |
|
6484 |
||
14768 | 6485 |
expandPlaceholders:escapeCharacter with:argArrayOrDictionary |
6486 |
"this is the generic version of the old %-escaping method, allowing for an arbitrary |
|
6487 |
escape character to be used (typically $$ or $% are effectively used). |
|
6488 |
Return a copy of the receiver, where all %i escapes are |
|
6489 |
replaced by corresponding arguments' printStrings from the argArrayOrDictionary. |
|
6490 |
I.e. 'hello %1; how is %2' expandPlaceholdersWith:#('world' 'this') results |
|
6491 |
in the new string 'hello world; how is this'. |
|
6492 |
As an extension, the argument may also be a dictionary, providing |
|
6493 |
values for symbolic keys. |
|
6494 |
In this case, %a .. %z and %(...) are also allowed. |
|
6495 |
(%1..%9 require a numeric key in the dictionary, however) |
|
6496 |
To get a '%' character, use a '%%'-escape. |
|
6497 |
To get an integer-indexed placeHolder followed by another digit, |
|
6498 |
or an index > 9, you must use %(digit). |
|
6499 |
See also bindWith:... for VisualAge compatibility." |
|
6500 |
||
6501 |
|stream| |
|
6502 |
||
18269 | 6503 |
stream := (TextStream ? CharacterWriteStream) on:(self species uninitializedNew:self size + 20). |
14768 | 6504 |
self expandPlaceholders:escapeCharacter with:argArrayOrDictionary on:stream. |
6505 |
^ stream contents. |
|
6506 |
||
6507 |
||
6508 |
" |
|
6509 |
'hello %1' expandPlaceholdersWith:#('world') |
|
6510 |
'hello %1; how is %2' expandPlaceholdersWith:#('world' 'this') |
|
6511 |
'hello %2; how is %1' expandPlaceholdersWith:#('world' 'this') |
|
6512 |
'%1 plus %2 gives %3 ' expandPlaceholdersWith:#(4 5 9) |
|
6513 |
'%%(1)0 gives %(1)0' expandPlaceholdersWith:#(123) |
|
6514 |
'%%10 gives %10' expandPlaceholdersWith:#(123) |
|
6515 |
'%%(10) gives %(10)' expandPlaceholdersWith:#(123) |
|
6516 |
'%test gives %1' expandPlaceholdersWith:#(123) |
|
6517 |
'bla %1 bla' expandPlaceholdersWith:{ 'hello' allBold } |
|
6518 |
'bla %1 bla' expandPlaceholdersWith:{ 'hello' } |
|
6519 |
" |
|
6520 |
||
6521 |
" |
|
6522 |
|dict| |
|
6523 |
||
6524 |
dict := Dictionary new. |
|
6525 |
dict at:1 put:'one'. |
|
6526 |
dict at:$a put:'AAAAA'. |
|
6527 |
dict at:$b put:[ Time now ]. |
|
6528 |
dict at:'foo' put:[ Date today ]. |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
6529 |
'hello $1 %a $b %(foo) $foo ' expandPlaceholders:$$ with:dict. |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
6530 |
'hello $1 %a $b %(foo) $foo ' expandPlaceholders:$% with:dict. |
14768 | 6531 |
" |
6532 |
||
6533 |
"Modified: 1.7.1997 / 00:53:24 / cg" |
|
6534 |
! |
|
6535 |
||
14765 | 6536 |
expandPlaceholders:escapeCharacter with:argArrayOrDictionary on:aStream |
6537 |
"this is the generic version of the old %-escaping method, allowing for an arbitrary |
|
6538 |
escape character to be used (typically $$ or $% are effectively used). |
|
18425 | 6539 |
|
14765 | 6540 |
Write the receiver to aStream, where all %i escapes are |
6541 |
replaced by corresponding arguments' printStrings from the argArrayOrDictionary. |
|
6542 |
I.e. 'hello %1; how is %2' expandPlaceholdersWith:#('world' 'this') results |
|
6543 |
in the new string 'hello world; how is this'. |
|
18425 | 6544 |
|
17192 | 6545 |
As an extension, the argument may also be a dictionary, providing values for symbolic keys. |
14765 | 6546 |
In this case, %a .. %z and %(...) are also allowed. |
6547 |
(%1..%9 require a numeric key in the dictionary, however) |
|
17192 | 6548 |
Also, the values in argArrayOrDictionary may be blocks. |
18425 | 6549 |
|
14765 | 6550 |
To get a '%' character, use a '%%'-escape. |
6551 |
To get an integer-indexed placeHolder followed by another digit, |
|
6552 |
or an index > 9, you must use %(digit). |
|
18425 | 6553 |
|
14765 | 6554 |
See also bindWith:... for VisualAge compatibility. |
6555 |
Use %<cr> to insert a CR and %<tab> to insert a TAB." |
|
6556 |
||
6557 |
|next v key |
|
6558 |
idx "{ SmallInteger }" |
|
6559 |
idx2 "{ SmallInteger }" |
|
6560 |
start "{ SmallInteger }" |
|
6561 |
stop "{ SmallInteger }"| |
|
6562 |
||
6563 |
stop := self size. |
|
6564 |
start := 1. |
|
6565 |
[start <= stop] whileTrue:[ |
|
20874 | 6566 |
idx := self indexOf:escapeCharacter startingAt:start. |
6567 |
(idx == 0 or:[idx == stop]) ifTrue:[ |
|
6568 |
aStream nextPutAll:self startingAt:start to:stop. |
|
6569 |
^ self. |
|
6570 |
]. |
|
6571 |
"found an escapeCharacter" |
|
6572 |
aStream nextPutAll:self startingAt:start to:(idx - 1). |
|
6573 |
next := self at:(idx + 1). |
|
6574 |
(next == escapeCharacter) ifTrue:[ |
|
6575 |
aStream nextPut:escapeCharacter. |
|
6576 |
] ifFalse:[ |
|
6577 |
next == $< ifTrue:[ |
|
6578 |
idx2 := self indexOf:$> startingAt:idx+2. |
|
6579 |
key := self copyFrom:idx+2 to:idx2-1. |
|
6580 |
idx := idx2 - 1. |
|
6581 |
key := key asSymbolIfInterned. |
|
6582 |
(#(cr tab nl return lf ff null) includesIdentical:key) ifTrue:[ |
|
6583 |
aStream nextPut:(Character perform:key). |
|
6584 |
]. |
|
6585 |
] ifFalse:[ |
|
6586 |
next isDigit ifTrue:[ |
|
6587 |
v := argArrayOrDictionary at:(next digitValue) ifAbsent:'' |
|
6588 |
] ifFalse:[ |
|
6589 |
next == $( ifTrue:[ |
|
6590 |
idx2 := self indexOf:$) startingAt:idx+2. |
|
6591 |
key := self copyFrom:idx+2 to:idx2-1. |
|
6592 |
idx := idx2 - 1. |
|
6593 |
(argArrayOrDictionary includesKey:key) ifTrue:[ |
|
6594 |
v := argArrayOrDictionary at:key |
|
6595 |
] ifFalse:[ |
|
6596 |
key := key asSymbolIfInterned ? key. |
|
6597 |
(argArrayOrDictionary includesKey:key) ifTrue:[ |
|
6598 |
v := argArrayOrDictionary at:key |
|
6599 |
] ifFalse:[ |
|
6600 |
(key size == 1 and:[ argArrayOrDictionary includesKey:(key at:1)]) ifTrue:[ |
|
6601 |
v := argArrayOrDictionary at:(key at:1) |
|
6602 |
] ifFalse:[ |
|
6603 |
key isNumeric ifTrue:[ |
|
6604 |
key := Integer readFrom:key onError:nil. |
|
6605 |
]. |
|
6606 |
v := argArrayOrDictionary at:key ifAbsent:'' |
|
6607 |
] |
|
6608 |
]. |
|
6609 |
]. |
|
6610 |
] ifFalse:[ |
|
6611 |
(next isLetter and:[argArrayOrDictionary isSequenceable not "is a Dictionary"]) ifTrue:[ |
|
6612 |
"so next is a non-numeric single character." |
|
6613 |
v := argArrayOrDictionary |
|
6614 |
at:next |
|
6615 |
ifAbsent:[ |
|
6616 |
"try symbol instead of character" |
|
6617 |
argArrayOrDictionary |
|
6618 |
at:next asSymbol |
|
6619 |
ifAbsent:[String with:escapeCharacter with:next]. |
|
6620 |
]. |
|
6621 |
] ifFalse:[ |
|
6622 |
v := String with:$% with:next. |
|
6623 |
]. |
|
6624 |
] |
|
6625 |
]. |
|
6626 |
"/ v notNil ifTrue:[ |
|
6627 |
v isBlock ifTrue:[ |
|
6628 |
v := v value |
|
6629 |
]. |
|
6630 |
||
6631 |
v printOn:aStream. |
|
6632 |
"/ ]. |
|
6633 |
] |
|
6634 |
]. |
|
6635 |
start := idx + 2 |
|
14765 | 6636 |
]. |
6637 |
||
6638 |
" |
|
6639 |
String streamContents:[:s| |
|
20874 | 6640 |
'hello %1' expandPlaceholders:$% with:#('world') on:s. |
6641 |
s cr. |
|
6642 |
'hello $1; how is $2' expandPlaceholders:$$ with:#('world' 'this') on:s. |
|
6643 |
s cr. |
|
6644 |
'hello %2; how is %1' expandPlaceholders:$% with:#('world' 'this') on:s. |
|
6645 |
s cr. |
|
6646 |
'%1 plus %2 gives %3 ' expandPlaceholders:$% with:#(4 5 9) on:s. |
|
6647 |
s cr. |
|
6648 |
'%%(1)0 gives %(1)0' expandPlaceholders:$% with:#(123) on:s. |
|
6649 |
s cr. |
|
6650 |
'%%10 gives %10' expandPlaceholders:$% with:#(123) on:s. |
|
6651 |
s cr. |
|
6652 |
'%%(10) gives %(10) %<cr>%<tab>next line' expandPlaceholders:$% with:#(123) on:s. |
|
6653 |
s cr. |
|
6654 |
'%%test gives %test' expandPlaceholders:$% with:#(123) on:s. |
|
6655 |
s cr. |
|
6656 |
'|%%<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
|
6657 |
] |
14765 | 6658 |
" |
6659 |
||
6660 |
" |
|
6661 |
|dict| |
|
6662 |
||
6663 |
dict := Dictionary new. |
|
6664 |
dict at:1 put:'one'. |
|
6665 |
dict at:$a put:'AAAAA'. |
|
6666 |
dict at:$b put:[ Time now ]. |
|
6667 |
String streamContents:[:s| |
|
20874 | 6668 |
'hello $1 $a $b' expandPlaceholders:$$ with:dict on:s. |
14765 | 6669 |
]. |
6670 |
" |
|
6671 |
||
18425 | 6672 |
"using blocks: |
6673 |
|dict| |
|
6674 |
||
6675 |
dict := Dictionary new. |
|
6676 |
dict at:'time' put:[Time now printString]. |
|
6677 |
dict at:'date' put:[Date today printString]. |
|
6678 |
String streamContents:[:s| |
|
20874 | 6679 |
'it is $(time) $(date)' expandPlaceholders:$$ with:dict on:s. |
18425 | 6680 |
]. |
6681 |
" |
|
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
6682 |
|
14765 | 6683 |
"Modified: / 18-11-2010 / 15:43:28 / cg" |
6684 |
! |
|
6685 |
||
9192 | 6686 |
expandPlaceholdersWith:argArrayOrDictionary |
608 | 6687 |
"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
|
6688 |
replaced by corresponding arguments' printStrings from the argArrayOrDictionary. |
608 | 6689 |
I.e. 'hello %1; how is %2' expandPlaceholdersWith:#('world' 'this') results |
6690 |
in the new string 'hello world; how is this'. |
|
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
6691 |
|
4410
2bdc0c89a42e
enhanced #exapndPlaceHolders, to allow for a dictionary arg
Claus Gittinger <cg@exept.de>
parents:
4409
diff
changeset
|
6692 |
As an extension, the argument may also be a dictionary, providing |
7313 | 6693 |
values for symbolic keys. |
4842 | 6694 |
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
|
6695 |
(%1..%9 require a numeric key in the dictionary, however) |
18425 | 6696 |
Also, the values in argArrayOrDictionary may be blocks. |
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
6697 |
|
608 | 6698 |
To get a '%' character, use a '%%'-escape. |
7313 | 6699 |
To get an integer-indexed placeHolder followed by another digit, |
6700 |
or an index > 9, you must use %(digit). |
|
608 | 6701 |
See also bindWith:... for VisualAge compatibility." |
6702 |
||
10906
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6703 |
|stream| |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6704 |
|
18269 | 6705 |
stream := (TextStream ? CharacterWriteStream) on:(self species uninitializedNew:self size + 20). |
10906
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6706 |
self expandPlaceholdersWith:argArrayOrDictionary on:stream. |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6707 |
^ stream contents. |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6708 |
|
608 | 6709 |
" |
8874 | 6710 |
'hello %1' expandPlaceholdersWith:#('world') |
6711 |
'hello %1; how is %2' expandPlaceholdersWith:#('world' 'this') |
|
6712 |
'hello %2; how is %1' expandPlaceholdersWith:#('world' 'this') |
|
6713 |
'%1 plus %2 gives %3 ' expandPlaceholdersWith:#(4 5 9) |
|
6714 |
'%%(1)0 gives %(1)0' expandPlaceholdersWith:#(123) |
|
6715 |
'%%10 gives %10' expandPlaceholdersWith:#(123) |
|
6716 |
'%%(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
|
6717 |
'%test gives %1' expandPlaceholdersWith:#(123) |
11028
cb1ee3ddd91a
expandPlaceHolders: care fr Text with emphasis
Claus Gittinger <cg@exept.de>
parents:
11018
diff
changeset
|
6718 |
'bla %1 bla' expandPlaceholdersWith:{ 'hello' allBold } |
cb1ee3ddd91a
expandPlaceHolders: care fr Text with emphasis
Claus Gittinger <cg@exept.de>
parents:
11018
diff
changeset
|
6719 |
'bla %1 bla' expandPlaceholdersWith:{ 'hello' } |
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
6720 |
('bla %1 bla' withColor:Color red) |
20874 | 6721 |
expandPlaceholdersWith:{ 'hello' } |
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
6722 |
('bla %1 bla' withColor:Color red) |
20874 | 6723 |
expandPlaceholdersWith:{ 'hello' withColor:Color blue } |
608 | 6724 |
" |
1026
455c9f609f4d
care for 16bit strings in expandPlaceHolders
Claus Gittinger <cg@exept.de>
parents:
1025
diff
changeset
|
6725 |
|
4410
2bdc0c89a42e
enhanced #exapndPlaceHolders, to allow for a dictionary arg
Claus Gittinger <cg@exept.de>
parents:
4409
diff
changeset
|
6726 |
" |
2bdc0c89a42e
enhanced #exapndPlaceHolders, to allow for a dictionary arg
Claus Gittinger <cg@exept.de>
parents:
4409
diff
changeset
|
6727 |
|dict| |
2bdc0c89a42e
enhanced #exapndPlaceHolders, to allow for a dictionary arg
Claus Gittinger <cg@exept.de>
parents:
4409
diff
changeset
|
6728 |
|
2bdc0c89a42e
enhanced #exapndPlaceHolders, to allow for a dictionary arg
Claus Gittinger <cg@exept.de>
parents:
4409
diff
changeset
|
6729 |
dict := Dictionary new. |
2bdc0c89a42e
enhanced #exapndPlaceHolders, to allow for a dictionary arg
Claus Gittinger <cg@exept.de>
parents:
4409
diff
changeset
|
6730 |
dict at:1 put:'one'. |
2bdc0c89a42e
enhanced #exapndPlaceHolders, to allow for a dictionary arg
Claus Gittinger <cg@exept.de>
parents:
4409
diff
changeset
|
6731 |
dict at:$a put:'AAAAA'. |
8794
a5df33935142
nindWith supports blocks in the value dictionary
Claus Gittinger <cg@exept.de>
parents:
8710
diff
changeset
|
6732 |
dict at:$b put:[ Time now ]. |
12316
3e78905d70a9
comment/format in: #expandPlaceholdersWith:
Claus Gittinger <cg@exept.de>
parents:
12196
diff
changeset
|
6733 |
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
|
6734 |
'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
|
6735 |
" |
2bdc0c89a42e
enhanced #exapndPlaceHolders, to allow for a dictionary arg
Claus Gittinger <cg@exept.de>
parents:
4409
diff
changeset
|
6736 |
|
2737
9f29aecea39d
care for % at end in expandPlaceHolders.
Claus Gittinger <cg@exept.de>
parents:
2736
diff
changeset
|
6737 |
"Modified: 1.7.1997 / 00:53:24 / cg" |
9192 | 6738 |
! |
6739 |
||
10906
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6740 |
expandPlaceholdersWith:argArrayOrDictionary on:aStream |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6741 |
"write the receiver to aStream, where all %i escapes are |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6742 |
replaced by corresponding arguments' printStrings from the argArrayOrDictionary. |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6743 |
I.e. 'hello %1; how is %2' expandPlaceholdersWith:#('world' 'this') results |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6744 |
in the new string 'hello world; how is this'. |
18425 | 6745 |
|
10906
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6746 |
As an extension, the argument may also be a dictionary, providing |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6747 |
values for symbolic keys. |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6748 |
In this case, %a .. %z and %(...) are also allowed. |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6749 |
(%1..%9 require a numeric key in the dictionary, however) |
18425 | 6750 |
Also, the values in argArrayOrDictionary may be blocks. |
6751 |
||
10906
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6752 |
To get a '%' character, use a '%%'-escape. |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6753 |
To get an integer-indexed placeHolder followed by another digit, |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6754 |
or an index > 9, you must use %(digit). |
18425 | 6755 |
|
10935 | 6756 |
See also bindWith:... for VisualAge compatibility. |
10939
d0ca31c1d1e9
#expandPlaceHoldersWith: handle %<cr> %<tab> etc
Stefan Vogel <sv@exept.de>
parents:
10935
diff
changeset
|
6757 |
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
|
6758 |
|
14765 | 6759 |
^ self expandPlaceholders:$% with:argArrayOrDictionary on:aStream |
10906
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6760 |
|
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6761 |
" |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6762 |
String streamContents:[:s| |
20874 | 6763 |
'hello %1' expandPlaceholdersWith:#('world') on:s. |
6764 |
s cr. |
|
6765 |
'hello %1; how is %2' expandPlaceholdersWith:#('world' 'this') on:s. |
|
6766 |
s cr. |
|
6767 |
'hello %2; how is %1' expandPlaceholdersWith:#('world' 'this') on:s. |
|
6768 |
s cr. |
|
6769 |
'%1 plus %2 gives %3 ' expandPlaceholdersWith:#(4 5 9) on:s. |
|
6770 |
s cr. |
|
6771 |
'%%(1)0 gives %(1)0' expandPlaceholdersWith:#(123) on:s. |
|
6772 |
s cr. |
|
6773 |
'%%10 gives %10' expandPlaceholdersWith:#(123) on:s. |
|
6774 |
s cr. |
|
6775 |
'%%(10) gives %(10) %<cr>%<tab>next line' expandPlaceholdersWith:#(123) on:s. |
|
6776 |
s cr. |
|
6777 |
'%test gives %1' expandPlaceholdersWith:#(123) on:s. |
|
10906
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6778 |
] |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6779 |
" |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6780 |
|
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6781 |
" |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6782 |
|dict| |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6783 |
|
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6784 |
dict := Dictionary new. |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6785 |
dict at:1 put:'one'. |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6786 |
dict at:$a put:'AAAAA'. |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6787 |
dict at:$b put:[ Time now ]. |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6788 |
String streamContents:[:s| |
20874 | 6789 |
'hello %1 %a %b' expandPlaceholdersWith:dict on:s. |
10906
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6790 |
]. |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6791 |
" |
13008
a27647c36b31
changed: #expandPlaceholdersWith:on:
Claus Gittinger <cg@exept.de>
parents:
12925
diff
changeset
|
6792 |
|
13138
d2eb0eba5907
changed: #expandPlaceholdersWith:on:
Claus Gittinger <cg@exept.de>
parents:
13137
diff
changeset
|
6793 |
"Modified: / 18-11-2010 / 15:43:28 / cg" |
10906
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6794 |
! |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6795 |
|
9230 | 6796 |
firstLine |
14405
1d0d99a95bba
comment/format in:7 methods
Claus Gittinger <cg@exept.de>
parents:
14168
diff
changeset
|
6797 |
"return the first line of a multiline string" |
1d0d99a95bba
comment/format in:7 methods
Claus Gittinger <cg@exept.de>
parents:
14168
diff
changeset
|
6798 |
|
1d0d99a95bba
comment/format in:7 methods
Claus Gittinger <cg@exept.de>
parents:
14168
diff
changeset
|
6799 |
^ self asCollectionOfSubCollectionsSeparatedBy:(Character cr) do:[:line | ^ line]. |
9230 | 6800 |
|
6801 |
" |
|
10703
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
6802 |
'hello' firstLine |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
6803 |
'1\2\3' withCRs firstLine |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
6804 |
'\1\2\3' withCRs firstLine |
9230 | 6805 |
" |
6806 |
! |
|
6807 |
||
13987 | 6808 |
tokensBasedOn:aCharacter |
6809 |
"this is an ST-80 alias for the ST/X method |
|
20874 | 6810 |
asCollectionOfSubstringsSeparatedBy:" |
13987 | 6811 |
|
6812 |
^ self asCollectionOfSubstringsSeparatedBy:aCharacter |
|
6813 |
||
6814 |
" |
|
6815 |
'hello:world:isnt:this nice' tokensBasedOn:$: |
|
6816 |
'foo,bar,baz' tokensBasedOn:$, |
|
6817 |
'/etc/passwd' asFilename readStream nextLine tokensBasedOn:$: |
|
6818 |
" |
|
6819 |
! |
|
6820 |
||
6821 |
withCEscapes |
|
16863 | 6822 |
"return a new string consisting of receiver's characters |
13987 | 6823 |
with all special and unprintable characters replaced by \X-character escapes. |
6824 |
(similar to the way C-language literal Strings are represented). |
|
6825 |
The resulting string will contain only 7-bit ascii characters. |
|
6826 |
Emphasis is not supported. |
|
6827 |
The following escapes are generated: |
|
20874 | 6828 |
\' single quote character |
6829 |
\dQuote double quote character |
|
6830 |
\r return character |
|
6831 |
\r return character |
|
6832 |
\n newline character |
|
6833 |
\t tab character |
|
6834 |
\\ the \ character itself |
|
6835 |
\xnn two digit hex number defining the characters ascii value |
|
6836 |
\unnnn four digit hex number defining the characters ascii value |
|
6837 |
\Unnnnnnnn eight digit hex number defining the characters ascii value |
|
16863 | 6838 |
This is the opposite of withoutCEscapes. |
17000 | 6839 |
|
6840 |
Sigh: this is named completely wrong (opposite naming of withCRs/witoutCRs), |
|
20874 | 6841 |
but it cannot be changed easily, as these methods are already used heavily |
13987 | 6842 |
" |
6843 |
||
6844 |
|anyEscapeNeeded out seq| |
|
6845 |
||
6846 |
" |
|
6847 |
first, check if any escape is needed and return the receiver unchanged if not |
|
6848 |
" |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
6849 |
anyEscapeNeeded := self |
20874 | 6850 |
contains:[:ch | |
6851 |
((ch codePoint between:32 and:126) not |
|
6852 |
or:[ch == $' or:[ch == $"]]) |
|
6853 |
]. |
|
13987 | 6854 |
anyEscapeNeeded ifFalse:[ ^ self ]. |
6855 |
||
6856 |
self hasChangeOfEmphasis ifTrue:[ self error:'emphasis not supported' ]. |
|
6857 |
||
18269 | 6858 |
out := WriteStream on:(String uninitializedNew:self size-1). |
13987 | 6859 |
|
6860 |
self do:[:ch | |
|
20874 | 6861 |
|cp| |
6862 |
||
6863 |
(ch == $' or:[ch == $"]) ifTrue:[ |
|
6864 |
out nextPut:$\. |
|
6865 |
out nextPut:ch. |
|
6866 |
] ifFalse:[ |
|
6867 |
(ch codePoint between:32 and:126) ifTrue:[ |
|
6868 |
out nextPut:ch |
|
6869 |
] ifFalse:[ |
|
6870 |
ch == Character return ifTrue:[ |
|
6871 |
seq := '\r' |
|
6872 |
] ifFalse:[ ch == Character nl ifTrue:[ |
|
6873 |
seq := '\n' |
|
6874 |
] ifFalse:[ ch == Character tab ifTrue:[ |
|
6875 |
seq := '\t' |
|
6876 |
] ifFalse:[ ch == $\ ifTrue:[ |
|
6877 |
seq := '\\' |
|
6878 |
] ifFalse:[ |
|
6879 |
cp := ch codePoint. |
|
6880 |
cp <= 16rFF ifTrue:[ |
|
6881 |
seq := '\x' , (cp printStringRadix:16 padTo:2) |
|
6882 |
] ifFalse:[ |
|
6883 |
cp <= 16rFFFF ifTrue:[ |
|
6884 |
seq := '\u' , (cp printStringRadix:16 padTo:4) |
|
6885 |
] ifFalse:[ |
|
6886 |
seq := '\U',(cp printStringRadix:16 padTo:8) |
|
6887 |
] |
|
6888 |
] |
|
6889 |
]]]]. |
|
6890 |
out nextPutAll:seq |
|
6891 |
]. |
|
6892 |
]. |
|
13987 | 6893 |
]. |
6894 |
^ out contents |
|
6895 |
||
6896 |
" |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
6897 |
'hello\n\tworld' withoutCEscapes. |
17000 | 6898 |
'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
|
6899 |
('hello ',(Character value:16r1234),' world') withCEscapes |
13987 | 6900 |
" |
6901 |
||
6902 |
"Created: / 25-01-2012 / 11:08:16 / cg" |
|
6903 |
! |
|
6904 |
||
9192 | 6905 |
withCRs |
17205 | 6906 |
"return a new string consisting of the receiver's characters |
16757 | 6907 |
with all \-characters replaced by cr-characters. |
6908 |
If there are no backslashes, return the original" |
|
6909 |
||
16759 | 6910 |
^ self copyReplaceAll:$\ with:(Character cr) ifNone:self |
9192 | 6911 |
|
6912 |
" |
|
6913 |
'hello\world' withCRs |
|
6914 |
" |
|
6915 |
||
6916 |
"Modified: / 18.7.1998 / 22:53:02 / cg" |
|
6917 |
! |
|
6918 |
||
6919 |
withEscapes |
|
15405 | 6920 |
<resource: #obsolete> |
13987 | 6921 |
"has been renamed; the name withEscapes is misleading" |
6922 |
||
6923 |
self obsoleteMethodWarning:'use withoutCEscapes'. |
|
6924 |
^ self withoutCEscapes. |
|
6925 |
||
6926 |
"Modified: / 25-01-2012 / 10:42:30 / cg" |
|
9192 | 6927 |
! |
6928 |
||
6929 |
withMatchEscapes |
|
4117 | 6930 |
"return a copy of the receiver with all match characters escaped |
6931 |
by $\ characters (to be usable as a match string). |
|
6932 |
Return the receiver, if there are none." |
|
6933 |
||
5342
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
6934 |
|in out c escape| |
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
6935 |
|
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
6936 |
escape := self class matchEscapeCharacter. |
4117 | 6937 |
|
6938 |
in := self readStream. |
|
7986 | 6939 |
out := WriteStream on:(self species new:self size). |
4117 | 6940 |
[in atEnd] whileFalse:[ |
20874 | 6941 |
c := in next. |
6942 |
(c == escape or:['*[#' includes:c]) ifTrue:[ |
|
6943 |
out nextPut:$\. |
|
6944 |
]. |
|
6945 |
out nextPut:c. |
|
4117 | 6946 |
]. |
6947 |
^ out contents. |
|
6948 |
||
6949 |
" |
|
8874 | 6950 |
'*foo' withMatchEscapes |
6951 |
'\*foo' withMatchEscapes |
|
6952 |
'*foo' withMatchEscapes |
|
6953 |
'\\*foo' withMatchEscapes |
|
6954 |
'foo*' withMatchEscapes |
|
6955 |
'foo\*' withMatchEscapes |
|
6956 |
'foo\' withMatchEscapes |
|
6957 |
'f*o*o' withMatchEscapes |
|
4117 | 6958 |
" |
6959 |
||
6960 |
"Modified: 2.4.1997 / 18:13:04 / cg" |
|
9192 | 6961 |
! |
6962 |
||
14992 | 6963 |
withSeparatorsCompacted |
6964 |
"return a new string with each sequence of whiteSpace replaced by a single space character. |
|
6965 |
Preserves a leading/trailing space." |
|
6966 |
||
6967 |
^ self species streamContents:[:s | |
|
20874 | 6968 |
|skipping| |
6969 |
||
6970 |
skipping := false. |
|
6971 |
1 to:self size do:[:idx | |
|
6972 |
|char| |
|
6973 |
||
6974 |
char := self at:idx. |
|
6975 |
char isSeparator ifFalse:[ |
|
6976 |
s nextPut:char. |
|
6977 |
skipping := false. |
|
6978 |
] ifTrue:[ |
|
6979 |
skipping ifFalse:[ |
|
6980 |
s nextPut:(Character space). |
|
6981 |
skipping := true |
|
6982 |
]. |
|
6983 |
] |
|
6984 |
] |
|
14992 | 6985 |
] |
6986 |
||
6987 |
" |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
6988 |
'hello wwww' withSeparatorsCompacted |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
6989 |
'hello wwww' withSeparatorsCompacted |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
6990 |
' hello wwww' withSeparatorsCompacted |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
6991 |
' hello wwww ' withSeparatorsCompacted |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
6992 |
' hello wwww ' withSeparatorsCompacted |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
6993 |
'hel lo www w' withSeparatorsCompacted |
14992 | 6994 |
" |
6995 |
! |
|
6996 |
||
18464 | 6997 |
withSeparatorsReplacedBy:replacementCharacter |
6998 |
"return a new string with each separator (whitespace) replaced by replacementCharacter. |
|
6999 |
Typically used with space as replacementCharacter" |
|
7000 |
||
7001 |
^ self species streamContents:[:s | |
|
20874 | 7002 |
self do:[:ch | |
7003 |
ch isSeparator ifTrue:[ |
|
7004 |
s nextPut:replacementCharacter |
|
7005 |
] ifFalse:[ |
|
7006 |
s nextPut:ch. |
|
7007 |
] |
|
7008 |
] |
|
18464 | 7009 |
] |
7010 |
||
7011 |
" |
|
7012 |
'hello wwww' withSeparatorsReplacedBy:$* |
|
7013 |
'hello ww ww' withSeparatorsReplacedBy:$* |
|
7014 |
' hello wwww' withSeparatorsReplacedBy:$* |
|
7015 |
'hel lo www w' withSeparatorsReplacedBy:$* |
|
7016 |
'hel |
|
7017 |
lo www |
|
7018 |
w' withSeparatorsReplacedBy:$* |
|
7019 |
" |
|
7020 |
! |
|
7021 |
||
9192 | 7022 |
withTabs |
19464 | 7023 |
"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
|
7024 |
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
|
7025 |
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
|
7026 |
otherwise a new string is returned. |
8874 | 7027 |
Limitation: only the very first spaces are replaced |
20874 | 7028 |
(i.e. if the receiver contains newLine characters, |
7029 |
no tabs are inserted after those lineBreaks)" |
|
8874 | 7030 |
|
7031 |
|idx "{ SmallInteger }" |
|
7032 |
nTabs "{ SmallInteger }" |
|
608 | 7033 |
newString| |
7034 |
||
7035 |
idx := self findFirst:[:c | (c ~~ Character space)]. |
|
7036 |
nTabs := (idx-1) // 8. |
|
5815
4acdd87a77d4
// can return negative numbers.
Stefan Vogel <sv@exept.de>
parents:
5795
diff
changeset
|
7037 |
nTabs <= 0 ifTrue:[^ self]. |
608 | 7038 |
|
7039 |
"any tabs" |
|
15523 | 7040 |
newString := self species new:(self size - (nTabs * 7)). |
608 | 7041 |
newString atAll:(1 to:nTabs) put:(Character tab). |
7042 |
newString replaceFrom:(nTabs + 1) with:self startingAt:(nTabs * 8 + 1). |
|
7043 |
^ newString |
|
7044 |
||
7045 |
" |
|
8874 | 7046 |
'12345678901234567890' withTabs |
7047 |
' 8901234567890' withTabs |
|
7048 |
' 901234567890' withTabs |
|
608 | 7049 |
' 67890' withTabs |
7050 |
' 7890' withTabs |
|
7051 |
' 890' withTabs |
|
7052 |
" |
|
9192 | 7053 |
! |
7054 |
||
7055 |
withTabsExpanded |
|
19464 | 7056 |
"return a string consisting of the receiver's characters, |
8874 | 7057 |
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
|
7058 |
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
|
7059 |
otherwise a new string is returned. |
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
678
diff
changeset
|
7060 |
This does handle multiline strings." |
608 | 7061 |
|
5741
3a6086774d38
added #withTabsExpanded: (i.e. arg to specify tab-column width)
Claus Gittinger <cg@exept.de>
parents:
5739
diff
changeset
|
7062 |
^ self withTabsExpanded:8 |
3a6086774d38
added #withTabsExpanded: (i.e. arg to specify tab-column width)
Claus Gittinger <cg@exept.de>
parents:
5739
diff
changeset
|
7063 |
|
3a6086774d38
added #withTabsExpanded: (i.e. arg to specify tab-column width)
Claus Gittinger <cg@exept.de>
parents:
5739
diff
changeset
|
7064 |
" |
8874 | 7065 |
('1' , Character tab asString , 'x') withTabsExpanded |
7066 |
('12345' , Character tab asString , 'x') withTabsExpanded |
|
7067 |
('123456' , Character tab asString , 'x') withTabsExpanded |
|
7068 |
('1234567' , Character tab asString , 'x') withTabsExpanded |
|
7069 |
('12345678' , Character tab asString , 'x') withTabsExpanded |
|
7070 |
('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
|
7071 |
|
3a6086774d38
added #withTabsExpanded: (i.e. arg to specify tab-column width)
Claus Gittinger <cg@exept.de>
parents:
5739
diff
changeset
|
7072 |
(String with:Character tab |
20874 | 7073 |
with:Character tab |
7074 |
with:$1) withTabsExpanded |
|
5741
3a6086774d38
added #withTabsExpanded: (i.e. arg to specify tab-column width)
Claus Gittinger <cg@exept.de>
parents:
5739
diff
changeset
|
7075 |
|
3a6086774d38
added #withTabsExpanded: (i.e. arg to specify tab-column width)
Claus Gittinger <cg@exept.de>
parents:
5739
diff
changeset
|
7076 |
(String with:Character tab |
20874 | 7077 |
with:$1 |
7078 |
with:Character tab |
|
7079 |
with:$2) withTabsExpanded |
|
5741
3a6086774d38
added #withTabsExpanded: (i.e. arg to specify tab-column width)
Claus Gittinger <cg@exept.de>
parents:
5739
diff
changeset
|
7080 |
|
3a6086774d38
added #withTabsExpanded: (i.e. arg to specify tab-column width)
Claus Gittinger <cg@exept.de>
parents:
5739
diff
changeset
|
7081 |
(String with:Character tab |
20874 | 7082 |
with:$1 |
7083 |
with:Character cr |
|
7084 |
with:Character tab |
|
7085 |
with:$2) withTabsExpanded |
|
5741
3a6086774d38
added #withTabsExpanded: (i.e. arg to specify tab-column width)
Claus Gittinger <cg@exept.de>
parents:
5739
diff
changeset
|
7086 |
" |
3a6086774d38
added #withTabsExpanded: (i.e. arg to specify tab-column width)
Claus Gittinger <cg@exept.de>
parents:
5739
diff
changeset
|
7087 |
|
3a6086774d38
added #withTabsExpanded: (i.e. arg to specify tab-column width)
Claus Gittinger <cg@exept.de>
parents:
5739
diff
changeset
|
7088 |
"Modified: 12.5.1996 / 13:05:10 / cg" |
9192 | 7089 |
! |
7090 |
||
7091 |
withTabsExpanded:numSpaces |
|
19464 | 7092 |
"return a string consisting of the receiver's characters, |
8874 | 7093 |
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
|
7094 |
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
|
7095 |
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
|
7096 |
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
|
7097 |
|
8874 | 7098 |
|col "{ SmallInteger }" |
1381 | 7099 |
str ch |
7100 |
dstIdx "{ SmallInteger }" |
|
7101 |
newSz "{ SmallInteger }" |
|
7102 |
sz "{ SmallInteger }" |
|
7103 |
hasEmphasis e| |
|
608 | 7104 |
|
7105 |
(self includes:(Character tab)) ifFalse:[^ self]. |
|
1381 | 7106 |
|
7107 |
sz := self size. |
|
7108 |
||
7109 |
"/ count the new size first, instead of |
|
7110 |
"/ multiple resizing (better for large strings) |
|
7111 |
||
7112 |
col := 1. newSz := 0. |
|
7113 |
1 to:sz do:[:srcIdx | |
|
20874 | 7114 |
ch := self at:srcIdx. |
7115 |
ch == Character tab ifFalse:[ |
|
7116 |
col := col + 1. |
|
7117 |
newSz := newSz + 1. |
|
7118 |
ch == Character cr ifTrue:[ |
|
7119 |
col := 1 |
|
7120 |
]. |
|
7121 |
] ifTrue:[ |
|
7122 |
(col \\ numSpaces) to:numSpaces do:[:ii | |
|
7123 |
newSz := newSz + 1. |
|
7124 |
col := col + 1 |
|
7125 |
]. |
|
7126 |
] |
|
608 | 7127 |
]. |
1381 | 7128 |
|
7129 |
str := self species new:newSz. |
|
7130 |
||
7131 |
hasEmphasis := self hasChangeOfEmphasis. |
|
7132 |
||
7133 |
col := 1. dstIdx := 1. |
|
7134 |
1 to:sz do:[:srcIdx | |
|
20874 | 7135 |
ch := self at:srcIdx. |
7136 |
||
7137 |
ch == Character tab ifFalse:[ |
|
7138 |
col := col + 1. |
|
7139 |
ch == Character cr ifTrue:[ |
|
7140 |
col := 1 |
|
7141 |
]. |
|
7142 |
hasEmphasis ifTrue:[ |
|
7143 |
e := self emphasisAt:srcIdx. |
|
7144 |
str emphasisAt:dstIdx put:e |
|
7145 |
]. |
|
7146 |
str at:dstIdx put:ch. |
|
7147 |
dstIdx := dstIdx + 1 |
|
7148 |
] ifTrue:[ |
|
7149 |
(col \\ numSpaces) to:numSpaces do:[:ii | |
|
7150 |
str at:dstIdx put:Character space. |
|
7151 |
dstIdx := dstIdx + 1. |
|
7152 |
col := col + 1 |
|
7153 |
]. |
|
7154 |
] |
|
1381 | 7155 |
]. |
7156 |
^ str |
|
608 | 7157 |
|
7158 |
" |
|
8874 | 7159 |
('1' , Character tab asString , 'x') withTabsExpanded |
7160 |
('1' , Character tab asString , 'x') withTabsExpanded:4 |
|
7161 |
('12345' , Character tab asString , 'x') withTabsExpanded |
|
7162 |
('123456' , Character tab asString , 'x') withTabsExpanded |
|
7163 |
('1234567' , Character tab asString , 'x') withTabsExpanded |
|
7164 |
('12345678' , Character tab asString , 'x') withTabsExpanded |
|
7165 |
('123456789' , Character tab asString , 'x') withTabsExpanded |
|
735
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
678
diff
changeset
|
7166 |
|
608 | 7167 |
(String with:Character tab |
20874 | 7168 |
with:Character tab |
7169 |
with:$1) withTabsExpanded |
|
608 | 7170 |
|
7171 |
(String with:Character tab |
|
20874 | 7172 |
with:$1 |
7173 |
with:Character tab |
|
7174 |
with:$2) withTabsExpanded |
|
735
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
678
diff
changeset
|
7175 |
|
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
678
diff
changeset
|
7176 |
(String with:Character tab |
20874 | 7177 |
with:$1 |
7178 |
with:Character cr |
|
7179 |
with:Character tab |
|
7180 |
with:$2) withTabsExpanded |
|
608 | 7181 |
" |
735
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
678
diff
changeset
|
7182 |
|
1381 | 7183 |
"Modified: 12.5.1996 / 13:05:10 / cg" |
9192 | 7184 |
! |
7185 |
||
13987 | 7186 |
withoutAllSpaces |
16855 | 7187 |
"return a copy of the receiver with all whitespace removed" |
7188 |
||
13987 | 7189 |
^ self asCollectionOfWords asStringWith:''. |
7190 |
"/ |col string| |
|
7191 |
"/ |
|
7192 |
"/ col := self asCollectionOfWords. |
|
7193 |
"/ string := String new. |
|
7194 |
"/ col do:[:el | |
|
7195 |
"/ string := string,el |
|
7196 |
"/ ]. |
|
7197 |
"/ ^string |
|
7198 |
||
7199 |
" |
|
7200 |
'hello wwww' withoutAllSpaces |
|
7201 |
'hel lo www w' withoutAllSpaces |
|
7202 |
" |
|
7203 |
||
7204 |
"Modified: / 18.7.1998 / 22:53:08 / cg" |
|
7205 |
! |
|
7206 |
||
7207 |
withoutCEscapes |
|
15628 | 7208 |
"return a new string consisting of receiver's characters |
13987 | 7209 |
with all \X-character escapes replaced by corresponding-characters. |
7210 |
(similar to the way C-language Strings are converted). |
|
7211 |
The following escapes are supported: |
|
20874 | 7212 |
\r return character |
7213 |
\n newline character |
|
7214 |
\b backspace character |
|
7215 |
\f formfeed character |
|
7216 |
\t tab character |
|
7217 |
\e escape character |
|
7218 |
\\ the \ character itself |
|
7219 |
\nnn three digit octal number defining the characters ascii value |
|
7220 |
\xnn two digit hex number defining the characters ascii value |
|
7221 |
\unnnn four digit hex number defining the characters unicode value |
|
7222 |
\Unnnnnnnn eight digit hex number defining the characters unicode value |
|
7223 |
\other other |
|
13987 | 7224 |
|
7225 |
Notice, that \' is NOT a valid escape, since the general syntax of |
|
7226 |
string constants is not affected by this method. |
|
7227 |
||
7228 |
Although easily implementable, this is NOT done automatically |
|
7229 |
by the compiler (due to a lack of a language standard for this). |
|
7230 |
However, the compiler may detect sends ot #withEscapes to string literals |
|
7231 |
and place a modified string constant into the binary/byte-code. |
|
7232 |
Therefore, no runtime penalty will be payed for using these escapes. |
|
7233 |
(not in pre 2.11 versions) |
|
16863 | 7234 |
|
7235 |
This is the opposite of withCEscapes. |
|
17000 | 7236 |
|
7237 |
Sigh: this is named completely wrong (opposite naming of withCRs/witoutCRs), |
|
20874 | 7238 |
but it cannot be changed easily, as these methods are already used heavily |
13987 | 7239 |
" |
7240 |
||
7241 |
|val "{ SmallInteger }" |
|
7242 |
in out nextChar nDigits| |
|
7243 |
||
7244 |
" |
|
7245 |
first, see if there is any escape; if not, return the receiver unchanged |
|
7246 |
" |
|
7247 |
(self includes:$\) ifFalse:[^ self ]. |
|
7248 |
||
7249 |
self hasChangeOfEmphasis ifTrue:[ self error:'emphasis not supported' ]. |
|
7250 |
out := CharacterWriteStream on:(String new:self size - 1). |
|
7251 |
||
7252 |
in := ReadStream on:self. |
|
7253 |
[in atEnd] whileFalse:[ |
|
20874 | 7254 |
nextChar := in next. |
7255 |
nextChar == $\ ifTrue:[ |
|
7256 |
in atEnd ifTrue:[ |
|
7257 |
] ifFalse:[ |
|
7258 |
nextChar := in next. |
|
7259 |
nextChar == $r ifTrue:[ |
|
7260 |
nextChar := Character return |
|
7261 |
] ifFalse:[ nextChar == $n ifTrue:[ |
|
7262 |
nextChar := Character nl |
|
7263 |
] ifFalse:[ nextChar == $b ifTrue:[ |
|
7264 |
nextChar := Character backspace |
|
7265 |
] ifFalse:[ nextChar == $f ifTrue:[ |
|
7266 |
nextChar := Character newPage |
|
7267 |
] ifFalse:[ nextChar == $t ifTrue:[ |
|
7268 |
nextChar := Character tab |
|
7269 |
] ifFalse:[ nextChar == $e ifTrue:[ |
|
7270 |
nextChar := Character esc |
|
7271 |
] ifFalse:[ |
|
7272 |
nextChar == $0 ifTrue:[ |
|
7273 |
val := 0. |
|
7274 |
nextChar := in peek. |
|
7275 |
nDigits := 1. |
|
7276 |
[nextChar notNil and:[nextChar isDigit and:[nDigits <= 3]]] whileTrue:[ |
|
7277 |
val := (val * 8) + nextChar digitValue. |
|
7278 |
nextChar := in nextPeek. |
|
7279 |
nDigits := nDigits + 1. |
|
7280 |
]. |
|
7281 |
nextChar := Character value:val. |
|
7282 |
] ifFalse:[ |
|
7283 |
val := 0. |
|
7284 |
nextChar == $x ifTrue:[ |
|
7285 |
2 timesRepeat:[ |
|
7286 |
nextChar := in next. |
|
7287 |
val := (val * 16) + nextChar digitValue. |
|
7288 |
]. |
|
7289 |
nextChar := Character value:val. |
|
7290 |
] ifFalse:[ |
|
7291 |
nextChar == $u ifTrue:[ |
|
7292 |
4 timesRepeat:[ |
|
7293 |
nextChar := in next. |
|
7294 |
val := (val * 16) + nextChar digitValue. |
|
7295 |
]. |
|
7296 |
nextChar := Character value:val. |
|
7297 |
] ifFalse:[ |
|
7298 |
nextChar == $U ifTrue:[ |
|
7299 |
8 timesRepeat:[ |
|
7300 |
nextChar := in next. |
|
7301 |
val := (val * 16) + nextChar digitValue. |
|
7302 |
]. |
|
7303 |
nextChar := Character value:val. |
|
7304 |
] |
|
7305 |
] |
|
7306 |
] |
|
7307 |
] |
|
7308 |
]]]]]]. |
|
7309 |
]. |
|
7310 |
]. |
|
7311 |
out nextPut:nextChar. |
|
13987 | 7312 |
]. |
7313 |
^ out contents |
|
7314 |
||
7315 |
" |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
7316 |
'hello world' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
7317 |
'hello\world' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
7318 |
'hello\world\' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
7319 |
'hello world\' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
7320 |
'hello\tworld' withoutCEscapes |
13987 | 7321 |
'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
|
7322 |
'hello\tworld\n' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
7323 |
'hello\010world' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
7324 |
'hello\r\nworld' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
7325 |
'hello\r\n\x08world' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
7326 |
'0\x080' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
7327 |
'0\u12340' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
7328 |
'0\U123456780' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
7329 |
'0\0a' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
7330 |
'0\00a' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
7331 |
'0\000a' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
7332 |
'0\0000a' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
7333 |
'0\00000a' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
7334 |
'0\03770' withoutCEscapes |
13987 | 7335 |
" |
7336 |
||
7337 |
"Created: / 25-01-2012 / 10:41:44 / cg" |
|
7338 |
! |
|
7339 |
||
9192 | 7340 |
withoutCRs |
19464 | 7341 |
"return a new collection consisting of receiver's elements |
9192 | 7342 |
with all cr-characters replaced by \-characters. |
7343 |
This is the reverse operation of withCRs." |
|
7344 |
||
16759 | 7345 |
^ self copyReplaceAll:(Character cr) with:$\ ifNone:self |
12319
18b80c2ce1e3
oops #withoutSeparators #withoutSpaces were noops
Claus Gittinger <cg@exept.de>
parents:
12317
diff
changeset
|
7346 |
|
9192 | 7347 |
" |
7348 |
'hello |
|
7349 |
world' withoutCRs |
|
7350 |
" |
|
7351 |
||
7352 |
"Modified: / 18.7.1998 / 22:53:08 / cg" |
|
7353 |
! |
|
7354 |
||
7355 |
withoutLeadingSeparators |
|
608 | 7356 |
"return a copy of myself without leading separators. |
7357 |
Notice: this does remove tabs, newline or any other whitespace. |
|
7358 |
Returns an empty string, if the receiver consist only of whitespace." |
|
7359 |
||
7360 |
|index| |
|
7361 |
||
7362 |
index := self indexOfNonSeparatorStartingAt:1. |
|
7363 |
index ~~ 0 ifTrue:[ |
|
20874 | 7364 |
index == 1 ifTrue:[ |
7365 |
^ self |
|
7366 |
]. |
|
7367 |
^ self copyFrom:index |
|
608 | 7368 |
]. |
7369 |
^ '' |
|
7370 |
||
7371 |
" |
|
8874 | 7372 |
' foo ' withoutLeadingSeparators |
7373 |
'foo ' withoutLeadingSeparators |
|
7374 |
' foo' withoutLeadingSeparators |
|
7375 |
' ' withoutLeadingSeparators |
|
7376 |
'foo' withoutLeadingSeparators |
|
7377 |
(' ' , Character tab asString , ' foo ') withoutLeadingSeparators inspect |
|
608 | 7378 |
" |
9192 | 7379 |
! |
7380 |
||
7381 |
withoutMatchEscapes |
|
4117 | 7382 |
"return a copy of the receiver with all $\ removed or |
7383 |
the receiver, if there are none." |
|
7384 |
||
5342
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
7385 |
|in out c escape| |
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
7386 |
|
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
7387 |
escape := self class matchEscapeCharacter. |
4117 | 7388 |
|
7389 |
in := self readStream. |
|
8222
f2c454a9a038
replaced '' writeStream by String writeStream
Claus Gittinger <cg@exept.de>
parents:
8124
diff
changeset
|
7390 |
out := self species writeStream. |
4117 | 7391 |
[in atEnd] whileFalse:[ |
20874 | 7392 |
c := in next. |
7393 |
c == escape ifTrue:[ |
|
7394 |
in atEnd ifFalse:[ |
|
7395 |
c := in next. |
|
7396 |
] |
|
7397 |
]. |
|
7398 |
out nextPut:c. |
|
4117 | 7399 |
]. |
7400 |
^ out contents. |
|
7401 |
||
7402 |
" |
|
8874 | 7403 |
'*foo' withoutMatchEscapes |
7404 |
'\*foo' withoutMatchEscapes |
|
7405 |
'*foo' withoutMatchEscapes |
|
7406 |
'\\*foo' withoutMatchEscapes |
|
7407 |
'foo*' withoutMatchEscapes |
|
7408 |
'foo\*' withoutMatchEscapes |
|
7409 |
'foo\' withoutMatchEscapes |
|
7410 |
'f\*o\*o' withoutMatchEscapes |
|
4117 | 7411 |
" |
7412 |
||
7413 |
"Modified: 30.6.1997 / 13:40:23 / cg" |
|
9192 | 7414 |
! |
7415 |
||
7416 |
withoutPrefix:aString |
|
19677 | 7417 |
"if the receiver startsWith aString, return a copy without it. |
9192 | 7418 |
Otherwise return the receiver" |
7419 |
||
7420 |
(self startsWith:aString) ifTrue:[ |
|
20874 | 7421 |
^ self copyFrom:aString size+1 |
9192 | 7422 |
]. |
7423 |
^ self |
|
7424 |
||
7425 |
" |
|
7426 |
'helloworld' withoutPrefix:'hello' |
|
7427 |
'helloworld' withoutPrefix:'foo' |
|
7428 |
" |
|
19677 | 7429 |
|
7430 |
"Modified: / 30-04-2016 / 10:01:00 / cg" |
|
9192 | 7431 |
! |
7432 |
||
10671 | 7433 |
withoutQuotes |
17000 | 7434 |
"/ remove quotes ($" and $') from the front and end of myself (if matching)" |
7435 |
||
7436 |
|firstChar| |
|
7437 |
||
17024 | 7438 |
self isEmpty ifTrue:[^ self]. |
7439 |
||
17000 | 7440 |
firstChar := self first. |
7441 |
((firstChar == $") or:[firstChar == $']) ifFalse:[^ self]. |
|
7442 |
||
7443 |
self last == firstChar ifTrue:[ |
|
20874 | 7444 |
^ self copyFrom:2 to:(self size-1) |
10671 | 7445 |
]. |
17000 | 7446 |
^ self |
7447 |
||
7448 |
"/ |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
7449 |
"/ '"hello"' withoutQuotes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
7450 |
"/ '''hello''' withoutQuotes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
7451 |
"/ 'hello' withoutQuotes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
7452 |
"/ '"hello' withoutQuotes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
7453 |
"/ 'hello"' withoutQuotes |
17000 | 7454 |
"/ |
10671 | 7455 |
! |
7456 |
||
9192 | 7457 |
withoutSeparators |
608 | 7458 |
"return a copy of myself without leading and trailing whitespace. |
4156 | 7459 |
(but whiteSpace in-between is preserved) |
608 | 7460 |
Whitespace is space, tab, newline, formfeed. |
7461 |
Use withoutSpaces, if you want to remove spaces only." |
|
7462 |
||
12319
18b80c2ce1e3
oops #withoutSeparators #withoutSpaces were noops
Claus Gittinger <cg@exept.de>
parents:
12317
diff
changeset
|
7463 |
^ self trimForWhich:[:ch | ch isSeparator] |
608 | 7464 |
|
7465 |
" |
|
8874 | 7466 |
' foo ' withoutSeparators |
7467 |
' foo' withoutSeparators |
|
7468 |
'foo ' withoutSeparators |
|
7469 |
' ' withoutSeparators |
|
7470 |
(' foo' , Character tab asString , ' ') withoutSeparators inspect |
|
12319
18b80c2ce1e3
oops #withoutSeparators #withoutSpaces were noops
Claus Gittinger <cg@exept.de>
parents:
12317
diff
changeset
|
7471 |
' foo ' asUnicode16String withoutSeparators |
608 | 7472 |
" |
9192 | 7473 |
! |
7474 |
||
7475 |
withoutSpaces |
|
608 | 7476 |
"return a copy of myself without leading and trailing spaces. |
4156 | 7477 |
(but spaces in-between are preserved) |
608 | 7478 |
Notice: this does NOT remove tabs, newline or any other whitespace. |
7479 |
Use withoutSeparators for this." |
|
7480 |
||
12319
18b80c2ce1e3
oops #withoutSeparators #withoutSpaces were noops
Claus Gittinger <cg@exept.de>
parents:
12317
diff
changeset
|
7481 |
^ self trimForWhich:[:ch | ch == Character space] |
608 | 7482 |
|
7483 |
" |
|
8874 | 7484 |
' foo ' withoutSpaces |
7485 |
'foo ' withoutSpaces |
|
7486 |
' foo' withoutSpaces |
|
7487 |
' ' withoutSpaces |
|
7488 |
'a b' withoutSpaces |
|
7489 |
(' foo' , Character tab asString , ' ') withoutSpaces inspect |
|
608 | 7490 |
" |
9192 | 7491 |
! |
7492 |
||
7493 |
withoutTrailingSeparators |
|
915
1ac1c7e942f4
added withoutTrailingSeparators
Claus Gittinger <cg@exept.de>
parents:
828
diff
changeset
|
7494 |
"return a copy of myself without trailing separators. |
1ac1c7e942f4
added withoutTrailingSeparators
Claus Gittinger <cg@exept.de>
parents:
828
diff
changeset
|
7495 |
Notice: this does remove tabs, newline or any other whitespace. |
1ac1c7e942f4
added withoutTrailingSeparators
Claus Gittinger <cg@exept.de>
parents:
828
diff
changeset
|
7496 |
Returns an empty string, if the receiver consist only of whitespace." |
1ac1c7e942f4
added withoutTrailingSeparators
Claus Gittinger <cg@exept.de>
parents:
828
diff
changeset
|
7497 |
|
12188 | 7498 |
^ self withoutTrailingForWhich:[:ch | ch isSeparator] |
915
1ac1c7e942f4
added withoutTrailingSeparators
Claus Gittinger <cg@exept.de>
parents:
828
diff
changeset
|
7499 |
|
1ac1c7e942f4
added withoutTrailingSeparators
Claus Gittinger <cg@exept.de>
parents:
828
diff
changeset
|
7500 |
" |
8874 | 7501 |
' foo ' withoutTrailingSeparators |
7502 |
'foo ' withoutTrailingSeparators |
|
7503 |
' foo' withoutTrailingSeparators |
|
7504 |
' ' withoutTrailingSeparators |
|
7505 |
'foo' withoutTrailingSeparators |
|
7506 |
(' ' , Character tab asString , ' foo ') withoutTrailingSeparators inspect |
|
7507 |
(' foo' , Character tab asString) withoutTrailingSeparators inspect |
|
915
1ac1c7e942f4
added withoutTrailingSeparators
Claus Gittinger <cg@exept.de>
parents:
828
diff
changeset
|
7508 |
" |
9192 | 7509 |
! ! |
7510 |
||
20873 | 7511 |
|
9192 | 7512 |
!CharacterArray methodsFor:'substring searching'! |
7513 |
||
15055 | 7514 |
findRangeOfString:subString |
7515 |
"find a substring. if found, return the start- and endIndex; |
|
7516 |
if not found, return an empty interval." |
|
7517 |
||
7518 |
^ self rangeOfSubCollection:subString startingAt:1 ifAbsent:[0 to:-1] caseSensitive:true |
|
7519 |
||
7520 |
" |
|
7521 |
'hello world' findRangeOfString:'llo' |
|
7522 |
'hello world' findRangeOfString:'ole' |
|
7523 |
" |
|
7524 |
! |
|
7525 |
||
9192 | 7526 |
findString:subString |
7527 |
"find a substring. if found, return the index; |
|
7528 |
if not found, return 0." |
|
7529 |
||
7530 |
^ self indexOfSubCollection:subString startingAt:1 ifAbsent:0 caseSensitive:true |
|
7531 |
||
7532 |
" |
|
7533 |
'hello world' findString:'llo' |
|
7534 |
'hello world' findString:'ole' |
|
7535 |
" |
|
7536 |
! |
|
608 | 7537 |
|
15734 | 7538 |
findString:subString caseSensitive:caseSensitive |
7539 |
"find a substring. if found, return the index; |
|
7540 |
if not found, return 0." |
|
7541 |
||
7542 |
^ self indexOfSubCollection:subString startingAt:1 ifAbsent:0 caseSensitive:caseSensitive |
|
7543 |
||
7544 |
" |
|
7545 |
'hello world' findString:'LLo' caseSensitive:true |
|
7546 |
'hello world' findString:'LLo' caseSensitive:false |
|
7547 |
" |
|
7548 |
! |
|
7549 |
||
608 | 7550 |
findString:subString ifAbsent:exceptionBlock |
7551 |
"find a substring. If found, return the index; |
|
7552 |
if not found, return the result of evaluating exceptionBlock." |
|
7553 |
||
9087
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
7554 |
^ self indexOfSubCollection:subString startingAt:1 ifAbsent:exceptionBlock caseSensitive:true |
608 | 7555 |
! |
7556 |
||
11018 | 7557 |
findString:subString ignoreCase:ignoreCase |
15734 | 7558 |
<resource: #obsolete> |
11018 | 7559 |
"find a substring. if found, return the index; |
7560 |
if not found, return 0." |
|
7561 |
||
7562 |
^ self indexOfSubCollection:subString startingAt:1 ifAbsent:0 caseSensitive:ignoreCase not |
|
7563 |
||
7564 |
" |
|
7565 |
'hello world' findString:'LLo' ignoreCase:false |
|
7566 |
'hello world' findString:'LLo' ignoreCase:true |
|
7567 |
" |
|
7568 |
! |
|
7569 |
||
608 | 7570 |
findString:subString startingAt:index |
7571 |
"find a substring, starting at index. if found, return the index; |
|
7572 |
if not found, return 0." |
|
7573 |
||
9087
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
7574 |
^ self indexOfSubCollection:subString startingAt:index ifAbsent:0 caseSensitive:true |
608 | 7575 |
|
7576 |
" |
|
9192 | 7577 |
'hello yello' findString:'llo' startingAt:1 |
7578 |
'hello yello' findString:'llo' startingAt:5 |
|
7579 |
'hello yello' findString:'llo' startingAt:15 |
|
7580 |
" |
|
7581 |
! |
|
608 | 7582 |
|
7583 |
findString:subString startingAt:index ifAbsent:exceptionBlock |
|
7584 |
"find a substring, starting at index. if found, return the index; |
|
7585 |
if not found, return the result of evaluating exceptionBlock." |
|
7586 |
||
9087
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
7587 |
^ self indexOfSubCollection:subString startingAt:index ifAbsent:exceptionBlock caseSensitive:true |
608 | 7588 |
! |
7589 |
||
12177 | 7590 |
findString:subString startingAt:index ifAbsent:exceptionBlock caseSensitive:caseSensitive |
10929 | 7591 |
"find a substring, starting at index. if found, return the index; |
7592 |
if not found, return the result of evaluating exceptionBlock." |
|
7593 |
||
7594 |
^ self indexOfSubCollection:subString startingAt:index ifAbsent:exceptionBlock caseSensitive:caseSensitive |
|
7595 |
! |
|
7596 |
||
608 | 7597 |
includesString:aString |
7598 |
"return true, if a substring is contained in the receiver" |
|
7599 |
||
9087
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
7600 |
^ (self indexOfSubCollection:aString startingAt:1 ifAbsent:0 caseSensitive:true) ~~ 0 |
608 | 7601 |
|
7602 |
" |
|
12177 | 7603 |
'hello world' includesString:'hel' |
8874 | 7604 |
'hello world' includesString:'rld' |
9192 | 7605 |
'hello world' includesString:'llo' |
7606 |
'hello world' includesString:'LLO' |
|
12177 | 7607 |
'hello world' includesString:'' |
9192 | 7608 |
" |
7609 |
! |
|
9191 | 7610 |
|
7611 |
includesString:aString caseSensitive:caseSensitive |
|
9087
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
7612 |
"return true, if a substring is contained in the receiver" |
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
7613 |
|
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
7614 |
^ (self indexOfSubCollection:aString startingAt:1 ifAbsent:0 caseSensitive:caseSensitive) ~~ 0 |
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
7615 |
|
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
7616 |
" |
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
7617 |
'hello world' includesString:'hel' caseSensitive:true |
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
7618 |
'hello world' includesString:'HEL' caseSensitive:true |
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
7619 |
'hello world' includesString:'HEL' caseSensitive:false |
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
7620 |
|
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
7621 |
'hello world' includesString:'wor' caseSensitive:true |
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
7622 |
'hello world' includesString:'WOR' caseSensitive:true |
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
7623 |
'hello world' includesString:'WOR' caseSensitive:false |
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
7624 |
" |
9192 | 7625 |
! |
7626 |
||
17486 | 7627 |
indexOfString: aString |
7628 |
"VSE and V'age compatibility" |
|
7629 |
"find a substring. If found, return the index; if not found, return 0." |
|
7630 |
||
18939 | 7631 |
^ self indexOfSubCollection: aString startingAt:1 ifAbsent:[0] |
7632 |
! |
|
7633 |
||
7634 |
indexOfString:aString ifAbsent:exceptionValue |
|
7635 |
"VSE and V'age compatibility" |
|
7636 |
"find a substring. If found, return the index; if not found, return 0." |
|
7637 |
||
7638 |
^ self indexOfSubCollection: aString startingAt:1 ifAbsent:exceptionValue |
|
7639 |
||
7640 |
" |
|
7641 |
'abcdefabcdef' indexOfString:'fab' ifAbsent:[999] |
|
7642 |
'abcdefabcdef' indexOfString:'xxx' ifAbsent:[999] |
|
7643 |
" |
|
17486 | 7644 |
! |
7645 |
||
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
7646 |
indexOfString:aString startingAt:startIndex |
18721 | 7647 |
"VSE and V'age compatibility" |
7648 |
"find a substring. If found, return the index; if not found, return 0." |
|
7649 |
||
7650 |
^ self indexOfSubCollection:aString startingAt:startIndex ifAbsent:[0] |
|
7651 |
! |
|
7652 |
||
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
7653 |
indexOfString:aString startingAt:startIndex ifAbsent:exceptionalValue |
18721 | 7654 |
"VSE and V'age compatibility" |
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
7655 |
"find a substring. |
18721 | 7656 |
If found, return the index; if not found, the value from exceptionalValue." |
7657 |
||
7658 |
^ self indexOfSubCollection:aString startingAt:startIndex ifAbsent:exceptionalValue |
|
7659 |
! |
|
7660 |
||
18671 | 7661 |
indexOfSubCollection:subString caseSensitive:caseSensitive |
7662 |
"find a substring, starting at index. if found, return the index; |
|
7663 |
if not found, return the result of evaluating exceptionBlock. |
|
7664 |
This is a q&d hack - not very efficient" |
|
7665 |
||
7666 |
^ self indexOfSubCollection:subString startingAt:1 ifAbsent:[0] caseSensitive:caseSensitive. |
|
7667 |
! |
|
7668 |
||
9192 | 7669 |
indexOfSubCollection:subString startingAt:index ifAbsent:exceptionBlock |
7670 |
"find a substring, starting at index. if found, return the index; |
|
7671 |
if not found, return the result of evaluating exceptionBlock. |
|
7672 |
This is a q&d hack - not very efficient" |
|
7673 |
||
7674 |
^ self indexOfSubCollection:subString startingAt:index ifAbsent:exceptionBlock caseSensitive:true. |
|
7675 |
! |
|
7676 |
||
7677 |
indexOfSubCollection:subString startingAt:index ifAbsent:exceptionBlock caseSensitive:caseSensitive |
|
9087
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
7678 |
"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
|
7679 |
if not found, return the result of evaluating exceptionBlock. |
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
7680 |
This is a q&d hack - not very efficient" |
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
7681 |
|
608 | 7682 |
|firstChar found |
7683 |
startIndex "{ Class: SmallInteger }" |
|
7684 |
subSize "{ Class: SmallInteger }" |
|
7685 |
mySize "{ Class: SmallInteger }" |
|
10703
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
7686 |
runIdx "{ Class: SmallInteger }" |
9087
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
7687 |
tester| |
608 | 7688 |
|
7689 |
subSize := subString size. |
|
12177 | 7690 |
subSize == 0 ifTrue:[ |
20874 | 7691 |
subString isString ifFalse:[ |
7692 |
self error:'non string argument' mayProceed:true. |
|
7693 |
]. |
|
7694 |
"empty string does not match" |
|
7695 |
^ 0. |
|
7696 |
"empty string matches" |
|
15433 | 7697 |
"/ ^ index |
6122
a98958f1c85f
handle non-string arg to indexOfSubString:
Claus Gittinger <cg@exept.de>
parents:
6119
diff
changeset
|
7698 |
]. |
9087
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
7699 |
|
608 | 7700 |
mySize := self size. |
7701 |
firstChar := subString at:1. |
|
9087
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
7702 |
caseSensitive ifTrue:[ |
20874 | 7703 |
tester := [:c1 :c2 | c1 = c2 ]. |
7704 |
startIndex := self indexOf:firstChar startingAt:index. |
|
9087
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
7705 |
] ifFalse:[ |
20874 | 7706 |
tester := [:c1 :c2 | c1 sameAs: c2 ]. |
7707 |
startIndex := self findFirst:[:c | c sameAs:firstChar] startingAt:index. |
|
9087
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
7708 |
]. |
608 | 7709 |
[startIndex == 0] whileFalse:[ |
20874 | 7710 |
runIdx := startIndex. |
7711 |
found := true. |
|
7712 |
1 to:subSize do:[:i | |
|
7713 |
runIdx > mySize ifTrue:[ |
|
7714 |
found := false |
|
7715 |
] ifFalse:[ |
|
7716 |
(tester value:(subString at:i) value:(self at:runIdx)) ifFalse:[ |
|
7717 |
found := false |
|
7718 |
] |
|
7719 |
]. |
|
7720 |
runIdx := runIdx + 1 |
|
7721 |
]. |
|
7722 |
found ifTrue:[ |
|
7723 |
^ startIndex |
|
7724 |
]. |
|
7725 |
caseSensitive ifTrue:[ |
|
7726 |
startIndex := self indexOf:firstChar startingAt:(startIndex + 1) |
|
7727 |
] ifFalse:[ |
|
7728 |
startIndex := self findFirst:[:c | c sameAs:firstChar] startingAt:(startIndex + 1). |
|
7729 |
]. |
|
608 | 7730 |
]. |
7731 |
^ exceptionBlock value |
|
1005
4507487e0be6
more on encoding/decoding (still unfinished)
Claus Gittinger <cg@exept.de>
parents:
997
diff
changeset
|
7732 |
|
4507487e0be6
more on encoding/decoding (still unfinished)
Claus Gittinger <cg@exept.de>
parents:
997
diff
changeset
|
7733 |
"Modified: 23.2.1996 / 15:35:15 / cg" |
9192 | 7734 |
! |
7735 |
||
17191 | 7736 |
indexOfSubCollection:subString startingAt:index ifAbsent:exceptionBlock caseSensitive:caseSensitive ignoreDiacritics:ignoreDiacritics |
7737 |
"find a substring, starting at index. if found, return the index; |
|
7738 |
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
|
7739 |
This is a q&d hack - not very efficient |
17191 | 7740 |
(see implementation in string, for a much faster algorithm)" |
7741 |
||
7742 |
|firstChar found |
|
7743 |
startIndex "{ Class: SmallInteger }" |
|
7744 |
subSize "{ Class: SmallInteger }" |
|
7745 |
mySize "{ Class: SmallInteger }" |
|
7746 |
runIdx "{ Class: SmallInteger }" |
|
7747 |
tester charMap firstCharMapped findNextIndex| |
|
7748 |
||
7749 |
subSize := subString size. |
|
7750 |
subSize == 0 ifTrue:[ |
|
20874 | 7751 |
subString isString ifFalse:[ |
7752 |
self error:'non string argument' mayProceed:true. |
|
7753 |
]. |
|
7754 |
"empty string does not match" |
|
7755 |
^ 0. |
|
7756 |
"empty string matches" |
|
17191 | 7757 |
"/ ^ index |
7758 |
]. |
|
7759 |
||
7760 |
mySize := self size. |
|
7761 |
firstChar := subString at:1. |
|
7762 |
ignoreDiacritics ifTrue:[ |
|
20874 | 7763 |
caseSensitive ifTrue:[ |
7764 |
charMap := [:ch | ch asLowercase withoutDiacritics]. |
|
7765 |
] ifFalse:[ |
|
7766 |
charMap := [:ch | ch withoutDiacritics]. |
|
7767 |
]. |
|
7768 |
tester := [:c1 :c2 | (charMap value:c1) = (charMap value:c2) ]. |
|
7769 |
firstCharMapped := (charMap value:firstChar). |
|
7770 |
findNextIndex := [:index | self findFirst:[:ch | (charMap value:ch) = firstCharMapped] startingAt:index]. |
|
17191 | 7771 |
] ifFalse:[ |
20874 | 7772 |
caseSensitive ifTrue:[ |
7773 |
tester := [:c1 :c2 | c1 = c2 ]. |
|
7774 |
findNextIndex := [:index | self indexOf:firstChar startingAt:index]. |
|
7775 |
] ifFalse:[ |
|
7776 |
tester := [:c1 :c2 | c1 sameAs: c2 ]. |
|
7777 |
findNextIndex := [:index | self findFirst:[:c | c sameAs:firstChar] startingAt:index]. |
|
7778 |
]. |
|
17191 | 7779 |
]. |
7780 |
startIndex := findNextIndex value:index. |
|
7781 |
[startIndex == 0] whileFalse:[ |
|
20874 | 7782 |
runIdx := startIndex. |
7783 |
found := true. |
|
7784 |
1 to:subSize do:[:i | |
|
7785 |
runIdx > mySize ifTrue:[ |
|
7786 |
found := false |
|
7787 |
] ifFalse:[ |
|
7788 |
(tester value:(subString at:i) value:(self at:runIdx)) ifFalse:[ |
|
7789 |
found := false |
|
7790 |
] |
|
7791 |
]. |
|
7792 |
runIdx := runIdx + 1 |
|
7793 |
]. |
|
7794 |
found ifTrue:[ |
|
7795 |
^ startIndex |
|
7796 |
]. |
|
7797 |
startIndex := findNextIndex value:(startIndex + 1) |
|
17191 | 7798 |
]. |
7799 |
^ exceptionBlock value |
|
7800 |
||
7801 |
" |
|
17600 | 7802 |
'bla depot bla dépots' indexOfSubCollection:'dep' startingAt:1 ifAbsent:0 caseSensitive:false ignoreDiacritics:true. 5 |
7803 |
'bla depot bla dépots' indexOfSubCollection:'dep' startingAt:6 ifAbsent:0 caseSensitive:false ignoreDiacritics:true. 0 |
|
7804 |
||
7805 |
'bla depot bla dépots' indexOfSubCollection:'dep' startingAt:1 ifAbsent:0 caseSensitive:false ignoreDiacritics:false. 5 |
|
7806 |
'bla depot bla dépots' indexOfSubCollection:'dep' startingAt:6 ifAbsent:0 caseSensitive:false ignoreDiacritics:false. 0 |
|
17191 | 7807 |
" |
7808 |
! |
|
7809 |
||
18939 | 7810 |
lastIndexOfString:aString |
7811 |
"VSE and V'age compatibility" |
|
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
7812 |
"find the last occurrence of substring. |
18939 | 7813 |
If found, return the index; if not found, return 0." |
7814 |
||
7815 |
^ self lastIndexOfSubCollection:aString startingAt:(self size-aString size+1) ifAbsent:[0] |
|
7816 |
||
7817 |
" 123456789012 |
|
7818 |
'abcdefabcdef' lastIndexOfString:'abc' |
|
7819 |
'abcdefabcdef' lastIndexOfString:'abc' startingAt:6 |
|
7820 |
'abcdefabcdef' lastIndexOfString:'xxx' startingAt:6 |
|
7821 |
" |
|
7822 |
! |
|
7823 |
||
7824 |
lastIndexOfString:aString ifAbsent:exceptionValue |
|
7825 |
"VSE and V'age compatibility" |
|
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
7826 |
"find the last occurrence of substring. |
18939 | 7827 |
If found, return the index; if not found, return 0." |
7828 |
||
7829 |
^ self lastIndexOfSubCollection:aString startingAt:(self size-aString size+1) ifAbsent:exceptionValue |
|
7830 |
||
7831 |
" 123456789012 |
|
7832 |
'abcdefabcdef' lastIndexOfString:'abc' ifAbsent:[999] |
|
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
7833 |
'abcdefabcdef' lastIndexOfString:'xxx' ifAbsent:[999] |
18939 | 7834 |
" |
7835 |
! |
|
7836 |
||
7837 |
lastIndexOfString:aString startingAt:startIndex |
|
7838 |
"VSE and V'age compatibility" |
|
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
7839 |
"find the last occurrence of a substring. |
18939 | 7840 |
If found, return the index; if not found, return 0." |
7841 |
||
7842 |
^ self lastIndexOfSubCollection:aString startingAt:startIndex ifAbsent:[0] |
|
7843 |
||
7844 |
" 123456789012 |
|
7845 |
'abcdefabcdef' lastIndexOfString:'abc' |
|
7846 |
'abcdefabcdef' lastIndexOfString:'abc' startingAt:6 |
|
7847 |
" |
|
7848 |
! |
|
7849 |
||
7850 |
lastIndexOfString:aString startingAt:startIndex ifAbsent:exceptionValue |
|
7851 |
"VSE and V'age compatibility" |
|
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
7852 |
"find the last occurrence of a substring. |
18939 | 7853 |
If found, return the index; if not found, return 0." |
7854 |
||
7855 |
^ self lastIndexOfSubCollection:aString startingAt:startIndex ifAbsent:exceptionValue |
|
7856 |
||
7857 |
" 123456789012 |
|
7858 |
'abcdefabcdef' lastIndexOfString:'abc' |
|
7859 |
'abcdefabcdef' lastIndexOfString:'abc' startingAt:6 |
|
7860 |
'abcdefabcdef' lastIndexOfString:'xxx' startingAt:6 ifAbsent:999 |
|
7861 |
" |
|
7862 |
! |
|
7863 |
||
15055 | 7864 |
rangeOfSubCollection:subString startingAt:start ifAbsent:exceptionValue caseSensitive:caseSensitive |
7865 |
"find a substring. if found, return the start- and endIndex; |
|
7866 |
if not found, return the value of exceptionValue." |
|
7867 |
||
7868 |
|i| |
|
7869 |
||
7870 |
i := self indexOfSubCollection:subString startingAt:start ifAbsent:0 caseSensitive:caseSensitive. |
|
7871 |
i == 0 ifTrue:[ |
|
20874 | 7872 |
^ exceptionValue value |
15055 | 7873 |
]. |
7874 |
^ i to:(i + subString size - 1) |
|
7875 |
||
7876 |
" |
|
7877 |
'hello world' findRangeOfString:'llo' |
|
7878 |
'hello world' findRangeOfString:'ole' |
|
7879 |
" |
|
9192 | 7880 |
! ! |
7881 |
||
7882 |
!CharacterArray methodsFor:'testing'! |
|
7883 |
||
18463 | 7884 |
argumentCount |
7885 |
"treating the receiver as a message selector, return how many arguments would it take (ANSI)" |
|
7886 |
||
7887 |
|binopChars| |
|
7888 |
||
7889 |
(self size <= Method maxBinarySelectorSize) ifTrue:[ |
|
20874 | 7890 |
binopChars := Method binarySelectorCharacters. |
7891 |
(self conform:[:eachChar | (binopChars includes:eachChar)]) ifTrue:[ |
|
7892 |
^ 1 |
|
7893 |
]. |
|
18463 | 7894 |
]. |
7895 |
^ self occurrencesOf:$: |
|
7896 |
||
7897 |
" |
|
7898 |
'foo:bar:' argumentCount |
|
7899 |
#foo:bar: argumentCount |
|
7900 |
'hello' argumentCount |
|
7901 |
'+' argumentCount |
|
7902 |
'++' argumentCount |
|
7903 |
'+++' argumentCount |
|
7904 |
'|' argumentCount |
|
7905 |
'?' argumentCount |
|
7906 |
'_' argumentCount |
|
7907 |
'_:' argumentCount |
|
7908 |
'_:_:' argumentCount |
|
7909 |
'<->' argumentCount |
|
7910 |
" |
|
7911 |
! |
|
7912 |
||
18597 | 7913 |
characterSize |
7914 |
"answer the size in bits of my largest character (actually only 7, 8, 16 or 32)" |
|
7915 |
||
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
7916 |
|string max |
18597 | 7917 |
sz "{ Class:SmallInteger}" | |
15829 | 7918 |
|
7919 |
(string := self string) ~~ self ifTrue:[ |
|
20874 | 7920 |
^ string characterSize. |
18597 | 7921 |
]. |
7922 |
||
7923 |
sz := self size. |
|
7924 |
max := 7. |
|
7925 |
1 to:sz do:[:idx | |
|
20874 | 7926 |
|thisSize| |
7927 |
||
7928 |
thisSize := (self at:idx) characterSize. |
|
7929 |
thisSize > max ifTrue:[ |
|
7930 |
max := thisSize. |
|
7931 |
max == 32 ifTrue:[ |
|
7932 |
"shortcut: we know, that max size is 32" |
|
7933 |
^ 32. |
|
7934 |
]. |
|
7935 |
]. |
|
15829 | 7936 |
]. |
18597 | 7937 |
|
7938 |
^ max. |
|
7939 |
||
7940 |
" |
|
7941 |
'hello' characterSize |
|
7942 |
'hello' asUnicode32String characterSize |
|
7943 |
'helloü' asUnicode32String characterSize |
|
7944 |
'hello' asText allBold characterSize |
|
7945 |
" |
|
7946 |
! |
|
7947 |
||
7948 |
contains8BitCharacters |
|
7949 |
<resource: #obsolete> |
|
7950 |
||
7951 |
^ self containsNon7BitAscii |
|
7952 |
! |
|
7953 |
||
7954 |
containsNon7BitAscii |
|
7955 |
"return true, if the underlying string contains 8BitCharacters (or widers) |
|
7956 |
(i.e. if it is non-ascii)" |
|
7957 |
||
7958 |
|string |
|
7959 |
sz "{ Class:SmallInteger }"| |
|
7960 |
||
7961 |
(string := self string) ~~ self ifTrue:[ |
|
20874 | 7962 |
^ string containsNon7BitAscii |
18597 | 7963 |
]. |
7964 |
sz := self size. |
|
7965 |
1 to:sz do:[:idx| |
|
20874 | 7966 |
(self at:idx) codePoint > 16r7F ifTrue:[ |
7967 |
^ true. |
|
7968 |
]. |
|
18597 | 7969 |
]. |
7970 |
^ false. |
|
7971 |
||
7972 |
" |
|
7973 |
'hello' asUnicode32String containsNon7BitAscii |
|
7974 |
'hello üöä' asUnicode32String containsNon7BitAscii |
|
7975 |
'hello' asUnicode32String asText allBold containsNon7BitAscii |
|
7976 |
'hello üö' asUnicode32String asText allBold containsNon7BitAscii |
|
15829 | 7977 |
" |
7978 |
! |
|
7979 |
||
9192 | 7980 |
isAlphaNumeric |
7981 |
"return true, if the receiver is some alphanumeric word; |
|
7982 |
i.e. consists of a letter followed by letters or digits." |
|
7983 |
||
7984 |
self size == 0 ifTrue:[ |
|
20874 | 7985 |
"mhmh what is this ?" |
7986 |
^ false |
|
9192 | 7987 |
]. |
7988 |
(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
|
7989 |
^ self conform:[:char | char isLetterOrDigit]. |
8cff5eca1c6f
code cleanup: use #contains/#conform instead of explicit loop
Claus Gittinger <cg@exept.de>
parents:
10088
diff
changeset
|
7990 |
|
8cff5eca1c6f
code cleanup: use #contains/#conform instead of explicit loop
Claus Gittinger <cg@exept.de>
parents:
10088
diff
changeset
|
7991 |
" |
10703
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
7992 |
'helloWorld' isAlphaNumeric |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
7993 |
'foo1234' isAlphaNumeric |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
7994 |
'f1234' isAlphaNumeric |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
7995 |
'1234' isAlphaNumeric |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
7996 |
'+' isAlphaNumeric |
10093
8cff5eca1c6f
code cleanup: use #contains/#conform instead of explicit loop
Claus Gittinger <cg@exept.de>
parents:
10088
diff
changeset
|
7997 |
" |
8cff5eca1c6f
code cleanup: use #contains/#conform instead of explicit loop
Claus Gittinger <cg@exept.de>
parents:
10088
diff
changeset
|
7998 |
|
8cff5eca1c6f
code cleanup: use #contains/#conform instead of explicit loop
Claus Gittinger <cg@exept.de>
parents:
10088
diff
changeset
|
7999 |
"Modified: / 13-10-2006 / 12:53:49 / cg" |
9192 | 8000 |
! |
9190 | 8001 |
|
8002 |
isBinarySelector |
|
10903 | 8003 |
"treating the receiver as a message selector, return true if its a binary selector. |
8004 |
Notice, that st/x does not have a size <= 2 limit for unaries" |
|
9190 | 8005 |
|
8006 |
|binopChars| |
|
8007 |
||
18463 | 8008 |
(self size > Method maxBinarySelectorSize) ifTrue:[^ false]. |
8009 |
||
8010 |
binopChars := Method binarySelectorCharacters. |
|
9190 | 8011 |
^ (self conform:[:char | (binopChars includes:char)]) |
8012 |
||
8013 |
" |
|
8014 |
'foo:bar:' isBinarySelector |
|
8015 |
#foo:bar: isBinarySelector |
|
8016 |
'hello' isBinarySelector |
|
8017 |
'+' isBinarySelector |
|
8018 |
'|' isBinarySelector |
|
8874 | 8019 |
'?' isBinarySelector |
8020 |
':' isBinarySelector |
|
8021 |
'a:' isBinarySelector |
|
8022 |
'->' isBinarySelector |
|
8023 |
'<->' isBinarySelector |
|
8024 |
'::' isBinarySelector |
|
7482 | 8025 |
" |
8026 |
||
8027 |
"Modified: 4.1.1997 / 14:16:14 / cg" |
|
9192 | 8028 |
! |
8029 |
||
8030 |
isBlank |
|
8031 |
"return true, if the receiver contains spaces only" |
|
8032 |
||
9230 | 8033 |
^ (self contains:[:char | char ~~ Character space]) not |
8034 |
||
8035 |
" |
|
10703
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
8036 |
'' isBlank |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
8037 |
' a ' isBlank |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
8038 |
' ' isBlank |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
8039 |
' ' asUnicode16String isBlank |
9230 | 8040 |
" |
9192 | 8041 |
! |
8042 |
||
10903 | 8043 |
isKeywordSelector |
8044 |
"return true, iff there are only alphanumeric or underline characters separated by colons. |
|
19682 | 8045 |
Must end with a colon. |
8046 |
You can use this to check an arbitrary string for being valid as a keyword. |
|
8047 |
If you have a valid selector at hand, and need to know if it is a keyword or not, |
|
8048 |
use #isKeyword, which is much faster." |
|
10903 | 8049 |
|
8050 |
|state| |
|
8051 |
||
19682 | 8052 |
(self size == 0) ifTrue:[^ false]. |
8053 |
(self last == $:) ifFalse:[^ false]. |
|
8054 |
||
12177 | 8055 |
state := #initial. |
10903 | 8056 |
self do:[:char | |
20874 | 8057 |
(state == #initial or:[ state == #gotColon]) ifTrue:[ |
8058 |
(char isLetterOrUnderline) ifFalse:[^ false]. |
|
8059 |
state := #gotCharacter. |
|
8060 |
] ifFalse:[ |
|
8061 |
char == $: ifTrue:[ |
|
8062 |
state := #gotColon. |
|
8063 |
] ifFalse:[ |
|
8064 |
(char isLetterOrDigit or:[char == $_]) ifFalse:[^ false]. |
|
8065 |
]. |
|
8066 |
]. |
|
10903 | 8067 |
]. |
8068 |
^ state == #gotColon. |
|
8069 |
||
8070 |
" |
|
12177 | 8071 |
self assert:(':' isKeywordSelector not). |
10903 | 8072 |
self assert:(':a' isKeywordSelector not). |
8073 |
self assert:('1:' isKeywordSelector not). |
|
8074 |
self assert:('a:' isKeywordSelector). |
|
8075 |
self assert:('_:' isKeywordSelector). |
|
8076 |
self assert:('_a:' isKeywordSelector). |
|
8077 |
self assert:('_1:' isKeywordSelector). |
|
8078 |
self assert:('_1::' isKeywordSelector not). |
|
8079 |
self assert:('_:_:' isKeywordSelector). |
|
8080 |
self assert:('a:b:' isKeywordSelector). |
|
8081 |
self assert:('aa:bb:' isKeywordSelector). |
|
8082 |
self assert:('aa:bb:a' isKeywordSelector not). |
|
8083 |
self assert:('1:2:' isKeywordSelector not). |
|
8084 |
" |
|
19682 | 8085 |
|
8086 |
"Modified (comment): / 30-04-2016 / 18:20:14 / cg" |
|
10903 | 8087 |
! |
8088 |
||
10431 | 8089 |
isNameSpaceSelector |
8090 |
"Answer true if the receiver contains chars which form a nameSpace selector name. |
|
8091 |
These are of the form ':<ns>::<sel>', where ns is the NameSpace and sel is the regular selector. |
|
8092 |
For example, the #+ selector as seen by the Foo namespace would be actually #':Foo::+'. |
|
17511 | 8093 |
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
|
8094 |
is legal, and this can be checked quickly by just looking at the first character. |
10431 | 8095 |
You cannot easily change this algorithm here, as it is also known by the VM's lookup function." |
8096 |
||
8097 |
|i| |
|
8098 |
||
16733 | 8099 |
(self at:1) == $: ifFalse:[^ false]. |
17511 | 8100 |
|
10431 | 8101 |
i := self indexOf:$: startingAt:2. |
8102 |
i == 0 ifTrue:[^ false]. |
|
8103 |
self size <= (i+1) ifTrue:[^ false]. |
|
8104 |
(self at:i+1) == $: ifFalse:[^ false]. |
|
8105 |
(self at:i+2) == $: ifTrue:[^ false]. |
|
8106 |
"/ could check if the rest after the ns-prefix and colons is a valid selector... |
|
8107 |
^ true |
|
8108 |
||
8109 |
"test: |
|
10703
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
8110 |
self assert:('+' isNameSpaceSelector) not. |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
8111 |
self assert:(':+' isNameSpaceSelector) not. |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
8112 |
self assert:(':Foo:+' isNameSpaceSelector) not. |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
8113 |
|
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
8114 |
self assert:(':Foo::+' isNameSpaceSelector). |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
8115 |
self assert:(':Foo::bar:baz:' isNameSpaceSelector). |
10431 | 8116 |
" |
8117 |
||
8118 |
"Created: / 05-03-2007 / 11:35:31 / cg" |
|
8119 |
! |
|
8120 |
||
9192 | 8121 |
isNumeric |
8122 |
"return true, if the receiver is some numeric word; |
|
8123 |
i.e. consists only of digits." |
|
8124 |
||
8125 |
self size == 0 ifTrue:[ |
|
20874 | 8126 |
^ false |
9192 | 8127 |
]. |
10093
8cff5eca1c6f
code cleanup: use #contains/#conform instead of explicit loop
Claus Gittinger <cg@exept.de>
parents:
10088
diff
changeset
|
8128 |
^ self conform:[:char | char isDigit] |
9192 | 8129 |
|
8130 |
" |
|
8131 |
'helloWorld' isNumeric |
|
8132 |
'foo1234' isNumeric |
|
8133 |
'f1234' isNumeric |
|
8134 |
'1234' isNumeric |
|
8135 |
'+' isNumeric |
|
8136 |
" |
|
10093
8cff5eca1c6f
code cleanup: use #contains/#conform instead of explicit loop
Claus Gittinger <cg@exept.de>
parents:
10088
diff
changeset
|
8137 |
|
8cff5eca1c6f
code cleanup: use #contains/#conform instead of explicit loop
Claus Gittinger <cg@exept.de>
parents:
10088
diff
changeset
|
8138 |
"Modified: / 13-10-2006 / 12:54:12 / cg" |
9192 | 8139 |
! |
8140 |
||
18893 | 8141 |
isPlainString |
8142 |
"return true, if the receiver is a plain string (without attributes); |
|
8143 |
true is returned here - redefinition of Object>>isPlainString." |
|
8144 |
||
8145 |
^ true |
|
8146 |
! |
|
8147 |
||
9230 | 8148 |
isString |
8149 |
"return true, if the receiver is some kind of string; |
|
8150 |
true is returned here - redefinition of Object>>isString." |
|
8151 |
||
8152 |
^ true |
|
8153 |
! |
|
8154 |
||
8155 |
isUnarySelector |
|
8156 |
"Answer true if the receiver contains only chars in an ANSI unary method selector, false otherwise." |
|
8157 |
||
17442 | 8158 |
^ (self first isLetterOrUnderline) |
9845 | 8159 |
and:[ self conform: [ :chr | chr isLetterOrDigit or:[chr == $_] ]] |
8160 |
||
8161 |
"Modified: / 13-09-2006 / 11:35:15 / cg" |
|
9230 | 8162 |
! |
8163 |
||
14122 | 8164 |
isUnicode16String |
8165 |
^ false |
|
8166 |
! |
|
8167 |
||
8168 |
isUnicode32String |
|
19939 | 8169 |
"true if this is a 4-byte unicode string" |
8170 |
||
14122 | 8171 |
^ false |
8172 |
! |
|
8173 |
||
8174 |
isUnicodeString |
|
19938 | 8175 |
"true if this is a 2- or 4-byte unicode string |
8176 |
(i.e. not a single byte string). |
|
20194 | 8177 |
Notice, that the name is misleading: |
19938 | 8178 |
all strings are use unicode encoding" |
20194 | 8179 |
|
14122 | 8180 |
^ false |
8181 |
! |
|
8182 |
||
9230 | 8183 |
isValidSmalltalkIdentifier |
19464 | 8184 |
"return true, if the receiver's characters make up a valid smalltalk identifier" |
9230 | 8185 |
|
8186 |
|scanner tok| |
|
8187 |
||
8188 |
scanner := Compiler new. |
|
8189 |
scanner source:(self readStream). |
|
11630
6db5956f188f
isValidSmalltalkIdentifier: return true/false (catch parseError)
Claus Gittinger <cg@exept.de>
parents:
11565
diff
changeset
|
8190 |
Parser parseErrorSignal handle:[:ex | |
20874 | 8191 |
tok := nil. |
11630
6db5956f188f
isValidSmalltalkIdentifier: return true/false (catch parseError)
Claus Gittinger <cg@exept.de>
parents:
11565
diff
changeset
|
8192 |
] do:[ |
20874 | 8193 |
tok := scanner nextToken. |
11630
6db5956f188f
isValidSmalltalkIdentifier: return true/false (catch parseError)
Claus Gittinger <cg@exept.de>
parents:
11565
diff
changeset
|
8194 |
]. |
9230 | 8195 |
tok ~~ #Identifier ifTrue:[ |
20874 | 8196 |
^ false |
9230 | 8197 |
]. |
8198 |
scanner tokenPosition == 1 ifFalse:[^ false]. |
|
8199 |
^ scanner sourceStream atEnd. |
|
8200 |
||
8201 |
" |
|
8202 |
'foo' isValidSmalltalkIdentifier |
|
8203 |
'1foo' isValidSmalltalkIdentifier |
|
8204 |
'_foo' isValidSmalltalkIdentifier |
|
8205 |
'_foo_bar_' isValidSmalltalkIdentifier |
|
8206 |
'foo ' isValidSmalltalkIdentifier |
|
8207 |
' foo' isValidSmalltalkIdentifier |
|
8208 |
" |
|
8209 |
! |
|
8210 |
||
14555 | 8211 |
isWideString |
19580 | 8212 |
"true if I require more than one byte per character" |
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
8213 |
|
15829 | 8214 |
|string| |
8215 |
||
8216 |
(string := self string) ~~ self ifTrue:[ |
|
20874 | 8217 |
^ string isWideString. |
15829 | 8218 |
]. |
8219 |
^ self contains:[:aCharacter | aCharacter codePoint > 16rFF]. |
|
14555 | 8220 |
! |
8221 |
||
9192 | 8222 |
numArgs |
18463 | 8223 |
<resource: #obsolete> |
608 | 8224 |
"treating the receiver as a message selector, return how many arguments would it take" |
8225 |
||
18463 | 8226 |
^ self argumentCount |
9192 | 8227 |
! |
8228 |
||
8229 |
partsIfSelector |
|
14629 | 8230 |
"treat the receiver as a message selector, return a collection of parts. |
8231 |
Notice: this is more tolerant than Smalltalk's syntax would suggest; |
|
8232 |
especially it allows for empty keyword parts between colons. |
|
8233 |
This is not (and should not be checked here), to allow parsing of |
|
8234 |
degenerate selectors as appearing with objectiveC." |
|
608 | 8235 |
|
9192 | 8236 |
|idx1 "{ Class: SmallInteger }" |
8237 |
coll idx2 sz| |
|
8238 |
||
8239 |
coll := OrderedCollection new. |
|
8240 |
idx1 := 1. |
|
8241 |
sz := self size. |
|
16245 | 8242 |
[ |
20874 | 8243 |
idx2 := self indexOf:$: startingAt:idx1. |
8244 |
(idx2 == 0 or:[idx2 == sz]) ifTrue:[ |
|
8245 |
coll add:(self copyFrom:idx1). |
|
8246 |
^ coll |
|
8247 |
]. |
|
8248 |
coll add:(self copyFrom:idx1 to:idx2). |
|
8249 |
idx1 := idx2 + 1 |
|
16245 | 8250 |
] loop. |
9192 | 8251 |
|
8252 |
" |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
8253 |
'foo:' partsIfSelector |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
8254 |
'foo:bar:' partsIfSelector |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
8255 |
'foo::::' partsIfSelector |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
8256 |
#foo:bar: partsIfSelector |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
8257 |
'hello' partsIfSelector |
9192 | 8258 |
'+' partsIfSelector |
8259 |
" |
|
64 | 8260 |
! ! |
8261 |
||
9192 | 8262 |
!CharacterArray methodsFor:'tracing'! |
8263 |
||
8264 |
traceInto:aRequestor level:level from:referrer |
|
8265 |
"double dispatch into tracer, passing my type implicitely in the selector" |
|
8266 |
||
8267 |
^ aRequestor traceCharacterArray:self level:level from:referrer |
|
8268 |
||
8269 |
||
8270 |
! ! |
|
8271 |
||
8272 |
!CharacterArray methodsFor:'visiting'! |
|
8395
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8376
diff
changeset
|
8273 |
|
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8376
diff
changeset
|
8274 |
acceptVisitor:aVisitor with:aParameter |
16733 | 8275 |
"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
|
8276 |
|
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8376
diff
changeset
|
8277 |
^ aVisitor visitString:self with:aParameter |
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8376
diff
changeset
|
8278 |
! ! |
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8376
diff
changeset
|
8279 |
|
20873 | 8280 |
|
1849
9f35f5934ec6
added msdos-latin1 en/decoder (code = #msdos850)
Claus Gittinger <cg@exept.de>
parents:
1669
diff
changeset
|
8281 |
!CharacterArray class methodsFor:'documentation'! |
629 | 8282 |
|
8283 |
version |
|
18476 | 8284 |
^ '$Header$' |
12058 | 8285 |
! |
8286 |
||
8287 |
version_CVS |
|
18476 | 8288 |
^ '$Header$' |
629 | 8289 |
! ! |
6809 | 8290 |
|
14606 | 8291 |
|
2728
7569e45a4d15
raise error signal if anything goes wrong while decoding.
Claus Gittinger <cg@exept.de>
parents:
2721
diff
changeset
|
8292 |
CharacterArray initialize! |