author | Claus Gittinger <cg@exept.de> |
Fri, 04 Nov 2016 15:51:03 +0100 | |
changeset 20787 | 65eaf57179ab |
parent 20739 | d9d2ce1e2761 |
child 20863 | 63c5a5ce7ac2 |
permissions | -rw-r--r-- |
17600 | 1 |
"{ Encoding: utf8 }" |
2 |
||
64 | 3 |
" |
4 |
COPYRIGHT (c) 1994 by Claus Gittinger |
|
20012 | 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 |
20012 | 19 |
instanceVariableNames:'' |
20705 | 20 |
classVariableNames:'DecoderTables DecodingFailedSignal EncoderTables |
21 |
EncodingFailedSignal PreviousMatches UnicodeDenormalizationMap |
|
22 |
UnicodeNormalizationMap' |
|
20012 | 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 |
|
20012 | 32 |
All Rights Reserved |
82 | 33 |
|
34 |
This software is furnished under a license and may be used |
|
35 |
only in accordance with the terms of that license and with the |
|
36 |
inclusion of the above copyright notice. This software may not |
|
37 |
be provided or otherwise made available to, or used by, any |
|
38 |
other person. No title to or ownership of the software is |
|
39 |
hereby transferred. |
|
40 |
" |
|
41 |
! |
|
42 |
||
64 | 43 |
documentation |
44 |
" |
|
255 | 45 |
CharacterArray is a superclass for all kinds of Strings (i.e. |
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
46 |
(singleByte-)Strings, TwoByteStrings, UnicodeStrings |
20194 | 47 |
and whatever may come in the future. |
18318 | 48 |
|
49 |
This class is abstract, meaning that there are no instances of it; |
|
50 |
concrete subclasses define how the characters are stored (i.e. either as |
|
51 |
single byte, two-byte or four byte strings). |
|
52 |
||
2537 | 53 |
All this class does is provide common protocol for concrete subclasses. |
1290 | 54 |
|
18318 | 55 |
Notice: |
20194 | 56 |
internally, ST/X uses a unicode encoding for ALL characters - both |
57 |
for individual chatacter entities and for strings of characters. |
|
58 |
When reading/writing files in different encodings, the conversion is |
|
59 |
done at read/write time by use of a CharacterEncoder instance. |
|
60 |
These know how to convert to a wide range of encodings. |
|
18318 | 61 |
|
62 |
Also notice: |
|
20194 | 63 |
UTF8 and UTF16 are external encodings of a Unicode string; they are never |
64 |
used internally. When interacting with a UTF8 interface (OS-API or files), |
|
65 |
you should convert UTF8 into the internal full Unicode right at the interface. |
|
66 |
Do not keep UTF8 around internally as String instances. |
|
67 |
The reason is that UTF8 makes it harder to manipulate strings (for example |
|
68 |
to insert/extract substrings or to get its size. Of such operations would |
|
69 |
require a scan of the UTF8, which would complicate them). |
|
70 |
Of course, there may be rare exceptions to this, for example if a file's contents |
|
71 |
is treated as raw data, and the strings have to be copied/shuffled around only, |
|
72 |
without any real processing on it. |
|
18318 | 73 |
|
1290 | 74 |
[author:] |
20194 | 75 |
Claus Gittinger |
1290 | 76 |
|
77 |
[see also:] |
|
20194 | 78 |
String TwoByteString Unicode16String Uniode32String |
79 |
StringCollection |
|
64 | 80 |
" |
81 |
! ! |
|
82 |
||
2728
7569e45a4d15
raise error signal if anything goes wrong while decoding.
Claus Gittinger <cg@exept.de>
parents:
2721
diff
changeset
|
83 |
!CharacterArray class methodsFor:'initialization'! |
7569e45a4d15
raise error signal if anything goes wrong while decoding.
Claus Gittinger <cg@exept.de>
parents:
2721
diff
changeset
|
84 |
|
7569e45a4d15
raise error signal if anything goes wrong while decoding.
Claus Gittinger <cg@exept.de>
parents:
2721
diff
changeset
|
85 |
initialize |
8874 | 86 |
DecodingFailedSignal isNil ifTrue:[ |
20012 | 87 |
DecodingFailedSignal := DecodingError. |
88 |
DecodingFailedSignal notifierString:'error during decode'. |
|
89 |
||
90 |
EncodingFailedSignal :=EncodingError. |
|
91 |
EncodingFailedSignal notifierString:'error during encode'. |
|
5440 | 92 |
] |
2728
7569e45a4d15
raise error signal if anything goes wrong while decoding.
Claus Gittinger <cg@exept.de>
parents:
2721
diff
changeset
|
93 |
|
7569e45a4d15
raise error signal if anything goes wrong while decoding.
Claus Gittinger <cg@exept.de>
parents:
2721
diff
changeset
|
94 |
" |
7569e45a4d15
raise error signal if anything goes wrong while decoding.
Claus Gittinger <cg@exept.de>
parents:
2721
diff
changeset
|
95 |
CharacterArray initialize |
7569e45a4d15
raise error signal if anything goes wrong while decoding.
Claus Gittinger <cg@exept.de>
parents:
2721
diff
changeset
|
96 |
" |
7569e45a4d15
raise error signal if anything goes wrong while decoding.
Claus Gittinger <cg@exept.de>
parents:
2721
diff
changeset
|
97 |
|
2833 | 98 |
"Modified: 3.8.1997 / 18:15:59 / cg" |
2728
7569e45a4d15
raise error signal if anything goes wrong while decoding.
Claus Gittinger <cg@exept.de>
parents:
2721
diff
changeset
|
99 |
! ! |
7569e45a4d15
raise error signal if anything goes wrong while decoding.
Claus Gittinger <cg@exept.de>
parents:
2721
diff
changeset
|
100 |
|
1849
9f35f5934ec6
added msdos-latin1 en/decoder (code = #msdos850)
Claus Gittinger <cg@exept.de>
parents:
1669
diff
changeset
|
101 |
!CharacterArray class methodsFor:'instance creation'! |
64 | 102 |
|
103 |
basicNew |
|
104 |
"return a new empty string" |
|
105 |
||
106 |
^ self basicNew:0 |
|
107 |
! |
|
108 |
||
2734 | 109 |
fromBytes:aByteCollection |
110 |
"return an instance of the receiver class, |
|
19757 | 111 |
taking untranslated bytes from the argument, aByteCollection |
112 |
in most-significant first order. |
|
2734 | 113 |
Only useful, when reading twoByteStrings from external sources." |
114 |
||
19757 | 115 |
^ self fromBytes:aByteCollection MSB:true |
116 |
||
117 |
" |
|
118 |
Unicode16String fromBytes:#[16r02 16r20] |
|
119 |
Unicode16String fromBytes:#[16r02 16r20] MSB:true |
|
120 |
Unicode16String fromBytes:#[16r02 16r20] MSB:false |
|
121 |
" |
|
122 |
||
123 |
"Modified: 30.6.1997 / 20:08:37 / cg" |
|
124 |
! |
|
125 |
||
126 |
fromBytes:aByteCollection MSB:msb |
|
127 |
"return an instance of the receiver class, |
|
128 |
taking untranslated bytes from the argument, aByteCollection |
|
129 |
in the given byte order. |
|
130 |
Only useful, when reading twoByteStrings from external sources." |
|
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
131 |
|
2734 | 132 |
|mySize nBytes newString dstIdx| |
133 |
||
19757 | 134 |
self assert:(self ~~ CharacterArray). "/ only works for concrete subclasses. |
135 |
||
136 |
"/ the following is a quite inefficient implementation. |
|
137 |
"/ consider rewriting, if heavily used. |
|
2734 | 138 |
nBytes := aByteCollection size. |
139 |
mySize := self basicNew bitsPerCharacter. |
|
140 |
mySize == 16 ifTrue:[ |
|
20012 | 141 |
newString := self uninitializedNew:(nBytes // 2). |
142 |
dstIdx := 1. |
|
143 |
msb ifTrue:[ |
|
144 |
aByteCollection pairWiseDo:[:hi :lo | |
|
145 |
newString at:dstIdx put:(Character value:(hi bitShift:8)+lo). |
|
146 |
dstIdx := dstIdx + 1 |
|
147 |
]. |
|
148 |
] ifFalse:[ |
|
149 |
aByteCollection pairWiseDo:[:lo :hi | |
|
150 |
newString at:dstIdx put:(Character value:(hi bitShift:8)+lo). |
|
151 |
dstIdx := dstIdx + 1 |
|
152 |
]. |
|
153 |
]. |
|
154 |
^ newString. |
|
2734 | 155 |
]. |
156 |
||
17625 | 157 |
^ (self uninitializedNew:nBytes) replaceFrom:1 with:aByteCollection |
158 |
||
159 |
" |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
160 |
Unicode16String fromBytes:#[16r02 16r20] |
2734 | 161 |
" |
162 |
||
163 |
"Modified: 30.6.1997 / 20:08:37 / cg" |
|
164 |
! |
|
165 |
||
64 | 166 |
fromString:aString |
167 |
"return a copy of the argument, aString" |
|
168 |
||
18967 | 169 |
|sz| |
170 |
||
171 |
sz := aString size. |
|
172 |
^ (self uninitializedNew:sz) replaceFrom:1 to:sz with:aString startingAt:1 |
|
173 |
||
174 |
" |
|
20012 | 175 |
Unicode16String fromString:'hello' |
17625 | 176 |
" |
581 | 177 |
! |
178 |
||
179 |
fromStringCollection:aCollectionOfStrings |
|
180 |
"return new string formed by concatenating a copy of the argument, aString" |
|
181 |
||
182 |
^ self fromStringCollection:aCollectionOfStrings separatedBy:'' |
|
183 |
||
184 |
" |
|
185 |
String fromStringCollection:#('hello' 'world' 'how' 'about' 'this') |
|
186 |
" |
|
187 |
||
188 |
"Created: 20.11.1995 / 15:26:59 / cg" |
|
189 |
! |
|
190 |
||
191 |
fromStringCollection:aCollectionOfStrings separatedBy:aSeparatorString |
|
192 |
"return new string formed by concatenating a copy of the argument, aString" |
|
193 |
||
17625 | 194 |
|stream| |
195 |
||
196 |
aCollectionOfStrings do:[:eachString | |
|
20012 | 197 |
stream isNil ifTrue:[ |
198 |
stream := self writeStreamClass with:eachString. |
|
199 |
] ifFalse:[ |
|
200 |
stream |
|
201 |
nextPutAll:aSeparatorString; |
|
202 |
nextPutAll:eachString. |
|
203 |
]. |
|
581 | 204 |
]. |
17700 | 205 |
stream isNil ifTrue:[^ '']. |
17625 | 206 |
^ stream contents |
581 | 207 |
|
208 |
" |
|
209 |
String fromStringCollection:#('hello' 'world' 'how' 'about' 'this') separatedBy:' ' |
|
17625 | 210 |
String fromStringCollection:#('hello' 'world' 'how' 'about' 'this') separatedBy:'Ƞ' |
211 |
Text fromStringCollection:{'hello'. 'world'. 'how' allBold. 'about'. 'this'. 'äöü'} separatedBy:'Ƞ' |
|
581 | 212 |
" |
213 |
||
214 |
"Created: 20.11.1995 / 15:32:17 / cg" |
|
608 | 215 |
! |
216 |
||
5757 | 217 |
fromUTF8Bytes:aByteCollection |
5758 | 218 |
"return a new string which represents the characters as decoded |
219 |
from the utf8 encoded bytes, aByteCollection. |
|
220 |
Returns either a normal String, or a TwoByteString instance. |
|
6810 | 221 |
Only useful, when reading twoByteStrings from external sources. |
222 |
This only handles up-to 16bit characters" |
|
5757 | 223 |
|
7876 | 224 |
^ self decodeFromUTF8:aByteCollection. |
5757 | 225 |
|
226 |
" |
|
8874 | 227 |
CharacterArray fromUTF8Bytes:#[ 16r41 16r42 ] |
228 |
CharacterArray fromUTF8Bytes:#[ 16rC1 16r02 ] |
|
229 |
CharacterArray fromUTF8Bytes:#[ 16rE0 16r81 16r02 ] |
|
6810 | 230 |
CharacterArray fromUTF8Bytes:#[ 16rEF 16rBF 16rBF ] |
231 |
||
232 |
rfc2279 examples: |
|
8874 | 233 |
CharacterArray fromUTF8Bytes:#[ 16r41 16rE2 16r89 16rA2 16rCE 16r91 16r2E ] |
234 |
CharacterArray fromUTF8Bytes:#[ 16rED 16r95 16r9C 16rEA 16rB5 16rAD 16rEC 16r96 16rB4 ] |
|
235 |
CharacterArray fromUTF8Bytes:#[ 16rE6 16r97 16rA5 16rE6 16r9C 16rAC 16rE8 16rAA 16r9E ] |
|
6810 | 236 |
|
237 |
invalid: |
|
8874 | 238 |
CharacterArray fromUTF8Bytes:#[ 16rC0 16r80 ] |
239 |
CharacterArray fromUTF8Bytes:#[ 16rE0 16r80 16r80 ] |
|
5757 | 240 |
" |
241 |
! |
|
242 |
||
608 | 243 |
new |
244 |
"return a new empty string" |
|
245 |
||
246 |
^ self basicNew:0 |
|
9425 | 247 |
! |
248 |
||
249 |
readSmalltalkStringFrom:aStreamOrString onError:exceptionBlock |
|
250 |
"read & return the next String from the (character-)stream aStream; |
|
251 |
skipping all whitespace first; return the value of exceptionBlock, |
|
252 |
if no string can be read. The sequence of characters as read from the |
|
253 |
stream must be one as stored via storeOn: or storeString." |
|
254 |
||
255 |
|str collected char| |
|
256 |
||
257 |
str := aStreamOrString readStream. |
|
258 |
||
259 |
"skip whiteSpace" |
|
260 |
str skipSeparators. |
|
11320 | 261 |
|
14452
ef3591302afa
changed: #readSmalltalkStringFrom:onError:
Stefan Vogel <sv@exept.de>
parents:
14451
diff
changeset
|
262 |
(str peekOrNil == $') ifTrue:[ |
20012 | 263 |
str next. |
264 |
collected := self writeStream. |
|
265 |
[str atEnd] whileFalse:[ |
|
266 |
char := str next. |
|
267 |
char == $' ifTrue:[ |
|
268 |
"/ look for another quote |
|
269 |
str peekOrNil ~~ $' ifTrue:[ |
|
270 |
"end of string reached" |
|
271 |
^ collected contents. |
|
272 |
]. |
|
273 |
"eat doubled quote" |
|
274 |
str next. |
|
275 |
]. |
|
276 |
((char ~~ Character return) or:[str peekOrNil ~~ Character lf]) ifTrue:[ |
|
277 |
"compress CRLF to LF, but keep a single CR" |
|
278 |
collected nextPut:char. |
|
279 |
]. |
|
280 |
]. |
|
281 |
"if we come here, we reached the end without finding a closing $'" |
|
9425 | 282 |
]. |
283 |
^ exceptionBlock value |
|
284 |
||
285 |
" |
|
286 |
String readFrom:('''hello world''' readStream) |
|
287 |
String readFrom:('''hello '''' world''' readStream) |
|
288 |
String readFrom:('1 ''hello'' ' readStream) |
|
289 |
String readFrom:('1 ''hello'' ' readStream) onError:['foobar'] |
|
290 |
" |
|
291 |
||
292 |
"Created: / 05-07-2006 / 16:41:04 / cg" |
|
10045
bbd328940fb4
care for cr-lf when reading a smalltalk string
Claus Gittinger <cg@exept.de>
parents:
9845
diff
changeset
|
293 |
"Modified: / 06-10-2006 / 14:05:32 / cg" |
11243
4bf099f5bb56
CharacterArray class>>#writeStream - answer a CharacterWriteStrem
Stefan Vogel <sv@exept.de>
parents:
11229
diff
changeset
|
294 |
! |
4bf099f5bb56
CharacterArray class>>#writeStream - answer a CharacterWriteStrem
Stefan Vogel <sv@exept.de>
parents:
11229
diff
changeset
|
295 |
|
13201 | 296 |
writeStreamClass |
17213 | 297 |
"the class used by writeStream. |
298 |
Here, we return CharacterWriteStream, which automatically changes |
|
299 |
the underlying collection to a multiByte string (i.e. UnicodeString). |
|
300 |
So you can stream wide characters into it." |
|
13201 | 301 |
|
302 |
^ CharacterWriteStream |
|
11243
4bf099f5bb56
CharacterArray class>>#writeStream - answer a CharacterWriteStrem
Stefan Vogel <sv@exept.de>
parents:
11229
diff
changeset
|
303 |
|
4bf099f5bb56
CharacterArray class>>#writeStream - answer a CharacterWriteStrem
Stefan Vogel <sv@exept.de>
parents:
11229
diff
changeset
|
304 |
" |
4bf099f5bb56
CharacterArray class>>#writeStream - answer a CharacterWriteStrem
Stefan Vogel <sv@exept.de>
parents:
11229
diff
changeset
|
305 |
String writeStream |
4bf099f5bb56
CharacterArray class>>#writeStream - answer a CharacterWriteStrem
Stefan Vogel <sv@exept.de>
parents:
11229
diff
changeset
|
306 |
" |
13201 | 307 |
|
308 |
"Created: / 09-01-2011 / 10:37:57 / cg" |
|
64 | 309 |
! ! |
310 |
||
9265 | 311 |
!CharacterArray class methodsFor:'Compatibility-VW'! |
312 |
||
313 |
fromIntegerArray: anArray |
|
314 |
"Answer a new instance of the receiver that is created from the argument, anArray." |
|
315 |
||
316 |
| new | |
|
317 |
||
318 |
new := self new: anArray size. |
|
319 |
1 to: anArray size do:[:index | |
|
20012 | 320 |
new at: index put: (anArray at: index) asCharacter |
9265 | 321 |
]. |
322 |
^new |
|
323 |
||
324 |
" |
|
325 |
String fromIntegerArray: #[8 127] |
|
326 |
" |
|
327 |
! ! |
|
328 |
||
2728
7569e45a4d15
raise error signal if anything goes wrong while decoding.
Claus Gittinger <cg@exept.de>
parents:
2721
diff
changeset
|
329 |
!CharacterArray class methodsFor:'Signal constants'! |
7569e45a4d15
raise error signal if anything goes wrong while decoding.
Claus Gittinger <cg@exept.de>
parents:
2721
diff
changeset
|
330 |
|
7569e45a4d15
raise error signal if anything goes wrong while decoding.
Claus Gittinger <cg@exept.de>
parents:
2721
diff
changeset
|
331 |
decodingFailedSignal |
2833 | 332 |
"return the signal, raised when decoding of a string is not possible |
2728
7569e45a4d15
raise error signal if anything goes wrong while decoding.
Claus Gittinger <cg@exept.de>
parents:
2721
diff
changeset
|
333 |
due to invalid characters contained in the source. |
7569e45a4d15
raise error signal if anything goes wrong while decoding.
Claus Gittinger <cg@exept.de>
parents:
2721
diff
changeset
|
334 |
This may happen for example, if a non EUC coded 8-bit string |
7569e45a4d15
raise error signal if anything goes wrong while decoding.
Claus Gittinger <cg@exept.de>
parents:
2721
diff
changeset
|
335 |
is attempted to be decoded into a JIS string." |
7569e45a4d15
raise error signal if anything goes wrong while decoding.
Claus Gittinger <cg@exept.de>
parents:
2721
diff
changeset
|
336 |
|
7569e45a4d15
raise error signal if anything goes wrong while decoding.
Claus Gittinger <cg@exept.de>
parents:
2721
diff
changeset
|
337 |
^ DecodingFailedSignal |
7569e45a4d15
raise error signal if anything goes wrong while decoding.
Claus Gittinger <cg@exept.de>
parents:
2721
diff
changeset
|
338 |
|
2833 | 339 |
"Created: 28.6.1997 / 20:09:55 / cg" |
340 |
"Modified: 3.8.1997 / 18:16:47 / cg" |
|
341 |
! |
|
342 |
||
343 |
encodingFailedSignal |
|
344 |
"return the (query-) signal, raised when encoding of a string is not possible |
|
345 |
due to invalid characters contained in the source." |
|
346 |
||
347 |
^ EncodingFailedSignal |
|
348 |
||
2728
7569e45a4d15
raise error signal if anything goes wrong while decoding.
Claus Gittinger <cg@exept.de>
parents:
2721
diff
changeset
|
349 |
"Modified: 28.6.1997 / 20:09:35 / cg" |
2833 | 350 |
"Created: 3.8.1997 / 18:16:40 / cg" |
2728
7569e45a4d15
raise error signal if anything goes wrong while decoding.
Claus Gittinger <cg@exept.de>
parents:
2721
diff
changeset
|
351 |
! ! |
7569e45a4d15
raise error signal if anything goes wrong while decoding.
Claus Gittinger <cg@exept.de>
parents:
2721
diff
changeset
|
352 |
|
1849
9f35f5934ec6
added msdos-latin1 en/decoder (code = #msdos850)
Claus Gittinger <cg@exept.de>
parents:
1669
diff
changeset
|
353 |
!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
|
354 |
|
f0b45a4b82d2
first attempt in encoding/decoding for different character representations (mac/msdos etc.)
Claus Gittinger <cg@exept.de>
parents:
986
diff
changeset
|
355 |
lowSpaceCleanup |
f0b45a4b82d2
first attempt in encoding/decoding for different character representations (mac/msdos etc.)
Claus Gittinger <cg@exept.de>
parents:
986
diff
changeset
|
356 |
"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
|
357 |
|
f0b45a4b82d2
first attempt in encoding/decoding for different character representations (mac/msdos etc.)
Claus Gittinger <cg@exept.de>
parents:
986
diff
changeset
|
358 |
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
|
359 |
|
f0b45a4b82d2
first attempt in encoding/decoding for different character representations (mac/msdos etc.)
Claus Gittinger <cg@exept.de>
parents:
986
diff
changeset
|
360 |
" |
f0b45a4b82d2
first attempt in encoding/decoding for different character representations (mac/msdos etc.)
Claus Gittinger <cg@exept.de>
parents:
986
diff
changeset
|
361 |
CharacterArray lowSpaceCleanup |
f0b45a4b82d2
first attempt in encoding/decoding for different character representations (mac/msdos etc.)
Claus Gittinger <cg@exept.de>
parents:
986
diff
changeset
|
362 |
" |
f0b45a4b82d2
first attempt in encoding/decoding for different character representations (mac/msdos etc.)
Claus Gittinger <cg@exept.de>
parents:
986
diff
changeset
|
363 |
|
f0b45a4b82d2
first attempt in encoding/decoding for different character representations (mac/msdos etc.)
Claus Gittinger <cg@exept.de>
parents:
986
diff
changeset
|
364 |
"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
|
365 |
"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
|
366 |
! ! |
f0b45a4b82d2
first attempt in encoding/decoding for different character representations (mac/msdos etc.)
Claus Gittinger <cg@exept.de>
parents:
986
diff
changeset
|
367 |
|
7421 | 368 |
!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
|
369 |
|
7876 | 370 |
decodeFromUTF8:aStringOrByteCollection |
19837 | 371 |
"given a string in UTF8 encoding, |
372 |
return a new string containing the same characters, in Unicode encoding. |
|
373 |
Returns either a normal String, a Unicode16String or a Unicode32String instance. |
|
374 |
This is only useful, when reading from external sources or communicating with |
|
375 |
other systems |
|
376 |
(ST/X never uses utf8 internally, but always uses strings of fully decoded unicode characters). |
|
377 |
This only handles up-to 30bit characters." |
|
378 |
||
379 |
|sz anyAbove7BitAscii nBitsRequired |
|
380 |
ascii "{ Class: SmallInteger }" |
|
381 |
byte "{ Class: SmallInteger }" |
|
382 |
lastIdx "{ Class: SmallInteger }" |
|
383 |
srcIdx "{ Class: SmallInteger }" |
|
384 |
idx "{ Class: SmallInteger }" |
|
385 |
nFollowBytes "{ Class: SmallInteger }" |
|
386 |
minValue "{ Class: SmallInteger }" |
|
387 |
newString| |
|
388 |
||
389 |
"/ fast track, also avoid creation of new strings if aStringOrByteCollection is already a 7-bit string |
|
390 |
aStringOrByteCollection containsNon7BitAscii ifFalse:[ |
|
391 |
^ aStringOrByteCollection asSingleByteString |
|
392 |
]. |
|
393 |
||
394 |
nBitsRequired := 8. |
|
395 |
anyAbove7BitAscii := false. |
|
396 |
sz := 0. |
|
397 |
||
398 |
lastIdx := aStringOrByteCollection size. |
|
399 |
srcIdx := 1. |
|
400 |
||
401 |
"first determine the string size and max element size. |
|
402 |
Check for UTF-8 confomance on the fly." |
|
403 |
[srcIdx <= lastIdx] whileTrue:[ |
|
404 |
byte := ascii := aStringOrByteCollection byteAt:srcIdx. |
|
405 |
srcIdx := srcIdx + 1. |
|
406 |
(byte bitAnd:16r80) ~~ 0 ifTrue:[ |
|
407 |
anyAbove7BitAscii := true. |
|
408 |
(byte bitAnd:2r11100000) == 2r11000000 ifTrue:[ |
|
409 |
"/ 80 .. 7FF |
|
410 |
ascii := byte bitAnd:2r00011111. |
|
411 |
nFollowBytes := 1. |
|
412 |
minValue := 16r80. |
|
413 |
] ifFalse:[(byte bitAnd:2r11110000) == 2r11100000 ifTrue:[ |
|
414 |
"/ 800 .. FFFF |
|
415 |
ascii := byte bitAnd:2r00001111. |
|
416 |
nFollowBytes := 2. |
|
417 |
minValue := 16r800. |
|
418 |
] ifFalse:[(byte bitAnd:2r11111000) == 2r11110000 ifTrue:[ |
|
419 |
"/ 10000 .. 1FFFFF |
|
420 |
ascii := byte bitAnd:2r00000111. |
|
421 |
nFollowBytes := 3. |
|
422 |
minValue := 16r10000. |
|
423 |
] ifFalse:[(byte bitAnd:2r11111100) == 2r11111000 ifTrue:[ |
|
424 |
"/ 200000 .. 3FFFFFF |
|
425 |
ascii := byte bitAnd:2r00000011. |
|
426 |
nFollowBytes := 4. |
|
427 |
minValue := 16r200000. |
|
428 |
] ifFalse:[(byte bitAnd:2r11111110) == 2r11111100 ifTrue:[ |
|
429 |
"/ 4000000 .. 7FFFFFFF |
|
430 |
ascii := byte bitAnd:2r00000001. |
|
431 |
ascii ~~ 0 ifTrue:[ |
|
432 |
"/ ST/X can only represent 30 bit unicode characters. |
|
433 |
"/ but the max unicode character is defined as 16r10 FFFF anyway |
|
434 |
^ DecodingError newException |
|
435 |
defaultValue:aStringOrByteCollection; |
|
436 |
raiseRequestWith:aStringOrByteCollection errorString:'unicode character out of range'. |
|
437 |
]. |
|
438 |
nFollowBytes := 5. |
|
439 |
minValue := 16r4000000. |
|
440 |
] ifFalse:[ |
|
441 |
^ DecodingError newException |
|
442 |
defaultValue:aStringOrByteCollection; |
|
443 |
raiseRequestWith:aStringOrByteCollection errorString:'invalid utf8 encoding'. |
|
444 |
]]]]]. |
|
445 |
||
446 |
nFollowBytes timesRepeat:[ |
|
447 |
byte := aStringOrByteCollection byteAt:srcIdx. |
|
448 |
srcIdx := srcIdx + 1. |
|
449 |
(byte bitAnd:2r11000000) ~~ 2r10000000 ifTrue:[ |
|
450 |
^ DecodingError newException |
|
451 |
defaultValue:aStringOrByteCollection; |
|
452 |
raiseRequestWith:aStringOrByteCollection errorString:'illegal followbyte'. |
|
453 |
]. |
|
454 |
ascii := (ascii bitShift:6) bitOr:(byte bitAnd:2r00111111). |
|
455 |
]. |
|
456 |
ascii > 16rFFFF ifTrue:[ |
|
457 |
nBitsRequired < 32 ifTrue:[nBitsRequired := 32]. |
|
458 |
] ifFalse:[ascii > 16rFF ifTrue:[ |
|
459 |
nBitsRequired < 16 ifTrue:[nBitsRequired := 16]. |
|
460 |
]]. |
|
461 |
ascii < minValue ifTrue:[ |
|
462 |
"encoding a value in a longer utf8-sequence than required is not allowed |
|
463 |
and imposes a security risk" |
|
464 |
^ DecodingError newException |
|
465 |
defaultValue:aStringOrByteCollection; |
|
466 |
raiseRequestWith:aStringOrByteCollection errorString:'overlong utf8 sequence'. |
|
467 |
]. |
|
468 |
]. |
|
469 |
sz := sz + 1. |
|
470 |
]. |
|
471 |
||
472 |
nBitsRequired == 8 ifTrue:[ |
|
473 |
anyAbove7BitAscii ifFalse:[ |
|
474 |
"/ can return the original string |
|
475 |
^ aStringOrByteCollection asSingleByteString. |
|
476 |
]. |
|
477 |
newString := String uninitializedNew:sz |
|
478 |
] ifFalse:[nBitsRequired <= 16 ifTrue:[ |
|
479 |
newString := Unicode16String new:sz |
|
480 |
] ifFalse:[ |
|
481 |
newString := Unicode32String new:sz |
|
482 |
]]. |
|
483 |
||
484 |
idx := srcIdx := 1. |
|
485 |
||
486 |
"now fill the string" |
|
487 |
[srcIdx <= lastIdx] whileTrue:[ |
|
488 |
byte := ascii := aStringOrByteCollection byteAt:srcIdx. |
|
489 |
srcIdx := srcIdx + 1. |
|
490 |
(byte bitAnd:2r10000000) ~~ 0 ifTrue:[ |
|
491 |
(byte bitAnd:2r11100000) == 2r11000000 ifTrue:[ |
|
492 |
ascii := byte bitAnd:2r00011111. |
|
493 |
nFollowBytes := 1. |
|
494 |
] ifFalse:[(byte bitAnd:2r11110000) == 2r11100000 ifTrue:[ |
|
495 |
ascii := byte bitAnd:2r00001111. |
|
496 |
nFollowBytes := 2. |
|
497 |
] ifFalse:[(byte bitAnd:2r11111000) == 2r11110000 ifTrue:[ |
|
498 |
ascii := byte bitAnd:2r00000111. |
|
499 |
nFollowBytes := 3. |
|
500 |
] ifFalse:[(byte bitAnd:2r11111100) == 2r11111000 ifTrue:[ |
|
501 |
"5 byte sequences do not result in valid unicode" |
|
502 |
ascii := byte bitAnd:2r00000011. |
|
503 |
nFollowBytes := 4. |
|
504 |
] ifFalse:[(byte bitAnd:2r11111110) == 2r11111100 ifTrue:[ |
|
505 |
"6 byte sequences do not result in valid unicode" |
|
506 |
ascii := byte bitAnd:2r00000001. |
|
507 |
nFollowBytes := 5. |
|
508 |
]]]]]. |
|
509 |
nFollowBytes timesRepeat:[ |
|
510 |
byte := aStringOrByteCollection byteAt:srcIdx. |
|
511 |
srcIdx := srcIdx + 1. |
|
512 |
ascii := (ascii bitShift:6) bitOr:(byte bitAnd:2r00111111). |
|
513 |
]. |
|
514 |
]. |
|
515 |
newString at:idx put:(Character value:ascii). |
|
516 |
idx := idx + 1. |
|
517 |
]. |
|
518 |
^ newString |
|
519 |
||
520 |
" |
|
521 |
CharacterArray decodeFromUTF8:#[ 16r41 16r42 ] |
|
522 |
CharacterArray decodeFromUTF8:#[ 16rC6 16r8F ] |
|
523 |
CharacterArray decodeFromUTF8:#[ 16rE0 16r81 16r02 ] |
|
524 |
CharacterArray decodeFromUTF8:#[ 16rEF 16rBF 16rBF ] |
|
525 |
||
526 |
rfc2279 (deprecated) examples: |
|
527 |
CharacterArray decodeFromUTF8:#[ 16r41 16rE2 16r89 16rA2 16rCE 16r91 16r2E ] |
|
528 |
CharacterArray decodeFromUTF8:#[ 16rED 16r95 16r9C 16rEA 16rB5 16rAD 16rEC 16r96 16rB4 ] |
|
529 |
CharacterArray decodeFromUTF8:#[ 16rE6 16r97 16rA5 16rE6 16r9C 16rAC 16rE8 16rAA 16r9E ] |
|
530 |
||
531 |
invalid: |
|
532 |
CharacterArray decodeFromUTF8:#[ 16rC0 16r80 ] |
|
533 |
CharacterArray decodeFromUTF8:#[ 16rE0 16r80 16r80 ] |
|
534 |
" |
|
535 |
||
536 |
" |
|
537 |
CharacterArray decodeFromUTF8:#[ 16r41 16r42 ] |
|
538 |
CharacterArray decodeFromUTF8:#[ 16rC1 16r02 ] |
|
539 |
CharacterArray decodeFromUTF8:#[ 16rE0 16r81 16r02 ] |
|
540 |
CharacterArray decodeFromUTF8:#[ 16rEF 16rBF 16rBF ] |
|
7876 | 541 |
|
542 |
rfc2279 examples: |
|
19837 | 543 |
CharacterArray decodeFromUTF8:#[ 16r41 16rE2 16r89 16rA2 16rCE 16r91 16r2E ] |
544 |
CharacterArray decodeFromUTF8:#[ 16rED 16r95 16r9C 16rEA 16rB5 16rAD 16rEC 16r96 16rB4 ] |
|
545 |
CharacterArray decodeFromUTF8:#[ 16rE6 16r97 16rA5 16rE6 16r9C 16rAC 16rE8 16rAA 16r9E ] |
|
7876 | 546 |
|
547 |
invalid: |
|
19837 | 548 |
CharacterArray decodeFromUTF8:#[ 16rC0 16r80 ] |
549 |
CharacterArray decodeFromUTF8:#[ 16rE0 16r80 16r80 ] |
|
7876 | 550 |
" |
20010 | 551 |
! |
552 |
||
20011 | 553 |
setupNormalizationMaps |
554 |
"returns a 2-stage map from ch2 -> ch1 -> mappedChar |
|
555 |
for unicode normalization (i.e. for making combining chars regular ones)" |
|
556 |
||
557 |
|def| |
|
558 |
||
559 |
UnicodeNormalizationMap := Dictionary new. |
|
20013 | 560 |
UnicodeDenormalizationMap := Dictionary new. |
561 |
||
562 |
def := [:combiner :chars :mappedChars | |
|
20011 | 563 |
|d| |
564 |
||
20013 | 565 |
d := UnicodeNormalizationMap at:combiner ifAbsentPut:[Dictionary new]. |
566 |
chars with:mappedChars do:[:ch1 :mappedChar | |
|
567 |
d at:ch1 put:mappedChar. |
|
568 |
UnicodeDenormalizationMap at:mappedChar put:(Unicode16String with:ch1 with:combiner). |
|
569 |
]. |
|
20011 | 570 |
]. |
20014 | 571 |
def value:(Character codePoint:16r0300) "grave" value:'AEIOUaeiouWw' value:'ÀÈÌÒÙàèìòùẀẁ'. |
572 |
def value:(Character codePoint:16r0301) "acute" value:'AEIOUYaeiouyÇçKkMmPpWw' value:'ÁÉÍÓÚÝáéíóúýḈḉḰḱḾḿṔṕẂẃ'. |
|
573 |
def value:(Character codePoint:16r0302) "circumflex" value:'AEIOUaeiouZz' value:'ÂÊÎÔÛâêîôûẐẑ'. |
|
20011 | 574 |
def value:(Character codePoint:16r0303) "tilde" value:'AONaon' value:'ÃÕÑãõñ'. |
20014 | 575 |
def value:(Character codePoint:16r0307) "dot over" value:'BbDdFfHhMmNnPpRrSsTtWwXxYyṢṣ' value:'ḂḃḊḋḞḟḢḣṀṁṄṅṖṗṘṙṠṡṪṫẆẇẊẋẎẏṨṩ'. |
576 |
def value:(Character codePoint:16r0308) "diaresis" value:'AEIOUaeiouyHhWwXxt' value:'ÄËÏÖÜäëïöüÿḦḧẄẅẌẍẗ'. |
|
577 |
def value:(Character codePoint:16r030A) "ring" value:'Aawy' value:'Ååẘẙ'. |
|
578 |
def value:(Character codePoint:16r0323) "dot under" value:'AaBbDdEeIiHhKkLlMmNnOoRrSsTtUuVvWwZzṠṡ' value:'ẠạḄḅḌḍẸẹỊịḤḥḲḳḶḷṂṃṆṇỌọṚṛṢṣṬṭỤụṾṿẈẉẒẓṨṩ'. |
|
20011 | 579 |
def value:(Character codePoint:16r0327) "cedilla" value:'Cc' value:'Çç'. |
580 |
||
581 |
" |
|
20014 | 582 |
self setupNormalizationMaps |
20011 | 583 |
" |
584 |
! |
|
585 |
||
586 |
unicodeDenormalizationMap |
|
587 |
"returns a 2-stage map from ch2 -> ch1 -> mappedChar |
|
588 |
for unicode normalization (i.e. for making combining chars regular ones)" |
|
589 |
||
590 |
UnicodeDenormalizationMap isNil ifTrue:[ |
|
591 |
self setupNormalizationMaps |
|
592 |
]. |
|
593 |
^ UnicodeDenormalizationMap |
|
594 |
||
595 |
" |
|
596 |
self unicodeDenormalizationMap |
|
597 |
" |
|
598 |
! |
|
599 |
||
20010 | 600 |
unicodeNormalizationMap |
601 |
"returns a 2-stage map from ch2 -> ch1 -> mappedChar |
|
602 |
for unicode normalization (i.e. for making combining chars regular ones)" |
|
603 |
||
604 |
UnicodeNormalizationMap isNil ifTrue:[ |
|
20011 | 605 |
self setupNormalizationMaps |
20010 | 606 |
]. |
607 |
^ UnicodeNormalizationMap |
|
608 |
||
609 |
" |
|
610 |
self unicodeNormalizationMap |
|
611 |
" |
|
443 | 612 |
! ! |
613 |
||
1849
9f35f5934ec6
added msdos-latin1 en/decoder (code = #msdos850)
Claus Gittinger <cg@exept.de>
parents:
1669
diff
changeset
|
614 |
!CharacterArray class methodsFor:'pattern matching'! |
64 | 615 |
|
5342
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
616 |
matchEscapeCharacter |
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
617 |
"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
|
618 |
(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
|
619 |
|
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
620 |
^ $\ |
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
621 |
! |
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
622 |
|
15734 | 623 |
matchScan:matchScanArray from:matchStart to:matchStop with:aString from:start to:stop caseSensitive:caseSensitive |
327 | 624 |
"helper for match; return true if the characters from start to stop in |
625 |
aString are matching the scan in matchScan from matchStart to matchStop. |
|
328 | 626 |
The matchScan is as created by asMatchScanArray. |
327 | 627 |
|
8874 | 628 |
This algorithm is not at all the most efficient; |
629 |
for heavy duty pattern matching, an interface (primitive) to the regex |
|
328 | 630 |
pattern matching package should be added." |
327 | 631 |
|
8874 | 632 |
|matchEntry |
327 | 633 |
mStart "{ Class: SmallInteger }" |
634 |
mStop "{ Class: SmallInteger }" |
|
635 |
sStart "{ Class: SmallInteger }" |
|
636 |
sStop "{ Class: SmallInteger }" |
|
637 |
mSize "{ Class: SmallInteger }" |
|
638 |
sSize "{ Class: SmallInteger }" |
|
639 |
index "{ Class: SmallInteger }" |
|
2561 | 640 |
quickCheck matchLast nextMatchEntry |
986 | 641 |
checkChar included| |
327 | 642 |
|
643 |
mStart := matchStart. |
|
644 |
mStop := matchStop. |
|
645 |
sStart := start. |
|
646 |
sStop := stop. |
|
647 |
||
16245 | 648 |
[ |
8874 | 649 |
"/ Transcript showCR:('match: ''' , (aString copyFrom:sStart to:sStop) , |
3537 | 650 |
"/ ''' against:' , (matchScanArray copyFrom:mStart to:mStop) printString). |
651 |
||
20012 | 652 |
mSize := mStop - mStart + 1. |
653 |
sSize := sStop - sStart + 1. |
|
654 |
||
655 |
"empty strings match" |
|
656 |
(mSize == 0) ifTrue:[^ (sSize == 0)]. |
|
657 |
||
658 |
matchEntry := matchScanArray at:mStart. |
|
659 |
||
660 |
"/ the most common case first: |
|
661 |
(sSize ~~ 0 |
|
662 |
and:[(checkChar := (aString at:sStart)) = matchEntry]) ifTrue:[ |
|
663 |
"advance by one and continue" |
|
664 |
mStart := mStart + 1. |
|
665 |
sStart := sStart + 1 |
|
666 |
] ifFalse:[ |
|
667 |
(matchEntry == #any) ifTrue:[ |
|
668 |
"restString empty -> no match" |
|
669 |
(sSize == 0) ifTrue:[^ false]. |
|
670 |
"# matches single character" |
|
671 |
((sSize == 1) and:[mSize == 1]) ifTrue:[^ true]. |
|
672 |
"advance by one and continue" |
|
673 |
mStart := mStart + 1. |
|
674 |
sStart := sStart + 1 |
|
675 |
] ifFalse:[ |
|
676 |
(matchEntry == #anyString) ifTrue:[ |
|
677 |
"* alone matches anything" |
|
678 |
(mSize == 1) ifTrue:[^ true]. |
|
679 |
"restString empty & matchString not empty -> no match" |
|
680 |
(sSize == 0) ifTrue:[^ false]. |
|
681 |
||
682 |
" |
|
683 |
try to avoid some of the recursion by checking last |
|
684 |
character and continue with shortened strings if possible |
|
685 |
" |
|
686 |
quickCheck := false. |
|
687 |
(mStop >= mStart) ifTrue:[ |
|
688 |
matchLast := matchScanArray at:mStop. |
|
689 |
(matchLast ~~ #anyString) ifTrue:[ |
|
690 |
(matchLast == #any) ifTrue:[ |
|
691 |
quickCheck := true |
|
692 |
] ifFalse:[ |
|
693 |
matchLast == (aString at:sStop) ifTrue:[ |
|
694 |
quickCheck := true |
|
695 |
] ifFalse:[ |
|
696 |
matchLast isString ifTrue:[ |
|
697 |
quickCheck := matchLast includes:(aString at:sStop) |
|
698 |
] |
|
699 |
] |
|
700 |
] |
|
701 |
] |
|
702 |
]. |
|
703 |
quickCheck ifTrue:[ |
|
704 |
" |
|
705 |
quickCheck ok, advance from the right |
|
706 |
" |
|
707 |
mStop := mStop - 1. |
|
708 |
sStop := sStop - 1 |
|
709 |
] ifFalse:[ |
|
710 |
"/ no quick check; |
|
711 |
"/ look for the next character(s) |
|
712 |
"/ and try matching there |
|
713 |
"/ (to avoid recursion) |
|
714 |
||
715 |
mStart < mStop ifTrue:[ |
|
716 |
nextMatchEntry := matchScanArray at:mStart+1. |
|
717 |
nextMatchEntry isCharacter ifTrue:[ |
|
718 |
sStart <= sStop ifTrue:[ |
|
719 |
[ |
|
720 |
caseSensitive ifTrue:[ |
|
721 |
index := aString indexOf:nextMatchEntry startingAt:sStart |
|
722 |
] ifFalse:[ |
|
723 |
index := aString findFirst:[:c | c asLowercase = nextMatchEntry asLowercase] |
|
724 |
startingAt:sStart. |
|
725 |
]. |
|
726 |
(index == 0 or:[index > sStop]) ifTrue:[ |
|
727 |
^ false |
|
728 |
]. |
|
729 |
(self matchScan:matchScanArray |
|
730 |
from:(mStart + 1) |
|
731 |
to:mStop |
|
732 |
with:aString |
|
733 |
from:index |
|
734 |
to:sStop |
|
735 |
caseSensitive:caseSensitive |
|
736 |
) ifTrue:[ |
|
737 |
^ true |
|
738 |
]. |
|
739 |
sStart := index + 1. |
|
740 |
] loop. |
|
741 |
] |
|
742 |
] |
|
743 |
]. |
|
744 |
||
745 |
" |
|
746 |
no quick check possible; |
|
747 |
loop over all possible substrings |
|
748 |
" |
|
749 |
index := sStart. |
|
750 |
[index <= sStop] whileTrue:[ |
|
751 |
(self matchScan:matchScanArray |
|
752 |
from:(mStart + 1) |
|
753 |
to:mStop |
|
754 |
with:aString |
|
755 |
from:index |
|
756 |
to:sStop |
|
757 |
caseSensitive:caseSensitive |
|
758 |
) ifTrue:[ |
|
759 |
^ true |
|
760 |
]. |
|
761 |
index := index + 1 |
|
762 |
]. |
|
763 |
^ false |
|
764 |
]. |
|
765 |
] ifFalse:[ |
|
766 |
(matchEntry isString) ifTrue:[ |
|
767 |
"testString empty -> no match" |
|
768 |
(sSize == 0) ifTrue:[^ false]. |
|
769 |
||
770 |
included := false. |
|
771 |
"/ checkChar := aString at:sStart. |
|
772 |
included := matchEntry includes:checkChar. |
|
773 |
included ifFalse:[ |
|
774 |
caseSensitive ifFalse:[ |
|
775 |
checkChar isUppercase ifTrue:[ |
|
776 |
included := matchEntry includes:checkChar asLowercase. |
|
777 |
] ifFalse:[ |
|
778 |
included := matchEntry includes:checkChar asUppercase. |
|
779 |
] |
|
780 |
]. |
|
781 |
]. |
|
782 |
mStart := mStart + 1. |
|
783 |
mSize := mSize - 1. |
|
784 |
included ifFalse:[^ false]. |
|
785 |
||
786 |
((sSize == 1) and:[mSize == 0]) ifTrue:[^ true]. |
|
787 |
] ifFalse:[ |
|
788 |
"/ must be single character |
|
789 |
||
790 |
"testString empty ?" |
|
791 |
(sSize == 0) ifTrue:[^ false]. |
|
792 |
||
793 |
"first characters equal ?" |
|
794 |
"/ checkChar := aString at:sStart. |
|
795 |
caseSensitive ifTrue:[^ false]. |
|
796 |
(checkChar asUppercase ~= matchEntry asUppercase) ifTrue:[^ false]. |
|
797 |
||
798 |
"advance and continue" |
|
799 |
mStart := mStart + 1. |
|
800 |
]. |
|
801 |
"cut off 1st char and continue" |
|
802 |
sStart := sStart + 1 |
|
803 |
] |
|
804 |
] |
|
805 |
] |
|
16245 | 806 |
] loop. |
2519
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
807 |
|
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
808 |
" |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
809 |
|scanArray s| |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
810 |
|
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
811 |
scanArray := self matchScanArrayFrom:'*hello'. |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
812 |
s := 'foo bar hello world'. |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
813 |
CharacterArray |
20012 | 814 |
matchScan:scanArray |
815 |
from:1 |
|
816 |
to:scanArray size |
|
817 |
with:s |
|
818 |
from:1 |
|
819 |
to:s size |
|
820 |
caseSensitive:true |
|
2519
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
821 |
" |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
822 |
" |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
823 |
|scanArray s| |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
824 |
|
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
825 |
scanArray := self matchScanArrayFrom:'*hello*'. |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
826 |
s := 'foo bar hello world'. |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
827 |
CharacterArray |
20012 | 828 |
matchScan:scanArray |
829 |
from:1 |
|
830 |
to:scanArray size |
|
831 |
with:s |
|
832 |
from:1 |
|
833 |
to:s size |
|
834 |
caseSensitive:true |
|
15734 | 835 |
" |
836 |
||
837 |
"Modified: / 24-07-2011 / 07:17:03 / cg" |
|
838 |
"Modified (comment): / 24-07-2011 / 08:55:14 / cg" |
|
839 |
! |
|
840 |
||
841 |
matchScan:matchScanArray from:matchStart to:matchStop with:aString from:start to:stop ignoreCase:ignoreCase |
|
842 |
<resource: #obsolete> |
|
843 |
"helper for match; return true if the characters from start to stop in |
|
844 |
aString are matching the scan in matchScan from matchStart to matchStop. |
|
845 |
The matchScan is as created by asMatchScanArray. |
|
846 |
||
847 |
This algorithm is not at all the most efficient; |
|
848 |
for heavy duty pattern matching, an interface (primitive) to the regex |
|
849 |
pattern matching package should be added." |
|
850 |
||
851 |
^ self |
|
20012 | 852 |
matchScan:matchScanArray from:matchStart to:matchStop with:aString from:start to:stop caseSensitive:ignoreCase not |
15734 | 853 |
|
854 |
" |
|
855 |
|scanArray s| |
|
856 |
||
857 |
scanArray := self matchScanArrayFrom:'*hello'. |
|
858 |
s := 'foo bar hello world'. |
|
859 |
CharacterArray |
|
20012 | 860 |
matchScan:scanArray |
861 |
from:1 |
|
862 |
to:scanArray size |
|
863 |
with:s |
|
864 |
from:1 |
|
865 |
to:s size |
|
866 |
ignoreCase:false |
|
15734 | 867 |
" |
868 |
" |
|
869 |
|scanArray s| |
|
870 |
||
871 |
scanArray := self matchScanArrayFrom:'*hello*'. |
|
872 |
s := 'foo bar hello world'. |
|
873 |
CharacterArray |
|
20012 | 874 |
matchScan:scanArray |
875 |
from:1 |
|
876 |
to:scanArray size |
|
877 |
with:s |
|
878 |
from:1 |
|
879 |
to:s size |
|
880 |
ignoreCase:false |
|
13520 | 881 |
" |
882 |
||
883 |
"Modified: / 24-07-2011 / 07:17:03 / cg" |
|
884 |
"Modified (comment): / 24-07-2011 / 08:55:14 / cg" |
|
608 | 885 |
! |
886 |
||
887 |
matchScanArrayFrom:aString |
|
888 |
"scan a pattern string and decompose it into a scanArray. |
|
889 |
This is processed faster (especially with character ranges), and |
|
890 |
can also be reused later. (if the same pattern is to be searched again)" |
|
891 |
||
9282
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
892 |
^ self matchScanArrayFrom:aString escapeCharacter:(self matchEscapeCharacter) |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
893 |
|
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
894 |
" |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
895 |
String matchScanArrayFrom:'*ute*' |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
896 |
String matchScanArrayFrom:'**ute**' |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
897 |
String matchScanArrayFrom:'*uter' |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
898 |
String matchScanArrayFrom:'\*uter' |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
899 |
String matchScanArrayFrom:'[cC]#mpute[rR]' |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
900 |
String matchScanArrayFrom:'[abcd]*' |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
901 |
String matchScanArrayFrom:'[a-k]*' |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
902 |
String matchScanArrayFrom:'*some*compl*ern*' |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
903 |
String matchScanArrayFrom:'[a-' |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
904 |
String matchScanArrayFrom:'[a-zA-Z]' |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
905 |
String matchScanArrayFrom:'[a-z01234A-Z]' |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
906 |
" |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
907 |
|
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
908 |
"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
|
909 |
! |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
910 |
|
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
911 |
matchScanArrayFrom:aString escapeCharacter:escape |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
912 |
"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
|
913 |
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
|
914 |
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
|
915 |
|
8874 | 916 |
|coll |
608 | 917 |
idx "{ Class: SmallInteger }" |
9282
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
918 |
end c1 c2 matchSet previous| |
5342
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
919 |
|
5487 | 920 |
previous := nil. |
608 | 921 |
|
922 |
coll := OrderedCollection new. |
|
923 |
idx := 1. end := aString size. |
|
924 |
[idx <= end] whileTrue:[ |
|
20012 | 925 |
|char this| |
926 |
||
927 |
char := aString at:idx. |
|
928 |
char == $* ifTrue:[ |
|
929 |
previous ~~ #anyString ifTrue:[ |
|
930 |
this := #anyString |
|
931 |
] |
|
932 |
] ifFalse:[ |
|
933 |
char == $# ifTrue:[ |
|
934 |
previous ~~ #anyString ifTrue:[ |
|
935 |
this := #any |
|
936 |
] |
|
937 |
] ifFalse:[ |
|
938 |
char == $[ ifTrue:[ |
|
939 |
matchSet := IdentitySet new. |
|
940 |
idx := idx + 1. |
|
941 |
idx > end ifTrue:[^ nil]. |
|
942 |
char := aString at:idx. |
|
943 |
c1 := nil. |
|
944 |
[char ~~ $]] whileTrue:[ |
|
945 |
((char == $-) and:[c1 notNil]) ifTrue:[ |
|
946 |
idx := idx + 1. |
|
947 |
idx > end ifTrue:[^ nil]. |
|
948 |
c2 := aString at:idx. |
|
949 |
c1 to:c2 do:[:c | matchSet add:c]. |
|
950 |
c1 := nil. |
|
951 |
idx := idx + 1. |
|
952 |
] ifFalse:[ |
|
953 |
(char ~~ $]) ifTrue:[ |
|
954 |
matchSet add:char. |
|
955 |
c1 := char. |
|
956 |
idx := idx + 1 |
|
957 |
] |
|
958 |
]. |
|
959 |
idx > end ifTrue:[^ nil]. |
|
960 |
char := aString at:idx |
|
961 |
]. |
|
962 |
this := matchSet asString |
|
963 |
] ifFalse:[ |
|
964 |
char == escape ifTrue:[ |
|
965 |
idx := idx + 1. |
|
966 |
idx > end ifTrue:[ |
|
967 |
"/ mhmh - what should we do here ? |
|
968 |
this := char |
|
969 |
] ifFalse:[ |
|
970 |
this := aString at:idx. |
|
971 |
] |
|
972 |
] ifFalse:[ |
|
973 |
this := char |
|
974 |
] |
|
975 |
] |
|
976 |
] |
|
977 |
]. |
|
978 |
this notNil ifTrue:[coll add:this. previous := this]. |
|
979 |
idx := idx + 1 |
|
608 | 980 |
]. |
981 |
||
982 |
^ coll asArray |
|
983 |
||
984 |
" |
|
8874 | 985 |
String matchScanArrayFrom:'*ute*' |
986 |
String matchScanArrayFrom:'**ute**' |
|
987 |
String matchScanArrayFrom:'*uter' |
|
988 |
String matchScanArrayFrom:'\*uter' |
|
989 |
String matchScanArrayFrom:'[cC]#mpute[rR]' |
|
990 |
String matchScanArrayFrom:'[abcd]*' |
|
991 |
String matchScanArrayFrom:'[a-k]*' |
|
992 |
String matchScanArrayFrom:'*some*compl*ern*' |
|
993 |
String matchScanArrayFrom:'[a-' |
|
994 |
String matchScanArrayFrom:'[a-zA-Z]' |
|
995 |
String matchScanArrayFrom:'[a-z01234A-Z]' |
|
608 | 996 |
" |
2519
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
997 |
|
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
998 |
"Modified: 2.4.1997 / 16:20:29 / cg" |
9192 | 999 |
! ! |
1000 |
||
1001 |
!CharacterArray class methodsFor:'queries'! |
|
1002 |
||
1003 |
isAbstract |
|
11229 | 1004 |
"Return if this class is an abstract class. |
1005 |
True is returned for CharacterArray here; false for subclasses. |
|
19464 | 1006 |
Abstract subclasses must redefine this again." |
11229 | 1007 |
|
9192 | 1008 |
^ self == CharacterArray |
18597 | 1009 |
! |
1010 |
||
1011 |
speciesForCharacterSize:characterSize |
|
1012 |
"answer the class, that is able to hold characters of size characterSize" |
|
1013 |
||
1014 |
characterSize <= 8 ifTrue:[^ String]. |
|
1015 |
characterSize <= 16 ifTrue:[^ Unicode16String]. |
|
1016 |
^ Unicode32String |
|
9192 | 1017 |
! ! |
1018 |
||
1019 |
!CharacterArray methodsFor:'Compatibility-ANSI'! |
|
1020 |
||
1021 |
addLineDelimiters |
|
1022 |
"Ansi compatibility - same as withCRs" |
|
1023 |
||
1024 |
^ self withCRs |
|
1025 |
||
1026 |
"Modified: / 13.11.2001 / 19:16:25 / cg" |
|
17543 | 1027 |
! ! |
1028 |
||
1029 |
!CharacterArray methodsFor:'Compatibility-Dolphin'! |
|
9192 | 1030 |
|
1031 |
copyExpanding:expandTable |
|
1032 |
"return a copy of myself, with translations from the expandTable sliced in. |
|
18392 | 1033 |
The argument is supposed to map from characters to either characters or strings. |
1034 |
Added for Dolphin compatibility" |
|
9192 | 1035 |
|
1036 |
|ds| |
|
1037 |
||
1038 |
ds := WriteStream on:(self species new). |
|
1039 |
self do:[:eachChar | |
|
20012 | 1040 |
|repl| |
1041 |
||
1042 |
repl := expandTable at:eachChar ifAbsent:[nil]. |
|
1043 |
repl isNil ifTrue:[ |
|
1044 |
ds nextPut:eachChar |
|
1045 |
] ifFalse:[ |
|
1046 |
repl size == 0 ifTrue:[ |
|
1047 |
ds nextPut:repl |
|
1048 |
] ifFalse:[ |
|
1049 |
ds nextPutAll:repl |
|
1050 |
] |
|
1051 |
]. |
|
9192 | 1052 |
]. |
6870 | 1053 |
^ ds contents. |
1054 |
! |
|
1055 |
||
1056 |
formatWith:aString |
|
11955 | 1057 |
"Compatibility method - do not use in new code. |
18392 | 1058 |
same as #bindWith: for Dolphin compatibility" |
6870 | 1059 |
|
1060 |
^ self bindWith:aString |
|
1061 |
||
1062 |
" |
|
1063 |
'hello%1world' formatWith:'123' |
|
1064 |
" |
|
1065 |
! |
|
1066 |
||
1067 |
formatWith:arg1 with:arg2 |
|
11955 | 1068 |
"Compatibility method - do not use in new code. |
18392 | 1069 |
same as #bindWith: for Dolphin compatibility" |
6870 | 1070 |
|
1071 |
^ self bindWith:arg1 with:arg2 |
|
1072 |
||
1073 |
" |
|
1074 |
'hello%1 %2world' formatWith:'123' with:234 |
|
1075 |
" |
|
1076 |
! |
|
1077 |
||
1078 |
formatWith:arg1 with:arg2 with:arg3 |
|
11955 | 1079 |
"Compatibility method - do not use in new code. |
18392 | 1080 |
same as #bindWith: for Dolphin compatibility" |
6870 | 1081 |
|
1082 |
^ self bindWith:arg1 with:arg2 with:arg3 |
|
1083 |
||
1084 |
" |
|
1085 |
'hello%1 %2 %3world' formatWith:'123' with:234 with:345 |
|
1086 |
" |
|
6391 | 1087 |
! ! |
1088 |
||
14681 | 1089 |
!CharacterArray methodsFor:'Compatibility-GNU'! |
1090 |
||
1091 |
% anArrayOfOperands |
|
1092 |
"return a copy of the receiver, where a '%i' escape |
|
1093 |
is replaced by the coresponding string from the argument array. |
|
1094 |
'i' may be between 1 and 9 (i.e. a maximum of 9 placeholders is allowed). |
|
1095 |
Added for GNU-ST compatibility." |
|
1096 |
||
1097 |
^ self expandPlaceholdersWith:anArrayOfOperands |
|
1098 |
||
1099 |
" |
|
1100 |
'do you prefer %1 or rather %2 (not talking about %3) ?' |
|
20012 | 1101 |
% #('smalltalk' 'c++' 'c') |
14681 | 1102 |
|
1103 |
'do you %(what) ?' |
|
20012 | 1104 |
% (Dictionary new at:#'what' put:'understand'; yourself) |
14681 | 1105 |
" |
1106 |
! ! |
|
1107 |
||
7261 | 1108 |
!CharacterArray methodsFor:'Compatibility-ST/V'! |
608 | 1109 |
|
17517 | 1110 |
asArrayOfSubstrings |
1111 |
"return an array of substrings from the receiver, interpreting |
|
1112 |
separators (i.e. spaces & newlines) as word-delimiters. |
|
18390 | 1113 |
This has been added for ST/V compatibility |
1114 |
- the actual work is done in asCollectionOfWords. |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
1115 |
(sigh: it is called #'subStrings' in V'Age, #'substrings' in Squeak |
17517 | 1116 |
and #'asCollectionOfWords' in ST/X) " |
1117 |
||
1118 |
^ self asCollectionOfWords asArray |
|
1119 |
||
1120 |
" |
|
1121 |
'1 one two three four 5 five' asArrayOfSubstrings |
|
1122 |
'1 |
|
1123 |
one |
|
20012 | 1124 |
two three four 5 five' asArrayOfSubstrings |
17517 | 1125 |
" |
1126 |
! |
|
1127 |
||
608 | 1128 |
byteAt:index put:aByte |
1129 |
"store a byte at given index. |
|
1130 |
This is an ST/V compatibility method." |
|
1131 |
||
1333
d3fdca956f0d
allow storing of 0-bytes (be careful with printing then ...)
Claus Gittinger <cg@exept.de>
parents:
1312
diff
changeset
|
1132 |
"/ (aByte == 0) ifTrue:[ |
d3fdca956f0d
allow storing of 0-bytes (be careful with printing then ...)
Claus Gittinger <cg@exept.de>
parents:
1312
diff
changeset
|
1133 |
"/ "store a space instead" |
d3fdca956f0d
allow storing of 0-bytes (be careful with printing then ...)
Claus Gittinger <cg@exept.de>
parents:
1312
diff
changeset
|
1134 |
"/ ^ super basicAt:index put:(Character space) |
d3fdca956f0d
allow storing of 0-bytes (be careful with printing then ...)
Claus Gittinger <cg@exept.de>
parents:
1312
diff
changeset
|
1135 |
"/ ]. |
11754
5132248cfd6e
common definition of #byteAt:put between CharacterArray and Symbol
Stefan Vogel <sv@exept.de>
parents:
11632
diff
changeset
|
1136 |
^ self basicAt:index put:(Character value:aByte) |
9192 | 1137 |
|
1138 |
" |
|
1139 |
'hello' copy at:1 put:$H asciiValue; yourself |
|
1140 |
'hello' copy byteAt:1 put:72; yourself |
|
1141 |
'hello' copy byteAt:1 put:0; yourself |
|
1142 |
" |
|
1143 |
||
1144 |
"Modified: 6.5.1996 / 10:35:26 / cg" |
|
1145 |
! |
|
608 | 1146 |
|
19497 | 1147 |
equalsIgnoreCase:aString |
1148 |
"This is an ST/V compatibility method and an alias for sameAs:." |
|
1149 |
||
1150 |
^ self sameAs:aString |
|
1151 |
||
1152 |
" |
|
1153 |
'abc' equalsIgnoreCase: 'aBC' |
|
1154 |
" |
|
1155 |
! |
|
1156 |
||
608 | 1157 |
replChar:oldChar with:newChar |
19497 | 1158 |
"return a copy of the receiver, with all oldChars replaced by newChar. |
1159 |
This is an ST/V compatibility method and an alias for copyReplaceAll." |
|
608 | 1160 |
|
3656 | 1161 |
^ self copyReplaceAll:oldChar with:newChar |
608 | 1162 |
|
1163 |
" |
|
8874 | 1164 |
'12345678901234567890' replChar:$0 with:$* |
608 | 1165 |
" |
3656 | 1166 |
|
1167 |
"Modified: / 18.7.1998 / 22:52:57 / cg" |
|
608 | 1168 |
! |
1169 |
||
1170 |
replChar:oldChar withString:newString |
|
1171 |
"return a copy of the receiver, with all oldChars replaced |
|
1172 |
by newString (i.e. slice in the newString in place of the oldChar). |
|
1173 |
This is an ST/V compatibility method." |
|
1174 |
||
1175 |
|tmpStream| |
|
1176 |
||
15523 | 1177 |
tmpStream := self species writeStream. |
608 | 1178 |
self do:[:element | |
20012 | 1179 |
element = oldChar ifTrue:[ |
1180 |
tmpStream nextPutAll:newString |
|
1181 |
] ifFalse:[ |
|
1182 |
tmpStream nextPut:element |
|
1183 |
]. |
|
608 | 1184 |
]. |
1185 |
^ tmpStream contents |
|
1186 |
||
1187 |
" |
|
8874 | 1188 |
'12345678901234567890' replChar:$0 withString:'foo' |
1189 |
'a string with spaces' replChar:$ withString:' foo ' |
|
608 | 1190 |
" |
9192 | 1191 |
! |
1192 |
||
1193 |
replString:subString withString:newString |
|
4244
5deefa3144fb
added #replString:withString:
Claus Gittinger <cg@exept.de>
parents:
4156
diff
changeset
|
1194 |
"return a copy of the receiver, with all sequences of subString replaced |
18390 | 1195 |
by newString (i.e. slice in the newString in place of the oldString). |
19497 | 1196 |
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
|
1197 |
|
8347 | 1198 |
^ self copyReplaceString:subString withString:newString |
4244
5deefa3144fb
added #replString:withString:
Claus Gittinger <cg@exept.de>
parents:
4156
diff
changeset
|
1199 |
|
5deefa3144fb
added #replString:withString:
Claus Gittinger <cg@exept.de>
parents:
4156
diff
changeset
|
1200 |
" |
8874 | 1201 |
'12345678901234567890' replString:'123' withString:'OneTwoThree' |
1202 |
'12345678901234567890' replString:'123' withString:'*' |
|
1203 |
'12345678901234567890' replString:'234' withString:'foo' |
|
4244
5deefa3144fb
added #replString:withString:
Claus Gittinger <cg@exept.de>
parents:
4156
diff
changeset
|
1204 |
|
5deefa3144fb
added #replString:withString:
Claus Gittinger <cg@exept.de>
parents:
4156
diff
changeset
|
1205 |
('a string with spaces' replChar:$ withString:' foo ') |
20012 | 1206 |
replString:'foo' withString:'bar' |
4244
5deefa3144fb
added #replString:withString:
Claus Gittinger <cg@exept.de>
parents:
4156
diff
changeset
|
1207 |
" |
5deefa3144fb
added #replString:withString:
Claus Gittinger <cg@exept.de>
parents:
4156
diff
changeset
|
1208 |
|
8347 | 1209 |
"Modified: / 12-05-2004 / 12:00:27 / cg" |
9192 | 1210 |
! |
1211 |
||
9275 | 1212 |
subString:start to:end |
18390 | 1213 |
"same as copyFrom:to: |
19497 | 1214 |
This is an ST/V compatibility method and an alias for copyFrom:to:." |
18390 | 1215 |
|
9275 | 1216 |
^ self copyFrom:start to:end |
1217 |
||
1218 |
" |
|
1219 |
'12345678901234567890' subString:3 to:8 |
|
1220 |
" |
|
1221 |
! |
|
1222 |
||
9192 | 1223 |
trimBlanks |
14992 | 1224 |
"return a copy of the receiver without leading and trailing spaces. |
18464 | 1225 |
This is an ST/V compatibility method and an alias for withoutSpaces." |
9192 | 1226 |
|
1227 |
^ self withoutSpaces |
|
1228 |
||
1229 |
" |
|
1230 |
' spaces at beginning' trimBlanks |
|
1231 |
'spaces at end ' trimBlanks |
|
1232 |
' spaces at beginning and end ' trimBlanks |
|
1233 |
'no spaces' trimBlanks |
|
1234 |
" |
|
1235 |
! ! |
|
1236 |
||
1237 |
!CharacterArray methodsFor:'Compatibility-Squeak'! |
|
4409
86e049b21f8d
added #includesSubstring:caseSensitive:
Claus Gittinger <cg@exept.de>
parents:
4389
diff
changeset
|
1238 |
|
19171 | 1239 |
asBoldText |
1240 |
"return self as a bold text" |
|
1241 |
||
1242 |
^Text string: self emphasis: #bold |
|
1243 |
! |
|
1244 |
||
6148 | 1245 |
asDate |
1246 |
"Many allowed forms, see Date.readFrom:" |
|
1247 |
||
1248 |
^ Date readFrom: (ReadStream on: self) |
|
1249 |
||
1250 |
" |
|
8874 | 1251 |
'30 Apr 1999' asDate dayName capitalized |
6148 | 1252 |
" |
1253 |
! |
|
1254 |
||
16051 | 1255 |
asOneByteString |
1256 |
"return the receiver converted to a 'normal' string. |
|
18391 | 1257 |
Same as asSingleByteString - for Squeak/Pharo compatibility." |
16051 | 1258 |
|
1259 |
^ self asSingleByteString |
|
1260 |
! |
|
1261 |
||
8971 | 1262 |
asUrl |
18391 | 1263 |
"Same as asURL - for Squeak/Pharo compatibility." |
1264 |
||
8971 | 1265 |
^ self asURL |
1266 |
! |
|
1267 |
||
10840 | 1268 |
asWideString |
18391 | 1269 |
"return a two-byte string containing the same characters as the receiver. |
1270 |
Same as asTwoByteString - for Squeak/Pharo compatibility." |
|
14405
1d0d99a95bba
comment/format in:7 methods
Claus Gittinger <cg@exept.de>
parents:
14168
diff
changeset
|
1271 |
|
10840 | 1272 |
^ self asTwoByteString |
1273 |
||
1274 |
" |
|
1275 |
'abc' asWideString |
|
1276 |
" |
|
1277 |
! |
|
1278 |
||
4989 | 1279 |
capitalized |
18391 | 1280 |
"same as asUppercaseFirst for Squeak/Pharo compatibility" |
14405
1d0d99a95bba
comment/format in:7 methods
Claus Gittinger <cg@exept.de>
parents:
14168
diff
changeset
|
1281 |
|
4989 | 1282 |
^ self asUppercaseFirst |
1283 |
||
1284 |
" |
|
1285 |
'hello' capitalized |
|
1286 |
" |
|
1287 |
! |
|
1288 |
||
10841 | 1289 |
caseInsensitiveLessOrEqual:aString |
18391 | 1290 |
"compare the receiver against the argument ignoring case differences |
1291 |
For Squeak/Pharo compatibility" |
|
1292 |
||
10841 | 1293 |
^ (self compareCaselessWith:aString) <= 0 |
1294 |
! |
|
1295 |
||
1296 |
caseSensitiveLessOrEqual:aString |
|
18391 | 1297 |
"compare the receiver against the argument caring for case differences |
1298 |
For Squeak/Pharo compatibility" |
|
1299 |
||
10841 | 1300 |
^ self <= aString |
1301 |
! |
|
1302 |
||
1303 |
charactersExactlyMatching:aString |
|
14405
1d0d99a95bba
comment/format in:7 methods
Claus Gittinger <cg@exept.de>
parents:
14168
diff
changeset
|
1304 |
"return the number of characters I share as a prefix with the argument, aString" |
1d0d99a95bba
comment/format in:7 methods
Claus Gittinger <cg@exept.de>
parents:
14168
diff
changeset
|
1305 |
|
10841 | 1306 |
|idx nMax| |
1307 |
||
1308 |
nMax :=(self size) min:(aString size). |
|
1309 |
idx := 1. |
|
1310 |
[idx <= nMax] whileTrue:[ |
|
20012 | 1311 |
(self at:idx) = (aString at:idx) ifFalse:[ |
1312 |
^ idx - 1 |
|
1313 |
]. |
|
1314 |
idx := idx + 1. |
|
10841 | 1315 |
]. |
1316 |
^ nMax |
|
1317 |
||
1318 |
" |
|
12177 | 1319 |
'abc' charactersExactlyMatching:'abc' |
1320 |
'abc' charactersExactlyMatching:'abcd' |
|
1321 |
'abcd' charactersExactlyMatching:'abc' |
|
1322 |
'abc' charactersExactlyMatching:'abd' |
|
1323 |
'abc' charactersExactlyMatching:'xxx' |
|
10841 | 1324 |
" |
1325 |
! |
|
1326 |
||
9192 | 1327 |
endsWithDigit |
1328 |
"Answer whether the receiver's final character represents a digit. 3/11/96 sw" |
|
1329 |
||
18391 | 1330 |
|len| |
1331 |
||
1332 |
^ (len := self size) > 0 and: [(self at:len) isDigit] |
|
9192 | 1333 |
! |
1334 |
||
10703
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
1335 |
findDelimiters:delimiters startingAt:start |
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
1336 |
"Answer the index of the character within the receiver, starting at start, |
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
1337 |
that matches one of the delimiters. |
18391 | 1338 |
If the receiver does not contain any of the delimiters, answer size + 1." |
9192 | 1339 |
|
1340 |
|idx| |
|
1341 |
||
1342 |
idx := self indexOfAny:delimiters startingAt:start. |
|
1343 |
idx == 0 ifTrue:[^ self size + 1]. |
|
1344 |
^ idx. |
|
1345 |
"/start to: self size do: [:i | |
|
1346 |
"/ |char| |
|
1347 |
"/ |
|
1348 |
"/ char := self at: i. |
|
1349 |
"/ delimiters do: [:delim | delim = char ifTrue: [^ i]] |
|
8971 | 1350 |
"/ ]. |
1351 |
"/ ^ self size + 1 |
|
1352 |
! |
|
1353 |
||
13624 | 1354 |
findTokens:delimiterOrDelimiters |
7270 | 1355 |
"cg: I am not sure, if this is really the squeak semantics (w.r.t. empty fields)" |
1356 |
||
13624 | 1357 |
delimiterOrDelimiters size == 0 ifTrue:[ |
20012 | 1358 |
^ self asCollectionOfSubstringsSeparatedBy:delimiterOrDelimiters |
13624 | 1359 |
] ifFalse:[ |
20012 | 1360 |
^ self asCollectionOfSubstringsSeparatedByAny:delimiterOrDelimiters |
13624 | 1361 |
]. |
7270 | 1362 |
|
1363 |
" |
|
8874 | 1364 |
'a|b#c||e' findTokens:#($# $|) |
13624 | 1365 |
'a|b#c||e' findTokens:$| |
1366 |
" |
|
1367 |
||
1368 |
"Modified: / 04-09-2011 / 20:31:21 / cg" |
|
1369 |
"Modified (comment): / 05-09-2011 / 04:25:28 / cg" |
|
4981 | 1370 |
! |
1371 |
||
4974 | 1372 |
includesSubString:aString |
1373 |
"return true, if a substring is contained in the receiver. |
|
1374 |
The compare is case sensitive." |
|
1375 |
||
1376 |
^ self includesString:aString |
|
1377 |
||
1378 |
" |
|
8874 | 1379 |
'hello world' includesSubString:'Hel' |
9192 | 1380 |
'hello world' includesSubString:'hel' |
1381 |
'hello world' includesSubString:'llo' |
|
1382 |
" |
|
1383 |
||
1384 |
||
1385 |
||
1386 |
! |
|
1387 |
||
11178 | 1388 |
includesSubString:aString caseSensitive:caseSensitive |
18391 | 1389 |
"sigh - an alias; added for Squeak/Pharo compatibility" |
1390 |
||
11179 | 1391 |
^ self includesSubstring:aString caseSensitive:caseSensitive |
11178 | 1392 |
! |
1393 |
||
16875
32e61bb3a742
class: CharacterArray>>#includesSubstring: (for Squeak compatibility)
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
16863
diff
changeset
|
1394 |
includesSubstring: aString |
18391 | 1395 |
"sigh - an alias; added for Squeak/Pharo compatibility" |
1396 |
||
16875
32e61bb3a742
class: CharacterArray>>#includesSubstring: (for Squeak compatibility)
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
16863
diff
changeset
|
1397 |
^ self includesSubString: aString |
32e61bb3a742
class: CharacterArray>>#includesSubstring: (for Squeak compatibility)
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
16863
diff
changeset
|
1398 |
|
32e61bb3a742
class: CharacterArray>>#includesSubstring: (for Squeak compatibility)
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
16863
diff
changeset
|
1399 |
"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
|
1400 |
! |
32e61bb3a742
class: CharacterArray>>#includesSubstring: (for Squeak compatibility)
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
16863
diff
changeset
|
1401 |
|
9192 | 1402 |
includesSubstring:aString caseSensitive:caseSensitive |
9190 | 1403 |
"return true, if a substring is contained in the receiver. |
9192 | 1404 |
The argument, caseSensitive controls if case is ignored in the compare." |
1405 |
||
1406 |
"/ for now, a q&d hack ... |
|
1407 |
||
1408 |
caseSensitive ifFalse:[ |
|
20012 | 1409 |
^ self asLowercase includesString:aString asLowercase |
9192 | 1410 |
]. |
9190 | 1411 |
^ self includesString:aString |
1412 |
||
1413 |
" |
|
9192 | 1414 |
'hello world' includesSubstring:'Hel' caseSensitive:true |
1415 |
'hello world' includesSubstring:'Hel' caseSensitive:false |
|
1416 |
" |
|
1417 |
||
1418 |
||
1419 |
||
1420 |
! |
|
9190 | 1421 |
|
6979 | 1422 |
isAllDigits |
1423 |
"Answer whether the receiver's characters are all digits" |
|
1424 |
||
1425 |
^ self conform:[:eachChar | eachChar isDigit] |
|
1426 |
||
1427 |
" |
|
1428 |
'hello world' isAllDigits |
|
1429 |
'12344' isAllDigits |
|
1430 |
" |
|
1431 |
! |
|
1432 |
||
4890 | 1433 |
lastSpacePosition |
18391 | 1434 |
"return the index of the last space character; 0 if there is none. |
1435 |
Added for Squeak/Pharo compatibility" |
|
14405
1d0d99a95bba
comment/format in:7 methods
Claus Gittinger <cg@exept.de>
parents:
14168
diff
changeset
|
1436 |
|
20739 | 1437 |
"/ CG: is this correct? separator or space only??? |
4890 | 1438 |
^ self lastIndexOfSeparator |
1439 |
! |
|
1440 |
||
20738 | 1441 |
linesDo:aBlock |
1442 |
"evaluate the argument, aBlock for all lines, |
|
1443 |
up to the end" |
|
1444 |
||
1445 |
^ self readStream linesDo:aBlock |
|
1446 |
! |
|
1447 |
||
7022 | 1448 |
padded:leftOrRight to:paddedSize with:padCharacter |
20155 | 1449 |
"pad left (leftOrRight == #left) or right" |
14053
acbb97913c30
comment/format in: #padded:to:with:
Claus Gittinger <cg@exept.de>
parents:
14050
diff
changeset
|
1450 |
|
7022 | 1451 |
leftOrRight == #left ifTrue:[ |
20155 | 1452 |
^ self leftPaddedTo:paddedSize with:padCharacter |
9190 | 1453 |
]. |
1454 |
^ self paddedTo:paddedSize with:padCharacter |
|
1455 |
||
1456 |
" |
|
8874 | 1457 |
'hello' padded:#right to:10 with:$. |
9192 | 1458 |
'hello' padded:#left to:10 with:$. |
1459 |
" |
|
14053
acbb97913c30
comment/format in: #padded:to:with:
Claus Gittinger <cg@exept.de>
parents:
14050
diff
changeset
|
1460 |
|
acbb97913c30
comment/format in: #padded:to:with:
Claus Gittinger <cg@exept.de>
parents:
14050
diff
changeset
|
1461 |
"Modified (comment): / 07-03-2012 / 16:31:33 / cg" |
9192 | 1462 |
! |
1463 |
||
1464 |
skipDelimiters:delimiters startingAt:start |
|
1465 |
"Answer the index of the character within the receiver, starting at start, |
|
1466 |
that does NOT match one of the delimiters. |
|
1467 |
If the receiver does not contain any of the delimiters, answer size + 1. |
|
1468 |
Assumes the delimiters to be a non-empty string." |
|
1469 |
||
1470 |
start to:self size do:[:i | |
|
20012 | 1471 |
delimiters detect:[:delim | delim = (self at:i) ] ifNone:[ ^ i ] |
9192 | 1472 |
]. |
1473 |
^ self size + 1 |
|
1474 |
||
1475 |
" |
|
1476 |
'123***7890' skipDelimiters:'*' startingAt:4 |
|
1477 |
'123***7890' skipDelimiters:'*' startingAt:3 |
|
9190 | 1478 |
'123***7890' skipDelimiters:'*' startingAt:10 |
1479 |
'123*******' skipDelimiters:'*' startingAt:10 |
|
8874 | 1480 |
" |
1481 |
! |
|
1482 |
||
9190 | 1483 |
substrings |
18921 | 1484 |
"return a collection consisting of all words contained in the receiver. |
17517 | 1485 |
Words are separated by whitespace. |
1486 |
This has been added for Squeak compatibility. |
|
1487 |
(sigh: it is called #'subStrings' in V'Age, and #'asCollectionOfWords' in ST/X) " |
|
1488 |
||
9190 | 1489 |
^ self asCollectionOfWords |
1490 |
||
1491 |
" |
|
1492 |
'foo bar baz' substrings |
|
1493 |
" |
|
1494 |
! |
|
1495 |
||
12665
b027fc5aec4e
added: #substringsSeparatedBy:
Claus Gittinger <cg@exept.de>
parents:
12664
diff
changeset
|
1496 |
substringsSeparatedBy:separatorCharacter |
18921 | 1497 |
"return a collection consisting of all words contained in the receiver. |
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
1498 |
Words are separated by the given separator character. |
18921 | 1499 |
This has been added for Squeak/Pharo compatibility. |
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
1500 |
(sigh: it is called #'subStrings:' in V'Age, |
18921 | 1501 |
and #'asCollectionOfSubstringsSeparatedBy' in ST/X) " |
18391 | 1502 |
|
12665
b027fc5aec4e
added: #substringsSeparatedBy:
Claus Gittinger <cg@exept.de>
parents:
12664
diff
changeset
|
1503 |
^ self asCollectionOfSubstringsSeparatedBy:separatorCharacter |
b027fc5aec4e
added: #substringsSeparatedBy:
Claus Gittinger <cg@exept.de>
parents:
12664
diff
changeset
|
1504 |
|
b027fc5aec4e
added: #substringsSeparatedBy:
Claus Gittinger <cg@exept.de>
parents:
12664
diff
changeset
|
1505 |
" |
b027fc5aec4e
added: #substringsSeparatedBy:
Claus Gittinger <cg@exept.de>
parents:
12664
diff
changeset
|
1506 |
'foo bar, baz' substringsSeparatedBy:$, |
b027fc5aec4e
added: #substringsSeparatedBy:
Claus Gittinger <cg@exept.de>
parents:
12664
diff
changeset
|
1507 |
" |
b027fc5aec4e
added: #substringsSeparatedBy:
Claus Gittinger <cg@exept.de>
parents:
12664
diff
changeset
|
1508 |
! |
b027fc5aec4e
added: #substringsSeparatedBy:
Claus Gittinger <cg@exept.de>
parents:
12664
diff
changeset
|
1509 |
|
9190 | 1510 |
truncateTo:smallSize |
9192 | 1511 |
"return myself or a copy shortened to smallSize. 1/18/96 sw" |
1512 |
||
1513 |
self size <= smallSize ifTrue:[^ self]. |
|
1514 |
^ self copyFrom: 1 to: smallSize |
|
1515 |
||
1516 |
" |
|
1517 |
'hello world' truncateTo:5 |
|
1518 |
'hello' truncateTo:10 |
|
1519 |
||
1520 |
'hello world' copyTo:5 |
|
1521 |
'hello' copyTo:10 |
|
1522 |
" |
|
1523 |
! |
|
5795 | 1524 |
|
4890 | 1525 |
withBlanksTrimmed |
18464 | 1526 |
"Return a copy of the receiver from which leading and trailing whitespace have been trimmed. |
1527 |
Notice the bad naming - it is trimming separators, not just blanks. |
|
1528 |
Added for Squeak compatibility and an alias for withoutSeparators" |
|
4890 | 1529 |
|
14865 | 1530 |
^ self withoutSeparators "withoutSpaces" "/ cg: it seems that squeak cares for any whitespace |
4890 | 1531 |
|
1532 |
" |
|
8874 | 1533 |
' hello world ' withBlanksTrimmed |
4890 | 1534 |
" |
1535 |
! |
|
1536 |
||
1537 |
withNoLineLongerThan: aNumber |
|
1538 |
"Answer a string with the same content as receiver, but rewrapped so that no line has more characters than the given number" |
|
1539 |
||
9192 | 1540 |
| listOfLines currentLast currentStart resultString putativeLast putativeLine crPosition | |
1541 |
||
14451
c06e7885b675
changed: #withNoLineLongerThan:
Claus Gittinger <cg@exept.de>
parents:
14420
diff
changeset
|
1542 |
(aNumber isNumber not or:[ aNumber < 1] ) ifTrue: [self error: 'too narrow']. |
9192 | 1543 |
listOfLines _ OrderedCollection new. |
1544 |
currentLast _ 0. |
|
1545 |
[currentLast < self size] whileTrue: |
|
20012 | 1546 |
[currentStart _ currentLast + 1. |
1547 |
putativeLast _ (currentStart + aNumber - 1) min: self size. |
|
1548 |
putativeLine _ self copyFrom: currentStart to: putativeLast. |
|
1549 |
(crPosition _ putativeLine indexOf: Character cr) > 0 ifTrue: |
|
1550 |
[putativeLast _ currentStart + crPosition - 1. |
|
1551 |
putativeLine _ self copyFrom: currentStart to: putativeLast]. |
|
1552 |
currentLast _ putativeLast == self size |
|
1553 |
ifTrue: |
|
1554 |
[putativeLast] |
|
1555 |
ifFalse: |
|
1556 |
[currentStart + putativeLine lastSpacePosition - 1]. |
|
1557 |
currentLast <= currentStart ifTrue: |
|
1558 |
["line has NO spaces; baleout!!" |
|
1559 |
currentLast _ putativeLast]. |
|
1560 |
listOfLines add: (self copyFrom: currentStart to: currentLast) withBlanksTrimmed]. |
|
9192 | 1561 |
|
1562 |
listOfLines size > 0 ifFalse: [^ '']. |
|
1563 |
resultString _ listOfLines first. |
|
1564 |
2 to: listOfLines size do: |
|
20012 | 1565 |
[:i | resultString _ resultString, Character cr asString, (listOfLines at: i)]. |
9192 | 1566 |
^ resultString |
1567 |
||
1568 |
" |
|
1569 |
#(5 7 20) collect: |
|
20012 | 1570 |
[:i | 'Fred the bear went down to the brook to read his book in silence' withNoLineLongerThan: i] |
14451
c06e7885b675
changed: #withNoLineLongerThan:
Claus Gittinger <cg@exept.de>
parents:
14420
diff
changeset
|
1571 |
" |
10883 | 1572 |
! |
1573 |
||
12343
0a93eee37d65
added: #withSqueakLineEndings
Claus Gittinger <cg@exept.de>
parents:
12330
diff
changeset
|
1574 |
withSqueakLineEndings |
13882
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
1575 |
"assume the string is textual, and that CR, LF, and CRLF are all |
20258 | 1576 |
valid line endings. Replace each occurrence with a single CR" |
12343
0a93eee37d65
added: #withSqueakLineEndings
Claus Gittinger <cg@exept.de>
parents:
12330
diff
changeset
|
1577 |
|
0a93eee37d65
added: #withSqueakLineEndings
Claus Gittinger <cg@exept.de>
parents:
12330
diff
changeset
|
1578 |
^ self asStringCollection asStringWith:Character cr. |
0a93eee37d65
added: #withSqueakLineEndings
Claus Gittinger <cg@exept.de>
parents:
12330
diff
changeset
|
1579 |
! |
0a93eee37d65
added: #withSqueakLineEndings
Claus Gittinger <cg@exept.de>
parents:
12330
diff
changeset
|
1580 |
|
12188 | 1581 |
withoutLeading: char |
1582 |
"return a copy of myself without leading a char. |
|
1583 |
Returns an empty string, if the receiver consist only of a char." |
|
1584 |
||
1585 |
^ self withoutLeadingForWhich:[:ch | ch = char] |
|
1586 |
||
1587 |
" |
|
13882
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
1588 |
'****foo****' withoutLeading: $* |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
1589 |
'foo****' withoutLeading: $* |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
1590 |
'*' withoutLeading: $* |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
1591 |
'' withoutLeading: $* |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
1592 |
'****foo' withoutLeading: $* |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
1593 |
'*******' withoutLeading: $* |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
1594 |
'foo' withoutLeading: $* |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
1595 |
'f***o***o' withoutLeading: $* |
12188 | 1596 |
('**' , Character tab asString , '*foo***') withoutLeading: $* inspect |
1597 |
" |
|
1598 |
! |
|
1599 |
||
11565 | 1600 |
withoutTrailing:char |
1601 |
"return a copy of myself without trailing char. |
|
1602 |
Returns an empty string, if the receiver consist only of char." |
|
1603 |
||
12188 | 1604 |
^ self withoutTrailingForWhich:[:ch | ch = char] |
1605 |
||
1606 |
" |
|
13882
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
1607 |
' foo....' withoutTrailing:$. |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
1608 |
'foo....' withoutTrailing:$. |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
1609 |
' foo' withoutTrailing:$. |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
1610 |
'.......' withoutTrailing:$. |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
1611 |
'foo' withoutTrailing:$. |
11565 | 1612 |
" |
9190 | 1613 |
! ! |
1614 |
||
1615 |
!CharacterArray methodsFor:'Compatibility-V''Age'! |
|
1616 |
||
1617 |
addLineDelimiter |
|
1618 |
"replace all '\'-characters by line delimiter (cr) - characters. |
|
9192 | 1619 |
This has been added for VisualAge compatibility." |
1620 |
||
1621 |
^ self withCRs |
|
1622 |
! |
|
1623 |
||
1624 |
bindWith:aString |
|
1625 |
"return a copy of the receiver, where a '%1' escape is |
|
1626 |
replaced by aString. |
|
1627 |
This has been added for VisualAge compatibility." |
|
1628 |
||
1629 |
^ self expandPlaceholdersWith:(Array with:aString) |
|
1630 |
||
1631 |
" |
|
1632 |
'do you like %1 ?' bindWith:'smalltalk' |
|
19785 | 1633 |
'do you like %(foo) ?' bindWithArguments:(Dictionary new at:'foo' put:'smalltalk'; yourself) |
9192 | 1634 |
" |
1635 |
! |
|
608 | 1636 |
|
1637 |
bindWith:string1 with:string2 |
|
1638 |
"return a copy of the receiver, where a '%1' escape is |
|
1639 |
replaced by string1 and '%2' is replaced by string2. |
|
1640 |
This has been added for VisualAge compatibility." |
|
1641 |
||
1642 |
^ self expandPlaceholdersWith:(Array with:string1 with:string2) |
|
1643 |
||
1644 |
" |
|
1645 |
'do you prefer %1 or rather %2 ?' |
|
20012 | 1646 |
bindWith:'smalltalk' with:'c++' |
9192 | 1647 |
" |
1648 |
! |
|
9190 | 1649 |
|
1650 |
bindWith:str1 with:str2 with:str3 |
|
608 | 1651 |
"return a copy of the receiver, where a '%1', '%2' and '%3' escapes |
1652 |
are replaced by str1, str2 and str3 respectively. |
|
1653 |
This has been added for VisualAge compatibility." |
|
1654 |
||
1655 |
^ self expandPlaceholdersWith:(Array with:str1 with:str2 with:str3) |
|
1656 |
||
1657 |
" |
|
9192 | 1658 |
'do you prefer %1 or rather %2 (not talking about %3) ?' |
20012 | 1659 |
bindWith:'smalltalk' with:'c++' with:'c' |
9192 | 1660 |
" |
1661 |
! |
|
608 | 1662 |
|
1663 |
bindWith:str1 with:str2 with:str3 with:str4 |
|
1664 |
"return a copy of the receiver, where a '%1', '%2', '%3' and '%4' escapes |
|
1665 |
are replaced by str1, str2, str3 and str4 respectively. |
|
1666 |
This has been added for VisualAge compatibility." |
|
1667 |
||
1668 |
^ self expandPlaceholdersWith:(Array with:str1 with:str2 with:str3 with:str4) |
|
1669 |
||
1670 |
" |
|
9192 | 1671 |
'do you prefer %1 or rather %2 (not talking about %3 or even %4) ?' |
20012 | 1672 |
bindWith:'smalltalk' with:'c++' with:'c' with:'assembler' |
9192 | 1673 |
" |
1674 |
! |
|
608 | 1675 |
|
2344 | 1676 |
bindWith:str1 with:str2 with:str3 with:str4 with:str5 |
1677 |
"return a copy of the receiver, where a '%1' .. '%5' escapes |
|
1678 |
are replaced by str1 .. str5 respectively. |
|
1679 |
This has been added for VisualAge compatibility." |
|
1680 |
||
1681 |
^ self expandPlaceholdersWith:(Array with:str1 with:str2 with:str3 with:str4 with:str5) |
|
1682 |
||
1683 |
"Created: 31.1.1997 / 16:25:42 / cg" |
|
1684 |
! |
|
1685 |
||
5343 | 1686 |
bindWith:str1 with:str2 with:str3 with:str4 with:str5 with:str6 |
1687 |
"return a copy of the receiver, where a '%1' .. '%6' escapes |
|
11524 | 1688 |
are replaced by str1 .. str6 respectively. |
5343 | 1689 |
This has been added for VisualAge compatibility." |
1690 |
||
8874 | 1691 |
^ self expandPlaceholdersWith:(Array with:str1 with:str2 |
20012 | 1692 |
with:str3 with:str4 |
1693 |
with:str5 with:str6) |
|
11524 | 1694 |
! |
1695 |
||
1696 |
bindWith:str1 with:str2 with:str3 with:str4 with:str5 with:str6 with:str7 |
|
1697 |
"return a copy of the receiver, where a '%1' .. '%7' escapes |
|
1698 |
are replaced by str1 .. str7 respectively. |
|
1699 |
This has been added for VisualAge compatibility." |
|
1700 |
||
1701 |
^ self expandPlaceholdersWith:(Array with:str1 with:str2 |
|
20012 | 1702 |
with:str3 with:str4 |
1703 |
with:str5 with:str6 |
|
1704 |
with:str7) |
|
5343 | 1705 |
! |
1706 |
||
13996
a86faa4f17b8
added: #bindWith:with:with:with:with:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
13987
diff
changeset
|
1707 |
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
|
1708 |
"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
|
1709 |
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
|
1710 |
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
|
1711 |
|
a86faa4f17b8
added: #bindWith:with:with:with:with:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
13987
diff
changeset
|
1712 |
^ self expandPlaceholdersWith:(Array with:str1 with:str2 |
20012 | 1713 |
with:str3 with:str4 |
1714 |
with:str5 with:str6 |
|
1715 |
with:str7 with:str8) |
|
13996
a86faa4f17b8
added: #bindWith:with:with:with:with:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
13987
diff
changeset
|
1716 |
|
a86faa4f17b8
added: #bindWith:with:with:with:with:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
13987
diff
changeset
|
1717 |
"Created: / 06-02-2012 / 10:33:18 / cg" |
a86faa4f17b8
added: #bindWith:with:with:with:with:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
13987
diff
changeset
|
1718 |
! |
a86faa4f17b8
added: #bindWith:with:with:with:with:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
13987
diff
changeset
|
1719 |
|
14014
3aff2d7c41c8
added: #bindWith:with:with:with:with:with:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
13996
diff
changeset
|
1720 |
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
|
1721 |
"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
|
1722 |
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
|
1723 |
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
|
1724 |
|
3aff2d7c41c8
added: #bindWith:with:with:with:with:with:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
13996
diff
changeset
|
1725 |
^ self expandPlaceholdersWith:(Array with:str1 with:str2 |
20012 | 1726 |
with:str3 with:str4 |
1727 |
with:str5 with:str6 |
|
1728 |
with:str7 with:str8 |
|
1729 |
with:str9) |
|
14014
3aff2d7c41c8
added: #bindWith:with:with:with:with:with:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
13996
diff
changeset
|
1730 |
|
3aff2d7c41c8
added: #bindWith:with:with:with:with:with:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
13996
diff
changeset
|
1731 |
"Created: / 14-02-2012 / 17:42:31 / cg" |
3aff2d7c41c8
added: #bindWith:with:with:with:with:with:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
13996
diff
changeset
|
1732 |
! |
3aff2d7c41c8
added: #bindWith:with:with:with:with:with:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
13996
diff
changeset
|
1733 |
|
608 | 1734 |
bindWithArguments:anArrayOfStrings |
1735 |
"return a copy of the receiver, where a '%i' escape |
|
1736 |
is replaced by the coresponding string from the argument array. |
|
1737 |
'i' may be between 1 and 9 (i.e. a maximum of 9 placeholders is allowed). |
|
1738 |
This has been added for VisualAge compatibility." |
|
1739 |
||
9192 | 1740 |
^ self expandPlaceholdersWith:anArrayOfStrings |
1741 |
||
1742 |
" |
|
1743 |
'do you prefer %1 or rather %2 (not talking about %3) ?' |
|
20012 | 1744 |
bindWithArguments:#('smalltalk' 'c++' 'c') |
12196 | 1745 |
|
1746 |
'do you %(what) ?' |
|
20012 | 1747 |
bindWithArguments:(Dictionary new at:#'what' put:'understand'; yourself) |
9192 | 1748 |
" |
1749 |
! |
|
608 | 1750 |
|
1751 |
subStrings |
|
18921 | 1752 |
"return a collection consisting of all words contained in the receiver. |
608 | 1753 |
Words are separated by whitespace. |
17517 | 1754 |
This has been added for VisualAge compatibility. |
1755 |
(sigh: it is called #'subbtrings' in Squeak, and #'asCollectionOfWords' in ST/X) " |
|
608 | 1756 |
|
1757 |
^ self asCollectionOfWords |
|
1758 |
||
1759 |
" |
|
1760 |
'hello world, this is smalltalk' subStrings |
|
1761 |
" |
|
1762 |
! |
|
1763 |
||
16181 | 1764 |
subStrings:separatorCharacterOrString |
18921 | 1765 |
"return a collection consisting of all words contained in the receiver. |
608 | 1766 |
Words are separated by separatorCharacter. |
18921 | 1767 |
This is similar to split: (squeak) and asCollectionOfSubstringsSeparatedBy: (st/x) |
1768 |
and has been added for VisualAge compatibility." |
|
608 | 1769 |
|
16181 | 1770 |
separatorCharacterOrString isCharacter ifTrue:[ |
20012 | 1771 |
^ self asCollectionOfSubstringsSeparatedBy:separatorCharacterOrString |
16181 | 1772 |
]. |
1773 |
^ self asCollectionOfSubstringsSeparatedByAny:separatorCharacterOrString |
|
1774 |
||
1775 |
" |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
1776 |
'foo:bar:baz:smalltalk' subStrings:$: |
16181 | 1777 |
'foo:bar:baz:smalltalk' subStrings:':' |
1778 |
'foo.bar,baz-smalltalk' subStrings:'.,-' |
|
9192 | 1779 |
" |
1780 |
! |
|
1781 |
||
1782 |
trimSeparators |
|
18392 | 1783 |
"return a copy of the receiver without leading and trailing whiteSpace. |
18921 | 1784 |
Added for VisualAge compatibility (an alias for withoutSeparators)" |
9192 | 1785 |
|
1786 |
^ self withoutSeparators |
|
1787 |
! ! |
|
1788 |
||
1789 |
!CharacterArray methodsFor:'Compatibility-VW'! |
|
5964 | 1790 |
|
6139 | 1791 |
asLogicalFileSpecification |
1792 |
^ self asFilename |
|
1793 |
||
1794 |
"Created: / 30.10.2001 / 17:29:53 / cg" |
|
1795 |
! |
|
1796 |
||
5964 | 1797 |
expandMacros |
1798 |
"ST80 compatibility - expand '<..>' macros with |
|
1799 |
argument strings. Similar to #bindWith:. |
|
1800 |
Read the comment in #expandMacrosWithArguments: about |
|
1801 |
limited compatibility issues." |
|
1802 |
||
1803 |
^ self expandMacrosWithArguments:#() |
|
1804 |
||
1805 |
" |
|
1806 |
'hello<n>foo' expandMacros |
|
1807 |
" |
|
1808 |
||
9192 | 1809 |
"Modified: / 18.6.1998 / 16:03:02 / cg" |
1810 |
! |
|
1811 |
||
1812 |
expandMacrosWith:arg |
|
5964 | 1813 |
"ST80 compatibility - expand '<..>' macros with |
1814 |
argument strings. Similar to #bindWith:. |
|
1815 |
Read the comment in #expandMacrosWithArguments: about |
|
1816 |
limited compatibility issues." |
|
1817 |
||
9192 | 1818 |
^ self expandMacrosWithArguments:(Array with:arg) |
1819 |
||
1820 |
"Created: / 1.11.1997 / 13:01:28 / cg" |
|
1821 |
"Modified: / 1.11.1997 / 13:30:50 / cg" |
|
1822 |
! |
|
5964 | 1823 |
|
1824 |
expandMacrosWith:arg1 with:arg2 |
|
1825 |
"ST80 compatibility - expand '<..>' macros with |
|
1826 |
argument strings. Similar to #bindWith:. |
|
1827 |
Read the comment in #expandMacrosWithArguments: about |
|
1828 |
limited compatibility issues." |
|
1829 |
||
1830 |
^ self expandMacrosWithArguments:(Array with:arg1 with:arg2) |
|
1831 |
||
1832 |
"Modified: / 6.7.1998 / 21:58:14 / cg" |
|
1833 |
! |
|
1834 |
||
1835 |
expandMacrosWith:arg1 with:arg2 with:arg3 |
|
1836 |
"ST80 compatibility - expand '<..>' macros with |
|
1837 |
argument strings. Similar to #bindWith:. |
|
1838 |
Read the comment in #expandMacrosWithArguments: about |
|
1839 |
limited compatibility issues." |
|
1840 |
||
1841 |
^ self expandMacrosWithArguments:(Array with:arg1 with:arg2 with:arg3) |
|
1842 |
! |
|
1843 |
||
10353
65fb7aa2c6c2
added #expandMacrosWith:with:with:with:
Stefan Vogel <sv@exept.de>
parents:
10134
diff
changeset
|
1844 |
expandMacrosWith:arg1 with:arg2 with:arg3 with:arg4 |
65fb7aa2c6c2
added #expandMacrosWith:with:with:with:
Stefan Vogel <sv@exept.de>
parents:
10134
diff
changeset
|
1845 |
"ST80 compatibility - expand '<..>' macros with |
65fb7aa2c6c2
added #expandMacrosWith:with:with:with:
Stefan Vogel <sv@exept.de>
parents:
10134
diff
changeset
|
1846 |
argument strings. Similar to #bindWith:. |
65fb7aa2c6c2
added #expandMacrosWith:with:with:with:
Stefan Vogel <sv@exept.de>
parents:
10134
diff
changeset
|
1847 |
Read the comment in #expandMacrosWithArguments: about |
65fb7aa2c6c2
added #expandMacrosWith:with:with:with:
Stefan Vogel <sv@exept.de>
parents:
10134
diff
changeset
|
1848 |
limited compatibility issues." |
65fb7aa2c6c2
added #expandMacrosWith:with:with:with:
Stefan Vogel <sv@exept.de>
parents:
10134
diff
changeset
|
1849 |
|
65fb7aa2c6c2
added #expandMacrosWith:with:with:with:
Stefan Vogel <sv@exept.de>
parents:
10134
diff
changeset
|
1850 |
^ self expandMacrosWithArguments:(Array with:arg1 with:arg2 with:arg3 with:arg4) |
65fb7aa2c6c2
added #expandMacrosWith:with:with:with:
Stefan Vogel <sv@exept.de>
parents:
10134
diff
changeset
|
1851 |
! |
65fb7aa2c6c2
added #expandMacrosWith:with:with:with:
Stefan Vogel <sv@exept.de>
parents:
10134
diff
changeset
|
1852 |
|
9192 | 1853 |
expandMacrosWithArguments:argArray |
5964 | 1854 |
"ST80 compatibility - expand '<..>' macros with |
1855 |
argument strings. Similar to #bindWith:. |
|
1856 |
WARNING: possibly not all ST80 expansions are supported here." |
|
1857 |
||
1858 |
"/ supported expansions: |
|
1859 |
"/ |
|
1860 |
"/ <#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
|
1861 |
"/ <#s> # is arg Number; slice in the arg itself (must know asString) |
5964 | 1862 |
"/ <#?s1:s2> # is arg Number; slice in s1 if the arg is true, s2 otherwise |
7643 | 1863 |
"/ use first arg if # is not given (i.e. no number before s,p ...) |
5964 | 1864 |
"/ <n> replace by a newLine character |
1865 |
"/ <t> replace by a tab character |
|
6834
eba4b58b8692
fixed expandMacros (%-escape)
Claus Gittinger <cg@exept.de>
parents:
6810
diff
changeset
|
1866 |
"/ %X the X character itself |
5964 | 1867 |
|
8999
f3fda66ea51e
#expandMacrosWithArguments: - be mor friendly to xml strings.
Stefan Vogel <sv@exept.de>
parents:
8971
diff
changeset
|
1868 |
|in out c fmt nr arg s1 s2 peekc| |
5964 | 1869 |
|
1870 |
in := self readStream. |
|
10721
58e6950264cb
care for bitsPerCharacter when expanding macros
Claus Gittinger <cg@exept.de>
parents:
10703
diff
changeset
|
1871 |
out := CharacterWriteStream on:(self species uninitializedNew:self size). |
5964 | 1872 |
|
1873 |
[in atEnd] whileFalse:[ |
|
20012 | 1874 |
c := in next. |
1875 |
c == $% ifTrue:[ |
|
1876 |
c := in next. |
|
1877 |
out nextPut:c |
|
1878 |
] ifFalse:[c ~~ $< ifTrue:[ |
|
1879 |
out nextPut:c. |
|
1880 |
] ifFalse:[ |
|
1881 |
peekc := in peek. |
|
1882 |
[peekc == $<] whileTrue:[ |
|
1883 |
out nextPut:$<. |
|
1884 |
peekc := in nextPeek. |
|
1885 |
]. |
|
1886 |
peekc == $n ifTrue:[ |
|
1887 |
peekc := in nextPeek. |
|
1888 |
peekc == $> ifTrue:[ |
|
1889 |
in next. |
|
1890 |
out cr. |
|
1891 |
] ifFalse:[ |
|
1892 |
out nextPutAll:'<n'. |
|
1893 |
] |
|
1894 |
] ifFalse:[peekc == $t ifTrue:[ |
|
1895 |
peekc := in nextPeek. |
|
1896 |
peekc == $> ifTrue:[ |
|
1897 |
in next. |
|
1898 |
out tab. |
|
1899 |
] ifFalse:[ |
|
1900 |
out nextPutAll:'<t'. |
|
1901 |
] |
|
1902 |
] ifFalse:[ |
|
1903 |
peekc isDigit ifTrue:[ |
|
1904 |
"start an argument expansion ..." |
|
1905 |
nr := Integer readFrom:in onError:nil. |
|
1906 |
nr isNil ifTrue:[ |
|
1907 |
"this cannot happen (there is at least one digit)" |
|
1908 |
self error:'invalid format' mayProceed:true. |
|
1909 |
^ self |
|
1910 |
]. |
|
1911 |
fmt := in next. |
|
1912 |
(fmt ~~ $? and:[in peek ~~ $>]) ifTrue:[ |
|
1913 |
out nextPut:$<. |
|
1914 |
nr printOn:out. |
|
1915 |
out nextPut:fmt. |
|
1916 |
] ifFalse:[ |
|
1917 |
(nr between:1 and:argArray size) ifTrue:[ |
|
1918 |
arg := argArray at:nr. |
|
1919 |
] ifFalse:[ |
|
1920 |
arg := '' |
|
1921 |
]. |
|
1922 |
||
1923 |
fmt == $p ifTrue:[ |
|
1924 |
"expand with args printString" |
|
1925 |
arg printOn:out. |
|
1926 |
] ifFalse:[fmt == $s ifTrue:[ |
|
1927 |
"expand with arg itself" |
|
1928 |
arg isText ifTrue:[ |
|
1929 |
out contentsSpecies isText ifFalse:[ |
|
1930 |
out := (TextStream ? CharacterWriteStream on:Text new) nextPutAll:out contents; yourself. |
|
1931 |
]. |
|
1932 |
out nextPutAll:arg. |
|
1933 |
] ifFalse:[ |
|
1934 |
out nextPutAll:arg asString string. "see method comment: arg must know #asString" |
|
1935 |
] |
|
1936 |
] ifFalse:[fmt == $? ifTrue:[ |
|
1937 |
s1 := in upTo:$:. |
|
1938 |
s2 := in nextUpTo:$>. |
|
1939 |
arg == true ifTrue:[ |
|
1940 |
out nextPutAll:s1 |
|
1941 |
] ifFalse:[ |
|
1942 |
out nextPutAll:s2 |
|
1943 |
]. |
|
1944 |
] ifFalse:[ |
|
1945 |
"what does VW do here ?" |
|
1946 |
self error:'invalid format' mayProceed:true. |
|
1947 |
^ self |
|
1948 |
]]]. |
|
1949 |
c := in next. |
|
1950 |
c ~~ $> ifTrue:[ |
|
1951 |
"what does VW do here ?" |
|
1952 |
self error:'invalid format' mayProceed:true. |
|
1953 |
^ self |
|
1954 |
] |
|
1955 |
||
1956 |
]. |
|
1957 |
] ifFalse:[ |
|
1958 |
out nextPut:$<. |
|
1959 |
]. |
|
1960 |
]]. |
|
1961 |
]]. |
|
5964 | 1962 |
]. |
1963 |
^ out contents |
|
1964 |
||
1965 |
" |
|
8874 | 1966 |
'hello <1s> how are you' expandMacrosWith:(OperatingSystem getLoginName) |
1967 |
'one plus one is <1p>' expandMacrosWith:2 |
|
5964 | 1968 |
" |
1969 |
||
10721
58e6950264cb
care for bitsPerCharacter when expanding macros
Claus Gittinger <cg@exept.de>
parents:
10703
diff
changeset
|
1970 |
"Modified: / 18-09-2007 / 22:50:43 / cg" |
9192 | 1971 |
! |
1972 |
||
1973 |
isCharacters |
|
1974 |
"added for visual works compatibility" |
|
1975 |
^ true |
|
1976 |
! ! |
|
1977 |
||
16295 | 1978 |
!CharacterArray methodsFor:'JavaScript support'! |
1979 |
||
1980 |
unquote |
|
17000 | 1981 |
"removes double quotes from the receiver. |
19698 | 1982 |
This is the JavaScript standard unquote function." |
1983 |
||
1984 |
^ self unquote:$" |
|
16295 | 1985 |
|
1986 |
" |
|
17000 | 1987 |
'hello' quote unquote |
1988 |
||
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
1989 |
JavaScriptParser evaluate:'''hello''.quote.unquote' |
16295 | 1990 |
" |
19698 | 1991 |
! |
1992 |
||
1993 |
unquote:quoteCharacter |
|
1994 |
"removes quoteCharacter from either end of the receiver." |
|
1995 |
||
1996 |
|mySize| |
|
1997 |
||
1998 |
(mySize := self size) >= 2 ifTrue:[ |
|
20012 | 1999 |
((self first == quoteCharacter) and:[self last == quoteCharacter]) ifTrue:[ |
2000 |
^ self copyFrom:2 to:mySize-1 |
|
2001 |
]. |
|
19698 | 2002 |
]. |
2003 |
^ self |
|
2004 |
||
2005 |
" |
|
2006 |
'*hello*' unquote:$* |
|
2007 |
" |
|
16295 | 2008 |
! ! |
13510 | 2009 |
|
16175 | 2010 |
|
9192 | 2011 |
!CharacterArray methodsFor:'character searching'! |
2012 |
||
2013 |
includesMatchCharacters |
|
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
2014 |
"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
|
2015 |
for match operations; false if not. |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2016 |
Here, do not care for $\ escapes" |
608 | 2017 |
|
2018 |
^ self includesAny:'*#[' |
|
2519
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2019 |
|
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2020 |
" |
8874 | 2021 |
'*foo' includesMatchCharacters |
2022 |
'\*foo' includesMatchCharacters |
|
2023 |
'\*foo' includesUnescapedMatchCharacters |
|
2024 |
'*foo' includesMatchCharacters |
|
2519
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2025 |
'\\*foo' includesMatchCharacters |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2026 |
'foo*' includesMatchCharacters |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2027 |
'foo\*' includesMatchCharacters |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2028 |
'foo\' includesMatchCharacters |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2029 |
" |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2030 |
|
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2031 |
"Modified: 2.4.1997 / 18:12:34 / cg" |
9192 | 2032 |
! |
2033 |
||
2034 |
includesSeparator |
|
2035 |
"return true, if the receiver contains any whitespace characters" |
|
2036 |
||
2037 |
^ (self indexOfSeparator ~~ 0) |
|
2038 |
||
2039 |
" |
|
2040 |
'hello world' includesSeparator |
|
2041 |
'helloworld' includesSeparator |
|
2042 |
" |
|
2043 |
! |
|
2044 |
||
2045 |
includesUnescapedMatchCharacters |
|
8874 | 2046 |
"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
|
2047 |
for match operations; false if not. |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2048 |
Here, care for $\ escapes" |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2049 |
|
5342
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
2050 |
|idx sz specialChars escape| |
2519
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2051 |
|
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2052 |
idx := 1. |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2053 |
sz := self size. |
5342
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
2054 |
specialChars := '*#[\'. |
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
2055 |
(escape := self class matchEscapeCharacter) ~~ $\ ifTrue:[ |
20012 | 2056 |
specialChars := specialChars copy. |
2057 |
specialChars at:specialChars size put:escape |
|
5342
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
2058 |
]. |
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
2059 |
|
16245 | 2060 |
[ |
20012 | 2061 |
idx := self indexOfAny:specialChars startingAt:idx. |
2062 |
idx == 0 ifTrue:[^ false]. |
|
2063 |
(self at:idx) == escape ifFalse:[^ true]. |
|
2064 |
idx := idx + 2. |
|
2065 |
idx > sz ifTrue:[^ false]. |
|
16245 | 2066 |
] loop. |
8874 | 2067 |
|
2068 |
" |
|
2069 |
'*foo' includesUnescapedMatchCharacters |
|
2070 |
'\*foo' includesUnescapedMatchCharacters |
|
2071 |
'\\foo' includesUnescapedMatchCharacters |
|
2072 |
'\\\$foo' includesUnescapedMatchCharacters |
|
2519
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2073 |
'*foo' includesUnescapedMatchCharacters |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2074 |
'\\*foo' includesUnescapedMatchCharacters |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2075 |
'foo*' includesUnescapedMatchCharacters |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2076 |
'foo\*' includesUnescapedMatchCharacters |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2077 |
'foo\' includesUnescapedMatchCharacters |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2078 |
" |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2079 |
|
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2080 |
"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
|
2081 |
"Created: 2.4.1997 / 17:23:26 / cg" |
9192 | 2082 |
! |
2083 |
||
2084 |
indexOfControlCharacterStartingAt:startIndex |
|
3668
76271941f044
added #indexOfControlCharacterStartingAt:
Claus Gittinger <cg@exept.de>
parents:
3656
diff
changeset
|
2085 |
"return the index of the next control character; |
76271941f044
added #indexOfControlCharacterStartingAt:
Claus Gittinger <cg@exept.de>
parents:
3656
diff
changeset
|
2086 |
that is a character with asciiValue < 32. |
15605 | 2087 |
Start the search at startIndex, searching forward. |
3668
76271941f044
added #indexOfControlCharacterStartingAt:
Claus Gittinger <cg@exept.de>
parents:
3656
diff
changeset
|
2088 |
Return 0 if none is found." |
76271941f044
added #indexOfControlCharacterStartingAt:
Claus Gittinger <cg@exept.de>
parents:
3656
diff
changeset
|
2089 |
|
76271941f044
added #indexOfControlCharacterStartingAt:
Claus Gittinger <cg@exept.de>
parents:
3656
diff
changeset
|
2090 |
|start "{ Class: SmallInteger }" |
9192 | 2091 |
mySize "{ Class: SmallInteger }"| |
2092 |
||
2093 |
start := startIndex. |
|
2094 |
mySize := self size. |
|
2095 |
||
2096 |
start to:mySize do:[:index | |
|
20012 | 2097 |
(self at:index) isControlCharacter ifTrue:[^ index] |
9192 | 2098 |
]. |
2099 |
^ 0 |
|
2100 |
||
2101 |
" |
|
2102 |
'hello world' asTwoByteString indexOfControlCharacterStartingAt:1 |
|
2103 |
'hello world\foo' withCRsasTwoByteString indexOfControlCharacterStartingAt:1 |
|
2104 |
" |
|
2105 |
||
2106 |
"Modified: / 21.7.1998 / 17:25:07 / cg" |
|
2107 |
! |
|
3668
76271941f044
added #indexOfControlCharacterStartingAt:
Claus Gittinger <cg@exept.de>
parents:
3656
diff
changeset
|
2108 |
|
8541 | 2109 |
indexOfNonSeparator |
2110 |
"return the index of the first non-whitespace character. |
|
2111 |
return 0 if no non-separator was found" |
|
2112 |
||
2113 |
^ self indexOfNonSeparatorStartingAt:1. |
|
2114 |
||
2115 |
" |
|
8874 | 2116 |
' hello world' indexOfNonSeparator |
2117 |
' ' indexOfNonSeparator |
|
2118 |
'a ' indexOfNonSeparator |
|
2119 |
'abc' indexOfNonSeparator |
|
2120 |
' ' indexOfNonSeparator |
|
9192 | 2121 |
'' indexOfNonSeparator |
2122 |
" |
|
2123 |
! |
|
2124 |
||
2125 |
indexOfNonSeparatorStartingAt:startIndex |
|
4117 | 2126 |
"return the index of the next non-whitespace character, |
2127 |
starting the search at startIndex, searching forward; |
|
2128 |
return 0 if no non-separator was found" |
|
608 | 2129 |
|
2130 |
|start "{ Class: SmallInteger }" |
|
2131 |
mySize "{ Class: SmallInteger }"| |
|
2132 |
||
2133 |
start := startIndex. |
|
2134 |
mySize := self size. |
|
2135 |
||
2136 |
start to:mySize do:[:index | |
|
20012 | 2137 |
(self at:index) isSeparator ifFalse:[^ index] |
608 | 2138 |
]. |
2139 |
^ 0 |
|
2140 |
||
2141 |
" |
|
8874 | 2142 |
' hello world' indexOfNonSeparatorStartingAt:1 |
2143 |
' ' indexOfNonSeparatorStartingAt:1 |
|
2144 |
'a ' indexOfNonSeparatorStartingAt:2 |
|
4117 | 2145 |
" |
2146 |
||
608 | 2147 |
" |
2148 |
|s index1 index2| |
|
2149 |
s := ' foo bar baz'. |
|
2150 |
index1 := s indexOfNonSeparatorStartingAt:1. |
|
2151 |
index2 := s indexOfSeparatorStartingAt:index1. |
|
2152 |
s copyFrom:index1 to:index2 - 1 |
|
2153 |
" |
|
9192 | 2154 |
! |
2155 |
||
2156 |
indexOfSeparator |
|
2157 |
"return the index of the first whitespace character; |
|
2158 |
starting the search at the beginning, searching forward; |
|
2159 |
return 0 if no separator was found" |
|
2160 |
||
2161 |
^ self indexOfSeparatorStartingAt:1 |
|
2162 |
||
2163 |
" |
|
2164 |
'hello world' indexOfSeparator |
|
2165 |
'helloworld' indexOfSeparator |
|
2166 |
'hello ' indexOfSeparator |
|
2167 |
' hello' indexOfSeparator |
|
2168 |
" |
|
2169 |
! |
|
2170 |
||
15912 | 2171 |
indexOfSeparatorOrEndStartingAt:startIndex |
2172 |
"return the index of the next whitespace character, |
|
2173 |
starting the search at startIndex, searching forward; |
|
19737 | 2174 |
return the index of one beyond the end of the receiver if no separator was found. |
15912 | 2175 |
To extract the word, copy from startIndex to the returned index-1" |
2176 |
||
2177 |
|idx| |
|
2178 |
||
2179 |
idx := self indexOfSeparatorStartingAt:startIndex. |
|
2180 |
idx == 0 ifTrue:[^ self size + 1]. |
|
2181 |
^ idx. |
|
2182 |
||
2183 |
" |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2184 |
'hello world' indexOfSeparatorOrEndStartingAt:3 |
15912 | 2185 |
' hello world' indexOfSeparatorOrEndStartingAt:3 |
2186 |
'hello world ' indexOfSeparatorOrEndStartingAt:3 |
|
2187 |
'hello world ' indexOfSeparatorOrEndStartingAt:6 |
|
2188 |
'hello world ' indexOfSeparatorOrEndStartingAt:7 |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2189 |
'helloworld ' indexOfSeparatorOrEndStartingAt:7 |
15912 | 2190 |
'helloworld' indexOfSeparatorOrEndStartingAt:7 |
19737 | 2191 |
'helloworld' indexOfSeparatorStartingAt:7 |
15912 | 2192 |
" |
2193 |
! |
|
2194 |
||
9192 | 2195 |
indexOfSeparatorStartingAt:startIndex |
2196 |
"return the index of the next whitespace character, |
|
2197 |
starting the search at startIndex, searching forward; |
|
2198 |
return 0 if no separator was found" |
|
2199 |
||
2200 |
|start "{ Class: SmallInteger }" |
|
2201 |
mySize "{ Class: SmallInteger }"| |
|
2202 |
||
2203 |
start := startIndex. |
|
2204 |
mySize := self size. |
|
2205 |
||
2206 |
start to:mySize do:[:index | |
|
20012 | 2207 |
(self at:index) isSeparator ifTrue:[^ index] |
9192 | 2208 |
]. |
2209 |
^ 0 |
|
2210 |
||
2211 |
" |
|
2212 |
'hello world' indexOfSeparatorStartingAt:3 |
|
2213 |
' hello world' indexOfSeparatorStartingAt:3 |
|
8874 | 2214 |
'hello world ' indexOfSeparatorStartingAt:3 |
2215 |
'hello world ' indexOfSeparatorStartingAt:6 |
|
2216 |
'hello world ' indexOfSeparatorStartingAt:7 |
|
2217 |
'helloworld ' indexOfSeparatorStartingAt:7 |
|
2218 |
'helloworld' indexOfSeparatorStartingAt:7 |
|
4117 | 2219 |
" |
2519
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2220 |
! |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2221 |
|
4117 | 2222 |
lastIndexOfSeparator |
14150
e13160aa321a
comment/format in: #lastIndexOfSeparator
Claus Gittinger <cg@exept.de>
parents:
14141
diff
changeset
|
2223 |
"return the last index of a whitespace character (space or tab). |
4117 | 2224 |
(i.e. start the search at the end and search backwards); |
2225 |
Returns 0 if no separator is found." |
|
2226 |
||
2227 |
^ self lastIndexOfSeparatorStartingAt:(self size) |
|
2228 |
||
2229 |
" |
|
8874 | 2230 |
'hello world' lastIndexOfSeparator |
2231 |
'helloworld' lastIndexOfSeparator |
|
2232 |
'hel lo wor ld' lastIndexOfSeparator |
|
2233 |
'hel ' lastIndexOfSeparator 6 |
|
4117 | 2234 |
" |
14150
e13160aa321a
comment/format in: #lastIndexOfSeparator
Claus Gittinger <cg@exept.de>
parents:
14141
diff
changeset
|
2235 |
|
e13160aa321a
comment/format in: #lastIndexOfSeparator
Claus Gittinger <cg@exept.de>
parents:
14141
diff
changeset
|
2236 |
"Modified (comment): / 01-06-2012 / 13:10:30 / cg" |
4117 | 2237 |
! |
2238 |
||
2239 |
lastIndexOfSeparatorStartingAt:startIndex |
|
9192 | 2240 |
"return the index of the previous whitespace character, |
2241 |
starting the search at startIndex (and searching backwards); |
|
2242 |
returns 0 if no separator was found" |
|
2243 |
||
2244 |
|start "{ Class: SmallInteger }"| |
|
2245 |
||
2246 |
start := startIndex. |
|
2247 |
||
2248 |
start to:1 by:-1 do:[:index | |
|
20012 | 2249 |
(self at:index) isSeparator ifTrue:[^ index] |
9192 | 2250 |
]. |
2251 |
^ 0 |
|
2252 |
||
2253 |
" |
|
2254 |
'hello world' lastIndexOfSeparatorStartingAt:3 |
|
2255 |
'hello world' lastIndexOfSeparatorStartingAt:7 |
|
2256 |
'helloworld' lastIndexOfSeparatorStartingAt:7 |
|
8874 | 2257 |
' helloworld' lastIndexOfSeparatorStartingAt:7 |
4117 | 2258 |
" |
608 | 2259 |
! ! |
2260 |
||
2261 |
!CharacterArray methodsFor:'comparing'! |
|
2262 |
||
6742
292775fda600
avoid endless recursion when comparing int < string
Claus Gittinger <cg@exept.de>
parents:
6655
diff
changeset
|
2263 |
< aString |
608 | 2264 |
"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
|
2265 |
receiver is greater than the argument. Otherwise return false. |
8874 | 2266 |
This comparison is based on the elements ascii code - |
9192 | 2267 |
i.e. upper/lowercase & upper/lowercase & national characters are NOT treated specially." |
2268 |
||
2269 |
|mySize "{ Class: SmallInteger }" |
|
2270 |
otherSize "{ Class: SmallInteger }" |
|
2271 |
n "{ Class: SmallInteger }" |
|
2272 |
c1 c2| |
|
2273 |
||
2274 |
mySize := self size. |
|
2275 |
otherSize := aString string size. |
|
6742
292775fda600
avoid endless recursion when comparing int < string
Claus Gittinger <cg@exept.de>
parents:
6655
diff
changeset
|
2276 |
n := mySize min:otherSize. |
292775fda600
avoid endless recursion when comparing int < string
Claus Gittinger <cg@exept.de>
parents:
6655
diff
changeset
|
2277 |
|
292775fda600
avoid endless recursion when comparing int < string
Claus Gittinger <cg@exept.de>
parents:
6655
diff
changeset
|
2278 |
1 to:n do:[:index | |
20012 | 2279 |
c1 := self at:index. |
2280 |
c2 := aString at:index. |
|
2281 |
(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
|
2282 |
]. |
292775fda600
avoid endless recursion when comparing int < string
Claus Gittinger <cg@exept.de>
parents:
6655
diff
changeset
|
2283 |
^ mySize < otherSize |
608 | 2284 |
! |
2285 |
||
2286 |
= aString |
|
2287 |
"Compare the receiver with the argument and return true if the |
|
2288 |
receiver is equal to the argument. Otherwise return false. |
|
1252 | 2289 |
|
9191 | 2290 |
This compare does NOT ignore case differences, |
608 | 2291 |
therefore 'foo' = 'Foo' will return false. |
2292 |
Since this is incompatible to ST-80 (at least, V2.x) , this may change." |
|
2293 |
||
14137
1eb1b4a3f364
Inherit from UninterpretedBytes instead of ByteArray
Stefan Vogel <sv@exept.de>
parents:
14122
diff
changeset
|
2294 |
|mySize "{ Class: SmallInteger }"| |
608 | 2295 |
|
5963
23462e06dcff
compare with Texts fixed (should compare equal if strings are)
Claus Gittinger <cg@exept.de>
parents:
5946
diff
changeset
|
2296 |
(aString isString or:[aString species == self species]) ifFalse:[ |
20012 | 2297 |
^ false |
5963
23462e06dcff
compare with Texts fixed (should compare equal if strings are)
Claus Gittinger <cg@exept.de>
parents:
5946
diff
changeset
|
2298 |
]. |
608 | 2299 |
mySize := self size. |
14137
1eb1b4a3f364
Inherit from UninterpretedBytes instead of ByteArray
Stefan Vogel <sv@exept.de>
parents:
14122
diff
changeset
|
2300 |
mySize ~~ (aString size) ifTrue:[^ false]. |
608 | 2301 |
|
2302 |
1 to:mySize do:[:index | |
|
20012 | 2303 |
(self at:index) = (aString at:index) ifFalse:[^ false]. |
608 | 2304 |
]. |
2305 |
^ true |
|
2306 |
||
2307 |
" |
|
8874 | 2308 |
'foo' = 'Foo' |
2309 |
'foo' = 'bar' |
|
2310 |
'foo' = 'foo' |
|
2311 |
'foo' = 'foo' asText |
|
2312 |
'foo' asText = 'foo' |
|
2313 |
'foo' asText = 'foo' asText |
|
608 | 2314 |
" |
1252 | 2315 |
|
2316 |
"Modified: 22.4.1996 / 15:53:58 / cg" |
|
608 | 2317 |
! |
9192 | 2318 |
|
9191 | 2319 |
> aString |
608 | 2320 |
"Compare the receiver with the argument and return true if the |
2321 |
receiver is greater than the argument. Otherwise return false. |
|
8874 | 2322 |
This comparison is based on the elements ascii code - |
1252 | 2323 |
i.e. upper/lowercase & upper/lowercase & national characters are NOT treated specially." |
608 | 2324 |
|
2325 |
|mySize "{ Class: SmallInteger }" |
|
8874 | 2326 |
otherSize "{ Class: SmallInteger }" |
9192 | 2327 |
n "{ Class: SmallInteger }" |
2328 |
c1 c2| |
|
2329 |
||
2330 |
mySize := self size. |
|
5929 | 2331 |
otherSize := aString string size. |
2332 |
n := mySize min:otherSize. |
|
2333 |
||
2334 |
1 to:n do:[:index | |
|
20012 | 2335 |
c1 := self at:index. |
2336 |
c2 := aString at:index. |
|
2337 |
(c1 == c2 or:[c1 = c2]) ifFalse:[^ c1 > c2]. |
|
5929 | 2338 |
]. |
9192 | 2339 |
^ mySize > otherSize |
2340 |
||
2341 |
"Modified: 22.4.1996 / 15:55:00 / cg" |
|
2342 |
! |
|
2343 |
||
14660 | 2344 |
after:aString |
2345 |
"Compare the receiver with the argument and return true if the |
|
2346 |
receiver should come after the argument in a sorted list. |
|
2347 |
Otherwise return false. |
|
2348 |
NOTE: The comparison should be language specific, depending on the value of |
|
20012 | 2349 |
LC_COLLATE, which is initialized from the environment. |
2350 |
||
2351 |
Currently it is for Strings, but not for UnicodeStrings... |
|
14660 | 2352 |
|
2353 |
STUPID: |
|
20012 | 2354 |
#after has a completely different meaning in SeqColl ..." |
14660 | 2355 |
|
2356 |
^ (self compareCollatingWith:aString) > 0 |
|
2357 |
! |
|
2358 |
||
15814 | 2359 |
compareAsVersionNumberWith:aStringOrCollection |
2360 |
"Compare the receiver with the argument and return 1 if the receiver is |
|
2361 |
greater, 0 if equal and -1 if less than the argument in a sorted list. |
|
2362 |
Compare as version numbers in the form a.b.c... ." |
|
2363 |
||
20374 | 2364 |
|rev1 rev2 removeTrailingZerosBlock trailingZerosCount| |
15814 | 2365 |
|
2366 |
rev1 := self asCollectionOfSubstringsSeparatedBy:$.. |
|
2367 |
aStringOrCollection isString ifTrue:[ |
|
20374 | 2368 |
rev2 := aStringOrCollection asCollectionOfSubstringsSeparatedBy:$.. |
15814 | 2369 |
]. |
2370 |
rev1 := rev1 collect:[:each| each asInteger]. |
|
2371 |
rev2 := rev2 collect:[:each| each asInteger]. |
|
2372 |
||
20374 | 2373 |
removeTrailingZerosBlock := |
2374 |
[:numbers | |
|
2375 |
trailingZerosCount := 0. |
|
2376 |
numbers reversed doWithExit:[:each :exit | |
|
2377 |
each == 0 ifTrue:[ |
|
2378 |
trailingZerosCount := trailingZerosCount + 1. |
|
2379 |
] ifFalse:[ |
|
2380 |
exit value:nil. |
|
2381 |
]. |
|
2382 |
]. |
|
2383 |
||
2384 |
trailingZerosCount > 0 ifTrue:[ |
|
2385 |
numbers copyTo:numbers size - trailingZerosCount |
|
2386 |
] ifFalse:[ |
|
2387 |
numbers |
|
2388 |
] |
|
2389 |
]. |
|
2390 |
||
2391 |
rev1 := removeTrailingZerosBlock value:rev1. |
|
2392 |
rev2 := removeTrailingZerosBlock value:rev2. |
|
2393 |
||
15814 | 2394 |
^ rev1 compareWith:rev2 |
2395 |
||
2396 |
" |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2397 |
self assert:('1' compareAsVersionNumberWith:'2') < 0. |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2398 |
self assert:('2' compareAsVersionNumberWith:'1') > 0. |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2399 |
self assert:('1.1' compareAsVersionNumberWith:'2.1.2') < 0. |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2400 |
self assert:('2.1' compareAsVersionNumberWith:'1.2.3') > 0. |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2401 |
self assert:('1' compareAsVersionNumberWith:'1.1') < 0. |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2402 |
self assert:('1.1' compareAsVersionNumberWith:'1') > 0. |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2403 |
self assert:('1.1' compareAsVersionNumberWith:'1.2') < 0. |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2404 |
self assert:('1.10' compareAsVersionNumberWith:'1.2') > 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.5') < 0. |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2406 |
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
|
2407 |
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
|
2408 |
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
|
2409 |
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
|
2410 |
self assert:('1.2.3.4' compareAsVersionNumberWith:#(1 2 3 4)) = 0. |
15814 | 2411 |
" |
2412 |
! |
|
2413 |
||
9192 | 2414 |
compareCaselessWith:aString |
9190 | 2415 |
"Compare the receiver against the argument, ignoreing case. |
2416 |
Return 1 if the receiver is greater, 0 if equal and -1 if less than the argument. |
|
2417 |
||
8874 | 2418 |
This comparison is based on the elements ascii code - |
9190 | 2419 |
i.e. national characters are NOT treated specially. |
2420 |
'foo' compareWith: 'Foo' will return 0" |
|
608 | 2421 |
|
2422 |
|mySize "{ Class: SmallInteger }" |
|
8874 | 2423 |
otherSize "{ Class: SmallInteger }" |
9192 | 2424 |
n "{ Class: SmallInteger }" |
608 | 2425 |
c1 c2| |
2426 |
||
2427 |
mySize := self size. |
|
5739
4fd3be7ccc75
oops some relational ops did accept a non-string arg
Claus Gittinger <cg@exept.de>
parents:
5732
diff
changeset
|
2428 |
otherSize := aString string size. |
608 | 2429 |
n := mySize min:otherSize. |
2430 |
||
2431 |
1 to:n do:[:index | |
|
20012 | 2432 |
c1 := (self at:index) asLowercase. |
2433 |
c2 := (aString at:index) asLowercase. |
|
2434 |
c1 > c2 ifTrue:[^ 1]. |
|
2435 |
c1 < c2 ifTrue:[^ -1]. |
|
608 | 2436 |
]. |
2437 |
mySize > otherSize ifTrue:[^ 1]. |
|
2438 |
mySize < otherSize ifTrue:[^ -1]. |
|
2439 |
^ 0 |
|
1252 | 2440 |
|
2441 |
"Modified: 22.4.1996 / 15:56:07 / cg" |
|
608 | 2442 |
! |
2443 |
||
14660 | 2444 |
compareCollatingWith:aString |
2445 |
"Compare the receiver with the argument and return 1 if the receiver is |
|
2446 |
greater, 0 if equal and -1 if less than the argument in a sorted list. |
|
2447 |
The comparison is language specific, depending on the value of |
|
2448 |
LC_COLLATE, which is in the shell environment." |
|
2449 |
||
2450 |
"TODO not yet defined for unicode" |
|
2451 |
<resource: #todo> |
|
2452 |
||
2453 |
|s| |
|
2454 |
||
2455 |
(s := self string) ~~ self ifTrue:[ |
|
20012 | 2456 |
^ s compareCollatingWith:aString |
14660 | 2457 |
]. |
2458 |
^ self compareWith:aString |
|
2459 |
! |
|
2460 |
||
9192 | 2461 |
compareWith:aString |
9190 | 2462 |
"Compare the receiver with the argument and return 1 if the receiver is |
2463 |
greater, 0 if equal and -1 if less than the argument. |
|
11861 | 2464 |
This comparison is based on the elements' codepoints - |
9190 | 2465 |
i.e. upper/lowercase & national characters are NOT treated specially. |
2466 |
'foo' compareWith: 'Foo' will return 1. |
|
2467 |
while 'foo' sameAs:'Foo' will return true" |
|
2468 |
||
15814 | 2469 |
|s| |
2470 |
||
2471 |
s := self string. |
|
2472 |
s ~~ self ifTrue:[ |
|
20012 | 2473 |
^ s compareWith:aString string. |
9192 | 2474 |
]. |
15814 | 2475 |
^ super compareWith:aString string. |
9192 | 2476 |
! |
2477 |
||
11861 | 2478 |
endsWith:aStringOrCharacter |
16175 | 2479 |
"return true, if the receiver ends with something, aStringOrCharacter. |
2480 |
If aStringOrCharacter is empty, true is returned" |
|
11861 | 2481 |
|
2482 |
|s| |
|
2483 |
||
2484 |
(s := self string) ~~ self ifTrue:[ |
|
20012 | 2485 |
^ s endsWith:aStringOrCharacter |
11861 | 2486 |
]. |
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2487 |
(self size > 0 and:[aStringOrCharacter isCharacter]) ifTrue:[ |
20012 | 2488 |
^ self last = aStringOrCharacter |
11861 | 2489 |
]. |
2490 |
^ super endsWith:aStringOrCharacter |
|
2491 |
||
2492 |
" |
|
2493 |
'hello world' endsWith:'world' |
|
2494 |
'hello world' asText allBold endsWith:'world' |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2495 |
'hello world' endsWith:'' |
16175 | 2496 |
'hello world' asText allBold endsWith:'' |
11861 | 2497 |
" |
2498 |
||
2499 |
"Modified: 12.5.1996 / 15:49:18 / cg" |
|
2500 |
! |
|
2501 |
||
11838 | 2502 |
hammingDistanceTo:aString |
2503 |
"return the hamming distance (the number of characters which are different). |
|
12177 | 2504 |
In information theory, the Hamming distance between two strings of equal length |
2505 |
is the number of positions for which the corresponding symbols are different. |
|
2506 |
Put another way, it measures the minimum number of substitutions required to change |
|
11838 | 2507 |
one into the other, or the number of errors that transformed one string into the other." |
2508 |
||
2509 |
self assert:(aString size == self size). |
|
2510 |
^ 1 to:self size count:[:idx | (self at:idx) ~= (aString at:idx)] |
|
2511 |
||
2512 |
" |
|
2513 |
'roses' hammingDistanceTo:'toned' |
|
2514 |
" |
|
2515 |
! |
|
2516 |
||
9192 | 2517 |
hash |
2518 |
"return an integer useful as a hash-key" |
|
2519 |
||
16898 | 2520 |
"/ whenever changing, also care for String>>hash. |
13879 | 2521 |
"/ immediately after any change, execute (maybe in a debugger): |
2522 |
"/ Set allSubInstancesDo:[:s | s rehash] |
|
16898 | 2523 |
^ self hash_fnv1a |
2524 |
||
2525 |
" |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2526 |
'a' hash |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2527 |
'a' asUnicode16String hash |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2528 |
'a' asUnicode32String hash |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2529 |
'aa' hash |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2530 |
'aa' asUnicode16String hash |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2531 |
'aa' asUnicode32String hash |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2532 |
'ab' hash |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2533 |
'ab' asUnicode16String hash |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2534 |
'ab' asUnicode32String hash |
13879 | 2535 |
'ab' hash |
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2536 |
'ab' asArray hash |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2537 |
" |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2538 |
|
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2539 |
" |
20012 | 2540 |
|syms ms| |
2541 |
||
2542 |
syms := Symbol allInstances. |
|
2543 |
Transcript show:'syms: '; showCR:syms size. |
|
2544 |
Transcript show:'sdbm hashes: '; showCR:(syms collect:[:s| s hash]) asSet size. |
|
2545 |
Transcript show:'dragonBook hashes: '; showCR:(syms collect:[:s| s hash_dragonBook]) asSet size. |
|
2546 |
||
2547 |
ms := Time millisecondsToRun:[ |
|
2548 |
10 timesRepeat:[ |
|
2549 |
syms do:[:each| each hash]. |
|
2550 |
]. |
|
2551 |
]. |
|
2552 |
Transcript show:'sdbm hash: '; showCR:ms. |
|
2553 |
||
2554 |
ms := Time millisecondsToRun:[ |
|
2555 |
10 timesRepeat:[ |
|
2556 |
syms do:[:each| each hash_dragonBook]. |
|
2557 |
]. |
|
2558 |
]. |
|
2559 |
Transcript show:'dragonBook: '; showCR:ms. |
|
2560 |
||
2561 |
syms := syms collect:[:each| each asUnicode16String]. |
|
2562 |
ms := Time millisecondsToRun:[ |
|
2563 |
10 timesRepeat:[ |
|
2564 |
syms do:[:each| each hash]. |
|
2565 |
]. |
|
2566 |
]. |
|
2567 |
Transcript show:'unicode sdbm hash: '; showCR:ms. |
|
2568 |
||
2569 |
ms := Time millisecondsToRun:[ |
|
2570 |
10 timesRepeat:[ |
|
2571 |
syms do:[:each| each hash_dragonBook]. |
|
2572 |
]. |
|
2573 |
]. |
|
2574 |
Transcript show:'unicode dragonBook:'; showCR:ms. |
|
14641 | 2575 |
" |
2576 |
||
13879 | 2577 |
"Modified: / 26-12-2011 / 14:09:07 / cg" |
2578 |
! |
|
2579 |
||
2580 |
hash_dragonBook |
|
2581 |
"return an integer useful as a hash-key" |
|
2582 |
||
10698
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2583 |
|h g| |
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2584 |
|
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2585 |
"/ |
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2586 |
"/ this is the dragon-book algorithm |
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2587 |
"/ |
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2588 |
h := 0. |
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2589 |
self reverseDo:[:char | |
10703
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
2590 |
"/ 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
|
2591 |
"/ h := (h bitShift:4) + char asciiValue. |
20012 | 2592 |
h := (h bitShift:4). |
2593 |
h := h + char codePoint. |
|
2594 |
h := h bitAnd:16rFFFFFFFF. |
|
2595 |
g := (h bitAnd: 16rF0000000). |
|
2596 |
g ~~ 0 ifTrue:[ |
|
2597 |
h := h bitXor:(g bitShift:-24). |
|
2598 |
h := h bitXor:g. |
|
2599 |
]. |
|
10698
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2600 |
]. |
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2601 |
"/ |
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2602 |
"/ multiply by large prime to spread values |
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2603 |
"/ This speeds up Set and Dictionary by a factor of 10!! |
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2604 |
"/ |
14137
1eb1b4a3f364
Inherit from UninterpretedBytes instead of ByteArray
Stefan Vogel <sv@exept.de>
parents:
14122
diff
changeset
|
2605 |
h := h times:31415821. |
10698
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2606 |
^ h |
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2607 |
|
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2608 |
" |
10703
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
2609 |
'a' hash |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
2610 |
'a' asUnicode16String hash |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
2611 |
'aa' hash |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
2612 |
'aa' asUnicode16String hash |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
2613 |
'ab' hash |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
2614 |
'ab' asUnicode16String hash |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
2615 |
'ab' hash |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
2616 |
'ab' asArray hash |
10698
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2617 |
" |
13879 | 2618 |
|
2619 |
"Created: / 26-12-2011 / 13:46:06 / cg" |
|
2620 |
! |
|
2621 |
||
16887 | 2622 |
hash_fnv1a |
2623 |
"return an integer useful as a hash-key. |
|
2624 |
This method uses the fnv-1a algorithm |
|
19743 | 2625 |
(which is actually a very good one). |
2626 |
Attention: stops when a 0-codepoint is encountered |
|
20012 | 2627 |
(for compatibility with the hash used by the VM) |
19743 | 2628 |
Also: on 64bit CPUs, only small 4-byte hashvalues are returned, |
20012 | 2629 |
(so hash values are independent from the architecture)" |
19743 | 2630 |
|
2631 |
|h byte| |
|
16887 | 2632 |
|
2633 |
h := 2166136261. |
|
2634 |
self do:[:eachChar | |
|
20012 | 2635 |
byte := eachChar codePoint. |
2636 |
byte == 0 ifTrue:[ |
|
2637 |
"/ stop |
|
2638 |
^ (h bitXor: (h >> 30)) bitAnd: 16r3FFFFFFF. |
|
2639 |
]. |
|
2640 |
h := h bitXor:byte. |
|
2641 |
h := (h * 16777619) bitAnd:16rFFFFFFFF. |
|
16887 | 2642 |
]. |
16898 | 2643 |
"/ make sure, it fits into a smallInt |
2644 |
h := (h bitXor: (h >> 30)) bitAnd: 16r3FFFFFFF. |
|
16887 | 2645 |
^ h |
2646 |
||
2647 |
" |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2648 |
'abc' hash_fnv1a |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2649 |
'abc' asUnicode16String hash_fnv1a |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2650 |
'abc' asUnicode32String hash_fnv1a |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2651 |
|
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2652 |
'foofooHelloWorld' hash_fnv1a |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2653 |
'foofooHelloWorld' asUnicode16String hash_fnv1a |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2654 |
'foofooHelloWorld' asUnicode32String hash_fnv1a |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2655 |
|
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2656 |
'blablaHelloWorld' hash_fnv1a |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2657 |
'blablaHelloWorld' asUnicode16String hash_fnv1a |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2658 |
'blablaHelloWorld' asUnicode32String hash_fnv1a |
16887 | 2659 |
" |
2660 |
! |
|
2661 |
||
2662 |
hash_java |
|
2663 |
"return an integer useful as a hash-key. |
|
2664 |
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
|
2665 |
the java virtual machine |
16887 | 2666 |
(which is actually not a very good one)." |
2667 |
||
2668 |
|h| |
|
2669 |
||
2670 |
h := 0. |
|
2671 |
self do:[:eachChar | |
|
20012 | 2672 |
h := (h * 31) + (eachChar codePoint). |
2673 |
h := h bitAnd:16rFFFFFFFF. |
|
16887 | 2674 |
]. |
2675 |
^ h |
|
2676 |
||
2677 |
" |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2678 |
'abc' hash_java |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2679 |
'foofooHelloWorld' hash_java |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2680 |
'blablaHelloWorld' hash_java |
16887 | 2681 |
" |
2682 |
! |
|
2683 |
||
16898 | 2684 |
hash_sdbm |
2685 |
"return an integer useful as a hash-key. |
|
2686 |
This method implements the sdbm algorithm." |
|
2687 |
||
2688 |
|h| |
|
2689 |
||
2690 |
"/ |
|
2691 |
"/ this is the sdbm algorithm |
|
2692 |
"/ |
|
2693 |
h := 0. |
|
2694 |
self do:[:char | |
|
20012 | 2695 |
h := (65599 times:h) plus:char codePoint. |
16898 | 2696 |
]. |
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2697 |
^ h |
16898 | 2698 |
|
2699 |
" |
|
2700 |
'a' hash |
|
2701 |
'a' asUnicode16String hash |
|
2702 |
'aa' hash |
|
2703 |
'aa' asUnicode16String hash |
|
2704 |
'ab' hash |
|
2705 |
'ab' asUnicode16String hash |
|
2706 |
'ab' hash |
|
2707 |
'ab' asArray hash |
|
2708 |
" |
|
2709 |
||
2710 |
" |
|
20012 | 2711 |
|syms ms| |
2712 |
||
2713 |
syms := Symbol allInstances. |
|
2714 |
Transcript show:'syms: '; showCR:syms size. |
|
2715 |
Transcript show:'sdbm hashes: '; showCR:(syms collect:[:s| s hash]) asSet size. |
|
2716 |
Transcript show:'dragonBook hashes: '; showCR:(syms collect:[:s| s hash_dragonBook]) asSet size. |
|
2717 |
||
2718 |
ms := Time millisecondsToRun:[ |
|
2719 |
10 timesRepeat:[ |
|
2720 |
syms do:[:each| each hash]. |
|
2721 |
]. |
|
2722 |
]. |
|
2723 |
Transcript show:'sdbm hash: '; showCR:ms. |
|
2724 |
||
2725 |
ms := Time millisecondsToRun:[ |
|
2726 |
10 timesRepeat:[ |
|
2727 |
syms do:[:each| each hash_dragonBook]. |
|
2728 |
]. |
|
2729 |
]. |
|
2730 |
Transcript show:'dragonBook: '; showCR:ms. |
|
2731 |
||
2732 |
syms := syms collect:[:each| each asUnicode16String]. |
|
2733 |
ms := Time millisecondsToRun:[ |
|
2734 |
10 timesRepeat:[ |
|
2735 |
syms do:[:each| each hash]. |
|
2736 |
]. |
|
2737 |
]. |
|
2738 |
Transcript show:'unicode sdbm hash: '; showCR:ms. |
|
2739 |
||
2740 |
ms := Time millisecondsToRun:[ |
|
2741 |
10 timesRepeat:[ |
|
2742 |
syms do:[:each| each hash_dragonBook]. |
|
2743 |
]. |
|
2744 |
]. |
|
2745 |
Transcript show:'unicode dragonBook:'; showCR:ms. |
|
16898 | 2746 |
" |
2747 |
||
2748 |
"Modified: / 26-12-2011 / 14:09:07 / cg" |
|
2749 |
! |
|
2750 |
||
11861 | 2751 |
levenshteinTo:aString |
2752 |
"return the levenshtein distance to the argument, aString; |
|
2753 |
this value corresponds to the number of replacements that have to be |
|
2754 |
made to get aString from the receiver. |
|
2755 |
See IEEE transactions on Computers 1976 Pg 172 ff." |
|
2756 |
||
2757 |
" |
|
2758 |
in the following, we assume that ommiting a character |
|
2759 |
is less of an error than inserting an extra character. |
|
2760 |
Therefore the different insertion (i) and deletion (d) values. |
|
20012 | 2761 |
s: substitution weight (4) |
2762 |
k: keyboard weight (k) (typing a nearby key) - or nil (then use s) |
|
2763 |
c: case weight (4) - or nil (then use s) |
|
2764 |
e: exchange weight (8) - or nil (then use s*2) |
|
2765 |
i: insertion of extra character weight (2) |
|
2766 |
d: delete of a character weight (6) |
|
11861 | 2767 |
" |
2768 |
||
2769 |
^ StringUtilities |
|
20012 | 2770 |
levenshteinDistanceFrom:self |
2771 |
to:aString |
|
2772 |
s:4 k:4 c:4 e:nil i:2 d:6 |
|
12177 | 2773 |
|
2774 |
" |
|
2775 |
'computer' levenshteinTo:'computer' |
|
11861 | 2776 |
'cOmputer' levenshteinTo:'computer' |
12177 | 2777 |
'cOmpuTer' levenshteinTo:'computer' |
11861 | 2778 |
'cimputer' levenshteinTo:'computer' |
2779 |
'cumputer' levenshteinTo:'computer' |
|
2780 |
||
2781 |
'cmputer' levenshteinTo:'computer' |
|
2782 |
'coomputer' levenshteinTo:'computer' |
|
2783 |
||
2784 |
'ocmprt' levenshteinTo:'computer' |
|
2785 |
'computer' levenshteinTo:'computer' |
|
2786 |
'ocmputer' levenshteinTo:'computer' |
|
2787 |
'cmputer' levenshteinTo:'computer' |
|
2788 |
'computer' levenshteinTo:'cmputer' |
|
2789 |
'Computer' levenshteinTo:'computer' |
|
2790 |
||
12177 | 2791 |
'compiter' levenshteinTo:'computer' |
2792 |
'compoter' levenshteinTo:'computer' |
|
2793 |
||
2794 |
'comptuer' levenshteinTo:'computer' |
|
11861 | 2795 |
" |
2796 |
! |
|
2797 |
||
2798 |
levenshteinTo:aString s:substWeight k:kbdTypoWeight c:caseWeight i:insrtWeight d:deleteWeight |
|
2799 |
"parametrized levenshtein. |
|
2800 |
return the levenshtein distance to the argument, aString; |
|
2801 |
this value corrensponds to the number of replacements that have to be |
|
2802 |
made to get aString from the receiver. |
|
2803 |
The arguments are the costs for |
|
20012 | 2804 |
s:substitution, |
2805 |
k:keyboard type (substitution), |
|
2806 |
c:case-change, |
|
2807 |
i:insertion |
|
2808 |
d:deletion |
|
11861 | 2809 |
of a character. |
2810 |
See IEEE transactions on Computers 1976 Pg 172 ff" |
|
2811 |
||
2812 |
^ StringUtilities |
|
20012 | 2813 |
levenshteinDistanceFrom:self |
2814 |
to:aString |
|
2815 |
s:substWeight k:kbdTypoWeight c:caseWeight e:nil i:insrtWeight d:deleteWeight |
|
11861 | 2816 |
! |
2817 |
||
9190 | 2818 |
sameAs:aString |
2819 |
"Compare the receiver with the argument like =, but ignore case differences. |
|
2820 |
Return true or false." |
|
2821 |
||
2822 |
|mySize "{ Class: SmallInteger }" |
|
2823 |
otherSize c1 c2| |
|
2824 |
||
9192 | 2825 |
self == aString ifTrue:[^ true]. |
2826 |
||
2827 |
mySize := self size. |
|
2828 |
otherSize := aString string size. |
|
18884 | 2829 |
mySize ~~ otherSize ifTrue:[^ false]. |
9192 | 2830 |
|
2831 |
1 to:mySize do:[:index | |
|
20012 | 2832 |
c1 := self at:index. |
2833 |
c2 := aString at:index. |
|
2834 |
c1 ~~ c2 ifTrue:[ |
|
2835 |
(c1 sameAs:c2) ifFalse:[^ false]. |
|
2836 |
] |
|
9192 | 2837 |
]. |
2838 |
^ true |
|
2839 |
||
608 | 2840 |
" |
8874 | 2841 |
'foo' sameAs: 'Foo' |
2842 |
'foo' sameAs: 'bar' |
|
2843 |
'foo' sameAs: 'foo' |
|
9192 | 2844 |
" |
2845 |
||
2846 |
"Modified: 22.4.1996 / 15:56:17 / cg" |
|
2847 |
! |
|
608 | 2848 |
|
15734 | 2849 |
sameAs:aString caseSensitive:caseSensitive |
2850 |
"Compare the receiver with the argument. |
|
2851 |
If caseSensitive is false, this is the same as #sameAs:, |
|
2852 |
if false, this is the same as #=." |
|
2853 |
||
2854 |
caseSensitive ifFalse:[ |
|
20012 | 2855 |
^ self sameAs:aString |
15734 | 2856 |
]. |
2857 |
^ self = aString |
|
2858 |
||
2859 |
" |
|
2860 |
'foo' sameAs:'Foo' caseSensitive:false |
|
2861 |
'foo' sameAs:'foo' caseSensitive:true |
|
2862 |
" |
|
2863 |
! |
|
2864 |
||
5242 | 2865 |
sameAs:aString ignoreCase:ignoreCase |
15734 | 2866 |
<resource: #obsolete> |
5242 | 2867 |
"Compare the receiver with the argument. |
2868 |
If ignoreCase is true, this is the same as #sameAs:, |
|
2869 |
if false, this is the same as #=." |
|
2870 |
||
2871 |
ignoreCase ifTrue:[ |
|
20012 | 2872 |
^ self sameAs:aString |
5242 | 2873 |
]. |
2874 |
^ self = aString |
|
2875 |
||
2876 |
" |
|
8874 | 2877 |
'foo' sameAs:'Foo' ignoreCase:false |
2878 |
'foo' sameAs:'foo' ignoreCase:true |
|
5242 | 2879 |
" |
2880 |
||
2881 |
! |
|
2882 |
||
608 | 2883 |
sameCharacters:aString |
9192 | 2884 |
"count & return the number of characters which are the same |
2885 |
(ignoring case and emphasis) in the receiver and the argument, aString." |
|
2886 |
||
2887 |
|n "{ Class: SmallInteger }" |
|
2888 |
c1 c2 cnt| |
|
2889 |
||
2890 |
n := self size. |
|
2891 |
n := n min:(aString string size). |
|
2892 |
||
2893 |
cnt := 0. |
|
2894 |
1 to:n do:[:index | |
|
20012 | 2895 |
c1 := self at:index. |
2896 |
c2 := aString at:index. |
|
2897 |
((c1 == c2) |
|
2898 |
or:[c1 asLowercase = c2 asLowercase]) ifTrue:[ |
|
2899 |
cnt := cnt + 1 |
|
2900 |
] |
|
9192 | 2901 |
]. |
2902 |
^ cnt |
|
2903 |
||
2904 |
" |
|
2905 |
'foobarbaz' sameCharacters: 'foo' |
|
8874 | 2906 |
'foobarbaz' sameCharacters: 'Foo' |
2907 |
'foobarbaz' sameCharacters: 'baz' |
|
608 | 2908 |
" |
5510
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
2909 |
! |
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
2910 |
|
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
2911 |
sameEmphasisAs:aStringOrText |
17215 | 2912 |
"compare the receiver's and the argument's emphasis" |
5510
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
2913 |
|
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
2914 |
^ self emphasis = aStringOrText emphasis |
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
2915 |
|
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
2916 |
" |
8874 | 2917 |
'hello' asText sameEmphasisAs: 'hello' |
2918 |
'hello' asText sameEmphasisAs: 'hello' asText |
|
2919 |
'hello' asText allBold sameEmphasisAs: 'hello' |
|
2920 |
'hello' asText allBold sameEmphasisAs: 'fooba' asText allBold |
|
9192 | 2921 |
'hello' asText allBold sameEmphasisAs: 'fooba' asText allItalic |
2922 |
" |
|
2923 |
! |
|
2924 |
||
9191 | 2925 |
sameStringAndEmphasisAs:aStringOrText |
5739
4fd3be7ccc75
oops some relational ops did accept a non-string arg
Claus Gittinger <cg@exept.de>
parents:
5732
diff
changeset
|
2926 |
"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
|
2927 |
|
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
2928 |
aStringOrText isString ifFalse:[^ false]. |
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
2929 |
(self string = aStringOrText string) ifFalse:[^ false]. |
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
2930 |
self hasChangeOfEmphasis = aStringOrText hasChangeOfEmphasis ifFalse:[^ false]. |
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
2931 |
^ self emphasis = aStringOrText emphasis |
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
2932 |
|
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
2933 |
" |
8874 | 2934 |
'hello' asText sameEmphasisAs: 'hello' |
2935 |
'hello' asText sameEmphasisAs: 'hello' asText |
|
2936 |
'hello' asText allBold sameEmphasisAs: 'hello' |
|
2937 |
'hello' asText allBold sameEmphasisAs: 'fooba' asText allBold |
|
2938 |
'hello' asText allBold sameEmphasisAs: 'fooba' asText allItalic |
|
2939 |
||
2940 |
'hello' sameEmphasisAs: 'hello' asText |
|
2941 |
'hello' sameEmphasisAs: 'hello' asText allBold |
|
2942 |
'hello' sameEmphasisAs: 'fooba' |
|
2943 |
'hello' sameEmphasisAs: 'fooba' asText |
|
2944 |
'hello' sameEmphasisAs: 'fooba' asText allBold |
|
2945 |
'hello' sameEmphasisAs: 'fooba' asText allItalic |
|
2946 |
||
2947 |
'hello' asText sameStringAndEmphasisAs: 'hello' |
|
2948 |
'hello' asText sameStringAndEmphasisAs: 'hello' asText |
|
2949 |
'hello' asText allBold sameStringAndEmphasisAs: 'hello' |
|
2950 |
'hello' asText allBold sameStringAndEmphasisAs: 'fooba' asText allBold |
|
2951 |
'hello' asText allBold sameStringAndEmphasisAs: 'fooba' asText allItalic |
|
2952 |
||
2953 |
'hello' sameStringAndEmphasisAs: 'hello' asText |
|
2954 |
'hello' sameStringAndEmphasisAs: 'hello' asText allBold |
|
2955 |
'hello' sameStringAndEmphasisAs: 'fooba' |
|
2956 |
'hello' sameStringAndEmphasisAs: 'fooba' asText |
|
2957 |
'hello' sameStringAndEmphasisAs: 'fooba' asText allBold |
|
2958 |
'hello' sameStringAndEmphasisAs: 'fooba' asText allItalic |
|
5510
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
2959 |
" |
11861 | 2960 |
! |
2961 |
||
2962 |
spellAgainst: aString |
|
2963 |
"return an integer between 0 and 100 indicating how similar |
|
2964 |
the argument is to the receiver. No case conversion is done. |
|
2965 |
This algorithm is much simpler (but also less exact) than the |
|
2966 |
levenshtein distance. Experiment which is better for your |
|
2967 |
application." |
|
2968 |
||
2969 |
| i1 "{ Class: SmallInteger }" |
|
2970 |
i2 "{ Class: SmallInteger }" |
|
2971 |
next1 "{ Class: SmallInteger }" |
|
2972 |
next2 "{ Class: SmallInteger }" |
|
2973 |
size1 "{ Class: SmallInteger }" |
|
2974 |
size2 "{ Class: SmallInteger }" |
|
2975 |
score "{ Class: SmallInteger }" |
|
2976 |
maxLen "{ Class: SmallInteger }" | |
|
2977 |
||
2978 |
size1 := self size. |
|
2979 |
size2 := aString size. |
|
2980 |
maxLen := size1 max:size2. |
|
2981 |
score := 0. |
|
2982 |
i1 := i2 := 1. |
|
2983 |
[i1 <= size1 and: [i2 <= size2]] whileTrue:[ |
|
20012 | 2984 |
next1 := i1 + 1. |
2985 |
next2 := i2 + 1. |
|
2986 |
(self at:i1) == (aString at:i2) ifTrue: [ |
|
2987 |
score := score+1. |
|
2988 |
i1 := next1. |
|
2989 |
i2 := next2 |
|
2990 |
] ifFalse: [ |
|
2991 |
(i2 < size2 and: [(self at:i1) == (aString at:next2)]) ifTrue: [ |
|
2992 |
i2 := next2 |
|
2993 |
] ifFalse: [ |
|
2994 |
(i1 < size1 and: [(self at:next1) == (aString at:i2)]) ifTrue: [ |
|
2995 |
i1 := next1 |
|
2996 |
] ifFalse: [ |
|
2997 |
i1 := next1. |
|
2998 |
i2 := next2 |
|
2999 |
] |
|
3000 |
] |
|
3001 |
] |
|
11861 | 3002 |
]. |
3003 |
||
3004 |
score == maxLen ifTrue: [^ 100]. |
|
3005 |
^ 100 * score // maxLen |
|
3006 |
||
3007 |
" |
|
3008 |
'Smalltalk' spellAgainst: 'Smalltlak' |
|
3009 |
'Smalltalk' spellAgainst: 'smalltlak' |
|
3010 |
'Smalltalk' spellAgainst: 'smalltalk' |
|
3011 |
'Smalltalk' spellAgainst: 'smalltlk' |
|
3012 |
'Smalltalk' spellAgainst: 'Smalltolk' |
|
3013 |
" |
|
3014 |
! |
|
3015 |
||
16553 | 3016 |
startsWith:aStringOrCharacter |
3017 |
"return true, if the receiver starts with something, aStringOrCharacter. |
|
17371 | 3018 |
If the argument is empty, true is returned. |
3019 |
Notice, that this is similar to, but slightly different from VW's and Squeak's beginsWith:, |
|
3020 |
which are both inconsistent w.r.t. an empty argument." |
|
11861 | 3021 |
|
3022 |
|s| |
|
3023 |
||
16553 | 3024 |
aStringOrCharacter isCharacter ifTrue:[ |
20012 | 3025 |
^ (self size > 0) and:[ (self at:1) == aStringOrCharacter ] |
16553 | 3026 |
]. |
11861 | 3027 |
(s := self string) ~~ self ifTrue:[ |
20012 | 3028 |
^ s startsWith:aStringOrCharacter |
11861 | 3029 |
]. |
16553 | 3030 |
^ super startsWith:aStringOrCharacter |
11861 | 3031 |
|
3032 |
" |
|
17364 | 3033 |
'abcde' startsWith:#($a $b $c) |
3034 |
'abcde' startsWith:'abc' |
|
3035 |
'abcd' startsWith:'abcde' |
|
3036 |
'abcde' startsWith:'abd' |
|
11861 | 3037 |
'hello world' startsWith:'hello' |
3038 |
'hello world' asText allBold startsWith:'hello' |
|
3039 |
'hello world' asText allBold startsWith:'' |
|
3040 |
" |
|
3041 |
||
3042 |
"Created: 12.5.1996 / 15:46:40 / cg" |
|
3043 |
"Modified: 12.5.1996 / 15:49:24 / cg" |
|
9192 | 3044 |
! ! |
3045 |
||
3046 |
!CharacterArray methodsFor:'converting'! |
|
3047 |
||
11881 | 3048 |
asAsciiZ |
3049 |
"if the receiver does not end with a 0-valued character, return a copy of it, |
|
3050 |
with an additional 0-character. Otherwise return the receiver. This is sometimes |
|
3051 |
needed when a string has to be passed to C, which needs 0-terminated strings. |
|
3052 |
Notice, that all singleByte strings are already 0-terminated in ST/X, whereas wide |
|
3053 |
strings are not." |
|
3054 |
||
12658 | 3055 |
(self notEmpty and:[self last codePoint == 0]) ifTrue:[^ self ]. |
11881 | 3056 |
^ self copyWith:(Character value:0). |
3057 |
||
3058 |
" |
|
3059 |
'abc' asAsciiZ |
|
3060 |
'abc' asWideString asAsciiZ |
|
3061 |
" |
|
3062 |
! |
|
3063 |
||
13524 | 3064 |
asByteArray |
15936 | 3065 |
"depending on the size of the characters in the receiver, |
3066 |
return a byteArray containing single-, double- or even 4-bytewise values. |
|
3067 |
The size of the returned byteArray will be the strings size multiplied by the |
|
3068 |
size required for the largest character. |
|
19233 | 3069 |
Attention: The bytes are in native byte order. |
15936 | 3070 |
Caveat: better use utf8Encoded, to get reproducable results" |
3071 |
||
14555 | 3072 |
|bytes sz bytesPerCharacter idx str| |
3073 |
||
3074 |
str := self string. |
|
3075 |
str ~~ self ifTrue:[ |
|
20012 | 3076 |
"/ for text and other wrappers |
3077 |
^ str asByteArray |
|
13611
ba8c7372218a
variable renamed in: #inspector2TabBytes
Claus Gittinger <cg@exept.de>
parents:
13524
diff
changeset
|
3078 |
]. |
ba8c7372218a
variable renamed in: #inspector2TabBytes
Claus Gittinger <cg@exept.de>
parents:
13524
diff
changeset
|
3079 |
|
ba8c7372218a
variable renamed in: #inspector2TabBytes
Claus Gittinger <cg@exept.de>
parents:
13524
diff
changeset
|
3080 |
"/ for real strings, a fallback |
13524 | 3081 |
sz := self size. |
19757 | 3082 |
bytesPerCharacter := self bytesPerCharacter. |
13524 | 3083 |
bytes := ByteArray new:(sz * bytesPerCharacter). |
3084 |
idx := 1. |
|
3085 |
self do:[:char | |
|
20012 | 3086 |
|code| |
3087 |
||
3088 |
code := char codePoint. |
|
3089 |
bytesPerCharacter == 2 ifTrue:[ |
|
3090 |
bytes unsignedInt16At:idx put:code |
|
3091 |
] ifFalse:[ |
|
3092 |
bytesPerCharacter == 4 ifTrue:[ |
|
3093 |
bytes unsignedInt32At:idx put:code |
|
3094 |
] ifFalse:[ |
|
3095 |
bytes at:idx put:code |
|
3096 |
]. |
|
3097 |
]. |
|
3098 |
idx := idx + bytesPerCharacter. |
|
13524 | 3099 |
]. |
3100 |
^ bytes |
|
3101 |
||
3102 |
"Created: / 27-07-2011 / 00:56:17 / cg" |
|
3103 |
! |
|
3104 |
||
19233 | 3105 |
asByteArrayMSB:msb |
3106 |
"depending on the size of the characters in the receiver, |
|
3107 |
return a byteArray containing single-, double- or even 4-bytewise values. |
|
3108 |
The size of the returned byteArray will be the strings size multiplied by the |
|
3109 |
size required for the largest character. |
|
3110 |
Caveat: better use utf8Encoded, to get reproducable results" |
|
3111 |
||
3112 |
|ba| |
|
3113 |
||
3114 |
ba := self asByteArray. "/ native order |
|
3115 |
UninterpretedBytes isBigEndian ~~ msb ifTrue:[ |
|
20012 | 3116 |
ba swapBytes |
19233 | 3117 |
]. |
3118 |
^ ba |
|
3119 |
! |
|
3120 |
||
20480 | 3121 |
asCanonicalizedFilename |
3122 |
"return a Filename with pathname taken from the receiver. |
|
3123 |
The filename is canonicalized, meaning that it cares for trailing directory separators, |
|
3124 |
'.' components etc." |
|
3125 |
||
3126 |
^ self asFilename asCanonicalizedFilename |
|
3127 |
||
3128 |
"on windows: |
|
3129 |
'c:\foo\bar' asFilename |
|
3130 |
'c:\foo\bar\' asFilename |
|
3131 |
'c:\foo\bar\..\baz' asFilename |
|
3132 |
'c:\foo\bar\..\baz\.' asFilename |
|
3133 |
'c:\foo\bar' asCanonicalizedFilename |
|
3134 |
'c:\foo\bar\' asCanonicalizedFilename |
|
3135 |
'c:\foo\bar\..\baz' asCanonicalizedFilename |
|
3136 |
'c:\foo\bar\..\baz\.' asCanonicalizedFilename |
|
3137 |
||
3138 |
on unix: |
|
3139 |
'/foo/bar' asFilename |
|
3140 |
'/foo/bar/' asFilename |
|
3141 |
'/foo/bar/../baz' asFilename |
|
3142 |
'/foo/bar/../baz/.' asFilename |
|
3143 |
'/foo/bar' asCanonicalizedFilename |
|
3144 |
'/foo/bar/' asCanonicalizedFilename |
|
3145 |
'/foo/bar/../baz' asCanonicalizedFilename |
|
3146 |
'/foo/bar/../baz/.' asCanonicalizedFilename |
|
3147 |
" |
|
3148 |
! |
|
3149 |
||
608 | 3150 |
asCollectionOfLines |
8874 | 3151 |
"return a collection containing the lines (separated by cr) |
608 | 3152 |
of the receiver. If multiple cr's occur in a row, the result will |
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
3153 |
contain empty strings. |
14456
d7c3e67647f1
comment/format in: #asCollectionOfLines
Claus Gittinger <cg@exept.de>
parents:
14455
diff
changeset
|
3154 |
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
|
3155 |
See also #asCollectionOfLinesWithReturn |
d7c3e67647f1
comment/format in: #asCollectionOfLines
Claus Gittinger <cg@exept.de>
parents:
14455
diff
changeset
|
3156 |
(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
|
3157 |
this method and we did not want to risk any incompatibilities)" |
14455
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3158 |
|
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3159 |
^ self asCollectionOfSubstringsSeparatedBy:Character cr. |
608 | 3160 |
|
3161 |
" |
|
3162 |
'1 one\2 two\3 three\4 four\5 five' withCRs asCollectionOfLines |
|
8874 | 3163 |
'1 one\\\\2 two\3 three' withCRs asCollectionOfLines |
20182 | 3164 |
|
3165 |
('foo \r\nbar\nbaz\t\r\n\r\nbla' printf:#()) |
|
3166 |
asCollectionOfLines collect:[:l | (l endsWith:Character return) ifTrue:[l copyButLast:1] ifFalse:l] |
|
608 | 3167 |
" |
3168 |
! |
|
3169 |
||
14455
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3170 |
asCollectionOfLinesWithReturn |
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3171 |
"return a collection containing the lines (separated by cr) |
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3172 |
of the receiver. If multiple cr's occur in a row, the result will |
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3173 |
contain empty strings." |
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3174 |
|
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3175 |
|lines| |
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3176 |
|
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3177 |
lines := self asCollectionOfSubstringsSeparatedBy:Character cr. |
18194 | 3178 |
(lines notEmpty and:[lines last isEmpty]) ifTrue:[ |
20012 | 3179 |
^ lines copyButLast:1 |
14455
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3180 |
]. |
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3181 |
^ lines |
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3182 |
|
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3183 |
" |
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3184 |
'1\2\3' withCRs asCollectionOfLines |
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3185 |
'1\2\3\' withCRs asCollectionOfLines |
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3186 |
'1\2\3' withCRs asCollectionOfLinesWithReturn |
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3187 |
'1\2\3\' withCRs asCollectionOfLinesWithReturn |
18194 | 3188 |
'' withCRs asCollectionOfLinesWithReturn |
14455
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3189 |
" |
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3190 |
! |
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3191 |
|
608 | 3192 |
asCollectionOfSubstringsSeparatedBy:aCharacter |
15740 | 3193 |
"return a collection containing substrings (separated by aCharacter) |
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
3194 |
of the receiver. |
15740 | 3195 |
If aCharacter occurs multiple times in a row, the result will contain empty strings. |
3196 |
If the receiver ends with aCharacter, an empty string with be the last result element." |
|
608 | 3197 |
|
9192 | 3198 |
^ self asCollectionOfSubCollectionsSeparatedBy:aCharacter |
3199 |
||
3200 |
" |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
3201 |
'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
|
3202 |
'1 one:2 two:3 three:4 four:5 five:' asCollectionOfSubstringsSeparatedBy:$: |
15740 | 3203 |
'1 one 2 two 3 three 4 four 5 five' asCollectionOfSubstringsSeparatedBy:Character space |
9192 | 3204 |
" |
3205 |
! |
|
608 | 3206 |
|
11828 | 3207 |
asCollectionOfSubstringsSeparatedBy:aCharacter exceptIn:ch |
15740 | 3208 |
"return a collection containing the substrings (separated by aCharacter) |
12177 | 3209 |
of the receiver. If aCharacter occurs multiple times in a row, |
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
3210 |
the result will contain empty strings. |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
3211 |
The separation is not done, inside a matching pair of ch-substrings. |
15740 | 3212 |
Can be used to tokenize csv-like strings, which may or may not be enclosed in quotes." |
11828 | 3213 |
|
3214 |
|lines myClass except i c |
|
3215 |
startIndex "{ Class:SmallInteger }" |
|
3216 |
stopIndex "{ Class:SmallInteger }" | |
|
3217 |
||
11905
cbc2191cfe2d
return stringCollections from asCollectionOfXXX
Claus Gittinger <cg@exept.de>
parents:
11904
diff
changeset
|
3218 |
lines := StringCollection new. |
11828 | 3219 |
myClass := self species. |
3220 |
||
3221 |
startIndex := 1. |
|
3222 |
except := false. |
|
3223 |
[ |
|
20012 | 3224 |
i := startIndex-1. |
3225 |
[ |
|
3226 |
i := i+1. |
|
3227 |
c := self at:i. |
|
3228 |
c = ch ifTrue:[ except := except not. ]. |
|
3229 |
i < self size and:[except or:[c ~= aCharacter]] |
|
3230 |
] whileTrue. |
|
3231 |
||
3232 |
c = aCharacter ifTrue:[ |
|
3233 |
stopIndex := i -1. |
|
3234 |
] ifFalse: [ |
|
3235 |
stopIndex := i. |
|
3236 |
]. |
|
3237 |
(stopIndex < startIndex) ifTrue: [ |
|
3238 |
lines add:(myClass new:0) |
|
3239 |
] ifFalse: [ |
|
3240 |
lines add:(self copyFrom:startIndex to:stopIndex) |
|
3241 |
]. |
|
3242 |
startIndex := stopIndex + 2. |
|
3243 |
startIndex <= self size |
|
11828 | 3244 |
] whileTrue. |
3245 |
^ lines |
|
3246 |
||
3247 |
" |
|
12177 | 3248 |
'asd''f;d''dd;s' asCollectionOfSubstringsSeparatedBy:$; exceptIn:$' |
3249 |
" |
|
3250 |
"/ 'asd "hello bla" foo "bla bla" bar' asCollectionOfSubstringsSeparatedBy:$ exceptIn:$" |
|
11828 | 3251 |
! |
3252 |
||
13344
ec6860c49990
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13343
diff
changeset
|
3253 |
asCollectionOfSubstringsSeparatedBy:aFieldSeparatorString textSeparator:aTextSeparatorOrNil |
13343
3f058ad0d2f1
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13332
diff
changeset
|
3254 |
"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
|
3255 |
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
|
3256 |
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
|
3257 |
|
13344
ec6860c49990
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13343
diff
changeset
|
3258 |
|aTextSeparatorChar items scanningWord inStream element lastIsFieldSeparator sz| |
ec6860c49990
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13343
diff
changeset
|
3259 |
|
ec6860c49990
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13343
diff
changeset
|
3260 |
aTextSeparatorOrNil isNil ifTrue:[ |
19961 | 3261 |
^ self asCollectionOfSubstringsSeparatedByAll: aFieldSeparatorString |
13327
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3262 |
]. |
13344
ec6860c49990
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13343
diff
changeset
|
3263 |
sz := aTextSeparatorOrNil size. |
ec6860c49990
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13343
diff
changeset
|
3264 |
sz = 0 ifTrue:[ |
19961 | 3265 |
aTextSeparatorChar := aTextSeparatorOrNil |
13344
ec6860c49990
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13343
diff
changeset
|
3266 |
] ifFalse:[sz = 1 ifTrue:[ |
19961 | 3267 |
"this is a String. Fetch the first character - compatibility to older expecco libs" |
3268 |
aTextSeparatorChar := aTextSeparatorOrNil first. |
|
13344
ec6860c49990
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13343
diff
changeset
|
3269 |
] ifFalse:[ |
19961 | 3270 |
self error:'textSeparatorSize > 1'. |
13344
ec6860c49990
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13343
diff
changeset
|
3271 |
]]. |
13327
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3272 |
|
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3273 |
items := OrderedCollection new. |
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3274 |
|
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3275 |
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
|
3276 |
[ |
19961 | 3277 |
inStream skipSeparators. |
3278 |
inStream atEnd |
|
13327
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3279 |
] whileFalse:[ |
19961 | 3280 |
lastIsFieldSeparator := false. |
3281 |
inStream peek == aTextSeparatorChar ifTrue:[ |
|
3282 |
inStream next. |
|
3283 |
element := ''. |
|
3284 |
scanningWord := true. |
|
3285 |
[ scanningWord and:[inStream atEnd not] ] whileTrue:[ |
|
3286 |
element := element , (inStream upTo:aTextSeparatorChar). |
|
3287 |
(inStream peek == aTextSeparatorChar) ifTrue:[ |
|
3288 |
element := element , aTextSeparatorChar . |
|
3289 |
inStream next. |
|
3290 |
] ifFalse:[ |
|
3291 |
scanningWord := false. |
|
3292 |
]. |
|
3293 |
]. |
|
3294 |
inStream upToAll_positionBefore:aFieldSeparatorString. |
|
3295 |
] ifFalse:[ |
|
3296 |
element := inStream upToAll_positionBefore:aFieldSeparatorString |
|
3297 |
]. |
|
3298 |
items add:element. |
|
3299 |
lastIsFieldSeparator := (inStream skipThroughAll:aFieldSeparatorString) notNil. |
|
13343
3f058ad0d2f1
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13332
diff
changeset
|
3300 |
]. |
3f058ad0d2f1
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13332
diff
changeset
|
3301 |
lastIsFieldSeparator ifTrue:[ |
19961 | 3302 |
"empty element at the end of the line" |
3303 |
items add:''. |
|
13327
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3304 |
]. |
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3305 |
|
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3306 |
^ items |
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3307 |
|
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3308 |
" |
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3309 |
self assert:(('#First#, #Second,SecondAdd#, #Third#' asCollectionOfSubstringsSeparatedBy:',' textSeparator: $#) |
19961 | 3310 |
sameContentsAs:#('First' 'Second,SecondAdd' 'Third')). |
13327
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3311 |
self assert:(('#Fir##st#, #Second,SecondAdd#, #Third#' asCollectionOfSubstringsSeparatedBy:',' textSeparator: $#) |
19961 | 3312 |
sameContentsAs:#('Fir#st' 'Second,SecondAdd' 'Third')). |
13327
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3313 |
self assert:(('#Fir##st#, Second,SecondAdd, #Third#' asCollectionOfSubstringsSeparatedBy:',' textSeparator: $#) |
19961 | 3314 |
sameContentsAs:#('Fir#st' 'Second' 'SecondAdd' 'Third')). |
13343
3f058ad0d2f1
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13332
diff
changeset
|
3315 |
self assert:(('First,Second,Third,,' asCollectionOfSubstringsSeparatedBy:',' textSeparator:nil) |
19961 | 3316 |
sameContentsAs:#('First' 'Second' 'Third' '' '')). |
13344
ec6860c49990
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13343
diff
changeset
|
3317 |
self assert:(('First,Second,Third,,' asCollectionOfSubstringsSeparatedBy:',' textSeparator:'#') |
19961 | 3318 |
sameContentsAs:#('First' 'Second' 'Third' '' '')). |
13327
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3319 |
" |
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3320 |
|
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3321 |
"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
|
3322 |
! |
999589445aa7
add new method for splitting a csv string into csv elements
ab
parents:
10437
diff
changeset
|
3323 |
|
7339
20969b5770c9
asCollectionOfSubstringsSeparatedByAll:
Claus Gittinger <cg@exept.de>
parents:
7318
diff
changeset
|
3324 |
asCollectionOfSubstringsSeparatedByAll:aSeparatorString |
8874 | 3325 |
"return a collection containing the lines (separated by aSeparatorString) |
3326 |
of the receiver. If aSeparatorString occurs multiple times in a row, |
|
7339
20969b5770c9
asCollectionOfSubstringsSeparatedByAll:
Claus Gittinger <cg@exept.de>
parents:
7318
diff
changeset
|
3327 |
the result will contain empty strings." |
20969b5770c9
asCollectionOfSubstringsSeparatedByAll:
Claus Gittinger <cg@exept.de>
parents:
7318
diff
changeset
|
3328 |
|
8852
9f5984eff2c2
use asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8794
diff
changeset
|
3329 |
^ self asCollectionOfSubCollectionsSeparatedByAll:aSeparatorString |
9f5984eff2c2
use asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8794
diff
changeset
|
3330 |
|
9f5984eff2c2
use asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8794
diff
changeset
|
3331 |
" |
8874 | 3332 |
'1::2::3::4::5::' asCollectionOfSubstringsSeparatedByAll:'::' |
7339
20969b5770c9
asCollectionOfSubstringsSeparatedByAll:
Claus Gittinger <cg@exept.de>
parents:
7318
diff
changeset
|
3333 |
" |
20969b5770c9
asCollectionOfSubstringsSeparatedByAll:
Claus Gittinger <cg@exept.de>
parents:
7318
diff
changeset
|
3334 |
! |
20969b5770c9
asCollectionOfSubstringsSeparatedByAll:
Claus Gittinger <cg@exept.de>
parents:
7318
diff
changeset
|
3335 |
|
608 | 3336 |
asCollectionOfSubstringsSeparatedByAny:aCollectionOfSeparators |
3337 |
"return a collection containing the words (separated by any character |
|
3338 |
from aCollectionOfSeparators) of the receiver. |
|
3339 |
This allows breaking up strings using any character as separator." |
|
3340 |
||
8852
9f5984eff2c2
use asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8794
diff
changeset
|
3341 |
^ self asCollectionOfSubCollectionsSeparatedByAny:aCollectionOfSeparators |
608 | 3342 |
|
3343 |
" |
|
3344 |
'hello:world:isnt:this nice' asCollectionOfSubstringsSeparatedByAny:#($:) |
|
8874 | 3345 |
'hello:world:isnt:this nice' asCollectionOfSubstringsSeparatedByAny:':' |
3346 |
'hello:world:isnt:this nice' asCollectionOfSubstringsSeparatedByAny:(Array with:$: with:Character space) |
|
3347 |
'hello:world:isnt:this nice' asCollectionOfSubstringsSeparatedByAny:': ' |
|
3348 |
'h1e2l3l4o' asCollectionOfSubstringsSeparatedByAny:($1 to: $9) |
|
608 | 3349 |
" |
3350 |
! |
|
3351 |
||
3352 |
asCollectionOfWords |
|
8874 | 3353 |
"return a collection containing the words (separated by whitespace) |
3354 |
of the receiver. Multiple occurrences of whitespace characters will |
|
17517 | 3355 |
be treated like one - i.e. whitespace is skipped. |
3356 |
(sigh: it is called #'subStrings' in V'Age, and #'substrings' in Squeak) " |
|
608 | 3357 |
|
7797 | 3358 |
|words| |
608 | 3359 |
|
11905
cbc2191cfe2d
return stringCollections from asCollectionOfXXX
Claus Gittinger <cg@exept.de>
parents:
11904
diff
changeset
|
3360 |
words := StringCollection new. |
7797 | 3361 |
self asCollectionOfWordsDo:[:w | words add:w]. |
608 | 3362 |
^ words |
3363 |
||
3364 |
" |
|
3365 |
'hello world isnt this nice' asCollectionOfWords |
|
3366 |
' hello world isnt this nice ' asCollectionOfWords |
|
3367 |
'hello' asCollectionOfWords |
|
3368 |
'' asCollectionOfWords |
|
3369 |
' ' asCollectionOfWords |
|
10703
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
3370 |
' foo bar__baz__bla__ bar ' asCollectionOfWords |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
3371 |
' foo __bar__baz__bla__ bar ' asCollectionOfWords |
608 | 3372 |
" |
9192 | 3373 |
! |
3374 |
||
3375 |
asCollectionOfWordsDo:aBlock |
|
8874 | 3376 |
"evaluate aBlock for each word (separated by whitespace) of the receiver. |
3377 |
Multiple occurrences of whitespace characters will be treated like one |
|
7797 | 3378 |
- i.e. whitespace is skipped. |
3379 |
Returns the number of words (i.e. the number of invocations of aBlock)." |
|
3380 |
||
3381 |
|count "{ Class:SmallInteger }" |
|
8874 | 3382 |
start "{ Class:SmallInteger }" |
3383 |
stop "{ Class:SmallInteger }" |
|
7797 | 3384 |
mySize "{ Class:SmallInteger }"| |
3385 |
||
3386 |
count := 0. |
|
3387 |
start := 1. |
|
3388 |
mySize := self size. |
|
3389 |
[start <= mySize] whileTrue:[ |
|
20012 | 3390 |
start := self indexOfNonSeparatorStartingAt:start. |
3391 |
start == 0 ifTrue:[ |
|
3392 |
^ count |
|
3393 |
]. |
|
3394 |
stop := self indexOfSeparatorStartingAt:start. |
|
3395 |
stop == 0 ifTrue:[ |
|
3396 |
aBlock value:(self copyFrom:start to:mySize). |
|
3397 |
^ count + 1 |
|
3398 |
]. |
|
3399 |
aBlock value:(self copyFrom:start to:(stop - 1)). |
|
3400 |
start := stop. |
|
3401 |
count := count + 1 |
|
7797 | 3402 |
]. |
3403 |
^ count |
|
3404 |
||
3405 |
" |
|
3406 |
'hello world isnt this nice' asCollectionOfWordsDo:[:w | Transcript showCR:w] |
|
3407 |
' hello world isnt this nice ' asCollectionOfWordsDo:[:w | Transcript showCR:w] |
|
3408 |
'hello' asCollectionOfWordsDo:[:w | Transcript showCR:w] |
|
3409 |
'' asCollectionOfWordsDo:[:w | Transcript showCR:w] |
|
3410 |
' ' asCollectionOfWordsDo:[:w | Transcript showCR:w] |
|
3411 |
" |
|
9192 | 3412 |
! |
3413 |
||
3414 |
asFilename |
|
3415 |
"return a Filename with pathname taken from the receiver" |
|
3416 |
||
3417 |
^ Filename named:self "(self asSingleByteStringReplaceInvalidWith:$?)" |
|
3418 |
||
3419 |
"Modified: 20.5.1996 / 09:38:15 / cg" |
|
3420 |
! |
|
608 | 3421 |
|
3060 | 3422 |
asFixedPoint |
3423 |
"read a fixedPoint number from the receiver. |
|
8874 | 3424 |
Notice, that errors may occur during the read, |
7423 | 3425 |
so you better setup some signal handler when using this method." |
3060 | 3426 |
|
3427 |
^ FixedPoint readFromString:self |
|
3428 |
||
3429 |
" |
|
8874 | 3430 |
'0.123' asFixedPoint |
3431 |
'12345' asFixedPoint |
|
3060 | 3432 |
'(1/5)' asFixedPoint |
3433 |
'foo' asFixedPoint |
|
9192 | 3434 |
Object errorSignal handle:[:ex | ex return:0] do:['foo' asFixedPoint] |
3435 |
" |
|
3436 |
||
3437 |
"Modified: / 25.10.1997 / 15:19:00 / cg" |
|
3438 |
! |
|
3439 |
||
3440 |
asFixedPoint:scale |
|
3060 | 3441 |
"read a fixedPoint number with scale number of post-decimal digits |
3442 |
from the receiver. Scale controls the number of displayed digits, |
|
3443 |
not the number of actually valid digits. |
|
8874 | 3444 |
Notice, that errors may occur during the read, |
7423 | 3445 |
so you better setup some signal handler when using this method." |
3060 | 3446 |
|
3447 |
^ (FixedPoint readFromString:self) scale:scale |
|
3448 |
||
3449 |
" |
|
8874 | 3450 |
'0.123' asFixedPoint:2 |
3451 |
'123456' asFixedPoint:2 |
|
3452 |
('3.14157' asFixedPoint:1) asFixedPoint:5 |
|
3453 |
'3.14157' asFixedPoint:2 |
|
3454 |
'foo' asFixedPoint:2 |
|
3060 | 3455 |
" |
3456 |
||
3457 |
"Modified: / 25.10.1997 / 15:21:57 / cg" |
|
9192 | 3458 |
! |
3459 |
||
3460 |
asFloat |
|
608 | 3461 |
"read a float number from the receiver. |
8874 | 3462 |
Notice, that errors may occur during the read, |
7422 | 3463 |
so you better setup some exception handler when using this method." |
608 | 3464 |
|
3465 |
^ (Number readFromString:self) asFloat |
|
3466 |
||
3467 |
" |
|
8874 | 3468 |
'0.123' asFloat |
608 | 3469 |
'12345' asFloat |
3470 |
'(1/5)' asFloat |
|
8874 | 3471 |
Object errorSignal handle:[:ex | ex return:0] do:['foo' asFloat] |
608 | 3472 |
" |
3473 |
! |
|
3474 |
||
3475 |
asInteger |
|
9190 | 3476 |
"convert the receiver into an integer. |
8874 | 3477 |
Notice, that errors may occur during the read, |
9190 | 3478 |
so you better setup some exception handler when using this method. |
3479 |
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
|
3480 |
in other smalltalks. |
9190 | 3481 |
For less strict integer reading, use Integer readFrom:aString" |
608 | 3482 |
|
3483 |
^ Integer readFromString:self |
|
3484 |
||
3485 |
" |
|
3486 |
'12345678901234567890' asInteger |
|
3487 |
'-1234' asInteger |
|
9190 | 3488 |
|
3489 |
The following raises an error: |
|
20012 | 3490 |
'0.123' asInteger <- reader finds more after reading 0 |
9190 | 3491 |
|
3492 |
whereas the less strict readFrom does not: |
|
20012 | 3493 |
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
|
3494 |
|
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
3495 |
'0.123' asInteger |
608 | 3496 |
'0.123' asNumber <- returns what you expect |
8874 | 3497 |
Object errorSignal handle:[:ex | ex return:0] do:['foo' asInteger] |
9190 | 3498 |
|
3499 |
" |
|
9192 | 3500 |
! |
3501 |
||
3502 |
asLowercase |
|
608 | 3503 |
"return a copy of myself in lowercase letters" |
3504 |
||
8003
6e690f4b79f1
asLowercase/asUppercase and friends care for 16-bit chars
Claus Gittinger <cg@exept.de>
parents:
7995
diff
changeset
|
3505 |
|newStr c bitsPerCharacter |
9192 | 3506 |
mySize "{ Class: SmallInteger }" | |
3507 |
||
3508 |
mySize := self size. |
|
3509 |
mySize == 0 ifTrue:[^ self]. |
|
18657 | 3510 |
|
9192 | 3511 |
newStr := self species new:mySize. |
3512 |
bitsPerCharacter := newStr bitsPerCharacter. |
|
3513 |
||
18657 | 3514 |
"/ handle the very seldom case of an uppercase char which needs |
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
3515 |
"/ more bits in its lowercase variant |
18657 | 3516 |
"/ (there are only a few of them) |
3517 |
||
9192 | 3518 |
1 to:mySize do:[:i | |
20705 | 3519 |
c := (self at:i) asLowercase. |
3520 |
(c bitsPerCharacter > bitsPerCharacter |
|
3521 |
and:[c stringSpecies ~= newStr stringSpecies]) ifTrue:[ |
|
3522 |
newStr := c stringSpecies fromString:newStr. |
|
3523 |
bitsPerCharacter := newStr bitsPerCharacter. |
|
3524 |
]. |
|
3525 |
newStr at:i put:c |
|
9192 | 3526 |
]. |
3527 |
^ newStr |
|
3528 |
||
3529 |
" |
|
3530 |
'HelloWorld' asLowercase |
|
18662 | 3531 |
'HelloWorld' asUnicode16String asLowercase |
9192 | 3532 |
'HelloWorld' asLowercaseFirst |
18657 | 3533 |
'HelloWorld' asUppercase |
20705 | 3534 |
'HelloWorldÿ' asUppercase |
3535 |
'HelloWorldŸ' asLowercase - currently returns an U16 string; should this be u8? |
|
9192 | 3536 |
" |
3537 |
! |
|
3538 |
||
3539 |
asLowercaseFirst |
|
17508 | 3540 |
"return a copy of myself where the first character is converted to lowercase. |
3541 |
If the first character is already lowercase, or there is no uppercase for it, return the |
|
3542 |
receiver." |
|
9192 | 3543 |
|
3544 |
|newString firstChar firstCharAsLowercase| |
|
3545 |
||
11525 | 3546 |
self isEmpty ifTrue:[^ self]. |
9192 | 3547 |
firstChar := (self at:1). |
3548 |
firstCharAsLowercase := firstChar asLowercase. |
|
3549 |
firstChar == firstCharAsLowercase ifTrue:[ ^ self]. |
|
3550 |
||
3551 |
firstCharAsLowercase bitsPerCharacter > self bitsPerCharacter ifTrue:[ |
|
20012 | 3552 |
newString := firstCharAsLowercase stringSpecies fromString:self. |
9192 | 3553 |
] ifFalse:[ |
20012 | 3554 |
newString := self stringSpecies fromString:self. |
9192 | 3555 |
]. |
9190 | 3556 |
newString at:1 put:firstCharAsLowercase. |
3557 |
^ newString |
|
608 | 3558 |
|
3559 |
" |
|
8874 | 3560 |
'HelloWorld' asLowercase |
3561 |
'HelloWorld' asLowercaseFirst |
|
608 | 3562 |
" |
3563 |
! |
|
3564 |
||
11828 | 3565 |
asLowercaseLast |
3566 |
"return a copy of myself where the last character is |
|
3567 |
converted to lowercase." |
|
3568 |
||
3569 |
|newString sz| |
|
3570 |
||
3571 |
sz := self size. |
|
3572 |
newString := self copyFrom:1 to:sz. |
|
3573 |
sz > 0 ifTrue:[ |
|
20012 | 3574 |
newString at:sz put:(newString at:sz) asLowercase |
11828 | 3575 |
]. |
3576 |
^ newString |
|
3577 |
||
3578 |
" |
|
12177 | 3579 |
'HelloWorld' asLowercase |
3580 |
'HelloWorlD' asLowercaseLast |
|
11828 | 3581 |
" |
3582 |
! |
|
3583 |
||
17644 | 3584 |
asMutator |
19473 | 3585 |
"return a corresponding setter method's selector. |
3586 |
I.e. #foo asMutator returns #foo:" |
|
17644 | 3587 |
|
3588 |
^ (self asOneByteString,':') asSymbol |
|
3589 |
! |
|
3590 |
||
9190 | 3591 |
asNumber |
3592 |
"read a number from the receiver. |
|
3593 |
Notice, that (in contrast to ST-80) errors may occur during the read, |
|
3594 |
so you better setup some signal handler when using this method. |
|
3595 |
Also notice, that this is meant to read end-user numbers from a string; |
|
9135 | 3596 |
it does not handle smalltalk numbers (i.e. radix). |
3597 |
To read a smalltalk number, use Number >> readSmalltalkFrom:. |
|
608 | 3598 |
This may change if ANSI specifies it." |
3599 |
||
3600 |
"/ ST-80 behavior: |
|
3601 |
"/ ^ Number readFromString:self onError:0 |
|
3602 |
||
17061 | 3603 |
^ Number fromString:self |
3604 |
||
3605 |
" |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
3606 |
'123' asNumber |
608 | 3607 |
'123.567' asNumber |
3608 |
'(5/6)' asNumber |
|
3609 |
'foo' asNumber |
|
17061 | 3610 |
'123a' asNumber |
8874 | 3611 |
Object errorSignal handle:[:ex | ex returnWith:0] do:['foo' asNumber] |
608 | 3612 |
" |
9192 | 3613 |
! |
3614 |
||
3615 |
asNumberFromFormatString:ignored |
|
608 | 3616 |
"read a number from the receiver, ignoring any nonDigit characters. |
3617 |
This is typically used to convert from strings which include |
|
3618 |
dollar-signs or millenium digits. However, this method also ignores |
|
3619 |
the decimal point (if any) and therefore should be used with care." |
|
3620 |
||
3621 |
|tempString| |
|
3622 |
||
3623 |
tempString := self collect:[:char | char isDigit]. |
|
3624 |
^ Number readFromString:tempString onError:0 |
|
3625 |
||
3626 |
" |
|
3627 |
'USD 123' asNumberFromFormatString:'foo' |
|
3628 |
'DM 123' asNumberFromFormatString:'foo' |
|
3629 |
'123' asNumberFromFormatString:'foo' |
|
3630 |
'123.567' asNumberFromFormatString:'foo' |
|
3631 |
'(5/6)' asNumberFromFormatString:'foo' |
|
3632 |
'foo' asNumberFromFormatString:'foo' |
|
3633 |
" |
|
9192 | 3634 |
! |
3635 |
||
7977 | 3636 |
asSingleByteStringIfPossible |
3637 |
"if possible, return the receiver converted to a 'normal' string. |
|
18181 | 3638 |
It is only possible, if there are no characters with codePoints above 255 in the receiver. |
3639 |
If not possible, the (wideString) receiver is returned." |
|
7977 | 3640 |
|
14555 | 3641 |
self isWideString ifFalse:[^ self]. |
18967 | 3642 |
self containsNon8BitElements ifTrue:[^ self]. |
3643 |
^ self asSingleByteString |
|
9192 | 3644 |
|
3645 |
" |
|
3646 |
'hello' asSingleByteStringIfPossible |
|
18967 | 3647 |
'hello' asText asSingleByteStringIfPossible |
3648 |
'hello' asUnicodeString asText asSingleByteStringIfPossible |
|
9192 | 3649 |
'hello' asUnicodeString asSingleByteStringIfPossible |
3650 |
" |
|
3651 |
! |
|
3652 |
||
3653 |
asSingleByteStringReplaceInvalidWith:replacementCharacter |
|
3654 |
"return the receiver converted to a 'normal' string, |
|
3655 |
with invalid characters replaced by replacementCharacter. |
|
3656 |
Can be used to convert from 16-bit strings to 8-bit strings |
|
3657 |
and replace characters above code-255 with some replacement." |
|
3658 |
||
3659 |
|newString| |
|
3660 |
||
3661 |
newString := String new:(self size). |
|
3662 |
1 to:self size do:[:idx | |
|
20012 | 3663 |
|char| |
3664 |
||
3665 |
char := self at:idx. |
|
3666 |
char codePoint <= 16rFF ifTrue:[ |
|
3667 |
newString at:idx put:char |
|
3668 |
] ifFalse:[ |
|
3669 |
newString at:idx put:replacementCharacter |
|
3670 |
]. |
|
9190 | 3671 |
]. |
3672 |
^ newString |
|
3673 |
||
3674 |
"Created: 30.6.1997 / 13:02:14 / cg" |
|
7977 | 3675 |
! |
3676 |
||
9192 | 3677 |
asString |
3678 |
"return myself - I am a string" |
|
3679 |
||
3680 |
^ self |
|
3681 |
! |
|
3682 |
||
3683 |
asStringCollection |
|
3684 |
"return a collection of lines from myself." |
|
3685 |
||
3686 |
^ StringCollection fromString:self "string" |
|
3687 |
||
3688 |
"Modified: 13.5.1996 / 20:36:59 / cg" |
|
3689 |
! |
|
1375
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
3690 |
|
1434 | 3691 |
asSymbol |
19039 | 3692 |
"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
|
3693 |
The receiver must be a singleByte-String. |
ccd0e8f26439
printing - migrating towards unicodeSupport in ST-code
Claus Gittinger <cg@exept.de>
parents:
8040
diff
changeset
|
3694 |
TwoByte- and FourByteSymbols are (currently ?) not allowed." |
ccd0e8f26439
printing - migrating towards unicodeSupport in ST-code
Claus Gittinger <cg@exept.de>
parents:
8040
diff
changeset
|
3695 |
|
ccd0e8f26439
printing - migrating towards unicodeSupport in ST-code
Claus Gittinger <cg@exept.de>
parents:
8040
diff
changeset
|
3696 |
|str| |
ccd0e8f26439
printing - migrating towards unicodeSupport in ST-code
Claus Gittinger <cg@exept.de>
parents:
8040
diff
changeset
|
3697 |
|
9192 | 3698 |
str := self string. |
3699 |
str ~~ self ifTrue:[ ^ str asSymbol ]. |
|
3700 |
^ self asSingleByteString asSymbol |
|
3701 |
! |
|
3702 |
||
3703 |
asSymbolIfInterned |
|
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
3704 |
"If a symbol with the receiver's characters is already known, return it. |
19692 | 3705 |
Otherwise, return nil. |
9230 | 3706 |
This can be used to query for an existing symbol and is the same as: |
20012 | 3707 |
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
|
3708 |
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
|
3709 |
The receiver must be a singleByte-String. |
ccd0e8f26439
printing - migrating towards unicodeSupport in ST-code
Claus Gittinger <cg@exept.de>
parents:
8040
diff
changeset
|
3710 |
TwoByte- and FourByteSymbols are (currently ?) not allowed." |
1435
3ee1fde2aa58
allow asSymbolIfInterned for Texts
Claus Gittinger <cg@exept.de>
parents:
1434
diff
changeset
|
3711 |
|
18967 | 3712 |
|str| |
3713 |
||
3714 |
str := self string. |
|
3715 |
str == self ifTrue:[ |
|
20012 | 3716 |
"must be some kind of N-ByteString" |
3717 |
str := self asSingleByteStringIfPossible. |
|
3718 |
str == self ifTrue:[ |
|
3719 |
"single byte string conversion is not possible" |
|
3720 |
^ nil. |
|
3721 |
]. |
|
6642 | 3722 |
]. |
18967 | 3723 |
^ str asSymbolIfInterned |
3724 |
||
3725 |
" |
|
3726 |
(Unicode16String with:(Character value:16rFFFF)) asSymbolIfInterned |
|
3727 |
'new' asUnicodeString asSymbolIfInterned |
|
3728 |
'new' asText asSymbolIfInterned |
|
3729 |
'new' asUnicodeString asText asSymbolIfInterned |
|
3730 |
" |
|
1435
3ee1fde2aa58
allow asSymbolIfInterned for Texts
Claus Gittinger <cg@exept.de>
parents:
1434
diff
changeset
|
3731 |
|
3ee1fde2aa58
allow asSymbolIfInterned for Texts
Claus Gittinger <cg@exept.de>
parents:
1434
diff
changeset
|
3732 |
"Created: 22.5.1996 / 16:37:04 / cg" |
9192 | 3733 |
! |
3734 |
||
20160 | 3735 |
asSymbolIfInternedOrSelf |
3736 |
"If a symbol with the receiver's characters is already known, return it. |
|
3737 |
Otherwise, return self. |
|
3738 |
This can be used to query for an existing symbol and is the same as: |
|
3739 |
self knownAsSymbol ifTrue:[self asSymbol] ifFalse:[self] |
|
3740 |
but slightly faster, since the symbol lookup operation is only performed once. |
|
3741 |
The receiver must be a singleByte-String. |
|
3742 |
TwoByte- and FourByteSymbols are (currently ?) not allowed." |
|
3743 |
||
3744 |
|sym| |
|
3745 |
||
3746 |
sym := self asSymbolIfInterned. |
|
3747 |
^ sym isNil ifTrue:[self] ifFalse:[sym]. |
|
3748 |
||
3749 |
||
3750 |
" |
|
3751 |
(Unicode16String with:(Character value:16rFFFF)) asSymbolIfInternedOrSelf |
|
3752 |
'new' asUnicodeString asSymbolIfInternedOrSelf |
|
3753 |
'new' asText asSymbolIfInternedOrSelf |
|
3754 |
'new' asUnicodeString asText asSymbolIfInternedOrSelf |
|
3755 |
" |
|
3756 |
||
3757 |
"Created: 22.5.1996 / 16:37:04 / cg" |
|
3758 |
! |
|
3759 |
||
9192 | 3760 |
asText |
9403 | 3761 |
"return a Text-object (string with emphasis) from myself." |
9192 | 3762 |
|
3763 |
"this test allows for small non-gui apps to be built without libbasic2 (where Text is)" |
|
3764 |
Text isNil ifTrue:[^ self]. |
|
17065 | 3765 |
^ Text string:self |
9192 | 3766 |
|
3767 |
"Created: 12.5.1996 / 10:41:14 / cg" |
|
3768 |
! |
|
3769 |
||
17021 | 3770 |
asTimestamp |
3771 |
"convert the receiver into an Timestamp. |
|
3772 |
Notice, that errors may occur during the read, |
|
3773 |
so you better setup some exception handler when using this method." |
|
3774 |
||
3775 |
^ Timestamp readFromString:self |
|
3776 |
||
3777 |
" |
|
3778 |
'2014-11-10 21:30:22.444' asTimestamp |
|
3779 |
'2014-11-10 21:30:22.444Z' asTimestamp |
|
3780 |
'2014-11-10 21:30:22.444+0200' asTimestamp |
|
3781 |
" |
|
3782 |
! |
|
3783 |
||
9192 | 3784 |
asTitlecase |
8027 | 3785 |
"return a version of the receiver, where the first character is converted to titlecase, |
3786 |
and everything else to lowercase. |
|
3787 |
See the comment in Character on what titlecase is." |
|
3788 |
||
3789 |
|newStr c bitsPerCharacter |
|
3790 |
mySize "{ Class: SmallInteger }" | |
|
3791 |
||
3792 |
mySize := self size. |
|
3793 |
newStr := self species new:mySize. |
|
3794 |
bitsPerCharacter := newStr bitsPerCharacter. |
|
3795 |
||
3796 |
1 to:mySize do:[:i | |
|
20012 | 3797 |
i == 1 ifTrue:[ |
3798 |
c := (self at:i) asTitlecase. |
|
3799 |
] ifFalse:[ |
|
3800 |
c := (self at:i) asLowercase. |
|
3801 |
]. |
|
3802 |
c bitsPerCharacter > bitsPerCharacter ifTrue:[ |
|
3803 |
newStr := c stringSpecies fromString:newStr. |
|
3804 |
bitsPerCharacter := newStr bitsPerCharacter. |
|
3805 |
]. |
|
3806 |
newStr at:i put:c |
|
8027 | 3807 |
]. |
3808 |
^ newStr |
|
3809 |
||
3810 |
" |
|
8874 | 3811 |
'helloWorld' asTitlecase |
3812 |
'HelloWorld' asTitlecase |
|
3813 |
'HELLOWORLD' asTitlecase |
|
3814 |
'helloworld' asTitlecase |
|
8027 | 3815 |
" |
9192 | 3816 |
! |
3817 |
||
3818 |
asTitlecaseFirst |
|
8023 | 3819 |
"return a version of the receiver, where the first character is converted to titlecase. |
8024 | 3820 |
Titlecase is much like uppercase for most characters, with the exception of some combined |
3821 |
(2-character glyphs), which consist of an upper- and lower-case characters. |
|
3822 |
If the first character is already titlecase, or there is no titlecasepercase for it, return the |
|
8023 | 3823 |
receiver." |
3824 |
||
8026 | 3825 |
" |
8874 | 3826 |
For example, in Unicode, character U+01F3 is LATIN SMALL LETTER DZ. |
3827 |
(Let us write this compound character using ASCII as 'dz'.) |
|
3828 |
This character uppercases to character U+01F1, LATIN CAPITAL LETTER DZ. |
|
3829 |
(Which is basically 'DZ'.) |
|
3830 |
But it titlecases to to character U+01F2, LATIN CAPITAL LETTER D WITH SMALL LETTER Z. |
|
8026 | 3831 |
(Which we can write 'Dz'.) |
3832 |
||
3833 |
character uppercase titlecase |
|
3834 |
--------- --------- --------- |
|
3835 |
dz DZ Dz |
|
3836 |
" |
|
3837 |
||
8023 | 3838 |
|newString firstChar firstCharAsTitlecase| |
3839 |
||
3840 |
firstChar := (self at:1). |
|
8025 | 3841 |
firstCharAsTitlecase := firstChar asTitlecase. |
8023 | 3842 |
firstChar == firstCharAsTitlecase ifTrue:[ ^ self]. |
8037
8cce25b1059c
care for wide chars in asXXXFirst
Claus Gittinger <cg@exept.de>
parents:
8032
diff
changeset
|
3843 |
|
8cce25b1059c
care for wide chars in asXXXFirst
Claus Gittinger <cg@exept.de>
parents:
8032
diff
changeset
|
3844 |
firstCharAsTitlecase bitsPerCharacter > self bitsPerCharacter ifTrue:[ |
20012 | 3845 |
newString := firstCharAsTitlecase stringSpecies fromString:self. |
8037
8cce25b1059c
care for wide chars in asXXXFirst
Claus Gittinger <cg@exept.de>
parents:
8032
diff
changeset
|
3846 |
] ifFalse:[ |
20012 | 3847 |
newString := self stringSpecies fromString:self. |
8037
8cce25b1059c
care for wide chars in asXXXFirst
Claus Gittinger <cg@exept.de>
parents:
8032
diff
changeset
|
3848 |
]. |
8023 | 3849 |
newString at:1 put:firstCharAsTitlecase. |
3850 |
^ newString |
|
3851 |
||
3852 |
" |
|
8874 | 3853 |
'helloWorld' asTitlecaseFirst |
3854 |
'HelloWorld' asTitlecaseFirst |
|
8023 | 3855 |
" |
9192 | 3856 |
! |
3857 |
||
3858 |
asTwoByteString |
|
3859 |
"return the receiver converted to a two-byte string. |
|
16051 | 3860 |
Will be obsolete soon - use asUnicode16String." |
9192 | 3861 |
|
3862 |
^ TwoByteString fromString:self |
|
3863 |
! |
|
3864 |
||
3865 |
asURI |
|
3866 |
"return an URI with string taken from the receiver" |
|
3867 |
||
3868 |
^ URI fromString:self |
|
3869 |
! |
|
8710 | 3870 |
|
5776 | 3871 |
asURL |
3872 |
"return an URL-object from myself." |
|
3873 |
||
3874 |
^ URL fromString:self |
|
3875 |
||
3876 |
" |
|
8874 | 3877 |
'http://www.exept.de:80/index.html' asURL host |
3878 |
'http://www.exept.de:80/index.html' asURL port |
|
9192 | 3879 |
'http://www.exept.de:80/index.html' asURL method |
3880 |
'http://www.exept.de:80/index.html' asURL path |
|
3881 |
" |
|
3882 |
! |
|
3883 |
||
18901 | 3884 |
asUUID |
3885 |
"return self as a UUID" |
|
3886 |
||
3887 |
^ UUID fromString:self |
|
3888 |
||
3889 |
" |
|
3890 |
UUID genUUID asString asUUID |
|
3891 |
'{EAB22AC0-30C1-11CF-A7EB-0000C05BAE0B}' asUUID |
|
3892 |
'{EAB22AC0-30C1-11CF-A7EB-0000C05BAE0B}' asUnicodeString asUUID |
|
3893 |
'EAB22AC0-30C1-11CF-A7EB-0000C05BAE0B' asUUID |
|
3894 |
'EAB22AC0-30C1-11CF-A7EB-0000C05BAE0B quatsch' asUUID |
|
3895 |
'quark EAB22AC0-30C1-11CF-A7EB-0000C05BAE0B' asUUID |
|
3896 |
" |
|
3897 |
||
3898 |
"Modified: / 02-08-2007 / 16:43:29 / cg" |
|
3899 |
! |
|
3900 |
||
9192 | 3901 |
asUnicode16String |
15035 | 3902 |
"return the receiver in a two-byte per character representation. |
3903 |
Normally, the internal ST/X representation should be transparent and not |
|
3904 |
of the programmer's concern; except when strings are passed to the outside world, |
|
3905 |
such as wide-string ffi calls or file contents." |
|
3906 |
||
17600 | 3907 |
|sz| |
3908 |
||
3909 |
sz := self size. |
|
3910 |
||
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
3911 |
^ (Unicode16String new:sz) |
20012 | 3912 |
replaceFrom:1 to:sz with:self startingAt:1; |
3913 |
yourself. |
|
3914 |
||
3915 |
" |
|
3916 |
'abc' asUnicode16String |
|
17600 | 3917 |
" |
3918 |
! |
|
3919 |
||
3920 |
asUnicode16StringZ |
|
3921 |
"return the receiver in a two-byte per character representation. |
|
17625 | 3922 |
Make sure that is zero terminated (last char is 16r0000). |
17600 | 3923 |
Normally, the internal ST/X representation should be transparent and not |
3924 |
of the programmer's concern; except when strings are passed to the outside world, |
|
3925 |
such as wide-string ffi calls or file contents." |
|
3926 |
||
3927 |
|sz| |
|
3928 |
||
3929 |
sz := self size. |
|
3930 |
(self at:sz) == (Character codePoint:0) ifTrue:[ |
|
20012 | 3931 |
^ self asUnicode16String. |
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
3932 |
]. |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
3933 |
|
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
3934 |
^ (Unicode16String new:sz+1) |
20012 | 3935 |
replaceFrom:1 to:sz with:self startingAt:1; |
3936 |
at:sz+1 put:(Character codePoint:0); |
|
3937 |
yourself. |
|
17600 | 3938 |
|
3939 |
" |
|
20012 | 3940 |
'abc' asUnicode16StringZ |
3941 |
'abc' asUnicode16String asUnicode16StringZ |
|
17600 | 3942 |
" |
9192 | 3943 |
! |
8295
8433d819226b
Define #asUnicode16String and #asUnicode32String in CharacterArray
Stefan Vogel <sv@exept.de>
parents:
8222
diff
changeset
|
3944 |
|
8433d819226b
Define #asUnicode16String and #asUnicode32String in CharacterArray
Stefan Vogel <sv@exept.de>
parents:
8222
diff
changeset
|
3945 |
asUnicode32String |
16051 | 3946 |
"return the receiver in a four-byte per character representation. |
3947 |
Normally, the internal ST/X representation should be transparent and not |
|
3948 |
of the programmer's concern; except when strings are passed to the outside world, |
|
3949 |
such as wide-string ffi calls or file contents." |
|
3950 |
||
3951 |
^ self as:Unicode32String |
|
8295
8433d819226b
Define #asUnicode16String and #asUnicode32String in CharacterArray
Stefan Vogel <sv@exept.de>
parents:
8222
diff
changeset
|
3952 |
! |
8433d819226b
Define #asUnicode16String and #asUnicode32String in CharacterArray
Stefan Vogel <sv@exept.de>
parents:
8222
diff
changeset
|
3953 |
|
8433d819226b
Define #asUnicode16String and #asUnicode32String in CharacterArray
Stefan Vogel <sv@exept.de>
parents:
8222
diff
changeset
|
3954 |
asUnicodeString |
16051 | 3955 |
"return the receiver in a two-byte per character representation. |
3956 |
Normally, the internal ST/X representation should be transparent and not |
|
3957 |
of the programmer's concern; except when strings are passed to the outside world, |
|
3958 |
such as wide-string ffi calls or file contents." |
|
3959 |
||
3960 |
^ self asUnicode16String |
|
3961 |
" |
|
3962 |
'Hello World' asUnicodeString |
|
14141
e86d245213b0
comment/format in: #asUnicodeString
Stefan Vogel <sv@exept.de>
parents:
14137
diff
changeset
|
3963 |
" |
8295
8433d819226b
Define #asUnicode16String and #asUnicode32String in CharacterArray
Stefan Vogel <sv@exept.de>
parents:
8222
diff
changeset
|
3964 |
! |
8433d819226b
Define #asUnicode16String and #asUnicode32String in CharacterArray
Stefan Vogel <sv@exept.de>
parents:
8222
diff
changeset
|
3965 |
|
13073 | 3966 |
asUnixFilename |
3967 |
"return a UnixFilename with pathname taken from the receiver" |
|
3968 |
||
3969 |
^ UnixFilename named:self |
|
3970 |
||
3971 |
"Created: / 20-09-2010 / 21:42:55 / cg" |
|
3972 |
! |
|
3973 |
||
608 | 3974 |
asUppercase |
3975 |
"return a copy of myself in uppercase letters" |
|
3976 |
||
8003
6e690f4b79f1
asLowercase/asUppercase and friends care for 16-bit chars
Claus Gittinger <cg@exept.de>
parents:
7995
diff
changeset
|
3977 |
|newStr c bitsPerCharacter |
608 | 3978 |
mySize "{ Class: SmallInteger }" | |
3979 |
||
3980 |
mySize := self size. |
|
8954
12e701d7b5ac
allow for receiver to be empty in asLowercase and asUppercase.
Claus Gittinger <cg@exept.de>
parents:
8938
diff
changeset
|
3981 |
mySize == 0 ifTrue:[^ self]. |
608 | 3982 |
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
|
3983 |
bitsPerCharacter := newStr bitsPerCharacter. |
6e690f4b79f1
asLowercase/asUppercase and friends care for 16-bit chars
Claus Gittinger <cg@exept.de>
parents:
7995
diff
changeset
|
3984 |
|
18657 | 3985 |
"/ handle the very seldom case of a lowercase char which needs |
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
3986 |
"/ more bits in its uppercase variant |
18657 | 3987 |
"/ (there are only a few of them) |
3988 |
||
608 | 3989 |
1 to:mySize do:[:i | |
20012 | 3990 |
c := (self at:i) asUppercase. |
3991 |
c bitsPerCharacter > bitsPerCharacter ifTrue:[ |
|
3992 |
newStr := c stringSpecies fromString:newStr. |
|
3993 |
bitsPerCharacter := newStr bitsPerCharacter. |
|
3994 |
]. |
|
3995 |
newStr at:i put:c |
|
608 | 3996 |
]. |
3997 |
^ newStr |
|
3998 |
||
3999 |
" |
|
8874 | 4000 |
'helloWorld' asUppercase |
4001 |
'helloWorld' asUppercaseFirst |
|
9192 | 4002 |
(Character value:16rB5) asString asUppercase -- needs 16 bits !! |
18657 | 4003 |
(Character value:16rFF) asString asUppercase -- needs 16 bits !! |
9192 | 4004 |
" |
4005 |
! |
|
4006 |
||
4007 |
asUppercaseFirst |
|
8003
6e690f4b79f1
asLowercase/asUppercase and friends care for 16-bit chars
Claus Gittinger <cg@exept.de>
parents:
7995
diff
changeset
|
4008 |
"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
|
4009 |
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
|
4010 |
receiver." |
6e690f4b79f1
asLowercase/asUppercase and friends care for 16-bit chars
Claus Gittinger <cg@exept.de>
parents:
7995
diff
changeset
|
4011 |
|
6e690f4b79f1
asLowercase/asUppercase and friends care for 16-bit chars
Claus Gittinger <cg@exept.de>
parents:
7995
diff
changeset
|
4012 |
|newString firstChar firstCharAsUppercase| |
6e690f4b79f1
asLowercase/asUppercase and friends care for 16-bit chars
Claus Gittinger <cg@exept.de>
parents:
7995
diff
changeset
|
4013 |
|
12679 | 4014 |
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
|
4015 |
firstChar := self at:1. |
8003
6e690f4b79f1
asLowercase/asUppercase and friends care for 16-bit chars
Claus Gittinger <cg@exept.de>
parents:
7995
diff
changeset
|
4016 |
firstCharAsUppercase := firstChar asUppercase. |
6e690f4b79f1
asLowercase/asUppercase and friends care for 16-bit chars
Claus Gittinger <cg@exept.de>
parents:
7995
diff
changeset
|
4017 |
firstChar == firstCharAsUppercase ifTrue:[ ^ self]. |
6e690f4b79f1
asLowercase/asUppercase and friends care for 16-bit chars
Claus Gittinger <cg@exept.de>
parents:
7995
diff
changeset
|
4018 |
|
12679 | 4019 |
"/ 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
|
4020 |
firstCharAsUppercase bitsPerCharacter > self bitsPerCharacter ifTrue:[ |
20012 | 4021 |
newString := firstCharAsUppercase stringSpecies fromString:self. |
8037
8cce25b1059c
care for wide chars in asXXXFirst
Claus Gittinger <cg@exept.de>
parents:
8032
diff
changeset
|
4022 |
] ifFalse:[ |
20012 | 4023 |
newString := self stringSpecies fromString:self. |
8037
8cce25b1059c
care for wide chars in asXXXFirst
Claus Gittinger <cg@exept.de>
parents:
8032
diff
changeset
|
4024 |
]. |
8003
6e690f4b79f1
asLowercase/asUppercase and friends care for 16-bit chars
Claus Gittinger <cg@exept.de>
parents:
7995
diff
changeset
|
4025 |
newString at:1 put:firstCharAsUppercase. |
608 | 4026 |
^ newString |
4027 |
||
4028 |
" |
|
8874 | 4029 |
'helloWorld' asUppercase |
4030 |
'helloWorld' asUppercaseFirst |
|
4031 |
'HelloWorld' asUppercaseFirst |
|
608 | 4032 |
" |
9192 | 4033 |
! |
4034 |
||
11828 | 4035 |
asUppercaseLast |
4036 |
"return a copy of myself where the last character is |
|
4037 |
converted to uppercase." |
|
4038 |
||
4039 |
|newString sz| |
|
4040 |
||
4041 |
sz := self size. |
|
4042 |
newString := self copyFrom:1 to:sz. |
|
4043 |
sz > 0 ifTrue:[ |
|
20012 | 4044 |
newString at:sz put:(newString at:sz) asUppercase |
11828 | 4045 |
]. |
4046 |
^ newString |
|
4047 |
||
4048 |
" |
|
12177 | 4049 |
'HelloWorld' asUppercase |
4050 |
'HelloWorld' asUppercaseLast |
|
11828 | 4051 |
" |
4052 |
! |
|
4053 |
||
17021 | 4054 |
asUtcTimestamp |
4055 |
"convert the receiver into an UtcTimestamp (Time is interpreted in the UTC timezone). |
|
4056 |
Notice, that errors may occur during the read, |
|
4057 |
so you better setup some exception handler when using this method." |
|
4058 |
||
4059 |
^ UtcTimestamp readFromString:self |
|
4060 |
||
4061 |
" |
|
4062 |
'2014-11-10 21:30:22.444' asUtcTimestamp |
|
4063 |
" |
|
4064 |
! |
|
4065 |
||
11062 | 4066 |
literalArrayEncoding |
4067 |
"encode myself as an array literal, from which a copy of the receiver |
|
4068 |
can be reconstructed with #decodeAsLiteralArray." |
|
4069 |
||
4070 |
^ self |
|
4071 |
||
4072 |
"Modified: 1.9.1995 / 02:25:45 / claus" |
|
4073 |
"Modified: 22.4.1996 / 13:00:50 / cg" |
|
4074 |
! |
|
4075 |
||
9192 | 4076 |
string |
4077 |
"return the receiver - for ST-80 compatibility" |
|
4078 |
||
4079 |
^ self |
|
4080 |
||
4081 |
"Modified: 27.4.1996 / 13:29:30 / cg" |
|
4082 |
! ! |
|
4083 |
||
4084 |
!CharacterArray methodsFor:'copying'! |
|
4085 |
||
4086 |
, aStringOrCharacter |
|
2720
98c21d9a07c0
allow mixed strings to be concatenated using comma
Claus Gittinger <cg@exept.de>
parents:
2692
diff
changeset
|
4087 |
"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
|
4088 |
This is nonStandard, but convenient" |
2f14bbd792a9
, now accepts a character-argument
Claus Gittinger <cg@exept.de>
parents:
1028
diff
changeset
|
4089 |
|
2720
98c21d9a07c0
allow mixed strings to be concatenated using comma
Claus Gittinger <cg@exept.de>
parents:
2692
diff
changeset
|
4090 |
|myWidth otherWidth| |
98c21d9a07c0
allow mixed strings to be concatenated using comma
Claus Gittinger <cg@exept.de>
parents:
2692
diff
changeset
|
4091 |
|
1050
2f14bbd792a9
, now accepts a character-argument
Claus Gittinger <cg@exept.de>
parents:
1028
diff
changeset
|
4092 |
aStringOrCharacter isCharacter ifTrue:[ |
20012 | 4093 |
^ self copyWith:aStringOrCharacter |
1050
2f14bbd792a9
, now accepts a character-argument
Claus Gittinger <cg@exept.de>
parents:
1028
diff
changeset
|
4094 |
]. |
1396
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4095 |
aStringOrCharacter isText ifTrue:[ |
20012 | 4096 |
^ aStringOrCharacter concatenateFromString:self |
1396
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4097 |
]. |
2720
98c21d9a07c0
allow mixed strings to be concatenated using comma
Claus Gittinger <cg@exept.de>
parents:
2692
diff
changeset
|
4098 |
aStringOrCharacter isString ifTrue:[ |
20012 | 4099 |
(otherWidth := aStringOrCharacter bitsPerCharacter) ~~ (myWidth := self bitsPerCharacter) ifTrue:[ |
4100 |
otherWidth > myWidth ifTrue:[ |
|
4101 |
^ (aStringOrCharacter species fromString:self) , aStringOrCharacter |
|
4102 |
]. |
|
4103 |
^ self , (self species fromString:aStringOrCharacter) |
|
4104 |
]. |
|
2720
98c21d9a07c0
allow mixed strings to be concatenated using comma
Claus Gittinger <cg@exept.de>
parents:
2692
diff
changeset
|
4105 |
]. |
1050
2f14bbd792a9
, now accepts a character-argument
Claus Gittinger <cg@exept.de>
parents:
1028
diff
changeset
|
4106 |
^ super , aStringOrCharacter |
2f14bbd792a9
, now accepts a character-argument
Claus Gittinger <cg@exept.de>
parents:
1028
diff
changeset
|
4107 |
|
2f14bbd792a9
, now accepts a character-argument
Claus Gittinger <cg@exept.de>
parents:
1028
diff
changeset
|
4108 |
" |
8874 | 4109 |
'hello' , $1 |
4110 |
'hello' , '1' |
|
4111 |
'hello' , (' world' asText allBold) |
|
4112 |
'hello' , (JISEncodedString fromString:' world') |
|
2720
98c21d9a07c0
allow mixed strings to be concatenated using comma
Claus Gittinger <cg@exept.de>
parents:
2692
diff
changeset
|
4113 |
(JISEncodedString fromString:'hello') , ' world' |
1411 | 4114 |
|
1422 | 4115 |
Transcript showCR: |
20012 | 4116 |
(Text string:'hello' emphasis:#italic) , (Text string:' world' emphasis:#bold) |
1050
2f14bbd792a9
, now accepts a character-argument
Claus Gittinger <cg@exept.de>
parents:
1028
diff
changeset
|
4117 |
" |
2f14bbd792a9
, now accepts a character-argument
Claus Gittinger <cg@exept.de>
parents:
1028
diff
changeset
|
4118 |
|
2720
98c21d9a07c0
allow mixed strings to be concatenated using comma
Claus Gittinger <cg@exept.de>
parents:
2692
diff
changeset
|
4119 |
"Modified: 28.6.1997 / 00:13:17 / cg" |
9192 | 4120 |
! |
4121 |
||
4122 |
,, aString |
|
7020 | 4123 |
"concatenate with a newLine in between" |
6965 | 4124 |
|
4125 |
^ (self copyWith:Character cr) , aString |
|
4126 |
||
4127 |
" |
|
4128 |
hello ,, world |
|
7020 | 4129 |
'hello' ,, 'world' |
6965 | 4130 |
" |
4131 |
! |
|
4132 |
||
11329 | 4133 |
chopTo:maxLen |
19464 | 4134 |
"if the receiver's size is less or equal to maxLen, return it. |
11329 | 4135 |
Otherwise, return a copy of the receiver, where some characters |
4136 |
in the middle have been removed for a total string length |
|
4137 |
of maxLen." |
|
4138 |
||
4139 |
|sz n1 n2| |
|
4140 |
||
4141 |
(sz := self size) <= maxLen ifTrue:[ ^ self ]. |
|
4142 |
||
4143 |
n1 := n2 := maxLen // 2. |
|
4144 |
maxLen odd ifTrue:[ |
|
20012 | 4145 |
n2 := n1 + 1 |
11329 | 4146 |
]. |
4147 |
^ (self copyFrom:1 to:n1) , (self copyFrom:sz - n2 + 1) |
|
4148 |
||
4149 |
" |
|
4150 |
'12345678901234' chopTo:15 |
|
4151 |
'123456789012345' chopTo:15 |
|
4152 |
'1234567890123456' chopTo:15 |
|
4153 |
'aShortString' chopTo:15 |
|
4154 |
'aVeryLongNameForAStringThatShouldBeShortened' chopTo:15 |
|
4155 |
" |
|
4156 |
||
4157 |
"Modified: / 24-10-2006 / 12:32:01 / cg" |
|
4158 |
! |
|
4159 |
||
4160 |
contractAtBeginningTo:maxLen |
|
19464 | 4161 |
"if the receiver's size is less or equal to maxLen, return it. |
11329 | 4162 |
Otherwise, return a copy of the receiver, where some characters |
4163 |
at the beginning have been replaced by '...' for a total string length |
|
4164 |
of maxLen. Can be used to abbreviate long entries in tables." |
|
4165 |
||
4166 |
|sz| |
|
4167 |
||
4168 |
(sz := self size) <= maxLen ifTrue:[ ^ self ]. |
|
4169 |
||
4170 |
^ '...' , (self copyFrom:(sz - (maxLen - 4))) |
|
4171 |
||
4172 |
" |
|
4173 |
'12345678901234' contractAtBeginningTo:15 |
|
4174 |
'123456789012345' contractAtBeginningTo:15 |
|
4175 |
'1234567890123456' contractAtBeginningTo:15 |
|
4176 |
'aShortString' contractAtBeginningTo:15 |
|
4177 |
'aVeryLongNameForAStringThatShouldBeShortened' contractAtBeginningTo:15 |
|
4178 |
" |
|
4179 |
||
4180 |
"Modified: / 24-10-2006 / 12:32:13 / cg" |
|
4181 |
! |
|
4182 |
||
4183 |
contractAtEndTo:maxLen |
|
19464 | 4184 |
"if the receiver's size is less or equal to maxLen, return it. |
11329 | 4185 |
Otherwise, return a copy of the receiver, where some characters |
4186 |
at the end have been replaced by '...' for a total string length |
|
4187 |
of maxLen. Can be used to abbreviate long entries in tables." |
|
4188 |
||
4189 |
|sz| |
|
4190 |
||
4191 |
(sz := self size) <= maxLen ifTrue:[ ^ self ]. |
|
4192 |
||
4193 |
^ (self copyTo:maxLen-3),'...' |
|
4194 |
||
4195 |
" |
|
4196 |
'12345678901234' contractAtEndTo:15 |
|
4197 |
'123456789012345' contractAtEndTo:15 |
|
4198 |
'1234567890123456' contractAtEndTo:15 |
|
4199 |
'aShortString' contractAtEndTo:15 |
|
4200 |
'aVeryLongNameForAStringThatShouldBeShortened' contractAtEndTo:15 |
|
4201 |
" |
|
4202 |
||
4203 |
"Modified: / 24-10-2006 / 12:32:26 / cg" |
|
4204 |
! |
|
4205 |
||
4206 |
contractLeftTo:maxLen |
|
19464 | 4207 |
"if the receiver's size is less or equal to maxLen, return it. |
11329 | 4208 |
Otherwise, return a copy of the receiver, where some characters |
4209 |
near the first quarter have been replaced by '...' for a total string length |
|
12177 | 4210 |
of maxLen. |
4211 |
Very similar to contractTo:, but better to abbreviate long filename entries, |
|
11329 | 4212 |
where the right part is of more use than the left." |
4213 |
||
4214 |
|sz "{ SmallInteger }" |
|
4215 |
halfSize quarterSize "{ SmallInteger }" |
|
4216 |
leftEnd rightEnd rightStart| |
|
4217 |
||
4218 |
(sz := self size) <= maxLen ifTrue:[ ^ self ]. |
|
4219 |
||
4220 |
halfSize := maxLen // 2. |
|
4221 |
quarterSize := maxLen // 4. |
|
4222 |
leftEnd := quarterSize-1. |
|
4223 |
rightEnd := maxLen - leftEnd - 3. |
|
4224 |
rightStart := sz - rightEnd + 1. |
|
4225 |
^ (self copyTo:leftEnd),'...',(self copyFrom:rightStart) |
|
4226 |
||
4227 |
" |
|
12177 | 4228 |
'12345678901234' contractLeftTo:15 |
4229 |
'123456789012345' contractLeftTo:15 |
|
4230 |
'1234567890123456' contractLeftTo:15 |
|
4231 |
'aShortString' contractLeftTo:15 |
|
4232 |
'aVeryLongNameForAStringThatShouldBeShortened' contractLeftTo:15 |
|
11329 | 4233 |
'C:\Dokumente und Einstellungen\cg\work\bosch\dapas\hw_schnittstellen\DAPAS__HpibDLL.st' contractLeftTo:40 |
4234 |
" |
|
4235 |
! |
|
4236 |
||
4237 |
contractTo:maxLen |
|
19464 | 4238 |
"if the receiver's size is less or equal to maxLen, return it. |
11329 | 4239 |
Otherwise, return a copy of the receiver, where some characters |
4240 |
in the middle have been replaced by '...' for a total string length |
|
4241 |
of maxLen. Can be used to abbreviate long entries in tables." |
|
4242 |
||
13362 | 4243 |
|sz "{ SmallInteger }" leftSize rightSize| |
11329 | 4244 |
|
4245 |
(sz := self size) <= maxLen ifTrue:[ ^ self ]. |
|
4246 |
||
13362 | 4247 |
rightSize := maxLen // 2. |
4248 |
leftSize := maxLen - rightSize. |
|
13822 | 4249 |
leftSize := leftSize - 2. |
4250 |
rightSize := rightSize - 1. |
|
13362 | 4251 |
^ (self copyTo:leftSize),'...',(self copyFrom:(sz+1-rightSize)) |
4252 |
||
4253 |
" |
|
13882
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
4254 |
'12345678901234' contractTo:15 |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
4255 |
'123456789012345' contractTo:15 |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
4256 |
'1234567890123456' contractTo:15 |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
4257 |
'12345678901234567' contractTo:15 |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
4258 |
'123456789012345678' contractTo:15 |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
4259 |
'aShortString' contractTo:15 |
11329 | 4260 |
'aVeryLongNameForAStringThatShouldBeShortened' contractTo:15 |
4261 |
'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
|
4262 |
('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
|
4263 |
('1234567890123456789012345678901234567' contractTo:29) size |
13362 | 4264 |
" |
4265 |
||
13822 | 4266 |
"Modified (comment): / 24-11-2011 / 19:17:46 / cg" |
11329 | 4267 |
! |
4268 |
||
16202 | 4269 |
copyReplaceAll:oldElement with:newElement |
4270 |
"return a copy of the receiver as a string, where all elements equal to oldElement |
|
4271 |
have been replaced by newElement." |
|
4272 |
||
4273 |
"/ ANSI seems to allow a sequence to be replaced by another sequence, |
|
4274 |
"/ whereas the old ST80 meant replace all occurrences... - sigh. |
|
4275 |
oldElement isByteCollection ifTrue:[ |
|
20012 | 4276 |
newElement isByteCollection ifTrue:[ |
4277 |
^ self copyReplaceString:oldElement withString:newElement. |
|
4278 |
]. |
|
4279 |
self halt:'check if this is legal'. |
|
16202 | 4280 |
]. |
4281 |
newElement isByteCollection ifTrue:[ |
|
20012 | 4282 |
self halt:'check if this is legal'. |
16202 | 4283 |
]. |
16212 | 4284 |
^ super copyReplaceAll:oldElement with:newElement |
16202 | 4285 |
! |
4286 |
||
8347 | 4287 |
copyReplaceString:subString withString:newString |
4288 |
"return a copy of the receiver, with all sequences of subString replaced |
|
4289 |
by newString (i.e. slice in the newString in place of the oldString)." |
|
4290 |
||
4291 |
|tmpStream idx idx1| |
|
4292 |
||
15523 | 4293 |
tmpStream := self species writeStream. |
8347 | 4294 |
idx := 1. |
4295 |
[idx ~~ 0] whileTrue:[ |
|
20012 | 4296 |
idx1 := idx. |
4297 |
idx := self indexOfSubCollection:subString startingAt:idx. |
|
4298 |
idx ~~ 0 ifTrue:[ |
|
4299 |
tmpStream nextPutAll:(self copyFrom:idx1 to:idx-1). |
|
4300 |
tmpStream nextPutAll:newString. |
|
4301 |
idx := idx + subString size |
|
4302 |
] |
|
8347 | 4303 |
]. |
4304 |
tmpStream nextPutAll:(self copyFrom:idx1). |
|
4305 |
^ tmpStream contents |
|
4306 |
||
4307 |
" |
|
9306 | 4308 |
'12345678901234567890' copyReplaceString:'123' withString:'OneTwoThree' |
4309 |
'12345678901234567890' copyReplaceString:'123' withString:'*' |
|
4310 |
'12345678901234567890' copyReplaceString:'234' withString:'foo' |
|
4311 |
||
4312 |
('a string with spaces' copyReplaceAll:$ withAll:' foo ') |
|
20012 | 4313 |
copyReplaceString:'foo' withString:'bar' |
8347 | 4314 |
" |
4315 |
||
4316 |
"Modified: / 31-05-1999 / 12:33:59 / cg" |
|
4317 |
"Created: / 12-05-2004 / 12:00:00 / cg" |
|
4318 |
! |
|
4319 |
||
8040
f10cca439066
copyWith for mixed-width string/character
Claus Gittinger <cg@exept.de>
parents:
8037
diff
changeset
|
4320 |
copyWith:aCharacter |
19464 | 4321 |
"return a new string containing the receiver's characters |
8874 | 4322 |
and the single new character, aCharacter. |
8040
f10cca439066
copyWith for mixed-width string/character
Claus Gittinger <cg@exept.de>
parents:
8037
diff
changeset
|
4323 |
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
|
4324 |
as argument, but equivalent to copy-and-addLast. |
8874 | 4325 |
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
|
4326 |
(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
|
4327 |
|
f10cca439066
copyWith for mixed-width string/character
Claus Gittinger <cg@exept.de>
parents:
8037
diff
changeset
|
4328 |
|sz newString| |
f10cca439066
copyWith for mixed-width string/character
Claus Gittinger <cg@exept.de>
parents:
8037
diff
changeset
|
4329 |
|
f10cca439066
copyWith for mixed-width string/character
Claus Gittinger <cg@exept.de>
parents:
8037
diff
changeset
|
4330 |
aCharacter bitsPerCharacter > self bitsPerCharacter ifTrue:[ |
20012 | 4331 |
sz := self size. |
4332 |
newString := aCharacter stringSpecies new:sz + 1. |
|
4333 |
newString replaceFrom:1 to:sz with:self startingAt:1. |
|
4334 |
newString at:sz+1 put:aCharacter. |
|
4335 |
^ newString. |
|
8040
f10cca439066
copyWith for mixed-width string/character
Claus Gittinger <cg@exept.de>
parents:
8037
diff
changeset
|
4336 |
]. |
f10cca439066
copyWith for mixed-width string/character
Claus Gittinger <cg@exept.de>
parents:
8037
diff
changeset
|
4337 |
^ super copyWith:aCharacter |
17241 | 4338 |
! |
4339 |
||
4340 |
restAfter:keyword withoutSeparators:strip |
|
4341 |
"compare the left of the receiver with keyword, |
|
4342 |
if it matches return the right. |
|
4343 |
Finally, if strip is true, remove whiteSpace. |
|
4344 |
This method is used to match and extract lines of the form: |
|
20012 | 4345 |
something: rest |
17241 | 4346 |
where we are interested in rest, but only if the receiver string |
4347 |
begins with something. |
|
4348 |
||
4349 |
You may wonder why such a specialized method exists here |
|
4350 |
- this is so common when processing mailboxes, |
|
4351 |
rcs files, nntp/pop3 responses, that is was considered worth |
|
4352 |
a special method here to avoid having the code below a hundred |
|
4353 |
times in variuos places." |
|
4354 |
||
4355 |
|rest| |
|
4356 |
||
4357 |
(self startsWith:keyword) ifTrue:[ |
|
20012 | 4358 |
rest := self copyFrom:(keyword size + 1). |
4359 |
strip ifTrue:[ |
|
4360 |
rest := rest withoutSeparators |
|
4361 |
]. |
|
4362 |
^ rest |
|
17241 | 4363 |
]. |
4364 |
^ nil |
|
4365 |
||
4366 |
" |
|
4367 |
'foo: hello world' restAfter:'foo:' withoutSeparators:true |
|
4368 |
'funny: something' restAfter:'foo:' withoutSeparators:true |
|
4369 |
||
4370 |
'foo: hello world ' restAfter:'foo:' withoutSeparators:true |
|
4371 |
'foo: hello world ' restAfter:'foo:' withoutSeparators:false |
|
4372 |
" |
|
4373 |
||
4374 |
"Created: 25.11.1995 / 11:04:18 / cg" |
|
4375 |
! |
|
4376 |
||
4377 |
splitAtString:subString withoutSeparators:strip |
|
4378 |
"If the receiver is of the form: |
|
20012 | 4379 |
<left><subString><right> |
17241 | 4380 |
return a collection containing left and right only. |
4381 |
If strip is true, remove whiteSpace in the returned substrings." |
|
4382 |
||
4383 |
|idx left right| |
|
4384 |
||
4385 |
(idx := self indexOfSubCollection:subString) ~~ 0 ifTrue:[ |
|
20012 | 4386 |
left := self copyTo:(idx - 1). |
4387 |
right := self copyFrom:(idx + subString size). |
|
4388 |
strip ifTrue:[ |
|
4389 |
left := left withoutSeparators. |
|
4390 |
right := right withoutSeparators. |
|
4391 |
]. |
|
4392 |
^ StringCollection with:left with:right |
|
17241 | 4393 |
]. |
4394 |
self error:'substring not present in receiver' mayProceed:true. |
|
4395 |
^ self |
|
4396 |
||
4397 |
" |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
4398 |
'hello -> world' splitAtString:'->' withoutSeparators:false |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
4399 |
'hello -> world' splitAtString:'->' withoutSeparators:true |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
4400 |
'hello -> ' splitAtString:'->' withoutSeparators:true |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
4401 |
'hello > error' splitAtString:'->' withoutSeparators:true |
17241 | 4402 |
" |
4403 |
||
4404 |
"Created: 25.11.1995 / 11:04:18 / cg" |
|
608 | 4405 |
! ! |
4406 |
||
4407 |
!CharacterArray methodsFor:'displaying'! |
|
4408 |
||
2762 | 4409 |
displayOn:aGC x:x y:y from:start to:stop |
4410 |
"display the receiver on a GC" |
|
4411 |
||
12171 | 4412 |
self displayOn:aGC x:x y:y from:start to:stop opaque:false |
4413 |
||
4414 |
"Modified: 12.5.1996 / 12:49:33 / cg" |
|
4415 |
! |
|
4416 |
||
4417 |
displayOn:aGC x:x y:y from:start to:stop opaque:opaque |
|
4418 |
"display the receiver on a GC" |
|
4419 |
||
2762 | 4420 |
"q&d hack" |
4421 |
||
12177 | 4422 |
(start == 1 and:[stop == self size]) ifTrue:[ |
20012 | 4423 |
self displayOn:aGC x:x y:y opaque:opaque. |
12177 | 4424 |
] ifFalse:[ |
20012 | 4425 |
(self copyFrom:start to:stop) displayOn:aGC x:x y:y opaque:opaque. |
12177 | 4426 |
]. |
2762 | 4427 |
! |
4428 |
||
7082 | 4429 |
displayOn:aGc x:x y:y opaque:opaque |
2762 | 4430 |
"display the receiver in a graphicsContext - this method allows |
4431 |
strings to be used like DisplayObjects." |
|
4432 |
||
4433 |
|s| |
|
4434 |
||
4435 |
s := self string. |
|
7082 | 4436 |
opaque ifTrue:[ |
20012 | 4437 |
aGc displayOpaqueString:s x:x y:y. |
2762 | 4438 |
] ifFalse:[ |
20012 | 4439 |
aGc displayString:s x:x y:y. |
2762 | 4440 |
]. |
1381 | 4441 |
|
4442 |
"Modified: 11.5.1996 / 14:42:48 / cg" |
|
4443 |
! |
|
4444 |
||
2762 | 4445 |
displayOpaqueOn:aGC x:x y:y from:start to:stop |
4446 |
"display the receiver on a GC" |
|
4447 |
||
12171 | 4448 |
self displayOn:aGC x:x y:y from:start to:stop opaque:true |
608 | 4449 |
! ! |
4450 |
||
1375
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
4451 |
!CharacterArray methodsFor:'emphasis'! |
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
4452 |
|
11632
b82e9f3cabd1
new: #actionForAll: #emphasisAtPoint:on:
Stefan Vogel <sv@exept.de>
parents:
11630
diff
changeset
|
4453 |
actionForAll:aBlock |
12326
c67f12213ddb
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
12319
diff
changeset
|
4454 |
"change the action block of all characters. |
c67f12213ddb
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
12319
diff
changeset
|
4455 |
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
|
4456 |
|
b82e9f3cabd1
new: #actionForAll: #emphasisAtPoint:on:
Stefan Vogel <sv@exept.de>
parents:
11630
diff
changeset
|
4457 |
^ self asText actionForAll:aBlock |
b82e9f3cabd1
new: #actionForAll: #emphasisAtPoint:on:
Stefan Vogel <sv@exept.de>
parents:
11630
diff
changeset
|
4458 |
! |
b82e9f3cabd1
new: #actionForAll: #emphasisAtPoint:on:
Stefan Vogel <sv@exept.de>
parents:
11630
diff
changeset
|
4459 |
|
5732 | 4460 |
allBold |
12326
c67f12213ddb
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
12319
diff
changeset
|
4461 |
"return a text object representing the receiver, but all boldified" |
5732 | 4462 |
|
8874 | 4463 |
"this test allows for small non-gui apps to be built without libbasic2 (where Text is)" |
6187 | 4464 |
Text isNil ifTrue:[^ self]. |
5732 | 4465 |
^ self asText allBold |
4466 |
||
4467 |
" |
|
4468 |
Transcript showCR:'hello' asText allBold |
|
4469 |
Transcript showCR:'hello' allBold |
|
4470 |
" |
|
4471 |
! |
|
4472 |
||
5898
7bc4f1593b4e
allItalic now understood by CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5848
diff
changeset
|
4473 |
allItalic |
12326
c67f12213ddb
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
12319
diff
changeset
|
4474 |
"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
|
4475 |
|
8874 | 4476 |
"this test allows for small non-gui apps to be built without libbasic2 (where Text is)" |
6187 | 4477 |
Text isNil ifTrue:[^ self]. |
5898
7bc4f1593b4e
allItalic now understood by CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5848
diff
changeset
|
4478 |
^ self asText allItalic |
7bc4f1593b4e
allItalic now understood by CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5848
diff
changeset
|
4479 |
|
7bc4f1593b4e
allItalic now understood by CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5848
diff
changeset
|
4480 |
" |
7bc4f1593b4e
allItalic now understood by CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5848
diff
changeset
|
4481 |
Transcript showCR:'hello' asText allItalic |
7bc4f1593b4e
allItalic now understood by CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5848
diff
changeset
|
4482 |
Transcript showCR:'hello' allItalic |
7bc4f1593b4e
allItalic now understood by CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5848
diff
changeset
|
4483 |
" |
7bc4f1593b4e
allItalic now understood by CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5848
diff
changeset
|
4484 |
! |
7bc4f1593b4e
allItalic now understood by CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5848
diff
changeset
|
4485 |
|
11345 | 4486 |
allStrikedOut |
12326
c67f12213ddb
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
12319
diff
changeset
|
4487 |
"return a text object representing the receiver, but all in strikeout" |
11345 | 4488 |
|
4489 |
"this test allows for small non-gui apps to be built without libbasic2 (where Text is)" |
|
4490 |
Text isNil ifTrue:[^ self]. |
|
4491 |
^ self asText allStrikedOut |
|
4492 |
||
4493 |
" |
|
4494 |
Transcript showCR:'hello' asText allStrikedOut |
|
4495 |
Transcript showCR:'hello' allStrikedOut |
|
4496 |
" |
|
4497 |
! |
|
4498 |
||
10862 | 4499 |
allUnderlined |
12326
c67f12213ddb
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
12319
diff
changeset
|
4500 |
"return a text object representing the receiver, but all with underline" |
10862 | 4501 |
|
4502 |
"this test allows for small non-gui apps to be built without libbasic2 (where Text is)" |
|
4503 |
Text isNil ifTrue:[^ self]. |
|
4504 |
^ self asText allUnderlined |
|
4505 |
||
4506 |
" |
|
4507 |
Transcript showCR:'hello' asText allUnderlined |
|
4508 |
Transcript showCR:'hello' allUnderlined |
|
4509 |
" |
|
4510 |
! |
|
4511 |
||
19774 | 4512 |
asActionLinkTo:aBlock |
4513 |
"change the action block of all characters and colorize as a link. |
|
4514 |
Some widgets use this like a href if clicked onto the text |
|
4515 |
(for example, the system-browser's info at the bottom is such a widget)." |
|
4516 |
||
4517 |
"/ the blue may become a user-setting some time later.. |
|
4518 |
^ (self actionForAll:aBlock) withColor:(Color blue) |
|
4519 |
! |
|
4520 |
||
6228 | 4521 |
colorizeAllWith:aColor |
12326
c67f12213ddb
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
12319
diff
changeset
|
4522 |
"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
|
4523 |
|
c67f12213ddb
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
12319
diff
changeset
|
4524 |
"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
|
4525 |
Text isNil ifTrue:[^ self]. |
6228 | 4526 |
^ self asText colorizeAllWith:aColor |
4527 |
||
4528 |
" |
|
4529 |
Transcript showCR:('hello' colorizeAllWith:Color red) |
|
4530 |
Transcript showCR:('world' colorizeAllWith:Color green darkened) |
|
4531 |
" |
|
4532 |
! |
|
4533 |
||
12330
f9b57b0b8a8b
added: #colorizeAllWith:on:
Claus Gittinger <cg@exept.de>
parents:
12326
diff
changeset
|
4534 |
colorizeAllWith:fgColor on:bgColor |
f9b57b0b8a8b
added: #colorizeAllWith:on:
Claus Gittinger <cg@exept.de>
parents:
12326
diff
changeset
|
4535 |
"return a text object representing the receiver, but all colorized with |
f9b57b0b8a8b
added: #colorizeAllWith:on:
Claus Gittinger <cg@exept.de>
parents:
12326
diff
changeset
|
4536 |
both fg and background color" |
f9b57b0b8a8b
added: #colorizeAllWith:on:
Claus Gittinger <cg@exept.de>
parents:
12326
diff
changeset
|
4537 |
|
f9b57b0b8a8b
added: #colorizeAllWith:on:
Claus Gittinger <cg@exept.de>
parents:
12326
diff
changeset
|
4538 |
"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
|
4539 |
Text isNil ifTrue:[^ self]. |
f9b57b0b8a8b
added: #colorizeAllWith:on:
Claus Gittinger <cg@exept.de>
parents:
12326
diff
changeset
|
4540 |
^ self asText colorizeAllWith:fgColor on:bgColor |
f9b57b0b8a8b
added: #colorizeAllWith:on:
Claus Gittinger <cg@exept.de>
parents:
12326
diff
changeset
|
4541 |
|
f9b57b0b8a8b
added: #colorizeAllWith:on:
Claus Gittinger <cg@exept.de>
parents:
12326
diff
changeset
|
4542 |
" |
f9b57b0b8a8b
added: #colorizeAllWith:on:
Claus Gittinger <cg@exept.de>
parents:
12326
diff
changeset
|
4543 |
Transcript showCR:('hello' colorizeAllWith:Color red on:Color yellow) |
f9b57b0b8a8b
added: #colorizeAllWith:on:
Claus Gittinger <cg@exept.de>
parents:
12326
diff
changeset
|
4544 |
Transcript showCR:('world' colorizeAllWith:Color red) |
f9b57b0b8a8b
added: #colorizeAllWith:on:
Claus Gittinger <cg@exept.de>
parents:
12326
diff
changeset
|
4545 |
" |
f9b57b0b8a8b
added: #colorizeAllWith:on:
Claus Gittinger <cg@exept.de>
parents:
12326
diff
changeset
|
4546 |
! |
f9b57b0b8a8b
added: #colorizeAllWith:on:
Claus Gittinger <cg@exept.de>
parents:
12326
diff
changeset
|
4547 |
|
1396
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4548 |
emphasis |
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4549 |
"return the emphasis. |
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4550 |
Since characterArrays do not hold any emphasis information, |
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4551 |
nil (no emphasis) is returned here." |
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4552 |
|
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4553 |
^ RunArray new:self size withAll:nil |
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4554 |
|
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4555 |
"Created: 14.5.1996 / 13:58:58 / cg" |
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4556 |
! |
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4557 |
|
6655 | 4558 |
emphasis:emphasisCollection |
4559 |
^ self asText emphasis:emphasisCollection |
|
4560 |
||
4561 |
" |
|
4562 |
Transcript showCR:('hello' emphasis:#(bold bold bold bold bold)) |
|
4563 |
" |
|
4564 |
! |
|
4565 |
||
1375
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
4566 |
emphasisAt:characterIndex |
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
4567 |
"return the emphasis at some index. |
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
4568 |
Since characterArrays do not hold any emphasis information, |
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
4569 |
nil (no emphasis) is returned here." |
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
4570 |
|
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
4571 |
^ nil |
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
4572 |
|
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
4573 |
"Created: 11.5.1996 / 14:13:27 / cg" |
1396
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4574 |
! |
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4575 |
|
11632
b82e9f3cabd1
new: #actionForAll: #emphasisAtPoint:on:
Stefan Vogel <sv@exept.de>
parents:
11630
diff
changeset
|
4576 |
emphasisAtPoint:aPoint on:aGC |
b82e9f3cabd1
new: #actionForAll: #emphasisAtPoint:on:
Stefan Vogel <sv@exept.de>
parents:
11630
diff
changeset
|
4577 |
"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
|
4578 |
|
b82e9f3cabd1
new: #actionForAll: #emphasisAtPoint:on:
Stefan Vogel <sv@exept.de>
parents:
11630
diff
changeset
|
4579 |
^ nil |
b82e9f3cabd1
new: #actionForAll: #emphasisAtPoint:on:
Stefan Vogel <sv@exept.de>
parents:
11630
diff
changeset
|
4580 |
! |
b82e9f3cabd1
new: #actionForAll: #emphasisAtPoint:on:
Stefan Vogel <sv@exept.de>
parents:
11630
diff
changeset
|
4581 |
|
1396
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4582 |
emphasisCollection |
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4583 |
"return the emphasis. |
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4584 |
Since characterArrays do not hold any emphasis information, |
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4585 |
nil (no emphasis) is returned here." |
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4586 |
|
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4587 |
^ RunArray new:(self size) |
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4588 |
|
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4589 |
"Created: 14.5.1996 / 13:58:58 / cg" |
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4590 |
"Modified: 14.5.1996 / 15:02:29 / cg" |
3404 | 4591 |
! |
4592 |
||
11919 | 4593 |
emphasiseFrom:start to:stop with:newEmphasis |
4594 |
"set to the emphasis within some range. return the receiver" |
|
4595 |
||
4596 |
^ self asText emphasiseFrom:start to:stop with:newEmphasis |
|
4597 |
||
4598 |
" |
|
12177 | 4599 |
'hello' emphasiseFrom:2 with:#italic |
11919 | 4600 |
|
4601 |
Transcript showCR:('hello' emphasiseFrom:2 with:#italic) |
|
4602 |
" |
|
4603 |
! |
|
4604 |
||
4605 |
emphasiseFrom:start with:newEmphasis |
|
4606 |
"set to the emphasis within some range. return the receiver" |
|
4607 |
||
4608 |
^ self emphasiseFrom:start to:(self size) with:newEmphasis |
|
4609 |
||
4610 |
" |
|
12177 | 4611 |
'hello' emphasiseFrom:2 with:#italic |
11919 | 4612 |
|
4613 |
Transcript showCR:('hello' emphasiseFrom:2 with:#italic) |
|
4614 |
" |
|
4615 |
! |
|
4616 |
||
3404 | 4617 |
emphasizeAllWith:emphasis |
12326
c67f12213ddb
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
12319
diff
changeset
|
4618 |
"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
|
4619 |
|
c67f12213ddb
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
12319
diff
changeset
|
4620 |
"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
|
4621 |
Text isNil ifTrue:[^ self]. |
3404 | 4622 |
^ self asText emphasizeAllWith:emphasis |
4623 |
||
4624 |
" |
|
4625 |
Transcript showCR:('hello' emphasizeAllWith:#bold) |
|
3599 | 4626 |
Transcript showCR:('hello' emphasizeAllWith:(#color -> Color red)) |
4627 |
Transcript showCR:('hello' emphasizeAllWith:(#color -> Color red)) |
|
4628 |
" |
|
4629 |
||
4630 |
"Modified: / 17.6.1998 / 12:51:44 / cg" |
|
5097 | 4631 |
! |
4632 |
||
4633 |
makeSelectorBoldIn:aClass |
|
14050 | 4634 |
"the receiver represents some source code for a method in aClass. |
5097 | 4635 |
Change myself to boldify the selector. |
14050 | 4636 |
Not yet implemented (could easily use the syntaxHighlighter for this ...). |
4637 |
For protocol compatibility with other smalltalks" |
|
5097 | 4638 |
|
4639 |
^ self |
|
4640 |
||
14050 | 4641 |
"Created: / 13-12-1999 / 21:49:24 / cg" |
4642 |
! |
|
4643 |
||
19762 | 4644 |
withColor:aColorOrColorSymbol |
20697 | 4645 |
"return a text object representing the receiver, but all colorized. |
4646 |
Usage of a colorSymbol is considered bad style (provided for backward compatibility); |
|
4647 |
please change to pass a proper color |
|
4648 |
(makes it easier to find color uses)" |
|
4649 |
||
4650 |
|color| |
|
4651 |
||
19762 | 4652 |
aColorOrColorSymbol isSymbol ifTrue:[ |
20697 | 4653 |
color := (Color perform:aColorOrColorSymbol) |
4654 |
] ifFalse:[ |
|
4655 |
color := aColorOrColorSymbol |
|
4656 |
]. |
|
4657 |
^ self colorizeAllWith:color |
|
19762 | 4658 |
|
4659 |
" |
|
4660 |
Transcript showCR:('hello' withColor:#red) |
|
20697 | 4661 |
Transcript showCR:('world' withColor:#blue) |
16697 | 4662 |
Transcript showCR:('hello' withColor:Color red) |
4663 |
Transcript showCR:('world' withColor:Color green darkened) |
|
4664 |
" |
|
4665 |
! |
|
4666 |
||
14050 | 4667 |
withoutAnyColorEmphasis |
4668 |
"for protocol compatibility with Text" |
|
4669 |
||
4670 |
^ self |
|
4671 |
||
4672 |
"Created: / 06-03-2012 / 18:15:38 / cg" |
|
6653 | 4673 |
! |
4674 |
||
12396 | 4675 |
withoutBackgroundColorEmphasis |
14050 | 4676 |
"for protocol compatibility with Text" |
4677 |
||
12396 | 4678 |
^ self |
14050 | 4679 |
|
4680 |
"Modified (comment): / 06-03-2012 / 18:14:27 / cg" |
|
12396 | 4681 |
! |
4682 |
||
6653 | 4683 |
withoutEmphasis:emphasisToRemove |
14050 | 4684 |
"for protocol compatibility with Text" |
4685 |
||
6653 | 4686 |
^ self |
14050 | 4687 |
|
4688 |
"Modified (comment): / 06-03-2012 / 18:14:29 / cg" |
|
12396 | 4689 |
! |
4690 |
||
4691 |
withoutForegroundColorEmphasis |
|
14050 | 4692 |
"for protocol compatibility with Text" |
4693 |
||
12396 | 4694 |
^ self |
14050 | 4695 |
|
4696 |
"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
|
4697 |
! ! |
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
4698 |
|
7265 | 4699 |
!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
|
4700 |
|
20013 | 4701 |
asDenormalizedUnicodeString |
4702 |
"return a new string containing the same characters, as a denormalized Unicode string. |
|
4703 |
This replaces diacritical chars (umlauts, accented chars etc) by |
|
4704 |
a sequence with combination characters. |
|
4705 |
(i.e. a plain character followed by a combining diacritical in the 0x03xx range)" |
|
4706 |
||
20014 | 4707 |
|map outStream mapChar| |
20013 | 4708 |
|
4709 |
map := self class unicodeDenormalizationMap. |
|
4710 |
||
4711 |
self containsNon7BitAscii ifFalse:[^ self]. "/ I cannot contain any |
|
4712 |
||
20014 | 4713 |
mapChar := |
4714 |
[:char | |
|
4715 |
|mappedChars| |
|
4716 |
||
4717 |
(mappedChars := map at:char ifAbsent:nil) notNil ifTrue:[ |
|
4718 |
mappedChars do:mapChar. |
|
4719 |
] ifFalse:[ |
|
4720 |
outStream nextPut:char. |
|
4721 |
]. |
|
4722 |
]. |
|
20013 | 4723 |
|
20014 | 4724 |
outStream := WriteStream on:(Unicode16String new:self size). |
4725 |
self do:mapChar. |
|
20013 | 4726 |
^ outStream contents asSingleByteStringIfPossible. |
4727 |
||
4728 |
" |
|
4729 |
'Ö' asDenormalizedUnicodeString |
|
4730 |
'aÖÄx' asDenormalizedUnicodeString |
|
4731 |
'abc' asDenormalizedUnicodeString |
|
20014 | 4732 |
'ṩ' asString asDenormalizedUnicodeString |
20013 | 4733 |
" |
4734 |
! |
|
4735 |
||
20011 | 4736 |
asNormalizedUnicodeString |
20010 | 4737 |
"return a new string containing the same characters, as a normalized Unicode string. |
4738 |
This replaces combination characters by corresponding single characters. |
|
4739 |
(i.e. diaresis and other combining diacriticals in the 0x03xx range)" |
|
4740 |
||
4741 |
|outStream prevChar map mapEntries mappedChar| |
|
4742 |
||
4743 |
map := self class unicodeNormalizationMap. |
|
20017 | 4744 |
|
20010 | 4745 |
self bitsPerCharacter <= 8 ifTrue:[^ self]. "/ I cannot contain any overtypes |
20017 | 4746 |
(self contains:[:ch | ch unicodeBlock == #'COMBINING_DIACRITICAL_MARKS']) ifFalse:[^ self]. "/ I do not contain any overtypes |
20010 | 4747 |
|
4748 |
outStream := WriteStream on:(self species new:self size). |
|
4749 |
self do:[:char | |
|
20017 | 4750 |
((char unicodeBlock == #'COMBINING_DIACRITICAL_MARKS') |
20010 | 4751 |
and:[ (mapEntries := map at:char ifAbsent:nil) notNil |
4752 |
and:[ (mappedChar := mapEntries at:prevChar ifAbsent:nil) notNil ]]) ifTrue:[ |
|
20014 | 4753 |
prevChar := mappedChar. |
20010 | 4754 |
] ifFalse:[ |
4755 |
prevChar notNil ifTrue:[ outStream nextPut:prevChar]. |
|
4756 |
prevChar := char |
|
4757 |
]. |
|
4758 |
]. |
|
20011 | 4759 |
prevChar notNil ifTrue:[ outStream nextPut:prevChar]. |
20010 | 4760 |
^ outStream contents asSingleByteStringIfPossible. |
4761 |
||
4762 |
" |
|
4763 |
self unicodeNormalizationMap |
|
20012 | 4764 |
('O' , (Character value:16r0308)) asNormalizedUnicodeString -> 'Ö' |
20010 | 4765 |
('O' , (Character value:16r0308) , |
20012 | 4766 |
'A' , (Character value:16r0308)) asNormalizedUnicodeString -> 'ÖÄ' |
20014 | 4767 |
('s' , (Character value:16r0323) , (Character value:16r0307)) asNormalizedUnicodeString -> 'ṩ' |
4768 |
('s' , (Character value:16r0307) , (Character value:16r0323)) asNormalizedUnicodeString -> 'ṩ' |
|
20010 | 4769 |
" |
4770 |
! |
|
4771 |
||
12767
17d2c7268065
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12679
diff
changeset
|
4772 |
decodeAsLiteralArray |
17d2c7268065
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12679
diff
changeset
|
4773 |
"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
|
4774 |
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
|
4775 |
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
|
4776 |
|
17d2c7268065
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12679
diff
changeset
|
4777 |
^ self |
17d2c7268065
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12679
diff
changeset
|
4778 |
! |
17d2c7268065
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12679
diff
changeset
|
4779 |
|
991
f0b45a4b82d2
first attempt in encoding/decoding for different character representations (mac/msdos etc.)
Claus Gittinger <cg@exept.de>
parents:
986
diff
changeset
|
4780 |
decodeFrom:encodingSymbol |
8874 | 4781 |
"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
|
4782 |
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
|
4783 |
|
ca774158b317
code cleanup (removed old obsolete encoding stuff)
Claus Gittinger <cg@exept.de>
parents:
8102
diff
changeset
|
4784 |
|myEncoding encoder| |
7950 | 4785 |
|
4786 |
encodingSymbol isNil ifTrue:[^ self]. |
|
4787 |
myEncoding := self encoding. |
|
4788 |
encodingSymbol == myEncoding ifTrue:[^ self]. |
|
4789 |
||
4790 |
encoder := CharacterEncoder encoderToEncodeFrom:(self encoding) into:encodingSymbol. |
|
8124
ca774158b317
code cleanup (removed old obsolete encoding stuff)
Claus Gittinger <cg@exept.de>
parents:
8102
diff
changeset
|
4791 |
encoder isNil ifTrue:[^ self]. |
ca774158b317
code cleanup (removed old obsolete encoding stuff)
Claus Gittinger <cg@exept.de>
parents:
8102
diff
changeset
|
4792 |
^ encoder decodeString:self. |
991
f0b45a4b82d2
first attempt in encoding/decoding for different character representations (mac/msdos etc.)
Claus Gittinger <cg@exept.de>
parents:
986
diff
changeset
|
4793 |
! |
f0b45a4b82d2
first attempt in encoding/decoding for different character representations (mac/msdos etc.)
Claus Gittinger <cg@exept.de>
parents:
986
diff
changeset
|
4794 |
|
9192 | 4795 |
encodeFrom:oldEncoding into:newEncoding |
4796 |
^ CharacterEncoder encodeString:self from:oldEncoding into:newEncoding |
|
14405
1d0d99a95bba
comment/format in:7 methods
Claus Gittinger <cg@exept.de>
parents:
14168
diff
changeset
|
4797 |
|
1d0d99a95bba
comment/format in:7 methods
Claus Gittinger <cg@exept.de>
parents:
14168
diff
changeset
|
4798 |
" |
17600 | 4799 |
'äüö' encodeFrom:#iso8859 into:#utf8 |
4800 |
('äüö' encodeFrom:#iso8859 into:#utf8) encodeFrom:#utf8 into:#unicode |
|
14405
1d0d99a95bba
comment/format in:7 methods
Claus Gittinger <cg@exept.de>
parents:
14168
diff
changeset
|
4801 |
" |
9192 | 4802 |
! |
4803 |
||
4804 |
rot13 |
|
8874 | 4805 |
"Usenet: from `rotate alphabet 13 places'] |
6835
bdb2cb03200c
added utf8Decode with 8-bit chars only
Claus Gittinger <cg@exept.de>
parents:
6834
diff
changeset
|
4806 |
The simple Caesar-cypher encryption that replaces each English |
8874 | 4807 |
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
|
4808 |
so that 'The butler did it!!' becomes 'Gur ohgyre qvq vg!!' |
8874 | 4809 |
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
|
4810 |
It is used to enclose the text in a sealed wrapper that the reader must choose |
8874 | 4811 |
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
|
4812 |
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
|
4813 |
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
|
4814 |
|
11865 | 4815 |
^ self rot:13 |
8874 | 4816 |
|
4817 |
" |
|
4818 |
'hello world' rot13 |
|
4819 |
'hello world' rot13 rot13 |
|
6835
bdb2cb03200c
added utf8Decode with 8-bit chars only
Claus Gittinger <cg@exept.de>
parents:
6834
diff
changeset
|
4820 |
" |
bdb2cb03200c
added utf8Decode with 8-bit chars only
Claus Gittinger <cg@exept.de>
parents:
6834
diff
changeset
|
4821 |
! |
bdb2cb03200c
added utf8Decode with 8-bit chars only
Claus Gittinger <cg@exept.de>
parents:
6834
diff
changeset
|
4822 |
|
11865 | 4823 |
rot:n |
4824 |
"Usenet: from `rotate alphabet N places'] |
|
4825 |
The simple Caesar-cypher encryption that replaces each English |
|
4826 |
letter with the one N places forward or back along the alphabet, |
|
4827 |
so that 'The butler did it!!' becomes 'Gur ohgyre qvq vg!!' by rot 13 |
|
4828 |
Most Usenet news reading and posting programs include a rot13 feature. |
|
4829 |
It is used to enclose the text in a sealed wrapper that the reader must choose |
|
4830 |
to open -- e.g., for posting things that might offend some readers, or spoilers. |
|
4831 |
A major advantage of rot13 over rot(N) for other N is that it |
|
4832 |
is self-inverse, so the same code can be used for encoding and decoding." |
|
4833 |
||
4834 |
^ self species |
|
20012 | 4835 |
streamContents:[:aStream | |
4836 |
self do:[:char | |
|
4837 |
aStream nextPut:(char rot:n) ]] |
|
11865 | 4838 |
|
4839 |
" |
|
4840 |
'hello world' rot:13 |
|
4841 |
('hello world' rot:13) rot:13 |
|
4842 |
" |
|
4843 |
! |
|
4844 |
||
15656 | 4845 |
utf16Encoded |
4846 |
"Return my UTF-16 representation as a new TwoByteString" |
|
4847 |
||
4848 |
|s| |
|
4849 |
||
4850 |
s := WriteStream on:(TwoByteString uninitializedNew:self size). |
|
4851 |
s nextPutAllUtf16:self. |
|
4852 |
^ s contents |
|
4853 |
||
4854 |
" |
|
4855 |
'abcde1234' utf16Encoded |
|
17600 | 4856 |
'abcdeäöüß' utf16Encoded |
15656 | 4857 |
" |
4858 |
||
4859 |
"Modified: / 11-05-2010 / 19:12:37 / cg" |
|
4860 |
! |
|
4861 |
||
9192 | 4862 |
utf8Encoded |
19837 | 4863 |
"return the UTF-8 representation of a Unicode string. |
4864 |
The resulting string is only useful to be stored on some external file, |
|
4865 |
not for being used inside ST/X." |
|
4866 |
||
4867 |
|string stream| |
|
17625 | 4868 |
|
4869 |
string := self string. |
|
19837 | 4870 |
|
4871 |
"/ avoid creation of new strings if possible |
|
4872 |
string containsNon7BitAscii ifFalse:[ |
|
4873 |
^ string asSingleByteString |
|
4874 |
]. |
|
4875 |
||
4876 |
"make it size 2 for 1-byte strings" |
|
4877 |
stream := WriteStream on:(String uninitializedNew:(1 + (string size * 3 // 2))). |
|
4878 |
string utf8EncodedOn:stream. |
|
4879 |
^ stream contents. |
|
4880 |
||
4881 |
" |
|
4882 |
'hello' utf8Encoded asByteArray #[104 101 108 108 111] |
|
4883 |
(Character value:16r40) asString utf8Encoded asByteArray #[64] |
|
4884 |
(Character value:16r7F) asString utf8Encoded asByteArray #[127] |
|
4885 |
(Character value:16r80) asString utf8Encoded asByteArray #[194 128] |
|
4886 |
(Character value:16rFF) asString utf8Encoded asByteArray #[195 191] |
|
4887 |
(Character value:16r100) asString utf8Encoded asByteArray #[196 128] |
|
4888 |
(Character value:16r200) asString utf8Encoded asByteArray #[200 128] |
|
4889 |
(Character value:16r400) asString utf8Encoded asByteArray #[208 128] |
|
4890 |
(Character value:16r800) asString utf8Encoded asByteArray #[224 160 128] |
|
4891 |
(Character value:16r1000) asString utf8Encoded asByteArray #[225 128 128] |
|
4892 |
(Character value:16r2000) asString utf8Encoded asByteArray #[226 128 128] |
|
4893 |
(Character value:16r4000) asString utf8Encoded asByteArray #[228 128 128] |
|
4894 |
(Character value:16r8000) asString utf8Encoded asByteArray #[232 128 128] |
|
4895 |
(Character value:16rFFFF) asString utf8Encoded asByteArray #[239 191 191] |
|
4896 |
||
18597 | 4897 |
'abcde1234' asUnicode32String utf8Encoded |
4898 |
'abcdeäöüß' asUnicode32String utf8Encoded |
|
12925
5c0a9a633ca1
comment/format in: #utf8Encoded
Claus Gittinger <cg@exept.de>
parents:
12912
diff
changeset
|
4899 |
" |
17625 | 4900 |
! |
4901 |
||
4902 |
utf8EncodedOn:aStream |
|
19837 | 4903 |
"return the UTF-8 representation of a Unicode string. |
4904 |
The resulting string is only useful to be stored on some external file, |
|
4905 |
not for being used inside ST/X." |
|
4906 |
||
4907 |
|string |
|
4908 |
stringSize "{ Class: SmallInteger }" |
|
4909 |
codePoint "{Class: SmallInteger }" |
|
4910 |
v "{Class: SmallInteger }" |
|
4911 |
character b1 b2 b3 b4 b5| |
|
17625 | 4912 |
|
4913 |
string := self string. |
|
19837 | 4914 |
|
4915 |
"/ avoid creation of new strings if possible |
|
4916 |
string containsNon7BitAscii ifFalse:[ |
|
4917 |
aStream nextPutAll:string asSingleByteString. |
|
4918 |
^ self. |
|
4919 |
]. |
|
4920 |
||
4921 |
stringSize := string size. |
|
4922 |
||
4923 |
1 to:stringSize do:[:idx | |
|
4924 |
character := string at:idx. |
|
4925 |
codePoint := character codePoint. |
|
4926 |
codePoint < 16r80 ifTrue:[ |
|
4927 |
aStream nextPutByte:codePoint. |
|
4928 |
] ifFalse:[ |
|
4929 |
b1 := (codePoint bitAnd:16r3F) bitOr:2r10000000. |
|
4930 |
v := codePoint bitShift:-6. |
|
4931 |
v <= 16r1F ifTrue:[ |
|
4932 |
aStream nextPutByte:(v bitOr:2r11000000). |
|
4933 |
aStream nextPutByte:b1. |
|
4934 |
] ifFalse:[ |
|
4935 |
b2 := (v bitAnd:16r3F) bitOr:2r10000000. |
|
4936 |
v := v bitShift:-6. |
|
4937 |
v <= 16r0F ifTrue:[ |
|
4938 |
aStream nextPutByte:(v bitOr:2r11100000). |
|
4939 |
aStream nextPutByte:b2; nextPutByte:b1. |
|
4940 |
] ifFalse:[ |
|
4941 |
b3 := (v bitAnd:16r3F) bitOr:2r10000000. |
|
4942 |
v := v bitShift:-6. |
|
4943 |
v <= 16r07 ifTrue:[ |
|
4944 |
aStream nextPutByte:(v bitOr:2r11110000). |
|
4945 |
aStream nextPutByte:b3; nextPutByte:b2; nextPutByte:b1. |
|
4946 |
] ifFalse:[ |
|
4947 |
b4 := (v bitAnd:16r3F) bitOr:2r10000000. |
|
4948 |
v := v bitShift:-6. |
|
4949 |
v <= 16r03 ifTrue:[ |
|
4950 |
aStream nextPutByte:(v bitOr:2r11111000). |
|
4951 |
aStream nextPutByte:b4; nextPutByte:b3; nextPutByte:b2; nextPutByte:b1. |
|
4952 |
] ifFalse:[ |
|
4953 |
b5 := (v bitAnd:16r3F) bitOr:2r10000000. |
|
4954 |
v := v bitShift:-6. |
|
4955 |
v <= 16r01 ifTrue:[ |
|
4956 |
aStream nextPutByte:(v bitOr:2r11111100). |
|
4957 |
aStream nextPutByte:b5; nextPutByte:b4; nextPutByte:b3; nextPutByte:b2; nextPutByte:b1. |
|
4958 |
] ifFalse:[ |
|
4959 |
"/ cannot happen - we only support up to 30 bit characters |
|
4960 |
EncodingError raiseWith:character errorString:'codePoint > 31bit in #utf8Encode'. |
|
4961 |
] |
|
4962 |
]. |
|
4963 |
]. |
|
4964 |
]. |
|
4965 |
]. |
|
4966 |
]. |
|
4967 |
]. |
|
4968 |
||
4969 |
" |
|
4970 |
String streamContents:[:s| |
|
4971 |
'hallo' utf8EncodedOn:s |
|
4972 |
]. |
|
4973 |
||
4974 |
ByteArray streamContents:[:s| |
|
4975 |
'hallo' utf8EncodedOn:s |
|
4976 |
]. |
|
4977 |
||
4978 |
String streamContents:[:s| |
|
4979 |
'abcdeäöüß' asUnicode32String utf8EncodedOn:s |
|
4980 |
]. |
|
4981 |
. |
|
4982 |
ByteArray streamContents:[:s| |
|
4983 |
'abcdeäöüß' asUnicode32String utf8EncodedOn:s |
|
4984 |
]. |
|
4985 |
||
4986 |
'/tmp/bytes' asFilename writingFileDo:[:s| |
|
4987 |
'abcdeäöüß' utf8EncodedOn:s |
|
4988 |
]. |
|
17625 | 4989 |
" |
9192 | 4990 |
! ! |
4991 |
||
11345 | 4992 |
|
12111 | 4993 |
!CharacterArray methodsFor:'matching - glob expressions'! |
4994 |
||
4995 |
compoundMatch:aString |
|
4996 |
"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
|
4997 |
consisting of multiple simple GLOB patterns, separated by semicolons. |
12111 | 4998 |
This is usable with fileName pattern fields. |
4999 |
||
5000 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20012 | 5001 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5002 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 5003 |
NOTICE: the receiver is the match pattern" |
5004 |
||
15890 | 5005 |
^ self compoundMatch:aString caseSensitive:true withoutSeparators:false |
12111 | 5006 |
|
5007 |
" |
|
5008 |
'f*' match:'foo' |
|
5009 |
'b*' match:'foo' |
|
5010 |
'f*;b*' match:'foo' |
|
5011 |
'f*;b*' match:'bar' |
|
5012 |
'f*;b*' compoundMatch:'foo' |
|
5013 |
'f*;b*' compoundMatch:'bar' |
|
5014 |
" |
|
5015 |
||
5016 |
"Modified: / 30.1.1998 / 11:40:18 / stefan" |
|
5017 |
"Modified: / 16.12.1999 / 01:22:08 / cg" |
|
5018 |
! |
|
5019 |
||
15734 | 5020 |
compoundMatch:aString caseSensitive:caseSensitive |
12111 | 5021 |
"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
|
5022 |
consisting of multiple simple GLOB patterns, separated by semicolons. |
12111 | 5023 |
This is usable with fileName pattern fields. |
5024 |
||
5025 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20012 | 5026 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5027 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 5028 |
NOTICE: the receiver is the match pattern" |
5029 |
||
15890 | 5030 |
^self compoundMatch:aString caseSensitive:caseSensitive withoutSeparators:false |
5031 |
||
5032 |
" |
|
5033 |
'f*' match:'foo' |
|
5034 |
'b*' match:'foo' |
|
5035 |
'f*;b*' match:'foo' |
|
5036 |
'f*;b*' match:'bar' |
|
5037 |
'f*;b*' compoundMatch:'foo' |
|
5038 |
'f*;b*' compoundMatch:'bar' |
|
5039 |
'f*;b*' compoundMatch:'Foo' caseSensitive:false |
|
5040 |
'f*;b*' compoundMatch:'Bar' caseSensitive:false |
|
5041 |
'f*;b*' compoundMatch:'ccc' caseSensitive:false |
|
5042 |
" |
|
5043 |
||
5044 |
"Modified: / 15.4.1997 / 15:50:33 / cg" |
|
5045 |
"Modified: / 30.1.1998 / 11:40:18 / stefan" |
|
5046 |
"Created: / 16.12.1999 / 01:21:35 / cg" |
|
5047 |
! |
|
5048 |
||
5049 |
compoundMatch:aString caseSensitive:caseSensitive withoutSeparators:withoutSeparators |
|
5050 |
"like match, but the receiver may be a compound match pattern, |
|
5051 |
consisting of multiple simple GLOB patterns, separated by semicolons. |
|
5052 |
If withoutSeparators is true, spaces around individual patterns are stripped off. |
|
5053 |
This is usable with fileName pattern fields. |
|
5054 |
||
5055 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20012 | 5056 |
NOT the ST-80 meaning. |
15890 | 5057 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
5058 |
NOTICE: the receiver is the match pattern" |
|
5059 |
||
12111 | 5060 |
|matchers| |
5061 |
||
5062 |
matchers := self asCollectionOfSubstringsSeparatedBy:$;. |
|
15890 | 5063 |
withoutSeparators ifTrue:[ matchers := matchers collect:[:each | each withoutSeparators] ]. |
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
5064 |
^ matchers |
20012 | 5065 |
contains:[:aPattern | |
5066 |
aPattern match:aString caseSensitive:caseSensitive escapeCharacter:nil |
|
5067 |
]. |
|
15734 | 5068 |
|
5069 |
" |
|
5070 |
'f*' match:'foo' |
|
5071 |
'b*' match:'foo' |
|
5072 |
'f*;b*' match:'foo' |
|
5073 |
'f*;b*' match:'bar' |
|
5074 |
'f*;b*' compoundMatch:'foo' |
|
5075 |
'f*;b*' compoundMatch:'bar' |
|
5076 |
'f*;b*' compoundMatch:'Foo' caseSensitive:false |
|
5077 |
'f*;b*' compoundMatch:'Bar' caseSensitive:false |
|
5078 |
'f*;b*' compoundMatch:'ccc' caseSensitive:false |
|
15890 | 5079 |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
5080 |
'f* ; b*' compoundMatch:'foo' |
15890 | 5081 |
'f* ; b*' compoundMatch:'foo' caseSensitive:true withoutSeparators:true |
15734 | 5082 |
" |
5083 |
||
5084 |
"Modified: / 15.4.1997 / 15:50:33 / cg" |
|
5085 |
"Modified: / 30.1.1998 / 11:40:18 / stefan" |
|
5086 |
"Created: / 16.12.1999 / 01:21:35 / cg" |
|
5087 |
! |
|
5088 |
||
5089 |
compoundMatch:aString ignoreCase:ignoreCase |
|
5090 |
<resource: #obsolete> |
|
5091 |
||
5092 |
"like match, but the receiver may be a compound match pattern, |
|
5093 |
consisting of multiple simple GLOB patterns, separated by semicolons. |
|
5094 |
This is usable with fileName pattern fields. |
|
5095 |
||
5096 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20012 | 5097 |
NOT the ST-80 meaning. |
15734 | 5098 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
5099 |
NOTICE: the receiver is the match pattern" |
|
5100 |
||
5101 |
^ self compoundMatch:aString caseSensitive:ignoreCase not |
|
12111 | 5102 |
|
5103 |
" |
|
5104 |
'f*' match:'foo' |
|
5105 |
'b*' match:'foo' |
|
5106 |
'f*;b*' match:'foo' |
|
5107 |
'f*;b*' match:'bar' |
|
5108 |
'f*;b*' compoundMatch:'foo' |
|
5109 |
'f*;b*' compoundMatch:'bar' |
|
5110 |
'f*;b*' compoundMatch:'Foo' ignoreCase:true |
|
5111 |
'f*;b*' compoundMatch:'Bar' ignoreCase:true |
|
5112 |
'f*;b*' compoundMatch:'ccc' ignoreCase:true |
|
5113 |
" |
|
5114 |
||
5115 |
"Modified: / 15.4.1997 / 15:50:33 / cg" |
|
5116 |
"Modified: / 30.1.1998 / 11:40:18 / stefan" |
|
5117 |
"Created: / 16.12.1999 / 01:21:35 / cg" |
|
5118 |
! |
|
5119 |
||
5120 |
findMatchString:matchString |
|
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5121 |
"like findString/indexOfSubCollection, but allowing GLOB match patterns. |
12111 | 5122 |
find matchstring; if found, return the index; |
5123 |
if not found, return 0. |
|
5124 |
||
5125 |
NOTICE: match-meta character interpretation is like in unix-matching, |
|
20012 | 5126 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5127 |
NOTICE: this GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 5128 |
NOTICE: the argument is the match pattern" |
5129 |
||
19033 | 5130 |
^ self findMatchString:matchString startingAt:1 caseSensitive:true ifAbsent:0 |
5131 |
||
5132 |
" |
|
5133 |
1234567890123 |
|
5134 |
'hello world bla foo baz' findMatchString:'b* ' |
|
5135 |
" |
|
12111 | 5136 |
! |
5137 |
||
5138 |
findMatchString:matchString startingAt:index |
|
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5139 |
"like findString, but allowing GLOB match patterns. |
12111 | 5140 |
find matchstring, starting at index. if found, return the index; |
5141 |
if not found, return 0. |
|
5142 |
||
5143 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20012 | 5144 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5145 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 5146 |
NOTICE: the argument is the match pattern" |
5147 |
||
19033 | 5148 |
^ self findMatchString:matchString startingAt:index caseSensitive:true ifAbsent:0 |
12111 | 5149 |
! |
5150 |
||
15734 | 5151 |
findMatchString:matchString startingAt:index caseSensitive:caseSensitive ifAbsent:exceptionBlock |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5152 |
"like findString, but allowing GLOB match patterns. |
12111 | 5153 |
find matchstring, starting at index. if found, return the index; |
5154 |
if not found, return the result of evaluating exceptionBlock. |
|
5155 |
This is a q&d hack - not very efficient. |
|
5156 |
||
5157 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20012 | 5158 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5159 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 5160 |
NOTICE: the argument is the match pattern" |
5161 |
||
5162 |
|firstChar firstSet |
|
5163 |
startIndex "{ Class: SmallInteger }" |
|
5164 |
matchSize "{ Class: SmallInteger }" |
|
5165 |
mySize "{ Class: SmallInteger }" |
|
5166 |
realMatchString lcChar ucChar| |
|
5167 |
||
5168 |
matchSize := matchString size. |
|
5169 |
matchSize == 0 ifTrue:[^ index]. "empty string matches" |
|
5170 |
||
5171 |
realMatchString := matchString. |
|
5172 |
(realMatchString endsWith:$*) ifFalse:[ |
|
20012 | 5173 |
realMatchString := realMatchString , '*'. |
5174 |
matchSize := matchSize + 1 |
|
12111 | 5175 |
]. |
5176 |
||
5177 |
mySize := self size. |
|
5178 |
firstChar := realMatchString at:1. |
|
5179 |
firstChar == self class matchEscapeCharacter ifTrue:[ |
|
20012 | 5180 |
firstChar := realMatchString at:2. |
12111 | 5181 |
]. |
5182 |
||
5183 |
firstChar asString includesMatchCharacters ifTrue:[ |
|
20012 | 5184 |
index to:mySize do:[:col | |
5185 |
(realMatchString match:self from:col to:mySize caseSensitive:caseSensitive) |
|
5186 |
ifTrue:[^ col] |
|
5187 |
]. |
|
5188 |
^ exceptionBlock value. |
|
12111 | 5189 |
]. |
5190 |
||
5191 |
lcChar := firstChar asLowercase. |
|
5192 |
ucChar := firstChar asUppercase. |
|
15734 | 5193 |
(caseSensitive not and:[ lcChar ~= ucChar ]) ifTrue:[ |
20012 | 5194 |
firstSet := Array with:ucChar with:lcChar. |
5195 |
startIndex := self indexOfAny:firstSet startingAt:index. |
|
12111 | 5196 |
] ifFalse:[ |
20012 | 5197 |
startIndex := self indexOf:firstChar startingAt:index. |
12111 | 5198 |
]. |
5199 |
[startIndex == 0] whileFalse:[ |
|
20012 | 5200 |
(realMatchString match:self from:startIndex to:mySize caseSensitive:caseSensitive) |
5201 |
ifTrue:[^ startIndex]. |
|
5202 |
||
5203 |
firstSet notNil ifTrue:[ |
|
5204 |
startIndex := self indexOfAny:firstSet startingAt:(startIndex + 1). |
|
5205 |
] ifFalse:[ |
|
5206 |
startIndex := self indexOf:firstChar startingAt:(startIndex + 1). |
|
5207 |
]. |
|
12111 | 5208 |
]. |
5209 |
^ exceptionBlock value |
|
5210 |
||
5211 |
" |
|
5212 |
'one two three four' findMatchString:'o[nu]' |
|
5213 |
'one two three four' findMatchString:'o[nu]' startingAt:3 |
|
15734 | 5214 |
'one two three four one' findMatchString:'ONE' startingAt:3 caseSensitive:false ifAbsent:0 |
5215 |
'one two three four one' findMatchString:'ONE' startingAt:3 caseSensitive:true ifAbsent:0 |
|
5216 |
'one two three four ONE' findMatchString:'O#E' startingAt:1 caseSensitive:false ifAbsent:0 |
|
5217 |
'one two three four ONE' findMatchString:'O#E' startingAt:1 caseSensitive:true ifAbsent:0 |
|
5218 |
12345678901234567890 |
|
5219 |
" |
|
5220 |
||
5221 |
"Modified: 13.9.1997 / 06:31:22 / cg" |
|
5222 |
! |
|
5223 |
||
5224 |
findMatchString:matchString startingAt:index ignoreCase:ignoreCase ifAbsent:exceptionBlock |
|
5225 |
<resource: #obsolete> |
|
5226 |
"like findString, but allowing GLOB match patterns. |
|
5227 |
find matchstring, starting at index. if found, return the index; |
|
5228 |
if not found, return the result of evaluating exceptionBlock. |
|
5229 |
This is a q&d hack - not very efficient. |
|
5230 |
||
5231 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20012 | 5232 |
NOT the ST-80 meaning. |
15734 | 5233 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
5234 |
NOTICE: the argument is the match pattern" |
|
5235 |
||
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
5236 |
^ self |
20012 | 5237 |
findMatchString:matchString startingAt:index caseSensitive:ignoreCase not ifAbsent:exceptionBlock |
15734 | 5238 |
|
5239 |
" |
|
5240 |
'one two three four' findMatchString:'o[nu]' |
|
5241 |
'one two three four' findMatchString:'o[nu]' startingAt:3 |
|
12111 | 5242 |
'one two three four one' findMatchString:'ONE' startingAt:3 ignoreCase:true ifAbsent:0 |
5243 |
" |
|
5244 |
||
5245 |
"Modified: 13.9.1997 / 06:31:22 / cg" |
|
5246 |
! |
|
5247 |
||
19746 | 5248 |
globPatternAsRegexPattern |
5249 |
"taking the receiver as a GLOB pattern, |
|
5250 |
return a corresponding regex pattern. |
|
5251 |
As regex does provide information about the matching substring, |
|
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
5252 |
it may be useful to apply a regex after a GLOB match, |
19746 | 5253 |
in order to highlight matching substrings (eg. in a CodeView after a search). |
5254 |
If it turns out to be better, we may convert all GLOB searches to regex and use it right away. |
|
5255 |
(currently, it is not sure, if GLOB is not better for most simple searches, as they are encountered |
|
5256 |
in typical real life)" |
|
5257 |
||
5258 |
^ self species streamContents:[:s | |
|
20012 | 5259 |
|addCharacter| |
5260 |
||
5261 |
addCharacter := |
|
5262 |
[:ch | |
|
5263 |
ch isLetterOrDigit ifFalse:[ |
|
5264 |
s nextPut:$\. |
|
5265 |
]. |
|
5266 |
s nextPut:ch |
|
5267 |
]. |
|
5268 |
||
5269 |
(String matchScanArrayFrom:self) do:[:matchEntry | |
|
5270 |
matchEntry isCharacter ifTrue:[ |
|
5271 |
addCharacter value:matchEntry |
|
5272 |
] ifFalse:[ |
|
5273 |
matchEntry == #anyString ifTrue:[ |
|
5274 |
s nextPutAll:'.*' |
|
5275 |
] ifFalse:[ |
|
5276 |
matchEntry == #any ifTrue:[ |
|
5277 |
s nextPut:$. |
|
5278 |
] ifFalse:[ |
|
5279 |
matchEntry isString ifTrue:[ |
|
5280 |
|set min max| |
|
5281 |
||
5282 |
s nextPut:$[. |
|
5283 |
set := matchEntry copy sort. |
|
5284 |
min := set min. |
|
5285 |
max := set max. |
|
5286 |
set asSet = (min to:max) asSet ifTrue:[ |
|
5287 |
addCharacter value:min. |
|
5288 |
s nextPut:$-. |
|
5289 |
addCharacter value:max. |
|
5290 |
] ifFalse:[ |
|
5291 |
set do:addCharacter. |
|
5292 |
]. |
|
5293 |
s nextPut:$]. |
|
5294 |
] ifFalse:[ |
|
5295 |
self halt. |
|
5296 |
]. |
|
5297 |
]. |
|
5298 |
]. |
|
5299 |
] |
|
5300 |
]. |
|
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
5301 |
]. |
19746 | 5302 |
|
5303 |
" |
|
5304 |
'hello' globPatternAsRegexPattern |
|
5305 |
'hello*' globPatternAsRegexPattern |
|
5306 |
'*hello*' globPatternAsRegexPattern |
|
5307 |
'h###' globPatternAsRegexPattern |
|
5308 |
'h[0-9]' globPatternAsRegexPattern |
|
5309 |
'h[0-9][0-9][0-9]' globPatternAsRegexPattern |
|
5310 |
'h[0-9]*' globPatternAsRegexPattern |
|
19747 | 5311 |
'h[-+]*' globPatternAsRegexPattern |
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
5312 |
|
19747 | 5313 |
'hello world' matches:'h*w' |
5314 |
'hello world' matchesRegex:('h*w' globPatternAsRegexPattern) |
|
5315 |
'hello world' matches:'h*d' |
|
5316 |
'hello world' matchesRegex:('h*d' globPatternAsRegexPattern) |
|
19746 | 5317 |
" |
5318 |
! |
|
5319 |
||
12111 | 5320 |
includesMatchString:matchString |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5321 |
"like includesString, but allowing GLOB match patterns. |
12111 | 5322 |
find matchstring; if found, return true, otherwise return false. |
5323 |
||
5324 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20012 | 5325 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5326 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 5327 |
NOTICE: the argument is the match pattern" |
5328 |
||
5329 |
^ (self findMatchString:matchString) ~~ 0 |
|
5330 |
||
5331 |
" |
|
5332 |
'hello world' includesMatchString:'h*' |
|
5333 |
'hello world' includesMatchString:'h[aeiou]llo' |
|
5334 |
'hello world' includesMatchString:'wor*' |
|
5335 |
'hello world' includesMatchString:'woR*' |
|
5336 |
" |
|
5337 |
! |
|
5338 |
||
5339 |
includesMatchString:matchString caseSensitive:caseSensitive |
|
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5340 |
"like includesString, but allowing GLOB match patterns. |
12111 | 5341 |
find matchstring; if found, return true, otherwise return false. |
5342 |
||
5343 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20012 | 5344 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5345 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 5346 |
NOTICE: the argument is the match pattern" |
5347 |
||
19033 | 5348 |
^ (self findMatchString:matchString startingAt:1 caseSensitive:caseSensitive ifAbsent:0) ~~ 0 |
12111 | 5349 |
|
5350 |
" |
|
5351 |
'hello world' includesMatchString:'h*' caseSensitive:true |
|
5352 |
'hello world' includesMatchString:'h*' caseSensitive:false |
|
5353 |
'Hello world' includesMatchString:'h*' caseSensitive:true |
|
5354 |
'Hello world' includesMatchString:'h*' caseSensitive:false |
|
5355 |
||
5356 |
'hello world' includesMatchString:'h[aeiou]llo' caseSensitive:true |
|
5357 |
'hello world' includesMatchString:'h[aeiou]llo' caseSensitive:false |
|
5358 |
||
5359 |
'hello world' includesMatchString:'wor*' caseSensitive:true |
|
5360 |
'hello world' includesMatchString:'wor*' caseSensitive:false |
|
5361 |
||
5362 |
'hello world' includesMatchString:'woR*' caseSensitive:true |
|
5363 |
'hello world' includesMatchString:'woR*' caseSensitive:false |
|
5364 |
" |
|
5365 |
! |
|
5366 |
||
5367 |
match:aString |
|
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5368 |
"return true if aString matches self, where self may contain GLOB meta-match |
12111 | 5369 |
characters $* (to match any string) or $# (to match any character). |
5370 |
or [...] to match a set of characters. |
|
5371 |
Lower/uppercase are considered different. |
|
5372 |
The escape character is the backQuote. |
|
5373 |
||
5374 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20012 | 5375 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5376 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 5377 |
NOTICE: the receiver is the match pattern" |
5378 |
||
15734 | 5379 |
^ self match:aString from:1 to:aString size caseSensitive:true |
12111 | 5380 |
|
5381 |
" |
|
5382 |
'\*f*' match:'f' |
|
5383 |
'\*f*' match:'*f' |
|
5384 |
'*\*f*' match:'*f' |
|
5385 |
'*f*' match:'*f' |
|
5386 |
'*ute*' match:'computer' |
|
5387 |
'*uter' match:'computer' |
|
5388 |
'uter*' match:'computer' |
|
5389 |
'*ute*' match:'' |
|
5390 |
'[abcd]*' match:'computer' |
|
5391 |
'[abcd]*' match:'komputer' |
|
5392 |
'*some*compl*ern*' match:'this is some more complicated pattern match' |
|
5393 |
'*some*compl*ern*' match:'this is another complicated pattern match' |
|
5394 |
'*-hh' match:'anton-h' |
|
5395 |
" |
|
5396 |
||
5397 |
"Modified: / 9.6.1998 / 18:50:00 / cg" |
|
5398 |
! |
|
5399 |
||
15734 | 5400 |
match:aString caseSensitive:caseSensitive |
5401 |
"return true if aString matches self, where self may contain GLOB meta-match |
|
5402 |
characters $* (to match any string) or $# (to match any character) |
|
5403 |
or [...] to match a set of characters. |
|
5404 |
If caseSensitive is false, lower/uppercase are considered the same. |
|
5405 |
The escape character is the backQuote. |
|
5406 |
||
5407 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20012 | 5408 |
NOT the ST-80 meaning. |
15734 | 5409 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
5410 |
NOTICE: the receiver is the match pattern" |
|
5411 |
||
5412 |
^ self match:aString from:1 to:aString size caseSensitive:caseSensitive |
|
5413 |
||
5414 |
" |
|
5415 |
'*ute*' match:'COMPUTER' caseSensitive:false |
|
5416 |
'*uter' match:'COMPUTER' caseSensitive:true |
|
5417 |
'[abcd]*' match:'computer' caseSensitive:true |
|
5418 |
'[abcd]*' match:'Computer' caseSensitive:true |
|
5419 |
'[a-k]*' match:'komputer' caseSensitive:true |
|
5420 |
'[a-k]*' match:'zomputer' caseSensitive:true |
|
5421 |
'[a-k]*' match:'Komputer' caseSensitive:true |
|
5422 |
'[a-k]*' match:'Komputer' caseSensitive:false |
|
5423 |
'*some*compl*ern*' match:'this is some more complicated pattern match' caseSensitive:false |
|
5424 |
'*some*compl*ern*' match:'this is another complicated pattern match' caseSensitive:false |
|
5425 |
||
5426 |
Time millisecondsToRun:[ |
|
20012 | 5427 |
Symbol allInstancesDo:[:sym | |
5428 |
'[ab]*' match:sym caseSensitive:true |
|
5429 |
] |
|
15734 | 5430 |
]. |
5431 |
Time millisecondsToRun:[ |
|
20012 | 5432 |
Symbol allInstancesDo:[:sym | |
5433 |
'*at:*' match:sym caseSensitive:true |
|
5434 |
] |
|
15734 | 5435 |
]. |
5436 |
" |
|
5437 |
||
5438 |
"Modified: 2.4.1997 / 17:28:58 / cg" |
|
5439 |
! |
|
5440 |
||
5441 |
match:aString caseSensitive:caseSensitive escapeCharacter:escape |
|
5442 |
"return true if aString matches self, where self may contain GLOB meta-match |
|
5443 |
characters $* (to match any string) or $# (to match any character) |
|
5444 |
or [...] to match a set of characters. |
|
5445 |
If caseSensitive is false, lower/uppercase are considered the same. |
|
5446 |
||
5447 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20012 | 5448 |
NOT the ST-80 meaning. |
15734 | 5449 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
5450 |
NOTICE: the receiver is the match pattern" |
|
5451 |
||
5452 |
^ self match:aString from:1 to:aString size caseSensitive:caseSensitive escapeCharacter:escape |
|
5453 |
||
5454 |
" |
|
5455 |
'*ute*' match:'COMPUTER' caseSensitive:false |
|
5456 |
'*uter' match:'COMPUTER' caseSensitive:true |
|
5457 |
'[abcd]*' match:'computer' caseSensitive:true |
|
5458 |
'[abcd]*' match:'Computer' caseSensitive:true |
|
5459 |
'[a-k]*' match:'komputer' caseSensitive:true |
|
5460 |
'[a-k]*' match:'zomputer' caseSensitive:true |
|
5461 |
'[a-k]*' match:'Komputer' caseSensitive:true |
|
5462 |
'[a-k]*' match:'Komputer' caseSensitive:false |
|
5463 |
'*some*compl*ern*' match:'this is some more complicated pattern match' caseSensitive:false |
|
5464 |
'*some*compl*ern*' match:'this is another complicated pattern match' caseSensitive:false |
|
5465 |
||
5466 |
Time millisecondsToRun:[ |
|
20012 | 5467 |
Symbol allInstancesDo:[:sym | |
5468 |
'[ab]*' match:sym caseSensitive:true |
|
5469 |
] |
|
15734 | 5470 |
]. |
5471 |
Time millisecondsToRun:[ |
|
20012 | 5472 |
Symbol allInstancesDo:[:sym | |
5473 |
'*at:*' match:sym caseSensitive:true |
|
5474 |
] |
|
15734 | 5475 |
]. |
5476 |
" |
|
5477 |
||
5478 |
"Modified: 2.4.1997 / 17:28:58 / cg" |
|
5479 |
! |
|
5480 |
||
12111 | 5481 |
match:aString escapeCharacter:escape |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5482 |
"return true if aString matches self, where self may contain GLOB meta-match |
12111 | 5483 |
characters $* (to match any string) or $# (to match any character). |
5484 |
or [...] to match a set of characters. |
|
5485 |
Lower/uppercase are considered different. |
|
5486 |
||
5487 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20012 | 5488 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5489 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 5490 |
NOTICE: the receiver is the match pattern" |
5491 |
||
19033 | 5492 |
^ self match:aString from:1 to:aString size caseSensitive:true escapeCharacter:escape |
12111 | 5493 |
|
5494 |
" |
|
5495 |
'a\b\c\*' match:'a\b\c\d' |
|
5496 |
'a\b\c\*' match:'a\b\c\d' escapeCharacter:nil |
|
5497 |
" |
|
5498 |
! |
|
5499 |
||
15734 | 5500 |
match:aString from:start to:stop caseSensitive:caseSensitive |
5501 |
"return true if part of aString matches myself, |
|
5502 |
where self may contain GLOB meta-match |
|
5503 |
characters $* (to match any string) or $# (to match any character) |
|
5504 |
or [...] to match a set of characters. |
|
5505 |
If caseSensitive is false, lower/uppercase are considered the same. |
|
5506 |
The escape character is the backQuote. |
|
5507 |
||
5508 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20012 | 5509 |
NOT the ST-80 meaning. |
15734 | 5510 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
5511 |
NOTICE: the receiver is the match pattern" |
|
5512 |
||
5513 |
^ self |
|
20012 | 5514 |
match:aString from:start to:stop caseSensitive:caseSensitive |
5515 |
escapeCharacter:(self class matchEscapeCharacter) |
|
15734 | 5516 |
|
5517 |
" |
|
5518 |
'*ute*' match:'12345COMPUTER' from:1 to:5 caseSensitive:false |
|
5519 |
'*ute*' match:'12345COMPUTER' from:6 to:13 caseSensitive:false |
|
5520 |
" |
|
5521 |
||
5522 |
"Modified: / 10.11.1998 / 21:43:46 / cg" |
|
5523 |
! |
|
5524 |
||
5525 |
match:aString from:start to:stop caseSensitive:caseSensitive escapeCharacter:escape |
|
5526 |
"return true if part of aString matches myself, |
|
5527 |
where self may contain GLOB meta-match |
|
5528 |
characters $* (to match any string) or $# (to match any character) |
|
5529 |
or [...] to match a set of characters. |
|
5530 |
If caseSensitive is false, lower/uppercase are considered the same. |
|
5531 |
||
5532 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20667 | 5533 |
NOT the ST-80 meaning. |
15734 | 5534 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
5535 |
NOTICE: the receiver is the match pattern" |
|
5536 |
||
5537 |
|matchScanArray| |
|
5538 |
||
5539 |
" |
|
5540 |
keep the matchScanArray from the most recent match - |
|
5541 |
avoids parsing the pattern over-and over if multiple searches |
|
5542 |
are done with the same pattern. |
|
5543 |
" |
|
20667 | 5544 |
|
5545 |
(PreviousMatches isNil |
|
5546 |
or:[(matchScanArray := PreviousMatches at: self ifAbsent:[nil]) isNil]) ifTrue:[ |
|
5547 |
matchScanArray := self class matchScanArrayFrom:self escapeCharacter:escape. |
|
5548 |
matchScanArray isNil ifTrue:[ |
|
5549 |
'CharacterArray [info]: invalid matchpattern:''' infoPrint. self infoPrint. ''' comparing for equality.' infoPrintCR. |
|
5550 |
^ self = aString |
|
15734 | 5551 |
"/ ^ false |
20667 | 5552 |
]. |
5553 |
PreviousMatches isNil ifTrue:[ |
|
5554 |
PreviousMatches := CacheDictionary new:10 |
|
5555 |
]. |
|
5556 |
PreviousMatches at:self put:matchScanArray. |
|
15734 | 5557 |
]. |
5558 |
||
5559 |
^ self class |
|
20667 | 5560 |
matchScan:matchScanArray |
5561 |
from:1 to:matchScanArray size |
|
5562 |
with:aString |
|
5563 |
from:start to:stop |
|
5564 |
caseSensitive:caseSensitive |
|
15734 | 5565 |
|
5566 |
" |
|
5567 |
'*ute*' match:'12345COMPUTER' from:1 to:5 caseSensitive:false |
|
5568 |
'*ute*' match:'12345COMPUTER' from:6 to:13 caseSensitive:false |
|
5569 |
" |
|
5570 |
||
5571 |
"Modified: / 10.11.1998 / 21:43:46 / cg" |
|
5572 |
! |
|
5573 |
||
12111 | 5574 |
match:aString from:start to:stop ignoreCase:ignoreCase |
15734 | 5575 |
<resource: #obsolete> |
12111 | 5576 |
"return true if part of aString matches myself, |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5577 |
where self may contain GLOB meta-match |
12111 | 5578 |
characters $* (to match any string) or $# (to match any character) |
5579 |
or [...] to match a set of characters. |
|
5580 |
If ignoreCase is true, lower/uppercase are considered the same. |
|
5581 |
The escape character is the backQuote. |
|
5582 |
||
5583 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20012 | 5584 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5585 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 5586 |
NOTICE: the receiver is the match pattern" |
5587 |
||
5588 |
^ self |
|
20012 | 5589 |
match:aString from:start to:stop caseSensitive:ignoreCase not |
5590 |
escapeCharacter:(self class matchEscapeCharacter) |
|
12111 | 5591 |
|
5592 |
" |
|
5593 |
'*ute*' match:'12345COMPUTER' from:1 to:5 ignoreCase:true |
|
5594 |
'*ute*' match:'12345COMPUTER' from:6 to:13 ignoreCase:true |
|
5595 |
" |
|
5596 |
||
5597 |
"Modified: / 10.11.1998 / 21:43:46 / cg" |
|
5598 |
! |
|
5599 |
||
5600 |
match:aString from:start to:stop ignoreCase:ignoreCase escapeCharacter:escape |
|
15734 | 5601 |
<resource: #obsolete> |
12111 | 5602 |
"return true if part of aString matches myself, |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5603 |
where self may contain GLOB meta-match |
12111 | 5604 |
characters $* (to match any string) or $# (to match any character) |
5605 |
or [...] to match a set of characters. |
|
5606 |
If ignoreCase is true, lower/uppercase are considered the same. |
|
5607 |
||
5608 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20012 | 5609 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5610 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 5611 |
NOTICE: the receiver is the match pattern" |
5612 |
||
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
5613 |
^ self |
20012 | 5614 |
match:aString from:start to:stop |
5615 |
caseSensitive:ignoreCase not |
|
5616 |
escapeCharacter:escape |
|
12111 | 5617 |
|
5618 |
" |
|
5619 |
'*ute*' match:'12345COMPUTER' from:1 to:5 ignoreCase:true |
|
5620 |
'*ute*' match:'12345COMPUTER' from:6 to:13 ignoreCase:true |
|
5621 |
" |
|
5622 |
||
5623 |
"Modified: / 10.11.1998 / 21:43:46 / cg" |
|
5624 |
! |
|
5625 |
||
5626 |
match:aString ignoreCase:ignoreCase |
|
15734 | 5627 |
<resource: #obsolete> |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5628 |
"return true if aString matches self, where self may contain GLOB meta-match |
12111 | 5629 |
characters $* (to match any string) or $# (to match any character) |
5630 |
or [...] to match a set of characters. |
|
5631 |
If ignoreCase is true, lower/uppercase are considered the same. |
|
5632 |
The escape character is the backQuote. |
|
5633 |
||
5634 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20012 | 5635 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5636 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 5637 |
NOTICE: the receiver is the match pattern" |
5638 |
||
15734 | 5639 |
"/ leave it this way, in case a customer has redefined it for performance (for a while) |
19033 | 5640 |
^ self match:aString from:1 to:aString size caseSensitive:ignoreCase not |
12111 | 5641 |
|
5642 |
" |
|
5643 |
'*ute*' match:'COMPUTER' ignoreCase:true |
|
5644 |
'*uter' match:'COMPUTER' ignoreCase:false |
|
5645 |
'[abcd]*' match:'computer' ignoreCase:false |
|
5646 |
'[abcd]*' match:'Computer' ignoreCase:false |
|
5647 |
'[a-k]*' match:'komputer' ignoreCase:false |
|
5648 |
'[a-k]*' match:'zomputer' ignoreCase:false |
|
5649 |
'[a-k]*' match:'Komputer' ignoreCase:false |
|
5650 |
'[a-k]*' match:'Komputer' ignoreCase:true |
|
5651 |
'*some*compl*ern*' match:'this is some more complicated pattern match' ignoreCase:true |
|
5652 |
'*some*compl*ern*' match:'this is another complicated pattern match' ignoreCase:true |
|
5653 |
||
5654 |
Time millisecondsToRun:[ |
|
20012 | 5655 |
Symbol allInstancesDo:[:sym | |
5656 |
'[ab]*' match:sym ignoreCase:false |
|
5657 |
] |
|
12111 | 5658 |
]. |
5659 |
Time millisecondsToRun:[ |
|
20012 | 5660 |
Symbol allInstancesDo:[:sym | |
5661 |
'*at:*' match:sym ignoreCase:false |
|
5662 |
] |
|
12111 | 5663 |
]. |
5664 |
" |
|
5665 |
||
5666 |
"Modified: 2.4.1997 / 17:28:58 / cg" |
|
5667 |
! |
|
5668 |
||
5669 |
match:aString ignoreCase:ignoreCase escapeCharacter:escape |
|
15734 | 5670 |
<resource: #obsolete> |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5671 |
"return true if aString matches self, where self may contain GLOB meta-match |
12111 | 5672 |
characters $* (to match any string) or $# (to match any character) |
5673 |
or [...] to match a set of characters. |
|
5674 |
If ignoreCase is true, lower/uppercase are considered the same. |
|
5675 |
||
5676 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20012 | 5677 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5678 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 5679 |
NOTICE: the receiver is the match pattern" |
5680 |
||
19033 | 5681 |
^ self match:aString from:1 to:aString size caseSensitive:ignoreCase not escapeCharacter:escape |
12111 | 5682 |
|
5683 |
" |
|
5684 |
'*ute*' match:'COMPUTER' ignoreCase:true |
|
5685 |
'*uter' match:'COMPUTER' ignoreCase:false |
|
5686 |
'[abcd]*' match:'computer' ignoreCase:false |
|
5687 |
'[abcd]*' match:'Computer' ignoreCase:false |
|
5688 |
'[a-k]*' match:'komputer' ignoreCase:false |
|
5689 |
'[a-k]*' match:'zomputer' ignoreCase:false |
|
5690 |
'[a-k]*' match:'Komputer' ignoreCase:false |
|
5691 |
'[a-k]*' match:'Komputer' ignoreCase:true |
|
5692 |
'*some*compl*ern*' match:'this is some more complicated pattern match' ignoreCase:true |
|
5693 |
'*some*compl*ern*' match:'this is another complicated pattern match' ignoreCase:true |
|
5694 |
||
5695 |
Time millisecondsToRun:[ |
|
20012 | 5696 |
Symbol allInstancesDo:[:sym | |
5697 |
'[ab]*' match:sym ignoreCase:false |
|
5698 |
] |
|
12111 | 5699 |
]. |
5700 |
Time millisecondsToRun:[ |
|
20012 | 5701 |
Symbol allInstancesDo:[:sym | |
5702 |
'*at:*' match:sym ignoreCase:false |
|
5703 |
] |
|
12111 | 5704 |
]. |
5705 |
" |
|
5706 |
||
5707 |
"Modified: 2.4.1997 / 17:28:58 / cg" |
|
5708 |
! |
|
5709 |
||
5710 |
matches:aPatternString |
|
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5711 |
"return true if the receiver matches aString, where aPatternString may contain GLOB meta-match |
12111 | 5712 |
characters $* (to match any string) or $# (to match any character). |
5713 |
or [...] to match a set of characters. |
|
5714 |
Lower/uppercase are considered different. |
|
5715 |
||
5716 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20012 | 5717 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5718 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 5719 |
NOTICE: the receiver is the match pattern" |
5720 |
||
5721 |
^ aPatternString match:self |
|
14054
48d3fab77bd9
added: #matches:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
14053
diff
changeset
|
5722 |
! |
48d3fab77bd9
added: #matches:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
14053
diff
changeset
|
5723 |
|
15734 | 5724 |
matches:aPatternString caseSensitive:caseSensitive |
5725 |
"return true if the receiver matches aString, where aPatternString may contain GLOB meta-match |
|
5726 |
characters $* (to match any string) or $# (to match any character). |
|
5727 |
or [...] to match a set of characters. |
|
5728 |
||
5729 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20012 | 5730 |
NOT the ST-80 meaning. |
15734 | 5731 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
5732 |
NOTICE: the receiver is the match pattern" |
|
5733 |
||
5734 |
^ aPatternString match:self caseSensitive:caseSensitive |
|
5735 |
! |
|
5736 |
||
14054
48d3fab77bd9
added: #matches:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
14053
diff
changeset
|
5737 |
matches:aPatternString ignoreCase:ignoreCase |
15734 | 5738 |
<resource: #obsolete> |
14054
48d3fab77bd9
added: #matches:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
14053
diff
changeset
|
5739 |
"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
|
5740 |
characters $* (to match any string) or $# (to match any character). |
48d3fab77bd9
added: #matches:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
14053
diff
changeset
|
5741 |
or [...] to match a set of characters. |
48d3fab77bd9
added: #matches:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
14053
diff
changeset
|
5742 |
Lower/uppercase are considered different. |
48d3fab77bd9
added: #matches:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
14053
diff
changeset
|
5743 |
|
48d3fab77bd9
added: #matches:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
14053
diff
changeset
|
5744 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
20012 | 5745 |
NOT the ST-80 meaning. |
14054
48d3fab77bd9
added: #matches:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
14053
diff
changeset
|
5746 |
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
|
5747 |
NOTICE: the receiver is the match pattern" |
48d3fab77bd9
added: #matches:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
14053
diff
changeset
|
5748 |
|
15734 | 5749 |
^ aPatternString match:self caseSensitive:ignoreCase not |
14054
48d3fab77bd9
added: #matches:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
14053
diff
changeset
|
5750 |
|
48d3fab77bd9
added: #matches:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
14053
diff
changeset
|
5751 |
"Created: / 08-03-2012 / 03:11:11 / cg" |
12111 | 5752 |
! ! |
5753 |
||
5754 |
||
18722 | 5755 |
!CharacterArray methodsFor:'matching - regex'! |
5756 |
||
5757 |
matchesRegex:regexString caseSensitive:aBoolean |
|
5758 |
"Test if the receiver matches a regex. |
|
5759 |
May raise RxParser>>regexErrorSignal or child signals. |
|
5760 |
This is a part of the Regular Expression Matcher package, |
|
20012 | 5761 |
(c) 1996, 1999 Vassili Bykov. |
18722 | 5762 |
Refer to `documentation' protocol of RxParser class for details." |
5763 |
||
5764 |
aBoolean ifFalse:[ |
|
20012 | 5765 |
^ self matchesRegexIgnoringCase:regexString |
18722 | 5766 |
] ifTrue:[ |
20012 | 5767 |
^ self matchesRegex:regexString |
18722 | 5768 |
]. |
5769 |
||
5770 |
" |
|
5771 |
'hello world' matchesRegex:'h.*d'. |
|
5772 |
" |
|
5773 |
||
5774 |
"Created: / 13-12-2010 / 11:01:49 / cg" |
|
5775 |
! ! |
|
17625 | 5776 |
|
9192 | 5777 |
!CharacterArray methodsFor:'padded copying'! |
5778 |
||
5779 |
centerPaddedTo:newSize |
|
19464 | 5780 |
"return a new string consisting of the receiver's characters, |
5781 |
plus spaces up to length and center the receiver's characters in |
|
5782 |
the resulting string. |
|
5783 |
If the receiver's size is equal or greater than the length argument, |
|
5784 |
the original receiver is returned unchanged." |
|
9192 | 5785 |
|
5786 |
^ self centerPaddedTo:newSize with:(Character space) |
|
5787 |
||
5788 |
" |
|
5789 |
'foo' centerPaddedTo:10 |
|
5790 |
123 printString centerPaddedTo:10 |
|
5791 |
" |
|
5792 |
||
5793 |
"Created: 25.11.1995 / 10:53:57 / cg" |
|
5794 |
! |
|
9190 | 5795 |
|
5796 |
centerPaddedTo:size with:padCharacter |
|
5797 |
"return a new string of length size, which contains the receiver |
|
5798 |
centered (i.e. padded on both sides). |
|
5799 |
Characters are filled with padCharacter. |
|
19464 | 5800 |
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
|
5801 |
the original receiver is returned unchanged." |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
5802 |
|
9190 | 5803 |
|len s| |
5804 |
||
5805 |
len := self size. |
|
9192 | 5806 |
(len < size) ifTrue:[ |
20012 | 5807 |
s := self species new:size withAll:padCharacter. |
5808 |
s replaceFrom:(size - len) // 2 + 1 with:self. |
|
5809 |
^ s |
|
9192 | 5810 |
] |
5811 |
||
9190 | 5812 |
" |
5813 |
'foo' centerPaddedTo:11 with:$. |
|
5814 |
'fooBar' centerPaddedTo:5 with:$. |
|
5815 |
123 printString centerPaddedTo:10 with:$. |
|
5816 |
(' ' , 123 printString) centerPaddedTo:10 with:$. |
|
5817 |
(Float pi printString) centerPaddedTo:15 with:(Character space) |
|
5818 |
(Float pi printString) centerPaddedTo:15 with:$- |
|
5819 |
(' ' , Float pi class name) centerPaddedTo:15 with:$. |
|
5820 |
" |
|
815
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
5821 |
! |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
5822 |
|
9190 | 5823 |
decimalPaddedTo:size and:afterPeriod at:decimalCharacter |
815
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
5824 |
"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
|
5825 |
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
|
5826 |
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
|
5827 |
to be padded). |
19464 | 5828 |
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
|
5829 |
the original receiver is returned unchanged. |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
5830 |
(sounds complicated ? -> see examples below)." |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
5831 |
|
8874 | 5832 |
^ self |
20012 | 5833 |
decimalPaddedTo:size |
5834 |
and:afterPeriod |
|
5835 |
at:decimalCharacter |
|
5836 |
withLeft:(Character space) |
|
5837 |
right:$0 |
|
815
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
5838 |
|
8874 | 5839 |
" |
5840 |
'123' decimalPaddedTo:10 and:3 at:$. -> ' 123 ' |
|
5841 |
'123' decimalPaddedTo:10 and:3 at:$. -> ' 123.000' |
|
5842 |
'123.' decimalPaddedTo:10 and:3 at:$. -> ' 123.000' |
|
815
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
5843 |
'123.1' decimalPaddedTo:10 and:3 at:$. -> ' 123.100' |
8874 | 5844 |
'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
|
5845 |
'123.123' decimalPaddedTo:10 and:3 at:$. -> ' 123.123' |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
5846 |
" |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
5847 |
|
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
5848 |
"Created: 23.12.1995 / 13:11:52 / cg" |
9192 | 5849 |
! |
5850 |
||
5851 |
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
|
5852 |
"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
|
5853 |
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
|
5854 |
of the period. |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
5855 |
Characters on the left are filled with leftPadChar. |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
5856 |
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
|
5857 |
otherwise, if missing, a decimal point is added and right characters filled with this. |
19464 | 5858 |
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
|
5859 |
the original receiver is returned unchanged. |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
5860 |
(sounds complicated ? -> see examples below)." |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
5861 |
|
5230
655c21ffe6cc
care for negative size of rest-string when decimalPadding
Claus Gittinger <cg@exept.de>
parents:
5221
diff
changeset
|
5862 |
|s idx n rest| |
815
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
5863 |
|
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
5864 |
idx := self indexOf:decimalCharacter. |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
5865 |
idx == 0 ifTrue:[ |
20012 | 5866 |
"/ |
5867 |
"/ no decimal point found; adjust string to the left of the period column |
|
5868 |
"/ |
|
5869 |
rightPadChar isNil ifTrue:[ |
|
5870 |
s := self , (self species new:afterPeriod + 1 withAll:leftPadChar) |
|
5871 |
] ifFalse:[ |
|
5872 |
s:= self , decimalCharacter asString , (self species new:afterPeriod withAll:rightPadChar). |
|
5873 |
]. |
|
815
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
5874 |
] ifFalse:[ |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
5875 |
|
20012 | 5876 |
"/ the number of after-decimalPoint characters |
5877 |
n := self size - idx. |
|
5878 |
rest := afterPeriod - n. |
|
5879 |
rest > 0 ifTrue:[ |
|
5880 |
s := (self species new:rest withAll:(rightPadChar ? leftPadChar)). |
|
5881 |
] ifFalse:[ |
|
5882 |
s := '' |
|
5883 |
]. |
|
5884 |
s := self , s. |
|
815
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
5885 |
]. |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
5886 |
|
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
5887 |
^ s leftPaddedTo:size with:leftPadChar |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
5888 |
|
8874 | 5889 |
" |
5890 |
'123' decimalPaddedTo:10 and:3 at:$. withLeft:(Character space) right:nil -> ' 123 ' |
|
5891 |
'123' decimalPaddedTo:10 and:3 at:$. withLeft:(Character space) right:$0 -> ' 123.000' |
|
5892 |
'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
|
5893 |
'123.1' decimalPaddedTo:10 and:3 at:$. withLeft:(Character space) right:$0 -> ' 123.100' |
8874 | 5894 |
'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
|
5895 |
'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
|
5896 |
" |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
5897 |
|
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
5898 |
"Modified: 23.12.1995 / 13:08:18 / cg" |
9192 | 5899 |
! |
5900 |
||
5901 |
leftPaddedTo:size |
|
5902 |
"return a new string of length size, which contains the receiver |
|
5903 |
right-adjusted (i.e. padded on the left). |
|
5904 |
Characters on the left are filled with spaces. |
|
19464 | 5905 |
If the receiver's size is equal or greater than the length argument, |
9192 | 5906 |
the original receiver is returned unchanged." |
5907 |
||
5908 |
^ self leftPaddedTo:size with:(Character space) |
|
5909 |
||
5910 |
" |
|
5911 |
'foo' leftPaddedTo:10 |
|
5912 |
'fooBar' leftPaddedTo:5 |
|
5913 |
123 printString leftPaddedTo:10 |
|
5914 |
" |
|
5915 |
! |
|
608 | 5916 |
|
5917 |
paddedTo:newSize |
|
19753 | 5918 |
"return a new string consisting of the receiver's characters, |
608 | 5919 |
plus spaces up to length. |
19464 | 5920 |
If the receiver's size is equal or greater than the length argument, |
608 | 5921 |
the original receiver is returned unchanged." |
5922 |
||
5923 |
^ self paddedTo:newSize with:(Character space) |
|
5924 |
||
5925 |
" |
|
8874 | 5926 |
'foo' paddedTo:10 |
5927 |
123 printString paddedTo:10 |
|
15734 | 5928 |
'12345678901234' paddedTo:10 |
608 | 5929 |
" |
64 | 5930 |
! ! |
5931 |
||
9192 | 5932 |
!CharacterArray methodsFor:'printing & storing'! |
608 | 5933 |
|
5934 |
article |
|
17137 | 5935 |
"return an article string for the receiver. |
5936 |
The original code only looked at the first character being a vowel; |
|
5937 |
this has been enhanced by some heuristics - not perfect, still." |
|
608 | 5938 |
|
17241 | 5939 |
|firstChar secondChar thirdChar| |
608 | 5940 |
|
8874 | 5941 |
firstChar := (self at:1) asLowercase. |
5848 | 5942 |
((firstChar isVowel and:[firstChar ~~ $u]) or:[firstChar == $x]) ifTrue:[ |
20012 | 5943 |
^ 'an' |
608 | 5944 |
]. |
17043 | 5945 |
|
5946 |
(self size >= 3) ifTrue:[ |
|
20012 | 5947 |
secondChar := (self at:2) asLowercase. |
5948 |
||
5949 |
"/ may need more here... |
|
5950 |
( #('rb') includes:(String with:firstChar with:secondChar)) ifTrue:[ |
|
5951 |
^ 'an' |
|
5952 |
]. |
|
5953 |
||
5954 |
thirdChar := (self at:3) asLowercase. |
|
5955 |
||
5956 |
(firstChar isVowel not |
|
5957 |
and:[(secondChar isVowel or:[secondChar == $y]) not |
|
5958 |
and:[thirdChar isVowel not ]]) ifTrue:[ |
|
5959 |
"/ exceptions: 3 non-vowels in a row: looks like an abbreviation |
|
5960 |
(self size > 4) ifTrue:[ |
|
5961 |
(firstChar == $s) ifTrue:[ |
|
5962 |
((secondChar == $c and:[thirdChar == $r]) |
|
5963 |
or:[ (secondChar == $t and:[thirdChar == $r]) ]) ifTrue:[ |
|
5964 |
(self at:4) isVowel ifTrue:[ |
|
5965 |
^ 'a' |
|
5966 |
] |
|
5967 |
] |
|
5968 |
]. |
|
5969 |
]. |
|
5970 |
"/ an abbreviation; treat x, s as vowels |
|
5971 |
(firstChar == $x or:[ firstChar == $s ]) ifTrue:[^ 'an']. |
|
5972 |
] |
|
17043 | 5973 |
]. |
608 | 5974 |
^ 'a' |
5848 | 5975 |
|
5976 |
" |
|
20012 | 5977 |
'uboot' article. |
5978 |
'xmas' article. |
|
5979 |
'alarm' article. |
|
5980 |
'baby' article. |
|
5981 |
'sql' article. |
|
5982 |
'scr' article. |
|
5983 |
'screen' article. |
|
5984 |
'scrollbar' article. |
|
5985 |
'scrs' article. |
|
5986 |
'cvs' article. |
|
5987 |
'cvssource' article. |
|
5988 |
'symbol' article. |
|
5989 |
'string' article. |
|
5990 |
'rbparser' article. |
|
19683 | 5991 |
" |
5992 |
||
5993 |
"Modified (comment): / 01-05-2016 / 10:57:25 / cg" |
|
608 | 5994 |
! |
5995 |
||
8049
ccd0e8f26439
printing - migrating towards unicodeSupport in ST-code
Claus Gittinger <cg@exept.de>
parents:
8040
diff
changeset
|
5996 |
basicStoreString |
ccd0e8f26439
printing - migrating towards unicodeSupport in ST-code
Claus Gittinger <cg@exept.de>
parents:
8040
diff
changeset
|
5997 |
"return a String for storing myself" |
ccd0e8f26439
printing - migrating towards unicodeSupport in ST-code
Claus Gittinger <cg@exept.de>
parents:
8040
diff
changeset
|
5998 |
|
ccd0e8f26439
printing - migrating towards unicodeSupport in ST-code
Claus Gittinger <cg@exept.de>
parents:
8040
diff
changeset
|
5999 |
|s n index| |
ccd0e8f26439
printing - migrating towards unicodeSupport in ST-code
Claus Gittinger <cg@exept.de>
parents:
8040
diff
changeset
|
6000 |
|
ccd0e8f26439
printing - migrating towards unicodeSupport in ST-code
Claus Gittinger <cg@exept.de>
parents:
8040
diff
changeset
|
6001 |
n := self occurrencesOf:$'. |
11320 | 6002 |
n ~~ 0 ifTrue:[ |
20012 | 6003 |
s := self species new:(n + 2 + self size). |
6004 |
s at:1 put:$'. |
|
6005 |
index := 2. |
|
6006 |
self do:[:thisChar | |
|
6007 |
(thisChar == $') ifTrue:[ |
|
6008 |
s at:index put:thisChar. |
|
6009 |
index := index + 1. |
|
6010 |
]. |
|
6011 |
s at:index put:thisChar. |
|
6012 |
index := index + 1. |
|
6013 |
]. |
|
6014 |
s at:index put:$'. |
|
6015 |
^ s |
|
9192 | 6016 |
]. |
11320 | 6017 |
|
9192 | 6018 |
^ '''' , self , '''' |
15523 | 6019 |
|
6020 |
" |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
6021 |
'''immutable'' string' asImmutableString basicStoreString |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
6022 |
'immutable string' asImmutableString basicStoreString |
15523 | 6023 |
" |
6024 |
||
6025 |
"Modified: / 14-07-2013 / 19:20:49 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
9192 | 6026 |
! |
8049
ccd0e8f26439
printing - migrating towards unicodeSupport in ST-code
Claus Gittinger <cg@exept.de>
parents:
8040
diff
changeset
|
6027 |
|
11774
6f6e2b71b607
displayOn (inspector's interface) was broken
Claus Gittinger <cg@exept.de>
parents:
11754
diff
changeset
|
6028 |
displayOn:aGCOrStream |
6f6e2b71b607
displayOn (inspector's interface) was broken
Claus Gittinger <cg@exept.de>
parents:
11754
diff
changeset
|
6029 |
"Compatibility |
6f6e2b71b607
displayOn (inspector's interface) was broken
Claus Gittinger <cg@exept.de>
parents:
11754
diff
changeset
|
6030 |
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
|
6031 |
OR: |
6f6e2b71b607
displayOn (inspector's interface) was broken
Claus Gittinger <cg@exept.de>
parents:
11754
diff
changeset
|
6032 |
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
|
6033 |
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
|
6034 |
(although the fallBack is to display its printString ...)" |
6f6e2b71b607
displayOn (inspector's interface) was broken
Claus Gittinger <cg@exept.de>
parents:
11754
diff
changeset
|
6035 |
|
6f6e2b71b607
displayOn (inspector's interface) was broken
Claus Gittinger <cg@exept.de>
parents:
11754
diff
changeset
|
6036 |
"/ what a kludge - Dolphin and Squeak mean: printOn: a stream; |
6f6e2b71b607
displayOn (inspector's interface) was broken
Claus Gittinger <cg@exept.de>
parents:
11754
diff
changeset
|
6037 |
"/ ST/X (and some old ST80's) mean: draw-yourself on a GC. |
16738 | 6038 |
(aGCOrStream isStream) ifTrue:[ |
20012 | 6039 |
self storeOn:aGCOrStream. |
6040 |
^ self |
|
11774
6f6e2b71b607
displayOn (inspector's interface) was broken
Claus Gittinger <cg@exept.de>
parents:
11754
diff
changeset
|
6041 |
]. |
6f6e2b71b607
displayOn (inspector's interface) was broken
Claus Gittinger <cg@exept.de>
parents:
11754
diff
changeset
|
6042 |
^ super displayOn:aGCOrStream |
6f6e2b71b607
displayOn (inspector's interface) was broken
Claus Gittinger <cg@exept.de>
parents:
11754
diff
changeset
|
6043 |
! |
6f6e2b71b607
displayOn (inspector's interface) was broken
Claus Gittinger <cg@exept.de>
parents:
11754
diff
changeset
|
6044 |
|
608 | 6045 |
printOn:aStream |
6046 |
"print the receiver on aStream" |
|
6047 |
||
6048 |
aStream nextPutAll:self |
|
6049 |
! |
|
6050 |
||
6051 |
printString |
|
13079
111e7e456648
Implement display with #displayOn: instead of #displayString
Stefan Vogel <sv@exept.de>
parents:
13073
diff
changeset
|
6052 |
"return a string for printing - that's myself" |
608 | 6053 |
|
6054 |
^ self |
|
3578 | 6055 |
! |
6056 |
||
6057 |
printWithQuotesDoubledOn:aStream |
|
6058 |
"put the raw storeString of myself on aStream" |
|
6059 |
||
6060 |
self do:[:thisChar | |
|
20012 | 6061 |
(thisChar == $') ifTrue:[aStream nextPut:thisChar]. |
6062 |
aStream nextPut:thisChar |
|
3578 | 6063 |
] |
6064 |
||
6065 |
"Modified: / 15.6.1998 / 17:21:17 / cg" |
|
6066 |
"Created: / 15.6.1998 / 17:22:13 / cg" |
|
12171 | 6067 |
! |
6068 |
||
15788 | 6069 |
printXmlQuotedOn:aStream |
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
6070 |
"convert aString to a valid XML string |
15788 | 6071 |
that can be used for attributes, text, comments an PIs |
6072 |
TODO: care for 16bit UNICODE string and escape chars ..." |
|
6073 |
||
6074 |
self do:[:eachChar | |
|
20012 | 6075 |
eachChar == $< ifTrue:[ |
6076 |
aStream nextPutAll:'<' "mapping needed for xml text" |
|
6077 |
] ifFalse:[ eachChar == $& ifTrue:[ |
|
6078 |
aStream nextPutAll:'&' "mapping needed for all" |
|
6079 |
] ifFalse:[ eachChar == $> ifTrue:[ |
|
6080 |
aStream nextPutAll:'>' "mapping needed for comments" |
|
6081 |
] ifFalse:[ eachChar == $' ifTrue:[ |
|
6082 |
aStream nextPutAll:''' "mapping needed for attributes" |
|
6083 |
] ifFalse:[ eachChar == $" ifTrue:[ |
|
6084 |
aStream nextPutAll:'"' "mapping needed for attributes" |
|
6085 |
] ifFalse:[ |
|
6086 |
|codePoint| |
|
6087 |
codePoint := eachChar codePoint. |
|
6088 |
(codePoint < 16r20 or:[codePoint >= 16r7F]) ifTrue:[ |
|
6089 |
aStream nextPutAll:'&#'. |
|
6090 |
codePoint printOn:aStream. |
|
6091 |
aStream nextPut:$;. |
|
6092 |
] ifFalse:[ |
|
6093 |
aStream nextPut:eachChar |
|
6094 |
]]]]]] |
|
15788 | 6095 |
]. |
6096 |
! |
|
6097 |
||
6098 |
printXmlTextQuotedOn:aStream |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
6099 |
"convert aString to a valid XML string |
15788 | 6100 |
that can be used for XML text. |
6101 |
Here line formatting characters are not escaped. |
|
6102 |
TODO: care for 16bit UNICODE string and escape chars ..." |
|
6103 |
||
6104 |
self do:[:eachChar | |
|
20012 | 6105 |
eachChar == $< ifTrue:[ |
6106 |
aStream nextPutAll:'<' "mapping needed for xml text" |
|
6107 |
] ifFalse:[ eachChar == $& ifTrue:[ |
|
6108 |
aStream nextPutAll:'&' "mapping needed for all" |
|
15788 | 6109 |
"/ ] ifFalse:[ eachChar == $> ifTrue:[ |
6110 |
"/ aStream nextPutAll:'>' "mapping needed for comments" |
|
6111 |
"/ ] ifFalse:[ eachChar == $' ifTrue:[ |
|
6112 |
"/ aStream nextPutAll:''' "mapping needed for attributes" |
|
6113 |
"/ ] ifFalse:[ eachChar == $" ifTrue:[ |
|
6114 |
"/ aStream nextPutAll:'"' "mapping needed for attributes" |
|
20012 | 6115 |
] ifFalse:[ |
6116 |
|codePoint| |
|
6117 |
codePoint := eachChar codePoint. |
|
6118 |
((codePoint < 16r20 and:[codePoint ~~ 9 and:[codePoint ~~ 10 and:[codePoint ~~ 13]]]) |
|
6119 |
or:[codePoint >= 16r7F]) ifTrue:[ |
|
6120 |
aStream nextPutAll:'&#'. |
|
6121 |
codePoint printOn:aStream. |
|
6122 |
aStream nextPut:$;. |
|
6123 |
] ifFalse:[ |
|
6124 |
aStream nextPut:eachChar |
|
6125 |
]]]"/]]] |
|
15788 | 6126 |
]. |
6127 |
! |
|
6128 |
||
12171 | 6129 |
printfWith:arg1 with:arg2 with:arg3 with:arg4 with:arg5 |
12177 | 6130 |
"Format and print the receiver with <argI> formatted in C style, |
12171 | 6131 |
as specified in the Unix C-language manual page for printf(3). |
15712 | 6132 |
Return the resulting string (i.e actually, this is more like an sprintf)." |
12177 | 6133 |
|
12171 | 6134 |
^ self printf:(Array with:arg1 with:arg2 with:arg3 with:arg4 with:arg5) |
6135 |
||
6136 |
" |
|
12177 | 6137 |
Transcript showCR:('%d %05x %08o %b' printfWith:123 with:234 with:345 with:123) |
12171 | 6138 |
" |
15788 | 6139 |
! |
6140 |
||
6141 |
xmlQuotedPrintString |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
6142 |
"convert aString to a valid XML string |
15788 | 6143 |
that can be used for attributes, text, comments an PIs |
6144 |
TODO: care for 16bit UNICODE string and escape chars ..." |
|
6145 |
||
6146 |
^ String streamContents:[:s| |
|
20012 | 6147 |
self printXmlQuotedOn:s |
15788 | 6148 |
]. |
9192 | 6149 |
! ! |
6150 |
||
11955 | 6151 |
|
9192 | 6152 |
!CharacterArray methodsFor:'queries'! |
608 | 6153 |
|
4429 | 6154 |
bitsPerCharacter |
18662 | 6155 |
"return the underlying string's bitsPerCharacter |
4429 | 6156 |
(i.e. is it a regular String or a TwoByteString)" |
6157 |
||
8457 | 6158 |
|string max| |
6159 |
||
6160 |
(string := self string) ~~ self ifTrue:[ |
|
20012 | 6161 |
^ string bitsPerCharacter |
8457 | 6162 |
]. |
6163 |
||
6164 |
max := 8. |
|
6165 |
self do:[:eachCharacter | |
|
20012 | 6166 |
max := max max:(eachCharacter bitsPerCharacter) |
9192 | 6167 |
]. |
6168 |
^ max |
|
6169 |
||
6170 |
" |
|
6171 |
'hello' bitsPerCharacter |
|
6172 |
'hello' asText allBold bitsPerCharacter |
|
6173 |
" |
|
6174 |
! |
|
8457 | 6175 |
|
19757 | 6176 |
bytesPerCharacter |
6177 |
"return the underlying string's required |
|
6178 |
bytesPerCharacter |
|
6179 |
(i.e. is it a regular String or a TwoByteString)" |
|
6180 |
||
6181 |
|string max| |
|
6182 |
||
6183 |
(string := self string) ~~ self ifTrue:[ |
|
20012 | 6184 |
^ string bytesPerCharacter |
19757 | 6185 |
]. |
6186 |
||
6187 |
max := 1. |
|
6188 |
self do:[:eachCharacter | |
|
20012 | 6189 |
max := max max:(eachCharacter bytesPerCharacter) |
19757 | 6190 |
]. |
6191 |
^ max |
|
6192 |
||
6193 |
" |
|
6194 |
'hello' bytesPerCharacter |
|
6195 |
'hello' asUnicode16String bytesPerCharacter |
|
6196 |
'hello' asText allBold bytesPerCharacter |
|
6197 |
" |
|
6198 |
! |
|
6199 |
||
18967 | 6200 |
containsNon8BitElements |
6201 |
"return true, if the underlying string contains elements larger than a single byte" |
|
6202 |
||
6203 |
|sz "{ Class:SmallInteger }"| |
|
6204 |
||
6205 |
sz := self size. |
|
6206 |
1 to:sz do:[:idx| |
|
20012 | 6207 |
(self at:idx) codePoint > 16rFF ifTrue:[ |
6208 |
^ true. |
|
6209 |
]. |
|
18967 | 6210 |
]. |
6211 |
^ false. |
|
6212 |
! |
|
6213 |
||
10903 | 6214 |
continuesWith:aString startingAt:startIndex |
6215 |
"return true, if the receiver beginning at startIndex |
|
6216 |
contains the characters in aString." |
|
6217 |
||
6218 |
|sz "{Class: SmallInteger }" |
|
6219 |
idx "{Class: SmallInteger }"| |
|
6220 |
||
6221 |
sz := aString size. |
|
6222 |
idx := startIndex. |
|
6223 |
||
6224 |
1 to:sz do:[:i | |
|
20012 | 6225 |
(self at:idx) ~~ (aString at:i) ifTrue:[^ false]. |
6226 |
idx := idx + 1 |
|
10903 | 6227 |
]. |
6228 |
^ true |
|
6229 |
||
6230 |
" |
|
6231 |
'hello world' continuesWith:'world' startingAt:6 |
|
6232 |
'hello world' continuesWith:'world' startingAt:7 |
|
6233 |
" |
|
6234 |
||
6235 |
"Created: 12.5.1996 / 15:46:40 / cg" |
|
6236 |
"Modified: 26.7.1996 / 19:08:36 / cg" |
|
6237 |
! |
|
6238 |
||
6239 |
countWords |
|
6240 |
"return the number of words, which are separated by separators" |
|
6241 |
||
6242 |
|tally "{ Class: SmallInteger }" |
|
6243 |
start "{ Class: SmallInteger }" |
|
6244 |
mySize "{ Class: SmallInteger }" |
|
6245 |
stop ch| |
|
6246 |
||
6247 |
tally := 0. |
|
6248 |
start := 1. |
|
6249 |
mySize := self size. |
|
6250 |
[start <= mySize] whileTrue:[ |
|
20012 | 6251 |
ch := self at:start. |
6252 |
ch isSeparator ifTrue:[ |
|
6253 |
start := start + 1 |
|
6254 |
] ifFalse:[ |
|
6255 |
stop := self indexOfSeparatorStartingAt:start. |
|
6256 |
(stop == 0) ifTrue:[ |
|
6257 |
stop := mySize + 1 |
|
6258 |
]. |
|
6259 |
tally := tally + 1. |
|
6260 |
start := stop |
|
6261 |
] |
|
10903 | 6262 |
]. |
6263 |
^ tally |
|
6264 |
||
6265 |
" |
|
6266 |
'hello world isnt this nice' countWords' |
|
6267 |
" |
|
6268 |
! |
|
6269 |
||
11788 | 6270 |
defaultElement |
6271 |
^ Character space |
|
6272 |
! |
|
6273 |
||
608 | 6274 |
encoding |
1311 | 6275 |
"return the strings encoding, as a symbol. |
7982 | 6276 |
Here, by default, we assume unicode-encoding. |
20006 | 6277 |
Notice, that ISO-8859-1 is a true subset of unicode, |
8874 | 6278 |
and that singleByteStrings are therefore both unicode AND |
7982 | 6279 |
8859-1 encoded." |
6280 |
||
14168 | 6281 |
^ #unicode |
608 | 6282 |
! |
6283 |
||
1375
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
6284 |
hasChangeOfEmphasis |
14405
1d0d99a95bba
comment/format in:7 methods
Claus Gittinger <cg@exept.de>
parents:
14168
diff
changeset
|
6285 |
"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
|
6286 |
i.e. any non-normal (=emphasized) characters" |
1d0d99a95bba
comment/format in:7 methods
Claus Gittinger <cg@exept.de>
parents:
14168
diff
changeset
|
6287 |
|
1375
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
6288 |
^ false |
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
6289 |
|
1381 | 6290 |
"Created: 12.5.1996 / 12:31:39 / cg" |
1375
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
6291 |
! |
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
6292 |
|
7995
8ddbb403d67b
+hasIcon and hasImage for LabelAndIcon compatibility
Claus Gittinger <cg@exept.de>
parents:
7991
diff
changeset
|
6293 |
hasIcon |
9191 | 6294 |
"for LabelAndIcon compatibility" |
6295 |
||
9192 | 6296 |
^ false |
6297 |
! |
|
6298 |
||
6299 |
hasImage |
|
6300 |
"for LabelAndIcon compatibility" |
|
6301 |
||
6302 |
^ false |
|
6303 |
! |
|
7995
8ddbb403d67b
+hasIcon and hasImage for LabelAndIcon compatibility
Claus Gittinger <cg@exept.de>
parents:
7991
diff
changeset
|
6304 |
|
1388 | 6305 |
heightOn:aGC |
19464 | 6306 |
"return the size of the receiver in device units if displayed on aGC" |
1388 | 6307 |
|
20221 | 6308 |
^ aGC deviceFont heightOf:self "font is already a device font" |
1388 | 6309 |
|
6310 |
" |
|
8874 | 6311 |
'hello world' heightOn:(View new) |
1388 | 6312 |
" |
6313 |
||
6314 |
"Created: 12.5.1996 / 20:09:29 / cg" |
|
6315 |
"Modified: 12.5.1996 / 20:32:05 / cg" |
|
6316 |
! |
|
6317 |
||
11828 | 6318 |
isLowercaseFirst |
6319 |
"return true, if the first character is a lowercase character." |
|
6320 |
||
6321 |
^ self size ~~ 0 and:[ self first isLowercase ] |
|
6322 |
||
6323 |
" |
|
12177 | 6324 |
'helloWorld' isLowercaseFirst |
6325 |
'HelloWorld' isLowercaseFirst |
|
11828 | 6326 |
" |
6327 |
! |
|
6328 |
||
6329 |
isUppercaseFirst |
|
6330 |
"return true, if the first character is an uppercase character." |
|
6331 |
||
6332 |
^ self size ~~ 0 and:[ self first isUppercase ] |
|
6333 |
||
6334 |
" |
|
12177 | 6335 |
'helloWorld' isUppercaseFirst |
6336 |
'HelloWorld' isUppercaseFirst |
|
11828 | 6337 |
" |
6338 |
! |
|
6339 |
||
15773 | 6340 |
keywords |
6341 |
"assuming the receiver is a keyword message selector, |
|
6342 |
return the individual keywords (i.e. break it up at colons) |
|
6343 |
and return these as a collection. |
|
6344 |
For binary and unary selectors, the result may be nonsense (an array containing the receiver)." |
|
6345 |
||
6346 |
|coll s part| |
|
6347 |
||
6348 |
coll := OrderedCollection new. |
|
6349 |
s := ReadStream on:self. |
|
6350 |
[s atEnd] whileFalse:[ |
|
20012 | 6351 |
part := s through:$:. |
6352 |
coll add:part |
|
15773 | 6353 |
]. |
6354 |
^ coll asArray |
|
6355 |
||
6356 |
" |
|
6357 |
#at:put: keywords |
|
6358 |
#at: keywords |
|
6359 |
#+ keywords |
|
6360 |
#size keywords |
|
6361 |
" |
|
6362 |
||
6363 |
"Modified (Comment): / 30-06-2011 / 17:46:21 / cg" |
|
6364 |
! |
|
6365 |
||
9190 | 6366 |
knownAsSymbol |
6367 |
"for now, only single character strings are allowed as symbols. |
|
6368 |
This method is redefined in String." |
|
6369 |
||
6370 |
^ false |
|
6371 |
! |
|
6372 |
||
6373 |
leftIndent |
|
6374 |
"if the receiver starts with spaces, return the number of spaces |
|
6375 |
at the left - otherwise, return 0. |
|
19464 | 6376 |
If the receiver consists of spaces only, return the receiver's size." |
9192 | 6377 |
|
6378 |
|index "{Class: SmallInteger }" |
|
6379 |
end "{Class: SmallInteger }"| |
|
6380 |
||
6381 |
index := 1. |
|
6382 |
end := self size. |
|
6383 |
[index <= end] whileTrue:[ |
|
20012 | 6384 |
(self at:index) isSeparator ifFalse:[^ index - 1]. |
6385 |
index := index + 1 |
|
8350 | 6386 |
]. |
9192 | 6387 |
^ end |
6388 |
||
6389 |
" |
|
6390 |
' hello' leftIndent |
|
6391 |
'foo ' leftIndent |
|
9190 | 6392 |
' ' leftIndent |
6393 |
" |
|
6394 |
||
6395 |
"Modified: 20.4.1996 / 19:28:43 / cg" |
|
8350 | 6396 |
! |
6397 |
||
10437 | 6398 |
nameSpaceSelectorParts |
6399 |
"Answer the namespace and baseSelector parts of a namespace selector. |
|
6400 |
Namespace selectors are those generated by sends from a method in a different |
|
6401 |
namespace; they are prefixed by ':'<ns>'::'. |
|
6402 |
You cannot easily change this algorithm here, as it is also known by the VM's lookup function. |
|
6403 |
Experimental" |
|
6404 |
||
6405 |
|nsPart selPart idx| |
|
6406 |
||
6407 |
(self at:1) == $: ifFalse:[^ Array with:'' with:self]. |
|
6408 |
idx := self indexOf:$: startingAt:3. |
|
6409 |
idx == 0 ifTrue:[^ Array with:'' with:self]. |
|
6410 |
||
6411 |
(idx+2 > self size) ifTrue:[^ Array with:'' with:self]. |
|
6412 |
(self at:idx+1) == $: ifFalse:[^ Array with:'' with:self]. |
|
6413 |
nsPart := self copyFrom:2 to:idx-1. |
|
6414 |
selPart := self copyFrom:idx+2. |
|
6415 |
^ Array with:nsPart with:selPart |
|
6416 |
||
6417 |
"test: |
|
10703
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
6418 |
self assert:('+' nameSpaceSelectorParts) = #('' '+'). |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
6419 |
self assert:(':+' nameSpaceSelectorParts) = #('' ':+'). |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
6420 |
self assert:(':Foo:+' nameSpaceSelectorParts) = #('' ':Foo:+'). |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
6421 |
|
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
6422 |
self assert:(':Foo::+' nameSpaceSelectorParts) = #('Foo' '+'). |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
6423 |
self assert:(':Foo::bar:baz:' nameSpaceSelectorParts) = #('Foo' 'bar:baz:'). |
10437 | 6424 |
" |
6425 |
||
6426 |
"Created: / 05-03-2007 / 17:16:58 / cg" |
|
6427 |
"Modified: / 06-03-2007 / 11:51:15 / cg" |
|
6428 |
! |
|
6429 |
||
9190 | 6430 |
stringSpecies |
8874 | 6431 |
"return the underlying strings bitsPerCharacter |
8037
8cce25b1059c
care for wide chars in asXXXFirst
Claus Gittinger <cg@exept.de>
parents:
8032
diff
changeset
|
6432 |
(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
|
6433 |
|
8cce25b1059c
care for wide chars in asXXXFirst
Claus Gittinger <cg@exept.de>
parents:
8032
diff
changeset
|
6434 |
|string| |
8cce25b1059c
care for wide chars in asXXXFirst
Claus Gittinger <cg@exept.de>
parents:
8032
diff
changeset
|
6435 |
|
15523 | 6436 |
string := self string. |
6437 |
string == self ifTrue:[^ self species]. |
|
8037
8cce25b1059c
care for wide chars in asXXXFirst
Claus Gittinger <cg@exept.de>
parents:
8032
diff
changeset
|
6438 |
^ string stringSpecies |
8cce25b1059c
care for wide chars in asXXXFirst
Claus Gittinger <cg@exept.de>
parents:
8032
diff
changeset
|
6439 |
|
8cce25b1059c
care for wide chars in asXXXFirst
Claus Gittinger <cg@exept.de>
parents:
8032
diff
changeset
|
6440 |
" |
8874 | 6441 |
'hello' stringSpecies |
9192 | 6442 |
'hello' asText allBold stringSpecies |
6443 |
" |
|
6444 |
! |
|
8037
8cce25b1059c
care for wide chars in asXXXFirst
Claus Gittinger <cg@exept.de>
parents:
8032
diff
changeset
|
6445 |
|
6653 | 6446 |
widthFrom:startIndex to:endIndex on:aGC |
19464 | 6447 |
"return ths size of part of the receiver in device units if displayed on aGC" |
6653 | 6448 |
|
20221 | 6449 |
^ aGC deviceFont widthOf:self from:startIndex to:endIndex |
6653 | 6450 |
|
6451 |
" |
|
8874 | 6452 |
'hello world' widthFrom:1 to:5 on:(View new) |
6453 |
'hello' widthOn:(View new) |
|
6653 | 6454 |
" |
9192 | 6455 |
! |
6653 | 6456 |
|
1388 | 6457 |
widthOn:aGC |
19464 | 6458 |
"return ths size of the receiver in device units if displayed on aGC" |
1388 | 6459 |
|
20221 | 6460 |
^ aGC deviceFont widthOf:self "font is already a device font" |
1388 | 6461 |
|
6462 |
" |
|
8874 | 6463 |
'hello world' widthOn:(View new) |
1388 | 6464 |
" |
6465 |
||
6466 |
"Created: 12.5.1996 / 20:09:29 / cg" |
|
2554 | 6467 |
"Modified: 17.4.1997 / 12:50:23 / cg" |
608 | 6468 |
! ! |
6469 |
||
6470 |
!CharacterArray methodsFor:'special string converting'! |
|
6471 |
||
16759 | 6472 |
asUnixFilenameString |
6473 |
"return a new string consisting of receiver's characters |
|
6474 |
with all \-characters replaced by /-characters. |
|
6475 |
If there are no backslashes, return the original" |
|
6476 |
||
6477 |
^ self copyReplaceAll:$\ with:$/ ifNone:self |
|
6478 |
||
6479 |
" |
|
6480 |
'hello\world' asUnixFilenameString |
|
6481 |
" |
|
6482 |
||
6483 |
"Modified: / 18.7.1998 / 22:53:02 / cg" |
|
6484 |
! |
|
6485 |
||
14768 | 6486 |
expandPlaceholders:escapeCharacter with:argArrayOrDictionary |
6487 |
"this is the generic version of the old %-escaping method, allowing for an arbitrary |
|
6488 |
escape character to be used (typically $$ or $% are effectively used). |
|
6489 |
Return a copy of the receiver, where all %i escapes are |
|
6490 |
replaced by corresponding arguments' printStrings from the argArrayOrDictionary. |
|
6491 |
I.e. 'hello %1; how is %2' expandPlaceholdersWith:#('world' 'this') results |
|
6492 |
in the new string 'hello world; how is this'. |
|
6493 |
As an extension, the argument may also be a dictionary, providing |
|
6494 |
values for symbolic keys. |
|
6495 |
In this case, %a .. %z and %(...) are also allowed. |
|
6496 |
(%1..%9 require a numeric key in the dictionary, however) |
|
6497 |
To get a '%' character, use a '%%'-escape. |
|
6498 |
To get an integer-indexed placeHolder followed by another digit, |
|
6499 |
or an index > 9, you must use %(digit). |
|
6500 |
See also bindWith:... for VisualAge compatibility." |
|
6501 |
||
6502 |
|stream| |
|
6503 |
||
18269 | 6504 |
stream := (TextStream ? CharacterWriteStream) on:(self species uninitializedNew:self size + 20). |
14768 | 6505 |
self expandPlaceholders:escapeCharacter with:argArrayOrDictionary on:stream. |
6506 |
^ stream contents. |
|
6507 |
||
6508 |
||
6509 |
" |
|
6510 |
'hello %1' expandPlaceholdersWith:#('world') |
|
6511 |
'hello %1; how is %2' expandPlaceholdersWith:#('world' 'this') |
|
6512 |
'hello %2; how is %1' expandPlaceholdersWith:#('world' 'this') |
|
6513 |
'%1 plus %2 gives %3 ' expandPlaceholdersWith:#(4 5 9) |
|
6514 |
'%%(1)0 gives %(1)0' expandPlaceholdersWith:#(123) |
|
6515 |
'%%10 gives %10' expandPlaceholdersWith:#(123) |
|
6516 |
'%%(10) gives %(10)' expandPlaceholdersWith:#(123) |
|
6517 |
'%test gives %1' expandPlaceholdersWith:#(123) |
|
6518 |
'bla %1 bla' expandPlaceholdersWith:{ 'hello' allBold } |
|
6519 |
'bla %1 bla' expandPlaceholdersWith:{ 'hello' } |
|
6520 |
" |
|
6521 |
||
6522 |
" |
|
6523 |
|dict| |
|
6524 |
||
6525 |
dict := Dictionary new. |
|
6526 |
dict at:1 put:'one'. |
|
6527 |
dict at:$a put:'AAAAA'. |
|
6528 |
dict at:$b put:[ Time now ]. |
|
6529 |
dict at:'foo' put:[ Date today ]. |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
6530 |
'hello $1 %a $b %(foo) $foo ' expandPlaceholders:$$ with:dict. |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
6531 |
'hello $1 %a $b %(foo) $foo ' expandPlaceholders:$% with:dict. |
14768 | 6532 |
" |
6533 |
||
6534 |
"Modified: 1.7.1997 / 00:53:24 / cg" |
|
6535 |
! |
|
6536 |
||
14765 | 6537 |
expandPlaceholders:escapeCharacter with:argArrayOrDictionary on:aStream |
6538 |
"this is the generic version of the old %-escaping method, allowing for an arbitrary |
|
6539 |
escape character to be used (typically $$ or $% are effectively used). |
|
18425 | 6540 |
|
14765 | 6541 |
Write the receiver to aStream, where all %i escapes are |
6542 |
replaced by corresponding arguments' printStrings from the argArrayOrDictionary. |
|
6543 |
I.e. 'hello %1; how is %2' expandPlaceholdersWith:#('world' 'this') results |
|
6544 |
in the new string 'hello world; how is this'. |
|
18425 | 6545 |
|
17192 | 6546 |
As an extension, the argument may also be a dictionary, providing values for symbolic keys. |
14765 | 6547 |
In this case, %a .. %z and %(...) are also allowed. |
6548 |
(%1..%9 require a numeric key in the dictionary, however) |
|
17192 | 6549 |
Also, the values in argArrayOrDictionary may be blocks. |
18425 | 6550 |
|
14765 | 6551 |
To get a '%' character, use a '%%'-escape. |
6552 |
To get an integer-indexed placeHolder followed by another digit, |
|
6553 |
or an index > 9, you must use %(digit). |
|
18425 | 6554 |
|
14765 | 6555 |
See also bindWith:... for VisualAge compatibility. |
6556 |
Use %<cr> to insert a CR and %<tab> to insert a TAB." |
|
6557 |
||
6558 |
|next v key |
|
6559 |
idx "{ SmallInteger }" |
|
6560 |
idx2 "{ SmallInteger }" |
|
6561 |
start "{ SmallInteger }" |
|
6562 |
stop "{ SmallInteger }"| |
|
6563 |
||
6564 |
stop := self size. |
|
6565 |
start := 1. |
|
6566 |
[start <= stop] whileTrue:[ |
|
20012 | 6567 |
idx := self indexOf:escapeCharacter startingAt:start. |
6568 |
(idx == 0 or:[idx == stop]) ifTrue:[ |
|
6569 |
aStream nextPutAll:self startingAt:start to:stop. |
|
6570 |
^ self. |
|
6571 |
]. |
|
6572 |
"found an escapeCharacter" |
|
6573 |
aStream nextPutAll:self startingAt:start to:(idx - 1). |
|
6574 |
next := self at:(idx + 1). |
|
6575 |
(next == escapeCharacter) ifTrue:[ |
|
6576 |
aStream nextPut:escapeCharacter. |
|
6577 |
] ifFalse:[ |
|
6578 |
next == $< ifTrue:[ |
|
6579 |
idx2 := self indexOf:$> startingAt:idx+2. |
|
6580 |
key := self copyFrom:idx+2 to:idx2-1. |
|
6581 |
idx := idx2 - 1. |
|
6582 |
key := key asSymbolIfInterned. |
|
6583 |
(#(cr tab nl return lf ff null) includesIdentical:key) ifTrue:[ |
|
6584 |
aStream nextPut:(Character perform:key). |
|
6585 |
]. |
|
6586 |
] ifFalse:[ |
|
6587 |
next isDigit ifTrue:[ |
|
6588 |
v := argArrayOrDictionary at:(next digitValue) ifAbsent:'' |
|
6589 |
] ifFalse:[ |
|
6590 |
next == $( ifTrue:[ |
|
6591 |
idx2 := self indexOf:$) startingAt:idx+2. |
|
6592 |
key := self copyFrom:idx+2 to:idx2-1. |
|
6593 |
idx := idx2 - 1. |
|
6594 |
(argArrayOrDictionary includesKey:key) ifTrue:[ |
|
6595 |
v := argArrayOrDictionary at:key |
|
6596 |
] ifFalse:[ |
|
6597 |
key := key asSymbolIfInterned ? key. |
|
6598 |
(argArrayOrDictionary includesKey:key) ifTrue:[ |
|
6599 |
v := argArrayOrDictionary at:key |
|
6600 |
] ifFalse:[ |
|
6601 |
(key size == 1 and:[ argArrayOrDictionary includesKey:(key at:1)]) ifTrue:[ |
|
6602 |
v := argArrayOrDictionary at:(key at:1) |
|
6603 |
] ifFalse:[ |
|
6604 |
key isNumeric ifTrue:[ |
|
6605 |
key := Integer readFrom:key onError:nil. |
|
6606 |
]. |
|
6607 |
v := argArrayOrDictionary at:key ifAbsent:'' |
|
6608 |
] |
|
6609 |
]. |
|
6610 |
]. |
|
6611 |
] ifFalse:[ |
|
6612 |
(next isLetter and:[argArrayOrDictionary isSequenceable not "is a Dictionary"]) ifTrue:[ |
|
6613 |
"so next is a non-numeric single character." |
|
6614 |
v := argArrayOrDictionary |
|
6615 |
at:next |
|
6616 |
ifAbsent:[ |
|
6617 |
"try symbol instead of character" |
|
6618 |
argArrayOrDictionary |
|
6619 |
at:next asSymbol |
|
6620 |
ifAbsent:[String with:escapeCharacter with:next]. |
|
6621 |
]. |
|
6622 |
] ifFalse:[ |
|
6623 |
v := String with:$% with:next. |
|
6624 |
]. |
|
6625 |
] |
|
6626 |
]. |
|
6627 |
"/ v notNil ifTrue:[ |
|
6628 |
v isBlock ifTrue:[ |
|
6629 |
v := v value |
|
6630 |
]. |
|
6631 |
||
6632 |
v printOn:aStream. |
|
6633 |
"/ ]. |
|
6634 |
] |
|
6635 |
]. |
|
6636 |
start := idx + 2 |
|
14765 | 6637 |
]. |
6638 |
||
6639 |
" |
|
6640 |
String streamContents:[:s| |
|
20012 | 6641 |
'hello %1' expandPlaceholders:$% with:#('world') on:s. |
6642 |
s cr. |
|
6643 |
'hello $1; how is $2' expandPlaceholders:$$ with:#('world' 'this') on:s. |
|
6644 |
s cr. |
|
6645 |
'hello %2; how is %1' expandPlaceholders:$% with:#('world' 'this') on:s. |
|
6646 |
s cr. |
|
6647 |
'%1 plus %2 gives %3 ' expandPlaceholders:$% with:#(4 5 9) on:s. |
|
6648 |
s cr. |
|
6649 |
'%%(1)0 gives %(1)0' expandPlaceholders:$% with:#(123) on:s. |
|
6650 |
s cr. |
|
6651 |
'%%10 gives %10' expandPlaceholders:$% with:#(123) on:s. |
|
6652 |
s cr. |
|
6653 |
'%%(10) gives %(10) %<cr>%<tab>next line' expandPlaceholders:$% with:#(123) on:s. |
|
6654 |
s cr. |
|
6655 |
'%%test gives %test' expandPlaceholders:$% with:#(123) on:s. |
|
6656 |
s cr. |
|
6657 |
'|%%<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
|
6658 |
] |
14765 | 6659 |
" |
6660 |
||
6661 |
" |
|
6662 |
|dict| |
|
6663 |
||
6664 |
dict := Dictionary new. |
|
6665 |
dict at:1 put:'one'. |
|
6666 |
dict at:$a put:'AAAAA'. |
|
6667 |
dict at:$b put:[ Time now ]. |
|
6668 |
String streamContents:[:s| |
|
20012 | 6669 |
'hello $1 $a $b' expandPlaceholders:$$ with:dict on:s. |
14765 | 6670 |
]. |
6671 |
" |
|
6672 |
||
18425 | 6673 |
"using blocks: |
6674 |
|dict| |
|
6675 |
||
6676 |
dict := Dictionary new. |
|
6677 |
dict at:'time' put:[Time now printString]. |
|
6678 |
dict at:'date' put:[Date today printString]. |
|
6679 |
String streamContents:[:s| |
|
20012 | 6680 |
'it is $(time) $(date)' expandPlaceholders:$$ with:dict on:s. |
18425 | 6681 |
]. |
6682 |
" |
|
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
6683 |
|
14765 | 6684 |
"Modified: / 18-11-2010 / 15:43:28 / cg" |
6685 |
! |
|
6686 |
||
9192 | 6687 |
expandPlaceholdersWith:argArrayOrDictionary |
608 | 6688 |
"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
|
6689 |
replaced by corresponding arguments' printStrings from the argArrayOrDictionary. |
608 | 6690 |
I.e. 'hello %1; how is %2' expandPlaceholdersWith:#('world' 'this') results |
6691 |
in the new string 'hello world; how is this'. |
|
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
6692 |
|
4410
2bdc0c89a42e
enhanced #exapndPlaceHolders, to allow for a dictionary arg
Claus Gittinger <cg@exept.de>
parents:
4409
diff
changeset
|
6693 |
As an extension, the argument may also be a dictionary, providing |
7313 | 6694 |
values for symbolic keys. |
4842 | 6695 |
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
|
6696 |
(%1..%9 require a numeric key in the dictionary, however) |
18425 | 6697 |
Also, the values in argArrayOrDictionary may be blocks. |
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
6698 |
|
608 | 6699 |
To get a '%' character, use a '%%'-escape. |
7313 | 6700 |
To get an integer-indexed placeHolder followed by another digit, |
6701 |
or an index > 9, you must use %(digit). |
|
608 | 6702 |
See also bindWith:... for VisualAge compatibility." |
6703 |
||
10906
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6704 |
|stream| |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6705 |
|
18269 | 6706 |
stream := (TextStream ? CharacterWriteStream) on:(self species uninitializedNew:self size + 20). |
10906
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6707 |
self expandPlaceholdersWith:argArrayOrDictionary on:stream. |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6708 |
^ stream contents. |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6709 |
|
608 | 6710 |
" |
8874 | 6711 |
'hello %1' expandPlaceholdersWith:#('world') |
6712 |
'hello %1; how is %2' expandPlaceholdersWith:#('world' 'this') |
|
6713 |
'hello %2; how is %1' expandPlaceholdersWith:#('world' 'this') |
|
6714 |
'%1 plus %2 gives %3 ' expandPlaceholdersWith:#(4 5 9) |
|
6715 |
'%%(1)0 gives %(1)0' expandPlaceholdersWith:#(123) |
|
6716 |
'%%10 gives %10' expandPlaceholdersWith:#(123) |
|
6717 |
'%%(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
|
6718 |
'%test gives %1' expandPlaceholdersWith:#(123) |
11028
cb1ee3ddd91a
expandPlaceHolders: care fr Text with emphasis
Claus Gittinger <cg@exept.de>
parents:
11018
diff
changeset
|
6719 |
'bla %1 bla' expandPlaceholdersWith:{ 'hello' allBold } |
cb1ee3ddd91a
expandPlaceHolders: care fr Text with emphasis
Claus Gittinger <cg@exept.de>
parents:
11018
diff
changeset
|
6720 |
'bla %1 bla' expandPlaceholdersWith:{ 'hello' } |
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
6721 |
('bla %1 bla' withColor:Color red) |
20012 | 6722 |
expandPlaceholdersWith:{ 'hello' } |
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
6723 |
('bla %1 bla' withColor:Color red) |
20012 | 6724 |
expandPlaceholdersWith:{ 'hello' withColor:Color blue } |
608 | 6725 |
" |
1026
455c9f609f4d
care for 16bit strings in expandPlaceHolders
Claus Gittinger <cg@exept.de>
parents:
1025
diff
changeset
|
6726 |
|
4410
2bdc0c89a42e
enhanced #exapndPlaceHolders, to allow for a dictionary arg
Claus Gittinger <cg@exept.de>
parents:
4409
diff
changeset
|
6727 |
" |
2bdc0c89a42e
enhanced #exapndPlaceHolders, to allow for a dictionary arg
Claus Gittinger <cg@exept.de>
parents:
4409
diff
changeset
|
6728 |
|dict| |
2bdc0c89a42e
enhanced #exapndPlaceHolders, to allow for a dictionary arg
Claus Gittinger <cg@exept.de>
parents:
4409
diff
changeset
|
6729 |
|
2bdc0c89a42e
enhanced #exapndPlaceHolders, to allow for a dictionary arg
Claus Gittinger <cg@exept.de>
parents:
4409
diff
changeset
|
6730 |
dict := Dictionary new. |
2bdc0c89a42e
enhanced #exapndPlaceHolders, to allow for a dictionary arg
Claus Gittinger <cg@exept.de>
parents:
4409
diff
changeset
|
6731 |
dict at:1 put:'one'. |
2bdc0c89a42e
enhanced #exapndPlaceHolders, to allow for a dictionary arg
Claus Gittinger <cg@exept.de>
parents:
4409
diff
changeset
|
6732 |
dict at:$a put:'AAAAA'. |
8794
a5df33935142
nindWith supports blocks in the value dictionary
Claus Gittinger <cg@exept.de>
parents:
8710
diff
changeset
|
6733 |
dict at:$b put:[ Time now ]. |
12316
3e78905d70a9
comment/format in: #expandPlaceholdersWith:
Claus Gittinger <cg@exept.de>
parents:
12196
diff
changeset
|
6734 |
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
|
6735 |
'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
|
6736 |
" |
2bdc0c89a42e
enhanced #exapndPlaceHolders, to allow for a dictionary arg
Claus Gittinger <cg@exept.de>
parents:
4409
diff
changeset
|
6737 |
|
2737
9f29aecea39d
care for % at end in expandPlaceHolders.
Claus Gittinger <cg@exept.de>
parents:
2736
diff
changeset
|
6738 |
"Modified: 1.7.1997 / 00:53:24 / cg" |
9192 | 6739 |
! |
6740 |
||
10906
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6741 |
expandPlaceholdersWith:argArrayOrDictionary on:aStream |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6742 |
"write the receiver to aStream, where all %i escapes are |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6743 |
replaced by corresponding arguments' printStrings from the argArrayOrDictionary. |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6744 |
I.e. 'hello %1; how is %2' expandPlaceholdersWith:#('world' 'this') results |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6745 |
in the new string 'hello world; how is this'. |
18425 | 6746 |
|
10906
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6747 |
As an extension, the argument may also be a dictionary, providing |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6748 |
values for symbolic keys. |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6749 |
In this case, %a .. %z and %(...) are also allowed. |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6750 |
(%1..%9 require a numeric key in the dictionary, however) |
18425 | 6751 |
Also, the values in argArrayOrDictionary may be blocks. |
6752 |
||
10906
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6753 |
To get a '%' character, use a '%%'-escape. |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6754 |
To get an integer-indexed placeHolder followed by another digit, |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6755 |
or an index > 9, you must use %(digit). |
18425 | 6756 |
|
10935 | 6757 |
See also bindWith:... for VisualAge compatibility. |
10939
d0ca31c1d1e9
#expandPlaceHoldersWith: handle %<cr> %<tab> etc
Stefan Vogel <sv@exept.de>
parents:
10935
diff
changeset
|
6758 |
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
|
6759 |
|
14765 | 6760 |
^ self expandPlaceholders:$% with:argArrayOrDictionary on:aStream |
10906
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6761 |
|
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6762 |
" |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6763 |
String streamContents:[:s| |
20012 | 6764 |
'hello %1' expandPlaceholdersWith:#('world') on:s. |
6765 |
s cr. |
|
6766 |
'hello %1; how is %2' expandPlaceholdersWith:#('world' 'this') on:s. |
|
6767 |
s cr. |
|
6768 |
'hello %2; how is %1' expandPlaceholdersWith:#('world' 'this') on:s. |
|
6769 |
s cr. |
|
6770 |
'%1 plus %2 gives %3 ' expandPlaceholdersWith:#(4 5 9) on:s. |
|
6771 |
s cr. |
|
6772 |
'%%(1)0 gives %(1)0' expandPlaceholdersWith:#(123) on:s. |
|
6773 |
s cr. |
|
6774 |
'%%10 gives %10' expandPlaceholdersWith:#(123) on:s. |
|
6775 |
s cr. |
|
6776 |
'%%(10) gives %(10) %<cr>%<tab>next line' expandPlaceholdersWith:#(123) on:s. |
|
6777 |
s cr. |
|
6778 |
'%test gives %1' expandPlaceholdersWith:#(123) on:s. |
|
10906
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6779 |
] |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6780 |
" |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6781 |
|
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6782 |
" |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6783 |
|dict| |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6784 |
|
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6785 |
dict := Dictionary new. |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6786 |
dict at:1 put:'one'. |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6787 |
dict at:$a put:'AAAAA'. |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6788 |
dict at:$b put:[ Time now ]. |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6789 |
String streamContents:[:s| |
20012 | 6790 |
'hello %1 %a %b' expandPlaceholdersWith:dict on:s. |
10906
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6791 |
]. |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6792 |
" |
13008
a27647c36b31
changed: #expandPlaceholdersWith:on:
Claus Gittinger <cg@exept.de>
parents:
12925
diff
changeset
|
6793 |
|
13138
d2eb0eba5907
changed: #expandPlaceholdersWith:on:
Claus Gittinger <cg@exept.de>
parents:
13137
diff
changeset
|
6794 |
"Modified: / 18-11-2010 / 15:43:28 / cg" |
10906
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6795 |
! |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6796 |
|
9230 | 6797 |
firstLine |
14405
1d0d99a95bba
comment/format in:7 methods
Claus Gittinger <cg@exept.de>
parents:
14168
diff
changeset
|
6798 |
"return the first line of a multiline string" |
1d0d99a95bba
comment/format in:7 methods
Claus Gittinger <cg@exept.de>
parents:
14168
diff
changeset
|
6799 |
|
1d0d99a95bba
comment/format in:7 methods
Claus Gittinger <cg@exept.de>
parents:
14168
diff
changeset
|
6800 |
^ self asCollectionOfSubCollectionsSeparatedBy:(Character cr) do:[:line | ^ line]. |
9230 | 6801 |
|
6802 |
" |
|
10703
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
6803 |
'hello' firstLine |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
6804 |
'1\2\3' withCRs firstLine |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
6805 |
'\1\2\3' withCRs firstLine |
9230 | 6806 |
" |
6807 |
! |
|
6808 |
||
13987 | 6809 |
tokensBasedOn:aCharacter |
6810 |
"this is an ST-80 alias for the ST/X method |
|
20012 | 6811 |
asCollectionOfSubstringsSeparatedBy:" |
13987 | 6812 |
|
6813 |
^ self asCollectionOfSubstringsSeparatedBy:aCharacter |
|
6814 |
||
6815 |
" |
|
6816 |
'hello:world:isnt:this nice' tokensBasedOn:$: |
|
6817 |
'foo,bar,baz' tokensBasedOn:$, |
|
6818 |
'/etc/passwd' asFilename readStream nextLine tokensBasedOn:$: |
|
6819 |
" |
|
6820 |
! |
|
6821 |
||
6822 |
withCEscapes |
|
16863 | 6823 |
"return a new string consisting of receiver's characters |
13987 | 6824 |
with all special and unprintable characters replaced by \X-character escapes. |
6825 |
(similar to the way C-language literal Strings are represented). |
|
6826 |
The resulting string will contain only 7-bit ascii characters. |
|
6827 |
Emphasis is not supported. |
|
6828 |
The following escapes are generated: |
|
20012 | 6829 |
\' single quote character |
6830 |
\dQuote double quote character |
|
6831 |
\r return character |
|
6832 |
\r return character |
|
6833 |
\n newline character |
|
6834 |
\t tab character |
|
6835 |
\\ the \ character itself |
|
6836 |
\xnn two digit hex number defining the characters ascii value |
|
6837 |
\unnnn four digit hex number defining the characters ascii value |
|
6838 |
\Unnnnnnnn eight digit hex number defining the characters ascii value |
|
16863 | 6839 |
This is the opposite of withoutCEscapes. |
17000 | 6840 |
|
6841 |
Sigh: this is named completely wrong (opposite naming of withCRs/witoutCRs), |
|
20012 | 6842 |
but it cannot be changed easily, as these methods are already used heavily |
13987 | 6843 |
" |
6844 |
||
6845 |
|anyEscapeNeeded out seq| |
|
6846 |
||
6847 |
" |
|
6848 |
first, check if any escape is needed and return the receiver unchanged if not |
|
6849 |
" |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
6850 |
anyEscapeNeeded := self |
20012 | 6851 |
contains:[:ch | |
6852 |
((ch codePoint between:32 and:126) not |
|
6853 |
or:[ch == $' or:[ch == $"]]) |
|
6854 |
]. |
|
13987 | 6855 |
anyEscapeNeeded ifFalse:[ ^ self ]. |
6856 |
||
6857 |
self hasChangeOfEmphasis ifTrue:[ self error:'emphasis not supported' ]. |
|
6858 |
||
18269 | 6859 |
out := WriteStream on:(String uninitializedNew:self size-1). |
13987 | 6860 |
|
6861 |
self do:[:ch | |
|
20012 | 6862 |
|cp| |
6863 |
||
6864 |
(ch == $' or:[ch == $"]) ifTrue:[ |
|
6865 |
out nextPut:$\. |
|
6866 |
out nextPut:ch. |
|
6867 |
] ifFalse:[ |
|
6868 |
(ch codePoint between:32 and:126) ifTrue:[ |
|
6869 |
out nextPut:ch |
|
6870 |
] ifFalse:[ |
|
6871 |
ch == Character return ifTrue:[ |
|
6872 |
seq := '\r' |
|
6873 |
] ifFalse:[ ch == Character nl ifTrue:[ |
|
6874 |
seq := '\n' |
|
6875 |
] ifFalse:[ ch == Character tab ifTrue:[ |
|
6876 |
seq := '\t' |
|
6877 |
] ifFalse:[ ch == $\ ifTrue:[ |
|
6878 |
seq := '\\' |
|
6879 |
] ifFalse:[ |
|
6880 |
cp := ch codePoint. |
|
6881 |
cp <= 16rFF ifTrue:[ |
|
6882 |
seq := '\x' , (cp printStringRadix:16 padTo:2) |
|
6883 |
] ifFalse:[ |
|
6884 |
cp <= 16rFFFF ifTrue:[ |
|
6885 |
seq := '\u' , (cp printStringRadix:16 padTo:4) |
|
6886 |
] ifFalse:[ |
|
6887 |
seq := '\U',(cp printStringRadix:16 padTo:8) |
|
6888 |
] |
|
6889 |
] |
|
6890 |
]]]]. |
|
6891 |
out nextPutAll:seq |
|
6892 |
]. |
|
6893 |
]. |
|
13987 | 6894 |
]. |
6895 |
^ out contents |
|
6896 |
||
6897 |
" |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
6898 |
'hello\n\tworld' withoutCEscapes. |
17000 | 6899 |
'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
|
6900 |
('hello ',(Character value:16r1234),' world') withCEscapes |
13987 | 6901 |
" |
6902 |
||
6903 |
"Created: / 25-01-2012 / 11:08:16 / cg" |
|
6904 |
! |
|
6905 |
||
9192 | 6906 |
withCRs |
17205 | 6907 |
"return a new string consisting of the receiver's characters |
16757 | 6908 |
with all \-characters replaced by cr-characters. |
6909 |
If there are no backslashes, return the original" |
|
6910 |
||
16759 | 6911 |
^ self copyReplaceAll:$\ with:(Character cr) ifNone:self |
9192 | 6912 |
|
6913 |
" |
|
6914 |
'hello\world' withCRs |
|
6915 |
" |
|
6916 |
||
6917 |
"Modified: / 18.7.1998 / 22:53:02 / cg" |
|
6918 |
! |
|
6919 |
||
6920 |
withEscapes |
|
15405 | 6921 |
<resource: #obsolete> |
13987 | 6922 |
"has been renamed; the name withEscapes is misleading" |
6923 |
||
6924 |
self obsoleteMethodWarning:'use withoutCEscapes'. |
|
6925 |
^ self withoutCEscapes. |
|
6926 |
||
6927 |
"Modified: / 25-01-2012 / 10:42:30 / cg" |
|
9192 | 6928 |
! |
6929 |
||
6930 |
withMatchEscapes |
|
4117 | 6931 |
"return a copy of the receiver with all match characters escaped |
6932 |
by $\ characters (to be usable as a match string). |
|
6933 |
Return the receiver, if there are none." |
|
6934 |
||
5342
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
6935 |
|in out c escape| |
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
6936 |
|
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
6937 |
escape := self class matchEscapeCharacter. |
4117 | 6938 |
|
6939 |
in := self readStream. |
|
7986 | 6940 |
out := WriteStream on:(self species new:self size). |
4117 | 6941 |
[in atEnd] whileFalse:[ |
20012 | 6942 |
c := in next. |
6943 |
(c == escape or:['*[#' includes:c]) ifTrue:[ |
|
6944 |
out nextPut:$\. |
|
6945 |
]. |
|
6946 |
out nextPut:c. |
|
4117 | 6947 |
]. |
6948 |
^ out contents. |
|
6949 |
||
6950 |
" |
|
8874 | 6951 |
'*foo' withMatchEscapes |
6952 |
'\*foo' withMatchEscapes |
|
6953 |
'*foo' withMatchEscapes |
|
6954 |
'\\*foo' withMatchEscapes |
|
6955 |
'foo*' withMatchEscapes |
|
6956 |
'foo\*' withMatchEscapes |
|
6957 |
'foo\' withMatchEscapes |
|
6958 |
'f*o*o' withMatchEscapes |
|
4117 | 6959 |
" |
6960 |
||
6961 |
"Modified: 2.4.1997 / 18:13:04 / cg" |
|
9192 | 6962 |
! |
6963 |
||
14992 | 6964 |
withSeparatorsCompacted |
6965 |
"return a new string with each sequence of whiteSpace replaced by a single space character. |
|
6966 |
Preserves a leading/trailing space." |
|
6967 |
||
6968 |
^ self species streamContents:[:s | |
|
20012 | 6969 |
|skipping| |
6970 |
||
6971 |
skipping := false. |
|
6972 |
1 to:self size do:[:idx | |
|
6973 |
|char| |
|
6974 |
||
6975 |
char := self at:idx. |
|
6976 |
char isSeparator ifFalse:[ |
|
6977 |
s nextPut:char. |
|
6978 |
skipping := false. |
|
6979 |
] ifTrue:[ |
|
6980 |
skipping ifFalse:[ |
|
6981 |
s nextPut:(Character space). |
|
6982 |
skipping := true |
|
6983 |
]. |
|
6984 |
] |
|
6985 |
] |
|
14992 | 6986 |
] |
6987 |
||
6988 |
" |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
6989 |
'hello wwww' withSeparatorsCompacted |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
6990 |
'hello wwww' withSeparatorsCompacted |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
6991 |
' hello wwww' withSeparatorsCompacted |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
6992 |
' hello wwww ' withSeparatorsCompacted |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
6993 |
' hello wwww ' withSeparatorsCompacted |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
6994 |
'hel lo www w' withSeparatorsCompacted |
14992 | 6995 |
" |
6996 |
! |
|
6997 |
||
18464 | 6998 |
withSeparatorsReplacedBy:replacementCharacter |
6999 |
"return a new string with each separator (whitespace) replaced by replacementCharacter. |
|
7000 |
Typically used with space as replacementCharacter" |
|
7001 |
||
7002 |
^ self species streamContents:[:s | |
|
20012 | 7003 |
self do:[:ch | |
7004 |
ch isSeparator ifTrue:[ |
|
7005 |
s nextPut:replacementCharacter |
|
7006 |
] ifFalse:[ |
|
7007 |
s nextPut:ch. |
|
7008 |
] |
|
7009 |
] |
|
18464 | 7010 |
] |
7011 |
||
7012 |
" |
|
7013 |
'hello wwww' withSeparatorsReplacedBy:$* |
|
7014 |
'hello ww ww' withSeparatorsReplacedBy:$* |
|
7015 |
' hello wwww' withSeparatorsReplacedBy:$* |
|
7016 |
'hel lo www w' withSeparatorsReplacedBy:$* |
|
7017 |
'hel |
|
7018 |
lo www |
|
7019 |
w' withSeparatorsReplacedBy:$* |
|
7020 |
" |
|
7021 |
! |
|
7022 |
||
9192 | 7023 |
withTabs |
19464 | 7024 |
"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
|
7025 |
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
|
7026 |
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
|
7027 |
otherwise a new string is returned. |
8874 | 7028 |
Limitation: only the very first spaces are replaced |
20012 | 7029 |
(i.e. if the receiver contains newLine characters, |
7030 |
no tabs are inserted after those lineBreaks)" |
|
8874 | 7031 |
|
7032 |
|idx "{ SmallInteger }" |
|
7033 |
nTabs "{ SmallInteger }" |
|
608 | 7034 |
newString| |
7035 |
||
7036 |
idx := self findFirst:[:c | (c ~~ Character space)]. |
|
7037 |
nTabs := (idx-1) // 8. |
|
5815
4acdd87a77d4
// can return negative numbers.
Stefan Vogel <sv@exept.de>
parents:
5795
diff
changeset
|
7038 |
nTabs <= 0 ifTrue:[^ self]. |
608 | 7039 |
|
7040 |
"any tabs" |
|
15523 | 7041 |
newString := self species new:(self size - (nTabs * 7)). |
608 | 7042 |
newString atAll:(1 to:nTabs) put:(Character tab). |
7043 |
newString replaceFrom:(nTabs + 1) with:self startingAt:(nTabs * 8 + 1). |
|
7044 |
^ newString |
|
7045 |
||
7046 |
" |
|
8874 | 7047 |
'12345678901234567890' withTabs |
7048 |
' 8901234567890' withTabs |
|
7049 |
' 901234567890' withTabs |
|
608 | 7050 |
' 67890' withTabs |
7051 |
' 7890' withTabs |
|
7052 |
' 890' withTabs |
|
7053 |
" |
|
9192 | 7054 |
! |
7055 |
||
7056 |
withTabsExpanded |
|
19464 | 7057 |
"return a string consisting of the receiver's characters, |
8874 | 7058 |
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
|
7059 |
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
|
7060 |
otherwise a new string is returned. |
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
678
diff
changeset
|
7061 |
This does handle multiline strings." |
608 | 7062 |
|
5741
3a6086774d38
added #withTabsExpanded: (i.e. arg to specify tab-column width)
Claus Gittinger <cg@exept.de>
parents:
5739
diff
changeset
|
7063 |
^ self withTabsExpanded:8 |
3a6086774d38
added #withTabsExpanded: (i.e. arg to specify tab-column width)
Claus Gittinger <cg@exept.de>
parents:
5739
diff
changeset
|
7064 |
|
3a6086774d38
added #withTabsExpanded: (i.e. arg to specify tab-column width)
Claus Gittinger <cg@exept.de>
parents:
5739
diff
changeset
|
7065 |
" |
8874 | 7066 |
('1' , Character tab asString , 'x') withTabsExpanded |
7067 |
('12345' , Character tab asString , 'x') withTabsExpanded |
|
7068 |
('123456' , Character tab asString , 'x') withTabsExpanded |
|
7069 |
('1234567' , Character tab asString , 'x') withTabsExpanded |
|
7070 |
('12345678' , Character tab asString , 'x') withTabsExpanded |
|
7071 |
('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
|
7072 |
|
3a6086774d38
added #withTabsExpanded: (i.e. arg to specify tab-column width)
Claus Gittinger <cg@exept.de>
parents:
5739
diff
changeset
|
7073 |
(String with:Character tab |
20012 | 7074 |
with:Character tab |
7075 |
with:$1) withTabsExpanded |
|
5741
3a6086774d38
added #withTabsExpanded: (i.e. arg to specify tab-column width)
Claus Gittinger <cg@exept.de>
parents:
5739
diff
changeset
|
7076 |
|
3a6086774d38
added #withTabsExpanded: (i.e. arg to specify tab-column width)
Claus Gittinger <cg@exept.de>
parents:
5739
diff
changeset
|
7077 |
(String with:Character tab |
20012 | 7078 |
with:$1 |
7079 |
with:Character tab |
|
7080 |
with:$2) withTabsExpanded |
|
5741
3a6086774d38
added #withTabsExpanded: (i.e. arg to specify tab-column width)
Claus Gittinger <cg@exept.de>
parents:
5739
diff
changeset
|
7081 |
|
3a6086774d38
added #withTabsExpanded: (i.e. arg to specify tab-column width)
Claus Gittinger <cg@exept.de>
parents:
5739
diff
changeset
|
7082 |
(String with:Character tab |
20012 | 7083 |
with:$1 |
7084 |
with:Character cr |
|
7085 |
with:Character tab |
|
7086 |
with:$2) withTabsExpanded |
|
5741
3a6086774d38
added #withTabsExpanded: (i.e. arg to specify tab-column width)
Claus Gittinger <cg@exept.de>
parents:
5739
diff
changeset
|
7087 |
" |
3a6086774d38
added #withTabsExpanded: (i.e. arg to specify tab-column width)
Claus Gittinger <cg@exept.de>
parents:
5739
diff
changeset
|
7088 |
|
3a6086774d38
added #withTabsExpanded: (i.e. arg to specify tab-column width)
Claus Gittinger <cg@exept.de>
parents:
5739
diff
changeset
|
7089 |
"Modified: 12.5.1996 / 13:05:10 / cg" |
9192 | 7090 |
! |
7091 |
||
7092 |
withTabsExpanded:numSpaces |
|
19464 | 7093 |
"return a string consisting of the receiver's characters, |
8874 | 7094 |
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
|
7095 |
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
|
7096 |
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
|
7097 |
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
|
7098 |
|
8874 | 7099 |
|col "{ SmallInteger }" |
1381 | 7100 |
str ch |
7101 |
dstIdx "{ SmallInteger }" |
|
7102 |
newSz "{ SmallInteger }" |
|
7103 |
sz "{ SmallInteger }" |
|
7104 |
hasEmphasis e| |
|
608 | 7105 |
|
7106 |
(self includes:(Character tab)) ifFalse:[^ self]. |
|
1381 | 7107 |
|
7108 |
sz := self size. |
|
7109 |
||
7110 |
"/ count the new size first, instead of |
|
7111 |
"/ multiple resizing (better for large strings) |
|
7112 |
||
7113 |
col := 1. newSz := 0. |
|
7114 |
1 to:sz do:[:srcIdx | |
|
20012 | 7115 |
ch := self at:srcIdx. |
7116 |
ch == Character tab ifFalse:[ |
|
7117 |
col := col + 1. |
|
7118 |
newSz := newSz + 1. |
|
7119 |
ch == Character cr ifTrue:[ |
|
7120 |
col := 1 |
|
7121 |
]. |
|
7122 |
] ifTrue:[ |
|
7123 |
(col \\ numSpaces) to:numSpaces do:[:ii | |
|
7124 |
newSz := newSz + 1. |
|
7125 |
col := col + 1 |
|
7126 |
]. |
|
7127 |
] |
|
608 | 7128 |
]. |
1381 | 7129 |
|
7130 |
str := self species new:newSz. |
|
7131 |
||
7132 |
hasEmphasis := self hasChangeOfEmphasis. |
|
7133 |
||
7134 |
col := 1. dstIdx := 1. |
|
7135 |
1 to:sz do:[:srcIdx | |
|
20012 | 7136 |
ch := self at:srcIdx. |
7137 |
||
7138 |
ch == Character tab ifFalse:[ |
|
7139 |
col := col + 1. |
|
7140 |
ch == Character cr ifTrue:[ |
|
7141 |
col := 1 |
|
7142 |
]. |
|
7143 |
hasEmphasis ifTrue:[ |
|
7144 |
e := self emphasisAt:srcIdx. |
|
7145 |
str emphasisAt:dstIdx put:e |
|
7146 |
]. |
|
7147 |
str at:dstIdx put:ch. |
|
7148 |
dstIdx := dstIdx + 1 |
|
7149 |
] ifTrue:[ |
|
7150 |
(col \\ numSpaces) to:numSpaces do:[:ii | |
|
7151 |
str at:dstIdx put:Character space. |
|
7152 |
dstIdx := dstIdx + 1. |
|
7153 |
col := col + 1 |
|
7154 |
]. |
|
7155 |
] |
|
1381 | 7156 |
]. |
7157 |
^ str |
|
608 | 7158 |
|
7159 |
" |
|
8874 | 7160 |
('1' , Character tab asString , 'x') withTabsExpanded |
7161 |
('1' , Character tab asString , 'x') withTabsExpanded:4 |
|
7162 |
('12345' , Character tab asString , 'x') withTabsExpanded |
|
7163 |
('123456' , Character tab asString , 'x') withTabsExpanded |
|
7164 |
('1234567' , Character tab asString , 'x') withTabsExpanded |
|
7165 |
('12345678' , Character tab asString , 'x') withTabsExpanded |
|
7166 |
('123456789' , Character tab asString , 'x') withTabsExpanded |
|
735
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
678
diff
changeset
|
7167 |
|
608 | 7168 |
(String with:Character tab |
20012 | 7169 |
with:Character tab |
7170 |
with:$1) withTabsExpanded |
|
608 | 7171 |
|
7172 |
(String with:Character tab |
|
20012 | 7173 |
with:$1 |
7174 |
with:Character tab |
|
7175 |
with:$2) withTabsExpanded |
|
735
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
678
diff
changeset
|
7176 |
|
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
678
diff
changeset
|
7177 |
(String with:Character tab |
20012 | 7178 |
with:$1 |
7179 |
with:Character cr |
|
7180 |
with:Character tab |
|
7181 |
with:$2) withTabsExpanded |
|
608 | 7182 |
" |
735
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
678
diff
changeset
|
7183 |
|
1381 | 7184 |
"Modified: 12.5.1996 / 13:05:10 / cg" |
9192 | 7185 |
! |
7186 |
||
13987 | 7187 |
withoutAllSpaces |
16855 | 7188 |
"return a copy of the receiver with all whitespace removed" |
7189 |
||
13987 | 7190 |
^ self asCollectionOfWords asStringWith:''. |
7191 |
"/ |col string| |
|
7192 |
"/ |
|
7193 |
"/ col := self asCollectionOfWords. |
|
7194 |
"/ string := String new. |
|
7195 |
"/ col do:[:el | |
|
7196 |
"/ string := string,el |
|
7197 |
"/ ]. |
|
7198 |
"/ ^string |
|
7199 |
||
7200 |
" |
|
7201 |
'hello wwww' withoutAllSpaces |
|
7202 |
'hel lo www w' withoutAllSpaces |
|
7203 |
" |
|
7204 |
||
7205 |
"Modified: / 18.7.1998 / 22:53:08 / cg" |
|
7206 |
! |
|
7207 |
||
7208 |
withoutCEscapes |
|
15628 | 7209 |
"return a new string consisting of receiver's characters |
13987 | 7210 |
with all \X-character escapes replaced by corresponding-characters. |
7211 |
(similar to the way C-language Strings are converted). |
|
7212 |
The following escapes are supported: |
|
20012 | 7213 |
\r return character |
7214 |
\n newline character |
|
7215 |
\b backspace character |
|
7216 |
\f formfeed character |
|
7217 |
\t tab character |
|
7218 |
\e escape character |
|
7219 |
\\ the \ character itself |
|
7220 |
\nnn three digit octal number defining the characters ascii value |
|
7221 |
\xnn two digit hex number defining the characters ascii value |
|
7222 |
\unnnn four digit hex number defining the characters unicode value |
|
7223 |
\Unnnnnnnn eight digit hex number defining the characters unicode value |
|
7224 |
\other other |
|
13987 | 7225 |
|
7226 |
Notice, that \' is NOT a valid escape, since the general syntax of |
|
7227 |
string constants is not affected by this method. |
|
7228 |
||
7229 |
Although easily implementable, this is NOT done automatically |
|
7230 |
by the compiler (due to a lack of a language standard for this). |
|
7231 |
However, the compiler may detect sends ot #withEscapes to string literals |
|
7232 |
and place a modified string constant into the binary/byte-code. |
|
7233 |
Therefore, no runtime penalty will be payed for using these escapes. |
|
7234 |
(not in pre 2.11 versions) |
|
16863 | 7235 |
|
7236 |
This is the opposite of withCEscapes. |
|
17000 | 7237 |
|
7238 |
Sigh: this is named completely wrong (opposite naming of withCRs/witoutCRs), |
|
20012 | 7239 |
but it cannot be changed easily, as these methods are already used heavily |
13987 | 7240 |
" |
7241 |
||
7242 |
|val "{ SmallInteger }" |
|
7243 |
in out nextChar nDigits| |
|
7244 |
||
7245 |
" |
|
7246 |
first, see if there is any escape; if not, return the receiver unchanged |
|
7247 |
" |
|
7248 |
(self includes:$\) ifFalse:[^ self ]. |
|
7249 |
||
7250 |
self hasChangeOfEmphasis ifTrue:[ self error:'emphasis not supported' ]. |
|
7251 |
out := CharacterWriteStream on:(String new:self size - 1). |
|
7252 |
||
7253 |
in := ReadStream on:self. |
|
7254 |
[in atEnd] whileFalse:[ |
|
20012 | 7255 |
nextChar := in next. |
7256 |
nextChar == $\ ifTrue:[ |
|
7257 |
in atEnd ifTrue:[ |
|
7258 |
] ifFalse:[ |
|
7259 |
nextChar := in next. |
|
7260 |
nextChar == $r ifTrue:[ |
|
7261 |
nextChar := Character return |
|
7262 |
] ifFalse:[ nextChar == $n ifTrue:[ |
|
7263 |
nextChar := Character nl |
|
7264 |
] ifFalse:[ nextChar == $b ifTrue:[ |
|
7265 |
nextChar := Character backspace |
|
7266 |
] ifFalse:[ nextChar == $f ifTrue:[ |
|
7267 |
nextChar := Character newPage |
|
7268 |
] ifFalse:[ nextChar == $t ifTrue:[ |
|
7269 |
nextChar := Character tab |
|
7270 |
] ifFalse:[ nextChar == $e ifTrue:[ |
|
7271 |
nextChar := Character esc |
|
7272 |
] ifFalse:[ |
|
7273 |
nextChar == $0 ifTrue:[ |
|
7274 |
val := 0. |
|
7275 |
nextChar := in peek. |
|
7276 |
nDigits := 1. |
|
7277 |
[nextChar notNil and:[nextChar isDigit and:[nDigits <= 3]]] whileTrue:[ |
|
7278 |
val := (val * 8) + nextChar digitValue. |
|
7279 |
nextChar := in nextPeek. |
|
7280 |
nDigits := nDigits + 1. |
|
7281 |
]. |
|
7282 |
nextChar := Character value:val. |
|
7283 |
] ifFalse:[ |
|
7284 |
val := 0. |
|
7285 |
nextChar == $x ifTrue:[ |
|
7286 |
2 timesRepeat:[ |
|
7287 |
nextChar := in next. |
|
7288 |
val := (val * 16) + nextChar digitValue. |
|
7289 |
]. |
|
7290 |
nextChar := Character value:val. |
|
7291 |
] ifFalse:[ |
|
7292 |
nextChar == $u ifTrue:[ |
|
7293 |
4 timesRepeat:[ |
|
7294 |
nextChar := in next. |
|
7295 |
val := (val * 16) + nextChar digitValue. |
|
7296 |
]. |
|
7297 |
nextChar := Character value:val. |
|
7298 |
] ifFalse:[ |
|
7299 |
nextChar == $U ifTrue:[ |
|
7300 |
8 timesRepeat:[ |
|
7301 |
nextChar := in next. |
|
7302 |
val := (val * 16) + nextChar digitValue. |
|
7303 |
]. |
|
7304 |
nextChar := Character value:val. |
|
7305 |
] |
|
7306 |
] |
|
7307 |
] |
|
7308 |
] |
|
7309 |
]]]]]]. |
|
7310 |
]. |
|
7311 |
]. |
|
7312 |
out nextPut:nextChar. |
|
13987 | 7313 |
]. |
7314 |
^ out contents |
|
7315 |
||
7316 |
" |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
7317 |
'hello world' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
7318 |
'hello\world' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
7319 |
'hello\world\' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
7320 |
'hello world\' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
7321 |
'hello\tworld' withoutCEscapes |
13987 | 7322 |
'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
|
7323 |
'hello\tworld\n' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
7324 |
'hello\010world' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
7325 |
'hello\r\nworld' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
7326 |
'hello\r\n\x08world' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
7327 |
'0\x080' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
7328 |
'0\u12340' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
7329 |
'0\U123456780' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
7330 |
'0\0a' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
7331 |
'0\00a' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
7332 |
'0\000a' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
7333 |
'0\0000a' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
7334 |
'0\00000a' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
7335 |
'0\03770' withoutCEscapes |
13987 | 7336 |
" |
7337 |
||
7338 |
"Created: / 25-01-2012 / 10:41:44 / cg" |
|
7339 |
! |
|
7340 |
||
9192 | 7341 |
withoutCRs |
19464 | 7342 |
"return a new collection consisting of receiver's elements |
9192 | 7343 |
with all cr-characters replaced by \-characters. |
7344 |
This is the reverse operation of withCRs." |
|
7345 |
||
16759 | 7346 |
^ self copyReplaceAll:(Character cr) with:$\ ifNone:self |
12319
18b80c2ce1e3
oops #withoutSeparators #withoutSpaces were noops
Claus Gittinger <cg@exept.de>
parents:
12317
diff
changeset
|
7347 |
|
9192 | 7348 |
" |
7349 |
'hello |
|
7350 |
world' withoutCRs |
|
7351 |
" |
|
7352 |
||
7353 |
"Modified: / 18.7.1998 / 22:53:08 / cg" |
|
7354 |
! |
|
7355 |
||
7356 |
withoutLeadingSeparators |
|
608 | 7357 |
"return a copy of myself without leading separators. |
7358 |
Notice: this does remove tabs, newline or any other whitespace. |
|
7359 |
Returns an empty string, if the receiver consist only of whitespace." |
|
7360 |
||
7361 |
|index| |
|
7362 |
||
7363 |
index := self indexOfNonSeparatorStartingAt:1. |
|
7364 |
index ~~ 0 ifTrue:[ |
|
20012 | 7365 |
index == 1 ifTrue:[ |
7366 |
^ self |
|
7367 |
]. |
|
7368 |
^ self copyFrom:index |
|
608 | 7369 |
]. |
7370 |
^ '' |
|
7371 |
||
7372 |
" |
|
8874 | 7373 |
' foo ' withoutLeadingSeparators |
7374 |
'foo ' withoutLeadingSeparators |
|
7375 |
' foo' withoutLeadingSeparators |
|
7376 |
' ' withoutLeadingSeparators |
|
7377 |
'foo' withoutLeadingSeparators |
|
7378 |
(' ' , Character tab asString , ' foo ') withoutLeadingSeparators inspect |
|
608 | 7379 |
" |
9192 | 7380 |
! |
7381 |
||
7382 |
withoutMatchEscapes |
|
4117 | 7383 |
"return a copy of the receiver with all $\ removed or |
7384 |
the receiver, if there are none." |
|
7385 |
||
5342
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
7386 |
|in out c escape| |
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
7387 |
|
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
7388 |
escape := self class matchEscapeCharacter. |
4117 | 7389 |
|
7390 |
in := self readStream. |
|
8222
f2c454a9a038
replaced '' writeStream by String writeStream
Claus Gittinger <cg@exept.de>
parents:
8124
diff
changeset
|
7391 |
out := self species writeStream. |
4117 | 7392 |
[in atEnd] whileFalse:[ |
20012 | 7393 |
c := in next. |
7394 |
c == escape ifTrue:[ |
|
7395 |
in atEnd ifFalse:[ |
|
7396 |
c := in next. |
|
7397 |
] |
|
7398 |
]. |
|
7399 |
out nextPut:c. |
|
4117 | 7400 |
]. |
7401 |
^ out contents. |
|
7402 |
||
7403 |
" |
|
8874 | 7404 |
'*foo' withoutMatchEscapes |
7405 |
'\*foo' withoutMatchEscapes |
|
7406 |
'*foo' withoutMatchEscapes |
|
7407 |
'\\*foo' withoutMatchEscapes |
|
7408 |
'foo*' withoutMatchEscapes |
|
7409 |
'foo\*' withoutMatchEscapes |
|
7410 |
'foo\' withoutMatchEscapes |
|
7411 |
'f\*o\*o' withoutMatchEscapes |
|
4117 | 7412 |
" |
7413 |
||
7414 |
"Modified: 30.6.1997 / 13:40:23 / cg" |
|
9192 | 7415 |
! |
7416 |
||
7417 |
withoutPrefix:aString |
|
19677 | 7418 |
"if the receiver startsWith aString, return a copy without it. |
9192 | 7419 |
Otherwise return the receiver" |
7420 |
||
7421 |
(self startsWith:aString) ifTrue:[ |
|
20012 | 7422 |
^ self copyFrom:aString size+1 |
9192 | 7423 |
]. |
7424 |
^ self |
|
7425 |
||
7426 |
" |
|
7427 |
'helloworld' withoutPrefix:'hello' |
|
7428 |
'helloworld' withoutPrefix:'foo' |
|
7429 |
" |
|
19677 | 7430 |
|
7431 |
"Modified: / 30-04-2016 / 10:01:00 / cg" |
|
9192 | 7432 |
! |
7433 |
||
10671 | 7434 |
withoutQuotes |
17000 | 7435 |
"/ remove quotes ($" and $') from the front and end of myself (if matching)" |
7436 |
||
7437 |
|firstChar| |
|
7438 |
||
17024 | 7439 |
self isEmpty ifTrue:[^ self]. |
7440 |
||
17000 | 7441 |
firstChar := self first. |
7442 |
((firstChar == $") or:[firstChar == $']) ifFalse:[^ self]. |
|
7443 |
||
7444 |
self last == firstChar ifTrue:[ |
|
20012 | 7445 |
^ self copyFrom:2 to:(self size-1) |
10671 | 7446 |
]. |
17000 | 7447 |
^ self |
7448 |
||
7449 |
"/ |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
7450 |
"/ '"hello"' withoutQuotes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
7451 |
"/ '''hello''' withoutQuotes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
7452 |
"/ 'hello' withoutQuotes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
7453 |
"/ '"hello' withoutQuotes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
7454 |
"/ 'hello"' withoutQuotes |
17000 | 7455 |
"/ |
10671 | 7456 |
! |
7457 |
||
9192 | 7458 |
withoutSeparators |
608 | 7459 |
"return a copy of myself without leading and trailing whitespace. |
4156 | 7460 |
(but whiteSpace in-between is preserved) |
608 | 7461 |
Whitespace is space, tab, newline, formfeed. |
7462 |
Use withoutSpaces, if you want to remove spaces only." |
|
7463 |
||
12319
18b80c2ce1e3
oops #withoutSeparators #withoutSpaces were noops
Claus Gittinger <cg@exept.de>
parents:
12317
diff
changeset
|
7464 |
^ self trimForWhich:[:ch | ch isSeparator] |
608 | 7465 |
|
7466 |
" |
|
8874 | 7467 |
' foo ' withoutSeparators |
7468 |
' foo' withoutSeparators |
|
7469 |
'foo ' withoutSeparators |
|
7470 |
' ' withoutSeparators |
|
7471 |
(' foo' , Character tab asString , ' ') withoutSeparators inspect |
|
12319
18b80c2ce1e3
oops #withoutSeparators #withoutSpaces were noops
Claus Gittinger <cg@exept.de>
parents:
12317
diff
changeset
|
7472 |
' foo ' asUnicode16String withoutSeparators |
608 | 7473 |
" |
9192 | 7474 |
! |
7475 |
||
7476 |
withoutSpaces |
|
608 | 7477 |
"return a copy of myself without leading and trailing spaces. |
4156 | 7478 |
(but spaces in-between are preserved) |
608 | 7479 |
Notice: this does NOT remove tabs, newline or any other whitespace. |
7480 |
Use withoutSeparators for this." |
|
7481 |
||
12319
18b80c2ce1e3
oops #withoutSeparators #withoutSpaces were noops
Claus Gittinger <cg@exept.de>
parents:
12317
diff
changeset
|
7482 |
^ self trimForWhich:[:ch | ch == Character space] |
608 | 7483 |
|
7484 |
" |
|
8874 | 7485 |
' foo ' withoutSpaces |
7486 |
'foo ' withoutSpaces |
|
7487 |
' foo' withoutSpaces |
|
7488 |
' ' withoutSpaces |
|
7489 |
'a b' withoutSpaces |
|
7490 |
(' foo' , Character tab asString , ' ') withoutSpaces inspect |
|
608 | 7491 |
" |
9192 | 7492 |
! |
7493 |
||
7494 |
withoutTrailingSeparators |
|
915
1ac1c7e942f4
added withoutTrailingSeparators
Claus Gittinger <cg@exept.de>
parents:
828
diff
changeset
|
7495 |
"return a copy of myself without trailing separators. |
1ac1c7e942f4
added withoutTrailingSeparators
Claus Gittinger <cg@exept.de>
parents:
828
diff
changeset
|
7496 |
Notice: this does remove tabs, newline or any other whitespace. |
1ac1c7e942f4
added withoutTrailingSeparators
Claus Gittinger <cg@exept.de>
parents:
828
diff
changeset
|
7497 |
Returns an empty string, if the receiver consist only of whitespace." |
1ac1c7e942f4
added withoutTrailingSeparators
Claus Gittinger <cg@exept.de>
parents:
828
diff
changeset
|
7498 |
|
12188 | 7499 |
^ self withoutTrailingForWhich:[:ch | ch isSeparator] |
915
1ac1c7e942f4
added withoutTrailingSeparators
Claus Gittinger <cg@exept.de>
parents:
828
diff
changeset
|
7500 |
|
1ac1c7e942f4
added withoutTrailingSeparators
Claus Gittinger <cg@exept.de>
parents:
828
diff
changeset
|
7501 |
" |
8874 | 7502 |
' foo ' withoutTrailingSeparators |
7503 |
'foo ' withoutTrailingSeparators |
|
7504 |
' foo' withoutTrailingSeparators |
|
7505 |
' ' withoutTrailingSeparators |
|
7506 |
'foo' withoutTrailingSeparators |
|
7507 |
(' ' , Character tab asString , ' foo ') withoutTrailingSeparators inspect |
|
7508 |
(' foo' , Character tab asString) withoutTrailingSeparators inspect |
|
915
1ac1c7e942f4
added withoutTrailingSeparators
Claus Gittinger <cg@exept.de>
parents:
828
diff
changeset
|
7509 |
" |
9192 | 7510 |
! ! |
7511 |
||
7512 |
!CharacterArray methodsFor:'substring searching'! |
|
7513 |
||
15055 | 7514 |
findRangeOfString:subString |
7515 |
"find a substring. if found, return the start- and endIndex; |
|
7516 |
if not found, return an empty interval." |
|
7517 |
||
7518 |
^ self rangeOfSubCollection:subString startingAt:1 ifAbsent:[0 to:-1] caseSensitive:true |
|
7519 |
||
7520 |
" |
|
7521 |
'hello world' findRangeOfString:'llo' |
|
7522 |
'hello world' findRangeOfString:'ole' |
|
7523 |
" |
|
7524 |
! |
|
7525 |
||
9192 | 7526 |
findString:subString |
7527 |
"find a substring. if found, return the index; |
|
7528 |
if not found, return 0." |
|
7529 |
||
7530 |
^ self indexOfSubCollection:subString startingAt:1 ifAbsent:0 caseSensitive:true |
|
7531 |
||
7532 |
" |
|
7533 |
'hello world' findString:'llo' |
|
7534 |
'hello world' findString:'ole' |
|
7535 |
" |
|
7536 |
! |
|
608 | 7537 |
|
15734 | 7538 |
findString:subString caseSensitive:caseSensitive |
7539 |
"find a substring. if found, return the index; |
|
7540 |
if not found, return 0." |
|
7541 |
||
7542 |
^ self indexOfSubCollection:subString startingAt:1 ifAbsent:0 caseSensitive:caseSensitive |
|
7543 |
||
7544 |
" |
|
7545 |
'hello world' findString:'LLo' caseSensitive:true |
|
7546 |
'hello world' findString:'LLo' caseSensitive:false |
|
7547 |
" |
|
7548 |
! |
|
7549 |
||
608 | 7550 |
findString:subString ifAbsent:exceptionBlock |
7551 |
"find a substring. If found, return the index; |
|
7552 |
if not found, return the result of evaluating exceptionBlock." |
|
7553 |
||
9087
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
7554 |
^ self indexOfSubCollection:subString startingAt:1 ifAbsent:exceptionBlock caseSensitive:true |
608 | 7555 |
! |
7556 |
||
11018 | 7557 |
findString:subString ignoreCase:ignoreCase |
15734 | 7558 |
<resource: #obsolete> |
11018 | 7559 |
"find a substring. if found, return the index; |
7560 |
if not found, return 0." |
|
7561 |
||
7562 |
^ self indexOfSubCollection:subString startingAt:1 ifAbsent:0 caseSensitive:ignoreCase not |
|
7563 |
||
7564 |
" |
|
7565 |
'hello world' findString:'LLo' ignoreCase:false |
|
7566 |
'hello world' findString:'LLo' ignoreCase:true |
|
7567 |
" |
|
7568 |
! |
|
7569 |
||
608 | 7570 |
findString:subString startingAt:index |
7571 |
"find a substring, starting at index. if found, return the index; |
|
7572 |
if not found, return 0." |
|
7573 |
||
9087
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
7574 |
^ self indexOfSubCollection:subString startingAt:index ifAbsent:0 caseSensitive:true |
608 | 7575 |
|
7576 |
" |
|
9192 | 7577 |
'hello yello' findString:'llo' startingAt:1 |
7578 |
'hello yello' findString:'llo' startingAt:5 |
|
7579 |
'hello yello' findString:'llo' startingAt:15 |
|
7580 |
" |
|
7581 |
! |
|
608 | 7582 |
|
7583 |
findString:subString startingAt:index ifAbsent:exceptionBlock |
|
7584 |
"find a substring, starting at index. if found, return the index; |
|
7585 |
if not found, return the result of evaluating exceptionBlock." |
|
7586 |
||
9087
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
7587 |
^ self indexOfSubCollection:subString startingAt:index ifAbsent:exceptionBlock caseSensitive:true |
608 | 7588 |
! |
7589 |
||
12177 | 7590 |
findString:subString startingAt:index ifAbsent:exceptionBlock caseSensitive:caseSensitive |
10929 | 7591 |
"find a substring, starting at index. if found, return the index; |
7592 |
if not found, return the result of evaluating exceptionBlock." |
|
7593 |
||
7594 |
^ self indexOfSubCollection:subString startingAt:index ifAbsent:exceptionBlock caseSensitive:caseSensitive |
|
7595 |
! |
|
7596 |
||
608 | 7597 |
includesString:aString |
7598 |
"return true, if a substring is contained in the receiver" |
|
7599 |
||
9087
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
7600 |
^ (self indexOfSubCollection:aString startingAt:1 ifAbsent:0 caseSensitive:true) ~~ 0 |
608 | 7601 |
|
7602 |
" |
|
12177 | 7603 |
'hello world' includesString:'hel' |
8874 | 7604 |
'hello world' includesString:'rld' |
9192 | 7605 |
'hello world' includesString:'llo' |
7606 |
'hello world' includesString:'LLO' |
|
12177 | 7607 |
'hello world' includesString:'' |
9192 | 7608 |
" |
7609 |
! |
|
9191 | 7610 |
|
7611 |
includesString:aString caseSensitive:caseSensitive |
|
9087
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
7612 |
"return true, if a substring is contained in the receiver" |
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
7613 |
|
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
7614 |
^ (self indexOfSubCollection:aString startingAt:1 ifAbsent:0 caseSensitive:caseSensitive) ~~ 0 |
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
7615 |
|
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
7616 |
" |
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
7617 |
'hello world' includesString:'hel' caseSensitive:true |
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
7618 |
'hello world' includesString:'HEL' caseSensitive:true |
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
7619 |
'hello world' includesString:'HEL' caseSensitive:false |
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
7620 |
|
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
7621 |
'hello world' includesString:'wor' caseSensitive:true |
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
7622 |
'hello world' includesString:'WOR' caseSensitive:true |
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
7623 |
'hello world' includesString:'WOR' caseSensitive:false |
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
7624 |
" |
9192 | 7625 |
! |
7626 |
||
17486 | 7627 |
indexOfString: aString |
7628 |
"VSE and V'age compatibility" |
|
7629 |
"find a substring. If found, return the index; if not found, return 0." |
|
7630 |
||
18939 | 7631 |
^ self indexOfSubCollection: aString startingAt:1 ifAbsent:[0] |
7632 |
! |
|
7633 |
||
7634 |
indexOfString:aString ifAbsent:exceptionValue |
|
7635 |
"VSE and V'age compatibility" |
|
7636 |
"find a substring. If found, return the index; if not found, return 0." |
|
7637 |
||
7638 |
^ self indexOfSubCollection: aString startingAt:1 ifAbsent:exceptionValue |
|
7639 |
||
7640 |
" |
|
7641 |
'abcdefabcdef' indexOfString:'fab' ifAbsent:[999] |
|
7642 |
'abcdefabcdef' indexOfString:'xxx' ifAbsent:[999] |
|
7643 |
" |
|
17486 | 7644 |
! |
7645 |
||
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
7646 |
indexOfString:aString startingAt:startIndex |
18721 | 7647 |
"VSE and V'age compatibility" |
7648 |
"find a substring. If found, return the index; if not found, return 0." |
|
7649 |
||
7650 |
^ self indexOfSubCollection:aString startingAt:startIndex ifAbsent:[0] |
|
7651 |
! |
|
7652 |
||
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
7653 |
indexOfString:aString startingAt:startIndex ifAbsent:exceptionalValue |
18721 | 7654 |
"VSE and V'age compatibility" |
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
7655 |
"find a substring. |
18721 | 7656 |
If found, return the index; if not found, the value from exceptionalValue." |
7657 |
||
7658 |
^ self indexOfSubCollection:aString startingAt:startIndex ifAbsent:exceptionalValue |
|
7659 |
! |
|
7660 |
||
18671 | 7661 |
indexOfSubCollection:subString caseSensitive:caseSensitive |
7662 |
"find a substring, starting at index. if found, return the index; |
|
7663 |
if not found, return the result of evaluating exceptionBlock. |
|
7664 |
This is a q&d hack - not very efficient" |
|
7665 |
||
7666 |
^ self indexOfSubCollection:subString startingAt:1 ifAbsent:[0] caseSensitive:caseSensitive. |
|
7667 |
! |
|
7668 |
||
9192 | 7669 |
indexOfSubCollection:subString startingAt:index ifAbsent:exceptionBlock |
7670 |
"find a substring, starting at index. if found, return the index; |
|
7671 |
if not found, return the result of evaluating exceptionBlock. |
|
7672 |
This is a q&d hack - not very efficient" |
|
7673 |
||
7674 |
^ self indexOfSubCollection:subString startingAt:index ifAbsent:exceptionBlock caseSensitive:true. |
|
7675 |
! |
|
7676 |
||
7677 |
indexOfSubCollection:subString startingAt:index ifAbsent:exceptionBlock caseSensitive:caseSensitive |
|
9087
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
7678 |
"find a substring, starting at index. if found, return the index; |
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
7679 |
if not found, return the result of evaluating exceptionBlock. |
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
7680 |
This is a q&d hack - not very efficient" |
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
7681 |
|
608 | 7682 |
|firstChar found |
7683 |
startIndex "{ Class: SmallInteger }" |
|
7684 |
subSize "{ Class: SmallInteger }" |
|
7685 |
mySize "{ Class: SmallInteger }" |
|
10703
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
7686 |
runIdx "{ Class: SmallInteger }" |
9087
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
7687 |
tester| |
608 | 7688 |
|
7689 |
subSize := subString size. |
|
12177 | 7690 |
subSize == 0 ifTrue:[ |
20012 | 7691 |
subString isString ifFalse:[ |
7692 |
self error:'non string argument' mayProceed:true. |
|
7693 |
]. |
|
7694 |
"empty string does not match" |
|
7695 |
^ 0. |
|
7696 |
"empty string matches" |
|
15433 | 7697 |
"/ ^ index |
6122
a98958f1c85f
handle non-string arg to indexOfSubString:
Claus Gittinger <cg@exept.de>
parents:
6119
diff
changeset
|
7698 |
]. |
9087
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
7699 |
|
608 | 7700 |
mySize := self size. |
7701 |
firstChar := subString at:1. |
|
9087
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
7702 |
caseSensitive ifTrue:[ |
20012 | 7703 |
tester := [:c1 :c2 | c1 = c2 ]. |
7704 |
startIndex := self indexOf:firstChar startingAt:index. |
|
9087
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
7705 |
] ifFalse:[ |
20012 | 7706 |
tester := [:c1 :c2 | c1 sameAs: c2 ]. |
7707 |
startIndex := self findFirst:[:c | c sameAs:firstChar] startingAt:index. |
|
9087
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
7708 |
]. |
608 | 7709 |
[startIndex == 0] whileFalse:[ |
20012 | 7710 |
runIdx := startIndex. |
7711 |
found := true. |
|
7712 |
1 to:subSize do:[:i | |
|
7713 |
runIdx > mySize ifTrue:[ |
|
7714 |
found := false |
|
7715 |
] ifFalse:[ |
|
7716 |
(tester value:(subString at:i) value:(self at:runIdx)) ifFalse:[ |
|
7717 |
found := false |
|
7718 |
] |
|
7719 |
]. |
|
7720 |
runIdx := runIdx + 1 |
|
7721 |
]. |
|
7722 |
found ifTrue:[ |
|
7723 |
^ startIndex |
|
7724 |
]. |
|
7725 |
caseSensitive ifTrue:[ |
|
7726 |
startIndex := self indexOf:firstChar startingAt:(startIndex + 1) |
|
7727 |
] ifFalse:[ |
|
7728 |
startIndex := self findFirst:[:c | c sameAs:firstChar] startingAt:(startIndex + 1). |
|
7729 |
]. |
|
608 | 7730 |
]. |
7731 |
^ exceptionBlock value |
|
1005
4507487e0be6
more on encoding/decoding (still unfinished)
Claus Gittinger <cg@exept.de>
parents:
997
diff
changeset
|
7732 |
|
4507487e0be6
more on encoding/decoding (still unfinished)
Claus Gittinger <cg@exept.de>
parents:
997
diff
changeset
|
7733 |
"Modified: 23.2.1996 / 15:35:15 / cg" |
9192 | 7734 |
! |
7735 |
||
17191 | 7736 |
indexOfSubCollection:subString startingAt:index ifAbsent:exceptionBlock caseSensitive:caseSensitive ignoreDiacritics:ignoreDiacritics |
7737 |
"find a substring, starting at index. if found, return the index; |
|
7738 |
if not found, return the result of evaluating exceptionBlock. |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
7739 |
This is a q&d hack - not very efficient |
17191 | 7740 |
(see implementation in string, for a much faster algorithm)" |
7741 |
||
7742 |
|firstChar found |
|
7743 |
startIndex "{ Class: SmallInteger }" |
|
7744 |
subSize "{ Class: SmallInteger }" |
|
7745 |
mySize "{ Class: SmallInteger }" |
|
7746 |
runIdx "{ Class: SmallInteger }" |
|
7747 |
tester charMap firstCharMapped findNextIndex| |
|
7748 |
||
7749 |
subSize := subString size. |
|
7750 |
subSize == 0 ifTrue:[ |
|
20012 | 7751 |
subString isString ifFalse:[ |
7752 |
self error:'non string argument' mayProceed:true. |
|
7753 |
]. |
|
7754 |
"empty string does not match" |
|
7755 |
^ 0. |
|
7756 |
"empty string matches" |
|
17191 | 7757 |
"/ ^ index |
7758 |
]. |
|
7759 |
||
7760 |
mySize := self size. |
|
7761 |
firstChar := subString at:1. |
|
7762 |
ignoreDiacritics ifTrue:[ |
|
20012 | 7763 |
caseSensitive ifTrue:[ |
7764 |
charMap := [:ch | ch asLowercase withoutDiacritics]. |
|
7765 |
] ifFalse:[ |
|
7766 |
charMap := [:ch | ch withoutDiacritics]. |
|
7767 |
]. |
|
7768 |
tester := [:c1 :c2 | (charMap value:c1) = (charMap value:c2) ]. |
|
7769 |
firstCharMapped := (charMap value:firstChar). |
|
7770 |
findNextIndex := [:index | self findFirst:[:ch | (charMap value:ch) = firstCharMapped] startingAt:index]. |
|
17191 | 7771 |
] ifFalse:[ |
20012 | 7772 |
caseSensitive ifTrue:[ |
7773 |
tester := [:c1 :c2 | c1 = c2 ]. |
|
7774 |
findNextIndex := [:index | self indexOf:firstChar startingAt:index]. |
|
7775 |
] ifFalse:[ |
|
7776 |
tester := [:c1 :c2 | c1 sameAs: c2 ]. |
|
7777 |
findNextIndex := [:index | self findFirst:[:c | c sameAs:firstChar] startingAt:index]. |
|
7778 |
]. |
|
17191 | 7779 |
]. |
7780 |
startIndex := findNextIndex value:index. |
|
7781 |
[startIndex == 0] whileFalse:[ |
|
20012 | 7782 |
runIdx := startIndex. |
7783 |
found := true. |
|
7784 |
1 to:subSize do:[:i | |
|
7785 |
runIdx > mySize ifTrue:[ |
|
7786 |
found := false |
|
7787 |
] ifFalse:[ |
|
7788 |
(tester value:(subString at:i) value:(self at:runIdx)) ifFalse:[ |
|
7789 |
found := false |
|
7790 |
] |
|
7791 |
]. |
|
7792 |
runIdx := runIdx + 1 |
|
7793 |
]. |
|
7794 |
found ifTrue:[ |
|
7795 |
^ startIndex |
|
7796 |
]. |
|
7797 |
startIndex := findNextIndex value:(startIndex + 1) |
|
17191 | 7798 |
]. |
7799 |
^ exceptionBlock value |
|
7800 |
||
7801 |
" |
|
17600 | 7802 |
'bla depot bla dépots' indexOfSubCollection:'dep' startingAt:1 ifAbsent:0 caseSensitive:false ignoreDiacritics:true. 5 |
7803 |
'bla depot bla dépots' indexOfSubCollection:'dep' startingAt:6 ifAbsent:0 caseSensitive:false ignoreDiacritics:true. 0 |
|
7804 |
||
7805 |
'bla depot bla dépots' indexOfSubCollection:'dep' startingAt:1 ifAbsent:0 caseSensitive:false ignoreDiacritics:false. 5 |
|
7806 |
'bla depot bla dépots' indexOfSubCollection:'dep' startingAt:6 ifAbsent:0 caseSensitive:false ignoreDiacritics:false. 0 |
|
17191 | 7807 |
" |
7808 |
! |
|
7809 |
||
18939 | 7810 |
lastIndexOfString:aString |
7811 |
"VSE and V'age compatibility" |
|
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
7812 |
"find the last occurrence of substring. |
18939 | 7813 |
If found, return the index; if not found, return 0." |
7814 |
||
7815 |
^ self lastIndexOfSubCollection:aString startingAt:(self size-aString size+1) ifAbsent:[0] |
|
7816 |
||
7817 |
" 123456789012 |
|
7818 |
'abcdefabcdef' lastIndexOfString:'abc' |
|
7819 |
'abcdefabcdef' lastIndexOfString:'abc' startingAt:6 |
|
7820 |
'abcdefabcdef' lastIndexOfString:'xxx' startingAt:6 |
|
7821 |
" |
|
7822 |
! |
|
7823 |
||
7824 |
lastIndexOfString:aString ifAbsent:exceptionValue |
|
7825 |
"VSE and V'age compatibility" |
|
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
7826 |
"find the last occurrence of substring. |
18939 | 7827 |
If found, return the index; if not found, return 0." |
7828 |
||
7829 |
^ self lastIndexOfSubCollection:aString startingAt:(self size-aString size+1) ifAbsent:exceptionValue |
|
7830 |
||
7831 |
" 123456789012 |
|
7832 |
'abcdefabcdef' lastIndexOfString:'abc' ifAbsent:[999] |
|
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
7833 |
'abcdefabcdef' lastIndexOfString:'xxx' ifAbsent:[999] |
18939 | 7834 |
" |
7835 |
! |
|
7836 |
||
7837 |
lastIndexOfString:aString startingAt:startIndex |
|
7838 |
"VSE and V'age compatibility" |
|
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
7839 |
"find the last occurrence of a substring. |
18939 | 7840 |
If found, return the index; if not found, return 0." |
7841 |
||
7842 |
^ self lastIndexOfSubCollection:aString startingAt:startIndex ifAbsent:[0] |
|
7843 |
||
7844 |
" 123456789012 |
|
7845 |
'abcdefabcdef' lastIndexOfString:'abc' |
|
7846 |
'abcdefabcdef' lastIndexOfString:'abc' startingAt:6 |
|
7847 |
" |
|
7848 |
! |
|
7849 |
||
7850 |
lastIndexOfString:aString startingAt:startIndex ifAbsent:exceptionValue |
|
7851 |
"VSE and V'age compatibility" |
|
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
7852 |
"find the last occurrence of a substring. |
18939 | 7853 |
If found, return the index; if not found, return 0." |
7854 |
||
7855 |
^ self lastIndexOfSubCollection:aString startingAt:startIndex ifAbsent:exceptionValue |
|
7856 |
||
7857 |
" 123456789012 |
|
7858 |
'abcdefabcdef' lastIndexOfString:'abc' |
|
7859 |
'abcdefabcdef' lastIndexOfString:'abc' startingAt:6 |
|
7860 |
'abcdefabcdef' lastIndexOfString:'xxx' startingAt:6 ifAbsent:999 |
|
7861 |
" |
|
7862 |
! |
|
7863 |
||
15055 | 7864 |
rangeOfSubCollection:subString startingAt:start ifAbsent:exceptionValue caseSensitive:caseSensitive |
7865 |
"find a substring. if found, return the start- and endIndex; |
|
7866 |
if not found, return the value of exceptionValue." |
|
7867 |
||
7868 |
|i| |
|
7869 |
||
7870 |
i := self indexOfSubCollection:subString startingAt:start ifAbsent:0 caseSensitive:caseSensitive. |
|
7871 |
i == 0 ifTrue:[ |
|
20012 | 7872 |
^ exceptionValue value |
15055 | 7873 |
]. |
7874 |
^ i to:(i + subString size - 1) |
|
7875 |
||
7876 |
" |
|
7877 |
'hello world' findRangeOfString:'llo' |
|
7878 |
'hello world' findRangeOfString:'ole' |
|
7879 |
" |
|
9192 | 7880 |
! ! |
7881 |
||
7882 |
!CharacterArray methodsFor:'testing'! |
|
7883 |
||
18463 | 7884 |
argumentCount |
7885 |
"treating the receiver as a message selector, return how many arguments would it take (ANSI)" |
|
7886 |
||
7887 |
|binopChars| |
|
7888 |
||
7889 |
(self size <= Method maxBinarySelectorSize) ifTrue:[ |
|
20012 | 7890 |
binopChars := Method binarySelectorCharacters. |
7891 |
(self conform:[:eachChar | (binopChars includes:eachChar)]) ifTrue:[ |
|
7892 |
^ 1 |
|
7893 |
]. |
|
18463 | 7894 |
]. |
7895 |
^ self occurrencesOf:$: |
|
7896 |
||
7897 |
" |
|
7898 |
'foo:bar:' argumentCount |
|
7899 |
#foo:bar: argumentCount |
|
7900 |
'hello' argumentCount |
|
7901 |
'+' argumentCount |
|
7902 |
'++' argumentCount |
|
7903 |
'+++' argumentCount |
|
7904 |
'|' argumentCount |
|
7905 |
'?' argumentCount |
|
7906 |
'_' argumentCount |
|
7907 |
'_:' argumentCount |
|
7908 |
'_:_:' argumentCount |
|
7909 |
'<->' argumentCount |
|
7910 |
" |
|
7911 |
! |
|
7912 |
||
18597 | 7913 |
characterSize |
7914 |
"answer the size in bits of my largest character (actually only 7, 8, 16 or 32)" |
|
7915 |
||
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
7916 |
|string max |
18597 | 7917 |
sz "{ Class:SmallInteger}" | |
15829 | 7918 |
|
7919 |
(string := self string) ~~ self ifTrue:[ |
|
20012 | 7920 |
^ string characterSize. |
18597 | 7921 |
]. |
7922 |
||
7923 |
sz := self size. |
|
7924 |
max := 7. |
|
7925 |
1 to:sz do:[:idx | |
|
20012 | 7926 |
|thisSize| |
7927 |
||
7928 |
thisSize := (self at:idx) characterSize. |
|
7929 |
thisSize > max ifTrue:[ |
|
7930 |
max := thisSize. |
|
7931 |
max == 32 ifTrue:[ |
|
7932 |
"shortcut: we know, that max size is 32" |
|
7933 |
^ 32. |
|
7934 |
]. |
|
7935 |
]. |
|
15829 | 7936 |
]. |
18597 | 7937 |
|
7938 |
^ max. |
|
7939 |
||
7940 |
" |
|
7941 |
'hello' characterSize |
|
7942 |
'hello' asUnicode32String characterSize |
|
7943 |
'helloü' asUnicode32String characterSize |
|
7944 |
'hello' asText allBold characterSize |
|
7945 |
" |
|
7946 |
! |
|
7947 |
||
7948 |
contains8BitCharacters |
|
7949 |
<resource: #obsolete> |
|
7950 |
||
7951 |
^ self containsNon7BitAscii |
|
7952 |
! |
|
7953 |
||
7954 |
containsNon7BitAscii |
|
7955 |
"return true, if the underlying string contains 8BitCharacters (or widers) |
|
7956 |
(i.e. if it is non-ascii)" |
|
7957 |
||
7958 |
|string |
|
7959 |
sz "{ Class:SmallInteger }"| |
|
7960 |
||
7961 |
(string := self string) ~~ self ifTrue:[ |
|
20012 | 7962 |
^ string containsNon7BitAscii |
18597 | 7963 |
]. |
7964 |
sz := self size. |
|
7965 |
1 to:sz do:[:idx| |
|
20012 | 7966 |
(self at:idx) codePoint > 16r7F ifTrue:[ |
7967 |
^ true. |
|
7968 |
]. |
|
18597 | 7969 |
]. |
7970 |
^ false. |
|
7971 |
||
7972 |
" |
|
7973 |
'hello' asUnicode32String containsNon7BitAscii |
|
7974 |
'hello üöä' asUnicode32String containsNon7BitAscii |
|
7975 |
'hello' asUnicode32String asText allBold containsNon7BitAscii |
|
7976 |
'hello üö' asUnicode32String asText allBold containsNon7BitAscii |
|
15829 | 7977 |
" |
7978 |
! |
|
7979 |
||
9192 | 7980 |
isAlphaNumeric |
7981 |
"return true, if the receiver is some alphanumeric word; |
|
7982 |
i.e. consists of a letter followed by letters or digits." |
|
7983 |
||
7984 |
self size == 0 ifTrue:[ |
|
20012 | 7985 |
"mhmh what is this ?" |
7986 |
^ false |
|
9192 | 7987 |
]. |
7988 |
(self at:1) isLetter ifFalse:[^ false]. |
|
10093
8cff5eca1c6f
code cleanup: use #contains/#conform instead of explicit loop
Claus Gittinger <cg@exept.de>
parents:
10088
diff
changeset
|
7989 |
^ self conform:[:char | char isLetterOrDigit]. |
8cff5eca1c6f
code cleanup: use #contains/#conform instead of explicit loop
Claus Gittinger <cg@exept.de>
parents:
10088
diff
changeset
|
7990 |
|
8cff5eca1c6f
code cleanup: use #contains/#conform instead of explicit loop
Claus Gittinger <cg@exept.de>
parents:
10088
diff
changeset
|
7991 |
" |
10703
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
7992 |
'helloWorld' isAlphaNumeric |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
7993 |
'foo1234' isAlphaNumeric |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
7994 |
'f1234' isAlphaNumeric |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
7995 |
'1234' isAlphaNumeric |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
7996 |
'+' isAlphaNumeric |
10093
8cff5eca1c6f
code cleanup: use #contains/#conform instead of explicit loop
Claus Gittinger <cg@exept.de>
parents:
10088
diff
changeset
|
7997 |
" |
8cff5eca1c6f
code cleanup: use #contains/#conform instead of explicit loop
Claus Gittinger <cg@exept.de>
parents:
10088
diff
changeset
|
7998 |
|
8cff5eca1c6f
code cleanup: use #contains/#conform instead of explicit loop
Claus Gittinger <cg@exept.de>
parents:
10088
diff
changeset
|
7999 |
"Modified: / 13-10-2006 / 12:53:49 / cg" |
9192 | 8000 |
! |
9190 | 8001 |
|
8002 |
isBinarySelector |
|
10903 | 8003 |
"treating the receiver as a message selector, return true if its a binary selector. |
8004 |
Notice, that st/x does not have a size <= 2 limit for unaries" |
|
9190 | 8005 |
|
8006 |
|binopChars| |
|
8007 |
||
18463 | 8008 |
(self size > Method maxBinarySelectorSize) ifTrue:[^ false]. |
8009 |
||
8010 |
binopChars := Method binarySelectorCharacters. |
|
9190 | 8011 |
^ (self conform:[:char | (binopChars includes:char)]) |
8012 |
||
8013 |
" |
|
8014 |
'foo:bar:' isBinarySelector |
|
8015 |
#foo:bar: isBinarySelector |
|
8016 |
'hello' isBinarySelector |
|
8017 |
'+' isBinarySelector |
|
8018 |
'|' isBinarySelector |
|
8874 | 8019 |
'?' isBinarySelector |
8020 |
':' isBinarySelector |
|
8021 |
'a:' isBinarySelector |
|
8022 |
'->' isBinarySelector |
|
8023 |
'<->' isBinarySelector |
|
8024 |
'::' isBinarySelector |
|
7482 | 8025 |
" |
8026 |
||
8027 |
"Modified: 4.1.1997 / 14:16:14 / cg" |
|
9192 | 8028 |
! |
8029 |
||
8030 |
isBlank |
|
8031 |
"return true, if the receiver contains spaces only" |
|
8032 |
||
9230 | 8033 |
^ (self contains:[:char | char ~~ Character space]) not |
8034 |
||
8035 |
" |
|
10703
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
8036 |
'' isBlank |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
8037 |
' a ' isBlank |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
8038 |
' ' isBlank |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
8039 |
' ' asUnicode16String isBlank |
9230 | 8040 |
" |
9192 | 8041 |
! |
8042 |
||
10903 | 8043 |
isKeywordSelector |
8044 |
"return true, iff there are only alphanumeric or underline characters separated by colons. |
|
19682 | 8045 |
Must end with a colon. |
8046 |
You can use this to check an arbitrary string for being valid as a keyword. |
|
8047 |
If you have a valid selector at hand, and need to know if it is a keyword or not, |
|
8048 |
use #isKeyword, which is much faster." |
|
10903 | 8049 |
|
8050 |
|state| |
|
8051 |
||
19682 | 8052 |
(self size == 0) ifTrue:[^ false]. |
8053 |
(self last == $:) ifFalse:[^ false]. |
|
8054 |
||
12177 | 8055 |
state := #initial. |
10903 | 8056 |
self do:[:char | |
20012 | 8057 |
(state == #initial or:[ state == #gotColon]) ifTrue:[ |
8058 |
(char isLetterOrUnderline) ifFalse:[^ false]. |
|
8059 |
state := #gotCharacter. |
|
8060 |
] ifFalse:[ |
|
8061 |
char == $: ifTrue:[ |
|
8062 |
state := #gotColon. |
|
8063 |
] ifFalse:[ |
|
8064 |
(char isLetterOrDigit or:[char == $_]) ifFalse:[^ false]. |
|
8065 |
]. |
|
8066 |
]. |
|
10903 | 8067 |
]. |
8068 |
^ state == #gotColon. |
|
8069 |
||
8070 |
" |
|
12177 | 8071 |
self assert:(':' isKeywordSelector not). |
10903 | 8072 |
self assert:(':a' isKeywordSelector not). |
8073 |
self assert:('1:' isKeywordSelector not). |
|
8074 |
self assert:('a:' isKeywordSelector). |
|
8075 |
self assert:('_:' isKeywordSelector). |
|
8076 |
self assert:('_a:' isKeywordSelector). |
|
8077 |
self assert:('_1:' isKeywordSelector). |
|
8078 |
self assert:('_1::' isKeywordSelector not). |
|
8079 |
self assert:('_:_:' isKeywordSelector). |
|
8080 |
self assert:('a:b:' isKeywordSelector). |
|
8081 |
self assert:('aa:bb:' isKeywordSelector). |
|
8082 |
self assert:('aa:bb:a' isKeywordSelector not). |
|
8083 |
self assert:('1:2:' isKeywordSelector not). |
|
8084 |
" |
|
19682 | 8085 |
|
8086 |
"Modified (comment): / 30-04-2016 / 18:20:14 / cg" |
|
10903 | 8087 |
! |
8088 |
||
10431 | 8089 |
isNameSpaceSelector |
8090 |
"Answer true if the receiver contains chars which form a nameSpace selector name. |
|
8091 |
These are of the form ':<ns>::<sel>', where ns is the NameSpace and sel is the regular selector. |
|
8092 |
For example, the #+ selector as seen by the Foo namespace would be actually #':Foo::+'. |
|
17511 | 8093 |
This special format (a symbol starting with a colon) was chosen, because almost every other selector |
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
8094 |
is legal, and this can be checked quickly by just looking at the first character. |
10431 | 8095 |
You cannot easily change this algorithm here, as it is also known by the VM's lookup function." |
8096 |
||
8097 |
|i| |
|
8098 |
||
16733 | 8099 |
(self at:1) == $: ifFalse:[^ false]. |
17511 | 8100 |
|
10431 | 8101 |
i := self indexOf:$: startingAt:2. |
8102 |
i == 0 ifTrue:[^ false]. |
|
8103 |
self size <= (i+1) ifTrue:[^ false]. |
|
8104 |
(self at:i+1) == $: ifFalse:[^ false]. |
|
8105 |
(self at:i+2) == $: ifTrue:[^ false]. |
|
8106 |
"/ could check if the rest after the ns-prefix and colons is a valid selector... |
|
8107 |
^ true |
|
8108 |
||
8109 |
"test: |
|
10703
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
8110 |
self assert:('+' isNameSpaceSelector) not. |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
8111 |
self assert:(':+' isNameSpaceSelector) not. |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
8112 |
self assert:(':Foo:+' isNameSpaceSelector) not. |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
8113 |
|
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
8114 |
self assert:(':Foo::+' isNameSpaceSelector). |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
8115 |
self assert:(':Foo::bar:baz:' isNameSpaceSelector). |
10431 | 8116 |
" |
8117 |
||
8118 |
"Created: / 05-03-2007 / 11:35:31 / cg" |
|
8119 |
! |
|
8120 |
||
9192 | 8121 |
isNumeric |
8122 |
"return true, if the receiver is some numeric word; |
|
8123 |
i.e. consists only of digits." |
|
8124 |
||
8125 |
self size == 0 ifTrue:[ |
|
20012 | 8126 |
^ false |
9192 | 8127 |
]. |
10093
8cff5eca1c6f
code cleanup: use #contains/#conform instead of explicit loop
Claus Gittinger <cg@exept.de>
parents:
10088
diff
changeset
|
8128 |
^ self conform:[:char | char isDigit] |
9192 | 8129 |
|
8130 |
" |
|
8131 |
'helloWorld' isNumeric |
|
8132 |
'foo1234' isNumeric |
|
8133 |
'f1234' isNumeric |
|
8134 |
'1234' isNumeric |
|
8135 |
'+' isNumeric |
|
8136 |
" |
|
10093
8cff5eca1c6f
code cleanup: use #contains/#conform instead of explicit loop
Claus Gittinger <cg@exept.de>
parents:
10088
diff
changeset
|
8137 |
|
8cff5eca1c6f
code cleanup: use #contains/#conform instead of explicit loop
Claus Gittinger <cg@exept.de>
parents:
10088
diff
changeset
|
8138 |
"Modified: / 13-10-2006 / 12:54:12 / cg" |
9192 | 8139 |
! |
8140 |
||
18893 | 8141 |
isPlainString |
8142 |
"return true, if the receiver is a plain string (without attributes); |
|
8143 |
true is returned here - redefinition of Object>>isPlainString." |
|
8144 |
||
8145 |
^ true |
|
8146 |
! |
|
8147 |
||
9230 | 8148 |
isString |
8149 |
"return true, if the receiver is some kind of string; |
|
8150 |
true is returned here - redefinition of Object>>isString." |
|
8151 |
||
8152 |
^ true |
|
8153 |
! |
|
8154 |
||
8155 |
isUnarySelector |
|
8156 |
"Answer true if the receiver contains only chars in an ANSI unary method selector, false otherwise." |
|
8157 |
||
17442 | 8158 |
^ (self first isLetterOrUnderline) |
9845 | 8159 |
and:[ self conform: [ :chr | chr isLetterOrDigit or:[chr == $_] ]] |
8160 |
||
8161 |
"Modified: / 13-09-2006 / 11:35:15 / cg" |
|
9230 | 8162 |
! |
8163 |
||
14122 | 8164 |
isUnicode16String |
8165 |
^ false |
|
8166 |
! |
|
8167 |
||
8168 |
isUnicode32String |
|
19939 | 8169 |
"true if this is a 4-byte unicode string" |
8170 |
||
14122 | 8171 |
^ false |
8172 |
! |
|
8173 |
||
8174 |
isUnicodeString |
|
19938 | 8175 |
"true if this is a 2- or 4-byte unicode string |
8176 |
(i.e. not a single byte string). |
|
20194 | 8177 |
Notice, that the name is misleading: |
19938 | 8178 |
all strings are use unicode encoding" |
20194 | 8179 |
|
14122 | 8180 |
^ false |
8181 |
! |
|
8182 |
||
9230 | 8183 |
isValidSmalltalkIdentifier |
19464 | 8184 |
"return true, if the receiver's characters make up a valid smalltalk identifier" |
9230 | 8185 |
|
8186 |
|scanner tok| |
|
8187 |
||
8188 |
scanner := Compiler new. |
|
8189 |
scanner source:(self readStream). |
|
11630
6db5956f188f
isValidSmalltalkIdentifier: return true/false (catch parseError)
Claus Gittinger <cg@exept.de>
parents:
11565
diff
changeset
|
8190 |
Parser parseErrorSignal handle:[:ex | |
20012 | 8191 |
tok := nil. |
11630
6db5956f188f
isValidSmalltalkIdentifier: return true/false (catch parseError)
Claus Gittinger <cg@exept.de>
parents:
11565
diff
changeset
|
8192 |
] do:[ |
20012 | 8193 |
tok := scanner nextToken. |
11630
6db5956f188f
isValidSmalltalkIdentifier: return true/false (catch parseError)
Claus Gittinger <cg@exept.de>
parents:
11565
diff
changeset
|
8194 |
]. |
9230 | 8195 |
tok ~~ #Identifier ifTrue:[ |
20012 | 8196 |
^ false |
9230 | 8197 |
]. |
8198 |
scanner tokenPosition == 1 ifFalse:[^ false]. |
|
8199 |
^ scanner sourceStream atEnd. |
|
8200 |
||
8201 |
" |
|
8202 |
'foo' isValidSmalltalkIdentifier |
|
8203 |
'1foo' isValidSmalltalkIdentifier |
|
8204 |
'_foo' isValidSmalltalkIdentifier |
|
8205 |
'_foo_bar_' isValidSmalltalkIdentifier |
|
8206 |
'foo ' isValidSmalltalkIdentifier |
|
8207 |
' foo' isValidSmalltalkIdentifier |
|
8208 |
" |
|
8209 |
! |
|
8210 |
||
14555 | 8211 |
isWideString |
19580 | 8212 |
"true if I require more than one byte per character" |
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
8213 |
|
15829 | 8214 |
|string| |
8215 |
||
8216 |
(string := self string) ~~ self ifTrue:[ |
|
20012 | 8217 |
^ string isWideString. |
15829 | 8218 |
]. |
8219 |
^ self contains:[:aCharacter | aCharacter codePoint > 16rFF]. |
|
14555 | 8220 |
! |
8221 |
||
9192 | 8222 |
numArgs |
18463 | 8223 |
<resource: #obsolete> |
608 | 8224 |
"treating the receiver as a message selector, return how many arguments would it take" |
8225 |
||
18463 | 8226 |
^ self argumentCount |
9192 | 8227 |
! |
8228 |
||
8229 |
partsIfSelector |
|
14629 | 8230 |
"treat the receiver as a message selector, return a collection of parts. |
8231 |
Notice: this is more tolerant than Smalltalk's syntax would suggest; |
|
8232 |
especially it allows for empty keyword parts between colons. |
|
8233 |
This is not (and should not be checked here), to allow parsing of |
|
8234 |
degenerate selectors as appearing with objectiveC." |
|
608 | 8235 |
|
9192 | 8236 |
|idx1 "{ Class: SmallInteger }" |
8237 |
coll idx2 sz| |
|
8238 |
||
8239 |
coll := OrderedCollection new. |
|
8240 |
idx1 := 1. |
|
8241 |
sz := self size. |
|
16245 | 8242 |
[ |
20012 | 8243 |
idx2 := self indexOf:$: startingAt:idx1. |
8244 |
(idx2 == 0 or:[idx2 == sz]) ifTrue:[ |
|
8245 |
coll add:(self copyFrom:idx1). |
|
8246 |
^ coll |
|
8247 |
]. |
|
8248 |
coll add:(self copyFrom:idx1 to:idx2). |
|
8249 |
idx1 := idx2 + 1 |
|
16245 | 8250 |
] loop. |
9192 | 8251 |
|
8252 |
" |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
8253 |
'foo:' partsIfSelector |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
8254 |
'foo:bar:' partsIfSelector |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
8255 |
'foo::::' partsIfSelector |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
8256 |
#foo:bar: partsIfSelector |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
8257 |
'hello' partsIfSelector |
9192 | 8258 |
'+' partsIfSelector |
8259 |
" |
|
64 | 8260 |
! ! |
8261 |
||
9192 | 8262 |
!CharacterArray methodsFor:'tracing'! |
8263 |
||
8264 |
traceInto:aRequestor level:level from:referrer |
|
8265 |
"double dispatch into tracer, passing my type implicitely in the selector" |
|
8266 |
||
8267 |
^ aRequestor traceCharacterArray:self level:level from:referrer |
|
8268 |
||
8269 |
||
8270 |
! ! |
|
8271 |
||
8272 |
!CharacterArray methodsFor:'visiting'! |
|
8395
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8376
diff
changeset
|
8273 |
|
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8376
diff
changeset
|
8274 |
acceptVisitor:aVisitor with:aParameter |
16733 | 8275 |
"dispatch for visitor pattern; send #visitString:with: to aVisitor" |
8395
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8376
diff
changeset
|
8276 |
|
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8376
diff
changeset
|
8277 |
^ aVisitor visitString:self with:aParameter |
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8376
diff
changeset
|
8278 |
! ! |
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8376
diff
changeset
|
8279 |
|
1849
9f35f5934ec6
added msdos-latin1 en/decoder (code = #msdos850)
Claus Gittinger <cg@exept.de>
parents:
1669
diff
changeset
|
8280 |
!CharacterArray class methodsFor:'documentation'! |
629 | 8281 |
|
8282 |
version |
|
18476 | 8283 |
^ '$Header$' |
12058 | 8284 |
! |
8285 |
||
8286 |
version_CVS |
|
18476 | 8287 |
^ '$Header$' |
629 | 8288 |
! ! |
6809 | 8289 |
|
14606 | 8290 |
|
2728
7569e45a4d15
raise error signal if anything goes wrong while decoding.
Claus Gittinger <cg@exept.de>
parents:
2721
diff
changeset
|
8291 |
CharacterArray initialize! |