author | Claus Gittinger <cg@exept.de> |
Tue, 09 Jul 2019 20:55:17 +0200 | |
changeset 24417 | 03b083548da2 |
parent 24411 | 96a7c2830fad |
child 24481 | c70408eab4ad |
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 |
24041 | 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 |
||
273 |
"skip whiteSpace" |
|
274 |
str skipSeparators. |
|
11320 | 275 |
|
14452
ef3591302afa
changed: #readSmalltalkStringFrom:onError:
Stefan Vogel <sv@exept.de>
parents:
14451
diff
changeset
|
276 |
(str peekOrNil == $') ifTrue:[ |
20874 | 277 |
str next. |
278 |
collected := self writeStream. |
|
279 |
[str atEnd] whileFalse:[ |
|
280 |
char := str next. |
|
281 |
char == $' ifTrue:[ |
|
282 |
"/ look for another quote |
|
283 |
str peekOrNil ~~ $' ifTrue:[ |
|
284 |
"end of string reached" |
|
285 |
^ collected contents. |
|
286 |
]. |
|
287 |
"eat doubled quote" |
|
288 |
str next. |
|
289 |
]. |
|
290 |
((char ~~ Character return) or:[str peekOrNil ~~ Character lf]) ifTrue:[ |
|
291 |
"compress CRLF to LF, but keep a single CR" |
|
292 |
collected nextPut:char. |
|
293 |
]. |
|
294 |
]. |
|
295 |
"if we come here, we reached the end without finding a closing $'" |
|
9425 | 296 |
]. |
297 |
^ exceptionBlock value |
|
298 |
||
299 |
" |
|
300 |
String readFrom:('''hello world''' readStream) |
|
301 |
String readFrom:('''hello '''' world''' readStream) |
|
302 |
String readFrom:('1 ''hello'' ' readStream) |
|
303 |
String readFrom:('1 ''hello'' ' readStream) onError:['foobar'] |
|
304 |
" |
|
305 |
||
306 |
"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
|
307 |
"Modified: / 06-10-2006 / 14:05:32 / cg" |
11243
4bf099f5bb56
CharacterArray class>>#writeStream - answer a CharacterWriteStrem
Stefan Vogel <sv@exept.de>
parents:
11229
diff
changeset
|
308 |
! |
4bf099f5bb56
CharacterArray class>>#writeStream - answer a CharacterWriteStrem
Stefan Vogel <sv@exept.de>
parents:
11229
diff
changeset
|
309 |
|
13201 | 310 |
writeStreamClass |
21145 | 311 |
"the type of stream used in writeStream. |
17213 | 312 |
Here, we return CharacterWriteStream, which automatically changes |
313 |
the underlying collection to a multiByte string (i.e. UnicodeString). |
|
314 |
So you can stream wide characters into it." |
|
13201 | 315 |
|
316 |
^ CharacterWriteStream |
|
11243
4bf099f5bb56
CharacterArray class>>#writeStream - answer a CharacterWriteStrem
Stefan Vogel <sv@exept.de>
parents:
11229
diff
changeset
|
317 |
|
4bf099f5bb56
CharacterArray class>>#writeStream - answer a CharacterWriteStrem
Stefan Vogel <sv@exept.de>
parents:
11229
diff
changeset
|
318 |
" |
4bf099f5bb56
CharacterArray class>>#writeStream - answer a CharacterWriteStrem
Stefan Vogel <sv@exept.de>
parents:
11229
diff
changeset
|
319 |
String writeStream |
21145 | 320 |
|
321 |
- the following delivers a Unicode16String: |
|
322 |
||
323 |
String streamContents:[:s | |
|
324 |
s nextPut:(Character value:16r3f30). |
|
325 |
s nextPutAll:'hello'. |
|
326 |
s cr. |
|
327 |
] |
|
11243
4bf099f5bb56
CharacterArray class>>#writeStream - answer a CharacterWriteStrem
Stefan Vogel <sv@exept.de>
parents:
11229
diff
changeset
|
328 |
" |
13201 | 329 |
|
330 |
"Created: / 09-01-2011 / 10:37:57 / cg" |
|
64 | 331 |
! ! |
332 |
||
9265 | 333 |
!CharacterArray class methodsFor:'Compatibility-VW'! |
334 |
||
335 |
fromIntegerArray: anArray |
|
336 |
"Answer a new instance of the receiver that is created from the argument, anArray." |
|
337 |
||
338 |
| new | |
|
339 |
||
340 |
new := self new: anArray size. |
|
341 |
1 to: anArray size do:[:index | |
|
20874 | 342 |
new at: index put: (anArray at: index) asCharacter |
9265 | 343 |
]. |
344 |
^new |
|
345 |
||
346 |
" |
|
347 |
String fromIntegerArray: #[8 127] |
|
348 |
" |
|
349 |
! ! |
|
350 |
||
2728
7569e45a4d15
raise error signal if anything goes wrong while decoding.
Claus Gittinger <cg@exept.de>
parents:
2721
diff
changeset
|
351 |
!CharacterArray class methodsFor:'Signal constants'! |
7569e45a4d15
raise error signal if anything goes wrong while decoding.
Claus Gittinger <cg@exept.de>
parents:
2721
diff
changeset
|
352 |
|
7569e45a4d15
raise error signal if anything goes wrong while decoding.
Claus Gittinger <cg@exept.de>
parents:
2721
diff
changeset
|
353 |
decodingFailedSignal |
2833 | 354 |
"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
|
355 |
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
|
356 |
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
|
357 |
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
|
358 |
|
22432 | 359 |
^ DecodingError |
360 |
||
361 |
"Created: / 28-06-1997 / 20:09:55 / cg" |
|
362 |
"Modified: / 03-08-1997 / 18:16:47 / cg" |
|
363 |
"Modified: / 16-01-2018 / 18:58:10 / stefan" |
|
2833 | 364 |
! |
365 |
||
366 |
encodingFailedSignal |
|
367 |
"return the (query-) signal, raised when encoding of a string is not possible |
|
368 |
due to invalid characters contained in the source." |
|
369 |
||
22432 | 370 |
^ EncodingError |
371 |
||
372 |
"Modified: / 28-06-1997 / 20:09:35 / cg" |
|
373 |
"Created: / 03-08-1997 / 18:16:40 / cg" |
|
374 |
"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
|
375 |
! ! |
7569e45a4d15
raise error signal if anything goes wrong while decoding.
Claus Gittinger <cg@exept.de>
parents:
2721
diff
changeset
|
376 |
|
24250 | 377 |
|
1849
9f35f5934ec6
added msdos-latin1 en/decoder (code = #msdos850)
Claus Gittinger <cg@exept.de>
parents:
1669
diff
changeset
|
378 |
!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
|
379 |
|
f0b45a4b82d2
first attempt in encoding/decoding for different character representations (mac/msdos etc.)
Claus Gittinger <cg@exept.de>
parents:
986
diff
changeset
|
380 |
lowSpaceCleanup |
f0b45a4b82d2
first attempt in encoding/decoding for different character representations (mac/msdos etc.)
Claus Gittinger <cg@exept.de>
parents:
986
diff
changeset
|
381 |
"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
|
382 |
|
f0b45a4b82d2
first attempt in encoding/decoding for different character representations (mac/msdos etc.)
Claus Gittinger <cg@exept.de>
parents:
986
diff
changeset
|
383 |
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
|
384 |
|
f0b45a4b82d2
first attempt in encoding/decoding for different character representations (mac/msdos etc.)
Claus Gittinger <cg@exept.de>
parents:
986
diff
changeset
|
385 |
" |
f0b45a4b82d2
first attempt in encoding/decoding for different character representations (mac/msdos etc.)
Claus Gittinger <cg@exept.de>
parents:
986
diff
changeset
|
386 |
CharacterArray lowSpaceCleanup |
f0b45a4b82d2
first attempt in encoding/decoding for different character representations (mac/msdos etc.)
Claus Gittinger <cg@exept.de>
parents:
986
diff
changeset
|
387 |
" |
f0b45a4b82d2
first attempt in encoding/decoding for different character representations (mac/msdos etc.)
Claus Gittinger <cg@exept.de>
parents:
986
diff
changeset
|
388 |
|
f0b45a4b82d2
first attempt in encoding/decoding for different character representations (mac/msdos etc.)
Claus Gittinger <cg@exept.de>
parents:
986
diff
changeset
|
389 |
"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
|
390 |
"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
|
391 |
! ! |
f0b45a4b82d2
first attempt in encoding/decoding for different character representations (mac/msdos etc.)
Claus Gittinger <cg@exept.de>
parents:
986
diff
changeset
|
392 |
|
7421 | 393 |
!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
|
394 |
|
7876 | 395 |
decodeFromUTF8:aStringOrByteCollection |
19837 | 396 |
"given a string in UTF8 encoding, |
397 |
return a new string containing the same characters, in Unicode encoding. |
|
398 |
Returns either a normal String, a Unicode16String or a Unicode32String instance. |
|
399 |
This is only useful, when reading from external sources or communicating with |
|
400 |
other systems |
|
401 |
(ST/X never uses utf8 internally, but always uses strings of fully decoded unicode characters). |
|
402 |
This only handles up-to 30bit characters." |
|
403 |
||
21379 | 404 |
|decodedSize nBitsRequired |
405 |
codepoint "{ Class: SmallInteger }" |
|
19837 | 406 |
byte "{ Class: SmallInteger }" |
21379 | 407 |
encodedSize "{ Class: SmallInteger }" |
19837 | 408 |
srcIdx "{ Class: SmallInteger }" |
409 |
idx "{ Class: SmallInteger }" |
|
410 |
nFollowBytes "{ Class: SmallInteger }" |
|
411 |
minValue "{ Class: SmallInteger }" |
|
412 |
newString| |
|
413 |
||
21379 | 414 |
encodedSize := aStringOrByteCollection size. |
415 |
decodedSize := aStringOrByteCollection utf8DecodedSize. |
|
416 |
||
417 |
encodedSize == decodedSize ifTrue:[ |
|
418 |
"/ fast track, also avoid creation of new strings if aStringOrByteCollection is already a 7-bit string |
|
23400 | 419 |
^ aStringOrByteCollection asSingleByteStringIfPossible |
19837 | 420 |
]. |
421 |
||
422 |
nBitsRequired := 8. |
|
423 |
srcIdx := 1. |
|
424 |
||
21379 | 425 |
"this loop is only used to find the largest character" |
426 |
[srcIdx <= encodedSize] whileTrue:[ |
|
427 |
byte := aStringOrByteCollection byteAt:srcIdx. |
|
19837 | 428 |
srcIdx := srcIdx + 1. |
21379 | 429 |
byte > 16r7F ifTrue:[ |
19837 | 430 |
(byte bitAnd:2r11100000) == 2r11000000 ifTrue:[ |
431 |
"/ 80 .. 7FF |
|
21379 | 432 |
nBitsRequired < 16 ifTrue:[ |
433 |
codepoint := ((byte bitAnd:2r00011111) bitShift:6) |
|
434 |
+ ((aStringOrByteCollection byteAt:srcIdx) bitAnd:2r00111111). |
|
435 |
codepoint > 16rFF ifTrue:[ |
|
436 |
nBitsRequired := 16. |
|
437 |
]. |
|
438 |
]. |
|
439 |
srcIdx := srcIdx + 1. |
|
19837 | 440 |
] ifFalse:[(byte bitAnd:2r11110000) == 2r11100000 ifTrue:[ |
441 |
"/ 800 .. FFFF |
|
21379 | 442 |
srcIdx := srcIdx + 2. |
443 |
nBitsRequired < 16 ifTrue:[nBitsRequired := 16]. |
|
19837 | 444 |
] ifFalse:[(byte bitAnd:2r11111000) == 2r11110000 ifTrue:[ |
445 |
"/ 10000 .. 1FFFFF |
|
21379 | 446 |
srcIdx := srcIdx + 3. |
447 |
nBitsRequired := 32. |
|
19837 | 448 |
] ifFalse:[(byte bitAnd:2r11111100) == 2r11111000 ifTrue:[ |
21379 | 449 |
"5 byte sequences do not result in valid unicode" |
19837 | 450 |
"/ 200000 .. 3FFFFFF |
21379 | 451 |
srcIdx := srcIdx + 4. |
452 |
nBitsRequired := 32. |
|
19837 | 453 |
] ifFalse:[(byte bitAnd:2r11111110) == 2r11111100 ifTrue:[ |
21379 | 454 |
"6 byte sequences do not result in valid unicode" |
19837 | 455 |
"/ 4000000 .. 7FFFFFFF |
21379 | 456 |
(byte bitAnd:2r00000001) ~~ 0 ifTrue:[ |
457 |
"/ ST/X can only represent up to 30 bit characters. |
|
19837 | 458 |
"/ but the max unicode character is defined as 16r10 FFFF anyway |
459 |
^ DecodingError newException |
|
460 |
defaultValue:aStringOrByteCollection; |
|
21145 | 461 |
raiseRequestWith:aStringOrByteCollection |
462 |
errorString:('unicode character out of range at: %1' bindWith:srcIdx). |
|
19837 | 463 |
]. |
21379 | 464 |
srcIdx := srcIdx + 5. |
465 |
nBitsRequired := 32. |
|
19837 | 466 |
] ifFalse:[ |
467 |
^ DecodingError newException |
|
468 |
defaultValue:aStringOrByteCollection; |
|
469 |
raiseRequestWith:aStringOrByteCollection errorString:'invalid utf8 encoding'. |
|
470 |
]]]]]. |
|
471 |
]. |
|
472 |
]. |
|
473 |
||
474 |
nBitsRequired == 8 ifTrue:[ |
|
21379 | 475 |
newString := String uninitializedNew:decodedSize |
476 |
] ifFalse:[nBitsRequired == 16 ifTrue:[ |
|
477 |
newString := Unicode16String new:decodedSize |
|
19837 | 478 |
] ifFalse:[ |
21379 | 479 |
newString := Unicode32String new:decodedSize |
19837 | 480 |
]]. |
481 |
||
482 |
idx := srcIdx := 1. |
|
483 |
||
484 |
"now fill the string" |
|
21379 | 485 |
[srcIdx <= encodedSize] whileTrue:[ |
486 |
byte := codepoint := aStringOrByteCollection byteAt:srcIdx. |
|
19837 | 487 |
srcIdx := srcIdx + 1. |
21379 | 488 |
byte > 16r7F ifTrue:[ |
19837 | 489 |
(byte bitAnd:2r11100000) == 2r11000000 ifTrue:[ |
21379 | 490 |
codepoint := byte bitAnd:2r00011111. |
19837 | 491 |
nFollowBytes := 1. |
21379 | 492 |
minValue := 16r80. |
19837 | 493 |
] ifFalse:[(byte bitAnd:2r11110000) == 2r11100000 ifTrue:[ |
21379 | 494 |
codepoint := byte bitAnd:2r00001111. |
19837 | 495 |
nFollowBytes := 2. |
21379 | 496 |
minValue := 16r800. |
19837 | 497 |
] ifFalse:[(byte bitAnd:2r11111000) == 2r11110000 ifTrue:[ |
21379 | 498 |
codepoint := byte bitAnd:2r00000111. |
19837 | 499 |
nFollowBytes := 3. |
21379 | 500 |
minValue := 16r10000. |
19837 | 501 |
] ifFalse:[(byte bitAnd:2r11111100) == 2r11111000 ifTrue:[ |
502 |
"5 byte sequences do not result in valid unicode" |
|
21379 | 503 |
codepoint := byte bitAnd:2r00000011. |
19837 | 504 |
nFollowBytes := 4. |
21379 | 505 |
minValue := 16r200000. |
19837 | 506 |
] ifFalse:[(byte bitAnd:2r11111110) == 2r11111100 ifTrue:[ |
507 |
"6 byte sequences do not result in valid unicode" |
|
21379 | 508 |
codepoint := byte bitAnd:2r00000001. |
19837 | 509 |
nFollowBytes := 5. |
21379 | 510 |
minValue := 16r4000000. |
19837 | 511 |
]]]]]. |
21379 | 512 |
|
19837 | 513 |
nFollowBytes timesRepeat:[ |
514 |
byte := aStringOrByteCollection byteAt:srcIdx. |
|
21379 | 515 |
(byte bitAnd:2r11000000) ~~ 2r10000000 ifTrue:[ |
516 |
^ DecodingError newException |
|
517 |
defaultValue:aStringOrByteCollection; |
|
518 |
raiseRequestWith:aStringOrByteCollection |
|
519 |
errorString:('illegal followbyte (expect 0x80): 0x%1 at:%2' bindWith:(byte hexPrintString:2) with:srcIdx). |
|
520 |
]. |
|
19837 | 521 |
srcIdx := srcIdx + 1. |
21379 | 522 |
codepoint := (codepoint bitShift:6) bitOr:(byte bitAnd:2r00111111). |
523 |
]. |
|
524 |
||
525 |
codepoint < minValue ifTrue:[ |
|
526 |
"encoding a value in a longer utf8-sequence than required is not allowed |
|
527 |
and imposes a security risk. Proceed to continue with decoding." |
|
528 |
DecodingError newException |
|
529 |
defaultValue:aStringOrByteCollection; |
|
530 |
raiseRequestWith:aStringOrByteCollection errorString:'overlong utf8 sequence'. |
|
19837 | 531 |
]. |
532 |
]. |
|
21379 | 533 |
newString at:idx put:(Character value:codepoint). |
19837 | 534 |
idx := idx + 1. |
535 |
]. |
|
536 |
^ newString |
|
537 |
||
538 |
" |
|
539 |
CharacterArray decodeFromUTF8:#[ 16r41 16r42 ] |
|
540 |
CharacterArray decodeFromUTF8:#[ 16rC6 16r8F ] |
|
541 |
CharacterArray decodeFromUTF8:#[ 16rEF 16rBF 16rBF ] |
|
542 |
||
21379 | 543 |
rfc3629 examples: |
19837 | 544 |
CharacterArray decodeFromUTF8:#[ 16r41 16rE2 16r89 16rA2 16rCE 16r91 16r2E ] |
545 |
CharacterArray decodeFromUTF8:#[ 16rED 16r95 16r9C 16rEA 16rB5 16rAD 16rEC 16r96 16rB4 ] |
|
546 |
CharacterArray decodeFromUTF8:#[ 16rE6 16r97 16rA5 16rE6 16r9C 16rAC 16rE8 16rAA 16r9E ] |
|
547 |
||
548 |
invalid: |
|
549 |
CharacterArray decodeFromUTF8:#[ 16rC0 16r80 ] |
|
550 |
CharacterArray decodeFromUTF8:#[ 16rE0 16r80 16r80 ] |
|
551 |
CharacterArray decodeFromUTF8:#[ 16rE0 16r81 16r02 ] |
|
21379 | 552 |
" |
553 |
||
554 |
"Modified: / 07-02-2017 / 17:41:17 / stefan" |
|
23400 | 555 |
"Modified: / 04-10-2018 / 13:29:53 / Claus Gittinger" |
20010 | 556 |
! |
557 |
||
20011 | 558 |
setupNormalizationMaps |
20864 | 559 |
"returns a 2-stage map from ch2 -> ch1 -> mappedChar. |
560 |
for unicode normalization |
|
561 |
(i.e. for replacing combining char-sequences with regular characters). |
|
562 |
ch2 is the combining charCode (eg. 0x0308), ch1 is the previous character (eg. $A), |
|
563 |
mappedChar is the result (eg. $Ä). |
|
564 |
Caveat: |
|
565 |
possibly incomplete: only COMBINING_DIACRITICAL_MARKS are cared for. |
|
566 |
Does not care for COMBINING_DIACRITICAL_MARKS_EXTENDED |
|
567 |
and COMBINING_DIACRITICAL_MARKS_SUPPLEMENT. |
|
568 |
However; those are used for German dialectology, ancient Greek and other similar |
|
569 |
exotic uses. Probably noone will ever even notice that they are missing..." |
|
20011 | 570 |
|
571 |
|def| |
|
572 |
||
573 |
UnicodeNormalizationMap := Dictionary new. |
|
20013 | 574 |
UnicodeDenormalizationMap := Dictionary new. |
575 |
||
576 |
def := [:combiner :chars :mappedChars | |
|
20011 | 577 |
|d| |
578 |
||
20013 | 579 |
d := UnicodeNormalizationMap at:combiner ifAbsentPut:[Dictionary new]. |
580 |
chars with:mappedChars do:[:ch1 :mappedChar | |
|
581 |
d at:ch1 put:mappedChar. |
|
582 |
UnicodeDenormalizationMap at:mappedChar put:(Unicode16String with:ch1 with:combiner). |
|
583 |
]. |
|
20011 | 584 |
]. |
20014 | 585 |
def value:(Character codePoint:16r0300) "grave" value:'AEIOUaeiouWw' value:'ÀÈÌÒÙàèìòùẀẁ'. |
586 |
def value:(Character codePoint:16r0301) "acute" value:'AEIOUYaeiouyÇçKkMmPpWw' value:'ÁÉÍÓÚÝáéíóúýḈḉḰḱḾḿṔṕẂẃ'. |
|
587 |
def value:(Character codePoint:16r0302) "circumflex" value:'AEIOUaeiouZz' value:'ÂÊÎÔÛâêîôûẐẑ'. |
|
20011 | 588 |
def value:(Character codePoint:16r0303) "tilde" value:'AONaon' value:'ÃÕÑãõñ'. |
20014 | 589 |
def value:(Character codePoint:16r0307) "dot over" value:'BbDdFfHhMmNnPpRrSsTtWwXxYyṢṣ' value:'ḂḃḊḋḞḟḢḣṀṁṄṅṖṗṘṙṠṡṪṫẆẇẊẋẎẏṨṩ'. |
590 |
def value:(Character codePoint:16r0308) "diaresis" value:'AEIOUaeiouyHhWwXxt' value:'ÄËÏÖÜäëïöüÿḦḧẄẅẌẍẗ'. |
|
591 |
def value:(Character codePoint:16r030A) "ring" value:'Aawy' value:'Ååẘẙ'. |
|
592 |
def value:(Character codePoint:16r0323) "dot under" value:'AaBbDdEeIiHhKkLlMmNnOoRrSsTtUuVvWwZzṠṡ' value:'ẠạḄḅḌḍẸẹỊịḤḥḲḳḶḷṂṃṆṇỌọṚṛṢṣṬṭỤụṾṿẈẉẒẓṨṩ'. |
|
20011 | 593 |
def value:(Character codePoint:16r0327) "cedilla" value:'Cc' value:'Çç'. |
594 |
||
595 |
" |
|
20014 | 596 |
self setupNormalizationMaps |
20011 | 597 |
" |
598 |
! |
|
599 |
||
600 |
unicodeDenormalizationMap |
|
601 |
"returns a 2-stage map from ch2 -> ch1 -> mappedChar |
|
20864 | 602 |
for unicode normalization (i.e. for making combining chars regular ones). |
603 |
Caveat: |
|
604 |
possibly incomplete: only COMBINING_DIACRITICAL_MARKS are cared for. |
|
605 |
Does not care for COMBINING_DIACRITICAL_MARKS_EXTENDED |
|
606 |
and COMBINING_DIACRITICAL_MARKS_SUPPLEMENT. |
|
607 |
However; those are used for German dialectology, ancient Greek and other similar |
|
608 |
exotic uses. Probably noone will ever even notice that they are missing..." |
|
20011 | 609 |
|
610 |
UnicodeDenormalizationMap isNil ifTrue:[ |
|
611 |
self setupNormalizationMaps |
|
612 |
]. |
|
613 |
^ UnicodeDenormalizationMap |
|
614 |
||
615 |
" |
|
616 |
self unicodeDenormalizationMap |
|
617 |
" |
|
618 |
! |
|
619 |
||
20010 | 620 |
unicodeNormalizationMap |
621 |
"returns a 2-stage map from ch2 -> ch1 -> mappedChar |
|
20864 | 622 |
for unicode normalization (i.e. for making combining chars regular ones). |
623 |
Caveat: |
|
624 |
possibly incomplete: only COMBINING_DIACRITICAL_MARKS are cared for. |
|
625 |
Does not care for COMBINING_DIACRITICAL_MARKS_EXTENDED |
|
626 |
and COMBINING_DIACRITICAL_MARKS_SUPPLEMENT. |
|
627 |
However; those are used for German dialectology, ancient Greek and other similar |
|
628 |
exotic uses. Probably noone will ever even notice that they are missing..." |
|
20010 | 629 |
|
630 |
UnicodeNormalizationMap isNil ifTrue:[ |
|
20011 | 631 |
self setupNormalizationMaps |
20010 | 632 |
]. |
633 |
^ UnicodeNormalizationMap |
|
634 |
||
635 |
" |
|
636 |
self unicodeNormalizationMap |
|
637 |
" |
|
443 | 638 |
! ! |
639 |
||
24250 | 640 |
|
1849
9f35f5934ec6
added msdos-latin1 en/decoder (code = #msdos850)
Claus Gittinger <cg@exept.de>
parents:
1669
diff
changeset
|
641 |
!CharacterArray class methodsFor:'pattern matching'! |
64 | 642 |
|
5342
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
643 |
matchEscapeCharacter |
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
644 |
"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
|
645 |
(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
|
646 |
|
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
647 |
^ $\ |
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
648 |
! |
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
649 |
|
15734 | 650 |
matchScan:matchScanArray from:matchStart to:matchStop with:aString from:start to:stop caseSensitive:caseSensitive |
327 | 651 |
"helper for match; return true if the characters from start to stop in |
652 |
aString are matching the scan in matchScan from matchStart to matchStop. |
|
328 | 653 |
The matchScan is as created by asMatchScanArray. |
327 | 654 |
|
8874 | 655 |
This algorithm is not at all the most efficient; |
656 |
for heavy duty pattern matching, an interface (primitive) to the regex |
|
328 | 657 |
pattern matching package should be added." |
327 | 658 |
|
8874 | 659 |
|matchEntry |
327 | 660 |
mStart "{ Class: SmallInteger }" |
661 |
mStop "{ Class: SmallInteger }" |
|
662 |
sStart "{ Class: SmallInteger }" |
|
663 |
sStop "{ Class: SmallInteger }" |
|
664 |
mSize "{ Class: SmallInteger }" |
|
665 |
sSize "{ Class: SmallInteger }" |
|
666 |
index "{ Class: SmallInteger }" |
|
2561 | 667 |
quickCheck matchLast nextMatchEntry |
986 | 668 |
checkChar included| |
327 | 669 |
|
670 |
mStart := matchStart. |
|
671 |
mStop := matchStop. |
|
672 |
sStart := start. |
|
673 |
sStop := stop. |
|
674 |
||
16245 | 675 |
[ |
8874 | 676 |
"/ Transcript showCR:('match: ''' , (aString copyFrom:sStart to:sStop) , |
3537 | 677 |
"/ ''' against:' , (matchScanArray copyFrom:mStart to:mStop) printString). |
678 |
||
20874 | 679 |
mSize := mStop - mStart + 1. |
680 |
sSize := sStop - sStart + 1. |
|
681 |
||
682 |
"empty strings match" |
|
683 |
(mSize == 0) ifTrue:[^ (sSize == 0)]. |
|
684 |
||
685 |
matchEntry := matchScanArray at:mStart. |
|
686 |
||
687 |
"/ the most common case first: |
|
688 |
(sSize ~~ 0 |
|
689 |
and:[(checkChar := (aString at:sStart)) = matchEntry]) ifTrue:[ |
|
690 |
"advance by one and continue" |
|
691 |
mStart := mStart + 1. |
|
692 |
sStart := sStart + 1 |
|
693 |
] ifFalse:[ |
|
694 |
(matchEntry == #any) ifTrue:[ |
|
695 |
"restString empty -> no match" |
|
696 |
(sSize == 0) ifTrue:[^ false]. |
|
697 |
"# matches single character" |
|
698 |
((sSize == 1) and:[mSize == 1]) ifTrue:[^ true]. |
|
699 |
"advance by one and continue" |
|
700 |
mStart := mStart + 1. |
|
701 |
sStart := sStart + 1 |
|
702 |
] ifFalse:[ |
|
703 |
(matchEntry == #anyString) ifTrue:[ |
|
704 |
"* alone matches anything" |
|
705 |
(mSize == 1) ifTrue:[^ true]. |
|
706 |
"restString empty & matchString not empty -> no match" |
|
707 |
(sSize == 0) ifTrue:[^ false]. |
|
708 |
||
709 |
" |
|
710 |
try to avoid some of the recursion by checking last |
|
711 |
character and continue with shortened strings if possible |
|
712 |
" |
|
713 |
quickCheck := false. |
|
714 |
(mStop >= mStart) ifTrue:[ |
|
715 |
matchLast := matchScanArray at:mStop. |
|
716 |
(matchLast ~~ #anyString) ifTrue:[ |
|
717 |
(matchLast == #any) ifTrue:[ |
|
718 |
quickCheck := true |
|
719 |
] ifFalse:[ |
|
720 |
matchLast == (aString at:sStop) ifTrue:[ |
|
721 |
quickCheck := true |
|
722 |
] ifFalse:[ |
|
723 |
matchLast isString ifTrue:[ |
|
724 |
quickCheck := matchLast includes:(aString at:sStop) |
|
725 |
] |
|
726 |
] |
|
727 |
] |
|
728 |
] |
|
729 |
]. |
|
730 |
quickCheck ifTrue:[ |
|
731 |
" |
|
732 |
quickCheck ok, advance from the right |
|
733 |
" |
|
734 |
mStop := mStop - 1. |
|
735 |
sStop := sStop - 1 |
|
736 |
] ifFalse:[ |
|
737 |
"/ no quick check; |
|
738 |
"/ look for the next character(s) |
|
739 |
"/ and try matching there |
|
740 |
"/ (to avoid recursion) |
|
741 |
||
742 |
mStart < mStop ifTrue:[ |
|
743 |
nextMatchEntry := matchScanArray at:mStart+1. |
|
744 |
nextMatchEntry isCharacter ifTrue:[ |
|
745 |
sStart <= sStop ifTrue:[ |
|
746 |
[ |
|
747 |
caseSensitive ifTrue:[ |
|
748 |
index := aString indexOf:nextMatchEntry startingAt:sStart |
|
749 |
] ifFalse:[ |
|
750 |
index := aString findFirst:[:c | c asLowercase = nextMatchEntry asLowercase] |
|
751 |
startingAt:sStart. |
|
752 |
]. |
|
753 |
(index == 0 or:[index > sStop]) ifTrue:[ |
|
754 |
^ false |
|
755 |
]. |
|
756 |
(self matchScan:matchScanArray |
|
757 |
from:(mStart + 1) |
|
758 |
to:mStop |
|
759 |
with:aString |
|
760 |
from:index |
|
761 |
to:sStop |
|
762 |
caseSensitive:caseSensitive |
|
763 |
) ifTrue:[ |
|
764 |
^ true |
|
765 |
]. |
|
766 |
sStart := index + 1. |
|
767 |
] loop. |
|
768 |
] |
|
769 |
] |
|
770 |
]. |
|
771 |
||
772 |
" |
|
773 |
no quick check possible; |
|
774 |
loop over all possible substrings |
|
775 |
" |
|
776 |
index := sStart. |
|
777 |
[index <= sStop] whileTrue:[ |
|
778 |
(self matchScan:matchScanArray |
|
779 |
from:(mStart + 1) |
|
780 |
to:mStop |
|
781 |
with:aString |
|
782 |
from:index |
|
783 |
to:sStop |
|
784 |
caseSensitive:caseSensitive |
|
785 |
) ifTrue:[ |
|
786 |
^ true |
|
787 |
]. |
|
788 |
index := index + 1 |
|
789 |
]. |
|
790 |
^ false |
|
791 |
]. |
|
792 |
] ifFalse:[ |
|
793 |
(matchEntry isString) ifTrue:[ |
|
794 |
"testString empty -> no match" |
|
795 |
(sSize == 0) ifTrue:[^ false]. |
|
796 |
||
797 |
included := false. |
|
798 |
"/ checkChar := aString at:sStart. |
|
799 |
included := matchEntry includes:checkChar. |
|
800 |
included ifFalse:[ |
|
801 |
caseSensitive ifFalse:[ |
|
802 |
checkChar isUppercase ifTrue:[ |
|
803 |
included := matchEntry includes:checkChar asLowercase. |
|
804 |
] ifFalse:[ |
|
805 |
included := matchEntry includes:checkChar asUppercase. |
|
806 |
] |
|
807 |
]. |
|
808 |
]. |
|
809 |
mStart := mStart + 1. |
|
810 |
mSize := mSize - 1. |
|
811 |
included ifFalse:[^ false]. |
|
812 |
||
813 |
((sSize == 1) and:[mSize == 0]) ifTrue:[^ true]. |
|
814 |
] ifFalse:[ |
|
815 |
"/ must be single character |
|
816 |
||
817 |
"testString empty ?" |
|
818 |
(sSize == 0) ifTrue:[^ false]. |
|
819 |
||
820 |
"first characters equal ?" |
|
821 |
"/ checkChar := aString at:sStart. |
|
822 |
caseSensitive ifTrue:[^ false]. |
|
823 |
(checkChar asUppercase ~= matchEntry asUppercase) ifTrue:[^ false]. |
|
824 |
||
825 |
"advance and continue" |
|
826 |
mStart := mStart + 1. |
|
827 |
]. |
|
828 |
"cut off 1st char and continue" |
|
829 |
sStart := sStart + 1 |
|
830 |
] |
|
831 |
] |
|
832 |
] |
|
16245 | 833 |
] loop. |
2519
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
834 |
|
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
835 |
" |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
836 |
|scanArray s| |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
837 |
|
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
838 |
scanArray := self matchScanArrayFrom:'*hello'. |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
839 |
s := 'foo bar hello world'. |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
840 |
CharacterArray |
20874 | 841 |
matchScan:scanArray |
842 |
from:1 |
|
843 |
to:scanArray size |
|
844 |
with:s |
|
845 |
from:1 |
|
846 |
to:s size |
|
847 |
caseSensitive:true |
|
2519
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
848 |
" |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
849 |
" |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
850 |
|scanArray s| |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
851 |
|
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
852 |
scanArray := self matchScanArrayFrom:'*hello*'. |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
853 |
s := 'foo bar hello world'. |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
854 |
CharacterArray |
20874 | 855 |
matchScan:scanArray |
856 |
from:1 |
|
857 |
to:scanArray size |
|
858 |
with:s |
|
859 |
from:1 |
|
860 |
to:s size |
|
861 |
caseSensitive:true |
|
15734 | 862 |
" |
863 |
||
864 |
"Modified: / 24-07-2011 / 07:17:03 / cg" |
|
865 |
"Modified (comment): / 24-07-2011 / 08:55:14 / cg" |
|
866 |
! |
|
867 |
||
868 |
matchScan:matchScanArray from:matchStart to:matchStop with:aString from:start to:stop ignoreCase:ignoreCase |
|
869 |
<resource: #obsolete> |
|
870 |
"helper for match; return true if the characters from start to stop in |
|
871 |
aString are matching the scan in matchScan from matchStart to matchStop. |
|
872 |
The matchScan is as created by asMatchScanArray. |
|
873 |
||
874 |
This algorithm is not at all the most efficient; |
|
875 |
for heavy duty pattern matching, an interface (primitive) to the regex |
|
876 |
pattern matching package should be added." |
|
877 |
||
878 |
^ self |
|
20874 | 879 |
matchScan:matchScanArray from:matchStart to:matchStop with:aString from:start to:stop caseSensitive:ignoreCase not |
15734 | 880 |
|
881 |
" |
|
882 |
|scanArray s| |
|
883 |
||
884 |
scanArray := self matchScanArrayFrom:'*hello'. |
|
885 |
s := 'foo bar hello world'. |
|
886 |
CharacterArray |
|
20874 | 887 |
matchScan:scanArray |
888 |
from:1 |
|
889 |
to:scanArray size |
|
890 |
with:s |
|
891 |
from:1 |
|
892 |
to:s size |
|
893 |
ignoreCase:false |
|
15734 | 894 |
" |
895 |
" |
|
896 |
|scanArray s| |
|
897 |
||
898 |
scanArray := self matchScanArrayFrom:'*hello*'. |
|
899 |
s := 'foo bar hello world'. |
|
900 |
CharacterArray |
|
20874 | 901 |
matchScan:scanArray |
902 |
from:1 |
|
903 |
to:scanArray size |
|
904 |
with:s |
|
905 |
from:1 |
|
906 |
to:s size |
|
907 |
ignoreCase:false |
|
13520 | 908 |
" |
909 |
||
910 |
"Modified: / 24-07-2011 / 07:17:03 / cg" |
|
911 |
"Modified (comment): / 24-07-2011 / 08:55:14 / cg" |
|
608 | 912 |
! |
913 |
||
914 |
matchScanArrayFrom:aString |
|
915 |
"scan a pattern string and decompose it into a scanArray. |
|
916 |
This is processed faster (especially with character ranges), and |
|
917 |
can also be reused later. (if the same pattern is to be searched again)" |
|
918 |
||
9282
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
919 |
^ self matchScanArrayFrom:aString escapeCharacter:(self matchEscapeCharacter) |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
920 |
|
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
921 |
" |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
922 |
String matchScanArrayFrom:'*ute*' |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
923 |
String matchScanArrayFrom:'**ute**' |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
924 |
String matchScanArrayFrom:'*uter' |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
925 |
String matchScanArrayFrom:'\*uter' |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
926 |
String matchScanArrayFrom:'[cC]#mpute[rR]' |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
927 |
String matchScanArrayFrom:'[abcd]*' |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
928 |
String matchScanArrayFrom:'[a-k]*' |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
929 |
String matchScanArrayFrom:'*some*compl*ern*' |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
930 |
String matchScanArrayFrom:'[a-' |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
931 |
String matchScanArrayFrom:'[a-zA-Z]' |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
932 |
String matchScanArrayFrom:'[a-z01234A-Z]' |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
933 |
" |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
934 |
|
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
935 |
"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
|
936 |
! |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
937 |
|
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
938 |
matchScanArrayFrom:aString escapeCharacter:escape |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
939 |
"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
|
940 |
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
|
941 |
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
|
942 |
|
8874 | 943 |
|coll |
608 | 944 |
idx "{ Class: SmallInteger }" |
9282
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
945 |
end c1 c2 matchSet previous| |
5342
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
946 |
|
5487 | 947 |
previous := nil. |
608 | 948 |
|
949 |
coll := OrderedCollection new. |
|
950 |
idx := 1. end := aString size. |
|
951 |
[idx <= end] whileTrue:[ |
|
20874 | 952 |
|char this| |
953 |
||
954 |
char := aString at:idx. |
|
955 |
char == $* ifTrue:[ |
|
956 |
previous ~~ #anyString ifTrue:[ |
|
957 |
this := #anyString |
|
958 |
] |
|
959 |
] ifFalse:[ |
|
960 |
char == $# ifTrue:[ |
|
961 |
previous ~~ #anyString ifTrue:[ |
|
962 |
this := #any |
|
963 |
] |
|
964 |
] ifFalse:[ |
|
965 |
char == $[ ifTrue:[ |
|
966 |
matchSet := IdentitySet new. |
|
967 |
idx := idx + 1. |
|
968 |
idx > end ifTrue:[^ nil]. |
|
969 |
char := aString at:idx. |
|
970 |
c1 := nil. |
|
971 |
[char ~~ $]] whileTrue:[ |
|
972 |
((char == $-) and:[c1 notNil]) ifTrue:[ |
|
973 |
idx := idx + 1. |
|
974 |
idx > end ifTrue:[^ nil]. |
|
975 |
c2 := aString at:idx. |
|
976 |
c1 to:c2 do:[:c | matchSet add:c]. |
|
977 |
c1 := nil. |
|
978 |
idx := idx + 1. |
|
979 |
] ifFalse:[ |
|
980 |
(char ~~ $]) ifTrue:[ |
|
981 |
matchSet add:char. |
|
982 |
c1 := char. |
|
983 |
idx := idx + 1 |
|
984 |
] |
|
985 |
]. |
|
986 |
idx > end ifTrue:[^ nil]. |
|
987 |
char := aString at:idx |
|
988 |
]. |
|
23552 | 989 |
this := matchSet asString sort. |
20874 | 990 |
] ifFalse:[ |
991 |
char == escape ifTrue:[ |
|
992 |
idx := idx + 1. |
|
993 |
idx > end ifTrue:[ |
|
994 |
"/ mhmh - what should we do here ? |
|
995 |
this := char |
|
996 |
] ifFalse:[ |
|
997 |
this := aString at:idx. |
|
998 |
] |
|
999 |
] ifFalse:[ |
|
1000 |
this := char |
|
1001 |
] |
|
1002 |
] |
|
1003 |
] |
|
1004 |
]. |
|
1005 |
this notNil ifTrue:[coll add:this. previous := this]. |
|
1006 |
idx := idx + 1 |
|
608 | 1007 |
]. |
1008 |
||
1009 |
^ coll asArray |
|
1010 |
||
1011 |
" |
|
8874 | 1012 |
String matchScanArrayFrom:'*ute*' |
1013 |
String matchScanArrayFrom:'**ute**' |
|
1014 |
String matchScanArrayFrom:'*uter' |
|
1015 |
String matchScanArrayFrom:'\*uter' |
|
1016 |
String matchScanArrayFrom:'[cC]#mpute[rR]' |
|
1017 |
String matchScanArrayFrom:'[abcd]*' |
|
23552 | 1018 |
String matchScanArrayFrom:'[abcdŴĂĂ]*' |
8874 | 1019 |
String matchScanArrayFrom:'[a-k]*' |
1020 |
String matchScanArrayFrom:'*some*compl*ern*' |
|
1021 |
String matchScanArrayFrom:'[a-' |
|
1022 |
String matchScanArrayFrom:'[a-zA-Z]' |
|
1023 |
String matchScanArrayFrom:'[a-z01234A-Z]' |
|
608 | 1024 |
" |
2519
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
1025 |
|
23552 | 1026 |
"Modified: / 02-04-1997 / 16:20:29 / cg" |
1027 |
"Modified: / 03-12-2018 / 15:41:54 / Stefan Vogel" |
|
9192 | 1028 |
! ! |
1029 |
||
1030 |
!CharacterArray class methodsFor:'queries'! |
|
1031 |
||
1032 |
isAbstract |
|
11229 | 1033 |
"Return if this class is an abstract class. |
1034 |
True is returned for CharacterArray here; false for subclasses. |
|
19464 | 1035 |
Abstract subclasses must redefine this again." |
11229 | 1036 |
|
9192 | 1037 |
^ self == CharacterArray |
18597 | 1038 |
! |
1039 |
||
1040 |
speciesForCharacterSize:characterSize |
|
1041 |
"answer the class, that is able to hold characters of size characterSize" |
|
1042 |
||
1043 |
characterSize <= 8 ifTrue:[^ String]. |
|
1044 |
characterSize <= 16 ifTrue:[^ Unicode16String]. |
|
1045 |
^ Unicode32String |
|
9192 | 1046 |
! ! |
1047 |
||
24250 | 1048 |
|
22525 | 1049 |
!CharacterArray class methodsFor:'utilities'! |
1050 |
||
23983 | 1051 |
through:aCharacter in:inStream |
1052 |
"read all characters through aCharacter and return a |
|
1053 |
dense strings for it (i.e. String/TwoByteString, if possible). |
|
1054 |
This helper is present because when reading from a unicode32 stream, |
|
1055 |
dense strings are preferred. |
|
1056 |
If inStream is known to return single byte characters, |
|
1057 |
you should better use inStream through:aCharacter" |
|
1058 |
||
1059 |
|answerStream char| |
|
1060 |
||
1061 |
answerStream := CharacterWriteStream new:20. |
|
1062 |
[inStream atEnd] whileFalse:[ |
|
1063 |
char := inStream next. |
|
1064 |
answerStream nextPut:char. |
|
1065 |
(aCharacter == char) ifTrue:[ |
|
1066 |
^ answerStream contents |
|
1067 |
]. |
|
1068 |
]. |
|
1069 |
^ answerStream contents |
|
1070 |
||
1071 |
"Created: / 27-03-2019 / 13:42:00 / Claus Gittinger" |
|
1072 |
! |
|
1073 |
||
1074 |
throughAny:aCollection in:inStream |
|
1075 |
"read all characters through any in aCollection and return a |
|
1076 |
dense strings for it (i.e. String/TwoByteString, if possible). |
|
1077 |
This helper is present because when reading from a unicode32 stream, |
|
1078 |
dense strings are preferred. |
|
1079 |
If inStream is known to return single byte characters, |
|
1080 |
you should better use inStream throughAny:aCollection" |
|
1081 |
||
1082 |
|answerStream char| |
|
1083 |
||
1084 |
answerStream := CharacterWriteStream new:20. |
|
1085 |
[inStream atEnd] whileFalse:[ |
|
1086 |
char := inStream next. |
|
1087 |
answerStream nextPut:char. |
|
1088 |
(aCollection includes:char) ifTrue:[ |
|
1089 |
^ answerStream contents |
|
1090 |
]. |
|
1091 |
]. |
|
1092 |
^ answerStream contents |
|
1093 |
||
1094 |
"Created: / 27-03-2019 / 13:40:49 / Claus Gittinger" |
|
1095 |
! |
|
1096 |
||
22525 | 1097 |
withoutAmpersandEscapes:label |
1098 |
"remove single ampersands; |
|
1099 |
replace double ampersands by single ones. |
|
1100 |
This is used to unescape menu-labels |
|
1101 |
(which use the ampersand as shortKey marker)" |
|
1102 |
||
1103 |
^ String |
|
1104 |
streamContents:[:out | |
|
1105 |
|in ch next| |
|
1106 |
||
1107 |
in := label readStream. |
|
1108 |
[in atEnd] whileFalse:[ |
|
1109 |
ch := in next. |
|
1110 |
ch == $& ifTrue:[ |
|
1111 |
(next := in peek) == $& ifTrue:[ |
|
1112 |
in next. |
|
1113 |
out nextPut:ch |
|
1114 |
] ifFalse:[ |
|
1115 |
ch := next |
|
1116 |
]. |
|
1117 |
] ifFalse:[ |
|
1118 |
out nextPut:ch |
|
1119 |
]. |
|
1120 |
] |
|
1121 |
] |
|
1122 |
||
1123 |
" |
|
1124 |
String withoutAmpersandEscapes:'' |
|
1125 |
String withoutAmpersandEscapes:'a' |
|
1126 |
String withoutAmpersandEscapes:'abcd' |
|
1127 |
String withoutAmpersandEscapes:'&abcd' |
|
1128 |
String withoutAmpersandEscapes:'&abcd&' |
|
1129 |
String withoutAmpersandEscapes:'&a&b&c&d' |
|
1130 |
String withoutAmpersandEscapes:'&a&b&c&d&' |
|
1131 |
String withoutAmpersandEscapes:'&&a&&b&&c&&d&&' |
|
1132 |
String withoutAmpersandEscapes:'&&a&&b&&c&&d&' |
|
1133 |
String withoutAmpersandEscapes:'&a&&b&&c&&d&' |
|
1134 |
String withoutAmpersandEscapes:'a&&b&&c&&d' |
|
1135 |
" |
|
1136 |
! ! |
|
1137 |
||
24407 | 1138 |
|
9192 | 1139 |
!CharacterArray methodsFor:'Compatibility-ANSI'! |
1140 |
||
1141 |
addLineDelimiters |
|
1142 |
"Ansi compatibility - same as withCRs" |
|
1143 |
||
1144 |
^ self withCRs |
|
1145 |
||
1146 |
"Modified: / 13.11.2001 / 19:16:25 / cg" |
|
17543 | 1147 |
! ! |
1148 |
||
1149 |
!CharacterArray methodsFor:'Compatibility-Dolphin'! |
|
9192 | 1150 |
|
1151 |
copyExpanding:expandTable |
|
1152 |
"return a copy of myself, with translations from the expandTable sliced in. |
|
18392 | 1153 |
The argument is supposed to map from characters to either characters or strings. |
1154 |
Added for Dolphin compatibility" |
|
9192 | 1155 |
|
1156 |
|ds| |
|
1157 |
||
1158 |
ds := WriteStream on:(self species new). |
|
1159 |
self do:[:eachChar | |
|
20874 | 1160 |
|repl| |
1161 |
||
1162 |
repl := expandTable at:eachChar ifAbsent:[nil]. |
|
1163 |
repl isNil ifTrue:[ |
|
1164 |
ds nextPut:eachChar |
|
1165 |
] ifFalse:[ |
|
1166 |
repl size == 0 ifTrue:[ |
|
1167 |
ds nextPut:repl |
|
1168 |
] ifFalse:[ |
|
1169 |
ds nextPutAll:repl |
|
1170 |
] |
|
1171 |
]. |
|
9192 | 1172 |
]. |
6870 | 1173 |
^ ds contents. |
24036 | 1174 |
|
1175 |
" |
|
1176 |
'hello' copyExpanding:(Dictionary withKeys:{$h . $e . $o} andValues:{'HH' . 'EE' . $O }) |
|
1177 |
" |
|
1178 |
||
1179 |
"Modified (format): / 02-04-2019 / 10:59:59 / Claus Gittinger" |
|
6870 | 1180 |
! |
1181 |
||
1182 |
formatWith:aString |
|
11955 | 1183 |
"Compatibility method - do not use in new code. |
18392 | 1184 |
same as #bindWith: for Dolphin compatibility" |
6870 | 1185 |
|
1186 |
^ self bindWith:aString |
|
1187 |
||
1188 |
" |
|
1189 |
'hello%1world' formatWith:'123' |
|
1190 |
" |
|
1191 |
! |
|
1192 |
||
1193 |
formatWith:arg1 with:arg2 |
|
11955 | 1194 |
"Compatibility method - do not use in new code. |
18392 | 1195 |
same as #bindWith: for Dolphin compatibility" |
6870 | 1196 |
|
1197 |
^ self bindWith:arg1 with:arg2 |
|
1198 |
||
1199 |
" |
|
1200 |
'hello%1 %2world' formatWith:'123' with:234 |
|
1201 |
" |
|
1202 |
! |
|
1203 |
||
1204 |
formatWith:arg1 with:arg2 with:arg3 |
|
11955 | 1205 |
"Compatibility method - do not use in new code. |
18392 | 1206 |
same as #bindWith: for Dolphin compatibility" |
6870 | 1207 |
|
1208 |
^ self bindWith:arg1 with:arg2 with:arg3 |
|
1209 |
||
1210 |
" |
|
1211 |
'hello%1 %2 %3world' formatWith:'123' with:234 with:345 |
|
1212 |
" |
|
6391 | 1213 |
! ! |
1214 |
||
14681 | 1215 |
!CharacterArray methodsFor:'Compatibility-GNU'! |
1216 |
||
1217 |
% anArrayOfOperands |
|
1218 |
"return a copy of the receiver, where a '%i' escape |
|
21747 | 1219 |
is replaced by the corresponding string from the argument array. |
14681 | 1220 |
'i' may be between 1 and 9 (i.e. a maximum of 9 placeholders is allowed). |
1221 |
Added for GNU-ST compatibility." |
|
1222 |
||
1223 |
^ self expandPlaceholdersWith:anArrayOfOperands |
|
1224 |
||
1225 |
" |
|
1226 |
'do you prefer %1 or rather %2 (not talking about %3) ?' |
|
20874 | 1227 |
% #('smalltalk' 'c++' 'c') |
14681 | 1228 |
|
1229 |
'do you %(what) ?' |
|
20874 | 1230 |
% (Dictionary new at:#'what' put:'understand'; yourself) |
14681 | 1231 |
" |
21747 | 1232 |
|
1233 |
"Modified (comment): / 11-05-2017 / 12:43:08 / mawalch" |
|
14681 | 1234 |
! ! |
1235 |
||
7261 | 1236 |
!CharacterArray methodsFor:'Compatibility-ST/V'! |
608 | 1237 |
|
17517 | 1238 |
asArrayOfSubstrings |
1239 |
"return an array of substrings from the receiver, interpreting |
|
1240 |
separators (i.e. spaces & newlines) as word-delimiters. |
|
18390 | 1241 |
This has been added for ST/V compatibility |
1242 |
- the actual work is done in asCollectionOfWords. |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
1243 |
(sigh: it is called #'subStrings' in V'Age, #'substrings' in Squeak |
17517 | 1244 |
and #'asCollectionOfWords' in ST/X) " |
1245 |
||
1246 |
^ self asCollectionOfWords asArray |
|
1247 |
||
1248 |
" |
|
1249 |
'1 one two three four 5 five' asArrayOfSubstrings |
|
1250 |
'1 |
|
1251 |
one |
|
20874 | 1252 |
two three four 5 five' asArrayOfSubstrings |
17517 | 1253 |
" |
1254 |
! |
|
1255 |
||
19497 | 1256 |
equalsIgnoreCase:aString |
1257 |
"This is an ST/V compatibility method and an alias for sameAs:." |
|
1258 |
||
1259 |
^ self sameAs:aString |
|
1260 |
||
1261 |
" |
|
1262 |
'abc' equalsIgnoreCase: 'aBC' |
|
1263 |
" |
|
1264 |
! |
|
1265 |
||
608 | 1266 |
replChar:oldChar with:newChar |
19497 | 1267 |
"return a copy of the receiver, with all oldChars replaced by newChar. |
1268 |
This is an ST/V compatibility method and an alias for copyReplaceAll." |
|
608 | 1269 |
|
3656 | 1270 |
^ self copyReplaceAll:oldChar with:newChar |
608 | 1271 |
|
1272 |
" |
|
8874 | 1273 |
'12345678901234567890' replChar:$0 with:$* |
608 | 1274 |
" |
3656 | 1275 |
|
1276 |
"Modified: / 18.7.1998 / 22:52:57 / cg" |
|
608 | 1277 |
! |
1278 |
||
1279 |
replChar:oldChar withString:newString |
|
1280 |
"return a copy of the receiver, with all oldChars replaced |
|
1281 |
by newString (i.e. slice in the newString in place of the oldChar). |
|
1282 |
This is an ST/V compatibility method." |
|
1283 |
||
1284 |
|tmpStream| |
|
1285 |
||
15523 | 1286 |
tmpStream := self species writeStream. |
608 | 1287 |
self do:[:element | |
20874 | 1288 |
element = oldChar ifTrue:[ |
1289 |
tmpStream nextPutAll:newString |
|
1290 |
] ifFalse:[ |
|
1291 |
tmpStream nextPut:element |
|
1292 |
]. |
|
608 | 1293 |
]. |
1294 |
^ tmpStream contents |
|
1295 |
||
1296 |
" |
|
8874 | 1297 |
'12345678901234567890' replChar:$0 withString:'foo' |
1298 |
'a string with spaces' replChar:$ withString:' foo ' |
|
608 | 1299 |
" |
9192 | 1300 |
! |
1301 |
||
1302 |
replString:subString withString:newString |
|
4244
5deefa3144fb
added #replString:withString:
Claus Gittinger <cg@exept.de>
parents:
4156
diff
changeset
|
1303 |
"return a copy of the receiver, with all sequences of subString replaced |
18390 | 1304 |
by newString (i.e. slice in the newString in place of the oldString). |
19497 | 1305 |
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
|
1306 |
|
8347 | 1307 |
^ self copyReplaceString:subString withString:newString |
4244
5deefa3144fb
added #replString:withString:
Claus Gittinger <cg@exept.de>
parents:
4156
diff
changeset
|
1308 |
|
5deefa3144fb
added #replString:withString:
Claus Gittinger <cg@exept.de>
parents:
4156
diff
changeset
|
1309 |
" |
8874 | 1310 |
'12345678901234567890' replString:'123' withString:'OneTwoThree' |
1311 |
'12345678901234567890' replString:'123' withString:'*' |
|
1312 |
'12345678901234567890' replString:'234' withString:'foo' |
|
4244
5deefa3144fb
added #replString:withString:
Claus Gittinger <cg@exept.de>
parents:
4156
diff
changeset
|
1313 |
|
5deefa3144fb
added #replString:withString:
Claus Gittinger <cg@exept.de>
parents:
4156
diff
changeset
|
1314 |
('a string with spaces' replChar:$ withString:' foo ') |
20874 | 1315 |
replString:'foo' withString:'bar' |
4244
5deefa3144fb
added #replString:withString:
Claus Gittinger <cg@exept.de>
parents:
4156
diff
changeset
|
1316 |
" |
5deefa3144fb
added #replString:withString:
Claus Gittinger <cg@exept.de>
parents:
4156
diff
changeset
|
1317 |
|
8347 | 1318 |
"Modified: / 12-05-2004 / 12:00:27 / cg" |
9192 | 1319 |
! |
1320 |
||
9275 | 1321 |
subString:start to:end |
18390 | 1322 |
"same as copyFrom:to: |
19497 | 1323 |
This is an ST/V compatibility method and an alias for copyFrom:to:." |
18390 | 1324 |
|
9275 | 1325 |
^ self copyFrom:start to:end |
1326 |
||
1327 |
" |
|
1328 |
'12345678901234567890' subString:3 to:8 |
|
1329 |
" |
|
1330 |
! |
|
1331 |
||
9192 | 1332 |
trimBlanks |
14992 | 1333 |
"return a copy of the receiver without leading and trailing spaces. |
18464 | 1334 |
This is an ST/V compatibility method and an alias for withoutSpaces." |
9192 | 1335 |
|
1336 |
^ self withoutSpaces |
|
1337 |
||
1338 |
" |
|
1339 |
' spaces at beginning' trimBlanks |
|
1340 |
'spaces at end ' trimBlanks |
|
1341 |
' spaces at beginning and end ' trimBlanks |
|
1342 |
'no spaces' trimBlanks |
|
1343 |
" |
|
1344 |
! ! |
|
1345 |
||
1346 |
!CharacterArray methodsFor:'Compatibility-Squeak'! |
|
4409
86e049b21f8d
added #includesSubstring:caseSensitive:
Claus Gittinger <cg@exept.de>
parents:
4389
diff
changeset
|
1347 |
|
19171 | 1348 |
asBoldText |
1349 |
"return self as a bold text" |
|
1350 |
||
21843 | 1351 |
"this test allows for small non-gui apps to be built without libbasic2 (where Text is)" |
1352 |
Text isNil ifTrue:[^ self]. |
|
1353 |
^ Text string: self emphasis: #bold |
|
1354 |
||
1355 |
"Modified (comment): / 20-06-2017 / 08:23:49 / cg" |
|
19171 | 1356 |
! |
1357 |
||
6148 | 1358 |
asDate |
1359 |
"Many allowed forms, see Date.readFrom:" |
|
1360 |
||
1361 |
^ Date readFrom: (ReadStream on: self) |
|
1362 |
||
1363 |
" |
|
8874 | 1364 |
'30 Apr 1999' asDate dayName capitalized |
6148 | 1365 |
" |
1366 |
! |
|
1367 |
||
10703
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
1368 |
findDelimiters:delimiters startingAt:start |
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
1369 |
"Answer the index of the character within the receiver, starting at start, |
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
1370 |
that matches one of the delimiters. |
18391 | 1371 |
If the receiver does not contain any of the delimiters, answer size + 1." |
9192 | 1372 |
|
1373 |
|idx| |
|
1374 |
||
1375 |
idx := self indexOfAny:delimiters startingAt:start. |
|
1376 |
idx == 0 ifTrue:[^ self size + 1]. |
|
1377 |
^ idx. |
|
1378 |
"/start to: self size do: [:i | |
|
1379 |
"/ |char| |
|
1380 |
"/ |
|
1381 |
"/ char := self at: i. |
|
1382 |
"/ delimiters do: [:delim | delim = char ifTrue: [^ i]] |
|
8971 | 1383 |
"/ ]. |
1384 |
"/ ^ self size + 1 |
|
1385 |
! |
|
1386 |
||
22005 | 1387 |
findString:key startingAt:start caseSensitive:caseSensitive |
1388 |
^ self indexOfSubCollection:key startingAt:start ifAbsent:[0] caseSensitive:caseSensitive |
|
1389 |
||
1390 |
"Created: / 13-07-2017 / 12:44:50 / cg" |
|
1391 |
! |
|
1392 |
||
13624 | 1393 |
findTokens:delimiterOrDelimiters |
7270 | 1394 |
"cg: I am not sure, if this is really the squeak semantics (w.r.t. empty fields)" |
1395 |
||
13624 | 1396 |
delimiterOrDelimiters size == 0 ifTrue:[ |
20874 | 1397 |
^ self asCollectionOfSubstringsSeparatedBy:delimiterOrDelimiters |
13624 | 1398 |
] ifFalse:[ |
20874 | 1399 |
^ self asCollectionOfSubstringsSeparatedByAny:delimiterOrDelimiters |
13624 | 1400 |
]. |
7270 | 1401 |
|
1402 |
" |
|
8874 | 1403 |
'a|b#c||e' findTokens:#($# $|) |
13624 | 1404 |
'a|b#c||e' findTokens:$| |
1405 |
" |
|
1406 |
||
1407 |
"Modified: / 04-09-2011 / 20:31:21 / cg" |
|
1408 |
"Modified (comment): / 05-09-2011 / 04:25:28 / cg" |
|
4981 | 1409 |
! |
1410 |
||
4974 | 1411 |
includesSubString:aString |
1412 |
"return true, if a substring is contained in the receiver. |
|
1413 |
The compare is case sensitive." |
|
1414 |
||
1415 |
^ self includesString:aString |
|
1416 |
||
1417 |
" |
|
8874 | 1418 |
'hello world' includesSubString:'Hel' |
9192 | 1419 |
'hello world' includesSubString:'hel' |
1420 |
'hello world' includesSubString:'llo' |
|
1421 |
" |
|
1422 |
||
1423 |
||
1424 |
||
1425 |
! |
|
1426 |
||
11178 | 1427 |
includesSubString:aString caseSensitive:caseSensitive |
18391 | 1428 |
"sigh - an alias; added for Squeak/Pharo compatibility" |
1429 |
||
11179 | 1430 |
^ self includesSubstring:aString caseSensitive:caseSensitive |
11178 | 1431 |
! |
1432 |
||
16875
32e61bb3a742
class: CharacterArray>>#includesSubstring: (for Squeak compatibility)
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
16863
diff
changeset
|
1433 |
includesSubstring: aString |
18391 | 1434 |
"sigh - an alias; added for Squeak/Pharo compatibility" |
1435 |
||
24085 | 1436 |
^ self includesString: aString |
16875
32e61bb3a742
class: CharacterArray>>#includesSubstring: (for Squeak compatibility)
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
16863
diff
changeset
|
1437 |
|
32e61bb3a742
class: CharacterArray>>#includesSubstring: (for Squeak compatibility)
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
16863
diff
changeset
|
1438 |
"Created: / 03-10-2014 / 02:47:52 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
24085 | 1439 |
"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
|
1440 |
! |
32e61bb3a742
class: CharacterArray>>#includesSubstring: (for Squeak compatibility)
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
16863
diff
changeset
|
1441 |
|
9192 | 1442 |
includesSubstring:aString caseSensitive:caseSensitive |
9190 | 1443 |
"return true, if a substring is contained in the receiver. |
9192 | 1444 |
The argument, caseSensitive controls if case is ignored in the compare." |
1445 |
||
24085 | 1446 |
^ (self indexOfSubCollection:aString startingAt:1 ifAbsent:0 caseSensitive:caseSensitive) ~~ 0 |
9190 | 1447 |
|
1448 |
" |
|
9192 | 1449 |
'hello world' includesSubstring:'Hel' caseSensitive:true |
1450 |
'hello world' includesSubstring:'Hel' caseSensitive:false |
|
1451 |
" |
|
1452 |
||
24085 | 1453 |
"Modified (comment): / 16-04-2019 / 12:35:33 / Stefan Vogel" |
9192 | 1454 |
! |
9190 | 1455 |
|
6979 | 1456 |
isAllDigits |
1457 |
"Answer whether the receiver's characters are all digits" |
|
1458 |
||
1459 |
^ self conform:[:eachChar | eachChar isDigit] |
|
1460 |
||
1461 |
" |
|
1462 |
'hello world' isAllDigits |
|
1463 |
'12344' isAllDigits |
|
1464 |
" |
|
1465 |
! |
|
1466 |
||
4890 | 1467 |
lastSpacePosition |
18391 | 1468 |
"return the index of the last space character; 0 if there is none. |
1469 |
Added for Squeak/Pharo compatibility" |
|
14405
1d0d99a95bba
comment/format in:7 methods
Claus Gittinger <cg@exept.de>
parents:
14168
diff
changeset
|
1470 |
|
20739 | 1471 |
"/ CG: is this correct? separator or space only??? |
4890 | 1472 |
^ self lastIndexOfSeparator |
1473 |
! |
|
1474 |
||
20738 | 1475 |
linesDo:aBlock |
1476 |
"evaluate the argument, aBlock for all lines, |
|
1477 |
up to the end" |
|
1478 |
||
1479 |
^ self readStream linesDo:aBlock |
|
1480 |
! |
|
1481 |
||
7022 | 1482 |
padded:leftOrRight to:paddedSize with:padCharacter |
20155 | 1483 |
"pad left (leftOrRight == #left) or right" |
14053
acbb97913c30
comment/format in: #padded:to:with:
Claus Gittinger <cg@exept.de>
parents:
14050
diff
changeset
|
1484 |
|
7022 | 1485 |
leftOrRight == #left ifTrue:[ |
20155 | 1486 |
^ self leftPaddedTo:paddedSize with:padCharacter |
9190 | 1487 |
]. |
1488 |
^ self paddedTo:paddedSize with:padCharacter |
|
1489 |
||
1490 |
" |
|
8874 | 1491 |
'hello' padded:#right to:10 with:$. |
9192 | 1492 |
'hello' padded:#left to:10 with:$. |
1493 |
" |
|
14053
acbb97913c30
comment/format in: #padded:to:with:
Claus Gittinger <cg@exept.de>
parents:
14050
diff
changeset
|
1494 |
|
acbb97913c30
comment/format in: #padded:to:with:
Claus Gittinger <cg@exept.de>
parents:
14050
diff
changeset
|
1495 |
"Modified (comment): / 07-03-2012 / 16:31:33 / cg" |
9192 | 1496 |
! |
1497 |
||
1498 |
skipDelimiters:delimiters startingAt:start |
|
1499 |
"Answer the index of the character within the receiver, starting at start, |
|
1500 |
that does NOT match one of the delimiters. |
|
1501 |
If the receiver does not contain any of the delimiters, answer size + 1. |
|
1502 |
Assumes the delimiters to be a non-empty string." |
|
1503 |
||
1504 |
start to:self size do:[:i | |
|
20874 | 1505 |
delimiters detect:[:delim | delim = (self at:i) ] ifNone:[ ^ i ] |
9192 | 1506 |
]. |
1507 |
^ self size + 1 |
|
1508 |
||
1509 |
" |
|
1510 |
'123***7890' skipDelimiters:'*' startingAt:4 |
|
1511 |
'123***7890' skipDelimiters:'*' startingAt:3 |
|
9190 | 1512 |
'123***7890' skipDelimiters:'*' startingAt:10 |
1513 |
'123*******' skipDelimiters:'*' startingAt:10 |
|
8874 | 1514 |
" |
1515 |
! |
|
1516 |
||
9190 | 1517 |
substrings |
18921 | 1518 |
"return a collection consisting of all words contained in the receiver. |
17517 | 1519 |
Words are separated by whitespace. |
1520 |
This has been added for Squeak compatibility. |
|
1521 |
(sigh: it is called #'subStrings' in V'Age, and #'asCollectionOfWords' in ST/X) " |
|
1522 |
||
9190 | 1523 |
^ self asCollectionOfWords |
1524 |
||
1525 |
" |
|
1526 |
'foo bar baz' substrings |
|
1527 |
" |
|
1528 |
! |
|
1529 |
||
12665
b027fc5aec4e
added: #substringsSeparatedBy:
Claus Gittinger <cg@exept.de>
parents:
12664
diff
changeset
|
1530 |
substringsSeparatedBy:separatorCharacter |
18921 | 1531 |
"return a collection consisting of all words contained in the receiver. |
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
1532 |
Words are separated by the given separator character. |
18921 | 1533 |
This has been added for Squeak/Pharo compatibility. |
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
1534 |
(sigh: it is called #'subStrings:' in V'Age, |
18921 | 1535 |
and #'asCollectionOfSubstringsSeparatedBy' in ST/X) " |
18391 | 1536 |
|
12665
b027fc5aec4e
added: #substringsSeparatedBy:
Claus Gittinger <cg@exept.de>
parents:
12664
diff
changeset
|
1537 |
^ self asCollectionOfSubstringsSeparatedBy:separatorCharacter |
b027fc5aec4e
added: #substringsSeparatedBy:
Claus Gittinger <cg@exept.de>
parents:
12664
diff
changeset
|
1538 |
|
b027fc5aec4e
added: #substringsSeparatedBy:
Claus Gittinger <cg@exept.de>
parents:
12664
diff
changeset
|
1539 |
" |
b027fc5aec4e
added: #substringsSeparatedBy:
Claus Gittinger <cg@exept.de>
parents:
12664
diff
changeset
|
1540 |
'foo bar, baz' substringsSeparatedBy:$, |
21151 | 1541 |
'1.2.3.4' substringsSeparatedBy:$. |
12665
b027fc5aec4e
added: #substringsSeparatedBy:
Claus Gittinger <cg@exept.de>
parents:
12664
diff
changeset
|
1542 |
" |
b027fc5aec4e
added: #substringsSeparatedBy:
Claus Gittinger <cg@exept.de>
parents:
12664
diff
changeset
|
1543 |
! |
b027fc5aec4e
added: #substringsSeparatedBy:
Claus Gittinger <cg@exept.de>
parents:
12664
diff
changeset
|
1544 |
|
23188 | 1545 |
trimBoth |
1546 |
"return a copy of the receiver without leading and trailing whiteSpace. |
|
1547 |
Added for Squeak compatibility (an alias for withoutSeparators)" |
|
1548 |
||
1549 |
^ self withoutSeparators |
|
1550 |
||
1551 |
"Created: / 03-07-2018 / 11:08:29 / Claus Gittinger" |
|
1552 |
! |
|
1553 |
||
9190 | 1554 |
truncateTo:smallSize |
9192 | 1555 |
"return myself or a copy shortened to smallSize. 1/18/96 sw" |
1556 |
||
1557 |
self size <= smallSize ifTrue:[^ self]. |
|
1558 |
^ self copyFrom: 1 to: smallSize |
|
1559 |
||
1560 |
" |
|
1561 |
'hello world' truncateTo:5 |
|
1562 |
'hello' truncateTo:10 |
|
1563 |
||
1564 |
'hello world' copyTo:5 |
|
1565 |
'hello' copyTo:10 |
|
1566 |
" |
|
1567 |
! |
|
5795 | 1568 |
|
4890 | 1569 |
withBlanksTrimmed |
18464 | 1570 |
"Return a copy of the receiver from which leading and trailing whitespace have been trimmed. |
1571 |
Notice the bad naming - it is trimming separators, not just blanks. |
|
1572 |
Added for Squeak compatibility and an alias for withoutSeparators" |
|
4890 | 1573 |
|
14865 | 1574 |
^ self withoutSeparators "withoutSpaces" "/ cg: it seems that squeak cares for any whitespace |
4890 | 1575 |
|
1576 |
" |
|
8874 | 1577 |
' hello world ' withBlanksTrimmed |
4890 | 1578 |
" |
1579 |
! |
|
1580 |
||
12188 | 1581 |
withoutLeading: char |
1582 |
"return a copy of myself without leading a char. |
|
1583 |
Returns an empty string, if the receiver consist only of a char." |
|
1584 |
||
1585 |
^ self withoutLeadingForWhich:[:ch | ch = char] |
|
1586 |
||
1587 |
" |
|
13882
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
1588 |
'****foo****' withoutLeading: $* |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
1589 |
'foo****' withoutLeading: $* |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
1590 |
'*' withoutLeading: $* |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
1591 |
'' withoutLeading: $* |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
1592 |
'****foo' withoutLeading: $* |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
1593 |
'*******' withoutLeading: $* |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
1594 |
'foo' withoutLeading: $* |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
1595 |
'f***o***o' withoutLeading: $* |
12188 | 1596 |
('**' , Character tab asString , '*foo***') withoutLeading: $* inspect |
1597 |
" |
|
1598 |
! |
|
1599 |
||
11565 | 1600 |
withoutTrailing:char |
1601 |
"return a copy of myself without trailing char. |
|
1602 |
Returns an empty string, if the receiver consist only of char." |
|
1603 |
||
12188 | 1604 |
^ self withoutTrailingForWhich:[:ch | ch = char] |
1605 |
||
1606 |
" |
|
13882
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
1607 |
' foo....' withoutTrailing:$. |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
1608 |
'foo....' withoutTrailing:$. |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
1609 |
' foo' withoutTrailing:$. |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
1610 |
'.......' withoutTrailing:$. |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
1611 |
'foo' withoutTrailing:$. |
11565 | 1612 |
" |
9190 | 1613 |
! ! |
1614 |
||
1615 |
!CharacterArray methodsFor:'Compatibility-V''Age'! |
|
1616 |
||
1617 |
addLineDelimiter |
|
1618 |
"replace all '\'-characters by line delimiter (cr) - characters. |
|
9192 | 1619 |
This has been added for VisualAge compatibility." |
1620 |
||
1621 |
^ self withCRs |
|
1622 |
! |
|
1623 |
||
1624 |
bindWith:aString |
|
1625 |
"return a copy of the receiver, where a '%1' escape is |
|
1626 |
replaced by aString. |
|
1627 |
This has been added for VisualAge compatibility." |
|
1628 |
||
24038 | 1629 |
^ self expandPlaceholdersWith:{ aString } |
9192 | 1630 |
|
1631 |
" |
|
1632 |
'do you like %1 ?' bindWith:'smalltalk' |
|
19785 | 1633 |
'do you like %(foo) ?' bindWithArguments:(Dictionary new at:'foo' put:'smalltalk'; yourself) |
9192 | 1634 |
" |
24038 | 1635 |
|
1636 |
"Modified (format): / 02-04-2019 / 14:05:22 / Claus Gittinger" |
|
9192 | 1637 |
! |
608 | 1638 |
|
1639 |
bindWith:string1 with:string2 |
|
1640 |
"return a copy of the receiver, where a '%1' escape is |
|
1641 |
replaced by string1 and '%2' is replaced by string2. |
|
1642 |
This has been added for VisualAge compatibility." |
|
1643 |
||
24038 | 1644 |
^ self expandPlaceholdersWith:{string1 . string2} |
608 | 1645 |
|
1646 |
" |
|
1647 |
'do you prefer %1 or rather %2 ?' |
|
20874 | 1648 |
bindWith:'smalltalk' with:'c++' |
9192 | 1649 |
" |
24038 | 1650 |
|
1651 |
"Modified (format): / 02-04-2019 / 14:05:32 / Claus Gittinger" |
|
9192 | 1652 |
! |
9190 | 1653 |
|
1654 |
bindWith:str1 with:str2 with:str3 |
|
608 | 1655 |
"return a copy of the receiver, where a '%1', '%2' and '%3' escapes |
1656 |
are replaced by str1, str2 and str3 respectively. |
|
1657 |
This has been added for VisualAge compatibility." |
|
1658 |
||
24038 | 1659 |
^ self expandPlaceholdersWith:{ str1 . str2 . str3 } |
608 | 1660 |
|
1661 |
" |
|
9192 | 1662 |
'do you prefer %1 or rather %2 (not talking about %3) ?' |
20874 | 1663 |
bindWith:'smalltalk' with:'c++' with:'c' |
9192 | 1664 |
" |
24038 | 1665 |
|
1666 |
"Modified (format): / 02-04-2019 / 14:05:45 / Claus Gittinger" |
|
9192 | 1667 |
! |
608 | 1668 |
|
1669 |
bindWith:str1 with:str2 with:str3 with:str4 |
|
1670 |
"return a copy of the receiver, where a '%1', '%2', '%3' and '%4' escapes |
|
1671 |
are replaced by str1, str2, str3 and str4 respectively. |
|
1672 |
This has been added for VisualAge compatibility." |
|
1673 |
||
24038 | 1674 |
^ self expandPlaceholdersWith:{ str1 . str2 . str3 . str4 } |
608 | 1675 |
|
1676 |
" |
|
9192 | 1677 |
'do you prefer %1 or rather %2 (not talking about %3 or even %4) ?' |
20874 | 1678 |
bindWith:'smalltalk' with:'c++' with:'c' with:'assembler' |
9192 | 1679 |
" |
24038 | 1680 |
|
1681 |
"Modified (format): / 02-04-2019 / 14:06:01 / Claus Gittinger" |
|
9192 | 1682 |
! |
608 | 1683 |
|
2344 | 1684 |
bindWith:str1 with:str2 with:str3 with:str4 with:str5 |
1685 |
"return a copy of the receiver, where a '%1' .. '%5' escapes |
|
1686 |
are replaced by str1 .. str5 respectively. |
|
1687 |
This has been added for VisualAge compatibility." |
|
1688 |
||
24038 | 1689 |
^ self expandPlaceholdersWith:{ str1 . str2 . str3 . str4 . str5 } |
1690 |
||
1691 |
"Created: / 31-01-1997 / 16:25:42 / cg" |
|
1692 |
"Modified: / 02-04-2019 / 14:06:17 / Claus Gittinger" |
|
2344 | 1693 |
! |
1694 |
||
5343 | 1695 |
bindWith:str1 with:str2 with:str3 with:str4 with:str5 with:str6 |
1696 |
"return a copy of the receiver, where a '%1' .. '%6' escapes |
|
11524 | 1697 |
are replaced by str1 .. str6 respectively. |
5343 | 1698 |
This has been added for VisualAge compatibility." |
1699 |
||
24038 | 1700 |
^ self expandPlaceholdersWith:{ str1 . str2 . str3 . str4 . str5 . str6 } |
1701 |
||
1702 |
"Modified (format): / 02-04-2019 / 14:06:36 / Claus Gittinger" |
|
11524 | 1703 |
! |
1704 |
||
1705 |
bindWith:str1 with:str2 with:str3 with:str4 with:str5 with:str6 with:str7 |
|
1706 |
"return a copy of the receiver, where a '%1' .. '%7' escapes |
|
1707 |
are replaced by str1 .. str7 respectively. |
|
1708 |
This has been added for VisualAge compatibility." |
|
1709 |
||
24038 | 1710 |
^ self expandPlaceholdersWith:{ str1 . str2 . str3 . str4 . str5 . str6 . str7 } |
1711 |
||
1712 |
"Modified (format): / 02-04-2019 / 14:06:59 / Claus Gittinger" |
|
5343 | 1713 |
! |
1714 |
||
13996
a86faa4f17b8
added: #bindWith:with:with:with:with:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
13987
diff
changeset
|
1715 |
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
|
1716 |
"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
|
1717 |
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
|
1718 |
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
|
1719 |
|
24038 | 1720 |
^ 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
|
1721 |
|
a86faa4f17b8
added: #bindWith:with:with:with:with:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
13987
diff
changeset
|
1722 |
"Created: / 06-02-2012 / 10:33:18 / cg" |
24038 | 1723 |
"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
|
1724 |
! |
a86faa4f17b8
added: #bindWith:with:with:with:with:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
13987
diff
changeset
|
1725 |
|
14014
3aff2d7c41c8
added: #bindWith:with:with:with:with:with:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
13996
diff
changeset
|
1726 |
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
|
1727 |
"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
|
1728 |
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
|
1729 |
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
|
1730 |
|
24038 | 1731 |
^ 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
|
1732 |
|
3aff2d7c41c8
added: #bindWith:with:with:with:with:with:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
13996
diff
changeset
|
1733 |
"Created: / 14-02-2012 / 17:42:31 / cg" |
24038 | 1734 |
"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
|
1735 |
! |
3aff2d7c41c8
added: #bindWith:with:with:with:with:with:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
13996
diff
changeset
|
1736 |
|
22924 | 1737 |
bindWithArguments:argumentsCollection |
608 | 1738 |
"return a copy of the receiver, where a '%i' escape |
21747 | 1739 |
is replaced by the corresponding string from the argument array. |
22924 | 1740 |
'i' may be between 1 and 9 (i.e. a maximum of 9 placeholders is allowed) |
1741 |
or %(key); the argumentsCollection must then be a dictionary. |
|
21255 | 1742 |
To get an integer-indexed placeHolder followed by another digit, |
1743 |
or an index > 9, you must use %(digit). |
|
608 | 1744 |
This has been added for VisualAge compatibility." |
1745 |
||
22924 | 1746 |
^ self expandPlaceholdersWith:argumentsCollection |
9192 | 1747 |
|
1748 |
" |
|
1749 |
'do you prefer %1 or rather %2 (not talking about %3) ?' |
|
20874 | 1750 |
bindWithArguments:#('smalltalk' 'c++' 'c') |
12196 | 1751 |
|
1752 |
'do you %(what) ?' |
|
20874 | 1753 |
bindWithArguments:(Dictionary new at:#'what' put:'understand'; yourself) |
9192 | 1754 |
" |
21747 | 1755 |
|
1756 |
"Modified (comment): / 11-05-2017 / 12:42:57 / mawalch" |
|
9192 | 1757 |
! |
608 | 1758 |
|
1759 |
subStrings |
|
18921 | 1760 |
"return a collection consisting of all words contained in the receiver. |
608 | 1761 |
Words are separated by whitespace. |
17517 | 1762 |
This has been added for VisualAge compatibility. |
1763 |
(sigh: it is called #'subbtrings' in Squeak, and #'asCollectionOfWords' in ST/X) " |
|
608 | 1764 |
|
1765 |
^ self asCollectionOfWords |
|
1766 |
||
1767 |
" |
|
1768 |
'hello world, this is smalltalk' subStrings |
|
1769 |
" |
|
1770 |
! |
|
1771 |
||
16181 | 1772 |
subStrings:separatorCharacterOrString |
18921 | 1773 |
"return a collection consisting of all words contained in the receiver. |
608 | 1774 |
Words are separated by separatorCharacter. |
18921 | 1775 |
This is similar to split: (squeak) and asCollectionOfSubstringsSeparatedBy: (st/x) |
1776 |
and has been added for VisualAge compatibility." |
|
608 | 1777 |
|
16181 | 1778 |
separatorCharacterOrString isCharacter ifTrue:[ |
20874 | 1779 |
^ self asCollectionOfSubstringsSeparatedBy:separatorCharacterOrString |
16181 | 1780 |
]. |
1781 |
^ self asCollectionOfSubstringsSeparatedByAny:separatorCharacterOrString |
|
1782 |
||
1783 |
" |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
1784 |
'foo:bar:baz:smalltalk' subStrings:$: |
16181 | 1785 |
'foo:bar:baz:smalltalk' subStrings:':' |
1786 |
'foo.bar,baz-smalltalk' subStrings:'.,-' |
|
9192 | 1787 |
" |
1788 |
! |
|
1789 |
||
1790 |
trimSeparators |
|
18392 | 1791 |
"return a copy of the receiver without leading and trailing whiteSpace. |
18921 | 1792 |
Added for VisualAge compatibility (an alias for withoutSeparators)" |
9192 | 1793 |
|
1794 |
^ self withoutSeparators |
|
1795 |
! ! |
|
1796 |
||
1797 |
!CharacterArray methodsFor:'Compatibility-VW'! |
|
5964 | 1798 |
|
1799 |
expandMacros |
|
1800 |
"ST80 compatibility - expand '<..>' macros with |
|
1801 |
argument strings. Similar to #bindWith:. |
|
1802 |
Read the comment in #expandMacrosWithArguments: about |
|
1803 |
limited compatibility issues." |
|
1804 |
||
1805 |
^ self expandMacrosWithArguments:#() |
|
1806 |
||
1807 |
" |
|
1808 |
'hello<n>foo' expandMacros |
|
1809 |
" |
|
1810 |
||
9192 | 1811 |
"Modified: / 18.6.1998 / 16:03:02 / cg" |
1812 |
! |
|
1813 |
||
1814 |
expandMacrosWith:arg |
|
5964 | 1815 |
"ST80 compatibility - expand '<..>' macros with |
1816 |
argument strings. Similar to #bindWith:. |
|
1817 |
Read the comment in #expandMacrosWithArguments: about |
|
1818 |
limited compatibility issues." |
|
1819 |
||
24038 | 1820 |
^ self expandMacrosWithArguments:{ arg } |
1821 |
||
1822 |
"Created: / 01-11-1997 / 13:01:28 / cg" |
|
1823 |
"Modified: / 02-04-2019 / 14:09:26 / Claus Gittinger" |
|
9192 | 1824 |
! |
5964 | 1825 |
|
1826 |
expandMacrosWith:arg1 with:arg2 |
|
1827 |
"ST80 compatibility - expand '<..>' macros with |
|
1828 |
argument strings. Similar to #bindWith:. |
|
1829 |
Read the comment in #expandMacrosWithArguments: about |
|
1830 |
limited compatibility issues." |
|
1831 |
||
24038 | 1832 |
^ self expandMacrosWithArguments:{arg1 . arg2 } |
1833 |
||
1834 |
"Modified: / 06-07-1998 / 21:58:14 / cg" |
|
1835 |
"Modified: / 02-04-2019 / 14:09:36 / Claus Gittinger" |
|
5964 | 1836 |
! |
1837 |
||
1838 |
expandMacrosWith:arg1 with:arg2 with:arg3 |
|
1839 |
"ST80 compatibility - expand '<..>' macros with |
|
1840 |
argument strings. Similar to #bindWith:. |
|
1841 |
Read the comment in #expandMacrosWithArguments: about |
|
1842 |
limited compatibility issues." |
|
1843 |
||
24038 | 1844 |
^ self expandMacrosWithArguments:{ arg1 . arg2 . arg3 } |
1845 |
||
1846 |
"Modified (format): / 02-04-2019 / 14:09:48 / Claus Gittinger" |
|
5964 | 1847 |
! |
1848 |
||
10353
65fb7aa2c6c2
added #expandMacrosWith:with:with:with:
Stefan Vogel <sv@exept.de>
parents:
10134
diff
changeset
|
1849 |
expandMacrosWith:arg1 with:arg2 with:arg3 with:arg4 |
65fb7aa2c6c2
added #expandMacrosWith:with:with:with:
Stefan Vogel <sv@exept.de>
parents:
10134
diff
changeset
|
1850 |
"ST80 compatibility - expand '<..>' macros with |
65fb7aa2c6c2
added #expandMacrosWith:with:with:with:
Stefan Vogel <sv@exept.de>
parents:
10134
diff
changeset
|
1851 |
argument strings. Similar to #bindWith:. |
65fb7aa2c6c2
added #expandMacrosWith:with:with:with:
Stefan Vogel <sv@exept.de>
parents:
10134
diff
changeset
|
1852 |
Read the comment in #expandMacrosWithArguments: about |
65fb7aa2c6c2
added #expandMacrosWith:with:with:with:
Stefan Vogel <sv@exept.de>
parents:
10134
diff
changeset
|
1853 |
limited compatibility issues." |
65fb7aa2c6c2
added #expandMacrosWith:with:with:with:
Stefan Vogel <sv@exept.de>
parents:
10134
diff
changeset
|
1854 |
|
24038 | 1855 |
^ self expandMacrosWithArguments:{ arg1 . arg2 . arg3 . arg4 } |
1856 |
||
1857 |
"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
|
1858 |
! |
65fb7aa2c6c2
added #expandMacrosWith:with:with:with:
Stefan Vogel <sv@exept.de>
parents:
10134
diff
changeset
|
1859 |
|
9192 | 1860 |
expandMacrosWithArguments:argArray |
5964 | 1861 |
"ST80 compatibility - expand '<..>' macros with |
1862 |
argument strings. Similar to #bindWith:. |
|
1863 |
WARNING: possibly not all ST80 expansions are supported here." |
|
1864 |
||
1865 |
"/ supported expansions: |
|
1866 |
"/ |
|
1867 |
"/ <#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
|
1868 |
"/ <#s> # is arg Number; slice in the arg itself (must know asString) |
5964 | 1869 |
"/ <#?s1:s2> # is arg Number; slice in s1 if the arg is true, s2 otherwise |
7643 | 1870 |
"/ use first arg if # is not given (i.e. no number before s,p ...) |
5964 | 1871 |
"/ <n> replace by a newLine character |
1872 |
"/ <t> replace by a tab character |
|
6834
eba4b58b8692
fixed expandMacros (%-escape)
Claus Gittinger <cg@exept.de>
parents:
6810
diff
changeset
|
1873 |
"/ %X the X character itself |
5964 | 1874 |
|
8999
f3fda66ea51e
#expandMacrosWithArguments: - be mor friendly to xml strings.
Stefan Vogel <sv@exept.de>
parents:
8971
diff
changeset
|
1875 |
|in out c fmt nr arg s1 s2 peekc| |
5964 | 1876 |
|
1877 |
in := self readStream. |
|
10721
58e6950264cb
care for bitsPerCharacter when expanding macros
Claus Gittinger <cg@exept.de>
parents:
10703
diff
changeset
|
1878 |
out := CharacterWriteStream on:(self species uninitializedNew:self size). |
5964 | 1879 |
|
1880 |
[in atEnd] whileFalse:[ |
|
20874 | 1881 |
c := in next. |
1882 |
c == $% ifTrue:[ |
|
1883 |
c := in next. |
|
1884 |
out nextPut:c |
|
1885 |
] ifFalse:[c ~~ $< ifTrue:[ |
|
1886 |
out nextPut:c. |
|
1887 |
] ifFalse:[ |
|
1888 |
peekc := in peek. |
|
1889 |
[peekc == $<] whileTrue:[ |
|
1890 |
out nextPut:$<. |
|
1891 |
peekc := in nextPeek. |
|
1892 |
]. |
|
1893 |
peekc == $n ifTrue:[ |
|
1894 |
peekc := in nextPeek. |
|
1895 |
peekc == $> ifTrue:[ |
|
1896 |
in next. |
|
1897 |
out cr. |
|
1898 |
] ifFalse:[ |
|
1899 |
out nextPutAll:'<n'. |
|
1900 |
] |
|
1901 |
] ifFalse:[peekc == $t ifTrue:[ |
|
1902 |
peekc := in nextPeek. |
|
1903 |
peekc == $> ifTrue:[ |
|
1904 |
in next. |
|
1905 |
out tab. |
|
1906 |
] ifFalse:[ |
|
1907 |
out nextPutAll:'<t'. |
|
1908 |
] |
|
1909 |
] ifFalse:[ |
|
1910 |
peekc isDigit ifTrue:[ |
|
1911 |
"start an argument expansion ..." |
|
1912 |
nr := Integer readFrom:in onError:nil. |
|
1913 |
nr isNil ifTrue:[ |
|
1914 |
"this cannot happen (there is at least one digit)" |
|
23009 | 1915 |
self proceedableError:'invalid format'. |
20874 | 1916 |
^ self |
1917 |
]. |
|
1918 |
fmt := in next. |
|
22999 | 1919 |
(fmt ~~ $? and:[fmt ~~ $# and:[in peek ~~ $>]]) ifTrue:[ |
20874 | 1920 |
out nextPut:$<. |
1921 |
nr printOn:out. |
|
1922 |
out nextPut:fmt. |
|
1923 |
] ifFalse:[ |
|
1924 |
(nr between:1 and:argArray size) ifTrue:[ |
|
1925 |
arg := argArray at:nr. |
|
1926 |
] ifFalse:[ |
|
1927 |
arg := '' |
|
1928 |
]. |
|
1929 |
||
1930 |
fmt == $p ifTrue:[ |
|
1931 |
"expand with args printString" |
|
1932 |
arg printOn:out. |
|
1933 |
] ifFalse:[fmt == $s ifTrue:[ |
|
1934 |
"expand with arg itself" |
|
1935 |
arg isText ifTrue:[ |
|
1936 |
out contentsSpecies isText ifFalse:[ |
|
1937 |
out := (TextStream ? CharacterWriteStream on:Text new) nextPutAll:out contents; yourself. |
|
1938 |
]. |
|
1939 |
out nextPutAll:arg. |
|
1940 |
] ifFalse:[ |
|
1941 |
out nextPutAll:arg asString string. "see method comment: arg must know #asString" |
|
1942 |
] |
|
22999 | 1943 |
] ifFalse:[(fmt == $? or:[fmt == $#]) ifTrue:[ |
20874 | 1944 |
s1 := in upTo:$:. |
1945 |
s2 := in nextUpTo:$>. |
|
22999 | 1946 |
((fmt == $? and:[arg == true]) |
23009 | 1947 |
or:[(fmt == $# and:[arg == 1])]) ifTrue:[ |
20874 | 1948 |
out nextPutAll:s1 |
1949 |
] ifFalse:[ |
|
1950 |
out nextPutAll:s2 |
|
1951 |
]. |
|
1952 |
] ifFalse:[ |
|
1953 |
"what does VW do here ?" |
|
1954 |
self error:'invalid format' mayProceed:true. |
|
1955 |
^ self |
|
1956 |
]]]. |
|
1957 |
c := in next. |
|
1958 |
c ~~ $> ifTrue:[ |
|
1959 |
"what does VW do here ?" |
|
1960 |
self error:'invalid format' mayProceed:true. |
|
1961 |
^ self |
|
1962 |
] |
|
1963 |
||
1964 |
]. |
|
1965 |
] ifFalse:[ |
|
1966 |
out nextPut:$<. |
|
1967 |
]. |
|
1968 |
]]. |
|
1969 |
]]. |
|
5964 | 1970 |
]. |
1971 |
^ out contents |
|
1972 |
||
1973 |
" |
|
8874 | 1974 |
'hello <1s> how are you' expandMacrosWith:(OperatingSystem getLoginName) |
1975 |
'one plus one is <1p>' expandMacrosWith:2 |
|
5964 | 1976 |
" |
1977 |
||
10721
58e6950264cb
care for bitsPerCharacter when expanding macros
Claus Gittinger <cg@exept.de>
parents:
10703
diff
changeset
|
1978 |
"Modified: / 18-09-2007 / 22:50:43 / cg" |
23009 | 1979 |
"Modified: / 24-05-2018 / 21:06:31 / Claus Gittinger" |
9192 | 1980 |
! |
1981 |
||
1982 |
isCharacters |
|
21318 | 1983 |
"true, if the receiver is a string-like thing. |
1984 |
added for visual works compatibility" |
|
1985 |
||
1986 |
"/ kept in libbasic package, because it is used in libjava and imap-implementation |
|
9192 | 1987 |
^ true |
1988 |
! ! |
|
1989 |
||
16295 | 1990 |
!CharacterArray methodsFor:'JavaScript support'! |
1991 |
||
1992 |
unquote |
|
17000 | 1993 |
"removes double quotes from the receiver. |
19698 | 1994 |
This is the JavaScript standard unquote function." |
1995 |
||
1996 |
^ self unquote:$" |
|
16295 | 1997 |
|
1998 |
" |
|
17000 | 1999 |
'hello' quote unquote |
2000 |
||
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2001 |
JavaScriptParser evaluate:'''hello''.quote.unquote' |
16295 | 2002 |
" |
19698 | 2003 |
! |
2004 |
||
2005 |
unquote:quoteCharacter |
|
2006 |
"removes quoteCharacter from either end of the receiver." |
|
2007 |
||
2008 |
|mySize| |
|
2009 |
||
2010 |
(mySize := self size) >= 2 ifTrue:[ |
|
20874 | 2011 |
((self first == quoteCharacter) and:[self last == quoteCharacter]) ifTrue:[ |
2012 |
^ self copyFrom:2 to:mySize-1 |
|
2013 |
]. |
|
19698 | 2014 |
]. |
2015 |
^ self |
|
2016 |
||
2017 |
" |
|
2018 |
'*hello*' unquote:$* |
|
2019 |
" |
|
16295 | 2020 |
! ! |
13510 | 2021 |
|
16175 | 2022 |
|
9192 | 2023 |
!CharacterArray methodsFor:'character searching'! |
2024 |
||
2025 |
includesMatchCharacters |
|
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
2026 |
"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
|
2027 |
for match operations; false if not. |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2028 |
Here, do not care for $\ escapes" |
608 | 2029 |
|
2030 |
^ self includesAny:'*#[' |
|
2519
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2031 |
|
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2032 |
" |
8874 | 2033 |
'*foo' includesMatchCharacters |
2034 |
'\*foo' includesMatchCharacters |
|
2035 |
'\*foo' includesUnescapedMatchCharacters |
|
2036 |
'*foo' includesMatchCharacters |
|
2519
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2037 |
'\\*foo' includesMatchCharacters |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2038 |
'foo*' includesMatchCharacters |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2039 |
'foo\*' includesMatchCharacters |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2040 |
'foo\' includesMatchCharacters |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2041 |
" |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2042 |
|
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2043 |
"Modified: 2.4.1997 / 18:12:34 / cg" |
9192 | 2044 |
! |
2045 |
||
2046 |
includesSeparator |
|
2047 |
"return true, if the receiver contains any whitespace characters" |
|
2048 |
||
2049 |
^ (self indexOfSeparator ~~ 0) |
|
2050 |
||
2051 |
" |
|
2052 |
'hello world' includesSeparator |
|
2053 |
'helloworld' includesSeparator |
|
2054 |
" |
|
2055 |
! |
|
2056 |
||
2057 |
includesUnescapedMatchCharacters |
|
8874 | 2058 |
"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
|
2059 |
for match operations; false if not. |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2060 |
Here, care for $\ escapes" |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2061 |
|
5342
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
2062 |
|idx sz specialChars escape| |
2519
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2063 |
|
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2064 |
idx := 1. |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2065 |
sz := self size. |
5342
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
2066 |
specialChars := '*#[\'. |
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
2067 |
(escape := self class matchEscapeCharacter) ~~ $\ ifTrue:[ |
20874 | 2068 |
specialChars := specialChars copy. |
2069 |
specialChars at:specialChars size put:escape |
|
5342
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
2070 |
]. |
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
2071 |
|
16245 | 2072 |
[ |
20874 | 2073 |
idx := self indexOfAny:specialChars startingAt:idx. |
2074 |
idx == 0 ifTrue:[^ false]. |
|
2075 |
(self at:idx) == escape ifFalse:[^ true]. |
|
2076 |
idx := idx + 2. |
|
2077 |
idx > sz ifTrue:[^ false]. |
|
16245 | 2078 |
] loop. |
8874 | 2079 |
|
2080 |
" |
|
2081 |
'*foo' includesUnescapedMatchCharacters |
|
2082 |
'\*foo' includesUnescapedMatchCharacters |
|
2083 |
'\\foo' includesUnescapedMatchCharacters |
|
2084 |
'\\\$foo' includesUnescapedMatchCharacters |
|
2519
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2085 |
'*foo' includesUnescapedMatchCharacters |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2086 |
'\\*foo' includesUnescapedMatchCharacters |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2087 |
'foo*' includesUnescapedMatchCharacters |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2088 |
'foo\*' includesUnescapedMatchCharacters |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2089 |
'foo\' includesUnescapedMatchCharacters |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2090 |
" |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2091 |
|
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2092 |
"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
|
2093 |
"Created: 2.4.1997 / 17:23:26 / cg" |
9192 | 2094 |
! |
2095 |
||
2096 |
indexOfControlCharacterStartingAt:startIndex |
|
3668
76271941f044
added #indexOfControlCharacterStartingAt:
Claus Gittinger <cg@exept.de>
parents:
3656
diff
changeset
|
2097 |
"return the index of the next control character; |
76271941f044
added #indexOfControlCharacterStartingAt:
Claus Gittinger <cg@exept.de>
parents:
3656
diff
changeset
|
2098 |
that is a character with asciiValue < 32. |
15605 | 2099 |
Start the search at startIndex, searching forward. |
3668
76271941f044
added #indexOfControlCharacterStartingAt:
Claus Gittinger <cg@exept.de>
parents:
3656
diff
changeset
|
2100 |
Return 0 if none is found." |
76271941f044
added #indexOfControlCharacterStartingAt:
Claus Gittinger <cg@exept.de>
parents:
3656
diff
changeset
|
2101 |
|
76271941f044
added #indexOfControlCharacterStartingAt:
Claus Gittinger <cg@exept.de>
parents:
3656
diff
changeset
|
2102 |
|start "{ Class: SmallInteger }" |
9192 | 2103 |
mySize "{ Class: SmallInteger }"| |
2104 |
||
2105 |
start := startIndex. |
|
2106 |
mySize := self size. |
|
2107 |
||
2108 |
start to:mySize do:[:index | |
|
20874 | 2109 |
(self at:index) isControlCharacter ifTrue:[^ index] |
9192 | 2110 |
]. |
2111 |
^ 0 |
|
2112 |
||
2113 |
" |
|
2114 |
'hello world' asTwoByteString indexOfControlCharacterStartingAt:1 |
|
2115 |
'hello world\foo' withCRsasTwoByteString indexOfControlCharacterStartingAt:1 |
|
2116 |
" |
|
2117 |
||
2118 |
"Modified: / 21.7.1998 / 17:25:07 / cg" |
|
2119 |
! |
|
3668
76271941f044
added #indexOfControlCharacterStartingAt:
Claus Gittinger <cg@exept.de>
parents:
3656
diff
changeset
|
2120 |
|
8541 | 2121 |
indexOfNonSeparator |
2122 |
"return the index of the first non-whitespace character. |
|
2123 |
return 0 if no non-separator was found" |
|
2124 |
||
2125 |
^ self indexOfNonSeparatorStartingAt:1. |
|
2126 |
||
2127 |
" |
|
8874 | 2128 |
' hello world' indexOfNonSeparator |
2129 |
' ' indexOfNonSeparator |
|
2130 |
'a ' indexOfNonSeparator |
|
2131 |
'abc' indexOfNonSeparator |
|
2132 |
' ' indexOfNonSeparator |
|
9192 | 2133 |
'' indexOfNonSeparator |
2134 |
" |
|
2135 |
! |
|
2136 |
||
2137 |
indexOfNonSeparatorStartingAt:startIndex |
|
4117 | 2138 |
"return the index of the next non-whitespace character, |
2139 |
starting the search at startIndex, searching forward; |
|
2140 |
return 0 if no non-separator was found" |
|
608 | 2141 |
|
2142 |
|start "{ Class: SmallInteger }" |
|
2143 |
mySize "{ Class: SmallInteger }"| |
|
2144 |
||
2145 |
start := startIndex. |
|
2146 |
mySize := self size. |
|
2147 |
||
2148 |
start to:mySize do:[:index | |
|
20874 | 2149 |
(self at:index) isSeparator ifFalse:[^ index] |
608 | 2150 |
]. |
2151 |
^ 0 |
|
2152 |
||
2153 |
" |
|
8874 | 2154 |
' hello world' indexOfNonSeparatorStartingAt:1 |
2155 |
' ' indexOfNonSeparatorStartingAt:1 |
|
2156 |
'a ' indexOfNonSeparatorStartingAt:2 |
|
4117 | 2157 |
" |
2158 |
||
608 | 2159 |
" |
2160 |
|s index1 index2| |
|
2161 |
s := ' foo bar baz'. |
|
2162 |
index1 := s indexOfNonSeparatorStartingAt:1. |
|
2163 |
index2 := s indexOfSeparatorStartingAt:index1. |
|
2164 |
s copyFrom:index1 to:index2 - 1 |
|
2165 |
" |
|
9192 | 2166 |
! |
2167 |
||
2168 |
indexOfSeparator |
|
2169 |
"return the index of the first whitespace character; |
|
2170 |
starting the search at the beginning, searching forward; |
|
2171 |
return 0 if no separator was found" |
|
2172 |
||
2173 |
^ self indexOfSeparatorStartingAt:1 |
|
2174 |
||
2175 |
" |
|
2176 |
'hello world' indexOfSeparator |
|
2177 |
'helloworld' indexOfSeparator |
|
2178 |
'hello ' indexOfSeparator |
|
2179 |
' hello' indexOfSeparator |
|
2180 |
" |
|
2181 |
! |
|
2182 |
||
15912 | 2183 |
indexOfSeparatorOrEndStartingAt:startIndex |
2184 |
"return the index of the next whitespace character, |
|
2185 |
starting the search at startIndex, searching forward; |
|
19737 | 2186 |
return the index of one beyond the end of the receiver if no separator was found. |
15912 | 2187 |
To extract the word, copy from startIndex to the returned index-1" |
2188 |
||
2189 |
|idx| |
|
2190 |
||
2191 |
idx := self indexOfSeparatorStartingAt:startIndex. |
|
2192 |
idx == 0 ifTrue:[^ self size + 1]. |
|
2193 |
^ idx. |
|
2194 |
||
2195 |
" |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2196 |
'hello world' indexOfSeparatorOrEndStartingAt:3 |
15912 | 2197 |
' hello world' indexOfSeparatorOrEndStartingAt:3 |
2198 |
'hello world ' indexOfSeparatorOrEndStartingAt:3 |
|
2199 |
'hello world ' indexOfSeparatorOrEndStartingAt:6 |
|
2200 |
'hello world ' indexOfSeparatorOrEndStartingAt:7 |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2201 |
'helloworld ' indexOfSeparatorOrEndStartingAt:7 |
15912 | 2202 |
'helloworld' indexOfSeparatorOrEndStartingAt:7 |
19737 | 2203 |
'helloworld' indexOfSeparatorStartingAt:7 |
15912 | 2204 |
" |
2205 |
! |
|
2206 |
||
9192 | 2207 |
indexOfSeparatorStartingAt:startIndex |
2208 |
"return the index of the next whitespace character, |
|
2209 |
starting the search at startIndex, searching forward; |
|
2210 |
return 0 if no separator was found" |
|
2211 |
||
2212 |
|start "{ Class: SmallInteger }" |
|
2213 |
mySize "{ Class: SmallInteger }"| |
|
2214 |
||
2215 |
start := startIndex. |
|
2216 |
mySize := self size. |
|
2217 |
||
2218 |
start to:mySize do:[:index | |
|
20874 | 2219 |
(self at:index) isSeparator ifTrue:[^ index] |
9192 | 2220 |
]. |
2221 |
^ 0 |
|
2222 |
||
2223 |
" |
|
2224 |
'hello world' indexOfSeparatorStartingAt:3 |
|
2225 |
' hello world' indexOfSeparatorStartingAt:3 |
|
8874 | 2226 |
'hello world ' indexOfSeparatorStartingAt:3 |
2227 |
'hello world ' indexOfSeparatorStartingAt:6 |
|
2228 |
'hello world ' indexOfSeparatorStartingAt:7 |
|
2229 |
'helloworld ' indexOfSeparatorStartingAt:7 |
|
2230 |
'helloworld' indexOfSeparatorStartingAt:7 |
|
4117 | 2231 |
" |
2519
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2232 |
! |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2233 |
|
4117 | 2234 |
lastIndexOfSeparator |
14150
e13160aa321a
comment/format in: #lastIndexOfSeparator
Claus Gittinger <cg@exept.de>
parents:
14141
diff
changeset
|
2235 |
"return the last index of a whitespace character (space or tab). |
4117 | 2236 |
(i.e. start the search at the end and search backwards); |
2237 |
Returns 0 if no separator is found." |
|
2238 |
||
2239 |
^ self lastIndexOfSeparatorStartingAt:(self size) |
|
2240 |
||
2241 |
" |
|
8874 | 2242 |
'hello world' lastIndexOfSeparator |
2243 |
'helloworld' lastIndexOfSeparator |
|
2244 |
'hel lo wor ld' lastIndexOfSeparator |
|
2245 |
'hel ' lastIndexOfSeparator 6 |
|
4117 | 2246 |
" |
14150
e13160aa321a
comment/format in: #lastIndexOfSeparator
Claus Gittinger <cg@exept.de>
parents:
14141
diff
changeset
|
2247 |
|
e13160aa321a
comment/format in: #lastIndexOfSeparator
Claus Gittinger <cg@exept.de>
parents:
14141
diff
changeset
|
2248 |
"Modified (comment): / 01-06-2012 / 13:10:30 / cg" |
4117 | 2249 |
! |
2250 |
||
2251 |
lastIndexOfSeparatorStartingAt:startIndex |
|
9192 | 2252 |
"return the index of the previous whitespace character, |
2253 |
starting the search at startIndex (and searching backwards); |
|
2254 |
returns 0 if no separator was found" |
|
2255 |
||
2256 |
|start "{ Class: SmallInteger }"| |
|
2257 |
||
2258 |
start := startIndex. |
|
2259 |
||
2260 |
start to:1 by:-1 do:[:index | |
|
20874 | 2261 |
(self at:index) isSeparator ifTrue:[^ index] |
9192 | 2262 |
]. |
2263 |
^ 0 |
|
2264 |
||
2265 |
" |
|
2266 |
'hello world' lastIndexOfSeparatorStartingAt:3 |
|
2267 |
'hello world' lastIndexOfSeparatorStartingAt:7 |
|
2268 |
'helloworld' lastIndexOfSeparatorStartingAt:7 |
|
8874 | 2269 |
' helloworld' lastIndexOfSeparatorStartingAt:7 |
4117 | 2270 |
" |
608 | 2271 |
! ! |
2272 |
||
2273 |
!CharacterArray methodsFor:'comparing'! |
|
2274 |
||
6742
292775fda600
avoid endless recursion when comparing int < string
Claus Gittinger <cg@exept.de>
parents:
6655
diff
changeset
|
2275 |
< aString |
608 | 2276 |
"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
|
2277 |
receiver is greater than the argument. Otherwise return false. |
8874 | 2278 |
This comparison is based on the elements ascii code - |
9192 | 2279 |
i.e. upper/lowercase & upper/lowercase & national characters are NOT treated specially." |
2280 |
||
2281 |
|mySize "{ Class: SmallInteger }" |
|
2282 |
otherSize "{ Class: SmallInteger }" |
|
2283 |
n "{ Class: SmallInteger }" |
|
2284 |
c1 c2| |
|
2285 |
||
2286 |
mySize := self size. |
|
2287 |
otherSize := aString string size. |
|
6742
292775fda600
avoid endless recursion when comparing int < string
Claus Gittinger <cg@exept.de>
parents:
6655
diff
changeset
|
2288 |
n := mySize min:otherSize. |
292775fda600
avoid endless recursion when comparing int < string
Claus Gittinger <cg@exept.de>
parents:
6655
diff
changeset
|
2289 |
|
292775fda600
avoid endless recursion when comparing int < string
Claus Gittinger <cg@exept.de>
parents:
6655
diff
changeset
|
2290 |
1 to:n do:[:index | |
20874 | 2291 |
c1 := self at:index. |
2292 |
c2 := aString at:index. |
|
2293 |
(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
|
2294 |
]. |
292775fda600
avoid endless recursion when comparing int < string
Claus Gittinger <cg@exept.de>
parents:
6655
diff
changeset
|
2295 |
^ mySize < otherSize |
608 | 2296 |
! |
2297 |
||
2298 |
= aString |
|
2299 |
"Compare the receiver with the argument and return true if the |
|
2300 |
receiver is equal to the argument. Otherwise return false. |
|
1252 | 2301 |
|
9191 | 2302 |
This compare does NOT ignore case differences, |
608 | 2303 |
therefore 'foo' = 'Foo' will return false. |
2304 |
Since this is incompatible to ST-80 (at least, V2.x) , this may change." |
|
2305 |
||
14137
1eb1b4a3f364
Inherit from UninterpretedBytes instead of ByteArray
Stefan Vogel <sv@exept.de>
parents:
14122
diff
changeset
|
2306 |
|mySize "{ Class: SmallInteger }"| |
608 | 2307 |
|
5963
23462e06dcff
compare with Texts fixed (should compare equal if strings are)
Claus Gittinger <cg@exept.de>
parents:
5946
diff
changeset
|
2308 |
(aString isString or:[aString species == self species]) ifFalse:[ |
20874 | 2309 |
^ false |
5963
23462e06dcff
compare with Texts fixed (should compare equal if strings are)
Claus Gittinger <cg@exept.de>
parents:
5946
diff
changeset
|
2310 |
]. |
608 | 2311 |
mySize := self size. |
14137
1eb1b4a3f364
Inherit from UninterpretedBytes instead of ByteArray
Stefan Vogel <sv@exept.de>
parents:
14122
diff
changeset
|
2312 |
mySize ~~ (aString size) ifTrue:[^ false]. |
608 | 2313 |
|
2314 |
1 to:mySize do:[:index | |
|
20874 | 2315 |
(self at:index) = (aString at:index) ifFalse:[^ false]. |
608 | 2316 |
]. |
2317 |
^ true |
|
2318 |
||
2319 |
" |
|
8874 | 2320 |
'foo' = 'Foo' |
2321 |
'foo' = 'bar' |
|
2322 |
'foo' = 'foo' |
|
2323 |
'foo' = 'foo' asText |
|
2324 |
'foo' asText = 'foo' |
|
2325 |
'foo' asText = 'foo' asText |
|
608 | 2326 |
" |
1252 | 2327 |
|
2328 |
"Modified: 22.4.1996 / 15:53:58 / cg" |
|
608 | 2329 |
! |
9192 | 2330 |
|
9191 | 2331 |
> aString |
608 | 2332 |
"Compare the receiver with the argument and return true if the |
2333 |
receiver is greater than the argument. Otherwise return false. |
|
8874 | 2334 |
This comparison is based on the elements ascii code - |
1252 | 2335 |
i.e. upper/lowercase & upper/lowercase & national characters are NOT treated specially." |
608 | 2336 |
|
2337 |
|mySize "{ Class: SmallInteger }" |
|
8874 | 2338 |
otherSize "{ Class: SmallInteger }" |
9192 | 2339 |
n "{ Class: SmallInteger }" |
2340 |
c1 c2| |
|
2341 |
||
2342 |
mySize := self size. |
|
5929 | 2343 |
otherSize := aString string size. |
2344 |
n := mySize min:otherSize. |
|
2345 |
||
2346 |
1 to:n do:[:index | |
|
20874 | 2347 |
c1 := self at:index. |
2348 |
c2 := aString at:index. |
|
2349 |
(c1 == c2 or:[c1 = c2]) ifFalse:[^ c1 > c2]. |
|
5929 | 2350 |
]. |
9192 | 2351 |
^ mySize > otherSize |
2352 |
||
2353 |
"Modified: 22.4.1996 / 15:55:00 / cg" |
|
2354 |
! |
|
2355 |
||
14660 | 2356 |
after:aString |
23155 | 2357 |
<resource: #obsolete> |
14660 | 2358 |
"Compare the receiver with the argument and return true if the |
2359 |
receiver should come after the argument in a sorted list. |
|
2360 |
Otherwise return false. |
|
2361 |
NOTE: The comparison should be language specific, depending on the value of |
|
20874 | 2362 |
LC_COLLATE, which is initialized from the environment. |
2363 |
||
2364 |
Currently it is for Strings, but not for UnicodeStrings... |
|
14660 | 2365 |
|
2366 |
STUPID: |
|
23155 | 2367 |
#after has a completely different meaning in SeqColl... |
2368 |
... therefore it is marked as obsolete. |
|
2369 |
" |
|
14660 | 2370 |
|
2371 |
^ (self compareCollatingWith:aString) > 0 |
|
23155 | 2372 |
|
2373 |
"Modified (comment): / 29-06-2018 / 11:41:33 / Claus Gittinger" |
|
14660 | 2374 |
! |
2375 |
||
24232 | 2376 |
caselessAfter:aString |
2377 |
"True if the receiver comes after aString, if compared caseless. |
|
2378 |
(i.e. if receiver > aString, ignoring case)" |
|
2379 |
||
2380 |
^ (self compareCaselessWith:aString) > 0 |
|
2381 |
||
2382 |
" |
|
2383 |
'aaa1' > 'aaA2' -> true |
|
2384 |
'aaa1' caselessAfter: 'aaA2' -> false |
|
2385 |
" |
|
2386 |
||
2387 |
"Created: / 29-05-2019 / 10:09:55 / Claus Gittinger" |
|
2388 |
! |
|
2389 |
||
2390 |
caselessBefore:aString |
|
2391 |
"True if the receiver comes before aString, if compared caseless. |
|
2392 |
(i.e. if receiver < aString, ignoring case)" |
|
2393 |
||
2394 |
^ (self compareCaselessWith:aString) < 0 |
|
2395 |
||
2396 |
" |
|
2397 |
'aaa1' < 'aaA2' -> false |
|
2398 |
'aaa1' caselessBefore: 'aaA2' -> true |
|
2399 |
" |
|
2400 |
||
2401 |
"Created: / 29-05-2019 / 10:09:18 / Claus Gittinger" |
|
2402 |
! |
|
2403 |
||
2404 |
caselessEqual:aString |
|
2405 |
"True if the receiver has the same characters as aString, if compared caseless. |
|
2406 |
(i.e. if receiver = aString, ignoring case)" |
|
2407 |
||
2408 |
^ (self sameAs:aString) |
|
2409 |
||
2410 |
" |
|
2411 |
'aaa1' = 'aaA1' -> false |
|
2412 |
'aaa1' caselessEqual: 'aaA1' -> true |
|
2413 |
" |
|
2414 |
||
2415 |
"Created: / 29-05-2019 / 10:10:53 / Claus Gittinger" |
|
2416 |
! |
|
2417 |
||
15814 | 2418 |
compareAsVersionNumberWith:aStringOrCollection |
2419 |
"Compare the receiver with the argument and return 1 if the receiver is |
|
2420 |
greater, 0 if equal and -1 if less than the argument in a sorted list. |
|
2421 |
Compare as version numbers in the form a.b.c... ." |
|
2422 |
||
23121 | 2423 |
|
2424 |
^ self asVersionNumberCollection |
|
2425 |
compareWith:aStringOrCollection asVersionNumberCollection |
|
15814 | 2426 |
|
2427 |
" |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2428 |
self assert:('1' compareAsVersionNumberWith:'2') < 0. |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2429 |
self assert:('2' compareAsVersionNumberWith:'1') > 0. |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2430 |
self assert:('1.1' compareAsVersionNumberWith:'2.1.2') < 0. |
23121 | 2431 |
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
|
2432 |
self assert:('2.1' compareAsVersionNumberWith:'1.2.3') > 0. |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2433 |
self assert:('1' compareAsVersionNumberWith:'1.1') < 0. |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2434 |
self assert:('1.1' compareAsVersionNumberWith:'1') > 0. |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2435 |
self assert:('1.1' compareAsVersionNumberWith:'1.2') < 0. |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2436 |
self assert:('1.10' compareAsVersionNumberWith:'1.2') > 0. |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2437 |
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
|
2438 |
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
|
2439 |
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
|
2440 |
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
|
2441 |
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
|
2442 |
self assert:('1.2.3.4' compareAsVersionNumberWith:#(1 2 3 4)) = 0. |
22325 | 2443 |
self assert:('1.2.3.4' compareAsVersionNumberWith:#('1' 2 3 4)) = 0. |
23121 | 2444 |
|
2445 |
self assert:('1.1' compareAsVersionNumberWith:'1.1a') < 0. |
|
2446 |
||
22325 | 2447 |
" |
2448 |
||
2449 |
"Modified: / 25-10-2017 / 11:25:08 / stefan" |
|
23121 | 2450 |
"Modified (format): / 20-06-2018 / 19:23:18 / Stefan Vogel" |
15814 | 2451 |
! |
2452 |
||
9192 | 2453 |
compareCaselessWith:aString |
22327 | 2454 |
"Compare the receiver against the argument, ignoring case. |
9190 | 2455 |
Return 1 if the receiver is greater, 0 if equal and -1 if less than the argument. |
2456 |
||
8874 | 2457 |
This comparison is based on the elements ascii code - |
9190 | 2458 |
i.e. national characters are NOT treated specially. |
2459 |
'foo' compareWith: 'Foo' will return 0" |
|
608 | 2460 |
|
2461 |
|mySize "{ Class: SmallInteger }" |
|
8874 | 2462 |
otherSize "{ Class: SmallInteger }" |
9192 | 2463 |
n "{ Class: SmallInteger }" |
608 | 2464 |
c1 c2| |
2465 |
||
2466 |
mySize := self size. |
|
5739
4fd3be7ccc75
oops some relational ops did accept a non-string arg
Claus Gittinger <cg@exept.de>
parents:
5732
diff
changeset
|
2467 |
otherSize := aString string size. |
608 | 2468 |
n := mySize min:otherSize. |
2469 |
||
2470 |
1 to:n do:[:index | |
|
20874 | 2471 |
c1 := (self at:index) asLowercase. |
2472 |
c2 := (aString at:index) asLowercase. |
|
2473 |
c1 > c2 ifTrue:[^ 1]. |
|
2474 |
c1 < c2 ifTrue:[^ -1]. |
|
608 | 2475 |
]. |
2476 |
mySize > otherSize ifTrue:[^ 1]. |
|
2477 |
mySize < otherSize ifTrue:[^ -1]. |
|
2478 |
^ 0 |
|
1252 | 2479 |
|
24232 | 2480 |
" |
2481 |
'aaa1' < 'aaA2' -> false |
|
2482 |
'aaa1' compareCaselessWith: 'aaA2' -> -1 |
|
2483 |
" |
|
2484 |
||
22327 | 2485 |
"Modified: / 22-04-1996 / 15:56:07 / cg" |
2486 |
"Modified (comment): / 26-10-2017 / 16:01:01 / mawalch" |
|
24232 | 2487 |
"Modified (comment): / 29-05-2019 / 10:06:00 / Claus Gittinger" |
608 | 2488 |
! |
2489 |
||
14660 | 2490 |
compareCollatingWith:aString |
2491 |
"Compare the receiver with the argument and return 1 if the receiver is |
|
2492 |
greater, 0 if equal and -1 if less than the argument in a sorted list. |
|
2493 |
The comparison is language specific, depending on the value of |
|
2494 |
LC_COLLATE, which is in the shell environment." |
|
2495 |
||
2496 |
"TODO not yet defined for unicode" |
|
2497 |
<resource: #todo> |
|
2498 |
||
2499 |
|s| |
|
2500 |
||
2501 |
(s := self string) ~~ self ifTrue:[ |
|
20874 | 2502 |
^ s compareCollatingWith:aString |
14660 | 2503 |
]. |
2504 |
^ self compareWith:aString |
|
2505 |
! |
|
2506 |
||
9192 | 2507 |
compareWith:aString |
9190 | 2508 |
"Compare the receiver with the argument and return 1 if the receiver is |
2509 |
greater, 0 if equal and -1 if less than the argument. |
|
11861 | 2510 |
This comparison is based on the elements' codepoints - |
9190 | 2511 |
i.e. upper/lowercase & national characters are NOT treated specially. |
2512 |
'foo' compareWith: 'Foo' will return 1. |
|
2513 |
while 'foo' sameAs:'Foo' will return true" |
|
2514 |
||
15814 | 2515 |
|s| |
2516 |
||
2517 |
s := self string. |
|
2518 |
s ~~ self ifTrue:[ |
|
20874 | 2519 |
^ s compareWith:aString string. |
9192 | 2520 |
]. |
15814 | 2521 |
^ super compareWith:aString string. |
9192 | 2522 |
! |
2523 |
||
11861 | 2524 |
endsWith:aStringOrCharacter |
16175 | 2525 |
"return true, if the receiver ends with something, aStringOrCharacter. |
2526 |
If aStringOrCharacter is empty, true is returned" |
|
11861 | 2527 |
|
2528 |
|s| |
|
2529 |
||
2530 |
(s := self string) ~~ self ifTrue:[ |
|
20874 | 2531 |
^ s endsWith:aStringOrCharacter |
11861 | 2532 |
]. |
21083 | 2533 |
(self size ~~ 0 and:[aStringOrCharacter isCharacter]) ifTrue:[ |
20874 | 2534 |
^ self last = aStringOrCharacter |
11861 | 2535 |
]. |
2536 |
^ super endsWith:aStringOrCharacter |
|
2537 |
||
2538 |
" |
|
2539 |
'hello world' endsWith:'world' |
|
2540 |
'hello world' asText allBold endsWith:'world' |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2541 |
'hello world' endsWith:'' |
16175 | 2542 |
'hello world' asText allBold endsWith:'' |
11861 | 2543 |
" |
2544 |
||
2545 |
"Modified: 12.5.1996 / 15:49:18 / cg" |
|
2546 |
! |
|
2547 |
||
23154 | 2548 |
endsWith:aStringOrCharacter caseSensitive:caseSensitive |
2549 |
"return true, if the receiver ends with something, aStringOrCharacter. |
|
2550 |
If aStringOrCharacter is empty, true is returned" |
|
2551 |
||
2552 |
|s| |
|
2553 |
||
2554 |
caseSensitive ifTrue:[^ self endsWith:aStringOrCharacter]. |
|
2555 |
||
2556 |
(s := self string) ~~ self ifTrue:[ |
|
2557 |
^ s endsWith:aStringOrCharacter |
|
2558 |
]. |
|
2559 |
(self size ~~ 0 and:[aStringOrCharacter isCharacter]) ifTrue:[ |
|
2560 |
^ self last asLowercase = aStringOrCharacter asLowercase |
|
2561 |
]. |
|
2562 |
^ self endsWith:aStringOrCharacter using:[:a :b | a asLowercase = b asLowercase]. |
|
2563 |
||
2564 |
" |
|
2565 |
'hello World' endsWith:'world' caseSensitive:true |
|
2566 |
'hello World' endsWith:'world' caseSensitive:false |
|
2567 |
" |
|
2568 |
||
2569 |
"Created: / 29-06-2018 / 11:38:57 / Claus Gittinger" |
|
2570 |
! |
|
2571 |
||
23783 | 2572 |
endsWithDigit |
2573 |
"Answer whether the receiver's final character represents a digit. 3/11/96 sw" |
|
2574 |
||
2575 |
|len| |
|
2576 |
||
2577 |
^ (len := self size) ~~ 0 and:[(self at:len) isDigit] |
|
24300 | 2578 |
" |
2579 |
'hello' endsWithDigit |
|
2580 |
'12hello' endsWithDigit |
|
2581 |
'hello12' endsWithDigit |
|
2582 |
" |
|
2583 |
" |
|
2584 |
'hello' startsWithDigit |
|
2585 |
'12hello' startsWithDigit |
|
2586 |
'hello12' startsWithDigit |
|
2587 |
" |
|
2588 |
||
2589 |
"Modified (comment): / 08-06-2019 / 17:23:38 / Claus Gittinger" |
|
23783 | 2590 |
! |
2591 |
||
11838 | 2592 |
hammingDistanceTo:aString |
2593 |
"return the hamming distance (the number of characters which are different). |
|
12177 | 2594 |
In information theory, the Hamming distance between two strings of equal length |
2595 |
is the number of positions for which the corresponding symbols are different. |
|
2596 |
Put another way, it measures the minimum number of substitutions required to change |
|
11838 | 2597 |
one into the other, or the number of errors that transformed one string into the other." |
2598 |
||
22377 | 2599 |
|mySize| |
2600 |
||
2601 |
mySize := self size. |
|
2602 |
self assert:(aString size == mySize). |
|
2603 |
^ 1 to:mySize count:[:idx | (self at:idx) ~= (aString at:idx)] |
|
11838 | 2604 |
|
2605 |
" |
|
2606 |
'roses' hammingDistanceTo:'toned' |
|
22377 | 2607 |
'roses' hammingDistanceTo:'doses' |
2608 |
" |
|
2609 |
||
2610 |
"Modified (comment): / 24-11-2017 / 08:50:58 / cg" |
|
11838 | 2611 |
! |
2612 |
||
9192 | 2613 |
hash |
2614 |
"return an integer useful as a hash-key" |
|
2615 |
||
16898 | 2616 |
"/ whenever changing, also care for String>>hash. |
13879 | 2617 |
"/ immediately after any change, execute (maybe in a debugger): |
2618 |
"/ Set allSubInstancesDo:[:s | s rehash] |
|
16898 | 2619 |
^ self hash_fnv1a |
2620 |
||
2621 |
" |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2622 |
'a' hash |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2623 |
'a' asUnicode16String hash |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2624 |
'a' asUnicode32String hash |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2625 |
'aa' hash |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2626 |
'aa' asUnicode16String hash |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2627 |
'aa' asUnicode32String hash |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2628 |
'ab' hash |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2629 |
'ab' asUnicode16String hash |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2630 |
'ab' asUnicode32String hash |
13879 | 2631 |
'ab' hash |
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2632 |
'ab' asArray hash |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2633 |
" |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2634 |
|
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2635 |
" |
20874 | 2636 |
|syms ms| |
2637 |
||
2638 |
syms := Symbol allInstances. |
|
2639 |
Transcript show:'syms: '; showCR:syms size. |
|
2640 |
Transcript show:'sdbm hashes: '; showCR:(syms collect:[:s| s hash]) asSet size. |
|
2641 |
Transcript show:'dragonBook hashes: '; showCR:(syms collect:[:s| s hash_dragonBook]) asSet size. |
|
2642 |
||
2643 |
ms := Time millisecondsToRun:[ |
|
2644 |
10 timesRepeat:[ |
|
2645 |
syms do:[:each| each hash]. |
|
2646 |
]. |
|
2647 |
]. |
|
2648 |
Transcript show:'sdbm hash: '; showCR:ms. |
|
2649 |
||
2650 |
ms := Time millisecondsToRun:[ |
|
2651 |
10 timesRepeat:[ |
|
2652 |
syms do:[:each| each hash_dragonBook]. |
|
2653 |
]. |
|
2654 |
]. |
|
2655 |
Transcript show:'dragonBook: '; showCR:ms. |
|
2656 |
||
2657 |
syms := syms collect:[:each| each asUnicode16String]. |
|
2658 |
ms := Time millisecondsToRun:[ |
|
2659 |
10 timesRepeat:[ |
|
2660 |
syms do:[:each| each hash]. |
|
2661 |
]. |
|
2662 |
]. |
|
2663 |
Transcript show:'unicode sdbm hash: '; showCR:ms. |
|
2664 |
||
2665 |
ms := Time millisecondsToRun:[ |
|
2666 |
10 timesRepeat:[ |
|
2667 |
syms do:[:each| each hash_dragonBook]. |
|
2668 |
]. |
|
2669 |
]. |
|
2670 |
Transcript show:'unicode dragonBook:'; showCR:ms. |
|
14641 | 2671 |
" |
2672 |
||
13879 | 2673 |
"Modified: / 26-12-2011 / 14:09:07 / cg" |
2674 |
! |
|
2675 |
||
2676 |
hash_dragonBook |
|
2677 |
"return an integer useful as a hash-key" |
|
2678 |
||
10698
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2679 |
|h g| |
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2680 |
|
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2681 |
"/ |
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2682 |
"/ this is the dragon-book algorithm |
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2683 |
"/ |
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2684 |
h := 0. |
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2685 |
self reverseDo:[:char | |
10703
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
2686 |
"/ 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
|
2687 |
"/ h := (h bitShift:4) + char asciiValue. |
20874 | 2688 |
h := (h bitShift:4). |
2689 |
h := h + char codePoint. |
|
2690 |
h := h bitAnd:16rFFFFFFFF. |
|
2691 |
g := (h bitAnd: 16rF0000000). |
|
2692 |
g ~~ 0 ifTrue:[ |
|
2693 |
h := h bitXor:(g bitShift:-24). |
|
2694 |
h := h bitXor:g. |
|
2695 |
]. |
|
10698
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2696 |
]. |
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2697 |
"/ |
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2698 |
"/ multiply by large prime to spread values |
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2699 |
"/ This speeds up Set and Dictionary by a factor of 10!! |
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2700 |
"/ |
14137
1eb1b4a3f364
Inherit from UninterpretedBytes instead of ByteArray
Stefan Vogel <sv@exept.de>
parents:
14122
diff
changeset
|
2701 |
h := h times:31415821. |
10698
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2702 |
^ h |
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2703 |
|
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2704 |
" |
10703
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
2705 |
'a' hash |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
2706 |
'a' asUnicode16String hash |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
2707 |
'aa' hash |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
2708 |
'aa' asUnicode16String hash |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
2709 |
'ab' hash |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
2710 |
'ab' asUnicode16String hash |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
2711 |
'ab' hash |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
2712 |
'ab' asArray hash |
10698
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2713 |
" |
13879 | 2714 |
|
2715 |
"Created: / 26-12-2011 / 13:46:06 / cg" |
|
2716 |
! |
|
2717 |
||
16887 | 2718 |
hash_fnv1a |
2719 |
"return an integer useful as a hash-key. |
|
2720 |
This method uses the fnv-1a algorithm |
|
19743 | 2721 |
(which is actually a very good one). |
2722 |
Attention: stops when a 0-codepoint is encountered |
|
20874 | 2723 |
(for compatibility with the hash used by the VM) |
19743 | 2724 |
Also: on 64bit CPUs, only small 4-byte hashvalues are returned, |
20874 | 2725 |
(so hash values are independent from the architecture)" |
19743 | 2726 |
|
2727 |
|h byte| |
|
16887 | 2728 |
|
2729 |
h := 2166136261. |
|
2730 |
self do:[:eachChar | |
|
20874 | 2731 |
byte := eachChar codePoint. |
2732 |
byte == 0 ifTrue:[ |
|
2733 |
"/ stop |
|
2734 |
^ (h bitXor: (h >> 30)) bitAnd: 16r3FFFFFFF. |
|
2735 |
]. |
|
2736 |
h := h bitXor:byte. |
|
2737 |
h := (h * 16777619) bitAnd:16rFFFFFFFF. |
|
16887 | 2738 |
]. |
16898 | 2739 |
"/ make sure, it fits into a smallInt |
2740 |
h := (h bitXor: (h >> 30)) bitAnd: 16r3FFFFFFF. |
|
16887 | 2741 |
^ h |
2742 |
||
2743 |
" |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2744 |
'abc' hash_fnv1a |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2745 |
'abc' asUnicode16String hash_fnv1a |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2746 |
'abc' asUnicode32String hash_fnv1a |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2747 |
|
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2748 |
'foofooHelloWorld' hash_fnv1a |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2749 |
'foofooHelloWorld' asUnicode16String hash_fnv1a |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2750 |
'foofooHelloWorld' asUnicode32String hash_fnv1a |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2751 |
|
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2752 |
'blablaHelloWorld' hash_fnv1a |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2753 |
'blablaHelloWorld' asUnicode16String hash_fnv1a |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2754 |
'blablaHelloWorld' asUnicode32String hash_fnv1a |
16887 | 2755 |
" |
2756 |
! |
|
2757 |
||
23863 | 2758 |
hash_fnv1a_64 |
2759 |
"return a 64bit integer useful as a hash-key. |
|
2760 |
This method uses the fnv-1a algorithm |
|
2761 |
(which is actually a very good one)." |
|
2762 |
||
2763 |
|h byte| |
|
2764 |
||
2765 |
h := 14695981039346656037. |
|
2766 |
self do:[:eachChar | |
|
2767 |
byte := eachChar codePoint. |
|
2768 |
h := h bitXor:byte. |
|
2769 |
h := (h * 1099511628211) bitAnd:16rFFFFFFFFFFFFFFFF. |
|
2770 |
]. |
|
2771 |
"/ make sure, it fits into 64 bit |
|
2772 |
h := (h bitXor: (h >> 30)) bitAnd: 16r3FFFFFFFFFFFFFFF. |
|
2773 |
^ h |
|
2774 |
||
2775 |
" |
|
2776 |
'abc' hash_fnv1a_64 |
|
2777 |
'abc' asUnicode16String hash_fnv1a_64 |
|
2778 |
'abc' asUnicode32String hash_fnv1a_64 |
|
2779 |
||
2780 |
'foofooHelloWorld' hash_fnv1a_64 |
|
2781 |
'foofooHelloWorld' asUnicode16String hash_fnv1a_64 |
|
2782 |
'foofooHelloWorld' asUnicode32String hash_fnv1a_64 |
|
2783 |
||
2784 |
'blablaHelloWorld' hash_fnv1a_64 |
|
2785 |
'blablaHelloWorld' asUnicode16String hash_fnv1a_64 |
|
2786 |
'blablaHelloWorld' asUnicode32String hash_fnv1a_64 |
|
2787 |
" |
|
2788 |
||
2789 |
"Created: / 11-03-2019 / 11:47:15 / Claus Gittinger" |
|
2790 |
! |
|
2791 |
||
16887 | 2792 |
hash_java |
2793 |
"return an integer useful as a hash-key. |
|
2794 |
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
|
2795 |
the java virtual machine |
16887 | 2796 |
(which is actually not a very good one)." |
2797 |
||
2798 |
|h| |
|
2799 |
||
2800 |
h := 0. |
|
2801 |
self do:[:eachChar | |
|
20874 | 2802 |
h := (h * 31) + (eachChar codePoint). |
2803 |
h := h bitAnd:16rFFFFFFFF. |
|
16887 | 2804 |
]. |
2805 |
^ h |
|
2806 |
||
2807 |
" |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2808 |
'abc' hash_java |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2809 |
'foofooHelloWorld' hash_java |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2810 |
'blablaHelloWorld' hash_java |
16887 | 2811 |
" |
2812 |
! |
|
2813 |
||
16898 | 2814 |
hash_sdbm |
2815 |
"return an integer useful as a hash-key. |
|
2816 |
This method implements the sdbm algorithm." |
|
2817 |
||
2818 |
|h| |
|
2819 |
||
2820 |
"/ |
|
2821 |
"/ this is the sdbm algorithm |
|
2822 |
"/ |
|
2823 |
h := 0. |
|
2824 |
self do:[:char | |
|
20874 | 2825 |
h := (65599 times:h) plus:char codePoint. |
16898 | 2826 |
]. |
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2827 |
^ h |
16898 | 2828 |
|
2829 |
" |
|
2830 |
'a' hash |
|
2831 |
'a' asUnicode16String hash |
|
2832 |
'aa' hash |
|
2833 |
'aa' asUnicode16String hash |
|
2834 |
'ab' hash |
|
2835 |
'ab' asUnicode16String hash |
|
2836 |
'ab' hash |
|
2837 |
'ab' asArray hash |
|
2838 |
" |
|
2839 |
||
2840 |
" |
|
20874 | 2841 |
|syms ms| |
2842 |
||
2843 |
syms := Symbol allInstances. |
|
2844 |
Transcript show:'syms: '; showCR:syms size. |
|
2845 |
Transcript show:'sdbm hashes: '; showCR:(syms collect:[:s| s hash]) asSet size. |
|
2846 |
Transcript show:'dragonBook hashes: '; showCR:(syms collect:[:s| s hash_dragonBook]) asSet size. |
|
2847 |
||
2848 |
ms := Time millisecondsToRun:[ |
|
2849 |
10 timesRepeat:[ |
|
2850 |
syms do:[:each| each hash]. |
|
2851 |
]. |
|
2852 |
]. |
|
2853 |
Transcript show:'sdbm hash: '; showCR:ms. |
|
2854 |
||
2855 |
ms := Time millisecondsToRun:[ |
|
2856 |
10 timesRepeat:[ |
|
2857 |
syms do:[:each| each hash_dragonBook]. |
|
2858 |
]. |
|
2859 |
]. |
|
2860 |
Transcript show:'dragonBook: '; showCR:ms. |
|
2861 |
||
2862 |
syms := syms collect:[:each| each asUnicode16String]. |
|
2863 |
ms := Time millisecondsToRun:[ |
|
2864 |
10 timesRepeat:[ |
|
2865 |
syms do:[:each| each hash]. |
|
2866 |
]. |
|
2867 |
]. |
|
2868 |
Transcript show:'unicode sdbm hash: '; showCR:ms. |
|
2869 |
||
2870 |
ms := Time millisecondsToRun:[ |
|
2871 |
10 timesRepeat:[ |
|
2872 |
syms do:[:each| each hash_dragonBook]. |
|
2873 |
]. |
|
2874 |
]. |
|
2875 |
Transcript show:'unicode dragonBook:'; showCR:ms. |
|
16898 | 2876 |
" |
2877 |
||
2878 |
"Modified: / 26-12-2011 / 14:09:07 / cg" |
|
2879 |
! |
|
2880 |
||
11861 | 2881 |
levenshteinTo:aString |
2882 |
"return the levenshtein distance to the argument, aString; |
|
2883 |
this value corresponds to the number of replacements that have to be |
|
2884 |
made to get aString from the receiver. |
|
2885 |
See IEEE transactions on Computers 1976 Pg 172 ff." |
|
2886 |
||
2887 |
" |
|
21760 | 2888 |
in the following, we assume that omiting a character |
11861 | 2889 |
is less of an error than inserting an extra character. |
2890 |
Therefore the different insertion (i) and deletion (d) values. |
|
20874 | 2891 |
s: substitution weight (4) |
2892 |
k: keyboard weight (k) (typing a nearby key) - or nil (then use s) |
|
2893 |
c: case weight (4) - or nil (then use s) |
|
2894 |
e: exchange weight (8) - or nil (then use s*2) |
|
2895 |
i: insertion of extra character weight (2) |
|
2896 |
d: delete of a character weight (6) |
|
22157 | 2897 |
|
2898 |
Notice that the standard levenshtein uses the same weight for insertion and deletion, |
|
2899 |
and is computed by: |
|
2900 |
'flaw' levenshteinTo:'lawn' s:2 k:nil c:nil i:1 d:1 |
|
2901 |
||
11861 | 2902 |
" |
2903 |
||
2904 |
^ StringUtilities |
|
20874 | 2905 |
levenshteinDistanceFrom:self |
2906 |
to:aString |
|
2907 |
s:4 k:4 c:4 e:nil i:2 d:6 |
|
12177 | 2908 |
|
2909 |
" |
|
2910 |
'computer' levenshteinTo:'computer' |
|
11861 | 2911 |
'cOmputer' levenshteinTo:'computer' |
12177 | 2912 |
'cOmpuTer' levenshteinTo:'computer' |
11861 | 2913 |
'cimputer' levenshteinTo:'computer' |
2914 |
'cumputer' levenshteinTo:'computer' |
|
2915 |
||
2916 |
'cmputer' levenshteinTo:'computer' |
|
2917 |
'coomputer' levenshteinTo:'computer' |
|
2918 |
||
2919 |
'ocmprt' levenshteinTo:'computer' |
|
2920 |
'computer' levenshteinTo:'computer' |
|
2921 |
'ocmputer' levenshteinTo:'computer' |
|
2922 |
'cmputer' levenshteinTo:'computer' |
|
2923 |
'computer' levenshteinTo:'cmputer' |
|
2924 |
'Computer' levenshteinTo:'computer' |
|
2925 |
||
12177 | 2926 |
'compiter' levenshteinTo:'computer' |
2927 |
'compoter' levenshteinTo:'computer' |
|
2928 |
||
2929 |
'comptuer' levenshteinTo:'computer' |
|
11861 | 2930 |
" |
21760 | 2931 |
|
2932 |
"Modified (comment): / 17-05-2017 / 16:15:41 / mawalch" |
|
22157 | 2933 |
"Modified (comment): / 02-08-2017 / 17:13:04 / cg" |
11861 | 2934 |
! |
2935 |
||
2936 |
levenshteinTo:aString s:substWeight k:kbdTypoWeight c:caseWeight i:insrtWeight d:deleteWeight |
|
2937 |
"parametrized levenshtein. |
|
2938 |
return the levenshtein distance to the argument, aString; |
|
2939 |
this value corrensponds to the number of replacements that have to be |
|
2940 |
made to get aString from the receiver. |
|
2941 |
The arguments are the costs for |
|
20874 | 2942 |
s:substitution, |
2943 |
k:keyboard type (substitution), |
|
2944 |
c:case-change, |
|
2945 |
i:insertion |
|
2946 |
d:deletion |
|
11861 | 2947 |
of a character. |
2948 |
See IEEE transactions on Computers 1976 Pg 172 ff" |
|
2949 |
||
2950 |
^ StringUtilities |
|
20874 | 2951 |
levenshteinDistanceFrom:self |
2952 |
to:aString |
|
2953 |
s:substWeight k:kbdTypoWeight c:caseWeight e:nil i:insrtWeight d:deleteWeight |
|
11861 | 2954 |
! |
2955 |
||
9190 | 2956 |
sameAs:aString |
2957 |
"Compare the receiver with the argument like =, but ignore case differences. |
|
2958 |
Return true or false." |
|
2959 |
||
2960 |
|mySize "{ Class: SmallInteger }" |
|
2961 |
otherSize c1 c2| |
|
2962 |
||
9192 | 2963 |
self == aString ifTrue:[^ true]. |
2964 |
||
2965 |
mySize := self size. |
|
2966 |
otherSize := aString string size. |
|
18884 | 2967 |
mySize ~~ otherSize ifTrue:[^ false]. |
9192 | 2968 |
|
2969 |
1 to:mySize do:[:index | |
|
20874 | 2970 |
c1 := self at:index. |
2971 |
c2 := aString at:index. |
|
2972 |
c1 ~~ c2 ifTrue:[ |
|
2973 |
(c1 sameAs:c2) ifFalse:[^ false]. |
|
2974 |
] |
|
9192 | 2975 |
]. |
2976 |
^ true |
|
2977 |
||
608 | 2978 |
" |
8874 | 2979 |
'foo' sameAs: 'Foo' |
2980 |
'foo' sameAs: 'bar' |
|
2981 |
'foo' sameAs: 'foo' |
|
9192 | 2982 |
" |
2983 |
||
2984 |
"Modified: 22.4.1996 / 15:56:17 / cg" |
|
2985 |
! |
|
608 | 2986 |
|
15734 | 2987 |
sameAs:aString caseSensitive:caseSensitive |
2988 |
"Compare the receiver with the argument. |
|
2989 |
If caseSensitive is false, this is the same as #sameAs:, |
|
2990 |
if false, this is the same as #=." |
|
2991 |
||
2992 |
caseSensitive ifFalse:[ |
|
20874 | 2993 |
^ self sameAs:aString |
15734 | 2994 |
]. |
2995 |
^ self = aString |
|
2996 |
||
2997 |
" |
|
2998 |
'foo' sameAs:'Foo' caseSensitive:false |
|
2999 |
'foo' sameAs:'foo' caseSensitive:true |
|
3000 |
" |
|
3001 |
! |
|
3002 |
||
5242 | 3003 |
sameAs:aString ignoreCase:ignoreCase |
15734 | 3004 |
<resource: #obsolete> |
5242 | 3005 |
"Compare the receiver with the argument. |
3006 |
If ignoreCase is true, this is the same as #sameAs:, |
|
3007 |
if false, this is the same as #=." |
|
3008 |
||
3009 |
ignoreCase ifTrue:[ |
|
20874 | 3010 |
^ self sameAs:aString |
5242 | 3011 |
]. |
3012 |
^ self = aString |
|
3013 |
||
3014 |
" |
|
8874 | 3015 |
'foo' sameAs:'Foo' ignoreCase:false |
3016 |
'foo' sameAs:'foo' ignoreCase:true |
|
5242 | 3017 |
" |
3018 |
||
3019 |
! |
|
3020 |
||
608 | 3021 |
sameCharacters:aString |
9192 | 3022 |
"count & return the number of characters which are the same |
3023 |
(ignoring case and emphasis) in the receiver and the argument, aString." |
|
3024 |
||
3025 |
|n "{ Class: SmallInteger }" |
|
3026 |
c1 c2 cnt| |
|
3027 |
||
3028 |
n := self size. |
|
3029 |
n := n min:(aString string size). |
|
3030 |
||
3031 |
cnt := 0. |
|
3032 |
1 to:n do:[:index | |
|
20874 | 3033 |
c1 := self at:index. |
3034 |
c2 := aString at:index. |
|
3035 |
((c1 == c2) |
|
3036 |
or:[c1 asLowercase = c2 asLowercase]) ifTrue:[ |
|
3037 |
cnt := cnt + 1 |
|
3038 |
] |
|
9192 | 3039 |
]. |
3040 |
^ cnt |
|
3041 |
||
3042 |
" |
|
3043 |
'foobarbaz' sameCharacters: 'foo' |
|
8874 | 3044 |
'foobarbaz' sameCharacters: 'Foo' |
3045 |
'foobarbaz' sameCharacters: 'baz' |
|
608 | 3046 |
" |
5510
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
3047 |
! |
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
3048 |
|
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
3049 |
sameEmphasisAs:aStringOrText |
17215 | 3050 |
"compare the receiver's and the argument's emphasis" |
5510
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
3051 |
|
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
3052 |
^ self emphasis = aStringOrText emphasis |
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
3053 |
|
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
3054 |
" |
8874 | 3055 |
'hello' asText sameEmphasisAs: 'hello' |
3056 |
'hello' asText sameEmphasisAs: 'hello' asText |
|
3057 |
'hello' asText allBold sameEmphasisAs: 'hello' |
|
3058 |
'hello' asText allBold sameEmphasisAs: 'fooba' asText allBold |
|
9192 | 3059 |
'hello' asText allBold sameEmphasisAs: 'fooba' asText allItalic |
3060 |
" |
|
3061 |
! |
|
3062 |
||
9191 | 3063 |
sameStringAndEmphasisAs:aStringOrText |
5739
4fd3be7ccc75
oops some relational ops did accept a non-string arg
Claus Gittinger <cg@exept.de>
parents:
5732
diff
changeset
|
3064 |
"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
|
3065 |
|
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
3066 |
aStringOrText isString ifFalse:[^ false]. |
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
3067 |
(self string = aStringOrText string) ifFalse:[^ false]. |
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
3068 |
self hasChangeOfEmphasis = aStringOrText hasChangeOfEmphasis ifFalse:[^ false]. |
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
3069 |
^ self emphasis = aStringOrText emphasis |
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
3070 |
|
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
3071 |
" |
8874 | 3072 |
'hello' asText sameEmphasisAs: 'hello' |
3073 |
'hello' asText sameEmphasisAs: 'hello' asText |
|
3074 |
'hello' asText allBold sameEmphasisAs: 'hello' |
|
3075 |
'hello' asText allBold sameEmphasisAs: 'fooba' asText allBold |
|
3076 |
'hello' asText allBold sameEmphasisAs: 'fooba' asText allItalic |
|
3077 |
||
3078 |
'hello' sameEmphasisAs: 'hello' asText |
|
3079 |
'hello' sameEmphasisAs: 'hello' asText allBold |
|
3080 |
'hello' sameEmphasisAs: 'fooba' |
|
3081 |
'hello' sameEmphasisAs: 'fooba' asText |
|
3082 |
'hello' sameEmphasisAs: 'fooba' asText allBold |
|
3083 |
'hello' sameEmphasisAs: 'fooba' asText allItalic |
|
3084 |
||
3085 |
'hello' asText sameStringAndEmphasisAs: 'hello' |
|
3086 |
'hello' asText sameStringAndEmphasisAs: 'hello' asText |
|
3087 |
'hello' asText allBold sameStringAndEmphasisAs: 'hello' |
|
3088 |
'hello' asText allBold sameStringAndEmphasisAs: 'fooba' asText allBold |
|
3089 |
'hello' asText allBold sameStringAndEmphasisAs: 'fooba' asText allItalic |
|
3090 |
||
3091 |
'hello' sameStringAndEmphasisAs: 'hello' asText |
|
3092 |
'hello' sameStringAndEmphasisAs: 'hello' asText allBold |
|
3093 |
'hello' sameStringAndEmphasisAs: 'fooba' |
|
3094 |
'hello' sameStringAndEmphasisAs: 'fooba' asText |
|
3095 |
'hello' sameStringAndEmphasisAs: 'fooba' asText allBold |
|
3096 |
'hello' sameStringAndEmphasisAs: 'fooba' asText allItalic |
|
5510
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
3097 |
" |
11861 | 3098 |
! |
3099 |
||
3100 |
spellAgainst: aString |
|
3101 |
"return an integer between 0 and 100 indicating how similar |
|
3102 |
the argument is to the receiver. No case conversion is done. |
|
3103 |
This algorithm is much simpler (but also less exact) than the |
|
3104 |
levenshtein distance. Experiment which is better for your |
|
3105 |
application." |
|
3106 |
||
3107 |
| i1 "{ Class: SmallInteger }" |
|
3108 |
i2 "{ Class: SmallInteger }" |
|
3109 |
next1 "{ Class: SmallInteger }" |
|
3110 |
next2 "{ Class: SmallInteger }" |
|
3111 |
size1 "{ Class: SmallInteger }" |
|
3112 |
size2 "{ Class: SmallInteger }" |
|
3113 |
score "{ Class: SmallInteger }" |
|
3114 |
maxLen "{ Class: SmallInteger }" | |
|
3115 |
||
3116 |
size1 := self size. |
|
3117 |
size2 := aString size. |
|
3118 |
maxLen := size1 max:size2. |
|
3119 |
score := 0. |
|
3120 |
i1 := i2 := 1. |
|
3121 |
[i1 <= size1 and: [i2 <= size2]] whileTrue:[ |
|
20874 | 3122 |
next1 := i1 + 1. |
3123 |
next2 := i2 + 1. |
|
3124 |
(self at:i1) == (aString at:i2) ifTrue: [ |
|
3125 |
score := score+1. |
|
3126 |
i1 := next1. |
|
3127 |
i2 := next2 |
|
3128 |
] ifFalse: [ |
|
3129 |
(i2 < size2 and: [(self at:i1) == (aString at:next2)]) ifTrue: [ |
|
3130 |
i2 := next2 |
|
3131 |
] ifFalse: [ |
|
3132 |
(i1 < size1 and: [(self at:next1) == (aString at:i2)]) ifTrue: [ |
|
3133 |
i1 := next1 |
|
3134 |
] ifFalse: [ |
|
3135 |
i1 := next1. |
|
3136 |
i2 := next2 |
|
3137 |
] |
|
3138 |
] |
|
3139 |
] |
|
11861 | 3140 |
]. |
3141 |
||
3142 |
score == maxLen ifTrue: [^ 100]. |
|
3143 |
^ 100 * score // maxLen |
|
3144 |
||
3145 |
" |
|
3146 |
'Smalltalk' spellAgainst: 'Smalltlak' |
|
3147 |
'Smalltalk' spellAgainst: 'smalltlak' |
|
3148 |
'Smalltalk' spellAgainst: 'smalltalk' |
|
3149 |
'Smalltalk' spellAgainst: 'smalltlk' |
|
3150 |
'Smalltalk' spellAgainst: 'Smalltolk' |
|
3151 |
" |
|
3152 |
! |
|
3153 |
||
16553 | 3154 |
startsWith:aStringOrCharacter |
3155 |
"return true, if the receiver starts with something, aStringOrCharacter. |
|
17371 | 3156 |
If the argument is empty, true is returned. |
3157 |
Notice, that this is similar to, but slightly different from VW's and Squeak's beginsWith:, |
|
3158 |
which are both inconsistent w.r.t. an empty argument." |
|
11861 | 3159 |
|
3160 |
|s| |
|
3161 |
||
16553 | 3162 |
aStringOrCharacter isCharacter ifTrue:[ |
23548 | 3163 |
^ (self size ~~ 0) and:[(self at:1) = aStringOrCharacter] |
16553 | 3164 |
]. |
11861 | 3165 |
(s := self string) ~~ self ifTrue:[ |
20874 | 3166 |
^ s startsWith:aStringOrCharacter |
11861 | 3167 |
]. |
16553 | 3168 |
^ super startsWith:aStringOrCharacter |
11861 | 3169 |
|
3170 |
" |
|
17364 | 3171 |
'abcde' startsWith:#($a $b $c) |
3172 |
'abcde' startsWith:'abc' |
|
3173 |
'abcd' startsWith:'abcde' |
|
3174 |
'abcde' startsWith:'abd' |
|
11861 | 3175 |
'hello world' startsWith:'hello' |
3176 |
'hello world' asText allBold startsWith:'hello' |
|
3177 |
'hello world' asText allBold startsWith:'' |
|
3178 |
" |
|
3179 |
||
23154 | 3180 |
"Created: / 12-05-1996 / 15:46:40 / cg" |
3181 |
"Modified: / 29-06-2018 / 11:27:08 / Claus Gittinger" |
|
23548 | 3182 |
"Modified (format): / 26-11-2018 / 14:15:31 / Stefan Vogel" |
23154 | 3183 |
! |
3184 |
||
3185 |
startsWith:aStringOrCharacter caseSensitive:caseSensitive |
|
3186 |
"return true, if the receiver starts with something, aStringOrCharacter. |
|
3187 |
If the argument is empty, true is returned. |
|
3188 |
Notice, that this is similar to, but slightly different from VW's and Squeak's beginsWith:, |
|
3189 |
which are both inconsistent w.r.t. an empty argument." |
|
3190 |
||
3191 |
|s| |
|
3192 |
||
23548 | 3193 |
caseSensitive ifTrue:[ |
3194 |
^ self startsWith:aStringOrCharacter |
|
3195 |
]. |
|
23154 | 3196 |
aStringOrCharacter isCharacter ifTrue:[ |
23548 | 3197 |
self size == 0 ifTrue:[ |
3198 |
^ false. |
|
3199 |
]. |
|
3200 |
s := self at:1. |
|
3201 |
^ s == aStringOrCharacter or:[s asLowercase = aStringOrCharacter asLowercase] |
|
23154 | 3202 |
]. |
3203 |
(s := self string) ~~ self ifTrue:[ |
|
3204 |
^ s startsWith:aStringOrCharacter caseSensitive:caseSensitive |
|
3205 |
]. |
|
3206 |
^ self startsWith:aStringOrCharacter using:[:a :b | a asLowercase = b asLowercase]. |
|
3207 |
||
3208 |
" |
|
3209 |
'aBCde' startsWith:'abc' caseSensitive:true |
|
3210 |
'aBCde' startsWith:'abc' caseSensitive:false |
|
3211 |
" |
|
3212 |
||
3213 |
"Created: / 29-06-2018 / 11:27:04 / Claus Gittinger" |
|
23548 | 3214 |
"Modified: / 26-11-2018 / 14:18:43 / Stefan Vogel" |
24300 | 3215 |
! |
3216 |
||
3217 |
startsWithDigit |
|
3218 |
"Answer whether the receiver's first character represents a digit. 3/11/96 sw" |
|
3219 |
||
3220 |
|len| |
|
3221 |
||
3222 |
^ (len := self size) ~~ 0 and:[(self at:1) isDigit] |
|
3223 |
||
3224 |
" |
|
3225 |
'hello' startsWithDigit |
|
3226 |
'12hello' startsWithDigit |
|
3227 |
'hello12' startsWithDigit |
|
3228 |
" |
|
3229 |
" |
|
3230 |
'hello' endsWithDigit |
|
3231 |
'12hello' endsWithDigit |
|
3232 |
'hello12' endsWithDigit |
|
3233 |
" |
|
3234 |
||
3235 |
"Created: / 08-06-2019 / 17:22:13 / Claus Gittinger" |
|
9192 | 3236 |
! ! |
3237 |
||
3238 |
!CharacterArray methodsFor:'converting'! |
|
3239 |
||
11881 | 3240 |
asAsciiZ |
3241 |
"if the receiver does not end with a 0-valued character, return a copy of it, |
|
3242 |
with an additional 0-character. Otherwise return the receiver. This is sometimes |
|
3243 |
needed when a string has to be passed to C, which needs 0-terminated strings. |
|
3244 |
Notice, that all singleByte strings are already 0-terminated in ST/X, whereas wide |
|
3245 |
strings are not." |
|
3246 |
||
12658 | 3247 |
(self notEmpty and:[self last codePoint == 0]) ifTrue:[^ self ]. |
11881 | 3248 |
^ self copyWith:(Character value:0). |
3249 |
||
3250 |
" |
|
3251 |
'abc' asAsciiZ |
|
3252 |
'abc' asWideString asAsciiZ |
|
3253 |
" |
|
3254 |
! |
|
3255 |
||
13524 | 3256 |
asByteArray |
15936 | 3257 |
"depending on the size of the characters in the receiver, |
3258 |
return a byteArray containing single-, double- or even 4-bytewise values. |
|
3259 |
The size of the returned byteArray will be the strings size multiplied by the |
|
3260 |
size required for the largest character. |
|
19233 | 3261 |
Attention: The bytes are in native byte order. |
21745 | 3262 |
Caveat: better use utf8Encoded, to get reproducible results" |
15936 | 3263 |
|
14555 | 3264 |
|bytes sz bytesPerCharacter idx str| |
3265 |
||
3266 |
str := self string. |
|
3267 |
str ~~ self ifTrue:[ |
|
20874 | 3268 |
"/ for text and other wrappers |
3269 |
^ str asByteArray |
|
13611
ba8c7372218a
variable renamed in: #inspector2TabBytes
Claus Gittinger <cg@exept.de>
parents:
13524
diff
changeset
|
3270 |
]. |
ba8c7372218a
variable renamed in: #inspector2TabBytes
Claus Gittinger <cg@exept.de>
parents:
13524
diff
changeset
|
3271 |
|
ba8c7372218a
variable renamed in: #inspector2TabBytes
Claus Gittinger <cg@exept.de>
parents:
13524
diff
changeset
|
3272 |
"/ for real strings, a fallback |
13524 | 3273 |
sz := self size. |
19757 | 3274 |
bytesPerCharacter := self bytesPerCharacter. |
13524 | 3275 |
bytes := ByteArray new:(sz * bytesPerCharacter). |
3276 |
idx := 1. |
|
3277 |
self do:[:char | |
|
20874 | 3278 |
|code| |
3279 |
||
3280 |
code := char codePoint. |
|
3281 |
bytesPerCharacter == 2 ifTrue:[ |
|
3282 |
bytes unsignedInt16At:idx put:code |
|
3283 |
] ifFalse:[ |
|
3284 |
bytesPerCharacter == 4 ifTrue:[ |
|
3285 |
bytes unsignedInt32At:idx put:code |
|
3286 |
] ifFalse:[ |
|
3287 |
bytes at:idx put:code |
|
3288 |
]. |
|
3289 |
]. |
|
3290 |
idx := idx + bytesPerCharacter. |
|
13524 | 3291 |
]. |
3292 |
^ bytes |
|
3293 |
||
3294 |
"Created: / 27-07-2011 / 00:56:17 / cg" |
|
21745 | 3295 |
"Modified (comment): / 11-05-2017 / 09:21:57 / mawalch" |
13524 | 3296 |
! |
3297 |
||
19233 | 3298 |
asByteArrayMSB:msb |
3299 |
"depending on the size of the characters in the receiver, |
|
3300 |
return a byteArray containing single-, double- or even 4-bytewise values. |
|
3301 |
The size of the returned byteArray will be the strings size multiplied by the |
|
3302 |
size required for the largest character. |
|
21745 | 3303 |
Caveat: better use utf8Encoded, to get reproducible results" |
19233 | 3304 |
|
3305 |
|ba| |
|
3306 |
||
3307 |
ba := self asByteArray. "/ native order |
|
3308 |
UninterpretedBytes isBigEndian ~~ msb ifTrue:[ |
|
20874 | 3309 |
ba swapBytes |
19233 | 3310 |
]. |
3311 |
^ ba |
|
21745 | 3312 |
|
3313 |
"Modified (comment): / 11-05-2017 / 09:22:01 / mawalch" |
|
19233 | 3314 |
! |
3315 |
||
20480 | 3316 |
asCanonicalizedFilename |
3317 |
"return a Filename with pathname taken from the receiver. |
|
3318 |
The filename is canonicalized, meaning that it cares for trailing directory separators, |
|
3319 |
'.' components etc." |
|
3320 |
||
3321 |
^ self asFilename asCanonicalizedFilename |
|
3322 |
||
3323 |
"on windows: |
|
3324 |
'c:\foo\bar' asFilename |
|
3325 |
'c:\foo\bar\' asFilename |
|
3326 |
'c:\foo\bar\..\baz' asFilename |
|
3327 |
'c:\foo\bar\..\baz\.' asFilename |
|
3328 |
'c:\foo\bar' asCanonicalizedFilename |
|
3329 |
'c:\foo\bar\' asCanonicalizedFilename |
|
3330 |
'c:\foo\bar\..\baz' asCanonicalizedFilename |
|
3331 |
'c:\foo\bar\..\baz\.' asCanonicalizedFilename |
|
3332 |
||
3333 |
on unix: |
|
3334 |
'/foo/bar' asFilename |
|
3335 |
'/foo/bar/' asFilename |
|
3336 |
'/foo/bar/../baz' asFilename |
|
3337 |
'/foo/bar/../baz/.' asFilename |
|
3338 |
'/foo/bar' asCanonicalizedFilename |
|
3339 |
'/foo/bar/' asCanonicalizedFilename |
|
3340 |
'/foo/bar/../baz' asCanonicalizedFilename |
|
3341 |
'/foo/bar/../baz/.' asCanonicalizedFilename |
|
3342 |
" |
|
3343 |
! |
|
3344 |
||
608 | 3345 |
asCollectionOfLines |
23187 | 3346 |
"return a collection containing the lines (separated by cr) of the receiver. |
3347 |
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
|
3348 |
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
|
3349 |
See also #asCollectionOfLinesWithReturn |
d7c3e67647f1
comment/format in: #asCollectionOfLines
Claus Gittinger <cg@exept.de>
parents:
14455
diff
changeset
|
3350 |
(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
|
3351 |
this method and we did not want to risk any incompatibilities)" |
14455
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3352 |
|
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3353 |
^ self asCollectionOfSubstringsSeparatedBy:Character cr. |
608 | 3354 |
|
3355 |
" |
|
3356 |
'1 one\2 two\3 three\4 four\5 five' withCRs asCollectionOfLines |
|
8874 | 3357 |
'1 one\\\\2 two\3 three' withCRs asCollectionOfLines |
20182 | 3358 |
|
3359 |
('foo \r\nbar\nbaz\t\r\n\r\nbla' printf:#()) |
|
3360 |
asCollectionOfLines collect:[:l | (l endsWith:Character return) ifTrue:[l copyButLast:1] ifFalse:l] |
|
608 | 3361 |
" |
23187 | 3362 |
|
3363 |
"Modified (comment): / 03-07-2018 / 10:59:36 / Claus Gittinger" |
|
608 | 3364 |
! |
3365 |
||
14455
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3366 |
asCollectionOfLinesWithReturn |
23187 | 3367 |
"return a collection containing the lines (separated by cr) of the receiver. |
3368 |
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
|
3369 |
|
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3370 |
|lines| |
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3371 |
|
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3372 |
lines := self asCollectionOfSubstringsSeparatedBy:Character cr. |
18194 | 3373 |
(lines notEmpty and:[lines last isEmpty]) ifTrue:[ |
20874 | 3374 |
^ lines copyButLast:1 |
14455
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3375 |
]. |
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3376 |
^ lines |
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3377 |
|
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3378 |
" |
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3379 |
'1\2\3' withCRs asCollectionOfLines |
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3380 |
'1\2\3\' withCRs asCollectionOfLines |
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3381 |
'1\2\3' withCRs asCollectionOfLinesWithReturn |
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3382 |
'1\2\3\' withCRs asCollectionOfLinesWithReturn |
18194 | 3383 |
'' withCRs asCollectionOfLinesWithReturn |
14455
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3384 |
" |
23187 | 3385 |
|
3386 |
"Modified (comment): / 03-07-2018 / 10:59:47 / Claus Gittinger" |
|
14455
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3387 |
! |
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3388 |
|
608 | 3389 |
asCollectionOfSubstringsSeparatedBy:aCharacter |
23187 | 3390 |
"return a collection containing substrings (separated by aCharacter) of the receiver. |
15740 | 3391 |
If aCharacter occurs multiple times in a row, the result will contain empty strings. |
23717 | 3392 |
If the receiver starts with aCharacter, an empty string with be the first result element. |
3393 |
If the receiver ends with aCharacter, NO empty string with be the last result element." |
|
608 | 3394 |
|
9192 | 3395 |
^ self asCollectionOfSubCollectionsSeparatedBy:aCharacter |
3396 |
||
3397 |
" |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
3398 |
'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
|
3399 |
'1 one:2 two:3 three:4 four:5 five:' asCollectionOfSubstringsSeparatedBy:$: |
15740 | 3400 |
'1 one 2 two 3 three 4 four 5 five' asCollectionOfSubstringsSeparatedBy:Character space |
9192 | 3401 |
" |
23187 | 3402 |
|
23717 | 3403 |
"Modified (comment): / 11-02-2019 / 23:54:20 / Claus Gittinger" |
9192 | 3404 |
! |
608 | 3405 |
|
11828 | 3406 |
asCollectionOfSubstringsSeparatedBy:aCharacter exceptIn:ch |
23187 | 3407 |
"return a collection containing the substrings (separated by aCharacter) of the receiver. |
3408 |
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
|
3409 |
The separation is not done, inside a matching pair of ch-substrings. |
15740 | 3410 |
Can be used to tokenize csv-like strings, which may or may not be enclosed in quotes." |
11828 | 3411 |
|
3412 |
|lines myClass except i c |
|
3413 |
startIndex "{ Class:SmallInteger }" |
|
3414 |
stopIndex "{ Class:SmallInteger }" | |
|
3415 |
||
11905
cbc2191cfe2d
return stringCollections from asCollectionOfXXX
Claus Gittinger <cg@exept.de>
parents:
11904
diff
changeset
|
3416 |
lines := StringCollection new. |
11828 | 3417 |
myClass := self species. |
3418 |
||
3419 |
startIndex := 1. |
|
3420 |
except := false. |
|
3421 |
[ |
|
20874 | 3422 |
i := startIndex-1. |
3423 |
[ |
|
3424 |
i := i+1. |
|
3425 |
c := self at:i. |
|
3426 |
c = ch ifTrue:[ except := except not. ]. |
|
3427 |
i < self size and:[except or:[c ~= aCharacter]] |
|
3428 |
] whileTrue. |
|
3429 |
||
3430 |
c = aCharacter ifTrue:[ |
|
3431 |
stopIndex := i -1. |
|
3432 |
] ifFalse: [ |
|
3433 |
stopIndex := i. |
|
3434 |
]. |
|
3435 |
(stopIndex < startIndex) ifTrue: [ |
|
3436 |
lines add:(myClass new:0) |
|
3437 |
] ifFalse: [ |
|
3438 |
lines add:(self copyFrom:startIndex to:stopIndex) |
|
3439 |
]. |
|
3440 |
startIndex := stopIndex + 2. |
|
3441 |
startIndex <= self size |
|
11828 | 3442 |
] whileTrue. |
3443 |
^ lines |
|
3444 |
||
3445 |
" |
|
12177 | 3446 |
'asd''f;d''dd;s' asCollectionOfSubstringsSeparatedBy:$; exceptIn:$' |
3447 |
" |
|
3448 |
"/ 'asd "hello bla" foo "bla bla" bar' asCollectionOfSubstringsSeparatedBy:$ exceptIn:$" |
|
23187 | 3449 |
|
3450 |
"Modified (comment): / 03-07-2018 / 11:00:02 / Claus Gittinger" |
|
11828 | 3451 |
! |
3452 |
||
13344
ec6860c49990
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13343
diff
changeset
|
3453 |
asCollectionOfSubstringsSeparatedBy:aFieldSeparatorString textSeparator:aTextSeparatorOrNil |
13343
3f058ad0d2f1
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13332
diff
changeset
|
3454 |
"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
|
3455 |
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
|
3456 |
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
|
3457 |
|
13344
ec6860c49990
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13343
diff
changeset
|
3458 |
|aTextSeparatorChar items scanningWord inStream element lastIsFieldSeparator sz| |
ec6860c49990
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13343
diff
changeset
|
3459 |
|
ec6860c49990
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13343
diff
changeset
|
3460 |
aTextSeparatorOrNil isNil ifTrue:[ |
19961 | 3461 |
^ self asCollectionOfSubstringsSeparatedByAll: aFieldSeparatorString |
13327
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3462 |
]. |
13344
ec6860c49990
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13343
diff
changeset
|
3463 |
sz := aTextSeparatorOrNil size. |
ec6860c49990
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13343
diff
changeset
|
3464 |
sz = 0 ifTrue:[ |
19961 | 3465 |
aTextSeparatorChar := aTextSeparatorOrNil |
13344
ec6860c49990
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13343
diff
changeset
|
3466 |
] ifFalse:[sz = 1 ifTrue:[ |
19961 | 3467 |
"this is a String. Fetch the first character - compatibility to older expecco libs" |
3468 |
aTextSeparatorChar := aTextSeparatorOrNil first. |
|
13344
ec6860c49990
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13343
diff
changeset
|
3469 |
] ifFalse:[ |
19961 | 3470 |
self error:'textSeparatorSize > 1'. |
13344
ec6860c49990
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13343
diff
changeset
|
3471 |
]]. |
13327
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3472 |
|
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3473 |
items := OrderedCollection new. |
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3474 |
|
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3475 |
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
|
3476 |
[ |
19961 | 3477 |
inStream skipSeparators. |
3478 |
inStream atEnd |
|
13327
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3479 |
] whileFalse:[ |
19961 | 3480 |
lastIsFieldSeparator := false. |
3481 |
inStream peek == aTextSeparatorChar ifTrue:[ |
|
3482 |
inStream next. |
|
3483 |
element := ''. |
|
3484 |
scanningWord := true. |
|
3485 |
[ scanningWord and:[inStream atEnd not] ] whileTrue:[ |
|
3486 |
element := element , (inStream upTo:aTextSeparatorChar). |
|
3487 |
(inStream peek == aTextSeparatorChar) ifTrue:[ |
|
3488 |
element := element , aTextSeparatorChar . |
|
3489 |
inStream next. |
|
3490 |
] ifFalse:[ |
|
3491 |
scanningWord := false. |
|
3492 |
]. |
|
3493 |
]. |
|
3494 |
inStream upToAll_positionBefore:aFieldSeparatorString. |
|
3495 |
] ifFalse:[ |
|
3496 |
element := inStream upToAll_positionBefore:aFieldSeparatorString |
|
3497 |
]. |
|
3498 |
items add:element. |
|
3499 |
lastIsFieldSeparator := (inStream skipThroughAll:aFieldSeparatorString) notNil. |
|
13343
3f058ad0d2f1
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13332
diff
changeset
|
3500 |
]. |
3f058ad0d2f1
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13332
diff
changeset
|
3501 |
lastIsFieldSeparator ifTrue:[ |
19961 | 3502 |
"empty element at the end of the line" |
3503 |
items add:''. |
|
13327
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3504 |
]. |
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3505 |
|
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3506 |
^ items |
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3507 |
|
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3508 |
" |
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3509 |
self assert:(('#First#, #Second,SecondAdd#, #Third#' asCollectionOfSubstringsSeparatedBy:',' textSeparator: $#) |
19961 | 3510 |
sameContentsAs:#('First' 'Second,SecondAdd' 'Third')). |
13327
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3511 |
self assert:(('#Fir##st#, #Second,SecondAdd#, #Third#' asCollectionOfSubstringsSeparatedBy:',' textSeparator: $#) |
19961 | 3512 |
sameContentsAs:#('Fir#st' 'Second,SecondAdd' 'Third')). |
13327
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3513 |
self assert:(('#Fir##st#, Second,SecondAdd, #Third#' asCollectionOfSubstringsSeparatedBy:',' textSeparator: $#) |
19961 | 3514 |
sameContentsAs:#('Fir#st' 'Second' 'SecondAdd' 'Third')). |
13343
3f058ad0d2f1
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13332
diff
changeset
|
3515 |
self assert:(('First,Second,Third,,' asCollectionOfSubstringsSeparatedBy:',' textSeparator:nil) |
19961 | 3516 |
sameContentsAs:#('First' 'Second' 'Third' '' '')). |
13344
ec6860c49990
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13343
diff
changeset
|
3517 |
self assert:(('First,Second,Third,,' asCollectionOfSubstringsSeparatedBy:',' textSeparator:'#') |
19961 | 3518 |
sameContentsAs:#('First' 'Second' 'Third' '' '')). |
13327
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3519 |
" |
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3520 |
|
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3521 |
"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
|
3522 |
! |
999589445aa7
add new method for splitting a csv string into csv elements
ab
parents:
10437
diff
changeset
|
3523 |
|
7339
20969b5770c9
asCollectionOfSubstringsSeparatedByAll:
Claus Gittinger <cg@exept.de>
parents:
7318
diff
changeset
|
3524 |
asCollectionOfSubstringsSeparatedByAll:aSeparatorString |
23187 | 3525 |
"return a collection containing the lines (separated by aSeparatorString) of the receiver. |
3526 |
If aSeparatorString occurs multiple times in a row, |
|
7339
20969b5770c9
asCollectionOfSubstringsSeparatedByAll:
Claus Gittinger <cg@exept.de>
parents:
7318
diff
changeset
|
3527 |
the result will contain empty strings." |
20969b5770c9
asCollectionOfSubstringsSeparatedByAll:
Claus Gittinger <cg@exept.de>
parents:
7318
diff
changeset
|
3528 |
|
8852
9f5984eff2c2
use asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8794
diff
changeset
|
3529 |
^ self asCollectionOfSubCollectionsSeparatedByAll:aSeparatorString |
9f5984eff2c2
use asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8794
diff
changeset
|
3530 |
|
9f5984eff2c2
use asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8794
diff
changeset
|
3531 |
" |
8874 | 3532 |
'1::2::3::4::5::' asCollectionOfSubstringsSeparatedByAll:'::' |
7339
20969b5770c9
asCollectionOfSubstringsSeparatedByAll:
Claus Gittinger <cg@exept.de>
parents:
7318
diff
changeset
|
3533 |
" |
23187 | 3534 |
|
3535 |
"Modified (comment): / 03-07-2018 / 11:00:16 / Claus Gittinger" |
|
7339
20969b5770c9
asCollectionOfSubstringsSeparatedByAll:
Claus Gittinger <cg@exept.de>
parents:
7318
diff
changeset
|
3536 |
! |
20969b5770c9
asCollectionOfSubstringsSeparatedByAll:
Claus Gittinger <cg@exept.de>
parents:
7318
diff
changeset
|
3537 |
|
608 | 3538 |
asCollectionOfSubstringsSeparatedByAny:aCollectionOfSeparators |
3539 |
"return a collection containing the words (separated by any character |
|
3540 |
from aCollectionOfSeparators) of the receiver. |
|
3541 |
This allows breaking up strings using any character as separator." |
|
3542 |
||
8852
9f5984eff2c2
use asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8794
diff
changeset
|
3543 |
^ self asCollectionOfSubCollectionsSeparatedByAny:aCollectionOfSeparators |
608 | 3544 |
|
3545 |
" |
|
3546 |
'hello:world:isnt:this nice' asCollectionOfSubstringsSeparatedByAny:#($:) |
|
8874 | 3547 |
'hello:world:isnt:this nice' asCollectionOfSubstringsSeparatedByAny:':' |
3548 |
'hello:world:isnt:this nice' asCollectionOfSubstringsSeparatedByAny:(Array with:$: with:Character space) |
|
3549 |
'hello:world:isnt:this nice' asCollectionOfSubstringsSeparatedByAny:': ' |
|
3550 |
'h1e2l3l4o' asCollectionOfSubstringsSeparatedByAny:($1 to: $9) |
|
608 | 3551 |
" |
3552 |
! |
|
3553 |
||
3554 |
asCollectionOfWords |
|
8874 | 3555 |
"return a collection containing the words (separated by whitespace) |
3556 |
of the receiver. Multiple occurrences of whitespace characters will |
|
17517 | 3557 |
be treated like one - i.e. whitespace is skipped. |
3558 |
(sigh: it is called #'subStrings' in V'Age, and #'substrings' in Squeak) " |
|
608 | 3559 |
|
7797 | 3560 |
|words| |
608 | 3561 |
|
11905
cbc2191cfe2d
return stringCollections from asCollectionOfXXX
Claus Gittinger <cg@exept.de>
parents:
11904
diff
changeset
|
3562 |
words := StringCollection new. |
7797 | 3563 |
self asCollectionOfWordsDo:[:w | words add:w]. |
608 | 3564 |
^ words |
3565 |
||
3566 |
" |
|
3567 |
'hello world isnt this nice' asCollectionOfWords |
|
23660 | 3568 |
'hello world isnt this nice' asCollectionOfWordsDo:#transcribeCR |
608 | 3569 |
' hello world isnt this nice ' asCollectionOfWords |
3570 |
'hello' asCollectionOfWords |
|
3571 |
'' asCollectionOfWords |
|
3572 |
' ' asCollectionOfWords |
|
10703
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
3573 |
' foo bar__baz__bla__ bar ' asCollectionOfWords |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
3574 |
' foo __bar__baz__bla__ bar ' asCollectionOfWords |
608 | 3575 |
" |
23660 | 3576 |
|
3577 |
"Modified (comment): / 03-02-2019 / 13:07:57 / Claus Gittinger" |
|
9192 | 3578 |
! |
3579 |
||
3580 |
asCollectionOfWordsDo:aBlock |
|
8874 | 3581 |
"evaluate aBlock for each word (separated by whitespace) of the receiver. |
3582 |
Multiple occurrences of whitespace characters will be treated like one |
|
7797 | 3583 |
- i.e. whitespace is skipped. |
3584 |
Returns the number of words (i.e. the number of invocations of aBlock)." |
|
3585 |
||
3586 |
|count "{ Class:SmallInteger }" |
|
8874 | 3587 |
start "{ Class:SmallInteger }" |
3588 |
stop "{ Class:SmallInteger }" |
|
7797 | 3589 |
mySize "{ Class:SmallInteger }"| |
3590 |
||
3591 |
count := 0. |
|
3592 |
start := 1. |
|
3593 |
mySize := self size. |
|
3594 |
[start <= mySize] whileTrue:[ |
|
20874 | 3595 |
start := self indexOfNonSeparatorStartingAt:start. |
3596 |
start == 0 ifTrue:[ |
|
3597 |
^ count |
|
3598 |
]. |
|
3599 |
stop := self indexOfSeparatorStartingAt:start. |
|
3600 |
stop == 0 ifTrue:[ |
|
3601 |
aBlock value:(self copyFrom:start to:mySize). |
|
3602 |
^ count + 1 |
|
3603 |
]. |
|
3604 |
aBlock value:(self copyFrom:start to:(stop - 1)). |
|
3605 |
start := stop. |
|
3606 |
count := count + 1 |
|
7797 | 3607 |
]. |
3608 |
^ count |
|
3609 |
||
3610 |
" |
|
3611 |
'hello world isnt this nice' asCollectionOfWordsDo:[:w | Transcript showCR:w] |
|
23660 | 3612 |
'hello world isnt this nice' asCollectionOfWordsDo:#transcribeCR |
7797 | 3613 |
' hello world isnt this nice ' asCollectionOfWordsDo:[:w | Transcript showCR:w] |
3614 |
'hello' asCollectionOfWordsDo:[:w | Transcript showCR:w] |
|
3615 |
'' asCollectionOfWordsDo:[:w | Transcript showCR:w] |
|
3616 |
' ' asCollectionOfWordsDo:[:w | Transcript showCR:w] |
|
3617 |
" |
|
23660 | 3618 |
|
3619 |
"Modified (comment): / 03-02-2019 / 13:08:15 / Claus Gittinger" |
|
9192 | 3620 |
! |
3621 |
||
23971 | 3622 |
asDenseUnicodeString |
3623 |
"return the receiver as single-byte, double byte or 4-byte unicode string, |
|
3624 |
depending on the number of bits required to hold all characters in myself. |
|
3625 |
Use this to extract non-wide parts from a wide string, |
|
3626 |
i.e. after a substring has been copied out of a wide string" |
|
3627 |
||
3628 |
|nb| |
|
3629 |
||
3630 |
nb := self bytesPerCharacterNeeded. |
|
24034 | 3631 |
(nb ~~ self bytesPerCharacter) ifTrue:[ |
3632 |
nb == 1 ifTrue:[ |
|
3633 |
^ self asSingleByteString |
|
3634 |
]. |
|
3635 |
nb == 2 ifTrue:[ |
|
3636 |
^ self asUnicode16String |
|
23971 | 3637 |
]. |
3638 |
]. |
|
3639 |
^ self |
|
3640 |
||
3641 |
" |
|
3642 |
'abc' asUnicode16String asDenseUnicodeString |
|
3643 |
'abc' asUnicode32String asDenseUnicodeString |
|
3644 |
('abc',(Character value:16r165)) asDenseUnicodeString |
|
3645 |
('abc',(Character value:16r165)) asUnicode32String asDenseUnicodeString |
|
3646 |
" |
|
3647 |
||
3648 |
"Created: / 25-03-2019 / 16:28:02 / Claus Gittinger" |
|
24034 | 3649 |
"Modified: / 02-04-2019 / 10:48:44 / Claus Gittinger" |
23971 | 3650 |
! |
3651 |
||
9192 | 3652 |
asFilename |
3653 |
"return a Filename with pathname taken from the receiver" |
|
3654 |
||
3655 |
^ Filename named:self "(self asSingleByteStringReplaceInvalidWith:$?)" |
|
3656 |
||
3657 |
"Modified: 20.5.1996 / 09:38:15 / cg" |
|
3658 |
! |
|
608 | 3659 |
|
3060 | 3660 |
asFixedPoint |
3661 |
"read a fixedPoint number from the receiver. |
|
8874 | 3662 |
Notice, that errors may occur during the read, |
7423 | 3663 |
so you better setup some signal handler when using this method." |
3060 | 3664 |
|
3665 |
^ FixedPoint readFromString:self |
|
3666 |
||
3667 |
" |
|
8874 | 3668 |
'0.123' asFixedPoint |
3669 |
'12345' asFixedPoint |
|
3060 | 3670 |
'(1/5)' asFixedPoint |
3671 |
'foo' asFixedPoint |
|
9192 | 3672 |
Object errorSignal handle:[:ex | ex return:0] do:['foo' asFixedPoint] |
3673 |
" |
|
3674 |
||
3675 |
"Modified: / 25.10.1997 / 15:19:00 / cg" |
|
3676 |
! |
|
3677 |
||
3678 |
asFixedPoint:scale |
|
3060 | 3679 |
"read a fixedPoint number with scale number of post-decimal digits |
3680 |
from the receiver. Scale controls the number of displayed digits, |
|
3681 |
not the number of actually valid digits. |
|
8874 | 3682 |
Notice, that errors may occur during the read, |
7423 | 3683 |
so you better setup some signal handler when using this method." |
3060 | 3684 |
|
3685 |
^ (FixedPoint readFromString:self) scale:scale |
|
3686 |
||
3687 |
" |
|
8874 | 3688 |
'0.123' asFixedPoint:2 |
3689 |
'123456' asFixedPoint:2 |
|
3690 |
('3.14157' asFixedPoint:1) asFixedPoint:5 |
|
3691 |
'3.14157' asFixedPoint:2 |
|
3692 |
'foo' asFixedPoint:2 |
|
3060 | 3693 |
" |
3694 |
||
3695 |
"Modified: / 25.10.1997 / 15:21:57 / cg" |
|
9192 | 3696 |
! |
3697 |
||
3698 |
asFloat |
|
608 | 3699 |
"read a float number from the receiver. |
8874 | 3700 |
Notice, that errors may occur during the read, |
7422 | 3701 |
so you better setup some exception handler when using this method." |
608 | 3702 |
|
3703 |
^ (Number readFromString:self) asFloat |
|
3704 |
||
3705 |
" |
|
8874 | 3706 |
'0.123' asFloat |
608 | 3707 |
'12345' asFloat |
3708 |
'(1/5)' asFloat |
|
8874 | 3709 |
Object errorSignal handle:[:ex | ex return:0] do:['foo' asFloat] |
608 | 3710 |
" |
3711 |
! |
|
3712 |
||
3713 |
asInteger |
|
9190 | 3714 |
"convert the receiver into an integer. |
8874 | 3715 |
Notice, that errors may occur during the read, |
9190 | 3716 |
so you better setup some exception handler when using this method. |
3717 |
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
|
3718 |
in other smalltalks. |
9190 | 3719 |
For less strict integer reading, use Integer readFrom:aString" |
608 | 3720 |
|
3721 |
^ Integer readFromString:self |
|
3722 |
||
3723 |
" |
|
3724 |
'12345678901234567890' asInteger |
|
3725 |
'-1234' asInteger |
|
9190 | 3726 |
|
3727 |
The following raises an error: |
|
20874 | 3728 |
'0.123' asInteger <- reader finds more after reading 0 |
9190 | 3729 |
|
3730 |
whereas the less strict readFrom does not: |
|
20874 | 3731 |
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
|
3732 |
|
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
3733 |
'0.123' asInteger |
608 | 3734 |
'0.123' asNumber <- returns what you expect |
8874 | 3735 |
Object errorSignal handle:[:ex | ex return:0] do:['foo' asInteger] |
9190 | 3736 |
|
3737 |
" |
|
9192 | 3738 |
! |
3739 |
||
3740 |
asLowercase |
|
608 | 3741 |
"return a copy of myself in lowercase letters" |
3742 |
||
8003
6e690f4b79f1
asLowercase/asUppercase and friends care for 16-bit chars
Claus Gittinger <cg@exept.de>
parents:
7995
diff
changeset
|
3743 |
|newStr c bitsPerCharacter |
9192 | 3744 |
mySize "{ Class: SmallInteger }" | |
3745 |
||
3746 |
mySize := self size. |
|
3747 |
mySize == 0 ifTrue:[^ self]. |
|
18657 | 3748 |
|
9192 | 3749 |
newStr := self species new:mySize. |
3750 |
bitsPerCharacter := newStr bitsPerCharacter. |
|
3751 |
||
18657 | 3752 |
"/ handle the very seldom case of an uppercase char which needs |
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
3753 |
"/ more bits in its lowercase variant |
18657 | 3754 |
"/ (there are only a few of them) |
3755 |
||
9192 | 3756 |
1 to:mySize do:[:i | |
20705 | 3757 |
c := (self at:i) asLowercase. |
3758 |
(c bitsPerCharacter > bitsPerCharacter |
|
3759 |
and:[c stringSpecies ~= newStr stringSpecies]) ifTrue:[ |
|
3760 |
newStr := c stringSpecies fromString:newStr. |
|
3761 |
bitsPerCharacter := newStr bitsPerCharacter. |
|
3762 |
]. |
|
3763 |
newStr at:i put:c |
|
9192 | 3764 |
]. |
3765 |
^ newStr |
|
3766 |
||
3767 |
" |
|
3768 |
'HelloWorld' asLowercase |
|
18662 | 3769 |
'HelloWorld' asUnicode16String asLowercase |
9192 | 3770 |
'HelloWorld' asLowercaseFirst |
18657 | 3771 |
'HelloWorld' asUppercase |
20705 | 3772 |
'HelloWorldÿ' asUppercase |
3773 |
'HelloWorldŸ' asLowercase - currently returns an U16 string; should this be u8? |
|
9192 | 3774 |
" |
3775 |
! |
|
3776 |
||
3777 |
asLowercaseFirst |
|
17508 | 3778 |
"return a copy of myself where the first character is converted to lowercase. |
3779 |
If the first character is already lowercase, or there is no uppercase for it, return the |
|
3780 |
receiver." |
|
9192 | 3781 |
|
3782 |
|newString firstChar firstCharAsLowercase| |
|
3783 |
||
11525 | 3784 |
self isEmpty ifTrue:[^ self]. |
9192 | 3785 |
firstChar := (self at:1). |
3786 |
firstCharAsLowercase := firstChar asLowercase. |
|
3787 |
firstChar == firstCharAsLowercase ifTrue:[ ^ self]. |
|
3788 |
||
3789 |
firstCharAsLowercase bitsPerCharacter > self bitsPerCharacter ifTrue:[ |
|
20874 | 3790 |
newString := firstCharAsLowercase stringSpecies fromString:self. |
9192 | 3791 |
] ifFalse:[ |
20874 | 3792 |
newString := self stringSpecies fromString:self. |
9192 | 3793 |
]. |
9190 | 3794 |
newString at:1 put:firstCharAsLowercase. |
3795 |
^ newString |
|
608 | 3796 |
|
3797 |
" |
|
8874 | 3798 |
'HelloWorld' asLowercase |
3799 |
'HelloWorld' asLowercaseFirst |
|
608 | 3800 |
" |
3801 |
! |
|
3802 |
||
11828 | 3803 |
asLowercaseLast |
3804 |
"return a copy of myself where the last character is |
|
3805 |
converted to lowercase." |
|
3806 |
||
3807 |
|newString sz| |
|
3808 |
||
3809 |
sz := self size. |
|
3810 |
newString := self copyFrom:1 to:sz. |
|
3811 |
sz > 0 ifTrue:[ |
|
20874 | 3812 |
newString at:sz put:(newString at:sz) asLowercase |
11828 | 3813 |
]. |
3814 |
^ newString |
|
3815 |
||
3816 |
" |
|
12177 | 3817 |
'HelloWorld' asLowercase |
3818 |
'HelloWorlD' asLowercaseLast |
|
11828 | 3819 |
" |
3820 |
! |
|
3821 |
||
17644 | 3822 |
asMutator |
19473 | 3823 |
"return a corresponding setter method's selector. |
3824 |
I.e. #foo asMutator returns #foo:" |
|
17644 | 3825 |
|
20873 | 3826 |
^ (self asSingleByteString,':') asSymbol |
17644 | 3827 |
! |
3828 |
||
9190 | 3829 |
asNumber |
3830 |
"read a number from the receiver. |
|
3831 |
Notice, that (in contrast to ST-80) errors may occur during the read, |
|
3832 |
so you better setup some signal handler when using this method. |
|
3833 |
Also notice, that this is meant to read end-user numbers from a string; |
|
9135 | 3834 |
it does not handle smalltalk numbers (i.e. radix). |
3835 |
To read a smalltalk number, use Number >> readSmalltalkFrom:. |
|
608 | 3836 |
This may change if ANSI specifies it." |
3837 |
||
3838 |
"/ ST-80 behavior: |
|
3839 |
"/ ^ Number readFromString:self onError:0 |
|
3840 |
||
17061 | 3841 |
^ Number fromString:self |
3842 |
||
3843 |
" |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
3844 |
'123' asNumber |
608 | 3845 |
'123.567' asNumber |
3846 |
'(5/6)' asNumber |
|
3847 |
'foo' asNumber |
|
17061 | 3848 |
'123a' asNumber |
8874 | 3849 |
Object errorSignal handle:[:ex | ex returnWith:0] do:['foo' asNumber] |
608 | 3850 |
" |
9192 | 3851 |
! |
3852 |
||
3853 |
asNumberFromFormatString:ignored |
|
608 | 3854 |
"read a number from the receiver, ignoring any nonDigit characters. |
3855 |
This is typically used to convert from strings which include |
|
3856 |
dollar-signs or millenium digits. However, this method also ignores |
|
3857 |
the decimal point (if any) and therefore should be used with care." |
|
3858 |
||
3859 |
|tempString| |
|
3860 |
||
3861 |
tempString := self collect:[:char | char isDigit]. |
|
3862 |
^ Number readFromString:tempString onError:0 |
|
3863 |
||
3864 |
" |
|
3865 |
'USD 123' asNumberFromFormatString:'foo' |
|
3866 |
'DM 123' asNumberFromFormatString:'foo' |
|
3867 |
'123' asNumberFromFormatString:'foo' |
|
3868 |
'123.567' asNumberFromFormatString:'foo' |
|
3869 |
'(5/6)' asNumberFromFormatString:'foo' |
|
3870 |
'foo' asNumberFromFormatString:'foo' |
|
3871 |
" |
|
9192 | 3872 |
! |
3873 |
||
21994 | 3874 |
asPackageId |
3875 |
"given a package-string as receiver, return a packageId object. |
|
3876 |
packageIds hide the details of module/directory handling inside the path. |
|
3877 |
See PackageId for the required format of those strings." |
|
3878 |
||
3879 |
^ PackageId from: self string |
|
3880 |
||
21995 | 3881 |
" |
3882 |
'stx:libbasic' asPackageId |
|
3883 |
" |
|
3884 |
||
21994 | 3885 |
"Created: / 11-07-2017 / 18:24:47 / cg" |
3886 |
! |
|
3887 |
||
7977 | 3888 |
asSingleByteStringIfPossible |
3889 |
"if possible, return the receiver converted to a 'normal' string. |
|
18181 | 3890 |
It is only possible, if there are no characters with codePoints above 255 in the receiver. |
3891 |
If not possible, the (wideString) receiver is returned." |
|
7977 | 3892 |
|
14555 | 3893 |
self isWideString ifFalse:[^ self]. |
18967 | 3894 |
self containsNon8BitElements ifTrue:[^ self]. |
3895 |
^ self asSingleByteString |
|
9192 | 3896 |
|
3897 |
" |
|
3898 |
'hello' asSingleByteStringIfPossible |
|
18967 | 3899 |
'hello' asText asSingleByteStringIfPossible |
3900 |
'hello' asUnicodeString asText asSingleByteStringIfPossible |
|
9192 | 3901 |
'hello' asUnicodeString asSingleByteStringIfPossible |
3902 |
" |
|
3903 |
! |
|
3904 |
||
3905 |
asSingleByteStringReplaceInvalidWith:replacementCharacter |
|
3906 |
"return the receiver converted to a 'normal' string, |
|
3907 |
with invalid characters replaced by replacementCharacter. |
|
3908 |
Can be used to convert from 16-bit strings to 8-bit strings |
|
3909 |
and replace characters above code-255 with some replacement." |
|
3910 |
||
3911 |
|newString| |
|
3912 |
||
3913 |
newString := String new:(self size). |
|
3914 |
1 to:self size do:[:idx | |
|
20874 | 3915 |
|char| |
3916 |
||
3917 |
char := self at:idx. |
|
3918 |
char codePoint <= 16rFF ifTrue:[ |
|
3919 |
newString at:idx put:char |
|
3920 |
] ifFalse:[ |
|
3921 |
newString at:idx put:replacementCharacter |
|
3922 |
]. |
|
9190 | 3923 |
]. |
3924 |
^ newString |
|
3925 |
||
3926 |
"Created: 30.6.1997 / 13:02:14 / cg" |
|
7977 | 3927 |
! |
3928 |
||
9192 | 3929 |
asString |
3930 |
"return myself - I am a string" |
|
3931 |
||
3932 |
^ self |
|
3933 |
! |
|
3934 |
||
3935 |
asStringCollection |
|
3936 |
"return a collection of lines from myself." |
|
3937 |
||
3938 |
^ StringCollection fromString:self "string" |
|
3939 |
||
21639 | 3940 |
" |
3941 |
'hello\world\1\2\3' withCRs asStringCollection first |
|
3942 |
" |
|
3943 |
||
3944 |
"Modified: / 13-05-1996 / 20:36:59 / cg" |
|
3945 |
"Modified (comment): / 13-03-2017 / 14:05:14 / cg" |
|
9192 | 3946 |
! |
1375
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
3947 |
|
24162 | 3948 |
asStringWithBitsPerCharacterAtLeast:numRequiredBitsPerCharacter |
3949 |
"return the receiver in a representation which supports numRequiredBitsPerCharacter. |
|
3950 |
I.e. if required, convert to a Unicode16 or Unicode32 string" |
|
3951 |
||
3952 |
numRequiredBitsPerCharacter > 8 ifTrue:[ |
|
3953 |
numRequiredBitsPerCharacter > 16 ifTrue:[ |
|
3954 |
^ self asUnicode32String |
|
3955 |
]. |
|
3956 |
numRequiredBitsPerCharacter > self bitsPerCharacter ifTrue:[ |
|
3957 |
^ self asUnicode16String |
|
3958 |
]. |
|
3959 |
]. |
|
3960 |
^ self |
|
3961 |
||
3962 |
" |
|
3963 |
self assert:('abc' asStringWithBitsPerCharacterAtLeast:8) bitsPerCharacter == 8. |
|
3964 |
self assert:('abc' asStringWithBitsPerCharacterAtLeast:16) bitsPerCharacter == 16. |
|
3965 |
self assert:('abc' asStringWithBitsPerCharacterAtLeast:32) bitsPerCharacter == 32. |
|
3966 |
||
3967 |
self assert:('abc' asUnicode16String asStringWithBitsPerCharacterAtLeast:8) bitsPerCharacter == 16. |
|
3968 |
self assert:('abc' asUnicode16String asStringWithBitsPerCharacterAtLeast:16) bitsPerCharacter == 16. |
|
3969 |
self assert:('abc' asUnicode16String asStringWithBitsPerCharacterAtLeast:32) bitsPerCharacter == 32. |
|
3970 |
||
3971 |
self assert:('abc' asUnicode32String asStringWithBitsPerCharacterAtLeast:8) bitsPerCharacter == 32. |
|
3972 |
self assert:('abc' asUnicode32String asStringWithBitsPerCharacterAtLeast:16) bitsPerCharacter == 32. |
|
3973 |
self assert:('abc' asUnicode32String asStringWithBitsPerCharacterAtLeast:32) bitsPerCharacter == 32. |
|
3974 |
" |
|
3975 |
||
3976 |
"Created: / 26-05-2019 / 12:47:07 / Claus Gittinger" |
|
3977 |
! |
|
3978 |
||
23311 | 3979 |
asStringWithoutEmphasis |
3980 |
"return myself as a string without any emphasis" |
|
3981 |
"return myself - I am a string" |
|
3982 |
||
3983 |
^ self |
|
3984 |
||
3985 |
"Created: / 29-08-2018 / 09:21:07 / Claus Gittinger" |
|
3986 |
! |
|
3987 |
||
1434 | 3988 |
asSymbol |
19039 | 3989 |
"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
|
3990 |
The receiver must be a singleByte-String. |
ccd0e8f26439
printing - migrating towards unicodeSupport in ST-code
Claus Gittinger <cg@exept.de>
parents:
8040
diff
changeset
|
3991 |
TwoByte- and FourByteSymbols are (currently ?) not allowed." |
ccd0e8f26439
printing - migrating towards unicodeSupport in ST-code
Claus Gittinger <cg@exept.de>
parents:
8040
diff
changeset
|
3992 |
|
ccd0e8f26439
printing - migrating towards unicodeSupport in ST-code
Claus Gittinger <cg@exept.de>
parents:
8040
diff
changeset
|
3993 |
|str| |
ccd0e8f26439
printing - migrating towards unicodeSupport in ST-code
Claus Gittinger <cg@exept.de>
parents:
8040
diff
changeset
|
3994 |
|
9192 | 3995 |
str := self string. |
3996 |
str ~~ self ifTrue:[ ^ str asSymbol ]. |
|
3997 |
^ self asSingleByteString asSymbol |
|
3998 |
! |
|
3999 |
||
4000 |
asSymbolIfInterned |
|
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
4001 |
"If a symbol with the receiver's characters is already known, return it. |
19692 | 4002 |
Otherwise, return nil. |
9230 | 4003 |
This can be used to query for an existing symbol and is the same as: |
20874 | 4004 |
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
|
4005 |
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
|
4006 |
The receiver must be a singleByte-String. |
ccd0e8f26439
printing - migrating towards unicodeSupport in ST-code
Claus Gittinger <cg@exept.de>
parents:
8040
diff
changeset
|
4007 |
TwoByte- and FourByteSymbols are (currently ?) not allowed." |
1435
3ee1fde2aa58
allow asSymbolIfInterned for Texts
Claus Gittinger <cg@exept.de>
parents:
1434
diff
changeset
|
4008 |
|
18967 | 4009 |
|str| |
4010 |
||
4011 |
str := self string. |
|
4012 |
str == self ifTrue:[ |
|
20874 | 4013 |
"must be some kind of N-ByteString" |
4014 |
str := self asSingleByteStringIfPossible. |
|
4015 |
str == self ifTrue:[ |
|
4016 |
"single byte string conversion is not possible" |
|
4017 |
^ nil. |
|
4018 |
]. |
|
6642 | 4019 |
]. |
18967 | 4020 |
^ str asSymbolIfInterned |
4021 |
||
4022 |
" |
|
4023 |
(Unicode16String with:(Character value:16rFFFF)) asSymbolIfInterned |
|
4024 |
'new' asUnicodeString asSymbolIfInterned |
|
4025 |
'new' asText asSymbolIfInterned |
|
4026 |
'new' asUnicodeString asText asSymbolIfInterned |
|
4027 |
" |
|
1435
3ee1fde2aa58
allow asSymbolIfInterned for Texts
Claus Gittinger <cg@exept.de>
parents:
1434
diff
changeset
|
4028 |
|
3ee1fde2aa58
allow asSymbolIfInterned for Texts
Claus Gittinger <cg@exept.de>
parents:
1434
diff
changeset
|
4029 |
"Created: 22.5.1996 / 16:37:04 / cg" |
9192 | 4030 |
! |
4031 |
||
20160 | 4032 |
asSymbolIfInternedOrSelf |
4033 |
"If a symbol with the receiver's characters is already known, return it. |
|
4034 |
Otherwise, return self. |
|
4035 |
This can be used to query for an existing symbol and is the same as: |
|
4036 |
self knownAsSymbol ifTrue:[self asSymbol] ifFalse:[self] |
|
4037 |
but slightly faster, since the symbol lookup operation is only performed once. |
|
4038 |
The receiver must be a singleByte-String. |
|
4039 |
TwoByte- and FourByteSymbols are (currently ?) not allowed." |
|
4040 |
||
4041 |
|sym| |
|
4042 |
||
4043 |
sym := self asSymbolIfInterned. |
|
4044 |
^ sym isNil ifTrue:[self] ifFalse:[sym]. |
|
4045 |
||
4046 |
||
4047 |
" |
|
4048 |
(Unicode16String with:(Character value:16rFFFF)) asSymbolIfInternedOrSelf |
|
4049 |
'new' asUnicodeString asSymbolIfInternedOrSelf |
|
4050 |
'new' asText asSymbolIfInternedOrSelf |
|
4051 |
'new' asUnicodeString asText asSymbolIfInternedOrSelf |
|
4052 |
" |
|
4053 |
||
4054 |
"Created: 22.5.1996 / 16:37:04 / cg" |
|
4055 |
! |
|
4056 |
||
9192 | 4057 |
asText |
9403 | 4058 |
"return a Text-object (string with emphasis) from myself." |
9192 | 4059 |
|
4060 |
"this test allows for small non-gui apps to be built without libbasic2 (where Text is)" |
|
4061 |
Text isNil ifTrue:[^ self]. |
|
17065 | 4062 |
^ Text string:self |
9192 | 4063 |
|
4064 |
"Created: 12.5.1996 / 10:41:14 / cg" |
|
4065 |
! |
|
4066 |
||
23617 | 4067 |
asTimeDuration |
4068 |
"return an TimeDuration object from the parsing the receiver string" |
|
4069 |
||
4070 |
^ TimeDuration readFromString:self |
|
4071 |
||
4072 |
" |
|
4073 |
Time now asTimeDuration |
|
4074 |
10 asTimeDuration |
|
4075 |
10 seconds asTimeDuration |
|
4076 |
'10m 20s' asTimeDuration |
|
4077 |
" |
|
4078 |
||
4079 |
"Created: / 21-01-2019 / 10:29:51 / Claus Gittinger" |
|
4080 |
! |
|
4081 |
||
17021 | 4082 |
asTimestamp |
4083 |
"convert the receiver into an Timestamp. |
|
4084 |
Notice, that errors may occur during the read, |
|
4085 |
so you better setup some exception handler when using this method." |
|
4086 |
||
21030 | 4087 |
^ Timestamp readFrom:self |
17021 | 4088 |
|
4089 |
" |
|
4090 |
'2014-11-10 21:30:22.444' asTimestamp |
|
4091 |
'2014-11-10 21:30:22.444Z' asTimestamp |
|
4092 |
'2014-11-10 21:30:22.444+0200' asTimestamp |
|
4093 |
" |
|
4094 |
! |
|
4095 |
||
9192 | 4096 |
asTitlecase |
8027 | 4097 |
"return a version of the receiver, where the first character is converted to titlecase, |
4098 |
and everything else to lowercase. |
|
4099 |
See the comment in Character on what titlecase is." |
|
4100 |
||
4101 |
|newStr c bitsPerCharacter |
|
4102 |
mySize "{ Class: SmallInteger }" | |
|
4103 |
||
4104 |
mySize := self size. |
|
4105 |
newStr := self species new:mySize. |
|
4106 |
bitsPerCharacter := newStr bitsPerCharacter. |
|
4107 |
||
4108 |
1 to:mySize do:[:i | |
|
20874 | 4109 |
i == 1 ifTrue:[ |
4110 |
c := (self at:i) asTitlecase. |
|
4111 |
] ifFalse:[ |
|
4112 |
c := (self at:i) asLowercase. |
|
4113 |
]. |
|
4114 |
c bitsPerCharacter > bitsPerCharacter ifTrue:[ |
|
4115 |
newStr := c stringSpecies fromString:newStr. |
|
4116 |
bitsPerCharacter := newStr bitsPerCharacter. |
|
4117 |
]. |
|
4118 |
newStr at:i put:c |
|
8027 | 4119 |
]. |
4120 |
^ newStr |
|
4121 |
||
4122 |
" |
|
8874 | 4123 |
'helloWorld' asTitlecase |
4124 |
'HelloWorld' asTitlecase |
|
4125 |
'HELLOWORLD' asTitlecase |
|
4126 |
'helloworld' asTitlecase |
|
8027 | 4127 |
" |
9192 | 4128 |
! |
4129 |
||
4130 |
asTitlecaseFirst |
|
8023 | 4131 |
"return a version of the receiver, where the first character is converted to titlecase. |
8024 | 4132 |
Titlecase is much like uppercase for most characters, with the exception of some combined |
4133 |
(2-character glyphs), which consist of an upper- and lower-case characters. |
|
4134 |
If the first character is already titlecase, or there is no titlecasepercase for it, return the |
|
8023 | 4135 |
receiver." |
4136 |
||
8026 | 4137 |
" |
8874 | 4138 |
For example, in Unicode, character U+01F3 is LATIN SMALL LETTER DZ. |
4139 |
(Let us write this compound character using ASCII as 'dz'.) |
|
4140 |
This character uppercases to character U+01F1, LATIN CAPITAL LETTER DZ. |
|
4141 |
(Which is basically 'DZ'.) |
|
4142 |
But it titlecases to to character U+01F2, LATIN CAPITAL LETTER D WITH SMALL LETTER Z. |
|
8026 | 4143 |
(Which we can write 'Dz'.) |
4144 |
||
4145 |
character uppercase titlecase |
|
4146 |
--------- --------- --------- |
|
4147 |
dz DZ Dz |
|
4148 |
" |
|
4149 |
||
8023 | 4150 |
|newString firstChar firstCharAsTitlecase| |
4151 |
||
4152 |
firstChar := (self at:1). |
|
8025 | 4153 |
firstCharAsTitlecase := firstChar asTitlecase. |
8023 | 4154 |
firstChar == firstCharAsTitlecase ifTrue:[ ^ self]. |
8037
8cce25b1059c
care for wide chars in asXXXFirst
Claus Gittinger <cg@exept.de>
parents:
8032
diff
changeset
|
4155 |
|
8cce25b1059c
care for wide chars in asXXXFirst
Claus Gittinger <cg@exept.de>
parents:
8032
diff
changeset
|
4156 |
firstCharAsTitlecase bitsPerCharacter > self bitsPerCharacter ifTrue:[ |
20874 | 4157 |
newString := firstCharAsTitlecase stringSpecies fromString:self. |
8037
8cce25b1059c
care for wide chars in asXXXFirst
Claus Gittinger <cg@exept.de>
parents:
8032
diff
changeset
|
4158 |
] ifFalse:[ |
20874 | 4159 |
newString := self stringSpecies fromString:self. |
8037
8cce25b1059c
care for wide chars in asXXXFirst
Claus Gittinger <cg@exept.de>
parents:
8032
diff
changeset
|
4160 |
]. |
8023 | 4161 |
newString at:1 put:firstCharAsTitlecase. |
4162 |
^ newString |
|
4163 |
||
4164 |
" |
|
8874 | 4165 |
'helloWorld' asTitlecaseFirst |
4166 |
'HelloWorld' asTitlecaseFirst |
|
8023 | 4167 |
" |
9192 | 4168 |
! |
4169 |
||
4170 |
asTwoByteString |
|
4171 |
"return the receiver converted to a two-byte string. |
|
16051 | 4172 |
Will be obsolete soon - use asUnicode16String." |
9192 | 4173 |
|
4174 |
^ TwoByteString fromString:self |
|
4175 |
! |
|
4176 |
||
4177 |
asURI |
|
4178 |
"return an URI with string taken from the receiver" |
|
4179 |
||
4180 |
^ URI fromString:self |
|
4181 |
! |
|
8710 | 4182 |
|
5776 | 4183 |
asURL |
4184 |
"return an URL-object from myself." |
|
4185 |
||
4186 |
^ URL fromString:self |
|
4187 |
||
4188 |
" |
|
8874 | 4189 |
'http://www.exept.de:80/index.html' asURL host |
4190 |
'http://www.exept.de:80/index.html' asURL port |
|
9192 | 4191 |
'http://www.exept.de:80/index.html' asURL method |
4192 |
'http://www.exept.de:80/index.html' asURL path |
|
24147 | 4193 |
'file:///tmp/index.html' asURL path |
4194 |
'file:///tmp/index.html' asURL method |
|
4195 |
'file:///tmp/index.html' asURL asString |
|
4196 |
" |
|
4197 |
||
4198 |
"Modified (format): / 25-05-2019 / 09:33:18 / Claus Gittinger" |
|
9192 | 4199 |
! |
4200 |
||
18901 | 4201 |
asUUID |
4202 |
"return self as a UUID" |
|
4203 |
||
4204 |
^ UUID fromString:self |
|
4205 |
||
4206 |
" |
|
4207 |
UUID genUUID asString asUUID |
|
4208 |
'{EAB22AC0-30C1-11CF-A7EB-0000C05BAE0B}' asUUID |
|
4209 |
'{EAB22AC0-30C1-11CF-A7EB-0000C05BAE0B}' asUnicodeString asUUID |
|
4210 |
'EAB22AC0-30C1-11CF-A7EB-0000C05BAE0B' asUUID |
|
4211 |
'EAB22AC0-30C1-11CF-A7EB-0000C05BAE0B quatsch' asUUID |
|
4212 |
'quark EAB22AC0-30C1-11CF-A7EB-0000C05BAE0B' asUUID |
|
4213 |
" |
|
4214 |
||
4215 |
"Modified: / 02-08-2007 / 16:43:29 / cg" |
|
4216 |
! |
|
4217 |
||
9192 | 4218 |
asUnicode16String |
15035 | 4219 |
"return the receiver in a two-byte per character representation. |
4220 |
Normally, the internal ST/X representation should be transparent and not |
|
4221 |
of the programmer's concern; except when strings are passed to the outside world, |
|
4222 |
such as wide-string ffi calls or file contents." |
|
4223 |
||
17600 | 4224 |
|sz| |
4225 |
||
4226 |
sz := self size. |
|
4227 |
||
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
4228 |
^ (Unicode16String new:sz) |
20874 | 4229 |
replaceFrom:1 to:sz with:self startingAt:1; |
4230 |
yourself. |
|
4231 |
||
4232 |
" |
|
4233 |
'abc' asUnicode16String |
|
17600 | 4234 |
" |
24212 | 4235 |
|
4236 |
"Modified (comment): / 28-05-2019 / 14:41:04 / Stefan Vogel" |
|
4237 |
! |
|
4238 |
||
4239 |
asUnicode16StringReplaceInvalidWith:replacementCharacter |
|
4240 |
^ self asUnicode16String |
|
4241 |
||
4242 |
"Created: / 28-05-2019 / 12:15:44 / Stefan Vogel" |
|
17600 | 4243 |
! |
4244 |
||
4245 |
asUnicode16StringZ |
|
4246 |
"return the receiver in a two-byte per character representation. |
|
17625 | 4247 |
Make sure that is zero terminated (last char is 16r0000). |
17600 | 4248 |
Normally, the internal ST/X representation should be transparent and not |
4249 |
of the programmer's concern; except when strings are passed to the outside world, |
|
4250 |
such as wide-string ffi calls or file contents." |
|
4251 |
||
4252 |
|sz| |
|
4253 |
||
4254 |
sz := self size. |
|
22249 | 4255 |
sz > 0 ifTrue:[ |
4256 |
(self at:sz) == (Character codePoint:0) ifTrue:[ |
|
4257 |
^ self asUnicode16String. |
|
4258 |
]. |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
4259 |
]. |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
4260 |
|
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
4261 |
^ (Unicode16String new:sz+1) |
20874 | 4262 |
replaceFrom:1 to:sz with:self startingAt:1; |
4263 |
at:sz+1 put:(Character codePoint:0); |
|
4264 |
yourself. |
|
17600 | 4265 |
|
4266 |
" |
|
22249 | 4267 |
'' asUnicode16StringZ |
20874 | 4268 |
'abc' asUnicode16StringZ |
4269 |
'abc' asUnicode16String asUnicode16StringZ |
|
17600 | 4270 |
" |
9192 | 4271 |
! |
8295
8433d819226b
Define #asUnicode16String and #asUnicode32String in CharacterArray
Stefan Vogel <sv@exept.de>
parents:
8222
diff
changeset
|
4272 |
|
8433d819226b
Define #asUnicode16String and #asUnicode32String in CharacterArray
Stefan Vogel <sv@exept.de>
parents:
8222
diff
changeset
|
4273 |
asUnicode32String |
16051 | 4274 |
"return the receiver in a four-byte per character representation. |
4275 |
Normally, the internal ST/X representation should be transparent and not |
|
4276 |
of the programmer's concern; except when strings are passed to the outside world, |
|
4277 |
such as wide-string ffi calls or file contents." |
|
4278 |
||
4279 |
^ self as:Unicode32String |
|
8295
8433d819226b
Define #asUnicode16String and #asUnicode32String in CharacterArray
Stefan Vogel <sv@exept.de>
parents:
8222
diff
changeset
|
4280 |
! |
8433d819226b
Define #asUnicode16String and #asUnicode32String in CharacterArray
Stefan Vogel <sv@exept.de>
parents:
8222
diff
changeset
|
4281 |
|
8433d819226b
Define #asUnicode16String and #asUnicode32String in CharacterArray
Stefan Vogel <sv@exept.de>
parents:
8222
diff
changeset
|
4282 |
asUnicodeString |
16051 | 4283 |
"return the receiver in a two-byte per character representation. |
4284 |
Normally, the internal ST/X representation should be transparent and not |
|
4285 |
of the programmer's concern; except when strings are passed to the outside world, |
|
4286 |
such as wide-string ffi calls or file contents." |
|
4287 |
||
4288 |
^ self asUnicode16String |
|
4289 |
" |
|
4290 |
'Hello World' asUnicodeString |
|
14141
e86d245213b0
comment/format in: #asUnicodeString
Stefan Vogel <sv@exept.de>
parents:
14137
diff
changeset
|
4291 |
" |
8295
8433d819226b
Define #asUnicode16String and #asUnicode32String in CharacterArray
Stefan Vogel <sv@exept.de>
parents:
8222
diff
changeset
|
4292 |
! |
8433d819226b
Define #asUnicode16String and #asUnicode32String in CharacterArray
Stefan Vogel <sv@exept.de>
parents:
8222
diff
changeset
|
4293 |
|
13073 | 4294 |
asUnixFilename |
4295 |
"return a UnixFilename with pathname taken from the receiver" |
|
4296 |
||
4297 |
^ UnixFilename named:self |
|
4298 |
||
4299 |
"Created: / 20-09-2010 / 21:42:55 / cg" |
|
4300 |
! |
|
4301 |
||
608 | 4302 |
asUppercase |
4303 |
"return a copy of myself in uppercase letters" |
|
4304 |
||
8003
6e690f4b79f1
asLowercase/asUppercase and friends care for 16-bit chars
Claus Gittinger <cg@exept.de>
parents:
7995
diff
changeset
|
4305 |
|newStr c bitsPerCharacter |
608 | 4306 |
mySize "{ Class: SmallInteger }" | |
4307 |
||
4308 |
mySize := self size. |
|
8954
12e701d7b5ac
allow for receiver to be empty in asLowercase and asUppercase.
Claus Gittinger <cg@exept.de>
parents:
8938
diff
changeset
|
4309 |
mySize == 0 ifTrue:[^ self]. |
608 | 4310 |
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
|
4311 |
bitsPerCharacter := newStr bitsPerCharacter. |
6e690f4b79f1
asLowercase/asUppercase and friends care for 16-bit chars
Claus Gittinger <cg@exept.de>
parents:
7995
diff
changeset
|
4312 |
|
18657 | 4313 |
"/ handle the very seldom case of a lowercase char which needs |
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
4314 |
"/ more bits in its uppercase variant |
18657 | 4315 |
"/ (there are only a few of them) |
4316 |
||
608 | 4317 |
1 to:mySize do:[:i | |
20874 | 4318 |
c := (self at:i) asUppercase. |
4319 |
c bitsPerCharacter > bitsPerCharacter ifTrue:[ |
|
4320 |
newStr := c stringSpecies fromString:newStr. |
|
4321 |
bitsPerCharacter := newStr bitsPerCharacter. |
|
4322 |
]. |
|
4323 |
newStr at:i put:c |
|
608 | 4324 |
]. |
4325 |
^ newStr |
|
4326 |
||
4327 |
" |
|
8874 | 4328 |
'helloWorld' asUppercase |
4329 |
'helloWorld' asUppercaseFirst |
|
9192 | 4330 |
(Character value:16rB5) asString asUppercase -- needs 16 bits !! |
18657 | 4331 |
(Character value:16rFF) asString asUppercase -- needs 16 bits !! |
9192 | 4332 |
" |
4333 |
! |
|
4334 |
||
4335 |
asUppercaseFirst |
|
8003
6e690f4b79f1
asLowercase/asUppercase and friends care for 16-bit chars
Claus Gittinger <cg@exept.de>
parents:
7995
diff
changeset
|
4336 |
"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
|
4337 |
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
|
4338 |
receiver." |
6e690f4b79f1
asLowercase/asUppercase and friends care for 16-bit chars
Claus Gittinger <cg@exept.de>
parents:
7995
diff
changeset
|
4339 |
|
6e690f4b79f1
asLowercase/asUppercase and friends care for 16-bit chars
Claus Gittinger <cg@exept.de>
parents:
7995
diff
changeset
|
4340 |
|newString firstChar firstCharAsUppercase| |
6e690f4b79f1
asLowercase/asUppercase and friends care for 16-bit chars
Claus Gittinger <cg@exept.de>
parents:
7995
diff
changeset
|
4341 |
|
12679 | 4342 |
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
|
4343 |
firstChar := self at:1. |
8003
6e690f4b79f1
asLowercase/asUppercase and friends care for 16-bit chars
Claus Gittinger <cg@exept.de>
parents:
7995
diff
changeset
|
4344 |
firstCharAsUppercase := firstChar asUppercase. |
6e690f4b79f1
asLowercase/asUppercase and friends care for 16-bit chars
Claus Gittinger <cg@exept.de>
parents:
7995
diff
changeset
|
4345 |
firstChar == firstCharAsUppercase ifTrue:[ ^ self]. |
6e690f4b79f1
asLowercase/asUppercase and friends care for 16-bit chars
Claus Gittinger <cg@exept.de>
parents:
7995
diff
changeset
|
4346 |
|
12679 | 4347 |
"/ 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
|
4348 |
firstCharAsUppercase bitsPerCharacter > self bitsPerCharacter ifTrue:[ |
20874 | 4349 |
newString := firstCharAsUppercase stringSpecies fromString:self. |
8037
8cce25b1059c
care for wide chars in asXXXFirst
Claus Gittinger <cg@exept.de>
parents:
8032
diff
changeset
|
4350 |
] ifFalse:[ |
20874 | 4351 |
newString := self stringSpecies fromString:self. |
8037
8cce25b1059c
care for wide chars in asXXXFirst
Claus Gittinger <cg@exept.de>
parents:
8032
diff
changeset
|
4352 |
]. |
8003
6e690f4b79f1
asLowercase/asUppercase and friends care for 16-bit chars
Claus Gittinger <cg@exept.de>
parents:
7995
diff
changeset
|
4353 |
newString at:1 put:firstCharAsUppercase. |
608 | 4354 |
^ newString |
4355 |
||
4356 |
" |
|
8874 | 4357 |
'helloWorld' asUppercase |
4358 |
'helloWorld' asUppercaseFirst |
|
4359 |
'HelloWorld' asUppercaseFirst |
|
608 | 4360 |
" |
9192 | 4361 |
! |
4362 |
||
11828 | 4363 |
asUppercaseLast |
4364 |
"return a copy of myself where the last character is |
|
4365 |
converted to uppercase." |
|
4366 |
||
4367 |
|newString sz| |
|
4368 |
||
4369 |
sz := self size. |
|
4370 |
newString := self copyFrom:1 to:sz. |
|
4371 |
sz > 0 ifTrue:[ |
|
20874 | 4372 |
newString at:sz put:(newString at:sz) asUppercase |
11828 | 4373 |
]. |
4374 |
^ newString |
|
4375 |
||
4376 |
" |
|
12177 | 4377 |
'HelloWorld' asUppercase |
4378 |
'HelloWorld' asUppercaseLast |
|
11828 | 4379 |
" |
4380 |
! |
|
4381 |
||
17021 | 4382 |
asUtcTimestamp |
4383 |
"convert the receiver into an UtcTimestamp (Time is interpreted in the UTC timezone). |
|
4384 |
Notice, that errors may occur during the read, |
|
4385 |
so you better setup some exception handler when using this method." |
|
4386 |
||
21030 | 4387 |
^ UtcTimestamp readFrom:self |
17021 | 4388 |
|
4389 |
" |
|
4390 |
'2014-11-10 21:30:22.444' asUtcTimestamp |
|
4391 |
" |
|
4392 |
! |
|
4393 |
||
23121 | 4394 |
asVersionNumberCollection |
4395 |
"Convert a string like 1.2.3a to an Array of numbers (or string, if not a number). |
|
4396 |
Remove zeroes from the end." |
|
4397 |
||
4398 |
^ (self asCollectionOfSubstringsSeparatedByAny:'.-_') asVersionNumberCollection. |
|
4399 |
||
4400 |
" |
|
4401 |
'1' asVersionNumberCollection. |
|
4402 |
'1.1' asVersionNumberCollection. |
|
4403 |
'1.1a' asVersionNumberCollection. |
|
4404 |
'1.1.0' asVersionNumberCollection. |
|
4405 |
'01.01.0' asVersionNumberCollection. |
|
4406 |
'expecco_18_1_0' asVersionNumberCollection. |
|
4407 |
" |
|
4408 |
||
4409 |
"Created: / 20-06-2018 / 11:23:53 / Stefan Vogel" |
|
4410 |
"Modified: / 20-06-2018 / 19:24:36 / Stefan Vogel" |
|
4411 |
! |
|
4412 |
||
11062 | 4413 |
literalArrayEncoding |
4414 |
"encode myself as an array literal, from which a copy of the receiver |
|
4415 |
can be reconstructed with #decodeAsLiteralArray." |
|
4416 |
||
4417 |
^ self |
|
4418 |
||
4419 |
"Modified: 1.9.1995 / 02:25:45 / claus" |
|
4420 |
"Modified: 22.4.1996 / 13:00:50 / cg" |
|
4421 |
! |
|
4422 |
||
9192 | 4423 |
string |
4424 |
"return the receiver - for ST-80 compatibility" |
|
4425 |
||
4426 |
^ self |
|
4427 |
||
4428 |
"Modified: 27.4.1996 / 13:29:30 / cg" |
|
4429 |
! ! |
|
4430 |
||
4431 |
!CharacterArray methodsFor:'copying'! |
|
4432 |
||
22528 | 4433 |
, aStringOrCharacterOrAnyOther |
2720
98c21d9a07c0
allow mixed strings to be concatenated using comma
Claus Gittinger <cg@exept.de>
parents:
2692
diff
changeset
|
4434 |
"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
|
4435 |
This is nonStandard, but convenient" |
2f14bbd792a9
, now accepts a character-argument
Claus Gittinger <cg@exept.de>
parents:
1028
diff
changeset
|
4436 |
|
23998 | 4437 |
|stringifiedArg |
4438 |
newSpecies newString l1 l2 combinedLen| |
|
22528 | 4439 |
|
4440 |
aStringOrCharacterOrAnyOther isCharacter ifTrue:[ |
|
4441 |
^ self copyWith:aStringOrCharacterOrAnyOther |
|
4442 |
]. |
|
4443 |
aStringOrCharacterOrAnyOther isText ifTrue:[ |
|
4444 |
^ aStringOrCharacterOrAnyOther concatenateFromString:self |
|
4445 |
]. |
|
4446 |
stringifiedArg := aStringOrCharacterOrAnyOther asString. |
|
4447 |
stringifiedArg isString ifTrue:[ |
|
24037 | 4448 |
(stringifiedArg bitsPerCharacter) > (self bitsPerCharacter) ifTrue:[ |
4449 |
newSpecies := stringifiedArg species. |
|
4450 |
] ifFalse:[ |
|
4451 |
newSpecies := self species. |
|
4452 |
]. |
|
23998 | 4453 |
l1 := self size. |
4454 |
l2 := stringifiedArg size. |
|
4455 |
combinedLen := l1 + l2. |
|
4456 |
newString := newSpecies uninitializedNew:combinedLen. |
|
24037 | 4457 |
newString |
4458 |
replaceFrom:1 to:l1 with:self startingAt:1; |
|
4459 |
replaceFrom:l1+1 to:combinedLen with:stringifiedArg startingAt:1. |
|
23998 | 4460 |
^ newString |
2720
98c21d9a07c0
allow mixed strings to be concatenated using comma
Claus Gittinger <cg@exept.de>
parents:
2692
diff
changeset
|
4461 |
]. |
22528 | 4462 |
^ super , stringifiedArg |
1050
2f14bbd792a9
, now accepts a character-argument
Claus Gittinger <cg@exept.de>
parents:
1028
diff
changeset
|
4463 |
|
2f14bbd792a9
, now accepts a character-argument
Claus Gittinger <cg@exept.de>
parents:
1028
diff
changeset
|
4464 |
" |
8874 | 4465 |
'hello' , $1 |
24037 | 4466 |
'hello' , (Character codePoint:1046) |
8874 | 4467 |
'hello' , '1' |
4468 |
'hello' , (' world' asText allBold) |
|
4469 |
'hello' , (JISEncodedString fromString:' world') |
|
2720
98c21d9a07c0
allow mixed strings to be concatenated using comma
Claus Gittinger <cg@exept.de>
parents:
2692
diff
changeset
|
4470 |
(JISEncodedString fromString:'hello') , ' world' |
1411 | 4471 |
|
1422 | 4472 |
Transcript showCR: |
20874 | 4473 |
(Text string:'hello' emphasis:#italic) , (Text string:' world' emphasis:#bold) |
22528 | 4474 |
|
4475 |
'hello',123,'world' |
|
4476 |
'hello' allBold,123,'world' |
|
4477 |
'hello',123,'world' allBold |
|
1050
2f14bbd792a9
, now accepts a character-argument
Claus Gittinger <cg@exept.de>
parents:
1028
diff
changeset
|
4478 |
" |
2f14bbd792a9
, now accepts a character-argument
Claus Gittinger <cg@exept.de>
parents:
1028
diff
changeset
|
4479 |
|
23997 | 4480 |
"Modified: / 28-06-1997 / 00:13:17 / cg" |
23998 | 4481 |
"Modified: / 27-03-2019 / 19:37:34 / Claus Gittinger" |
24033 | 4482 |
"Modified: / 02-04-2019 / 10:39:28 / Maren" |
24037 | 4483 |
"Modified (comment): / 02-04-2019 / 11:36:00 / Stefan Vogel" |
9192 | 4484 |
! |
4485 |
||
4486 |
,, aString |
|
7020 | 4487 |
"concatenate with a newLine in between" |
6965 | 4488 |
|
4489 |
^ (self copyWith:Character cr) , aString |
|
4490 |
||
4491 |
" |
|
7020 | 4492 |
'hello' ,, 'world' |
6965 | 4493 |
" |
23388 | 4494 |
|
4495 |
"Modified (comment): / 25-09-2018 / 10:21:45 / Claus Gittinger" |
|
6965 | 4496 |
! |
4497 |
||
11329 | 4498 |
chopTo:maxLen |
19464 | 4499 |
"if the receiver's size is less or equal to maxLen, return it. |
11329 | 4500 |
Otherwise, return a copy of the receiver, where some characters |
4501 |
in the middle have been removed for a total string length |
|
4502 |
of maxLen." |
|
4503 |
||
4504 |
|sz n1 n2| |
|
4505 |
||
4506 |
(sz := self size) <= maxLen ifTrue:[ ^ self ]. |
|
4507 |
||
4508 |
n1 := n2 := maxLen // 2. |
|
4509 |
maxLen odd ifTrue:[ |
|
20874 | 4510 |
n2 := n1 + 1 |
11329 | 4511 |
]. |
4512 |
^ (self copyFrom:1 to:n1) , (self copyFrom:sz - n2 + 1) |
|
4513 |
||
4514 |
" |
|
4515 |
'12345678901234' chopTo:15 |
|
4516 |
'123456789012345' chopTo:15 |
|
4517 |
'1234567890123456' chopTo:15 |
|
4518 |
'aShortString' chopTo:15 |
|
4519 |
'aVeryLongNameForAStringThatShouldBeShortened' chopTo:15 |
|
4520 |
" |
|
4521 |
||
4522 |
"Modified: / 24-10-2006 / 12:32:01 / cg" |
|
4523 |
! |
|
4524 |
||
4525 |
contractAtBeginningTo:maxLen |
|
19464 | 4526 |
"if the receiver's size is less or equal to maxLen, return it. |
11329 | 4527 |
Otherwise, return a copy of the receiver, where some characters |
4528 |
at the beginning have been replaced by '...' for a total string length |
|
4529 |
of maxLen. Can be used to abbreviate long entries in tables." |
|
4530 |
||
4531 |
|sz| |
|
4532 |
||
4533 |
(sz := self size) <= maxLen ifTrue:[ ^ self ]. |
|
4534 |
||
4535 |
^ '...' , (self copyFrom:(sz - (maxLen - 4))) |
|
4536 |
||
4537 |
" |
|
4538 |
'12345678901234' contractAtBeginningTo:15 |
|
4539 |
'123456789012345' contractAtBeginningTo:15 |
|
4540 |
'1234567890123456' contractAtBeginningTo:15 |
|
4541 |
'aShortString' contractAtBeginningTo:15 |
|
4542 |
'aVeryLongNameForAStringThatShouldBeShortened' contractAtBeginningTo:15 |
|
4543 |
" |
|
4544 |
||
4545 |
"Modified: / 24-10-2006 / 12:32:13 / cg" |
|
4546 |
! |
|
4547 |
||
4548 |
contractAtEndTo:maxLen |
|
19464 | 4549 |
"if the receiver's size is less or equal to maxLen, return it. |
11329 | 4550 |
Otherwise, return a copy of the receiver, where some characters |
4551 |
at the end have been replaced by '...' for a total string length |
|
4552 |
of maxLen. Can be used to abbreviate long entries in tables." |
|
4553 |
||
21559 | 4554 |
(self size <= maxLen) ifTrue:[ ^ self ]. |
11329 | 4555 |
|
4556 |
^ (self copyTo:maxLen-3),'...' |
|
4557 |
||
4558 |
" |
|
4559 |
'12345678901234' contractAtEndTo:15 |
|
4560 |
'123456789012345' contractAtEndTo:15 |
|
4561 |
'1234567890123456' contractAtEndTo:15 |
|
4562 |
'aShortString' contractAtEndTo:15 |
|
4563 |
'aVeryLongNameForAStringThatShouldBeShortened' contractAtEndTo:15 |
|
4564 |
" |
|
4565 |
||
4566 |
"Modified: / 24-10-2006 / 12:32:26 / cg" |
|
21559 | 4567 |
"Modified: / 23-02-2017 / 21:32:21 / mawalch" |
11329 | 4568 |
! |
4569 |
||
4570 |
contractLeftTo:maxLen |
|
19464 | 4571 |
"if the receiver's size is less or equal to maxLen, return it. |
11329 | 4572 |
Otherwise, return a copy of the receiver, where some characters |
4573 |
near the first quarter have been replaced by '...' for a total string length |
|
12177 | 4574 |
of maxLen. |
4575 |
Very similar to contractTo:, but better to abbreviate long filename entries, |
|
11329 | 4576 |
where the right part is of more use than the left." |
4577 |
||
4578 |
|sz "{ SmallInteger }" |
|
4579 |
halfSize quarterSize "{ SmallInteger }" |
|
4580 |
leftEnd rightEnd rightStart| |
|
4581 |
||
4582 |
(sz := self size) <= maxLen ifTrue:[ ^ self ]. |
|
4583 |
||
4584 |
halfSize := maxLen // 2. |
|
4585 |
quarterSize := maxLen // 4. |
|
4586 |
leftEnd := quarterSize-1. |
|
4587 |
rightEnd := maxLen - leftEnd - 3. |
|
4588 |
rightStart := sz - rightEnd + 1. |
|
4589 |
^ (self copyTo:leftEnd),'...',(self copyFrom:rightStart) |
|
4590 |
||
4591 |
" |
|
12177 | 4592 |
'12345678901234' contractLeftTo:15 |
4593 |
'123456789012345' contractLeftTo:15 |
|
4594 |
'1234567890123456' contractLeftTo:15 |
|
4595 |
'aShortString' contractLeftTo:15 |
|
4596 |
'aVeryLongNameForAStringThatShouldBeShortened' contractLeftTo:15 |
|
11329 | 4597 |
'C:\Dokumente und Einstellungen\cg\work\bosch\dapas\hw_schnittstellen\DAPAS__HpibDLL.st' contractLeftTo:40 |
4598 |
" |
|
4599 |
! |
|
4600 |
||
4601 |
contractTo:maxLen |
|
19464 | 4602 |
"if the receiver's size is less or equal to maxLen, return it. |
11329 | 4603 |
Otherwise, return a copy of the receiver, where some characters |
4604 |
in the middle have been replaced by '...' for a total string length |
|
4605 |
of maxLen. Can be used to abbreviate long entries in tables." |
|
4606 |
||
13362 | 4607 |
|sz "{ SmallInteger }" leftSize rightSize| |
11329 | 4608 |
|
4609 |
(sz := self size) <= maxLen ifTrue:[ ^ self ]. |
|
4610 |
||
13362 | 4611 |
rightSize := maxLen // 2. |
4612 |
leftSize := maxLen - rightSize. |
|
13822 | 4613 |
leftSize := leftSize - 2. |
4614 |
rightSize := rightSize - 1. |
|
13362 | 4615 |
^ (self copyTo:leftSize),'...',(self copyFrom:(sz+1-rightSize)) |
4616 |
||
4617 |
" |
|
13882
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
4618 |
'12345678901234' contractTo:15 |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
4619 |
'123456789012345' contractTo:15 |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
4620 |
'1234567890123456' contractTo:15 |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
4621 |
'12345678901234567' contractTo:15 |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
4622 |
'123456789012345678' contractTo:15 |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
4623 |
'aShortString' contractTo:15 |
11329 | 4624 |
'aVeryLongNameForAStringThatShouldBeShortened' contractTo:15 |
4625 |
'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
|
4626 |
('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
|
4627 |
('1234567890123456789012345678901234567' contractTo:29) size |
13362 | 4628 |
" |
4629 |
||
13822 | 4630 |
"Modified (comment): / 24-11-2011 / 19:17:46 / cg" |
11329 | 4631 |
! |
4632 |
||
23061 | 4633 |
copyBetween:string1 and:string2 caseSensitive:caseSensitive |
4634 |
"return the substring between two matching sub-strings. |
|
4635 |
Returns nil, if not both subStrings are present in the receiver." |
|
4636 |
||
4637 |
|idx1 leftIndex rightIndex| |
|
4638 |
||
4639 |
idx1 := self indexOfString:string1 caseSensitive:caseSensitive. |
|
4640 |
idx1 ~~ 0 ifTrue:[ |
|
4641 |
leftIndex := (idx1 + string1 size). |
|
4642 |
rightIndex := self indexOfString:string2 caseSensitive:caseSensitive startingAt:leftIndex. |
|
4643 |
rightIndex ~~ 0 ifTrue:[ |
|
4644 |
^ self copyFrom:leftIndex to:rightIndex-1 |
|
4645 |
] |
|
4646 |
]. |
|
4647 |
^ nil |
|
4648 |
||
4649 |
" |
|
4650 |
'hello funny world' copyBetween:'hello' and:'world' caseSensitive:true -> ' funny ' |
|
4651 |
'helloworld' copyBetween:'hello' and:'world' caseSensitive:true -> '' |
|
4652 |
'helloorld' copyBetween:'hello' and:'world' caseSensitive:true -> nil |
|
4653 |
||
4654 |
'hello funny World' copyBetween:'hello' and:'world' caseSensitive:true -> nil |
|
4655 |
'helloWorld' copyBetween:'hello' and:'world' caseSensitive:true -> nil |
|
4656 |
||
4657 |
'hello funny World' copyBetween:'hello' and:'world' caseSensitive:false -> ' funny ' |
|
4658 |
'helloWorld' copyBetween:'hello' and:'world' caseSensitive:false -> '' |
|
4659 |
" |
|
4660 |
||
4661 |
"Created: / 08-06-2018 / 14:50:17 / Claus Gittinger" |
|
4662 |
! |
|
4663 |
||
16202 | 4664 |
copyReplaceAll:oldElement with:newElement |
4665 |
"return a copy of the receiver as a string, where all elements equal to oldElement |
|
4666 |
have been replaced by newElement." |
|
4667 |
||
4668 |
"/ ANSI seems to allow a sequence to be replaced by another sequence, |
|
4669 |
"/ whereas the old ST80 meant replace all occurrences... - sigh. |
|
22231 | 4670 |
oldElement isString ifTrue:[ |
4671 |
newElement isString ifTrue:[ |
|
20874 | 4672 |
^ self copyReplaceString:oldElement withString:newElement. |
4673 |
]. |
|
23552 | 4674 |
self halt:'check if this is legal (trying to replace string by non-string)'. |
16202 | 4675 |
]. |
22231 | 4676 |
newElement isString ifTrue:[ |
23552 | 4677 |
self halt:'check if this is legal (trying to replace non-string by string)'. |
16202 | 4678 |
]. |
16212 | 4679 |
^ super copyReplaceAll:oldElement with:newElement |
23552 | 4680 |
|
4681 |
"Modified: / 03-12-2018 / 16:03:32 / Stefan Vogel" |
|
16202 | 4682 |
! |
4683 |
||
8347 | 4684 |
copyReplaceString:subString withString:newString |
4685 |
"return a copy of the receiver, with all sequences of subString replaced |
|
4686 |
by newString (i.e. slice in the newString in place of the oldString)." |
|
4687 |
||
4688 |
|tmpStream idx idx1| |
|
4689 |
||
15523 | 4690 |
tmpStream := self species writeStream. |
8347 | 4691 |
idx := 1. |
4692 |
[idx ~~ 0] whileTrue:[ |
|
20874 | 4693 |
idx1 := idx. |
4694 |
idx := self indexOfSubCollection:subString startingAt:idx. |
|
4695 |
idx ~~ 0 ifTrue:[ |
|
4696 |
tmpStream nextPutAll:(self copyFrom:idx1 to:idx-1). |
|
4697 |
tmpStream nextPutAll:newString. |
|
4698 |
idx := idx + subString size |
|
4699 |
] |
|
8347 | 4700 |
]. |
4701 |
tmpStream nextPutAll:(self copyFrom:idx1). |
|
4702 |
^ tmpStream contents |
|
4703 |
||
4704 |
" |
|
9306 | 4705 |
'12345678901234567890' copyReplaceString:'123' withString:'OneTwoThree' |
4706 |
'12345678901234567890' copyReplaceString:'123' withString:'*' |
|
4707 |
'12345678901234567890' copyReplaceString:'234' withString:'foo' |
|
22231 | 4708 |
'12345678901234567890' asUnicode16String copyReplaceString:'234' asUnicode16String withString:'foo' |
4709 |
'12345678901234567890' asUnicode16String copyReplaceString:'234' withString:'foo' |
|
4710 |
'12345678901234567890' asUnicode16String copyReplaceString:'234' withString:'foo' asUnicode16String |
|
9306 | 4711 |
|
4712 |
('a string with spaces' copyReplaceAll:$ withAll:' foo ') |
|
20874 | 4713 |
copyReplaceString:'foo' withString:'bar' |
8347 | 4714 |
" |
4715 |
||
4716 |
"Modified: / 31-05-1999 / 12:33:59 / cg" |
|
4717 |
"Created: / 12-05-2004 / 12:00:00 / cg" |
|
4718 |
! |
|
4719 |
||
8040
f10cca439066
copyWith for mixed-width string/character
Claus Gittinger <cg@exept.de>
parents:
8037
diff
changeset
|
4720 |
copyWith:aCharacter |
19464 | 4721 |
"return a new string containing the receiver's characters |
8874 | 4722 |
and the single new character, aCharacter. |
8040
f10cca439066
copyWith for mixed-width string/character
Claus Gittinger <cg@exept.de>
parents:
8037
diff
changeset
|
4723 |
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
|
4724 |
as argument, but equivalent to copy-and-addLast. |
8874 | 4725 |
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
|
4726 |
(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
|
4727 |
|
f10cca439066
copyWith for mixed-width string/character
Claus Gittinger <cg@exept.de>
parents:
8037
diff
changeset
|
4728 |
|sz newString| |
f10cca439066
copyWith for mixed-width string/character
Claus Gittinger <cg@exept.de>
parents:
8037
diff
changeset
|
4729 |
|
f10cca439066
copyWith for mixed-width string/character
Claus Gittinger <cg@exept.de>
parents:
8037
diff
changeset
|
4730 |
aCharacter bitsPerCharacter > self bitsPerCharacter ifTrue:[ |
20874 | 4731 |
sz := self size. |
4732 |
newString := aCharacter stringSpecies new:sz + 1. |
|
4733 |
newString replaceFrom:1 to:sz with:self startingAt:1. |
|
4734 |
newString at:sz+1 put:aCharacter. |
|
4735 |
^ newString. |
|
8040
f10cca439066
copyWith for mixed-width string/character
Claus Gittinger <cg@exept.de>
parents:
8037
diff
changeset
|
4736 |
]. |
f10cca439066
copyWith for mixed-width string/character
Claus Gittinger <cg@exept.de>
parents:
8037
diff
changeset
|
4737 |
^ super copyWith:aCharacter |
17241 | 4738 |
! |
4739 |
||
4740 |
restAfter:keyword withoutSeparators:strip |
|
23392 | 4741 |
"compare the left part of the receiver with keyword, |
4742 |
if it matches return the right rest. |
|
17241 | 4743 |
Finally, if strip is true, remove whiteSpace. |
23392 | 4744 |
If the string does not start with keyword, return nil. |
4745 |
||
17241 | 4746 |
This method is used to match and extract lines of the form: |
20874 | 4747 |
something: rest |
17241 | 4748 |
where we are interested in rest, but only if the receiver string |
4749 |
begins with something. |
|
4750 |
||
4751 |
You may wonder why such a specialized method exists here |
|
4752 |
- this is so common when processing mailboxes, |
|
4753 |
rcs files, nntp/pop3 responses, that is was considered worth |
|
4754 |
a special method here to avoid having the code below a hundred |
|
4755 |
times in variuos places." |
|
4756 |
||
4757 |
|rest| |
|
4758 |
||
4759 |
(self startsWith:keyword) ifTrue:[ |
|
20874 | 4760 |
rest := self copyFrom:(keyword size + 1). |
4761 |
strip ifTrue:[ |
|
4762 |
rest := rest withoutSeparators |
|
4763 |
]. |
|
4764 |
^ rest |
|
17241 | 4765 |
]. |
4766 |
^ nil |
|
4767 |
||
4768 |
" |
|
4769 |
'foo: hello world' restAfter:'foo:' withoutSeparators:true |
|
4770 |
'funny: something' restAfter:'foo:' withoutSeparators:true |
|
4771 |
||
4772 |
'foo: hello world ' restAfter:'foo:' withoutSeparators:true |
|
4773 |
'foo: hello world ' restAfter:'foo:' withoutSeparators:false |
|
4774 |
" |
|
4775 |
||
23392 | 4776 |
"Created: / 25-11-1995 / 11:04:18 / cg" |
4777 |
"Modified (comment): / 27-09-2018 / 10:01:55 / Claus Gittinger" |
|
17241 | 4778 |
! |
4779 |
||
4780 |
splitAtString:subString withoutSeparators:strip |
|
4781 |
"If the receiver is of the form: |
|
20874 | 4782 |
<left><subString><right> |
17241 | 4783 |
return a collection containing left and right only. |
4784 |
If strip is true, remove whiteSpace in the returned substrings." |
|
4785 |
||
4786 |
|idx left right| |
|
4787 |
||
4788 |
(idx := self indexOfSubCollection:subString) ~~ 0 ifTrue:[ |
|
20874 | 4789 |
left := self copyTo:(idx - 1). |
4790 |
right := self copyFrom:(idx + subString size). |
|
4791 |
strip ifTrue:[ |
|
4792 |
left := left withoutSeparators. |
|
4793 |
right := right withoutSeparators. |
|
4794 |
]. |
|
4795 |
^ StringCollection with:left with:right |
|
17241 | 4796 |
]. |
23001 | 4797 |
self proceedableError:'substring not present in receiver'. |
17241 | 4798 |
^ self |
4799 |
||
4800 |
" |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
4801 |
'hello -> world' splitAtString:'->' withoutSeparators:false |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
4802 |
'hello -> world' splitAtString:'->' withoutSeparators:true |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
4803 |
'hello -> ' splitAtString:'->' withoutSeparators:true |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
4804 |
'hello > error' splitAtString:'->' withoutSeparators:true |
17241 | 4805 |
" |
4806 |
||
23001 | 4807 |
"Created: / 25-11-1995 / 11:04:18 / cg" |
4808 |
"Modified: / 24-05-2018 / 14:55:52 / Claus Gittinger" |
|
608 | 4809 |
! ! |
4810 |
||
4811 |
!CharacterArray methodsFor:'displaying'! |
|
4812 |
||
2762 | 4813 |
displayOn:aGC x:x y:y from:start to:stop |
4814 |
"display the receiver on a GC" |
|
4815 |
||
12171 | 4816 |
self displayOn:aGC x:x y:y from:start to:stop opaque:false |
4817 |
||
4818 |
"Modified: 12.5.1996 / 12:49:33 / cg" |
|
4819 |
! |
|
4820 |
||
4821 |
displayOn:aGC x:x y:y from:start to:stop opaque:opaque |
|
4822 |
"display the receiver on a GC" |
|
4823 |
||
2762 | 4824 |
"q&d hack" |
4825 |
||
12177 | 4826 |
(start == 1 and:[stop == self size]) ifTrue:[ |
20874 | 4827 |
self displayOn:aGC x:x y:y opaque:opaque. |
12177 | 4828 |
] ifFalse:[ |
20874 | 4829 |
(self copyFrom:start to:stop) displayOn:aGC x:x y:y opaque:opaque. |
12177 | 4830 |
]. |
2762 | 4831 |
! |
4832 |
||
7082 | 4833 |
displayOn:aGc x:x y:y opaque:opaque |
2762 | 4834 |
"display the receiver in a graphicsContext - this method allows |
4835 |
strings to be used like DisplayObjects." |
|
4836 |
||
4837 |
|s| |
|
4838 |
||
4839 |
s := self string. |
|
7082 | 4840 |
opaque ifTrue:[ |
20874 | 4841 |
aGc displayOpaqueString:s x:x y:y. |
2762 | 4842 |
] ifFalse:[ |
20874 | 4843 |
aGc displayString:s x:x y:y. |
2762 | 4844 |
]. |
1381 | 4845 |
|
4846 |
"Modified: 11.5.1996 / 14:42:48 / cg" |
|
4847 |
! |
|
4848 |
||
2762 | 4849 |
displayOpaqueOn:aGC x:x y:y from:start to:stop |
4850 |
"display the receiver on a GC" |
|
4851 |
||
12171 | 4852 |
self displayOn:aGC x:x y:y from:start to:stop opaque:true |
608 | 4853 |
! ! |
4854 |
||
1375
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
4855 |
!CharacterArray methodsFor:'emphasis'! |
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
4856 |
|
11632
b82e9f3cabd1
new: #actionForAll: #emphasisAtPoint:on:
Stefan Vogel <sv@exept.de>
parents:
11630
diff
changeset
|
4857 |
actionForAll:aBlock |
12326
c67f12213ddb
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
12319
diff
changeset
|
4858 |
"change the action block of all characters. |
c67f12213ddb
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
12319
diff
changeset
|
4859 |
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
|
4860 |
|
b82e9f3cabd1
new: #actionForAll: #emphasisAtPoint:on:
Stefan Vogel <sv@exept.de>
parents:
11630
diff
changeset
|
4861 |
^ self asText actionForAll:aBlock |
b82e9f3cabd1
new: #actionForAll: #emphasisAtPoint:on:
Stefan Vogel <sv@exept.de>
parents:
11630
diff
changeset
|
4862 |
! |
b82e9f3cabd1
new: #actionForAll: #emphasisAtPoint:on:
Stefan Vogel <sv@exept.de>
parents:
11630
diff
changeset
|
4863 |
|
5732 | 4864 |
allBold |
12326
c67f12213ddb
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
12319
diff
changeset
|
4865 |
"return a text object representing the receiver, but all boldified" |
5732 | 4866 |
|
8874 | 4867 |
"this test allows for small non-gui apps to be built without libbasic2 (where Text is)" |
6187 | 4868 |
Text isNil ifTrue:[^ self]. |
5732 | 4869 |
^ self asText allBold |
4870 |
||
4871 |
" |
|
4872 |
Transcript showCR:'hello' asText allBold |
|
4873 |
Transcript showCR:'hello' allBold |
|
4874 |
" |
|
4875 |
! |
|
4876 |
||
5898
7bc4f1593b4e
allItalic now understood by CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5848
diff
changeset
|
4877 |
allItalic |
12326
c67f12213ddb
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
12319
diff
changeset
|
4878 |
"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
|
4879 |
|
8874 | 4880 |
"this test allows for small non-gui apps to be built without libbasic2 (where Text is)" |
6187 | 4881 |
Text isNil ifTrue:[^ self]. |
5898
7bc4f1593b4e
allItalic now understood by CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5848
diff
changeset
|
4882 |
^ self asText allItalic |
7bc4f1593b4e
allItalic now understood by CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5848
diff
changeset
|
4883 |
|
7bc4f1593b4e
allItalic now understood by CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5848
diff
changeset
|
4884 |
" |
7bc4f1593b4e
allItalic now understood by CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5848
diff
changeset
|
4885 |
Transcript showCR:'hello' asText allItalic |
7bc4f1593b4e
allItalic now understood by CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5848
diff
changeset
|
4886 |
Transcript showCR:'hello' allItalic |
7bc4f1593b4e
allItalic now understood by CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5848
diff
changeset
|
4887 |
" |
7bc4f1593b4e
allItalic now understood by CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5848
diff
changeset
|
4888 |
! |
7bc4f1593b4e
allItalic now understood by CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5848
diff
changeset
|
4889 |
|
23903 | 4890 |
allNonBold |
4891 |
"make all characters non-bold; |
|
4892 |
I already have no emphasis, so the receiver string is returned" |
|
4893 |
||
4894 |
^ self |
|
4895 |
||
4896 |
"Created: / 13-03-2019 / 21:02:45 / Claus Gittinger" |
|
4897 |
! |
|
4898 |
||
11345 | 4899 |
allStrikedOut |
12326
c67f12213ddb
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
12319
diff
changeset
|
4900 |
"return a text object representing the receiver, but all in strikeout" |
11345 | 4901 |
|
4902 |
"this test allows for small non-gui apps to be built without libbasic2 (where Text is)" |
|
4903 |
Text isNil ifTrue:[^ self]. |
|
4904 |
^ self asText allStrikedOut |
|
4905 |
||
4906 |
" |
|
4907 |
Transcript showCR:'hello' asText allStrikedOut |
|
4908 |
Transcript showCR:'hello' allStrikedOut |
|
4909 |
" |
|
4910 |
! |
|
4911 |
||
10862 | 4912 |
allUnderlined |
12326
c67f12213ddb
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
12319
diff
changeset
|
4913 |
"return a text object representing the receiver, but all with underline" |
10862 | 4914 |
|
4915 |
"this test allows for small non-gui apps to be built without libbasic2 (where Text is)" |
|
4916 |
Text isNil ifTrue:[^ self]. |
|
4917 |
^ self asText allUnderlined |
|
4918 |
||
4919 |
" |
|
4920 |
Transcript showCR:'hello' asText allUnderlined |
|
4921 |
Transcript showCR:'hello' allUnderlined |
|
4922 |
" |
|
4923 |
! |
|
4924 |
||
19774 | 4925 |
asActionLinkTo:aBlock |
4926 |
"change the action block of all characters and colorize as a link. |
|
4927 |
Some widgets use this like a href if clicked onto the text |
|
4928 |
(for example, the system-browser's info at the bottom is such a widget)." |
|
4929 |
||
4930 |
"/ the blue may become a user-setting some time later.. |
|
4931 |
^ (self actionForAll:aBlock) withColor:(Color blue) |
|
4932 |
! |
|
4933 |
||
6228 | 4934 |
colorizeAllWith:aColor |
12326
c67f12213ddb
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
12319
diff
changeset
|
4935 |
"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
|
4936 |
|
c67f12213ddb
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
12319
diff
changeset
|
4937 |
"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
|
4938 |
Text isNil ifTrue:[^ self]. |
6228 | 4939 |
^ self asText colorizeAllWith:aColor |
4940 |
||
4941 |
" |
|
4942 |
Transcript showCR:('hello' colorizeAllWith:Color red) |
|
4943 |
Transcript showCR:('world' colorizeAllWith:Color green darkened) |
|
4944 |
" |
|
4945 |
! |
|
4946 |
||
12330
f9b57b0b8a8b
added: #colorizeAllWith:on:
Claus Gittinger <cg@exept.de>
parents:
12326
diff
changeset
|
4947 |
colorizeAllWith:fgColor on:bgColor |
f9b57b0b8a8b
added: #colorizeAllWith:on:
Claus Gittinger <cg@exept.de>
parents:
12326
diff
changeset
|
4948 |
"return a text object representing the receiver, but all colorized with |
f9b57b0b8a8b
added: #colorizeAllWith:on:
Claus Gittinger <cg@exept.de>
parents:
12326
diff
changeset
|
4949 |
both fg and background color" |
f9b57b0b8a8b
added: #colorizeAllWith:on:
Claus Gittinger <cg@exept.de>
parents:
12326
diff
changeset
|
4950 |
|
f9b57b0b8a8b
added: #colorizeAllWith:on:
Claus Gittinger <cg@exept.de>
parents:
12326
diff
changeset
|
4951 |
"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
|
4952 |
Text isNil ifTrue:[^ self]. |
f9b57b0b8a8b
added: #colorizeAllWith:on:
Claus Gittinger <cg@exept.de>
parents:
12326
diff
changeset
|
4953 |
^ self asText colorizeAllWith:fgColor on:bgColor |
f9b57b0b8a8b
added: #colorizeAllWith:on:
Claus Gittinger <cg@exept.de>
parents:
12326
diff
changeset
|
4954 |
|
f9b57b0b8a8b
added: #colorizeAllWith:on:
Claus Gittinger <cg@exept.de>
parents:
12326
diff
changeset
|
4955 |
" |
f9b57b0b8a8b
added: #colorizeAllWith:on:
Claus Gittinger <cg@exept.de>
parents:
12326
diff
changeset
|
4956 |
Transcript showCR:('hello' colorizeAllWith:Color red on:Color yellow) |
f9b57b0b8a8b
added: #colorizeAllWith:on:
Claus Gittinger <cg@exept.de>
parents:
12326
diff
changeset
|
4957 |
Transcript showCR:('world' colorizeAllWith:Color red) |
f9b57b0b8a8b
added: #colorizeAllWith:on:
Claus Gittinger <cg@exept.de>
parents:
12326
diff
changeset
|
4958 |
" |
f9b57b0b8a8b
added: #colorizeAllWith:on:
Claus Gittinger <cg@exept.de>
parents:
12326
diff
changeset
|
4959 |
! |
f9b57b0b8a8b
added: #colorizeAllWith:on:
Claus Gittinger <cg@exept.de>
parents:
12326
diff
changeset
|
4960 |
|
1396
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4961 |
emphasis |
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4962 |
"return the emphasis. |
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4963 |
Since characterArrays do not hold any emphasis information, |
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4964 |
nil (no emphasis) is returned here." |
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4965 |
|
23060 | 4966 |
^ RunArray new:(self size) withAll:nil |
4967 |
||
4968 |
"Created: / 14-05-1996 / 13:58:58 / cg" |
|
4969 |
"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
|
4970 |
! |
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4971 |
|
6655 | 4972 |
emphasis:emphasisCollection |
4973 |
^ self asText emphasis:emphasisCollection |
|
4974 |
||
4975 |
" |
|
4976 |
Transcript showCR:('hello' emphasis:#(bold bold bold bold bold)) |
|
4977 |
" |
|
4978 |
! |
|
4979 |
||
1375
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
4980 |
emphasisAt:characterIndex |
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
4981 |
"return the emphasis at some index. |
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
4982 |
Since characterArrays do not hold any emphasis information, |
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
4983 |
nil (no emphasis) is returned here." |
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
4984 |
|
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
4985 |
^ nil |
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
4986 |
|
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
4987 |
"Created: 11.5.1996 / 14:13:27 / cg" |
1396
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4988 |
! |
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4989 |
|
11632
b82e9f3cabd1
new: #actionForAll: #emphasisAtPoint:on:
Stefan Vogel <sv@exept.de>
parents:
11630
diff
changeset
|
4990 |
emphasisAtPoint:aPoint on:aGC |
b82e9f3cabd1
new: #actionForAll: #emphasisAtPoint:on:
Stefan Vogel <sv@exept.de>
parents:
11630
diff
changeset
|
4991 |
"return the emphasis at a given point, or nil if there is none" |
b82e9f3cabd1
new: #actionForAll: #emphasisAtPoint:on:
Stefan Vogel <sv@exept.de>
parents:
11630
diff
changeset
|
4992 |
|
b82e9f3cabd1
new: #actionForAll: #emphasisAtPoint:on:
Stefan Vogel <sv@exept.de>
parents:
11630
diff
changeset
|
4993 |
^ nil |
b82e9f3cabd1
new: #actionForAll: #emphasisAtPoint:on:
Stefan Vogel <sv@exept.de>
parents:
11630
diff
changeset
|
4994 |
! |
b82e9f3cabd1
new: #actionForAll: #emphasisAtPoint:on:
Stefan Vogel <sv@exept.de>
parents:
11630
diff
changeset
|
4995 |
|
1396
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4996 |
emphasisCollection |
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4997 |
"return the emphasis. |
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4998 |
Since characterArrays do not hold any emphasis information, |
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4999 |
nil (no emphasis) is returned here." |
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
5000 |
|
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
5001 |
^ RunArray new:(self size) |
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
5002 |
|
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
5003 |
"Created: 14.5.1996 / 13:58:58 / cg" |
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
5004 |
"Modified: 14.5.1996 / 15:02:29 / cg" |
3404 | 5005 |
! |
5006 |
||
11919 | 5007 |
emphasiseFrom:start to:stop with:newEmphasis |
5008 |
"set to the emphasis within some range. return the receiver" |
|
5009 |
||
5010 |
^ self asText emphasiseFrom:start to:stop with:newEmphasis |
|
5011 |
||
5012 |
" |
|
12177 | 5013 |
'hello' emphasiseFrom:2 with:#italic |
11919 | 5014 |
|
5015 |
Transcript showCR:('hello' emphasiseFrom:2 with:#italic) |
|
5016 |
" |
|
5017 |
! |
|
5018 |
||
5019 |
emphasiseFrom:start with:newEmphasis |
|
5020 |
"set to the emphasis within some range. return the receiver" |
|
5021 |
||
5022 |
^ self emphasiseFrom:start to:(self size) with:newEmphasis |
|
5023 |
||
5024 |
" |
|
12177 | 5025 |
'hello' emphasiseFrom:2 with:#italic |
11919 | 5026 |
|
5027 |
Transcript showCR:('hello' emphasiseFrom:2 with:#italic) |
|
5028 |
" |
|
5029 |
! |
|
5030 |
||
3404 | 5031 |
emphasizeAllWith:emphasis |
12326
c67f12213ddb
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
12319
diff
changeset
|
5032 |
"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
|
5033 |
|
c67f12213ddb
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
12319
diff
changeset
|
5034 |
"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
|
5035 |
Text isNil ifTrue:[^ self]. |
3404 | 5036 |
^ self asText emphasizeAllWith:emphasis |
5037 |
||
5038 |
" |
|
5039 |
Transcript showCR:('hello' emphasizeAllWith:#bold) |
|
3599 | 5040 |
Transcript showCR:('hello' emphasizeAllWith:(#color -> Color red)) |
5041 |
Transcript showCR:('hello' emphasizeAllWith:(#color -> Color red)) |
|
5042 |
" |
|
5043 |
||
5044 |
"Modified: / 17.6.1998 / 12:51:44 / cg" |
|
5097 | 5045 |
! |
5046 |
||
5047 |
makeSelectorBoldIn:aClass |
|
14050 | 5048 |
"the receiver represents some source code for a method in aClass. |
5097 | 5049 |
Change myself to boldify the selector. |
14050 | 5050 |
Not yet implemented (could easily use the syntaxHighlighter for this ...). |
5051 |
For protocol compatibility with other smalltalks" |
|
5097 | 5052 |
|
5053 |
^ self |
|
5054 |
||
14050 | 5055 |
"Created: / 13-12-1999 / 21:49:24 / cg" |
5056 |
! |
|
5057 |
||
19762 | 5058 |
withColor:aColorOrColorSymbol |
20697 | 5059 |
"return a text object representing the receiver, but all colorized. |
5060 |
Usage of a colorSymbol is considered bad style (provided for backward compatibility); |
|
5061 |
please change to pass a proper color |
|
5062 |
(makes it easier to find color uses)" |
|
5063 |
||
5064 |
|color| |
|
5065 |
||
19762 | 5066 |
aColorOrColorSymbol isSymbol ifTrue:[ |
20697 | 5067 |
color := (Color perform:aColorOrColorSymbol) |
5068 |
] ifFalse:[ |
|
5069 |
color := aColorOrColorSymbol |
|
5070 |
]. |
|
5071 |
^ self colorizeAllWith:color |
|
19762 | 5072 |
|
5073 |
" |
|
5074 |
Transcript showCR:('hello' withColor:#red) |
|
20697 | 5075 |
Transcript showCR:('world' withColor:#blue) |
16697 | 5076 |
Transcript showCR:('hello' withColor:Color red) |
5077 |
Transcript showCR:('world' withColor:Color green darkened) |
|
5078 |
" |
|
5079 |
! |
|
5080 |
||
22911 | 5081 |
withColor:foregroundColorOrColorSymbol on:backgroundColorOrColorSymbol |
5082 |
"return a text object representing the receiver, but all colorized with both fg and bg. |
|
5083 |
Usage of a colorSymbol is considered bad style (provided for backward compatibility); |
|
5084 |
please change to pass a proper color |
|
5085 |
(makes it easier to find color uses)" |
|
5086 |
||
5087 |
|fgColor bgColor| |
|
5088 |
||
5089 |
foregroundColorOrColorSymbol isSymbol ifTrue:[ |
|
5090 |
fgColor := (Color perform:foregroundColorOrColorSymbol) |
|
5091 |
] ifFalse:[ |
|
5092 |
fgColor := foregroundColorOrColorSymbol |
|
5093 |
]. |
|
5094 |
backgroundColorOrColorSymbol isSymbol ifTrue:[ |
|
5095 |
bgColor := (Color perform:backgroundColorOrColorSymbol) |
|
5096 |
] ifFalse:[ |
|
5097 |
bgColor := backgroundColorOrColorSymbol |
|
5098 |
]. |
|
5099 |
^ self colorizeAllWith:fgColor on:bgColor |
|
5100 |
||
5101 |
" |
|
5102 |
Transcript showCR:('hello' withColor:#red on:#blue) |
|
5103 |
Transcript showCR:('hello' withColor:Color red on:Color blue) |
|
5104 |
" |
|
5105 |
! |
|
5106 |
||
14050 | 5107 |
withoutAnyColorEmphasis |
5108 |
"for protocol compatibility with Text" |
|
5109 |
||
5110 |
^ self |
|
5111 |
||
5112 |
"Created: / 06-03-2012 / 18:15:38 / cg" |
|
6653 | 5113 |
! |
5114 |
||
12396 | 5115 |
withoutBackgroundColorEmphasis |
14050 | 5116 |
"for protocol compatibility with Text" |
5117 |
||
12396 | 5118 |
^ self |
14050 | 5119 |
|
5120 |
"Modified (comment): / 06-03-2012 / 18:14:27 / cg" |
|
12396 | 5121 |
! |
5122 |
||
23903 | 5123 |
withoutEmphasis |
5124 |
"for protocol compatibility with Text" |
|
5125 |
||
5126 |
^ self |
|
5127 |
||
5128 |
"Created: / 13-03-2019 / 21:03:41 / Claus Gittinger" |
|
5129 |
! |
|
5130 |
||
6653 | 5131 |
withoutEmphasis:emphasisToRemove |
14050 | 5132 |
"for protocol compatibility with Text" |
5133 |
||
6653 | 5134 |
^ self |
14050 | 5135 |
|
5136 |
"Modified (comment): / 06-03-2012 / 18:14:29 / cg" |
|
12396 | 5137 |
! |
5138 |
||
5139 |
withoutForegroundColorEmphasis |
|
14050 | 5140 |
"for protocol compatibility with Text" |
5141 |
||
12396 | 5142 |
^ self |
14050 | 5143 |
|
5144 |
"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
|
5145 |
! ! |
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
5146 |
|
7265 | 5147 |
!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
|
5148 |
|
20013 | 5149 |
asDenormalizedUnicodeString |
5150 |
"return a new string containing the same characters, as a denormalized Unicode string. |
|
5151 |
This replaces diacritical chars (umlauts, accented chars etc) by |
|
5152 |
a sequence with combination characters. |
|
5153 |
(i.e. a plain character followed by a combining diacritical in the 0x03xx range)" |
|
5154 |
||
20014 | 5155 |
|map outStream mapChar| |
20013 | 5156 |
|
22406 | 5157 |
self containsNon7BitAscii ifFalse:[ |
5158 |
^ self "/ I cannot contain any diacritical chars |
|
5159 |
]. |
|
5160 |
||
20013 | 5161 |
map := self class unicodeDenormalizationMap. |
22406 | 5162 |
mapChar := [:char | |
20014 | 5163 |
|mappedChars| |
5164 |
||
5165 |
(mappedChars := map at:char ifAbsent:nil) notNil ifTrue:[ |
|
5166 |
mappedChars do:mapChar. |
|
5167 |
] ifFalse:[ |
|
5168 |
outStream nextPut:char. |
|
5169 |
]. |
|
5170 |
]. |
|
20013 | 5171 |
|
20014 | 5172 |
outStream := WriteStream on:(Unicode16String new:self size). |
5173 |
self do:mapChar. |
|
20013 | 5174 |
^ outStream contents asSingleByteStringIfPossible. |
5175 |
||
5176 |
" |
|
5177 |
'Ö' asDenormalizedUnicodeString |
|
5178 |
'aÖÄx' asDenormalizedUnicodeString |
|
5179 |
'abc' asDenormalizedUnicodeString |
|
22406 | 5180 |
'ṩ' asDenormalizedUnicodeString |
5181 |
" |
|
5182 |
||
5183 |
"Modified (format): / 02-01-2018 / 18:52:33 / stefan" |
|
20013 | 5184 |
! |
5185 |
||
20011 | 5186 |
asNormalizedUnicodeString |
20010 | 5187 |
"return a new string containing the same characters, as a normalized Unicode string. |
5188 |
This replaces combination characters by corresponding single characters. |
|
20863 | 5189 |
(i.e. diaresis and other combining diacriticals in the 0x03xx range). |
5190 |
Caveat: |
|
5191 |
possibly incomplete: only COMBINING_DIACRITICAL_MARKS are cared for. |
|
5192 |
Does not care for COMBINING_DIACRITICAL_MARKS_EXTENDED |
|
20864 | 5193 |
and COMBINING_DIACRITICAL_MARKS_SUPPLEMENT. |
5194 |
However; those are used for German dialectology, ancient Greek and other similar |
|
5195 |
exotic uses. Probably noone will ever even notice that they are missing..." |
|
20010 | 5196 |
|
5197 |
|outStream prevChar map mapEntries mappedChar| |
|
5198 |
||
5199 |
map := self class unicodeNormalizationMap. |
|
20017 | 5200 |
|
20010 | 5201 |
self bitsPerCharacter <= 8 ifTrue:[^ self]. "/ I cannot contain any overtypes |
20017 | 5202 |
(self contains:[:ch | ch unicodeBlock == #'COMBINING_DIACRITICAL_MARKS']) ifFalse:[^ self]. "/ I do not contain any overtypes |
20010 | 5203 |
|
5204 |
outStream := WriteStream on:(self species new:self size). |
|
5205 |
self do:[:char | |
|
20017 | 5206 |
((char unicodeBlock == #'COMBINING_DIACRITICAL_MARKS') |
20010 | 5207 |
and:[ (mapEntries := map at:char ifAbsent:nil) notNil |
5208 |
and:[ (mappedChar := mapEntries at:prevChar ifAbsent:nil) notNil ]]) ifTrue:[ |
|
20014 | 5209 |
prevChar := mappedChar. |
20010 | 5210 |
] ifFalse:[ |
5211 |
prevChar notNil ifTrue:[ outStream nextPut:prevChar]. |
|
5212 |
prevChar := char |
|
5213 |
]. |
|
5214 |
]. |
|
20011 | 5215 |
prevChar notNil ifTrue:[ outStream nextPut:prevChar]. |
20010 | 5216 |
^ outStream contents asSingleByteStringIfPossible. |
5217 |
||
5218 |
" |
|
5219 |
self unicodeNormalizationMap |
|
20012 | 5220 |
('O' , (Character value:16r0308)) asNormalizedUnicodeString -> 'Ö' |
20010 | 5221 |
('O' , (Character value:16r0308) , |
20012 | 5222 |
'A' , (Character value:16r0308)) asNormalizedUnicodeString -> 'ÖÄ' |
20014 | 5223 |
('s' , (Character value:16r0323) , (Character value:16r0307)) asNormalizedUnicodeString -> 'ṩ' |
5224 |
('s' , (Character value:16r0307) , (Character value:16r0323)) asNormalizedUnicodeString -> 'ṩ' |
|
20010 | 5225 |
" |
5226 |
! |
|
5227 |
||
12767
17d2c7268065
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12679
diff
changeset
|
5228 |
decodeAsLiteralArray |
17d2c7268065
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12679
diff
changeset
|
5229 |
"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
|
5230 |
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
|
5231 |
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
|
5232 |
|
17d2c7268065
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12679
diff
changeset
|
5233 |
^ self |
17d2c7268065
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12679
diff
changeset
|
5234 |
! |
17d2c7268065
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12679
diff
changeset
|
5235 |
|
991
f0b45a4b82d2
first attempt in encoding/decoding for different character representations (mac/msdos etc.)
Claus Gittinger <cg@exept.de>
parents:
986
diff
changeset
|
5236 |
decodeFrom:encodingSymbol |
8874 | 5237 |
"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
|
5238 |
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
|
5239 |
|
ca774158b317
code cleanup (removed old obsolete encoding stuff)
Claus Gittinger <cg@exept.de>
parents:
8102
diff
changeset
|
5240 |
|myEncoding encoder| |
7950 | 5241 |
|
5242 |
encodingSymbol isNil ifTrue:[^ self]. |
|
5243 |
myEncoding := self encoding. |
|
5244 |
encodingSymbol == myEncoding ifTrue:[^ self]. |
|
5245 |
||
22451 | 5246 |
encoder := CharacterEncoder encoderToEncodeFrom:myEncoding into:encodingSymbol. |
8124
ca774158b317
code cleanup (removed old obsolete encoding stuff)
Claus Gittinger <cg@exept.de>
parents:
8102
diff
changeset
|
5247 |
encoder isNil ifTrue:[^ self]. |
ca774158b317
code cleanup (removed old obsolete encoding stuff)
Claus Gittinger <cg@exept.de>
parents:
8102
diff
changeset
|
5248 |
^ encoder decodeString:self. |
22451 | 5249 |
|
5250 |
"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
|
5251 |
! |
f0b45a4b82d2
first attempt in encoding/decoding for different character representations (mac/msdos etc.)
Claus Gittinger <cg@exept.de>
parents:
986
diff
changeset
|
5252 |
|
9192 | 5253 |
encodeFrom:oldEncoding into:newEncoding |
5254 |
^ CharacterEncoder encodeString:self from:oldEncoding into:newEncoding |
|
14405
1d0d99a95bba
comment/format in:7 methods
Claus Gittinger <cg@exept.de>
parents:
14168
diff
changeset
|
5255 |
|
1d0d99a95bba
comment/format in:7 methods
Claus Gittinger <cg@exept.de>
parents:
14168
diff
changeset
|
5256 |
" |
17600 | 5257 |
'äüö' encodeFrom:#iso8859 into:#utf8 |
5258 |
('äüö' encodeFrom:#iso8859 into:#utf8) encodeFrom:#utf8 into:#unicode |
|
14405
1d0d99a95bba
comment/format in:7 methods
Claus Gittinger <cg@exept.de>
parents:
14168
diff
changeset
|
5259 |
" |
9192 | 5260 |
! |
5261 |
||
22451 | 5262 |
encodeInto:newEncoding |
5263 |
^ CharacterEncoder encodeString:self from:self encoding into:newEncoding |
|
5264 |
||
5265 |
" |
|
5266 |
'äüö' encodeInto:#utf8 |
|
5267 |
('äüö' encodeInto:#utf8) decodeFrom:#utf8 |
|
5268 |
" |
|
5269 |
||
5270 |
"Created: / 17-01-2018 / 17:43:20 / stefan" |
|
5271 |
! |
|
5272 |
||
9192 | 5273 |
rot13 |
8874 | 5274 |
"Usenet: from `rotate alphabet 13 places'] |
6835
bdb2cb03200c
added utf8Decode with 8-bit chars only
Claus Gittinger <cg@exept.de>
parents:
6834
diff
changeset
|
5275 |
The simple Caesar-cypher encryption that replaces each English |
8874 | 5276 |
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
|
5277 |
so that 'The butler did it!!' becomes 'Gur ohgyre qvq vg!!' |
8874 | 5278 |
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
|
5279 |
It is used to enclose the text in a sealed wrapper that the reader must choose |
8874 | 5280 |
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
|
5281 |
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
|
5282 |
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
|
5283 |
|
11865 | 5284 |
^ self rot:13 |
8874 | 5285 |
|
5286 |
" |
|
5287 |
'hello world' rot13 |
|
5288 |
'hello world' rot13 rot13 |
|
6835
bdb2cb03200c
added utf8Decode with 8-bit chars only
Claus Gittinger <cg@exept.de>
parents:
6834
diff
changeset
|
5289 |
" |
bdb2cb03200c
added utf8Decode with 8-bit chars only
Claus Gittinger <cg@exept.de>
parents:
6834
diff
changeset
|
5290 |
! |
bdb2cb03200c
added utf8Decode with 8-bit chars only
Claus Gittinger <cg@exept.de>
parents:
6834
diff
changeset
|
5291 |
|
11865 | 5292 |
rot:n |
5293 |
"Usenet: from `rotate alphabet N places'] |
|
5294 |
The simple Caesar-cypher encryption that replaces each English |
|
5295 |
letter with the one N places forward or back along the alphabet, |
|
5296 |
so that 'The butler did it!!' becomes 'Gur ohgyre qvq vg!!' by rot 13 |
|
5297 |
Most Usenet news reading and posting programs include a rot13 feature. |
|
5298 |
It is used to enclose the text in a sealed wrapper that the reader must choose |
|
5299 |
to open -- e.g., for posting things that might offend some readers, or spoilers. |
|
5300 |
A major advantage of rot13 over rot(N) for other N is that it |
|
5301 |
is self-inverse, so the same code can be used for encoding and decoding." |
|
5302 |
||
5303 |
^ self species |
|
20874 | 5304 |
streamContents:[:aStream | |
5305 |
self do:[:char | |
|
5306 |
aStream nextPut:(char rot:n) ]] |
|
11865 | 5307 |
|
5308 |
" |
|
5309 |
'hello world' rot:13 |
|
5310 |
('hello world' rot:13) rot:13 |
|
5311 |
" |
|
5312 |
! |
|
5313 |
||
15656 | 5314 |
utf16Encoded |
5315 |
"Return my UTF-16 representation as a new TwoByteString" |
|
5316 |
||
5317 |
|s| |
|
5318 |
||
5319 |
s := WriteStream on:(TwoByteString uninitializedNew:self size). |
|
5320 |
s nextPutAllUtf16:self. |
|
5321 |
^ s contents |
|
5322 |
||
5323 |
" |
|
5324 |
'abcde1234' utf16Encoded |
|
17600 | 5325 |
'abcdeäöüß' utf16Encoded |
15656 | 5326 |
" |
5327 |
||
5328 |
"Modified: / 11-05-2010 / 19:12:37 / cg" |
|
5329 |
! |
|
5330 |
||
9192 | 5331 |
utf8Encoded |
19837 | 5332 |
"return the UTF-8 representation of a Unicode string. |
5333 |
The resulting string is only useful to be stored on some external file, |
|
21318 | 5334 |
or sent to a communaction channel. |
5335 |
Not for being used inside ST/X." |
|
19837 | 5336 |
|
5337 |
|string stream| |
|
17625 | 5338 |
|
5339 |
string := self string. |
|
19837 | 5340 |
|
5341 |
"/ avoid creation of new strings if possible |
|
5342 |
string containsNon7BitAscii ifFalse:[ |
|
5343 |
^ string asSingleByteString |
|
5344 |
]. |
|
5345 |
||
5346 |
"make it size 2 for 1-byte strings" |
|
5347 |
stream := WriteStream on:(String uninitializedNew:(1 + (string size * 3 // 2))). |
|
5348 |
string utf8EncodedOn:stream. |
|
5349 |
^ stream contents. |
|
5350 |
||
5351 |
" |
|
5352 |
'hello' utf8Encoded asByteArray #[104 101 108 108 111] |
|
5353 |
(Character value:16r40) asString utf8Encoded asByteArray #[64] |
|
5354 |
(Character value:16r7F) asString utf8Encoded asByteArray #[127] |
|
5355 |
(Character value:16r80) asString utf8Encoded asByteArray #[194 128] |
|
5356 |
(Character value:16rFF) asString utf8Encoded asByteArray #[195 191] |
|
5357 |
(Character value:16r100) asString utf8Encoded asByteArray #[196 128] |
|
5358 |
(Character value:16r200) asString utf8Encoded asByteArray #[200 128] |
|
5359 |
(Character value:16r400) asString utf8Encoded asByteArray #[208 128] |
|
5360 |
(Character value:16r800) asString utf8Encoded asByteArray #[224 160 128] |
|
5361 |
(Character value:16r1000) asString utf8Encoded asByteArray #[225 128 128] |
|
5362 |
(Character value:16r2000) asString utf8Encoded asByteArray #[226 128 128] |
|
5363 |
(Character value:16r4000) asString utf8Encoded asByteArray #[228 128 128] |
|
5364 |
(Character value:16r8000) asString utf8Encoded asByteArray #[232 128 128] |
|
5365 |
(Character value:16rFFFF) asString utf8Encoded asByteArray #[239 191 191] |
|
21318 | 5366 |
(Character value:16r1FFFF) asString utf8Encoded asByteArray #[240 159 191 191] |
5367 |
(Character value:16r3FFFF) asString utf8Encoded asByteArray #[240 191 191 191] |
|
5368 |
(Character value:16rFFFFF) asString utf8Encoded asByteArray #[243 191 191 191] |
|
5369 |
(Character value:16r3FFFFF) asString utf8Encoded asByteArray #[248 143 191 191 191] |
|
19837 | 5370 |
|
18597 | 5371 |
'abcde1234' asUnicode32String utf8Encoded |
5372 |
'abcdeäöüß' asUnicode32String utf8Encoded |
|
12925
5c0a9a633ca1
comment/format in: #utf8Encoded
Claus Gittinger <cg@exept.de>
parents:
12912
diff
changeset
|
5373 |
" |
17625 | 5374 |
! |
5375 |
||
5376 |
utf8EncodedOn:aStream |
|
21468 | 5377 |
"write the UTF-8 representation of myself to aStream." |
5378 |
||
5379 |
aStream nextPutAllUtf8:self. |
|
5380 |
||
5381 |
"Modified: / 16-02-2017 / 16:51:32 / stefan" |
|
9192 | 5382 |
! ! |
5383 |
||
22566 | 5384 |
!CharacterArray methodsFor:'filling and replacing'! |
5385 |
||
5386 |
clearContents |
|
5387 |
"to be used with cryptographic keys, to wipe their contents after use" |
|
5388 |
||
5389 |
self atAllPut:(Character value:0) |
|
5390 |
! ! |
|
22451 | 5391 |
|
5392 |
||
24250 | 5393 |
|
5394 |
||
12111 | 5395 |
!CharacterArray methodsFor:'matching - glob expressions'! |
5396 |
||
5397 |
compoundMatch:aString |
|
5398 |
"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
|
5399 |
consisting of multiple simple GLOB patterns, separated by semicolons. |
12111 | 5400 |
This is usable with fileName pattern fields. |
23791 | 5401 |
This is a case sensitive match: lower/uppercase are considered different. |
12111 | 5402 |
|
5403 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20874 | 5404 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5405 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 5406 |
NOTICE: the receiver is the match pattern" |
5407 |
||
15890 | 5408 |
^ self compoundMatch:aString caseSensitive:true withoutSeparators:false |
12111 | 5409 |
|
5410 |
" |
|
5411 |
'f*' match:'foo' |
|
5412 |
'b*' match:'foo' |
|
5413 |
'f*;b*' match:'foo' |
|
5414 |
'f*;b*' match:'bar' |
|
5415 |
'f*;b*' compoundMatch:'foo' |
|
5416 |
'f*;b*' compoundMatch:'bar' |
|
23791 | 5417 |
'*.png;*.gif' compoundMatch:'bar.jpg' |
5418 |
'*.png;*.gif' compoundMatch:'bar.gif' |
|
5419 |
" |
|
5420 |
||
5421 |
"Modified: / 30-01-1998 / 11:40:18 / stefan" |
|
5422 |
"Modified: / 16-12-1999 / 01:22:08 / cg" |
|
5423 |
"Modified (comment): / 02-03-2019 / 12:21:33 / Claus Gittinger" |
|
12111 | 5424 |
! |
5425 |
||
15734 | 5426 |
compoundMatch:aString caseSensitive:caseSensitive |
12111 | 5427 |
"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
|
5428 |
consisting of multiple simple GLOB patterns, separated by semicolons. |
12111 | 5429 |
This is usable with fileName pattern fields. |
5430 |
||
5431 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20874 | 5432 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5433 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 5434 |
NOTICE: the receiver is the match pattern" |
5435 |
||
15890 | 5436 |
^self compoundMatch:aString caseSensitive:caseSensitive withoutSeparators:false |
5437 |
||
5438 |
" |
|
5439 |
'f*' match:'foo' |
|
5440 |
'b*' match:'foo' |
|
5441 |
'f*;b*' match:'foo' |
|
5442 |
'f*;b*' match:'bar' |
|
5443 |
'f*;b*' compoundMatch:'foo' |
|
5444 |
'f*;b*' compoundMatch:'bar' |
|
5445 |
'f*;b*' compoundMatch:'Foo' caseSensitive:false |
|
5446 |
'f*;b*' compoundMatch:'Bar' caseSensitive:false |
|
5447 |
'f*;b*' compoundMatch:'ccc' caseSensitive:false |
|
23791 | 5448 |
|
5449 |
'*.png;*.gif' compoundMatch:'bar.GIF' |
|
5450 |
'*.png;*.gif' compoundMatch:'bar.GIF' caseSensitive:false |
|
5451 |
" |
|
5452 |
||
5453 |
"Modified: / 15-04-1997 / 15:50:33 / cg" |
|
5454 |
"Modified: / 30-01-1998 / 11:40:18 / stefan" |
|
5455 |
"Created: / 16-12-1999 / 01:21:35 / cg" |
|
5456 |
"Modified (comment): / 02-03-2019 / 12:22:14 / Claus Gittinger" |
|
15890 | 5457 |
! |
5458 |
||
5459 |
compoundMatch:aString caseSensitive:caseSensitive withoutSeparators:withoutSeparators |
|
5460 |
"like match, but the receiver may be a compound match pattern, |
|
5461 |
consisting of multiple simple GLOB patterns, separated by semicolons. |
|
5462 |
If withoutSeparators is true, spaces around individual patterns are stripped off. |
|
5463 |
This is usable with fileName pattern fields. |
|
5464 |
||
5465 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20874 | 5466 |
NOT the ST-80 meaning. |
15890 | 5467 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
5468 |
NOTICE: the receiver is the match pattern" |
|
5469 |
||
12111 | 5470 |
|matchers| |
5471 |
||
5472 |
matchers := self asCollectionOfSubstringsSeparatedBy:$;. |
|
15890 | 5473 |
withoutSeparators ifTrue:[ matchers := matchers collect:[:each | each withoutSeparators] ]. |
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
5474 |
^ matchers |
20874 | 5475 |
contains:[:aPattern | |
5476 |
aPattern match:aString caseSensitive:caseSensitive escapeCharacter:nil |
|
5477 |
]. |
|
15734 | 5478 |
|
5479 |
" |
|
5480 |
'f*' match:'foo' |
|
5481 |
'b*' match:'foo' |
|
5482 |
'f*;b*' match:'foo' |
|
5483 |
'f*;b*' match:'bar' |
|
5484 |
'f*;b*' compoundMatch:'foo' |
|
5485 |
'f*;b*' compoundMatch:'bar' |
|
5486 |
'f*;b*' compoundMatch:'Foo' caseSensitive:false |
|
5487 |
'f*;b*' compoundMatch:'Bar' caseSensitive:false |
|
5488 |
'f*;b*' compoundMatch:'ccc' caseSensitive:false |
|
15890 | 5489 |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
5490 |
'f* ; b*' compoundMatch:'foo' |
15890 | 5491 |
'f* ; b*' compoundMatch:'foo' caseSensitive:true withoutSeparators:true |
15734 | 5492 |
" |
5493 |
||
5494 |
"Modified: / 15.4.1997 / 15:50:33 / cg" |
|
5495 |
"Modified: / 30.1.1998 / 11:40:18 / stefan" |
|
5496 |
"Created: / 16.12.1999 / 01:21:35 / cg" |
|
5497 |
! |
|
5498 |
||
5499 |
compoundMatch:aString ignoreCase:ignoreCase |
|
5500 |
<resource: #obsolete> |
|
5501 |
||
5502 |
"like match, but the receiver may be a compound match pattern, |
|
5503 |
consisting of multiple simple GLOB patterns, separated by semicolons. |
|
5504 |
This is usable with fileName pattern fields. |
|
5505 |
||
5506 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20874 | 5507 |
NOT the ST-80 meaning. |
15734 | 5508 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
5509 |
NOTICE: the receiver is the match pattern" |
|
5510 |
||
5511 |
^ self compoundMatch:aString caseSensitive:ignoreCase not |
|
12111 | 5512 |
|
5513 |
" |
|
5514 |
'f*' match:'foo' |
|
5515 |
'b*' match:'foo' |
|
5516 |
'f*;b*' match:'foo' |
|
5517 |
'f*;b*' match:'bar' |
|
5518 |
'f*;b*' compoundMatch:'foo' |
|
5519 |
'f*;b*' compoundMatch:'bar' |
|
5520 |
'f*;b*' compoundMatch:'Foo' ignoreCase:true |
|
5521 |
'f*;b*' compoundMatch:'Bar' ignoreCase:true |
|
5522 |
'f*;b*' compoundMatch:'ccc' ignoreCase:true |
|
5523 |
" |
|
5524 |
||
5525 |
"Modified: / 15.4.1997 / 15:50:33 / cg" |
|
5526 |
"Modified: / 30.1.1998 / 11:40:18 / stefan" |
|
5527 |
"Created: / 16.12.1999 / 01:21:35 / cg" |
|
5528 |
! |
|
5529 |
||
5530 |
findMatchString:matchString |
|
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5531 |
"like findString/indexOfSubCollection, but allowing GLOB match patterns. |
23791 | 5532 |
find matchstring; if found, return the index, if not, return 0. |
5533 |
This is a case sensitive match: lower/uppercase are considered different. |
|
12111 | 5534 |
|
5535 |
NOTICE: match-meta character interpretation is like in unix-matching, |
|
20874 | 5536 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5537 |
NOTICE: this GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 5538 |
NOTICE: the argument is the match pattern" |
5539 |
||
19033 | 5540 |
^ self findMatchString:matchString startingAt:1 caseSensitive:true ifAbsent:0 |
5541 |
||
5542 |
" |
|
5543 |
1234567890123 |
|
5544 |
'hello world bla foo baz' findMatchString:'b* ' |
|
5545 |
" |
|
23791 | 5546 |
|
5547 |
"Modified (comment): / 02-03-2019 / 12:21:07 / Claus Gittinger" |
|
12111 | 5548 |
! |
5549 |
||
5550 |
findMatchString:matchString startingAt:index |
|
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5551 |
"like findString, but allowing GLOB match patterns. |
23791 | 5552 |
find matchstring, starting at index; if found, return the index, if not, return 0. |
5553 |
This is a case sensitive match: lower/uppercase are considered different. |
|
12111 | 5554 |
|
5555 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20874 | 5556 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5557 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 5558 |
NOTICE: the argument is the match pattern" |
5559 |
||
19033 | 5560 |
^ self findMatchString:matchString startingAt:index caseSensitive:true ifAbsent:0 |
23791 | 5561 |
|
5562 |
"Modified (comment): / 02-03-2019 / 12:21:05 / Claus Gittinger" |
|
12111 | 5563 |
! |
5564 |
||
15734 | 5565 |
findMatchString:matchString startingAt:index caseSensitive:caseSensitive ifAbsent:exceptionBlock |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5566 |
"like findString, but allowing GLOB match patterns. |
12111 | 5567 |
find matchstring, starting at index. if found, return the index; |
5568 |
if not found, return the result of evaluating exceptionBlock. |
|
5569 |
This is a q&d hack - not very efficient. |
|
5570 |
||
5571 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20874 | 5572 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5573 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 5574 |
NOTICE: the argument is the match pattern" |
5575 |
||
5576 |
|firstChar firstSet |
|
5577 |
startIndex "{ Class: SmallInteger }" |
|
5578 |
matchSize "{ Class: SmallInteger }" |
|
5579 |
mySize "{ Class: SmallInteger }" |
|
5580 |
realMatchString lcChar ucChar| |
|
5581 |
||
5582 |
matchSize := matchString size. |
|
5583 |
matchSize == 0 ifTrue:[^ index]. "empty string matches" |
|
5584 |
||
5585 |
realMatchString := matchString. |
|
5586 |
(realMatchString endsWith:$*) ifFalse:[ |
|
20874 | 5587 |
realMatchString := realMatchString , '*'. |
5588 |
matchSize := matchSize + 1 |
|
12111 | 5589 |
]. |
5590 |
||
5591 |
mySize := self size. |
|
5592 |
firstChar := realMatchString at:1. |
|
5593 |
firstChar == self class matchEscapeCharacter ifTrue:[ |
|
20874 | 5594 |
firstChar := realMatchString at:2. |
12111 | 5595 |
]. |
5596 |
||
5597 |
firstChar asString includesMatchCharacters ifTrue:[ |
|
20874 | 5598 |
index to:mySize do:[:col | |
5599 |
(realMatchString match:self from:col to:mySize caseSensitive:caseSensitive) |
|
5600 |
ifTrue:[^ col] |
|
5601 |
]. |
|
5602 |
^ exceptionBlock value. |
|
12111 | 5603 |
]. |
5604 |
||
5605 |
lcChar := firstChar asLowercase. |
|
5606 |
ucChar := firstChar asUppercase. |
|
15734 | 5607 |
(caseSensitive not and:[ lcChar ~= ucChar ]) ifTrue:[ |
20874 | 5608 |
firstSet := Array with:ucChar with:lcChar. |
5609 |
startIndex := self indexOfAny:firstSet startingAt:index. |
|
12111 | 5610 |
] ifFalse:[ |
20874 | 5611 |
startIndex := self indexOf:firstChar startingAt:index. |
12111 | 5612 |
]. |
5613 |
[startIndex == 0] whileFalse:[ |
|
20874 | 5614 |
(realMatchString match:self from:startIndex to:mySize caseSensitive:caseSensitive) |
5615 |
ifTrue:[^ startIndex]. |
|
5616 |
||
5617 |
firstSet notNil ifTrue:[ |
|
5618 |
startIndex := self indexOfAny:firstSet startingAt:(startIndex + 1). |
|
5619 |
] ifFalse:[ |
|
5620 |
startIndex := self indexOf:firstChar startingAt:(startIndex + 1). |
|
5621 |
]. |
|
12111 | 5622 |
]. |
5623 |
^ exceptionBlock value |
|
5624 |
||
5625 |
" |
|
5626 |
'one two three four' findMatchString:'o[nu]' |
|
5627 |
'one two three four' findMatchString:'o[nu]' startingAt:3 |
|
15734 | 5628 |
'one two three four one' findMatchString:'ONE' startingAt:3 caseSensitive:false ifAbsent:0 |
5629 |
'one two three four one' findMatchString:'ONE' startingAt:3 caseSensitive:true ifAbsent:0 |
|
5630 |
'one two three four ONE' findMatchString:'O#E' startingAt:1 caseSensitive:false ifAbsent:0 |
|
5631 |
'one two three four ONE' findMatchString:'O#E' startingAt:1 caseSensitive:true ifAbsent:0 |
|
5632 |
12345678901234567890 |
|
5633 |
" |
|
5634 |
||
5635 |
"Modified: 13.9.1997 / 06:31:22 / cg" |
|
5636 |
! |
|
5637 |
||
5638 |
findMatchString:matchString startingAt:index ignoreCase:ignoreCase ifAbsent:exceptionBlock |
|
5639 |
<resource: #obsolete> |
|
5640 |
"like findString, but allowing GLOB match patterns. |
|
5641 |
find matchstring, starting at index. if found, return the index; |
|
5642 |
if not found, return the result of evaluating exceptionBlock. |
|
5643 |
This is a q&d hack - not very efficient. |
|
5644 |
||
5645 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20874 | 5646 |
NOT the ST-80 meaning. |
15734 | 5647 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
5648 |
NOTICE: the argument is the match pattern" |
|
5649 |
||
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
5650 |
^ self |
20874 | 5651 |
findMatchString:matchString startingAt:index caseSensitive:ignoreCase not ifAbsent:exceptionBlock |
15734 | 5652 |
|
5653 |
" |
|
5654 |
'one two three four' findMatchString:'o[nu]' |
|
5655 |
'one two three four' findMatchString:'o[nu]' startingAt:3 |
|
12111 | 5656 |
'one two three four one' findMatchString:'ONE' startingAt:3 ignoreCase:true ifAbsent:0 |
5657 |
" |
|
5658 |
||
5659 |
"Modified: 13.9.1997 / 06:31:22 / cg" |
|
5660 |
! |
|
5661 |
||
19746 | 5662 |
globPatternAsRegexPattern |
5663 |
"taking the receiver as a GLOB pattern, |
|
5664 |
return a corresponding regex pattern. |
|
5665 |
As regex does provide information about the matching substring, |
|
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
5666 |
it may be useful to apply a regex after a GLOB match, |
19746 | 5667 |
in order to highlight matching substrings (eg. in a CodeView after a search). |
5668 |
If it turns out to be better, we may convert all GLOB searches to regex and use it right away. |
|
5669 |
(currently, it is not sure, if GLOB is not better for most simple searches, as they are encountered |
|
5670 |
in typical real life)" |
|
5671 |
||
5672 |
^ self species streamContents:[:s | |
|
20874 | 5673 |
|addCharacter| |
5674 |
||
5675 |
addCharacter := |
|
5676 |
[:ch | |
|
5677 |
ch isLetterOrDigit ifFalse:[ |
|
5678 |
s nextPut:$\. |
|
5679 |
]. |
|
5680 |
s nextPut:ch |
|
5681 |
]. |
|
5682 |
||
23552 | 5683 |
(self class matchScanArrayFrom:self) do:[:matchEntry | |
20874 | 5684 |
matchEntry isCharacter ifTrue:[ |
5685 |
addCharacter value:matchEntry |
|
5686 |
] ifFalse:[ |
|
5687 |
matchEntry == #anyString ifTrue:[ |
|
5688 |
s nextPutAll:'.*' |
|
5689 |
] ifFalse:[ |
|
5690 |
matchEntry == #any ifTrue:[ |
|
5691 |
s nextPut:$. |
|
5692 |
] ifFalse:[ |
|
5693 |
matchEntry isString ifTrue:[ |
|
5694 |
|set min max| |
|
5695 |
||
5696 |
s nextPut:$[. |
|
23552 | 5697 |
set := matchEntry "copy sort". "/ already sorted |
20874 | 5698 |
min := set min. |
5699 |
max := set max. |
|
5700 |
set asSet = (min to:max) asSet ifTrue:[ |
|
5701 |
addCharacter value:min. |
|
5702 |
s nextPut:$-. |
|
5703 |
addCharacter value:max. |
|
5704 |
] ifFalse:[ |
|
5705 |
set do:addCharacter. |
|
5706 |
]. |
|
5707 |
s nextPut:$]. |
|
5708 |
] ifFalse:[ |
|
22700 | 5709 |
self halt:'funny match entry'. |
20874 | 5710 |
]. |
5711 |
]. |
|
5712 |
]. |
|
5713 |
] |
|
5714 |
]. |
|
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
5715 |
]. |
19746 | 5716 |
|
5717 |
" |
|
5718 |
'hello' globPatternAsRegexPattern |
|
5719 |
'hello*' globPatternAsRegexPattern |
|
5720 |
'*hello*' globPatternAsRegexPattern |
|
5721 |
'h###' globPatternAsRegexPattern |
|
5722 |
'h[0-9]' globPatternAsRegexPattern |
|
5723 |
'h[0-9][0-9][0-9]' globPatternAsRegexPattern |
|
5724 |
'h[0-9]*' globPatternAsRegexPattern |
|
19747 | 5725 |
'h[-+]*' globPatternAsRegexPattern |
23552 | 5726 |
'h[abc]*' globPatternAsRegexPattern |
5727 |
'h[0-9abc]*' globPatternAsRegexPattern |
|
19747 | 5728 |
'hello world' matches:'h*w' |
5729 |
'hello world' matchesRegex:('h*w' globPatternAsRegexPattern) |
|
5730 |
'hello world' matches:'h*d' |
|
5731 |
'hello world' matchesRegex:('h*d' globPatternAsRegexPattern) |
|
19746 | 5732 |
" |
23552 | 5733 |
|
5734 |
"Modified: / 03-12-2018 / 15:47:15 / Stefan Vogel" |
|
19746 | 5735 |
! |
5736 |
||
12111 | 5737 |
includesMatchString:matchString |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5738 |
"like includesString, but allowing GLOB match patterns. |
23791 | 5739 |
find matchstring; if found, return true, false otherwise. |
5740 |
This is a case sensitive match: lower/uppercase are considered different. |
|
12111 | 5741 |
|
5742 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20874 | 5743 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5744 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 5745 |
NOTICE: the argument is the match pattern" |
5746 |
||
5747 |
^ (self findMatchString:matchString) ~~ 0 |
|
5748 |
||
5749 |
" |
|
5750 |
'hello world' includesMatchString:'h*' |
|
5751 |
'hello world' includesMatchString:'h[aeiou]llo' |
|
5752 |
'hello world' includesMatchString:'wor*' |
|
5753 |
'hello world' includesMatchString:'woR*' |
|
23791 | 5754 |
'menue' includesMatchString:'[mM]enu[Ee]' |
5755 |
'blamenuebla' includesMatchString:'[mM]enu[Ee]' |
|
5756 |
'blaMenuebla' includesMatchString:'[mM]enu[Ee]' |
|
5757 |
" |
|
5758 |
||
5759 |
"Modified (comment): / 02-03-2019 / 12:20:58 / Claus Gittinger" |
|
12111 | 5760 |
! |
5761 |
||
5762 |
includesMatchString:matchString caseSensitive:caseSensitive |
|
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5763 |
"like includesString, but allowing GLOB match patterns. |
12111 | 5764 |
find matchstring; if found, return true, otherwise return false. |
5765 |
||
5766 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20874 | 5767 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5768 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 5769 |
NOTICE: the argument is the match pattern" |
5770 |
||
19033 | 5771 |
^ (self findMatchString:matchString startingAt:1 caseSensitive:caseSensitive ifAbsent:0) ~~ 0 |
12111 | 5772 |
|
5773 |
" |
|
5774 |
'hello world' includesMatchString:'h*' caseSensitive:true |
|
5775 |
'hello world' includesMatchString:'h*' caseSensitive:false |
|
5776 |
'Hello world' includesMatchString:'h*' caseSensitive:true |
|
5777 |
'Hello world' includesMatchString:'h*' caseSensitive:false |
|
5778 |
||
5779 |
'hello world' includesMatchString:'h[aeiou]llo' caseSensitive:true |
|
5780 |
'hello world' includesMatchString:'h[aeiou]llo' caseSensitive:false |
|
5781 |
||
5782 |
'hello world' includesMatchString:'wor*' caseSensitive:true |
|
5783 |
'hello world' includesMatchString:'wor*' caseSensitive:false |
|
5784 |
||
5785 |
'hello world' includesMatchString:'woR*' caseSensitive:true |
|
5786 |
'hello world' includesMatchString:'woR*' caseSensitive:false |
|
23791 | 5787 |
|
5788 |
'menue' includesMatchString:'[mM]enu[Ee]' |
|
5789 |
'menue' includesMatchString:'[mM]enu[Ee]' caseSensitive:true |
|
5790 |
'blamenuebla' includesMatchString:'[mM]enu[Ee]' |
|
5791 |
'blamenuebla' includesMatchString:'[mM]enu[Ee]' caseSensitive:true |
|
5792 |
||
5793 |
'blaMenuebla' includesMatchString:'[MS]enu[EF]' |
|
5794 |
'blaMenuebla' includesMatchString:'[MS]enu[EF]' caseSensitive:true |
|
5795 |
'blaMenuebla' includesMatchString:'[MS]enu[EF]' caseSensitive:false |
|
5796 |
" |
|
5797 |
||
5798 |
"Modified (comment): / 02-03-2019 / 12:18:57 / Claus Gittinger" |
|
12111 | 5799 |
! |
5800 |
||
5801 |
match:aString |
|
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5802 |
"return true if aString matches self, where self may contain GLOB meta-match |
12111 | 5803 |
characters $* (to match any string) or $# (to match any character). |
5804 |
or [...] to match a set of characters. |
|
23791 | 5805 |
This is a case sensitive match: lower/uppercase are considered different. |
12111 | 5806 |
The escape character is the backQuote. |
5807 |
||
5808 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20874 | 5809 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5810 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 5811 |
NOTICE: the receiver is the match pattern" |
5812 |
||
15734 | 5813 |
^ self match:aString from:1 to:aString size caseSensitive:true |
12111 | 5814 |
|
5815 |
" |
|
22137 | 5816 |
'#f#' match:'afc' |
5817 |
'#f#' match:'aec' |
|
5818 |
'#f#' match:'ae' |
|
5819 |
'#f#' match:'a' |
|
5820 |
'#f#' match:'f' |
|
5821 |
'#f#' match:'ff' |
|
5822 |
||
12111 | 5823 |
'\*f*' match:'f' |
5824 |
'\*f*' match:'*f' |
|
5825 |
'*\*f*' match:'*f' |
|
5826 |
'*f*' match:'*f' |
|
5827 |
'*ute*' match:'computer' |
|
5828 |
'*uter' match:'computer' |
|
5829 |
'uter*' match:'computer' |
|
5830 |
'*ute*' match:'' |
|
5831 |
'[abcd]*' match:'computer' |
|
5832 |
'[abcd]*' match:'komputer' |
|
5833 |
'*some*compl*ern*' match:'this is some more complicated pattern match' |
|
5834 |
'*some*compl*ern*' match:'this is another complicated pattern match' |
|
5835 |
'*-hh' match:'anton-h' |
|
5836 |
" |
|
5837 |
||
22137 | 5838 |
"Modified: / 09-06-1998 / 18:50:00 / cg" |
5839 |
"Modified (comment): / 29-07-2017 / 14:00:23 / cg" |
|
23791 | 5840 |
"Modified (comment): / 02-03-2019 / 12:20:48 / Claus Gittinger" |
12111 | 5841 |
! |
5842 |
||
15734 | 5843 |
match:aString caseSensitive:caseSensitive |
5844 |
"return true if aString matches self, where self may contain GLOB meta-match |
|
5845 |
characters $* (to match any string) or $# (to match any character) |
|
5846 |
or [...] to match a set of characters. |
|
5847 |
If caseSensitive is false, lower/uppercase are considered the same. |
|
5848 |
The escape character is the backQuote. |
|
5849 |
||
5850 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20874 | 5851 |
NOT the ST-80 meaning. |
15734 | 5852 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
5853 |
NOTICE: the receiver is the match pattern" |
|
5854 |
||
5855 |
^ self match:aString from:1 to:aString size caseSensitive:caseSensitive |
|
5856 |
||
5857 |
" |
|
5858 |
'*ute*' match:'COMPUTER' caseSensitive:false |
|
5859 |
'*uter' match:'COMPUTER' caseSensitive:true |
|
5860 |
'[abcd]*' match:'computer' caseSensitive:true |
|
5861 |
'[abcd]*' match:'Computer' caseSensitive:true |
|
5862 |
'[a-k]*' match:'komputer' caseSensitive:true |
|
5863 |
'[a-k]*' match:'zomputer' caseSensitive:true |
|
5864 |
'[a-k]*' match:'Komputer' caseSensitive:true |
|
5865 |
'[a-k]*' match:'Komputer' caseSensitive:false |
|
5866 |
'*some*compl*ern*' match:'this is some more complicated pattern match' caseSensitive:false |
|
5867 |
'*some*compl*ern*' match:'this is another complicated pattern match' caseSensitive:false |
|
5868 |
||
5869 |
Time millisecondsToRun:[ |
|
20874 | 5870 |
Symbol allInstancesDo:[:sym | |
5871 |
'[ab]*' match:sym caseSensitive:true |
|
5872 |
] |
|
15734 | 5873 |
]. |
5874 |
Time millisecondsToRun:[ |
|
20874 | 5875 |
Symbol allInstancesDo:[:sym | |
5876 |
'*at:*' match:sym caseSensitive:true |
|
5877 |
] |
|
15734 | 5878 |
]. |
5879 |
" |
|
5880 |
||
5881 |
"Modified: 2.4.1997 / 17:28:58 / cg" |
|
5882 |
! |
|
5883 |
||
5884 |
match:aString caseSensitive:caseSensitive escapeCharacter:escape |
|
5885 |
"return true if aString matches self, where self may contain GLOB meta-match |
|
5886 |
characters $* (to match any string) or $# (to match any character) |
|
5887 |
or [...] to match a set of characters. |
|
5888 |
If caseSensitive is false, lower/uppercase are considered the same. |
|
5889 |
||
5890 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20874 | 5891 |
NOT the ST-80 meaning. |
15734 | 5892 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
5893 |
NOTICE: the receiver is the match pattern" |
|
5894 |
||
5895 |
^ self match:aString from:1 to:aString size caseSensitive:caseSensitive escapeCharacter:escape |
|
5896 |
||
5897 |
" |
|
5898 |
'*ute*' match:'COMPUTER' caseSensitive:false |
|
5899 |
'*uter' match:'COMPUTER' caseSensitive:true |
|
5900 |
'[abcd]*' match:'computer' caseSensitive:true |
|
5901 |
'[abcd]*' match:'Computer' caseSensitive:true |
|
5902 |
'[a-k]*' match:'komputer' caseSensitive:true |
|
5903 |
'[a-k]*' match:'zomputer' caseSensitive:true |
|
5904 |
'[a-k]*' match:'Komputer' caseSensitive:true |
|
5905 |
'[a-k]*' match:'Komputer' caseSensitive:false |
|
5906 |
'*some*compl*ern*' match:'this is some more complicated pattern match' caseSensitive:false |
|
5907 |
'*some*compl*ern*' match:'this is another complicated pattern match' caseSensitive:false |
|
5908 |
||
5909 |
Time millisecondsToRun:[ |
|
20874 | 5910 |
Symbol allInstancesDo:[:sym | |
5911 |
'[ab]*' match:sym caseSensitive:true |
|
5912 |
] |
|
15734 | 5913 |
]. |
5914 |
Time millisecondsToRun:[ |
|
20874 | 5915 |
Symbol allInstancesDo:[:sym | |
5916 |
'*at:*' match:sym caseSensitive:true |
|
5917 |
] |
|
15734 | 5918 |
]. |
5919 |
" |
|
5920 |
||
5921 |
"Modified: 2.4.1997 / 17:28:58 / cg" |
|
5922 |
! |
|
5923 |
||
12111 | 5924 |
match:aString escapeCharacter:escape |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5925 |
"return true if aString matches self, where self may contain GLOB meta-match |
12111 | 5926 |
characters $* (to match any string) or $# (to match any character). |
5927 |
or [...] to match a set of characters. |
|
5928 |
Lower/uppercase are considered different. |
|
5929 |
||
5930 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20874 | 5931 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5932 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 5933 |
NOTICE: the receiver is the match pattern" |
5934 |
||
19033 | 5935 |
^ self match:aString from:1 to:aString size caseSensitive:true escapeCharacter:escape |
12111 | 5936 |
|
5937 |
" |
|
5938 |
'a\b\c\*' match:'a\b\c\d' |
|
5939 |
'a\b\c\*' match:'a\b\c\d' escapeCharacter:nil |
|
5940 |
" |
|
5941 |
! |
|
5942 |
||
15734 | 5943 |
match:aString from:start to:stop caseSensitive:caseSensitive |
5944 |
"return true if part of aString matches myself, |
|
5945 |
where self may contain GLOB meta-match |
|
5946 |
characters $* (to match any string) or $# (to match any character) |
|
5947 |
or [...] to match a set of characters. |
|
5948 |
If caseSensitive is false, lower/uppercase are considered the same. |
|
5949 |
The escape character is the backQuote. |
|
5950 |
||
5951 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20874 | 5952 |
NOT the ST-80 meaning. |
15734 | 5953 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
5954 |
NOTICE: the receiver is the match pattern" |
|
5955 |
||
5956 |
^ self |
|
20874 | 5957 |
match:aString from:start to:stop caseSensitive:caseSensitive |
5958 |
escapeCharacter:(self class matchEscapeCharacter) |
|
15734 | 5959 |
|
5960 |
" |
|
5961 |
'*ute*' match:'12345COMPUTER' from:1 to:5 caseSensitive:false |
|
5962 |
'*ute*' match:'12345COMPUTER' from:6 to:13 caseSensitive:false |
|
5963 |
" |
|
5964 |
||
5965 |
"Modified: / 10.11.1998 / 21:43:46 / cg" |
|
5966 |
! |
|
5967 |
||
5968 |
match:aString from:start to:stop caseSensitive:caseSensitive escapeCharacter:escape |
|
5969 |
"return true if part of aString matches myself, |
|
5970 |
where self may contain GLOB meta-match |
|
5971 |
characters $* (to match any string) or $# (to match any character) |
|
5972 |
or [...] to match a set of characters. |
|
5973 |
If caseSensitive is false, lower/uppercase are considered the same. |
|
5974 |
||
5975 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20667 | 5976 |
NOT the ST-80 meaning. |
15734 | 5977 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
5978 |
NOTICE: the receiver is the match pattern" |
|
5979 |
||
5980 |
|matchScanArray| |
|
5981 |
||
5982 |
" |
|
22137 | 5983 |
keep the matchScanArray from the most recent matches - |
5984 |
avoids parsing the pattern over-and over again, |
|
5985 |
if multiple searches are done with the same pattern. |
|
5986 |
" |
|
20667 | 5987 |
(PreviousMatches isNil |
5988 |
or:[(matchScanArray := PreviousMatches at: self ifAbsent:[nil]) isNil]) ifTrue:[ |
|
5989 |
matchScanArray := self class matchScanArrayFrom:self escapeCharacter:escape. |
|
5990 |
matchScanArray isNil ifTrue:[ |
|
23459 | 5991 |
ParseWarning raiseRequestErrorString:('CharacterArray [info]: invalid matchpattern: ''%1'' comparing for equality' bindWith:self). |
20667 | 5992 |
^ self = aString |
23459 | 5993 |
"/ ^ false |
20667 | 5994 |
]. |
5995 |
PreviousMatches isNil ifTrue:[ |
|
22137 | 5996 |
PreviousMatches := CacheDictionary new:15 |
20667 | 5997 |
]. |
5998 |
PreviousMatches at:self put:matchScanArray. |
|
15734 | 5999 |
]. |
6000 |
||
6001 |
^ self class |
|
20667 | 6002 |
matchScan:matchScanArray |
6003 |
from:1 to:matchScanArray size |
|
6004 |
with:aString |
|
6005 |
from:start to:stop |
|
6006 |
caseSensitive:caseSensitive |
|
15734 | 6007 |
|
6008 |
" |
|
6009 |
'*ute*' match:'12345COMPUTER' from:1 to:5 caseSensitive:false |
|
6010 |
'*ute*' match:'12345COMPUTER' from:6 to:13 caseSensitive:false |
|
23459 | 6011 |
|
6012 |
'*[12' match:'12345COMPUTER' from:6 to:13 caseSensitive:false -- gives a warning |
|
15734 | 6013 |
" |
6014 |
||
22137 | 6015 |
"Modified: / 29-07-2017 / 14:01:42 / cg" |
23459 | 6016 |
"Modified (comment): / 24-10-2018 / 09:01:31 / Claus Gittinger" |
15734 | 6017 |
! |
6018 |
||
12111 | 6019 |
match:aString from:start to:stop ignoreCase:ignoreCase |
15734 | 6020 |
<resource: #obsolete> |
12111 | 6021 |
"return true if part of aString matches myself, |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
6022 |
where self may contain GLOB meta-match |
12111 | 6023 |
characters $* (to match any string) or $# (to match any character) |
6024 |
or [...] to match a set of characters. |
|
6025 |
If ignoreCase is true, lower/uppercase are considered the same. |
|
6026 |
The escape character is the backQuote. |
|
6027 |
||
6028 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20874 | 6029 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
6030 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 6031 |
NOTICE: the receiver is the match pattern" |
6032 |
||
6033 |
^ self |
|
20874 | 6034 |
match:aString from:start to:stop caseSensitive:ignoreCase not |
6035 |
escapeCharacter:(self class matchEscapeCharacter) |
|
12111 | 6036 |
|
6037 |
" |
|
6038 |
'*ute*' match:'12345COMPUTER' from:1 to:5 ignoreCase:true |
|
6039 |
'*ute*' match:'12345COMPUTER' from:6 to:13 ignoreCase:true |
|
6040 |
" |
|
6041 |
||
6042 |
"Modified: / 10.11.1998 / 21:43:46 / cg" |
|
6043 |
! |
|
6044 |
||
6045 |
match:aString from:start to:stop ignoreCase:ignoreCase escapeCharacter:escape |
|
15734 | 6046 |
<resource: #obsolete> |
12111 | 6047 |
"return true if part of aString matches myself, |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
6048 |
where self may contain GLOB meta-match |
12111 | 6049 |
characters $* (to match any string) or $# (to match any character) |
6050 |
or [...] to match a set of characters. |
|
6051 |
If ignoreCase is true, lower/uppercase are considered the same. |
|
6052 |
||
6053 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20874 | 6054 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
6055 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 6056 |
NOTICE: the receiver is the match pattern" |
6057 |
||
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
6058 |
^ self |
20874 | 6059 |
match:aString from:start to:stop |
6060 |
caseSensitive:ignoreCase not |
|
6061 |
escapeCharacter:escape |
|
12111 | 6062 |
|
6063 |
" |
|
6064 |
'*ute*' match:'12345COMPUTER' from:1 to:5 ignoreCase:true |
|
6065 |
'*ute*' match:'12345COMPUTER' from:6 to:13 ignoreCase:true |
|
6066 |
" |
|
6067 |
||
6068 |
"Modified: / 10.11.1998 / 21:43:46 / cg" |
|
6069 |
! |
|
6070 |
||
6071 |
match:aString ignoreCase:ignoreCase |
|
15734 | 6072 |
<resource: #obsolete> |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
6073 |
"return true if aString matches self, where self may contain GLOB meta-match |
12111 | 6074 |
characters $* (to match any string) or $# (to match any character) |
6075 |
or [...] to match a set of characters. |
|
6076 |
If ignoreCase is true, lower/uppercase are considered the same. |
|
6077 |
The escape character is the backQuote. |
|
6078 |
||
6079 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20874 | 6080 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
6081 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 6082 |
NOTICE: the receiver is the match pattern" |
6083 |
||
15734 | 6084 |
"/ leave it this way, in case a customer has redefined it for performance (for a while) |
19033 | 6085 |
^ self match:aString from:1 to:aString size caseSensitive:ignoreCase not |
12111 | 6086 |
|
6087 |
" |
|
6088 |
'*ute*' match:'COMPUTER' ignoreCase:true |
|
6089 |
'*uter' match:'COMPUTER' ignoreCase:false |
|
6090 |
'[abcd]*' match:'computer' ignoreCase:false |
|
6091 |
'[abcd]*' match:'Computer' ignoreCase:false |
|
6092 |
'[a-k]*' match:'komputer' ignoreCase:false |
|
6093 |
'[a-k]*' match:'zomputer' ignoreCase:false |
|
6094 |
'[a-k]*' match:'Komputer' ignoreCase:false |
|
6095 |
'[a-k]*' match:'Komputer' ignoreCase:true |
|
6096 |
'*some*compl*ern*' match:'this is some more complicated pattern match' ignoreCase:true |
|
6097 |
'*some*compl*ern*' match:'this is another complicated pattern match' ignoreCase:true |
|
6098 |
||
6099 |
Time millisecondsToRun:[ |
|
20874 | 6100 |
Symbol allInstancesDo:[:sym | |
6101 |
'[ab]*' match:sym ignoreCase:false |
|
6102 |
] |
|
12111 | 6103 |
]. |
6104 |
Time millisecondsToRun:[ |
|
20874 | 6105 |
Symbol allInstancesDo:[:sym | |
6106 |
'*at:*' match:sym ignoreCase:false |
|
6107 |
] |
|
12111 | 6108 |
]. |
6109 |
" |
|
6110 |
||
6111 |
"Modified: 2.4.1997 / 17:28:58 / cg" |
|
6112 |
! |
|
6113 |
||
6114 |
match:aString ignoreCase:ignoreCase escapeCharacter:escape |
|
15734 | 6115 |
<resource: #obsolete> |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
6116 |
"return true if aString matches self, where self may contain GLOB meta-match |
12111 | 6117 |
characters $* (to match any string) or $# (to match any character) |
6118 |
or [...] to match a set of characters. |
|
6119 |
If ignoreCase is true, lower/uppercase are considered the same. |
|
6120 |
||
6121 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20874 | 6122 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
6123 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 6124 |
NOTICE: the receiver is the match pattern" |
6125 |
||
19033 | 6126 |
^ self match:aString from:1 to:aString size caseSensitive:ignoreCase not escapeCharacter:escape |
12111 | 6127 |
|
6128 |
" |
|
6129 |
'*ute*' match:'COMPUTER' ignoreCase:true |
|
6130 |
'*uter' match:'COMPUTER' ignoreCase:false |
|
6131 |
'[abcd]*' match:'computer' ignoreCase:false |
|
6132 |
'[abcd]*' match:'Computer' ignoreCase:false |
|
6133 |
'[a-k]*' match:'komputer' ignoreCase:false |
|
6134 |
'[a-k]*' match:'zomputer' ignoreCase:false |
|
6135 |
'[a-k]*' match:'Komputer' ignoreCase:false |
|
6136 |
'[a-k]*' match:'Komputer' ignoreCase:true |
|
6137 |
'*some*compl*ern*' match:'this is some more complicated pattern match' ignoreCase:true |
|
6138 |
'*some*compl*ern*' match:'this is another complicated pattern match' ignoreCase:true |
|
6139 |
||
6140 |
Time millisecondsToRun:[ |
|
20874 | 6141 |
Symbol allInstancesDo:[:sym | |
6142 |
'[ab]*' match:sym ignoreCase:false |
|
6143 |
] |
|
12111 | 6144 |
]. |
6145 |
Time millisecondsToRun:[ |
|
20874 | 6146 |
Symbol allInstancesDo:[:sym | |
6147 |
'*at:*' match:sym ignoreCase:false |
|
6148 |
] |
|
12111 | 6149 |
]. |
6150 |
" |
|
6151 |
||
6152 |
"Modified: 2.4.1997 / 17:28:58 / cg" |
|
6153 |
! |
|
6154 |
||
23287 | 6155 |
matches:aGlobPatternString |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
6156 |
"return true if the receiver matches aString, where aPatternString may contain GLOB meta-match |
12111 | 6157 |
characters $* (to match any string) or $# (to match any character). |
6158 |
or [...] to match a set of characters. |
|
6159 |
Lower/uppercase are considered different. |
|
6160 |
||
6161 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20874 | 6162 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
6163 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 6164 |
NOTICE: the receiver is the match pattern" |
6165 |
||
23287 | 6166 |
^ aGlobPatternString match:self |
6167 |
||
6168 |
"Modified (format): / 18-08-2018 / 20:51:45 / Claus Gittinger" |
|
14054
48d3fab77bd9
added: #matches:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
14053
diff
changeset
|
6169 |
! |
48d3fab77bd9
added: #matches:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
14053
diff
changeset
|
6170 |
|
15734 | 6171 |
matches:aPatternString caseSensitive:caseSensitive |
6172 |
"return true if the receiver matches aString, where aPatternString may contain GLOB meta-match |
|
6173 |
characters $* (to match any string) or $# (to match any character). |
|
6174 |
or [...] to match a set of characters. |
|
6175 |
||
6176 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20874 | 6177 |
NOT the ST-80 meaning. |
15734 | 6178 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
6179 |
NOTICE: the receiver is the match pattern" |
|
6180 |
||
6181 |
^ aPatternString match:self caseSensitive:caseSensitive |
|
6182 |
! |
|
6183 |
||
14054
48d3fab77bd9
added: #matches:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
14053
diff
changeset
|
6184 |
matches:aPatternString ignoreCase:ignoreCase |
15734 | 6185 |
<resource: #obsolete> |
14054
48d3fab77bd9
added: #matches:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
14053
diff
changeset
|
6186 |
"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
|
6187 |
characters $* (to match any string) or $# (to match any character). |
48d3fab77bd9
added: #matches:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
14053
diff
changeset
|
6188 |
or [...] to match a set of characters. |
48d3fab77bd9
added: #matches:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
14053
diff
changeset
|
6189 |
Lower/uppercase are considered different. |
48d3fab77bd9
added: #matches:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
14053
diff
changeset
|
6190 |
|
48d3fab77bd9
added: #matches:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
14053
diff
changeset
|
6191 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
20874 | 6192 |
NOT the ST-80 meaning. |
14054
48d3fab77bd9
added: #matches:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
14053
diff
changeset
|
6193 |
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
|
6194 |
NOTICE: the receiver is the match pattern" |
48d3fab77bd9
added: #matches:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
14053
diff
changeset
|
6195 |
|
15734 | 6196 |
^ aPatternString match:self caseSensitive:ignoreCase not |
14054
48d3fab77bd9
added: #matches:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
14053
diff
changeset
|
6197 |
|
48d3fab77bd9
added: #matches:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
14053
diff
changeset
|
6198 |
"Created: / 08-03-2012 / 03:11:11 / cg" |
12111 | 6199 |
! ! |
6200 |
||
6201 |
||
24250 | 6202 |
|
9192 | 6203 |
!CharacterArray methodsFor:'padded copying'! |
6204 |
||
6205 |
centerPaddedTo:newSize |
|
19464 | 6206 |
"return a new string consisting of the receiver's characters, |
6207 |
plus spaces up to length and center the receiver's characters in |
|
6208 |
the resulting string. |
|
6209 |
If the receiver's size is equal or greater than the length argument, |
|
6210 |
the original receiver is returned unchanged." |
|
9192 | 6211 |
|
6212 |
^ self centerPaddedTo:newSize with:(Character space) |
|
6213 |
||
6214 |
" |
|
6215 |
'foo' centerPaddedTo:10 |
|
6216 |
123 printString centerPaddedTo:10 |
|
6217 |
" |
|
6218 |
||
6219 |
"Created: 25.11.1995 / 10:53:57 / cg" |
|
6220 |
! |
|
9190 | 6221 |
|
6222 |
centerPaddedTo:size with:padCharacter |
|
6223 |
"return a new string of length size, which contains the receiver |
|
6224 |
centered (i.e. padded on both sides). |
|
6225 |
Characters are filled with padCharacter. |
|
19464 | 6226 |
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
|
6227 |
the original receiver is returned unchanged." |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
6228 |
|
9190 | 6229 |
|len s| |
6230 |
||
6231 |
len := self size. |
|
9192 | 6232 |
(len < size) ifTrue:[ |
20874 | 6233 |
s := self species new:size withAll:padCharacter. |
6234 |
s replaceFrom:(size - len) // 2 + 1 with:self. |
|
6235 |
^ s |
|
9192 | 6236 |
] |
6237 |
||
9190 | 6238 |
" |
6239 |
'foo' centerPaddedTo:11 with:$. |
|
6240 |
'fooBar' centerPaddedTo:5 with:$. |
|
6241 |
123 printString centerPaddedTo:10 with:$. |
|
6242 |
(' ' , 123 printString) centerPaddedTo:10 with:$. |
|
6243 |
(Float pi printString) centerPaddedTo:15 with:(Character space) |
|
6244 |
(Float pi printString) centerPaddedTo:15 with:$- |
|
6245 |
(' ' , Float pi class name) centerPaddedTo:15 with:$. |
|
6246 |
" |
|
815
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
6247 |
! |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
6248 |
|
9190 | 6249 |
decimalPaddedTo:size and:afterPeriod at:decimalCharacter |
815
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
6250 |
"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
|
6251 |
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
|
6252 |
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
|
6253 |
to be padded). |
19464 | 6254 |
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
|
6255 |
the original receiver is returned unchanged. |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
6256 |
(sounds complicated ? -> see examples below)." |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
6257 |
|
8874 | 6258 |
^ self |
20874 | 6259 |
decimalPaddedTo:size |
6260 |
and:afterPeriod |
|
6261 |
at:decimalCharacter |
|
6262 |
withLeft:(Character space) |
|
6263 |
right:$0 |
|
815
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
6264 |
|
8874 | 6265 |
" |
6266 |
'123' decimalPaddedTo:10 and:3 at:$. -> ' 123 ' |
|
6267 |
'123' decimalPaddedTo:10 and:3 at:$. -> ' 123.000' |
|
6268 |
'123.' decimalPaddedTo:10 and:3 at:$. -> ' 123.000' |
|
815
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
6269 |
'123.1' decimalPaddedTo:10 and:3 at:$. -> ' 123.100' |
8874 | 6270 |
'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
|
6271 |
'123.123' decimalPaddedTo:10 and:3 at:$. -> ' 123.123' |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
6272 |
" |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
6273 |
|
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
6274 |
"Created: 23.12.1995 / 13:11:52 / cg" |
9192 | 6275 |
! |
6276 |
||
6277 |
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
|
6278 |
"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
|
6279 |
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
|
6280 |
of the period. |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
6281 |
Characters on the left are filled with leftPadChar. |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
6282 |
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
|
6283 |
otherwise, if missing, a decimal point is added and right characters filled with this. |
19464 | 6284 |
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
|
6285 |
the original receiver is returned unchanged. |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
6286 |
(sounds complicated ? -> see examples below)." |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
6287 |
|
5230
655c21ffe6cc
care for negative size of rest-string when decimalPadding
Claus Gittinger <cg@exept.de>
parents:
5221
diff
changeset
|
6288 |
|s idx n rest| |
815
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
6289 |
|
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
6290 |
idx := self indexOf:decimalCharacter. |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
6291 |
idx == 0 ifTrue:[ |
20874 | 6292 |
"/ |
6293 |
"/ no decimal point found; adjust string to the left of the period column |
|
6294 |
"/ |
|
6295 |
rightPadChar isNil ifTrue:[ |
|
6296 |
s := self , (self species new:afterPeriod + 1 withAll:leftPadChar) |
|
6297 |
] ifFalse:[ |
|
6298 |
s:= self , decimalCharacter asString , (self species new:afterPeriod withAll:rightPadChar). |
|
6299 |
]. |
|
815
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
6300 |
] ifFalse:[ |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
6301 |
|
20874 | 6302 |
"/ the number of after-decimalPoint characters |
6303 |
n := self size - idx. |
|
6304 |
rest := afterPeriod - n. |
|
6305 |
rest > 0 ifTrue:[ |
|
6306 |
s := (self species new:rest withAll:(rightPadChar ? leftPadChar)). |
|
6307 |
] ifFalse:[ |
|
6308 |
s := '' |
|
6309 |
]. |
|
6310 |
s := self , s. |
|
815
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
6311 |
]. |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
6312 |
|
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
6313 |
^ s leftPaddedTo:size with:leftPadChar |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
6314 |
|
8874 | 6315 |
" |
6316 |
'123' decimalPaddedTo:10 and:3 at:$. withLeft:(Character space) right:nil -> ' 123 ' |
|
6317 |
'123' decimalPaddedTo:10 and:3 at:$. withLeft:(Character space) right:$0 -> ' 123.000' |
|
6318 |
'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
|
6319 |
'123.1' decimalPaddedTo:10 and:3 at:$. withLeft:(Character space) right:$0 -> ' 123.100' |
8874 | 6320 |
'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
|
6321 |
'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
|
6322 |
" |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
6323 |
|
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
6324 |
"Modified: 23.12.1995 / 13:08:18 / cg" |
9192 | 6325 |
! |
6326 |
||
6327 |
leftPaddedTo:size |
|
6328 |
"return a new string of length size, which contains the receiver |
|
6329 |
right-adjusted (i.e. padded on the left). |
|
6330 |
Characters on the left are filled with spaces. |
|
19464 | 6331 |
If the receiver's size is equal or greater than the length argument, |
9192 | 6332 |
the original receiver is returned unchanged." |
6333 |
||
6334 |
^ self leftPaddedTo:size with:(Character space) |
|
6335 |
||
6336 |
" |
|
6337 |
'foo' leftPaddedTo:10 |
|
6338 |
'fooBar' leftPaddedTo:5 |
|
6339 |
123 printString leftPaddedTo:10 |
|
6340 |
" |
|
6341 |
! |
|
608 | 6342 |
|
6343 |
paddedTo:newSize |
|
19753 | 6344 |
"return a new string consisting of the receiver's characters, |
23945 | 6345 |
plus spaces up to length. |
6346 |
If the receiver's size is equal or greater than the length argument, |
|
6347 |
the original receiver is returned unchanged." |
|
608 | 6348 |
|
6349 |
^ self paddedTo:newSize with:(Character space) |
|
6350 |
||
6351 |
" |
|
8874 | 6352 |
'foo' paddedTo:10 |
6353 |
123 printString paddedTo:10 |
|
15734 | 6354 |
'12345678901234' paddedTo:10 |
608 | 6355 |
" |
23945 | 6356 |
|
6357 |
"Modified (format): / 21-03-2019 / 12:56:24 / Claus Gittinger" |
|
64 | 6358 |
! ! |
6359 |
||
9192 | 6360 |
!CharacterArray methodsFor:'printing & storing'! |
608 | 6361 |
|
6362 |
article |
|
17137 | 6363 |
"return an article string for the receiver. |
6364 |
The original code only looked at the first character being a vowel; |
|
6365 |
this has been enhanced by some heuristics - not perfect, still." |
|
608 | 6366 |
|
17241 | 6367 |
|firstChar secondChar thirdChar| |
608 | 6368 |
|
8874 | 6369 |
firstChar := (self at:1) asLowercase. |
24041 | 6370 |
((firstChar isVowel and:[firstChar ~~ $u]) |
6371 |
or:[firstChar == $x] |
|
6372 |
) ifTrue:[ |
|
20874 | 6373 |
^ 'an' |
608 | 6374 |
]. |
17043 | 6375 |
|
6376 |
(self size >= 3) ifTrue:[ |
|
20874 | 6377 |
secondChar := (self at:2) asLowercase. |
6378 |
||
6379 |
"/ may need more here... |
|
24041 | 6380 |
(firstChar == $r |
6381 |
and:[secondChar == $b] |
|
6382 |
) ifTrue:[ |
|
20874 | 6383 |
^ 'an' |
6384 |
]. |
|
6385 |
||
6386 |
thirdChar := (self at:3) asLowercase. |
|
6387 |
||
24041 | 6388 |
(firstChar == $s |
6389 |
and:[secondChar == $c |
|
6390 |
and:[thirdChar == $h]] |
|
6391 |
) ifTrue:[ |
|
6392 |
^ 'a' |
|
6393 |
]. |
|
6394 |
||
20874 | 6395 |
(firstChar isVowel not |
6396 |
and:[(secondChar isVowel or:[secondChar == $y]) not |
|
6397 |
and:[thirdChar isVowel not ]]) ifTrue:[ |
|
6398 |
"/ exceptions: 3 non-vowels in a row: looks like an abbreviation |
|
6399 |
(self size > 4) ifTrue:[ |
|
6400 |
(firstChar == $s) ifTrue:[ |
|
6401 |
((secondChar == $c and:[thirdChar == $r]) |
|
6402 |
or:[ (secondChar == $t and:[thirdChar == $r]) ]) ifTrue:[ |
|
6403 |
(self at:4) isVowel ifTrue:[ |
|
6404 |
^ 'a' |
|
6405 |
] |
|
6406 |
] |
|
6407 |
]. |
|
6408 |
]. |
|
6409 |
"/ an abbreviation; treat x, s as vowels |
|
6410 |
(firstChar == $x or:[ firstChar == $s ]) ifTrue:[^ 'an']. |
|
6411 |
] |
|
17043 | 6412 |
]. |
608 | 6413 |
^ 'a' |
5848 | 6414 |
|
6415 |
" |
|
24041 | 6416 |
'uboot' article. |
6417 |
'xmas' article. |
|
6418 |
'alarm' article. |
|
6419 |
'baby' article. |
|
6420 |
'sql' article. |
|
6421 |
'scr' article. |
|
6422 |
'screen' article. |
|
6423 |
'scrollbar' article. |
|
6424 |
'scrs' article. |
|
6425 |
'cvs' article. |
|
6426 |
'cvssource' article. |
|
6427 |
'symbol' article. |
|
6428 |
'string' article. |
|
6429 |
'rbparser' article. |
|
6430 |
'scheme' article. |
|
19683 | 6431 |
" |
6432 |
||
6433 |
"Modified (comment): / 01-05-2016 / 10:57:25 / cg" |
|
24041 | 6434 |
"Modified (comment): / 03-04-2019 / 10:08:57 / Claus Gittinger" |
608 | 6435 |
! |
6436 |
||
8049
ccd0e8f26439
printing - migrating towards unicodeSupport in ST-code
Claus Gittinger <cg@exept.de>
parents:
8040
diff
changeset
|
6437 |
basicStoreString |
ccd0e8f26439
printing - migrating towards unicodeSupport in ST-code
Claus Gittinger <cg@exept.de>
parents:
8040
diff
changeset
|
6438 |
"return a String for storing myself" |
ccd0e8f26439
printing - migrating towards unicodeSupport in ST-code
Claus Gittinger <cg@exept.de>
parents:
8040
diff
changeset
|
6439 |
|
ccd0e8f26439
printing - migrating towards unicodeSupport in ST-code
Claus Gittinger <cg@exept.de>
parents:
8040
diff
changeset
|
6440 |
|s n index| |
ccd0e8f26439
printing - migrating towards unicodeSupport in ST-code
Claus Gittinger <cg@exept.de>
parents:
8040
diff
changeset
|
6441 |
|
ccd0e8f26439
printing - migrating towards unicodeSupport in ST-code
Claus Gittinger <cg@exept.de>
parents:
8040
diff
changeset
|
6442 |
n := self occurrencesOf:$'. |
11320 | 6443 |
n ~~ 0 ifTrue:[ |
20874 | 6444 |
s := self species new:(n + 2 + self size). |
6445 |
s at:1 put:$'. |
|
6446 |
index := 2. |
|
6447 |
self do:[:thisChar | |
|
6448 |
(thisChar == $') ifTrue:[ |
|
6449 |
s at:index put:thisChar. |
|
6450 |
index := index + 1. |
|
6451 |
]. |
|
6452 |
s at:index put:thisChar. |
|
6453 |
index := index + 1. |
|
6454 |
]. |
|
6455 |
s at:index put:$'. |
|
6456 |
^ s |
|
9192 | 6457 |
]. |
11320 | 6458 |
|
9192 | 6459 |
^ '''' , self , '''' |
15523 | 6460 |
|
6461 |
" |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
6462 |
'''immutable'' string' asImmutableString basicStoreString |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
6463 |
'immutable string' asImmutableString basicStoreString |
15523 | 6464 |
" |
6465 |
||
6466 |
"Modified: / 14-07-2013 / 19:20:49 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
9192 | 6467 |
! |
8049
ccd0e8f26439
printing - migrating towards unicodeSupport in ST-code
Claus Gittinger <cg@exept.de>
parents:
8040
diff
changeset
|
6468 |
|
11774
6f6e2b71b607
displayOn (inspector's interface) was broken
Claus Gittinger <cg@exept.de>
parents:
11754
diff
changeset
|
6469 |
displayOn:aGCOrStream |
6f6e2b71b607
displayOn (inspector's interface) was broken
Claus Gittinger <cg@exept.de>
parents:
11754
diff
changeset
|
6470 |
"Compatibility |
6f6e2b71b607
displayOn (inspector's interface) was broken
Claus Gittinger <cg@exept.de>
parents:
11754
diff
changeset
|
6471 |
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
|
6472 |
OR: |
6f6e2b71b607
displayOn (inspector's interface) was broken
Claus Gittinger <cg@exept.de>
parents:
11754
diff
changeset
|
6473 |
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
|
6474 |
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
|
6475 |
(although the fallBack is to display its printString ...)" |
6f6e2b71b607
displayOn (inspector's interface) was broken
Claus Gittinger <cg@exept.de>
parents:
11754
diff
changeset
|
6476 |
|
6f6e2b71b607
displayOn (inspector's interface) was broken
Claus Gittinger <cg@exept.de>
parents:
11754
diff
changeset
|
6477 |
"/ what a kludge - Dolphin and Squeak mean: printOn: a stream; |
21540 | 6478 |
"/ old ST80 means: draw-yourself on a GC. |
6479 |
aGCOrStream isStream ifFalse:[ |
|
6480 |
^ super displayOn:aGCOrStream. |
|
6481 |
]. |
|
6482 |
||
6483 |
self storeOn:aGCOrStream. |
|
6484 |
||
6485 |
"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
|
6486 |
! |
6f6e2b71b607
displayOn (inspector's interface) was broken
Claus Gittinger <cg@exept.de>
parents:
11754
diff
changeset
|
6487 |
|
608 | 6488 |
printOn:aStream |
6489 |
"print the receiver on aStream" |
|
6490 |
||
6491 |
aStream nextPutAll:self |
|
6492 |
! |
|
6493 |
||
6494 |
printString |
|
13079
111e7e456648
Implement display with #displayOn: instead of #displayString
Stefan Vogel <sv@exept.de>
parents:
13073
diff
changeset
|
6495 |
"return a string for printing - that's myself" |
608 | 6496 |
|
6497 |
^ self |
|
3578 | 6498 |
! |
6499 |
||
6500 |
printWithQuotesDoubledOn:aStream |
|
6501 |
"put the raw storeString of myself on aStream" |
|
6502 |
||
6503 |
self do:[:thisChar | |
|
20874 | 6504 |
(thisChar == $') ifTrue:[aStream nextPut:thisChar]. |
6505 |
aStream nextPut:thisChar |
|
3578 | 6506 |
] |
6507 |
||
6508 |
"Modified: / 15.6.1998 / 17:21:17 / cg" |
|
6509 |
"Created: / 15.6.1998 / 17:22:13 / cg" |
|
12171 | 6510 |
! |
6511 |
||
15788 | 6512 |
printXmlQuotedOn:aStream |
23549 | 6513 |
"convert aString to a valid XML string |
6514 |
that can be used for XML attributes. |
|
6515 |
Can also be used for XML text, but better use #printXmlTextQuotedOn:. |
|
23551 | 6516 |
Various senders expect, that the output is ISO-8859-1 compatible |
6517 |
(they don't care for encoding), so for now, we always generate 8-bit chars. |
|
23549 | 6518 |
TODO: care for 16bit UNICODE string and escape chars ..." |
6519 |
||
23548 | 6520 |
<resource: #todo> |
15788 | 6521 |
|
6522 |
self do:[:eachChar | |
|
20874 | 6523 |
eachChar == $< ifTrue:[ |
6524 |
aStream nextPutAll:'<' "mapping needed for xml text" |
|
6525 |
] ifFalse:[ eachChar == $& ifTrue:[ |
|
6526 |
aStream nextPutAll:'&' "mapping needed for all" |
|
6527 |
] ifFalse:[ eachChar == $> ifTrue:[ |
|
6528 |
aStream nextPutAll:'>' "mapping needed for comments" |
|
6529 |
] ifFalse:[ eachChar == $' ifTrue:[ |
|
6530 |
aStream nextPutAll:''' "mapping needed for attributes" |
|
6531 |
] ifFalse:[ eachChar == $" ifTrue:[ |
|
6532 |
aStream nextPutAll:'"' "mapping needed for attributes" |
|
6533 |
] ifFalse:[ |
|
6534 |
|codePoint| |
|
6535 |
codePoint := eachChar codePoint. |
|
23552 | 6536 |
(codePoint < 16r20 |
6537 |
or:[codePoint >= 16r7F |
|
6538 |
and:[codePoint <= 16r9F |
|
6539 |
or:[codePoint > 16rFF] "for now, always make sure, that output is ISO-8859-1 compatible"] |
|
6540 |
]) ifTrue:[ |
|
20874 | 6541 |
aStream nextPutAll:'&#'. |
23549 | 6542 |
codePoint printOn:aStream base:16. |
20874 | 6543 |
aStream nextPut:$;. |
23549 | 6544 |
] ifFalse:[ |
6545 |
aStream nextPut:eachChar |
|
6546 |
]. |
|
6547 |
]]]]] |
|
6548 |
]. |
|
6549 |
||
23552 | 6550 |
"Modified: / 03-12-2018 / 15:21:27 / Stefan Vogel" |
15788 | 6551 |
! |
6552 |
||
6553 |
printXmlTextQuotedOn:aStream |
|
23290 | 6554 |
<resource: #todo> |
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
6555 |
"convert aString to a valid XML string |
15788 | 6556 |
that can be used for XML text. |
6557 |
Here line formatting characters are not escaped. |
|
23549 | 6558 |
For XML text (as opposed to XML attribute), $' and $"" need |
6559 |
not to be transliterated (see https://www.w3.org/TR/xml Section 2.4). |
|
23551 | 6560 |
Various senders expect, that the output is ISO-8859-1 compatible |
6561 |
(they don't care for encoding), so for now, we always generate 8-bit chars. |
|
23549 | 6562 |
TODO: care for 16bit UNICODE string and escape chars |
6563 |
- but we need to know the XML file encoding" |
|
15788 | 6564 |
|
6565 |
self do:[:eachChar | |
|
20874 | 6566 |
eachChar == $< ifTrue:[ |
6567 |
aStream nextPutAll:'<' "mapping needed for xml text" |
|
6568 |
] ifFalse:[ eachChar == $& ifTrue:[ |
|
6569 |
aStream nextPutAll:'&' "mapping needed for all" |
|
23290 | 6570 |
] ifFalse:[ eachChar == $> ifTrue:[ |
23549 | 6571 |
aStream nextPutAll:'>' "mapping needed only for ]]>" |
20874 | 6572 |
] ifFalse:[ |
6573 |
|codePoint| |
|
6574 |
codePoint := eachChar codePoint. |
|
6575 |
((codePoint < 16r20 and:[codePoint ~~ 9 and:[codePoint ~~ 10 and:[codePoint ~~ 13]]]) |
|
23552 | 6576 |
or:[codePoint >= 16r7F |
6577 |
and:[codePoint <= 16r9F |
|
6578 |
or:[codePoint > 16rFF] "for now, always make sure, that output is ISO-8859-1 compatible"] |
|
6579 |
]) ifTrue:[ |
|
23321 | 6580 |
aStream nextPutAll:'&#x'. |
23290 | 6581 |
codePoint printOn:aStream base:16. |
20874 | 6582 |
aStream nextPut:$;. |
23549 | 6583 |
] ifFalse:[ |
6584 |
aStream nextPut:eachChar |
|
6585 |
]. |
|
6586 |
]]] |
|
23290 | 6587 |
]. |
6588 |
||
6589 |
" |
|
6590 |
String streamContents:[:s|'< & >' printXmlTextQuotedOn:s] |
|
6591 |
String streamContents:[:s|'< & >',(Character value:7) printXmlTextQuotedOn:s] |
|
6592 |
String streamContents:[:s|'< & >',(Character value:129) printXmlTextQuotedOn:s] |
|
6593 |
String streamContents:[:s|'< & >',(Character value:1000) printXmlTextQuotedOn:s] |
|
6594 |
" |
|
6595 |
||
23321 | 6596 |
"Modified: / 30-08-2018 / 22:39:00 / Claus Gittinger" |
23552 | 6597 |
"Modified: / 03-12-2018 / 15:21:18 / Stefan Vogel" |
15788 | 6598 |
! |
6599 |
||
6600 |
xmlQuotedPrintString |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
6601 |
"convert aString to a valid XML string |
23241 | 6602 |
that can be used for attributes, text, comments an PIs" |
15788 | 6603 |
|
6604 |
^ String streamContents:[:s| |
|
20874 | 6605 |
self printXmlQuotedOn:s |
15788 | 6606 |
]. |
23241 | 6607 |
|
6608 |
"Modified (comment): / 28-07-2018 / 11:11:51 / Claus Gittinger" |
|
9192 | 6609 |
! ! |
6610 |
||
11955 | 6611 |
|
9192 | 6612 |
!CharacterArray methodsFor:'queries'! |
608 | 6613 |
|
23701 | 6614 |
argumentCount |
6615 |
"treating the receiver as a message selector, return how many arguments would it take (ANSI)" |
|
6616 |
||
6617 |
|binopChars| |
|
6618 |
||
6619 |
(self size <= Method maxBinarySelectorSize) ifTrue:[ |
|
6620 |
binopChars := Method binarySelectorCharacters. |
|
6621 |
(self conform:[:eachChar | (binopChars includes:eachChar)]) ifTrue:[ |
|
6622 |
^ 1 |
|
6623 |
]. |
|
6624 |
]. |
|
6625 |
^ self occurrencesOf:$: |
|
6626 |
||
6627 |
" |
|
6628 |
'foo:bar:' argumentCount |
|
6629 |
#foo:bar: argumentCount |
|
6630 |
'hello' argumentCount |
|
6631 |
'+' argumentCount |
|
6632 |
'++' argumentCount |
|
6633 |
'+++' argumentCount |
|
6634 |
'|' argumentCount |
|
6635 |
'?' argumentCount |
|
6636 |
'_' argumentCount |
|
6637 |
'_:' argumentCount |
|
6638 |
'_:_:' argumentCount |
|
6639 |
'<->' argumentCount |
|
6640 |
'<' argumentCount |
|
6641 |
#'<' argumentCount |
|
6642 |
" |
|
6643 |
||
6644 |
"Modified (comment): / 06-02-2017 / 13:48:57 / cg" |
|
6645 |
! |
|
6646 |
||
4429 | 6647 |
bitsPerCharacter |
18662 | 6648 |
"return the underlying string's bitsPerCharacter |
4429 | 6649 |
(i.e. is it a regular String or a TwoByteString)" |
6650 |
||
8457 | 6651 |
|string max| |
6652 |
||
6653 |
(string := self string) ~~ self ifTrue:[ |
|
20874 | 6654 |
^ string bitsPerCharacter |
8457 | 6655 |
]. |
6656 |
||
6657 |
max := 8. |
|
6658 |
self do:[:eachCharacter | |
|
20874 | 6659 |
max := max max:(eachCharacter bitsPerCharacter) |
9192 | 6660 |
]. |
6661 |
^ max |
|
6662 |
||
6663 |
" |
|
6664 |
'hello' bitsPerCharacter |
|
6665 |
'hello' asText allBold bitsPerCharacter |
|
6666 |
" |
|
6667 |
! |
|
8457 | 6668 |
|
19757 | 6669 |
bytesPerCharacter |
23971 | 6670 |
"return the underlying string's bytesPerCharacter |
19757 | 6671 |
(i.e. is it a regular String or a TwoByteString)" |
6672 |
||
6673 |
|string max| |
|
6674 |
||
6675 |
(string := self string) ~~ self ifTrue:[ |
|
20874 | 6676 |
^ string bytesPerCharacter |
19757 | 6677 |
]. |
6678 |
||
6679 |
max := 1. |
|
6680 |
self do:[:eachCharacter | |
|
20874 | 6681 |
max := max max:(eachCharacter bytesPerCharacter) |
19757 | 6682 |
]. |
6683 |
^ max |
|
6684 |
||
6685 |
" |
|
6686 |
'hello' bytesPerCharacter |
|
6687 |
'hello' asUnicode16String bytesPerCharacter |
|
6688 |
'hello' asText allBold bytesPerCharacter |
|
6689 |
" |
|
23971 | 6690 |
|
6691 |
"Modified (comment): / 25-03-2019 / 16:24:02 / Claus Gittinger" |
|
6692 |
! |
|
6693 |
||
6694 |
bytesPerCharacterNeeded |
|
6695 |
"return the actual underlying string's required bytesPerCharacter |
|
6696 |
(i.e. checks if all characters really need that depth)" |
|
6697 |
||
6698 |
|string max mySize| |
|
6699 |
||
6700 |
(string := self string) ~~ self ifTrue:[ |
|
6701 |
^ string bytesPerCharacterNeeded |
|
6702 |
]. |
|
6703 |
||
6704 |
mySize := self bytesPerCharacter. |
|
6705 |
||
6706 |
max := 1. |
|
6707 |
self do:[:eachCharacter | |
|
6708 |
max := max max:(eachCharacter bytesPerCharacter). |
|
6709 |
max == mySize ifTrue:[^ max]. |
|
6710 |
]. |
|
6711 |
^ max |
|
6712 |
||
6713 |
" |
|
6714 |
'hello' bytesPerCharacter -> 1 |
|
6715 |
'hello' bytesPerCharacterNeeded -> 1 |
|
6716 |
||
6717 |
'hello' asUnicode16String bytesPerCharacter -> 2 |
|
6718 |
'hello' asUnicode16String bytesPerCharacterNeeded -> 1 |
|
6719 |
" |
|
6720 |
||
6721 |
"Created: / 25-03-2019 / 16:22:00 / Claus Gittinger" |
|
19757 | 6722 |
! |
6723 |
||
23701 | 6724 |
characterSize |
6725 |
"answer the size in bits of my largest character (actually only 7, 8, 16 or 32)" |
|
6726 |
||
6727 |
|string max |
|
6728 |
sz "{ Class:SmallInteger}" | |
|
6729 |
||
6730 |
(string := self string) ~~ self ifTrue:[ |
|
6731 |
^ string characterSize. |
|
6732 |
]. |
|
6733 |
||
6734 |
sz := self size. |
|
6735 |
max := 7. |
|
6736 |
1 to:sz do:[:idx | |
|
6737 |
|thisSize| |
|
6738 |
||
6739 |
thisSize := (self at:idx) characterSize. |
|
6740 |
thisSize > max ifTrue:[ |
|
6741 |
max := thisSize. |
|
6742 |
max == 32 ifTrue:[ |
|
6743 |
"shortcut: we know, that max size is 32" |
|
6744 |
^ 32. |
|
6745 |
]. |
|
6746 |
]. |
|
6747 |
]. |
|
6748 |
||
6749 |
^ max. |
|
6750 |
||
6751 |
" |
|
6752 |
'hello' characterSize |
|
6753 |
'hello' asUnicode32String characterSize |
|
6754 |
'helloü' asUnicode32String characterSize |
|
6755 |
'hello' asText allBold characterSize |
|
6756 |
" |
|
6757 |
! |
|
6758 |
||
6759 |
contains8BitCharacters |
|
6760 |
<resource: #obsolete> |
|
6761 |
||
6762 |
^ self containsNon7BitAscii |
|
6763 |
! |
|
6764 |
||
6765 |
containsNon7BitAscii |
|
6766 |
"return true, if the underlying string contains 8BitCharacters (or widers) |
|
6767 |
(i.e. if it is non-ascii)" |
|
6768 |
||
6769 |
|string |
|
6770 |
sz "{ Class:SmallInteger }"| |
|
6771 |
||
6772 |
(string := self string) ~~ self ifTrue:[ |
|
6773 |
^ string containsNon7BitAscii |
|
6774 |
]. |
|
6775 |
sz := self size. |
|
6776 |
1 to:sz do:[:idx| |
|
6777 |
(self at:idx) codePoint > 16r7F ifTrue:[ |
|
6778 |
^ true. |
|
6779 |
]. |
|
6780 |
]. |
|
6781 |
^ false. |
|
6782 |
||
6783 |
" |
|
6784 |
'hello' asUnicode32String containsNon7BitAscii |
|
6785 |
'hello üöä' asUnicode32String containsNon7BitAscii |
|
6786 |
'hello' asUnicode32String asText allBold containsNon7BitAscii |
|
6787 |
'hello üö' asUnicode32String asText allBold containsNon7BitAscii |
|
6788 |
" |
|
6789 |
! |
|
6790 |
||
18967 | 6791 |
containsNon8BitElements |
6792 |
"return true, if the underlying string contains elements larger than a single byte" |
|
6793 |
||
21322 | 6794 |
|string sz "{ Class:SmallInteger }"| |
6795 |
||
6796 |
(string := self string) ~~ self ifTrue:[ |
|
6797 |
^ string containsNon8BitElements |
|
6798 |
]. |
|
18967 | 6799 |
sz := self size. |
6800 |
1 to:sz do:[:idx| |
|
20874 | 6801 |
(self at:idx) codePoint > 16rFF ifTrue:[ |
6802 |
^ true. |
|
6803 |
]. |
|
18967 | 6804 |
]. |
6805 |
^ false. |
|
6806 |
! |
|
6807 |
||
10903 | 6808 |
continuesWith:aString startingAt:startIndex |
6809 |
"return true, if the receiver beginning at startIndex |
|
22783 | 6810 |
contains the characters in aString. |
6811 |
Returns false if the search reaches the end of the receiver |
|
6812 |
before all characters have been compared." |
|
10903 | 6813 |
|
6814 |
|sz "{Class: SmallInteger }" |
|
6815 |
idx "{Class: SmallInteger }"| |
|
6816 |
||
6817 |
sz := aString size. |
|
22783 | 6818 |
(startIndex + sz - 1) > self size ifTrue:[^ false]. |
6819 |
||
10903 | 6820 |
idx := startIndex. |
22783 | 6821 |
|
10903 | 6822 |
1 to:sz do:[:i | |
20874 | 6823 |
(self at:idx) ~~ (aString at:i) ifTrue:[^ false]. |
6824 |
idx := idx + 1 |
|
10903 | 6825 |
]. |
6826 |
^ true |
|
6827 |
||
6828 |
" |
|
6829 |
'hello world' continuesWith:'world' startingAt:6 |
|
6830 |
'hello world' continuesWith:'world' startingAt:7 |
|
22783 | 6831 |
'hello' continuesWith:'llo' startingAt:3 |
6832 |
'hello' continuesWith:'llow' startingAt:3 |
|
10903 | 6833 |
" |
6834 |
||
6835 |
"Created: 12.5.1996 / 15:46:40 / cg" |
|
6836 |
"Modified: 26.7.1996 / 19:08:36 / cg" |
|
6837 |
! |
|
6838 |
||
6839 |
countWords |
|
6840 |
"return the number of words, which are separated by separators" |
|
6841 |
||
6842 |
|tally "{ Class: SmallInteger }" |
|
6843 |
start "{ Class: SmallInteger }" |
|
6844 |
mySize "{ Class: SmallInteger }" |
|
6845 |
stop ch| |
|
6846 |
||
6847 |
tally := 0. |
|
6848 |
start := 1. |
|
6849 |
mySize := self size. |
|
6850 |
[start <= mySize] whileTrue:[ |
|
20874 | 6851 |
ch := self at:start. |
6852 |
ch isSeparator ifTrue:[ |
|
6853 |
start := start + 1 |
|
6854 |
] ifFalse:[ |
|
6855 |
stop := self indexOfSeparatorStartingAt:start. |
|
6856 |
(stop == 0) ifTrue:[ |
|
6857 |
stop := mySize + 1 |
|
6858 |
]. |
|
6859 |
tally := tally + 1. |
|
6860 |
start := stop |
|
6861 |
] |
|
10903 | 6862 |
]. |
6863 |
^ tally |
|
6864 |
||
6865 |
" |
|
6866 |
'hello world isnt this nice' countWords' |
|
6867 |
" |
|
6868 |
! |
|
6869 |
||
11788 | 6870 |
defaultElement |
6871 |
^ Character space |
|
6872 |
! |
|
6873 |
||
608 | 6874 |
encoding |
1311 | 6875 |
"return the strings encoding, as a symbol. |
7982 | 6876 |
Here, by default, we assume unicode-encoding. |
20006 | 6877 |
Notice, that ISO-8859-1 is a true subset of unicode, |
8874 | 6878 |
and that singleByteStrings are therefore both unicode AND |
7982 | 6879 |
8859-1 encoded." |
6880 |
||
23290 | 6881 |
^ #'iso10646-1'. |
6882 |
"/ ^ #unicode |
|
6883 |
||
6884 |
"Modified: / 22-08-2018 / 09:30:16 / Claus Gittinger" |
|
608 | 6885 |
! |
6886 |
||
1375
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
6887 |
hasChangeOfEmphasis |
14405
1d0d99a95bba
comment/format in:7 methods
Claus Gittinger <cg@exept.de>
parents:
14168
diff
changeset
|
6888 |
"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
|
6889 |
i.e. any non-normal (=emphasized) characters" |
1d0d99a95bba
comment/format in:7 methods
Claus Gittinger <cg@exept.de>
parents:
14168
diff
changeset
|
6890 |
|
1375
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
6891 |
^ false |
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
6892 |
|
1381 | 6893 |
"Created: 12.5.1996 / 12:31:39 / cg" |
1375
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
6894 |
! |
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
6895 |
|
7995
8ddbb403d67b
+hasIcon and hasImage for LabelAndIcon compatibility
Claus Gittinger <cg@exept.de>
parents:
7991
diff
changeset
|
6896 |
hasIcon |
9191 | 6897 |
"for LabelAndIcon compatibility" |
6898 |
||
9192 | 6899 |
^ false |
6900 |
! |
|
6901 |
||
6902 |
hasImage |
|
6903 |
"for LabelAndIcon compatibility" |
|
6904 |
||
6905 |
^ false |
|
6906 |
! |
|
7995
8ddbb403d67b
+hasIcon and hasImage for LabelAndIcon compatibility
Claus Gittinger <cg@exept.de>
parents:
7991
diff
changeset
|
6907 |
|
1388 | 6908 |
heightOn:aGC |
19464 | 6909 |
"return the size of the receiver in device units if displayed on aGC" |
1388 | 6910 |
|
20221 | 6911 |
^ aGC deviceFont heightOf:self "font is already a device font" |
1388 | 6912 |
|
6913 |
" |
|
8874 | 6914 |
'hello world' heightOn:(View new) |
1388 | 6915 |
" |
6916 |
||
6917 |
"Created: 12.5.1996 / 20:09:29 / cg" |
|
6918 |
"Modified: 12.5.1996 / 20:32:05 / cg" |
|
6919 |
! |
|
6920 |
||
23701 | 6921 |
isAlphaNumeric |
6922 |
"return true, if the receiver is some alphanumeric word; |
|
6923 |
i.e. consists of a letter followed by letters or digits." |
|
6924 |
||
6925 |
self isEmpty ifTrue:[ |
|
6926 |
"mhmh what is this ?" |
|
6927 |
^ false |
|
6928 |
]. |
|
6929 |
(self at:1) isLetter ifFalse:[^ false]. |
|
6930 |
^ self conform:[:char | char isLetterOrDigit]. |
|
6931 |
||
6932 |
" |
|
6933 |
'helloWorld' isAlphaNumeric |
|
6934 |
'foo1234' isAlphaNumeric |
|
6935 |
'f1234' isAlphaNumeric |
|
6936 |
'1234' isAlphaNumeric |
|
6937 |
'+' isAlphaNumeric |
|
6938 |
" |
|
6939 |
||
6940 |
"Modified: / 13-10-2006 / 12:53:49 / cg" |
|
6941 |
! |
|
6942 |
||
6943 |
isBinarySelector |
|
6944 |
"treating the receiver as a message selector, return true if it's a binary selector. |
|
6945 |
Notice, that st/x does not have a size <= 2 limit for unaries" |
|
6946 |
||
6947 |
|binopChars| |
|
6948 |
||
6949 |
(self size > Method maxBinarySelectorSize) ifTrue:[^ false]. |
|
6950 |
||
6951 |
binopChars := Method binarySelectorCharacters. |
|
6952 |
^ (self conform:[:char | (binopChars includes:char)]) |
|
6953 |
||
6954 |
" |
|
6955 |
'foo:bar:' isBinarySelector |
|
6956 |
#foo:bar: isBinarySelector |
|
6957 |
'hello' isBinarySelector |
|
6958 |
'+' isBinarySelector |
|
6959 |
'|' isBinarySelector |
|
6960 |
'?' isBinarySelector |
|
6961 |
':' isBinarySelector |
|
6962 |
'a:' isBinarySelector |
|
6963 |
'->' isBinarySelector |
|
6964 |
'<->' isBinarySelector |
|
6965 |
'::' isBinarySelector |
|
6966 |
" |
|
6967 |
||
6968 |
"Modified: / 04-01-1997 / 14:16:14 / cg" |
|
6969 |
"Modified (comment): / 13-02-2017 / 19:57:29 / cg" |
|
6970 |
! |
|
6971 |
||
6972 |
isBlank |
|
6973 |
"return true, if the receiver contains spaces only" |
|
6974 |
||
6975 |
^ self size == 0 or:[(self contains:[:char | char ~~ Character space]) not] |
|
6976 |
||
6977 |
" |
|
6978 |
'' isBlank |
|
6979 |
'' asUnicode16String isBlank |
|
6980 |
' a ' isBlank |
|
6981 |
' ' isBlank |
|
6982 |
' ' asUnicode16String isBlank |
|
6983 |
" |
|
6984 |
||
6985 |
"Modified (comment): / 14-09-2018 / 10:04:18 / Stefan Vogel" |
|
6986 |
! |
|
6987 |
||
6988 |
isInfix |
|
6989 |
"return true, if the receiver is a binary message selector" |
|
6990 |
||
6991 |
^ self first isLetterOrDigit not |
|
6992 |
||
6993 |
" |
|
6994 |
#at:put: isInfix |
|
6995 |
#at: isInfix |
|
6996 |
#+ isInfix |
|
6997 |
#size isInfix |
|
6998 |
" |
|
6999 |
||
7000 |
"Created: / 1.11.1997 / 12:34:55 / cg" |
|
7001 |
"Modified: / 1.11.1997 / 12:36:37 / cg" |
|
7002 |
! |
|
7003 |
||
7004 |
isKeyword |
|
7005 |
"return true, if the receiver is a keyword message selector. |
|
7006 |
This is a quick check, which only looks at the last character. |
|
7007 |
Should only be used, if we already know that the receiver forms a valid selector. |
|
7008 |
To check an arbitrary string, use isKeywordSelector. |
|
7009 |
Bad naming, but compatibility is asking for it." |
|
7010 |
||
7011 |
^ self last == $: |
|
7012 |
||
7013 |
" |
|
7014 |
#at:put: isKeyword |
|
7015 |
#at: isKeyword |
|
7016 |
#+ isKeyword |
|
7017 |
#size isKeyword |
|
7018 |
" |
|
7019 |
||
7020 |
"Created: / 01-11-1997 / 12:34:55 / cg" |
|
7021 |
"Modified (comment): / 30-04-2016 / 18:19:11 / cg" |
|
7022 |
! |
|
7023 |
||
7024 |
isKeywordSelector |
|
7025 |
"return true, iff there are only alphanumeric or underline characters separated by colons. |
|
7026 |
Must end with a colon. |
|
7027 |
You can use this to check an arbitrary string for being valid as a keyword. |
|
7028 |
If you have a valid selector at hand, and need to know if it is a keyword or not, |
|
7029 |
use #isKeyword, which is much faster." |
|
7030 |
||
7031 |
|state| |
|
7032 |
||
7033 |
(self size == 0) ifTrue:[^ false]. |
|
7034 |
(self last == $:) ifFalse:[^ false]. |
|
7035 |
||
7036 |
state := #initial. |
|
7037 |
self do:[:char | |
|
7038 |
(state == #initial or:[ state == #gotColon]) ifTrue:[ |
|
7039 |
(char isLetterOrUnderline) ifFalse:[^ false]. |
|
7040 |
state := #gotCharacter. |
|
7041 |
] ifFalse:[ |
|
7042 |
char == $: ifTrue:[ |
|
7043 |
state := #gotColon. |
|
7044 |
] ifFalse:[ |
|
24254 | 7045 |
(char isLetterOrDigitOrUnderline) ifFalse:[^ false]. |
23701 | 7046 |
]. |
7047 |
]. |
|
7048 |
]. |
|
7049 |
^ state == #gotColon. |
|
7050 |
||
7051 |
" |
|
7052 |
self assert:(':' isKeywordSelector not). |
|
7053 |
self assert:(':a' isKeywordSelector not). |
|
7054 |
self assert:('1:' isKeywordSelector not). |
|
7055 |
self assert:('a:' isKeywordSelector). |
|
7056 |
self assert:('_:' isKeywordSelector). |
|
7057 |
self assert:('_a:' isKeywordSelector). |
|
7058 |
self assert:('_1:' isKeywordSelector). |
|
7059 |
self assert:('_1::' isKeywordSelector not). |
|
7060 |
self assert:('_:_:' isKeywordSelector). |
|
7061 |
self assert:('a:b:' isKeywordSelector). |
|
7062 |
self assert:('aa:bb:' isKeywordSelector). |
|
7063 |
self assert:('aa:bb:a' isKeywordSelector not). |
|
7064 |
self assert:('1:2:' isKeywordSelector not). |
|
7065 |
" |
|
7066 |
||
7067 |
"Modified (comment): / 30-04-2016 / 18:20:14 / cg" |
|
24254 | 7068 |
"Modified: / 05-06-2019 / 17:05:53 / Claus Gittinger" |
23701 | 7069 |
! |
7070 |
||
11828 | 7071 |
isLowercaseFirst |
7072 |
"return true, if the first character is a lowercase character." |
|
7073 |
||
7074 |
^ self size ~~ 0 and:[ self first isLowercase ] |
|
7075 |
||
7076 |
" |
|
12177 | 7077 |
'helloWorld' isLowercaseFirst |
7078 |
'HelloWorld' isLowercaseFirst |
|
11828 | 7079 |
" |
7080 |
! |
|
7081 |
||
23701 | 7082 |
isNameSpaceSelector |
7083 |
"Answer true if the receiver contains chars which form a nameSpace selector name. |
|
7084 |
These are of the form ':<ns>::<sel>', where ns is the NameSpace and sel is the regular selector. |
|
7085 |
For example, the #+ selector as seen by the Foo namespace would be actually #':Foo::+'. |
|
7086 |
This special format (a symbol starting with a colon) was chosen, because almost every other selector |
|
7087 |
is legal, and this can be checked quickly by just looking at the first character. |
|
7088 |
You cannot easily change this algorithm here, as it is also known by the VM's lookup function." |
|
7089 |
||
7090 |
(self at:1) == $: ifFalse:[^ false]. |
|
24120 | 7091 |
^ (self indexOfSubCollection:'::' startingAt:3 ifAbsent:0 caseSensitive:true) ~~ 0. |
23701 | 7092 |
|
7093 |
"test: |
|
7094 |
self assert:('+' isNameSpaceSelector) not. |
|
7095 |
self assert:(':+' isNameSpaceSelector) not. |
|
7096 |
self assert:(':Foo:+' isNameSpaceSelector) not. |
|
7097 |
||
7098 |
self assert:(':Foo::+' isNameSpaceSelector). |
|
7099 |
self assert:(':Foo::bar:baz:' isNameSpaceSelector). |
|
7100 |
" |
|
7101 |
||
7102 |
"Created: / 05-03-2007 / 11:35:31 / cg" |
|
24120 | 7103 |
"Modified: / 08-05-2019 / 14:41:30 / Stefan Vogel" |
23701 | 7104 |
! |
7105 |
||
7106 |
isNumeric |
|
7107 |
"return true, if the receiver is some numeric word; |
|
7108 |
i.e. consists only of digits." |
|
7109 |
||
7110 |
self size == 0 ifTrue:[ |
|
7111 |
^ false |
|
7112 |
]. |
|
7113 |
^ self conform:[:char | char isDigit] |
|
7114 |
||
7115 |
" |
|
7116 |
'helloWorld' isNumeric |
|
7117 |
'foo1234' isNumeric |
|
7118 |
'f1234' isNumeric |
|
7119 |
'1234' isNumeric |
|
7120 |
'+' isNumeric |
|
7121 |
" |
|
7122 |
||
7123 |
"Modified: / 13-10-2006 / 12:54:12 / cg" |
|
7124 |
! |
|
7125 |
||
11828 | 7126 |
isUppercaseFirst |
7127 |
"return true, if the first character is an uppercase character." |
|
7128 |
||
7129 |
^ self size ~~ 0 and:[ self first isUppercase ] |
|
7130 |
||
7131 |
" |
|
12177 | 7132 |
'helloWorld' isUppercaseFirst |
7133 |
'HelloWorld' isUppercaseFirst |
|
11828 | 7134 |
" |
7135 |
! |
|
7136 |
||
23701 | 7137 |
isValidSmalltalkIdentifier |
7138 |
"return true, if the receiver's characters make up a valid smalltalk identifier" |
|
7139 |
||
7140 |
|scanner tok| |
|
7141 |
||
7142 |
scanner := Compiler new. |
|
7143 |
scanner source:(self readStream). |
|
7144 |
ParseError handle:[:ex | |
|
7145 |
tok := nil. |
|
7146 |
] do:[ |
|
7147 |
tok := scanner nextToken. |
|
7148 |
]. |
|
7149 |
tok ~~ #Identifier ifTrue:[ |
|
7150 |
^ false |
|
7151 |
]. |
|
7152 |
scanner tokenPosition == 1 ifFalse:[^ false]. |
|
7153 |
^ scanner sourceStream atEnd. |
|
7154 |
||
7155 |
" |
|
7156 |
'foo' isValidSmalltalkIdentifier |
|
7157 |
'1foo' isValidSmalltalkIdentifier |
|
7158 |
'_foo' isValidSmalltalkIdentifier |
|
7159 |
'_foo_bar_' isValidSmalltalkIdentifier |
|
7160 |
'foo ' isValidSmalltalkIdentifier |
|
7161 |
' foo' isValidSmalltalkIdentifier |
|
7162 |
" |
|
7163 |
! |
|
7164 |
||
7165 |
isWhitespace |
|
7166 |
"return true, if the receiver is empty or contains only whitespace." |
|
7167 |
||
7168 |
^ (self indexOfNonSeparatorStartingAt:1) == 0 |
|
7169 |
||
7170 |
" |
|
7171 |
'' isWhitespace |
|
7172 |
' ' isWhitespace |
|
7173 |
' \ \' withCRs isWhitespace |
|
7174 |
' a\ \' withCRs isWhitespace |
|
7175 |
' \ \a' withCRs isWhitespace |
|
7176 |
'a \ \a' withCRs isWhitespace |
|
7177 |
" |
|
7178 |
||
7179 |
"Created: / 01-03-2017 / 15:24:53 / cg" |
|
7180 |
! |
|
7181 |
||
15773 | 7182 |
keywords |
7183 |
"assuming the receiver is a keyword message selector, |
|
7184 |
return the individual keywords (i.e. break it up at colons) |
|
7185 |
and return these as a collection. |
|
7186 |
For binary and unary selectors, the result may be nonsense (an array containing the receiver)." |
|
7187 |
||
7188 |
|coll s part| |
|
7189 |
||
7190 |
coll := OrderedCollection new. |
|
7191 |
s := ReadStream on:self. |
|
7192 |
[s atEnd] whileFalse:[ |
|
20874 | 7193 |
part := s through:$:. |
7194 |
coll add:part |
|
15773 | 7195 |
]. |
7196 |
^ coll asArray |
|
7197 |
||
7198 |
" |
|
7199 |
#at:put: keywords |
|
7200 |
#at: keywords |
|
7201 |
#+ keywords |
|
7202 |
#size keywords |
|
7203 |
" |
|
7204 |
||
7205 |
"Modified (Comment): / 30-06-2011 / 17:46:21 / cg" |
|
7206 |
! |
|
7207 |
||
9190 | 7208 |
knownAsSymbol |
7209 |
"for now, only single character strings are allowed as symbols. |
|
7210 |
This method is redefined in String." |
|
7211 |
||
7212 |
^ false |
|
7213 |
! |
|
7214 |
||
7215 |
leftIndent |
|
23663 | 7216 |
"if the receiver starts with whiteSpace, return the number of whiteSpace chars |
9190 | 7217 |
at the left - otherwise, return 0. |
23663 | 7218 |
If the receiver consists of whiteSpace only, return the receiver's size." |
9192 | 7219 |
|
7220 |
|index "{Class: SmallInteger }" |
|
7221 |
end "{Class: SmallInteger }"| |
|
7222 |
||
7223 |
index := 1. |
|
7224 |
end := self size. |
|
7225 |
[index <= end] whileTrue:[ |
|
20874 | 7226 |
(self at:index) isSeparator ifFalse:[^ index - 1]. |
7227 |
index := index + 1 |
|
8350 | 7228 |
]. |
9192 | 7229 |
^ end |
7230 |
||
7231 |
" |
|
7232 |
' hello' leftIndent |
|
7233 |
'foo ' leftIndent |
|
9190 | 7234 |
' ' leftIndent |
23663 | 7235 |
((Character tab),(Character tab),'foo') leftIndent |
7236 |
" |
|
7237 |
||
7238 |
"Modified: / 20-04-1996 / 19:28:43 / cg" |
|
7239 |
"Modified (comment): / 05-02-2019 / 11:22:57 / Claus Gittinger" |
|
8350 | 7240 |
! |
7241 |
||
10437 | 7242 |
nameSpaceSelectorParts |
7243 |
"Answer the namespace and baseSelector parts of a namespace selector. |
|
7244 |
Namespace selectors are those generated by sends from a method in a different |
|
7245 |
namespace; they are prefixed by ':'<ns>'::'. |
|
7246 |
You cannot easily change this algorithm here, as it is also known by the VM's lookup function. |
|
7247 |
Experimental" |
|
7248 |
||
7249 |
|nsPart selPart idx| |
|
7250 |
||
7251 |
(self at:1) == $: ifFalse:[^ Array with:'' with:self]. |
|
7252 |
idx := self indexOf:$: startingAt:3. |
|
7253 |
idx == 0 ifTrue:[^ Array with:'' with:self]. |
|
7254 |
||
7255 |
(idx+2 > self size) ifTrue:[^ Array with:'' with:self]. |
|
7256 |
(self at:idx+1) == $: ifFalse:[^ Array with:'' with:self]. |
|
7257 |
nsPart := self copyFrom:2 to:idx-1. |
|
7258 |
selPart := self copyFrom:idx+2. |
|
7259 |
^ Array with:nsPart with:selPart |
|
7260 |
||
7261 |
"test: |
|
10703
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
7262 |
self assert:('+' nameSpaceSelectorParts) = #('' '+'). |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
7263 |
self assert:(':+' nameSpaceSelectorParts) = #('' ':+'). |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
7264 |
self assert:(':Foo:+' nameSpaceSelectorParts) = #('' ':Foo:+'). |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
7265 |
|
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
7266 |
self assert:(':Foo::+' nameSpaceSelectorParts) = #('Foo' '+'). |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
7267 |
self assert:(':Foo::bar:baz:' nameSpaceSelectorParts) = #('Foo' 'bar:baz:'). |
10437 | 7268 |
" |
7269 |
||
7270 |
"Created: / 05-03-2007 / 17:16:58 / cg" |
|
7271 |
"Modified: / 06-03-2007 / 11:51:15 / cg" |
|
7272 |
! |
|
7273 |
||
23701 | 7274 |
numArgs |
7275 |
<resource: #obsolete> |
|
7276 |
"treating the receiver as a message selector, return how many arguments would it take. |
|
7277 |
Please use argumentCount for ANSI compatibility." |
|
7278 |
||
7279 |
^ self argumentCount |
|
7280 |
! |
|
7281 |
||
7282 |
partsIfSelector |
|
7283 |
"treat the receiver as a message selector, return a collection of parts. |
|
7284 |
Notice: this is more tolerant than Smalltalk's syntax would suggest; |
|
7285 |
especially it allows for empty keyword parts between colons. |
|
7286 |
This is not (and should not be checked here), to allow parsing of |
|
7287 |
degenerate selectors as appearing with objectiveC." |
|
7288 |
||
7289 |
|idx1 "{ Class: SmallInteger }" |
|
7290 |
coll idx2 sz| |
|
7291 |
||
7292 |
coll := OrderedCollection new. |
|
7293 |
idx1 := 1. |
|
7294 |
sz := self size. |
|
7295 |
[ |
|
7296 |
idx2 := self indexOf:$: startingAt:idx1. |
|
7297 |
(idx2 == 0 or:[idx2 == sz]) ifTrue:[ |
|
7298 |
coll add:(self copyFrom:idx1). |
|
7299 |
^ coll |
|
7300 |
]. |
|
7301 |
coll add:(self copyFrom:idx1 to:idx2). |
|
7302 |
idx1 := idx2 + 1 |
|
7303 |
] loop. |
|
7304 |
||
7305 |
" |
|
7306 |
'foo:' partsIfSelector |
|
7307 |
'foo:bar:' partsIfSelector |
|
7308 |
'foo::::' partsIfSelector |
|
7309 |
#foo:bar: partsIfSelector |
|
7310 |
'hello' partsIfSelector |
|
7311 |
'+' partsIfSelector |
|
7312 |
" |
|
7313 |
! |
|
7314 |
||
21283 | 7315 |
speciesForSubcollection |
7316 |
"answer the class, when splitting instances into subcollections" |
|
7317 |
||
7318 |
^ StringCollection |
|
7319 |
||
7320 |
"Created: / 24-01-2017 / 18:54:18 / stefan" |
|
7321 |
! |
|
7322 |
||
9190 | 7323 |
stringSpecies |
8874 | 7324 |
"return the underlying strings bitsPerCharacter |
8037
8cce25b1059c
care for wide chars in asXXXFirst
Claus Gittinger <cg@exept.de>
parents:
8032
diff
changeset
|
7325 |
(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
|
7326 |
|
8cce25b1059c
care for wide chars in asXXXFirst
Claus Gittinger <cg@exept.de>
parents:
8032
diff
changeset
|
7327 |
|string| |
8cce25b1059c
care for wide chars in asXXXFirst
Claus Gittinger <cg@exept.de>
parents:
8032
diff
changeset
|
7328 |
|
15523 | 7329 |
string := self string. |
7330 |
string == self ifTrue:[^ self species]. |
|
8037
8cce25b1059c
care for wide chars in asXXXFirst
Claus Gittinger <cg@exept.de>
parents:
8032
diff
changeset
|
7331 |
^ string stringSpecies |
8cce25b1059c
care for wide chars in asXXXFirst
Claus Gittinger <cg@exept.de>
parents:
8032
diff
changeset
|
7332 |
|
8cce25b1059c
care for wide chars in asXXXFirst
Claus Gittinger <cg@exept.de>
parents:
8032
diff
changeset
|
7333 |
" |
8874 | 7334 |
'hello' stringSpecies |
9192 | 7335 |
'hello' asText allBold stringSpecies |
7336 |
" |
|
7337 |
! |
|
8037
8cce25b1059c
care for wide chars in asXXXFirst
Claus Gittinger <cg@exept.de>
parents:
8032
diff
changeset
|
7338 |
|
6653 | 7339 |
widthFrom:startIndex to:endIndex on:aGC |
19464 | 7340 |
"return ths size of part of the receiver in device units if displayed on aGC" |
6653 | 7341 |
|
20221 | 7342 |
^ aGC deviceFont widthOf:self from:startIndex to:endIndex |
6653 | 7343 |
|
7344 |
" |
|
8874 | 7345 |
'hello world' widthFrom:1 to:5 on:(View new) |
7346 |
'hello' widthOn:(View new) |
|
6653 | 7347 |
" |
9192 | 7348 |
! |
6653 | 7349 |
|
1388 | 7350 |
widthOn:aGC |
19464 | 7351 |
"return ths size of the receiver in device units if displayed on aGC" |
1388 | 7352 |
|
20221 | 7353 |
^ aGC deviceFont widthOf:self "font is already a device font" |
1388 | 7354 |
|
7355 |
" |
|
8874 | 7356 |
'hello world' widthOn:(View new) |
1388 | 7357 |
" |
7358 |
||
7359 |
"Created: 12.5.1996 / 20:09:29 / cg" |
|
2554 | 7360 |
"Modified: 17.4.1997 / 12:50:23 / cg" |
608 | 7361 |
! ! |
7362 |
||
24250 | 7363 |
|
608 | 7364 |
!CharacterArray methodsFor:'special string converting'! |
7365 |
||
16759 | 7366 |
asUnixFilenameString |
7367 |
"return a new string consisting of receiver's characters |
|
7368 |
with all \-characters replaced by /-characters. |
|
7369 |
If there are no backslashes, return the original" |
|
7370 |
||
7371 |
^ self copyReplaceAll:$\ with:$/ ifNone:self |
|
7372 |
||
7373 |
" |
|
7374 |
'hello\world' asUnixFilenameString |
|
7375 |
" |
|
7376 |
||
7377 |
"Modified: / 18.7.1998 / 22:53:02 / cg" |
|
7378 |
! |
|
7379 |
||
24411 | 7380 |
expandNumericPlaceholdersWith:argArrayOrDictionary |
7381 |
"return a copy of the receiver, where all %i escapes with numeric keys are |
|
7382 |
replaced by corresponding arguments' printStrings from the argArrayOrDictionary. |
|
7383 |
I.e. 'hello %1; how is %2' expandPlaceholdersWith:#('world' 'this') results |
|
7384 |
in the new string 'hello world; how is this'. |
|
7385 |
||
7386 |
This will ignore all non-numeric keys (and leave them as is)." |
|
7387 |
||
7388 |
|stream| |
|
7389 |
||
7390 |
stream := (TextStream ? CharacterWriteStream) on:(self species uninitializedNew:self size + 20). |
|
7391 |
"/ self expandPlaceholdersWith:argArrayOrDictionary on:stream. |
|
7392 |
self |
|
7393 |
expandPlaceholders:$% with:argArrayOrDictionary |
|
7394 |
ignoreNumericEscapes:false |
|
7395 |
ignoreNonNumericEscapes:true |
|
7396 |
ignoreSpecialEscapes:true |
|
7397 |
requireParentheses:true |
|
7398 |
on:stream. |
|
7399 |
^ stream contents. |
|
7400 |
||
7401 |
" |
|
7402 |
'hello %1' expandNumericPlaceholdersWith:#('world') |
|
7403 |
'hello %1 %abc' expandNumericPlaceholdersWith:#('world') |
|
7404 |
'hello %1 %(abc)' expandNumericPlaceholdersWith:#('world') |
|
7405 |
" |
|
7406 |
||
7407 |
" |
|
7408 |
'hello %1' expandPlaceholdersWith:#('world') |
|
7409 |
'hello %1 %abc' expandPlaceholdersWith:#('world') |
|
7410 |
'hello %1 %(abc)' expandPlaceholdersWith:#('world') |
|
7411 |
" |
|
7412 |
||
7413 |
"Modified: / 01-07-1997 / 00:53:24 / cg" |
|
7414 |
"Modified: / 14-07-2018 / 09:23:31 / Claus Gittinger" |
|
7415 |
! |
|
7416 |
||
14768 | 7417 |
expandPlaceholders:escapeCharacter with:argArrayOrDictionary |
7418 |
"this is the generic version of the old %-escaping method, allowing for an arbitrary |
|
7419 |
escape character to be used (typically $$ or $% are effectively used). |
|
24407 | 7420 |
|
14768 | 7421 |
Return a copy of the receiver, where all %i escapes are |
7422 |
replaced by corresponding arguments' printStrings from the argArrayOrDictionary. |
|
7423 |
I.e. 'hello %1; how is %2' expandPlaceholdersWith:#('world' 'this') results |
|
7424 |
in the new string 'hello world; how is this'. |
|
7425 |
As an extension, the argument may also be a dictionary, providing |
|
7426 |
values for symbolic keys. |
|
7427 |
In this case, %a .. %z and %(...) are also allowed. |
|
7428 |
(%1..%9 require a numeric key in the dictionary, however) |
|
7429 |
To get a '%' character, use a '%%'-escape. |
|
7430 |
To get an integer-indexed placeHolder followed by another digit, |
|
7431 |
or an index > 9, you must use %(digit). |
|
7432 |
See also bindWith:... for VisualAge compatibility." |
|
7433 |
||
7434 |
|stream| |
|
7435 |
||
18269 | 7436 |
stream := (TextStream ? CharacterWriteStream) on:(self species uninitializedNew:self size + 20). |
24410 | 7437 |
self expandPlaceholders:escapeCharacter with:argArrayOrDictionary |
7438 |
ignoreNumericEscapes:false |
|
7439 |
ignoreNonNumericEscapes:false |
|
7440 |
ignoreSpecialEscapes:false |
|
7441 |
requireParentheses:true |
|
7442 |
on:stream. |
|
14768 | 7443 |
^ stream contents. |
7444 |
||
7445 |
" |
|
7446 |
'hello %1' expandPlaceholdersWith:#('world') |
|
7447 |
'hello %1; how is %2' expandPlaceholdersWith:#('world' 'this') |
|
7448 |
'hello %2; how is %1' expandPlaceholdersWith:#('world' 'this') |
|
7449 |
'%1 plus %2 gives %3 ' expandPlaceholdersWith:#(4 5 9) |
|
7450 |
'%%(1)0 gives %(1)0' expandPlaceholdersWith:#(123) |
|
7451 |
'%%10 gives %10' expandPlaceholdersWith:#(123) |
|
7452 |
'%%(10) gives %(10)' expandPlaceholdersWith:#(123) |
|
7453 |
'%test gives %1' expandPlaceholdersWith:#(123) |
|
7454 |
'bla %1 bla' expandPlaceholdersWith:{ 'hello' allBold } |
|
7455 |
'bla %1 bla' expandPlaceholdersWith:{ 'hello' } |
|
7456 |
" |
|
7457 |
||
7458 |
" |
|
7459 |
|dict| |
|
7460 |
||
7461 |
dict := Dictionary new. |
|
7462 |
dict at:1 put:'one'. |
|
7463 |
dict at:$a put:'AAAAA'. |
|
7464 |
dict at:$b put:[ Time now ]. |
|
7465 |
dict at:'foo' put:[ Date today ]. |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
7466 |
'hello $1 %a $b %(foo) $foo ' expandPlaceholders:$$ with:dict. |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
7467 |
'hello $1 %a $b %(foo) $foo ' expandPlaceholders:$% with:dict. |
14768 | 7468 |
" |
7469 |
||
23067 | 7470 |
"Modified: / 01-07-1997 / 00:53:24 / cg" |
7471 |
"Modified: / 14-06-2018 / 11:46:18 / Claus Gittinger" |
|
7472 |
! |
|
7473 |
||
24410 | 7474 |
expandPlaceholders:escapeCharacter with:argArrayOrDictionary |
7475 |
ignoreNumericEscapes:ignoreNumericEscapes |
|
7476 |
ignoreNonNumericEscapes:ignoreNonNumericEscapes |
|
7477 |
ignoreSpecialEscapes:ignoreSpecialEscapes |
|
7478 |
requireParentheses:requireParentheses |
|
24407 | 7479 |
"this is the generic version of the old %-escaping method, allowing for an arbitrary |
7480 |
escape character to be used (typically $$ or $% are effectively used). |
|
7481 |
||
7482 |
Return a copy of the receiver, where all %i escapes are |
|
7483 |
replaced by corresponding arguments' printStrings from the argArray. |
|
7484 |
I.e. 'hello %1; how is %2' expandPlaceholdersWith:#('world' 'this') results |
|
7485 |
in the new string 'hello world; how is this'. |
|
7486 |
||
7487 |
As an extension, the argument may also be a dictionary, providing values for symbolic keys. |
|
7488 |
In this case, %a .. %z and %(...) are also allowed. |
|
7489 |
(%1..%9 require a numeric key in the dictionary, however) |
|
7490 |
||
7491 |
Also, values in argArrayOrDictionary may be blocks. |
|
7492 |
||
7493 |
To get a '%' character, use a '%%'-escape. |
|
7494 |
To get an integer-indexed placeHolder followed by another digit, |
|
7495 |
or an index > 9, you must use %(digit). |
|
7496 |
||
7497 |
See also bindWith:... for VisualAge compatibility. |
|
7498 |
Use %<cr> to insert a CR and %<tab> to insert a TAB. |
|
7499 |
||
7500 |
ignoreNumericEscapes controls if %<nr> escapes are expanded or not. |
|
7501 |
This is required for Windows batch-script expansion, where %<nr> should be left unchanged. |
|
7502 |
||
7503 |
requireParentheses controls if $abc is allowed or not. |
|
7504 |
If true, multi-character replacements need to be parenthized as $(abc); |
|
7505 |
if false, you can also write $abc. |
|
7506 |
" |
|
7507 |
||
7508 |
|stream| |
|
7509 |
||
7510 |
stream := (TextStream ? CharacterWriteStream) on:(self species uninitializedNew:self size + 20). |
|
24410 | 7511 |
self |
7512 |
expandPlaceholders:escapeCharacter with:argArrayOrDictionary |
|
7513 |
ignoreNumericEscapes:ignoreNumericEscapes |
|
7514 |
ignoreNonNumericEscapes:ignoreNonNumericEscapes |
|
7515 |
ignoreSpecialEscapes:ignoreSpecialEscapes |
|
7516 |
requireParentheses:requireParentheses |
|
7517 |
on:stream. |
|
24407 | 7518 |
^ stream contents |
7519 |
||
7520 |
" |
|
7521 |
'hello %1' expandPlaceholders:$% with:#('world') on:Transcript. |
|
7522 |
'hello %a%<cr>' expandPlaceholders:$% |
|
7523 |
with:(Dictionary new at:'a' put:'world';yourself) |
|
7524 |
on:Transcript. |
|
7525 |
'hello %aa%<cr>' expandPlaceholders:$% with:(Dictionary new at:'a' put:'world';yourself) on:Transcript. |
|
7526 |
'hello %(aa)%<cr>' expandPlaceholders:$% with:(Dictionary new at:'aa' put:'world';yourself) on:Transcript. |
|
7527 |
'hello %aa%<cr>' expandPlaceholders:$% with:(Dictionary new at:'aa' put:'world';yourself) ignoreNumericEscapes:true requireParentheses:false on:Transcript. |
|
7528 |
||
7529 |
String streamContents:[:s| |
|
7530 |
'hello %1' expandPlaceholders:$% with:#('world') on:s. |
|
7531 |
s cr. |
|
7532 |
'hello $1; how is $2' expandPlaceholders:$$ with:#('world' 'this') on:s. |
|
7533 |
s cr. |
|
7534 |
'hello %2; how is %1' expandPlaceholders:$% with:#('world' 'this') on:s. |
|
7535 |
s cr. |
|
7536 |
'%1 plus %2 gives %3 ' expandPlaceholders:$% with:#(4 5 9) on:s. |
|
7537 |
s cr. |
|
7538 |
'%%(1)0 gives %(1)0' expandPlaceholders:$% with:#(123) on:s. |
|
7539 |
s cr. |
|
7540 |
'%%10 gives %10' expandPlaceholders:$% with:#(123) on:s. |
|
7541 |
s cr. |
|
7542 |
'%%(10) gives %(10) %<cr>%<tab>next line' expandPlaceholders:$% with:#(123) on:s. |
|
7543 |
s cr. |
|
7544 |
'%%test gives %test' expandPlaceholders:$% with:#(123) on:s. |
|
7545 |
s cr. |
|
7546 |
'|%%<tab>|%%1|%%<cr>| gives |%<tab>|%1|%<cr>|' expandPlaceholders:$% with:#(foo) on:s. |
|
7547 |
] |
|
7548 |
" |
|
7549 |
||
7550 |
"without xlation dictionary: |
|
7551 |
'hello %1' expandPlaceholdersWith:nil. |
|
7552 |
'hello%<cr> %1' expandPlaceholdersWith:nil. |
|
7553 |
" |
|
7554 |
||
7555 |
" |
|
7556 |
|dict| |
|
7557 |
||
7558 |
dict := Dictionary new. |
|
7559 |
dict at:1 put:'one'. |
|
7560 |
dict at:$a put:'AAAAA'. |
|
7561 |
dict at:$b put:[ Time now ]. |
|
7562 |
String streamContents:[:s| |
|
7563 |
'hello $1 $a $b' expandPlaceholders:$$ with:dict on:s. |
|
7564 |
]. |
|
7565 |
" |
|
7566 |
||
7567 |
"using blocks: |
|
7568 |
|dict| |
|
7569 |
||
7570 |
dict := Dictionary new. |
|
7571 |
dict at:'time' put:[Time now printString]. |
|
7572 |
dict at:'date' put:[Date today printString]. |
|
7573 |
String streamContents:[:s| |
|
7574 |
'it is $(time) $(date)' expandPlaceholders:$$ with:dict on:s. |
|
7575 |
]. |
|
7576 |
" |
|
7577 |
||
7578 |
"Created: / 14-01-2019 / 17:43:03 / Claus Gittinger" |
|
7579 |
"Modified: / 05-06-2019 / 17:05:47 / Claus Gittinger" |
|
7580 |
! |
|
7581 |
||
24410 | 7582 |
expandPlaceholders:escapeCharacter with:argArrayOrDictionary |
7583 |
ignoreNumericEscapes:ignoreNumericEscapes |
|
7584 |
ignoreNonNumericEscapes:ignoreNonNumericEscapes |
|
7585 |
ignoreSpecialEscapes:ignoreSpecialEscapes |
|
7586 |
requireParentheses:requireParentheses |
|
7587 |
on:aStream |
|
7588 |
||
7589 |
"this is the central method for %-escaping, allowing for an arbitrary |
|
7590 |
escape character to be used (typically $$ or $% are effectively used). |
|
7591 |
||
7592 |
Write the receiver to aStream, where all %i escapes are |
|
7593 |
replaced by corresponding arguments' printStrings from the argArray. |
|
7594 |
I.e. 'hello %1; how is %2' expandPlaceholdersWith:#('world' 'this') results |
|
7595 |
in the new string 'hello world; how is this'. |
|
7596 |
||
7597 |
As an extension, the argument may also be a dictionary, providing values for symbolic keys. |
|
7598 |
In this case, %a .. %z and %(...) are also allowed. |
|
7599 |
(%1..%9 require a numeric key in the dictionary, however) |
|
7600 |
||
7601 |
Also, values in argArrayOrDictionary may be blocks. |
|
7602 |
||
7603 |
To get a '%' character, use a '%%'-escape. |
|
7604 |
To get an integer-indexed placeHolder followed by another digit, |
|
7605 |
or an index > 9, you must use %(digit). |
|
7606 |
||
7607 |
See also bindWith:... for VisualAge compatibility. |
|
7608 |
Use %<cr> to insert a CR and %<tab> to insert a TAB. |
|
7609 |
||
7610 |
ignoreNumericEscapes controls if %<nr> escapes are expanded or not. |
|
7611 |
This is required for Windows batch-script expansion, where %<nr> should be left unchanged. |
|
7612 |
||
7613 |
ignoreSpecialEscapes controls if control characters like %<cr> are expanded or not. |
|
7614 |
||
7615 |
requireParentheses controls if $abc is allowed or not. |
|
7616 |
If true, multi-character replacements need to be parenthized as $(abc) and the above is |
|
7617 |
interpreted as $(a)bc; |
|
7618 |
if false, you can also write $abc. |
|
7619 |
" |
|
7620 |
||
7621 |
|next v key numericKey |
|
7622 |
idx "{ SmallInteger }" |
|
7623 |
idx2 "{ SmallInteger }" |
|
7624 |
start "{ SmallInteger }" |
|
7625 |
stop "{ SmallInteger }"| |
|
7626 |
||
7627 |
stop := self size. |
|
7628 |
start := 1. |
|
7629 |
[start <= stop] whileTrue:[ |
|
7630 |
idx := self indexOf:escapeCharacter startingAt:start. |
|
7631 |
(idx == 0 or:[idx == stop]) ifTrue:[ |
|
7632 |
aStream nextPutAll:self startingAt:start to:stop. |
|
7633 |
^ self. |
|
7634 |
]. |
|
7635 |
"found an escapeCharacter at idx" |
|
7636 |
idx > start ifTrue:[ |
|
7637 |
aStream nextPutAll:self startingAt:start to:(idx - 1). |
|
7638 |
]. |
|
7639 |
next := self at:(idx + 1). |
|
7640 |
(next == escapeCharacter) ifTrue:[ |
|
7641 |
"doubled escapeCharacter" |
|
7642 |
aStream nextPut:escapeCharacter. |
|
7643 |
] ifFalse:[ |
|
7644 |
"%<...> " |
|
7645 |
next == $< ifTrue:[ |
|
7646 |
ignoreSpecialEscapes ifTrue:[ |
|
7647 |
aStream nextPut:escapeCharacter. |
|
7648 |
aStream nextPut:next. |
|
7649 |
] ifFalse:[ |
|
7650 |
idx2 := self indexOf:$> startingAt:idx+2. |
|
7651 |
key := self copyFrom:idx+2 to:idx2-1. |
|
7652 |
idx := idx2 - 1. |
|
7653 |
key := key asSymbolIfInterned. |
|
7654 |
(#(cr tab nl return lf ff null) includesIdentical:key) ifTrue:[ |
|
7655 |
aStream nextPut:(Character perform:key). |
|
7656 |
]. |
|
7657 |
]. |
|
7658 |
] ifFalse:[ |
|
7659 |
argArrayOrDictionary isNil ifTrue:[ |
|
7660 |
aStream nextPut:escapeCharacter. |
|
7661 |
aStream nextPut:next. |
|
7662 |
] ifFalse:[ |
|
7663 |
(next isDigit and:[ignoreNumericEscapes not]) ifTrue:[ |
|
7664 |
v := argArrayOrDictionary at:(next digitValue) ifAbsent:'' |
|
7665 |
] ifFalse:[ |
|
7666 |
next == $( ifTrue:[ |
|
7667 |
idx2 := self indexOf:$) startingAt:idx+2. |
|
7668 |
self assert:(idx2 > 0) message:'closing parenthesis missing'. |
|
7669 |
key := self copyFrom:idx+2 to:idx2-1. |
|
7670 |
idx := idx2 - 1. |
|
7671 |
||
7672 |
(key isNumeric |
|
7673 |
and:[(numericKey := Integer readFrom:key onError:nil) notNil]) |
|
7674 |
ifTrue:[ |
|
7675 |
ignoreNumericEscapes ifTrue:[ |
|
7676 |
v := escapeCharacter,'(',key,')' |
|
7677 |
] ifFalse:[ |
|
7678 |
v := argArrayOrDictionary at:numericKey ifAbsent:'' |
|
7679 |
] |
|
7680 |
] ifFalse:[ |
|
7681 |
ignoreNonNumericEscapes ifTrue:[ |
|
7682 |
v := escapeCharacter,'(',key,')' |
|
7683 |
] ifFalse:[ |
|
7684 |
argArrayOrDictionary isBlock ifTrue:[ |
|
7685 |
v := argArrayOrDictionary value:key |
|
7686 |
] ifFalse:[ |
|
7687 |
(argArrayOrDictionary includesKey:key) ifTrue:[ |
|
7688 |
v := argArrayOrDictionary at:key |
|
7689 |
] ifFalse:[ |
|
7690 |
key := key asSymbolIfInternedOrSelf. |
|
7691 |
(argArrayOrDictionary includesKey:key) ifTrue:[ |
|
7692 |
v := argArrayOrDictionary at:key |
|
7693 |
] ifFalse:[ |
|
7694 |
(key size == 1 and:[ argArrayOrDictionary includesKey:(key at:1)]) ifTrue:[ |
|
7695 |
v := argArrayOrDictionary at:(key at:1) |
|
7696 |
] ifFalse:[ |
|
7697 |
v := '' |
|
7698 |
] |
|
7699 |
]. |
|
7700 |
]. |
|
7701 |
]. |
|
7702 |
]. |
|
7703 |
]. |
|
7704 |
] ifFalse:[ |
|
7705 |
(ignoreNonNumericEscapes not |
|
7706 |
and:[ next isLetter |
|
7707 |
and:[ argArrayOrDictionary isSequenceable not "is a Dictionary"]] |
|
7708 |
) ifTrue:[ |
|
7709 |
"so next is a non-numeric single character." |
|
7710 |
requireParentheses ifTrue:[ |
|
7711 |
key := next. |
|
7712 |
] ifFalse:[ |
|
7713 |
idx2 := self findFirst:[:ch | (ch isLetterOrDigitOrUnderline) not] startingAt:idx+2. |
|
7714 |
idx2 == 0 ifTrue:[idx2 := self size + 1]. |
|
7715 |
key := self copyFrom:idx+1 to:idx2-1. |
|
7716 |
idx := idx2 - 2. |
|
7717 |
]. |
|
7718 |
||
7719 |
argArrayOrDictionary isBlock ifTrue:[ |
|
7720 |
v := argArrayOrDictionary value:key |
|
7721 |
] ifFalse:[ |
|
7722 |
v := argArrayOrDictionary |
|
7723 |
at:key |
|
7724 |
ifAbsent:[ |
|
7725 |
"try symbol or string instead of character" |
|
7726 |
argArrayOrDictionary |
|
7727 |
at:key asString asSymbolIfInternedOrSelf |
|
7728 |
ifAbsent:[escapeCharacter asString , key]. |
|
7729 |
]. |
|
7730 |
]. |
|
7731 |
] ifFalse:[ |
|
7732 |
v := String with:escapeCharacter with:next. |
|
7733 |
]. |
|
7734 |
] |
|
7735 |
]. |
|
7736 |
||
7737 |
v isBlock ifTrue:[ |
|
7738 |
v := v value |
|
7739 |
]. |
|
7740 |
v printOn:aStream. |
|
7741 |
] |
|
7742 |
]. |
|
7743 |
]. |
|
7744 |
start := idx + 2 |
|
7745 |
]. |
|
7746 |
||
7747 |
" |
|
7748 |
'hello %1' expandPlaceholders:$% with:#('world') on:Transcript. |
|
7749 |
'hello %a%<cr>' expandPlaceholders:$% |
|
7750 |
with:(Dictionary new at:'a' put:'world';yourself) |
|
7751 |
on:Transcript. |
|
7752 |
'hello %aa%<cr>' expandPlaceholders:$% with:(Dictionary new at:'a' put:'world';yourself) on:Transcript. |
|
7753 |
'hello %(aa)%<cr>' expandPlaceholders:$% with:(Dictionary new at:'aa' put:'world';yourself) on:Transcript. |
|
7754 |
'hello %aa%<cr>' expandPlaceholders:$% with:(Dictionary new at:'aa' put:'world';yourself) ignoreNumericEscapes:true requireParentheses:false on:Transcript. |
|
7755 |
||
7756 |
String streamContents:[:s| |
|
7757 |
'hello %1' expandPlaceholders:$% with:#('world') on:s. |
|
7758 |
s cr. |
|
7759 |
'hello $1; how is $2' expandPlaceholders:$$ with:#('world' 'this') on:s. |
|
7760 |
s cr. |
|
7761 |
'hello %2; how is %1' expandPlaceholders:$% with:#('world' 'this') on:s. |
|
7762 |
s cr. |
|
7763 |
'%1 plus %2 gives %3 ' expandPlaceholders:$% with:#(4 5 9) on:s. |
|
7764 |
s cr. |
|
7765 |
'%%(1)0 gives %(1)0' expandPlaceholders:$% with:#(123) on:s. |
|
7766 |
s cr. |
|
7767 |
'%%10 gives %10' expandPlaceholders:$% with:#(123) on:s. |
|
7768 |
s cr. |
|
7769 |
'%%(10) gives %(10) %<cr>%<tab>next line' expandPlaceholders:$% with:#(123) on:s. |
|
7770 |
s cr. |
|
7771 |
'%%test gives %test' expandPlaceholders:$% with:#(123) on:s. |
|
7772 |
s cr. |
|
7773 |
'|%%<tab>|%%1|%%<cr>| gives |%<tab>|%1|%<cr>|' expandPlaceholders:$% with:#(foo) on:s. |
|
7774 |
] |
|
7775 |
" |
|
7776 |
||
7777 |
"without xlation dictionary: |
|
7778 |
'hello %1' expandPlaceholdersWith:nil. |
|
7779 |
'hello%<cr> %1' expandPlaceholdersWith:nil. |
|
7780 |
" |
|
7781 |
||
7782 |
" |
|
7783 |
|dict| |
|
7784 |
||
7785 |
dict := Dictionary new. |
|
7786 |
dict at:1 put:'one'. |
|
7787 |
dict at:$a put:'AAAAA'. |
|
7788 |
dict at:$b put:[ Time now ]. |
|
7789 |
String streamContents:[:s| |
|
7790 |
'hello $1 $a $b' expandPlaceholders:$$ with:dict on:s. |
|
7791 |
]. |
|
7792 |
" |
|
7793 |
||
7794 |
"using blocks: |
|
7795 |
|dict| |
|
7796 |
||
7797 |
dict := Dictionary new. |
|
7798 |
dict at:'time' put:[Time now printString]. |
|
7799 |
dict at:'date' put:[Date today printString]. |
|
7800 |
String streamContents:[:s| |
|
7801 |
'it is $(time) $(date)' expandPlaceholders:$$ with:dict on:s. |
|
7802 |
]. |
|
7803 |
" |
|
7804 |
||
7805 |
"Created: / 14-01-2019 / 17:43:03 / Claus Gittinger" |
|
7806 |
"Modified: / 05-06-2019 / 17:05:47 / Claus Gittinger" |
|
7807 |
! |
|
7808 |
||
7809 |
expandPlaceholders:escapeCharacter with:argArrayOrDictionary |
|
7810 |
ignoreNumericEscapes:ignoreNumericEscapes |
|
7811 |
on:aStream |
|
7812 |
"this is the generic version of the old %-escaping method, allowing for an arbitrary |
|
7813 |
escape character to be used (typically $$ or $% are effectively used). |
|
7814 |
||
7815 |
Write the receiver to aStream, where all %i escapes are |
|
7816 |
replaced by corresponding arguments' printStrings from the argArray. |
|
7817 |
I.e. 'hello %1; how is %2' expandPlaceholdersWith:#('world' 'this') results |
|
7818 |
in the new string 'hello world; how is this'. |
|
7819 |
||
7820 |
As an extension, the argument may also be a dictionary, providing values for symbolic keys. |
|
7821 |
In this case, %a .. %z and %(...) are also allowed. |
|
7822 |
(%1..%9 require a numeric key in the dictionary, however) |
|
7823 |
||
7824 |
Also, values in argArrayOrDictionary may be blocks. |
|
7825 |
||
7826 |
To get a '%' character, use a '%%'-escape. |
|
7827 |
To get an integer-indexed placeHolder followed by another digit, |
|
7828 |
or an index > 9, you must use %(digit). |
|
7829 |
||
7830 |
See also bindWith:... for VisualAge compatibility. |
|
7831 |
Use %<cr> to insert a CR and %<tab> to insert a TAB. |
|
7832 |
||
7833 |
ignoreNumericEscapes controls if %<nr> escapes are expanded or not. |
|
7834 |
This is required for Windows batch-script expansion, where %<nr> should be left |
|
7835 |
unchanged." |
|
7836 |
||
7837 |
^ self |
|
7838 |
expandPlaceholders:escapeCharacter |
|
7839 |
with:argArrayOrDictionary |
|
7840 |
ignoreNumericEscapes:ignoreNumericEscapes |
|
7841 |
ignoreNonNumericEscapes:false |
|
7842 |
ignoreSpecialEscapes:false |
|
7843 |
requireParentheses:true |
|
7844 |
on:aStream |
|
7845 |
||
7846 |
" |
|
7847 |
String streamContents:[:s| |
|
7848 |
'hello %1' expandPlaceholders:$% with:#('world') on:s. |
|
7849 |
s cr. |
|
7850 |
'hello $1; how is $2' expandPlaceholders:$$ with:#('world' 'this') on:s. |
|
7851 |
s cr. |
|
7852 |
'hello %2; how is %1' expandPlaceholders:$% with:#('world' 'this') on:s. |
|
7853 |
s cr. |
|
7854 |
'%1 plus %2 gives %3 ' expandPlaceholders:$% with:#(4 5 9) on:s. |
|
7855 |
s cr. |
|
7856 |
'%%(1)0 gives %(1)0' expandPlaceholders:$% with:#(123) on:s. |
|
7857 |
s cr. |
|
7858 |
'%%10 gives %10' expandPlaceholders:$% with:#(123) on:s. |
|
7859 |
s cr. |
|
7860 |
'%%(10) gives %(10) %<cr>%<tab>next line' expandPlaceholders:$% with:#(123) on:s. |
|
7861 |
s cr. |
|
7862 |
'%%test gives %test' expandPlaceholders:$% with:#(123) on:s. |
|
7863 |
s cr. |
|
7864 |
'|%%<tab>|%%1|%%<cr>| gives |%<tab>|%1|%<cr>|' expandPlaceholders:$% with:#(foo) on:s. |
|
7865 |
] |
|
7866 |
" |
|
7867 |
||
7868 |
"without xlation dictionary: |
|
7869 |
'hello %1' expandPlaceholdersWith:nil. |
|
7870 |
'hello%<cr> %1' expandPlaceholdersWith:nil. |
|
7871 |
" |
|
7872 |
||
7873 |
" |
|
7874 |
|dict| |
|
7875 |
||
7876 |
dict := Dictionary new. |
|
7877 |
dict at:1 put:'one'. |
|
7878 |
dict at:$a put:'AAAAA'. |
|
7879 |
dict at:$b put:[ Time now ]. |
|
7880 |
String streamContents:[:s| |
|
7881 |
'hello $1 $a $b' expandPlaceholders:$$ with:dict on:s. |
|
7882 |
]. |
|
7883 |
" |
|
7884 |
||
7885 |
"using blocks: |
|
7886 |
|dict| |
|
7887 |
||
7888 |
dict := Dictionary new. |
|
7889 |
dict at:'time' put:[Time now printString]. |
|
7890 |
dict at:'date' put:[Date today printString]. |
|
7891 |
String streamContents:[:s| |
|
7892 |
'it is $(time) $(date)' expandPlaceholders:$$ with:dict on:s. |
|
7893 |
]. |
|
7894 |
" |
|
7895 |
||
7896 |
"Created: / 14-06-2018 / 11:44:08 / Claus Gittinger" |
|
7897 |
"Modified (comment): / 14-01-2019 / 17:44:02 / Claus Gittinger" |
|
7898 |
! |
|
7899 |
||
7900 |
expandPlaceholders:escapeCharacter with:argArrayOrDictionary |
|
7901 |
ignoreNumericEscapes:ignoreNumericEscapes |
|
7902 |
requireParentheses:requireParentheses |
|
7903 |
"this is the generic version of the old %-escaping method, allowing for an arbitrary |
|
7904 |
escape character to be used (typically $$ or $% are effectively used). |
|
7905 |
||
7906 |
Return a copy of the receiver, where all %i escapes are |
|
7907 |
replaced by corresponding arguments' printStrings from the argArray. |
|
7908 |
I.e. 'hello %1; how is %2' expandPlaceholdersWith:#('world' 'this') results |
|
7909 |
in the new string 'hello world; how is this'. |
|
7910 |
||
7911 |
As an extension, the argument may also be a dictionary, providing values for symbolic keys. |
|
7912 |
In this case, %a .. %z and %(...) are also allowed. |
|
7913 |
(%1..%9 require a numeric key in the dictionary, however) |
|
7914 |
||
7915 |
Also, values in argArrayOrDictionary may be blocks. |
|
7916 |
||
7917 |
To get a '%' character, use a '%%'-escape. |
|
7918 |
To get an integer-indexed placeHolder followed by another digit, |
|
7919 |
or an index > 9, you must use %(digit). |
|
7920 |
||
7921 |
See also bindWith:... for VisualAge compatibility. |
|
7922 |
Use %<cr> to insert a CR and %<tab> to insert a TAB. |
|
7923 |
||
7924 |
ignoreNumericEscapes controls if %<nr> escapes are expanded or not. |
|
7925 |
This is required for Windows batch-script expansion, where %<nr> should be left unchanged. |
|
7926 |
||
7927 |
requireParentheses controls if $abc is allowed or not. |
|
7928 |
If true, multi-character replacements need to be parenthized as $(abc); |
|
7929 |
if false, you can also write $abc. |
|
7930 |
" |
|
7931 |
||
7932 |
|stream| |
|
7933 |
||
7934 |
stream := (TextStream ? CharacterWriteStream) on:(self species uninitializedNew:self size + 20). |
|
7935 |
self expandPlaceholders:escapeCharacter with:argArrayOrDictionary |
|
7936 |
ignoreNumericEscapes:ignoreNumericEscapes |
|
7937 |
ignoreNonNumericEscapes:false |
|
7938 |
ignoreSpecialEscapes:false |
|
7939 |
requireParentheses:requireParentheses on:stream. |
|
7940 |
^ stream contents |
|
7941 |
||
7942 |
" |
|
7943 |
'hello %1' expandPlaceholders:$% with:#('world') on:Transcript. |
|
7944 |
'hello %a%<cr>' expandPlaceholders:$% |
|
7945 |
with:(Dictionary new at:'a' put:'world';yourself) |
|
7946 |
on:Transcript. |
|
7947 |
'hello %aa%<cr>' expandPlaceholders:$% with:(Dictionary new at:'a' put:'world';yourself) on:Transcript. |
|
7948 |
'hello %(aa)%<cr>' expandPlaceholders:$% with:(Dictionary new at:'aa' put:'world';yourself) on:Transcript. |
|
7949 |
'hello %aa%<cr>' expandPlaceholders:$% with:(Dictionary new at:'aa' put:'world';yourself) ignoreNumericEscapes:true requireParentheses:false on:Transcript. |
|
7950 |
||
7951 |
String streamContents:[:s| |
|
7952 |
'hello %1' expandPlaceholders:$% with:#('world') on:s. |
|
7953 |
s cr. |
|
7954 |
'hello $1; how is $2' expandPlaceholders:$$ with:#('world' 'this') on:s. |
|
7955 |
s cr. |
|
7956 |
'hello %2; how is %1' expandPlaceholders:$% with:#('world' 'this') on:s. |
|
7957 |
s cr. |
|
7958 |
'%1 plus %2 gives %3 ' expandPlaceholders:$% with:#(4 5 9) on:s. |
|
7959 |
s cr. |
|
7960 |
'%%(1)0 gives %(1)0' expandPlaceholders:$% with:#(123) on:s. |
|
7961 |
s cr. |
|
7962 |
'%%10 gives %10' expandPlaceholders:$% with:#(123) on:s. |
|
7963 |
s cr. |
|
7964 |
'%%(10) gives %(10) %<cr>%<tab>next line' expandPlaceholders:$% with:#(123) on:s. |
|
7965 |
s cr. |
|
7966 |
'%%test gives %test' expandPlaceholders:$% with:#(123) on:s. |
|
7967 |
s cr. |
|
7968 |
'|%%<tab>|%%1|%%<cr>| gives |%<tab>|%1|%<cr>|' expandPlaceholders:$% with:#(foo) on:s. |
|
7969 |
] |
|
7970 |
" |
|
7971 |
||
7972 |
"without xlation dictionary: |
|
7973 |
'hello %1' expandPlaceholdersWith:nil. |
|
7974 |
'hello%<cr> %1' expandPlaceholdersWith:nil. |
|
7975 |
" |
|
7976 |
||
7977 |
" |
|
7978 |
|dict| |
|
7979 |
||
7980 |
dict := Dictionary new. |
|
7981 |
dict at:1 put:'one'. |
|
7982 |
dict at:$a put:'AAAAA'. |
|
7983 |
dict at:$b put:[ Time now ]. |
|
7984 |
String streamContents:[:s| |
|
7985 |
'hello $1 $a $b' expandPlaceholders:$$ with:dict on:s. |
|
7986 |
]. |
|
7987 |
" |
|
7988 |
||
7989 |
"using blocks: |
|
7990 |
|dict| |
|
7991 |
||
7992 |
dict := Dictionary new. |
|
7993 |
dict at:'time' put:[Time now printString]. |
|
7994 |
dict at:'date' put:[Date today printString]. |
|
7995 |
String streamContents:[:s| |
|
7996 |
'it is $(time) $(date)' expandPlaceholders:$$ with:dict on:s. |
|
7997 |
]. |
|
7998 |
" |
|
7999 |
||
8000 |
"Created: / 14-01-2019 / 17:43:03 / Claus Gittinger" |
|
8001 |
"Modified: / 05-06-2019 / 17:05:47 / Claus Gittinger" |
|
8002 |
! |
|
8003 |
||
8004 |
expandPlaceholders:escapeCharacter with:argArrayOrDictionary |
|
8005 |
ignoreNumericEscapes:ignoreNumericEscapes |
|
8006 |
requireParentheses:requireParentheses |
|
8007 |
on:aStream |
|
8008 |
||
23603 | 8009 |
"this is the generic version of the old %-escaping method, allowing for an arbitrary |
8010 |
escape character to be used (typically $$ or $% are effectively used). |
|
8011 |
||
8012 |
Write the receiver to aStream, where all %i escapes are |
|
8013 |
replaced by corresponding arguments' printStrings from the argArray. |
|
8014 |
I.e. 'hello %1; how is %2' expandPlaceholdersWith:#('world' 'this') results |
|
8015 |
in the new string 'hello world; how is this'. |
|
8016 |
||
8017 |
As an extension, the argument may also be a dictionary, providing values for symbolic keys. |
|
8018 |
In this case, %a .. %z and %(...) are also allowed. |
|
8019 |
(%1..%9 require a numeric key in the dictionary, however) |
|
8020 |
||
8021 |
Also, values in argArrayOrDictionary may be blocks. |
|
8022 |
||
8023 |
To get a '%' character, use a '%%'-escape. |
|
8024 |
To get an integer-indexed placeHolder followed by another digit, |
|
8025 |
or an index > 9, you must use %(digit). |
|
8026 |
||
8027 |
See also bindWith:... for VisualAge compatibility. |
|
8028 |
Use %<cr> to insert a CR and %<tab> to insert a TAB. |
|
8029 |
||
8030 |
ignoreNumericEscapes controls if %<nr> escapes are expanded or not. |
|
8031 |
This is required for Windows batch-script expansion, where %<nr> should be left unchanged. |
|
8032 |
||
8033 |
requireParentheses controls if $abc is allowed or not. |
|
8034 |
If true, multi-character replacements need to be parenthized as $(abc); |
|
8035 |
if false, you can also write $abc. |
|
8036 |
" |
|
8037 |
||
24410 | 8038 |
self expandPlaceholders:escapeCharacter with:argArrayOrDictionary |
8039 |
ignoreNumericEscapes:ignoreNumericEscapes |
|
8040 |
ignoreNonNumericEscapes:false |
|
8041 |
ignoreSpecialEscapes:false |
|
8042 |
requireParentheses:requireParentheses |
|
8043 |
on:aStream. |
|
23603 | 8044 |
|
8045 |
" |
|
8046 |
'hello %1' expandPlaceholders:$% with:#('world') on:Transcript. |
|
8047 |
'hello %a%<cr>' expandPlaceholders:$% |
|
8048 |
with:(Dictionary new at:'a' put:'world';yourself) |
|
8049 |
on:Transcript. |
|
8050 |
'hello %aa%<cr>' expandPlaceholders:$% with:(Dictionary new at:'a' put:'world';yourself) on:Transcript. |
|
8051 |
'hello %(aa)%<cr>' expandPlaceholders:$% with:(Dictionary new at:'aa' put:'world';yourself) on:Transcript. |
|
8052 |
'hello %aa%<cr>' expandPlaceholders:$% with:(Dictionary new at:'aa' put:'world';yourself) ignoreNumericEscapes:true requireParentheses:false on:Transcript. |
|
8053 |
||
8054 |
String streamContents:[:s| |
|
8055 |
'hello %1' expandPlaceholders:$% with:#('world') on:s. |
|
8056 |
s cr. |
|
8057 |
'hello $1; how is $2' expandPlaceholders:$$ with:#('world' 'this') on:s. |
|
8058 |
s cr. |
|
8059 |
'hello %2; how is %1' expandPlaceholders:$% with:#('world' 'this') on:s. |
|
8060 |
s cr. |
|
8061 |
'%1 plus %2 gives %3 ' expandPlaceholders:$% with:#(4 5 9) on:s. |
|
8062 |
s cr. |
|
8063 |
'%%(1)0 gives %(1)0' expandPlaceholders:$% with:#(123) on:s. |
|
8064 |
s cr. |
|
8065 |
'%%10 gives %10' expandPlaceholders:$% with:#(123) on:s. |
|
8066 |
s cr. |
|
8067 |
'%%(10) gives %(10) %<cr>%<tab>next line' expandPlaceholders:$% with:#(123) on:s. |
|
8068 |
s cr. |
|
8069 |
'%%test gives %test' expandPlaceholders:$% with:#(123) on:s. |
|
8070 |
s cr. |
|
8071 |
'|%%<tab>|%%1|%%<cr>| gives |%<tab>|%1|%<cr>|' expandPlaceholders:$% with:#(foo) on:s. |
|
8072 |
] |
|
8073 |
" |
|
8074 |
||
8075 |
"without xlation dictionary: |
|
8076 |
'hello %1' expandPlaceholdersWith:nil. |
|
8077 |
'hello%<cr> %1' expandPlaceholdersWith:nil. |
|
8078 |
" |
|
8079 |
||
8080 |
" |
|
8081 |
|dict| |
|
8082 |
||
8083 |
dict := Dictionary new. |
|
8084 |
dict at:1 put:'one'. |
|
8085 |
dict at:$a put:'AAAAA'. |
|
8086 |
dict at:$b put:[ Time now ]. |
|
8087 |
String streamContents:[:s| |
|
8088 |
'hello $1 $a $b' expandPlaceholders:$$ with:dict on:s. |
|
8089 |
]. |
|
8090 |
" |
|
8091 |
||
8092 |
"using blocks: |
|
8093 |
|dict| |
|
8094 |
||
8095 |
dict := Dictionary new. |
|
8096 |
dict at:'time' put:[Time now printString]. |
|
8097 |
dict at:'date' put:[Date today printString]. |
|
8098 |
String streamContents:[:s| |
|
8099 |
'it is $(time) $(date)' expandPlaceholders:$$ with:dict on:s. |
|
8100 |
]. |
|
8101 |
" |
|
8102 |
||
8103 |
"Created: / 14-01-2019 / 17:43:03 / Claus Gittinger" |
|
24254 | 8104 |
"Modified: / 05-06-2019 / 17:05:47 / Claus Gittinger" |
23067 | 8105 |
! |
8106 |
||
8107 |
expandPlaceholders:escapeCharacter with:argArrayOrDictionary on:aStream |
|
8108 |
"this is the generic version of the old %-escaping method, allowing for an arbitrary |
|
8109 |
escape character to be used (typically $$ or $% are effectively used). |
|
8110 |
||
8111 |
Write the receiver to aStream, where all %i escapes are |
|
8112 |
replaced by corresponding arguments' printStrings from the argArrayOrDictionary. |
|
8113 |
I.e. 'hello %1; how is %2' expandPlaceholdersWith:#('world' 'this') results |
|
8114 |
in the new string 'hello world; how is this'. |
|
8115 |
||
8116 |
As an extension, the argument may also be a dictionary, providing values for symbolic keys. |
|
8117 |
In this case, %a .. %z and %(...) are also allowed. |
|
8118 |
(%1..%9 require a numeric key in the dictionary, however) |
|
8119 |
Also, the values in argArrayOrDictionary may be blocks. |
|
8120 |
||
8121 |
To get a '%' character, use a '%%'-escape. |
|
8122 |
To get an integer-indexed placeHolder followed by another digit, |
|
8123 |
or an index > 9, you must use %(digit). |
|
8124 |
||
8125 |
See also bindWith:... for VisualAge compatibility. |
|
8126 |
Use %<cr> to insert a CR and %<tab> to insert a TAB." |
|
8127 |
||
8128 |
^ self |
|
8129 |
expandPlaceholders:escapeCharacter |
|
8130 |
with:argArrayOrDictionary |
|
8131 |
ignoreNumericEscapes:false |
|
24410 | 8132 |
ignoreNonNumericEscapes:false |
8133 |
ignoreSpecialEscapes:false |
|
23603 | 8134 |
requireParentheses:true |
23067 | 8135 |
on:aStream |
8136 |
||
8137 |
" |
|
23603 | 8138 |
'hello %1' expandPlaceholders:$% with:#('world') on:Transcript. |
8139 |
||
23067 | 8140 |
String streamContents:[:s| |
8141 |
'hello %1' expandPlaceholders:$% with:#('world') on:s. |
|
8142 |
s cr. |
|
8143 |
'hello $1; how is $2' expandPlaceholders:$$ with:#('world' 'this') on:s. |
|
8144 |
s cr. |
|
8145 |
'hello %2; how is %1' expandPlaceholders:$% with:#('world' 'this') on:s. |
|
8146 |
s cr. |
|
8147 |
'%1 plus %2 gives %3 ' expandPlaceholders:$% with:#(4 5 9) on:s. |
|
8148 |
s cr. |
|
8149 |
'%%(1)0 gives %(1)0' expandPlaceholders:$% with:#(123) on:s. |
|
8150 |
s cr. |
|
8151 |
'%%10 gives %10' expandPlaceholders:$% with:#(123) on:s. |
|
8152 |
s cr. |
|
8153 |
'%%(10) gives %(10) %<cr>%<tab>next line' expandPlaceholders:$% with:#(123) on:s. |
|
8154 |
s cr. |
|
8155 |
'%%test gives %test' expandPlaceholders:$% with:#(123) on:s. |
|
8156 |
s cr. |
|
8157 |
'|%%<tab>|%%1|%%<cr>| gives |%<tab>|%1|%<cr>|' expandPlaceholders:$% with:#(foo) on:s. |
|
8158 |
] |
|
8159 |
" |
|
8160 |
||
8161 |
" |
|
8162 |
|dict| |
|
8163 |
||
8164 |
dict := Dictionary new. |
|
8165 |
dict at:1 put:'one'. |
|
8166 |
dict at:$a put:'AAAAA'. |
|
8167 |
dict at:$b put:[ Time now ]. |
|
8168 |
String streamContents:[:s| |
|
8169 |
'hello $1 $a $b' expandPlaceholders:$$ with:dict on:s. |
|
8170 |
]. |
|
8171 |
" |
|
8172 |
||
8173 |
"using blocks: |
|
8174 |
|dict| |
|
8175 |
||
8176 |
dict := Dictionary new. |
|
8177 |
dict at:'time' put:[Time now printString]. |
|
8178 |
dict at:'date' put:[Date today printString]. |
|
8179 |
String streamContents:[:s| |
|
8180 |
'it is $(time) $(date)' expandPlaceholders:$$ with:dict on:s. |
|
8181 |
]. |
|
8182 |
" |
|
8183 |
||
14765 | 8184 |
"Modified: / 18-11-2010 / 15:43:28 / cg" |
23603 | 8185 |
"Modified (comment): / 14-01-2019 / 18:02:59 / Claus Gittinger" |
14765 | 8186 |
! |
8187 |
||
9192 | 8188 |
expandPlaceholdersWith:argArrayOrDictionary |
608 | 8189 |
"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
|
8190 |
replaced by corresponding arguments' printStrings from the argArrayOrDictionary. |
608 | 8191 |
I.e. 'hello %1; how is %2' expandPlaceholdersWith:#('world' 'this') results |
8192 |
in the new string 'hello world; how is this'. |
|
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
8193 |
|
4410
2bdc0c89a42e
enhanced #exapndPlaceHolders, to allow for a dictionary arg
Claus Gittinger <cg@exept.de>
parents:
4409
diff
changeset
|
8194 |
As an extension, the argument may also be a dictionary, providing |
7313 | 8195 |
values for symbolic keys. |
4842 | 8196 |
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
|
8197 |
(%1..%9 require a numeric key in the dictionary, however) |
18425 | 8198 |
Also, the values in argArrayOrDictionary may be blocks. |
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
8199 |
|
608 | 8200 |
To get a '%' character, use a '%%'-escape. |
7313 | 8201 |
To get an integer-indexed placeHolder followed by another digit, |
8202 |
or an index > 9, you must use %(digit). |
|
608 | 8203 |
See also bindWith:... for VisualAge compatibility." |
8204 |
||
10906
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
8205 |
|stream| |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
8206 |
|
18269 | 8207 |
stream := (TextStream ? CharacterWriteStream) on:(self species uninitializedNew:self size + 20). |
23209 | 8208 |
"/ self expandPlaceholdersWith:argArrayOrDictionary on:stream. |
24410 | 8209 |
self |
8210 |
expandPlaceholders:$% with:argArrayOrDictionary |
|
8211 |
ignoreNumericEscapes:false |
|
8212 |
ignoreNonNumericEscapes:false |
|
8213 |
ignoreSpecialEscapes:false |
|
8214 |
requireParentheses:true |
|
8215 |
on:stream. |
|
10906
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
8216 |
^ stream contents. |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
8217 |
|
608 | 8218 |
" |
8874 | 8219 |
'hello %1' expandPlaceholdersWith:#('world') |
8220 |
'hello %1; how is %2' expandPlaceholdersWith:#('world' 'this') |
|
8221 |
'hello %2; how is %1' expandPlaceholdersWith:#('world' 'this') |
|
8222 |
'%1 plus %2 gives %3 ' expandPlaceholdersWith:#(4 5 9) |
|
8223 |
'%%(1)0 gives %(1)0' expandPlaceholdersWith:#(123) |
|
8224 |
'%%10 gives %10' expandPlaceholdersWith:#(123) |
|
8225 |
'%%(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
|
8226 |
'%test gives %1' expandPlaceholdersWith:#(123) |
11028
cb1ee3ddd91a
expandPlaceHolders: care fr Text with emphasis
Claus Gittinger <cg@exept.de>
parents:
11018
diff
changeset
|
8227 |
'bla %1 bla' expandPlaceholdersWith:{ 'hello' allBold } |
cb1ee3ddd91a
expandPlaceHolders: care fr Text with emphasis
Claus Gittinger <cg@exept.de>
parents:
11018
diff
changeset
|
8228 |
'bla %1 bla' expandPlaceholdersWith:{ 'hello' } |
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
8229 |
('bla %1 bla' withColor:Color red) |
20874 | 8230 |
expandPlaceholdersWith:{ 'hello' } |
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
8231 |
('bla %1 bla' withColor:Color red) |
20874 | 8232 |
expandPlaceholdersWith:{ 'hello' withColor:Color blue } |
608 | 8233 |
" |
1026
455c9f609f4d
care for 16bit strings in expandPlaceHolders
Claus Gittinger <cg@exept.de>
parents:
1025
diff
changeset
|
8234 |
|
4410
2bdc0c89a42e
enhanced #exapndPlaceHolders, to allow for a dictionary arg
Claus Gittinger <cg@exept.de>
parents:
4409
diff
changeset
|
8235 |
" |
2bdc0c89a42e
enhanced #exapndPlaceHolders, to allow for a dictionary arg
Claus Gittinger <cg@exept.de>
parents:
4409
diff
changeset
|
8236 |
|dict| |
2bdc0c89a42e
enhanced #exapndPlaceHolders, to allow for a dictionary arg
Claus Gittinger <cg@exept.de>
parents:
4409
diff
changeset
|
8237 |
|
2bdc0c89a42e
enhanced #exapndPlaceHolders, to allow for a dictionary arg
Claus Gittinger <cg@exept.de>
parents:
4409
diff
changeset
|
8238 |
dict := Dictionary new. |
2bdc0c89a42e
enhanced #exapndPlaceHolders, to allow for a dictionary arg
Claus Gittinger <cg@exept.de>
parents:
4409
diff
changeset
|
8239 |
dict at:1 put:'one'. |
2bdc0c89a42e
enhanced #exapndPlaceHolders, to allow for a dictionary arg
Claus Gittinger <cg@exept.de>
parents:
4409
diff
changeset
|
8240 |
dict at:$a put:'AAAAA'. |
8794
a5df33935142
nindWith supports blocks in the value dictionary
Claus Gittinger <cg@exept.de>
parents:
8710
diff
changeset
|
8241 |
dict at:$b put:[ Time now ]. |
12316
3e78905d70a9
comment/format in: #expandPlaceholdersWith:
Claus Gittinger <cg@exept.de>
parents:
12196
diff
changeset
|
8242 |
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
|
8243 |
'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
|
8244 |
" |
2bdc0c89a42e
enhanced #exapndPlaceHolders, to allow for a dictionary arg
Claus Gittinger <cg@exept.de>
parents:
4409
diff
changeset
|
8245 |
|
23209 | 8246 |
"Modified: / 01-07-1997 / 00:53:24 / cg" |
8247 |
"Modified: / 14-07-2018 / 09:23:31 / Claus Gittinger" |
|
9192 | 8248 |
! |
8249 |
||
10906
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
8250 |
expandPlaceholdersWith:argArrayOrDictionary on:aStream |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
8251 |
"write the receiver to aStream, where all %i escapes are |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
8252 |
replaced by corresponding arguments' printStrings from the argArrayOrDictionary. |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
8253 |
I.e. 'hello %1; how is %2' expandPlaceholdersWith:#('world' 'this') results |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
8254 |
in the new string 'hello world; how is this'. |
18425 | 8255 |
|
10906
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
8256 |
As an extension, the argument may also be a dictionary, providing |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
8257 |
values for symbolic keys. |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
8258 |
In this case, %a .. %z and %(...) are also allowed. |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
8259 |
(%1..%9 require a numeric key in the dictionary, however) |
18425 | 8260 |
Also, the values in argArrayOrDictionary may be blocks. |
8261 |
||
10906
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
8262 |
To get a '%' character, use a '%%'-escape. |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
8263 |
To get an integer-indexed placeHolder followed by another digit, |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
8264 |
or an index > 9, you must use %(digit). |
18425 | 8265 |
|
10935 | 8266 |
See also bindWith:... for VisualAge compatibility. |
10939
d0ca31c1d1e9
#expandPlaceHoldersWith: handle %<cr> %<tab> etc
Stefan Vogel <sv@exept.de>
parents:
10935
diff
changeset
|
8267 |
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
|
8268 |
|
24410 | 8269 |
^ self |
8270 |
expandPlaceholders:$% with:argArrayOrDictionary |
|
8271 |
ignoreNumericEscapes:false |
|
8272 |
ignoreNonNumericEscapes:false |
|
8273 |
ignoreSpecialEscapes:false |
|
8274 |
requireParentheses:true |
|
8275 |
on:aStream |
|
10906
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
8276 |
|
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
8277 |
" |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
8278 |
String streamContents:[:s| |
20874 | 8279 |
'hello %1' expandPlaceholdersWith:#('world') on:s. |
8280 |
s cr. |
|
8281 |
'hello %1; how is %2' expandPlaceholdersWith:#('world' 'this') on:s. |
|
8282 |
s cr. |
|
8283 |
'hello %2; how is %1' expandPlaceholdersWith:#('world' 'this') on:s. |
|
8284 |
s cr. |
|
8285 |
'%1 plus %2 gives %3 ' expandPlaceholdersWith:#(4 5 9) on:s. |
|
8286 |
s cr. |
|
8287 |
'%%(1)0 gives %(1)0' expandPlaceholdersWith:#(123) on:s. |
|
8288 |
s cr. |
|
8289 |
'%%10 gives %10' expandPlaceholdersWith:#(123) on:s. |
|
8290 |
s cr. |
|
8291 |
'%%(10) gives %(10) %<cr>%<tab>next line' expandPlaceholdersWith:#(123) on:s. |
|
8292 |
s cr. |
|
8293 |
'%test gives %1' expandPlaceholdersWith:#(123) on:s. |
|
10906
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
8294 |
] |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
8295 |
" |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
8296 |
|
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
8297 |
" |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
8298 |
|dict| |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
8299 |
|
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
8300 |
dict := Dictionary new. |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
8301 |
dict at:1 put:'one'. |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
8302 |
dict at:$a put:'AAAAA'. |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
8303 |
dict at:$b put:[ Time now ]. |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
8304 |
String streamContents:[:s| |
20874 | 8305 |
'hello %1 %a %b' expandPlaceholdersWith:dict on:s. |
10906
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
8306 |
]. |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
8307 |
" |
13008
a27647c36b31
changed: #expandPlaceholdersWith:on:
Claus Gittinger <cg@exept.de>
parents:
12925
diff
changeset
|
8308 |
|
13138
d2eb0eba5907
changed: #expandPlaceholdersWith:on:
Claus Gittinger <cg@exept.de>
parents:
13137
diff
changeset
|
8309 |
"Modified: / 18-11-2010 / 15:43:28 / cg" |
23067 | 8310 |
"Modified: / 14-06-2018 / 11:45:36 / Claus Gittinger" |
10906
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
8311 |
! |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
8312 |
|
22929 | 8313 |
extractPlaceHolders:escapeCharacter |
8314 |
"returns a list of placeholder keys of the form %i, |
|
8315 |
where i is either a single digit (as in %1, %2) |
|
8316 |
or a single letter (as in %a, %z) |
|
8317 |
or a word-key, as in %(one), %(fooBar). |
|
8318 |
For numeric keys, the returned collection contains integers; |
|
8319 |
for non-numeric ones, it includes strings." |
|
8320 |
||
8321 |
|setOfKeys loggingDictionary| |
|
8322 |
||
8323 |
setOfKeys := Set new. |
|
8324 |
loggingDictionary := Plug new. |
|
8325 |
loggingDictionary respondTo:#at: with:[:key | setOfKeys add:key. key asString]. |
|
8326 |
loggingDictionary respondTo:#at:ifAbsent: with:[:key :def | setOfKeys add:key. key asString]. |
|
8327 |
loggingDictionary respondTo:#includesKey: with:[:key | setOfKeys add:key. true]. |
|
8328 |
loggingDictionary respondTo:#isSequenceable with:[false]. |
|
8329 |
self expandPlaceholders:escapeCharacter with:loggingDictionary. |
|
8330 |
^ setOfKeys |
|
8331 |
||
8332 |
" |
|
8333 |
self assert:('hello %1 and %2' extractPlaceHolders:$%) asSet = #(1 2) asSet |
|
8334 |
self assert:('hello %9 and %(10)' extractPlaceHolders:$%) asSet = #(9 10) asSet |
|
8335 |
self assert:('hello %a and %(foo) and %1' extractPlaceHolders:$%) asSet = #(1 'a' 'foo') asSet |
|
8336 |
self assert:('hello %a and %(foo) and %1' extractPlaceHolders:$$) asSet = #() asSet |
|
8337 |
" |
|
8338 |
! |
|
8339 |
||
9230 | 8340 |
firstLine |
14405
1d0d99a95bba
comment/format in:7 methods
Claus Gittinger <cg@exept.de>
parents:
14168
diff
changeset
|
8341 |
"return the first line of a multiline string" |
1d0d99a95bba
comment/format in:7 methods
Claus Gittinger <cg@exept.de>
parents:
14168
diff
changeset
|
8342 |
|
1d0d99a95bba
comment/format in:7 methods
Claus Gittinger <cg@exept.de>
parents:
14168
diff
changeset
|
8343 |
^ self asCollectionOfSubCollectionsSeparatedBy:(Character cr) do:[:line | ^ line]. |
9230 | 8344 |
|
8345 |
" |
|
10703
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
8346 |
'hello' firstLine |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
8347 |
'1\2\3' withCRs firstLine |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
8348 |
'\1\2\3' withCRs firstLine |
9230 | 8349 |
" |
8350 |
! |
|
8351 |
||
13987 | 8352 |
tokensBasedOn:aCharacter |
8353 |
"this is an ST-80 alias for the ST/X method |
|
20874 | 8354 |
asCollectionOfSubstringsSeparatedBy:" |
13987 | 8355 |
|
8356 |
^ self asCollectionOfSubstringsSeparatedBy:aCharacter |
|
8357 |
||
8358 |
" |
|
8359 |
'hello:world:isnt:this nice' tokensBasedOn:$: |
|
8360 |
'foo,bar,baz' tokensBasedOn:$, |
|
8361 |
'/etc/passwd' asFilename readStream nextLine tokensBasedOn:$: |
|
8362 |
" |
|
8363 |
! |
|
8364 |
||
8365 |
withCEscapes |
|
16863 | 8366 |
"return a new string consisting of receiver's characters |
13987 | 8367 |
with all special and unprintable characters replaced by \X-character escapes. |
8368 |
(similar to the way C-language literal Strings are represented). |
|
8369 |
The resulting string will contain only 7-bit ascii characters. |
|
8370 |
Emphasis is not supported. |
|
8371 |
The following escapes are generated: |
|
20874 | 8372 |
\' single quote character |
8373 |
\dQuote double quote character |
|
8374 |
\r return character |
|
8375 |
\r return character |
|
8376 |
\n newline character |
|
8377 |
\t tab character |
|
8378 |
\\ the \ character itself |
|
8379 |
\xnn two digit hex number defining the characters ascii value |
|
8380 |
\unnnn four digit hex number defining the characters ascii value |
|
8381 |
\Unnnnnnnn eight digit hex number defining the characters ascii value |
|
16863 | 8382 |
This is the opposite of withoutCEscapes. |
17000 | 8383 |
|
8384 |
Sigh: this is named completely wrong (opposite naming of withCRs/witoutCRs), |
|
20874 | 8385 |
but it cannot be changed easily, as these methods are already used heavily |
13987 | 8386 |
" |
8387 |
||
8388 |
|anyEscapeNeeded out seq| |
|
8389 |
||
8390 |
" |
|
8391 |
first, check if any escape is needed and return the receiver unchanged if not |
|
8392 |
" |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
8393 |
anyEscapeNeeded := self |
20874 | 8394 |
contains:[:ch | |
8395 |
((ch codePoint between:32 and:126) not |
|
8396 |
or:[ch == $' or:[ch == $"]]) |
|
8397 |
]. |
|
13987 | 8398 |
anyEscapeNeeded ifFalse:[ ^ self ]. |
8399 |
||
8400 |
self hasChangeOfEmphasis ifTrue:[ self error:'emphasis not supported' ]. |
|
8401 |
||
18269 | 8402 |
out := WriteStream on:(String uninitializedNew:self size-1). |
13987 | 8403 |
|
8404 |
self do:[:ch | |
|
20874 | 8405 |
|cp| |
8406 |
||
8407 |
(ch == $' or:[ch == $"]) ifTrue:[ |
|
8408 |
out nextPut:$\. |
|
8409 |
out nextPut:ch. |
|
8410 |
] ifFalse:[ |
|
8411 |
(ch codePoint between:32 and:126) ifTrue:[ |
|
8412 |
out nextPut:ch |
|
8413 |
] ifFalse:[ |
|
8414 |
ch == Character return ifTrue:[ |
|
8415 |
seq := '\r' |
|
8416 |
] ifFalse:[ ch == Character nl ifTrue:[ |
|
8417 |
seq := '\n' |
|
8418 |
] ifFalse:[ ch == Character tab ifTrue:[ |
|
8419 |
seq := '\t' |
|
8420 |
] ifFalse:[ ch == $\ ifTrue:[ |
|
8421 |
seq := '\\' |
|
8422 |
] ifFalse:[ |
|
8423 |
cp := ch codePoint. |
|
8424 |
cp <= 16rFF ifTrue:[ |
|
22344 | 8425 |
seq := '\x' , (cp printStringRadix:16 size:2 fill:$0) |
20874 | 8426 |
] ifFalse:[ |
8427 |
cp <= 16rFFFF ifTrue:[ |
|
22344 | 8428 |
seq := '\u' , (cp printStringRadix:16 size:4 fill:$0) |
20874 | 8429 |
] ifFalse:[ |
22344 | 8430 |
seq := '\U',(cp printStringRadix:16 size:8 fill:$0) |
20874 | 8431 |
] |
8432 |
] |
|
8433 |
]]]]. |
|
8434 |
out nextPutAll:seq |
|
8435 |
]. |
|
8436 |
]. |
|
13987 | 8437 |
]. |
8438 |
^ out contents |
|
8439 |
||
8440 |
" |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
8441 |
'hello\n\tworld' withoutCEscapes. |
17000 | 8442 |
'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
|
8443 |
('hello ',(Character value:16r1234),' world') withCEscapes |
13987 | 8444 |
" |
8445 |
||
8446 |
"Created: / 25-01-2012 / 11:08:16 / cg" |
|
22344 | 8447 |
"Modified: / 12-11-2017 / 12:39:57 / cg" |
13987 | 8448 |
! |
8449 |
||
9192 | 8450 |
withCRs |
17205 | 8451 |
"return a new string consisting of the receiver's characters |
16757 | 8452 |
with all \-characters replaced by cr-characters. |
8453 |
If there are no backslashes, return the original" |
|
8454 |
||
16759 | 8455 |
^ self copyReplaceAll:$\ with:(Character cr) ifNone:self |
9192 | 8456 |
|
8457 |
" |
|
8458 |
'hello\world' withCRs |
|
8459 |
" |
|
8460 |
||
8461 |
"Modified: / 18.7.1998 / 22:53:02 / cg" |
|
8462 |
! |
|
8463 |
||
8464 |
withEscapes |
|
15405 | 8465 |
<resource: #obsolete> |
13987 | 8466 |
"has been renamed; the name withEscapes is misleading" |
8467 |
||
8468 |
self obsoleteMethodWarning:'use withoutCEscapes'. |
|
8469 |
^ self withoutCEscapes. |
|
8470 |
||
8471 |
"Modified: / 25-01-2012 / 10:42:30 / cg" |
|
9192 | 8472 |
! |
8473 |
||
8474 |
withMatchEscapes |
|
4117 | 8475 |
"return a copy of the receiver with all match characters escaped |
8476 |
by $\ characters (to be usable as a match string). |
|
8477 |
Return the receiver, if there are none." |
|
8478 |
||
5342
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
8479 |
|in out c escape| |
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
8480 |
|
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
8481 |
escape := self class matchEscapeCharacter. |
4117 | 8482 |
|
8483 |
in := self readStream. |
|
7986 | 8484 |
out := WriteStream on:(self species new:self size). |
4117 | 8485 |
[in atEnd] whileFalse:[ |
20874 | 8486 |
c := in next. |
8487 |
(c == escape or:['*[#' includes:c]) ifTrue:[ |
|
8488 |
out nextPut:$\. |
|
8489 |
]. |
|
8490 |
out nextPut:c. |
|
4117 | 8491 |
]. |
8492 |
^ out contents. |
|
8493 |
||
8494 |
" |
|
8874 | 8495 |
'*foo' withMatchEscapes |
8496 |
'\*foo' withMatchEscapes |
|
8497 |
'*foo' withMatchEscapes |
|
8498 |
'\\*foo' withMatchEscapes |
|
8499 |
'foo*' withMatchEscapes |
|
8500 |
'foo\*' withMatchEscapes |
|
8501 |
'foo\' withMatchEscapes |
|
8502 |
'f*o*o' withMatchEscapes |
|
4117 | 8503 |
" |
8504 |
||
8505 |
"Modified: 2.4.1997 / 18:13:04 / cg" |
|
9192 | 8506 |
! |
8507 |
||
14992 | 8508 |
withSeparatorsCompacted |
8509 |
"return a new string with each sequence of whiteSpace replaced by a single space character. |
|
8510 |
Preserves a leading/trailing space." |
|
8511 |
||
8512 |
^ self species streamContents:[:s | |
|
20874 | 8513 |
|skipping| |
8514 |
||
8515 |
skipping := false. |
|
8516 |
1 to:self size do:[:idx | |
|
8517 |
|char| |
|
8518 |
||
8519 |
char := self at:idx. |
|
8520 |
char isSeparator ifFalse:[ |
|
8521 |
s nextPut:char. |
|
8522 |
skipping := false. |
|
8523 |
] ifTrue:[ |
|
8524 |
skipping ifFalse:[ |
|
8525 |
s nextPut:(Character space). |
|
8526 |
skipping := true |
|
8527 |
]. |
|
8528 |
] |
|
8529 |
] |
|
14992 | 8530 |
] |
8531 |
||
8532 |
" |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
8533 |
'hello wwww' withSeparatorsCompacted |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
8534 |
'hello wwww' withSeparatorsCompacted |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
8535 |
' hello wwww' withSeparatorsCompacted |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
8536 |
' hello wwww ' withSeparatorsCompacted |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
8537 |
' hello wwww ' withSeparatorsCompacted |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
8538 |
'hel lo www w' withSeparatorsCompacted |
14992 | 8539 |
" |
8540 |
! |
|
8541 |
||
18464 | 8542 |
withSeparatorsReplacedBy:replacementCharacter |
8543 |
"return a new string with each separator (whitespace) replaced by replacementCharacter. |
|
8544 |
Typically used with space as replacementCharacter" |
|
8545 |
||
8546 |
^ self species streamContents:[:s | |
|
20874 | 8547 |
self do:[:ch | |
8548 |
ch isSeparator ifTrue:[ |
|
8549 |
s nextPut:replacementCharacter |
|
8550 |
] ifFalse:[ |
|
8551 |
s nextPut:ch. |
|
8552 |
] |
|
8553 |
] |
|
18464 | 8554 |
] |
8555 |
||
8556 |
" |
|
8557 |
'hello wwww' withSeparatorsReplacedBy:$* |
|
8558 |
'hello ww ww' withSeparatorsReplacedBy:$* |
|
8559 |
' hello wwww' withSeparatorsReplacedBy:$* |
|
8560 |
'hel lo www w' withSeparatorsReplacedBy:$* |
|
8561 |
'hel |
|
8562 |
lo www |
|
8563 |
w' withSeparatorsReplacedBy:$* |
|
8564 |
" |
|
8565 |
! |
|
8566 |
||
9192 | 8567 |
withTabs |
19464 | 8568 |
"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
|
8569 |
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
|
8570 |
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
|
8571 |
otherwise a new string is returned. |
8874 | 8572 |
Limitation: only the very first spaces are replaced |
20874 | 8573 |
(i.e. if the receiver contains newLine characters, |
8574 |
no tabs are inserted after those lineBreaks)" |
|
8874 | 8575 |
|
8576 |
|idx "{ SmallInteger }" |
|
8577 |
nTabs "{ SmallInteger }" |
|
608 | 8578 |
newString| |
8579 |
||
8580 |
idx := self findFirst:[:c | (c ~~ Character space)]. |
|
8581 |
nTabs := (idx-1) // 8. |
|
5815
4acdd87a77d4
// can return negative numbers.
Stefan Vogel <sv@exept.de>
parents:
5795
diff
changeset
|
8582 |
nTabs <= 0 ifTrue:[^ self]. |
608 | 8583 |
|
8584 |
"any tabs" |
|
15523 | 8585 |
newString := self species new:(self size - (nTabs * 7)). |
608 | 8586 |
newString atAll:(1 to:nTabs) put:(Character tab). |
8587 |
newString replaceFrom:(nTabs + 1) with:self startingAt:(nTabs * 8 + 1). |
|
8588 |
^ newString |
|
8589 |
||
8590 |
" |
|
8874 | 8591 |
'12345678901234567890' withTabs |
8592 |
' 8901234567890' withTabs |
|
8593 |
' 901234567890' withTabs |
|
608 | 8594 |
' 67890' withTabs |
8595 |
' 7890' withTabs |
|
8596 |
' 890' withTabs |
|
8597 |
" |
|
9192 | 8598 |
! |
8599 |
||
8600 |
withTabsExpanded |
|
19464 | 8601 |
"return a string consisting of the receiver's characters, |
8874 | 8602 |
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
|
8603 |
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
|
8604 |
otherwise a new string is returned. |
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
678
diff
changeset
|
8605 |
This does handle multiline strings." |
608 | 8606 |
|
5741
3a6086774d38
added #withTabsExpanded: (i.e. arg to specify tab-column width)
Claus Gittinger <cg@exept.de>
parents:
5739
diff
changeset
|
8607 |
^ self withTabsExpanded:8 |
3a6086774d38
added #withTabsExpanded: (i.e. arg to specify tab-column width)
Claus Gittinger <cg@exept.de>
parents:
5739
diff
changeset
|
8608 |
|
3a6086774d38
added #withTabsExpanded: (i.e. arg to specify tab-column width)
Claus Gittinger <cg@exept.de>
parents:
5739
diff
changeset
|
8609 |
" |
8874 | 8610 |
('1' , Character tab asString , 'x') withTabsExpanded |
8611 |
('12345' , Character tab asString , 'x') withTabsExpanded |
|
8612 |
('123456' , Character tab asString , 'x') withTabsExpanded |
|
8613 |
('1234567' , Character tab asString , 'x') withTabsExpanded |
|
8614 |
('12345678' , Character tab asString , 'x') withTabsExpanded |
|
8615 |
('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
|
8616 |
|
3a6086774d38
added #withTabsExpanded: (i.e. arg to specify tab-column width)
Claus Gittinger <cg@exept.de>
parents:
5739
diff
changeset
|
8617 |
(String with:Character tab |
20874 | 8618 |
with:Character tab |
8619 |
with:$1) withTabsExpanded |
|
5741
3a6086774d38
added #withTabsExpanded: (i.e. arg to specify tab-column width)
Claus Gittinger <cg@exept.de>
parents:
5739
diff
changeset
|
8620 |
|
3a6086774d38
added #withTabsExpanded: (i.e. arg to specify tab-column width)
Claus Gittinger <cg@exept.de>
parents:
5739
diff
changeset
|
8621 |
(String with:Character tab |
20874 | 8622 |
with:$1 |
8623 |
with:Character tab |
|
8624 |
with:$2) withTabsExpanded |
|
5741
3a6086774d38
added #withTabsExpanded: (i.e. arg to specify tab-column width)
Claus Gittinger <cg@exept.de>
parents:
5739
diff
changeset
|
8625 |
|
3a6086774d38
added #withTabsExpanded: (i.e. arg to specify tab-column width)
Claus Gittinger <cg@exept.de>
parents:
5739
diff
changeset
|
8626 |
(String with:Character tab |
20874 | 8627 |
with:$1 |
8628 |
with:Character cr |
|
8629 |
with:Character tab |
|
8630 |
with:$2) withTabsExpanded |
|
5741
3a6086774d38
added #withTabsExpanded: (i.e. arg to specify tab-column width)
Claus Gittinger <cg@exept.de>
parents:
5739
diff
changeset
|
8631 |
" |
3a6086774d38
added #withTabsExpanded: (i.e. arg to specify tab-column width)
Claus Gittinger <cg@exept.de>
parents:
5739
diff
changeset
|
8632 |
|
3a6086774d38
added #withTabsExpanded: (i.e. arg to specify tab-column width)
Claus Gittinger <cg@exept.de>
parents:
5739
diff
changeset
|
8633 |
"Modified: 12.5.1996 / 13:05:10 / cg" |
9192 | 8634 |
! |
8635 |
||
8636 |
withTabsExpanded:numSpaces |
|
19464 | 8637 |
"return a string consisting of the receiver's characters, |
8874 | 8638 |
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
|
8639 |
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
|
8640 |
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
|
8641 |
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
|
8642 |
|
8874 | 8643 |
|col "{ SmallInteger }" |
1381 | 8644 |
str ch |
8645 |
dstIdx "{ SmallInteger }" |
|
8646 |
newSz "{ SmallInteger }" |
|
8647 |
sz "{ SmallInteger }" |
|
8648 |
hasEmphasis e| |
|
608 | 8649 |
|
8650 |
(self includes:(Character tab)) ifFalse:[^ self]. |
|
1381 | 8651 |
|
8652 |
sz := self size. |
|
8653 |
||
8654 |
"/ count the new size first, instead of |
|
8655 |
"/ multiple resizing (better for large strings) |
|
8656 |
||
8657 |
col := 1. newSz := 0. |
|
8658 |
1 to:sz do:[:srcIdx | |
|
20874 | 8659 |
ch := self at:srcIdx. |
8660 |
ch == Character tab ifFalse:[ |
|
8661 |
col := col + 1. |
|
8662 |
newSz := newSz + 1. |
|
8663 |
ch == Character cr ifTrue:[ |
|
8664 |
col := 1 |
|
8665 |
]. |
|
8666 |
] ifTrue:[ |
|
8667 |
(col \\ numSpaces) to:numSpaces do:[:ii | |
|
8668 |
newSz := newSz + 1. |
|
8669 |
col := col + 1 |
|
8670 |
]. |
|
8671 |
] |
|
608 | 8672 |
]. |
1381 | 8673 |
|
8674 |
str := self species new:newSz. |
|
23428 | 8675 |
(str isText and:[self bitsPerCharacter > 8]) ifTrue:[ |
8676 |
str := Text string:(Unicode16String new:newSz) |
|
8677 |
]. |
|
8678 |
||
1381 | 8679 |
hasEmphasis := self hasChangeOfEmphasis. |
8680 |
||
8681 |
col := 1. dstIdx := 1. |
|
8682 |
1 to:sz do:[:srcIdx | |
|
20874 | 8683 |
ch := self at:srcIdx. |
8684 |
||
8685 |
ch == Character tab ifFalse:[ |
|
8686 |
col := col + 1. |
|
8687 |
ch == Character cr ifTrue:[ |
|
8688 |
col := 1 |
|
8689 |
]. |
|
8690 |
hasEmphasis ifTrue:[ |
|
8691 |
e := self emphasisAt:srcIdx. |
|
8692 |
str emphasisAt:dstIdx put:e |
|
8693 |
]. |
|
8694 |
str at:dstIdx put:ch. |
|
8695 |
dstIdx := dstIdx + 1 |
|
8696 |
] ifTrue:[ |
|
8697 |
(col \\ numSpaces) to:numSpaces do:[:ii | |
|
8698 |
str at:dstIdx put:Character space. |
|
8699 |
dstIdx := dstIdx + 1. |
|
8700 |
col := col + 1 |
|
8701 |
]. |
|
8702 |
] |
|
1381 | 8703 |
]. |
8704 |
^ str |
|
608 | 8705 |
|
8706 |
" |
|
8874 | 8707 |
('1' , Character tab asString , 'x') withTabsExpanded |
8708 |
('1' , Character tab asString , 'x') withTabsExpanded:4 |
|
8709 |
('12345' , Character tab asString , 'x') withTabsExpanded |
|
8710 |
('123456' , Character tab asString , 'x') withTabsExpanded |
|
8711 |
('1234567' , Character tab asString , 'x') withTabsExpanded |
|
8712 |
('12345678' , Character tab asString , 'x') withTabsExpanded |
|
8713 |
('123456789' , Character tab asString , 'x') withTabsExpanded |
|
735
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
678
diff
changeset
|
8714 |
|
608 | 8715 |
(String with:Character tab |
20874 | 8716 |
with:Character tab |
8717 |
with:$1) withTabsExpanded |
|
608 | 8718 |
|
8719 |
(String with:Character tab |
|
20874 | 8720 |
with:$1 |
8721 |
with:Character tab |
|
8722 |
with:$2) withTabsExpanded |
|
735
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
678
diff
changeset
|
8723 |
|
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
678
diff
changeset
|
8724 |
(String with:Character tab |
20874 | 8725 |
with:$1 |
8726 |
with:Character cr |
|
8727 |
with:Character tab |
|
8728 |
with:$2) withTabsExpanded |
|
608 | 8729 |
" |
735
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
678
diff
changeset
|
8730 |
|
1381 | 8731 |
"Modified: 12.5.1996 / 13:05:10 / cg" |
9192 | 8732 |
! |
8733 |
||
13987 | 8734 |
withoutAllSpaces |
16855 | 8735 |
"return a copy of the receiver with all whitespace removed" |
8736 |
||
13987 | 8737 |
^ self asCollectionOfWords asStringWith:''. |
8738 |
"/ |col string| |
|
8739 |
"/ |
|
8740 |
"/ col := self asCollectionOfWords. |
|
8741 |
"/ string := String new. |
|
8742 |
"/ col do:[:el | |
|
8743 |
"/ string := string,el |
|
8744 |
"/ ]. |
|
8745 |
"/ ^string |
|
8746 |
||
8747 |
" |
|
8748 |
'hello wwww' withoutAllSpaces |
|
8749 |
'hel lo www w' withoutAllSpaces |
|
8750 |
" |
|
8751 |
||
8752 |
"Modified: / 18.7.1998 / 22:53:08 / cg" |
|
8753 |
! |
|
8754 |
||
8755 |
withoutCEscapes |
|
15628 | 8756 |
"return a new string consisting of receiver's characters |
22205 | 8757 |
with all \X-character escapes replaced by corresponding characters. |
13987 | 8758 |
(similar to the way C-language Strings are converted). |
8759 |
The following escapes are supported: |
|
20874 | 8760 |
\r return character |
8761 |
\n newline character |
|
8762 |
\b backspace character |
|
8763 |
\f formfeed character |
|
8764 |
\t tab character |
|
8765 |
\e escape character |
|
8766 |
\\ the \ character itself |
|
8767 |
\nnn three digit octal number defining the characters ascii value |
|
8768 |
\xnn two digit hex number defining the characters ascii value |
|
8769 |
\unnnn four digit hex number defining the characters unicode value |
|
8770 |
\Unnnnnnnn eight digit hex number defining the characters unicode value |
|
8771 |
\other other |
|
13987 | 8772 |
|
8773 |
Notice, that \' is NOT a valid escape, since the general syntax of |
|
8774 |
string constants is not affected by this method. |
|
8775 |
||
8776 |
Although easily implementable, this is NOT done automatically |
|
8777 |
by the compiler (due to a lack of a language standard for this). |
|
22205 | 8778 |
However, the compiler may detect sends of #withEscapes to string literals |
13987 | 8779 |
and place a modified string constant into the binary/byte-code. |
8780 |
Therefore, no runtime penalty will be payed for using these escapes. |
|
8781 |
(not in pre 2.11 versions) |
|
16863 | 8782 |
|
8783 |
This is the opposite of withCEscapes. |
|
17000 | 8784 |
|
8785 |
Sigh: this is named completely wrong (opposite naming of withCRs/witoutCRs), |
|
20874 | 8786 |
but it cannot be changed easily, as these methods are already used heavily |
13987 | 8787 |
" |
8788 |
||
8789 |
|val "{ SmallInteger }" |
|
8790 |
in out nextChar nDigits| |
|
8791 |
||
8792 |
" |
|
8793 |
first, see if there is any escape; if not, return the receiver unchanged |
|
8794 |
" |
|
8795 |
(self includes:$\) ifFalse:[^ self ]. |
|
8796 |
||
8797 |
self hasChangeOfEmphasis ifTrue:[ self error:'emphasis not supported' ]. |
|
8798 |
out := CharacterWriteStream on:(String new:self size - 1). |
|
8799 |
||
8800 |
in := ReadStream on:self. |
|
8801 |
[in atEnd] whileFalse:[ |
|
20874 | 8802 |
nextChar := in next. |
8803 |
nextChar == $\ ifTrue:[ |
|
8804 |
in atEnd ifTrue:[ |
|
8805 |
] ifFalse:[ |
|
8806 |
nextChar := in next. |
|
8807 |
nextChar == $r ifTrue:[ |
|
8808 |
nextChar := Character return |
|
8809 |
] ifFalse:[ nextChar == $n ifTrue:[ |
|
8810 |
nextChar := Character nl |
|
8811 |
] ifFalse:[ nextChar == $b ifTrue:[ |
|
8812 |
nextChar := Character backspace |
|
8813 |
] ifFalse:[ nextChar == $f ifTrue:[ |
|
8814 |
nextChar := Character newPage |
|
8815 |
] ifFalse:[ nextChar == $t ifTrue:[ |
|
8816 |
nextChar := Character tab |
|
8817 |
] ifFalse:[ nextChar == $e ifTrue:[ |
|
8818 |
nextChar := Character esc |
|
8819 |
] ifFalse:[ |
|
8820 |
nextChar == $0 ifTrue:[ |
|
8821 |
val := 0. |
|
8822 |
nextChar := in peek. |
|
8823 |
nDigits := 1. |
|
8824 |
[nextChar notNil and:[nextChar isDigit and:[nDigits <= 3]]] whileTrue:[ |
|
8825 |
val := (val * 8) + nextChar digitValue. |
|
8826 |
nextChar := in nextPeek. |
|
8827 |
nDigits := nDigits + 1. |
|
8828 |
]. |
|
8829 |
nextChar := Character value:val. |
|
8830 |
] ifFalse:[ |
|
8831 |
val := 0. |
|
8832 |
nextChar == $x ifTrue:[ |
|
8833 |
2 timesRepeat:[ |
|
8834 |
nextChar := in next. |
|
8835 |
val := (val * 16) + nextChar digitValue. |
|
8836 |
]. |
|
8837 |
nextChar := Character value:val. |
|
8838 |
] ifFalse:[ |
|
8839 |
nextChar == $u ifTrue:[ |
|
8840 |
4 timesRepeat:[ |
|
8841 |
nextChar := in next. |
|
8842 |
val := (val * 16) + nextChar digitValue. |
|
8843 |
]. |
|
8844 |
nextChar := Character value:val. |
|
8845 |
] ifFalse:[ |
|
8846 |
nextChar == $U ifTrue:[ |
|
8847 |
8 timesRepeat:[ |
|
8848 |
nextChar := in next. |
|
8849 |
val := (val * 16) + nextChar digitValue. |
|
8850 |
]. |
|
8851 |
nextChar := Character value:val. |
|
8852 |
] |
|
8853 |
] |
|
8854 |
] |
|
8855 |
] |
|
8856 |
]]]]]]. |
|
8857 |
]. |
|
8858 |
]. |
|
8859 |
out nextPut:nextChar. |
|
13987 | 8860 |
]. |
8861 |
^ out contents |
|
8862 |
||
8863 |
" |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
8864 |
'hello world' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
8865 |
'hello\world' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
8866 |
'hello\world\' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
8867 |
'hello world\' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
8868 |
'hello\tworld' withoutCEscapes |
13987 | 8869 |
'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
|
8870 |
'hello\tworld\n' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
8871 |
'hello\010world' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
8872 |
'hello\r\nworld' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
8873 |
'hello\r\n\x08world' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
8874 |
'0\x080' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
8875 |
'0\u12340' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
8876 |
'0\U123456780' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
8877 |
'0\0a' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
8878 |
'0\00a' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
8879 |
'0\000a' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
8880 |
'0\0000a' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
8881 |
'0\00000a' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
8882 |
'0\03770' withoutCEscapes |
13987 | 8883 |
" |
8884 |
||
8885 |
"Created: / 25-01-2012 / 10:41:44 / cg" |
|
22205 | 8886 |
"Modified (comment): / 23-08-2017 / 11:07:43 / mawalch" |
13987 | 8887 |
! |
8888 |
||
9192 | 8889 |
withoutCRs |
19464 | 8890 |
"return a new collection consisting of receiver's elements |
9192 | 8891 |
with all cr-characters replaced by \-characters. |
8892 |
This is the reverse operation of withCRs." |
|
8893 |
||
16759 | 8894 |
^ self copyReplaceAll:(Character cr) with:$\ ifNone:self |
12319
18b80c2ce1e3
oops #withoutSeparators #withoutSpaces were noops
Claus Gittinger <cg@exept.de>
parents:
12317
diff
changeset
|
8895 |
|
9192 | 8896 |
" |
8897 |
'hello |
|
8898 |
world' withoutCRs |
|
8899 |
" |
|
8900 |
||
8901 |
"Modified: / 18.7.1998 / 22:53:08 / cg" |
|
8902 |
! |
|
8903 |
||
8904 |
withoutLeadingSeparators |
|
608 | 8905 |
"return a copy of myself without leading separators. |
8906 |
Notice: this does remove tabs, newline or any other whitespace. |
|
8907 |
Returns an empty string, if the receiver consist only of whitespace." |
|
8908 |
||
8909 |
|index| |
|
8910 |
||
8911 |
index := self indexOfNonSeparatorStartingAt:1. |
|
8912 |
index ~~ 0 ifTrue:[ |
|
20874 | 8913 |
index == 1 ifTrue:[ |
8914 |
^ self |
|
8915 |
]. |
|
8916 |
^ self copyFrom:index |
|
608 | 8917 |
]. |
8918 |
^ '' |
|
8919 |
||
8920 |
" |
|
8874 | 8921 |
' foo ' withoutLeadingSeparators |
8922 |
'foo ' withoutLeadingSeparators |
|
8923 |
' foo' withoutLeadingSeparators |
|
8924 |
' ' withoutLeadingSeparators |
|
8925 |
'foo' withoutLeadingSeparators |
|
8926 |
(' ' , Character tab asString , ' foo ') withoutLeadingSeparators inspect |
|
608 | 8927 |
" |
9192 | 8928 |
! |
8929 |
||
8930 |
withoutMatchEscapes |
|
4117 | 8931 |
"return a copy of the receiver with all $\ removed or |
8932 |
the receiver, if there are none." |
|
8933 |
||
5342
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
8934 |
|in out c escape| |
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
8935 |
|
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
8936 |
escape := self class matchEscapeCharacter. |
4117 | 8937 |
|
8938 |
in := self readStream. |
|
8222
f2c454a9a038
replaced '' writeStream by String writeStream
Claus Gittinger <cg@exept.de>
parents:
8124
diff
changeset
|
8939 |
out := self species writeStream. |
4117 | 8940 |
[in atEnd] whileFalse:[ |
20874 | 8941 |
c := in next. |
8942 |
c == escape ifTrue:[ |
|
8943 |
in atEnd ifFalse:[ |
|
8944 |
c := in next. |
|
8945 |
] |
|
8946 |
]. |
|
8947 |
out nextPut:c. |
|
4117 | 8948 |
]. |
8949 |
^ out contents. |
|
8950 |
||
8951 |
" |
|
8874 | 8952 |
'*foo' withoutMatchEscapes |
8953 |
'\*foo' withoutMatchEscapes |
|
8954 |
'*foo' withoutMatchEscapes |
|
8955 |
'\\*foo' withoutMatchEscapes |
|
8956 |
'foo*' withoutMatchEscapes |
|
8957 |
'foo\*' withoutMatchEscapes |
|
8958 |
'foo\' withoutMatchEscapes |
|
8959 |
'f\*o\*o' withoutMatchEscapes |
|
4117 | 8960 |
" |
8961 |
||
8962 |
"Modified: 30.6.1997 / 13:40:23 / cg" |
|
9192 | 8963 |
! |
8964 |
||
23249 | 8965 |
withoutPrefix:aStringOrCharacter |
19677 | 8966 |
"if the receiver startsWith aString, return a copy without it. |
9192 | 8967 |
Otherwise return the receiver" |
8968 |
||
23249 | 8969 |
(self startsWith:aStringOrCharacter) ifTrue:[ |
8970 |
aStringOrCharacter isCharacter ifTrue:[ |
|
8971 |
^ self copyFrom:2 |
|
8972 |
] ifFalse:[ |
|
8973 |
^ self copyFrom:aStringOrCharacter size+1 |
|
8974 |
]. |
|
9192 | 8975 |
]. |
8976 |
^ self |
|
8977 |
||
8978 |
" |
|
8979 |
'helloworld' withoutPrefix:'hello' |
|
8980 |
'helloworld' withoutPrefix:'foo' |
|
24142 | 8981 |
'helloworld' withoutPrefix:$h |
8982 |
'helloworld' withoutPrefix:#( $h ) |
|
9192 | 8983 |
" |
19677 | 8984 |
|
8985 |
"Modified: / 30-04-2016 / 10:01:00 / cg" |
|
23249 | 8986 |
"Modified: / 31-07-2018 / 17:05:36 / Claus Gittinger" |
24142 | 8987 |
"Modified (comment): / 24-05-2019 / 09:22:54 / Claus Gittinger" |
23249 | 8988 |
! |
8989 |
||
8990 |
withoutPrefix:aStringOrCharacter caseSensitive:caseSensitive |
|
23234 | 8991 |
"if the receiver startsWith aString, return a copy without it. |
8992 |
Otherwise return the receiver" |
|
8993 |
||
23249 | 8994 |
(self startsWith:aStringOrCharacter caseSensitive:caseSensitive) ifTrue:[ |
8995 |
aStringOrCharacter isCharacter ifTrue:[ |
|
8996 |
^ self copyFrom:2 |
|
8997 |
] ifFalse:[ |
|
8998 |
^ self copyFrom:aStringOrCharacter size+1 |
|
8999 |
]. |
|
23234 | 9000 |
]. |
9001 |
^ self |
|
9002 |
||
9003 |
" |
|
24142 | 9004 |
'Helloworld' withoutPrefix:'hello' caseSensitive:false |
9005 |
'Helloworld' withoutPrefix:'foo' caseSensitive:false |
|
9006 |
'Helloworld' withoutPrefix:$h caseSensitive:false |
|
9007 |
'Helloworld' withoutPrefix:#( $h ) caseSensitive:false |
|
23234 | 9008 |
" |
9009 |
||
9010 |
"Created: / 27-07-2018 / 08:30:03 / Claus Gittinger" |
|
23249 | 9011 |
"Modified: / 31-07-2018 / 17:05:53 / Claus Gittinger" |
24142 | 9012 |
"Modified (comment): / 24-05-2019 / 09:23:30 / Claus Gittinger" |
23234 | 9013 |
! |
9014 |
||
10671 | 9015 |
withoutQuotes |
17000 | 9016 |
"/ remove quotes ($" and $') from the front and end of myself (if matching)" |
9017 |
||
9018 |
|firstChar| |
|
9019 |
||
17024 | 9020 |
self isEmpty ifTrue:[^ self]. |
9021 |
||
17000 | 9022 |
firstChar := self first. |
9023 |
((firstChar == $") or:[firstChar == $']) ifFalse:[^ self]. |
|
9024 |
||
9025 |
self last == firstChar ifTrue:[ |
|
20874 | 9026 |
^ self copyFrom:2 to:(self size-1) |
10671 | 9027 |
]. |
17000 | 9028 |
^ self |
9029 |
||
9030 |
"/ |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
9031 |
"/ '"hello"' withoutQuotes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
9032 |
"/ '''hello''' withoutQuotes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
9033 |
"/ 'hello' withoutQuotes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
9034 |
"/ '"hello' withoutQuotes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
9035 |
"/ 'hello"' withoutQuotes |
17000 | 9036 |
"/ |
10671 | 9037 |
! |
9038 |
||
9192 | 9039 |
withoutSeparators |
608 | 9040 |
"return a copy of myself without leading and trailing whitespace. |
4156 | 9041 |
(but whiteSpace in-between is preserved) |
608 | 9042 |
Whitespace is space, tab, newline, formfeed. |
9043 |
Use withoutSpaces, if you want to remove spaces only." |
|
9044 |
||
12319
18b80c2ce1e3
oops #withoutSeparators #withoutSpaces were noops
Claus Gittinger <cg@exept.de>
parents:
12317
diff
changeset
|
9045 |
^ self trimForWhich:[:ch | ch isSeparator] |
608 | 9046 |
|
9047 |
" |
|
8874 | 9048 |
' foo ' withoutSeparators |
9049 |
' foo' withoutSeparators |
|
9050 |
'foo ' withoutSeparators |
|
9051 |
' ' withoutSeparators |
|
9052 |
(' foo' , Character tab asString , ' ') withoutSeparators inspect |
|
12319
18b80c2ce1e3
oops #withoutSeparators #withoutSpaces were noops
Claus Gittinger <cg@exept.de>
parents:
12317
diff
changeset
|
9053 |
' foo ' asUnicode16String withoutSeparators |
608 | 9054 |
" |
9192 | 9055 |
! |
9056 |
||
9057 |
withoutSpaces |
|
608 | 9058 |
"return a copy of myself without leading and trailing spaces. |
4156 | 9059 |
(but spaces in-between are preserved) |
608 | 9060 |
Notice: this does NOT remove tabs, newline or any other whitespace. |
9061 |
Use withoutSeparators for this." |
|
9062 |
||
12319
18b80c2ce1e3
oops #withoutSeparators #withoutSpaces were noops
Claus Gittinger <cg@exept.de>
parents:
12317
diff
changeset
|
9063 |
^ self trimForWhich:[:ch | ch == Character space] |
608 | 9064 |
|
9065 |
" |
|
8874 | 9066 |
' foo ' withoutSpaces |
9067 |
'foo ' withoutSpaces |
|
9068 |
' foo' withoutSpaces |
|
9069 |
' ' withoutSpaces |
|
9070 |
'a b' withoutSpaces |
|
9071 |
(' foo' , Character tab asString , ' ') withoutSpaces inspect |
|
608 | 9072 |
" |
9192 | 9073 |
! |
9074 |
||
23249 | 9075 |
withoutSuffix:aStringOrCharacter |
22357 | 9076 |
"if the receiver endsWith aString, return a copy without it. |
9077 |
Otherwise return the receiver" |
|
9078 |
||
23249 | 9079 |
(self endsWith:aStringOrCharacter) ifTrue:[ |
9080 |
aStringOrCharacter isCharacter ifTrue:[ |
|
9081 |
^ self copyButLast:1 |
|
9082 |
] ifFalse:[ |
|
9083 |
^ self copyButLast:aStringOrCharacter size |
|
9084 |
]. |
|
22357 | 9085 |
]. |
9086 |
^ self |
|
9087 |
||
9088 |
" |
|
9089 |
'helloworld' withoutSuffix:'world' |
|
9090 |
'helloworld' withoutSuffix:'foo' |
|
23249 | 9091 |
'helloworldx' withoutSuffix:$x |
22357 | 9092 |
" |
9093 |
||
9094 |
"Created: / 23-10-2017 / 15:01:37 / cg" |
|
23249 | 9095 |
"Modified: / 31-07-2018 / 17:06:34 / Claus Gittinger" |
9096 |
! |
|
9097 |
||
9098 |
withoutSuffix:aStringOrCharacter caseSensitive:caseSensitive |
|
23234 | 9099 |
"if the receiver endsWith aString, return a copy without it. |
9100 |
Otherwise return the receiver" |
|
9101 |
||
23249 | 9102 |
(self endsWith:aStringOrCharacter caseSensitive:caseSensitive) ifTrue:[ |
9103 |
aStringOrCharacter isCharacter ifTrue:[ |
|
9104 |
^ self copyButLast:1 |
|
9105 |
] ifFalse:[ |
|
9106 |
^ self copyButLast:aStringOrCharacter size |
|
9107 |
]. |
|
23234 | 9108 |
]. |
9109 |
^ self |
|
9110 |
||
9111 |
" |
|
9112 |
'helloworld' withoutSuffix:'world' |
|
9113 |
'helloworld' withoutSuffix:'foo' |
|
9114 |
" |
|
9115 |
||
9116 |
"Created: / 27-07-2018 / 08:30:10 / Claus Gittinger" |
|
23249 | 9117 |
"Modified: / 31-07-2018 / 17:06:58 / Claus Gittinger" |
23234 | 9118 |
! |
9119 |
||
9192 | 9120 |
withoutTrailingSeparators |
915
1ac1c7e942f4
added withoutTrailingSeparators
Claus Gittinger <cg@exept.de>
parents:
828
diff
changeset
|
9121 |
"return a copy of myself without trailing separators. |
1ac1c7e942f4
added withoutTrailingSeparators
Claus Gittinger <cg@exept.de>
parents:
828
diff
changeset
|
9122 |
Notice: this does remove tabs, newline or any other whitespace. |
1ac1c7e942f4
added withoutTrailingSeparators
Claus Gittinger <cg@exept.de>
parents:
828
diff
changeset
|
9123 |
Returns an empty string, if the receiver consist only of whitespace." |
1ac1c7e942f4
added withoutTrailingSeparators
Claus Gittinger <cg@exept.de>
parents:
828
diff
changeset
|
9124 |
|
12188 | 9125 |
^ self withoutTrailingForWhich:[:ch | ch isSeparator] |
915
1ac1c7e942f4
added withoutTrailingSeparators
Claus Gittinger <cg@exept.de>
parents:
828
diff
changeset
|
9126 |
|
1ac1c7e942f4
added withoutTrailingSeparators
Claus Gittinger <cg@exept.de>
parents:
828
diff
changeset
|
9127 |
" |
8874 | 9128 |
' foo ' withoutTrailingSeparators |
9129 |
'foo ' withoutTrailingSeparators |
|
9130 |
' foo' withoutTrailingSeparators |
|
9131 |
' ' withoutTrailingSeparators |
|
9132 |
'foo' withoutTrailingSeparators |
|
9133 |
(' ' , Character tab asString , ' foo ') withoutTrailingSeparators inspect |
|
9134 |
(' foo' , Character tab asString) withoutTrailingSeparators inspect |
|
915
1ac1c7e942f4
added withoutTrailingSeparators
Claus Gittinger <cg@exept.de>
parents:
828
diff
changeset
|
9135 |
" |
9192 | 9136 |
! ! |
9137 |
||
24250 | 9138 |
|
9192 | 9139 |
!CharacterArray methodsFor:'substring searching'! |
9140 |
||
15055 | 9141 |
findRangeOfString:subString |
9142 |
"find a substring. if found, return the start- and endIndex; |
|
9143 |
if not found, return an empty interval." |
|
9144 |
||
9145 |
^ self rangeOfSubCollection:subString startingAt:1 ifAbsent:[0 to:-1] caseSensitive:true |
|
9146 |
||
9147 |
" |
|
9148 |
'hello world' findRangeOfString:'llo' |
|
9149 |
'hello world' findRangeOfString:'ole' |
|
9150 |
" |
|
9151 |
! |
|
9152 |
||
9192 | 9153 |
findString:subString |
9154 |
"find a substring. if found, return the index; |
|
9155 |
if not found, return 0." |
|
9156 |
||
9157 |
^ self indexOfSubCollection:subString startingAt:1 ifAbsent:0 caseSensitive:true |
|
9158 |
||
9159 |
" |
|
9160 |
'hello world' findString:'llo' |
|
9161 |
'hello world' findString:'ole' |
|
9162 |
" |
|
9163 |
! |
|
608 | 9164 |
|
15734 | 9165 |
findString:subString caseSensitive:caseSensitive |
9166 |
"find a substring. if found, return the index; |
|
9167 |
if not found, return 0." |
|
9168 |
||
9169 |
^ self indexOfSubCollection:subString startingAt:1 ifAbsent:0 caseSensitive:caseSensitive |
|
9170 |
||
9171 |
" |
|
9172 |
'hello world' findString:'LLo' caseSensitive:true |
|
9173 |
'hello world' findString:'LLo' caseSensitive:false |
|
9174 |
" |
|
9175 |
! |
|
9176 |
||
608 | 9177 |
findString:subString ifAbsent:exceptionBlock |
9178 |
"find a substring. If found, return the index; |
|
9179 |
if not found, return the result of evaluating exceptionBlock." |
|
9180 |
||
9087
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
9181 |
^ self indexOfSubCollection:subString startingAt:1 ifAbsent:exceptionBlock caseSensitive:true |
608 | 9182 |
! |
9183 |
||
11018 | 9184 |
findString:subString ignoreCase:ignoreCase |
15734 | 9185 |
<resource: #obsolete> |
11018 | 9186 |
"find a substring. if found, return the index; |
9187 |
if not found, return 0." |
|
9188 |
||
9189 |
^ self indexOfSubCollection:subString startingAt:1 ifAbsent:0 caseSensitive:ignoreCase not |
|
9190 |
||
9191 |
" |
|
9192 |
'hello world' findString:'LLo' ignoreCase:false |
|
9193 |
'hello world' findString:'LLo' ignoreCase:true |
|
9194 |
" |
|
9195 |
! |
|
9196 |
||
608 | 9197 |
findString:subString startingAt:index |
9198 |
"find a substring, starting at index. if found, return the index; |
|
9199 |
if not found, return 0." |
|
9200 |
||
9087
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
9201 |
^ self indexOfSubCollection:subString startingAt:index ifAbsent:0 caseSensitive:true |
608 | 9202 |
|
9203 |
" |
|
9192 | 9204 |
'hello yello' findString:'llo' startingAt:1 |
9205 |
'hello yello' findString:'llo' startingAt:5 |
|
9206 |
'hello yello' findString:'llo' startingAt:15 |
|
9207 |
" |
|
9208 |
! |
|
608 | 9209 |
|
9210 |
findString:subString startingAt:index ifAbsent:exceptionBlock |
|
9211 |
"find a substring, starting at index. if found, return the index; |
|
9212 |
if not found, return the result of evaluating exceptionBlock." |
|
9213 |
||
9087
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
9214 |
^ self indexOfSubCollection:subString startingAt:index ifAbsent:exceptionBlock caseSensitive:true |
608 | 9215 |
! |
9216 |
||
12177 | 9217 |
findString:subString startingAt:index ifAbsent:exceptionBlock caseSensitive:caseSensitive |
10929 | 9218 |
"find a substring, starting at index. if found, return the index; |
9219 |
if not found, return the result of evaluating exceptionBlock." |
|
9220 |
||
9221 |
^ self indexOfSubCollection:subString startingAt:index ifAbsent:exceptionBlock caseSensitive:caseSensitive |
|
9222 |
! |
|
9223 |
||
608 | 9224 |
includesString:aString |
9225 |
"return true, if a substring is contained in the receiver" |
|
9226 |
||
9087
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
9227 |
^ (self indexOfSubCollection:aString startingAt:1 ifAbsent:0 caseSensitive:true) ~~ 0 |
608 | 9228 |
|
9229 |
" |
|
12177 | 9230 |
'hello world' includesString:'hel' |
8874 | 9231 |
'hello world' includesString:'rld' |
9192 | 9232 |
'hello world' includesString:'llo' |
9233 |
'hello world' includesString:'LLO' |
|
12177 | 9234 |
'hello world' includesString:'' |
9192 | 9235 |
" |
9236 |
! |
|
9191 | 9237 |
|
9238 |
includesString:aString caseSensitive:caseSensitive |
|
9087
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
9239 |
"return true, if a substring is contained in the receiver" |
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
9240 |
|
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
9241 |
^ (self indexOfSubCollection:aString startingAt:1 ifAbsent:0 caseSensitive:caseSensitive) ~~ 0 |
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
9242 |
|
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
9243 |
" |
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
9244 |
'hello world' includesString:'hel' caseSensitive:true |
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
9245 |
'hello world' includesString:'HEL' caseSensitive:true |
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
9246 |
'hello world' includesString:'HEL' caseSensitive:false |
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
9247 |
|
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
9248 |
'hello world' includesString:'wor' caseSensitive:true |
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
9249 |
'hello world' includesString:'WOR' caseSensitive:true |
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
9250 |
'hello world' includesString:'WOR' caseSensitive:false |
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
9251 |
" |
9192 | 9252 |
! |
9253 |
||
23060 | 9254 |
indexOfString:aString |
9255 |
"VSE and V'age compatibility" |
|
9256 |
"find a substring. If found, return the index; if not found, return 0." |
|
9257 |
||
9258 |
^ self indexOfSubCollection:aString startingAt:1 ifAbsent:[0] caseSensitive:true |
|
9259 |
||
9260 |
" |
|
9261 |
'hello world' indexOfString:'hello' -> 1 |
|
9262 |
'hello world' indexOfString:'world' -> 7 |
|
9263 |
'hello world' indexOfString:'World' -> 0 |
|
9264 |
'hello world' indexOfString:'World' caseSensitive:false -> 7 |
|
9265 |
" |
|
9266 |
||
9267 |
"Modified (format): / 08-06-2018 / 14:43:15 / Claus Gittinger" |
|
9268 |
! |
|
9269 |
||
9270 |
indexOfString:aString caseSensitive:caseSensitive |
|
9271 |
"VSE and V'age compatibility" |
|
9272 |
"find a substring. If found, return the index; if not found, return 0." |
|
9273 |
||
9274 |
^ self indexOfSubCollection:aString startingAt:1 ifAbsent:[0] caseSensitive:caseSensitive |
|
9275 |
||
9276 |
" |
|
9277 |
'hello world' indexOfString:'hello' -> 1 |
|
9278 |
'hello world' indexOfString:'world' -> 7 |
|
9279 |
'hello world' indexOfString:'World' -> 0 |
|
9280 |
'hello world' indexOfString:'World' caseSensitive:false -> 7 |
|
9281 |
" |
|
9282 |
||
9283 |
"Created: / 08-06-2018 / 14:39:18 / Claus Gittinger" |
|
9284 |
! |
|
9285 |
||
9286 |
indexOfString:aString caseSensitive:caseSensitive ifAbsent:exceptionValue |
|
9287 |
"VSE and V'age compatibility" |
|
9288 |
"find a substring. If found, return the index; if not found, return 0." |
|
9289 |
||
9290 |
^ self indexOfSubCollection:aString startingAt:1 ifAbsent:exceptionValue caseSensitive:caseSensitive |
|
9291 |
||
9292 |
" |
|
9293 |
'abcdefabcdef' indexOfString:'fab' ifAbsent:[999] -> 6 |
|
9294 |
'abcdefabcdef' indexOfString:'Fab' ifAbsent:[999] -> 999 |
|
9295 |
'abcdefabcdef' indexOfString:'Fab' caseSensitive:false ifAbsent:[999] -> 6 |
|
9296 |
'abcdefabcdef' indexOfString:'xxx' caseSensitive:false ifAbsent:[999] -> 999 |
|
9297 |
" |
|
9298 |
||
9299 |
"Created: / 08-06-2018 / 14:40:21 / Claus Gittinger" |
|
9300 |
! |
|
9301 |
||
9302 |
indexOfString:aString caseSensitive:caseSensitive startingAt:startIndex |
|
9303 |
"VSE and V'age compatibility" |
|
9304 |
"find a substring. If found, return the index; if not found, return 0." |
|
9305 |
||
9306 |
^ self indexOfSubCollection:aString startingAt:startIndex ifAbsent:[0] caseSensitive:caseSensitive |
|
9307 |
||
9308 |
"Created: / 08-06-2018 / 14:44:17 / Claus Gittinger" |
|
9309 |
! |
|
9310 |
||
9311 |
indexOfString:aString caseSensitive:caseSensitive startingAt:startIndex ifAbsent:exceptionalValue |
|
9312 |
"VSE and V'age compatibility" |
|
9313 |
"find a substring. |
|
9314 |
If found, return the index; if not found, the value from exceptionalValue." |
|
9315 |
||
9316 |
^ self indexOfSubCollection:aString startingAt:startIndex ifAbsent:exceptionalValue caseSensitive:caseSensitive |
|
9317 |
||
9318 |
"Created: / 08-06-2018 / 14:42:33 / Claus Gittinger" |
|
9319 |
! |
|
9320 |
||
9321 |
indexOfString:aString ifAbsent:exceptionValue |
|
9322 |
"VSE and V'age compatibility" |
|
9323 |
"find a substring. If found, return the index; if not found, return 0." |
|
9324 |
||
9325 |
^ self indexOfSubCollection:aString startingAt:1 ifAbsent:exceptionValue caseSensitive:true |
|
9326 |
||
9327 |
" |
|
9328 |
'abcdefabcdef' indexOfString:'fab' ifAbsent:[999] -> 6 |
|
9329 |
'abcdefabcdef' indexOfString:'Fab' ifAbsent:[999] -> 999 |
|
9330 |
'abcdefabcdef' indexOfString:'Fab' caseSensitieve:false ifAbsent:[999] -> 999 |
|
9331 |
'abcdefabcdef' indexOfString:'xxx' ifAbsent:[999] -> 999 |
|
9332 |
" |
|
9333 |
||
9334 |
"Modified: / 08-06-2018 / 14:43:44 / Claus Gittinger" |
|
9335 |
! |
|
9336 |
||
9337 |
indexOfString:aString startingAt:startIndex |
|
9338 |
"VSE and V'age compatibility" |
|
9339 |
"find a substring. If found, return the index; if not found, return 0." |
|
9340 |
||
9341 |
^ self indexOfSubCollection:aString startingAt:startIndex ifAbsent:[0] caseSensitive:true |
|
9342 |
||
9343 |
"Modified: / 08-06-2018 / 14:37:52 / Claus Gittinger" |
|
9344 |
! |
|
9345 |
||
9346 |
indexOfString:aString startingAt:startIndex ifAbsent:exceptionalValue |
|
9347 |
"VSE and V'age compatibility" |
|
9348 |
"find a substring. |
|
9349 |
If found, return the index; if not found, the value from exceptionalValue." |
|
9350 |
||
9351 |
^ self indexOfSubCollection:aString startingAt:startIndex ifAbsent:exceptionalValue caseSensitive:true |
|
9352 |
||
9353 |
"Modified: / 08-06-2018 / 14:38:12 / Claus Gittinger" |
|
9354 |
! |
|
9355 |
||
18671 | 9356 |
indexOfSubCollection:subString caseSensitive:caseSensitive |
9357 |
"find a substring, starting at index. if found, return the index; |
|
21675 | 9358 |
if not found, return the result of evaluating exceptionBlock." |
18671 | 9359 |
|
9360 |
^ self indexOfSubCollection:subString startingAt:1 ifAbsent:[0] caseSensitive:caseSensitive. |
|
21675 | 9361 |
|
9362 |
"Modified (comment): / 28-03-2017 / 15:53:30 / stefan" |
|
18671 | 9363 |
! |
9364 |
||
9192 | 9365 |
indexOfSubCollection:subString startingAt:index ifAbsent:exceptionBlock |
9366 |
"find a substring, starting at index. if found, return the index; |
|
21675 | 9367 |
if not found, return the result of evaluating exceptionBlock." |
9192 | 9368 |
|
9369 |
^ self indexOfSubCollection:subString startingAt:index ifAbsent:exceptionBlock caseSensitive:true. |
|
21675 | 9370 |
|
9371 |
"Modified (comment): / 28-03-2017 / 15:53:14 / stefan" |
|
9192 | 9372 |
! |
9373 |
||
9374 |
indexOfSubCollection:subString startingAt:index ifAbsent:exceptionBlock caseSensitive:caseSensitive |
|
9087
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
9375 |
"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
|
9376 |
if not found, return the result of evaluating exceptionBlock. |
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
9377 |
This is a q&d hack - not very efficient" |
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
9378 |
|
608 | 9379 |
|firstChar found |
9380 |
startIndex "{ Class: SmallInteger }" |
|
9381 |
subSize "{ Class: SmallInteger }" |
|
9382 |
mySize "{ Class: SmallInteger }" |
|
21675 | 9383 |
runIdx "{ Class: SmallInteger }"| |
608 | 9384 |
|
9385 |
subSize := subString size. |
|
12177 | 9386 |
subSize == 0 ifTrue:[ |
20874 | 9387 |
subString isString ifFalse:[ |
23001 | 9388 |
self proceedableError:'non string argument'. |
20874 | 9389 |
]. |
9390 |
"empty string does not match" |
|
9391 |
^ 0. |
|
9392 |
"empty string matches" |
|
15433 | 9393 |
"/ ^ index |
6122
a98958f1c85f
handle non-string arg to indexOfSubString:
Claus Gittinger <cg@exept.de>
parents:
6119
diff
changeset
|
9394 |
]. |
9087
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
9395 |
|
608 | 9396 |
mySize := self size. |
9397 |
firstChar := subString at:1. |
|
9087
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
9398 |
caseSensitive ifTrue:[ |
20874 | 9399 |
startIndex := self indexOf:firstChar startingAt:index. |
9087
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
9400 |
] ifFalse:[ |
20874 | 9401 |
startIndex := self findFirst:[:c | c sameAs:firstChar] startingAt:index. |
9087
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
9402 |
]. |
21672 | 9403 |
[startIndex ~~ 0] whileTrue:[ |
20874 | 9404 |
runIdx := startIndex. |
9405 |
found := true. |
|
9406 |
1 to:subSize do:[:i | |
|
9407 |
runIdx > mySize ifTrue:[ |
|
9408 |
found := false |
|
9409 |
] ifFalse:[ |
|
21675 | 9410 |
caseSensitive ifTrue:[ |
9411 |
(subString at:i) ~= (self at:runIdx) ifTrue:[found := false]. |
|
9412 |
] ifFalse:[ |
|
9413 |
((subString at:i) sameAs:(self at:runIdx)) ifFalse:[found := false]. |
|
9414 |
]. |
|
20874 | 9415 |
]. |
9416 |
runIdx := runIdx + 1 |
|
9417 |
]. |
|
9418 |
found ifTrue:[ |
|
9419 |
^ startIndex |
|
9420 |
]. |
|
9421 |
caseSensitive ifTrue:[ |
|
9422 |
startIndex := self indexOf:firstChar startingAt:(startIndex + 1) |
|
9423 |
] ifFalse:[ |
|
9424 |
startIndex := self findFirst:[:c | c sameAs:firstChar] startingAt:(startIndex + 1). |
|
9425 |
]. |
|
608 | 9426 |
]. |
9427 |
^ exceptionBlock value |
|
1005
4507487e0be6
more on encoding/decoding (still unfinished)
Claus Gittinger <cg@exept.de>
parents:
997
diff
changeset
|
9428 |
|
21675 | 9429 |
" |
9430 |
'АБВГДЕЖ' asLowercase indexOfSubCollection:'ВГДЕ' startingAt:1 ifAbsent:nil caseSensitive:false. |
|
9431 |
'АБВГДЕЖ' indexOfSubCollection:'ВГДЕ' startingAt:1 ifAbsent:nil caseSensitive:true. |
|
9432 |
" |
|
9433 |
||
21672 | 9434 |
"Modified: / 23-02-1996 / 15:35:15 / cg" |
21675 | 9435 |
"Modified (comment): / 28-03-2017 / 16:05:48 / stefan" |
23001 | 9436 |
"Modified: / 24-05-2018 / 14:55:38 / Claus Gittinger" |
9192 | 9437 |
! |
9438 |
||
17191 | 9439 |
indexOfSubCollection:subString startingAt:index ifAbsent:exceptionBlock caseSensitive:caseSensitive ignoreDiacritics:ignoreDiacritics |
9440 |
"find a substring, starting at index. if found, return the index; |
|
9441 |
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
|
9442 |
This is a q&d hack - not very efficient |
17191 | 9443 |
(see implementation in string, for a much faster algorithm)" |
9444 |
||
9445 |
|firstChar found |
|
9446 |
startIndex "{ Class: SmallInteger }" |
|
9447 |
subSize "{ Class: SmallInteger }" |
|
9448 |
mySize "{ Class: SmallInteger }" |
|
9449 |
runIdx "{ Class: SmallInteger }" |
|
9450 |
tester charMap firstCharMapped findNextIndex| |
|
9451 |
||
9452 |
subSize := subString size. |
|
9453 |
subSize == 0 ifTrue:[ |
|
20874 | 9454 |
subString isString ifFalse:[ |
23001 | 9455 |
self proceedableError:'non string argument'. |
20874 | 9456 |
]. |
9457 |
"empty string does not match" |
|
9458 |
^ 0. |
|
9459 |
"empty string matches" |
|
17191 | 9460 |
"/ ^ index |
9461 |
]. |
|
9462 |
||
9463 |
mySize := self size. |
|
9464 |
firstChar := subString at:1. |
|
9465 |
ignoreDiacritics ifTrue:[ |
|
20874 | 9466 |
caseSensitive ifTrue:[ |
9467 |
charMap := [:ch | ch asLowercase withoutDiacritics]. |
|
9468 |
] ifFalse:[ |
|
9469 |
charMap := [:ch | ch withoutDiacritics]. |
|
9470 |
]. |
|
9471 |
tester := [:c1 :c2 | (charMap value:c1) = (charMap value:c2) ]. |
|
9472 |
firstCharMapped := (charMap value:firstChar). |
|
9473 |
findNextIndex := [:index | self findFirst:[:ch | (charMap value:ch) = firstCharMapped] startingAt:index]. |
|
17191 | 9474 |
] ifFalse:[ |
20874 | 9475 |
caseSensitive ifTrue:[ |
9476 |
tester := [:c1 :c2 | c1 = c2 ]. |
|
9477 |
findNextIndex := [:index | self indexOf:firstChar startingAt:index]. |
|
9478 |
] ifFalse:[ |
|
9479 |
tester := [:c1 :c2 | c1 sameAs: c2 ]. |
|
9480 |
findNextIndex := [:index | self findFirst:[:c | c sameAs:firstChar] startingAt:index]. |
|
9481 |
]. |
|
17191 | 9482 |
]. |
9483 |
startIndex := findNextIndex value:index. |
|
9484 |
[startIndex == 0] whileFalse:[ |
|
20874 | 9485 |
runIdx := startIndex. |
9486 |
found := true. |
|
9487 |
1 to:subSize do:[:i | |
|
9488 |
runIdx > mySize ifTrue:[ |
|
9489 |
found := false |
|
9490 |
] ifFalse:[ |
|
9491 |
(tester value:(subString at:i) value:(self at:runIdx)) ifFalse:[ |
|
9492 |
found := false |
|
9493 |
] |
|
9494 |
]. |
|
9495 |
runIdx := runIdx + 1 |
|
9496 |
]. |
|
9497 |
found ifTrue:[ |
|
9498 |
^ startIndex |
|
9499 |
]. |
|
9500 |
startIndex := findNextIndex value:(startIndex + 1) |
|
17191 | 9501 |
]. |
9502 |
^ exceptionBlock value |
|
9503 |
||
9504 |
" |
|
17600 | 9505 |
'bla depot bla dépots' indexOfSubCollection:'dep' startingAt:1 ifAbsent:0 caseSensitive:false ignoreDiacritics:true. 5 |
9506 |
'bla depot bla dépots' indexOfSubCollection:'dep' startingAt:6 ifAbsent:0 caseSensitive:false ignoreDiacritics:true. 0 |
|
9507 |
||
9508 |
'bla depot bla dépots' indexOfSubCollection:'dep' startingAt:1 ifAbsent:0 caseSensitive:false ignoreDiacritics:false. 5 |
|
9509 |
'bla depot bla dépots' indexOfSubCollection:'dep' startingAt:6 ifAbsent:0 caseSensitive:false ignoreDiacritics:false. 0 |
|
17191 | 9510 |
" |
23001 | 9511 |
|
9512 |
"Modified: / 24-05-2018 / 14:55:43 / Claus Gittinger" |
|
17191 | 9513 |
! |
9514 |
||
18939 | 9515 |
lastIndexOfString:aString |
9516 |
"VSE and V'age compatibility" |
|
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
9517 |
"find the last occurrence of substring. |
18939 | 9518 |
If found, return the index; if not found, return 0." |
9519 |
||
9520 |
^ self lastIndexOfSubCollection:aString startingAt:(self size-aString size+1) ifAbsent:[0] |
|
9521 |
||
9522 |
" 123456789012 |
|
9523 |
'abcdefabcdef' lastIndexOfString:'abc' |
|
9524 |
'abcdefabcdef' lastIndexOfString:'abc' startingAt:6 |
|
9525 |
'abcdefabcdef' lastIndexOfString:'xxx' startingAt:6 |
|
9526 |
" |
|
9527 |
! |
|
9528 |
||
9529 |
lastIndexOfString:aString ifAbsent:exceptionValue |
|
9530 |
"VSE and V'age compatibility" |
|
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
9531 |
"find the last occurrence of substring. |
18939 | 9532 |
If found, return the index; if not found, return 0." |
9533 |
||
9534 |
^ self lastIndexOfSubCollection:aString startingAt:(self size-aString size+1) ifAbsent:exceptionValue |
|
9535 |
||
9536 |
" 123456789012 |
|
9537 |
'abcdefabcdef' lastIndexOfString:'abc' ifAbsent:[999] |
|
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
9538 |
'abcdefabcdef' lastIndexOfString:'xxx' ifAbsent:[999] |
18939 | 9539 |
" |
9540 |
! |
|
9541 |
||
9542 |
lastIndexOfString:aString startingAt:startIndex |
|
9543 |
"VSE and V'age compatibility" |
|
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
9544 |
"find the last occurrence of a substring. |
18939 | 9545 |
If found, return the index; if not found, return 0." |
9546 |
||
9547 |
^ self lastIndexOfSubCollection:aString startingAt:startIndex ifAbsent:[0] |
|
9548 |
||
9549 |
" 123456789012 |
|
9550 |
'abcdefabcdef' lastIndexOfString:'abc' |
|
9551 |
'abcdefabcdef' lastIndexOfString:'abc' startingAt:6 |
|
9552 |
" |
|
9553 |
! |
|
9554 |
||
9555 |
lastIndexOfString:aString startingAt:startIndex ifAbsent:exceptionValue |
|
9556 |
"VSE and V'age compatibility" |
|
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
9557 |
"find the last occurrence of a substring. |
18939 | 9558 |
If found, return the index; if not found, return 0." |
9559 |
||
9560 |
^ self lastIndexOfSubCollection:aString startingAt:startIndex ifAbsent:exceptionValue |
|
9561 |
||
9562 |
" 123456789012 |
|
9563 |
'abcdefabcdef' lastIndexOfString:'abc' |
|
9564 |
'abcdefabcdef' lastIndexOfString:'abc' startingAt:6 |
|
9565 |
'abcdefabcdef' lastIndexOfString:'xxx' startingAt:6 ifAbsent:999 |
|
9566 |
" |
|
9567 |
! |
|
9568 |
||
24145 | 9569 |
occurrencesOfString:aSubString |
9570 |
"count how often the argument aSubString is contained in the receiver" |
|
9571 |
||
9572 |
^ self occurrencesOfString:aSubString caseSensitive:true |
|
9573 |
||
9574 |
" |
|
9575 |
'aa' indexOfString:'aa' startingAt:1 |
|
9576 |
||
9577 |
'' occurrencesOfString:'aa' |
|
9578 |
'a' occurrencesOfString:'aa' |
|
9579 |
'aa' occurrencesOfString:'aa' |
|
9580 |
' aa ' occurrencesOfString:'aa' |
|
9581 |
' aa a' occurrencesOfString:'aa' |
|
9582 |
' aaaa' occurrencesOfString:'aa' |
|
9583 |
' aa aa ' occurrencesOfString:'aa' |
|
9584 |
' aa bb ab ba aa ab' occurrencesOfString:'aa' |
|
9585 |
' aa bb cc aa bb cc aa bb ' occurrencesOfString:'aa' |
|
9586 |
" |
|
9587 |
||
9588 |
"Created: / 25-05-2019 / 08:57:14 / Claus Gittinger" |
|
9589 |
! |
|
9590 |
||
9591 |
occurrencesOfString:aSubString caseSensitive:caseSensitive |
|
9592 |
"count how often the argument aSubString is contained in the receiver" |
|
9593 |
||
9594 |
|idx count| |
|
9595 |
||
9596 |
idx := 1. |
|
9597 |
count := 0. |
|
9598 |
[idx ~~ 0] whileTrue:[ |
|
9599 |
idx := self indexOfString:aSubString caseSensitive:caseSensitive startingAt:idx. |
|
9600 |
idx ~~ 0 ifTrue:[ |
|
9601 |
count := count + 1. |
|
9602 |
idx := idx + aSubString size |
|
9603 |
] |
|
9604 |
]. |
|
9605 |
^ count |
|
9606 |
||
9607 |
" |
|
9608 |
'aa' indexOfString:'aa' startingAt:1 |
|
9609 |
||
9610 |
'' occurrencesOfString:'aA' caseSensitive:false |
|
9611 |
'a' occurrencesOfString:'aA' caseSensitive:false |
|
9612 |
'aa' occurrencesOfString:'aA' caseSensitive:false |
|
9613 |
' aa ' occurrencesOfString:'aA' caseSensitive:false |
|
9614 |
' aa a' occurrencesOfString:'aA' caseSensitive:false |
|
9615 |
' aaaa' occurrencesOfString:'aA' caseSensitive:false |
|
9616 |
' aa aa ' occurrencesOfString:'aA' caseSensitive:false |
|
9617 |
' aa bb ab ba aa ab' occurrencesOfString:'aA' caseSensitive:false |
|
9618 |
' aa bb cc aa bb cc aa bb ' occurrencesOfString:'aA' caseSensitive:false |
|
9619 |
" |
|
9620 |
||
9621 |
"Created: / 25-05-2019 / 09:06:46 / Claus Gittinger" |
|
9622 |
! |
|
9623 |
||
15055 | 9624 |
rangeOfSubCollection:subString startingAt:start ifAbsent:exceptionValue caseSensitive:caseSensitive |
9625 |
"find a substring. if found, return the start- and endIndex; |
|
9626 |
if not found, return the value of exceptionValue." |
|
9627 |
||
9628 |
|i| |
|
9629 |
||
9630 |
i := self indexOfSubCollection:subString startingAt:start ifAbsent:0 caseSensitive:caseSensitive. |
|
9631 |
i == 0 ifTrue:[ |
|
20874 | 9632 |
^ exceptionValue value |
15055 | 9633 |
]. |
9634 |
^ i to:(i + subString size - 1) |
|
9635 |
||
9636 |
" |
|
9637 |
'hello world' findRangeOfString:'llo' |
|
9638 |
'hello world' findRangeOfString:'ole' |
|
9639 |
" |
|
9192 | 9640 |
! ! |
9641 |
||
9642 |
!CharacterArray methodsFor:'testing'! |
|
9643 |
||
18893 | 9644 |
isPlainString |
9645 |
"return true, if the receiver is a plain string (without attributes); |
|
9646 |
true is returned here - redefinition of Object>>isPlainString." |
|
9647 |
||
9648 |
^ true |
|
9649 |
! |
|
9650 |
||
9230 | 9651 |
isString |
9652 |
"return true, if the receiver is some kind of string; |
|
9653 |
true is returned here - redefinition of Object>>isString." |
|
9654 |
||
9655 |
^ true |
|
9656 |
! |
|
9657 |
||
21126 | 9658 |
isUnary |
9659 |
"Answer true if the receiver is a unary selector. |
|
9660 |
That is not a check for being a valid selector, but instead relies on |
|
9661 |
the selector being valid." |
|
9662 |
||
9663 |
^ self isUnarySelector |
|
9664 |
! |
|
9665 |
||
9230 | 9666 |
isUnarySelector |
9667 |
"Answer true if the receiver contains only chars in an ANSI unary method selector, false otherwise." |
|
9668 |
||
17442 | 9669 |
^ (self first isLetterOrUnderline) |
24254 | 9670 |
and:[ self conform: [ :chr | chr isLetterOrDigitOrUnderline ]] |
9845 | 9671 |
|
9672 |
"Modified: / 13-09-2006 / 11:35:15 / cg" |
|
24254 | 9673 |
"Modified: / 05-06-2019 / 17:05:59 / Claus Gittinger" |
9230 | 9674 |
! |
9675 |
||
14122 | 9676 |
isUnicode16String |
9677 |
^ false |
|
9678 |
! |
|
9679 |
||
9680 |
isUnicode32String |
|
19939 | 9681 |
"true if this is a 4-byte unicode string" |
9682 |
||
14122 | 9683 |
^ false |
9684 |
! |
|
9685 |
||
9686 |
isUnicodeString |
|
19938 | 9687 |
"true if this is a 2- or 4-byte unicode string |
9688 |
(i.e. not a single byte string). |
|
20194 | 9689 |
Notice, that the name is misleading: |
19938 | 9690 |
all strings are use unicode encoding" |
20194 | 9691 |
|
14122 | 9692 |
^ false |
9693 |
! |
|
9694 |
||
14555 | 9695 |
isWideString |
19580 | 9696 |
"true if I require more than one byte per character" |
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
9697 |
|
15829 | 9698 |
|string| |
9699 |
||
9700 |
(string := self string) ~~ self ifTrue:[ |
|
20874 | 9701 |
^ string isWideString. |
15829 | 9702 |
]. |
9703 |
^ self contains:[:aCharacter | aCharacter codePoint > 16rFF]. |
|
64 | 9704 |
! ! |
9705 |
||
9192 | 9706 |
!CharacterArray methodsFor:'tracing'! |
9707 |
||
9708 |
traceInto:aRequestor level:level from:referrer |
|
9709 |
"double dispatch into tracer, passing my type implicitely in the selector" |
|
9710 |
||
9711 |
^ aRequestor traceCharacterArray:self level:level from:referrer |
|
9712 |
||
9713 |
||
9714 |
! ! |
|
9715 |
||
9716 |
!CharacterArray methodsFor:'visiting'! |
|
8395
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8376
diff
changeset
|
9717 |
|
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8376
diff
changeset
|
9718 |
acceptVisitor:aVisitor with:aParameter |
16733 | 9719 |
"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
|
9720 |
|
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8376
diff
changeset
|
9721 |
^ aVisitor visitString:self with:aParameter |
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8376
diff
changeset
|
9722 |
! ! |
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8376
diff
changeset
|
9723 |
|
24250 | 9724 |
|
1849
9f35f5934ec6
added msdos-latin1 en/decoder (code = #msdos850)
Claus Gittinger <cg@exept.de>
parents:
1669
diff
changeset
|
9725 |
!CharacterArray class methodsFor:'documentation'! |
629 | 9726 |
|
9727 |
version |
|
18476 | 9728 |
^ '$Header$' |
12058 | 9729 |
! |
9730 |
||
9731 |
version_CVS |
|
18476 | 9732 |
^ '$Header$' |
629 | 9733 |
! ! |
6809 | 9734 |
|
14606 | 9735 |
|
2728
7569e45a4d15
raise error signal if anything goes wrong while decoding.
Claus Gittinger <cg@exept.de>
parents:
2721
diff
changeset
|
9736 |
CharacterArray initialize! |