author | Claus Gittinger <cg@exept.de> |
Thu, 06 Feb 2020 21:18:18 +0100 | |
changeset 25250 | 00afed939386 |
parent 25219 | 7e4c664a7697 |
child 25258 | 58b39832286e |
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 |
24557 | 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 |
|
23936 | 74 |
[about hashing:] |
75 |
the ST/X VM uses the fnv1 hash (*) to quickly retrieve symbols, |
|
76 |
This has only 1 collision in my current systm with 66k symbols. |
|
77 |
||
78 |
To try, evaluate: |
|
79 |
((Symbol allInstances collect:#hash_fnv1a as:Bag) |
|
80 |
valuesAndCountsSelect:[:h :cnt | cnt > 1]) size |
|
81 |
in contrast, java hash is much worse (18 collisions): |
|
82 |
((Symbol allInstances collect:#hash_java as:Bag) |
|
83 |
valuesAndCountsSelect:[:h :cnt | cnt > 1]) size |
|
84 |
and hash_sdbm hash is even better (0 collisions), |
|
85 |
but slightly slower: |
|
86 |
((Symbol allInstances collect:#hash_sdbm as:Bag) |
|
87 |
valuesAndCountsSelect:[:h :cnt | cnt > 1]) size |
|
88 |
we could use CRC32 (also with 0 collisions), |
|
89 |
but that is slower on machines without CRC instruction: |
|
90 |
((Symbol allInstances collect:[:s | CRC32Stream hashValueOf:s] as:Bag) |
|
91 |
valuesAndCountsSelect:[:h :cnt | cnt > 1]) size |
|
92 |
||
93 |
(*) slightly modified to return a 31bit positive number, eg. a SmallInt in 32bit systems. |
|
94 |
||
1290 | 95 |
[author:] |
20194 | 96 |
Claus Gittinger |
1290 | 97 |
|
98 |
[see also:] |
|
20194 | 99 |
String TwoByteString Unicode16String Uniode32String |
100 |
StringCollection |
|
64 | 101 |
" |
102 |
! ! |
|
103 |
||
2728
7569e45a4d15
raise error signal if anything goes wrong while decoding.
Claus Gittinger <cg@exept.de>
parents:
2721
diff
changeset
|
104 |
!CharacterArray class methodsFor:'initialization'! |
7569e45a4d15
raise error signal if anything goes wrong while decoding.
Claus Gittinger <cg@exept.de>
parents:
2721
diff
changeset
|
105 |
|
7569e45a4d15
raise error signal if anything goes wrong while decoding.
Claus Gittinger <cg@exept.de>
parents:
2721
diff
changeset
|
106 |
initialize |
8874 | 107 |
DecodingFailedSignal isNil ifTrue:[ |
20874 | 108 |
DecodingFailedSignal := DecodingError. |
109 |
DecodingFailedSignal notifierString:'error during decode'. |
|
110 |
||
22432 | 111 |
EncodingFailedSignal := EncodingError. |
20874 | 112 |
EncodingFailedSignal notifierString:'error during encode'. |
5440 | 113 |
] |
2728
7569e45a4d15
raise error signal if anything goes wrong while decoding.
Claus Gittinger <cg@exept.de>
parents:
2721
diff
changeset
|
114 |
|
7569e45a4d15
raise error signal if anything goes wrong while decoding.
Claus Gittinger <cg@exept.de>
parents:
2721
diff
changeset
|
115 |
" |
7569e45a4d15
raise error signal if anything goes wrong while decoding.
Claus Gittinger <cg@exept.de>
parents:
2721
diff
changeset
|
116 |
CharacterArray initialize |
7569e45a4d15
raise error signal if anything goes wrong while decoding.
Claus Gittinger <cg@exept.de>
parents:
2721
diff
changeset
|
117 |
" |
7569e45a4d15
raise error signal if anything goes wrong while decoding.
Claus Gittinger <cg@exept.de>
parents:
2721
diff
changeset
|
118 |
|
22432 | 119 |
"Modified: / 03-08-1997 / 18:15:59 / cg" |
120 |
"Modified (format): / 16-01-2018 / 18:57:10 / stefan" |
|
2728
7569e45a4d15
raise error signal if anything goes wrong while decoding.
Claus Gittinger <cg@exept.de>
parents:
2721
diff
changeset
|
121 |
! ! |
7569e45a4d15
raise error signal if anything goes wrong while decoding.
Claus Gittinger <cg@exept.de>
parents:
2721
diff
changeset
|
122 |
|
1849
9f35f5934ec6
added msdos-latin1 en/decoder (code = #msdos850)
Claus Gittinger <cg@exept.de>
parents:
1669
diff
changeset
|
123 |
!CharacterArray class methodsFor:'instance creation'! |
64 | 124 |
|
125 |
basicNew |
|
126 |
"return a new empty string" |
|
127 |
||
128 |
^ self basicNew:0 |
|
129 |
! |
|
130 |
||
2734 | 131 |
fromBytes:aByteCollection |
132 |
"return an instance of the receiver class, |
|
19757 | 133 |
taking untranslated bytes from the argument, aByteCollection |
134 |
in most-significant first order. |
|
2734 | 135 |
Only useful, when reading twoByteStrings from external sources." |
136 |
||
19757 | 137 |
^ self fromBytes:aByteCollection MSB:true |
138 |
||
139 |
" |
|
140 |
Unicode16String fromBytes:#[16r02 16r20] |
|
141 |
Unicode16String fromBytes:#[16r02 16r20] MSB:true |
|
142 |
Unicode16String fromBytes:#[16r02 16r20] MSB:false |
|
143 |
" |
|
144 |
||
145 |
"Modified: 30.6.1997 / 20:08:37 / cg" |
|
146 |
! |
|
147 |
||
148 |
fromBytes:aByteCollection MSB:msb |
|
149 |
"return an instance of the receiver class, |
|
150 |
taking untranslated bytes from the argument, aByteCollection |
|
151 |
in the given byte order. |
|
152 |
Only useful, when reading twoByteStrings from external sources." |
|
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
153 |
|
2734 | 154 |
|mySize nBytes newString dstIdx| |
155 |
||
19757 | 156 |
self assert:(self ~~ CharacterArray). "/ only works for concrete subclasses. |
157 |
||
158 |
"/ the following is a quite inefficient implementation. |
|
159 |
"/ consider rewriting, if heavily used. |
|
2734 | 160 |
nBytes := aByteCollection size. |
161 |
mySize := self basicNew bitsPerCharacter. |
|
162 |
mySize == 16 ifTrue:[ |
|
20874 | 163 |
newString := self uninitializedNew:(nBytes // 2). |
164 |
dstIdx := 1. |
|
165 |
msb ifTrue:[ |
|
166 |
aByteCollection pairWiseDo:[:hi :lo | |
|
167 |
newString at:dstIdx put:(Character value:(hi bitShift:8)+lo). |
|
168 |
dstIdx := dstIdx + 1 |
|
169 |
]. |
|
170 |
] ifFalse:[ |
|
171 |
aByteCollection pairWiseDo:[:lo :hi | |
|
172 |
newString at:dstIdx put:(Character value:(hi bitShift:8)+lo). |
|
173 |
dstIdx := dstIdx + 1 |
|
174 |
]. |
|
175 |
]. |
|
176 |
^ newString. |
|
2734 | 177 |
]. |
178 |
||
17625 | 179 |
^ (self uninitializedNew:nBytes) replaceFrom:1 with:aByteCollection |
180 |
||
181 |
" |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
182 |
Unicode16String fromBytes:#[16r02 16r20] |
2734 | 183 |
" |
184 |
||
185 |
"Modified: 30.6.1997 / 20:08:37 / cg" |
|
186 |
! |
|
187 |
||
64 | 188 |
fromString:aString |
189 |
"return a copy of the argument, aString" |
|
190 |
||
18967 | 191 |
|sz| |
192 |
||
193 |
sz := aString size. |
|
194 |
^ (self uninitializedNew:sz) replaceFrom:1 to:sz with:aString startingAt:1 |
|
195 |
||
196 |
" |
|
20874 | 197 |
Unicode16String fromString:'hello' |
22233 | 198 |
String fromString:'hello' asUnicode16String |
199 |
Unicode16String fromString:'hello' asUnicode16String |
|
17625 | 200 |
" |
581 | 201 |
! |
202 |
||
203 |
fromStringCollection:aCollectionOfStrings |
|
24250 | 204 |
"return a new string formed by concatenating each in aCollectionOfStrings" |
581 | 205 |
|
206 |
^ self fromStringCollection:aCollectionOfStrings separatedBy:'' |
|
207 |
||
208 |
" |
|
209 |
String fromStringCollection:#('hello' 'world' 'how' 'about' 'this') |
|
210 |
" |
|
211 |
||
24250 | 212 |
"Created: / 20-11-1995 / 15:26:59 / cg" |
213 |
"Modified (comment): / 05-06-2019 / 14:28:35 / Claus Gittinger" |
|
581 | 214 |
! |
215 |
||
216 |
fromStringCollection:aCollectionOfStrings separatedBy:aSeparatorString |
|
24250 | 217 |
"return a new string formed by concatenating each in aCollectionOfStrings |
218 |
separating them by aSeparatorString" |
|
581 | 219 |
|
17625 | 220 |
|stream| |
221 |
||
222 |
aCollectionOfStrings do:[:eachString | |
|
20874 | 223 |
stream isNil ifTrue:[ |
224 |
stream := self writeStreamClass with:eachString. |
|
225 |
] ifFalse:[ |
|
226 |
stream |
|
227 |
nextPutAll:aSeparatorString; |
|
228 |
nextPutAll:eachString. |
|
229 |
]. |
|
581 | 230 |
]. |
17700 | 231 |
stream isNil ifTrue:[^ '']. |
17625 | 232 |
^ stream contents |
581 | 233 |
|
234 |
" |
|
235 |
String fromStringCollection:#('hello' 'world' 'how' 'about' 'this') separatedBy:' ' |
|
17625 | 236 |
String fromStringCollection:#('hello' 'world' 'how' 'about' 'this') separatedBy:'Ƞ' |
237 |
Text fromStringCollection:{'hello'. 'world'. 'how' allBold. 'about'. 'this'. 'äöü'} separatedBy:'Ƞ' |
|
581 | 238 |
" |
239 |
||
24250 | 240 |
"Created: / 20-11-1995 / 15:32:17 / cg" |
241 |
"Modified (comment): / 05-06-2019 / 14:27:46 / Claus Gittinger" |
|
608 | 242 |
! |
243 |
||
5757 | 244 |
fromUTF8Bytes:aByteCollection |
21379 | 245 |
<resource: #obsolete> |
5758 | 246 |
"return a new string which represents the characters as decoded |
247 |
from the utf8 encoded bytes, aByteCollection. |
|
248 |
Returns either a normal String, or a TwoByteString instance. |
|
6810 | 249 |
Only useful, when reading twoByteStrings from external sources. |
250 |
This only handles up-to 16bit characters" |
|
5757 | 251 |
|
7876 | 252 |
^ self decodeFromUTF8:aByteCollection. |
5757 | 253 |
|
21379 | 254 |
"Modified (comment): / 07-02-2017 / 17:32:38 / stefan" |
5757 | 255 |
! |
256 |
||
608 | 257 |
new |
258 |
"return a new empty string" |
|
259 |
||
260 |
^ self basicNew:0 |
|
9425 | 261 |
! |
262 |
||
263 |
readSmalltalkStringFrom:aStreamOrString onError:exceptionBlock |
|
264 |
"read & return the next String from the (character-)stream aStream; |
|
265 |
skipping all whitespace first; return the value of exceptionBlock, |
|
266 |
if no string can be read. The sequence of characters as read from the |
|
267 |
stream must be one as stored via storeOn: or storeString." |
|
268 |
||
269 |
|str collected char| |
|
270 |
||
271 |
str := aStreamOrString readStream. |
|
272 |
"skip whiteSpace" |
|
273 |
str skipSeparators. |
|
11320 | 274 |
|
14452
ef3591302afa
changed: #readSmalltalkStringFrom:onError:
Stefan Vogel <sv@exept.de>
parents:
14451
diff
changeset
|
275 |
(str peekOrNil == $') ifTrue:[ |
20874 | 276 |
str next. |
277 |
collected := self writeStream. |
|
24506 | 278 |
[(char := str nextOrNil) notNil] whileTrue:[ |
20874 | 279 |
char == $' ifTrue:[ |
280 |
"/ look for another quote |
|
281 |
str peekOrNil ~~ $' ifTrue:[ |
|
282 |
"end of string reached" |
|
283 |
^ collected contents. |
|
284 |
]. |
|
285 |
"eat doubled quote" |
|
286 |
str next. |
|
287 |
]. |
|
288 |
((char ~~ Character return) or:[str peekOrNil ~~ Character lf]) ifTrue:[ |
|
289 |
"compress CRLF to LF, but keep a single CR" |
|
290 |
collected nextPut:char. |
|
291 |
]. |
|
292 |
]. |
|
293 |
"if we come here, we reached the end without finding a closing $'" |
|
9425 | 294 |
]. |
295 |
^ exceptionBlock value |
|
296 |
||
297 |
" |
|
24506 | 298 |
String readSmalltalkStringFrom:('''hello world''' readStream) onError:[self halt] |
299 |
String readSmalltalkStringFrom:('''hello '''' world''' readStream) onError:[self halt] |
|
300 |
String readSmalltalkStringFrom:('1 ''hello'' ' readStream) onError:[self halt] |
|
301 |
String readSmalltalkStringFrom:('1 ''hello'' ' readStream) onError:['foobar'] |
|
9425 | 302 |
" |
303 |
||
304 |
"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
|
305 |
"Modified: / 06-10-2006 / 14:05:32 / cg" |
24506 | 306 |
"Modified (comment): / 02-08-2019 / 10:10:46 / Stefan Vogel" |
307 |
! |
|
308 |
||
309 |
readSmalltalkStringWithCRsFrom:aStreamOrString onError:exceptionBlock |
|
310 |
"read & return the next String from the (character-)stream aStream; |
|
311 |
skipping all whitespace first; return the value of exceptionBlock, |
|
312 |
if no string can be read. The sequence of characters as read from the |
|
313 |
stream must be one as stored via storeOn: or storeString. |
|
314 |
||
315 |
Different from #readSmalltalStringFrom:onError: we keep CRLF as is." |
|
316 |
||
317 |
|str collected char| |
|
318 |
||
319 |
str := aStreamOrString readStream. |
|
320 |
"skip whiteSpace" |
|
321 |
str skipSeparators. |
|
322 |
||
323 |
(str peekOrNil == $') ifTrue:[ |
|
324 |
str next. |
|
325 |
collected := self writeStream. |
|
326 |
[(char := str nextOrNil) notNil] whileTrue:[ |
|
327 |
char == $' ifTrue:[ |
|
328 |
"/ look for another quote |
|
329 |
str peekOrNil ~~ $' ifTrue:[ |
|
330 |
"end of string reached" |
|
331 |
^ collected contents. |
|
332 |
]. |
|
333 |
"eat doubled quote" |
|
334 |
str next. |
|
335 |
]. |
|
336 |
collected nextPut:char. |
|
337 |
]. |
|
338 |
"if we come here, we reached the end without finding a closing $'" |
|
339 |
]. |
|
340 |
^ exceptionBlock value |
|
341 |
||
342 |
" |
|
343 |
String readSmalltalkStringWithCRsFrom:('''hello world''' readStream) onError:[self halt] |
|
344 |
String readSmalltalkStringWithCRsFrom:('''hello '''' world''' readStream) onError:[self halt] |
|
345 |
String readSmalltalkStringWithCRsFrom:('1 ''hello'' ' readStream) onError:[self halt] |
|
346 |
String readSmalltalkStringWithCRsFrom:('1 ''hello'' ' readStream) onError:['foobar'] |
|
347 |
" |
|
348 |
||
349 |
"Created: / 01-08-2019 / 16:11:25 / Stefan Vogel" |
|
350 |
"Modified: / 02-08-2019 / 10:11:06 / Stefan Vogel" |
|
11243
4bf099f5bb56
CharacterArray class>>#writeStream - answer a CharacterWriteStrem
Stefan Vogel <sv@exept.de>
parents:
11229
diff
changeset
|
351 |
! |
4bf099f5bb56
CharacterArray class>>#writeStream - answer a CharacterWriteStrem
Stefan Vogel <sv@exept.de>
parents:
11229
diff
changeset
|
352 |
|
13201 | 353 |
writeStreamClass |
21145 | 354 |
"the type of stream used in writeStream. |
17213 | 355 |
Here, we return CharacterWriteStream, which automatically changes |
356 |
the underlying collection to a multiByte string (i.e. UnicodeString). |
|
357 |
So you can stream wide characters into it." |
|
13201 | 358 |
|
359 |
^ CharacterWriteStream |
|
11243
4bf099f5bb56
CharacterArray class>>#writeStream - answer a CharacterWriteStrem
Stefan Vogel <sv@exept.de>
parents:
11229
diff
changeset
|
360 |
|
4bf099f5bb56
CharacterArray class>>#writeStream - answer a CharacterWriteStrem
Stefan Vogel <sv@exept.de>
parents:
11229
diff
changeset
|
361 |
" |
4bf099f5bb56
CharacterArray class>>#writeStream - answer a CharacterWriteStrem
Stefan Vogel <sv@exept.de>
parents:
11229
diff
changeset
|
362 |
String writeStream |
21145 | 363 |
|
364 |
- the following delivers a Unicode16String: |
|
365 |
||
366 |
String streamContents:[:s | |
|
367 |
s nextPut:(Character value:16r3f30). |
|
368 |
s nextPutAll:'hello'. |
|
369 |
s cr. |
|
370 |
] |
|
11243
4bf099f5bb56
CharacterArray class>>#writeStream - answer a CharacterWriteStrem
Stefan Vogel <sv@exept.de>
parents:
11229
diff
changeset
|
371 |
" |
13201 | 372 |
|
373 |
"Created: / 09-01-2011 / 10:37:57 / cg" |
|
64 | 374 |
! ! |
375 |
||
9265 | 376 |
!CharacterArray class methodsFor:'Compatibility-VW'! |
377 |
||
378 |
fromIntegerArray: anArray |
|
379 |
"Answer a new instance of the receiver that is created from the argument, anArray." |
|
380 |
||
381 |
| new | |
|
382 |
||
383 |
new := self new: anArray size. |
|
384 |
1 to: anArray size do:[:index | |
|
20874 | 385 |
new at: index put: (anArray at: index) asCharacter |
9265 | 386 |
]. |
387 |
^new |
|
388 |
||
389 |
" |
|
390 |
String fromIntegerArray: #[8 127] |
|
391 |
" |
|
392 |
! ! |
|
393 |
||
2728
7569e45a4d15
raise error signal if anything goes wrong while decoding.
Claus Gittinger <cg@exept.de>
parents:
2721
diff
changeset
|
394 |
!CharacterArray class methodsFor:'Signal constants'! |
7569e45a4d15
raise error signal if anything goes wrong while decoding.
Claus Gittinger <cg@exept.de>
parents:
2721
diff
changeset
|
395 |
|
7569e45a4d15
raise error signal if anything goes wrong while decoding.
Claus Gittinger <cg@exept.de>
parents:
2721
diff
changeset
|
396 |
decodingFailedSignal |
2833 | 397 |
"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
|
398 |
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
|
399 |
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
|
400 |
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
|
401 |
|
22432 | 402 |
^ DecodingError |
403 |
||
404 |
"Created: / 28-06-1997 / 20:09:55 / cg" |
|
405 |
"Modified: / 03-08-1997 / 18:16:47 / cg" |
|
406 |
"Modified: / 16-01-2018 / 18:58:10 / stefan" |
|
2833 | 407 |
! |
408 |
||
409 |
encodingFailedSignal |
|
410 |
"return the (query-) signal, raised when encoding of a string is not possible |
|
411 |
due to invalid characters contained in the source." |
|
412 |
||
22432 | 413 |
^ EncodingError |
414 |
||
415 |
"Modified: / 28-06-1997 / 20:09:35 / cg" |
|
416 |
"Created: / 03-08-1997 / 18:16:40 / cg" |
|
417 |
"Modified: / 16-01-2018 / 18:59:04 / stefan" |
|
2728
7569e45a4d15
raise error signal if anything goes wrong while decoding.
Claus Gittinger <cg@exept.de>
parents:
2721
diff
changeset
|
418 |
! ! |
7569e45a4d15
raise error signal if anything goes wrong while decoding.
Claus Gittinger <cg@exept.de>
parents:
2721
diff
changeset
|
419 |
|
25217 | 420 |
|
1849
9f35f5934ec6
added msdos-latin1 en/decoder (code = #msdos850)
Claus Gittinger <cg@exept.de>
parents:
1669
diff
changeset
|
421 |
!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
|
422 |
|
f0b45a4b82d2
first attempt in encoding/decoding for different character representations (mac/msdos etc.)
Claus Gittinger <cg@exept.de>
parents:
986
diff
changeset
|
423 |
lowSpaceCleanup |
f0b45a4b82d2
first attempt in encoding/decoding for different character representations (mac/msdos etc.)
Claus Gittinger <cg@exept.de>
parents:
986
diff
changeset
|
424 |
"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
|
425 |
|
f0b45a4b82d2
first attempt in encoding/decoding for different character representations (mac/msdos etc.)
Claus Gittinger <cg@exept.de>
parents:
986
diff
changeset
|
426 |
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
|
427 |
|
f0b45a4b82d2
first attempt in encoding/decoding for different character representations (mac/msdos etc.)
Claus Gittinger <cg@exept.de>
parents:
986
diff
changeset
|
428 |
" |
f0b45a4b82d2
first attempt in encoding/decoding for different character representations (mac/msdos etc.)
Claus Gittinger <cg@exept.de>
parents:
986
diff
changeset
|
429 |
CharacterArray lowSpaceCleanup |
f0b45a4b82d2
first attempt in encoding/decoding for different character representations (mac/msdos etc.)
Claus Gittinger <cg@exept.de>
parents:
986
diff
changeset
|
430 |
" |
f0b45a4b82d2
first attempt in encoding/decoding for different character representations (mac/msdos etc.)
Claus Gittinger <cg@exept.de>
parents:
986
diff
changeset
|
431 |
|
f0b45a4b82d2
first attempt in encoding/decoding for different character representations (mac/msdos etc.)
Claus Gittinger <cg@exept.de>
parents:
986
diff
changeset
|
432 |
"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
|
433 |
"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
|
434 |
! ! |
f0b45a4b82d2
first attempt in encoding/decoding for different character representations (mac/msdos etc.)
Claus Gittinger <cg@exept.de>
parents:
986
diff
changeset
|
435 |
|
7421 | 436 |
!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
|
437 |
|
7876 | 438 |
decodeFromUTF8:aStringOrByteCollection |
19837 | 439 |
"given a string in UTF8 encoding, |
440 |
return a new string containing the same characters, in Unicode encoding. |
|
441 |
Returns either a normal String, a Unicode16String or a Unicode32String instance. |
|
442 |
This is only useful, when reading from external sources or communicating with |
|
443 |
other systems |
|
444 |
(ST/X never uses utf8 internally, but always uses strings of fully decoded unicode characters). |
|
445 |
This only handles up-to 30bit characters." |
|
446 |
||
21379 | 447 |
|decodedSize nBitsRequired |
448 |
codepoint "{ Class: SmallInteger }" |
|
19837 | 449 |
byte "{ Class: SmallInteger }" |
21379 | 450 |
encodedSize "{ Class: SmallInteger }" |
19837 | 451 |
srcIdx "{ Class: SmallInteger }" |
452 |
idx "{ Class: SmallInteger }" |
|
453 |
nFollowBytes "{ Class: SmallInteger }" |
|
454 |
minValue "{ Class: SmallInteger }" |
|
455 |
newString| |
|
456 |
||
21379 | 457 |
encodedSize := aStringOrByteCollection size. |
458 |
decodedSize := aStringOrByteCollection utf8DecodedSize. |
|
459 |
||
460 |
encodedSize == decodedSize ifTrue:[ |
|
461 |
"/ fast track, also avoid creation of new strings if aStringOrByteCollection is already a 7-bit string |
|
23400 | 462 |
^ aStringOrByteCollection asSingleByteStringIfPossible |
19837 | 463 |
]. |
464 |
||
465 |
nBitsRequired := 8. |
|
466 |
srcIdx := 1. |
|
467 |
||
21379 | 468 |
"this loop is only used to find the largest character" |
469 |
[srcIdx <= encodedSize] whileTrue:[ |
|
470 |
byte := aStringOrByteCollection byteAt:srcIdx. |
|
19837 | 471 |
srcIdx := srcIdx + 1. |
21379 | 472 |
byte > 16r7F ifTrue:[ |
19837 | 473 |
(byte bitAnd:2r11100000) == 2r11000000 ifTrue:[ |
474 |
"/ 80 .. 7FF |
|
21379 | 475 |
nBitsRequired < 16 ifTrue:[ |
476 |
codepoint := ((byte bitAnd:2r00011111) bitShift:6) |
|
477 |
+ ((aStringOrByteCollection byteAt:srcIdx) bitAnd:2r00111111). |
|
478 |
codepoint > 16rFF ifTrue:[ |
|
479 |
nBitsRequired := 16. |
|
480 |
]. |
|
481 |
]. |
|
482 |
srcIdx := srcIdx + 1. |
|
19837 | 483 |
] ifFalse:[(byte bitAnd:2r11110000) == 2r11100000 ifTrue:[ |
484 |
"/ 800 .. FFFF |
|
21379 | 485 |
srcIdx := srcIdx + 2. |
486 |
nBitsRequired < 16 ifTrue:[nBitsRequired := 16]. |
|
19837 | 487 |
] ifFalse:[(byte bitAnd:2r11111000) == 2r11110000 ifTrue:[ |
488 |
"/ 10000 .. 1FFFFF |
|
21379 | 489 |
srcIdx := srcIdx + 3. |
490 |
nBitsRequired := 32. |
|
19837 | 491 |
] ifFalse:[(byte bitAnd:2r11111100) == 2r11111000 ifTrue:[ |
21379 | 492 |
"5 byte sequences do not result in valid unicode" |
19837 | 493 |
"/ 200000 .. 3FFFFFF |
21379 | 494 |
srcIdx := srcIdx + 4. |
495 |
nBitsRequired := 32. |
|
19837 | 496 |
] ifFalse:[(byte bitAnd:2r11111110) == 2r11111100 ifTrue:[ |
21379 | 497 |
"6 byte sequences do not result in valid unicode" |
19837 | 498 |
"/ 4000000 .. 7FFFFFFF |
21379 | 499 |
(byte bitAnd:2r00000001) ~~ 0 ifTrue:[ |
500 |
"/ ST/X can only represent up to 30 bit characters. |
|
19837 | 501 |
"/ but the max unicode character is defined as 16r10 FFFF anyway |
502 |
^ DecodingError newException |
|
503 |
defaultValue:aStringOrByteCollection; |
|
21145 | 504 |
raiseRequestWith:aStringOrByteCollection |
505 |
errorString:('unicode character out of range at: %1' bindWith:srcIdx). |
|
19837 | 506 |
]. |
21379 | 507 |
srcIdx := srcIdx + 5. |
508 |
nBitsRequired := 32. |
|
19837 | 509 |
] ifFalse:[ |
510 |
^ DecodingError newException |
|
511 |
defaultValue:aStringOrByteCollection; |
|
512 |
raiseRequestWith:aStringOrByteCollection errorString:'invalid utf8 encoding'. |
|
513 |
]]]]]. |
|
514 |
]. |
|
515 |
]. |
|
516 |
||
517 |
nBitsRequired == 8 ifTrue:[ |
|
21379 | 518 |
newString := String uninitializedNew:decodedSize |
519 |
] ifFalse:[nBitsRequired == 16 ifTrue:[ |
|
520 |
newString := Unicode16String new:decodedSize |
|
19837 | 521 |
] ifFalse:[ |
21379 | 522 |
newString := Unicode32String new:decodedSize |
19837 | 523 |
]]. |
524 |
||
525 |
idx := srcIdx := 1. |
|
526 |
||
527 |
"now fill the string" |
|
21379 | 528 |
[srcIdx <= encodedSize] whileTrue:[ |
529 |
byte := codepoint := aStringOrByteCollection byteAt:srcIdx. |
|
19837 | 530 |
srcIdx := srcIdx + 1. |
21379 | 531 |
byte > 16r7F ifTrue:[ |
19837 | 532 |
(byte bitAnd:2r11100000) == 2r11000000 ifTrue:[ |
21379 | 533 |
codepoint := byte bitAnd:2r00011111. |
19837 | 534 |
nFollowBytes := 1. |
21379 | 535 |
minValue := 16r80. |
19837 | 536 |
] ifFalse:[(byte bitAnd:2r11110000) == 2r11100000 ifTrue:[ |
21379 | 537 |
codepoint := byte bitAnd:2r00001111. |
19837 | 538 |
nFollowBytes := 2. |
21379 | 539 |
minValue := 16r800. |
19837 | 540 |
] ifFalse:[(byte bitAnd:2r11111000) == 2r11110000 ifTrue:[ |
21379 | 541 |
codepoint := byte bitAnd:2r00000111. |
19837 | 542 |
nFollowBytes := 3. |
21379 | 543 |
minValue := 16r10000. |
19837 | 544 |
] ifFalse:[(byte bitAnd:2r11111100) == 2r11111000 ifTrue:[ |
545 |
"5 byte sequences do not result in valid unicode" |
|
21379 | 546 |
codepoint := byte bitAnd:2r00000011. |
19837 | 547 |
nFollowBytes := 4. |
21379 | 548 |
minValue := 16r200000. |
19837 | 549 |
] ifFalse:[(byte bitAnd:2r11111110) == 2r11111100 ifTrue:[ |
550 |
"6 byte sequences do not result in valid unicode" |
|
21379 | 551 |
codepoint := byte bitAnd:2r00000001. |
19837 | 552 |
nFollowBytes := 5. |
21379 | 553 |
minValue := 16r4000000. |
19837 | 554 |
]]]]]. |
21379 | 555 |
|
19837 | 556 |
nFollowBytes timesRepeat:[ |
557 |
byte := aStringOrByteCollection byteAt:srcIdx. |
|
21379 | 558 |
(byte bitAnd:2r11000000) ~~ 2r10000000 ifTrue:[ |
559 |
^ DecodingError newException |
|
560 |
defaultValue:aStringOrByteCollection; |
|
561 |
raiseRequestWith:aStringOrByteCollection |
|
562 |
errorString:('illegal followbyte (expect 0x80): 0x%1 at:%2' bindWith:(byte hexPrintString:2) with:srcIdx). |
|
563 |
]. |
|
19837 | 564 |
srcIdx := srcIdx + 1. |
21379 | 565 |
codepoint := (codepoint bitShift:6) bitOr:(byte bitAnd:2r00111111). |
566 |
]. |
|
567 |
||
568 |
codepoint < minValue ifTrue:[ |
|
569 |
"encoding a value in a longer utf8-sequence than required is not allowed |
|
570 |
and imposes a security risk. Proceed to continue with decoding." |
|
571 |
DecodingError newException |
|
572 |
defaultValue:aStringOrByteCollection; |
|
573 |
raiseRequestWith:aStringOrByteCollection errorString:'overlong utf8 sequence'. |
|
19837 | 574 |
]. |
575 |
]. |
|
21379 | 576 |
newString at:idx put:(Character value:codepoint). |
19837 | 577 |
idx := idx + 1. |
578 |
]. |
|
579 |
^ newString |
|
580 |
||
581 |
" |
|
582 |
CharacterArray decodeFromUTF8:#[ 16r41 16r42 ] |
|
583 |
CharacterArray decodeFromUTF8:#[ 16rC6 16r8F ] |
|
584 |
CharacterArray decodeFromUTF8:#[ 16rEF 16rBF 16rBF ] |
|
585 |
||
21379 | 586 |
rfc3629 examples: |
19837 | 587 |
CharacterArray decodeFromUTF8:#[ 16r41 16rE2 16r89 16rA2 16rCE 16r91 16r2E ] |
588 |
CharacterArray decodeFromUTF8:#[ 16rED 16r95 16r9C 16rEA 16rB5 16rAD 16rEC 16r96 16rB4 ] |
|
589 |
CharacterArray decodeFromUTF8:#[ 16rE6 16r97 16rA5 16rE6 16r9C 16rAC 16rE8 16rAA 16r9E ] |
|
590 |
||
591 |
invalid: |
|
592 |
CharacterArray decodeFromUTF8:#[ 16rC0 16r80 ] |
|
593 |
CharacterArray decodeFromUTF8:#[ 16rE0 16r80 16r80 ] |
|
594 |
CharacterArray decodeFromUTF8:#[ 16rE0 16r81 16r02 ] |
|
21379 | 595 |
" |
596 |
||
597 |
"Modified: / 07-02-2017 / 17:41:17 / stefan" |
|
23400 | 598 |
"Modified: / 04-10-2018 / 13:29:53 / Claus Gittinger" |
20010 | 599 |
! |
600 |
||
20011 | 601 |
setupNormalizationMaps |
20864 | 602 |
"returns a 2-stage map from ch2 -> ch1 -> mappedChar. |
603 |
for unicode normalization |
|
604 |
(i.e. for replacing combining char-sequences with regular characters). |
|
605 |
ch2 is the combining charCode (eg. 0x0308), ch1 is the previous character (eg. $A), |
|
606 |
mappedChar is the result (eg. $Ä). |
|
607 |
Caveat: |
|
608 |
possibly incomplete: only COMBINING_DIACRITICAL_MARKS are cared for. |
|
609 |
Does not care for COMBINING_DIACRITICAL_MARKS_EXTENDED |
|
610 |
and COMBINING_DIACRITICAL_MARKS_SUPPLEMENT. |
|
611 |
However; those are used for German dialectology, ancient Greek and other similar |
|
612 |
exotic uses. Probably noone will ever even notice that they are missing..." |
|
20011 | 613 |
|
614 |
|def| |
|
615 |
||
616 |
UnicodeNormalizationMap := Dictionary new. |
|
20013 | 617 |
UnicodeDenormalizationMap := Dictionary new. |
618 |
||
619 |
def := [:combiner :chars :mappedChars | |
|
20011 | 620 |
|d| |
621 |
||
20013 | 622 |
d := UnicodeNormalizationMap at:combiner ifAbsentPut:[Dictionary new]. |
623 |
chars with:mappedChars do:[:ch1 :mappedChar | |
|
624 |
d at:ch1 put:mappedChar. |
|
625 |
UnicodeDenormalizationMap at:mappedChar put:(Unicode16String with:ch1 with:combiner). |
|
626 |
]. |
|
20011 | 627 |
]. |
20014 | 628 |
def value:(Character codePoint:16r0300) "grave" value:'AEIOUaeiouWw' value:'ÀÈÌÒÙàèìòùẀẁ'. |
629 |
def value:(Character codePoint:16r0301) "acute" value:'AEIOUYaeiouyÇçKkMmPpWw' value:'ÁÉÍÓÚÝáéíóúýḈḉḰḱḾḿṔṕẂẃ'. |
|
630 |
def value:(Character codePoint:16r0302) "circumflex" value:'AEIOUaeiouZz' value:'ÂÊÎÔÛâêîôûẐẑ'. |
|
20011 | 631 |
def value:(Character codePoint:16r0303) "tilde" value:'AONaon' value:'ÃÕÑãõñ'. |
20014 | 632 |
def value:(Character codePoint:16r0307) "dot over" value:'BbDdFfHhMmNnPpRrSsTtWwXxYyṢṣ' value:'ḂḃḊḋḞḟḢḣṀṁṄṅṖṗṘṙṠṡṪṫẆẇẊẋẎẏṨṩ'. |
633 |
def value:(Character codePoint:16r0308) "diaresis" value:'AEIOUaeiouyHhWwXxt' value:'ÄËÏÖÜäëïöüÿḦḧẄẅẌẍẗ'. |
|
634 |
def value:(Character codePoint:16r030A) "ring" value:'Aawy' value:'Ååẘẙ'. |
|
635 |
def value:(Character codePoint:16r0323) "dot under" value:'AaBbDdEeIiHhKkLlMmNnOoRrSsTtUuVvWwZzṠṡ' value:'ẠạḄḅḌḍẸẹỊịḤḥḲḳḶḷṂṃṆṇỌọṚṛṢṣṬṭỤụṾṿẈẉẒẓṨṩ'. |
|
20011 | 636 |
def value:(Character codePoint:16r0327) "cedilla" value:'Cc' value:'Çç'. |
637 |
||
638 |
" |
|
20014 | 639 |
self setupNormalizationMaps |
20011 | 640 |
" |
641 |
! |
|
642 |
||
643 |
unicodeDenormalizationMap |
|
644 |
"returns a 2-stage map from ch2 -> ch1 -> mappedChar |
|
20864 | 645 |
for unicode normalization (i.e. for making combining chars regular ones). |
646 |
Caveat: |
|
647 |
possibly incomplete: only COMBINING_DIACRITICAL_MARKS are cared for. |
|
648 |
Does not care for COMBINING_DIACRITICAL_MARKS_EXTENDED |
|
649 |
and COMBINING_DIACRITICAL_MARKS_SUPPLEMENT. |
|
650 |
However; those are used for German dialectology, ancient Greek and other similar |
|
651 |
exotic uses. Probably noone will ever even notice that they are missing..." |
|
20011 | 652 |
|
653 |
UnicodeDenormalizationMap isNil ifTrue:[ |
|
654 |
self setupNormalizationMaps |
|
655 |
]. |
|
656 |
^ UnicodeDenormalizationMap |
|
657 |
||
658 |
" |
|
659 |
self unicodeDenormalizationMap |
|
660 |
" |
|
661 |
! |
|
662 |
||
20010 | 663 |
unicodeNormalizationMap |
664 |
"returns a 2-stage map from ch2 -> ch1 -> mappedChar |
|
20864 | 665 |
for unicode normalization (i.e. for making combining chars regular ones). |
666 |
Caveat: |
|
667 |
possibly incomplete: only COMBINING_DIACRITICAL_MARKS are cared for. |
|
668 |
Does not care for COMBINING_DIACRITICAL_MARKS_EXTENDED |
|
669 |
and COMBINING_DIACRITICAL_MARKS_SUPPLEMENT. |
|
670 |
However; those are used for German dialectology, ancient Greek and other similar |
|
671 |
exotic uses. Probably noone will ever even notice that they are missing..." |
|
20010 | 672 |
|
673 |
UnicodeNormalizationMap isNil ifTrue:[ |
|
20011 | 674 |
self setupNormalizationMaps |
20010 | 675 |
]. |
676 |
^ UnicodeNormalizationMap |
|
677 |
||
678 |
" |
|
679 |
self unicodeNormalizationMap |
|
680 |
" |
|
443 | 681 |
! ! |
682 |
||
25204 | 683 |
|
1849
9f35f5934ec6
added msdos-latin1 en/decoder (code = #msdos850)
Claus Gittinger <cg@exept.de>
parents:
1669
diff
changeset
|
684 |
!CharacterArray class methodsFor:'pattern matching'! |
64 | 685 |
|
5342
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
686 |
matchEscapeCharacter |
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
687 |
"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
|
688 |
(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
|
689 |
|
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
690 |
^ $\ |
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
691 |
! |
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
692 |
|
15734 | 693 |
matchScan:matchScanArray from:matchStart to:matchStop with:aString from:start to:stop caseSensitive:caseSensitive |
327 | 694 |
"helper for match; return true if the characters from start to stop in |
695 |
aString are matching the scan in matchScan from matchStart to matchStop. |
|
328 | 696 |
The matchScan is as created by asMatchScanArray. |
327 | 697 |
|
8874 | 698 |
This algorithm is not at all the most efficient; |
699 |
for heavy duty pattern matching, an interface (primitive) to the regex |
|
328 | 700 |
pattern matching package should be added." |
327 | 701 |
|
8874 | 702 |
|matchEntry |
327 | 703 |
mStart "{ Class: SmallInteger }" |
704 |
mStop "{ Class: SmallInteger }" |
|
705 |
sStart "{ Class: SmallInteger }" |
|
706 |
sStop "{ Class: SmallInteger }" |
|
707 |
mSize "{ Class: SmallInteger }" |
|
708 |
sSize "{ Class: SmallInteger }" |
|
709 |
index "{ Class: SmallInteger }" |
|
2561 | 710 |
quickCheck matchLast nextMatchEntry |
986 | 711 |
checkChar included| |
327 | 712 |
|
713 |
mStart := matchStart. |
|
714 |
mStop := matchStop. |
|
715 |
sStart := start. |
|
716 |
sStop := stop. |
|
717 |
||
16245 | 718 |
[ |
8874 | 719 |
"/ Transcript showCR:('match: ''' , (aString copyFrom:sStart to:sStop) , |
3537 | 720 |
"/ ''' against:' , (matchScanArray copyFrom:mStart to:mStop) printString). |
721 |
||
20874 | 722 |
mSize := mStop - mStart + 1. |
723 |
sSize := sStop - sStart + 1. |
|
724 |
||
725 |
"empty strings match" |
|
726 |
(mSize == 0) ifTrue:[^ (sSize == 0)]. |
|
727 |
||
728 |
matchEntry := matchScanArray at:mStart. |
|
729 |
||
730 |
"/ the most common case first: |
|
731 |
(sSize ~~ 0 |
|
732 |
and:[(checkChar := (aString at:sStart)) = matchEntry]) ifTrue:[ |
|
733 |
"advance by one and continue" |
|
734 |
mStart := mStart + 1. |
|
735 |
sStart := sStart + 1 |
|
736 |
] ifFalse:[ |
|
737 |
(matchEntry == #any) ifTrue:[ |
|
738 |
"restString empty -> no match" |
|
739 |
(sSize == 0) ifTrue:[^ false]. |
|
740 |
"# matches single character" |
|
741 |
((sSize == 1) and:[mSize == 1]) ifTrue:[^ true]. |
|
742 |
"advance by one and continue" |
|
743 |
mStart := mStart + 1. |
|
744 |
sStart := sStart + 1 |
|
745 |
] ifFalse:[ |
|
746 |
(matchEntry == #anyString) ifTrue:[ |
|
747 |
"* alone matches anything" |
|
748 |
(mSize == 1) ifTrue:[^ true]. |
|
749 |
"restString empty & matchString not empty -> no match" |
|
750 |
(sSize == 0) ifTrue:[^ false]. |
|
751 |
||
752 |
" |
|
753 |
try to avoid some of the recursion by checking last |
|
754 |
character and continue with shortened strings if possible |
|
755 |
" |
|
756 |
quickCheck := false. |
|
757 |
(mStop >= mStart) ifTrue:[ |
|
758 |
matchLast := matchScanArray at:mStop. |
|
759 |
(matchLast ~~ #anyString) ifTrue:[ |
|
760 |
(matchLast == #any) ifTrue:[ |
|
761 |
quickCheck := true |
|
762 |
] ifFalse:[ |
|
763 |
matchLast == (aString at:sStop) ifTrue:[ |
|
764 |
quickCheck := true |
|
765 |
] ifFalse:[ |
|
766 |
matchLast isString ifTrue:[ |
|
767 |
quickCheck := matchLast includes:(aString at:sStop) |
|
768 |
] |
|
769 |
] |
|
770 |
] |
|
771 |
] |
|
772 |
]. |
|
773 |
quickCheck ifTrue:[ |
|
774 |
" |
|
775 |
quickCheck ok, advance from the right |
|
776 |
" |
|
777 |
mStop := mStop - 1. |
|
778 |
sStop := sStop - 1 |
|
779 |
] ifFalse:[ |
|
780 |
"/ no quick check; |
|
781 |
"/ look for the next character(s) |
|
782 |
"/ and try matching there |
|
783 |
"/ (to avoid recursion) |
|
784 |
||
785 |
mStart < mStop ifTrue:[ |
|
786 |
nextMatchEntry := matchScanArray at:mStart+1. |
|
787 |
nextMatchEntry isCharacter ifTrue:[ |
|
788 |
sStart <= sStop ifTrue:[ |
|
789 |
[ |
|
790 |
caseSensitive ifTrue:[ |
|
791 |
index := aString indexOf:nextMatchEntry startingAt:sStart |
|
792 |
] ifFalse:[ |
|
793 |
index := aString findFirst:[:c | c asLowercase = nextMatchEntry asLowercase] |
|
794 |
startingAt:sStart. |
|
795 |
]. |
|
796 |
(index == 0 or:[index > sStop]) ifTrue:[ |
|
797 |
^ false |
|
798 |
]. |
|
799 |
(self matchScan:matchScanArray |
|
800 |
from:(mStart + 1) |
|
801 |
to:mStop |
|
802 |
with:aString |
|
803 |
from:index |
|
804 |
to:sStop |
|
805 |
caseSensitive:caseSensitive |
|
806 |
) ifTrue:[ |
|
807 |
^ true |
|
808 |
]. |
|
809 |
sStart := index + 1. |
|
810 |
] loop. |
|
811 |
] |
|
812 |
] |
|
813 |
]. |
|
814 |
||
815 |
" |
|
816 |
no quick check possible; |
|
817 |
loop over all possible substrings |
|
818 |
" |
|
819 |
index := sStart. |
|
820 |
[index <= sStop] whileTrue:[ |
|
821 |
(self matchScan:matchScanArray |
|
822 |
from:(mStart + 1) |
|
823 |
to:mStop |
|
824 |
with:aString |
|
825 |
from:index |
|
826 |
to:sStop |
|
827 |
caseSensitive:caseSensitive |
|
828 |
) ifTrue:[ |
|
829 |
^ true |
|
830 |
]. |
|
831 |
index := index + 1 |
|
832 |
]. |
|
833 |
^ false |
|
834 |
]. |
|
835 |
] ifFalse:[ |
|
836 |
(matchEntry isString) ifTrue:[ |
|
837 |
"testString empty -> no match" |
|
838 |
(sSize == 0) ifTrue:[^ false]. |
|
839 |
||
840 |
included := false. |
|
841 |
"/ checkChar := aString at:sStart. |
|
842 |
included := matchEntry includes:checkChar. |
|
843 |
included ifFalse:[ |
|
844 |
caseSensitive ifFalse:[ |
|
845 |
checkChar isUppercase ifTrue:[ |
|
846 |
included := matchEntry includes:checkChar asLowercase. |
|
847 |
] ifFalse:[ |
|
848 |
included := matchEntry includes:checkChar asUppercase. |
|
849 |
] |
|
850 |
]. |
|
851 |
]. |
|
852 |
mStart := mStart + 1. |
|
853 |
mSize := mSize - 1. |
|
854 |
included ifFalse:[^ false]. |
|
855 |
||
856 |
((sSize == 1) and:[mSize == 0]) ifTrue:[^ true]. |
|
857 |
] ifFalse:[ |
|
858 |
"/ must be single character |
|
859 |
||
860 |
"testString empty ?" |
|
861 |
(sSize == 0) ifTrue:[^ false]. |
|
862 |
||
863 |
"first characters equal ?" |
|
864 |
"/ checkChar := aString at:sStart. |
|
865 |
caseSensitive ifTrue:[^ false]. |
|
866 |
(checkChar asUppercase ~= matchEntry asUppercase) ifTrue:[^ false]. |
|
867 |
||
868 |
"advance and continue" |
|
869 |
mStart := mStart + 1. |
|
870 |
]. |
|
871 |
"cut off 1st char and continue" |
|
872 |
sStart := sStart + 1 |
|
873 |
] |
|
874 |
] |
|
875 |
] |
|
16245 | 876 |
] loop. |
2519
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
877 |
|
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
878 |
" |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
879 |
|scanArray s| |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
880 |
|
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
881 |
scanArray := self matchScanArrayFrom:'*hello'. |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
882 |
s := 'foo bar hello world'. |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
883 |
CharacterArray |
20874 | 884 |
matchScan:scanArray |
885 |
from:1 |
|
886 |
to:scanArray size |
|
887 |
with:s |
|
888 |
from:1 |
|
889 |
to:s size |
|
890 |
caseSensitive:true |
|
2519
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
891 |
" |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
892 |
" |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
893 |
|scanArray s| |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
894 |
|
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
895 |
scanArray := self matchScanArrayFrom:'*hello*'. |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
896 |
s := 'foo bar hello world'. |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
897 |
CharacterArray |
20874 | 898 |
matchScan:scanArray |
899 |
from:1 |
|
900 |
to:scanArray size |
|
901 |
with:s |
|
902 |
from:1 |
|
903 |
to:s size |
|
904 |
caseSensitive:true |
|
15734 | 905 |
" |
906 |
||
907 |
"Modified: / 24-07-2011 / 07:17:03 / cg" |
|
908 |
"Modified (comment): / 24-07-2011 / 08:55:14 / cg" |
|
909 |
! |
|
910 |
||
911 |
matchScan:matchScanArray from:matchStart to:matchStop with:aString from:start to:stop ignoreCase:ignoreCase |
|
912 |
<resource: #obsolete> |
|
913 |
"helper for match; return true if the characters from start to stop in |
|
914 |
aString are matching the scan in matchScan from matchStart to matchStop. |
|
915 |
The matchScan is as created by asMatchScanArray. |
|
916 |
||
917 |
This algorithm is not at all the most efficient; |
|
918 |
for heavy duty pattern matching, an interface (primitive) to the regex |
|
919 |
pattern matching package should be added." |
|
920 |
||
921 |
^ self |
|
20874 | 922 |
matchScan:matchScanArray from:matchStart to:matchStop with:aString from:start to:stop caseSensitive:ignoreCase not |
15734 | 923 |
|
924 |
" |
|
925 |
|scanArray s| |
|
926 |
||
927 |
scanArray := self matchScanArrayFrom:'*hello'. |
|
928 |
s := 'foo bar hello world'. |
|
929 |
CharacterArray |
|
20874 | 930 |
matchScan:scanArray |
931 |
from:1 |
|
932 |
to:scanArray size |
|
933 |
with:s |
|
934 |
from:1 |
|
935 |
to:s size |
|
936 |
ignoreCase:false |
|
15734 | 937 |
" |
938 |
" |
|
939 |
|scanArray s| |
|
940 |
||
941 |
scanArray := self matchScanArrayFrom:'*hello*'. |
|
942 |
s := 'foo bar hello world'. |
|
943 |
CharacterArray |
|
20874 | 944 |
matchScan:scanArray |
945 |
from:1 |
|
946 |
to:scanArray size |
|
947 |
with:s |
|
948 |
from:1 |
|
949 |
to:s size |
|
950 |
ignoreCase:false |
|
13520 | 951 |
" |
952 |
||
953 |
"Modified: / 24-07-2011 / 07:17:03 / cg" |
|
954 |
"Modified (comment): / 24-07-2011 / 08:55:14 / cg" |
|
608 | 955 |
! |
956 |
||
957 |
matchScanArrayFrom:aString |
|
958 |
"scan a pattern string and decompose it into a scanArray. |
|
959 |
This is processed faster (especially with character ranges), and |
|
960 |
can also be reused later. (if the same pattern is to be searched again)" |
|
961 |
||
9282
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
962 |
^ self matchScanArrayFrom:aString escapeCharacter:(self matchEscapeCharacter) |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
963 |
|
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
964 |
" |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
965 |
String matchScanArrayFrom:'*ute*' |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
966 |
String matchScanArrayFrom:'**ute**' |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
967 |
String matchScanArrayFrom:'*uter' |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
968 |
String matchScanArrayFrom:'\*uter' |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
969 |
String matchScanArrayFrom:'[cC]#mpute[rR]' |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
970 |
String matchScanArrayFrom:'[abcd]*' |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
971 |
String matchScanArrayFrom:'[a-k]*' |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
972 |
String matchScanArrayFrom:'*some*compl*ern*' |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
973 |
String matchScanArrayFrom:'[a-' |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
974 |
String matchScanArrayFrom:'[a-zA-Z]' |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
975 |
String matchScanArrayFrom:'[a-z01234A-Z]' |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
976 |
" |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
977 |
|
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
978 |
"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
|
979 |
! |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
980 |
|
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
981 |
matchScanArrayFrom:aString escapeCharacter:escape |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
982 |
"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
|
983 |
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
|
984 |
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
|
985 |
|
8874 | 986 |
|coll |
608 | 987 |
idx "{ Class: SmallInteger }" |
9282
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
988 |
end c1 c2 matchSet previous| |
5342
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
989 |
|
5487 | 990 |
previous := nil. |
608 | 991 |
|
992 |
coll := OrderedCollection new. |
|
993 |
idx := 1. end := aString size. |
|
994 |
[idx <= end] whileTrue:[ |
|
20874 | 995 |
|char this| |
996 |
||
997 |
char := aString at:idx. |
|
998 |
char == $* ifTrue:[ |
|
999 |
previous ~~ #anyString ifTrue:[ |
|
1000 |
this := #anyString |
|
1001 |
] |
|
1002 |
] ifFalse:[ |
|
1003 |
char == $# ifTrue:[ |
|
1004 |
previous ~~ #anyString ifTrue:[ |
|
1005 |
this := #any |
|
1006 |
] |
|
1007 |
] ifFalse:[ |
|
1008 |
char == $[ ifTrue:[ |
|
1009 |
matchSet := IdentitySet new. |
|
1010 |
idx := idx + 1. |
|
1011 |
idx > end ifTrue:[^ nil]. |
|
1012 |
char := aString at:idx. |
|
1013 |
c1 := nil. |
|
1014 |
[char ~~ $]] whileTrue:[ |
|
1015 |
((char == $-) and:[c1 notNil]) ifTrue:[ |
|
1016 |
idx := idx + 1. |
|
1017 |
idx > end ifTrue:[^ nil]. |
|
1018 |
c2 := aString at:idx. |
|
1019 |
c1 to:c2 do:[:c | matchSet add:c]. |
|
1020 |
c1 := nil. |
|
1021 |
idx := idx + 1. |
|
1022 |
] ifFalse:[ |
|
1023 |
(char ~~ $]) ifTrue:[ |
|
1024 |
matchSet add:char. |
|
1025 |
c1 := char. |
|
1026 |
idx := idx + 1 |
|
1027 |
] |
|
1028 |
]. |
|
1029 |
idx > end ifTrue:[^ nil]. |
|
1030 |
char := aString at:idx |
|
1031 |
]. |
|
23552 | 1032 |
this := matchSet asString sort. |
20874 | 1033 |
] ifFalse:[ |
1034 |
char == escape ifTrue:[ |
|
1035 |
idx := idx + 1. |
|
1036 |
idx > end ifTrue:[ |
|
1037 |
"/ mhmh - what should we do here ? |
|
1038 |
this := char |
|
1039 |
] ifFalse:[ |
|
1040 |
this := aString at:idx. |
|
1041 |
] |
|
1042 |
] ifFalse:[ |
|
1043 |
this := char |
|
1044 |
] |
|
1045 |
] |
|
1046 |
] |
|
1047 |
]. |
|
1048 |
this notNil ifTrue:[coll add:this. previous := this]. |
|
1049 |
idx := idx + 1 |
|
608 | 1050 |
]. |
1051 |
||
1052 |
^ coll asArray |
|
1053 |
||
1054 |
" |
|
8874 | 1055 |
String matchScanArrayFrom:'*ute*' |
1056 |
String matchScanArrayFrom:'**ute**' |
|
1057 |
String matchScanArrayFrom:'*uter' |
|
1058 |
String matchScanArrayFrom:'\*uter' |
|
1059 |
String matchScanArrayFrom:'[cC]#mpute[rR]' |
|
1060 |
String matchScanArrayFrom:'[abcd]*' |
|
23552 | 1061 |
String matchScanArrayFrom:'[abcdŴĂĂ]*' |
8874 | 1062 |
String matchScanArrayFrom:'[a-k]*' |
1063 |
String matchScanArrayFrom:'*some*compl*ern*' |
|
1064 |
String matchScanArrayFrom:'[a-' |
|
1065 |
String matchScanArrayFrom:'[a-zA-Z]' |
|
1066 |
String matchScanArrayFrom:'[a-z01234A-Z]' |
|
608 | 1067 |
" |
2519
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
1068 |
|
23552 | 1069 |
"Modified: / 02-04-1997 / 16:20:29 / cg" |
1070 |
"Modified: / 03-12-2018 / 15:41:54 / Stefan Vogel" |
|
9192 | 1071 |
! ! |
1072 |
||
1073 |
!CharacterArray class methodsFor:'queries'! |
|
1074 |
||
1075 |
isAbstract |
|
11229 | 1076 |
"Return if this class is an abstract class. |
1077 |
True is returned for CharacterArray here; false for subclasses. |
|
19464 | 1078 |
Abstract subclasses must redefine this again." |
11229 | 1079 |
|
9192 | 1080 |
^ self == CharacterArray |
18597 | 1081 |
! |
1082 |
||
1083 |
speciesForCharacterSize:characterSize |
|
1084 |
"answer the class, that is able to hold characters of size characterSize" |
|
1085 |
||
1086 |
characterSize <= 8 ifTrue:[^ String]. |
|
1087 |
characterSize <= 16 ifTrue:[^ Unicode16String]. |
|
1088 |
^ Unicode32String |
|
9192 | 1089 |
! ! |
1090 |
||
25204 | 1091 |
|
22525 | 1092 |
!CharacterArray class methodsFor:'utilities'! |
1093 |
||
23983 | 1094 |
through:aCharacter in:inStream |
1095 |
"read all characters through aCharacter and return a |
|
1096 |
dense strings for it (i.e. String/TwoByteString, if possible). |
|
1097 |
This helper is present because when reading from a unicode32 stream, |
|
1098 |
dense strings are preferred. |
|
1099 |
If inStream is known to return single byte characters, |
|
1100 |
you should better use inStream through:aCharacter" |
|
1101 |
||
1102 |
|answerStream char| |
|
1103 |
||
1104 |
answerStream := CharacterWriteStream new:20. |
|
1105 |
[inStream atEnd] whileFalse:[ |
|
1106 |
char := inStream next. |
|
1107 |
answerStream nextPut:char. |
|
1108 |
(aCharacter == char) ifTrue:[ |
|
1109 |
^ answerStream contents |
|
1110 |
]. |
|
1111 |
]. |
|
1112 |
^ answerStream contents |
|
1113 |
||
1114 |
"Created: / 27-03-2019 / 13:42:00 / Claus Gittinger" |
|
1115 |
! |
|
1116 |
||
1117 |
throughAny:aCollection in:inStream |
|
1118 |
"read all characters through any in aCollection and return a |
|
1119 |
dense strings for it (i.e. String/TwoByteString, if possible). |
|
1120 |
This helper is present because when reading from a unicode32 stream, |
|
1121 |
dense strings are preferred. |
|
1122 |
If inStream is known to return single byte characters, |
|
1123 |
you should better use inStream throughAny:aCollection" |
|
1124 |
||
1125 |
|answerStream char| |
|
1126 |
||
1127 |
answerStream := CharacterWriteStream new:20. |
|
1128 |
[inStream atEnd] whileFalse:[ |
|
1129 |
char := inStream next. |
|
1130 |
answerStream nextPut:char. |
|
1131 |
(aCollection includes:char) ifTrue:[ |
|
1132 |
^ answerStream contents |
|
1133 |
]. |
|
1134 |
]. |
|
1135 |
^ answerStream contents |
|
1136 |
||
1137 |
"Created: / 27-03-2019 / 13:40:49 / Claus Gittinger" |
|
1138 |
! |
|
1139 |
||
22525 | 1140 |
withoutAmpersandEscapes:label |
1141 |
"remove single ampersands; |
|
1142 |
replace double ampersands by single ones. |
|
1143 |
This is used to unescape menu-labels |
|
1144 |
(which use the ampersand as shortKey marker)" |
|
1145 |
||
1146 |
^ String |
|
1147 |
streamContents:[:out | |
|
1148 |
|in ch next| |
|
1149 |
||
1150 |
in := label readStream. |
|
1151 |
[in atEnd] whileFalse:[ |
|
1152 |
ch := in next. |
|
1153 |
ch == $& ifTrue:[ |
|
1154 |
(next := in peek) == $& ifTrue:[ |
|
1155 |
in next. |
|
1156 |
out nextPut:ch |
|
1157 |
] ifFalse:[ |
|
1158 |
ch := next |
|
1159 |
]. |
|
1160 |
] ifFalse:[ |
|
1161 |
out nextPut:ch |
|
1162 |
]. |
|
1163 |
] |
|
1164 |
] |
|
1165 |
||
1166 |
" |
|
1167 |
String withoutAmpersandEscapes:'' |
|
1168 |
String withoutAmpersandEscapes:'a' |
|
1169 |
String withoutAmpersandEscapes:'abcd' |
|
1170 |
String withoutAmpersandEscapes:'&abcd' |
|
1171 |
String withoutAmpersandEscapes:'&abcd&' |
|
1172 |
String withoutAmpersandEscapes:'&a&b&c&d' |
|
1173 |
String withoutAmpersandEscapes:'&a&b&c&d&' |
|
1174 |
String withoutAmpersandEscapes:'&&a&&b&&c&&d&&' |
|
1175 |
String withoutAmpersandEscapes:'&&a&&b&&c&&d&' |
|
1176 |
String withoutAmpersandEscapes:'&a&&b&&c&&d&' |
|
1177 |
String withoutAmpersandEscapes:'a&&b&&c&&d' |
|
1178 |
" |
|
1179 |
! ! |
|
1180 |
||
9192 | 1181 |
!CharacterArray methodsFor:'Compatibility-ANSI'! |
1182 |
||
1183 |
addLineDelimiters |
|
1184 |
"Ansi compatibility - same as withCRs" |
|
1185 |
||
1186 |
^ self withCRs |
|
1187 |
||
1188 |
"Modified: / 13.11.2001 / 19:16:25 / cg" |
|
17543 | 1189 |
! ! |
1190 |
||
1191 |
!CharacterArray methodsFor:'Compatibility-Dolphin'! |
|
9192 | 1192 |
|
1193 |
copyExpanding:expandTable |
|
1194 |
"return a copy of myself, with translations from the expandTable sliced in. |
|
18392 | 1195 |
The argument is supposed to map from characters to either characters or strings. |
1196 |
Added for Dolphin compatibility" |
|
9192 | 1197 |
|
1198 |
|ds| |
|
1199 |
||
1200 |
ds := WriteStream on:(self species new). |
|
1201 |
self do:[:eachChar | |
|
20874 | 1202 |
|repl| |
1203 |
||
1204 |
repl := expandTable at:eachChar ifAbsent:[nil]. |
|
1205 |
repl isNil ifTrue:[ |
|
1206 |
ds nextPut:eachChar |
|
1207 |
] ifFalse:[ |
|
1208 |
repl size == 0 ifTrue:[ |
|
1209 |
ds nextPut:repl |
|
1210 |
] ifFalse:[ |
|
1211 |
ds nextPutAll:repl |
|
1212 |
] |
|
1213 |
]. |
|
9192 | 1214 |
]. |
6870 | 1215 |
^ ds contents. |
24036 | 1216 |
|
1217 |
" |
|
1218 |
'hello' copyExpanding:(Dictionary withKeys:{$h . $e . $o} andValues:{'HH' . 'EE' . $O }) |
|
1219 |
" |
|
1220 |
||
1221 |
"Modified (format): / 02-04-2019 / 10:59:59 / Claus Gittinger" |
|
6870 | 1222 |
! |
1223 |
||
1224 |
formatWith:aString |
|
11955 | 1225 |
"Compatibility method - do not use in new code. |
18392 | 1226 |
same as #bindWith: for Dolphin compatibility" |
6870 | 1227 |
|
1228 |
^ self bindWith:aString |
|
1229 |
||
1230 |
" |
|
1231 |
'hello%1world' formatWith:'123' |
|
1232 |
" |
|
1233 |
! |
|
1234 |
||
1235 |
formatWith:arg1 with:arg2 |
|
11955 | 1236 |
"Compatibility method - do not use in new code. |
18392 | 1237 |
same as #bindWith: for Dolphin compatibility" |
6870 | 1238 |
|
1239 |
^ self bindWith:arg1 with:arg2 |
|
1240 |
||
1241 |
" |
|
1242 |
'hello%1 %2world' formatWith:'123' with:234 |
|
1243 |
" |
|
1244 |
! |
|
1245 |
||
1246 |
formatWith:arg1 with:arg2 with:arg3 |
|
11955 | 1247 |
"Compatibility method - do not use in new code. |
18392 | 1248 |
same as #bindWith: for Dolphin compatibility" |
6870 | 1249 |
|
1250 |
^ self bindWith:arg1 with:arg2 with:arg3 |
|
1251 |
||
1252 |
" |
|
1253 |
'hello%1 %2 %3world' formatWith:'123' with:234 with:345 |
|
1254 |
" |
|
6391 | 1255 |
! ! |
1256 |
||
14681 | 1257 |
!CharacterArray methodsFor:'Compatibility-GNU'! |
1258 |
||
1259 |
% anArrayOfOperands |
|
1260 |
"return a copy of the receiver, where a '%i' escape |
|
21747 | 1261 |
is replaced by the corresponding string from the argument array. |
14681 | 1262 |
'i' may be between 1 and 9 (i.e. a maximum of 9 placeholders is allowed). |
1263 |
Added for GNU-ST compatibility." |
|
1264 |
||
1265 |
^ self expandPlaceholdersWith:anArrayOfOperands |
|
1266 |
||
1267 |
" |
|
1268 |
'do you prefer %1 or rather %2 (not talking about %3) ?' |
|
20874 | 1269 |
% #('smalltalk' 'c++' 'c') |
14681 | 1270 |
|
1271 |
'do you %(what) ?' |
|
20874 | 1272 |
% (Dictionary new at:#'what' put:'understand'; yourself) |
14681 | 1273 |
" |
21747 | 1274 |
|
1275 |
"Modified (comment): / 11-05-2017 / 12:43:08 / mawalch" |
|
14681 | 1276 |
! ! |
1277 |
||
7261 | 1278 |
!CharacterArray methodsFor:'Compatibility-ST/V'! |
608 | 1279 |
|
17517 | 1280 |
asArrayOfSubstrings |
1281 |
"return an array of substrings from the receiver, interpreting |
|
1282 |
separators (i.e. spaces & newlines) as word-delimiters. |
|
18390 | 1283 |
This has been added for ST/V compatibility |
1284 |
- the actual work is done in asCollectionOfWords. |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
1285 |
(sigh: it is called #'subStrings' in V'Age, #'substrings' in Squeak |
17517 | 1286 |
and #'asCollectionOfWords' in ST/X) " |
1287 |
||
1288 |
^ self asCollectionOfWords asArray |
|
1289 |
||
1290 |
" |
|
1291 |
'1 one two three four 5 five' asArrayOfSubstrings |
|
1292 |
'1 |
|
1293 |
one |
|
20874 | 1294 |
two three four 5 five' asArrayOfSubstrings |
17517 | 1295 |
" |
1296 |
! |
|
1297 |
||
19497 | 1298 |
equalsIgnoreCase:aString |
1299 |
"This is an ST/V compatibility method and an alias for sameAs:." |
|
1300 |
||
1301 |
^ self sameAs:aString |
|
1302 |
||
1303 |
" |
|
1304 |
'abc' equalsIgnoreCase: 'aBC' |
|
1305 |
" |
|
1306 |
! |
|
1307 |
||
608 | 1308 |
replChar:oldChar with:newChar |
19497 | 1309 |
"return a copy of the receiver, with all oldChars replaced by newChar. |
1310 |
This is an ST/V compatibility method and an alias for copyReplaceAll." |
|
608 | 1311 |
|
3656 | 1312 |
^ self copyReplaceAll:oldChar with:newChar |
608 | 1313 |
|
1314 |
" |
|
8874 | 1315 |
'12345678901234567890' replChar:$0 with:$* |
608 | 1316 |
" |
3656 | 1317 |
|
1318 |
"Modified: / 18.7.1998 / 22:52:57 / cg" |
|
608 | 1319 |
! |
1320 |
||
1321 |
replChar:oldChar withString:newString |
|
1322 |
"return a copy of the receiver, with all oldChars replaced |
|
1323 |
by newString (i.e. slice in the newString in place of the oldChar). |
|
1324 |
This is an ST/V compatibility method." |
|
1325 |
||
1326 |
|tmpStream| |
|
1327 |
||
15523 | 1328 |
tmpStream := self species writeStream. |
608 | 1329 |
self do:[:element | |
20874 | 1330 |
element = oldChar ifTrue:[ |
1331 |
tmpStream nextPutAll:newString |
|
1332 |
] ifFalse:[ |
|
1333 |
tmpStream nextPut:element |
|
1334 |
]. |
|
608 | 1335 |
]. |
1336 |
^ tmpStream contents |
|
1337 |
||
1338 |
" |
|
8874 | 1339 |
'12345678901234567890' replChar:$0 withString:'foo' |
1340 |
'a string with spaces' replChar:$ withString:' foo ' |
|
608 | 1341 |
" |
9192 | 1342 |
! |
1343 |
||
1344 |
replString:subString withString:newString |
|
4244
5deefa3144fb
added #replString:withString:
Claus Gittinger <cg@exept.de>
parents:
4156
diff
changeset
|
1345 |
"return a copy of the receiver, with all sequences of subString replaced |
18390 | 1346 |
by newString (i.e. slice in the newString in place of the oldString). |
19497 | 1347 |
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
|
1348 |
|
8347 | 1349 |
^ self copyReplaceString:subString withString:newString |
4244
5deefa3144fb
added #replString:withString:
Claus Gittinger <cg@exept.de>
parents:
4156
diff
changeset
|
1350 |
|
5deefa3144fb
added #replString:withString:
Claus Gittinger <cg@exept.de>
parents:
4156
diff
changeset
|
1351 |
" |
8874 | 1352 |
'12345678901234567890' replString:'123' withString:'OneTwoThree' |
1353 |
'12345678901234567890' replString:'123' withString:'*' |
|
1354 |
'12345678901234567890' replString:'234' withString:'foo' |
|
4244
5deefa3144fb
added #replString:withString:
Claus Gittinger <cg@exept.de>
parents:
4156
diff
changeset
|
1355 |
|
5deefa3144fb
added #replString:withString:
Claus Gittinger <cg@exept.de>
parents:
4156
diff
changeset
|
1356 |
('a string with spaces' replChar:$ withString:' foo ') |
20874 | 1357 |
replString:'foo' withString:'bar' |
4244
5deefa3144fb
added #replString:withString:
Claus Gittinger <cg@exept.de>
parents:
4156
diff
changeset
|
1358 |
" |
5deefa3144fb
added #replString:withString:
Claus Gittinger <cg@exept.de>
parents:
4156
diff
changeset
|
1359 |
|
8347 | 1360 |
"Modified: / 12-05-2004 / 12:00:27 / cg" |
9192 | 1361 |
! |
1362 |
||
9275 | 1363 |
subString:start to:end |
18390 | 1364 |
"same as copyFrom:to: |
19497 | 1365 |
This is an ST/V compatibility method and an alias for copyFrom:to:." |
18390 | 1366 |
|
9275 | 1367 |
^ self copyFrom:start to:end |
1368 |
||
1369 |
" |
|
1370 |
'12345678901234567890' subString:3 to:8 |
|
1371 |
" |
|
1372 |
! |
|
1373 |
||
9192 | 1374 |
trimBlanks |
14992 | 1375 |
"return a copy of the receiver without leading and trailing spaces. |
18464 | 1376 |
This is an ST/V compatibility method and an alias for withoutSpaces." |
9192 | 1377 |
|
1378 |
^ self withoutSpaces |
|
1379 |
||
1380 |
" |
|
1381 |
' spaces at beginning' trimBlanks |
|
1382 |
'spaces at end ' trimBlanks |
|
1383 |
' spaces at beginning and end ' trimBlanks |
|
1384 |
'no spaces' trimBlanks |
|
1385 |
" |
|
1386 |
! ! |
|
1387 |
||
1388 |
!CharacterArray methodsFor:'Compatibility-Squeak'! |
|
4409
86e049b21f8d
added #includesSubstring:caseSensitive:
Claus Gittinger <cg@exept.de>
parents:
4389
diff
changeset
|
1389 |
|
19171 | 1390 |
asBoldText |
1391 |
"return self as a bold text" |
|
1392 |
||
21843 | 1393 |
"this test allows for small non-gui apps to be built without libbasic2 (where Text is)" |
1394 |
Text isNil ifTrue:[^ self]. |
|
1395 |
^ Text string: self emphasis: #bold |
|
1396 |
||
1397 |
"Modified (comment): / 20-06-2017 / 08:23:49 / cg" |
|
19171 | 1398 |
! |
1399 |
||
6148 | 1400 |
asDate |
1401 |
"Many allowed forms, see Date.readFrom:" |
|
1402 |
||
1403 |
^ Date readFrom: (ReadStream on: self) |
|
1404 |
||
1405 |
" |
|
8874 | 1406 |
'30 Apr 1999' asDate dayName capitalized |
6148 | 1407 |
" |
1408 |
! |
|
1409 |
||
10703
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
1410 |
findDelimiters:delimiters startingAt:start |
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
1411 |
"Answer the index of the character within the receiver, starting at start, |
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
1412 |
that matches one of the delimiters. |
18391 | 1413 |
If the receiver does not contain any of the delimiters, answer size + 1." |
9192 | 1414 |
|
1415 |
|idx| |
|
1416 |
||
1417 |
idx := self indexOfAny:delimiters startingAt:start. |
|
1418 |
idx == 0 ifTrue:[^ self size + 1]. |
|
1419 |
^ idx. |
|
1420 |
"/start to: self size do: [:i | |
|
1421 |
"/ |char| |
|
1422 |
"/ |
|
1423 |
"/ char := self at: i. |
|
1424 |
"/ delimiters do: [:delim | delim = char ifTrue: [^ i]] |
|
8971 | 1425 |
"/ ]. |
1426 |
"/ ^ self size + 1 |
|
1427 |
! |
|
1428 |
||
22005 | 1429 |
findString:key startingAt:start caseSensitive:caseSensitive |
1430 |
^ self indexOfSubCollection:key startingAt:start ifAbsent:[0] caseSensitive:caseSensitive |
|
1431 |
||
1432 |
"Created: / 13-07-2017 / 12:44:50 / cg" |
|
1433 |
! |
|
1434 |
||
13624 | 1435 |
findTokens:delimiterOrDelimiters |
7270 | 1436 |
"cg: I am not sure, if this is really the squeak semantics (w.r.t. empty fields)" |
1437 |
||
13624 | 1438 |
delimiterOrDelimiters size == 0 ifTrue:[ |
20874 | 1439 |
^ self asCollectionOfSubstringsSeparatedBy:delimiterOrDelimiters |
13624 | 1440 |
] ifFalse:[ |
20874 | 1441 |
^ self asCollectionOfSubstringsSeparatedByAny:delimiterOrDelimiters |
13624 | 1442 |
]. |
7270 | 1443 |
|
1444 |
" |
|
8874 | 1445 |
'a|b#c||e' findTokens:#($# $|) |
13624 | 1446 |
'a|b#c||e' findTokens:$| |
1447 |
" |
|
1448 |
||
1449 |
"Modified: / 04-09-2011 / 20:31:21 / cg" |
|
1450 |
"Modified (comment): / 05-09-2011 / 04:25:28 / cg" |
|
4981 | 1451 |
! |
1452 |
||
4974 | 1453 |
includesSubString:aString |
1454 |
"return true, if a substring is contained in the receiver. |
|
1455 |
The compare is case sensitive." |
|
1456 |
||
1457 |
^ self includesString:aString |
|
1458 |
||
1459 |
" |
|
8874 | 1460 |
'hello world' includesSubString:'Hel' |
9192 | 1461 |
'hello world' includesSubString:'hel' |
1462 |
'hello world' includesSubString:'llo' |
|
1463 |
" |
|
1464 |
||
1465 |
||
1466 |
||
1467 |
! |
|
1468 |
||
11178 | 1469 |
includesSubString:aString caseSensitive:caseSensitive |
18391 | 1470 |
"sigh - an alias; added for Squeak/Pharo compatibility" |
1471 |
||
25196 | 1472 |
^ self includesString:aString caseSensitive:caseSensitive |
11178 | 1473 |
! |
1474 |
||
16875
32e61bb3a742
class: CharacterArray>>#includesSubstring: (for Squeak compatibility)
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
16863
diff
changeset
|
1475 |
includesSubstring: aString |
18391 | 1476 |
"sigh - an alias; added for Squeak/Pharo compatibility" |
1477 |
||
24085 | 1478 |
^ self includesString: aString |
16875
32e61bb3a742
class: CharacterArray>>#includesSubstring: (for Squeak compatibility)
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
16863
diff
changeset
|
1479 |
|
32e61bb3a742
class: CharacterArray>>#includesSubstring: (for Squeak compatibility)
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
16863
diff
changeset
|
1480 |
"Created: / 03-10-2014 / 02:47:52 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
24085 | 1481 |
"Modified: / 16-04-2019 / 12:32:50 / Stefan Vogel" |
16875
32e61bb3a742
class: CharacterArray>>#includesSubstring: (for Squeak compatibility)
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
16863
diff
changeset
|
1482 |
! |
32e61bb3a742
class: CharacterArray>>#includesSubstring: (for Squeak compatibility)
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
16863
diff
changeset
|
1483 |
|
9192 | 1484 |
includesSubstring:aString caseSensitive:caseSensitive |
9190 | 1485 |
"return true, if a substring is contained in the receiver. |
9192 | 1486 |
The argument, caseSensitive controls if case is ignored in the compare." |
1487 |
||
24085 | 1488 |
^ (self indexOfSubCollection:aString startingAt:1 ifAbsent:0 caseSensitive:caseSensitive) ~~ 0 |
9190 | 1489 |
|
1490 |
" |
|
9192 | 1491 |
'hello world' includesSubstring:'Hel' caseSensitive:true |
1492 |
'hello world' includesSubstring:'Hel' caseSensitive:false |
|
1493 |
" |
|
1494 |
||
24085 | 1495 |
"Modified (comment): / 16-04-2019 / 12:35:33 / Stefan Vogel" |
9192 | 1496 |
! |
9190 | 1497 |
|
6979 | 1498 |
isAllDigits |
1499 |
"Answer whether the receiver's characters are all digits" |
|
1500 |
||
1501 |
^ self conform:[:eachChar | eachChar isDigit] |
|
1502 |
||
1503 |
" |
|
1504 |
'hello world' isAllDigits |
|
1505 |
'12344' isAllDigits |
|
1506 |
" |
|
1507 |
! |
|
1508 |
||
4890 | 1509 |
lastSpacePosition |
18391 | 1510 |
"return the index of the last space character; 0 if there is none. |
1511 |
Added for Squeak/Pharo compatibility" |
|
14405
1d0d99a95bba
comment/format in:7 methods
Claus Gittinger <cg@exept.de>
parents:
14168
diff
changeset
|
1512 |
|
20739 | 1513 |
"/ CG: is this correct? separator or space only??? |
4890 | 1514 |
^ self lastIndexOfSeparator |
1515 |
! |
|
1516 |
||
20738 | 1517 |
linesDo:aBlock |
1518 |
"evaluate the argument, aBlock for all lines, |
|
1519 |
up to the end" |
|
1520 |
||
1521 |
^ self readStream linesDo:aBlock |
|
1522 |
! |
|
1523 |
||
7022 | 1524 |
padded:leftOrRight to:paddedSize with:padCharacter |
20155 | 1525 |
"pad left (leftOrRight == #left) or right" |
14053
acbb97913c30
comment/format in: #padded:to:with:
Claus Gittinger <cg@exept.de>
parents:
14050
diff
changeset
|
1526 |
|
7022 | 1527 |
leftOrRight == #left ifTrue:[ |
20155 | 1528 |
^ self leftPaddedTo:paddedSize with:padCharacter |
9190 | 1529 |
]. |
1530 |
^ self paddedTo:paddedSize with:padCharacter |
|
1531 |
||
1532 |
" |
|
8874 | 1533 |
'hello' padded:#right to:10 with:$. |
9192 | 1534 |
'hello' padded:#left to:10 with:$. |
1535 |
" |
|
14053
acbb97913c30
comment/format in: #padded:to:with:
Claus Gittinger <cg@exept.de>
parents:
14050
diff
changeset
|
1536 |
|
acbb97913c30
comment/format in: #padded:to:with:
Claus Gittinger <cg@exept.de>
parents:
14050
diff
changeset
|
1537 |
"Modified (comment): / 07-03-2012 / 16:31:33 / cg" |
9192 | 1538 |
! |
1539 |
||
1540 |
skipDelimiters:delimiters startingAt:start |
|
1541 |
"Answer the index of the character within the receiver, starting at start, |
|
1542 |
that does NOT match one of the delimiters. |
|
1543 |
If the receiver does not contain any of the delimiters, answer size + 1. |
|
1544 |
Assumes the delimiters to be a non-empty string." |
|
1545 |
||
1546 |
start to:self size do:[:i | |
|
20874 | 1547 |
delimiters detect:[:delim | delim = (self at:i) ] ifNone:[ ^ i ] |
9192 | 1548 |
]. |
1549 |
^ self size + 1 |
|
1550 |
||
1551 |
" |
|
1552 |
'123***7890' skipDelimiters:'*' startingAt:4 |
|
1553 |
'123***7890' skipDelimiters:'*' startingAt:3 |
|
9190 | 1554 |
'123***7890' skipDelimiters:'*' startingAt:10 |
1555 |
'123*******' skipDelimiters:'*' startingAt:10 |
|
8874 | 1556 |
" |
1557 |
! |
|
1558 |
||
9190 | 1559 |
substrings |
18921 | 1560 |
"return a collection consisting of all words contained in the receiver. |
17517 | 1561 |
Words are separated by whitespace. |
1562 |
This has been added for Squeak compatibility. |
|
1563 |
(sigh: it is called #'subStrings' in V'Age, and #'asCollectionOfWords' in ST/X) " |
|
1564 |
||
9190 | 1565 |
^ self asCollectionOfWords |
1566 |
||
1567 |
" |
|
1568 |
'foo bar baz' substrings |
|
1569 |
" |
|
1570 |
! |
|
1571 |
||
12665
b027fc5aec4e
added: #substringsSeparatedBy:
Claus Gittinger <cg@exept.de>
parents:
12664
diff
changeset
|
1572 |
substringsSeparatedBy:separatorCharacter |
18921 | 1573 |
"return a collection consisting of all words contained in the receiver. |
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
1574 |
Words are separated by the given separator character. |
18921 | 1575 |
This has been added for Squeak/Pharo compatibility. |
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
1576 |
(sigh: it is called #'subStrings:' in V'Age, |
18921 | 1577 |
and #'asCollectionOfSubstringsSeparatedBy' in ST/X) " |
18391 | 1578 |
|
12665
b027fc5aec4e
added: #substringsSeparatedBy:
Claus Gittinger <cg@exept.de>
parents:
12664
diff
changeset
|
1579 |
^ self asCollectionOfSubstringsSeparatedBy:separatorCharacter |
b027fc5aec4e
added: #substringsSeparatedBy:
Claus Gittinger <cg@exept.de>
parents:
12664
diff
changeset
|
1580 |
|
b027fc5aec4e
added: #substringsSeparatedBy:
Claus Gittinger <cg@exept.de>
parents:
12664
diff
changeset
|
1581 |
" |
b027fc5aec4e
added: #substringsSeparatedBy:
Claus Gittinger <cg@exept.de>
parents:
12664
diff
changeset
|
1582 |
'foo bar, baz' substringsSeparatedBy:$, |
21151 | 1583 |
'1.2.3.4' substringsSeparatedBy:$. |
12665
b027fc5aec4e
added: #substringsSeparatedBy:
Claus Gittinger <cg@exept.de>
parents:
12664
diff
changeset
|
1584 |
" |
b027fc5aec4e
added: #substringsSeparatedBy:
Claus Gittinger <cg@exept.de>
parents:
12664
diff
changeset
|
1585 |
! |
b027fc5aec4e
added: #substringsSeparatedBy:
Claus Gittinger <cg@exept.de>
parents:
12664
diff
changeset
|
1586 |
|
23188 | 1587 |
trimBoth |
1588 |
"return a copy of the receiver without leading and trailing whiteSpace. |
|
1589 |
Added for Squeak compatibility (an alias for withoutSeparators)" |
|
1590 |
||
1591 |
^ self withoutSeparators |
|
1592 |
||
1593 |
"Created: / 03-07-2018 / 11:08:29 / Claus Gittinger" |
|
1594 |
! |
|
1595 |
||
9190 | 1596 |
truncateTo:smallSize |
9192 | 1597 |
"return myself or a copy shortened to smallSize. 1/18/96 sw" |
1598 |
||
1599 |
self size <= smallSize ifTrue:[^ self]. |
|
1600 |
^ self copyFrom: 1 to: smallSize |
|
1601 |
||
1602 |
" |
|
1603 |
'hello world' truncateTo:5 |
|
1604 |
'hello' truncateTo:10 |
|
1605 |
||
1606 |
'hello world' copyTo:5 |
|
1607 |
'hello' copyTo:10 |
|
1608 |
" |
|
1609 |
! |
|
5795 | 1610 |
|
4890 | 1611 |
withBlanksTrimmed |
18464 | 1612 |
"Return a copy of the receiver from which leading and trailing whitespace have been trimmed. |
1613 |
Notice the bad naming - it is trimming separators, not just blanks. |
|
1614 |
Added for Squeak compatibility and an alias for withoutSeparators" |
|
4890 | 1615 |
|
14865 | 1616 |
^ self withoutSeparators "withoutSpaces" "/ cg: it seems that squeak cares for any whitespace |
4890 | 1617 |
|
1618 |
" |
|
8874 | 1619 |
' hello world ' withBlanksTrimmed |
4890 | 1620 |
" |
1621 |
! |
|
1622 |
||
12188 | 1623 |
withoutLeading: char |
1624 |
"return a copy of myself without leading a char. |
|
1625 |
Returns an empty string, if the receiver consist only of a char." |
|
1626 |
||
1627 |
^ self withoutLeadingForWhich:[:ch | ch = char] |
|
1628 |
||
1629 |
" |
|
13882
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
1630 |
'****foo****' withoutLeading: $* |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
1631 |
'foo****' withoutLeading: $* |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
1632 |
'*' withoutLeading: $* |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
1633 |
'' withoutLeading: $* |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
1634 |
'****foo' withoutLeading: $* |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
1635 |
'*******' withoutLeading: $* |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
1636 |
'foo' withoutLeading: $* |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
1637 |
'f***o***o' withoutLeading: $* |
12188 | 1638 |
('**' , Character tab asString , '*foo***') withoutLeading: $* inspect |
1639 |
" |
|
1640 |
! |
|
1641 |
||
11565 | 1642 |
withoutTrailing:char |
1643 |
"return a copy of myself without trailing char. |
|
1644 |
Returns an empty string, if the receiver consist only of char." |
|
1645 |
||
12188 | 1646 |
^ self withoutTrailingForWhich:[:ch | ch = char] |
1647 |
||
1648 |
" |
|
13882
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
1649 |
' foo....' withoutTrailing:$. |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
1650 |
'foo....' withoutTrailing:$. |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
1651 |
' foo' withoutTrailing:$. |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
1652 |
'.......' withoutTrailing:$. |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
1653 |
'foo' withoutTrailing:$. |
11565 | 1654 |
" |
9190 | 1655 |
! ! |
1656 |
||
1657 |
!CharacterArray methodsFor:'Compatibility-V''Age'! |
|
1658 |
||
1659 |
addLineDelimiter |
|
1660 |
"replace all '\'-characters by line delimiter (cr) - characters. |
|
9192 | 1661 |
This has been added for VisualAge compatibility." |
1662 |
||
1663 |
^ self withCRs |
|
1664 |
! |
|
1665 |
||
1666 |
bindWith:aString |
|
1667 |
"return a copy of the receiver, where a '%1' escape is |
|
1668 |
replaced by aString. |
|
1669 |
This has been added for VisualAge compatibility." |
|
1670 |
||
24038 | 1671 |
^ self expandPlaceholdersWith:{ aString } |
9192 | 1672 |
|
1673 |
" |
|
1674 |
'do you like %1 ?' bindWith:'smalltalk' |
|
19785 | 1675 |
'do you like %(foo) ?' bindWithArguments:(Dictionary new at:'foo' put:'smalltalk'; yourself) |
9192 | 1676 |
" |
24038 | 1677 |
|
1678 |
"Modified (format): / 02-04-2019 / 14:05:22 / Claus Gittinger" |
|
9192 | 1679 |
! |
608 | 1680 |
|
1681 |
bindWith:string1 with:string2 |
|
1682 |
"return a copy of the receiver, where a '%1' escape is |
|
1683 |
replaced by string1 and '%2' is replaced by string2. |
|
1684 |
This has been added for VisualAge compatibility." |
|
1685 |
||
24038 | 1686 |
^ self expandPlaceholdersWith:{string1 . string2} |
608 | 1687 |
|
1688 |
" |
|
1689 |
'do you prefer %1 or rather %2 ?' |
|
20874 | 1690 |
bindWith:'smalltalk' with:'c++' |
9192 | 1691 |
" |
24038 | 1692 |
|
1693 |
"Modified (format): / 02-04-2019 / 14:05:32 / Claus Gittinger" |
|
9192 | 1694 |
! |
9190 | 1695 |
|
1696 |
bindWith:str1 with:str2 with:str3 |
|
608 | 1697 |
"return a copy of the receiver, where a '%1', '%2' and '%3' escapes |
1698 |
are replaced by str1, str2 and str3 respectively. |
|
1699 |
This has been added for VisualAge compatibility." |
|
1700 |
||
24038 | 1701 |
^ self expandPlaceholdersWith:{ str1 . str2 . str3 } |
608 | 1702 |
|
1703 |
" |
|
9192 | 1704 |
'do you prefer %1 or rather %2 (not talking about %3) ?' |
20874 | 1705 |
bindWith:'smalltalk' with:'c++' with:'c' |
9192 | 1706 |
" |
24038 | 1707 |
|
1708 |
"Modified (format): / 02-04-2019 / 14:05:45 / Claus Gittinger" |
|
9192 | 1709 |
! |
608 | 1710 |
|
1711 |
bindWith:str1 with:str2 with:str3 with:str4 |
|
1712 |
"return a copy of the receiver, where a '%1', '%2', '%3' and '%4' escapes |
|
1713 |
are replaced by str1, str2, str3 and str4 respectively. |
|
1714 |
This has been added for VisualAge compatibility." |
|
1715 |
||
24038 | 1716 |
^ self expandPlaceholdersWith:{ str1 . str2 . str3 . str4 } |
608 | 1717 |
|
1718 |
" |
|
9192 | 1719 |
'do you prefer %1 or rather %2 (not talking about %3 or even %4) ?' |
20874 | 1720 |
bindWith:'smalltalk' with:'c++' with:'c' with:'assembler' |
9192 | 1721 |
" |
24038 | 1722 |
|
1723 |
"Modified (format): / 02-04-2019 / 14:06:01 / Claus Gittinger" |
|
9192 | 1724 |
! |
608 | 1725 |
|
2344 | 1726 |
bindWith:str1 with:str2 with:str3 with:str4 with:str5 |
1727 |
"return a copy of the receiver, where a '%1' .. '%5' escapes |
|
1728 |
are replaced by str1 .. str5 respectively. |
|
1729 |
This has been added for VisualAge compatibility." |
|
1730 |
||
24038 | 1731 |
^ self expandPlaceholdersWith:{ str1 . str2 . str3 . str4 . str5 } |
1732 |
||
1733 |
"Created: / 31-01-1997 / 16:25:42 / cg" |
|
1734 |
"Modified: / 02-04-2019 / 14:06:17 / Claus Gittinger" |
|
2344 | 1735 |
! |
1736 |
||
5343 | 1737 |
bindWith:str1 with:str2 with:str3 with:str4 with:str5 with:str6 |
1738 |
"return a copy of the receiver, where a '%1' .. '%6' escapes |
|
11524 | 1739 |
are replaced by str1 .. str6 respectively. |
5343 | 1740 |
This has been added for VisualAge compatibility." |
1741 |
||
24038 | 1742 |
^ self expandPlaceholdersWith:{ str1 . str2 . str3 . str4 . str5 . str6 } |
1743 |
||
1744 |
"Modified (format): / 02-04-2019 / 14:06:36 / Claus Gittinger" |
|
11524 | 1745 |
! |
1746 |
||
1747 |
bindWith:str1 with:str2 with:str3 with:str4 with:str5 with:str6 with:str7 |
|
1748 |
"return a copy of the receiver, where a '%1' .. '%7' escapes |
|
1749 |
are replaced by str1 .. str7 respectively. |
|
1750 |
This has been added for VisualAge compatibility." |
|
1751 |
||
24038 | 1752 |
^ self expandPlaceholdersWith:{ str1 . str2 . str3 . str4 . str5 . str6 . str7 } |
1753 |
||
1754 |
"Modified (format): / 02-04-2019 / 14:06:59 / Claus Gittinger" |
|
5343 | 1755 |
! |
1756 |
||
13996
a86faa4f17b8
added: #bindWith:with:with:with:with:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
13987
diff
changeset
|
1757 |
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
|
1758 |
"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
|
1759 |
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
|
1760 |
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
|
1761 |
|
24038 | 1762 |
^ self expandPlaceholdersWith:{ str1 . str2 . str3 . str4 . str5 . str6 . str7 . str8 } |
13996
a86faa4f17b8
added: #bindWith:with:with:with:with:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
13987
diff
changeset
|
1763 |
|
a86faa4f17b8
added: #bindWith:with:with:with:with:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
13987
diff
changeset
|
1764 |
"Created: / 06-02-2012 / 10:33:18 / cg" |
24038 | 1765 |
"Modified: / 02-04-2019 / 14:07:15 / Claus Gittinger" |
13996
a86faa4f17b8
added: #bindWith:with:with:with:with:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
13987
diff
changeset
|
1766 |
! |
a86faa4f17b8
added: #bindWith:with:with:with:with:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
13987
diff
changeset
|
1767 |
|
14014
3aff2d7c41c8
added: #bindWith:with:with:with:with:with:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
13996
diff
changeset
|
1768 |
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
|
1769 |
"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
|
1770 |
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
|
1771 |
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
|
1772 |
|
24038 | 1773 |
^ self expandPlaceholdersWith:{ str1 . str2 . str3 . str4 . str5 . str6 . str7 . str8 . str9 } |
14014
3aff2d7c41c8
added: #bindWith:with:with:with:with:with:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
13996
diff
changeset
|
1774 |
|
3aff2d7c41c8
added: #bindWith:with:with:with:with:with:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
13996
diff
changeset
|
1775 |
"Created: / 14-02-2012 / 17:42:31 / cg" |
24038 | 1776 |
"Modified: / 02-04-2019 / 14:07:35 / Claus Gittinger" |
14014
3aff2d7c41c8
added: #bindWith:with:with:with:with:with:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
13996
diff
changeset
|
1777 |
! |
3aff2d7c41c8
added: #bindWith:with:with:with:with:with:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
13996
diff
changeset
|
1778 |
|
22924 | 1779 |
bindWithArguments:argumentsCollection |
608 | 1780 |
"return a copy of the receiver, where a '%i' escape |
21747 | 1781 |
is replaced by the corresponding string from the argument array. |
22924 | 1782 |
'i' may be between 1 and 9 (i.e. a maximum of 9 placeholders is allowed) |
1783 |
or %(key); the argumentsCollection must then be a dictionary. |
|
21255 | 1784 |
To get an integer-indexed placeHolder followed by another digit, |
1785 |
or an index > 9, you must use %(digit). |
|
608 | 1786 |
This has been added for VisualAge compatibility." |
1787 |
||
22924 | 1788 |
^ self expandPlaceholdersWith:argumentsCollection |
9192 | 1789 |
|
1790 |
" |
|
1791 |
'do you prefer %1 or rather %2 (not talking about %3) ?' |
|
20874 | 1792 |
bindWithArguments:#('smalltalk' 'c++' 'c') |
12196 | 1793 |
|
1794 |
'do you %(what) ?' |
|
20874 | 1795 |
bindWithArguments:(Dictionary new at:#'what' put:'understand'; yourself) |
9192 | 1796 |
" |
21747 | 1797 |
|
1798 |
"Modified (comment): / 11-05-2017 / 12:42:57 / mawalch" |
|
9192 | 1799 |
! |
608 | 1800 |
|
1801 |
subStrings |
|
18921 | 1802 |
"return a collection consisting of all words contained in the receiver. |
608 | 1803 |
Words are separated by whitespace. |
17517 | 1804 |
This has been added for VisualAge compatibility. |
1805 |
(sigh: it is called #'subbtrings' in Squeak, and #'asCollectionOfWords' in ST/X) " |
|
608 | 1806 |
|
1807 |
^ self asCollectionOfWords |
|
1808 |
||
1809 |
" |
|
1810 |
'hello world, this is smalltalk' subStrings |
|
1811 |
" |
|
1812 |
! |
|
1813 |
||
16181 | 1814 |
subStrings:separatorCharacterOrString |
18921 | 1815 |
"return a collection consisting of all words contained in the receiver. |
608 | 1816 |
Words are separated by separatorCharacter. |
18921 | 1817 |
This is similar to split: (squeak) and asCollectionOfSubstringsSeparatedBy: (st/x) |
1818 |
and has been added for VisualAge compatibility." |
|
608 | 1819 |
|
16181 | 1820 |
separatorCharacterOrString isCharacter ifTrue:[ |
20874 | 1821 |
^ self asCollectionOfSubstringsSeparatedBy:separatorCharacterOrString |
16181 | 1822 |
]. |
1823 |
^ self asCollectionOfSubstringsSeparatedByAny:separatorCharacterOrString |
|
1824 |
||
1825 |
" |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
1826 |
'foo:bar:baz:smalltalk' subStrings:$: |
16181 | 1827 |
'foo:bar:baz:smalltalk' subStrings:':' |
1828 |
'foo.bar,baz-smalltalk' subStrings:'.,-' |
|
9192 | 1829 |
" |
1830 |
! |
|
1831 |
||
1832 |
trimSeparators |
|
18392 | 1833 |
"return a copy of the receiver without leading and trailing whiteSpace. |
18921 | 1834 |
Added for VisualAge compatibility (an alias for withoutSeparators)" |
9192 | 1835 |
|
1836 |
^ self withoutSeparators |
|
24538 | 1837 |
! |
1838 |
||
1839 |
with:aString |
|
1840 |
"return a copy of the receiver, where a '%1' escape is replaced by aString. |
|
1841 |
Added for protocol compatibility with resourcePack xlation." |
|
1842 |
||
1843 |
^ self expandPlaceholdersWith:{ aString } |
|
1844 |
||
1845 |
" |
|
1846 |
'do you like %1 ?' with:'smalltalk' |
|
1847 |
'do you like %(foo) ?' withArguments:(Dictionary new at:'foo' put:'smalltalk'; yourself) |
|
1848 |
" |
|
1849 |
||
1850 |
||
1851 |
! |
|
1852 |
||
1853 |
with:string1 with:string2 |
|
1854 |
"return a copy of the receiver, |
|
1855 |
where a '%1' escape is replaced by string1 and '%2' is replaced by string2. |
|
1856 |
Added for protocol compatibility with resourcePack xlation." |
|
1857 |
||
1858 |
^ self expandPlaceholdersWith:{string1 . string2} |
|
1859 |
||
1860 |
" |
|
1861 |
'do you prefer %1 or rather %2 ?' with:'smalltalk' with:'c++' |
|
1862 |
" |
|
1863 |
||
1864 |
"Modified (format): / 02-04-2019 / 14:05:32 / Claus Gittinger" |
|
1865 |
! |
|
1866 |
||
1867 |
with:str1 with:str2 with:str3 |
|
1868 |
"return a copy of the receiver, |
|
1869 |
where a '%1', '%2' and '%3' escapes are replaced by str1, str2 and str3 respectively. |
|
1870 |
Added for protocol compatibility with resourcePack xlation." |
|
1871 |
||
1872 |
^ self expandPlaceholdersWith:{ str1 . str2 . str3 } |
|
1873 |
||
1874 |
" |
|
1875 |
'do you prefer %1 or rather %2 (not talking about %3) ?' |
|
1876 |
with:'smalltalk' with:'c++' with:'c' |
|
1877 |
" |
|
1878 |
||
1879 |
"Modified (format): / 02-04-2019 / 14:05:45 / Claus Gittinger" |
|
1880 |
! |
|
1881 |
||
1882 |
with:str1 with:str2 with:str3 with:str4 |
|
1883 |
"return a copy of the receiver, where a '%1', '%2', '%3' and '%4' escapes |
|
1884 |
are replaced by str1, str2, str3 and str4 respectively. |
|
1885 |
Added for protocol compatibility with resourcePack xlation." |
|
1886 |
||
1887 |
^ self expandPlaceholdersWith:{ str1 . str2 . str3 . str4 } |
|
1888 |
||
1889 |
" |
|
1890 |
'do you prefer %1 or rather %2 (not talking about %3 or even %4) ?' |
|
1891 |
with:'smalltalk' with:'c++' with:'c' with:'assembler' |
|
1892 |
" |
|
1893 |
||
1894 |
"Modified (format): / 02-04-2019 / 14:06:01 / Claus Gittinger" |
|
1895 |
! |
|
1896 |
||
1897 |
with:str1 with:str2 with:str3 with:str4 with:str5 |
|
1898 |
"return a copy of the receiver, where a '%1' .. '%5' escapes |
|
1899 |
are replaced by str1 .. str5 respectively. |
|
1900 |
Added for protocol compatibility with resourcePack xlation." |
|
1901 |
||
1902 |
^ self expandPlaceholdersWith:{ str1 . str2 . str3 . str4 . str5 } |
|
1903 |
||
1904 |
"Created: / 31-01-1997 / 16:25:42 / cg" |
|
1905 |
"Modified: / 02-04-2019 / 14:06:17 / Claus Gittinger" |
|
1906 |
! |
|
1907 |
||
1908 |
with:str1 with:str2 with:str3 with:str4 with:str5 with:str6 |
|
1909 |
"return a copy of the receiver, where a '%1' .. '%6' escapes |
|
1910 |
are replaced by str1 .. str6 respectively. |
|
1911 |
Added for protocol compatibility with resourcePack xlation." |
|
1912 |
||
1913 |
^ self expandPlaceholdersWith:{ str1 . str2 . str3 . str4 . str5 . str6 } |
|
1914 |
||
1915 |
"Modified (format): / 02-04-2019 / 14:06:36 / Claus Gittinger" |
|
1916 |
! |
|
1917 |
||
1918 |
with:str1 with:str2 with:str3 with:str4 with:str5 with:str6 with:str7 |
|
1919 |
"return a copy of the receiver, where a '%1' .. '%7' escapes |
|
1920 |
are replaced by str1 .. str7 respectively. |
|
1921 |
Added for protocol compatibility with resourcePack xlation." |
|
1922 |
||
1923 |
^ self expandPlaceholdersWith:{ str1 . str2 . str3 . str4 . str5 . str6 . str7 } |
|
1924 |
||
1925 |
"Modified (format): / 02-04-2019 / 14:06:59 / Claus Gittinger" |
|
1926 |
! |
|
1927 |
||
1928 |
with:str1 with:str2 with:str3 with:str4 with:str5 with:str6 with:str7 with:str8 |
|
1929 |
"return a copy of the receiver, where a '%1' .. '%8' escapes |
|
1930 |
are replaced by str1 .. str8 respectively. |
|
1931 |
Added for protocol compatibility with resourcePack xlation." |
|
1932 |
||
1933 |
^ self expandPlaceholdersWith:{ str1 . str2 . str3 . str4 . str5 . str6 . str7 . str8 } |
|
1934 |
||
1935 |
"Created: / 06-02-2012 / 10:33:18 / cg" |
|
1936 |
"Modified: / 02-04-2019 / 14:07:15 / Claus Gittinger" |
|
1937 |
! |
|
1938 |
||
1939 |
with:str1 with:str2 with:str3 with:str4 with:str5 with:str6 with:str7 with:str8 with:str9 |
|
1940 |
"return a copy of the receiver, where a '%1' .. '%9' escapes |
|
1941 |
are replaced by str1 .. str9 respectively. |
|
1942 |
Added for protocol compatibility with resourcePack xlation." |
|
1943 |
||
1944 |
^ self expandPlaceholdersWith:{ str1 . str2 . str3 . str4 . str5 . str6 . str7 . str8 . str9 } |
|
1945 |
||
1946 |
"Created: / 14-02-2012 / 17:42:31 / cg" |
|
1947 |
"Modified: / 02-04-2019 / 14:07:35 / Claus Gittinger" |
|
1948 |
! |
|
1949 |
||
1950 |
withArguments:argumentsCollection |
|
1951 |
"return a copy of the receiver, where the '%i' escapes |
|
1952 |
are replaced by the corresponding string from the argument array. |
|
1953 |
'i' may be between 1 and 9 (i.e. a maximum of 9 placeholders is allowed) |
|
1954 |
or %(key); the argumentsCollection must then be a dictionary. |
|
1955 |
To get an integer-indexed placeHolder followed by another digit, |
|
1956 |
or an index > 9, you must use %(digit). |
|
1957 |
This has been added for VisualAge compatibility." |
|
1958 |
||
1959 |
^ self expandPlaceholdersWith:argumentsCollection |
|
1960 |
||
1961 |
" |
|
1962 |
'do you prefer %1 or rather %2 (not talking about %3) ?' |
|
1963 |
withArguments:#('smalltalk' 'c++' 'c') |
|
1964 |
||
1965 |
'do you %(what) ?' |
|
1966 |
withArguments:(Dictionary new at:#'what' put:'understand'; yourself) |
|
1967 |
" |
|
1968 |
||
1969 |
"Modified (comment): / 11-05-2017 / 12:42:57 / mawalch" |
|
9192 | 1970 |
! ! |
1971 |
||
1972 |
!CharacterArray methodsFor:'Compatibility-VW'! |
|
5964 | 1973 |
|
1974 |
expandMacros |
|
1975 |
"ST80 compatibility - expand '<..>' macros with |
|
1976 |
argument strings. Similar to #bindWith:. |
|
1977 |
Read the comment in #expandMacrosWithArguments: about |
|
1978 |
limited compatibility issues." |
|
1979 |
||
1980 |
^ self expandMacrosWithArguments:#() |
|
1981 |
||
1982 |
" |
|
1983 |
'hello<n>foo' expandMacros |
|
1984 |
" |
|
1985 |
||
9192 | 1986 |
"Modified: / 18.6.1998 / 16:03:02 / cg" |
1987 |
! |
|
1988 |
||
1989 |
expandMacrosWith:arg |
|
5964 | 1990 |
"ST80 compatibility - expand '<..>' macros with |
1991 |
argument strings. Similar to #bindWith:. |
|
1992 |
Read the comment in #expandMacrosWithArguments: about |
|
1993 |
limited compatibility issues." |
|
1994 |
||
24038 | 1995 |
^ self expandMacrosWithArguments:{ arg } |
1996 |
||
1997 |
"Created: / 01-11-1997 / 13:01:28 / cg" |
|
1998 |
"Modified: / 02-04-2019 / 14:09:26 / Claus Gittinger" |
|
9192 | 1999 |
! |
5964 | 2000 |
|
2001 |
expandMacrosWith:arg1 with:arg2 |
|
2002 |
"ST80 compatibility - expand '<..>' macros with |
|
2003 |
argument strings. Similar to #bindWith:. |
|
2004 |
Read the comment in #expandMacrosWithArguments: about |
|
2005 |
limited compatibility issues." |
|
2006 |
||
24038 | 2007 |
^ self expandMacrosWithArguments:{arg1 . arg2 } |
2008 |
||
2009 |
"Modified: / 06-07-1998 / 21:58:14 / cg" |
|
2010 |
"Modified: / 02-04-2019 / 14:09:36 / Claus Gittinger" |
|
5964 | 2011 |
! |
2012 |
||
2013 |
expandMacrosWith:arg1 with:arg2 with:arg3 |
|
2014 |
"ST80 compatibility - expand '<..>' macros with |
|
2015 |
argument strings. Similar to #bindWith:. |
|
2016 |
Read the comment in #expandMacrosWithArguments: about |
|
2017 |
limited compatibility issues." |
|
2018 |
||
24038 | 2019 |
^ self expandMacrosWithArguments:{ arg1 . arg2 . arg3 } |
2020 |
||
2021 |
"Modified (format): / 02-04-2019 / 14:09:48 / Claus Gittinger" |
|
5964 | 2022 |
! |
2023 |
||
10353
65fb7aa2c6c2
added #expandMacrosWith:with:with:with:
Stefan Vogel <sv@exept.de>
parents:
10134
diff
changeset
|
2024 |
expandMacrosWith:arg1 with:arg2 with:arg3 with:arg4 |
65fb7aa2c6c2
added #expandMacrosWith:with:with:with:
Stefan Vogel <sv@exept.de>
parents:
10134
diff
changeset
|
2025 |
"ST80 compatibility - expand '<..>' macros with |
65fb7aa2c6c2
added #expandMacrosWith:with:with:with:
Stefan Vogel <sv@exept.de>
parents:
10134
diff
changeset
|
2026 |
argument strings. Similar to #bindWith:. |
65fb7aa2c6c2
added #expandMacrosWith:with:with:with:
Stefan Vogel <sv@exept.de>
parents:
10134
diff
changeset
|
2027 |
Read the comment in #expandMacrosWithArguments: about |
65fb7aa2c6c2
added #expandMacrosWith:with:with:with:
Stefan Vogel <sv@exept.de>
parents:
10134
diff
changeset
|
2028 |
limited compatibility issues." |
65fb7aa2c6c2
added #expandMacrosWith:with:with:with:
Stefan Vogel <sv@exept.de>
parents:
10134
diff
changeset
|
2029 |
|
24038 | 2030 |
^ self expandMacrosWithArguments:{ arg1 . arg2 . arg3 . arg4 } |
2031 |
||
2032 |
"Modified (format): / 02-04-2019 / 14:10:05 / Claus Gittinger" |
|
10353
65fb7aa2c6c2
added #expandMacrosWith:with:with:with:
Stefan Vogel <sv@exept.de>
parents:
10134
diff
changeset
|
2033 |
! |
65fb7aa2c6c2
added #expandMacrosWith:with:with:with:
Stefan Vogel <sv@exept.de>
parents:
10134
diff
changeset
|
2034 |
|
9192 | 2035 |
expandMacrosWithArguments:argArray |
5964 | 2036 |
"ST80 compatibility - expand '<..>' macros with |
2037 |
argument strings. Similar to #bindWith:. |
|
2038 |
WARNING: possibly not all ST80 expansions are supported here." |
|
2039 |
||
2040 |
"/ supported expansions: |
|
2041 |
"/ |
|
2042 |
"/ <#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
|
2043 |
"/ <#s> # is arg Number; slice in the arg itself (must know asString) |
5964 | 2044 |
"/ <#?s1:s2> # is arg Number; slice in s1 if the arg is true, s2 otherwise |
7643 | 2045 |
"/ use first arg if # is not given (i.e. no number before s,p ...) |
5964 | 2046 |
"/ <n> replace by a newLine character |
2047 |
"/ <t> replace by a tab character |
|
6834
eba4b58b8692
fixed expandMacros (%-escape)
Claus Gittinger <cg@exept.de>
parents:
6810
diff
changeset
|
2048 |
"/ %X the X character itself |
5964 | 2049 |
|
8999
f3fda66ea51e
#expandMacrosWithArguments: - be mor friendly to xml strings.
Stefan Vogel <sv@exept.de>
parents:
8971
diff
changeset
|
2050 |
|in out c fmt nr arg s1 s2 peekc| |
5964 | 2051 |
|
2052 |
in := self readStream. |
|
10721
58e6950264cb
care for bitsPerCharacter when expanding macros
Claus Gittinger <cg@exept.de>
parents:
10703
diff
changeset
|
2053 |
out := CharacterWriteStream on:(self species uninitializedNew:self size). |
5964 | 2054 |
|
2055 |
[in atEnd] whileFalse:[ |
|
20874 | 2056 |
c := in next. |
2057 |
c == $% ifTrue:[ |
|
2058 |
c := in next. |
|
2059 |
out nextPut:c |
|
2060 |
] ifFalse:[c ~~ $< ifTrue:[ |
|
2061 |
out nextPut:c. |
|
2062 |
] ifFalse:[ |
|
2063 |
peekc := in peek. |
|
2064 |
[peekc == $<] whileTrue:[ |
|
2065 |
out nextPut:$<. |
|
2066 |
peekc := in nextPeek. |
|
2067 |
]. |
|
2068 |
peekc == $n ifTrue:[ |
|
2069 |
peekc := in nextPeek. |
|
2070 |
peekc == $> ifTrue:[ |
|
2071 |
in next. |
|
2072 |
out cr. |
|
2073 |
] ifFalse:[ |
|
2074 |
out nextPutAll:'<n'. |
|
2075 |
] |
|
2076 |
] ifFalse:[peekc == $t ifTrue:[ |
|
2077 |
peekc := in nextPeek. |
|
2078 |
peekc == $> ifTrue:[ |
|
2079 |
in next. |
|
2080 |
out tab. |
|
2081 |
] ifFalse:[ |
|
2082 |
out nextPutAll:'<t'. |
|
2083 |
] |
|
2084 |
] ifFalse:[ |
|
2085 |
peekc isDigit ifTrue:[ |
|
2086 |
"start an argument expansion ..." |
|
2087 |
nr := Integer readFrom:in onError:nil. |
|
2088 |
nr isNil ifTrue:[ |
|
2089 |
"this cannot happen (there is at least one digit)" |
|
23009 | 2090 |
self proceedableError:'invalid format'. |
20874 | 2091 |
^ self |
2092 |
]. |
|
2093 |
fmt := in next. |
|
22999 | 2094 |
(fmt ~~ $? and:[fmt ~~ $# and:[in peek ~~ $>]]) ifTrue:[ |
20874 | 2095 |
out nextPut:$<. |
2096 |
nr printOn:out. |
|
2097 |
out nextPut:fmt. |
|
2098 |
] ifFalse:[ |
|
2099 |
(nr between:1 and:argArray size) ifTrue:[ |
|
2100 |
arg := argArray at:nr. |
|
2101 |
] ifFalse:[ |
|
2102 |
arg := '' |
|
2103 |
]. |
|
2104 |
||
2105 |
fmt == $p ifTrue:[ |
|
2106 |
"expand with args printString" |
|
2107 |
arg printOn:out. |
|
2108 |
] ifFalse:[fmt == $s ifTrue:[ |
|
2109 |
"expand with arg itself" |
|
2110 |
arg isText ifTrue:[ |
|
2111 |
out contentsSpecies isText ifFalse:[ |
|
2112 |
out := (TextStream ? CharacterWriteStream on:Text new) nextPutAll:out contents; yourself. |
|
2113 |
]. |
|
2114 |
out nextPutAll:arg. |
|
2115 |
] ifFalse:[ |
|
2116 |
out nextPutAll:arg asString string. "see method comment: arg must know #asString" |
|
2117 |
] |
|
22999 | 2118 |
] ifFalse:[(fmt == $? or:[fmt == $#]) ifTrue:[ |
20874 | 2119 |
s1 := in upTo:$:. |
2120 |
s2 := in nextUpTo:$>. |
|
22999 | 2121 |
((fmt == $? and:[arg == true]) |
23009 | 2122 |
or:[(fmt == $# and:[arg == 1])]) ifTrue:[ |
20874 | 2123 |
out nextPutAll:s1 |
2124 |
] ifFalse:[ |
|
2125 |
out nextPutAll:s2 |
|
2126 |
]. |
|
2127 |
] ifFalse:[ |
|
2128 |
"what does VW do here ?" |
|
2129 |
self error:'invalid format' mayProceed:true. |
|
2130 |
^ self |
|
2131 |
]]]. |
|
2132 |
c := in next. |
|
2133 |
c ~~ $> ifTrue:[ |
|
2134 |
"what does VW do here ?" |
|
2135 |
self error:'invalid format' mayProceed:true. |
|
2136 |
^ self |
|
2137 |
] |
|
2138 |
||
2139 |
]. |
|
2140 |
] ifFalse:[ |
|
2141 |
out nextPut:$<. |
|
2142 |
]. |
|
2143 |
]]. |
|
2144 |
]]. |
|
5964 | 2145 |
]. |
2146 |
^ out contents |
|
2147 |
||
2148 |
" |
|
8874 | 2149 |
'hello <1s> how are you' expandMacrosWith:(OperatingSystem getLoginName) |
2150 |
'one plus one is <1p>' expandMacrosWith:2 |
|
5964 | 2151 |
" |
2152 |
||
10721
58e6950264cb
care for bitsPerCharacter when expanding macros
Claus Gittinger <cg@exept.de>
parents:
10703
diff
changeset
|
2153 |
"Modified: / 18-09-2007 / 22:50:43 / cg" |
23009 | 2154 |
"Modified: / 24-05-2018 / 21:06:31 / Claus Gittinger" |
9192 | 2155 |
! |
2156 |
||
2157 |
isCharacters |
|
21318 | 2158 |
"true, if the receiver is a string-like thing. |
2159 |
added for visual works compatibility" |
|
2160 |
||
2161 |
"/ kept in libbasic package, because it is used in libjava and imap-implementation |
|
9192 | 2162 |
^ true |
2163 |
! ! |
|
2164 |
||
16295 | 2165 |
!CharacterArray methodsFor:'JavaScript support'! |
2166 |
||
2167 |
unquote |
|
17000 | 2168 |
"removes double quotes from the receiver. |
19698 | 2169 |
This is the JavaScript standard unquote function." |
2170 |
||
2171 |
^ self unquote:$" |
|
16295 | 2172 |
|
2173 |
" |
|
17000 | 2174 |
'hello' quote unquote |
2175 |
||
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2176 |
JavaScriptParser evaluate:'''hello''.quote.unquote' |
16295 | 2177 |
" |
19698 | 2178 |
! |
2179 |
||
2180 |
unquote:quoteCharacter |
|
2181 |
"removes quoteCharacter from either end of the receiver." |
|
2182 |
||
2183 |
|mySize| |
|
2184 |
||
2185 |
(mySize := self size) >= 2 ifTrue:[ |
|
20874 | 2186 |
((self first == quoteCharacter) and:[self last == quoteCharacter]) ifTrue:[ |
2187 |
^ self copyFrom:2 to:mySize-1 |
|
2188 |
]. |
|
19698 | 2189 |
]. |
2190 |
^ self |
|
2191 |
||
2192 |
" |
|
2193 |
'*hello*' unquote:$* |
|
2194 |
" |
|
16295 | 2195 |
! ! |
13510 | 2196 |
|
16175 | 2197 |
|
9192 | 2198 |
!CharacterArray methodsFor:'character searching'! |
2199 |
||
2200 |
includesMatchCharacters |
|
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
2201 |
"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
|
2202 |
for match operations; false if not. |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2203 |
Here, do not care for $\ escapes" |
608 | 2204 |
|
2205 |
^ self includesAny:'*#[' |
|
2519
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2206 |
|
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2207 |
" |
8874 | 2208 |
'*foo' includesMatchCharacters |
2209 |
'\*foo' includesMatchCharacters |
|
2210 |
'\*foo' includesUnescapedMatchCharacters |
|
2211 |
'*foo' includesMatchCharacters |
|
2519
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2212 |
'\\*foo' includesMatchCharacters |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2213 |
'foo*' includesMatchCharacters |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2214 |
'foo\*' includesMatchCharacters |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2215 |
'foo\' includesMatchCharacters |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2216 |
" |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2217 |
|
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2218 |
"Modified: 2.4.1997 / 18:12:34 / cg" |
9192 | 2219 |
! |
2220 |
||
2221 |
includesSeparator |
|
2222 |
"return true, if the receiver contains any whitespace characters" |
|
2223 |
||
2224 |
^ (self indexOfSeparator ~~ 0) |
|
2225 |
||
2226 |
" |
|
2227 |
'hello world' includesSeparator |
|
2228 |
'helloworld' includesSeparator |
|
2229 |
" |
|
2230 |
! |
|
2231 |
||
2232 |
includesUnescapedMatchCharacters |
|
8874 | 2233 |
"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
|
2234 |
for match operations; false if not. |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2235 |
Here, care for $\ escapes" |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2236 |
|
5342
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
2237 |
|idx sz specialChars escape| |
2519
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2238 |
|
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2239 |
idx := 1. |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2240 |
sz := self size. |
5342
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
2241 |
specialChars := '*#[\'. |
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
2242 |
(escape := self class matchEscapeCharacter) ~~ $\ ifTrue:[ |
20874 | 2243 |
specialChars := specialChars copy. |
2244 |
specialChars at:specialChars size put:escape |
|
5342
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
2245 |
]. |
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
2246 |
|
16245 | 2247 |
[ |
20874 | 2248 |
idx := self indexOfAny:specialChars startingAt:idx. |
2249 |
idx == 0 ifTrue:[^ false]. |
|
2250 |
(self at:idx) == escape ifFalse:[^ true]. |
|
2251 |
idx := idx + 2. |
|
2252 |
idx > sz ifTrue:[^ false]. |
|
16245 | 2253 |
] loop. |
8874 | 2254 |
|
2255 |
" |
|
2256 |
'*foo' includesUnescapedMatchCharacters |
|
2257 |
'\*foo' includesUnescapedMatchCharacters |
|
2258 |
'\\foo' includesUnescapedMatchCharacters |
|
2259 |
'\\\$foo' includesUnescapedMatchCharacters |
|
2519
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2260 |
'*foo' includesUnescapedMatchCharacters |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2261 |
'\\*foo' includesUnescapedMatchCharacters |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2262 |
'foo*' includesUnescapedMatchCharacters |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2263 |
'foo\*' includesUnescapedMatchCharacters |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2264 |
'foo\' includesUnescapedMatchCharacters |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2265 |
" |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2266 |
|
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2267 |
"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
|
2268 |
"Created: 2.4.1997 / 17:23:26 / cg" |
9192 | 2269 |
! |
2270 |
||
2271 |
indexOfControlCharacterStartingAt:startIndex |
|
3668
76271941f044
added #indexOfControlCharacterStartingAt:
Claus Gittinger <cg@exept.de>
parents:
3656
diff
changeset
|
2272 |
"return the index of the next control character; |
76271941f044
added #indexOfControlCharacterStartingAt:
Claus Gittinger <cg@exept.de>
parents:
3656
diff
changeset
|
2273 |
that is a character with asciiValue < 32. |
15605 | 2274 |
Start the search at startIndex, searching forward. |
3668
76271941f044
added #indexOfControlCharacterStartingAt:
Claus Gittinger <cg@exept.de>
parents:
3656
diff
changeset
|
2275 |
Return 0 if none is found." |
76271941f044
added #indexOfControlCharacterStartingAt:
Claus Gittinger <cg@exept.de>
parents:
3656
diff
changeset
|
2276 |
|
76271941f044
added #indexOfControlCharacterStartingAt:
Claus Gittinger <cg@exept.de>
parents:
3656
diff
changeset
|
2277 |
|start "{ Class: SmallInteger }" |
9192 | 2278 |
mySize "{ Class: SmallInteger }"| |
2279 |
||
2280 |
start := startIndex. |
|
2281 |
mySize := self size. |
|
2282 |
||
2283 |
start to:mySize do:[:index | |
|
20874 | 2284 |
(self at:index) isControlCharacter ifTrue:[^ index] |
9192 | 2285 |
]. |
2286 |
^ 0 |
|
2287 |
||
2288 |
" |
|
2289 |
'hello world' asTwoByteString indexOfControlCharacterStartingAt:1 |
|
2290 |
'hello world\foo' withCRsasTwoByteString indexOfControlCharacterStartingAt:1 |
|
2291 |
" |
|
2292 |
||
2293 |
"Modified: / 21.7.1998 / 17:25:07 / cg" |
|
2294 |
! |
|
3668
76271941f044
added #indexOfControlCharacterStartingAt:
Claus Gittinger <cg@exept.de>
parents:
3656
diff
changeset
|
2295 |
|
8541 | 2296 |
indexOfNonSeparator |
2297 |
"return the index of the first non-whitespace character. |
|
2298 |
return 0 if no non-separator was found" |
|
2299 |
||
2300 |
^ self indexOfNonSeparatorStartingAt:1. |
|
2301 |
||
2302 |
" |
|
8874 | 2303 |
' hello world' indexOfNonSeparator |
2304 |
' ' indexOfNonSeparator |
|
2305 |
'a ' indexOfNonSeparator |
|
2306 |
'abc' indexOfNonSeparator |
|
2307 |
' ' indexOfNonSeparator |
|
9192 | 2308 |
'' indexOfNonSeparator |
2309 |
" |
|
2310 |
! |
|
2311 |
||
2312 |
indexOfNonSeparatorStartingAt:startIndex |
|
4117 | 2313 |
"return the index of the next non-whitespace character, |
2314 |
starting the search at startIndex, searching forward; |
|
2315 |
return 0 if no non-separator was found" |
|
608 | 2316 |
|
2317 |
|start "{ Class: SmallInteger }" |
|
2318 |
mySize "{ Class: SmallInteger }"| |
|
2319 |
||
2320 |
start := startIndex. |
|
2321 |
mySize := self size. |
|
2322 |
||
2323 |
start to:mySize do:[:index | |
|
20874 | 2324 |
(self at:index) isSeparator ifFalse:[^ index] |
608 | 2325 |
]. |
2326 |
^ 0 |
|
2327 |
||
2328 |
" |
|
8874 | 2329 |
' hello world' indexOfNonSeparatorStartingAt:1 |
2330 |
' ' indexOfNonSeparatorStartingAt:1 |
|
2331 |
'a ' indexOfNonSeparatorStartingAt:2 |
|
4117 | 2332 |
" |
2333 |
||
608 | 2334 |
" |
2335 |
|s index1 index2| |
|
2336 |
s := ' foo bar baz'. |
|
2337 |
index1 := s indexOfNonSeparatorStartingAt:1. |
|
2338 |
index2 := s indexOfSeparatorStartingAt:index1. |
|
2339 |
s copyFrom:index1 to:index2 - 1 |
|
2340 |
" |
|
9192 | 2341 |
! |
2342 |
||
2343 |
indexOfSeparator |
|
2344 |
"return the index of the first whitespace character; |
|
2345 |
starting the search at the beginning, searching forward; |
|
2346 |
return 0 if no separator was found" |
|
2347 |
||
2348 |
^ self indexOfSeparatorStartingAt:1 |
|
2349 |
||
2350 |
" |
|
2351 |
'hello world' indexOfSeparator |
|
2352 |
'helloworld' indexOfSeparator |
|
2353 |
'hello ' indexOfSeparator |
|
2354 |
' hello' indexOfSeparator |
|
2355 |
" |
|
2356 |
! |
|
2357 |
||
15912 | 2358 |
indexOfSeparatorOrEndStartingAt:startIndex |
2359 |
"return the index of the next whitespace character, |
|
2360 |
starting the search at startIndex, searching forward; |
|
19737 | 2361 |
return the index of one beyond the end of the receiver if no separator was found. |
15912 | 2362 |
To extract the word, copy from startIndex to the returned index-1" |
2363 |
||
2364 |
|idx| |
|
2365 |
||
2366 |
idx := self indexOfSeparatorStartingAt:startIndex. |
|
2367 |
idx == 0 ifTrue:[^ self size + 1]. |
|
2368 |
^ idx. |
|
2369 |
||
2370 |
" |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2371 |
'hello world' indexOfSeparatorOrEndStartingAt:3 |
15912 | 2372 |
' hello world' indexOfSeparatorOrEndStartingAt:3 |
2373 |
'hello world ' indexOfSeparatorOrEndStartingAt:3 |
|
2374 |
'hello world ' indexOfSeparatorOrEndStartingAt:6 |
|
2375 |
'hello world ' indexOfSeparatorOrEndStartingAt:7 |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2376 |
'helloworld ' indexOfSeparatorOrEndStartingAt:7 |
15912 | 2377 |
'helloworld' indexOfSeparatorOrEndStartingAt:7 |
19737 | 2378 |
'helloworld' indexOfSeparatorStartingAt:7 |
15912 | 2379 |
" |
2380 |
! |
|
2381 |
||
9192 | 2382 |
indexOfSeparatorStartingAt:startIndex |
2383 |
"return the index of the next whitespace character, |
|
2384 |
starting the search at startIndex, searching forward; |
|
2385 |
return 0 if no separator was found" |
|
2386 |
||
2387 |
|start "{ Class: SmallInteger }" |
|
2388 |
mySize "{ Class: SmallInteger }"| |
|
2389 |
||
2390 |
start := startIndex. |
|
2391 |
mySize := self size. |
|
2392 |
||
2393 |
start to:mySize do:[:index | |
|
20874 | 2394 |
(self at:index) isSeparator ifTrue:[^ index] |
9192 | 2395 |
]. |
2396 |
^ 0 |
|
2397 |
||
2398 |
" |
|
2399 |
'hello world' indexOfSeparatorStartingAt:3 |
|
2400 |
' hello world' indexOfSeparatorStartingAt:3 |
|
8874 | 2401 |
'hello world ' indexOfSeparatorStartingAt:3 |
2402 |
'hello world ' indexOfSeparatorStartingAt:6 |
|
2403 |
'hello world ' indexOfSeparatorStartingAt:7 |
|
2404 |
'helloworld ' indexOfSeparatorStartingAt:7 |
|
2405 |
'helloworld' indexOfSeparatorStartingAt:7 |
|
4117 | 2406 |
" |
2519
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2407 |
! |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2408 |
|
4117 | 2409 |
lastIndexOfSeparator |
14150
e13160aa321a
comment/format in: #lastIndexOfSeparator
Claus Gittinger <cg@exept.de>
parents:
14141
diff
changeset
|
2410 |
"return the last index of a whitespace character (space or tab). |
4117 | 2411 |
(i.e. start the search at the end and search backwards); |
2412 |
Returns 0 if no separator is found." |
|
2413 |
||
2414 |
^ self lastIndexOfSeparatorStartingAt:(self size) |
|
2415 |
||
2416 |
" |
|
8874 | 2417 |
'hello world' lastIndexOfSeparator |
2418 |
'helloworld' lastIndexOfSeparator |
|
2419 |
'hel lo wor ld' lastIndexOfSeparator |
|
2420 |
'hel ' lastIndexOfSeparator 6 |
|
4117 | 2421 |
" |
14150
e13160aa321a
comment/format in: #lastIndexOfSeparator
Claus Gittinger <cg@exept.de>
parents:
14141
diff
changeset
|
2422 |
|
e13160aa321a
comment/format in: #lastIndexOfSeparator
Claus Gittinger <cg@exept.de>
parents:
14141
diff
changeset
|
2423 |
"Modified (comment): / 01-06-2012 / 13:10:30 / cg" |
4117 | 2424 |
! |
2425 |
||
2426 |
lastIndexOfSeparatorStartingAt:startIndex |
|
9192 | 2427 |
"return the index of the previous whitespace character, |
2428 |
starting the search at startIndex (and searching backwards); |
|
2429 |
returns 0 if no separator was found" |
|
2430 |
||
2431 |
|start "{ Class: SmallInteger }"| |
|
2432 |
||
2433 |
start := startIndex. |
|
2434 |
||
2435 |
start to:1 by:-1 do:[:index | |
|
20874 | 2436 |
(self at:index) isSeparator ifTrue:[^ index] |
9192 | 2437 |
]. |
2438 |
^ 0 |
|
2439 |
||
2440 |
" |
|
2441 |
'hello world' lastIndexOfSeparatorStartingAt:3 |
|
2442 |
'hello world' lastIndexOfSeparatorStartingAt:7 |
|
2443 |
'helloworld' lastIndexOfSeparatorStartingAt:7 |
|
8874 | 2444 |
' helloworld' lastIndexOfSeparatorStartingAt:7 |
4117 | 2445 |
" |
608 | 2446 |
! ! |
2447 |
||
2448 |
!CharacterArray methodsFor:'comparing'! |
|
2449 |
||
6742
292775fda600
avoid endless recursion when comparing int < string
Claus Gittinger <cg@exept.de>
parents:
6655
diff
changeset
|
2450 |
< aString |
608 | 2451 |
"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
|
2452 |
receiver is greater than the argument. Otherwise return false. |
8874 | 2453 |
This comparison is based on the elements ascii code - |
9192 | 2454 |
i.e. upper/lowercase & upper/lowercase & national characters are NOT treated specially." |
2455 |
||
2456 |
|mySize "{ Class: SmallInteger }" |
|
2457 |
otherSize "{ Class: SmallInteger }" |
|
2458 |
n "{ Class: SmallInteger }" |
|
2459 |
c1 c2| |
|
2460 |
||
2461 |
mySize := self size. |
|
2462 |
otherSize := aString string size. |
|
6742
292775fda600
avoid endless recursion when comparing int < string
Claus Gittinger <cg@exept.de>
parents:
6655
diff
changeset
|
2463 |
n := mySize min:otherSize. |
292775fda600
avoid endless recursion when comparing int < string
Claus Gittinger <cg@exept.de>
parents:
6655
diff
changeset
|
2464 |
|
292775fda600
avoid endless recursion when comparing int < string
Claus Gittinger <cg@exept.de>
parents:
6655
diff
changeset
|
2465 |
1 to:n do:[:index | |
20874 | 2466 |
c1 := self at:index. |
2467 |
c2 := aString at:index. |
|
2468 |
(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
|
2469 |
]. |
292775fda600
avoid endless recursion when comparing int < string
Claus Gittinger <cg@exept.de>
parents:
6655
diff
changeset
|
2470 |
^ mySize < otherSize |
608 | 2471 |
! |
2472 |
||
2473 |
= aString |
|
2474 |
"Compare the receiver with the argument and return true if the |
|
2475 |
receiver is equal to the argument. Otherwise return false. |
|
1252 | 2476 |
|
9191 | 2477 |
This compare does NOT ignore case differences, |
608 | 2478 |
therefore 'foo' = 'Foo' will return false. |
2479 |
Since this is incompatible to ST-80 (at least, V2.x) , this may change." |
|
2480 |
||
14137
1eb1b4a3f364
Inherit from UninterpretedBytes instead of ByteArray
Stefan Vogel <sv@exept.de>
parents:
14122
diff
changeset
|
2481 |
|mySize "{ Class: SmallInteger }"| |
608 | 2482 |
|
5963
23462e06dcff
compare with Texts fixed (should compare equal if strings are)
Claus Gittinger <cg@exept.de>
parents:
5946
diff
changeset
|
2483 |
(aString isString or:[aString species == self species]) ifFalse:[ |
20874 | 2484 |
^ false |
5963
23462e06dcff
compare with Texts fixed (should compare equal if strings are)
Claus Gittinger <cg@exept.de>
parents:
5946
diff
changeset
|
2485 |
]. |
608 | 2486 |
mySize := self size. |
14137
1eb1b4a3f364
Inherit from UninterpretedBytes instead of ByteArray
Stefan Vogel <sv@exept.de>
parents:
14122
diff
changeset
|
2487 |
mySize ~~ (aString size) ifTrue:[^ false]. |
608 | 2488 |
|
2489 |
1 to:mySize do:[:index | |
|
20874 | 2490 |
(self at:index) = (aString at:index) ifFalse:[^ false]. |
608 | 2491 |
]. |
2492 |
^ true |
|
2493 |
||
2494 |
" |
|
8874 | 2495 |
'foo' = 'Foo' |
2496 |
'foo' = 'bar' |
|
2497 |
'foo' = 'foo' |
|
2498 |
'foo' = 'foo' asText |
|
2499 |
'foo' asText = 'foo' |
|
2500 |
'foo' asText = 'foo' asText |
|
608 | 2501 |
" |
1252 | 2502 |
|
2503 |
"Modified: 22.4.1996 / 15:53:58 / cg" |
|
608 | 2504 |
! |
9192 | 2505 |
|
9191 | 2506 |
> aString |
608 | 2507 |
"Compare the receiver with the argument and return true if the |
2508 |
receiver is greater than the argument. Otherwise return false. |
|
8874 | 2509 |
This comparison is based on the elements ascii code - |
1252 | 2510 |
i.e. upper/lowercase & upper/lowercase & national characters are NOT treated specially." |
608 | 2511 |
|
2512 |
|mySize "{ Class: SmallInteger }" |
|
8874 | 2513 |
otherSize "{ Class: SmallInteger }" |
9192 | 2514 |
n "{ Class: SmallInteger }" |
2515 |
c1 c2| |
|
2516 |
||
2517 |
mySize := self size. |
|
5929 | 2518 |
otherSize := aString string size. |
2519 |
n := mySize min:otherSize. |
|
2520 |
||
2521 |
1 to:n do:[:index | |
|
20874 | 2522 |
c1 := self at:index. |
2523 |
c2 := aString at:index. |
|
2524 |
(c1 == c2 or:[c1 = c2]) ifFalse:[^ c1 > c2]. |
|
5929 | 2525 |
]. |
9192 | 2526 |
^ mySize > otherSize |
2527 |
||
2528 |
"Modified: 22.4.1996 / 15:55:00 / cg" |
|
2529 |
! |
|
2530 |
||
14660 | 2531 |
after:aString |
23155 | 2532 |
<resource: #obsolete> |
14660 | 2533 |
"Compare the receiver with the argument and return true if the |
2534 |
receiver should come after the argument in a sorted list. |
|
2535 |
Otherwise return false. |
|
2536 |
NOTE: The comparison should be language specific, depending on the value of |
|
20874 | 2537 |
LC_COLLATE, which is initialized from the environment. |
2538 |
||
2539 |
Currently it is for Strings, but not for UnicodeStrings... |
|
14660 | 2540 |
|
2541 |
STUPID: |
|
23155 | 2542 |
#after has a completely different meaning in SeqColl... |
2543 |
... therefore it is marked as obsolete. |
|
2544 |
" |
|
14660 | 2545 |
|
2546 |
^ (self compareCollatingWith:aString) > 0 |
|
23155 | 2547 |
|
2548 |
"Modified (comment): / 29-06-2018 / 11:41:33 / Claus Gittinger" |
|
14660 | 2549 |
! |
2550 |
||
24232 | 2551 |
caselessAfter:aString |
2552 |
"True if the receiver comes after aString, if compared caseless. |
|
2553 |
(i.e. if receiver > aString, ignoring case)" |
|
2554 |
||
2555 |
^ (self compareCaselessWith:aString) > 0 |
|
2556 |
||
2557 |
" |
|
2558 |
'aaa1' > 'aaA2' -> true |
|
2559 |
'aaa1' caselessAfter: 'aaA2' -> false |
|
2560 |
" |
|
2561 |
||
2562 |
"Created: / 29-05-2019 / 10:09:55 / Claus Gittinger" |
|
2563 |
! |
|
2564 |
||
2565 |
caselessBefore:aString |
|
2566 |
"True if the receiver comes before aString, if compared caseless. |
|
2567 |
(i.e. if receiver < aString, ignoring case)" |
|
2568 |
||
2569 |
^ (self compareCaselessWith:aString) < 0 |
|
2570 |
||
2571 |
" |
|
2572 |
'aaa1' < 'aaA2' -> false |
|
2573 |
'aaa1' caselessBefore: 'aaA2' -> true |
|
2574 |
" |
|
2575 |
||
2576 |
"Created: / 29-05-2019 / 10:09:18 / Claus Gittinger" |
|
2577 |
! |
|
2578 |
||
2579 |
caselessEqual:aString |
|
2580 |
"True if the receiver has the same characters as aString, if compared caseless. |
|
2581 |
(i.e. if receiver = aString, ignoring case)" |
|
2582 |
||
2583 |
^ (self sameAs:aString) |
|
2584 |
||
2585 |
" |
|
2586 |
'aaa1' = 'aaA1' -> false |
|
2587 |
'aaa1' caselessEqual: 'aaA1' -> true |
|
2588 |
" |
|
2589 |
||
2590 |
"Created: / 29-05-2019 / 10:10:53 / Claus Gittinger" |
|
2591 |
! |
|
2592 |
||
15814 | 2593 |
compareAsVersionNumberWith:aStringOrCollection |
2594 |
"Compare the receiver with the argument and return 1 if the receiver is |
|
2595 |
greater, 0 if equal and -1 if less than the argument in a sorted list. |
|
2596 |
Compare as version numbers in the form a.b.c... ." |
|
2597 |
||
23121 | 2598 |
|
2599 |
^ self asVersionNumberCollection |
|
2600 |
compareWith:aStringOrCollection asVersionNumberCollection |
|
15814 | 2601 |
|
2602 |
" |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2603 |
self assert:('1' compareAsVersionNumberWith:'2') < 0. |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2604 |
self assert:('2' compareAsVersionNumberWith:'1') > 0. |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2605 |
self assert:('1.1' compareAsVersionNumberWith:'2.1.2') < 0. |
23121 | 2606 |
self assert:('1.1a' compareAsVersionNumberWith:'2.1.2') < 0. |
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2607 |
self assert:('2.1' compareAsVersionNumberWith:'1.2.3') > 0. |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2608 |
self assert:('1' compareAsVersionNumberWith:'1.1') < 0. |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2609 |
self assert:('1.1' compareAsVersionNumberWith:'1') > 0. |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2610 |
self assert:('1.1' compareAsVersionNumberWith:'1.2') < 0. |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2611 |
self assert:('1.10' compareAsVersionNumberWith:'1.2') > 0. |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2612 |
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
|
2613 |
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
|
2614 |
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
|
2615 |
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
|
2616 |
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
|
2617 |
self assert:('1.2.3.4' compareAsVersionNumberWith:#(1 2 3 4)) = 0. |
22325 | 2618 |
self assert:('1.2.3.4' compareAsVersionNumberWith:#('1' 2 3 4)) = 0. |
23121 | 2619 |
|
2620 |
self assert:('1.1' compareAsVersionNumberWith:'1.1a') < 0. |
|
2621 |
||
22325 | 2622 |
" |
2623 |
||
2624 |
"Modified: / 25-10-2017 / 11:25:08 / stefan" |
|
23121 | 2625 |
"Modified (format): / 20-06-2018 / 19:23:18 / Stefan Vogel" |
15814 | 2626 |
! |
2627 |
||
9192 | 2628 |
compareCaselessWith:aString |
22327 | 2629 |
"Compare the receiver against the argument, ignoring case. |
9190 | 2630 |
Return 1 if the receiver is greater, 0 if equal and -1 if less than the argument. |
2631 |
||
8874 | 2632 |
This comparison is based on the elements ascii code - |
9190 | 2633 |
i.e. national characters are NOT treated specially. |
2634 |
'foo' compareWith: 'Foo' will return 0" |
|
608 | 2635 |
|
2636 |
|mySize "{ Class: SmallInteger }" |
|
8874 | 2637 |
otherSize "{ Class: SmallInteger }" |
9192 | 2638 |
n "{ Class: SmallInteger }" |
608 | 2639 |
c1 c2| |
2640 |
||
2641 |
mySize := self size. |
|
5739
4fd3be7ccc75
oops some relational ops did accept a non-string arg
Claus Gittinger <cg@exept.de>
parents:
5732
diff
changeset
|
2642 |
otherSize := aString string size. |
608 | 2643 |
n := mySize min:otherSize. |
2644 |
||
2645 |
1 to:n do:[:index | |
|
20874 | 2646 |
c1 := (self at:index) asLowercase. |
2647 |
c2 := (aString at:index) asLowercase. |
|
2648 |
c1 > c2 ifTrue:[^ 1]. |
|
2649 |
c1 < c2 ifTrue:[^ -1]. |
|
608 | 2650 |
]. |
2651 |
mySize > otherSize ifTrue:[^ 1]. |
|
2652 |
mySize < otherSize ifTrue:[^ -1]. |
|
2653 |
^ 0 |
|
1252 | 2654 |
|
24232 | 2655 |
" |
2656 |
'aaa1' < 'aaA2' -> false |
|
2657 |
'aaa1' compareCaselessWith: 'aaA2' -> -1 |
|
2658 |
" |
|
2659 |
||
22327 | 2660 |
"Modified: / 22-04-1996 / 15:56:07 / cg" |
2661 |
"Modified (comment): / 26-10-2017 / 16:01:01 / mawalch" |
|
24232 | 2662 |
"Modified (comment): / 29-05-2019 / 10:06:00 / Claus Gittinger" |
608 | 2663 |
! |
2664 |
||
14660 | 2665 |
compareCollatingWith:aString |
2666 |
"Compare the receiver with the argument and return 1 if the receiver is |
|
2667 |
greater, 0 if equal and -1 if less than the argument in a sorted list. |
|
2668 |
The comparison is language specific, depending on the value of |
|
2669 |
LC_COLLATE, which is in the shell environment." |
|
2670 |
||
2671 |
"TODO not yet defined for unicode" |
|
2672 |
<resource: #todo> |
|
2673 |
||
2674 |
|s| |
|
2675 |
||
2676 |
(s := self string) ~~ self ifTrue:[ |
|
20874 | 2677 |
^ s compareCollatingWith:aString |
14660 | 2678 |
]. |
2679 |
^ self compareWith:aString |
|
2680 |
! |
|
2681 |
||
9192 | 2682 |
compareWith:aString |
9190 | 2683 |
"Compare the receiver with the argument and return 1 if the receiver is |
2684 |
greater, 0 if equal and -1 if less than the argument. |
|
11861 | 2685 |
This comparison is based on the elements' codepoints - |
9190 | 2686 |
i.e. upper/lowercase & national characters are NOT treated specially. |
2687 |
'foo' compareWith: 'Foo' will return 1. |
|
2688 |
while 'foo' sameAs:'Foo' will return true" |
|
2689 |
||
15814 | 2690 |
|s| |
2691 |
||
2692 |
s := self string. |
|
2693 |
s ~~ self ifTrue:[ |
|
20874 | 2694 |
^ s compareWith:aString string. |
9192 | 2695 |
]. |
15814 | 2696 |
^ super compareWith:aString string. |
9192 | 2697 |
! |
2698 |
||
11861 | 2699 |
endsWith:aStringOrCharacter |
16175 | 2700 |
"return true, if the receiver ends with something, aStringOrCharacter. |
2701 |
If aStringOrCharacter is empty, true is returned" |
|
11861 | 2702 |
|
2703 |
|s| |
|
2704 |
||
2705 |
(s := self string) ~~ self ifTrue:[ |
|
20874 | 2706 |
^ s endsWith:aStringOrCharacter |
11861 | 2707 |
]. |
21083 | 2708 |
(self size ~~ 0 and:[aStringOrCharacter isCharacter]) ifTrue:[ |
20874 | 2709 |
^ self last = aStringOrCharacter |
11861 | 2710 |
]. |
2711 |
^ super endsWith:aStringOrCharacter |
|
2712 |
||
2713 |
" |
|
2714 |
'hello world' endsWith:'world' |
|
2715 |
'hello world' asText allBold endsWith:'world' |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2716 |
'hello world' endsWith:'' |
16175 | 2717 |
'hello world' asText allBold endsWith:'' |
11861 | 2718 |
" |
2719 |
||
2720 |
"Modified: 12.5.1996 / 15:49:18 / cg" |
|
2721 |
! |
|
2722 |
||
23154 | 2723 |
endsWith:aStringOrCharacter caseSensitive:caseSensitive |
2724 |
"return true, if the receiver ends with something, aStringOrCharacter. |
|
2725 |
If aStringOrCharacter is empty, true is returned" |
|
2726 |
||
2727 |
|s| |
|
2728 |
||
2729 |
caseSensitive ifTrue:[^ self endsWith:aStringOrCharacter]. |
|
2730 |
||
2731 |
(s := self string) ~~ self ifTrue:[ |
|
2732 |
^ s endsWith:aStringOrCharacter |
|
2733 |
]. |
|
2734 |
(self size ~~ 0 and:[aStringOrCharacter isCharacter]) ifTrue:[ |
|
2735 |
^ self last asLowercase = aStringOrCharacter asLowercase |
|
2736 |
]. |
|
2737 |
^ self endsWith:aStringOrCharacter using:[:a :b | a asLowercase = b asLowercase]. |
|
2738 |
||
2739 |
" |
|
2740 |
'hello World' endsWith:'world' caseSensitive:true |
|
2741 |
'hello World' endsWith:'world' caseSensitive:false |
|
2742 |
" |
|
2743 |
||
2744 |
"Created: / 29-06-2018 / 11:38:57 / Claus Gittinger" |
|
2745 |
! |
|
2746 |
||
23783 | 2747 |
endsWithDigit |
2748 |
"Answer whether the receiver's final character represents a digit. 3/11/96 sw" |
|
2749 |
||
2750 |
|len| |
|
2751 |
||
2752 |
^ (len := self size) ~~ 0 and:[(self at:len) isDigit] |
|
24300 | 2753 |
" |
2754 |
'hello' endsWithDigit |
|
2755 |
'12hello' endsWithDigit |
|
2756 |
'hello12' endsWithDigit |
|
2757 |
" |
|
2758 |
" |
|
2759 |
'hello' startsWithDigit |
|
2760 |
'12hello' startsWithDigit |
|
2761 |
'hello12' startsWithDigit |
|
2762 |
" |
|
2763 |
||
2764 |
"Modified (comment): / 08-06-2019 / 17:23:38 / Claus Gittinger" |
|
23783 | 2765 |
! |
2766 |
||
11838 | 2767 |
hammingDistanceTo:aString |
2768 |
"return the hamming distance (the number of characters which are different). |
|
12177 | 2769 |
In information theory, the Hamming distance between two strings of equal length |
2770 |
is the number of positions for which the corresponding symbols are different. |
|
2771 |
Put another way, it measures the minimum number of substitutions required to change |
|
11838 | 2772 |
one into the other, or the number of errors that transformed one string into the other." |
2773 |
||
22377 | 2774 |
|mySize| |
2775 |
||
2776 |
mySize := self size. |
|
2777 |
self assert:(aString size == mySize). |
|
2778 |
^ 1 to:mySize count:[:idx | (self at:idx) ~= (aString at:idx)] |
|
11838 | 2779 |
|
2780 |
" |
|
2781 |
'roses' hammingDistanceTo:'toned' |
|
22377 | 2782 |
'roses' hammingDistanceTo:'doses' |
2783 |
" |
|
2784 |
||
2785 |
"Modified (comment): / 24-11-2017 / 08:50:58 / cg" |
|
11838 | 2786 |
! |
2787 |
||
9192 | 2788 |
hash |
2789 |
"return an integer useful as a hash-key" |
|
2790 |
||
16898 | 2791 |
"/ whenever changing, also care for String>>hash. |
13879 | 2792 |
"/ immediately after any change, execute (maybe in a debugger): |
2793 |
"/ Set allSubInstancesDo:[:s | s rehash] |
|
16898 | 2794 |
^ self hash_fnv1a |
2795 |
||
2796 |
" |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2797 |
'a' hash |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2798 |
'a' asUnicode16String hash |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2799 |
'a' asUnicode32String hash |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2800 |
'aa' hash |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2801 |
'aa' asUnicode16String hash |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2802 |
'aa' asUnicode32String hash |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2803 |
'ab' hash |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2804 |
'ab' asUnicode16String hash |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2805 |
'ab' asUnicode32String hash |
13879 | 2806 |
'ab' hash |
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2807 |
'ab' asArray hash |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2808 |
" |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2809 |
|
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2810 |
" |
20874 | 2811 |
|syms ms| |
2812 |
||
2813 |
syms := Symbol allInstances. |
|
2814 |
Transcript show:'syms: '; showCR:syms size. |
|
2815 |
Transcript show:'sdbm hashes: '; showCR:(syms collect:[:s| s hash]) asSet size. |
|
2816 |
Transcript show:'dragonBook hashes: '; showCR:(syms collect:[:s| s hash_dragonBook]) asSet size. |
|
2817 |
||
2818 |
ms := Time millisecondsToRun:[ |
|
2819 |
10 timesRepeat:[ |
|
2820 |
syms do:[:each| each hash]. |
|
2821 |
]. |
|
2822 |
]. |
|
2823 |
Transcript show:'sdbm hash: '; showCR:ms. |
|
2824 |
||
2825 |
ms := Time millisecondsToRun:[ |
|
2826 |
10 timesRepeat:[ |
|
2827 |
syms do:[:each| each hash_dragonBook]. |
|
2828 |
]. |
|
2829 |
]. |
|
2830 |
Transcript show:'dragonBook: '; showCR:ms. |
|
2831 |
||
2832 |
syms := syms collect:[:each| each asUnicode16String]. |
|
2833 |
ms := Time millisecondsToRun:[ |
|
2834 |
10 timesRepeat:[ |
|
2835 |
syms do:[:each| each hash]. |
|
2836 |
]. |
|
2837 |
]. |
|
2838 |
Transcript show:'unicode sdbm hash: '; showCR:ms. |
|
2839 |
||
2840 |
ms := Time millisecondsToRun:[ |
|
2841 |
10 timesRepeat:[ |
|
2842 |
syms do:[:each| each hash_dragonBook]. |
|
2843 |
]. |
|
2844 |
]. |
|
2845 |
Transcript show:'unicode dragonBook:'; showCR:ms. |
|
14641 | 2846 |
" |
2847 |
||
13879 | 2848 |
"Modified: / 26-12-2011 / 14:09:07 / cg" |
2849 |
! |
|
2850 |
||
2851 |
hash_dragonBook |
|
2852 |
"return an integer useful as a hash-key" |
|
2853 |
||
10698
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2854 |
|h g| |
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2855 |
|
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2856 |
"/ |
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2857 |
"/ this is the dragon-book algorithm |
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2858 |
"/ |
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2859 |
h := 0. |
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2860 |
self reverseDo:[:char | |
10703
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
2861 |
"/ 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
|
2862 |
"/ h := (h bitShift:4) + char asciiValue. |
20874 | 2863 |
h := (h bitShift:4). |
2864 |
h := h + char codePoint. |
|
2865 |
h := h bitAnd:16rFFFFFFFF. |
|
2866 |
g := (h bitAnd: 16rF0000000). |
|
2867 |
g ~~ 0 ifTrue:[ |
|
2868 |
h := h bitXor:(g bitShift:-24). |
|
2869 |
h := h bitXor:g. |
|
2870 |
]. |
|
10698
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2871 |
]. |
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2872 |
"/ |
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2873 |
"/ multiply by large prime to spread values |
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2874 |
"/ This speeds up Set and Dictionary by a factor of 10!! |
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2875 |
"/ |
14137
1eb1b4a3f364
Inherit from UninterpretedBytes instead of ByteArray
Stefan Vogel <sv@exept.de>
parents:
14122
diff
changeset
|
2876 |
h := h times:31415821. |
10698
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2877 |
^ h |
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2878 |
|
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2879 |
" |
10703
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
2880 |
'a' hash |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
2881 |
'a' asUnicode16String hash |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
2882 |
'aa' hash |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
2883 |
'aa' asUnicode16String hash |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
2884 |
'ab' hash |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
2885 |
'ab' asUnicode16String hash |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
2886 |
'ab' hash |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
2887 |
'ab' asArray hash |
10698
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2888 |
" |
13879 | 2889 |
|
2890 |
"Created: / 26-12-2011 / 13:46:06 / cg" |
|
2891 |
! |
|
2892 |
||
16887 | 2893 |
hash_fnv1a |
2894 |
"return an integer useful as a hash-key. |
|
2895 |
This method uses the fnv-1a algorithm |
|
19743 | 2896 |
(which is actually a very good one). |
2897 |
Attention: stops when a 0-codepoint is encountered |
|
20874 | 2898 |
(for compatibility with the hash used by the VM) |
19743 | 2899 |
Also: on 64bit CPUs, only small 4-byte hashvalues are returned, |
20874 | 2900 |
(so hash values are independent from the architecture)" |
19743 | 2901 |
|
2902 |
|h byte| |
|
16887 | 2903 |
|
2904 |
h := 2166136261. |
|
2905 |
self do:[:eachChar | |
|
20874 | 2906 |
byte := eachChar codePoint. |
2907 |
byte == 0 ifTrue:[ |
|
2908 |
"/ stop |
|
2909 |
^ (h bitXor: (h >> 30)) bitAnd: 16r3FFFFFFF. |
|
2910 |
]. |
|
2911 |
h := h bitXor:byte. |
|
2912 |
h := (h * 16777619) bitAnd:16rFFFFFFFF. |
|
16887 | 2913 |
]. |
16898 | 2914 |
"/ make sure, it fits into a smallInt |
2915 |
h := (h bitXor: (h >> 30)) bitAnd: 16r3FFFFFFF. |
|
16887 | 2916 |
^ h |
2917 |
||
2918 |
" |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2919 |
'abc' hash_fnv1a |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2920 |
'abc' asUnicode16String hash_fnv1a |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2921 |
'abc' asUnicode32String hash_fnv1a |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2922 |
|
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2923 |
'foofooHelloWorld' hash_fnv1a |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2924 |
'foofooHelloWorld' asUnicode16String hash_fnv1a |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2925 |
'foofooHelloWorld' asUnicode32String hash_fnv1a |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2926 |
|
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2927 |
'blablaHelloWorld' hash_fnv1a |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2928 |
'blablaHelloWorld' asUnicode16String hash_fnv1a |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2929 |
'blablaHelloWorld' asUnicode32String hash_fnv1a |
16887 | 2930 |
" |
2931 |
! |
|
2932 |
||
23863 | 2933 |
hash_fnv1a_64 |
2934 |
"return a 64bit integer useful as a hash-key. |
|
2935 |
This method uses the fnv-1a algorithm |
|
2936 |
(which is actually a very good one)." |
|
2937 |
||
2938 |
|h byte| |
|
2939 |
||
2940 |
h := 14695981039346656037. |
|
2941 |
self do:[:eachChar | |
|
2942 |
byte := eachChar codePoint. |
|
2943 |
h := h bitXor:byte. |
|
2944 |
h := (h * 1099511628211) bitAnd:16rFFFFFFFFFFFFFFFF. |
|
2945 |
]. |
|
2946 |
"/ make sure, it fits into 64 bit |
|
2947 |
h := (h bitXor: (h >> 30)) bitAnd: 16r3FFFFFFFFFFFFFFF. |
|
2948 |
^ h |
|
2949 |
||
2950 |
" |
|
2951 |
'abc' hash_fnv1a_64 |
|
2952 |
'abc' asUnicode16String hash_fnv1a_64 |
|
2953 |
'abc' asUnicode32String hash_fnv1a_64 |
|
2954 |
||
2955 |
'foofooHelloWorld' hash_fnv1a_64 |
|
2956 |
'foofooHelloWorld' asUnicode16String hash_fnv1a_64 |
|
2957 |
'foofooHelloWorld' asUnicode32String hash_fnv1a_64 |
|
2958 |
||
2959 |
'blablaHelloWorld' hash_fnv1a_64 |
|
2960 |
'blablaHelloWorld' asUnicode16String hash_fnv1a_64 |
|
2961 |
'blablaHelloWorld' asUnicode32String hash_fnv1a_64 |
|
2962 |
" |
|
2963 |
||
2964 |
"Created: / 11-03-2019 / 11:47:15 / Claus Gittinger" |
|
2965 |
! |
|
2966 |
||
16887 | 2967 |
hash_java |
2968 |
"return an integer useful as a hash-key. |
|
2969 |
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
|
2970 |
the java virtual machine |
16887 | 2971 |
(which is actually not a very good one)." |
2972 |
||
2973 |
|h| |
|
2974 |
||
2975 |
h := 0. |
|
2976 |
self do:[:eachChar | |
|
20874 | 2977 |
h := (h * 31) + (eachChar codePoint). |
2978 |
h := h bitAnd:16rFFFFFFFF. |
|
16887 | 2979 |
]. |
2980 |
^ h |
|
2981 |
||
2982 |
" |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2983 |
'abc' hash_java |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2984 |
'foofooHelloWorld' hash_java |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2985 |
'blablaHelloWorld' hash_java |
16887 | 2986 |
" |
2987 |
! |
|
2988 |
||
16898 | 2989 |
hash_sdbm |
2990 |
"return an integer useful as a hash-key. |
|
2991 |
This method implements the sdbm algorithm." |
|
2992 |
||
2993 |
|h| |
|
2994 |
||
2995 |
"/ |
|
2996 |
"/ this is the sdbm algorithm |
|
2997 |
"/ |
|
2998 |
h := 0. |
|
2999 |
self do:[:char | |
|
20874 | 3000 |
h := (65599 times:h) plus:char codePoint. |
16898 | 3001 |
]. |
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
3002 |
^ h |
16898 | 3003 |
|
3004 |
" |
|
3005 |
'a' hash |
|
3006 |
'a' asUnicode16String hash |
|
3007 |
'aa' hash |
|
3008 |
'aa' asUnicode16String hash |
|
3009 |
'ab' hash |
|
3010 |
'ab' asUnicode16String hash |
|
3011 |
'ab' hash |
|
3012 |
'ab' asArray hash |
|
3013 |
" |
|
3014 |
||
3015 |
" |
|
20874 | 3016 |
|syms ms| |
3017 |
||
3018 |
syms := Symbol allInstances. |
|
3019 |
Transcript show:'syms: '; showCR:syms size. |
|
3020 |
Transcript show:'sdbm hashes: '; showCR:(syms collect:[:s| s hash]) asSet size. |
|
3021 |
Transcript show:'dragonBook hashes: '; showCR:(syms collect:[:s| s hash_dragonBook]) asSet size. |
|
3022 |
||
3023 |
ms := Time millisecondsToRun:[ |
|
3024 |
10 timesRepeat:[ |
|
3025 |
syms do:[:each| each hash]. |
|
3026 |
]. |
|
3027 |
]. |
|
3028 |
Transcript show:'sdbm hash: '; showCR:ms. |
|
3029 |
||
3030 |
ms := Time millisecondsToRun:[ |
|
3031 |
10 timesRepeat:[ |
|
3032 |
syms do:[:each| each hash_dragonBook]. |
|
3033 |
]. |
|
3034 |
]. |
|
3035 |
Transcript show:'dragonBook: '; showCR:ms. |
|
3036 |
||
3037 |
syms := syms collect:[:each| each asUnicode16String]. |
|
3038 |
ms := Time millisecondsToRun:[ |
|
3039 |
10 timesRepeat:[ |
|
3040 |
syms do:[:each| each hash]. |
|
3041 |
]. |
|
3042 |
]. |
|
3043 |
Transcript show:'unicode sdbm hash: '; showCR:ms. |
|
3044 |
||
3045 |
ms := Time millisecondsToRun:[ |
|
3046 |
10 timesRepeat:[ |
|
3047 |
syms do:[:each| each hash_dragonBook]. |
|
3048 |
]. |
|
3049 |
]. |
|
3050 |
Transcript show:'unicode dragonBook:'; showCR:ms. |
|
16898 | 3051 |
" |
3052 |
||
3053 |
"Modified: / 26-12-2011 / 14:09:07 / cg" |
|
3054 |
! |
|
3055 |
||
11861 | 3056 |
levenshteinTo:aString |
3057 |
"return the levenshtein distance to the argument, aString; |
|
3058 |
this value corresponds to the number of replacements that have to be |
|
3059 |
made to get aString from the receiver. |
|
3060 |
See IEEE transactions on Computers 1976 Pg 172 ff." |
|
3061 |
||
3062 |
" |
|
21760 | 3063 |
in the following, we assume that omiting a character |
11861 | 3064 |
is less of an error than inserting an extra character. |
3065 |
Therefore the different insertion (i) and deletion (d) values. |
|
20874 | 3066 |
s: substitution weight (4) |
3067 |
k: keyboard weight (k) (typing a nearby key) - or nil (then use s) |
|
3068 |
c: case weight (4) - or nil (then use s) |
|
3069 |
e: exchange weight (8) - or nil (then use s*2) |
|
3070 |
i: insertion of extra character weight (2) |
|
3071 |
d: delete of a character weight (6) |
|
22157 | 3072 |
|
3073 |
Notice that the standard levenshtein uses the same weight for insertion and deletion, |
|
3074 |
and is computed by: |
|
3075 |
'flaw' levenshteinTo:'lawn' s:2 k:nil c:nil i:1 d:1 |
|
3076 |
||
11861 | 3077 |
" |
3078 |
||
3079 |
^ StringUtilities |
|
20874 | 3080 |
levenshteinDistanceFrom:self |
3081 |
to:aString |
|
3082 |
s:4 k:4 c:4 e:nil i:2 d:6 |
|
12177 | 3083 |
|
3084 |
" |
|
3085 |
'computer' levenshteinTo:'computer' |
|
11861 | 3086 |
'cOmputer' levenshteinTo:'computer' |
12177 | 3087 |
'cOmpuTer' levenshteinTo:'computer' |
11861 | 3088 |
'cimputer' levenshteinTo:'computer' |
3089 |
'cumputer' levenshteinTo:'computer' |
|
3090 |
||
3091 |
'cmputer' levenshteinTo:'computer' |
|
3092 |
'coomputer' levenshteinTo:'computer' |
|
3093 |
||
3094 |
'ocmprt' levenshteinTo:'computer' |
|
3095 |
'computer' levenshteinTo:'computer' |
|
3096 |
'ocmputer' levenshteinTo:'computer' |
|
3097 |
'cmputer' levenshteinTo:'computer' |
|
3098 |
'computer' levenshteinTo:'cmputer' |
|
3099 |
'Computer' levenshteinTo:'computer' |
|
3100 |
||
12177 | 3101 |
'compiter' levenshteinTo:'computer' |
3102 |
'compoter' levenshteinTo:'computer' |
|
3103 |
||
3104 |
'comptuer' levenshteinTo:'computer' |
|
11861 | 3105 |
" |
21760 | 3106 |
|
3107 |
"Modified (comment): / 17-05-2017 / 16:15:41 / mawalch" |
|
22157 | 3108 |
"Modified (comment): / 02-08-2017 / 17:13:04 / cg" |
11861 | 3109 |
! |
3110 |
||
3111 |
levenshteinTo:aString s:substWeight k:kbdTypoWeight c:caseWeight i:insrtWeight d:deleteWeight |
|
3112 |
"parametrized levenshtein. |
|
3113 |
return the levenshtein distance to the argument, aString; |
|
3114 |
this value corrensponds to the number of replacements that have to be |
|
3115 |
made to get aString from the receiver. |
|
3116 |
The arguments are the costs for |
|
20874 | 3117 |
s:substitution, |
3118 |
k:keyboard type (substitution), |
|
3119 |
c:case-change, |
|
3120 |
i:insertion |
|
3121 |
d:deletion |
|
11861 | 3122 |
of a character. |
3123 |
See IEEE transactions on Computers 1976 Pg 172 ff" |
|
3124 |
||
3125 |
^ StringUtilities |
|
20874 | 3126 |
levenshteinDistanceFrom:self |
3127 |
to:aString |
|
3128 |
s:substWeight k:kbdTypoWeight c:caseWeight e:nil i:insrtWeight d:deleteWeight |
|
11861 | 3129 |
! |
3130 |
||
9190 | 3131 |
sameAs:aString |
3132 |
"Compare the receiver with the argument like =, but ignore case differences. |
|
3133 |
Return true or false." |
|
3134 |
||
3135 |
|mySize "{ Class: SmallInteger }" |
|
3136 |
otherSize c1 c2| |
|
3137 |
||
9192 | 3138 |
self == aString ifTrue:[^ true]. |
3139 |
||
3140 |
mySize := self size. |
|
3141 |
otherSize := aString string size. |
|
18884 | 3142 |
mySize ~~ otherSize ifTrue:[^ false]. |
9192 | 3143 |
|
3144 |
1 to:mySize do:[:index | |
|
20874 | 3145 |
c1 := self at:index. |
3146 |
c2 := aString at:index. |
|
3147 |
c1 ~~ c2 ifTrue:[ |
|
3148 |
(c1 sameAs:c2) ifFalse:[^ false]. |
|
3149 |
] |
|
9192 | 3150 |
]. |
3151 |
^ true |
|
3152 |
||
608 | 3153 |
" |
8874 | 3154 |
'foo' sameAs: 'Foo' |
3155 |
'foo' sameAs: 'bar' |
|
3156 |
'foo' sameAs: 'foo' |
|
9192 | 3157 |
" |
3158 |
||
3159 |
"Modified: 22.4.1996 / 15:56:17 / cg" |
|
3160 |
! |
|
608 | 3161 |
|
15734 | 3162 |
sameAs:aString caseSensitive:caseSensitive |
3163 |
"Compare the receiver with the argument. |
|
3164 |
If caseSensitive is false, this is the same as #sameAs:, |
|
3165 |
if false, this is the same as #=." |
|
3166 |
||
3167 |
caseSensitive ifFalse:[ |
|
20874 | 3168 |
^ self sameAs:aString |
15734 | 3169 |
]. |
3170 |
^ self = aString |
|
3171 |
||
3172 |
" |
|
3173 |
'foo' sameAs:'Foo' caseSensitive:false |
|
3174 |
'foo' sameAs:'foo' caseSensitive:true |
|
3175 |
" |
|
3176 |
! |
|
3177 |
||
5242 | 3178 |
sameAs:aString ignoreCase:ignoreCase |
15734 | 3179 |
<resource: #obsolete> |
5242 | 3180 |
"Compare the receiver with the argument. |
3181 |
If ignoreCase is true, this is the same as #sameAs:, |
|
3182 |
if false, this is the same as #=." |
|
3183 |
||
3184 |
ignoreCase ifTrue:[ |
|
20874 | 3185 |
^ self sameAs:aString |
5242 | 3186 |
]. |
3187 |
^ self = aString |
|
3188 |
||
3189 |
" |
|
8874 | 3190 |
'foo' sameAs:'Foo' ignoreCase:false |
3191 |
'foo' sameAs:'foo' ignoreCase:true |
|
5242 | 3192 |
" |
3193 |
||
3194 |
! |
|
3195 |
||
608 | 3196 |
sameCharacters:aString |
9192 | 3197 |
"count & return the number of characters which are the same |
3198 |
(ignoring case and emphasis) in the receiver and the argument, aString." |
|
3199 |
||
3200 |
|n "{ Class: SmallInteger }" |
|
3201 |
c1 c2 cnt| |
|
3202 |
||
3203 |
n := self size. |
|
3204 |
n := n min:(aString string size). |
|
3205 |
||
3206 |
cnt := 0. |
|
3207 |
1 to:n do:[:index | |
|
20874 | 3208 |
c1 := self at:index. |
3209 |
c2 := aString at:index. |
|
3210 |
((c1 == c2) |
|
3211 |
or:[c1 asLowercase = c2 asLowercase]) ifTrue:[ |
|
3212 |
cnt := cnt + 1 |
|
3213 |
] |
|
9192 | 3214 |
]. |
3215 |
^ cnt |
|
3216 |
||
3217 |
" |
|
3218 |
'foobarbaz' sameCharacters: 'foo' |
|
8874 | 3219 |
'foobarbaz' sameCharacters: 'Foo' |
3220 |
'foobarbaz' sameCharacters: 'baz' |
|
608 | 3221 |
" |
5510
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
3222 |
! |
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
3223 |
|
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
3224 |
sameEmphasisAs:aStringOrText |
17215 | 3225 |
"compare the receiver's and the argument's emphasis" |
5510
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
3226 |
|
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
3227 |
^ self emphasis = aStringOrText emphasis |
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
3228 |
|
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
3229 |
" |
8874 | 3230 |
'hello' asText sameEmphasisAs: 'hello' |
3231 |
'hello' asText sameEmphasisAs: 'hello' asText |
|
3232 |
'hello' asText allBold sameEmphasisAs: 'hello' |
|
3233 |
'hello' asText allBold sameEmphasisAs: 'fooba' asText allBold |
|
9192 | 3234 |
'hello' asText allBold sameEmphasisAs: 'fooba' asText allItalic |
3235 |
" |
|
3236 |
! |
|
3237 |
||
9191 | 3238 |
sameStringAndEmphasisAs:aStringOrText |
5739
4fd3be7ccc75
oops some relational ops did accept a non-string arg
Claus Gittinger <cg@exept.de>
parents:
5732
diff
changeset
|
3239 |
"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
|
3240 |
|
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
3241 |
aStringOrText isString ifFalse:[^ false]. |
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
3242 |
(self string = aStringOrText string) ifFalse:[^ false]. |
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
3243 |
self hasChangeOfEmphasis = aStringOrText hasChangeOfEmphasis ifFalse:[^ false]. |
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
3244 |
^ self emphasis = aStringOrText emphasis |
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
3245 |
|
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
3246 |
" |
8874 | 3247 |
'hello' asText sameEmphasisAs: 'hello' |
3248 |
'hello' asText sameEmphasisAs: 'hello' asText |
|
3249 |
'hello' asText allBold sameEmphasisAs: 'hello' |
|
3250 |
'hello' asText allBold sameEmphasisAs: 'fooba' asText allBold |
|
3251 |
'hello' asText allBold sameEmphasisAs: 'fooba' asText allItalic |
|
3252 |
||
3253 |
'hello' sameEmphasisAs: 'hello' asText |
|
3254 |
'hello' sameEmphasisAs: 'hello' asText allBold |
|
3255 |
'hello' sameEmphasisAs: 'fooba' |
|
3256 |
'hello' sameEmphasisAs: 'fooba' asText |
|
3257 |
'hello' sameEmphasisAs: 'fooba' asText allBold |
|
3258 |
'hello' sameEmphasisAs: 'fooba' asText allItalic |
|
3259 |
||
3260 |
'hello' asText sameStringAndEmphasisAs: 'hello' |
|
3261 |
'hello' asText sameStringAndEmphasisAs: 'hello' asText |
|
3262 |
'hello' asText allBold sameStringAndEmphasisAs: 'hello' |
|
3263 |
'hello' asText allBold sameStringAndEmphasisAs: 'fooba' asText allBold |
|
3264 |
'hello' asText allBold sameStringAndEmphasisAs: 'fooba' asText allItalic |
|
3265 |
||
3266 |
'hello' sameStringAndEmphasisAs: 'hello' asText |
|
3267 |
'hello' sameStringAndEmphasisAs: 'hello' asText allBold |
|
3268 |
'hello' sameStringAndEmphasisAs: 'fooba' |
|
3269 |
'hello' sameStringAndEmphasisAs: 'fooba' asText |
|
3270 |
'hello' sameStringAndEmphasisAs: 'fooba' asText allBold |
|
3271 |
'hello' sameStringAndEmphasisAs: 'fooba' asText allItalic |
|
5510
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
3272 |
" |
11861 | 3273 |
! |
3274 |
||
3275 |
spellAgainst: aString |
|
3276 |
"return an integer between 0 and 100 indicating how similar |
|
3277 |
the argument is to the receiver. No case conversion is done. |
|
3278 |
This algorithm is much simpler (but also less exact) than the |
|
3279 |
levenshtein distance. Experiment which is better for your |
|
3280 |
application." |
|
3281 |
||
3282 |
| i1 "{ Class: SmallInteger }" |
|
3283 |
i2 "{ Class: SmallInteger }" |
|
3284 |
next1 "{ Class: SmallInteger }" |
|
3285 |
next2 "{ Class: SmallInteger }" |
|
3286 |
size1 "{ Class: SmallInteger }" |
|
3287 |
size2 "{ Class: SmallInteger }" |
|
3288 |
score "{ Class: SmallInteger }" |
|
3289 |
maxLen "{ Class: SmallInteger }" | |
|
3290 |
||
3291 |
size1 := self size. |
|
3292 |
size2 := aString size. |
|
3293 |
maxLen := size1 max:size2. |
|
3294 |
score := 0. |
|
3295 |
i1 := i2 := 1. |
|
3296 |
[i1 <= size1 and: [i2 <= size2]] whileTrue:[ |
|
20874 | 3297 |
next1 := i1 + 1. |
3298 |
next2 := i2 + 1. |
|
3299 |
(self at:i1) == (aString at:i2) ifTrue: [ |
|
3300 |
score := score+1. |
|
3301 |
i1 := next1. |
|
3302 |
i2 := next2 |
|
3303 |
] ifFalse: [ |
|
3304 |
(i2 < size2 and: [(self at:i1) == (aString at:next2)]) ifTrue: [ |
|
3305 |
i2 := next2 |
|
3306 |
] ifFalse: [ |
|
3307 |
(i1 < size1 and: [(self at:next1) == (aString at:i2)]) ifTrue: [ |
|
3308 |
i1 := next1 |
|
3309 |
] ifFalse: [ |
|
3310 |
i1 := next1. |
|
3311 |
i2 := next2 |
|
3312 |
] |
|
3313 |
] |
|
3314 |
] |
|
11861 | 3315 |
]. |
3316 |
||
3317 |
score == maxLen ifTrue: [^ 100]. |
|
3318 |
^ 100 * score // maxLen |
|
3319 |
||
3320 |
" |
|
3321 |
'Smalltalk' spellAgainst: 'Smalltlak' |
|
3322 |
'Smalltalk' spellAgainst: 'smalltlak' |
|
3323 |
'Smalltalk' spellAgainst: 'smalltalk' |
|
3324 |
'Smalltalk' spellAgainst: 'smalltlk' |
|
3325 |
'Smalltalk' spellAgainst: 'Smalltolk' |
|
3326 |
" |
|
3327 |
! |
|
3328 |
||
16553 | 3329 |
startsWith:aStringOrCharacter |
3330 |
"return true, if the receiver starts with something, aStringOrCharacter. |
|
17371 | 3331 |
If the argument is empty, true is returned. |
3332 |
Notice, that this is similar to, but slightly different from VW's and Squeak's beginsWith:, |
|
3333 |
which are both inconsistent w.r.t. an empty argument." |
|
11861 | 3334 |
|
3335 |
|s| |
|
3336 |
||
16553 | 3337 |
aStringOrCharacter isCharacter ifTrue:[ |
23548 | 3338 |
^ (self size ~~ 0) and:[(self at:1) = aStringOrCharacter] |
16553 | 3339 |
]. |
11861 | 3340 |
(s := self string) ~~ self ifTrue:[ |
20874 | 3341 |
^ s startsWith:aStringOrCharacter |
11861 | 3342 |
]. |
16553 | 3343 |
^ super startsWith:aStringOrCharacter |
11861 | 3344 |
|
3345 |
" |
|
17364 | 3346 |
'abcde' startsWith:#($a $b $c) |
3347 |
'abcde' startsWith:'abc' |
|
3348 |
'abcd' startsWith:'abcde' |
|
3349 |
'abcde' startsWith:'abd' |
|
11861 | 3350 |
'hello world' startsWith:'hello' |
3351 |
'hello world' asText allBold startsWith:'hello' |
|
3352 |
'hello world' asText allBold startsWith:'' |
|
3353 |
" |
|
3354 |
||
23154 | 3355 |
"Created: / 12-05-1996 / 15:46:40 / cg" |
3356 |
"Modified: / 29-06-2018 / 11:27:08 / Claus Gittinger" |
|
23548 | 3357 |
"Modified (format): / 26-11-2018 / 14:15:31 / Stefan Vogel" |
23154 | 3358 |
! |
3359 |
||
3360 |
startsWith:aStringOrCharacter caseSensitive:caseSensitive |
|
3361 |
"return true, if the receiver starts with something, aStringOrCharacter. |
|
3362 |
If the argument is empty, true is returned. |
|
3363 |
Notice, that this is similar to, but slightly different from VW's and Squeak's beginsWith:, |
|
3364 |
which are both inconsistent w.r.t. an empty argument." |
|
3365 |
||
3366 |
|s| |
|
3367 |
||
23548 | 3368 |
caseSensitive ifTrue:[ |
3369 |
^ self startsWith:aStringOrCharacter |
|
3370 |
]. |
|
23154 | 3371 |
aStringOrCharacter isCharacter ifTrue:[ |
23548 | 3372 |
self size == 0 ifTrue:[ |
3373 |
^ false. |
|
3374 |
]. |
|
3375 |
s := self at:1. |
|
3376 |
^ s == aStringOrCharacter or:[s asLowercase = aStringOrCharacter asLowercase] |
|
23154 | 3377 |
]. |
3378 |
(s := self string) ~~ self ifTrue:[ |
|
3379 |
^ s startsWith:aStringOrCharacter caseSensitive:caseSensitive |
|
3380 |
]. |
|
3381 |
^ self startsWith:aStringOrCharacter using:[:a :b | a asLowercase = b asLowercase]. |
|
3382 |
||
3383 |
" |
|
3384 |
'aBCde' startsWith:'abc' caseSensitive:true |
|
3385 |
'aBCde' startsWith:'abc' caseSensitive:false |
|
3386 |
" |
|
3387 |
||
3388 |
"Created: / 29-06-2018 / 11:27:04 / Claus Gittinger" |
|
23548 | 3389 |
"Modified: / 26-11-2018 / 14:18:43 / Stefan Vogel" |
24300 | 3390 |
! |
3391 |
||
3392 |
startsWithDigit |
|
3393 |
"Answer whether the receiver's first character represents a digit. 3/11/96 sw" |
|
3394 |
||
3395 |
|len| |
|
3396 |
||
3397 |
^ (len := self size) ~~ 0 and:[(self at:1) isDigit] |
|
3398 |
||
3399 |
" |
|
3400 |
'hello' startsWithDigit |
|
3401 |
'12hello' startsWithDigit |
|
3402 |
'hello12' startsWithDigit |
|
3403 |
" |
|
3404 |
" |
|
3405 |
'hello' endsWithDigit |
|
3406 |
'12hello' endsWithDigit |
|
3407 |
'hello12' endsWithDigit |
|
3408 |
" |
|
3409 |
||
3410 |
"Created: / 08-06-2019 / 17:22:13 / Claus Gittinger" |
|
9192 | 3411 |
! ! |
3412 |
||
3413 |
!CharacterArray methodsFor:'converting'! |
|
3414 |
||
11881 | 3415 |
asAsciiZ |
3416 |
"if the receiver does not end with a 0-valued character, return a copy of it, |
|
3417 |
with an additional 0-character. Otherwise return the receiver. This is sometimes |
|
3418 |
needed when a string has to be passed to C, which needs 0-terminated strings. |
|
3419 |
Notice, that all singleByte strings are already 0-terminated in ST/X, whereas wide |
|
3420 |
strings are not." |
|
3421 |
||
12658 | 3422 |
(self notEmpty and:[self last codePoint == 0]) ifTrue:[^ self ]. |
11881 | 3423 |
^ self copyWith:(Character value:0). |
3424 |
||
3425 |
" |
|
3426 |
'abc' asAsciiZ |
|
3427 |
'abc' asWideString asAsciiZ |
|
3428 |
" |
|
3429 |
! |
|
3430 |
||
13524 | 3431 |
asByteArray |
15936 | 3432 |
"depending on the size of the characters in the receiver, |
3433 |
return a byteArray containing single-, double- or even 4-bytewise values. |
|
3434 |
The size of the returned byteArray will be the strings size multiplied by the |
|
3435 |
size required for the largest character. |
|
19233 | 3436 |
Attention: The bytes are in native byte order. |
21745 | 3437 |
Caveat: better use utf8Encoded, to get reproducible results" |
15936 | 3438 |
|
14555 | 3439 |
|bytes sz bytesPerCharacter idx str| |
3440 |
||
3441 |
str := self string. |
|
3442 |
str ~~ self ifTrue:[ |
|
20874 | 3443 |
"/ for text and other wrappers |
3444 |
^ str asByteArray |
|
13611
ba8c7372218a
variable renamed in: #inspector2TabBytes
Claus Gittinger <cg@exept.de>
parents:
13524
diff
changeset
|
3445 |
]. |
ba8c7372218a
variable renamed in: #inspector2TabBytes
Claus Gittinger <cg@exept.de>
parents:
13524
diff
changeset
|
3446 |
|
ba8c7372218a
variable renamed in: #inspector2TabBytes
Claus Gittinger <cg@exept.de>
parents:
13524
diff
changeset
|
3447 |
"/ for real strings, a fallback |
13524 | 3448 |
sz := self size. |
19757 | 3449 |
bytesPerCharacter := self bytesPerCharacter. |
13524 | 3450 |
bytes := ByteArray new:(sz * bytesPerCharacter). |
3451 |
idx := 1. |
|
3452 |
self do:[:char | |
|
20874 | 3453 |
|code| |
3454 |
||
3455 |
code := char codePoint. |
|
3456 |
bytesPerCharacter == 2 ifTrue:[ |
|
3457 |
bytes unsignedInt16At:idx put:code |
|
3458 |
] ifFalse:[ |
|
3459 |
bytesPerCharacter == 4 ifTrue:[ |
|
3460 |
bytes unsignedInt32At:idx put:code |
|
3461 |
] ifFalse:[ |
|
3462 |
bytes at:idx put:code |
|
3463 |
]. |
|
3464 |
]. |
|
3465 |
idx := idx + bytesPerCharacter. |
|
13524 | 3466 |
]. |
3467 |
^ bytes |
|
3468 |
||
3469 |
"Created: / 27-07-2011 / 00:56:17 / cg" |
|
21745 | 3470 |
"Modified (comment): / 11-05-2017 / 09:21:57 / mawalch" |
13524 | 3471 |
! |
3472 |
||
19233 | 3473 |
asByteArrayMSB:msb |
3474 |
"depending on the size of the characters in the receiver, |
|
3475 |
return a byteArray containing single-, double- or even 4-bytewise values. |
|
3476 |
The size of the returned byteArray will be the strings size multiplied by the |
|
3477 |
size required for the largest character. |
|
21745 | 3478 |
Caveat: better use utf8Encoded, to get reproducible results" |
19233 | 3479 |
|
3480 |
|ba| |
|
3481 |
||
3482 |
ba := self asByteArray. "/ native order |
|
3483 |
UninterpretedBytes isBigEndian ~~ msb ifTrue:[ |
|
20874 | 3484 |
ba swapBytes |
19233 | 3485 |
]. |
3486 |
^ ba |
|
21745 | 3487 |
|
3488 |
"Modified (comment): / 11-05-2017 / 09:22:01 / mawalch" |
|
19233 | 3489 |
! |
3490 |
||
20480 | 3491 |
asCanonicalizedFilename |
3492 |
"return a Filename with pathname taken from the receiver. |
|
3493 |
The filename is canonicalized, meaning that it cares for trailing directory separators, |
|
3494 |
'.' components etc." |
|
3495 |
||
3496 |
^ self asFilename asCanonicalizedFilename |
|
3497 |
||
3498 |
"on windows: |
|
3499 |
'c:\foo\bar' asFilename |
|
3500 |
'c:\foo\bar\' asFilename |
|
3501 |
'c:\foo\bar\..\baz' asFilename |
|
3502 |
'c:\foo\bar\..\baz\.' asFilename |
|
3503 |
'c:\foo\bar' asCanonicalizedFilename |
|
3504 |
'c:\foo\bar\' asCanonicalizedFilename |
|
3505 |
'c:\foo\bar\..\baz' asCanonicalizedFilename |
|
3506 |
'c:\foo\bar\..\baz\.' asCanonicalizedFilename |
|
3507 |
||
3508 |
on unix: |
|
3509 |
'/foo/bar' asFilename |
|
3510 |
'/foo/bar/' asFilename |
|
3511 |
'/foo/bar/../baz' asFilename |
|
3512 |
'/foo/bar/../baz/.' asFilename |
|
3513 |
'/foo/bar' asCanonicalizedFilename |
|
3514 |
'/foo/bar/' asCanonicalizedFilename |
|
3515 |
'/foo/bar/../baz' asCanonicalizedFilename |
|
3516 |
'/foo/bar/../baz/.' asCanonicalizedFilename |
|
3517 |
" |
|
3518 |
! |
|
3519 |
||
608 | 3520 |
asCollectionOfLines |
23187 | 3521 |
"return a collection containing the lines (separated by cr) of the receiver. |
3522 |
If multiple cr's occur in a row, the result will contain empty strings. |
|
14456
d7c3e67647f1
comment/format in: #asCollectionOfLines
Claus Gittinger <cg@exept.de>
parents:
14455
diff
changeset
|
3523 |
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
|
3524 |
See also #asCollectionOfLinesWithReturn |
d7c3e67647f1
comment/format in: #asCollectionOfLines
Claus Gittinger <cg@exept.de>
parents:
14455
diff
changeset
|
3525 |
(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
|
3526 |
this method and we did not want to risk any incompatibilities)" |
14455
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3527 |
|
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3528 |
^ self asCollectionOfSubstringsSeparatedBy:Character cr. |
608 | 3529 |
|
3530 |
" |
|
3531 |
'1 one\2 two\3 three\4 four\5 five' withCRs asCollectionOfLines |
|
8874 | 3532 |
'1 one\\\\2 two\3 three' withCRs asCollectionOfLines |
20182 | 3533 |
|
3534 |
('foo \r\nbar\nbaz\t\r\n\r\nbla' printf:#()) |
|
3535 |
asCollectionOfLines collect:[:l | (l endsWith:Character return) ifTrue:[l copyButLast:1] ifFalse:l] |
|
608 | 3536 |
" |
23187 | 3537 |
|
3538 |
"Modified (comment): / 03-07-2018 / 10:59:36 / Claus Gittinger" |
|
608 | 3539 |
! |
3540 |
||
24535 | 3541 |
asCollectionOfLinesDo:aBlock |
3542 |
"evaluate aBlock for each line (separated by cr) of the receiver. |
|
3543 |
Returns the number of lines (i.e. the number of invocations of aBlock). |
|
3544 |
This is similar to 'asCollectionOfLines do:...' or 'asStringCollection do:...' |
|
3545 |
but avoids the creation of a temporary collection." |
|
3546 |
||
3547 |
|count "{ Class:SmallInteger }" |
|
3548 |
start "{ Class:SmallInteger }" |
|
3549 |
stop "{ Class:SmallInteger }" |
|
3550 |
mySize "{ Class:SmallInteger }"| |
|
3551 |
||
3552 |
count := 0. |
|
3553 |
start := 1. |
|
3554 |
mySize := self size. |
|
3555 |
[start <= mySize] whileTrue:[ |
|
3556 |
stop := self indexOf:Character cr startingAt:start. |
|
3557 |
stop == 0 ifTrue:[ |
|
3558 |
aBlock value:(self copyFrom:start to:mySize). |
|
3559 |
^ count + 1 |
|
3560 |
]. |
|
3561 |
aBlock value:(self copyFrom:start to:(stop - 1)). |
|
3562 |
start := stop+1. |
|
3563 |
count := count + 1 |
|
3564 |
]. |
|
3565 |
^ count |
|
3566 |
||
3567 |
" |
|
3568 |
||
3569 |
c'hello\nworld\nisnt\nthis\n\nnice' asCollectionOfLinesDo:[:l | Transcript showCR:l] |
|
3570 |
c'hello\nworld\nisnt\nthis\n\nnice' asStringCollection do:[:l | Transcript showCR:l] |
|
3571 |
||
3572 |
c'hello\nworld\nisnt\nthis\n\nnice\n' asCollectionOfLinesDo:[:l | Transcript showCR:l] |
|
3573 |
c'hello\nworld\nisnt\nthis\n\nnice\n' asStringCollection do:[:l | Transcript showCR:l] |
|
3574 |
||
3575 |
c'hello\nworld\nisnt\nthis\n\nnice' asCollectionOfWordsDo:#transcribeCR |
|
3576 |
' hello\n world\n isnt\n this\n nice\n ' asCollectionOfLinesDo:#transcribeCR |
|
3577 |
'hello' asCollectionOfLinesDo:#transcribeCR |
|
3578 |
'' asCollectionOfLinesDo:#transcribeCR |
|
3579 |
' ' asCollectionOfLinesDo:#transcribeCR |
|
3580 |
" |
|
3581 |
||
3582 |
"Modified (comment): / 03-02-2019 / 13:08:15 / Claus Gittinger" |
|
3583 |
! |
|
3584 |
||
14455
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3585 |
asCollectionOfLinesWithReturn |
23187 | 3586 |
"return a collection containing the lines (separated by cr) of the receiver. |
3587 |
If multiple cr's occur in a row, the result will contain empty strings." |
|
14455
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3588 |
|
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3589 |
|lines| |
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3590 |
|
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3591 |
lines := self asCollectionOfSubstringsSeparatedBy:Character cr. |
18194 | 3592 |
(lines notEmpty and:[lines last isEmpty]) ifTrue:[ |
20874 | 3593 |
^ lines copyButLast:1 |
14455
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3594 |
]. |
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3595 |
^ lines |
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3596 |
|
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3597 |
" |
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3598 |
'1\2\3' withCRs asCollectionOfLines |
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3599 |
'1\2\3\' withCRs asCollectionOfLines |
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3600 |
'1\2\3' withCRs asCollectionOfLinesWithReturn |
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3601 |
'1\2\3\' withCRs asCollectionOfLinesWithReturn |
18194 | 3602 |
'' withCRs asCollectionOfLinesWithReturn |
14455
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3603 |
" |
23187 | 3604 |
|
3605 |
"Modified (comment): / 03-07-2018 / 10:59:47 / Claus Gittinger" |
|
14455
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3606 |
! |
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3607 |
|
608 | 3608 |
asCollectionOfSubstringsSeparatedBy:aCharacter |
23187 | 3609 |
"return a collection containing substrings (separated by aCharacter) of the receiver. |
15740 | 3610 |
If aCharacter occurs multiple times in a row, the result will contain empty strings. |
23717 | 3611 |
If the receiver starts with aCharacter, an empty string with be the first result element. |
3612 |
If the receiver ends with aCharacter, NO empty string with be the last result element." |
|
608 | 3613 |
|
9192 | 3614 |
^ self asCollectionOfSubCollectionsSeparatedBy:aCharacter |
3615 |
||
3616 |
" |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
3617 |
'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
|
3618 |
'1 one:2 two:3 three:4 four:5 five:' asCollectionOfSubstringsSeparatedBy:$: |
15740 | 3619 |
'1 one 2 two 3 three 4 four 5 five' asCollectionOfSubstringsSeparatedBy:Character space |
9192 | 3620 |
" |
23187 | 3621 |
|
23717 | 3622 |
"Modified (comment): / 11-02-2019 / 23:54:20 / Claus Gittinger" |
9192 | 3623 |
! |
608 | 3624 |
|
11828 | 3625 |
asCollectionOfSubstringsSeparatedBy:aCharacter exceptIn:ch |
23187 | 3626 |
"return a collection containing the substrings (separated by aCharacter) of the receiver. |
3627 |
If aCharacter occurs multiple times in a row, the result will contain empty strings. |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
3628 |
The separation is not done, inside a matching pair of ch-substrings. |
15740 | 3629 |
Can be used to tokenize csv-like strings, which may or may not be enclosed in quotes." |
11828 | 3630 |
|
3631 |
|lines myClass except i c |
|
3632 |
startIndex "{ Class:SmallInteger }" |
|
3633 |
stopIndex "{ Class:SmallInteger }" | |
|
3634 |
||
11905
cbc2191cfe2d
return stringCollections from asCollectionOfXXX
Claus Gittinger <cg@exept.de>
parents:
11904
diff
changeset
|
3635 |
lines := StringCollection new. |
11828 | 3636 |
myClass := self species. |
3637 |
||
3638 |
startIndex := 1. |
|
3639 |
except := false. |
|
3640 |
[ |
|
20874 | 3641 |
i := startIndex-1. |
3642 |
[ |
|
3643 |
i := i+1. |
|
3644 |
c := self at:i. |
|
3645 |
c = ch ifTrue:[ except := except not. ]. |
|
3646 |
i < self size and:[except or:[c ~= aCharacter]] |
|
3647 |
] whileTrue. |
|
3648 |
||
3649 |
c = aCharacter ifTrue:[ |
|
3650 |
stopIndex := i -1. |
|
3651 |
] ifFalse: [ |
|
3652 |
stopIndex := i. |
|
3653 |
]. |
|
3654 |
(stopIndex < startIndex) ifTrue: [ |
|
3655 |
lines add:(myClass new:0) |
|
3656 |
] ifFalse: [ |
|
3657 |
lines add:(self copyFrom:startIndex to:stopIndex) |
|
3658 |
]. |
|
3659 |
startIndex := stopIndex + 2. |
|
3660 |
startIndex <= self size |
|
11828 | 3661 |
] whileTrue. |
3662 |
^ lines |
|
3663 |
||
3664 |
" |
|
12177 | 3665 |
'asd''f;d''dd;s' asCollectionOfSubstringsSeparatedBy:$; exceptIn:$' |
3666 |
" |
|
3667 |
"/ 'asd "hello bla" foo "bla bla" bar' asCollectionOfSubstringsSeparatedBy:$ exceptIn:$" |
|
23187 | 3668 |
|
3669 |
"Modified (comment): / 03-07-2018 / 11:00:02 / Claus Gittinger" |
|
11828 | 3670 |
! |
3671 |
||
13344
ec6860c49990
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13343
diff
changeset
|
3672 |
asCollectionOfSubstringsSeparatedBy:aFieldSeparatorString textSeparator:aTextSeparatorOrNil |
13343
3f058ad0d2f1
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13332
diff
changeset
|
3673 |
"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
|
3674 |
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
|
3675 |
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
|
3676 |
|
13344
ec6860c49990
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13343
diff
changeset
|
3677 |
|aTextSeparatorChar items scanningWord inStream element lastIsFieldSeparator sz| |
ec6860c49990
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13343
diff
changeset
|
3678 |
|
ec6860c49990
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13343
diff
changeset
|
3679 |
aTextSeparatorOrNil isNil ifTrue:[ |
19961 | 3680 |
^ self asCollectionOfSubstringsSeparatedByAll: aFieldSeparatorString |
13327
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3681 |
]. |
13344
ec6860c49990
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13343
diff
changeset
|
3682 |
sz := aTextSeparatorOrNil size. |
24747 | 3683 |
sz == 0 ifTrue:[ |
19961 | 3684 |
aTextSeparatorChar := aTextSeparatorOrNil |
13344
ec6860c49990
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13343
diff
changeset
|
3685 |
] ifFalse:[ |
24747 | 3686 |
sz == 1 ifTrue:[ |
3687 |
"this is a String. Fetch the first character - compatibility to older expecco libs" |
|
3688 |
aTextSeparatorChar := aTextSeparatorOrNil first. |
|
3689 |
] ifFalse:[ |
|
3690 |
self error:'textSeparatorSize > 1'. |
|
3691 |
] |
|
3692 |
]. |
|
13327
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3693 |
|
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3694 |
items := OrderedCollection new. |
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3695 |
|
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3696 |
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
|
3697 |
[ |
19961 | 3698 |
inStream skipSeparators. |
3699 |
inStream atEnd |
|
13327
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3700 |
] whileFalse:[ |
19961 | 3701 |
lastIsFieldSeparator := false. |
3702 |
inStream peek == aTextSeparatorChar ifTrue:[ |
|
3703 |
inStream next. |
|
3704 |
element := ''. |
|
3705 |
scanningWord := true. |
|
3706 |
[ scanningWord and:[inStream atEnd not] ] whileTrue:[ |
|
3707 |
element := element , (inStream upTo:aTextSeparatorChar). |
|
3708 |
(inStream peek == aTextSeparatorChar) ifTrue:[ |
|
3709 |
element := element , aTextSeparatorChar . |
|
3710 |
inStream next. |
|
3711 |
] ifFalse:[ |
|
3712 |
scanningWord := false. |
|
3713 |
]. |
|
3714 |
]. |
|
3715 |
inStream upToAll_positionBefore:aFieldSeparatorString. |
|
3716 |
] ifFalse:[ |
|
3717 |
element := inStream upToAll_positionBefore:aFieldSeparatorString |
|
3718 |
]. |
|
3719 |
items add:element. |
|
3720 |
lastIsFieldSeparator := (inStream skipThroughAll:aFieldSeparatorString) notNil. |
|
13343
3f058ad0d2f1
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13332
diff
changeset
|
3721 |
]. |
3f058ad0d2f1
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13332
diff
changeset
|
3722 |
lastIsFieldSeparator ifTrue:[ |
19961 | 3723 |
"empty element at the end of the line" |
3724 |
items add:''. |
|
13327
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3725 |
]. |
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3726 |
|
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3727 |
^ items |
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3728 |
|
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3729 |
" |
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3730 |
self assert:(('#First#, #Second,SecondAdd#, #Third#' asCollectionOfSubstringsSeparatedBy:',' textSeparator: $#) |
19961 | 3731 |
sameContentsAs:#('First' 'Second,SecondAdd' 'Third')). |
13327
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3732 |
self assert:(('#Fir##st#, #Second,SecondAdd#, #Third#' asCollectionOfSubstringsSeparatedBy:',' textSeparator: $#) |
19961 | 3733 |
sameContentsAs:#('Fir#st' 'Second,SecondAdd' 'Third')). |
13327
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3734 |
self assert:(('#Fir##st#, Second,SecondAdd, #Third#' asCollectionOfSubstringsSeparatedBy:',' textSeparator: $#) |
19961 | 3735 |
sameContentsAs:#('Fir#st' 'Second' 'SecondAdd' 'Third')). |
13343
3f058ad0d2f1
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13332
diff
changeset
|
3736 |
self assert:(('First,Second,Third,,' asCollectionOfSubstringsSeparatedBy:',' textSeparator:nil) |
19961 | 3737 |
sameContentsAs:#('First' 'Second' 'Third' '' '')). |
13344
ec6860c49990
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13343
diff
changeset
|
3738 |
self assert:(('First,Second,Third,,' asCollectionOfSubstringsSeparatedBy:',' textSeparator:'#') |
19961 | 3739 |
sameContentsAs:#('First' 'Second' 'Third' '' '')). |
13327
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3740 |
" |
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3741 |
|
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3742 |
"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
|
3743 |
! |
999589445aa7
add new method for splitting a csv string into csv elements
ab
parents:
10437
diff
changeset
|
3744 |
|
7339
20969b5770c9
asCollectionOfSubstringsSeparatedByAll:
Claus Gittinger <cg@exept.de>
parents:
7318
diff
changeset
|
3745 |
asCollectionOfSubstringsSeparatedByAll:aSeparatorString |
23187 | 3746 |
"return a collection containing the lines (separated by aSeparatorString) of the receiver. |
3747 |
If aSeparatorString occurs multiple times in a row, |
|
7339
20969b5770c9
asCollectionOfSubstringsSeparatedByAll:
Claus Gittinger <cg@exept.de>
parents:
7318
diff
changeset
|
3748 |
the result will contain empty strings." |
20969b5770c9
asCollectionOfSubstringsSeparatedByAll:
Claus Gittinger <cg@exept.de>
parents:
7318
diff
changeset
|
3749 |
|
8852
9f5984eff2c2
use asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8794
diff
changeset
|
3750 |
^ self asCollectionOfSubCollectionsSeparatedByAll:aSeparatorString |
9f5984eff2c2
use asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8794
diff
changeset
|
3751 |
|
9f5984eff2c2
use asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8794
diff
changeset
|
3752 |
" |
8874 | 3753 |
'1::2::3::4::5::' asCollectionOfSubstringsSeparatedByAll:'::' |
7339
20969b5770c9
asCollectionOfSubstringsSeparatedByAll:
Claus Gittinger <cg@exept.de>
parents:
7318
diff
changeset
|
3754 |
" |
23187 | 3755 |
|
3756 |
"Modified (comment): / 03-07-2018 / 11:00:16 / Claus Gittinger" |
|
7339
20969b5770c9
asCollectionOfSubstringsSeparatedByAll:
Claus Gittinger <cg@exept.de>
parents:
7318
diff
changeset
|
3757 |
! |
20969b5770c9
asCollectionOfSubstringsSeparatedByAll:
Claus Gittinger <cg@exept.de>
parents:
7318
diff
changeset
|
3758 |
|
608 | 3759 |
asCollectionOfSubstringsSeparatedByAny:aCollectionOfSeparators |
3760 |
"return a collection containing the words (separated by any character |
|
3761 |
from aCollectionOfSeparators) of the receiver. |
|
3762 |
This allows breaking up strings using any character as separator." |
|
3763 |
||
8852
9f5984eff2c2
use asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8794
diff
changeset
|
3764 |
^ self asCollectionOfSubCollectionsSeparatedByAny:aCollectionOfSeparators |
608 | 3765 |
|
3766 |
" |
|
3767 |
'hello:world:isnt:this nice' asCollectionOfSubstringsSeparatedByAny:#($:) |
|
8874 | 3768 |
'hello:world:isnt:this nice' asCollectionOfSubstringsSeparatedByAny:':' |
3769 |
'hello:world:isnt:this nice' asCollectionOfSubstringsSeparatedByAny:(Array with:$: with:Character space) |
|
3770 |
'hello:world:isnt:this nice' asCollectionOfSubstringsSeparatedByAny:': ' |
|
3771 |
'h1e2l3l4o' asCollectionOfSubstringsSeparatedByAny:($1 to: $9) |
|
608 | 3772 |
" |
3773 |
! |
|
3774 |
||
3775 |
asCollectionOfWords |
|
8874 | 3776 |
"return a collection containing the words (separated by whitespace) |
3777 |
of the receiver. Multiple occurrences of whitespace characters will |
|
17517 | 3778 |
be treated like one - i.e. whitespace is skipped. |
3779 |
(sigh: it is called #'subStrings' in V'Age, and #'substrings' in Squeak) " |
|
608 | 3780 |
|
7797 | 3781 |
|words| |
608 | 3782 |
|
11905
cbc2191cfe2d
return stringCollections from asCollectionOfXXX
Claus Gittinger <cg@exept.de>
parents:
11904
diff
changeset
|
3783 |
words := StringCollection new. |
7797 | 3784 |
self asCollectionOfWordsDo:[:w | words add:w]. |
608 | 3785 |
^ words |
3786 |
||
3787 |
" |
|
3788 |
'hello world isnt this nice' asCollectionOfWords |
|
23660 | 3789 |
'hello world isnt this nice' asCollectionOfWordsDo:#transcribeCR |
608 | 3790 |
' hello world isnt this nice ' asCollectionOfWords |
3791 |
'hello' asCollectionOfWords |
|
3792 |
'' asCollectionOfWords |
|
3793 |
' ' asCollectionOfWords |
|
10703
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
3794 |
' foo bar__baz__bla__ bar ' asCollectionOfWords |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
3795 |
' foo __bar__baz__bla__ bar ' asCollectionOfWords |
608 | 3796 |
" |
23660 | 3797 |
|
3798 |
"Modified (comment): / 03-02-2019 / 13:07:57 / Claus Gittinger" |
|
9192 | 3799 |
! |
3800 |
||
3801 |
asCollectionOfWordsDo:aBlock |
|
8874 | 3802 |
"evaluate aBlock for each word (separated by whitespace) of the receiver. |
3803 |
Multiple occurrences of whitespace characters will be treated like one |
|
7797 | 3804 |
- i.e. whitespace is skipped. |
24535 | 3805 |
Returns the number of words (i.e. the number of invocations of aBlock). |
3806 |
This is the same as 'asCollectionOfWords do:...' but avoids the creation of |
|
3807 |
a temporary collection." |
|
7797 | 3808 |
|
3809 |
|count "{ Class:SmallInteger }" |
|
8874 | 3810 |
start "{ Class:SmallInteger }" |
3811 |
stop "{ Class:SmallInteger }" |
|
7797 | 3812 |
mySize "{ Class:SmallInteger }"| |
3813 |
||
3814 |
count := 0. |
|
3815 |
start := 1. |
|
3816 |
mySize := self size. |
|
3817 |
[start <= mySize] whileTrue:[ |
|
20874 | 3818 |
start := self indexOfNonSeparatorStartingAt:start. |
3819 |
start == 0 ifTrue:[ |
|
3820 |
^ count |
|
3821 |
]. |
|
3822 |
stop := self indexOfSeparatorStartingAt:start. |
|
3823 |
stop == 0 ifTrue:[ |
|
3824 |
aBlock value:(self copyFrom:start to:mySize). |
|
3825 |
^ count + 1 |
|
3826 |
]. |
|
3827 |
aBlock value:(self copyFrom:start to:(stop - 1)). |
|
3828 |
start := stop. |
|
3829 |
count := count + 1 |
|
7797 | 3830 |
]. |
3831 |
^ count |
|
3832 |
||
3833 |
" |
|
3834 |
'hello world isnt this nice' asCollectionOfWordsDo:[:w | Transcript showCR:w] |
|
23660 | 3835 |
'hello world isnt this nice' asCollectionOfWordsDo:#transcribeCR |
7797 | 3836 |
' hello world isnt this nice ' asCollectionOfWordsDo:[:w | Transcript showCR:w] |
3837 |
'hello' asCollectionOfWordsDo:[:w | Transcript showCR:w] |
|
3838 |
'' asCollectionOfWordsDo:[:w | Transcript showCR:w] |
|
3839 |
' ' asCollectionOfWordsDo:[:w | Transcript showCR:w] |
|
3840 |
" |
|
23660 | 3841 |
|
3842 |
"Modified (comment): / 03-02-2019 / 13:08:15 / Claus Gittinger" |
|
9192 | 3843 |
! |
3844 |
||
23971 | 3845 |
asDenseUnicodeString |
3846 |
"return the receiver as single-byte, double byte or 4-byte unicode string, |
|
3847 |
depending on the number of bits required to hold all characters in myself. |
|
3848 |
Use this to extract non-wide parts from a wide string, |
|
3849 |
i.e. after a substring has been copied out of a wide string" |
|
3850 |
||
3851 |
|nb| |
|
3852 |
||
3853 |
nb := self bytesPerCharacterNeeded. |
|
24034 | 3854 |
(nb ~~ self bytesPerCharacter) ifTrue:[ |
3855 |
nb == 1 ifTrue:[ |
|
3856 |
^ self asSingleByteString |
|
3857 |
]. |
|
3858 |
nb == 2 ifTrue:[ |
|
3859 |
^ self asUnicode16String |
|
23971 | 3860 |
]. |
3861 |
]. |
|
3862 |
^ self |
|
3863 |
||
3864 |
" |
|
3865 |
'abc' asUnicode16String asDenseUnicodeString |
|
3866 |
'abc' asUnicode32String asDenseUnicodeString |
|
3867 |
('abc',(Character value:16r165)) asDenseUnicodeString |
|
3868 |
('abc',(Character value:16r165)) asUnicode32String asDenseUnicodeString |
|
3869 |
" |
|
3870 |
||
3871 |
"Created: / 25-03-2019 / 16:28:02 / Claus Gittinger" |
|
24034 | 3872 |
"Modified: / 02-04-2019 / 10:48:44 / Claus Gittinger" |
23971 | 3873 |
! |
3874 |
||
9192 | 3875 |
asFilename |
3876 |
"return a Filename with pathname taken from the receiver" |
|
3877 |
||
3878 |
^ Filename named:self "(self asSingleByteStringReplaceInvalidWith:$?)" |
|
3879 |
||
3880 |
"Modified: 20.5.1996 / 09:38:15 / cg" |
|
3881 |
! |
|
608 | 3882 |
|
3060 | 3883 |
asFixedPoint |
3884 |
"read a fixedPoint number from the receiver. |
|
8874 | 3885 |
Notice, that errors may occur during the read, |
7423 | 3886 |
so you better setup some signal handler when using this method." |
3060 | 3887 |
|
3888 |
^ FixedPoint readFromString:self |
|
3889 |
||
3890 |
" |
|
8874 | 3891 |
'0.123' asFixedPoint |
3892 |
'12345' asFixedPoint |
|
3060 | 3893 |
'(1/5)' asFixedPoint |
3894 |
'foo' asFixedPoint |
|
9192 | 3895 |
Object errorSignal handle:[:ex | ex return:0] do:['foo' asFixedPoint] |
3896 |
" |
|
3897 |
||
3898 |
"Modified: / 25.10.1997 / 15:19:00 / cg" |
|
3899 |
! |
|
3900 |
||
3901 |
asFixedPoint:scale |
|
3060 | 3902 |
"read a fixedPoint number with scale number of post-decimal digits |
3903 |
from the receiver. Scale controls the number of displayed digits, |
|
3904 |
not the number of actually valid digits. |
|
8874 | 3905 |
Notice, that errors may occur during the read, |
7423 | 3906 |
so you better setup some signal handler when using this method." |
3060 | 3907 |
|
3908 |
^ (FixedPoint readFromString:self) scale:scale |
|
3909 |
||
3910 |
" |
|
8874 | 3911 |
'0.123' asFixedPoint:2 |
3912 |
'123456' asFixedPoint:2 |
|
3913 |
('3.14157' asFixedPoint:1) asFixedPoint:5 |
|
3914 |
'3.14157' asFixedPoint:2 |
|
3915 |
'foo' asFixedPoint:2 |
|
3060 | 3916 |
" |
3917 |
||
3918 |
"Modified: / 25.10.1997 / 15:21:57 / cg" |
|
9192 | 3919 |
! |
3920 |
||
3921 |
asFloat |
|
608 | 3922 |
"read a float number from the receiver. |
8874 | 3923 |
Notice, that errors may occur during the read, |
7422 | 3924 |
so you better setup some exception handler when using this method." |
608 | 3925 |
|
3926 |
^ (Number readFromString:self) asFloat |
|
3927 |
||
3928 |
" |
|
8874 | 3929 |
'0.123' asFloat |
608 | 3930 |
'12345' asFloat |
3931 |
'(1/5)' asFloat |
|
8874 | 3932 |
Object errorSignal handle:[:ex | ex return:0] do:['foo' asFloat] |
608 | 3933 |
" |
3934 |
! |
|
3935 |
||
25204 | 3936 |
asImmutableCollection |
3937 |
"fallback for subclasses of me which are not single byte strings. |
|
3938 |
return myself" |
|
3939 |
||
3940 |
^ self |
|
3941 |
! |
|
3942 |
||
3943 |
asImmutableString |
|
3944 |
"fallback for subclasses of me which are not single byte strings. |
|
3945 |
return myself" |
|
3946 |
||
3947 |
^ self |
|
3948 |
! |
|
3949 |
||
608 | 3950 |
asInteger |
9190 | 3951 |
"convert the receiver into an integer. |
8874 | 3952 |
Notice, that errors may occur during the read, |
9190 | 3953 |
so you better setup some exception handler when using this method. |
3954 |
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
|
3955 |
in other smalltalks. |
9190 | 3956 |
For less strict integer reading, use Integer readFrom:aString" |
608 | 3957 |
|
3958 |
^ Integer readFromString:self |
|
3959 |
||
3960 |
" |
|
3961 |
'12345678901234567890' asInteger |
|
3962 |
'-1234' asInteger |
|
9190 | 3963 |
|
3964 |
The following raises an error: |
|
20874 | 3965 |
'0.123' asInteger <- reader finds more after reading 0 |
9190 | 3966 |
|
3967 |
whereas the less strict readFrom does not: |
|
20874 | 3968 |
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
|
3969 |
|
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
3970 |
'0.123' asInteger |
608 | 3971 |
'0.123' asNumber <- returns what you expect |
8874 | 3972 |
Object errorSignal handle:[:ex | ex return:0] do:['foo' asInteger] |
9190 | 3973 |
|
3974 |
" |
|
9192 | 3975 |
! |
3976 |
||
3977 |
asLowercase |
|
608 | 3978 |
"return a copy of myself in lowercase letters" |
3979 |
||
8003
6e690f4b79f1
asLowercase/asUppercase and friends care for 16-bit chars
Claus Gittinger <cg@exept.de>
parents:
7995
diff
changeset
|
3980 |
|newStr c bitsPerCharacter |
9192 | 3981 |
mySize "{ Class: SmallInteger }" | |
3982 |
||
3983 |
mySize := self size. |
|
3984 |
mySize == 0 ifTrue:[^ self]. |
|
18657 | 3985 |
|
9192 | 3986 |
newStr := self species new:mySize. |
3987 |
bitsPerCharacter := newStr bitsPerCharacter. |
|
3988 |
||
18657 | 3989 |
"/ handle the very seldom case of an uppercase char which needs |
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
3990 |
"/ more bits in its lowercase variant |
18657 | 3991 |
"/ (there are only a few of them) |
3992 |
||
9192 | 3993 |
1 to:mySize do:[:i | |
20705 | 3994 |
c := (self at:i) asLowercase. |
3995 |
(c bitsPerCharacter > bitsPerCharacter |
|
3996 |
and:[c stringSpecies ~= newStr stringSpecies]) ifTrue:[ |
|
3997 |
newStr := c stringSpecies fromString:newStr. |
|
3998 |
bitsPerCharacter := newStr bitsPerCharacter. |
|
3999 |
]. |
|
4000 |
newStr at:i put:c |
|
9192 | 4001 |
]. |
4002 |
^ newStr |
|
4003 |
||
4004 |
" |
|
4005 |
'HelloWorld' asLowercase |
|
18662 | 4006 |
'HelloWorld' asUnicode16String asLowercase |
9192 | 4007 |
'HelloWorld' asLowercaseFirst |
18657 | 4008 |
'HelloWorld' asUppercase |
20705 | 4009 |
'HelloWorldÿ' asUppercase |
4010 |
'HelloWorldŸ' asLowercase - currently returns an U16 string; should this be u8? |
|
9192 | 4011 |
" |
4012 |
! |
|
4013 |
||
4014 |
asLowercaseFirst |
|
17508 | 4015 |
"return a copy of myself where the first character is converted to lowercase. |
4016 |
If the first character is already lowercase, or there is no uppercase for it, return the |
|
4017 |
receiver." |
|
9192 | 4018 |
|
4019 |
|newString firstChar firstCharAsLowercase| |
|
4020 |
||
11525 | 4021 |
self isEmpty ifTrue:[^ self]. |
9192 | 4022 |
firstChar := (self at:1). |
4023 |
firstCharAsLowercase := firstChar asLowercase. |
|
4024 |
firstChar == firstCharAsLowercase ifTrue:[ ^ self]. |
|
4025 |
||
4026 |
firstCharAsLowercase bitsPerCharacter > self bitsPerCharacter ifTrue:[ |
|
20874 | 4027 |
newString := firstCharAsLowercase stringSpecies fromString:self. |
9192 | 4028 |
] ifFalse:[ |
20874 | 4029 |
newString := self stringSpecies fromString:self. |
9192 | 4030 |
]. |
9190 | 4031 |
newString at:1 put:firstCharAsLowercase. |
4032 |
^ newString |
|
608 | 4033 |
|
4034 |
" |
|
8874 | 4035 |
'HelloWorld' asLowercase |
4036 |
'HelloWorld' asLowercaseFirst |
|
608 | 4037 |
" |
4038 |
! |
|
4039 |
||
11828 | 4040 |
asLowercaseLast |
4041 |
"return a copy of myself where the last character is |
|
4042 |
converted to lowercase." |
|
4043 |
||
4044 |
|newString sz| |
|
4045 |
||
4046 |
sz := self size. |
|
4047 |
newString := self copyFrom:1 to:sz. |
|
4048 |
sz > 0 ifTrue:[ |
|
20874 | 4049 |
newString at:sz put:(newString at:sz) asLowercase |
11828 | 4050 |
]. |
4051 |
^ newString |
|
4052 |
||
4053 |
" |
|
12177 | 4054 |
'HelloWorld' asLowercase |
4055 |
'HelloWorlD' asLowercaseLast |
|
11828 | 4056 |
" |
4057 |
! |
|
4058 |
||
17644 | 4059 |
asMutator |
19473 | 4060 |
"return a corresponding setter method's selector. |
4061 |
I.e. #foo asMutator returns #foo:" |
|
17644 | 4062 |
|
20873 | 4063 |
^ (self asSingleByteString,':') asSymbol |
17644 | 4064 |
! |
4065 |
||
9190 | 4066 |
asNumber |
4067 |
"read a number from the receiver. |
|
4068 |
Notice, that (in contrast to ST-80) errors may occur during the read, |
|
4069 |
so you better setup some signal handler when using this method. |
|
4070 |
Also notice, that this is meant to read end-user numbers from a string; |
|
9135 | 4071 |
it does not handle smalltalk numbers (i.e. radix). |
4072 |
To read a smalltalk number, use Number >> readSmalltalkFrom:. |
|
608 | 4073 |
This may change if ANSI specifies it." |
4074 |
||
4075 |
"/ ST-80 behavior: |
|
4076 |
"/ ^ Number readFromString:self onError:0 |
|
4077 |
||
17061 | 4078 |
^ Number fromString:self |
4079 |
||
4080 |
" |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
4081 |
'123' asNumber |
608 | 4082 |
'123.567' asNumber |
4083 |
'(5/6)' asNumber |
|
4084 |
'foo' asNumber |
|
17061 | 4085 |
'123a' asNumber |
8874 | 4086 |
Object errorSignal handle:[:ex | ex returnWith:0] do:['foo' asNumber] |
608 | 4087 |
" |
9192 | 4088 |
! |
4089 |
||
4090 |
asNumberFromFormatString:ignored |
|
608 | 4091 |
"read a number from the receiver, ignoring any nonDigit characters. |
4092 |
This is typically used to convert from strings which include |
|
4093 |
dollar-signs or millenium digits. However, this method also ignores |
|
4094 |
the decimal point (if any) and therefore should be used with care." |
|
4095 |
||
4096 |
|tempString| |
|
4097 |
||
4098 |
tempString := self collect:[:char | char isDigit]. |
|
4099 |
^ Number readFromString:tempString onError:0 |
|
4100 |
||
4101 |
" |
|
4102 |
'USD 123' asNumberFromFormatString:'foo' |
|
4103 |
'DM 123' asNumberFromFormatString:'foo' |
|
4104 |
'123' asNumberFromFormatString:'foo' |
|
4105 |
'123.567' asNumberFromFormatString:'foo' |
|
4106 |
'(5/6)' asNumberFromFormatString:'foo' |
|
4107 |
'foo' asNumberFromFormatString:'foo' |
|
4108 |
" |
|
9192 | 4109 |
! |
4110 |
||
21994 | 4111 |
asPackageId |
4112 |
"given a package-string as receiver, return a packageId object. |
|
4113 |
packageIds hide the details of module/directory handling inside the path. |
|
4114 |
See PackageId for the required format of those strings." |
|
4115 |
||
4116 |
^ PackageId from: self string |
|
4117 |
||
21995 | 4118 |
" |
4119 |
'stx:libbasic' asPackageId |
|
4120 |
" |
|
4121 |
||
21994 | 4122 |
"Created: / 11-07-2017 / 18:24:47 / cg" |
4123 |
! |
|
4124 |
||
7977 | 4125 |
asSingleByteStringIfPossible |
4126 |
"if possible, return the receiver converted to a 'normal' string. |
|
18181 | 4127 |
It is only possible, if there are no characters with codePoints above 255 in the receiver. |
4128 |
If not possible, the (wideString) receiver is returned." |
|
7977 | 4129 |
|
14555 | 4130 |
self isWideString ifFalse:[^ self]. |
18967 | 4131 |
self containsNon8BitElements ifTrue:[^ self]. |
4132 |
^ self asSingleByteString |
|
9192 | 4133 |
|
4134 |
" |
|
4135 |
'hello' asSingleByteStringIfPossible |
|
18967 | 4136 |
'hello' asText asSingleByteStringIfPossible |
4137 |
'hello' asUnicodeString asText asSingleByteStringIfPossible |
|
9192 | 4138 |
'hello' asUnicodeString asSingleByteStringIfPossible |
4139 |
" |
|
4140 |
! |
|
4141 |
||
4142 |
asSingleByteStringReplaceInvalidWith:replacementCharacter |
|
4143 |
"return the receiver converted to a 'normal' string, |
|
4144 |
with invalid characters replaced by replacementCharacter. |
|
4145 |
Can be used to convert from 16-bit strings to 8-bit strings |
|
4146 |
and replace characters above code-255 with some replacement." |
|
4147 |
||
4148 |
|newString| |
|
4149 |
||
4150 |
newString := String new:(self size). |
|
4151 |
1 to:self size do:[:idx | |
|
20874 | 4152 |
|char| |
4153 |
||
4154 |
char := self at:idx. |
|
4155 |
char codePoint <= 16rFF ifTrue:[ |
|
4156 |
newString at:idx put:char |
|
4157 |
] ifFalse:[ |
|
4158 |
newString at:idx put:replacementCharacter |
|
4159 |
]. |
|
9190 | 4160 |
]. |
4161 |
^ newString |
|
4162 |
||
4163 |
"Created: 30.6.1997 / 13:02:14 / cg" |
|
7977 | 4164 |
! |
4165 |
||
9192 | 4166 |
asString |
4167 |
"return myself - I am a string" |
|
4168 |
||
4169 |
^ self |
|
4170 |
! |
|
4171 |
||
4172 |
asStringCollection |
|
4173 |
"return a collection of lines from myself." |
|
4174 |
||
4175 |
^ StringCollection fromString:self "string" |
|
4176 |
||
21639 | 4177 |
" |
4178 |
'hello\world\1\2\3' withCRs asStringCollection first |
|
4179 |
" |
|
4180 |
||
4181 |
"Modified: / 13-05-1996 / 20:36:59 / cg" |
|
4182 |
"Modified (comment): / 13-03-2017 / 14:05:14 / cg" |
|
9192 | 4183 |
! |
1375
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
4184 |
|
24162 | 4185 |
asStringWithBitsPerCharacterAtLeast:numRequiredBitsPerCharacter |
4186 |
"return the receiver in a representation which supports numRequiredBitsPerCharacter. |
|
4187 |
I.e. if required, convert to a Unicode16 or Unicode32 string" |
|
4188 |
||
4189 |
numRequiredBitsPerCharacter > 8 ifTrue:[ |
|
4190 |
numRequiredBitsPerCharacter > 16 ifTrue:[ |
|
4191 |
^ self asUnicode32String |
|
4192 |
]. |
|
4193 |
numRequiredBitsPerCharacter > self bitsPerCharacter ifTrue:[ |
|
4194 |
^ self asUnicode16String |
|
4195 |
]. |
|
4196 |
]. |
|
4197 |
^ self |
|
4198 |
||
4199 |
" |
|
4200 |
self assert:('abc' asStringWithBitsPerCharacterAtLeast:8) bitsPerCharacter == 8. |
|
4201 |
self assert:('abc' asStringWithBitsPerCharacterAtLeast:16) bitsPerCharacter == 16. |
|
4202 |
self assert:('abc' asStringWithBitsPerCharacterAtLeast:32) bitsPerCharacter == 32. |
|
4203 |
||
4204 |
self assert:('abc' asUnicode16String asStringWithBitsPerCharacterAtLeast:8) bitsPerCharacter == 16. |
|
4205 |
self assert:('abc' asUnicode16String asStringWithBitsPerCharacterAtLeast:16) bitsPerCharacter == 16. |
|
4206 |
self assert:('abc' asUnicode16String asStringWithBitsPerCharacterAtLeast:32) bitsPerCharacter == 32. |
|
4207 |
||
4208 |
self assert:('abc' asUnicode32String asStringWithBitsPerCharacterAtLeast:8) bitsPerCharacter == 32. |
|
4209 |
self assert:('abc' asUnicode32String asStringWithBitsPerCharacterAtLeast:16) bitsPerCharacter == 32. |
|
4210 |
self assert:('abc' asUnicode32String asStringWithBitsPerCharacterAtLeast:32) bitsPerCharacter == 32. |
|
4211 |
" |
|
4212 |
||
4213 |
"Created: / 26-05-2019 / 12:47:07 / Claus Gittinger" |
|
4214 |
! |
|
4215 |
||
23311 | 4216 |
asStringWithoutEmphasis |
4217 |
"return myself as a string without any emphasis" |
|
4218 |
"return myself - I am a string" |
|
4219 |
||
4220 |
^ self |
|
4221 |
||
4222 |
"Created: / 29-08-2018 / 09:21:07 / Claus Gittinger" |
|
4223 |
! |
|
4224 |
||
1434 | 4225 |
asSymbol |
19039 | 4226 |
"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
|
4227 |
The receiver must be a singleByte-String. |
ccd0e8f26439
printing - migrating towards unicodeSupport in ST-code
Claus Gittinger <cg@exept.de>
parents:
8040
diff
changeset
|
4228 |
TwoByte- and FourByteSymbols are (currently ?) not allowed." |
ccd0e8f26439
printing - migrating towards unicodeSupport in ST-code
Claus Gittinger <cg@exept.de>
parents:
8040
diff
changeset
|
4229 |
|
ccd0e8f26439
printing - migrating towards unicodeSupport in ST-code
Claus Gittinger <cg@exept.de>
parents:
8040
diff
changeset
|
4230 |
|str| |
ccd0e8f26439
printing - migrating towards unicodeSupport in ST-code
Claus Gittinger <cg@exept.de>
parents:
8040
diff
changeset
|
4231 |
|
9192 | 4232 |
str := self string. |
4233 |
str ~~ self ifTrue:[ ^ str asSymbol ]. |
|
4234 |
^ self asSingleByteString asSymbol |
|
4235 |
! |
|
4236 |
||
4237 |
asSymbolIfInterned |
|
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
4238 |
"If a symbol with the receiver's characters is already known, return it. |
19692 | 4239 |
Otherwise, return nil. |
9230 | 4240 |
This can be used to query for an existing symbol and is the same as: |
20874 | 4241 |
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
|
4242 |
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
|
4243 |
The receiver must be a singleByte-String. |
ccd0e8f26439
printing - migrating towards unicodeSupport in ST-code
Claus Gittinger <cg@exept.de>
parents:
8040
diff
changeset
|
4244 |
TwoByte- and FourByteSymbols are (currently ?) not allowed." |
1435
3ee1fde2aa58
allow asSymbolIfInterned for Texts
Claus Gittinger <cg@exept.de>
parents:
1434
diff
changeset
|
4245 |
|
18967 | 4246 |
|str| |
4247 |
||
4248 |
str := self string. |
|
4249 |
str == self ifTrue:[ |
|
20874 | 4250 |
"must be some kind of N-ByteString" |
4251 |
str := self asSingleByteStringIfPossible. |
|
4252 |
str == self ifTrue:[ |
|
4253 |
"single byte string conversion is not possible" |
|
4254 |
^ nil. |
|
4255 |
]. |
|
6642 | 4256 |
]. |
18967 | 4257 |
^ str asSymbolIfInterned |
4258 |
||
4259 |
" |
|
4260 |
(Unicode16String with:(Character value:16rFFFF)) asSymbolIfInterned |
|
4261 |
'new' asUnicodeString asSymbolIfInterned |
|
4262 |
'new' asText asSymbolIfInterned |
|
4263 |
'new' asUnicodeString asText asSymbolIfInterned |
|
4264 |
" |
|
1435
3ee1fde2aa58
allow asSymbolIfInterned for Texts
Claus Gittinger <cg@exept.de>
parents:
1434
diff
changeset
|
4265 |
|
3ee1fde2aa58
allow asSymbolIfInterned for Texts
Claus Gittinger <cg@exept.de>
parents:
1434
diff
changeset
|
4266 |
"Created: 22.5.1996 / 16:37:04 / cg" |
9192 | 4267 |
! |
4268 |
||
20160 | 4269 |
asSymbolIfInternedOrSelf |
4270 |
"If a symbol with the receiver's characters is already known, return it. |
|
4271 |
Otherwise, return self. |
|
4272 |
This can be used to query for an existing symbol and is the same as: |
|
4273 |
self knownAsSymbol ifTrue:[self asSymbol] ifFalse:[self] |
|
4274 |
but slightly faster, since the symbol lookup operation is only performed once. |
|
4275 |
The receiver must be a singleByte-String. |
|
4276 |
TwoByte- and FourByteSymbols are (currently ?) not allowed." |
|
4277 |
||
4278 |
|sym| |
|
4279 |
||
4280 |
sym := self asSymbolIfInterned. |
|
4281 |
^ sym isNil ifTrue:[self] ifFalse:[sym]. |
|
4282 |
||
4283 |
||
4284 |
" |
|
4285 |
(Unicode16String with:(Character value:16rFFFF)) asSymbolIfInternedOrSelf |
|
4286 |
'new' asUnicodeString asSymbolIfInternedOrSelf |
|
4287 |
'new' asText asSymbolIfInternedOrSelf |
|
4288 |
'new' asUnicodeString asText asSymbolIfInternedOrSelf |
|
4289 |
" |
|
4290 |
||
4291 |
"Created: 22.5.1996 / 16:37:04 / cg" |
|
4292 |
! |
|
4293 |
||
9192 | 4294 |
asText |
9403 | 4295 |
"return a Text-object (string with emphasis) from myself." |
9192 | 4296 |
|
4297 |
"this test allows for small non-gui apps to be built without libbasic2 (where Text is)" |
|
4298 |
Text isNil ifTrue:[^ self]. |
|
17065 | 4299 |
^ Text string:self |
9192 | 4300 |
|
4301 |
"Created: 12.5.1996 / 10:41:14 / cg" |
|
4302 |
! |
|
4303 |
||
23617 | 4304 |
asTimeDuration |
4305 |
"return an TimeDuration object from the parsing the receiver string" |
|
4306 |
||
4307 |
^ TimeDuration readFromString:self |
|
4308 |
||
4309 |
" |
|
4310 |
Time now asTimeDuration |
|
4311 |
10 asTimeDuration |
|
4312 |
10 seconds asTimeDuration |
|
4313 |
'10m 20s' asTimeDuration |
|
4314 |
" |
|
4315 |
||
4316 |
"Created: / 21-01-2019 / 10:29:51 / Claus Gittinger" |
|
4317 |
! |
|
4318 |
||
17021 | 4319 |
asTimestamp |
4320 |
"convert the receiver into an Timestamp. |
|
4321 |
Notice, that errors may occur during the read, |
|
4322 |
so you better setup some exception handler when using this method." |
|
4323 |
||
21030 | 4324 |
^ Timestamp readFrom:self |
17021 | 4325 |
|
4326 |
" |
|
4327 |
'2014-11-10 21:30:22.444' asTimestamp |
|
4328 |
'2014-11-10 21:30:22.444Z' asTimestamp |
|
4329 |
'2014-11-10 21:30:22.444+0200' asTimestamp |
|
4330 |
" |
|
4331 |
! |
|
4332 |
||
9192 | 4333 |
asTitlecase |
8027 | 4334 |
"return a version of the receiver, where the first character is converted to titlecase, |
4335 |
and everything else to lowercase. |
|
4336 |
See the comment in Character on what titlecase is." |
|
4337 |
||
4338 |
|newStr c bitsPerCharacter |
|
4339 |
mySize "{ Class: SmallInteger }" | |
|
4340 |
||
4341 |
mySize := self size. |
|
4342 |
newStr := self species new:mySize. |
|
4343 |
bitsPerCharacter := newStr bitsPerCharacter. |
|
4344 |
||
4345 |
1 to:mySize do:[:i | |
|
20874 | 4346 |
i == 1 ifTrue:[ |
4347 |
c := (self at:i) asTitlecase. |
|
4348 |
] ifFalse:[ |
|
4349 |
c := (self at:i) asLowercase. |
|
4350 |
]. |
|
4351 |
c bitsPerCharacter > bitsPerCharacter ifTrue:[ |
|
4352 |
newStr := c stringSpecies fromString:newStr. |
|
4353 |
bitsPerCharacter := newStr bitsPerCharacter. |
|
4354 |
]. |
|
4355 |
newStr at:i put:c |
|
8027 | 4356 |
]. |
4357 |
^ newStr |
|
4358 |
||
4359 |
" |
|
8874 | 4360 |
'helloWorld' asTitlecase |
4361 |
'HelloWorld' asTitlecase |
|
4362 |
'HELLOWORLD' asTitlecase |
|
4363 |
'helloworld' asTitlecase |
|
8027 | 4364 |
" |
9192 | 4365 |
! |
4366 |
||
4367 |
asTitlecaseFirst |
|
8023 | 4368 |
"return a version of the receiver, where the first character is converted to titlecase. |
8024 | 4369 |
Titlecase is much like uppercase for most characters, with the exception of some combined |
4370 |
(2-character glyphs), which consist of an upper- and lower-case characters. |
|
4371 |
If the first character is already titlecase, or there is no titlecasepercase for it, return the |
|
8023 | 4372 |
receiver." |
4373 |
||
8026 | 4374 |
" |
8874 | 4375 |
For example, in Unicode, character U+01F3 is LATIN SMALL LETTER DZ. |
4376 |
(Let us write this compound character using ASCII as 'dz'.) |
|
4377 |
This character uppercases to character U+01F1, LATIN CAPITAL LETTER DZ. |
|
4378 |
(Which is basically 'DZ'.) |
|
4379 |
But it titlecases to to character U+01F2, LATIN CAPITAL LETTER D WITH SMALL LETTER Z. |
|
8026 | 4380 |
(Which we can write 'Dz'.) |
4381 |
||
4382 |
character uppercase titlecase |
|
4383 |
--------- --------- --------- |
|
4384 |
dz DZ Dz |
|
4385 |
" |
|
4386 |
||
8023 | 4387 |
|newString firstChar firstCharAsTitlecase| |
4388 |
||
4389 |
firstChar := (self at:1). |
|
8025 | 4390 |
firstCharAsTitlecase := firstChar asTitlecase. |
8023 | 4391 |
firstChar == firstCharAsTitlecase ifTrue:[ ^ self]. |
8037
8cce25b1059c
care for wide chars in asXXXFirst
Claus Gittinger <cg@exept.de>
parents:
8032
diff
changeset
|
4392 |
|
8cce25b1059c
care for wide chars in asXXXFirst
Claus Gittinger <cg@exept.de>
parents:
8032
diff
changeset
|
4393 |
firstCharAsTitlecase bitsPerCharacter > self bitsPerCharacter ifTrue:[ |
20874 | 4394 |
newString := firstCharAsTitlecase stringSpecies fromString:self. |
8037
8cce25b1059c
care for wide chars in asXXXFirst
Claus Gittinger <cg@exept.de>
parents:
8032
diff
changeset
|
4395 |
] ifFalse:[ |
20874 | 4396 |
newString := self stringSpecies fromString:self. |
8037
8cce25b1059c
care for wide chars in asXXXFirst
Claus Gittinger <cg@exept.de>
parents:
8032
diff
changeset
|
4397 |
]. |
8023 | 4398 |
newString at:1 put:firstCharAsTitlecase. |
4399 |
^ newString |
|
4400 |
||
4401 |
" |
|
8874 | 4402 |
'helloWorld' asTitlecaseFirst |
4403 |
'HelloWorld' asTitlecaseFirst |
|
8023 | 4404 |
" |
9192 | 4405 |
! |
4406 |
||
4407 |
asTwoByteString |
|
4408 |
"return the receiver converted to a two-byte string. |
|
16051 | 4409 |
Will be obsolete soon - use asUnicode16String." |
9192 | 4410 |
|
4411 |
^ TwoByteString fromString:self |
|
4412 |
! |
|
4413 |
||
4414 |
asURI |
|
4415 |
"return an URI with string taken from the receiver" |
|
4416 |
||
4417 |
^ URI fromString:self |
|
4418 |
! |
|
8710 | 4419 |
|
5776 | 4420 |
asURL |
4421 |
"return an URL-object from myself." |
|
4422 |
||
4423 |
^ URL fromString:self |
|
4424 |
||
4425 |
" |
|
8874 | 4426 |
'http://www.exept.de:80/index.html' asURL host |
4427 |
'http://www.exept.de:80/index.html' asURL port |
|
9192 | 4428 |
'http://www.exept.de:80/index.html' asURL method |
4429 |
'http://www.exept.de:80/index.html' asURL path |
|
24147 | 4430 |
'file:///tmp/index.html' asURL path |
4431 |
'file:///tmp/index.html' asURL method |
|
4432 |
'file:///tmp/index.html' asURL asString |
|
4433 |
" |
|
4434 |
||
4435 |
"Modified (format): / 25-05-2019 / 09:33:18 / Claus Gittinger" |
|
9192 | 4436 |
! |
4437 |
||
18901 | 4438 |
asUUID |
4439 |
"return self as a UUID" |
|
4440 |
||
4441 |
^ UUID fromString:self |
|
4442 |
||
4443 |
" |
|
4444 |
UUID genUUID asString asUUID |
|
4445 |
'{EAB22AC0-30C1-11CF-A7EB-0000C05BAE0B}' asUUID |
|
4446 |
'{EAB22AC0-30C1-11CF-A7EB-0000C05BAE0B}' asUnicodeString asUUID |
|
4447 |
'EAB22AC0-30C1-11CF-A7EB-0000C05BAE0B' asUUID |
|
4448 |
'EAB22AC0-30C1-11CF-A7EB-0000C05BAE0B quatsch' asUUID |
|
4449 |
'quark EAB22AC0-30C1-11CF-A7EB-0000C05BAE0B' asUUID |
|
4450 |
" |
|
4451 |
||
4452 |
"Modified: / 02-08-2007 / 16:43:29 / cg" |
|
4453 |
! |
|
4454 |
||
9192 | 4455 |
asUnicode16String |
15035 | 4456 |
"return the receiver in a two-byte per character representation. |
4457 |
Normally, the internal ST/X representation should be transparent and not |
|
4458 |
of the programmer's concern; except when strings are passed to the outside world, |
|
4459 |
such as wide-string ffi calls or file contents." |
|
4460 |
||
17600 | 4461 |
|sz| |
4462 |
||
4463 |
sz := self size. |
|
4464 |
||
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
4465 |
^ (Unicode16String new:sz) |
20874 | 4466 |
replaceFrom:1 to:sz with:self startingAt:1; |
4467 |
yourself. |
|
4468 |
||
4469 |
" |
|
4470 |
'abc' asUnicode16String |
|
17600 | 4471 |
" |
24212 | 4472 |
|
4473 |
"Modified (comment): / 28-05-2019 / 14:41:04 / Stefan Vogel" |
|
4474 |
! |
|
4475 |
||
4476 |
asUnicode16StringReplaceInvalidWith:replacementCharacter |
|
4477 |
^ self asUnicode16String |
|
4478 |
||
4479 |
"Created: / 28-05-2019 / 12:15:44 / Stefan Vogel" |
|
17600 | 4480 |
! |
4481 |
||
4482 |
asUnicode16StringZ |
|
4483 |
"return the receiver in a two-byte per character representation. |
|
17625 | 4484 |
Make sure that is zero terminated (last char is 16r0000). |
17600 | 4485 |
Normally, the internal ST/X representation should be transparent and not |
4486 |
of the programmer's concern; except when strings are passed to the outside world, |
|
4487 |
such as wide-string ffi calls or file contents." |
|
4488 |
||
4489 |
|sz| |
|
4490 |
||
4491 |
sz := self size. |
|
25187 | 4492 |
(sz ~~ 0 and:[(self at:sz) == (Character codePoint:0)]) ifTrue:[ |
4493 |
^ self asUnicode16String. |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
4494 |
]. |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
4495 |
|
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
4496 |
^ (Unicode16String new:sz+1) |
20874 | 4497 |
replaceFrom:1 to:sz with:self startingAt:1; |
4498 |
at:sz+1 put:(Character codePoint:0); |
|
4499 |
yourself. |
|
17600 | 4500 |
|
4501 |
" |
|
22249 | 4502 |
'' asUnicode16StringZ |
20874 | 4503 |
'abc' asUnicode16StringZ |
4504 |
'abc' asUnicode16String asUnicode16StringZ |
|
17600 | 4505 |
" |
9192 | 4506 |
! |
8295
8433d819226b
Define #asUnicode16String and #asUnicode32String in CharacterArray
Stefan Vogel <sv@exept.de>
parents:
8222
diff
changeset
|
4507 |
|
8433d819226b
Define #asUnicode16String and #asUnicode32String in CharacterArray
Stefan Vogel <sv@exept.de>
parents:
8222
diff
changeset
|
4508 |
asUnicode32String |
16051 | 4509 |
"return the receiver in a four-byte per character representation. |
4510 |
Normally, the internal ST/X representation should be transparent and not |
|
4511 |
of the programmer's concern; except when strings are passed to the outside world, |
|
4512 |
such as wide-string ffi calls or file contents." |
|
4513 |
||
4514 |
^ self as:Unicode32String |
|
8295
8433d819226b
Define #asUnicode16String and #asUnicode32String in CharacterArray
Stefan Vogel <sv@exept.de>
parents:
8222
diff
changeset
|
4515 |
! |
8433d819226b
Define #asUnicode16String and #asUnicode32String in CharacterArray
Stefan Vogel <sv@exept.de>
parents:
8222
diff
changeset
|
4516 |
|
8433d819226b
Define #asUnicode16String and #asUnicode32String in CharacterArray
Stefan Vogel <sv@exept.de>
parents:
8222
diff
changeset
|
4517 |
asUnicodeString |
16051 | 4518 |
"return the receiver in a two-byte per character representation. |
4519 |
Normally, the internal ST/X representation should be transparent and not |
|
4520 |
of the programmer's concern; except when strings are passed to the outside world, |
|
4521 |
such as wide-string ffi calls or file contents." |
|
4522 |
||
4523 |
^ self asUnicode16String |
|
4524 |
" |
|
4525 |
'Hello World' asUnicodeString |
|
14141
e86d245213b0
comment/format in: #asUnicodeString
Stefan Vogel <sv@exept.de>
parents:
14137
diff
changeset
|
4526 |
" |
8295
8433d819226b
Define #asUnicode16String and #asUnicode32String in CharacterArray
Stefan Vogel <sv@exept.de>
parents:
8222
diff
changeset
|
4527 |
! |
8433d819226b
Define #asUnicode16String and #asUnicode32String in CharacterArray
Stefan Vogel <sv@exept.de>
parents:
8222
diff
changeset
|
4528 |
|
13073 | 4529 |
asUnixFilename |
4530 |
"return a UnixFilename with pathname taken from the receiver" |
|
4531 |
||
4532 |
^ UnixFilename named:self |
|
4533 |
||
4534 |
"Created: / 20-09-2010 / 21:42:55 / cg" |
|
4535 |
! |
|
4536 |
||
608 | 4537 |
asUppercase |
4538 |
"return a copy of myself in uppercase letters" |
|
4539 |
||
8003
6e690f4b79f1
asLowercase/asUppercase and friends care for 16-bit chars
Claus Gittinger <cg@exept.de>
parents:
7995
diff
changeset
|
4540 |
|newStr c bitsPerCharacter |
608 | 4541 |
mySize "{ Class: SmallInteger }" | |
4542 |
||
4543 |
mySize := self size. |
|
8954
12e701d7b5ac
allow for receiver to be empty in asLowercase and asUppercase.
Claus Gittinger <cg@exept.de>
parents:
8938
diff
changeset
|
4544 |
mySize == 0 ifTrue:[^ self]. |
608 | 4545 |
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
|
4546 |
bitsPerCharacter := newStr bitsPerCharacter. |
6e690f4b79f1
asLowercase/asUppercase and friends care for 16-bit chars
Claus Gittinger <cg@exept.de>
parents:
7995
diff
changeset
|
4547 |
|
18657 | 4548 |
"/ handle the very seldom case of a lowercase char which needs |
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
4549 |
"/ more bits in its uppercase variant |
18657 | 4550 |
"/ (there are only a few of them) |
4551 |
||
608 | 4552 |
1 to:mySize do:[:i | |
20874 | 4553 |
c := (self at:i) asUppercase. |
4554 |
c bitsPerCharacter > bitsPerCharacter ifTrue:[ |
|
4555 |
newStr := c stringSpecies fromString:newStr. |
|
4556 |
bitsPerCharacter := newStr bitsPerCharacter. |
|
4557 |
]. |
|
4558 |
newStr at:i put:c |
|
608 | 4559 |
]. |
4560 |
^ newStr |
|
4561 |
||
4562 |
" |
|
8874 | 4563 |
'helloWorld' asUppercase |
4564 |
'helloWorld' asUppercaseFirst |
|
9192 | 4565 |
(Character value:16rB5) asString asUppercase -- needs 16 bits !! |
18657 | 4566 |
(Character value:16rFF) asString asUppercase -- needs 16 bits !! |
9192 | 4567 |
" |
4568 |
! |
|
4569 |
||
4570 |
asUppercaseFirst |
|
8003
6e690f4b79f1
asLowercase/asUppercase and friends care for 16-bit chars
Claus Gittinger <cg@exept.de>
parents:
7995
diff
changeset
|
4571 |
"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
|
4572 |
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
|
4573 |
receiver." |
6e690f4b79f1
asLowercase/asUppercase and friends care for 16-bit chars
Claus Gittinger <cg@exept.de>
parents:
7995
diff
changeset
|
4574 |
|
6e690f4b79f1
asLowercase/asUppercase and friends care for 16-bit chars
Claus Gittinger <cg@exept.de>
parents:
7995
diff
changeset
|
4575 |
|newString firstChar firstCharAsUppercase| |
6e690f4b79f1
asLowercase/asUppercase and friends care for 16-bit chars
Claus Gittinger <cg@exept.de>
parents:
7995
diff
changeset
|
4576 |
|
12679 | 4577 |
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
|
4578 |
firstChar := self at:1. |
8003
6e690f4b79f1
asLowercase/asUppercase and friends care for 16-bit chars
Claus Gittinger <cg@exept.de>
parents:
7995
diff
changeset
|
4579 |
firstCharAsUppercase := firstChar asUppercase. |
6e690f4b79f1
asLowercase/asUppercase and friends care for 16-bit chars
Claus Gittinger <cg@exept.de>
parents:
7995
diff
changeset
|
4580 |
firstChar == firstCharAsUppercase ifTrue:[ ^ self]. |
6e690f4b79f1
asLowercase/asUppercase and friends care for 16-bit chars
Claus Gittinger <cg@exept.de>
parents:
7995
diff
changeset
|
4581 |
|
12679 | 4582 |
"/ 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
|
4583 |
firstCharAsUppercase bitsPerCharacter > self bitsPerCharacter ifTrue:[ |
20874 | 4584 |
newString := firstCharAsUppercase stringSpecies fromString:self. |
8037
8cce25b1059c
care for wide chars in asXXXFirst
Claus Gittinger <cg@exept.de>
parents:
8032
diff
changeset
|
4585 |
] ifFalse:[ |
20874 | 4586 |
newString := self stringSpecies fromString:self. |
8037
8cce25b1059c
care for wide chars in asXXXFirst
Claus Gittinger <cg@exept.de>
parents:
8032
diff
changeset
|
4587 |
]. |
8003
6e690f4b79f1
asLowercase/asUppercase and friends care for 16-bit chars
Claus Gittinger <cg@exept.de>
parents:
7995
diff
changeset
|
4588 |
newString at:1 put:firstCharAsUppercase. |
608 | 4589 |
^ newString |
4590 |
||
4591 |
" |
|
8874 | 4592 |
'helloWorld' asUppercase |
4593 |
'helloWorld' asUppercaseFirst |
|
4594 |
'HelloWorld' asUppercaseFirst |
|
608 | 4595 |
" |
9192 | 4596 |
! |
4597 |
||
11828 | 4598 |
asUppercaseLast |
4599 |
"return a copy of myself where the last character is |
|
4600 |
converted to uppercase." |
|
4601 |
||
4602 |
|newString sz| |
|
4603 |
||
4604 |
sz := self size. |
|
4605 |
newString := self copyFrom:1 to:sz. |
|
4606 |
sz > 0 ifTrue:[ |
|
20874 | 4607 |
newString at:sz put:(newString at:sz) asUppercase |
11828 | 4608 |
]. |
4609 |
^ newString |
|
4610 |
||
4611 |
" |
|
12177 | 4612 |
'HelloWorld' asUppercase |
4613 |
'HelloWorld' asUppercaseLast |
|
11828 | 4614 |
" |
4615 |
! |
|
4616 |
||
17021 | 4617 |
asUtcTimestamp |
4618 |
"convert the receiver into an UtcTimestamp (Time is interpreted in the UTC timezone). |
|
4619 |
Notice, that errors may occur during the read, |
|
4620 |
so you better setup some exception handler when using this method." |
|
4621 |
||
21030 | 4622 |
^ UtcTimestamp readFrom:self |
17021 | 4623 |
|
4624 |
" |
|
4625 |
'2014-11-10 21:30:22.444' asUtcTimestamp |
|
4626 |
" |
|
4627 |
! |
|
4628 |
||
23121 | 4629 |
asVersionNumberCollection |
4630 |
"Convert a string like 1.2.3a to an Array of numbers (or string, if not a number). |
|
4631 |
Remove zeroes from the end." |
|
4632 |
||
4633 |
^ (self asCollectionOfSubstringsSeparatedByAny:'.-_') asVersionNumberCollection. |
|
4634 |
||
4635 |
" |
|
4636 |
'1' asVersionNumberCollection. |
|
4637 |
'1.1' asVersionNumberCollection. |
|
4638 |
'1.1a' asVersionNumberCollection. |
|
4639 |
'1.1.0' asVersionNumberCollection. |
|
4640 |
'01.01.0' asVersionNumberCollection. |
|
4641 |
'expecco_18_1_0' asVersionNumberCollection. |
|
4642 |
" |
|
4643 |
||
4644 |
"Created: / 20-06-2018 / 11:23:53 / Stefan Vogel" |
|
4645 |
"Modified: / 20-06-2018 / 19:24:36 / Stefan Vogel" |
|
4646 |
! |
|
4647 |
||
11062 | 4648 |
literalArrayEncoding |
4649 |
"encode myself as an array literal, from which a copy of the receiver |
|
4650 |
can be reconstructed with #decodeAsLiteralArray." |
|
4651 |
||
4652 |
^ self |
|
4653 |
||
4654 |
"Modified: 1.9.1995 / 02:25:45 / claus" |
|
4655 |
"Modified: 22.4.1996 / 13:00:50 / cg" |
|
4656 |
! |
|
4657 |
||
9192 | 4658 |
string |
4659 |
"return the receiver - for ST-80 compatibility" |
|
4660 |
||
4661 |
^ self |
|
4662 |
||
4663 |
"Modified: 27.4.1996 / 13:29:30 / cg" |
|
4664 |
! ! |
|
4665 |
||
4666 |
!CharacterArray methodsFor:'copying'! |
|
4667 |
||
22528 | 4668 |
, aStringOrCharacterOrAnyOther |
2720
98c21d9a07c0
allow mixed strings to be concatenated using comma
Claus Gittinger <cg@exept.de>
parents:
2692
diff
changeset
|
4669 |
"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
|
4670 |
This is nonStandard, but convenient" |
2f14bbd792a9
, now accepts a character-argument
Claus Gittinger <cg@exept.de>
parents:
1028
diff
changeset
|
4671 |
|
23998 | 4672 |
|stringifiedArg |
4673 |
newSpecies newString l1 l2 combinedLen| |
|
22528 | 4674 |
|
4675 |
aStringOrCharacterOrAnyOther isCharacter ifTrue:[ |
|
4676 |
^ self copyWith:aStringOrCharacterOrAnyOther |
|
4677 |
]. |
|
4678 |
aStringOrCharacterOrAnyOther isText ifTrue:[ |
|
4679 |
^ aStringOrCharacterOrAnyOther concatenateFromString:self |
|
4680 |
]. |
|
4681 |
stringifiedArg := aStringOrCharacterOrAnyOther asString. |
|
4682 |
stringifiedArg isString ifTrue:[ |
|
24037 | 4683 |
(stringifiedArg bitsPerCharacter) > (self bitsPerCharacter) ifTrue:[ |
4684 |
newSpecies := stringifiedArg species. |
|
4685 |
] ifFalse:[ |
|
4686 |
newSpecies := self species. |
|
4687 |
]. |
|
23998 | 4688 |
l1 := self size. |
4689 |
l2 := stringifiedArg size. |
|
4690 |
combinedLen := l1 + l2. |
|
4691 |
newString := newSpecies uninitializedNew:combinedLen. |
|
24037 | 4692 |
newString |
4693 |
replaceFrom:1 to:l1 with:self startingAt:1; |
|
4694 |
replaceFrom:l1+1 to:combinedLen with:stringifiedArg startingAt:1. |
|
23998 | 4695 |
^ newString |
2720
98c21d9a07c0
allow mixed strings to be concatenated using comma
Claus Gittinger <cg@exept.de>
parents:
2692
diff
changeset
|
4696 |
]. |
22528 | 4697 |
^ super , stringifiedArg |
1050
2f14bbd792a9
, now accepts a character-argument
Claus Gittinger <cg@exept.de>
parents:
1028
diff
changeset
|
4698 |
|
2f14bbd792a9
, now accepts a character-argument
Claus Gittinger <cg@exept.de>
parents:
1028
diff
changeset
|
4699 |
" |
8874 | 4700 |
'hello' , $1 |
24037 | 4701 |
'hello' , (Character codePoint:1046) |
8874 | 4702 |
'hello' , '1' |
4703 |
'hello' , (' world' asText allBold) |
|
4704 |
'hello' , (JISEncodedString fromString:' world') |
|
2720
98c21d9a07c0
allow mixed strings to be concatenated using comma
Claus Gittinger <cg@exept.de>
parents:
2692
diff
changeset
|
4705 |
(JISEncodedString fromString:'hello') , ' world' |
1411 | 4706 |
|
1422 | 4707 |
Transcript showCR: |
20874 | 4708 |
(Text string:'hello' emphasis:#italic) , (Text string:' world' emphasis:#bold) |
22528 | 4709 |
|
4710 |
'hello',123,'world' |
|
4711 |
'hello' allBold,123,'world' |
|
4712 |
'hello',123,'world' allBold |
|
1050
2f14bbd792a9
, now accepts a character-argument
Claus Gittinger <cg@exept.de>
parents:
1028
diff
changeset
|
4713 |
" |
2f14bbd792a9
, now accepts a character-argument
Claus Gittinger <cg@exept.de>
parents:
1028
diff
changeset
|
4714 |
|
23997 | 4715 |
"Modified: / 28-06-1997 / 00:13:17 / cg" |
23998 | 4716 |
"Modified: / 27-03-2019 / 19:37:34 / Claus Gittinger" |
24033 | 4717 |
"Modified: / 02-04-2019 / 10:39:28 / Maren" |
24037 | 4718 |
"Modified (comment): / 02-04-2019 / 11:36:00 / Stefan Vogel" |
9192 | 4719 |
! |
4720 |
||
4721 |
,, aString |
|
7020 | 4722 |
"concatenate with a newLine in between" |
6965 | 4723 |
|
4724 |
^ (self copyWith:Character cr) , aString |
|
4725 |
||
4726 |
" |
|
7020 | 4727 |
'hello' ,, 'world' |
6965 | 4728 |
" |
23388 | 4729 |
|
4730 |
"Modified (comment): / 25-09-2018 / 10:21:45 / Claus Gittinger" |
|
6965 | 4731 |
! |
4732 |
||
11329 | 4733 |
chopTo:maxLen |
19464 | 4734 |
"if the receiver's size is less or equal to maxLen, return it. |
11329 | 4735 |
Otherwise, return a copy of the receiver, where some characters |
4736 |
in the middle have been removed for a total string length |
|
4737 |
of maxLen." |
|
4738 |
||
4739 |
|sz n1 n2| |
|
4740 |
||
4741 |
(sz := self size) <= maxLen ifTrue:[ ^ self ]. |
|
4742 |
||
4743 |
n1 := n2 := maxLen // 2. |
|
4744 |
maxLen odd ifTrue:[ |
|
20874 | 4745 |
n2 := n1 + 1 |
11329 | 4746 |
]. |
4747 |
^ (self copyFrom:1 to:n1) , (self copyFrom:sz - n2 + 1) |
|
4748 |
||
4749 |
" |
|
4750 |
'12345678901234' chopTo:15 |
|
4751 |
'123456789012345' chopTo:15 |
|
4752 |
'1234567890123456' chopTo:15 |
|
4753 |
'aShortString' chopTo:15 |
|
4754 |
'aVeryLongNameForAStringThatShouldBeShortened' chopTo:15 |
|
4755 |
" |
|
4756 |
||
4757 |
"Modified: / 24-10-2006 / 12:32:01 / cg" |
|
4758 |
! |
|
4759 |
||
4760 |
contractAtBeginningTo:maxLen |
|
19464 | 4761 |
"if the receiver's size is less or equal to maxLen, return it. |
11329 | 4762 |
Otherwise, return a copy of the receiver, where some characters |
4763 |
at the beginning have been replaced by '...' for a total string length |
|
24539 | 4764 |
of maxLen. |
4765 |
Can be used to abbreviate long entries in tables. |
|
4766 |
See also contractAtEndTo:, contractTo: and chopTo:" |
|
11329 | 4767 |
|
4768 |
|sz| |
|
4769 |
||
4770 |
(sz := self size) <= maxLen ifTrue:[ ^ self ]. |
|
4771 |
||
4772 |
^ '...' , (self copyFrom:(sz - (maxLen - 4))) |
|
4773 |
||
4774 |
" |
|
4775 |
'12345678901234' contractAtBeginningTo:15 |
|
4776 |
'123456789012345' contractAtBeginningTo:15 |
|
4777 |
'1234567890123456' contractAtBeginningTo:15 |
|
4778 |
'aShortString' contractAtBeginningTo:15 |
|
4779 |
'aVeryLongNameForAStringThatShouldBeShortened' contractAtBeginningTo:15 |
|
4780 |
" |
|
4781 |
||
4782 |
"Modified: / 24-10-2006 / 12:32:13 / cg" |
|
4783 |
! |
|
4784 |
||
4785 |
contractAtEndTo:maxLen |
|
19464 | 4786 |
"if the receiver's size is less or equal to maxLen, return it. |
11329 | 4787 |
Otherwise, return a copy of the receiver, where some characters |
4788 |
at the end have been replaced by '...' for a total string length |
|
24539 | 4789 |
of maxLen. |
4790 |
Can be used to abbreviate long entries in tables. |
|
4791 |
See also contractAtBeginningTo:, contractTo: and chopTo:" |
|
11329 | 4792 |
|
21559 | 4793 |
(self size <= maxLen) ifTrue:[ ^ self ]. |
11329 | 4794 |
|
4795 |
^ (self copyTo:maxLen-3),'...' |
|
4796 |
||
4797 |
" |
|
4798 |
'12345678901234' contractAtEndTo:15 |
|
4799 |
'123456789012345' contractAtEndTo:15 |
|
4800 |
'1234567890123456' contractAtEndTo:15 |
|
4801 |
'aShortString' contractAtEndTo:15 |
|
4802 |
'aVeryLongNameForAStringThatShouldBeShortened' contractAtEndTo:15 |
|
4803 |
" |
|
4804 |
||
4805 |
"Modified: / 24-10-2006 / 12:32:26 / cg" |
|
21559 | 4806 |
"Modified: / 23-02-2017 / 21:32:21 / mawalch" |
11329 | 4807 |
! |
4808 |
||
4809 |
contractLeftTo:maxLen |
|
19464 | 4810 |
"if the receiver's size is less or equal to maxLen, return it. |
11329 | 4811 |
Otherwise, return a copy of the receiver, where some characters |
4812 |
near the first quarter have been replaced by '...' for a total string length |
|
12177 | 4813 |
of maxLen. |
4814 |
Very similar to contractTo:, but better to abbreviate long filename entries, |
|
24539 | 4815 |
where the right part is of more interest than the left. |
4816 |
See also contractAtBeginningTo:, contractAtEndTo:, contractTo: and chopTo:" |
|
4817 |
||
11329 | 4818 |
|
4819 |
|sz "{ SmallInteger }" |
|
4820 |
halfSize quarterSize "{ SmallInteger }" |
|
4821 |
leftEnd rightEnd rightStart| |
|
4822 |
||
4823 |
(sz := self size) <= maxLen ifTrue:[ ^ self ]. |
|
4824 |
||
4825 |
halfSize := maxLen // 2. |
|
4826 |
quarterSize := maxLen // 4. |
|
4827 |
leftEnd := quarterSize-1. |
|
4828 |
rightEnd := maxLen - leftEnd - 3. |
|
4829 |
rightStart := sz - rightEnd + 1. |
|
4830 |
^ (self copyTo:leftEnd),'...',(self copyFrom:rightStart) |
|
4831 |
||
4832 |
" |
|
12177 | 4833 |
'12345678901234' contractLeftTo:15 |
4834 |
'123456789012345' contractLeftTo:15 |
|
4835 |
'1234567890123456' contractLeftTo:15 |
|
4836 |
'aShortString' contractLeftTo:15 |
|
4837 |
'aVeryLongNameForAStringThatShouldBeShortened' contractLeftTo:15 |
|
11329 | 4838 |
'C:\Dokumente und Einstellungen\cg\work\bosch\dapas\hw_schnittstellen\DAPAS__HpibDLL.st' contractLeftTo:40 |
4839 |
" |
|
4840 |
! |
|
4841 |
||
4842 |
contractTo:maxLen |
|
19464 | 4843 |
"if the receiver's size is less or equal to maxLen, return it. |
11329 | 4844 |
Otherwise, return a copy of the receiver, where some characters |
4845 |
in the middle have been replaced by '...' for a total string length |
|
24539 | 4846 |
of maxLen. |
4847 |
Can be used to abbreviate long entries in tables. |
|
4848 |
See also contractAtBeginningTo:, contractAtEndTo: and chopTo:" |
|
11329 | 4849 |
|
13362 | 4850 |
|sz "{ SmallInteger }" leftSize rightSize| |
11329 | 4851 |
|
4852 |
(sz := self size) <= maxLen ifTrue:[ ^ self ]. |
|
4853 |
||
13362 | 4854 |
rightSize := maxLen // 2. |
4855 |
leftSize := maxLen - rightSize. |
|
13822 | 4856 |
leftSize := leftSize - 2. |
4857 |
rightSize := rightSize - 1. |
|
13362 | 4858 |
^ (self copyTo:leftSize),'...',(self copyFrom:(sz+1-rightSize)) |
4859 |
||
4860 |
" |
|
13882
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
4861 |
'12345678901234' contractTo:15 |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
4862 |
'123456789012345' contractTo:15 |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
4863 |
'1234567890123456' contractTo:15 |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
4864 |
'12345678901234567' contractTo:15 |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
4865 |
'123456789012345678' contractTo:15 |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
4866 |
'aShortString' contractTo:15 |
11329 | 4867 |
'aVeryLongNameForAStringThatShouldBeShortened' contractTo:15 |
4868 |
'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
|
4869 |
('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
|
4870 |
('1234567890123456789012345678901234567' contractTo:29) size |
13362 | 4871 |
" |
4872 |
||
13822 | 4873 |
"Modified (comment): / 24-11-2011 / 19:17:46 / cg" |
11329 | 4874 |
! |
4875 |
||
23061 | 4876 |
copyBetween:string1 and:string2 caseSensitive:caseSensitive |
4877 |
"return the substring between two matching sub-strings. |
|
4878 |
Returns nil, if not both subStrings are present in the receiver." |
|
4879 |
||
4880 |
|idx1 leftIndex rightIndex| |
|
4881 |
||
4882 |
idx1 := self indexOfString:string1 caseSensitive:caseSensitive. |
|
4883 |
idx1 ~~ 0 ifTrue:[ |
|
4884 |
leftIndex := (idx1 + string1 size). |
|
4885 |
rightIndex := self indexOfString:string2 caseSensitive:caseSensitive startingAt:leftIndex. |
|
4886 |
rightIndex ~~ 0 ifTrue:[ |
|
4887 |
^ self copyFrom:leftIndex to:rightIndex-1 |
|
4888 |
] |
|
4889 |
]. |
|
4890 |
^ nil |
|
4891 |
||
4892 |
" |
|
4893 |
'hello funny world' copyBetween:'hello' and:'world' caseSensitive:true -> ' funny ' |
|
4894 |
'helloworld' copyBetween:'hello' and:'world' caseSensitive:true -> '' |
|
4895 |
'helloorld' copyBetween:'hello' and:'world' caseSensitive:true -> nil |
|
4896 |
||
4897 |
'hello funny World' copyBetween:'hello' and:'world' caseSensitive:true -> nil |
|
4898 |
'helloWorld' copyBetween:'hello' and:'world' caseSensitive:true -> nil |
|
4899 |
||
4900 |
'hello funny World' copyBetween:'hello' and:'world' caseSensitive:false -> ' funny ' |
|
4901 |
'helloWorld' copyBetween:'hello' and:'world' caseSensitive:false -> '' |
|
24882 | 4902 |
'bla { foo }' copyBetween:'{' and:'}' caseSensitive:true -> ' foo ' |
23061 | 4903 |
" |
4904 |
||
4905 |
"Created: / 08-06-2018 / 14:50:17 / Claus Gittinger" |
|
4906 |
! |
|
4907 |
||
16202 | 4908 |
copyReplaceAll:oldElement with:newElement |
4909 |
"return a copy of the receiver as a string, where all elements equal to oldElement |
|
4910 |
have been replaced by newElement." |
|
4911 |
||
4912 |
"/ ANSI seems to allow a sequence to be replaced by another sequence, |
|
4913 |
"/ whereas the old ST80 meant replace all occurrences... - sigh. |
|
22231 | 4914 |
oldElement isString ifTrue:[ |
4915 |
newElement isString ifTrue:[ |
|
20874 | 4916 |
^ self copyReplaceString:oldElement withString:newElement. |
4917 |
]. |
|
23552 | 4918 |
self halt:'check if this is legal (trying to replace string by non-string)'. |
16202 | 4919 |
]. |
22231 | 4920 |
newElement isString ifTrue:[ |
23552 | 4921 |
self halt:'check if this is legal (trying to replace non-string by string)'. |
16202 | 4922 |
]. |
16212 | 4923 |
^ super copyReplaceAll:oldElement with:newElement |
23552 | 4924 |
|
4925 |
"Modified: / 03-12-2018 / 16:03:32 / Stefan Vogel" |
|
16202 | 4926 |
! |
4927 |
||
8347 | 4928 |
copyReplaceString:subString withString:newString |
4929 |
"return a copy of the receiver, with all sequences of subString replaced |
|
4930 |
by newString (i.e. slice in the newString in place of the oldString)." |
|
4931 |
||
4932 |
|tmpStream idx idx1| |
|
4933 |
||
15523 | 4934 |
tmpStream := self species writeStream. |
8347 | 4935 |
idx := 1. |
4936 |
[idx ~~ 0] whileTrue:[ |
|
20874 | 4937 |
idx1 := idx. |
4938 |
idx := self indexOfSubCollection:subString startingAt:idx. |
|
4939 |
idx ~~ 0 ifTrue:[ |
|
4940 |
tmpStream nextPutAll:(self copyFrom:idx1 to:idx-1). |
|
4941 |
tmpStream nextPutAll:newString. |
|
4942 |
idx := idx + subString size |
|
4943 |
] |
|
8347 | 4944 |
]. |
4945 |
tmpStream nextPutAll:(self copyFrom:idx1). |
|
4946 |
^ tmpStream contents |
|
4947 |
||
4948 |
" |
|
9306 | 4949 |
'12345678901234567890' copyReplaceString:'123' withString:'OneTwoThree' |
4950 |
'12345678901234567890' copyReplaceString:'123' withString:'*' |
|
4951 |
'12345678901234567890' copyReplaceString:'234' withString:'foo' |
|
22231 | 4952 |
'12345678901234567890' asUnicode16String copyReplaceString:'234' asUnicode16String withString:'foo' |
4953 |
'12345678901234567890' asUnicode16String copyReplaceString:'234' withString:'foo' |
|
4954 |
'12345678901234567890' asUnicode16String copyReplaceString:'234' withString:'foo' asUnicode16String |
|
9306 | 4955 |
|
4956 |
('a string with spaces' copyReplaceAll:$ withAll:' foo ') |
|
20874 | 4957 |
copyReplaceString:'foo' withString:'bar' |
8347 | 4958 |
" |
4959 |
||
4960 |
"Modified: / 31-05-1999 / 12:33:59 / cg" |
|
4961 |
"Created: / 12-05-2004 / 12:00:00 / cg" |
|
4962 |
! |
|
4963 |
||
8040
f10cca439066
copyWith for mixed-width string/character
Claus Gittinger <cg@exept.de>
parents:
8037
diff
changeset
|
4964 |
copyWith:aCharacter |
19464 | 4965 |
"return a new string containing the receiver's characters |
8874 | 4966 |
and the single new character, aCharacter. |
8040
f10cca439066
copyWith for mixed-width string/character
Claus Gittinger <cg@exept.de>
parents:
8037
diff
changeset
|
4967 |
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
|
4968 |
as argument, but equivalent to copy-and-addLast. |
8874 | 4969 |
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
|
4970 |
(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
|
4971 |
|
f10cca439066
copyWith for mixed-width string/character
Claus Gittinger <cg@exept.de>
parents:
8037
diff
changeset
|
4972 |
|sz newString| |
f10cca439066
copyWith for mixed-width string/character
Claus Gittinger <cg@exept.de>
parents:
8037
diff
changeset
|
4973 |
|
f10cca439066
copyWith for mixed-width string/character
Claus Gittinger <cg@exept.de>
parents:
8037
diff
changeset
|
4974 |
aCharacter bitsPerCharacter > self bitsPerCharacter ifTrue:[ |
20874 | 4975 |
sz := self size. |
4976 |
newString := aCharacter stringSpecies new:sz + 1. |
|
4977 |
newString replaceFrom:1 to:sz with:self startingAt:1. |
|
4978 |
newString at:sz+1 put:aCharacter. |
|
4979 |
^ newString. |
|
8040
f10cca439066
copyWith for mixed-width string/character
Claus Gittinger <cg@exept.de>
parents:
8037
diff
changeset
|
4980 |
]. |
f10cca439066
copyWith for mixed-width string/character
Claus Gittinger <cg@exept.de>
parents:
8037
diff
changeset
|
4981 |
^ super copyWith:aCharacter |
17241 | 4982 |
! |
4983 |
||
4984 |
restAfter:keyword withoutSeparators:strip |
|
23392 | 4985 |
"compare the left part of the receiver with keyword, |
4986 |
if it matches return the right rest. |
|
17241 | 4987 |
Finally, if strip is true, remove whiteSpace. |
23392 | 4988 |
If the string does not start with keyword, return nil. |
4989 |
||
17241 | 4990 |
This method is used to match and extract lines of the form: |
20874 | 4991 |
something: rest |
17241 | 4992 |
where we are interested in rest, but only if the receiver string |
4993 |
begins with something. |
|
4994 |
||
4995 |
You may wonder why such a specialized method exists here |
|
4996 |
- this is so common when processing mailboxes, |
|
4997 |
rcs files, nntp/pop3 responses, that is was considered worth |
|
4998 |
a special method here to avoid having the code below a hundred |
|
4999 |
times in variuos places." |
|
5000 |
||
5001 |
|rest| |
|
5002 |
||
5003 |
(self startsWith:keyword) ifTrue:[ |
|
20874 | 5004 |
rest := self copyFrom:(keyword size + 1). |
5005 |
strip ifTrue:[ |
|
5006 |
rest := rest withoutSeparators |
|
5007 |
]. |
|
5008 |
^ rest |
|
17241 | 5009 |
]. |
5010 |
^ nil |
|
5011 |
||
5012 |
" |
|
5013 |
'foo: hello world' restAfter:'foo:' withoutSeparators:true |
|
5014 |
'funny: something' restAfter:'foo:' withoutSeparators:true |
|
5015 |
||
5016 |
'foo: hello world ' restAfter:'foo:' withoutSeparators:true |
|
5017 |
'foo: hello world ' restAfter:'foo:' withoutSeparators:false |
|
5018 |
" |
|
5019 |
||
23392 | 5020 |
"Created: / 25-11-1995 / 11:04:18 / cg" |
5021 |
"Modified (comment): / 27-09-2018 / 10:01:55 / Claus Gittinger" |
|
17241 | 5022 |
! |
5023 |
||
5024 |
splitAtString:subString withoutSeparators:strip |
|
5025 |
"If the receiver is of the form: |
|
20874 | 5026 |
<left><subString><right> |
17241 | 5027 |
return a collection containing left and right only. |
5028 |
If strip is true, remove whiteSpace in the returned substrings." |
|
5029 |
||
5030 |
|idx left right| |
|
5031 |
||
5032 |
(idx := self indexOfSubCollection:subString) ~~ 0 ifTrue:[ |
|
20874 | 5033 |
left := self copyTo:(idx - 1). |
5034 |
right := self copyFrom:(idx + subString size). |
|
5035 |
strip ifTrue:[ |
|
5036 |
left := left withoutSeparators. |
|
5037 |
right := right withoutSeparators. |
|
5038 |
]. |
|
5039 |
^ StringCollection with:left with:right |
|
17241 | 5040 |
]. |
23001 | 5041 |
self proceedableError:'substring not present in receiver'. |
17241 | 5042 |
^ self |
5043 |
||
5044 |
" |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
5045 |
'hello -> world' splitAtString:'->' withoutSeparators:false |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
5046 |
'hello -> world' splitAtString:'->' withoutSeparators:true |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
5047 |
'hello -> ' splitAtString:'->' withoutSeparators:true |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
5048 |
'hello > error' splitAtString:'->' withoutSeparators:true |
17241 | 5049 |
" |
5050 |
||
23001 | 5051 |
"Created: / 25-11-1995 / 11:04:18 / cg" |
5052 |
"Modified: / 24-05-2018 / 14:55:52 / Claus Gittinger" |
|
608 | 5053 |
! ! |
5054 |
||
5055 |
!CharacterArray methodsFor:'displaying'! |
|
5056 |
||
2762 | 5057 |
displayOn:aGC x:x y:y from:start to:stop |
5058 |
"display the receiver on a GC" |
|
5059 |
||
12171 | 5060 |
self displayOn:aGC x:x y:y from:start to:stop opaque:false |
5061 |
||
5062 |
"Modified: 12.5.1996 / 12:49:33 / cg" |
|
5063 |
! |
|
5064 |
||
5065 |
displayOn:aGC x:x y:y from:start to:stop opaque:opaque |
|
5066 |
"display the receiver on a GC" |
|
5067 |
||
2762 | 5068 |
"q&d hack" |
5069 |
||
12177 | 5070 |
(start == 1 and:[stop == self size]) ifTrue:[ |
20874 | 5071 |
self displayOn:aGC x:x y:y opaque:opaque. |
12177 | 5072 |
] ifFalse:[ |
20874 | 5073 |
(self copyFrom:start to:stop) displayOn:aGC x:x y:y opaque:opaque. |
12177 | 5074 |
]. |
2762 | 5075 |
! |
5076 |
||
7082 | 5077 |
displayOn:aGc x:x y:y opaque:opaque |
2762 | 5078 |
"display the receiver in a graphicsContext - this method allows |
5079 |
strings to be used like DisplayObjects." |
|
5080 |
||
5081 |
|s| |
|
5082 |
||
5083 |
s := self string. |
|
7082 | 5084 |
opaque ifTrue:[ |
20874 | 5085 |
aGc displayOpaqueString:s x:x y:y. |
2762 | 5086 |
] ifFalse:[ |
20874 | 5087 |
aGc displayString:s x:x y:y. |
2762 | 5088 |
]. |
1381 | 5089 |
|
5090 |
"Modified: 11.5.1996 / 14:42:48 / cg" |
|
5091 |
! |
|
5092 |
||
2762 | 5093 |
displayOpaqueOn:aGC x:x y:y from:start to:stop |
5094 |
"display the receiver on a GC" |
|
5095 |
||
12171 | 5096 |
self displayOn:aGC x:x y:y from:start to:stop opaque:true |
608 | 5097 |
! ! |
5098 |
||
1375
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
5099 |
!CharacterArray methodsFor:'emphasis'! |
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
5100 |
|
11632
b82e9f3cabd1
new: #actionForAll: #emphasisAtPoint:on:
Stefan Vogel <sv@exept.de>
parents:
11630
diff
changeset
|
5101 |
actionForAll:aBlock |
12326
c67f12213ddb
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
12319
diff
changeset
|
5102 |
"change the action block of all characters. |
c67f12213ddb
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
12319
diff
changeset
|
5103 |
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
|
5104 |
|
24685 | 5105 |
Text isNil ifTrue:[^ self]. |
11632
b82e9f3cabd1
new: #actionForAll: #emphasisAtPoint:on:
Stefan Vogel <sv@exept.de>
parents:
11630
diff
changeset
|
5106 |
^ self asText actionForAll:aBlock |
b82e9f3cabd1
new: #actionForAll: #emphasisAtPoint:on:
Stefan Vogel <sv@exept.de>
parents:
11630
diff
changeset
|
5107 |
! |
b82e9f3cabd1
new: #actionForAll: #emphasisAtPoint:on:
Stefan Vogel <sv@exept.de>
parents:
11630
diff
changeset
|
5108 |
|
5732 | 5109 |
allBold |
12326
c67f12213ddb
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
12319
diff
changeset
|
5110 |
"return a text object representing the receiver, but all boldified" |
5732 | 5111 |
|
8874 | 5112 |
"this test allows for small non-gui apps to be built without libbasic2 (where Text is)" |
6187 | 5113 |
Text isNil ifTrue:[^ self]. |
5732 | 5114 |
^ self asText allBold |
5115 |
||
5116 |
" |
|
5117 |
Transcript showCR:'hello' asText allBold |
|
5118 |
Transcript showCR:'hello' allBold |
|
5119 |
" |
|
5120 |
! |
|
5121 |
||
24684 | 5122 |
allGray |
24686 | 5123 |
"return a text object representing the receiver, but in gray color. |
5124 |
This is used so often, that it's worth a utility here" |
|
24684 | 5125 |
|
24685 | 5126 |
Text isNil ifTrue:[^ self]. |
24684 | 5127 |
^ self withColor:Color gray |
5128 |
||
5129 |
" |
|
5130 |
Transcript showCR:'hello' asText allBold allGray |
|
24686 | 5131 |
Transcript showCR:'hello' allBold allGray |
24684 | 5132 |
Transcript showCR:'hello' allGray |
5133 |
" |
|
5134 |
! |
|
5135 |
||
5898
7bc4f1593b4e
allItalic now understood by CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5848
diff
changeset
|
5136 |
allItalic |
12326
c67f12213ddb
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
12319
diff
changeset
|
5137 |
"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
|
5138 |
|
8874 | 5139 |
"this test allows for small non-gui apps to be built without libbasic2 (where Text is)" |
6187 | 5140 |
Text isNil ifTrue:[^ self]. |
5898
7bc4f1593b4e
allItalic now understood by CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5848
diff
changeset
|
5141 |
^ self asText allItalic |
7bc4f1593b4e
allItalic now understood by CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5848
diff
changeset
|
5142 |
|
7bc4f1593b4e
allItalic now understood by CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5848
diff
changeset
|
5143 |
" |
7bc4f1593b4e
allItalic now understood by CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5848
diff
changeset
|
5144 |
Transcript showCR:'hello' asText allItalic |
7bc4f1593b4e
allItalic now understood by CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5848
diff
changeset
|
5145 |
Transcript showCR:'hello' allItalic |
7bc4f1593b4e
allItalic now understood by CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5848
diff
changeset
|
5146 |
" |
7bc4f1593b4e
allItalic now understood by CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5848
diff
changeset
|
5147 |
! |
7bc4f1593b4e
allItalic now understood by CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5848
diff
changeset
|
5148 |
|
23903 | 5149 |
allNonBold |
5150 |
"make all characters non-bold; |
|
5151 |
I already have no emphasis, so the receiver string is returned" |
|
5152 |
||
5153 |
^ self |
|
5154 |
||
5155 |
"Created: / 13-03-2019 / 21:02:45 / Claus Gittinger" |
|
5156 |
! |
|
5157 |
||
24686 | 5158 |
allRed |
5159 |
"return a text object representing the receiver, but in red color. |
|
5160 |
This is used so often, that it's worth a utility here" |
|
5161 |
||
5162 |
Text isNil ifTrue:[^ self]. |
|
5163 |
^ self withColor:Color red |
|
5164 |
||
5165 |
" |
|
5166 |
Transcript showCR:'hello' asText allBold allRed |
|
5167 |
Transcript showCR:'hello' allBold allRed |
|
5168 |
Transcript showCR:'hello' allRed |
|
5169 |
" |
|
5170 |
! |
|
5171 |
||
11345 | 5172 |
allStrikedOut |
12326
c67f12213ddb
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
12319
diff
changeset
|
5173 |
"return a text object representing the receiver, but all in strikeout" |
11345 | 5174 |
|
5175 |
"this test allows for small non-gui apps to be built without libbasic2 (where Text is)" |
|
5176 |
Text isNil ifTrue:[^ self]. |
|
5177 |
^ self asText allStrikedOut |
|
5178 |
||
5179 |
" |
|
5180 |
Transcript showCR:'hello' asText allStrikedOut |
|
5181 |
Transcript showCR:'hello' allStrikedOut |
|
5182 |
" |
|
5183 |
! |
|
5184 |
||
10862 | 5185 |
allUnderlined |
12326
c67f12213ddb
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
12319
diff
changeset
|
5186 |
"return a text object representing the receiver, but all with underline" |
10862 | 5187 |
|
5188 |
"this test allows for small non-gui apps to be built without libbasic2 (where Text is)" |
|
5189 |
Text isNil ifTrue:[^ self]. |
|
5190 |
^ self asText allUnderlined |
|
5191 |
||
5192 |
" |
|
5193 |
Transcript showCR:'hello' asText allUnderlined |
|
5194 |
Transcript showCR:'hello' allUnderlined |
|
5195 |
" |
|
5196 |
! |
|
5197 |
||
19774 | 5198 |
asActionLinkTo:aBlock |
5199 |
"change the action block of all characters and colorize as a link. |
|
5200 |
Some widgets use this like a href if clicked onto the text |
|
5201 |
(for example, the system-browser's info at the bottom is such a widget)." |
|
5202 |
||
5203 |
"/ the blue may become a user-setting some time later.. |
|
5204 |
^ (self actionForAll:aBlock) withColor:(Color blue) |
|
5205 |
! |
|
5206 |
||
25217 | 5207 |
asActionLinkTo:aBlock info:tooltipMessage |
5208 |
"change the action block of all characters and colorize as a link. |
|
5209 |
Some widgets use this like a href if clicked onto the text |
|
5210 |
(for example, the system-browser's info at the bottom is such a widget). |
|
5211 |
Caveat: currently the tooltipMessage is ignored (there is no mechanism for that, yet)" |
|
5212 |
||
25218 | 5213 |
|a| |
5214 |
a := Explainer::ActionWithInfo block:aBlock info:tooltipMessage. |
|
5215 |
^ (self actionForAll:a) withColor:(Color blue) |
|
25217 | 5216 |
! |
5217 |
||
6228 | 5218 |
colorizeAllWith:aColor |
12326
c67f12213ddb
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
12319
diff
changeset
|
5219 |
"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
|
5220 |
|
c67f12213ddb
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
12319
diff
changeset
|
5221 |
"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
|
5222 |
Text isNil ifTrue:[^ self]. |
6228 | 5223 |
^ self asText colorizeAllWith:aColor |
5224 |
||
5225 |
" |
|
5226 |
Transcript showCR:('hello' colorizeAllWith:Color red) |
|
5227 |
Transcript showCR:('world' colorizeAllWith:Color green darkened) |
|
5228 |
" |
|
5229 |
! |
|
5230 |
||
12330
f9b57b0b8a8b
added: #colorizeAllWith:on:
Claus Gittinger <cg@exept.de>
parents:
12326
diff
changeset
|
5231 |
colorizeAllWith:fgColor on:bgColor |
f9b57b0b8a8b
added: #colorizeAllWith:on:
Claus Gittinger <cg@exept.de>
parents:
12326
diff
changeset
|
5232 |
"return a text object representing the receiver, but all colorized with |
f9b57b0b8a8b
added: #colorizeAllWith:on:
Claus Gittinger <cg@exept.de>
parents:
12326
diff
changeset
|
5233 |
both fg and background color" |
f9b57b0b8a8b
added: #colorizeAllWith:on:
Claus Gittinger <cg@exept.de>
parents:
12326
diff
changeset
|
5234 |
|
f9b57b0b8a8b
added: #colorizeAllWith:on:
Claus Gittinger <cg@exept.de>
parents:
12326
diff
changeset
|
5235 |
"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
|
5236 |
Text isNil ifTrue:[^ self]. |
f9b57b0b8a8b
added: #colorizeAllWith:on:
Claus Gittinger <cg@exept.de>
parents:
12326
diff
changeset
|
5237 |
^ self asText colorizeAllWith:fgColor on:bgColor |
f9b57b0b8a8b
added: #colorizeAllWith:on:
Claus Gittinger <cg@exept.de>
parents:
12326
diff
changeset
|
5238 |
|
f9b57b0b8a8b
added: #colorizeAllWith:on:
Claus Gittinger <cg@exept.de>
parents:
12326
diff
changeset
|
5239 |
" |
f9b57b0b8a8b
added: #colorizeAllWith:on:
Claus Gittinger <cg@exept.de>
parents:
12326
diff
changeset
|
5240 |
Transcript showCR:('hello' colorizeAllWith:Color red on:Color yellow) |
f9b57b0b8a8b
added: #colorizeAllWith:on:
Claus Gittinger <cg@exept.de>
parents:
12326
diff
changeset
|
5241 |
Transcript showCR:('world' colorizeAllWith:Color red) |
f9b57b0b8a8b
added: #colorizeAllWith:on:
Claus Gittinger <cg@exept.de>
parents:
12326
diff
changeset
|
5242 |
" |
f9b57b0b8a8b
added: #colorizeAllWith:on:
Claus Gittinger <cg@exept.de>
parents:
12326
diff
changeset
|
5243 |
! |
f9b57b0b8a8b
added: #colorizeAllWith:on:
Claus Gittinger <cg@exept.de>
parents:
12326
diff
changeset
|
5244 |
|
1396
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
5245 |
emphasis |
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
5246 |
"return the emphasis. |
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
5247 |
Since characterArrays do not hold any emphasis information, |
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
5248 |
nil (no emphasis) is returned here." |
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
5249 |
|
23060 | 5250 |
^ RunArray new:(self size) withAll:nil |
5251 |
||
5252 |
"Created: / 14-05-1996 / 13:58:58 / cg" |
|
5253 |
"Modified (format): / 08-06-2018 / 12:01:59 / Claus Gittinger" |
|
1396
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
5254 |
! |
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
5255 |
|
6655 | 5256 |
emphasis:emphasisCollection |
5257 |
^ self asText emphasis:emphasisCollection |
|
5258 |
||
5259 |
" |
|
5260 |
Transcript showCR:('hello' emphasis:#(bold bold bold bold bold)) |
|
5261 |
" |
|
5262 |
! |
|
5263 |
||
1375
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
5264 |
emphasisAt:characterIndex |
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
5265 |
"return the emphasis at some index. |
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
5266 |
Since characterArrays do not hold any emphasis information, |
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
5267 |
nil (no emphasis) is returned here." |
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
5268 |
|
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
5269 |
^ nil |
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
5270 |
|
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
5271 |
"Created: 11.5.1996 / 14:13:27 / cg" |
1396
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
5272 |
! |
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
5273 |
|
25219 | 5274 |
emphasisAtX:xOffset on:aGC |
5275 |
"return the emphasis at a given x offset, or nil if there is none" |
|
11632
b82e9f3cabd1
new: #actionForAll: #emphasisAtPoint:on:
Stefan Vogel <sv@exept.de>
parents:
11630
diff
changeset
|
5276 |
|
b82e9f3cabd1
new: #actionForAll: #emphasisAtPoint:on:
Stefan Vogel <sv@exept.de>
parents:
11630
diff
changeset
|
5277 |
^ nil |
b82e9f3cabd1
new: #actionForAll: #emphasisAtPoint:on:
Stefan Vogel <sv@exept.de>
parents:
11630
diff
changeset
|
5278 |
! |
b82e9f3cabd1
new: #actionForAll: #emphasisAtPoint:on:
Stefan Vogel <sv@exept.de>
parents:
11630
diff
changeset
|
5279 |
|
1396
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
5280 |
emphasisCollection |
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
5281 |
"return the emphasis. |
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
5282 |
Since characterArrays do not hold any emphasis information, |
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
5283 |
nil (no emphasis) is returned here." |
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
5284 |
|
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
5285 |
^ RunArray new:(self size) |
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
5286 |
|
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
5287 |
"Created: 14.5.1996 / 13:58:58 / cg" |
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
5288 |
"Modified: 14.5.1996 / 15:02:29 / cg" |
3404 | 5289 |
! |
5290 |
||
11919 | 5291 |
emphasiseFrom:start to:stop with:newEmphasis |
5292 |
"set to the emphasis within some range. return the receiver" |
|
5293 |
||
5294 |
^ self asText emphasiseFrom:start to:stop with:newEmphasis |
|
5295 |
||
5296 |
" |
|
12177 | 5297 |
'hello' emphasiseFrom:2 with:#italic |
11919 | 5298 |
|
5299 |
Transcript showCR:('hello' emphasiseFrom:2 with:#italic) |
|
5300 |
" |
|
5301 |
! |
|
5302 |
||
5303 |
emphasiseFrom:start with:newEmphasis |
|
5304 |
"set to the emphasis within some range. return the receiver" |
|
5305 |
||
5306 |
^ self emphasiseFrom:start to:(self size) with:newEmphasis |
|
5307 |
||
5308 |
" |
|
12177 | 5309 |
'hello' emphasiseFrom:2 with:#italic |
11919 | 5310 |
|
5311 |
Transcript showCR:('hello' emphasiseFrom:2 with:#italic) |
|
5312 |
" |
|
5313 |
! |
|
5314 |
||
3404 | 5315 |
emphasizeAllWith:emphasis |
12326
c67f12213ddb
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
12319
diff
changeset
|
5316 |
"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
|
5317 |
|
c67f12213ddb
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
12319
diff
changeset
|
5318 |
"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
|
5319 |
Text isNil ifTrue:[^ self]. |
3404 | 5320 |
^ self asText emphasizeAllWith:emphasis |
5321 |
||
5322 |
" |
|
5323 |
Transcript showCR:('hello' emphasizeAllWith:#bold) |
|
3599 | 5324 |
Transcript showCR:('hello' emphasizeAllWith:(#color -> Color red)) |
5325 |
Transcript showCR:('hello' emphasizeAllWith:(#color -> Color red)) |
|
5326 |
" |
|
5327 |
||
5328 |
"Modified: / 17.6.1998 / 12:51:44 / cg" |
|
5097 | 5329 |
! |
5330 |
||
5331 |
makeSelectorBoldIn:aClass |
|
14050 | 5332 |
"the receiver represents some source code for a method in aClass. |
5097 | 5333 |
Change myself to boldify the selector. |
14050 | 5334 |
Not yet implemented (could easily use the syntaxHighlighter for this ...). |
5335 |
For protocol compatibility with other smalltalks" |
|
5097 | 5336 |
|
5337 |
^ self |
|
5338 |
||
14050 | 5339 |
"Created: / 13-12-1999 / 21:49:24 / cg" |
5340 |
! |
|
5341 |
||
19762 | 5342 |
withColor:aColorOrColorSymbol |
20697 | 5343 |
"return a text object representing the receiver, but all colorized. |
5344 |
Usage of a colorSymbol is considered bad style (provided for backward compatibility); |
|
5345 |
please change to pass a proper color |
|
5346 |
(makes it easier to find color uses)" |
|
5347 |
||
5348 |
|color| |
|
5349 |
||
19762 | 5350 |
aColorOrColorSymbol isSymbol ifTrue:[ |
20697 | 5351 |
color := (Color perform:aColorOrColorSymbol) |
5352 |
] ifFalse:[ |
|
5353 |
color := aColorOrColorSymbol |
|
5354 |
]. |
|
5355 |
^ self colorizeAllWith:color |
|
19762 | 5356 |
|
5357 |
" |
|
5358 |
Transcript showCR:('hello' withColor:#red) |
|
20697 | 5359 |
Transcript showCR:('world' withColor:#blue) |
16697 | 5360 |
Transcript showCR:('hello' withColor:Color red) |
5361 |
Transcript showCR:('world' withColor:Color green darkened) |
|
5362 |
" |
|
5363 |
! |
|
5364 |
||
22911 | 5365 |
withColor:foregroundColorOrColorSymbol on:backgroundColorOrColorSymbol |
5366 |
"return a text object representing the receiver, but all colorized with both fg and bg. |
|
5367 |
Usage of a colorSymbol is considered bad style (provided for backward compatibility); |
|
5368 |
please change to pass a proper color |
|
5369 |
(makes it easier to find color uses)" |
|
5370 |
||
5371 |
|fgColor bgColor| |
|
5372 |
||
5373 |
foregroundColorOrColorSymbol isSymbol ifTrue:[ |
|
5374 |
fgColor := (Color perform:foregroundColorOrColorSymbol) |
|
5375 |
] ifFalse:[ |
|
5376 |
fgColor := foregroundColorOrColorSymbol |
|
5377 |
]. |
|
5378 |
backgroundColorOrColorSymbol isSymbol ifTrue:[ |
|
5379 |
bgColor := (Color perform:backgroundColorOrColorSymbol) |
|
5380 |
] ifFalse:[ |
|
5381 |
bgColor := backgroundColorOrColorSymbol |
|
5382 |
]. |
|
5383 |
^ self colorizeAllWith:fgColor on:bgColor |
|
5384 |
||
5385 |
" |
|
5386 |
Transcript showCR:('hello' withColor:#red on:#blue) |
|
5387 |
Transcript showCR:('hello' withColor:Color red on:Color blue) |
|
5388 |
" |
|
5389 |
! |
|
5390 |
||
14050 | 5391 |
withoutAnyColorEmphasis |
5392 |
"for protocol compatibility with Text" |
|
5393 |
||
5394 |
^ self |
|
5395 |
||
5396 |
"Created: / 06-03-2012 / 18:15:38 / cg" |
|
6653 | 5397 |
! |
5398 |
||
12396 | 5399 |
withoutBackgroundColorEmphasis |
14050 | 5400 |
"for protocol compatibility with Text" |
5401 |
||
12396 | 5402 |
^ self |
14050 | 5403 |
|
5404 |
"Modified (comment): / 06-03-2012 / 18:14:27 / cg" |
|
12396 | 5405 |
! |
5406 |
||
23903 | 5407 |
withoutEmphasis |
5408 |
"for protocol compatibility with Text" |
|
5409 |
||
5410 |
^ self |
|
5411 |
||
5412 |
"Created: / 13-03-2019 / 21:03:41 / Claus Gittinger" |
|
5413 |
! |
|
5414 |
||
6653 | 5415 |
withoutEmphasis:emphasisToRemove |
14050 | 5416 |
"for protocol compatibility with Text" |
5417 |
||
6653 | 5418 |
^ self |
14050 | 5419 |
|
5420 |
"Modified (comment): / 06-03-2012 / 18:14:29 / cg" |
|
12396 | 5421 |
! |
5422 |
||
5423 |
withoutForegroundColorEmphasis |
|
14050 | 5424 |
"for protocol compatibility with Text" |
5425 |
||
12396 | 5426 |
^ self |
14050 | 5427 |
|
5428 |
"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
|
5429 |
! ! |
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
5430 |
|
7265 | 5431 |
!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
|
5432 |
|
20013 | 5433 |
asDenormalizedUnicodeString |
5434 |
"return a new string containing the same characters, as a denormalized Unicode string. |
|
5435 |
This replaces diacritical chars (umlauts, accented chars etc) by |
|
5436 |
a sequence with combination characters. |
|
5437 |
(i.e. a plain character followed by a combining diacritical in the 0x03xx range)" |
|
5438 |
||
20014 | 5439 |
|map outStream mapChar| |
20013 | 5440 |
|
22406 | 5441 |
self containsNon7BitAscii ifFalse:[ |
5442 |
^ self "/ I cannot contain any diacritical chars |
|
5443 |
]. |
|
5444 |
||
20013 | 5445 |
map := self class unicodeDenormalizationMap. |
22406 | 5446 |
mapChar := [:char | |
20014 | 5447 |
|mappedChars| |
5448 |
||
5449 |
(mappedChars := map at:char ifAbsent:nil) notNil ifTrue:[ |
|
5450 |
mappedChars do:mapChar. |
|
5451 |
] ifFalse:[ |
|
5452 |
outStream nextPut:char. |
|
5453 |
]. |
|
5454 |
]. |
|
20013 | 5455 |
|
20014 | 5456 |
outStream := WriteStream on:(Unicode16String new:self size). |
5457 |
self do:mapChar. |
|
20013 | 5458 |
^ outStream contents asSingleByteStringIfPossible. |
5459 |
||
5460 |
" |
|
5461 |
'Ö' asDenormalizedUnicodeString |
|
5462 |
'aÖÄx' asDenormalizedUnicodeString |
|
5463 |
'abc' asDenormalizedUnicodeString |
|
22406 | 5464 |
'ṩ' asDenormalizedUnicodeString |
5465 |
" |
|
5466 |
||
5467 |
"Modified (format): / 02-01-2018 / 18:52:33 / stefan" |
|
20013 | 5468 |
! |
5469 |
||
20011 | 5470 |
asNormalizedUnicodeString |
20010 | 5471 |
"return a new string containing the same characters, as a normalized Unicode string. |
5472 |
This replaces combination characters by corresponding single characters. |
|
20863 | 5473 |
(i.e. diaresis and other combining diacriticals in the 0x03xx range). |
5474 |
Caveat: |
|
5475 |
possibly incomplete: only COMBINING_DIACRITICAL_MARKS are cared for. |
|
5476 |
Does not care for COMBINING_DIACRITICAL_MARKS_EXTENDED |
|
20864 | 5477 |
and COMBINING_DIACRITICAL_MARKS_SUPPLEMENT. |
5478 |
However; those are used for German dialectology, ancient Greek and other similar |
|
5479 |
exotic uses. Probably noone will ever even notice that they are missing..." |
|
20010 | 5480 |
|
5481 |
|outStream prevChar map mapEntries mappedChar| |
|
5482 |
||
5483 |
map := self class unicodeNormalizationMap. |
|
20017 | 5484 |
|
20010 | 5485 |
self bitsPerCharacter <= 8 ifTrue:[^ self]. "/ I cannot contain any overtypes |
20017 | 5486 |
(self contains:[:ch | ch unicodeBlock == #'COMBINING_DIACRITICAL_MARKS']) ifFalse:[^ self]. "/ I do not contain any overtypes |
20010 | 5487 |
|
5488 |
outStream := WriteStream on:(self species new:self size). |
|
5489 |
self do:[:char | |
|
20017 | 5490 |
((char unicodeBlock == #'COMBINING_DIACRITICAL_MARKS') |
20010 | 5491 |
and:[ (mapEntries := map at:char ifAbsent:nil) notNil |
5492 |
and:[ (mappedChar := mapEntries at:prevChar ifAbsent:nil) notNil ]]) ifTrue:[ |
|
20014 | 5493 |
prevChar := mappedChar. |
20010 | 5494 |
] ifFalse:[ |
5495 |
prevChar notNil ifTrue:[ outStream nextPut:prevChar]. |
|
5496 |
prevChar := char |
|
5497 |
]. |
|
5498 |
]. |
|
20011 | 5499 |
prevChar notNil ifTrue:[ outStream nextPut:prevChar]. |
20010 | 5500 |
^ outStream contents asSingleByteStringIfPossible. |
5501 |
||
5502 |
" |
|
5503 |
self unicodeNormalizationMap |
|
20012 | 5504 |
('O' , (Character value:16r0308)) asNormalizedUnicodeString -> 'Ö' |
20010 | 5505 |
('O' , (Character value:16r0308) , |
20012 | 5506 |
'A' , (Character value:16r0308)) asNormalizedUnicodeString -> 'ÖÄ' |
20014 | 5507 |
('s' , (Character value:16r0323) , (Character value:16r0307)) asNormalizedUnicodeString -> 'ṩ' |
5508 |
('s' , (Character value:16r0307) , (Character value:16r0323)) asNormalizedUnicodeString -> 'ṩ' |
|
20010 | 5509 |
" |
5510 |
! |
|
5511 |
||
12767
17d2c7268065
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12679
diff
changeset
|
5512 |
decodeAsLiteralArray |
17d2c7268065
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12679
diff
changeset
|
5513 |
"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
|
5514 |
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
|
5515 |
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
|
5516 |
|
17d2c7268065
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12679
diff
changeset
|
5517 |
^ self |
17d2c7268065
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12679
diff
changeset
|
5518 |
! |
17d2c7268065
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12679
diff
changeset
|
5519 |
|
991
f0b45a4b82d2
first attempt in encoding/decoding for different character representations (mac/msdos etc.)
Claus Gittinger <cg@exept.de>
parents:
986
diff
changeset
|
5520 |
decodeFrom:encodingSymbol |
8874 | 5521 |
"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
|
5522 |
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
|
5523 |
|
ca774158b317
code cleanup (removed old obsolete encoding stuff)
Claus Gittinger <cg@exept.de>
parents:
8102
diff
changeset
|
5524 |
|myEncoding encoder| |
7950 | 5525 |
|
5526 |
encodingSymbol isNil ifTrue:[^ self]. |
|
5527 |
myEncoding := self encoding. |
|
5528 |
encodingSymbol == myEncoding ifTrue:[^ self]. |
|
5529 |
||
22451 | 5530 |
encoder := CharacterEncoder encoderToEncodeFrom:myEncoding into:encodingSymbol. |
8124
ca774158b317
code cleanup (removed old obsolete encoding stuff)
Claus Gittinger <cg@exept.de>
parents:
8102
diff
changeset
|
5531 |
encoder isNil ifTrue:[^ self]. |
ca774158b317
code cleanup (removed old obsolete encoding stuff)
Claus Gittinger <cg@exept.de>
parents:
8102
diff
changeset
|
5532 |
^ encoder decodeString:self. |
22451 | 5533 |
|
5534 |
"Modified: / 17-01-2018 / 17:34:43 / stefan" |
|
991
f0b45a4b82d2
first attempt in encoding/decoding for different character representations (mac/msdos etc.)
Claus Gittinger <cg@exept.de>
parents:
986
diff
changeset
|
5535 |
! |
f0b45a4b82d2
first attempt in encoding/decoding for different character representations (mac/msdos etc.)
Claus Gittinger <cg@exept.de>
parents:
986
diff
changeset
|
5536 |
|
9192 | 5537 |
encodeFrom:oldEncoding into:newEncoding |
5538 |
^ CharacterEncoder encodeString:self from:oldEncoding into:newEncoding |
|
14405
1d0d99a95bba
comment/format in:7 methods
Claus Gittinger <cg@exept.de>
parents:
14168
diff
changeset
|
5539 |
|
1d0d99a95bba
comment/format in:7 methods
Claus Gittinger <cg@exept.de>
parents:
14168
diff
changeset
|
5540 |
" |
17600 | 5541 |
'äüö' encodeFrom:#iso8859 into:#utf8 |
5542 |
('äüö' encodeFrom:#iso8859 into:#utf8) encodeFrom:#utf8 into:#unicode |
|
14405
1d0d99a95bba
comment/format in:7 methods
Claus Gittinger <cg@exept.de>
parents:
14168
diff
changeset
|
5543 |
" |
9192 | 5544 |
! |
5545 |
||
22451 | 5546 |
encodeInto:newEncoding |
5547 |
^ CharacterEncoder encodeString:self from:self encoding into:newEncoding |
|
5548 |
||
5549 |
" |
|
5550 |
'äüö' encodeInto:#utf8 |
|
5551 |
('äüö' encodeInto:#utf8) decodeFrom:#utf8 |
|
5552 |
" |
|
5553 |
||
5554 |
"Created: / 17-01-2018 / 17:43:20 / stefan" |
|
5555 |
! |
|
5556 |
||
9192 | 5557 |
rot13 |
8874 | 5558 |
"Usenet: from `rotate alphabet 13 places'] |
6835
bdb2cb03200c
added utf8Decode with 8-bit chars only
Claus Gittinger <cg@exept.de>
parents:
6834
diff
changeset
|
5559 |
The simple Caesar-cypher encryption that replaces each English |
8874 | 5560 |
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
|
5561 |
so that 'The butler did it!!' becomes 'Gur ohgyre qvq vg!!' |
8874 | 5562 |
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
|
5563 |
It is used to enclose the text in a sealed wrapper that the reader must choose |
8874 | 5564 |
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
|
5565 |
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
|
5566 |
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
|
5567 |
|
11865 | 5568 |
^ self rot:13 |
8874 | 5569 |
|
5570 |
" |
|
5571 |
'hello world' rot13 |
|
5572 |
'hello world' rot13 rot13 |
|
6835
bdb2cb03200c
added utf8Decode with 8-bit chars only
Claus Gittinger <cg@exept.de>
parents:
6834
diff
changeset
|
5573 |
" |
bdb2cb03200c
added utf8Decode with 8-bit chars only
Claus Gittinger <cg@exept.de>
parents:
6834
diff
changeset
|
5574 |
! |
bdb2cb03200c
added utf8Decode with 8-bit chars only
Claus Gittinger <cg@exept.de>
parents:
6834
diff
changeset
|
5575 |
|
11865 | 5576 |
rot:n |
5577 |
"Usenet: from `rotate alphabet N places'] |
|
5578 |
The simple Caesar-cypher encryption that replaces each English |
|
5579 |
letter with the one N places forward or back along the alphabet, |
|
5580 |
so that 'The butler did it!!' becomes 'Gur ohgyre qvq vg!!' by rot 13 |
|
5581 |
Most Usenet news reading and posting programs include a rot13 feature. |
|
5582 |
It is used to enclose the text in a sealed wrapper that the reader must choose |
|
5583 |
to open -- e.g., for posting things that might offend some readers, or spoilers. |
|
5584 |
A major advantage of rot13 over rot(N) for other N is that it |
|
5585 |
is self-inverse, so the same code can be used for encoding and decoding." |
|
5586 |
||
5587 |
^ self species |
|
20874 | 5588 |
streamContents:[:aStream | |
5589 |
self do:[:char | |
|
5590 |
aStream nextPut:(char rot:n) ]] |
|
11865 | 5591 |
|
5592 |
" |
|
5593 |
'hello world' rot:13 |
|
5594 |
('hello world' rot:13) rot:13 |
|
5595 |
" |
|
5596 |
! |
|
5597 |
||
15656 | 5598 |
utf16Encoded |
5599 |
"Return my UTF-16 representation as a new TwoByteString" |
|
5600 |
||
5601 |
|s| |
|
5602 |
||
5603 |
s := WriteStream on:(TwoByteString uninitializedNew:self size). |
|
5604 |
s nextPutAllUtf16:self. |
|
5605 |
^ s contents |
|
5606 |
||
5607 |
" |
|
5608 |
'abcde1234' utf16Encoded |
|
17600 | 5609 |
'abcdeäöüß' utf16Encoded |
15656 | 5610 |
" |
5611 |
||
5612 |
"Modified: / 11-05-2010 / 19:12:37 / cg" |
|
5613 |
! |
|
5614 |
||
9192 | 5615 |
utf8Encoded |
19837 | 5616 |
"return the UTF-8 representation of a Unicode string. |
5617 |
The resulting string is only useful to be stored on some external file, |
|
21318 | 5618 |
or sent to a communaction channel. |
5619 |
Not for being used inside ST/X." |
|
19837 | 5620 |
|
5621 |
|string stream| |
|
17625 | 5622 |
|
5623 |
string := self string. |
|
19837 | 5624 |
|
5625 |
"/ avoid creation of new strings if possible |
|
5626 |
string containsNon7BitAscii ifFalse:[ |
|
5627 |
^ string asSingleByteString |
|
5628 |
]. |
|
5629 |
||
5630 |
"make it size 2 for 1-byte strings" |
|
5631 |
stream := WriteStream on:(String uninitializedNew:(1 + (string size * 3 // 2))). |
|
5632 |
string utf8EncodedOn:stream. |
|
5633 |
^ stream contents. |
|
5634 |
||
5635 |
" |
|
5636 |
'hello' utf8Encoded asByteArray #[104 101 108 108 111] |
|
5637 |
(Character value:16r40) asString utf8Encoded asByteArray #[64] |
|
5638 |
(Character value:16r7F) asString utf8Encoded asByteArray #[127] |
|
5639 |
(Character value:16r80) asString utf8Encoded asByteArray #[194 128] |
|
5640 |
(Character value:16rFF) asString utf8Encoded asByteArray #[195 191] |
|
5641 |
(Character value:16r100) asString utf8Encoded asByteArray #[196 128] |
|
5642 |
(Character value:16r200) asString utf8Encoded asByteArray #[200 128] |
|
5643 |
(Character value:16r400) asString utf8Encoded asByteArray #[208 128] |
|
5644 |
(Character value:16r800) asString utf8Encoded asByteArray #[224 160 128] |
|
5645 |
(Character value:16r1000) asString utf8Encoded asByteArray #[225 128 128] |
|
5646 |
(Character value:16r2000) asString utf8Encoded asByteArray #[226 128 128] |
|
5647 |
(Character value:16r4000) asString utf8Encoded asByteArray #[228 128 128] |
|
5648 |
(Character value:16r8000) asString utf8Encoded asByteArray #[232 128 128] |
|
5649 |
(Character value:16rFFFF) asString utf8Encoded asByteArray #[239 191 191] |
|
21318 | 5650 |
(Character value:16r1FFFF) asString utf8Encoded asByteArray #[240 159 191 191] |
5651 |
(Character value:16r3FFFF) asString utf8Encoded asByteArray #[240 191 191 191] |
|
5652 |
(Character value:16rFFFFF) asString utf8Encoded asByteArray #[243 191 191 191] |
|
5653 |
(Character value:16r3FFFFF) asString utf8Encoded asByteArray #[248 143 191 191 191] |
|
19837 | 5654 |
|
18597 | 5655 |
'abcde1234' asUnicode32String utf8Encoded |
5656 |
'abcdeäöüß' asUnicode32String utf8Encoded |
|
12925
5c0a9a633ca1
comment/format in: #utf8Encoded
Claus Gittinger <cg@exept.de>
parents:
12912
diff
changeset
|
5657 |
" |
17625 | 5658 |
! |
5659 |
||
5660 |
utf8EncodedOn:aStream |
|
21468 | 5661 |
"write the UTF-8 representation of myself to aStream." |
5662 |
||
5663 |
aStream nextPutAllUtf8:self. |
|
5664 |
||
5665 |
"Modified: / 16-02-2017 / 16:51:32 / stefan" |
|
9192 | 5666 |
! ! |
5667 |
||
22566 | 5668 |
!CharacterArray methodsFor:'filling and replacing'! |
5669 |
||
5670 |
clearContents |
|
5671 |
"to be used with cryptographic keys, to wipe their contents after use" |
|
5672 |
||
5673 |
self atAllPut:(Character value:0) |
|
5674 |
! ! |
|
22451 | 5675 |
|
5676 |
||
25217 | 5677 |
|
5678 |
||
12111 | 5679 |
!CharacterArray methodsFor:'matching - glob expressions'! |
5680 |
||
5681 |
compoundMatch:aString |
|
5682 |
"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
|
5683 |
consisting of multiple simple GLOB patterns, separated by semicolons. |
12111 | 5684 |
This is usable with fileName pattern fields. |
23791 | 5685 |
This is a case sensitive match: lower/uppercase are considered different. |
12111 | 5686 |
|
5687 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20874 | 5688 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5689 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 5690 |
NOTICE: the receiver is the match pattern" |
5691 |
||
15890 | 5692 |
^ self compoundMatch:aString caseSensitive:true withoutSeparators:false |
12111 | 5693 |
|
5694 |
" |
|
5695 |
'f*' match:'foo' |
|
5696 |
'b*' match:'foo' |
|
5697 |
'f*;b*' match:'foo' |
|
5698 |
'f*;b*' match:'bar' |
|
5699 |
'f*;b*' compoundMatch:'foo' |
|
5700 |
'f*;b*' compoundMatch:'bar' |
|
23791 | 5701 |
'*.png;*.gif' compoundMatch:'bar.jpg' |
5702 |
'*.png;*.gif' compoundMatch:'bar.gif' |
|
5703 |
" |
|
5704 |
||
5705 |
"Modified: / 30-01-1998 / 11:40:18 / stefan" |
|
5706 |
"Modified: / 16-12-1999 / 01:22:08 / cg" |
|
5707 |
"Modified (comment): / 02-03-2019 / 12:21:33 / Claus Gittinger" |
|
12111 | 5708 |
! |
5709 |
||
15734 | 5710 |
compoundMatch:aString caseSensitive:caseSensitive |
12111 | 5711 |
"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
|
5712 |
consisting of multiple simple GLOB patterns, separated by semicolons. |
12111 | 5713 |
This is usable with fileName pattern fields. |
5714 |
||
5715 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20874 | 5716 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5717 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 5718 |
NOTICE: the receiver is the match pattern" |
5719 |
||
15890 | 5720 |
^self compoundMatch:aString caseSensitive:caseSensitive withoutSeparators:false |
5721 |
||
5722 |
" |
|
5723 |
'f*' match:'foo' |
|
5724 |
'b*' match:'foo' |
|
5725 |
'f*;b*' match:'foo' |
|
5726 |
'f*;b*' match:'bar' |
|
5727 |
'f*;b*' compoundMatch:'foo' |
|
5728 |
'f*;b*' compoundMatch:'bar' |
|
5729 |
'f*;b*' compoundMatch:'Foo' caseSensitive:false |
|
5730 |
'f*;b*' compoundMatch:'Bar' caseSensitive:false |
|
5731 |
'f*;b*' compoundMatch:'ccc' caseSensitive:false |
|
23791 | 5732 |
|
5733 |
'*.png;*.gif' compoundMatch:'bar.GIF' |
|
24524 | 5734 |
'*.png;*.gif' compoundMatch:'bar.GIF' caseSensitive:false |
5735 |
||
5736 |
'foo' compoundMatch:'bar' caseSensitive:false |
|
5737 |
'Bar' compoundMatch:'bar' caseSensitive:false |
|
5738 |
'bar' compoundMatch:'bar' caseSensitive:true |
|
5739 |
'Bar' compoundMatch:'bar' caseSensitive:true |
|
5740 |
'foo;Bar' compoundMatch:'bar' caseSensitive:false |
|
5741 |
'foo;bar' compoundMatch:'bar' caseSensitive:true |
|
5742 |
'foo;Bar' compoundMatch:'bar' caseSensitive:true |
|
5743 |
'foo;bar' compoundMatch:'Bar' caseSensitive:false |
|
5744 |
'foo;bar' compoundMatch:'Bar' caseSensitive:true |
|
5745 |
'foo;bar' compoundMatch:'Bar' caseSensitive:true |
|
5746 |
'.foo' compoundMatch:'.foo' caseSensitive:true |
|
5747 |
'.foo' compoundMatch:'.Foo' caseSensitive:true |
|
5748 |
'.foo' compoundMatch:'.Foo' caseSensitive:false |
|
5749 |
'bar;.foo' compoundMatch:'.foo' caseSensitive:true |
|
5750 |
'bar;.foo' compoundMatch:'.Foo' caseSensitive:true |
|
5751 |
'bar;.foo' compoundMatch:'.Foo' caseSensitive:false |
|
23791 | 5752 |
" |
5753 |
||
5754 |
"Modified: / 15-04-1997 / 15:50:33 / cg" |
|
5755 |
"Modified: / 30-01-1998 / 11:40:18 / stefan" |
|
5756 |
"Created: / 16-12-1999 / 01:21:35 / cg" |
|
5757 |
"Modified (comment): / 02-03-2019 / 12:22:14 / Claus Gittinger" |
|
15890 | 5758 |
! |
5759 |
||
5760 |
compoundMatch:aString caseSensitive:caseSensitive withoutSeparators:withoutSeparators |
|
5761 |
"like match, but the receiver may be a compound match pattern, |
|
5762 |
consisting of multiple simple GLOB patterns, separated by semicolons. |
|
5763 |
If withoutSeparators is true, spaces around individual patterns are stripped off. |
|
5764 |
This is usable with fileName pattern fields. |
|
5765 |
||
5766 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20874 | 5767 |
NOT the ST-80 meaning. |
15890 | 5768 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
5769 |
NOTICE: the receiver is the match pattern" |
|
5770 |
||
12111 | 5771 |
|matchers| |
5772 |
||
5773 |
matchers := self asCollectionOfSubstringsSeparatedBy:$;. |
|
15890 | 5774 |
withoutSeparators ifTrue:[ matchers := matchers collect:[:each | each withoutSeparators] ]. |
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
5775 |
^ matchers |
20874 | 5776 |
contains:[:aPattern | |
5777 |
aPattern match:aString caseSensitive:caseSensitive escapeCharacter:nil |
|
5778 |
]. |
|
15734 | 5779 |
|
5780 |
" |
|
5781 |
'f*' match:'foo' |
|
5782 |
'b*' match:'foo' |
|
5783 |
'f*;b*' match:'foo' |
|
5784 |
'f*;b*' match:'bar' |
|
5785 |
'f*;b*' compoundMatch:'foo' |
|
5786 |
'f*;b*' compoundMatch:'bar' |
|
5787 |
'f*;b*' compoundMatch:'Foo' caseSensitive:false |
|
5788 |
'f*;b*' compoundMatch:'Bar' caseSensitive:false |
|
5789 |
'f*;b*' compoundMatch:'ccc' caseSensitive:false |
|
15890 | 5790 |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
5791 |
'f* ; b*' compoundMatch:'foo' |
15890 | 5792 |
'f* ; b*' compoundMatch:'foo' caseSensitive:true withoutSeparators:true |
15734 | 5793 |
" |
5794 |
||
5795 |
"Modified: / 15.4.1997 / 15:50:33 / cg" |
|
5796 |
"Modified: / 30.1.1998 / 11:40:18 / stefan" |
|
5797 |
"Created: / 16.12.1999 / 01:21:35 / cg" |
|
5798 |
! |
|
5799 |
||
5800 |
compoundMatch:aString ignoreCase:ignoreCase |
|
5801 |
<resource: #obsolete> |
|
5802 |
||
5803 |
"like match, but the receiver may be a compound match pattern, |
|
5804 |
consisting of multiple simple GLOB patterns, separated by semicolons. |
|
5805 |
This is usable with fileName pattern fields. |
|
5806 |
||
5807 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20874 | 5808 |
NOT the ST-80 meaning. |
15734 | 5809 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
5810 |
NOTICE: the receiver is the match pattern" |
|
5811 |
||
5812 |
^ self compoundMatch:aString caseSensitive:ignoreCase not |
|
12111 | 5813 |
|
5814 |
" |
|
5815 |
'f*' match:'foo' |
|
5816 |
'b*' match:'foo' |
|
5817 |
'f*;b*' match:'foo' |
|
5818 |
'f*;b*' match:'bar' |
|
5819 |
'f*;b*' compoundMatch:'foo' |
|
5820 |
'f*;b*' compoundMatch:'bar' |
|
5821 |
'f*;b*' compoundMatch:'Foo' ignoreCase:true |
|
5822 |
'f*;b*' compoundMatch:'Bar' ignoreCase:true |
|
5823 |
'f*;b*' compoundMatch:'ccc' ignoreCase:true |
|
5824 |
" |
|
5825 |
||
5826 |
"Modified: / 15.4.1997 / 15:50:33 / cg" |
|
5827 |
"Modified: / 30.1.1998 / 11:40:18 / stefan" |
|
5828 |
"Created: / 16.12.1999 / 01:21:35 / cg" |
|
5829 |
! |
|
5830 |
||
5831 |
findMatchString:matchString |
|
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5832 |
"like findString/indexOfSubCollection, but allowing GLOB match patterns. |
23791 | 5833 |
find matchstring; if found, return the index, if not, return 0. |
5834 |
This is a case sensitive match: lower/uppercase are considered different. |
|
12111 | 5835 |
|
5836 |
NOTICE: match-meta character interpretation is like in unix-matching, |
|
20874 | 5837 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5838 |
NOTICE: this GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 5839 |
NOTICE: the argument is the match pattern" |
5840 |
||
19033 | 5841 |
^ self findMatchString:matchString startingAt:1 caseSensitive:true ifAbsent:0 |
5842 |
||
5843 |
" |
|
5844 |
1234567890123 |
|
5845 |
'hello world bla foo baz' findMatchString:'b* ' |
|
5846 |
" |
|
23791 | 5847 |
|
5848 |
"Modified (comment): / 02-03-2019 / 12:21:07 / Claus Gittinger" |
|
12111 | 5849 |
! |
5850 |
||
24481 | 5851 |
findMatchString:matchString caseSensitive:caseSensitive |
5852 |
"like findString/indexOfSubCollection, but allowing GLOB match patterns. |
|
5853 |
find matchstring; if found, return the index, if not, return 0. |
|
5854 |
This is a case sensitive match: lower/uppercase are considered different. |
|
5855 |
||
5856 |
NOTICE: match-meta character interpretation is like in unix-matching, |
|
5857 |
NOT the ST-80 meaning. |
|
5858 |
NOTICE: this GLOB, which is different from regex matching (see matchesRegex:) |
|
5859 |
NOTICE: the argument is the match pattern" |
|
5860 |
||
5861 |
^ self findMatchString:matchString startingAt:1 caseSensitive:caseSensitive ifAbsent:0 |
|
5862 |
||
5863 |
" |
|
5864 |
1234567890123 |
|
5865 |
'hello world bla foo baz' findMatchString:'b* ' |
|
5866 |
" |
|
5867 |
||
5868 |
"Modified (comment): / 02-03-2019 / 12:21:07 / Claus Gittinger" |
|
5869 |
! |
|
5870 |
||
12111 | 5871 |
findMatchString:matchString startingAt:index |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5872 |
"like findString, but allowing GLOB match patterns. |
23791 | 5873 |
find matchstring, starting at index; if found, return the index, if not, return 0. |
5874 |
This is a case sensitive match: lower/uppercase are considered different. |
|
12111 | 5875 |
|
5876 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20874 | 5877 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5878 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 5879 |
NOTICE: the argument is the match pattern" |
5880 |
||
19033 | 5881 |
^ self findMatchString:matchString startingAt:index caseSensitive:true ifAbsent:0 |
23791 | 5882 |
|
5883 |
"Modified (comment): / 02-03-2019 / 12:21:05 / Claus Gittinger" |
|
12111 | 5884 |
! |
5885 |
||
15734 | 5886 |
findMatchString:matchString startingAt:index caseSensitive:caseSensitive ifAbsent:exceptionBlock |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5887 |
"like findString, but allowing GLOB match patterns. |
12111 | 5888 |
find matchstring, starting at index. if found, return the index; |
5889 |
if not found, return the result of evaluating exceptionBlock. |
|
5890 |
This is a q&d hack - not very efficient. |
|
5891 |
||
5892 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20874 | 5893 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5894 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 5895 |
NOTICE: the argument is the match pattern" |
5896 |
||
5897 |
|firstChar firstSet |
|
5898 |
startIndex "{ Class: SmallInteger }" |
|
5899 |
matchSize "{ Class: SmallInteger }" |
|
5900 |
mySize "{ Class: SmallInteger }" |
|
5901 |
realMatchString lcChar ucChar| |
|
5902 |
||
5903 |
matchSize := matchString size. |
|
5904 |
matchSize == 0 ifTrue:[^ index]. "empty string matches" |
|
5905 |
||
5906 |
realMatchString := matchString. |
|
5907 |
(realMatchString endsWith:$*) ifFalse:[ |
|
20874 | 5908 |
realMatchString := realMatchString , '*'. |
5909 |
matchSize := matchSize + 1 |
|
12111 | 5910 |
]. |
5911 |
||
5912 |
mySize := self size. |
|
5913 |
firstChar := realMatchString at:1. |
|
5914 |
firstChar == self class matchEscapeCharacter ifTrue:[ |
|
20874 | 5915 |
firstChar := realMatchString at:2. |
12111 | 5916 |
]. |
5917 |
||
5918 |
firstChar asString includesMatchCharacters ifTrue:[ |
|
20874 | 5919 |
index to:mySize do:[:col | |
5920 |
(realMatchString match:self from:col to:mySize caseSensitive:caseSensitive) |
|
5921 |
ifTrue:[^ col] |
|
5922 |
]. |
|
5923 |
^ exceptionBlock value. |
|
12111 | 5924 |
]. |
5925 |
||
5926 |
lcChar := firstChar asLowercase. |
|
5927 |
ucChar := firstChar asUppercase. |
|
15734 | 5928 |
(caseSensitive not and:[ lcChar ~= ucChar ]) ifTrue:[ |
20874 | 5929 |
firstSet := Array with:ucChar with:lcChar. |
5930 |
startIndex := self indexOfAny:firstSet startingAt:index. |
|
12111 | 5931 |
] ifFalse:[ |
20874 | 5932 |
startIndex := self indexOf:firstChar startingAt:index. |
12111 | 5933 |
]. |
5934 |
[startIndex == 0] whileFalse:[ |
|
20874 | 5935 |
(realMatchString match:self from:startIndex to:mySize caseSensitive:caseSensitive) |
5936 |
ifTrue:[^ startIndex]. |
|
5937 |
||
5938 |
firstSet notNil ifTrue:[ |
|
5939 |
startIndex := self indexOfAny:firstSet startingAt:(startIndex + 1). |
|
5940 |
] ifFalse:[ |
|
5941 |
startIndex := self indexOf:firstChar startingAt:(startIndex + 1). |
|
5942 |
]. |
|
12111 | 5943 |
]. |
5944 |
^ exceptionBlock value |
|
5945 |
||
5946 |
" |
|
5947 |
'one two three four' findMatchString:'o[nu]' |
|
5948 |
'one two three four' findMatchString:'o[nu]' startingAt:3 |
|
15734 | 5949 |
'one two three four one' findMatchString:'ONE' startingAt:3 caseSensitive:false ifAbsent:0 |
5950 |
'one two three four one' findMatchString:'ONE' startingAt:3 caseSensitive:true ifAbsent:0 |
|
5951 |
'one two three four ONE' findMatchString:'O#E' startingAt:1 caseSensitive:false ifAbsent:0 |
|
5952 |
'one two three four ONE' findMatchString:'O#E' startingAt:1 caseSensitive:true ifAbsent:0 |
|
5953 |
12345678901234567890 |
|
5954 |
" |
|
5955 |
||
5956 |
"Modified: 13.9.1997 / 06:31:22 / cg" |
|
5957 |
! |
|
5958 |
||
5959 |
findMatchString:matchString startingAt:index ignoreCase:ignoreCase ifAbsent:exceptionBlock |
|
5960 |
<resource: #obsolete> |
|
5961 |
"like findString, but allowing GLOB match patterns. |
|
5962 |
find matchstring, starting at index. if found, return the index; |
|
5963 |
if not found, return the result of evaluating exceptionBlock. |
|
5964 |
This is a q&d hack - not very efficient. |
|
5965 |
||
5966 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20874 | 5967 |
NOT the ST-80 meaning. |
15734 | 5968 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
5969 |
NOTICE: the argument is the match pattern" |
|
5970 |
||
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
5971 |
^ self |
20874 | 5972 |
findMatchString:matchString startingAt:index caseSensitive:ignoreCase not ifAbsent:exceptionBlock |
15734 | 5973 |
|
5974 |
" |
|
5975 |
'one two three four' findMatchString:'o[nu]' |
|
5976 |
'one two three four' findMatchString:'o[nu]' startingAt:3 |
|
12111 | 5977 |
'one two three four one' findMatchString:'ONE' startingAt:3 ignoreCase:true ifAbsent:0 |
5978 |
" |
|
5979 |
||
5980 |
"Modified: 13.9.1997 / 06:31:22 / cg" |
|
5981 |
! |
|
5982 |
||
19746 | 5983 |
globPatternAsRegexPattern |
5984 |
"taking the receiver as a GLOB pattern, |
|
5985 |
return a corresponding regex pattern. |
|
5986 |
As regex does provide information about the matching substring, |
|
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
5987 |
it may be useful to apply a regex after a GLOB match, |
19746 | 5988 |
in order to highlight matching substrings (eg. in a CodeView after a search). |
5989 |
If it turns out to be better, we may convert all GLOB searches to regex and use it right away. |
|
5990 |
(currently, it is not sure, if GLOB is not better for most simple searches, as they are encountered |
|
5991 |
in typical real life)" |
|
5992 |
||
5993 |
^ self species streamContents:[:s | |
|
20874 | 5994 |
|addCharacter| |
5995 |
||
5996 |
addCharacter := |
|
5997 |
[:ch | |
|
5998 |
ch isLetterOrDigit ifFalse:[ |
|
5999 |
s nextPut:$\. |
|
6000 |
]. |
|
6001 |
s nextPut:ch |
|
6002 |
]. |
|
6003 |
||
23552 | 6004 |
(self class matchScanArrayFrom:self) do:[:matchEntry | |
20874 | 6005 |
matchEntry isCharacter ifTrue:[ |
6006 |
addCharacter value:matchEntry |
|
6007 |
] ifFalse:[ |
|
6008 |
matchEntry == #anyString ifTrue:[ |
|
6009 |
s nextPutAll:'.*' |
|
6010 |
] ifFalse:[ |
|
6011 |
matchEntry == #any ifTrue:[ |
|
6012 |
s nextPut:$. |
|
6013 |
] ifFalse:[ |
|
6014 |
matchEntry isString ifTrue:[ |
|
6015 |
|set min max| |
|
6016 |
||
6017 |
s nextPut:$[. |
|
23552 | 6018 |
set := matchEntry "copy sort". "/ already sorted |
20874 | 6019 |
min := set min. |
6020 |
max := set max. |
|
6021 |
set asSet = (min to:max) asSet ifTrue:[ |
|
6022 |
addCharacter value:min. |
|
6023 |
s nextPut:$-. |
|
6024 |
addCharacter value:max. |
|
6025 |
] ifFalse:[ |
|
6026 |
set do:addCharacter. |
|
6027 |
]. |
|
6028 |
s nextPut:$]. |
|
6029 |
] ifFalse:[ |
|
22700 | 6030 |
self halt:'funny match entry'. |
20874 | 6031 |
]. |
6032 |
]. |
|
6033 |
]. |
|
6034 |
] |
|
6035 |
]. |
|
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
6036 |
]. |
19746 | 6037 |
|
6038 |
" |
|
6039 |
'hello' globPatternAsRegexPattern |
|
6040 |
'hello*' globPatternAsRegexPattern |
|
6041 |
'*hello*' globPatternAsRegexPattern |
|
6042 |
'h###' globPatternAsRegexPattern |
|
6043 |
'h[0-9]' globPatternAsRegexPattern |
|
6044 |
'h[0-9][0-9][0-9]' globPatternAsRegexPattern |
|
6045 |
'h[0-9]*' globPatternAsRegexPattern |
|
19747 | 6046 |
'h[-+]*' globPatternAsRegexPattern |
23552 | 6047 |
'h[abc]*' globPatternAsRegexPattern |
6048 |
'h[0-9abc]*' globPatternAsRegexPattern |
|
19747 | 6049 |
'hello world' matches:'h*w' |
6050 |
'hello world' matchesRegex:('h*w' globPatternAsRegexPattern) |
|
6051 |
'hello world' matches:'h*d' |
|
6052 |
'hello world' matchesRegex:('h*d' globPatternAsRegexPattern) |
|
19746 | 6053 |
" |
23552 | 6054 |
|
6055 |
"Modified: / 03-12-2018 / 15:47:15 / Stefan Vogel" |
|
19746 | 6056 |
! |
6057 |
||
12111 | 6058 |
includesMatchString:matchString |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
6059 |
"like includesString, but allowing GLOB match patterns. |
23791 | 6060 |
find matchstring; if found, return true, false otherwise. |
6061 |
This is a case sensitive match: lower/uppercase are considered different. |
|
12111 | 6062 |
|
6063 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20874 | 6064 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
6065 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 6066 |
NOTICE: the argument is the match pattern" |
6067 |
||
6068 |
^ (self findMatchString:matchString) ~~ 0 |
|
6069 |
||
6070 |
" |
|
6071 |
'hello world' includesMatchString:'h*' |
|
6072 |
'hello world' includesMatchString:'h[aeiou]llo' |
|
6073 |
'hello world' includesMatchString:'wor*' |
|
6074 |
'hello world' includesMatchString:'woR*' |
|
23791 | 6075 |
'menue' includesMatchString:'[mM]enu[Ee]' |
6076 |
'blamenuebla' includesMatchString:'[mM]enu[Ee]' |
|
6077 |
'blaMenuebla' includesMatchString:'[mM]enu[Ee]' |
|
6078 |
" |
|
6079 |
||
6080 |
"Modified (comment): / 02-03-2019 / 12:20:58 / Claus Gittinger" |
|
12111 | 6081 |
! |
6082 |
||
6083 |
includesMatchString:matchString caseSensitive:caseSensitive |
|
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
6084 |
"like includesString, but allowing GLOB match patterns. |
12111 | 6085 |
find matchstring; if found, return true, otherwise return false. |
6086 |
||
6087 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20874 | 6088 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
6089 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 6090 |
NOTICE: the argument is the match pattern" |
6091 |
||
19033 | 6092 |
^ (self findMatchString:matchString startingAt:1 caseSensitive:caseSensitive ifAbsent:0) ~~ 0 |
12111 | 6093 |
|
6094 |
" |
|
6095 |
'hello world' includesMatchString:'h*' caseSensitive:true |
|
6096 |
'hello world' includesMatchString:'h*' caseSensitive:false |
|
6097 |
'Hello world' includesMatchString:'h*' caseSensitive:true |
|
6098 |
'Hello world' includesMatchString:'h*' caseSensitive:false |
|
6099 |
||
6100 |
'hello world' includesMatchString:'h[aeiou]llo' caseSensitive:true |
|
6101 |
'hello world' includesMatchString:'h[aeiou]llo' caseSensitive:false |
|
6102 |
||
6103 |
'hello world' includesMatchString:'wor*' caseSensitive:true |
|
6104 |
'hello world' includesMatchString:'wor*' caseSensitive:false |
|
6105 |
||
6106 |
'hello world' includesMatchString:'woR*' caseSensitive:true |
|
6107 |
'hello world' includesMatchString:'woR*' caseSensitive:false |
|
23791 | 6108 |
|
6109 |
'menue' includesMatchString:'[mM]enu[Ee]' |
|
6110 |
'menue' includesMatchString:'[mM]enu[Ee]' caseSensitive:true |
|
6111 |
'blamenuebla' includesMatchString:'[mM]enu[Ee]' |
|
6112 |
'blamenuebla' includesMatchString:'[mM]enu[Ee]' caseSensitive:true |
|
6113 |
||
6114 |
'blaMenuebla' includesMatchString:'[MS]enu[EF]' |
|
6115 |
'blaMenuebla' includesMatchString:'[MS]enu[EF]' caseSensitive:true |
|
6116 |
'blaMenuebla' includesMatchString:'[MS]enu[EF]' caseSensitive:false |
|
6117 |
" |
|
6118 |
||
6119 |
"Modified (comment): / 02-03-2019 / 12:18:57 / Claus Gittinger" |
|
12111 | 6120 |
! |
6121 |
||
6122 |
match:aString |
|
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
6123 |
"return true if aString matches self, where self may contain GLOB meta-match |
12111 | 6124 |
characters $* (to match any string) or $# (to match any character). |
6125 |
or [...] to match a set of characters. |
|
23791 | 6126 |
This is a case sensitive match: lower/uppercase are considered different. |
12111 | 6127 |
The escape character is the backQuote. |
6128 |
||
6129 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20874 | 6130 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
6131 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 6132 |
NOTICE: the receiver is the match pattern" |
6133 |
||
15734 | 6134 |
^ self match:aString from:1 to:aString size caseSensitive:true |
12111 | 6135 |
|
6136 |
" |
|
22137 | 6137 |
'#f#' match:'afc' |
6138 |
'#f#' match:'aec' |
|
6139 |
'#f#' match:'ae' |
|
6140 |
'#f#' match:'a' |
|
6141 |
'#f#' match:'f' |
|
6142 |
'#f#' match:'ff' |
|
6143 |
||
12111 | 6144 |
'\*f*' match:'f' |
6145 |
'\*f*' match:'*f' |
|
6146 |
'*\*f*' match:'*f' |
|
6147 |
'*f*' match:'*f' |
|
6148 |
'*ute*' match:'computer' |
|
6149 |
'*uter' match:'computer' |
|
6150 |
'uter*' match:'computer' |
|
6151 |
'*ute*' match:'' |
|
6152 |
'[abcd]*' match:'computer' |
|
6153 |
'[abcd]*' match:'komputer' |
|
6154 |
'*some*compl*ern*' match:'this is some more complicated pattern match' |
|
6155 |
'*some*compl*ern*' match:'this is another complicated pattern match' |
|
6156 |
'*-hh' match:'anton-h' |
|
6157 |
" |
|
6158 |
||
22137 | 6159 |
"Modified: / 09-06-1998 / 18:50:00 / cg" |
6160 |
"Modified (comment): / 29-07-2017 / 14:00:23 / cg" |
|
23791 | 6161 |
"Modified (comment): / 02-03-2019 / 12:20:48 / Claus Gittinger" |
12111 | 6162 |
! |
6163 |
||
15734 | 6164 |
match:aString caseSensitive:caseSensitive |
6165 |
"return true if aString matches self, where self may contain GLOB meta-match |
|
6166 |
characters $* (to match any string) or $# (to match any character) |
|
6167 |
or [...] to match a set of characters. |
|
6168 |
If caseSensitive is false, lower/uppercase are considered the same. |
|
6169 |
The escape character is the backQuote. |
|
6170 |
||
6171 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20874 | 6172 |
NOT the ST-80 meaning. |
15734 | 6173 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
6174 |
NOTICE: the receiver is the match pattern" |
|
6175 |
||
6176 |
^ self match:aString from:1 to:aString size caseSensitive:caseSensitive |
|
6177 |
||
6178 |
" |
|
6179 |
'*ute*' match:'COMPUTER' caseSensitive:false |
|
6180 |
'*uter' match:'COMPUTER' caseSensitive:true |
|
6181 |
'[abcd]*' match:'computer' caseSensitive:true |
|
6182 |
'[abcd]*' match:'Computer' caseSensitive:true |
|
6183 |
'[a-k]*' match:'komputer' caseSensitive:true |
|
6184 |
'[a-k]*' match:'zomputer' caseSensitive:true |
|
6185 |
'[a-k]*' match:'Komputer' caseSensitive:true |
|
6186 |
'[a-k]*' match:'Komputer' caseSensitive:false |
|
6187 |
'*some*compl*ern*' match:'this is some more complicated pattern match' caseSensitive:false |
|
6188 |
'*some*compl*ern*' match:'this is another complicated pattern match' caseSensitive:false |
|
6189 |
||
6190 |
Time millisecondsToRun:[ |
|
20874 | 6191 |
Symbol allInstancesDo:[:sym | |
6192 |
'[ab]*' match:sym caseSensitive:true |
|
6193 |
] |
|
15734 | 6194 |
]. |
6195 |
Time millisecondsToRun:[ |
|
20874 | 6196 |
Symbol allInstancesDo:[:sym | |
6197 |
'*at:*' match:sym caseSensitive:true |
|
6198 |
] |
|
15734 | 6199 |
]. |
6200 |
" |
|
6201 |
||
6202 |
"Modified: 2.4.1997 / 17:28:58 / cg" |
|
6203 |
! |
|
6204 |
||
6205 |
match:aString caseSensitive:caseSensitive escapeCharacter:escape |
|
6206 |
"return true if aString matches self, where self may contain GLOB meta-match |
|
6207 |
characters $* (to match any string) or $# (to match any character) |
|
6208 |
or [...] to match a set of characters. |
|
6209 |
If caseSensitive is false, lower/uppercase are considered the same. |
|
6210 |
||
6211 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20874 | 6212 |
NOT the ST-80 meaning. |
15734 | 6213 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
6214 |
NOTICE: the receiver is the match pattern" |
|
6215 |
||
6216 |
^ self match:aString from:1 to:aString size caseSensitive:caseSensitive escapeCharacter:escape |
|
6217 |
||
6218 |
" |
|
6219 |
'*ute*' match:'COMPUTER' caseSensitive:false |
|
6220 |
'*uter' match:'COMPUTER' caseSensitive:true |
|
6221 |
'[abcd]*' match:'computer' caseSensitive:true |
|
6222 |
'[abcd]*' match:'Computer' caseSensitive:true |
|
6223 |
'[a-k]*' match:'komputer' caseSensitive:true |
|
6224 |
'[a-k]*' match:'zomputer' caseSensitive:true |
|
6225 |
'[a-k]*' match:'Komputer' caseSensitive:true |
|
6226 |
'[a-k]*' match:'Komputer' caseSensitive:false |
|
6227 |
'*some*compl*ern*' match:'this is some more complicated pattern match' caseSensitive:false |
|
6228 |
'*some*compl*ern*' match:'this is another complicated pattern match' caseSensitive:false |
|
6229 |
||
6230 |
Time millisecondsToRun:[ |
|
20874 | 6231 |
Symbol allInstancesDo:[:sym | |
6232 |
'[ab]*' match:sym caseSensitive:true |
|
6233 |
] |
|
15734 | 6234 |
]. |
6235 |
Time millisecondsToRun:[ |
|
20874 | 6236 |
Symbol allInstancesDo:[:sym | |
6237 |
'*at:*' match:sym caseSensitive:true |
|
6238 |
] |
|
15734 | 6239 |
]. |
6240 |
" |
|
6241 |
||
6242 |
"Modified: 2.4.1997 / 17:28:58 / cg" |
|
6243 |
! |
|
6244 |
||
12111 | 6245 |
match:aString escapeCharacter:escape |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
6246 |
"return true if aString matches self, where self may contain GLOB meta-match |
12111 | 6247 |
characters $* (to match any string) or $# (to match any character). |
6248 |
or [...] to match a set of characters. |
|
6249 |
Lower/uppercase are considered different. |
|
6250 |
||
6251 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20874 | 6252 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
6253 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 6254 |
NOTICE: the receiver is the match pattern" |
6255 |
||
19033 | 6256 |
^ self match:aString from:1 to:aString size caseSensitive:true escapeCharacter:escape |
12111 | 6257 |
|
6258 |
" |
|
6259 |
'a\b\c\*' match:'a\b\c\d' |
|
6260 |
'a\b\c\*' match:'a\b\c\d' escapeCharacter:nil |
|
6261 |
" |
|
6262 |
! |
|
6263 |
||
15734 | 6264 |
match:aString from:start to:stop caseSensitive:caseSensitive |
6265 |
"return true if part of aString matches myself, |
|
6266 |
where self may contain GLOB meta-match |
|
6267 |
characters $* (to match any string) or $# (to match any character) |
|
6268 |
or [...] to match a set of characters. |
|
6269 |
If caseSensitive is false, lower/uppercase are considered the same. |
|
6270 |
The escape character is the backQuote. |
|
6271 |
||
6272 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20874 | 6273 |
NOT the ST-80 meaning. |
15734 | 6274 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
6275 |
NOTICE: the receiver is the match pattern" |
|
6276 |
||
6277 |
^ self |
|
20874 | 6278 |
match:aString from:start to:stop caseSensitive:caseSensitive |
6279 |
escapeCharacter:(self class matchEscapeCharacter) |
|
15734 | 6280 |
|
6281 |
" |
|
6282 |
'*ute*' match:'12345COMPUTER' from:1 to:5 caseSensitive:false |
|
6283 |
'*ute*' match:'12345COMPUTER' from:6 to:13 caseSensitive:false |
|
6284 |
" |
|
6285 |
||
6286 |
"Modified: / 10.11.1998 / 21:43:46 / cg" |
|
6287 |
! |
|
6288 |
||
6289 |
match:aString from:start to:stop caseSensitive:caseSensitive escapeCharacter:escape |
|
6290 |
"return true if part of aString matches myself, |
|
6291 |
where self may contain GLOB meta-match |
|
6292 |
characters $* (to match any string) or $# (to match any character) |
|
6293 |
or [...] to match a set of characters. |
|
6294 |
If caseSensitive is false, lower/uppercase are considered the same. |
|
6295 |
||
6296 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20667 | 6297 |
NOT the ST-80 meaning. |
15734 | 6298 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
6299 |
NOTICE: the receiver is the match pattern" |
|
6300 |
||
6301 |
|matchScanArray| |
|
6302 |
||
6303 |
" |
|
22137 | 6304 |
keep the matchScanArray from the most recent matches - |
6305 |
avoids parsing the pattern over-and over again, |
|
6306 |
if multiple searches are done with the same pattern. |
|
6307 |
" |
|
20667 | 6308 |
(PreviousMatches isNil |
6309 |
or:[(matchScanArray := PreviousMatches at: self ifAbsent:[nil]) isNil]) ifTrue:[ |
|
6310 |
matchScanArray := self class matchScanArrayFrom:self escapeCharacter:escape. |
|
6311 |
matchScanArray isNil ifTrue:[ |
|
23459 | 6312 |
ParseWarning raiseRequestErrorString:('CharacterArray [info]: invalid matchpattern: ''%1'' comparing for equality' bindWith:self). |
20667 | 6313 |
^ self = aString |
23459 | 6314 |
"/ ^ false |
20667 | 6315 |
]. |
6316 |
PreviousMatches isNil ifTrue:[ |
|
22137 | 6317 |
PreviousMatches := CacheDictionary new:15 |
20667 | 6318 |
]. |
6319 |
PreviousMatches at:self put:matchScanArray. |
|
15734 | 6320 |
]. |
6321 |
||
6322 |
^ self class |
|
20667 | 6323 |
matchScan:matchScanArray |
6324 |
from:1 to:matchScanArray size |
|
6325 |
with:aString |
|
6326 |
from:start to:stop |
|
6327 |
caseSensitive:caseSensitive |
|
15734 | 6328 |
|
6329 |
" |
|
6330 |
'*ute*' match:'12345COMPUTER' from:1 to:5 caseSensitive:false |
|
6331 |
'*ute*' match:'12345COMPUTER' from:6 to:13 caseSensitive:false |
|
23459 | 6332 |
|
6333 |
'*[12' match:'12345COMPUTER' from:6 to:13 caseSensitive:false -- gives a warning |
|
15734 | 6334 |
" |
6335 |
||
22137 | 6336 |
"Modified: / 29-07-2017 / 14:01:42 / cg" |
23459 | 6337 |
"Modified (comment): / 24-10-2018 / 09:01:31 / Claus Gittinger" |
15734 | 6338 |
! |
6339 |
||
12111 | 6340 |
match:aString from:start to:stop ignoreCase:ignoreCase |
15734 | 6341 |
<resource: #obsolete> |
12111 | 6342 |
"return true if part of aString matches myself, |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
6343 |
where self may contain GLOB meta-match |
12111 | 6344 |
characters $* (to match any string) or $# (to match any character) |
6345 |
or [...] to match a set of characters. |
|
6346 |
If ignoreCase is true, lower/uppercase are considered the same. |
|
6347 |
The escape character is the backQuote. |
|
6348 |
||
6349 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20874 | 6350 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
6351 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 6352 |
NOTICE: the receiver is the match pattern" |
6353 |
||
6354 |
^ self |
|
20874 | 6355 |
match:aString from:start to:stop caseSensitive:ignoreCase not |
6356 |
escapeCharacter:(self class matchEscapeCharacter) |
|
12111 | 6357 |
|
6358 |
" |
|
6359 |
'*ute*' match:'12345COMPUTER' from:1 to:5 ignoreCase:true |
|
6360 |
'*ute*' match:'12345COMPUTER' from:6 to:13 ignoreCase:true |
|
6361 |
" |
|
6362 |
||
6363 |
"Modified: / 10.11.1998 / 21:43:46 / cg" |
|
6364 |
! |
|
6365 |
||
6366 |
match:aString from:start to:stop ignoreCase:ignoreCase escapeCharacter:escape |
|
15734 | 6367 |
<resource: #obsolete> |
12111 | 6368 |
"return true if part of aString matches myself, |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
6369 |
where self may contain GLOB meta-match |
12111 | 6370 |
characters $* (to match any string) or $# (to match any character) |
6371 |
or [...] to match a set of characters. |
|
6372 |
If ignoreCase is true, lower/uppercase are considered the same. |
|
6373 |
||
6374 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20874 | 6375 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
6376 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 6377 |
NOTICE: the receiver is the match pattern" |
6378 |
||
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
6379 |
^ self |
20874 | 6380 |
match:aString from:start to:stop |
6381 |
caseSensitive:ignoreCase not |
|
6382 |
escapeCharacter:escape |
|
12111 | 6383 |
|
6384 |
" |
|
6385 |
'*ute*' match:'12345COMPUTER' from:1 to:5 ignoreCase:true |
|
6386 |
'*ute*' match:'12345COMPUTER' from:6 to:13 ignoreCase:true |
|
6387 |
" |
|
6388 |
||
6389 |
"Modified: / 10.11.1998 / 21:43:46 / cg" |
|
6390 |
! |
|
6391 |
||
6392 |
match:aString ignoreCase:ignoreCase |
|
15734 | 6393 |
<resource: #obsolete> |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
6394 |
"return true if aString matches self, where self may contain GLOB meta-match |
12111 | 6395 |
characters $* (to match any string) or $# (to match any character) |
6396 |
or [...] to match a set of characters. |
|
6397 |
If ignoreCase is true, lower/uppercase are considered the same. |
|
6398 |
The escape character is the backQuote. |
|
6399 |
||
6400 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20874 | 6401 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
6402 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 6403 |
NOTICE: the receiver is the match pattern" |
6404 |
||
15734 | 6405 |
"/ leave it this way, in case a customer has redefined it for performance (for a while) |
19033 | 6406 |
^ self match:aString from:1 to:aString size caseSensitive:ignoreCase not |
12111 | 6407 |
|
6408 |
" |
|
6409 |
'*ute*' match:'COMPUTER' ignoreCase:true |
|
6410 |
'*uter' match:'COMPUTER' ignoreCase:false |
|
6411 |
'[abcd]*' match:'computer' ignoreCase:false |
|
6412 |
'[abcd]*' match:'Computer' ignoreCase:false |
|
6413 |
'[a-k]*' match:'komputer' ignoreCase:false |
|
6414 |
'[a-k]*' match:'zomputer' ignoreCase:false |
|
6415 |
'[a-k]*' match:'Komputer' ignoreCase:false |
|
6416 |
'[a-k]*' match:'Komputer' ignoreCase:true |
|
6417 |
'*some*compl*ern*' match:'this is some more complicated pattern match' ignoreCase:true |
|
6418 |
'*some*compl*ern*' match:'this is another complicated pattern match' ignoreCase:true |
|
6419 |
||
6420 |
Time millisecondsToRun:[ |
|
20874 | 6421 |
Symbol allInstancesDo:[:sym | |
6422 |
'[ab]*' match:sym ignoreCase:false |
|
6423 |
] |
|
12111 | 6424 |
]. |
6425 |
Time millisecondsToRun:[ |
|
20874 | 6426 |
Symbol allInstancesDo:[:sym | |
6427 |
'*at:*' match:sym ignoreCase:false |
|
6428 |
] |
|
12111 | 6429 |
]. |
6430 |
" |
|
6431 |
||
6432 |
"Modified: 2.4.1997 / 17:28:58 / cg" |
|
6433 |
! |
|
6434 |
||
6435 |
match:aString ignoreCase:ignoreCase escapeCharacter:escape |
|
15734 | 6436 |
<resource: #obsolete> |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
6437 |
"return true if aString matches self, where self may contain GLOB meta-match |
12111 | 6438 |
characters $* (to match any string) or $# (to match any character) |
6439 |
or [...] to match a set of characters. |
|
6440 |
If ignoreCase is true, lower/uppercase are considered the same. |
|
6441 |
||
6442 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20874 | 6443 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
6444 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 6445 |
NOTICE: the receiver is the match pattern" |
6446 |
||
19033 | 6447 |
^ self match:aString from:1 to:aString size caseSensitive:ignoreCase not escapeCharacter:escape |
12111 | 6448 |
|
6449 |
" |
|
6450 |
'*ute*' match:'COMPUTER' ignoreCase:true |
|
6451 |
'*uter' match:'COMPUTER' ignoreCase:false |
|
6452 |
'[abcd]*' match:'computer' ignoreCase:false |
|
6453 |
'[abcd]*' match:'Computer' ignoreCase:false |
|
6454 |
'[a-k]*' match:'komputer' ignoreCase:false |
|
6455 |
'[a-k]*' match:'zomputer' ignoreCase:false |
|
6456 |
'[a-k]*' match:'Komputer' ignoreCase:false |
|
6457 |
'[a-k]*' match:'Komputer' ignoreCase:true |
|
6458 |
'*some*compl*ern*' match:'this is some more complicated pattern match' ignoreCase:true |
|
6459 |
'*some*compl*ern*' match:'this is another complicated pattern match' ignoreCase:true |
|
6460 |
||
6461 |
Time millisecondsToRun:[ |
|
20874 | 6462 |
Symbol allInstancesDo:[:sym | |
6463 |
'[ab]*' match:sym ignoreCase:false |
|
6464 |
] |
|
12111 | 6465 |
]. |
6466 |
Time millisecondsToRun:[ |
|
20874 | 6467 |
Symbol allInstancesDo:[:sym | |
6468 |
'*at:*' match:sym ignoreCase:false |
|
6469 |
] |
|
12111 | 6470 |
]. |
6471 |
" |
|
6472 |
||
6473 |
"Modified: 2.4.1997 / 17:28:58 / cg" |
|
6474 |
! |
|
6475 |
||
23287 | 6476 |
matches:aGlobPatternString |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
6477 |
"return true if the receiver matches aString, where aPatternString may contain GLOB meta-match |
12111 | 6478 |
characters $* (to match any string) or $# (to match any character). |
6479 |
or [...] to match a set of characters. |
|
6480 |
Lower/uppercase are considered different. |
|
6481 |
||
6482 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20874 | 6483 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
6484 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 6485 |
NOTICE: the receiver is the match pattern" |
6486 |
||
23287 | 6487 |
^ aGlobPatternString match:self |
6488 |
||
6489 |
"Modified (format): / 18-08-2018 / 20:51:45 / Claus Gittinger" |
|
14054
48d3fab77bd9
added: #matches:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
14053
diff
changeset
|
6490 |
! |
48d3fab77bd9
added: #matches:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
14053
diff
changeset
|
6491 |
|
15734 | 6492 |
matches:aPatternString caseSensitive:caseSensitive |
6493 |
"return true if the receiver matches aString, where aPatternString may contain GLOB meta-match |
|
6494 |
characters $* (to match any string) or $# (to match any character). |
|
6495 |
or [...] to match a set of characters. |
|
6496 |
||
6497 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20874 | 6498 |
NOT the ST-80 meaning. |
15734 | 6499 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
6500 |
NOTICE: the receiver is the match pattern" |
|
6501 |
||
6502 |
^ aPatternString match:self caseSensitive:caseSensitive |
|
6503 |
! |
|
6504 |
||
14054
48d3fab77bd9
added: #matches:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
14053
diff
changeset
|
6505 |
matches:aPatternString ignoreCase:ignoreCase |
15734 | 6506 |
<resource: #obsolete> |
14054
48d3fab77bd9
added: #matches:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
14053
diff
changeset
|
6507 |
"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
|
6508 |
characters $* (to match any string) or $# (to match any character). |
48d3fab77bd9
added: #matches:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
14053
diff
changeset
|
6509 |
or [...] to match a set of characters. |
48d3fab77bd9
added: #matches:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
14053
diff
changeset
|
6510 |
Lower/uppercase are considered different. |
48d3fab77bd9
added: #matches:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
14053
diff
changeset
|
6511 |
|
48d3fab77bd9
added: #matches:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
14053
diff
changeset
|
6512 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
20874 | 6513 |
NOT the ST-80 meaning. |
14054
48d3fab77bd9
added: #matches:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
14053
diff
changeset
|
6514 |
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
|
6515 |
NOTICE: the receiver is the match pattern" |
48d3fab77bd9
added: #matches:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
14053
diff
changeset
|
6516 |
|
15734 | 6517 |
^ aPatternString match:self caseSensitive:ignoreCase not |
14054
48d3fab77bd9
added: #matches:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
14053
diff
changeset
|
6518 |
|
48d3fab77bd9
added: #matches:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
14053
diff
changeset
|
6519 |
"Created: / 08-03-2012 / 03:11:11 / cg" |
12111 | 6520 |
! ! |
6521 |
||
6522 |
||
24601 | 6523 |
!CharacterArray methodsFor:'matching - regex'! |
6524 |
||
6525 |
subExpressionsInRegex:rxString caseSensitive:caseSensitive |
|
6526 |
"return the collection of matching regex-subexpressions. |
|
6527 |
Subexpressions are sub-patterns enclosed in parentheses in the regex. |
|
6528 |
substrings in the receiver. |
|
6529 |
Returns nil, if there is no match. |
|
6530 |
Use this to extract particular parts from the receiver. |
|
6531 |
Refer to `documentation' protocol of RxParser class for details." |
|
6532 |
||
6533 |
|matcher| |
|
6534 |
||
6535 |
matcher := Regex::RxMatcher new initializeFromString:rxString ignoreCase:caseSensitive not. |
|
6536 |
(matcher matches:self) ifTrue:[ |
|
6537 |
^ matcher subexpressions |
|
6538 |
]. |
|
6539 |
"/ no match |
|
6540 |
^ nil. |
|
6541 |
||
6542 |
" |
|
6543 |
'I am Tim Dalton' subExpressionsInRegex:'I am (.+)' caseSensitive:false |
|
6544 |
'i am Boris Johnson' subExpressionsInRegex:'I am (.+)' caseSensitive:false |
|
6545 |
'you are Boris Johnson' subExpressionsInRegex:'(i|you) (am|are) (.+)' caseSensitive:false |
|
6546 |
" |
|
6547 |
! ! |
|
24557 | 6548 |
|
9192 | 6549 |
!CharacterArray methodsFor:'padded copying'! |
6550 |
||
6551 |
centerPaddedTo:newSize |
|
19464 | 6552 |
"return a new string consisting of the receiver's characters, |
6553 |
plus spaces up to length and center the receiver's characters in |
|
6554 |
the resulting string. |
|
6555 |
If the receiver's size is equal or greater than the length argument, |
|
6556 |
the original receiver is returned unchanged." |
|
9192 | 6557 |
|
6558 |
^ self centerPaddedTo:newSize with:(Character space) |
|
6559 |
||
6560 |
" |
|
6561 |
'foo' centerPaddedTo:10 |
|
6562 |
123 printString centerPaddedTo:10 |
|
6563 |
" |
|
6564 |
||
6565 |
"Created: 25.11.1995 / 10:53:57 / cg" |
|
6566 |
! |
|
9190 | 6567 |
|
6568 |
centerPaddedTo:size with:padCharacter |
|
6569 |
"return a new string of length size, which contains the receiver |
|
6570 |
centered (i.e. padded on both sides). |
|
6571 |
Characters are filled with padCharacter. |
|
19464 | 6572 |
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
|
6573 |
the original receiver is returned unchanged." |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
6574 |
|
9190 | 6575 |
|len s| |
6576 |
||
6577 |
len := self size. |
|
9192 | 6578 |
(len < size) ifTrue:[ |
20874 | 6579 |
s := self species new:size withAll:padCharacter. |
6580 |
s replaceFrom:(size - len) // 2 + 1 with:self. |
|
6581 |
^ s |
|
9192 | 6582 |
] |
6583 |
||
9190 | 6584 |
" |
6585 |
'foo' centerPaddedTo:11 with:$. |
|
6586 |
'fooBar' centerPaddedTo:5 with:$. |
|
6587 |
123 printString centerPaddedTo:10 with:$. |
|
6588 |
(' ' , 123 printString) centerPaddedTo:10 with:$. |
|
6589 |
(Float pi printString) centerPaddedTo:15 with:(Character space) |
|
6590 |
(Float pi printString) centerPaddedTo:15 with:$- |
|
6591 |
(' ' , Float pi class name) centerPaddedTo:15 with:$. |
|
6592 |
" |
|
815
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
6593 |
! |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
6594 |
|
9190 | 6595 |
decimalPaddedTo:size and:afterPeriod at:decimalCharacter |
815
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
6596 |
"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
|
6597 |
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
|
6598 |
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
|
6599 |
to be padded). |
19464 | 6600 |
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
|
6601 |
the original receiver is returned unchanged. |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
6602 |
(sounds complicated ? -> see examples below)." |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
6603 |
|
8874 | 6604 |
^ self |
20874 | 6605 |
decimalPaddedTo:size |
6606 |
and:afterPeriod |
|
6607 |
at:decimalCharacter |
|
6608 |
withLeft:(Character space) |
|
6609 |
right:$0 |
|
815
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
6610 |
|
8874 | 6611 |
" |
6612 |
'123' decimalPaddedTo:10 and:3 at:$. -> ' 123 ' |
|
6613 |
'123' decimalPaddedTo:10 and:3 at:$. -> ' 123.000' |
|
6614 |
'123.' decimalPaddedTo:10 and:3 at:$. -> ' 123.000' |
|
815
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
6615 |
'123.1' decimalPaddedTo:10 and:3 at:$. -> ' 123.100' |
8874 | 6616 |
'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
|
6617 |
'123.123' decimalPaddedTo:10 and:3 at:$. -> ' 123.123' |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
6618 |
" |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
6619 |
|
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
6620 |
"Created: 23.12.1995 / 13:11:52 / cg" |
9192 | 6621 |
! |
6622 |
||
6623 |
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
|
6624 |
"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
|
6625 |
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
|
6626 |
of the period. |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
6627 |
Characters on the left are filled with leftPadChar. |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
6628 |
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
|
6629 |
otherwise, if missing, a decimal point is added and right characters filled with this. |
19464 | 6630 |
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
|
6631 |
the original receiver is returned unchanged. |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
6632 |
(sounds complicated ? -> see examples below)." |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
6633 |
|
5230
655c21ffe6cc
care for negative size of rest-string when decimalPadding
Claus Gittinger <cg@exept.de>
parents:
5221
diff
changeset
|
6634 |
|s idx n rest| |
815
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
6635 |
|
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
6636 |
idx := self indexOf:decimalCharacter. |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
6637 |
idx == 0 ifTrue:[ |
20874 | 6638 |
"/ |
6639 |
"/ no decimal point found; adjust string to the left of the period column |
|
6640 |
"/ |
|
6641 |
rightPadChar isNil ifTrue:[ |
|
6642 |
s := self , (self species new:afterPeriod + 1 withAll:leftPadChar) |
|
6643 |
] ifFalse:[ |
|
6644 |
s:= self , decimalCharacter asString , (self species new:afterPeriod withAll:rightPadChar). |
|
6645 |
]. |
|
815
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
6646 |
] ifFalse:[ |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
6647 |
|
20874 | 6648 |
"/ the number of after-decimalPoint characters |
6649 |
n := self size - idx. |
|
6650 |
rest := afterPeriod - n. |
|
6651 |
rest > 0 ifTrue:[ |
|
6652 |
s := (self species new:rest withAll:(rightPadChar ? leftPadChar)). |
|
6653 |
] ifFalse:[ |
|
6654 |
s := '' |
|
6655 |
]. |
|
6656 |
s := self , s. |
|
815
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
6657 |
]. |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
6658 |
|
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
6659 |
^ s leftPaddedTo:size with:leftPadChar |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
6660 |
|
8874 | 6661 |
" |
6662 |
'123' decimalPaddedTo:10 and:3 at:$. withLeft:(Character space) right:nil -> ' 123 ' |
|
6663 |
'123' decimalPaddedTo:10 and:3 at:$. withLeft:(Character space) right:$0 -> ' 123.000' |
|
6664 |
'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
|
6665 |
'123.1' decimalPaddedTo:10 and:3 at:$. withLeft:(Character space) right:$0 -> ' 123.100' |
8874 | 6666 |
'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
|
6667 |
'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
|
6668 |
" |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
6669 |
|
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
6670 |
"Modified: 23.12.1995 / 13:08:18 / cg" |
9192 | 6671 |
! |
6672 |
||
6673 |
leftPaddedTo:size |
|
6674 |
"return a new string of length size, which contains the receiver |
|
6675 |
right-adjusted (i.e. padded on the left). |
|
6676 |
Characters on the left are filled with spaces. |
|
19464 | 6677 |
If the receiver's size is equal or greater than the length argument, |
9192 | 6678 |
the original receiver is returned unchanged." |
6679 |
||
6680 |
^ self leftPaddedTo:size with:(Character space) |
|
6681 |
||
6682 |
" |
|
6683 |
'foo' leftPaddedTo:10 |
|
6684 |
'fooBar' leftPaddedTo:5 |
|
6685 |
123 printString leftPaddedTo:10 |
|
6686 |
" |
|
6687 |
! |
|
608 | 6688 |
|
6689 |
paddedTo:newSize |
|
19753 | 6690 |
"return a new string consisting of the receiver's characters, |
23945 | 6691 |
plus spaces up to length. |
6692 |
If the receiver's size is equal or greater than the length argument, |
|
6693 |
the original receiver is returned unchanged." |
|
608 | 6694 |
|
6695 |
^ self paddedTo:newSize with:(Character space) |
|
6696 |
||
6697 |
" |
|
8874 | 6698 |
'foo' paddedTo:10 |
6699 |
123 printString paddedTo:10 |
|
15734 | 6700 |
'12345678901234' paddedTo:10 |
608 | 6701 |
" |
23945 | 6702 |
|
6703 |
"Modified (format): / 21-03-2019 / 12:56:24 / Claus Gittinger" |
|
64 | 6704 |
! ! |
6705 |
||
9192 | 6706 |
!CharacterArray methodsFor:'printing & storing'! |
608 | 6707 |
|
6708 |
article |
|
17137 | 6709 |
"return an article string for the receiver. |
6710 |
The original code only looked at the first character being a vowel; |
|
6711 |
this has been enhanced by some heuristics - not perfect, still." |
|
608 | 6712 |
|
17241 | 6713 |
|firstChar secondChar thirdChar| |
608 | 6714 |
|
8874 | 6715 |
firstChar := (self at:1) asLowercase. |
24041 | 6716 |
((firstChar isVowel and:[firstChar ~~ $u]) |
6717 |
or:[firstChar == $x] |
|
6718 |
) ifTrue:[ |
|
20874 | 6719 |
^ 'an' |
608 | 6720 |
]. |
17043 | 6721 |
|
6722 |
(self size >= 3) ifTrue:[ |
|
20874 | 6723 |
secondChar := (self at:2) asLowercase. |
6724 |
||
6725 |
"/ may need more here... |
|
24041 | 6726 |
(firstChar == $r |
6727 |
and:[secondChar == $b] |
|
6728 |
) ifTrue:[ |
|
20874 | 6729 |
^ 'an' |
6730 |
]. |
|
6731 |
||
6732 |
thirdChar := (self at:3) asLowercase. |
|
6733 |
||
24041 | 6734 |
(firstChar == $s |
6735 |
and:[secondChar == $c |
|
6736 |
and:[thirdChar == $h]] |
|
6737 |
) ifTrue:[ |
|
6738 |
^ 'a' |
|
6739 |
]. |
|
6740 |
||
20874 | 6741 |
(firstChar isVowel not |
6742 |
and:[(secondChar isVowel or:[secondChar == $y]) not |
|
6743 |
and:[thirdChar isVowel not ]]) ifTrue:[ |
|
6744 |
"/ exceptions: 3 non-vowels in a row: looks like an abbreviation |
|
6745 |
(self size > 4) ifTrue:[ |
|
6746 |
(firstChar == $s) ifTrue:[ |
|
6747 |
((secondChar == $c and:[thirdChar == $r]) |
|
6748 |
or:[ (secondChar == $t and:[thirdChar == $r]) ]) ifTrue:[ |
|
6749 |
(self at:4) isVowel ifTrue:[ |
|
6750 |
^ 'a' |
|
6751 |
] |
|
6752 |
] |
|
6753 |
]. |
|
6754 |
]. |
|
6755 |
"/ an abbreviation; treat x, s as vowels |
|
6756 |
(firstChar == $x or:[ firstChar == $s ]) ifTrue:[^ 'an']. |
|
6757 |
] |
|
17043 | 6758 |
]. |
608 | 6759 |
^ 'a' |
5848 | 6760 |
|
6761 |
" |
|
24041 | 6762 |
'uboot' article. |
6763 |
'xmas' article. |
|
6764 |
'alarm' article. |
|
6765 |
'baby' article. |
|
6766 |
'sql' article. |
|
6767 |
'scr' article. |
|
6768 |
'screen' article. |
|
6769 |
'scrollbar' article. |
|
6770 |
'scrs' article. |
|
6771 |
'cvs' article. |
|
6772 |
'cvssource' article. |
|
6773 |
'symbol' article. |
|
6774 |
'string' article. |
|
6775 |
'rbparser' article. |
|
6776 |
'scheme' article. |
|
19683 | 6777 |
" |
6778 |
||
6779 |
"Modified (comment): / 01-05-2016 / 10:57:25 / cg" |
|
24041 | 6780 |
"Modified (comment): / 03-04-2019 / 10:08:57 / Claus Gittinger" |
608 | 6781 |
! |
6782 |
||
8049
ccd0e8f26439
printing - migrating towards unicodeSupport in ST-code
Claus Gittinger <cg@exept.de>
parents:
8040
diff
changeset
|
6783 |
basicStoreString |
ccd0e8f26439
printing - migrating towards unicodeSupport in ST-code
Claus Gittinger <cg@exept.de>
parents:
8040
diff
changeset
|
6784 |
"return a String for storing myself" |
ccd0e8f26439
printing - migrating towards unicodeSupport in ST-code
Claus Gittinger <cg@exept.de>
parents:
8040
diff
changeset
|
6785 |
|
ccd0e8f26439
printing - migrating towards unicodeSupport in ST-code
Claus Gittinger <cg@exept.de>
parents:
8040
diff
changeset
|
6786 |
|s n index| |
ccd0e8f26439
printing - migrating towards unicodeSupport in ST-code
Claus Gittinger <cg@exept.de>
parents:
8040
diff
changeset
|
6787 |
|
ccd0e8f26439
printing - migrating towards unicodeSupport in ST-code
Claus Gittinger <cg@exept.de>
parents:
8040
diff
changeset
|
6788 |
n := self occurrencesOf:$'. |
11320 | 6789 |
n ~~ 0 ifTrue:[ |
20874 | 6790 |
s := self species new:(n + 2 + self size). |
6791 |
s at:1 put:$'. |
|
6792 |
index := 2. |
|
6793 |
self do:[:thisChar | |
|
6794 |
(thisChar == $') ifTrue:[ |
|
6795 |
s at:index put:thisChar. |
|
6796 |
index := index + 1. |
|
6797 |
]. |
|
6798 |
s at:index put:thisChar. |
|
6799 |
index := index + 1. |
|
6800 |
]. |
|
6801 |
s at:index put:$'. |
|
6802 |
^ s |
|
9192 | 6803 |
]. |
11320 | 6804 |
|
9192 | 6805 |
^ '''' , self , '''' |
15523 | 6806 |
|
6807 |
" |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
6808 |
'''immutable'' string' asImmutableString basicStoreString |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
6809 |
'immutable string' asImmutableString basicStoreString |
15523 | 6810 |
" |
6811 |
||
6812 |
"Modified: / 14-07-2013 / 19:20:49 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
9192 | 6813 |
! |
8049
ccd0e8f26439
printing - migrating towards unicodeSupport in ST-code
Claus Gittinger <cg@exept.de>
parents:
8040
diff
changeset
|
6814 |
|
11774
6f6e2b71b607
displayOn (inspector's interface) was broken
Claus Gittinger <cg@exept.de>
parents:
11754
diff
changeset
|
6815 |
displayOn:aGCOrStream |
6f6e2b71b607
displayOn (inspector's interface) was broken
Claus Gittinger <cg@exept.de>
parents:
11754
diff
changeset
|
6816 |
"Compatibility |
6f6e2b71b607
displayOn (inspector's interface) was broken
Claus Gittinger <cg@exept.de>
parents:
11754
diff
changeset
|
6817 |
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
|
6818 |
OR: |
6f6e2b71b607
displayOn (inspector's interface) was broken
Claus Gittinger <cg@exept.de>
parents:
11754
diff
changeset
|
6819 |
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
|
6820 |
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
|
6821 |
(although the fallBack is to display its printString ...)" |
6f6e2b71b607
displayOn (inspector's interface) was broken
Claus Gittinger <cg@exept.de>
parents:
11754
diff
changeset
|
6822 |
|
6f6e2b71b607
displayOn (inspector's interface) was broken
Claus Gittinger <cg@exept.de>
parents:
11754
diff
changeset
|
6823 |
"/ what a kludge - Dolphin and Squeak mean: printOn: a stream; |
21540 | 6824 |
"/ old ST80 means: draw-yourself on a GC. |
6825 |
aGCOrStream isStream ifFalse:[ |
|
6826 |
^ super displayOn:aGCOrStream. |
|
6827 |
]. |
|
6828 |
||
6829 |
self storeOn:aGCOrStream. |
|
6830 |
||
6831 |
"Modified: / 22-02-2017 / 16:59:25 / cg" |
|
11774
6f6e2b71b607
displayOn (inspector's interface) was broken
Claus Gittinger <cg@exept.de>
parents:
11754
diff
changeset
|
6832 |
! |
6f6e2b71b607
displayOn (inspector's interface) was broken
Claus Gittinger <cg@exept.de>
parents:
11754
diff
changeset
|
6833 |
|
608 | 6834 |
printOn:aStream |
6835 |
"print the receiver on aStream" |
|
6836 |
||
6837 |
aStream nextPutAll:self |
|
6838 |
! |
|
6839 |
||
6840 |
printString |
|
13079
111e7e456648
Implement display with #displayOn: instead of #displayString
Stefan Vogel <sv@exept.de>
parents:
13073
diff
changeset
|
6841 |
"return a string for printing - that's myself" |
608 | 6842 |
|
6843 |
^ self |
|
3578 | 6844 |
! |
6845 |
||
25189 | 6846 |
printWithCEscapesOn:aStream |
6847 |
"append the receiver's characters |
|
6848 |
with all special and unprintable characters replaced by \X-character escapes. |
|
6849 |
(similar to the way C-language literal Strings are represented). |
|
6850 |
The resulting string will contain only 7-bit ascii characters. |
|
6851 |
Emphasis is not supported. |
|
6852 |
The following escapes are generated: |
|
6853 |
\' single quote character |
|
6854 |
\dQuote double quote character |
|
6855 |
\b backspace character |
|
6856 |
\r return character |
|
6857 |
\n newline character |
|
6858 |
\t tab character |
|
6859 |
\\ the \ character itself |
|
6860 |
\xnn two digit hex number defining the characters ascii value |
|
6861 |
\unnnn four digit hex number defining the characters ascii value |
|
6862 |
\Unnnnnnnn eight digit hex number defining the characters ascii value |
|
6863 |
||
6864 |
Sigh: this is named completely wrong (opposite naming of withCRs/witoutCRs), |
|
6865 |
but it cannot be changed easily, as these methods are already used heavily |
|
6866 |
" |
|
6867 |
||
6868 |
self hasChangeOfEmphasis ifTrue:[ self error:'emphasis not supported' ]. |
|
6869 |
||
6870 |
self do:[:ch | |
|
6871 |
|cp seq| |
|
6872 |
||
6873 |
(ch == $' or:[ch == $" or:[ch == $\]]) ifTrue:[ |
|
6874 |
aStream nextPut:$\. |
|
6875 |
aStream nextPut:ch. |
|
6876 |
] ifFalse:[ |
|
6877 |
(ch codePoint between:32 and:126) ifTrue:[ |
|
6878 |
aStream nextPut:ch |
|
6879 |
] ifFalse:[ |
|
6880 |
ch == Character return ifTrue:[ |
|
6881 |
seq := '\r' |
|
6882 |
] ifFalse:[ ch == Character nl ifTrue:[ |
|
6883 |
seq := '\n' |
|
6884 |
] ifFalse:[ ch == Character backspace ifTrue:[ |
|
6885 |
seq := '\b' |
|
6886 |
] ifFalse:[ ch == Character tab ifTrue:[ |
|
6887 |
seq := '\t' |
|
6888 |
] ifFalse:[ ch == $\ ifTrue:[ |
|
6889 |
seq := '\\' |
|
6890 |
] ifFalse:[ |
|
6891 |
cp := ch codePoint. |
|
6892 |
cp <= 16rFF ifTrue:[ |
|
6893 |
seq := '\x' , (cp printStringRadix:16 size:2 fill:$0) |
|
6894 |
] ifFalse:[ |
|
6895 |
cp <= 16rFFFF ifTrue:[ |
|
6896 |
seq := '\u' , (cp printStringRadix:16 size:4 fill:$0) |
|
6897 |
] ifFalse:[ |
|
6898 |
seq := '\U',(cp printStringRadix:16 size:8 fill:$0) |
|
6899 |
] |
|
6900 |
] |
|
6901 |
]]]]]. |
|
6902 |
aStream nextPutAll:seq |
|
6903 |
]. |
|
6904 |
]. |
|
6905 |
]. |
|
6906 |
||
6907 |
" |
|
6908 |
'c:\foo\bar\baz' printWithCEscapesOn:Transcript. |
|
6909 |
c'hello\n\tworld' printWithCEscapesOn:Transcript. |
|
6910 |
'hello\b\tworld' withoutCEscapes printWithCEscapesOn:Transcript. |
|
6911 |
'hello\nworld\na\n\tnice\n\t\tstring' withoutCEscapes printWithCEscapesOn:Transcript. |
|
6912 |
('hello ',(Character value:16r1234),' world') printWithCEscapesOn:Transcript |
|
6913 |
" |
|
6914 |
! |
|
6915 |
||
3578 | 6916 |
printWithQuotesDoubledOn:aStream |
6917 |
"put the raw storeString of myself on aStream" |
|
6918 |
||
6919 |
self do:[:thisChar | |
|
20874 | 6920 |
(thisChar == $') ifTrue:[aStream nextPut:thisChar]. |
6921 |
aStream nextPut:thisChar |
|
3578 | 6922 |
] |
6923 |
||
6924 |
"Modified: / 15.6.1998 / 17:21:17 / cg" |
|
6925 |
"Created: / 15.6.1998 / 17:22:13 / cg" |
|
12171 | 6926 |
! |
6927 |
||
15788 | 6928 |
printXmlQuotedOn:aStream |
23549 | 6929 |
"convert aString to a valid XML string |
6930 |
that can be used for XML attributes. |
|
6931 |
Can also be used for XML text, but better use #printXmlTextQuotedOn:. |
|
23551 | 6932 |
Various senders expect, that the output is ISO-8859-1 compatible |
6933 |
(they don't care for encoding), so for now, we always generate 8-bit chars. |
|
23549 | 6934 |
TODO: care for 16bit UNICODE string and escape chars ..." |
6935 |
||
23548 | 6936 |
<resource: #todo> |
15788 | 6937 |
|
6938 |
self do:[:eachChar | |
|
20874 | 6939 |
eachChar == $< ifTrue:[ |
6940 |
aStream nextPutAll:'<' "mapping needed for xml text" |
|
6941 |
] ifFalse:[ eachChar == $& ifTrue:[ |
|
6942 |
aStream nextPutAll:'&' "mapping needed for all" |
|
6943 |
] ifFalse:[ eachChar == $> ifTrue:[ |
|
6944 |
aStream nextPutAll:'>' "mapping needed for comments" |
|
6945 |
] ifFalse:[ eachChar == $' ifTrue:[ |
|
6946 |
aStream nextPutAll:''' "mapping needed for attributes" |
|
6947 |
] ifFalse:[ eachChar == $" ifTrue:[ |
|
6948 |
aStream nextPutAll:'"' "mapping needed for attributes" |
|
6949 |
] ifFalse:[ |
|
6950 |
|codePoint| |
|
6951 |
codePoint := eachChar codePoint. |
|
23552 | 6952 |
(codePoint < 16r20 |
6953 |
or:[codePoint >= 16r7F |
|
6954 |
and:[codePoint <= 16r9F |
|
6955 |
or:[codePoint > 16rFF] "for now, always make sure, that output is ISO-8859-1 compatible"] |
|
6956 |
]) ifTrue:[ |
|
20874 | 6957 |
aStream nextPutAll:'&#'. |
23549 | 6958 |
codePoint printOn:aStream base:16. |
20874 | 6959 |
aStream nextPut:$;. |
23549 | 6960 |
] ifFalse:[ |
6961 |
aStream nextPut:eachChar |
|
6962 |
]. |
|
6963 |
]]]]] |
|
6964 |
]. |
|
6965 |
||
23552 | 6966 |
"Modified: / 03-12-2018 / 15:21:27 / Stefan Vogel" |
15788 | 6967 |
! |
6968 |
||
6969 |
printXmlTextQuotedOn:aStream |
|
23290 | 6970 |
<resource: #todo> |
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
6971 |
"convert aString to a valid XML string |
15788 | 6972 |
that can be used for XML text. |
6973 |
Here line formatting characters are not escaped. |
|
23549 | 6974 |
For XML text (as opposed to XML attribute), $' and $"" need |
6975 |
not to be transliterated (see https://www.w3.org/TR/xml Section 2.4). |
|
23551 | 6976 |
Various senders expect, that the output is ISO-8859-1 compatible |
6977 |
(they don't care for encoding), so for now, we always generate 8-bit chars. |
|
23549 | 6978 |
TODO: care for 16bit UNICODE string and escape chars |
6979 |
- but we need to know the XML file encoding" |
|
15788 | 6980 |
|
6981 |
self do:[:eachChar | |
|
20874 | 6982 |
eachChar == $< ifTrue:[ |
6983 |
aStream nextPutAll:'<' "mapping needed for xml text" |
|
6984 |
] ifFalse:[ eachChar == $& ifTrue:[ |
|
6985 |
aStream nextPutAll:'&' "mapping needed for all" |
|
23290 | 6986 |
] ifFalse:[ eachChar == $> ifTrue:[ |
23549 | 6987 |
aStream nextPutAll:'>' "mapping needed only for ]]>" |
20874 | 6988 |
] ifFalse:[ |
6989 |
|codePoint| |
|
6990 |
codePoint := eachChar codePoint. |
|
6991 |
((codePoint < 16r20 and:[codePoint ~~ 9 and:[codePoint ~~ 10 and:[codePoint ~~ 13]]]) |
|
23552 | 6992 |
or:[codePoint >= 16r7F |
6993 |
and:[codePoint <= 16r9F |
|
6994 |
or:[codePoint > 16rFF] "for now, always make sure, that output is ISO-8859-1 compatible"] |
|
6995 |
]) ifTrue:[ |
|
23321 | 6996 |
aStream nextPutAll:'&#x'. |
23290 | 6997 |
codePoint printOn:aStream base:16. |
20874 | 6998 |
aStream nextPut:$;. |
23549 | 6999 |
] ifFalse:[ |
7000 |
aStream nextPut:eachChar |
|
7001 |
]. |
|
7002 |
]]] |
|
23290 | 7003 |
]. |
7004 |
||
7005 |
" |
|
7006 |
String streamContents:[:s|'< & >' printXmlTextQuotedOn:s] |
|
7007 |
String streamContents:[:s|'< & >',(Character value:7) printXmlTextQuotedOn:s] |
|
7008 |
String streamContents:[:s|'< & >',(Character value:129) printXmlTextQuotedOn:s] |
|
7009 |
String streamContents:[:s|'< & >',(Character value:1000) printXmlTextQuotedOn:s] |
|
7010 |
" |
|
7011 |
||
23321 | 7012 |
"Modified: / 30-08-2018 / 22:39:00 / Claus Gittinger" |
23552 | 7013 |
"Modified: / 03-12-2018 / 15:21:18 / Stefan Vogel" |
15788 | 7014 |
! |
7015 |
||
7016 |
xmlQuotedPrintString |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
7017 |
"convert aString to a valid XML string |
23241 | 7018 |
that can be used for attributes, text, comments an PIs" |
15788 | 7019 |
|
7020 |
^ String streamContents:[:s| |
|
20874 | 7021 |
self printXmlQuotedOn:s |
15788 | 7022 |
]. |
23241 | 7023 |
|
7024 |
"Modified (comment): / 28-07-2018 / 11:11:51 / Claus Gittinger" |
|
9192 | 7025 |
! ! |
7026 |
||
11955 | 7027 |
|
9192 | 7028 |
!CharacterArray methodsFor:'queries'! |
608 | 7029 |
|
23701 | 7030 |
argumentCount |
7031 |
"treating the receiver as a message selector, return how many arguments would it take (ANSI)" |
|
7032 |
||
7033 |
|binopChars| |
|
7034 |
||
7035 |
(self size <= Method maxBinarySelectorSize) ifTrue:[ |
|
7036 |
binopChars := Method binarySelectorCharacters. |
|
7037 |
(self conform:[:eachChar | (binopChars includes:eachChar)]) ifTrue:[ |
|
7038 |
^ 1 |
|
7039 |
]. |
|
7040 |
]. |
|
7041 |
^ self occurrencesOf:$: |
|
7042 |
||
7043 |
" |
|
7044 |
'foo:bar:' argumentCount |
|
7045 |
#foo:bar: argumentCount |
|
7046 |
'hello' argumentCount |
|
7047 |
'+' argumentCount |
|
7048 |
'++' argumentCount |
|
7049 |
'+++' argumentCount |
|
7050 |
'|' argumentCount |
|
7051 |
'?' argumentCount |
|
7052 |
'_' argumentCount |
|
7053 |
'_:' argumentCount |
|
7054 |
'_:_:' argumentCount |
|
7055 |
'<->' argumentCount |
|
7056 |
'<' argumentCount |
|
7057 |
#'<' argumentCount |
|
7058 |
" |
|
7059 |
||
7060 |
"Modified (comment): / 06-02-2017 / 13:48:57 / cg" |
|
7061 |
! |
|
7062 |
||
4429 | 7063 |
bitsPerCharacter |
18662 | 7064 |
"return the underlying string's bitsPerCharacter |
4429 | 7065 |
(i.e. is it a regular String or a TwoByteString)" |
7066 |
||
8457 | 7067 |
|string max| |
7068 |
||
7069 |
(string := self string) ~~ self ifTrue:[ |
|
20874 | 7070 |
^ string bitsPerCharacter |
8457 | 7071 |
]. |
7072 |
||
7073 |
max := 8. |
|
7074 |
self do:[:eachCharacter | |
|
20874 | 7075 |
max := max max:(eachCharacter bitsPerCharacter) |
9192 | 7076 |
]. |
7077 |
^ max |
|
7078 |
||
7079 |
" |
|
7080 |
'hello' bitsPerCharacter |
|
7081 |
'hello' asText allBold bitsPerCharacter |
|
7082 |
" |
|
7083 |
! |
|
8457 | 7084 |
|
19757 | 7085 |
bytesPerCharacter |
23971 | 7086 |
"return the underlying string's bytesPerCharacter |
19757 | 7087 |
(i.e. is it a regular String or a TwoByteString)" |
7088 |
||
7089 |
|string max| |
|
7090 |
||
7091 |
(string := self string) ~~ self ifTrue:[ |
|
20874 | 7092 |
^ string bytesPerCharacter |
19757 | 7093 |
]. |
7094 |
||
7095 |
max := 1. |
|
7096 |
self do:[:eachCharacter | |
|
20874 | 7097 |
max := max max:(eachCharacter bytesPerCharacter) |
19757 | 7098 |
]. |
7099 |
^ max |
|
7100 |
||
7101 |
" |
|
7102 |
'hello' bytesPerCharacter |
|
7103 |
'hello' asUnicode16String bytesPerCharacter |
|
7104 |
'hello' asText allBold bytesPerCharacter |
|
7105 |
" |
|
23971 | 7106 |
|
7107 |
"Modified (comment): / 25-03-2019 / 16:24:02 / Claus Gittinger" |
|
7108 |
! |
|
7109 |
||
7110 |
bytesPerCharacterNeeded |
|
7111 |
"return the actual underlying string's required bytesPerCharacter |
|
7112 |
(i.e. checks if all characters really need that depth)" |
|
7113 |
||
7114 |
|string max mySize| |
|
7115 |
||
7116 |
(string := self string) ~~ self ifTrue:[ |
|
7117 |
^ string bytesPerCharacterNeeded |
|
7118 |
]. |
|
7119 |
||
7120 |
mySize := self bytesPerCharacter. |
|
7121 |
||
7122 |
max := 1. |
|
7123 |
self do:[:eachCharacter | |
|
7124 |
max := max max:(eachCharacter bytesPerCharacter). |
|
7125 |
max == mySize ifTrue:[^ max]. |
|
7126 |
]. |
|
7127 |
^ max |
|
7128 |
||
7129 |
" |
|
7130 |
'hello' bytesPerCharacter -> 1 |
|
7131 |
'hello' bytesPerCharacterNeeded -> 1 |
|
7132 |
||
7133 |
'hello' asUnicode16String bytesPerCharacter -> 2 |
|
7134 |
'hello' asUnicode16String bytesPerCharacterNeeded -> 1 |
|
7135 |
" |
|
7136 |
||
7137 |
"Created: / 25-03-2019 / 16:22:00 / Claus Gittinger" |
|
19757 | 7138 |
! |
7139 |
||
24831 | 7140 |
camelCaseSeparatedWordsDo:aBlock |
7141 |
|state newState in out ch part| |
|
7142 |
||
7143 |
in := self readStream. |
|
7144 |
out := '' writeStream. |
|
7145 |
[in atEnd] whileFalse:[ |
|
7146 |
ch := in next. |
|
7147 |
(ch isDigit or:[ch == $_]) ifFalse:[ |
|
7148 |
newState := ch isUppercase. |
|
7149 |
]. |
|
7150 |
(newState ~~ state) ifTrue:[ |
|
7151 |
newState == true ifTrue:[ |
|
7152 |
"/ going from lower- to uppercase |
|
7153 |
part := out contents. |
|
7154 |
part notEmpty ifTrue:[ aBlock value:part ]. |
|
7155 |
out := '' writeStream. |
|
7156 |
out nextPut:ch. |
|
7157 |
state := newState. |
|
7158 |
] ifFalse:[ |
|
7159 |
"/ going upper- to lowercase |
|
7160 |
out size <= 1 ifTrue:[ |
|
7161 |
out nextPut:ch. |
|
7162 |
] ifFalse:[ |
|
7163 |
|prev| |
|
7164 |
||
7165 |
prev := out contents. |
|
7166 |
aBlock value:(prev copyButLast). |
|
7167 |
out := '' writeStream. |
|
7168 |
out nextPut:prev last. |
|
7169 |
out nextPut:ch. |
|
7170 |
]. |
|
7171 |
state := newState. |
|
7172 |
]. |
|
7173 |
] ifFalse:[ |
|
7174 |
out nextPut:ch. |
|
7175 |
]. |
|
7176 |
]. |
|
7177 |
part := out contents. |
|
7178 |
part notEmpty ifTrue:[ aBlock value:part ]. |
|
7179 |
||
7180 |
" |
|
7181 |
'HelloWorld' camelCaseSeparatedWordsDo:[:w | Transcript showCR:w] |
|
7182 |
'abcDef' camelCaseSeparatedWordsDo:[:w | Transcript showCR:w] |
|
7183 |
'UTFEncoder' camelCaseSeparatedWordsDo:[:w | Transcript showCR:w] |
|
7184 |
'JisEncoder' camelCaseSeparatedWordsDo:[:w | Transcript showCR:w] |
|
7185 |
'JISEncode' camelCaseSeparatedWordsDo:[:w | Transcript showCR:w] |
|
7186 |
" |
|
7187 |
! |
|
7188 |
||
23701 | 7189 |
characterSize |
7190 |
"answer the size in bits of my largest character (actually only 7, 8, 16 or 32)" |
|
7191 |
||
7192 |
|string max |
|
7193 |
sz "{ Class:SmallInteger}" | |
|
7194 |
||
7195 |
(string := self string) ~~ self ifTrue:[ |
|
7196 |
^ string characterSize. |
|
7197 |
]. |
|
7198 |
||
7199 |
sz := self size. |
|
7200 |
max := 7. |
|
7201 |
1 to:sz do:[:idx | |
|
7202 |
|thisSize| |
|
7203 |
||
7204 |
thisSize := (self at:idx) characterSize. |
|
7205 |
thisSize > max ifTrue:[ |
|
7206 |
max := thisSize. |
|
7207 |
max == 32 ifTrue:[ |
|
7208 |
"shortcut: we know, that max size is 32" |
|
7209 |
^ 32. |
|
7210 |
]. |
|
7211 |
]. |
|
7212 |
]. |
|
7213 |
||
7214 |
^ max. |
|
7215 |
||
7216 |
" |
|
7217 |
'hello' characterSize |
|
7218 |
'hello' asUnicode32String characterSize |
|
7219 |
'helloü' asUnicode32String characterSize |
|
7220 |
'hello' asText allBold characterSize |
|
7221 |
" |
|
7222 |
! |
|
7223 |
||
7224 |
contains8BitCharacters |
|
7225 |
<resource: #obsolete> |
|
7226 |
||
7227 |
^ self containsNon7BitAscii |
|
7228 |
! |
|
7229 |
||
7230 |
containsNon7BitAscii |
|
7231 |
"return true, if the underlying string contains 8BitCharacters (or widers) |
|
7232 |
(i.e. if it is non-ascii)" |
|
7233 |
||
7234 |
|string |
|
7235 |
sz "{ Class:SmallInteger }"| |
|
7236 |
||
7237 |
(string := self string) ~~ self ifTrue:[ |
|
7238 |
^ string containsNon7BitAscii |
|
7239 |
]. |
|
7240 |
sz := self size. |
|
7241 |
1 to:sz do:[:idx| |
|
7242 |
(self at:idx) codePoint > 16r7F ifTrue:[ |
|
7243 |
^ true. |
|
7244 |
]. |
|
7245 |
]. |
|
7246 |
^ false. |
|
7247 |
||
7248 |
" |
|
7249 |
'hello' asUnicode32String containsNon7BitAscii |
|
7250 |
'hello üöä' asUnicode32String containsNon7BitAscii |
|
7251 |
'hello' asUnicode32String asText allBold containsNon7BitAscii |
|
7252 |
'hello üö' asUnicode32String asText allBold containsNon7BitAscii |
|
7253 |
" |
|
7254 |
! |
|
7255 |
||
18967 | 7256 |
containsNon8BitElements |
7257 |
"return true, if the underlying string contains elements larger than a single byte" |
|
7258 |
||
21322 | 7259 |
|string sz "{ Class:SmallInteger }"| |
7260 |
||
7261 |
(string := self string) ~~ self ifTrue:[ |
|
7262 |
^ string containsNon8BitElements |
|
7263 |
]. |
|
18967 | 7264 |
sz := self size. |
7265 |
1 to:sz do:[:idx| |
|
20874 | 7266 |
(self at:idx) codePoint > 16rFF ifTrue:[ |
7267 |
^ true. |
|
7268 |
]. |
|
18967 | 7269 |
]. |
7270 |
^ false. |
|
7271 |
! |
|
7272 |
||
10903 | 7273 |
continuesWith:aString startingAt:startIndex |
7274 |
"return true, if the receiver beginning at startIndex |
|
22783 | 7275 |
contains the characters in aString. |
7276 |
Returns false if the search reaches the end of the receiver |
|
7277 |
before all characters have been compared." |
|
10903 | 7278 |
|
7279 |
|sz "{Class: SmallInteger }" |
|
7280 |
idx "{Class: SmallInteger }"| |
|
7281 |
||
7282 |
sz := aString size. |
|
22783 | 7283 |
(startIndex + sz - 1) > self size ifTrue:[^ false]. |
7284 |
||
10903 | 7285 |
idx := startIndex. |
22783 | 7286 |
|
10903 | 7287 |
1 to:sz do:[:i | |
20874 | 7288 |
(self at:idx) ~~ (aString at:i) ifTrue:[^ false]. |
7289 |
idx := idx + 1 |
|
10903 | 7290 |
]. |
7291 |
^ true |
|
7292 |
||
7293 |
" |
|
7294 |
'hello world' continuesWith:'world' startingAt:6 |
|
7295 |
'hello world' continuesWith:'world' startingAt:7 |
|
22783 | 7296 |
'hello' continuesWith:'llo' startingAt:3 |
7297 |
'hello' continuesWith:'llow' startingAt:3 |
|
10903 | 7298 |
" |
7299 |
||
7300 |
"Created: 12.5.1996 / 15:46:40 / cg" |
|
7301 |
"Modified: 26.7.1996 / 19:08:36 / cg" |
|
7302 |
! |
|
7303 |
||
7304 |
countWords |
|
7305 |
"return the number of words, which are separated by separators" |
|
7306 |
||
7307 |
|tally "{ Class: SmallInteger }" |
|
7308 |
start "{ Class: SmallInteger }" |
|
7309 |
mySize "{ Class: SmallInteger }" |
|
7310 |
stop ch| |
|
7311 |
||
7312 |
tally := 0. |
|
7313 |
start := 1. |
|
7314 |
mySize := self size. |
|
7315 |
[start <= mySize] whileTrue:[ |
|
20874 | 7316 |
ch := self at:start. |
7317 |
ch isSeparator ifTrue:[ |
|
7318 |
start := start + 1 |
|
7319 |
] ifFalse:[ |
|
7320 |
stop := self indexOfSeparatorStartingAt:start. |
|
7321 |
(stop == 0) ifTrue:[ |
|
7322 |
stop := mySize + 1 |
|
7323 |
]. |
|
7324 |
tally := tally + 1. |
|
7325 |
start := stop |
|
7326 |
] |
|
10903 | 7327 |
]. |
7328 |
^ tally |
|
7329 |
||
7330 |
" |
|
7331 |
'hello world isnt this nice' countWords' |
|
7332 |
" |
|
7333 |
! |
|
7334 |
||
11788 | 7335 |
defaultElement |
7336 |
^ Character space |
|
7337 |
! |
|
7338 |
||
608 | 7339 |
encoding |
1311 | 7340 |
"return the strings encoding, as a symbol. |
7982 | 7341 |
Here, by default, we assume unicode-encoding. |
20006 | 7342 |
Notice, that ISO-8859-1 is a true subset of unicode, |
8874 | 7343 |
and that singleByteStrings are therefore both unicode AND |
7982 | 7344 |
8859-1 encoded." |
7345 |
||
23290 | 7346 |
^ #'iso10646-1'. |
7347 |
"/ ^ #unicode |
|
7348 |
||
7349 |
"Modified: / 22-08-2018 / 09:30:16 / Claus Gittinger" |
|
608 | 7350 |
! |
7351 |
||
1375
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
7352 |
hasChangeOfEmphasis |
14405
1d0d99a95bba
comment/format in:7 methods
Claus Gittinger <cg@exept.de>
parents:
14168
diff
changeset
|
7353 |
"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
|
7354 |
i.e. any non-normal (=emphasized) characters" |
1d0d99a95bba
comment/format in:7 methods
Claus Gittinger <cg@exept.de>
parents:
14168
diff
changeset
|
7355 |
|
1375
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
7356 |
^ false |
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
7357 |
|
1381 | 7358 |
"Created: 12.5.1996 / 12:31:39 / cg" |
1375
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
7359 |
! |
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
7360 |
|
7995
8ddbb403d67b
+hasIcon and hasImage for LabelAndIcon compatibility
Claus Gittinger <cg@exept.de>
parents:
7991
diff
changeset
|
7361 |
hasIcon |
9191 | 7362 |
"for LabelAndIcon compatibility" |
7363 |
||
9192 | 7364 |
^ false |
7365 |
! |
|
7366 |
||
7367 |
hasImage |
|
7368 |
"for LabelAndIcon compatibility" |
|
7369 |
||
7370 |
^ false |
|
7371 |
! |
|
7995
8ddbb403d67b
+hasIcon and hasImage for LabelAndIcon compatibility
Claus Gittinger <cg@exept.de>
parents:
7991
diff
changeset
|
7372 |
|
1388 | 7373 |
heightOn:aGC |
19464 | 7374 |
"return the size of the receiver in device units if displayed on aGC" |
1388 | 7375 |
|
20221 | 7376 |
^ aGC deviceFont heightOf:self "font is already a device font" |
1388 | 7377 |
|
7378 |
" |
|
8874 | 7379 |
'hello world' heightOn:(View new) |
1388 | 7380 |
" |
7381 |
||
7382 |
"Created: 12.5.1996 / 20:09:29 / cg" |
|
7383 |
"Modified: 12.5.1996 / 20:32:05 / cg" |
|
7384 |
! |
|
7385 |
||
23701 | 7386 |
isAlphaNumeric |
7387 |
"return true, if the receiver is some alphanumeric word; |
|
7388 |
i.e. consists of a letter followed by letters or digits." |
|
7389 |
||
7390 |
self isEmpty ifTrue:[ |
|
7391 |
"mhmh what is this ?" |
|
7392 |
^ false |
|
7393 |
]. |
|
7394 |
(self at:1) isLetter ifFalse:[^ false]. |
|
7395 |
^ self conform:[:char | char isLetterOrDigit]. |
|
7396 |
||
7397 |
" |
|
7398 |
'helloWorld' isAlphaNumeric |
|
7399 |
'foo1234' isAlphaNumeric |
|
7400 |
'f1234' isAlphaNumeric |
|
7401 |
'1234' isAlphaNumeric |
|
7402 |
'+' isAlphaNumeric |
|
7403 |
" |
|
7404 |
||
7405 |
"Modified: / 13-10-2006 / 12:53:49 / cg" |
|
7406 |
! |
|
7407 |
||
7408 |
isBinarySelector |
|
7409 |
"treating the receiver as a message selector, return true if it's a binary selector. |
|
7410 |
Notice, that st/x does not have a size <= 2 limit for unaries" |
|
7411 |
||
7412 |
|binopChars| |
|
7413 |
||
7414 |
(self size > Method maxBinarySelectorSize) ifTrue:[^ false]. |
|
7415 |
||
7416 |
binopChars := Method binarySelectorCharacters. |
|
7417 |
^ (self conform:[:char | (binopChars includes:char)]) |
|
7418 |
||
7419 |
" |
|
7420 |
'foo:bar:' isBinarySelector |
|
7421 |
#foo:bar: isBinarySelector |
|
7422 |
'hello' isBinarySelector |
|
7423 |
'+' isBinarySelector |
|
7424 |
'|' isBinarySelector |
|
7425 |
'?' isBinarySelector |
|
7426 |
':' isBinarySelector |
|
7427 |
'a:' isBinarySelector |
|
7428 |
'->' isBinarySelector |
|
7429 |
'<->' isBinarySelector |
|
7430 |
'::' isBinarySelector |
|
7431 |
" |
|
7432 |
||
7433 |
"Modified: / 04-01-1997 / 14:16:14 / cg" |
|
7434 |
"Modified (comment): / 13-02-2017 / 19:57:29 / cg" |
|
7435 |
! |
|
7436 |
||
7437 |
isBlank |
|
7438 |
"return true, if the receiver contains spaces only" |
|
7439 |
||
7440 |
^ self size == 0 or:[(self contains:[:char | char ~~ Character space]) not] |
|
7441 |
||
7442 |
" |
|
7443 |
'' isBlank |
|
7444 |
'' asUnicode16String isBlank |
|
7445 |
' a ' isBlank |
|
7446 |
' ' isBlank |
|
7447 |
' ' asUnicode16String isBlank |
|
7448 |
" |
|
7449 |
||
7450 |
"Modified (comment): / 14-09-2018 / 10:04:18 / Stefan Vogel" |
|
7451 |
! |
|
7452 |
||
7453 |
isInfix |
|
7454 |
"return true, if the receiver is a binary message selector" |
|
7455 |
||
7456 |
^ self first isLetterOrDigit not |
|
7457 |
||
7458 |
" |
|
7459 |
#at:put: isInfix |
|
7460 |
#at: isInfix |
|
7461 |
#+ isInfix |
|
7462 |
#size isInfix |
|
7463 |
" |
|
7464 |
||
7465 |
"Created: / 1.11.1997 / 12:34:55 / cg" |
|
7466 |
"Modified: / 1.11.1997 / 12:36:37 / cg" |
|
7467 |
! |
|
7468 |
||
7469 |
isKeyword |
|
7470 |
"return true, if the receiver is a keyword message selector. |
|
7471 |
This is a quick check, which only looks at the last character. |
|
7472 |
Should only be used, if we already know that the receiver forms a valid selector. |
|
7473 |
To check an arbitrary string, use isKeywordSelector. |
|
7474 |
Bad naming, but compatibility is asking for it." |
|
7475 |
||
7476 |
^ self last == $: |
|
7477 |
||
7478 |
" |
|
7479 |
#at:put: isKeyword |
|
7480 |
#at: isKeyword |
|
7481 |
#+ isKeyword |
|
7482 |
#size isKeyword |
|
7483 |
" |
|
7484 |
||
7485 |
"Created: / 01-11-1997 / 12:34:55 / cg" |
|
7486 |
"Modified (comment): / 30-04-2016 / 18:19:11 / cg" |
|
7487 |
! |
|
7488 |
||
7489 |
isKeywordSelector |
|
7490 |
"return true, iff there are only alphanumeric or underline characters separated by colons. |
|
7491 |
Must end with a colon. |
|
7492 |
You can use this to check an arbitrary string for being valid as a keyword. |
|
7493 |
If you have a valid selector at hand, and need to know if it is a keyword or not, |
|
7494 |
use #isKeyword, which is much faster." |
|
7495 |
||
7496 |
|state| |
|
7497 |
||
7498 |
(self size == 0) ifTrue:[^ false]. |
|
7499 |
(self last == $:) ifFalse:[^ false]. |
|
7500 |
||
7501 |
state := #initial. |
|
7502 |
self do:[:char | |
|
7503 |
(state == #initial or:[ state == #gotColon]) ifTrue:[ |
|
7504 |
(char isLetterOrUnderline) ifFalse:[^ false]. |
|
7505 |
state := #gotCharacter. |
|
7506 |
] ifFalse:[ |
|
7507 |
char == $: ifTrue:[ |
|
7508 |
state := #gotColon. |
|
7509 |
] ifFalse:[ |
|
24254 | 7510 |
(char isLetterOrDigitOrUnderline) ifFalse:[^ false]. |
23701 | 7511 |
]. |
7512 |
]. |
|
7513 |
]. |
|
7514 |
^ state == #gotColon. |
|
7515 |
||
7516 |
" |
|
7517 |
self assert:(':' isKeywordSelector not). |
|
7518 |
self assert:(':a' isKeywordSelector not). |
|
7519 |
self assert:('1:' isKeywordSelector not). |
|
7520 |
self assert:('a:' isKeywordSelector). |
|
7521 |
self assert:('_:' isKeywordSelector). |
|
7522 |
self assert:('_a:' isKeywordSelector). |
|
7523 |
self assert:('_1:' isKeywordSelector). |
|
7524 |
self assert:('_1::' isKeywordSelector not). |
|
7525 |
self assert:('_:_:' isKeywordSelector). |
|
7526 |
self assert:('a:b:' isKeywordSelector). |
|
7527 |
self assert:('aa:bb:' isKeywordSelector). |
|
7528 |
self assert:('aa:bb:a' isKeywordSelector not). |
|
7529 |
self assert:('1:2:' isKeywordSelector not). |
|
7530 |
" |
|
7531 |
||
7532 |
"Modified (comment): / 30-04-2016 / 18:20:14 / cg" |
|
24254 | 7533 |
"Modified: / 05-06-2019 / 17:05:53 / Claus Gittinger" |
23701 | 7534 |
! |
7535 |
||
11828 | 7536 |
isLowercaseFirst |
7537 |
"return true, if the first character is a lowercase character." |
|
7538 |
||
7539 |
^ self size ~~ 0 and:[ self first isLowercase ] |
|
7540 |
||
7541 |
" |
|
12177 | 7542 |
'helloWorld' isLowercaseFirst |
7543 |
'HelloWorld' isLowercaseFirst |
|
11828 | 7544 |
" |
7545 |
! |
|
7546 |
||
23701 | 7547 |
isNameSpaceSelector |
7548 |
"Answer true if the receiver contains chars which form a nameSpace selector name. |
|
7549 |
These are of the form ':<ns>::<sel>', where ns is the NameSpace and sel is the regular selector. |
|
7550 |
For example, the #+ selector as seen by the Foo namespace would be actually #':Foo::+'. |
|
7551 |
This special format (a symbol starting with a colon) was chosen, because almost every other selector |
|
7552 |
is legal, and this can be checked quickly by just looking at the first character. |
|
7553 |
You cannot easily change this algorithm here, as it is also known by the VM's lookup function." |
|
7554 |
||
7555 |
(self at:1) == $: ifFalse:[^ false]. |
|
24120 | 7556 |
^ (self indexOfSubCollection:'::' startingAt:3 ifAbsent:0 caseSensitive:true) ~~ 0. |
23701 | 7557 |
|
7558 |
"test: |
|
7559 |
self assert:('+' isNameSpaceSelector) not. |
|
7560 |
self assert:(':+' isNameSpaceSelector) not. |
|
7561 |
self assert:(':Foo:+' isNameSpaceSelector) not. |
|
7562 |
||
7563 |
self assert:(':Foo::+' isNameSpaceSelector). |
|
7564 |
self assert:(':Foo::bar:baz:' isNameSpaceSelector). |
|
7565 |
" |
|
7566 |
||
7567 |
"Created: / 05-03-2007 / 11:35:31 / cg" |
|
24120 | 7568 |
"Modified: / 08-05-2019 / 14:41:30 / Stefan Vogel" |
23701 | 7569 |
! |
7570 |
||
7571 |
isNumeric |
|
7572 |
"return true, if the receiver is some numeric word; |
|
7573 |
i.e. consists only of digits." |
|
7574 |
||
7575 |
self size == 0 ifTrue:[ |
|
7576 |
^ false |
|
7577 |
]. |
|
7578 |
^ self conform:[:char | char isDigit] |
|
7579 |
||
7580 |
" |
|
7581 |
'helloWorld' isNumeric |
|
7582 |
'foo1234' isNumeric |
|
7583 |
'f1234' isNumeric |
|
7584 |
'1234' isNumeric |
|
7585 |
'+' isNumeric |
|
7586 |
" |
|
7587 |
||
7588 |
"Modified: / 13-10-2006 / 12:54:12 / cg" |
|
7589 |
! |
|
7590 |
||
11828 | 7591 |
isUppercaseFirst |
7592 |
"return true, if the first character is an uppercase character." |
|
7593 |
||
7594 |
^ self size ~~ 0 and:[ self first isUppercase ] |
|
7595 |
||
7596 |
" |
|
12177 | 7597 |
'helloWorld' isUppercaseFirst |
7598 |
'HelloWorld' isUppercaseFirst |
|
11828 | 7599 |
" |
7600 |
! |
|
7601 |
||
23701 | 7602 |
isValidSmalltalkIdentifier |
7603 |
"return true, if the receiver's characters make up a valid smalltalk identifier" |
|
7604 |
||
7605 |
|scanner tok| |
|
7606 |
||
7607 |
scanner := Compiler new. |
|
7608 |
scanner source:(self readStream). |
|
7609 |
ParseError handle:[:ex | |
|
7610 |
tok := nil. |
|
7611 |
] do:[ |
|
7612 |
tok := scanner nextToken. |
|
7613 |
]. |
|
7614 |
tok ~~ #Identifier ifTrue:[ |
|
7615 |
^ false |
|
7616 |
]. |
|
7617 |
scanner tokenPosition == 1 ifFalse:[^ false]. |
|
7618 |
^ scanner sourceStream atEnd. |
|
7619 |
||
7620 |
" |
|
7621 |
'foo' isValidSmalltalkIdentifier |
|
7622 |
'1foo' isValidSmalltalkIdentifier |
|
7623 |
'_foo' isValidSmalltalkIdentifier |
|
7624 |
'_foo_bar_' isValidSmalltalkIdentifier |
|
7625 |
'foo ' isValidSmalltalkIdentifier |
|
7626 |
' foo' isValidSmalltalkIdentifier |
|
7627 |
" |
|
7628 |
! |
|
7629 |
||
7630 |
isWhitespace |
|
7631 |
"return true, if the receiver is empty or contains only whitespace." |
|
7632 |
||
7633 |
^ (self indexOfNonSeparatorStartingAt:1) == 0 |
|
7634 |
||
7635 |
" |
|
7636 |
'' isWhitespace |
|
7637 |
' ' isWhitespace |
|
7638 |
' \ \' withCRs isWhitespace |
|
7639 |
' a\ \' withCRs isWhitespace |
|
7640 |
' \ \a' withCRs isWhitespace |
|
7641 |
'a \ \a' withCRs isWhitespace |
|
7642 |
" |
|
7643 |
||
7644 |
"Created: / 01-03-2017 / 15:24:53 / cg" |
|
7645 |
! |
|
7646 |
||
15773 | 7647 |
keywords |
7648 |
"assuming the receiver is a keyword message selector, |
|
7649 |
return the individual keywords (i.e. break it up at colons) |
|
7650 |
and return these as a collection. |
|
7651 |
For binary and unary selectors, the result may be nonsense (an array containing the receiver)." |
|
7652 |
||
7653 |
|coll s part| |
|
7654 |
||
7655 |
coll := OrderedCollection new. |
|
7656 |
s := ReadStream on:self. |
|
7657 |
[s atEnd] whileFalse:[ |
|
20874 | 7658 |
part := s through:$:. |
7659 |
coll add:part |
|
15773 | 7660 |
]. |
7661 |
^ coll asArray |
|
7662 |
||
7663 |
" |
|
7664 |
#at:put: keywords |
|
7665 |
#at: keywords |
|
7666 |
#+ keywords |
|
7667 |
#size keywords |
|
7668 |
" |
|
7669 |
||
7670 |
"Modified (Comment): / 30-06-2011 / 17:46:21 / cg" |
|
7671 |
! |
|
7672 |
||
9190 | 7673 |
knownAsSymbol |
7674 |
"for now, only single character strings are allowed as symbols. |
|
7675 |
This method is redefined in String." |
|
7676 |
||
7677 |
^ false |
|
7678 |
! |
|
7679 |
||
7680 |
leftIndent |
|
23663 | 7681 |
"if the receiver starts with whiteSpace, return the number of whiteSpace chars |
9190 | 7682 |
at the left - otherwise, return 0. |
23663 | 7683 |
If the receiver consists of whiteSpace only, return the receiver's size." |
9192 | 7684 |
|
7685 |
|index "{Class: SmallInteger }" |
|
7686 |
end "{Class: SmallInteger }"| |
|
7687 |
||
7688 |
index := 1. |
|
7689 |
end := self size. |
|
7690 |
[index <= end] whileTrue:[ |
|
20874 | 7691 |
(self at:index) isSeparator ifFalse:[^ index - 1]. |
7692 |
index := index + 1 |
|
8350 | 7693 |
]. |
9192 | 7694 |
^ end |
7695 |
||
7696 |
" |
|
7697 |
' hello' leftIndent |
|
7698 |
'foo ' leftIndent |
|
9190 | 7699 |
' ' leftIndent |
23663 | 7700 |
((Character tab),(Character tab),'foo') leftIndent |
7701 |
" |
|
7702 |
||
7703 |
"Modified: / 20-04-1996 / 19:28:43 / cg" |
|
7704 |
"Modified (comment): / 05-02-2019 / 11:22:57 / Claus Gittinger" |
|
8350 | 7705 |
! |
7706 |
||
10437 | 7707 |
nameSpaceSelectorParts |
7708 |
"Answer the namespace and baseSelector parts of a namespace selector. |
|
7709 |
Namespace selectors are those generated by sends from a method in a different |
|
7710 |
namespace; they are prefixed by ':'<ns>'::'. |
|
7711 |
You cannot easily change this algorithm here, as it is also known by the VM's lookup function. |
|
7712 |
Experimental" |
|
7713 |
||
7714 |
|nsPart selPart idx| |
|
7715 |
||
7716 |
(self at:1) == $: ifFalse:[^ Array with:'' with:self]. |
|
7717 |
idx := self indexOf:$: startingAt:3. |
|
7718 |
idx == 0 ifTrue:[^ Array with:'' with:self]. |
|
7719 |
||
7720 |
(idx+2 > self size) ifTrue:[^ Array with:'' with:self]. |
|
7721 |
(self at:idx+1) == $: ifFalse:[^ Array with:'' with:self]. |
|
7722 |
nsPart := self copyFrom:2 to:idx-1. |
|
7723 |
selPart := self copyFrom:idx+2. |
|
7724 |
^ Array with:nsPart with:selPart |
|
7725 |
||
7726 |
"test: |
|
10703
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
7727 |
self assert:('+' nameSpaceSelectorParts) = #('' '+'). |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
7728 |
self assert:(':+' nameSpaceSelectorParts) = #('' ':+'). |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
7729 |
self assert:(':Foo:+' nameSpaceSelectorParts) = #('' ':Foo:+'). |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
7730 |
|
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
7731 |
self assert:(':Foo::+' nameSpaceSelectorParts) = #('Foo' '+'). |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
7732 |
self assert:(':Foo::bar:baz:' nameSpaceSelectorParts) = #('Foo' 'bar:baz:'). |
10437 | 7733 |
" |
7734 |
||
7735 |
"Created: / 05-03-2007 / 17:16:58 / cg" |
|
7736 |
"Modified: / 06-03-2007 / 11:51:15 / cg" |
|
7737 |
! |
|
7738 |
||
23701 | 7739 |
numArgs |
7740 |
<resource: #obsolete> |
|
7741 |
"treating the receiver as a message selector, return how many arguments would it take. |
|
7742 |
Please use argumentCount for ANSI compatibility." |
|
7743 |
||
7744 |
^ self argumentCount |
|
7745 |
! |
|
7746 |
||
7747 |
partsIfSelector |
|
7748 |
"treat the receiver as a message selector, return a collection of parts. |
|
7749 |
Notice: this is more tolerant than Smalltalk's syntax would suggest; |
|
7750 |
especially it allows for empty keyword parts between colons. |
|
7751 |
This is not (and should not be checked here), to allow parsing of |
|
7752 |
degenerate selectors as appearing with objectiveC." |
|
7753 |
||
7754 |
|idx1 "{ Class: SmallInteger }" |
|
7755 |
coll idx2 sz| |
|
7756 |
||
7757 |
coll := OrderedCollection new. |
|
7758 |
idx1 := 1. |
|
7759 |
sz := self size. |
|
7760 |
[ |
|
7761 |
idx2 := self indexOf:$: startingAt:idx1. |
|
7762 |
(idx2 == 0 or:[idx2 == sz]) ifTrue:[ |
|
7763 |
coll add:(self copyFrom:idx1). |
|
7764 |
^ coll |
|
7765 |
]. |
|
7766 |
coll add:(self copyFrom:idx1 to:idx2). |
|
7767 |
idx1 := idx2 + 1 |
|
7768 |
] loop. |
|
7769 |
||
7770 |
" |
|
7771 |
'foo:' partsIfSelector |
|
7772 |
'foo:bar:' partsIfSelector |
|
7773 |
'foo::::' partsIfSelector |
|
7774 |
#foo:bar: partsIfSelector |
|
7775 |
'hello' partsIfSelector |
|
7776 |
'+' partsIfSelector |
|
7777 |
" |
|
7778 |
! |
|
7779 |
||
21283 | 7780 |
speciesForSubcollection |
7781 |
"answer the class, when splitting instances into subcollections" |
|
7782 |
||
7783 |
^ StringCollection |
|
7784 |
||
7785 |
"Created: / 24-01-2017 / 18:54:18 / stefan" |
|
7786 |
! |
|
7787 |
||
9190 | 7788 |
stringSpecies |
8874 | 7789 |
"return the underlying strings bitsPerCharacter |
8037
8cce25b1059c
care for wide chars in asXXXFirst
Claus Gittinger <cg@exept.de>
parents:
8032
diff
changeset
|
7790 |
(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
|
7791 |
|
8cce25b1059c
care for wide chars in asXXXFirst
Claus Gittinger <cg@exept.de>
parents:
8032
diff
changeset
|
7792 |
|string| |
8cce25b1059c
care for wide chars in asXXXFirst
Claus Gittinger <cg@exept.de>
parents:
8032
diff
changeset
|
7793 |
|
15523 | 7794 |
string := self string. |
7795 |
string == self ifTrue:[^ self species]. |
|
8037
8cce25b1059c
care for wide chars in asXXXFirst
Claus Gittinger <cg@exept.de>
parents:
8032
diff
changeset
|
7796 |
^ string stringSpecies |
8cce25b1059c
care for wide chars in asXXXFirst
Claus Gittinger <cg@exept.de>
parents:
8032
diff
changeset
|
7797 |
|
8cce25b1059c
care for wide chars in asXXXFirst
Claus Gittinger <cg@exept.de>
parents:
8032
diff
changeset
|
7798 |
" |
8874 | 7799 |
'hello' stringSpecies |
9192 | 7800 |
'hello' asText allBold stringSpecies |
7801 |
" |
|
7802 |
! |
|
8037
8cce25b1059c
care for wide chars in asXXXFirst
Claus Gittinger <cg@exept.de>
parents:
8032
diff
changeset
|
7803 |
|
6653 | 7804 |
widthFrom:startIndex to:endIndex on:aGC |
19464 | 7805 |
"return ths size of part of the receiver in device units if displayed on aGC" |
6653 | 7806 |
|
20221 | 7807 |
^ aGC deviceFont widthOf:self from:startIndex to:endIndex |
6653 | 7808 |
|
7809 |
" |
|
8874 | 7810 |
'hello world' widthFrom:1 to:5 on:(View new) |
7811 |
'hello' widthOn:(View new) |
|
6653 | 7812 |
" |
9192 | 7813 |
! |
6653 | 7814 |
|
1388 | 7815 |
widthOn:aGC |
19464 | 7816 |
"return ths size of the receiver in device units if displayed on aGC" |
1388 | 7817 |
|
20221 | 7818 |
^ aGC deviceFont widthOf:self "font is already a device font" |
1388 | 7819 |
|
7820 |
" |
|
8874 | 7821 |
'hello world' widthOn:(View new) |
1388 | 7822 |
" |
7823 |
||
7824 |
"Created: 12.5.1996 / 20:09:29 / cg" |
|
2554 | 7825 |
"Modified: 17.4.1997 / 12:50:23 / cg" |
608 | 7826 |
! ! |
7827 |
||
25204 | 7828 |
|
608 | 7829 |
!CharacterArray methodsFor:'special string converting'! |
7830 |
||
16759 | 7831 |
asUnixFilenameString |
7832 |
"return a new string consisting of receiver's characters |
|
7833 |
with all \-characters replaced by /-characters. |
|
7834 |
If there are no backslashes, return the original" |
|
7835 |
||
7836 |
^ self copyReplaceAll:$\ with:$/ ifNone:self |
|
7837 |
||
7838 |
" |
|
7839 |
'hello\world' asUnixFilenameString |
|
7840 |
" |
|
7841 |
||
7842 |
"Modified: / 18.7.1998 / 22:53:02 / cg" |
|
7843 |
! |
|
7844 |
||
24411 | 7845 |
expandNumericPlaceholdersWith:argArrayOrDictionary |
7846 |
"return a copy of the receiver, where all %i escapes with numeric keys are |
|
7847 |
replaced by corresponding arguments' printStrings from the argArrayOrDictionary. |
|
7848 |
I.e. 'hello %1; how is %2' expandPlaceholdersWith:#('world' 'this') results |
|
7849 |
in the new string 'hello world; how is this'. |
|
7850 |
||
7851 |
This will ignore all non-numeric keys (and leave them as is)." |
|
7852 |
||
7853 |
|stream| |
|
7854 |
||
7855 |
stream := (TextStream ? CharacterWriteStream) on:(self species uninitializedNew:self size + 20). |
|
7856 |
"/ self expandPlaceholdersWith:argArrayOrDictionary on:stream. |
|
7857 |
self |
|
7858 |
expandPlaceholders:$% with:argArrayOrDictionary |
|
7859 |
ignoreNumericEscapes:false |
|
7860 |
ignoreNonNumericEscapes:true |
|
7861 |
ignoreSpecialEscapes:true |
|
7862 |
requireParentheses:true |
|
24541 | 7863 |
ifKeyAbsent:nil |
24411 | 7864 |
on:stream. |
7865 |
^ stream contents. |
|
7866 |
||
7867 |
" |
|
7868 |
'hello %1' expandNumericPlaceholdersWith:#('world') |
|
24541 | 7869 |
'hello %(1)' expandNumericPlaceholdersWith:#('world') |
7870 |
'hello %(10)' expandNumericPlaceholdersWith:#('world') |
|
24411 | 7871 |
'hello %1 %abc' expandNumericPlaceholdersWith:#('world') |
7872 |
'hello %1 %(abc)' expandNumericPlaceholdersWith:#('world') |
|
7873 |
" |
|
7874 |
||
7875 |
" |
|
7876 |
'hello %1' expandPlaceholdersWith:#('world') |
|
24774 | 7877 |
'hello %1 %abc' expandNumericPlaceholdersWith:#('world' 'nononono') |
7878 |
'hello %1 %abc' expandPlaceholdersWith:#('world' 'nononono') |
|
7879 |
'hello %1 %(abc)' expandNumericPlaceholdersWith:#('world' 'nononono') |
|
7880 |
'hello %1 %(abc)' expandPlaceholdersWith:#('world' 'nononono') |
|
24411 | 7881 |
" |
7882 |
||
7883 |
"Modified: / 01-07-1997 / 00:53:24 / cg" |
|
7884 |
"Modified: / 14-07-2018 / 09:23:31 / Claus Gittinger" |
|
24774 | 7885 |
"Modified (comment): / 09-09-2019 / 13:58:20 / Stefan Vogel" |
24411 | 7886 |
! |
7887 |
||
24541 | 7888 |
expandPlaceholders |
7889 |
"return a copy of the receiver, where %<special> escapes are expanded. |
|
7890 |
%<..> |
|
7891 |
Insert a character constant or character sequence, being one of: |
|
7892 |
cr nl tab return lf crlf ff null backspace bell esc newPage space |
|
7893 |
i.e. you can use %<cr> to insert a CR, and %<tab> to insert a TAB. |
|
7894 |
||
7895 |
See also bindWith:... for VisualAge compatibility." |
|
7896 |
||
7897 |
^ self expandPlaceholdersWith:nil |
|
7898 |
||
7899 |
" |
|
7900 |
'hello a%<crlf>b' expandPlaceholders |
|
7901 |
" |
|
7902 |
||
7903 |
"Modified: / 01-07-1997 / 00:53:24 / cg" |
|
7904 |
"Modified: / 14-07-2018 / 09:23:31 / Claus Gittinger" |
|
7905 |
! |
|
7906 |
||
14768 | 7907 |
expandPlaceholders:escapeCharacter with:argArrayOrDictionary |
24541 | 7908 |
"this is a more general version of the old %-escaping method, allowing for an arbitrary |
14768 | 7909 |
escape character to be used (typically $$ or $% are effectively used). |
24407 | 7910 |
|
24541 | 7911 |
Returns a copy of the receiver, where all %i escapes are |
14768 | 7912 |
replaced by corresponding arguments' printStrings from the argArrayOrDictionary. |
7913 |
I.e. 'hello %1; how is %2' expandPlaceholdersWith:#('world' 'this') results |
|
7914 |
in the new string 'hello world; how is this'. |
|
7915 |
As an extension, the argument may also be a dictionary, providing |
|
7916 |
values for symbolic keys. |
|
7917 |
In this case, %a .. %z and %(...) are also allowed. |
|
7918 |
(%1..%9 require a numeric key in the dictionary, however) |
|
7919 |
To get a '%' character, use a '%%'-escape. |
|
24717 | 7920 |
To get an integer-indexed placeHolder followed by another digit, you must use %(digit). |
14768 | 7921 |
See also bindWith:... for VisualAge compatibility." |
7922 |
||
7923 |
|stream| |
|
7924 |
||
18269 | 7925 |
stream := (TextStream ? CharacterWriteStream) on:(self species uninitializedNew:self size + 20). |
24410 | 7926 |
self expandPlaceholders:escapeCharacter with:argArrayOrDictionary |
7927 |
ignoreNumericEscapes:false |
|
7928 |
ignoreNonNumericEscapes:false |
|
7929 |
ignoreSpecialEscapes:false |
|
7930 |
requireParentheses:true |
|
24541 | 7931 |
ifKeyAbsent:nil |
24410 | 7932 |
on:stream. |
14768 | 7933 |
^ stream contents. |
7934 |
||
7935 |
" |
|
7936 |
'hello %1' expandPlaceholdersWith:#('world') |
|
24843 | 7937 |
'hello %1' expandPlaceholders:$% with:#('world') |
7938 |
'hello %1' expandPlaceholders:$$ with:#('world') |
|
7939 |
'hello $1' expandPlaceholders:$$ with:#('world') |
|
14768 | 7940 |
'hello %1; how is %2' expandPlaceholdersWith:#('world' 'this') |
7941 |
'hello %2; how is %1' expandPlaceholdersWith:#('world' 'this') |
|
7942 |
'%1 plus %2 gives %3 ' expandPlaceholdersWith:#(4 5 9) |
|
7943 |
'%%(1)0 gives %(1)0' expandPlaceholdersWith:#(123) |
|
7944 |
'%%10 gives %10' expandPlaceholdersWith:#(123) |
|
7945 |
'%%(10) gives %(10)' expandPlaceholdersWith:#(123) |
|
7946 |
'%test gives %1' expandPlaceholdersWith:#(123) |
|
7947 |
'bla %1 bla' expandPlaceholdersWith:{ 'hello' allBold } |
|
7948 |
'bla %1 bla' expandPlaceholdersWith:{ 'hello' } |
|
7949 |
" |
|
7950 |
||
7951 |
" |
|
7952 |
|dict| |
|
7953 |
||
7954 |
dict := Dictionary new. |
|
7955 |
dict at:1 put:'one'. |
|
7956 |
dict at:$a put:'AAAAA'. |
|
7957 |
dict at:$b put:[ Time now ]. |
|
7958 |
dict at:'foo' put:[ Date today ]. |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
7959 |
'hello $1 %a $b %(foo) $foo ' expandPlaceholders:$$ with:dict. |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
7960 |
'hello $1 %a $b %(foo) $foo ' expandPlaceholders:$% with:dict. |
14768 | 7961 |
" |
7962 |
||
23067 | 7963 |
"Modified: / 01-07-1997 / 00:53:24 / cg" |
7964 |
"Modified: / 14-06-2018 / 11:46:18 / Claus Gittinger" |
|
7965 |
! |
|
7966 |
||
24410 | 7967 |
expandPlaceholders:escapeCharacter with:argArrayOrDictionary |
7968 |
ignoreNumericEscapes:ignoreNumericEscapes |
|
7969 |
ignoreNonNumericEscapes:ignoreNonNumericEscapes |
|
7970 |
ignoreSpecialEscapes:ignoreSpecialEscapes |
|
7971 |
requireParentheses:requireParentheses |
|
24407 | 7972 |
"this is the generic version of the old %-escaping method, allowing for an arbitrary |
7973 |
escape character to be used (typically $$ or $% are effectively used). |
|
7974 |
||
7975 |
Return a copy of the receiver, where all %i escapes are |
|
7976 |
replaced by corresponding arguments' printStrings from the argArray. |
|
7977 |
I.e. 'hello %1; how is %2' expandPlaceholdersWith:#('world' 'this') results |
|
7978 |
in the new string 'hello world; how is this'. |
|
7979 |
||
7980 |
As an extension, the argument may also be a dictionary, providing values for symbolic keys. |
|
7981 |
In this case, %a .. %z and %(...) are also allowed. |
|
7982 |
(%1..%9 require a numeric key in the dictionary, however) |
|
7983 |
||
7984 |
Also, values in argArrayOrDictionary may be blocks. |
|
7985 |
||
7986 |
To get a '%' character, use a '%%'-escape. |
|
24717 | 7987 |
To get an integer-indexed placeHolder followed by another digit, you must use %(digit). |
24407 | 7988 |
|
7989 |
See also bindWith:... for VisualAge compatibility. |
|
7990 |
Use %<cr> to insert a CR and %<tab> to insert a TAB. |
|
7991 |
||
7992 |
ignoreNumericEscapes controls if %<nr> escapes are expanded or not. |
|
7993 |
This is required for Windows batch-script expansion, where %<nr> should be left unchanged. |
|
7994 |
||
7995 |
requireParentheses controls if $abc is allowed or not. |
|
7996 |
If true, multi-character replacements need to be parenthized as $(abc); |
|
7997 |
if false, you can also write $abc. |
|
7998 |
" |
|
7999 |
||
8000 |
|stream| |
|
8001 |
||
8002 |
stream := (TextStream ? CharacterWriteStream) on:(self species uninitializedNew:self size + 20). |
|
24410 | 8003 |
self |
8004 |
expandPlaceholders:escapeCharacter with:argArrayOrDictionary |
|
8005 |
ignoreNumericEscapes:ignoreNumericEscapes |
|
8006 |
ignoreNonNumericEscapes:ignoreNonNumericEscapes |
|
8007 |
ignoreSpecialEscapes:ignoreSpecialEscapes |
|
8008 |
requireParentheses:requireParentheses |
|
24541 | 8009 |
ifKeyAbsent:nil |
8010 |
on:stream. |
|
8011 |
^ stream contents |
|
8012 |
||
8013 |
" |
|
8014 |
'hello %1' expandPlaceholders:$% with:#('world') on:Transcript. |
|
8015 |
'hello %a%<cr>' expandPlaceholders:$% |
|
8016 |
with:(Dictionary new at:'a' put:'world';yourself) |
|
8017 |
on:Transcript. |
|
8018 |
'hello %aa%<cr>' expandPlaceholders:$% with:(Dictionary new at:'a' put:'world';yourself) on:Transcript. |
|
8019 |
'hello %(aa)%<cr>' expandPlaceholders:$% with:(Dictionary new at:'aa' put:'world';yourself) on:Transcript. |
|
8020 |
'hello %aa%<cr>' expandPlaceholders:$% with:(Dictionary new at:'aa' put:'world';yourself) ignoreNumericEscapes:true requireParentheses:false on:Transcript. |
|
8021 |
||
8022 |
String streamContents:[:s| |
|
8023 |
'hello %1' expandPlaceholders:$% with:#('world') on:s. |
|
8024 |
s cr. |
|
8025 |
'hello $1; how is $2' expandPlaceholders:$$ with:#('world' 'this') on:s. |
|
8026 |
s cr. |
|
8027 |
'hello %2; how is %1' expandPlaceholders:$% with:#('world' 'this') on:s. |
|
8028 |
s cr. |
|
8029 |
'%1 plus %2 gives %3 ' expandPlaceholders:$% with:#(4 5 9) on:s. |
|
8030 |
s cr. |
|
8031 |
'%%(1)0 gives %(1)0' expandPlaceholders:$% with:#(123) on:s. |
|
8032 |
s cr. |
|
8033 |
'%%10 gives %10' expandPlaceholders:$% with:#(123) on:s. |
|
8034 |
s cr. |
|
8035 |
'%%(10) gives %(10) %<cr>%<tab>next line' expandPlaceholders:$% with:#(123) on:s. |
|
8036 |
s cr. |
|
8037 |
'%%test gives %test' expandPlaceholders:$% with:#(123) on:s. |
|
8038 |
s cr. |
|
8039 |
'|%%<tab>|%%1|%%<cr>| gives |%<tab>|%1|%<cr>|' expandPlaceholders:$% with:#(foo) on:s. |
|
8040 |
] |
|
8041 |
" |
|
8042 |
||
8043 |
"without xlation dictionary: |
|
8044 |
'hello %1' expandPlaceholdersWith:nil. |
|
8045 |
'hello%<cr> %1' expandPlaceholdersWith:nil. |
|
8046 |
" |
|
8047 |
||
8048 |
" |
|
8049 |
|dict| |
|
8050 |
||
8051 |
dict := Dictionary new. |
|
8052 |
dict at:1 put:'one'. |
|
8053 |
dict at:$a put:'AAAAA'. |
|
8054 |
dict at:$b put:[ Time now ]. |
|
8055 |
String streamContents:[:s| |
|
8056 |
'hello $1 $a $b' expandPlaceholders:$$ with:dict on:s. |
|
8057 |
]. |
|
8058 |
" |
|
8059 |
||
8060 |
"using blocks: |
|
8061 |
|dict| |
|
8062 |
||
8063 |
dict := Dictionary new. |
|
8064 |
dict at:'time' put:[Time now printString]. |
|
8065 |
dict at:'date' put:[Date today printString]. |
|
8066 |
String streamContents:[:s| |
|
8067 |
'it is $(time) $(date)' expandPlaceholders:$$ with:dict on:s. |
|
8068 |
]. |
|
8069 |
" |
|
8070 |
||
8071 |
"Created: / 14-01-2019 / 17:43:03 / Claus Gittinger" |
|
8072 |
"Modified: / 05-06-2019 / 17:05:47 / Claus Gittinger" |
|
8073 |
! |
|
8074 |
||
8075 |
expandPlaceholders:escapeCharacter with:argArrayOrDictionary |
|
8076 |
ignoreNumericEscapes:ignoreNumericEscapes |
|
8077 |
ignoreNonNumericEscapes:ignoreNonNumericEscapes |
|
8078 |
ignoreSpecialEscapes:ignoreSpecialEscapes |
|
8079 |
requireParentheses:requireParentheses |
|
8080 |
ifKeyAbsent:ifKeyAbsentBlockOrNil |
|
8081 |
||
8082 |
"this is the generic version of the old %-escaping method, allowing for an arbitrary |
|
8083 |
escape character to be used (typically $$ or $% are effectively used). |
|
8084 |
||
8085 |
Return a copy of the receiver, where all %i escapes are |
|
8086 |
replaced by corresponding arguments' printStrings from the argArray. |
|
8087 |
I.e. 'hello %1; how is %2' expandPlaceholdersWith:#('world' 'this') results |
|
8088 |
in the new string 'hello world; how is this'. |
|
8089 |
||
8090 |
As an extension, the argument may also be a dictionary, providing values for symbolic keys. |
|
8091 |
In this case, %a .. %z and %(...) are also allowed. |
|
8092 |
(%1..%9 require a numeric key in the dictionary, however) |
|
8093 |
||
8094 |
Also, values in argArrayOrDictionary may be blocks. |
|
8095 |
||
8096 |
To get a '%' character, use a '%%'-escape. |
|
24717 | 8097 |
To get an integer-indexed placeHolder followed by another digit, you must use %(digit). |
24541 | 8098 |
|
8099 |
See also bindWith:... for VisualAge compatibility. |
|
8100 |
Use %<cr> to insert a CR and %<tab> to insert a TAB. |
|
8101 |
||
8102 |
ignoreNumericEscapes controls if %<nr> escapes are expanded or not. |
|
8103 |
This is required for Windows batch-script expansion, where %<nr> should be left unchanged. |
|
8104 |
||
8105 |
requireParentheses controls if $abc is allowed or not. |
|
8106 |
If true, multi-character replacements need to be parenthized as $(abc); |
|
8107 |
if false, you can also write $abc. |
|
8108 |
" |
|
8109 |
||
8110 |
|stream| |
|
8111 |
||
8112 |
stream := (TextStream ? CharacterWriteStream) on:(self species uninitializedNew:self size + 20). |
|
8113 |
self |
|
8114 |
expandPlaceholders:escapeCharacter with:argArrayOrDictionary |
|
8115 |
ignoreNumericEscapes:ignoreNumericEscapes |
|
8116 |
ignoreNonNumericEscapes:ignoreNonNumericEscapes |
|
8117 |
ignoreSpecialEscapes:ignoreSpecialEscapes |
|
8118 |
requireParentheses:requireParentheses |
|
8119 |
ifKeyAbsent:ifKeyAbsentBlockOrNil |
|
24410 | 8120 |
on:stream. |
24407 | 8121 |
^ stream contents |
8122 |
||
8123 |
" |
|
8124 |
'hello %1' expandPlaceholders:$% with:#('world') on:Transcript. |
|
8125 |
'hello %a%<cr>' expandPlaceholders:$% |
|
8126 |
with:(Dictionary new at:'a' put:'world';yourself) |
|
8127 |
on:Transcript. |
|
8128 |
'hello %aa%<cr>' expandPlaceholders:$% with:(Dictionary new at:'a' put:'world';yourself) on:Transcript. |
|
8129 |
'hello %(aa)%<cr>' expandPlaceholders:$% with:(Dictionary new at:'aa' put:'world';yourself) on:Transcript. |
|
8130 |
'hello %aa%<cr>' expandPlaceholders:$% with:(Dictionary new at:'aa' put:'world';yourself) ignoreNumericEscapes:true requireParentheses:false on:Transcript. |
|
8131 |
||
8132 |
String streamContents:[:s| |
|
8133 |
'hello %1' expandPlaceholders:$% with:#('world') on:s. |
|
8134 |
s cr. |
|
8135 |
'hello $1; how is $2' expandPlaceholders:$$ with:#('world' 'this') on:s. |
|
8136 |
s cr. |
|
8137 |
'hello %2; how is %1' expandPlaceholders:$% with:#('world' 'this') on:s. |
|
8138 |
s cr. |
|
8139 |
'%1 plus %2 gives %3 ' expandPlaceholders:$% with:#(4 5 9) on:s. |
|
8140 |
s cr. |
|
8141 |
'%%(1)0 gives %(1)0' expandPlaceholders:$% with:#(123) on:s. |
|
8142 |
s cr. |
|
8143 |
'%%10 gives %10' expandPlaceholders:$% with:#(123) on:s. |
|
8144 |
s cr. |
|
8145 |
'%%(10) gives %(10) %<cr>%<tab>next line' expandPlaceholders:$% with:#(123) on:s. |
|
8146 |
s cr. |
|
8147 |
'%%test gives %test' expandPlaceholders:$% with:#(123) on:s. |
|
8148 |
s cr. |
|
8149 |
'|%%<tab>|%%1|%%<cr>| gives |%<tab>|%1|%<cr>|' expandPlaceholders:$% with:#(foo) on:s. |
|
8150 |
] |
|
8151 |
" |
|
8152 |
||
8153 |
"without xlation dictionary: |
|
8154 |
'hello %1' expandPlaceholdersWith:nil. |
|
8155 |
'hello%<cr> %1' expandPlaceholdersWith:nil. |
|
8156 |
" |
|
8157 |
||
8158 |
" |
|
8159 |
|dict| |
|
8160 |
||
8161 |
dict := Dictionary new. |
|
8162 |
dict at:1 put:'one'. |
|
8163 |
dict at:$a put:'AAAAA'. |
|
8164 |
dict at:$b put:[ Time now ]. |
|
8165 |
String streamContents:[:s| |
|
8166 |
'hello $1 $a $b' expandPlaceholders:$$ with:dict on:s. |
|
8167 |
]. |
|
8168 |
" |
|
8169 |
||
8170 |
"using blocks: |
|
8171 |
|dict| |
|
8172 |
||
8173 |
dict := Dictionary new. |
|
8174 |
dict at:'time' put:[Time now printString]. |
|
8175 |
dict at:'date' put:[Date today printString]. |
|
8176 |
String streamContents:[:s| |
|
8177 |
'it is $(time) $(date)' expandPlaceholders:$$ with:dict on:s. |
|
8178 |
]. |
|
8179 |
" |
|
8180 |
||
8181 |
"Created: / 14-01-2019 / 17:43:03 / Claus Gittinger" |
|
8182 |
"Modified: / 05-06-2019 / 17:05:47 / Claus Gittinger" |
|
8183 |
! |
|
8184 |
||
24410 | 8185 |
expandPlaceholders:escapeCharacter with:argArrayOrDictionary |
8186 |
ignoreNumericEscapes:ignoreNumericEscapes |
|
8187 |
ignoreNonNumericEscapes:ignoreNonNumericEscapes |
|
8188 |
ignoreSpecialEscapes:ignoreSpecialEscapes |
|
24541 | 8189 |
requireParentheses:requireParentheses |
8190 |
ifKeyAbsent:replaceActionOrNil |
|
24410 | 8191 |
on:aStream |
8192 |
||
8193 |
"this is the central method for %-escaping, allowing for an arbitrary |
|
8194 |
escape character to be used (typically $$ or $% are effectively used). |
|
8195 |
||
8196 |
Write the receiver to aStream, where all %i escapes are |
|
8197 |
replaced by corresponding arguments' printStrings from the argArray. |
|
8198 |
I.e. 'hello %1; how is %2' expandPlaceholdersWith:#('world' 'this') results |
|
8199 |
in the new string 'hello world; how is this'. |
|
8200 |
||
8201 |
As an extension, the argument may also be a dictionary, providing values for symbolic keys. |
|
8202 |
In this case, %a .. %z and %(...) are also allowed. |
|
8203 |
(%1..%9 require a numeric key in the dictionary, however) |
|
8204 |
||
8205 |
Also, values in argArrayOrDictionary may be blocks. |
|
8206 |
||
8207 |
To get a '%' character, use a '%%'-escape. |
|
24555 | 8208 |
To get an integer-indexed placeHolder followed by another digit, you must use %(digit). |
24410 | 8209 |
|
8210 |
See also bindWith:... for VisualAge compatibility. |
|
24541 | 8211 |
|
8212 |
- %<..> |
|
8213 |
Insert a character constant or character sequence, being one of: |
|
8214 |
cr nl tab return lf crlf ff null backspace bell esc newPage space |
|
8215 |
i.e. you can use %<cr> to insert a CR, and %<tab> to insert a TAB. |
|
8216 |
||
8217 |
- ignoreNumericEscapes |
|
8218 |
controls if %<nr> escapes are expanded or not. |
|
8219 |
This is required for Windows batch-script expansion, where %<nr> should be left unchanged. |
|
8220 |
||
8221 |
- ignoreSpecialEscapes |
|
8222 |
controls if control characters like %<cr> are expanded or not. |
|
8223 |
||
8224 |
- requireParentheses |
|
8225 |
controls if $abc is allowed or not. |
|
8226 |
If true, multi-character replacements need to be parenthized as $(abc), |
|
8227 |
and the above is interpreted as $(a)bc |
|
8228 |
If false, you can also write $abc. |
|
8229 |
||
8230 |
- keepIfNoSuchKey |
|
8231 |
controls what should happen if a variable/index is encountered which is not found in argArrayOrDictionary. |
|
8232 |
It can be nil or a two arg block. |
|
8233 |
If nil, the sequence is replaced by an empty string (i.e. 'abc$(foo)def' -> 'abcdef') |
|
8234 |
if aBlock, it will be called with both the full escape sequence and the cariable only as arguments, |
|
8235 |
and the expansion will be what the block returns. |
|
8236 |
i.e. if the block is [:meta :name | meta], then the above will result in 'abc$(foo)def' |
|
8237 |
and if the block is [:meta :name | name], then the above will result in 'abcfoodef' |
|
8238 |
Usefull if you want to expand a string twice, without loosing the key-sequences in the first place. |
|
8239 |
Notice: for supid backward compatibility, keepIfNoSuchKey is not applied for %X sequences, where X is a single letter. |
|
24410 | 8240 |
" |
8241 |
||
8242 |
|next v key numericKey |
|
8243 |
idx "{ SmallInteger }" |
|
8244 |
idx2 "{ SmallInteger }" |
|
8245 |
start "{ SmallInteger }" |
|
24541 | 8246 |
stop "{ SmallInteger }" |
8247 |
noReplacementAction| |
|
8248 |
||
8249 |
replaceActionOrNil isNil ifTrue:[ |
|
8250 |
noReplacementAction := [:seq :var | ''] |
|
8251 |
] ifFalse:[ |
|
8252 |
noReplacementAction := replaceActionOrNil |
|
8253 |
]. |
|
24410 | 8254 |
|
8255 |
stop := self size. |
|
8256 |
start := 1. |
|
8257 |
[start <= stop] whileTrue:[ |
|
8258 |
idx := self indexOf:escapeCharacter startingAt:start. |
|
8259 |
(idx == 0 or:[idx == stop]) ifTrue:[ |
|
8260 |
aStream nextPutAll:self startingAt:start to:stop. |
|
8261 |
^ self. |
|
8262 |
]. |
|
8263 |
"found an escapeCharacter at idx" |
|
8264 |
idx > start ifTrue:[ |
|
8265 |
aStream nextPutAll:self startingAt:start to:(idx - 1). |
|
8266 |
]. |
|
8267 |
next := self at:(idx + 1). |
|
8268 |
(next == escapeCharacter) ifTrue:[ |
|
8269 |
"doubled escapeCharacter" |
|
8270 |
aStream nextPut:escapeCharacter. |
|
8271 |
] ifFalse:[ |
|
8272 |
"%<...> " |
|
8273 |
next == $< ifTrue:[ |
|
8274 |
ignoreSpecialEscapes ifTrue:[ |
|
8275 |
aStream nextPut:escapeCharacter. |
|
8276 |
aStream nextPut:next. |
|
8277 |
] ifFalse:[ |
|
8278 |
idx2 := self indexOf:$> startingAt:idx+2. |
|
8279 |
key := self copyFrom:idx+2 to:idx2-1. |
|
8280 |
idx := idx2 - 1. |
|
8281 |
key := key asSymbolIfInterned. |
|
24541 | 8282 |
(#(cr tab nl return lf ff null backspace bell esc newPage space) includesIdentical:key) ifTrue:[ |
24410 | 8283 |
aStream nextPut:(Character perform:key). |
24541 | 8284 |
] ifFalse:[ |
8285 |
(key == #crlf) ifTrue:[ |
|
8286 |
aStream nextPutAll:(String crlf). |
|
8287 |
] ifFalse:[ |
|
8288 |
aStream nextPutAll:key. |
|
8289 |
] |
|
24410 | 8290 |
]. |
8291 |
]. |
|
8292 |
] ifFalse:[ |
|
8293 |
argArrayOrDictionary isNil ifTrue:[ |
|
24541 | 8294 |
"/ %x but no dictionary provided (strange error case, actually) |
24410 | 8295 |
aStream nextPut:next. |
8296 |
] ifFalse:[ |
|
8297 |
(next isDigit and:[ignoreNumericEscapes not]) ifTrue:[ |
|
24555 | 8298 |
"/ %N (N is digit) |
24556 | 8299 |
key := next asString. |
8300 |
numericKey := next digitValue. |
|
24558 | 8301 |
(((idx + 2) <= self size) and:[(next := self at:idx+2) isDigit]) ifTrue:[ |
8302 |
key := key,next. |
|
24556 | 8303 |
idx := idx + 1. |
24558 | 8304 |
[((idx + 2) <= self size) and:[(next := self at:idx+2) isDigit]] whileTrue:[ |
8305 |
key := key,next. |
|
8306 |
idx := idx + 1. |
|
8307 |
]. |
|
8308 |
numericKey := Integer readFrom:key. |
|
24555 | 8309 |
]. |
24541 | 8310 |
v := argArrayOrDictionary |
24555 | 8311 |
at:numericKey |
24558 | 8312 |
ifAbsent:[ noReplacementAction value:escapeCharacter,key value:key ]. |
24410 | 8313 |
] ifFalse:[ |
8314 |
next == $( ifTrue:[ |
|
24541 | 8315 |
"/ %(name) |
24410 | 8316 |
idx2 := self indexOf:$) startingAt:idx+2. |
8317 |
self assert:(idx2 > 0) message:'closing parenthesis missing'. |
|
8318 |
key := self copyFrom:idx+2 to:idx2-1. |
|
24557 | 8319 |
idx := idx2-1. |
24410 | 8320 |
|
8321 |
(key isNumeric |
|
8322 |
and:[(numericKey := Integer readFrom:key onError:nil) notNil]) |
|
8323 |
ifTrue:[ |
|
8324 |
ignoreNumericEscapes ifTrue:[ |
|
8325 |
v := escapeCharacter,'(',key,')' |
|
8326 |
] ifFalse:[ |
|
24541 | 8327 |
v := argArrayOrDictionary |
8328 |
at:numericKey |
|
8329 |
ifAbsent:[ noReplacementAction value:escapeCharacter,'(',key,')' value:key ] |
|
24410 | 8330 |
] |
8331 |
] ifFalse:[ |
|
8332 |
ignoreNonNumericEscapes ifTrue:[ |
|
8333 |
v := escapeCharacter,'(',key,')' |
|
8334 |
] ifFalse:[ |
|
8335 |
argArrayOrDictionary isBlock ifTrue:[ |
|
8336 |
v := argArrayOrDictionary value:key |
|
8337 |
] ifFalse:[ |
|
8338 |
(argArrayOrDictionary includesKey:key) ifTrue:[ |
|
8339 |
v := argArrayOrDictionary at:key |
|
8340 |
] ifFalse:[ |
|
8341 |
key := key asSymbolIfInternedOrSelf. |
|
8342 |
(argArrayOrDictionary includesKey:key) ifTrue:[ |
|
8343 |
v := argArrayOrDictionary at:key |
|
8344 |
] ifFalse:[ |
|
8345 |
(key size == 1 and:[ argArrayOrDictionary includesKey:(key at:1)]) ifTrue:[ |
|
8346 |
v := argArrayOrDictionary at:(key at:1) |
|
8347 |
] ifFalse:[ |
|
24541 | 8348 |
v := noReplacementAction value:escapeCharacter,'(',key,')' value:key |
24410 | 8349 |
] |
8350 |
]. |
|
8351 |
]. |
|
8352 |
]. |
|
8353 |
]. |
|
8354 |
]. |
|
8355 |
] ifFalse:[ |
|
8356 |
(ignoreNonNumericEscapes not |
|
8357 |
and:[ next isLetter |
|
8358 |
and:[ argArrayOrDictionary isSequenceable not "is a Dictionary"]] |
|
8359 |
) ifTrue:[ |
|
24541 | 8360 |
"%X (X is letter)" |
24410 | 8361 |
requireParentheses ifTrue:[ |
8362 |
key := next. |
|
8363 |
] ifFalse:[ |
|
8364 |
idx2 := self findFirst:[:ch | (ch isLetterOrDigitOrUnderline) not] startingAt:idx+2. |
|
8365 |
idx2 == 0 ifTrue:[idx2 := self size + 1]. |
|
8366 |
key := self copyFrom:idx+1 to:idx2-1. |
|
8367 |
idx := idx2 - 2. |
|
8368 |
]. |
|
24556 | 8369 |
|
24410 | 8370 |
argArrayOrDictionary isBlock ifTrue:[ |
8371 |
v := argArrayOrDictionary value:key |
|
8372 |
] ifFalse:[ |
|
8373 |
v := argArrayOrDictionary |
|
8374 |
at:key |
|
8375 |
ifAbsent:[ |
|
8376 |
"try symbol or string instead of character" |
|
8377 |
argArrayOrDictionary |
|
8378 |
at:key asString asSymbolIfInternedOrSelf |
|
24541 | 8379 |
ifAbsent:[ |
8380 |
escapeCharacter asString , key |
|
8381 |
]. |
|
24410 | 8382 |
]. |
8383 |
]. |
|
8384 |
] ifFalse:[ |
|
8385 |
v := String with:escapeCharacter with:next. |
|
8386 |
]. |
|
8387 |
] |
|
8388 |
]. |
|
8389 |
||
8390 |
v isBlock ifTrue:[ |
|
8391 |
v := v value |
|
8392 |
]. |
|
8393 |
v printOn:aStream. |
|
8394 |
] |
|
8395 |
]. |
|
8396 |
]. |
|
8397 |
start := idx + 2 |
|
8398 |
]. |
|
8399 |
||
8400 |
" |
|
8401 |
'hello %1' expandPlaceholders:$% with:#('world') on:Transcript. |
|
8402 |
'hello %a%<cr>' expandPlaceholders:$% |
|
8403 |
with:(Dictionary new at:'a' put:'world';yourself) |
|
8404 |
on:Transcript. |
|
8405 |
'hello %aa%<cr>' expandPlaceholders:$% with:(Dictionary new at:'a' put:'world';yourself) on:Transcript. |
|
8406 |
'hello %(aa)%<cr>' expandPlaceholders:$% with:(Dictionary new at:'aa' put:'world';yourself) on:Transcript. |
|
8407 |
'hello %aa%<cr>' expandPlaceholders:$% with:(Dictionary new at:'aa' put:'world';yourself) ignoreNumericEscapes:true requireParentheses:false on:Transcript. |
|
8408 |
||
24556 | 8409 |
'hello %10%9%8' expandPlaceholders:$% with:{ 'x1' . 'x2' . 'x3' . 'x4' . 'x5' . 'x6' . 'x7' . 'x8' . 'x9' . 'x10' } |
24555 | 8410 |
'hello %10' expandPlaceholders:$% with:{ 'x1' . 'x2' . 'x3' . 'x4' . 'x5' . 'x6' . 'x7' . 'x8' . 'x9' . 'x10' } |
8411 |
||
24410 | 8412 |
String streamContents:[:s| |
8413 |
'hello %1' expandPlaceholders:$% with:#('world') on:s. |
|
8414 |
s cr. |
|
8415 |
'hello $1; how is $2' expandPlaceholders:$$ with:#('world' 'this') on:s. |
|
8416 |
s cr. |
|
8417 |
'hello %2; how is %1' expandPlaceholders:$% with:#('world' 'this') on:s. |
|
8418 |
s cr. |
|
8419 |
'%1 plus %2 gives %3 ' expandPlaceholders:$% with:#(4 5 9) on:s. |
|
8420 |
s cr. |
|
8421 |
'%%(1)0 gives %(1)0' expandPlaceholders:$% with:#(123) on:s. |
|
8422 |
s cr. |
|
8423 |
'%%10 gives %10' expandPlaceholders:$% with:#(123) on:s. |
|
8424 |
s cr. |
|
8425 |
'%%(10) gives %(10) %<cr>%<tab>next line' expandPlaceholders:$% with:#(123) on:s. |
|
8426 |
s cr. |
|
8427 |
'%%test gives %test' expandPlaceholders:$% with:#(123) on:s. |
|
8428 |
s cr. |
|
8429 |
'|%%<tab>|%%1|%%<cr>| gives |%<tab>|%1|%<cr>|' expandPlaceholders:$% with:#(foo) on:s. |
|
8430 |
] |
|
8431 |
" |
|
8432 |
||
8433 |
"without xlation dictionary: |
|
8434 |
'hello %1' expandPlaceholdersWith:nil. |
|
8435 |
'hello%<cr> %1' expandPlaceholdersWith:nil. |
|
8436 |
" |
|
8437 |
||
8438 |
" |
|
8439 |
|dict| |
|
8440 |
||
8441 |
dict := Dictionary new. |
|
8442 |
dict at:1 put:'one'. |
|
8443 |
dict at:$a put:'AAAAA'. |
|
8444 |
dict at:$b put:[ Time now ]. |
|
8445 |
String streamContents:[:s| |
|
8446 |
'hello $1 $a $b' expandPlaceholders:$$ with:dict on:s. |
|
8447 |
]. |
|
8448 |
" |
|
8449 |
||
8450 |
"using blocks: |
|
8451 |
|dict| |
|
8452 |
||
8453 |
dict := Dictionary new. |
|
8454 |
dict at:'time' put:[Time now printString]. |
|
8455 |
dict at:'date' put:[Date today printString]. |
|
8456 |
String streamContents:[:s| |
|
8457 |
'it is $(time) $(date)' expandPlaceholders:$$ with:dict on:s. |
|
8458 |
]. |
|
8459 |
" |
|
8460 |
||
8461 |
"Created: / 14-01-2019 / 17:43:03 / Claus Gittinger" |
|
8462 |
"Modified: / 05-06-2019 / 17:05:47 / Claus Gittinger" |
|
8463 |
! |
|
8464 |
||
24541 | 8465 |
expandPlaceholders:escapeCharacter |
8466 |
with:argArrayOrDictionary |
|
8467 |
ignoreNumericEscapes:ignoreNumericEscapes |
|
8468 |
ignoreNonNumericEscapes:ignoreNonNumericEscapes |
|
8469 |
ignoreSpecialEscapes:ignoreSpecialEscapes |
|
8470 |
requireParentheses:requireParentheses |
|
8471 |
on:aStream |
|
8472 |
||
8473 |
"this is the central method for %-escaping, allowing for an arbitrary |
|
8474 |
escape character to be used (typically $$ or $% are effectively used). |
|
8475 |
||
8476 |
Write the receiver to aStream, where all %i escapes are |
|
8477 |
replaced by corresponding arguments' printStrings from the argArray. |
|
8478 |
I.e. 'hello %1; how is %2' expandPlaceholdersWith:#('world' 'this') results |
|
8479 |
in the new string 'hello world; how is this'. |
|
8480 |
||
8481 |
As an extension, the argument may also be a dictionary, providing values for symbolic keys. |
|
8482 |
In this case, %a .. %z and %(...) are also allowed. |
|
8483 |
(%1..%9 require a numeric key in the dictionary, however) |
|
8484 |
||
8485 |
Also, values in argArrayOrDictionary may be blocks. |
|
8486 |
||
8487 |
To get a '%' character, use a '%%'-escape. |
|
24717 | 8488 |
To get an integer-indexed placeHolder followed by another digit, you must use %(digit). |
24541 | 8489 |
|
8490 |
See also bindWith:... for VisualAge compatibility. |
|
8491 |
Use %<cr> to insert a CR and %<tab> to insert a TAB. |
|
8492 |
||
8493 |
ignoreNumericEscapes controls if %<nr> escapes are expanded or not. |
|
8494 |
This is required for Windows batch-script expansion, where %<nr> should be left unchanged. |
|
8495 |
||
8496 |
ignoreSpecialEscapes controls if control characters like %<cr> are expanded or not. |
|
8497 |
||
8498 |
requireParentheses controls if $abc is allowed or not. |
|
8499 |
If true, multi-character replacements need to be parenthized as $(abc) and the above is |
|
8500 |
interpreted as $(a)bc; |
|
8501 |
if false, you can also write $abc. |
|
8502 |
" |
|
8503 |
||
8504 |
^ self |
|
8505 |
expandPlaceholders:escapeCharacter |
|
8506 |
with:argArrayOrDictionary |
|
8507 |
ignoreNumericEscapes:ignoreNumericEscapes |
|
8508 |
ignoreNonNumericEscapes:ignoreNonNumericEscapes |
|
8509 |
ignoreSpecialEscapes:ignoreSpecialEscapes |
|
8510 |
requireParentheses:requireParentheses |
|
8511 |
ifKeyAbsent:nil |
|
8512 |
on:aStream |
|
8513 |
! |
|
8514 |
||
24410 | 8515 |
expandPlaceholders:escapeCharacter with:argArrayOrDictionary |
8516 |
ignoreNumericEscapes:ignoreNumericEscapes |
|
8517 |
on:aStream |
|
8518 |
"this is the generic version of the old %-escaping method, allowing for an arbitrary |
|
8519 |
escape character to be used (typically $$ or $% are effectively used). |
|
8520 |
||
8521 |
Write the receiver to aStream, where all %i escapes are |
|
8522 |
replaced by corresponding arguments' printStrings from the argArray. |
|
8523 |
I.e. 'hello %1; how is %2' expandPlaceholdersWith:#('world' 'this') results |
|
8524 |
in the new string 'hello world; how is this'. |
|
8525 |
||
8526 |
As an extension, the argument may also be a dictionary, providing values for symbolic keys. |
|
8527 |
In this case, %a .. %z and %(...) are also allowed. |
|
8528 |
(%1..%9 require a numeric key in the dictionary, however) |
|
8529 |
||
8530 |
Also, values in argArrayOrDictionary may be blocks. |
|
8531 |
||
8532 |
To get a '%' character, use a '%%'-escape. |
|
24717 | 8533 |
To get an integer-indexed placeHolder followed by another digit, you must use %(digit). |
24410 | 8534 |
|
8535 |
See also bindWith:... for VisualAge compatibility. |
|
8536 |
Use %<cr> to insert a CR and %<tab> to insert a TAB. |
|
8537 |
||
8538 |
ignoreNumericEscapes controls if %<nr> escapes are expanded or not. |
|
8539 |
This is required for Windows batch-script expansion, where %<nr> should be left |
|
8540 |
unchanged." |
|
8541 |
||
8542 |
^ self |
|
8543 |
expandPlaceholders:escapeCharacter |
|
8544 |
with:argArrayOrDictionary |
|
8545 |
ignoreNumericEscapes:ignoreNumericEscapes |
|
8546 |
ignoreNonNumericEscapes:false |
|
8547 |
ignoreSpecialEscapes:false |
|
8548 |
requireParentheses:true |
|
24541 | 8549 |
ifKeyAbsent:nil |
24410 | 8550 |
on:aStream |
8551 |
||
8552 |
" |
|
8553 |
String streamContents:[:s| |
|
8554 |
'hello %1' expandPlaceholders:$% with:#('world') on:s. |
|
8555 |
s cr. |
|
8556 |
'hello $1; how is $2' expandPlaceholders:$$ with:#('world' 'this') on:s. |
|
8557 |
s cr. |
|
8558 |
'hello %2; how is %1' expandPlaceholders:$% with:#('world' 'this') on:s. |
|
8559 |
s cr. |
|
8560 |
'%1 plus %2 gives %3 ' expandPlaceholders:$% with:#(4 5 9) on:s. |
|
8561 |
s cr. |
|
8562 |
'%%(1)0 gives %(1)0' expandPlaceholders:$% with:#(123) on:s. |
|
8563 |
s cr. |
|
8564 |
'%%10 gives %10' expandPlaceholders:$% with:#(123) on:s. |
|
8565 |
s cr. |
|
8566 |
'%%(10) gives %(10) %<cr>%<tab>next line' expandPlaceholders:$% with:#(123) on:s. |
|
8567 |
s cr. |
|
8568 |
'%%test gives %test' expandPlaceholders:$% with:#(123) on:s. |
|
8569 |
s cr. |
|
8570 |
'|%%<tab>|%%1|%%<cr>| gives |%<tab>|%1|%<cr>|' expandPlaceholders:$% with:#(foo) on:s. |
|
8571 |
] |
|
8572 |
" |
|
8573 |
||
8574 |
"without xlation dictionary: |
|
8575 |
'hello %1' expandPlaceholdersWith:nil. |
|
8576 |
'hello%<cr> %1' expandPlaceholdersWith:nil. |
|
8577 |
" |
|
8578 |
||
8579 |
" |
|
8580 |
|dict| |
|
8581 |
||
8582 |
dict := Dictionary new. |
|
8583 |
dict at:1 put:'one'. |
|
8584 |
dict at:$a put:'AAAAA'. |
|
8585 |
dict at:$b put:[ Time now ]. |
|
25094 | 8586 |
dict at:'foo' put:'FOO'. |
8587 |
dict at:'foo2' put:'FOO\BAR'. |
|
24410 | 8588 |
String streamContents:[:s| |
25094 | 8589 |
'hello $1 $a $b $(foo) $$(foo) $(foo2) ' expandPlaceholders:$$ with:dict on:s. |
8590 |
]. |
|
24410 | 8591 |
" |
8592 |
||
8593 |
"using blocks: |
|
8594 |
|dict| |
|
8595 |
||
8596 |
dict := Dictionary new. |
|
8597 |
dict at:'time' put:[Time now printString]. |
|
8598 |
dict at:'date' put:[Date today printString]. |
|
8599 |
String streamContents:[:s| |
|
8600 |
'it is $(time) $(date)' expandPlaceholders:$$ with:dict on:s. |
|
8601 |
]. |
|
8602 |
" |
|
8603 |
||
8604 |
"Created: / 14-06-2018 / 11:44:08 / Claus Gittinger" |
|
8605 |
"Modified (comment): / 14-01-2019 / 17:44:02 / Claus Gittinger" |
|
8606 |
! |
|
8607 |
||
24541 | 8608 |
expandPlaceholders:escapeCharacter |
8609 |
with:argArrayOrDictionary |
|
24410 | 8610 |
ignoreNumericEscapes:ignoreNumericEscapes |
8611 |
requireParentheses:requireParentheses |
|
24541 | 8612 |
|
8613 |
"this is a more general version of the old %-escaping method, allowing for an arbitrary |
|
24410 | 8614 |
escape character to be used (typically $$ or $% are effectively used). |
8615 |
See also bindWith:... for VisualAge compatibility. |
|
24541 | 8616 |
|
8617 |
- ignoreNumericEscapes |
|
8618 |
controls if %<nr> escapes are expanded or not. |
|
8619 |
This is required for Windows batch-script expansion, where %<nr> should be left unchanged. |
|
8620 |
||
8621 |
- requireParentheses |
|
8622 |
controls if $abc is allowed or not. |
|
8623 |
If true, multi-character replacements need to be parenthized as $(abc); |
|
8624 |
if false, you can also write $abc. |
|
24410 | 8625 |
" |
8626 |
||
8627 |
|stream| |
|
8628 |
||
8629 |
stream := (TextStream ? CharacterWriteStream) on:(self species uninitializedNew:self size + 20). |
|
24541 | 8630 |
self |
8631 |
expandPlaceholders:escapeCharacter |
|
8632 |
with:argArrayOrDictionary |
|
24410 | 8633 |
ignoreNumericEscapes:ignoreNumericEscapes |
8634 |
ignoreNonNumericEscapes:false |
|
8635 |
ignoreSpecialEscapes:false |
|
24541 | 8636 |
requireParentheses:requireParentheses |
8637 |
ifKeyAbsent:nil |
|
8638 |
on:stream. |
|
24410 | 8639 |
^ stream contents |
8640 |
||
8641 |
" |
|
8642 |
'hello %1' expandPlaceholders:$% with:#('world') on:Transcript. |
|
8643 |
'hello %a%<cr>' expandPlaceholders:$% |
|
8644 |
with:(Dictionary new at:'a' put:'world';yourself) |
|
8645 |
on:Transcript. |
|
8646 |
'hello %aa%<cr>' expandPlaceholders:$% with:(Dictionary new at:'a' put:'world';yourself) on:Transcript. |
|
8647 |
'hello %(aa)%<cr>' expandPlaceholders:$% with:(Dictionary new at:'aa' put:'world';yourself) on:Transcript. |
|
8648 |
'hello %aa%<cr>' expandPlaceholders:$% with:(Dictionary new at:'aa' put:'world';yourself) ignoreNumericEscapes:true requireParentheses:false on:Transcript. |
|
8649 |
||
8650 |
String streamContents:[:s| |
|
8651 |
'hello %1' expandPlaceholders:$% with:#('world') on:s. |
|
8652 |
s cr. |
|
8653 |
'hello $1; how is $2' expandPlaceholders:$$ with:#('world' 'this') on:s. |
|
8654 |
s cr. |
|
8655 |
'hello %2; how is %1' expandPlaceholders:$% with:#('world' 'this') on:s. |
|
8656 |
s cr. |
|
8657 |
'%1 plus %2 gives %3 ' expandPlaceholders:$% with:#(4 5 9) on:s. |
|
8658 |
s cr. |
|
8659 |
'%%(1)0 gives %(1)0' expandPlaceholders:$% with:#(123) on:s. |
|
8660 |
s cr. |
|
8661 |
'%%10 gives %10' expandPlaceholders:$% with:#(123) on:s. |
|
8662 |
s cr. |
|
8663 |
'%%(10) gives %(10) %<cr>%<tab>next line' expandPlaceholders:$% with:#(123) on:s. |
|
8664 |
s cr. |
|
8665 |
'%%test gives %test' expandPlaceholders:$% with:#(123) on:s. |
|
8666 |
s cr. |
|
8667 |
'|%%<tab>|%%1|%%<cr>| gives |%<tab>|%1|%<cr>|' expandPlaceholders:$% with:#(foo) on:s. |
|
8668 |
] |
|
8669 |
" |
|
8670 |
||
8671 |
"without xlation dictionary: |
|
8672 |
'hello %1' expandPlaceholdersWith:nil. |
|
8673 |
'hello%<cr> %1' expandPlaceholdersWith:nil. |
|
8674 |
" |
|
8675 |
||
8676 |
" |
|
8677 |
|dict| |
|
8678 |
||
8679 |
dict := Dictionary new. |
|
8680 |
dict at:1 put:'one'. |
|
8681 |
dict at:$a put:'AAAAA'. |
|
8682 |
dict at:$b put:[ Time now ]. |
|
8683 |
String streamContents:[:s| |
|
8684 |
'hello $1 $a $b' expandPlaceholders:$$ with:dict on:s. |
|
8685 |
]. |
|
8686 |
" |
|
8687 |
||
8688 |
"using blocks: |
|
8689 |
|dict| |
|
8690 |
||
8691 |
dict := Dictionary new. |
|
8692 |
dict at:'time' put:[Time now printString]. |
|
8693 |
dict at:'date' put:[Date today printString]. |
|
8694 |
String streamContents:[:s| |
|
8695 |
'it is $(time) $(date)' expandPlaceholders:$$ with:dict on:s. |
|
8696 |
]. |
|
8697 |
" |
|
8698 |
||
8699 |
"Created: / 14-01-2019 / 17:43:03 / Claus Gittinger" |
|
8700 |
"Modified: / 05-06-2019 / 17:05:47 / Claus Gittinger" |
|
8701 |
! |
|
8702 |
||
8703 |
expandPlaceholders:escapeCharacter with:argArrayOrDictionary |
|
8704 |
ignoreNumericEscapes:ignoreNumericEscapes |
|
8705 |
requireParentheses:requireParentheses |
|
8706 |
on:aStream |
|
8707 |
||
23603 | 8708 |
"this is the generic version of the old %-escaping method, allowing for an arbitrary |
8709 |
escape character to be used (typically $$ or $% are effectively used). |
|
8710 |
||
8711 |
Write the receiver to aStream, where all %i escapes are |
|
8712 |
replaced by corresponding arguments' printStrings from the argArray. |
|
8713 |
I.e. 'hello %1; how is %2' expandPlaceholdersWith:#('world' 'this') results |
|
8714 |
in the new string 'hello world; how is this'. |
|
8715 |
||
8716 |
As an extension, the argument may also be a dictionary, providing values for symbolic keys. |
|
8717 |
In this case, %a .. %z and %(...) are also allowed. |
|
8718 |
(%1..%9 require a numeric key in the dictionary, however) |
|
8719 |
||
8720 |
Also, values in argArrayOrDictionary may be blocks. |
|
8721 |
||
8722 |
To get a '%' character, use a '%%'-escape. |
|
24717 | 8723 |
To get an integer-indexed placeHolder followed by another digit, you must use %(digit). |
23603 | 8724 |
|
8725 |
See also bindWith:... for VisualAge compatibility. |
|
8726 |
Use %<cr> to insert a CR and %<tab> to insert a TAB. |
|
8727 |
||
8728 |
ignoreNumericEscapes controls if %<nr> escapes are expanded or not. |
|
8729 |
This is required for Windows batch-script expansion, where %<nr> should be left unchanged. |
|
8730 |
||
8731 |
requireParentheses controls if $abc is allowed or not. |
|
8732 |
If true, multi-character replacements need to be parenthized as $(abc); |
|
8733 |
if false, you can also write $abc. |
|
8734 |
" |
|
8735 |
||
24541 | 8736 |
self |
8737 |
expandPlaceholders:escapeCharacter |
|
8738 |
with:argArrayOrDictionary |
|
24410 | 8739 |
ignoreNumericEscapes:ignoreNumericEscapes |
8740 |
ignoreNonNumericEscapes:false |
|
8741 |
ignoreSpecialEscapes:false |
|
8742 |
requireParentheses:requireParentheses |
|
24541 | 8743 |
ifKeyAbsent:nil |
24410 | 8744 |
on:aStream. |
23603 | 8745 |
|
8746 |
" |
|
8747 |
'hello %1' expandPlaceholders:$% with:#('world') on:Transcript. |
|
8748 |
'hello %a%<cr>' expandPlaceholders:$% |
|
8749 |
with:(Dictionary new at:'a' put:'world';yourself) |
|
8750 |
on:Transcript. |
|
8751 |
'hello %aa%<cr>' expandPlaceholders:$% with:(Dictionary new at:'a' put:'world';yourself) on:Transcript. |
|
8752 |
'hello %(aa)%<cr>' expandPlaceholders:$% with:(Dictionary new at:'aa' put:'world';yourself) on:Transcript. |
|
8753 |
'hello %aa%<cr>' expandPlaceholders:$% with:(Dictionary new at:'aa' put:'world';yourself) ignoreNumericEscapes:true requireParentheses:false on:Transcript. |
|
8754 |
||
8755 |
String streamContents:[:s| |
|
8756 |
'hello %1' expandPlaceholders:$% with:#('world') on:s. |
|
8757 |
s cr. |
|
8758 |
'hello $1; how is $2' expandPlaceholders:$$ with:#('world' 'this') on:s. |
|
8759 |
s cr. |
|
8760 |
'hello %2; how is %1' expandPlaceholders:$% with:#('world' 'this') on:s. |
|
8761 |
s cr. |
|
8762 |
'%1 plus %2 gives %3 ' expandPlaceholders:$% with:#(4 5 9) on:s. |
|
8763 |
s cr. |
|
8764 |
'%%(1)0 gives %(1)0' expandPlaceholders:$% with:#(123) on:s. |
|
8765 |
s cr. |
|
8766 |
'%%10 gives %10' expandPlaceholders:$% with:#(123) on:s. |
|
8767 |
s cr. |
|
8768 |
'%%(10) gives %(10) %<cr>%<tab>next line' expandPlaceholders:$% with:#(123) on:s. |
|
8769 |
s cr. |
|
8770 |
'%%test gives %test' expandPlaceholders:$% with:#(123) on:s. |
|
8771 |
s cr. |
|
8772 |
'|%%<tab>|%%1|%%<cr>| gives |%<tab>|%1|%<cr>|' expandPlaceholders:$% with:#(foo) on:s. |
|
8773 |
] |
|
8774 |
" |
|
8775 |
||
8776 |
"without xlation dictionary: |
|
8777 |
'hello %1' expandPlaceholdersWith:nil. |
|
8778 |
'hello%<cr> %1' expandPlaceholdersWith:nil. |
|
8779 |
" |
|
8780 |
||
8781 |
" |
|
8782 |
|dict| |
|
8783 |
||
8784 |
dict := Dictionary new. |
|
8785 |
dict at:1 put:'one'. |
|
8786 |
dict at:$a put:'AAAAA'. |
|
8787 |
dict at:$b put:[ Time now ]. |
|
8788 |
String streamContents:[:s| |
|
8789 |
'hello $1 $a $b' expandPlaceholders:$$ with:dict on:s. |
|
8790 |
]. |
|
8791 |
" |
|
8792 |
||
8793 |
"using blocks: |
|
8794 |
|dict| |
|
8795 |
||
8796 |
dict := Dictionary new. |
|
8797 |
dict at:'time' put:[Time now printString]. |
|
8798 |
dict at:'date' put:[Date today printString]. |
|
8799 |
String streamContents:[:s| |
|
8800 |
'it is $(time) $(date)' expandPlaceholders:$$ with:dict on:s. |
|
8801 |
]. |
|
8802 |
" |
|
8803 |
||
8804 |
"Created: / 14-01-2019 / 17:43:03 / Claus Gittinger" |
|
24254 | 8805 |
"Modified: / 05-06-2019 / 17:05:47 / Claus Gittinger" |
23067 | 8806 |
! |
8807 |
||
8808 |
expandPlaceholders:escapeCharacter with:argArrayOrDictionary on:aStream |
|
8809 |
"this is the generic version of the old %-escaping method, allowing for an arbitrary |
|
8810 |
escape character to be used (typically $$ or $% are effectively used). |
|
8811 |
||
8812 |
Write the receiver to aStream, where all %i escapes are |
|
8813 |
replaced by corresponding arguments' printStrings from the argArrayOrDictionary. |
|
8814 |
I.e. 'hello %1; how is %2' expandPlaceholdersWith:#('world' 'this') results |
|
8815 |
in the new string 'hello world; how is this'. |
|
8816 |
||
8817 |
As an extension, the argument may also be a dictionary, providing values for symbolic keys. |
|
8818 |
In this case, %a .. %z and %(...) are also allowed. |
|
8819 |
(%1..%9 require a numeric key in the dictionary, however) |
|
8820 |
Also, the values in argArrayOrDictionary may be blocks. |
|
8821 |
||
8822 |
To get a '%' character, use a '%%'-escape. |
|
24717 | 8823 |
To get an integer-indexed placeHolder followed by another digit, you must use %(digit). |
23067 | 8824 |
|
8825 |
See also bindWith:... for VisualAge compatibility. |
|
8826 |
Use %<cr> to insert a CR and %<tab> to insert a TAB." |
|
8827 |
||
8828 |
^ self |
|
8829 |
expandPlaceholders:escapeCharacter |
|
8830 |
with:argArrayOrDictionary |
|
8831 |
ignoreNumericEscapes:false |
|
24410 | 8832 |
ignoreNonNumericEscapes:false |
8833 |
ignoreSpecialEscapes:false |
|
23603 | 8834 |
requireParentheses:true |
24541 | 8835 |
ifKeyAbsent:nil |
23067 | 8836 |
on:aStream |
8837 |
||
8838 |
" |
|
23603 | 8839 |
'hello %1' expandPlaceholders:$% with:#('world') on:Transcript. |
8840 |
||
23067 | 8841 |
String streamContents:[:s| |
8842 |
'hello %1' expandPlaceholders:$% with:#('world') on:s. |
|
8843 |
s cr. |
|
8844 |
'hello $1; how is $2' expandPlaceholders:$$ with:#('world' 'this') on:s. |
|
8845 |
s cr. |
|
8846 |
'hello %2; how is %1' expandPlaceholders:$% with:#('world' 'this') on:s. |
|
8847 |
s cr. |
|
8848 |
'%1 plus %2 gives %3 ' expandPlaceholders:$% with:#(4 5 9) on:s. |
|
8849 |
s cr. |
|
8850 |
'%%(1)0 gives %(1)0' expandPlaceholders:$% with:#(123) on:s. |
|
8851 |
s cr. |
|
8852 |
'%%10 gives %10' expandPlaceholders:$% with:#(123) on:s. |
|
8853 |
s cr. |
|
8854 |
'%%(10) gives %(10) %<cr>%<tab>next line' expandPlaceholders:$% with:#(123) on:s. |
|
8855 |
s cr. |
|
8856 |
'%%test gives %test' expandPlaceholders:$% with:#(123) on:s. |
|
8857 |
s cr. |
|
8858 |
'|%%<tab>|%%1|%%<cr>| gives |%<tab>|%1|%<cr>|' expandPlaceholders:$% with:#(foo) on:s. |
|
8859 |
] |
|
8860 |
" |
|
8861 |
||
8862 |
" |
|
8863 |
|dict| |
|
8864 |
||
8865 |
dict := Dictionary new. |
|
8866 |
dict at:1 put:'one'. |
|
8867 |
dict at:$a put:'AAAAA'. |
|
8868 |
dict at:$b put:[ Time now ]. |
|
8869 |
String streamContents:[:s| |
|
8870 |
'hello $1 $a $b' expandPlaceholders:$$ with:dict on:s. |
|
8871 |
]. |
|
8872 |
" |
|
8873 |
||
8874 |
"using blocks: |
|
8875 |
|dict| |
|
8876 |
||
8877 |
dict := Dictionary new. |
|
8878 |
dict at:'time' put:[Time now printString]. |
|
8879 |
dict at:'date' put:[Date today printString]. |
|
8880 |
String streamContents:[:s| |
|
8881 |
'it is $(time) $(date)' expandPlaceholders:$$ with:dict on:s. |
|
8882 |
]. |
|
8883 |
" |
|
8884 |
||
14765 | 8885 |
"Modified: / 18-11-2010 / 15:43:28 / cg" |
23603 | 8886 |
"Modified (comment): / 14-01-2019 / 18:02:59 / Claus Gittinger" |
14765 | 8887 |
! |
8888 |
||
9192 | 8889 |
expandPlaceholdersWith:argArrayOrDictionary |
608 | 8890 |
"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
|
8891 |
replaced by corresponding arguments' printStrings from the argArrayOrDictionary. |
608 | 8892 |
I.e. 'hello %1; how is %2' expandPlaceholdersWith:#('world' 'this') results |
8893 |
in the new string 'hello world; how is this'. |
|
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
8894 |
|
4410
2bdc0c89a42e
enhanced #exapndPlaceHolders, to allow for a dictionary arg
Claus Gittinger <cg@exept.de>
parents:
4409
diff
changeset
|
8895 |
As an extension, the argument may also be a dictionary, providing |
7313 | 8896 |
values for symbolic keys. |
4842 | 8897 |
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
|
8898 |
(%1..%9 require a numeric key in the dictionary, however) |
18425 | 8899 |
Also, the values in argArrayOrDictionary may be blocks. |
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
8900 |
|
608 | 8901 |
To get a '%' character, use a '%%'-escape. |
24717 | 8902 |
To get an integer-indexed placeHolder followed by another digit, you must use %(digit). |
608 | 8903 |
See also bindWith:... for VisualAge compatibility." |
8904 |
||
10906
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
8905 |
|stream| |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
8906 |
|
18269 | 8907 |
stream := (TextStream ? CharacterWriteStream) on:(self species uninitializedNew:self size + 20). |
23209 | 8908 |
"/ self expandPlaceholdersWith:argArrayOrDictionary on:stream. |
24410 | 8909 |
self |
8910 |
expandPlaceholders:$% with:argArrayOrDictionary |
|
8911 |
ignoreNumericEscapes:false |
|
8912 |
ignoreNonNumericEscapes:false |
|
8913 |
ignoreSpecialEscapes:false |
|
8914 |
requireParentheses:true |
|
24541 | 8915 |
ifKeyAbsent:nil |
24410 | 8916 |
on:stream. |
10906
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
8917 |
^ stream contents. |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
8918 |
|
608 | 8919 |
" |
8874 | 8920 |
'hello %1' expandPlaceholdersWith:#('world') |
8921 |
'hello %1; how is %2' expandPlaceholdersWith:#('world' 'this') |
|
8922 |
'hello %2; how is %1' expandPlaceholdersWith:#('world' 'this') |
|
8923 |
'%1 plus %2 gives %3 ' expandPlaceholdersWith:#(4 5 9) |
|
8924 |
'%%(1)0 gives %(1)0' expandPlaceholdersWith:#(123) |
|
8925 |
'%%10 gives %10' expandPlaceholdersWith:#(123) |
|
8926 |
'%%(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
|
8927 |
'%test gives %1' expandPlaceholdersWith:#(123) |
11028
cb1ee3ddd91a
expandPlaceHolders: care fr Text with emphasis
Claus Gittinger <cg@exept.de>
parents:
11018
diff
changeset
|
8928 |
'bla %1 bla' expandPlaceholdersWith:{ 'hello' allBold } |
cb1ee3ddd91a
expandPlaceHolders: care fr Text with emphasis
Claus Gittinger <cg@exept.de>
parents:
11018
diff
changeset
|
8929 |
'bla %1 bla' expandPlaceholdersWith:{ 'hello' } |
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
8930 |
('bla %1 bla' withColor:Color red) |
20874 | 8931 |
expandPlaceholdersWith:{ 'hello' } |
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
8932 |
('bla %1 bla' withColor:Color red) |
20874 | 8933 |
expandPlaceholdersWith:{ 'hello' withColor:Color blue } |
608 | 8934 |
" |
1026
455c9f609f4d
care for 16bit strings in expandPlaceHolders
Claus Gittinger <cg@exept.de>
parents:
1025
diff
changeset
|
8935 |
|
4410
2bdc0c89a42e
enhanced #exapndPlaceHolders, to allow for a dictionary arg
Claus Gittinger <cg@exept.de>
parents:
4409
diff
changeset
|
8936 |
" |
2bdc0c89a42e
enhanced #exapndPlaceHolders, to allow for a dictionary arg
Claus Gittinger <cg@exept.de>
parents:
4409
diff
changeset
|
8937 |
|dict| |
2bdc0c89a42e
enhanced #exapndPlaceHolders, to allow for a dictionary arg
Claus Gittinger <cg@exept.de>
parents:
4409
diff
changeset
|
8938 |
|
2bdc0c89a42e
enhanced #exapndPlaceHolders, to allow for a dictionary arg
Claus Gittinger <cg@exept.de>
parents:
4409
diff
changeset
|
8939 |
dict := Dictionary new. |
2bdc0c89a42e
enhanced #exapndPlaceHolders, to allow for a dictionary arg
Claus Gittinger <cg@exept.de>
parents:
4409
diff
changeset
|
8940 |
dict at:1 put:'one'. |
2bdc0c89a42e
enhanced #exapndPlaceHolders, to allow for a dictionary arg
Claus Gittinger <cg@exept.de>
parents:
4409
diff
changeset
|
8941 |
dict at:$a put:'AAAAA'. |
8794
a5df33935142
nindWith supports blocks in the value dictionary
Claus Gittinger <cg@exept.de>
parents:
8710
diff
changeset
|
8942 |
dict at:$b put:[ Time now ]. |
12316
3e78905d70a9
comment/format in: #expandPlaceholdersWith:
Claus Gittinger <cg@exept.de>
parents:
12196
diff
changeset
|
8943 |
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
|
8944 |
'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
|
8945 |
" |
2bdc0c89a42e
enhanced #exapndPlaceHolders, to allow for a dictionary arg
Claus Gittinger <cg@exept.de>
parents:
4409
diff
changeset
|
8946 |
|
23209 | 8947 |
"Modified: / 01-07-1997 / 00:53:24 / cg" |
8948 |
"Modified: / 14-07-2018 / 09:23:31 / Claus Gittinger" |
|
9192 | 8949 |
! |
8950 |
||
24541 | 8951 |
expandPlaceholdersWith:argArrayOrDictionary ifKeyAbsent:ifNoSuchKeyActionOrNil |
8952 |
"return a copy of the receiver, where all %i escapes are |
|
8953 |
replaced by corresponding arguments' printStrings from the argArrayOrDictionary. |
|
8954 |
I.e. 'hello %1; how is %2' expandPlaceholdersWith:#('world' 'this') results |
|
8955 |
in the new string 'hello world; how is this'. |
|
8956 |
The argument may also be a dictionary, providing values for symbolic keys. |
|
8957 |
To get a '%' character, use a '%%'-escape. |
|
8958 |
||
8959 |
See the comment in |
|
8960 |
expandPlaceholders:with:ignoreNumericEscapes:ignoreNonNumericEscapes:ignoreSpecialEscapes:requireParentheses:ifKeyAbsent:on: |
|
8961 |
for a full explanation. |
|
8962 |
||
8963 |
See also bindWith:... for VisualAge compatibility." |
|
8964 |
||
8965 |
|stream| |
|
8966 |
||
8967 |
stream := (TextStream ? CharacterWriteStream) on:(self species uninitializedNew:self size + 20). |
|
8968 |
self |
|
8969 |
expandPlaceholders:$% with:argArrayOrDictionary |
|
8970 |
ignoreNumericEscapes:false |
|
8971 |
ignoreNonNumericEscapes:false |
|
8972 |
ignoreSpecialEscapes:false |
|
8973 |
requireParentheses:true |
|
8974 |
ifKeyAbsent:ifNoSuchKeyActionOrNil |
|
8975 |
on:stream. |
|
8976 |
^ stream contents. |
|
8977 |
||
8978 |
" |
|
8979 |
'hello %(abc) %1 %a %; %%' expandPlaceholdersWith:nil ifNoSuchKey:[:str :nm | str] |
|
8980 |
" |
|
8981 |
! |
|
8982 |
||
10906
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
8983 |
expandPlaceholdersWith:argArrayOrDictionary on:aStream |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
8984 |
"write the receiver to aStream, where all %i escapes are |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
8985 |
replaced by corresponding arguments' printStrings from the argArrayOrDictionary. |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
8986 |
I.e. 'hello %1; how is %2' expandPlaceholdersWith:#('world' 'this') results |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
8987 |
in the new string 'hello world; how is this'. |
18425 | 8988 |
|
10906
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
8989 |
As an extension, the argument may also be a dictionary, providing |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
8990 |
values for symbolic keys. |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
8991 |
In this case, %a .. %z and %(...) are also allowed. |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
8992 |
(%1..%9 require a numeric key in the dictionary, however) |
18425 | 8993 |
Also, the values in argArrayOrDictionary may be blocks. |
8994 |
||
10906
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
8995 |
To get a '%' character, use a '%%'-escape. |
24717 | 8996 |
To get an integer-indexed placeHolder followed by another digit, you must use %(digit). |
18425 | 8997 |
|
10935 | 8998 |
See also bindWith:... for VisualAge compatibility. |
10939
d0ca31c1d1e9
#expandPlaceHoldersWith: handle %<cr> %<tab> etc
Stefan Vogel <sv@exept.de>
parents:
10935
diff
changeset
|
8999 |
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
|
9000 |
|
24410 | 9001 |
^ self |
9002 |
expandPlaceholders:$% with:argArrayOrDictionary |
|
9003 |
ignoreNumericEscapes:false |
|
9004 |
ignoreNonNumericEscapes:false |
|
9005 |
ignoreSpecialEscapes:false |
|
9006 |
requireParentheses:true |
|
24541 | 9007 |
ifKeyAbsent:nil |
24410 | 9008 |
on:aStream |
10906
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
9009 |
|
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
9010 |
" |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
9011 |
String streamContents:[:s| |
20874 | 9012 |
'hello %1' expandPlaceholdersWith:#('world') on:s. |
9013 |
s cr. |
|
9014 |
'hello %1; how is %2' expandPlaceholdersWith:#('world' 'this') on:s. |
|
9015 |
s cr. |
|
9016 |
'hello %2; how is %1' expandPlaceholdersWith:#('world' 'this') on:s. |
|
9017 |
s cr. |
|
9018 |
'%1 plus %2 gives %3 ' expandPlaceholdersWith:#(4 5 9) on:s. |
|
9019 |
s cr. |
|
9020 |
'%%(1)0 gives %(1)0' expandPlaceholdersWith:#(123) on:s. |
|
9021 |
s cr. |
|
9022 |
'%%10 gives %10' expandPlaceholdersWith:#(123) on:s. |
|
9023 |
s cr. |
|
9024 |
'%%(10) gives %(10) %<cr>%<tab>next line' expandPlaceholdersWith:#(123) on:s. |
|
9025 |
s cr. |
|
9026 |
'%test gives %1' expandPlaceholdersWith:#(123) on:s. |
|
10906
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
9027 |
] |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
9028 |
" |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
9029 |
|
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
9030 |
" |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
9031 |
|dict| |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
9032 |
|
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
9033 |
dict := Dictionary new. |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
9034 |
dict at:1 put:'one'. |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
9035 |
dict at:$a put:'AAAAA'. |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
9036 |
dict at:$b put:[ Time now ]. |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
9037 |
String streamContents:[:s| |
20874 | 9038 |
'hello %1 %a %b' expandPlaceholdersWith:dict on:s. |
10906
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
9039 |
]. |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
9040 |
" |
13008
a27647c36b31
changed: #expandPlaceholdersWith:on:
Claus Gittinger <cg@exept.de>
parents:
12925
diff
changeset
|
9041 |
|
13138
d2eb0eba5907
changed: #expandPlaceholdersWith:on:
Claus Gittinger <cg@exept.de>
parents:
13137
diff
changeset
|
9042 |
"Modified: / 18-11-2010 / 15:43:28 / cg" |
23067 | 9043 |
"Modified: / 14-06-2018 / 11:45:36 / Claus Gittinger" |
10906
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
9044 |
! |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
9045 |
|
22929 | 9046 |
extractPlaceHolders:escapeCharacter |
9047 |
"returns a list of placeholder keys of the form %i, |
|
9048 |
where i is either a single digit (as in %1, %2) |
|
9049 |
or a single letter (as in %a, %z) |
|
9050 |
or a word-key, as in %(one), %(fooBar). |
|
9051 |
For numeric keys, the returned collection contains integers; |
|
9052 |
for non-numeric ones, it includes strings." |
|
9053 |
||
9054 |
|setOfKeys loggingDictionary| |
|
9055 |
||
9056 |
setOfKeys := Set new. |
|
9057 |
loggingDictionary := Plug new. |
|
9058 |
loggingDictionary respondTo:#at: with:[:key | setOfKeys add:key. key asString]. |
|
9059 |
loggingDictionary respondTo:#at:ifAbsent: with:[:key :def | setOfKeys add:key. key asString]. |
|
9060 |
loggingDictionary respondTo:#includesKey: with:[:key | setOfKeys add:key. true]. |
|
9061 |
loggingDictionary respondTo:#isSequenceable with:[false]. |
|
9062 |
self expandPlaceholders:escapeCharacter with:loggingDictionary. |
|
9063 |
^ setOfKeys |
|
9064 |
||
9065 |
" |
|
9066 |
self assert:('hello %1 and %2' extractPlaceHolders:$%) asSet = #(1 2) asSet |
|
9067 |
self assert:('hello %9 and %(10)' extractPlaceHolders:$%) asSet = #(9 10) asSet |
|
9068 |
self assert:('hello %a and %(foo) and %1' extractPlaceHolders:$%) asSet = #(1 'a' 'foo') asSet |
|
9069 |
self assert:('hello %a and %(foo) and %1' extractPlaceHolders:$$) asSet = #() asSet |
|
9070 |
" |
|
9071 |
! |
|
9072 |
||
9230 | 9073 |
firstLine |
14405
1d0d99a95bba
comment/format in:7 methods
Claus Gittinger <cg@exept.de>
parents:
14168
diff
changeset
|
9074 |
"return the first line of a multiline string" |
1d0d99a95bba
comment/format in:7 methods
Claus Gittinger <cg@exept.de>
parents:
14168
diff
changeset
|
9075 |
|
1d0d99a95bba
comment/format in:7 methods
Claus Gittinger <cg@exept.de>
parents:
14168
diff
changeset
|
9076 |
^ self asCollectionOfSubCollectionsSeparatedBy:(Character cr) do:[:line | ^ line]. |
9230 | 9077 |
|
9078 |
" |
|
10703
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
9079 |
'hello' firstLine |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
9080 |
'1\2\3' withCRs firstLine |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
9081 |
'\1\2\3' withCRs firstLine |
9230 | 9082 |
" |
9083 |
! |
|
9084 |
||
13987 | 9085 |
tokensBasedOn:aCharacter |
9086 |
"this is an ST-80 alias for the ST/X method |
|
20874 | 9087 |
asCollectionOfSubstringsSeparatedBy:" |
13987 | 9088 |
|
9089 |
^ self asCollectionOfSubstringsSeparatedBy:aCharacter |
|
9090 |
||
9091 |
" |
|
9092 |
'hello:world:isnt:this nice' tokensBasedOn:$: |
|
9093 |
'foo,bar,baz' tokensBasedOn:$, |
|
9094 |
'/etc/passwd' asFilename readStream nextLine tokensBasedOn:$: |
|
9095 |
" |
|
9096 |
! |
|
9097 |
||
9098 |
withCEscapes |
|
16863 | 9099 |
"return a new string consisting of receiver's characters |
13987 | 9100 |
with all special and unprintable characters replaced by \X-character escapes. |
9101 |
(similar to the way C-language literal Strings are represented). |
|
9102 |
The resulting string will contain only 7-bit ascii characters. |
|
9103 |
Emphasis is not supported. |
|
9104 |
The following escapes are generated: |
|
20874 | 9105 |
\' single quote character |
9106 |
\dQuote double quote character |
|
24514 | 9107 |
\b backspace character |
20874 | 9108 |
\r return character |
9109 |
\n newline character |
|
9110 |
\t tab character |
|
9111 |
\\ the \ character itself |
|
9112 |
\xnn two digit hex number defining the characters ascii value |
|
9113 |
\unnnn four digit hex number defining the characters ascii value |
|
9114 |
\Unnnnnnnn eight digit hex number defining the characters ascii value |
|
16863 | 9115 |
This is the opposite of withoutCEscapes. |
17000 | 9116 |
|
9117 |
Sigh: this is named completely wrong (opposite naming of withCRs/witoutCRs), |
|
20874 | 9118 |
but it cannot be changed easily, as these methods are already used heavily |
13987 | 9119 |
" |
9120 |
||
25189 | 9121 |
|anyEscapeNeeded out| |
13987 | 9122 |
|
9123 |
" |
|
9124 |
first, check if any escape is needed and return the receiver unchanged if not |
|
9125 |
" |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
9126 |
anyEscapeNeeded := self |
20874 | 9127 |
contains:[:ch | |
9128 |
((ch codePoint between:32 and:126) not |
|
25080 | 9129 |
or:[ch == $' or:[ch == $" or:[ch == $\]]]) |
20874 | 9130 |
]. |
13987 | 9131 |
anyEscapeNeeded ifFalse:[ ^ self ]. |
9132 |
||
9133 |
self hasChangeOfEmphasis ifTrue:[ self error:'emphasis not supported' ]. |
|
9134 |
||
18269 | 9135 |
out := WriteStream on:(String uninitializedNew:self size-1). |
25189 | 9136 |
self printWithCEscapesOn:out. |
13987 | 9137 |
^ out contents |
9138 |
||
9139 |
" |
|
25080 | 9140 |
'c:\foo\bar\baz' withCEscapes. |
9141 |
c'hello\n\tworld' withCEscapes. |
|
24514 | 9142 |
'hello\b\tworld' withoutCEscapes withCEscapes. |
17000 | 9143 |
'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
|
9144 |
('hello ',(Character value:16r1234),' world') withCEscapes |
13987 | 9145 |
" |
9146 |
||
9147 |
"Created: / 25-01-2012 / 11:08:16 / cg" |
|
22344 | 9148 |
"Modified: / 12-11-2017 / 12:39:57 / cg" |
13987 | 9149 |
! |
9150 |
||
9192 | 9151 |
withCRs |
17205 | 9152 |
"return a new string consisting of the receiver's characters |
16757 | 9153 |
with all \-characters replaced by cr-characters. |
9154 |
If there are no backslashes, return the original" |
|
9155 |
||
16759 | 9156 |
^ self copyReplaceAll:$\ with:(Character cr) ifNone:self |
9192 | 9157 |
|
9158 |
" |
|
9159 |
'hello\world' withCRs |
|
9160 |
" |
|
9161 |
||
9162 |
"Modified: / 18.7.1998 / 22:53:02 / cg" |
|
9163 |
! |
|
9164 |
||
9165 |
withEscapes |
|
15405 | 9166 |
<resource: #obsolete> |
13987 | 9167 |
"has been renamed; the name withEscapes is misleading" |
9168 |
||
9169 |
self obsoleteMethodWarning:'use withoutCEscapes'. |
|
9170 |
^ self withoutCEscapes. |
|
9171 |
||
9172 |
"Modified: / 25-01-2012 / 10:42:30 / cg" |
|
9192 | 9173 |
! |
9174 |
||
9175 |
withMatchEscapes |
|
4117 | 9176 |
"return a copy of the receiver with all match characters escaped |
9177 |
by $\ characters (to be usable as a match string). |
|
9178 |
Return the receiver, if there are none." |
|
9179 |
||
5342
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
9180 |
|in out c escape| |
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
9181 |
|
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
9182 |
escape := self class matchEscapeCharacter. |
4117 | 9183 |
|
9184 |
in := self readStream. |
|
7986 | 9185 |
out := WriteStream on:(self species new:self size). |
4117 | 9186 |
[in atEnd] whileFalse:[ |
20874 | 9187 |
c := in next. |
9188 |
(c == escape or:['*[#' includes:c]) ifTrue:[ |
|
9189 |
out nextPut:$\. |
|
9190 |
]. |
|
9191 |
out nextPut:c. |
|
4117 | 9192 |
]. |
9193 |
^ out contents. |
|
9194 |
||
9195 |
" |
|
8874 | 9196 |
'*foo' withMatchEscapes |
9197 |
'\*foo' withMatchEscapes |
|
9198 |
'*foo' withMatchEscapes |
|
9199 |
'\\*foo' withMatchEscapes |
|
9200 |
'foo*' withMatchEscapes |
|
9201 |
'foo\*' withMatchEscapes |
|
9202 |
'foo\' withMatchEscapes |
|
9203 |
'f*o*o' withMatchEscapes |
|
4117 | 9204 |
" |
9205 |
||
9206 |
"Modified: 2.4.1997 / 18:13:04 / cg" |
|
9192 | 9207 |
! |
9208 |
||
14992 | 9209 |
withSeparatorsCompacted |
9210 |
"return a new string with each sequence of whiteSpace replaced by a single space character. |
|
9211 |
Preserves a leading/trailing space." |
|
9212 |
||
9213 |
^ self species streamContents:[:s | |
|
20874 | 9214 |
|skipping| |
9215 |
||
9216 |
skipping := false. |
|
9217 |
1 to:self size do:[:idx | |
|
9218 |
|char| |
|
9219 |
||
9220 |
char := self at:idx. |
|
9221 |
char isSeparator ifFalse:[ |
|
9222 |
s nextPut:char. |
|
9223 |
skipping := false. |
|
9224 |
] ifTrue:[ |
|
9225 |
skipping ifFalse:[ |
|
9226 |
s nextPut:(Character space). |
|
9227 |
skipping := true |
|
9228 |
]. |
|
9229 |
] |
|
9230 |
] |
|
14992 | 9231 |
] |
9232 |
||
9233 |
" |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
9234 |
'hello wwww' withSeparatorsCompacted |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
9235 |
'hello wwww' withSeparatorsCompacted |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
9236 |
' hello wwww' withSeparatorsCompacted |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
9237 |
' hello wwww ' withSeparatorsCompacted |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
9238 |
' hello wwww ' withSeparatorsCompacted |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
9239 |
'hel lo www w' withSeparatorsCompacted |
14992 | 9240 |
" |
9241 |
! |
|
9242 |
||
18464 | 9243 |
withSeparatorsReplacedBy:replacementCharacter |
9244 |
"return a new string with each separator (whitespace) replaced by replacementCharacter. |
|
9245 |
Typically used with space as replacementCharacter" |
|
9246 |
||
9247 |
^ self species streamContents:[:s | |
|
20874 | 9248 |
self do:[:ch | |
9249 |
ch isSeparator ifTrue:[ |
|
9250 |
s nextPut:replacementCharacter |
|
9251 |
] ifFalse:[ |
|
9252 |
s nextPut:ch. |
|
9253 |
] |
|
9254 |
] |
|
18464 | 9255 |
] |
9256 |
||
9257 |
" |
|
9258 |
'hello wwww' withSeparatorsReplacedBy:$* |
|
9259 |
'hello ww ww' withSeparatorsReplacedBy:$* |
|
9260 |
' hello wwww' withSeparatorsReplacedBy:$* |
|
9261 |
'hel lo www w' withSeparatorsReplacedBy:$* |
|
9262 |
'hel |
|
9263 |
lo www |
|
9264 |
w' withSeparatorsReplacedBy:$* |
|
9265 |
" |
|
9266 |
! |
|
9267 |
||
9192 | 9268 |
withTabs |
19464 | 9269 |
"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
|
9270 |
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
|
9271 |
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
|
9272 |
otherwise a new string is returned. |
8874 | 9273 |
Limitation: only the very first spaces are replaced |
20874 | 9274 |
(i.e. if the receiver contains newLine characters, |
9275 |
no tabs are inserted after those lineBreaks)" |
|
8874 | 9276 |
|
9277 |
|idx "{ SmallInteger }" |
|
9278 |
nTabs "{ SmallInteger }" |
|
608 | 9279 |
newString| |
9280 |
||
9281 |
idx := self findFirst:[:c | (c ~~ Character space)]. |
|
9282 |
nTabs := (idx-1) // 8. |
|
5815
4acdd87a77d4
// can return negative numbers.
Stefan Vogel <sv@exept.de>
parents:
5795
diff
changeset
|
9283 |
nTabs <= 0 ifTrue:[^ self]. |
608 | 9284 |
|
9285 |
"any tabs" |
|
15523 | 9286 |
newString := self species new:(self size - (nTabs * 7)). |
608 | 9287 |
newString atAll:(1 to:nTabs) put:(Character tab). |
9288 |
newString replaceFrom:(nTabs + 1) with:self startingAt:(nTabs * 8 + 1). |
|
9289 |
^ newString |
|
9290 |
||
9291 |
" |
|
8874 | 9292 |
'12345678901234567890' withTabs |
9293 |
' 8901234567890' withTabs |
|
9294 |
' 901234567890' withTabs |
|
608 | 9295 |
' 67890' withTabs |
9296 |
' 7890' withTabs |
|
9297 |
' 890' withTabs |
|
9298 |
" |
|
9192 | 9299 |
! |
9300 |
||
9301 |
withTabsExpanded |
|
19464 | 9302 |
"return a string consisting of the receiver's characters, |
8874 | 9303 |
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
|
9304 |
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
|
9305 |
otherwise a new string is returned. |
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
678
diff
changeset
|
9306 |
This does handle multiline strings." |
608 | 9307 |
|
5741
3a6086774d38
added #withTabsExpanded: (i.e. arg to specify tab-column width)
Claus Gittinger <cg@exept.de>
parents:
5739
diff
changeset
|
9308 |
^ self withTabsExpanded:8 |
3a6086774d38
added #withTabsExpanded: (i.e. arg to specify tab-column width)
Claus Gittinger <cg@exept.de>
parents:
5739
diff
changeset
|
9309 |
|
3a6086774d38
added #withTabsExpanded: (i.e. arg to specify tab-column width)
Claus Gittinger <cg@exept.de>
parents:
5739
diff
changeset
|
9310 |
" |
8874 | 9311 |
('1' , Character tab asString , 'x') withTabsExpanded |
9312 |
('12345' , Character tab asString , 'x') withTabsExpanded |
|
9313 |
('123456' , Character tab asString , 'x') withTabsExpanded |
|
9314 |
('1234567' , Character tab asString , 'x') withTabsExpanded |
|
9315 |
('12345678' , Character tab asString , 'x') withTabsExpanded |
|
9316 |
('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
|
9317 |
|
3a6086774d38
added #withTabsExpanded: (i.e. arg to specify tab-column width)
Claus Gittinger <cg@exept.de>
parents:
5739
diff
changeset
|
9318 |
(String with:Character tab |
20874 | 9319 |
with:Character tab |
9320 |
with:$1) withTabsExpanded |
|
5741
3a6086774d38
added #withTabsExpanded: (i.e. arg to specify tab-column width)
Claus Gittinger <cg@exept.de>
parents:
5739
diff
changeset
|
9321 |
|
3a6086774d38
added #withTabsExpanded: (i.e. arg to specify tab-column width)
Claus Gittinger <cg@exept.de>
parents:
5739
diff
changeset
|
9322 |
(String with:Character tab |
20874 | 9323 |
with:$1 |
9324 |
with:Character tab |
|
9325 |
with:$2) withTabsExpanded |
|
5741
3a6086774d38
added #withTabsExpanded: (i.e. arg to specify tab-column width)
Claus Gittinger <cg@exept.de>
parents:
5739
diff
changeset
|
9326 |
|
3a6086774d38
added #withTabsExpanded: (i.e. arg to specify tab-column width)
Claus Gittinger <cg@exept.de>
parents:
5739
diff
changeset
|
9327 |
(String with:Character tab |
20874 | 9328 |
with:$1 |
9329 |
with:Character cr |
|
9330 |
with:Character tab |
|
9331 |
with:$2) withTabsExpanded |
|
5741
3a6086774d38
added #withTabsExpanded: (i.e. arg to specify tab-column width)
Claus Gittinger <cg@exept.de>
parents:
5739
diff
changeset
|
9332 |
" |
3a6086774d38
added #withTabsExpanded: (i.e. arg to specify tab-column width)
Claus Gittinger <cg@exept.de>
parents:
5739
diff
changeset
|
9333 |
|
3a6086774d38
added #withTabsExpanded: (i.e. arg to specify tab-column width)
Claus Gittinger <cg@exept.de>
parents:
5739
diff
changeset
|
9334 |
"Modified: 12.5.1996 / 13:05:10 / cg" |
9192 | 9335 |
! |
9336 |
||
9337 |
withTabsExpanded:numSpaces |
|
19464 | 9338 |
"return a string consisting of the receiver's characters, |
8874 | 9339 |
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
|
9340 |
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
|
9341 |
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
|
9342 |
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
|
9343 |
|
8874 | 9344 |
|col "{ SmallInteger }" |
1381 | 9345 |
str ch |
9346 |
dstIdx "{ SmallInteger }" |
|
9347 |
newSz "{ SmallInteger }" |
|
9348 |
sz "{ SmallInteger }" |
|
9349 |
hasEmphasis e| |
|
608 | 9350 |
|
9351 |
(self includes:(Character tab)) ifFalse:[^ self]. |
|
1381 | 9352 |
|
9353 |
sz := self size. |
|
9354 |
||
9355 |
"/ count the new size first, instead of |
|
9356 |
"/ multiple resizing (better for large strings) |
|
9357 |
||
9358 |
col := 1. newSz := 0. |
|
9359 |
1 to:sz do:[:srcIdx | |
|
20874 | 9360 |
ch := self at:srcIdx. |
9361 |
ch == Character tab ifFalse:[ |
|
9362 |
col := col + 1. |
|
9363 |
newSz := newSz + 1. |
|
9364 |
ch == Character cr ifTrue:[ |
|
9365 |
col := 1 |
|
9366 |
]. |
|
9367 |
] ifTrue:[ |
|
9368 |
(col \\ numSpaces) to:numSpaces do:[:ii | |
|
9369 |
newSz := newSz + 1. |
|
9370 |
col := col + 1 |
|
9371 |
]. |
|
9372 |
] |
|
608 | 9373 |
]. |
1381 | 9374 |
|
9375 |
str := self species new:newSz. |
|
23428 | 9376 |
(str isText and:[self bitsPerCharacter > 8]) ifTrue:[ |
9377 |
str := Text string:(Unicode16String new:newSz) |
|
9378 |
]. |
|
9379 |
||
1381 | 9380 |
hasEmphasis := self hasChangeOfEmphasis. |
9381 |
||
9382 |
col := 1. dstIdx := 1. |
|
9383 |
1 to:sz do:[:srcIdx | |
|
20874 | 9384 |
ch := self at:srcIdx. |
9385 |
||
9386 |
ch == Character tab ifFalse:[ |
|
9387 |
col := col + 1. |
|
9388 |
ch == Character cr ifTrue:[ |
|
9389 |
col := 1 |
|
9390 |
]. |
|
9391 |
hasEmphasis ifTrue:[ |
|
9392 |
e := self emphasisAt:srcIdx. |
|
9393 |
str emphasisAt:dstIdx put:e |
|
9394 |
]. |
|
9395 |
str at:dstIdx put:ch. |
|
9396 |
dstIdx := dstIdx + 1 |
|
9397 |
] ifTrue:[ |
|
9398 |
(col \\ numSpaces) to:numSpaces do:[:ii | |
|
9399 |
str at:dstIdx put:Character space. |
|
9400 |
dstIdx := dstIdx + 1. |
|
9401 |
col := col + 1 |
|
9402 |
]. |
|
9403 |
] |
|
1381 | 9404 |
]. |
9405 |
^ str |
|
608 | 9406 |
|
9407 |
" |
|
8874 | 9408 |
('1' , Character tab asString , 'x') withTabsExpanded |
9409 |
('1' , Character tab asString , 'x') withTabsExpanded:4 |
|
9410 |
('12345' , Character tab asString , 'x') withTabsExpanded |
|
9411 |
('123456' , Character tab asString , 'x') withTabsExpanded |
|
9412 |
('1234567' , Character tab asString , 'x') withTabsExpanded |
|
9413 |
('12345678' , Character tab asString , 'x') withTabsExpanded |
|
9414 |
('123456789' , Character tab asString , 'x') withTabsExpanded |
|
735
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
678
diff
changeset
|
9415 |
|
608 | 9416 |
(String with:Character tab |
20874 | 9417 |
with:Character tab |
9418 |
with:$1) withTabsExpanded |
|
608 | 9419 |
|
9420 |
(String with:Character tab |
|
20874 | 9421 |
with:$1 |
9422 |
with:Character tab |
|
9423 |
with:$2) withTabsExpanded |
|
735
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
678
diff
changeset
|
9424 |
|
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
678
diff
changeset
|
9425 |
(String with:Character tab |
20874 | 9426 |
with:$1 |
9427 |
with:Character cr |
|
9428 |
with:Character tab |
|
9429 |
with:$2) withTabsExpanded |
|
608 | 9430 |
" |
735
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
678
diff
changeset
|
9431 |
|
1381 | 9432 |
"Modified: 12.5.1996 / 13:05:10 / cg" |
9192 | 9433 |
! |
9434 |
||
13987 | 9435 |
withoutAllSpaces |
16855 | 9436 |
"return a copy of the receiver with all whitespace removed" |
9437 |
||
13987 | 9438 |
^ self asCollectionOfWords asStringWith:''. |
9439 |
"/ |col string| |
|
9440 |
"/ |
|
9441 |
"/ col := self asCollectionOfWords. |
|
9442 |
"/ string := String new. |
|
9443 |
"/ col do:[:el | |
|
9444 |
"/ string := string,el |
|
9445 |
"/ ]. |
|
9446 |
"/ ^string |
|
9447 |
||
9448 |
" |
|
9449 |
'hello wwww' withoutAllSpaces |
|
9450 |
'hel lo www w' withoutAllSpaces |
|
9451 |
" |
|
9452 |
||
9453 |
"Modified: / 18.7.1998 / 22:53:08 / cg" |
|
9454 |
! |
|
9455 |
||
9456 |
withoutCEscapes |
|
15628 | 9457 |
"return a new string consisting of receiver's characters |
22205 | 9458 |
with all \X-character escapes replaced by corresponding characters. |
13987 | 9459 |
(similar to the way C-language Strings are converted). |
9460 |
The following escapes are supported: |
|
20874 | 9461 |
\r return character |
9462 |
\n newline character |
|
9463 |
\b backspace character |
|
9464 |
\f formfeed character |
|
9465 |
\t tab character |
|
9466 |
\e escape character |
|
9467 |
\\ the \ character itself |
|
9468 |
\nnn three digit octal number defining the characters ascii value |
|
9469 |
\xnn two digit hex number defining the characters ascii value |
|
9470 |
\unnnn four digit hex number defining the characters unicode value |
|
9471 |
\Unnnnnnnn eight digit hex number defining the characters unicode value |
|
9472 |
\other other |
|
13987 | 9473 |
|
9474 |
Notice, that \' is NOT a valid escape, since the general syntax of |
|
9475 |
string constants is not affected by this method. |
|
9476 |
||
9477 |
Although easily implementable, this is NOT done automatically |
|
9478 |
by the compiler (due to a lack of a language standard for this). |
|
22205 | 9479 |
However, the compiler may detect sends of #withEscapes to string literals |
13987 | 9480 |
and place a modified string constant into the binary/byte-code. |
9481 |
Therefore, no runtime penalty will be payed for using these escapes. |
|
9482 |
(not in pre 2.11 versions) |
|
16863 | 9483 |
|
9484 |
This is the opposite of withCEscapes. |
|
17000 | 9485 |
|
9486 |
Sigh: this is named completely wrong (opposite naming of withCRs/witoutCRs), |
|
20874 | 9487 |
but it cannot be changed easily, as these methods are already used heavily |
13987 | 9488 |
" |
9489 |
||
9490 |
|val "{ SmallInteger }" |
|
9491 |
in out nextChar nDigits| |
|
9492 |
||
9493 |
" |
|
9494 |
first, see if there is any escape; if not, return the receiver unchanged |
|
9495 |
" |
|
9496 |
(self includes:$\) ifFalse:[^ self ]. |
|
9497 |
||
9498 |
self hasChangeOfEmphasis ifTrue:[ self error:'emphasis not supported' ]. |
|
9499 |
out := CharacterWriteStream on:(String new:self size - 1). |
|
9500 |
||
9501 |
in := ReadStream on:self. |
|
9502 |
[in atEnd] whileFalse:[ |
|
20874 | 9503 |
nextChar := in next. |
9504 |
nextChar == $\ ifTrue:[ |
|
9505 |
in atEnd ifTrue:[ |
|
9506 |
] ifFalse:[ |
|
9507 |
nextChar := in next. |
|
9508 |
nextChar == $r ifTrue:[ |
|
9509 |
nextChar := Character return |
|
9510 |
] ifFalse:[ nextChar == $n ifTrue:[ |
|
9511 |
nextChar := Character nl |
|
9512 |
] ifFalse:[ nextChar == $b ifTrue:[ |
|
9513 |
nextChar := Character backspace |
|
9514 |
] ifFalse:[ nextChar == $f ifTrue:[ |
|
9515 |
nextChar := Character newPage |
|
9516 |
] ifFalse:[ nextChar == $t ifTrue:[ |
|
9517 |
nextChar := Character tab |
|
9518 |
] ifFalse:[ nextChar == $e ifTrue:[ |
|
9519 |
nextChar := Character esc |
|
9520 |
] ifFalse:[ |
|
9521 |
nextChar == $0 ifTrue:[ |
|
9522 |
val := 0. |
|
9523 |
nextChar := in peek. |
|
9524 |
nDigits := 1. |
|
9525 |
[nextChar notNil and:[nextChar isDigit and:[nDigits <= 3]]] whileTrue:[ |
|
9526 |
val := (val * 8) + nextChar digitValue. |
|
9527 |
nextChar := in nextPeek. |
|
9528 |
nDigits := nDigits + 1. |
|
9529 |
]. |
|
9530 |
nextChar := Character value:val. |
|
9531 |
] ifFalse:[ |
|
9532 |
val := 0. |
|
9533 |
nextChar == $x ifTrue:[ |
|
9534 |
2 timesRepeat:[ |
|
9535 |
nextChar := in next. |
|
9536 |
val := (val * 16) + nextChar digitValue. |
|
9537 |
]. |
|
9538 |
nextChar := Character value:val. |
|
9539 |
] ifFalse:[ |
|
9540 |
nextChar == $u ifTrue:[ |
|
9541 |
4 timesRepeat:[ |
|
9542 |
nextChar := in next. |
|
9543 |
val := (val * 16) + nextChar digitValue. |
|
9544 |
]. |
|
9545 |
nextChar := Character value:val. |
|
9546 |
] ifFalse:[ |
|
9547 |
nextChar == $U ifTrue:[ |
|
9548 |
8 timesRepeat:[ |
|
9549 |
nextChar := in next. |
|
9550 |
val := (val * 16) + nextChar digitValue. |
|
9551 |
]. |
|
9552 |
nextChar := Character value:val. |
|
9553 |
] |
|
9554 |
] |
|
9555 |
] |
|
9556 |
] |
|
9557 |
]]]]]]. |
|
9558 |
]. |
|
9559 |
]. |
|
9560 |
out nextPut:nextChar. |
|
13987 | 9561 |
]. |
9562 |
^ out contents |
|
9563 |
||
9564 |
" |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
9565 |
'hello world' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
9566 |
'hello\world' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
9567 |
'hello\world\' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
9568 |
'hello world\' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
9569 |
'hello\tworld' withoutCEscapes |
13987 | 9570 |
'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
|
9571 |
'hello\tworld\n' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
9572 |
'hello\010world' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
9573 |
'hello\r\nworld' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
9574 |
'hello\r\n\x08world' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
9575 |
'0\x080' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
9576 |
'0\u12340' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
9577 |
'0\U123456780' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
9578 |
'0\0a' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
9579 |
'0\00a' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
9580 |
'0\000a' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
9581 |
'0\0000a' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
9582 |
'0\00000a' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
9583 |
'0\03770' withoutCEscapes |
13987 | 9584 |
" |
9585 |
||
9586 |
"Created: / 25-01-2012 / 10:41:44 / cg" |
|
22205 | 9587 |
"Modified (comment): / 23-08-2017 / 11:07:43 / mawalch" |
13987 | 9588 |
! |
9589 |
||
9192 | 9590 |
withoutCRs |
19464 | 9591 |
"return a new collection consisting of receiver's elements |
9192 | 9592 |
with all cr-characters replaced by \-characters. |
9593 |
This is the reverse operation of withCRs." |
|
9594 |
||
16759 | 9595 |
^ self copyReplaceAll:(Character cr) with:$\ ifNone:self |
12319
18b80c2ce1e3
oops #withoutSeparators #withoutSpaces were noops
Claus Gittinger <cg@exept.de>
parents:
12317
diff
changeset
|
9596 |
|
9192 | 9597 |
" |
9598 |
'hello |
|
9599 |
world' withoutCRs |
|
9600 |
" |
|
9601 |
||
9602 |
"Modified: / 18.7.1998 / 22:53:08 / cg" |
|
9603 |
! |
|
9604 |
||
9605 |
withoutLeadingSeparators |
|
608 | 9606 |
"return a copy of myself without leading separators. |
9607 |
Notice: this does remove tabs, newline or any other whitespace. |
|
9608 |
Returns an empty string, if the receiver consist only of whitespace." |
|
9609 |
||
9610 |
|index| |
|
9611 |
||
9612 |
index := self indexOfNonSeparatorStartingAt:1. |
|
9613 |
index ~~ 0 ifTrue:[ |
|
20874 | 9614 |
index == 1 ifTrue:[ |
9615 |
^ self |
|
9616 |
]. |
|
9617 |
^ self copyFrom:index |
|
608 | 9618 |
]. |
9619 |
^ '' |
|
9620 |
||
9621 |
" |
|
8874 | 9622 |
' foo ' withoutLeadingSeparators |
9623 |
'foo ' withoutLeadingSeparators |
|
9624 |
' foo' withoutLeadingSeparators |
|
9625 |
' ' withoutLeadingSeparators |
|
9626 |
'foo' withoutLeadingSeparators |
|
9627 |
(' ' , Character tab asString , ' foo ') withoutLeadingSeparators inspect |
|
608 | 9628 |
" |
9192 | 9629 |
! |
9630 |
||
9631 |
withoutMatchEscapes |
|
4117 | 9632 |
"return a copy of the receiver with all $\ removed or |
9633 |
the receiver, if there are none." |
|
9634 |
||
5342
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
9635 |
|in out c escape| |
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
9636 |
|
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
9637 |
escape := self class matchEscapeCharacter. |
4117 | 9638 |
|
9639 |
in := self readStream. |
|
8222
f2c454a9a038
replaced '' writeStream by String writeStream
Claus Gittinger <cg@exept.de>
parents:
8124
diff
changeset
|
9640 |
out := self species writeStream. |
4117 | 9641 |
[in atEnd] whileFalse:[ |
20874 | 9642 |
c := in next. |
9643 |
c == escape ifTrue:[ |
|
9644 |
in atEnd ifFalse:[ |
|
9645 |
c := in next. |
|
9646 |
] |
|
9647 |
]. |
|
9648 |
out nextPut:c. |
|
4117 | 9649 |
]. |
9650 |
^ out contents. |
|
9651 |
||
9652 |
" |
|
8874 | 9653 |
'*foo' withoutMatchEscapes |
9654 |
'\*foo' withoutMatchEscapes |
|
9655 |
'*foo' withoutMatchEscapes |
|
9656 |
'\\*foo' withoutMatchEscapes |
|
9657 |
'foo*' withoutMatchEscapes |
|
9658 |
'foo\*' withoutMatchEscapes |
|
9659 |
'foo\' withoutMatchEscapes |
|
9660 |
'f\*o\*o' withoutMatchEscapes |
|
4117 | 9661 |
" |
9662 |
||
9663 |
"Modified: 30.6.1997 / 13:40:23 / cg" |
|
9192 | 9664 |
! |
9665 |
||
23249 | 9666 |
withoutPrefix:aStringOrCharacter caseSensitive:caseSensitive |
23234 | 9667 |
"if the receiver startsWith aString, return a copy without it. |
9668 |
Otherwise return the receiver" |
|
9669 |
||
23249 | 9670 |
(self startsWith:aStringOrCharacter caseSensitive:caseSensitive) ifTrue:[ |
9671 |
aStringOrCharacter isCharacter ifTrue:[ |
|
9672 |
^ self copyFrom:2 |
|
9673 |
] ifFalse:[ |
|
9674 |
^ self copyFrom:aStringOrCharacter size+1 |
|
9675 |
]. |
|
23234 | 9676 |
]. |
9677 |
^ self |
|
9678 |
||
9679 |
" |
|
24142 | 9680 |
'Helloworld' withoutPrefix:'hello' caseSensitive:false |
9681 |
'Helloworld' withoutPrefix:'foo' caseSensitive:false |
|
9682 |
'Helloworld' withoutPrefix:$h caseSensitive:false |
|
9683 |
'Helloworld' withoutPrefix:#( $h ) caseSensitive:false |
|
23234 | 9684 |
" |
9685 |
||
9686 |
"Created: / 27-07-2018 / 08:30:03 / Claus Gittinger" |
|
23249 | 9687 |
"Modified: / 31-07-2018 / 17:05:53 / Claus Gittinger" |
24142 | 9688 |
"Modified (comment): / 24-05-2019 / 09:23:30 / Claus Gittinger" |
23234 | 9689 |
! |
9690 |
||
10671 | 9691 |
withoutQuotes |
17000 | 9692 |
"/ remove quotes ($" and $') from the front and end of myself (if matching)" |
9693 |
||
9694 |
|firstChar| |
|
9695 |
||
17024 | 9696 |
self isEmpty ifTrue:[^ self]. |
9697 |
||
17000 | 9698 |
firstChar := self first. |
9699 |
((firstChar == $") or:[firstChar == $']) ifFalse:[^ self]. |
|
9700 |
||
9701 |
self last == firstChar ifTrue:[ |
|
20874 | 9702 |
^ self copyFrom:2 to:(self size-1) |
10671 | 9703 |
]. |
17000 | 9704 |
^ self |
9705 |
||
9706 |
"/ |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
9707 |
"/ '"hello"' withoutQuotes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
9708 |
"/ '''hello''' withoutQuotes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
9709 |
"/ 'hello' withoutQuotes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
9710 |
"/ '"hello' withoutQuotes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
9711 |
"/ 'hello"' withoutQuotes |
17000 | 9712 |
"/ |
10671 | 9713 |
! |
9714 |
||
9192 | 9715 |
withoutSeparators |
608 | 9716 |
"return a copy of myself without leading and trailing whitespace. |
4156 | 9717 |
(but whiteSpace in-between is preserved) |
608 | 9718 |
Whitespace is space, tab, newline, formfeed. |
9719 |
Use withoutSpaces, if you want to remove spaces only." |
|
9720 |
||
12319
18b80c2ce1e3
oops #withoutSeparators #withoutSpaces were noops
Claus Gittinger <cg@exept.de>
parents:
12317
diff
changeset
|
9721 |
^ self trimForWhich:[:ch | ch isSeparator] |
608 | 9722 |
|
9723 |
" |
|
8874 | 9724 |
' foo ' withoutSeparators |
9725 |
' foo' withoutSeparators |
|
9726 |
'foo ' withoutSeparators |
|
9727 |
' ' withoutSeparators |
|
9728 |
(' foo' , Character tab asString , ' ') withoutSeparators inspect |
|
12319
18b80c2ce1e3
oops #withoutSeparators #withoutSpaces were noops
Claus Gittinger <cg@exept.de>
parents:
12317
diff
changeset
|
9729 |
' foo ' asUnicode16String withoutSeparators |
608 | 9730 |
" |
9192 | 9731 |
! |
9732 |
||
9733 |
withoutSpaces |
|
608 | 9734 |
"return a copy of myself without leading and trailing spaces. |
4156 | 9735 |
(but spaces in-between are preserved) |
608 | 9736 |
Notice: this does NOT remove tabs, newline or any other whitespace. |
9737 |
Use withoutSeparators for this." |
|
9738 |
||
12319
18b80c2ce1e3
oops #withoutSeparators #withoutSpaces were noops
Claus Gittinger <cg@exept.de>
parents:
12317
diff
changeset
|
9739 |
^ self trimForWhich:[:ch | ch == Character space] |
608 | 9740 |
|
9741 |
" |
|
8874 | 9742 |
' foo ' withoutSpaces |
9743 |
'foo ' withoutSpaces |
|
9744 |
' foo' withoutSpaces |
|
9745 |
' ' withoutSpaces |
|
9746 |
'a b' withoutSpaces |
|
9747 |
(' foo' , Character tab asString , ' ') withoutSpaces inspect |
|
608 | 9748 |
" |
9192 | 9749 |
! |
9750 |
||
23249 | 9751 |
withoutSuffix:aStringOrCharacter caseSensitive:caseSensitive |
23234 | 9752 |
"if the receiver endsWith aString, return a copy without it. |
9753 |
Otherwise return the receiver" |
|
9754 |
||
23249 | 9755 |
(self endsWith:aStringOrCharacter caseSensitive:caseSensitive) ifTrue:[ |
9756 |
aStringOrCharacter isCharacter ifTrue:[ |
|
9757 |
^ self copyButLast:1 |
|
9758 |
] ifFalse:[ |
|
9759 |
^ self copyButLast:aStringOrCharacter size |
|
9760 |
]. |
|
23234 | 9761 |
]. |
9762 |
^ self |
|
9763 |
||
9764 |
" |
|
9765 |
'helloworld' withoutSuffix:'world' |
|
9766 |
'helloworld' withoutSuffix:'foo' |
|
9767 |
" |
|
9768 |
||
9769 |
"Created: / 27-07-2018 / 08:30:10 / Claus Gittinger" |
|
23249 | 9770 |
"Modified: / 31-07-2018 / 17:06:58 / Claus Gittinger" |
23234 | 9771 |
! |
9772 |
||
9192 | 9773 |
withoutTrailingSeparators |
915
1ac1c7e942f4
added withoutTrailingSeparators
Claus Gittinger <cg@exept.de>
parents:
828
diff
changeset
|
9774 |
"return a copy of myself without trailing separators. |
1ac1c7e942f4
added withoutTrailingSeparators
Claus Gittinger <cg@exept.de>
parents:
828
diff
changeset
|
9775 |
Notice: this does remove tabs, newline or any other whitespace. |
1ac1c7e942f4
added withoutTrailingSeparators
Claus Gittinger <cg@exept.de>
parents:
828
diff
changeset
|
9776 |
Returns an empty string, if the receiver consist only of whitespace." |
1ac1c7e942f4
added withoutTrailingSeparators
Claus Gittinger <cg@exept.de>
parents:
828
diff
changeset
|
9777 |
|
12188 | 9778 |
^ self withoutTrailingForWhich:[:ch | ch isSeparator] |
915
1ac1c7e942f4
added withoutTrailingSeparators
Claus Gittinger <cg@exept.de>
parents:
828
diff
changeset
|
9779 |
|
1ac1c7e942f4
added withoutTrailingSeparators
Claus Gittinger <cg@exept.de>
parents:
828
diff
changeset
|
9780 |
" |
25099 | 9781 |
' foo ' asUnicodeString withoutTrailingSeparators |
9782 |
'foo ' asUnicodeString withoutTrailingSeparators |
|
9783 |
' foo' asUnicodeString withoutTrailingSeparators |
|
9784 |
' ' asUnicodeString withoutTrailingSeparators |
|
9785 |
'foo' asUnicodeString withoutTrailingSeparators |
|
9786 |
(' ' , Character tab, ' foo ') asUnicodeString withoutTrailingSeparators inspect |
|
9787 |
(' foo' , Character tab) asUnicodeString withoutTrailingSeparators inspect |
|
9788 |
" |
|
9789 |
||
9790 |
"Modified (comment): / 12-12-2019 / 14:15:09 / Stefan Vogel" |
|
9192 | 9791 |
! ! |
9792 |
||
25204 | 9793 |
|
9192 | 9794 |
!CharacterArray methodsFor:'substring searching'! |
9795 |
||
15055 | 9796 |
findRangeOfString:subString |
9797 |
"find a substring. if found, return the start- and endIndex; |
|
9798 |
if not found, return an empty interval." |
|
9799 |
||
9800 |
^ self rangeOfSubCollection:subString startingAt:1 ifAbsent:[0 to:-1] caseSensitive:true |
|
9801 |
||
9802 |
" |
|
9803 |
'hello world' findRangeOfString:'llo' |
|
9804 |
'hello world' findRangeOfString:'ole' |
|
9805 |
" |
|
9806 |
! |
|
9807 |
||
9192 | 9808 |
findString:subString |
9809 |
"find a substring. if found, return the index; |
|
9810 |
if not found, return 0." |
|
9811 |
||
9812 |
^ self indexOfSubCollection:subString startingAt:1 ifAbsent:0 caseSensitive:true |
|
9813 |
||
9814 |
" |
|
9815 |
'hello world' findString:'llo' |
|
9816 |
'hello world' findString:'ole' |
|
9817 |
" |
|
9818 |
! |
|
608 | 9819 |
|
15734 | 9820 |
findString:subString caseSensitive:caseSensitive |
9821 |
"find a substring. if found, return the index; |
|
9822 |
if not found, return 0." |
|
9823 |
||
9824 |
^ self indexOfSubCollection:subString startingAt:1 ifAbsent:0 caseSensitive:caseSensitive |
|
9825 |
||
9826 |
" |
|
9827 |
'hello world' findString:'LLo' caseSensitive:true |
|
9828 |
'hello world' findString:'LLo' caseSensitive:false |
|
9829 |
" |
|
9830 |
! |
|
9831 |
||
608 | 9832 |
findString:subString ifAbsent:exceptionBlock |
9833 |
"find a substring. If found, return the index; |
|
9834 |
if not found, return the result of evaluating exceptionBlock." |
|
9835 |
||
9087
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
9836 |
^ self indexOfSubCollection:subString startingAt:1 ifAbsent:exceptionBlock caseSensitive:true |
608 | 9837 |
! |
9838 |
||
11018 | 9839 |
findString:subString ignoreCase:ignoreCase |
15734 | 9840 |
<resource: #obsolete> |
11018 | 9841 |
"find a substring. if found, return the index; |
9842 |
if not found, return 0." |
|
9843 |
||
9844 |
^ self indexOfSubCollection:subString startingAt:1 ifAbsent:0 caseSensitive:ignoreCase not |
|
9845 |
||
9846 |
" |
|
9847 |
'hello world' findString:'LLo' ignoreCase:false |
|
9848 |
'hello world' findString:'LLo' ignoreCase:true |
|
9849 |
" |
|
9850 |
! |
|
9851 |
||
608 | 9852 |
findString:subString startingAt:index |
9853 |
"find a substring, starting at index. if found, return the index; |
|
9854 |
if not found, return 0." |
|
9855 |
||
9087
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
9856 |
^ self indexOfSubCollection:subString startingAt:index ifAbsent:0 caseSensitive:true |
608 | 9857 |
|
9858 |
" |
|
9192 | 9859 |
'hello yello' findString:'llo' startingAt:1 |
9860 |
'hello yello' findString:'llo' startingAt:5 |
|
9861 |
'hello yello' findString:'llo' startingAt:15 |
|
9862 |
" |
|
9863 |
! |
|
608 | 9864 |
|
9865 |
findString:subString startingAt:index ifAbsent:exceptionBlock |
|
9866 |
"find a substring, starting at index. if found, return the index; |
|
9867 |
if not found, return the result of evaluating exceptionBlock." |
|
9868 |
||
9087
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
9869 |
^ self indexOfSubCollection:subString startingAt:index ifAbsent:exceptionBlock caseSensitive:true |
608 | 9870 |
! |
9871 |
||
12177 | 9872 |
findString:subString startingAt:index ifAbsent:exceptionBlock caseSensitive:caseSensitive |
10929 | 9873 |
"find a substring, starting at index. if found, return the index; |
9874 |
if not found, return the result of evaluating exceptionBlock." |
|
9875 |
||
9876 |
^ self indexOfSubCollection:subString startingAt:index ifAbsent:exceptionBlock caseSensitive:caseSensitive |
|
9877 |
! |
|
9878 |
||
608 | 9879 |
includesString:aString |
9880 |
"return true, if a substring is contained in the receiver" |
|
9881 |
||
9087
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
9882 |
^ (self indexOfSubCollection:aString startingAt:1 ifAbsent:0 caseSensitive:true) ~~ 0 |
608 | 9883 |
|
9884 |
" |
|
12177 | 9885 |
'hello world' includesString:'hel' |
8874 | 9886 |
'hello world' includesString:'rld' |
9192 | 9887 |
'hello world' includesString:'llo' |
9888 |
'hello world' includesString:'LLO' |
|
12177 | 9889 |
'hello world' includesString:'' |
9192 | 9890 |
" |
9891 |
! |
|
9191 | 9892 |
|
9893 |
includesString:aString caseSensitive:caseSensitive |
|
9087
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
9894 |
"return true, if a substring is contained in the receiver" |
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
9895 |
|
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
9896 |
^ (self indexOfSubCollection:aString startingAt:1 ifAbsent:0 caseSensitive:caseSensitive) ~~ 0 |
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
9897 |
|
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
9898 |
" |
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
9899 |
'hello world' includesString:'hel' caseSensitive:true |
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
9900 |
'hello world' includesString:'HEL' caseSensitive:true |
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
9901 |
'hello world' includesString:'HEL' caseSensitive:false |
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
9902 |
|
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
9903 |
'hello world' includesString:'wor' caseSensitive:true |
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
9904 |
'hello world' includesString:'WOR' caseSensitive:true |
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
9905 |
'hello world' includesString:'WOR' caseSensitive:false |
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
9906 |
" |
9192 | 9907 |
! |
9908 |
||
23060 | 9909 |
indexOfString:aString |
9910 |
"VSE and V'age compatibility" |
|
9911 |
"find a substring. If found, return the index; if not found, return 0." |
|
9912 |
||
9913 |
^ self indexOfSubCollection:aString startingAt:1 ifAbsent:[0] caseSensitive:true |
|
9914 |
||
9915 |
" |
|
9916 |
'hello world' indexOfString:'hello' -> 1 |
|
9917 |
'hello world' indexOfString:'world' -> 7 |
|
9918 |
'hello world' indexOfString:'World' -> 0 |
|
9919 |
'hello world' indexOfString:'World' caseSensitive:false -> 7 |
|
9920 |
" |
|
9921 |
||
9922 |
"Modified (format): / 08-06-2018 / 14:43:15 / Claus Gittinger" |
|
9923 |
! |
|
9924 |
||
9925 |
indexOfString:aString caseSensitive:caseSensitive |
|
9926 |
"VSE and V'age compatibility" |
|
9927 |
"find a substring. If found, return the index; if not found, return 0." |
|
9928 |
||
9929 |
^ self indexOfSubCollection:aString startingAt:1 ifAbsent:[0] caseSensitive:caseSensitive |
|
9930 |
||
9931 |
" |
|
9932 |
'hello world' indexOfString:'hello' -> 1 |
|
9933 |
'hello world' indexOfString:'world' -> 7 |
|
9934 |
'hello world' indexOfString:'World' -> 0 |
|
9935 |
'hello world' indexOfString:'World' caseSensitive:false -> 7 |
|
9936 |
" |
|
9937 |
||
9938 |
"Created: / 08-06-2018 / 14:39:18 / Claus Gittinger" |
|
9939 |
! |
|
9940 |
||
9941 |
indexOfString:aString caseSensitive:caseSensitive ifAbsent:exceptionValue |
|
9942 |
"VSE and V'age compatibility" |
|
9943 |
"find a substring. If found, return the index; if not found, return 0." |
|
9944 |
||
9945 |
^ self indexOfSubCollection:aString startingAt:1 ifAbsent:exceptionValue caseSensitive:caseSensitive |
|
9946 |
||
9947 |
" |
|
9948 |
'abcdefabcdef' indexOfString:'fab' ifAbsent:[999] -> 6 |
|
9949 |
'abcdefabcdef' indexOfString:'Fab' ifAbsent:[999] -> 999 |
|
9950 |
'abcdefabcdef' indexOfString:'Fab' caseSensitive:false ifAbsent:[999] -> 6 |
|
9951 |
'abcdefabcdef' indexOfString:'xxx' caseSensitive:false ifAbsent:[999] -> 999 |
|
9952 |
" |
|
9953 |
||
9954 |
"Created: / 08-06-2018 / 14:40:21 / Claus Gittinger" |
|
9955 |
! |
|
9956 |
||
9957 |
indexOfString:aString caseSensitive:caseSensitive startingAt:startIndex |
|
9958 |
"VSE and V'age compatibility" |
|
9959 |
"find a substring. If found, return the index; if not found, return 0." |
|
9960 |
||
9961 |
^ self indexOfSubCollection:aString startingAt:startIndex ifAbsent:[0] caseSensitive:caseSensitive |
|
9962 |
||
9963 |
"Created: / 08-06-2018 / 14:44:17 / Claus Gittinger" |
|
9964 |
! |
|
9965 |
||
9966 |
indexOfString:aString caseSensitive:caseSensitive startingAt:startIndex ifAbsent:exceptionalValue |
|
9967 |
"VSE and V'age compatibility" |
|
9968 |
"find a substring. |
|
9969 |
If found, return the index; if not found, the value from exceptionalValue." |
|
9970 |
||
9971 |
^ self indexOfSubCollection:aString startingAt:startIndex ifAbsent:exceptionalValue caseSensitive:caseSensitive |
|
9972 |
||
9973 |
"Created: / 08-06-2018 / 14:42:33 / Claus Gittinger" |
|
9974 |
! |
|
9975 |
||
9976 |
indexOfString:aString ifAbsent:exceptionValue |
|
9977 |
"VSE and V'age compatibility" |
|
9978 |
"find a substring. If found, return the index; if not found, return 0." |
|
9979 |
||
9980 |
^ self indexOfSubCollection:aString startingAt:1 ifAbsent:exceptionValue caseSensitive:true |
|
9981 |
||
9982 |
" |
|
9983 |
'abcdefabcdef' indexOfString:'fab' ifAbsent:[999] -> 6 |
|
9984 |
'abcdefabcdef' indexOfString:'Fab' ifAbsent:[999] -> 999 |
|
9985 |
'abcdefabcdef' indexOfString:'Fab' caseSensitieve:false ifAbsent:[999] -> 999 |
|
9986 |
'abcdefabcdef' indexOfString:'xxx' ifAbsent:[999] -> 999 |
|
9987 |
" |
|
9988 |
||
9989 |
"Modified: / 08-06-2018 / 14:43:44 / Claus Gittinger" |
|
9990 |
! |
|
9991 |
||
9992 |
indexOfString:aString startingAt:startIndex |
|
9993 |
"VSE and V'age compatibility" |
|
9994 |
"find a substring. If found, return the index; if not found, return 0." |
|
9995 |
||
9996 |
^ self indexOfSubCollection:aString startingAt:startIndex ifAbsent:[0] caseSensitive:true |
|
9997 |
||
9998 |
"Modified: / 08-06-2018 / 14:37:52 / Claus Gittinger" |
|
9999 |
! |
|
10000 |
||
10001 |
indexOfString:aString startingAt:startIndex ifAbsent:exceptionalValue |
|
10002 |
"VSE and V'age compatibility" |
|
10003 |
"find a substring. |
|
10004 |
If found, return the index; if not found, the value from exceptionalValue." |
|
10005 |
||
10006 |
^ self indexOfSubCollection:aString startingAt:startIndex ifAbsent:exceptionalValue caseSensitive:true |
|
10007 |
||
10008 |
"Modified: / 08-06-2018 / 14:38:12 / Claus Gittinger" |
|
10009 |
! |
|
10010 |
||
18671 | 10011 |
indexOfSubCollection:subString caseSensitive:caseSensitive |
10012 |
"find a substring, starting at index. if found, return the index; |
|
21675 | 10013 |
if not found, return the result of evaluating exceptionBlock." |
18671 | 10014 |
|
10015 |
^ self indexOfSubCollection:subString startingAt:1 ifAbsent:[0] caseSensitive:caseSensitive. |
|
21675 | 10016 |
|
10017 |
"Modified (comment): / 28-03-2017 / 15:53:30 / stefan" |
|
18671 | 10018 |
! |
10019 |
||
9192 | 10020 |
indexOfSubCollection:subString startingAt:index ifAbsent:exceptionBlock |
10021 |
"find a substring, starting at index. if found, return the index; |
|
21675 | 10022 |
if not found, return the result of evaluating exceptionBlock." |
9192 | 10023 |
|
10024 |
^ self indexOfSubCollection:subString startingAt:index ifAbsent:exceptionBlock caseSensitive:true. |
|
21675 | 10025 |
|
10026 |
"Modified (comment): / 28-03-2017 / 15:53:14 / stefan" |
|
9192 | 10027 |
! |
10028 |
||
10029 |
indexOfSubCollection:subString startingAt:index ifAbsent:exceptionBlock caseSensitive:caseSensitive |
|
9087
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
10030 |
"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
|
10031 |
if not found, return the result of evaluating exceptionBlock. |
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
10032 |
This is a q&d hack - not very efficient" |
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
10033 |
|
608 | 10034 |
|firstChar found |
10035 |
startIndex "{ Class: SmallInteger }" |
|
10036 |
subSize "{ Class: SmallInteger }" |
|
10037 |
mySize "{ Class: SmallInteger }" |
|
21675 | 10038 |
runIdx "{ Class: SmallInteger }"| |
608 | 10039 |
|
10040 |
subSize := subString size. |
|
12177 | 10041 |
subSize == 0 ifTrue:[ |
20874 | 10042 |
subString isString ifFalse:[ |
23001 | 10043 |
self proceedableError:'non string argument'. |
20874 | 10044 |
]. |
10045 |
"empty string does not match" |
|
10046 |
^ 0. |
|
10047 |
"empty string matches" |
|
15433 | 10048 |
"/ ^ index |
6122
a98958f1c85f
handle non-string arg to indexOfSubString:
Claus Gittinger <cg@exept.de>
parents:
6119
diff
changeset
|
10049 |
]. |
9087
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
10050 |
|
608 | 10051 |
mySize := self size. |
10052 |
firstChar := subString at:1. |
|
9087
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
10053 |
caseSensitive ifTrue:[ |
20874 | 10054 |
startIndex := self indexOf:firstChar startingAt:index. |
9087
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
10055 |
] ifFalse:[ |
20874 | 10056 |
startIndex := self findFirst:[:c | c sameAs:firstChar] startingAt:index. |
9087
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
10057 |
]. |
21672 | 10058 |
[startIndex ~~ 0] whileTrue:[ |
20874 | 10059 |
runIdx := startIndex. |
10060 |
found := true. |
|
10061 |
1 to:subSize do:[:i | |
|
10062 |
runIdx > mySize ifTrue:[ |
|
10063 |
found := false |
|
10064 |
] ifFalse:[ |
|
21675 | 10065 |
caseSensitive ifTrue:[ |
10066 |
(subString at:i) ~= (self at:runIdx) ifTrue:[found := false]. |
|
10067 |
] ifFalse:[ |
|
10068 |
((subString at:i) sameAs:(self at:runIdx)) ifFalse:[found := false]. |
|
10069 |
]. |
|
20874 | 10070 |
]. |
10071 |
runIdx := runIdx + 1 |
|
10072 |
]. |
|
10073 |
found ifTrue:[ |
|
10074 |
^ startIndex |
|
10075 |
]. |
|
10076 |
caseSensitive ifTrue:[ |
|
10077 |
startIndex := self indexOf:firstChar startingAt:(startIndex + 1) |
|
10078 |
] ifFalse:[ |
|
10079 |
startIndex := self findFirst:[:c | c sameAs:firstChar] startingAt:(startIndex + 1). |
|
10080 |
]. |
|
608 | 10081 |
]. |
10082 |
^ exceptionBlock value |
|
1005
4507487e0be6
more on encoding/decoding (still unfinished)
Claus Gittinger <cg@exept.de>
parents:
997
diff
changeset
|
10083 |
|
21675 | 10084 |
" |
10085 |
'АБВГДЕЖ' asLowercase indexOfSubCollection:'ВГДЕ' startingAt:1 ifAbsent:nil caseSensitive:false. |
|
10086 |
'АБВГДЕЖ' indexOfSubCollection:'ВГДЕ' startingAt:1 ifAbsent:nil caseSensitive:true. |
|
10087 |
" |
|
10088 |
||
21672 | 10089 |
"Modified: / 23-02-1996 / 15:35:15 / cg" |
21675 | 10090 |
"Modified (comment): / 28-03-2017 / 16:05:48 / stefan" |
23001 | 10091 |
"Modified: / 24-05-2018 / 14:55:38 / Claus Gittinger" |
9192 | 10092 |
! |
10093 |
||
17191 | 10094 |
indexOfSubCollection:subString startingAt:index ifAbsent:exceptionBlock caseSensitive:caseSensitive ignoreDiacritics:ignoreDiacritics |
10095 |
"find a substring, starting at index. if found, return the index; |
|
10096 |
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
|
10097 |
This is a q&d hack - not very efficient |
17191 | 10098 |
(see implementation in string, for a much faster algorithm)" |
10099 |
||
10100 |
|firstChar found |
|
10101 |
startIndex "{ Class: SmallInteger }" |
|
10102 |
subSize "{ Class: SmallInteger }" |
|
10103 |
mySize "{ Class: SmallInteger }" |
|
10104 |
runIdx "{ Class: SmallInteger }" |
|
10105 |
tester charMap firstCharMapped findNextIndex| |
|
10106 |
||
10107 |
subSize := subString size. |
|
10108 |
subSize == 0 ifTrue:[ |
|
20874 | 10109 |
subString isString ifFalse:[ |
23001 | 10110 |
self proceedableError:'non string argument'. |
20874 | 10111 |
]. |
10112 |
"empty string does not match" |
|
10113 |
^ 0. |
|
10114 |
"empty string matches" |
|
17191 | 10115 |
"/ ^ index |
10116 |
]. |
|
10117 |
||
10118 |
mySize := self size. |
|
10119 |
firstChar := subString at:1. |
|
10120 |
ignoreDiacritics ifTrue:[ |
|
20874 | 10121 |
caseSensitive ifTrue:[ |
10122 |
charMap := [:ch | ch asLowercase withoutDiacritics]. |
|
10123 |
] ifFalse:[ |
|
10124 |
charMap := [:ch | ch withoutDiacritics]. |
|
10125 |
]. |
|
10126 |
tester := [:c1 :c2 | (charMap value:c1) = (charMap value:c2) ]. |
|
10127 |
firstCharMapped := (charMap value:firstChar). |
|
10128 |
findNextIndex := [:index | self findFirst:[:ch | (charMap value:ch) = firstCharMapped] startingAt:index]. |
|
17191 | 10129 |
] ifFalse:[ |
20874 | 10130 |
caseSensitive ifTrue:[ |
10131 |
tester := [:c1 :c2 | c1 = c2 ]. |
|
10132 |
findNextIndex := [:index | self indexOf:firstChar startingAt:index]. |
|
10133 |
] ifFalse:[ |
|
10134 |
tester := [:c1 :c2 | c1 sameAs: c2 ]. |
|
10135 |
findNextIndex := [:index | self findFirst:[:c | c sameAs:firstChar] startingAt:index]. |
|
10136 |
]. |
|
17191 | 10137 |
]. |
10138 |
startIndex := findNextIndex value:index. |
|
10139 |
[startIndex == 0] whileFalse:[ |
|
20874 | 10140 |
runIdx := startIndex. |
10141 |
found := true. |
|
10142 |
1 to:subSize do:[:i | |
|
10143 |
runIdx > mySize ifTrue:[ |
|
10144 |
found := false |
|
10145 |
] ifFalse:[ |
|
10146 |
(tester value:(subString at:i) value:(self at:runIdx)) ifFalse:[ |
|
10147 |
found := false |
|
10148 |
] |
|
10149 |
]. |
|
10150 |
runIdx := runIdx + 1 |
|
10151 |
]. |
|
10152 |
found ifTrue:[ |
|
10153 |
^ startIndex |
|
10154 |
]. |
|
10155 |
startIndex := findNextIndex value:(startIndex + 1) |
|
17191 | 10156 |
]. |
10157 |
^ exceptionBlock value |
|
10158 |
||
10159 |
" |
|
17600 | 10160 |
'bla depot bla dépots' indexOfSubCollection:'dep' startingAt:1 ifAbsent:0 caseSensitive:false ignoreDiacritics:true. 5 |
10161 |
'bla depot bla dépots' indexOfSubCollection:'dep' startingAt:6 ifAbsent:0 caseSensitive:false ignoreDiacritics:true. 0 |
|
10162 |
||
10163 |
'bla depot bla dépots' indexOfSubCollection:'dep' startingAt:1 ifAbsent:0 caseSensitive:false ignoreDiacritics:false. 5 |
|
10164 |
'bla depot bla dépots' indexOfSubCollection:'dep' startingAt:6 ifAbsent:0 caseSensitive:false ignoreDiacritics:false. 0 |
|
17191 | 10165 |
" |
23001 | 10166 |
|
10167 |
"Modified: / 24-05-2018 / 14:55:43 / Claus Gittinger" |
|
17191 | 10168 |
! |
10169 |
||
18939 | 10170 |
lastIndexOfString:aString |
10171 |
"VSE and V'age compatibility" |
|
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
10172 |
"find the last occurrence of substring. |
18939 | 10173 |
If found, return the index; if not found, return 0." |
10174 |
||
10175 |
^ self lastIndexOfSubCollection:aString startingAt:(self size-aString size+1) ifAbsent:[0] |
|
10176 |
||
10177 |
" 123456789012 |
|
10178 |
'abcdefabcdef' lastIndexOfString:'abc' |
|
10179 |
'abcdefabcdef' lastIndexOfString:'abc' startingAt:6 |
|
10180 |
'abcdefabcdef' lastIndexOfString:'xxx' startingAt:6 |
|
10181 |
" |
|
10182 |
! |
|
10183 |
||
10184 |
lastIndexOfString:aString ifAbsent:exceptionValue |
|
10185 |
"VSE and V'age compatibility" |
|
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
10186 |
"find the last occurrence of substring. |
18939 | 10187 |
If found, return the index; if not found, return 0." |
10188 |
||
10189 |
^ self lastIndexOfSubCollection:aString startingAt:(self size-aString size+1) ifAbsent:exceptionValue |
|
10190 |
||
10191 |
" 123456789012 |
|
10192 |
'abcdefabcdef' lastIndexOfString:'abc' ifAbsent:[999] |
|
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
10193 |
'abcdefabcdef' lastIndexOfString:'xxx' ifAbsent:[999] |
18939 | 10194 |
" |
10195 |
! |
|
10196 |
||
10197 |
lastIndexOfString:aString startingAt:startIndex |
|
10198 |
"VSE and V'age compatibility" |
|
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
10199 |
"find the last occurrence of a substring. |
18939 | 10200 |
If found, return the index; if not found, return 0." |
10201 |
||
10202 |
^ self lastIndexOfSubCollection:aString startingAt:startIndex ifAbsent:[0] |
|
10203 |
||
10204 |
" 123456789012 |
|
10205 |
'abcdefabcdef' lastIndexOfString:'abc' |
|
10206 |
'abcdefabcdef' lastIndexOfString:'abc' startingAt:6 |
|
10207 |
" |
|
10208 |
! |
|
10209 |
||
10210 |
lastIndexOfString:aString startingAt:startIndex ifAbsent:exceptionValue |
|
10211 |
"VSE and V'age compatibility" |
|
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
10212 |
"find the last occurrence of a substring. |
18939 | 10213 |
If found, return the index; if not found, return 0." |
10214 |
||
10215 |
^ self lastIndexOfSubCollection:aString startingAt:startIndex ifAbsent:exceptionValue |
|
10216 |
||
10217 |
" 123456789012 |
|
10218 |
'abcdefabcdef' lastIndexOfString:'abc' |
|
10219 |
'abcdefabcdef' lastIndexOfString:'abc' startingAt:6 |
|
10220 |
'abcdefabcdef' lastIndexOfString:'xxx' startingAt:6 ifAbsent:999 |
|
10221 |
" |
|
10222 |
! |
|
10223 |
||
24145 | 10224 |
occurrencesOfString:aSubString |
10225 |
"count how often the argument aSubString is contained in the receiver" |
|
10226 |
||
10227 |
^ self occurrencesOfString:aSubString caseSensitive:true |
|
10228 |
||
10229 |
" |
|
10230 |
'aa' indexOfString:'aa' startingAt:1 |
|
10231 |
||
10232 |
'' occurrencesOfString:'aa' |
|
10233 |
'a' occurrencesOfString:'aa' |
|
10234 |
'aa' occurrencesOfString:'aa' |
|
10235 |
' aa ' occurrencesOfString:'aa' |
|
10236 |
' aa a' occurrencesOfString:'aa' |
|
10237 |
' aaaa' occurrencesOfString:'aa' |
|
10238 |
' aa aa ' occurrencesOfString:'aa' |
|
10239 |
' aa bb ab ba aa ab' occurrencesOfString:'aa' |
|
10240 |
' aa bb cc aa bb cc aa bb ' occurrencesOfString:'aa' |
|
10241 |
" |
|
10242 |
||
10243 |
"Created: / 25-05-2019 / 08:57:14 / Claus Gittinger" |
|
10244 |
! |
|
10245 |
||
10246 |
occurrencesOfString:aSubString caseSensitive:caseSensitive |
|
10247 |
"count how often the argument aSubString is contained in the receiver" |
|
10248 |
||
10249 |
|idx count| |
|
10250 |
||
10251 |
idx := 1. |
|
10252 |
count := 0. |
|
10253 |
[idx ~~ 0] whileTrue:[ |
|
10254 |
idx := self indexOfString:aSubString caseSensitive:caseSensitive startingAt:idx. |
|
10255 |
idx ~~ 0 ifTrue:[ |
|
10256 |
count := count + 1. |
|
10257 |
idx := idx + aSubString size |
|
10258 |
] |
|
10259 |
]. |
|
10260 |
^ count |
|
10261 |
||
10262 |
" |
|
10263 |
'aa' indexOfString:'aa' startingAt:1 |
|
10264 |
||
10265 |
'' occurrencesOfString:'aA' caseSensitive:false |
|
10266 |
'a' occurrencesOfString:'aA' caseSensitive:false |
|
10267 |
'aa' occurrencesOfString:'aA' caseSensitive:false |
|
10268 |
' aa ' occurrencesOfString:'aA' caseSensitive:false |
|
10269 |
' aa a' occurrencesOfString:'aA' caseSensitive:false |
|
10270 |
' aaaa' occurrencesOfString:'aA' caseSensitive:false |
|
10271 |
' aa aa ' occurrencesOfString:'aA' caseSensitive:false |
|
10272 |
' aa bb ab ba aa ab' occurrencesOfString:'aA' caseSensitive:false |
|
10273 |
' aa bb cc aa bb cc aa bb ' occurrencesOfString:'aA' caseSensitive:false |
|
10274 |
" |
|
10275 |
||
10276 |
"Created: / 25-05-2019 / 09:06:46 / Claus Gittinger" |
|
10277 |
! |
|
10278 |
||
15055 | 10279 |
rangeOfSubCollection:subString startingAt:start ifAbsent:exceptionValue caseSensitive:caseSensitive |
10280 |
"find a substring. if found, return the start- and endIndex; |
|
10281 |
if not found, return the value of exceptionValue." |
|
10282 |
||
10283 |
|i| |
|
10284 |
||
10285 |
i := self indexOfSubCollection:subString startingAt:start ifAbsent:0 caseSensitive:caseSensitive. |
|
10286 |
i == 0 ifTrue:[ |
|
20874 | 10287 |
^ exceptionValue value |
15055 | 10288 |
]. |
10289 |
^ i to:(i + subString size - 1) |
|
10290 |
||
10291 |
" |
|
10292 |
'hello world' findRangeOfString:'llo' |
|
10293 |
'hello world' findRangeOfString:'ole' |
|
10294 |
" |
|
9192 | 10295 |
! ! |
10296 |
||
10297 |
!CharacterArray methodsFor:'testing'! |
|
10298 |
||
18893 | 10299 |
isPlainString |
10300 |
"return true, if the receiver is a plain string (without attributes); |
|
10301 |
true is returned here - redefinition of Object>>isPlainString." |
|
10302 |
||
10303 |
^ true |
|
10304 |
! |
|
10305 |
||
9230 | 10306 |
isString |
10307 |
"return true, if the receiver is some kind of string; |
|
10308 |
true is returned here - redefinition of Object>>isString." |
|
10309 |
||
10310 |
^ true |
|
10311 |
! |
|
10312 |
||
21126 | 10313 |
isUnary |
10314 |
"Answer true if the receiver is a unary selector. |
|
10315 |
That is not a check for being a valid selector, but instead relies on |
|
10316 |
the selector being valid." |
|
10317 |
||
10318 |
^ self isUnarySelector |
|
10319 |
! |
|
10320 |
||
9230 | 10321 |
isUnarySelector |
10322 |
"Answer true if the receiver contains only chars in an ANSI unary method selector, false otherwise." |
|
10323 |
||
17442 | 10324 |
^ (self first isLetterOrUnderline) |
24254 | 10325 |
and:[ self conform: [ :chr | chr isLetterOrDigitOrUnderline ]] |
9845 | 10326 |
|
10327 |
"Modified: / 13-09-2006 / 11:35:15 / cg" |
|
24254 | 10328 |
"Modified: / 05-06-2019 / 17:05:59 / Claus Gittinger" |
9230 | 10329 |
! |
10330 |
||
14122 | 10331 |
isUnicode16String |
10332 |
^ false |
|
10333 |
! |
|
10334 |
||
10335 |
isUnicode32String |
|
19939 | 10336 |
"true if this is a 4-byte unicode string" |
10337 |
||
14122 | 10338 |
^ false |
10339 |
! |
|
10340 |
||
10341 |
isUnicodeString |
|
19938 | 10342 |
"true if this is a 2- or 4-byte unicode string |
10343 |
(i.e. not a single byte string). |
|
20194 | 10344 |
Notice, that the name is misleading: |
24939 | 10345 |
all strings use unicode encoding" |
20194 | 10346 |
|
14122 | 10347 |
^ false |
24939 | 10348 |
|
10349 |
"Modified (comment): / 21-11-2019 / 18:25:19 / Stefan Vogel" |
|
14122 | 10350 |
! |
10351 |
||
14555 | 10352 |
isWideString |
19580 | 10353 |
"true if I require more than one byte per character" |
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
10354 |
|
15829 | 10355 |
|string| |
10356 |
||
10357 |
(string := self string) ~~ self ifTrue:[ |
|
20874 | 10358 |
^ string isWideString. |
15829 | 10359 |
]. |
10360 |
^ self contains:[:aCharacter | aCharacter codePoint > 16rFF]. |
|
64 | 10361 |
! ! |
10362 |
||
9192 | 10363 |
!CharacterArray methodsFor:'tracing'! |
10364 |
||
10365 |
traceInto:aRequestor level:level from:referrer |
|
10366 |
"double dispatch into tracer, passing my type implicitely in the selector" |
|
10367 |
||
10368 |
^ aRequestor traceCharacterArray:self level:level from:referrer |
|
10369 |
||
10370 |
||
10371 |
! ! |
|
10372 |
||
10373 |
!CharacterArray methodsFor:'visiting'! |
|
8395
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8376
diff
changeset
|
10374 |
|
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8376
diff
changeset
|
10375 |
acceptVisitor:aVisitor with:aParameter |
16733 | 10376 |
"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
|
10377 |
|
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8376
diff
changeset
|
10378 |
^ aVisitor visitString:self with:aParameter |
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8376
diff
changeset
|
10379 |
! ! |
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8376
diff
changeset
|
10380 |
|
25217 | 10381 |
|
1849
9f35f5934ec6
added msdos-latin1 en/decoder (code = #msdos850)
Claus Gittinger <cg@exept.de>
parents:
1669
diff
changeset
|
10382 |
!CharacterArray class methodsFor:'documentation'! |
629 | 10383 |
|
10384 |
version |
|
18476 | 10385 |
^ '$Header$' |
12058 | 10386 |
! |
10387 |
||
10388 |
version_CVS |
|
18476 | 10389 |
^ '$Header$' |
629 | 10390 |
! ! |
6809 | 10391 |
|
14606 | 10392 |
|
2728
7569e45a4d15
raise error signal if anything goes wrong while decoding.
Claus Gittinger <cg@exept.de>
parents:
2721
diff
changeset
|
10393 |
CharacterArray initialize! |