author | Claus Gittinger <cg@exept.de> |
Thu, 11 Apr 2019 08:55:36 +0200 | |
changeset 24056 | a7b360193da5 |
parent 24041 | ffac2cc791c3 |
child 24085 | d7c121c70d1e |
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 |
|
204 |
"return new string formed by concatenating a copy of the argument, aString" |
|
205 |
||
206 |
^ self fromStringCollection:aCollectionOfStrings separatedBy:'' |
|
207 |
||
208 |
" |
|
209 |
String fromStringCollection:#('hello' 'world' 'how' 'about' 'this') |
|
210 |
" |
|
211 |
||
212 |
"Created: 20.11.1995 / 15:26:59 / cg" |
|
213 |
! |
|
214 |
||
215 |
fromStringCollection:aCollectionOfStrings separatedBy:aSeparatorString |
|
216 |
"return new string formed by concatenating a copy of the argument, aString" |
|
217 |
||
17625 | 218 |
|stream| |
219 |
||
220 |
aCollectionOfStrings do:[:eachString | |
|
20874 | 221 |
stream isNil ifTrue:[ |
222 |
stream := self writeStreamClass with:eachString. |
|
223 |
] ifFalse:[ |
|
224 |
stream |
|
225 |
nextPutAll:aSeparatorString; |
|
226 |
nextPutAll:eachString. |
|
227 |
]. |
|
581 | 228 |
]. |
17700 | 229 |
stream isNil ifTrue:[^ '']. |
17625 | 230 |
^ stream contents |
581 | 231 |
|
232 |
" |
|
233 |
String fromStringCollection:#('hello' 'world' 'how' 'about' 'this') separatedBy:' ' |
|
17625 | 234 |
String fromStringCollection:#('hello' 'world' 'how' 'about' 'this') separatedBy:'Ƞ' |
235 |
Text fromStringCollection:{'hello'. 'world'. 'how' allBold. 'about'. 'this'. 'äöü'} separatedBy:'Ƞ' |
|
581 | 236 |
" |
237 |
||
238 |
"Created: 20.11.1995 / 15:32:17 / cg" |
|
608 | 239 |
! |
240 |
||
5757 | 241 |
fromUTF8Bytes:aByteCollection |
21379 | 242 |
<resource: #obsolete> |
5758 | 243 |
"return a new string which represents the characters as decoded |
244 |
from the utf8 encoded bytes, aByteCollection. |
|
245 |
Returns either a normal String, or a TwoByteString instance. |
|
6810 | 246 |
Only useful, when reading twoByteStrings from external sources. |
247 |
This only handles up-to 16bit characters" |
|
5757 | 248 |
|
7876 | 249 |
^ self decodeFromUTF8:aByteCollection. |
5757 | 250 |
|
21379 | 251 |
"Modified (comment): / 07-02-2017 / 17:32:38 / stefan" |
5757 | 252 |
! |
253 |
||
608 | 254 |
new |
255 |
"return a new empty string" |
|
256 |
||
257 |
^ self basicNew:0 |
|
9425 | 258 |
! |
259 |
||
260 |
readSmalltalkStringFrom:aStreamOrString onError:exceptionBlock |
|
261 |
"read & return the next String from the (character-)stream aStream; |
|
262 |
skipping all whitespace first; return the value of exceptionBlock, |
|
263 |
if no string can be read. The sequence of characters as read from the |
|
264 |
stream must be one as stored via storeOn: or storeString." |
|
265 |
||
266 |
|str collected char| |
|
267 |
||
268 |
str := aStreamOrString readStream. |
|
269 |
||
270 |
"skip whiteSpace" |
|
271 |
str skipSeparators. |
|
11320 | 272 |
|
14452
ef3591302afa
changed: #readSmalltalkStringFrom:onError:
Stefan Vogel <sv@exept.de>
parents:
14451
diff
changeset
|
273 |
(str peekOrNil == $') ifTrue:[ |
20874 | 274 |
str next. |
275 |
collected := self writeStream. |
|
276 |
[str atEnd] whileFalse:[ |
|
277 |
char := str next. |
|
278 |
char == $' ifTrue:[ |
|
279 |
"/ look for another quote |
|
280 |
str peekOrNil ~~ $' ifTrue:[ |
|
281 |
"end of string reached" |
|
282 |
^ collected contents. |
|
283 |
]. |
|
284 |
"eat doubled quote" |
|
285 |
str next. |
|
286 |
]. |
|
287 |
((char ~~ Character return) or:[str peekOrNil ~~ Character lf]) ifTrue:[ |
|
288 |
"compress CRLF to LF, but keep a single CR" |
|
289 |
collected nextPut:char. |
|
290 |
]. |
|
291 |
]. |
|
292 |
"if we come here, we reached the end without finding a closing $'" |
|
9425 | 293 |
]. |
294 |
^ exceptionBlock value |
|
295 |
||
296 |
" |
|
297 |
String readFrom:('''hello world''' readStream) |
|
298 |
String readFrom:('''hello '''' world''' readStream) |
|
299 |
String readFrom:('1 ''hello'' ' readStream) |
|
300 |
String readFrom:('1 ''hello'' ' readStream) onError:['foobar'] |
|
301 |
" |
|
302 |
||
303 |
"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
|
304 |
"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
|
305 |
! |
4bf099f5bb56
CharacterArray class>>#writeStream - answer a CharacterWriteStrem
Stefan Vogel <sv@exept.de>
parents:
11229
diff
changeset
|
306 |
|
13201 | 307 |
writeStreamClass |
21145 | 308 |
"the type of stream used in writeStream. |
17213 | 309 |
Here, we return CharacterWriteStream, which automatically changes |
310 |
the underlying collection to a multiByte string (i.e. UnicodeString). |
|
311 |
So you can stream wide characters into it." |
|
13201 | 312 |
|
313 |
^ CharacterWriteStream |
|
11243
4bf099f5bb56
CharacterArray class>>#writeStream - answer a CharacterWriteStrem
Stefan Vogel <sv@exept.de>
parents:
11229
diff
changeset
|
314 |
|
4bf099f5bb56
CharacterArray class>>#writeStream - answer a CharacterWriteStrem
Stefan Vogel <sv@exept.de>
parents:
11229
diff
changeset
|
315 |
" |
4bf099f5bb56
CharacterArray class>>#writeStream - answer a CharacterWriteStrem
Stefan Vogel <sv@exept.de>
parents:
11229
diff
changeset
|
316 |
String writeStream |
21145 | 317 |
|
318 |
- the following delivers a Unicode16String: |
|
319 |
||
320 |
String streamContents:[:s | |
|
321 |
s nextPut:(Character value:16r3f30). |
|
322 |
s nextPutAll:'hello'. |
|
323 |
s cr. |
|
324 |
] |
|
11243
4bf099f5bb56
CharacterArray class>>#writeStream - answer a CharacterWriteStrem
Stefan Vogel <sv@exept.de>
parents:
11229
diff
changeset
|
325 |
" |
13201 | 326 |
|
327 |
"Created: / 09-01-2011 / 10:37:57 / cg" |
|
64 | 328 |
! ! |
329 |
||
9265 | 330 |
!CharacterArray class methodsFor:'Compatibility-VW'! |
331 |
||
332 |
fromIntegerArray: anArray |
|
333 |
"Answer a new instance of the receiver that is created from the argument, anArray." |
|
334 |
||
335 |
| new | |
|
336 |
||
337 |
new := self new: anArray size. |
|
338 |
1 to: anArray size do:[:index | |
|
20874 | 339 |
new at: index put: (anArray at: index) asCharacter |
9265 | 340 |
]. |
341 |
^new |
|
342 |
||
343 |
" |
|
344 |
String fromIntegerArray: #[8 127] |
|
345 |
" |
|
346 |
! ! |
|
347 |
||
2728
7569e45a4d15
raise error signal if anything goes wrong while decoding.
Claus Gittinger <cg@exept.de>
parents:
2721
diff
changeset
|
348 |
!CharacterArray class methodsFor:'Signal constants'! |
7569e45a4d15
raise error signal if anything goes wrong while decoding.
Claus Gittinger <cg@exept.de>
parents:
2721
diff
changeset
|
349 |
|
7569e45a4d15
raise error signal if anything goes wrong while decoding.
Claus Gittinger <cg@exept.de>
parents:
2721
diff
changeset
|
350 |
decodingFailedSignal |
2833 | 351 |
"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
|
352 |
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
|
353 |
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
|
354 |
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
|
355 |
|
22432 | 356 |
^ DecodingError |
357 |
||
358 |
"Created: / 28-06-1997 / 20:09:55 / cg" |
|
359 |
"Modified: / 03-08-1997 / 18:16:47 / cg" |
|
360 |
"Modified: / 16-01-2018 / 18:58:10 / stefan" |
|
2833 | 361 |
! |
362 |
||
363 |
encodingFailedSignal |
|
364 |
"return the (query-) signal, raised when encoding of a string is not possible |
|
365 |
due to invalid characters contained in the source." |
|
366 |
||
22432 | 367 |
^ EncodingError |
368 |
||
369 |
"Modified: / 28-06-1997 / 20:09:35 / cg" |
|
370 |
"Created: / 03-08-1997 / 18:16:40 / cg" |
|
371 |
"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
|
372 |
! ! |
7569e45a4d15
raise error signal if anything goes wrong while decoding.
Claus Gittinger <cg@exept.de>
parents:
2721
diff
changeset
|
373 |
|
24033 | 374 |
|
1849
9f35f5934ec6
added msdos-latin1 en/decoder (code = #msdos850)
Claus Gittinger <cg@exept.de>
parents:
1669
diff
changeset
|
375 |
!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
|
376 |
|
f0b45a4b82d2
first attempt in encoding/decoding for different character representations (mac/msdos etc.)
Claus Gittinger <cg@exept.de>
parents:
986
diff
changeset
|
377 |
lowSpaceCleanup |
f0b45a4b82d2
first attempt in encoding/decoding for different character representations (mac/msdos etc.)
Claus Gittinger <cg@exept.de>
parents:
986
diff
changeset
|
378 |
"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
|
379 |
|
f0b45a4b82d2
first attempt in encoding/decoding for different character representations (mac/msdos etc.)
Claus Gittinger <cg@exept.de>
parents:
986
diff
changeset
|
380 |
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
|
381 |
|
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 |
CharacterArray lowSpaceCleanup |
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 |
"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
|
387 |
"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
|
388 |
! ! |
f0b45a4b82d2
first attempt in encoding/decoding for different character representations (mac/msdos etc.)
Claus Gittinger <cg@exept.de>
parents:
986
diff
changeset
|
389 |
|
7421 | 390 |
!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
|
391 |
|
7876 | 392 |
decodeFromUTF8:aStringOrByteCollection |
19837 | 393 |
"given a string in UTF8 encoding, |
394 |
return a new string containing the same characters, in Unicode encoding. |
|
395 |
Returns either a normal String, a Unicode16String or a Unicode32String instance. |
|
396 |
This is only useful, when reading from external sources or communicating with |
|
397 |
other systems |
|
398 |
(ST/X never uses utf8 internally, but always uses strings of fully decoded unicode characters). |
|
399 |
This only handles up-to 30bit characters." |
|
400 |
||
21379 | 401 |
|decodedSize nBitsRequired |
402 |
codepoint "{ Class: SmallInteger }" |
|
19837 | 403 |
byte "{ Class: SmallInteger }" |
21379 | 404 |
encodedSize "{ Class: SmallInteger }" |
19837 | 405 |
srcIdx "{ Class: SmallInteger }" |
406 |
idx "{ Class: SmallInteger }" |
|
407 |
nFollowBytes "{ Class: SmallInteger }" |
|
408 |
minValue "{ Class: SmallInteger }" |
|
409 |
newString| |
|
410 |
||
21379 | 411 |
encodedSize := aStringOrByteCollection size. |
412 |
decodedSize := aStringOrByteCollection utf8DecodedSize. |
|
413 |
||
414 |
encodedSize == decodedSize ifTrue:[ |
|
415 |
"/ fast track, also avoid creation of new strings if aStringOrByteCollection is already a 7-bit string |
|
23400 | 416 |
^ aStringOrByteCollection asSingleByteStringIfPossible |
19837 | 417 |
]. |
418 |
||
419 |
nBitsRequired := 8. |
|
420 |
srcIdx := 1. |
|
421 |
||
21379 | 422 |
"this loop is only used to find the largest character" |
423 |
[srcIdx <= encodedSize] whileTrue:[ |
|
424 |
byte := aStringOrByteCollection byteAt:srcIdx. |
|
19837 | 425 |
srcIdx := srcIdx + 1. |
21379 | 426 |
byte > 16r7F ifTrue:[ |
19837 | 427 |
(byte bitAnd:2r11100000) == 2r11000000 ifTrue:[ |
428 |
"/ 80 .. 7FF |
|
21379 | 429 |
nBitsRequired < 16 ifTrue:[ |
430 |
codepoint := ((byte bitAnd:2r00011111) bitShift:6) |
|
431 |
+ ((aStringOrByteCollection byteAt:srcIdx) bitAnd:2r00111111). |
|
432 |
codepoint > 16rFF ifTrue:[ |
|
433 |
nBitsRequired := 16. |
|
434 |
]. |
|
435 |
]. |
|
436 |
srcIdx := srcIdx + 1. |
|
19837 | 437 |
] ifFalse:[(byte bitAnd:2r11110000) == 2r11100000 ifTrue:[ |
438 |
"/ 800 .. FFFF |
|
21379 | 439 |
srcIdx := srcIdx + 2. |
440 |
nBitsRequired < 16 ifTrue:[nBitsRequired := 16]. |
|
19837 | 441 |
] ifFalse:[(byte bitAnd:2r11111000) == 2r11110000 ifTrue:[ |
442 |
"/ 10000 .. 1FFFFF |
|
21379 | 443 |
srcIdx := srcIdx + 3. |
444 |
nBitsRequired := 32. |
|
19837 | 445 |
] ifFalse:[(byte bitAnd:2r11111100) == 2r11111000 ifTrue:[ |
21379 | 446 |
"5 byte sequences do not result in valid unicode" |
19837 | 447 |
"/ 200000 .. 3FFFFFF |
21379 | 448 |
srcIdx := srcIdx + 4. |
449 |
nBitsRequired := 32. |
|
19837 | 450 |
] ifFalse:[(byte bitAnd:2r11111110) == 2r11111100 ifTrue:[ |
21379 | 451 |
"6 byte sequences do not result in valid unicode" |
19837 | 452 |
"/ 4000000 .. 7FFFFFFF |
21379 | 453 |
(byte bitAnd:2r00000001) ~~ 0 ifTrue:[ |
454 |
"/ ST/X can only represent up to 30 bit characters. |
|
19837 | 455 |
"/ but the max unicode character is defined as 16r10 FFFF anyway |
456 |
^ DecodingError newException |
|
457 |
defaultValue:aStringOrByteCollection; |
|
21145 | 458 |
raiseRequestWith:aStringOrByteCollection |
459 |
errorString:('unicode character out of range at: %1' bindWith:srcIdx). |
|
19837 | 460 |
]. |
21379 | 461 |
srcIdx := srcIdx + 5. |
462 |
nBitsRequired := 32. |
|
19837 | 463 |
] ifFalse:[ |
464 |
^ DecodingError newException |
|
465 |
defaultValue:aStringOrByteCollection; |
|
466 |
raiseRequestWith:aStringOrByteCollection errorString:'invalid utf8 encoding'. |
|
467 |
]]]]]. |
|
468 |
]. |
|
469 |
]. |
|
470 |
||
471 |
nBitsRequired == 8 ifTrue:[ |
|
21379 | 472 |
newString := String uninitializedNew:decodedSize |
473 |
] ifFalse:[nBitsRequired == 16 ifTrue:[ |
|
474 |
newString := Unicode16String new:decodedSize |
|
19837 | 475 |
] ifFalse:[ |
21379 | 476 |
newString := Unicode32String new:decodedSize |
19837 | 477 |
]]. |
478 |
||
479 |
idx := srcIdx := 1. |
|
480 |
||
481 |
"now fill the string" |
|
21379 | 482 |
[srcIdx <= encodedSize] whileTrue:[ |
483 |
byte := codepoint := aStringOrByteCollection byteAt:srcIdx. |
|
19837 | 484 |
srcIdx := srcIdx + 1. |
21379 | 485 |
byte > 16r7F ifTrue:[ |
19837 | 486 |
(byte bitAnd:2r11100000) == 2r11000000 ifTrue:[ |
21379 | 487 |
codepoint := byte bitAnd:2r00011111. |
19837 | 488 |
nFollowBytes := 1. |
21379 | 489 |
minValue := 16r80. |
19837 | 490 |
] ifFalse:[(byte bitAnd:2r11110000) == 2r11100000 ifTrue:[ |
21379 | 491 |
codepoint := byte bitAnd:2r00001111. |
19837 | 492 |
nFollowBytes := 2. |
21379 | 493 |
minValue := 16r800. |
19837 | 494 |
] ifFalse:[(byte bitAnd:2r11111000) == 2r11110000 ifTrue:[ |
21379 | 495 |
codepoint := byte bitAnd:2r00000111. |
19837 | 496 |
nFollowBytes := 3. |
21379 | 497 |
minValue := 16r10000. |
19837 | 498 |
] ifFalse:[(byte bitAnd:2r11111100) == 2r11111000 ifTrue:[ |
499 |
"5 byte sequences do not result in valid unicode" |
|
21379 | 500 |
codepoint := byte bitAnd:2r00000011. |
19837 | 501 |
nFollowBytes := 4. |
21379 | 502 |
minValue := 16r200000. |
19837 | 503 |
] ifFalse:[(byte bitAnd:2r11111110) == 2r11111100 ifTrue:[ |
504 |
"6 byte sequences do not result in valid unicode" |
|
21379 | 505 |
codepoint := byte bitAnd:2r00000001. |
19837 | 506 |
nFollowBytes := 5. |
21379 | 507 |
minValue := 16r4000000. |
19837 | 508 |
]]]]]. |
21379 | 509 |
|
19837 | 510 |
nFollowBytes timesRepeat:[ |
511 |
byte := aStringOrByteCollection byteAt:srcIdx. |
|
21379 | 512 |
(byte bitAnd:2r11000000) ~~ 2r10000000 ifTrue:[ |
513 |
^ DecodingError newException |
|
514 |
defaultValue:aStringOrByteCollection; |
|
515 |
raiseRequestWith:aStringOrByteCollection |
|
516 |
errorString:('illegal followbyte (expect 0x80): 0x%1 at:%2' bindWith:(byte hexPrintString:2) with:srcIdx). |
|
517 |
]. |
|
19837 | 518 |
srcIdx := srcIdx + 1. |
21379 | 519 |
codepoint := (codepoint bitShift:6) bitOr:(byte bitAnd:2r00111111). |
520 |
]. |
|
521 |
||
522 |
codepoint < minValue ifTrue:[ |
|
523 |
"encoding a value in a longer utf8-sequence than required is not allowed |
|
524 |
and imposes a security risk. Proceed to continue with decoding." |
|
525 |
DecodingError newException |
|
526 |
defaultValue:aStringOrByteCollection; |
|
527 |
raiseRequestWith:aStringOrByteCollection errorString:'overlong utf8 sequence'. |
|
19837 | 528 |
]. |
529 |
]. |
|
21379 | 530 |
newString at:idx put:(Character value:codepoint). |
19837 | 531 |
idx := idx + 1. |
532 |
]. |
|
533 |
^ newString |
|
534 |
||
535 |
" |
|
536 |
CharacterArray decodeFromUTF8:#[ 16r41 16r42 ] |
|
537 |
CharacterArray decodeFromUTF8:#[ 16rC6 16r8F ] |
|
538 |
CharacterArray decodeFromUTF8:#[ 16rEF 16rBF 16rBF ] |
|
539 |
||
21379 | 540 |
rfc3629 examples: |
19837 | 541 |
CharacterArray decodeFromUTF8:#[ 16r41 16rE2 16r89 16rA2 16rCE 16r91 16r2E ] |
542 |
CharacterArray decodeFromUTF8:#[ 16rED 16r95 16r9C 16rEA 16rB5 16rAD 16rEC 16r96 16rB4 ] |
|
543 |
CharacterArray decodeFromUTF8:#[ 16rE6 16r97 16rA5 16rE6 16r9C 16rAC 16rE8 16rAA 16r9E ] |
|
544 |
||
545 |
invalid: |
|
546 |
CharacterArray decodeFromUTF8:#[ 16rC0 16r80 ] |
|
547 |
CharacterArray decodeFromUTF8:#[ 16rE0 16r80 16r80 ] |
|
548 |
CharacterArray decodeFromUTF8:#[ 16rE0 16r81 16r02 ] |
|
21379 | 549 |
" |
550 |
||
551 |
"Modified: / 07-02-2017 / 17:41:17 / stefan" |
|
23400 | 552 |
"Modified: / 04-10-2018 / 13:29:53 / Claus Gittinger" |
20010 | 553 |
! |
554 |
||
20011 | 555 |
setupNormalizationMaps |
20864 | 556 |
"returns a 2-stage map from ch2 -> ch1 -> mappedChar. |
557 |
for unicode normalization |
|
558 |
(i.e. for replacing combining char-sequences with regular characters). |
|
559 |
ch2 is the combining charCode (eg. 0x0308), ch1 is the previous character (eg. $A), |
|
560 |
mappedChar is the result (eg. $Ä). |
|
561 |
Caveat: |
|
562 |
possibly incomplete: only COMBINING_DIACRITICAL_MARKS are cared for. |
|
563 |
Does not care for COMBINING_DIACRITICAL_MARKS_EXTENDED |
|
564 |
and COMBINING_DIACRITICAL_MARKS_SUPPLEMENT. |
|
565 |
However; those are used for German dialectology, ancient Greek and other similar |
|
566 |
exotic uses. Probably noone will ever even notice that they are missing..." |
|
20011 | 567 |
|
568 |
|def| |
|
569 |
||
570 |
UnicodeNormalizationMap := Dictionary new. |
|
20013 | 571 |
UnicodeDenormalizationMap := Dictionary new. |
572 |
||
573 |
def := [:combiner :chars :mappedChars | |
|
20011 | 574 |
|d| |
575 |
||
20013 | 576 |
d := UnicodeNormalizationMap at:combiner ifAbsentPut:[Dictionary new]. |
577 |
chars with:mappedChars do:[:ch1 :mappedChar | |
|
578 |
d at:ch1 put:mappedChar. |
|
579 |
UnicodeDenormalizationMap at:mappedChar put:(Unicode16String with:ch1 with:combiner). |
|
580 |
]. |
|
20011 | 581 |
]. |
20014 | 582 |
def value:(Character codePoint:16r0300) "grave" value:'AEIOUaeiouWw' value:'ÀÈÌÒÙàèìòùẀẁ'. |
583 |
def value:(Character codePoint:16r0301) "acute" value:'AEIOUYaeiouyÇçKkMmPpWw' value:'ÁÉÍÓÚÝáéíóúýḈḉḰḱḾḿṔṕẂẃ'. |
|
584 |
def value:(Character codePoint:16r0302) "circumflex" value:'AEIOUaeiouZz' value:'ÂÊÎÔÛâêîôûẐẑ'. |
|
20011 | 585 |
def value:(Character codePoint:16r0303) "tilde" value:'AONaon' value:'ÃÕÑãõñ'. |
20014 | 586 |
def value:(Character codePoint:16r0307) "dot over" value:'BbDdFfHhMmNnPpRrSsTtWwXxYyṢṣ' value:'ḂḃḊḋḞḟḢḣṀṁṄṅṖṗṘṙṠṡṪṫẆẇẊẋẎẏṨṩ'. |
587 |
def value:(Character codePoint:16r0308) "diaresis" value:'AEIOUaeiouyHhWwXxt' value:'ÄËÏÖÜäëïöüÿḦḧẄẅẌẍẗ'. |
|
588 |
def value:(Character codePoint:16r030A) "ring" value:'Aawy' value:'Ååẘẙ'. |
|
589 |
def value:(Character codePoint:16r0323) "dot under" value:'AaBbDdEeIiHhKkLlMmNnOoRrSsTtUuVvWwZzṠṡ' value:'ẠạḄḅḌḍẸẹỊịḤḥḲḳḶḷṂṃṆṇỌọṚṛṢṣṬṭỤụṾṿẈẉẒẓṨṩ'. |
|
20011 | 590 |
def value:(Character codePoint:16r0327) "cedilla" value:'Cc' value:'Çç'. |
591 |
||
592 |
" |
|
20014 | 593 |
self setupNormalizationMaps |
20011 | 594 |
" |
595 |
! |
|
596 |
||
597 |
unicodeDenormalizationMap |
|
598 |
"returns a 2-stage map from ch2 -> ch1 -> mappedChar |
|
20864 | 599 |
for unicode normalization (i.e. for making combining chars regular ones). |
600 |
Caveat: |
|
601 |
possibly incomplete: only COMBINING_DIACRITICAL_MARKS are cared for. |
|
602 |
Does not care for COMBINING_DIACRITICAL_MARKS_EXTENDED |
|
603 |
and COMBINING_DIACRITICAL_MARKS_SUPPLEMENT. |
|
604 |
However; those are used for German dialectology, ancient Greek and other similar |
|
605 |
exotic uses. Probably noone will ever even notice that they are missing..." |
|
20011 | 606 |
|
607 |
UnicodeDenormalizationMap isNil ifTrue:[ |
|
608 |
self setupNormalizationMaps |
|
609 |
]. |
|
610 |
^ UnicodeDenormalizationMap |
|
611 |
||
612 |
" |
|
613 |
self unicodeDenormalizationMap |
|
614 |
" |
|
615 |
! |
|
616 |
||
20010 | 617 |
unicodeNormalizationMap |
618 |
"returns a 2-stage map from ch2 -> ch1 -> mappedChar |
|
20864 | 619 |
for unicode normalization (i.e. for making combining chars regular ones). |
620 |
Caveat: |
|
621 |
possibly incomplete: only COMBINING_DIACRITICAL_MARKS are cared for. |
|
622 |
Does not care for COMBINING_DIACRITICAL_MARKS_EXTENDED |
|
623 |
and COMBINING_DIACRITICAL_MARKS_SUPPLEMENT. |
|
624 |
However; those are used for German dialectology, ancient Greek and other similar |
|
625 |
exotic uses. Probably noone will ever even notice that they are missing..." |
|
20010 | 626 |
|
627 |
UnicodeNormalizationMap isNil ifTrue:[ |
|
20011 | 628 |
self setupNormalizationMaps |
20010 | 629 |
]. |
630 |
^ UnicodeNormalizationMap |
|
631 |
||
632 |
" |
|
633 |
self unicodeNormalizationMap |
|
634 |
" |
|
443 | 635 |
! ! |
636 |
||
24033 | 637 |
|
1849
9f35f5934ec6
added msdos-latin1 en/decoder (code = #msdos850)
Claus Gittinger <cg@exept.de>
parents:
1669
diff
changeset
|
638 |
!CharacterArray class methodsFor:'pattern matching'! |
64 | 639 |
|
5342
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
640 |
matchEscapeCharacter |
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
641 |
"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
|
642 |
(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
|
643 |
|
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
644 |
^ $\ |
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
645 |
! |
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
646 |
|
15734 | 647 |
matchScan:matchScanArray from:matchStart to:matchStop with:aString from:start to:stop caseSensitive:caseSensitive |
327 | 648 |
"helper for match; return true if the characters from start to stop in |
649 |
aString are matching the scan in matchScan from matchStart to matchStop. |
|
328 | 650 |
The matchScan is as created by asMatchScanArray. |
327 | 651 |
|
8874 | 652 |
This algorithm is not at all the most efficient; |
653 |
for heavy duty pattern matching, an interface (primitive) to the regex |
|
328 | 654 |
pattern matching package should be added." |
327 | 655 |
|
8874 | 656 |
|matchEntry |
327 | 657 |
mStart "{ Class: SmallInteger }" |
658 |
mStop "{ Class: SmallInteger }" |
|
659 |
sStart "{ Class: SmallInteger }" |
|
660 |
sStop "{ Class: SmallInteger }" |
|
661 |
mSize "{ Class: SmallInteger }" |
|
662 |
sSize "{ Class: SmallInteger }" |
|
663 |
index "{ Class: SmallInteger }" |
|
2561 | 664 |
quickCheck matchLast nextMatchEntry |
986 | 665 |
checkChar included| |
327 | 666 |
|
667 |
mStart := matchStart. |
|
668 |
mStop := matchStop. |
|
669 |
sStart := start. |
|
670 |
sStop := stop. |
|
671 |
||
16245 | 672 |
[ |
8874 | 673 |
"/ Transcript showCR:('match: ''' , (aString copyFrom:sStart to:sStop) , |
3537 | 674 |
"/ ''' against:' , (matchScanArray copyFrom:mStart to:mStop) printString). |
675 |
||
20874 | 676 |
mSize := mStop - mStart + 1. |
677 |
sSize := sStop - sStart + 1. |
|
678 |
||
679 |
"empty strings match" |
|
680 |
(mSize == 0) ifTrue:[^ (sSize == 0)]. |
|
681 |
||
682 |
matchEntry := matchScanArray at:mStart. |
|
683 |
||
684 |
"/ the most common case first: |
|
685 |
(sSize ~~ 0 |
|
686 |
and:[(checkChar := (aString at:sStart)) = matchEntry]) ifTrue:[ |
|
687 |
"advance by one and continue" |
|
688 |
mStart := mStart + 1. |
|
689 |
sStart := sStart + 1 |
|
690 |
] ifFalse:[ |
|
691 |
(matchEntry == #any) ifTrue:[ |
|
692 |
"restString empty -> no match" |
|
693 |
(sSize == 0) ifTrue:[^ false]. |
|
694 |
"# matches single character" |
|
695 |
((sSize == 1) and:[mSize == 1]) ifTrue:[^ true]. |
|
696 |
"advance by one and continue" |
|
697 |
mStart := mStart + 1. |
|
698 |
sStart := sStart + 1 |
|
699 |
] ifFalse:[ |
|
700 |
(matchEntry == #anyString) ifTrue:[ |
|
701 |
"* alone matches anything" |
|
702 |
(mSize == 1) ifTrue:[^ true]. |
|
703 |
"restString empty & matchString not empty -> no match" |
|
704 |
(sSize == 0) ifTrue:[^ false]. |
|
705 |
||
706 |
" |
|
707 |
try to avoid some of the recursion by checking last |
|
708 |
character and continue with shortened strings if possible |
|
709 |
" |
|
710 |
quickCheck := false. |
|
711 |
(mStop >= mStart) ifTrue:[ |
|
712 |
matchLast := matchScanArray at:mStop. |
|
713 |
(matchLast ~~ #anyString) ifTrue:[ |
|
714 |
(matchLast == #any) ifTrue:[ |
|
715 |
quickCheck := true |
|
716 |
] ifFalse:[ |
|
717 |
matchLast == (aString at:sStop) ifTrue:[ |
|
718 |
quickCheck := true |
|
719 |
] ifFalse:[ |
|
720 |
matchLast isString ifTrue:[ |
|
721 |
quickCheck := matchLast includes:(aString at:sStop) |
|
722 |
] |
|
723 |
] |
|
724 |
] |
|
725 |
] |
|
726 |
]. |
|
727 |
quickCheck ifTrue:[ |
|
728 |
" |
|
729 |
quickCheck ok, advance from the right |
|
730 |
" |
|
731 |
mStop := mStop - 1. |
|
732 |
sStop := sStop - 1 |
|
733 |
] ifFalse:[ |
|
734 |
"/ no quick check; |
|
735 |
"/ look for the next character(s) |
|
736 |
"/ and try matching there |
|
737 |
"/ (to avoid recursion) |
|
738 |
||
739 |
mStart < mStop ifTrue:[ |
|
740 |
nextMatchEntry := matchScanArray at:mStart+1. |
|
741 |
nextMatchEntry isCharacter ifTrue:[ |
|
742 |
sStart <= sStop ifTrue:[ |
|
743 |
[ |
|
744 |
caseSensitive ifTrue:[ |
|
745 |
index := aString indexOf:nextMatchEntry startingAt:sStart |
|
746 |
] ifFalse:[ |
|
747 |
index := aString findFirst:[:c | c asLowercase = nextMatchEntry asLowercase] |
|
748 |
startingAt:sStart. |
|
749 |
]. |
|
750 |
(index == 0 or:[index > sStop]) ifTrue:[ |
|
751 |
^ false |
|
752 |
]. |
|
753 |
(self matchScan:matchScanArray |
|
754 |
from:(mStart + 1) |
|
755 |
to:mStop |
|
756 |
with:aString |
|
757 |
from:index |
|
758 |
to:sStop |
|
759 |
caseSensitive:caseSensitive |
|
760 |
) ifTrue:[ |
|
761 |
^ true |
|
762 |
]. |
|
763 |
sStart := index + 1. |
|
764 |
] loop. |
|
765 |
] |
|
766 |
] |
|
767 |
]. |
|
768 |
||
769 |
" |
|
770 |
no quick check possible; |
|
771 |
loop over all possible substrings |
|
772 |
" |
|
773 |
index := sStart. |
|
774 |
[index <= sStop] whileTrue:[ |
|
775 |
(self matchScan:matchScanArray |
|
776 |
from:(mStart + 1) |
|
777 |
to:mStop |
|
778 |
with:aString |
|
779 |
from:index |
|
780 |
to:sStop |
|
781 |
caseSensitive:caseSensitive |
|
782 |
) ifTrue:[ |
|
783 |
^ true |
|
784 |
]. |
|
785 |
index := index + 1 |
|
786 |
]. |
|
787 |
^ false |
|
788 |
]. |
|
789 |
] ifFalse:[ |
|
790 |
(matchEntry isString) ifTrue:[ |
|
791 |
"testString empty -> no match" |
|
792 |
(sSize == 0) ifTrue:[^ false]. |
|
793 |
||
794 |
included := false. |
|
795 |
"/ checkChar := aString at:sStart. |
|
796 |
included := matchEntry includes:checkChar. |
|
797 |
included ifFalse:[ |
|
798 |
caseSensitive ifFalse:[ |
|
799 |
checkChar isUppercase ifTrue:[ |
|
800 |
included := matchEntry includes:checkChar asLowercase. |
|
801 |
] ifFalse:[ |
|
802 |
included := matchEntry includes:checkChar asUppercase. |
|
803 |
] |
|
804 |
]. |
|
805 |
]. |
|
806 |
mStart := mStart + 1. |
|
807 |
mSize := mSize - 1. |
|
808 |
included ifFalse:[^ false]. |
|
809 |
||
810 |
((sSize == 1) and:[mSize == 0]) ifTrue:[^ true]. |
|
811 |
] ifFalse:[ |
|
812 |
"/ must be single character |
|
813 |
||
814 |
"testString empty ?" |
|
815 |
(sSize == 0) ifTrue:[^ false]. |
|
816 |
||
817 |
"first characters equal ?" |
|
818 |
"/ checkChar := aString at:sStart. |
|
819 |
caseSensitive ifTrue:[^ false]. |
|
820 |
(checkChar asUppercase ~= matchEntry asUppercase) ifTrue:[^ false]. |
|
821 |
||
822 |
"advance and continue" |
|
823 |
mStart := mStart + 1. |
|
824 |
]. |
|
825 |
"cut off 1st char and continue" |
|
826 |
sStart := sStart + 1 |
|
827 |
] |
|
828 |
] |
|
829 |
] |
|
16245 | 830 |
] loop. |
2519
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
831 |
|
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
832 |
" |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
833 |
|scanArray s| |
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 |
scanArray := self matchScanArrayFrom:'*hello'. |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
836 |
s := 'foo bar hello world'. |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
837 |
CharacterArray |
20874 | 838 |
matchScan:scanArray |
839 |
from:1 |
|
840 |
to:scanArray size |
|
841 |
with:s |
|
842 |
from:1 |
|
843 |
to:s size |
|
844 |
caseSensitive:true |
|
2519
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
845 |
" |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
846 |
" |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
847 |
|scanArray s| |
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 |
scanArray := self matchScanArrayFrom:'*hello*'. |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
850 |
s := 'foo bar hello world'. |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
851 |
CharacterArray |
20874 | 852 |
matchScan:scanArray |
853 |
from:1 |
|
854 |
to:scanArray size |
|
855 |
with:s |
|
856 |
from:1 |
|
857 |
to:s size |
|
858 |
caseSensitive:true |
|
15734 | 859 |
" |
860 |
||
861 |
"Modified: / 24-07-2011 / 07:17:03 / cg" |
|
862 |
"Modified (comment): / 24-07-2011 / 08:55:14 / cg" |
|
863 |
! |
|
864 |
||
865 |
matchScan:matchScanArray from:matchStart to:matchStop with:aString from:start to:stop ignoreCase:ignoreCase |
|
866 |
<resource: #obsolete> |
|
867 |
"helper for match; return true if the characters from start to stop in |
|
868 |
aString are matching the scan in matchScan from matchStart to matchStop. |
|
869 |
The matchScan is as created by asMatchScanArray. |
|
870 |
||
871 |
This algorithm is not at all the most efficient; |
|
872 |
for heavy duty pattern matching, an interface (primitive) to the regex |
|
873 |
pattern matching package should be added." |
|
874 |
||
875 |
^ self |
|
20874 | 876 |
matchScan:matchScanArray from:matchStart to:matchStop with:aString from:start to:stop caseSensitive:ignoreCase not |
15734 | 877 |
|
878 |
" |
|
879 |
|scanArray s| |
|
880 |
||
881 |
scanArray := self matchScanArrayFrom:'*hello'. |
|
882 |
s := 'foo bar hello world'. |
|
883 |
CharacterArray |
|
20874 | 884 |
matchScan:scanArray |
885 |
from:1 |
|
886 |
to:scanArray size |
|
887 |
with:s |
|
888 |
from:1 |
|
889 |
to:s size |
|
890 |
ignoreCase:false |
|
15734 | 891 |
" |
892 |
" |
|
893 |
|scanArray s| |
|
894 |
||
895 |
scanArray := self matchScanArrayFrom:'*hello*'. |
|
896 |
s := 'foo bar hello world'. |
|
897 |
CharacterArray |
|
20874 | 898 |
matchScan:scanArray |
899 |
from:1 |
|
900 |
to:scanArray size |
|
901 |
with:s |
|
902 |
from:1 |
|
903 |
to:s size |
|
904 |
ignoreCase:false |
|
13520 | 905 |
" |
906 |
||
907 |
"Modified: / 24-07-2011 / 07:17:03 / cg" |
|
908 |
"Modified (comment): / 24-07-2011 / 08:55:14 / cg" |
|
608 | 909 |
! |
910 |
||
911 |
matchScanArrayFrom:aString |
|
912 |
"scan a pattern string and decompose it into a scanArray. |
|
913 |
This is processed faster (especially with character ranges), and |
|
914 |
can also be reused later. (if the same pattern is to be searched again)" |
|
915 |
||
9282
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
916 |
^ self matchScanArrayFrom:aString escapeCharacter:(self matchEscapeCharacter) |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
917 |
|
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
918 |
" |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
919 |
String matchScanArrayFrom:'*ute*' |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
920 |
String matchScanArrayFrom:'**ute**' |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
921 |
String matchScanArrayFrom:'*uter' |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
922 |
String matchScanArrayFrom:'\*uter' |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
923 |
String matchScanArrayFrom:'[cC]#mpute[rR]' |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
924 |
String matchScanArrayFrom:'[abcd]*' |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
925 |
String matchScanArrayFrom:'[a-k]*' |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
926 |
String matchScanArrayFrom:'*some*compl*ern*' |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
927 |
String matchScanArrayFrom:'[a-' |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
928 |
String matchScanArrayFrom:'[a-zA-Z]' |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
929 |
String matchScanArrayFrom:'[a-z01234A-Z]' |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
930 |
" |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
931 |
|
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
932 |
"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
|
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 |
matchScanArrayFrom:aString escapeCharacter:escape |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
936 |
"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
|
937 |
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
|
938 |
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
|
939 |
|
8874 | 940 |
|coll |
608 | 941 |
idx "{ Class: SmallInteger }" |
9282
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
942 |
end c1 c2 matchSet previous| |
5342
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
943 |
|
5487 | 944 |
previous := nil. |
608 | 945 |
|
946 |
coll := OrderedCollection new. |
|
947 |
idx := 1. end := aString size. |
|
948 |
[idx <= end] whileTrue:[ |
|
20874 | 949 |
|char this| |
950 |
||
951 |
char := aString at:idx. |
|
952 |
char == $* ifTrue:[ |
|
953 |
previous ~~ #anyString ifTrue:[ |
|
954 |
this := #anyString |
|
955 |
] |
|
956 |
] ifFalse:[ |
|
957 |
char == $# ifTrue:[ |
|
958 |
previous ~~ #anyString ifTrue:[ |
|
959 |
this := #any |
|
960 |
] |
|
961 |
] ifFalse:[ |
|
962 |
char == $[ ifTrue:[ |
|
963 |
matchSet := IdentitySet new. |
|
964 |
idx := idx + 1. |
|
965 |
idx > end ifTrue:[^ nil]. |
|
966 |
char := aString at:idx. |
|
967 |
c1 := nil. |
|
968 |
[char ~~ $]] whileTrue:[ |
|
969 |
((char == $-) and:[c1 notNil]) ifTrue:[ |
|
970 |
idx := idx + 1. |
|
971 |
idx > end ifTrue:[^ nil]. |
|
972 |
c2 := aString at:idx. |
|
973 |
c1 to:c2 do:[:c | matchSet add:c]. |
|
974 |
c1 := nil. |
|
975 |
idx := idx + 1. |
|
976 |
] ifFalse:[ |
|
977 |
(char ~~ $]) ifTrue:[ |
|
978 |
matchSet add:char. |
|
979 |
c1 := char. |
|
980 |
idx := idx + 1 |
|
981 |
] |
|
982 |
]. |
|
983 |
idx > end ifTrue:[^ nil]. |
|
984 |
char := aString at:idx |
|
985 |
]. |
|
23552 | 986 |
this := matchSet asString sort. |
20874 | 987 |
] ifFalse:[ |
988 |
char == escape ifTrue:[ |
|
989 |
idx := idx + 1. |
|
990 |
idx > end ifTrue:[ |
|
991 |
"/ mhmh - what should we do here ? |
|
992 |
this := char |
|
993 |
] ifFalse:[ |
|
994 |
this := aString at:idx. |
|
995 |
] |
|
996 |
] ifFalse:[ |
|
997 |
this := char |
|
998 |
] |
|
999 |
] |
|
1000 |
] |
|
1001 |
]. |
|
1002 |
this notNil ifTrue:[coll add:this. previous := this]. |
|
1003 |
idx := idx + 1 |
|
608 | 1004 |
]. |
1005 |
||
1006 |
^ coll asArray |
|
1007 |
||
1008 |
" |
|
8874 | 1009 |
String matchScanArrayFrom:'*ute*' |
1010 |
String matchScanArrayFrom:'**ute**' |
|
1011 |
String matchScanArrayFrom:'*uter' |
|
1012 |
String matchScanArrayFrom:'\*uter' |
|
1013 |
String matchScanArrayFrom:'[cC]#mpute[rR]' |
|
1014 |
String matchScanArrayFrom:'[abcd]*' |
|
23552 | 1015 |
String matchScanArrayFrom:'[abcdŴĂĂ]*' |
8874 | 1016 |
String matchScanArrayFrom:'[a-k]*' |
1017 |
String matchScanArrayFrom:'*some*compl*ern*' |
|
1018 |
String matchScanArrayFrom:'[a-' |
|
1019 |
String matchScanArrayFrom:'[a-zA-Z]' |
|
1020 |
String matchScanArrayFrom:'[a-z01234A-Z]' |
|
608 | 1021 |
" |
2519
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
1022 |
|
23552 | 1023 |
"Modified: / 02-04-1997 / 16:20:29 / cg" |
1024 |
"Modified: / 03-12-2018 / 15:41:54 / Stefan Vogel" |
|
9192 | 1025 |
! ! |
1026 |
||
1027 |
!CharacterArray class methodsFor:'queries'! |
|
1028 |
||
1029 |
isAbstract |
|
11229 | 1030 |
"Return if this class is an abstract class. |
1031 |
True is returned for CharacterArray here; false for subclasses. |
|
19464 | 1032 |
Abstract subclasses must redefine this again." |
11229 | 1033 |
|
9192 | 1034 |
^ self == CharacterArray |
18597 | 1035 |
! |
1036 |
||
1037 |
speciesForCharacterSize:characterSize |
|
1038 |
"answer the class, that is able to hold characters of size characterSize" |
|
1039 |
||
1040 |
characterSize <= 8 ifTrue:[^ String]. |
|
1041 |
characterSize <= 16 ifTrue:[^ Unicode16String]. |
|
1042 |
^ Unicode32String |
|
9192 | 1043 |
! ! |
1044 |
||
24033 | 1045 |
|
22525 | 1046 |
!CharacterArray class methodsFor:'utilities'! |
1047 |
||
23983 | 1048 |
through:aCharacter in:inStream |
1049 |
"read all characters through aCharacter and return a |
|
1050 |
dense strings for it (i.e. String/TwoByteString, if possible). |
|
1051 |
This helper is present because when reading from a unicode32 stream, |
|
1052 |
dense strings are preferred. |
|
1053 |
If inStream is known to return single byte characters, |
|
1054 |
you should better use inStream through:aCharacter" |
|
1055 |
||
1056 |
|answerStream char| |
|
1057 |
||
1058 |
answerStream := CharacterWriteStream new:20. |
|
1059 |
[inStream atEnd] whileFalse:[ |
|
1060 |
char := inStream next. |
|
1061 |
answerStream nextPut:char. |
|
1062 |
(aCharacter == char) ifTrue:[ |
|
1063 |
^ answerStream contents |
|
1064 |
]. |
|
1065 |
]. |
|
1066 |
^ answerStream contents |
|
1067 |
||
1068 |
"Created: / 27-03-2019 / 13:42:00 / Claus Gittinger" |
|
1069 |
! |
|
1070 |
||
1071 |
throughAny:aCollection in:inStream |
|
1072 |
"read all characters through any in aCollection and return a |
|
1073 |
dense strings for it (i.e. String/TwoByteString, if possible). |
|
1074 |
This helper is present because when reading from a unicode32 stream, |
|
1075 |
dense strings are preferred. |
|
1076 |
If inStream is known to return single byte characters, |
|
1077 |
you should better use inStream throughAny:aCollection" |
|
1078 |
||
1079 |
|answerStream char| |
|
1080 |
||
1081 |
answerStream := CharacterWriteStream new:20. |
|
1082 |
[inStream atEnd] whileFalse:[ |
|
1083 |
char := inStream next. |
|
1084 |
answerStream nextPut:char. |
|
1085 |
(aCollection includes:char) ifTrue:[ |
|
1086 |
^ answerStream contents |
|
1087 |
]. |
|
1088 |
]. |
|
1089 |
^ answerStream contents |
|
1090 |
||
1091 |
"Created: / 27-03-2019 / 13:40:49 / Claus Gittinger" |
|
1092 |
! |
|
1093 |
||
22525 | 1094 |
withoutAmpersandEscapes:label |
1095 |
"remove single ampersands; |
|
1096 |
replace double ampersands by single ones. |
|
1097 |
This is used to unescape menu-labels |
|
1098 |
(which use the ampersand as shortKey marker)" |
|
1099 |
||
1100 |
^ String |
|
1101 |
streamContents:[:out | |
|
1102 |
|in ch next| |
|
1103 |
||
1104 |
in := label readStream. |
|
1105 |
[in atEnd] whileFalse:[ |
|
1106 |
ch := in next. |
|
1107 |
ch == $& ifTrue:[ |
|
1108 |
(next := in peek) == $& ifTrue:[ |
|
1109 |
in next. |
|
1110 |
out nextPut:ch |
|
1111 |
] ifFalse:[ |
|
1112 |
ch := next |
|
1113 |
]. |
|
1114 |
] ifFalse:[ |
|
1115 |
out nextPut:ch |
|
1116 |
]. |
|
1117 |
] |
|
1118 |
] |
|
1119 |
||
1120 |
" |
|
1121 |
String withoutAmpersandEscapes:'' |
|
1122 |
String withoutAmpersandEscapes:'a' |
|
1123 |
String withoutAmpersandEscapes:'abcd' |
|
1124 |
String withoutAmpersandEscapes:'&abcd' |
|
1125 |
String withoutAmpersandEscapes:'&abcd&' |
|
1126 |
String withoutAmpersandEscapes:'&a&b&c&d' |
|
1127 |
String withoutAmpersandEscapes:'&a&b&c&d&' |
|
1128 |
String withoutAmpersandEscapes:'&&a&&b&&c&&d&&' |
|
1129 |
String withoutAmpersandEscapes:'&&a&&b&&c&&d&' |
|
1130 |
String withoutAmpersandEscapes:'&a&&b&&c&&d&' |
|
1131 |
String withoutAmpersandEscapes:'a&&b&&c&&d' |
|
1132 |
" |
|
1133 |
! ! |
|
1134 |
||
9192 | 1135 |
!CharacterArray methodsFor:'Compatibility-ANSI'! |
1136 |
||
1137 |
addLineDelimiters |
|
1138 |
"Ansi compatibility - same as withCRs" |
|
1139 |
||
1140 |
^ self withCRs |
|
1141 |
||
1142 |
"Modified: / 13.11.2001 / 19:16:25 / cg" |
|
17543 | 1143 |
! ! |
1144 |
||
1145 |
!CharacterArray methodsFor:'Compatibility-Dolphin'! |
|
9192 | 1146 |
|
1147 |
copyExpanding:expandTable |
|
1148 |
"return a copy of myself, with translations from the expandTable sliced in. |
|
18392 | 1149 |
The argument is supposed to map from characters to either characters or strings. |
1150 |
Added for Dolphin compatibility" |
|
9192 | 1151 |
|
1152 |
|ds| |
|
1153 |
||
1154 |
ds := WriteStream on:(self species new). |
|
1155 |
self do:[:eachChar | |
|
20874 | 1156 |
|repl| |
1157 |
||
1158 |
repl := expandTable at:eachChar ifAbsent:[nil]. |
|
1159 |
repl isNil ifTrue:[ |
|
1160 |
ds nextPut:eachChar |
|
1161 |
] ifFalse:[ |
|
1162 |
repl size == 0 ifTrue:[ |
|
1163 |
ds nextPut:repl |
|
1164 |
] ifFalse:[ |
|
1165 |
ds nextPutAll:repl |
|
1166 |
] |
|
1167 |
]. |
|
9192 | 1168 |
]. |
6870 | 1169 |
^ ds contents. |
24036 | 1170 |
|
1171 |
" |
|
1172 |
'hello' copyExpanding:(Dictionary withKeys:{$h . $e . $o} andValues:{'HH' . 'EE' . $O }) |
|
1173 |
" |
|
1174 |
||
1175 |
"Modified (format): / 02-04-2019 / 10:59:59 / Claus Gittinger" |
|
6870 | 1176 |
! |
1177 |
||
1178 |
formatWith:aString |
|
11955 | 1179 |
"Compatibility method - do not use in new code. |
18392 | 1180 |
same as #bindWith: for Dolphin compatibility" |
6870 | 1181 |
|
1182 |
^ self bindWith:aString |
|
1183 |
||
1184 |
" |
|
1185 |
'hello%1world' formatWith:'123' |
|
1186 |
" |
|
1187 |
! |
|
1188 |
||
1189 |
formatWith:arg1 with:arg2 |
|
11955 | 1190 |
"Compatibility method - do not use in new code. |
18392 | 1191 |
same as #bindWith: for Dolphin compatibility" |
6870 | 1192 |
|
1193 |
^ self bindWith:arg1 with:arg2 |
|
1194 |
||
1195 |
" |
|
1196 |
'hello%1 %2world' formatWith:'123' with:234 |
|
1197 |
" |
|
1198 |
! |
|
1199 |
||
1200 |
formatWith:arg1 with:arg2 with:arg3 |
|
11955 | 1201 |
"Compatibility method - do not use in new code. |
18392 | 1202 |
same as #bindWith: for Dolphin compatibility" |
6870 | 1203 |
|
1204 |
^ self bindWith:arg1 with:arg2 with:arg3 |
|
1205 |
||
1206 |
" |
|
1207 |
'hello%1 %2 %3world' formatWith:'123' with:234 with:345 |
|
1208 |
" |
|
6391 | 1209 |
! ! |
1210 |
||
14681 | 1211 |
!CharacterArray methodsFor:'Compatibility-GNU'! |
1212 |
||
1213 |
% anArrayOfOperands |
|
1214 |
"return a copy of the receiver, where a '%i' escape |
|
21747 | 1215 |
is replaced by the corresponding string from the argument array. |
14681 | 1216 |
'i' may be between 1 and 9 (i.e. a maximum of 9 placeholders is allowed). |
1217 |
Added for GNU-ST compatibility." |
|
1218 |
||
1219 |
^ self expandPlaceholdersWith:anArrayOfOperands |
|
1220 |
||
1221 |
" |
|
1222 |
'do you prefer %1 or rather %2 (not talking about %3) ?' |
|
20874 | 1223 |
% #('smalltalk' 'c++' 'c') |
14681 | 1224 |
|
1225 |
'do you %(what) ?' |
|
20874 | 1226 |
% (Dictionary new at:#'what' put:'understand'; yourself) |
14681 | 1227 |
" |
21747 | 1228 |
|
1229 |
"Modified (comment): / 11-05-2017 / 12:43:08 / mawalch" |
|
14681 | 1230 |
! ! |
1231 |
||
7261 | 1232 |
!CharacterArray methodsFor:'Compatibility-ST/V'! |
608 | 1233 |
|
17517 | 1234 |
asArrayOfSubstrings |
1235 |
"return an array of substrings from the receiver, interpreting |
|
1236 |
separators (i.e. spaces & newlines) as word-delimiters. |
|
18390 | 1237 |
This has been added for ST/V compatibility |
1238 |
- the actual work is done in asCollectionOfWords. |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
1239 |
(sigh: it is called #'subStrings' in V'Age, #'substrings' in Squeak |
17517 | 1240 |
and #'asCollectionOfWords' in ST/X) " |
1241 |
||
1242 |
^ self asCollectionOfWords asArray |
|
1243 |
||
1244 |
" |
|
1245 |
'1 one two three four 5 five' asArrayOfSubstrings |
|
1246 |
'1 |
|
1247 |
one |
|
20874 | 1248 |
two three four 5 five' asArrayOfSubstrings |
17517 | 1249 |
" |
1250 |
! |
|
1251 |
||
19497 | 1252 |
equalsIgnoreCase:aString |
1253 |
"This is an ST/V compatibility method and an alias for sameAs:." |
|
1254 |
||
1255 |
^ self sameAs:aString |
|
1256 |
||
1257 |
" |
|
1258 |
'abc' equalsIgnoreCase: 'aBC' |
|
1259 |
" |
|
1260 |
! |
|
1261 |
||
608 | 1262 |
replChar:oldChar with:newChar |
19497 | 1263 |
"return a copy of the receiver, with all oldChars replaced by newChar. |
1264 |
This is an ST/V compatibility method and an alias for copyReplaceAll." |
|
608 | 1265 |
|
3656 | 1266 |
^ self copyReplaceAll:oldChar with:newChar |
608 | 1267 |
|
1268 |
" |
|
8874 | 1269 |
'12345678901234567890' replChar:$0 with:$* |
608 | 1270 |
" |
3656 | 1271 |
|
1272 |
"Modified: / 18.7.1998 / 22:52:57 / cg" |
|
608 | 1273 |
! |
1274 |
||
1275 |
replChar:oldChar withString:newString |
|
1276 |
"return a copy of the receiver, with all oldChars replaced |
|
1277 |
by newString (i.e. slice in the newString in place of the oldChar). |
|
1278 |
This is an ST/V compatibility method." |
|
1279 |
||
1280 |
|tmpStream| |
|
1281 |
||
15523 | 1282 |
tmpStream := self species writeStream. |
608 | 1283 |
self do:[:element | |
20874 | 1284 |
element = oldChar ifTrue:[ |
1285 |
tmpStream nextPutAll:newString |
|
1286 |
] ifFalse:[ |
|
1287 |
tmpStream nextPut:element |
|
1288 |
]. |
|
608 | 1289 |
]. |
1290 |
^ tmpStream contents |
|
1291 |
||
1292 |
" |
|
8874 | 1293 |
'12345678901234567890' replChar:$0 withString:'foo' |
1294 |
'a string with spaces' replChar:$ withString:' foo ' |
|
608 | 1295 |
" |
9192 | 1296 |
! |
1297 |
||
1298 |
replString:subString withString:newString |
|
4244
5deefa3144fb
added #replString:withString:
Claus Gittinger <cg@exept.de>
parents:
4156
diff
changeset
|
1299 |
"return a copy of the receiver, with all sequences of subString replaced |
18390 | 1300 |
by newString (i.e. slice in the newString in place of the oldString). |
19497 | 1301 |
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
|
1302 |
|
8347 | 1303 |
^ self copyReplaceString:subString withString:newString |
4244
5deefa3144fb
added #replString:withString:
Claus Gittinger <cg@exept.de>
parents:
4156
diff
changeset
|
1304 |
|
5deefa3144fb
added #replString:withString:
Claus Gittinger <cg@exept.de>
parents:
4156
diff
changeset
|
1305 |
" |
8874 | 1306 |
'12345678901234567890' replString:'123' withString:'OneTwoThree' |
1307 |
'12345678901234567890' replString:'123' withString:'*' |
|
1308 |
'12345678901234567890' replString:'234' withString:'foo' |
|
4244
5deefa3144fb
added #replString:withString:
Claus Gittinger <cg@exept.de>
parents:
4156
diff
changeset
|
1309 |
|
5deefa3144fb
added #replString:withString:
Claus Gittinger <cg@exept.de>
parents:
4156
diff
changeset
|
1310 |
('a string with spaces' replChar:$ withString:' foo ') |
20874 | 1311 |
replString:'foo' withString:'bar' |
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 |
|
8347 | 1314 |
"Modified: / 12-05-2004 / 12:00:27 / cg" |
9192 | 1315 |
! |
1316 |
||
9275 | 1317 |
subString:start to:end |
18390 | 1318 |
"same as copyFrom:to: |
19497 | 1319 |
This is an ST/V compatibility method and an alias for copyFrom:to:." |
18390 | 1320 |
|
9275 | 1321 |
^ self copyFrom:start to:end |
1322 |
||
1323 |
" |
|
1324 |
'12345678901234567890' subString:3 to:8 |
|
1325 |
" |
|
1326 |
! |
|
1327 |
||
9192 | 1328 |
trimBlanks |
14992 | 1329 |
"return a copy of the receiver without leading and trailing spaces. |
18464 | 1330 |
This is an ST/V compatibility method and an alias for withoutSpaces." |
9192 | 1331 |
|
1332 |
^ self withoutSpaces |
|
1333 |
||
1334 |
" |
|
1335 |
' spaces at beginning' trimBlanks |
|
1336 |
'spaces at end ' trimBlanks |
|
1337 |
' spaces at beginning and end ' trimBlanks |
|
1338 |
'no spaces' trimBlanks |
|
1339 |
" |
|
1340 |
! ! |
|
1341 |
||
1342 |
!CharacterArray methodsFor:'Compatibility-Squeak'! |
|
4409
86e049b21f8d
added #includesSubstring:caseSensitive:
Claus Gittinger <cg@exept.de>
parents:
4389
diff
changeset
|
1343 |
|
19171 | 1344 |
asBoldText |
1345 |
"return self as a bold text" |
|
1346 |
||
21843 | 1347 |
"this test allows for small non-gui apps to be built without libbasic2 (where Text is)" |
1348 |
Text isNil ifTrue:[^ self]. |
|
1349 |
^ Text string: self emphasis: #bold |
|
1350 |
||
1351 |
"Modified (comment): / 20-06-2017 / 08:23:49 / cg" |
|
19171 | 1352 |
! |
1353 |
||
6148 | 1354 |
asDate |
1355 |
"Many allowed forms, see Date.readFrom:" |
|
1356 |
||
1357 |
^ Date readFrom: (ReadStream on: self) |
|
1358 |
||
1359 |
" |
|
8874 | 1360 |
'30 Apr 1999' asDate dayName capitalized |
6148 | 1361 |
" |
1362 |
! |
|
1363 |
||
10703
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
1364 |
findDelimiters:delimiters startingAt:start |
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
1365 |
"Answer the index of the character within the receiver, starting at start, |
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
1366 |
that matches one of the delimiters. |
18391 | 1367 |
If the receiver does not contain any of the delimiters, answer size + 1." |
9192 | 1368 |
|
1369 |
|idx| |
|
1370 |
||
1371 |
idx := self indexOfAny:delimiters startingAt:start. |
|
1372 |
idx == 0 ifTrue:[^ self size + 1]. |
|
1373 |
^ idx. |
|
1374 |
"/start to: self size do: [:i | |
|
1375 |
"/ |char| |
|
1376 |
"/ |
|
1377 |
"/ char := self at: i. |
|
1378 |
"/ delimiters do: [:delim | delim = char ifTrue: [^ i]] |
|
8971 | 1379 |
"/ ]. |
1380 |
"/ ^ self size + 1 |
|
1381 |
! |
|
1382 |
||
22005 | 1383 |
findString:key startingAt:start caseSensitive:caseSensitive |
1384 |
^ self indexOfSubCollection:key startingAt:start ifAbsent:[0] caseSensitive:caseSensitive |
|
1385 |
||
1386 |
"Created: / 13-07-2017 / 12:44:50 / cg" |
|
1387 |
! |
|
1388 |
||
13624 | 1389 |
findTokens:delimiterOrDelimiters |
7270 | 1390 |
"cg: I am not sure, if this is really the squeak semantics (w.r.t. empty fields)" |
1391 |
||
13624 | 1392 |
delimiterOrDelimiters size == 0 ifTrue:[ |
20874 | 1393 |
^ self asCollectionOfSubstringsSeparatedBy:delimiterOrDelimiters |
13624 | 1394 |
] ifFalse:[ |
20874 | 1395 |
^ self asCollectionOfSubstringsSeparatedByAny:delimiterOrDelimiters |
13624 | 1396 |
]. |
7270 | 1397 |
|
1398 |
" |
|
8874 | 1399 |
'a|b#c||e' findTokens:#($# $|) |
13624 | 1400 |
'a|b#c||e' findTokens:$| |
1401 |
" |
|
1402 |
||
1403 |
"Modified: / 04-09-2011 / 20:31:21 / cg" |
|
1404 |
"Modified (comment): / 05-09-2011 / 04:25:28 / cg" |
|
4981 | 1405 |
! |
1406 |
||
4974 | 1407 |
includesSubString:aString |
1408 |
"return true, if a substring is contained in the receiver. |
|
1409 |
The compare is case sensitive." |
|
1410 |
||
1411 |
^ self includesString:aString |
|
1412 |
||
1413 |
" |
|
8874 | 1414 |
'hello world' includesSubString:'Hel' |
9192 | 1415 |
'hello world' includesSubString:'hel' |
1416 |
'hello world' includesSubString:'llo' |
|
1417 |
" |
|
1418 |
||
1419 |
||
1420 |
||
1421 |
! |
|
1422 |
||
11178 | 1423 |
includesSubString:aString caseSensitive:caseSensitive |
18391 | 1424 |
"sigh - an alias; added for Squeak/Pharo compatibility" |
1425 |
||
11179 | 1426 |
^ self includesSubstring:aString caseSensitive:caseSensitive |
11178 | 1427 |
! |
1428 |
||
16875
32e61bb3a742
class: CharacterArray>>#includesSubstring: (for Squeak compatibility)
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
16863
diff
changeset
|
1429 |
includesSubstring: aString |
18391 | 1430 |
"sigh - an alias; added for Squeak/Pharo compatibility" |
1431 |
||
16875
32e61bb3a742
class: CharacterArray>>#includesSubstring: (for Squeak compatibility)
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
16863
diff
changeset
|
1432 |
^ self includesSubString: aString |
32e61bb3a742
class: CharacterArray>>#includesSubstring: (for Squeak compatibility)
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
16863
diff
changeset
|
1433 |
|
32e61bb3a742
class: CharacterArray>>#includesSubstring: (for Squeak compatibility)
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
16863
diff
changeset
|
1434 |
"Created: / 03-10-2014 / 02:47:52 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
32e61bb3a742
class: CharacterArray>>#includesSubstring: (for Squeak compatibility)
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
16863
diff
changeset
|
1435 |
! |
32e61bb3a742
class: CharacterArray>>#includesSubstring: (for Squeak compatibility)
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
16863
diff
changeset
|
1436 |
|
9192 | 1437 |
includesSubstring:aString caseSensitive:caseSensitive |
9190 | 1438 |
"return true, if a substring is contained in the receiver. |
9192 | 1439 |
The argument, caseSensitive controls if case is ignored in the compare." |
1440 |
||
1441 |
"/ for now, a q&d hack ... |
|
1442 |
||
1443 |
caseSensitive ifFalse:[ |
|
20874 | 1444 |
^ self asLowercase includesString:aString asLowercase |
9192 | 1445 |
]. |
9190 | 1446 |
^ self includesString:aString |
1447 |
||
1448 |
" |
|
9192 | 1449 |
'hello world' includesSubstring:'Hel' caseSensitive:true |
1450 |
'hello world' includesSubstring:'Hel' caseSensitive:false |
|
1451 |
" |
|
1452 |
||
1453 |
||
1454 |
||
1455 |
! |
|
9190 | 1456 |
|
6979 | 1457 |
isAllDigits |
1458 |
"Answer whether the receiver's characters are all digits" |
|
1459 |
||
1460 |
^ self conform:[:eachChar | eachChar isDigit] |
|
1461 |
||
1462 |
" |
|
1463 |
'hello world' isAllDigits |
|
1464 |
'12344' isAllDigits |
|
1465 |
" |
|
1466 |
! |
|
1467 |
||
4890 | 1468 |
lastSpacePosition |
18391 | 1469 |
"return the index of the last space character; 0 if there is none. |
1470 |
Added for Squeak/Pharo compatibility" |
|
14405
1d0d99a95bba
comment/format in:7 methods
Claus Gittinger <cg@exept.de>
parents:
14168
diff
changeset
|
1471 |
|
20739 | 1472 |
"/ CG: is this correct? separator or space only??? |
4890 | 1473 |
^ self lastIndexOfSeparator |
1474 |
! |
|
1475 |
||
20738 | 1476 |
linesDo:aBlock |
1477 |
"evaluate the argument, aBlock for all lines, |
|
1478 |
up to the end" |
|
1479 |
||
1480 |
^ self readStream linesDo:aBlock |
|
1481 |
! |
|
1482 |
||
7022 | 1483 |
padded:leftOrRight to:paddedSize with:padCharacter |
20155 | 1484 |
"pad left (leftOrRight == #left) or right" |
14053
acbb97913c30
comment/format in: #padded:to:with:
Claus Gittinger <cg@exept.de>
parents:
14050
diff
changeset
|
1485 |
|
7022 | 1486 |
leftOrRight == #left ifTrue:[ |
20155 | 1487 |
^ self leftPaddedTo:paddedSize with:padCharacter |
9190 | 1488 |
]. |
1489 |
^ self paddedTo:paddedSize with:padCharacter |
|
1490 |
||
1491 |
" |
|
8874 | 1492 |
'hello' padded:#right to:10 with:$. |
9192 | 1493 |
'hello' padded:#left to:10 with:$. |
1494 |
" |
|
14053
acbb97913c30
comment/format in: #padded:to:with:
Claus Gittinger <cg@exept.de>
parents:
14050
diff
changeset
|
1495 |
|
acbb97913c30
comment/format in: #padded:to:with:
Claus Gittinger <cg@exept.de>
parents:
14050
diff
changeset
|
1496 |
"Modified (comment): / 07-03-2012 / 16:31:33 / cg" |
9192 | 1497 |
! |
1498 |
||
1499 |
skipDelimiters:delimiters startingAt:start |
|
1500 |
"Answer the index of the character within the receiver, starting at start, |
|
1501 |
that does NOT match one of the delimiters. |
|
1502 |
If the receiver does not contain any of the delimiters, answer size + 1. |
|
1503 |
Assumes the delimiters to be a non-empty string." |
|
1504 |
||
1505 |
start to:self size do:[:i | |
|
20874 | 1506 |
delimiters detect:[:delim | delim = (self at:i) ] ifNone:[ ^ i ] |
9192 | 1507 |
]. |
1508 |
^ self size + 1 |
|
1509 |
||
1510 |
" |
|
1511 |
'123***7890' skipDelimiters:'*' startingAt:4 |
|
1512 |
'123***7890' skipDelimiters:'*' startingAt:3 |
|
9190 | 1513 |
'123***7890' skipDelimiters:'*' startingAt:10 |
1514 |
'123*******' skipDelimiters:'*' startingAt:10 |
|
8874 | 1515 |
" |
1516 |
! |
|
1517 |
||
9190 | 1518 |
substrings |
18921 | 1519 |
"return a collection consisting of all words contained in the receiver. |
17517 | 1520 |
Words are separated by whitespace. |
1521 |
This has been added for Squeak compatibility. |
|
1522 |
(sigh: it is called #'subStrings' in V'Age, and #'asCollectionOfWords' in ST/X) " |
|
1523 |
||
9190 | 1524 |
^ self asCollectionOfWords |
1525 |
||
1526 |
" |
|
1527 |
'foo bar baz' substrings |
|
1528 |
" |
|
1529 |
! |
|
1530 |
||
12665
b027fc5aec4e
added: #substringsSeparatedBy:
Claus Gittinger <cg@exept.de>
parents:
12664
diff
changeset
|
1531 |
substringsSeparatedBy:separatorCharacter |
18921 | 1532 |
"return a collection consisting of all words contained in the receiver. |
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
1533 |
Words are separated by the given separator character. |
18921 | 1534 |
This has been added for Squeak/Pharo compatibility. |
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
1535 |
(sigh: it is called #'subStrings:' in V'Age, |
18921 | 1536 |
and #'asCollectionOfSubstringsSeparatedBy' in ST/X) " |
18391 | 1537 |
|
12665
b027fc5aec4e
added: #substringsSeparatedBy:
Claus Gittinger <cg@exept.de>
parents:
12664
diff
changeset
|
1538 |
^ self asCollectionOfSubstringsSeparatedBy:separatorCharacter |
b027fc5aec4e
added: #substringsSeparatedBy:
Claus Gittinger <cg@exept.de>
parents:
12664
diff
changeset
|
1539 |
|
b027fc5aec4e
added: #substringsSeparatedBy:
Claus Gittinger <cg@exept.de>
parents:
12664
diff
changeset
|
1540 |
" |
b027fc5aec4e
added: #substringsSeparatedBy:
Claus Gittinger <cg@exept.de>
parents:
12664
diff
changeset
|
1541 |
'foo bar, baz' substringsSeparatedBy:$, |
21151 | 1542 |
'1.2.3.4' substringsSeparatedBy:$. |
12665
b027fc5aec4e
added: #substringsSeparatedBy:
Claus Gittinger <cg@exept.de>
parents:
12664
diff
changeset
|
1543 |
" |
b027fc5aec4e
added: #substringsSeparatedBy:
Claus Gittinger <cg@exept.de>
parents:
12664
diff
changeset
|
1544 |
! |
b027fc5aec4e
added: #substringsSeparatedBy:
Claus Gittinger <cg@exept.de>
parents:
12664
diff
changeset
|
1545 |
|
23188 | 1546 |
trimBoth |
1547 |
"return a copy of the receiver without leading and trailing whiteSpace. |
|
1548 |
Added for Squeak compatibility (an alias for withoutSeparators)" |
|
1549 |
||
1550 |
^ self withoutSeparators |
|
1551 |
||
1552 |
"Created: / 03-07-2018 / 11:08:29 / Claus Gittinger" |
|
1553 |
! |
|
1554 |
||
9190 | 1555 |
truncateTo:smallSize |
9192 | 1556 |
"return myself or a copy shortened to smallSize. 1/18/96 sw" |
1557 |
||
1558 |
self size <= smallSize ifTrue:[^ self]. |
|
1559 |
^ self copyFrom: 1 to: smallSize |
|
1560 |
||
1561 |
" |
|
1562 |
'hello world' truncateTo:5 |
|
1563 |
'hello' truncateTo:10 |
|
1564 |
||
1565 |
'hello world' copyTo:5 |
|
1566 |
'hello' copyTo:10 |
|
1567 |
" |
|
1568 |
! |
|
5795 | 1569 |
|
4890 | 1570 |
withBlanksTrimmed |
18464 | 1571 |
"Return a copy of the receiver from which leading and trailing whitespace have been trimmed. |
1572 |
Notice the bad naming - it is trimming separators, not just blanks. |
|
1573 |
Added for Squeak compatibility and an alias for withoutSeparators" |
|
4890 | 1574 |
|
14865 | 1575 |
^ self withoutSeparators "withoutSpaces" "/ cg: it seems that squeak cares for any whitespace |
4890 | 1576 |
|
1577 |
" |
|
8874 | 1578 |
' hello world ' withBlanksTrimmed |
4890 | 1579 |
" |
1580 |
! |
|
1581 |
||
12188 | 1582 |
withoutLeading: char |
1583 |
"return a copy of myself without leading a char. |
|
1584 |
Returns an empty string, if the receiver consist only of a char." |
|
1585 |
||
1586 |
^ self withoutLeadingForWhich:[:ch | ch = char] |
|
1587 |
||
1588 |
" |
|
13882
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
1589 |
'****foo****' withoutLeading: $* |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
1590 |
'foo****' withoutLeading: $* |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
1591 |
'*' withoutLeading: $* |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
1592 |
'' 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 |
'*******' withoutLeading: $* |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
1595 |
'foo' withoutLeading: $* |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
1596 |
'f***o***o' withoutLeading: $* |
12188 | 1597 |
('**' , Character tab asString , '*foo***') withoutLeading: $* inspect |
1598 |
" |
|
1599 |
! |
|
1600 |
||
11565 | 1601 |
withoutTrailing:char |
1602 |
"return a copy of myself without trailing char. |
|
1603 |
Returns an empty string, if the receiver consist only of char." |
|
1604 |
||
12188 | 1605 |
^ self withoutTrailingForWhich:[:ch | ch = char] |
1606 |
||
1607 |
" |
|
13882
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
1608 |
' foo....' withoutTrailing:$. |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
1609 |
'foo....' withoutTrailing:$. |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
1610 |
' foo' withoutTrailing:$. |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
1611 |
'.......' withoutTrailing:$. |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
1612 |
'foo' withoutTrailing:$. |
11565 | 1613 |
" |
9190 | 1614 |
! ! |
1615 |
||
1616 |
!CharacterArray methodsFor:'Compatibility-V''Age'! |
|
1617 |
||
1618 |
addLineDelimiter |
|
1619 |
"replace all '\'-characters by line delimiter (cr) - characters. |
|
9192 | 1620 |
This has been added for VisualAge compatibility." |
1621 |
||
1622 |
^ self withCRs |
|
1623 |
! |
|
1624 |
||
1625 |
bindWith:aString |
|
1626 |
"return a copy of the receiver, where a '%1' escape is |
|
1627 |
replaced by aString. |
|
1628 |
This has been added for VisualAge compatibility." |
|
1629 |
||
24038 | 1630 |
^ self expandPlaceholdersWith:{ aString } |
9192 | 1631 |
|
1632 |
" |
|
1633 |
'do you like %1 ?' bindWith:'smalltalk' |
|
19785 | 1634 |
'do you like %(foo) ?' bindWithArguments:(Dictionary new at:'foo' put:'smalltalk'; yourself) |
9192 | 1635 |
" |
24038 | 1636 |
|
1637 |
"Modified (format): / 02-04-2019 / 14:05:22 / Claus Gittinger" |
|
9192 | 1638 |
! |
608 | 1639 |
|
1640 |
bindWith:string1 with:string2 |
|
1641 |
"return a copy of the receiver, where a '%1' escape is |
|
1642 |
replaced by string1 and '%2' is replaced by string2. |
|
1643 |
This has been added for VisualAge compatibility." |
|
1644 |
||
24038 | 1645 |
^ self expandPlaceholdersWith:{string1 . string2} |
608 | 1646 |
|
1647 |
" |
|
1648 |
'do you prefer %1 or rather %2 ?' |
|
20874 | 1649 |
bindWith:'smalltalk' with:'c++' |
9192 | 1650 |
" |
24038 | 1651 |
|
1652 |
"Modified (format): / 02-04-2019 / 14:05:32 / Claus Gittinger" |
|
9192 | 1653 |
! |
9190 | 1654 |
|
1655 |
bindWith:str1 with:str2 with:str3 |
|
608 | 1656 |
"return a copy of the receiver, where a '%1', '%2' and '%3' escapes |
1657 |
are replaced by str1, str2 and str3 respectively. |
|
1658 |
This has been added for VisualAge compatibility." |
|
1659 |
||
24038 | 1660 |
^ self expandPlaceholdersWith:{ str1 . str2 . str3 } |
608 | 1661 |
|
1662 |
" |
|
9192 | 1663 |
'do you prefer %1 or rather %2 (not talking about %3) ?' |
20874 | 1664 |
bindWith:'smalltalk' with:'c++' with:'c' |
9192 | 1665 |
" |
24038 | 1666 |
|
1667 |
"Modified (format): / 02-04-2019 / 14:05:45 / Claus Gittinger" |
|
9192 | 1668 |
! |
608 | 1669 |
|
1670 |
bindWith:str1 with:str2 with:str3 with:str4 |
|
1671 |
"return a copy of the receiver, where a '%1', '%2', '%3' and '%4' escapes |
|
1672 |
are replaced by str1, str2, str3 and str4 respectively. |
|
1673 |
This has been added for VisualAge compatibility." |
|
1674 |
||
24038 | 1675 |
^ self expandPlaceholdersWith:{ str1 . str2 . str3 . str4 } |
608 | 1676 |
|
1677 |
" |
|
9192 | 1678 |
'do you prefer %1 or rather %2 (not talking about %3 or even %4) ?' |
20874 | 1679 |
bindWith:'smalltalk' with:'c++' with:'c' with:'assembler' |
9192 | 1680 |
" |
24038 | 1681 |
|
1682 |
"Modified (format): / 02-04-2019 / 14:06:01 / Claus Gittinger" |
|
9192 | 1683 |
! |
608 | 1684 |
|
2344 | 1685 |
bindWith:str1 with:str2 with:str3 with:str4 with:str5 |
1686 |
"return a copy of the receiver, where a '%1' .. '%5' escapes |
|
1687 |
are replaced by str1 .. str5 respectively. |
|
1688 |
This has been added for VisualAge compatibility." |
|
1689 |
||
24038 | 1690 |
^ self expandPlaceholdersWith:{ str1 . str2 . str3 . str4 . str5 } |
1691 |
||
1692 |
"Created: / 31-01-1997 / 16:25:42 / cg" |
|
1693 |
"Modified: / 02-04-2019 / 14:06:17 / Claus Gittinger" |
|
2344 | 1694 |
! |
1695 |
||
5343 | 1696 |
bindWith:str1 with:str2 with:str3 with:str4 with:str5 with:str6 |
1697 |
"return a copy of the receiver, where a '%1' .. '%6' escapes |
|
11524 | 1698 |
are replaced by str1 .. str6 respectively. |
5343 | 1699 |
This has been added for VisualAge compatibility." |
1700 |
||
24038 | 1701 |
^ self expandPlaceholdersWith:{ str1 . str2 . str3 . str4 . str5 . str6 } |
1702 |
||
1703 |
"Modified (format): / 02-04-2019 / 14:06:36 / Claus Gittinger" |
|
11524 | 1704 |
! |
1705 |
||
1706 |
bindWith:str1 with:str2 with:str3 with:str4 with:str5 with:str6 with:str7 |
|
1707 |
"return a copy of the receiver, where a '%1' .. '%7' escapes |
|
1708 |
are replaced by str1 .. str7 respectively. |
|
1709 |
This has been added for VisualAge compatibility." |
|
1710 |
||
24038 | 1711 |
^ self expandPlaceholdersWith:{ str1 . str2 . str3 . str4 . str5 . str6 . str7 } |
1712 |
||
1713 |
"Modified (format): / 02-04-2019 / 14:06:59 / Claus Gittinger" |
|
5343 | 1714 |
! |
1715 |
||
13996
a86faa4f17b8
added: #bindWith:with:with:with:with:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
13987
diff
changeset
|
1716 |
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
|
1717 |
"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
|
1718 |
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
|
1719 |
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
|
1720 |
|
24038 | 1721 |
^ 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
|
1722 |
|
a86faa4f17b8
added: #bindWith:with:with:with:with:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
13987
diff
changeset
|
1723 |
"Created: / 06-02-2012 / 10:33:18 / cg" |
24038 | 1724 |
"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
|
1725 |
! |
a86faa4f17b8
added: #bindWith:with:with:with:with:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
13987
diff
changeset
|
1726 |
|
14014
3aff2d7c41c8
added: #bindWith:with:with:with:with:with:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
13996
diff
changeset
|
1727 |
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
|
1728 |
"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
|
1729 |
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
|
1730 |
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
|
1731 |
|
24038 | 1732 |
^ 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
|
1733 |
|
3aff2d7c41c8
added: #bindWith:with:with:with:with:with:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
13996
diff
changeset
|
1734 |
"Created: / 14-02-2012 / 17:42:31 / cg" |
24038 | 1735 |
"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
|
1736 |
! |
3aff2d7c41c8
added: #bindWith:with:with:with:with:with:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
13996
diff
changeset
|
1737 |
|
22924 | 1738 |
bindWithArguments:argumentsCollection |
608 | 1739 |
"return a copy of the receiver, where a '%i' escape |
21747 | 1740 |
is replaced by the corresponding string from the argument array. |
22924 | 1741 |
'i' may be between 1 and 9 (i.e. a maximum of 9 placeholders is allowed) |
1742 |
or %(key); the argumentsCollection must then be a dictionary. |
|
21255 | 1743 |
To get an integer-indexed placeHolder followed by another digit, |
1744 |
or an index > 9, you must use %(digit). |
|
608 | 1745 |
This has been added for VisualAge compatibility." |
1746 |
||
22924 | 1747 |
^ self expandPlaceholdersWith:argumentsCollection |
9192 | 1748 |
|
1749 |
" |
|
1750 |
'do you prefer %1 or rather %2 (not talking about %3) ?' |
|
20874 | 1751 |
bindWithArguments:#('smalltalk' 'c++' 'c') |
12196 | 1752 |
|
1753 |
'do you %(what) ?' |
|
20874 | 1754 |
bindWithArguments:(Dictionary new at:#'what' put:'understand'; yourself) |
9192 | 1755 |
" |
21747 | 1756 |
|
1757 |
"Modified (comment): / 11-05-2017 / 12:42:57 / mawalch" |
|
9192 | 1758 |
! |
608 | 1759 |
|
1760 |
subStrings |
|
18921 | 1761 |
"return a collection consisting of all words contained in the receiver. |
608 | 1762 |
Words are separated by whitespace. |
17517 | 1763 |
This has been added for VisualAge compatibility. |
1764 |
(sigh: it is called #'subbtrings' in Squeak, and #'asCollectionOfWords' in ST/X) " |
|
608 | 1765 |
|
1766 |
^ self asCollectionOfWords |
|
1767 |
||
1768 |
" |
|
1769 |
'hello world, this is smalltalk' subStrings |
|
1770 |
" |
|
1771 |
! |
|
1772 |
||
16181 | 1773 |
subStrings:separatorCharacterOrString |
18921 | 1774 |
"return a collection consisting of all words contained in the receiver. |
608 | 1775 |
Words are separated by separatorCharacter. |
18921 | 1776 |
This is similar to split: (squeak) and asCollectionOfSubstringsSeparatedBy: (st/x) |
1777 |
and has been added for VisualAge compatibility." |
|
608 | 1778 |
|
16181 | 1779 |
separatorCharacterOrString isCharacter ifTrue:[ |
20874 | 1780 |
^ self asCollectionOfSubstringsSeparatedBy:separatorCharacterOrString |
16181 | 1781 |
]. |
1782 |
^ self asCollectionOfSubstringsSeparatedByAny:separatorCharacterOrString |
|
1783 |
||
1784 |
" |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
1785 |
'foo:bar:baz:smalltalk' subStrings:$: |
16181 | 1786 |
'foo:bar:baz:smalltalk' subStrings:':' |
1787 |
'foo.bar,baz-smalltalk' subStrings:'.,-' |
|
9192 | 1788 |
" |
1789 |
! |
|
1790 |
||
1791 |
trimSeparators |
|
18392 | 1792 |
"return a copy of the receiver without leading and trailing whiteSpace. |
18921 | 1793 |
Added for VisualAge compatibility (an alias for withoutSeparators)" |
9192 | 1794 |
|
1795 |
^ self withoutSeparators |
|
1796 |
! ! |
|
1797 |
||
1798 |
!CharacterArray methodsFor:'Compatibility-VW'! |
|
5964 | 1799 |
|
6139 | 1800 |
asLogicalFileSpecification |
1801 |
^ self asFilename |
|
1802 |
||
1803 |
"Created: / 30.10.2001 / 17:29:53 / cg" |
|
1804 |
! |
|
1805 |
||
5964 | 1806 |
expandMacros |
1807 |
"ST80 compatibility - expand '<..>' macros with |
|
1808 |
argument strings. Similar to #bindWith:. |
|
1809 |
Read the comment in #expandMacrosWithArguments: about |
|
1810 |
limited compatibility issues." |
|
1811 |
||
1812 |
^ self expandMacrosWithArguments:#() |
|
1813 |
||
1814 |
" |
|
1815 |
'hello<n>foo' expandMacros |
|
1816 |
" |
|
1817 |
||
9192 | 1818 |
"Modified: / 18.6.1998 / 16:03:02 / cg" |
1819 |
! |
|
1820 |
||
1821 |
expandMacrosWith:arg |
|
5964 | 1822 |
"ST80 compatibility - expand '<..>' macros with |
1823 |
argument strings. Similar to #bindWith:. |
|
1824 |
Read the comment in #expandMacrosWithArguments: about |
|
1825 |
limited compatibility issues." |
|
1826 |
||
24038 | 1827 |
^ self expandMacrosWithArguments:{ arg } |
1828 |
||
1829 |
"Created: / 01-11-1997 / 13:01:28 / cg" |
|
1830 |
"Modified: / 02-04-2019 / 14:09:26 / Claus Gittinger" |
|
9192 | 1831 |
! |
5964 | 1832 |
|
1833 |
expandMacrosWith:arg1 with:arg2 |
|
1834 |
"ST80 compatibility - expand '<..>' macros with |
|
1835 |
argument strings. Similar to #bindWith:. |
|
1836 |
Read the comment in #expandMacrosWithArguments: about |
|
1837 |
limited compatibility issues." |
|
1838 |
||
24038 | 1839 |
^ self expandMacrosWithArguments:{arg1 . arg2 } |
1840 |
||
1841 |
"Modified: / 06-07-1998 / 21:58:14 / cg" |
|
1842 |
"Modified: / 02-04-2019 / 14:09:36 / Claus Gittinger" |
|
5964 | 1843 |
! |
1844 |
||
1845 |
expandMacrosWith:arg1 with:arg2 with:arg3 |
|
1846 |
"ST80 compatibility - expand '<..>' macros with |
|
1847 |
argument strings. Similar to #bindWith:. |
|
1848 |
Read the comment in #expandMacrosWithArguments: about |
|
1849 |
limited compatibility issues." |
|
1850 |
||
24038 | 1851 |
^ self expandMacrosWithArguments:{ arg1 . arg2 . arg3 } |
1852 |
||
1853 |
"Modified (format): / 02-04-2019 / 14:09:48 / Claus Gittinger" |
|
5964 | 1854 |
! |
1855 |
||
10353
65fb7aa2c6c2
added #expandMacrosWith:with:with:with:
Stefan Vogel <sv@exept.de>
parents:
10134
diff
changeset
|
1856 |
expandMacrosWith:arg1 with:arg2 with:arg3 with:arg4 |
65fb7aa2c6c2
added #expandMacrosWith:with:with:with:
Stefan Vogel <sv@exept.de>
parents:
10134
diff
changeset
|
1857 |
"ST80 compatibility - expand '<..>' macros with |
65fb7aa2c6c2
added #expandMacrosWith:with:with:with:
Stefan Vogel <sv@exept.de>
parents:
10134
diff
changeset
|
1858 |
argument strings. Similar to #bindWith:. |
65fb7aa2c6c2
added #expandMacrosWith:with:with:with:
Stefan Vogel <sv@exept.de>
parents:
10134
diff
changeset
|
1859 |
Read the comment in #expandMacrosWithArguments: about |
65fb7aa2c6c2
added #expandMacrosWith:with:with:with:
Stefan Vogel <sv@exept.de>
parents:
10134
diff
changeset
|
1860 |
limited compatibility issues." |
65fb7aa2c6c2
added #expandMacrosWith:with:with:with:
Stefan Vogel <sv@exept.de>
parents:
10134
diff
changeset
|
1861 |
|
24038 | 1862 |
^ self expandMacrosWithArguments:{ arg1 . arg2 . arg3 . arg4 } |
1863 |
||
1864 |
"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
|
1865 |
! |
65fb7aa2c6c2
added #expandMacrosWith:with:with:with:
Stefan Vogel <sv@exept.de>
parents:
10134
diff
changeset
|
1866 |
|
9192 | 1867 |
expandMacrosWithArguments:argArray |
5964 | 1868 |
"ST80 compatibility - expand '<..>' macros with |
1869 |
argument strings. Similar to #bindWith:. |
|
1870 |
WARNING: possibly not all ST80 expansions are supported here." |
|
1871 |
||
1872 |
"/ supported expansions: |
|
1873 |
"/ |
|
1874 |
"/ <#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
|
1875 |
"/ <#s> # is arg Number; slice in the arg itself (must know asString) |
5964 | 1876 |
"/ <#?s1:s2> # is arg Number; slice in s1 if the arg is true, s2 otherwise |
7643 | 1877 |
"/ use first arg if # is not given (i.e. no number before s,p ...) |
5964 | 1878 |
"/ <n> replace by a newLine character |
1879 |
"/ <t> replace by a tab character |
|
6834
eba4b58b8692
fixed expandMacros (%-escape)
Claus Gittinger <cg@exept.de>
parents:
6810
diff
changeset
|
1880 |
"/ %X the X character itself |
5964 | 1881 |
|
8999
f3fda66ea51e
#expandMacrosWithArguments: - be mor friendly to xml strings.
Stefan Vogel <sv@exept.de>
parents:
8971
diff
changeset
|
1882 |
|in out c fmt nr arg s1 s2 peekc| |
5964 | 1883 |
|
1884 |
in := self readStream. |
|
10721
58e6950264cb
care for bitsPerCharacter when expanding macros
Claus Gittinger <cg@exept.de>
parents:
10703
diff
changeset
|
1885 |
out := CharacterWriteStream on:(self species uninitializedNew:self size). |
5964 | 1886 |
|
1887 |
[in atEnd] whileFalse:[ |
|
20874 | 1888 |
c := in next. |
1889 |
c == $% ifTrue:[ |
|
1890 |
c := in next. |
|
1891 |
out nextPut:c |
|
1892 |
] ifFalse:[c ~~ $< ifTrue:[ |
|
1893 |
out nextPut:c. |
|
1894 |
] ifFalse:[ |
|
1895 |
peekc := in peek. |
|
1896 |
[peekc == $<] whileTrue:[ |
|
1897 |
out nextPut:$<. |
|
1898 |
peekc := in nextPeek. |
|
1899 |
]. |
|
1900 |
peekc == $n ifTrue:[ |
|
1901 |
peekc := in nextPeek. |
|
1902 |
peekc == $> ifTrue:[ |
|
1903 |
in next. |
|
1904 |
out cr. |
|
1905 |
] ifFalse:[ |
|
1906 |
out nextPutAll:'<n'. |
|
1907 |
] |
|
1908 |
] ifFalse:[peekc == $t ifTrue:[ |
|
1909 |
peekc := in nextPeek. |
|
1910 |
peekc == $> ifTrue:[ |
|
1911 |
in next. |
|
1912 |
out tab. |
|
1913 |
] ifFalse:[ |
|
1914 |
out nextPutAll:'<t'. |
|
1915 |
] |
|
1916 |
] ifFalse:[ |
|
1917 |
peekc isDigit ifTrue:[ |
|
1918 |
"start an argument expansion ..." |
|
1919 |
nr := Integer readFrom:in onError:nil. |
|
1920 |
nr isNil ifTrue:[ |
|
1921 |
"this cannot happen (there is at least one digit)" |
|
23009 | 1922 |
self proceedableError:'invalid format'. |
20874 | 1923 |
^ self |
1924 |
]. |
|
1925 |
fmt := in next. |
|
22999 | 1926 |
(fmt ~~ $? and:[fmt ~~ $# and:[in peek ~~ $>]]) ifTrue:[ |
20874 | 1927 |
out nextPut:$<. |
1928 |
nr printOn:out. |
|
1929 |
out nextPut:fmt. |
|
1930 |
] ifFalse:[ |
|
1931 |
(nr between:1 and:argArray size) ifTrue:[ |
|
1932 |
arg := argArray at:nr. |
|
1933 |
] ifFalse:[ |
|
1934 |
arg := '' |
|
1935 |
]. |
|
1936 |
||
1937 |
fmt == $p ifTrue:[ |
|
1938 |
"expand with args printString" |
|
1939 |
arg printOn:out. |
|
1940 |
] ifFalse:[fmt == $s ifTrue:[ |
|
1941 |
"expand with arg itself" |
|
1942 |
arg isText ifTrue:[ |
|
1943 |
out contentsSpecies isText ifFalse:[ |
|
1944 |
out := (TextStream ? CharacterWriteStream on:Text new) nextPutAll:out contents; yourself. |
|
1945 |
]. |
|
1946 |
out nextPutAll:arg. |
|
1947 |
] ifFalse:[ |
|
1948 |
out nextPutAll:arg asString string. "see method comment: arg must know #asString" |
|
1949 |
] |
|
22999 | 1950 |
] ifFalse:[(fmt == $? or:[fmt == $#]) ifTrue:[ |
20874 | 1951 |
s1 := in upTo:$:. |
1952 |
s2 := in nextUpTo:$>. |
|
22999 | 1953 |
((fmt == $? and:[arg == true]) |
23009 | 1954 |
or:[(fmt == $# and:[arg == 1])]) ifTrue:[ |
20874 | 1955 |
out nextPutAll:s1 |
1956 |
] ifFalse:[ |
|
1957 |
out nextPutAll:s2 |
|
1958 |
]. |
|
1959 |
] ifFalse:[ |
|
1960 |
"what does VW do here ?" |
|
1961 |
self error:'invalid format' mayProceed:true. |
|
1962 |
^ self |
|
1963 |
]]]. |
|
1964 |
c := in next. |
|
1965 |
c ~~ $> ifTrue:[ |
|
1966 |
"what does VW do here ?" |
|
1967 |
self error:'invalid format' mayProceed:true. |
|
1968 |
^ self |
|
1969 |
] |
|
1970 |
||
1971 |
]. |
|
1972 |
] ifFalse:[ |
|
1973 |
out nextPut:$<. |
|
1974 |
]. |
|
1975 |
]]. |
|
1976 |
]]. |
|
5964 | 1977 |
]. |
1978 |
^ out contents |
|
1979 |
||
1980 |
" |
|
8874 | 1981 |
'hello <1s> how are you' expandMacrosWith:(OperatingSystem getLoginName) |
1982 |
'one plus one is <1p>' expandMacrosWith:2 |
|
5964 | 1983 |
" |
1984 |
||
10721
58e6950264cb
care for bitsPerCharacter when expanding macros
Claus Gittinger <cg@exept.de>
parents:
10703
diff
changeset
|
1985 |
"Modified: / 18-09-2007 / 22:50:43 / cg" |
23009 | 1986 |
"Modified: / 24-05-2018 / 21:06:31 / Claus Gittinger" |
9192 | 1987 |
! |
1988 |
||
1989 |
isCharacters |
|
21318 | 1990 |
"true, if the receiver is a string-like thing. |
1991 |
added for visual works compatibility" |
|
1992 |
||
1993 |
"/ kept in libbasic package, because it is used in libjava and imap-implementation |
|
9192 | 1994 |
^ true |
1995 |
! ! |
|
1996 |
||
16295 | 1997 |
!CharacterArray methodsFor:'JavaScript support'! |
1998 |
||
1999 |
unquote |
|
17000 | 2000 |
"removes double quotes from the receiver. |
19698 | 2001 |
This is the JavaScript standard unquote function." |
2002 |
||
2003 |
^ self unquote:$" |
|
16295 | 2004 |
|
2005 |
" |
|
17000 | 2006 |
'hello' quote unquote |
2007 |
||
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2008 |
JavaScriptParser evaluate:'''hello''.quote.unquote' |
16295 | 2009 |
" |
19698 | 2010 |
! |
2011 |
||
2012 |
unquote:quoteCharacter |
|
2013 |
"removes quoteCharacter from either end of the receiver." |
|
2014 |
||
2015 |
|mySize| |
|
2016 |
||
2017 |
(mySize := self size) >= 2 ifTrue:[ |
|
20874 | 2018 |
((self first == quoteCharacter) and:[self last == quoteCharacter]) ifTrue:[ |
2019 |
^ self copyFrom:2 to:mySize-1 |
|
2020 |
]. |
|
19698 | 2021 |
]. |
2022 |
^ self |
|
2023 |
||
2024 |
" |
|
2025 |
'*hello*' unquote:$* |
|
2026 |
" |
|
16295 | 2027 |
! ! |
13510 | 2028 |
|
16175 | 2029 |
|
9192 | 2030 |
!CharacterArray methodsFor:'character searching'! |
2031 |
||
2032 |
includesMatchCharacters |
|
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
2033 |
"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
|
2034 |
for match operations; false if not. |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2035 |
Here, do not care for $\ escapes" |
608 | 2036 |
|
2037 |
^ self includesAny:'*#[' |
|
2519
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2038 |
|
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2039 |
" |
8874 | 2040 |
'*foo' includesMatchCharacters |
2041 |
'\*foo' includesMatchCharacters |
|
2042 |
'\*foo' includesUnescapedMatchCharacters |
|
2043 |
'*foo' includesMatchCharacters |
|
2519
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 |
'foo\*' includesMatchCharacters |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2047 |
'foo\' includesMatchCharacters |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2048 |
" |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2049 |
|
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2050 |
"Modified: 2.4.1997 / 18:12:34 / cg" |
9192 | 2051 |
! |
2052 |
||
2053 |
includesSeparator |
|
2054 |
"return true, if the receiver contains any whitespace characters" |
|
2055 |
||
2056 |
^ (self indexOfSeparator ~~ 0) |
|
2057 |
||
2058 |
" |
|
2059 |
'hello world' includesSeparator |
|
2060 |
'helloworld' includesSeparator |
|
2061 |
" |
|
2062 |
! |
|
2063 |
||
2064 |
includesUnescapedMatchCharacters |
|
8874 | 2065 |
"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
|
2066 |
for match operations; false if not. |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2067 |
Here, care for $\ escapes" |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2068 |
|
5342
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
2069 |
|idx sz specialChars escape| |
2519
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2070 |
|
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2071 |
idx := 1. |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2072 |
sz := self size. |
5342
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
2073 |
specialChars := '*#[\'. |
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
2074 |
(escape := self class matchEscapeCharacter) ~~ $\ ifTrue:[ |
20874 | 2075 |
specialChars := specialChars copy. |
2076 |
specialChars at:specialChars size put:escape |
|
5342
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
2077 |
]. |
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
2078 |
|
16245 | 2079 |
[ |
20874 | 2080 |
idx := self indexOfAny:specialChars startingAt:idx. |
2081 |
idx == 0 ifTrue:[^ false]. |
|
2082 |
(self at:idx) == escape ifFalse:[^ true]. |
|
2083 |
idx := idx + 2. |
|
2084 |
idx > sz ifTrue:[^ false]. |
|
16245 | 2085 |
] loop. |
8874 | 2086 |
|
2087 |
" |
|
2088 |
'*foo' includesUnescapedMatchCharacters |
|
2089 |
'\*foo' includesUnescapedMatchCharacters |
|
2090 |
'\\foo' includesUnescapedMatchCharacters |
|
2091 |
'\\\$foo' includesUnescapedMatchCharacters |
|
2519
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 |
'foo\*' includesUnescapedMatchCharacters |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2096 |
'foo\' includesUnescapedMatchCharacters |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2097 |
" |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2098 |
|
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2099 |
"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
|
2100 |
"Created: 2.4.1997 / 17:23:26 / cg" |
9192 | 2101 |
! |
2102 |
||
2103 |
indexOfControlCharacterStartingAt:startIndex |
|
3668
76271941f044
added #indexOfControlCharacterStartingAt:
Claus Gittinger <cg@exept.de>
parents:
3656
diff
changeset
|
2104 |
"return the index of the next control character; |
76271941f044
added #indexOfControlCharacterStartingAt:
Claus Gittinger <cg@exept.de>
parents:
3656
diff
changeset
|
2105 |
that is a character with asciiValue < 32. |
15605 | 2106 |
Start the search at startIndex, searching forward. |
3668
76271941f044
added #indexOfControlCharacterStartingAt:
Claus Gittinger <cg@exept.de>
parents:
3656
diff
changeset
|
2107 |
Return 0 if none is found." |
76271941f044
added #indexOfControlCharacterStartingAt:
Claus Gittinger <cg@exept.de>
parents:
3656
diff
changeset
|
2108 |
|
76271941f044
added #indexOfControlCharacterStartingAt:
Claus Gittinger <cg@exept.de>
parents:
3656
diff
changeset
|
2109 |
|start "{ Class: SmallInteger }" |
9192 | 2110 |
mySize "{ Class: SmallInteger }"| |
2111 |
||
2112 |
start := startIndex. |
|
2113 |
mySize := self size. |
|
2114 |
||
2115 |
start to:mySize do:[:index | |
|
20874 | 2116 |
(self at:index) isControlCharacter ifTrue:[^ index] |
9192 | 2117 |
]. |
2118 |
^ 0 |
|
2119 |
||
2120 |
" |
|
2121 |
'hello world' asTwoByteString indexOfControlCharacterStartingAt:1 |
|
2122 |
'hello world\foo' withCRsasTwoByteString indexOfControlCharacterStartingAt:1 |
|
2123 |
" |
|
2124 |
||
2125 |
"Modified: / 21.7.1998 / 17:25:07 / cg" |
|
2126 |
! |
|
3668
76271941f044
added #indexOfControlCharacterStartingAt:
Claus Gittinger <cg@exept.de>
parents:
3656
diff
changeset
|
2127 |
|
8541 | 2128 |
indexOfNonSeparator |
2129 |
"return the index of the first non-whitespace character. |
|
2130 |
return 0 if no non-separator was found" |
|
2131 |
||
2132 |
^ self indexOfNonSeparatorStartingAt:1. |
|
2133 |
||
2134 |
" |
|
8874 | 2135 |
' hello world' indexOfNonSeparator |
2136 |
' ' indexOfNonSeparator |
|
2137 |
'a ' indexOfNonSeparator |
|
2138 |
'abc' indexOfNonSeparator |
|
2139 |
' ' indexOfNonSeparator |
|
9192 | 2140 |
'' indexOfNonSeparator |
2141 |
" |
|
2142 |
! |
|
2143 |
||
2144 |
indexOfNonSeparatorStartingAt:startIndex |
|
4117 | 2145 |
"return the index of the next non-whitespace character, |
2146 |
starting the search at startIndex, searching forward; |
|
2147 |
return 0 if no non-separator was found" |
|
608 | 2148 |
|
2149 |
|start "{ Class: SmallInteger }" |
|
2150 |
mySize "{ Class: SmallInteger }"| |
|
2151 |
||
2152 |
start := startIndex. |
|
2153 |
mySize := self size. |
|
2154 |
||
2155 |
start to:mySize do:[:index | |
|
20874 | 2156 |
(self at:index) isSeparator ifFalse:[^ index] |
608 | 2157 |
]. |
2158 |
^ 0 |
|
2159 |
||
2160 |
" |
|
8874 | 2161 |
' hello world' indexOfNonSeparatorStartingAt:1 |
2162 |
' ' indexOfNonSeparatorStartingAt:1 |
|
2163 |
'a ' indexOfNonSeparatorStartingAt:2 |
|
4117 | 2164 |
" |
2165 |
||
608 | 2166 |
" |
2167 |
|s index1 index2| |
|
2168 |
s := ' foo bar baz'. |
|
2169 |
index1 := s indexOfNonSeparatorStartingAt:1. |
|
2170 |
index2 := s indexOfSeparatorStartingAt:index1. |
|
2171 |
s copyFrom:index1 to:index2 - 1 |
|
2172 |
" |
|
9192 | 2173 |
! |
2174 |
||
2175 |
indexOfSeparator |
|
2176 |
"return the index of the first whitespace character; |
|
2177 |
starting the search at the beginning, searching forward; |
|
2178 |
return 0 if no separator was found" |
|
2179 |
||
2180 |
^ self indexOfSeparatorStartingAt:1 |
|
2181 |
||
2182 |
" |
|
2183 |
'hello world' indexOfSeparator |
|
2184 |
'helloworld' indexOfSeparator |
|
2185 |
'hello ' indexOfSeparator |
|
2186 |
' hello' indexOfSeparator |
|
2187 |
" |
|
2188 |
! |
|
2189 |
||
15912 | 2190 |
indexOfSeparatorOrEndStartingAt:startIndex |
2191 |
"return the index of the next whitespace character, |
|
2192 |
starting the search at startIndex, searching forward; |
|
19737 | 2193 |
return the index of one beyond the end of the receiver if no separator was found. |
15912 | 2194 |
To extract the word, copy from startIndex to the returned index-1" |
2195 |
||
2196 |
|idx| |
|
2197 |
||
2198 |
idx := self indexOfSeparatorStartingAt:startIndex. |
|
2199 |
idx == 0 ifTrue:[^ self size + 1]. |
|
2200 |
^ idx. |
|
2201 |
||
2202 |
" |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2203 |
'hello world' indexOfSeparatorOrEndStartingAt:3 |
15912 | 2204 |
' hello world' indexOfSeparatorOrEndStartingAt:3 |
2205 |
'hello world ' indexOfSeparatorOrEndStartingAt:3 |
|
2206 |
'hello world ' indexOfSeparatorOrEndStartingAt:6 |
|
2207 |
'hello world ' indexOfSeparatorOrEndStartingAt:7 |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2208 |
'helloworld ' indexOfSeparatorOrEndStartingAt:7 |
15912 | 2209 |
'helloworld' indexOfSeparatorOrEndStartingAt:7 |
19737 | 2210 |
'helloworld' indexOfSeparatorStartingAt:7 |
15912 | 2211 |
" |
2212 |
! |
|
2213 |
||
9192 | 2214 |
indexOfSeparatorStartingAt:startIndex |
2215 |
"return the index of the next whitespace character, |
|
2216 |
starting the search at startIndex, searching forward; |
|
2217 |
return 0 if no separator was found" |
|
2218 |
||
2219 |
|start "{ Class: SmallInteger }" |
|
2220 |
mySize "{ Class: SmallInteger }"| |
|
2221 |
||
2222 |
start := startIndex. |
|
2223 |
mySize := self size. |
|
2224 |
||
2225 |
start to:mySize do:[:index | |
|
20874 | 2226 |
(self at:index) isSeparator ifTrue:[^ index] |
9192 | 2227 |
]. |
2228 |
^ 0 |
|
2229 |
||
2230 |
" |
|
2231 |
'hello world' indexOfSeparatorStartingAt:3 |
|
2232 |
' hello world' indexOfSeparatorStartingAt:3 |
|
8874 | 2233 |
'hello world ' indexOfSeparatorStartingAt:3 |
2234 |
'hello world ' indexOfSeparatorStartingAt:6 |
|
2235 |
'hello world ' indexOfSeparatorStartingAt:7 |
|
2236 |
'helloworld ' indexOfSeparatorStartingAt:7 |
|
2237 |
'helloworld' indexOfSeparatorStartingAt:7 |
|
4117 | 2238 |
" |
2519
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2239 |
! |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2240 |
|
4117 | 2241 |
lastIndexOfSeparator |
14150
e13160aa321a
comment/format in: #lastIndexOfSeparator
Claus Gittinger <cg@exept.de>
parents:
14141
diff
changeset
|
2242 |
"return the last index of a whitespace character (space or tab). |
4117 | 2243 |
(i.e. start the search at the end and search backwards); |
2244 |
Returns 0 if no separator is found." |
|
2245 |
||
2246 |
^ self lastIndexOfSeparatorStartingAt:(self size) |
|
2247 |
||
2248 |
" |
|
8874 | 2249 |
'hello world' lastIndexOfSeparator |
2250 |
'helloworld' lastIndexOfSeparator |
|
2251 |
'hel lo wor ld' lastIndexOfSeparator |
|
2252 |
'hel ' lastIndexOfSeparator 6 |
|
4117 | 2253 |
" |
14150
e13160aa321a
comment/format in: #lastIndexOfSeparator
Claus Gittinger <cg@exept.de>
parents:
14141
diff
changeset
|
2254 |
|
e13160aa321a
comment/format in: #lastIndexOfSeparator
Claus Gittinger <cg@exept.de>
parents:
14141
diff
changeset
|
2255 |
"Modified (comment): / 01-06-2012 / 13:10:30 / cg" |
4117 | 2256 |
! |
2257 |
||
2258 |
lastIndexOfSeparatorStartingAt:startIndex |
|
9192 | 2259 |
"return the index of the previous whitespace character, |
2260 |
starting the search at startIndex (and searching backwards); |
|
2261 |
returns 0 if no separator was found" |
|
2262 |
||
2263 |
|start "{ Class: SmallInteger }"| |
|
2264 |
||
2265 |
start := startIndex. |
|
2266 |
||
2267 |
start to:1 by:-1 do:[:index | |
|
20874 | 2268 |
(self at:index) isSeparator ifTrue:[^ index] |
9192 | 2269 |
]. |
2270 |
^ 0 |
|
2271 |
||
2272 |
" |
|
2273 |
'hello world' lastIndexOfSeparatorStartingAt:3 |
|
2274 |
'hello world' lastIndexOfSeparatorStartingAt:7 |
|
2275 |
'helloworld' lastIndexOfSeparatorStartingAt:7 |
|
8874 | 2276 |
' helloworld' lastIndexOfSeparatorStartingAt:7 |
4117 | 2277 |
" |
608 | 2278 |
! ! |
2279 |
||
2280 |
!CharacterArray methodsFor:'comparing'! |
|
2281 |
||
6742
292775fda600
avoid endless recursion when comparing int < string
Claus Gittinger <cg@exept.de>
parents:
6655
diff
changeset
|
2282 |
< aString |
608 | 2283 |
"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
|
2284 |
receiver is greater than the argument. Otherwise return false. |
8874 | 2285 |
This comparison is based on the elements ascii code - |
9192 | 2286 |
i.e. upper/lowercase & upper/lowercase & national characters are NOT treated specially." |
2287 |
||
2288 |
|mySize "{ Class: SmallInteger }" |
|
2289 |
otherSize "{ Class: SmallInteger }" |
|
2290 |
n "{ Class: SmallInteger }" |
|
2291 |
c1 c2| |
|
2292 |
||
2293 |
mySize := self size. |
|
2294 |
otherSize := aString string size. |
|
6742
292775fda600
avoid endless recursion when comparing int < string
Claus Gittinger <cg@exept.de>
parents:
6655
diff
changeset
|
2295 |
n := mySize min:otherSize. |
292775fda600
avoid endless recursion when comparing int < string
Claus Gittinger <cg@exept.de>
parents:
6655
diff
changeset
|
2296 |
|
292775fda600
avoid endless recursion when comparing int < string
Claus Gittinger <cg@exept.de>
parents:
6655
diff
changeset
|
2297 |
1 to:n do:[:index | |
20874 | 2298 |
c1 := self at:index. |
2299 |
c2 := aString at:index. |
|
2300 |
(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
|
2301 |
]. |
292775fda600
avoid endless recursion when comparing int < string
Claus Gittinger <cg@exept.de>
parents:
6655
diff
changeset
|
2302 |
^ mySize < otherSize |
608 | 2303 |
! |
2304 |
||
2305 |
= aString |
|
2306 |
"Compare the receiver with the argument and return true if the |
|
2307 |
receiver is equal to the argument. Otherwise return false. |
|
1252 | 2308 |
|
9191 | 2309 |
This compare does NOT ignore case differences, |
608 | 2310 |
therefore 'foo' = 'Foo' will return false. |
2311 |
Since this is incompatible to ST-80 (at least, V2.x) , this may change." |
|
2312 |
||
14137
1eb1b4a3f364
Inherit from UninterpretedBytes instead of ByteArray
Stefan Vogel <sv@exept.de>
parents:
14122
diff
changeset
|
2313 |
|mySize "{ Class: SmallInteger }"| |
608 | 2314 |
|
5963
23462e06dcff
compare with Texts fixed (should compare equal if strings are)
Claus Gittinger <cg@exept.de>
parents:
5946
diff
changeset
|
2315 |
(aString isString or:[aString species == self species]) ifFalse:[ |
20874 | 2316 |
^ false |
5963
23462e06dcff
compare with Texts fixed (should compare equal if strings are)
Claus Gittinger <cg@exept.de>
parents:
5946
diff
changeset
|
2317 |
]. |
608 | 2318 |
mySize := self size. |
14137
1eb1b4a3f364
Inherit from UninterpretedBytes instead of ByteArray
Stefan Vogel <sv@exept.de>
parents:
14122
diff
changeset
|
2319 |
mySize ~~ (aString size) ifTrue:[^ false]. |
608 | 2320 |
|
2321 |
1 to:mySize do:[:index | |
|
20874 | 2322 |
(self at:index) = (aString at:index) ifFalse:[^ false]. |
608 | 2323 |
]. |
2324 |
^ true |
|
2325 |
||
2326 |
" |
|
8874 | 2327 |
'foo' = 'Foo' |
2328 |
'foo' = 'bar' |
|
2329 |
'foo' = 'foo' |
|
2330 |
'foo' = 'foo' asText |
|
2331 |
'foo' asText = 'foo' |
|
2332 |
'foo' asText = 'foo' asText |
|
608 | 2333 |
" |
1252 | 2334 |
|
2335 |
"Modified: 22.4.1996 / 15:53:58 / cg" |
|
608 | 2336 |
! |
9192 | 2337 |
|
9191 | 2338 |
> aString |
608 | 2339 |
"Compare the receiver with the argument and return true if the |
2340 |
receiver is greater than the argument. Otherwise return false. |
|
8874 | 2341 |
This comparison is based on the elements ascii code - |
1252 | 2342 |
i.e. upper/lowercase & upper/lowercase & national characters are NOT treated specially." |
608 | 2343 |
|
2344 |
|mySize "{ Class: SmallInteger }" |
|
8874 | 2345 |
otherSize "{ Class: SmallInteger }" |
9192 | 2346 |
n "{ Class: SmallInteger }" |
2347 |
c1 c2| |
|
2348 |
||
2349 |
mySize := self size. |
|
5929 | 2350 |
otherSize := aString string size. |
2351 |
n := mySize min:otherSize. |
|
2352 |
||
2353 |
1 to:n do:[:index | |
|
20874 | 2354 |
c1 := self at:index. |
2355 |
c2 := aString at:index. |
|
2356 |
(c1 == c2 or:[c1 = c2]) ifFalse:[^ c1 > c2]. |
|
5929 | 2357 |
]. |
9192 | 2358 |
^ mySize > otherSize |
2359 |
||
2360 |
"Modified: 22.4.1996 / 15:55:00 / cg" |
|
2361 |
! |
|
2362 |
||
14660 | 2363 |
after:aString |
23155 | 2364 |
<resource: #obsolete> |
14660 | 2365 |
"Compare the receiver with the argument and return true if the |
2366 |
receiver should come after the argument in a sorted list. |
|
2367 |
Otherwise return false. |
|
2368 |
NOTE: The comparison should be language specific, depending on the value of |
|
20874 | 2369 |
LC_COLLATE, which is initialized from the environment. |
2370 |
||
2371 |
Currently it is for Strings, but not for UnicodeStrings... |
|
14660 | 2372 |
|
2373 |
STUPID: |
|
23155 | 2374 |
#after has a completely different meaning in SeqColl... |
2375 |
... therefore it is marked as obsolete. |
|
2376 |
" |
|
14660 | 2377 |
|
2378 |
^ (self compareCollatingWith:aString) > 0 |
|
23155 | 2379 |
|
2380 |
"Modified (comment): / 29-06-2018 / 11:41:33 / Claus Gittinger" |
|
14660 | 2381 |
! |
2382 |
||
15814 | 2383 |
compareAsVersionNumberWith:aStringOrCollection |
2384 |
"Compare the receiver with the argument and return 1 if the receiver is |
|
2385 |
greater, 0 if equal and -1 if less than the argument in a sorted list. |
|
2386 |
Compare as version numbers in the form a.b.c... ." |
|
2387 |
||
23121 | 2388 |
|
2389 |
^ self asVersionNumberCollection |
|
2390 |
compareWith:aStringOrCollection asVersionNumberCollection |
|
15814 | 2391 |
|
2392 |
" |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2393 |
self assert:('1' compareAsVersionNumberWith:'2') < 0. |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2394 |
self assert:('2' compareAsVersionNumberWith:'1') > 0. |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2395 |
self assert:('1.1' compareAsVersionNumberWith:'2.1.2') < 0. |
23121 | 2396 |
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
|
2397 |
self assert:('2.1' compareAsVersionNumberWith:'1.2.3') > 0. |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2398 |
self assert:('1' compareAsVersionNumberWith:'1.1') < 0. |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2399 |
self assert:('1.1' compareAsVersionNumberWith:'1') > 0. |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2400 |
self assert:('1.1' compareAsVersionNumberWith:'1.2') < 0. |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2401 |
self assert:('1.10' compareAsVersionNumberWith:'1.2') > 0. |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2402 |
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
|
2403 |
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
|
2404 |
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
|
2405 |
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
|
2406 |
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
|
2407 |
self assert:('1.2.3.4' compareAsVersionNumberWith:#(1 2 3 4)) = 0. |
22325 | 2408 |
self assert:('1.2.3.4' compareAsVersionNumberWith:#('1' 2 3 4)) = 0. |
23121 | 2409 |
|
2410 |
self assert:('1.1' compareAsVersionNumberWith:'1.1a') < 0. |
|
2411 |
||
22325 | 2412 |
" |
2413 |
||
2414 |
"Modified: / 25-10-2017 / 11:25:08 / stefan" |
|
23121 | 2415 |
"Modified (format): / 20-06-2018 / 19:23:18 / Stefan Vogel" |
15814 | 2416 |
! |
2417 |
||
9192 | 2418 |
compareCaselessWith:aString |
22327 | 2419 |
"Compare the receiver against the argument, ignoring case. |
9190 | 2420 |
Return 1 if the receiver is greater, 0 if equal and -1 if less than the argument. |
2421 |
||
8874 | 2422 |
This comparison is based on the elements ascii code - |
9190 | 2423 |
i.e. national characters are NOT treated specially. |
2424 |
'foo' compareWith: 'Foo' will return 0" |
|
608 | 2425 |
|
2426 |
|mySize "{ Class: SmallInteger }" |
|
8874 | 2427 |
otherSize "{ Class: SmallInteger }" |
9192 | 2428 |
n "{ Class: SmallInteger }" |
608 | 2429 |
c1 c2| |
2430 |
||
2431 |
mySize := self size. |
|
5739
4fd3be7ccc75
oops some relational ops did accept a non-string arg
Claus Gittinger <cg@exept.de>
parents:
5732
diff
changeset
|
2432 |
otherSize := aString string size. |
608 | 2433 |
n := mySize min:otherSize. |
2434 |
||
2435 |
1 to:n do:[:index | |
|
20874 | 2436 |
c1 := (self at:index) asLowercase. |
2437 |
c2 := (aString at:index) asLowercase. |
|
2438 |
c1 > c2 ifTrue:[^ 1]. |
|
2439 |
c1 < c2 ifTrue:[^ -1]. |
|
608 | 2440 |
]. |
2441 |
mySize > otherSize ifTrue:[^ 1]. |
|
2442 |
mySize < otherSize ifTrue:[^ -1]. |
|
2443 |
^ 0 |
|
1252 | 2444 |
|
22327 | 2445 |
"Modified: / 22-04-1996 / 15:56:07 / cg" |
2446 |
"Modified (comment): / 26-10-2017 / 16:01:01 / mawalch" |
|
608 | 2447 |
! |
2448 |
||
14660 | 2449 |
compareCollatingWith:aString |
2450 |
"Compare the receiver with the argument and return 1 if the receiver is |
|
2451 |
greater, 0 if equal and -1 if less than the argument in a sorted list. |
|
2452 |
The comparison is language specific, depending on the value of |
|
2453 |
LC_COLLATE, which is in the shell environment." |
|
2454 |
||
2455 |
"TODO not yet defined for unicode" |
|
2456 |
<resource: #todo> |
|
2457 |
||
2458 |
|s| |
|
2459 |
||
2460 |
(s := self string) ~~ self ifTrue:[ |
|
20874 | 2461 |
^ s compareCollatingWith:aString |
14660 | 2462 |
]. |
2463 |
^ self compareWith:aString |
|
2464 |
! |
|
2465 |
||
9192 | 2466 |
compareWith:aString |
9190 | 2467 |
"Compare the receiver with the argument and return 1 if the receiver is |
2468 |
greater, 0 if equal and -1 if less than the argument. |
|
11861 | 2469 |
This comparison is based on the elements' codepoints - |
9190 | 2470 |
i.e. upper/lowercase & national characters are NOT treated specially. |
2471 |
'foo' compareWith: 'Foo' will return 1. |
|
2472 |
while 'foo' sameAs:'Foo' will return true" |
|
2473 |
||
15814 | 2474 |
|s| |
2475 |
||
2476 |
s := self string. |
|
2477 |
s ~~ self ifTrue:[ |
|
20874 | 2478 |
^ s compareWith:aString string. |
9192 | 2479 |
]. |
15814 | 2480 |
^ super compareWith:aString string. |
9192 | 2481 |
! |
2482 |
||
11861 | 2483 |
endsWith:aStringOrCharacter |
16175 | 2484 |
"return true, if the receiver ends with something, aStringOrCharacter. |
2485 |
If aStringOrCharacter is empty, true is returned" |
|
11861 | 2486 |
|
2487 |
|s| |
|
2488 |
||
2489 |
(s := self string) ~~ self ifTrue:[ |
|
20874 | 2490 |
^ s endsWith:aStringOrCharacter |
11861 | 2491 |
]. |
21083 | 2492 |
(self size ~~ 0 and:[aStringOrCharacter isCharacter]) ifTrue:[ |
20874 | 2493 |
^ self last = aStringOrCharacter |
11861 | 2494 |
]. |
2495 |
^ super endsWith:aStringOrCharacter |
|
2496 |
||
2497 |
" |
|
2498 |
'hello world' endsWith:'world' |
|
2499 |
'hello world' asText allBold endsWith:'world' |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2500 |
'hello world' endsWith:'' |
16175 | 2501 |
'hello world' asText allBold endsWith:'' |
11861 | 2502 |
" |
2503 |
||
2504 |
"Modified: 12.5.1996 / 15:49:18 / cg" |
|
2505 |
! |
|
2506 |
||
23154 | 2507 |
endsWith:aStringOrCharacter caseSensitive:caseSensitive |
2508 |
"return true, if the receiver ends with something, aStringOrCharacter. |
|
2509 |
If aStringOrCharacter is empty, true is returned" |
|
2510 |
||
2511 |
|s| |
|
2512 |
||
2513 |
caseSensitive ifTrue:[^ self endsWith:aStringOrCharacter]. |
|
2514 |
||
2515 |
(s := self string) ~~ self ifTrue:[ |
|
2516 |
^ s endsWith:aStringOrCharacter |
|
2517 |
]. |
|
2518 |
(self size ~~ 0 and:[aStringOrCharacter isCharacter]) ifTrue:[ |
|
2519 |
^ self last asLowercase = aStringOrCharacter asLowercase |
|
2520 |
]. |
|
2521 |
^ self endsWith:aStringOrCharacter using:[:a :b | a asLowercase = b asLowercase]. |
|
2522 |
||
2523 |
" |
|
2524 |
'hello World' endsWith:'world' caseSensitive:true |
|
2525 |
'hello World' endsWith:'world' caseSensitive:false |
|
2526 |
" |
|
2527 |
||
2528 |
"Created: / 29-06-2018 / 11:38:57 / Claus Gittinger" |
|
2529 |
! |
|
2530 |
||
23783 | 2531 |
endsWithDigit |
2532 |
"Answer whether the receiver's final character represents a digit. 3/11/96 sw" |
|
2533 |
||
2534 |
|len| |
|
2535 |
||
2536 |
^ (len := self size) ~~ 0 and:[(self at:len) isDigit] |
|
2537 |
! |
|
2538 |
||
11838 | 2539 |
hammingDistanceTo:aString |
2540 |
"return the hamming distance (the number of characters which are different). |
|
12177 | 2541 |
In information theory, the Hamming distance between two strings of equal length |
2542 |
is the number of positions for which the corresponding symbols are different. |
|
2543 |
Put another way, it measures the minimum number of substitutions required to change |
|
11838 | 2544 |
one into the other, or the number of errors that transformed one string into the other." |
2545 |
||
22377 | 2546 |
|mySize| |
2547 |
||
2548 |
mySize := self size. |
|
2549 |
self assert:(aString size == mySize). |
|
2550 |
^ 1 to:mySize count:[:idx | (self at:idx) ~= (aString at:idx)] |
|
11838 | 2551 |
|
2552 |
" |
|
2553 |
'roses' hammingDistanceTo:'toned' |
|
22377 | 2554 |
'roses' hammingDistanceTo:'doses' |
2555 |
" |
|
2556 |
||
2557 |
"Modified (comment): / 24-11-2017 / 08:50:58 / cg" |
|
11838 | 2558 |
! |
2559 |
||
9192 | 2560 |
hash |
2561 |
"return an integer useful as a hash-key" |
|
2562 |
||
16898 | 2563 |
"/ whenever changing, also care for String>>hash. |
13879 | 2564 |
"/ immediately after any change, execute (maybe in a debugger): |
2565 |
"/ Set allSubInstancesDo:[:s | s rehash] |
|
16898 | 2566 |
^ self hash_fnv1a |
2567 |
||
2568 |
" |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2569 |
'a' hash |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2570 |
'a' asUnicode16String hash |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2571 |
'a' asUnicode32String hash |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2572 |
'aa' hash |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2573 |
'aa' asUnicode16String hash |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2574 |
'aa' asUnicode32String hash |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2575 |
'ab' hash |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2576 |
'ab' asUnicode16String hash |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2577 |
'ab' asUnicode32String hash |
13879 | 2578 |
'ab' hash |
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2579 |
'ab' asArray hash |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2580 |
" |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2581 |
|
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2582 |
" |
20874 | 2583 |
|syms ms| |
2584 |
||
2585 |
syms := Symbol allInstances. |
|
2586 |
Transcript show:'syms: '; showCR:syms size. |
|
2587 |
Transcript show:'sdbm hashes: '; showCR:(syms collect:[:s| s hash]) asSet size. |
|
2588 |
Transcript show:'dragonBook hashes: '; showCR:(syms collect:[:s| s hash_dragonBook]) asSet size. |
|
2589 |
||
2590 |
ms := Time millisecondsToRun:[ |
|
2591 |
10 timesRepeat:[ |
|
2592 |
syms do:[:each| each hash]. |
|
2593 |
]. |
|
2594 |
]. |
|
2595 |
Transcript show:'sdbm hash: '; showCR:ms. |
|
2596 |
||
2597 |
ms := Time millisecondsToRun:[ |
|
2598 |
10 timesRepeat:[ |
|
2599 |
syms do:[:each| each hash_dragonBook]. |
|
2600 |
]. |
|
2601 |
]. |
|
2602 |
Transcript show:'dragonBook: '; showCR:ms. |
|
2603 |
||
2604 |
syms := syms collect:[:each| each asUnicode16String]. |
|
2605 |
ms := Time millisecondsToRun:[ |
|
2606 |
10 timesRepeat:[ |
|
2607 |
syms do:[:each| each hash]. |
|
2608 |
]. |
|
2609 |
]. |
|
2610 |
Transcript show:'unicode sdbm hash: '; showCR:ms. |
|
2611 |
||
2612 |
ms := Time millisecondsToRun:[ |
|
2613 |
10 timesRepeat:[ |
|
2614 |
syms do:[:each| each hash_dragonBook]. |
|
2615 |
]. |
|
2616 |
]. |
|
2617 |
Transcript show:'unicode dragonBook:'; showCR:ms. |
|
14641 | 2618 |
" |
2619 |
||
13879 | 2620 |
"Modified: / 26-12-2011 / 14:09:07 / cg" |
2621 |
! |
|
2622 |
||
2623 |
hash_dragonBook |
|
2624 |
"return an integer useful as a hash-key" |
|
2625 |
||
10698
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2626 |
|h g| |
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2627 |
|
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2628 |
"/ |
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2629 |
"/ this is the dragon-book algorithm |
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2630 |
"/ |
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2631 |
h := 0. |
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2632 |
self reverseDo:[:char | |
10703
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
2633 |
"/ 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
|
2634 |
"/ h := (h bitShift:4) + char asciiValue. |
20874 | 2635 |
h := (h bitShift:4). |
2636 |
h := h + char codePoint. |
|
2637 |
h := h bitAnd:16rFFFFFFFF. |
|
2638 |
g := (h bitAnd: 16rF0000000). |
|
2639 |
g ~~ 0 ifTrue:[ |
|
2640 |
h := h bitXor:(g bitShift:-24). |
|
2641 |
h := h bitXor:g. |
|
2642 |
]. |
|
10698
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2643 |
]. |
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2644 |
"/ |
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2645 |
"/ multiply by large prime to spread values |
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2646 |
"/ This speeds up Set and Dictionary by a factor of 10!! |
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2647 |
"/ |
14137
1eb1b4a3f364
Inherit from UninterpretedBytes instead of ByteArray
Stefan Vogel <sv@exept.de>
parents:
14122
diff
changeset
|
2648 |
h := h times:31415821. |
10698
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2649 |
^ h |
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2650 |
|
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2651 |
" |
10703
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
2652 |
'a' hash |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
2653 |
'a' asUnicode16String hash |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
2654 |
'aa' hash |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
2655 |
'aa' asUnicode16String hash |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
2656 |
'ab' hash |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
2657 |
'ab' asUnicode16String hash |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
2658 |
'ab' hash |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
2659 |
'ab' asArray hash |
10698
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2660 |
" |
13879 | 2661 |
|
2662 |
"Created: / 26-12-2011 / 13:46:06 / cg" |
|
2663 |
! |
|
2664 |
||
16887 | 2665 |
hash_fnv1a |
2666 |
"return an integer useful as a hash-key. |
|
2667 |
This method uses the fnv-1a algorithm |
|
19743 | 2668 |
(which is actually a very good one). |
2669 |
Attention: stops when a 0-codepoint is encountered |
|
20874 | 2670 |
(for compatibility with the hash used by the VM) |
19743 | 2671 |
Also: on 64bit CPUs, only small 4-byte hashvalues are returned, |
20874 | 2672 |
(so hash values are independent from the architecture)" |
19743 | 2673 |
|
2674 |
|h byte| |
|
16887 | 2675 |
|
2676 |
h := 2166136261. |
|
2677 |
self do:[:eachChar | |
|
20874 | 2678 |
byte := eachChar codePoint. |
2679 |
byte == 0 ifTrue:[ |
|
2680 |
"/ stop |
|
2681 |
^ (h bitXor: (h >> 30)) bitAnd: 16r3FFFFFFF. |
|
2682 |
]. |
|
2683 |
h := h bitXor:byte. |
|
2684 |
h := (h * 16777619) bitAnd:16rFFFFFFFF. |
|
16887 | 2685 |
]. |
16898 | 2686 |
"/ make sure, it fits into a smallInt |
2687 |
h := (h bitXor: (h >> 30)) bitAnd: 16r3FFFFFFF. |
|
16887 | 2688 |
^ h |
2689 |
||
2690 |
" |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2691 |
'abc' hash_fnv1a |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2692 |
'abc' asUnicode16String hash_fnv1a |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2693 |
'abc' asUnicode32String hash_fnv1a |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2694 |
|
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2695 |
'foofooHelloWorld' hash_fnv1a |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2696 |
'foofooHelloWorld' asUnicode16String hash_fnv1a |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2697 |
'foofooHelloWorld' asUnicode32String hash_fnv1a |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2698 |
|
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2699 |
'blablaHelloWorld' hash_fnv1a |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2700 |
'blablaHelloWorld' asUnicode16String hash_fnv1a |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2701 |
'blablaHelloWorld' asUnicode32String hash_fnv1a |
16887 | 2702 |
" |
2703 |
! |
|
2704 |
||
23863 | 2705 |
hash_fnv1a_64 |
2706 |
"return a 64bit integer useful as a hash-key. |
|
2707 |
This method uses the fnv-1a algorithm |
|
2708 |
(which is actually a very good one)." |
|
2709 |
||
2710 |
|h byte| |
|
2711 |
||
2712 |
h := 14695981039346656037. |
|
2713 |
self do:[:eachChar | |
|
2714 |
byte := eachChar codePoint. |
|
2715 |
h := h bitXor:byte. |
|
2716 |
h := (h * 1099511628211) bitAnd:16rFFFFFFFFFFFFFFFF. |
|
2717 |
]. |
|
2718 |
"/ make sure, it fits into 64 bit |
|
2719 |
h := (h bitXor: (h >> 30)) bitAnd: 16r3FFFFFFFFFFFFFFF. |
|
2720 |
^ h |
|
2721 |
||
2722 |
" |
|
2723 |
'abc' hash_fnv1a_64 |
|
2724 |
'abc' asUnicode16String hash_fnv1a_64 |
|
2725 |
'abc' asUnicode32String hash_fnv1a_64 |
|
2726 |
||
2727 |
'foofooHelloWorld' hash_fnv1a_64 |
|
2728 |
'foofooHelloWorld' asUnicode16String hash_fnv1a_64 |
|
2729 |
'foofooHelloWorld' asUnicode32String hash_fnv1a_64 |
|
2730 |
||
2731 |
'blablaHelloWorld' hash_fnv1a_64 |
|
2732 |
'blablaHelloWorld' asUnicode16String hash_fnv1a_64 |
|
2733 |
'blablaHelloWorld' asUnicode32String hash_fnv1a_64 |
|
2734 |
" |
|
2735 |
||
2736 |
"Created: / 11-03-2019 / 11:47:15 / Claus Gittinger" |
|
2737 |
! |
|
2738 |
||
16887 | 2739 |
hash_java |
2740 |
"return an integer useful as a hash-key. |
|
2741 |
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
|
2742 |
the java virtual machine |
16887 | 2743 |
(which is actually not a very good one)." |
2744 |
||
2745 |
|h| |
|
2746 |
||
2747 |
h := 0. |
|
2748 |
self do:[:eachChar | |
|
20874 | 2749 |
h := (h * 31) + (eachChar codePoint). |
2750 |
h := h bitAnd:16rFFFFFFFF. |
|
16887 | 2751 |
]. |
2752 |
^ h |
|
2753 |
||
2754 |
" |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2755 |
'abc' hash_java |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2756 |
'foofooHelloWorld' hash_java |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2757 |
'blablaHelloWorld' hash_java |
16887 | 2758 |
" |
2759 |
! |
|
2760 |
||
16898 | 2761 |
hash_sdbm |
2762 |
"return an integer useful as a hash-key. |
|
2763 |
This method implements the sdbm algorithm." |
|
2764 |
||
2765 |
|h| |
|
2766 |
||
2767 |
"/ |
|
2768 |
"/ this is the sdbm algorithm |
|
2769 |
"/ |
|
2770 |
h := 0. |
|
2771 |
self do:[:char | |
|
20874 | 2772 |
h := (65599 times:h) plus:char codePoint. |
16898 | 2773 |
]. |
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2774 |
^ h |
16898 | 2775 |
|
2776 |
" |
|
2777 |
'a' hash |
|
2778 |
'a' asUnicode16String hash |
|
2779 |
'aa' hash |
|
2780 |
'aa' asUnicode16String hash |
|
2781 |
'ab' hash |
|
2782 |
'ab' asUnicode16String hash |
|
2783 |
'ab' hash |
|
2784 |
'ab' asArray hash |
|
2785 |
" |
|
2786 |
||
2787 |
" |
|
20874 | 2788 |
|syms ms| |
2789 |
||
2790 |
syms := Symbol allInstances. |
|
2791 |
Transcript show:'syms: '; showCR:syms size. |
|
2792 |
Transcript show:'sdbm hashes: '; showCR:(syms collect:[:s| s hash]) asSet size. |
|
2793 |
Transcript show:'dragonBook hashes: '; showCR:(syms collect:[:s| s hash_dragonBook]) asSet size. |
|
2794 |
||
2795 |
ms := Time millisecondsToRun:[ |
|
2796 |
10 timesRepeat:[ |
|
2797 |
syms do:[:each| each hash]. |
|
2798 |
]. |
|
2799 |
]. |
|
2800 |
Transcript show:'sdbm hash: '; showCR:ms. |
|
2801 |
||
2802 |
ms := Time millisecondsToRun:[ |
|
2803 |
10 timesRepeat:[ |
|
2804 |
syms do:[:each| each hash_dragonBook]. |
|
2805 |
]. |
|
2806 |
]. |
|
2807 |
Transcript show:'dragonBook: '; showCR:ms. |
|
2808 |
||
2809 |
syms := syms collect:[:each| each asUnicode16String]. |
|
2810 |
ms := Time millisecondsToRun:[ |
|
2811 |
10 timesRepeat:[ |
|
2812 |
syms do:[:each| each hash]. |
|
2813 |
]. |
|
2814 |
]. |
|
2815 |
Transcript show:'unicode sdbm hash: '; showCR:ms. |
|
2816 |
||
2817 |
ms := Time millisecondsToRun:[ |
|
2818 |
10 timesRepeat:[ |
|
2819 |
syms do:[:each| each hash_dragonBook]. |
|
2820 |
]. |
|
2821 |
]. |
|
2822 |
Transcript show:'unicode dragonBook:'; showCR:ms. |
|
16898 | 2823 |
" |
2824 |
||
2825 |
"Modified: / 26-12-2011 / 14:09:07 / cg" |
|
2826 |
! |
|
2827 |
||
11861 | 2828 |
levenshteinTo:aString |
2829 |
"return the levenshtein distance to the argument, aString; |
|
2830 |
this value corresponds to the number of replacements that have to be |
|
2831 |
made to get aString from the receiver. |
|
2832 |
See IEEE transactions on Computers 1976 Pg 172 ff." |
|
2833 |
||
2834 |
" |
|
21760 | 2835 |
in the following, we assume that omiting a character |
11861 | 2836 |
is less of an error than inserting an extra character. |
2837 |
Therefore the different insertion (i) and deletion (d) values. |
|
20874 | 2838 |
s: substitution weight (4) |
2839 |
k: keyboard weight (k) (typing a nearby key) - or nil (then use s) |
|
2840 |
c: case weight (4) - or nil (then use s) |
|
2841 |
e: exchange weight (8) - or nil (then use s*2) |
|
2842 |
i: insertion of extra character weight (2) |
|
2843 |
d: delete of a character weight (6) |
|
22157 | 2844 |
|
2845 |
Notice that the standard levenshtein uses the same weight for insertion and deletion, |
|
2846 |
and is computed by: |
|
2847 |
'flaw' levenshteinTo:'lawn' s:2 k:nil c:nil i:1 d:1 |
|
2848 |
||
11861 | 2849 |
" |
2850 |
||
2851 |
^ StringUtilities |
|
20874 | 2852 |
levenshteinDistanceFrom:self |
2853 |
to:aString |
|
2854 |
s:4 k:4 c:4 e:nil i:2 d:6 |
|
12177 | 2855 |
|
2856 |
" |
|
2857 |
'computer' levenshteinTo:'computer' |
|
11861 | 2858 |
'cOmputer' levenshteinTo:'computer' |
12177 | 2859 |
'cOmpuTer' levenshteinTo:'computer' |
11861 | 2860 |
'cimputer' levenshteinTo:'computer' |
2861 |
'cumputer' levenshteinTo:'computer' |
|
2862 |
||
2863 |
'cmputer' levenshteinTo:'computer' |
|
2864 |
'coomputer' levenshteinTo:'computer' |
|
2865 |
||
2866 |
'ocmprt' levenshteinTo:'computer' |
|
2867 |
'computer' levenshteinTo:'computer' |
|
2868 |
'ocmputer' levenshteinTo:'computer' |
|
2869 |
'cmputer' levenshteinTo:'computer' |
|
2870 |
'computer' levenshteinTo:'cmputer' |
|
2871 |
'Computer' levenshteinTo:'computer' |
|
2872 |
||
12177 | 2873 |
'compiter' levenshteinTo:'computer' |
2874 |
'compoter' levenshteinTo:'computer' |
|
2875 |
||
2876 |
'comptuer' levenshteinTo:'computer' |
|
11861 | 2877 |
" |
21760 | 2878 |
|
2879 |
"Modified (comment): / 17-05-2017 / 16:15:41 / mawalch" |
|
22157 | 2880 |
"Modified (comment): / 02-08-2017 / 17:13:04 / cg" |
11861 | 2881 |
! |
2882 |
||
2883 |
levenshteinTo:aString s:substWeight k:kbdTypoWeight c:caseWeight i:insrtWeight d:deleteWeight |
|
2884 |
"parametrized levenshtein. |
|
2885 |
return the levenshtein distance to the argument, aString; |
|
2886 |
this value corrensponds to the number of replacements that have to be |
|
2887 |
made to get aString from the receiver. |
|
2888 |
The arguments are the costs for |
|
20874 | 2889 |
s:substitution, |
2890 |
k:keyboard type (substitution), |
|
2891 |
c:case-change, |
|
2892 |
i:insertion |
|
2893 |
d:deletion |
|
11861 | 2894 |
of a character. |
2895 |
See IEEE transactions on Computers 1976 Pg 172 ff" |
|
2896 |
||
2897 |
^ StringUtilities |
|
20874 | 2898 |
levenshteinDistanceFrom:self |
2899 |
to:aString |
|
2900 |
s:substWeight k:kbdTypoWeight c:caseWeight e:nil i:insrtWeight d:deleteWeight |
|
11861 | 2901 |
! |
2902 |
||
9190 | 2903 |
sameAs:aString |
2904 |
"Compare the receiver with the argument like =, but ignore case differences. |
|
2905 |
Return true or false." |
|
2906 |
||
2907 |
|mySize "{ Class: SmallInteger }" |
|
2908 |
otherSize c1 c2| |
|
2909 |
||
9192 | 2910 |
self == aString ifTrue:[^ true]. |
2911 |
||
2912 |
mySize := self size. |
|
2913 |
otherSize := aString string size. |
|
18884 | 2914 |
mySize ~~ otherSize ifTrue:[^ false]. |
9192 | 2915 |
|
2916 |
1 to:mySize do:[:index | |
|
20874 | 2917 |
c1 := self at:index. |
2918 |
c2 := aString at:index. |
|
2919 |
c1 ~~ c2 ifTrue:[ |
|
2920 |
(c1 sameAs:c2) ifFalse:[^ false]. |
|
2921 |
] |
|
9192 | 2922 |
]. |
2923 |
^ true |
|
2924 |
||
608 | 2925 |
" |
8874 | 2926 |
'foo' sameAs: 'Foo' |
2927 |
'foo' sameAs: 'bar' |
|
2928 |
'foo' sameAs: 'foo' |
|
9192 | 2929 |
" |
2930 |
||
2931 |
"Modified: 22.4.1996 / 15:56:17 / cg" |
|
2932 |
! |
|
608 | 2933 |
|
15734 | 2934 |
sameAs:aString caseSensitive:caseSensitive |
2935 |
"Compare the receiver with the argument. |
|
2936 |
If caseSensitive is false, this is the same as #sameAs:, |
|
2937 |
if false, this is the same as #=." |
|
2938 |
||
2939 |
caseSensitive ifFalse:[ |
|
20874 | 2940 |
^ self sameAs:aString |
15734 | 2941 |
]. |
2942 |
^ self = aString |
|
2943 |
||
2944 |
" |
|
2945 |
'foo' sameAs:'Foo' caseSensitive:false |
|
2946 |
'foo' sameAs:'foo' caseSensitive:true |
|
2947 |
" |
|
2948 |
! |
|
2949 |
||
5242 | 2950 |
sameAs:aString ignoreCase:ignoreCase |
15734 | 2951 |
<resource: #obsolete> |
5242 | 2952 |
"Compare the receiver with the argument. |
2953 |
If ignoreCase is true, this is the same as #sameAs:, |
|
2954 |
if false, this is the same as #=." |
|
2955 |
||
2956 |
ignoreCase ifTrue:[ |
|
20874 | 2957 |
^ self sameAs:aString |
5242 | 2958 |
]. |
2959 |
^ self = aString |
|
2960 |
||
2961 |
" |
|
8874 | 2962 |
'foo' sameAs:'Foo' ignoreCase:false |
2963 |
'foo' sameAs:'foo' ignoreCase:true |
|
5242 | 2964 |
" |
2965 |
||
2966 |
! |
|
2967 |
||
608 | 2968 |
sameCharacters:aString |
9192 | 2969 |
"count & return the number of characters which are the same |
2970 |
(ignoring case and emphasis) in the receiver and the argument, aString." |
|
2971 |
||
2972 |
|n "{ Class: SmallInteger }" |
|
2973 |
c1 c2 cnt| |
|
2974 |
||
2975 |
n := self size. |
|
2976 |
n := n min:(aString string size). |
|
2977 |
||
2978 |
cnt := 0. |
|
2979 |
1 to:n do:[:index | |
|
20874 | 2980 |
c1 := self at:index. |
2981 |
c2 := aString at:index. |
|
2982 |
((c1 == c2) |
|
2983 |
or:[c1 asLowercase = c2 asLowercase]) ifTrue:[ |
|
2984 |
cnt := cnt + 1 |
|
2985 |
] |
|
9192 | 2986 |
]. |
2987 |
^ cnt |
|
2988 |
||
2989 |
" |
|
2990 |
'foobarbaz' sameCharacters: 'foo' |
|
8874 | 2991 |
'foobarbaz' sameCharacters: 'Foo' |
2992 |
'foobarbaz' sameCharacters: 'baz' |
|
608 | 2993 |
" |
5510
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
2994 |
! |
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
2995 |
|
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
2996 |
sameEmphasisAs:aStringOrText |
17215 | 2997 |
"compare the receiver's and the argument's emphasis" |
5510
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
2998 |
|
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
2999 |
^ self emphasis = aStringOrText emphasis |
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
3000 |
|
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
3001 |
" |
8874 | 3002 |
'hello' asText sameEmphasisAs: 'hello' |
3003 |
'hello' asText sameEmphasisAs: 'hello' asText |
|
3004 |
'hello' asText allBold sameEmphasisAs: 'hello' |
|
3005 |
'hello' asText allBold sameEmphasisAs: 'fooba' asText allBold |
|
9192 | 3006 |
'hello' asText allBold sameEmphasisAs: 'fooba' asText allItalic |
3007 |
" |
|
3008 |
! |
|
3009 |
||
9191 | 3010 |
sameStringAndEmphasisAs:aStringOrText |
5739
4fd3be7ccc75
oops some relational ops did accept a non-string arg
Claus Gittinger <cg@exept.de>
parents:
5732
diff
changeset
|
3011 |
"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
|
3012 |
|
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
3013 |
aStringOrText isString ifFalse:[^ false]. |
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
3014 |
(self string = aStringOrText string) ifFalse:[^ false]. |
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
3015 |
self hasChangeOfEmphasis = aStringOrText hasChangeOfEmphasis ifFalse:[^ false]. |
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
3016 |
^ self emphasis = aStringOrText emphasis |
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
3017 |
|
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
3018 |
" |
8874 | 3019 |
'hello' asText sameEmphasisAs: 'hello' |
3020 |
'hello' asText sameEmphasisAs: 'hello' asText |
|
3021 |
'hello' asText allBold sameEmphasisAs: 'hello' |
|
3022 |
'hello' asText allBold sameEmphasisAs: 'fooba' asText allBold |
|
3023 |
'hello' asText allBold sameEmphasisAs: 'fooba' asText allItalic |
|
3024 |
||
3025 |
'hello' sameEmphasisAs: 'hello' asText |
|
3026 |
'hello' sameEmphasisAs: 'hello' asText allBold |
|
3027 |
'hello' sameEmphasisAs: 'fooba' |
|
3028 |
'hello' sameEmphasisAs: 'fooba' asText |
|
3029 |
'hello' sameEmphasisAs: 'fooba' asText allBold |
|
3030 |
'hello' sameEmphasisAs: 'fooba' asText allItalic |
|
3031 |
||
3032 |
'hello' asText sameStringAndEmphasisAs: 'hello' |
|
3033 |
'hello' asText sameStringAndEmphasisAs: 'hello' asText |
|
3034 |
'hello' asText allBold sameStringAndEmphasisAs: 'hello' |
|
3035 |
'hello' asText allBold sameStringAndEmphasisAs: 'fooba' asText allBold |
|
3036 |
'hello' asText allBold sameStringAndEmphasisAs: 'fooba' asText allItalic |
|
3037 |
||
3038 |
'hello' sameStringAndEmphasisAs: 'hello' asText |
|
3039 |
'hello' sameStringAndEmphasisAs: 'hello' asText allBold |
|
3040 |
'hello' sameStringAndEmphasisAs: 'fooba' |
|
3041 |
'hello' sameStringAndEmphasisAs: 'fooba' asText |
|
3042 |
'hello' sameStringAndEmphasisAs: 'fooba' asText allBold |
|
3043 |
'hello' sameStringAndEmphasisAs: 'fooba' asText allItalic |
|
5510
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
3044 |
" |
11861 | 3045 |
! |
3046 |
||
3047 |
spellAgainst: aString |
|
3048 |
"return an integer between 0 and 100 indicating how similar |
|
3049 |
the argument is to the receiver. No case conversion is done. |
|
3050 |
This algorithm is much simpler (but also less exact) than the |
|
3051 |
levenshtein distance. Experiment which is better for your |
|
3052 |
application." |
|
3053 |
||
3054 |
| i1 "{ Class: SmallInteger }" |
|
3055 |
i2 "{ Class: SmallInteger }" |
|
3056 |
next1 "{ Class: SmallInteger }" |
|
3057 |
next2 "{ Class: SmallInteger }" |
|
3058 |
size1 "{ Class: SmallInteger }" |
|
3059 |
size2 "{ Class: SmallInteger }" |
|
3060 |
score "{ Class: SmallInteger }" |
|
3061 |
maxLen "{ Class: SmallInteger }" | |
|
3062 |
||
3063 |
size1 := self size. |
|
3064 |
size2 := aString size. |
|
3065 |
maxLen := size1 max:size2. |
|
3066 |
score := 0. |
|
3067 |
i1 := i2 := 1. |
|
3068 |
[i1 <= size1 and: [i2 <= size2]] whileTrue:[ |
|
20874 | 3069 |
next1 := i1 + 1. |
3070 |
next2 := i2 + 1. |
|
3071 |
(self at:i1) == (aString at:i2) ifTrue: [ |
|
3072 |
score := score+1. |
|
3073 |
i1 := next1. |
|
3074 |
i2 := next2 |
|
3075 |
] ifFalse: [ |
|
3076 |
(i2 < size2 and: [(self at:i1) == (aString at:next2)]) ifTrue: [ |
|
3077 |
i2 := next2 |
|
3078 |
] ifFalse: [ |
|
3079 |
(i1 < size1 and: [(self at:next1) == (aString at:i2)]) ifTrue: [ |
|
3080 |
i1 := next1 |
|
3081 |
] ifFalse: [ |
|
3082 |
i1 := next1. |
|
3083 |
i2 := next2 |
|
3084 |
] |
|
3085 |
] |
|
3086 |
] |
|
11861 | 3087 |
]. |
3088 |
||
3089 |
score == maxLen ifTrue: [^ 100]. |
|
3090 |
^ 100 * score // maxLen |
|
3091 |
||
3092 |
" |
|
3093 |
'Smalltalk' spellAgainst: 'Smalltlak' |
|
3094 |
'Smalltalk' spellAgainst: 'smalltlak' |
|
3095 |
'Smalltalk' spellAgainst: 'smalltalk' |
|
3096 |
'Smalltalk' spellAgainst: 'smalltlk' |
|
3097 |
'Smalltalk' spellAgainst: 'Smalltolk' |
|
3098 |
" |
|
3099 |
! |
|
3100 |
||
16553 | 3101 |
startsWith:aStringOrCharacter |
3102 |
"return true, if the receiver starts with something, aStringOrCharacter. |
|
17371 | 3103 |
If the argument is empty, true is returned. |
3104 |
Notice, that this is similar to, but slightly different from VW's and Squeak's beginsWith:, |
|
3105 |
which are both inconsistent w.r.t. an empty argument." |
|
11861 | 3106 |
|
3107 |
|s| |
|
3108 |
||
16553 | 3109 |
aStringOrCharacter isCharacter ifTrue:[ |
23548 | 3110 |
^ (self size ~~ 0) and:[(self at:1) = aStringOrCharacter] |
16553 | 3111 |
]. |
11861 | 3112 |
(s := self string) ~~ self ifTrue:[ |
20874 | 3113 |
^ s startsWith:aStringOrCharacter |
11861 | 3114 |
]. |
16553 | 3115 |
^ super startsWith:aStringOrCharacter |
11861 | 3116 |
|
3117 |
" |
|
17364 | 3118 |
'abcde' startsWith:#($a $b $c) |
3119 |
'abcde' startsWith:'abc' |
|
3120 |
'abcd' startsWith:'abcde' |
|
3121 |
'abcde' startsWith:'abd' |
|
11861 | 3122 |
'hello world' startsWith:'hello' |
3123 |
'hello world' asText allBold startsWith:'hello' |
|
3124 |
'hello world' asText allBold startsWith:'' |
|
3125 |
" |
|
3126 |
||
23154 | 3127 |
"Created: / 12-05-1996 / 15:46:40 / cg" |
3128 |
"Modified: / 29-06-2018 / 11:27:08 / Claus Gittinger" |
|
23548 | 3129 |
"Modified (format): / 26-11-2018 / 14:15:31 / Stefan Vogel" |
23154 | 3130 |
! |
3131 |
||
3132 |
startsWith:aStringOrCharacter caseSensitive:caseSensitive |
|
3133 |
"return true, if the receiver starts with something, aStringOrCharacter. |
|
3134 |
If the argument is empty, true is returned. |
|
3135 |
Notice, that this is similar to, but slightly different from VW's and Squeak's beginsWith:, |
|
3136 |
which are both inconsistent w.r.t. an empty argument." |
|
3137 |
||
3138 |
|s| |
|
3139 |
||
23548 | 3140 |
caseSensitive ifTrue:[ |
3141 |
^ self startsWith:aStringOrCharacter |
|
3142 |
]. |
|
23154 | 3143 |
aStringOrCharacter isCharacter ifTrue:[ |
23548 | 3144 |
self size == 0 ifTrue:[ |
3145 |
^ false. |
|
3146 |
]. |
|
3147 |
s := self at:1. |
|
3148 |
^ s == aStringOrCharacter or:[s asLowercase = aStringOrCharacter asLowercase] |
|
23154 | 3149 |
]. |
3150 |
(s := self string) ~~ self ifTrue:[ |
|
3151 |
^ s startsWith:aStringOrCharacter caseSensitive:caseSensitive |
|
3152 |
]. |
|
3153 |
^ self startsWith:aStringOrCharacter using:[:a :b | a asLowercase = b asLowercase]. |
|
3154 |
||
3155 |
" |
|
3156 |
'aBCde' startsWith:'abc' caseSensitive:true |
|
3157 |
'aBCde' startsWith:'abc' caseSensitive:false |
|
3158 |
" |
|
3159 |
||
3160 |
"Created: / 29-06-2018 / 11:27:04 / Claus Gittinger" |
|
23548 | 3161 |
"Modified: / 26-11-2018 / 14:18:43 / Stefan Vogel" |
9192 | 3162 |
! ! |
3163 |
||
3164 |
!CharacterArray methodsFor:'converting'! |
|
3165 |
||
11881 | 3166 |
asAsciiZ |
3167 |
"if the receiver does not end with a 0-valued character, return a copy of it, |
|
3168 |
with an additional 0-character. Otherwise return the receiver. This is sometimes |
|
3169 |
needed when a string has to be passed to C, which needs 0-terminated strings. |
|
3170 |
Notice, that all singleByte strings are already 0-terminated in ST/X, whereas wide |
|
3171 |
strings are not." |
|
3172 |
||
12658 | 3173 |
(self notEmpty and:[self last codePoint == 0]) ifTrue:[^ self ]. |
11881 | 3174 |
^ self copyWith:(Character value:0). |
3175 |
||
3176 |
" |
|
3177 |
'abc' asAsciiZ |
|
3178 |
'abc' asWideString asAsciiZ |
|
3179 |
" |
|
3180 |
! |
|
3181 |
||
13524 | 3182 |
asByteArray |
15936 | 3183 |
"depending on the size of the characters in the receiver, |
3184 |
return a byteArray containing single-, double- or even 4-bytewise values. |
|
3185 |
The size of the returned byteArray will be the strings size multiplied by the |
|
3186 |
size required for the largest character. |
|
19233 | 3187 |
Attention: The bytes are in native byte order. |
21745 | 3188 |
Caveat: better use utf8Encoded, to get reproducible results" |
15936 | 3189 |
|
14555 | 3190 |
|bytes sz bytesPerCharacter idx str| |
3191 |
||
3192 |
str := self string. |
|
3193 |
str ~~ self ifTrue:[ |
|
20874 | 3194 |
"/ for text and other wrappers |
3195 |
^ str asByteArray |
|
13611
ba8c7372218a
variable renamed in: #inspector2TabBytes
Claus Gittinger <cg@exept.de>
parents:
13524
diff
changeset
|
3196 |
]. |
ba8c7372218a
variable renamed in: #inspector2TabBytes
Claus Gittinger <cg@exept.de>
parents:
13524
diff
changeset
|
3197 |
|
ba8c7372218a
variable renamed in: #inspector2TabBytes
Claus Gittinger <cg@exept.de>
parents:
13524
diff
changeset
|
3198 |
"/ for real strings, a fallback |
13524 | 3199 |
sz := self size. |
19757 | 3200 |
bytesPerCharacter := self bytesPerCharacter. |
13524 | 3201 |
bytes := ByteArray new:(sz * bytesPerCharacter). |
3202 |
idx := 1. |
|
3203 |
self do:[:char | |
|
20874 | 3204 |
|code| |
3205 |
||
3206 |
code := char codePoint. |
|
3207 |
bytesPerCharacter == 2 ifTrue:[ |
|
3208 |
bytes unsignedInt16At:idx put:code |
|
3209 |
] ifFalse:[ |
|
3210 |
bytesPerCharacter == 4 ifTrue:[ |
|
3211 |
bytes unsignedInt32At:idx put:code |
|
3212 |
] ifFalse:[ |
|
3213 |
bytes at:idx put:code |
|
3214 |
]. |
|
3215 |
]. |
|
3216 |
idx := idx + bytesPerCharacter. |
|
13524 | 3217 |
]. |
3218 |
^ bytes |
|
3219 |
||
3220 |
"Created: / 27-07-2011 / 00:56:17 / cg" |
|
21745 | 3221 |
"Modified (comment): / 11-05-2017 / 09:21:57 / mawalch" |
13524 | 3222 |
! |
3223 |
||
19233 | 3224 |
asByteArrayMSB:msb |
3225 |
"depending on the size of the characters in the receiver, |
|
3226 |
return a byteArray containing single-, double- or even 4-bytewise values. |
|
3227 |
The size of the returned byteArray will be the strings size multiplied by the |
|
3228 |
size required for the largest character. |
|
21745 | 3229 |
Caveat: better use utf8Encoded, to get reproducible results" |
19233 | 3230 |
|
3231 |
|ba| |
|
3232 |
||
3233 |
ba := self asByteArray. "/ native order |
|
3234 |
UninterpretedBytes isBigEndian ~~ msb ifTrue:[ |
|
20874 | 3235 |
ba swapBytes |
19233 | 3236 |
]. |
3237 |
^ ba |
|
21745 | 3238 |
|
3239 |
"Modified (comment): / 11-05-2017 / 09:22:01 / mawalch" |
|
19233 | 3240 |
! |
3241 |
||
20480 | 3242 |
asCanonicalizedFilename |
3243 |
"return a Filename with pathname taken from the receiver. |
|
3244 |
The filename is canonicalized, meaning that it cares for trailing directory separators, |
|
3245 |
'.' components etc." |
|
3246 |
||
3247 |
^ self asFilename asCanonicalizedFilename |
|
3248 |
||
3249 |
"on windows: |
|
3250 |
'c:\foo\bar' asFilename |
|
3251 |
'c:\foo\bar\' asFilename |
|
3252 |
'c:\foo\bar\..\baz' asFilename |
|
3253 |
'c:\foo\bar\..\baz\.' asFilename |
|
3254 |
'c:\foo\bar' asCanonicalizedFilename |
|
3255 |
'c:\foo\bar\' asCanonicalizedFilename |
|
3256 |
'c:\foo\bar\..\baz' asCanonicalizedFilename |
|
3257 |
'c:\foo\bar\..\baz\.' asCanonicalizedFilename |
|
3258 |
||
3259 |
on unix: |
|
3260 |
'/foo/bar' asFilename |
|
3261 |
'/foo/bar/' asFilename |
|
3262 |
'/foo/bar/../baz' asFilename |
|
3263 |
'/foo/bar/../baz/.' asFilename |
|
3264 |
'/foo/bar' asCanonicalizedFilename |
|
3265 |
'/foo/bar/' asCanonicalizedFilename |
|
3266 |
'/foo/bar/../baz' asCanonicalizedFilename |
|
3267 |
'/foo/bar/../baz/.' asCanonicalizedFilename |
|
3268 |
" |
|
3269 |
! |
|
3270 |
||
608 | 3271 |
asCollectionOfLines |
23187 | 3272 |
"return a collection containing the lines (separated by cr) of the receiver. |
3273 |
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
|
3274 |
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
|
3275 |
See also #asCollectionOfLinesWithReturn |
d7c3e67647f1
comment/format in: #asCollectionOfLines
Claus Gittinger <cg@exept.de>
parents:
14455
diff
changeset
|
3276 |
(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
|
3277 |
this method and we did not want to risk any incompatibilities)" |
14455
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3278 |
|
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3279 |
^ self asCollectionOfSubstringsSeparatedBy:Character cr. |
608 | 3280 |
|
3281 |
" |
|
3282 |
'1 one\2 two\3 three\4 four\5 five' withCRs asCollectionOfLines |
|
8874 | 3283 |
'1 one\\\\2 two\3 three' withCRs asCollectionOfLines |
20182 | 3284 |
|
3285 |
('foo \r\nbar\nbaz\t\r\n\r\nbla' printf:#()) |
|
3286 |
asCollectionOfLines collect:[:l | (l endsWith:Character return) ifTrue:[l copyButLast:1] ifFalse:l] |
|
608 | 3287 |
" |
23187 | 3288 |
|
3289 |
"Modified (comment): / 03-07-2018 / 10:59:36 / Claus Gittinger" |
|
608 | 3290 |
! |
3291 |
||
14455
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3292 |
asCollectionOfLinesWithReturn |
23187 | 3293 |
"return a collection containing the lines (separated by cr) of the receiver. |
3294 |
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
|
3295 |
|
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3296 |
|lines| |
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3297 |
|
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3298 |
lines := self asCollectionOfSubstringsSeparatedBy:Character cr. |
18194 | 3299 |
(lines notEmpty and:[lines last isEmpty]) ifTrue:[ |
20874 | 3300 |
^ lines copyButLast:1 |
14455
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3301 |
]. |
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3302 |
^ lines |
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3303 |
|
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3304 |
" |
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3305 |
'1\2\3' withCRs asCollectionOfLines |
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3306 |
'1\2\3\' withCRs asCollectionOfLines |
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3307 |
'1\2\3' withCRs asCollectionOfLinesWithReturn |
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3308 |
'1\2\3\' withCRs asCollectionOfLinesWithReturn |
18194 | 3309 |
'' withCRs asCollectionOfLinesWithReturn |
14455
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3310 |
" |
23187 | 3311 |
|
3312 |
"Modified (comment): / 03-07-2018 / 10:59:47 / Claus Gittinger" |
|
14455
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3313 |
! |
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3314 |
|
608 | 3315 |
asCollectionOfSubstringsSeparatedBy:aCharacter |
23187 | 3316 |
"return a collection containing substrings (separated by aCharacter) of the receiver. |
15740 | 3317 |
If aCharacter occurs multiple times in a row, the result will contain empty strings. |
23717 | 3318 |
If the receiver starts with aCharacter, an empty string with be the first result element. |
3319 |
If the receiver ends with aCharacter, NO empty string with be the last result element." |
|
608 | 3320 |
|
9192 | 3321 |
^ self asCollectionOfSubCollectionsSeparatedBy:aCharacter |
3322 |
||
3323 |
" |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
3324 |
'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
|
3325 |
'1 one:2 two:3 three:4 four:5 five:' asCollectionOfSubstringsSeparatedBy:$: |
15740 | 3326 |
'1 one 2 two 3 three 4 four 5 five' asCollectionOfSubstringsSeparatedBy:Character space |
9192 | 3327 |
" |
23187 | 3328 |
|
23717 | 3329 |
"Modified (comment): / 11-02-2019 / 23:54:20 / Claus Gittinger" |
9192 | 3330 |
! |
608 | 3331 |
|
11828 | 3332 |
asCollectionOfSubstringsSeparatedBy:aCharacter exceptIn:ch |
23187 | 3333 |
"return a collection containing the substrings (separated by aCharacter) of the receiver. |
3334 |
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
|
3335 |
The separation is not done, inside a matching pair of ch-substrings. |
15740 | 3336 |
Can be used to tokenize csv-like strings, which may or may not be enclosed in quotes." |
11828 | 3337 |
|
3338 |
|lines myClass except i c |
|
3339 |
startIndex "{ Class:SmallInteger }" |
|
3340 |
stopIndex "{ Class:SmallInteger }" | |
|
3341 |
||
11905
cbc2191cfe2d
return stringCollections from asCollectionOfXXX
Claus Gittinger <cg@exept.de>
parents:
11904
diff
changeset
|
3342 |
lines := StringCollection new. |
11828 | 3343 |
myClass := self species. |
3344 |
||
3345 |
startIndex := 1. |
|
3346 |
except := false. |
|
3347 |
[ |
|
20874 | 3348 |
i := startIndex-1. |
3349 |
[ |
|
3350 |
i := i+1. |
|
3351 |
c := self at:i. |
|
3352 |
c = ch ifTrue:[ except := except not. ]. |
|
3353 |
i < self size and:[except or:[c ~= aCharacter]] |
|
3354 |
] whileTrue. |
|
3355 |
||
3356 |
c = aCharacter ifTrue:[ |
|
3357 |
stopIndex := i -1. |
|
3358 |
] ifFalse: [ |
|
3359 |
stopIndex := i. |
|
3360 |
]. |
|
3361 |
(stopIndex < startIndex) ifTrue: [ |
|
3362 |
lines add:(myClass new:0) |
|
3363 |
] ifFalse: [ |
|
3364 |
lines add:(self copyFrom:startIndex to:stopIndex) |
|
3365 |
]. |
|
3366 |
startIndex := stopIndex + 2. |
|
3367 |
startIndex <= self size |
|
11828 | 3368 |
] whileTrue. |
3369 |
^ lines |
|
3370 |
||
3371 |
" |
|
12177 | 3372 |
'asd''f;d''dd;s' asCollectionOfSubstringsSeparatedBy:$; exceptIn:$' |
3373 |
" |
|
3374 |
"/ 'asd "hello bla" foo "bla bla" bar' asCollectionOfSubstringsSeparatedBy:$ exceptIn:$" |
|
23187 | 3375 |
|
3376 |
"Modified (comment): / 03-07-2018 / 11:00:02 / Claus Gittinger" |
|
11828 | 3377 |
! |
3378 |
||
13344
ec6860c49990
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13343
diff
changeset
|
3379 |
asCollectionOfSubstringsSeparatedBy:aFieldSeparatorString textSeparator:aTextSeparatorOrNil |
13343
3f058ad0d2f1
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13332
diff
changeset
|
3380 |
"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
|
3381 |
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
|
3382 |
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
|
3383 |
|
13344
ec6860c49990
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13343
diff
changeset
|
3384 |
|aTextSeparatorChar items scanningWord inStream element lastIsFieldSeparator sz| |
ec6860c49990
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13343
diff
changeset
|
3385 |
|
ec6860c49990
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13343
diff
changeset
|
3386 |
aTextSeparatorOrNil isNil ifTrue:[ |
19961 | 3387 |
^ self asCollectionOfSubstringsSeparatedByAll: aFieldSeparatorString |
13327
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3388 |
]. |
13344
ec6860c49990
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13343
diff
changeset
|
3389 |
sz := aTextSeparatorOrNil size. |
ec6860c49990
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13343
diff
changeset
|
3390 |
sz = 0 ifTrue:[ |
19961 | 3391 |
aTextSeparatorChar := aTextSeparatorOrNil |
13344
ec6860c49990
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13343
diff
changeset
|
3392 |
] ifFalse:[sz = 1 ifTrue:[ |
19961 | 3393 |
"this is a String. Fetch the first character - compatibility to older expecco libs" |
3394 |
aTextSeparatorChar := aTextSeparatorOrNil first. |
|
13344
ec6860c49990
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13343
diff
changeset
|
3395 |
] ifFalse:[ |
19961 | 3396 |
self error:'textSeparatorSize > 1'. |
13344
ec6860c49990
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13343
diff
changeset
|
3397 |
]]. |
13327
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3398 |
|
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3399 |
items := OrderedCollection new. |
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3400 |
|
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3401 |
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
|
3402 |
[ |
19961 | 3403 |
inStream skipSeparators. |
3404 |
inStream atEnd |
|
13327
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3405 |
] whileFalse:[ |
19961 | 3406 |
lastIsFieldSeparator := false. |
3407 |
inStream peek == aTextSeparatorChar ifTrue:[ |
|
3408 |
inStream next. |
|
3409 |
element := ''. |
|
3410 |
scanningWord := true. |
|
3411 |
[ scanningWord and:[inStream atEnd not] ] whileTrue:[ |
|
3412 |
element := element , (inStream upTo:aTextSeparatorChar). |
|
3413 |
(inStream peek == aTextSeparatorChar) ifTrue:[ |
|
3414 |
element := element , aTextSeparatorChar . |
|
3415 |
inStream next. |
|
3416 |
] ifFalse:[ |
|
3417 |
scanningWord := false. |
|
3418 |
]. |
|
3419 |
]. |
|
3420 |
inStream upToAll_positionBefore:aFieldSeparatorString. |
|
3421 |
] ifFalse:[ |
|
3422 |
element := inStream upToAll_positionBefore:aFieldSeparatorString |
|
3423 |
]. |
|
3424 |
items add:element. |
|
3425 |
lastIsFieldSeparator := (inStream skipThroughAll:aFieldSeparatorString) notNil. |
|
13343
3f058ad0d2f1
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13332
diff
changeset
|
3426 |
]. |
3f058ad0d2f1
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13332
diff
changeset
|
3427 |
lastIsFieldSeparator ifTrue:[ |
19961 | 3428 |
"empty element at the end of the line" |
3429 |
items add:''. |
|
13327
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3430 |
]. |
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3431 |
|
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3432 |
^ items |
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3433 |
|
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3434 |
" |
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3435 |
self assert:(('#First#, #Second,SecondAdd#, #Third#' asCollectionOfSubstringsSeparatedBy:',' textSeparator: $#) |
19961 | 3436 |
sameContentsAs:#('First' 'Second,SecondAdd' 'Third')). |
13327
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3437 |
self assert:(('#Fir##st#, #Second,SecondAdd#, #Third#' asCollectionOfSubstringsSeparatedBy:',' textSeparator: $#) |
19961 | 3438 |
sameContentsAs:#('Fir#st' 'Second,SecondAdd' 'Third')). |
13327
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3439 |
self assert:(('#Fir##st#, Second,SecondAdd, #Third#' asCollectionOfSubstringsSeparatedBy:',' textSeparator: $#) |
19961 | 3440 |
sameContentsAs:#('Fir#st' 'Second' 'SecondAdd' 'Third')). |
13343
3f058ad0d2f1
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13332
diff
changeset
|
3441 |
self assert:(('First,Second,Third,,' asCollectionOfSubstringsSeparatedBy:',' textSeparator:nil) |
19961 | 3442 |
sameContentsAs:#('First' 'Second' 'Third' '' '')). |
13344
ec6860c49990
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13343
diff
changeset
|
3443 |
self assert:(('First,Second,Third,,' asCollectionOfSubstringsSeparatedBy:',' textSeparator:'#') |
19961 | 3444 |
sameContentsAs:#('First' 'Second' 'Third' '' '')). |
13327
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3445 |
" |
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3446 |
|
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3447 |
"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
|
3448 |
! |
999589445aa7
add new method for splitting a csv string into csv elements
ab
parents:
10437
diff
changeset
|
3449 |
|
7339
20969b5770c9
asCollectionOfSubstringsSeparatedByAll:
Claus Gittinger <cg@exept.de>
parents:
7318
diff
changeset
|
3450 |
asCollectionOfSubstringsSeparatedByAll:aSeparatorString |
23187 | 3451 |
"return a collection containing the lines (separated by aSeparatorString) of the receiver. |
3452 |
If aSeparatorString occurs multiple times in a row, |
|
7339
20969b5770c9
asCollectionOfSubstringsSeparatedByAll:
Claus Gittinger <cg@exept.de>
parents:
7318
diff
changeset
|
3453 |
the result will contain empty strings." |
20969b5770c9
asCollectionOfSubstringsSeparatedByAll:
Claus Gittinger <cg@exept.de>
parents:
7318
diff
changeset
|
3454 |
|
8852
9f5984eff2c2
use asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8794
diff
changeset
|
3455 |
^ self asCollectionOfSubCollectionsSeparatedByAll:aSeparatorString |
9f5984eff2c2
use asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8794
diff
changeset
|
3456 |
|
9f5984eff2c2
use asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8794
diff
changeset
|
3457 |
" |
8874 | 3458 |
'1::2::3::4::5::' asCollectionOfSubstringsSeparatedByAll:'::' |
7339
20969b5770c9
asCollectionOfSubstringsSeparatedByAll:
Claus Gittinger <cg@exept.de>
parents:
7318
diff
changeset
|
3459 |
" |
23187 | 3460 |
|
3461 |
"Modified (comment): / 03-07-2018 / 11:00:16 / Claus Gittinger" |
|
7339
20969b5770c9
asCollectionOfSubstringsSeparatedByAll:
Claus Gittinger <cg@exept.de>
parents:
7318
diff
changeset
|
3462 |
! |
20969b5770c9
asCollectionOfSubstringsSeparatedByAll:
Claus Gittinger <cg@exept.de>
parents:
7318
diff
changeset
|
3463 |
|
608 | 3464 |
asCollectionOfSubstringsSeparatedByAny:aCollectionOfSeparators |
3465 |
"return a collection containing the words (separated by any character |
|
3466 |
from aCollectionOfSeparators) of the receiver. |
|
3467 |
This allows breaking up strings using any character as separator." |
|
3468 |
||
8852
9f5984eff2c2
use asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8794
diff
changeset
|
3469 |
^ self asCollectionOfSubCollectionsSeparatedByAny:aCollectionOfSeparators |
608 | 3470 |
|
3471 |
" |
|
3472 |
'hello:world:isnt:this nice' asCollectionOfSubstringsSeparatedByAny:#($:) |
|
8874 | 3473 |
'hello:world:isnt:this nice' asCollectionOfSubstringsSeparatedByAny:':' |
3474 |
'hello:world:isnt:this nice' asCollectionOfSubstringsSeparatedByAny:(Array with:$: with:Character space) |
|
3475 |
'hello:world:isnt:this nice' asCollectionOfSubstringsSeparatedByAny:': ' |
|
3476 |
'h1e2l3l4o' asCollectionOfSubstringsSeparatedByAny:($1 to: $9) |
|
608 | 3477 |
" |
3478 |
! |
|
3479 |
||
3480 |
asCollectionOfWords |
|
8874 | 3481 |
"return a collection containing the words (separated by whitespace) |
3482 |
of the receiver. Multiple occurrences of whitespace characters will |
|
17517 | 3483 |
be treated like one - i.e. whitespace is skipped. |
3484 |
(sigh: it is called #'subStrings' in V'Age, and #'substrings' in Squeak) " |
|
608 | 3485 |
|
7797 | 3486 |
|words| |
608 | 3487 |
|
11905
cbc2191cfe2d
return stringCollections from asCollectionOfXXX
Claus Gittinger <cg@exept.de>
parents:
11904
diff
changeset
|
3488 |
words := StringCollection new. |
7797 | 3489 |
self asCollectionOfWordsDo:[:w | words add:w]. |
608 | 3490 |
^ words |
3491 |
||
3492 |
" |
|
3493 |
'hello world isnt this nice' asCollectionOfWords |
|
23660 | 3494 |
'hello world isnt this nice' asCollectionOfWordsDo:#transcribeCR |
608 | 3495 |
' hello world isnt this nice ' asCollectionOfWords |
3496 |
'hello' asCollectionOfWords |
|
3497 |
'' asCollectionOfWords |
|
3498 |
' ' asCollectionOfWords |
|
10703
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
3499 |
' foo bar__baz__bla__ bar ' asCollectionOfWords |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
3500 |
' foo __bar__baz__bla__ bar ' asCollectionOfWords |
608 | 3501 |
" |
23660 | 3502 |
|
3503 |
"Modified (comment): / 03-02-2019 / 13:07:57 / Claus Gittinger" |
|
9192 | 3504 |
! |
3505 |
||
3506 |
asCollectionOfWordsDo:aBlock |
|
8874 | 3507 |
"evaluate aBlock for each word (separated by whitespace) of the receiver. |
3508 |
Multiple occurrences of whitespace characters will be treated like one |
|
7797 | 3509 |
- i.e. whitespace is skipped. |
3510 |
Returns the number of words (i.e. the number of invocations of aBlock)." |
|
3511 |
||
3512 |
|count "{ Class:SmallInteger }" |
|
8874 | 3513 |
start "{ Class:SmallInteger }" |
3514 |
stop "{ Class:SmallInteger }" |
|
7797 | 3515 |
mySize "{ Class:SmallInteger }"| |
3516 |
||
3517 |
count := 0. |
|
3518 |
start := 1. |
|
3519 |
mySize := self size. |
|
3520 |
[start <= mySize] whileTrue:[ |
|
20874 | 3521 |
start := self indexOfNonSeparatorStartingAt:start. |
3522 |
start == 0 ifTrue:[ |
|
3523 |
^ count |
|
3524 |
]. |
|
3525 |
stop := self indexOfSeparatorStartingAt:start. |
|
3526 |
stop == 0 ifTrue:[ |
|
3527 |
aBlock value:(self copyFrom:start to:mySize). |
|
3528 |
^ count + 1 |
|
3529 |
]. |
|
3530 |
aBlock value:(self copyFrom:start to:(stop - 1)). |
|
3531 |
start := stop. |
|
3532 |
count := count + 1 |
|
7797 | 3533 |
]. |
3534 |
^ count |
|
3535 |
||
3536 |
" |
|
3537 |
'hello world isnt this nice' asCollectionOfWordsDo:[:w | Transcript showCR:w] |
|
23660 | 3538 |
'hello world isnt this nice' asCollectionOfWordsDo:#transcribeCR |
7797 | 3539 |
' hello world isnt this nice ' asCollectionOfWordsDo:[:w | Transcript showCR:w] |
3540 |
'hello' asCollectionOfWordsDo:[:w | Transcript showCR:w] |
|
3541 |
'' asCollectionOfWordsDo:[:w | Transcript showCR:w] |
|
3542 |
' ' asCollectionOfWordsDo:[:w | Transcript showCR:w] |
|
3543 |
" |
|
23660 | 3544 |
|
3545 |
"Modified (comment): / 03-02-2019 / 13:08:15 / Claus Gittinger" |
|
9192 | 3546 |
! |
3547 |
||
23971 | 3548 |
asDenseUnicodeString |
3549 |
"return the receiver as single-byte, double byte or 4-byte unicode string, |
|
3550 |
depending on the number of bits required to hold all characters in myself. |
|
3551 |
Use this to extract non-wide parts from a wide string, |
|
3552 |
i.e. after a substring has been copied out of a wide string" |
|
3553 |
||
3554 |
|nb| |
|
3555 |
||
3556 |
nb := self bytesPerCharacterNeeded. |
|
24034 | 3557 |
(nb ~~ self bytesPerCharacter) ifTrue:[ |
3558 |
nb == 1 ifTrue:[ |
|
3559 |
^ self asSingleByteString |
|
3560 |
]. |
|
3561 |
nb == 2 ifTrue:[ |
|
3562 |
^ self asUnicode16String |
|
23971 | 3563 |
]. |
3564 |
]. |
|
3565 |
^ self |
|
3566 |
||
3567 |
" |
|
3568 |
'abc' asUnicode16String asDenseUnicodeString |
|
3569 |
'abc' asUnicode32String asDenseUnicodeString |
|
3570 |
('abc',(Character value:16r165)) asDenseUnicodeString |
|
3571 |
('abc',(Character value:16r165)) asUnicode32String asDenseUnicodeString |
|
3572 |
" |
|
3573 |
||
3574 |
"Created: / 25-03-2019 / 16:28:02 / Claus Gittinger" |
|
24034 | 3575 |
"Modified: / 02-04-2019 / 10:48:44 / Claus Gittinger" |
23971 | 3576 |
! |
3577 |
||
9192 | 3578 |
asFilename |
3579 |
"return a Filename with pathname taken from the receiver" |
|
3580 |
||
3581 |
^ Filename named:self "(self asSingleByteStringReplaceInvalidWith:$?)" |
|
3582 |
||
3583 |
"Modified: 20.5.1996 / 09:38:15 / cg" |
|
3584 |
! |
|
608 | 3585 |
|
3060 | 3586 |
asFixedPoint |
3587 |
"read a fixedPoint number from the receiver. |
|
8874 | 3588 |
Notice, that errors may occur during the read, |
7423 | 3589 |
so you better setup some signal handler when using this method." |
3060 | 3590 |
|
3591 |
^ FixedPoint readFromString:self |
|
3592 |
||
3593 |
" |
|
8874 | 3594 |
'0.123' asFixedPoint |
3595 |
'12345' asFixedPoint |
|
3060 | 3596 |
'(1/5)' asFixedPoint |
3597 |
'foo' asFixedPoint |
|
9192 | 3598 |
Object errorSignal handle:[:ex | ex return:0] do:['foo' asFixedPoint] |
3599 |
" |
|
3600 |
||
3601 |
"Modified: / 25.10.1997 / 15:19:00 / cg" |
|
3602 |
! |
|
3603 |
||
3604 |
asFixedPoint:scale |
|
3060 | 3605 |
"read a fixedPoint number with scale number of post-decimal digits |
3606 |
from the receiver. Scale controls the number of displayed digits, |
|
3607 |
not the number of actually valid digits. |
|
8874 | 3608 |
Notice, that errors may occur during the read, |
7423 | 3609 |
so you better setup some signal handler when using this method." |
3060 | 3610 |
|
3611 |
^ (FixedPoint readFromString:self) scale:scale |
|
3612 |
||
3613 |
" |
|
8874 | 3614 |
'0.123' asFixedPoint:2 |
3615 |
'123456' asFixedPoint:2 |
|
3616 |
('3.14157' asFixedPoint:1) asFixedPoint:5 |
|
3617 |
'3.14157' asFixedPoint:2 |
|
3618 |
'foo' asFixedPoint:2 |
|
3060 | 3619 |
" |
3620 |
||
3621 |
"Modified: / 25.10.1997 / 15:21:57 / cg" |
|
9192 | 3622 |
! |
3623 |
||
3624 |
asFloat |
|
608 | 3625 |
"read a float number from the receiver. |
8874 | 3626 |
Notice, that errors may occur during the read, |
7422 | 3627 |
so you better setup some exception handler when using this method." |
608 | 3628 |
|
3629 |
^ (Number readFromString:self) asFloat |
|
3630 |
||
3631 |
" |
|
8874 | 3632 |
'0.123' asFloat |
608 | 3633 |
'12345' asFloat |
3634 |
'(1/5)' asFloat |
|
8874 | 3635 |
Object errorSignal handle:[:ex | ex return:0] do:['foo' asFloat] |
608 | 3636 |
" |
3637 |
! |
|
3638 |
||
3639 |
asInteger |
|
9190 | 3640 |
"convert the receiver into an integer. |
8874 | 3641 |
Notice, that errors may occur during the read, |
9190 | 3642 |
so you better setup some exception handler when using this method. |
3643 |
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
|
3644 |
in other smalltalks. |
9190 | 3645 |
For less strict integer reading, use Integer readFrom:aString" |
608 | 3646 |
|
3647 |
^ Integer readFromString:self |
|
3648 |
||
3649 |
" |
|
3650 |
'12345678901234567890' asInteger |
|
3651 |
'-1234' asInteger |
|
9190 | 3652 |
|
3653 |
The following raises an error: |
|
20874 | 3654 |
'0.123' asInteger <- reader finds more after reading 0 |
9190 | 3655 |
|
3656 |
whereas the less strict readFrom does not: |
|
20874 | 3657 |
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
|
3658 |
|
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
3659 |
'0.123' asInteger |
608 | 3660 |
'0.123' asNumber <- returns what you expect |
8874 | 3661 |
Object errorSignal handle:[:ex | ex return:0] do:['foo' asInteger] |
9190 | 3662 |
|
3663 |
" |
|
9192 | 3664 |
! |
3665 |
||
3666 |
asLowercase |
|
608 | 3667 |
"return a copy of myself in lowercase letters" |
3668 |
||
8003
6e690f4b79f1
asLowercase/asUppercase and friends care for 16-bit chars
Claus Gittinger <cg@exept.de>
parents:
7995
diff
changeset
|
3669 |
|newStr c bitsPerCharacter |
9192 | 3670 |
mySize "{ Class: SmallInteger }" | |
3671 |
||
3672 |
mySize := self size. |
|
3673 |
mySize == 0 ifTrue:[^ self]. |
|
18657 | 3674 |
|
9192 | 3675 |
newStr := self species new:mySize. |
3676 |
bitsPerCharacter := newStr bitsPerCharacter. |
|
3677 |
||
18657 | 3678 |
"/ handle the very seldom case of an uppercase char which needs |
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
3679 |
"/ more bits in its lowercase variant |
18657 | 3680 |
"/ (there are only a few of them) |
3681 |
||
9192 | 3682 |
1 to:mySize do:[:i | |
20705 | 3683 |
c := (self at:i) asLowercase. |
3684 |
(c bitsPerCharacter > bitsPerCharacter |
|
3685 |
and:[c stringSpecies ~= newStr stringSpecies]) ifTrue:[ |
|
3686 |
newStr := c stringSpecies fromString:newStr. |
|
3687 |
bitsPerCharacter := newStr bitsPerCharacter. |
|
3688 |
]. |
|
3689 |
newStr at:i put:c |
|
9192 | 3690 |
]. |
3691 |
^ newStr |
|
3692 |
||
3693 |
" |
|
3694 |
'HelloWorld' asLowercase |
|
18662 | 3695 |
'HelloWorld' asUnicode16String asLowercase |
9192 | 3696 |
'HelloWorld' asLowercaseFirst |
18657 | 3697 |
'HelloWorld' asUppercase |
20705 | 3698 |
'HelloWorldÿ' asUppercase |
3699 |
'HelloWorldŸ' asLowercase - currently returns an U16 string; should this be u8? |
|
9192 | 3700 |
" |
3701 |
! |
|
3702 |
||
3703 |
asLowercaseFirst |
|
17508 | 3704 |
"return a copy of myself where the first character is converted to lowercase. |
3705 |
If the first character is already lowercase, or there is no uppercase for it, return the |
|
3706 |
receiver." |
|
9192 | 3707 |
|
3708 |
|newString firstChar firstCharAsLowercase| |
|
3709 |
||
11525 | 3710 |
self isEmpty ifTrue:[^ self]. |
9192 | 3711 |
firstChar := (self at:1). |
3712 |
firstCharAsLowercase := firstChar asLowercase. |
|
3713 |
firstChar == firstCharAsLowercase ifTrue:[ ^ self]. |
|
3714 |
||
3715 |
firstCharAsLowercase bitsPerCharacter > self bitsPerCharacter ifTrue:[ |
|
20874 | 3716 |
newString := firstCharAsLowercase stringSpecies fromString:self. |
9192 | 3717 |
] ifFalse:[ |
20874 | 3718 |
newString := self stringSpecies fromString:self. |
9192 | 3719 |
]. |
9190 | 3720 |
newString at:1 put:firstCharAsLowercase. |
3721 |
^ newString |
|
608 | 3722 |
|
3723 |
" |
|
8874 | 3724 |
'HelloWorld' asLowercase |
3725 |
'HelloWorld' asLowercaseFirst |
|
608 | 3726 |
" |
3727 |
! |
|
3728 |
||
11828 | 3729 |
asLowercaseLast |
3730 |
"return a copy of myself where the last character is |
|
3731 |
converted to lowercase." |
|
3732 |
||
3733 |
|newString sz| |
|
3734 |
||
3735 |
sz := self size. |
|
3736 |
newString := self copyFrom:1 to:sz. |
|
3737 |
sz > 0 ifTrue:[ |
|
20874 | 3738 |
newString at:sz put:(newString at:sz) asLowercase |
11828 | 3739 |
]. |
3740 |
^ newString |
|
3741 |
||
3742 |
" |
|
12177 | 3743 |
'HelloWorld' asLowercase |
3744 |
'HelloWorlD' asLowercaseLast |
|
11828 | 3745 |
" |
3746 |
! |
|
3747 |
||
17644 | 3748 |
asMutator |
19473 | 3749 |
"return a corresponding setter method's selector. |
3750 |
I.e. #foo asMutator returns #foo:" |
|
17644 | 3751 |
|
20873 | 3752 |
^ (self asSingleByteString,':') asSymbol |
17644 | 3753 |
! |
3754 |
||
9190 | 3755 |
asNumber |
3756 |
"read a number from the receiver. |
|
3757 |
Notice, that (in contrast to ST-80) errors may occur during the read, |
|
3758 |
so you better setup some signal handler when using this method. |
|
3759 |
Also notice, that this is meant to read end-user numbers from a string; |
|
9135 | 3760 |
it does not handle smalltalk numbers (i.e. radix). |
3761 |
To read a smalltalk number, use Number >> readSmalltalkFrom:. |
|
608 | 3762 |
This may change if ANSI specifies it." |
3763 |
||
3764 |
"/ ST-80 behavior: |
|
3765 |
"/ ^ Number readFromString:self onError:0 |
|
3766 |
||
17061 | 3767 |
^ Number fromString:self |
3768 |
||
3769 |
" |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
3770 |
'123' asNumber |
608 | 3771 |
'123.567' asNumber |
3772 |
'(5/6)' asNumber |
|
3773 |
'foo' asNumber |
|
17061 | 3774 |
'123a' asNumber |
8874 | 3775 |
Object errorSignal handle:[:ex | ex returnWith:0] do:['foo' asNumber] |
608 | 3776 |
" |
9192 | 3777 |
! |
3778 |
||
3779 |
asNumberFromFormatString:ignored |
|
608 | 3780 |
"read a number from the receiver, ignoring any nonDigit characters. |
3781 |
This is typically used to convert from strings which include |
|
3782 |
dollar-signs or millenium digits. However, this method also ignores |
|
3783 |
the decimal point (if any) and therefore should be used with care." |
|
3784 |
||
3785 |
|tempString| |
|
3786 |
||
3787 |
tempString := self collect:[:char | char isDigit]. |
|
3788 |
^ Number readFromString:tempString onError:0 |
|
3789 |
||
3790 |
" |
|
3791 |
'USD 123' asNumberFromFormatString:'foo' |
|
3792 |
'DM 123' asNumberFromFormatString:'foo' |
|
3793 |
'123' asNumberFromFormatString:'foo' |
|
3794 |
'123.567' asNumberFromFormatString:'foo' |
|
3795 |
'(5/6)' asNumberFromFormatString:'foo' |
|
3796 |
'foo' asNumberFromFormatString:'foo' |
|
3797 |
" |
|
9192 | 3798 |
! |
3799 |
||
21994 | 3800 |
asPackageId |
3801 |
"given a package-string as receiver, return a packageId object. |
|
3802 |
packageIds hide the details of module/directory handling inside the path. |
|
3803 |
See PackageId for the required format of those strings." |
|
3804 |
||
3805 |
^ PackageId from: self string |
|
3806 |
||
21995 | 3807 |
" |
3808 |
'stx:libbasic' asPackageId |
|
3809 |
" |
|
3810 |
||
21994 | 3811 |
"Created: / 11-07-2017 / 18:24:47 / cg" |
3812 |
! |
|
3813 |
||
7977 | 3814 |
asSingleByteStringIfPossible |
3815 |
"if possible, return the receiver converted to a 'normal' string. |
|
18181 | 3816 |
It is only possible, if there are no characters with codePoints above 255 in the receiver. |
3817 |
If not possible, the (wideString) receiver is returned." |
|
7977 | 3818 |
|
14555 | 3819 |
self isWideString ifFalse:[^ self]. |
18967 | 3820 |
self containsNon8BitElements ifTrue:[^ self]. |
3821 |
^ self asSingleByteString |
|
9192 | 3822 |
|
3823 |
" |
|
3824 |
'hello' asSingleByteStringIfPossible |
|
18967 | 3825 |
'hello' asText asSingleByteStringIfPossible |
3826 |
'hello' asUnicodeString asText asSingleByteStringIfPossible |
|
9192 | 3827 |
'hello' asUnicodeString asSingleByteStringIfPossible |
3828 |
" |
|
3829 |
! |
|
3830 |
||
3831 |
asSingleByteStringReplaceInvalidWith:replacementCharacter |
|
3832 |
"return the receiver converted to a 'normal' string, |
|
3833 |
with invalid characters replaced by replacementCharacter. |
|
3834 |
Can be used to convert from 16-bit strings to 8-bit strings |
|
3835 |
and replace characters above code-255 with some replacement." |
|
3836 |
||
3837 |
|newString| |
|
3838 |
||
3839 |
newString := String new:(self size). |
|
3840 |
1 to:self size do:[:idx | |
|
20874 | 3841 |
|char| |
3842 |
||
3843 |
char := self at:idx. |
|
3844 |
char codePoint <= 16rFF ifTrue:[ |
|
3845 |
newString at:idx put:char |
|
3846 |
] ifFalse:[ |
|
3847 |
newString at:idx put:replacementCharacter |
|
3848 |
]. |
|
9190 | 3849 |
]. |
3850 |
^ newString |
|
3851 |
||
3852 |
"Created: 30.6.1997 / 13:02:14 / cg" |
|
7977 | 3853 |
! |
3854 |
||
9192 | 3855 |
asString |
3856 |
"return myself - I am a string" |
|
3857 |
||
3858 |
^ self |
|
3859 |
! |
|
3860 |
||
3861 |
asStringCollection |
|
3862 |
"return a collection of lines from myself." |
|
3863 |
||
3864 |
^ StringCollection fromString:self "string" |
|
3865 |
||
21639 | 3866 |
" |
3867 |
'hello\world\1\2\3' withCRs asStringCollection first |
|
3868 |
" |
|
3869 |
||
3870 |
"Modified: / 13-05-1996 / 20:36:59 / cg" |
|
3871 |
"Modified (comment): / 13-03-2017 / 14:05:14 / cg" |
|
9192 | 3872 |
! |
1375
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
3873 |
|
23311 | 3874 |
asStringWithoutEmphasis |
3875 |
"return myself as a string without any emphasis" |
|
3876 |
"return myself - I am a string" |
|
3877 |
||
3878 |
^ self |
|
3879 |
||
3880 |
"Created: / 29-08-2018 / 09:21:07 / Claus Gittinger" |
|
3881 |
! |
|
3882 |
||
1434 | 3883 |
asSymbol |
19039 | 3884 |
"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
|
3885 |
The receiver must be a singleByte-String. |
ccd0e8f26439
printing - migrating towards unicodeSupport in ST-code
Claus Gittinger <cg@exept.de>
parents:
8040
diff
changeset
|
3886 |
TwoByte- and FourByteSymbols are (currently ?) not allowed." |
ccd0e8f26439
printing - migrating towards unicodeSupport in ST-code
Claus Gittinger <cg@exept.de>
parents:
8040
diff
changeset
|
3887 |
|
ccd0e8f26439
printing - migrating towards unicodeSupport in ST-code
Claus Gittinger <cg@exept.de>
parents:
8040
diff
changeset
|
3888 |
|str| |
ccd0e8f26439
printing - migrating towards unicodeSupport in ST-code
Claus Gittinger <cg@exept.de>
parents:
8040
diff
changeset
|
3889 |
|
9192 | 3890 |
str := self string. |
3891 |
str ~~ self ifTrue:[ ^ str asSymbol ]. |
|
3892 |
^ self asSingleByteString asSymbol |
|
3893 |
! |
|
3894 |
||
3895 |
asSymbolIfInterned |
|
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
3896 |
"If a symbol with the receiver's characters is already known, return it. |
19692 | 3897 |
Otherwise, return nil. |
9230 | 3898 |
This can be used to query for an existing symbol and is the same as: |
20874 | 3899 |
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
|
3900 |
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
|
3901 |
The receiver must be a singleByte-String. |
ccd0e8f26439
printing - migrating towards unicodeSupport in ST-code
Claus Gittinger <cg@exept.de>
parents:
8040
diff
changeset
|
3902 |
TwoByte- and FourByteSymbols are (currently ?) not allowed." |
1435
3ee1fde2aa58
allow asSymbolIfInterned for Texts
Claus Gittinger <cg@exept.de>
parents:
1434
diff
changeset
|
3903 |
|
18967 | 3904 |
|str| |
3905 |
||
3906 |
str := self string. |
|
3907 |
str == self ifTrue:[ |
|
20874 | 3908 |
"must be some kind of N-ByteString" |
3909 |
str := self asSingleByteStringIfPossible. |
|
3910 |
str == self ifTrue:[ |
|
3911 |
"single byte string conversion is not possible" |
|
3912 |
^ nil. |
|
3913 |
]. |
|
6642 | 3914 |
]. |
18967 | 3915 |
^ str asSymbolIfInterned |
3916 |
||
3917 |
" |
|
3918 |
(Unicode16String with:(Character value:16rFFFF)) asSymbolIfInterned |
|
3919 |
'new' asUnicodeString asSymbolIfInterned |
|
3920 |
'new' asText asSymbolIfInterned |
|
3921 |
'new' asUnicodeString asText asSymbolIfInterned |
|
3922 |
" |
|
1435
3ee1fde2aa58
allow asSymbolIfInterned for Texts
Claus Gittinger <cg@exept.de>
parents:
1434
diff
changeset
|
3923 |
|
3ee1fde2aa58
allow asSymbolIfInterned for Texts
Claus Gittinger <cg@exept.de>
parents:
1434
diff
changeset
|
3924 |
"Created: 22.5.1996 / 16:37:04 / cg" |
9192 | 3925 |
! |
3926 |
||
20160 | 3927 |
asSymbolIfInternedOrSelf |
3928 |
"If a symbol with the receiver's characters is already known, return it. |
|
3929 |
Otherwise, return self. |
|
3930 |
This can be used to query for an existing symbol and is the same as: |
|
3931 |
self knownAsSymbol ifTrue:[self asSymbol] ifFalse:[self] |
|
3932 |
but slightly faster, since the symbol lookup operation is only performed once. |
|
3933 |
The receiver must be a singleByte-String. |
|
3934 |
TwoByte- and FourByteSymbols are (currently ?) not allowed." |
|
3935 |
||
3936 |
|sym| |
|
3937 |
||
3938 |
sym := self asSymbolIfInterned. |
|
3939 |
^ sym isNil ifTrue:[self] ifFalse:[sym]. |
|
3940 |
||
3941 |
||
3942 |
" |
|
3943 |
(Unicode16String with:(Character value:16rFFFF)) asSymbolIfInternedOrSelf |
|
3944 |
'new' asUnicodeString asSymbolIfInternedOrSelf |
|
3945 |
'new' asText asSymbolIfInternedOrSelf |
|
3946 |
'new' asUnicodeString asText asSymbolIfInternedOrSelf |
|
3947 |
" |
|
3948 |
||
3949 |
"Created: 22.5.1996 / 16:37:04 / cg" |
|
3950 |
! |
|
3951 |
||
9192 | 3952 |
asText |
9403 | 3953 |
"return a Text-object (string with emphasis) from myself." |
9192 | 3954 |
|
3955 |
"this test allows for small non-gui apps to be built without libbasic2 (where Text is)" |
|
3956 |
Text isNil ifTrue:[^ self]. |
|
17065 | 3957 |
^ Text string:self |
9192 | 3958 |
|
3959 |
"Created: 12.5.1996 / 10:41:14 / cg" |
|
3960 |
! |
|
3961 |
||
23617 | 3962 |
asTimeDuration |
3963 |
"return an TimeDuration object from the parsing the receiver string" |
|
3964 |
||
3965 |
^ TimeDuration readFromString:self |
|
3966 |
||
3967 |
" |
|
3968 |
Time now asTimeDuration |
|
3969 |
10 asTimeDuration |
|
3970 |
10 seconds asTimeDuration |
|
3971 |
'10m 20s' asTimeDuration |
|
3972 |
" |
|
3973 |
||
3974 |
"Created: / 21-01-2019 / 10:29:51 / Claus Gittinger" |
|
3975 |
! |
|
3976 |
||
17021 | 3977 |
asTimestamp |
3978 |
"convert the receiver into an Timestamp. |
|
3979 |
Notice, that errors may occur during the read, |
|
3980 |
so you better setup some exception handler when using this method." |
|
3981 |
||
21030 | 3982 |
^ Timestamp readFrom:self |
17021 | 3983 |
|
3984 |
" |
|
3985 |
'2014-11-10 21:30:22.444' asTimestamp |
|
3986 |
'2014-11-10 21:30:22.444Z' asTimestamp |
|
3987 |
'2014-11-10 21:30:22.444+0200' asTimestamp |
|
3988 |
" |
|
3989 |
! |
|
3990 |
||
9192 | 3991 |
asTitlecase |
8027 | 3992 |
"return a version of the receiver, where the first character is converted to titlecase, |
3993 |
and everything else to lowercase. |
|
3994 |
See the comment in Character on what titlecase is." |
|
3995 |
||
3996 |
|newStr c bitsPerCharacter |
|
3997 |
mySize "{ Class: SmallInteger }" | |
|
3998 |
||
3999 |
mySize := self size. |
|
4000 |
newStr := self species new:mySize. |
|
4001 |
bitsPerCharacter := newStr bitsPerCharacter. |
|
4002 |
||
4003 |
1 to:mySize do:[:i | |
|
20874 | 4004 |
i == 1 ifTrue:[ |
4005 |
c := (self at:i) asTitlecase. |
|
4006 |
] ifFalse:[ |
|
4007 |
c := (self at:i) asLowercase. |
|
4008 |
]. |
|
4009 |
c bitsPerCharacter > bitsPerCharacter ifTrue:[ |
|
4010 |
newStr := c stringSpecies fromString:newStr. |
|
4011 |
bitsPerCharacter := newStr bitsPerCharacter. |
|
4012 |
]. |
|
4013 |
newStr at:i put:c |
|
8027 | 4014 |
]. |
4015 |
^ newStr |
|
4016 |
||
4017 |
" |
|
8874 | 4018 |
'helloWorld' asTitlecase |
4019 |
'HelloWorld' asTitlecase |
|
4020 |
'HELLOWORLD' asTitlecase |
|
4021 |
'helloworld' asTitlecase |
|
8027 | 4022 |
" |
9192 | 4023 |
! |
4024 |
||
4025 |
asTitlecaseFirst |
|
8023 | 4026 |
"return a version of the receiver, where the first character is converted to titlecase. |
8024 | 4027 |
Titlecase is much like uppercase for most characters, with the exception of some combined |
4028 |
(2-character glyphs), which consist of an upper- and lower-case characters. |
|
4029 |
If the first character is already titlecase, or there is no titlecasepercase for it, return the |
|
8023 | 4030 |
receiver." |
4031 |
||
8026 | 4032 |
" |
8874 | 4033 |
For example, in Unicode, character U+01F3 is LATIN SMALL LETTER DZ. |
4034 |
(Let us write this compound character using ASCII as 'dz'.) |
|
4035 |
This character uppercases to character U+01F1, LATIN CAPITAL LETTER DZ. |
|
4036 |
(Which is basically 'DZ'.) |
|
4037 |
But it titlecases to to character U+01F2, LATIN CAPITAL LETTER D WITH SMALL LETTER Z. |
|
8026 | 4038 |
(Which we can write 'Dz'.) |
4039 |
||
4040 |
character uppercase titlecase |
|
4041 |
--------- --------- --------- |
|
4042 |
dz DZ Dz |
|
4043 |
" |
|
4044 |
||
8023 | 4045 |
|newString firstChar firstCharAsTitlecase| |
4046 |
||
4047 |
firstChar := (self at:1). |
|
8025 | 4048 |
firstCharAsTitlecase := firstChar asTitlecase. |
8023 | 4049 |
firstChar == firstCharAsTitlecase ifTrue:[ ^ self]. |
8037
8cce25b1059c
care for wide chars in asXXXFirst
Claus Gittinger <cg@exept.de>
parents:
8032
diff
changeset
|
4050 |
|
8cce25b1059c
care for wide chars in asXXXFirst
Claus Gittinger <cg@exept.de>
parents:
8032
diff
changeset
|
4051 |
firstCharAsTitlecase bitsPerCharacter > self bitsPerCharacter ifTrue:[ |
20874 | 4052 |
newString := firstCharAsTitlecase stringSpecies fromString:self. |
8037
8cce25b1059c
care for wide chars in asXXXFirst
Claus Gittinger <cg@exept.de>
parents:
8032
diff
changeset
|
4053 |
] ifFalse:[ |
20874 | 4054 |
newString := self stringSpecies fromString:self. |
8037
8cce25b1059c
care for wide chars in asXXXFirst
Claus Gittinger <cg@exept.de>
parents:
8032
diff
changeset
|
4055 |
]. |
8023 | 4056 |
newString at:1 put:firstCharAsTitlecase. |
4057 |
^ newString |
|
4058 |
||
4059 |
" |
|
8874 | 4060 |
'helloWorld' asTitlecaseFirst |
4061 |
'HelloWorld' asTitlecaseFirst |
|
8023 | 4062 |
" |
9192 | 4063 |
! |
4064 |
||
4065 |
asTwoByteString |
|
4066 |
"return the receiver converted to a two-byte string. |
|
16051 | 4067 |
Will be obsolete soon - use asUnicode16String." |
9192 | 4068 |
|
4069 |
^ TwoByteString fromString:self |
|
4070 |
! |
|
4071 |
||
4072 |
asURI |
|
4073 |
"return an URI with string taken from the receiver" |
|
4074 |
||
4075 |
^ URI fromString:self |
|
4076 |
! |
|
8710 | 4077 |
|
5776 | 4078 |
asURL |
4079 |
"return an URL-object from myself." |
|
4080 |
||
4081 |
^ URL fromString:self |
|
4082 |
||
4083 |
" |
|
8874 | 4084 |
'http://www.exept.de:80/index.html' asURL host |
4085 |
'http://www.exept.de:80/index.html' asURL port |
|
9192 | 4086 |
'http://www.exept.de:80/index.html' asURL method |
4087 |
'http://www.exept.de:80/index.html' asURL path |
|
4088 |
" |
|
4089 |
! |
|
4090 |
||
18901 | 4091 |
asUUID |
4092 |
"return self as a UUID" |
|
4093 |
||
4094 |
^ UUID fromString:self |
|
4095 |
||
4096 |
" |
|
4097 |
UUID genUUID asString asUUID |
|
4098 |
'{EAB22AC0-30C1-11CF-A7EB-0000C05BAE0B}' asUUID |
|
4099 |
'{EAB22AC0-30C1-11CF-A7EB-0000C05BAE0B}' asUnicodeString asUUID |
|
4100 |
'EAB22AC0-30C1-11CF-A7EB-0000C05BAE0B' asUUID |
|
4101 |
'EAB22AC0-30C1-11CF-A7EB-0000C05BAE0B quatsch' asUUID |
|
4102 |
'quark EAB22AC0-30C1-11CF-A7EB-0000C05BAE0B' asUUID |
|
4103 |
" |
|
4104 |
||
4105 |
"Modified: / 02-08-2007 / 16:43:29 / cg" |
|
4106 |
! |
|
4107 |
||
9192 | 4108 |
asUnicode16String |
15035 | 4109 |
"return the receiver in a two-byte per character representation. |
4110 |
Normally, the internal ST/X representation should be transparent and not |
|
4111 |
of the programmer's concern; except when strings are passed to the outside world, |
|
4112 |
such as wide-string ffi calls or file contents." |
|
4113 |
||
17600 | 4114 |
|sz| |
4115 |
||
4116 |
sz := self size. |
|
4117 |
||
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
4118 |
^ (Unicode16String new:sz) |
20874 | 4119 |
replaceFrom:1 to:sz with:self startingAt:1; |
4120 |
yourself. |
|
4121 |
||
4122 |
" |
|
4123 |
'abc' asUnicode16String |
|
17600 | 4124 |
" |
4125 |
! |
|
4126 |
||
4127 |
asUnicode16StringZ |
|
4128 |
"return the receiver in a two-byte per character representation. |
|
17625 | 4129 |
Make sure that is zero terminated (last char is 16r0000). |
17600 | 4130 |
Normally, the internal ST/X representation should be transparent and not |
4131 |
of the programmer's concern; except when strings are passed to the outside world, |
|
4132 |
such as wide-string ffi calls or file contents." |
|
4133 |
||
4134 |
|sz| |
|
4135 |
||
4136 |
sz := self size. |
|
22249 | 4137 |
sz > 0 ifTrue:[ |
4138 |
(self at:sz) == (Character codePoint:0) ifTrue:[ |
|
4139 |
^ self asUnicode16String. |
|
4140 |
]. |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
4141 |
]. |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
4142 |
|
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
4143 |
^ (Unicode16String new:sz+1) |
20874 | 4144 |
replaceFrom:1 to:sz with:self startingAt:1; |
4145 |
at:sz+1 put:(Character codePoint:0); |
|
4146 |
yourself. |
|
17600 | 4147 |
|
4148 |
" |
|
22249 | 4149 |
'' asUnicode16StringZ |
20874 | 4150 |
'abc' asUnicode16StringZ |
4151 |
'abc' asUnicode16String asUnicode16StringZ |
|
17600 | 4152 |
" |
9192 | 4153 |
! |
8295
8433d819226b
Define #asUnicode16String and #asUnicode32String in CharacterArray
Stefan Vogel <sv@exept.de>
parents:
8222
diff
changeset
|
4154 |
|
8433d819226b
Define #asUnicode16String and #asUnicode32String in CharacterArray
Stefan Vogel <sv@exept.de>
parents:
8222
diff
changeset
|
4155 |
asUnicode32String |
16051 | 4156 |
"return the receiver in a four-byte per character representation. |
4157 |
Normally, the internal ST/X representation should be transparent and not |
|
4158 |
of the programmer's concern; except when strings are passed to the outside world, |
|
4159 |
such as wide-string ffi calls or file contents." |
|
4160 |
||
4161 |
^ self as:Unicode32String |
|
8295
8433d819226b
Define #asUnicode16String and #asUnicode32String in CharacterArray
Stefan Vogel <sv@exept.de>
parents:
8222
diff
changeset
|
4162 |
! |
8433d819226b
Define #asUnicode16String and #asUnicode32String in CharacterArray
Stefan Vogel <sv@exept.de>
parents:
8222
diff
changeset
|
4163 |
|
8433d819226b
Define #asUnicode16String and #asUnicode32String in CharacterArray
Stefan Vogel <sv@exept.de>
parents:
8222
diff
changeset
|
4164 |
asUnicodeString |
16051 | 4165 |
"return the receiver in a two-byte per character representation. |
4166 |
Normally, the internal ST/X representation should be transparent and not |
|
4167 |
of the programmer's concern; except when strings are passed to the outside world, |
|
4168 |
such as wide-string ffi calls or file contents." |
|
4169 |
||
4170 |
^ self asUnicode16String |
|
4171 |
" |
|
4172 |
'Hello World' asUnicodeString |
|
14141
e86d245213b0
comment/format in: #asUnicodeString
Stefan Vogel <sv@exept.de>
parents:
14137
diff
changeset
|
4173 |
" |
8295
8433d819226b
Define #asUnicode16String and #asUnicode32String in CharacterArray
Stefan Vogel <sv@exept.de>
parents:
8222
diff
changeset
|
4174 |
! |
8433d819226b
Define #asUnicode16String and #asUnicode32String in CharacterArray
Stefan Vogel <sv@exept.de>
parents:
8222
diff
changeset
|
4175 |
|
13073 | 4176 |
asUnixFilename |
4177 |
"return a UnixFilename with pathname taken from the receiver" |
|
4178 |
||
4179 |
^ UnixFilename named:self |
|
4180 |
||
4181 |
"Created: / 20-09-2010 / 21:42:55 / cg" |
|
4182 |
! |
|
4183 |
||
608 | 4184 |
asUppercase |
4185 |
"return a copy of myself in uppercase letters" |
|
4186 |
||
8003
6e690f4b79f1
asLowercase/asUppercase and friends care for 16-bit chars
Claus Gittinger <cg@exept.de>
parents:
7995
diff
changeset
|
4187 |
|newStr c bitsPerCharacter |
608 | 4188 |
mySize "{ Class: SmallInteger }" | |
4189 |
||
4190 |
mySize := self size. |
|
8954
12e701d7b5ac
allow for receiver to be empty in asLowercase and asUppercase.
Claus Gittinger <cg@exept.de>
parents:
8938
diff
changeset
|
4191 |
mySize == 0 ifTrue:[^ self]. |
608 | 4192 |
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
|
4193 |
bitsPerCharacter := newStr bitsPerCharacter. |
6e690f4b79f1
asLowercase/asUppercase and friends care for 16-bit chars
Claus Gittinger <cg@exept.de>
parents:
7995
diff
changeset
|
4194 |
|
18657 | 4195 |
"/ handle the very seldom case of a lowercase char which needs |
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
4196 |
"/ more bits in its uppercase variant |
18657 | 4197 |
"/ (there are only a few of them) |
4198 |
||
608 | 4199 |
1 to:mySize do:[:i | |
20874 | 4200 |
c := (self at:i) asUppercase. |
4201 |
c bitsPerCharacter > bitsPerCharacter ifTrue:[ |
|
4202 |
newStr := c stringSpecies fromString:newStr. |
|
4203 |
bitsPerCharacter := newStr bitsPerCharacter. |
|
4204 |
]. |
|
4205 |
newStr at:i put:c |
|
608 | 4206 |
]. |
4207 |
^ newStr |
|
4208 |
||
4209 |
" |
|
8874 | 4210 |
'helloWorld' asUppercase |
4211 |
'helloWorld' asUppercaseFirst |
|
9192 | 4212 |
(Character value:16rB5) asString asUppercase -- needs 16 bits !! |
18657 | 4213 |
(Character value:16rFF) asString asUppercase -- needs 16 bits !! |
9192 | 4214 |
" |
4215 |
! |
|
4216 |
||
4217 |
asUppercaseFirst |
|
8003
6e690f4b79f1
asLowercase/asUppercase and friends care for 16-bit chars
Claus Gittinger <cg@exept.de>
parents:
7995
diff
changeset
|
4218 |
"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
|
4219 |
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
|
4220 |
receiver." |
6e690f4b79f1
asLowercase/asUppercase and friends care for 16-bit chars
Claus Gittinger <cg@exept.de>
parents:
7995
diff
changeset
|
4221 |
|
6e690f4b79f1
asLowercase/asUppercase and friends care for 16-bit chars
Claus Gittinger <cg@exept.de>
parents:
7995
diff
changeset
|
4222 |
|newString firstChar firstCharAsUppercase| |
6e690f4b79f1
asLowercase/asUppercase and friends care for 16-bit chars
Claus Gittinger <cg@exept.de>
parents:
7995
diff
changeset
|
4223 |
|
12679 | 4224 |
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
|
4225 |
firstChar := self at:1. |
8003
6e690f4b79f1
asLowercase/asUppercase and friends care for 16-bit chars
Claus Gittinger <cg@exept.de>
parents:
7995
diff
changeset
|
4226 |
firstCharAsUppercase := firstChar asUppercase. |
6e690f4b79f1
asLowercase/asUppercase and friends care for 16-bit chars
Claus Gittinger <cg@exept.de>
parents:
7995
diff
changeset
|
4227 |
firstChar == firstCharAsUppercase ifTrue:[ ^ self]. |
6e690f4b79f1
asLowercase/asUppercase and friends care for 16-bit chars
Claus Gittinger <cg@exept.de>
parents:
7995
diff
changeset
|
4228 |
|
12679 | 4229 |
"/ 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
|
4230 |
firstCharAsUppercase bitsPerCharacter > self bitsPerCharacter ifTrue:[ |
20874 | 4231 |
newString := firstCharAsUppercase stringSpecies fromString:self. |
8037
8cce25b1059c
care for wide chars in asXXXFirst
Claus Gittinger <cg@exept.de>
parents:
8032
diff
changeset
|
4232 |
] ifFalse:[ |
20874 | 4233 |
newString := self stringSpecies fromString:self. |
8037
8cce25b1059c
care for wide chars in asXXXFirst
Claus Gittinger <cg@exept.de>
parents:
8032
diff
changeset
|
4234 |
]. |
8003
6e690f4b79f1
asLowercase/asUppercase and friends care for 16-bit chars
Claus Gittinger <cg@exept.de>
parents:
7995
diff
changeset
|
4235 |
newString at:1 put:firstCharAsUppercase. |
608 | 4236 |
^ newString |
4237 |
||
4238 |
" |
|
8874 | 4239 |
'helloWorld' asUppercase |
4240 |
'helloWorld' asUppercaseFirst |
|
4241 |
'HelloWorld' asUppercaseFirst |
|
608 | 4242 |
" |
9192 | 4243 |
! |
4244 |
||
11828 | 4245 |
asUppercaseLast |
4246 |
"return a copy of myself where the last character is |
|
4247 |
converted to uppercase." |
|
4248 |
||
4249 |
|newString sz| |
|
4250 |
||
4251 |
sz := self size. |
|
4252 |
newString := self copyFrom:1 to:sz. |
|
4253 |
sz > 0 ifTrue:[ |
|
20874 | 4254 |
newString at:sz put:(newString at:sz) asUppercase |
11828 | 4255 |
]. |
4256 |
^ newString |
|
4257 |
||
4258 |
" |
|
12177 | 4259 |
'HelloWorld' asUppercase |
4260 |
'HelloWorld' asUppercaseLast |
|
11828 | 4261 |
" |
4262 |
! |
|
4263 |
||
17021 | 4264 |
asUtcTimestamp |
4265 |
"convert the receiver into an UtcTimestamp (Time is interpreted in the UTC timezone). |
|
4266 |
Notice, that errors may occur during the read, |
|
4267 |
so you better setup some exception handler when using this method." |
|
4268 |
||
21030 | 4269 |
^ UtcTimestamp readFrom:self |
17021 | 4270 |
|
4271 |
" |
|
4272 |
'2014-11-10 21:30:22.444' asUtcTimestamp |
|
4273 |
" |
|
4274 |
! |
|
4275 |
||
23121 | 4276 |
asVersionNumberCollection |
4277 |
"Convert a string like 1.2.3a to an Array of numbers (or string, if not a number). |
|
4278 |
Remove zeroes from the end." |
|
4279 |
||
4280 |
^ (self asCollectionOfSubstringsSeparatedByAny:'.-_') asVersionNumberCollection. |
|
4281 |
||
4282 |
" |
|
4283 |
'1' asVersionNumberCollection. |
|
4284 |
'1.1' asVersionNumberCollection. |
|
4285 |
'1.1a' asVersionNumberCollection. |
|
4286 |
'1.1.0' asVersionNumberCollection. |
|
4287 |
'01.01.0' asVersionNumberCollection. |
|
4288 |
'expecco_18_1_0' asVersionNumberCollection. |
|
4289 |
" |
|
4290 |
||
4291 |
"Created: / 20-06-2018 / 11:23:53 / Stefan Vogel" |
|
4292 |
"Modified: / 20-06-2018 / 19:24:36 / Stefan Vogel" |
|
4293 |
! |
|
4294 |
||
11062 | 4295 |
literalArrayEncoding |
4296 |
"encode myself as an array literal, from which a copy of the receiver |
|
4297 |
can be reconstructed with #decodeAsLiteralArray." |
|
4298 |
||
4299 |
^ self |
|
4300 |
||
4301 |
"Modified: 1.9.1995 / 02:25:45 / claus" |
|
4302 |
"Modified: 22.4.1996 / 13:00:50 / cg" |
|
4303 |
! |
|
4304 |
||
9192 | 4305 |
string |
4306 |
"return the receiver - for ST-80 compatibility" |
|
4307 |
||
4308 |
^ self |
|
4309 |
||
4310 |
"Modified: 27.4.1996 / 13:29:30 / cg" |
|
4311 |
! ! |
|
4312 |
||
4313 |
!CharacterArray methodsFor:'copying'! |
|
4314 |
||
22528 | 4315 |
, aStringOrCharacterOrAnyOther |
2720
98c21d9a07c0
allow mixed strings to be concatenated using comma
Claus Gittinger <cg@exept.de>
parents:
2692
diff
changeset
|
4316 |
"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
|
4317 |
This is nonStandard, but convenient" |
2f14bbd792a9
, now accepts a character-argument
Claus Gittinger <cg@exept.de>
parents:
1028
diff
changeset
|
4318 |
|
23998 | 4319 |
|stringifiedArg |
4320 |
newSpecies newString l1 l2 combinedLen| |
|
22528 | 4321 |
|
4322 |
aStringOrCharacterOrAnyOther isCharacter ifTrue:[ |
|
4323 |
^ self copyWith:aStringOrCharacterOrAnyOther |
|
4324 |
]. |
|
4325 |
aStringOrCharacterOrAnyOther isText ifTrue:[ |
|
4326 |
^ aStringOrCharacterOrAnyOther concatenateFromString:self |
|
4327 |
]. |
|
4328 |
stringifiedArg := aStringOrCharacterOrAnyOther asString. |
|
4329 |
stringifiedArg isString ifTrue:[ |
|
24037 | 4330 |
(stringifiedArg bitsPerCharacter) > (self bitsPerCharacter) ifTrue:[ |
4331 |
newSpecies := stringifiedArg species. |
|
4332 |
] ifFalse:[ |
|
4333 |
newSpecies := self species. |
|
4334 |
]. |
|
23998 | 4335 |
l1 := self size. |
4336 |
l2 := stringifiedArg size. |
|
4337 |
combinedLen := l1 + l2. |
|
4338 |
newString := newSpecies uninitializedNew:combinedLen. |
|
24037 | 4339 |
newString |
4340 |
replaceFrom:1 to:l1 with:self startingAt:1; |
|
4341 |
replaceFrom:l1+1 to:combinedLen with:stringifiedArg startingAt:1. |
|
23998 | 4342 |
^ newString |
2720
98c21d9a07c0
allow mixed strings to be concatenated using comma
Claus Gittinger <cg@exept.de>
parents:
2692
diff
changeset
|
4343 |
]. |
22528 | 4344 |
^ super , stringifiedArg |
1050
2f14bbd792a9
, now accepts a character-argument
Claus Gittinger <cg@exept.de>
parents:
1028
diff
changeset
|
4345 |
|
2f14bbd792a9
, now accepts a character-argument
Claus Gittinger <cg@exept.de>
parents:
1028
diff
changeset
|
4346 |
" |
8874 | 4347 |
'hello' , $1 |
24037 | 4348 |
'hello' , (Character codePoint:1046) |
8874 | 4349 |
'hello' , '1' |
4350 |
'hello' , (' world' asText allBold) |
|
4351 |
'hello' , (JISEncodedString fromString:' world') |
|
2720
98c21d9a07c0
allow mixed strings to be concatenated using comma
Claus Gittinger <cg@exept.de>
parents:
2692
diff
changeset
|
4352 |
(JISEncodedString fromString:'hello') , ' world' |
1411 | 4353 |
|
1422 | 4354 |
Transcript showCR: |
20874 | 4355 |
(Text string:'hello' emphasis:#italic) , (Text string:' world' emphasis:#bold) |
22528 | 4356 |
|
4357 |
'hello',123,'world' |
|
4358 |
'hello' allBold,123,'world' |
|
4359 |
'hello',123,'world' allBold |
|
1050
2f14bbd792a9
, now accepts a character-argument
Claus Gittinger <cg@exept.de>
parents:
1028
diff
changeset
|
4360 |
" |
2f14bbd792a9
, now accepts a character-argument
Claus Gittinger <cg@exept.de>
parents:
1028
diff
changeset
|
4361 |
|
23997 | 4362 |
"Modified: / 28-06-1997 / 00:13:17 / cg" |
23998 | 4363 |
"Modified: / 27-03-2019 / 19:37:34 / Claus Gittinger" |
24033 | 4364 |
"Modified: / 02-04-2019 / 10:39:28 / Maren" |
24037 | 4365 |
"Modified (comment): / 02-04-2019 / 11:36:00 / Stefan Vogel" |
9192 | 4366 |
! |
4367 |
||
4368 |
,, aString |
|
7020 | 4369 |
"concatenate with a newLine in between" |
6965 | 4370 |
|
4371 |
^ (self copyWith:Character cr) , aString |
|
4372 |
||
4373 |
" |
|
7020 | 4374 |
'hello' ,, 'world' |
6965 | 4375 |
" |
23388 | 4376 |
|
4377 |
"Modified (comment): / 25-09-2018 / 10:21:45 / Claus Gittinger" |
|
6965 | 4378 |
! |
4379 |
||
11329 | 4380 |
chopTo:maxLen |
19464 | 4381 |
"if the receiver's size is less or equal to maxLen, return it. |
11329 | 4382 |
Otherwise, return a copy of the receiver, where some characters |
4383 |
in the middle have been removed for a total string length |
|
4384 |
of maxLen." |
|
4385 |
||
4386 |
|sz n1 n2| |
|
4387 |
||
4388 |
(sz := self size) <= maxLen ifTrue:[ ^ self ]. |
|
4389 |
||
4390 |
n1 := n2 := maxLen // 2. |
|
4391 |
maxLen odd ifTrue:[ |
|
20874 | 4392 |
n2 := n1 + 1 |
11329 | 4393 |
]. |
4394 |
^ (self copyFrom:1 to:n1) , (self copyFrom:sz - n2 + 1) |
|
4395 |
||
4396 |
" |
|
4397 |
'12345678901234' chopTo:15 |
|
4398 |
'123456789012345' chopTo:15 |
|
4399 |
'1234567890123456' chopTo:15 |
|
4400 |
'aShortString' chopTo:15 |
|
4401 |
'aVeryLongNameForAStringThatShouldBeShortened' chopTo:15 |
|
4402 |
" |
|
4403 |
||
4404 |
"Modified: / 24-10-2006 / 12:32:01 / cg" |
|
4405 |
! |
|
4406 |
||
4407 |
contractAtBeginningTo:maxLen |
|
19464 | 4408 |
"if the receiver's size is less or equal to maxLen, return it. |
11329 | 4409 |
Otherwise, return a copy of the receiver, where some characters |
4410 |
at the beginning have been replaced by '...' for a total string length |
|
4411 |
of maxLen. Can be used to abbreviate long entries in tables." |
|
4412 |
||
4413 |
|sz| |
|
4414 |
||
4415 |
(sz := self size) <= maxLen ifTrue:[ ^ self ]. |
|
4416 |
||
4417 |
^ '...' , (self copyFrom:(sz - (maxLen - 4))) |
|
4418 |
||
4419 |
" |
|
4420 |
'12345678901234' contractAtBeginningTo:15 |
|
4421 |
'123456789012345' contractAtBeginningTo:15 |
|
4422 |
'1234567890123456' contractAtBeginningTo:15 |
|
4423 |
'aShortString' contractAtBeginningTo:15 |
|
4424 |
'aVeryLongNameForAStringThatShouldBeShortened' contractAtBeginningTo:15 |
|
4425 |
" |
|
4426 |
||
4427 |
"Modified: / 24-10-2006 / 12:32:13 / cg" |
|
4428 |
! |
|
4429 |
||
4430 |
contractAtEndTo:maxLen |
|
19464 | 4431 |
"if the receiver's size is less or equal to maxLen, return it. |
11329 | 4432 |
Otherwise, return a copy of the receiver, where some characters |
4433 |
at the end have been replaced by '...' for a total string length |
|
4434 |
of maxLen. Can be used to abbreviate long entries in tables." |
|
4435 |
||
21559 | 4436 |
(self size <= maxLen) ifTrue:[ ^ self ]. |
11329 | 4437 |
|
4438 |
^ (self copyTo:maxLen-3),'...' |
|
4439 |
||
4440 |
" |
|
4441 |
'12345678901234' contractAtEndTo:15 |
|
4442 |
'123456789012345' contractAtEndTo:15 |
|
4443 |
'1234567890123456' contractAtEndTo:15 |
|
4444 |
'aShortString' contractAtEndTo:15 |
|
4445 |
'aVeryLongNameForAStringThatShouldBeShortened' contractAtEndTo:15 |
|
4446 |
" |
|
4447 |
||
4448 |
"Modified: / 24-10-2006 / 12:32:26 / cg" |
|
21559 | 4449 |
"Modified: / 23-02-2017 / 21:32:21 / mawalch" |
11329 | 4450 |
! |
4451 |
||
4452 |
contractLeftTo:maxLen |
|
19464 | 4453 |
"if the receiver's size is less or equal to maxLen, return it. |
11329 | 4454 |
Otherwise, return a copy of the receiver, where some characters |
4455 |
near the first quarter have been replaced by '...' for a total string length |
|
12177 | 4456 |
of maxLen. |
4457 |
Very similar to contractTo:, but better to abbreviate long filename entries, |
|
11329 | 4458 |
where the right part is of more use than the left." |
4459 |
||
4460 |
|sz "{ SmallInteger }" |
|
4461 |
halfSize quarterSize "{ SmallInteger }" |
|
4462 |
leftEnd rightEnd rightStart| |
|
4463 |
||
4464 |
(sz := self size) <= maxLen ifTrue:[ ^ self ]. |
|
4465 |
||
4466 |
halfSize := maxLen // 2. |
|
4467 |
quarterSize := maxLen // 4. |
|
4468 |
leftEnd := quarterSize-1. |
|
4469 |
rightEnd := maxLen - leftEnd - 3. |
|
4470 |
rightStart := sz - rightEnd + 1. |
|
4471 |
^ (self copyTo:leftEnd),'...',(self copyFrom:rightStart) |
|
4472 |
||
4473 |
" |
|
12177 | 4474 |
'12345678901234' contractLeftTo:15 |
4475 |
'123456789012345' contractLeftTo:15 |
|
4476 |
'1234567890123456' contractLeftTo:15 |
|
4477 |
'aShortString' contractLeftTo:15 |
|
4478 |
'aVeryLongNameForAStringThatShouldBeShortened' contractLeftTo:15 |
|
11329 | 4479 |
'C:\Dokumente und Einstellungen\cg\work\bosch\dapas\hw_schnittstellen\DAPAS__HpibDLL.st' contractLeftTo:40 |
4480 |
" |
|
4481 |
! |
|
4482 |
||
4483 |
contractTo:maxLen |
|
19464 | 4484 |
"if the receiver's size is less or equal to maxLen, return it. |
11329 | 4485 |
Otherwise, return a copy of the receiver, where some characters |
4486 |
in the middle have been replaced by '...' for a total string length |
|
4487 |
of maxLen. Can be used to abbreviate long entries in tables." |
|
4488 |
||
13362 | 4489 |
|sz "{ SmallInteger }" leftSize rightSize| |
11329 | 4490 |
|
4491 |
(sz := self size) <= maxLen ifTrue:[ ^ self ]. |
|
4492 |
||
13362 | 4493 |
rightSize := maxLen // 2. |
4494 |
leftSize := maxLen - rightSize. |
|
13822 | 4495 |
leftSize := leftSize - 2. |
4496 |
rightSize := rightSize - 1. |
|
13362 | 4497 |
^ (self copyTo:leftSize),'...',(self copyFrom:(sz+1-rightSize)) |
4498 |
||
4499 |
" |
|
13882
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
4500 |
'12345678901234' contractTo:15 |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
4501 |
'123456789012345' contractTo:15 |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
4502 |
'1234567890123456' contractTo:15 |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
4503 |
'12345678901234567' contractTo:15 |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
4504 |
'123456789012345678' contractTo:15 |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
4505 |
'aShortString' contractTo:15 |
11329 | 4506 |
'aVeryLongNameForAStringThatShouldBeShortened' contractTo:15 |
4507 |
'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
|
4508 |
('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
|
4509 |
('1234567890123456789012345678901234567' contractTo:29) size |
13362 | 4510 |
" |
4511 |
||
13822 | 4512 |
"Modified (comment): / 24-11-2011 / 19:17:46 / cg" |
11329 | 4513 |
! |
4514 |
||
23061 | 4515 |
copyBetween:string1 and:string2 caseSensitive:caseSensitive |
4516 |
"return the substring between two matching sub-strings. |
|
4517 |
Returns nil, if not both subStrings are present in the receiver." |
|
4518 |
||
4519 |
|idx1 leftIndex rightIndex| |
|
4520 |
||
4521 |
idx1 := self indexOfString:string1 caseSensitive:caseSensitive. |
|
4522 |
idx1 ~~ 0 ifTrue:[ |
|
4523 |
leftIndex := (idx1 + string1 size). |
|
4524 |
rightIndex := self indexOfString:string2 caseSensitive:caseSensitive startingAt:leftIndex. |
|
4525 |
rightIndex ~~ 0 ifTrue:[ |
|
4526 |
^ self copyFrom:leftIndex to:rightIndex-1 |
|
4527 |
] |
|
4528 |
]. |
|
4529 |
^ nil |
|
4530 |
||
4531 |
" |
|
4532 |
'hello funny world' copyBetween:'hello' and:'world' caseSensitive:true -> ' funny ' |
|
4533 |
'helloworld' copyBetween:'hello' and:'world' caseSensitive:true -> '' |
|
4534 |
'helloorld' copyBetween:'hello' and:'world' caseSensitive:true -> nil |
|
4535 |
||
4536 |
'hello funny World' copyBetween:'hello' and:'world' caseSensitive:true -> nil |
|
4537 |
'helloWorld' copyBetween:'hello' and:'world' caseSensitive:true -> nil |
|
4538 |
||
4539 |
'hello funny World' copyBetween:'hello' and:'world' caseSensitive:false -> ' funny ' |
|
4540 |
'helloWorld' copyBetween:'hello' and:'world' caseSensitive:false -> '' |
|
4541 |
" |
|
4542 |
||
4543 |
"Created: / 08-06-2018 / 14:50:17 / Claus Gittinger" |
|
4544 |
! |
|
4545 |
||
16202 | 4546 |
copyReplaceAll:oldElement with:newElement |
4547 |
"return a copy of the receiver as a string, where all elements equal to oldElement |
|
4548 |
have been replaced by newElement." |
|
4549 |
||
4550 |
"/ ANSI seems to allow a sequence to be replaced by another sequence, |
|
4551 |
"/ whereas the old ST80 meant replace all occurrences... - sigh. |
|
22231 | 4552 |
oldElement isString ifTrue:[ |
4553 |
newElement isString ifTrue:[ |
|
20874 | 4554 |
^ self copyReplaceString:oldElement withString:newElement. |
4555 |
]. |
|
23552 | 4556 |
self halt:'check if this is legal (trying to replace string by non-string)'. |
16202 | 4557 |
]. |
22231 | 4558 |
newElement isString ifTrue:[ |
23552 | 4559 |
self halt:'check if this is legal (trying to replace non-string by string)'. |
16202 | 4560 |
]. |
16212 | 4561 |
^ super copyReplaceAll:oldElement with:newElement |
23552 | 4562 |
|
4563 |
"Modified: / 03-12-2018 / 16:03:32 / Stefan Vogel" |
|
16202 | 4564 |
! |
4565 |
||
8347 | 4566 |
copyReplaceString:subString withString:newString |
4567 |
"return a copy of the receiver, with all sequences of subString replaced |
|
4568 |
by newString (i.e. slice in the newString in place of the oldString)." |
|
4569 |
||
4570 |
|tmpStream idx idx1| |
|
4571 |
||
15523 | 4572 |
tmpStream := self species writeStream. |
8347 | 4573 |
idx := 1. |
4574 |
[idx ~~ 0] whileTrue:[ |
|
20874 | 4575 |
idx1 := idx. |
4576 |
idx := self indexOfSubCollection:subString startingAt:idx. |
|
4577 |
idx ~~ 0 ifTrue:[ |
|
4578 |
tmpStream nextPutAll:(self copyFrom:idx1 to:idx-1). |
|
4579 |
tmpStream nextPutAll:newString. |
|
4580 |
idx := idx + subString size |
|
4581 |
] |
|
8347 | 4582 |
]. |
4583 |
tmpStream nextPutAll:(self copyFrom:idx1). |
|
4584 |
^ tmpStream contents |
|
4585 |
||
4586 |
" |
|
9306 | 4587 |
'12345678901234567890' copyReplaceString:'123' withString:'OneTwoThree' |
4588 |
'12345678901234567890' copyReplaceString:'123' withString:'*' |
|
4589 |
'12345678901234567890' copyReplaceString:'234' withString:'foo' |
|
22231 | 4590 |
'12345678901234567890' asUnicode16String copyReplaceString:'234' asUnicode16String withString:'foo' |
4591 |
'12345678901234567890' asUnicode16String copyReplaceString:'234' withString:'foo' |
|
4592 |
'12345678901234567890' asUnicode16String copyReplaceString:'234' withString:'foo' asUnicode16String |
|
9306 | 4593 |
|
4594 |
('a string with spaces' copyReplaceAll:$ withAll:' foo ') |
|
20874 | 4595 |
copyReplaceString:'foo' withString:'bar' |
8347 | 4596 |
" |
4597 |
||
4598 |
"Modified: / 31-05-1999 / 12:33:59 / cg" |
|
4599 |
"Created: / 12-05-2004 / 12:00:00 / cg" |
|
4600 |
! |
|
4601 |
||
8040
f10cca439066
copyWith for mixed-width string/character
Claus Gittinger <cg@exept.de>
parents:
8037
diff
changeset
|
4602 |
copyWith:aCharacter |
19464 | 4603 |
"return a new string containing the receiver's characters |
8874 | 4604 |
and the single new character, aCharacter. |
8040
f10cca439066
copyWith for mixed-width string/character
Claus Gittinger <cg@exept.de>
parents:
8037
diff
changeset
|
4605 |
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
|
4606 |
as argument, but equivalent to copy-and-addLast. |
8874 | 4607 |
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
|
4608 |
(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
|
4609 |
|
f10cca439066
copyWith for mixed-width string/character
Claus Gittinger <cg@exept.de>
parents:
8037
diff
changeset
|
4610 |
|sz newString| |
f10cca439066
copyWith for mixed-width string/character
Claus Gittinger <cg@exept.de>
parents:
8037
diff
changeset
|
4611 |
|
f10cca439066
copyWith for mixed-width string/character
Claus Gittinger <cg@exept.de>
parents:
8037
diff
changeset
|
4612 |
aCharacter bitsPerCharacter > self bitsPerCharacter ifTrue:[ |
20874 | 4613 |
sz := self size. |
4614 |
newString := aCharacter stringSpecies new:sz + 1. |
|
4615 |
newString replaceFrom:1 to:sz with:self startingAt:1. |
|
4616 |
newString at:sz+1 put:aCharacter. |
|
4617 |
^ newString. |
|
8040
f10cca439066
copyWith for mixed-width string/character
Claus Gittinger <cg@exept.de>
parents:
8037
diff
changeset
|
4618 |
]. |
f10cca439066
copyWith for mixed-width string/character
Claus Gittinger <cg@exept.de>
parents:
8037
diff
changeset
|
4619 |
^ super copyWith:aCharacter |
17241 | 4620 |
! |
4621 |
||
4622 |
restAfter:keyword withoutSeparators:strip |
|
23392 | 4623 |
"compare the left part of the receiver with keyword, |
4624 |
if it matches return the right rest. |
|
17241 | 4625 |
Finally, if strip is true, remove whiteSpace. |
23392 | 4626 |
If the string does not start with keyword, return nil. |
4627 |
||
17241 | 4628 |
This method is used to match and extract lines of the form: |
20874 | 4629 |
something: rest |
17241 | 4630 |
where we are interested in rest, but only if the receiver string |
4631 |
begins with something. |
|
4632 |
||
4633 |
You may wonder why such a specialized method exists here |
|
4634 |
- this is so common when processing mailboxes, |
|
4635 |
rcs files, nntp/pop3 responses, that is was considered worth |
|
4636 |
a special method here to avoid having the code below a hundred |
|
4637 |
times in variuos places." |
|
4638 |
||
4639 |
|rest| |
|
4640 |
||
4641 |
(self startsWith:keyword) ifTrue:[ |
|
20874 | 4642 |
rest := self copyFrom:(keyword size + 1). |
4643 |
strip ifTrue:[ |
|
4644 |
rest := rest withoutSeparators |
|
4645 |
]. |
|
4646 |
^ rest |
|
17241 | 4647 |
]. |
4648 |
^ nil |
|
4649 |
||
4650 |
" |
|
4651 |
'foo: hello world' restAfter:'foo:' withoutSeparators:true |
|
4652 |
'funny: something' restAfter:'foo:' withoutSeparators:true |
|
4653 |
||
4654 |
'foo: hello world ' restAfter:'foo:' withoutSeparators:true |
|
4655 |
'foo: hello world ' restAfter:'foo:' withoutSeparators:false |
|
4656 |
" |
|
4657 |
||
23392 | 4658 |
"Created: / 25-11-1995 / 11:04:18 / cg" |
4659 |
"Modified (comment): / 27-09-2018 / 10:01:55 / Claus Gittinger" |
|
17241 | 4660 |
! |
4661 |
||
4662 |
splitAtString:subString withoutSeparators:strip |
|
4663 |
"If the receiver is of the form: |
|
20874 | 4664 |
<left><subString><right> |
17241 | 4665 |
return a collection containing left and right only. |
4666 |
If strip is true, remove whiteSpace in the returned substrings." |
|
4667 |
||
4668 |
|idx left right| |
|
4669 |
||
4670 |
(idx := self indexOfSubCollection:subString) ~~ 0 ifTrue:[ |
|
20874 | 4671 |
left := self copyTo:(idx - 1). |
4672 |
right := self copyFrom:(idx + subString size). |
|
4673 |
strip ifTrue:[ |
|
4674 |
left := left withoutSeparators. |
|
4675 |
right := right withoutSeparators. |
|
4676 |
]. |
|
4677 |
^ StringCollection with:left with:right |
|
17241 | 4678 |
]. |
23001 | 4679 |
self proceedableError:'substring not present in receiver'. |
17241 | 4680 |
^ self |
4681 |
||
4682 |
" |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
4683 |
'hello -> world' splitAtString:'->' withoutSeparators:false |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
4684 |
'hello -> world' splitAtString:'->' withoutSeparators:true |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
4685 |
'hello -> ' splitAtString:'->' withoutSeparators:true |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
4686 |
'hello > error' splitAtString:'->' withoutSeparators:true |
17241 | 4687 |
" |
4688 |
||
23001 | 4689 |
"Created: / 25-11-1995 / 11:04:18 / cg" |
4690 |
"Modified: / 24-05-2018 / 14:55:52 / Claus Gittinger" |
|
608 | 4691 |
! ! |
4692 |
||
4693 |
!CharacterArray methodsFor:'displaying'! |
|
4694 |
||
2762 | 4695 |
displayOn:aGC x:x y:y from:start to:stop |
4696 |
"display the receiver on a GC" |
|
4697 |
||
12171 | 4698 |
self displayOn:aGC x:x y:y from:start to:stop opaque:false |
4699 |
||
4700 |
"Modified: 12.5.1996 / 12:49:33 / cg" |
|
4701 |
! |
|
4702 |
||
4703 |
displayOn:aGC x:x y:y from:start to:stop opaque:opaque |
|
4704 |
"display the receiver on a GC" |
|
4705 |
||
2762 | 4706 |
"q&d hack" |
4707 |
||
12177 | 4708 |
(start == 1 and:[stop == self size]) ifTrue:[ |
20874 | 4709 |
self displayOn:aGC x:x y:y opaque:opaque. |
12177 | 4710 |
] ifFalse:[ |
20874 | 4711 |
(self copyFrom:start to:stop) displayOn:aGC x:x y:y opaque:opaque. |
12177 | 4712 |
]. |
2762 | 4713 |
! |
4714 |
||
7082 | 4715 |
displayOn:aGc x:x y:y opaque:opaque |
2762 | 4716 |
"display the receiver in a graphicsContext - this method allows |
4717 |
strings to be used like DisplayObjects." |
|
4718 |
||
4719 |
|s| |
|
4720 |
||
4721 |
s := self string. |
|
7082 | 4722 |
opaque ifTrue:[ |
20874 | 4723 |
aGc displayOpaqueString:s x:x y:y. |
2762 | 4724 |
] ifFalse:[ |
20874 | 4725 |
aGc displayString:s x:x y:y. |
2762 | 4726 |
]. |
1381 | 4727 |
|
4728 |
"Modified: 11.5.1996 / 14:42:48 / cg" |
|
4729 |
! |
|
4730 |
||
2762 | 4731 |
displayOpaqueOn:aGC x:x y:y from:start to:stop |
4732 |
"display the receiver on a GC" |
|
4733 |
||
12171 | 4734 |
self displayOn:aGC x:x y:y from:start to:stop opaque:true |
608 | 4735 |
! ! |
4736 |
||
1375
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
4737 |
!CharacterArray methodsFor:'emphasis'! |
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
4738 |
|
11632
b82e9f3cabd1
new: #actionForAll: #emphasisAtPoint:on:
Stefan Vogel <sv@exept.de>
parents:
11630
diff
changeset
|
4739 |
actionForAll:aBlock |
12326
c67f12213ddb
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
12319
diff
changeset
|
4740 |
"change the action block of all characters. |
c67f12213ddb
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
12319
diff
changeset
|
4741 |
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
|
4742 |
|
b82e9f3cabd1
new: #actionForAll: #emphasisAtPoint:on:
Stefan Vogel <sv@exept.de>
parents:
11630
diff
changeset
|
4743 |
^ self asText actionForAll:aBlock |
b82e9f3cabd1
new: #actionForAll: #emphasisAtPoint:on:
Stefan Vogel <sv@exept.de>
parents:
11630
diff
changeset
|
4744 |
! |
b82e9f3cabd1
new: #actionForAll: #emphasisAtPoint:on:
Stefan Vogel <sv@exept.de>
parents:
11630
diff
changeset
|
4745 |
|
5732 | 4746 |
allBold |
12326
c67f12213ddb
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
12319
diff
changeset
|
4747 |
"return a text object representing the receiver, but all boldified" |
5732 | 4748 |
|
8874 | 4749 |
"this test allows for small non-gui apps to be built without libbasic2 (where Text is)" |
6187 | 4750 |
Text isNil ifTrue:[^ self]. |
5732 | 4751 |
^ self asText allBold |
4752 |
||
4753 |
" |
|
4754 |
Transcript showCR:'hello' asText allBold |
|
4755 |
Transcript showCR:'hello' allBold |
|
4756 |
" |
|
4757 |
! |
|
4758 |
||
5898
7bc4f1593b4e
allItalic now understood by CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5848
diff
changeset
|
4759 |
allItalic |
12326
c67f12213ddb
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
12319
diff
changeset
|
4760 |
"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
|
4761 |
|
8874 | 4762 |
"this test allows for small non-gui apps to be built without libbasic2 (where Text is)" |
6187 | 4763 |
Text isNil ifTrue:[^ self]. |
5898
7bc4f1593b4e
allItalic now understood by CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5848
diff
changeset
|
4764 |
^ self asText allItalic |
7bc4f1593b4e
allItalic now understood by CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5848
diff
changeset
|
4765 |
|
7bc4f1593b4e
allItalic now understood by CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5848
diff
changeset
|
4766 |
" |
7bc4f1593b4e
allItalic now understood by CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5848
diff
changeset
|
4767 |
Transcript showCR:'hello' asText allItalic |
7bc4f1593b4e
allItalic now understood by CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5848
diff
changeset
|
4768 |
Transcript showCR:'hello' allItalic |
7bc4f1593b4e
allItalic now understood by CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5848
diff
changeset
|
4769 |
" |
7bc4f1593b4e
allItalic now understood by CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5848
diff
changeset
|
4770 |
! |
7bc4f1593b4e
allItalic now understood by CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5848
diff
changeset
|
4771 |
|
23903 | 4772 |
allNonBold |
4773 |
"make all characters non-bold; |
|
4774 |
I already have no emphasis, so the receiver string is returned" |
|
4775 |
||
4776 |
^ self |
|
4777 |
||
4778 |
"Created: / 13-03-2019 / 21:02:45 / Claus Gittinger" |
|
4779 |
! |
|
4780 |
||
11345 | 4781 |
allStrikedOut |
12326
c67f12213ddb
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
12319
diff
changeset
|
4782 |
"return a text object representing the receiver, but all in strikeout" |
11345 | 4783 |
|
4784 |
"this test allows for small non-gui apps to be built without libbasic2 (where Text is)" |
|
4785 |
Text isNil ifTrue:[^ self]. |
|
4786 |
^ self asText allStrikedOut |
|
4787 |
||
4788 |
" |
|
4789 |
Transcript showCR:'hello' asText allStrikedOut |
|
4790 |
Transcript showCR:'hello' allStrikedOut |
|
4791 |
" |
|
4792 |
! |
|
4793 |
||
10862 | 4794 |
allUnderlined |
12326
c67f12213ddb
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
12319
diff
changeset
|
4795 |
"return a text object representing the receiver, but all with underline" |
10862 | 4796 |
|
4797 |
"this test allows for small non-gui apps to be built without libbasic2 (where Text is)" |
|
4798 |
Text isNil ifTrue:[^ self]. |
|
4799 |
^ self asText allUnderlined |
|
4800 |
||
4801 |
" |
|
4802 |
Transcript showCR:'hello' asText allUnderlined |
|
4803 |
Transcript showCR:'hello' allUnderlined |
|
4804 |
" |
|
4805 |
! |
|
4806 |
||
19774 | 4807 |
asActionLinkTo:aBlock |
4808 |
"change the action block of all characters and colorize as a link. |
|
4809 |
Some widgets use this like a href if clicked onto the text |
|
4810 |
(for example, the system-browser's info at the bottom is such a widget)." |
|
4811 |
||
4812 |
"/ the blue may become a user-setting some time later.. |
|
4813 |
^ (self actionForAll:aBlock) withColor:(Color blue) |
|
4814 |
! |
|
4815 |
||
6228 | 4816 |
colorizeAllWith:aColor |
12326
c67f12213ddb
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
12319
diff
changeset
|
4817 |
"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
|
4818 |
|
c67f12213ddb
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
12319
diff
changeset
|
4819 |
"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
|
4820 |
Text isNil ifTrue:[^ self]. |
6228 | 4821 |
^ self asText colorizeAllWith:aColor |
4822 |
||
4823 |
" |
|
4824 |
Transcript showCR:('hello' colorizeAllWith:Color red) |
|
4825 |
Transcript showCR:('world' colorizeAllWith:Color green darkened) |
|
4826 |
" |
|
4827 |
! |
|
4828 |
||
12330
f9b57b0b8a8b
added: #colorizeAllWith:on:
Claus Gittinger <cg@exept.de>
parents:
12326
diff
changeset
|
4829 |
colorizeAllWith:fgColor on:bgColor |
f9b57b0b8a8b
added: #colorizeAllWith:on:
Claus Gittinger <cg@exept.de>
parents:
12326
diff
changeset
|
4830 |
"return a text object representing the receiver, but all colorized with |
f9b57b0b8a8b
added: #colorizeAllWith:on:
Claus Gittinger <cg@exept.de>
parents:
12326
diff
changeset
|
4831 |
both fg and background color" |
f9b57b0b8a8b
added: #colorizeAllWith:on:
Claus Gittinger <cg@exept.de>
parents:
12326
diff
changeset
|
4832 |
|
f9b57b0b8a8b
added: #colorizeAllWith:on:
Claus Gittinger <cg@exept.de>
parents:
12326
diff
changeset
|
4833 |
"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
|
4834 |
Text isNil ifTrue:[^ self]. |
f9b57b0b8a8b
added: #colorizeAllWith:on:
Claus Gittinger <cg@exept.de>
parents:
12326
diff
changeset
|
4835 |
^ self asText colorizeAllWith:fgColor on:bgColor |
f9b57b0b8a8b
added: #colorizeAllWith:on:
Claus Gittinger <cg@exept.de>
parents:
12326
diff
changeset
|
4836 |
|
f9b57b0b8a8b
added: #colorizeAllWith:on:
Claus Gittinger <cg@exept.de>
parents:
12326
diff
changeset
|
4837 |
" |
f9b57b0b8a8b
added: #colorizeAllWith:on:
Claus Gittinger <cg@exept.de>
parents:
12326
diff
changeset
|
4838 |
Transcript showCR:('hello' colorizeAllWith:Color red on:Color yellow) |
f9b57b0b8a8b
added: #colorizeAllWith:on:
Claus Gittinger <cg@exept.de>
parents:
12326
diff
changeset
|
4839 |
Transcript showCR:('world' colorizeAllWith:Color red) |
f9b57b0b8a8b
added: #colorizeAllWith:on:
Claus Gittinger <cg@exept.de>
parents:
12326
diff
changeset
|
4840 |
" |
f9b57b0b8a8b
added: #colorizeAllWith:on:
Claus Gittinger <cg@exept.de>
parents:
12326
diff
changeset
|
4841 |
! |
f9b57b0b8a8b
added: #colorizeAllWith:on:
Claus Gittinger <cg@exept.de>
parents:
12326
diff
changeset
|
4842 |
|
1396
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4843 |
emphasis |
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4844 |
"return the emphasis. |
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4845 |
Since characterArrays do not hold any emphasis information, |
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4846 |
nil (no emphasis) is returned here." |
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4847 |
|
23060 | 4848 |
^ RunArray new:(self size) withAll:nil |
4849 |
||
4850 |
"Created: / 14-05-1996 / 13:58:58 / cg" |
|
4851 |
"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
|
4852 |
! |
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4853 |
|
6655 | 4854 |
emphasis:emphasisCollection |
4855 |
^ self asText emphasis:emphasisCollection |
|
4856 |
||
4857 |
" |
|
4858 |
Transcript showCR:('hello' emphasis:#(bold bold bold bold bold)) |
|
4859 |
" |
|
4860 |
! |
|
4861 |
||
1375
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
4862 |
emphasisAt:characterIndex |
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
4863 |
"return the emphasis at some index. |
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
4864 |
Since characterArrays do not hold any emphasis information, |
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
4865 |
nil (no emphasis) is returned here." |
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
4866 |
|
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
4867 |
^ nil |
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
4868 |
|
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
4869 |
"Created: 11.5.1996 / 14:13:27 / cg" |
1396
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4870 |
! |
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4871 |
|
11632
b82e9f3cabd1
new: #actionForAll: #emphasisAtPoint:on:
Stefan Vogel <sv@exept.de>
parents:
11630
diff
changeset
|
4872 |
emphasisAtPoint:aPoint on:aGC |
b82e9f3cabd1
new: #actionForAll: #emphasisAtPoint:on:
Stefan Vogel <sv@exept.de>
parents:
11630
diff
changeset
|
4873 |
"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
|
4874 |
|
b82e9f3cabd1
new: #actionForAll: #emphasisAtPoint:on:
Stefan Vogel <sv@exept.de>
parents:
11630
diff
changeset
|
4875 |
^ nil |
b82e9f3cabd1
new: #actionForAll: #emphasisAtPoint:on:
Stefan Vogel <sv@exept.de>
parents:
11630
diff
changeset
|
4876 |
! |
b82e9f3cabd1
new: #actionForAll: #emphasisAtPoint:on:
Stefan Vogel <sv@exept.de>
parents:
11630
diff
changeset
|
4877 |
|
1396
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4878 |
emphasisCollection |
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4879 |
"return the emphasis. |
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4880 |
Since characterArrays do not hold any emphasis information, |
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4881 |
nil (no emphasis) is returned here." |
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4882 |
|
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4883 |
^ RunArray new:(self size) |
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4884 |
|
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4885 |
"Created: 14.5.1996 / 13:58:58 / cg" |
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4886 |
"Modified: 14.5.1996 / 15:02:29 / cg" |
3404 | 4887 |
! |
4888 |
||
11919 | 4889 |
emphasiseFrom:start to:stop with:newEmphasis |
4890 |
"set to the emphasis within some range. return the receiver" |
|
4891 |
||
4892 |
^ self asText emphasiseFrom:start to:stop with:newEmphasis |
|
4893 |
||
4894 |
" |
|
12177 | 4895 |
'hello' emphasiseFrom:2 with:#italic |
11919 | 4896 |
|
4897 |
Transcript showCR:('hello' emphasiseFrom:2 with:#italic) |
|
4898 |
" |
|
4899 |
! |
|
4900 |
||
4901 |
emphasiseFrom:start with:newEmphasis |
|
4902 |
"set to the emphasis within some range. return the receiver" |
|
4903 |
||
4904 |
^ self emphasiseFrom:start to:(self size) with:newEmphasis |
|
4905 |
||
4906 |
" |
|
12177 | 4907 |
'hello' emphasiseFrom:2 with:#italic |
11919 | 4908 |
|
4909 |
Transcript showCR:('hello' emphasiseFrom:2 with:#italic) |
|
4910 |
" |
|
4911 |
! |
|
4912 |
||
3404 | 4913 |
emphasizeAllWith:emphasis |
12326
c67f12213ddb
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
12319
diff
changeset
|
4914 |
"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
|
4915 |
|
c67f12213ddb
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
12319
diff
changeset
|
4916 |
"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
|
4917 |
Text isNil ifTrue:[^ self]. |
3404 | 4918 |
^ self asText emphasizeAllWith:emphasis |
4919 |
||
4920 |
" |
|
4921 |
Transcript showCR:('hello' emphasizeAllWith:#bold) |
|
3599 | 4922 |
Transcript showCR:('hello' emphasizeAllWith:(#color -> Color red)) |
4923 |
Transcript showCR:('hello' emphasizeAllWith:(#color -> Color red)) |
|
4924 |
" |
|
4925 |
||
4926 |
"Modified: / 17.6.1998 / 12:51:44 / cg" |
|
5097 | 4927 |
! |
4928 |
||
4929 |
makeSelectorBoldIn:aClass |
|
14050 | 4930 |
"the receiver represents some source code for a method in aClass. |
5097 | 4931 |
Change myself to boldify the selector. |
14050 | 4932 |
Not yet implemented (could easily use the syntaxHighlighter for this ...). |
4933 |
For protocol compatibility with other smalltalks" |
|
5097 | 4934 |
|
4935 |
^ self |
|
4936 |
||
14050 | 4937 |
"Created: / 13-12-1999 / 21:49:24 / cg" |
4938 |
! |
|
4939 |
||
19762 | 4940 |
withColor:aColorOrColorSymbol |
20697 | 4941 |
"return a text object representing the receiver, but all colorized. |
4942 |
Usage of a colorSymbol is considered bad style (provided for backward compatibility); |
|
4943 |
please change to pass a proper color |
|
4944 |
(makes it easier to find color uses)" |
|
4945 |
||
4946 |
|color| |
|
4947 |
||
19762 | 4948 |
aColorOrColorSymbol isSymbol ifTrue:[ |
20697 | 4949 |
color := (Color perform:aColorOrColorSymbol) |
4950 |
] ifFalse:[ |
|
4951 |
color := aColorOrColorSymbol |
|
4952 |
]. |
|
4953 |
^ self colorizeAllWith:color |
|
19762 | 4954 |
|
4955 |
" |
|
4956 |
Transcript showCR:('hello' withColor:#red) |
|
20697 | 4957 |
Transcript showCR:('world' withColor:#blue) |
16697 | 4958 |
Transcript showCR:('hello' withColor:Color red) |
4959 |
Transcript showCR:('world' withColor:Color green darkened) |
|
4960 |
" |
|
4961 |
! |
|
4962 |
||
22911 | 4963 |
withColor:foregroundColorOrColorSymbol on:backgroundColorOrColorSymbol |
4964 |
"return a text object representing the receiver, but all colorized with both fg and bg. |
|
4965 |
Usage of a colorSymbol is considered bad style (provided for backward compatibility); |
|
4966 |
please change to pass a proper color |
|
4967 |
(makes it easier to find color uses)" |
|
4968 |
||
4969 |
|fgColor bgColor| |
|
4970 |
||
4971 |
foregroundColorOrColorSymbol isSymbol ifTrue:[ |
|
4972 |
fgColor := (Color perform:foregroundColorOrColorSymbol) |
|
4973 |
] ifFalse:[ |
|
4974 |
fgColor := foregroundColorOrColorSymbol |
|
4975 |
]. |
|
4976 |
backgroundColorOrColorSymbol isSymbol ifTrue:[ |
|
4977 |
bgColor := (Color perform:backgroundColorOrColorSymbol) |
|
4978 |
] ifFalse:[ |
|
4979 |
bgColor := backgroundColorOrColorSymbol |
|
4980 |
]. |
|
4981 |
^ self colorizeAllWith:fgColor on:bgColor |
|
4982 |
||
4983 |
" |
|
4984 |
Transcript showCR:('hello' withColor:#red on:#blue) |
|
4985 |
Transcript showCR:('hello' withColor:Color red on:Color blue) |
|
4986 |
" |
|
4987 |
! |
|
4988 |
||
14050 | 4989 |
withoutAnyColorEmphasis |
4990 |
"for protocol compatibility with Text" |
|
4991 |
||
4992 |
^ self |
|
4993 |
||
4994 |
"Created: / 06-03-2012 / 18:15:38 / cg" |
|
6653 | 4995 |
! |
4996 |
||
12396 | 4997 |
withoutBackgroundColorEmphasis |
14050 | 4998 |
"for protocol compatibility with Text" |
4999 |
||
12396 | 5000 |
^ self |
14050 | 5001 |
|
5002 |
"Modified (comment): / 06-03-2012 / 18:14:27 / cg" |
|
12396 | 5003 |
! |
5004 |
||
23903 | 5005 |
withoutEmphasis |
5006 |
"for protocol compatibility with Text" |
|
5007 |
||
5008 |
^ self |
|
5009 |
||
5010 |
"Created: / 13-03-2019 / 21:03:41 / Claus Gittinger" |
|
5011 |
! |
|
5012 |
||
6653 | 5013 |
withoutEmphasis:emphasisToRemove |
14050 | 5014 |
"for protocol compatibility with Text" |
5015 |
||
6653 | 5016 |
^ self |
14050 | 5017 |
|
5018 |
"Modified (comment): / 06-03-2012 / 18:14:29 / cg" |
|
12396 | 5019 |
! |
5020 |
||
5021 |
withoutForegroundColorEmphasis |
|
14050 | 5022 |
"for protocol compatibility with Text" |
5023 |
||
12396 | 5024 |
^ self |
14050 | 5025 |
|
5026 |
"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
|
5027 |
! ! |
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
5028 |
|
7265 | 5029 |
!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
|
5030 |
|
20013 | 5031 |
asDenormalizedUnicodeString |
5032 |
"return a new string containing the same characters, as a denormalized Unicode string. |
|
5033 |
This replaces diacritical chars (umlauts, accented chars etc) by |
|
5034 |
a sequence with combination characters. |
|
5035 |
(i.e. a plain character followed by a combining diacritical in the 0x03xx range)" |
|
5036 |
||
20014 | 5037 |
|map outStream mapChar| |
20013 | 5038 |
|
22406 | 5039 |
self containsNon7BitAscii ifFalse:[ |
5040 |
^ self "/ I cannot contain any diacritical chars |
|
5041 |
]. |
|
5042 |
||
20013 | 5043 |
map := self class unicodeDenormalizationMap. |
22406 | 5044 |
mapChar := [:char | |
20014 | 5045 |
|mappedChars| |
5046 |
||
5047 |
(mappedChars := map at:char ifAbsent:nil) notNil ifTrue:[ |
|
5048 |
mappedChars do:mapChar. |
|
5049 |
] ifFalse:[ |
|
5050 |
outStream nextPut:char. |
|
5051 |
]. |
|
5052 |
]. |
|
20013 | 5053 |
|
20014 | 5054 |
outStream := WriteStream on:(Unicode16String new:self size). |
5055 |
self do:mapChar. |
|
20013 | 5056 |
^ outStream contents asSingleByteStringIfPossible. |
5057 |
||
5058 |
" |
|
5059 |
'Ö' asDenormalizedUnicodeString |
|
5060 |
'aÖÄx' asDenormalizedUnicodeString |
|
5061 |
'abc' asDenormalizedUnicodeString |
|
22406 | 5062 |
'ṩ' asDenormalizedUnicodeString |
5063 |
" |
|
5064 |
||
5065 |
"Modified (format): / 02-01-2018 / 18:52:33 / stefan" |
|
20013 | 5066 |
! |
5067 |
||
20011 | 5068 |
asNormalizedUnicodeString |
20010 | 5069 |
"return a new string containing the same characters, as a normalized Unicode string. |
5070 |
This replaces combination characters by corresponding single characters. |
|
20863 | 5071 |
(i.e. diaresis and other combining diacriticals in the 0x03xx range). |
5072 |
Caveat: |
|
5073 |
possibly incomplete: only COMBINING_DIACRITICAL_MARKS are cared for. |
|
5074 |
Does not care for COMBINING_DIACRITICAL_MARKS_EXTENDED |
|
20864 | 5075 |
and COMBINING_DIACRITICAL_MARKS_SUPPLEMENT. |
5076 |
However; those are used for German dialectology, ancient Greek and other similar |
|
5077 |
exotic uses. Probably noone will ever even notice that they are missing..." |
|
20010 | 5078 |
|
5079 |
|outStream prevChar map mapEntries mappedChar| |
|
5080 |
||
5081 |
map := self class unicodeNormalizationMap. |
|
20017 | 5082 |
|
20010 | 5083 |
self bitsPerCharacter <= 8 ifTrue:[^ self]. "/ I cannot contain any overtypes |
20017 | 5084 |
(self contains:[:ch | ch unicodeBlock == #'COMBINING_DIACRITICAL_MARKS']) ifFalse:[^ self]. "/ I do not contain any overtypes |
20010 | 5085 |
|
5086 |
outStream := WriteStream on:(self species new:self size). |
|
5087 |
self do:[:char | |
|
20017 | 5088 |
((char unicodeBlock == #'COMBINING_DIACRITICAL_MARKS') |
20010 | 5089 |
and:[ (mapEntries := map at:char ifAbsent:nil) notNil |
5090 |
and:[ (mappedChar := mapEntries at:prevChar ifAbsent:nil) notNil ]]) ifTrue:[ |
|
20014 | 5091 |
prevChar := mappedChar. |
20010 | 5092 |
] ifFalse:[ |
5093 |
prevChar notNil ifTrue:[ outStream nextPut:prevChar]. |
|
5094 |
prevChar := char |
|
5095 |
]. |
|
5096 |
]. |
|
20011 | 5097 |
prevChar notNil ifTrue:[ outStream nextPut:prevChar]. |
20010 | 5098 |
^ outStream contents asSingleByteStringIfPossible. |
5099 |
||
5100 |
" |
|
5101 |
self unicodeNormalizationMap |
|
20012 | 5102 |
('O' , (Character value:16r0308)) asNormalizedUnicodeString -> 'Ö' |
20010 | 5103 |
('O' , (Character value:16r0308) , |
20012 | 5104 |
'A' , (Character value:16r0308)) asNormalizedUnicodeString -> 'ÖÄ' |
20014 | 5105 |
('s' , (Character value:16r0323) , (Character value:16r0307)) asNormalizedUnicodeString -> 'ṩ' |
5106 |
('s' , (Character value:16r0307) , (Character value:16r0323)) asNormalizedUnicodeString -> 'ṩ' |
|
20010 | 5107 |
" |
5108 |
! |
|
5109 |
||
12767
17d2c7268065
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12679
diff
changeset
|
5110 |
decodeAsLiteralArray |
17d2c7268065
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12679
diff
changeset
|
5111 |
"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
|
5112 |
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
|
5113 |
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
|
5114 |
|
17d2c7268065
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12679
diff
changeset
|
5115 |
^ self |
17d2c7268065
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12679
diff
changeset
|
5116 |
! |
17d2c7268065
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12679
diff
changeset
|
5117 |
|
991
f0b45a4b82d2
first attempt in encoding/decoding for different character representations (mac/msdos etc.)
Claus Gittinger <cg@exept.de>
parents:
986
diff
changeset
|
5118 |
decodeFrom:encodingSymbol |
8874 | 5119 |
"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
|
5120 |
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
|
5121 |
|
ca774158b317
code cleanup (removed old obsolete encoding stuff)
Claus Gittinger <cg@exept.de>
parents:
8102
diff
changeset
|
5122 |
|myEncoding encoder| |
7950 | 5123 |
|
5124 |
encodingSymbol isNil ifTrue:[^ self]. |
|
5125 |
myEncoding := self encoding. |
|
5126 |
encodingSymbol == myEncoding ifTrue:[^ self]. |
|
5127 |
||
22451 | 5128 |
encoder := CharacterEncoder encoderToEncodeFrom:myEncoding into:encodingSymbol. |
8124
ca774158b317
code cleanup (removed old obsolete encoding stuff)
Claus Gittinger <cg@exept.de>
parents:
8102
diff
changeset
|
5129 |
encoder isNil ifTrue:[^ self]. |
ca774158b317
code cleanup (removed old obsolete encoding stuff)
Claus Gittinger <cg@exept.de>
parents:
8102
diff
changeset
|
5130 |
^ encoder decodeString:self. |
22451 | 5131 |
|
5132 |
"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
|
5133 |
! |
f0b45a4b82d2
first attempt in encoding/decoding for different character representations (mac/msdos etc.)
Claus Gittinger <cg@exept.de>
parents:
986
diff
changeset
|
5134 |
|
9192 | 5135 |
encodeFrom:oldEncoding into:newEncoding |
5136 |
^ CharacterEncoder encodeString:self from:oldEncoding into:newEncoding |
|
14405
1d0d99a95bba
comment/format in:7 methods
Claus Gittinger <cg@exept.de>
parents:
14168
diff
changeset
|
5137 |
|
1d0d99a95bba
comment/format in:7 methods
Claus Gittinger <cg@exept.de>
parents:
14168
diff
changeset
|
5138 |
" |
17600 | 5139 |
'äüö' encodeFrom:#iso8859 into:#utf8 |
5140 |
('äüö' encodeFrom:#iso8859 into:#utf8) encodeFrom:#utf8 into:#unicode |
|
14405
1d0d99a95bba
comment/format in:7 methods
Claus Gittinger <cg@exept.de>
parents:
14168
diff
changeset
|
5141 |
" |
9192 | 5142 |
! |
5143 |
||
22451 | 5144 |
encodeInto:newEncoding |
5145 |
^ CharacterEncoder encodeString:self from:self encoding into:newEncoding |
|
5146 |
||
5147 |
" |
|
5148 |
'äüö' encodeInto:#utf8 |
|
5149 |
('äüö' encodeInto:#utf8) decodeFrom:#utf8 |
|
5150 |
" |
|
5151 |
||
5152 |
"Created: / 17-01-2018 / 17:43:20 / stefan" |
|
5153 |
! |
|
5154 |
||
9192 | 5155 |
rot13 |
8874 | 5156 |
"Usenet: from `rotate alphabet 13 places'] |
6835
bdb2cb03200c
added utf8Decode with 8-bit chars only
Claus Gittinger <cg@exept.de>
parents:
6834
diff
changeset
|
5157 |
The simple Caesar-cypher encryption that replaces each English |
8874 | 5158 |
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
|
5159 |
so that 'The butler did it!!' becomes 'Gur ohgyre qvq vg!!' |
8874 | 5160 |
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
|
5161 |
It is used to enclose the text in a sealed wrapper that the reader must choose |
8874 | 5162 |
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
|
5163 |
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
|
5164 |
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
|
5165 |
|
11865 | 5166 |
^ self rot:13 |
8874 | 5167 |
|
5168 |
" |
|
5169 |
'hello world' rot13 |
|
5170 |
'hello world' rot13 rot13 |
|
6835
bdb2cb03200c
added utf8Decode with 8-bit chars only
Claus Gittinger <cg@exept.de>
parents:
6834
diff
changeset
|
5171 |
" |
bdb2cb03200c
added utf8Decode with 8-bit chars only
Claus Gittinger <cg@exept.de>
parents:
6834
diff
changeset
|
5172 |
! |
bdb2cb03200c
added utf8Decode with 8-bit chars only
Claus Gittinger <cg@exept.de>
parents:
6834
diff
changeset
|
5173 |
|
11865 | 5174 |
rot:n |
5175 |
"Usenet: from `rotate alphabet N places'] |
|
5176 |
The simple Caesar-cypher encryption that replaces each English |
|
5177 |
letter with the one N places forward or back along the alphabet, |
|
5178 |
so that 'The butler did it!!' becomes 'Gur ohgyre qvq vg!!' by rot 13 |
|
5179 |
Most Usenet news reading and posting programs include a rot13 feature. |
|
5180 |
It is used to enclose the text in a sealed wrapper that the reader must choose |
|
5181 |
to open -- e.g., for posting things that might offend some readers, or spoilers. |
|
5182 |
A major advantage of rot13 over rot(N) for other N is that it |
|
5183 |
is self-inverse, so the same code can be used for encoding and decoding." |
|
5184 |
||
5185 |
^ self species |
|
20874 | 5186 |
streamContents:[:aStream | |
5187 |
self do:[:char | |
|
5188 |
aStream nextPut:(char rot:n) ]] |
|
11865 | 5189 |
|
5190 |
" |
|
5191 |
'hello world' rot:13 |
|
5192 |
('hello world' rot:13) rot:13 |
|
5193 |
" |
|
5194 |
! |
|
5195 |
||
15656 | 5196 |
utf16Encoded |
5197 |
"Return my UTF-16 representation as a new TwoByteString" |
|
5198 |
||
5199 |
|s| |
|
5200 |
||
5201 |
s := WriteStream on:(TwoByteString uninitializedNew:self size). |
|
5202 |
s nextPutAllUtf16:self. |
|
5203 |
^ s contents |
|
5204 |
||
5205 |
" |
|
5206 |
'abcde1234' utf16Encoded |
|
17600 | 5207 |
'abcdeäöüß' utf16Encoded |
15656 | 5208 |
" |
5209 |
||
5210 |
"Modified: / 11-05-2010 / 19:12:37 / cg" |
|
5211 |
! |
|
5212 |
||
9192 | 5213 |
utf8Encoded |
19837 | 5214 |
"return the UTF-8 representation of a Unicode string. |
5215 |
The resulting string is only useful to be stored on some external file, |
|
21318 | 5216 |
or sent to a communaction channel. |
5217 |
Not for being used inside ST/X." |
|
19837 | 5218 |
|
5219 |
|string stream| |
|
17625 | 5220 |
|
5221 |
string := self string. |
|
19837 | 5222 |
|
5223 |
"/ avoid creation of new strings if possible |
|
5224 |
string containsNon7BitAscii ifFalse:[ |
|
5225 |
^ string asSingleByteString |
|
5226 |
]. |
|
5227 |
||
5228 |
"make it size 2 for 1-byte strings" |
|
5229 |
stream := WriteStream on:(String uninitializedNew:(1 + (string size * 3 // 2))). |
|
5230 |
string utf8EncodedOn:stream. |
|
5231 |
^ stream contents. |
|
5232 |
||
5233 |
" |
|
5234 |
'hello' utf8Encoded asByteArray #[104 101 108 108 111] |
|
5235 |
(Character value:16r40) asString utf8Encoded asByteArray #[64] |
|
5236 |
(Character value:16r7F) asString utf8Encoded asByteArray #[127] |
|
5237 |
(Character value:16r80) asString utf8Encoded asByteArray #[194 128] |
|
5238 |
(Character value:16rFF) asString utf8Encoded asByteArray #[195 191] |
|
5239 |
(Character value:16r100) asString utf8Encoded asByteArray #[196 128] |
|
5240 |
(Character value:16r200) asString utf8Encoded asByteArray #[200 128] |
|
5241 |
(Character value:16r400) asString utf8Encoded asByteArray #[208 128] |
|
5242 |
(Character value:16r800) asString utf8Encoded asByteArray #[224 160 128] |
|
5243 |
(Character value:16r1000) asString utf8Encoded asByteArray #[225 128 128] |
|
5244 |
(Character value:16r2000) asString utf8Encoded asByteArray #[226 128 128] |
|
5245 |
(Character value:16r4000) asString utf8Encoded asByteArray #[228 128 128] |
|
5246 |
(Character value:16r8000) asString utf8Encoded asByteArray #[232 128 128] |
|
5247 |
(Character value:16rFFFF) asString utf8Encoded asByteArray #[239 191 191] |
|
21318 | 5248 |
(Character value:16r1FFFF) asString utf8Encoded asByteArray #[240 159 191 191] |
5249 |
(Character value:16r3FFFF) asString utf8Encoded asByteArray #[240 191 191 191] |
|
5250 |
(Character value:16rFFFFF) asString utf8Encoded asByteArray #[243 191 191 191] |
|
5251 |
(Character value:16r3FFFFF) asString utf8Encoded asByteArray #[248 143 191 191 191] |
|
19837 | 5252 |
|
18597 | 5253 |
'abcde1234' asUnicode32String utf8Encoded |
5254 |
'abcdeäöüß' asUnicode32String utf8Encoded |
|
12925
5c0a9a633ca1
comment/format in: #utf8Encoded
Claus Gittinger <cg@exept.de>
parents:
12912
diff
changeset
|
5255 |
" |
17625 | 5256 |
! |
5257 |
||
5258 |
utf8EncodedOn:aStream |
|
21468 | 5259 |
"write the UTF-8 representation of myself to aStream." |
5260 |
||
5261 |
aStream nextPutAllUtf8:self. |
|
5262 |
||
5263 |
"Modified: / 16-02-2017 / 16:51:32 / stefan" |
|
9192 | 5264 |
! ! |
5265 |
||
22566 | 5266 |
!CharacterArray methodsFor:'filling and replacing'! |
5267 |
||
5268 |
clearContents |
|
5269 |
"to be used with cryptographic keys, to wipe their contents after use" |
|
5270 |
||
5271 |
self atAllPut:(Character value:0) |
|
5272 |
! ! |
|
22451 | 5273 |
|
5274 |
||
24033 | 5275 |
|
5276 |
||
12111 | 5277 |
!CharacterArray methodsFor:'matching - glob expressions'! |
5278 |
||
5279 |
compoundMatch:aString |
|
5280 |
"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
|
5281 |
consisting of multiple simple GLOB patterns, separated by semicolons. |
12111 | 5282 |
This is usable with fileName pattern fields. |
23791 | 5283 |
This is a case sensitive match: lower/uppercase are considered different. |
12111 | 5284 |
|
5285 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20874 | 5286 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5287 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 5288 |
NOTICE: the receiver is the match pattern" |
5289 |
||
15890 | 5290 |
^ self compoundMatch:aString caseSensitive:true withoutSeparators:false |
12111 | 5291 |
|
5292 |
" |
|
5293 |
'f*' match:'foo' |
|
5294 |
'b*' match:'foo' |
|
5295 |
'f*;b*' match:'foo' |
|
5296 |
'f*;b*' match:'bar' |
|
5297 |
'f*;b*' compoundMatch:'foo' |
|
5298 |
'f*;b*' compoundMatch:'bar' |
|
23791 | 5299 |
'*.png;*.gif' compoundMatch:'bar.jpg' |
5300 |
'*.png;*.gif' compoundMatch:'bar.gif' |
|
5301 |
" |
|
5302 |
||
5303 |
"Modified: / 30-01-1998 / 11:40:18 / stefan" |
|
5304 |
"Modified: / 16-12-1999 / 01:22:08 / cg" |
|
5305 |
"Modified (comment): / 02-03-2019 / 12:21:33 / Claus Gittinger" |
|
12111 | 5306 |
! |
5307 |
||
15734 | 5308 |
compoundMatch:aString caseSensitive:caseSensitive |
12111 | 5309 |
"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
|
5310 |
consisting of multiple simple GLOB patterns, separated by semicolons. |
12111 | 5311 |
This is usable with fileName pattern fields. |
5312 |
||
5313 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20874 | 5314 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5315 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 5316 |
NOTICE: the receiver is the match pattern" |
5317 |
||
15890 | 5318 |
^self compoundMatch:aString caseSensitive:caseSensitive withoutSeparators:false |
5319 |
||
5320 |
" |
|
5321 |
'f*' match:'foo' |
|
5322 |
'b*' match:'foo' |
|
5323 |
'f*;b*' match:'foo' |
|
5324 |
'f*;b*' match:'bar' |
|
5325 |
'f*;b*' compoundMatch:'foo' |
|
5326 |
'f*;b*' compoundMatch:'bar' |
|
5327 |
'f*;b*' compoundMatch:'Foo' caseSensitive:false |
|
5328 |
'f*;b*' compoundMatch:'Bar' caseSensitive:false |
|
5329 |
'f*;b*' compoundMatch:'ccc' caseSensitive:false |
|
23791 | 5330 |
|
5331 |
'*.png;*.gif' compoundMatch:'bar.GIF' |
|
5332 |
'*.png;*.gif' compoundMatch:'bar.GIF' caseSensitive:false |
|
5333 |
" |
|
5334 |
||
5335 |
"Modified: / 15-04-1997 / 15:50:33 / cg" |
|
5336 |
"Modified: / 30-01-1998 / 11:40:18 / stefan" |
|
5337 |
"Created: / 16-12-1999 / 01:21:35 / cg" |
|
5338 |
"Modified (comment): / 02-03-2019 / 12:22:14 / Claus Gittinger" |
|
15890 | 5339 |
! |
5340 |
||
5341 |
compoundMatch:aString caseSensitive:caseSensitive withoutSeparators:withoutSeparators |
|
5342 |
"like match, but the receiver may be a compound match pattern, |
|
5343 |
consisting of multiple simple GLOB patterns, separated by semicolons. |
|
5344 |
If withoutSeparators is true, spaces around individual patterns are stripped off. |
|
5345 |
This is usable with fileName pattern fields. |
|
5346 |
||
5347 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20874 | 5348 |
NOT the ST-80 meaning. |
15890 | 5349 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
5350 |
NOTICE: the receiver is the match pattern" |
|
5351 |
||
12111 | 5352 |
|matchers| |
5353 |
||
5354 |
matchers := self asCollectionOfSubstringsSeparatedBy:$;. |
|
15890 | 5355 |
withoutSeparators ifTrue:[ matchers := matchers collect:[:each | each withoutSeparators] ]. |
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
5356 |
^ matchers |
20874 | 5357 |
contains:[:aPattern | |
5358 |
aPattern match:aString caseSensitive:caseSensitive escapeCharacter:nil |
|
5359 |
]. |
|
15734 | 5360 |
|
5361 |
" |
|
5362 |
'f*' match:'foo' |
|
5363 |
'b*' match:'foo' |
|
5364 |
'f*;b*' match:'foo' |
|
5365 |
'f*;b*' match:'bar' |
|
5366 |
'f*;b*' compoundMatch:'foo' |
|
5367 |
'f*;b*' compoundMatch:'bar' |
|
5368 |
'f*;b*' compoundMatch:'Foo' caseSensitive:false |
|
5369 |
'f*;b*' compoundMatch:'Bar' caseSensitive:false |
|
5370 |
'f*;b*' compoundMatch:'ccc' caseSensitive:false |
|
15890 | 5371 |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
5372 |
'f* ; b*' compoundMatch:'foo' |
15890 | 5373 |
'f* ; b*' compoundMatch:'foo' caseSensitive:true withoutSeparators:true |
15734 | 5374 |
" |
5375 |
||
5376 |
"Modified: / 15.4.1997 / 15:50:33 / cg" |
|
5377 |
"Modified: / 30.1.1998 / 11:40:18 / stefan" |
|
5378 |
"Created: / 16.12.1999 / 01:21:35 / cg" |
|
5379 |
! |
|
5380 |
||
5381 |
compoundMatch:aString ignoreCase:ignoreCase |
|
5382 |
<resource: #obsolete> |
|
5383 |
||
5384 |
"like match, but the receiver may be a compound match pattern, |
|
5385 |
consisting of multiple simple GLOB patterns, separated by semicolons. |
|
5386 |
This is usable with fileName pattern fields. |
|
5387 |
||
5388 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20874 | 5389 |
NOT the ST-80 meaning. |
15734 | 5390 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
5391 |
NOTICE: the receiver is the match pattern" |
|
5392 |
||
5393 |
^ self compoundMatch:aString caseSensitive:ignoreCase not |
|
12111 | 5394 |
|
5395 |
" |
|
5396 |
'f*' match:'foo' |
|
5397 |
'b*' match:'foo' |
|
5398 |
'f*;b*' match:'foo' |
|
5399 |
'f*;b*' match:'bar' |
|
5400 |
'f*;b*' compoundMatch:'foo' |
|
5401 |
'f*;b*' compoundMatch:'bar' |
|
5402 |
'f*;b*' compoundMatch:'Foo' ignoreCase:true |
|
5403 |
'f*;b*' compoundMatch:'Bar' ignoreCase:true |
|
5404 |
'f*;b*' compoundMatch:'ccc' ignoreCase:true |
|
5405 |
" |
|
5406 |
||
5407 |
"Modified: / 15.4.1997 / 15:50:33 / cg" |
|
5408 |
"Modified: / 30.1.1998 / 11:40:18 / stefan" |
|
5409 |
"Created: / 16.12.1999 / 01:21:35 / cg" |
|
5410 |
! |
|
5411 |
||
5412 |
findMatchString:matchString |
|
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5413 |
"like findString/indexOfSubCollection, but allowing GLOB match patterns. |
23791 | 5414 |
find matchstring; if found, return the index, if not, return 0. |
5415 |
This is a case sensitive match: lower/uppercase are considered different. |
|
12111 | 5416 |
|
5417 |
NOTICE: match-meta character interpretation is like in unix-matching, |
|
20874 | 5418 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5419 |
NOTICE: this GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 5420 |
NOTICE: the argument is the match pattern" |
5421 |
||
19033 | 5422 |
^ self findMatchString:matchString startingAt:1 caseSensitive:true ifAbsent:0 |
5423 |
||
5424 |
" |
|
5425 |
1234567890123 |
|
5426 |
'hello world bla foo baz' findMatchString:'b* ' |
|
5427 |
" |
|
23791 | 5428 |
|
5429 |
"Modified (comment): / 02-03-2019 / 12:21:07 / Claus Gittinger" |
|
12111 | 5430 |
! |
5431 |
||
5432 |
findMatchString:matchString startingAt:index |
|
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5433 |
"like findString, but allowing GLOB match patterns. |
23791 | 5434 |
find matchstring, starting at index; if found, return the index, if not, return 0. |
5435 |
This is a case sensitive match: lower/uppercase are considered different. |
|
12111 | 5436 |
|
5437 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20874 | 5438 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5439 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 5440 |
NOTICE: the argument is the match pattern" |
5441 |
||
19033 | 5442 |
^ self findMatchString:matchString startingAt:index caseSensitive:true ifAbsent:0 |
23791 | 5443 |
|
5444 |
"Modified (comment): / 02-03-2019 / 12:21:05 / Claus Gittinger" |
|
12111 | 5445 |
! |
5446 |
||
15734 | 5447 |
findMatchString:matchString startingAt:index caseSensitive:caseSensitive ifAbsent:exceptionBlock |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5448 |
"like findString, but allowing GLOB match patterns. |
12111 | 5449 |
find matchstring, starting at index. if found, return the index; |
5450 |
if not found, return the result of evaluating exceptionBlock. |
|
5451 |
This is a q&d hack - not very efficient. |
|
5452 |
||
5453 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20874 | 5454 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5455 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 5456 |
NOTICE: the argument is the match pattern" |
5457 |
||
5458 |
|firstChar firstSet |
|
5459 |
startIndex "{ Class: SmallInteger }" |
|
5460 |
matchSize "{ Class: SmallInteger }" |
|
5461 |
mySize "{ Class: SmallInteger }" |
|
5462 |
realMatchString lcChar ucChar| |
|
5463 |
||
5464 |
matchSize := matchString size. |
|
5465 |
matchSize == 0 ifTrue:[^ index]. "empty string matches" |
|
5466 |
||
5467 |
realMatchString := matchString. |
|
5468 |
(realMatchString endsWith:$*) ifFalse:[ |
|
20874 | 5469 |
realMatchString := realMatchString , '*'. |
5470 |
matchSize := matchSize + 1 |
|
12111 | 5471 |
]. |
5472 |
||
5473 |
mySize := self size. |
|
5474 |
firstChar := realMatchString at:1. |
|
5475 |
firstChar == self class matchEscapeCharacter ifTrue:[ |
|
20874 | 5476 |
firstChar := realMatchString at:2. |
12111 | 5477 |
]. |
5478 |
||
5479 |
firstChar asString includesMatchCharacters ifTrue:[ |
|
20874 | 5480 |
index to:mySize do:[:col | |
5481 |
(realMatchString match:self from:col to:mySize caseSensitive:caseSensitive) |
|
5482 |
ifTrue:[^ col] |
|
5483 |
]. |
|
5484 |
^ exceptionBlock value. |
|
12111 | 5485 |
]. |
5486 |
||
5487 |
lcChar := firstChar asLowercase. |
|
5488 |
ucChar := firstChar asUppercase. |
|
15734 | 5489 |
(caseSensitive not and:[ lcChar ~= ucChar ]) ifTrue:[ |
20874 | 5490 |
firstSet := Array with:ucChar with:lcChar. |
5491 |
startIndex := self indexOfAny:firstSet startingAt:index. |
|
12111 | 5492 |
] ifFalse:[ |
20874 | 5493 |
startIndex := self indexOf:firstChar startingAt:index. |
12111 | 5494 |
]. |
5495 |
[startIndex == 0] whileFalse:[ |
|
20874 | 5496 |
(realMatchString match:self from:startIndex to:mySize caseSensitive:caseSensitive) |
5497 |
ifTrue:[^ startIndex]. |
|
5498 |
||
5499 |
firstSet notNil ifTrue:[ |
|
5500 |
startIndex := self indexOfAny:firstSet startingAt:(startIndex + 1). |
|
5501 |
] ifFalse:[ |
|
5502 |
startIndex := self indexOf:firstChar startingAt:(startIndex + 1). |
|
5503 |
]. |
|
12111 | 5504 |
]. |
5505 |
^ exceptionBlock value |
|
5506 |
||
5507 |
" |
|
5508 |
'one two three four' findMatchString:'o[nu]' |
|
5509 |
'one two three four' findMatchString:'o[nu]' startingAt:3 |
|
15734 | 5510 |
'one two three four one' findMatchString:'ONE' startingAt:3 caseSensitive:false ifAbsent:0 |
5511 |
'one two three four one' findMatchString:'ONE' startingAt:3 caseSensitive:true ifAbsent:0 |
|
5512 |
'one two three four ONE' findMatchString:'O#E' startingAt:1 caseSensitive:false ifAbsent:0 |
|
5513 |
'one two three four ONE' findMatchString:'O#E' startingAt:1 caseSensitive:true ifAbsent:0 |
|
5514 |
12345678901234567890 |
|
5515 |
" |
|
5516 |
||
5517 |
"Modified: 13.9.1997 / 06:31:22 / cg" |
|
5518 |
! |
|
5519 |
||
5520 |
findMatchString:matchString startingAt:index ignoreCase:ignoreCase ifAbsent:exceptionBlock |
|
5521 |
<resource: #obsolete> |
|
5522 |
"like findString, but allowing GLOB match patterns. |
|
5523 |
find matchstring, starting at index. if found, return the index; |
|
5524 |
if not found, return the result of evaluating exceptionBlock. |
|
5525 |
This is a q&d hack - not very efficient. |
|
5526 |
||
5527 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20874 | 5528 |
NOT the ST-80 meaning. |
15734 | 5529 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
5530 |
NOTICE: the argument is the match pattern" |
|
5531 |
||
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
5532 |
^ self |
20874 | 5533 |
findMatchString:matchString startingAt:index caseSensitive:ignoreCase not ifAbsent:exceptionBlock |
15734 | 5534 |
|
5535 |
" |
|
5536 |
'one two three four' findMatchString:'o[nu]' |
|
5537 |
'one two three four' findMatchString:'o[nu]' startingAt:3 |
|
12111 | 5538 |
'one two three four one' findMatchString:'ONE' startingAt:3 ignoreCase:true ifAbsent:0 |
5539 |
" |
|
5540 |
||
5541 |
"Modified: 13.9.1997 / 06:31:22 / cg" |
|
5542 |
! |
|
5543 |
||
19746 | 5544 |
globPatternAsRegexPattern |
5545 |
"taking the receiver as a GLOB pattern, |
|
5546 |
return a corresponding regex pattern. |
|
5547 |
As regex does provide information about the matching substring, |
|
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
5548 |
it may be useful to apply a regex after a GLOB match, |
19746 | 5549 |
in order to highlight matching substrings (eg. in a CodeView after a search). |
5550 |
If it turns out to be better, we may convert all GLOB searches to regex and use it right away. |
|
5551 |
(currently, it is not sure, if GLOB is not better for most simple searches, as they are encountered |
|
5552 |
in typical real life)" |
|
5553 |
||
5554 |
^ self species streamContents:[:s | |
|
20874 | 5555 |
|addCharacter| |
5556 |
||
5557 |
addCharacter := |
|
5558 |
[:ch | |
|
5559 |
ch isLetterOrDigit ifFalse:[ |
|
5560 |
s nextPut:$\. |
|
5561 |
]. |
|
5562 |
s nextPut:ch |
|
5563 |
]. |
|
5564 |
||
23552 | 5565 |
(self class matchScanArrayFrom:self) do:[:matchEntry | |
20874 | 5566 |
matchEntry isCharacter ifTrue:[ |
5567 |
addCharacter value:matchEntry |
|
5568 |
] ifFalse:[ |
|
5569 |
matchEntry == #anyString ifTrue:[ |
|
5570 |
s nextPutAll:'.*' |
|
5571 |
] ifFalse:[ |
|
5572 |
matchEntry == #any ifTrue:[ |
|
5573 |
s nextPut:$. |
|
5574 |
] ifFalse:[ |
|
5575 |
matchEntry isString ifTrue:[ |
|
5576 |
|set min max| |
|
5577 |
||
5578 |
s nextPut:$[. |
|
23552 | 5579 |
set := matchEntry "copy sort". "/ already sorted |
20874 | 5580 |
min := set min. |
5581 |
max := set max. |
|
5582 |
set asSet = (min to:max) asSet ifTrue:[ |
|
5583 |
addCharacter value:min. |
|
5584 |
s nextPut:$-. |
|
5585 |
addCharacter value:max. |
|
5586 |
] ifFalse:[ |
|
5587 |
set do:addCharacter. |
|
5588 |
]. |
|
5589 |
s nextPut:$]. |
|
5590 |
] ifFalse:[ |
|
22700 | 5591 |
self halt:'funny match entry'. |
20874 | 5592 |
]. |
5593 |
]. |
|
5594 |
]. |
|
5595 |
] |
|
5596 |
]. |
|
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
5597 |
]. |
19746 | 5598 |
|
5599 |
" |
|
5600 |
'hello' globPatternAsRegexPattern |
|
5601 |
'hello*' globPatternAsRegexPattern |
|
5602 |
'*hello*' globPatternAsRegexPattern |
|
5603 |
'h###' globPatternAsRegexPattern |
|
5604 |
'h[0-9]' globPatternAsRegexPattern |
|
5605 |
'h[0-9][0-9][0-9]' globPatternAsRegexPattern |
|
5606 |
'h[0-9]*' globPatternAsRegexPattern |
|
19747 | 5607 |
'h[-+]*' globPatternAsRegexPattern |
23552 | 5608 |
'h[abc]*' globPatternAsRegexPattern |
5609 |
'h[0-9abc]*' globPatternAsRegexPattern |
|
19747 | 5610 |
'hello world' matches:'h*w' |
5611 |
'hello world' matchesRegex:('h*w' globPatternAsRegexPattern) |
|
5612 |
'hello world' matches:'h*d' |
|
5613 |
'hello world' matchesRegex:('h*d' globPatternAsRegexPattern) |
|
19746 | 5614 |
" |
23552 | 5615 |
|
5616 |
"Modified: / 03-12-2018 / 15:47:15 / Stefan Vogel" |
|
19746 | 5617 |
! |
5618 |
||
12111 | 5619 |
includesMatchString:matchString |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5620 |
"like includesString, but allowing GLOB match patterns. |
23791 | 5621 |
find matchstring; if found, return true, false otherwise. |
5622 |
This is a case sensitive match: lower/uppercase are considered different. |
|
12111 | 5623 |
|
5624 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20874 | 5625 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5626 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 5627 |
NOTICE: the argument is the match pattern" |
5628 |
||
5629 |
^ (self findMatchString:matchString) ~~ 0 |
|
5630 |
||
5631 |
" |
|
5632 |
'hello world' includesMatchString:'h*' |
|
5633 |
'hello world' includesMatchString:'h[aeiou]llo' |
|
5634 |
'hello world' includesMatchString:'wor*' |
|
5635 |
'hello world' includesMatchString:'woR*' |
|
23791 | 5636 |
'menue' includesMatchString:'[mM]enu[Ee]' |
5637 |
'blamenuebla' includesMatchString:'[mM]enu[Ee]' |
|
5638 |
'blaMenuebla' includesMatchString:'[mM]enu[Ee]' |
|
5639 |
" |
|
5640 |
||
5641 |
"Modified (comment): / 02-03-2019 / 12:20:58 / Claus Gittinger" |
|
12111 | 5642 |
! |
5643 |
||
5644 |
includesMatchString:matchString caseSensitive:caseSensitive |
|
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5645 |
"like includesString, but allowing GLOB match patterns. |
12111 | 5646 |
find matchstring; if found, return true, otherwise return false. |
5647 |
||
5648 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20874 | 5649 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5650 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 5651 |
NOTICE: the argument is the match pattern" |
5652 |
||
19033 | 5653 |
^ (self findMatchString:matchString startingAt:1 caseSensitive:caseSensitive ifAbsent:0) ~~ 0 |
12111 | 5654 |
|
5655 |
" |
|
5656 |
'hello world' includesMatchString:'h*' caseSensitive:true |
|
5657 |
'hello world' includesMatchString:'h*' caseSensitive:false |
|
5658 |
'Hello world' includesMatchString:'h*' caseSensitive:true |
|
5659 |
'Hello world' includesMatchString:'h*' caseSensitive:false |
|
5660 |
||
5661 |
'hello world' includesMatchString:'h[aeiou]llo' caseSensitive:true |
|
5662 |
'hello world' includesMatchString:'h[aeiou]llo' caseSensitive:false |
|
5663 |
||
5664 |
'hello world' includesMatchString:'wor*' caseSensitive:true |
|
5665 |
'hello world' includesMatchString:'wor*' caseSensitive:false |
|
5666 |
||
5667 |
'hello world' includesMatchString:'woR*' caseSensitive:true |
|
5668 |
'hello world' includesMatchString:'woR*' caseSensitive:false |
|
23791 | 5669 |
|
5670 |
'menue' includesMatchString:'[mM]enu[Ee]' |
|
5671 |
'menue' includesMatchString:'[mM]enu[Ee]' caseSensitive:true |
|
5672 |
'blamenuebla' includesMatchString:'[mM]enu[Ee]' |
|
5673 |
'blamenuebla' includesMatchString:'[mM]enu[Ee]' caseSensitive:true |
|
5674 |
||
5675 |
'blaMenuebla' includesMatchString:'[MS]enu[EF]' |
|
5676 |
'blaMenuebla' includesMatchString:'[MS]enu[EF]' caseSensitive:true |
|
5677 |
'blaMenuebla' includesMatchString:'[MS]enu[EF]' caseSensitive:false |
|
5678 |
" |
|
5679 |
||
5680 |
"Modified (comment): / 02-03-2019 / 12:18:57 / Claus Gittinger" |
|
12111 | 5681 |
! |
5682 |
||
5683 |
match:aString |
|
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5684 |
"return true if aString matches self, where self may contain GLOB meta-match |
12111 | 5685 |
characters $* (to match any string) or $# (to match any character). |
5686 |
or [...] to match a set of characters. |
|
23791 | 5687 |
This is a case sensitive match: lower/uppercase are considered different. |
12111 | 5688 |
The escape character is the backQuote. |
5689 |
||
5690 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20874 | 5691 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5692 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 5693 |
NOTICE: the receiver is the match pattern" |
5694 |
||
15734 | 5695 |
^ self match:aString from:1 to:aString size caseSensitive:true |
12111 | 5696 |
|
5697 |
" |
|
22137 | 5698 |
'#f#' match:'afc' |
5699 |
'#f#' match:'aec' |
|
5700 |
'#f#' match:'ae' |
|
5701 |
'#f#' match:'a' |
|
5702 |
'#f#' match:'f' |
|
5703 |
'#f#' match:'ff' |
|
5704 |
||
12111 | 5705 |
'\*f*' match:'f' |
5706 |
'\*f*' match:'*f' |
|
5707 |
'*\*f*' match:'*f' |
|
5708 |
'*f*' match:'*f' |
|
5709 |
'*ute*' match:'computer' |
|
5710 |
'*uter' match:'computer' |
|
5711 |
'uter*' match:'computer' |
|
5712 |
'*ute*' match:'' |
|
5713 |
'[abcd]*' match:'computer' |
|
5714 |
'[abcd]*' match:'komputer' |
|
5715 |
'*some*compl*ern*' match:'this is some more complicated pattern match' |
|
5716 |
'*some*compl*ern*' match:'this is another complicated pattern match' |
|
5717 |
'*-hh' match:'anton-h' |
|
5718 |
" |
|
5719 |
||
22137 | 5720 |
"Modified: / 09-06-1998 / 18:50:00 / cg" |
5721 |
"Modified (comment): / 29-07-2017 / 14:00:23 / cg" |
|
23791 | 5722 |
"Modified (comment): / 02-03-2019 / 12:20:48 / Claus Gittinger" |
12111 | 5723 |
! |
5724 |
||
15734 | 5725 |
match:aString caseSensitive:caseSensitive |
5726 |
"return true if aString matches self, where self may contain GLOB meta-match |
|
5727 |
characters $* (to match any string) or $# (to match any character) |
|
5728 |
or [...] to match a set of characters. |
|
5729 |
If caseSensitive is false, lower/uppercase are considered the same. |
|
5730 |
The escape character is the backQuote. |
|
5731 |
||
5732 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20874 | 5733 |
NOT the ST-80 meaning. |
15734 | 5734 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
5735 |
NOTICE: the receiver is the match pattern" |
|
5736 |
||
5737 |
^ self match:aString from:1 to:aString size caseSensitive:caseSensitive |
|
5738 |
||
5739 |
" |
|
5740 |
'*ute*' match:'COMPUTER' caseSensitive:false |
|
5741 |
'*uter' match:'COMPUTER' caseSensitive:true |
|
5742 |
'[abcd]*' match:'computer' caseSensitive:true |
|
5743 |
'[abcd]*' match:'Computer' caseSensitive:true |
|
5744 |
'[a-k]*' match:'komputer' caseSensitive:true |
|
5745 |
'[a-k]*' match:'zomputer' caseSensitive:true |
|
5746 |
'[a-k]*' match:'Komputer' caseSensitive:true |
|
5747 |
'[a-k]*' match:'Komputer' caseSensitive:false |
|
5748 |
'*some*compl*ern*' match:'this is some more complicated pattern match' caseSensitive:false |
|
5749 |
'*some*compl*ern*' match:'this is another complicated pattern match' caseSensitive:false |
|
5750 |
||
5751 |
Time millisecondsToRun:[ |
|
20874 | 5752 |
Symbol allInstancesDo:[:sym | |
5753 |
'[ab]*' match:sym caseSensitive:true |
|
5754 |
] |
|
15734 | 5755 |
]. |
5756 |
Time millisecondsToRun:[ |
|
20874 | 5757 |
Symbol allInstancesDo:[:sym | |
5758 |
'*at:*' match:sym caseSensitive:true |
|
5759 |
] |
|
15734 | 5760 |
]. |
5761 |
" |
|
5762 |
||
5763 |
"Modified: 2.4.1997 / 17:28:58 / cg" |
|
5764 |
! |
|
5765 |
||
5766 |
match:aString caseSensitive:caseSensitive escapeCharacter:escape |
|
5767 |
"return true if aString matches self, where self may contain GLOB meta-match |
|
5768 |
characters $* (to match any string) or $# (to match any character) |
|
5769 |
or [...] to match a set of characters. |
|
5770 |
If caseSensitive is false, lower/uppercase are considered the same. |
|
5771 |
||
5772 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20874 | 5773 |
NOT the ST-80 meaning. |
15734 | 5774 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
5775 |
NOTICE: the receiver is the match pattern" |
|
5776 |
||
5777 |
^ self match:aString from:1 to:aString size caseSensitive:caseSensitive escapeCharacter:escape |
|
5778 |
||
5779 |
" |
|
5780 |
'*ute*' match:'COMPUTER' caseSensitive:false |
|
5781 |
'*uter' match:'COMPUTER' caseSensitive:true |
|
5782 |
'[abcd]*' match:'computer' caseSensitive:true |
|
5783 |
'[abcd]*' match:'Computer' caseSensitive:true |
|
5784 |
'[a-k]*' match:'komputer' caseSensitive:true |
|
5785 |
'[a-k]*' match:'zomputer' caseSensitive:true |
|
5786 |
'[a-k]*' match:'Komputer' caseSensitive:true |
|
5787 |
'[a-k]*' match:'Komputer' caseSensitive:false |
|
5788 |
'*some*compl*ern*' match:'this is some more complicated pattern match' caseSensitive:false |
|
5789 |
'*some*compl*ern*' match:'this is another complicated pattern match' caseSensitive:false |
|
5790 |
||
5791 |
Time millisecondsToRun:[ |
|
20874 | 5792 |
Symbol allInstancesDo:[:sym | |
5793 |
'[ab]*' match:sym caseSensitive:true |
|
5794 |
] |
|
15734 | 5795 |
]. |
5796 |
Time millisecondsToRun:[ |
|
20874 | 5797 |
Symbol allInstancesDo:[:sym | |
5798 |
'*at:*' match:sym caseSensitive:true |
|
5799 |
] |
|
15734 | 5800 |
]. |
5801 |
" |
|
5802 |
||
5803 |
"Modified: 2.4.1997 / 17:28:58 / cg" |
|
5804 |
! |
|
5805 |
||
12111 | 5806 |
match:aString escapeCharacter:escape |
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. |
|
5810 |
Lower/uppercase are considered different. |
|
5811 |
||
5812 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20874 | 5813 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5814 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 5815 |
NOTICE: the receiver is the match pattern" |
5816 |
||
19033 | 5817 |
^ self match:aString from:1 to:aString size caseSensitive:true escapeCharacter:escape |
12111 | 5818 |
|
5819 |
" |
|
5820 |
'a\b\c\*' match:'a\b\c\d' |
|
5821 |
'a\b\c\*' match:'a\b\c\d' escapeCharacter:nil |
|
5822 |
" |
|
5823 |
! |
|
5824 |
||
15734 | 5825 |
match:aString from:start to:stop caseSensitive:caseSensitive |
5826 |
"return true if part of aString matches myself, |
|
5827 |
where self may contain GLOB meta-match |
|
5828 |
characters $* (to match any string) or $# (to match any character) |
|
5829 |
or [...] to match a set of characters. |
|
5830 |
If caseSensitive is false, lower/uppercase are considered the same. |
|
5831 |
The escape character is the backQuote. |
|
5832 |
||
5833 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20874 | 5834 |
NOT the ST-80 meaning. |
15734 | 5835 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
5836 |
NOTICE: the receiver is the match pattern" |
|
5837 |
||
5838 |
^ self |
|
20874 | 5839 |
match:aString from:start to:stop caseSensitive:caseSensitive |
5840 |
escapeCharacter:(self class matchEscapeCharacter) |
|
15734 | 5841 |
|
5842 |
" |
|
5843 |
'*ute*' match:'12345COMPUTER' from:1 to:5 caseSensitive:false |
|
5844 |
'*ute*' match:'12345COMPUTER' from:6 to:13 caseSensitive:false |
|
5845 |
" |
|
5846 |
||
5847 |
"Modified: / 10.11.1998 / 21:43:46 / cg" |
|
5848 |
! |
|
5849 |
||
5850 |
match:aString from:start to:stop caseSensitive:caseSensitive escapeCharacter:escape |
|
5851 |
"return true if part of aString matches myself, |
|
5852 |
where self may contain GLOB meta-match |
|
5853 |
characters $* (to match any string) or $# (to match any character) |
|
5854 |
or [...] to match a set of characters. |
|
5855 |
If caseSensitive is false, lower/uppercase are considered the same. |
|
5856 |
||
5857 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20667 | 5858 |
NOT the ST-80 meaning. |
15734 | 5859 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
5860 |
NOTICE: the receiver is the match pattern" |
|
5861 |
||
5862 |
|matchScanArray| |
|
5863 |
||
5864 |
" |
|
22137 | 5865 |
keep the matchScanArray from the most recent matches - |
5866 |
avoids parsing the pattern over-and over again, |
|
5867 |
if multiple searches are done with the same pattern. |
|
5868 |
" |
|
20667 | 5869 |
(PreviousMatches isNil |
5870 |
or:[(matchScanArray := PreviousMatches at: self ifAbsent:[nil]) isNil]) ifTrue:[ |
|
5871 |
matchScanArray := self class matchScanArrayFrom:self escapeCharacter:escape. |
|
5872 |
matchScanArray isNil ifTrue:[ |
|
23459 | 5873 |
ParseWarning raiseRequestErrorString:('CharacterArray [info]: invalid matchpattern: ''%1'' comparing for equality' bindWith:self). |
20667 | 5874 |
^ self = aString |
23459 | 5875 |
"/ ^ false |
20667 | 5876 |
]. |
5877 |
PreviousMatches isNil ifTrue:[ |
|
22137 | 5878 |
PreviousMatches := CacheDictionary new:15 |
20667 | 5879 |
]. |
5880 |
PreviousMatches at:self put:matchScanArray. |
|
15734 | 5881 |
]. |
5882 |
||
5883 |
^ self class |
|
20667 | 5884 |
matchScan:matchScanArray |
5885 |
from:1 to:matchScanArray size |
|
5886 |
with:aString |
|
5887 |
from:start to:stop |
|
5888 |
caseSensitive:caseSensitive |
|
15734 | 5889 |
|
5890 |
" |
|
5891 |
'*ute*' match:'12345COMPUTER' from:1 to:5 caseSensitive:false |
|
5892 |
'*ute*' match:'12345COMPUTER' from:6 to:13 caseSensitive:false |
|
23459 | 5893 |
|
5894 |
'*[12' match:'12345COMPUTER' from:6 to:13 caseSensitive:false -- gives a warning |
|
15734 | 5895 |
" |
5896 |
||
22137 | 5897 |
"Modified: / 29-07-2017 / 14:01:42 / cg" |
23459 | 5898 |
"Modified (comment): / 24-10-2018 / 09:01:31 / Claus Gittinger" |
15734 | 5899 |
! |
5900 |
||
12111 | 5901 |
match:aString from:start to:stop ignoreCase:ignoreCase |
15734 | 5902 |
<resource: #obsolete> |
12111 | 5903 |
"return true if part of aString matches myself, |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5904 |
where self may contain GLOB meta-match |
12111 | 5905 |
characters $* (to match any string) or $# (to match any character) |
5906 |
or [...] to match a set of characters. |
|
5907 |
If ignoreCase is true, lower/uppercase are considered the same. |
|
5908 |
The escape character is the backQuote. |
|
5909 |
||
5910 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20874 | 5911 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5912 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 5913 |
NOTICE: the receiver is the match pattern" |
5914 |
||
5915 |
^ self |
|
20874 | 5916 |
match:aString from:start to:stop caseSensitive:ignoreCase not |
5917 |
escapeCharacter:(self class matchEscapeCharacter) |
|
12111 | 5918 |
|
5919 |
" |
|
5920 |
'*ute*' match:'12345COMPUTER' from:1 to:5 ignoreCase:true |
|
5921 |
'*ute*' match:'12345COMPUTER' from:6 to:13 ignoreCase:true |
|
5922 |
" |
|
5923 |
||
5924 |
"Modified: / 10.11.1998 / 21:43:46 / cg" |
|
5925 |
! |
|
5926 |
||
5927 |
match:aString from:start to:stop ignoreCase:ignoreCase escapeCharacter:escape |
|
15734 | 5928 |
<resource: #obsolete> |
12111 | 5929 |
"return true if part of aString matches myself, |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5930 |
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 |
If ignoreCase is true, lower/uppercase are considered the same. |
|
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 |
||
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
5940 |
^ self |
20874 | 5941 |
match:aString from:start to:stop |
5942 |
caseSensitive:ignoreCase not |
|
5943 |
escapeCharacter:escape |
|
12111 | 5944 |
|
5945 |
" |
|
5946 |
'*ute*' match:'12345COMPUTER' from:1 to:5 ignoreCase:true |
|
5947 |
'*ute*' match:'12345COMPUTER' from:6 to:13 ignoreCase:true |
|
5948 |
" |
|
5949 |
||
5950 |
"Modified: / 10.11.1998 / 21:43:46 / cg" |
|
5951 |
! |
|
5952 |
||
5953 |
match:aString ignoreCase:ignoreCase |
|
15734 | 5954 |
<resource: #obsolete> |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5955 |
"return true if aString matches self, where self may contain GLOB meta-match |
12111 | 5956 |
characters $* (to match any string) or $# (to match any character) |
5957 |
or [...] to match a set of characters. |
|
5958 |
If ignoreCase is true, lower/uppercase are considered the same. |
|
5959 |
The escape character is the backQuote. |
|
5960 |
||
5961 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20874 | 5962 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5963 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 5964 |
NOTICE: the receiver is the match pattern" |
5965 |
||
15734 | 5966 |
"/ leave it this way, in case a customer has redefined it for performance (for a while) |
19033 | 5967 |
^ self match:aString from:1 to:aString size caseSensitive:ignoreCase not |
12111 | 5968 |
|
5969 |
" |
|
5970 |
'*ute*' match:'COMPUTER' ignoreCase:true |
|
5971 |
'*uter' match:'COMPUTER' ignoreCase:false |
|
5972 |
'[abcd]*' match:'computer' ignoreCase:false |
|
5973 |
'[abcd]*' match:'Computer' ignoreCase:false |
|
5974 |
'[a-k]*' match:'komputer' ignoreCase:false |
|
5975 |
'[a-k]*' match:'zomputer' ignoreCase:false |
|
5976 |
'[a-k]*' match:'Komputer' ignoreCase:false |
|
5977 |
'[a-k]*' match:'Komputer' ignoreCase:true |
|
5978 |
'*some*compl*ern*' match:'this is some more complicated pattern match' ignoreCase:true |
|
5979 |
'*some*compl*ern*' match:'this is another complicated pattern match' ignoreCase:true |
|
5980 |
||
5981 |
Time millisecondsToRun:[ |
|
20874 | 5982 |
Symbol allInstancesDo:[:sym | |
5983 |
'[ab]*' match:sym ignoreCase:false |
|
5984 |
] |
|
12111 | 5985 |
]. |
5986 |
Time millisecondsToRun:[ |
|
20874 | 5987 |
Symbol allInstancesDo:[:sym | |
5988 |
'*at:*' match:sym ignoreCase:false |
|
5989 |
] |
|
12111 | 5990 |
]. |
5991 |
" |
|
5992 |
||
5993 |
"Modified: 2.4.1997 / 17:28:58 / cg" |
|
5994 |
! |
|
5995 |
||
5996 |
match:aString ignoreCase:ignoreCase escapeCharacter:escape |
|
15734 | 5997 |
<resource: #obsolete> |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5998 |
"return true if aString matches self, where self may contain GLOB meta-match |
12111 | 5999 |
characters $* (to match any string) or $# (to match any character) |
6000 |
or [...] to match a set of characters. |
|
6001 |
If ignoreCase is true, lower/uppercase are considered the same. |
|
6002 |
||
6003 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20874 | 6004 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
6005 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 6006 |
NOTICE: the receiver is the match pattern" |
6007 |
||
19033 | 6008 |
^ self match:aString from:1 to:aString size caseSensitive:ignoreCase not escapeCharacter:escape |
12111 | 6009 |
|
6010 |
" |
|
6011 |
'*ute*' match:'COMPUTER' ignoreCase:true |
|
6012 |
'*uter' match:'COMPUTER' ignoreCase:false |
|
6013 |
'[abcd]*' match:'computer' ignoreCase:false |
|
6014 |
'[abcd]*' match:'Computer' ignoreCase:false |
|
6015 |
'[a-k]*' match:'komputer' ignoreCase:false |
|
6016 |
'[a-k]*' match:'zomputer' ignoreCase:false |
|
6017 |
'[a-k]*' match:'Komputer' ignoreCase:false |
|
6018 |
'[a-k]*' match:'Komputer' ignoreCase:true |
|
6019 |
'*some*compl*ern*' match:'this is some more complicated pattern match' ignoreCase:true |
|
6020 |
'*some*compl*ern*' match:'this is another complicated pattern match' ignoreCase:true |
|
6021 |
||
6022 |
Time millisecondsToRun:[ |
|
20874 | 6023 |
Symbol allInstancesDo:[:sym | |
6024 |
'[ab]*' match:sym ignoreCase:false |
|
6025 |
] |
|
12111 | 6026 |
]. |
6027 |
Time millisecondsToRun:[ |
|
20874 | 6028 |
Symbol allInstancesDo:[:sym | |
6029 |
'*at:*' match:sym ignoreCase:false |
|
6030 |
] |
|
12111 | 6031 |
]. |
6032 |
" |
|
6033 |
||
6034 |
"Modified: 2.4.1997 / 17:28:58 / cg" |
|
6035 |
! |
|
6036 |
||
23287 | 6037 |
matches:aGlobPatternString |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
6038 |
"return true if the receiver matches aString, where aPatternString may contain GLOB meta-match |
12111 | 6039 |
characters $* (to match any string) or $# (to match any character). |
6040 |
or [...] to match a set of characters. |
|
6041 |
Lower/uppercase are considered different. |
|
6042 |
||
6043 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20874 | 6044 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
6045 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 6046 |
NOTICE: the receiver is the match pattern" |
6047 |
||
23287 | 6048 |
^ aGlobPatternString match:self |
6049 |
||
6050 |
"Modified (format): / 18-08-2018 / 20:51:45 / Claus Gittinger" |
|
14054
48d3fab77bd9
added: #matches:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
14053
diff
changeset
|
6051 |
! |
48d3fab77bd9
added: #matches:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
14053
diff
changeset
|
6052 |
|
15734 | 6053 |
matches:aPatternString caseSensitive:caseSensitive |
6054 |
"return true if the receiver matches aString, where aPatternString may contain GLOB meta-match |
|
6055 |
characters $* (to match any string) or $# (to match any character). |
|
6056 |
or [...] to match a set of characters. |
|
6057 |
||
6058 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20874 | 6059 |
NOT the ST-80 meaning. |
15734 | 6060 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
6061 |
NOTICE: the receiver is the match pattern" |
|
6062 |
||
6063 |
^ aPatternString match:self caseSensitive:caseSensitive |
|
6064 |
! |
|
6065 |
||
14054
48d3fab77bd9
added: #matches:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
14053
diff
changeset
|
6066 |
matches:aPatternString ignoreCase:ignoreCase |
15734 | 6067 |
<resource: #obsolete> |
14054
48d3fab77bd9
added: #matches:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
14053
diff
changeset
|
6068 |
"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
|
6069 |
characters $* (to match any string) or $# (to match any character). |
48d3fab77bd9
added: #matches:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
14053
diff
changeset
|
6070 |
or [...] to match a set of characters. |
48d3fab77bd9
added: #matches:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
14053
diff
changeset
|
6071 |
Lower/uppercase are considered different. |
48d3fab77bd9
added: #matches:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
14053
diff
changeset
|
6072 |
|
48d3fab77bd9
added: #matches:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
14053
diff
changeset
|
6073 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
20874 | 6074 |
NOT the ST-80 meaning. |
14054
48d3fab77bd9
added: #matches:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
14053
diff
changeset
|
6075 |
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
|
6076 |
NOTICE: the receiver is the match pattern" |
48d3fab77bd9
added: #matches:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
14053
diff
changeset
|
6077 |
|
15734 | 6078 |
^ aPatternString match:self caseSensitive:ignoreCase not |
14054
48d3fab77bd9
added: #matches:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
14053
diff
changeset
|
6079 |
|
48d3fab77bd9
added: #matches:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
14053
diff
changeset
|
6080 |
"Created: / 08-03-2012 / 03:11:11 / cg" |
12111 | 6081 |
! ! |
6082 |
||
6083 |
||
24033 | 6084 |
|
9192 | 6085 |
!CharacterArray methodsFor:'padded copying'! |
6086 |
||
6087 |
centerPaddedTo:newSize |
|
19464 | 6088 |
"return a new string consisting of the receiver's characters, |
6089 |
plus spaces up to length and center the receiver's characters in |
|
6090 |
the resulting string. |
|
6091 |
If the receiver's size is equal or greater than the length argument, |
|
6092 |
the original receiver is returned unchanged." |
|
9192 | 6093 |
|
6094 |
^ self centerPaddedTo:newSize with:(Character space) |
|
6095 |
||
6096 |
" |
|
6097 |
'foo' centerPaddedTo:10 |
|
6098 |
123 printString centerPaddedTo:10 |
|
6099 |
" |
|
6100 |
||
6101 |
"Created: 25.11.1995 / 10:53:57 / cg" |
|
6102 |
! |
|
9190 | 6103 |
|
6104 |
centerPaddedTo:size with:padCharacter |
|
6105 |
"return a new string of length size, which contains the receiver |
|
6106 |
centered (i.e. padded on both sides). |
|
6107 |
Characters are filled with padCharacter. |
|
19464 | 6108 |
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
|
6109 |
the original receiver is returned unchanged." |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
6110 |
|
9190 | 6111 |
|len s| |
6112 |
||
6113 |
len := self size. |
|
9192 | 6114 |
(len < size) ifTrue:[ |
20874 | 6115 |
s := self species new:size withAll:padCharacter. |
6116 |
s replaceFrom:(size - len) // 2 + 1 with:self. |
|
6117 |
^ s |
|
9192 | 6118 |
] |
6119 |
||
9190 | 6120 |
" |
6121 |
'foo' centerPaddedTo:11 with:$. |
|
6122 |
'fooBar' centerPaddedTo:5 with:$. |
|
6123 |
123 printString centerPaddedTo:10 with:$. |
|
6124 |
(' ' , 123 printString) centerPaddedTo:10 with:$. |
|
6125 |
(Float pi printString) centerPaddedTo:15 with:(Character space) |
|
6126 |
(Float pi printString) centerPaddedTo:15 with:$- |
|
6127 |
(' ' , Float pi class name) centerPaddedTo:15 with:$. |
|
6128 |
" |
|
815
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
6129 |
! |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
6130 |
|
9190 | 6131 |
decimalPaddedTo:size and:afterPeriod at:decimalCharacter |
815
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
6132 |
"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
|
6133 |
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
|
6134 |
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
|
6135 |
to be padded). |
19464 | 6136 |
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
|
6137 |
the original receiver is returned unchanged. |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
6138 |
(sounds complicated ? -> see examples below)." |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
6139 |
|
8874 | 6140 |
^ self |
20874 | 6141 |
decimalPaddedTo:size |
6142 |
and:afterPeriod |
|
6143 |
at:decimalCharacter |
|
6144 |
withLeft:(Character space) |
|
6145 |
right:$0 |
|
815
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
6146 |
|
8874 | 6147 |
" |
6148 |
'123' decimalPaddedTo:10 and:3 at:$. -> ' 123 ' |
|
6149 |
'123' decimalPaddedTo:10 and:3 at:$. -> ' 123.000' |
|
6150 |
'123.' decimalPaddedTo:10 and:3 at:$. -> ' 123.000' |
|
815
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
6151 |
'123.1' decimalPaddedTo:10 and:3 at:$. -> ' 123.100' |
8874 | 6152 |
'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
|
6153 |
'123.123' decimalPaddedTo:10 and:3 at:$. -> ' 123.123' |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
6154 |
" |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
6155 |
|
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
6156 |
"Created: 23.12.1995 / 13:11:52 / cg" |
9192 | 6157 |
! |
6158 |
||
6159 |
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
|
6160 |
"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
|
6161 |
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
|
6162 |
of the period. |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
6163 |
Characters on the left are filled with leftPadChar. |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
6164 |
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
|
6165 |
otherwise, if missing, a decimal point is added and right characters filled with this. |
19464 | 6166 |
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
|
6167 |
the original receiver is returned unchanged. |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
6168 |
(sounds complicated ? -> see examples below)." |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
6169 |
|
5230
655c21ffe6cc
care for negative size of rest-string when decimalPadding
Claus Gittinger <cg@exept.de>
parents:
5221
diff
changeset
|
6170 |
|s idx n rest| |
815
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
6171 |
|
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
6172 |
idx := self indexOf:decimalCharacter. |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
6173 |
idx == 0 ifTrue:[ |
20874 | 6174 |
"/ |
6175 |
"/ no decimal point found; adjust string to the left of the period column |
|
6176 |
"/ |
|
6177 |
rightPadChar isNil ifTrue:[ |
|
6178 |
s := self , (self species new:afterPeriod + 1 withAll:leftPadChar) |
|
6179 |
] ifFalse:[ |
|
6180 |
s:= self , decimalCharacter asString , (self species new:afterPeriod withAll:rightPadChar). |
|
6181 |
]. |
|
815
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
6182 |
] ifFalse:[ |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
6183 |
|
20874 | 6184 |
"/ the number of after-decimalPoint characters |
6185 |
n := self size - idx. |
|
6186 |
rest := afterPeriod - n. |
|
6187 |
rest > 0 ifTrue:[ |
|
6188 |
s := (self species new:rest withAll:(rightPadChar ? leftPadChar)). |
|
6189 |
] ifFalse:[ |
|
6190 |
s := '' |
|
6191 |
]. |
|
6192 |
s := self , s. |
|
815
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
6193 |
]. |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
6194 |
|
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
6195 |
^ s leftPaddedTo:size with:leftPadChar |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
6196 |
|
8874 | 6197 |
" |
6198 |
'123' decimalPaddedTo:10 and:3 at:$. withLeft:(Character space) right:nil -> ' 123 ' |
|
6199 |
'123' decimalPaddedTo:10 and:3 at:$. withLeft:(Character space) right:$0 -> ' 123.000' |
|
6200 |
'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
|
6201 |
'123.1' decimalPaddedTo:10 and:3 at:$. withLeft:(Character space) right:$0 -> ' 123.100' |
8874 | 6202 |
'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
|
6203 |
'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
|
6204 |
" |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
6205 |
|
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
6206 |
"Modified: 23.12.1995 / 13:08:18 / cg" |
9192 | 6207 |
! |
6208 |
||
6209 |
leftPaddedTo:size |
|
6210 |
"return a new string of length size, which contains the receiver |
|
6211 |
right-adjusted (i.e. padded on the left). |
|
6212 |
Characters on the left are filled with spaces. |
|
19464 | 6213 |
If the receiver's size is equal or greater than the length argument, |
9192 | 6214 |
the original receiver is returned unchanged." |
6215 |
||
6216 |
^ self leftPaddedTo:size with:(Character space) |
|
6217 |
||
6218 |
" |
|
6219 |
'foo' leftPaddedTo:10 |
|
6220 |
'fooBar' leftPaddedTo:5 |
|
6221 |
123 printString leftPaddedTo:10 |
|
6222 |
" |
|
6223 |
! |
|
608 | 6224 |
|
6225 |
paddedTo:newSize |
|
19753 | 6226 |
"return a new string consisting of the receiver's characters, |
23945 | 6227 |
plus spaces up to length. |
6228 |
If the receiver's size is equal or greater than the length argument, |
|
6229 |
the original receiver is returned unchanged." |
|
608 | 6230 |
|
6231 |
^ self paddedTo:newSize with:(Character space) |
|
6232 |
||
6233 |
" |
|
8874 | 6234 |
'foo' paddedTo:10 |
6235 |
123 printString paddedTo:10 |
|
15734 | 6236 |
'12345678901234' paddedTo:10 |
608 | 6237 |
" |
23945 | 6238 |
|
6239 |
"Modified (format): / 21-03-2019 / 12:56:24 / Claus Gittinger" |
|
64 | 6240 |
! ! |
6241 |
||
9192 | 6242 |
!CharacterArray methodsFor:'printing & storing'! |
608 | 6243 |
|
6244 |
article |
|
17137 | 6245 |
"return an article string for the receiver. |
6246 |
The original code only looked at the first character being a vowel; |
|
6247 |
this has been enhanced by some heuristics - not perfect, still." |
|
608 | 6248 |
|
17241 | 6249 |
|firstChar secondChar thirdChar| |
608 | 6250 |
|
8874 | 6251 |
firstChar := (self at:1) asLowercase. |
24041 | 6252 |
((firstChar isVowel and:[firstChar ~~ $u]) |
6253 |
or:[firstChar == $x] |
|
6254 |
) ifTrue:[ |
|
20874 | 6255 |
^ 'an' |
608 | 6256 |
]. |
17043 | 6257 |
|
6258 |
(self size >= 3) ifTrue:[ |
|
20874 | 6259 |
secondChar := (self at:2) asLowercase. |
6260 |
||
6261 |
"/ may need more here... |
|
24041 | 6262 |
(firstChar == $r |
6263 |
and:[secondChar == $b] |
|
6264 |
) ifTrue:[ |
|
20874 | 6265 |
^ 'an' |
6266 |
]. |
|
6267 |
||
6268 |
thirdChar := (self at:3) asLowercase. |
|
6269 |
||
24041 | 6270 |
(firstChar == $s |
6271 |
and:[secondChar == $c |
|
6272 |
and:[thirdChar == $h]] |
|
6273 |
) ifTrue:[ |
|
6274 |
^ 'a' |
|
6275 |
]. |
|
6276 |
||
20874 | 6277 |
(firstChar isVowel not |
6278 |
and:[(secondChar isVowel or:[secondChar == $y]) not |
|
6279 |
and:[thirdChar isVowel not ]]) ifTrue:[ |
|
6280 |
"/ exceptions: 3 non-vowels in a row: looks like an abbreviation |
|
6281 |
(self size > 4) ifTrue:[ |
|
6282 |
(firstChar == $s) ifTrue:[ |
|
6283 |
((secondChar == $c and:[thirdChar == $r]) |
|
6284 |
or:[ (secondChar == $t and:[thirdChar == $r]) ]) ifTrue:[ |
|
6285 |
(self at:4) isVowel ifTrue:[ |
|
6286 |
^ 'a' |
|
6287 |
] |
|
6288 |
] |
|
6289 |
]. |
|
6290 |
]. |
|
6291 |
"/ an abbreviation; treat x, s as vowels |
|
6292 |
(firstChar == $x or:[ firstChar == $s ]) ifTrue:[^ 'an']. |
|
6293 |
] |
|
17043 | 6294 |
]. |
608 | 6295 |
^ 'a' |
5848 | 6296 |
|
6297 |
" |
|
24041 | 6298 |
'uboot' article. |
6299 |
'xmas' article. |
|
6300 |
'alarm' article. |
|
6301 |
'baby' article. |
|
6302 |
'sql' article. |
|
6303 |
'scr' article. |
|
6304 |
'screen' article. |
|
6305 |
'scrollbar' article. |
|
6306 |
'scrs' article. |
|
6307 |
'cvs' article. |
|
6308 |
'cvssource' article. |
|
6309 |
'symbol' article. |
|
6310 |
'string' article. |
|
6311 |
'rbparser' article. |
|
6312 |
'scheme' article. |
|
19683 | 6313 |
" |
6314 |
||
6315 |
"Modified (comment): / 01-05-2016 / 10:57:25 / cg" |
|
24041 | 6316 |
"Modified (comment): / 03-04-2019 / 10:08:57 / Claus Gittinger" |
608 | 6317 |
! |
6318 |
||
8049
ccd0e8f26439
printing - migrating towards unicodeSupport in ST-code
Claus Gittinger <cg@exept.de>
parents:
8040
diff
changeset
|
6319 |
basicStoreString |
ccd0e8f26439
printing - migrating towards unicodeSupport in ST-code
Claus Gittinger <cg@exept.de>
parents:
8040
diff
changeset
|
6320 |
"return a String for storing myself" |
ccd0e8f26439
printing - migrating towards unicodeSupport in ST-code
Claus Gittinger <cg@exept.de>
parents:
8040
diff
changeset
|
6321 |
|
ccd0e8f26439
printing - migrating towards unicodeSupport in ST-code
Claus Gittinger <cg@exept.de>
parents:
8040
diff
changeset
|
6322 |
|s n index| |
ccd0e8f26439
printing - migrating towards unicodeSupport in ST-code
Claus Gittinger <cg@exept.de>
parents:
8040
diff
changeset
|
6323 |
|
ccd0e8f26439
printing - migrating towards unicodeSupport in ST-code
Claus Gittinger <cg@exept.de>
parents:
8040
diff
changeset
|
6324 |
n := self occurrencesOf:$'. |
11320 | 6325 |
n ~~ 0 ifTrue:[ |
20874 | 6326 |
s := self species new:(n + 2 + self size). |
6327 |
s at:1 put:$'. |
|
6328 |
index := 2. |
|
6329 |
self do:[:thisChar | |
|
6330 |
(thisChar == $') ifTrue:[ |
|
6331 |
s at:index put:thisChar. |
|
6332 |
index := index + 1. |
|
6333 |
]. |
|
6334 |
s at:index put:thisChar. |
|
6335 |
index := index + 1. |
|
6336 |
]. |
|
6337 |
s at:index put:$'. |
|
6338 |
^ s |
|
9192 | 6339 |
]. |
11320 | 6340 |
|
9192 | 6341 |
^ '''' , self , '''' |
15523 | 6342 |
|
6343 |
" |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
6344 |
'''immutable'' string' asImmutableString basicStoreString |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
6345 |
'immutable string' asImmutableString basicStoreString |
15523 | 6346 |
" |
6347 |
||
6348 |
"Modified: / 14-07-2013 / 19:20:49 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
9192 | 6349 |
! |
8049
ccd0e8f26439
printing - migrating towards unicodeSupport in ST-code
Claus Gittinger <cg@exept.de>
parents:
8040
diff
changeset
|
6350 |
|
11774
6f6e2b71b607
displayOn (inspector's interface) was broken
Claus Gittinger <cg@exept.de>
parents:
11754
diff
changeset
|
6351 |
displayOn:aGCOrStream |
6f6e2b71b607
displayOn (inspector's interface) was broken
Claus Gittinger <cg@exept.de>
parents:
11754
diff
changeset
|
6352 |
"Compatibility |
6f6e2b71b607
displayOn (inspector's interface) was broken
Claus Gittinger <cg@exept.de>
parents:
11754
diff
changeset
|
6353 |
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
|
6354 |
OR: |
6f6e2b71b607
displayOn (inspector's interface) was broken
Claus Gittinger <cg@exept.de>
parents:
11754
diff
changeset
|
6355 |
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
|
6356 |
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
|
6357 |
(although the fallBack is to display its printString ...)" |
6f6e2b71b607
displayOn (inspector's interface) was broken
Claus Gittinger <cg@exept.de>
parents:
11754
diff
changeset
|
6358 |
|
6f6e2b71b607
displayOn (inspector's interface) was broken
Claus Gittinger <cg@exept.de>
parents:
11754
diff
changeset
|
6359 |
"/ what a kludge - Dolphin and Squeak mean: printOn: a stream; |
21540 | 6360 |
"/ old ST80 means: draw-yourself on a GC. |
6361 |
aGCOrStream isStream ifFalse:[ |
|
6362 |
^ super displayOn:aGCOrStream. |
|
6363 |
]. |
|
6364 |
||
6365 |
self storeOn:aGCOrStream. |
|
6366 |
||
6367 |
"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
|
6368 |
! |
6f6e2b71b607
displayOn (inspector's interface) was broken
Claus Gittinger <cg@exept.de>
parents:
11754
diff
changeset
|
6369 |
|
608 | 6370 |
printOn:aStream |
6371 |
"print the receiver on aStream" |
|
6372 |
||
6373 |
aStream nextPutAll:self |
|
6374 |
! |
|
6375 |
||
6376 |
printString |
|
13079
111e7e456648
Implement display with #displayOn: instead of #displayString
Stefan Vogel <sv@exept.de>
parents:
13073
diff
changeset
|
6377 |
"return a string for printing - that's myself" |
608 | 6378 |
|
6379 |
^ self |
|
3578 | 6380 |
! |
6381 |
||
6382 |
printWithQuotesDoubledOn:aStream |
|
6383 |
"put the raw storeString of myself on aStream" |
|
6384 |
||
6385 |
self do:[:thisChar | |
|
20874 | 6386 |
(thisChar == $') ifTrue:[aStream nextPut:thisChar]. |
6387 |
aStream nextPut:thisChar |
|
3578 | 6388 |
] |
6389 |
||
6390 |
"Modified: / 15.6.1998 / 17:21:17 / cg" |
|
6391 |
"Created: / 15.6.1998 / 17:22:13 / cg" |
|
12171 | 6392 |
! |
6393 |
||
15788 | 6394 |
printXmlQuotedOn:aStream |
23549 | 6395 |
"convert aString to a valid XML string |
6396 |
that can be used for XML attributes. |
|
6397 |
Can also be used for XML text, but better use #printXmlTextQuotedOn:. |
|
23551 | 6398 |
Various senders expect, that the output is ISO-8859-1 compatible |
6399 |
(they don't care for encoding), so for now, we always generate 8-bit chars. |
|
23549 | 6400 |
TODO: care for 16bit UNICODE string and escape chars ..." |
6401 |
||
23548 | 6402 |
<resource: #todo> |
15788 | 6403 |
|
6404 |
self do:[:eachChar | |
|
20874 | 6405 |
eachChar == $< ifTrue:[ |
6406 |
aStream nextPutAll:'<' "mapping needed for xml text" |
|
6407 |
] ifFalse:[ eachChar == $& ifTrue:[ |
|
6408 |
aStream nextPutAll:'&' "mapping needed for all" |
|
6409 |
] ifFalse:[ eachChar == $> ifTrue:[ |
|
6410 |
aStream nextPutAll:'>' "mapping needed for comments" |
|
6411 |
] ifFalse:[ eachChar == $' ifTrue:[ |
|
6412 |
aStream nextPutAll:''' "mapping needed for attributes" |
|
6413 |
] ifFalse:[ eachChar == $" ifTrue:[ |
|
6414 |
aStream nextPutAll:'"' "mapping needed for attributes" |
|
6415 |
] ifFalse:[ |
|
6416 |
|codePoint| |
|
6417 |
codePoint := eachChar codePoint. |
|
23552 | 6418 |
(codePoint < 16r20 |
6419 |
or:[codePoint >= 16r7F |
|
6420 |
and:[codePoint <= 16r9F |
|
6421 |
or:[codePoint > 16rFF] "for now, always make sure, that output is ISO-8859-1 compatible"] |
|
6422 |
]) ifTrue:[ |
|
20874 | 6423 |
aStream nextPutAll:'&#'. |
23549 | 6424 |
codePoint printOn:aStream base:16. |
20874 | 6425 |
aStream nextPut:$;. |
23549 | 6426 |
] ifFalse:[ |
6427 |
aStream nextPut:eachChar |
|
6428 |
]. |
|
6429 |
]]]]] |
|
6430 |
]. |
|
6431 |
||
23552 | 6432 |
"Modified: / 03-12-2018 / 15:21:27 / Stefan Vogel" |
15788 | 6433 |
! |
6434 |
||
6435 |
printXmlTextQuotedOn:aStream |
|
23290 | 6436 |
<resource: #todo> |
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
6437 |
"convert aString to a valid XML string |
15788 | 6438 |
that can be used for XML text. |
6439 |
Here line formatting characters are not escaped. |
|
23549 | 6440 |
For XML text (as opposed to XML attribute), $' and $"" need |
6441 |
not to be transliterated (see https://www.w3.org/TR/xml Section 2.4). |
|
23551 | 6442 |
Various senders expect, that the output is ISO-8859-1 compatible |
6443 |
(they don't care for encoding), so for now, we always generate 8-bit chars. |
|
23549 | 6444 |
TODO: care for 16bit UNICODE string and escape chars |
6445 |
- but we need to know the XML file encoding" |
|
15788 | 6446 |
|
6447 |
self do:[:eachChar | |
|
20874 | 6448 |
eachChar == $< ifTrue:[ |
6449 |
aStream nextPutAll:'<' "mapping needed for xml text" |
|
6450 |
] ifFalse:[ eachChar == $& ifTrue:[ |
|
6451 |
aStream nextPutAll:'&' "mapping needed for all" |
|
23290 | 6452 |
] ifFalse:[ eachChar == $> ifTrue:[ |
23549 | 6453 |
aStream nextPutAll:'>' "mapping needed only for ]]>" |
20874 | 6454 |
] ifFalse:[ |
6455 |
|codePoint| |
|
6456 |
codePoint := eachChar codePoint. |
|
6457 |
((codePoint < 16r20 and:[codePoint ~~ 9 and:[codePoint ~~ 10 and:[codePoint ~~ 13]]]) |
|
23552 | 6458 |
or:[codePoint >= 16r7F |
6459 |
and:[codePoint <= 16r9F |
|
6460 |
or:[codePoint > 16rFF] "for now, always make sure, that output is ISO-8859-1 compatible"] |
|
6461 |
]) ifTrue:[ |
|
23321 | 6462 |
aStream nextPutAll:'&#x'. |
23290 | 6463 |
codePoint printOn:aStream base:16. |
20874 | 6464 |
aStream nextPut:$;. |
23549 | 6465 |
] ifFalse:[ |
6466 |
aStream nextPut:eachChar |
|
6467 |
]. |
|
6468 |
]]] |
|
23290 | 6469 |
]. |
6470 |
||
6471 |
" |
|
6472 |
String streamContents:[:s|'< & >' printXmlTextQuotedOn:s] |
|
6473 |
String streamContents:[:s|'< & >',(Character value:7) printXmlTextQuotedOn:s] |
|
6474 |
String streamContents:[:s|'< & >',(Character value:129) printXmlTextQuotedOn:s] |
|
6475 |
String streamContents:[:s|'< & >',(Character value:1000) printXmlTextQuotedOn:s] |
|
6476 |
" |
|
6477 |
||
23321 | 6478 |
"Modified: / 30-08-2018 / 22:39:00 / Claus Gittinger" |
23552 | 6479 |
"Modified: / 03-12-2018 / 15:21:18 / Stefan Vogel" |
15788 | 6480 |
! |
6481 |
||
6482 |
xmlQuotedPrintString |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
6483 |
"convert aString to a valid XML string |
23241 | 6484 |
that can be used for attributes, text, comments an PIs" |
15788 | 6485 |
|
6486 |
^ String streamContents:[:s| |
|
20874 | 6487 |
self printXmlQuotedOn:s |
15788 | 6488 |
]. |
23241 | 6489 |
|
6490 |
"Modified (comment): / 28-07-2018 / 11:11:51 / Claus Gittinger" |
|
9192 | 6491 |
! ! |
6492 |
||
11955 | 6493 |
|
9192 | 6494 |
!CharacterArray methodsFor:'queries'! |
608 | 6495 |
|
23701 | 6496 |
argumentCount |
6497 |
"treating the receiver as a message selector, return how many arguments would it take (ANSI)" |
|
6498 |
||
6499 |
|binopChars| |
|
6500 |
||
6501 |
(self size <= Method maxBinarySelectorSize) ifTrue:[ |
|
6502 |
binopChars := Method binarySelectorCharacters. |
|
6503 |
(self conform:[:eachChar | (binopChars includes:eachChar)]) ifTrue:[ |
|
6504 |
^ 1 |
|
6505 |
]. |
|
6506 |
]. |
|
6507 |
^ self occurrencesOf:$: |
|
6508 |
||
6509 |
" |
|
6510 |
'foo:bar:' argumentCount |
|
6511 |
#foo:bar: argumentCount |
|
6512 |
'hello' argumentCount |
|
6513 |
'+' argumentCount |
|
6514 |
'++' argumentCount |
|
6515 |
'+++' argumentCount |
|
6516 |
'|' argumentCount |
|
6517 |
'?' argumentCount |
|
6518 |
'_' argumentCount |
|
6519 |
'_:' argumentCount |
|
6520 |
'_:_:' argumentCount |
|
6521 |
'<->' argumentCount |
|
6522 |
'<' argumentCount |
|
6523 |
#'<' argumentCount |
|
6524 |
" |
|
6525 |
||
6526 |
"Modified (comment): / 06-02-2017 / 13:48:57 / cg" |
|
6527 |
! |
|
6528 |
||
4429 | 6529 |
bitsPerCharacter |
18662 | 6530 |
"return the underlying string's bitsPerCharacter |
4429 | 6531 |
(i.e. is it a regular String or a TwoByteString)" |
6532 |
||
8457 | 6533 |
|string max| |
6534 |
||
6535 |
(string := self string) ~~ self ifTrue:[ |
|
20874 | 6536 |
^ string bitsPerCharacter |
8457 | 6537 |
]. |
6538 |
||
6539 |
max := 8. |
|
6540 |
self do:[:eachCharacter | |
|
20874 | 6541 |
max := max max:(eachCharacter bitsPerCharacter) |
9192 | 6542 |
]. |
6543 |
^ max |
|
6544 |
||
6545 |
" |
|
6546 |
'hello' bitsPerCharacter |
|
6547 |
'hello' asText allBold bitsPerCharacter |
|
6548 |
" |
|
6549 |
! |
|
8457 | 6550 |
|
19757 | 6551 |
bytesPerCharacter |
23971 | 6552 |
"return the underlying string's bytesPerCharacter |
19757 | 6553 |
(i.e. is it a regular String or a TwoByteString)" |
6554 |
||
6555 |
|string max| |
|
6556 |
||
6557 |
(string := self string) ~~ self ifTrue:[ |
|
20874 | 6558 |
^ string bytesPerCharacter |
19757 | 6559 |
]. |
6560 |
||
6561 |
max := 1. |
|
6562 |
self do:[:eachCharacter | |
|
20874 | 6563 |
max := max max:(eachCharacter bytesPerCharacter) |
19757 | 6564 |
]. |
6565 |
^ max |
|
6566 |
||
6567 |
" |
|
6568 |
'hello' bytesPerCharacter |
|
6569 |
'hello' asUnicode16String bytesPerCharacter |
|
6570 |
'hello' asText allBold bytesPerCharacter |
|
6571 |
" |
|
23971 | 6572 |
|
6573 |
"Modified (comment): / 25-03-2019 / 16:24:02 / Claus Gittinger" |
|
6574 |
! |
|
6575 |
||
6576 |
bytesPerCharacterNeeded |
|
6577 |
"return the actual underlying string's required bytesPerCharacter |
|
6578 |
(i.e. checks if all characters really need that depth)" |
|
6579 |
||
6580 |
|string max mySize| |
|
6581 |
||
6582 |
(string := self string) ~~ self ifTrue:[ |
|
6583 |
^ string bytesPerCharacterNeeded |
|
6584 |
]. |
|
6585 |
||
6586 |
mySize := self bytesPerCharacter. |
|
6587 |
||
6588 |
max := 1. |
|
6589 |
self do:[:eachCharacter | |
|
6590 |
max := max max:(eachCharacter bytesPerCharacter). |
|
6591 |
max == mySize ifTrue:[^ max]. |
|
6592 |
]. |
|
6593 |
^ max |
|
6594 |
||
6595 |
" |
|
6596 |
'hello' bytesPerCharacter -> 1 |
|
6597 |
'hello' bytesPerCharacterNeeded -> 1 |
|
6598 |
||
6599 |
'hello' asUnicode16String bytesPerCharacter -> 2 |
|
6600 |
'hello' asUnicode16String bytesPerCharacterNeeded -> 1 |
|
6601 |
" |
|
6602 |
||
6603 |
"Created: / 25-03-2019 / 16:22:00 / Claus Gittinger" |
|
19757 | 6604 |
! |
6605 |
||
23701 | 6606 |
characterSize |
6607 |
"answer the size in bits of my largest character (actually only 7, 8, 16 or 32)" |
|
6608 |
||
6609 |
|string max |
|
6610 |
sz "{ Class:SmallInteger}" | |
|
6611 |
||
6612 |
(string := self string) ~~ self ifTrue:[ |
|
6613 |
^ string characterSize. |
|
6614 |
]. |
|
6615 |
||
6616 |
sz := self size. |
|
6617 |
max := 7. |
|
6618 |
1 to:sz do:[:idx | |
|
6619 |
|thisSize| |
|
6620 |
||
6621 |
thisSize := (self at:idx) characterSize. |
|
6622 |
thisSize > max ifTrue:[ |
|
6623 |
max := thisSize. |
|
6624 |
max == 32 ifTrue:[ |
|
6625 |
"shortcut: we know, that max size is 32" |
|
6626 |
^ 32. |
|
6627 |
]. |
|
6628 |
]. |
|
6629 |
]. |
|
6630 |
||
6631 |
^ max. |
|
6632 |
||
6633 |
" |
|
6634 |
'hello' characterSize |
|
6635 |
'hello' asUnicode32String characterSize |
|
6636 |
'helloü' asUnicode32String characterSize |
|
6637 |
'hello' asText allBold characterSize |
|
6638 |
" |
|
6639 |
! |
|
6640 |
||
6641 |
contains8BitCharacters |
|
6642 |
<resource: #obsolete> |
|
6643 |
||
6644 |
^ self containsNon7BitAscii |
|
6645 |
! |
|
6646 |
||
6647 |
containsNon7BitAscii |
|
6648 |
"return true, if the underlying string contains 8BitCharacters (or widers) |
|
6649 |
(i.e. if it is non-ascii)" |
|
6650 |
||
6651 |
|string |
|
6652 |
sz "{ Class:SmallInteger }"| |
|
6653 |
||
6654 |
(string := self string) ~~ self ifTrue:[ |
|
6655 |
^ string containsNon7BitAscii |
|
6656 |
]. |
|
6657 |
sz := self size. |
|
6658 |
1 to:sz do:[:idx| |
|
6659 |
(self at:idx) codePoint > 16r7F ifTrue:[ |
|
6660 |
^ true. |
|
6661 |
]. |
|
6662 |
]. |
|
6663 |
^ false. |
|
6664 |
||
6665 |
" |
|
6666 |
'hello' asUnicode32String containsNon7BitAscii |
|
6667 |
'hello üöä' asUnicode32String containsNon7BitAscii |
|
6668 |
'hello' asUnicode32String asText allBold containsNon7BitAscii |
|
6669 |
'hello üö' asUnicode32String asText allBold containsNon7BitAscii |
|
6670 |
" |
|
6671 |
! |
|
6672 |
||
18967 | 6673 |
containsNon8BitElements |
6674 |
"return true, if the underlying string contains elements larger than a single byte" |
|
6675 |
||
21322 | 6676 |
|string sz "{ Class:SmallInteger }"| |
6677 |
||
6678 |
(string := self string) ~~ self ifTrue:[ |
|
6679 |
^ string containsNon8BitElements |
|
6680 |
]. |
|
18967 | 6681 |
sz := self size. |
6682 |
1 to:sz do:[:idx| |
|
20874 | 6683 |
(self at:idx) codePoint > 16rFF ifTrue:[ |
6684 |
^ true. |
|
6685 |
]. |
|
18967 | 6686 |
]. |
6687 |
^ false. |
|
6688 |
! |
|
6689 |
||
10903 | 6690 |
continuesWith:aString startingAt:startIndex |
6691 |
"return true, if the receiver beginning at startIndex |
|
22783 | 6692 |
contains the characters in aString. |
6693 |
Returns false if the search reaches the end of the receiver |
|
6694 |
before all characters have been compared." |
|
10903 | 6695 |
|
6696 |
|sz "{Class: SmallInteger }" |
|
6697 |
idx "{Class: SmallInteger }"| |
|
6698 |
||
6699 |
sz := aString size. |
|
22783 | 6700 |
(startIndex + sz - 1) > self size ifTrue:[^ false]. |
6701 |
||
10903 | 6702 |
idx := startIndex. |
22783 | 6703 |
|
10903 | 6704 |
1 to:sz do:[:i | |
20874 | 6705 |
(self at:idx) ~~ (aString at:i) ifTrue:[^ false]. |
6706 |
idx := idx + 1 |
|
10903 | 6707 |
]. |
6708 |
^ true |
|
6709 |
||
6710 |
" |
|
6711 |
'hello world' continuesWith:'world' startingAt:6 |
|
6712 |
'hello world' continuesWith:'world' startingAt:7 |
|
22783 | 6713 |
'hello' continuesWith:'llo' startingAt:3 |
6714 |
'hello' continuesWith:'llow' startingAt:3 |
|
10903 | 6715 |
" |
6716 |
||
6717 |
"Created: 12.5.1996 / 15:46:40 / cg" |
|
6718 |
"Modified: 26.7.1996 / 19:08:36 / cg" |
|
6719 |
! |
|
6720 |
||
6721 |
countWords |
|
6722 |
"return the number of words, which are separated by separators" |
|
6723 |
||
6724 |
|tally "{ Class: SmallInteger }" |
|
6725 |
start "{ Class: SmallInteger }" |
|
6726 |
mySize "{ Class: SmallInteger }" |
|
6727 |
stop ch| |
|
6728 |
||
6729 |
tally := 0. |
|
6730 |
start := 1. |
|
6731 |
mySize := self size. |
|
6732 |
[start <= mySize] whileTrue:[ |
|
20874 | 6733 |
ch := self at:start. |
6734 |
ch isSeparator ifTrue:[ |
|
6735 |
start := start + 1 |
|
6736 |
] ifFalse:[ |
|
6737 |
stop := self indexOfSeparatorStartingAt:start. |
|
6738 |
(stop == 0) ifTrue:[ |
|
6739 |
stop := mySize + 1 |
|
6740 |
]. |
|
6741 |
tally := tally + 1. |
|
6742 |
start := stop |
|
6743 |
] |
|
10903 | 6744 |
]. |
6745 |
^ tally |
|
6746 |
||
6747 |
" |
|
6748 |
'hello world isnt this nice' countWords' |
|
6749 |
" |
|
6750 |
! |
|
6751 |
||
11788 | 6752 |
defaultElement |
6753 |
^ Character space |
|
6754 |
! |
|
6755 |
||
608 | 6756 |
encoding |
1311 | 6757 |
"return the strings encoding, as a symbol. |
7982 | 6758 |
Here, by default, we assume unicode-encoding. |
20006 | 6759 |
Notice, that ISO-8859-1 is a true subset of unicode, |
8874 | 6760 |
and that singleByteStrings are therefore both unicode AND |
7982 | 6761 |
8859-1 encoded." |
6762 |
||
23290 | 6763 |
^ #'iso10646-1'. |
6764 |
"/ ^ #unicode |
|
6765 |
||
6766 |
"Modified: / 22-08-2018 / 09:30:16 / Claus Gittinger" |
|
608 | 6767 |
! |
6768 |
||
1375
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
6769 |
hasChangeOfEmphasis |
14405
1d0d99a95bba
comment/format in:7 methods
Claus Gittinger <cg@exept.de>
parents:
14168
diff
changeset
|
6770 |
"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
|
6771 |
i.e. any non-normal (=emphasized) characters" |
1d0d99a95bba
comment/format in:7 methods
Claus Gittinger <cg@exept.de>
parents:
14168
diff
changeset
|
6772 |
|
1375
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
6773 |
^ false |
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
6774 |
|
1381 | 6775 |
"Created: 12.5.1996 / 12:31:39 / cg" |
1375
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
6776 |
! |
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
6777 |
|
7995
8ddbb403d67b
+hasIcon and hasImage for LabelAndIcon compatibility
Claus Gittinger <cg@exept.de>
parents:
7991
diff
changeset
|
6778 |
hasIcon |
9191 | 6779 |
"for LabelAndIcon compatibility" |
6780 |
||
9192 | 6781 |
^ false |
6782 |
! |
|
6783 |
||
6784 |
hasImage |
|
6785 |
"for LabelAndIcon compatibility" |
|
6786 |
||
6787 |
^ false |
|
6788 |
! |
|
7995
8ddbb403d67b
+hasIcon and hasImage for LabelAndIcon compatibility
Claus Gittinger <cg@exept.de>
parents:
7991
diff
changeset
|
6789 |
|
1388 | 6790 |
heightOn:aGC |
19464 | 6791 |
"return the size of the receiver in device units if displayed on aGC" |
1388 | 6792 |
|
20221 | 6793 |
^ aGC deviceFont heightOf:self "font is already a device font" |
1388 | 6794 |
|
6795 |
" |
|
8874 | 6796 |
'hello world' heightOn:(View new) |
1388 | 6797 |
" |
6798 |
||
6799 |
"Created: 12.5.1996 / 20:09:29 / cg" |
|
6800 |
"Modified: 12.5.1996 / 20:32:05 / cg" |
|
6801 |
! |
|
6802 |
||
23701 | 6803 |
isAlphaNumeric |
6804 |
"return true, if the receiver is some alphanumeric word; |
|
6805 |
i.e. consists of a letter followed by letters or digits." |
|
6806 |
||
6807 |
self isEmpty ifTrue:[ |
|
6808 |
"mhmh what is this ?" |
|
6809 |
^ false |
|
6810 |
]. |
|
6811 |
(self at:1) isLetter ifFalse:[^ false]. |
|
6812 |
^ self conform:[:char | char isLetterOrDigit]. |
|
6813 |
||
6814 |
" |
|
6815 |
'helloWorld' isAlphaNumeric |
|
6816 |
'foo1234' isAlphaNumeric |
|
6817 |
'f1234' isAlphaNumeric |
|
6818 |
'1234' isAlphaNumeric |
|
6819 |
'+' isAlphaNumeric |
|
6820 |
" |
|
6821 |
||
6822 |
"Modified: / 13-10-2006 / 12:53:49 / cg" |
|
6823 |
! |
|
6824 |
||
6825 |
isBinarySelector |
|
6826 |
"treating the receiver as a message selector, return true if it's a binary selector. |
|
6827 |
Notice, that st/x does not have a size <= 2 limit for unaries" |
|
6828 |
||
6829 |
|binopChars| |
|
6830 |
||
6831 |
(self size > Method maxBinarySelectorSize) ifTrue:[^ false]. |
|
6832 |
||
6833 |
binopChars := Method binarySelectorCharacters. |
|
6834 |
^ (self conform:[:char | (binopChars includes:char)]) |
|
6835 |
||
6836 |
" |
|
6837 |
'foo:bar:' isBinarySelector |
|
6838 |
#foo:bar: isBinarySelector |
|
6839 |
'hello' isBinarySelector |
|
6840 |
'+' isBinarySelector |
|
6841 |
'|' isBinarySelector |
|
6842 |
'?' isBinarySelector |
|
6843 |
':' isBinarySelector |
|
6844 |
'a:' isBinarySelector |
|
6845 |
'->' isBinarySelector |
|
6846 |
'<->' isBinarySelector |
|
6847 |
'::' isBinarySelector |
|
6848 |
" |
|
6849 |
||
6850 |
"Modified: / 04-01-1997 / 14:16:14 / cg" |
|
6851 |
"Modified (comment): / 13-02-2017 / 19:57:29 / cg" |
|
6852 |
! |
|
6853 |
||
6854 |
isBlank |
|
6855 |
"return true, if the receiver contains spaces only" |
|
6856 |
||
6857 |
^ self size == 0 or:[(self contains:[:char | char ~~ Character space]) not] |
|
6858 |
||
6859 |
" |
|
6860 |
'' isBlank |
|
6861 |
'' asUnicode16String isBlank |
|
6862 |
' a ' isBlank |
|
6863 |
' ' isBlank |
|
6864 |
' ' asUnicode16String isBlank |
|
6865 |
" |
|
6866 |
||
6867 |
"Modified (comment): / 14-09-2018 / 10:04:18 / Stefan Vogel" |
|
6868 |
! |
|
6869 |
||
6870 |
isInfix |
|
6871 |
"return true, if the receiver is a binary message selector" |
|
6872 |
||
6873 |
^ self first isLetterOrDigit not |
|
6874 |
||
6875 |
" |
|
6876 |
#at:put: isInfix |
|
6877 |
#at: isInfix |
|
6878 |
#+ isInfix |
|
6879 |
#size isInfix |
|
6880 |
" |
|
6881 |
||
6882 |
"Created: / 1.11.1997 / 12:34:55 / cg" |
|
6883 |
"Modified: / 1.11.1997 / 12:36:37 / cg" |
|
6884 |
! |
|
6885 |
||
6886 |
isKeyword |
|
6887 |
"return true, if the receiver is a keyword message selector. |
|
6888 |
This is a quick check, which only looks at the last character. |
|
6889 |
Should only be used, if we already know that the receiver forms a valid selector. |
|
6890 |
To check an arbitrary string, use isKeywordSelector. |
|
6891 |
Bad naming, but compatibility is asking for it." |
|
6892 |
||
6893 |
^ self last == $: |
|
6894 |
||
6895 |
" |
|
6896 |
#at:put: isKeyword |
|
6897 |
#at: isKeyword |
|
6898 |
#+ isKeyword |
|
6899 |
#size isKeyword |
|
6900 |
" |
|
6901 |
||
6902 |
"Created: / 01-11-1997 / 12:34:55 / cg" |
|
6903 |
"Modified (comment): / 30-04-2016 / 18:19:11 / cg" |
|
6904 |
! |
|
6905 |
||
6906 |
isKeywordSelector |
|
6907 |
"return true, iff there are only alphanumeric or underline characters separated by colons. |
|
6908 |
Must end with a colon. |
|
6909 |
You can use this to check an arbitrary string for being valid as a keyword. |
|
6910 |
If you have a valid selector at hand, and need to know if it is a keyword or not, |
|
6911 |
use #isKeyword, which is much faster." |
|
6912 |
||
6913 |
|state| |
|
6914 |
||
6915 |
(self size == 0) ifTrue:[^ false]. |
|
6916 |
(self last == $:) ifFalse:[^ false]. |
|
6917 |
||
6918 |
state := #initial. |
|
6919 |
self do:[:char | |
|
6920 |
(state == #initial or:[ state == #gotColon]) ifTrue:[ |
|
6921 |
(char isLetterOrUnderline) ifFalse:[^ false]. |
|
6922 |
state := #gotCharacter. |
|
6923 |
] ifFalse:[ |
|
6924 |
char == $: ifTrue:[ |
|
6925 |
state := #gotColon. |
|
6926 |
] ifFalse:[ |
|
6927 |
(char isLetterOrDigit or:[char == $_]) ifFalse:[^ false]. |
|
6928 |
]. |
|
6929 |
]. |
|
6930 |
]. |
|
6931 |
^ state == #gotColon. |
|
6932 |
||
6933 |
" |
|
6934 |
self assert:(':' isKeywordSelector not). |
|
6935 |
self assert:(':a' isKeywordSelector not). |
|
6936 |
self assert:('1:' isKeywordSelector not). |
|
6937 |
self assert:('a:' isKeywordSelector). |
|
6938 |
self assert:('_:' isKeywordSelector). |
|
6939 |
self assert:('_a:' isKeywordSelector). |
|
6940 |
self assert:('_1:' isKeywordSelector). |
|
6941 |
self assert:('_1::' isKeywordSelector not). |
|
6942 |
self assert:('_:_:' isKeywordSelector). |
|
6943 |
self assert:('a:b:' isKeywordSelector). |
|
6944 |
self assert:('aa:bb:' isKeywordSelector). |
|
6945 |
self assert:('aa:bb:a' isKeywordSelector not). |
|
6946 |
self assert:('1:2:' isKeywordSelector not). |
|
6947 |
" |
|
6948 |
||
6949 |
"Modified (comment): / 30-04-2016 / 18:20:14 / cg" |
|
6950 |
! |
|
6951 |
||
11828 | 6952 |
isLowercaseFirst |
6953 |
"return true, if the first character is a lowercase character." |
|
6954 |
||
6955 |
^ self size ~~ 0 and:[ self first isLowercase ] |
|
6956 |
||
6957 |
" |
|
12177 | 6958 |
'helloWorld' isLowercaseFirst |
6959 |
'HelloWorld' isLowercaseFirst |
|
11828 | 6960 |
" |
6961 |
! |
|
6962 |
||
23701 | 6963 |
isNameSpaceSelector |
6964 |
"Answer true if the receiver contains chars which form a nameSpace selector name. |
|
6965 |
These are of the form ':<ns>::<sel>', where ns is the NameSpace and sel is the regular selector. |
|
6966 |
For example, the #+ selector as seen by the Foo namespace would be actually #':Foo::+'. |
|
6967 |
This special format (a symbol starting with a colon) was chosen, because almost every other selector |
|
6968 |
is legal, and this can be checked quickly by just looking at the first character. |
|
6969 |
You cannot easily change this algorithm here, as it is also known by the VM's lookup function." |
|
6970 |
||
6971 |
|i| |
|
6972 |
||
6973 |
(self at:1) == $: ifFalse:[^ false]. |
|
6974 |
||
6975 |
i := self indexOf:$: startingAt:2. |
|
6976 |
i == 0 ifTrue:[^ false]. |
|
6977 |
self size <= (i+1) ifTrue:[^ false]. |
|
6978 |
(self at:i+1) == $: ifFalse:[^ false]. |
|
6979 |
(self at:i+2) == $: ifTrue:[^ false]. |
|
6980 |
"/ could check if the rest after the ns-prefix and colons is a valid selector... |
|
6981 |
^ true |
|
6982 |
||
6983 |
"test: |
|
6984 |
self assert:('+' isNameSpaceSelector) not. |
|
6985 |
self assert:(':+' isNameSpaceSelector) not. |
|
6986 |
self assert:(':Foo:+' isNameSpaceSelector) not. |
|
6987 |
||
6988 |
self assert:(':Foo::+' isNameSpaceSelector). |
|
6989 |
self assert:(':Foo::bar:baz:' isNameSpaceSelector). |
|
6990 |
" |
|
6991 |
||
6992 |
"Created: / 05-03-2007 / 11:35:31 / cg" |
|
6993 |
! |
|
6994 |
||
6995 |
isNumeric |
|
6996 |
"return true, if the receiver is some numeric word; |
|
6997 |
i.e. consists only of digits." |
|
6998 |
||
6999 |
self size == 0 ifTrue:[ |
|
7000 |
^ false |
|
7001 |
]. |
|
7002 |
^ self conform:[:char | char isDigit] |
|
7003 |
||
7004 |
" |
|
7005 |
'helloWorld' isNumeric |
|
7006 |
'foo1234' isNumeric |
|
7007 |
'f1234' isNumeric |
|
7008 |
'1234' isNumeric |
|
7009 |
'+' isNumeric |
|
7010 |
" |
|
7011 |
||
7012 |
"Modified: / 13-10-2006 / 12:54:12 / cg" |
|
7013 |
! |
|
7014 |
||
11828 | 7015 |
isUppercaseFirst |
7016 |
"return true, if the first character is an uppercase character." |
|
7017 |
||
7018 |
^ self size ~~ 0 and:[ self first isUppercase ] |
|
7019 |
||
7020 |
" |
|
12177 | 7021 |
'helloWorld' isUppercaseFirst |
7022 |
'HelloWorld' isUppercaseFirst |
|
11828 | 7023 |
" |
7024 |
! |
|
7025 |
||
23701 | 7026 |
isValidSmalltalkIdentifier |
7027 |
"return true, if the receiver's characters make up a valid smalltalk identifier" |
|
7028 |
||
7029 |
|scanner tok| |
|
7030 |
||
7031 |
scanner := Compiler new. |
|
7032 |
scanner source:(self readStream). |
|
7033 |
ParseError handle:[:ex | |
|
7034 |
tok := nil. |
|
7035 |
] do:[ |
|
7036 |
tok := scanner nextToken. |
|
7037 |
]. |
|
7038 |
tok ~~ #Identifier ifTrue:[ |
|
7039 |
^ false |
|
7040 |
]. |
|
7041 |
scanner tokenPosition == 1 ifFalse:[^ false]. |
|
7042 |
^ scanner sourceStream atEnd. |
|
7043 |
||
7044 |
" |
|
7045 |
'foo' isValidSmalltalkIdentifier |
|
7046 |
'1foo' isValidSmalltalkIdentifier |
|
7047 |
'_foo' isValidSmalltalkIdentifier |
|
7048 |
'_foo_bar_' isValidSmalltalkIdentifier |
|
7049 |
'foo ' isValidSmalltalkIdentifier |
|
7050 |
' foo' isValidSmalltalkIdentifier |
|
7051 |
" |
|
7052 |
! |
|
7053 |
||
7054 |
isWhitespace |
|
7055 |
"return true, if the receiver is empty or contains only whitespace." |
|
7056 |
||
7057 |
^ (self indexOfNonSeparatorStartingAt:1) == 0 |
|
7058 |
||
7059 |
" |
|
7060 |
'' isWhitespace |
|
7061 |
' ' isWhitespace |
|
7062 |
' \ \' withCRs isWhitespace |
|
7063 |
' a\ \' withCRs isWhitespace |
|
7064 |
' \ \a' withCRs isWhitespace |
|
7065 |
'a \ \a' withCRs isWhitespace |
|
7066 |
" |
|
7067 |
||
7068 |
"Created: / 01-03-2017 / 15:24:53 / cg" |
|
7069 |
! |
|
7070 |
||
15773 | 7071 |
keywords |
7072 |
"assuming the receiver is a keyword message selector, |
|
7073 |
return the individual keywords (i.e. break it up at colons) |
|
7074 |
and return these as a collection. |
|
7075 |
For binary and unary selectors, the result may be nonsense (an array containing the receiver)." |
|
7076 |
||
7077 |
|coll s part| |
|
7078 |
||
7079 |
coll := OrderedCollection new. |
|
7080 |
s := ReadStream on:self. |
|
7081 |
[s atEnd] whileFalse:[ |
|
20874 | 7082 |
part := s through:$:. |
7083 |
coll add:part |
|
15773 | 7084 |
]. |
7085 |
^ coll asArray |
|
7086 |
||
7087 |
" |
|
7088 |
#at:put: keywords |
|
7089 |
#at: keywords |
|
7090 |
#+ keywords |
|
7091 |
#size keywords |
|
7092 |
" |
|
7093 |
||
7094 |
"Modified (Comment): / 30-06-2011 / 17:46:21 / cg" |
|
7095 |
! |
|
7096 |
||
9190 | 7097 |
knownAsSymbol |
7098 |
"for now, only single character strings are allowed as symbols. |
|
7099 |
This method is redefined in String." |
|
7100 |
||
7101 |
^ false |
|
7102 |
! |
|
7103 |
||
7104 |
leftIndent |
|
23663 | 7105 |
"if the receiver starts with whiteSpace, return the number of whiteSpace chars |
9190 | 7106 |
at the left - otherwise, return 0. |
23663 | 7107 |
If the receiver consists of whiteSpace only, return the receiver's size." |
9192 | 7108 |
|
7109 |
|index "{Class: SmallInteger }" |
|
7110 |
end "{Class: SmallInteger }"| |
|
7111 |
||
7112 |
index := 1. |
|
7113 |
end := self size. |
|
7114 |
[index <= end] whileTrue:[ |
|
20874 | 7115 |
(self at:index) isSeparator ifFalse:[^ index - 1]. |
7116 |
index := index + 1 |
|
8350 | 7117 |
]. |
9192 | 7118 |
^ end |
7119 |
||
7120 |
" |
|
7121 |
' hello' leftIndent |
|
7122 |
'foo ' leftIndent |
|
9190 | 7123 |
' ' leftIndent |
23663 | 7124 |
((Character tab),(Character tab),'foo') leftIndent |
7125 |
" |
|
7126 |
||
7127 |
"Modified: / 20-04-1996 / 19:28:43 / cg" |
|
7128 |
"Modified (comment): / 05-02-2019 / 11:22:57 / Claus Gittinger" |
|
8350 | 7129 |
! |
7130 |
||
10437 | 7131 |
nameSpaceSelectorParts |
7132 |
"Answer the namespace and baseSelector parts of a namespace selector. |
|
7133 |
Namespace selectors are those generated by sends from a method in a different |
|
7134 |
namespace; they are prefixed by ':'<ns>'::'. |
|
7135 |
You cannot easily change this algorithm here, as it is also known by the VM's lookup function. |
|
7136 |
Experimental" |
|
7137 |
||
7138 |
|nsPart selPart idx| |
|
7139 |
||
7140 |
(self at:1) == $: ifFalse:[^ Array with:'' with:self]. |
|
7141 |
idx := self indexOf:$: startingAt:3. |
|
7142 |
idx == 0 ifTrue:[^ Array with:'' with:self]. |
|
7143 |
||
7144 |
(idx+2 > self size) ifTrue:[^ Array with:'' with:self]. |
|
7145 |
(self at:idx+1) == $: ifFalse:[^ Array with:'' with:self]. |
|
7146 |
nsPart := self copyFrom:2 to:idx-1. |
|
7147 |
selPart := self copyFrom:idx+2. |
|
7148 |
^ Array with:nsPart with:selPart |
|
7149 |
||
7150 |
"test: |
|
10703
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
7151 |
self assert:('+' nameSpaceSelectorParts) = #('' '+'). |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
7152 |
self assert:(':+' nameSpaceSelectorParts) = #('' ':+'). |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
7153 |
self assert:(':Foo:+' nameSpaceSelectorParts) = #('' ':Foo:+'). |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
7154 |
|
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
7155 |
self assert:(':Foo::+' nameSpaceSelectorParts) = #('Foo' '+'). |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
7156 |
self assert:(':Foo::bar:baz:' nameSpaceSelectorParts) = #('Foo' 'bar:baz:'). |
10437 | 7157 |
" |
7158 |
||
7159 |
"Created: / 05-03-2007 / 17:16:58 / cg" |
|
7160 |
"Modified: / 06-03-2007 / 11:51:15 / cg" |
|
7161 |
! |
|
7162 |
||
23701 | 7163 |
numArgs |
7164 |
<resource: #obsolete> |
|
7165 |
"treating the receiver as a message selector, return how many arguments would it take. |
|
7166 |
Please use argumentCount for ANSI compatibility." |
|
7167 |
||
7168 |
^ self argumentCount |
|
7169 |
! |
|
7170 |
||
7171 |
partsIfSelector |
|
7172 |
"treat the receiver as a message selector, return a collection of parts. |
|
7173 |
Notice: this is more tolerant than Smalltalk's syntax would suggest; |
|
7174 |
especially it allows for empty keyword parts between colons. |
|
7175 |
This is not (and should not be checked here), to allow parsing of |
|
7176 |
degenerate selectors as appearing with objectiveC." |
|
7177 |
||
7178 |
|idx1 "{ Class: SmallInteger }" |
|
7179 |
coll idx2 sz| |
|
7180 |
||
7181 |
coll := OrderedCollection new. |
|
7182 |
idx1 := 1. |
|
7183 |
sz := self size. |
|
7184 |
[ |
|
7185 |
idx2 := self indexOf:$: startingAt:idx1. |
|
7186 |
(idx2 == 0 or:[idx2 == sz]) ifTrue:[ |
|
7187 |
coll add:(self copyFrom:idx1). |
|
7188 |
^ coll |
|
7189 |
]. |
|
7190 |
coll add:(self copyFrom:idx1 to:idx2). |
|
7191 |
idx1 := idx2 + 1 |
|
7192 |
] loop. |
|
7193 |
||
7194 |
" |
|
7195 |
'foo:' partsIfSelector |
|
7196 |
'foo:bar:' partsIfSelector |
|
7197 |
'foo::::' partsIfSelector |
|
7198 |
#foo:bar: partsIfSelector |
|
7199 |
'hello' partsIfSelector |
|
7200 |
'+' partsIfSelector |
|
7201 |
" |
|
7202 |
! |
|
7203 |
||
21283 | 7204 |
speciesForSubcollection |
7205 |
"answer the class, when splitting instances into subcollections" |
|
7206 |
||
7207 |
^ StringCollection |
|
7208 |
||
7209 |
"Created: / 24-01-2017 / 18:54:18 / stefan" |
|
7210 |
! |
|
7211 |
||
9190 | 7212 |
stringSpecies |
8874 | 7213 |
"return the underlying strings bitsPerCharacter |
8037
8cce25b1059c
care for wide chars in asXXXFirst
Claus Gittinger <cg@exept.de>
parents:
8032
diff
changeset
|
7214 |
(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
|
7215 |
|
8cce25b1059c
care for wide chars in asXXXFirst
Claus Gittinger <cg@exept.de>
parents:
8032
diff
changeset
|
7216 |
|string| |
8cce25b1059c
care for wide chars in asXXXFirst
Claus Gittinger <cg@exept.de>
parents:
8032
diff
changeset
|
7217 |
|
15523 | 7218 |
string := self string. |
7219 |
string == self ifTrue:[^ self species]. |
|
8037
8cce25b1059c
care for wide chars in asXXXFirst
Claus Gittinger <cg@exept.de>
parents:
8032
diff
changeset
|
7220 |
^ string stringSpecies |
8cce25b1059c
care for wide chars in asXXXFirst
Claus Gittinger <cg@exept.de>
parents:
8032
diff
changeset
|
7221 |
|
8cce25b1059c
care for wide chars in asXXXFirst
Claus Gittinger <cg@exept.de>
parents:
8032
diff
changeset
|
7222 |
" |
8874 | 7223 |
'hello' stringSpecies |
9192 | 7224 |
'hello' asText allBold stringSpecies |
7225 |
" |
|
7226 |
! |
|
8037
8cce25b1059c
care for wide chars in asXXXFirst
Claus Gittinger <cg@exept.de>
parents:
8032
diff
changeset
|
7227 |
|
6653 | 7228 |
widthFrom:startIndex to:endIndex on:aGC |
19464 | 7229 |
"return ths size of part of the receiver in device units if displayed on aGC" |
6653 | 7230 |
|
20221 | 7231 |
^ aGC deviceFont widthOf:self from:startIndex to:endIndex |
6653 | 7232 |
|
7233 |
" |
|
8874 | 7234 |
'hello world' widthFrom:1 to:5 on:(View new) |
7235 |
'hello' widthOn:(View new) |
|
6653 | 7236 |
" |
9192 | 7237 |
! |
6653 | 7238 |
|
1388 | 7239 |
widthOn:aGC |
19464 | 7240 |
"return ths size of the receiver in device units if displayed on aGC" |
1388 | 7241 |
|
20221 | 7242 |
^ aGC deviceFont widthOf:self "font is already a device font" |
1388 | 7243 |
|
7244 |
" |
|
8874 | 7245 |
'hello world' widthOn:(View new) |
1388 | 7246 |
" |
7247 |
||
7248 |
"Created: 12.5.1996 / 20:09:29 / cg" |
|
2554 | 7249 |
"Modified: 17.4.1997 / 12:50:23 / cg" |
608 | 7250 |
! ! |
7251 |
||
24033 | 7252 |
|
608 | 7253 |
!CharacterArray methodsFor:'special string converting'! |
7254 |
||
16759 | 7255 |
asUnixFilenameString |
7256 |
"return a new string consisting of receiver's characters |
|
7257 |
with all \-characters replaced by /-characters. |
|
7258 |
If there are no backslashes, return the original" |
|
7259 |
||
7260 |
^ self copyReplaceAll:$\ with:$/ ifNone:self |
|
7261 |
||
7262 |
" |
|
7263 |
'hello\world' asUnixFilenameString |
|
7264 |
" |
|
7265 |
||
7266 |
"Modified: / 18.7.1998 / 22:53:02 / cg" |
|
7267 |
! |
|
7268 |
||
14768 | 7269 |
expandPlaceholders:escapeCharacter with:argArrayOrDictionary |
7270 |
"this is the generic version of the old %-escaping method, allowing for an arbitrary |
|
7271 |
escape character to be used (typically $$ or $% are effectively used). |
|
7272 |
Return a copy of the receiver, where all %i escapes are |
|
7273 |
replaced by corresponding arguments' printStrings from the argArrayOrDictionary. |
|
7274 |
I.e. 'hello %1; how is %2' expandPlaceholdersWith:#('world' 'this') results |
|
7275 |
in the new string 'hello world; how is this'. |
|
7276 |
As an extension, the argument may also be a dictionary, providing |
|
7277 |
values for symbolic keys. |
|
7278 |
In this case, %a .. %z and %(...) are also allowed. |
|
7279 |
(%1..%9 require a numeric key in the dictionary, however) |
|
7280 |
To get a '%' character, use a '%%'-escape. |
|
7281 |
To get an integer-indexed placeHolder followed by another digit, |
|
7282 |
or an index > 9, you must use %(digit). |
|
7283 |
See also bindWith:... for VisualAge compatibility." |
|
7284 |
||
7285 |
|stream| |
|
7286 |
||
18269 | 7287 |
stream := (TextStream ? CharacterWriteStream) on:(self species uninitializedNew:self size + 20). |
23067 | 7288 |
self expandPlaceholders:escapeCharacter with:argArrayOrDictionary ignoreNumericEscapes:false on:stream. |
14768 | 7289 |
^ stream contents. |
7290 |
||
7291 |
||
7292 |
" |
|
7293 |
'hello %1' expandPlaceholdersWith:#('world') |
|
7294 |
'hello %1; how is %2' expandPlaceholdersWith:#('world' 'this') |
|
7295 |
'hello %2; how is %1' expandPlaceholdersWith:#('world' 'this') |
|
7296 |
'%1 plus %2 gives %3 ' expandPlaceholdersWith:#(4 5 9) |
|
7297 |
'%%(1)0 gives %(1)0' expandPlaceholdersWith:#(123) |
|
7298 |
'%%10 gives %10' expandPlaceholdersWith:#(123) |
|
7299 |
'%%(10) gives %(10)' expandPlaceholdersWith:#(123) |
|
7300 |
'%test gives %1' expandPlaceholdersWith:#(123) |
|
7301 |
'bla %1 bla' expandPlaceholdersWith:{ 'hello' allBold } |
|
7302 |
'bla %1 bla' expandPlaceholdersWith:{ 'hello' } |
|
7303 |
" |
|
7304 |
||
7305 |
" |
|
7306 |
|dict| |
|
7307 |
||
7308 |
dict := Dictionary new. |
|
7309 |
dict at:1 put:'one'. |
|
7310 |
dict at:$a put:'AAAAA'. |
|
7311 |
dict at:$b put:[ Time now ]. |
|
7312 |
dict at:'foo' put:[ Date today ]. |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
7313 |
'hello $1 %a $b %(foo) $foo ' expandPlaceholders:$$ with:dict. |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
7314 |
'hello $1 %a $b %(foo) $foo ' expandPlaceholders:$% with:dict. |
14768 | 7315 |
" |
7316 |
||
23067 | 7317 |
"Modified: / 01-07-1997 / 00:53:24 / cg" |
7318 |
"Modified: / 14-06-2018 / 11:46:18 / Claus Gittinger" |
|
7319 |
! |
|
7320 |
||
7321 |
expandPlaceholders:escapeCharacter with:argArrayOrDictionary ignoreNumericEscapes:ignoreNumericEscapes on:aStream |
|
14765 | 7322 |
"this is the generic version of the old %-escaping method, allowing for an arbitrary |
7323 |
escape character to be used (typically $$ or $% are effectively used). |
|
18425 | 7324 |
|
14765 | 7325 |
Write the receiver to aStream, where all %i escapes are |
23067 | 7326 |
replaced by corresponding arguments' printStrings from the argArray. |
14765 | 7327 |
I.e. 'hello %1; how is %2' expandPlaceholdersWith:#('world' 'this') results |
7328 |
in the new string 'hello world; how is this'. |
|
18425 | 7329 |
|
17192 | 7330 |
As an extension, the argument may also be a dictionary, providing values for symbolic keys. |
14765 | 7331 |
In this case, %a .. %z and %(...) are also allowed. |
7332 |
(%1..%9 require a numeric key in the dictionary, however) |
|
23067 | 7333 |
|
7334 |
Also, values in argArrayOrDictionary may be blocks. |
|
18425 | 7335 |
|
14765 | 7336 |
To get a '%' character, use a '%%'-escape. |
7337 |
To get an integer-indexed placeHolder followed by another digit, |
|
7338 |
or an index > 9, you must use %(digit). |
|
18425 | 7339 |
|
14765 | 7340 |
See also bindWith:... for VisualAge compatibility. |
23067 | 7341 |
Use %<cr> to insert a CR and %<tab> to insert a TAB. |
7342 |
||
7343 |
ignoreNumericEscapes controls if %<nr> escapes are expanded or not. |
|
7344 |
This is required for Windows batch-script expansion, where %<nr> should be left |
|
7345 |
unchanged." |
|
14765 | 7346 |
|
23603 | 7347 |
^ self |
7348 |
expandPlaceholders:escapeCharacter |
|
7349 |
with:argArrayOrDictionary |
|
7350 |
ignoreNumericEscapes:ignoreNumericEscapes |
|
7351 |
requireParentheses:true |
|
7352 |
on:aStream |
|
14765 | 7353 |
|
7354 |
" |
|
7355 |
String streamContents:[:s| |
|
20874 | 7356 |
'hello %1' expandPlaceholders:$% with:#('world') on:s. |
7357 |
s cr. |
|
7358 |
'hello $1; how is $2' expandPlaceholders:$$ with:#('world' 'this') on:s. |
|
7359 |
s cr. |
|
7360 |
'hello %2; how is %1' expandPlaceholders:$% with:#('world' 'this') on:s. |
|
7361 |
s cr. |
|
7362 |
'%1 plus %2 gives %3 ' expandPlaceholders:$% with:#(4 5 9) on:s. |
|
7363 |
s cr. |
|
7364 |
'%%(1)0 gives %(1)0' expandPlaceholders:$% with:#(123) on:s. |
|
7365 |
s cr. |
|
7366 |
'%%10 gives %10' expandPlaceholders:$% with:#(123) on:s. |
|
7367 |
s cr. |
|
7368 |
'%%(10) gives %(10) %<cr>%<tab>next line' expandPlaceholders:$% with:#(123) on:s. |
|
7369 |
s cr. |
|
7370 |
'%%test gives %test' expandPlaceholders:$% with:#(123) on:s. |
|
7371 |
s cr. |
|
7372 |
'|%%<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
|
7373 |
] |
14765 | 7374 |
" |
7375 |
||
23325 | 7376 |
"without xlation dictionary: |
7377 |
'hello %1' expandPlaceholdersWith:nil. |
|
7378 |
'hello%<cr> %1' expandPlaceholdersWith:nil. |
|
7379 |
" |
|
7380 |
||
14765 | 7381 |
" |
7382 |
|dict| |
|
7383 |
||
7384 |
dict := Dictionary new. |
|
7385 |
dict at:1 put:'one'. |
|
7386 |
dict at:$a put:'AAAAA'. |
|
7387 |
dict at:$b put:[ Time now ]. |
|
7388 |
String streamContents:[:s| |
|
20874 | 7389 |
'hello $1 $a $b' expandPlaceholders:$$ with:dict on:s. |
14765 | 7390 |
]. |
7391 |
" |
|
7392 |
||
18425 | 7393 |
"using blocks: |
7394 |
|dict| |
|
7395 |
||
7396 |
dict := Dictionary new. |
|
7397 |
dict at:'time' put:[Time now printString]. |
|
7398 |
dict at:'date' put:[Date today printString]. |
|
7399 |
String streamContents:[:s| |
|
20874 | 7400 |
'it is $(time) $(date)' expandPlaceholders:$$ with:dict on:s. |
18425 | 7401 |
]. |
7402 |
" |
|
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
7403 |
|
23067 | 7404 |
"Created: / 14-06-2018 / 11:44:08 / Claus Gittinger" |
23603 | 7405 |
"Modified (comment): / 14-01-2019 / 17:44:02 / Claus Gittinger" |
7406 |
! |
|
7407 |
||
7408 |
expandPlaceholders:escapeCharacter with:argArrayOrDictionary ignoreNumericEscapes:ignoreNumericEscapes requireParentheses:requireParentheses on:aStream |
|
7409 |
"this is the generic version of the old %-escaping method, allowing for an arbitrary |
|
7410 |
escape character to be used (typically $$ or $% are effectively used). |
|
7411 |
||
7412 |
Write the receiver to aStream, where all %i escapes are |
|
7413 |
replaced by corresponding arguments' printStrings from the argArray. |
|
7414 |
I.e. 'hello %1; how is %2' expandPlaceholdersWith:#('world' 'this') results |
|
7415 |
in the new string 'hello world; how is this'. |
|
7416 |
||
7417 |
As an extension, the argument may also be a dictionary, providing values for symbolic keys. |
|
7418 |
In this case, %a .. %z and %(...) are also allowed. |
|
7419 |
(%1..%9 require a numeric key in the dictionary, however) |
|
7420 |
||
7421 |
Also, values in argArrayOrDictionary may be blocks. |
|
7422 |
||
7423 |
To get a '%' character, use a '%%'-escape. |
|
7424 |
To get an integer-indexed placeHolder followed by another digit, |
|
7425 |
or an index > 9, you must use %(digit). |
|
7426 |
||
7427 |
See also bindWith:... for VisualAge compatibility. |
|
7428 |
Use %<cr> to insert a CR and %<tab> to insert a TAB. |
|
7429 |
||
7430 |
ignoreNumericEscapes controls if %<nr> escapes are expanded or not. |
|
7431 |
This is required for Windows batch-script expansion, where %<nr> should be left unchanged. |
|
7432 |
||
7433 |
requireParentheses controls if $abc is allowed or not. |
|
7434 |
If true, multi-character replacements need to be parenthized as $(abc); |
|
7435 |
if false, you can also write $abc. |
|
7436 |
" |
|
7437 |
||
7438 |
|next v key numericKey |
|
7439 |
idx "{ SmallInteger }" |
|
7440 |
idx2 "{ SmallInteger }" |
|
7441 |
start "{ SmallInteger }" |
|
7442 |
stop "{ SmallInteger }"| |
|
7443 |
||
7444 |
stop := self size. |
|
7445 |
start := 1. |
|
7446 |
[start <= stop] whileTrue:[ |
|
7447 |
idx := self indexOf:escapeCharacter startingAt:start. |
|
7448 |
(idx == 0 or:[idx == stop]) ifTrue:[ |
|
7449 |
aStream nextPutAll:self startingAt:start to:stop. |
|
7450 |
^ self. |
|
7451 |
]. |
|
7452 |
"found an escapeCharacter" |
|
7453 |
aStream nextPutAll:self startingAt:start to:(idx - 1). |
|
7454 |
next := self at:(idx + 1). |
|
7455 |
(next == escapeCharacter) ifTrue:[ |
|
7456 |
"doubled escapeCharacter" |
|
7457 |
aStream nextPut:escapeCharacter. |
|
7458 |
] ifFalse:[ |
|
7459 |
"%<...> " |
|
7460 |
next == $< ifTrue:[ |
|
7461 |
idx2 := self indexOf:$> startingAt:idx+2. |
|
7462 |
key := self copyFrom:idx+2 to:idx2-1. |
|
7463 |
idx := idx2 - 1. |
|
7464 |
key := key asSymbolIfInterned. |
|
7465 |
(#(cr tab nl return lf ff null) includesIdentical:key) ifTrue:[ |
|
7466 |
aStream nextPut:(Character perform:key). |
|
7467 |
]. |
|
7468 |
] ifFalse:[ |
|
7469 |
argArrayOrDictionary isNil ifTrue:[ |
|
7470 |
aStream nextPut:escapeCharacter. |
|
7471 |
aStream nextPut:next. |
|
7472 |
] ifFalse:[ |
|
7473 |
(next isDigit and:[ignoreNumericEscapes not]) ifTrue:[ |
|
7474 |
v := argArrayOrDictionary at:(next digitValue) ifAbsent:'' |
|
7475 |
] ifFalse:[ |
|
7476 |
next == $( ifTrue:[ |
|
7477 |
idx2 := self indexOf:$) startingAt:idx+2. |
|
7478 |
self assert:(idx2 > 0). |
|
7479 |
key := self copyFrom:idx+2 to:idx2-1. |
|
7480 |
idx := idx2 - 1. |
|
7481 |
||
7482 |
(key isNumeric |
|
7483 |
and:[(numericKey := Integer readFrom:key onError:nil) notNil |
|
7484 |
and:[(argArrayOrDictionary includesKey:numericKey)]]) ifTrue:[ |
|
7485 |
v := argArrayOrDictionary at:numericKey ifAbsent:'' |
|
7486 |
] ifFalse:[ |
|
7487 |
argArrayOrDictionary isBlock ifTrue:[ |
|
7488 |
v := argArrayOrDictionary value:key |
|
7489 |
] ifFalse:[ |
|
7490 |
(argArrayOrDictionary includesKey:key) ifTrue:[ |
|
7491 |
v := argArrayOrDictionary at:key |
|
7492 |
] ifFalse:[ |
|
7493 |
key := key asSymbolIfInternedOrSelf. |
|
7494 |
(argArrayOrDictionary includesKey:key) ifTrue:[ |
|
7495 |
v := argArrayOrDictionary at:key |
|
7496 |
] ifFalse:[ |
|
7497 |
(key size == 1 and:[ argArrayOrDictionary includesKey:(key at:1)]) ifTrue:[ |
|
7498 |
v := argArrayOrDictionary at:(key at:1) |
|
7499 |
] ifFalse:[ |
|
7500 |
v := '' |
|
7501 |
] |
|
7502 |
]. |
|
7503 |
]. |
|
7504 |
]. |
|
7505 |
]. |
|
7506 |
] ifFalse:[ |
|
7507 |
(next isLetter and:[argArrayOrDictionary isSequenceable not "is a Dictionary"]) ifTrue:[ |
|
7508 |
"so next is a non-numeric single character." |
|
7509 |
requireParentheses ifTrue:[ |
|
7510 |
key := next. |
|
7511 |
] ifFalse:[ |
|
7512 |
idx2 := self findFirst:[:ch | (ch isLetterOrDigit or:[ch == $_]) not] startingAt:idx+2. |
|
7513 |
idx2 == 0 ifTrue:[idx2 := self size + 1]. |
|
7514 |
key := self copyFrom:idx+1 to:idx2-1. |
|
7515 |
idx := idx2 - 2. |
|
7516 |
]. |
|
7517 |
||
7518 |
argArrayOrDictionary isBlock ifTrue:[ |
|
7519 |
v := argArrayOrDictionary value:key |
|
7520 |
] ifFalse:[ |
|
7521 |
v := argArrayOrDictionary |
|
7522 |
at:key |
|
7523 |
ifAbsent:[ |
|
7524 |
"try symbol or string instead of character" |
|
7525 |
argArrayOrDictionary |
|
7526 |
at:key asString asSymbolIfInternedOrSelf |
|
7527 |
ifAbsent:[escapeCharacter asString , key]. |
|
7528 |
]. |
|
7529 |
]. |
|
7530 |
] ifFalse:[ |
|
7531 |
v := String with:$% with:next. |
|
7532 |
]. |
|
7533 |
] |
|
7534 |
]. |
|
7535 |
"/ v notNil ifTrue:[ |
|
7536 |
v isBlock ifTrue:[ |
|
7537 |
v := v value |
|
7538 |
]. |
|
7539 |
||
7540 |
v printOn:aStream. |
|
7541 |
"/ ]. |
|
7542 |
] |
|
7543 |
]. |
|
7544 |
]. |
|
7545 |
start := idx + 2 |
|
7546 |
]. |
|
7547 |
||
7548 |
" |
|
7549 |
'hello %1' expandPlaceholders:$% with:#('world') on:Transcript. |
|
7550 |
'hello %a%<cr>' expandPlaceholders:$% |
|
7551 |
with:(Dictionary new at:'a' put:'world';yourself) |
|
7552 |
on:Transcript. |
|
7553 |
'hello %aa%<cr>' expandPlaceholders:$% with:(Dictionary new at:'a' put:'world';yourself) on:Transcript. |
|
7554 |
'hello %(aa)%<cr>' expandPlaceholders:$% with:(Dictionary new at:'aa' put:'world';yourself) on:Transcript. |
|
7555 |
'hello %aa%<cr>' expandPlaceholders:$% with:(Dictionary new at:'aa' put:'world';yourself) ignoreNumericEscapes:true requireParentheses:false on:Transcript. |
|
7556 |
||
7557 |
String streamContents:[:s| |
|
7558 |
'hello %1' expandPlaceholders:$% with:#('world') on:s. |
|
7559 |
s cr. |
|
7560 |
'hello $1; how is $2' expandPlaceholders:$$ with:#('world' 'this') on:s. |
|
7561 |
s cr. |
|
7562 |
'hello %2; how is %1' expandPlaceholders:$% with:#('world' 'this') on:s. |
|
7563 |
s cr. |
|
7564 |
'%1 plus %2 gives %3 ' expandPlaceholders:$% with:#(4 5 9) on:s. |
|
7565 |
s cr. |
|
7566 |
'%%(1)0 gives %(1)0' expandPlaceholders:$% with:#(123) on:s. |
|
7567 |
s cr. |
|
7568 |
'%%10 gives %10' expandPlaceholders:$% with:#(123) on:s. |
|
7569 |
s cr. |
|
7570 |
'%%(10) gives %(10) %<cr>%<tab>next line' expandPlaceholders:$% with:#(123) on:s. |
|
7571 |
s cr. |
|
7572 |
'%%test gives %test' expandPlaceholders:$% with:#(123) on:s. |
|
7573 |
s cr. |
|
7574 |
'|%%<tab>|%%1|%%<cr>| gives |%<tab>|%1|%<cr>|' expandPlaceholders:$% with:#(foo) on:s. |
|
7575 |
] |
|
7576 |
" |
|
7577 |
||
7578 |
"without xlation dictionary: |
|
7579 |
'hello %1' expandPlaceholdersWith:nil. |
|
7580 |
'hello%<cr> %1' expandPlaceholdersWith:nil. |
|
7581 |
" |
|
7582 |
||
7583 |
" |
|
7584 |
|dict| |
|
7585 |
||
7586 |
dict := Dictionary new. |
|
7587 |
dict at:1 put:'one'. |
|
7588 |
dict at:$a put:'AAAAA'. |
|
7589 |
dict at:$b put:[ Time now ]. |
|
7590 |
String streamContents:[:s| |
|
7591 |
'hello $1 $a $b' expandPlaceholders:$$ with:dict on:s. |
|
7592 |
]. |
|
7593 |
" |
|
7594 |
||
7595 |
"using blocks: |
|
7596 |
|dict| |
|
7597 |
||
7598 |
dict := Dictionary new. |
|
7599 |
dict at:'time' put:[Time now printString]. |
|
7600 |
dict at:'date' put:[Date today printString]. |
|
7601 |
String streamContents:[:s| |
|
7602 |
'it is $(time) $(date)' expandPlaceholders:$$ with:dict on:s. |
|
7603 |
]. |
|
7604 |
" |
|
7605 |
||
7606 |
"Created: / 14-01-2019 / 17:43:03 / Claus Gittinger" |
|
23067 | 7607 |
! |
7608 |
||
7609 |
expandPlaceholders:escapeCharacter with:argArrayOrDictionary on:aStream |
|
7610 |
"this is the generic version of the old %-escaping method, allowing for an arbitrary |
|
7611 |
escape character to be used (typically $$ or $% are effectively used). |
|
7612 |
||
7613 |
Write the receiver to aStream, where all %i escapes are |
|
7614 |
replaced by corresponding arguments' printStrings from the argArrayOrDictionary. |
|
7615 |
I.e. 'hello %1; how is %2' expandPlaceholdersWith:#('world' 'this') results |
|
7616 |
in the new string 'hello world; how is this'. |
|
7617 |
||
7618 |
As an extension, the argument may also be a dictionary, providing values for symbolic keys. |
|
7619 |
In this case, %a .. %z and %(...) are also allowed. |
|
7620 |
(%1..%9 require a numeric key in the dictionary, however) |
|
7621 |
Also, the values in argArrayOrDictionary may be blocks. |
|
7622 |
||
7623 |
To get a '%' character, use a '%%'-escape. |
|
7624 |
To get an integer-indexed placeHolder followed by another digit, |
|
7625 |
or an index > 9, you must use %(digit). |
|
7626 |
||
7627 |
See also bindWith:... for VisualAge compatibility. |
|
7628 |
Use %<cr> to insert a CR and %<tab> to insert a TAB." |
|
7629 |
||
7630 |
^ self |
|
7631 |
expandPlaceholders:escapeCharacter |
|
7632 |
with:argArrayOrDictionary |
|
7633 |
ignoreNumericEscapes:false |
|
23603 | 7634 |
requireParentheses:true |
23067 | 7635 |
on:aStream |
7636 |
||
7637 |
" |
|
23603 | 7638 |
'hello %1' expandPlaceholders:$% with:#('world') on:Transcript. |
7639 |
||
23067 | 7640 |
String streamContents:[:s| |
7641 |
'hello %1' expandPlaceholders:$% with:#('world') on:s. |
|
7642 |
s cr. |
|
7643 |
'hello $1; how is $2' expandPlaceholders:$$ with:#('world' 'this') on:s. |
|
7644 |
s cr. |
|
7645 |
'hello %2; how is %1' expandPlaceholders:$% with:#('world' 'this') on:s. |
|
7646 |
s cr. |
|
7647 |
'%1 plus %2 gives %3 ' expandPlaceholders:$% with:#(4 5 9) on:s. |
|
7648 |
s cr. |
|
7649 |
'%%(1)0 gives %(1)0' expandPlaceholders:$% with:#(123) on:s. |
|
7650 |
s cr. |
|
7651 |
'%%10 gives %10' expandPlaceholders:$% with:#(123) on:s. |
|
7652 |
s cr. |
|
7653 |
'%%(10) gives %(10) %<cr>%<tab>next line' expandPlaceholders:$% with:#(123) on:s. |
|
7654 |
s cr. |
|
7655 |
'%%test gives %test' expandPlaceholders:$% with:#(123) on:s. |
|
7656 |
s cr. |
|
7657 |
'|%%<tab>|%%1|%%<cr>| gives |%<tab>|%1|%<cr>|' expandPlaceholders:$% with:#(foo) on:s. |
|
7658 |
] |
|
7659 |
" |
|
7660 |
||
7661 |
" |
|
7662 |
|dict| |
|
7663 |
||
7664 |
dict := Dictionary new. |
|
7665 |
dict at:1 put:'one'. |
|
7666 |
dict at:$a put:'AAAAA'. |
|
7667 |
dict at:$b put:[ Time now ]. |
|
7668 |
String streamContents:[:s| |
|
7669 |
'hello $1 $a $b' expandPlaceholders:$$ with:dict on:s. |
|
7670 |
]. |
|
7671 |
" |
|
7672 |
||
7673 |
"using blocks: |
|
7674 |
|dict| |
|
7675 |
||
7676 |
dict := Dictionary new. |
|
7677 |
dict at:'time' put:[Time now printString]. |
|
7678 |
dict at:'date' put:[Date today printString]. |
|
7679 |
String streamContents:[:s| |
|
7680 |
'it is $(time) $(date)' expandPlaceholders:$$ with:dict on:s. |
|
7681 |
]. |
|
7682 |
" |
|
7683 |
||
14765 | 7684 |
"Modified: / 18-11-2010 / 15:43:28 / cg" |
23603 | 7685 |
"Modified (comment): / 14-01-2019 / 18:02:59 / Claus Gittinger" |
14765 | 7686 |
! |
7687 |
||
9192 | 7688 |
expandPlaceholdersWith:argArrayOrDictionary |
608 | 7689 |
"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
|
7690 |
replaced by corresponding arguments' printStrings from the argArrayOrDictionary. |
608 | 7691 |
I.e. 'hello %1; how is %2' expandPlaceholdersWith:#('world' 'this') results |
7692 |
in the new string 'hello world; how is this'. |
|
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
7693 |
|
4410
2bdc0c89a42e
enhanced #exapndPlaceHolders, to allow for a dictionary arg
Claus Gittinger <cg@exept.de>
parents:
4409
diff
changeset
|
7694 |
As an extension, the argument may also be a dictionary, providing |
7313 | 7695 |
values for symbolic keys. |
4842 | 7696 |
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
|
7697 |
(%1..%9 require a numeric key in the dictionary, however) |
18425 | 7698 |
Also, the values in argArrayOrDictionary may be blocks. |
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
7699 |
|
608 | 7700 |
To get a '%' character, use a '%%'-escape. |
7313 | 7701 |
To get an integer-indexed placeHolder followed by another digit, |
7702 |
or an index > 9, you must use %(digit). |
|
608 | 7703 |
See also bindWith:... for VisualAge compatibility." |
7704 |
||
10906
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
7705 |
|stream| |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
7706 |
|
18269 | 7707 |
stream := (TextStream ? CharacterWriteStream) on:(self species uninitializedNew:self size + 20). |
23209 | 7708 |
"/ self expandPlaceholdersWith:argArrayOrDictionary on:stream. |
7709 |
self expandPlaceholders:$% with:argArrayOrDictionary ignoreNumericEscapes:false on:stream. |
|
10906
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
7710 |
^ stream contents. |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
7711 |
|
608 | 7712 |
" |
8874 | 7713 |
'hello %1' expandPlaceholdersWith:#('world') |
7714 |
'hello %1; how is %2' expandPlaceholdersWith:#('world' 'this') |
|
7715 |
'hello %2; how is %1' expandPlaceholdersWith:#('world' 'this') |
|
7716 |
'%1 plus %2 gives %3 ' expandPlaceholdersWith:#(4 5 9) |
|
7717 |
'%%(1)0 gives %(1)0' expandPlaceholdersWith:#(123) |
|
7718 |
'%%10 gives %10' expandPlaceholdersWith:#(123) |
|
7719 |
'%%(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
|
7720 |
'%test gives %1' expandPlaceholdersWith:#(123) |
11028
cb1ee3ddd91a
expandPlaceHolders: care fr Text with emphasis
Claus Gittinger <cg@exept.de>
parents:
11018
diff
changeset
|
7721 |
'bla %1 bla' expandPlaceholdersWith:{ 'hello' allBold } |
cb1ee3ddd91a
expandPlaceHolders: care fr Text with emphasis
Claus Gittinger <cg@exept.de>
parents:
11018
diff
changeset
|
7722 |
'bla %1 bla' expandPlaceholdersWith:{ 'hello' } |
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
7723 |
('bla %1 bla' withColor:Color red) |
20874 | 7724 |
expandPlaceholdersWith:{ 'hello' } |
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
7725 |
('bla %1 bla' withColor:Color red) |
20874 | 7726 |
expandPlaceholdersWith:{ 'hello' withColor:Color blue } |
608 | 7727 |
" |
1026
455c9f609f4d
care for 16bit strings in expandPlaceHolders
Claus Gittinger <cg@exept.de>
parents:
1025
diff
changeset
|
7728 |
|
4410
2bdc0c89a42e
enhanced #exapndPlaceHolders, to allow for a dictionary arg
Claus Gittinger <cg@exept.de>
parents:
4409
diff
changeset
|
7729 |
" |
2bdc0c89a42e
enhanced #exapndPlaceHolders, to allow for a dictionary arg
Claus Gittinger <cg@exept.de>
parents:
4409
diff
changeset
|
7730 |
|dict| |
2bdc0c89a42e
enhanced #exapndPlaceHolders, to allow for a dictionary arg
Claus Gittinger <cg@exept.de>
parents:
4409
diff
changeset
|
7731 |
|
2bdc0c89a42e
enhanced #exapndPlaceHolders, to allow for a dictionary arg
Claus Gittinger <cg@exept.de>
parents:
4409
diff
changeset
|
7732 |
dict := Dictionary new. |
2bdc0c89a42e
enhanced #exapndPlaceHolders, to allow for a dictionary arg
Claus Gittinger <cg@exept.de>
parents:
4409
diff
changeset
|
7733 |
dict at:1 put:'one'. |
2bdc0c89a42e
enhanced #exapndPlaceHolders, to allow for a dictionary arg
Claus Gittinger <cg@exept.de>
parents:
4409
diff
changeset
|
7734 |
dict at:$a put:'AAAAA'. |
8794
a5df33935142
nindWith supports blocks in the value dictionary
Claus Gittinger <cg@exept.de>
parents:
8710
diff
changeset
|
7735 |
dict at:$b put:[ Time now ]. |
12316
3e78905d70a9
comment/format in: #expandPlaceholdersWith:
Claus Gittinger <cg@exept.de>
parents:
12196
diff
changeset
|
7736 |
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
|
7737 |
'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
|
7738 |
" |
2bdc0c89a42e
enhanced #exapndPlaceHolders, to allow for a dictionary arg
Claus Gittinger <cg@exept.de>
parents:
4409
diff
changeset
|
7739 |
|
23209 | 7740 |
"Modified: / 01-07-1997 / 00:53:24 / cg" |
7741 |
"Modified: / 14-07-2018 / 09:23:31 / Claus Gittinger" |
|
9192 | 7742 |
! |
7743 |
||
10906
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
7744 |
expandPlaceholdersWith:argArrayOrDictionary on:aStream |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
7745 |
"write the receiver to aStream, where all %i escapes are |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
7746 |
replaced by corresponding arguments' printStrings from the argArrayOrDictionary. |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
7747 |
I.e. 'hello %1; how is %2' expandPlaceholdersWith:#('world' 'this') results |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
7748 |
in the new string 'hello world; how is this'. |
18425 | 7749 |
|
10906
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
7750 |
As an extension, the argument may also be a dictionary, providing |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
7751 |
values for symbolic keys. |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
7752 |
In this case, %a .. %z and %(...) are also allowed. |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
7753 |
(%1..%9 require a numeric key in the dictionary, however) |
18425 | 7754 |
Also, the values in argArrayOrDictionary may be blocks. |
7755 |
||
10906
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
7756 |
To get a '%' character, use a '%%'-escape. |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
7757 |
To get an integer-indexed placeHolder followed by another digit, |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
7758 |
or an index > 9, you must use %(digit). |
18425 | 7759 |
|
10935 | 7760 |
See also bindWith:... for VisualAge compatibility. |
10939
d0ca31c1d1e9
#expandPlaceHoldersWith: handle %<cr> %<tab> etc
Stefan Vogel <sv@exept.de>
parents:
10935
diff
changeset
|
7761 |
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
|
7762 |
|
23067 | 7763 |
^ self expandPlaceholders:$% with:argArrayOrDictionary ignoreNumericEscapes:false on:aStream |
10906
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
7764 |
|
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
7765 |
" |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
7766 |
String streamContents:[:s| |
20874 | 7767 |
'hello %1' expandPlaceholdersWith:#('world') on:s. |
7768 |
s cr. |
|
7769 |
'hello %1; how is %2' expandPlaceholdersWith:#('world' 'this') on:s. |
|
7770 |
s cr. |
|
7771 |
'hello %2; how is %1' expandPlaceholdersWith:#('world' 'this') on:s. |
|
7772 |
s cr. |
|
7773 |
'%1 plus %2 gives %3 ' expandPlaceholdersWith:#(4 5 9) on:s. |
|
7774 |
s cr. |
|
7775 |
'%%(1)0 gives %(1)0' expandPlaceholdersWith:#(123) on:s. |
|
7776 |
s cr. |
|
7777 |
'%%10 gives %10' expandPlaceholdersWith:#(123) on:s. |
|
7778 |
s cr. |
|
7779 |
'%%(10) gives %(10) %<cr>%<tab>next line' expandPlaceholdersWith:#(123) on:s. |
|
7780 |
s cr. |
|
7781 |
'%test gives %1' expandPlaceholdersWith:#(123) on:s. |
|
10906
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
7782 |
] |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
7783 |
" |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
7784 |
|
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
7785 |
" |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
7786 |
|dict| |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
7787 |
|
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
7788 |
dict := Dictionary new. |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
7789 |
dict at:1 put:'one'. |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
7790 |
dict at:$a put:'AAAAA'. |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
7791 |
dict at:$b put:[ Time now ]. |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
7792 |
String streamContents:[:s| |
20874 | 7793 |
'hello %1 %a %b' expandPlaceholdersWith:dict on:s. |
10906
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
7794 |
]. |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
7795 |
" |
13008
a27647c36b31
changed: #expandPlaceholdersWith:on:
Claus Gittinger <cg@exept.de>
parents:
12925
diff
changeset
|
7796 |
|
13138
d2eb0eba5907
changed: #expandPlaceholdersWith:on:
Claus Gittinger <cg@exept.de>
parents:
13137
diff
changeset
|
7797 |
"Modified: / 18-11-2010 / 15:43:28 / cg" |
23067 | 7798 |
"Modified: / 14-06-2018 / 11:45:36 / Claus Gittinger" |
10906
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
7799 |
! |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
7800 |
|
22929 | 7801 |
extractPlaceHolders:escapeCharacter |
7802 |
"returns a list of placeholder keys of the form %i, |
|
7803 |
where i is either a single digit (as in %1, %2) |
|
7804 |
or a single letter (as in %a, %z) |
|
7805 |
or a word-key, as in %(one), %(fooBar). |
|
7806 |
For numeric keys, the returned collection contains integers; |
|
7807 |
for non-numeric ones, it includes strings." |
|
7808 |
||
7809 |
|setOfKeys loggingDictionary| |
|
7810 |
||
7811 |
setOfKeys := Set new. |
|
7812 |
loggingDictionary := Plug new. |
|
7813 |
loggingDictionary respondTo:#at: with:[:key | setOfKeys add:key. key asString]. |
|
7814 |
loggingDictionary respondTo:#at:ifAbsent: with:[:key :def | setOfKeys add:key. key asString]. |
|
7815 |
loggingDictionary respondTo:#includesKey: with:[:key | setOfKeys add:key. true]. |
|
7816 |
loggingDictionary respondTo:#isSequenceable with:[false]. |
|
7817 |
self expandPlaceholders:escapeCharacter with:loggingDictionary. |
|
7818 |
^ setOfKeys |
|
7819 |
||
7820 |
" |
|
7821 |
self assert:('hello %1 and %2' extractPlaceHolders:$%) asSet = #(1 2) asSet |
|
7822 |
self assert:('hello %9 and %(10)' extractPlaceHolders:$%) asSet = #(9 10) asSet |
|
7823 |
self assert:('hello %a and %(foo) and %1' extractPlaceHolders:$%) asSet = #(1 'a' 'foo') asSet |
|
7824 |
self assert:('hello %a and %(foo) and %1' extractPlaceHolders:$$) asSet = #() asSet |
|
7825 |
" |
|
7826 |
! |
|
7827 |
||
9230 | 7828 |
firstLine |
14405
1d0d99a95bba
comment/format in:7 methods
Claus Gittinger <cg@exept.de>
parents:
14168
diff
changeset
|
7829 |
"return the first line of a multiline string" |
1d0d99a95bba
comment/format in:7 methods
Claus Gittinger <cg@exept.de>
parents:
14168
diff
changeset
|
7830 |
|
1d0d99a95bba
comment/format in:7 methods
Claus Gittinger <cg@exept.de>
parents:
14168
diff
changeset
|
7831 |
^ self asCollectionOfSubCollectionsSeparatedBy:(Character cr) do:[:line | ^ line]. |
9230 | 7832 |
|
7833 |
" |
|
10703
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
7834 |
'hello' firstLine |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
7835 |
'1\2\3' withCRs firstLine |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
7836 |
'\1\2\3' withCRs firstLine |
9230 | 7837 |
" |
7838 |
! |
|
7839 |
||
13987 | 7840 |
tokensBasedOn:aCharacter |
7841 |
"this is an ST-80 alias for the ST/X method |
|
20874 | 7842 |
asCollectionOfSubstringsSeparatedBy:" |
13987 | 7843 |
|
7844 |
^ self asCollectionOfSubstringsSeparatedBy:aCharacter |
|
7845 |
||
7846 |
" |
|
7847 |
'hello:world:isnt:this nice' tokensBasedOn:$: |
|
7848 |
'foo,bar,baz' tokensBasedOn:$, |
|
7849 |
'/etc/passwd' asFilename readStream nextLine tokensBasedOn:$: |
|
7850 |
" |
|
7851 |
! |
|
7852 |
||
7853 |
withCEscapes |
|
16863 | 7854 |
"return a new string consisting of receiver's characters |
13987 | 7855 |
with all special and unprintable characters replaced by \X-character escapes. |
7856 |
(similar to the way C-language literal Strings are represented). |
|
7857 |
The resulting string will contain only 7-bit ascii characters. |
|
7858 |
Emphasis is not supported. |
|
7859 |
The following escapes are generated: |
|
20874 | 7860 |
\' single quote character |
7861 |
\dQuote double quote character |
|
7862 |
\r return character |
|
7863 |
\r return character |
|
7864 |
\n newline character |
|
7865 |
\t tab character |
|
7866 |
\\ the \ character itself |
|
7867 |
\xnn two digit hex number defining the characters ascii value |
|
7868 |
\unnnn four digit hex number defining the characters ascii value |
|
7869 |
\Unnnnnnnn eight digit hex number defining the characters ascii value |
|
16863 | 7870 |
This is the opposite of withoutCEscapes. |
17000 | 7871 |
|
7872 |
Sigh: this is named completely wrong (opposite naming of withCRs/witoutCRs), |
|
20874 | 7873 |
but it cannot be changed easily, as these methods are already used heavily |
13987 | 7874 |
" |
7875 |
||
7876 |
|anyEscapeNeeded out seq| |
|
7877 |
||
7878 |
" |
|
7879 |
first, check if any escape is needed and return the receiver unchanged if not |
|
7880 |
" |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
7881 |
anyEscapeNeeded := self |
20874 | 7882 |
contains:[:ch | |
7883 |
((ch codePoint between:32 and:126) not |
|
7884 |
or:[ch == $' or:[ch == $"]]) |
|
7885 |
]. |
|
13987 | 7886 |
anyEscapeNeeded ifFalse:[ ^ self ]. |
7887 |
||
7888 |
self hasChangeOfEmphasis ifTrue:[ self error:'emphasis not supported' ]. |
|
7889 |
||
18269 | 7890 |
out := WriteStream on:(String uninitializedNew:self size-1). |
13987 | 7891 |
|
7892 |
self do:[:ch | |
|
20874 | 7893 |
|cp| |
7894 |
||
7895 |
(ch == $' or:[ch == $"]) ifTrue:[ |
|
7896 |
out nextPut:$\. |
|
7897 |
out nextPut:ch. |
|
7898 |
] ifFalse:[ |
|
7899 |
(ch codePoint between:32 and:126) ifTrue:[ |
|
7900 |
out nextPut:ch |
|
7901 |
] ifFalse:[ |
|
7902 |
ch == Character return ifTrue:[ |
|
7903 |
seq := '\r' |
|
7904 |
] ifFalse:[ ch == Character nl ifTrue:[ |
|
7905 |
seq := '\n' |
|
7906 |
] ifFalse:[ ch == Character tab ifTrue:[ |
|
7907 |
seq := '\t' |
|
7908 |
] ifFalse:[ ch == $\ ifTrue:[ |
|
7909 |
seq := '\\' |
|
7910 |
] ifFalse:[ |
|
7911 |
cp := ch codePoint. |
|
7912 |
cp <= 16rFF ifTrue:[ |
|
22344 | 7913 |
seq := '\x' , (cp printStringRadix:16 size:2 fill:$0) |
20874 | 7914 |
] ifFalse:[ |
7915 |
cp <= 16rFFFF ifTrue:[ |
|
22344 | 7916 |
seq := '\u' , (cp printStringRadix:16 size:4 fill:$0) |
20874 | 7917 |
] ifFalse:[ |
22344 | 7918 |
seq := '\U',(cp printStringRadix:16 size:8 fill:$0) |
20874 | 7919 |
] |
7920 |
] |
|
7921 |
]]]]. |
|
7922 |
out nextPutAll:seq |
|
7923 |
]. |
|
7924 |
]. |
|
13987 | 7925 |
]. |
7926 |
^ out contents |
|
7927 |
||
7928 |
" |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
7929 |
'hello\n\tworld' withoutCEscapes. |
17000 | 7930 |
'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
|
7931 |
('hello ',(Character value:16r1234),' world') withCEscapes |
13987 | 7932 |
" |
7933 |
||
7934 |
"Created: / 25-01-2012 / 11:08:16 / cg" |
|
22344 | 7935 |
"Modified: / 12-11-2017 / 12:39:57 / cg" |
13987 | 7936 |
! |
7937 |
||
9192 | 7938 |
withCRs |
17205 | 7939 |
"return a new string consisting of the receiver's characters |
16757 | 7940 |
with all \-characters replaced by cr-characters. |
7941 |
If there are no backslashes, return the original" |
|
7942 |
||
16759 | 7943 |
^ self copyReplaceAll:$\ with:(Character cr) ifNone:self |
9192 | 7944 |
|
7945 |
" |
|
7946 |
'hello\world' withCRs |
|
7947 |
" |
|
7948 |
||
7949 |
"Modified: / 18.7.1998 / 22:53:02 / cg" |
|
7950 |
! |
|
7951 |
||
7952 |
withEscapes |
|
15405 | 7953 |
<resource: #obsolete> |
13987 | 7954 |
"has been renamed; the name withEscapes is misleading" |
7955 |
||
7956 |
self obsoleteMethodWarning:'use withoutCEscapes'. |
|
7957 |
^ self withoutCEscapes. |
|
7958 |
||
7959 |
"Modified: / 25-01-2012 / 10:42:30 / cg" |
|
9192 | 7960 |
! |
7961 |
||
7962 |
withMatchEscapes |
|
4117 | 7963 |
"return a copy of the receiver with all match characters escaped |
7964 |
by $\ characters (to be usable as a match string). |
|
7965 |
Return the receiver, if there are none." |
|
7966 |
||
5342
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
7967 |
|in out c escape| |
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
7968 |
|
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
7969 |
escape := self class matchEscapeCharacter. |
4117 | 7970 |
|
7971 |
in := self readStream. |
|
7986 | 7972 |
out := WriteStream on:(self species new:self size). |
4117 | 7973 |
[in atEnd] whileFalse:[ |
20874 | 7974 |
c := in next. |
7975 |
(c == escape or:['*[#' includes:c]) ifTrue:[ |
|
7976 |
out nextPut:$\. |
|
7977 |
]. |
|
7978 |
out nextPut:c. |
|
4117 | 7979 |
]. |
7980 |
^ out contents. |
|
7981 |
||
7982 |
" |
|
8874 | 7983 |
'*foo' withMatchEscapes |
7984 |
'\*foo' withMatchEscapes |
|
7985 |
'*foo' withMatchEscapes |
|
7986 |
'\\*foo' withMatchEscapes |
|
7987 |
'foo*' withMatchEscapes |
|
7988 |
'foo\*' withMatchEscapes |
|
7989 |
'foo\' withMatchEscapes |
|
7990 |
'f*o*o' withMatchEscapes |
|
4117 | 7991 |
" |
7992 |
||
7993 |
"Modified: 2.4.1997 / 18:13:04 / cg" |
|
9192 | 7994 |
! |
7995 |
||
14992 | 7996 |
withSeparatorsCompacted |
7997 |
"return a new string with each sequence of whiteSpace replaced by a single space character. |
|
7998 |
Preserves a leading/trailing space." |
|
7999 |
||
8000 |
^ self species streamContents:[:s | |
|
20874 | 8001 |
|skipping| |
8002 |
||
8003 |
skipping := false. |
|
8004 |
1 to:self size do:[:idx | |
|
8005 |
|char| |
|
8006 |
||
8007 |
char := self at:idx. |
|
8008 |
char isSeparator ifFalse:[ |
|
8009 |
s nextPut:char. |
|
8010 |
skipping := false. |
|
8011 |
] ifTrue:[ |
|
8012 |
skipping ifFalse:[ |
|
8013 |
s nextPut:(Character space). |
|
8014 |
skipping := true |
|
8015 |
]. |
|
8016 |
] |
|
8017 |
] |
|
14992 | 8018 |
] |
8019 |
||
8020 |
" |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
8021 |
'hello wwww' withSeparatorsCompacted |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
8022 |
'hello wwww' withSeparatorsCompacted |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
8023 |
' hello wwww' withSeparatorsCompacted |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
8024 |
' hello wwww ' withSeparatorsCompacted |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
8025 |
' hello wwww ' withSeparatorsCompacted |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
8026 |
'hel lo www w' withSeparatorsCompacted |
14992 | 8027 |
" |
8028 |
! |
|
8029 |
||
18464 | 8030 |
withSeparatorsReplacedBy:replacementCharacter |
8031 |
"return a new string with each separator (whitespace) replaced by replacementCharacter. |
|
8032 |
Typically used with space as replacementCharacter" |
|
8033 |
||
8034 |
^ self species streamContents:[:s | |
|
20874 | 8035 |
self do:[:ch | |
8036 |
ch isSeparator ifTrue:[ |
|
8037 |
s nextPut:replacementCharacter |
|
8038 |
] ifFalse:[ |
|
8039 |
s nextPut:ch. |
|
8040 |
] |
|
8041 |
] |
|
18464 | 8042 |
] |
8043 |
||
8044 |
" |
|
8045 |
'hello wwww' withSeparatorsReplacedBy:$* |
|
8046 |
'hello ww ww' withSeparatorsReplacedBy:$* |
|
8047 |
' hello wwww' withSeparatorsReplacedBy:$* |
|
8048 |
'hel lo www w' withSeparatorsReplacedBy:$* |
|
8049 |
'hel |
|
8050 |
lo www |
|
8051 |
w' withSeparatorsReplacedBy:$* |
|
8052 |
" |
|
8053 |
! |
|
8054 |
||
9192 | 8055 |
withTabs |
19464 | 8056 |
"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
|
8057 |
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
|
8058 |
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
|
8059 |
otherwise a new string is returned. |
8874 | 8060 |
Limitation: only the very first spaces are replaced |
20874 | 8061 |
(i.e. if the receiver contains newLine characters, |
8062 |
no tabs are inserted after those lineBreaks)" |
|
8874 | 8063 |
|
8064 |
|idx "{ SmallInteger }" |
|
8065 |
nTabs "{ SmallInteger }" |
|
608 | 8066 |
newString| |
8067 |
||
8068 |
idx := self findFirst:[:c | (c ~~ Character space)]. |
|
8069 |
nTabs := (idx-1) // 8. |
|
5815
4acdd87a77d4
// can return negative numbers.
Stefan Vogel <sv@exept.de>
parents:
5795
diff
changeset
|
8070 |
nTabs <= 0 ifTrue:[^ self]. |
608 | 8071 |
|
8072 |
"any tabs" |
|
15523 | 8073 |
newString := self species new:(self size - (nTabs * 7)). |
608 | 8074 |
newString atAll:(1 to:nTabs) put:(Character tab). |
8075 |
newString replaceFrom:(nTabs + 1) with:self startingAt:(nTabs * 8 + 1). |
|
8076 |
^ newString |
|
8077 |
||
8078 |
" |
|
8874 | 8079 |
'12345678901234567890' withTabs |
8080 |
' 8901234567890' withTabs |
|
8081 |
' 901234567890' withTabs |
|
608 | 8082 |
' 67890' withTabs |
8083 |
' 7890' withTabs |
|
8084 |
' 890' withTabs |
|
8085 |
" |
|
9192 | 8086 |
! |
8087 |
||
8088 |
withTabsExpanded |
|
19464 | 8089 |
"return a string consisting of the receiver's characters, |
8874 | 8090 |
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
|
8091 |
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
|
8092 |
otherwise a new string is returned. |
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
678
diff
changeset
|
8093 |
This does handle multiline strings." |
608 | 8094 |
|
5741
3a6086774d38
added #withTabsExpanded: (i.e. arg to specify tab-column width)
Claus Gittinger <cg@exept.de>
parents:
5739
diff
changeset
|
8095 |
^ self withTabsExpanded:8 |
3a6086774d38
added #withTabsExpanded: (i.e. arg to specify tab-column width)
Claus Gittinger <cg@exept.de>
parents:
5739
diff
changeset
|
8096 |
|
3a6086774d38
added #withTabsExpanded: (i.e. arg to specify tab-column width)
Claus Gittinger <cg@exept.de>
parents:
5739
diff
changeset
|
8097 |
" |
8874 | 8098 |
('1' , Character tab asString , 'x') withTabsExpanded |
8099 |
('12345' , Character tab asString , 'x') withTabsExpanded |
|
8100 |
('123456' , Character tab asString , 'x') withTabsExpanded |
|
8101 |
('1234567' , Character tab asString , 'x') withTabsExpanded |
|
8102 |
('12345678' , Character tab asString , 'x') withTabsExpanded |
|
8103 |
('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
|
8104 |
|
3a6086774d38
added #withTabsExpanded: (i.e. arg to specify tab-column width)
Claus Gittinger <cg@exept.de>
parents:
5739
diff
changeset
|
8105 |
(String with:Character tab |
20874 | 8106 |
with:Character tab |
8107 |
with:$1) withTabsExpanded |
|
5741
3a6086774d38
added #withTabsExpanded: (i.e. arg to specify tab-column width)
Claus Gittinger <cg@exept.de>
parents:
5739
diff
changeset
|
8108 |
|
3a6086774d38
added #withTabsExpanded: (i.e. arg to specify tab-column width)
Claus Gittinger <cg@exept.de>
parents:
5739
diff
changeset
|
8109 |
(String with:Character tab |
20874 | 8110 |
with:$1 |
8111 |
with:Character tab |
|
8112 |
with:$2) withTabsExpanded |
|
5741
3a6086774d38
added #withTabsExpanded: (i.e. arg to specify tab-column width)
Claus Gittinger <cg@exept.de>
parents:
5739
diff
changeset
|
8113 |
|
3a6086774d38
added #withTabsExpanded: (i.e. arg to specify tab-column width)
Claus Gittinger <cg@exept.de>
parents:
5739
diff
changeset
|
8114 |
(String with:Character tab |
20874 | 8115 |
with:$1 |
8116 |
with:Character cr |
|
8117 |
with:Character tab |
|
8118 |
with:$2) withTabsExpanded |
|
5741
3a6086774d38
added #withTabsExpanded: (i.e. arg to specify tab-column width)
Claus Gittinger <cg@exept.de>
parents:
5739
diff
changeset
|
8119 |
" |
3a6086774d38
added #withTabsExpanded: (i.e. arg to specify tab-column width)
Claus Gittinger <cg@exept.de>
parents:
5739
diff
changeset
|
8120 |
|
3a6086774d38
added #withTabsExpanded: (i.e. arg to specify tab-column width)
Claus Gittinger <cg@exept.de>
parents:
5739
diff
changeset
|
8121 |
"Modified: 12.5.1996 / 13:05:10 / cg" |
9192 | 8122 |
! |
8123 |
||
8124 |
withTabsExpanded:numSpaces |
|
19464 | 8125 |
"return a string consisting of the receiver's characters, |
8874 | 8126 |
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
|
8127 |
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
|
8128 |
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
|
8129 |
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
|
8130 |
|
8874 | 8131 |
|col "{ SmallInteger }" |
1381 | 8132 |
str ch |
8133 |
dstIdx "{ SmallInteger }" |
|
8134 |
newSz "{ SmallInteger }" |
|
8135 |
sz "{ SmallInteger }" |
|
8136 |
hasEmphasis e| |
|
608 | 8137 |
|
8138 |
(self includes:(Character tab)) ifFalse:[^ self]. |
|
1381 | 8139 |
|
8140 |
sz := self size. |
|
8141 |
||
8142 |
"/ count the new size first, instead of |
|
8143 |
"/ multiple resizing (better for large strings) |
|
8144 |
||
8145 |
col := 1. newSz := 0. |
|
8146 |
1 to:sz do:[:srcIdx | |
|
20874 | 8147 |
ch := self at:srcIdx. |
8148 |
ch == Character tab ifFalse:[ |
|
8149 |
col := col + 1. |
|
8150 |
newSz := newSz + 1. |
|
8151 |
ch == Character cr ifTrue:[ |
|
8152 |
col := 1 |
|
8153 |
]. |
|
8154 |
] ifTrue:[ |
|
8155 |
(col \\ numSpaces) to:numSpaces do:[:ii | |
|
8156 |
newSz := newSz + 1. |
|
8157 |
col := col + 1 |
|
8158 |
]. |
|
8159 |
] |
|
608 | 8160 |
]. |
1381 | 8161 |
|
8162 |
str := self species new:newSz. |
|
23428 | 8163 |
(str isText and:[self bitsPerCharacter > 8]) ifTrue:[ |
8164 |
str := Text string:(Unicode16String new:newSz) |
|
8165 |
]. |
|
8166 |
||
1381 | 8167 |
hasEmphasis := self hasChangeOfEmphasis. |
8168 |
||
8169 |
col := 1. dstIdx := 1. |
|
8170 |
1 to:sz do:[:srcIdx | |
|
20874 | 8171 |
ch := self at:srcIdx. |
8172 |
||
8173 |
ch == Character tab ifFalse:[ |
|
8174 |
col := col + 1. |
|
8175 |
ch == Character cr ifTrue:[ |
|
8176 |
col := 1 |
|
8177 |
]. |
|
8178 |
hasEmphasis ifTrue:[ |
|
8179 |
e := self emphasisAt:srcIdx. |
|
8180 |
str emphasisAt:dstIdx put:e |
|
8181 |
]. |
|
8182 |
str at:dstIdx put:ch. |
|
8183 |
dstIdx := dstIdx + 1 |
|
8184 |
] ifTrue:[ |
|
8185 |
(col \\ numSpaces) to:numSpaces do:[:ii | |
|
8186 |
str at:dstIdx put:Character space. |
|
8187 |
dstIdx := dstIdx + 1. |
|
8188 |
col := col + 1 |
|
8189 |
]. |
|
8190 |
] |
|
1381 | 8191 |
]. |
8192 |
^ str |
|
608 | 8193 |
|
8194 |
" |
|
8874 | 8195 |
('1' , Character tab asString , 'x') withTabsExpanded |
8196 |
('1' , Character tab asString , 'x') withTabsExpanded:4 |
|
8197 |
('12345' , Character tab asString , 'x') withTabsExpanded |
|
8198 |
('123456' , Character tab asString , 'x') withTabsExpanded |
|
8199 |
('1234567' , Character tab asString , 'x') withTabsExpanded |
|
8200 |
('12345678' , Character tab asString , 'x') withTabsExpanded |
|
8201 |
('123456789' , Character tab asString , 'x') withTabsExpanded |
|
735
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
678
diff
changeset
|
8202 |
|
608 | 8203 |
(String with:Character tab |
20874 | 8204 |
with:Character tab |
8205 |
with:$1) withTabsExpanded |
|
608 | 8206 |
|
8207 |
(String with:Character tab |
|
20874 | 8208 |
with:$1 |
8209 |
with:Character tab |
|
8210 |
with:$2) withTabsExpanded |
|
735
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
678
diff
changeset
|
8211 |
|
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
678
diff
changeset
|
8212 |
(String with:Character tab |
20874 | 8213 |
with:$1 |
8214 |
with:Character cr |
|
8215 |
with:Character tab |
|
8216 |
with:$2) withTabsExpanded |
|
608 | 8217 |
" |
735
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
678
diff
changeset
|
8218 |
|
1381 | 8219 |
"Modified: 12.5.1996 / 13:05:10 / cg" |
9192 | 8220 |
! |
8221 |
||
13987 | 8222 |
withoutAllSpaces |
16855 | 8223 |
"return a copy of the receiver with all whitespace removed" |
8224 |
||
13987 | 8225 |
^ self asCollectionOfWords asStringWith:''. |
8226 |
"/ |col string| |
|
8227 |
"/ |
|
8228 |
"/ col := self asCollectionOfWords. |
|
8229 |
"/ string := String new. |
|
8230 |
"/ col do:[:el | |
|
8231 |
"/ string := string,el |
|
8232 |
"/ ]. |
|
8233 |
"/ ^string |
|
8234 |
||
8235 |
" |
|
8236 |
'hello wwww' withoutAllSpaces |
|
8237 |
'hel lo www w' withoutAllSpaces |
|
8238 |
" |
|
8239 |
||
8240 |
"Modified: / 18.7.1998 / 22:53:08 / cg" |
|
8241 |
! |
|
8242 |
||
8243 |
withoutCEscapes |
|
15628 | 8244 |
"return a new string consisting of receiver's characters |
22205 | 8245 |
with all \X-character escapes replaced by corresponding characters. |
13987 | 8246 |
(similar to the way C-language Strings are converted). |
8247 |
The following escapes are supported: |
|
20874 | 8248 |
\r return character |
8249 |
\n newline character |
|
8250 |
\b backspace character |
|
8251 |
\f formfeed character |
|
8252 |
\t tab character |
|
8253 |
\e escape character |
|
8254 |
\\ the \ character itself |
|
8255 |
\nnn three digit octal number defining the characters ascii value |
|
8256 |
\xnn two digit hex number defining the characters ascii value |
|
8257 |
\unnnn four digit hex number defining the characters unicode value |
|
8258 |
\Unnnnnnnn eight digit hex number defining the characters unicode value |
|
8259 |
\other other |
|
13987 | 8260 |
|
8261 |
Notice, that \' is NOT a valid escape, since the general syntax of |
|
8262 |
string constants is not affected by this method. |
|
8263 |
||
8264 |
Although easily implementable, this is NOT done automatically |
|
8265 |
by the compiler (due to a lack of a language standard for this). |
|
22205 | 8266 |
However, the compiler may detect sends of #withEscapes to string literals |
13987 | 8267 |
and place a modified string constant into the binary/byte-code. |
8268 |
Therefore, no runtime penalty will be payed for using these escapes. |
|
8269 |
(not in pre 2.11 versions) |
|
16863 | 8270 |
|
8271 |
This is the opposite of withCEscapes. |
|
17000 | 8272 |
|
8273 |
Sigh: this is named completely wrong (opposite naming of withCRs/witoutCRs), |
|
20874 | 8274 |
but it cannot be changed easily, as these methods are already used heavily |
13987 | 8275 |
" |
8276 |
||
8277 |
|val "{ SmallInteger }" |
|
8278 |
in out nextChar nDigits| |
|
8279 |
||
8280 |
" |
|
8281 |
first, see if there is any escape; if not, return the receiver unchanged |
|
8282 |
" |
|
8283 |
(self includes:$\) ifFalse:[^ self ]. |
|
8284 |
||
8285 |
self hasChangeOfEmphasis ifTrue:[ self error:'emphasis not supported' ]. |
|
8286 |
out := CharacterWriteStream on:(String new:self size - 1). |
|
8287 |
||
8288 |
in := ReadStream on:self. |
|
8289 |
[in atEnd] whileFalse:[ |
|
20874 | 8290 |
nextChar := in next. |
8291 |
nextChar == $\ ifTrue:[ |
|
8292 |
in atEnd ifTrue:[ |
|
8293 |
] ifFalse:[ |
|
8294 |
nextChar := in next. |
|
8295 |
nextChar == $r ifTrue:[ |
|
8296 |
nextChar := Character return |
|
8297 |
] ifFalse:[ nextChar == $n ifTrue:[ |
|
8298 |
nextChar := Character nl |
|
8299 |
] ifFalse:[ nextChar == $b ifTrue:[ |
|
8300 |
nextChar := Character backspace |
|
8301 |
] ifFalse:[ nextChar == $f ifTrue:[ |
|
8302 |
nextChar := Character newPage |
|
8303 |
] ifFalse:[ nextChar == $t ifTrue:[ |
|
8304 |
nextChar := Character tab |
|
8305 |
] ifFalse:[ nextChar == $e ifTrue:[ |
|
8306 |
nextChar := Character esc |
|
8307 |
] ifFalse:[ |
|
8308 |
nextChar == $0 ifTrue:[ |
|
8309 |
val := 0. |
|
8310 |
nextChar := in peek. |
|
8311 |
nDigits := 1. |
|
8312 |
[nextChar notNil and:[nextChar isDigit and:[nDigits <= 3]]] whileTrue:[ |
|
8313 |
val := (val * 8) + nextChar digitValue. |
|
8314 |
nextChar := in nextPeek. |
|
8315 |
nDigits := nDigits + 1. |
|
8316 |
]. |
|
8317 |
nextChar := Character value:val. |
|
8318 |
] ifFalse:[ |
|
8319 |
val := 0. |
|
8320 |
nextChar == $x ifTrue:[ |
|
8321 |
2 timesRepeat:[ |
|
8322 |
nextChar := in next. |
|
8323 |
val := (val * 16) + nextChar digitValue. |
|
8324 |
]. |
|
8325 |
nextChar := Character value:val. |
|
8326 |
] ifFalse:[ |
|
8327 |
nextChar == $u ifTrue:[ |
|
8328 |
4 timesRepeat:[ |
|
8329 |
nextChar := in next. |
|
8330 |
val := (val * 16) + nextChar digitValue. |
|
8331 |
]. |
|
8332 |
nextChar := Character value:val. |
|
8333 |
] ifFalse:[ |
|
8334 |
nextChar == $U ifTrue:[ |
|
8335 |
8 timesRepeat:[ |
|
8336 |
nextChar := in next. |
|
8337 |
val := (val * 16) + nextChar digitValue. |
|
8338 |
]. |
|
8339 |
nextChar := Character value:val. |
|
8340 |
] |
|
8341 |
] |
|
8342 |
] |
|
8343 |
] |
|
8344 |
]]]]]]. |
|
8345 |
]. |
|
8346 |
]. |
|
8347 |
out nextPut:nextChar. |
|
13987 | 8348 |
]. |
8349 |
^ out contents |
|
8350 |
||
8351 |
" |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
8352 |
'hello world' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
8353 |
'hello\world' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
8354 |
'hello\world\' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
8355 |
'hello world\' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
8356 |
'hello\tworld' withoutCEscapes |
13987 | 8357 |
'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
|
8358 |
'hello\tworld\n' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
8359 |
'hello\010world' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
8360 |
'hello\r\nworld' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
8361 |
'hello\r\n\x08world' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
8362 |
'0\x080' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
8363 |
'0\u12340' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
8364 |
'0\U123456780' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
8365 |
'0\0a' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
8366 |
'0\00a' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
8367 |
'0\000a' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
8368 |
'0\0000a' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
8369 |
'0\00000a' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
8370 |
'0\03770' withoutCEscapes |
13987 | 8371 |
" |
8372 |
||
8373 |
"Created: / 25-01-2012 / 10:41:44 / cg" |
|
22205 | 8374 |
"Modified (comment): / 23-08-2017 / 11:07:43 / mawalch" |
13987 | 8375 |
! |
8376 |
||
9192 | 8377 |
withoutCRs |
19464 | 8378 |
"return a new collection consisting of receiver's elements |
9192 | 8379 |
with all cr-characters replaced by \-characters. |
8380 |
This is the reverse operation of withCRs." |
|
8381 |
||
16759 | 8382 |
^ self copyReplaceAll:(Character cr) with:$\ ifNone:self |
12319
18b80c2ce1e3
oops #withoutSeparators #withoutSpaces were noops
Claus Gittinger <cg@exept.de>
parents:
12317
diff
changeset
|
8383 |
|
9192 | 8384 |
" |
8385 |
'hello |
|
8386 |
world' withoutCRs |
|
8387 |
" |
|
8388 |
||
8389 |
"Modified: / 18.7.1998 / 22:53:08 / cg" |
|
8390 |
! |
|
8391 |
||
8392 |
withoutLeadingSeparators |
|
608 | 8393 |
"return a copy of myself without leading separators. |
8394 |
Notice: this does remove tabs, newline or any other whitespace. |
|
8395 |
Returns an empty string, if the receiver consist only of whitespace." |
|
8396 |
||
8397 |
|index| |
|
8398 |
||
8399 |
index := self indexOfNonSeparatorStartingAt:1. |
|
8400 |
index ~~ 0 ifTrue:[ |
|
20874 | 8401 |
index == 1 ifTrue:[ |
8402 |
^ self |
|
8403 |
]. |
|
8404 |
^ self copyFrom:index |
|
608 | 8405 |
]. |
8406 |
^ '' |
|
8407 |
||
8408 |
" |
|
8874 | 8409 |
' foo ' withoutLeadingSeparators |
8410 |
'foo ' withoutLeadingSeparators |
|
8411 |
' foo' withoutLeadingSeparators |
|
8412 |
' ' withoutLeadingSeparators |
|
8413 |
'foo' withoutLeadingSeparators |
|
8414 |
(' ' , Character tab asString , ' foo ') withoutLeadingSeparators inspect |
|
608 | 8415 |
" |
9192 | 8416 |
! |
8417 |
||
8418 |
withoutMatchEscapes |
|
4117 | 8419 |
"return a copy of the receiver with all $\ removed or |
8420 |
the receiver, if there are none." |
|
8421 |
||
5342
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
8422 |
|in out c escape| |
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
8423 |
|
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
8424 |
escape := self class matchEscapeCharacter. |
4117 | 8425 |
|
8426 |
in := self readStream. |
|
8222
f2c454a9a038
replaced '' writeStream by String writeStream
Claus Gittinger <cg@exept.de>
parents:
8124
diff
changeset
|
8427 |
out := self species writeStream. |
4117 | 8428 |
[in atEnd] whileFalse:[ |
20874 | 8429 |
c := in next. |
8430 |
c == escape ifTrue:[ |
|
8431 |
in atEnd ifFalse:[ |
|
8432 |
c := in next. |
|
8433 |
] |
|
8434 |
]. |
|
8435 |
out nextPut:c. |
|
4117 | 8436 |
]. |
8437 |
^ out contents. |
|
8438 |
||
8439 |
" |
|
8874 | 8440 |
'*foo' withoutMatchEscapes |
8441 |
'\*foo' withoutMatchEscapes |
|
8442 |
'*foo' withoutMatchEscapes |
|
8443 |
'\\*foo' withoutMatchEscapes |
|
8444 |
'foo*' withoutMatchEscapes |
|
8445 |
'foo\*' withoutMatchEscapes |
|
8446 |
'foo\' withoutMatchEscapes |
|
8447 |
'f\*o\*o' withoutMatchEscapes |
|
4117 | 8448 |
" |
8449 |
||
8450 |
"Modified: 30.6.1997 / 13:40:23 / cg" |
|
9192 | 8451 |
! |
8452 |
||
23249 | 8453 |
withoutPrefix:aStringOrCharacter |
19677 | 8454 |
"if the receiver startsWith aString, return a copy without it. |
9192 | 8455 |
Otherwise return the receiver" |
8456 |
||
23249 | 8457 |
(self startsWith:aStringOrCharacter) ifTrue:[ |
8458 |
aStringOrCharacter isCharacter ifTrue:[ |
|
8459 |
^ self copyFrom:2 |
|
8460 |
] ifFalse:[ |
|
8461 |
^ self copyFrom:aStringOrCharacter size+1 |
|
8462 |
]. |
|
9192 | 8463 |
]. |
8464 |
^ self |
|
8465 |
||
8466 |
" |
|
8467 |
'helloworld' withoutPrefix:'hello' |
|
8468 |
'helloworld' withoutPrefix:'foo' |
|
8469 |
" |
|
19677 | 8470 |
|
8471 |
"Modified: / 30-04-2016 / 10:01:00 / cg" |
|
23249 | 8472 |
"Modified: / 31-07-2018 / 17:05:36 / Claus Gittinger" |
8473 |
! |
|
8474 |
||
8475 |
withoutPrefix:aStringOrCharacter caseSensitive:caseSensitive |
|
23234 | 8476 |
"if the receiver startsWith aString, return a copy without it. |
8477 |
Otherwise return the receiver" |
|
8478 |
||
23249 | 8479 |
(self startsWith:aStringOrCharacter caseSensitive:caseSensitive) ifTrue:[ |
8480 |
aStringOrCharacter isCharacter ifTrue:[ |
|
8481 |
^ self copyFrom:2 |
|
8482 |
] ifFalse:[ |
|
8483 |
^ self copyFrom:aStringOrCharacter size+1 |
|
8484 |
]. |
|
23234 | 8485 |
]. |
8486 |
^ self |
|
8487 |
||
8488 |
" |
|
8489 |
'helloworld' withoutPrefix:'hello' |
|
8490 |
'helloworld' withoutPrefix:'foo' |
|
8491 |
" |
|
8492 |
||
8493 |
"Created: / 27-07-2018 / 08:30:03 / Claus Gittinger" |
|
23249 | 8494 |
"Modified: / 31-07-2018 / 17:05:53 / Claus Gittinger" |
23234 | 8495 |
! |
8496 |
||
10671 | 8497 |
withoutQuotes |
17000 | 8498 |
"/ remove quotes ($" and $') from the front and end of myself (if matching)" |
8499 |
||
8500 |
|firstChar| |
|
8501 |
||
17024 | 8502 |
self isEmpty ifTrue:[^ self]. |
8503 |
||
17000 | 8504 |
firstChar := self first. |
8505 |
((firstChar == $") or:[firstChar == $']) ifFalse:[^ self]. |
|
8506 |
||
8507 |
self last == firstChar ifTrue:[ |
|
20874 | 8508 |
^ self copyFrom:2 to:(self size-1) |
10671 | 8509 |
]. |
17000 | 8510 |
^ self |
8511 |
||
8512 |
"/ |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
8513 |
"/ '"hello"' withoutQuotes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
8514 |
"/ '''hello''' withoutQuotes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
8515 |
"/ 'hello' withoutQuotes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
8516 |
"/ '"hello' withoutQuotes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
8517 |
"/ 'hello"' withoutQuotes |
17000 | 8518 |
"/ |
10671 | 8519 |
! |
8520 |
||
9192 | 8521 |
withoutSeparators |
608 | 8522 |
"return a copy of myself without leading and trailing whitespace. |
4156 | 8523 |
(but whiteSpace in-between is preserved) |
608 | 8524 |
Whitespace is space, tab, newline, formfeed. |
8525 |
Use withoutSpaces, if you want to remove spaces only." |
|
8526 |
||
12319
18b80c2ce1e3
oops #withoutSeparators #withoutSpaces were noops
Claus Gittinger <cg@exept.de>
parents:
12317
diff
changeset
|
8527 |
^ self trimForWhich:[:ch | ch isSeparator] |
608 | 8528 |
|
8529 |
" |
|
8874 | 8530 |
' foo ' withoutSeparators |
8531 |
' foo' withoutSeparators |
|
8532 |
'foo ' withoutSeparators |
|
8533 |
' ' withoutSeparators |
|
8534 |
(' foo' , Character tab asString , ' ') withoutSeparators inspect |
|
12319
18b80c2ce1e3
oops #withoutSeparators #withoutSpaces were noops
Claus Gittinger <cg@exept.de>
parents:
12317
diff
changeset
|
8535 |
' foo ' asUnicode16String withoutSeparators |
608 | 8536 |
" |
9192 | 8537 |
! |
8538 |
||
8539 |
withoutSpaces |
|
608 | 8540 |
"return a copy of myself without leading and trailing spaces. |
4156 | 8541 |
(but spaces in-between are preserved) |
608 | 8542 |
Notice: this does NOT remove tabs, newline or any other whitespace. |
8543 |
Use withoutSeparators for this." |
|
8544 |
||
12319
18b80c2ce1e3
oops #withoutSeparators #withoutSpaces were noops
Claus Gittinger <cg@exept.de>
parents:
12317
diff
changeset
|
8545 |
^ self trimForWhich:[:ch | ch == Character space] |
608 | 8546 |
|
8547 |
" |
|
8874 | 8548 |
' foo ' withoutSpaces |
8549 |
'foo ' withoutSpaces |
|
8550 |
' foo' withoutSpaces |
|
8551 |
' ' withoutSpaces |
|
8552 |
'a b' withoutSpaces |
|
8553 |
(' foo' , Character tab asString , ' ') withoutSpaces inspect |
|
608 | 8554 |
" |
9192 | 8555 |
! |
8556 |
||
23249 | 8557 |
withoutSuffix:aStringOrCharacter |
22357 | 8558 |
"if the receiver endsWith aString, return a copy without it. |
8559 |
Otherwise return the receiver" |
|
8560 |
||
23249 | 8561 |
(self endsWith:aStringOrCharacter) ifTrue:[ |
8562 |
aStringOrCharacter isCharacter ifTrue:[ |
|
8563 |
^ self copyButLast:1 |
|
8564 |
] ifFalse:[ |
|
8565 |
^ self copyButLast:aStringOrCharacter size |
|
8566 |
]. |
|
22357 | 8567 |
]. |
8568 |
^ self |
|
8569 |
||
8570 |
" |
|
8571 |
'helloworld' withoutSuffix:'world' |
|
8572 |
'helloworld' withoutSuffix:'foo' |
|
23249 | 8573 |
'helloworldx' withoutSuffix:$x |
22357 | 8574 |
" |
8575 |
||
8576 |
"Created: / 23-10-2017 / 15:01:37 / cg" |
|
23249 | 8577 |
"Modified: / 31-07-2018 / 17:06:34 / Claus Gittinger" |
8578 |
! |
|
8579 |
||
8580 |
withoutSuffix:aStringOrCharacter caseSensitive:caseSensitive |
|
23234 | 8581 |
"if the receiver endsWith aString, return a copy without it. |
8582 |
Otherwise return the receiver" |
|
8583 |
||
23249 | 8584 |
(self endsWith:aStringOrCharacter caseSensitive:caseSensitive) ifTrue:[ |
8585 |
aStringOrCharacter isCharacter ifTrue:[ |
|
8586 |
^ self copyButLast:1 |
|
8587 |
] ifFalse:[ |
|
8588 |
^ self copyButLast:aStringOrCharacter size |
|
8589 |
]. |
|
23234 | 8590 |
]. |
8591 |
^ self |
|
8592 |
||
8593 |
" |
|
8594 |
'helloworld' withoutSuffix:'world' |
|
8595 |
'helloworld' withoutSuffix:'foo' |
|
8596 |
" |
|
8597 |
||
8598 |
"Created: / 27-07-2018 / 08:30:10 / Claus Gittinger" |
|
23249 | 8599 |
"Modified: / 31-07-2018 / 17:06:58 / Claus Gittinger" |
23234 | 8600 |
! |
8601 |
||
9192 | 8602 |
withoutTrailingSeparators |
915
1ac1c7e942f4
added withoutTrailingSeparators
Claus Gittinger <cg@exept.de>
parents:
828
diff
changeset
|
8603 |
"return a copy of myself without trailing separators. |
1ac1c7e942f4
added withoutTrailingSeparators
Claus Gittinger <cg@exept.de>
parents:
828
diff
changeset
|
8604 |
Notice: this does remove tabs, newline or any other whitespace. |
1ac1c7e942f4
added withoutTrailingSeparators
Claus Gittinger <cg@exept.de>
parents:
828
diff
changeset
|
8605 |
Returns an empty string, if the receiver consist only of whitespace." |
1ac1c7e942f4
added withoutTrailingSeparators
Claus Gittinger <cg@exept.de>
parents:
828
diff
changeset
|
8606 |
|
12188 | 8607 |
^ self withoutTrailingForWhich:[:ch | ch isSeparator] |
915
1ac1c7e942f4
added withoutTrailingSeparators
Claus Gittinger <cg@exept.de>
parents:
828
diff
changeset
|
8608 |
|
1ac1c7e942f4
added withoutTrailingSeparators
Claus Gittinger <cg@exept.de>
parents:
828
diff
changeset
|
8609 |
" |
8874 | 8610 |
' foo ' withoutTrailingSeparators |
8611 |
'foo ' withoutTrailingSeparators |
|
8612 |
' foo' withoutTrailingSeparators |
|
8613 |
' ' withoutTrailingSeparators |
|
8614 |
'foo' withoutTrailingSeparators |
|
8615 |
(' ' , Character tab asString , ' foo ') withoutTrailingSeparators inspect |
|
8616 |
(' foo' , Character tab asString) withoutTrailingSeparators inspect |
|
915
1ac1c7e942f4
added withoutTrailingSeparators
Claus Gittinger <cg@exept.de>
parents:
828
diff
changeset
|
8617 |
" |
9192 | 8618 |
! ! |
8619 |
||
24033 | 8620 |
|
9192 | 8621 |
!CharacterArray methodsFor:'substring searching'! |
8622 |
||
15055 | 8623 |
findRangeOfString:subString |
8624 |
"find a substring. if found, return the start- and endIndex; |
|
8625 |
if not found, return an empty interval." |
|
8626 |
||
8627 |
^ self rangeOfSubCollection:subString startingAt:1 ifAbsent:[0 to:-1] caseSensitive:true |
|
8628 |
||
8629 |
" |
|
8630 |
'hello world' findRangeOfString:'llo' |
|
8631 |
'hello world' findRangeOfString:'ole' |
|
8632 |
" |
|
8633 |
! |
|
8634 |
||
9192 | 8635 |
findString:subString |
8636 |
"find a substring. if found, return the index; |
|
8637 |
if not found, return 0." |
|
8638 |
||
8639 |
^ self indexOfSubCollection:subString startingAt:1 ifAbsent:0 caseSensitive:true |
|
8640 |
||
8641 |
" |
|
8642 |
'hello world' findString:'llo' |
|
8643 |
'hello world' findString:'ole' |
|
8644 |
" |
|
8645 |
! |
|
608 | 8646 |
|
15734 | 8647 |
findString:subString caseSensitive:caseSensitive |
8648 |
"find a substring. if found, return the index; |
|
8649 |
if not found, return 0." |
|
8650 |
||
8651 |
^ self indexOfSubCollection:subString startingAt:1 ifAbsent:0 caseSensitive:caseSensitive |
|
8652 |
||
8653 |
" |
|
8654 |
'hello world' findString:'LLo' caseSensitive:true |
|
8655 |
'hello world' findString:'LLo' caseSensitive:false |
|
8656 |
" |
|
8657 |
! |
|
8658 |
||
608 | 8659 |
findString:subString ifAbsent:exceptionBlock |
8660 |
"find a substring. If found, return the index; |
|
8661 |
if not found, return the result of evaluating exceptionBlock." |
|
8662 |
||
9087
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
8663 |
^ self indexOfSubCollection:subString startingAt:1 ifAbsent:exceptionBlock caseSensitive:true |
608 | 8664 |
! |
8665 |
||
11018 | 8666 |
findString:subString ignoreCase:ignoreCase |
15734 | 8667 |
<resource: #obsolete> |
11018 | 8668 |
"find a substring. if found, return the index; |
8669 |
if not found, return 0." |
|
8670 |
||
8671 |
^ self indexOfSubCollection:subString startingAt:1 ifAbsent:0 caseSensitive:ignoreCase not |
|
8672 |
||
8673 |
" |
|
8674 |
'hello world' findString:'LLo' ignoreCase:false |
|
8675 |
'hello world' findString:'LLo' ignoreCase:true |
|
8676 |
" |
|
8677 |
! |
|
8678 |
||
608 | 8679 |
findString:subString startingAt:index |
8680 |
"find a substring, starting at index. if found, return the index; |
|
8681 |
if not found, return 0." |
|
8682 |
||
9087
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
8683 |
^ self indexOfSubCollection:subString startingAt:index ifAbsent:0 caseSensitive:true |
608 | 8684 |
|
8685 |
" |
|
9192 | 8686 |
'hello yello' findString:'llo' startingAt:1 |
8687 |
'hello yello' findString:'llo' startingAt:5 |
|
8688 |
'hello yello' findString:'llo' startingAt:15 |
|
8689 |
" |
|
8690 |
! |
|
608 | 8691 |
|
8692 |
findString:subString startingAt:index ifAbsent:exceptionBlock |
|
8693 |
"find a substring, starting at index. if found, return the index; |
|
8694 |
if not found, return the result of evaluating exceptionBlock." |
|
8695 |
||
9087
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
8696 |
^ self indexOfSubCollection:subString startingAt:index ifAbsent:exceptionBlock caseSensitive:true |
608 | 8697 |
! |
8698 |
||
12177 | 8699 |
findString:subString startingAt:index ifAbsent:exceptionBlock caseSensitive:caseSensitive |
10929 | 8700 |
"find a substring, starting at index. if found, return the index; |
8701 |
if not found, return the result of evaluating exceptionBlock." |
|
8702 |
||
8703 |
^ self indexOfSubCollection:subString startingAt:index ifAbsent:exceptionBlock caseSensitive:caseSensitive |
|
8704 |
! |
|
8705 |
||
608 | 8706 |
includesString:aString |
8707 |
"return true, if a substring is contained in the receiver" |
|
8708 |
||
9087
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
8709 |
^ (self indexOfSubCollection:aString startingAt:1 ifAbsent:0 caseSensitive:true) ~~ 0 |
608 | 8710 |
|
8711 |
" |
|
12177 | 8712 |
'hello world' includesString:'hel' |
8874 | 8713 |
'hello world' includesString:'rld' |
9192 | 8714 |
'hello world' includesString:'llo' |
8715 |
'hello world' includesString:'LLO' |
|
12177 | 8716 |
'hello world' includesString:'' |
9192 | 8717 |
" |
8718 |
! |
|
9191 | 8719 |
|
8720 |
includesString:aString caseSensitive:caseSensitive |
|
9087
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
8721 |
"return true, if a substring is contained in the receiver" |
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
8722 |
|
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
8723 |
^ (self indexOfSubCollection:aString startingAt:1 ifAbsent:0 caseSensitive:caseSensitive) ~~ 0 |
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
8724 |
|
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
8725 |
" |
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
8726 |
'hello world' includesString:'hel' caseSensitive:true |
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
8727 |
'hello world' includesString:'HEL' caseSensitive:true |
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
8728 |
'hello world' includesString:'HEL' caseSensitive:false |
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
8729 |
|
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
8730 |
'hello world' includesString:'wor' caseSensitive:true |
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
8731 |
'hello world' includesString:'WOR' caseSensitive:true |
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
8732 |
'hello world' includesString:'WOR' caseSensitive:false |
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
8733 |
" |
9192 | 8734 |
! |
8735 |
||
23060 | 8736 |
indexOfString:aString |
8737 |
"VSE and V'age compatibility" |
|
8738 |
"find a substring. If found, return the index; if not found, return 0." |
|
8739 |
||
8740 |
^ self indexOfSubCollection:aString startingAt:1 ifAbsent:[0] caseSensitive:true |
|
8741 |
||
8742 |
" |
|
8743 |
'hello world' indexOfString:'hello' -> 1 |
|
8744 |
'hello world' indexOfString:'world' -> 7 |
|
8745 |
'hello world' indexOfString:'World' -> 0 |
|
8746 |
'hello world' indexOfString:'World' caseSensitive:false -> 7 |
|
8747 |
" |
|
8748 |
||
8749 |
"Modified (format): / 08-06-2018 / 14:43:15 / Claus Gittinger" |
|
8750 |
! |
|
8751 |
||
8752 |
indexOfString:aString caseSensitive:caseSensitive |
|
8753 |
"VSE and V'age compatibility" |
|
8754 |
"find a substring. If found, return the index; if not found, return 0." |
|
8755 |
||
8756 |
^ self indexOfSubCollection:aString startingAt:1 ifAbsent:[0] caseSensitive:caseSensitive |
|
8757 |
||
8758 |
" |
|
8759 |
'hello world' indexOfString:'hello' -> 1 |
|
8760 |
'hello world' indexOfString:'world' -> 7 |
|
8761 |
'hello world' indexOfString:'World' -> 0 |
|
8762 |
'hello world' indexOfString:'World' caseSensitive:false -> 7 |
|
8763 |
" |
|
8764 |
||
8765 |
"Created: / 08-06-2018 / 14:39:18 / Claus Gittinger" |
|
8766 |
! |
|
8767 |
||
8768 |
indexOfString:aString caseSensitive:caseSensitive ifAbsent:exceptionValue |
|
8769 |
"VSE and V'age compatibility" |
|
8770 |
"find a substring. If found, return the index; if not found, return 0." |
|
8771 |
||
8772 |
^ self indexOfSubCollection:aString startingAt:1 ifAbsent:exceptionValue caseSensitive:caseSensitive |
|
8773 |
||
8774 |
" |
|
8775 |
'abcdefabcdef' indexOfString:'fab' ifAbsent:[999] -> 6 |
|
8776 |
'abcdefabcdef' indexOfString:'Fab' ifAbsent:[999] -> 999 |
|
8777 |
'abcdefabcdef' indexOfString:'Fab' caseSensitive:false ifAbsent:[999] -> 6 |
|
8778 |
'abcdefabcdef' indexOfString:'xxx' caseSensitive:false ifAbsent:[999] -> 999 |
|
8779 |
" |
|
8780 |
||
8781 |
"Created: / 08-06-2018 / 14:40:21 / Claus Gittinger" |
|
8782 |
! |
|
8783 |
||
8784 |
indexOfString:aString caseSensitive:caseSensitive startingAt:startIndex |
|
8785 |
"VSE and V'age compatibility" |
|
8786 |
"find a substring. If found, return the index; if not found, return 0." |
|
8787 |
||
8788 |
^ self indexOfSubCollection:aString startingAt:startIndex ifAbsent:[0] caseSensitive:caseSensitive |
|
8789 |
||
8790 |
"Created: / 08-06-2018 / 14:44:17 / Claus Gittinger" |
|
8791 |
! |
|
8792 |
||
8793 |
indexOfString:aString caseSensitive:caseSensitive startingAt:startIndex ifAbsent:exceptionalValue |
|
8794 |
"VSE and V'age compatibility" |
|
8795 |
"find a substring. |
|
8796 |
If found, return the index; if not found, the value from exceptionalValue." |
|
8797 |
||
8798 |
^ self indexOfSubCollection:aString startingAt:startIndex ifAbsent:exceptionalValue caseSensitive:caseSensitive |
|
8799 |
||
8800 |
"Created: / 08-06-2018 / 14:42:33 / Claus Gittinger" |
|
8801 |
! |
|
8802 |
||
8803 |
indexOfString:aString ifAbsent:exceptionValue |
|
8804 |
"VSE and V'age compatibility" |
|
8805 |
"find a substring. If found, return the index; if not found, return 0." |
|
8806 |
||
8807 |
^ self indexOfSubCollection:aString startingAt:1 ifAbsent:exceptionValue caseSensitive:true |
|
8808 |
||
8809 |
" |
|
8810 |
'abcdefabcdef' indexOfString:'fab' ifAbsent:[999] -> 6 |
|
8811 |
'abcdefabcdef' indexOfString:'Fab' ifAbsent:[999] -> 999 |
|
8812 |
'abcdefabcdef' indexOfString:'Fab' caseSensitieve:false ifAbsent:[999] -> 999 |
|
8813 |
'abcdefabcdef' indexOfString:'xxx' ifAbsent:[999] -> 999 |
|
8814 |
" |
|
8815 |
||
8816 |
"Modified: / 08-06-2018 / 14:43:44 / Claus Gittinger" |
|
8817 |
! |
|
8818 |
||
8819 |
indexOfString:aString startingAt:startIndex |
|
8820 |
"VSE and V'age compatibility" |
|
8821 |
"find a substring. If found, return the index; if not found, return 0." |
|
8822 |
||
8823 |
^ self indexOfSubCollection:aString startingAt:startIndex ifAbsent:[0] caseSensitive:true |
|
8824 |
||
8825 |
"Modified: / 08-06-2018 / 14:37:52 / Claus Gittinger" |
|
8826 |
! |
|
8827 |
||
8828 |
indexOfString:aString startingAt:startIndex ifAbsent:exceptionalValue |
|
8829 |
"VSE and V'age compatibility" |
|
8830 |
"find a substring. |
|
8831 |
If found, return the index; if not found, the value from exceptionalValue." |
|
8832 |
||
8833 |
^ self indexOfSubCollection:aString startingAt:startIndex ifAbsent:exceptionalValue caseSensitive:true |
|
8834 |
||
8835 |
"Modified: / 08-06-2018 / 14:38:12 / Claus Gittinger" |
|
8836 |
! |
|
8837 |
||
18671 | 8838 |
indexOfSubCollection:subString caseSensitive:caseSensitive |
8839 |
"find a substring, starting at index. if found, return the index; |
|
21675 | 8840 |
if not found, return the result of evaluating exceptionBlock." |
18671 | 8841 |
|
8842 |
^ self indexOfSubCollection:subString startingAt:1 ifAbsent:[0] caseSensitive:caseSensitive. |
|
21675 | 8843 |
|
8844 |
"Modified (comment): / 28-03-2017 / 15:53:30 / stefan" |
|
18671 | 8845 |
! |
8846 |
||
9192 | 8847 |
indexOfSubCollection:subString startingAt:index ifAbsent:exceptionBlock |
8848 |
"find a substring, starting at index. if found, return the index; |
|
21675 | 8849 |
if not found, return the result of evaluating exceptionBlock." |
9192 | 8850 |
|
8851 |
^ self indexOfSubCollection:subString startingAt:index ifAbsent:exceptionBlock caseSensitive:true. |
|
21675 | 8852 |
|
8853 |
"Modified (comment): / 28-03-2017 / 15:53:14 / stefan" |
|
9192 | 8854 |
! |
8855 |
||
8856 |
indexOfSubCollection:subString startingAt:index ifAbsent:exceptionBlock caseSensitive:caseSensitive |
|
9087
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
8857 |
"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
|
8858 |
if not found, return the result of evaluating exceptionBlock. |
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
8859 |
This is a q&d hack - not very efficient" |
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
8860 |
|
608 | 8861 |
|firstChar found |
8862 |
startIndex "{ Class: SmallInteger }" |
|
8863 |
subSize "{ Class: SmallInteger }" |
|
8864 |
mySize "{ Class: SmallInteger }" |
|
21675 | 8865 |
runIdx "{ Class: SmallInteger }"| |
608 | 8866 |
|
8867 |
subSize := subString size. |
|
12177 | 8868 |
subSize == 0 ifTrue:[ |
20874 | 8869 |
subString isString ifFalse:[ |
23001 | 8870 |
self proceedableError:'non string argument'. |
20874 | 8871 |
]. |
8872 |
"empty string does not match" |
|
8873 |
^ 0. |
|
8874 |
"empty string matches" |
|
15433 | 8875 |
"/ ^ index |
6122
a98958f1c85f
handle non-string arg to indexOfSubString:
Claus Gittinger <cg@exept.de>
parents:
6119
diff
changeset
|
8876 |
]. |
9087
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
8877 |
|
608 | 8878 |
mySize := self size. |
8879 |
firstChar := subString at:1. |
|
9087
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
8880 |
caseSensitive ifTrue:[ |
20874 | 8881 |
startIndex := self indexOf:firstChar startingAt:index. |
9087
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
8882 |
] ifFalse:[ |
20874 | 8883 |
startIndex := self findFirst:[:c | c sameAs:firstChar] startingAt:index. |
9087
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
8884 |
]. |
21672 | 8885 |
[startIndex ~~ 0] whileTrue:[ |
20874 | 8886 |
runIdx := startIndex. |
8887 |
found := true. |
|
8888 |
1 to:subSize do:[:i | |
|
8889 |
runIdx > mySize ifTrue:[ |
|
8890 |
found := false |
|
8891 |
] ifFalse:[ |
|
21675 | 8892 |
caseSensitive ifTrue:[ |
8893 |
(subString at:i) ~= (self at:runIdx) ifTrue:[found := false]. |
|
8894 |
] ifFalse:[ |
|
8895 |
((subString at:i) sameAs:(self at:runIdx)) ifFalse:[found := false]. |
|
8896 |
]. |
|
20874 | 8897 |
]. |
8898 |
runIdx := runIdx + 1 |
|
8899 |
]. |
|
8900 |
found ifTrue:[ |
|
8901 |
^ startIndex |
|
8902 |
]. |
|
8903 |
caseSensitive ifTrue:[ |
|
8904 |
startIndex := self indexOf:firstChar startingAt:(startIndex + 1) |
|
8905 |
] ifFalse:[ |
|
8906 |
startIndex := self findFirst:[:c | c sameAs:firstChar] startingAt:(startIndex + 1). |
|
8907 |
]. |
|
608 | 8908 |
]. |
8909 |
^ exceptionBlock value |
|
1005
4507487e0be6
more on encoding/decoding (still unfinished)
Claus Gittinger <cg@exept.de>
parents:
997
diff
changeset
|
8910 |
|
21675 | 8911 |
" |
8912 |
'АБВГДЕЖ' asLowercase indexOfSubCollection:'ВГДЕ' startingAt:1 ifAbsent:nil caseSensitive:false. |
|
8913 |
'АБВГДЕЖ' indexOfSubCollection:'ВГДЕ' startingAt:1 ifAbsent:nil caseSensitive:true. |
|
8914 |
" |
|
8915 |
||
21672 | 8916 |
"Modified: / 23-02-1996 / 15:35:15 / cg" |
21675 | 8917 |
"Modified (comment): / 28-03-2017 / 16:05:48 / stefan" |
23001 | 8918 |
"Modified: / 24-05-2018 / 14:55:38 / Claus Gittinger" |
9192 | 8919 |
! |
8920 |
||
17191 | 8921 |
indexOfSubCollection:subString startingAt:index ifAbsent:exceptionBlock caseSensitive:caseSensitive ignoreDiacritics:ignoreDiacritics |
8922 |
"find a substring, starting at index. if found, return the index; |
|
8923 |
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
|
8924 |
This is a q&d hack - not very efficient |
17191 | 8925 |
(see implementation in string, for a much faster algorithm)" |
8926 |
||
8927 |
|firstChar found |
|
8928 |
startIndex "{ Class: SmallInteger }" |
|
8929 |
subSize "{ Class: SmallInteger }" |
|
8930 |
mySize "{ Class: SmallInteger }" |
|
8931 |
runIdx "{ Class: SmallInteger }" |
|
8932 |
tester charMap firstCharMapped findNextIndex| |
|
8933 |
||
8934 |
subSize := subString size. |
|
8935 |
subSize == 0 ifTrue:[ |
|
20874 | 8936 |
subString isString ifFalse:[ |
23001 | 8937 |
self proceedableError:'non string argument'. |
20874 | 8938 |
]. |
8939 |
"empty string does not match" |
|
8940 |
^ 0. |
|
8941 |
"empty string matches" |
|
17191 | 8942 |
"/ ^ index |
8943 |
]. |
|
8944 |
||
8945 |
mySize := self size. |
|
8946 |
firstChar := subString at:1. |
|
8947 |
ignoreDiacritics ifTrue:[ |
|
20874 | 8948 |
caseSensitive ifTrue:[ |
8949 |
charMap := [:ch | ch asLowercase withoutDiacritics]. |
|
8950 |
] ifFalse:[ |
|
8951 |
charMap := [:ch | ch withoutDiacritics]. |
|
8952 |
]. |
|
8953 |
tester := [:c1 :c2 | (charMap value:c1) = (charMap value:c2) ]. |
|
8954 |
firstCharMapped := (charMap value:firstChar). |
|
8955 |
findNextIndex := [:index | self findFirst:[:ch | (charMap value:ch) = firstCharMapped] startingAt:index]. |
|
17191 | 8956 |
] ifFalse:[ |
20874 | 8957 |
caseSensitive ifTrue:[ |
8958 |
tester := [:c1 :c2 | c1 = c2 ]. |
|
8959 |
findNextIndex := [:index | self indexOf:firstChar startingAt:index]. |
|
8960 |
] ifFalse:[ |
|
8961 |
tester := [:c1 :c2 | c1 sameAs: c2 ]. |
|
8962 |
findNextIndex := [:index | self findFirst:[:c | c sameAs:firstChar] startingAt:index]. |
|
8963 |
]. |
|
17191 | 8964 |
]. |
8965 |
startIndex := findNextIndex value:index. |
|
8966 |
[startIndex == 0] whileFalse:[ |
|
20874 | 8967 |
runIdx := startIndex. |
8968 |
found := true. |
|
8969 |
1 to:subSize do:[:i | |
|
8970 |
runIdx > mySize ifTrue:[ |
|
8971 |
found := false |
|
8972 |
] ifFalse:[ |
|
8973 |
(tester value:(subString at:i) value:(self at:runIdx)) ifFalse:[ |
|
8974 |
found := false |
|
8975 |
] |
|
8976 |
]. |
|
8977 |
runIdx := runIdx + 1 |
|
8978 |
]. |
|
8979 |
found ifTrue:[ |
|
8980 |
^ startIndex |
|
8981 |
]. |
|
8982 |
startIndex := findNextIndex value:(startIndex + 1) |
|
17191 | 8983 |
]. |
8984 |
^ exceptionBlock value |
|
8985 |
||
8986 |
" |
|
17600 | 8987 |
'bla depot bla dépots' indexOfSubCollection:'dep' startingAt:1 ifAbsent:0 caseSensitive:false ignoreDiacritics:true. 5 |
8988 |
'bla depot bla dépots' indexOfSubCollection:'dep' startingAt:6 ifAbsent:0 caseSensitive:false ignoreDiacritics:true. 0 |
|
8989 |
||
8990 |
'bla depot bla dépots' indexOfSubCollection:'dep' startingAt:1 ifAbsent:0 caseSensitive:false ignoreDiacritics:false. 5 |
|
8991 |
'bla depot bla dépots' indexOfSubCollection:'dep' startingAt:6 ifAbsent:0 caseSensitive:false ignoreDiacritics:false. 0 |
|
17191 | 8992 |
" |
23001 | 8993 |
|
8994 |
"Modified: / 24-05-2018 / 14:55:43 / Claus Gittinger" |
|
17191 | 8995 |
! |
8996 |
||
18939 | 8997 |
lastIndexOfString:aString |
8998 |
"VSE and V'age compatibility" |
|
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
8999 |
"find the last occurrence of substring. |
18939 | 9000 |
If found, return the index; if not found, return 0." |
9001 |
||
9002 |
^ self lastIndexOfSubCollection:aString startingAt:(self size-aString size+1) ifAbsent:[0] |
|
9003 |
||
9004 |
" 123456789012 |
|
9005 |
'abcdefabcdef' lastIndexOfString:'abc' |
|
9006 |
'abcdefabcdef' lastIndexOfString:'abc' startingAt:6 |
|
9007 |
'abcdefabcdef' lastIndexOfString:'xxx' startingAt:6 |
|
9008 |
" |
|
9009 |
! |
|
9010 |
||
9011 |
lastIndexOfString:aString ifAbsent:exceptionValue |
|
9012 |
"VSE and V'age compatibility" |
|
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
9013 |
"find the last occurrence of substring. |
18939 | 9014 |
If found, return the index; if not found, return 0." |
9015 |
||
9016 |
^ self lastIndexOfSubCollection:aString startingAt:(self size-aString size+1) ifAbsent:exceptionValue |
|
9017 |
||
9018 |
" 123456789012 |
|
9019 |
'abcdefabcdef' lastIndexOfString:'abc' ifAbsent:[999] |
|
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
9020 |
'abcdefabcdef' lastIndexOfString:'xxx' ifAbsent:[999] |
18939 | 9021 |
" |
9022 |
! |
|
9023 |
||
9024 |
lastIndexOfString:aString startingAt:startIndex |
|
9025 |
"VSE and V'age compatibility" |
|
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
9026 |
"find the last occurrence of a substring. |
18939 | 9027 |
If found, return the index; if not found, return 0." |
9028 |
||
9029 |
^ self lastIndexOfSubCollection:aString startingAt:startIndex ifAbsent:[0] |
|
9030 |
||
9031 |
" 123456789012 |
|
9032 |
'abcdefabcdef' lastIndexOfString:'abc' |
|
9033 |
'abcdefabcdef' lastIndexOfString:'abc' startingAt:6 |
|
9034 |
" |
|
9035 |
! |
|
9036 |
||
9037 |
lastIndexOfString:aString startingAt:startIndex ifAbsent:exceptionValue |
|
9038 |
"VSE and V'age compatibility" |
|
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
9039 |
"find the last occurrence of a substring. |
18939 | 9040 |
If found, return the index; if not found, return 0." |
9041 |
||
9042 |
^ self lastIndexOfSubCollection:aString startingAt:startIndex ifAbsent:exceptionValue |
|
9043 |
||
9044 |
" 123456789012 |
|
9045 |
'abcdefabcdef' lastIndexOfString:'abc' |
|
9046 |
'abcdefabcdef' lastIndexOfString:'abc' startingAt:6 |
|
9047 |
'abcdefabcdef' lastIndexOfString:'xxx' startingAt:6 ifAbsent:999 |
|
9048 |
" |
|
9049 |
! |
|
9050 |
||
15055 | 9051 |
rangeOfSubCollection:subString startingAt:start ifAbsent:exceptionValue caseSensitive:caseSensitive |
9052 |
"find a substring. if found, return the start- and endIndex; |
|
9053 |
if not found, return the value of exceptionValue." |
|
9054 |
||
9055 |
|i| |
|
9056 |
||
9057 |
i := self indexOfSubCollection:subString startingAt:start ifAbsent:0 caseSensitive:caseSensitive. |
|
9058 |
i == 0 ifTrue:[ |
|
20874 | 9059 |
^ exceptionValue value |
15055 | 9060 |
]. |
9061 |
^ i to:(i + subString size - 1) |
|
9062 |
||
9063 |
" |
|
9064 |
'hello world' findRangeOfString:'llo' |
|
9065 |
'hello world' findRangeOfString:'ole' |
|
9066 |
" |
|
9192 | 9067 |
! ! |
9068 |
||
9069 |
!CharacterArray methodsFor:'testing'! |
|
9070 |
||
18893 | 9071 |
isPlainString |
9072 |
"return true, if the receiver is a plain string (without attributes); |
|
9073 |
true is returned here - redefinition of Object>>isPlainString." |
|
9074 |
||
9075 |
^ true |
|
9076 |
! |
|
9077 |
||
9230 | 9078 |
isString |
9079 |
"return true, if the receiver is some kind of string; |
|
9080 |
true is returned here - redefinition of Object>>isString." |
|
9081 |
||
9082 |
^ true |
|
9083 |
! |
|
9084 |
||
21126 | 9085 |
isUnary |
9086 |
"Answer true if the receiver is a unary selector. |
|
9087 |
That is not a check for being a valid selector, but instead relies on |
|
9088 |
the selector being valid." |
|
9089 |
||
9090 |
^ self isUnarySelector |
|
9091 |
! |
|
9092 |
||
9230 | 9093 |
isUnarySelector |
9094 |
"Answer true if the receiver contains only chars in an ANSI unary method selector, false otherwise." |
|
9095 |
||
17442 | 9096 |
^ (self first isLetterOrUnderline) |
9845 | 9097 |
and:[ self conform: [ :chr | chr isLetterOrDigit or:[chr == $_] ]] |
9098 |
||
9099 |
"Modified: / 13-09-2006 / 11:35:15 / cg" |
|
9230 | 9100 |
! |
9101 |
||
14122 | 9102 |
isUnicode16String |
9103 |
^ false |
|
9104 |
! |
|
9105 |
||
9106 |
isUnicode32String |
|
19939 | 9107 |
"true if this is a 4-byte unicode string" |
9108 |
||
14122 | 9109 |
^ false |
9110 |
! |
|
9111 |
||
9112 |
isUnicodeString |
|
19938 | 9113 |
"true if this is a 2- or 4-byte unicode string |
9114 |
(i.e. not a single byte string). |
|
20194 | 9115 |
Notice, that the name is misleading: |
19938 | 9116 |
all strings are use unicode encoding" |
20194 | 9117 |
|
14122 | 9118 |
^ false |
9119 |
! |
|
9120 |
||
14555 | 9121 |
isWideString |
19580 | 9122 |
"true if I require more than one byte per character" |
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
9123 |
|
15829 | 9124 |
|string| |
9125 |
||
9126 |
(string := self string) ~~ self ifTrue:[ |
|
20874 | 9127 |
^ string isWideString. |
15829 | 9128 |
]. |
9129 |
^ self contains:[:aCharacter | aCharacter codePoint > 16rFF]. |
|
64 | 9130 |
! ! |
9131 |
||
9192 | 9132 |
!CharacterArray methodsFor:'tracing'! |
9133 |
||
9134 |
traceInto:aRequestor level:level from:referrer |
|
9135 |
"double dispatch into tracer, passing my type implicitely in the selector" |
|
9136 |
||
9137 |
^ aRequestor traceCharacterArray:self level:level from:referrer |
|
9138 |
||
9139 |
||
9140 |
! ! |
|
9141 |
||
9142 |
!CharacterArray methodsFor:'visiting'! |
|
8395
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8376
diff
changeset
|
9143 |
|
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8376
diff
changeset
|
9144 |
acceptVisitor:aVisitor with:aParameter |
16733 | 9145 |
"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
|
9146 |
|
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8376
diff
changeset
|
9147 |
^ aVisitor visitString:self with:aParameter |
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8376
diff
changeset
|
9148 |
! ! |
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8376
diff
changeset
|
9149 |
|
24033 | 9150 |
|
1849
9f35f5934ec6
added msdos-latin1 en/decoder (code = #msdos850)
Claus Gittinger <cg@exept.de>
parents:
1669
diff
changeset
|
9151 |
!CharacterArray class methodsFor:'documentation'! |
629 | 9152 |
|
9153 |
version |
|
18476 | 9154 |
^ '$Header$' |
12058 | 9155 |
! |
9156 |
||
9157 |
version_CVS |
|
18476 | 9158 |
^ '$Header$' |
629 | 9159 |
! ! |
6809 | 9160 |
|
14606 | 9161 |
|
2728
7569e45a4d15
raise error signal if anything goes wrong while decoding.
Claus Gittinger <cg@exept.de>
parents:
2721
diff
changeset
|
9162 |
CharacterArray initialize! |