author | Claus Gittinger <cg@exept.de> |
Thu, 27 Jun 2019 16:34:40 +0200 | |
changeset 24379 | 852be153b0ec |
parent 24300 | 9d2d154ca84b |
child 24407 | 37ddfbbe6d37 |
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 |
||
9192 | 1138 |
!CharacterArray methodsFor:'Compatibility-ANSI'! |
1139 |
||
1140 |
addLineDelimiters |
|
1141 |
"Ansi compatibility - same as withCRs" |
|
1142 |
||
1143 |
^ self withCRs |
|
1144 |
||
1145 |
"Modified: / 13.11.2001 / 19:16:25 / cg" |
|
17543 | 1146 |
! ! |
1147 |
||
1148 |
!CharacterArray methodsFor:'Compatibility-Dolphin'! |
|
9192 | 1149 |
|
1150 |
copyExpanding:expandTable |
|
1151 |
"return a copy of myself, with translations from the expandTable sliced in. |
|
18392 | 1152 |
The argument is supposed to map from characters to either characters or strings. |
1153 |
Added for Dolphin compatibility" |
|
9192 | 1154 |
|
1155 |
|ds| |
|
1156 |
||
1157 |
ds := WriteStream on:(self species new). |
|
1158 |
self do:[:eachChar | |
|
20874 | 1159 |
|repl| |
1160 |
||
1161 |
repl := expandTable at:eachChar ifAbsent:[nil]. |
|
1162 |
repl isNil ifTrue:[ |
|
1163 |
ds nextPut:eachChar |
|
1164 |
] ifFalse:[ |
|
1165 |
repl size == 0 ifTrue:[ |
|
1166 |
ds nextPut:repl |
|
1167 |
] ifFalse:[ |
|
1168 |
ds nextPutAll:repl |
|
1169 |
] |
|
1170 |
]. |
|
9192 | 1171 |
]. |
6870 | 1172 |
^ ds contents. |
24036 | 1173 |
|
1174 |
" |
|
1175 |
'hello' copyExpanding:(Dictionary withKeys:{$h . $e . $o} andValues:{'HH' . 'EE' . $O }) |
|
1176 |
" |
|
1177 |
||
1178 |
"Modified (format): / 02-04-2019 / 10:59:59 / Claus Gittinger" |
|
6870 | 1179 |
! |
1180 |
||
1181 |
formatWith:aString |
|
11955 | 1182 |
"Compatibility method - do not use in new code. |
18392 | 1183 |
same as #bindWith: for Dolphin compatibility" |
6870 | 1184 |
|
1185 |
^ self bindWith:aString |
|
1186 |
||
1187 |
" |
|
1188 |
'hello%1world' formatWith:'123' |
|
1189 |
" |
|
1190 |
! |
|
1191 |
||
1192 |
formatWith:arg1 with:arg2 |
|
11955 | 1193 |
"Compatibility method - do not use in new code. |
18392 | 1194 |
same as #bindWith: for Dolphin compatibility" |
6870 | 1195 |
|
1196 |
^ self bindWith:arg1 with:arg2 |
|
1197 |
||
1198 |
" |
|
1199 |
'hello%1 %2world' formatWith:'123' with:234 |
|
1200 |
" |
|
1201 |
! |
|
1202 |
||
1203 |
formatWith:arg1 with:arg2 with:arg3 |
|
11955 | 1204 |
"Compatibility method - do not use in new code. |
18392 | 1205 |
same as #bindWith: for Dolphin compatibility" |
6870 | 1206 |
|
1207 |
^ self bindWith:arg1 with:arg2 with:arg3 |
|
1208 |
||
1209 |
" |
|
1210 |
'hello%1 %2 %3world' formatWith:'123' with:234 with:345 |
|
1211 |
" |
|
6391 | 1212 |
! ! |
1213 |
||
14681 | 1214 |
!CharacterArray methodsFor:'Compatibility-GNU'! |
1215 |
||
1216 |
% anArrayOfOperands |
|
1217 |
"return a copy of the receiver, where a '%i' escape |
|
21747 | 1218 |
is replaced by the corresponding string from the argument array. |
14681 | 1219 |
'i' may be between 1 and 9 (i.e. a maximum of 9 placeholders is allowed). |
1220 |
Added for GNU-ST compatibility." |
|
1221 |
||
1222 |
^ self expandPlaceholdersWith:anArrayOfOperands |
|
1223 |
||
1224 |
" |
|
1225 |
'do you prefer %1 or rather %2 (not talking about %3) ?' |
|
20874 | 1226 |
% #('smalltalk' 'c++' 'c') |
14681 | 1227 |
|
1228 |
'do you %(what) ?' |
|
20874 | 1229 |
% (Dictionary new at:#'what' put:'understand'; yourself) |
14681 | 1230 |
" |
21747 | 1231 |
|
1232 |
"Modified (comment): / 11-05-2017 / 12:43:08 / mawalch" |
|
14681 | 1233 |
! ! |
1234 |
||
7261 | 1235 |
!CharacterArray methodsFor:'Compatibility-ST/V'! |
608 | 1236 |
|
17517 | 1237 |
asArrayOfSubstrings |
1238 |
"return an array of substrings from the receiver, interpreting |
|
1239 |
separators (i.e. spaces & newlines) as word-delimiters. |
|
18390 | 1240 |
This has been added for ST/V compatibility |
1241 |
- the actual work is done in asCollectionOfWords. |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
1242 |
(sigh: it is called #'subStrings' in V'Age, #'substrings' in Squeak |
17517 | 1243 |
and #'asCollectionOfWords' in ST/X) " |
1244 |
||
1245 |
^ self asCollectionOfWords asArray |
|
1246 |
||
1247 |
" |
|
1248 |
'1 one two three four 5 five' asArrayOfSubstrings |
|
1249 |
'1 |
|
1250 |
one |
|
20874 | 1251 |
two three four 5 five' asArrayOfSubstrings |
17517 | 1252 |
" |
1253 |
! |
|
1254 |
||
19497 | 1255 |
equalsIgnoreCase:aString |
1256 |
"This is an ST/V compatibility method and an alias for sameAs:." |
|
1257 |
||
1258 |
^ self sameAs:aString |
|
1259 |
||
1260 |
" |
|
1261 |
'abc' equalsIgnoreCase: 'aBC' |
|
1262 |
" |
|
1263 |
! |
|
1264 |
||
608 | 1265 |
replChar:oldChar with:newChar |
19497 | 1266 |
"return a copy of the receiver, with all oldChars replaced by newChar. |
1267 |
This is an ST/V compatibility method and an alias for copyReplaceAll." |
|
608 | 1268 |
|
3656 | 1269 |
^ self copyReplaceAll:oldChar with:newChar |
608 | 1270 |
|
1271 |
" |
|
8874 | 1272 |
'12345678901234567890' replChar:$0 with:$* |
608 | 1273 |
" |
3656 | 1274 |
|
1275 |
"Modified: / 18.7.1998 / 22:52:57 / cg" |
|
608 | 1276 |
! |
1277 |
||
1278 |
replChar:oldChar withString:newString |
|
1279 |
"return a copy of the receiver, with all oldChars replaced |
|
1280 |
by newString (i.e. slice in the newString in place of the oldChar). |
|
1281 |
This is an ST/V compatibility method." |
|
1282 |
||
1283 |
|tmpStream| |
|
1284 |
||
15523 | 1285 |
tmpStream := self species writeStream. |
608 | 1286 |
self do:[:element | |
20874 | 1287 |
element = oldChar ifTrue:[ |
1288 |
tmpStream nextPutAll:newString |
|
1289 |
] ifFalse:[ |
|
1290 |
tmpStream nextPut:element |
|
1291 |
]. |
|
608 | 1292 |
]. |
1293 |
^ tmpStream contents |
|
1294 |
||
1295 |
" |
|
8874 | 1296 |
'12345678901234567890' replChar:$0 withString:'foo' |
1297 |
'a string with spaces' replChar:$ withString:' foo ' |
|
608 | 1298 |
" |
9192 | 1299 |
! |
1300 |
||
1301 |
replString:subString withString:newString |
|
4244
5deefa3144fb
added #replString:withString:
Claus Gittinger <cg@exept.de>
parents:
4156
diff
changeset
|
1302 |
"return a copy of the receiver, with all sequences of subString replaced |
18390 | 1303 |
by newString (i.e. slice in the newString in place of the oldString). |
19497 | 1304 |
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
|
1305 |
|
8347 | 1306 |
^ self copyReplaceString:subString withString:newString |
4244
5deefa3144fb
added #replString:withString:
Claus Gittinger <cg@exept.de>
parents:
4156
diff
changeset
|
1307 |
|
5deefa3144fb
added #replString:withString:
Claus Gittinger <cg@exept.de>
parents:
4156
diff
changeset
|
1308 |
" |
8874 | 1309 |
'12345678901234567890' replString:'123' withString:'OneTwoThree' |
1310 |
'12345678901234567890' replString:'123' withString:'*' |
|
1311 |
'12345678901234567890' replString:'234' withString:'foo' |
|
4244
5deefa3144fb
added #replString:withString:
Claus Gittinger <cg@exept.de>
parents:
4156
diff
changeset
|
1312 |
|
5deefa3144fb
added #replString:withString:
Claus Gittinger <cg@exept.de>
parents:
4156
diff
changeset
|
1313 |
('a string with spaces' replChar:$ withString:' foo ') |
20874 | 1314 |
replString:'foo' withString:'bar' |
4244
5deefa3144fb
added #replString:withString:
Claus Gittinger <cg@exept.de>
parents:
4156
diff
changeset
|
1315 |
" |
5deefa3144fb
added #replString:withString:
Claus Gittinger <cg@exept.de>
parents:
4156
diff
changeset
|
1316 |
|
8347 | 1317 |
"Modified: / 12-05-2004 / 12:00:27 / cg" |
9192 | 1318 |
! |
1319 |
||
9275 | 1320 |
subString:start to:end |
18390 | 1321 |
"same as copyFrom:to: |
19497 | 1322 |
This is an ST/V compatibility method and an alias for copyFrom:to:." |
18390 | 1323 |
|
9275 | 1324 |
^ self copyFrom:start to:end |
1325 |
||
1326 |
" |
|
1327 |
'12345678901234567890' subString:3 to:8 |
|
1328 |
" |
|
1329 |
! |
|
1330 |
||
9192 | 1331 |
trimBlanks |
14992 | 1332 |
"return a copy of the receiver without leading and trailing spaces. |
18464 | 1333 |
This is an ST/V compatibility method and an alias for withoutSpaces." |
9192 | 1334 |
|
1335 |
^ self withoutSpaces |
|
1336 |
||
1337 |
" |
|
1338 |
' spaces at beginning' trimBlanks |
|
1339 |
'spaces at end ' trimBlanks |
|
1340 |
' spaces at beginning and end ' trimBlanks |
|
1341 |
'no spaces' trimBlanks |
|
1342 |
" |
|
1343 |
! ! |
|
1344 |
||
1345 |
!CharacterArray methodsFor:'Compatibility-Squeak'! |
|
4409
86e049b21f8d
added #includesSubstring:caseSensitive:
Claus Gittinger <cg@exept.de>
parents:
4389
diff
changeset
|
1346 |
|
19171 | 1347 |
asBoldText |
1348 |
"return self as a bold text" |
|
1349 |
||
21843 | 1350 |
"this test allows for small non-gui apps to be built without libbasic2 (where Text is)" |
1351 |
Text isNil ifTrue:[^ self]. |
|
1352 |
^ Text string: self emphasis: #bold |
|
1353 |
||
1354 |
"Modified (comment): / 20-06-2017 / 08:23:49 / cg" |
|
19171 | 1355 |
! |
1356 |
||
6148 | 1357 |
asDate |
1358 |
"Many allowed forms, see Date.readFrom:" |
|
1359 |
||
1360 |
^ Date readFrom: (ReadStream on: self) |
|
1361 |
||
1362 |
" |
|
8874 | 1363 |
'30 Apr 1999' asDate dayName capitalized |
6148 | 1364 |
" |
1365 |
! |
|
1366 |
||
10703
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
1367 |
findDelimiters:delimiters startingAt:start |
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
1368 |
"Answer the index of the character within the receiver, starting at start, |
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
1369 |
that matches one of the delimiters. |
18391 | 1370 |
If the receiver does not contain any of the delimiters, answer size + 1." |
9192 | 1371 |
|
1372 |
|idx| |
|
1373 |
||
1374 |
idx := self indexOfAny:delimiters startingAt:start. |
|
1375 |
idx == 0 ifTrue:[^ self size + 1]. |
|
1376 |
^ idx. |
|
1377 |
"/start to: self size do: [:i | |
|
1378 |
"/ |char| |
|
1379 |
"/ |
|
1380 |
"/ char := self at: i. |
|
1381 |
"/ delimiters do: [:delim | delim = char ifTrue: [^ i]] |
|
8971 | 1382 |
"/ ]. |
1383 |
"/ ^ self size + 1 |
|
1384 |
! |
|
1385 |
||
22005 | 1386 |
findString:key startingAt:start caseSensitive:caseSensitive |
1387 |
^ self indexOfSubCollection:key startingAt:start ifAbsent:[0] caseSensitive:caseSensitive |
|
1388 |
||
1389 |
"Created: / 13-07-2017 / 12:44:50 / cg" |
|
1390 |
! |
|
1391 |
||
13624 | 1392 |
findTokens:delimiterOrDelimiters |
7270 | 1393 |
"cg: I am not sure, if this is really the squeak semantics (w.r.t. empty fields)" |
1394 |
||
13624 | 1395 |
delimiterOrDelimiters size == 0 ifTrue:[ |
20874 | 1396 |
^ self asCollectionOfSubstringsSeparatedBy:delimiterOrDelimiters |
13624 | 1397 |
] ifFalse:[ |
20874 | 1398 |
^ self asCollectionOfSubstringsSeparatedByAny:delimiterOrDelimiters |
13624 | 1399 |
]. |
7270 | 1400 |
|
1401 |
" |
|
8874 | 1402 |
'a|b#c||e' findTokens:#($# $|) |
13624 | 1403 |
'a|b#c||e' findTokens:$| |
1404 |
" |
|
1405 |
||
1406 |
"Modified: / 04-09-2011 / 20:31:21 / cg" |
|
1407 |
"Modified (comment): / 05-09-2011 / 04:25:28 / cg" |
|
4981 | 1408 |
! |
1409 |
||
4974 | 1410 |
includesSubString:aString |
1411 |
"return true, if a substring is contained in the receiver. |
|
1412 |
The compare is case sensitive." |
|
1413 |
||
1414 |
^ self includesString:aString |
|
1415 |
||
1416 |
" |
|
8874 | 1417 |
'hello world' includesSubString:'Hel' |
9192 | 1418 |
'hello world' includesSubString:'hel' |
1419 |
'hello world' includesSubString:'llo' |
|
1420 |
" |
|
1421 |
||
1422 |
||
1423 |
||
1424 |
! |
|
1425 |
||
11178 | 1426 |
includesSubString:aString caseSensitive:caseSensitive |
18391 | 1427 |
"sigh - an alias; added for Squeak/Pharo compatibility" |
1428 |
||
11179 | 1429 |
^ self includesSubstring:aString caseSensitive:caseSensitive |
11178 | 1430 |
! |
1431 |
||
16875
32e61bb3a742
class: CharacterArray>>#includesSubstring: (for Squeak compatibility)
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
16863
diff
changeset
|
1432 |
includesSubstring: aString |
18391 | 1433 |
"sigh - an alias; added for Squeak/Pharo compatibility" |
1434 |
||
24085 | 1435 |
^ self includesString: aString |
16875
32e61bb3a742
class: CharacterArray>>#includesSubstring: (for Squeak compatibility)
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
16863
diff
changeset
|
1436 |
|
32e61bb3a742
class: CharacterArray>>#includesSubstring: (for Squeak compatibility)
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
16863
diff
changeset
|
1437 |
"Created: / 03-10-2014 / 02:47:52 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
24085 | 1438 |
"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
|
1439 |
! |
32e61bb3a742
class: CharacterArray>>#includesSubstring: (for Squeak compatibility)
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
16863
diff
changeset
|
1440 |
|
9192 | 1441 |
includesSubstring:aString caseSensitive:caseSensitive |
9190 | 1442 |
"return true, if a substring is contained in the receiver. |
9192 | 1443 |
The argument, caseSensitive controls if case is ignored in the compare." |
1444 |
||
24085 | 1445 |
^ (self indexOfSubCollection:aString startingAt:1 ifAbsent:0 caseSensitive:caseSensitive) ~~ 0 |
9190 | 1446 |
|
1447 |
" |
|
9192 | 1448 |
'hello world' includesSubstring:'Hel' caseSensitive:true |
1449 |
'hello world' includesSubstring:'Hel' caseSensitive:false |
|
1450 |
" |
|
1451 |
||
24085 | 1452 |
"Modified (comment): / 16-04-2019 / 12:35:33 / Stefan Vogel" |
9192 | 1453 |
! |
9190 | 1454 |
|
6979 | 1455 |
isAllDigits |
1456 |
"Answer whether the receiver's characters are all digits" |
|
1457 |
||
1458 |
^ self conform:[:eachChar | eachChar isDigit] |
|
1459 |
||
1460 |
" |
|
1461 |
'hello world' isAllDigits |
|
1462 |
'12344' isAllDigits |
|
1463 |
" |
|
1464 |
! |
|
1465 |
||
4890 | 1466 |
lastSpacePosition |
18391 | 1467 |
"return the index of the last space character; 0 if there is none. |
1468 |
Added for Squeak/Pharo compatibility" |
|
14405
1d0d99a95bba
comment/format in:7 methods
Claus Gittinger <cg@exept.de>
parents:
14168
diff
changeset
|
1469 |
|
20739 | 1470 |
"/ CG: is this correct? separator or space only??? |
4890 | 1471 |
^ self lastIndexOfSeparator |
1472 |
! |
|
1473 |
||
20738 | 1474 |
linesDo:aBlock |
1475 |
"evaluate the argument, aBlock for all lines, |
|
1476 |
up to the end" |
|
1477 |
||
1478 |
^ self readStream linesDo:aBlock |
|
1479 |
! |
|
1480 |
||
7022 | 1481 |
padded:leftOrRight to:paddedSize with:padCharacter |
20155 | 1482 |
"pad left (leftOrRight == #left) or right" |
14053
acbb97913c30
comment/format in: #padded:to:with:
Claus Gittinger <cg@exept.de>
parents:
14050
diff
changeset
|
1483 |
|
7022 | 1484 |
leftOrRight == #left ifTrue:[ |
20155 | 1485 |
^ self leftPaddedTo:paddedSize with:padCharacter |
9190 | 1486 |
]. |
1487 |
^ self paddedTo:paddedSize with:padCharacter |
|
1488 |
||
1489 |
" |
|
8874 | 1490 |
'hello' padded:#right to:10 with:$. |
9192 | 1491 |
'hello' padded:#left to:10 with:$. |
1492 |
" |
|
14053
acbb97913c30
comment/format in: #padded:to:with:
Claus Gittinger <cg@exept.de>
parents:
14050
diff
changeset
|
1493 |
|
acbb97913c30
comment/format in: #padded:to:with:
Claus Gittinger <cg@exept.de>
parents:
14050
diff
changeset
|
1494 |
"Modified (comment): / 07-03-2012 / 16:31:33 / cg" |
9192 | 1495 |
! |
1496 |
||
1497 |
skipDelimiters:delimiters startingAt:start |
|
1498 |
"Answer the index of the character within the receiver, starting at start, |
|
1499 |
that does NOT match one of the delimiters. |
|
1500 |
If the receiver does not contain any of the delimiters, answer size + 1. |
|
1501 |
Assumes the delimiters to be a non-empty string." |
|
1502 |
||
1503 |
start to:self size do:[:i | |
|
20874 | 1504 |
delimiters detect:[:delim | delim = (self at:i) ] ifNone:[ ^ i ] |
9192 | 1505 |
]. |
1506 |
^ self size + 1 |
|
1507 |
||
1508 |
" |
|
1509 |
'123***7890' skipDelimiters:'*' startingAt:4 |
|
1510 |
'123***7890' skipDelimiters:'*' startingAt:3 |
|
9190 | 1511 |
'123***7890' skipDelimiters:'*' startingAt:10 |
1512 |
'123*******' skipDelimiters:'*' startingAt:10 |
|
8874 | 1513 |
" |
1514 |
! |
|
1515 |
||
9190 | 1516 |
substrings |
18921 | 1517 |
"return a collection consisting of all words contained in the receiver. |
17517 | 1518 |
Words are separated by whitespace. |
1519 |
This has been added for Squeak compatibility. |
|
1520 |
(sigh: it is called #'subStrings' in V'Age, and #'asCollectionOfWords' in ST/X) " |
|
1521 |
||
9190 | 1522 |
^ self asCollectionOfWords |
1523 |
||
1524 |
" |
|
1525 |
'foo bar baz' substrings |
|
1526 |
" |
|
1527 |
! |
|
1528 |
||
12665
b027fc5aec4e
added: #substringsSeparatedBy:
Claus Gittinger <cg@exept.de>
parents:
12664
diff
changeset
|
1529 |
substringsSeparatedBy:separatorCharacter |
18921 | 1530 |
"return a collection consisting of all words contained in the receiver. |
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
1531 |
Words are separated by the given separator character. |
18921 | 1532 |
This has been added for Squeak/Pharo compatibility. |
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
1533 |
(sigh: it is called #'subStrings:' in V'Age, |
18921 | 1534 |
and #'asCollectionOfSubstringsSeparatedBy' in ST/X) " |
18391 | 1535 |
|
12665
b027fc5aec4e
added: #substringsSeparatedBy:
Claus Gittinger <cg@exept.de>
parents:
12664
diff
changeset
|
1536 |
^ self asCollectionOfSubstringsSeparatedBy:separatorCharacter |
b027fc5aec4e
added: #substringsSeparatedBy:
Claus Gittinger <cg@exept.de>
parents:
12664
diff
changeset
|
1537 |
|
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 |
'foo bar, baz' substringsSeparatedBy:$, |
21151 | 1540 |
'1.2.3.4' substringsSeparatedBy:$. |
12665
b027fc5aec4e
added: #substringsSeparatedBy:
Claus Gittinger <cg@exept.de>
parents:
12664
diff
changeset
|
1541 |
" |
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 |
|
23188 | 1544 |
trimBoth |
1545 |
"return a copy of the receiver without leading and trailing whiteSpace. |
|
1546 |
Added for Squeak compatibility (an alias for withoutSeparators)" |
|
1547 |
||
1548 |
^ self withoutSeparators |
|
1549 |
||
1550 |
"Created: / 03-07-2018 / 11:08:29 / Claus Gittinger" |
|
1551 |
! |
|
1552 |
||
9190 | 1553 |
truncateTo:smallSize |
9192 | 1554 |
"return myself or a copy shortened to smallSize. 1/18/96 sw" |
1555 |
||
1556 |
self size <= smallSize ifTrue:[^ self]. |
|
1557 |
^ self copyFrom: 1 to: smallSize |
|
1558 |
||
1559 |
" |
|
1560 |
'hello world' truncateTo:5 |
|
1561 |
'hello' truncateTo:10 |
|
1562 |
||
1563 |
'hello world' copyTo:5 |
|
1564 |
'hello' copyTo:10 |
|
1565 |
" |
|
1566 |
! |
|
5795 | 1567 |
|
4890 | 1568 |
withBlanksTrimmed |
18464 | 1569 |
"Return a copy of the receiver from which leading and trailing whitespace have been trimmed. |
1570 |
Notice the bad naming - it is trimming separators, not just blanks. |
|
1571 |
Added for Squeak compatibility and an alias for withoutSeparators" |
|
4890 | 1572 |
|
14865 | 1573 |
^ self withoutSeparators "withoutSpaces" "/ cg: it seems that squeak cares for any whitespace |
4890 | 1574 |
|
1575 |
" |
|
8874 | 1576 |
' hello world ' withBlanksTrimmed |
4890 | 1577 |
" |
1578 |
! |
|
1579 |
||
12188 | 1580 |
withoutLeading: char |
1581 |
"return a copy of myself without leading a char. |
|
1582 |
Returns an empty string, if the receiver consist only of a char." |
|
1583 |
||
1584 |
^ self withoutLeadingForWhich:[:ch | ch = char] |
|
1585 |
||
1586 |
" |
|
13882
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
1587 |
'****foo****' withoutLeading: $* |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
1588 |
'foo****' withoutLeading: $* |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
1589 |
'*' 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 |
'****foo' withoutLeading: $* |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
1592 |
'*******' withoutLeading: $* |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
1593 |
'foo' withoutLeading: $* |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
1594 |
'f***o***o' withoutLeading: $* |
12188 | 1595 |
('**' , Character tab asString , '*foo***') withoutLeading: $* inspect |
1596 |
" |
|
1597 |
! |
|
1598 |
||
11565 | 1599 |
withoutTrailing:char |
1600 |
"return a copy of myself without trailing char. |
|
1601 |
Returns an empty string, if the receiver consist only of char." |
|
1602 |
||
12188 | 1603 |
^ self withoutTrailingForWhich:[:ch | ch = char] |
1604 |
||
1605 |
" |
|
13882
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
1606 |
' foo....' withoutTrailing:$. |
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 |
'.......' withoutTrailing:$. |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
1610 |
'foo' withoutTrailing:$. |
11565 | 1611 |
" |
9190 | 1612 |
! ! |
1613 |
||
1614 |
!CharacterArray methodsFor:'Compatibility-V''Age'! |
|
1615 |
||
1616 |
addLineDelimiter |
|
1617 |
"replace all '\'-characters by line delimiter (cr) - characters. |
|
9192 | 1618 |
This has been added for VisualAge compatibility." |
1619 |
||
1620 |
^ self withCRs |
|
1621 |
! |
|
1622 |
||
1623 |
bindWith:aString |
|
1624 |
"return a copy of the receiver, where a '%1' escape is |
|
1625 |
replaced by aString. |
|
1626 |
This has been added for VisualAge compatibility." |
|
1627 |
||
24038 | 1628 |
^ self expandPlaceholdersWith:{ aString } |
9192 | 1629 |
|
1630 |
" |
|
1631 |
'do you like %1 ?' bindWith:'smalltalk' |
|
19785 | 1632 |
'do you like %(foo) ?' bindWithArguments:(Dictionary new at:'foo' put:'smalltalk'; yourself) |
9192 | 1633 |
" |
24038 | 1634 |
|
1635 |
"Modified (format): / 02-04-2019 / 14:05:22 / Claus Gittinger" |
|
9192 | 1636 |
! |
608 | 1637 |
|
1638 |
bindWith:string1 with:string2 |
|
1639 |
"return a copy of the receiver, where a '%1' escape is |
|
1640 |
replaced by string1 and '%2' is replaced by string2. |
|
1641 |
This has been added for VisualAge compatibility." |
|
1642 |
||
24038 | 1643 |
^ self expandPlaceholdersWith:{string1 . string2} |
608 | 1644 |
|
1645 |
" |
|
1646 |
'do you prefer %1 or rather %2 ?' |
|
20874 | 1647 |
bindWith:'smalltalk' with:'c++' |
9192 | 1648 |
" |
24038 | 1649 |
|
1650 |
"Modified (format): / 02-04-2019 / 14:05:32 / Claus Gittinger" |
|
9192 | 1651 |
! |
9190 | 1652 |
|
1653 |
bindWith:str1 with:str2 with:str3 |
|
608 | 1654 |
"return a copy of the receiver, where a '%1', '%2' and '%3' escapes |
1655 |
are replaced by str1, str2 and str3 respectively. |
|
1656 |
This has been added for VisualAge compatibility." |
|
1657 |
||
24038 | 1658 |
^ self expandPlaceholdersWith:{ str1 . str2 . str3 } |
608 | 1659 |
|
1660 |
" |
|
9192 | 1661 |
'do you prefer %1 or rather %2 (not talking about %3) ?' |
20874 | 1662 |
bindWith:'smalltalk' with:'c++' with:'c' |
9192 | 1663 |
" |
24038 | 1664 |
|
1665 |
"Modified (format): / 02-04-2019 / 14:05:45 / Claus Gittinger" |
|
9192 | 1666 |
! |
608 | 1667 |
|
1668 |
bindWith:str1 with:str2 with:str3 with:str4 |
|
1669 |
"return a copy of the receiver, where a '%1', '%2', '%3' and '%4' escapes |
|
1670 |
are replaced by str1, str2, str3 and str4 respectively. |
|
1671 |
This has been added for VisualAge compatibility." |
|
1672 |
||
24038 | 1673 |
^ self expandPlaceholdersWith:{ str1 . str2 . str3 . str4 } |
608 | 1674 |
|
1675 |
" |
|
9192 | 1676 |
'do you prefer %1 or rather %2 (not talking about %3 or even %4) ?' |
20874 | 1677 |
bindWith:'smalltalk' with:'c++' with:'c' with:'assembler' |
9192 | 1678 |
" |
24038 | 1679 |
|
1680 |
"Modified (format): / 02-04-2019 / 14:06:01 / Claus Gittinger" |
|
9192 | 1681 |
! |
608 | 1682 |
|
2344 | 1683 |
bindWith:str1 with:str2 with:str3 with:str4 with:str5 |
1684 |
"return a copy of the receiver, where a '%1' .. '%5' escapes |
|
1685 |
are replaced by str1 .. str5 respectively. |
|
1686 |
This has been added for VisualAge compatibility." |
|
1687 |
||
24038 | 1688 |
^ self expandPlaceholdersWith:{ str1 . str2 . str3 . str4 . str5 } |
1689 |
||
1690 |
"Created: / 31-01-1997 / 16:25:42 / cg" |
|
1691 |
"Modified: / 02-04-2019 / 14:06:17 / Claus Gittinger" |
|
2344 | 1692 |
! |
1693 |
||
5343 | 1694 |
bindWith:str1 with:str2 with:str3 with:str4 with:str5 with:str6 |
1695 |
"return a copy of the receiver, where a '%1' .. '%6' escapes |
|
11524 | 1696 |
are replaced by str1 .. str6 respectively. |
5343 | 1697 |
This has been added for VisualAge compatibility." |
1698 |
||
24038 | 1699 |
^ self expandPlaceholdersWith:{ str1 . str2 . str3 . str4 . str5 . str6 } |
1700 |
||
1701 |
"Modified (format): / 02-04-2019 / 14:06:36 / Claus Gittinger" |
|
11524 | 1702 |
! |
1703 |
||
1704 |
bindWith:str1 with:str2 with:str3 with:str4 with:str5 with:str6 with:str7 |
|
1705 |
"return a copy of the receiver, where a '%1' .. '%7' escapes |
|
1706 |
are replaced by str1 .. str7 respectively. |
|
1707 |
This has been added for VisualAge compatibility." |
|
1708 |
||
24038 | 1709 |
^ self expandPlaceholdersWith:{ str1 . str2 . str3 . str4 . str5 . str6 . str7 } |
1710 |
||
1711 |
"Modified (format): / 02-04-2019 / 14:06:59 / Claus Gittinger" |
|
5343 | 1712 |
! |
1713 |
||
13996
a86faa4f17b8
added: #bindWith:with:with:with:with:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
13987
diff
changeset
|
1714 |
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
|
1715 |
"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
|
1716 |
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
|
1717 |
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
|
1718 |
|
24038 | 1719 |
^ 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
|
1720 |
|
a86faa4f17b8
added: #bindWith:with:with:with:with:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
13987
diff
changeset
|
1721 |
"Created: / 06-02-2012 / 10:33:18 / cg" |
24038 | 1722 |
"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
|
1723 |
! |
a86faa4f17b8
added: #bindWith:with:with:with:with:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
13987
diff
changeset
|
1724 |
|
14014
3aff2d7c41c8
added: #bindWith:with:with:with:with:with:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
13996
diff
changeset
|
1725 |
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
|
1726 |
"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
|
1727 |
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
|
1728 |
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
|
1729 |
|
24038 | 1730 |
^ 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
|
1731 |
|
3aff2d7c41c8
added: #bindWith:with:with:with:with:with:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
13996
diff
changeset
|
1732 |
"Created: / 14-02-2012 / 17:42:31 / cg" |
24038 | 1733 |
"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
|
1734 |
! |
3aff2d7c41c8
added: #bindWith:with:with:with:with:with:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
13996
diff
changeset
|
1735 |
|
22924 | 1736 |
bindWithArguments:argumentsCollection |
608 | 1737 |
"return a copy of the receiver, where a '%i' escape |
21747 | 1738 |
is replaced by the corresponding string from the argument array. |
22924 | 1739 |
'i' may be between 1 and 9 (i.e. a maximum of 9 placeholders is allowed) |
1740 |
or %(key); the argumentsCollection must then be a dictionary. |
|
21255 | 1741 |
To get an integer-indexed placeHolder followed by another digit, |
1742 |
or an index > 9, you must use %(digit). |
|
608 | 1743 |
This has been added for VisualAge compatibility." |
1744 |
||
22924 | 1745 |
^ self expandPlaceholdersWith:argumentsCollection |
9192 | 1746 |
|
1747 |
" |
|
1748 |
'do you prefer %1 or rather %2 (not talking about %3) ?' |
|
20874 | 1749 |
bindWithArguments:#('smalltalk' 'c++' 'c') |
12196 | 1750 |
|
1751 |
'do you %(what) ?' |
|
20874 | 1752 |
bindWithArguments:(Dictionary new at:#'what' put:'understand'; yourself) |
9192 | 1753 |
" |
21747 | 1754 |
|
1755 |
"Modified (comment): / 11-05-2017 / 12:42:57 / mawalch" |
|
9192 | 1756 |
! |
608 | 1757 |
|
1758 |
subStrings |
|
18921 | 1759 |
"return a collection consisting of all words contained in the receiver. |
608 | 1760 |
Words are separated by whitespace. |
17517 | 1761 |
This has been added for VisualAge compatibility. |
1762 |
(sigh: it is called #'subbtrings' in Squeak, and #'asCollectionOfWords' in ST/X) " |
|
608 | 1763 |
|
1764 |
^ self asCollectionOfWords |
|
1765 |
||
1766 |
" |
|
1767 |
'hello world, this is smalltalk' subStrings |
|
1768 |
" |
|
1769 |
! |
|
1770 |
||
16181 | 1771 |
subStrings:separatorCharacterOrString |
18921 | 1772 |
"return a collection consisting of all words contained in the receiver. |
608 | 1773 |
Words are separated by separatorCharacter. |
18921 | 1774 |
This is similar to split: (squeak) and asCollectionOfSubstringsSeparatedBy: (st/x) |
1775 |
and has been added for VisualAge compatibility." |
|
608 | 1776 |
|
16181 | 1777 |
separatorCharacterOrString isCharacter ifTrue:[ |
20874 | 1778 |
^ self asCollectionOfSubstringsSeparatedBy:separatorCharacterOrString |
16181 | 1779 |
]. |
1780 |
^ self asCollectionOfSubstringsSeparatedByAny:separatorCharacterOrString |
|
1781 |
||
1782 |
" |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
1783 |
'foo:bar:baz:smalltalk' subStrings:$: |
16181 | 1784 |
'foo:bar:baz:smalltalk' subStrings:':' |
1785 |
'foo.bar,baz-smalltalk' subStrings:'.,-' |
|
9192 | 1786 |
" |
1787 |
! |
|
1788 |
||
1789 |
trimSeparators |
|
18392 | 1790 |
"return a copy of the receiver without leading and trailing whiteSpace. |
18921 | 1791 |
Added for VisualAge compatibility (an alias for withoutSeparators)" |
9192 | 1792 |
|
1793 |
^ self withoutSeparators |
|
1794 |
! ! |
|
1795 |
||
1796 |
!CharacterArray methodsFor:'Compatibility-VW'! |
|
5964 | 1797 |
|
6139 | 1798 |
asLogicalFileSpecification |
1799 |
^ self asFilename |
|
1800 |
||
1801 |
"Created: / 30.10.2001 / 17:29:53 / cg" |
|
1802 |
! |
|
1803 |
||
5964 | 1804 |
expandMacros |
1805 |
"ST80 compatibility - expand '<..>' macros with |
|
1806 |
argument strings. Similar to #bindWith:. |
|
1807 |
Read the comment in #expandMacrosWithArguments: about |
|
1808 |
limited compatibility issues." |
|
1809 |
||
1810 |
^ self expandMacrosWithArguments:#() |
|
1811 |
||
1812 |
" |
|
1813 |
'hello<n>foo' expandMacros |
|
1814 |
" |
|
1815 |
||
9192 | 1816 |
"Modified: / 18.6.1998 / 16:03:02 / cg" |
1817 |
! |
|
1818 |
||
1819 |
expandMacrosWith:arg |
|
5964 | 1820 |
"ST80 compatibility - expand '<..>' macros with |
1821 |
argument strings. Similar to #bindWith:. |
|
1822 |
Read the comment in #expandMacrosWithArguments: about |
|
1823 |
limited compatibility issues." |
|
1824 |
||
24038 | 1825 |
^ self expandMacrosWithArguments:{ arg } |
1826 |
||
1827 |
"Created: / 01-11-1997 / 13:01:28 / cg" |
|
1828 |
"Modified: / 02-04-2019 / 14:09:26 / Claus Gittinger" |
|
9192 | 1829 |
! |
5964 | 1830 |
|
1831 |
expandMacrosWith:arg1 with:arg2 |
|
1832 |
"ST80 compatibility - expand '<..>' macros with |
|
1833 |
argument strings. Similar to #bindWith:. |
|
1834 |
Read the comment in #expandMacrosWithArguments: about |
|
1835 |
limited compatibility issues." |
|
1836 |
||
24038 | 1837 |
^ self expandMacrosWithArguments:{arg1 . arg2 } |
1838 |
||
1839 |
"Modified: / 06-07-1998 / 21:58:14 / cg" |
|
1840 |
"Modified: / 02-04-2019 / 14:09:36 / Claus Gittinger" |
|
5964 | 1841 |
! |
1842 |
||
1843 |
expandMacrosWith:arg1 with:arg2 with:arg3 |
|
1844 |
"ST80 compatibility - expand '<..>' macros with |
|
1845 |
argument strings. Similar to #bindWith:. |
|
1846 |
Read the comment in #expandMacrosWithArguments: about |
|
1847 |
limited compatibility issues." |
|
1848 |
||
24038 | 1849 |
^ self expandMacrosWithArguments:{ arg1 . arg2 . arg3 } |
1850 |
||
1851 |
"Modified (format): / 02-04-2019 / 14:09:48 / Claus Gittinger" |
|
5964 | 1852 |
! |
1853 |
||
10353
65fb7aa2c6c2
added #expandMacrosWith:with:with:with:
Stefan Vogel <sv@exept.de>
parents:
10134
diff
changeset
|
1854 |
expandMacrosWith:arg1 with:arg2 with:arg3 with:arg4 |
65fb7aa2c6c2
added #expandMacrosWith:with:with:with:
Stefan Vogel <sv@exept.de>
parents:
10134
diff
changeset
|
1855 |
"ST80 compatibility - expand '<..>' macros with |
65fb7aa2c6c2
added #expandMacrosWith:with:with:with:
Stefan Vogel <sv@exept.de>
parents:
10134
diff
changeset
|
1856 |
argument strings. Similar to #bindWith:. |
65fb7aa2c6c2
added #expandMacrosWith:with:with:with:
Stefan Vogel <sv@exept.de>
parents:
10134
diff
changeset
|
1857 |
Read the comment in #expandMacrosWithArguments: about |
65fb7aa2c6c2
added #expandMacrosWith:with:with:with:
Stefan Vogel <sv@exept.de>
parents:
10134
diff
changeset
|
1858 |
limited compatibility issues." |
65fb7aa2c6c2
added #expandMacrosWith:with:with:with:
Stefan Vogel <sv@exept.de>
parents:
10134
diff
changeset
|
1859 |
|
24038 | 1860 |
^ self expandMacrosWithArguments:{ arg1 . arg2 . arg3 . arg4 } |
1861 |
||
1862 |
"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
|
1863 |
! |
65fb7aa2c6c2
added #expandMacrosWith:with:with:with:
Stefan Vogel <sv@exept.de>
parents:
10134
diff
changeset
|
1864 |
|
9192 | 1865 |
expandMacrosWithArguments:argArray |
5964 | 1866 |
"ST80 compatibility - expand '<..>' macros with |
1867 |
argument strings. Similar to #bindWith:. |
|
1868 |
WARNING: possibly not all ST80 expansions are supported here." |
|
1869 |
||
1870 |
"/ supported expansions: |
|
1871 |
"/ |
|
1872 |
"/ <#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
|
1873 |
"/ <#s> # is arg Number; slice in the arg itself (must know asString) |
5964 | 1874 |
"/ <#?s1:s2> # is arg Number; slice in s1 if the arg is true, s2 otherwise |
7643 | 1875 |
"/ use first arg if # is not given (i.e. no number before s,p ...) |
5964 | 1876 |
"/ <n> replace by a newLine character |
1877 |
"/ <t> replace by a tab character |
|
6834
eba4b58b8692
fixed expandMacros (%-escape)
Claus Gittinger <cg@exept.de>
parents:
6810
diff
changeset
|
1878 |
"/ %X the X character itself |
5964 | 1879 |
|
8999
f3fda66ea51e
#expandMacrosWithArguments: - be mor friendly to xml strings.
Stefan Vogel <sv@exept.de>
parents:
8971
diff
changeset
|
1880 |
|in out c fmt nr arg s1 s2 peekc| |
5964 | 1881 |
|
1882 |
in := self readStream. |
|
10721
58e6950264cb
care for bitsPerCharacter when expanding macros
Claus Gittinger <cg@exept.de>
parents:
10703
diff
changeset
|
1883 |
out := CharacterWriteStream on:(self species uninitializedNew:self size). |
5964 | 1884 |
|
1885 |
[in atEnd] whileFalse:[ |
|
20874 | 1886 |
c := in next. |
1887 |
c == $% ifTrue:[ |
|
1888 |
c := in next. |
|
1889 |
out nextPut:c |
|
1890 |
] ifFalse:[c ~~ $< ifTrue:[ |
|
1891 |
out nextPut:c. |
|
1892 |
] ifFalse:[ |
|
1893 |
peekc := in peek. |
|
1894 |
[peekc == $<] whileTrue:[ |
|
1895 |
out nextPut:$<. |
|
1896 |
peekc := in nextPeek. |
|
1897 |
]. |
|
1898 |
peekc == $n ifTrue:[ |
|
1899 |
peekc := in nextPeek. |
|
1900 |
peekc == $> ifTrue:[ |
|
1901 |
in next. |
|
1902 |
out cr. |
|
1903 |
] ifFalse:[ |
|
1904 |
out nextPutAll:'<n'. |
|
1905 |
] |
|
1906 |
] ifFalse:[peekc == $t ifTrue:[ |
|
1907 |
peekc := in nextPeek. |
|
1908 |
peekc == $> ifTrue:[ |
|
1909 |
in next. |
|
1910 |
out tab. |
|
1911 |
] ifFalse:[ |
|
1912 |
out nextPutAll:'<t'. |
|
1913 |
] |
|
1914 |
] ifFalse:[ |
|
1915 |
peekc isDigit ifTrue:[ |
|
1916 |
"start an argument expansion ..." |
|
1917 |
nr := Integer readFrom:in onError:nil. |
|
1918 |
nr isNil ifTrue:[ |
|
1919 |
"this cannot happen (there is at least one digit)" |
|
23009 | 1920 |
self proceedableError:'invalid format'. |
20874 | 1921 |
^ self |
1922 |
]. |
|
1923 |
fmt := in next. |
|
22999 | 1924 |
(fmt ~~ $? and:[fmt ~~ $# and:[in peek ~~ $>]]) ifTrue:[ |
20874 | 1925 |
out nextPut:$<. |
1926 |
nr printOn:out. |
|
1927 |
out nextPut:fmt. |
|
1928 |
] ifFalse:[ |
|
1929 |
(nr between:1 and:argArray size) ifTrue:[ |
|
1930 |
arg := argArray at:nr. |
|
1931 |
] ifFalse:[ |
|
1932 |
arg := '' |
|
1933 |
]. |
|
1934 |
||
1935 |
fmt == $p ifTrue:[ |
|
1936 |
"expand with args printString" |
|
1937 |
arg printOn:out. |
|
1938 |
] ifFalse:[fmt == $s ifTrue:[ |
|
1939 |
"expand with arg itself" |
|
1940 |
arg isText ifTrue:[ |
|
1941 |
out contentsSpecies isText ifFalse:[ |
|
1942 |
out := (TextStream ? CharacterWriteStream on:Text new) nextPutAll:out contents; yourself. |
|
1943 |
]. |
|
1944 |
out nextPutAll:arg. |
|
1945 |
] ifFalse:[ |
|
1946 |
out nextPutAll:arg asString string. "see method comment: arg must know #asString" |
|
1947 |
] |
|
22999 | 1948 |
] ifFalse:[(fmt == $? or:[fmt == $#]) ifTrue:[ |
20874 | 1949 |
s1 := in upTo:$:. |
1950 |
s2 := in nextUpTo:$>. |
|
22999 | 1951 |
((fmt == $? and:[arg == true]) |
23009 | 1952 |
or:[(fmt == $# and:[arg == 1])]) ifTrue:[ |
20874 | 1953 |
out nextPutAll:s1 |
1954 |
] ifFalse:[ |
|
1955 |
out nextPutAll:s2 |
|
1956 |
]. |
|
1957 |
] ifFalse:[ |
|
1958 |
"what does VW do here ?" |
|
1959 |
self error:'invalid format' mayProceed:true. |
|
1960 |
^ self |
|
1961 |
]]]. |
|
1962 |
c := in next. |
|
1963 |
c ~~ $> ifTrue:[ |
|
1964 |
"what does VW do here ?" |
|
1965 |
self error:'invalid format' mayProceed:true. |
|
1966 |
^ self |
|
1967 |
] |
|
1968 |
||
1969 |
]. |
|
1970 |
] ifFalse:[ |
|
1971 |
out nextPut:$<. |
|
1972 |
]. |
|
1973 |
]]. |
|
1974 |
]]. |
|
5964 | 1975 |
]. |
1976 |
^ out contents |
|
1977 |
||
1978 |
" |
|
8874 | 1979 |
'hello <1s> how are you' expandMacrosWith:(OperatingSystem getLoginName) |
1980 |
'one plus one is <1p>' expandMacrosWith:2 |
|
5964 | 1981 |
" |
1982 |
||
10721
58e6950264cb
care for bitsPerCharacter when expanding macros
Claus Gittinger <cg@exept.de>
parents:
10703
diff
changeset
|
1983 |
"Modified: / 18-09-2007 / 22:50:43 / cg" |
23009 | 1984 |
"Modified: / 24-05-2018 / 21:06:31 / Claus Gittinger" |
9192 | 1985 |
! |
1986 |
||
1987 |
isCharacters |
|
21318 | 1988 |
"true, if the receiver is a string-like thing. |
1989 |
added for visual works compatibility" |
|
1990 |
||
1991 |
"/ kept in libbasic package, because it is used in libjava and imap-implementation |
|
9192 | 1992 |
^ true |
1993 |
! ! |
|
1994 |
||
16295 | 1995 |
!CharacterArray methodsFor:'JavaScript support'! |
1996 |
||
1997 |
unquote |
|
17000 | 1998 |
"removes double quotes from the receiver. |
19698 | 1999 |
This is the JavaScript standard unquote function." |
2000 |
||
2001 |
^ self unquote:$" |
|
16295 | 2002 |
|
2003 |
" |
|
17000 | 2004 |
'hello' quote unquote |
2005 |
||
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2006 |
JavaScriptParser evaluate:'''hello''.quote.unquote' |
16295 | 2007 |
" |
19698 | 2008 |
! |
2009 |
||
2010 |
unquote:quoteCharacter |
|
2011 |
"removes quoteCharacter from either end of the receiver." |
|
2012 |
||
2013 |
|mySize| |
|
2014 |
||
2015 |
(mySize := self size) >= 2 ifTrue:[ |
|
20874 | 2016 |
((self first == quoteCharacter) and:[self last == quoteCharacter]) ifTrue:[ |
2017 |
^ self copyFrom:2 to:mySize-1 |
|
2018 |
]. |
|
19698 | 2019 |
]. |
2020 |
^ self |
|
2021 |
||
2022 |
" |
|
2023 |
'*hello*' unquote:$* |
|
2024 |
" |
|
16295 | 2025 |
! ! |
13510 | 2026 |
|
16175 | 2027 |
|
9192 | 2028 |
!CharacterArray methodsFor:'character searching'! |
2029 |
||
2030 |
includesMatchCharacters |
|
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
2031 |
"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
|
2032 |
for match operations; false if not. |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2033 |
Here, do not care for $\ escapes" |
608 | 2034 |
|
2035 |
^ self includesAny:'*#[' |
|
2519
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2036 |
|
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2037 |
" |
8874 | 2038 |
'*foo' includesMatchCharacters |
2039 |
'\*foo' includesMatchCharacters |
|
2040 |
'\*foo' includesUnescapedMatchCharacters |
|
2041 |
'*foo' includesMatchCharacters |
|
2519
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2042 |
'\\*foo' includesMatchCharacters |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2043 |
'foo*' includesMatchCharacters |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2044 |
'foo\*' includesMatchCharacters |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2045 |
'foo\' includesMatchCharacters |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2046 |
" |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2047 |
|
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2048 |
"Modified: 2.4.1997 / 18:12:34 / cg" |
9192 | 2049 |
! |
2050 |
||
2051 |
includesSeparator |
|
2052 |
"return true, if the receiver contains any whitespace characters" |
|
2053 |
||
2054 |
^ (self indexOfSeparator ~~ 0) |
|
2055 |
||
2056 |
" |
|
2057 |
'hello world' includesSeparator |
|
2058 |
'helloworld' includesSeparator |
|
2059 |
" |
|
2060 |
! |
|
2061 |
||
2062 |
includesUnescapedMatchCharacters |
|
8874 | 2063 |
"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
|
2064 |
for match operations; false if not. |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2065 |
Here, care for $\ escapes" |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2066 |
|
5342
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
2067 |
|idx sz specialChars escape| |
2519
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2068 |
|
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2069 |
idx := 1. |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2070 |
sz := self size. |
5342
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
2071 |
specialChars := '*#[\'. |
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
2072 |
(escape := self class matchEscapeCharacter) ~~ $\ ifTrue:[ |
20874 | 2073 |
specialChars := specialChars copy. |
2074 |
specialChars at:specialChars size put:escape |
|
5342
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
2075 |
]. |
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
2076 |
|
16245 | 2077 |
[ |
20874 | 2078 |
idx := self indexOfAny:specialChars startingAt:idx. |
2079 |
idx == 0 ifTrue:[^ false]. |
|
2080 |
(self at:idx) == escape ifFalse:[^ true]. |
|
2081 |
idx := idx + 2. |
|
2082 |
idx > sz ifTrue:[^ false]. |
|
16245 | 2083 |
] loop. |
8874 | 2084 |
|
2085 |
" |
|
2086 |
'*foo' includesUnescapedMatchCharacters |
|
2087 |
'\*foo' includesUnescapedMatchCharacters |
|
2088 |
'\\foo' includesUnescapedMatchCharacters |
|
2089 |
'\\\$foo' includesUnescapedMatchCharacters |
|
2519
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2090 |
'*foo' includesUnescapedMatchCharacters |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2091 |
'\\*foo' includesUnescapedMatchCharacters |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2092 |
'foo*' includesUnescapedMatchCharacters |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2093 |
'foo\*' includesUnescapedMatchCharacters |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2094 |
'foo\' includesUnescapedMatchCharacters |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2095 |
" |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2096 |
|
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2097 |
"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
|
2098 |
"Created: 2.4.1997 / 17:23:26 / cg" |
9192 | 2099 |
! |
2100 |
||
2101 |
indexOfControlCharacterStartingAt:startIndex |
|
3668
76271941f044
added #indexOfControlCharacterStartingAt:
Claus Gittinger <cg@exept.de>
parents:
3656
diff
changeset
|
2102 |
"return the index of the next control character; |
76271941f044
added #indexOfControlCharacterStartingAt:
Claus Gittinger <cg@exept.de>
parents:
3656
diff
changeset
|
2103 |
that is a character with asciiValue < 32. |
15605 | 2104 |
Start the search at startIndex, searching forward. |
3668
76271941f044
added #indexOfControlCharacterStartingAt:
Claus Gittinger <cg@exept.de>
parents:
3656
diff
changeset
|
2105 |
Return 0 if none is found." |
76271941f044
added #indexOfControlCharacterStartingAt:
Claus Gittinger <cg@exept.de>
parents:
3656
diff
changeset
|
2106 |
|
76271941f044
added #indexOfControlCharacterStartingAt:
Claus Gittinger <cg@exept.de>
parents:
3656
diff
changeset
|
2107 |
|start "{ Class: SmallInteger }" |
9192 | 2108 |
mySize "{ Class: SmallInteger }"| |
2109 |
||
2110 |
start := startIndex. |
|
2111 |
mySize := self size. |
|
2112 |
||
2113 |
start to:mySize do:[:index | |
|
20874 | 2114 |
(self at:index) isControlCharacter ifTrue:[^ index] |
9192 | 2115 |
]. |
2116 |
^ 0 |
|
2117 |
||
2118 |
" |
|
2119 |
'hello world' asTwoByteString indexOfControlCharacterStartingAt:1 |
|
2120 |
'hello world\foo' withCRsasTwoByteString indexOfControlCharacterStartingAt:1 |
|
2121 |
" |
|
2122 |
||
2123 |
"Modified: / 21.7.1998 / 17:25:07 / cg" |
|
2124 |
! |
|
3668
76271941f044
added #indexOfControlCharacterStartingAt:
Claus Gittinger <cg@exept.de>
parents:
3656
diff
changeset
|
2125 |
|
8541 | 2126 |
indexOfNonSeparator |
2127 |
"return the index of the first non-whitespace character. |
|
2128 |
return 0 if no non-separator was found" |
|
2129 |
||
2130 |
^ self indexOfNonSeparatorStartingAt:1. |
|
2131 |
||
2132 |
" |
|
8874 | 2133 |
' hello world' indexOfNonSeparator |
2134 |
' ' indexOfNonSeparator |
|
2135 |
'a ' indexOfNonSeparator |
|
2136 |
'abc' indexOfNonSeparator |
|
2137 |
' ' indexOfNonSeparator |
|
9192 | 2138 |
'' indexOfNonSeparator |
2139 |
" |
|
2140 |
! |
|
2141 |
||
2142 |
indexOfNonSeparatorStartingAt:startIndex |
|
4117 | 2143 |
"return the index of the next non-whitespace character, |
2144 |
starting the search at startIndex, searching forward; |
|
2145 |
return 0 if no non-separator was found" |
|
608 | 2146 |
|
2147 |
|start "{ Class: SmallInteger }" |
|
2148 |
mySize "{ Class: SmallInteger }"| |
|
2149 |
||
2150 |
start := startIndex. |
|
2151 |
mySize := self size. |
|
2152 |
||
2153 |
start to:mySize do:[:index | |
|
20874 | 2154 |
(self at:index) isSeparator ifFalse:[^ index] |
608 | 2155 |
]. |
2156 |
^ 0 |
|
2157 |
||
2158 |
" |
|
8874 | 2159 |
' hello world' indexOfNonSeparatorStartingAt:1 |
2160 |
' ' indexOfNonSeparatorStartingAt:1 |
|
2161 |
'a ' indexOfNonSeparatorStartingAt:2 |
|
4117 | 2162 |
" |
2163 |
||
608 | 2164 |
" |
2165 |
|s index1 index2| |
|
2166 |
s := ' foo bar baz'. |
|
2167 |
index1 := s indexOfNonSeparatorStartingAt:1. |
|
2168 |
index2 := s indexOfSeparatorStartingAt:index1. |
|
2169 |
s copyFrom:index1 to:index2 - 1 |
|
2170 |
" |
|
9192 | 2171 |
! |
2172 |
||
2173 |
indexOfSeparator |
|
2174 |
"return the index of the first whitespace character; |
|
2175 |
starting the search at the beginning, searching forward; |
|
2176 |
return 0 if no separator was found" |
|
2177 |
||
2178 |
^ self indexOfSeparatorStartingAt:1 |
|
2179 |
||
2180 |
" |
|
2181 |
'hello world' indexOfSeparator |
|
2182 |
'helloworld' indexOfSeparator |
|
2183 |
'hello ' indexOfSeparator |
|
2184 |
' hello' indexOfSeparator |
|
2185 |
" |
|
2186 |
! |
|
2187 |
||
15912 | 2188 |
indexOfSeparatorOrEndStartingAt:startIndex |
2189 |
"return the index of the next whitespace character, |
|
2190 |
starting the search at startIndex, searching forward; |
|
19737 | 2191 |
return the index of one beyond the end of the receiver if no separator was found. |
15912 | 2192 |
To extract the word, copy from startIndex to the returned index-1" |
2193 |
||
2194 |
|idx| |
|
2195 |
||
2196 |
idx := self indexOfSeparatorStartingAt:startIndex. |
|
2197 |
idx == 0 ifTrue:[^ self size + 1]. |
|
2198 |
^ idx. |
|
2199 |
||
2200 |
" |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2201 |
'hello world' indexOfSeparatorOrEndStartingAt:3 |
15912 | 2202 |
' hello world' indexOfSeparatorOrEndStartingAt:3 |
2203 |
'hello world ' indexOfSeparatorOrEndStartingAt:3 |
|
2204 |
'hello world ' indexOfSeparatorOrEndStartingAt:6 |
|
2205 |
'hello world ' indexOfSeparatorOrEndStartingAt:7 |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2206 |
'helloworld ' indexOfSeparatorOrEndStartingAt:7 |
15912 | 2207 |
'helloworld' indexOfSeparatorOrEndStartingAt:7 |
19737 | 2208 |
'helloworld' indexOfSeparatorStartingAt:7 |
15912 | 2209 |
" |
2210 |
! |
|
2211 |
||
9192 | 2212 |
indexOfSeparatorStartingAt:startIndex |
2213 |
"return the index of the next whitespace character, |
|
2214 |
starting the search at startIndex, searching forward; |
|
2215 |
return 0 if no separator was found" |
|
2216 |
||
2217 |
|start "{ Class: SmallInteger }" |
|
2218 |
mySize "{ Class: SmallInteger }"| |
|
2219 |
||
2220 |
start := startIndex. |
|
2221 |
mySize := self size. |
|
2222 |
||
2223 |
start to:mySize do:[:index | |
|
20874 | 2224 |
(self at:index) isSeparator ifTrue:[^ index] |
9192 | 2225 |
]. |
2226 |
^ 0 |
|
2227 |
||
2228 |
" |
|
2229 |
'hello world' indexOfSeparatorStartingAt:3 |
|
2230 |
' hello world' indexOfSeparatorStartingAt:3 |
|
8874 | 2231 |
'hello world ' indexOfSeparatorStartingAt:3 |
2232 |
'hello world ' indexOfSeparatorStartingAt:6 |
|
2233 |
'hello world ' indexOfSeparatorStartingAt:7 |
|
2234 |
'helloworld ' indexOfSeparatorStartingAt:7 |
|
2235 |
'helloworld' indexOfSeparatorStartingAt:7 |
|
4117 | 2236 |
" |
2519
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2237 |
! |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2238 |
|
4117 | 2239 |
lastIndexOfSeparator |
14150
e13160aa321a
comment/format in: #lastIndexOfSeparator
Claus Gittinger <cg@exept.de>
parents:
14141
diff
changeset
|
2240 |
"return the last index of a whitespace character (space or tab). |
4117 | 2241 |
(i.e. start the search at the end and search backwards); |
2242 |
Returns 0 if no separator is found." |
|
2243 |
||
2244 |
^ self lastIndexOfSeparatorStartingAt:(self size) |
|
2245 |
||
2246 |
" |
|
8874 | 2247 |
'hello world' lastIndexOfSeparator |
2248 |
'helloworld' lastIndexOfSeparator |
|
2249 |
'hel lo wor ld' lastIndexOfSeparator |
|
2250 |
'hel ' lastIndexOfSeparator 6 |
|
4117 | 2251 |
" |
14150
e13160aa321a
comment/format in: #lastIndexOfSeparator
Claus Gittinger <cg@exept.de>
parents:
14141
diff
changeset
|
2252 |
|
e13160aa321a
comment/format in: #lastIndexOfSeparator
Claus Gittinger <cg@exept.de>
parents:
14141
diff
changeset
|
2253 |
"Modified (comment): / 01-06-2012 / 13:10:30 / cg" |
4117 | 2254 |
! |
2255 |
||
2256 |
lastIndexOfSeparatorStartingAt:startIndex |
|
9192 | 2257 |
"return the index of the previous whitespace character, |
2258 |
starting the search at startIndex (and searching backwards); |
|
2259 |
returns 0 if no separator was found" |
|
2260 |
||
2261 |
|start "{ Class: SmallInteger }"| |
|
2262 |
||
2263 |
start := startIndex. |
|
2264 |
||
2265 |
start to:1 by:-1 do:[:index | |
|
20874 | 2266 |
(self at:index) isSeparator ifTrue:[^ index] |
9192 | 2267 |
]. |
2268 |
^ 0 |
|
2269 |
||
2270 |
" |
|
2271 |
'hello world' lastIndexOfSeparatorStartingAt:3 |
|
2272 |
'hello world' lastIndexOfSeparatorStartingAt:7 |
|
2273 |
'helloworld' lastIndexOfSeparatorStartingAt:7 |
|
8874 | 2274 |
' helloworld' lastIndexOfSeparatorStartingAt:7 |
4117 | 2275 |
" |
608 | 2276 |
! ! |
2277 |
||
2278 |
!CharacterArray methodsFor:'comparing'! |
|
2279 |
||
6742
292775fda600
avoid endless recursion when comparing int < string
Claus Gittinger <cg@exept.de>
parents:
6655
diff
changeset
|
2280 |
< aString |
608 | 2281 |
"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
|
2282 |
receiver is greater than the argument. Otherwise return false. |
8874 | 2283 |
This comparison is based on the elements ascii code - |
9192 | 2284 |
i.e. upper/lowercase & upper/lowercase & national characters are NOT treated specially." |
2285 |
||
2286 |
|mySize "{ Class: SmallInteger }" |
|
2287 |
otherSize "{ Class: SmallInteger }" |
|
2288 |
n "{ Class: SmallInteger }" |
|
2289 |
c1 c2| |
|
2290 |
||
2291 |
mySize := self size. |
|
2292 |
otherSize := aString string size. |
|
6742
292775fda600
avoid endless recursion when comparing int < string
Claus Gittinger <cg@exept.de>
parents:
6655
diff
changeset
|
2293 |
n := mySize min:otherSize. |
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 |
1 to:n do:[:index | |
20874 | 2296 |
c1 := self at:index. |
2297 |
c2 := aString at:index. |
|
2298 |
(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
|
2299 |
]. |
292775fda600
avoid endless recursion when comparing int < string
Claus Gittinger <cg@exept.de>
parents:
6655
diff
changeset
|
2300 |
^ mySize < otherSize |
608 | 2301 |
! |
2302 |
||
2303 |
= aString |
|
2304 |
"Compare the receiver with the argument and return true if the |
|
2305 |
receiver is equal to the argument. Otherwise return false. |
|
1252 | 2306 |
|
9191 | 2307 |
This compare does NOT ignore case differences, |
608 | 2308 |
therefore 'foo' = 'Foo' will return false. |
2309 |
Since this is incompatible to ST-80 (at least, V2.x) , this may change." |
|
2310 |
||
14137
1eb1b4a3f364
Inherit from UninterpretedBytes instead of ByteArray
Stefan Vogel <sv@exept.de>
parents:
14122
diff
changeset
|
2311 |
|mySize "{ Class: SmallInteger }"| |
608 | 2312 |
|
5963
23462e06dcff
compare with Texts fixed (should compare equal if strings are)
Claus Gittinger <cg@exept.de>
parents:
5946
diff
changeset
|
2313 |
(aString isString or:[aString species == self species]) ifFalse:[ |
20874 | 2314 |
^ false |
5963
23462e06dcff
compare with Texts fixed (should compare equal if strings are)
Claus Gittinger <cg@exept.de>
parents:
5946
diff
changeset
|
2315 |
]. |
608 | 2316 |
mySize := self size. |
14137
1eb1b4a3f364
Inherit from UninterpretedBytes instead of ByteArray
Stefan Vogel <sv@exept.de>
parents:
14122
diff
changeset
|
2317 |
mySize ~~ (aString size) ifTrue:[^ false]. |
608 | 2318 |
|
2319 |
1 to:mySize do:[:index | |
|
20874 | 2320 |
(self at:index) = (aString at:index) ifFalse:[^ false]. |
608 | 2321 |
]. |
2322 |
^ true |
|
2323 |
||
2324 |
" |
|
8874 | 2325 |
'foo' = 'Foo' |
2326 |
'foo' = 'bar' |
|
2327 |
'foo' = 'foo' |
|
2328 |
'foo' = 'foo' asText |
|
2329 |
'foo' asText = 'foo' |
|
2330 |
'foo' asText = 'foo' asText |
|
608 | 2331 |
" |
1252 | 2332 |
|
2333 |
"Modified: 22.4.1996 / 15:53:58 / cg" |
|
608 | 2334 |
! |
9192 | 2335 |
|
9191 | 2336 |
> aString |
608 | 2337 |
"Compare the receiver with the argument and return true if the |
2338 |
receiver is greater than the argument. Otherwise return false. |
|
8874 | 2339 |
This comparison is based on the elements ascii code - |
1252 | 2340 |
i.e. upper/lowercase & upper/lowercase & national characters are NOT treated specially." |
608 | 2341 |
|
2342 |
|mySize "{ Class: SmallInteger }" |
|
8874 | 2343 |
otherSize "{ Class: SmallInteger }" |
9192 | 2344 |
n "{ Class: SmallInteger }" |
2345 |
c1 c2| |
|
2346 |
||
2347 |
mySize := self size. |
|
5929 | 2348 |
otherSize := aString string size. |
2349 |
n := mySize min:otherSize. |
|
2350 |
||
2351 |
1 to:n do:[:index | |
|
20874 | 2352 |
c1 := self at:index. |
2353 |
c2 := aString at:index. |
|
2354 |
(c1 == c2 or:[c1 = c2]) ifFalse:[^ c1 > c2]. |
|
5929 | 2355 |
]. |
9192 | 2356 |
^ mySize > otherSize |
2357 |
||
2358 |
"Modified: 22.4.1996 / 15:55:00 / cg" |
|
2359 |
! |
|
2360 |
||
14660 | 2361 |
after:aString |
23155 | 2362 |
<resource: #obsolete> |
14660 | 2363 |
"Compare the receiver with the argument and return true if the |
2364 |
receiver should come after the argument in a sorted list. |
|
2365 |
Otherwise return false. |
|
2366 |
NOTE: The comparison should be language specific, depending on the value of |
|
20874 | 2367 |
LC_COLLATE, which is initialized from the environment. |
2368 |
||
2369 |
Currently it is for Strings, but not for UnicodeStrings... |
|
14660 | 2370 |
|
2371 |
STUPID: |
|
23155 | 2372 |
#after has a completely different meaning in SeqColl... |
2373 |
... therefore it is marked as obsolete. |
|
2374 |
" |
|
14660 | 2375 |
|
2376 |
^ (self compareCollatingWith:aString) > 0 |
|
23155 | 2377 |
|
2378 |
"Modified (comment): / 29-06-2018 / 11:41:33 / Claus Gittinger" |
|
14660 | 2379 |
! |
2380 |
||
24232 | 2381 |
caselessAfter:aString |
2382 |
"True if the receiver comes after aString, if compared caseless. |
|
2383 |
(i.e. if receiver > aString, ignoring case)" |
|
2384 |
||
2385 |
^ (self compareCaselessWith:aString) > 0 |
|
2386 |
||
2387 |
" |
|
2388 |
'aaa1' > 'aaA2' -> true |
|
2389 |
'aaa1' caselessAfter: 'aaA2' -> false |
|
2390 |
" |
|
2391 |
||
2392 |
"Created: / 29-05-2019 / 10:09:55 / Claus Gittinger" |
|
2393 |
! |
|
2394 |
||
2395 |
caselessBefore:aString |
|
2396 |
"True if the receiver comes before aString, if compared caseless. |
|
2397 |
(i.e. if receiver < aString, ignoring case)" |
|
2398 |
||
2399 |
^ (self compareCaselessWith:aString) < 0 |
|
2400 |
||
2401 |
" |
|
2402 |
'aaa1' < 'aaA2' -> false |
|
2403 |
'aaa1' caselessBefore: 'aaA2' -> true |
|
2404 |
" |
|
2405 |
||
2406 |
"Created: / 29-05-2019 / 10:09:18 / Claus Gittinger" |
|
2407 |
! |
|
2408 |
||
2409 |
caselessEqual:aString |
|
2410 |
"True if the receiver has the same characters as aString, if compared caseless. |
|
2411 |
(i.e. if receiver = aString, ignoring case)" |
|
2412 |
||
2413 |
^ (self sameAs:aString) |
|
2414 |
||
2415 |
" |
|
2416 |
'aaa1' = 'aaA1' -> false |
|
2417 |
'aaa1' caselessEqual: 'aaA1' -> true |
|
2418 |
" |
|
2419 |
||
2420 |
"Created: / 29-05-2019 / 10:10:53 / Claus Gittinger" |
|
2421 |
! |
|
2422 |
||
15814 | 2423 |
compareAsVersionNumberWith:aStringOrCollection |
2424 |
"Compare the receiver with the argument and return 1 if the receiver is |
|
2425 |
greater, 0 if equal and -1 if less than the argument in a sorted list. |
|
2426 |
Compare as version numbers in the form a.b.c... ." |
|
2427 |
||
23121 | 2428 |
|
2429 |
^ self asVersionNumberCollection |
|
2430 |
compareWith:aStringOrCollection asVersionNumberCollection |
|
15814 | 2431 |
|
2432 |
" |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2433 |
self assert:('1' compareAsVersionNumberWith:'2') < 0. |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2434 |
self assert:('2' compareAsVersionNumberWith:'1') > 0. |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2435 |
self assert:('1.1' compareAsVersionNumberWith:'2.1.2') < 0. |
23121 | 2436 |
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
|
2437 |
self assert:('2.1' compareAsVersionNumberWith:'1.2.3') > 0. |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2438 |
self assert:('1' compareAsVersionNumberWith:'1.1') < 0. |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2439 |
self assert:('1.1' compareAsVersionNumberWith:'1') > 0. |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2440 |
self assert:('1.1' compareAsVersionNumberWith:'1.2') < 0. |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2441 |
self assert:('1.10' compareAsVersionNumberWith:'1.2') > 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.5') < 0. |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2443 |
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
|
2444 |
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
|
2445 |
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
|
2446 |
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
|
2447 |
self assert:('1.2.3.4' compareAsVersionNumberWith:#(1 2 3 4)) = 0. |
22325 | 2448 |
self assert:('1.2.3.4' compareAsVersionNumberWith:#('1' 2 3 4)) = 0. |
23121 | 2449 |
|
2450 |
self assert:('1.1' compareAsVersionNumberWith:'1.1a') < 0. |
|
2451 |
||
22325 | 2452 |
" |
2453 |
||
2454 |
"Modified: / 25-10-2017 / 11:25:08 / stefan" |
|
23121 | 2455 |
"Modified (format): / 20-06-2018 / 19:23:18 / Stefan Vogel" |
15814 | 2456 |
! |
2457 |
||
9192 | 2458 |
compareCaselessWith:aString |
22327 | 2459 |
"Compare the receiver against the argument, ignoring case. |
9190 | 2460 |
Return 1 if the receiver is greater, 0 if equal and -1 if less than the argument. |
2461 |
||
8874 | 2462 |
This comparison is based on the elements ascii code - |
9190 | 2463 |
i.e. national characters are NOT treated specially. |
2464 |
'foo' compareWith: 'Foo' will return 0" |
|
608 | 2465 |
|
2466 |
|mySize "{ Class: SmallInteger }" |
|
8874 | 2467 |
otherSize "{ Class: SmallInteger }" |
9192 | 2468 |
n "{ Class: SmallInteger }" |
608 | 2469 |
c1 c2| |
2470 |
||
2471 |
mySize := self size. |
|
5739
4fd3be7ccc75
oops some relational ops did accept a non-string arg
Claus Gittinger <cg@exept.de>
parents:
5732
diff
changeset
|
2472 |
otherSize := aString string size. |
608 | 2473 |
n := mySize min:otherSize. |
2474 |
||
2475 |
1 to:n do:[:index | |
|
20874 | 2476 |
c1 := (self at:index) asLowercase. |
2477 |
c2 := (aString at:index) asLowercase. |
|
2478 |
c1 > c2 ifTrue:[^ 1]. |
|
2479 |
c1 < c2 ifTrue:[^ -1]. |
|
608 | 2480 |
]. |
2481 |
mySize > otherSize ifTrue:[^ 1]. |
|
2482 |
mySize < otherSize ifTrue:[^ -1]. |
|
2483 |
^ 0 |
|
1252 | 2484 |
|
24232 | 2485 |
" |
2486 |
'aaa1' < 'aaA2' -> false |
|
2487 |
'aaa1' compareCaselessWith: 'aaA2' -> -1 |
|
2488 |
" |
|
2489 |
||
22327 | 2490 |
"Modified: / 22-04-1996 / 15:56:07 / cg" |
2491 |
"Modified (comment): / 26-10-2017 / 16:01:01 / mawalch" |
|
24232 | 2492 |
"Modified (comment): / 29-05-2019 / 10:06:00 / Claus Gittinger" |
608 | 2493 |
! |
2494 |
||
14660 | 2495 |
compareCollatingWith:aString |
2496 |
"Compare the receiver with the argument and return 1 if the receiver is |
|
2497 |
greater, 0 if equal and -1 if less than the argument in a sorted list. |
|
2498 |
The comparison is language specific, depending on the value of |
|
2499 |
LC_COLLATE, which is in the shell environment." |
|
2500 |
||
2501 |
"TODO not yet defined for unicode" |
|
2502 |
<resource: #todo> |
|
2503 |
||
2504 |
|s| |
|
2505 |
||
2506 |
(s := self string) ~~ self ifTrue:[ |
|
20874 | 2507 |
^ s compareCollatingWith:aString |
14660 | 2508 |
]. |
2509 |
^ self compareWith:aString |
|
2510 |
! |
|
2511 |
||
9192 | 2512 |
compareWith:aString |
9190 | 2513 |
"Compare the receiver with the argument and return 1 if the receiver is |
2514 |
greater, 0 if equal and -1 if less than the argument. |
|
11861 | 2515 |
This comparison is based on the elements' codepoints - |
9190 | 2516 |
i.e. upper/lowercase & national characters are NOT treated specially. |
2517 |
'foo' compareWith: 'Foo' will return 1. |
|
2518 |
while 'foo' sameAs:'Foo' will return true" |
|
2519 |
||
15814 | 2520 |
|s| |
2521 |
||
2522 |
s := self string. |
|
2523 |
s ~~ self ifTrue:[ |
|
20874 | 2524 |
^ s compareWith:aString string. |
9192 | 2525 |
]. |
15814 | 2526 |
^ super compareWith:aString string. |
9192 | 2527 |
! |
2528 |
||
11861 | 2529 |
endsWith:aStringOrCharacter |
16175 | 2530 |
"return true, if the receiver ends with something, aStringOrCharacter. |
2531 |
If aStringOrCharacter is empty, true is returned" |
|
11861 | 2532 |
|
2533 |
|s| |
|
2534 |
||
2535 |
(s := self string) ~~ self ifTrue:[ |
|
20874 | 2536 |
^ s endsWith:aStringOrCharacter |
11861 | 2537 |
]. |
21083 | 2538 |
(self size ~~ 0 and:[aStringOrCharacter isCharacter]) ifTrue:[ |
20874 | 2539 |
^ self last = aStringOrCharacter |
11861 | 2540 |
]. |
2541 |
^ super endsWith:aStringOrCharacter |
|
2542 |
||
2543 |
" |
|
2544 |
'hello world' endsWith:'world' |
|
2545 |
'hello world' asText allBold endsWith:'world' |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2546 |
'hello world' endsWith:'' |
16175 | 2547 |
'hello world' asText allBold endsWith:'' |
11861 | 2548 |
" |
2549 |
||
2550 |
"Modified: 12.5.1996 / 15:49:18 / cg" |
|
2551 |
! |
|
2552 |
||
23154 | 2553 |
endsWith:aStringOrCharacter caseSensitive:caseSensitive |
2554 |
"return true, if the receiver ends with something, aStringOrCharacter. |
|
2555 |
If aStringOrCharacter is empty, true is returned" |
|
2556 |
||
2557 |
|s| |
|
2558 |
||
2559 |
caseSensitive ifTrue:[^ self endsWith:aStringOrCharacter]. |
|
2560 |
||
2561 |
(s := self string) ~~ self ifTrue:[ |
|
2562 |
^ s endsWith:aStringOrCharacter |
|
2563 |
]. |
|
2564 |
(self size ~~ 0 and:[aStringOrCharacter isCharacter]) ifTrue:[ |
|
2565 |
^ self last asLowercase = aStringOrCharacter asLowercase |
|
2566 |
]. |
|
2567 |
^ self endsWith:aStringOrCharacter using:[:a :b | a asLowercase = b asLowercase]. |
|
2568 |
||
2569 |
" |
|
2570 |
'hello World' endsWith:'world' caseSensitive:true |
|
2571 |
'hello World' endsWith:'world' caseSensitive:false |
|
2572 |
" |
|
2573 |
||
2574 |
"Created: / 29-06-2018 / 11:38:57 / Claus Gittinger" |
|
2575 |
! |
|
2576 |
||
23783 | 2577 |
endsWithDigit |
2578 |
"Answer whether the receiver's final character represents a digit. 3/11/96 sw" |
|
2579 |
||
2580 |
|len| |
|
2581 |
||
2582 |
^ (len := self size) ~~ 0 and:[(self at:len) isDigit] |
|
24300 | 2583 |
" |
2584 |
'hello' endsWithDigit |
|
2585 |
'12hello' endsWithDigit |
|
2586 |
'hello12' endsWithDigit |
|
2587 |
" |
|
2588 |
" |
|
2589 |
'hello' startsWithDigit |
|
2590 |
'12hello' startsWithDigit |
|
2591 |
'hello12' startsWithDigit |
|
2592 |
" |
|
2593 |
||
2594 |
"Modified (comment): / 08-06-2019 / 17:23:38 / Claus Gittinger" |
|
23783 | 2595 |
! |
2596 |
||
11838 | 2597 |
hammingDistanceTo:aString |
2598 |
"return the hamming distance (the number of characters which are different). |
|
12177 | 2599 |
In information theory, the Hamming distance between two strings of equal length |
2600 |
is the number of positions for which the corresponding symbols are different. |
|
2601 |
Put another way, it measures the minimum number of substitutions required to change |
|
11838 | 2602 |
one into the other, or the number of errors that transformed one string into the other." |
2603 |
||
22377 | 2604 |
|mySize| |
2605 |
||
2606 |
mySize := self size. |
|
2607 |
self assert:(aString size == mySize). |
|
2608 |
^ 1 to:mySize count:[:idx | (self at:idx) ~= (aString at:idx)] |
|
11838 | 2609 |
|
2610 |
" |
|
2611 |
'roses' hammingDistanceTo:'toned' |
|
22377 | 2612 |
'roses' hammingDistanceTo:'doses' |
2613 |
" |
|
2614 |
||
2615 |
"Modified (comment): / 24-11-2017 / 08:50:58 / cg" |
|
11838 | 2616 |
! |
2617 |
||
9192 | 2618 |
hash |
2619 |
"return an integer useful as a hash-key" |
|
2620 |
||
16898 | 2621 |
"/ whenever changing, also care for String>>hash. |
13879 | 2622 |
"/ immediately after any change, execute (maybe in a debugger): |
2623 |
"/ Set allSubInstancesDo:[:s | s rehash] |
|
16898 | 2624 |
^ self hash_fnv1a |
2625 |
||
2626 |
" |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2627 |
'a' hash |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2628 |
'a' asUnicode16String hash |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2629 |
'a' asUnicode32String hash |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2630 |
'aa' hash |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2631 |
'aa' asUnicode16String hash |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2632 |
'aa' asUnicode32String hash |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2633 |
'ab' hash |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2634 |
'ab' asUnicode16String hash |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2635 |
'ab' asUnicode32String hash |
13879 | 2636 |
'ab' hash |
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2637 |
'ab' asArray hash |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2638 |
" |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2639 |
|
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2640 |
" |
20874 | 2641 |
|syms ms| |
2642 |
||
2643 |
syms := Symbol allInstances. |
|
2644 |
Transcript show:'syms: '; showCR:syms size. |
|
2645 |
Transcript show:'sdbm hashes: '; showCR:(syms collect:[:s| s hash]) asSet size. |
|
2646 |
Transcript show:'dragonBook hashes: '; showCR:(syms collect:[:s| s hash_dragonBook]) asSet size. |
|
2647 |
||
2648 |
ms := Time millisecondsToRun:[ |
|
2649 |
10 timesRepeat:[ |
|
2650 |
syms do:[:each| each hash]. |
|
2651 |
]. |
|
2652 |
]. |
|
2653 |
Transcript show:'sdbm hash: '; showCR:ms. |
|
2654 |
||
2655 |
ms := Time millisecondsToRun:[ |
|
2656 |
10 timesRepeat:[ |
|
2657 |
syms do:[:each| each hash_dragonBook]. |
|
2658 |
]. |
|
2659 |
]. |
|
2660 |
Transcript show:'dragonBook: '; showCR:ms. |
|
2661 |
||
2662 |
syms := syms collect:[:each| each asUnicode16String]. |
|
2663 |
ms := Time millisecondsToRun:[ |
|
2664 |
10 timesRepeat:[ |
|
2665 |
syms do:[:each| each hash]. |
|
2666 |
]. |
|
2667 |
]. |
|
2668 |
Transcript show:'unicode sdbm hash: '; showCR:ms. |
|
2669 |
||
2670 |
ms := Time millisecondsToRun:[ |
|
2671 |
10 timesRepeat:[ |
|
2672 |
syms do:[:each| each hash_dragonBook]. |
|
2673 |
]. |
|
2674 |
]. |
|
2675 |
Transcript show:'unicode dragonBook:'; showCR:ms. |
|
14641 | 2676 |
" |
2677 |
||
13879 | 2678 |
"Modified: / 26-12-2011 / 14:09:07 / cg" |
2679 |
! |
|
2680 |
||
2681 |
hash_dragonBook |
|
2682 |
"return an integer useful as a hash-key" |
|
2683 |
||
10698
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2684 |
|h g| |
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2685 |
|
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2686 |
"/ |
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2687 |
"/ this is the dragon-book algorithm |
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2688 |
"/ |
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2689 |
h := 0. |
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2690 |
self reverseDo:[:char | |
10703
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
2691 |
"/ 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
|
2692 |
"/ h := (h bitShift:4) + char asciiValue. |
20874 | 2693 |
h := (h bitShift:4). |
2694 |
h := h + char codePoint. |
|
2695 |
h := h bitAnd:16rFFFFFFFF. |
|
2696 |
g := (h bitAnd: 16rF0000000). |
|
2697 |
g ~~ 0 ifTrue:[ |
|
2698 |
h := h bitXor:(g bitShift:-24). |
|
2699 |
h := h bitXor:g. |
|
2700 |
]. |
|
10698
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2701 |
]. |
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2702 |
"/ |
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2703 |
"/ multiply by large prime to spread values |
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2704 |
"/ This speeds up Set and Dictionary by a factor of 10!! |
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2705 |
"/ |
14137
1eb1b4a3f364
Inherit from UninterpretedBytes instead of ByteArray
Stefan Vogel <sv@exept.de>
parents:
14122
diff
changeset
|
2706 |
h := h times:31415821. |
10698
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2707 |
^ h |
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2708 |
|
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2709 |
" |
10703
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
2710 |
'a' hash |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
2711 |
'a' asUnicode16String hash |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
2712 |
'aa' hash |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
2713 |
'aa' asUnicode16String hash |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
2714 |
'ab' hash |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
2715 |
'ab' asUnicode16String hash |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
2716 |
'ab' hash |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
2717 |
'ab' asArray hash |
10698
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2718 |
" |
13879 | 2719 |
|
2720 |
"Created: / 26-12-2011 / 13:46:06 / cg" |
|
2721 |
! |
|
2722 |
||
16887 | 2723 |
hash_fnv1a |
2724 |
"return an integer useful as a hash-key. |
|
2725 |
This method uses the fnv-1a algorithm |
|
19743 | 2726 |
(which is actually a very good one). |
2727 |
Attention: stops when a 0-codepoint is encountered |
|
20874 | 2728 |
(for compatibility with the hash used by the VM) |
19743 | 2729 |
Also: on 64bit CPUs, only small 4-byte hashvalues are returned, |
20874 | 2730 |
(so hash values are independent from the architecture)" |
19743 | 2731 |
|
2732 |
|h byte| |
|
16887 | 2733 |
|
2734 |
h := 2166136261. |
|
2735 |
self do:[:eachChar | |
|
20874 | 2736 |
byte := eachChar codePoint. |
2737 |
byte == 0 ifTrue:[ |
|
2738 |
"/ stop |
|
2739 |
^ (h bitXor: (h >> 30)) bitAnd: 16r3FFFFFFF. |
|
2740 |
]. |
|
2741 |
h := h bitXor:byte. |
|
2742 |
h := (h * 16777619) bitAnd:16rFFFFFFFF. |
|
16887 | 2743 |
]. |
16898 | 2744 |
"/ make sure, it fits into a smallInt |
2745 |
h := (h bitXor: (h >> 30)) bitAnd: 16r3FFFFFFF. |
|
16887 | 2746 |
^ h |
2747 |
||
2748 |
" |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2749 |
'abc' hash_fnv1a |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2750 |
'abc' asUnicode16String hash_fnv1a |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2751 |
'abc' asUnicode32String hash_fnv1a |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2752 |
|
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2753 |
'foofooHelloWorld' hash_fnv1a |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2754 |
'foofooHelloWorld' asUnicode16String hash_fnv1a |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2755 |
'foofooHelloWorld' asUnicode32String hash_fnv1a |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2756 |
|
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2757 |
'blablaHelloWorld' hash_fnv1a |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2758 |
'blablaHelloWorld' asUnicode16String hash_fnv1a |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2759 |
'blablaHelloWorld' asUnicode32String hash_fnv1a |
16887 | 2760 |
" |
2761 |
! |
|
2762 |
||
23863 | 2763 |
hash_fnv1a_64 |
2764 |
"return a 64bit integer useful as a hash-key. |
|
2765 |
This method uses the fnv-1a algorithm |
|
2766 |
(which is actually a very good one)." |
|
2767 |
||
2768 |
|h byte| |
|
2769 |
||
2770 |
h := 14695981039346656037. |
|
2771 |
self do:[:eachChar | |
|
2772 |
byte := eachChar codePoint. |
|
2773 |
h := h bitXor:byte. |
|
2774 |
h := (h * 1099511628211) bitAnd:16rFFFFFFFFFFFFFFFF. |
|
2775 |
]. |
|
2776 |
"/ make sure, it fits into 64 bit |
|
2777 |
h := (h bitXor: (h >> 30)) bitAnd: 16r3FFFFFFFFFFFFFFF. |
|
2778 |
^ h |
|
2779 |
||
2780 |
" |
|
2781 |
'abc' hash_fnv1a_64 |
|
2782 |
'abc' asUnicode16String hash_fnv1a_64 |
|
2783 |
'abc' asUnicode32String hash_fnv1a_64 |
|
2784 |
||
2785 |
'foofooHelloWorld' hash_fnv1a_64 |
|
2786 |
'foofooHelloWorld' asUnicode16String hash_fnv1a_64 |
|
2787 |
'foofooHelloWorld' asUnicode32String hash_fnv1a_64 |
|
2788 |
||
2789 |
'blablaHelloWorld' hash_fnv1a_64 |
|
2790 |
'blablaHelloWorld' asUnicode16String hash_fnv1a_64 |
|
2791 |
'blablaHelloWorld' asUnicode32String hash_fnv1a_64 |
|
2792 |
" |
|
2793 |
||
2794 |
"Created: / 11-03-2019 / 11:47:15 / Claus Gittinger" |
|
2795 |
! |
|
2796 |
||
16887 | 2797 |
hash_java |
2798 |
"return an integer useful as a hash-key. |
|
2799 |
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
|
2800 |
the java virtual machine |
16887 | 2801 |
(which is actually not a very good one)." |
2802 |
||
2803 |
|h| |
|
2804 |
||
2805 |
h := 0. |
|
2806 |
self do:[:eachChar | |
|
20874 | 2807 |
h := (h * 31) + (eachChar codePoint). |
2808 |
h := h bitAnd:16rFFFFFFFF. |
|
16887 | 2809 |
]. |
2810 |
^ h |
|
2811 |
||
2812 |
" |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2813 |
'abc' hash_java |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2814 |
'foofooHelloWorld' hash_java |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2815 |
'blablaHelloWorld' hash_java |
16887 | 2816 |
" |
2817 |
! |
|
2818 |
||
16898 | 2819 |
hash_sdbm |
2820 |
"return an integer useful as a hash-key. |
|
2821 |
This method implements the sdbm algorithm." |
|
2822 |
||
2823 |
|h| |
|
2824 |
||
2825 |
"/ |
|
2826 |
"/ this is the sdbm algorithm |
|
2827 |
"/ |
|
2828 |
h := 0. |
|
2829 |
self do:[:char | |
|
20874 | 2830 |
h := (65599 times:h) plus:char codePoint. |
16898 | 2831 |
]. |
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2832 |
^ h |
16898 | 2833 |
|
2834 |
" |
|
2835 |
'a' hash |
|
2836 |
'a' asUnicode16String hash |
|
2837 |
'aa' hash |
|
2838 |
'aa' asUnicode16String hash |
|
2839 |
'ab' hash |
|
2840 |
'ab' asUnicode16String hash |
|
2841 |
'ab' hash |
|
2842 |
'ab' asArray hash |
|
2843 |
" |
|
2844 |
||
2845 |
" |
|
20874 | 2846 |
|syms ms| |
2847 |
||
2848 |
syms := Symbol allInstances. |
|
2849 |
Transcript show:'syms: '; showCR:syms size. |
|
2850 |
Transcript show:'sdbm hashes: '; showCR:(syms collect:[:s| s hash]) asSet size. |
|
2851 |
Transcript show:'dragonBook hashes: '; showCR:(syms collect:[:s| s hash_dragonBook]) asSet size. |
|
2852 |
||
2853 |
ms := Time millisecondsToRun:[ |
|
2854 |
10 timesRepeat:[ |
|
2855 |
syms do:[:each| each hash]. |
|
2856 |
]. |
|
2857 |
]. |
|
2858 |
Transcript show:'sdbm hash: '; showCR:ms. |
|
2859 |
||
2860 |
ms := Time millisecondsToRun:[ |
|
2861 |
10 timesRepeat:[ |
|
2862 |
syms do:[:each| each hash_dragonBook]. |
|
2863 |
]. |
|
2864 |
]. |
|
2865 |
Transcript show:'dragonBook: '; showCR:ms. |
|
2866 |
||
2867 |
syms := syms collect:[:each| each asUnicode16String]. |
|
2868 |
ms := Time millisecondsToRun:[ |
|
2869 |
10 timesRepeat:[ |
|
2870 |
syms do:[:each| each hash]. |
|
2871 |
]. |
|
2872 |
]. |
|
2873 |
Transcript show:'unicode sdbm hash: '; showCR:ms. |
|
2874 |
||
2875 |
ms := Time millisecondsToRun:[ |
|
2876 |
10 timesRepeat:[ |
|
2877 |
syms do:[:each| each hash_dragonBook]. |
|
2878 |
]. |
|
2879 |
]. |
|
2880 |
Transcript show:'unicode dragonBook:'; showCR:ms. |
|
16898 | 2881 |
" |
2882 |
||
2883 |
"Modified: / 26-12-2011 / 14:09:07 / cg" |
|
2884 |
! |
|
2885 |
||
11861 | 2886 |
levenshteinTo:aString |
2887 |
"return the levenshtein distance to the argument, aString; |
|
2888 |
this value corresponds to the number of replacements that have to be |
|
2889 |
made to get aString from the receiver. |
|
2890 |
See IEEE transactions on Computers 1976 Pg 172 ff." |
|
2891 |
||
2892 |
" |
|
21760 | 2893 |
in the following, we assume that omiting a character |
11861 | 2894 |
is less of an error than inserting an extra character. |
2895 |
Therefore the different insertion (i) and deletion (d) values. |
|
20874 | 2896 |
s: substitution weight (4) |
2897 |
k: keyboard weight (k) (typing a nearby key) - or nil (then use s) |
|
2898 |
c: case weight (4) - or nil (then use s) |
|
2899 |
e: exchange weight (8) - or nil (then use s*2) |
|
2900 |
i: insertion of extra character weight (2) |
|
2901 |
d: delete of a character weight (6) |
|
22157 | 2902 |
|
2903 |
Notice that the standard levenshtein uses the same weight for insertion and deletion, |
|
2904 |
and is computed by: |
|
2905 |
'flaw' levenshteinTo:'lawn' s:2 k:nil c:nil i:1 d:1 |
|
2906 |
||
11861 | 2907 |
" |
2908 |
||
2909 |
^ StringUtilities |
|
20874 | 2910 |
levenshteinDistanceFrom:self |
2911 |
to:aString |
|
2912 |
s:4 k:4 c:4 e:nil i:2 d:6 |
|
12177 | 2913 |
|
2914 |
" |
|
2915 |
'computer' levenshteinTo:'computer' |
|
11861 | 2916 |
'cOmputer' levenshteinTo:'computer' |
12177 | 2917 |
'cOmpuTer' levenshteinTo:'computer' |
11861 | 2918 |
'cimputer' levenshteinTo:'computer' |
2919 |
'cumputer' levenshteinTo:'computer' |
|
2920 |
||
2921 |
'cmputer' levenshteinTo:'computer' |
|
2922 |
'coomputer' levenshteinTo:'computer' |
|
2923 |
||
2924 |
'ocmprt' levenshteinTo:'computer' |
|
2925 |
'computer' levenshteinTo:'computer' |
|
2926 |
'ocmputer' levenshteinTo:'computer' |
|
2927 |
'cmputer' levenshteinTo:'computer' |
|
2928 |
'computer' levenshteinTo:'cmputer' |
|
2929 |
'Computer' levenshteinTo:'computer' |
|
2930 |
||
12177 | 2931 |
'compiter' levenshteinTo:'computer' |
2932 |
'compoter' levenshteinTo:'computer' |
|
2933 |
||
2934 |
'comptuer' levenshteinTo:'computer' |
|
11861 | 2935 |
" |
21760 | 2936 |
|
2937 |
"Modified (comment): / 17-05-2017 / 16:15:41 / mawalch" |
|
22157 | 2938 |
"Modified (comment): / 02-08-2017 / 17:13:04 / cg" |
11861 | 2939 |
! |
2940 |
||
2941 |
levenshteinTo:aString s:substWeight k:kbdTypoWeight c:caseWeight i:insrtWeight d:deleteWeight |
|
2942 |
"parametrized levenshtein. |
|
2943 |
return the levenshtein distance to the argument, aString; |
|
2944 |
this value corrensponds to the number of replacements that have to be |
|
2945 |
made to get aString from the receiver. |
|
2946 |
The arguments are the costs for |
|
20874 | 2947 |
s:substitution, |
2948 |
k:keyboard type (substitution), |
|
2949 |
c:case-change, |
|
2950 |
i:insertion |
|
2951 |
d:deletion |
|
11861 | 2952 |
of a character. |
2953 |
See IEEE transactions on Computers 1976 Pg 172 ff" |
|
2954 |
||
2955 |
^ StringUtilities |
|
20874 | 2956 |
levenshteinDistanceFrom:self |
2957 |
to:aString |
|
2958 |
s:substWeight k:kbdTypoWeight c:caseWeight e:nil i:insrtWeight d:deleteWeight |
|
11861 | 2959 |
! |
2960 |
||
9190 | 2961 |
sameAs:aString |
2962 |
"Compare the receiver with the argument like =, but ignore case differences. |
|
2963 |
Return true or false." |
|
2964 |
||
2965 |
|mySize "{ Class: SmallInteger }" |
|
2966 |
otherSize c1 c2| |
|
2967 |
||
9192 | 2968 |
self == aString ifTrue:[^ true]. |
2969 |
||
2970 |
mySize := self size. |
|
2971 |
otherSize := aString string size. |
|
18884 | 2972 |
mySize ~~ otherSize ifTrue:[^ false]. |
9192 | 2973 |
|
2974 |
1 to:mySize do:[:index | |
|
20874 | 2975 |
c1 := self at:index. |
2976 |
c2 := aString at:index. |
|
2977 |
c1 ~~ c2 ifTrue:[ |
|
2978 |
(c1 sameAs:c2) ifFalse:[^ false]. |
|
2979 |
] |
|
9192 | 2980 |
]. |
2981 |
^ true |
|
2982 |
||
608 | 2983 |
" |
8874 | 2984 |
'foo' sameAs: 'Foo' |
2985 |
'foo' sameAs: 'bar' |
|
2986 |
'foo' sameAs: 'foo' |
|
9192 | 2987 |
" |
2988 |
||
2989 |
"Modified: 22.4.1996 / 15:56:17 / cg" |
|
2990 |
! |
|
608 | 2991 |
|
15734 | 2992 |
sameAs:aString caseSensitive:caseSensitive |
2993 |
"Compare the receiver with the argument. |
|
2994 |
If caseSensitive is false, this is the same as #sameAs:, |
|
2995 |
if false, this is the same as #=." |
|
2996 |
||
2997 |
caseSensitive ifFalse:[ |
|
20874 | 2998 |
^ self sameAs:aString |
15734 | 2999 |
]. |
3000 |
^ self = aString |
|
3001 |
||
3002 |
" |
|
3003 |
'foo' sameAs:'Foo' caseSensitive:false |
|
3004 |
'foo' sameAs:'foo' caseSensitive:true |
|
3005 |
" |
|
3006 |
! |
|
3007 |
||
5242 | 3008 |
sameAs:aString ignoreCase:ignoreCase |
15734 | 3009 |
<resource: #obsolete> |
5242 | 3010 |
"Compare the receiver with the argument. |
3011 |
If ignoreCase is true, this is the same as #sameAs:, |
|
3012 |
if false, this is the same as #=." |
|
3013 |
||
3014 |
ignoreCase ifTrue:[ |
|
20874 | 3015 |
^ self sameAs:aString |
5242 | 3016 |
]. |
3017 |
^ self = aString |
|
3018 |
||
3019 |
" |
|
8874 | 3020 |
'foo' sameAs:'Foo' ignoreCase:false |
3021 |
'foo' sameAs:'foo' ignoreCase:true |
|
5242 | 3022 |
" |
3023 |
||
3024 |
! |
|
3025 |
||
608 | 3026 |
sameCharacters:aString |
9192 | 3027 |
"count & return the number of characters which are the same |
3028 |
(ignoring case and emphasis) in the receiver and the argument, aString." |
|
3029 |
||
3030 |
|n "{ Class: SmallInteger }" |
|
3031 |
c1 c2 cnt| |
|
3032 |
||
3033 |
n := self size. |
|
3034 |
n := n min:(aString string size). |
|
3035 |
||
3036 |
cnt := 0. |
|
3037 |
1 to:n do:[:index | |
|
20874 | 3038 |
c1 := self at:index. |
3039 |
c2 := aString at:index. |
|
3040 |
((c1 == c2) |
|
3041 |
or:[c1 asLowercase = c2 asLowercase]) ifTrue:[ |
|
3042 |
cnt := cnt + 1 |
|
3043 |
] |
|
9192 | 3044 |
]. |
3045 |
^ cnt |
|
3046 |
||
3047 |
" |
|
3048 |
'foobarbaz' sameCharacters: 'foo' |
|
8874 | 3049 |
'foobarbaz' sameCharacters: 'Foo' |
3050 |
'foobarbaz' sameCharacters: 'baz' |
|
608 | 3051 |
" |
5510
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
3052 |
! |
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 |
sameEmphasisAs:aStringOrText |
17215 | 3055 |
"compare the receiver's and the argument's emphasis" |
5510
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
3056 |
|
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
3057 |
^ self emphasis = aStringOrText emphasis |
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
3058 |
|
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
3059 |
" |
8874 | 3060 |
'hello' asText sameEmphasisAs: 'hello' |
3061 |
'hello' asText sameEmphasisAs: 'hello' asText |
|
3062 |
'hello' asText allBold sameEmphasisAs: 'hello' |
|
3063 |
'hello' asText allBold sameEmphasisAs: 'fooba' asText allBold |
|
9192 | 3064 |
'hello' asText allBold sameEmphasisAs: 'fooba' asText allItalic |
3065 |
" |
|
3066 |
! |
|
3067 |
||
9191 | 3068 |
sameStringAndEmphasisAs:aStringOrText |
5739
4fd3be7ccc75
oops some relational ops did accept a non-string arg
Claus Gittinger <cg@exept.de>
parents:
5732
diff
changeset
|
3069 |
"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
|
3070 |
|
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
3071 |
aStringOrText isString ifFalse:[^ false]. |
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
3072 |
(self string = aStringOrText string) ifFalse:[^ false]. |
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
3073 |
self hasChangeOfEmphasis = aStringOrText hasChangeOfEmphasis ifFalse:[^ false]. |
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
3074 |
^ self emphasis = aStringOrText emphasis |
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
3075 |
|
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
3076 |
" |
8874 | 3077 |
'hello' asText sameEmphasisAs: 'hello' |
3078 |
'hello' asText sameEmphasisAs: 'hello' asText |
|
3079 |
'hello' asText allBold sameEmphasisAs: 'hello' |
|
3080 |
'hello' asText allBold sameEmphasisAs: 'fooba' asText allBold |
|
3081 |
'hello' asText allBold sameEmphasisAs: 'fooba' asText allItalic |
|
3082 |
||
3083 |
'hello' sameEmphasisAs: 'hello' asText |
|
3084 |
'hello' sameEmphasisAs: 'hello' asText allBold |
|
3085 |
'hello' sameEmphasisAs: 'fooba' |
|
3086 |
'hello' sameEmphasisAs: 'fooba' asText |
|
3087 |
'hello' sameEmphasisAs: 'fooba' asText allBold |
|
3088 |
'hello' sameEmphasisAs: 'fooba' asText allItalic |
|
3089 |
||
3090 |
'hello' asText sameStringAndEmphasisAs: 'hello' |
|
3091 |
'hello' asText sameStringAndEmphasisAs: 'hello' asText |
|
3092 |
'hello' asText allBold sameStringAndEmphasisAs: 'hello' |
|
3093 |
'hello' asText allBold sameStringAndEmphasisAs: 'fooba' asText allBold |
|
3094 |
'hello' asText allBold sameStringAndEmphasisAs: 'fooba' asText allItalic |
|
3095 |
||
3096 |
'hello' sameStringAndEmphasisAs: 'hello' asText |
|
3097 |
'hello' sameStringAndEmphasisAs: 'hello' asText allBold |
|
3098 |
'hello' sameStringAndEmphasisAs: 'fooba' |
|
3099 |
'hello' sameStringAndEmphasisAs: 'fooba' asText |
|
3100 |
'hello' sameStringAndEmphasisAs: 'fooba' asText allBold |
|
3101 |
'hello' sameStringAndEmphasisAs: 'fooba' asText allItalic |
|
5510
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
3102 |
" |
11861 | 3103 |
! |
3104 |
||
3105 |
spellAgainst: aString |
|
3106 |
"return an integer between 0 and 100 indicating how similar |
|
3107 |
the argument is to the receiver. No case conversion is done. |
|
3108 |
This algorithm is much simpler (but also less exact) than the |
|
3109 |
levenshtein distance. Experiment which is better for your |
|
3110 |
application." |
|
3111 |
||
3112 |
| i1 "{ Class: SmallInteger }" |
|
3113 |
i2 "{ Class: SmallInteger }" |
|
3114 |
next1 "{ Class: SmallInteger }" |
|
3115 |
next2 "{ Class: SmallInteger }" |
|
3116 |
size1 "{ Class: SmallInteger }" |
|
3117 |
size2 "{ Class: SmallInteger }" |
|
3118 |
score "{ Class: SmallInteger }" |
|
3119 |
maxLen "{ Class: SmallInteger }" | |
|
3120 |
||
3121 |
size1 := self size. |
|
3122 |
size2 := aString size. |
|
3123 |
maxLen := size1 max:size2. |
|
3124 |
score := 0. |
|
3125 |
i1 := i2 := 1. |
|
3126 |
[i1 <= size1 and: [i2 <= size2]] whileTrue:[ |
|
20874 | 3127 |
next1 := i1 + 1. |
3128 |
next2 := i2 + 1. |
|
3129 |
(self at:i1) == (aString at:i2) ifTrue: [ |
|
3130 |
score := score+1. |
|
3131 |
i1 := next1. |
|
3132 |
i2 := next2 |
|
3133 |
] ifFalse: [ |
|
3134 |
(i2 < size2 and: [(self at:i1) == (aString at:next2)]) ifTrue: [ |
|
3135 |
i2 := next2 |
|
3136 |
] ifFalse: [ |
|
3137 |
(i1 < size1 and: [(self at:next1) == (aString at:i2)]) ifTrue: [ |
|
3138 |
i1 := next1 |
|
3139 |
] ifFalse: [ |
|
3140 |
i1 := next1. |
|
3141 |
i2 := next2 |
|
3142 |
] |
|
3143 |
] |
|
3144 |
] |
|
11861 | 3145 |
]. |
3146 |
||
3147 |
score == maxLen ifTrue: [^ 100]. |
|
3148 |
^ 100 * score // maxLen |
|
3149 |
||
3150 |
" |
|
3151 |
'Smalltalk' spellAgainst: 'Smalltlak' |
|
3152 |
'Smalltalk' spellAgainst: 'smalltlak' |
|
3153 |
'Smalltalk' spellAgainst: 'smalltalk' |
|
3154 |
'Smalltalk' spellAgainst: 'smalltlk' |
|
3155 |
'Smalltalk' spellAgainst: 'Smalltolk' |
|
3156 |
" |
|
3157 |
! |
|
3158 |
||
16553 | 3159 |
startsWith:aStringOrCharacter |
3160 |
"return true, if the receiver starts with something, aStringOrCharacter. |
|
17371 | 3161 |
If the argument is empty, true is returned. |
3162 |
Notice, that this is similar to, but slightly different from VW's and Squeak's beginsWith:, |
|
3163 |
which are both inconsistent w.r.t. an empty argument." |
|
11861 | 3164 |
|
3165 |
|s| |
|
3166 |
||
16553 | 3167 |
aStringOrCharacter isCharacter ifTrue:[ |
23548 | 3168 |
^ (self size ~~ 0) and:[(self at:1) = aStringOrCharacter] |
16553 | 3169 |
]. |
11861 | 3170 |
(s := self string) ~~ self ifTrue:[ |
20874 | 3171 |
^ s startsWith:aStringOrCharacter |
11861 | 3172 |
]. |
16553 | 3173 |
^ super startsWith:aStringOrCharacter |
11861 | 3174 |
|
3175 |
" |
|
17364 | 3176 |
'abcde' startsWith:#($a $b $c) |
3177 |
'abcde' startsWith:'abc' |
|
3178 |
'abcd' startsWith:'abcde' |
|
3179 |
'abcde' startsWith:'abd' |
|
11861 | 3180 |
'hello world' startsWith:'hello' |
3181 |
'hello world' asText allBold startsWith:'hello' |
|
3182 |
'hello world' asText allBold startsWith:'' |
|
3183 |
" |
|
3184 |
||
23154 | 3185 |
"Created: / 12-05-1996 / 15:46:40 / cg" |
3186 |
"Modified: / 29-06-2018 / 11:27:08 / Claus Gittinger" |
|
23548 | 3187 |
"Modified (format): / 26-11-2018 / 14:15:31 / Stefan Vogel" |
23154 | 3188 |
! |
3189 |
||
3190 |
startsWith:aStringOrCharacter caseSensitive:caseSensitive |
|
3191 |
"return true, if the receiver starts with something, aStringOrCharacter. |
|
3192 |
If the argument is empty, true is returned. |
|
3193 |
Notice, that this is similar to, but slightly different from VW's and Squeak's beginsWith:, |
|
3194 |
which are both inconsistent w.r.t. an empty argument." |
|
3195 |
||
3196 |
|s| |
|
3197 |
||
23548 | 3198 |
caseSensitive ifTrue:[ |
3199 |
^ self startsWith:aStringOrCharacter |
|
3200 |
]. |
|
23154 | 3201 |
aStringOrCharacter isCharacter ifTrue:[ |
23548 | 3202 |
self size == 0 ifTrue:[ |
3203 |
^ false. |
|
3204 |
]. |
|
3205 |
s := self at:1. |
|
3206 |
^ s == aStringOrCharacter or:[s asLowercase = aStringOrCharacter asLowercase] |
|
23154 | 3207 |
]. |
3208 |
(s := self string) ~~ self ifTrue:[ |
|
3209 |
^ s startsWith:aStringOrCharacter caseSensitive:caseSensitive |
|
3210 |
]. |
|
3211 |
^ self startsWith:aStringOrCharacter using:[:a :b | a asLowercase = b asLowercase]. |
|
3212 |
||
3213 |
" |
|
3214 |
'aBCde' startsWith:'abc' caseSensitive:true |
|
3215 |
'aBCde' startsWith:'abc' caseSensitive:false |
|
3216 |
" |
|
3217 |
||
3218 |
"Created: / 29-06-2018 / 11:27:04 / Claus Gittinger" |
|
23548 | 3219 |
"Modified: / 26-11-2018 / 14:18:43 / Stefan Vogel" |
24300 | 3220 |
! |
3221 |
||
3222 |
startsWithDigit |
|
3223 |
"Answer whether the receiver's first character represents a digit. 3/11/96 sw" |
|
3224 |
||
3225 |
|len| |
|
3226 |
||
3227 |
^ (len := self size) ~~ 0 and:[(self at:1) isDigit] |
|
3228 |
||
3229 |
" |
|
3230 |
'hello' startsWithDigit |
|
3231 |
'12hello' startsWithDigit |
|
3232 |
'hello12' startsWithDigit |
|
3233 |
" |
|
3234 |
" |
|
3235 |
'hello' endsWithDigit |
|
3236 |
'12hello' endsWithDigit |
|
3237 |
'hello12' endsWithDigit |
|
3238 |
" |
|
3239 |
||
3240 |
"Created: / 08-06-2019 / 17:22:13 / Claus Gittinger" |
|
9192 | 3241 |
! ! |
3242 |
||
3243 |
!CharacterArray methodsFor:'converting'! |
|
3244 |
||
11881 | 3245 |
asAsciiZ |
3246 |
"if the receiver does not end with a 0-valued character, return a copy of it, |
|
3247 |
with an additional 0-character. Otherwise return the receiver. This is sometimes |
|
3248 |
needed when a string has to be passed to C, which needs 0-terminated strings. |
|
3249 |
Notice, that all singleByte strings are already 0-terminated in ST/X, whereas wide |
|
3250 |
strings are not." |
|
3251 |
||
12658 | 3252 |
(self notEmpty and:[self last codePoint == 0]) ifTrue:[^ self ]. |
11881 | 3253 |
^ self copyWith:(Character value:0). |
3254 |
||
3255 |
" |
|
3256 |
'abc' asAsciiZ |
|
3257 |
'abc' asWideString asAsciiZ |
|
3258 |
" |
|
3259 |
! |
|
3260 |
||
13524 | 3261 |
asByteArray |
15936 | 3262 |
"depending on the size of the characters in the receiver, |
3263 |
return a byteArray containing single-, double- or even 4-bytewise values. |
|
3264 |
The size of the returned byteArray will be the strings size multiplied by the |
|
3265 |
size required for the largest character. |
|
19233 | 3266 |
Attention: The bytes are in native byte order. |
21745 | 3267 |
Caveat: better use utf8Encoded, to get reproducible results" |
15936 | 3268 |
|
14555 | 3269 |
|bytes sz bytesPerCharacter idx str| |
3270 |
||
3271 |
str := self string. |
|
3272 |
str ~~ self ifTrue:[ |
|
20874 | 3273 |
"/ for text and other wrappers |
3274 |
^ str asByteArray |
|
13611
ba8c7372218a
variable renamed in: #inspector2TabBytes
Claus Gittinger <cg@exept.de>
parents:
13524
diff
changeset
|
3275 |
]. |
ba8c7372218a
variable renamed in: #inspector2TabBytes
Claus Gittinger <cg@exept.de>
parents:
13524
diff
changeset
|
3276 |
|
ba8c7372218a
variable renamed in: #inspector2TabBytes
Claus Gittinger <cg@exept.de>
parents:
13524
diff
changeset
|
3277 |
"/ for real strings, a fallback |
13524 | 3278 |
sz := self size. |
19757 | 3279 |
bytesPerCharacter := self bytesPerCharacter. |
13524 | 3280 |
bytes := ByteArray new:(sz * bytesPerCharacter). |
3281 |
idx := 1. |
|
3282 |
self do:[:char | |
|
20874 | 3283 |
|code| |
3284 |
||
3285 |
code := char codePoint. |
|
3286 |
bytesPerCharacter == 2 ifTrue:[ |
|
3287 |
bytes unsignedInt16At:idx put:code |
|
3288 |
] ifFalse:[ |
|
3289 |
bytesPerCharacter == 4 ifTrue:[ |
|
3290 |
bytes unsignedInt32At:idx put:code |
|
3291 |
] ifFalse:[ |
|
3292 |
bytes at:idx put:code |
|
3293 |
]. |
|
3294 |
]. |
|
3295 |
idx := idx + bytesPerCharacter. |
|
13524 | 3296 |
]. |
3297 |
^ bytes |
|
3298 |
||
3299 |
"Created: / 27-07-2011 / 00:56:17 / cg" |
|
21745 | 3300 |
"Modified (comment): / 11-05-2017 / 09:21:57 / mawalch" |
13524 | 3301 |
! |
3302 |
||
19233 | 3303 |
asByteArrayMSB:msb |
3304 |
"depending on the size of the characters in the receiver, |
|
3305 |
return a byteArray containing single-, double- or even 4-bytewise values. |
|
3306 |
The size of the returned byteArray will be the strings size multiplied by the |
|
3307 |
size required for the largest character. |
|
21745 | 3308 |
Caveat: better use utf8Encoded, to get reproducible results" |
19233 | 3309 |
|
3310 |
|ba| |
|
3311 |
||
3312 |
ba := self asByteArray. "/ native order |
|
3313 |
UninterpretedBytes isBigEndian ~~ msb ifTrue:[ |
|
20874 | 3314 |
ba swapBytes |
19233 | 3315 |
]. |
3316 |
^ ba |
|
21745 | 3317 |
|
3318 |
"Modified (comment): / 11-05-2017 / 09:22:01 / mawalch" |
|
19233 | 3319 |
! |
3320 |
||
20480 | 3321 |
asCanonicalizedFilename |
3322 |
"return a Filename with pathname taken from the receiver. |
|
3323 |
The filename is canonicalized, meaning that it cares for trailing directory separators, |
|
3324 |
'.' components etc." |
|
3325 |
||
3326 |
^ self asFilename asCanonicalizedFilename |
|
3327 |
||
3328 |
"on windows: |
|
3329 |
'c:\foo\bar' asFilename |
|
3330 |
'c:\foo\bar\' asFilename |
|
3331 |
'c:\foo\bar\..\baz' asFilename |
|
3332 |
'c:\foo\bar\..\baz\.' asFilename |
|
3333 |
'c:\foo\bar' asCanonicalizedFilename |
|
3334 |
'c:\foo\bar\' asCanonicalizedFilename |
|
3335 |
'c:\foo\bar\..\baz' asCanonicalizedFilename |
|
3336 |
'c:\foo\bar\..\baz\.' asCanonicalizedFilename |
|
3337 |
||
3338 |
on unix: |
|
3339 |
'/foo/bar' asFilename |
|
3340 |
'/foo/bar/' asFilename |
|
3341 |
'/foo/bar/../baz' asFilename |
|
3342 |
'/foo/bar/../baz/.' asFilename |
|
3343 |
'/foo/bar' asCanonicalizedFilename |
|
3344 |
'/foo/bar/' asCanonicalizedFilename |
|
3345 |
'/foo/bar/../baz' asCanonicalizedFilename |
|
3346 |
'/foo/bar/../baz/.' asCanonicalizedFilename |
|
3347 |
" |
|
3348 |
! |
|
3349 |
||
608 | 3350 |
asCollectionOfLines |
23187 | 3351 |
"return a collection containing the lines (separated by cr) of the receiver. |
3352 |
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
|
3353 |
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
|
3354 |
See also #asCollectionOfLinesWithReturn |
d7c3e67647f1
comment/format in: #asCollectionOfLines
Claus Gittinger <cg@exept.de>
parents:
14455
diff
changeset
|
3355 |
(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
|
3356 |
this method and we did not want to risk any incompatibilities)" |
14455
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3357 |
|
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3358 |
^ self asCollectionOfSubstringsSeparatedBy:Character cr. |
608 | 3359 |
|
3360 |
" |
|
3361 |
'1 one\2 two\3 three\4 four\5 five' withCRs asCollectionOfLines |
|
8874 | 3362 |
'1 one\\\\2 two\3 three' withCRs asCollectionOfLines |
20182 | 3363 |
|
3364 |
('foo \r\nbar\nbaz\t\r\n\r\nbla' printf:#()) |
|
3365 |
asCollectionOfLines collect:[:l | (l endsWith:Character return) ifTrue:[l copyButLast:1] ifFalse:l] |
|
608 | 3366 |
" |
23187 | 3367 |
|
3368 |
"Modified (comment): / 03-07-2018 / 10:59:36 / Claus Gittinger" |
|
608 | 3369 |
! |
3370 |
||
14455
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3371 |
asCollectionOfLinesWithReturn |
23187 | 3372 |
"return a collection containing the lines (separated by cr) of the receiver. |
3373 |
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
|
3374 |
|
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3375 |
|lines| |
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3376 |
|
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3377 |
lines := self asCollectionOfSubstringsSeparatedBy:Character cr. |
18194 | 3378 |
(lines notEmpty and:[lines last isEmpty]) ifTrue:[ |
20874 | 3379 |
^ lines copyButLast:1 |
14455
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3380 |
]. |
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3381 |
^ lines |
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3382 |
|
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3383 |
" |
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3384 |
'1\2\3' withCRs asCollectionOfLines |
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3385 |
'1\2\3\' withCRs asCollectionOfLines |
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3386 |
'1\2\3' withCRs asCollectionOfLinesWithReturn |
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3387 |
'1\2\3\' withCRs asCollectionOfLinesWithReturn |
18194 | 3388 |
'' withCRs asCollectionOfLinesWithReturn |
14455
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3389 |
" |
23187 | 3390 |
|
3391 |
"Modified (comment): / 03-07-2018 / 10:59:47 / Claus Gittinger" |
|
14455
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3392 |
! |
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3393 |
|
608 | 3394 |
asCollectionOfSubstringsSeparatedBy:aCharacter |
23187 | 3395 |
"return a collection containing substrings (separated by aCharacter) of the receiver. |
15740 | 3396 |
If aCharacter occurs multiple times in a row, the result will contain empty strings. |
23717 | 3397 |
If the receiver starts with aCharacter, an empty string with be the first result element. |
3398 |
If the receiver ends with aCharacter, NO empty string with be the last result element." |
|
608 | 3399 |
|
9192 | 3400 |
^ self asCollectionOfSubCollectionsSeparatedBy:aCharacter |
3401 |
||
3402 |
" |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
3403 |
'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
|
3404 |
'1 one:2 two:3 three:4 four:5 five:' asCollectionOfSubstringsSeparatedBy:$: |
15740 | 3405 |
'1 one 2 two 3 three 4 four 5 five' asCollectionOfSubstringsSeparatedBy:Character space |
9192 | 3406 |
" |
23187 | 3407 |
|
23717 | 3408 |
"Modified (comment): / 11-02-2019 / 23:54:20 / Claus Gittinger" |
9192 | 3409 |
! |
608 | 3410 |
|
11828 | 3411 |
asCollectionOfSubstringsSeparatedBy:aCharacter exceptIn:ch |
23187 | 3412 |
"return a collection containing the substrings (separated by aCharacter) of the receiver. |
3413 |
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
|
3414 |
The separation is not done, inside a matching pair of ch-substrings. |
15740 | 3415 |
Can be used to tokenize csv-like strings, which may or may not be enclosed in quotes." |
11828 | 3416 |
|
3417 |
|lines myClass except i c |
|
3418 |
startIndex "{ Class:SmallInteger }" |
|
3419 |
stopIndex "{ Class:SmallInteger }" | |
|
3420 |
||
11905
cbc2191cfe2d
return stringCollections from asCollectionOfXXX
Claus Gittinger <cg@exept.de>
parents:
11904
diff
changeset
|
3421 |
lines := StringCollection new. |
11828 | 3422 |
myClass := self species. |
3423 |
||
3424 |
startIndex := 1. |
|
3425 |
except := false. |
|
3426 |
[ |
|
20874 | 3427 |
i := startIndex-1. |
3428 |
[ |
|
3429 |
i := i+1. |
|
3430 |
c := self at:i. |
|
3431 |
c = ch ifTrue:[ except := except not. ]. |
|
3432 |
i < self size and:[except or:[c ~= aCharacter]] |
|
3433 |
] whileTrue. |
|
3434 |
||
3435 |
c = aCharacter ifTrue:[ |
|
3436 |
stopIndex := i -1. |
|
3437 |
] ifFalse: [ |
|
3438 |
stopIndex := i. |
|
3439 |
]. |
|
3440 |
(stopIndex < startIndex) ifTrue: [ |
|
3441 |
lines add:(myClass new:0) |
|
3442 |
] ifFalse: [ |
|
3443 |
lines add:(self copyFrom:startIndex to:stopIndex) |
|
3444 |
]. |
|
3445 |
startIndex := stopIndex + 2. |
|
3446 |
startIndex <= self size |
|
11828 | 3447 |
] whileTrue. |
3448 |
^ lines |
|
3449 |
||
3450 |
" |
|
12177 | 3451 |
'asd''f;d''dd;s' asCollectionOfSubstringsSeparatedBy:$; exceptIn:$' |
3452 |
" |
|
3453 |
"/ 'asd "hello bla" foo "bla bla" bar' asCollectionOfSubstringsSeparatedBy:$ exceptIn:$" |
|
23187 | 3454 |
|
3455 |
"Modified (comment): / 03-07-2018 / 11:00:02 / Claus Gittinger" |
|
11828 | 3456 |
! |
3457 |
||
13344
ec6860c49990
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13343
diff
changeset
|
3458 |
asCollectionOfSubstringsSeparatedBy:aFieldSeparatorString textSeparator:aTextSeparatorOrNil |
13343
3f058ad0d2f1
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13332
diff
changeset
|
3459 |
"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
|
3460 |
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
|
3461 |
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
|
3462 |
|
13344
ec6860c49990
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13343
diff
changeset
|
3463 |
|aTextSeparatorChar items scanningWord inStream element lastIsFieldSeparator sz| |
ec6860c49990
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13343
diff
changeset
|
3464 |
|
ec6860c49990
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13343
diff
changeset
|
3465 |
aTextSeparatorOrNil isNil ifTrue:[ |
19961 | 3466 |
^ self asCollectionOfSubstringsSeparatedByAll: aFieldSeparatorString |
13327
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3467 |
]. |
13344
ec6860c49990
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13343
diff
changeset
|
3468 |
sz := aTextSeparatorOrNil size. |
ec6860c49990
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13343
diff
changeset
|
3469 |
sz = 0 ifTrue:[ |
19961 | 3470 |
aTextSeparatorChar := aTextSeparatorOrNil |
13344
ec6860c49990
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13343
diff
changeset
|
3471 |
] ifFalse:[sz = 1 ifTrue:[ |
19961 | 3472 |
"this is a String. Fetch the first character - compatibility to older expecco libs" |
3473 |
aTextSeparatorChar := aTextSeparatorOrNil first. |
|
13344
ec6860c49990
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13343
diff
changeset
|
3474 |
] ifFalse:[ |
19961 | 3475 |
self error:'textSeparatorSize > 1'. |
13344
ec6860c49990
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13343
diff
changeset
|
3476 |
]]. |
13327
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3477 |
|
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3478 |
items := OrderedCollection new. |
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3479 |
|
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3480 |
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
|
3481 |
[ |
19961 | 3482 |
inStream skipSeparators. |
3483 |
inStream atEnd |
|
13327
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3484 |
] whileFalse:[ |
19961 | 3485 |
lastIsFieldSeparator := false. |
3486 |
inStream peek == aTextSeparatorChar ifTrue:[ |
|
3487 |
inStream next. |
|
3488 |
element := ''. |
|
3489 |
scanningWord := true. |
|
3490 |
[ scanningWord and:[inStream atEnd not] ] whileTrue:[ |
|
3491 |
element := element , (inStream upTo:aTextSeparatorChar). |
|
3492 |
(inStream peek == aTextSeparatorChar) ifTrue:[ |
|
3493 |
element := element , aTextSeparatorChar . |
|
3494 |
inStream next. |
|
3495 |
] ifFalse:[ |
|
3496 |
scanningWord := false. |
|
3497 |
]. |
|
3498 |
]. |
|
3499 |
inStream upToAll_positionBefore:aFieldSeparatorString. |
|
3500 |
] ifFalse:[ |
|
3501 |
element := inStream upToAll_positionBefore:aFieldSeparatorString |
|
3502 |
]. |
|
3503 |
items add:element. |
|
3504 |
lastIsFieldSeparator := (inStream skipThroughAll:aFieldSeparatorString) notNil. |
|
13343
3f058ad0d2f1
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13332
diff
changeset
|
3505 |
]. |
3f058ad0d2f1
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13332
diff
changeset
|
3506 |
lastIsFieldSeparator ifTrue:[ |
19961 | 3507 |
"empty element at the end of the line" |
3508 |
items add:''. |
|
13327
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3509 |
]. |
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3510 |
|
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3511 |
^ items |
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3512 |
|
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3513 |
" |
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3514 |
self assert:(('#First#, #Second,SecondAdd#, #Third#' asCollectionOfSubstringsSeparatedBy:',' textSeparator: $#) |
19961 | 3515 |
sameContentsAs:#('First' 'Second,SecondAdd' 'Third')). |
13327
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3516 |
self assert:(('#Fir##st#, #Second,SecondAdd#, #Third#' asCollectionOfSubstringsSeparatedBy:',' textSeparator: $#) |
19961 | 3517 |
sameContentsAs:#('Fir#st' 'Second,SecondAdd' 'Third')). |
13327
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3518 |
self assert:(('#Fir##st#, Second,SecondAdd, #Third#' asCollectionOfSubstringsSeparatedBy:',' textSeparator: $#) |
19961 | 3519 |
sameContentsAs:#('Fir#st' 'Second' 'SecondAdd' 'Third')). |
13343
3f058ad0d2f1
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13332
diff
changeset
|
3520 |
self assert:(('First,Second,Third,,' asCollectionOfSubstringsSeparatedBy:',' textSeparator:nil) |
19961 | 3521 |
sameContentsAs:#('First' 'Second' 'Third' '' '')). |
13344
ec6860c49990
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13343
diff
changeset
|
3522 |
self assert:(('First,Second,Third,,' asCollectionOfSubstringsSeparatedBy:',' textSeparator:'#') |
19961 | 3523 |
sameContentsAs:#('First' 'Second' 'Third' '' '')). |
13327
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3524 |
" |
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3525 |
|
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3526 |
"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
|
3527 |
! |
999589445aa7
add new method for splitting a csv string into csv elements
ab
parents:
10437
diff
changeset
|
3528 |
|
7339
20969b5770c9
asCollectionOfSubstringsSeparatedByAll:
Claus Gittinger <cg@exept.de>
parents:
7318
diff
changeset
|
3529 |
asCollectionOfSubstringsSeparatedByAll:aSeparatorString |
23187 | 3530 |
"return a collection containing the lines (separated by aSeparatorString) of the receiver. |
3531 |
If aSeparatorString occurs multiple times in a row, |
|
7339
20969b5770c9
asCollectionOfSubstringsSeparatedByAll:
Claus Gittinger <cg@exept.de>
parents:
7318
diff
changeset
|
3532 |
the result will contain empty strings." |
20969b5770c9
asCollectionOfSubstringsSeparatedByAll:
Claus Gittinger <cg@exept.de>
parents:
7318
diff
changeset
|
3533 |
|
8852
9f5984eff2c2
use asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8794
diff
changeset
|
3534 |
^ self asCollectionOfSubCollectionsSeparatedByAll:aSeparatorString |
9f5984eff2c2
use asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8794
diff
changeset
|
3535 |
|
9f5984eff2c2
use asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8794
diff
changeset
|
3536 |
" |
8874 | 3537 |
'1::2::3::4::5::' asCollectionOfSubstringsSeparatedByAll:'::' |
7339
20969b5770c9
asCollectionOfSubstringsSeparatedByAll:
Claus Gittinger <cg@exept.de>
parents:
7318
diff
changeset
|
3538 |
" |
23187 | 3539 |
|
3540 |
"Modified (comment): / 03-07-2018 / 11:00:16 / Claus Gittinger" |
|
7339
20969b5770c9
asCollectionOfSubstringsSeparatedByAll:
Claus Gittinger <cg@exept.de>
parents:
7318
diff
changeset
|
3541 |
! |
20969b5770c9
asCollectionOfSubstringsSeparatedByAll:
Claus Gittinger <cg@exept.de>
parents:
7318
diff
changeset
|
3542 |
|
608 | 3543 |
asCollectionOfSubstringsSeparatedByAny:aCollectionOfSeparators |
3544 |
"return a collection containing the words (separated by any character |
|
3545 |
from aCollectionOfSeparators) of the receiver. |
|
3546 |
This allows breaking up strings using any character as separator." |
|
3547 |
||
8852
9f5984eff2c2
use asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8794
diff
changeset
|
3548 |
^ self asCollectionOfSubCollectionsSeparatedByAny:aCollectionOfSeparators |
608 | 3549 |
|
3550 |
" |
|
3551 |
'hello:world:isnt:this nice' asCollectionOfSubstringsSeparatedByAny:#($:) |
|
8874 | 3552 |
'hello:world:isnt:this nice' asCollectionOfSubstringsSeparatedByAny:':' |
3553 |
'hello:world:isnt:this nice' asCollectionOfSubstringsSeparatedByAny:(Array with:$: with:Character space) |
|
3554 |
'hello:world:isnt:this nice' asCollectionOfSubstringsSeparatedByAny:': ' |
|
3555 |
'h1e2l3l4o' asCollectionOfSubstringsSeparatedByAny:($1 to: $9) |
|
608 | 3556 |
" |
3557 |
! |
|
3558 |
||
3559 |
asCollectionOfWords |
|
8874 | 3560 |
"return a collection containing the words (separated by whitespace) |
3561 |
of the receiver. Multiple occurrences of whitespace characters will |
|
17517 | 3562 |
be treated like one - i.e. whitespace is skipped. |
3563 |
(sigh: it is called #'subStrings' in V'Age, and #'substrings' in Squeak) " |
|
608 | 3564 |
|
7797 | 3565 |
|words| |
608 | 3566 |
|
11905
cbc2191cfe2d
return stringCollections from asCollectionOfXXX
Claus Gittinger <cg@exept.de>
parents:
11904
diff
changeset
|
3567 |
words := StringCollection new. |
7797 | 3568 |
self asCollectionOfWordsDo:[:w | words add:w]. |
608 | 3569 |
^ words |
3570 |
||
3571 |
" |
|
3572 |
'hello world isnt this nice' asCollectionOfWords |
|
23660 | 3573 |
'hello world isnt this nice' asCollectionOfWordsDo:#transcribeCR |
608 | 3574 |
' hello world isnt this nice ' asCollectionOfWords |
3575 |
'hello' asCollectionOfWords |
|
3576 |
'' asCollectionOfWords |
|
3577 |
' ' asCollectionOfWords |
|
10703
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
3578 |
' foo bar__baz__bla__ bar ' asCollectionOfWords |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
3579 |
' foo __bar__baz__bla__ bar ' asCollectionOfWords |
608 | 3580 |
" |
23660 | 3581 |
|
3582 |
"Modified (comment): / 03-02-2019 / 13:07:57 / Claus Gittinger" |
|
9192 | 3583 |
! |
3584 |
||
3585 |
asCollectionOfWordsDo:aBlock |
|
8874 | 3586 |
"evaluate aBlock for each word (separated by whitespace) of the receiver. |
3587 |
Multiple occurrences of whitespace characters will be treated like one |
|
7797 | 3588 |
- i.e. whitespace is skipped. |
3589 |
Returns the number of words (i.e. the number of invocations of aBlock)." |
|
3590 |
||
3591 |
|count "{ Class:SmallInteger }" |
|
8874 | 3592 |
start "{ Class:SmallInteger }" |
3593 |
stop "{ Class:SmallInteger }" |
|
7797 | 3594 |
mySize "{ Class:SmallInteger }"| |
3595 |
||
3596 |
count := 0. |
|
3597 |
start := 1. |
|
3598 |
mySize := self size. |
|
3599 |
[start <= mySize] whileTrue:[ |
|
20874 | 3600 |
start := self indexOfNonSeparatorStartingAt:start. |
3601 |
start == 0 ifTrue:[ |
|
3602 |
^ count |
|
3603 |
]. |
|
3604 |
stop := self indexOfSeparatorStartingAt:start. |
|
3605 |
stop == 0 ifTrue:[ |
|
3606 |
aBlock value:(self copyFrom:start to:mySize). |
|
3607 |
^ count + 1 |
|
3608 |
]. |
|
3609 |
aBlock value:(self copyFrom:start to:(stop - 1)). |
|
3610 |
start := stop. |
|
3611 |
count := count + 1 |
|
7797 | 3612 |
]. |
3613 |
^ count |
|
3614 |
||
3615 |
" |
|
3616 |
'hello world isnt this nice' asCollectionOfWordsDo:[:w | Transcript showCR:w] |
|
23660 | 3617 |
'hello world isnt this nice' asCollectionOfWordsDo:#transcribeCR |
7797 | 3618 |
' hello world isnt this nice ' asCollectionOfWordsDo:[:w | Transcript showCR:w] |
3619 |
'hello' asCollectionOfWordsDo:[:w | Transcript showCR:w] |
|
3620 |
'' asCollectionOfWordsDo:[:w | Transcript showCR:w] |
|
3621 |
' ' asCollectionOfWordsDo:[:w | Transcript showCR:w] |
|
3622 |
" |
|
23660 | 3623 |
|
3624 |
"Modified (comment): / 03-02-2019 / 13:08:15 / Claus Gittinger" |
|
9192 | 3625 |
! |
3626 |
||
23971 | 3627 |
asDenseUnicodeString |
3628 |
"return the receiver as single-byte, double byte or 4-byte unicode string, |
|
3629 |
depending on the number of bits required to hold all characters in myself. |
|
3630 |
Use this to extract non-wide parts from a wide string, |
|
3631 |
i.e. after a substring has been copied out of a wide string" |
|
3632 |
||
3633 |
|nb| |
|
3634 |
||
3635 |
nb := self bytesPerCharacterNeeded. |
|
24034 | 3636 |
(nb ~~ self bytesPerCharacter) ifTrue:[ |
3637 |
nb == 1 ifTrue:[ |
|
3638 |
^ self asSingleByteString |
|
3639 |
]. |
|
3640 |
nb == 2 ifTrue:[ |
|
3641 |
^ self asUnicode16String |
|
23971 | 3642 |
]. |
3643 |
]. |
|
3644 |
^ self |
|
3645 |
||
3646 |
" |
|
3647 |
'abc' asUnicode16String asDenseUnicodeString |
|
3648 |
'abc' asUnicode32String asDenseUnicodeString |
|
3649 |
('abc',(Character value:16r165)) asDenseUnicodeString |
|
3650 |
('abc',(Character value:16r165)) asUnicode32String asDenseUnicodeString |
|
3651 |
" |
|
3652 |
||
3653 |
"Created: / 25-03-2019 / 16:28:02 / Claus Gittinger" |
|
24034 | 3654 |
"Modified: / 02-04-2019 / 10:48:44 / Claus Gittinger" |
23971 | 3655 |
! |
3656 |
||
9192 | 3657 |
asFilename |
3658 |
"return a Filename with pathname taken from the receiver" |
|
3659 |
||
3660 |
^ Filename named:self "(self asSingleByteStringReplaceInvalidWith:$?)" |
|
3661 |
||
3662 |
"Modified: 20.5.1996 / 09:38:15 / cg" |
|
3663 |
! |
|
608 | 3664 |
|
3060 | 3665 |
asFixedPoint |
3666 |
"read a fixedPoint number from the receiver. |
|
8874 | 3667 |
Notice, that errors may occur during the read, |
7423 | 3668 |
so you better setup some signal handler when using this method." |
3060 | 3669 |
|
3670 |
^ FixedPoint readFromString:self |
|
3671 |
||
3672 |
" |
|
8874 | 3673 |
'0.123' asFixedPoint |
3674 |
'12345' asFixedPoint |
|
3060 | 3675 |
'(1/5)' asFixedPoint |
3676 |
'foo' asFixedPoint |
|
9192 | 3677 |
Object errorSignal handle:[:ex | ex return:0] do:['foo' asFixedPoint] |
3678 |
" |
|
3679 |
||
3680 |
"Modified: / 25.10.1997 / 15:19:00 / cg" |
|
3681 |
! |
|
3682 |
||
3683 |
asFixedPoint:scale |
|
3060 | 3684 |
"read a fixedPoint number with scale number of post-decimal digits |
3685 |
from the receiver. Scale controls the number of displayed digits, |
|
3686 |
not the number of actually valid digits. |
|
8874 | 3687 |
Notice, that errors may occur during the read, |
7423 | 3688 |
so you better setup some signal handler when using this method." |
3060 | 3689 |
|
3690 |
^ (FixedPoint readFromString:self) scale:scale |
|
3691 |
||
3692 |
" |
|
8874 | 3693 |
'0.123' asFixedPoint:2 |
3694 |
'123456' asFixedPoint:2 |
|
3695 |
('3.14157' asFixedPoint:1) asFixedPoint:5 |
|
3696 |
'3.14157' asFixedPoint:2 |
|
3697 |
'foo' asFixedPoint:2 |
|
3060 | 3698 |
" |
3699 |
||
3700 |
"Modified: / 25.10.1997 / 15:21:57 / cg" |
|
9192 | 3701 |
! |
3702 |
||
3703 |
asFloat |
|
608 | 3704 |
"read a float number from the receiver. |
8874 | 3705 |
Notice, that errors may occur during the read, |
7422 | 3706 |
so you better setup some exception handler when using this method." |
608 | 3707 |
|
3708 |
^ (Number readFromString:self) asFloat |
|
3709 |
||
3710 |
" |
|
8874 | 3711 |
'0.123' asFloat |
608 | 3712 |
'12345' asFloat |
3713 |
'(1/5)' asFloat |
|
8874 | 3714 |
Object errorSignal handle:[:ex | ex return:0] do:['foo' asFloat] |
608 | 3715 |
" |
3716 |
! |
|
3717 |
||
3718 |
asInteger |
|
9190 | 3719 |
"convert the receiver into an integer. |
8874 | 3720 |
Notice, that errors may occur during the read, |
9190 | 3721 |
so you better setup some exception handler when using this method. |
3722 |
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
|
3723 |
in other smalltalks. |
9190 | 3724 |
For less strict integer reading, use Integer readFrom:aString" |
608 | 3725 |
|
3726 |
^ Integer readFromString:self |
|
3727 |
||
3728 |
" |
|
3729 |
'12345678901234567890' asInteger |
|
3730 |
'-1234' asInteger |
|
9190 | 3731 |
|
3732 |
The following raises an error: |
|
20874 | 3733 |
'0.123' asInteger <- reader finds more after reading 0 |
9190 | 3734 |
|
3735 |
whereas the less strict readFrom does not: |
|
20874 | 3736 |
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
|
3737 |
|
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
3738 |
'0.123' asInteger |
608 | 3739 |
'0.123' asNumber <- returns what you expect |
8874 | 3740 |
Object errorSignal handle:[:ex | ex return:0] do:['foo' asInteger] |
9190 | 3741 |
|
3742 |
" |
|
9192 | 3743 |
! |
3744 |
||
3745 |
asLowercase |
|
608 | 3746 |
"return a copy of myself in lowercase letters" |
3747 |
||
8003
6e690f4b79f1
asLowercase/asUppercase and friends care for 16-bit chars
Claus Gittinger <cg@exept.de>
parents:
7995
diff
changeset
|
3748 |
|newStr c bitsPerCharacter |
9192 | 3749 |
mySize "{ Class: SmallInteger }" | |
3750 |
||
3751 |
mySize := self size. |
|
3752 |
mySize == 0 ifTrue:[^ self]. |
|
18657 | 3753 |
|
9192 | 3754 |
newStr := self species new:mySize. |
3755 |
bitsPerCharacter := newStr bitsPerCharacter. |
|
3756 |
||
18657 | 3757 |
"/ handle the very seldom case of an uppercase char which needs |
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
3758 |
"/ more bits in its lowercase variant |
18657 | 3759 |
"/ (there are only a few of them) |
3760 |
||
9192 | 3761 |
1 to:mySize do:[:i | |
20705 | 3762 |
c := (self at:i) asLowercase. |
3763 |
(c bitsPerCharacter > bitsPerCharacter |
|
3764 |
and:[c stringSpecies ~= newStr stringSpecies]) ifTrue:[ |
|
3765 |
newStr := c stringSpecies fromString:newStr. |
|
3766 |
bitsPerCharacter := newStr bitsPerCharacter. |
|
3767 |
]. |
|
3768 |
newStr at:i put:c |
|
9192 | 3769 |
]. |
3770 |
^ newStr |
|
3771 |
||
3772 |
" |
|
3773 |
'HelloWorld' asLowercase |
|
18662 | 3774 |
'HelloWorld' asUnicode16String asLowercase |
9192 | 3775 |
'HelloWorld' asLowercaseFirst |
18657 | 3776 |
'HelloWorld' asUppercase |
20705 | 3777 |
'HelloWorldÿ' asUppercase |
3778 |
'HelloWorldŸ' asLowercase - currently returns an U16 string; should this be u8? |
|
9192 | 3779 |
" |
3780 |
! |
|
3781 |
||
3782 |
asLowercaseFirst |
|
17508 | 3783 |
"return a copy of myself where the first character is converted to lowercase. |
3784 |
If the first character is already lowercase, or there is no uppercase for it, return the |
|
3785 |
receiver." |
|
9192 | 3786 |
|
3787 |
|newString firstChar firstCharAsLowercase| |
|
3788 |
||
11525 | 3789 |
self isEmpty ifTrue:[^ self]. |
9192 | 3790 |
firstChar := (self at:1). |
3791 |
firstCharAsLowercase := firstChar asLowercase. |
|
3792 |
firstChar == firstCharAsLowercase ifTrue:[ ^ self]. |
|
3793 |
||
3794 |
firstCharAsLowercase bitsPerCharacter > self bitsPerCharacter ifTrue:[ |
|
20874 | 3795 |
newString := firstCharAsLowercase stringSpecies fromString:self. |
9192 | 3796 |
] ifFalse:[ |
20874 | 3797 |
newString := self stringSpecies fromString:self. |
9192 | 3798 |
]. |
9190 | 3799 |
newString at:1 put:firstCharAsLowercase. |
3800 |
^ newString |
|
608 | 3801 |
|
3802 |
" |
|
8874 | 3803 |
'HelloWorld' asLowercase |
3804 |
'HelloWorld' asLowercaseFirst |
|
608 | 3805 |
" |
3806 |
! |
|
3807 |
||
11828 | 3808 |
asLowercaseLast |
3809 |
"return a copy of myself where the last character is |
|
3810 |
converted to lowercase." |
|
3811 |
||
3812 |
|newString sz| |
|
3813 |
||
3814 |
sz := self size. |
|
3815 |
newString := self copyFrom:1 to:sz. |
|
3816 |
sz > 0 ifTrue:[ |
|
20874 | 3817 |
newString at:sz put:(newString at:sz) asLowercase |
11828 | 3818 |
]. |
3819 |
^ newString |
|
3820 |
||
3821 |
" |
|
12177 | 3822 |
'HelloWorld' asLowercase |
3823 |
'HelloWorlD' asLowercaseLast |
|
11828 | 3824 |
" |
3825 |
! |
|
3826 |
||
17644 | 3827 |
asMutator |
19473 | 3828 |
"return a corresponding setter method's selector. |
3829 |
I.e. #foo asMutator returns #foo:" |
|
17644 | 3830 |
|
20873 | 3831 |
^ (self asSingleByteString,':') asSymbol |
17644 | 3832 |
! |
3833 |
||
9190 | 3834 |
asNumber |
3835 |
"read a number from the receiver. |
|
3836 |
Notice, that (in contrast to ST-80) errors may occur during the read, |
|
3837 |
so you better setup some signal handler when using this method. |
|
3838 |
Also notice, that this is meant to read end-user numbers from a string; |
|
9135 | 3839 |
it does not handle smalltalk numbers (i.e. radix). |
3840 |
To read a smalltalk number, use Number >> readSmalltalkFrom:. |
|
608 | 3841 |
This may change if ANSI specifies it." |
3842 |
||
3843 |
"/ ST-80 behavior: |
|
3844 |
"/ ^ Number readFromString:self onError:0 |
|
3845 |
||
17061 | 3846 |
^ Number fromString:self |
3847 |
||
3848 |
" |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
3849 |
'123' asNumber |
608 | 3850 |
'123.567' asNumber |
3851 |
'(5/6)' asNumber |
|
3852 |
'foo' asNumber |
|
17061 | 3853 |
'123a' asNumber |
8874 | 3854 |
Object errorSignal handle:[:ex | ex returnWith:0] do:['foo' asNumber] |
608 | 3855 |
" |
9192 | 3856 |
! |
3857 |
||
3858 |
asNumberFromFormatString:ignored |
|
608 | 3859 |
"read a number from the receiver, ignoring any nonDigit characters. |
3860 |
This is typically used to convert from strings which include |
|
3861 |
dollar-signs or millenium digits. However, this method also ignores |
|
3862 |
the decimal point (if any) and therefore should be used with care." |
|
3863 |
||
3864 |
|tempString| |
|
3865 |
||
3866 |
tempString := self collect:[:char | char isDigit]. |
|
3867 |
^ Number readFromString:tempString onError:0 |
|
3868 |
||
3869 |
" |
|
3870 |
'USD 123' asNumberFromFormatString:'foo' |
|
3871 |
'DM 123' asNumberFromFormatString:'foo' |
|
3872 |
'123' asNumberFromFormatString:'foo' |
|
3873 |
'123.567' asNumberFromFormatString:'foo' |
|
3874 |
'(5/6)' asNumberFromFormatString:'foo' |
|
3875 |
'foo' asNumberFromFormatString:'foo' |
|
3876 |
" |
|
9192 | 3877 |
! |
3878 |
||
21994 | 3879 |
asPackageId |
3880 |
"given a package-string as receiver, return a packageId object. |
|
3881 |
packageIds hide the details of module/directory handling inside the path. |
|
3882 |
See PackageId for the required format of those strings." |
|
3883 |
||
3884 |
^ PackageId from: self string |
|
3885 |
||
21995 | 3886 |
" |
3887 |
'stx:libbasic' asPackageId |
|
3888 |
" |
|
3889 |
||
21994 | 3890 |
"Created: / 11-07-2017 / 18:24:47 / cg" |
3891 |
! |
|
3892 |
||
7977 | 3893 |
asSingleByteStringIfPossible |
3894 |
"if possible, return the receiver converted to a 'normal' string. |
|
18181 | 3895 |
It is only possible, if there are no characters with codePoints above 255 in the receiver. |
3896 |
If not possible, the (wideString) receiver is returned." |
|
7977 | 3897 |
|
14555 | 3898 |
self isWideString ifFalse:[^ self]. |
18967 | 3899 |
self containsNon8BitElements ifTrue:[^ self]. |
3900 |
^ self asSingleByteString |
|
9192 | 3901 |
|
3902 |
" |
|
3903 |
'hello' asSingleByteStringIfPossible |
|
18967 | 3904 |
'hello' asText asSingleByteStringIfPossible |
3905 |
'hello' asUnicodeString asText asSingleByteStringIfPossible |
|
9192 | 3906 |
'hello' asUnicodeString asSingleByteStringIfPossible |
3907 |
" |
|
3908 |
! |
|
3909 |
||
3910 |
asSingleByteStringReplaceInvalidWith:replacementCharacter |
|
3911 |
"return the receiver converted to a 'normal' string, |
|
3912 |
with invalid characters replaced by replacementCharacter. |
|
3913 |
Can be used to convert from 16-bit strings to 8-bit strings |
|
3914 |
and replace characters above code-255 with some replacement." |
|
3915 |
||
3916 |
|newString| |
|
3917 |
||
3918 |
newString := String new:(self size). |
|
3919 |
1 to:self size do:[:idx | |
|
20874 | 3920 |
|char| |
3921 |
||
3922 |
char := self at:idx. |
|
3923 |
char codePoint <= 16rFF ifTrue:[ |
|
3924 |
newString at:idx put:char |
|
3925 |
] ifFalse:[ |
|
3926 |
newString at:idx put:replacementCharacter |
|
3927 |
]. |
|
9190 | 3928 |
]. |
3929 |
^ newString |
|
3930 |
||
3931 |
"Created: 30.6.1997 / 13:02:14 / cg" |
|
7977 | 3932 |
! |
3933 |
||
9192 | 3934 |
asString |
3935 |
"return myself - I am a string" |
|
3936 |
||
3937 |
^ self |
|
3938 |
! |
|
3939 |
||
3940 |
asStringCollection |
|
3941 |
"return a collection of lines from myself." |
|
3942 |
||
3943 |
^ StringCollection fromString:self "string" |
|
3944 |
||
21639 | 3945 |
" |
3946 |
'hello\world\1\2\3' withCRs asStringCollection first |
|
3947 |
" |
|
3948 |
||
3949 |
"Modified: / 13-05-1996 / 20:36:59 / cg" |
|
3950 |
"Modified (comment): / 13-03-2017 / 14:05:14 / cg" |
|
9192 | 3951 |
! |
1375
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
3952 |
|
24162 | 3953 |
asStringWithBitsPerCharacterAtLeast:numRequiredBitsPerCharacter |
3954 |
"return the receiver in a representation which supports numRequiredBitsPerCharacter. |
|
3955 |
I.e. if required, convert to a Unicode16 or Unicode32 string" |
|
3956 |
||
3957 |
numRequiredBitsPerCharacter > 8 ifTrue:[ |
|
3958 |
numRequiredBitsPerCharacter > 16 ifTrue:[ |
|
3959 |
^ self asUnicode32String |
|
3960 |
]. |
|
3961 |
numRequiredBitsPerCharacter > self bitsPerCharacter ifTrue:[ |
|
3962 |
^ self asUnicode16String |
|
3963 |
]. |
|
3964 |
]. |
|
3965 |
^ self |
|
3966 |
||
3967 |
" |
|
3968 |
self assert:('abc' asStringWithBitsPerCharacterAtLeast:8) bitsPerCharacter == 8. |
|
3969 |
self assert:('abc' asStringWithBitsPerCharacterAtLeast:16) bitsPerCharacter == 16. |
|
3970 |
self assert:('abc' asStringWithBitsPerCharacterAtLeast:32) bitsPerCharacter == 32. |
|
3971 |
||
3972 |
self assert:('abc' asUnicode16String asStringWithBitsPerCharacterAtLeast:8) bitsPerCharacter == 16. |
|
3973 |
self assert:('abc' asUnicode16String asStringWithBitsPerCharacterAtLeast:16) bitsPerCharacter == 16. |
|
3974 |
self assert:('abc' asUnicode16String asStringWithBitsPerCharacterAtLeast:32) bitsPerCharacter == 32. |
|
3975 |
||
3976 |
self assert:('abc' asUnicode32String asStringWithBitsPerCharacterAtLeast:8) bitsPerCharacter == 32. |
|
3977 |
self assert:('abc' asUnicode32String asStringWithBitsPerCharacterAtLeast:16) bitsPerCharacter == 32. |
|
3978 |
self assert:('abc' asUnicode32String asStringWithBitsPerCharacterAtLeast:32) bitsPerCharacter == 32. |
|
3979 |
" |
|
3980 |
||
3981 |
"Created: / 26-05-2019 / 12:47:07 / Claus Gittinger" |
|
3982 |
! |
|
3983 |
||
23311 | 3984 |
asStringWithoutEmphasis |
3985 |
"return myself as a string without any emphasis" |
|
3986 |
"return myself - I am a string" |
|
3987 |
||
3988 |
^ self |
|
3989 |
||
3990 |
"Created: / 29-08-2018 / 09:21:07 / Claus Gittinger" |
|
3991 |
! |
|
3992 |
||
1434 | 3993 |
asSymbol |
19039 | 3994 |
"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
|
3995 |
The receiver must be a singleByte-String. |
ccd0e8f26439
printing - migrating towards unicodeSupport in ST-code
Claus Gittinger <cg@exept.de>
parents:
8040
diff
changeset
|
3996 |
TwoByte- and FourByteSymbols are (currently ?) not allowed." |
ccd0e8f26439
printing - migrating towards unicodeSupport in ST-code
Claus Gittinger <cg@exept.de>
parents:
8040
diff
changeset
|
3997 |
|
ccd0e8f26439
printing - migrating towards unicodeSupport in ST-code
Claus Gittinger <cg@exept.de>
parents:
8040
diff
changeset
|
3998 |
|str| |
ccd0e8f26439
printing - migrating towards unicodeSupport in ST-code
Claus Gittinger <cg@exept.de>
parents:
8040
diff
changeset
|
3999 |
|
9192 | 4000 |
str := self string. |
4001 |
str ~~ self ifTrue:[ ^ str asSymbol ]. |
|
4002 |
^ self asSingleByteString asSymbol |
|
4003 |
! |
|
4004 |
||
4005 |
asSymbolIfInterned |
|
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
4006 |
"If a symbol with the receiver's characters is already known, return it. |
19692 | 4007 |
Otherwise, return nil. |
9230 | 4008 |
This can be used to query for an existing symbol and is the same as: |
20874 | 4009 |
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
|
4010 |
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
|
4011 |
The receiver must be a singleByte-String. |
ccd0e8f26439
printing - migrating towards unicodeSupport in ST-code
Claus Gittinger <cg@exept.de>
parents:
8040
diff
changeset
|
4012 |
TwoByte- and FourByteSymbols are (currently ?) not allowed." |
1435
3ee1fde2aa58
allow asSymbolIfInterned for Texts
Claus Gittinger <cg@exept.de>
parents:
1434
diff
changeset
|
4013 |
|
18967 | 4014 |
|str| |
4015 |
||
4016 |
str := self string. |
|
4017 |
str == self ifTrue:[ |
|
20874 | 4018 |
"must be some kind of N-ByteString" |
4019 |
str := self asSingleByteStringIfPossible. |
|
4020 |
str == self ifTrue:[ |
|
4021 |
"single byte string conversion is not possible" |
|
4022 |
^ nil. |
|
4023 |
]. |
|
6642 | 4024 |
]. |
18967 | 4025 |
^ str asSymbolIfInterned |
4026 |
||
4027 |
" |
|
4028 |
(Unicode16String with:(Character value:16rFFFF)) asSymbolIfInterned |
|
4029 |
'new' asUnicodeString asSymbolIfInterned |
|
4030 |
'new' asText asSymbolIfInterned |
|
4031 |
'new' asUnicodeString asText asSymbolIfInterned |
|
4032 |
" |
|
1435
3ee1fde2aa58
allow asSymbolIfInterned for Texts
Claus Gittinger <cg@exept.de>
parents:
1434
diff
changeset
|
4033 |
|
3ee1fde2aa58
allow asSymbolIfInterned for Texts
Claus Gittinger <cg@exept.de>
parents:
1434
diff
changeset
|
4034 |
"Created: 22.5.1996 / 16:37:04 / cg" |
9192 | 4035 |
! |
4036 |
||
20160 | 4037 |
asSymbolIfInternedOrSelf |
4038 |
"If a symbol with the receiver's characters is already known, return it. |
|
4039 |
Otherwise, return self. |
|
4040 |
This can be used to query for an existing symbol and is the same as: |
|
4041 |
self knownAsSymbol ifTrue:[self asSymbol] ifFalse:[self] |
|
4042 |
but slightly faster, since the symbol lookup operation is only performed once. |
|
4043 |
The receiver must be a singleByte-String. |
|
4044 |
TwoByte- and FourByteSymbols are (currently ?) not allowed." |
|
4045 |
||
4046 |
|sym| |
|
4047 |
||
4048 |
sym := self asSymbolIfInterned. |
|
4049 |
^ sym isNil ifTrue:[self] ifFalse:[sym]. |
|
4050 |
||
4051 |
||
4052 |
" |
|
4053 |
(Unicode16String with:(Character value:16rFFFF)) asSymbolIfInternedOrSelf |
|
4054 |
'new' asUnicodeString asSymbolIfInternedOrSelf |
|
4055 |
'new' asText asSymbolIfInternedOrSelf |
|
4056 |
'new' asUnicodeString asText asSymbolIfInternedOrSelf |
|
4057 |
" |
|
4058 |
||
4059 |
"Created: 22.5.1996 / 16:37:04 / cg" |
|
4060 |
! |
|
4061 |
||
9192 | 4062 |
asText |
9403 | 4063 |
"return a Text-object (string with emphasis) from myself." |
9192 | 4064 |
|
4065 |
"this test allows for small non-gui apps to be built without libbasic2 (where Text is)" |
|
4066 |
Text isNil ifTrue:[^ self]. |
|
17065 | 4067 |
^ Text string:self |
9192 | 4068 |
|
4069 |
"Created: 12.5.1996 / 10:41:14 / cg" |
|
4070 |
! |
|
4071 |
||
23617 | 4072 |
asTimeDuration |
4073 |
"return an TimeDuration object from the parsing the receiver string" |
|
4074 |
||
4075 |
^ TimeDuration readFromString:self |
|
4076 |
||
4077 |
" |
|
4078 |
Time now asTimeDuration |
|
4079 |
10 asTimeDuration |
|
4080 |
10 seconds asTimeDuration |
|
4081 |
'10m 20s' asTimeDuration |
|
4082 |
" |
|
4083 |
||
4084 |
"Created: / 21-01-2019 / 10:29:51 / Claus Gittinger" |
|
4085 |
! |
|
4086 |
||
17021 | 4087 |
asTimestamp |
4088 |
"convert the receiver into an Timestamp. |
|
4089 |
Notice, that errors may occur during the read, |
|
4090 |
so you better setup some exception handler when using this method." |
|
4091 |
||
21030 | 4092 |
^ Timestamp readFrom:self |
17021 | 4093 |
|
4094 |
" |
|
4095 |
'2014-11-10 21:30:22.444' asTimestamp |
|
4096 |
'2014-11-10 21:30:22.444Z' asTimestamp |
|
4097 |
'2014-11-10 21:30:22.444+0200' asTimestamp |
|
4098 |
" |
|
4099 |
! |
|
4100 |
||
9192 | 4101 |
asTitlecase |
8027 | 4102 |
"return a version of the receiver, where the first character is converted to titlecase, |
4103 |
and everything else to lowercase. |
|
4104 |
See the comment in Character on what titlecase is." |
|
4105 |
||
4106 |
|newStr c bitsPerCharacter |
|
4107 |
mySize "{ Class: SmallInteger }" | |
|
4108 |
||
4109 |
mySize := self size. |
|
4110 |
newStr := self species new:mySize. |
|
4111 |
bitsPerCharacter := newStr bitsPerCharacter. |
|
4112 |
||
4113 |
1 to:mySize do:[:i | |
|
20874 | 4114 |
i == 1 ifTrue:[ |
4115 |
c := (self at:i) asTitlecase. |
|
4116 |
] ifFalse:[ |
|
4117 |
c := (self at:i) asLowercase. |
|
4118 |
]. |
|
4119 |
c bitsPerCharacter > bitsPerCharacter ifTrue:[ |
|
4120 |
newStr := c stringSpecies fromString:newStr. |
|
4121 |
bitsPerCharacter := newStr bitsPerCharacter. |
|
4122 |
]. |
|
4123 |
newStr at:i put:c |
|
8027 | 4124 |
]. |
4125 |
^ newStr |
|
4126 |
||
4127 |
" |
|
8874 | 4128 |
'helloWorld' asTitlecase |
4129 |
'HelloWorld' asTitlecase |
|
4130 |
'HELLOWORLD' asTitlecase |
|
4131 |
'helloworld' asTitlecase |
|
8027 | 4132 |
" |
9192 | 4133 |
! |
4134 |
||
4135 |
asTitlecaseFirst |
|
8023 | 4136 |
"return a version of the receiver, where the first character is converted to titlecase. |
8024 | 4137 |
Titlecase is much like uppercase for most characters, with the exception of some combined |
4138 |
(2-character glyphs), which consist of an upper- and lower-case characters. |
|
4139 |
If the first character is already titlecase, or there is no titlecasepercase for it, return the |
|
8023 | 4140 |
receiver." |
4141 |
||
8026 | 4142 |
" |
8874 | 4143 |
For example, in Unicode, character U+01F3 is LATIN SMALL LETTER DZ. |
4144 |
(Let us write this compound character using ASCII as 'dz'.) |
|
4145 |
This character uppercases to character U+01F1, LATIN CAPITAL LETTER DZ. |
|
4146 |
(Which is basically 'DZ'.) |
|
4147 |
But it titlecases to to character U+01F2, LATIN CAPITAL LETTER D WITH SMALL LETTER Z. |
|
8026 | 4148 |
(Which we can write 'Dz'.) |
4149 |
||
4150 |
character uppercase titlecase |
|
4151 |
--------- --------- --------- |
|
4152 |
dz DZ Dz |
|
4153 |
" |
|
4154 |
||
8023 | 4155 |
|newString firstChar firstCharAsTitlecase| |
4156 |
||
4157 |
firstChar := (self at:1). |
|
8025 | 4158 |
firstCharAsTitlecase := firstChar asTitlecase. |
8023 | 4159 |
firstChar == firstCharAsTitlecase ifTrue:[ ^ self]. |
8037
8cce25b1059c
care for wide chars in asXXXFirst
Claus Gittinger <cg@exept.de>
parents:
8032
diff
changeset
|
4160 |
|
8cce25b1059c
care for wide chars in asXXXFirst
Claus Gittinger <cg@exept.de>
parents:
8032
diff
changeset
|
4161 |
firstCharAsTitlecase bitsPerCharacter > self bitsPerCharacter ifTrue:[ |
20874 | 4162 |
newString := firstCharAsTitlecase stringSpecies fromString:self. |
8037
8cce25b1059c
care for wide chars in asXXXFirst
Claus Gittinger <cg@exept.de>
parents:
8032
diff
changeset
|
4163 |
] ifFalse:[ |
20874 | 4164 |
newString := self stringSpecies fromString:self. |
8037
8cce25b1059c
care for wide chars in asXXXFirst
Claus Gittinger <cg@exept.de>
parents:
8032
diff
changeset
|
4165 |
]. |
8023 | 4166 |
newString at:1 put:firstCharAsTitlecase. |
4167 |
^ newString |
|
4168 |
||
4169 |
" |
|
8874 | 4170 |
'helloWorld' asTitlecaseFirst |
4171 |
'HelloWorld' asTitlecaseFirst |
|
8023 | 4172 |
" |
9192 | 4173 |
! |
4174 |
||
4175 |
asTwoByteString |
|
4176 |
"return the receiver converted to a two-byte string. |
|
16051 | 4177 |
Will be obsolete soon - use asUnicode16String." |
9192 | 4178 |
|
4179 |
^ TwoByteString fromString:self |
|
4180 |
! |
|
4181 |
||
4182 |
asURI |
|
4183 |
"return an URI with string taken from the receiver" |
|
4184 |
||
4185 |
^ URI fromString:self |
|
4186 |
! |
|
8710 | 4187 |
|
5776 | 4188 |
asURL |
4189 |
"return an URL-object from myself." |
|
4190 |
||
4191 |
^ URL fromString:self |
|
4192 |
||
4193 |
" |
|
8874 | 4194 |
'http://www.exept.de:80/index.html' asURL host |
4195 |
'http://www.exept.de:80/index.html' asURL port |
|
9192 | 4196 |
'http://www.exept.de:80/index.html' asURL method |
4197 |
'http://www.exept.de:80/index.html' asURL path |
|
24147 | 4198 |
'file:///tmp/index.html' asURL path |
4199 |
'file:///tmp/index.html' asURL method |
|
4200 |
'file:///tmp/index.html' asURL asString |
|
4201 |
" |
|
4202 |
||
4203 |
"Modified (format): / 25-05-2019 / 09:33:18 / Claus Gittinger" |
|
9192 | 4204 |
! |
4205 |
||
18901 | 4206 |
asUUID |
4207 |
"return self as a UUID" |
|
4208 |
||
4209 |
^ UUID fromString:self |
|
4210 |
||
4211 |
" |
|
4212 |
UUID genUUID asString asUUID |
|
4213 |
'{EAB22AC0-30C1-11CF-A7EB-0000C05BAE0B}' asUUID |
|
4214 |
'{EAB22AC0-30C1-11CF-A7EB-0000C05BAE0B}' asUnicodeString asUUID |
|
4215 |
'EAB22AC0-30C1-11CF-A7EB-0000C05BAE0B' asUUID |
|
4216 |
'EAB22AC0-30C1-11CF-A7EB-0000C05BAE0B quatsch' asUUID |
|
4217 |
'quark EAB22AC0-30C1-11CF-A7EB-0000C05BAE0B' asUUID |
|
4218 |
" |
|
4219 |
||
4220 |
"Modified: / 02-08-2007 / 16:43:29 / cg" |
|
4221 |
! |
|
4222 |
||
9192 | 4223 |
asUnicode16String |
15035 | 4224 |
"return the receiver in a two-byte per character representation. |
4225 |
Normally, the internal ST/X representation should be transparent and not |
|
4226 |
of the programmer's concern; except when strings are passed to the outside world, |
|
4227 |
such as wide-string ffi calls or file contents." |
|
4228 |
||
17600 | 4229 |
|sz| |
4230 |
||
4231 |
sz := self size. |
|
4232 |
||
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
4233 |
^ (Unicode16String new:sz) |
20874 | 4234 |
replaceFrom:1 to:sz with:self startingAt:1; |
4235 |
yourself. |
|
4236 |
||
4237 |
" |
|
4238 |
'abc' asUnicode16String |
|
17600 | 4239 |
" |
24212 | 4240 |
|
4241 |
"Modified (comment): / 28-05-2019 / 14:41:04 / Stefan Vogel" |
|
4242 |
! |
|
4243 |
||
4244 |
asUnicode16StringReplaceInvalidWith:replacementCharacter |
|
4245 |
^ self asUnicode16String |
|
4246 |
||
4247 |
"Created: / 28-05-2019 / 12:15:44 / Stefan Vogel" |
|
17600 | 4248 |
! |
4249 |
||
4250 |
asUnicode16StringZ |
|
4251 |
"return the receiver in a two-byte per character representation. |
|
17625 | 4252 |
Make sure that is zero terminated (last char is 16r0000). |
17600 | 4253 |
Normally, the internal ST/X representation should be transparent and not |
4254 |
of the programmer's concern; except when strings are passed to the outside world, |
|
4255 |
such as wide-string ffi calls or file contents." |
|
4256 |
||
4257 |
|sz| |
|
4258 |
||
4259 |
sz := self size. |
|
22249 | 4260 |
sz > 0 ifTrue:[ |
4261 |
(self at:sz) == (Character codePoint:0) ifTrue:[ |
|
4262 |
^ self asUnicode16String. |
|
4263 |
]. |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
4264 |
]. |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
4265 |
|
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
4266 |
^ (Unicode16String new:sz+1) |
20874 | 4267 |
replaceFrom:1 to:sz with:self startingAt:1; |
4268 |
at:sz+1 put:(Character codePoint:0); |
|
4269 |
yourself. |
|
17600 | 4270 |
|
4271 |
" |
|
22249 | 4272 |
'' asUnicode16StringZ |
20874 | 4273 |
'abc' asUnicode16StringZ |
4274 |
'abc' asUnicode16String asUnicode16StringZ |
|
17600 | 4275 |
" |
9192 | 4276 |
! |
8295
8433d819226b
Define #asUnicode16String and #asUnicode32String in CharacterArray
Stefan Vogel <sv@exept.de>
parents:
8222
diff
changeset
|
4277 |
|
8433d819226b
Define #asUnicode16String and #asUnicode32String in CharacterArray
Stefan Vogel <sv@exept.de>
parents:
8222
diff
changeset
|
4278 |
asUnicode32String |
16051 | 4279 |
"return the receiver in a four-byte per character representation. |
4280 |
Normally, the internal ST/X representation should be transparent and not |
|
4281 |
of the programmer's concern; except when strings are passed to the outside world, |
|
4282 |
such as wide-string ffi calls or file contents." |
|
4283 |
||
4284 |
^ self as:Unicode32String |
|
8295
8433d819226b
Define #asUnicode16String and #asUnicode32String in CharacterArray
Stefan Vogel <sv@exept.de>
parents:
8222
diff
changeset
|
4285 |
! |
8433d819226b
Define #asUnicode16String and #asUnicode32String in CharacterArray
Stefan Vogel <sv@exept.de>
parents:
8222
diff
changeset
|
4286 |
|
8433d819226b
Define #asUnicode16String and #asUnicode32String in CharacterArray
Stefan Vogel <sv@exept.de>
parents:
8222
diff
changeset
|
4287 |
asUnicodeString |
16051 | 4288 |
"return the receiver in a two-byte per character representation. |
4289 |
Normally, the internal ST/X representation should be transparent and not |
|
4290 |
of the programmer's concern; except when strings are passed to the outside world, |
|
4291 |
such as wide-string ffi calls or file contents." |
|
4292 |
||
4293 |
^ self asUnicode16String |
|
4294 |
" |
|
4295 |
'Hello World' asUnicodeString |
|
14141
e86d245213b0
comment/format in: #asUnicodeString
Stefan Vogel <sv@exept.de>
parents:
14137
diff
changeset
|
4296 |
" |
8295
8433d819226b
Define #asUnicode16String and #asUnicode32String in CharacterArray
Stefan Vogel <sv@exept.de>
parents:
8222
diff
changeset
|
4297 |
! |
8433d819226b
Define #asUnicode16String and #asUnicode32String in CharacterArray
Stefan Vogel <sv@exept.de>
parents:
8222
diff
changeset
|
4298 |
|
13073 | 4299 |
asUnixFilename |
4300 |
"return a UnixFilename with pathname taken from the receiver" |
|
4301 |
||
4302 |
^ UnixFilename named:self |
|
4303 |
||
4304 |
"Created: / 20-09-2010 / 21:42:55 / cg" |
|
4305 |
! |
|
4306 |
||
608 | 4307 |
asUppercase |
4308 |
"return a copy of myself in uppercase letters" |
|
4309 |
||
8003
6e690f4b79f1
asLowercase/asUppercase and friends care for 16-bit chars
Claus Gittinger <cg@exept.de>
parents:
7995
diff
changeset
|
4310 |
|newStr c bitsPerCharacter |
608 | 4311 |
mySize "{ Class: SmallInteger }" | |
4312 |
||
4313 |
mySize := self size. |
|
8954
12e701d7b5ac
allow for receiver to be empty in asLowercase and asUppercase.
Claus Gittinger <cg@exept.de>
parents:
8938
diff
changeset
|
4314 |
mySize == 0 ifTrue:[^ self]. |
608 | 4315 |
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
|
4316 |
bitsPerCharacter := newStr bitsPerCharacter. |
6e690f4b79f1
asLowercase/asUppercase and friends care for 16-bit chars
Claus Gittinger <cg@exept.de>
parents:
7995
diff
changeset
|
4317 |
|
18657 | 4318 |
"/ handle the very seldom case of a lowercase char which needs |
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
4319 |
"/ more bits in its uppercase variant |
18657 | 4320 |
"/ (there are only a few of them) |
4321 |
||
608 | 4322 |
1 to:mySize do:[:i | |
20874 | 4323 |
c := (self at:i) asUppercase. |
4324 |
c bitsPerCharacter > bitsPerCharacter ifTrue:[ |
|
4325 |
newStr := c stringSpecies fromString:newStr. |
|
4326 |
bitsPerCharacter := newStr bitsPerCharacter. |
|
4327 |
]. |
|
4328 |
newStr at:i put:c |
|
608 | 4329 |
]. |
4330 |
^ newStr |
|
4331 |
||
4332 |
" |
|
8874 | 4333 |
'helloWorld' asUppercase |
4334 |
'helloWorld' asUppercaseFirst |
|
9192 | 4335 |
(Character value:16rB5) asString asUppercase -- needs 16 bits !! |
18657 | 4336 |
(Character value:16rFF) asString asUppercase -- needs 16 bits !! |
9192 | 4337 |
" |
4338 |
! |
|
4339 |
||
4340 |
asUppercaseFirst |
|
8003
6e690f4b79f1
asLowercase/asUppercase and friends care for 16-bit chars
Claus Gittinger <cg@exept.de>
parents:
7995
diff
changeset
|
4341 |
"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
|
4342 |
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
|
4343 |
receiver." |
6e690f4b79f1
asLowercase/asUppercase and friends care for 16-bit chars
Claus Gittinger <cg@exept.de>
parents:
7995
diff
changeset
|
4344 |
|
6e690f4b79f1
asLowercase/asUppercase and friends care for 16-bit chars
Claus Gittinger <cg@exept.de>
parents:
7995
diff
changeset
|
4345 |
|newString firstChar firstCharAsUppercase| |
6e690f4b79f1
asLowercase/asUppercase and friends care for 16-bit chars
Claus Gittinger <cg@exept.de>
parents:
7995
diff
changeset
|
4346 |
|
12679 | 4347 |
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
|
4348 |
firstChar := self at:1. |
8003
6e690f4b79f1
asLowercase/asUppercase and friends care for 16-bit chars
Claus Gittinger <cg@exept.de>
parents:
7995
diff
changeset
|
4349 |
firstCharAsUppercase := firstChar asUppercase. |
6e690f4b79f1
asLowercase/asUppercase and friends care for 16-bit chars
Claus Gittinger <cg@exept.de>
parents:
7995
diff
changeset
|
4350 |
firstChar == firstCharAsUppercase ifTrue:[ ^ self]. |
6e690f4b79f1
asLowercase/asUppercase and friends care for 16-bit chars
Claus Gittinger <cg@exept.de>
parents:
7995
diff
changeset
|
4351 |
|
12679 | 4352 |
"/ 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
|
4353 |
firstCharAsUppercase bitsPerCharacter > self bitsPerCharacter ifTrue:[ |
20874 | 4354 |
newString := firstCharAsUppercase stringSpecies fromString:self. |
8037
8cce25b1059c
care for wide chars in asXXXFirst
Claus Gittinger <cg@exept.de>
parents:
8032
diff
changeset
|
4355 |
] ifFalse:[ |
20874 | 4356 |
newString := self stringSpecies fromString:self. |
8037
8cce25b1059c
care for wide chars in asXXXFirst
Claus Gittinger <cg@exept.de>
parents:
8032
diff
changeset
|
4357 |
]. |
8003
6e690f4b79f1
asLowercase/asUppercase and friends care for 16-bit chars
Claus Gittinger <cg@exept.de>
parents:
7995
diff
changeset
|
4358 |
newString at:1 put:firstCharAsUppercase. |
608 | 4359 |
^ newString |
4360 |
||
4361 |
" |
|
8874 | 4362 |
'helloWorld' asUppercase |
4363 |
'helloWorld' asUppercaseFirst |
|
4364 |
'HelloWorld' asUppercaseFirst |
|
608 | 4365 |
" |
9192 | 4366 |
! |
4367 |
||
11828 | 4368 |
asUppercaseLast |
4369 |
"return a copy of myself where the last character is |
|
4370 |
converted to uppercase." |
|
4371 |
||
4372 |
|newString sz| |
|
4373 |
||
4374 |
sz := self size. |
|
4375 |
newString := self copyFrom:1 to:sz. |
|
4376 |
sz > 0 ifTrue:[ |
|
20874 | 4377 |
newString at:sz put:(newString at:sz) asUppercase |
11828 | 4378 |
]. |
4379 |
^ newString |
|
4380 |
||
4381 |
" |
|
12177 | 4382 |
'HelloWorld' asUppercase |
4383 |
'HelloWorld' asUppercaseLast |
|
11828 | 4384 |
" |
4385 |
! |
|
4386 |
||
17021 | 4387 |
asUtcTimestamp |
4388 |
"convert the receiver into an UtcTimestamp (Time is interpreted in the UTC timezone). |
|
4389 |
Notice, that errors may occur during the read, |
|
4390 |
so you better setup some exception handler when using this method." |
|
4391 |
||
21030 | 4392 |
^ UtcTimestamp readFrom:self |
17021 | 4393 |
|
4394 |
" |
|
4395 |
'2014-11-10 21:30:22.444' asUtcTimestamp |
|
4396 |
" |
|
4397 |
! |
|
4398 |
||
23121 | 4399 |
asVersionNumberCollection |
4400 |
"Convert a string like 1.2.3a to an Array of numbers (or string, if not a number). |
|
4401 |
Remove zeroes from the end." |
|
4402 |
||
4403 |
^ (self asCollectionOfSubstringsSeparatedByAny:'.-_') asVersionNumberCollection. |
|
4404 |
||
4405 |
" |
|
4406 |
'1' asVersionNumberCollection. |
|
4407 |
'1.1' asVersionNumberCollection. |
|
4408 |
'1.1a' asVersionNumberCollection. |
|
4409 |
'1.1.0' asVersionNumberCollection. |
|
4410 |
'01.01.0' asVersionNumberCollection. |
|
4411 |
'expecco_18_1_0' asVersionNumberCollection. |
|
4412 |
" |
|
4413 |
||
4414 |
"Created: / 20-06-2018 / 11:23:53 / Stefan Vogel" |
|
4415 |
"Modified: / 20-06-2018 / 19:24:36 / Stefan Vogel" |
|
4416 |
! |
|
4417 |
||
11062 | 4418 |
literalArrayEncoding |
4419 |
"encode myself as an array literal, from which a copy of the receiver |
|
4420 |
can be reconstructed with #decodeAsLiteralArray." |
|
4421 |
||
4422 |
^ self |
|
4423 |
||
4424 |
"Modified: 1.9.1995 / 02:25:45 / claus" |
|
4425 |
"Modified: 22.4.1996 / 13:00:50 / cg" |
|
4426 |
! |
|
4427 |
||
9192 | 4428 |
string |
4429 |
"return the receiver - for ST-80 compatibility" |
|
4430 |
||
4431 |
^ self |
|
4432 |
||
4433 |
"Modified: 27.4.1996 / 13:29:30 / cg" |
|
4434 |
! ! |
|
4435 |
||
4436 |
!CharacterArray methodsFor:'copying'! |
|
4437 |
||
22528 | 4438 |
, aStringOrCharacterOrAnyOther |
2720
98c21d9a07c0
allow mixed strings to be concatenated using comma
Claus Gittinger <cg@exept.de>
parents:
2692
diff
changeset
|
4439 |
"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
|
4440 |
This is nonStandard, but convenient" |
2f14bbd792a9
, now accepts a character-argument
Claus Gittinger <cg@exept.de>
parents:
1028
diff
changeset
|
4441 |
|
23998 | 4442 |
|stringifiedArg |
4443 |
newSpecies newString l1 l2 combinedLen| |
|
22528 | 4444 |
|
4445 |
aStringOrCharacterOrAnyOther isCharacter ifTrue:[ |
|
4446 |
^ self copyWith:aStringOrCharacterOrAnyOther |
|
4447 |
]. |
|
4448 |
aStringOrCharacterOrAnyOther isText ifTrue:[ |
|
4449 |
^ aStringOrCharacterOrAnyOther concatenateFromString:self |
|
4450 |
]. |
|
4451 |
stringifiedArg := aStringOrCharacterOrAnyOther asString. |
|
4452 |
stringifiedArg isString ifTrue:[ |
|
24037 | 4453 |
(stringifiedArg bitsPerCharacter) > (self bitsPerCharacter) ifTrue:[ |
4454 |
newSpecies := stringifiedArg species. |
|
4455 |
] ifFalse:[ |
|
4456 |
newSpecies := self species. |
|
4457 |
]. |
|
23998 | 4458 |
l1 := self size. |
4459 |
l2 := stringifiedArg size. |
|
4460 |
combinedLen := l1 + l2. |
|
4461 |
newString := newSpecies uninitializedNew:combinedLen. |
|
24037 | 4462 |
newString |
4463 |
replaceFrom:1 to:l1 with:self startingAt:1; |
|
4464 |
replaceFrom:l1+1 to:combinedLen with:stringifiedArg startingAt:1. |
|
23998 | 4465 |
^ newString |
2720
98c21d9a07c0
allow mixed strings to be concatenated using comma
Claus Gittinger <cg@exept.de>
parents:
2692
diff
changeset
|
4466 |
]. |
22528 | 4467 |
^ super , stringifiedArg |
1050
2f14bbd792a9
, now accepts a character-argument
Claus Gittinger <cg@exept.de>
parents:
1028
diff
changeset
|
4468 |
|
2f14bbd792a9
, now accepts a character-argument
Claus Gittinger <cg@exept.de>
parents:
1028
diff
changeset
|
4469 |
" |
8874 | 4470 |
'hello' , $1 |
24037 | 4471 |
'hello' , (Character codePoint:1046) |
8874 | 4472 |
'hello' , '1' |
4473 |
'hello' , (' world' asText allBold) |
|
4474 |
'hello' , (JISEncodedString fromString:' world') |
|
2720
98c21d9a07c0
allow mixed strings to be concatenated using comma
Claus Gittinger <cg@exept.de>
parents:
2692
diff
changeset
|
4475 |
(JISEncodedString fromString:'hello') , ' world' |
1411 | 4476 |
|
1422 | 4477 |
Transcript showCR: |
20874 | 4478 |
(Text string:'hello' emphasis:#italic) , (Text string:' world' emphasis:#bold) |
22528 | 4479 |
|
4480 |
'hello',123,'world' |
|
4481 |
'hello' allBold,123,'world' |
|
4482 |
'hello',123,'world' allBold |
|
1050
2f14bbd792a9
, now accepts a character-argument
Claus Gittinger <cg@exept.de>
parents:
1028
diff
changeset
|
4483 |
" |
2f14bbd792a9
, now accepts a character-argument
Claus Gittinger <cg@exept.de>
parents:
1028
diff
changeset
|
4484 |
|
23997 | 4485 |
"Modified: / 28-06-1997 / 00:13:17 / cg" |
23998 | 4486 |
"Modified: / 27-03-2019 / 19:37:34 / Claus Gittinger" |
24033 | 4487 |
"Modified: / 02-04-2019 / 10:39:28 / Maren" |
24037 | 4488 |
"Modified (comment): / 02-04-2019 / 11:36:00 / Stefan Vogel" |
9192 | 4489 |
! |
4490 |
||
4491 |
,, aString |
|
7020 | 4492 |
"concatenate with a newLine in between" |
6965 | 4493 |
|
4494 |
^ (self copyWith:Character cr) , aString |
|
4495 |
||
4496 |
" |
|
7020 | 4497 |
'hello' ,, 'world' |
6965 | 4498 |
" |
23388 | 4499 |
|
4500 |
"Modified (comment): / 25-09-2018 / 10:21:45 / Claus Gittinger" |
|
6965 | 4501 |
! |
4502 |
||
11329 | 4503 |
chopTo:maxLen |
19464 | 4504 |
"if the receiver's size is less or equal to maxLen, return it. |
11329 | 4505 |
Otherwise, return a copy of the receiver, where some characters |
4506 |
in the middle have been removed for a total string length |
|
4507 |
of maxLen." |
|
4508 |
||
4509 |
|sz n1 n2| |
|
4510 |
||
4511 |
(sz := self size) <= maxLen ifTrue:[ ^ self ]. |
|
4512 |
||
4513 |
n1 := n2 := maxLen // 2. |
|
4514 |
maxLen odd ifTrue:[ |
|
20874 | 4515 |
n2 := n1 + 1 |
11329 | 4516 |
]. |
4517 |
^ (self copyFrom:1 to:n1) , (self copyFrom:sz - n2 + 1) |
|
4518 |
||
4519 |
" |
|
4520 |
'12345678901234' chopTo:15 |
|
4521 |
'123456789012345' chopTo:15 |
|
4522 |
'1234567890123456' chopTo:15 |
|
4523 |
'aShortString' chopTo:15 |
|
4524 |
'aVeryLongNameForAStringThatShouldBeShortened' chopTo:15 |
|
4525 |
" |
|
4526 |
||
4527 |
"Modified: / 24-10-2006 / 12:32:01 / cg" |
|
4528 |
! |
|
4529 |
||
4530 |
contractAtBeginningTo:maxLen |
|
19464 | 4531 |
"if the receiver's size is less or equal to maxLen, return it. |
11329 | 4532 |
Otherwise, return a copy of the receiver, where some characters |
4533 |
at the beginning have been replaced by '...' for a total string length |
|
4534 |
of maxLen. Can be used to abbreviate long entries in tables." |
|
4535 |
||
4536 |
|sz| |
|
4537 |
||
4538 |
(sz := self size) <= maxLen ifTrue:[ ^ self ]. |
|
4539 |
||
4540 |
^ '...' , (self copyFrom:(sz - (maxLen - 4))) |
|
4541 |
||
4542 |
" |
|
4543 |
'12345678901234' contractAtBeginningTo:15 |
|
4544 |
'123456789012345' contractAtBeginningTo:15 |
|
4545 |
'1234567890123456' contractAtBeginningTo:15 |
|
4546 |
'aShortString' contractAtBeginningTo:15 |
|
4547 |
'aVeryLongNameForAStringThatShouldBeShortened' contractAtBeginningTo:15 |
|
4548 |
" |
|
4549 |
||
4550 |
"Modified: / 24-10-2006 / 12:32:13 / cg" |
|
4551 |
! |
|
4552 |
||
4553 |
contractAtEndTo:maxLen |
|
19464 | 4554 |
"if the receiver's size is less or equal to maxLen, return it. |
11329 | 4555 |
Otherwise, return a copy of the receiver, where some characters |
4556 |
at the end have been replaced by '...' for a total string length |
|
4557 |
of maxLen. Can be used to abbreviate long entries in tables." |
|
4558 |
||
21559 | 4559 |
(self size <= maxLen) ifTrue:[ ^ self ]. |
11329 | 4560 |
|
4561 |
^ (self copyTo:maxLen-3),'...' |
|
4562 |
||
4563 |
" |
|
4564 |
'12345678901234' contractAtEndTo:15 |
|
4565 |
'123456789012345' contractAtEndTo:15 |
|
4566 |
'1234567890123456' contractAtEndTo:15 |
|
4567 |
'aShortString' contractAtEndTo:15 |
|
4568 |
'aVeryLongNameForAStringThatShouldBeShortened' contractAtEndTo:15 |
|
4569 |
" |
|
4570 |
||
4571 |
"Modified: / 24-10-2006 / 12:32:26 / cg" |
|
21559 | 4572 |
"Modified: / 23-02-2017 / 21:32:21 / mawalch" |
11329 | 4573 |
! |
4574 |
||
4575 |
contractLeftTo:maxLen |
|
19464 | 4576 |
"if the receiver's size is less or equal to maxLen, return it. |
11329 | 4577 |
Otherwise, return a copy of the receiver, where some characters |
4578 |
near the first quarter have been replaced by '...' for a total string length |
|
12177 | 4579 |
of maxLen. |
4580 |
Very similar to contractTo:, but better to abbreviate long filename entries, |
|
11329 | 4581 |
where the right part is of more use than the left." |
4582 |
||
4583 |
|sz "{ SmallInteger }" |
|
4584 |
halfSize quarterSize "{ SmallInteger }" |
|
4585 |
leftEnd rightEnd rightStart| |
|
4586 |
||
4587 |
(sz := self size) <= maxLen ifTrue:[ ^ self ]. |
|
4588 |
||
4589 |
halfSize := maxLen // 2. |
|
4590 |
quarterSize := maxLen // 4. |
|
4591 |
leftEnd := quarterSize-1. |
|
4592 |
rightEnd := maxLen - leftEnd - 3. |
|
4593 |
rightStart := sz - rightEnd + 1. |
|
4594 |
^ (self copyTo:leftEnd),'...',(self copyFrom:rightStart) |
|
4595 |
||
4596 |
" |
|
12177 | 4597 |
'12345678901234' contractLeftTo:15 |
4598 |
'123456789012345' contractLeftTo:15 |
|
4599 |
'1234567890123456' contractLeftTo:15 |
|
4600 |
'aShortString' contractLeftTo:15 |
|
4601 |
'aVeryLongNameForAStringThatShouldBeShortened' contractLeftTo:15 |
|
11329 | 4602 |
'C:\Dokumente und Einstellungen\cg\work\bosch\dapas\hw_schnittstellen\DAPAS__HpibDLL.st' contractLeftTo:40 |
4603 |
" |
|
4604 |
! |
|
4605 |
||
4606 |
contractTo:maxLen |
|
19464 | 4607 |
"if the receiver's size is less or equal to maxLen, return it. |
11329 | 4608 |
Otherwise, return a copy of the receiver, where some characters |
4609 |
in the middle have been replaced by '...' for a total string length |
|
4610 |
of maxLen. Can be used to abbreviate long entries in tables." |
|
4611 |
||
13362 | 4612 |
|sz "{ SmallInteger }" leftSize rightSize| |
11329 | 4613 |
|
4614 |
(sz := self size) <= maxLen ifTrue:[ ^ self ]. |
|
4615 |
||
13362 | 4616 |
rightSize := maxLen // 2. |
4617 |
leftSize := maxLen - rightSize. |
|
13822 | 4618 |
leftSize := leftSize - 2. |
4619 |
rightSize := rightSize - 1. |
|
13362 | 4620 |
^ (self copyTo:leftSize),'...',(self copyFrom:(sz+1-rightSize)) |
4621 |
||
4622 |
" |
|
13882
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
4623 |
'12345678901234' contractTo:15 |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
4624 |
'123456789012345' contractTo:15 |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
4625 |
'1234567890123456' contractTo:15 |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
4626 |
'12345678901234567' contractTo:15 |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
4627 |
'123456789012345678' contractTo:15 |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
4628 |
'aShortString' contractTo:15 |
11329 | 4629 |
'aVeryLongNameForAStringThatShouldBeShortened' contractTo:15 |
4630 |
'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
|
4631 |
('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
|
4632 |
('1234567890123456789012345678901234567' contractTo:29) size |
13362 | 4633 |
" |
4634 |
||
13822 | 4635 |
"Modified (comment): / 24-11-2011 / 19:17:46 / cg" |
11329 | 4636 |
! |
4637 |
||
23061 | 4638 |
copyBetween:string1 and:string2 caseSensitive:caseSensitive |
4639 |
"return the substring between two matching sub-strings. |
|
4640 |
Returns nil, if not both subStrings are present in the receiver." |
|
4641 |
||
4642 |
|idx1 leftIndex rightIndex| |
|
4643 |
||
4644 |
idx1 := self indexOfString:string1 caseSensitive:caseSensitive. |
|
4645 |
idx1 ~~ 0 ifTrue:[ |
|
4646 |
leftIndex := (idx1 + string1 size). |
|
4647 |
rightIndex := self indexOfString:string2 caseSensitive:caseSensitive startingAt:leftIndex. |
|
4648 |
rightIndex ~~ 0 ifTrue:[ |
|
4649 |
^ self copyFrom:leftIndex to:rightIndex-1 |
|
4650 |
] |
|
4651 |
]. |
|
4652 |
^ nil |
|
4653 |
||
4654 |
" |
|
4655 |
'hello funny world' copyBetween:'hello' and:'world' caseSensitive:true -> ' funny ' |
|
4656 |
'helloworld' copyBetween:'hello' and:'world' caseSensitive:true -> '' |
|
4657 |
'helloorld' copyBetween:'hello' and:'world' caseSensitive:true -> nil |
|
4658 |
||
4659 |
'hello funny World' copyBetween:'hello' and:'world' caseSensitive:true -> nil |
|
4660 |
'helloWorld' copyBetween:'hello' and:'world' caseSensitive:true -> nil |
|
4661 |
||
4662 |
'hello funny World' copyBetween:'hello' and:'world' caseSensitive:false -> ' funny ' |
|
4663 |
'helloWorld' copyBetween:'hello' and:'world' caseSensitive:false -> '' |
|
4664 |
" |
|
4665 |
||
4666 |
"Created: / 08-06-2018 / 14:50:17 / Claus Gittinger" |
|
4667 |
! |
|
4668 |
||
16202 | 4669 |
copyReplaceAll:oldElement with:newElement |
4670 |
"return a copy of the receiver as a string, where all elements equal to oldElement |
|
4671 |
have been replaced by newElement." |
|
4672 |
||
4673 |
"/ ANSI seems to allow a sequence to be replaced by another sequence, |
|
4674 |
"/ whereas the old ST80 meant replace all occurrences... - sigh. |
|
22231 | 4675 |
oldElement isString ifTrue:[ |
4676 |
newElement isString ifTrue:[ |
|
20874 | 4677 |
^ self copyReplaceString:oldElement withString:newElement. |
4678 |
]. |
|
23552 | 4679 |
self halt:'check if this is legal (trying to replace string by non-string)'. |
16202 | 4680 |
]. |
22231 | 4681 |
newElement isString ifTrue:[ |
23552 | 4682 |
self halt:'check if this is legal (trying to replace non-string by string)'. |
16202 | 4683 |
]. |
16212 | 4684 |
^ super copyReplaceAll:oldElement with:newElement |
23552 | 4685 |
|
4686 |
"Modified: / 03-12-2018 / 16:03:32 / Stefan Vogel" |
|
16202 | 4687 |
! |
4688 |
||
8347 | 4689 |
copyReplaceString:subString withString:newString |
4690 |
"return a copy of the receiver, with all sequences of subString replaced |
|
4691 |
by newString (i.e. slice in the newString in place of the oldString)." |
|
4692 |
||
4693 |
|tmpStream idx idx1| |
|
4694 |
||
15523 | 4695 |
tmpStream := self species writeStream. |
8347 | 4696 |
idx := 1. |
4697 |
[idx ~~ 0] whileTrue:[ |
|
20874 | 4698 |
idx1 := idx. |
4699 |
idx := self indexOfSubCollection:subString startingAt:idx. |
|
4700 |
idx ~~ 0 ifTrue:[ |
|
4701 |
tmpStream nextPutAll:(self copyFrom:idx1 to:idx-1). |
|
4702 |
tmpStream nextPutAll:newString. |
|
4703 |
idx := idx + subString size |
|
4704 |
] |
|
8347 | 4705 |
]. |
4706 |
tmpStream nextPutAll:(self copyFrom:idx1). |
|
4707 |
^ tmpStream contents |
|
4708 |
||
4709 |
" |
|
9306 | 4710 |
'12345678901234567890' copyReplaceString:'123' withString:'OneTwoThree' |
4711 |
'12345678901234567890' copyReplaceString:'123' withString:'*' |
|
4712 |
'12345678901234567890' copyReplaceString:'234' withString:'foo' |
|
22231 | 4713 |
'12345678901234567890' asUnicode16String copyReplaceString:'234' asUnicode16String withString:'foo' |
4714 |
'12345678901234567890' asUnicode16String copyReplaceString:'234' withString:'foo' |
|
4715 |
'12345678901234567890' asUnicode16String copyReplaceString:'234' withString:'foo' asUnicode16String |
|
9306 | 4716 |
|
4717 |
('a string with spaces' copyReplaceAll:$ withAll:' foo ') |
|
20874 | 4718 |
copyReplaceString:'foo' withString:'bar' |
8347 | 4719 |
" |
4720 |
||
4721 |
"Modified: / 31-05-1999 / 12:33:59 / cg" |
|
4722 |
"Created: / 12-05-2004 / 12:00:00 / cg" |
|
4723 |
! |
|
4724 |
||
8040
f10cca439066
copyWith for mixed-width string/character
Claus Gittinger <cg@exept.de>
parents:
8037
diff
changeset
|
4725 |
copyWith:aCharacter |
19464 | 4726 |
"return a new string containing the receiver's characters |
8874 | 4727 |
and the single new character, aCharacter. |
8040
f10cca439066
copyWith for mixed-width string/character
Claus Gittinger <cg@exept.de>
parents:
8037
diff
changeset
|
4728 |
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
|
4729 |
as argument, but equivalent to copy-and-addLast. |
8874 | 4730 |
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
|
4731 |
(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
|
4732 |
|
f10cca439066
copyWith for mixed-width string/character
Claus Gittinger <cg@exept.de>
parents:
8037
diff
changeset
|
4733 |
|sz newString| |
f10cca439066
copyWith for mixed-width string/character
Claus Gittinger <cg@exept.de>
parents:
8037
diff
changeset
|
4734 |
|
f10cca439066
copyWith for mixed-width string/character
Claus Gittinger <cg@exept.de>
parents:
8037
diff
changeset
|
4735 |
aCharacter bitsPerCharacter > self bitsPerCharacter ifTrue:[ |
20874 | 4736 |
sz := self size. |
4737 |
newString := aCharacter stringSpecies new:sz + 1. |
|
4738 |
newString replaceFrom:1 to:sz with:self startingAt:1. |
|
4739 |
newString at:sz+1 put:aCharacter. |
|
4740 |
^ newString. |
|
8040
f10cca439066
copyWith for mixed-width string/character
Claus Gittinger <cg@exept.de>
parents:
8037
diff
changeset
|
4741 |
]. |
f10cca439066
copyWith for mixed-width string/character
Claus Gittinger <cg@exept.de>
parents:
8037
diff
changeset
|
4742 |
^ super copyWith:aCharacter |
17241 | 4743 |
! |
4744 |
||
4745 |
restAfter:keyword withoutSeparators:strip |
|
23392 | 4746 |
"compare the left part of the receiver with keyword, |
4747 |
if it matches return the right rest. |
|
17241 | 4748 |
Finally, if strip is true, remove whiteSpace. |
23392 | 4749 |
If the string does not start with keyword, return nil. |
4750 |
||
17241 | 4751 |
This method is used to match and extract lines of the form: |
20874 | 4752 |
something: rest |
17241 | 4753 |
where we are interested in rest, but only if the receiver string |
4754 |
begins with something. |
|
4755 |
||
4756 |
You may wonder why such a specialized method exists here |
|
4757 |
- this is so common when processing mailboxes, |
|
4758 |
rcs files, nntp/pop3 responses, that is was considered worth |
|
4759 |
a special method here to avoid having the code below a hundred |
|
4760 |
times in variuos places." |
|
4761 |
||
4762 |
|rest| |
|
4763 |
||
4764 |
(self startsWith:keyword) ifTrue:[ |
|
20874 | 4765 |
rest := self copyFrom:(keyword size + 1). |
4766 |
strip ifTrue:[ |
|
4767 |
rest := rest withoutSeparators |
|
4768 |
]. |
|
4769 |
^ rest |
|
17241 | 4770 |
]. |
4771 |
^ nil |
|
4772 |
||
4773 |
" |
|
4774 |
'foo: hello world' restAfter:'foo:' withoutSeparators:true |
|
4775 |
'funny: something' restAfter:'foo:' withoutSeparators:true |
|
4776 |
||
4777 |
'foo: hello world ' restAfter:'foo:' withoutSeparators:true |
|
4778 |
'foo: hello world ' restAfter:'foo:' withoutSeparators:false |
|
4779 |
" |
|
4780 |
||
23392 | 4781 |
"Created: / 25-11-1995 / 11:04:18 / cg" |
4782 |
"Modified (comment): / 27-09-2018 / 10:01:55 / Claus Gittinger" |
|
17241 | 4783 |
! |
4784 |
||
4785 |
splitAtString:subString withoutSeparators:strip |
|
4786 |
"If the receiver is of the form: |
|
20874 | 4787 |
<left><subString><right> |
17241 | 4788 |
return a collection containing left and right only. |
4789 |
If strip is true, remove whiteSpace in the returned substrings." |
|
4790 |
||
4791 |
|idx left right| |
|
4792 |
||
4793 |
(idx := self indexOfSubCollection:subString) ~~ 0 ifTrue:[ |
|
20874 | 4794 |
left := self copyTo:(idx - 1). |
4795 |
right := self copyFrom:(idx + subString size). |
|
4796 |
strip ifTrue:[ |
|
4797 |
left := left withoutSeparators. |
|
4798 |
right := right withoutSeparators. |
|
4799 |
]. |
|
4800 |
^ StringCollection with:left with:right |
|
17241 | 4801 |
]. |
23001 | 4802 |
self proceedableError:'substring not present in receiver'. |
17241 | 4803 |
^ self |
4804 |
||
4805 |
" |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
4806 |
'hello -> world' splitAtString:'->' withoutSeparators:false |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
4807 |
'hello -> world' splitAtString:'->' withoutSeparators:true |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
4808 |
'hello -> ' splitAtString:'->' withoutSeparators:true |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
4809 |
'hello > error' splitAtString:'->' withoutSeparators:true |
17241 | 4810 |
" |
4811 |
||
23001 | 4812 |
"Created: / 25-11-1995 / 11:04:18 / cg" |
4813 |
"Modified: / 24-05-2018 / 14:55:52 / Claus Gittinger" |
|
608 | 4814 |
! ! |
4815 |
||
4816 |
!CharacterArray methodsFor:'displaying'! |
|
4817 |
||
2762 | 4818 |
displayOn:aGC x:x y:y from:start to:stop |
4819 |
"display the receiver on a GC" |
|
4820 |
||
12171 | 4821 |
self displayOn:aGC x:x y:y from:start to:stop opaque:false |
4822 |
||
4823 |
"Modified: 12.5.1996 / 12:49:33 / cg" |
|
4824 |
! |
|
4825 |
||
4826 |
displayOn:aGC x:x y:y from:start to:stop opaque:opaque |
|
4827 |
"display the receiver on a GC" |
|
4828 |
||
2762 | 4829 |
"q&d hack" |
4830 |
||
12177 | 4831 |
(start == 1 and:[stop == self size]) ifTrue:[ |
20874 | 4832 |
self displayOn:aGC x:x y:y opaque:opaque. |
12177 | 4833 |
] ifFalse:[ |
20874 | 4834 |
(self copyFrom:start to:stop) displayOn:aGC x:x y:y opaque:opaque. |
12177 | 4835 |
]. |
2762 | 4836 |
! |
4837 |
||
7082 | 4838 |
displayOn:aGc x:x y:y opaque:opaque |
2762 | 4839 |
"display the receiver in a graphicsContext - this method allows |
4840 |
strings to be used like DisplayObjects." |
|
4841 |
||
4842 |
|s| |
|
4843 |
||
4844 |
s := self string. |
|
7082 | 4845 |
opaque ifTrue:[ |
20874 | 4846 |
aGc displayOpaqueString:s x:x y:y. |
2762 | 4847 |
] ifFalse:[ |
20874 | 4848 |
aGc displayString:s x:x y:y. |
2762 | 4849 |
]. |
1381 | 4850 |
|
4851 |
"Modified: 11.5.1996 / 14:42:48 / cg" |
|
4852 |
! |
|
4853 |
||
2762 | 4854 |
displayOpaqueOn:aGC x:x y:y from:start to:stop |
4855 |
"display the receiver on a GC" |
|
4856 |
||
12171 | 4857 |
self displayOn:aGC x:x y:y from:start to:stop opaque:true |
608 | 4858 |
! ! |
4859 |
||
1375
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
4860 |
!CharacterArray methodsFor:'emphasis'! |
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
4861 |
|
11632
b82e9f3cabd1
new: #actionForAll: #emphasisAtPoint:on:
Stefan Vogel <sv@exept.de>
parents:
11630
diff
changeset
|
4862 |
actionForAll:aBlock |
12326
c67f12213ddb
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
12319
diff
changeset
|
4863 |
"change the action block of all characters. |
c67f12213ddb
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
12319
diff
changeset
|
4864 |
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
|
4865 |
|
b82e9f3cabd1
new: #actionForAll: #emphasisAtPoint:on:
Stefan Vogel <sv@exept.de>
parents:
11630
diff
changeset
|
4866 |
^ self asText actionForAll:aBlock |
b82e9f3cabd1
new: #actionForAll: #emphasisAtPoint:on:
Stefan Vogel <sv@exept.de>
parents:
11630
diff
changeset
|
4867 |
! |
b82e9f3cabd1
new: #actionForAll: #emphasisAtPoint:on:
Stefan Vogel <sv@exept.de>
parents:
11630
diff
changeset
|
4868 |
|
5732 | 4869 |
allBold |
12326
c67f12213ddb
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
12319
diff
changeset
|
4870 |
"return a text object representing the receiver, but all boldified" |
5732 | 4871 |
|
8874 | 4872 |
"this test allows for small non-gui apps to be built without libbasic2 (where Text is)" |
6187 | 4873 |
Text isNil ifTrue:[^ self]. |
5732 | 4874 |
^ self asText allBold |
4875 |
||
4876 |
" |
|
4877 |
Transcript showCR:'hello' asText allBold |
|
4878 |
Transcript showCR:'hello' allBold |
|
4879 |
" |
|
4880 |
! |
|
4881 |
||
5898
7bc4f1593b4e
allItalic now understood by CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5848
diff
changeset
|
4882 |
allItalic |
12326
c67f12213ddb
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
12319
diff
changeset
|
4883 |
"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
|
4884 |
|
8874 | 4885 |
"this test allows for small non-gui apps to be built without libbasic2 (where Text is)" |
6187 | 4886 |
Text isNil ifTrue:[^ self]. |
5898
7bc4f1593b4e
allItalic now understood by CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5848
diff
changeset
|
4887 |
^ self asText allItalic |
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 |
" |
7bc4f1593b4e
allItalic now understood by CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5848
diff
changeset
|
4890 |
Transcript showCR:'hello' asText allItalic |
7bc4f1593b4e
allItalic now understood by CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5848
diff
changeset
|
4891 |
Transcript showCR:'hello' allItalic |
7bc4f1593b4e
allItalic now understood by CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5848
diff
changeset
|
4892 |
" |
7bc4f1593b4e
allItalic now understood by CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5848
diff
changeset
|
4893 |
! |
7bc4f1593b4e
allItalic now understood by CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5848
diff
changeset
|
4894 |
|
23903 | 4895 |
allNonBold |
4896 |
"make all characters non-bold; |
|
4897 |
I already have no emphasis, so the receiver string is returned" |
|
4898 |
||
4899 |
^ self |
|
4900 |
||
4901 |
"Created: / 13-03-2019 / 21:02:45 / Claus Gittinger" |
|
4902 |
! |
|
4903 |
||
11345 | 4904 |
allStrikedOut |
12326
c67f12213ddb
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
12319
diff
changeset
|
4905 |
"return a text object representing the receiver, but all in strikeout" |
11345 | 4906 |
|
4907 |
"this test allows for small non-gui apps to be built without libbasic2 (where Text is)" |
|
4908 |
Text isNil ifTrue:[^ self]. |
|
4909 |
^ self asText allStrikedOut |
|
4910 |
||
4911 |
" |
|
4912 |
Transcript showCR:'hello' asText allStrikedOut |
|
4913 |
Transcript showCR:'hello' allStrikedOut |
|
4914 |
" |
|
4915 |
! |
|
4916 |
||
10862 | 4917 |
allUnderlined |
12326
c67f12213ddb
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
12319
diff
changeset
|
4918 |
"return a text object representing the receiver, but all with underline" |
10862 | 4919 |
|
4920 |
"this test allows for small non-gui apps to be built without libbasic2 (where Text is)" |
|
4921 |
Text isNil ifTrue:[^ self]. |
|
4922 |
^ self asText allUnderlined |
|
4923 |
||
4924 |
" |
|
4925 |
Transcript showCR:'hello' asText allUnderlined |
|
4926 |
Transcript showCR:'hello' allUnderlined |
|
4927 |
" |
|
4928 |
! |
|
4929 |
||
19774 | 4930 |
asActionLinkTo:aBlock |
4931 |
"change the action block of all characters and colorize as a link. |
|
4932 |
Some widgets use this like a href if clicked onto the text |
|
4933 |
(for example, the system-browser's info at the bottom is such a widget)." |
|
4934 |
||
4935 |
"/ the blue may become a user-setting some time later.. |
|
4936 |
^ (self actionForAll:aBlock) withColor:(Color blue) |
|
4937 |
! |
|
4938 |
||
6228 | 4939 |
colorizeAllWith:aColor |
12326
c67f12213ddb
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
12319
diff
changeset
|
4940 |
"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
|
4941 |
|
c67f12213ddb
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
12319
diff
changeset
|
4942 |
"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
|
4943 |
Text isNil ifTrue:[^ self]. |
6228 | 4944 |
^ self asText colorizeAllWith:aColor |
4945 |
||
4946 |
" |
|
4947 |
Transcript showCR:('hello' colorizeAllWith:Color red) |
|
4948 |
Transcript showCR:('world' colorizeAllWith:Color green darkened) |
|
4949 |
" |
|
4950 |
! |
|
4951 |
||
12330
f9b57b0b8a8b
added: #colorizeAllWith:on:
Claus Gittinger <cg@exept.de>
parents:
12326
diff
changeset
|
4952 |
colorizeAllWith:fgColor on:bgColor |
f9b57b0b8a8b
added: #colorizeAllWith:on:
Claus Gittinger <cg@exept.de>
parents:
12326
diff
changeset
|
4953 |
"return a text object representing the receiver, but all colorized with |
f9b57b0b8a8b
added: #colorizeAllWith:on:
Claus Gittinger <cg@exept.de>
parents:
12326
diff
changeset
|
4954 |
both fg and background color" |
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 |
"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
|
4957 |
Text isNil ifTrue:[^ self]. |
f9b57b0b8a8b
added: #colorizeAllWith:on:
Claus Gittinger <cg@exept.de>
parents:
12326
diff
changeset
|
4958 |
^ self asText colorizeAllWith:fgColor on:bgColor |
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 |
" |
f9b57b0b8a8b
added: #colorizeAllWith:on:
Claus Gittinger <cg@exept.de>
parents:
12326
diff
changeset
|
4961 |
Transcript showCR:('hello' colorizeAllWith:Color red on:Color yellow) |
f9b57b0b8a8b
added: #colorizeAllWith:on:
Claus Gittinger <cg@exept.de>
parents:
12326
diff
changeset
|
4962 |
Transcript showCR:('world' colorizeAllWith:Color red) |
f9b57b0b8a8b
added: #colorizeAllWith:on:
Claus Gittinger <cg@exept.de>
parents:
12326
diff
changeset
|
4963 |
" |
f9b57b0b8a8b
added: #colorizeAllWith:on:
Claus Gittinger <cg@exept.de>
parents:
12326
diff
changeset
|
4964 |
! |
f9b57b0b8a8b
added: #colorizeAllWith:on:
Claus Gittinger <cg@exept.de>
parents:
12326
diff
changeset
|
4965 |
|
1396
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4966 |
emphasis |
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4967 |
"return the emphasis. |
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4968 |
Since characterArrays do not hold any emphasis information, |
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4969 |
nil (no emphasis) is returned here." |
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4970 |
|
23060 | 4971 |
^ RunArray new:(self size) withAll:nil |
4972 |
||
4973 |
"Created: / 14-05-1996 / 13:58:58 / cg" |
|
4974 |
"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
|
4975 |
! |
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4976 |
|
6655 | 4977 |
emphasis:emphasisCollection |
4978 |
^ self asText emphasis:emphasisCollection |
|
4979 |
||
4980 |
" |
|
4981 |
Transcript showCR:('hello' emphasis:#(bold bold bold bold bold)) |
|
4982 |
" |
|
4983 |
! |
|
4984 |
||
1375
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
4985 |
emphasisAt:characterIndex |
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
4986 |
"return the emphasis at some index. |
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
4987 |
Since characterArrays do not hold any emphasis information, |
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
4988 |
nil (no emphasis) is returned here." |
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
4989 |
|
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
4990 |
^ nil |
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
4991 |
|
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
4992 |
"Created: 11.5.1996 / 14:13:27 / cg" |
1396
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4993 |
! |
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4994 |
|
11632
b82e9f3cabd1
new: #actionForAll: #emphasisAtPoint:on:
Stefan Vogel <sv@exept.de>
parents:
11630
diff
changeset
|
4995 |
emphasisAtPoint:aPoint on:aGC |
b82e9f3cabd1
new: #actionForAll: #emphasisAtPoint:on:
Stefan Vogel <sv@exept.de>
parents:
11630
diff
changeset
|
4996 |
"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
|
4997 |
|
b82e9f3cabd1
new: #actionForAll: #emphasisAtPoint:on:
Stefan Vogel <sv@exept.de>
parents:
11630
diff
changeset
|
4998 |
^ nil |
b82e9f3cabd1
new: #actionForAll: #emphasisAtPoint:on:
Stefan Vogel <sv@exept.de>
parents:
11630
diff
changeset
|
4999 |
! |
b82e9f3cabd1
new: #actionForAll: #emphasisAtPoint:on:
Stefan Vogel <sv@exept.de>
parents:
11630
diff
changeset
|
5000 |
|
1396
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
5001 |
emphasisCollection |
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
5002 |
"return the emphasis. |
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
5003 |
Since characterArrays do not hold any emphasis information, |
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
5004 |
nil (no emphasis) is returned here." |
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
5005 |
|
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
5006 |
^ RunArray new:(self size) |
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
5007 |
|
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
5008 |
"Created: 14.5.1996 / 13:58:58 / cg" |
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
5009 |
"Modified: 14.5.1996 / 15:02:29 / cg" |
3404 | 5010 |
! |
5011 |
||
11919 | 5012 |
emphasiseFrom:start to:stop with:newEmphasis |
5013 |
"set to the emphasis within some range. return the receiver" |
|
5014 |
||
5015 |
^ self asText emphasiseFrom:start to:stop with:newEmphasis |
|
5016 |
||
5017 |
" |
|
12177 | 5018 |
'hello' emphasiseFrom:2 with:#italic |
11919 | 5019 |
|
5020 |
Transcript showCR:('hello' emphasiseFrom:2 with:#italic) |
|
5021 |
" |
|
5022 |
! |
|
5023 |
||
5024 |
emphasiseFrom:start with:newEmphasis |
|
5025 |
"set to the emphasis within some range. return the receiver" |
|
5026 |
||
5027 |
^ self emphasiseFrom:start to:(self size) with:newEmphasis |
|
5028 |
||
5029 |
" |
|
12177 | 5030 |
'hello' emphasiseFrom:2 with:#italic |
11919 | 5031 |
|
5032 |
Transcript showCR:('hello' emphasiseFrom:2 with:#italic) |
|
5033 |
" |
|
5034 |
! |
|
5035 |
||
3404 | 5036 |
emphasizeAllWith:emphasis |
12326
c67f12213ddb
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
12319
diff
changeset
|
5037 |
"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
|
5038 |
|
c67f12213ddb
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
12319
diff
changeset
|
5039 |
"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
|
5040 |
Text isNil ifTrue:[^ self]. |
3404 | 5041 |
^ self asText emphasizeAllWith:emphasis |
5042 |
||
5043 |
" |
|
5044 |
Transcript showCR:('hello' emphasizeAllWith:#bold) |
|
3599 | 5045 |
Transcript showCR:('hello' emphasizeAllWith:(#color -> Color red)) |
5046 |
Transcript showCR:('hello' emphasizeAllWith:(#color -> Color red)) |
|
5047 |
" |
|
5048 |
||
5049 |
"Modified: / 17.6.1998 / 12:51:44 / cg" |
|
5097 | 5050 |
! |
5051 |
||
5052 |
makeSelectorBoldIn:aClass |
|
14050 | 5053 |
"the receiver represents some source code for a method in aClass. |
5097 | 5054 |
Change myself to boldify the selector. |
14050 | 5055 |
Not yet implemented (could easily use the syntaxHighlighter for this ...). |
5056 |
For protocol compatibility with other smalltalks" |
|
5097 | 5057 |
|
5058 |
^ self |
|
5059 |
||
14050 | 5060 |
"Created: / 13-12-1999 / 21:49:24 / cg" |
5061 |
! |
|
5062 |
||
19762 | 5063 |
withColor:aColorOrColorSymbol |
20697 | 5064 |
"return a text object representing the receiver, but all colorized. |
5065 |
Usage of a colorSymbol is considered bad style (provided for backward compatibility); |
|
5066 |
please change to pass a proper color |
|
5067 |
(makes it easier to find color uses)" |
|
5068 |
||
5069 |
|color| |
|
5070 |
||
19762 | 5071 |
aColorOrColorSymbol isSymbol ifTrue:[ |
20697 | 5072 |
color := (Color perform:aColorOrColorSymbol) |
5073 |
] ifFalse:[ |
|
5074 |
color := aColorOrColorSymbol |
|
5075 |
]. |
|
5076 |
^ self colorizeAllWith:color |
|
19762 | 5077 |
|
5078 |
" |
|
5079 |
Transcript showCR:('hello' withColor:#red) |
|
20697 | 5080 |
Transcript showCR:('world' withColor:#blue) |
16697 | 5081 |
Transcript showCR:('hello' withColor:Color red) |
5082 |
Transcript showCR:('world' withColor:Color green darkened) |
|
5083 |
" |
|
5084 |
! |
|
5085 |
||
22911 | 5086 |
withColor:foregroundColorOrColorSymbol on:backgroundColorOrColorSymbol |
5087 |
"return a text object representing the receiver, but all colorized with both fg and bg. |
|
5088 |
Usage of a colorSymbol is considered bad style (provided for backward compatibility); |
|
5089 |
please change to pass a proper color |
|
5090 |
(makes it easier to find color uses)" |
|
5091 |
||
5092 |
|fgColor bgColor| |
|
5093 |
||
5094 |
foregroundColorOrColorSymbol isSymbol ifTrue:[ |
|
5095 |
fgColor := (Color perform:foregroundColorOrColorSymbol) |
|
5096 |
] ifFalse:[ |
|
5097 |
fgColor := foregroundColorOrColorSymbol |
|
5098 |
]. |
|
5099 |
backgroundColorOrColorSymbol isSymbol ifTrue:[ |
|
5100 |
bgColor := (Color perform:backgroundColorOrColorSymbol) |
|
5101 |
] ifFalse:[ |
|
5102 |
bgColor := backgroundColorOrColorSymbol |
|
5103 |
]. |
|
5104 |
^ self colorizeAllWith:fgColor on:bgColor |
|
5105 |
||
5106 |
" |
|
5107 |
Transcript showCR:('hello' withColor:#red on:#blue) |
|
5108 |
Transcript showCR:('hello' withColor:Color red on:Color blue) |
|
5109 |
" |
|
5110 |
! |
|
5111 |
||
14050 | 5112 |
withoutAnyColorEmphasis |
5113 |
"for protocol compatibility with Text" |
|
5114 |
||
5115 |
^ self |
|
5116 |
||
5117 |
"Created: / 06-03-2012 / 18:15:38 / cg" |
|
6653 | 5118 |
! |
5119 |
||
12396 | 5120 |
withoutBackgroundColorEmphasis |
14050 | 5121 |
"for protocol compatibility with Text" |
5122 |
||
12396 | 5123 |
^ self |
14050 | 5124 |
|
5125 |
"Modified (comment): / 06-03-2012 / 18:14:27 / cg" |
|
12396 | 5126 |
! |
5127 |
||
23903 | 5128 |
withoutEmphasis |
5129 |
"for protocol compatibility with Text" |
|
5130 |
||
5131 |
^ self |
|
5132 |
||
5133 |
"Created: / 13-03-2019 / 21:03:41 / Claus Gittinger" |
|
5134 |
! |
|
5135 |
||
6653 | 5136 |
withoutEmphasis:emphasisToRemove |
14050 | 5137 |
"for protocol compatibility with Text" |
5138 |
||
6653 | 5139 |
^ self |
14050 | 5140 |
|
5141 |
"Modified (comment): / 06-03-2012 / 18:14:29 / cg" |
|
12396 | 5142 |
! |
5143 |
||
5144 |
withoutForegroundColorEmphasis |
|
14050 | 5145 |
"for protocol compatibility with Text" |
5146 |
||
12396 | 5147 |
^ self |
14050 | 5148 |
|
5149 |
"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
|
5150 |
! ! |
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
5151 |
|
7265 | 5152 |
!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
|
5153 |
|
20013 | 5154 |
asDenormalizedUnicodeString |
5155 |
"return a new string containing the same characters, as a denormalized Unicode string. |
|
5156 |
This replaces diacritical chars (umlauts, accented chars etc) by |
|
5157 |
a sequence with combination characters. |
|
5158 |
(i.e. a plain character followed by a combining diacritical in the 0x03xx range)" |
|
5159 |
||
20014 | 5160 |
|map outStream mapChar| |
20013 | 5161 |
|
22406 | 5162 |
self containsNon7BitAscii ifFalse:[ |
5163 |
^ self "/ I cannot contain any diacritical chars |
|
5164 |
]. |
|
5165 |
||
20013 | 5166 |
map := self class unicodeDenormalizationMap. |
22406 | 5167 |
mapChar := [:char | |
20014 | 5168 |
|mappedChars| |
5169 |
||
5170 |
(mappedChars := map at:char ifAbsent:nil) notNil ifTrue:[ |
|
5171 |
mappedChars do:mapChar. |
|
5172 |
] ifFalse:[ |
|
5173 |
outStream nextPut:char. |
|
5174 |
]. |
|
5175 |
]. |
|
20013 | 5176 |
|
20014 | 5177 |
outStream := WriteStream on:(Unicode16String new:self size). |
5178 |
self do:mapChar. |
|
20013 | 5179 |
^ outStream contents asSingleByteStringIfPossible. |
5180 |
||
5181 |
" |
|
5182 |
'Ö' asDenormalizedUnicodeString |
|
5183 |
'aÖÄx' asDenormalizedUnicodeString |
|
5184 |
'abc' asDenormalizedUnicodeString |
|
22406 | 5185 |
'ṩ' asDenormalizedUnicodeString |
5186 |
" |
|
5187 |
||
5188 |
"Modified (format): / 02-01-2018 / 18:52:33 / stefan" |
|
20013 | 5189 |
! |
5190 |
||
20011 | 5191 |
asNormalizedUnicodeString |
20010 | 5192 |
"return a new string containing the same characters, as a normalized Unicode string. |
5193 |
This replaces combination characters by corresponding single characters. |
|
20863 | 5194 |
(i.e. diaresis and other combining diacriticals in the 0x03xx range). |
5195 |
Caveat: |
|
5196 |
possibly incomplete: only COMBINING_DIACRITICAL_MARKS are cared for. |
|
5197 |
Does not care for COMBINING_DIACRITICAL_MARKS_EXTENDED |
|
20864 | 5198 |
and COMBINING_DIACRITICAL_MARKS_SUPPLEMENT. |
5199 |
However; those are used for German dialectology, ancient Greek and other similar |
|
5200 |
exotic uses. Probably noone will ever even notice that they are missing..." |
|
20010 | 5201 |
|
5202 |
|outStream prevChar map mapEntries mappedChar| |
|
5203 |
||
5204 |
map := self class unicodeNormalizationMap. |
|
20017 | 5205 |
|
20010 | 5206 |
self bitsPerCharacter <= 8 ifTrue:[^ self]. "/ I cannot contain any overtypes |
20017 | 5207 |
(self contains:[:ch | ch unicodeBlock == #'COMBINING_DIACRITICAL_MARKS']) ifFalse:[^ self]. "/ I do not contain any overtypes |
20010 | 5208 |
|
5209 |
outStream := WriteStream on:(self species new:self size). |
|
5210 |
self do:[:char | |
|
20017 | 5211 |
((char unicodeBlock == #'COMBINING_DIACRITICAL_MARKS') |
20010 | 5212 |
and:[ (mapEntries := map at:char ifAbsent:nil) notNil |
5213 |
and:[ (mappedChar := mapEntries at:prevChar ifAbsent:nil) notNil ]]) ifTrue:[ |
|
20014 | 5214 |
prevChar := mappedChar. |
20010 | 5215 |
] ifFalse:[ |
5216 |
prevChar notNil ifTrue:[ outStream nextPut:prevChar]. |
|
5217 |
prevChar := char |
|
5218 |
]. |
|
5219 |
]. |
|
20011 | 5220 |
prevChar notNil ifTrue:[ outStream nextPut:prevChar]. |
20010 | 5221 |
^ outStream contents asSingleByteStringIfPossible. |
5222 |
||
5223 |
" |
|
5224 |
self unicodeNormalizationMap |
|
20012 | 5225 |
('O' , (Character value:16r0308)) asNormalizedUnicodeString -> 'Ö' |
20010 | 5226 |
('O' , (Character value:16r0308) , |
20012 | 5227 |
'A' , (Character value:16r0308)) asNormalizedUnicodeString -> 'ÖÄ' |
20014 | 5228 |
('s' , (Character value:16r0323) , (Character value:16r0307)) asNormalizedUnicodeString -> 'ṩ' |
5229 |
('s' , (Character value:16r0307) , (Character value:16r0323)) asNormalizedUnicodeString -> 'ṩ' |
|
20010 | 5230 |
" |
5231 |
! |
|
5232 |
||
12767
17d2c7268065
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12679
diff
changeset
|
5233 |
decodeAsLiteralArray |
17d2c7268065
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12679
diff
changeset
|
5234 |
"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
|
5235 |
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
|
5236 |
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
|
5237 |
|
17d2c7268065
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12679
diff
changeset
|
5238 |
^ self |
17d2c7268065
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12679
diff
changeset
|
5239 |
! |
17d2c7268065
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12679
diff
changeset
|
5240 |
|
991
f0b45a4b82d2
first attempt in encoding/decoding for different character representations (mac/msdos etc.)
Claus Gittinger <cg@exept.de>
parents:
986
diff
changeset
|
5241 |
decodeFrom:encodingSymbol |
8874 | 5242 |
"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
|
5243 |
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
|
5244 |
|
ca774158b317
code cleanup (removed old obsolete encoding stuff)
Claus Gittinger <cg@exept.de>
parents:
8102
diff
changeset
|
5245 |
|myEncoding encoder| |
7950 | 5246 |
|
5247 |
encodingSymbol isNil ifTrue:[^ self]. |
|
5248 |
myEncoding := self encoding. |
|
5249 |
encodingSymbol == myEncoding ifTrue:[^ self]. |
|
5250 |
||
22451 | 5251 |
encoder := CharacterEncoder encoderToEncodeFrom:myEncoding into:encodingSymbol. |
8124
ca774158b317
code cleanup (removed old obsolete encoding stuff)
Claus Gittinger <cg@exept.de>
parents:
8102
diff
changeset
|
5252 |
encoder isNil ifTrue:[^ self]. |
ca774158b317
code cleanup (removed old obsolete encoding stuff)
Claus Gittinger <cg@exept.de>
parents:
8102
diff
changeset
|
5253 |
^ encoder decodeString:self. |
22451 | 5254 |
|
5255 |
"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
|
5256 |
! |
f0b45a4b82d2
first attempt in encoding/decoding for different character representations (mac/msdos etc.)
Claus Gittinger <cg@exept.de>
parents:
986
diff
changeset
|
5257 |
|
9192 | 5258 |
encodeFrom:oldEncoding into:newEncoding |
5259 |
^ CharacterEncoder encodeString:self from:oldEncoding into:newEncoding |
|
14405
1d0d99a95bba
comment/format in:7 methods
Claus Gittinger <cg@exept.de>
parents:
14168
diff
changeset
|
5260 |
|
1d0d99a95bba
comment/format in:7 methods
Claus Gittinger <cg@exept.de>
parents:
14168
diff
changeset
|
5261 |
" |
17600 | 5262 |
'äüö' encodeFrom:#iso8859 into:#utf8 |
5263 |
('äüö' encodeFrom:#iso8859 into:#utf8) encodeFrom:#utf8 into:#unicode |
|
14405
1d0d99a95bba
comment/format in:7 methods
Claus Gittinger <cg@exept.de>
parents:
14168
diff
changeset
|
5264 |
" |
9192 | 5265 |
! |
5266 |
||
22451 | 5267 |
encodeInto:newEncoding |
5268 |
^ CharacterEncoder encodeString:self from:self encoding into:newEncoding |
|
5269 |
||
5270 |
" |
|
5271 |
'äüö' encodeInto:#utf8 |
|
5272 |
('äüö' encodeInto:#utf8) decodeFrom:#utf8 |
|
5273 |
" |
|
5274 |
||
5275 |
"Created: / 17-01-2018 / 17:43:20 / stefan" |
|
5276 |
! |
|
5277 |
||
9192 | 5278 |
rot13 |
8874 | 5279 |
"Usenet: from `rotate alphabet 13 places'] |
6835
bdb2cb03200c
added utf8Decode with 8-bit chars only
Claus Gittinger <cg@exept.de>
parents:
6834
diff
changeset
|
5280 |
The simple Caesar-cypher encryption that replaces each English |
8874 | 5281 |
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
|
5282 |
so that 'The butler did it!!' becomes 'Gur ohgyre qvq vg!!' |
8874 | 5283 |
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
|
5284 |
It is used to enclose the text in a sealed wrapper that the reader must choose |
8874 | 5285 |
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
|
5286 |
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
|
5287 |
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
|
5288 |
|
11865 | 5289 |
^ self rot:13 |
8874 | 5290 |
|
5291 |
" |
|
5292 |
'hello world' rot13 |
|
5293 |
'hello world' rot13 rot13 |
|
6835
bdb2cb03200c
added utf8Decode with 8-bit chars only
Claus Gittinger <cg@exept.de>
parents:
6834
diff
changeset
|
5294 |
" |
bdb2cb03200c
added utf8Decode with 8-bit chars only
Claus Gittinger <cg@exept.de>
parents:
6834
diff
changeset
|
5295 |
! |
bdb2cb03200c
added utf8Decode with 8-bit chars only
Claus Gittinger <cg@exept.de>
parents:
6834
diff
changeset
|
5296 |
|
11865 | 5297 |
rot:n |
5298 |
"Usenet: from `rotate alphabet N places'] |
|
5299 |
The simple Caesar-cypher encryption that replaces each English |
|
5300 |
letter with the one N places forward or back along the alphabet, |
|
5301 |
so that 'The butler did it!!' becomes 'Gur ohgyre qvq vg!!' by rot 13 |
|
5302 |
Most Usenet news reading and posting programs include a rot13 feature. |
|
5303 |
It is used to enclose the text in a sealed wrapper that the reader must choose |
|
5304 |
to open -- e.g., for posting things that might offend some readers, or spoilers. |
|
5305 |
A major advantage of rot13 over rot(N) for other N is that it |
|
5306 |
is self-inverse, so the same code can be used for encoding and decoding." |
|
5307 |
||
5308 |
^ self species |
|
20874 | 5309 |
streamContents:[:aStream | |
5310 |
self do:[:char | |
|
5311 |
aStream nextPut:(char rot:n) ]] |
|
11865 | 5312 |
|
5313 |
" |
|
5314 |
'hello world' rot:13 |
|
5315 |
('hello world' rot:13) rot:13 |
|
5316 |
" |
|
5317 |
! |
|
5318 |
||
15656 | 5319 |
utf16Encoded |
5320 |
"Return my UTF-16 representation as a new TwoByteString" |
|
5321 |
||
5322 |
|s| |
|
5323 |
||
5324 |
s := WriteStream on:(TwoByteString uninitializedNew:self size). |
|
5325 |
s nextPutAllUtf16:self. |
|
5326 |
^ s contents |
|
5327 |
||
5328 |
" |
|
5329 |
'abcde1234' utf16Encoded |
|
17600 | 5330 |
'abcdeäöüß' utf16Encoded |
15656 | 5331 |
" |
5332 |
||
5333 |
"Modified: / 11-05-2010 / 19:12:37 / cg" |
|
5334 |
! |
|
5335 |
||
9192 | 5336 |
utf8Encoded |
19837 | 5337 |
"return the UTF-8 representation of a Unicode string. |
5338 |
The resulting string is only useful to be stored on some external file, |
|
21318 | 5339 |
or sent to a communaction channel. |
5340 |
Not for being used inside ST/X." |
|
19837 | 5341 |
|
5342 |
|string stream| |
|
17625 | 5343 |
|
5344 |
string := self string. |
|
19837 | 5345 |
|
5346 |
"/ avoid creation of new strings if possible |
|
5347 |
string containsNon7BitAscii ifFalse:[ |
|
5348 |
^ string asSingleByteString |
|
5349 |
]. |
|
5350 |
||
5351 |
"make it size 2 for 1-byte strings" |
|
5352 |
stream := WriteStream on:(String uninitializedNew:(1 + (string size * 3 // 2))). |
|
5353 |
string utf8EncodedOn:stream. |
|
5354 |
^ stream contents. |
|
5355 |
||
5356 |
" |
|
5357 |
'hello' utf8Encoded asByteArray #[104 101 108 108 111] |
|
5358 |
(Character value:16r40) asString utf8Encoded asByteArray #[64] |
|
5359 |
(Character value:16r7F) asString utf8Encoded asByteArray #[127] |
|
5360 |
(Character value:16r80) asString utf8Encoded asByteArray #[194 128] |
|
5361 |
(Character value:16rFF) asString utf8Encoded asByteArray #[195 191] |
|
5362 |
(Character value:16r100) asString utf8Encoded asByteArray #[196 128] |
|
5363 |
(Character value:16r200) asString utf8Encoded asByteArray #[200 128] |
|
5364 |
(Character value:16r400) asString utf8Encoded asByteArray #[208 128] |
|
5365 |
(Character value:16r800) asString utf8Encoded asByteArray #[224 160 128] |
|
5366 |
(Character value:16r1000) asString utf8Encoded asByteArray #[225 128 128] |
|
5367 |
(Character value:16r2000) asString utf8Encoded asByteArray #[226 128 128] |
|
5368 |
(Character value:16r4000) asString utf8Encoded asByteArray #[228 128 128] |
|
5369 |
(Character value:16r8000) asString utf8Encoded asByteArray #[232 128 128] |
|
5370 |
(Character value:16rFFFF) asString utf8Encoded asByteArray #[239 191 191] |
|
21318 | 5371 |
(Character value:16r1FFFF) asString utf8Encoded asByteArray #[240 159 191 191] |
5372 |
(Character value:16r3FFFF) asString utf8Encoded asByteArray #[240 191 191 191] |
|
5373 |
(Character value:16rFFFFF) asString utf8Encoded asByteArray #[243 191 191 191] |
|
5374 |
(Character value:16r3FFFFF) asString utf8Encoded asByteArray #[248 143 191 191 191] |
|
19837 | 5375 |
|
18597 | 5376 |
'abcde1234' asUnicode32String utf8Encoded |
5377 |
'abcdeäöüß' asUnicode32String utf8Encoded |
|
12925
5c0a9a633ca1
comment/format in: #utf8Encoded
Claus Gittinger <cg@exept.de>
parents:
12912
diff
changeset
|
5378 |
" |
17625 | 5379 |
! |
5380 |
||
5381 |
utf8EncodedOn:aStream |
|
21468 | 5382 |
"write the UTF-8 representation of myself to aStream." |
5383 |
||
5384 |
aStream nextPutAllUtf8:self. |
|
5385 |
||
5386 |
"Modified: / 16-02-2017 / 16:51:32 / stefan" |
|
9192 | 5387 |
! ! |
5388 |
||
22566 | 5389 |
!CharacterArray methodsFor:'filling and replacing'! |
5390 |
||
5391 |
clearContents |
|
5392 |
"to be used with cryptographic keys, to wipe their contents after use" |
|
5393 |
||
5394 |
self atAllPut:(Character value:0) |
|
5395 |
! ! |
|
22451 | 5396 |
|
5397 |
||
24250 | 5398 |
|
5399 |
||
12111 | 5400 |
!CharacterArray methodsFor:'matching - glob expressions'! |
5401 |
||
5402 |
compoundMatch:aString |
|
5403 |
"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
|
5404 |
consisting of multiple simple GLOB patterns, separated by semicolons. |
12111 | 5405 |
This is usable with fileName pattern fields. |
23791 | 5406 |
This is a case sensitive match: lower/uppercase are considered different. |
12111 | 5407 |
|
5408 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20874 | 5409 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5410 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 5411 |
NOTICE: the receiver is the match pattern" |
5412 |
||
15890 | 5413 |
^ self compoundMatch:aString caseSensitive:true withoutSeparators:false |
12111 | 5414 |
|
5415 |
" |
|
5416 |
'f*' match:'foo' |
|
5417 |
'b*' match:'foo' |
|
5418 |
'f*;b*' match:'foo' |
|
5419 |
'f*;b*' match:'bar' |
|
5420 |
'f*;b*' compoundMatch:'foo' |
|
5421 |
'f*;b*' compoundMatch:'bar' |
|
23791 | 5422 |
'*.png;*.gif' compoundMatch:'bar.jpg' |
5423 |
'*.png;*.gif' compoundMatch:'bar.gif' |
|
5424 |
" |
|
5425 |
||
5426 |
"Modified: / 30-01-1998 / 11:40:18 / stefan" |
|
5427 |
"Modified: / 16-12-1999 / 01:22:08 / cg" |
|
5428 |
"Modified (comment): / 02-03-2019 / 12:21:33 / Claus Gittinger" |
|
12111 | 5429 |
! |
5430 |
||
15734 | 5431 |
compoundMatch:aString caseSensitive:caseSensitive |
12111 | 5432 |
"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
|
5433 |
consisting of multiple simple GLOB patterns, separated by semicolons. |
12111 | 5434 |
This is usable with fileName pattern fields. |
5435 |
||
5436 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20874 | 5437 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5438 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 5439 |
NOTICE: the receiver is the match pattern" |
5440 |
||
15890 | 5441 |
^self compoundMatch:aString caseSensitive:caseSensitive withoutSeparators:false |
5442 |
||
5443 |
" |
|
5444 |
'f*' match:'foo' |
|
5445 |
'b*' match:'foo' |
|
5446 |
'f*;b*' match:'foo' |
|
5447 |
'f*;b*' match:'bar' |
|
5448 |
'f*;b*' compoundMatch:'foo' |
|
5449 |
'f*;b*' compoundMatch:'bar' |
|
5450 |
'f*;b*' compoundMatch:'Foo' caseSensitive:false |
|
5451 |
'f*;b*' compoundMatch:'Bar' caseSensitive:false |
|
5452 |
'f*;b*' compoundMatch:'ccc' caseSensitive:false |
|
23791 | 5453 |
|
5454 |
'*.png;*.gif' compoundMatch:'bar.GIF' |
|
5455 |
'*.png;*.gif' compoundMatch:'bar.GIF' caseSensitive:false |
|
5456 |
" |
|
5457 |
||
5458 |
"Modified: / 15-04-1997 / 15:50:33 / cg" |
|
5459 |
"Modified: / 30-01-1998 / 11:40:18 / stefan" |
|
5460 |
"Created: / 16-12-1999 / 01:21:35 / cg" |
|
5461 |
"Modified (comment): / 02-03-2019 / 12:22:14 / Claus Gittinger" |
|
15890 | 5462 |
! |
5463 |
||
5464 |
compoundMatch:aString caseSensitive:caseSensitive withoutSeparators:withoutSeparators |
|
5465 |
"like match, but the receiver may be a compound match pattern, |
|
5466 |
consisting of multiple simple GLOB patterns, separated by semicolons. |
|
5467 |
If withoutSeparators is true, spaces around individual patterns are stripped off. |
|
5468 |
This is usable with fileName pattern fields. |
|
5469 |
||
5470 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20874 | 5471 |
NOT the ST-80 meaning. |
15890 | 5472 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
5473 |
NOTICE: the receiver is the match pattern" |
|
5474 |
||
12111 | 5475 |
|matchers| |
5476 |
||
5477 |
matchers := self asCollectionOfSubstringsSeparatedBy:$;. |
|
15890 | 5478 |
withoutSeparators ifTrue:[ matchers := matchers collect:[:each | each withoutSeparators] ]. |
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
5479 |
^ matchers |
20874 | 5480 |
contains:[:aPattern | |
5481 |
aPattern match:aString caseSensitive:caseSensitive escapeCharacter:nil |
|
5482 |
]. |
|
15734 | 5483 |
|
5484 |
" |
|
5485 |
'f*' match:'foo' |
|
5486 |
'b*' match:'foo' |
|
5487 |
'f*;b*' match:'foo' |
|
5488 |
'f*;b*' match:'bar' |
|
5489 |
'f*;b*' compoundMatch:'foo' |
|
5490 |
'f*;b*' compoundMatch:'bar' |
|
5491 |
'f*;b*' compoundMatch:'Foo' caseSensitive:false |
|
5492 |
'f*;b*' compoundMatch:'Bar' caseSensitive:false |
|
5493 |
'f*;b*' compoundMatch:'ccc' caseSensitive:false |
|
15890 | 5494 |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
5495 |
'f* ; b*' compoundMatch:'foo' |
15890 | 5496 |
'f* ; b*' compoundMatch:'foo' caseSensitive:true withoutSeparators:true |
15734 | 5497 |
" |
5498 |
||
5499 |
"Modified: / 15.4.1997 / 15:50:33 / cg" |
|
5500 |
"Modified: / 30.1.1998 / 11:40:18 / stefan" |
|
5501 |
"Created: / 16.12.1999 / 01:21:35 / cg" |
|
5502 |
! |
|
5503 |
||
5504 |
compoundMatch:aString ignoreCase:ignoreCase |
|
5505 |
<resource: #obsolete> |
|
5506 |
||
5507 |
"like match, but the receiver may be a compound match pattern, |
|
5508 |
consisting of multiple simple GLOB patterns, separated by semicolons. |
|
5509 |
This is usable with fileName pattern fields. |
|
5510 |
||
5511 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20874 | 5512 |
NOT the ST-80 meaning. |
15734 | 5513 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
5514 |
NOTICE: the receiver is the match pattern" |
|
5515 |
||
5516 |
^ self compoundMatch:aString caseSensitive:ignoreCase not |
|
12111 | 5517 |
|
5518 |
" |
|
5519 |
'f*' match:'foo' |
|
5520 |
'b*' match:'foo' |
|
5521 |
'f*;b*' match:'foo' |
|
5522 |
'f*;b*' match:'bar' |
|
5523 |
'f*;b*' compoundMatch:'foo' |
|
5524 |
'f*;b*' compoundMatch:'bar' |
|
5525 |
'f*;b*' compoundMatch:'Foo' ignoreCase:true |
|
5526 |
'f*;b*' compoundMatch:'Bar' ignoreCase:true |
|
5527 |
'f*;b*' compoundMatch:'ccc' ignoreCase:true |
|
5528 |
" |
|
5529 |
||
5530 |
"Modified: / 15.4.1997 / 15:50:33 / cg" |
|
5531 |
"Modified: / 30.1.1998 / 11:40:18 / stefan" |
|
5532 |
"Created: / 16.12.1999 / 01:21:35 / cg" |
|
5533 |
! |
|
5534 |
||
5535 |
findMatchString:matchString |
|
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5536 |
"like findString/indexOfSubCollection, but allowing GLOB match patterns. |
23791 | 5537 |
find matchstring; if found, return the index, if not, return 0. |
5538 |
This is a case sensitive match: lower/uppercase are considered different. |
|
12111 | 5539 |
|
5540 |
NOTICE: match-meta character interpretation is like in unix-matching, |
|
20874 | 5541 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5542 |
NOTICE: this GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 5543 |
NOTICE: the argument is the match pattern" |
5544 |
||
19033 | 5545 |
^ self findMatchString:matchString startingAt:1 caseSensitive:true ifAbsent:0 |
5546 |
||
5547 |
" |
|
5548 |
1234567890123 |
|
5549 |
'hello world bla foo baz' findMatchString:'b* ' |
|
5550 |
" |
|
23791 | 5551 |
|
5552 |
"Modified (comment): / 02-03-2019 / 12:21:07 / Claus Gittinger" |
|
12111 | 5553 |
! |
5554 |
||
5555 |
findMatchString:matchString startingAt:index |
|
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5556 |
"like findString, but allowing GLOB match patterns. |
23791 | 5557 |
find matchstring, starting at index; if found, return the index, if not, return 0. |
5558 |
This is a case sensitive match: lower/uppercase are considered different. |
|
12111 | 5559 |
|
5560 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20874 | 5561 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5562 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 5563 |
NOTICE: the argument is the match pattern" |
5564 |
||
19033 | 5565 |
^ self findMatchString:matchString startingAt:index caseSensitive:true ifAbsent:0 |
23791 | 5566 |
|
5567 |
"Modified (comment): / 02-03-2019 / 12:21:05 / Claus Gittinger" |
|
12111 | 5568 |
! |
5569 |
||
15734 | 5570 |
findMatchString:matchString startingAt:index caseSensitive:caseSensitive ifAbsent:exceptionBlock |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5571 |
"like findString, but allowing GLOB match patterns. |
12111 | 5572 |
find matchstring, starting at index. if found, return the index; |
5573 |
if not found, return the result of evaluating exceptionBlock. |
|
5574 |
This is a q&d hack - not very efficient. |
|
5575 |
||
5576 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20874 | 5577 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5578 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 5579 |
NOTICE: the argument is the match pattern" |
5580 |
||
5581 |
|firstChar firstSet |
|
5582 |
startIndex "{ Class: SmallInteger }" |
|
5583 |
matchSize "{ Class: SmallInteger }" |
|
5584 |
mySize "{ Class: SmallInteger }" |
|
5585 |
realMatchString lcChar ucChar| |
|
5586 |
||
5587 |
matchSize := matchString size. |
|
5588 |
matchSize == 0 ifTrue:[^ index]. "empty string matches" |
|
5589 |
||
5590 |
realMatchString := matchString. |
|
5591 |
(realMatchString endsWith:$*) ifFalse:[ |
|
20874 | 5592 |
realMatchString := realMatchString , '*'. |
5593 |
matchSize := matchSize + 1 |
|
12111 | 5594 |
]. |
5595 |
||
5596 |
mySize := self size. |
|
5597 |
firstChar := realMatchString at:1. |
|
5598 |
firstChar == self class matchEscapeCharacter ifTrue:[ |
|
20874 | 5599 |
firstChar := realMatchString at:2. |
12111 | 5600 |
]. |
5601 |
||
5602 |
firstChar asString includesMatchCharacters ifTrue:[ |
|
20874 | 5603 |
index to:mySize do:[:col | |
5604 |
(realMatchString match:self from:col to:mySize caseSensitive:caseSensitive) |
|
5605 |
ifTrue:[^ col] |
|
5606 |
]. |
|
5607 |
^ exceptionBlock value. |
|
12111 | 5608 |
]. |
5609 |
||
5610 |
lcChar := firstChar asLowercase. |
|
5611 |
ucChar := firstChar asUppercase. |
|
15734 | 5612 |
(caseSensitive not and:[ lcChar ~= ucChar ]) ifTrue:[ |
20874 | 5613 |
firstSet := Array with:ucChar with:lcChar. |
5614 |
startIndex := self indexOfAny:firstSet startingAt:index. |
|
12111 | 5615 |
] ifFalse:[ |
20874 | 5616 |
startIndex := self indexOf:firstChar startingAt:index. |
12111 | 5617 |
]. |
5618 |
[startIndex == 0] whileFalse:[ |
|
20874 | 5619 |
(realMatchString match:self from:startIndex to:mySize caseSensitive:caseSensitive) |
5620 |
ifTrue:[^ startIndex]. |
|
5621 |
||
5622 |
firstSet notNil ifTrue:[ |
|
5623 |
startIndex := self indexOfAny:firstSet startingAt:(startIndex + 1). |
|
5624 |
] ifFalse:[ |
|
5625 |
startIndex := self indexOf:firstChar startingAt:(startIndex + 1). |
|
5626 |
]. |
|
12111 | 5627 |
]. |
5628 |
^ exceptionBlock value |
|
5629 |
||
5630 |
" |
|
5631 |
'one two three four' findMatchString:'o[nu]' |
|
5632 |
'one two three four' findMatchString:'o[nu]' startingAt:3 |
|
15734 | 5633 |
'one two three four one' findMatchString:'ONE' startingAt:3 caseSensitive:false ifAbsent:0 |
5634 |
'one two three four one' findMatchString:'ONE' startingAt:3 caseSensitive:true ifAbsent:0 |
|
5635 |
'one two three four ONE' findMatchString:'O#E' startingAt:1 caseSensitive:false ifAbsent:0 |
|
5636 |
'one two three four ONE' findMatchString:'O#E' startingAt:1 caseSensitive:true ifAbsent:0 |
|
5637 |
12345678901234567890 |
|
5638 |
" |
|
5639 |
||
5640 |
"Modified: 13.9.1997 / 06:31:22 / cg" |
|
5641 |
! |
|
5642 |
||
5643 |
findMatchString:matchString startingAt:index ignoreCase:ignoreCase ifAbsent:exceptionBlock |
|
5644 |
<resource: #obsolete> |
|
5645 |
"like findString, but allowing GLOB match patterns. |
|
5646 |
find matchstring, starting at index. if found, return the index; |
|
5647 |
if not found, return the result of evaluating exceptionBlock. |
|
5648 |
This is a q&d hack - not very efficient. |
|
5649 |
||
5650 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20874 | 5651 |
NOT the ST-80 meaning. |
15734 | 5652 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
5653 |
NOTICE: the argument is the match pattern" |
|
5654 |
||
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
5655 |
^ self |
20874 | 5656 |
findMatchString:matchString startingAt:index caseSensitive:ignoreCase not ifAbsent:exceptionBlock |
15734 | 5657 |
|
5658 |
" |
|
5659 |
'one two three four' findMatchString:'o[nu]' |
|
5660 |
'one two three four' findMatchString:'o[nu]' startingAt:3 |
|
12111 | 5661 |
'one two three four one' findMatchString:'ONE' startingAt:3 ignoreCase:true ifAbsent:0 |
5662 |
" |
|
5663 |
||
5664 |
"Modified: 13.9.1997 / 06:31:22 / cg" |
|
5665 |
! |
|
5666 |
||
19746 | 5667 |
globPatternAsRegexPattern |
5668 |
"taking the receiver as a GLOB pattern, |
|
5669 |
return a corresponding regex pattern. |
|
5670 |
As regex does provide information about the matching substring, |
|
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
5671 |
it may be useful to apply a regex after a GLOB match, |
19746 | 5672 |
in order to highlight matching substrings (eg. in a CodeView after a search). |
5673 |
If it turns out to be better, we may convert all GLOB searches to regex and use it right away. |
|
5674 |
(currently, it is not sure, if GLOB is not better for most simple searches, as they are encountered |
|
5675 |
in typical real life)" |
|
5676 |
||
5677 |
^ self species streamContents:[:s | |
|
20874 | 5678 |
|addCharacter| |
5679 |
||
5680 |
addCharacter := |
|
5681 |
[:ch | |
|
5682 |
ch isLetterOrDigit ifFalse:[ |
|
5683 |
s nextPut:$\. |
|
5684 |
]. |
|
5685 |
s nextPut:ch |
|
5686 |
]. |
|
5687 |
||
23552 | 5688 |
(self class matchScanArrayFrom:self) do:[:matchEntry | |
20874 | 5689 |
matchEntry isCharacter ifTrue:[ |
5690 |
addCharacter value:matchEntry |
|
5691 |
] ifFalse:[ |
|
5692 |
matchEntry == #anyString ifTrue:[ |
|
5693 |
s nextPutAll:'.*' |
|
5694 |
] ifFalse:[ |
|
5695 |
matchEntry == #any ifTrue:[ |
|
5696 |
s nextPut:$. |
|
5697 |
] ifFalse:[ |
|
5698 |
matchEntry isString ifTrue:[ |
|
5699 |
|set min max| |
|
5700 |
||
5701 |
s nextPut:$[. |
|
23552 | 5702 |
set := matchEntry "copy sort". "/ already sorted |
20874 | 5703 |
min := set min. |
5704 |
max := set max. |
|
5705 |
set asSet = (min to:max) asSet ifTrue:[ |
|
5706 |
addCharacter value:min. |
|
5707 |
s nextPut:$-. |
|
5708 |
addCharacter value:max. |
|
5709 |
] ifFalse:[ |
|
5710 |
set do:addCharacter. |
|
5711 |
]. |
|
5712 |
s nextPut:$]. |
|
5713 |
] ifFalse:[ |
|
22700 | 5714 |
self halt:'funny match entry'. |
20874 | 5715 |
]. |
5716 |
]. |
|
5717 |
]. |
|
5718 |
] |
|
5719 |
]. |
|
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
5720 |
]. |
19746 | 5721 |
|
5722 |
" |
|
5723 |
'hello' globPatternAsRegexPattern |
|
5724 |
'hello*' globPatternAsRegexPattern |
|
5725 |
'*hello*' globPatternAsRegexPattern |
|
5726 |
'h###' globPatternAsRegexPattern |
|
5727 |
'h[0-9]' globPatternAsRegexPattern |
|
5728 |
'h[0-9][0-9][0-9]' globPatternAsRegexPattern |
|
5729 |
'h[0-9]*' globPatternAsRegexPattern |
|
19747 | 5730 |
'h[-+]*' globPatternAsRegexPattern |
23552 | 5731 |
'h[abc]*' globPatternAsRegexPattern |
5732 |
'h[0-9abc]*' globPatternAsRegexPattern |
|
19747 | 5733 |
'hello world' matches:'h*w' |
5734 |
'hello world' matchesRegex:('h*w' globPatternAsRegexPattern) |
|
5735 |
'hello world' matches:'h*d' |
|
5736 |
'hello world' matchesRegex:('h*d' globPatternAsRegexPattern) |
|
19746 | 5737 |
" |
23552 | 5738 |
|
5739 |
"Modified: / 03-12-2018 / 15:47:15 / Stefan Vogel" |
|
19746 | 5740 |
! |
5741 |
||
12111 | 5742 |
includesMatchString:matchString |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5743 |
"like includesString, but allowing GLOB match patterns. |
23791 | 5744 |
find matchstring; if found, return true, false otherwise. |
5745 |
This is a case sensitive match: lower/uppercase are considered different. |
|
12111 | 5746 |
|
5747 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20874 | 5748 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5749 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 5750 |
NOTICE: the argument is the match pattern" |
5751 |
||
5752 |
^ (self findMatchString:matchString) ~~ 0 |
|
5753 |
||
5754 |
" |
|
5755 |
'hello world' includesMatchString:'h*' |
|
5756 |
'hello world' includesMatchString:'h[aeiou]llo' |
|
5757 |
'hello world' includesMatchString:'wor*' |
|
5758 |
'hello world' includesMatchString:'woR*' |
|
23791 | 5759 |
'menue' includesMatchString:'[mM]enu[Ee]' |
5760 |
'blamenuebla' includesMatchString:'[mM]enu[Ee]' |
|
5761 |
'blaMenuebla' includesMatchString:'[mM]enu[Ee]' |
|
5762 |
" |
|
5763 |
||
5764 |
"Modified (comment): / 02-03-2019 / 12:20:58 / Claus Gittinger" |
|
12111 | 5765 |
! |
5766 |
||
5767 |
includesMatchString:matchString caseSensitive:caseSensitive |
|
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5768 |
"like includesString, but allowing GLOB match patterns. |
12111 | 5769 |
find matchstring; if found, return true, otherwise return false. |
5770 |
||
5771 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20874 | 5772 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5773 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 5774 |
NOTICE: the argument is the match pattern" |
5775 |
||
19033 | 5776 |
^ (self findMatchString:matchString startingAt:1 caseSensitive:caseSensitive ifAbsent:0) ~~ 0 |
12111 | 5777 |
|
5778 |
" |
|
5779 |
'hello world' includesMatchString:'h*' caseSensitive:true |
|
5780 |
'hello world' includesMatchString:'h*' caseSensitive:false |
|
5781 |
'Hello world' includesMatchString:'h*' caseSensitive:true |
|
5782 |
'Hello world' includesMatchString:'h*' caseSensitive:false |
|
5783 |
||
5784 |
'hello world' includesMatchString:'h[aeiou]llo' caseSensitive:true |
|
5785 |
'hello world' includesMatchString:'h[aeiou]llo' caseSensitive:false |
|
5786 |
||
5787 |
'hello world' includesMatchString:'wor*' caseSensitive:true |
|
5788 |
'hello world' includesMatchString:'wor*' caseSensitive:false |
|
5789 |
||
5790 |
'hello world' includesMatchString:'woR*' caseSensitive:true |
|
5791 |
'hello world' includesMatchString:'woR*' caseSensitive:false |
|
23791 | 5792 |
|
5793 |
'menue' includesMatchString:'[mM]enu[Ee]' |
|
5794 |
'menue' includesMatchString:'[mM]enu[Ee]' caseSensitive:true |
|
5795 |
'blamenuebla' includesMatchString:'[mM]enu[Ee]' |
|
5796 |
'blamenuebla' includesMatchString:'[mM]enu[Ee]' caseSensitive:true |
|
5797 |
||
5798 |
'blaMenuebla' includesMatchString:'[MS]enu[EF]' |
|
5799 |
'blaMenuebla' includesMatchString:'[MS]enu[EF]' caseSensitive:true |
|
5800 |
'blaMenuebla' includesMatchString:'[MS]enu[EF]' caseSensitive:false |
|
5801 |
" |
|
5802 |
||
5803 |
"Modified (comment): / 02-03-2019 / 12:18:57 / Claus Gittinger" |
|
12111 | 5804 |
! |
5805 |
||
5806 |
match:aString |
|
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5807 |
"return true if aString matches self, where self may contain GLOB meta-match |
12111 | 5808 |
characters $* (to match any string) or $# (to match any character). |
5809 |
or [...] to match a set of characters. |
|
23791 | 5810 |
This is a case sensitive match: lower/uppercase are considered different. |
12111 | 5811 |
The escape character is the backQuote. |
5812 |
||
5813 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20874 | 5814 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5815 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 5816 |
NOTICE: the receiver is the match pattern" |
5817 |
||
15734 | 5818 |
^ self match:aString from:1 to:aString size caseSensitive:true |
12111 | 5819 |
|
5820 |
" |
|
22137 | 5821 |
'#f#' match:'afc' |
5822 |
'#f#' match:'aec' |
|
5823 |
'#f#' match:'ae' |
|
5824 |
'#f#' match:'a' |
|
5825 |
'#f#' match:'f' |
|
5826 |
'#f#' match:'ff' |
|
5827 |
||
12111 | 5828 |
'\*f*' match:'f' |
5829 |
'\*f*' match:'*f' |
|
5830 |
'*\*f*' match:'*f' |
|
5831 |
'*f*' match:'*f' |
|
5832 |
'*ute*' match:'computer' |
|
5833 |
'*uter' match:'computer' |
|
5834 |
'uter*' match:'computer' |
|
5835 |
'*ute*' match:'' |
|
5836 |
'[abcd]*' match:'computer' |
|
5837 |
'[abcd]*' match:'komputer' |
|
5838 |
'*some*compl*ern*' match:'this is some more complicated pattern match' |
|
5839 |
'*some*compl*ern*' match:'this is another complicated pattern match' |
|
5840 |
'*-hh' match:'anton-h' |
|
5841 |
" |
|
5842 |
||
22137 | 5843 |
"Modified: / 09-06-1998 / 18:50:00 / cg" |
5844 |
"Modified (comment): / 29-07-2017 / 14:00:23 / cg" |
|
23791 | 5845 |
"Modified (comment): / 02-03-2019 / 12:20:48 / Claus Gittinger" |
12111 | 5846 |
! |
5847 |
||
15734 | 5848 |
match:aString caseSensitive:caseSensitive |
5849 |
"return true if aString matches self, where self may contain GLOB meta-match |
|
5850 |
characters $* (to match any string) or $# (to match any character) |
|
5851 |
or [...] to match a set of characters. |
|
5852 |
If caseSensitive is false, lower/uppercase are considered the same. |
|
5853 |
The escape character is the backQuote. |
|
5854 |
||
5855 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20874 | 5856 |
NOT the ST-80 meaning. |
15734 | 5857 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
5858 |
NOTICE: the receiver is the match pattern" |
|
5859 |
||
5860 |
^ self match:aString from:1 to:aString size caseSensitive:caseSensitive |
|
5861 |
||
5862 |
" |
|
5863 |
'*ute*' match:'COMPUTER' caseSensitive:false |
|
5864 |
'*uter' match:'COMPUTER' caseSensitive:true |
|
5865 |
'[abcd]*' match:'computer' caseSensitive:true |
|
5866 |
'[abcd]*' match:'Computer' caseSensitive:true |
|
5867 |
'[a-k]*' match:'komputer' caseSensitive:true |
|
5868 |
'[a-k]*' match:'zomputer' caseSensitive:true |
|
5869 |
'[a-k]*' match:'Komputer' caseSensitive:true |
|
5870 |
'[a-k]*' match:'Komputer' caseSensitive:false |
|
5871 |
'*some*compl*ern*' match:'this is some more complicated pattern match' caseSensitive:false |
|
5872 |
'*some*compl*ern*' match:'this is another complicated pattern match' caseSensitive:false |
|
5873 |
||
5874 |
Time millisecondsToRun:[ |
|
20874 | 5875 |
Symbol allInstancesDo:[:sym | |
5876 |
'[ab]*' match:sym caseSensitive:true |
|
5877 |
] |
|
15734 | 5878 |
]. |
5879 |
Time millisecondsToRun:[ |
|
20874 | 5880 |
Symbol allInstancesDo:[:sym | |
5881 |
'*at:*' match:sym caseSensitive:true |
|
5882 |
] |
|
15734 | 5883 |
]. |
5884 |
" |
|
5885 |
||
5886 |
"Modified: 2.4.1997 / 17:28:58 / cg" |
|
5887 |
! |
|
5888 |
||
5889 |
match:aString caseSensitive:caseSensitive escapeCharacter:escape |
|
5890 |
"return true if aString matches self, where self may contain GLOB meta-match |
|
5891 |
characters $* (to match any string) or $# (to match any character) |
|
5892 |
or [...] to match a set of characters. |
|
5893 |
If caseSensitive is false, lower/uppercase are considered the same. |
|
5894 |
||
5895 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20874 | 5896 |
NOT the ST-80 meaning. |
15734 | 5897 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
5898 |
NOTICE: the receiver is the match pattern" |
|
5899 |
||
5900 |
^ self match:aString from:1 to:aString size caseSensitive:caseSensitive escapeCharacter:escape |
|
5901 |
||
5902 |
" |
|
5903 |
'*ute*' match:'COMPUTER' caseSensitive:false |
|
5904 |
'*uter' match:'COMPUTER' caseSensitive:true |
|
5905 |
'[abcd]*' match:'computer' caseSensitive:true |
|
5906 |
'[abcd]*' match:'Computer' caseSensitive:true |
|
5907 |
'[a-k]*' match:'komputer' caseSensitive:true |
|
5908 |
'[a-k]*' match:'zomputer' caseSensitive:true |
|
5909 |
'[a-k]*' match:'Komputer' caseSensitive:true |
|
5910 |
'[a-k]*' match:'Komputer' caseSensitive:false |
|
5911 |
'*some*compl*ern*' match:'this is some more complicated pattern match' caseSensitive:false |
|
5912 |
'*some*compl*ern*' match:'this is another complicated pattern match' caseSensitive:false |
|
5913 |
||
5914 |
Time millisecondsToRun:[ |
|
20874 | 5915 |
Symbol allInstancesDo:[:sym | |
5916 |
'[ab]*' match:sym caseSensitive:true |
|
5917 |
] |
|
15734 | 5918 |
]. |
5919 |
Time millisecondsToRun:[ |
|
20874 | 5920 |
Symbol allInstancesDo:[:sym | |
5921 |
'*at:*' match:sym caseSensitive:true |
|
5922 |
] |
|
15734 | 5923 |
]. |
5924 |
" |
|
5925 |
||
5926 |
"Modified: 2.4.1997 / 17:28:58 / cg" |
|
5927 |
! |
|
5928 |
||
12111 | 5929 |
match:aString escapeCharacter:escape |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5930 |
"return true if aString matches self, where self may contain GLOB meta-match |
12111 | 5931 |
characters $* (to match any string) or $# (to match any character). |
5932 |
or [...] to match a set of characters. |
|
5933 |
Lower/uppercase are considered different. |
|
5934 |
||
5935 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20874 | 5936 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5937 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 5938 |
NOTICE: the receiver is the match pattern" |
5939 |
||
19033 | 5940 |
^ self match:aString from:1 to:aString size caseSensitive:true escapeCharacter:escape |
12111 | 5941 |
|
5942 |
" |
|
5943 |
'a\b\c\*' match:'a\b\c\d' |
|
5944 |
'a\b\c\*' match:'a\b\c\d' escapeCharacter:nil |
|
5945 |
" |
|
5946 |
! |
|
5947 |
||
15734 | 5948 |
match:aString from:start to:stop caseSensitive:caseSensitive |
5949 |
"return true if part of aString matches myself, |
|
5950 |
where self may contain GLOB meta-match |
|
5951 |
characters $* (to match any string) or $# (to match any character) |
|
5952 |
or [...] to match a set of characters. |
|
5953 |
If caseSensitive is false, lower/uppercase are considered the same. |
|
5954 |
The escape character is the backQuote. |
|
5955 |
||
5956 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20874 | 5957 |
NOT the ST-80 meaning. |
15734 | 5958 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
5959 |
NOTICE: the receiver is the match pattern" |
|
5960 |
||
5961 |
^ self |
|
20874 | 5962 |
match:aString from:start to:stop caseSensitive:caseSensitive |
5963 |
escapeCharacter:(self class matchEscapeCharacter) |
|
15734 | 5964 |
|
5965 |
" |
|
5966 |
'*ute*' match:'12345COMPUTER' from:1 to:5 caseSensitive:false |
|
5967 |
'*ute*' match:'12345COMPUTER' from:6 to:13 caseSensitive:false |
|
5968 |
" |
|
5969 |
||
5970 |
"Modified: / 10.11.1998 / 21:43:46 / cg" |
|
5971 |
! |
|
5972 |
||
5973 |
match:aString from:start to:stop caseSensitive:caseSensitive escapeCharacter:escape |
|
5974 |
"return true if part of aString matches myself, |
|
5975 |
where self may contain GLOB meta-match |
|
5976 |
characters $* (to match any string) or $# (to match any character) |
|
5977 |
or [...] to match a set of characters. |
|
5978 |
If caseSensitive is false, lower/uppercase are considered the same. |
|
5979 |
||
5980 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20667 | 5981 |
NOT the ST-80 meaning. |
15734 | 5982 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
5983 |
NOTICE: the receiver is the match pattern" |
|
5984 |
||
5985 |
|matchScanArray| |
|
5986 |
||
5987 |
" |
|
22137 | 5988 |
keep the matchScanArray from the most recent matches - |
5989 |
avoids parsing the pattern over-and over again, |
|
5990 |
if multiple searches are done with the same pattern. |
|
5991 |
" |
|
20667 | 5992 |
(PreviousMatches isNil |
5993 |
or:[(matchScanArray := PreviousMatches at: self ifAbsent:[nil]) isNil]) ifTrue:[ |
|
5994 |
matchScanArray := self class matchScanArrayFrom:self escapeCharacter:escape. |
|
5995 |
matchScanArray isNil ifTrue:[ |
|
23459 | 5996 |
ParseWarning raiseRequestErrorString:('CharacterArray [info]: invalid matchpattern: ''%1'' comparing for equality' bindWith:self). |
20667 | 5997 |
^ self = aString |
23459 | 5998 |
"/ ^ false |
20667 | 5999 |
]. |
6000 |
PreviousMatches isNil ifTrue:[ |
|
22137 | 6001 |
PreviousMatches := CacheDictionary new:15 |
20667 | 6002 |
]. |
6003 |
PreviousMatches at:self put:matchScanArray. |
|
15734 | 6004 |
]. |
6005 |
||
6006 |
^ self class |
|
20667 | 6007 |
matchScan:matchScanArray |
6008 |
from:1 to:matchScanArray size |
|
6009 |
with:aString |
|
6010 |
from:start to:stop |
|
6011 |
caseSensitive:caseSensitive |
|
15734 | 6012 |
|
6013 |
" |
|
6014 |
'*ute*' match:'12345COMPUTER' from:1 to:5 caseSensitive:false |
|
6015 |
'*ute*' match:'12345COMPUTER' from:6 to:13 caseSensitive:false |
|
23459 | 6016 |
|
6017 |
'*[12' match:'12345COMPUTER' from:6 to:13 caseSensitive:false -- gives a warning |
|
15734 | 6018 |
" |
6019 |
||
22137 | 6020 |
"Modified: / 29-07-2017 / 14:01:42 / cg" |
23459 | 6021 |
"Modified (comment): / 24-10-2018 / 09:01:31 / Claus Gittinger" |
15734 | 6022 |
! |
6023 |
||
12111 | 6024 |
match:aString from:start to:stop ignoreCase:ignoreCase |
15734 | 6025 |
<resource: #obsolete> |
12111 | 6026 |
"return true if part of aString matches myself, |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
6027 |
where self may contain GLOB meta-match |
12111 | 6028 |
characters $* (to match any string) or $# (to match any character) |
6029 |
or [...] to match a set of characters. |
|
6030 |
If ignoreCase is true, lower/uppercase are considered the same. |
|
6031 |
The escape character is the backQuote. |
|
6032 |
||
6033 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20874 | 6034 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
6035 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 6036 |
NOTICE: the receiver is the match pattern" |
6037 |
||
6038 |
^ self |
|
20874 | 6039 |
match:aString from:start to:stop caseSensitive:ignoreCase not |
6040 |
escapeCharacter:(self class matchEscapeCharacter) |
|
12111 | 6041 |
|
6042 |
" |
|
6043 |
'*ute*' match:'12345COMPUTER' from:1 to:5 ignoreCase:true |
|
6044 |
'*ute*' match:'12345COMPUTER' from:6 to:13 ignoreCase:true |
|
6045 |
" |
|
6046 |
||
6047 |
"Modified: / 10.11.1998 / 21:43:46 / cg" |
|
6048 |
! |
|
6049 |
||
6050 |
match:aString from:start to:stop ignoreCase:ignoreCase escapeCharacter:escape |
|
15734 | 6051 |
<resource: #obsolete> |
12111 | 6052 |
"return true if part of aString matches myself, |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
6053 |
where self may contain GLOB meta-match |
12111 | 6054 |
characters $* (to match any string) or $# (to match any character) |
6055 |
or [...] to match a set of characters. |
|
6056 |
If ignoreCase is true, lower/uppercase are considered the same. |
|
6057 |
||
6058 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20874 | 6059 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
6060 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 6061 |
NOTICE: the receiver is the match pattern" |
6062 |
||
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
6063 |
^ self |
20874 | 6064 |
match:aString from:start to:stop |
6065 |
caseSensitive:ignoreCase not |
|
6066 |
escapeCharacter:escape |
|
12111 | 6067 |
|
6068 |
" |
|
6069 |
'*ute*' match:'12345COMPUTER' from:1 to:5 ignoreCase:true |
|
6070 |
'*ute*' match:'12345COMPUTER' from:6 to:13 ignoreCase:true |
|
6071 |
" |
|
6072 |
||
6073 |
"Modified: / 10.11.1998 / 21:43:46 / cg" |
|
6074 |
! |
|
6075 |
||
6076 |
match:aString ignoreCase:ignoreCase |
|
15734 | 6077 |
<resource: #obsolete> |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
6078 |
"return true if aString matches self, where self may contain GLOB meta-match |
12111 | 6079 |
characters $* (to match any string) or $# (to match any character) |
6080 |
or [...] to match a set of characters. |
|
6081 |
If ignoreCase is true, lower/uppercase are considered the same. |
|
6082 |
The escape character is the backQuote. |
|
6083 |
||
6084 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20874 | 6085 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
6086 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 6087 |
NOTICE: the receiver is the match pattern" |
6088 |
||
15734 | 6089 |
"/ leave it this way, in case a customer has redefined it for performance (for a while) |
19033 | 6090 |
^ self match:aString from:1 to:aString size caseSensitive:ignoreCase not |
12111 | 6091 |
|
6092 |
" |
|
6093 |
'*ute*' match:'COMPUTER' ignoreCase:true |
|
6094 |
'*uter' match:'COMPUTER' ignoreCase:false |
|
6095 |
'[abcd]*' match:'computer' ignoreCase:false |
|
6096 |
'[abcd]*' match:'Computer' ignoreCase:false |
|
6097 |
'[a-k]*' match:'komputer' ignoreCase:false |
|
6098 |
'[a-k]*' match:'zomputer' ignoreCase:false |
|
6099 |
'[a-k]*' match:'Komputer' ignoreCase:false |
|
6100 |
'[a-k]*' match:'Komputer' ignoreCase:true |
|
6101 |
'*some*compl*ern*' match:'this is some more complicated pattern match' ignoreCase:true |
|
6102 |
'*some*compl*ern*' match:'this is another complicated pattern match' ignoreCase:true |
|
6103 |
||
6104 |
Time millisecondsToRun:[ |
|
20874 | 6105 |
Symbol allInstancesDo:[:sym | |
6106 |
'[ab]*' match:sym ignoreCase:false |
|
6107 |
] |
|
12111 | 6108 |
]. |
6109 |
Time millisecondsToRun:[ |
|
20874 | 6110 |
Symbol allInstancesDo:[:sym | |
6111 |
'*at:*' match:sym ignoreCase:false |
|
6112 |
] |
|
12111 | 6113 |
]. |
6114 |
" |
|
6115 |
||
6116 |
"Modified: 2.4.1997 / 17:28:58 / cg" |
|
6117 |
! |
|
6118 |
||
6119 |
match:aString ignoreCase:ignoreCase escapeCharacter:escape |
|
15734 | 6120 |
<resource: #obsolete> |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
6121 |
"return true if aString matches self, where self may contain GLOB meta-match |
12111 | 6122 |
characters $* (to match any string) or $# (to match any character) |
6123 |
or [...] to match a set of characters. |
|
6124 |
If ignoreCase is true, lower/uppercase are considered the same. |
|
6125 |
||
6126 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20874 | 6127 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
6128 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 6129 |
NOTICE: the receiver is the match pattern" |
6130 |
||
19033 | 6131 |
^ self match:aString from:1 to:aString size caseSensitive:ignoreCase not escapeCharacter:escape |
12111 | 6132 |
|
6133 |
" |
|
6134 |
'*ute*' match:'COMPUTER' ignoreCase:true |
|
6135 |
'*uter' match:'COMPUTER' ignoreCase:false |
|
6136 |
'[abcd]*' match:'computer' ignoreCase:false |
|
6137 |
'[abcd]*' match:'Computer' ignoreCase:false |
|
6138 |
'[a-k]*' match:'komputer' ignoreCase:false |
|
6139 |
'[a-k]*' match:'zomputer' ignoreCase:false |
|
6140 |
'[a-k]*' match:'Komputer' ignoreCase:false |
|
6141 |
'[a-k]*' match:'Komputer' ignoreCase:true |
|
6142 |
'*some*compl*ern*' match:'this is some more complicated pattern match' ignoreCase:true |
|
6143 |
'*some*compl*ern*' match:'this is another complicated pattern match' ignoreCase:true |
|
6144 |
||
6145 |
Time millisecondsToRun:[ |
|
20874 | 6146 |
Symbol allInstancesDo:[:sym | |
6147 |
'[ab]*' match:sym ignoreCase:false |
|
6148 |
] |
|
12111 | 6149 |
]. |
6150 |
Time millisecondsToRun:[ |
|
20874 | 6151 |
Symbol allInstancesDo:[:sym | |
6152 |
'*at:*' match:sym ignoreCase:false |
|
6153 |
] |
|
12111 | 6154 |
]. |
6155 |
" |
|
6156 |
||
6157 |
"Modified: 2.4.1997 / 17:28:58 / cg" |
|
6158 |
! |
|
6159 |
||
23287 | 6160 |
matches:aGlobPatternString |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
6161 |
"return true if the receiver matches aString, where aPatternString may contain GLOB meta-match |
12111 | 6162 |
characters $* (to match any string) or $# (to match any character). |
6163 |
or [...] to match a set of characters. |
|
6164 |
Lower/uppercase are considered different. |
|
6165 |
||
6166 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20874 | 6167 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
6168 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 6169 |
NOTICE: the receiver is the match pattern" |
6170 |
||
23287 | 6171 |
^ aGlobPatternString match:self |
6172 |
||
6173 |
"Modified (format): / 18-08-2018 / 20:51:45 / Claus Gittinger" |
|
14054
48d3fab77bd9
added: #matches:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
14053
diff
changeset
|
6174 |
! |
48d3fab77bd9
added: #matches:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
14053
diff
changeset
|
6175 |
|
15734 | 6176 |
matches:aPatternString caseSensitive:caseSensitive |
6177 |
"return true if the receiver matches aString, where aPatternString may contain GLOB meta-match |
|
6178 |
characters $* (to match any string) or $# (to match any character). |
|
6179 |
or [...] to match a set of characters. |
|
6180 |
||
6181 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20874 | 6182 |
NOT the ST-80 meaning. |
15734 | 6183 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
6184 |
NOTICE: the receiver is the match pattern" |
|
6185 |
||
6186 |
^ aPatternString match:self caseSensitive:caseSensitive |
|
6187 |
! |
|
6188 |
||
14054
48d3fab77bd9
added: #matches:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
14053
diff
changeset
|
6189 |
matches:aPatternString ignoreCase:ignoreCase |
15734 | 6190 |
<resource: #obsolete> |
14054
48d3fab77bd9
added: #matches:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
14053
diff
changeset
|
6191 |
"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
|
6192 |
characters $* (to match any string) or $# (to match any character). |
48d3fab77bd9
added: #matches:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
14053
diff
changeset
|
6193 |
or [...] to match a set of characters. |
48d3fab77bd9
added: #matches:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
14053
diff
changeset
|
6194 |
Lower/uppercase are considered different. |
48d3fab77bd9
added: #matches:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
14053
diff
changeset
|
6195 |
|
48d3fab77bd9
added: #matches:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
14053
diff
changeset
|
6196 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
20874 | 6197 |
NOT the ST-80 meaning. |
14054
48d3fab77bd9
added: #matches:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
14053
diff
changeset
|
6198 |
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
|
6199 |
NOTICE: the receiver is the match pattern" |
48d3fab77bd9
added: #matches:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
14053
diff
changeset
|
6200 |
|
15734 | 6201 |
^ aPatternString match:self caseSensitive:ignoreCase not |
14054
48d3fab77bd9
added: #matches:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
14053
diff
changeset
|
6202 |
|
48d3fab77bd9
added: #matches:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
14053
diff
changeset
|
6203 |
"Created: / 08-03-2012 / 03:11:11 / cg" |
12111 | 6204 |
! ! |
6205 |
||
6206 |
||
24250 | 6207 |
|
9192 | 6208 |
!CharacterArray methodsFor:'padded copying'! |
6209 |
||
6210 |
centerPaddedTo:newSize |
|
19464 | 6211 |
"return a new string consisting of the receiver's characters, |
6212 |
plus spaces up to length and center the receiver's characters in |
|
6213 |
the resulting string. |
|
6214 |
If the receiver's size is equal or greater than the length argument, |
|
6215 |
the original receiver is returned unchanged." |
|
9192 | 6216 |
|
6217 |
^ self centerPaddedTo:newSize with:(Character space) |
|
6218 |
||
6219 |
" |
|
6220 |
'foo' centerPaddedTo:10 |
|
6221 |
123 printString centerPaddedTo:10 |
|
6222 |
" |
|
6223 |
||
6224 |
"Created: 25.11.1995 / 10:53:57 / cg" |
|
6225 |
! |
|
9190 | 6226 |
|
6227 |
centerPaddedTo:size with:padCharacter |
|
6228 |
"return a new string of length size, which contains the receiver |
|
6229 |
centered (i.e. padded on both sides). |
|
6230 |
Characters are filled with padCharacter. |
|
19464 | 6231 |
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
|
6232 |
the original receiver is returned unchanged." |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
6233 |
|
9190 | 6234 |
|len s| |
6235 |
||
6236 |
len := self size. |
|
9192 | 6237 |
(len < size) ifTrue:[ |
20874 | 6238 |
s := self species new:size withAll:padCharacter. |
6239 |
s replaceFrom:(size - len) // 2 + 1 with:self. |
|
6240 |
^ s |
|
9192 | 6241 |
] |
6242 |
||
9190 | 6243 |
" |
6244 |
'foo' centerPaddedTo:11 with:$. |
|
6245 |
'fooBar' centerPaddedTo:5 with:$. |
|
6246 |
123 printString centerPaddedTo:10 with:$. |
|
6247 |
(' ' , 123 printString) centerPaddedTo:10 with:$. |
|
6248 |
(Float pi printString) centerPaddedTo:15 with:(Character space) |
|
6249 |
(Float pi printString) centerPaddedTo:15 with:$- |
|
6250 |
(' ' , Float pi class name) centerPaddedTo:15 with:$. |
|
6251 |
" |
|
815
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
6252 |
! |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
6253 |
|
9190 | 6254 |
decimalPaddedTo:size and:afterPeriod at:decimalCharacter |
815
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
6255 |
"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
|
6256 |
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
|
6257 |
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
|
6258 |
to be padded). |
19464 | 6259 |
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
|
6260 |
the original receiver is returned unchanged. |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
6261 |
(sounds complicated ? -> see examples below)." |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
6262 |
|
8874 | 6263 |
^ self |
20874 | 6264 |
decimalPaddedTo:size |
6265 |
and:afterPeriod |
|
6266 |
at:decimalCharacter |
|
6267 |
withLeft:(Character space) |
|
6268 |
right:$0 |
|
815
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
6269 |
|
8874 | 6270 |
" |
6271 |
'123' decimalPaddedTo:10 and:3 at:$. -> ' 123 ' |
|
6272 |
'123' decimalPaddedTo:10 and:3 at:$. -> ' 123.000' |
|
6273 |
'123.' decimalPaddedTo:10 and:3 at:$. -> ' 123.000' |
|
815
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
6274 |
'123.1' decimalPaddedTo:10 and:3 at:$. -> ' 123.100' |
8874 | 6275 |
'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
|
6276 |
'123.123' decimalPaddedTo:10 and:3 at:$. -> ' 123.123' |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
6277 |
" |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
6278 |
|
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
6279 |
"Created: 23.12.1995 / 13:11:52 / cg" |
9192 | 6280 |
! |
6281 |
||
6282 |
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
|
6283 |
"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
|
6284 |
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
|
6285 |
of the period. |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
6286 |
Characters on the left are filled with leftPadChar. |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
6287 |
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
|
6288 |
otherwise, if missing, a decimal point is added and right characters filled with this. |
19464 | 6289 |
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
|
6290 |
the original receiver is returned unchanged. |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
6291 |
(sounds complicated ? -> see examples below)." |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
6292 |
|
5230
655c21ffe6cc
care for negative size of rest-string when decimalPadding
Claus Gittinger <cg@exept.de>
parents:
5221
diff
changeset
|
6293 |
|s idx n rest| |
815
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
6294 |
|
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
6295 |
idx := self indexOf:decimalCharacter. |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
6296 |
idx == 0 ifTrue:[ |
20874 | 6297 |
"/ |
6298 |
"/ no decimal point found; adjust string to the left of the period column |
|
6299 |
"/ |
|
6300 |
rightPadChar isNil ifTrue:[ |
|
6301 |
s := self , (self species new:afterPeriod + 1 withAll:leftPadChar) |
|
6302 |
] ifFalse:[ |
|
6303 |
s:= self , decimalCharacter asString , (self species new:afterPeriod withAll:rightPadChar). |
|
6304 |
]. |
|
815
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
6305 |
] ifFalse:[ |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
6306 |
|
20874 | 6307 |
"/ the number of after-decimalPoint characters |
6308 |
n := self size - idx. |
|
6309 |
rest := afterPeriod - n. |
|
6310 |
rest > 0 ifTrue:[ |
|
6311 |
s := (self species new:rest withAll:(rightPadChar ? leftPadChar)). |
|
6312 |
] ifFalse:[ |
|
6313 |
s := '' |
|
6314 |
]. |
|
6315 |
s := self , s. |
|
815
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
6316 |
]. |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
6317 |
|
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
6318 |
^ s leftPaddedTo:size with:leftPadChar |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
6319 |
|
8874 | 6320 |
" |
6321 |
'123' decimalPaddedTo:10 and:3 at:$. withLeft:(Character space) right:nil -> ' 123 ' |
|
6322 |
'123' decimalPaddedTo:10 and:3 at:$. withLeft:(Character space) right:$0 -> ' 123.000' |
|
6323 |
'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
|
6324 |
'123.1' decimalPaddedTo:10 and:3 at:$. withLeft:(Character space) right:$0 -> ' 123.100' |
8874 | 6325 |
'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
|
6326 |
'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
|
6327 |
" |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
6328 |
|
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
6329 |
"Modified: 23.12.1995 / 13:08:18 / cg" |
9192 | 6330 |
! |
6331 |
||
6332 |
leftPaddedTo:size |
|
6333 |
"return a new string of length size, which contains the receiver |
|
6334 |
right-adjusted (i.e. padded on the left). |
|
6335 |
Characters on the left are filled with spaces. |
|
19464 | 6336 |
If the receiver's size is equal or greater than the length argument, |
9192 | 6337 |
the original receiver is returned unchanged." |
6338 |
||
6339 |
^ self leftPaddedTo:size with:(Character space) |
|
6340 |
||
6341 |
" |
|
6342 |
'foo' leftPaddedTo:10 |
|
6343 |
'fooBar' leftPaddedTo:5 |
|
6344 |
123 printString leftPaddedTo:10 |
|
6345 |
" |
|
6346 |
! |
|
608 | 6347 |
|
6348 |
paddedTo:newSize |
|
19753 | 6349 |
"return a new string consisting of the receiver's characters, |
23945 | 6350 |
plus spaces up to length. |
6351 |
If the receiver's size is equal or greater than the length argument, |
|
6352 |
the original receiver is returned unchanged." |
|
608 | 6353 |
|
6354 |
^ self paddedTo:newSize with:(Character space) |
|
6355 |
||
6356 |
" |
|
8874 | 6357 |
'foo' paddedTo:10 |
6358 |
123 printString paddedTo:10 |
|
15734 | 6359 |
'12345678901234' paddedTo:10 |
608 | 6360 |
" |
23945 | 6361 |
|
6362 |
"Modified (format): / 21-03-2019 / 12:56:24 / Claus Gittinger" |
|
64 | 6363 |
! ! |
6364 |
||
9192 | 6365 |
!CharacterArray methodsFor:'printing & storing'! |
608 | 6366 |
|
6367 |
article |
|
17137 | 6368 |
"return an article string for the receiver. |
6369 |
The original code only looked at the first character being a vowel; |
|
6370 |
this has been enhanced by some heuristics - not perfect, still." |
|
608 | 6371 |
|
17241 | 6372 |
|firstChar secondChar thirdChar| |
608 | 6373 |
|
8874 | 6374 |
firstChar := (self at:1) asLowercase. |
24041 | 6375 |
((firstChar isVowel and:[firstChar ~~ $u]) |
6376 |
or:[firstChar == $x] |
|
6377 |
) ifTrue:[ |
|
20874 | 6378 |
^ 'an' |
608 | 6379 |
]. |
17043 | 6380 |
|
6381 |
(self size >= 3) ifTrue:[ |
|
20874 | 6382 |
secondChar := (self at:2) asLowercase. |
6383 |
||
6384 |
"/ may need more here... |
|
24041 | 6385 |
(firstChar == $r |
6386 |
and:[secondChar == $b] |
|
6387 |
) ifTrue:[ |
|
20874 | 6388 |
^ 'an' |
6389 |
]. |
|
6390 |
||
6391 |
thirdChar := (self at:3) asLowercase. |
|
6392 |
||
24041 | 6393 |
(firstChar == $s |
6394 |
and:[secondChar == $c |
|
6395 |
and:[thirdChar == $h]] |
|
6396 |
) ifTrue:[ |
|
6397 |
^ 'a' |
|
6398 |
]. |
|
6399 |
||
20874 | 6400 |
(firstChar isVowel not |
6401 |
and:[(secondChar isVowel or:[secondChar == $y]) not |
|
6402 |
and:[thirdChar isVowel not ]]) ifTrue:[ |
|
6403 |
"/ exceptions: 3 non-vowels in a row: looks like an abbreviation |
|
6404 |
(self size > 4) ifTrue:[ |
|
6405 |
(firstChar == $s) ifTrue:[ |
|
6406 |
((secondChar == $c and:[thirdChar == $r]) |
|
6407 |
or:[ (secondChar == $t and:[thirdChar == $r]) ]) ifTrue:[ |
|
6408 |
(self at:4) isVowel ifTrue:[ |
|
6409 |
^ 'a' |
|
6410 |
] |
|
6411 |
] |
|
6412 |
]. |
|
6413 |
]. |
|
6414 |
"/ an abbreviation; treat x, s as vowels |
|
6415 |
(firstChar == $x or:[ firstChar == $s ]) ifTrue:[^ 'an']. |
|
6416 |
] |
|
17043 | 6417 |
]. |
608 | 6418 |
^ 'a' |
5848 | 6419 |
|
6420 |
" |
|
24041 | 6421 |
'uboot' article. |
6422 |
'xmas' article. |
|
6423 |
'alarm' article. |
|
6424 |
'baby' article. |
|
6425 |
'sql' article. |
|
6426 |
'scr' article. |
|
6427 |
'screen' article. |
|
6428 |
'scrollbar' article. |
|
6429 |
'scrs' article. |
|
6430 |
'cvs' article. |
|
6431 |
'cvssource' article. |
|
6432 |
'symbol' article. |
|
6433 |
'string' article. |
|
6434 |
'rbparser' article. |
|
6435 |
'scheme' article. |
|
19683 | 6436 |
" |
6437 |
||
6438 |
"Modified (comment): / 01-05-2016 / 10:57:25 / cg" |
|
24041 | 6439 |
"Modified (comment): / 03-04-2019 / 10:08:57 / Claus Gittinger" |
608 | 6440 |
! |
6441 |
||
8049
ccd0e8f26439
printing - migrating towards unicodeSupport in ST-code
Claus Gittinger <cg@exept.de>
parents:
8040
diff
changeset
|
6442 |
basicStoreString |
ccd0e8f26439
printing - migrating towards unicodeSupport in ST-code
Claus Gittinger <cg@exept.de>
parents:
8040
diff
changeset
|
6443 |
"return a String for storing myself" |
ccd0e8f26439
printing - migrating towards unicodeSupport in ST-code
Claus Gittinger <cg@exept.de>
parents:
8040
diff
changeset
|
6444 |
|
ccd0e8f26439
printing - migrating towards unicodeSupport in ST-code
Claus Gittinger <cg@exept.de>
parents:
8040
diff
changeset
|
6445 |
|s n index| |
ccd0e8f26439
printing - migrating towards unicodeSupport in ST-code
Claus Gittinger <cg@exept.de>
parents:
8040
diff
changeset
|
6446 |
|
ccd0e8f26439
printing - migrating towards unicodeSupport in ST-code
Claus Gittinger <cg@exept.de>
parents:
8040
diff
changeset
|
6447 |
n := self occurrencesOf:$'. |
11320 | 6448 |
n ~~ 0 ifTrue:[ |
20874 | 6449 |
s := self species new:(n + 2 + self size). |
6450 |
s at:1 put:$'. |
|
6451 |
index := 2. |
|
6452 |
self do:[:thisChar | |
|
6453 |
(thisChar == $') ifTrue:[ |
|
6454 |
s at:index put:thisChar. |
|
6455 |
index := index + 1. |
|
6456 |
]. |
|
6457 |
s at:index put:thisChar. |
|
6458 |
index := index + 1. |
|
6459 |
]. |
|
6460 |
s at:index put:$'. |
|
6461 |
^ s |
|
9192 | 6462 |
]. |
11320 | 6463 |
|
9192 | 6464 |
^ '''' , self , '''' |
15523 | 6465 |
|
6466 |
" |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
6467 |
'''immutable'' string' asImmutableString basicStoreString |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
6468 |
'immutable string' asImmutableString basicStoreString |
15523 | 6469 |
" |
6470 |
||
6471 |
"Modified: / 14-07-2013 / 19:20:49 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
9192 | 6472 |
! |
8049
ccd0e8f26439
printing - migrating towards unicodeSupport in ST-code
Claus Gittinger <cg@exept.de>
parents:
8040
diff
changeset
|
6473 |
|
11774
6f6e2b71b607
displayOn (inspector's interface) was broken
Claus Gittinger <cg@exept.de>
parents:
11754
diff
changeset
|
6474 |
displayOn:aGCOrStream |
6f6e2b71b607
displayOn (inspector's interface) was broken
Claus Gittinger <cg@exept.de>
parents:
11754
diff
changeset
|
6475 |
"Compatibility |
6f6e2b71b607
displayOn (inspector's interface) was broken
Claus Gittinger <cg@exept.de>
parents:
11754
diff
changeset
|
6476 |
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
|
6477 |
OR: |
6f6e2b71b607
displayOn (inspector's interface) was broken
Claus Gittinger <cg@exept.de>
parents:
11754
diff
changeset
|
6478 |
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
|
6479 |
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
|
6480 |
(although the fallBack is to display its printString ...)" |
6f6e2b71b607
displayOn (inspector's interface) was broken
Claus Gittinger <cg@exept.de>
parents:
11754
diff
changeset
|
6481 |
|
6f6e2b71b607
displayOn (inspector's interface) was broken
Claus Gittinger <cg@exept.de>
parents:
11754
diff
changeset
|
6482 |
"/ what a kludge - Dolphin and Squeak mean: printOn: a stream; |
21540 | 6483 |
"/ old ST80 means: draw-yourself on a GC. |
6484 |
aGCOrStream isStream ifFalse:[ |
|
6485 |
^ super displayOn:aGCOrStream. |
|
6486 |
]. |
|
6487 |
||
6488 |
self storeOn:aGCOrStream. |
|
6489 |
||
6490 |
"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
|
6491 |
! |
6f6e2b71b607
displayOn (inspector's interface) was broken
Claus Gittinger <cg@exept.de>
parents:
11754
diff
changeset
|
6492 |
|
608 | 6493 |
printOn:aStream |
6494 |
"print the receiver on aStream" |
|
6495 |
||
6496 |
aStream nextPutAll:self |
|
6497 |
! |
|
6498 |
||
6499 |
printString |
|
13079
111e7e456648
Implement display with #displayOn: instead of #displayString
Stefan Vogel <sv@exept.de>
parents:
13073
diff
changeset
|
6500 |
"return a string for printing - that's myself" |
608 | 6501 |
|
6502 |
^ self |
|
3578 | 6503 |
! |
6504 |
||
6505 |
printWithQuotesDoubledOn:aStream |
|
6506 |
"put the raw storeString of myself on aStream" |
|
6507 |
||
6508 |
self do:[:thisChar | |
|
20874 | 6509 |
(thisChar == $') ifTrue:[aStream nextPut:thisChar]. |
6510 |
aStream nextPut:thisChar |
|
3578 | 6511 |
] |
6512 |
||
6513 |
"Modified: / 15.6.1998 / 17:21:17 / cg" |
|
6514 |
"Created: / 15.6.1998 / 17:22:13 / cg" |
|
12171 | 6515 |
! |
6516 |
||
15788 | 6517 |
printXmlQuotedOn:aStream |
23549 | 6518 |
"convert aString to a valid XML string |
6519 |
that can be used for XML attributes. |
|
6520 |
Can also be used for XML text, but better use #printXmlTextQuotedOn:. |
|
23551 | 6521 |
Various senders expect, that the output is ISO-8859-1 compatible |
6522 |
(they don't care for encoding), so for now, we always generate 8-bit chars. |
|
23549 | 6523 |
TODO: care for 16bit UNICODE string and escape chars ..." |
6524 |
||
23548 | 6525 |
<resource: #todo> |
15788 | 6526 |
|
6527 |
self do:[:eachChar | |
|
20874 | 6528 |
eachChar == $< ifTrue:[ |
6529 |
aStream nextPutAll:'<' "mapping needed for xml text" |
|
6530 |
] ifFalse:[ eachChar == $& ifTrue:[ |
|
6531 |
aStream nextPutAll:'&' "mapping needed for all" |
|
6532 |
] ifFalse:[ eachChar == $> ifTrue:[ |
|
6533 |
aStream nextPutAll:'>' "mapping needed for comments" |
|
6534 |
] ifFalse:[ eachChar == $' ifTrue:[ |
|
6535 |
aStream nextPutAll:''' "mapping needed for attributes" |
|
6536 |
] ifFalse:[ eachChar == $" ifTrue:[ |
|
6537 |
aStream nextPutAll:'"' "mapping needed for attributes" |
|
6538 |
] ifFalse:[ |
|
6539 |
|codePoint| |
|
6540 |
codePoint := eachChar codePoint. |
|
23552 | 6541 |
(codePoint < 16r20 |
6542 |
or:[codePoint >= 16r7F |
|
6543 |
and:[codePoint <= 16r9F |
|
6544 |
or:[codePoint > 16rFF] "for now, always make sure, that output is ISO-8859-1 compatible"] |
|
6545 |
]) ifTrue:[ |
|
20874 | 6546 |
aStream nextPutAll:'&#'. |
23549 | 6547 |
codePoint printOn:aStream base:16. |
20874 | 6548 |
aStream nextPut:$;. |
23549 | 6549 |
] ifFalse:[ |
6550 |
aStream nextPut:eachChar |
|
6551 |
]. |
|
6552 |
]]]]] |
|
6553 |
]. |
|
6554 |
||
23552 | 6555 |
"Modified: / 03-12-2018 / 15:21:27 / Stefan Vogel" |
15788 | 6556 |
! |
6557 |
||
6558 |
printXmlTextQuotedOn:aStream |
|
23290 | 6559 |
<resource: #todo> |
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
6560 |
"convert aString to a valid XML string |
15788 | 6561 |
that can be used for XML text. |
6562 |
Here line formatting characters are not escaped. |
|
23549 | 6563 |
For XML text (as opposed to XML attribute), $' and $"" need |
6564 |
not to be transliterated (see https://www.w3.org/TR/xml Section 2.4). |
|
23551 | 6565 |
Various senders expect, that the output is ISO-8859-1 compatible |
6566 |
(they don't care for encoding), so for now, we always generate 8-bit chars. |
|
23549 | 6567 |
TODO: care for 16bit UNICODE string and escape chars |
6568 |
- but we need to know the XML file encoding" |
|
15788 | 6569 |
|
6570 |
self do:[:eachChar | |
|
20874 | 6571 |
eachChar == $< ifTrue:[ |
6572 |
aStream nextPutAll:'<' "mapping needed for xml text" |
|
6573 |
] ifFalse:[ eachChar == $& ifTrue:[ |
|
6574 |
aStream nextPutAll:'&' "mapping needed for all" |
|
23290 | 6575 |
] ifFalse:[ eachChar == $> ifTrue:[ |
23549 | 6576 |
aStream nextPutAll:'>' "mapping needed only for ]]>" |
20874 | 6577 |
] ifFalse:[ |
6578 |
|codePoint| |
|
6579 |
codePoint := eachChar codePoint. |
|
6580 |
((codePoint < 16r20 and:[codePoint ~~ 9 and:[codePoint ~~ 10 and:[codePoint ~~ 13]]]) |
|
23552 | 6581 |
or:[codePoint >= 16r7F |
6582 |
and:[codePoint <= 16r9F |
|
6583 |
or:[codePoint > 16rFF] "for now, always make sure, that output is ISO-8859-1 compatible"] |
|
6584 |
]) ifTrue:[ |
|
23321 | 6585 |
aStream nextPutAll:'&#x'. |
23290 | 6586 |
codePoint printOn:aStream base:16. |
20874 | 6587 |
aStream nextPut:$;. |
23549 | 6588 |
] ifFalse:[ |
6589 |
aStream nextPut:eachChar |
|
6590 |
]. |
|
6591 |
]]] |
|
23290 | 6592 |
]. |
6593 |
||
6594 |
" |
|
6595 |
String streamContents:[:s|'< & >' printXmlTextQuotedOn:s] |
|
6596 |
String streamContents:[:s|'< & >',(Character value:7) printXmlTextQuotedOn:s] |
|
6597 |
String streamContents:[:s|'< & >',(Character value:129) printXmlTextQuotedOn:s] |
|
6598 |
String streamContents:[:s|'< & >',(Character value:1000) printXmlTextQuotedOn:s] |
|
6599 |
" |
|
6600 |
||
23321 | 6601 |
"Modified: / 30-08-2018 / 22:39:00 / Claus Gittinger" |
23552 | 6602 |
"Modified: / 03-12-2018 / 15:21:18 / Stefan Vogel" |
15788 | 6603 |
! |
6604 |
||
6605 |
xmlQuotedPrintString |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
6606 |
"convert aString to a valid XML string |
23241 | 6607 |
that can be used for attributes, text, comments an PIs" |
15788 | 6608 |
|
6609 |
^ String streamContents:[:s| |
|
20874 | 6610 |
self printXmlQuotedOn:s |
15788 | 6611 |
]. |
23241 | 6612 |
|
6613 |
"Modified (comment): / 28-07-2018 / 11:11:51 / Claus Gittinger" |
|
9192 | 6614 |
! ! |
6615 |
||
11955 | 6616 |
|
9192 | 6617 |
!CharacterArray methodsFor:'queries'! |
608 | 6618 |
|
23701 | 6619 |
argumentCount |
6620 |
"treating the receiver as a message selector, return how many arguments would it take (ANSI)" |
|
6621 |
||
6622 |
|binopChars| |
|
6623 |
||
6624 |
(self size <= Method maxBinarySelectorSize) ifTrue:[ |
|
6625 |
binopChars := Method binarySelectorCharacters. |
|
6626 |
(self conform:[:eachChar | (binopChars includes:eachChar)]) ifTrue:[ |
|
6627 |
^ 1 |
|
6628 |
]. |
|
6629 |
]. |
|
6630 |
^ self occurrencesOf:$: |
|
6631 |
||
6632 |
" |
|
6633 |
'foo:bar:' argumentCount |
|
6634 |
#foo:bar: argumentCount |
|
6635 |
'hello' argumentCount |
|
6636 |
'+' argumentCount |
|
6637 |
'++' argumentCount |
|
6638 |
'+++' argumentCount |
|
6639 |
'|' argumentCount |
|
6640 |
'?' argumentCount |
|
6641 |
'_' argumentCount |
|
6642 |
'_:' argumentCount |
|
6643 |
'_:_:' argumentCount |
|
6644 |
'<->' argumentCount |
|
6645 |
'<' argumentCount |
|
6646 |
#'<' argumentCount |
|
6647 |
" |
|
6648 |
||
6649 |
"Modified (comment): / 06-02-2017 / 13:48:57 / cg" |
|
6650 |
! |
|
6651 |
||
4429 | 6652 |
bitsPerCharacter |
18662 | 6653 |
"return the underlying string's bitsPerCharacter |
4429 | 6654 |
(i.e. is it a regular String or a TwoByteString)" |
6655 |
||
8457 | 6656 |
|string max| |
6657 |
||
6658 |
(string := self string) ~~ self ifTrue:[ |
|
20874 | 6659 |
^ string bitsPerCharacter |
8457 | 6660 |
]. |
6661 |
||
6662 |
max := 8. |
|
6663 |
self do:[:eachCharacter | |
|
20874 | 6664 |
max := max max:(eachCharacter bitsPerCharacter) |
9192 | 6665 |
]. |
6666 |
^ max |
|
6667 |
||
6668 |
" |
|
6669 |
'hello' bitsPerCharacter |
|
6670 |
'hello' asText allBold bitsPerCharacter |
|
6671 |
" |
|
6672 |
! |
|
8457 | 6673 |
|
19757 | 6674 |
bytesPerCharacter |
23971 | 6675 |
"return the underlying string's bytesPerCharacter |
19757 | 6676 |
(i.e. is it a regular String or a TwoByteString)" |
6677 |
||
6678 |
|string max| |
|
6679 |
||
6680 |
(string := self string) ~~ self ifTrue:[ |
|
20874 | 6681 |
^ string bytesPerCharacter |
19757 | 6682 |
]. |
6683 |
||
6684 |
max := 1. |
|
6685 |
self do:[:eachCharacter | |
|
20874 | 6686 |
max := max max:(eachCharacter bytesPerCharacter) |
19757 | 6687 |
]. |
6688 |
^ max |
|
6689 |
||
6690 |
" |
|
6691 |
'hello' bytesPerCharacter |
|
6692 |
'hello' asUnicode16String bytesPerCharacter |
|
6693 |
'hello' asText allBold bytesPerCharacter |
|
6694 |
" |
|
23971 | 6695 |
|
6696 |
"Modified (comment): / 25-03-2019 / 16:24:02 / Claus Gittinger" |
|
6697 |
! |
|
6698 |
||
6699 |
bytesPerCharacterNeeded |
|
6700 |
"return the actual underlying string's required bytesPerCharacter |
|
6701 |
(i.e. checks if all characters really need that depth)" |
|
6702 |
||
6703 |
|string max mySize| |
|
6704 |
||
6705 |
(string := self string) ~~ self ifTrue:[ |
|
6706 |
^ string bytesPerCharacterNeeded |
|
6707 |
]. |
|
6708 |
||
6709 |
mySize := self bytesPerCharacter. |
|
6710 |
||
6711 |
max := 1. |
|
6712 |
self do:[:eachCharacter | |
|
6713 |
max := max max:(eachCharacter bytesPerCharacter). |
|
6714 |
max == mySize ifTrue:[^ max]. |
|
6715 |
]. |
|
6716 |
^ max |
|
6717 |
||
6718 |
" |
|
6719 |
'hello' bytesPerCharacter -> 1 |
|
6720 |
'hello' bytesPerCharacterNeeded -> 1 |
|
6721 |
||
6722 |
'hello' asUnicode16String bytesPerCharacter -> 2 |
|
6723 |
'hello' asUnicode16String bytesPerCharacterNeeded -> 1 |
|
6724 |
" |
|
6725 |
||
6726 |
"Created: / 25-03-2019 / 16:22:00 / Claus Gittinger" |
|
19757 | 6727 |
! |
6728 |
||
23701 | 6729 |
characterSize |
6730 |
"answer the size in bits of my largest character (actually only 7, 8, 16 or 32)" |
|
6731 |
||
6732 |
|string max |
|
6733 |
sz "{ Class:SmallInteger}" | |
|
6734 |
||
6735 |
(string := self string) ~~ self ifTrue:[ |
|
6736 |
^ string characterSize. |
|
6737 |
]. |
|
6738 |
||
6739 |
sz := self size. |
|
6740 |
max := 7. |
|
6741 |
1 to:sz do:[:idx | |
|
6742 |
|thisSize| |
|
6743 |
||
6744 |
thisSize := (self at:idx) characterSize. |
|
6745 |
thisSize > max ifTrue:[ |
|
6746 |
max := thisSize. |
|
6747 |
max == 32 ifTrue:[ |
|
6748 |
"shortcut: we know, that max size is 32" |
|
6749 |
^ 32. |
|
6750 |
]. |
|
6751 |
]. |
|
6752 |
]. |
|
6753 |
||
6754 |
^ max. |
|
6755 |
||
6756 |
" |
|
6757 |
'hello' characterSize |
|
6758 |
'hello' asUnicode32String characterSize |
|
6759 |
'helloü' asUnicode32String characterSize |
|
6760 |
'hello' asText allBold characterSize |
|
6761 |
" |
|
6762 |
! |
|
6763 |
||
6764 |
contains8BitCharacters |
|
6765 |
<resource: #obsolete> |
|
6766 |
||
6767 |
^ self containsNon7BitAscii |
|
6768 |
! |
|
6769 |
||
6770 |
containsNon7BitAscii |
|
6771 |
"return true, if the underlying string contains 8BitCharacters (or widers) |
|
6772 |
(i.e. if it is non-ascii)" |
|
6773 |
||
6774 |
|string |
|
6775 |
sz "{ Class:SmallInteger }"| |
|
6776 |
||
6777 |
(string := self string) ~~ self ifTrue:[ |
|
6778 |
^ string containsNon7BitAscii |
|
6779 |
]. |
|
6780 |
sz := self size. |
|
6781 |
1 to:sz do:[:idx| |
|
6782 |
(self at:idx) codePoint > 16r7F ifTrue:[ |
|
6783 |
^ true. |
|
6784 |
]. |
|
6785 |
]. |
|
6786 |
^ false. |
|
6787 |
||
6788 |
" |
|
6789 |
'hello' asUnicode32String containsNon7BitAscii |
|
6790 |
'hello üöä' asUnicode32String containsNon7BitAscii |
|
6791 |
'hello' asUnicode32String asText allBold containsNon7BitAscii |
|
6792 |
'hello üö' asUnicode32String asText allBold containsNon7BitAscii |
|
6793 |
" |
|
6794 |
! |
|
6795 |
||
18967 | 6796 |
containsNon8BitElements |
6797 |
"return true, if the underlying string contains elements larger than a single byte" |
|
6798 |
||
21322 | 6799 |
|string sz "{ Class:SmallInteger }"| |
6800 |
||
6801 |
(string := self string) ~~ self ifTrue:[ |
|
6802 |
^ string containsNon8BitElements |
|
6803 |
]. |
|
18967 | 6804 |
sz := self size. |
6805 |
1 to:sz do:[:idx| |
|
20874 | 6806 |
(self at:idx) codePoint > 16rFF ifTrue:[ |
6807 |
^ true. |
|
6808 |
]. |
|
18967 | 6809 |
]. |
6810 |
^ false. |
|
6811 |
! |
|
6812 |
||
10903 | 6813 |
continuesWith:aString startingAt:startIndex |
6814 |
"return true, if the receiver beginning at startIndex |
|
22783 | 6815 |
contains the characters in aString. |
6816 |
Returns false if the search reaches the end of the receiver |
|
6817 |
before all characters have been compared." |
|
10903 | 6818 |
|
6819 |
|sz "{Class: SmallInteger }" |
|
6820 |
idx "{Class: SmallInteger }"| |
|
6821 |
||
6822 |
sz := aString size. |
|
22783 | 6823 |
(startIndex + sz - 1) > self size ifTrue:[^ false]. |
6824 |
||
10903 | 6825 |
idx := startIndex. |
22783 | 6826 |
|
10903 | 6827 |
1 to:sz do:[:i | |
20874 | 6828 |
(self at:idx) ~~ (aString at:i) ifTrue:[^ false]. |
6829 |
idx := idx + 1 |
|
10903 | 6830 |
]. |
6831 |
^ true |
|
6832 |
||
6833 |
" |
|
6834 |
'hello world' continuesWith:'world' startingAt:6 |
|
6835 |
'hello world' continuesWith:'world' startingAt:7 |
|
22783 | 6836 |
'hello' continuesWith:'llo' startingAt:3 |
6837 |
'hello' continuesWith:'llow' startingAt:3 |
|
10903 | 6838 |
" |
6839 |
||
6840 |
"Created: 12.5.1996 / 15:46:40 / cg" |
|
6841 |
"Modified: 26.7.1996 / 19:08:36 / cg" |
|
6842 |
! |
|
6843 |
||
6844 |
countWords |
|
6845 |
"return the number of words, which are separated by separators" |
|
6846 |
||
6847 |
|tally "{ Class: SmallInteger }" |
|
6848 |
start "{ Class: SmallInteger }" |
|
6849 |
mySize "{ Class: SmallInteger }" |
|
6850 |
stop ch| |
|
6851 |
||
6852 |
tally := 0. |
|
6853 |
start := 1. |
|
6854 |
mySize := self size. |
|
6855 |
[start <= mySize] whileTrue:[ |
|
20874 | 6856 |
ch := self at:start. |
6857 |
ch isSeparator ifTrue:[ |
|
6858 |
start := start + 1 |
|
6859 |
] ifFalse:[ |
|
6860 |
stop := self indexOfSeparatorStartingAt:start. |
|
6861 |
(stop == 0) ifTrue:[ |
|
6862 |
stop := mySize + 1 |
|
6863 |
]. |
|
6864 |
tally := tally + 1. |
|
6865 |
start := stop |
|
6866 |
] |
|
10903 | 6867 |
]. |
6868 |
^ tally |
|
6869 |
||
6870 |
" |
|
6871 |
'hello world isnt this nice' countWords' |
|
6872 |
" |
|
6873 |
! |
|
6874 |
||
11788 | 6875 |
defaultElement |
6876 |
^ Character space |
|
6877 |
! |
|
6878 |
||
608 | 6879 |
encoding |
1311 | 6880 |
"return the strings encoding, as a symbol. |
7982 | 6881 |
Here, by default, we assume unicode-encoding. |
20006 | 6882 |
Notice, that ISO-8859-1 is a true subset of unicode, |
8874 | 6883 |
and that singleByteStrings are therefore both unicode AND |
7982 | 6884 |
8859-1 encoded." |
6885 |
||
23290 | 6886 |
^ #'iso10646-1'. |
6887 |
"/ ^ #unicode |
|
6888 |
||
6889 |
"Modified: / 22-08-2018 / 09:30:16 / Claus Gittinger" |
|
608 | 6890 |
! |
6891 |
||
1375
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
6892 |
hasChangeOfEmphasis |
14405
1d0d99a95bba
comment/format in:7 methods
Claus Gittinger <cg@exept.de>
parents:
14168
diff
changeset
|
6893 |
"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
|
6894 |
i.e. any non-normal (=emphasized) characters" |
1d0d99a95bba
comment/format in:7 methods
Claus Gittinger <cg@exept.de>
parents:
14168
diff
changeset
|
6895 |
|
1375
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
6896 |
^ false |
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
6897 |
|
1381 | 6898 |
"Created: 12.5.1996 / 12:31:39 / cg" |
1375
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
6899 |
! |
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
6900 |
|
7995
8ddbb403d67b
+hasIcon and hasImage for LabelAndIcon compatibility
Claus Gittinger <cg@exept.de>
parents:
7991
diff
changeset
|
6901 |
hasIcon |
9191 | 6902 |
"for LabelAndIcon compatibility" |
6903 |
||
9192 | 6904 |
^ false |
6905 |
! |
|
6906 |
||
6907 |
hasImage |
|
6908 |
"for LabelAndIcon compatibility" |
|
6909 |
||
6910 |
^ false |
|
6911 |
! |
|
7995
8ddbb403d67b
+hasIcon and hasImage for LabelAndIcon compatibility
Claus Gittinger <cg@exept.de>
parents:
7991
diff
changeset
|
6912 |
|
1388 | 6913 |
heightOn:aGC |
19464 | 6914 |
"return the size of the receiver in device units if displayed on aGC" |
1388 | 6915 |
|
20221 | 6916 |
^ aGC deviceFont heightOf:self "font is already a device font" |
1388 | 6917 |
|
6918 |
" |
|
8874 | 6919 |
'hello world' heightOn:(View new) |
1388 | 6920 |
" |
6921 |
||
6922 |
"Created: 12.5.1996 / 20:09:29 / cg" |
|
6923 |
"Modified: 12.5.1996 / 20:32:05 / cg" |
|
6924 |
! |
|
6925 |
||
23701 | 6926 |
isAlphaNumeric |
6927 |
"return true, if the receiver is some alphanumeric word; |
|
6928 |
i.e. consists of a letter followed by letters or digits." |
|
6929 |
||
6930 |
self isEmpty ifTrue:[ |
|
6931 |
"mhmh what is this ?" |
|
6932 |
^ false |
|
6933 |
]. |
|
6934 |
(self at:1) isLetter ifFalse:[^ false]. |
|
6935 |
^ self conform:[:char | char isLetterOrDigit]. |
|
6936 |
||
6937 |
" |
|
6938 |
'helloWorld' isAlphaNumeric |
|
6939 |
'foo1234' isAlphaNumeric |
|
6940 |
'f1234' isAlphaNumeric |
|
6941 |
'1234' isAlphaNumeric |
|
6942 |
'+' isAlphaNumeric |
|
6943 |
" |
|
6944 |
||
6945 |
"Modified: / 13-10-2006 / 12:53:49 / cg" |
|
6946 |
! |
|
6947 |
||
6948 |
isBinarySelector |
|
6949 |
"treating the receiver as a message selector, return true if it's a binary selector. |
|
6950 |
Notice, that st/x does not have a size <= 2 limit for unaries" |
|
6951 |
||
6952 |
|binopChars| |
|
6953 |
||
6954 |
(self size > Method maxBinarySelectorSize) ifTrue:[^ false]. |
|
6955 |
||
6956 |
binopChars := Method binarySelectorCharacters. |
|
6957 |
^ (self conform:[:char | (binopChars includes:char)]) |
|
6958 |
||
6959 |
" |
|
6960 |
'foo:bar:' isBinarySelector |
|
6961 |
#foo:bar: isBinarySelector |
|
6962 |
'hello' isBinarySelector |
|
6963 |
'+' isBinarySelector |
|
6964 |
'|' isBinarySelector |
|
6965 |
'?' isBinarySelector |
|
6966 |
':' isBinarySelector |
|
6967 |
'a:' isBinarySelector |
|
6968 |
'->' isBinarySelector |
|
6969 |
'<->' isBinarySelector |
|
6970 |
'::' isBinarySelector |
|
6971 |
" |
|
6972 |
||
6973 |
"Modified: / 04-01-1997 / 14:16:14 / cg" |
|
6974 |
"Modified (comment): / 13-02-2017 / 19:57:29 / cg" |
|
6975 |
! |
|
6976 |
||
6977 |
isBlank |
|
6978 |
"return true, if the receiver contains spaces only" |
|
6979 |
||
6980 |
^ self size == 0 or:[(self contains:[:char | char ~~ Character space]) not] |
|
6981 |
||
6982 |
" |
|
6983 |
'' isBlank |
|
6984 |
'' asUnicode16String isBlank |
|
6985 |
' a ' isBlank |
|
6986 |
' ' isBlank |
|
6987 |
' ' asUnicode16String isBlank |
|
6988 |
" |
|
6989 |
||
6990 |
"Modified (comment): / 14-09-2018 / 10:04:18 / Stefan Vogel" |
|
6991 |
! |
|
6992 |
||
6993 |
isInfix |
|
6994 |
"return true, if the receiver is a binary message selector" |
|
6995 |
||
6996 |
^ self first isLetterOrDigit not |
|
6997 |
||
6998 |
" |
|
6999 |
#at:put: isInfix |
|
7000 |
#at: isInfix |
|
7001 |
#+ isInfix |
|
7002 |
#size isInfix |
|
7003 |
" |
|
7004 |
||
7005 |
"Created: / 1.11.1997 / 12:34:55 / cg" |
|
7006 |
"Modified: / 1.11.1997 / 12:36:37 / cg" |
|
7007 |
! |
|
7008 |
||
7009 |
isKeyword |
|
7010 |
"return true, if the receiver is a keyword message selector. |
|
7011 |
This is a quick check, which only looks at the last character. |
|
7012 |
Should only be used, if we already know that the receiver forms a valid selector. |
|
7013 |
To check an arbitrary string, use isKeywordSelector. |
|
7014 |
Bad naming, but compatibility is asking for it." |
|
7015 |
||
7016 |
^ self last == $: |
|
7017 |
||
7018 |
" |
|
7019 |
#at:put: isKeyword |
|
7020 |
#at: isKeyword |
|
7021 |
#+ isKeyword |
|
7022 |
#size isKeyword |
|
7023 |
" |
|
7024 |
||
7025 |
"Created: / 01-11-1997 / 12:34:55 / cg" |
|
7026 |
"Modified (comment): / 30-04-2016 / 18:19:11 / cg" |
|
7027 |
! |
|
7028 |
||
7029 |
isKeywordSelector |
|
7030 |
"return true, iff there are only alphanumeric or underline characters separated by colons. |
|
7031 |
Must end with a colon. |
|
7032 |
You can use this to check an arbitrary string for being valid as a keyword. |
|
7033 |
If you have a valid selector at hand, and need to know if it is a keyword or not, |
|
7034 |
use #isKeyword, which is much faster." |
|
7035 |
||
7036 |
|state| |
|
7037 |
||
7038 |
(self size == 0) ifTrue:[^ false]. |
|
7039 |
(self last == $:) ifFalse:[^ false]. |
|
7040 |
||
7041 |
state := #initial. |
|
7042 |
self do:[:char | |
|
7043 |
(state == #initial or:[ state == #gotColon]) ifTrue:[ |
|
7044 |
(char isLetterOrUnderline) ifFalse:[^ false]. |
|
7045 |
state := #gotCharacter. |
|
7046 |
] ifFalse:[ |
|
7047 |
char == $: ifTrue:[ |
|
7048 |
state := #gotColon. |
|
7049 |
] ifFalse:[ |
|
24254 | 7050 |
(char isLetterOrDigitOrUnderline) ifFalse:[^ false]. |
23701 | 7051 |
]. |
7052 |
]. |
|
7053 |
]. |
|
7054 |
^ state == #gotColon. |
|
7055 |
||
7056 |
" |
|
7057 |
self assert:(':' isKeywordSelector not). |
|
7058 |
self assert:(':a' isKeywordSelector not). |
|
7059 |
self assert:('1:' isKeywordSelector not). |
|
7060 |
self assert:('a:' isKeywordSelector). |
|
7061 |
self assert:('_:' isKeywordSelector). |
|
7062 |
self assert:('_a:' isKeywordSelector). |
|
7063 |
self assert:('_1:' isKeywordSelector). |
|
7064 |
self assert:('_1::' isKeywordSelector not). |
|
7065 |
self assert:('_:_:' isKeywordSelector). |
|
7066 |
self assert:('a:b:' isKeywordSelector). |
|
7067 |
self assert:('aa:bb:' isKeywordSelector). |
|
7068 |
self assert:('aa:bb:a' isKeywordSelector not). |
|
7069 |
self assert:('1:2:' isKeywordSelector not). |
|
7070 |
" |
|
7071 |
||
7072 |
"Modified (comment): / 30-04-2016 / 18:20:14 / cg" |
|
24254 | 7073 |
"Modified: / 05-06-2019 / 17:05:53 / Claus Gittinger" |
23701 | 7074 |
! |
7075 |
||
11828 | 7076 |
isLowercaseFirst |
7077 |
"return true, if the first character is a lowercase character." |
|
7078 |
||
7079 |
^ self size ~~ 0 and:[ self first isLowercase ] |
|
7080 |
||
7081 |
" |
|
12177 | 7082 |
'helloWorld' isLowercaseFirst |
7083 |
'HelloWorld' isLowercaseFirst |
|
11828 | 7084 |
" |
7085 |
! |
|
7086 |
||
23701 | 7087 |
isNameSpaceSelector |
7088 |
"Answer true if the receiver contains chars which form a nameSpace selector name. |
|
7089 |
These are of the form ':<ns>::<sel>', where ns is the NameSpace and sel is the regular selector. |
|
7090 |
For example, the #+ selector as seen by the Foo namespace would be actually #':Foo::+'. |
|
7091 |
This special format (a symbol starting with a colon) was chosen, because almost every other selector |
|
7092 |
is legal, and this can be checked quickly by just looking at the first character. |
|
7093 |
You cannot easily change this algorithm here, as it is also known by the VM's lookup function." |
|
7094 |
||
7095 |
(self at:1) == $: ifFalse:[^ false]. |
|
24120 | 7096 |
^ (self indexOfSubCollection:'::' startingAt:3 ifAbsent:0 caseSensitive:true) ~~ 0. |
23701 | 7097 |
|
7098 |
"test: |
|
7099 |
self assert:('+' isNameSpaceSelector) not. |
|
7100 |
self assert:(':+' isNameSpaceSelector) not. |
|
7101 |
self assert:(':Foo:+' isNameSpaceSelector) not. |
|
7102 |
||
7103 |
self assert:(':Foo::+' isNameSpaceSelector). |
|
7104 |
self assert:(':Foo::bar:baz:' isNameSpaceSelector). |
|
7105 |
" |
|
7106 |
||
7107 |
"Created: / 05-03-2007 / 11:35:31 / cg" |
|
24120 | 7108 |
"Modified: / 08-05-2019 / 14:41:30 / Stefan Vogel" |
23701 | 7109 |
! |
7110 |
||
7111 |
isNumeric |
|
7112 |
"return true, if the receiver is some numeric word; |
|
7113 |
i.e. consists only of digits." |
|
7114 |
||
7115 |
self size == 0 ifTrue:[ |
|
7116 |
^ false |
|
7117 |
]. |
|
7118 |
^ self conform:[:char | char isDigit] |
|
7119 |
||
7120 |
" |
|
7121 |
'helloWorld' isNumeric |
|
7122 |
'foo1234' isNumeric |
|
7123 |
'f1234' isNumeric |
|
7124 |
'1234' isNumeric |
|
7125 |
'+' isNumeric |
|
7126 |
" |
|
7127 |
||
7128 |
"Modified: / 13-10-2006 / 12:54:12 / cg" |
|
7129 |
! |
|
7130 |
||
11828 | 7131 |
isUppercaseFirst |
7132 |
"return true, if the first character is an uppercase character." |
|
7133 |
||
7134 |
^ self size ~~ 0 and:[ self first isUppercase ] |
|
7135 |
||
7136 |
" |
|
12177 | 7137 |
'helloWorld' isUppercaseFirst |
7138 |
'HelloWorld' isUppercaseFirst |
|
11828 | 7139 |
" |
7140 |
! |
|
7141 |
||
23701 | 7142 |
isValidSmalltalkIdentifier |
7143 |
"return true, if the receiver's characters make up a valid smalltalk identifier" |
|
7144 |
||
7145 |
|scanner tok| |
|
7146 |
||
7147 |
scanner := Compiler new. |
|
7148 |
scanner source:(self readStream). |
|
7149 |
ParseError handle:[:ex | |
|
7150 |
tok := nil. |
|
7151 |
] do:[ |
|
7152 |
tok := scanner nextToken. |
|
7153 |
]. |
|
7154 |
tok ~~ #Identifier ifTrue:[ |
|
7155 |
^ false |
|
7156 |
]. |
|
7157 |
scanner tokenPosition == 1 ifFalse:[^ false]. |
|
7158 |
^ scanner sourceStream atEnd. |
|
7159 |
||
7160 |
" |
|
7161 |
'foo' isValidSmalltalkIdentifier |
|
7162 |
'1foo' isValidSmalltalkIdentifier |
|
7163 |
'_foo' isValidSmalltalkIdentifier |
|
7164 |
'_foo_bar_' isValidSmalltalkIdentifier |
|
7165 |
'foo ' isValidSmalltalkIdentifier |
|
7166 |
' foo' isValidSmalltalkIdentifier |
|
7167 |
" |
|
7168 |
! |
|
7169 |
||
7170 |
isWhitespace |
|
7171 |
"return true, if the receiver is empty or contains only whitespace." |
|
7172 |
||
7173 |
^ (self indexOfNonSeparatorStartingAt:1) == 0 |
|
7174 |
||
7175 |
" |
|
7176 |
'' isWhitespace |
|
7177 |
' ' isWhitespace |
|
7178 |
' \ \' withCRs isWhitespace |
|
7179 |
' a\ \' withCRs isWhitespace |
|
7180 |
' \ \a' withCRs isWhitespace |
|
7181 |
'a \ \a' withCRs isWhitespace |
|
7182 |
" |
|
7183 |
||
7184 |
"Created: / 01-03-2017 / 15:24:53 / cg" |
|
7185 |
! |
|
7186 |
||
15773 | 7187 |
keywords |
7188 |
"assuming the receiver is a keyword message selector, |
|
7189 |
return the individual keywords (i.e. break it up at colons) |
|
7190 |
and return these as a collection. |
|
7191 |
For binary and unary selectors, the result may be nonsense (an array containing the receiver)." |
|
7192 |
||
7193 |
|coll s part| |
|
7194 |
||
7195 |
coll := OrderedCollection new. |
|
7196 |
s := ReadStream on:self. |
|
7197 |
[s atEnd] whileFalse:[ |
|
20874 | 7198 |
part := s through:$:. |
7199 |
coll add:part |
|
15773 | 7200 |
]. |
7201 |
^ coll asArray |
|
7202 |
||
7203 |
" |
|
7204 |
#at:put: keywords |
|
7205 |
#at: keywords |
|
7206 |
#+ keywords |
|
7207 |
#size keywords |
|
7208 |
" |
|
7209 |
||
7210 |
"Modified (Comment): / 30-06-2011 / 17:46:21 / cg" |
|
7211 |
! |
|
7212 |
||
9190 | 7213 |
knownAsSymbol |
7214 |
"for now, only single character strings are allowed as symbols. |
|
7215 |
This method is redefined in String." |
|
7216 |
||
7217 |
^ false |
|
7218 |
! |
|
7219 |
||
7220 |
leftIndent |
|
23663 | 7221 |
"if the receiver starts with whiteSpace, return the number of whiteSpace chars |
9190 | 7222 |
at the left - otherwise, return 0. |
23663 | 7223 |
If the receiver consists of whiteSpace only, return the receiver's size." |
9192 | 7224 |
|
7225 |
|index "{Class: SmallInteger }" |
|
7226 |
end "{Class: SmallInteger }"| |
|
7227 |
||
7228 |
index := 1. |
|
7229 |
end := self size. |
|
7230 |
[index <= end] whileTrue:[ |
|
20874 | 7231 |
(self at:index) isSeparator ifFalse:[^ index - 1]. |
7232 |
index := index + 1 |
|
8350 | 7233 |
]. |
9192 | 7234 |
^ end |
7235 |
||
7236 |
" |
|
7237 |
' hello' leftIndent |
|
7238 |
'foo ' leftIndent |
|
9190 | 7239 |
' ' leftIndent |
23663 | 7240 |
((Character tab),(Character tab),'foo') leftIndent |
7241 |
" |
|
7242 |
||
7243 |
"Modified: / 20-04-1996 / 19:28:43 / cg" |
|
7244 |
"Modified (comment): / 05-02-2019 / 11:22:57 / Claus Gittinger" |
|
8350 | 7245 |
! |
7246 |
||
10437 | 7247 |
nameSpaceSelectorParts |
7248 |
"Answer the namespace and baseSelector parts of a namespace selector. |
|
7249 |
Namespace selectors are those generated by sends from a method in a different |
|
7250 |
namespace; they are prefixed by ':'<ns>'::'. |
|
7251 |
You cannot easily change this algorithm here, as it is also known by the VM's lookup function. |
|
7252 |
Experimental" |
|
7253 |
||
7254 |
|nsPart selPart idx| |
|
7255 |
||
7256 |
(self at:1) == $: ifFalse:[^ Array with:'' with:self]. |
|
7257 |
idx := self indexOf:$: startingAt:3. |
|
7258 |
idx == 0 ifTrue:[^ Array with:'' with:self]. |
|
7259 |
||
7260 |
(idx+2 > self size) ifTrue:[^ Array with:'' with:self]. |
|
7261 |
(self at:idx+1) == $: ifFalse:[^ Array with:'' with:self]. |
|
7262 |
nsPart := self copyFrom:2 to:idx-1. |
|
7263 |
selPart := self copyFrom:idx+2. |
|
7264 |
^ Array with:nsPart with:selPart |
|
7265 |
||
7266 |
"test: |
|
10703
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
7267 |
self assert:('+' nameSpaceSelectorParts) = #('' '+'). |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
7268 |
self assert:(':+' nameSpaceSelectorParts) = #('' ':+'). |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
7269 |
self assert:(':Foo:+' nameSpaceSelectorParts) = #('' ':Foo:+'). |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
7270 |
|
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
7271 |
self assert:(':Foo::+' nameSpaceSelectorParts) = #('Foo' '+'). |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
7272 |
self assert:(':Foo::bar:baz:' nameSpaceSelectorParts) = #('Foo' 'bar:baz:'). |
10437 | 7273 |
" |
7274 |
||
7275 |
"Created: / 05-03-2007 / 17:16:58 / cg" |
|
7276 |
"Modified: / 06-03-2007 / 11:51:15 / cg" |
|
7277 |
! |
|
7278 |
||
23701 | 7279 |
numArgs |
7280 |
<resource: #obsolete> |
|
7281 |
"treating the receiver as a message selector, return how many arguments would it take. |
|
7282 |
Please use argumentCount for ANSI compatibility." |
|
7283 |
||
7284 |
^ self argumentCount |
|
7285 |
! |
|
7286 |
||
7287 |
partsIfSelector |
|
7288 |
"treat the receiver as a message selector, return a collection of parts. |
|
7289 |
Notice: this is more tolerant than Smalltalk's syntax would suggest; |
|
7290 |
especially it allows for empty keyword parts between colons. |
|
7291 |
This is not (and should not be checked here), to allow parsing of |
|
7292 |
degenerate selectors as appearing with objectiveC." |
|
7293 |
||
7294 |
|idx1 "{ Class: SmallInteger }" |
|
7295 |
coll idx2 sz| |
|
7296 |
||
7297 |
coll := OrderedCollection new. |
|
7298 |
idx1 := 1. |
|
7299 |
sz := self size. |
|
7300 |
[ |
|
7301 |
idx2 := self indexOf:$: startingAt:idx1. |
|
7302 |
(idx2 == 0 or:[idx2 == sz]) ifTrue:[ |
|
7303 |
coll add:(self copyFrom:idx1). |
|
7304 |
^ coll |
|
7305 |
]. |
|
7306 |
coll add:(self copyFrom:idx1 to:idx2). |
|
7307 |
idx1 := idx2 + 1 |
|
7308 |
] loop. |
|
7309 |
||
7310 |
" |
|
7311 |
'foo:' partsIfSelector |
|
7312 |
'foo:bar:' partsIfSelector |
|
7313 |
'foo::::' partsIfSelector |
|
7314 |
#foo:bar: partsIfSelector |
|
7315 |
'hello' partsIfSelector |
|
7316 |
'+' partsIfSelector |
|
7317 |
" |
|
7318 |
! |
|
7319 |
||
21283 | 7320 |
speciesForSubcollection |
7321 |
"answer the class, when splitting instances into subcollections" |
|
7322 |
||
7323 |
^ StringCollection |
|
7324 |
||
7325 |
"Created: / 24-01-2017 / 18:54:18 / stefan" |
|
7326 |
! |
|
7327 |
||
9190 | 7328 |
stringSpecies |
8874 | 7329 |
"return the underlying strings bitsPerCharacter |
8037
8cce25b1059c
care for wide chars in asXXXFirst
Claus Gittinger <cg@exept.de>
parents:
8032
diff
changeset
|
7330 |
(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
|
7331 |
|
8cce25b1059c
care for wide chars in asXXXFirst
Claus Gittinger <cg@exept.de>
parents:
8032
diff
changeset
|
7332 |
|string| |
8cce25b1059c
care for wide chars in asXXXFirst
Claus Gittinger <cg@exept.de>
parents:
8032
diff
changeset
|
7333 |
|
15523 | 7334 |
string := self string. |
7335 |
string == self ifTrue:[^ self species]. |
|
8037
8cce25b1059c
care for wide chars in asXXXFirst
Claus Gittinger <cg@exept.de>
parents:
8032
diff
changeset
|
7336 |
^ string stringSpecies |
8cce25b1059c
care for wide chars in asXXXFirst
Claus Gittinger <cg@exept.de>
parents:
8032
diff
changeset
|
7337 |
|
8cce25b1059c
care for wide chars in asXXXFirst
Claus Gittinger <cg@exept.de>
parents:
8032
diff
changeset
|
7338 |
" |
8874 | 7339 |
'hello' stringSpecies |
9192 | 7340 |
'hello' asText allBold stringSpecies |
7341 |
" |
|
7342 |
! |
|
8037
8cce25b1059c
care for wide chars in asXXXFirst
Claus Gittinger <cg@exept.de>
parents:
8032
diff
changeset
|
7343 |
|
6653 | 7344 |
widthFrom:startIndex to:endIndex on:aGC |
19464 | 7345 |
"return ths size of part of the receiver in device units if displayed on aGC" |
6653 | 7346 |
|
20221 | 7347 |
^ aGC deviceFont widthOf:self from:startIndex to:endIndex |
6653 | 7348 |
|
7349 |
" |
|
8874 | 7350 |
'hello world' widthFrom:1 to:5 on:(View new) |
7351 |
'hello' widthOn:(View new) |
|
6653 | 7352 |
" |
9192 | 7353 |
! |
6653 | 7354 |
|
1388 | 7355 |
widthOn:aGC |
19464 | 7356 |
"return ths size of the receiver in device units if displayed on aGC" |
1388 | 7357 |
|
20221 | 7358 |
^ aGC deviceFont widthOf:self "font is already a device font" |
1388 | 7359 |
|
7360 |
" |
|
8874 | 7361 |
'hello world' widthOn:(View new) |
1388 | 7362 |
" |
7363 |
||
7364 |
"Created: 12.5.1996 / 20:09:29 / cg" |
|
2554 | 7365 |
"Modified: 17.4.1997 / 12:50:23 / cg" |
608 | 7366 |
! ! |
7367 |
||
24250 | 7368 |
|
608 | 7369 |
!CharacterArray methodsFor:'special string converting'! |
7370 |
||
16759 | 7371 |
asUnixFilenameString |
7372 |
"return a new string consisting of receiver's characters |
|
7373 |
with all \-characters replaced by /-characters. |
|
7374 |
If there are no backslashes, return the original" |
|
7375 |
||
7376 |
^ self copyReplaceAll:$\ with:$/ ifNone:self |
|
7377 |
||
7378 |
" |
|
7379 |
'hello\world' asUnixFilenameString |
|
7380 |
" |
|
7381 |
||
7382 |
"Modified: / 18.7.1998 / 22:53:02 / cg" |
|
7383 |
! |
|
7384 |
||
14768 | 7385 |
expandPlaceholders:escapeCharacter with:argArrayOrDictionary |
7386 |
"this is the generic version of the old %-escaping method, allowing for an arbitrary |
|
7387 |
escape character to be used (typically $$ or $% are effectively used). |
|
7388 |
Return a copy of the receiver, where all %i escapes are |
|
7389 |
replaced by corresponding arguments' printStrings from the argArrayOrDictionary. |
|
7390 |
I.e. 'hello %1; how is %2' expandPlaceholdersWith:#('world' 'this') results |
|
7391 |
in the new string 'hello world; how is this'. |
|
7392 |
As an extension, the argument may also be a dictionary, providing |
|
7393 |
values for symbolic keys. |
|
7394 |
In this case, %a .. %z and %(...) are also allowed. |
|
7395 |
(%1..%9 require a numeric key in the dictionary, however) |
|
7396 |
To get a '%' character, use a '%%'-escape. |
|
7397 |
To get an integer-indexed placeHolder followed by another digit, |
|
7398 |
or an index > 9, you must use %(digit). |
|
7399 |
See also bindWith:... for VisualAge compatibility." |
|
7400 |
||
7401 |
|stream| |
|
7402 |
||
18269 | 7403 |
stream := (TextStream ? CharacterWriteStream) on:(self species uninitializedNew:self size + 20). |
23067 | 7404 |
self expandPlaceholders:escapeCharacter with:argArrayOrDictionary ignoreNumericEscapes:false on:stream. |
14768 | 7405 |
^ stream contents. |
7406 |
||
7407 |
||
7408 |
" |
|
7409 |
'hello %1' expandPlaceholdersWith:#('world') |
|
7410 |
'hello %1; how is %2' expandPlaceholdersWith:#('world' 'this') |
|
7411 |
'hello %2; how is %1' expandPlaceholdersWith:#('world' 'this') |
|
7412 |
'%1 plus %2 gives %3 ' expandPlaceholdersWith:#(4 5 9) |
|
7413 |
'%%(1)0 gives %(1)0' expandPlaceholdersWith:#(123) |
|
7414 |
'%%10 gives %10' expandPlaceholdersWith:#(123) |
|
7415 |
'%%(10) gives %(10)' expandPlaceholdersWith:#(123) |
|
7416 |
'%test gives %1' expandPlaceholdersWith:#(123) |
|
7417 |
'bla %1 bla' expandPlaceholdersWith:{ 'hello' allBold } |
|
7418 |
'bla %1 bla' expandPlaceholdersWith:{ 'hello' } |
|
7419 |
" |
|
7420 |
||
7421 |
" |
|
7422 |
|dict| |
|
7423 |
||
7424 |
dict := Dictionary new. |
|
7425 |
dict at:1 put:'one'. |
|
7426 |
dict at:$a put:'AAAAA'. |
|
7427 |
dict at:$b put:[ Time now ]. |
|
7428 |
dict at:'foo' put:[ Date today ]. |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
7429 |
'hello $1 %a $b %(foo) $foo ' expandPlaceholders:$$ with:dict. |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
7430 |
'hello $1 %a $b %(foo) $foo ' expandPlaceholders:$% with:dict. |
14768 | 7431 |
" |
7432 |
||
23067 | 7433 |
"Modified: / 01-07-1997 / 00:53:24 / cg" |
7434 |
"Modified: / 14-06-2018 / 11:46:18 / Claus Gittinger" |
|
7435 |
! |
|
7436 |
||
7437 |
expandPlaceholders:escapeCharacter with:argArrayOrDictionary ignoreNumericEscapes:ignoreNumericEscapes on:aStream |
|
14765 | 7438 |
"this is the generic version of the old %-escaping method, allowing for an arbitrary |
7439 |
escape character to be used (typically $$ or $% are effectively used). |
|
18425 | 7440 |
|
14765 | 7441 |
Write the receiver to aStream, where all %i escapes are |
23067 | 7442 |
replaced by corresponding arguments' printStrings from the argArray. |
14765 | 7443 |
I.e. 'hello %1; how is %2' expandPlaceholdersWith:#('world' 'this') results |
7444 |
in the new string 'hello world; how is this'. |
|
18425 | 7445 |
|
17192 | 7446 |
As an extension, the argument may also be a dictionary, providing values for symbolic keys. |
14765 | 7447 |
In this case, %a .. %z and %(...) are also allowed. |
7448 |
(%1..%9 require a numeric key in the dictionary, however) |
|
23067 | 7449 |
|
7450 |
Also, values in argArrayOrDictionary may be blocks. |
|
18425 | 7451 |
|
14765 | 7452 |
To get a '%' character, use a '%%'-escape. |
7453 |
To get an integer-indexed placeHolder followed by another digit, |
|
7454 |
or an index > 9, you must use %(digit). |
|
18425 | 7455 |
|
14765 | 7456 |
See also bindWith:... for VisualAge compatibility. |
23067 | 7457 |
Use %<cr> to insert a CR and %<tab> to insert a TAB. |
7458 |
||
7459 |
ignoreNumericEscapes controls if %<nr> escapes are expanded or not. |
|
7460 |
This is required for Windows batch-script expansion, where %<nr> should be left |
|
7461 |
unchanged." |
|
14765 | 7462 |
|
23603 | 7463 |
^ self |
7464 |
expandPlaceholders:escapeCharacter |
|
7465 |
with:argArrayOrDictionary |
|
7466 |
ignoreNumericEscapes:ignoreNumericEscapes |
|
7467 |
requireParentheses:true |
|
7468 |
on:aStream |
|
14765 | 7469 |
|
7470 |
" |
|
7471 |
String streamContents:[:s| |
|
20874 | 7472 |
'hello %1' expandPlaceholders:$% with:#('world') on:s. |
7473 |
s cr. |
|
7474 |
'hello $1; how is $2' expandPlaceholders:$$ with:#('world' 'this') on:s. |
|
7475 |
s cr. |
|
7476 |
'hello %2; how is %1' expandPlaceholders:$% with:#('world' 'this') on:s. |
|
7477 |
s cr. |
|
7478 |
'%1 plus %2 gives %3 ' expandPlaceholders:$% with:#(4 5 9) on:s. |
|
7479 |
s cr. |
|
7480 |
'%%(1)0 gives %(1)0' expandPlaceholders:$% with:#(123) on:s. |
|
7481 |
s cr. |
|
7482 |
'%%10 gives %10' expandPlaceholders:$% with:#(123) on:s. |
|
7483 |
s cr. |
|
7484 |
'%%(10) gives %(10) %<cr>%<tab>next line' expandPlaceholders:$% with:#(123) on:s. |
|
7485 |
s cr. |
|
7486 |
'%%test gives %test' expandPlaceholders:$% with:#(123) on:s. |
|
7487 |
s cr. |
|
7488 |
'|%%<tab>|%%1|%%<cr>| gives |%<tab>|%1|%<cr>|' expandPlaceholders:$% with:#(foo) on:s. |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
7489 |
] |
14765 | 7490 |
" |
7491 |
||
23325 | 7492 |
"without xlation dictionary: |
7493 |
'hello %1' expandPlaceholdersWith:nil. |
|
7494 |
'hello%<cr> %1' expandPlaceholdersWith:nil. |
|
7495 |
" |
|
7496 |
||
14765 | 7497 |
" |
7498 |
|dict| |
|
7499 |
||
7500 |
dict := Dictionary new. |
|
7501 |
dict at:1 put:'one'. |
|
7502 |
dict at:$a put:'AAAAA'. |
|
7503 |
dict at:$b put:[ Time now ]. |
|
7504 |
String streamContents:[:s| |
|
20874 | 7505 |
'hello $1 $a $b' expandPlaceholders:$$ with:dict on:s. |
14765 | 7506 |
]. |
7507 |
" |
|
7508 |
||
18425 | 7509 |
"using blocks: |
7510 |
|dict| |
|
7511 |
||
7512 |
dict := Dictionary new. |
|
7513 |
dict at:'time' put:[Time now printString]. |
|
7514 |
dict at:'date' put:[Date today printString]. |
|
7515 |
String streamContents:[:s| |
|
20874 | 7516 |
'it is $(time) $(date)' expandPlaceholders:$$ with:dict on:s. |
18425 | 7517 |
]. |
7518 |
" |
|
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
7519 |
|
23067 | 7520 |
"Created: / 14-06-2018 / 11:44:08 / Claus Gittinger" |
23603 | 7521 |
"Modified (comment): / 14-01-2019 / 17:44:02 / Claus Gittinger" |
7522 |
! |
|
7523 |
||
7524 |
expandPlaceholders:escapeCharacter with:argArrayOrDictionary ignoreNumericEscapes:ignoreNumericEscapes requireParentheses:requireParentheses on:aStream |
|
7525 |
"this is the generic version of the old %-escaping method, allowing for an arbitrary |
|
7526 |
escape character to be used (typically $$ or $% are effectively used). |
|
7527 |
||
7528 |
Write the receiver to aStream, where all %i escapes are |
|
7529 |
replaced by corresponding arguments' printStrings from the argArray. |
|
7530 |
I.e. 'hello %1; how is %2' expandPlaceholdersWith:#('world' 'this') results |
|
7531 |
in the new string 'hello world; how is this'. |
|
7532 |
||
7533 |
As an extension, the argument may also be a dictionary, providing values for symbolic keys. |
|
7534 |
In this case, %a .. %z and %(...) are also allowed. |
|
7535 |
(%1..%9 require a numeric key in the dictionary, however) |
|
7536 |
||
7537 |
Also, values in argArrayOrDictionary may be blocks. |
|
7538 |
||
7539 |
To get a '%' character, use a '%%'-escape. |
|
7540 |
To get an integer-indexed placeHolder followed by another digit, |
|
7541 |
or an index > 9, you must use %(digit). |
|
7542 |
||
7543 |
See also bindWith:... for VisualAge compatibility. |
|
7544 |
Use %<cr> to insert a CR and %<tab> to insert a TAB. |
|
7545 |
||
7546 |
ignoreNumericEscapes controls if %<nr> escapes are expanded or not. |
|
7547 |
This is required for Windows batch-script expansion, where %<nr> should be left unchanged. |
|
7548 |
||
7549 |
requireParentheses controls if $abc is allowed or not. |
|
7550 |
If true, multi-character replacements need to be parenthized as $(abc); |
|
7551 |
if false, you can also write $abc. |
|
7552 |
" |
|
7553 |
||
7554 |
|next v key numericKey |
|
7555 |
idx "{ SmallInteger }" |
|
7556 |
idx2 "{ SmallInteger }" |
|
7557 |
start "{ SmallInteger }" |
|
7558 |
stop "{ SmallInteger }"| |
|
7559 |
||
7560 |
stop := self size. |
|
7561 |
start := 1. |
|
7562 |
[start <= stop] whileTrue:[ |
|
7563 |
idx := self indexOf:escapeCharacter startingAt:start. |
|
7564 |
(idx == 0 or:[idx == stop]) ifTrue:[ |
|
7565 |
aStream nextPutAll:self startingAt:start to:stop. |
|
7566 |
^ self. |
|
7567 |
]. |
|
7568 |
"found an escapeCharacter" |
|
7569 |
aStream nextPutAll:self startingAt:start to:(idx - 1). |
|
7570 |
next := self at:(idx + 1). |
|
7571 |
(next == escapeCharacter) ifTrue:[ |
|
7572 |
"doubled escapeCharacter" |
|
7573 |
aStream nextPut:escapeCharacter. |
|
7574 |
] ifFalse:[ |
|
7575 |
"%<...> " |
|
7576 |
next == $< ifTrue:[ |
|
7577 |
idx2 := self indexOf:$> startingAt:idx+2. |
|
7578 |
key := self copyFrom:idx+2 to:idx2-1. |
|
7579 |
idx := idx2 - 1. |
|
7580 |
key := key asSymbolIfInterned. |
|
7581 |
(#(cr tab nl return lf ff null) includesIdentical:key) ifTrue:[ |
|
7582 |
aStream nextPut:(Character perform:key). |
|
7583 |
]. |
|
7584 |
] ifFalse:[ |
|
7585 |
argArrayOrDictionary isNil ifTrue:[ |
|
7586 |
aStream nextPut:escapeCharacter. |
|
7587 |
aStream nextPut:next. |
|
7588 |
] ifFalse:[ |
|
7589 |
(next isDigit and:[ignoreNumericEscapes not]) ifTrue:[ |
|
7590 |
v := argArrayOrDictionary at:(next digitValue) ifAbsent:'' |
|
7591 |
] ifFalse:[ |
|
7592 |
next == $( ifTrue:[ |
|
7593 |
idx2 := self indexOf:$) startingAt:idx+2. |
|
7594 |
self assert:(idx2 > 0). |
|
7595 |
key := self copyFrom:idx+2 to:idx2-1. |
|
7596 |
idx := idx2 - 1. |
|
7597 |
||
7598 |
(key isNumeric |
|
7599 |
and:[(numericKey := Integer readFrom:key onError:nil) notNil |
|
7600 |
and:[(argArrayOrDictionary includesKey:numericKey)]]) ifTrue:[ |
|
7601 |
v := argArrayOrDictionary at:numericKey ifAbsent:'' |
|
7602 |
] ifFalse:[ |
|
7603 |
argArrayOrDictionary isBlock ifTrue:[ |
|
7604 |
v := argArrayOrDictionary value:key |
|
7605 |
] ifFalse:[ |
|
7606 |
(argArrayOrDictionary includesKey:key) ifTrue:[ |
|
7607 |
v := argArrayOrDictionary at:key |
|
7608 |
] ifFalse:[ |
|
7609 |
key := key asSymbolIfInternedOrSelf. |
|
7610 |
(argArrayOrDictionary includesKey:key) ifTrue:[ |
|
7611 |
v := argArrayOrDictionary at:key |
|
7612 |
] ifFalse:[ |
|
7613 |
(key size == 1 and:[ argArrayOrDictionary includesKey:(key at:1)]) ifTrue:[ |
|
7614 |
v := argArrayOrDictionary at:(key at:1) |
|
7615 |
] ifFalse:[ |
|
7616 |
v := '' |
|
7617 |
] |
|
7618 |
]. |
|
7619 |
]. |
|
7620 |
]. |
|
7621 |
]. |
|
7622 |
] ifFalse:[ |
|
7623 |
(next isLetter and:[argArrayOrDictionary isSequenceable not "is a Dictionary"]) ifTrue:[ |
|
7624 |
"so next is a non-numeric single character." |
|
7625 |
requireParentheses ifTrue:[ |
|
7626 |
key := next. |
|
7627 |
] ifFalse:[ |
|
24254 | 7628 |
idx2 := self findFirst:[:ch | (ch isLetterOrDigitOrUnderline) not] startingAt:idx+2. |
23603 | 7629 |
idx2 == 0 ifTrue:[idx2 := self size + 1]. |
7630 |
key := self copyFrom:idx+1 to:idx2-1. |
|
7631 |
idx := idx2 - 2. |
|
7632 |
]. |
|
7633 |
||
7634 |
argArrayOrDictionary isBlock ifTrue:[ |
|
7635 |
v := argArrayOrDictionary value:key |
|
7636 |
] ifFalse:[ |
|
7637 |
v := argArrayOrDictionary |
|
7638 |
at:key |
|
7639 |
ifAbsent:[ |
|
7640 |
"try symbol or string instead of character" |
|
7641 |
argArrayOrDictionary |
|
7642 |
at:key asString asSymbolIfInternedOrSelf |
|
7643 |
ifAbsent:[escapeCharacter asString , key]. |
|
7644 |
]. |
|
7645 |
]. |
|
7646 |
] ifFalse:[ |
|
7647 |
v := String with:$% with:next. |
|
7648 |
]. |
|
7649 |
] |
|
7650 |
]. |
|
7651 |
"/ v notNil ifTrue:[ |
|
7652 |
v isBlock ifTrue:[ |
|
7653 |
v := v value |
|
7654 |
]. |
|
7655 |
||
7656 |
v printOn:aStream. |
|
7657 |
"/ ]. |
|
7658 |
] |
|
7659 |
]. |
|
7660 |
]. |
|
7661 |
start := idx + 2 |
|
7662 |
]. |
|
7663 |
||
7664 |
" |
|
7665 |
'hello %1' expandPlaceholders:$% with:#('world') on:Transcript. |
|
7666 |
'hello %a%<cr>' expandPlaceholders:$% |
|
7667 |
with:(Dictionary new at:'a' put:'world';yourself) |
|
7668 |
on:Transcript. |
|
7669 |
'hello %aa%<cr>' expandPlaceholders:$% with:(Dictionary new at:'a' put:'world';yourself) on:Transcript. |
|
7670 |
'hello %(aa)%<cr>' expandPlaceholders:$% with:(Dictionary new at:'aa' put:'world';yourself) on:Transcript. |
|
7671 |
'hello %aa%<cr>' expandPlaceholders:$% with:(Dictionary new at:'aa' put:'world';yourself) ignoreNumericEscapes:true requireParentheses:false on:Transcript. |
|
7672 |
||
7673 |
String streamContents:[:s| |
|
7674 |
'hello %1' expandPlaceholders:$% with:#('world') on:s. |
|
7675 |
s cr. |
|
7676 |
'hello $1; how is $2' expandPlaceholders:$$ with:#('world' 'this') on:s. |
|
7677 |
s cr. |
|
7678 |
'hello %2; how is %1' expandPlaceholders:$% with:#('world' 'this') on:s. |
|
7679 |
s cr. |
|
7680 |
'%1 plus %2 gives %3 ' expandPlaceholders:$% with:#(4 5 9) on:s. |
|
7681 |
s cr. |
|
7682 |
'%%(1)0 gives %(1)0' expandPlaceholders:$% with:#(123) on:s. |
|
7683 |
s cr. |
|
7684 |
'%%10 gives %10' expandPlaceholders:$% with:#(123) on:s. |
|
7685 |
s cr. |
|
7686 |
'%%(10) gives %(10) %<cr>%<tab>next line' expandPlaceholders:$% with:#(123) on:s. |
|
7687 |
s cr. |
|
7688 |
'%%test gives %test' expandPlaceholders:$% with:#(123) on:s. |
|
7689 |
s cr. |
|
7690 |
'|%%<tab>|%%1|%%<cr>| gives |%<tab>|%1|%<cr>|' expandPlaceholders:$% with:#(foo) on:s. |
|
7691 |
] |
|
7692 |
" |
|
7693 |
||
7694 |
"without xlation dictionary: |
|
7695 |
'hello %1' expandPlaceholdersWith:nil. |
|
7696 |
'hello%<cr> %1' expandPlaceholdersWith:nil. |
|
7697 |
" |
|
7698 |
||
7699 |
" |
|
7700 |
|dict| |
|
7701 |
||
7702 |
dict := Dictionary new. |
|
7703 |
dict at:1 put:'one'. |
|
7704 |
dict at:$a put:'AAAAA'. |
|
7705 |
dict at:$b put:[ Time now ]. |
|
7706 |
String streamContents:[:s| |
|
7707 |
'hello $1 $a $b' expandPlaceholders:$$ with:dict on:s. |
|
7708 |
]. |
|
7709 |
" |
|
7710 |
||
7711 |
"using blocks: |
|
7712 |
|dict| |
|
7713 |
||
7714 |
dict := Dictionary new. |
|
7715 |
dict at:'time' put:[Time now printString]. |
|
7716 |
dict at:'date' put:[Date today printString]. |
|
7717 |
String streamContents:[:s| |
|
7718 |
'it is $(time) $(date)' expandPlaceholders:$$ with:dict on:s. |
|
7719 |
]. |
|
7720 |
" |
|
7721 |
||
7722 |
"Created: / 14-01-2019 / 17:43:03 / Claus Gittinger" |
|
24254 | 7723 |
"Modified: / 05-06-2019 / 17:05:47 / Claus Gittinger" |
23067 | 7724 |
! |
7725 |
||
7726 |
expandPlaceholders:escapeCharacter with:argArrayOrDictionary on:aStream |
|
7727 |
"this is the generic version of the old %-escaping method, allowing for an arbitrary |
|
7728 |
escape character to be used (typically $$ or $% are effectively used). |
|
7729 |
||
7730 |
Write the receiver to aStream, where all %i escapes are |
|
7731 |
replaced by corresponding arguments' printStrings from the argArrayOrDictionary. |
|
7732 |
I.e. 'hello %1; how is %2' expandPlaceholdersWith:#('world' 'this') results |
|
7733 |
in the new string 'hello world; how is this'. |
|
7734 |
||
7735 |
As an extension, the argument may also be a dictionary, providing values for symbolic keys. |
|
7736 |
In this case, %a .. %z and %(...) are also allowed. |
|
7737 |
(%1..%9 require a numeric key in the dictionary, however) |
|
7738 |
Also, the values in argArrayOrDictionary may be blocks. |
|
7739 |
||
7740 |
To get a '%' character, use a '%%'-escape. |
|
7741 |
To get an integer-indexed placeHolder followed by another digit, |
|
7742 |
or an index > 9, you must use %(digit). |
|
7743 |
||
7744 |
See also bindWith:... for VisualAge compatibility. |
|
7745 |
Use %<cr> to insert a CR and %<tab> to insert a TAB." |
|
7746 |
||
7747 |
^ self |
|
7748 |
expandPlaceholders:escapeCharacter |
|
7749 |
with:argArrayOrDictionary |
|
7750 |
ignoreNumericEscapes:false |
|
23603 | 7751 |
requireParentheses:true |
23067 | 7752 |
on:aStream |
7753 |
||
7754 |
" |
|
23603 | 7755 |
'hello %1' expandPlaceholders:$% with:#('world') on:Transcript. |
7756 |
||
23067 | 7757 |
String streamContents:[:s| |
7758 |
'hello %1' expandPlaceholders:$% with:#('world') on:s. |
|
7759 |
s cr. |
|
7760 |
'hello $1; how is $2' expandPlaceholders:$$ with:#('world' 'this') on:s. |
|
7761 |
s cr. |
|
7762 |
'hello %2; how is %1' expandPlaceholders:$% with:#('world' 'this') on:s. |
|
7763 |
s cr. |
|
7764 |
'%1 plus %2 gives %3 ' expandPlaceholders:$% with:#(4 5 9) on:s. |
|
7765 |
s cr. |
|
7766 |
'%%(1)0 gives %(1)0' expandPlaceholders:$% with:#(123) on:s. |
|
7767 |
s cr. |
|
7768 |
'%%10 gives %10' expandPlaceholders:$% with:#(123) on:s. |
|
7769 |
s cr. |
|
7770 |
'%%(10) gives %(10) %<cr>%<tab>next line' expandPlaceholders:$% with:#(123) on:s. |
|
7771 |
s cr. |
|
7772 |
'%%test gives %test' expandPlaceholders:$% with:#(123) on:s. |
|
7773 |
s cr. |
|
7774 |
'|%%<tab>|%%1|%%<cr>| gives |%<tab>|%1|%<cr>|' expandPlaceholders:$% with:#(foo) on:s. |
|
7775 |
] |
|
7776 |
" |
|
7777 |
||
7778 |
" |
|
7779 |
|dict| |
|
7780 |
||
7781 |
dict := Dictionary new. |
|
7782 |
dict at:1 put:'one'. |
|
7783 |
dict at:$a put:'AAAAA'. |
|
7784 |
dict at:$b put:[ Time now ]. |
|
7785 |
String streamContents:[:s| |
|
7786 |
'hello $1 $a $b' expandPlaceholders:$$ with:dict on:s. |
|
7787 |
]. |
|
7788 |
" |
|
7789 |
||
7790 |
"using blocks: |
|
7791 |
|dict| |
|
7792 |
||
7793 |
dict := Dictionary new. |
|
7794 |
dict at:'time' put:[Time now printString]. |
|
7795 |
dict at:'date' put:[Date today printString]. |
|
7796 |
String streamContents:[:s| |
|
7797 |
'it is $(time) $(date)' expandPlaceholders:$$ with:dict on:s. |
|
7798 |
]. |
|
7799 |
" |
|
7800 |
||
14765 | 7801 |
"Modified: / 18-11-2010 / 15:43:28 / cg" |
23603 | 7802 |
"Modified (comment): / 14-01-2019 / 18:02:59 / Claus Gittinger" |
14765 | 7803 |
! |
7804 |
||
9192 | 7805 |
expandPlaceholdersWith:argArrayOrDictionary |
608 | 7806 |
"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
|
7807 |
replaced by corresponding arguments' printStrings from the argArrayOrDictionary. |
608 | 7808 |
I.e. 'hello %1; how is %2' expandPlaceholdersWith:#('world' 'this') results |
7809 |
in the new string 'hello world; how is this'. |
|
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
7810 |
|
4410
2bdc0c89a42e
enhanced #exapndPlaceHolders, to allow for a dictionary arg
Claus Gittinger <cg@exept.de>
parents:
4409
diff
changeset
|
7811 |
As an extension, the argument may also be a dictionary, providing |
7313 | 7812 |
values for symbolic keys. |
4842 | 7813 |
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
|
7814 |
(%1..%9 require a numeric key in the dictionary, however) |
18425 | 7815 |
Also, the values in argArrayOrDictionary may be blocks. |
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
7816 |
|
608 | 7817 |
To get a '%' character, use a '%%'-escape. |
7313 | 7818 |
To get an integer-indexed placeHolder followed by another digit, |
7819 |
or an index > 9, you must use %(digit). |
|
608 | 7820 |
See also bindWith:... for VisualAge compatibility." |
7821 |
||
10906
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
7822 |
|stream| |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
7823 |
|
18269 | 7824 |
stream := (TextStream ? CharacterWriteStream) on:(self species uninitializedNew:self size + 20). |
23209 | 7825 |
"/ self expandPlaceholdersWith:argArrayOrDictionary on:stream. |
7826 |
self expandPlaceholders:$% with:argArrayOrDictionary ignoreNumericEscapes:false on:stream. |
|
10906
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
7827 |
^ stream contents. |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
7828 |
|
608 | 7829 |
" |
8874 | 7830 |
'hello %1' expandPlaceholdersWith:#('world') |
7831 |
'hello %1; how is %2' expandPlaceholdersWith:#('world' 'this') |
|
7832 |
'hello %2; how is %1' expandPlaceholdersWith:#('world' 'this') |
|
7833 |
'%1 plus %2 gives %3 ' expandPlaceholdersWith:#(4 5 9) |
|
7834 |
'%%(1)0 gives %(1)0' expandPlaceholdersWith:#(123) |
|
7835 |
'%%10 gives %10' expandPlaceholdersWith:#(123) |
|
7836 |
'%%(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
|
7837 |
'%test gives %1' expandPlaceholdersWith:#(123) |
11028
cb1ee3ddd91a
expandPlaceHolders: care fr Text with emphasis
Claus Gittinger <cg@exept.de>
parents:
11018
diff
changeset
|
7838 |
'bla %1 bla' expandPlaceholdersWith:{ 'hello' allBold } |
cb1ee3ddd91a
expandPlaceHolders: care fr Text with emphasis
Claus Gittinger <cg@exept.de>
parents:
11018
diff
changeset
|
7839 |
'bla %1 bla' expandPlaceholdersWith:{ 'hello' } |
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
7840 |
('bla %1 bla' withColor:Color red) |
20874 | 7841 |
expandPlaceholdersWith:{ 'hello' } |
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
7842 |
('bla %1 bla' withColor:Color red) |
20874 | 7843 |
expandPlaceholdersWith:{ 'hello' withColor:Color blue } |
608 | 7844 |
" |
1026
455c9f609f4d
care for 16bit strings in expandPlaceHolders
Claus Gittinger <cg@exept.de>
parents:
1025
diff
changeset
|
7845 |
|
4410
2bdc0c89a42e
enhanced #exapndPlaceHolders, to allow for a dictionary arg
Claus Gittinger <cg@exept.de>
parents:
4409
diff
changeset
|
7846 |
" |
2bdc0c89a42e
enhanced #exapndPlaceHolders, to allow for a dictionary arg
Claus Gittinger <cg@exept.de>
parents:
4409
diff
changeset
|
7847 |
|dict| |
2bdc0c89a42e
enhanced #exapndPlaceHolders, to allow for a dictionary arg
Claus Gittinger <cg@exept.de>
parents:
4409
diff
changeset
|
7848 |
|
2bdc0c89a42e
enhanced #exapndPlaceHolders, to allow for a dictionary arg
Claus Gittinger <cg@exept.de>
parents:
4409
diff
changeset
|
7849 |
dict := Dictionary new. |
2bdc0c89a42e
enhanced #exapndPlaceHolders, to allow for a dictionary arg
Claus Gittinger <cg@exept.de>
parents:
4409
diff
changeset
|
7850 |
dict at:1 put:'one'. |
2bdc0c89a42e
enhanced #exapndPlaceHolders, to allow for a dictionary arg
Claus Gittinger <cg@exept.de>
parents:
4409
diff
changeset
|
7851 |
dict at:$a put:'AAAAA'. |
8794
a5df33935142
nindWith supports blocks in the value dictionary
Claus Gittinger <cg@exept.de>
parents:
8710
diff
changeset
|
7852 |
dict at:$b put:[ Time now ]. |
12316
3e78905d70a9
comment/format in: #expandPlaceholdersWith:
Claus Gittinger <cg@exept.de>
parents:
12196
diff
changeset
|
7853 |
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
|
7854 |
'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
|
7855 |
" |
2bdc0c89a42e
enhanced #exapndPlaceHolders, to allow for a dictionary arg
Claus Gittinger <cg@exept.de>
parents:
4409
diff
changeset
|
7856 |
|
23209 | 7857 |
"Modified: / 01-07-1997 / 00:53:24 / cg" |
7858 |
"Modified: / 14-07-2018 / 09:23:31 / Claus Gittinger" |
|
9192 | 7859 |
! |
7860 |
||
10906
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
7861 |
expandPlaceholdersWith:argArrayOrDictionary on:aStream |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
7862 |
"write the receiver to aStream, where all %i escapes are |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
7863 |
replaced by corresponding arguments' printStrings from the argArrayOrDictionary. |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
7864 |
I.e. 'hello %1; how is %2' expandPlaceholdersWith:#('world' 'this') results |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
7865 |
in the new string 'hello world; how is this'. |
18425 | 7866 |
|
10906
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
7867 |
As an extension, the argument may also be a dictionary, providing |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
7868 |
values for symbolic keys. |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
7869 |
In this case, %a .. %z and %(...) are also allowed. |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
7870 |
(%1..%9 require a numeric key in the dictionary, however) |
18425 | 7871 |
Also, the values in argArrayOrDictionary may be blocks. |
7872 |
||
10906
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
7873 |
To get a '%' character, use a '%%'-escape. |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
7874 |
To get an integer-indexed placeHolder followed by another digit, |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
7875 |
or an index > 9, you must use %(digit). |
18425 | 7876 |
|
10935 | 7877 |
See also bindWith:... for VisualAge compatibility. |
10939
d0ca31c1d1e9
#expandPlaceHoldersWith: handle %<cr> %<tab> etc
Stefan Vogel <sv@exept.de>
parents:
10935
diff
changeset
|
7878 |
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
|
7879 |
|
23067 | 7880 |
^ self expandPlaceholders:$% with:argArrayOrDictionary ignoreNumericEscapes:false on:aStream |
10906
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
7881 |
|
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
7882 |
" |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
7883 |
String streamContents:[:s| |
20874 | 7884 |
'hello %1' expandPlaceholdersWith:#('world') on:s. |
7885 |
s cr. |
|
7886 |
'hello %1; how is %2' expandPlaceholdersWith:#('world' 'this') on:s. |
|
7887 |
s cr. |
|
7888 |
'hello %2; how is %1' expandPlaceholdersWith:#('world' 'this') on:s. |
|
7889 |
s cr. |
|
7890 |
'%1 plus %2 gives %3 ' expandPlaceholdersWith:#(4 5 9) on:s. |
|
7891 |
s cr. |
|
7892 |
'%%(1)0 gives %(1)0' expandPlaceholdersWith:#(123) on:s. |
|
7893 |
s cr. |
|
7894 |
'%%10 gives %10' expandPlaceholdersWith:#(123) on:s. |
|
7895 |
s cr. |
|
7896 |
'%%(10) gives %(10) %<cr>%<tab>next line' expandPlaceholdersWith:#(123) on:s. |
|
7897 |
s cr. |
|
7898 |
'%test gives %1' expandPlaceholdersWith:#(123) on:s. |
|
10906
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
7899 |
] |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
7900 |
" |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
7901 |
|
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
7902 |
" |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
7903 |
|dict| |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
7904 |
|
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
7905 |
dict := Dictionary new. |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
7906 |
dict at:1 put:'one'. |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
7907 |
dict at:$a put:'AAAAA'. |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
7908 |
dict at:$b put:[ Time now ]. |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
7909 |
String streamContents:[:s| |
20874 | 7910 |
'hello %1 %a %b' expandPlaceholdersWith:dict on:s. |
10906
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
7911 |
]. |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
7912 |
" |
13008
a27647c36b31
changed: #expandPlaceholdersWith:on:
Claus Gittinger <cg@exept.de>
parents:
12925
diff
changeset
|
7913 |
|
13138
d2eb0eba5907
changed: #expandPlaceholdersWith:on:
Claus Gittinger <cg@exept.de>
parents:
13137
diff
changeset
|
7914 |
"Modified: / 18-11-2010 / 15:43:28 / cg" |
23067 | 7915 |
"Modified: / 14-06-2018 / 11:45:36 / Claus Gittinger" |
10906
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
7916 |
! |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
7917 |
|
22929 | 7918 |
extractPlaceHolders:escapeCharacter |
7919 |
"returns a list of placeholder keys of the form %i, |
|
7920 |
where i is either a single digit (as in %1, %2) |
|
7921 |
or a single letter (as in %a, %z) |
|
7922 |
or a word-key, as in %(one), %(fooBar). |
|
7923 |
For numeric keys, the returned collection contains integers; |
|
7924 |
for non-numeric ones, it includes strings." |
|
7925 |
||
7926 |
|setOfKeys loggingDictionary| |
|
7927 |
||
7928 |
setOfKeys := Set new. |
|
7929 |
loggingDictionary := Plug new. |
|
7930 |
loggingDictionary respondTo:#at: with:[:key | setOfKeys add:key. key asString]. |
|
7931 |
loggingDictionary respondTo:#at:ifAbsent: with:[:key :def | setOfKeys add:key. key asString]. |
|
7932 |
loggingDictionary respondTo:#includesKey: with:[:key | setOfKeys add:key. true]. |
|
7933 |
loggingDictionary respondTo:#isSequenceable with:[false]. |
|
7934 |
self expandPlaceholders:escapeCharacter with:loggingDictionary. |
|
7935 |
^ setOfKeys |
|
7936 |
||
7937 |
" |
|
7938 |
self assert:('hello %1 and %2' extractPlaceHolders:$%) asSet = #(1 2) asSet |
|
7939 |
self assert:('hello %9 and %(10)' extractPlaceHolders:$%) asSet = #(9 10) asSet |
|
7940 |
self assert:('hello %a and %(foo) and %1' extractPlaceHolders:$%) asSet = #(1 'a' 'foo') asSet |
|
7941 |
self assert:('hello %a and %(foo) and %1' extractPlaceHolders:$$) asSet = #() asSet |
|
7942 |
" |
|
7943 |
! |
|
7944 |
||
9230 | 7945 |
firstLine |
14405
1d0d99a95bba
comment/format in:7 methods
Claus Gittinger <cg@exept.de>
parents:
14168
diff
changeset
|
7946 |
"return the first line of a multiline string" |
1d0d99a95bba
comment/format in:7 methods
Claus Gittinger <cg@exept.de>
parents:
14168
diff
changeset
|
7947 |
|
1d0d99a95bba
comment/format in:7 methods
Claus Gittinger <cg@exept.de>
parents:
14168
diff
changeset
|
7948 |
^ self asCollectionOfSubCollectionsSeparatedBy:(Character cr) do:[:line | ^ line]. |
9230 | 7949 |
|
7950 |
" |
|
10703
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
7951 |
'hello' firstLine |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
7952 |
'1\2\3' withCRs firstLine |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
7953 |
'\1\2\3' withCRs firstLine |
9230 | 7954 |
" |
7955 |
! |
|
7956 |
||
13987 | 7957 |
tokensBasedOn:aCharacter |
7958 |
"this is an ST-80 alias for the ST/X method |
|
20874 | 7959 |
asCollectionOfSubstringsSeparatedBy:" |
13987 | 7960 |
|
7961 |
^ self asCollectionOfSubstringsSeparatedBy:aCharacter |
|
7962 |
||
7963 |
" |
|
7964 |
'hello:world:isnt:this nice' tokensBasedOn:$: |
|
7965 |
'foo,bar,baz' tokensBasedOn:$, |
|
7966 |
'/etc/passwd' asFilename readStream nextLine tokensBasedOn:$: |
|
7967 |
" |
|
7968 |
! |
|
7969 |
||
7970 |
withCEscapes |
|
16863 | 7971 |
"return a new string consisting of receiver's characters |
13987 | 7972 |
with all special and unprintable characters replaced by \X-character escapes. |
7973 |
(similar to the way C-language literal Strings are represented). |
|
7974 |
The resulting string will contain only 7-bit ascii characters. |
|
7975 |
Emphasis is not supported. |
|
7976 |
The following escapes are generated: |
|
20874 | 7977 |
\' single quote character |
7978 |
\dQuote double quote character |
|
7979 |
\r return character |
|
7980 |
\r return character |
|
7981 |
\n newline character |
|
7982 |
\t tab character |
|
7983 |
\\ the \ character itself |
|
7984 |
\xnn two digit hex number defining the characters ascii value |
|
7985 |
\unnnn four digit hex number defining the characters ascii value |
|
7986 |
\Unnnnnnnn eight digit hex number defining the characters ascii value |
|
16863 | 7987 |
This is the opposite of withoutCEscapes. |
17000 | 7988 |
|
7989 |
Sigh: this is named completely wrong (opposite naming of withCRs/witoutCRs), |
|
20874 | 7990 |
but it cannot be changed easily, as these methods are already used heavily |
13987 | 7991 |
" |
7992 |
||
7993 |
|anyEscapeNeeded out seq| |
|
7994 |
||
7995 |
" |
|
7996 |
first, check if any escape is needed and return the receiver unchanged if not |
|
7997 |
" |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
7998 |
anyEscapeNeeded := self |
20874 | 7999 |
contains:[:ch | |
8000 |
((ch codePoint between:32 and:126) not |
|
8001 |
or:[ch == $' or:[ch == $"]]) |
|
8002 |
]. |
|
13987 | 8003 |
anyEscapeNeeded ifFalse:[ ^ self ]. |
8004 |
||
8005 |
self hasChangeOfEmphasis ifTrue:[ self error:'emphasis not supported' ]. |
|
8006 |
||
18269 | 8007 |
out := WriteStream on:(String uninitializedNew:self size-1). |
13987 | 8008 |
|
8009 |
self do:[:ch | |
|
20874 | 8010 |
|cp| |
8011 |
||
8012 |
(ch == $' or:[ch == $"]) ifTrue:[ |
|
8013 |
out nextPut:$\. |
|
8014 |
out nextPut:ch. |
|
8015 |
] ifFalse:[ |
|
8016 |
(ch codePoint between:32 and:126) ifTrue:[ |
|
8017 |
out nextPut:ch |
|
8018 |
] ifFalse:[ |
|
8019 |
ch == Character return ifTrue:[ |
|
8020 |
seq := '\r' |
|
8021 |
] ifFalse:[ ch == Character nl ifTrue:[ |
|
8022 |
seq := '\n' |
|
8023 |
] ifFalse:[ ch == Character tab ifTrue:[ |
|
8024 |
seq := '\t' |
|
8025 |
] ifFalse:[ ch == $\ ifTrue:[ |
|
8026 |
seq := '\\' |
|
8027 |
] ifFalse:[ |
|
8028 |
cp := ch codePoint. |
|
8029 |
cp <= 16rFF ifTrue:[ |
|
22344 | 8030 |
seq := '\x' , (cp printStringRadix:16 size:2 fill:$0) |
20874 | 8031 |
] ifFalse:[ |
8032 |
cp <= 16rFFFF ifTrue:[ |
|
22344 | 8033 |
seq := '\u' , (cp printStringRadix:16 size:4 fill:$0) |
20874 | 8034 |
] ifFalse:[ |
22344 | 8035 |
seq := '\U',(cp printStringRadix:16 size:8 fill:$0) |
20874 | 8036 |
] |
8037 |
] |
|
8038 |
]]]]. |
|
8039 |
out nextPutAll:seq |
|
8040 |
]. |
|
8041 |
]. |
|
13987 | 8042 |
]. |
8043 |
^ out contents |
|
8044 |
||
8045 |
" |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
8046 |
'hello\n\tworld' withoutCEscapes. |
17000 | 8047 |
'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
|
8048 |
('hello ',(Character value:16r1234),' world') withCEscapes |
13987 | 8049 |
" |
8050 |
||
8051 |
"Created: / 25-01-2012 / 11:08:16 / cg" |
|
22344 | 8052 |
"Modified: / 12-11-2017 / 12:39:57 / cg" |
13987 | 8053 |
! |
8054 |
||
9192 | 8055 |
withCRs |
17205 | 8056 |
"return a new string consisting of the receiver's characters |
16757 | 8057 |
with all \-characters replaced by cr-characters. |
8058 |
If there are no backslashes, return the original" |
|
8059 |
||
16759 | 8060 |
^ self copyReplaceAll:$\ with:(Character cr) ifNone:self |
9192 | 8061 |
|
8062 |
" |
|
8063 |
'hello\world' withCRs |
|
8064 |
" |
|
8065 |
||
8066 |
"Modified: / 18.7.1998 / 22:53:02 / cg" |
|
8067 |
! |
|
8068 |
||
8069 |
withEscapes |
|
15405 | 8070 |
<resource: #obsolete> |
13987 | 8071 |
"has been renamed; the name withEscapes is misleading" |
8072 |
||
8073 |
self obsoleteMethodWarning:'use withoutCEscapes'. |
|
8074 |
^ self withoutCEscapes. |
|
8075 |
||
8076 |
"Modified: / 25-01-2012 / 10:42:30 / cg" |
|
9192 | 8077 |
! |
8078 |
||
8079 |
withMatchEscapes |
|
4117 | 8080 |
"return a copy of the receiver with all match characters escaped |
8081 |
by $\ characters (to be usable as a match string). |
|
8082 |
Return the receiver, if there are none." |
|
8083 |
||
5342
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
8084 |
|in out c escape| |
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
8085 |
|
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
8086 |
escape := self class matchEscapeCharacter. |
4117 | 8087 |
|
8088 |
in := self readStream. |
|
7986 | 8089 |
out := WriteStream on:(self species new:self size). |
4117 | 8090 |
[in atEnd] whileFalse:[ |
20874 | 8091 |
c := in next. |
8092 |
(c == escape or:['*[#' includes:c]) ifTrue:[ |
|
8093 |
out nextPut:$\. |
|
8094 |
]. |
|
8095 |
out nextPut:c. |
|
4117 | 8096 |
]. |
8097 |
^ out contents. |
|
8098 |
||
8099 |
" |
|
8874 | 8100 |
'*foo' withMatchEscapes |
8101 |
'\*foo' withMatchEscapes |
|
8102 |
'*foo' withMatchEscapes |
|
8103 |
'\\*foo' withMatchEscapes |
|
8104 |
'foo*' withMatchEscapes |
|
8105 |
'foo\*' withMatchEscapes |
|
8106 |
'foo\' withMatchEscapes |
|
8107 |
'f*o*o' withMatchEscapes |
|
4117 | 8108 |
" |
8109 |
||
8110 |
"Modified: 2.4.1997 / 18:13:04 / cg" |
|
9192 | 8111 |
! |
8112 |
||
14992 | 8113 |
withSeparatorsCompacted |
8114 |
"return a new string with each sequence of whiteSpace replaced by a single space character. |
|
8115 |
Preserves a leading/trailing space." |
|
8116 |
||
8117 |
^ self species streamContents:[:s | |
|
20874 | 8118 |
|skipping| |
8119 |
||
8120 |
skipping := false. |
|
8121 |
1 to:self size do:[:idx | |
|
8122 |
|char| |
|
8123 |
||
8124 |
char := self at:idx. |
|
8125 |
char isSeparator ifFalse:[ |
|
8126 |
s nextPut:char. |
|
8127 |
skipping := false. |
|
8128 |
] ifTrue:[ |
|
8129 |
skipping ifFalse:[ |
|
8130 |
s nextPut:(Character space). |
|
8131 |
skipping := true |
|
8132 |
]. |
|
8133 |
] |
|
8134 |
] |
|
14992 | 8135 |
] |
8136 |
||
8137 |
" |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
8138 |
'hello wwww' withSeparatorsCompacted |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
8139 |
'hello wwww' withSeparatorsCompacted |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
8140 |
' hello wwww' withSeparatorsCompacted |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
8141 |
' hello wwww ' withSeparatorsCompacted |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
8142 |
' hello wwww ' withSeparatorsCompacted |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
8143 |
'hel lo www w' withSeparatorsCompacted |
14992 | 8144 |
" |
8145 |
! |
|
8146 |
||
18464 | 8147 |
withSeparatorsReplacedBy:replacementCharacter |
8148 |
"return a new string with each separator (whitespace) replaced by replacementCharacter. |
|
8149 |
Typically used with space as replacementCharacter" |
|
8150 |
||
8151 |
^ self species streamContents:[:s | |
|
20874 | 8152 |
self do:[:ch | |
8153 |
ch isSeparator ifTrue:[ |
|
8154 |
s nextPut:replacementCharacter |
|
8155 |
] ifFalse:[ |
|
8156 |
s nextPut:ch. |
|
8157 |
] |
|
8158 |
] |
|
18464 | 8159 |
] |
8160 |
||
8161 |
" |
|
8162 |
'hello wwww' withSeparatorsReplacedBy:$* |
|
8163 |
'hello ww ww' withSeparatorsReplacedBy:$* |
|
8164 |
' hello wwww' withSeparatorsReplacedBy:$* |
|
8165 |
'hel lo www w' withSeparatorsReplacedBy:$* |
|
8166 |
'hel |
|
8167 |
lo www |
|
8168 |
w' withSeparatorsReplacedBy:$* |
|
8169 |
" |
|
8170 |
! |
|
8171 |
||
9192 | 8172 |
withTabs |
19464 | 8173 |
"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
|
8174 |
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
|
8175 |
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
|
8176 |
otherwise a new string is returned. |
8874 | 8177 |
Limitation: only the very first spaces are replaced |
20874 | 8178 |
(i.e. if the receiver contains newLine characters, |
8179 |
no tabs are inserted after those lineBreaks)" |
|
8874 | 8180 |
|
8181 |
|idx "{ SmallInteger }" |
|
8182 |
nTabs "{ SmallInteger }" |
|
608 | 8183 |
newString| |
8184 |
||
8185 |
idx := self findFirst:[:c | (c ~~ Character space)]. |
|
8186 |
nTabs := (idx-1) // 8. |
|
5815
4acdd87a77d4
// can return negative numbers.
Stefan Vogel <sv@exept.de>
parents:
5795
diff
changeset
|
8187 |
nTabs <= 0 ifTrue:[^ self]. |
608 | 8188 |
|
8189 |
"any tabs" |
|
15523 | 8190 |
newString := self species new:(self size - (nTabs * 7)). |
608 | 8191 |
newString atAll:(1 to:nTabs) put:(Character tab). |
8192 |
newString replaceFrom:(nTabs + 1) with:self startingAt:(nTabs * 8 + 1). |
|
8193 |
^ newString |
|
8194 |
||
8195 |
" |
|
8874 | 8196 |
'12345678901234567890' withTabs |
8197 |
' 8901234567890' withTabs |
|
8198 |
' 901234567890' withTabs |
|
608 | 8199 |
' 67890' withTabs |
8200 |
' 7890' withTabs |
|
8201 |
' 890' withTabs |
|
8202 |
" |
|
9192 | 8203 |
! |
8204 |
||
8205 |
withTabsExpanded |
|
19464 | 8206 |
"return a string consisting of the receiver's characters, |
8874 | 8207 |
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
|
8208 |
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
|
8209 |
otherwise a new string is returned. |
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
678
diff
changeset
|
8210 |
This does handle multiline strings." |
608 | 8211 |
|
5741
3a6086774d38
added #withTabsExpanded: (i.e. arg to specify tab-column width)
Claus Gittinger <cg@exept.de>
parents:
5739
diff
changeset
|
8212 |
^ self withTabsExpanded:8 |
3a6086774d38
added #withTabsExpanded: (i.e. arg to specify tab-column width)
Claus Gittinger <cg@exept.de>
parents:
5739
diff
changeset
|
8213 |
|
3a6086774d38
added #withTabsExpanded: (i.e. arg to specify tab-column width)
Claus Gittinger <cg@exept.de>
parents:
5739
diff
changeset
|
8214 |
" |
8874 | 8215 |
('1' , Character tab asString , 'x') withTabsExpanded |
8216 |
('12345' , Character tab asString , 'x') withTabsExpanded |
|
8217 |
('123456' , Character tab asString , 'x') withTabsExpanded |
|
8218 |
('1234567' , Character tab asString , 'x') withTabsExpanded |
|
8219 |
('12345678' , Character tab asString , 'x') withTabsExpanded |
|
8220 |
('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
|
8221 |
|
3a6086774d38
added #withTabsExpanded: (i.e. arg to specify tab-column width)
Claus Gittinger <cg@exept.de>
parents:
5739
diff
changeset
|
8222 |
(String with:Character tab |
20874 | 8223 |
with:Character tab |
8224 |
with:$1) withTabsExpanded |
|
5741
3a6086774d38
added #withTabsExpanded: (i.e. arg to specify tab-column width)
Claus Gittinger <cg@exept.de>
parents:
5739
diff
changeset
|
8225 |
|
3a6086774d38
added #withTabsExpanded: (i.e. arg to specify tab-column width)
Claus Gittinger <cg@exept.de>
parents:
5739
diff
changeset
|
8226 |
(String with:Character tab |
20874 | 8227 |
with:$1 |
8228 |
with:Character tab |
|
8229 |
with:$2) withTabsExpanded |
|
5741
3a6086774d38
added #withTabsExpanded: (i.e. arg to specify tab-column width)
Claus Gittinger <cg@exept.de>
parents:
5739
diff
changeset
|
8230 |
|
3a6086774d38
added #withTabsExpanded: (i.e. arg to specify tab-column width)
Claus Gittinger <cg@exept.de>
parents:
5739
diff
changeset
|
8231 |
(String with:Character tab |
20874 | 8232 |
with:$1 |
8233 |
with:Character cr |
|
8234 |
with:Character tab |
|
8235 |
with:$2) withTabsExpanded |
|
5741
3a6086774d38
added #withTabsExpanded: (i.e. arg to specify tab-column width)
Claus Gittinger <cg@exept.de>
parents:
5739
diff
changeset
|
8236 |
" |
3a6086774d38
added #withTabsExpanded: (i.e. arg to specify tab-column width)
Claus Gittinger <cg@exept.de>
parents:
5739
diff
changeset
|
8237 |
|
3a6086774d38
added #withTabsExpanded: (i.e. arg to specify tab-column width)
Claus Gittinger <cg@exept.de>
parents:
5739
diff
changeset
|
8238 |
"Modified: 12.5.1996 / 13:05:10 / cg" |
9192 | 8239 |
! |
8240 |
||
8241 |
withTabsExpanded:numSpaces |
|
19464 | 8242 |
"return a string consisting of the receiver's characters, |
8874 | 8243 |
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
|
8244 |
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
|
8245 |
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
|
8246 |
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
|
8247 |
|
8874 | 8248 |
|col "{ SmallInteger }" |
1381 | 8249 |
str ch |
8250 |
dstIdx "{ SmallInteger }" |
|
8251 |
newSz "{ SmallInteger }" |
|
8252 |
sz "{ SmallInteger }" |
|
8253 |
hasEmphasis e| |
|
608 | 8254 |
|
8255 |
(self includes:(Character tab)) ifFalse:[^ self]. |
|
1381 | 8256 |
|
8257 |
sz := self size. |
|
8258 |
||
8259 |
"/ count the new size first, instead of |
|
8260 |
"/ multiple resizing (better for large strings) |
|
8261 |
||
8262 |
col := 1. newSz := 0. |
|
8263 |
1 to:sz do:[:srcIdx | |
|
20874 | 8264 |
ch := self at:srcIdx. |
8265 |
ch == Character tab ifFalse:[ |
|
8266 |
col := col + 1. |
|
8267 |
newSz := newSz + 1. |
|
8268 |
ch == Character cr ifTrue:[ |
|
8269 |
col := 1 |
|
8270 |
]. |
|
8271 |
] ifTrue:[ |
|
8272 |
(col \\ numSpaces) to:numSpaces do:[:ii | |
|
8273 |
newSz := newSz + 1. |
|
8274 |
col := col + 1 |
|
8275 |
]. |
|
8276 |
] |
|
608 | 8277 |
]. |
1381 | 8278 |
|
8279 |
str := self species new:newSz. |
|
23428 | 8280 |
(str isText and:[self bitsPerCharacter > 8]) ifTrue:[ |
8281 |
str := Text string:(Unicode16String new:newSz) |
|
8282 |
]. |
|
8283 |
||
1381 | 8284 |
hasEmphasis := self hasChangeOfEmphasis. |
8285 |
||
8286 |
col := 1. dstIdx := 1. |
|
8287 |
1 to:sz do:[:srcIdx | |
|
20874 | 8288 |
ch := self at:srcIdx. |
8289 |
||
8290 |
ch == Character tab ifFalse:[ |
|
8291 |
col := col + 1. |
|
8292 |
ch == Character cr ifTrue:[ |
|
8293 |
col := 1 |
|
8294 |
]. |
|
8295 |
hasEmphasis ifTrue:[ |
|
8296 |
e := self emphasisAt:srcIdx. |
|
8297 |
str emphasisAt:dstIdx put:e |
|
8298 |
]. |
|
8299 |
str at:dstIdx put:ch. |
|
8300 |
dstIdx := dstIdx + 1 |
|
8301 |
] ifTrue:[ |
|
8302 |
(col \\ numSpaces) to:numSpaces do:[:ii | |
|
8303 |
str at:dstIdx put:Character space. |
|
8304 |
dstIdx := dstIdx + 1. |
|
8305 |
col := col + 1 |
|
8306 |
]. |
|
8307 |
] |
|
1381 | 8308 |
]. |
8309 |
^ str |
|
608 | 8310 |
|
8311 |
" |
|
8874 | 8312 |
('1' , Character tab asString , 'x') withTabsExpanded |
8313 |
('1' , Character tab asString , 'x') withTabsExpanded:4 |
|
8314 |
('12345' , Character tab asString , 'x') withTabsExpanded |
|
8315 |
('123456' , Character tab asString , 'x') withTabsExpanded |
|
8316 |
('1234567' , Character tab asString , 'x') withTabsExpanded |
|
8317 |
('12345678' , Character tab asString , 'x') withTabsExpanded |
|
8318 |
('123456789' , Character tab asString , 'x') withTabsExpanded |
|
735
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
678
diff
changeset
|
8319 |
|
608 | 8320 |
(String with:Character tab |
20874 | 8321 |
with:Character tab |
8322 |
with:$1) withTabsExpanded |
|
608 | 8323 |
|
8324 |
(String with:Character tab |
|
20874 | 8325 |
with:$1 |
8326 |
with:Character tab |
|
8327 |
with:$2) withTabsExpanded |
|
735
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
678
diff
changeset
|
8328 |
|
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
678
diff
changeset
|
8329 |
(String with:Character tab |
20874 | 8330 |
with:$1 |
8331 |
with:Character cr |
|
8332 |
with:Character tab |
|
8333 |
with:$2) withTabsExpanded |
|
608 | 8334 |
" |
735
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
678
diff
changeset
|
8335 |
|
1381 | 8336 |
"Modified: 12.5.1996 / 13:05:10 / cg" |
9192 | 8337 |
! |
8338 |
||
13987 | 8339 |
withoutAllSpaces |
16855 | 8340 |
"return a copy of the receiver with all whitespace removed" |
8341 |
||
13987 | 8342 |
^ self asCollectionOfWords asStringWith:''. |
8343 |
"/ |col string| |
|
8344 |
"/ |
|
8345 |
"/ col := self asCollectionOfWords. |
|
8346 |
"/ string := String new. |
|
8347 |
"/ col do:[:el | |
|
8348 |
"/ string := string,el |
|
8349 |
"/ ]. |
|
8350 |
"/ ^string |
|
8351 |
||
8352 |
" |
|
8353 |
'hello wwww' withoutAllSpaces |
|
8354 |
'hel lo www w' withoutAllSpaces |
|
8355 |
" |
|
8356 |
||
8357 |
"Modified: / 18.7.1998 / 22:53:08 / cg" |
|
8358 |
! |
|
8359 |
||
8360 |
withoutCEscapes |
|
15628 | 8361 |
"return a new string consisting of receiver's characters |
22205 | 8362 |
with all \X-character escapes replaced by corresponding characters. |
13987 | 8363 |
(similar to the way C-language Strings are converted). |
8364 |
The following escapes are supported: |
|
20874 | 8365 |
\r return character |
8366 |
\n newline character |
|
8367 |
\b backspace character |
|
8368 |
\f formfeed character |
|
8369 |
\t tab character |
|
8370 |
\e escape character |
|
8371 |
\\ the \ character itself |
|
8372 |
\nnn three digit octal number defining the characters ascii value |
|
8373 |
\xnn two digit hex number defining the characters ascii value |
|
8374 |
\unnnn four digit hex number defining the characters unicode value |
|
8375 |
\Unnnnnnnn eight digit hex number defining the characters unicode value |
|
8376 |
\other other |
|
13987 | 8377 |
|
8378 |
Notice, that \' is NOT a valid escape, since the general syntax of |
|
8379 |
string constants is not affected by this method. |
|
8380 |
||
8381 |
Although easily implementable, this is NOT done automatically |
|
8382 |
by the compiler (due to a lack of a language standard for this). |
|
22205 | 8383 |
However, the compiler may detect sends of #withEscapes to string literals |
13987 | 8384 |
and place a modified string constant into the binary/byte-code. |
8385 |
Therefore, no runtime penalty will be payed for using these escapes. |
|
8386 |
(not in pre 2.11 versions) |
|
16863 | 8387 |
|
8388 |
This is the opposite of withCEscapes. |
|
17000 | 8389 |
|
8390 |
Sigh: this is named completely wrong (opposite naming of withCRs/witoutCRs), |
|
20874 | 8391 |
but it cannot be changed easily, as these methods are already used heavily |
13987 | 8392 |
" |
8393 |
||
8394 |
|val "{ SmallInteger }" |
|
8395 |
in out nextChar nDigits| |
|
8396 |
||
8397 |
" |
|
8398 |
first, see if there is any escape; if not, return the receiver unchanged |
|
8399 |
" |
|
8400 |
(self includes:$\) ifFalse:[^ self ]. |
|
8401 |
||
8402 |
self hasChangeOfEmphasis ifTrue:[ self error:'emphasis not supported' ]. |
|
8403 |
out := CharacterWriteStream on:(String new:self size - 1). |
|
8404 |
||
8405 |
in := ReadStream on:self. |
|
8406 |
[in atEnd] whileFalse:[ |
|
20874 | 8407 |
nextChar := in next. |
8408 |
nextChar == $\ ifTrue:[ |
|
8409 |
in atEnd ifTrue:[ |
|
8410 |
] ifFalse:[ |
|
8411 |
nextChar := in next. |
|
8412 |
nextChar == $r ifTrue:[ |
|
8413 |
nextChar := Character return |
|
8414 |
] ifFalse:[ nextChar == $n ifTrue:[ |
|
8415 |
nextChar := Character nl |
|
8416 |
] ifFalse:[ nextChar == $b ifTrue:[ |
|
8417 |
nextChar := Character backspace |
|
8418 |
] ifFalse:[ nextChar == $f ifTrue:[ |
|
8419 |
nextChar := Character newPage |
|
8420 |
] ifFalse:[ nextChar == $t ifTrue:[ |
|
8421 |
nextChar := Character tab |
|
8422 |
] ifFalse:[ nextChar == $e ifTrue:[ |
|
8423 |
nextChar := Character esc |
|
8424 |
] ifFalse:[ |
|
8425 |
nextChar == $0 ifTrue:[ |
|
8426 |
val := 0. |
|
8427 |
nextChar := in peek. |
|
8428 |
nDigits := 1. |
|
8429 |
[nextChar notNil and:[nextChar isDigit and:[nDigits <= 3]]] whileTrue:[ |
|
8430 |
val := (val * 8) + nextChar digitValue. |
|
8431 |
nextChar := in nextPeek. |
|
8432 |
nDigits := nDigits + 1. |
|
8433 |
]. |
|
8434 |
nextChar := Character value:val. |
|
8435 |
] ifFalse:[ |
|
8436 |
val := 0. |
|
8437 |
nextChar == $x ifTrue:[ |
|
8438 |
2 timesRepeat:[ |
|
8439 |
nextChar := in next. |
|
8440 |
val := (val * 16) + nextChar digitValue. |
|
8441 |
]. |
|
8442 |
nextChar := Character value:val. |
|
8443 |
] ifFalse:[ |
|
8444 |
nextChar == $u ifTrue:[ |
|
8445 |
4 timesRepeat:[ |
|
8446 |
nextChar := in next. |
|
8447 |
val := (val * 16) + nextChar digitValue. |
|
8448 |
]. |
|
8449 |
nextChar := Character value:val. |
|
8450 |
] ifFalse:[ |
|
8451 |
nextChar == $U ifTrue:[ |
|
8452 |
8 timesRepeat:[ |
|
8453 |
nextChar := in next. |
|
8454 |
val := (val * 16) + nextChar digitValue. |
|
8455 |
]. |
|
8456 |
nextChar := Character value:val. |
|
8457 |
] |
|
8458 |
] |
|
8459 |
] |
|
8460 |
] |
|
8461 |
]]]]]]. |
|
8462 |
]. |
|
8463 |
]. |
|
8464 |
out nextPut:nextChar. |
|
13987 | 8465 |
]. |
8466 |
^ out contents |
|
8467 |
||
8468 |
" |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
8469 |
'hello world' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
8470 |
'hello\world' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
8471 |
'hello\world\' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
8472 |
'hello world\' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
8473 |
'hello\tworld' withoutCEscapes |
13987 | 8474 |
'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
|
8475 |
'hello\tworld\n' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
8476 |
'hello\010world' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
8477 |
'hello\r\nworld' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
8478 |
'hello\r\n\x08world' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
8479 |
'0\x080' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
8480 |
'0\u12340' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
8481 |
'0\U123456780' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
8482 |
'0\0a' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
8483 |
'0\00a' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
8484 |
'0\000a' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
8485 |
'0\0000a' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
8486 |
'0\00000a' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
8487 |
'0\03770' withoutCEscapes |
13987 | 8488 |
" |
8489 |
||
8490 |
"Created: / 25-01-2012 / 10:41:44 / cg" |
|
22205 | 8491 |
"Modified (comment): / 23-08-2017 / 11:07:43 / mawalch" |
13987 | 8492 |
! |
8493 |
||
9192 | 8494 |
withoutCRs |
19464 | 8495 |
"return a new collection consisting of receiver's elements |
9192 | 8496 |
with all cr-characters replaced by \-characters. |
8497 |
This is the reverse operation of withCRs." |
|
8498 |
||
16759 | 8499 |
^ self copyReplaceAll:(Character cr) with:$\ ifNone:self |
12319
18b80c2ce1e3
oops #withoutSeparators #withoutSpaces were noops
Claus Gittinger <cg@exept.de>
parents:
12317
diff
changeset
|
8500 |
|
9192 | 8501 |
" |
8502 |
'hello |
|
8503 |
world' withoutCRs |
|
8504 |
" |
|
8505 |
||
8506 |
"Modified: / 18.7.1998 / 22:53:08 / cg" |
|
8507 |
! |
|
8508 |
||
8509 |
withoutLeadingSeparators |
|
608 | 8510 |
"return a copy of myself without leading separators. |
8511 |
Notice: this does remove tabs, newline or any other whitespace. |
|
8512 |
Returns an empty string, if the receiver consist only of whitespace." |
|
8513 |
||
8514 |
|index| |
|
8515 |
||
8516 |
index := self indexOfNonSeparatorStartingAt:1. |
|
8517 |
index ~~ 0 ifTrue:[ |
|
20874 | 8518 |
index == 1 ifTrue:[ |
8519 |
^ self |
|
8520 |
]. |
|
8521 |
^ self copyFrom:index |
|
608 | 8522 |
]. |
8523 |
^ '' |
|
8524 |
||
8525 |
" |
|
8874 | 8526 |
' foo ' withoutLeadingSeparators |
8527 |
'foo ' withoutLeadingSeparators |
|
8528 |
' foo' withoutLeadingSeparators |
|
8529 |
' ' withoutLeadingSeparators |
|
8530 |
'foo' withoutLeadingSeparators |
|
8531 |
(' ' , Character tab asString , ' foo ') withoutLeadingSeparators inspect |
|
608 | 8532 |
" |
9192 | 8533 |
! |
8534 |
||
8535 |
withoutMatchEscapes |
|
4117 | 8536 |
"return a copy of the receiver with all $\ removed or |
8537 |
the receiver, if there are none." |
|
8538 |
||
5342
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
8539 |
|in out c escape| |
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
8540 |
|
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
8541 |
escape := self class matchEscapeCharacter. |
4117 | 8542 |
|
8543 |
in := self readStream. |
|
8222
f2c454a9a038
replaced '' writeStream by String writeStream
Claus Gittinger <cg@exept.de>
parents:
8124
diff
changeset
|
8544 |
out := self species writeStream. |
4117 | 8545 |
[in atEnd] whileFalse:[ |
20874 | 8546 |
c := in next. |
8547 |
c == escape ifTrue:[ |
|
8548 |
in atEnd ifFalse:[ |
|
8549 |
c := in next. |
|
8550 |
] |
|
8551 |
]. |
|
8552 |
out nextPut:c. |
|
4117 | 8553 |
]. |
8554 |
^ out contents. |
|
8555 |
||
8556 |
" |
|
8874 | 8557 |
'*foo' withoutMatchEscapes |
8558 |
'\*foo' withoutMatchEscapes |
|
8559 |
'*foo' withoutMatchEscapes |
|
8560 |
'\\*foo' withoutMatchEscapes |
|
8561 |
'foo*' withoutMatchEscapes |
|
8562 |
'foo\*' withoutMatchEscapes |
|
8563 |
'foo\' withoutMatchEscapes |
|
8564 |
'f\*o\*o' withoutMatchEscapes |
|
4117 | 8565 |
" |
8566 |
||
8567 |
"Modified: 30.6.1997 / 13:40:23 / cg" |
|
9192 | 8568 |
! |
8569 |
||
23249 | 8570 |
withoutPrefix:aStringOrCharacter |
19677 | 8571 |
"if the receiver startsWith aString, return a copy without it. |
9192 | 8572 |
Otherwise return the receiver" |
8573 |
||
23249 | 8574 |
(self startsWith:aStringOrCharacter) ifTrue:[ |
8575 |
aStringOrCharacter isCharacter ifTrue:[ |
|
8576 |
^ self copyFrom:2 |
|
8577 |
] ifFalse:[ |
|
8578 |
^ self copyFrom:aStringOrCharacter size+1 |
|
8579 |
]. |
|
9192 | 8580 |
]. |
8581 |
^ self |
|
8582 |
||
8583 |
" |
|
8584 |
'helloworld' withoutPrefix:'hello' |
|
8585 |
'helloworld' withoutPrefix:'foo' |
|
24142 | 8586 |
'helloworld' withoutPrefix:$h |
8587 |
'helloworld' withoutPrefix:#( $h ) |
|
9192 | 8588 |
" |
19677 | 8589 |
|
8590 |
"Modified: / 30-04-2016 / 10:01:00 / cg" |
|
23249 | 8591 |
"Modified: / 31-07-2018 / 17:05:36 / Claus Gittinger" |
24142 | 8592 |
"Modified (comment): / 24-05-2019 / 09:22:54 / Claus Gittinger" |
23249 | 8593 |
! |
8594 |
||
8595 |
withoutPrefix:aStringOrCharacter caseSensitive:caseSensitive |
|
23234 | 8596 |
"if the receiver startsWith aString, return a copy without it. |
8597 |
Otherwise return the receiver" |
|
8598 |
||
23249 | 8599 |
(self startsWith:aStringOrCharacter caseSensitive:caseSensitive) ifTrue:[ |
8600 |
aStringOrCharacter isCharacter ifTrue:[ |
|
8601 |
^ self copyFrom:2 |
|
8602 |
] ifFalse:[ |
|
8603 |
^ self copyFrom:aStringOrCharacter size+1 |
|
8604 |
]. |
|
23234 | 8605 |
]. |
8606 |
^ self |
|
8607 |
||
8608 |
" |
|
24142 | 8609 |
'Helloworld' withoutPrefix:'hello' caseSensitive:false |
8610 |
'Helloworld' withoutPrefix:'foo' caseSensitive:false |
|
8611 |
'Helloworld' withoutPrefix:$h caseSensitive:false |
|
8612 |
'Helloworld' withoutPrefix:#( $h ) caseSensitive:false |
|
23234 | 8613 |
" |
8614 |
||
8615 |
"Created: / 27-07-2018 / 08:30:03 / Claus Gittinger" |
|
23249 | 8616 |
"Modified: / 31-07-2018 / 17:05:53 / Claus Gittinger" |
24142 | 8617 |
"Modified (comment): / 24-05-2019 / 09:23:30 / Claus Gittinger" |
23234 | 8618 |
! |
8619 |
||
10671 | 8620 |
withoutQuotes |
17000 | 8621 |
"/ remove quotes ($" and $') from the front and end of myself (if matching)" |
8622 |
||
8623 |
|firstChar| |
|
8624 |
||
17024 | 8625 |
self isEmpty ifTrue:[^ self]. |
8626 |
||
17000 | 8627 |
firstChar := self first. |
8628 |
((firstChar == $") or:[firstChar == $']) ifFalse:[^ self]. |
|
8629 |
||
8630 |
self last == firstChar ifTrue:[ |
|
20874 | 8631 |
^ self copyFrom:2 to:(self size-1) |
10671 | 8632 |
]. |
17000 | 8633 |
^ self |
8634 |
||
8635 |
"/ |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
8636 |
"/ '"hello"' withoutQuotes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
8637 |
"/ '''hello''' withoutQuotes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
8638 |
"/ 'hello' withoutQuotes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
8639 |
"/ '"hello' withoutQuotes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
8640 |
"/ 'hello"' withoutQuotes |
17000 | 8641 |
"/ |
10671 | 8642 |
! |
8643 |
||
9192 | 8644 |
withoutSeparators |
608 | 8645 |
"return a copy of myself without leading and trailing whitespace. |
4156 | 8646 |
(but whiteSpace in-between is preserved) |
608 | 8647 |
Whitespace is space, tab, newline, formfeed. |
8648 |
Use withoutSpaces, if you want to remove spaces only." |
|
8649 |
||
12319
18b80c2ce1e3
oops #withoutSeparators #withoutSpaces were noops
Claus Gittinger <cg@exept.de>
parents:
12317
diff
changeset
|
8650 |
^ self trimForWhich:[:ch | ch isSeparator] |
608 | 8651 |
|
8652 |
" |
|
8874 | 8653 |
' foo ' withoutSeparators |
8654 |
' foo' withoutSeparators |
|
8655 |
'foo ' withoutSeparators |
|
8656 |
' ' withoutSeparators |
|
8657 |
(' foo' , Character tab asString , ' ') withoutSeparators inspect |
|
12319
18b80c2ce1e3
oops #withoutSeparators #withoutSpaces were noops
Claus Gittinger <cg@exept.de>
parents:
12317
diff
changeset
|
8658 |
' foo ' asUnicode16String withoutSeparators |
608 | 8659 |
" |
9192 | 8660 |
! |
8661 |
||
8662 |
withoutSpaces |
|
608 | 8663 |
"return a copy of myself without leading and trailing spaces. |
4156 | 8664 |
(but spaces in-between are preserved) |
608 | 8665 |
Notice: this does NOT remove tabs, newline or any other whitespace. |
8666 |
Use withoutSeparators for this." |
|
8667 |
||
12319
18b80c2ce1e3
oops #withoutSeparators #withoutSpaces were noops
Claus Gittinger <cg@exept.de>
parents:
12317
diff
changeset
|
8668 |
^ self trimForWhich:[:ch | ch == Character space] |
608 | 8669 |
|
8670 |
" |
|
8874 | 8671 |
' foo ' withoutSpaces |
8672 |
'foo ' withoutSpaces |
|
8673 |
' foo' withoutSpaces |
|
8674 |
' ' withoutSpaces |
|
8675 |
'a b' withoutSpaces |
|
8676 |
(' foo' , Character tab asString , ' ') withoutSpaces inspect |
|
608 | 8677 |
" |
9192 | 8678 |
! |
8679 |
||
23249 | 8680 |
withoutSuffix:aStringOrCharacter |
22357 | 8681 |
"if the receiver endsWith aString, return a copy without it. |
8682 |
Otherwise return the receiver" |
|
8683 |
||
23249 | 8684 |
(self endsWith:aStringOrCharacter) ifTrue:[ |
8685 |
aStringOrCharacter isCharacter ifTrue:[ |
|
8686 |
^ self copyButLast:1 |
|
8687 |
] ifFalse:[ |
|
8688 |
^ self copyButLast:aStringOrCharacter size |
|
8689 |
]. |
|
22357 | 8690 |
]. |
8691 |
^ self |
|
8692 |
||
8693 |
" |
|
8694 |
'helloworld' withoutSuffix:'world' |
|
8695 |
'helloworld' withoutSuffix:'foo' |
|
23249 | 8696 |
'helloworldx' withoutSuffix:$x |
22357 | 8697 |
" |
8698 |
||
8699 |
"Created: / 23-10-2017 / 15:01:37 / cg" |
|
23249 | 8700 |
"Modified: / 31-07-2018 / 17:06:34 / Claus Gittinger" |
8701 |
! |
|
8702 |
||
8703 |
withoutSuffix:aStringOrCharacter caseSensitive:caseSensitive |
|
23234 | 8704 |
"if the receiver endsWith aString, return a copy without it. |
8705 |
Otherwise return the receiver" |
|
8706 |
||
23249 | 8707 |
(self endsWith:aStringOrCharacter caseSensitive:caseSensitive) ifTrue:[ |
8708 |
aStringOrCharacter isCharacter ifTrue:[ |
|
8709 |
^ self copyButLast:1 |
|
8710 |
] ifFalse:[ |
|
8711 |
^ self copyButLast:aStringOrCharacter size |
|
8712 |
]. |
|
23234 | 8713 |
]. |
8714 |
^ self |
|
8715 |
||
8716 |
" |
|
8717 |
'helloworld' withoutSuffix:'world' |
|
8718 |
'helloworld' withoutSuffix:'foo' |
|
8719 |
" |
|
8720 |
||
8721 |
"Created: / 27-07-2018 / 08:30:10 / Claus Gittinger" |
|
23249 | 8722 |
"Modified: / 31-07-2018 / 17:06:58 / Claus Gittinger" |
23234 | 8723 |
! |
8724 |
||
9192 | 8725 |
withoutTrailingSeparators |
915
1ac1c7e942f4
added withoutTrailingSeparators
Claus Gittinger <cg@exept.de>
parents:
828
diff
changeset
|
8726 |
"return a copy of myself without trailing separators. |
1ac1c7e942f4
added withoutTrailingSeparators
Claus Gittinger <cg@exept.de>
parents:
828
diff
changeset
|
8727 |
Notice: this does remove tabs, newline or any other whitespace. |
1ac1c7e942f4
added withoutTrailingSeparators
Claus Gittinger <cg@exept.de>
parents:
828
diff
changeset
|
8728 |
Returns an empty string, if the receiver consist only of whitespace." |
1ac1c7e942f4
added withoutTrailingSeparators
Claus Gittinger <cg@exept.de>
parents:
828
diff
changeset
|
8729 |
|
12188 | 8730 |
^ self withoutTrailingForWhich:[:ch | ch isSeparator] |
915
1ac1c7e942f4
added withoutTrailingSeparators
Claus Gittinger <cg@exept.de>
parents:
828
diff
changeset
|
8731 |
|
1ac1c7e942f4
added withoutTrailingSeparators
Claus Gittinger <cg@exept.de>
parents:
828
diff
changeset
|
8732 |
" |
8874 | 8733 |
' foo ' withoutTrailingSeparators |
8734 |
'foo ' withoutTrailingSeparators |
|
8735 |
' foo' withoutTrailingSeparators |
|
8736 |
' ' withoutTrailingSeparators |
|
8737 |
'foo' withoutTrailingSeparators |
|
8738 |
(' ' , Character tab asString , ' foo ') withoutTrailingSeparators inspect |
|
8739 |
(' foo' , Character tab asString) withoutTrailingSeparators inspect |
|
915
1ac1c7e942f4
added withoutTrailingSeparators
Claus Gittinger <cg@exept.de>
parents:
828
diff
changeset
|
8740 |
" |
9192 | 8741 |
! ! |
8742 |
||
24250 | 8743 |
|
9192 | 8744 |
!CharacterArray methodsFor:'substring searching'! |
8745 |
||
15055 | 8746 |
findRangeOfString:subString |
8747 |
"find a substring. if found, return the start- and endIndex; |
|
8748 |
if not found, return an empty interval." |
|
8749 |
||
8750 |
^ self rangeOfSubCollection:subString startingAt:1 ifAbsent:[0 to:-1] caseSensitive:true |
|
8751 |
||
8752 |
" |
|
8753 |
'hello world' findRangeOfString:'llo' |
|
8754 |
'hello world' findRangeOfString:'ole' |
|
8755 |
" |
|
8756 |
! |
|
8757 |
||
9192 | 8758 |
findString:subString |
8759 |
"find a substring. if found, return the index; |
|
8760 |
if not found, return 0." |
|
8761 |
||
8762 |
^ self indexOfSubCollection:subString startingAt:1 ifAbsent:0 caseSensitive:true |
|
8763 |
||
8764 |
" |
|
8765 |
'hello world' findString:'llo' |
|
8766 |
'hello world' findString:'ole' |
|
8767 |
" |
|
8768 |
! |
|
608 | 8769 |
|
15734 | 8770 |
findString:subString caseSensitive:caseSensitive |
8771 |
"find a substring. if found, return the index; |
|
8772 |
if not found, return 0." |
|
8773 |
||
8774 |
^ self indexOfSubCollection:subString startingAt:1 ifAbsent:0 caseSensitive:caseSensitive |
|
8775 |
||
8776 |
" |
|
8777 |
'hello world' findString:'LLo' caseSensitive:true |
|
8778 |
'hello world' findString:'LLo' caseSensitive:false |
|
8779 |
" |
|
8780 |
! |
|
8781 |
||
608 | 8782 |
findString:subString ifAbsent:exceptionBlock |
8783 |
"find a substring. If found, return the index; |
|
8784 |
if not found, return the result of evaluating exceptionBlock." |
|
8785 |
||
9087
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
8786 |
^ self indexOfSubCollection:subString startingAt:1 ifAbsent:exceptionBlock caseSensitive:true |
608 | 8787 |
! |
8788 |
||
11018 | 8789 |
findString:subString ignoreCase:ignoreCase |
15734 | 8790 |
<resource: #obsolete> |
11018 | 8791 |
"find a substring. if found, return the index; |
8792 |
if not found, return 0." |
|
8793 |
||
8794 |
^ self indexOfSubCollection:subString startingAt:1 ifAbsent:0 caseSensitive:ignoreCase not |
|
8795 |
||
8796 |
" |
|
8797 |
'hello world' findString:'LLo' ignoreCase:false |
|
8798 |
'hello world' findString:'LLo' ignoreCase:true |
|
8799 |
" |
|
8800 |
! |
|
8801 |
||
608 | 8802 |
findString:subString startingAt:index |
8803 |
"find a substring, starting at index. if found, return the index; |
|
8804 |
if not found, return 0." |
|
8805 |
||
9087
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
8806 |
^ self indexOfSubCollection:subString startingAt:index ifAbsent:0 caseSensitive:true |
608 | 8807 |
|
8808 |
" |
|
9192 | 8809 |
'hello yello' findString:'llo' startingAt:1 |
8810 |
'hello yello' findString:'llo' startingAt:5 |
|
8811 |
'hello yello' findString:'llo' startingAt:15 |
|
8812 |
" |
|
8813 |
! |
|
608 | 8814 |
|
8815 |
findString:subString startingAt:index ifAbsent:exceptionBlock |
|
8816 |
"find a substring, starting at index. if found, return the index; |
|
8817 |
if not found, return the result of evaluating exceptionBlock." |
|
8818 |
||
9087
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
8819 |
^ self indexOfSubCollection:subString startingAt:index ifAbsent:exceptionBlock caseSensitive:true |
608 | 8820 |
! |
8821 |
||
12177 | 8822 |
findString:subString startingAt:index ifAbsent:exceptionBlock caseSensitive:caseSensitive |
10929 | 8823 |
"find a substring, starting at index. if found, return the index; |
8824 |
if not found, return the result of evaluating exceptionBlock." |
|
8825 |
||
8826 |
^ self indexOfSubCollection:subString startingAt:index ifAbsent:exceptionBlock caseSensitive:caseSensitive |
|
8827 |
! |
|
8828 |
||
608 | 8829 |
includesString:aString |
8830 |
"return true, if a substring is contained in the receiver" |
|
8831 |
||
9087
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
8832 |
^ (self indexOfSubCollection:aString startingAt:1 ifAbsent:0 caseSensitive:true) ~~ 0 |
608 | 8833 |
|
8834 |
" |
|
12177 | 8835 |
'hello world' includesString:'hel' |
8874 | 8836 |
'hello world' includesString:'rld' |
9192 | 8837 |
'hello world' includesString:'llo' |
8838 |
'hello world' includesString:'LLO' |
|
12177 | 8839 |
'hello world' includesString:'' |
9192 | 8840 |
" |
8841 |
! |
|
9191 | 8842 |
|
8843 |
includesString:aString caseSensitive:caseSensitive |
|
9087
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
8844 |
"return true, if a substring is contained in the receiver" |
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
8845 |
|
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
8846 |
^ (self indexOfSubCollection:aString startingAt:1 ifAbsent:0 caseSensitive:caseSensitive) ~~ 0 |
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
8847 |
|
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
8848 |
" |
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
8849 |
'hello world' includesString:'hel' caseSensitive:true |
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
8850 |
'hello world' includesString:'HEL' caseSensitive:true |
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
8851 |
'hello world' includesString:'HEL' caseSensitive:false |
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
8852 |
|
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
8853 |
'hello world' includesString:'wor' caseSensitive:true |
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
8854 |
'hello world' includesString:'WOR' caseSensitive:true |
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
8855 |
'hello world' includesString:'WOR' caseSensitive:false |
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
8856 |
" |
9192 | 8857 |
! |
8858 |
||
23060 | 8859 |
indexOfString:aString |
8860 |
"VSE and V'age compatibility" |
|
8861 |
"find a substring. If found, return the index; if not found, return 0." |
|
8862 |
||
8863 |
^ self indexOfSubCollection:aString startingAt:1 ifAbsent:[0] caseSensitive:true |
|
8864 |
||
8865 |
" |
|
8866 |
'hello world' indexOfString:'hello' -> 1 |
|
8867 |
'hello world' indexOfString:'world' -> 7 |
|
8868 |
'hello world' indexOfString:'World' -> 0 |
|
8869 |
'hello world' indexOfString:'World' caseSensitive:false -> 7 |
|
8870 |
" |
|
8871 |
||
8872 |
"Modified (format): / 08-06-2018 / 14:43:15 / Claus Gittinger" |
|
8873 |
! |
|
8874 |
||
8875 |
indexOfString:aString caseSensitive:caseSensitive |
|
8876 |
"VSE and V'age compatibility" |
|
8877 |
"find a substring. If found, return the index; if not found, return 0." |
|
8878 |
||
8879 |
^ self indexOfSubCollection:aString startingAt:1 ifAbsent:[0] caseSensitive:caseSensitive |
|
8880 |
||
8881 |
" |
|
8882 |
'hello world' indexOfString:'hello' -> 1 |
|
8883 |
'hello world' indexOfString:'world' -> 7 |
|
8884 |
'hello world' indexOfString:'World' -> 0 |
|
8885 |
'hello world' indexOfString:'World' caseSensitive:false -> 7 |
|
8886 |
" |
|
8887 |
||
8888 |
"Created: / 08-06-2018 / 14:39:18 / Claus Gittinger" |
|
8889 |
! |
|
8890 |
||
8891 |
indexOfString:aString caseSensitive:caseSensitive ifAbsent:exceptionValue |
|
8892 |
"VSE and V'age compatibility" |
|
8893 |
"find a substring. If found, return the index; if not found, return 0." |
|
8894 |
||
8895 |
^ self indexOfSubCollection:aString startingAt:1 ifAbsent:exceptionValue caseSensitive:caseSensitive |
|
8896 |
||
8897 |
" |
|
8898 |
'abcdefabcdef' indexOfString:'fab' ifAbsent:[999] -> 6 |
|
8899 |
'abcdefabcdef' indexOfString:'Fab' ifAbsent:[999] -> 999 |
|
8900 |
'abcdefabcdef' indexOfString:'Fab' caseSensitive:false ifAbsent:[999] -> 6 |
|
8901 |
'abcdefabcdef' indexOfString:'xxx' caseSensitive:false ifAbsent:[999] -> 999 |
|
8902 |
" |
|
8903 |
||
8904 |
"Created: / 08-06-2018 / 14:40:21 / Claus Gittinger" |
|
8905 |
! |
|
8906 |
||
8907 |
indexOfString:aString caseSensitive:caseSensitive startingAt:startIndex |
|
8908 |
"VSE and V'age compatibility" |
|
8909 |
"find a substring. If found, return the index; if not found, return 0." |
|
8910 |
||
8911 |
^ self indexOfSubCollection:aString startingAt:startIndex ifAbsent:[0] caseSensitive:caseSensitive |
|
8912 |
||
8913 |
"Created: / 08-06-2018 / 14:44:17 / Claus Gittinger" |
|
8914 |
! |
|
8915 |
||
8916 |
indexOfString:aString caseSensitive:caseSensitive startingAt:startIndex ifAbsent:exceptionalValue |
|
8917 |
"VSE and V'age compatibility" |
|
8918 |
"find a substring. |
|
8919 |
If found, return the index; if not found, the value from exceptionalValue." |
|
8920 |
||
8921 |
^ self indexOfSubCollection:aString startingAt:startIndex ifAbsent:exceptionalValue caseSensitive:caseSensitive |
|
8922 |
||
8923 |
"Created: / 08-06-2018 / 14:42:33 / Claus Gittinger" |
|
8924 |
! |
|
8925 |
||
8926 |
indexOfString:aString ifAbsent:exceptionValue |
|
8927 |
"VSE and V'age compatibility" |
|
8928 |
"find a substring. If found, return the index; if not found, return 0." |
|
8929 |
||
8930 |
^ self indexOfSubCollection:aString startingAt:1 ifAbsent:exceptionValue caseSensitive:true |
|
8931 |
||
8932 |
" |
|
8933 |
'abcdefabcdef' indexOfString:'fab' ifAbsent:[999] -> 6 |
|
8934 |
'abcdefabcdef' indexOfString:'Fab' ifAbsent:[999] -> 999 |
|
8935 |
'abcdefabcdef' indexOfString:'Fab' caseSensitieve:false ifAbsent:[999] -> 999 |
|
8936 |
'abcdefabcdef' indexOfString:'xxx' ifAbsent:[999] -> 999 |
|
8937 |
" |
|
8938 |
||
8939 |
"Modified: / 08-06-2018 / 14:43:44 / Claus Gittinger" |
|
8940 |
! |
|
8941 |
||
8942 |
indexOfString:aString startingAt:startIndex |
|
8943 |
"VSE and V'age compatibility" |
|
8944 |
"find a substring. If found, return the index; if not found, return 0." |
|
8945 |
||
8946 |
^ self indexOfSubCollection:aString startingAt:startIndex ifAbsent:[0] caseSensitive:true |
|
8947 |
||
8948 |
"Modified: / 08-06-2018 / 14:37:52 / Claus Gittinger" |
|
8949 |
! |
|
8950 |
||
8951 |
indexOfString:aString startingAt:startIndex ifAbsent:exceptionalValue |
|
8952 |
"VSE and V'age compatibility" |
|
8953 |
"find a substring. |
|
8954 |
If found, return the index; if not found, the value from exceptionalValue." |
|
8955 |
||
8956 |
^ self indexOfSubCollection:aString startingAt:startIndex ifAbsent:exceptionalValue caseSensitive:true |
|
8957 |
||
8958 |
"Modified: / 08-06-2018 / 14:38:12 / Claus Gittinger" |
|
8959 |
! |
|
8960 |
||
18671 | 8961 |
indexOfSubCollection:subString caseSensitive:caseSensitive |
8962 |
"find a substring, starting at index. if found, return the index; |
|
21675 | 8963 |
if not found, return the result of evaluating exceptionBlock." |
18671 | 8964 |
|
8965 |
^ self indexOfSubCollection:subString startingAt:1 ifAbsent:[0] caseSensitive:caseSensitive. |
|
21675 | 8966 |
|
8967 |
"Modified (comment): / 28-03-2017 / 15:53:30 / stefan" |
|
18671 | 8968 |
! |
8969 |
||
9192 | 8970 |
indexOfSubCollection:subString startingAt:index ifAbsent:exceptionBlock |
8971 |
"find a substring, starting at index. if found, return the index; |
|
21675 | 8972 |
if not found, return the result of evaluating exceptionBlock." |
9192 | 8973 |
|
8974 |
^ self indexOfSubCollection:subString startingAt:index ifAbsent:exceptionBlock caseSensitive:true. |
|
21675 | 8975 |
|
8976 |
"Modified (comment): / 28-03-2017 / 15:53:14 / stefan" |
|
9192 | 8977 |
! |
8978 |
||
8979 |
indexOfSubCollection:subString startingAt:index ifAbsent:exceptionBlock caseSensitive:caseSensitive |
|
9087
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
8980 |
"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
|
8981 |
if not found, return the result of evaluating exceptionBlock. |
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
8982 |
This is a q&d hack - not very efficient" |
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
8983 |
|
608 | 8984 |
|firstChar found |
8985 |
startIndex "{ Class: SmallInteger }" |
|
8986 |
subSize "{ Class: SmallInteger }" |
|
8987 |
mySize "{ Class: SmallInteger }" |
|
21675 | 8988 |
runIdx "{ Class: SmallInteger }"| |
608 | 8989 |
|
8990 |
subSize := subString size. |
|
12177 | 8991 |
subSize == 0 ifTrue:[ |
20874 | 8992 |
subString isString ifFalse:[ |
23001 | 8993 |
self proceedableError:'non string argument'. |
20874 | 8994 |
]. |
8995 |
"empty string does not match" |
|
8996 |
^ 0. |
|
8997 |
"empty string matches" |
|
15433 | 8998 |
"/ ^ index |
6122
a98958f1c85f
handle non-string arg to indexOfSubString:
Claus Gittinger <cg@exept.de>
parents:
6119
diff
changeset
|
8999 |
]. |
9087
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
9000 |
|
608 | 9001 |
mySize := self size. |
9002 |
firstChar := subString at:1. |
|
9087
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
9003 |
caseSensitive ifTrue:[ |
20874 | 9004 |
startIndex := self indexOf:firstChar startingAt:index. |
9087
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
9005 |
] ifFalse:[ |
20874 | 9006 |
startIndex := self findFirst:[:c | c sameAs:firstChar] startingAt:index. |
9087
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
9007 |
]. |
21672 | 9008 |
[startIndex ~~ 0] whileTrue:[ |
20874 | 9009 |
runIdx := startIndex. |
9010 |
found := true. |
|
9011 |
1 to:subSize do:[:i | |
|
9012 |
runIdx > mySize ifTrue:[ |
|
9013 |
found := false |
|
9014 |
] ifFalse:[ |
|
21675 | 9015 |
caseSensitive ifTrue:[ |
9016 |
(subString at:i) ~= (self at:runIdx) ifTrue:[found := false]. |
|
9017 |
] ifFalse:[ |
|
9018 |
((subString at:i) sameAs:(self at:runIdx)) ifFalse:[found := false]. |
|
9019 |
]. |
|
20874 | 9020 |
]. |
9021 |
runIdx := runIdx + 1 |
|
9022 |
]. |
|
9023 |
found ifTrue:[ |
|
9024 |
^ startIndex |
|
9025 |
]. |
|
9026 |
caseSensitive ifTrue:[ |
|
9027 |
startIndex := self indexOf:firstChar startingAt:(startIndex + 1) |
|
9028 |
] ifFalse:[ |
|
9029 |
startIndex := self findFirst:[:c | c sameAs:firstChar] startingAt:(startIndex + 1). |
|
9030 |
]. |
|
608 | 9031 |
]. |
9032 |
^ exceptionBlock value |
|
1005
4507487e0be6
more on encoding/decoding (still unfinished)
Claus Gittinger <cg@exept.de>
parents:
997
diff
changeset
|
9033 |
|
21675 | 9034 |
" |
9035 |
'АБВГДЕЖ' asLowercase indexOfSubCollection:'ВГДЕ' startingAt:1 ifAbsent:nil caseSensitive:false. |
|
9036 |
'АБВГДЕЖ' indexOfSubCollection:'ВГДЕ' startingAt:1 ifAbsent:nil caseSensitive:true. |
|
9037 |
" |
|
9038 |
||
21672 | 9039 |
"Modified: / 23-02-1996 / 15:35:15 / cg" |
21675 | 9040 |
"Modified (comment): / 28-03-2017 / 16:05:48 / stefan" |
23001 | 9041 |
"Modified: / 24-05-2018 / 14:55:38 / Claus Gittinger" |
9192 | 9042 |
! |
9043 |
||
17191 | 9044 |
indexOfSubCollection:subString startingAt:index ifAbsent:exceptionBlock caseSensitive:caseSensitive ignoreDiacritics:ignoreDiacritics |
9045 |
"find a substring, starting at index. if found, return the index; |
|
9046 |
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
|
9047 |
This is a q&d hack - not very efficient |
17191 | 9048 |
(see implementation in string, for a much faster algorithm)" |
9049 |
||
9050 |
|firstChar found |
|
9051 |
startIndex "{ Class: SmallInteger }" |
|
9052 |
subSize "{ Class: SmallInteger }" |
|
9053 |
mySize "{ Class: SmallInteger }" |
|
9054 |
runIdx "{ Class: SmallInteger }" |
|
9055 |
tester charMap firstCharMapped findNextIndex| |
|
9056 |
||
9057 |
subSize := subString size. |
|
9058 |
subSize == 0 ifTrue:[ |
|
20874 | 9059 |
subString isString ifFalse:[ |
23001 | 9060 |
self proceedableError:'non string argument'. |
20874 | 9061 |
]. |
9062 |
"empty string does not match" |
|
9063 |
^ 0. |
|
9064 |
"empty string matches" |
|
17191 | 9065 |
"/ ^ index |
9066 |
]. |
|
9067 |
||
9068 |
mySize := self size. |
|
9069 |
firstChar := subString at:1. |
|
9070 |
ignoreDiacritics ifTrue:[ |
|
20874 | 9071 |
caseSensitive ifTrue:[ |
9072 |
charMap := [:ch | ch asLowercase withoutDiacritics]. |
|
9073 |
] ifFalse:[ |
|
9074 |
charMap := [:ch | ch withoutDiacritics]. |
|
9075 |
]. |
|
9076 |
tester := [:c1 :c2 | (charMap value:c1) = (charMap value:c2) ]. |
|
9077 |
firstCharMapped := (charMap value:firstChar). |
|
9078 |
findNextIndex := [:index | self findFirst:[:ch | (charMap value:ch) = firstCharMapped] startingAt:index]. |
|
17191 | 9079 |
] ifFalse:[ |
20874 | 9080 |
caseSensitive ifTrue:[ |
9081 |
tester := [:c1 :c2 | c1 = c2 ]. |
|
9082 |
findNextIndex := [:index | self indexOf:firstChar startingAt:index]. |
|
9083 |
] ifFalse:[ |
|
9084 |
tester := [:c1 :c2 | c1 sameAs: c2 ]. |
|
9085 |
findNextIndex := [:index | self findFirst:[:c | c sameAs:firstChar] startingAt:index]. |
|
9086 |
]. |
|
17191 | 9087 |
]. |
9088 |
startIndex := findNextIndex value:index. |
|
9089 |
[startIndex == 0] whileFalse:[ |
|
20874 | 9090 |
runIdx := startIndex. |
9091 |
found := true. |
|
9092 |
1 to:subSize do:[:i | |
|
9093 |
runIdx > mySize ifTrue:[ |
|
9094 |
found := false |
|
9095 |
] ifFalse:[ |
|
9096 |
(tester value:(subString at:i) value:(self at:runIdx)) ifFalse:[ |
|
9097 |
found := false |
|
9098 |
] |
|
9099 |
]. |
|
9100 |
runIdx := runIdx + 1 |
|
9101 |
]. |
|
9102 |
found ifTrue:[ |
|
9103 |
^ startIndex |
|
9104 |
]. |
|
9105 |
startIndex := findNextIndex value:(startIndex + 1) |
|
17191 | 9106 |
]. |
9107 |
^ exceptionBlock value |
|
9108 |
||
9109 |
" |
|
17600 | 9110 |
'bla depot bla dépots' indexOfSubCollection:'dep' startingAt:1 ifAbsent:0 caseSensitive:false ignoreDiacritics:true. 5 |
9111 |
'bla depot bla dépots' indexOfSubCollection:'dep' startingAt:6 ifAbsent:0 caseSensitive:false ignoreDiacritics:true. 0 |
|
9112 |
||
9113 |
'bla depot bla dépots' indexOfSubCollection:'dep' startingAt:1 ifAbsent:0 caseSensitive:false ignoreDiacritics:false. 5 |
|
9114 |
'bla depot bla dépots' indexOfSubCollection:'dep' startingAt:6 ifAbsent:0 caseSensitive:false ignoreDiacritics:false. 0 |
|
17191 | 9115 |
" |
23001 | 9116 |
|
9117 |
"Modified: / 24-05-2018 / 14:55:43 / Claus Gittinger" |
|
17191 | 9118 |
! |
9119 |
||
18939 | 9120 |
lastIndexOfString:aString |
9121 |
"VSE and V'age compatibility" |
|
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
9122 |
"find the last occurrence of substring. |
18939 | 9123 |
If found, return the index; if not found, return 0." |
9124 |
||
9125 |
^ self lastIndexOfSubCollection:aString startingAt:(self size-aString size+1) ifAbsent:[0] |
|
9126 |
||
9127 |
" 123456789012 |
|
9128 |
'abcdefabcdef' lastIndexOfString:'abc' |
|
9129 |
'abcdefabcdef' lastIndexOfString:'abc' startingAt:6 |
|
9130 |
'abcdefabcdef' lastIndexOfString:'xxx' startingAt:6 |
|
9131 |
" |
|
9132 |
! |
|
9133 |
||
9134 |
lastIndexOfString:aString ifAbsent:exceptionValue |
|
9135 |
"VSE and V'age compatibility" |
|
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
9136 |
"find the last occurrence of substring. |
18939 | 9137 |
If found, return the index; if not found, return 0." |
9138 |
||
9139 |
^ self lastIndexOfSubCollection:aString startingAt:(self size-aString size+1) ifAbsent:exceptionValue |
|
9140 |
||
9141 |
" 123456789012 |
|
9142 |
'abcdefabcdef' lastIndexOfString:'abc' ifAbsent:[999] |
|
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
9143 |
'abcdefabcdef' lastIndexOfString:'xxx' ifAbsent:[999] |
18939 | 9144 |
" |
9145 |
! |
|
9146 |
||
9147 |
lastIndexOfString:aString startingAt:startIndex |
|
9148 |
"VSE and V'age compatibility" |
|
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
9149 |
"find the last occurrence of a substring. |
18939 | 9150 |
If found, return the index; if not found, return 0." |
9151 |
||
9152 |
^ self lastIndexOfSubCollection:aString startingAt:startIndex ifAbsent:[0] |
|
9153 |
||
9154 |
" 123456789012 |
|
9155 |
'abcdefabcdef' lastIndexOfString:'abc' |
|
9156 |
'abcdefabcdef' lastIndexOfString:'abc' startingAt:6 |
|
9157 |
" |
|
9158 |
! |
|
9159 |
||
9160 |
lastIndexOfString:aString startingAt:startIndex ifAbsent:exceptionValue |
|
9161 |
"VSE and V'age compatibility" |
|
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
9162 |
"find the last occurrence of a substring. |
18939 | 9163 |
If found, return the index; if not found, return 0." |
9164 |
||
9165 |
^ self lastIndexOfSubCollection:aString startingAt:startIndex ifAbsent:exceptionValue |
|
9166 |
||
9167 |
" 123456789012 |
|
9168 |
'abcdefabcdef' lastIndexOfString:'abc' |
|
9169 |
'abcdefabcdef' lastIndexOfString:'abc' startingAt:6 |
|
9170 |
'abcdefabcdef' lastIndexOfString:'xxx' startingAt:6 ifAbsent:999 |
|
9171 |
" |
|
9172 |
! |
|
9173 |
||
24145 | 9174 |
occurrencesOfString:aSubString |
9175 |
"count how often the argument aSubString is contained in the receiver" |
|
9176 |
||
9177 |
^ self occurrencesOfString:aSubString caseSensitive:true |
|
9178 |
||
9179 |
" |
|
9180 |
'aa' indexOfString:'aa' startingAt:1 |
|
9181 |
||
9182 |
'' occurrencesOfString:'aa' |
|
9183 |
'a' occurrencesOfString:'aa' |
|
9184 |
'aa' occurrencesOfString:'aa' |
|
9185 |
' aa ' occurrencesOfString:'aa' |
|
9186 |
' aa a' occurrencesOfString:'aa' |
|
9187 |
' aaaa' occurrencesOfString:'aa' |
|
9188 |
' aa aa ' occurrencesOfString:'aa' |
|
9189 |
' aa bb ab ba aa ab' occurrencesOfString:'aa' |
|
9190 |
' aa bb cc aa bb cc aa bb ' occurrencesOfString:'aa' |
|
9191 |
" |
|
9192 |
||
9193 |
"Created: / 25-05-2019 / 08:57:14 / Claus Gittinger" |
|
9194 |
! |
|
9195 |
||
9196 |
occurrencesOfString:aSubString caseSensitive:caseSensitive |
|
9197 |
"count how often the argument aSubString is contained in the receiver" |
|
9198 |
||
9199 |
|idx count| |
|
9200 |
||
9201 |
idx := 1. |
|
9202 |
count := 0. |
|
9203 |
[idx ~~ 0] whileTrue:[ |
|
9204 |
idx := self indexOfString:aSubString caseSensitive:caseSensitive startingAt:idx. |
|
9205 |
idx ~~ 0 ifTrue:[ |
|
9206 |
count := count + 1. |
|
9207 |
idx := idx + aSubString size |
|
9208 |
] |
|
9209 |
]. |
|
9210 |
^ count |
|
9211 |
||
9212 |
" |
|
9213 |
'aa' indexOfString:'aa' startingAt:1 |
|
9214 |
||
9215 |
'' occurrencesOfString:'aA' caseSensitive:false |
|
9216 |
'a' occurrencesOfString:'aA' caseSensitive:false |
|
9217 |
'aa' occurrencesOfString:'aA' caseSensitive:false |
|
9218 |
' aa ' occurrencesOfString:'aA' caseSensitive:false |
|
9219 |
' aa a' occurrencesOfString:'aA' caseSensitive:false |
|
9220 |
' aaaa' occurrencesOfString:'aA' caseSensitive:false |
|
9221 |
' aa aa ' occurrencesOfString:'aA' caseSensitive:false |
|
9222 |
' aa bb ab ba aa ab' occurrencesOfString:'aA' caseSensitive:false |
|
9223 |
' aa bb cc aa bb cc aa bb ' occurrencesOfString:'aA' caseSensitive:false |
|
9224 |
" |
|
9225 |
||
9226 |
"Created: / 25-05-2019 / 09:06:46 / Claus Gittinger" |
|
9227 |
! |
|
9228 |
||
15055 | 9229 |
rangeOfSubCollection:subString startingAt:start ifAbsent:exceptionValue caseSensitive:caseSensitive |
9230 |
"find a substring. if found, return the start- and endIndex; |
|
9231 |
if not found, return the value of exceptionValue." |
|
9232 |
||
9233 |
|i| |
|
9234 |
||
9235 |
i := self indexOfSubCollection:subString startingAt:start ifAbsent:0 caseSensitive:caseSensitive. |
|
9236 |
i == 0 ifTrue:[ |
|
20874 | 9237 |
^ exceptionValue value |
15055 | 9238 |
]. |
9239 |
^ i to:(i + subString size - 1) |
|
9240 |
||
9241 |
" |
|
9242 |
'hello world' findRangeOfString:'llo' |
|
9243 |
'hello world' findRangeOfString:'ole' |
|
9244 |
" |
|
9192 | 9245 |
! ! |
9246 |
||
9247 |
!CharacterArray methodsFor:'testing'! |
|
9248 |
||
18893 | 9249 |
isPlainString |
9250 |
"return true, if the receiver is a plain string (without attributes); |
|
9251 |
true is returned here - redefinition of Object>>isPlainString." |
|
9252 |
||
9253 |
^ true |
|
9254 |
! |
|
9255 |
||
9230 | 9256 |
isString |
9257 |
"return true, if the receiver is some kind of string; |
|
9258 |
true is returned here - redefinition of Object>>isString." |
|
9259 |
||
9260 |
^ true |
|
9261 |
! |
|
9262 |
||
21126 | 9263 |
isUnary |
9264 |
"Answer true if the receiver is a unary selector. |
|
9265 |
That is not a check for being a valid selector, but instead relies on |
|
9266 |
the selector being valid." |
|
9267 |
||
9268 |
^ self isUnarySelector |
|
9269 |
! |
|
9270 |
||
9230 | 9271 |
isUnarySelector |
9272 |
"Answer true if the receiver contains only chars in an ANSI unary method selector, false otherwise." |
|
9273 |
||
17442 | 9274 |
^ (self first isLetterOrUnderline) |
24254 | 9275 |
and:[ self conform: [ :chr | chr isLetterOrDigitOrUnderline ]] |
9845 | 9276 |
|
9277 |
"Modified: / 13-09-2006 / 11:35:15 / cg" |
|
24254 | 9278 |
"Modified: / 05-06-2019 / 17:05:59 / Claus Gittinger" |
9230 | 9279 |
! |
9280 |
||
14122 | 9281 |
isUnicode16String |
9282 |
^ false |
|
9283 |
! |
|
9284 |
||
9285 |
isUnicode32String |
|
19939 | 9286 |
"true if this is a 4-byte unicode string" |
9287 |
||
14122 | 9288 |
^ false |
9289 |
! |
|
9290 |
||
9291 |
isUnicodeString |
|
19938 | 9292 |
"true if this is a 2- or 4-byte unicode string |
9293 |
(i.e. not a single byte string). |
|
20194 | 9294 |
Notice, that the name is misleading: |
19938 | 9295 |
all strings are use unicode encoding" |
20194 | 9296 |
|
14122 | 9297 |
^ false |
9298 |
! |
|
9299 |
||
14555 | 9300 |
isWideString |
19580 | 9301 |
"true if I require more than one byte per character" |
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
9302 |
|
15829 | 9303 |
|string| |
9304 |
||
9305 |
(string := self string) ~~ self ifTrue:[ |
|
20874 | 9306 |
^ string isWideString. |
15829 | 9307 |
]. |
9308 |
^ self contains:[:aCharacter | aCharacter codePoint > 16rFF]. |
|
64 | 9309 |
! ! |
9310 |
||
9192 | 9311 |
!CharacterArray methodsFor:'tracing'! |
9312 |
||
9313 |
traceInto:aRequestor level:level from:referrer |
|
9314 |
"double dispatch into tracer, passing my type implicitely in the selector" |
|
9315 |
||
9316 |
^ aRequestor traceCharacterArray:self level:level from:referrer |
|
9317 |
||
9318 |
||
9319 |
! ! |
|
9320 |
||
9321 |
!CharacterArray methodsFor:'visiting'! |
|
8395
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8376
diff
changeset
|
9322 |
|
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8376
diff
changeset
|
9323 |
acceptVisitor:aVisitor with:aParameter |
16733 | 9324 |
"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
|
9325 |
|
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8376
diff
changeset
|
9326 |
^ aVisitor visitString:self with:aParameter |
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8376
diff
changeset
|
9327 |
! ! |
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8376
diff
changeset
|
9328 |
|
24250 | 9329 |
|
1849
9f35f5934ec6
added msdos-latin1 en/decoder (code = #msdos850)
Claus Gittinger <cg@exept.de>
parents:
1669
diff
changeset
|
9330 |
!CharacterArray class methodsFor:'documentation'! |
629 | 9331 |
|
9332 |
version |
|
18476 | 9333 |
^ '$Header$' |
12058 | 9334 |
! |
9335 |
||
9336 |
version_CVS |
|
18476 | 9337 |
^ '$Header$' |
629 | 9338 |
! ! |
6809 | 9339 |
|
14606 | 9340 |
|
2728
7569e45a4d15
raise error signal if anything goes wrong while decoding.
Claus Gittinger <cg@exept.de>
parents:
2721
diff
changeset
|
9341 |
CharacterArray initialize! |