author | Claus Gittinger <cg@exept.de> |
Fri, 29 Jun 2018 11:42:52 +0200 | |
changeset 23155 | 3f803938a75c |
parent 23154 | 57acd331a79a |
child 23187 | 0f22aaefc623 |
permissions | -rw-r--r-- |
17600 | 1 |
"{ Encoding: utf8 }" |
2 |
||
64 | 3 |
" |
4 |
COPYRIGHT (c) 1994 by Claus Gittinger |
|
20874 | 5 |
All Rights Reserved |
64 | 6 |
|
7 |
This software is furnished under a license and may be used |
|
8 |
only in accordance with the terms of that license and with the |
|
9 |
inclusion of the above copyright notice. This software may not |
|
10 |
be provided or otherwise made available to, or used by, any |
|
11 |
other person. No title to or ownership of the software is |
|
12 |
hereby transferred. |
|
13 |
" |
|
5342
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
14 |
"{ Package: 'stx:libbasic' }" |
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
15 |
|
17364 | 16 |
"{ NameSpace: Smalltalk }" |
17 |
||
14137
1eb1b4a3f364
Inherit from UninterpretedBytes instead of ByteArray
Stefan Vogel <sv@exept.de>
parents:
14122
diff
changeset
|
18 |
UninterpretedBytes variableByteSubclass:#CharacterArray |
23060 | 19 |
instanceVariableNames:'' |
20 |
classVariableNames:'DecoderTables DecodingFailedSignal EncoderTables |
|
21 |
EncodingFailedSignal PreviousMatches UnicodeDenormalizationMap |
|
22 |
UnicodeNormalizationMap' |
|
23 |
poolDictionaries:'' |
|
24 |
category:'Collections-Text' |
|
64 | 25 |
! |
26 |
||
1849
9f35f5934ec6
added msdos-latin1 en/decoder (code = #msdos850)
Claus Gittinger <cg@exept.de>
parents:
1669
diff
changeset
|
27 |
!CharacterArray class methodsFor:'documentation'! |
64 | 28 |
|
82 | 29 |
copyright |
30 |
" |
|
31 |
COPYRIGHT (c) 1994 by Claus Gittinger |
|
20874 | 32 |
All Rights Reserved |
82 | 33 |
|
34 |
This software is furnished under a license and may be used |
|
35 |
only in accordance with the terms of that license and with the |
|
36 |
inclusion of the above copyright notice. This software may not |
|
37 |
be provided or otherwise made available to, or used by, any |
|
38 |
other person. No title to or ownership of the software is |
|
39 |
hereby transferred. |
|
40 |
" |
|
41 |
! |
|
42 |
||
64 | 43 |
documentation |
44 |
" |
|
255 | 45 |
CharacterArray is a superclass for all kinds of Strings (i.e. |
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
46 |
(singleByte-)Strings, TwoByteStrings, UnicodeStrings |
20194 | 47 |
and whatever may come in the future. |
18318 | 48 |
|
49 |
This class is abstract, meaning that there are no instances of it; |
|
50 |
concrete subclasses define how the characters are stored (i.e. either as |
|
51 |
single byte, two-byte or four byte strings). |
|
52 |
||
2537 | 53 |
All this class does is provide common protocol for concrete subclasses. |
1290 | 54 |
|
18318 | 55 |
Notice: |
20194 | 56 |
internally, ST/X uses a unicode encoding for ALL characters - both |
57 |
for individual chatacter entities and for strings of characters. |
|
58 |
When reading/writing files in different encodings, the conversion is |
|
59 |
done at read/write time by use of a CharacterEncoder instance. |
|
60 |
These know how to convert to a wide range of encodings. |
|
18318 | 61 |
|
62 |
Also notice: |
|
20194 | 63 |
UTF8 and UTF16 are external encodings of a Unicode string; they are never |
64 |
used internally. When interacting with a UTF8 interface (OS-API or files), |
|
65 |
you should convert UTF8 into the internal full Unicode right at the interface. |
|
66 |
Do not keep UTF8 around internally as String instances. |
|
67 |
The reason is that UTF8 makes it harder to manipulate strings (for example |
|
68 |
to insert/extract substrings or to get its size. Of such operations would |
|
69 |
require a scan of the UTF8, which would complicate them). |
|
70 |
Of course, there may be rare exceptions to this, for example if a file's contents |
|
71 |
is treated as raw data, and the strings have to be copied/shuffled around only, |
|
72 |
without any real processing on it. |
|
18318 | 73 |
|
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:[ |
20874 | 87 |
DecodingFailedSignal := DecodingError. |
88 |
DecodingFailedSignal notifierString:'error during decode'. |
|
89 |
||
22432 | 90 |
EncodingFailedSignal := EncodingError. |
20874 | 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 |
|
22432 | 98 |
"Modified: / 03-08-1997 / 18:15:59 / cg" |
99 |
"Modified (format): / 16-01-2018 / 18:57:10 / stefan" |
|
2728
7569e45a4d15
raise error signal if anything goes wrong while decoding.
Claus Gittinger <cg@exept.de>
parents:
2721
diff
changeset
|
100 |
! ! |
7569e45a4d15
raise error signal if anything goes wrong while decoding.
Claus Gittinger <cg@exept.de>
parents:
2721
diff
changeset
|
101 |
|
1849
9f35f5934ec6
added msdos-latin1 en/decoder (code = #msdos850)
Claus Gittinger <cg@exept.de>
parents:
1669
diff
changeset
|
102 |
!CharacterArray class methodsFor:'instance creation'! |
64 | 103 |
|
104 |
basicNew |
|
105 |
"return a new empty string" |
|
106 |
||
107 |
^ self basicNew:0 |
|
108 |
! |
|
109 |
||
2734 | 110 |
fromBytes:aByteCollection |
111 |
"return an instance of the receiver class, |
|
19757 | 112 |
taking untranslated bytes from the argument, aByteCollection |
113 |
in most-significant first order. |
|
2734 | 114 |
Only useful, when reading twoByteStrings from external sources." |
115 |
||
19757 | 116 |
^ self fromBytes:aByteCollection MSB:true |
117 |
||
118 |
" |
|
119 |
Unicode16String fromBytes:#[16r02 16r20] |
|
120 |
Unicode16String fromBytes:#[16r02 16r20] MSB:true |
|
121 |
Unicode16String fromBytes:#[16r02 16r20] MSB:false |
|
122 |
" |
|
123 |
||
124 |
"Modified: 30.6.1997 / 20:08:37 / cg" |
|
125 |
! |
|
126 |
||
127 |
fromBytes:aByteCollection MSB:msb |
|
128 |
"return an instance of the receiver class, |
|
129 |
taking untranslated bytes from the argument, aByteCollection |
|
130 |
in the given byte order. |
|
131 |
Only useful, when reading twoByteStrings from external sources." |
|
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
132 |
|
2734 | 133 |
|mySize nBytes newString dstIdx| |
134 |
||
19757 | 135 |
self assert:(self ~~ CharacterArray). "/ only works for concrete subclasses. |
136 |
||
137 |
"/ the following is a quite inefficient implementation. |
|
138 |
"/ consider rewriting, if heavily used. |
|
2734 | 139 |
nBytes := aByteCollection size. |
140 |
mySize := self basicNew bitsPerCharacter. |
|
141 |
mySize == 16 ifTrue:[ |
|
20874 | 142 |
newString := self uninitializedNew:(nBytes // 2). |
143 |
dstIdx := 1. |
|
144 |
msb ifTrue:[ |
|
145 |
aByteCollection pairWiseDo:[:hi :lo | |
|
146 |
newString at:dstIdx put:(Character value:(hi bitShift:8)+lo). |
|
147 |
dstIdx := dstIdx + 1 |
|
148 |
]. |
|
149 |
] ifFalse:[ |
|
150 |
aByteCollection pairWiseDo:[:lo :hi | |
|
151 |
newString at:dstIdx put:(Character value:(hi bitShift:8)+lo). |
|
152 |
dstIdx := dstIdx + 1 |
|
153 |
]. |
|
154 |
]. |
|
155 |
^ newString. |
|
2734 | 156 |
]. |
157 |
||
17625 | 158 |
^ (self uninitializedNew:nBytes) replaceFrom:1 with:aByteCollection |
159 |
||
160 |
" |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
161 |
Unicode16String fromBytes:#[16r02 16r20] |
2734 | 162 |
" |
163 |
||
164 |
"Modified: 30.6.1997 / 20:08:37 / cg" |
|
165 |
! |
|
166 |
||
64 | 167 |
fromString:aString |
168 |
"return a copy of the argument, aString" |
|
169 |
||
18967 | 170 |
|sz| |
171 |
||
172 |
sz := aString size. |
|
173 |
^ (self uninitializedNew:sz) replaceFrom:1 to:sz with:aString startingAt:1 |
|
174 |
||
175 |
" |
|
20874 | 176 |
Unicode16String fromString:'hello' |
22233 | 177 |
String fromString:'hello' asUnicode16String |
178 |
Unicode16String fromString:'hello' asUnicode16String |
|
17625 | 179 |
" |
581 | 180 |
! |
181 |
||
182 |
fromStringCollection:aCollectionOfStrings |
|
183 |
"return new string formed by concatenating a copy of the argument, aString" |
|
184 |
||
185 |
^ self fromStringCollection:aCollectionOfStrings separatedBy:'' |
|
186 |
||
187 |
" |
|
188 |
String fromStringCollection:#('hello' 'world' 'how' 'about' 'this') |
|
189 |
" |
|
190 |
||
191 |
"Created: 20.11.1995 / 15:26:59 / cg" |
|
192 |
! |
|
193 |
||
194 |
fromStringCollection:aCollectionOfStrings separatedBy:aSeparatorString |
|
195 |
"return new string formed by concatenating a copy of the argument, aString" |
|
196 |
||
17625 | 197 |
|stream| |
198 |
||
199 |
aCollectionOfStrings do:[:eachString | |
|
20874 | 200 |
stream isNil ifTrue:[ |
201 |
stream := self writeStreamClass with:eachString. |
|
202 |
] ifFalse:[ |
|
203 |
stream |
|
204 |
nextPutAll:aSeparatorString; |
|
205 |
nextPutAll:eachString. |
|
206 |
]. |
|
581 | 207 |
]. |
17700 | 208 |
stream isNil ifTrue:[^ '']. |
17625 | 209 |
^ stream contents |
581 | 210 |
|
211 |
" |
|
212 |
String fromStringCollection:#('hello' 'world' 'how' 'about' 'this') separatedBy:' ' |
|
17625 | 213 |
String fromStringCollection:#('hello' 'world' 'how' 'about' 'this') separatedBy:'Ƞ' |
214 |
Text fromStringCollection:{'hello'. 'world'. 'how' allBold. 'about'. 'this'. 'äöü'} separatedBy:'Ƞ' |
|
581 | 215 |
" |
216 |
||
217 |
"Created: 20.11.1995 / 15:32:17 / cg" |
|
608 | 218 |
! |
219 |
||
5757 | 220 |
fromUTF8Bytes:aByteCollection |
21379 | 221 |
<resource: #obsolete> |
5758 | 222 |
"return a new string which represents the characters as decoded |
223 |
from the utf8 encoded bytes, aByteCollection. |
|
224 |
Returns either a normal String, or a TwoByteString instance. |
|
6810 | 225 |
Only useful, when reading twoByteStrings from external sources. |
226 |
This only handles up-to 16bit characters" |
|
5757 | 227 |
|
7876 | 228 |
^ self decodeFromUTF8:aByteCollection. |
5757 | 229 |
|
21379 | 230 |
"Modified (comment): / 07-02-2017 / 17:32:38 / stefan" |
5757 | 231 |
! |
232 |
||
608 | 233 |
new |
234 |
"return a new empty string" |
|
235 |
||
236 |
^ self basicNew:0 |
|
9425 | 237 |
! |
238 |
||
239 |
readSmalltalkStringFrom:aStreamOrString onError:exceptionBlock |
|
240 |
"read & return the next String from the (character-)stream aStream; |
|
241 |
skipping all whitespace first; return the value of exceptionBlock, |
|
242 |
if no string can be read. The sequence of characters as read from the |
|
243 |
stream must be one as stored via storeOn: or storeString." |
|
244 |
||
245 |
|str collected char| |
|
246 |
||
247 |
str := aStreamOrString readStream. |
|
248 |
||
249 |
"skip whiteSpace" |
|
250 |
str skipSeparators. |
|
11320 | 251 |
|
14452
ef3591302afa
changed: #readSmalltalkStringFrom:onError:
Stefan Vogel <sv@exept.de>
parents:
14451
diff
changeset
|
252 |
(str peekOrNil == $') ifTrue:[ |
20874 | 253 |
str next. |
254 |
collected := self writeStream. |
|
255 |
[str atEnd] whileFalse:[ |
|
256 |
char := str next. |
|
257 |
char == $' ifTrue:[ |
|
258 |
"/ look for another quote |
|
259 |
str peekOrNil ~~ $' ifTrue:[ |
|
260 |
"end of string reached" |
|
261 |
^ collected contents. |
|
262 |
]. |
|
263 |
"eat doubled quote" |
|
264 |
str next. |
|
265 |
]. |
|
266 |
((char ~~ Character return) or:[str peekOrNil ~~ Character lf]) ifTrue:[ |
|
267 |
"compress CRLF to LF, but keep a single CR" |
|
268 |
collected nextPut:char. |
|
269 |
]. |
|
270 |
]. |
|
271 |
"if we come here, we reached the end without finding a closing $'" |
|
9425 | 272 |
]. |
273 |
^ exceptionBlock value |
|
274 |
||
275 |
" |
|
276 |
String readFrom:('''hello world''' readStream) |
|
277 |
String readFrom:('''hello '''' world''' readStream) |
|
278 |
String readFrom:('1 ''hello'' ' readStream) |
|
279 |
String readFrom:('1 ''hello'' ' readStream) onError:['foobar'] |
|
280 |
" |
|
281 |
||
282 |
"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
|
283 |
"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
|
284 |
! |
4bf099f5bb56
CharacterArray class>>#writeStream - answer a CharacterWriteStrem
Stefan Vogel <sv@exept.de>
parents:
11229
diff
changeset
|
285 |
|
13201 | 286 |
writeStreamClass |
21145 | 287 |
"the type of stream used in writeStream. |
17213 | 288 |
Here, we return CharacterWriteStream, which automatically changes |
289 |
the underlying collection to a multiByte string (i.e. UnicodeString). |
|
290 |
So you can stream wide characters into it." |
|
13201 | 291 |
|
292 |
^ CharacterWriteStream |
|
11243
4bf099f5bb56
CharacterArray class>>#writeStream - answer a CharacterWriteStrem
Stefan Vogel <sv@exept.de>
parents:
11229
diff
changeset
|
293 |
|
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 |
String writeStream |
21145 | 296 |
|
297 |
- the following delivers a Unicode16String: |
|
298 |
||
299 |
String streamContents:[:s | |
|
300 |
s nextPut:(Character value:16r3f30). |
|
301 |
s nextPutAll:'hello'. |
|
302 |
s cr. |
|
303 |
] |
|
11243
4bf099f5bb56
CharacterArray class>>#writeStream - answer a CharacterWriteStrem
Stefan Vogel <sv@exept.de>
parents:
11229
diff
changeset
|
304 |
" |
13201 | 305 |
|
306 |
"Created: / 09-01-2011 / 10:37:57 / cg" |
|
64 | 307 |
! ! |
308 |
||
9265 | 309 |
!CharacterArray class methodsFor:'Compatibility-VW'! |
310 |
||
311 |
fromIntegerArray: anArray |
|
312 |
"Answer a new instance of the receiver that is created from the argument, anArray." |
|
313 |
||
314 |
| new | |
|
315 |
||
316 |
new := self new: anArray size. |
|
317 |
1 to: anArray size do:[:index | |
|
20874 | 318 |
new at: index put: (anArray at: index) asCharacter |
9265 | 319 |
]. |
320 |
^new |
|
321 |
||
322 |
" |
|
323 |
String fromIntegerArray: #[8 127] |
|
324 |
" |
|
325 |
! ! |
|
326 |
||
2728
7569e45a4d15
raise error signal if anything goes wrong while decoding.
Claus Gittinger <cg@exept.de>
parents:
2721
diff
changeset
|
327 |
!CharacterArray class methodsFor:'Signal constants'! |
7569e45a4d15
raise error signal if anything goes wrong while decoding.
Claus Gittinger <cg@exept.de>
parents:
2721
diff
changeset
|
328 |
|
7569e45a4d15
raise error signal if anything goes wrong while decoding.
Claus Gittinger <cg@exept.de>
parents:
2721
diff
changeset
|
329 |
decodingFailedSignal |
2833 | 330 |
"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
|
331 |
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
|
332 |
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
|
333 |
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
|
334 |
|
22432 | 335 |
^ DecodingError |
336 |
||
337 |
"Created: / 28-06-1997 / 20:09:55 / cg" |
|
338 |
"Modified: / 03-08-1997 / 18:16:47 / cg" |
|
339 |
"Modified: / 16-01-2018 / 18:58:10 / stefan" |
|
2833 | 340 |
! |
341 |
||
342 |
encodingFailedSignal |
|
343 |
"return the (query-) signal, raised when encoding of a string is not possible |
|
344 |
due to invalid characters contained in the source." |
|
345 |
||
22432 | 346 |
^ EncodingError |
347 |
||
348 |
"Modified: / 28-06-1997 / 20:09:35 / cg" |
|
349 |
"Created: / 03-08-1997 / 18:16:40 / cg" |
|
350 |
"Modified: / 16-01-2018 / 18:59:04 / stefan" |
|
2728
7569e45a4d15
raise error signal if anything goes wrong while decoding.
Claus Gittinger <cg@exept.de>
parents:
2721
diff
changeset
|
351 |
! ! |
7569e45a4d15
raise error signal if anything goes wrong while decoding.
Claus Gittinger <cg@exept.de>
parents:
2721
diff
changeset
|
352 |
|
22953 | 353 |
|
1849
9f35f5934ec6
added msdos-latin1 en/decoder (code = #msdos850)
Claus Gittinger <cg@exept.de>
parents:
1669
diff
changeset
|
354 |
!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
|
355 |
|
f0b45a4b82d2
first attempt in encoding/decoding for different character representations (mac/msdos etc.)
Claus Gittinger <cg@exept.de>
parents:
986
diff
changeset
|
356 |
lowSpaceCleanup |
f0b45a4b82d2
first attempt in encoding/decoding for different character representations (mac/msdos etc.)
Claus Gittinger <cg@exept.de>
parents:
986
diff
changeset
|
357 |
"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
|
358 |
|
f0b45a4b82d2
first attempt in encoding/decoding for different character representations (mac/msdos etc.)
Claus Gittinger <cg@exept.de>
parents:
986
diff
changeset
|
359 |
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
|
360 |
|
f0b45a4b82d2
first attempt in encoding/decoding for different character representations (mac/msdos etc.)
Claus Gittinger <cg@exept.de>
parents:
986
diff
changeset
|
361 |
" |
f0b45a4b82d2
first attempt in encoding/decoding for different character representations (mac/msdos etc.)
Claus Gittinger <cg@exept.de>
parents:
986
diff
changeset
|
362 |
CharacterArray lowSpaceCleanup |
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 |
|
f0b45a4b82d2
first attempt in encoding/decoding for different character representations (mac/msdos etc.)
Claus Gittinger <cg@exept.de>
parents:
986
diff
changeset
|
365 |
"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
|
366 |
"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
|
367 |
! ! |
f0b45a4b82d2
first attempt in encoding/decoding for different character representations (mac/msdos etc.)
Claus Gittinger <cg@exept.de>
parents:
986
diff
changeset
|
368 |
|
7421 | 369 |
!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
|
370 |
|
7876 | 371 |
decodeFromUTF8:aStringOrByteCollection |
19837 | 372 |
"given a string in UTF8 encoding, |
373 |
return a new string containing the same characters, in Unicode encoding. |
|
374 |
Returns either a normal String, a Unicode16String or a Unicode32String instance. |
|
375 |
This is only useful, when reading from external sources or communicating with |
|
376 |
other systems |
|
377 |
(ST/X never uses utf8 internally, but always uses strings of fully decoded unicode characters). |
|
378 |
This only handles up-to 30bit characters." |
|
379 |
||
21379 | 380 |
|decodedSize nBitsRequired |
381 |
codepoint "{ Class: SmallInteger }" |
|
19837 | 382 |
byte "{ Class: SmallInteger }" |
21379 | 383 |
encodedSize "{ Class: SmallInteger }" |
19837 | 384 |
srcIdx "{ Class: SmallInteger }" |
385 |
idx "{ Class: SmallInteger }" |
|
386 |
nFollowBytes "{ Class: SmallInteger }" |
|
387 |
minValue "{ Class: SmallInteger }" |
|
388 |
newString| |
|
389 |
||
21379 | 390 |
encodedSize := aStringOrByteCollection size. |
391 |
decodedSize := aStringOrByteCollection utf8DecodedSize. |
|
392 |
||
393 |
encodedSize == decodedSize ifTrue:[ |
|
394 |
"/ fast track, also avoid creation of new strings if aStringOrByteCollection is already a 7-bit string |
|
19837 | 395 |
^ aStringOrByteCollection asSingleByteString |
396 |
]. |
|
397 |
||
398 |
nBitsRequired := 8. |
|
399 |
srcIdx := 1. |
|
400 |
||
21379 | 401 |
"this loop is only used to find the largest character" |
402 |
[srcIdx <= encodedSize] whileTrue:[ |
|
403 |
byte := aStringOrByteCollection byteAt:srcIdx. |
|
19837 | 404 |
srcIdx := srcIdx + 1. |
21379 | 405 |
byte > 16r7F ifTrue:[ |
19837 | 406 |
(byte bitAnd:2r11100000) == 2r11000000 ifTrue:[ |
407 |
"/ 80 .. 7FF |
|
21379 | 408 |
nBitsRequired < 16 ifTrue:[ |
409 |
codepoint := ((byte bitAnd:2r00011111) bitShift:6) |
|
410 |
+ ((aStringOrByteCollection byteAt:srcIdx) bitAnd:2r00111111). |
|
411 |
codepoint > 16rFF ifTrue:[ |
|
412 |
nBitsRequired := 16. |
|
413 |
]. |
|
414 |
]. |
|
415 |
srcIdx := srcIdx + 1. |
|
19837 | 416 |
] ifFalse:[(byte bitAnd:2r11110000) == 2r11100000 ifTrue:[ |
417 |
"/ 800 .. FFFF |
|
21379 | 418 |
srcIdx := srcIdx + 2. |
419 |
nBitsRequired < 16 ifTrue:[nBitsRequired := 16]. |
|
19837 | 420 |
] ifFalse:[(byte bitAnd:2r11111000) == 2r11110000 ifTrue:[ |
421 |
"/ 10000 .. 1FFFFF |
|
21379 | 422 |
srcIdx := srcIdx + 3. |
423 |
nBitsRequired := 32. |
|
19837 | 424 |
] ifFalse:[(byte bitAnd:2r11111100) == 2r11111000 ifTrue:[ |
21379 | 425 |
"5 byte sequences do not result in valid unicode" |
19837 | 426 |
"/ 200000 .. 3FFFFFF |
21379 | 427 |
srcIdx := srcIdx + 4. |
428 |
nBitsRequired := 32. |
|
19837 | 429 |
] ifFalse:[(byte bitAnd:2r11111110) == 2r11111100 ifTrue:[ |
21379 | 430 |
"6 byte sequences do not result in valid unicode" |
19837 | 431 |
"/ 4000000 .. 7FFFFFFF |
21379 | 432 |
(byte bitAnd:2r00000001) ~~ 0 ifTrue:[ |
433 |
"/ ST/X can only represent up to 30 bit characters. |
|
19837 | 434 |
"/ but the max unicode character is defined as 16r10 FFFF anyway |
435 |
^ DecodingError newException |
|
436 |
defaultValue:aStringOrByteCollection; |
|
21145 | 437 |
raiseRequestWith:aStringOrByteCollection |
438 |
errorString:('unicode character out of range at: %1' bindWith:srcIdx). |
|
19837 | 439 |
]. |
21379 | 440 |
srcIdx := srcIdx + 5. |
441 |
nBitsRequired := 32. |
|
19837 | 442 |
] ifFalse:[ |
443 |
^ DecodingError newException |
|
444 |
defaultValue:aStringOrByteCollection; |
|
445 |
raiseRequestWith:aStringOrByteCollection errorString:'invalid utf8 encoding'. |
|
446 |
]]]]]. |
|
447 |
]. |
|
448 |
]. |
|
449 |
||
450 |
nBitsRequired == 8 ifTrue:[ |
|
21379 | 451 |
newString := String uninitializedNew:decodedSize |
452 |
] ifFalse:[nBitsRequired == 16 ifTrue:[ |
|
453 |
newString := Unicode16String new:decodedSize |
|
19837 | 454 |
] ifFalse:[ |
21379 | 455 |
newString := Unicode32String new:decodedSize |
19837 | 456 |
]]. |
457 |
||
458 |
idx := srcIdx := 1. |
|
459 |
||
460 |
"now fill the string" |
|
21379 | 461 |
[srcIdx <= encodedSize] whileTrue:[ |
462 |
byte := codepoint := aStringOrByteCollection byteAt:srcIdx. |
|
19837 | 463 |
srcIdx := srcIdx + 1. |
21379 | 464 |
byte > 16r7F ifTrue:[ |
19837 | 465 |
(byte bitAnd:2r11100000) == 2r11000000 ifTrue:[ |
21379 | 466 |
codepoint := byte bitAnd:2r00011111. |
19837 | 467 |
nFollowBytes := 1. |
21379 | 468 |
minValue := 16r80. |
19837 | 469 |
] ifFalse:[(byte bitAnd:2r11110000) == 2r11100000 ifTrue:[ |
21379 | 470 |
codepoint := byte bitAnd:2r00001111. |
19837 | 471 |
nFollowBytes := 2. |
21379 | 472 |
minValue := 16r800. |
19837 | 473 |
] ifFalse:[(byte bitAnd:2r11111000) == 2r11110000 ifTrue:[ |
21379 | 474 |
codepoint := byte bitAnd:2r00000111. |
19837 | 475 |
nFollowBytes := 3. |
21379 | 476 |
minValue := 16r10000. |
19837 | 477 |
] ifFalse:[(byte bitAnd:2r11111100) == 2r11111000 ifTrue:[ |
478 |
"5 byte sequences do not result in valid unicode" |
|
21379 | 479 |
codepoint := byte bitAnd:2r00000011. |
19837 | 480 |
nFollowBytes := 4. |
21379 | 481 |
minValue := 16r200000. |
19837 | 482 |
] ifFalse:[(byte bitAnd:2r11111110) == 2r11111100 ifTrue:[ |
483 |
"6 byte sequences do not result in valid unicode" |
|
21379 | 484 |
codepoint := byte bitAnd:2r00000001. |
19837 | 485 |
nFollowBytes := 5. |
21379 | 486 |
minValue := 16r4000000. |
19837 | 487 |
]]]]]. |
21379 | 488 |
|
19837 | 489 |
nFollowBytes timesRepeat:[ |
490 |
byte := aStringOrByteCollection byteAt:srcIdx. |
|
21379 | 491 |
(byte bitAnd:2r11000000) ~~ 2r10000000 ifTrue:[ |
492 |
^ DecodingError newException |
|
493 |
defaultValue:aStringOrByteCollection; |
|
494 |
raiseRequestWith:aStringOrByteCollection |
|
495 |
errorString:('illegal followbyte (expect 0x80): 0x%1 at:%2' bindWith:(byte hexPrintString:2) with:srcIdx). |
|
496 |
]. |
|
19837 | 497 |
srcIdx := srcIdx + 1. |
21379 | 498 |
codepoint := (codepoint bitShift:6) bitOr:(byte bitAnd:2r00111111). |
499 |
]. |
|
500 |
||
501 |
codepoint < minValue ifTrue:[ |
|
502 |
"encoding a value in a longer utf8-sequence than required is not allowed |
|
503 |
and imposes a security risk. Proceed to continue with decoding." |
|
504 |
DecodingError newException |
|
505 |
defaultValue:aStringOrByteCollection; |
|
506 |
raiseRequestWith:aStringOrByteCollection errorString:'overlong utf8 sequence'. |
|
19837 | 507 |
]. |
508 |
]. |
|
21379 | 509 |
newString at:idx put:(Character value:codepoint). |
19837 | 510 |
idx := idx + 1. |
511 |
]. |
|
512 |
^ newString |
|
513 |
||
514 |
" |
|
515 |
CharacterArray decodeFromUTF8:#[ 16r41 16r42 ] |
|
516 |
CharacterArray decodeFromUTF8:#[ 16rC6 16r8F ] |
|
517 |
CharacterArray decodeFromUTF8:#[ 16rEF 16rBF 16rBF ] |
|
518 |
||
21379 | 519 |
rfc3629 examples: |
19837 | 520 |
CharacterArray decodeFromUTF8:#[ 16r41 16rE2 16r89 16rA2 16rCE 16r91 16r2E ] |
521 |
CharacterArray decodeFromUTF8:#[ 16rED 16r95 16r9C 16rEA 16rB5 16rAD 16rEC 16r96 16rB4 ] |
|
522 |
CharacterArray decodeFromUTF8:#[ 16rE6 16r97 16rA5 16rE6 16r9C 16rAC 16rE8 16rAA 16r9E ] |
|
523 |
||
524 |
invalid: |
|
525 |
CharacterArray decodeFromUTF8:#[ 16rC0 16r80 ] |
|
526 |
CharacterArray decodeFromUTF8:#[ 16rE0 16r80 16r80 ] |
|
527 |
CharacterArray decodeFromUTF8:#[ 16rE0 16r81 16r02 ] |
|
21379 | 528 |
" |
529 |
||
530 |
"Modified: / 07-02-2017 / 17:41:17 / stefan" |
|
20010 | 531 |
! |
532 |
||
20011 | 533 |
setupNormalizationMaps |
20864 | 534 |
"returns a 2-stage map from ch2 -> ch1 -> mappedChar. |
535 |
for unicode normalization |
|
536 |
(i.e. for replacing combining char-sequences with regular characters). |
|
537 |
ch2 is the combining charCode (eg. 0x0308), ch1 is the previous character (eg. $A), |
|
538 |
mappedChar is the result (eg. $Ä). |
|
539 |
Caveat: |
|
540 |
possibly incomplete: only COMBINING_DIACRITICAL_MARKS are cared for. |
|
541 |
Does not care for COMBINING_DIACRITICAL_MARKS_EXTENDED |
|
542 |
and COMBINING_DIACRITICAL_MARKS_SUPPLEMENT. |
|
543 |
However; those are used for German dialectology, ancient Greek and other similar |
|
544 |
exotic uses. Probably noone will ever even notice that they are missing..." |
|
20011 | 545 |
|
546 |
|def| |
|
547 |
||
548 |
UnicodeNormalizationMap := Dictionary new. |
|
20013 | 549 |
UnicodeDenormalizationMap := Dictionary new. |
550 |
||
551 |
def := [:combiner :chars :mappedChars | |
|
20011 | 552 |
|d| |
553 |
||
20013 | 554 |
d := UnicodeNormalizationMap at:combiner ifAbsentPut:[Dictionary new]. |
555 |
chars with:mappedChars do:[:ch1 :mappedChar | |
|
556 |
d at:ch1 put:mappedChar. |
|
557 |
UnicodeDenormalizationMap at:mappedChar put:(Unicode16String with:ch1 with:combiner). |
|
558 |
]. |
|
20011 | 559 |
]. |
20014 | 560 |
def value:(Character codePoint:16r0300) "grave" value:'AEIOUaeiouWw' value:'ÀÈÌÒÙàèìòùẀẁ'. |
561 |
def value:(Character codePoint:16r0301) "acute" value:'AEIOUYaeiouyÇçKkMmPpWw' value:'ÁÉÍÓÚÝáéíóúýḈḉḰḱḾḿṔṕẂẃ'. |
|
562 |
def value:(Character codePoint:16r0302) "circumflex" value:'AEIOUaeiouZz' value:'ÂÊÎÔÛâêîôûẐẑ'. |
|
20011 | 563 |
def value:(Character codePoint:16r0303) "tilde" value:'AONaon' value:'ÃÕÑãõñ'. |
20014 | 564 |
def value:(Character codePoint:16r0307) "dot over" value:'BbDdFfHhMmNnPpRrSsTtWwXxYyṢṣ' value:'ḂḃḊḋḞḟḢḣṀṁṄṅṖṗṘṙṠṡṪṫẆẇẊẋẎẏṨṩ'. |
565 |
def value:(Character codePoint:16r0308) "diaresis" value:'AEIOUaeiouyHhWwXxt' value:'ÄËÏÖÜäëïöüÿḦḧẄẅẌẍẗ'. |
|
566 |
def value:(Character codePoint:16r030A) "ring" value:'Aawy' value:'Ååẘẙ'. |
|
567 |
def value:(Character codePoint:16r0323) "dot under" value:'AaBbDdEeIiHhKkLlMmNnOoRrSsTtUuVvWwZzṠṡ' value:'ẠạḄḅḌḍẸẹỊịḤḥḲḳḶḷṂṃṆṇỌọṚṛṢṣṬṭỤụṾṿẈẉẒẓṨṩ'. |
|
20011 | 568 |
def value:(Character codePoint:16r0327) "cedilla" value:'Cc' value:'Çç'. |
569 |
||
570 |
" |
|
20014 | 571 |
self setupNormalizationMaps |
20011 | 572 |
" |
573 |
! |
|
574 |
||
575 |
unicodeDenormalizationMap |
|
576 |
"returns a 2-stage map from ch2 -> ch1 -> mappedChar |
|
20864 | 577 |
for unicode normalization (i.e. for making combining chars regular ones). |
578 |
Caveat: |
|
579 |
possibly incomplete: only COMBINING_DIACRITICAL_MARKS are cared for. |
|
580 |
Does not care for COMBINING_DIACRITICAL_MARKS_EXTENDED |
|
581 |
and COMBINING_DIACRITICAL_MARKS_SUPPLEMENT. |
|
582 |
However; those are used for German dialectology, ancient Greek and other similar |
|
583 |
exotic uses. Probably noone will ever even notice that they are missing..." |
|
20011 | 584 |
|
585 |
UnicodeDenormalizationMap isNil ifTrue:[ |
|
586 |
self setupNormalizationMaps |
|
587 |
]. |
|
588 |
^ UnicodeDenormalizationMap |
|
589 |
||
590 |
" |
|
591 |
self unicodeDenormalizationMap |
|
592 |
" |
|
593 |
! |
|
594 |
||
20010 | 595 |
unicodeNormalizationMap |
596 |
"returns a 2-stage map from ch2 -> ch1 -> mappedChar |
|
20864 | 597 |
for unicode normalization (i.e. for making combining chars regular ones). |
598 |
Caveat: |
|
599 |
possibly incomplete: only COMBINING_DIACRITICAL_MARKS are cared for. |
|
600 |
Does not care for COMBINING_DIACRITICAL_MARKS_EXTENDED |
|
601 |
and COMBINING_DIACRITICAL_MARKS_SUPPLEMENT. |
|
602 |
However; those are used for German dialectology, ancient Greek and other similar |
|
603 |
exotic uses. Probably noone will ever even notice that they are missing..." |
|
20010 | 604 |
|
605 |
UnicodeNormalizationMap isNil ifTrue:[ |
|
20011 | 606 |
self setupNormalizationMaps |
20010 | 607 |
]. |
608 |
^ UnicodeNormalizationMap |
|
609 |
||
610 |
" |
|
611 |
self unicodeNormalizationMap |
|
612 |
" |
|
443 | 613 |
! ! |
614 |
||
22924 | 615 |
|
1849
9f35f5934ec6
added msdos-latin1 en/decoder (code = #msdos850)
Claus Gittinger <cg@exept.de>
parents:
1669
diff
changeset
|
616 |
!CharacterArray class methodsFor:'pattern matching'! |
64 | 617 |
|
5342
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
618 |
matchEscapeCharacter |
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
619 |
"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
|
620 |
(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
|
621 |
|
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
622 |
^ $\ |
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
623 |
! |
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
624 |
|
15734 | 625 |
matchScan:matchScanArray from:matchStart to:matchStop with:aString from:start to:stop caseSensitive:caseSensitive |
327 | 626 |
"helper for match; return true if the characters from start to stop in |
627 |
aString are matching the scan in matchScan from matchStart to matchStop. |
|
328 | 628 |
The matchScan is as created by asMatchScanArray. |
327 | 629 |
|
8874 | 630 |
This algorithm is not at all the most efficient; |
631 |
for heavy duty pattern matching, an interface (primitive) to the regex |
|
328 | 632 |
pattern matching package should be added." |
327 | 633 |
|
8874 | 634 |
|matchEntry |
327 | 635 |
mStart "{ Class: SmallInteger }" |
636 |
mStop "{ Class: SmallInteger }" |
|
637 |
sStart "{ Class: SmallInteger }" |
|
638 |
sStop "{ Class: SmallInteger }" |
|
639 |
mSize "{ Class: SmallInteger }" |
|
640 |
sSize "{ Class: SmallInteger }" |
|
641 |
index "{ Class: SmallInteger }" |
|
2561 | 642 |
quickCheck matchLast nextMatchEntry |
986 | 643 |
checkChar included| |
327 | 644 |
|
645 |
mStart := matchStart. |
|
646 |
mStop := matchStop. |
|
647 |
sStart := start. |
|
648 |
sStop := stop. |
|
649 |
||
16245 | 650 |
[ |
8874 | 651 |
"/ Transcript showCR:('match: ''' , (aString copyFrom:sStart to:sStop) , |
3537 | 652 |
"/ ''' against:' , (matchScanArray copyFrom:mStart to:mStop) printString). |
653 |
||
20874 | 654 |
mSize := mStop - mStart + 1. |
655 |
sSize := sStop - sStart + 1. |
|
656 |
||
657 |
"empty strings match" |
|
658 |
(mSize == 0) ifTrue:[^ (sSize == 0)]. |
|
659 |
||
660 |
matchEntry := matchScanArray at:mStart. |
|
661 |
||
662 |
"/ the most common case first: |
|
663 |
(sSize ~~ 0 |
|
664 |
and:[(checkChar := (aString at:sStart)) = matchEntry]) ifTrue:[ |
|
665 |
"advance by one and continue" |
|
666 |
mStart := mStart + 1. |
|
667 |
sStart := sStart + 1 |
|
668 |
] ifFalse:[ |
|
669 |
(matchEntry == #any) ifTrue:[ |
|
670 |
"restString empty -> no match" |
|
671 |
(sSize == 0) ifTrue:[^ false]. |
|
672 |
"# matches single character" |
|
673 |
((sSize == 1) and:[mSize == 1]) ifTrue:[^ true]. |
|
674 |
"advance by one and continue" |
|
675 |
mStart := mStart + 1. |
|
676 |
sStart := sStart + 1 |
|
677 |
] ifFalse:[ |
|
678 |
(matchEntry == #anyString) ifTrue:[ |
|
679 |
"* alone matches anything" |
|
680 |
(mSize == 1) ifTrue:[^ true]. |
|
681 |
"restString empty & matchString not empty -> no match" |
|
682 |
(sSize == 0) ifTrue:[^ false]. |
|
683 |
||
684 |
" |
|
685 |
try to avoid some of the recursion by checking last |
|
686 |
character and continue with shortened strings if possible |
|
687 |
" |
|
688 |
quickCheck := false. |
|
689 |
(mStop >= mStart) ifTrue:[ |
|
690 |
matchLast := matchScanArray at:mStop. |
|
691 |
(matchLast ~~ #anyString) ifTrue:[ |
|
692 |
(matchLast == #any) ifTrue:[ |
|
693 |
quickCheck := true |
|
694 |
] ifFalse:[ |
|
695 |
matchLast == (aString at:sStop) ifTrue:[ |
|
696 |
quickCheck := true |
|
697 |
] ifFalse:[ |
|
698 |
matchLast isString ifTrue:[ |
|
699 |
quickCheck := matchLast includes:(aString at:sStop) |
|
700 |
] |
|
701 |
] |
|
702 |
] |
|
703 |
] |
|
704 |
]. |
|
705 |
quickCheck ifTrue:[ |
|
706 |
" |
|
707 |
quickCheck ok, advance from the right |
|
708 |
" |
|
709 |
mStop := mStop - 1. |
|
710 |
sStop := sStop - 1 |
|
711 |
] ifFalse:[ |
|
712 |
"/ no quick check; |
|
713 |
"/ look for the next character(s) |
|
714 |
"/ and try matching there |
|
715 |
"/ (to avoid recursion) |
|
716 |
||
717 |
mStart < mStop ifTrue:[ |
|
718 |
nextMatchEntry := matchScanArray at:mStart+1. |
|
719 |
nextMatchEntry isCharacter ifTrue:[ |
|
720 |
sStart <= sStop ifTrue:[ |
|
721 |
[ |
|
722 |
caseSensitive ifTrue:[ |
|
723 |
index := aString indexOf:nextMatchEntry startingAt:sStart |
|
724 |
] ifFalse:[ |
|
725 |
index := aString findFirst:[:c | c asLowercase = nextMatchEntry asLowercase] |
|
726 |
startingAt:sStart. |
|
727 |
]. |
|
728 |
(index == 0 or:[index > sStop]) ifTrue:[ |
|
729 |
^ false |
|
730 |
]. |
|
731 |
(self matchScan:matchScanArray |
|
732 |
from:(mStart + 1) |
|
733 |
to:mStop |
|
734 |
with:aString |
|
735 |
from:index |
|
736 |
to:sStop |
|
737 |
caseSensitive:caseSensitive |
|
738 |
) ifTrue:[ |
|
739 |
^ true |
|
740 |
]. |
|
741 |
sStart := index + 1. |
|
742 |
] loop. |
|
743 |
] |
|
744 |
] |
|
745 |
]. |
|
746 |
||
747 |
" |
|
748 |
no quick check possible; |
|
749 |
loop over all possible substrings |
|
750 |
" |
|
751 |
index := sStart. |
|
752 |
[index <= sStop] whileTrue:[ |
|
753 |
(self matchScan:matchScanArray |
|
754 |
from:(mStart + 1) |
|
755 |
to:mStop |
|
756 |
with:aString |
|
757 |
from:index |
|
758 |
to:sStop |
|
759 |
caseSensitive:caseSensitive |
|
760 |
) ifTrue:[ |
|
761 |
^ true |
|
762 |
]. |
|
763 |
index := index + 1 |
|
764 |
]. |
|
765 |
^ false |
|
766 |
]. |
|
767 |
] ifFalse:[ |
|
768 |
(matchEntry isString) ifTrue:[ |
|
769 |
"testString empty -> no match" |
|
770 |
(sSize == 0) ifTrue:[^ false]. |
|
771 |
||
772 |
included := false. |
|
773 |
"/ checkChar := aString at:sStart. |
|
774 |
included := matchEntry includes:checkChar. |
|
775 |
included ifFalse:[ |
|
776 |
caseSensitive ifFalse:[ |
|
777 |
checkChar isUppercase ifTrue:[ |
|
778 |
included := matchEntry includes:checkChar asLowercase. |
|
779 |
] ifFalse:[ |
|
780 |
included := matchEntry includes:checkChar asUppercase. |
|
781 |
] |
|
782 |
]. |
|
783 |
]. |
|
784 |
mStart := mStart + 1. |
|
785 |
mSize := mSize - 1. |
|
786 |
included ifFalse:[^ false]. |
|
787 |
||
788 |
((sSize == 1) and:[mSize == 0]) ifTrue:[^ true]. |
|
789 |
] ifFalse:[ |
|
790 |
"/ must be single character |
|
791 |
||
792 |
"testString empty ?" |
|
793 |
(sSize == 0) ifTrue:[^ false]. |
|
794 |
||
795 |
"first characters equal ?" |
|
796 |
"/ checkChar := aString at:sStart. |
|
797 |
caseSensitive ifTrue:[^ false]. |
|
798 |
(checkChar asUppercase ~= matchEntry asUppercase) ifTrue:[^ false]. |
|
799 |
||
800 |
"advance and continue" |
|
801 |
mStart := mStart + 1. |
|
802 |
]. |
|
803 |
"cut off 1st char and continue" |
|
804 |
sStart := sStart + 1 |
|
805 |
] |
|
806 |
] |
|
807 |
] |
|
16245 | 808 |
] loop. |
2519
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
809 |
|
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 s| |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
812 |
|
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
813 |
scanArray := self matchScanArrayFrom:'*hello'. |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
814 |
s := 'foo bar hello world'. |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
815 |
CharacterArray |
20874 | 816 |
matchScan:scanArray |
817 |
from:1 |
|
818 |
to:scanArray size |
|
819 |
with:s |
|
820 |
from:1 |
|
821 |
to:s size |
|
822 |
caseSensitive:true |
|
2519
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
823 |
" |
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 s| |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
826 |
|
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
827 |
scanArray := self matchScanArrayFrom:'*hello*'. |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
828 |
s := 'foo bar hello world'. |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
829 |
CharacterArray |
20874 | 830 |
matchScan:scanArray |
831 |
from:1 |
|
832 |
to:scanArray size |
|
833 |
with:s |
|
834 |
from:1 |
|
835 |
to:s size |
|
836 |
caseSensitive:true |
|
15734 | 837 |
" |
838 |
||
839 |
"Modified: / 24-07-2011 / 07:17:03 / cg" |
|
840 |
"Modified (comment): / 24-07-2011 / 08:55:14 / cg" |
|
841 |
! |
|
842 |
||
843 |
matchScan:matchScanArray from:matchStart to:matchStop with:aString from:start to:stop ignoreCase:ignoreCase |
|
844 |
<resource: #obsolete> |
|
845 |
"helper for match; return true if the characters from start to stop in |
|
846 |
aString are matching the scan in matchScan from matchStart to matchStop. |
|
847 |
The matchScan is as created by asMatchScanArray. |
|
848 |
||
849 |
This algorithm is not at all the most efficient; |
|
850 |
for heavy duty pattern matching, an interface (primitive) to the regex |
|
851 |
pattern matching package should be added." |
|
852 |
||
853 |
^ self |
|
20874 | 854 |
matchScan:matchScanArray from:matchStart to:matchStop with:aString from:start to:stop caseSensitive:ignoreCase not |
15734 | 855 |
|
856 |
" |
|
857 |
|scanArray s| |
|
858 |
||
859 |
scanArray := self matchScanArrayFrom:'*hello'. |
|
860 |
s := 'foo bar hello world'. |
|
861 |
CharacterArray |
|
20874 | 862 |
matchScan:scanArray |
863 |
from:1 |
|
864 |
to:scanArray size |
|
865 |
with:s |
|
866 |
from:1 |
|
867 |
to:s size |
|
868 |
ignoreCase:false |
|
15734 | 869 |
" |
870 |
" |
|
871 |
|scanArray s| |
|
872 |
||
873 |
scanArray := self matchScanArrayFrom:'*hello*'. |
|
874 |
s := 'foo bar hello world'. |
|
875 |
CharacterArray |
|
20874 | 876 |
matchScan:scanArray |
877 |
from:1 |
|
878 |
to:scanArray size |
|
879 |
with:s |
|
880 |
from:1 |
|
881 |
to:s size |
|
882 |
ignoreCase:false |
|
13520 | 883 |
" |
884 |
||
885 |
"Modified: / 24-07-2011 / 07:17:03 / cg" |
|
886 |
"Modified (comment): / 24-07-2011 / 08:55:14 / cg" |
|
608 | 887 |
! |
888 |
||
889 |
matchScanArrayFrom:aString |
|
890 |
"scan a pattern string and decompose it into a scanArray. |
|
891 |
This is processed faster (especially with character ranges), and |
|
892 |
can also be reused later. (if the same pattern is to be searched again)" |
|
893 |
||
9282
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
894 |
^ self matchScanArrayFrom:aString escapeCharacter:(self matchEscapeCharacter) |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
895 |
|
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
896 |
" |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
897 |
String matchScanArrayFrom:'*ute*' |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
898 |
String matchScanArrayFrom:'**ute**' |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
899 |
String matchScanArrayFrom:'*uter' |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
900 |
String matchScanArrayFrom:'\*uter' |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
901 |
String matchScanArrayFrom:'[cC]#mpute[rR]' |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
902 |
String matchScanArrayFrom:'[abcd]*' |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
903 |
String matchScanArrayFrom:'[a-k]*' |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
904 |
String matchScanArrayFrom:'*some*compl*ern*' |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
905 |
String matchScanArrayFrom:'[a-' |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
906 |
String matchScanArrayFrom:'[a-zA-Z]' |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
907 |
String matchScanArrayFrom:'[a-z01234A-Z]' |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
908 |
" |
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 |
"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
|
911 |
! |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
912 |
|
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
913 |
matchScanArrayFrom:aString escapeCharacter:escape |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
914 |
"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
|
915 |
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
|
916 |
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
|
917 |
|
8874 | 918 |
|coll |
608 | 919 |
idx "{ Class: SmallInteger }" |
9282
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
920 |
end c1 c2 matchSet previous| |
5342
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
921 |
|
5487 | 922 |
previous := nil. |
608 | 923 |
|
924 |
coll := OrderedCollection new. |
|
925 |
idx := 1. end := aString size. |
|
926 |
[idx <= end] whileTrue:[ |
|
20874 | 927 |
|char this| |
928 |
||
929 |
char := aString at:idx. |
|
930 |
char == $* ifTrue:[ |
|
931 |
previous ~~ #anyString ifTrue:[ |
|
932 |
this := #anyString |
|
933 |
] |
|
934 |
] ifFalse:[ |
|
935 |
char == $# ifTrue:[ |
|
936 |
previous ~~ #anyString ifTrue:[ |
|
937 |
this := #any |
|
938 |
] |
|
939 |
] ifFalse:[ |
|
940 |
char == $[ ifTrue:[ |
|
941 |
matchSet := IdentitySet new. |
|
942 |
idx := idx + 1. |
|
943 |
idx > end ifTrue:[^ nil]. |
|
944 |
char := aString at:idx. |
|
945 |
c1 := nil. |
|
946 |
[char ~~ $]] whileTrue:[ |
|
947 |
((char == $-) and:[c1 notNil]) ifTrue:[ |
|
948 |
idx := idx + 1. |
|
949 |
idx > end ifTrue:[^ nil]. |
|
950 |
c2 := aString at:idx. |
|
951 |
c1 to:c2 do:[:c | matchSet add:c]. |
|
952 |
c1 := nil. |
|
953 |
idx := idx + 1. |
|
954 |
] ifFalse:[ |
|
955 |
(char ~~ $]) ifTrue:[ |
|
956 |
matchSet add:char. |
|
957 |
c1 := char. |
|
958 |
idx := idx + 1 |
|
959 |
] |
|
960 |
]. |
|
961 |
idx > end ifTrue:[^ nil]. |
|
962 |
char := aString at:idx |
|
963 |
]. |
|
964 |
this := matchSet asString |
|
965 |
] ifFalse:[ |
|
966 |
char == escape ifTrue:[ |
|
967 |
idx := idx + 1. |
|
968 |
idx > end ifTrue:[ |
|
969 |
"/ mhmh - what should we do here ? |
|
970 |
this := char |
|
971 |
] ifFalse:[ |
|
972 |
this := aString at:idx. |
|
973 |
] |
|
974 |
] ifFalse:[ |
|
975 |
this := char |
|
976 |
] |
|
977 |
] |
|
978 |
] |
|
979 |
]. |
|
980 |
this notNil ifTrue:[coll add:this. previous := this]. |
|
981 |
idx := idx + 1 |
|
608 | 982 |
]. |
983 |
||
984 |
^ coll asArray |
|
985 |
||
986 |
" |
|
8874 | 987 |
String matchScanArrayFrom:'*ute*' |
988 |
String matchScanArrayFrom:'**ute**' |
|
989 |
String matchScanArrayFrom:'*uter' |
|
990 |
String matchScanArrayFrom:'\*uter' |
|
991 |
String matchScanArrayFrom:'[cC]#mpute[rR]' |
|
992 |
String matchScanArrayFrom:'[abcd]*' |
|
993 |
String matchScanArrayFrom:'[a-k]*' |
|
994 |
String matchScanArrayFrom:'*some*compl*ern*' |
|
995 |
String matchScanArrayFrom:'[a-' |
|
996 |
String matchScanArrayFrom:'[a-zA-Z]' |
|
997 |
String matchScanArrayFrom:'[a-z01234A-Z]' |
|
608 | 998 |
" |
2519
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
999 |
|
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
1000 |
"Modified: 2.4.1997 / 16:20:29 / cg" |
9192 | 1001 |
! ! |
1002 |
||
1003 |
!CharacterArray class methodsFor:'queries'! |
|
1004 |
||
1005 |
isAbstract |
|
11229 | 1006 |
"Return if this class is an abstract class. |
1007 |
True is returned for CharacterArray here; false for subclasses. |
|
19464 | 1008 |
Abstract subclasses must redefine this again." |
11229 | 1009 |
|
9192 | 1010 |
^ self == CharacterArray |
18597 | 1011 |
! |
1012 |
||
1013 |
speciesForCharacterSize:characterSize |
|
1014 |
"answer the class, that is able to hold characters of size characterSize" |
|
1015 |
||
1016 |
characterSize <= 8 ifTrue:[^ String]. |
|
1017 |
characterSize <= 16 ifTrue:[^ Unicode16String]. |
|
1018 |
^ Unicode32String |
|
9192 | 1019 |
! ! |
1020 |
||
22924 | 1021 |
|
22525 | 1022 |
!CharacterArray class methodsFor:'utilities'! |
1023 |
||
1024 |
withoutAmpersandEscapes:label |
|
1025 |
"remove single ampersands; |
|
1026 |
replace double ampersands by single ones. |
|
1027 |
This is used to unescape menu-labels |
|
1028 |
(which use the ampersand as shortKey marker)" |
|
1029 |
||
1030 |
^ String |
|
1031 |
streamContents:[:out | |
|
1032 |
|in ch next| |
|
1033 |
||
1034 |
in := label readStream. |
|
1035 |
[in atEnd] whileFalse:[ |
|
1036 |
ch := in next. |
|
1037 |
ch == $& ifTrue:[ |
|
1038 |
(next := in peek) == $& ifTrue:[ |
|
1039 |
in next. |
|
1040 |
out nextPut:ch |
|
1041 |
] ifFalse:[ |
|
1042 |
ch := next |
|
1043 |
]. |
|
1044 |
] ifFalse:[ |
|
1045 |
out nextPut:ch |
|
1046 |
]. |
|
1047 |
] |
|
1048 |
] |
|
1049 |
||
1050 |
" |
|
1051 |
String withoutAmpersandEscapes:'' |
|
1052 |
String withoutAmpersandEscapes:'a' |
|
1053 |
String withoutAmpersandEscapes:'abcd' |
|
1054 |
String withoutAmpersandEscapes:'&abcd' |
|
1055 |
String withoutAmpersandEscapes:'&abcd&' |
|
1056 |
String withoutAmpersandEscapes:'&a&b&c&d' |
|
1057 |
String withoutAmpersandEscapes:'&a&b&c&d&' |
|
1058 |
String withoutAmpersandEscapes:'&&a&&b&&c&&d&&' |
|
1059 |
String withoutAmpersandEscapes:'&&a&&b&&c&&d&' |
|
1060 |
String withoutAmpersandEscapes:'&a&&b&&c&&d&' |
|
1061 |
String withoutAmpersandEscapes:'a&&b&&c&&d' |
|
1062 |
" |
|
1063 |
! ! |
|
1064 |
||
23154 | 1065 |
|
9192 | 1066 |
!CharacterArray methodsFor:'Compatibility-ANSI'! |
1067 |
||
1068 |
addLineDelimiters |
|
1069 |
"Ansi compatibility - same as withCRs" |
|
1070 |
||
1071 |
^ self withCRs |
|
1072 |
||
1073 |
"Modified: / 13.11.2001 / 19:16:25 / cg" |
|
17543 | 1074 |
! ! |
1075 |
||
1076 |
!CharacterArray methodsFor:'Compatibility-Dolphin'! |
|
9192 | 1077 |
|
1078 |
copyExpanding:expandTable |
|
1079 |
"return a copy of myself, with translations from the expandTable sliced in. |
|
18392 | 1080 |
The argument is supposed to map from characters to either characters or strings. |
1081 |
Added for Dolphin compatibility" |
|
9192 | 1082 |
|
1083 |
|ds| |
|
1084 |
||
1085 |
ds := WriteStream on:(self species new). |
|
1086 |
self do:[:eachChar | |
|
20874 | 1087 |
|repl| |
1088 |
||
1089 |
repl := expandTable at:eachChar ifAbsent:[nil]. |
|
1090 |
repl isNil ifTrue:[ |
|
1091 |
ds nextPut:eachChar |
|
1092 |
] ifFalse:[ |
|
1093 |
repl size == 0 ifTrue:[ |
|
1094 |
ds nextPut:repl |
|
1095 |
] ifFalse:[ |
|
1096 |
ds nextPutAll:repl |
|
1097 |
] |
|
1098 |
]. |
|
9192 | 1099 |
]. |
6870 | 1100 |
^ ds contents. |
1101 |
! |
|
1102 |
||
1103 |
formatWith:aString |
|
11955 | 1104 |
"Compatibility method - do not use in new code. |
18392 | 1105 |
same as #bindWith: for Dolphin compatibility" |
6870 | 1106 |
|
1107 |
^ self bindWith:aString |
|
1108 |
||
1109 |
" |
|
1110 |
'hello%1world' formatWith:'123' |
|
1111 |
" |
|
1112 |
! |
|
1113 |
||
1114 |
formatWith:arg1 with:arg2 |
|
11955 | 1115 |
"Compatibility method - do not use in new code. |
18392 | 1116 |
same as #bindWith: for Dolphin compatibility" |
6870 | 1117 |
|
1118 |
^ self bindWith:arg1 with:arg2 |
|
1119 |
||
1120 |
" |
|
1121 |
'hello%1 %2world' formatWith:'123' with:234 |
|
1122 |
" |
|
1123 |
! |
|
1124 |
||
1125 |
formatWith:arg1 with:arg2 with:arg3 |
|
11955 | 1126 |
"Compatibility method - do not use in new code. |
18392 | 1127 |
same as #bindWith: for Dolphin compatibility" |
6870 | 1128 |
|
1129 |
^ self bindWith:arg1 with:arg2 with:arg3 |
|
1130 |
||
1131 |
" |
|
1132 |
'hello%1 %2 %3world' formatWith:'123' with:234 with:345 |
|
1133 |
" |
|
6391 | 1134 |
! ! |
1135 |
||
14681 | 1136 |
!CharacterArray methodsFor:'Compatibility-GNU'! |
1137 |
||
1138 |
% anArrayOfOperands |
|
1139 |
"return a copy of the receiver, where a '%i' escape |
|
21747 | 1140 |
is replaced by the corresponding string from the argument array. |
14681 | 1141 |
'i' may be between 1 and 9 (i.e. a maximum of 9 placeholders is allowed). |
1142 |
Added for GNU-ST compatibility." |
|
1143 |
||
1144 |
^ self expandPlaceholdersWith:anArrayOfOperands |
|
1145 |
||
1146 |
" |
|
1147 |
'do you prefer %1 or rather %2 (not talking about %3) ?' |
|
20874 | 1148 |
% #('smalltalk' 'c++' 'c') |
14681 | 1149 |
|
1150 |
'do you %(what) ?' |
|
20874 | 1151 |
% (Dictionary new at:#'what' put:'understand'; yourself) |
14681 | 1152 |
" |
21747 | 1153 |
|
1154 |
"Modified (comment): / 11-05-2017 / 12:43:08 / mawalch" |
|
14681 | 1155 |
! ! |
1156 |
||
7261 | 1157 |
!CharacterArray methodsFor:'Compatibility-ST/V'! |
608 | 1158 |
|
17517 | 1159 |
asArrayOfSubstrings |
1160 |
"return an array of substrings from the receiver, interpreting |
|
1161 |
separators (i.e. spaces & newlines) as word-delimiters. |
|
18390 | 1162 |
This has been added for ST/V compatibility |
1163 |
- the actual work is done in asCollectionOfWords. |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
1164 |
(sigh: it is called #'subStrings' in V'Age, #'substrings' in Squeak |
17517 | 1165 |
and #'asCollectionOfWords' in ST/X) " |
1166 |
||
1167 |
^ self asCollectionOfWords asArray |
|
1168 |
||
1169 |
" |
|
1170 |
'1 one two three four 5 five' asArrayOfSubstrings |
|
1171 |
'1 |
|
1172 |
one |
|
20874 | 1173 |
two three four 5 five' asArrayOfSubstrings |
17517 | 1174 |
" |
1175 |
! |
|
1176 |
||
19497 | 1177 |
equalsIgnoreCase:aString |
1178 |
"This is an ST/V compatibility method and an alias for sameAs:." |
|
1179 |
||
1180 |
^ self sameAs:aString |
|
1181 |
||
1182 |
" |
|
1183 |
'abc' equalsIgnoreCase: 'aBC' |
|
1184 |
" |
|
1185 |
! |
|
1186 |
||
608 | 1187 |
replChar:oldChar with:newChar |
19497 | 1188 |
"return a copy of the receiver, with all oldChars replaced by newChar. |
1189 |
This is an ST/V compatibility method and an alias for copyReplaceAll." |
|
608 | 1190 |
|
3656 | 1191 |
^ self copyReplaceAll:oldChar with:newChar |
608 | 1192 |
|
1193 |
" |
|
8874 | 1194 |
'12345678901234567890' replChar:$0 with:$* |
608 | 1195 |
" |
3656 | 1196 |
|
1197 |
"Modified: / 18.7.1998 / 22:52:57 / cg" |
|
608 | 1198 |
! |
1199 |
||
1200 |
replChar:oldChar withString:newString |
|
1201 |
"return a copy of the receiver, with all oldChars replaced |
|
1202 |
by newString (i.e. slice in the newString in place of the oldChar). |
|
1203 |
This is an ST/V compatibility method." |
|
1204 |
||
1205 |
|tmpStream| |
|
1206 |
||
15523 | 1207 |
tmpStream := self species writeStream. |
608 | 1208 |
self do:[:element | |
20874 | 1209 |
element = oldChar ifTrue:[ |
1210 |
tmpStream nextPutAll:newString |
|
1211 |
] ifFalse:[ |
|
1212 |
tmpStream nextPut:element |
|
1213 |
]. |
|
608 | 1214 |
]. |
1215 |
^ tmpStream contents |
|
1216 |
||
1217 |
" |
|
8874 | 1218 |
'12345678901234567890' replChar:$0 withString:'foo' |
1219 |
'a string with spaces' replChar:$ withString:' foo ' |
|
608 | 1220 |
" |
9192 | 1221 |
! |
1222 |
||
1223 |
replString:subString withString:newString |
|
4244
5deefa3144fb
added #replString:withString:
Claus Gittinger <cg@exept.de>
parents:
4156
diff
changeset
|
1224 |
"return a copy of the receiver, with all sequences of subString replaced |
18390 | 1225 |
by newString (i.e. slice in the newString in place of the oldString). |
19497 | 1226 |
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
|
1227 |
|
8347 | 1228 |
^ self copyReplaceString:subString withString:newString |
4244
5deefa3144fb
added #replString:withString:
Claus Gittinger <cg@exept.de>
parents:
4156
diff
changeset
|
1229 |
|
5deefa3144fb
added #replString:withString:
Claus Gittinger <cg@exept.de>
parents:
4156
diff
changeset
|
1230 |
" |
8874 | 1231 |
'12345678901234567890' replString:'123' withString:'OneTwoThree' |
1232 |
'12345678901234567890' replString:'123' withString:'*' |
|
1233 |
'12345678901234567890' replString:'234' withString:'foo' |
|
4244
5deefa3144fb
added #replString:withString:
Claus Gittinger <cg@exept.de>
parents:
4156
diff
changeset
|
1234 |
|
5deefa3144fb
added #replString:withString:
Claus Gittinger <cg@exept.de>
parents:
4156
diff
changeset
|
1235 |
('a string with spaces' replChar:$ withString:' foo ') |
20874 | 1236 |
replString:'foo' withString:'bar' |
4244
5deefa3144fb
added #replString:withString:
Claus Gittinger <cg@exept.de>
parents:
4156
diff
changeset
|
1237 |
" |
5deefa3144fb
added #replString:withString:
Claus Gittinger <cg@exept.de>
parents:
4156
diff
changeset
|
1238 |
|
8347 | 1239 |
"Modified: / 12-05-2004 / 12:00:27 / cg" |
9192 | 1240 |
! |
1241 |
||
9275 | 1242 |
subString:start to:end |
18390 | 1243 |
"same as copyFrom:to: |
19497 | 1244 |
This is an ST/V compatibility method and an alias for copyFrom:to:." |
18390 | 1245 |
|
9275 | 1246 |
^ self copyFrom:start to:end |
1247 |
||
1248 |
" |
|
1249 |
'12345678901234567890' subString:3 to:8 |
|
1250 |
" |
|
1251 |
! |
|
1252 |
||
9192 | 1253 |
trimBlanks |
14992 | 1254 |
"return a copy of the receiver without leading and trailing spaces. |
18464 | 1255 |
This is an ST/V compatibility method and an alias for withoutSpaces." |
9192 | 1256 |
|
1257 |
^ self withoutSpaces |
|
1258 |
||
1259 |
" |
|
1260 |
' spaces at beginning' trimBlanks |
|
1261 |
'spaces at end ' trimBlanks |
|
1262 |
' spaces at beginning and end ' trimBlanks |
|
1263 |
'no spaces' trimBlanks |
|
1264 |
" |
|
1265 |
! ! |
|
1266 |
||
1267 |
!CharacterArray methodsFor:'Compatibility-Squeak'! |
|
4409
86e049b21f8d
added #includesSubstring:caseSensitive:
Claus Gittinger <cg@exept.de>
parents:
4389
diff
changeset
|
1268 |
|
19171 | 1269 |
asBoldText |
1270 |
"return self as a bold text" |
|
1271 |
||
21843 | 1272 |
"this test allows for small non-gui apps to be built without libbasic2 (where Text is)" |
1273 |
Text isNil ifTrue:[^ self]. |
|
1274 |
^ Text string: self emphasis: #bold |
|
1275 |
||
1276 |
"Modified (comment): / 20-06-2017 / 08:23:49 / cg" |
|
19171 | 1277 |
! |
1278 |
||
6148 | 1279 |
asDate |
1280 |
"Many allowed forms, see Date.readFrom:" |
|
1281 |
||
1282 |
^ Date readFrom: (ReadStream on: self) |
|
1283 |
||
1284 |
" |
|
8874 | 1285 |
'30 Apr 1999' asDate dayName capitalized |
6148 | 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:[ |
|
20874 | 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 |
||
22598 | 1327 |
convertToEncoding:nameOfEncoding |
1328 |
"/ special case, because it is so common: |
|
1329 |
nameOfEncoding = 'utf8' ifTrue:[^ self utf8Encoded]. |
|
1330 |
^ (CharacterEncoder encoderFor:nameOfEncoding) encodeString:self. |
|
1331 |
||
1332 |
" |
|
1333 |
'hellöäü' convertToEncoding:'utf8' |
|
1334 |
'hello' convertToEncoding:'cp1252' |
|
1335 |
" |
|
1336 |
! |
|
1337 |
||
9192 | 1338 |
endsWithDigit |
1339 |
"Answer whether the receiver's final character represents a digit. 3/11/96 sw" |
|
1340 |
||
18391 | 1341 |
|len| |
1342 |
||
20873 | 1343 |
^ (len := self size) ~~ 0 and:[(self at:len) isDigit] |
9192 | 1344 |
! |
1345 |
||
10703
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
1346 |
findDelimiters:delimiters startingAt:start |
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
1347 |
"Answer the index of the character within the receiver, starting at start, |
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
1348 |
that matches one of the delimiters. |
18391 | 1349 |
If the receiver does not contain any of the delimiters, answer size + 1." |
9192 | 1350 |
|
1351 |
|idx| |
|
1352 |
||
1353 |
idx := self indexOfAny:delimiters startingAt:start. |
|
1354 |
idx == 0 ifTrue:[^ self size + 1]. |
|
1355 |
^ idx. |
|
1356 |
"/start to: self size do: [:i | |
|
1357 |
"/ |char| |
|
1358 |
"/ |
|
1359 |
"/ char := self at: i. |
|
1360 |
"/ delimiters do: [:delim | delim = char ifTrue: [^ i]] |
|
8971 | 1361 |
"/ ]. |
1362 |
"/ ^ self size + 1 |
|
1363 |
! |
|
1364 |
||
22005 | 1365 |
findString:key startingAt:start caseSensitive:caseSensitive |
1366 |
^ self indexOfSubCollection:key startingAt:start ifAbsent:[0] caseSensitive:caseSensitive |
|
1367 |
||
1368 |
"Created: / 13-07-2017 / 12:44:50 / cg" |
|
1369 |
! |
|
1370 |
||
13624 | 1371 |
findTokens:delimiterOrDelimiters |
7270 | 1372 |
"cg: I am not sure, if this is really the squeak semantics (w.r.t. empty fields)" |
1373 |
||
13624 | 1374 |
delimiterOrDelimiters size == 0 ifTrue:[ |
20874 | 1375 |
^ self asCollectionOfSubstringsSeparatedBy:delimiterOrDelimiters |
13624 | 1376 |
] ifFalse:[ |
20874 | 1377 |
^ self asCollectionOfSubstringsSeparatedByAny:delimiterOrDelimiters |
13624 | 1378 |
]. |
7270 | 1379 |
|
1380 |
" |
|
8874 | 1381 |
'a|b#c||e' findTokens:#($# $|) |
13624 | 1382 |
'a|b#c||e' findTokens:$| |
1383 |
" |
|
1384 |
||
1385 |
"Modified: / 04-09-2011 / 20:31:21 / cg" |
|
1386 |
"Modified (comment): / 05-09-2011 / 04:25:28 / cg" |
|
4981 | 1387 |
! |
1388 |
||
4974 | 1389 |
includesSubString:aString |
1390 |
"return true, if a substring is contained in the receiver. |
|
1391 |
The compare is case sensitive." |
|
1392 |
||
1393 |
^ self includesString:aString |
|
1394 |
||
1395 |
" |
|
8874 | 1396 |
'hello world' includesSubString:'Hel' |
9192 | 1397 |
'hello world' includesSubString:'hel' |
1398 |
'hello world' includesSubString:'llo' |
|
1399 |
" |
|
1400 |
||
1401 |
||
1402 |
||
1403 |
! |
|
1404 |
||
11178 | 1405 |
includesSubString:aString caseSensitive:caseSensitive |
18391 | 1406 |
"sigh - an alias; added for Squeak/Pharo compatibility" |
1407 |
||
11179 | 1408 |
^ self includesSubstring:aString caseSensitive:caseSensitive |
11178 | 1409 |
! |
1410 |
||
16875
32e61bb3a742
class: CharacterArray>>#includesSubstring: (for Squeak compatibility)
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
16863
diff
changeset
|
1411 |
includesSubstring: aString |
18391 | 1412 |
"sigh - an alias; added for Squeak/Pharo compatibility" |
1413 |
||
16875
32e61bb3a742
class: CharacterArray>>#includesSubstring: (for Squeak compatibility)
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
16863
diff
changeset
|
1414 |
^ self includesSubString: aString |
32e61bb3a742
class: CharacterArray>>#includesSubstring: (for Squeak compatibility)
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
16863
diff
changeset
|
1415 |
|
32e61bb3a742
class: CharacterArray>>#includesSubstring: (for Squeak compatibility)
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
16863
diff
changeset
|
1416 |
"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
|
1417 |
! |
32e61bb3a742
class: CharacterArray>>#includesSubstring: (for Squeak compatibility)
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
16863
diff
changeset
|
1418 |
|
9192 | 1419 |
includesSubstring:aString caseSensitive:caseSensitive |
9190 | 1420 |
"return true, if a substring is contained in the receiver. |
9192 | 1421 |
The argument, caseSensitive controls if case is ignored in the compare." |
1422 |
||
1423 |
"/ for now, a q&d hack ... |
|
1424 |
||
1425 |
caseSensitive ifFalse:[ |
|
20874 | 1426 |
^ self asLowercase includesString:aString asLowercase |
9192 | 1427 |
]. |
9190 | 1428 |
^ self includesString:aString |
1429 |
||
1430 |
" |
|
9192 | 1431 |
'hello world' includesSubstring:'Hel' caseSensitive:true |
1432 |
'hello world' includesSubstring:'Hel' caseSensitive:false |
|
1433 |
" |
|
1434 |
||
1435 |
||
1436 |
||
1437 |
! |
|
9190 | 1438 |
|
6979 | 1439 |
isAllDigits |
1440 |
"Answer whether the receiver's characters are all digits" |
|
1441 |
||
1442 |
^ self conform:[:eachChar | eachChar isDigit] |
|
1443 |
||
1444 |
" |
|
1445 |
'hello world' isAllDigits |
|
1446 |
'12344' isAllDigits |
|
1447 |
" |
|
1448 |
! |
|
1449 |
||
4890 | 1450 |
lastSpacePosition |
18391 | 1451 |
"return the index of the last space character; 0 if there is none. |
1452 |
Added for Squeak/Pharo compatibility" |
|
14405
1d0d99a95bba
comment/format in:7 methods
Claus Gittinger <cg@exept.de>
parents:
14168
diff
changeset
|
1453 |
|
20739 | 1454 |
"/ CG: is this correct? separator or space only??? |
4890 | 1455 |
^ self lastIndexOfSeparator |
1456 |
! |
|
1457 |
||
20738 | 1458 |
linesDo:aBlock |
1459 |
"evaluate the argument, aBlock for all lines, |
|
1460 |
up to the end" |
|
1461 |
||
1462 |
^ self readStream linesDo:aBlock |
|
1463 |
! |
|
1464 |
||
7022 | 1465 |
padded:leftOrRight to:paddedSize with:padCharacter |
20155 | 1466 |
"pad left (leftOrRight == #left) or right" |
14053
acbb97913c30
comment/format in: #padded:to:with:
Claus Gittinger <cg@exept.de>
parents:
14050
diff
changeset
|
1467 |
|
7022 | 1468 |
leftOrRight == #left ifTrue:[ |
20155 | 1469 |
^ self leftPaddedTo:paddedSize with:padCharacter |
9190 | 1470 |
]. |
1471 |
^ self paddedTo:paddedSize with:padCharacter |
|
1472 |
||
1473 |
" |
|
8874 | 1474 |
'hello' padded:#right to:10 with:$. |
9192 | 1475 |
'hello' padded:#left to:10 with:$. |
1476 |
" |
|
14053
acbb97913c30
comment/format in: #padded:to:with:
Claus Gittinger <cg@exept.de>
parents:
14050
diff
changeset
|
1477 |
|
acbb97913c30
comment/format in: #padded:to:with:
Claus Gittinger <cg@exept.de>
parents:
14050
diff
changeset
|
1478 |
"Modified (comment): / 07-03-2012 / 16:31:33 / cg" |
9192 | 1479 |
! |
1480 |
||
1481 |
skipDelimiters:delimiters startingAt:start |
|
1482 |
"Answer the index of the character within the receiver, starting at start, |
|
1483 |
that does NOT match one of the delimiters. |
|
1484 |
If the receiver does not contain any of the delimiters, answer size + 1. |
|
1485 |
Assumes the delimiters to be a non-empty string." |
|
1486 |
||
1487 |
start to:self size do:[:i | |
|
20874 | 1488 |
delimiters detect:[:delim | delim = (self at:i) ] ifNone:[ ^ i ] |
9192 | 1489 |
]. |
1490 |
^ self size + 1 |
|
1491 |
||
1492 |
" |
|
1493 |
'123***7890' skipDelimiters:'*' startingAt:4 |
|
1494 |
'123***7890' skipDelimiters:'*' startingAt:3 |
|
9190 | 1495 |
'123***7890' skipDelimiters:'*' startingAt:10 |
1496 |
'123*******' skipDelimiters:'*' startingAt:10 |
|
8874 | 1497 |
" |
1498 |
! |
|
1499 |
||
9190 | 1500 |
substrings |
18921 | 1501 |
"return a collection consisting of all words contained in the receiver. |
17517 | 1502 |
Words are separated by whitespace. |
1503 |
This has been added for Squeak compatibility. |
|
1504 |
(sigh: it is called #'subStrings' in V'Age, and #'asCollectionOfWords' in ST/X) " |
|
1505 |
||
9190 | 1506 |
^ self asCollectionOfWords |
1507 |
||
1508 |
" |
|
1509 |
'foo bar baz' substrings |
|
1510 |
" |
|
1511 |
! |
|
1512 |
||
12665
b027fc5aec4e
added: #substringsSeparatedBy:
Claus Gittinger <cg@exept.de>
parents:
12664
diff
changeset
|
1513 |
substringsSeparatedBy:separatorCharacter |
18921 | 1514 |
"return a collection consisting of all words contained in the receiver. |
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
1515 |
Words are separated by the given separator character. |
18921 | 1516 |
This has been added for Squeak/Pharo compatibility. |
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
1517 |
(sigh: it is called #'subStrings:' in V'Age, |
18921 | 1518 |
and #'asCollectionOfSubstringsSeparatedBy' in ST/X) " |
18391 | 1519 |
|
12665
b027fc5aec4e
added: #substringsSeparatedBy:
Claus Gittinger <cg@exept.de>
parents:
12664
diff
changeset
|
1520 |
^ self asCollectionOfSubstringsSeparatedBy:separatorCharacter |
b027fc5aec4e
added: #substringsSeparatedBy:
Claus Gittinger <cg@exept.de>
parents:
12664
diff
changeset
|
1521 |
|
b027fc5aec4e
added: #substringsSeparatedBy:
Claus Gittinger <cg@exept.de>
parents:
12664
diff
changeset
|
1522 |
" |
b027fc5aec4e
added: #substringsSeparatedBy:
Claus Gittinger <cg@exept.de>
parents:
12664
diff
changeset
|
1523 |
'foo bar, baz' substringsSeparatedBy:$, |
21151 | 1524 |
'1.2.3.4' substringsSeparatedBy:$. |
12665
b027fc5aec4e
added: #substringsSeparatedBy:
Claus Gittinger <cg@exept.de>
parents:
12664
diff
changeset
|
1525 |
" |
b027fc5aec4e
added: #substringsSeparatedBy:
Claus Gittinger <cg@exept.de>
parents:
12664
diff
changeset
|
1526 |
! |
b027fc5aec4e
added: #substringsSeparatedBy:
Claus Gittinger <cg@exept.de>
parents:
12664
diff
changeset
|
1527 |
|
9190 | 1528 |
truncateTo:smallSize |
9192 | 1529 |
"return myself or a copy shortened to smallSize. 1/18/96 sw" |
1530 |
||
1531 |
self size <= smallSize ifTrue:[^ self]. |
|
1532 |
^ self copyFrom: 1 to: smallSize |
|
1533 |
||
1534 |
" |
|
1535 |
'hello world' truncateTo:5 |
|
1536 |
'hello' truncateTo:10 |
|
1537 |
||
1538 |
'hello world' copyTo:5 |
|
1539 |
'hello' copyTo:10 |
|
1540 |
" |
|
1541 |
! |
|
5795 | 1542 |
|
4890 | 1543 |
withBlanksTrimmed |
18464 | 1544 |
"Return a copy of the receiver from which leading and trailing whitespace have been trimmed. |
1545 |
Notice the bad naming - it is trimming separators, not just blanks. |
|
1546 |
Added for Squeak compatibility and an alias for withoutSeparators" |
|
4890 | 1547 |
|
14865 | 1548 |
^ self withoutSeparators "withoutSpaces" "/ cg: it seems that squeak cares for any whitespace |
4890 | 1549 |
|
1550 |
" |
|
8874 | 1551 |
' hello world ' withBlanksTrimmed |
4890 | 1552 |
" |
1553 |
! |
|
1554 |
||
1555 |
withNoLineLongerThan: aNumber |
|
1556 |
"Answer a string with the same content as receiver, but rewrapped so that no line has more characters than the given number" |
|
1557 |
||
9192 | 1558 |
| listOfLines currentLast currentStart resultString putativeLast putativeLine crPosition | |
1559 |
||
14451
c06e7885b675
changed: #withNoLineLongerThan:
Claus Gittinger <cg@exept.de>
parents:
14420
diff
changeset
|
1560 |
(aNumber isNumber not or:[ aNumber < 1] ) ifTrue: [self error: 'too narrow']. |
9192 | 1561 |
listOfLines _ OrderedCollection new. |
1562 |
currentLast _ 0. |
|
1563 |
[currentLast < self size] whileTrue: |
|
20874 | 1564 |
[currentStart _ currentLast + 1. |
1565 |
putativeLast _ (currentStart + aNumber - 1) min: self size. |
|
1566 |
putativeLine _ self copyFrom: currentStart to: putativeLast. |
|
1567 |
(crPosition _ putativeLine indexOf: Character cr) > 0 ifTrue: |
|
1568 |
[putativeLast _ currentStart + crPosition - 1. |
|
1569 |
putativeLine _ self copyFrom: currentStart to: putativeLast]. |
|
1570 |
currentLast _ putativeLast == self size |
|
1571 |
ifTrue: |
|
1572 |
[putativeLast] |
|
1573 |
ifFalse: |
|
1574 |
[currentStart + putativeLine lastSpacePosition - 1]. |
|
1575 |
currentLast <= currentStart ifTrue: |
|
1576 |
["line has NO spaces; baleout!!" |
|
1577 |
currentLast _ putativeLast]. |
|
1578 |
listOfLines add: (self copyFrom: currentStart to: currentLast) withBlanksTrimmed]. |
|
9192 | 1579 |
|
1580 |
listOfLines size > 0 ifFalse: [^ '']. |
|
1581 |
resultString _ listOfLines first. |
|
1582 |
2 to: listOfLines size do: |
|
20874 | 1583 |
[:i | resultString _ resultString, Character cr asString, (listOfLines at: i)]. |
9192 | 1584 |
^ resultString |
1585 |
||
1586 |
" |
|
1587 |
#(5 7 20) collect: |
|
20874 | 1588 |
[: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
|
1589 |
" |
10883 | 1590 |
! |
1591 |
||
12343
0a93eee37d65
added: #withSqueakLineEndings
Claus Gittinger <cg@exept.de>
parents:
12330
diff
changeset
|
1592 |
withSqueakLineEndings |
13882
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
1593 |
"assume the string is textual, and that CR, LF, and CRLF are all |
20258 | 1594 |
valid line endings. Replace each occurrence with a single CR" |
12343
0a93eee37d65
added: #withSqueakLineEndings
Claus Gittinger <cg@exept.de>
parents:
12330
diff
changeset
|
1595 |
|
0a93eee37d65
added: #withSqueakLineEndings
Claus Gittinger <cg@exept.de>
parents:
12330
diff
changeset
|
1596 |
^ self asStringCollection asStringWith:Character cr. |
0a93eee37d65
added: #withSqueakLineEndings
Claus Gittinger <cg@exept.de>
parents:
12330
diff
changeset
|
1597 |
! |
0a93eee37d65
added: #withSqueakLineEndings
Claus Gittinger <cg@exept.de>
parents:
12330
diff
changeset
|
1598 |
|
12188 | 1599 |
withoutLeading: char |
1600 |
"return a copy of myself without leading a char. |
|
1601 |
Returns an empty string, if the receiver consist only of a char." |
|
1602 |
||
1603 |
^ self withoutLeadingForWhich:[:ch | ch = char] |
|
1604 |
||
1605 |
" |
|
13882
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
1606 |
'****foo****' withoutLeading: $* |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
1607 |
'foo****' withoutLeading: $* |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
1608 |
'*' withoutLeading: $* |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
1609 |
'' withoutLeading: $* |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
1610 |
'****foo' withoutLeading: $* |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
1611 |
'*******' withoutLeading: $* |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
1612 |
'foo' withoutLeading: $* |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
1613 |
'f***o***o' withoutLeading: $* |
12188 | 1614 |
('**' , Character tab asString , '*foo***') withoutLeading: $* inspect |
1615 |
" |
|
1616 |
! |
|
1617 |
||
11565 | 1618 |
withoutTrailing:char |
1619 |
"return a copy of myself without trailing char. |
|
1620 |
Returns an empty string, if the receiver consist only of char." |
|
1621 |
||
12188 | 1622 |
^ self withoutTrailingForWhich:[:ch | ch = char] |
1623 |
||
1624 |
" |
|
13882
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
1625 |
' foo....' withoutTrailing:$. |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
1626 |
'foo....' withoutTrailing:$. |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
1627 |
' foo' withoutTrailing:$. |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
1628 |
'.......' withoutTrailing:$. |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
1629 |
'foo' withoutTrailing:$. |
11565 | 1630 |
" |
9190 | 1631 |
! ! |
1632 |
||
1633 |
!CharacterArray methodsFor:'Compatibility-V''Age'! |
|
1634 |
||
1635 |
addLineDelimiter |
|
1636 |
"replace all '\'-characters by line delimiter (cr) - characters. |
|
9192 | 1637 |
This has been added for VisualAge compatibility." |
1638 |
||
1639 |
^ self withCRs |
|
1640 |
! |
|
1641 |
||
1642 |
bindWith:aString |
|
1643 |
"return a copy of the receiver, where a '%1' escape is |
|
1644 |
replaced by aString. |
|
1645 |
This has been added for VisualAge compatibility." |
|
1646 |
||
1647 |
^ self expandPlaceholdersWith:(Array with:aString) |
|
1648 |
||
1649 |
" |
|
1650 |
'do you like %1 ?' bindWith:'smalltalk' |
|
19785 | 1651 |
'do you like %(foo) ?' bindWithArguments:(Dictionary new at:'foo' put:'smalltalk'; yourself) |
9192 | 1652 |
" |
1653 |
! |
|
608 | 1654 |
|
1655 |
bindWith:string1 with:string2 |
|
1656 |
"return a copy of the receiver, where a '%1' escape is |
|
1657 |
replaced by string1 and '%2' is replaced by string2. |
|
1658 |
This has been added for VisualAge compatibility." |
|
1659 |
||
1660 |
^ self expandPlaceholdersWith:(Array with:string1 with:string2) |
|
1661 |
||
1662 |
" |
|
1663 |
'do you prefer %1 or rather %2 ?' |
|
20874 | 1664 |
bindWith:'smalltalk' with:'c++' |
9192 | 1665 |
" |
1666 |
! |
|
9190 | 1667 |
|
1668 |
bindWith:str1 with:str2 with:str3 |
|
608 | 1669 |
"return a copy of the receiver, where a '%1', '%2' and '%3' escapes |
1670 |
are replaced by str1, str2 and str3 respectively. |
|
1671 |
This has been added for VisualAge compatibility." |
|
1672 |
||
1673 |
^ self expandPlaceholdersWith:(Array with:str1 with:str2 with:str3) |
|
1674 |
||
1675 |
" |
|
9192 | 1676 |
'do you prefer %1 or rather %2 (not talking about %3) ?' |
20874 | 1677 |
bindWith:'smalltalk' with:'c++' with:'c' |
9192 | 1678 |
" |
1679 |
! |
|
608 | 1680 |
|
1681 |
bindWith:str1 with:str2 with:str3 with:str4 |
|
1682 |
"return a copy of the receiver, where a '%1', '%2', '%3' and '%4' escapes |
|
1683 |
are replaced by str1, str2, str3 and str4 respectively. |
|
1684 |
This has been added for VisualAge compatibility." |
|
1685 |
||
1686 |
^ self expandPlaceholdersWith:(Array with:str1 with:str2 with:str3 with:str4) |
|
1687 |
||
1688 |
" |
|
9192 | 1689 |
'do you prefer %1 or rather %2 (not talking about %3 or even %4) ?' |
20874 | 1690 |
bindWith:'smalltalk' with:'c++' with:'c' with:'assembler' |
9192 | 1691 |
" |
1692 |
! |
|
608 | 1693 |
|
2344 | 1694 |
bindWith:str1 with:str2 with:str3 with:str4 with:str5 |
1695 |
"return a copy of the receiver, where a '%1' .. '%5' escapes |
|
1696 |
are replaced by str1 .. str5 respectively. |
|
1697 |
This has been added for VisualAge compatibility." |
|
1698 |
||
1699 |
^ self expandPlaceholdersWith:(Array with:str1 with:str2 with:str3 with:str4 with:str5) |
|
1700 |
||
1701 |
"Created: 31.1.1997 / 16:25:42 / cg" |
|
1702 |
! |
|
1703 |
||
5343 | 1704 |
bindWith:str1 with:str2 with:str3 with:str4 with:str5 with:str6 |
1705 |
"return a copy of the receiver, where a '%1' .. '%6' escapes |
|
11524 | 1706 |
are replaced by str1 .. str6 respectively. |
5343 | 1707 |
This has been added for VisualAge compatibility." |
1708 |
||
8874 | 1709 |
^ self expandPlaceholdersWith:(Array with:str1 with:str2 |
20874 | 1710 |
with:str3 with:str4 |
1711 |
with:str5 with:str6) |
|
11524 | 1712 |
! |
1713 |
||
1714 |
bindWith:str1 with:str2 with:str3 with:str4 with:str5 with:str6 with:str7 |
|
1715 |
"return a copy of the receiver, where a '%1' .. '%7' escapes |
|
1716 |
are replaced by str1 .. str7 respectively. |
|
1717 |
This has been added for VisualAge compatibility." |
|
1718 |
||
1719 |
^ self expandPlaceholdersWith:(Array with:str1 with:str2 |
|
20874 | 1720 |
with:str3 with:str4 |
1721 |
with:str5 with:str6 |
|
1722 |
with:str7) |
|
5343 | 1723 |
! |
1724 |
||
13996
a86faa4f17b8
added: #bindWith:with:with:with:with:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
13987
diff
changeset
|
1725 |
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
|
1726 |
"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
|
1727 |
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
|
1728 |
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
|
1729 |
|
a86faa4f17b8
added: #bindWith:with:with:with:with:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
13987
diff
changeset
|
1730 |
^ self expandPlaceholdersWith:(Array with:str1 with:str2 |
20874 | 1731 |
with:str3 with:str4 |
1732 |
with:str5 with:str6 |
|
1733 |
with:str7 with:str8) |
|
13996
a86faa4f17b8
added: #bindWith:with:with:with:with:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
13987
diff
changeset
|
1734 |
|
a86faa4f17b8
added: #bindWith:with:with:with:with:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
13987
diff
changeset
|
1735 |
"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
|
1736 |
! |
a86faa4f17b8
added: #bindWith:with:with:with:with:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
13987
diff
changeset
|
1737 |
|
14014
3aff2d7c41c8
added: #bindWith:with:with:with:with:with:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
13996
diff
changeset
|
1738 |
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
|
1739 |
"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
|
1740 |
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
|
1741 |
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
|
1742 |
|
3aff2d7c41c8
added: #bindWith:with:with:with:with:with:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
13996
diff
changeset
|
1743 |
^ self expandPlaceholdersWith:(Array with:str1 with:str2 |
20874 | 1744 |
with:str3 with:str4 |
1745 |
with:str5 with:str6 |
|
1746 |
with:str7 with:str8 |
|
1747 |
with:str9) |
|
14014
3aff2d7c41c8
added: #bindWith:with:with:with:with:with:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
13996
diff
changeset
|
1748 |
|
3aff2d7c41c8
added: #bindWith:with:with:with:with:with:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
13996
diff
changeset
|
1749 |
"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
|
1750 |
! |
3aff2d7c41c8
added: #bindWith:with:with:with:with:with:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
13996
diff
changeset
|
1751 |
|
22924 | 1752 |
bindWithArguments:argumentsCollection |
608 | 1753 |
"return a copy of the receiver, where a '%i' escape |
21747 | 1754 |
is replaced by the corresponding string from the argument array. |
22924 | 1755 |
'i' may be between 1 and 9 (i.e. a maximum of 9 placeholders is allowed) |
1756 |
or %(key); the argumentsCollection must then be a dictionary. |
|
21255 | 1757 |
To get an integer-indexed placeHolder followed by another digit, |
1758 |
or an index > 9, you must use %(digit). |
|
608 | 1759 |
This has been added for VisualAge compatibility." |
1760 |
||
22924 | 1761 |
^ self expandPlaceholdersWith:argumentsCollection |
9192 | 1762 |
|
1763 |
" |
|
1764 |
'do you prefer %1 or rather %2 (not talking about %3) ?' |
|
20874 | 1765 |
bindWithArguments:#('smalltalk' 'c++' 'c') |
12196 | 1766 |
|
1767 |
'do you %(what) ?' |
|
20874 | 1768 |
bindWithArguments:(Dictionary new at:#'what' put:'understand'; yourself) |
9192 | 1769 |
" |
21747 | 1770 |
|
1771 |
"Modified (comment): / 11-05-2017 / 12:42:57 / mawalch" |
|
9192 | 1772 |
! |
608 | 1773 |
|
1774 |
subStrings |
|
18921 | 1775 |
"return a collection consisting of all words contained in the receiver. |
608 | 1776 |
Words are separated by whitespace. |
17517 | 1777 |
This has been added for VisualAge compatibility. |
1778 |
(sigh: it is called #'subbtrings' in Squeak, and #'asCollectionOfWords' in ST/X) " |
|
608 | 1779 |
|
1780 |
^ self asCollectionOfWords |
|
1781 |
||
1782 |
" |
|
1783 |
'hello world, this is smalltalk' subStrings |
|
1784 |
" |
|
1785 |
! |
|
1786 |
||
16181 | 1787 |
subStrings:separatorCharacterOrString |
18921 | 1788 |
"return a collection consisting of all words contained in the receiver. |
608 | 1789 |
Words are separated by separatorCharacter. |
18921 | 1790 |
This is similar to split: (squeak) and asCollectionOfSubstringsSeparatedBy: (st/x) |
1791 |
and has been added for VisualAge compatibility." |
|
608 | 1792 |
|
16181 | 1793 |
separatorCharacterOrString isCharacter ifTrue:[ |
20874 | 1794 |
^ self asCollectionOfSubstringsSeparatedBy:separatorCharacterOrString |
16181 | 1795 |
]. |
1796 |
^ self asCollectionOfSubstringsSeparatedByAny:separatorCharacterOrString |
|
1797 |
||
1798 |
" |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
1799 |
'foo:bar:baz:smalltalk' subStrings:$: |
16181 | 1800 |
'foo:bar:baz:smalltalk' subStrings:':' |
1801 |
'foo.bar,baz-smalltalk' subStrings:'.,-' |
|
9192 | 1802 |
" |
1803 |
! |
|
1804 |
||
1805 |
trimSeparators |
|
18392 | 1806 |
"return a copy of the receiver without leading and trailing whiteSpace. |
18921 | 1807 |
Added for VisualAge compatibility (an alias for withoutSeparators)" |
9192 | 1808 |
|
1809 |
^ self withoutSeparators |
|
1810 |
! ! |
|
1811 |
||
1812 |
!CharacterArray methodsFor:'Compatibility-VW'! |
|
5964 | 1813 |
|
6139 | 1814 |
asLogicalFileSpecification |
1815 |
^ self asFilename |
|
1816 |
||
1817 |
"Created: / 30.10.2001 / 17:29:53 / cg" |
|
1818 |
! |
|
1819 |
||
5964 | 1820 |
expandMacros |
1821 |
"ST80 compatibility - expand '<..>' macros with |
|
1822 |
argument strings. Similar to #bindWith:. |
|
1823 |
Read the comment in #expandMacrosWithArguments: about |
|
1824 |
limited compatibility issues." |
|
1825 |
||
1826 |
^ self expandMacrosWithArguments:#() |
|
1827 |
||
1828 |
" |
|
1829 |
'hello<n>foo' expandMacros |
|
1830 |
" |
|
1831 |
||
9192 | 1832 |
"Modified: / 18.6.1998 / 16:03:02 / cg" |
1833 |
! |
|
1834 |
||
1835 |
expandMacrosWith:arg |
|
5964 | 1836 |
"ST80 compatibility - expand '<..>' macros with |
1837 |
argument strings. Similar to #bindWith:. |
|
1838 |
Read the comment in #expandMacrosWithArguments: about |
|
1839 |
limited compatibility issues." |
|
1840 |
||
9192 | 1841 |
^ self expandMacrosWithArguments:(Array with:arg) |
1842 |
||
1843 |
"Created: / 1.11.1997 / 13:01:28 / cg" |
|
1844 |
"Modified: / 1.11.1997 / 13:30:50 / cg" |
|
1845 |
! |
|
5964 | 1846 |
|
1847 |
expandMacrosWith:arg1 with:arg2 |
|
1848 |
"ST80 compatibility - expand '<..>' macros with |
|
1849 |
argument strings. Similar to #bindWith:. |
|
1850 |
Read the comment in #expandMacrosWithArguments: about |
|
1851 |
limited compatibility issues." |
|
1852 |
||
1853 |
^ self expandMacrosWithArguments:(Array with:arg1 with:arg2) |
|
1854 |
||
1855 |
"Modified: / 6.7.1998 / 21:58:14 / cg" |
|
1856 |
! |
|
1857 |
||
1858 |
expandMacrosWith:arg1 with:arg2 with:arg3 |
|
1859 |
"ST80 compatibility - expand '<..>' macros with |
|
1860 |
argument strings. Similar to #bindWith:. |
|
1861 |
Read the comment in #expandMacrosWithArguments: about |
|
1862 |
limited compatibility issues." |
|
1863 |
||
1864 |
^ self expandMacrosWithArguments:(Array with:arg1 with:arg2 with:arg3) |
|
1865 |
! |
|
1866 |
||
10353
65fb7aa2c6c2
added #expandMacrosWith:with:with:with:
Stefan Vogel <sv@exept.de>
parents:
10134
diff
changeset
|
1867 |
expandMacrosWith:arg1 with:arg2 with:arg3 with:arg4 |
65fb7aa2c6c2
added #expandMacrosWith:with:with:with:
Stefan Vogel <sv@exept.de>
parents:
10134
diff
changeset
|
1868 |
"ST80 compatibility - expand '<..>' macros with |
65fb7aa2c6c2
added #expandMacrosWith:with:with:with:
Stefan Vogel <sv@exept.de>
parents:
10134
diff
changeset
|
1869 |
argument strings. Similar to #bindWith:. |
65fb7aa2c6c2
added #expandMacrosWith:with:with:with:
Stefan Vogel <sv@exept.de>
parents:
10134
diff
changeset
|
1870 |
Read the comment in #expandMacrosWithArguments: about |
65fb7aa2c6c2
added #expandMacrosWith:with:with:with:
Stefan Vogel <sv@exept.de>
parents:
10134
diff
changeset
|
1871 |
limited compatibility issues." |
65fb7aa2c6c2
added #expandMacrosWith:with:with:with:
Stefan Vogel <sv@exept.de>
parents:
10134
diff
changeset
|
1872 |
|
65fb7aa2c6c2
added #expandMacrosWith:with:with:with:
Stefan Vogel <sv@exept.de>
parents:
10134
diff
changeset
|
1873 |
^ 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
|
1874 |
! |
65fb7aa2c6c2
added #expandMacrosWith:with:with:with:
Stefan Vogel <sv@exept.de>
parents:
10134
diff
changeset
|
1875 |
|
9192 | 1876 |
expandMacrosWithArguments:argArray |
5964 | 1877 |
"ST80 compatibility - expand '<..>' macros with |
1878 |
argument strings. Similar to #bindWith:. |
|
1879 |
WARNING: possibly not all ST80 expansions are supported here." |
|
1880 |
||
1881 |
"/ supported expansions: |
|
1882 |
"/ |
|
1883 |
"/ <#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
|
1884 |
"/ <#s> # is arg Number; slice in the arg itself (must know asString) |
5964 | 1885 |
"/ <#?s1:s2> # is arg Number; slice in s1 if the arg is true, s2 otherwise |
7643 | 1886 |
"/ use first arg if # is not given (i.e. no number before s,p ...) |
5964 | 1887 |
"/ <n> replace by a newLine character |
1888 |
"/ <t> replace by a tab character |
|
6834
eba4b58b8692
fixed expandMacros (%-escape)
Claus Gittinger <cg@exept.de>
parents:
6810
diff
changeset
|
1889 |
"/ %X the X character itself |
5964 | 1890 |
|
8999
f3fda66ea51e
#expandMacrosWithArguments: - be mor friendly to xml strings.
Stefan Vogel <sv@exept.de>
parents:
8971
diff
changeset
|
1891 |
|in out c fmt nr arg s1 s2 peekc| |
5964 | 1892 |
|
1893 |
in := self readStream. |
|
10721
58e6950264cb
care for bitsPerCharacter when expanding macros
Claus Gittinger <cg@exept.de>
parents:
10703
diff
changeset
|
1894 |
out := CharacterWriteStream on:(self species uninitializedNew:self size). |
5964 | 1895 |
|
1896 |
[in atEnd] whileFalse:[ |
|
20874 | 1897 |
c := in next. |
1898 |
c == $% ifTrue:[ |
|
1899 |
c := in next. |
|
1900 |
out nextPut:c |
|
1901 |
] ifFalse:[c ~~ $< ifTrue:[ |
|
1902 |
out nextPut:c. |
|
1903 |
] ifFalse:[ |
|
1904 |
peekc := in peek. |
|
1905 |
[peekc == $<] whileTrue:[ |
|
1906 |
out nextPut:$<. |
|
1907 |
peekc := in nextPeek. |
|
1908 |
]. |
|
1909 |
peekc == $n ifTrue:[ |
|
1910 |
peekc := in nextPeek. |
|
1911 |
peekc == $> ifTrue:[ |
|
1912 |
in next. |
|
1913 |
out cr. |
|
1914 |
] ifFalse:[ |
|
1915 |
out nextPutAll:'<n'. |
|
1916 |
] |
|
1917 |
] ifFalse:[peekc == $t ifTrue:[ |
|
1918 |
peekc := in nextPeek. |
|
1919 |
peekc == $> ifTrue:[ |
|
1920 |
in next. |
|
1921 |
out tab. |
|
1922 |
] ifFalse:[ |
|
1923 |
out nextPutAll:'<t'. |
|
1924 |
] |
|
1925 |
] ifFalse:[ |
|
1926 |
peekc isDigit ifTrue:[ |
|
1927 |
"start an argument expansion ..." |
|
1928 |
nr := Integer readFrom:in onError:nil. |
|
1929 |
nr isNil ifTrue:[ |
|
1930 |
"this cannot happen (there is at least one digit)" |
|
23009 | 1931 |
self proceedableError:'invalid format'. |
20874 | 1932 |
^ self |
1933 |
]. |
|
1934 |
fmt := in next. |
|
22999 | 1935 |
(fmt ~~ $? and:[fmt ~~ $# and:[in peek ~~ $>]]) ifTrue:[ |
20874 | 1936 |
out nextPut:$<. |
1937 |
nr printOn:out. |
|
1938 |
out nextPut:fmt. |
|
1939 |
] ifFalse:[ |
|
1940 |
(nr between:1 and:argArray size) ifTrue:[ |
|
1941 |
arg := argArray at:nr. |
|
1942 |
] ifFalse:[ |
|
1943 |
arg := '' |
|
1944 |
]. |
|
1945 |
||
1946 |
fmt == $p ifTrue:[ |
|
1947 |
"expand with args printString" |
|
1948 |
arg printOn:out. |
|
1949 |
] ifFalse:[fmt == $s ifTrue:[ |
|
1950 |
"expand with arg itself" |
|
1951 |
arg isText ifTrue:[ |
|
1952 |
out contentsSpecies isText ifFalse:[ |
|
1953 |
out := (TextStream ? CharacterWriteStream on:Text new) nextPutAll:out contents; yourself. |
|
1954 |
]. |
|
1955 |
out nextPutAll:arg. |
|
1956 |
] ifFalse:[ |
|
1957 |
out nextPutAll:arg asString string. "see method comment: arg must know #asString" |
|
1958 |
] |
|
22999 | 1959 |
] ifFalse:[(fmt == $? or:[fmt == $#]) ifTrue:[ |
20874 | 1960 |
s1 := in upTo:$:. |
1961 |
s2 := in nextUpTo:$>. |
|
22999 | 1962 |
((fmt == $? and:[arg == true]) |
23009 | 1963 |
or:[(fmt == $# and:[arg == 1])]) ifTrue:[ |
20874 | 1964 |
out nextPutAll:s1 |
1965 |
] ifFalse:[ |
|
1966 |
out nextPutAll:s2 |
|
1967 |
]. |
|
1968 |
] ifFalse:[ |
|
1969 |
"what does VW do here ?" |
|
1970 |
self error:'invalid format' mayProceed:true. |
|
1971 |
^ self |
|
1972 |
]]]. |
|
1973 |
c := in next. |
|
1974 |
c ~~ $> ifTrue:[ |
|
1975 |
"what does VW do here ?" |
|
1976 |
self error:'invalid format' mayProceed:true. |
|
1977 |
^ self |
|
1978 |
] |
|
1979 |
||
1980 |
]. |
|
1981 |
] ifFalse:[ |
|
1982 |
out nextPut:$<. |
|
1983 |
]. |
|
1984 |
]]. |
|
1985 |
]]. |
|
5964 | 1986 |
]. |
1987 |
^ out contents |
|
1988 |
||
1989 |
" |
|
8874 | 1990 |
'hello <1s> how are you' expandMacrosWith:(OperatingSystem getLoginName) |
1991 |
'one plus one is <1p>' expandMacrosWith:2 |
|
5964 | 1992 |
" |
1993 |
||
10721
58e6950264cb
care for bitsPerCharacter when expanding macros
Claus Gittinger <cg@exept.de>
parents:
10703
diff
changeset
|
1994 |
"Modified: / 18-09-2007 / 22:50:43 / cg" |
23009 | 1995 |
"Modified: / 24-05-2018 / 21:06:31 / Claus Gittinger" |
9192 | 1996 |
! |
1997 |
||
1998 |
isCharacters |
|
21318 | 1999 |
"true, if the receiver is a string-like thing. |
2000 |
added for visual works compatibility" |
|
2001 |
||
2002 |
"/ kept in libbasic package, because it is used in libjava and imap-implementation |
|
9192 | 2003 |
^ true |
2004 |
! ! |
|
2005 |
||
16295 | 2006 |
!CharacterArray methodsFor:'JavaScript support'! |
2007 |
||
2008 |
unquote |
|
17000 | 2009 |
"removes double quotes from the receiver. |
19698 | 2010 |
This is the JavaScript standard unquote function." |
2011 |
||
2012 |
^ self unquote:$" |
|
16295 | 2013 |
|
2014 |
" |
|
17000 | 2015 |
'hello' quote unquote |
2016 |
||
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2017 |
JavaScriptParser evaluate:'''hello''.quote.unquote' |
16295 | 2018 |
" |
19698 | 2019 |
! |
2020 |
||
2021 |
unquote:quoteCharacter |
|
2022 |
"removes quoteCharacter from either end of the receiver." |
|
2023 |
||
2024 |
|mySize| |
|
2025 |
||
2026 |
(mySize := self size) >= 2 ifTrue:[ |
|
20874 | 2027 |
((self first == quoteCharacter) and:[self last == quoteCharacter]) ifTrue:[ |
2028 |
^ self copyFrom:2 to:mySize-1 |
|
2029 |
]. |
|
19698 | 2030 |
]. |
2031 |
^ self |
|
2032 |
||
2033 |
" |
|
2034 |
'*hello*' unquote:$* |
|
2035 |
" |
|
16295 | 2036 |
! ! |
13510 | 2037 |
|
16175 | 2038 |
|
9192 | 2039 |
!CharacterArray methodsFor:'character searching'! |
2040 |
||
2041 |
includesMatchCharacters |
|
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
2042 |
"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
|
2043 |
for match operations; false if not. |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2044 |
Here, do not care for $\ escapes" |
608 | 2045 |
|
2046 |
^ self includesAny:'*#[' |
|
2519
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2047 |
|
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2048 |
" |
8874 | 2049 |
'*foo' includesMatchCharacters |
2050 |
'\*foo' includesMatchCharacters |
|
2051 |
'\*foo' includesUnescapedMatchCharacters |
|
2052 |
'*foo' includesMatchCharacters |
|
2519
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2053 |
'\\*foo' includesMatchCharacters |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2054 |
'foo*' includesMatchCharacters |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2055 |
'foo\*' includesMatchCharacters |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2056 |
'foo\' includesMatchCharacters |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2057 |
" |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2058 |
|
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2059 |
"Modified: 2.4.1997 / 18:12:34 / cg" |
9192 | 2060 |
! |
2061 |
||
2062 |
includesSeparator |
|
2063 |
"return true, if the receiver contains any whitespace characters" |
|
2064 |
||
2065 |
^ (self indexOfSeparator ~~ 0) |
|
2066 |
||
2067 |
" |
|
2068 |
'hello world' includesSeparator |
|
2069 |
'helloworld' includesSeparator |
|
2070 |
" |
|
2071 |
! |
|
2072 |
||
2073 |
includesUnescapedMatchCharacters |
|
8874 | 2074 |
"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
|
2075 |
for match operations; false if not. |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2076 |
Here, care for $\ escapes" |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2077 |
|
5342
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
2078 |
|idx sz specialChars escape| |
2519
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 |
idx := 1. |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2081 |
sz := self size. |
5342
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
2082 |
specialChars := '*#[\'. |
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
2083 |
(escape := self class matchEscapeCharacter) ~~ $\ ifTrue:[ |
20874 | 2084 |
specialChars := specialChars copy. |
2085 |
specialChars at:specialChars size put:escape |
|
5342
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
2086 |
]. |
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
2087 |
|
16245 | 2088 |
[ |
20874 | 2089 |
idx := self indexOfAny:specialChars startingAt:idx. |
2090 |
idx == 0 ifTrue:[^ false]. |
|
2091 |
(self at:idx) == escape ifFalse:[^ true]. |
|
2092 |
idx := idx + 2. |
|
2093 |
idx > sz ifTrue:[^ false]. |
|
16245 | 2094 |
] loop. |
8874 | 2095 |
|
2096 |
" |
|
2097 |
'*foo' includesUnescapedMatchCharacters |
|
2098 |
'\*foo' includesUnescapedMatchCharacters |
|
2099 |
'\\foo' includesUnescapedMatchCharacters |
|
2100 |
'\\\$foo' includesUnescapedMatchCharacters |
|
2519
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2101 |
'*foo' includesUnescapedMatchCharacters |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2102 |
'\\*foo' includesUnescapedMatchCharacters |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2103 |
'foo*' includesUnescapedMatchCharacters |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2104 |
'foo\*' includesUnescapedMatchCharacters |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2105 |
'foo\' includesUnescapedMatchCharacters |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2106 |
" |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2107 |
|
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2108 |
"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
|
2109 |
"Created: 2.4.1997 / 17:23:26 / cg" |
9192 | 2110 |
! |
2111 |
||
2112 |
indexOfControlCharacterStartingAt:startIndex |
|
3668
76271941f044
added #indexOfControlCharacterStartingAt:
Claus Gittinger <cg@exept.de>
parents:
3656
diff
changeset
|
2113 |
"return the index of the next control character; |
76271941f044
added #indexOfControlCharacterStartingAt:
Claus Gittinger <cg@exept.de>
parents:
3656
diff
changeset
|
2114 |
that is a character with asciiValue < 32. |
15605 | 2115 |
Start the search at startIndex, searching forward. |
3668
76271941f044
added #indexOfControlCharacterStartingAt:
Claus Gittinger <cg@exept.de>
parents:
3656
diff
changeset
|
2116 |
Return 0 if none is found." |
76271941f044
added #indexOfControlCharacterStartingAt:
Claus Gittinger <cg@exept.de>
parents:
3656
diff
changeset
|
2117 |
|
76271941f044
added #indexOfControlCharacterStartingAt:
Claus Gittinger <cg@exept.de>
parents:
3656
diff
changeset
|
2118 |
|start "{ Class: SmallInteger }" |
9192 | 2119 |
mySize "{ Class: SmallInteger }"| |
2120 |
||
2121 |
start := startIndex. |
|
2122 |
mySize := self size. |
|
2123 |
||
2124 |
start to:mySize do:[:index | |
|
20874 | 2125 |
(self at:index) isControlCharacter ifTrue:[^ index] |
9192 | 2126 |
]. |
2127 |
^ 0 |
|
2128 |
||
2129 |
" |
|
2130 |
'hello world' asTwoByteString indexOfControlCharacterStartingAt:1 |
|
2131 |
'hello world\foo' withCRsasTwoByteString indexOfControlCharacterStartingAt:1 |
|
2132 |
" |
|
2133 |
||
2134 |
"Modified: / 21.7.1998 / 17:25:07 / cg" |
|
2135 |
! |
|
3668
76271941f044
added #indexOfControlCharacterStartingAt:
Claus Gittinger <cg@exept.de>
parents:
3656
diff
changeset
|
2136 |
|
8541 | 2137 |
indexOfNonSeparator |
2138 |
"return the index of the first non-whitespace character. |
|
2139 |
return 0 if no non-separator was found" |
|
2140 |
||
2141 |
^ self indexOfNonSeparatorStartingAt:1. |
|
2142 |
||
2143 |
" |
|
8874 | 2144 |
' hello world' indexOfNonSeparator |
2145 |
' ' indexOfNonSeparator |
|
2146 |
'a ' indexOfNonSeparator |
|
2147 |
'abc' indexOfNonSeparator |
|
2148 |
' ' indexOfNonSeparator |
|
9192 | 2149 |
'' indexOfNonSeparator |
2150 |
" |
|
2151 |
! |
|
2152 |
||
2153 |
indexOfNonSeparatorStartingAt:startIndex |
|
4117 | 2154 |
"return the index of the next non-whitespace character, |
2155 |
starting the search at startIndex, searching forward; |
|
2156 |
return 0 if no non-separator was found" |
|
608 | 2157 |
|
2158 |
|start "{ Class: SmallInteger }" |
|
2159 |
mySize "{ Class: SmallInteger }"| |
|
2160 |
||
2161 |
start := startIndex. |
|
2162 |
mySize := self size. |
|
2163 |
||
2164 |
start to:mySize do:[:index | |
|
20874 | 2165 |
(self at:index) isSeparator ifFalse:[^ index] |
608 | 2166 |
]. |
2167 |
^ 0 |
|
2168 |
||
2169 |
" |
|
8874 | 2170 |
' hello world' indexOfNonSeparatorStartingAt:1 |
2171 |
' ' indexOfNonSeparatorStartingAt:1 |
|
2172 |
'a ' indexOfNonSeparatorStartingAt:2 |
|
4117 | 2173 |
" |
2174 |
||
608 | 2175 |
" |
2176 |
|s index1 index2| |
|
2177 |
s := ' foo bar baz'. |
|
2178 |
index1 := s indexOfNonSeparatorStartingAt:1. |
|
2179 |
index2 := s indexOfSeparatorStartingAt:index1. |
|
2180 |
s copyFrom:index1 to:index2 - 1 |
|
2181 |
" |
|
9192 | 2182 |
! |
2183 |
||
2184 |
indexOfSeparator |
|
2185 |
"return the index of the first whitespace character; |
|
2186 |
starting the search at the beginning, searching forward; |
|
2187 |
return 0 if no separator was found" |
|
2188 |
||
2189 |
^ self indexOfSeparatorStartingAt:1 |
|
2190 |
||
2191 |
" |
|
2192 |
'hello world' indexOfSeparator |
|
2193 |
'helloworld' indexOfSeparator |
|
2194 |
'hello ' indexOfSeparator |
|
2195 |
' hello' indexOfSeparator |
|
2196 |
" |
|
2197 |
! |
|
2198 |
||
15912 | 2199 |
indexOfSeparatorOrEndStartingAt:startIndex |
2200 |
"return the index of the next whitespace character, |
|
2201 |
starting the search at startIndex, searching forward; |
|
19737 | 2202 |
return the index of one beyond the end of the receiver if no separator was found. |
15912 | 2203 |
To extract the word, copy from startIndex to the returned index-1" |
2204 |
||
2205 |
|idx| |
|
2206 |
||
2207 |
idx := self indexOfSeparatorStartingAt:startIndex. |
|
2208 |
idx == 0 ifTrue:[^ self size + 1]. |
|
2209 |
^ idx. |
|
2210 |
||
2211 |
" |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2212 |
'hello world' indexOfSeparatorOrEndStartingAt:3 |
15912 | 2213 |
' hello world' indexOfSeparatorOrEndStartingAt:3 |
2214 |
'hello world ' indexOfSeparatorOrEndStartingAt:3 |
|
2215 |
'hello world ' indexOfSeparatorOrEndStartingAt:6 |
|
2216 |
'hello world ' indexOfSeparatorOrEndStartingAt:7 |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2217 |
'helloworld ' indexOfSeparatorOrEndStartingAt:7 |
15912 | 2218 |
'helloworld' indexOfSeparatorOrEndStartingAt:7 |
19737 | 2219 |
'helloworld' indexOfSeparatorStartingAt:7 |
15912 | 2220 |
" |
2221 |
! |
|
2222 |
||
9192 | 2223 |
indexOfSeparatorStartingAt:startIndex |
2224 |
"return the index of the next whitespace character, |
|
2225 |
starting the search at startIndex, searching forward; |
|
2226 |
return 0 if no separator was found" |
|
2227 |
||
2228 |
|start "{ Class: SmallInteger }" |
|
2229 |
mySize "{ Class: SmallInteger }"| |
|
2230 |
||
2231 |
start := startIndex. |
|
2232 |
mySize := self size. |
|
2233 |
||
2234 |
start to:mySize do:[:index | |
|
20874 | 2235 |
(self at:index) isSeparator ifTrue:[^ index] |
9192 | 2236 |
]. |
2237 |
^ 0 |
|
2238 |
||
2239 |
" |
|
2240 |
'hello world' indexOfSeparatorStartingAt:3 |
|
2241 |
' hello world' indexOfSeparatorStartingAt:3 |
|
8874 | 2242 |
'hello world ' indexOfSeparatorStartingAt:3 |
2243 |
'hello world ' indexOfSeparatorStartingAt:6 |
|
2244 |
'hello world ' indexOfSeparatorStartingAt:7 |
|
2245 |
'helloworld ' indexOfSeparatorStartingAt:7 |
|
2246 |
'helloworld' indexOfSeparatorStartingAt:7 |
|
4117 | 2247 |
" |
2519
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2248 |
! |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
2249 |
|
4117 | 2250 |
lastIndexOfSeparator |
14150
e13160aa321a
comment/format in: #lastIndexOfSeparator
Claus Gittinger <cg@exept.de>
parents:
14141
diff
changeset
|
2251 |
"return the last index of a whitespace character (space or tab). |
4117 | 2252 |
(i.e. start the search at the end and search backwards); |
2253 |
Returns 0 if no separator is found." |
|
2254 |
||
2255 |
^ self lastIndexOfSeparatorStartingAt:(self size) |
|
2256 |
||
2257 |
" |
|
8874 | 2258 |
'hello world' lastIndexOfSeparator |
2259 |
'helloworld' lastIndexOfSeparator |
|
2260 |
'hel lo wor ld' lastIndexOfSeparator |
|
2261 |
'hel ' lastIndexOfSeparator 6 |
|
4117 | 2262 |
" |
14150
e13160aa321a
comment/format in: #lastIndexOfSeparator
Claus Gittinger <cg@exept.de>
parents:
14141
diff
changeset
|
2263 |
|
e13160aa321a
comment/format in: #lastIndexOfSeparator
Claus Gittinger <cg@exept.de>
parents:
14141
diff
changeset
|
2264 |
"Modified (comment): / 01-06-2012 / 13:10:30 / cg" |
4117 | 2265 |
! |
2266 |
||
2267 |
lastIndexOfSeparatorStartingAt:startIndex |
|
9192 | 2268 |
"return the index of the previous whitespace character, |
2269 |
starting the search at startIndex (and searching backwards); |
|
2270 |
returns 0 if no separator was found" |
|
2271 |
||
2272 |
|start "{ Class: SmallInteger }"| |
|
2273 |
||
2274 |
start := startIndex. |
|
2275 |
||
2276 |
start to:1 by:-1 do:[:index | |
|
20874 | 2277 |
(self at:index) isSeparator ifTrue:[^ index] |
9192 | 2278 |
]. |
2279 |
^ 0 |
|
2280 |
||
2281 |
" |
|
2282 |
'hello world' lastIndexOfSeparatorStartingAt:3 |
|
2283 |
'hello world' lastIndexOfSeparatorStartingAt:7 |
|
2284 |
'helloworld' lastIndexOfSeparatorStartingAt:7 |
|
8874 | 2285 |
' helloworld' lastIndexOfSeparatorStartingAt:7 |
4117 | 2286 |
" |
608 | 2287 |
! ! |
2288 |
||
2289 |
!CharacterArray methodsFor:'comparing'! |
|
2290 |
||
6742
292775fda600
avoid endless recursion when comparing int < string
Claus Gittinger <cg@exept.de>
parents:
6655
diff
changeset
|
2291 |
< aString |
608 | 2292 |
"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
|
2293 |
receiver is greater than the argument. Otherwise return false. |
8874 | 2294 |
This comparison is based on the elements ascii code - |
9192 | 2295 |
i.e. upper/lowercase & upper/lowercase & national characters are NOT treated specially." |
2296 |
||
2297 |
|mySize "{ Class: SmallInteger }" |
|
2298 |
otherSize "{ Class: SmallInteger }" |
|
2299 |
n "{ Class: SmallInteger }" |
|
2300 |
c1 c2| |
|
2301 |
||
2302 |
mySize := self size. |
|
2303 |
otherSize := aString string size. |
|
6742
292775fda600
avoid endless recursion when comparing int < string
Claus Gittinger <cg@exept.de>
parents:
6655
diff
changeset
|
2304 |
n := mySize min:otherSize. |
292775fda600
avoid endless recursion when comparing int < string
Claus Gittinger <cg@exept.de>
parents:
6655
diff
changeset
|
2305 |
|
292775fda600
avoid endless recursion when comparing int < string
Claus Gittinger <cg@exept.de>
parents:
6655
diff
changeset
|
2306 |
1 to:n do:[:index | |
20874 | 2307 |
c1 := self at:index. |
2308 |
c2 := aString at:index. |
|
2309 |
(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
|
2310 |
]. |
292775fda600
avoid endless recursion when comparing int < string
Claus Gittinger <cg@exept.de>
parents:
6655
diff
changeset
|
2311 |
^ mySize < otherSize |
608 | 2312 |
! |
2313 |
||
2314 |
= aString |
|
2315 |
"Compare the receiver with the argument and return true if the |
|
2316 |
receiver is equal to the argument. Otherwise return false. |
|
1252 | 2317 |
|
9191 | 2318 |
This compare does NOT ignore case differences, |
608 | 2319 |
therefore 'foo' = 'Foo' will return false. |
2320 |
Since this is incompatible to ST-80 (at least, V2.x) , this may change." |
|
2321 |
||
14137
1eb1b4a3f364
Inherit from UninterpretedBytes instead of ByteArray
Stefan Vogel <sv@exept.de>
parents:
14122
diff
changeset
|
2322 |
|mySize "{ Class: SmallInteger }"| |
608 | 2323 |
|
5963
23462e06dcff
compare with Texts fixed (should compare equal if strings are)
Claus Gittinger <cg@exept.de>
parents:
5946
diff
changeset
|
2324 |
(aString isString or:[aString species == self species]) ifFalse:[ |
20874 | 2325 |
^ false |
5963
23462e06dcff
compare with Texts fixed (should compare equal if strings are)
Claus Gittinger <cg@exept.de>
parents:
5946
diff
changeset
|
2326 |
]. |
608 | 2327 |
mySize := self size. |
14137
1eb1b4a3f364
Inherit from UninterpretedBytes instead of ByteArray
Stefan Vogel <sv@exept.de>
parents:
14122
diff
changeset
|
2328 |
mySize ~~ (aString size) ifTrue:[^ false]. |
608 | 2329 |
|
2330 |
1 to:mySize do:[:index | |
|
20874 | 2331 |
(self at:index) = (aString at:index) ifFalse:[^ false]. |
608 | 2332 |
]. |
2333 |
^ true |
|
2334 |
||
2335 |
" |
|
8874 | 2336 |
'foo' = 'Foo' |
2337 |
'foo' = 'bar' |
|
2338 |
'foo' = 'foo' |
|
2339 |
'foo' = 'foo' asText |
|
2340 |
'foo' asText = 'foo' |
|
2341 |
'foo' asText = 'foo' asText |
|
608 | 2342 |
" |
1252 | 2343 |
|
2344 |
"Modified: 22.4.1996 / 15:53:58 / cg" |
|
608 | 2345 |
! |
9192 | 2346 |
|
9191 | 2347 |
> aString |
608 | 2348 |
"Compare the receiver with the argument and return true if the |
2349 |
receiver is greater than the argument. Otherwise return false. |
|
8874 | 2350 |
This comparison is based on the elements ascii code - |
1252 | 2351 |
i.e. upper/lowercase & upper/lowercase & national characters are NOT treated specially." |
608 | 2352 |
|
2353 |
|mySize "{ Class: SmallInteger }" |
|
8874 | 2354 |
otherSize "{ Class: SmallInteger }" |
9192 | 2355 |
n "{ Class: SmallInteger }" |
2356 |
c1 c2| |
|
2357 |
||
2358 |
mySize := self size. |
|
5929 | 2359 |
otherSize := aString string size. |
2360 |
n := mySize min:otherSize. |
|
2361 |
||
2362 |
1 to:n do:[:index | |
|
20874 | 2363 |
c1 := self at:index. |
2364 |
c2 := aString at:index. |
|
2365 |
(c1 == c2 or:[c1 = c2]) ifFalse:[^ c1 > c2]. |
|
5929 | 2366 |
]. |
9192 | 2367 |
^ mySize > otherSize |
2368 |
||
2369 |
"Modified: 22.4.1996 / 15:55:00 / cg" |
|
2370 |
! |
|
2371 |
||
14660 | 2372 |
after:aString |
23155 | 2373 |
<resource: #obsolete> |
14660 | 2374 |
"Compare the receiver with the argument and return true if the |
2375 |
receiver should come after the argument in a sorted list. |
|
2376 |
Otherwise return false. |
|
2377 |
NOTE: The comparison should be language specific, depending on the value of |
|
20874 | 2378 |
LC_COLLATE, which is initialized from the environment. |
2379 |
||
2380 |
Currently it is for Strings, but not for UnicodeStrings... |
|
14660 | 2381 |
|
2382 |
STUPID: |
|
23155 | 2383 |
#after has a completely different meaning in SeqColl... |
2384 |
... therefore it is marked as obsolete. |
|
2385 |
" |
|
14660 | 2386 |
|
2387 |
^ (self compareCollatingWith:aString) > 0 |
|
23155 | 2388 |
|
2389 |
"Modified (comment): / 29-06-2018 / 11:41:33 / Claus Gittinger" |
|
14660 | 2390 |
! |
2391 |
||
15814 | 2392 |
compareAsVersionNumberWith:aStringOrCollection |
2393 |
"Compare the receiver with the argument and return 1 if the receiver is |
|
2394 |
greater, 0 if equal and -1 if less than the argument in a sorted list. |
|
2395 |
Compare as version numbers in the form a.b.c... ." |
|
2396 |
||
23121 | 2397 |
|
2398 |
^ self asVersionNumberCollection |
|
2399 |
compareWith:aStringOrCollection asVersionNumberCollection |
|
15814 | 2400 |
|
2401 |
" |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2402 |
self assert:('1' compareAsVersionNumberWith:'2') < 0. |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2403 |
self assert:('2' compareAsVersionNumberWith:'1') > 0. |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2404 |
self assert:('1.1' compareAsVersionNumberWith:'2.1.2') < 0. |
23121 | 2405 |
self assert:('1.1a' compareAsVersionNumberWith:'2.1.2') < 0. |
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2406 |
self assert:('2.1' compareAsVersionNumberWith:'1.2.3') > 0. |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2407 |
self assert:('1' compareAsVersionNumberWith:'1.1') < 0. |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2408 |
self assert:('1.1' compareAsVersionNumberWith:'1') > 0. |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2409 |
self assert:('1.1' compareAsVersionNumberWith:'1.2') < 0. |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2410 |
self assert:('1.10' compareAsVersionNumberWith:'1.2') > 0. |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2411 |
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
|
2412 |
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
|
2413 |
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
|
2414 |
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
|
2415 |
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
|
2416 |
self assert:('1.2.3.4' compareAsVersionNumberWith:#(1 2 3 4)) = 0. |
22325 | 2417 |
self assert:('1.2.3.4' compareAsVersionNumberWith:#('1' 2 3 4)) = 0. |
23121 | 2418 |
|
2419 |
self assert:('1.1' compareAsVersionNumberWith:'1.1a') < 0. |
|
2420 |
||
22325 | 2421 |
" |
2422 |
||
2423 |
"Modified: / 25-10-2017 / 11:25:08 / stefan" |
|
23121 | 2424 |
"Modified (format): / 20-06-2018 / 19:23:18 / Stefan Vogel" |
15814 | 2425 |
! |
2426 |
||
9192 | 2427 |
compareCaselessWith:aString |
22327 | 2428 |
"Compare the receiver against the argument, ignoring case. |
9190 | 2429 |
Return 1 if the receiver is greater, 0 if equal and -1 if less than the argument. |
2430 |
||
8874 | 2431 |
This comparison is based on the elements ascii code - |
9190 | 2432 |
i.e. national characters are NOT treated specially. |
2433 |
'foo' compareWith: 'Foo' will return 0" |
|
608 | 2434 |
|
2435 |
|mySize "{ Class: SmallInteger }" |
|
8874 | 2436 |
otherSize "{ Class: SmallInteger }" |
9192 | 2437 |
n "{ Class: SmallInteger }" |
608 | 2438 |
c1 c2| |
2439 |
||
2440 |
mySize := self size. |
|
5739
4fd3be7ccc75
oops some relational ops did accept a non-string arg
Claus Gittinger <cg@exept.de>
parents:
5732
diff
changeset
|
2441 |
otherSize := aString string size. |
608 | 2442 |
n := mySize min:otherSize. |
2443 |
||
2444 |
1 to:n do:[:index | |
|
20874 | 2445 |
c1 := (self at:index) asLowercase. |
2446 |
c2 := (aString at:index) asLowercase. |
|
2447 |
c1 > c2 ifTrue:[^ 1]. |
|
2448 |
c1 < c2 ifTrue:[^ -1]. |
|
608 | 2449 |
]. |
2450 |
mySize > otherSize ifTrue:[^ 1]. |
|
2451 |
mySize < otherSize ifTrue:[^ -1]. |
|
2452 |
^ 0 |
|
1252 | 2453 |
|
22327 | 2454 |
"Modified: / 22-04-1996 / 15:56:07 / cg" |
2455 |
"Modified (comment): / 26-10-2017 / 16:01:01 / mawalch" |
|
608 | 2456 |
! |
2457 |
||
14660 | 2458 |
compareCollatingWith:aString |
2459 |
"Compare the receiver with the argument and return 1 if the receiver is |
|
2460 |
greater, 0 if equal and -1 if less than the argument in a sorted list. |
|
2461 |
The comparison is language specific, depending on the value of |
|
2462 |
LC_COLLATE, which is in the shell environment." |
|
2463 |
||
2464 |
"TODO not yet defined for unicode" |
|
2465 |
<resource: #todo> |
|
2466 |
||
2467 |
|s| |
|
2468 |
||
2469 |
(s := self string) ~~ self ifTrue:[ |
|
20874 | 2470 |
^ s compareCollatingWith:aString |
14660 | 2471 |
]. |
2472 |
^ self compareWith:aString |
|
2473 |
! |
|
2474 |
||
9192 | 2475 |
compareWith:aString |
9190 | 2476 |
"Compare the receiver with the argument and return 1 if the receiver is |
2477 |
greater, 0 if equal and -1 if less than the argument. |
|
11861 | 2478 |
This comparison is based on the elements' codepoints - |
9190 | 2479 |
i.e. upper/lowercase & national characters are NOT treated specially. |
2480 |
'foo' compareWith: 'Foo' will return 1. |
|
2481 |
while 'foo' sameAs:'Foo' will return true" |
|
2482 |
||
15814 | 2483 |
|s| |
2484 |
||
2485 |
s := self string. |
|
2486 |
s ~~ self ifTrue:[ |
|
20874 | 2487 |
^ s compareWith:aString string. |
9192 | 2488 |
]. |
15814 | 2489 |
^ super compareWith:aString string. |
9192 | 2490 |
! |
2491 |
||
11861 | 2492 |
endsWith:aStringOrCharacter |
16175 | 2493 |
"return true, if the receiver ends with something, aStringOrCharacter. |
2494 |
If aStringOrCharacter is empty, true is returned" |
|
11861 | 2495 |
|
2496 |
|s| |
|
2497 |
||
2498 |
(s := self string) ~~ self ifTrue:[ |
|
20874 | 2499 |
^ s endsWith:aStringOrCharacter |
11861 | 2500 |
]. |
21083 | 2501 |
(self size ~~ 0 and:[aStringOrCharacter isCharacter]) ifTrue:[ |
20874 | 2502 |
^ self last = aStringOrCharacter |
11861 | 2503 |
]. |
2504 |
^ super endsWith:aStringOrCharacter |
|
2505 |
||
2506 |
" |
|
2507 |
'hello world' endsWith:'world' |
|
2508 |
'hello world' asText allBold endsWith:'world' |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2509 |
'hello world' endsWith:'' |
16175 | 2510 |
'hello world' asText allBold endsWith:'' |
11861 | 2511 |
" |
2512 |
||
2513 |
"Modified: 12.5.1996 / 15:49:18 / cg" |
|
2514 |
! |
|
2515 |
||
23154 | 2516 |
endsWith:aStringOrCharacter caseSensitive:caseSensitive |
2517 |
"return true, if the receiver ends with something, aStringOrCharacter. |
|
2518 |
If aStringOrCharacter is empty, true is returned" |
|
2519 |
||
2520 |
|s| |
|
2521 |
||
2522 |
caseSensitive ifTrue:[^ self endsWith:aStringOrCharacter]. |
|
2523 |
||
2524 |
(s := self string) ~~ self ifTrue:[ |
|
2525 |
^ s endsWith:aStringOrCharacter |
|
2526 |
]. |
|
2527 |
(self size ~~ 0 and:[aStringOrCharacter isCharacter]) ifTrue:[ |
|
2528 |
^ self last asLowercase = aStringOrCharacter asLowercase |
|
2529 |
]. |
|
2530 |
^ self endsWith:aStringOrCharacter using:[:a :b | a asLowercase = b asLowercase]. |
|
2531 |
||
2532 |
" |
|
2533 |
'hello World' endsWith:'world' caseSensitive:true |
|
2534 |
'hello World' endsWith:'world' caseSensitive:false |
|
2535 |
" |
|
2536 |
||
2537 |
"Created: / 29-06-2018 / 11:38:57 / Claus Gittinger" |
|
2538 |
! |
|
2539 |
||
11838 | 2540 |
hammingDistanceTo:aString |
2541 |
"return the hamming distance (the number of characters which are different). |
|
12177 | 2542 |
In information theory, the Hamming distance between two strings of equal length |
2543 |
is the number of positions for which the corresponding symbols are different. |
|
2544 |
Put another way, it measures the minimum number of substitutions required to change |
|
11838 | 2545 |
one into the other, or the number of errors that transformed one string into the other." |
2546 |
||
22377 | 2547 |
|mySize| |
2548 |
||
2549 |
mySize := self size. |
|
2550 |
self assert:(aString size == mySize). |
|
2551 |
^ 1 to:mySize count:[:idx | (self at:idx) ~= (aString at:idx)] |
|
11838 | 2552 |
|
2553 |
" |
|
2554 |
'roses' hammingDistanceTo:'toned' |
|
22377 | 2555 |
'roses' hammingDistanceTo:'doses' |
2556 |
" |
|
2557 |
||
2558 |
"Modified (comment): / 24-11-2017 / 08:50:58 / cg" |
|
11838 | 2559 |
! |
2560 |
||
9192 | 2561 |
hash |
2562 |
"return an integer useful as a hash-key" |
|
2563 |
||
16898 | 2564 |
"/ whenever changing, also care for String>>hash. |
13879 | 2565 |
"/ immediately after any change, execute (maybe in a debugger): |
2566 |
"/ Set allSubInstancesDo:[:s | s rehash] |
|
16898 | 2567 |
^ self hash_fnv1a |
2568 |
||
2569 |
" |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2570 |
'a' hash |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2571 |
'a' asUnicode16String hash |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2572 |
'a' asUnicode32String hash |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2573 |
'aa' hash |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2574 |
'aa' asUnicode16String hash |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2575 |
'aa' asUnicode32String hash |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2576 |
'ab' hash |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2577 |
'ab' asUnicode16String hash |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2578 |
'ab' asUnicode32String hash |
13879 | 2579 |
'ab' hash |
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2580 |
'ab' asArray hash |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2581 |
" |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2582 |
|
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2583 |
" |
20874 | 2584 |
|syms ms| |
2585 |
||
2586 |
syms := Symbol allInstances. |
|
2587 |
Transcript show:'syms: '; showCR:syms size. |
|
2588 |
Transcript show:'sdbm hashes: '; showCR:(syms collect:[:s| s hash]) asSet size. |
|
2589 |
Transcript show:'dragonBook hashes: '; showCR:(syms collect:[:s| s hash_dragonBook]) asSet size. |
|
2590 |
||
2591 |
ms := Time millisecondsToRun:[ |
|
2592 |
10 timesRepeat:[ |
|
2593 |
syms do:[:each| each hash]. |
|
2594 |
]. |
|
2595 |
]. |
|
2596 |
Transcript show:'sdbm hash: '; showCR:ms. |
|
2597 |
||
2598 |
ms := Time millisecondsToRun:[ |
|
2599 |
10 timesRepeat:[ |
|
2600 |
syms do:[:each| each hash_dragonBook]. |
|
2601 |
]. |
|
2602 |
]. |
|
2603 |
Transcript show:'dragonBook: '; showCR:ms. |
|
2604 |
||
2605 |
syms := syms collect:[:each| each asUnicode16String]. |
|
2606 |
ms := Time millisecondsToRun:[ |
|
2607 |
10 timesRepeat:[ |
|
2608 |
syms do:[:each| each hash]. |
|
2609 |
]. |
|
2610 |
]. |
|
2611 |
Transcript show:'unicode sdbm hash: '; showCR:ms. |
|
2612 |
||
2613 |
ms := Time millisecondsToRun:[ |
|
2614 |
10 timesRepeat:[ |
|
2615 |
syms do:[:each| each hash_dragonBook]. |
|
2616 |
]. |
|
2617 |
]. |
|
2618 |
Transcript show:'unicode dragonBook:'; showCR:ms. |
|
14641 | 2619 |
" |
2620 |
||
13879 | 2621 |
"Modified: / 26-12-2011 / 14:09:07 / cg" |
2622 |
! |
|
2623 |
||
2624 |
hash_dragonBook |
|
2625 |
"return an integer useful as a hash-key" |
|
2626 |
||
10698
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2627 |
|h g| |
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2628 |
|
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2629 |
"/ |
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2630 |
"/ this is the dragon-book algorithm |
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2631 |
"/ |
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2632 |
h := 0. |
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2633 |
self reverseDo:[:char | |
10703
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
2634 |
"/ 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
|
2635 |
"/ h := (h bitShift:4) + char asciiValue. |
20874 | 2636 |
h := (h bitShift:4). |
2637 |
h := h + char codePoint. |
|
2638 |
h := h bitAnd:16rFFFFFFFF. |
|
2639 |
g := (h bitAnd: 16rF0000000). |
|
2640 |
g ~~ 0 ifTrue:[ |
|
2641 |
h := h bitXor:(g bitShift:-24). |
|
2642 |
h := h bitXor:g. |
|
2643 |
]. |
|
10698
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2644 |
]. |
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2645 |
"/ |
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2646 |
"/ multiply by large prime to spread values |
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2647 |
"/ This speeds up Set and Dictionary by a factor of 10!! |
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2648 |
"/ |
14137
1eb1b4a3f364
Inherit from UninterpretedBytes instead of ByteArray
Stefan Vogel <sv@exept.de>
parents:
14122
diff
changeset
|
2649 |
h := h times:31415821. |
10698
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2650 |
^ h |
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2651 |
|
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2652 |
" |
10703
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
2653 |
'a' hash |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
2654 |
'a' asUnicode16String hash |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
2655 |
'aa' hash |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
2656 |
'aa' asUnicode16String hash |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
2657 |
'ab' hash |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
2658 |
'ab' asUnicode16String hash |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
2659 |
'ab' hash |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
2660 |
'ab' asArray hash |
10698
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2661 |
" |
13879 | 2662 |
|
2663 |
"Created: / 26-12-2011 / 13:46:06 / cg" |
|
2664 |
! |
|
2665 |
||
16887 | 2666 |
hash_fnv1a |
2667 |
"return an integer useful as a hash-key. |
|
2668 |
This method uses the fnv-1a algorithm |
|
19743 | 2669 |
(which is actually a very good one). |
2670 |
Attention: stops when a 0-codepoint is encountered |
|
20874 | 2671 |
(for compatibility with the hash used by the VM) |
19743 | 2672 |
Also: on 64bit CPUs, only small 4-byte hashvalues are returned, |
20874 | 2673 |
(so hash values are independent from the architecture)" |
19743 | 2674 |
|
2675 |
|h byte| |
|
16887 | 2676 |
|
2677 |
h := 2166136261. |
|
2678 |
self do:[:eachChar | |
|
20874 | 2679 |
byte := eachChar codePoint. |
2680 |
byte == 0 ifTrue:[ |
|
2681 |
"/ stop |
|
2682 |
^ (h bitXor: (h >> 30)) bitAnd: 16r3FFFFFFF. |
|
2683 |
]. |
|
2684 |
h := h bitXor:byte. |
|
2685 |
h := (h * 16777619) bitAnd:16rFFFFFFFF. |
|
16887 | 2686 |
]. |
16898 | 2687 |
"/ make sure, it fits into a smallInt |
2688 |
h := (h bitXor: (h >> 30)) bitAnd: 16r3FFFFFFF. |
|
16887 | 2689 |
^ h |
2690 |
||
2691 |
" |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2692 |
'abc' hash_fnv1a |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2693 |
'abc' asUnicode16String hash_fnv1a |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2694 |
'abc' asUnicode32String hash_fnv1a |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2695 |
|
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2696 |
'foofooHelloWorld' hash_fnv1a |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2697 |
'foofooHelloWorld' asUnicode16String hash_fnv1a |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2698 |
'foofooHelloWorld' asUnicode32String hash_fnv1a |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2699 |
|
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2700 |
'blablaHelloWorld' hash_fnv1a |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2701 |
'blablaHelloWorld' asUnicode16String hash_fnv1a |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2702 |
'blablaHelloWorld' asUnicode32String hash_fnv1a |
16887 | 2703 |
" |
2704 |
! |
|
2705 |
||
2706 |
hash_java |
|
2707 |
"return an integer useful as a hash-key. |
|
2708 |
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
|
2709 |
the java virtual machine |
16887 | 2710 |
(which is actually not a very good one)." |
2711 |
||
2712 |
|h| |
|
2713 |
||
2714 |
h := 0. |
|
2715 |
self do:[:eachChar | |
|
20874 | 2716 |
h := (h * 31) + (eachChar codePoint). |
2717 |
h := h bitAnd:16rFFFFFFFF. |
|
16887 | 2718 |
]. |
2719 |
^ h |
|
2720 |
||
2721 |
" |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2722 |
'abc' hash_java |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2723 |
'foofooHelloWorld' hash_java |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2724 |
'blablaHelloWorld' hash_java |
16887 | 2725 |
" |
2726 |
! |
|
2727 |
||
16898 | 2728 |
hash_sdbm |
2729 |
"return an integer useful as a hash-key. |
|
2730 |
This method implements the sdbm algorithm." |
|
2731 |
||
2732 |
|h| |
|
2733 |
||
2734 |
"/ |
|
2735 |
"/ this is the sdbm algorithm |
|
2736 |
"/ |
|
2737 |
h := 0. |
|
2738 |
self do:[:char | |
|
20874 | 2739 |
h := (65599 times:h) plus:char codePoint. |
16898 | 2740 |
]. |
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
2741 |
^ h |
16898 | 2742 |
|
2743 |
" |
|
2744 |
'a' hash |
|
2745 |
'a' asUnicode16String hash |
|
2746 |
'aa' hash |
|
2747 |
'aa' asUnicode16String hash |
|
2748 |
'ab' hash |
|
2749 |
'ab' asUnicode16String hash |
|
2750 |
'ab' hash |
|
2751 |
'ab' asArray hash |
|
2752 |
" |
|
2753 |
||
2754 |
" |
|
20874 | 2755 |
|syms ms| |
2756 |
||
2757 |
syms := Symbol allInstances. |
|
2758 |
Transcript show:'syms: '; showCR:syms size. |
|
2759 |
Transcript show:'sdbm hashes: '; showCR:(syms collect:[:s| s hash]) asSet size. |
|
2760 |
Transcript show:'dragonBook hashes: '; showCR:(syms collect:[:s| s hash_dragonBook]) asSet size. |
|
2761 |
||
2762 |
ms := Time millisecondsToRun:[ |
|
2763 |
10 timesRepeat:[ |
|
2764 |
syms do:[:each| each hash]. |
|
2765 |
]. |
|
2766 |
]. |
|
2767 |
Transcript show:'sdbm hash: '; showCR:ms. |
|
2768 |
||
2769 |
ms := Time millisecondsToRun:[ |
|
2770 |
10 timesRepeat:[ |
|
2771 |
syms do:[:each| each hash_dragonBook]. |
|
2772 |
]. |
|
2773 |
]. |
|
2774 |
Transcript show:'dragonBook: '; showCR:ms. |
|
2775 |
||
2776 |
syms := syms collect:[:each| each asUnicode16String]. |
|
2777 |
ms := Time millisecondsToRun:[ |
|
2778 |
10 timesRepeat:[ |
|
2779 |
syms do:[:each| each hash]. |
|
2780 |
]. |
|
2781 |
]. |
|
2782 |
Transcript show:'unicode sdbm hash: '; showCR:ms. |
|
2783 |
||
2784 |
ms := Time millisecondsToRun:[ |
|
2785 |
10 timesRepeat:[ |
|
2786 |
syms do:[:each| each hash_dragonBook]. |
|
2787 |
]. |
|
2788 |
]. |
|
2789 |
Transcript show:'unicode dragonBook:'; showCR:ms. |
|
16898 | 2790 |
" |
2791 |
||
2792 |
"Modified: / 26-12-2011 / 14:09:07 / cg" |
|
2793 |
! |
|
2794 |
||
11861 | 2795 |
levenshteinTo:aString |
2796 |
"return the levenshtein distance to the argument, aString; |
|
2797 |
this value corresponds to the number of replacements that have to be |
|
2798 |
made to get aString from the receiver. |
|
2799 |
See IEEE transactions on Computers 1976 Pg 172 ff." |
|
2800 |
||
2801 |
" |
|
21760 | 2802 |
in the following, we assume that omiting a character |
11861 | 2803 |
is less of an error than inserting an extra character. |
2804 |
Therefore the different insertion (i) and deletion (d) values. |
|
20874 | 2805 |
s: substitution weight (4) |
2806 |
k: keyboard weight (k) (typing a nearby key) - or nil (then use s) |
|
2807 |
c: case weight (4) - or nil (then use s) |
|
2808 |
e: exchange weight (8) - or nil (then use s*2) |
|
2809 |
i: insertion of extra character weight (2) |
|
2810 |
d: delete of a character weight (6) |
|
22157 | 2811 |
|
2812 |
Notice that the standard levenshtein uses the same weight for insertion and deletion, |
|
2813 |
and is computed by: |
|
2814 |
'flaw' levenshteinTo:'lawn' s:2 k:nil c:nil i:1 d:1 |
|
2815 |
||
11861 | 2816 |
" |
2817 |
||
2818 |
^ StringUtilities |
|
20874 | 2819 |
levenshteinDistanceFrom:self |
2820 |
to:aString |
|
2821 |
s:4 k:4 c:4 e:nil i:2 d:6 |
|
12177 | 2822 |
|
2823 |
" |
|
2824 |
'computer' levenshteinTo:'computer' |
|
11861 | 2825 |
'cOmputer' levenshteinTo:'computer' |
12177 | 2826 |
'cOmpuTer' levenshteinTo:'computer' |
11861 | 2827 |
'cimputer' levenshteinTo:'computer' |
2828 |
'cumputer' levenshteinTo:'computer' |
|
2829 |
||
2830 |
'cmputer' levenshteinTo:'computer' |
|
2831 |
'coomputer' levenshteinTo:'computer' |
|
2832 |
||
2833 |
'ocmprt' levenshteinTo:'computer' |
|
2834 |
'computer' levenshteinTo:'computer' |
|
2835 |
'ocmputer' levenshteinTo:'computer' |
|
2836 |
'cmputer' levenshteinTo:'computer' |
|
2837 |
'computer' levenshteinTo:'cmputer' |
|
2838 |
'Computer' levenshteinTo:'computer' |
|
2839 |
||
12177 | 2840 |
'compiter' levenshteinTo:'computer' |
2841 |
'compoter' levenshteinTo:'computer' |
|
2842 |
||
2843 |
'comptuer' levenshteinTo:'computer' |
|
11861 | 2844 |
" |
21760 | 2845 |
|
2846 |
"Modified (comment): / 17-05-2017 / 16:15:41 / mawalch" |
|
22157 | 2847 |
"Modified (comment): / 02-08-2017 / 17:13:04 / cg" |
11861 | 2848 |
! |
2849 |
||
2850 |
levenshteinTo:aString s:substWeight k:kbdTypoWeight c:caseWeight i:insrtWeight d:deleteWeight |
|
2851 |
"parametrized levenshtein. |
|
2852 |
return the levenshtein distance to the argument, aString; |
|
2853 |
this value corrensponds to the number of replacements that have to be |
|
2854 |
made to get aString from the receiver. |
|
2855 |
The arguments are the costs for |
|
20874 | 2856 |
s:substitution, |
2857 |
k:keyboard type (substitution), |
|
2858 |
c:case-change, |
|
2859 |
i:insertion |
|
2860 |
d:deletion |
|
11861 | 2861 |
of a character. |
2862 |
See IEEE transactions on Computers 1976 Pg 172 ff" |
|
2863 |
||
2864 |
^ StringUtilities |
|
20874 | 2865 |
levenshteinDistanceFrom:self |
2866 |
to:aString |
|
2867 |
s:substWeight k:kbdTypoWeight c:caseWeight e:nil i:insrtWeight d:deleteWeight |
|
11861 | 2868 |
! |
2869 |
||
9190 | 2870 |
sameAs:aString |
2871 |
"Compare the receiver with the argument like =, but ignore case differences. |
|
2872 |
Return true or false." |
|
2873 |
||
2874 |
|mySize "{ Class: SmallInteger }" |
|
2875 |
otherSize c1 c2| |
|
2876 |
||
9192 | 2877 |
self == aString ifTrue:[^ true]. |
2878 |
||
2879 |
mySize := self size. |
|
2880 |
otherSize := aString string size. |
|
18884 | 2881 |
mySize ~~ otherSize ifTrue:[^ false]. |
9192 | 2882 |
|
2883 |
1 to:mySize do:[:index | |
|
20874 | 2884 |
c1 := self at:index. |
2885 |
c2 := aString at:index. |
|
2886 |
c1 ~~ c2 ifTrue:[ |
|
2887 |
(c1 sameAs:c2) ifFalse:[^ false]. |
|
2888 |
] |
|
9192 | 2889 |
]. |
2890 |
^ true |
|
2891 |
||
608 | 2892 |
" |
8874 | 2893 |
'foo' sameAs: 'Foo' |
2894 |
'foo' sameAs: 'bar' |
|
2895 |
'foo' sameAs: 'foo' |
|
9192 | 2896 |
" |
2897 |
||
2898 |
"Modified: 22.4.1996 / 15:56:17 / cg" |
|
2899 |
! |
|
608 | 2900 |
|
15734 | 2901 |
sameAs:aString caseSensitive:caseSensitive |
2902 |
"Compare the receiver with the argument. |
|
2903 |
If caseSensitive is false, this is the same as #sameAs:, |
|
2904 |
if false, this is the same as #=." |
|
2905 |
||
2906 |
caseSensitive ifFalse:[ |
|
20874 | 2907 |
^ self sameAs:aString |
15734 | 2908 |
]. |
2909 |
^ self = aString |
|
2910 |
||
2911 |
" |
|
2912 |
'foo' sameAs:'Foo' caseSensitive:false |
|
2913 |
'foo' sameAs:'foo' caseSensitive:true |
|
2914 |
" |
|
2915 |
! |
|
2916 |
||
5242 | 2917 |
sameAs:aString ignoreCase:ignoreCase |
15734 | 2918 |
<resource: #obsolete> |
5242 | 2919 |
"Compare the receiver with the argument. |
2920 |
If ignoreCase is true, this is the same as #sameAs:, |
|
2921 |
if false, this is the same as #=." |
|
2922 |
||
2923 |
ignoreCase ifTrue:[ |
|
20874 | 2924 |
^ self sameAs:aString |
5242 | 2925 |
]. |
2926 |
^ self = aString |
|
2927 |
||
2928 |
" |
|
8874 | 2929 |
'foo' sameAs:'Foo' ignoreCase:false |
2930 |
'foo' sameAs:'foo' ignoreCase:true |
|
5242 | 2931 |
" |
2932 |
||
2933 |
! |
|
2934 |
||
608 | 2935 |
sameCharacters:aString |
9192 | 2936 |
"count & return the number of characters which are the same |
2937 |
(ignoring case and emphasis) in the receiver and the argument, aString." |
|
2938 |
||
2939 |
|n "{ Class: SmallInteger }" |
|
2940 |
c1 c2 cnt| |
|
2941 |
||
2942 |
n := self size. |
|
2943 |
n := n min:(aString string size). |
|
2944 |
||
2945 |
cnt := 0. |
|
2946 |
1 to:n do:[:index | |
|
20874 | 2947 |
c1 := self at:index. |
2948 |
c2 := aString at:index. |
|
2949 |
((c1 == c2) |
|
2950 |
or:[c1 asLowercase = c2 asLowercase]) ifTrue:[ |
|
2951 |
cnt := cnt + 1 |
|
2952 |
] |
|
9192 | 2953 |
]. |
2954 |
^ cnt |
|
2955 |
||
2956 |
" |
|
2957 |
'foobarbaz' sameCharacters: 'foo' |
|
8874 | 2958 |
'foobarbaz' sameCharacters: 'Foo' |
2959 |
'foobarbaz' sameCharacters: 'baz' |
|
608 | 2960 |
" |
5510
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
2961 |
! |
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
2962 |
|
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
2963 |
sameEmphasisAs:aStringOrText |
17215 | 2964 |
"compare the receiver's and the argument's emphasis" |
5510
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
2965 |
|
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
2966 |
^ self emphasis = aStringOrText emphasis |
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
2967 |
|
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
2968 |
" |
8874 | 2969 |
'hello' asText sameEmphasisAs: 'hello' |
2970 |
'hello' asText sameEmphasisAs: 'hello' asText |
|
2971 |
'hello' asText allBold sameEmphasisAs: 'hello' |
|
2972 |
'hello' asText allBold sameEmphasisAs: 'fooba' asText allBold |
|
9192 | 2973 |
'hello' asText allBold sameEmphasisAs: 'fooba' asText allItalic |
2974 |
" |
|
2975 |
! |
|
2976 |
||
9191 | 2977 |
sameStringAndEmphasisAs:aStringOrText |
5739
4fd3be7ccc75
oops some relational ops did accept a non-string arg
Claus Gittinger <cg@exept.de>
parents:
5732
diff
changeset
|
2978 |
"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
|
2979 |
|
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
2980 |
aStringOrText isString ifFalse:[^ false]. |
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
2981 |
(self string = aStringOrText string) ifFalse:[^ false]. |
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
2982 |
self hasChangeOfEmphasis = aStringOrText hasChangeOfEmphasis ifFalse:[^ false]. |
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
2983 |
^ self emphasis = aStringOrText emphasis |
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
2984 |
|
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
2985 |
" |
8874 | 2986 |
'hello' asText sameEmphasisAs: 'hello' |
2987 |
'hello' asText sameEmphasisAs: 'hello' asText |
|
2988 |
'hello' asText allBold sameEmphasisAs: 'hello' |
|
2989 |
'hello' asText allBold sameEmphasisAs: 'fooba' asText allBold |
|
2990 |
'hello' asText allBold sameEmphasisAs: 'fooba' asText allItalic |
|
2991 |
||
2992 |
'hello' sameEmphasisAs: 'hello' asText |
|
2993 |
'hello' sameEmphasisAs: 'hello' asText allBold |
|
2994 |
'hello' sameEmphasisAs: 'fooba' |
|
2995 |
'hello' sameEmphasisAs: 'fooba' asText |
|
2996 |
'hello' sameEmphasisAs: 'fooba' asText allBold |
|
2997 |
'hello' sameEmphasisAs: 'fooba' asText allItalic |
|
2998 |
||
2999 |
'hello' asText sameStringAndEmphasisAs: 'hello' |
|
3000 |
'hello' asText sameStringAndEmphasisAs: 'hello' asText |
|
3001 |
'hello' asText allBold sameStringAndEmphasisAs: 'hello' |
|
3002 |
'hello' asText allBold sameStringAndEmphasisAs: 'fooba' asText allBold |
|
3003 |
'hello' asText allBold sameStringAndEmphasisAs: 'fooba' asText allItalic |
|
3004 |
||
3005 |
'hello' sameStringAndEmphasisAs: 'hello' asText |
|
3006 |
'hello' sameStringAndEmphasisAs: 'hello' asText allBold |
|
3007 |
'hello' sameStringAndEmphasisAs: 'fooba' |
|
3008 |
'hello' sameStringAndEmphasisAs: 'fooba' asText |
|
3009 |
'hello' sameStringAndEmphasisAs: 'fooba' asText allBold |
|
3010 |
'hello' sameStringAndEmphasisAs: 'fooba' asText allItalic |
|
5510
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
3011 |
" |
11861 | 3012 |
! |
3013 |
||
3014 |
spellAgainst: aString |
|
3015 |
"return an integer between 0 and 100 indicating how similar |
|
3016 |
the argument is to the receiver. No case conversion is done. |
|
3017 |
This algorithm is much simpler (but also less exact) than the |
|
3018 |
levenshtein distance. Experiment which is better for your |
|
3019 |
application." |
|
3020 |
||
3021 |
| i1 "{ Class: SmallInteger }" |
|
3022 |
i2 "{ Class: SmallInteger }" |
|
3023 |
next1 "{ Class: SmallInteger }" |
|
3024 |
next2 "{ Class: SmallInteger }" |
|
3025 |
size1 "{ Class: SmallInteger }" |
|
3026 |
size2 "{ Class: SmallInteger }" |
|
3027 |
score "{ Class: SmallInteger }" |
|
3028 |
maxLen "{ Class: SmallInteger }" | |
|
3029 |
||
3030 |
size1 := self size. |
|
3031 |
size2 := aString size. |
|
3032 |
maxLen := size1 max:size2. |
|
3033 |
score := 0. |
|
3034 |
i1 := i2 := 1. |
|
3035 |
[i1 <= size1 and: [i2 <= size2]] whileTrue:[ |
|
20874 | 3036 |
next1 := i1 + 1. |
3037 |
next2 := i2 + 1. |
|
3038 |
(self at:i1) == (aString at:i2) ifTrue: [ |
|
3039 |
score := score+1. |
|
3040 |
i1 := next1. |
|
3041 |
i2 := next2 |
|
3042 |
] ifFalse: [ |
|
3043 |
(i2 < size2 and: [(self at:i1) == (aString at:next2)]) ifTrue: [ |
|
3044 |
i2 := next2 |
|
3045 |
] ifFalse: [ |
|
3046 |
(i1 < size1 and: [(self at:next1) == (aString at:i2)]) ifTrue: [ |
|
3047 |
i1 := next1 |
|
3048 |
] ifFalse: [ |
|
3049 |
i1 := next1. |
|
3050 |
i2 := next2 |
|
3051 |
] |
|
3052 |
] |
|
3053 |
] |
|
11861 | 3054 |
]. |
3055 |
||
3056 |
score == maxLen ifTrue: [^ 100]. |
|
3057 |
^ 100 * score // maxLen |
|
3058 |
||
3059 |
" |
|
3060 |
'Smalltalk' spellAgainst: 'Smalltlak' |
|
3061 |
'Smalltalk' spellAgainst: 'smalltlak' |
|
3062 |
'Smalltalk' spellAgainst: 'smalltalk' |
|
3063 |
'Smalltalk' spellAgainst: 'smalltlk' |
|
3064 |
'Smalltalk' spellAgainst: 'Smalltolk' |
|
3065 |
" |
|
3066 |
! |
|
3067 |
||
16553 | 3068 |
startsWith:aStringOrCharacter |
3069 |
"return true, if the receiver starts with something, aStringOrCharacter. |
|
17371 | 3070 |
If the argument is empty, true is returned. |
3071 |
Notice, that this is similar to, but slightly different from VW's and Squeak's beginsWith:, |
|
3072 |
which are both inconsistent w.r.t. an empty argument." |
|
11861 | 3073 |
|
3074 |
|s| |
|
3075 |
||
16553 | 3076 |
aStringOrCharacter isCharacter ifTrue:[ |
23154 | 3077 |
^ (self size ~~ 0) and:[ (self at:1) = aStringOrCharacter ] |
16553 | 3078 |
]. |
11861 | 3079 |
(s := self string) ~~ self ifTrue:[ |
20874 | 3080 |
^ s startsWith:aStringOrCharacter |
11861 | 3081 |
]. |
16553 | 3082 |
^ super startsWith:aStringOrCharacter |
11861 | 3083 |
|
3084 |
" |
|
17364 | 3085 |
'abcde' startsWith:#($a $b $c) |
3086 |
'abcde' startsWith:'abc' |
|
3087 |
'abcd' startsWith:'abcde' |
|
3088 |
'abcde' startsWith:'abd' |
|
11861 | 3089 |
'hello world' startsWith:'hello' |
3090 |
'hello world' asText allBold startsWith:'hello' |
|
3091 |
'hello world' asText allBold startsWith:'' |
|
3092 |
" |
|
3093 |
||
23154 | 3094 |
"Created: / 12-05-1996 / 15:46:40 / cg" |
3095 |
"Modified: / 29-06-2018 / 11:27:08 / Claus Gittinger" |
|
3096 |
! |
|
3097 |
||
3098 |
startsWith:aStringOrCharacter caseSensitive:caseSensitive |
|
3099 |
"return true, if the receiver starts with something, aStringOrCharacter. |
|
3100 |
If the argument is empty, true is returned. |
|
3101 |
Notice, that this is similar to, but slightly different from VW's and Squeak's beginsWith:, |
|
3102 |
which are both inconsistent w.r.t. an empty argument." |
|
3103 |
||
3104 |
|s| |
|
3105 |
||
3106 |
caseSensitive ifTrue:[^ self startsWith:aStringOrCharacter]. |
|
3107 |
aStringOrCharacter isCharacter ifTrue:[ |
|
3108 |
^ (self size ~~ 0) and:[ (self at:1) asLowercase = aStringOrCharacter asLowercase ] |
|
3109 |
]. |
|
3110 |
(s := self string) ~~ self ifTrue:[ |
|
3111 |
^ s startsWith:aStringOrCharacter caseSensitive:caseSensitive |
|
3112 |
]. |
|
3113 |
^ self startsWith:aStringOrCharacter using:[:a :b | a asLowercase = b asLowercase]. |
|
3114 |
||
3115 |
" |
|
3116 |
'aBCde' startsWith:'abc' caseSensitive:true |
|
3117 |
'aBCde' startsWith:'abc' caseSensitive:false |
|
3118 |
" |
|
3119 |
||
3120 |
"Created: / 29-06-2018 / 11:27:04 / Claus Gittinger" |
|
9192 | 3121 |
! ! |
3122 |
||
3123 |
!CharacterArray methodsFor:'converting'! |
|
3124 |
||
11881 | 3125 |
asAsciiZ |
3126 |
"if the receiver does not end with a 0-valued character, return a copy of it, |
|
3127 |
with an additional 0-character. Otherwise return the receiver. This is sometimes |
|
3128 |
needed when a string has to be passed to C, which needs 0-terminated strings. |
|
3129 |
Notice, that all singleByte strings are already 0-terminated in ST/X, whereas wide |
|
3130 |
strings are not." |
|
3131 |
||
12658 | 3132 |
(self notEmpty and:[self last codePoint == 0]) ifTrue:[^ self ]. |
11881 | 3133 |
^ self copyWith:(Character value:0). |
3134 |
||
3135 |
" |
|
3136 |
'abc' asAsciiZ |
|
3137 |
'abc' asWideString asAsciiZ |
|
3138 |
" |
|
3139 |
! |
|
3140 |
||
13524 | 3141 |
asByteArray |
15936 | 3142 |
"depending on the size of the characters in the receiver, |
3143 |
return a byteArray containing single-, double- or even 4-bytewise values. |
|
3144 |
The size of the returned byteArray will be the strings size multiplied by the |
|
3145 |
size required for the largest character. |
|
19233 | 3146 |
Attention: The bytes are in native byte order. |
21745 | 3147 |
Caveat: better use utf8Encoded, to get reproducible results" |
15936 | 3148 |
|
14555 | 3149 |
|bytes sz bytesPerCharacter idx str| |
3150 |
||
3151 |
str := self string. |
|
3152 |
str ~~ self ifTrue:[ |
|
20874 | 3153 |
"/ for text and other wrappers |
3154 |
^ str asByteArray |
|
13611
ba8c7372218a
variable renamed in: #inspector2TabBytes
Claus Gittinger <cg@exept.de>
parents:
13524
diff
changeset
|
3155 |
]. |
ba8c7372218a
variable renamed in: #inspector2TabBytes
Claus Gittinger <cg@exept.de>
parents:
13524
diff
changeset
|
3156 |
|
ba8c7372218a
variable renamed in: #inspector2TabBytes
Claus Gittinger <cg@exept.de>
parents:
13524
diff
changeset
|
3157 |
"/ for real strings, a fallback |
13524 | 3158 |
sz := self size. |
19757 | 3159 |
bytesPerCharacter := self bytesPerCharacter. |
13524 | 3160 |
bytes := ByteArray new:(sz * bytesPerCharacter). |
3161 |
idx := 1. |
|
3162 |
self do:[:char | |
|
20874 | 3163 |
|code| |
3164 |
||
3165 |
code := char codePoint. |
|
3166 |
bytesPerCharacter == 2 ifTrue:[ |
|
3167 |
bytes unsignedInt16At:idx put:code |
|
3168 |
] ifFalse:[ |
|
3169 |
bytesPerCharacter == 4 ifTrue:[ |
|
3170 |
bytes unsignedInt32At:idx put:code |
|
3171 |
] ifFalse:[ |
|
3172 |
bytes at:idx put:code |
|
3173 |
]. |
|
3174 |
]. |
|
3175 |
idx := idx + bytesPerCharacter. |
|
13524 | 3176 |
]. |
3177 |
^ bytes |
|
3178 |
||
3179 |
"Created: / 27-07-2011 / 00:56:17 / cg" |
|
21745 | 3180 |
"Modified (comment): / 11-05-2017 / 09:21:57 / mawalch" |
13524 | 3181 |
! |
3182 |
||
19233 | 3183 |
asByteArrayMSB:msb |
3184 |
"depending on the size of the characters in the receiver, |
|
3185 |
return a byteArray containing single-, double- or even 4-bytewise values. |
|
3186 |
The size of the returned byteArray will be the strings size multiplied by the |
|
3187 |
size required for the largest character. |
|
21745 | 3188 |
Caveat: better use utf8Encoded, to get reproducible results" |
19233 | 3189 |
|
3190 |
|ba| |
|
3191 |
||
3192 |
ba := self asByteArray. "/ native order |
|
3193 |
UninterpretedBytes isBigEndian ~~ msb ifTrue:[ |
|
20874 | 3194 |
ba swapBytes |
19233 | 3195 |
]. |
3196 |
^ ba |
|
21745 | 3197 |
|
3198 |
"Modified (comment): / 11-05-2017 / 09:22:01 / mawalch" |
|
19233 | 3199 |
! |
3200 |
||
20480 | 3201 |
asCanonicalizedFilename |
3202 |
"return a Filename with pathname taken from the receiver. |
|
3203 |
The filename is canonicalized, meaning that it cares for trailing directory separators, |
|
3204 |
'.' components etc." |
|
3205 |
||
3206 |
^ self asFilename asCanonicalizedFilename |
|
3207 |
||
3208 |
"on windows: |
|
3209 |
'c:\foo\bar' asFilename |
|
3210 |
'c:\foo\bar\' asFilename |
|
3211 |
'c:\foo\bar\..\baz' asFilename |
|
3212 |
'c:\foo\bar\..\baz\.' asFilename |
|
3213 |
'c:\foo\bar' asCanonicalizedFilename |
|
3214 |
'c:\foo\bar\' asCanonicalizedFilename |
|
3215 |
'c:\foo\bar\..\baz' asCanonicalizedFilename |
|
3216 |
'c:\foo\bar\..\baz\.' asCanonicalizedFilename |
|
3217 |
||
3218 |
on unix: |
|
3219 |
'/foo/bar' asFilename |
|
3220 |
'/foo/bar/' asFilename |
|
3221 |
'/foo/bar/../baz' asFilename |
|
3222 |
'/foo/bar/../baz/.' asFilename |
|
3223 |
'/foo/bar' asCanonicalizedFilename |
|
3224 |
'/foo/bar/' asCanonicalizedFilename |
|
3225 |
'/foo/bar/../baz' asCanonicalizedFilename |
|
3226 |
'/foo/bar/../baz/.' asCanonicalizedFilename |
|
3227 |
" |
|
3228 |
! |
|
3229 |
||
608 | 3230 |
asCollectionOfLines |
8874 | 3231 |
"return a collection containing the lines (separated by cr) |
608 | 3232 |
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
|
3233 |
contain empty strings. |
14456
d7c3e67647f1
comment/format in: #asCollectionOfLines
Claus Gittinger <cg@exept.de>
parents:
14455
diff
changeset
|
3234 |
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
|
3235 |
See also #asCollectionOfLinesWithReturn |
d7c3e67647f1
comment/format in: #asCollectionOfLines
Claus Gittinger <cg@exept.de>
parents:
14455
diff
changeset
|
3236 |
(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
|
3237 |
this method and we did not want to risk any incompatibilities)" |
14455
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3238 |
|
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3239 |
^ self asCollectionOfSubstringsSeparatedBy:Character cr. |
608 | 3240 |
|
3241 |
" |
|
3242 |
'1 one\2 two\3 three\4 four\5 five' withCRs asCollectionOfLines |
|
8874 | 3243 |
'1 one\\\\2 two\3 three' withCRs asCollectionOfLines |
20182 | 3244 |
|
3245 |
('foo \r\nbar\nbaz\t\r\n\r\nbla' printf:#()) |
|
3246 |
asCollectionOfLines collect:[:l | (l endsWith:Character return) ifTrue:[l copyButLast:1] ifFalse:l] |
|
608 | 3247 |
" |
3248 |
! |
|
3249 |
||
14455
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3250 |
asCollectionOfLinesWithReturn |
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3251 |
"return a collection containing the lines (separated by cr) |
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3252 |
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
|
3253 |
contain empty strings." |
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3254 |
|
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3255 |
|lines| |
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3256 |
|
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3257 |
lines := self asCollectionOfSubstringsSeparatedBy:Character cr. |
18194 | 3258 |
(lines notEmpty and:[lines last isEmpty]) ifTrue:[ |
20874 | 3259 |
^ lines copyButLast:1 |
14455
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3260 |
]. |
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3261 |
^ lines |
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3262 |
|
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3263 |
" |
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3264 |
'1\2\3' withCRs asCollectionOfLines |
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3265 |
'1\2\3\' withCRs asCollectionOfLines |
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3266 |
'1\2\3' withCRs asCollectionOfLinesWithReturn |
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3267 |
'1\2\3\' withCRs asCollectionOfLinesWithReturn |
18194 | 3268 |
'' withCRs asCollectionOfLinesWithReturn |
14455
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3269 |
" |
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3270 |
! |
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
3271 |
|
608 | 3272 |
asCollectionOfSubstringsSeparatedBy:aCharacter |
15740 | 3273 |
"return a collection containing substrings (separated by aCharacter) |
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
3274 |
of the receiver. |
15740 | 3275 |
If aCharacter occurs multiple times in a row, the result will contain empty strings. |
3276 |
If the receiver ends with aCharacter, an empty string with be the last result element." |
|
608 | 3277 |
|
9192 | 3278 |
^ self asCollectionOfSubCollectionsSeparatedBy:aCharacter |
3279 |
||
3280 |
" |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
3281 |
'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
|
3282 |
'1 one:2 two:3 three:4 four:5 five:' asCollectionOfSubstringsSeparatedBy:$: |
15740 | 3283 |
'1 one 2 two 3 three 4 four 5 five' asCollectionOfSubstringsSeparatedBy:Character space |
9192 | 3284 |
" |
3285 |
! |
|
608 | 3286 |
|
11828 | 3287 |
asCollectionOfSubstringsSeparatedBy:aCharacter exceptIn:ch |
15740 | 3288 |
"return a collection containing the substrings (separated by aCharacter) |
12177 | 3289 |
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
|
3290 |
the result will contain empty strings. |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
3291 |
The separation is not done, inside a matching pair of ch-substrings. |
15740 | 3292 |
Can be used to tokenize csv-like strings, which may or may not be enclosed in quotes." |
11828 | 3293 |
|
3294 |
|lines myClass except i c |
|
3295 |
startIndex "{ Class:SmallInteger }" |
|
3296 |
stopIndex "{ Class:SmallInteger }" | |
|
3297 |
||
11905
cbc2191cfe2d
return stringCollections from asCollectionOfXXX
Claus Gittinger <cg@exept.de>
parents:
11904
diff
changeset
|
3298 |
lines := StringCollection new. |
11828 | 3299 |
myClass := self species. |
3300 |
||
3301 |
startIndex := 1. |
|
3302 |
except := false. |
|
3303 |
[ |
|
20874 | 3304 |
i := startIndex-1. |
3305 |
[ |
|
3306 |
i := i+1. |
|
3307 |
c := self at:i. |
|
3308 |
c = ch ifTrue:[ except := except not. ]. |
|
3309 |
i < self size and:[except or:[c ~= aCharacter]] |
|
3310 |
] whileTrue. |
|
3311 |
||
3312 |
c = aCharacter ifTrue:[ |
|
3313 |
stopIndex := i -1. |
|
3314 |
] ifFalse: [ |
|
3315 |
stopIndex := i. |
|
3316 |
]. |
|
3317 |
(stopIndex < startIndex) ifTrue: [ |
|
3318 |
lines add:(myClass new:0) |
|
3319 |
] ifFalse: [ |
|
3320 |
lines add:(self copyFrom:startIndex to:stopIndex) |
|
3321 |
]. |
|
3322 |
startIndex := stopIndex + 2. |
|
3323 |
startIndex <= self size |
|
11828 | 3324 |
] whileTrue. |
3325 |
^ lines |
|
3326 |
||
3327 |
" |
|
12177 | 3328 |
'asd''f;d''dd;s' asCollectionOfSubstringsSeparatedBy:$; exceptIn:$' |
3329 |
" |
|
3330 |
"/ 'asd "hello bla" foo "bla bla" bar' asCollectionOfSubstringsSeparatedBy:$ exceptIn:$" |
|
11828 | 3331 |
! |
3332 |
||
13344
ec6860c49990
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13343
diff
changeset
|
3333 |
asCollectionOfSubstringsSeparatedBy:aFieldSeparatorString textSeparator:aTextSeparatorOrNil |
13343
3f058ad0d2f1
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13332
diff
changeset
|
3334 |
"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
|
3335 |
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
|
3336 |
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
|
3337 |
|
13344
ec6860c49990
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13343
diff
changeset
|
3338 |
|aTextSeparatorChar items scanningWord inStream element lastIsFieldSeparator sz| |
ec6860c49990
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13343
diff
changeset
|
3339 |
|
ec6860c49990
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13343
diff
changeset
|
3340 |
aTextSeparatorOrNil isNil ifTrue:[ |
19961 | 3341 |
^ self asCollectionOfSubstringsSeparatedByAll: aFieldSeparatorString |
13327
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3342 |
]. |
13344
ec6860c49990
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13343
diff
changeset
|
3343 |
sz := aTextSeparatorOrNil size. |
ec6860c49990
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13343
diff
changeset
|
3344 |
sz = 0 ifTrue:[ |
19961 | 3345 |
aTextSeparatorChar := aTextSeparatorOrNil |
13344
ec6860c49990
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13343
diff
changeset
|
3346 |
] ifFalse:[sz = 1 ifTrue:[ |
19961 | 3347 |
"this is a String. Fetch the first character - compatibility to older expecco libs" |
3348 |
aTextSeparatorChar := aTextSeparatorOrNil first. |
|
13344
ec6860c49990
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13343
diff
changeset
|
3349 |
] ifFalse:[ |
19961 | 3350 |
self error:'textSeparatorSize > 1'. |
13344
ec6860c49990
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13343
diff
changeset
|
3351 |
]]. |
13327
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3352 |
|
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3353 |
items := OrderedCollection new. |
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3354 |
|
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3355 |
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
|
3356 |
[ |
19961 | 3357 |
inStream skipSeparators. |
3358 |
inStream atEnd |
|
13327
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3359 |
] whileFalse:[ |
19961 | 3360 |
lastIsFieldSeparator := false. |
3361 |
inStream peek == aTextSeparatorChar ifTrue:[ |
|
3362 |
inStream next. |
|
3363 |
element := ''. |
|
3364 |
scanningWord := true. |
|
3365 |
[ scanningWord and:[inStream atEnd not] ] whileTrue:[ |
|
3366 |
element := element , (inStream upTo:aTextSeparatorChar). |
|
3367 |
(inStream peek == aTextSeparatorChar) ifTrue:[ |
|
3368 |
element := element , aTextSeparatorChar . |
|
3369 |
inStream next. |
|
3370 |
] ifFalse:[ |
|
3371 |
scanningWord := false. |
|
3372 |
]. |
|
3373 |
]. |
|
3374 |
inStream upToAll_positionBefore:aFieldSeparatorString. |
|
3375 |
] ifFalse:[ |
|
3376 |
element := inStream upToAll_positionBefore:aFieldSeparatorString |
|
3377 |
]. |
|
3378 |
items add:element. |
|
3379 |
lastIsFieldSeparator := (inStream skipThroughAll:aFieldSeparatorString) notNil. |
|
13343
3f058ad0d2f1
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13332
diff
changeset
|
3380 |
]. |
3f058ad0d2f1
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13332
diff
changeset
|
3381 |
lastIsFieldSeparator ifTrue:[ |
19961 | 3382 |
"empty element at the end of the line" |
3383 |
items add:''. |
|
13327
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3384 |
]. |
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3385 |
|
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3386 |
^ items |
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3387 |
|
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3388 |
" |
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3389 |
self assert:(('#First#, #Second,SecondAdd#, #Third#' asCollectionOfSubstringsSeparatedBy:',' textSeparator: $#) |
19961 | 3390 |
sameContentsAs:#('First' 'Second,SecondAdd' 'Third')). |
13327
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3391 |
self assert:(('#Fir##st#, #Second,SecondAdd#, #Third#' asCollectionOfSubstringsSeparatedBy:',' textSeparator: $#) |
19961 | 3392 |
sameContentsAs:#('Fir#st' 'Second,SecondAdd' 'Third')). |
13327
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3393 |
self assert:(('#Fir##st#, Second,SecondAdd, #Third#' asCollectionOfSubstringsSeparatedBy:',' textSeparator: $#) |
19961 | 3394 |
sameContentsAs:#('Fir#st' 'Second' 'SecondAdd' 'Third')). |
13343
3f058ad0d2f1
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13332
diff
changeset
|
3395 |
self assert:(('First,Second,Third,,' asCollectionOfSubstringsSeparatedBy:',' textSeparator:nil) |
19961 | 3396 |
sameContentsAs:#('First' 'Second' 'Third' '' '')). |
13344
ec6860c49990
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13343
diff
changeset
|
3397 |
self assert:(('First,Second,Third,,' asCollectionOfSubstringsSeparatedBy:',' textSeparator:'#') |
19961 | 3398 |
sameContentsAs:#('First' 'Second' 'Third' '' '')). |
13327
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3399 |
" |
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3400 |
|
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
3401 |
"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
|
3402 |
! |
999589445aa7
add new method for splitting a csv string into csv elements
ab
parents:
10437
diff
changeset
|
3403 |
|
7339
20969b5770c9
asCollectionOfSubstringsSeparatedByAll:
Claus Gittinger <cg@exept.de>
parents:
7318
diff
changeset
|
3404 |
asCollectionOfSubstringsSeparatedByAll:aSeparatorString |
8874 | 3405 |
"return a collection containing the lines (separated by aSeparatorString) |
3406 |
of the receiver. If aSeparatorString occurs multiple times in a row, |
|
7339
20969b5770c9
asCollectionOfSubstringsSeparatedByAll:
Claus Gittinger <cg@exept.de>
parents:
7318
diff
changeset
|
3407 |
the result will contain empty strings." |
20969b5770c9
asCollectionOfSubstringsSeparatedByAll:
Claus Gittinger <cg@exept.de>
parents:
7318
diff
changeset
|
3408 |
|
8852
9f5984eff2c2
use asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8794
diff
changeset
|
3409 |
^ self asCollectionOfSubCollectionsSeparatedByAll:aSeparatorString |
9f5984eff2c2
use asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8794
diff
changeset
|
3410 |
|
9f5984eff2c2
use asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8794
diff
changeset
|
3411 |
" |
8874 | 3412 |
'1::2::3::4::5::' asCollectionOfSubstringsSeparatedByAll:'::' |
7339
20969b5770c9
asCollectionOfSubstringsSeparatedByAll:
Claus Gittinger <cg@exept.de>
parents:
7318
diff
changeset
|
3413 |
" |
20969b5770c9
asCollectionOfSubstringsSeparatedByAll:
Claus Gittinger <cg@exept.de>
parents:
7318
diff
changeset
|
3414 |
! |
20969b5770c9
asCollectionOfSubstringsSeparatedByAll:
Claus Gittinger <cg@exept.de>
parents:
7318
diff
changeset
|
3415 |
|
608 | 3416 |
asCollectionOfSubstringsSeparatedByAny:aCollectionOfSeparators |
3417 |
"return a collection containing the words (separated by any character |
|
3418 |
from aCollectionOfSeparators) of the receiver. |
|
3419 |
This allows breaking up strings using any character as separator." |
|
3420 |
||
8852
9f5984eff2c2
use asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8794
diff
changeset
|
3421 |
^ self asCollectionOfSubCollectionsSeparatedByAny:aCollectionOfSeparators |
608 | 3422 |
|
3423 |
" |
|
3424 |
'hello:world:isnt:this nice' asCollectionOfSubstringsSeparatedByAny:#($:) |
|
8874 | 3425 |
'hello:world:isnt:this nice' asCollectionOfSubstringsSeparatedByAny:':' |
3426 |
'hello:world:isnt:this nice' asCollectionOfSubstringsSeparatedByAny:(Array with:$: with:Character space) |
|
3427 |
'hello:world:isnt:this nice' asCollectionOfSubstringsSeparatedByAny:': ' |
|
3428 |
'h1e2l3l4o' asCollectionOfSubstringsSeparatedByAny:($1 to: $9) |
|
608 | 3429 |
" |
3430 |
! |
|
3431 |
||
3432 |
asCollectionOfWords |
|
8874 | 3433 |
"return a collection containing the words (separated by whitespace) |
3434 |
of the receiver. Multiple occurrences of whitespace characters will |
|
17517 | 3435 |
be treated like one - i.e. whitespace is skipped. |
3436 |
(sigh: it is called #'subStrings' in V'Age, and #'substrings' in Squeak) " |
|
608 | 3437 |
|
7797 | 3438 |
|words| |
608 | 3439 |
|
11905
cbc2191cfe2d
return stringCollections from asCollectionOfXXX
Claus Gittinger <cg@exept.de>
parents:
11904
diff
changeset
|
3440 |
words := StringCollection new. |
7797 | 3441 |
self asCollectionOfWordsDo:[:w | words add:w]. |
608 | 3442 |
^ words |
3443 |
||
3444 |
" |
|
3445 |
'hello world isnt this nice' asCollectionOfWords |
|
3446 |
' hello world isnt this nice ' asCollectionOfWords |
|
3447 |
'hello' asCollectionOfWords |
|
3448 |
'' asCollectionOfWords |
|
3449 |
' ' asCollectionOfWords |
|
10703
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
3450 |
' foo bar__baz__bla__ bar ' asCollectionOfWords |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
3451 |
' foo __bar__baz__bla__ bar ' asCollectionOfWords |
608 | 3452 |
" |
9192 | 3453 |
! |
3454 |
||
3455 |
asCollectionOfWordsDo:aBlock |
|
8874 | 3456 |
"evaluate aBlock for each word (separated by whitespace) of the receiver. |
3457 |
Multiple occurrences of whitespace characters will be treated like one |
|
7797 | 3458 |
- i.e. whitespace is skipped. |
3459 |
Returns the number of words (i.e. the number of invocations of aBlock)." |
|
3460 |
||
3461 |
|count "{ Class:SmallInteger }" |
|
8874 | 3462 |
start "{ Class:SmallInteger }" |
3463 |
stop "{ Class:SmallInteger }" |
|
7797 | 3464 |
mySize "{ Class:SmallInteger }"| |
3465 |
||
3466 |
count := 0. |
|
3467 |
start := 1. |
|
3468 |
mySize := self size. |
|
3469 |
[start <= mySize] whileTrue:[ |
|
20874 | 3470 |
start := self indexOfNonSeparatorStartingAt:start. |
3471 |
start == 0 ifTrue:[ |
|
3472 |
^ count |
|
3473 |
]. |
|
3474 |
stop := self indexOfSeparatorStartingAt:start. |
|
3475 |
stop == 0 ifTrue:[ |
|
3476 |
aBlock value:(self copyFrom:start to:mySize). |
|
3477 |
^ count + 1 |
|
3478 |
]. |
|
3479 |
aBlock value:(self copyFrom:start to:(stop - 1)). |
|
3480 |
start := stop. |
|
3481 |
count := count + 1 |
|
7797 | 3482 |
]. |
3483 |
^ count |
|
3484 |
||
3485 |
" |
|
3486 |
'hello world isnt this nice' asCollectionOfWordsDo:[:w | Transcript showCR:w] |
|
3487 |
' hello world isnt this nice ' asCollectionOfWordsDo:[:w | Transcript showCR:w] |
|
3488 |
'hello' asCollectionOfWordsDo:[:w | Transcript showCR:w] |
|
3489 |
'' asCollectionOfWordsDo:[:w | Transcript showCR:w] |
|
3490 |
' ' asCollectionOfWordsDo:[:w | Transcript showCR:w] |
|
3491 |
" |
|
9192 | 3492 |
! |
3493 |
||
3494 |
asFilename |
|
3495 |
"return a Filename with pathname taken from the receiver" |
|
3496 |
||
3497 |
^ Filename named:self "(self asSingleByteStringReplaceInvalidWith:$?)" |
|
3498 |
||
3499 |
"Modified: 20.5.1996 / 09:38:15 / cg" |
|
3500 |
! |
|
608 | 3501 |
|
3060 | 3502 |
asFixedPoint |
3503 |
"read a fixedPoint number from the receiver. |
|
8874 | 3504 |
Notice, that errors may occur during the read, |
7423 | 3505 |
so you better setup some signal handler when using this method." |
3060 | 3506 |
|
3507 |
^ FixedPoint readFromString:self |
|
3508 |
||
3509 |
" |
|
8874 | 3510 |
'0.123' asFixedPoint |
3511 |
'12345' asFixedPoint |
|
3060 | 3512 |
'(1/5)' asFixedPoint |
3513 |
'foo' asFixedPoint |
|
9192 | 3514 |
Object errorSignal handle:[:ex | ex return:0] do:['foo' asFixedPoint] |
3515 |
" |
|
3516 |
||
3517 |
"Modified: / 25.10.1997 / 15:19:00 / cg" |
|
3518 |
! |
|
3519 |
||
3520 |
asFixedPoint:scale |
|
3060 | 3521 |
"read a fixedPoint number with scale number of post-decimal digits |
3522 |
from the receiver. Scale controls the number of displayed digits, |
|
3523 |
not the number of actually valid digits. |
|
8874 | 3524 |
Notice, that errors may occur during the read, |
7423 | 3525 |
so you better setup some signal handler when using this method." |
3060 | 3526 |
|
3527 |
^ (FixedPoint readFromString:self) scale:scale |
|
3528 |
||
3529 |
" |
|
8874 | 3530 |
'0.123' asFixedPoint:2 |
3531 |
'123456' asFixedPoint:2 |
|
3532 |
('3.14157' asFixedPoint:1) asFixedPoint:5 |
|
3533 |
'3.14157' asFixedPoint:2 |
|
3534 |
'foo' asFixedPoint:2 |
|
3060 | 3535 |
" |
3536 |
||
3537 |
"Modified: / 25.10.1997 / 15:21:57 / cg" |
|
9192 | 3538 |
! |
3539 |
||
3540 |
asFloat |
|
608 | 3541 |
"read a float number from the receiver. |
8874 | 3542 |
Notice, that errors may occur during the read, |
7422 | 3543 |
so you better setup some exception handler when using this method." |
608 | 3544 |
|
3545 |
^ (Number readFromString:self) asFloat |
|
3546 |
||
3547 |
" |
|
8874 | 3548 |
'0.123' asFloat |
608 | 3549 |
'12345' asFloat |
3550 |
'(1/5)' asFloat |
|
8874 | 3551 |
Object errorSignal handle:[:ex | ex return:0] do:['foo' asFloat] |
608 | 3552 |
" |
3553 |
! |
|
3554 |
||
3555 |
asInteger |
|
9190 | 3556 |
"convert the receiver into an integer. |
8874 | 3557 |
Notice, that errors may occur during the read, |
9190 | 3558 |
so you better setup some exception handler when using this method. |
3559 |
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
|
3560 |
in other smalltalks. |
9190 | 3561 |
For less strict integer reading, use Integer readFrom:aString" |
608 | 3562 |
|
3563 |
^ Integer readFromString:self |
|
3564 |
||
3565 |
" |
|
3566 |
'12345678901234567890' asInteger |
|
3567 |
'-1234' asInteger |
|
9190 | 3568 |
|
3569 |
The following raises an error: |
|
20874 | 3570 |
'0.123' asInteger <- reader finds more after reading 0 |
9190 | 3571 |
|
3572 |
whereas the less strict readFrom does not: |
|
20874 | 3573 |
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
|
3574 |
|
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
3575 |
'0.123' asInteger |
608 | 3576 |
'0.123' asNumber <- returns what you expect |
8874 | 3577 |
Object errorSignal handle:[:ex | ex return:0] do:['foo' asInteger] |
9190 | 3578 |
|
3579 |
" |
|
9192 | 3580 |
! |
3581 |
||
3582 |
asLowercase |
|
608 | 3583 |
"return a copy of myself in lowercase letters" |
3584 |
||
8003
6e690f4b79f1
asLowercase/asUppercase and friends care for 16-bit chars
Claus Gittinger <cg@exept.de>
parents:
7995
diff
changeset
|
3585 |
|newStr c bitsPerCharacter |
9192 | 3586 |
mySize "{ Class: SmallInteger }" | |
3587 |
||
3588 |
mySize := self size. |
|
3589 |
mySize == 0 ifTrue:[^ self]. |
|
18657 | 3590 |
|
9192 | 3591 |
newStr := self species new:mySize. |
3592 |
bitsPerCharacter := newStr bitsPerCharacter. |
|
3593 |
||
18657 | 3594 |
"/ handle the very seldom case of an uppercase char which needs |
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
3595 |
"/ more bits in its lowercase variant |
18657 | 3596 |
"/ (there are only a few of them) |
3597 |
||
9192 | 3598 |
1 to:mySize do:[:i | |
20705 | 3599 |
c := (self at:i) asLowercase. |
3600 |
(c bitsPerCharacter > bitsPerCharacter |
|
3601 |
and:[c stringSpecies ~= newStr stringSpecies]) ifTrue:[ |
|
3602 |
newStr := c stringSpecies fromString:newStr. |
|
3603 |
bitsPerCharacter := newStr bitsPerCharacter. |
|
3604 |
]. |
|
3605 |
newStr at:i put:c |
|
9192 | 3606 |
]. |
3607 |
^ newStr |
|
3608 |
||
3609 |
" |
|
3610 |
'HelloWorld' asLowercase |
|
18662 | 3611 |
'HelloWorld' asUnicode16String asLowercase |
9192 | 3612 |
'HelloWorld' asLowercaseFirst |
18657 | 3613 |
'HelloWorld' asUppercase |
20705 | 3614 |
'HelloWorldÿ' asUppercase |
3615 |
'HelloWorldŸ' asLowercase - currently returns an U16 string; should this be u8? |
|
9192 | 3616 |
" |
3617 |
! |
|
3618 |
||
3619 |
asLowercaseFirst |
|
17508 | 3620 |
"return a copy of myself where the first character is converted to lowercase. |
3621 |
If the first character is already lowercase, or there is no uppercase for it, return the |
|
3622 |
receiver." |
|
9192 | 3623 |
|
3624 |
|newString firstChar firstCharAsLowercase| |
|
3625 |
||
11525 | 3626 |
self isEmpty ifTrue:[^ self]. |
9192 | 3627 |
firstChar := (self at:1). |
3628 |
firstCharAsLowercase := firstChar asLowercase. |
|
3629 |
firstChar == firstCharAsLowercase ifTrue:[ ^ self]. |
|
3630 |
||
3631 |
firstCharAsLowercase bitsPerCharacter > self bitsPerCharacter ifTrue:[ |
|
20874 | 3632 |
newString := firstCharAsLowercase stringSpecies fromString:self. |
9192 | 3633 |
] ifFalse:[ |
20874 | 3634 |
newString := self stringSpecies fromString:self. |
9192 | 3635 |
]. |
9190 | 3636 |
newString at:1 put:firstCharAsLowercase. |
3637 |
^ newString |
|
608 | 3638 |
|
3639 |
" |
|
8874 | 3640 |
'HelloWorld' asLowercase |
3641 |
'HelloWorld' asLowercaseFirst |
|
608 | 3642 |
" |
3643 |
! |
|
3644 |
||
11828 | 3645 |
asLowercaseLast |
3646 |
"return a copy of myself where the last character is |
|
3647 |
converted to lowercase." |
|
3648 |
||
3649 |
|newString sz| |
|
3650 |
||
3651 |
sz := self size. |
|
3652 |
newString := self copyFrom:1 to:sz. |
|
3653 |
sz > 0 ifTrue:[ |
|
20874 | 3654 |
newString at:sz put:(newString at:sz) asLowercase |
11828 | 3655 |
]. |
3656 |
^ newString |
|
3657 |
||
3658 |
" |
|
12177 | 3659 |
'HelloWorld' asLowercase |
3660 |
'HelloWorlD' asLowercaseLast |
|
11828 | 3661 |
" |
3662 |
! |
|
3663 |
||
17644 | 3664 |
asMutator |
19473 | 3665 |
"return a corresponding setter method's selector. |
3666 |
I.e. #foo asMutator returns #foo:" |
|
17644 | 3667 |
|
20873 | 3668 |
^ (self asSingleByteString,':') asSymbol |
17644 | 3669 |
! |
3670 |
||
9190 | 3671 |
asNumber |
3672 |
"read a number from the receiver. |
|
3673 |
Notice, that (in contrast to ST-80) errors may occur during the read, |
|
3674 |
so you better setup some signal handler when using this method. |
|
3675 |
Also notice, that this is meant to read end-user numbers from a string; |
|
9135 | 3676 |
it does not handle smalltalk numbers (i.e. radix). |
3677 |
To read a smalltalk number, use Number >> readSmalltalkFrom:. |
|
608 | 3678 |
This may change if ANSI specifies it." |
3679 |
||
3680 |
"/ ST-80 behavior: |
|
3681 |
"/ ^ Number readFromString:self onError:0 |
|
3682 |
||
17061 | 3683 |
^ Number fromString:self |
3684 |
||
3685 |
" |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
3686 |
'123' asNumber |
608 | 3687 |
'123.567' asNumber |
3688 |
'(5/6)' asNumber |
|
3689 |
'foo' asNumber |
|
17061 | 3690 |
'123a' asNumber |
8874 | 3691 |
Object errorSignal handle:[:ex | ex returnWith:0] do:['foo' asNumber] |
608 | 3692 |
" |
9192 | 3693 |
! |
3694 |
||
3695 |
asNumberFromFormatString:ignored |
|
608 | 3696 |
"read a number from the receiver, ignoring any nonDigit characters. |
3697 |
This is typically used to convert from strings which include |
|
3698 |
dollar-signs or millenium digits. However, this method also ignores |
|
3699 |
the decimal point (if any) and therefore should be used with care." |
|
3700 |
||
3701 |
|tempString| |
|
3702 |
||
3703 |
tempString := self collect:[:char | char isDigit]. |
|
3704 |
^ Number readFromString:tempString onError:0 |
|
3705 |
||
3706 |
" |
|
3707 |
'USD 123' asNumberFromFormatString:'foo' |
|
3708 |
'DM 123' asNumberFromFormatString:'foo' |
|
3709 |
'123' asNumberFromFormatString:'foo' |
|
3710 |
'123.567' asNumberFromFormatString:'foo' |
|
3711 |
'(5/6)' asNumberFromFormatString:'foo' |
|
3712 |
'foo' asNumberFromFormatString:'foo' |
|
3713 |
" |
|
9192 | 3714 |
! |
3715 |
||
21994 | 3716 |
asPackageId |
3717 |
"given a package-string as receiver, return a packageId object. |
|
3718 |
packageIds hide the details of module/directory handling inside the path. |
|
3719 |
See PackageId for the required format of those strings." |
|
3720 |
||
3721 |
^ PackageId from: self string |
|
3722 |
||
21995 | 3723 |
" |
3724 |
'stx:libbasic' asPackageId |
|
3725 |
" |
|
3726 |
||
21994 | 3727 |
"Created: / 11-07-2017 / 18:24:47 / cg" |
3728 |
! |
|
3729 |
||
7977 | 3730 |
asSingleByteStringIfPossible |
3731 |
"if possible, return the receiver converted to a 'normal' string. |
|
18181 | 3732 |
It is only possible, if there are no characters with codePoints above 255 in the receiver. |
3733 |
If not possible, the (wideString) receiver is returned." |
|
7977 | 3734 |
|
14555 | 3735 |
self isWideString ifFalse:[^ self]. |
18967 | 3736 |
self containsNon8BitElements ifTrue:[^ self]. |
3737 |
^ self asSingleByteString |
|
9192 | 3738 |
|
3739 |
" |
|
3740 |
'hello' asSingleByteStringIfPossible |
|
18967 | 3741 |
'hello' asText asSingleByteStringIfPossible |
3742 |
'hello' asUnicodeString asText asSingleByteStringIfPossible |
|
9192 | 3743 |
'hello' asUnicodeString asSingleByteStringIfPossible |
3744 |
" |
|
3745 |
! |
|
3746 |
||
3747 |
asSingleByteStringReplaceInvalidWith:replacementCharacter |
|
3748 |
"return the receiver converted to a 'normal' string, |
|
3749 |
with invalid characters replaced by replacementCharacter. |
|
3750 |
Can be used to convert from 16-bit strings to 8-bit strings |
|
3751 |
and replace characters above code-255 with some replacement." |
|
3752 |
||
3753 |
|newString| |
|
3754 |
||
3755 |
newString := String new:(self size). |
|
3756 |
1 to:self size do:[:idx | |
|
20874 | 3757 |
|char| |
3758 |
||
3759 |
char := self at:idx. |
|
3760 |
char codePoint <= 16rFF ifTrue:[ |
|
3761 |
newString at:idx put:char |
|
3762 |
] ifFalse:[ |
|
3763 |
newString at:idx put:replacementCharacter |
|
3764 |
]. |
|
9190 | 3765 |
]. |
3766 |
^ newString |
|
3767 |
||
3768 |
"Created: 30.6.1997 / 13:02:14 / cg" |
|
7977 | 3769 |
! |
3770 |
||
9192 | 3771 |
asString |
3772 |
"return myself - I am a string" |
|
3773 |
||
3774 |
^ self |
|
3775 |
! |
|
3776 |
||
3777 |
asStringCollection |
|
3778 |
"return a collection of lines from myself." |
|
3779 |
||
3780 |
^ StringCollection fromString:self "string" |
|
3781 |
||
21639 | 3782 |
" |
3783 |
'hello\world\1\2\3' withCRs asStringCollection first |
|
3784 |
" |
|
3785 |
||
3786 |
"Modified: / 13-05-1996 / 20:36:59 / cg" |
|
3787 |
"Modified (comment): / 13-03-2017 / 14:05:14 / cg" |
|
9192 | 3788 |
! |
1375
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
3789 |
|
1434 | 3790 |
asSymbol |
19039 | 3791 |
"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
|
3792 |
The receiver must be a singleByte-String. |
ccd0e8f26439
printing - migrating towards unicodeSupport in ST-code
Claus Gittinger <cg@exept.de>
parents:
8040
diff
changeset
|
3793 |
TwoByte- and FourByteSymbols are (currently ?) not allowed." |
ccd0e8f26439
printing - migrating towards unicodeSupport in ST-code
Claus Gittinger <cg@exept.de>
parents:
8040
diff
changeset
|
3794 |
|
ccd0e8f26439
printing - migrating towards unicodeSupport in ST-code
Claus Gittinger <cg@exept.de>
parents:
8040
diff
changeset
|
3795 |
|str| |
ccd0e8f26439
printing - migrating towards unicodeSupport in ST-code
Claus Gittinger <cg@exept.de>
parents:
8040
diff
changeset
|
3796 |
|
9192 | 3797 |
str := self string. |
3798 |
str ~~ self ifTrue:[ ^ str asSymbol ]. |
|
3799 |
^ self asSingleByteString asSymbol |
|
3800 |
! |
|
3801 |
||
3802 |
asSymbolIfInterned |
|
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
3803 |
"If a symbol with the receiver's characters is already known, return it. |
19692 | 3804 |
Otherwise, return nil. |
9230 | 3805 |
This can be used to query for an existing symbol and is the same as: |
20874 | 3806 |
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
|
3807 |
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
|
3808 |
The receiver must be a singleByte-String. |
ccd0e8f26439
printing - migrating towards unicodeSupport in ST-code
Claus Gittinger <cg@exept.de>
parents:
8040
diff
changeset
|
3809 |
TwoByte- and FourByteSymbols are (currently ?) not allowed." |
1435
3ee1fde2aa58
allow asSymbolIfInterned for Texts
Claus Gittinger <cg@exept.de>
parents:
1434
diff
changeset
|
3810 |
|
18967 | 3811 |
|str| |
3812 |
||
3813 |
str := self string. |
|
3814 |
str == self ifTrue:[ |
|
20874 | 3815 |
"must be some kind of N-ByteString" |
3816 |
str := self asSingleByteStringIfPossible. |
|
3817 |
str == self ifTrue:[ |
|
3818 |
"single byte string conversion is not possible" |
|
3819 |
^ nil. |
|
3820 |
]. |
|
6642 | 3821 |
]. |
18967 | 3822 |
^ str asSymbolIfInterned |
3823 |
||
3824 |
" |
|
3825 |
(Unicode16String with:(Character value:16rFFFF)) asSymbolIfInterned |
|
3826 |
'new' asUnicodeString asSymbolIfInterned |
|
3827 |
'new' asText asSymbolIfInterned |
|
3828 |
'new' asUnicodeString asText asSymbolIfInterned |
|
3829 |
" |
|
1435
3ee1fde2aa58
allow asSymbolIfInterned for Texts
Claus Gittinger <cg@exept.de>
parents:
1434
diff
changeset
|
3830 |
|
3ee1fde2aa58
allow asSymbolIfInterned for Texts
Claus Gittinger <cg@exept.de>
parents:
1434
diff
changeset
|
3831 |
"Created: 22.5.1996 / 16:37:04 / cg" |
9192 | 3832 |
! |
3833 |
||
20160 | 3834 |
asSymbolIfInternedOrSelf |
3835 |
"If a symbol with the receiver's characters is already known, return it. |
|
3836 |
Otherwise, return self. |
|
3837 |
This can be used to query for an existing symbol and is the same as: |
|
3838 |
self knownAsSymbol ifTrue:[self asSymbol] ifFalse:[self] |
|
3839 |
but slightly faster, since the symbol lookup operation is only performed once. |
|
3840 |
The receiver must be a singleByte-String. |
|
3841 |
TwoByte- and FourByteSymbols are (currently ?) not allowed." |
|
3842 |
||
3843 |
|sym| |
|
3844 |
||
3845 |
sym := self asSymbolIfInterned. |
|
3846 |
^ sym isNil ifTrue:[self] ifFalse:[sym]. |
|
3847 |
||
3848 |
||
3849 |
" |
|
3850 |
(Unicode16String with:(Character value:16rFFFF)) asSymbolIfInternedOrSelf |
|
3851 |
'new' asUnicodeString asSymbolIfInternedOrSelf |
|
3852 |
'new' asText asSymbolIfInternedOrSelf |
|
3853 |
'new' asUnicodeString asText asSymbolIfInternedOrSelf |
|
3854 |
" |
|
3855 |
||
3856 |
"Created: 22.5.1996 / 16:37:04 / cg" |
|
3857 |
! |
|
3858 |
||
9192 | 3859 |
asText |
9403 | 3860 |
"return a Text-object (string with emphasis) from myself." |
9192 | 3861 |
|
3862 |
"this test allows for small non-gui apps to be built without libbasic2 (where Text is)" |
|
3863 |
Text isNil ifTrue:[^ self]. |
|
17065 | 3864 |
^ Text string:self |
9192 | 3865 |
|
3866 |
"Created: 12.5.1996 / 10:41:14 / cg" |
|
3867 |
! |
|
3868 |
||
17021 | 3869 |
asTimestamp |
3870 |
"convert the receiver into an Timestamp. |
|
3871 |
Notice, that errors may occur during the read, |
|
3872 |
so you better setup some exception handler when using this method." |
|
3873 |
||
21030 | 3874 |
^ Timestamp readFrom:self |
17021 | 3875 |
|
3876 |
" |
|
3877 |
'2014-11-10 21:30:22.444' asTimestamp |
|
3878 |
'2014-11-10 21:30:22.444Z' asTimestamp |
|
3879 |
'2014-11-10 21:30:22.444+0200' asTimestamp |
|
3880 |
" |
|
3881 |
! |
|
3882 |
||
9192 | 3883 |
asTitlecase |
8027 | 3884 |
"return a version of the receiver, where the first character is converted to titlecase, |
3885 |
and everything else to lowercase. |
|
3886 |
See the comment in Character on what titlecase is." |
|
3887 |
||
3888 |
|newStr c bitsPerCharacter |
|
3889 |
mySize "{ Class: SmallInteger }" | |
|
3890 |
||
3891 |
mySize := self size. |
|
3892 |
newStr := self species new:mySize. |
|
3893 |
bitsPerCharacter := newStr bitsPerCharacter. |
|
3894 |
||
3895 |
1 to:mySize do:[:i | |
|
20874 | 3896 |
i == 1 ifTrue:[ |
3897 |
c := (self at:i) asTitlecase. |
|
3898 |
] ifFalse:[ |
|
3899 |
c := (self at:i) asLowercase. |
|
3900 |
]. |
|
3901 |
c bitsPerCharacter > bitsPerCharacter ifTrue:[ |
|
3902 |
newStr := c stringSpecies fromString:newStr. |
|
3903 |
bitsPerCharacter := newStr bitsPerCharacter. |
|
3904 |
]. |
|
3905 |
newStr at:i put:c |
|
8027 | 3906 |
]. |
3907 |
^ newStr |
|
3908 |
||
3909 |
" |
|
8874 | 3910 |
'helloWorld' asTitlecase |
3911 |
'HelloWorld' asTitlecase |
|
3912 |
'HELLOWORLD' asTitlecase |
|
3913 |
'helloworld' asTitlecase |
|
8027 | 3914 |
" |
9192 | 3915 |
! |
3916 |
||
3917 |
asTitlecaseFirst |
|
8023 | 3918 |
"return a version of the receiver, where the first character is converted to titlecase. |
8024 | 3919 |
Titlecase is much like uppercase for most characters, with the exception of some combined |
3920 |
(2-character glyphs), which consist of an upper- and lower-case characters. |
|
3921 |
If the first character is already titlecase, or there is no titlecasepercase for it, return the |
|
8023 | 3922 |
receiver." |
3923 |
||
8026 | 3924 |
" |
8874 | 3925 |
For example, in Unicode, character U+01F3 is LATIN SMALL LETTER DZ. |
3926 |
(Let us write this compound character using ASCII as 'dz'.) |
|
3927 |
This character uppercases to character U+01F1, LATIN CAPITAL LETTER DZ. |
|
3928 |
(Which is basically 'DZ'.) |
|
3929 |
But it titlecases to to character U+01F2, LATIN CAPITAL LETTER D WITH SMALL LETTER Z. |
|
8026 | 3930 |
(Which we can write 'Dz'.) |
3931 |
||
3932 |
character uppercase titlecase |
|
3933 |
--------- --------- --------- |
|
3934 |
dz DZ Dz |
|
3935 |
" |
|
3936 |
||
8023 | 3937 |
|newString firstChar firstCharAsTitlecase| |
3938 |
||
3939 |
firstChar := (self at:1). |
|
8025 | 3940 |
firstCharAsTitlecase := firstChar asTitlecase. |
8023 | 3941 |
firstChar == firstCharAsTitlecase ifTrue:[ ^ self]. |
8037
8cce25b1059c
care for wide chars in asXXXFirst
Claus Gittinger <cg@exept.de>
parents:
8032
diff
changeset
|
3942 |
|
8cce25b1059c
care for wide chars in asXXXFirst
Claus Gittinger <cg@exept.de>
parents:
8032
diff
changeset
|
3943 |
firstCharAsTitlecase bitsPerCharacter > self bitsPerCharacter ifTrue:[ |
20874 | 3944 |
newString := firstCharAsTitlecase stringSpecies fromString:self. |
8037
8cce25b1059c
care for wide chars in asXXXFirst
Claus Gittinger <cg@exept.de>
parents:
8032
diff
changeset
|
3945 |
] ifFalse:[ |
20874 | 3946 |
newString := self stringSpecies fromString:self. |
8037
8cce25b1059c
care for wide chars in asXXXFirst
Claus Gittinger <cg@exept.de>
parents:
8032
diff
changeset
|
3947 |
]. |
8023 | 3948 |
newString at:1 put:firstCharAsTitlecase. |
3949 |
^ newString |
|
3950 |
||
3951 |
" |
|
8874 | 3952 |
'helloWorld' asTitlecaseFirst |
3953 |
'HelloWorld' asTitlecaseFirst |
|
8023 | 3954 |
" |
9192 | 3955 |
! |
3956 |
||
3957 |
asTwoByteString |
|
3958 |
"return the receiver converted to a two-byte string. |
|
16051 | 3959 |
Will be obsolete soon - use asUnicode16String." |
9192 | 3960 |
|
3961 |
^ TwoByteString fromString:self |
|
3962 |
! |
|
3963 |
||
3964 |
asURI |
|
3965 |
"return an URI with string taken from the receiver" |
|
3966 |
||
3967 |
^ URI fromString:self |
|
3968 |
! |
|
8710 | 3969 |
|
5776 | 3970 |
asURL |
3971 |
"return an URL-object from myself." |
|
3972 |
||
3973 |
^ URL fromString:self |
|
3974 |
||
3975 |
" |
|
8874 | 3976 |
'http://www.exept.de:80/index.html' asURL host |
3977 |
'http://www.exept.de:80/index.html' asURL port |
|
9192 | 3978 |
'http://www.exept.de:80/index.html' asURL method |
3979 |
'http://www.exept.de:80/index.html' asURL path |
|
3980 |
" |
|
3981 |
! |
|
3982 |
||
18901 | 3983 |
asUUID |
3984 |
"return self as a UUID" |
|
3985 |
||
3986 |
^ UUID fromString:self |
|
3987 |
||
3988 |
" |
|
3989 |
UUID genUUID asString asUUID |
|
3990 |
'{EAB22AC0-30C1-11CF-A7EB-0000C05BAE0B}' asUUID |
|
3991 |
'{EAB22AC0-30C1-11CF-A7EB-0000C05BAE0B}' asUnicodeString asUUID |
|
3992 |
'EAB22AC0-30C1-11CF-A7EB-0000C05BAE0B' asUUID |
|
3993 |
'EAB22AC0-30C1-11CF-A7EB-0000C05BAE0B quatsch' asUUID |
|
3994 |
'quark EAB22AC0-30C1-11CF-A7EB-0000C05BAE0B' asUUID |
|
3995 |
" |
|
3996 |
||
3997 |
"Modified: / 02-08-2007 / 16:43:29 / cg" |
|
3998 |
! |
|
3999 |
||
9192 | 4000 |
asUnicode16String |
15035 | 4001 |
"return the receiver in a two-byte per character representation. |
4002 |
Normally, the internal ST/X representation should be transparent and not |
|
4003 |
of the programmer's concern; except when strings are passed to the outside world, |
|
4004 |
such as wide-string ffi calls or file contents." |
|
4005 |
||
17600 | 4006 |
|sz| |
4007 |
||
4008 |
sz := self size. |
|
4009 |
||
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
4010 |
^ (Unicode16String new:sz) |
20874 | 4011 |
replaceFrom:1 to:sz with:self startingAt:1; |
4012 |
yourself. |
|
4013 |
||
4014 |
" |
|
4015 |
'abc' asUnicode16String |
|
17600 | 4016 |
" |
4017 |
! |
|
4018 |
||
4019 |
asUnicode16StringZ |
|
4020 |
"return the receiver in a two-byte per character representation. |
|
17625 | 4021 |
Make sure that is zero terminated (last char is 16r0000). |
17600 | 4022 |
Normally, the internal ST/X representation should be transparent and not |
4023 |
of the programmer's concern; except when strings are passed to the outside world, |
|
4024 |
such as wide-string ffi calls or file contents." |
|
4025 |
||
4026 |
|sz| |
|
4027 |
||
4028 |
sz := self size. |
|
22249 | 4029 |
sz > 0 ifTrue:[ |
4030 |
(self at:sz) == (Character codePoint:0) ifTrue:[ |
|
4031 |
^ self asUnicode16String. |
|
4032 |
]. |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
4033 |
]. |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
4034 |
|
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
4035 |
^ (Unicode16String new:sz+1) |
20874 | 4036 |
replaceFrom:1 to:sz with:self startingAt:1; |
4037 |
at:sz+1 put:(Character codePoint:0); |
|
4038 |
yourself. |
|
17600 | 4039 |
|
4040 |
" |
|
22249 | 4041 |
'' asUnicode16StringZ |
20874 | 4042 |
'abc' asUnicode16StringZ |
4043 |
'abc' asUnicode16String asUnicode16StringZ |
|
17600 | 4044 |
" |
9192 | 4045 |
! |
8295
8433d819226b
Define #asUnicode16String and #asUnicode32String in CharacterArray
Stefan Vogel <sv@exept.de>
parents:
8222
diff
changeset
|
4046 |
|
8433d819226b
Define #asUnicode16String and #asUnicode32String in CharacterArray
Stefan Vogel <sv@exept.de>
parents:
8222
diff
changeset
|
4047 |
asUnicode32String |
16051 | 4048 |
"return the receiver in a four-byte per character representation. |
4049 |
Normally, the internal ST/X representation should be transparent and not |
|
4050 |
of the programmer's concern; except when strings are passed to the outside world, |
|
4051 |
such as wide-string ffi calls or file contents." |
|
4052 |
||
4053 |
^ self as:Unicode32String |
|
8295
8433d819226b
Define #asUnicode16String and #asUnicode32String in CharacterArray
Stefan Vogel <sv@exept.de>
parents:
8222
diff
changeset
|
4054 |
! |
8433d819226b
Define #asUnicode16String and #asUnicode32String in CharacterArray
Stefan Vogel <sv@exept.de>
parents:
8222
diff
changeset
|
4055 |
|
8433d819226b
Define #asUnicode16String and #asUnicode32String in CharacterArray
Stefan Vogel <sv@exept.de>
parents:
8222
diff
changeset
|
4056 |
asUnicodeString |
16051 | 4057 |
"return the receiver in a two-byte per character representation. |
4058 |
Normally, the internal ST/X representation should be transparent and not |
|
4059 |
of the programmer's concern; except when strings are passed to the outside world, |
|
4060 |
such as wide-string ffi calls or file contents." |
|
4061 |
||
4062 |
^ self asUnicode16String |
|
4063 |
" |
|
4064 |
'Hello World' asUnicodeString |
|
14141
e86d245213b0
comment/format in: #asUnicodeString
Stefan Vogel <sv@exept.de>
parents:
14137
diff
changeset
|
4065 |
" |
8295
8433d819226b
Define #asUnicode16String and #asUnicode32String in CharacterArray
Stefan Vogel <sv@exept.de>
parents:
8222
diff
changeset
|
4066 |
! |
8433d819226b
Define #asUnicode16String and #asUnicode32String in CharacterArray
Stefan Vogel <sv@exept.de>
parents:
8222
diff
changeset
|
4067 |
|
13073 | 4068 |
asUnixFilename |
4069 |
"return a UnixFilename with pathname taken from the receiver" |
|
4070 |
||
4071 |
^ UnixFilename named:self |
|
4072 |
||
4073 |
"Created: / 20-09-2010 / 21:42:55 / cg" |
|
4074 |
! |
|
4075 |
||
608 | 4076 |
asUppercase |
4077 |
"return a copy of myself in uppercase letters" |
|
4078 |
||
8003
6e690f4b79f1
asLowercase/asUppercase and friends care for 16-bit chars
Claus Gittinger <cg@exept.de>
parents:
7995
diff
changeset
|
4079 |
|newStr c bitsPerCharacter |
608 | 4080 |
mySize "{ Class: SmallInteger }" | |
4081 |
||
4082 |
mySize := self size. |
|
8954
12e701d7b5ac
allow for receiver to be empty in asLowercase and asUppercase.
Claus Gittinger <cg@exept.de>
parents:
8938
diff
changeset
|
4083 |
mySize == 0 ifTrue:[^ self]. |
608 | 4084 |
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
|
4085 |
bitsPerCharacter := newStr bitsPerCharacter. |
6e690f4b79f1
asLowercase/asUppercase and friends care for 16-bit chars
Claus Gittinger <cg@exept.de>
parents:
7995
diff
changeset
|
4086 |
|
18657 | 4087 |
"/ handle the very seldom case of a lowercase char which needs |
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
4088 |
"/ more bits in its uppercase variant |
18657 | 4089 |
"/ (there are only a few of them) |
4090 |
||
608 | 4091 |
1 to:mySize do:[:i | |
20874 | 4092 |
c := (self at:i) asUppercase. |
4093 |
c bitsPerCharacter > bitsPerCharacter ifTrue:[ |
|
4094 |
newStr := c stringSpecies fromString:newStr. |
|
4095 |
bitsPerCharacter := newStr bitsPerCharacter. |
|
4096 |
]. |
|
4097 |
newStr at:i put:c |
|
608 | 4098 |
]. |
4099 |
^ newStr |
|
4100 |
||
4101 |
" |
|
8874 | 4102 |
'helloWorld' asUppercase |
4103 |
'helloWorld' asUppercaseFirst |
|
9192 | 4104 |
(Character value:16rB5) asString asUppercase -- needs 16 bits !! |
18657 | 4105 |
(Character value:16rFF) asString asUppercase -- needs 16 bits !! |
9192 | 4106 |
" |
4107 |
! |
|
4108 |
||
4109 |
asUppercaseFirst |
|
8003
6e690f4b79f1
asLowercase/asUppercase and friends care for 16-bit chars
Claus Gittinger <cg@exept.de>
parents:
7995
diff
changeset
|
4110 |
"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
|
4111 |
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
|
4112 |
receiver." |
6e690f4b79f1
asLowercase/asUppercase and friends care for 16-bit chars
Claus Gittinger <cg@exept.de>
parents:
7995
diff
changeset
|
4113 |
|
6e690f4b79f1
asLowercase/asUppercase and friends care for 16-bit chars
Claus Gittinger <cg@exept.de>
parents:
7995
diff
changeset
|
4114 |
|newString firstChar firstCharAsUppercase| |
6e690f4b79f1
asLowercase/asUppercase and friends care for 16-bit chars
Claus Gittinger <cg@exept.de>
parents:
7995
diff
changeset
|
4115 |
|
12679 | 4116 |
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
|
4117 |
firstChar := self at:1. |
8003
6e690f4b79f1
asLowercase/asUppercase and friends care for 16-bit chars
Claus Gittinger <cg@exept.de>
parents:
7995
diff
changeset
|
4118 |
firstCharAsUppercase := firstChar asUppercase. |
6e690f4b79f1
asLowercase/asUppercase and friends care for 16-bit chars
Claus Gittinger <cg@exept.de>
parents:
7995
diff
changeset
|
4119 |
firstChar == firstCharAsUppercase ifTrue:[ ^ self]. |
6e690f4b79f1
asLowercase/asUppercase and friends care for 16-bit chars
Claus Gittinger <cg@exept.de>
parents:
7995
diff
changeset
|
4120 |
|
12679 | 4121 |
"/ 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
|
4122 |
firstCharAsUppercase bitsPerCharacter > self bitsPerCharacter ifTrue:[ |
20874 | 4123 |
newString := firstCharAsUppercase stringSpecies fromString:self. |
8037
8cce25b1059c
care for wide chars in asXXXFirst
Claus Gittinger <cg@exept.de>
parents:
8032
diff
changeset
|
4124 |
] ifFalse:[ |
20874 | 4125 |
newString := self stringSpecies fromString:self. |
8037
8cce25b1059c
care for wide chars in asXXXFirst
Claus Gittinger <cg@exept.de>
parents:
8032
diff
changeset
|
4126 |
]. |
8003
6e690f4b79f1
asLowercase/asUppercase and friends care for 16-bit chars
Claus Gittinger <cg@exept.de>
parents:
7995
diff
changeset
|
4127 |
newString at:1 put:firstCharAsUppercase. |
608 | 4128 |
^ newString |
4129 |
||
4130 |
" |
|
8874 | 4131 |
'helloWorld' asUppercase |
4132 |
'helloWorld' asUppercaseFirst |
|
4133 |
'HelloWorld' asUppercaseFirst |
|
608 | 4134 |
" |
9192 | 4135 |
! |
4136 |
||
11828 | 4137 |
asUppercaseLast |
4138 |
"return a copy of myself where the last character is |
|
4139 |
converted to uppercase." |
|
4140 |
||
4141 |
|newString sz| |
|
4142 |
||
4143 |
sz := self size. |
|
4144 |
newString := self copyFrom:1 to:sz. |
|
4145 |
sz > 0 ifTrue:[ |
|
20874 | 4146 |
newString at:sz put:(newString at:sz) asUppercase |
11828 | 4147 |
]. |
4148 |
^ newString |
|
4149 |
||
4150 |
" |
|
12177 | 4151 |
'HelloWorld' asUppercase |
4152 |
'HelloWorld' asUppercaseLast |
|
11828 | 4153 |
" |
4154 |
! |
|
4155 |
||
17021 | 4156 |
asUtcTimestamp |
4157 |
"convert the receiver into an UtcTimestamp (Time is interpreted in the UTC timezone). |
|
4158 |
Notice, that errors may occur during the read, |
|
4159 |
so you better setup some exception handler when using this method." |
|
4160 |
||
21030 | 4161 |
^ UtcTimestamp readFrom:self |
17021 | 4162 |
|
4163 |
" |
|
4164 |
'2014-11-10 21:30:22.444' asUtcTimestamp |
|
4165 |
" |
|
4166 |
! |
|
4167 |
||
23121 | 4168 |
asVersionNumberCollection |
4169 |
"Convert a string like 1.2.3a to an Array of numbers (or string, if not a number). |
|
4170 |
Remove zeroes from the end." |
|
4171 |
||
4172 |
^ (self asCollectionOfSubstringsSeparatedByAny:'.-_') asVersionNumberCollection. |
|
4173 |
||
4174 |
" |
|
4175 |
'1' asVersionNumberCollection. |
|
4176 |
'1.1' asVersionNumberCollection. |
|
4177 |
'1.1a' asVersionNumberCollection. |
|
4178 |
'1.1.0' asVersionNumberCollection. |
|
4179 |
'01.01.0' asVersionNumberCollection. |
|
4180 |
'expecco_18_1_0' asVersionNumberCollection. |
|
4181 |
" |
|
4182 |
||
4183 |
"Created: / 20-06-2018 / 11:23:53 / Stefan Vogel" |
|
4184 |
"Modified: / 20-06-2018 / 19:24:36 / Stefan Vogel" |
|
4185 |
! |
|
4186 |
||
11062 | 4187 |
literalArrayEncoding |
4188 |
"encode myself as an array literal, from which a copy of the receiver |
|
4189 |
can be reconstructed with #decodeAsLiteralArray." |
|
4190 |
||
4191 |
^ self |
|
4192 |
||
4193 |
"Modified: 1.9.1995 / 02:25:45 / claus" |
|
4194 |
"Modified: 22.4.1996 / 13:00:50 / cg" |
|
4195 |
! |
|
4196 |
||
9192 | 4197 |
string |
4198 |
"return the receiver - for ST-80 compatibility" |
|
4199 |
||
4200 |
^ self |
|
4201 |
||
4202 |
"Modified: 27.4.1996 / 13:29:30 / cg" |
|
4203 |
! ! |
|
4204 |
||
4205 |
!CharacterArray methodsFor:'copying'! |
|
4206 |
||
22528 | 4207 |
, aStringOrCharacterOrAnyOther |
2720
98c21d9a07c0
allow mixed strings to be concatenated using comma
Claus Gittinger <cg@exept.de>
parents:
2692
diff
changeset
|
4208 |
"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
|
4209 |
This is nonStandard, but convenient" |
2f14bbd792a9
, now accepts a character-argument
Claus Gittinger <cg@exept.de>
parents:
1028
diff
changeset
|
4210 |
|
22528 | 4211 |
|myWidth otherWidth stringifiedArg| |
4212 |
||
4213 |
aStringOrCharacterOrAnyOther isCharacter ifTrue:[ |
|
4214 |
^ self copyWith:aStringOrCharacterOrAnyOther |
|
4215 |
]. |
|
4216 |
aStringOrCharacterOrAnyOther isText ifTrue:[ |
|
4217 |
^ aStringOrCharacterOrAnyOther concatenateFromString:self |
|
4218 |
]. |
|
4219 |
stringifiedArg := aStringOrCharacterOrAnyOther asString. |
|
4220 |
stringifiedArg isString ifTrue:[ |
|
4221 |
(otherWidth := stringifiedArg bitsPerCharacter) ~~ (myWidth := self bitsPerCharacter) ifTrue:[ |
|
20874 | 4222 |
otherWidth > myWidth ifTrue:[ |
22528 | 4223 |
^ (stringifiedArg species fromString:self) , stringifiedArg |
20874 | 4224 |
]. |
22528 | 4225 |
^ self , (self species fromString:stringifiedArg) |
20874 | 4226 |
]. |
2720
98c21d9a07c0
allow mixed strings to be concatenated using comma
Claus Gittinger <cg@exept.de>
parents:
2692
diff
changeset
|
4227 |
]. |
22528 | 4228 |
^ super , stringifiedArg |
1050
2f14bbd792a9
, now accepts a character-argument
Claus Gittinger <cg@exept.de>
parents:
1028
diff
changeset
|
4229 |
|
2f14bbd792a9
, now accepts a character-argument
Claus Gittinger <cg@exept.de>
parents:
1028
diff
changeset
|
4230 |
" |
8874 | 4231 |
'hello' , $1 |
4232 |
'hello' , '1' |
|
4233 |
'hello' , (' world' asText allBold) |
|
4234 |
'hello' , (JISEncodedString fromString:' world') |
|
2720
98c21d9a07c0
allow mixed strings to be concatenated using comma
Claus Gittinger <cg@exept.de>
parents:
2692
diff
changeset
|
4235 |
(JISEncodedString fromString:'hello') , ' world' |
1411 | 4236 |
|
1422 | 4237 |
Transcript showCR: |
20874 | 4238 |
(Text string:'hello' emphasis:#italic) , (Text string:' world' emphasis:#bold) |
22528 | 4239 |
|
4240 |
'hello',123,'world' |
|
4241 |
'hello' allBold,123,'world' |
|
4242 |
'hello',123,'world' allBold |
|
1050
2f14bbd792a9
, now accepts a character-argument
Claus Gittinger <cg@exept.de>
parents:
1028
diff
changeset
|
4243 |
" |
2f14bbd792a9
, now accepts a character-argument
Claus Gittinger <cg@exept.de>
parents:
1028
diff
changeset
|
4244 |
|
2720
98c21d9a07c0
allow mixed strings to be concatenated using comma
Claus Gittinger <cg@exept.de>
parents:
2692
diff
changeset
|
4245 |
"Modified: 28.6.1997 / 00:13:17 / cg" |
9192 | 4246 |
! |
4247 |
||
4248 |
,, aString |
|
7020 | 4249 |
"concatenate with a newLine in between" |
6965 | 4250 |
|
4251 |
^ (self copyWith:Character cr) , aString |
|
4252 |
||
4253 |
" |
|
4254 |
hello ,, world |
|
7020 | 4255 |
'hello' ,, 'world' |
6965 | 4256 |
" |
4257 |
! |
|
4258 |
||
11329 | 4259 |
chopTo:maxLen |
19464 | 4260 |
"if the receiver's size is less or equal to maxLen, return it. |
11329 | 4261 |
Otherwise, return a copy of the receiver, where some characters |
4262 |
in the middle have been removed for a total string length |
|
4263 |
of maxLen." |
|
4264 |
||
4265 |
|sz n1 n2| |
|
4266 |
||
4267 |
(sz := self size) <= maxLen ifTrue:[ ^ self ]. |
|
4268 |
||
4269 |
n1 := n2 := maxLen // 2. |
|
4270 |
maxLen odd ifTrue:[ |
|
20874 | 4271 |
n2 := n1 + 1 |
11329 | 4272 |
]. |
4273 |
^ (self copyFrom:1 to:n1) , (self copyFrom:sz - n2 + 1) |
|
4274 |
||
4275 |
" |
|
4276 |
'12345678901234' chopTo:15 |
|
4277 |
'123456789012345' chopTo:15 |
|
4278 |
'1234567890123456' chopTo:15 |
|
4279 |
'aShortString' chopTo:15 |
|
4280 |
'aVeryLongNameForAStringThatShouldBeShortened' chopTo:15 |
|
4281 |
" |
|
4282 |
||
4283 |
"Modified: / 24-10-2006 / 12:32:01 / cg" |
|
4284 |
! |
|
4285 |
||
4286 |
contractAtBeginningTo:maxLen |
|
19464 | 4287 |
"if the receiver's size is less or equal to maxLen, return it. |
11329 | 4288 |
Otherwise, return a copy of the receiver, where some characters |
4289 |
at the beginning have been replaced by '...' for a total string length |
|
4290 |
of maxLen. Can be used to abbreviate long entries in tables." |
|
4291 |
||
4292 |
|sz| |
|
4293 |
||
4294 |
(sz := self size) <= maxLen ifTrue:[ ^ self ]. |
|
4295 |
||
4296 |
^ '...' , (self copyFrom:(sz - (maxLen - 4))) |
|
4297 |
||
4298 |
" |
|
4299 |
'12345678901234' contractAtBeginningTo:15 |
|
4300 |
'123456789012345' contractAtBeginningTo:15 |
|
4301 |
'1234567890123456' contractAtBeginningTo:15 |
|
4302 |
'aShortString' contractAtBeginningTo:15 |
|
4303 |
'aVeryLongNameForAStringThatShouldBeShortened' contractAtBeginningTo:15 |
|
4304 |
" |
|
4305 |
||
4306 |
"Modified: / 24-10-2006 / 12:32:13 / cg" |
|
4307 |
! |
|
4308 |
||
4309 |
contractAtEndTo:maxLen |
|
19464 | 4310 |
"if the receiver's size is less or equal to maxLen, return it. |
11329 | 4311 |
Otherwise, return a copy of the receiver, where some characters |
4312 |
at the end have been replaced by '...' for a total string length |
|
4313 |
of maxLen. Can be used to abbreviate long entries in tables." |
|
4314 |
||
21559 | 4315 |
(self size <= maxLen) ifTrue:[ ^ self ]. |
11329 | 4316 |
|
4317 |
^ (self copyTo:maxLen-3),'...' |
|
4318 |
||
4319 |
" |
|
4320 |
'12345678901234' contractAtEndTo:15 |
|
4321 |
'123456789012345' contractAtEndTo:15 |
|
4322 |
'1234567890123456' contractAtEndTo:15 |
|
4323 |
'aShortString' contractAtEndTo:15 |
|
4324 |
'aVeryLongNameForAStringThatShouldBeShortened' contractAtEndTo:15 |
|
4325 |
" |
|
4326 |
||
4327 |
"Modified: / 24-10-2006 / 12:32:26 / cg" |
|
21559 | 4328 |
"Modified: / 23-02-2017 / 21:32:21 / mawalch" |
11329 | 4329 |
! |
4330 |
||
4331 |
contractLeftTo:maxLen |
|
19464 | 4332 |
"if the receiver's size is less or equal to maxLen, return it. |
11329 | 4333 |
Otherwise, return a copy of the receiver, where some characters |
4334 |
near the first quarter have been replaced by '...' for a total string length |
|
12177 | 4335 |
of maxLen. |
4336 |
Very similar to contractTo:, but better to abbreviate long filename entries, |
|
11329 | 4337 |
where the right part is of more use than the left." |
4338 |
||
4339 |
|sz "{ SmallInteger }" |
|
4340 |
halfSize quarterSize "{ SmallInteger }" |
|
4341 |
leftEnd rightEnd rightStart| |
|
4342 |
||
4343 |
(sz := self size) <= maxLen ifTrue:[ ^ self ]. |
|
4344 |
||
4345 |
halfSize := maxLen // 2. |
|
4346 |
quarterSize := maxLen // 4. |
|
4347 |
leftEnd := quarterSize-1. |
|
4348 |
rightEnd := maxLen - leftEnd - 3. |
|
4349 |
rightStart := sz - rightEnd + 1. |
|
4350 |
^ (self copyTo:leftEnd),'...',(self copyFrom:rightStart) |
|
4351 |
||
4352 |
" |
|
12177 | 4353 |
'12345678901234' contractLeftTo:15 |
4354 |
'123456789012345' contractLeftTo:15 |
|
4355 |
'1234567890123456' contractLeftTo:15 |
|
4356 |
'aShortString' contractLeftTo:15 |
|
4357 |
'aVeryLongNameForAStringThatShouldBeShortened' contractLeftTo:15 |
|
11329 | 4358 |
'C:\Dokumente und Einstellungen\cg\work\bosch\dapas\hw_schnittstellen\DAPAS__HpibDLL.st' contractLeftTo:40 |
4359 |
" |
|
4360 |
! |
|
4361 |
||
4362 |
contractTo:maxLen |
|
19464 | 4363 |
"if the receiver's size is less or equal to maxLen, return it. |
11329 | 4364 |
Otherwise, return a copy of the receiver, where some characters |
4365 |
in the middle have been replaced by '...' for a total string length |
|
4366 |
of maxLen. Can be used to abbreviate long entries in tables." |
|
4367 |
||
13362 | 4368 |
|sz "{ SmallInteger }" leftSize rightSize| |
11329 | 4369 |
|
4370 |
(sz := self size) <= maxLen ifTrue:[ ^ self ]. |
|
4371 |
||
13362 | 4372 |
rightSize := maxLen // 2. |
4373 |
leftSize := maxLen - rightSize. |
|
13822 | 4374 |
leftSize := leftSize - 2. |
4375 |
rightSize := rightSize - 1. |
|
13362 | 4376 |
^ (self copyTo:leftSize),'...',(self copyFrom:(sz+1-rightSize)) |
4377 |
||
4378 |
" |
|
13882
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
4379 |
'12345678901234' contractTo:15 |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
4380 |
'123456789012345' contractTo:15 |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
4381 |
'1234567890123456' contractTo:15 |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
4382 |
'12345678901234567' contractTo:15 |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
4383 |
'123456789012345678' contractTo:15 |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
4384 |
'aShortString' contractTo:15 |
11329 | 4385 |
'aVeryLongNameForAStringThatShouldBeShortened' contractTo:15 |
4386 |
'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
|
4387 |
('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
|
4388 |
('1234567890123456789012345678901234567' contractTo:29) size |
13362 | 4389 |
" |
4390 |
||
13822 | 4391 |
"Modified (comment): / 24-11-2011 / 19:17:46 / cg" |
11329 | 4392 |
! |
4393 |
||
23061 | 4394 |
copyBetween:string1 and:string2 caseSensitive:caseSensitive |
4395 |
"return the substring between two matching sub-strings. |
|
4396 |
Returns nil, if not both subStrings are present in the receiver." |
|
4397 |
||
4398 |
|idx1 leftIndex rightIndex| |
|
4399 |
||
4400 |
idx1 := self indexOfString:string1 caseSensitive:caseSensitive. |
|
4401 |
idx1 ~~ 0 ifTrue:[ |
|
4402 |
leftIndex := (idx1 + string1 size). |
|
4403 |
rightIndex := self indexOfString:string2 caseSensitive:caseSensitive startingAt:leftIndex. |
|
4404 |
rightIndex ~~ 0 ifTrue:[ |
|
4405 |
^ self copyFrom:leftIndex to:rightIndex-1 |
|
4406 |
] |
|
4407 |
]. |
|
4408 |
^ nil |
|
4409 |
||
4410 |
" |
|
4411 |
'hello funny world' copyBetween:'hello' and:'world' caseSensitive:true -> ' funny ' |
|
4412 |
'helloworld' copyBetween:'hello' and:'world' caseSensitive:true -> '' |
|
4413 |
'helloorld' copyBetween:'hello' and:'world' caseSensitive:true -> nil |
|
4414 |
||
4415 |
'hello funny World' copyBetween:'hello' and:'world' caseSensitive:true -> nil |
|
4416 |
'helloWorld' copyBetween:'hello' and:'world' caseSensitive:true -> nil |
|
4417 |
||
4418 |
'hello funny World' copyBetween:'hello' and:'world' caseSensitive:false -> ' funny ' |
|
4419 |
'helloWorld' copyBetween:'hello' and:'world' caseSensitive:false -> '' |
|
4420 |
" |
|
4421 |
||
4422 |
"Created: / 08-06-2018 / 14:50:17 / Claus Gittinger" |
|
4423 |
! |
|
4424 |
||
16202 | 4425 |
copyReplaceAll:oldElement with:newElement |
4426 |
"return a copy of the receiver as a string, where all elements equal to oldElement |
|
4427 |
have been replaced by newElement." |
|
4428 |
||
4429 |
"/ ANSI seems to allow a sequence to be replaced by another sequence, |
|
4430 |
"/ whereas the old ST80 meant replace all occurrences... - sigh. |
|
22231 | 4431 |
oldElement isString ifTrue:[ |
4432 |
newElement isString ifTrue:[ |
|
20874 | 4433 |
^ self copyReplaceString:oldElement withString:newElement. |
4434 |
]. |
|
4435 |
self halt:'check if this is legal'. |
|
16202 | 4436 |
]. |
22231 | 4437 |
newElement isString ifTrue:[ |
20874 | 4438 |
self halt:'check if this is legal'. |
16202 | 4439 |
]. |
16212 | 4440 |
^ super copyReplaceAll:oldElement with:newElement |
16202 | 4441 |
! |
4442 |
||
8347 | 4443 |
copyReplaceString:subString withString:newString |
4444 |
"return a copy of the receiver, with all sequences of subString replaced |
|
4445 |
by newString (i.e. slice in the newString in place of the oldString)." |
|
4446 |
||
4447 |
|tmpStream idx idx1| |
|
4448 |
||
15523 | 4449 |
tmpStream := self species writeStream. |
8347 | 4450 |
idx := 1. |
4451 |
[idx ~~ 0] whileTrue:[ |
|
20874 | 4452 |
idx1 := idx. |
4453 |
idx := self indexOfSubCollection:subString startingAt:idx. |
|
4454 |
idx ~~ 0 ifTrue:[ |
|
4455 |
tmpStream nextPutAll:(self copyFrom:idx1 to:idx-1). |
|
4456 |
tmpStream nextPutAll:newString. |
|
4457 |
idx := idx + subString size |
|
4458 |
] |
|
8347 | 4459 |
]. |
4460 |
tmpStream nextPutAll:(self copyFrom:idx1). |
|
4461 |
^ tmpStream contents |
|
4462 |
||
4463 |
" |
|
9306 | 4464 |
'12345678901234567890' copyReplaceString:'123' withString:'OneTwoThree' |
4465 |
'12345678901234567890' copyReplaceString:'123' withString:'*' |
|
4466 |
'12345678901234567890' copyReplaceString:'234' withString:'foo' |
|
22231 | 4467 |
'12345678901234567890' asUnicode16String copyReplaceString:'234' asUnicode16String withString:'foo' |
4468 |
'12345678901234567890' asUnicode16String copyReplaceString:'234' withString:'foo' |
|
4469 |
'12345678901234567890' asUnicode16String copyReplaceString:'234' withString:'foo' asUnicode16String |
|
9306 | 4470 |
|
4471 |
('a string with spaces' copyReplaceAll:$ withAll:' foo ') |
|
20874 | 4472 |
copyReplaceString:'foo' withString:'bar' |
8347 | 4473 |
" |
4474 |
||
4475 |
"Modified: / 31-05-1999 / 12:33:59 / cg" |
|
4476 |
"Created: / 12-05-2004 / 12:00:00 / cg" |
|
4477 |
! |
|
4478 |
||
8040
f10cca439066
copyWith for mixed-width string/character
Claus Gittinger <cg@exept.de>
parents:
8037
diff
changeset
|
4479 |
copyWith:aCharacter |
19464 | 4480 |
"return a new string containing the receiver's characters |
8874 | 4481 |
and the single new character, aCharacter. |
8040
f10cca439066
copyWith for mixed-width string/character
Claus Gittinger <cg@exept.de>
parents:
8037
diff
changeset
|
4482 |
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
|
4483 |
as argument, but equivalent to copy-and-addLast. |
8874 | 4484 |
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
|
4485 |
(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
|
4486 |
|
f10cca439066
copyWith for mixed-width string/character
Claus Gittinger <cg@exept.de>
parents:
8037
diff
changeset
|
4487 |
|sz newString| |
f10cca439066
copyWith for mixed-width string/character
Claus Gittinger <cg@exept.de>
parents:
8037
diff
changeset
|
4488 |
|
f10cca439066
copyWith for mixed-width string/character
Claus Gittinger <cg@exept.de>
parents:
8037
diff
changeset
|
4489 |
aCharacter bitsPerCharacter > self bitsPerCharacter ifTrue:[ |
20874 | 4490 |
sz := self size. |
4491 |
newString := aCharacter stringSpecies new:sz + 1. |
|
4492 |
newString replaceFrom:1 to:sz with:self startingAt:1. |
|
4493 |
newString at:sz+1 put:aCharacter. |
|
4494 |
^ newString. |
|
8040
f10cca439066
copyWith for mixed-width string/character
Claus Gittinger <cg@exept.de>
parents:
8037
diff
changeset
|
4495 |
]. |
f10cca439066
copyWith for mixed-width string/character
Claus Gittinger <cg@exept.de>
parents:
8037
diff
changeset
|
4496 |
^ super copyWith:aCharacter |
17241 | 4497 |
! |
4498 |
||
4499 |
restAfter:keyword withoutSeparators:strip |
|
4500 |
"compare the left of the receiver with keyword, |
|
4501 |
if it matches return the right. |
|
4502 |
Finally, if strip is true, remove whiteSpace. |
|
4503 |
This method is used to match and extract lines of the form: |
|
20874 | 4504 |
something: rest |
17241 | 4505 |
where we are interested in rest, but only if the receiver string |
4506 |
begins with something. |
|
4507 |
||
4508 |
You may wonder why such a specialized method exists here |
|
4509 |
- this is so common when processing mailboxes, |
|
4510 |
rcs files, nntp/pop3 responses, that is was considered worth |
|
4511 |
a special method here to avoid having the code below a hundred |
|
4512 |
times in variuos places." |
|
4513 |
||
4514 |
|rest| |
|
4515 |
||
4516 |
(self startsWith:keyword) ifTrue:[ |
|
20874 | 4517 |
rest := self copyFrom:(keyword size + 1). |
4518 |
strip ifTrue:[ |
|
4519 |
rest := rest withoutSeparators |
|
4520 |
]. |
|
4521 |
^ rest |
|
17241 | 4522 |
]. |
4523 |
^ nil |
|
4524 |
||
4525 |
" |
|
4526 |
'foo: hello world' restAfter:'foo:' withoutSeparators:true |
|
4527 |
'funny: something' restAfter:'foo:' withoutSeparators:true |
|
4528 |
||
4529 |
'foo: hello world ' restAfter:'foo:' withoutSeparators:true |
|
4530 |
'foo: hello world ' restAfter:'foo:' withoutSeparators:false |
|
4531 |
" |
|
4532 |
||
4533 |
"Created: 25.11.1995 / 11:04:18 / cg" |
|
4534 |
! |
|
4535 |
||
4536 |
splitAtString:subString withoutSeparators:strip |
|
4537 |
"If the receiver is of the form: |
|
20874 | 4538 |
<left><subString><right> |
17241 | 4539 |
return a collection containing left and right only. |
4540 |
If strip is true, remove whiteSpace in the returned substrings." |
|
4541 |
||
4542 |
|idx left right| |
|
4543 |
||
4544 |
(idx := self indexOfSubCollection:subString) ~~ 0 ifTrue:[ |
|
20874 | 4545 |
left := self copyTo:(idx - 1). |
4546 |
right := self copyFrom:(idx + subString size). |
|
4547 |
strip ifTrue:[ |
|
4548 |
left := left withoutSeparators. |
|
4549 |
right := right withoutSeparators. |
|
4550 |
]. |
|
4551 |
^ StringCollection with:left with:right |
|
17241 | 4552 |
]. |
23001 | 4553 |
self proceedableError:'substring not present in receiver'. |
17241 | 4554 |
^ self |
4555 |
||
4556 |
" |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
4557 |
'hello -> world' splitAtString:'->' withoutSeparators:false |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
4558 |
'hello -> world' splitAtString:'->' withoutSeparators:true |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
4559 |
'hello -> ' splitAtString:'->' withoutSeparators:true |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
4560 |
'hello > error' splitAtString:'->' withoutSeparators:true |
17241 | 4561 |
" |
4562 |
||
23001 | 4563 |
"Created: / 25-11-1995 / 11:04:18 / cg" |
4564 |
"Modified: / 24-05-2018 / 14:55:52 / Claus Gittinger" |
|
608 | 4565 |
! ! |
4566 |
||
4567 |
!CharacterArray methodsFor:'displaying'! |
|
4568 |
||
2762 | 4569 |
displayOn:aGC x:x y:y from:start to:stop |
4570 |
"display the receiver on a GC" |
|
4571 |
||
12171 | 4572 |
self displayOn:aGC x:x y:y from:start to:stop opaque:false |
4573 |
||
4574 |
"Modified: 12.5.1996 / 12:49:33 / cg" |
|
4575 |
! |
|
4576 |
||
4577 |
displayOn:aGC x:x y:y from:start to:stop opaque:opaque |
|
4578 |
"display the receiver on a GC" |
|
4579 |
||
2762 | 4580 |
"q&d hack" |
4581 |
||
12177 | 4582 |
(start == 1 and:[stop == self size]) ifTrue:[ |
20874 | 4583 |
self displayOn:aGC x:x y:y opaque:opaque. |
12177 | 4584 |
] ifFalse:[ |
20874 | 4585 |
(self copyFrom:start to:stop) displayOn:aGC x:x y:y opaque:opaque. |
12177 | 4586 |
]. |
2762 | 4587 |
! |
4588 |
||
7082 | 4589 |
displayOn:aGc x:x y:y opaque:opaque |
2762 | 4590 |
"display the receiver in a graphicsContext - this method allows |
4591 |
strings to be used like DisplayObjects." |
|
4592 |
||
4593 |
|s| |
|
4594 |
||
4595 |
s := self string. |
|
7082 | 4596 |
opaque ifTrue:[ |
20874 | 4597 |
aGc displayOpaqueString:s x:x y:y. |
2762 | 4598 |
] ifFalse:[ |
20874 | 4599 |
aGc displayString:s x:x y:y. |
2762 | 4600 |
]. |
1381 | 4601 |
|
4602 |
"Modified: 11.5.1996 / 14:42:48 / cg" |
|
4603 |
! |
|
4604 |
||
2762 | 4605 |
displayOpaqueOn:aGC x:x y:y from:start to:stop |
4606 |
"display the receiver on a GC" |
|
4607 |
||
12171 | 4608 |
self displayOn:aGC x:x y:y from:start to:stop opaque:true |
608 | 4609 |
! ! |
4610 |
||
1375
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
4611 |
!CharacterArray methodsFor:'emphasis'! |
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
4612 |
|
11632
b82e9f3cabd1
new: #actionForAll: #emphasisAtPoint:on:
Stefan Vogel <sv@exept.de>
parents:
11630
diff
changeset
|
4613 |
actionForAll:aBlock |
12326
c67f12213ddb
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
12319
diff
changeset
|
4614 |
"change the action block of all characters. |
c67f12213ddb
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
12319
diff
changeset
|
4615 |
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
|
4616 |
|
b82e9f3cabd1
new: #actionForAll: #emphasisAtPoint:on:
Stefan Vogel <sv@exept.de>
parents:
11630
diff
changeset
|
4617 |
^ self asText actionForAll:aBlock |
b82e9f3cabd1
new: #actionForAll: #emphasisAtPoint:on:
Stefan Vogel <sv@exept.de>
parents:
11630
diff
changeset
|
4618 |
! |
b82e9f3cabd1
new: #actionForAll: #emphasisAtPoint:on:
Stefan Vogel <sv@exept.de>
parents:
11630
diff
changeset
|
4619 |
|
5732 | 4620 |
allBold |
12326
c67f12213ddb
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
12319
diff
changeset
|
4621 |
"return a text object representing the receiver, but all boldified" |
5732 | 4622 |
|
8874 | 4623 |
"this test allows for small non-gui apps to be built without libbasic2 (where Text is)" |
6187 | 4624 |
Text isNil ifTrue:[^ self]. |
5732 | 4625 |
^ self asText allBold |
4626 |
||
4627 |
" |
|
4628 |
Transcript showCR:'hello' asText allBold |
|
4629 |
Transcript showCR:'hello' allBold |
|
4630 |
" |
|
4631 |
! |
|
4632 |
||
5898
7bc4f1593b4e
allItalic now understood by CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5848
diff
changeset
|
4633 |
allItalic |
12326
c67f12213ddb
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
12319
diff
changeset
|
4634 |
"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
|
4635 |
|
8874 | 4636 |
"this test allows for small non-gui apps to be built without libbasic2 (where Text is)" |
6187 | 4637 |
Text isNil ifTrue:[^ self]. |
5898
7bc4f1593b4e
allItalic now understood by CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5848
diff
changeset
|
4638 |
^ self asText allItalic |
7bc4f1593b4e
allItalic now understood by CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5848
diff
changeset
|
4639 |
|
7bc4f1593b4e
allItalic now understood by CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5848
diff
changeset
|
4640 |
" |
7bc4f1593b4e
allItalic now understood by CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5848
diff
changeset
|
4641 |
Transcript showCR:'hello' asText allItalic |
7bc4f1593b4e
allItalic now understood by CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5848
diff
changeset
|
4642 |
Transcript showCR:'hello' allItalic |
7bc4f1593b4e
allItalic now understood by CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5848
diff
changeset
|
4643 |
" |
7bc4f1593b4e
allItalic now understood by CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5848
diff
changeset
|
4644 |
! |
7bc4f1593b4e
allItalic now understood by CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5848
diff
changeset
|
4645 |
|
11345 | 4646 |
allStrikedOut |
12326
c67f12213ddb
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
12319
diff
changeset
|
4647 |
"return a text object representing the receiver, but all in strikeout" |
11345 | 4648 |
|
4649 |
"this test allows for small non-gui apps to be built without libbasic2 (where Text is)" |
|
4650 |
Text isNil ifTrue:[^ self]. |
|
4651 |
^ self asText allStrikedOut |
|
4652 |
||
4653 |
" |
|
4654 |
Transcript showCR:'hello' asText allStrikedOut |
|
4655 |
Transcript showCR:'hello' allStrikedOut |
|
4656 |
" |
|
4657 |
! |
|
4658 |
||
10862 | 4659 |
allUnderlined |
12326
c67f12213ddb
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
12319
diff
changeset
|
4660 |
"return a text object representing the receiver, but all with underline" |
10862 | 4661 |
|
4662 |
"this test allows for small non-gui apps to be built without libbasic2 (where Text is)" |
|
4663 |
Text isNil ifTrue:[^ self]. |
|
4664 |
^ self asText allUnderlined |
|
4665 |
||
4666 |
" |
|
4667 |
Transcript showCR:'hello' asText allUnderlined |
|
4668 |
Transcript showCR:'hello' allUnderlined |
|
4669 |
" |
|
4670 |
! |
|
4671 |
||
19774 | 4672 |
asActionLinkTo:aBlock |
4673 |
"change the action block of all characters and colorize as a link. |
|
4674 |
Some widgets use this like a href if clicked onto the text |
|
4675 |
(for example, the system-browser's info at the bottom is such a widget)." |
|
4676 |
||
4677 |
"/ the blue may become a user-setting some time later.. |
|
4678 |
^ (self actionForAll:aBlock) withColor:(Color blue) |
|
4679 |
! |
|
4680 |
||
6228 | 4681 |
colorizeAllWith:aColor |
12326
c67f12213ddb
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
12319
diff
changeset
|
4682 |
"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
|
4683 |
|
c67f12213ddb
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
12319
diff
changeset
|
4684 |
"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
|
4685 |
Text isNil ifTrue:[^ self]. |
6228 | 4686 |
^ self asText colorizeAllWith:aColor |
4687 |
||
4688 |
" |
|
4689 |
Transcript showCR:('hello' colorizeAllWith:Color red) |
|
4690 |
Transcript showCR:('world' colorizeAllWith:Color green darkened) |
|
4691 |
" |
|
4692 |
! |
|
4693 |
||
12330
f9b57b0b8a8b
added: #colorizeAllWith:on:
Claus Gittinger <cg@exept.de>
parents:
12326
diff
changeset
|
4694 |
colorizeAllWith:fgColor on:bgColor |
f9b57b0b8a8b
added: #colorizeAllWith:on:
Claus Gittinger <cg@exept.de>
parents:
12326
diff
changeset
|
4695 |
"return a text object representing the receiver, but all colorized with |
f9b57b0b8a8b
added: #colorizeAllWith:on:
Claus Gittinger <cg@exept.de>
parents:
12326
diff
changeset
|
4696 |
both fg and background color" |
f9b57b0b8a8b
added: #colorizeAllWith:on:
Claus Gittinger <cg@exept.de>
parents:
12326
diff
changeset
|
4697 |
|
f9b57b0b8a8b
added: #colorizeAllWith:on:
Claus Gittinger <cg@exept.de>
parents:
12326
diff
changeset
|
4698 |
"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
|
4699 |
Text isNil ifTrue:[^ self]. |
f9b57b0b8a8b
added: #colorizeAllWith:on:
Claus Gittinger <cg@exept.de>
parents:
12326
diff
changeset
|
4700 |
^ self asText colorizeAllWith:fgColor on:bgColor |
f9b57b0b8a8b
added: #colorizeAllWith:on:
Claus Gittinger <cg@exept.de>
parents:
12326
diff
changeset
|
4701 |
|
f9b57b0b8a8b
added: #colorizeAllWith:on:
Claus Gittinger <cg@exept.de>
parents:
12326
diff
changeset
|
4702 |
" |
f9b57b0b8a8b
added: #colorizeAllWith:on:
Claus Gittinger <cg@exept.de>
parents:
12326
diff
changeset
|
4703 |
Transcript showCR:('hello' colorizeAllWith:Color red on:Color yellow) |
f9b57b0b8a8b
added: #colorizeAllWith:on:
Claus Gittinger <cg@exept.de>
parents:
12326
diff
changeset
|
4704 |
Transcript showCR:('world' colorizeAllWith:Color red) |
f9b57b0b8a8b
added: #colorizeAllWith:on:
Claus Gittinger <cg@exept.de>
parents:
12326
diff
changeset
|
4705 |
" |
f9b57b0b8a8b
added: #colorizeAllWith:on:
Claus Gittinger <cg@exept.de>
parents:
12326
diff
changeset
|
4706 |
! |
f9b57b0b8a8b
added: #colorizeAllWith:on:
Claus Gittinger <cg@exept.de>
parents:
12326
diff
changeset
|
4707 |
|
1396
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4708 |
emphasis |
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4709 |
"return the emphasis. |
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4710 |
Since characterArrays do not hold any emphasis information, |
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4711 |
nil (no emphasis) is returned here." |
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4712 |
|
23060 | 4713 |
^ RunArray new:(self size) withAll:nil |
4714 |
||
4715 |
"Created: / 14-05-1996 / 13:58:58 / cg" |
|
4716 |
"Modified (format): / 08-06-2018 / 12:01:59 / Claus Gittinger" |
|
1396
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4717 |
! |
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4718 |
|
6655 | 4719 |
emphasis:emphasisCollection |
4720 |
^ self asText emphasis:emphasisCollection |
|
4721 |
||
4722 |
" |
|
4723 |
Transcript showCR:('hello' emphasis:#(bold bold bold bold bold)) |
|
4724 |
" |
|
4725 |
! |
|
4726 |
||
1375
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
4727 |
emphasisAt:characterIndex |
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
4728 |
"return the emphasis at some index. |
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
4729 |
Since characterArrays do not hold any emphasis information, |
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
4730 |
nil (no emphasis) is returned here." |
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
4731 |
|
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
4732 |
^ nil |
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
4733 |
|
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
4734 |
"Created: 11.5.1996 / 14:13:27 / cg" |
1396
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4735 |
! |
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4736 |
|
11632
b82e9f3cabd1
new: #actionForAll: #emphasisAtPoint:on:
Stefan Vogel <sv@exept.de>
parents:
11630
diff
changeset
|
4737 |
emphasisAtPoint:aPoint on:aGC |
b82e9f3cabd1
new: #actionForAll: #emphasisAtPoint:on:
Stefan Vogel <sv@exept.de>
parents:
11630
diff
changeset
|
4738 |
"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
|
4739 |
|
b82e9f3cabd1
new: #actionForAll: #emphasisAtPoint:on:
Stefan Vogel <sv@exept.de>
parents:
11630
diff
changeset
|
4740 |
^ nil |
b82e9f3cabd1
new: #actionForAll: #emphasisAtPoint:on:
Stefan Vogel <sv@exept.de>
parents:
11630
diff
changeset
|
4741 |
! |
b82e9f3cabd1
new: #actionForAll: #emphasisAtPoint:on:
Stefan Vogel <sv@exept.de>
parents:
11630
diff
changeset
|
4742 |
|
1396
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4743 |
emphasisCollection |
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4744 |
"return the emphasis. |
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4745 |
Since characterArrays do not hold any emphasis information, |
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4746 |
nil (no emphasis) is returned here." |
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4747 |
|
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4748 |
^ RunArray new:(self size) |
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4749 |
|
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4750 |
"Created: 14.5.1996 / 13:58:58 / cg" |
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
4751 |
"Modified: 14.5.1996 / 15:02:29 / cg" |
3404 | 4752 |
! |
4753 |
||
11919 | 4754 |
emphasiseFrom:start to:stop with:newEmphasis |
4755 |
"set to the emphasis within some range. return the receiver" |
|
4756 |
||
4757 |
^ self asText emphasiseFrom:start to:stop with:newEmphasis |
|
4758 |
||
4759 |
" |
|
12177 | 4760 |
'hello' emphasiseFrom:2 with:#italic |
11919 | 4761 |
|
4762 |
Transcript showCR:('hello' emphasiseFrom:2 with:#italic) |
|
4763 |
" |
|
4764 |
! |
|
4765 |
||
4766 |
emphasiseFrom:start with:newEmphasis |
|
4767 |
"set to the emphasis within some range. return the receiver" |
|
4768 |
||
4769 |
^ self emphasiseFrom:start to:(self size) with:newEmphasis |
|
4770 |
||
4771 |
" |
|
12177 | 4772 |
'hello' emphasiseFrom:2 with:#italic |
11919 | 4773 |
|
4774 |
Transcript showCR:('hello' emphasiseFrom:2 with:#italic) |
|
4775 |
" |
|
4776 |
! |
|
4777 |
||
3404 | 4778 |
emphasizeAllWith:emphasis |
12326
c67f12213ddb
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
12319
diff
changeset
|
4779 |
"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
|
4780 |
|
c67f12213ddb
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
12319
diff
changeset
|
4781 |
"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
|
4782 |
Text isNil ifTrue:[^ self]. |
3404 | 4783 |
^ self asText emphasizeAllWith:emphasis |
4784 |
||
4785 |
" |
|
4786 |
Transcript showCR:('hello' emphasizeAllWith:#bold) |
|
3599 | 4787 |
Transcript showCR:('hello' emphasizeAllWith:(#color -> Color red)) |
4788 |
Transcript showCR:('hello' emphasizeAllWith:(#color -> Color red)) |
|
4789 |
" |
|
4790 |
||
4791 |
"Modified: / 17.6.1998 / 12:51:44 / cg" |
|
5097 | 4792 |
! |
4793 |
||
4794 |
makeSelectorBoldIn:aClass |
|
14050 | 4795 |
"the receiver represents some source code for a method in aClass. |
5097 | 4796 |
Change myself to boldify the selector. |
14050 | 4797 |
Not yet implemented (could easily use the syntaxHighlighter for this ...). |
4798 |
For protocol compatibility with other smalltalks" |
|
5097 | 4799 |
|
4800 |
^ self |
|
4801 |
||
14050 | 4802 |
"Created: / 13-12-1999 / 21:49:24 / cg" |
4803 |
! |
|
4804 |
||
19762 | 4805 |
withColor:aColorOrColorSymbol |
20697 | 4806 |
"return a text object representing the receiver, but all colorized. |
4807 |
Usage of a colorSymbol is considered bad style (provided for backward compatibility); |
|
4808 |
please change to pass a proper color |
|
4809 |
(makes it easier to find color uses)" |
|
4810 |
||
4811 |
|color| |
|
4812 |
||
19762 | 4813 |
aColorOrColorSymbol isSymbol ifTrue:[ |
20697 | 4814 |
color := (Color perform:aColorOrColorSymbol) |
4815 |
] ifFalse:[ |
|
4816 |
color := aColorOrColorSymbol |
|
4817 |
]. |
|
4818 |
^ self colorizeAllWith:color |
|
19762 | 4819 |
|
4820 |
" |
|
4821 |
Transcript showCR:('hello' withColor:#red) |
|
20697 | 4822 |
Transcript showCR:('world' withColor:#blue) |
16697 | 4823 |
Transcript showCR:('hello' withColor:Color red) |
4824 |
Transcript showCR:('world' withColor:Color green darkened) |
|
4825 |
" |
|
4826 |
! |
|
4827 |
||
22911 | 4828 |
withColor:foregroundColorOrColorSymbol on:backgroundColorOrColorSymbol |
4829 |
"return a text object representing the receiver, but all colorized with both fg and bg. |
|
4830 |
Usage of a colorSymbol is considered bad style (provided for backward compatibility); |
|
4831 |
please change to pass a proper color |
|
4832 |
(makes it easier to find color uses)" |
|
4833 |
||
4834 |
|fgColor bgColor| |
|
4835 |
||
4836 |
foregroundColorOrColorSymbol isSymbol ifTrue:[ |
|
4837 |
fgColor := (Color perform:foregroundColorOrColorSymbol) |
|
4838 |
] ifFalse:[ |
|
4839 |
fgColor := foregroundColorOrColorSymbol |
|
4840 |
]. |
|
4841 |
backgroundColorOrColorSymbol isSymbol ifTrue:[ |
|
4842 |
bgColor := (Color perform:backgroundColorOrColorSymbol) |
|
4843 |
] ifFalse:[ |
|
4844 |
bgColor := backgroundColorOrColorSymbol |
|
4845 |
]. |
|
4846 |
^ self colorizeAllWith:fgColor on:bgColor |
|
4847 |
||
4848 |
" |
|
4849 |
Transcript showCR:('hello' withColor:#red on:#blue) |
|
4850 |
Transcript showCR:('hello' withColor:Color red on:Color blue) |
|
4851 |
" |
|
4852 |
! |
|
4853 |
||
14050 | 4854 |
withoutAnyColorEmphasis |
4855 |
"for protocol compatibility with Text" |
|
4856 |
||
4857 |
^ self |
|
4858 |
||
4859 |
"Created: / 06-03-2012 / 18:15:38 / cg" |
|
6653 | 4860 |
! |
4861 |
||
12396 | 4862 |
withoutBackgroundColorEmphasis |
14050 | 4863 |
"for protocol compatibility with Text" |
4864 |
||
12396 | 4865 |
^ self |
14050 | 4866 |
|
4867 |
"Modified (comment): / 06-03-2012 / 18:14:27 / cg" |
|
12396 | 4868 |
! |
4869 |
||
6653 | 4870 |
withoutEmphasis:emphasisToRemove |
14050 | 4871 |
"for protocol compatibility with Text" |
4872 |
||
6653 | 4873 |
^ self |
14050 | 4874 |
|
4875 |
"Modified (comment): / 06-03-2012 / 18:14:29 / cg" |
|
12396 | 4876 |
! |
4877 |
||
4878 |
withoutForegroundColorEmphasis |
|
14050 | 4879 |
"for protocol compatibility with Text" |
4880 |
||
12396 | 4881 |
^ self |
14050 | 4882 |
|
4883 |
"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
|
4884 |
! ! |
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
4885 |
|
7265 | 4886 |
!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
|
4887 |
|
20013 | 4888 |
asDenormalizedUnicodeString |
4889 |
"return a new string containing the same characters, as a denormalized Unicode string. |
|
4890 |
This replaces diacritical chars (umlauts, accented chars etc) by |
|
4891 |
a sequence with combination characters. |
|
4892 |
(i.e. a plain character followed by a combining diacritical in the 0x03xx range)" |
|
4893 |
||
20014 | 4894 |
|map outStream mapChar| |
20013 | 4895 |
|
22406 | 4896 |
self containsNon7BitAscii ifFalse:[ |
4897 |
^ self "/ I cannot contain any diacritical chars |
|
4898 |
]. |
|
4899 |
||
20013 | 4900 |
map := self class unicodeDenormalizationMap. |
22406 | 4901 |
mapChar := [:char | |
20014 | 4902 |
|mappedChars| |
4903 |
||
4904 |
(mappedChars := map at:char ifAbsent:nil) notNil ifTrue:[ |
|
4905 |
mappedChars do:mapChar. |
|
4906 |
] ifFalse:[ |
|
4907 |
outStream nextPut:char. |
|
4908 |
]. |
|
4909 |
]. |
|
20013 | 4910 |
|
20014 | 4911 |
outStream := WriteStream on:(Unicode16String new:self size). |
4912 |
self do:mapChar. |
|
20013 | 4913 |
^ outStream contents asSingleByteStringIfPossible. |
4914 |
||
4915 |
" |
|
4916 |
'Ö' asDenormalizedUnicodeString |
|
4917 |
'aÖÄx' asDenormalizedUnicodeString |
|
4918 |
'abc' asDenormalizedUnicodeString |
|
22406 | 4919 |
'ṩ' asDenormalizedUnicodeString |
4920 |
" |
|
4921 |
||
4922 |
"Modified (format): / 02-01-2018 / 18:52:33 / stefan" |
|
20013 | 4923 |
! |
4924 |
||
20011 | 4925 |
asNormalizedUnicodeString |
20010 | 4926 |
"return a new string containing the same characters, as a normalized Unicode string. |
4927 |
This replaces combination characters by corresponding single characters. |
|
20863 | 4928 |
(i.e. diaresis and other combining diacriticals in the 0x03xx range). |
4929 |
Caveat: |
|
4930 |
possibly incomplete: only COMBINING_DIACRITICAL_MARKS are cared for. |
|
4931 |
Does not care for COMBINING_DIACRITICAL_MARKS_EXTENDED |
|
20864 | 4932 |
and COMBINING_DIACRITICAL_MARKS_SUPPLEMENT. |
4933 |
However; those are used for German dialectology, ancient Greek and other similar |
|
4934 |
exotic uses. Probably noone will ever even notice that they are missing..." |
|
20010 | 4935 |
|
4936 |
|outStream prevChar map mapEntries mappedChar| |
|
4937 |
||
4938 |
map := self class unicodeNormalizationMap. |
|
20017 | 4939 |
|
20010 | 4940 |
self bitsPerCharacter <= 8 ifTrue:[^ self]. "/ I cannot contain any overtypes |
20017 | 4941 |
(self contains:[:ch | ch unicodeBlock == #'COMBINING_DIACRITICAL_MARKS']) ifFalse:[^ self]. "/ I do not contain any overtypes |
20010 | 4942 |
|
4943 |
outStream := WriteStream on:(self species new:self size). |
|
4944 |
self do:[:char | |
|
20017 | 4945 |
((char unicodeBlock == #'COMBINING_DIACRITICAL_MARKS') |
20010 | 4946 |
and:[ (mapEntries := map at:char ifAbsent:nil) notNil |
4947 |
and:[ (mappedChar := mapEntries at:prevChar ifAbsent:nil) notNil ]]) ifTrue:[ |
|
20014 | 4948 |
prevChar := mappedChar. |
20010 | 4949 |
] ifFalse:[ |
4950 |
prevChar notNil ifTrue:[ outStream nextPut:prevChar]. |
|
4951 |
prevChar := char |
|
4952 |
]. |
|
4953 |
]. |
|
20011 | 4954 |
prevChar notNil ifTrue:[ outStream nextPut:prevChar]. |
20010 | 4955 |
^ outStream contents asSingleByteStringIfPossible. |
4956 |
||
4957 |
" |
|
4958 |
self unicodeNormalizationMap |
|
20012 | 4959 |
('O' , (Character value:16r0308)) asNormalizedUnicodeString -> 'Ö' |
20010 | 4960 |
('O' , (Character value:16r0308) , |
20012 | 4961 |
'A' , (Character value:16r0308)) asNormalizedUnicodeString -> 'ÖÄ' |
20014 | 4962 |
('s' , (Character value:16r0323) , (Character value:16r0307)) asNormalizedUnicodeString -> 'ṩ' |
4963 |
('s' , (Character value:16r0307) , (Character value:16r0323)) asNormalizedUnicodeString -> 'ṩ' |
|
20010 | 4964 |
" |
4965 |
! |
|
4966 |
||
12767
17d2c7268065
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12679
diff
changeset
|
4967 |
decodeAsLiteralArray |
17d2c7268065
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12679
diff
changeset
|
4968 |
"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
|
4969 |
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
|
4970 |
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
|
4971 |
|
17d2c7268065
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12679
diff
changeset
|
4972 |
^ self |
17d2c7268065
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12679
diff
changeset
|
4973 |
! |
17d2c7268065
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12679
diff
changeset
|
4974 |
|
991
f0b45a4b82d2
first attempt in encoding/decoding for different character representations (mac/msdos etc.)
Claus Gittinger <cg@exept.de>
parents:
986
diff
changeset
|
4975 |
decodeFrom:encodingSymbol |
8874 | 4976 |
"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
|
4977 |
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
|
4978 |
|
ca774158b317
code cleanup (removed old obsolete encoding stuff)
Claus Gittinger <cg@exept.de>
parents:
8102
diff
changeset
|
4979 |
|myEncoding encoder| |
7950 | 4980 |
|
4981 |
encodingSymbol isNil ifTrue:[^ self]. |
|
4982 |
myEncoding := self encoding. |
|
4983 |
encodingSymbol == myEncoding ifTrue:[^ self]. |
|
4984 |
||
22451 | 4985 |
encoder := CharacterEncoder encoderToEncodeFrom:myEncoding into:encodingSymbol. |
8124
ca774158b317
code cleanup (removed old obsolete encoding stuff)
Claus Gittinger <cg@exept.de>
parents:
8102
diff
changeset
|
4986 |
encoder isNil ifTrue:[^ self]. |
ca774158b317
code cleanup (removed old obsolete encoding stuff)
Claus Gittinger <cg@exept.de>
parents:
8102
diff
changeset
|
4987 |
^ encoder decodeString:self. |
22451 | 4988 |
|
4989 |
"Modified: / 17-01-2018 / 17:34:43 / stefan" |
|
991
f0b45a4b82d2
first attempt in encoding/decoding for different character representations (mac/msdos etc.)
Claus Gittinger <cg@exept.de>
parents:
986
diff
changeset
|
4990 |
! |
f0b45a4b82d2
first attempt in encoding/decoding for different character representations (mac/msdos etc.)
Claus Gittinger <cg@exept.de>
parents:
986
diff
changeset
|
4991 |
|
9192 | 4992 |
encodeFrom:oldEncoding into:newEncoding |
4993 |
^ CharacterEncoder encodeString:self from:oldEncoding into:newEncoding |
|
14405
1d0d99a95bba
comment/format in:7 methods
Claus Gittinger <cg@exept.de>
parents:
14168
diff
changeset
|
4994 |
|
1d0d99a95bba
comment/format in:7 methods
Claus Gittinger <cg@exept.de>
parents:
14168
diff
changeset
|
4995 |
" |
17600 | 4996 |
'äüö' encodeFrom:#iso8859 into:#utf8 |
4997 |
('äüö' encodeFrom:#iso8859 into:#utf8) encodeFrom:#utf8 into:#unicode |
|
14405
1d0d99a95bba
comment/format in:7 methods
Claus Gittinger <cg@exept.de>
parents:
14168
diff
changeset
|
4998 |
" |
9192 | 4999 |
! |
5000 |
||
22451 | 5001 |
encodeInto:newEncoding |
5002 |
^ CharacterEncoder encodeString:self from:self encoding into:newEncoding |
|
5003 |
||
5004 |
" |
|
5005 |
'äüö' encodeInto:#utf8 |
|
5006 |
('äüö' encodeInto:#utf8) decodeFrom:#utf8 |
|
5007 |
" |
|
5008 |
||
5009 |
"Created: / 17-01-2018 / 17:43:20 / stefan" |
|
5010 |
! |
|
5011 |
||
9192 | 5012 |
rot13 |
8874 | 5013 |
"Usenet: from `rotate alphabet 13 places'] |
6835
bdb2cb03200c
added utf8Decode with 8-bit chars only
Claus Gittinger <cg@exept.de>
parents:
6834
diff
changeset
|
5014 |
The simple Caesar-cypher encryption that replaces each English |
8874 | 5015 |
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
|
5016 |
so that 'The butler did it!!' becomes 'Gur ohgyre qvq vg!!' |
8874 | 5017 |
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
|
5018 |
It is used to enclose the text in a sealed wrapper that the reader must choose |
8874 | 5019 |
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
|
5020 |
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
|
5021 |
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
|
5022 |
|
11865 | 5023 |
^ self rot:13 |
8874 | 5024 |
|
5025 |
" |
|
5026 |
'hello world' rot13 |
|
5027 |
'hello world' rot13 rot13 |
|
6835
bdb2cb03200c
added utf8Decode with 8-bit chars only
Claus Gittinger <cg@exept.de>
parents:
6834
diff
changeset
|
5028 |
" |
bdb2cb03200c
added utf8Decode with 8-bit chars only
Claus Gittinger <cg@exept.de>
parents:
6834
diff
changeset
|
5029 |
! |
bdb2cb03200c
added utf8Decode with 8-bit chars only
Claus Gittinger <cg@exept.de>
parents:
6834
diff
changeset
|
5030 |
|
11865 | 5031 |
rot:n |
5032 |
"Usenet: from `rotate alphabet N places'] |
|
5033 |
The simple Caesar-cypher encryption that replaces each English |
|
5034 |
letter with the one N places forward or back along the alphabet, |
|
5035 |
so that 'The butler did it!!' becomes 'Gur ohgyre qvq vg!!' by rot 13 |
|
5036 |
Most Usenet news reading and posting programs include a rot13 feature. |
|
5037 |
It is used to enclose the text in a sealed wrapper that the reader must choose |
|
5038 |
to open -- e.g., for posting things that might offend some readers, or spoilers. |
|
5039 |
A major advantage of rot13 over rot(N) for other N is that it |
|
5040 |
is self-inverse, so the same code can be used for encoding and decoding." |
|
5041 |
||
5042 |
^ self species |
|
20874 | 5043 |
streamContents:[:aStream | |
5044 |
self do:[:char | |
|
5045 |
aStream nextPut:(char rot:n) ]] |
|
11865 | 5046 |
|
5047 |
" |
|
5048 |
'hello world' rot:13 |
|
5049 |
('hello world' rot:13) rot:13 |
|
5050 |
" |
|
5051 |
! |
|
5052 |
||
15656 | 5053 |
utf16Encoded |
5054 |
"Return my UTF-16 representation as a new TwoByteString" |
|
5055 |
||
5056 |
|s| |
|
5057 |
||
5058 |
s := WriteStream on:(TwoByteString uninitializedNew:self size). |
|
5059 |
s nextPutAllUtf16:self. |
|
5060 |
^ s contents |
|
5061 |
||
5062 |
" |
|
5063 |
'abcde1234' utf16Encoded |
|
17600 | 5064 |
'abcdeäöüß' utf16Encoded |
15656 | 5065 |
" |
5066 |
||
5067 |
"Modified: / 11-05-2010 / 19:12:37 / cg" |
|
5068 |
! |
|
5069 |
||
9192 | 5070 |
utf8Encoded |
19837 | 5071 |
"return the UTF-8 representation of a Unicode string. |
5072 |
The resulting string is only useful to be stored on some external file, |
|
21318 | 5073 |
or sent to a communaction channel. |
5074 |
Not for being used inside ST/X." |
|
19837 | 5075 |
|
5076 |
|string stream| |
|
17625 | 5077 |
|
5078 |
string := self string. |
|
19837 | 5079 |
|
5080 |
"/ avoid creation of new strings if possible |
|
5081 |
string containsNon7BitAscii ifFalse:[ |
|
5082 |
^ string asSingleByteString |
|
5083 |
]. |
|
5084 |
||
5085 |
"make it size 2 for 1-byte strings" |
|
5086 |
stream := WriteStream on:(String uninitializedNew:(1 + (string size * 3 // 2))). |
|
5087 |
string utf8EncodedOn:stream. |
|
5088 |
^ stream contents. |
|
5089 |
||
5090 |
" |
|
5091 |
'hello' utf8Encoded asByteArray #[104 101 108 108 111] |
|
5092 |
(Character value:16r40) asString utf8Encoded asByteArray #[64] |
|
5093 |
(Character value:16r7F) asString utf8Encoded asByteArray #[127] |
|
5094 |
(Character value:16r80) asString utf8Encoded asByteArray #[194 128] |
|
5095 |
(Character value:16rFF) asString utf8Encoded asByteArray #[195 191] |
|
5096 |
(Character value:16r100) asString utf8Encoded asByteArray #[196 128] |
|
5097 |
(Character value:16r200) asString utf8Encoded asByteArray #[200 128] |
|
5098 |
(Character value:16r400) asString utf8Encoded asByteArray #[208 128] |
|
5099 |
(Character value:16r800) asString utf8Encoded asByteArray #[224 160 128] |
|
5100 |
(Character value:16r1000) asString utf8Encoded asByteArray #[225 128 128] |
|
5101 |
(Character value:16r2000) asString utf8Encoded asByteArray #[226 128 128] |
|
5102 |
(Character value:16r4000) asString utf8Encoded asByteArray #[228 128 128] |
|
5103 |
(Character value:16r8000) asString utf8Encoded asByteArray #[232 128 128] |
|
5104 |
(Character value:16rFFFF) asString utf8Encoded asByteArray #[239 191 191] |
|
21318 | 5105 |
(Character value:16r1FFFF) asString utf8Encoded asByteArray #[240 159 191 191] |
5106 |
(Character value:16r3FFFF) asString utf8Encoded asByteArray #[240 191 191 191] |
|
5107 |
(Character value:16rFFFFF) asString utf8Encoded asByteArray #[243 191 191 191] |
|
5108 |
(Character value:16r3FFFFF) asString utf8Encoded asByteArray #[248 143 191 191 191] |
|
19837 | 5109 |
|
18597 | 5110 |
'abcde1234' asUnicode32String utf8Encoded |
5111 |
'abcdeäöüß' asUnicode32String utf8Encoded |
|
12925
5c0a9a633ca1
comment/format in: #utf8Encoded
Claus Gittinger <cg@exept.de>
parents:
12912
diff
changeset
|
5112 |
" |
17625 | 5113 |
! |
5114 |
||
5115 |
utf8EncodedOn:aStream |
|
21468 | 5116 |
"write the UTF-8 representation of myself to aStream." |
5117 |
||
5118 |
aStream nextPutAllUtf8:self. |
|
5119 |
||
5120 |
"Modified: / 16-02-2017 / 16:51:32 / stefan" |
|
9192 | 5121 |
! ! |
5122 |
||
22566 | 5123 |
!CharacterArray methodsFor:'filling and replacing'! |
5124 |
||
5125 |
clearContents |
|
5126 |
"to be used with cryptographic keys, to wipe their contents after use" |
|
5127 |
||
5128 |
self atAllPut:(Character value:0) |
|
5129 |
! ! |
|
22451 | 5130 |
|
5131 |
||
22953 | 5132 |
|
5133 |
||
12111 | 5134 |
!CharacterArray methodsFor:'matching - glob expressions'! |
5135 |
||
5136 |
compoundMatch:aString |
|
5137 |
"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
|
5138 |
consisting of multiple simple GLOB patterns, separated by semicolons. |
12111 | 5139 |
This is usable with fileName pattern fields. |
5140 |
||
5141 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20874 | 5142 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5143 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 5144 |
NOTICE: the receiver is the match pattern" |
5145 |
||
15890 | 5146 |
^ self compoundMatch:aString caseSensitive:true withoutSeparators:false |
12111 | 5147 |
|
5148 |
" |
|
5149 |
'f*' match:'foo' |
|
5150 |
'b*' match:'foo' |
|
5151 |
'f*;b*' match:'foo' |
|
5152 |
'f*;b*' match:'bar' |
|
5153 |
'f*;b*' compoundMatch:'foo' |
|
5154 |
'f*;b*' compoundMatch:'bar' |
|
5155 |
" |
|
5156 |
||
5157 |
"Modified: / 30.1.1998 / 11:40:18 / stefan" |
|
5158 |
"Modified: / 16.12.1999 / 01:22:08 / cg" |
|
5159 |
! |
|
5160 |
||
15734 | 5161 |
compoundMatch:aString caseSensitive:caseSensitive |
12111 | 5162 |
"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
|
5163 |
consisting of multiple simple GLOB patterns, separated by semicolons. |
12111 | 5164 |
This is usable with fileName pattern fields. |
5165 |
||
5166 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20874 | 5167 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5168 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 5169 |
NOTICE: the receiver is the match pattern" |
5170 |
||
15890 | 5171 |
^self compoundMatch:aString caseSensitive:caseSensitive withoutSeparators:false |
5172 |
||
5173 |
" |
|
5174 |
'f*' match:'foo' |
|
5175 |
'b*' match:'foo' |
|
5176 |
'f*;b*' match:'foo' |
|
5177 |
'f*;b*' match:'bar' |
|
5178 |
'f*;b*' compoundMatch:'foo' |
|
5179 |
'f*;b*' compoundMatch:'bar' |
|
5180 |
'f*;b*' compoundMatch:'Foo' caseSensitive:false |
|
5181 |
'f*;b*' compoundMatch:'Bar' caseSensitive:false |
|
5182 |
'f*;b*' compoundMatch:'ccc' caseSensitive:false |
|
5183 |
" |
|
5184 |
||
5185 |
"Modified: / 15.4.1997 / 15:50:33 / cg" |
|
5186 |
"Modified: / 30.1.1998 / 11:40:18 / stefan" |
|
5187 |
"Created: / 16.12.1999 / 01:21:35 / cg" |
|
5188 |
! |
|
5189 |
||
5190 |
compoundMatch:aString caseSensitive:caseSensitive withoutSeparators:withoutSeparators |
|
5191 |
"like match, but the receiver may be a compound match pattern, |
|
5192 |
consisting of multiple simple GLOB patterns, separated by semicolons. |
|
5193 |
If withoutSeparators is true, spaces around individual patterns are stripped off. |
|
5194 |
This is usable with fileName pattern fields. |
|
5195 |
||
5196 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20874 | 5197 |
NOT the ST-80 meaning. |
15890 | 5198 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
5199 |
NOTICE: the receiver is the match pattern" |
|
5200 |
||
12111 | 5201 |
|matchers| |
5202 |
||
5203 |
matchers := self asCollectionOfSubstringsSeparatedBy:$;. |
|
15890 | 5204 |
withoutSeparators ifTrue:[ matchers := matchers collect:[:each | each withoutSeparators] ]. |
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
5205 |
^ matchers |
20874 | 5206 |
contains:[:aPattern | |
5207 |
aPattern match:aString caseSensitive:caseSensitive escapeCharacter:nil |
|
5208 |
]. |
|
15734 | 5209 |
|
5210 |
" |
|
5211 |
'f*' match:'foo' |
|
5212 |
'b*' match:'foo' |
|
5213 |
'f*;b*' match:'foo' |
|
5214 |
'f*;b*' match:'bar' |
|
5215 |
'f*;b*' compoundMatch:'foo' |
|
5216 |
'f*;b*' compoundMatch:'bar' |
|
5217 |
'f*;b*' compoundMatch:'Foo' caseSensitive:false |
|
5218 |
'f*;b*' compoundMatch:'Bar' caseSensitive:false |
|
5219 |
'f*;b*' compoundMatch:'ccc' caseSensitive:false |
|
15890 | 5220 |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
5221 |
'f* ; b*' compoundMatch:'foo' |
15890 | 5222 |
'f* ; b*' compoundMatch:'foo' caseSensitive:true withoutSeparators:true |
15734 | 5223 |
" |
5224 |
||
5225 |
"Modified: / 15.4.1997 / 15:50:33 / cg" |
|
5226 |
"Modified: / 30.1.1998 / 11:40:18 / stefan" |
|
5227 |
"Created: / 16.12.1999 / 01:21:35 / cg" |
|
5228 |
! |
|
5229 |
||
5230 |
compoundMatch:aString ignoreCase:ignoreCase |
|
5231 |
<resource: #obsolete> |
|
5232 |
||
5233 |
"like match, but the receiver may be a compound match pattern, |
|
5234 |
consisting of multiple simple GLOB patterns, separated by semicolons. |
|
5235 |
This is usable with fileName pattern fields. |
|
5236 |
||
5237 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20874 | 5238 |
NOT the ST-80 meaning. |
15734 | 5239 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
5240 |
NOTICE: the receiver is the match pattern" |
|
5241 |
||
5242 |
^ self compoundMatch:aString caseSensitive:ignoreCase not |
|
12111 | 5243 |
|
5244 |
" |
|
5245 |
'f*' match:'foo' |
|
5246 |
'b*' match:'foo' |
|
5247 |
'f*;b*' match:'foo' |
|
5248 |
'f*;b*' match:'bar' |
|
5249 |
'f*;b*' compoundMatch:'foo' |
|
5250 |
'f*;b*' compoundMatch:'bar' |
|
5251 |
'f*;b*' compoundMatch:'Foo' ignoreCase:true |
|
5252 |
'f*;b*' compoundMatch:'Bar' ignoreCase:true |
|
5253 |
'f*;b*' compoundMatch:'ccc' ignoreCase:true |
|
5254 |
" |
|
5255 |
||
5256 |
"Modified: / 15.4.1997 / 15:50:33 / cg" |
|
5257 |
"Modified: / 30.1.1998 / 11:40:18 / stefan" |
|
5258 |
"Created: / 16.12.1999 / 01:21:35 / cg" |
|
5259 |
! |
|
5260 |
||
5261 |
findMatchString:matchString |
|
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5262 |
"like findString/indexOfSubCollection, but allowing GLOB match patterns. |
12111 | 5263 |
find matchstring; if found, return the index; |
5264 |
if not found, return 0. |
|
5265 |
||
5266 |
NOTICE: match-meta character interpretation is like in unix-matching, |
|
20874 | 5267 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5268 |
NOTICE: this GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 5269 |
NOTICE: the argument is the match pattern" |
5270 |
||
19033 | 5271 |
^ self findMatchString:matchString startingAt:1 caseSensitive:true ifAbsent:0 |
5272 |
||
5273 |
" |
|
5274 |
1234567890123 |
|
5275 |
'hello world bla foo baz' findMatchString:'b* ' |
|
5276 |
" |
|
12111 | 5277 |
! |
5278 |
||
5279 |
findMatchString:matchString startingAt:index |
|
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5280 |
"like findString, but allowing GLOB match patterns. |
12111 | 5281 |
find matchstring, starting at index. if found, return the index; |
5282 |
if not found, return 0. |
|
5283 |
||
5284 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20874 | 5285 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5286 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 5287 |
NOTICE: the argument is the match pattern" |
5288 |
||
19033 | 5289 |
^ self findMatchString:matchString startingAt:index caseSensitive:true ifAbsent:0 |
12111 | 5290 |
! |
5291 |
||
15734 | 5292 |
findMatchString:matchString startingAt:index caseSensitive:caseSensitive ifAbsent:exceptionBlock |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5293 |
"like findString, but allowing GLOB match patterns. |
12111 | 5294 |
find matchstring, starting at index. if found, return the index; |
5295 |
if not found, return the result of evaluating exceptionBlock. |
|
5296 |
This is a q&d hack - not very efficient. |
|
5297 |
||
5298 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20874 | 5299 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5300 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 5301 |
NOTICE: the argument is the match pattern" |
5302 |
||
5303 |
|firstChar firstSet |
|
5304 |
startIndex "{ Class: SmallInteger }" |
|
5305 |
matchSize "{ Class: SmallInteger }" |
|
5306 |
mySize "{ Class: SmallInteger }" |
|
5307 |
realMatchString lcChar ucChar| |
|
5308 |
||
5309 |
matchSize := matchString size. |
|
5310 |
matchSize == 0 ifTrue:[^ index]. "empty string matches" |
|
5311 |
||
5312 |
realMatchString := matchString. |
|
5313 |
(realMatchString endsWith:$*) ifFalse:[ |
|
20874 | 5314 |
realMatchString := realMatchString , '*'. |
5315 |
matchSize := matchSize + 1 |
|
12111 | 5316 |
]. |
5317 |
||
5318 |
mySize := self size. |
|
5319 |
firstChar := realMatchString at:1. |
|
5320 |
firstChar == self class matchEscapeCharacter ifTrue:[ |
|
20874 | 5321 |
firstChar := realMatchString at:2. |
12111 | 5322 |
]. |
5323 |
||
5324 |
firstChar asString includesMatchCharacters ifTrue:[ |
|
20874 | 5325 |
index to:mySize do:[:col | |
5326 |
(realMatchString match:self from:col to:mySize caseSensitive:caseSensitive) |
|
5327 |
ifTrue:[^ col] |
|
5328 |
]. |
|
5329 |
^ exceptionBlock value. |
|
12111 | 5330 |
]. |
5331 |
||
5332 |
lcChar := firstChar asLowercase. |
|
5333 |
ucChar := firstChar asUppercase. |
|
15734 | 5334 |
(caseSensitive not and:[ lcChar ~= ucChar ]) ifTrue:[ |
20874 | 5335 |
firstSet := Array with:ucChar with:lcChar. |
5336 |
startIndex := self indexOfAny:firstSet startingAt:index. |
|
12111 | 5337 |
] ifFalse:[ |
20874 | 5338 |
startIndex := self indexOf:firstChar startingAt:index. |
12111 | 5339 |
]. |
5340 |
[startIndex == 0] whileFalse:[ |
|
20874 | 5341 |
(realMatchString match:self from:startIndex to:mySize caseSensitive:caseSensitive) |
5342 |
ifTrue:[^ startIndex]. |
|
5343 |
||
5344 |
firstSet notNil ifTrue:[ |
|
5345 |
startIndex := self indexOfAny:firstSet startingAt:(startIndex + 1). |
|
5346 |
] ifFalse:[ |
|
5347 |
startIndex := self indexOf:firstChar startingAt:(startIndex + 1). |
|
5348 |
]. |
|
12111 | 5349 |
]. |
5350 |
^ exceptionBlock value |
|
5351 |
||
5352 |
" |
|
5353 |
'one two three four' findMatchString:'o[nu]' |
|
5354 |
'one two three four' findMatchString:'o[nu]' startingAt:3 |
|
15734 | 5355 |
'one two three four one' findMatchString:'ONE' startingAt:3 caseSensitive:false ifAbsent:0 |
5356 |
'one two three four one' findMatchString:'ONE' startingAt:3 caseSensitive:true ifAbsent:0 |
|
5357 |
'one two three four ONE' findMatchString:'O#E' startingAt:1 caseSensitive:false ifAbsent:0 |
|
5358 |
'one two three four ONE' findMatchString:'O#E' startingAt:1 caseSensitive:true ifAbsent:0 |
|
5359 |
12345678901234567890 |
|
5360 |
" |
|
5361 |
||
5362 |
"Modified: 13.9.1997 / 06:31:22 / cg" |
|
5363 |
! |
|
5364 |
||
5365 |
findMatchString:matchString startingAt:index ignoreCase:ignoreCase ifAbsent:exceptionBlock |
|
5366 |
<resource: #obsolete> |
|
5367 |
"like findString, but allowing GLOB match patterns. |
|
5368 |
find matchstring, starting at index. if found, return the index; |
|
5369 |
if not found, return the result of evaluating exceptionBlock. |
|
5370 |
This is a q&d hack - not very efficient. |
|
5371 |
||
5372 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20874 | 5373 |
NOT the ST-80 meaning. |
15734 | 5374 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
5375 |
NOTICE: the argument is the match pattern" |
|
5376 |
||
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
5377 |
^ self |
20874 | 5378 |
findMatchString:matchString startingAt:index caseSensitive:ignoreCase not ifAbsent:exceptionBlock |
15734 | 5379 |
|
5380 |
" |
|
5381 |
'one two three four' findMatchString:'o[nu]' |
|
5382 |
'one two three four' findMatchString:'o[nu]' startingAt:3 |
|
12111 | 5383 |
'one two three four one' findMatchString:'ONE' startingAt:3 ignoreCase:true ifAbsent:0 |
5384 |
" |
|
5385 |
||
5386 |
"Modified: 13.9.1997 / 06:31:22 / cg" |
|
5387 |
! |
|
5388 |
||
19746 | 5389 |
globPatternAsRegexPattern |
5390 |
"taking the receiver as a GLOB pattern, |
|
5391 |
return a corresponding regex pattern. |
|
5392 |
As regex does provide information about the matching substring, |
|
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
5393 |
it may be useful to apply a regex after a GLOB match, |
19746 | 5394 |
in order to highlight matching substrings (eg. in a CodeView after a search). |
5395 |
If it turns out to be better, we may convert all GLOB searches to regex and use it right away. |
|
5396 |
(currently, it is not sure, if GLOB is not better for most simple searches, as they are encountered |
|
5397 |
in typical real life)" |
|
5398 |
||
5399 |
^ self species streamContents:[:s | |
|
20874 | 5400 |
|addCharacter| |
5401 |
||
5402 |
addCharacter := |
|
5403 |
[:ch | |
|
5404 |
ch isLetterOrDigit ifFalse:[ |
|
5405 |
s nextPut:$\. |
|
5406 |
]. |
|
5407 |
s nextPut:ch |
|
5408 |
]. |
|
5409 |
||
5410 |
(String matchScanArrayFrom:self) do:[:matchEntry | |
|
5411 |
matchEntry isCharacter ifTrue:[ |
|
5412 |
addCharacter value:matchEntry |
|
5413 |
] ifFalse:[ |
|
5414 |
matchEntry == #anyString ifTrue:[ |
|
5415 |
s nextPutAll:'.*' |
|
5416 |
] ifFalse:[ |
|
5417 |
matchEntry == #any ifTrue:[ |
|
5418 |
s nextPut:$. |
|
5419 |
] ifFalse:[ |
|
5420 |
matchEntry isString ifTrue:[ |
|
5421 |
|set min max| |
|
5422 |
||
5423 |
s nextPut:$[. |
|
5424 |
set := matchEntry copy sort. |
|
5425 |
min := set min. |
|
5426 |
max := set max. |
|
5427 |
set asSet = (min to:max) asSet ifTrue:[ |
|
5428 |
addCharacter value:min. |
|
5429 |
s nextPut:$-. |
|
5430 |
addCharacter value:max. |
|
5431 |
] ifFalse:[ |
|
5432 |
set do:addCharacter. |
|
5433 |
]. |
|
5434 |
s nextPut:$]. |
|
5435 |
] ifFalse:[ |
|
22700 | 5436 |
self halt:'funny match entry'. |
20874 | 5437 |
]. |
5438 |
]. |
|
5439 |
]. |
|
5440 |
] |
|
5441 |
]. |
|
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
5442 |
]. |
19746 | 5443 |
|
5444 |
" |
|
5445 |
'hello' globPatternAsRegexPattern |
|
5446 |
'hello*' globPatternAsRegexPattern |
|
5447 |
'*hello*' globPatternAsRegexPattern |
|
5448 |
'h###' globPatternAsRegexPattern |
|
5449 |
'h[0-9]' globPatternAsRegexPattern |
|
5450 |
'h[0-9][0-9][0-9]' globPatternAsRegexPattern |
|
5451 |
'h[0-9]*' globPatternAsRegexPattern |
|
19747 | 5452 |
'h[-+]*' globPatternAsRegexPattern |
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
5453 |
|
19747 | 5454 |
'hello world' matches:'h*w' |
5455 |
'hello world' matchesRegex:('h*w' globPatternAsRegexPattern) |
|
5456 |
'hello world' matches:'h*d' |
|
5457 |
'hello world' matchesRegex:('h*d' globPatternAsRegexPattern) |
|
19746 | 5458 |
" |
5459 |
! |
|
5460 |
||
12111 | 5461 |
includesMatchString:matchString |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5462 |
"like includesString, but allowing GLOB match patterns. |
12111 | 5463 |
find matchstring; if found, return true, otherwise return false. |
5464 |
||
5465 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20874 | 5466 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5467 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 5468 |
NOTICE: the argument is the match pattern" |
5469 |
||
5470 |
^ (self findMatchString:matchString) ~~ 0 |
|
5471 |
||
5472 |
" |
|
5473 |
'hello world' includesMatchString:'h*' |
|
5474 |
'hello world' includesMatchString:'h[aeiou]llo' |
|
5475 |
'hello world' includesMatchString:'wor*' |
|
5476 |
'hello world' includesMatchString:'woR*' |
|
5477 |
" |
|
5478 |
! |
|
5479 |
||
5480 |
includesMatchString:matchString caseSensitive:caseSensitive |
|
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5481 |
"like includesString, but allowing GLOB match patterns. |
12111 | 5482 |
find matchstring; if found, return true, otherwise return false. |
5483 |
||
5484 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20874 | 5485 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5486 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 5487 |
NOTICE: the argument is the match pattern" |
5488 |
||
19033 | 5489 |
^ (self findMatchString:matchString startingAt:1 caseSensitive:caseSensitive ifAbsent:0) ~~ 0 |
12111 | 5490 |
|
5491 |
" |
|
5492 |
'hello world' includesMatchString:'h*' caseSensitive:true |
|
5493 |
'hello world' includesMatchString:'h*' caseSensitive:false |
|
5494 |
'Hello world' includesMatchString:'h*' caseSensitive:true |
|
5495 |
'Hello world' includesMatchString:'h*' caseSensitive:false |
|
5496 |
||
5497 |
'hello world' includesMatchString:'h[aeiou]llo' caseSensitive:true |
|
5498 |
'hello world' includesMatchString:'h[aeiou]llo' caseSensitive:false |
|
5499 |
||
5500 |
'hello world' includesMatchString:'wor*' caseSensitive:true |
|
5501 |
'hello world' includesMatchString:'wor*' caseSensitive:false |
|
5502 |
||
5503 |
'hello world' includesMatchString:'woR*' caseSensitive:true |
|
5504 |
'hello world' includesMatchString:'woR*' caseSensitive:false |
|
5505 |
" |
|
5506 |
! |
|
5507 |
||
5508 |
match:aString |
|
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5509 |
"return true if aString matches self, where self may contain GLOB meta-match |
12111 | 5510 |
characters $* (to match any string) or $# (to match any character). |
5511 |
or [...] to match a set of characters. |
|
5512 |
Lower/uppercase are considered different. |
|
5513 |
The escape character is the backQuote. |
|
5514 |
||
5515 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20874 | 5516 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5517 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 5518 |
NOTICE: the receiver is the match pattern" |
5519 |
||
15734 | 5520 |
^ self match:aString from:1 to:aString size caseSensitive:true |
12111 | 5521 |
|
5522 |
" |
|
22137 | 5523 |
'#f#' match:'afc' |
5524 |
'#f#' match:'aec' |
|
5525 |
'#f#' match:'ae' |
|
5526 |
'#f#' match:'a' |
|
5527 |
'#f#' match:'f' |
|
5528 |
'#f#' match:'ff' |
|
5529 |
||
12111 | 5530 |
'\*f*' match:'f' |
5531 |
'\*f*' match:'*f' |
|
5532 |
'*\*f*' match:'*f' |
|
5533 |
'*f*' match:'*f' |
|
5534 |
'*ute*' match:'computer' |
|
5535 |
'*uter' match:'computer' |
|
5536 |
'uter*' match:'computer' |
|
5537 |
'*ute*' match:'' |
|
5538 |
'[abcd]*' match:'computer' |
|
5539 |
'[abcd]*' match:'komputer' |
|
5540 |
'*some*compl*ern*' match:'this is some more complicated pattern match' |
|
5541 |
'*some*compl*ern*' match:'this is another complicated pattern match' |
|
5542 |
'*-hh' match:'anton-h' |
|
5543 |
" |
|
5544 |
||
22137 | 5545 |
"Modified: / 09-06-1998 / 18:50:00 / cg" |
5546 |
"Modified (comment): / 29-07-2017 / 14:00:23 / cg" |
|
12111 | 5547 |
! |
5548 |
||
15734 | 5549 |
match:aString caseSensitive:caseSensitive |
5550 |
"return true if aString matches self, where self may contain GLOB meta-match |
|
5551 |
characters $* (to match any string) or $# (to match any character) |
|
5552 |
or [...] to match a set of characters. |
|
5553 |
If caseSensitive is false, lower/uppercase are considered the same. |
|
5554 |
The escape character is the backQuote. |
|
5555 |
||
5556 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20874 | 5557 |
NOT the ST-80 meaning. |
15734 | 5558 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
5559 |
NOTICE: the receiver is the match pattern" |
|
5560 |
||
5561 |
^ self match:aString from:1 to:aString size caseSensitive:caseSensitive |
|
5562 |
||
5563 |
" |
|
5564 |
'*ute*' match:'COMPUTER' caseSensitive:false |
|
5565 |
'*uter' match:'COMPUTER' caseSensitive:true |
|
5566 |
'[abcd]*' match:'computer' caseSensitive:true |
|
5567 |
'[abcd]*' match:'Computer' caseSensitive:true |
|
5568 |
'[a-k]*' match:'komputer' caseSensitive:true |
|
5569 |
'[a-k]*' match:'zomputer' caseSensitive:true |
|
5570 |
'[a-k]*' match:'Komputer' caseSensitive:true |
|
5571 |
'[a-k]*' match:'Komputer' caseSensitive:false |
|
5572 |
'*some*compl*ern*' match:'this is some more complicated pattern match' caseSensitive:false |
|
5573 |
'*some*compl*ern*' match:'this is another complicated pattern match' caseSensitive:false |
|
5574 |
||
5575 |
Time millisecondsToRun:[ |
|
20874 | 5576 |
Symbol allInstancesDo:[:sym | |
5577 |
'[ab]*' match:sym caseSensitive:true |
|
5578 |
] |
|
15734 | 5579 |
]. |
5580 |
Time millisecondsToRun:[ |
|
20874 | 5581 |
Symbol allInstancesDo:[:sym | |
5582 |
'*at:*' match:sym caseSensitive:true |
|
5583 |
] |
|
15734 | 5584 |
]. |
5585 |
" |
|
5586 |
||
5587 |
"Modified: 2.4.1997 / 17:28:58 / cg" |
|
5588 |
! |
|
5589 |
||
5590 |
match:aString caseSensitive:caseSensitive escapeCharacter:escape |
|
5591 |
"return true if aString matches self, where self may contain GLOB meta-match |
|
5592 |
characters $* (to match any string) or $# (to match any character) |
|
5593 |
or [...] to match a set of characters. |
|
5594 |
If caseSensitive is false, lower/uppercase are considered the same. |
|
5595 |
||
5596 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20874 | 5597 |
NOT the ST-80 meaning. |
15734 | 5598 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
5599 |
NOTICE: the receiver is the match pattern" |
|
5600 |
||
5601 |
^ self match:aString from:1 to:aString size caseSensitive:caseSensitive escapeCharacter:escape |
|
5602 |
||
5603 |
" |
|
5604 |
'*ute*' match:'COMPUTER' caseSensitive:false |
|
5605 |
'*uter' match:'COMPUTER' caseSensitive:true |
|
5606 |
'[abcd]*' match:'computer' caseSensitive:true |
|
5607 |
'[abcd]*' match:'Computer' caseSensitive:true |
|
5608 |
'[a-k]*' match:'komputer' caseSensitive:true |
|
5609 |
'[a-k]*' match:'zomputer' caseSensitive:true |
|
5610 |
'[a-k]*' match:'Komputer' caseSensitive:true |
|
5611 |
'[a-k]*' match:'Komputer' caseSensitive:false |
|
5612 |
'*some*compl*ern*' match:'this is some more complicated pattern match' caseSensitive:false |
|
5613 |
'*some*compl*ern*' match:'this is another complicated pattern match' caseSensitive:false |
|
5614 |
||
5615 |
Time millisecondsToRun:[ |
|
20874 | 5616 |
Symbol allInstancesDo:[:sym | |
5617 |
'[ab]*' match:sym caseSensitive:true |
|
5618 |
] |
|
15734 | 5619 |
]. |
5620 |
Time millisecondsToRun:[ |
|
20874 | 5621 |
Symbol allInstancesDo:[:sym | |
5622 |
'*at:*' match:sym caseSensitive:true |
|
5623 |
] |
|
15734 | 5624 |
]. |
5625 |
" |
|
5626 |
||
5627 |
"Modified: 2.4.1997 / 17:28:58 / cg" |
|
5628 |
! |
|
5629 |
||
12111 | 5630 |
match:aString escapeCharacter:escape |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5631 |
"return true if aString matches self, where self may contain GLOB meta-match |
12111 | 5632 |
characters $* (to match any string) or $# (to match any character). |
5633 |
or [...] to match a set of characters. |
|
5634 |
Lower/uppercase are considered different. |
|
5635 |
||
5636 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20874 | 5637 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5638 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 5639 |
NOTICE: the receiver is the match pattern" |
5640 |
||
19033 | 5641 |
^ self match:aString from:1 to:aString size caseSensitive:true escapeCharacter:escape |
12111 | 5642 |
|
5643 |
" |
|
5644 |
'a\b\c\*' match:'a\b\c\d' |
|
5645 |
'a\b\c\*' match:'a\b\c\d' escapeCharacter:nil |
|
5646 |
" |
|
5647 |
! |
|
5648 |
||
15734 | 5649 |
match:aString from:start to:stop caseSensitive:caseSensitive |
5650 |
"return true if part of aString matches myself, |
|
5651 |
where self may contain GLOB meta-match |
|
5652 |
characters $* (to match any string) or $# (to match any character) |
|
5653 |
or [...] to match a set of characters. |
|
5654 |
If caseSensitive is false, lower/uppercase are considered the same. |
|
5655 |
The escape character is the backQuote. |
|
5656 |
||
5657 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20874 | 5658 |
NOT the ST-80 meaning. |
15734 | 5659 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
5660 |
NOTICE: the receiver is the match pattern" |
|
5661 |
||
5662 |
^ self |
|
20874 | 5663 |
match:aString from:start to:stop caseSensitive:caseSensitive |
5664 |
escapeCharacter:(self class matchEscapeCharacter) |
|
15734 | 5665 |
|
5666 |
" |
|
5667 |
'*ute*' match:'12345COMPUTER' from:1 to:5 caseSensitive:false |
|
5668 |
'*ute*' match:'12345COMPUTER' from:6 to:13 caseSensitive:false |
|
5669 |
" |
|
5670 |
||
5671 |
"Modified: / 10.11.1998 / 21:43:46 / cg" |
|
5672 |
! |
|
5673 |
||
5674 |
match:aString from:start to:stop caseSensitive:caseSensitive escapeCharacter:escape |
|
5675 |
"return true if part of aString matches myself, |
|
5676 |
where self may contain GLOB meta-match |
|
5677 |
characters $* (to match any string) or $# (to match any character) |
|
5678 |
or [...] to match a set of characters. |
|
5679 |
If caseSensitive is false, lower/uppercase are considered the same. |
|
5680 |
||
5681 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20667 | 5682 |
NOT the ST-80 meaning. |
15734 | 5683 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
5684 |
NOTICE: the receiver is the match pattern" |
|
5685 |
||
5686 |
|matchScanArray| |
|
5687 |
||
5688 |
" |
|
22137 | 5689 |
keep the matchScanArray from the most recent matches - |
5690 |
avoids parsing the pattern over-and over again, |
|
5691 |
if multiple searches are done with the same pattern. |
|
5692 |
" |
|
20667 | 5693 |
(PreviousMatches isNil |
5694 |
or:[(matchScanArray := PreviousMatches at: self ifAbsent:[nil]) isNil]) ifTrue:[ |
|
5695 |
matchScanArray := self class matchScanArrayFrom:self escapeCharacter:escape. |
|
5696 |
matchScanArray isNil ifTrue:[ |
|
5697 |
'CharacterArray [info]: invalid matchpattern:''' infoPrint. self infoPrint. ''' comparing for equality.' infoPrintCR. |
|
5698 |
^ self = aString |
|
15734 | 5699 |
"/ ^ false |
20667 | 5700 |
]. |
5701 |
PreviousMatches isNil ifTrue:[ |
|
22137 | 5702 |
PreviousMatches := CacheDictionary new:15 |
20667 | 5703 |
]. |
5704 |
PreviousMatches at:self put:matchScanArray. |
|
15734 | 5705 |
]. |
5706 |
||
5707 |
^ self class |
|
20667 | 5708 |
matchScan:matchScanArray |
5709 |
from:1 to:matchScanArray size |
|
5710 |
with:aString |
|
5711 |
from:start to:stop |
|
5712 |
caseSensitive:caseSensitive |
|
15734 | 5713 |
|
5714 |
" |
|
5715 |
'*ute*' match:'12345COMPUTER' from:1 to:5 caseSensitive:false |
|
5716 |
'*ute*' match:'12345COMPUTER' from:6 to:13 caseSensitive:false |
|
5717 |
" |
|
5718 |
||
22137 | 5719 |
"Modified: / 29-07-2017 / 14:01:42 / cg" |
15734 | 5720 |
! |
5721 |
||
12111 | 5722 |
match:aString from:start to:stop ignoreCase:ignoreCase |
15734 | 5723 |
<resource: #obsolete> |
12111 | 5724 |
"return true if part of aString matches myself, |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5725 |
where self may contain GLOB meta-match |
12111 | 5726 |
characters $* (to match any string) or $# (to match any character) |
5727 |
or [...] to match a set of characters. |
|
5728 |
If ignoreCase is true, lower/uppercase are considered the same. |
|
5729 |
The escape character is the backQuote. |
|
5730 |
||
5731 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20874 | 5732 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5733 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 5734 |
NOTICE: the receiver is the match pattern" |
5735 |
||
5736 |
^ self |
|
20874 | 5737 |
match:aString from:start to:stop caseSensitive:ignoreCase not |
5738 |
escapeCharacter:(self class matchEscapeCharacter) |
|
12111 | 5739 |
|
5740 |
" |
|
5741 |
'*ute*' match:'12345COMPUTER' from:1 to:5 ignoreCase:true |
|
5742 |
'*ute*' match:'12345COMPUTER' from:6 to:13 ignoreCase:true |
|
5743 |
" |
|
5744 |
||
5745 |
"Modified: / 10.11.1998 / 21:43:46 / cg" |
|
5746 |
! |
|
5747 |
||
5748 |
match:aString from:start to:stop ignoreCase:ignoreCase escapeCharacter:escape |
|
15734 | 5749 |
<resource: #obsolete> |
12111 | 5750 |
"return true if part of aString matches myself, |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5751 |
where self may contain GLOB meta-match |
12111 | 5752 |
characters $* (to match any string) or $# (to match any character) |
5753 |
or [...] to match a set of characters. |
|
5754 |
If ignoreCase is true, lower/uppercase are considered the same. |
|
5755 |
||
5756 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20874 | 5757 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5758 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 5759 |
NOTICE: the receiver is the match pattern" |
5760 |
||
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
5761 |
^ self |
20874 | 5762 |
match:aString from:start to:stop |
5763 |
caseSensitive:ignoreCase not |
|
5764 |
escapeCharacter:escape |
|
12111 | 5765 |
|
5766 |
" |
|
5767 |
'*ute*' match:'12345COMPUTER' from:1 to:5 ignoreCase:true |
|
5768 |
'*ute*' match:'12345COMPUTER' from:6 to:13 ignoreCase:true |
|
5769 |
" |
|
5770 |
||
5771 |
"Modified: / 10.11.1998 / 21:43:46 / cg" |
|
5772 |
! |
|
5773 |
||
5774 |
match:aString ignoreCase:ignoreCase |
|
15734 | 5775 |
<resource: #obsolete> |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5776 |
"return true if aString matches self, where self may contain GLOB meta-match |
12111 | 5777 |
characters $* (to match any string) or $# (to match any character) |
5778 |
or [...] to match a set of characters. |
|
5779 |
If ignoreCase is true, lower/uppercase are considered the same. |
|
5780 |
The escape character is the backQuote. |
|
5781 |
||
5782 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20874 | 5783 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5784 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 5785 |
NOTICE: the receiver is the match pattern" |
5786 |
||
15734 | 5787 |
"/ leave it this way, in case a customer has redefined it for performance (for a while) |
19033 | 5788 |
^ self match:aString from:1 to:aString size caseSensitive:ignoreCase not |
12111 | 5789 |
|
5790 |
" |
|
5791 |
'*ute*' match:'COMPUTER' ignoreCase:true |
|
5792 |
'*uter' match:'COMPUTER' ignoreCase:false |
|
5793 |
'[abcd]*' match:'computer' ignoreCase:false |
|
5794 |
'[abcd]*' match:'Computer' ignoreCase:false |
|
5795 |
'[a-k]*' match:'komputer' ignoreCase:false |
|
5796 |
'[a-k]*' match:'zomputer' ignoreCase:false |
|
5797 |
'[a-k]*' match:'Komputer' ignoreCase:false |
|
5798 |
'[a-k]*' match:'Komputer' ignoreCase:true |
|
5799 |
'*some*compl*ern*' match:'this is some more complicated pattern match' ignoreCase:true |
|
5800 |
'*some*compl*ern*' match:'this is another complicated pattern match' ignoreCase:true |
|
5801 |
||
5802 |
Time millisecondsToRun:[ |
|
20874 | 5803 |
Symbol allInstancesDo:[:sym | |
5804 |
'[ab]*' match:sym ignoreCase:false |
|
5805 |
] |
|
12111 | 5806 |
]. |
5807 |
Time millisecondsToRun:[ |
|
20874 | 5808 |
Symbol allInstancesDo:[:sym | |
5809 |
'*at:*' match:sym ignoreCase:false |
|
5810 |
] |
|
12111 | 5811 |
]. |
5812 |
" |
|
5813 |
||
5814 |
"Modified: 2.4.1997 / 17:28:58 / cg" |
|
5815 |
! |
|
5816 |
||
5817 |
match:aString ignoreCase:ignoreCase escapeCharacter:escape |
|
15734 | 5818 |
<resource: #obsolete> |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5819 |
"return true if aString matches self, where self may contain GLOB meta-match |
12111 | 5820 |
characters $* (to match any string) or $# (to match any character) |
5821 |
or [...] to match a set of characters. |
|
5822 |
If ignoreCase is true, lower/uppercase are considered the same. |
|
5823 |
||
5824 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20874 | 5825 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5826 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 5827 |
NOTICE: the receiver is the match pattern" |
5828 |
||
19033 | 5829 |
^ self match:aString from:1 to:aString size caseSensitive:ignoreCase not escapeCharacter:escape |
12111 | 5830 |
|
5831 |
" |
|
5832 |
'*ute*' match:'COMPUTER' ignoreCase:true |
|
5833 |
'*uter' match:'COMPUTER' ignoreCase:false |
|
5834 |
'[abcd]*' match:'computer' ignoreCase:false |
|
5835 |
'[abcd]*' match:'Computer' ignoreCase:false |
|
5836 |
'[a-k]*' match:'komputer' ignoreCase:false |
|
5837 |
'[a-k]*' match:'zomputer' ignoreCase:false |
|
5838 |
'[a-k]*' match:'Komputer' ignoreCase:false |
|
5839 |
'[a-k]*' match:'Komputer' ignoreCase:true |
|
5840 |
'*some*compl*ern*' match:'this is some more complicated pattern match' ignoreCase:true |
|
5841 |
'*some*compl*ern*' match:'this is another complicated pattern match' ignoreCase:true |
|
5842 |
||
5843 |
Time millisecondsToRun:[ |
|
20874 | 5844 |
Symbol allInstancesDo:[:sym | |
5845 |
'[ab]*' match:sym ignoreCase:false |
|
5846 |
] |
|
12111 | 5847 |
]. |
5848 |
Time millisecondsToRun:[ |
|
20874 | 5849 |
Symbol allInstancesDo:[:sym | |
5850 |
'*at:*' match:sym ignoreCase:false |
|
5851 |
] |
|
12111 | 5852 |
]. |
5853 |
" |
|
5854 |
||
5855 |
"Modified: 2.4.1997 / 17:28:58 / cg" |
|
5856 |
! |
|
5857 |
||
5858 |
matches:aPatternString |
|
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5859 |
"return true if the receiver matches aString, where aPatternString may contain GLOB meta-match |
12111 | 5860 |
characters $* (to match any string) or $# (to match any character). |
5861 |
or [...] to match a set of characters. |
|
5862 |
Lower/uppercase are considered different. |
|
5863 |
||
5864 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20874 | 5865 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
5866 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 5867 |
NOTICE: the receiver is the match pattern" |
5868 |
||
5869 |
^ aPatternString match:self |
|
14054
48d3fab77bd9
added: #matches:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
14053
diff
changeset
|
5870 |
! |
48d3fab77bd9
added: #matches:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
14053
diff
changeset
|
5871 |
|
15734 | 5872 |
matches:aPatternString caseSensitive:caseSensitive |
5873 |
"return true if the receiver matches aString, where aPatternString may contain GLOB meta-match |
|
5874 |
characters $* (to match any string) or $# (to match any character). |
|
5875 |
or [...] to match a set of characters. |
|
5876 |
||
5877 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
20874 | 5878 |
NOT the ST-80 meaning. |
15734 | 5879 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
5880 |
NOTICE: the receiver is the match pattern" |
|
5881 |
||
5882 |
^ aPatternString match:self caseSensitive:caseSensitive |
|
5883 |
! |
|
5884 |
||
14054
48d3fab77bd9
added: #matches:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
14053
diff
changeset
|
5885 |
matches:aPatternString ignoreCase:ignoreCase |
15734 | 5886 |
<resource: #obsolete> |
14054
48d3fab77bd9
added: #matches:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
14053
diff
changeset
|
5887 |
"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
|
5888 |
characters $* (to match any string) or $# (to match any character). |
48d3fab77bd9
added: #matches:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
14053
diff
changeset
|
5889 |
or [...] to match a set of characters. |
48d3fab77bd9
added: #matches:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
14053
diff
changeset
|
5890 |
Lower/uppercase are considered different. |
48d3fab77bd9
added: #matches:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
14053
diff
changeset
|
5891 |
|
48d3fab77bd9
added: #matches:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
14053
diff
changeset
|
5892 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
20874 | 5893 |
NOT the ST-80 meaning. |
14054
48d3fab77bd9
added: #matches:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
14053
diff
changeset
|
5894 |
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
|
5895 |
NOTICE: the receiver is the match pattern" |
48d3fab77bd9
added: #matches:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
14053
diff
changeset
|
5896 |
|
15734 | 5897 |
^ aPatternString match:self caseSensitive:ignoreCase not |
14054
48d3fab77bd9
added: #matches:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
14053
diff
changeset
|
5898 |
|
48d3fab77bd9
added: #matches:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
14053
diff
changeset
|
5899 |
"Created: / 08-03-2012 / 03:11:11 / cg" |
12111 | 5900 |
! ! |
5901 |
||
5902 |
||
22908 | 5903 |
|
9192 | 5904 |
!CharacterArray methodsFor:'padded copying'! |
5905 |
||
5906 |
centerPaddedTo:newSize |
|
19464 | 5907 |
"return a new string consisting of the receiver's characters, |
5908 |
plus spaces up to length and center the receiver's characters in |
|
5909 |
the resulting string. |
|
5910 |
If the receiver's size is equal or greater than the length argument, |
|
5911 |
the original receiver is returned unchanged." |
|
9192 | 5912 |
|
5913 |
^ self centerPaddedTo:newSize with:(Character space) |
|
5914 |
||
5915 |
" |
|
5916 |
'foo' centerPaddedTo:10 |
|
5917 |
123 printString centerPaddedTo:10 |
|
5918 |
" |
|
5919 |
||
5920 |
"Created: 25.11.1995 / 10:53:57 / cg" |
|
5921 |
! |
|
9190 | 5922 |
|
5923 |
centerPaddedTo:size with:padCharacter |
|
5924 |
"return a new string of length size, which contains the receiver |
|
5925 |
centered (i.e. padded on both sides). |
|
5926 |
Characters are filled with padCharacter. |
|
19464 | 5927 |
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
|
5928 |
the original receiver is returned unchanged." |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
5929 |
|
9190 | 5930 |
|len s| |
5931 |
||
5932 |
len := self size. |
|
9192 | 5933 |
(len < size) ifTrue:[ |
20874 | 5934 |
s := self species new:size withAll:padCharacter. |
5935 |
s replaceFrom:(size - len) // 2 + 1 with:self. |
|
5936 |
^ s |
|
9192 | 5937 |
] |
5938 |
||
9190 | 5939 |
" |
5940 |
'foo' centerPaddedTo:11 with:$. |
|
5941 |
'fooBar' centerPaddedTo:5 with:$. |
|
5942 |
123 printString centerPaddedTo:10 with:$. |
|
5943 |
(' ' , 123 printString) centerPaddedTo:10 with:$. |
|
5944 |
(Float pi printString) centerPaddedTo:15 with:(Character space) |
|
5945 |
(Float pi printString) centerPaddedTo:15 with:$- |
|
5946 |
(' ' , Float pi class name) centerPaddedTo:15 with:$. |
|
5947 |
" |
|
815
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
5948 |
! |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
5949 |
|
9190 | 5950 |
decimalPaddedTo:size and:afterPeriod at:decimalCharacter |
815
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
5951 |
"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
|
5952 |
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
|
5953 |
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
|
5954 |
to be padded). |
19464 | 5955 |
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
|
5956 |
the original receiver is returned unchanged. |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
5957 |
(sounds complicated ? -> see examples below)." |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
5958 |
|
8874 | 5959 |
^ self |
20874 | 5960 |
decimalPaddedTo:size |
5961 |
and:afterPeriod |
|
5962 |
at:decimalCharacter |
|
5963 |
withLeft:(Character space) |
|
5964 |
right:$0 |
|
815
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
5965 |
|
8874 | 5966 |
" |
5967 |
'123' decimalPaddedTo:10 and:3 at:$. -> ' 123 ' |
|
5968 |
'123' decimalPaddedTo:10 and:3 at:$. -> ' 123.000' |
|
5969 |
'123.' decimalPaddedTo:10 and:3 at:$. -> ' 123.000' |
|
815
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
5970 |
'123.1' decimalPaddedTo:10 and:3 at:$. -> ' 123.100' |
8874 | 5971 |
'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
|
5972 |
'123.123' decimalPaddedTo:10 and:3 at:$. -> ' 123.123' |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
5973 |
" |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
5974 |
|
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
5975 |
"Created: 23.12.1995 / 13:11:52 / cg" |
9192 | 5976 |
! |
5977 |
||
5978 |
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
|
5979 |
"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
|
5980 |
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
|
5981 |
of the period. |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
5982 |
Characters on the left are filled with leftPadChar. |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
5983 |
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
|
5984 |
otherwise, if missing, a decimal point is added and right characters filled with this. |
19464 | 5985 |
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
|
5986 |
the original receiver is returned unchanged. |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
5987 |
(sounds complicated ? -> see examples below)." |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
5988 |
|
5230
655c21ffe6cc
care for negative size of rest-string when decimalPadding
Claus Gittinger <cg@exept.de>
parents:
5221
diff
changeset
|
5989 |
|s idx n rest| |
815
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
5990 |
|
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
5991 |
idx := self indexOf:decimalCharacter. |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
5992 |
idx == 0 ifTrue:[ |
20874 | 5993 |
"/ |
5994 |
"/ no decimal point found; adjust string to the left of the period column |
|
5995 |
"/ |
|
5996 |
rightPadChar isNil ifTrue:[ |
|
5997 |
s := self , (self species new:afterPeriod + 1 withAll:leftPadChar) |
|
5998 |
] ifFalse:[ |
|
5999 |
s:= self , decimalCharacter asString , (self species new:afterPeriod withAll:rightPadChar). |
|
6000 |
]. |
|
815
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
6001 |
] ifFalse:[ |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
6002 |
|
20874 | 6003 |
"/ the number of after-decimalPoint characters |
6004 |
n := self size - idx. |
|
6005 |
rest := afterPeriod - n. |
|
6006 |
rest > 0 ifTrue:[ |
|
6007 |
s := (self species new:rest withAll:(rightPadChar ? leftPadChar)). |
|
6008 |
] ifFalse:[ |
|
6009 |
s := '' |
|
6010 |
]. |
|
6011 |
s := self , s. |
|
815
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
6012 |
]. |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
6013 |
|
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
6014 |
^ s leftPaddedTo:size with:leftPadChar |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
6015 |
|
8874 | 6016 |
" |
6017 |
'123' decimalPaddedTo:10 and:3 at:$. withLeft:(Character space) right:nil -> ' 123 ' |
|
6018 |
'123' decimalPaddedTo:10 and:3 at:$. withLeft:(Character space) right:$0 -> ' 123.000' |
|
6019 |
'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
|
6020 |
'123.1' decimalPaddedTo:10 and:3 at:$. withLeft:(Character space) right:$0 -> ' 123.100' |
8874 | 6021 |
'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
|
6022 |
'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
|
6023 |
" |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
6024 |
|
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
6025 |
"Modified: 23.12.1995 / 13:08:18 / cg" |
9192 | 6026 |
! |
6027 |
||
6028 |
leftPaddedTo:size |
|
6029 |
"return a new string of length size, which contains the receiver |
|
6030 |
right-adjusted (i.e. padded on the left). |
|
6031 |
Characters on the left are filled with spaces. |
|
19464 | 6032 |
If the receiver's size is equal or greater than the length argument, |
9192 | 6033 |
the original receiver is returned unchanged." |
6034 |
||
6035 |
^ self leftPaddedTo:size with:(Character space) |
|
6036 |
||
6037 |
" |
|
6038 |
'foo' leftPaddedTo:10 |
|
6039 |
'fooBar' leftPaddedTo:5 |
|
6040 |
123 printString leftPaddedTo:10 |
|
6041 |
" |
|
6042 |
! |
|
608 | 6043 |
|
6044 |
paddedTo:newSize |
|
19753 | 6045 |
"return a new string consisting of the receiver's characters, |
608 | 6046 |
plus spaces up to length. |
19464 | 6047 |
If the receiver's size is equal or greater than the length argument, |
608 | 6048 |
the original receiver is returned unchanged." |
6049 |
||
6050 |
^ self paddedTo:newSize with:(Character space) |
|
6051 |
||
6052 |
" |
|
8874 | 6053 |
'foo' paddedTo:10 |
6054 |
123 printString paddedTo:10 |
|
15734 | 6055 |
'12345678901234' paddedTo:10 |
608 | 6056 |
" |
64 | 6057 |
! ! |
6058 |
||
9192 | 6059 |
!CharacterArray methodsFor:'printing & storing'! |
608 | 6060 |
|
6061 |
article |
|
17137 | 6062 |
"return an article string for the receiver. |
6063 |
The original code only looked at the first character being a vowel; |
|
6064 |
this has been enhanced by some heuristics - not perfect, still." |
|
608 | 6065 |
|
17241 | 6066 |
|firstChar secondChar thirdChar| |
608 | 6067 |
|
8874 | 6068 |
firstChar := (self at:1) asLowercase. |
5848 | 6069 |
((firstChar isVowel and:[firstChar ~~ $u]) or:[firstChar == $x]) ifTrue:[ |
20874 | 6070 |
^ 'an' |
608 | 6071 |
]. |
17043 | 6072 |
|
6073 |
(self size >= 3) ifTrue:[ |
|
20874 | 6074 |
secondChar := (self at:2) asLowercase. |
6075 |
||
6076 |
"/ may need more here... |
|
6077 |
( #('rb') includes:(String with:firstChar with:secondChar)) ifTrue:[ |
|
6078 |
^ 'an' |
|
6079 |
]. |
|
6080 |
||
6081 |
thirdChar := (self at:3) asLowercase. |
|
6082 |
||
6083 |
(firstChar isVowel not |
|
6084 |
and:[(secondChar isVowel or:[secondChar == $y]) not |
|
6085 |
and:[thirdChar isVowel not ]]) ifTrue:[ |
|
6086 |
"/ exceptions: 3 non-vowels in a row: looks like an abbreviation |
|
6087 |
(self size > 4) ifTrue:[ |
|
6088 |
(firstChar == $s) ifTrue:[ |
|
6089 |
((secondChar == $c and:[thirdChar == $r]) |
|
6090 |
or:[ (secondChar == $t and:[thirdChar == $r]) ]) ifTrue:[ |
|
6091 |
(self at:4) isVowel ifTrue:[ |
|
6092 |
^ 'a' |
|
6093 |
] |
|
6094 |
] |
|
6095 |
]. |
|
6096 |
]. |
|
6097 |
"/ an abbreviation; treat x, s as vowels |
|
6098 |
(firstChar == $x or:[ firstChar == $s ]) ifTrue:[^ 'an']. |
|
6099 |
] |
|
17043 | 6100 |
]. |
608 | 6101 |
^ 'a' |
5848 | 6102 |
|
6103 |
" |
|
20874 | 6104 |
'uboot' article. |
6105 |
'xmas' article. |
|
6106 |
'alarm' article. |
|
6107 |
'baby' article. |
|
6108 |
'sql' article. |
|
6109 |
'scr' article. |
|
6110 |
'screen' article. |
|
6111 |
'scrollbar' article. |
|
6112 |
'scrs' article. |
|
6113 |
'cvs' article. |
|
6114 |
'cvssource' article. |
|
6115 |
'symbol' article. |
|
6116 |
'string' article. |
|
6117 |
'rbparser' article. |
|
19683 | 6118 |
" |
6119 |
||
6120 |
"Modified (comment): / 01-05-2016 / 10:57:25 / cg" |
|
608 | 6121 |
! |
6122 |
||
8049
ccd0e8f26439
printing - migrating towards unicodeSupport in ST-code
Claus Gittinger <cg@exept.de>
parents:
8040
diff
changeset
|
6123 |
basicStoreString |
ccd0e8f26439
printing - migrating towards unicodeSupport in ST-code
Claus Gittinger <cg@exept.de>
parents:
8040
diff
changeset
|
6124 |
"return a String for storing myself" |
ccd0e8f26439
printing - migrating towards unicodeSupport in ST-code
Claus Gittinger <cg@exept.de>
parents:
8040
diff
changeset
|
6125 |
|
ccd0e8f26439
printing - migrating towards unicodeSupport in ST-code
Claus Gittinger <cg@exept.de>
parents:
8040
diff
changeset
|
6126 |
|s n index| |
ccd0e8f26439
printing - migrating towards unicodeSupport in ST-code
Claus Gittinger <cg@exept.de>
parents:
8040
diff
changeset
|
6127 |
|
ccd0e8f26439
printing - migrating towards unicodeSupport in ST-code
Claus Gittinger <cg@exept.de>
parents:
8040
diff
changeset
|
6128 |
n := self occurrencesOf:$'. |
11320 | 6129 |
n ~~ 0 ifTrue:[ |
20874 | 6130 |
s := self species new:(n + 2 + self size). |
6131 |
s at:1 put:$'. |
|
6132 |
index := 2. |
|
6133 |
self do:[:thisChar | |
|
6134 |
(thisChar == $') ifTrue:[ |
|
6135 |
s at:index put:thisChar. |
|
6136 |
index := index + 1. |
|
6137 |
]. |
|
6138 |
s at:index put:thisChar. |
|
6139 |
index := index + 1. |
|
6140 |
]. |
|
6141 |
s at:index put:$'. |
|
6142 |
^ s |
|
9192 | 6143 |
]. |
11320 | 6144 |
|
9192 | 6145 |
^ '''' , self , '''' |
15523 | 6146 |
|
6147 |
" |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
6148 |
'''immutable'' string' asImmutableString basicStoreString |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
6149 |
'immutable string' asImmutableString basicStoreString |
15523 | 6150 |
" |
6151 |
||
6152 |
"Modified: / 14-07-2013 / 19:20:49 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
9192 | 6153 |
! |
8049
ccd0e8f26439
printing - migrating towards unicodeSupport in ST-code
Claus Gittinger <cg@exept.de>
parents:
8040
diff
changeset
|
6154 |
|
11774
6f6e2b71b607
displayOn (inspector's interface) was broken
Claus Gittinger <cg@exept.de>
parents:
11754
diff
changeset
|
6155 |
displayOn:aGCOrStream |
6f6e2b71b607
displayOn (inspector's interface) was broken
Claus Gittinger <cg@exept.de>
parents:
11754
diff
changeset
|
6156 |
"Compatibility |
6f6e2b71b607
displayOn (inspector's interface) was broken
Claus Gittinger <cg@exept.de>
parents:
11754
diff
changeset
|
6157 |
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
|
6158 |
OR: |
6f6e2b71b607
displayOn (inspector's interface) was broken
Claus Gittinger <cg@exept.de>
parents:
11754
diff
changeset
|
6159 |
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
|
6160 |
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
|
6161 |
(although the fallBack is to display its printString ...)" |
6f6e2b71b607
displayOn (inspector's interface) was broken
Claus Gittinger <cg@exept.de>
parents:
11754
diff
changeset
|
6162 |
|
6f6e2b71b607
displayOn (inspector's interface) was broken
Claus Gittinger <cg@exept.de>
parents:
11754
diff
changeset
|
6163 |
"/ what a kludge - Dolphin and Squeak mean: printOn: a stream; |
21540 | 6164 |
"/ old ST80 means: draw-yourself on a GC. |
6165 |
aGCOrStream isStream ifFalse:[ |
|
6166 |
^ super displayOn:aGCOrStream. |
|
6167 |
]. |
|
6168 |
||
6169 |
self storeOn:aGCOrStream. |
|
6170 |
||
6171 |
"Modified: / 22-02-2017 / 16:59:25 / cg" |
|
11774
6f6e2b71b607
displayOn (inspector's interface) was broken
Claus Gittinger <cg@exept.de>
parents:
11754
diff
changeset
|
6172 |
! |
6f6e2b71b607
displayOn (inspector's interface) was broken
Claus Gittinger <cg@exept.de>
parents:
11754
diff
changeset
|
6173 |
|
608 | 6174 |
printOn:aStream |
6175 |
"print the receiver on aStream" |
|
6176 |
||
6177 |
aStream nextPutAll:self |
|
6178 |
! |
|
6179 |
||
6180 |
printString |
|
13079
111e7e456648
Implement display with #displayOn: instead of #displayString
Stefan Vogel <sv@exept.de>
parents:
13073
diff
changeset
|
6181 |
"return a string for printing - that's myself" |
608 | 6182 |
|
6183 |
^ self |
|
3578 | 6184 |
! |
6185 |
||
6186 |
printWithQuotesDoubledOn:aStream |
|
6187 |
"put the raw storeString of myself on aStream" |
|
6188 |
||
6189 |
self do:[:thisChar | |
|
20874 | 6190 |
(thisChar == $') ifTrue:[aStream nextPut:thisChar]. |
6191 |
aStream nextPut:thisChar |
|
3578 | 6192 |
] |
6193 |
||
6194 |
"Modified: / 15.6.1998 / 17:21:17 / cg" |
|
6195 |
"Created: / 15.6.1998 / 17:22:13 / cg" |
|
12171 | 6196 |
! |
6197 |
||
15788 | 6198 |
printXmlQuotedOn:aStream |
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
6199 |
"convert aString to a valid XML string |
15788 | 6200 |
that can be used for attributes, text, comments an PIs |
6201 |
TODO: care for 16bit UNICODE string and escape chars ..." |
|
6202 |
||
6203 |
self do:[:eachChar | |
|
20874 | 6204 |
eachChar == $< ifTrue:[ |
6205 |
aStream nextPutAll:'<' "mapping needed for xml text" |
|
6206 |
] ifFalse:[ eachChar == $& ifTrue:[ |
|
6207 |
aStream nextPutAll:'&' "mapping needed for all" |
|
6208 |
] ifFalse:[ eachChar == $> ifTrue:[ |
|
6209 |
aStream nextPutAll:'>' "mapping needed for comments" |
|
6210 |
] ifFalse:[ eachChar == $' ifTrue:[ |
|
6211 |
aStream nextPutAll:''' "mapping needed for attributes" |
|
6212 |
] ifFalse:[ eachChar == $" ifTrue:[ |
|
6213 |
aStream nextPutAll:'"' "mapping needed for attributes" |
|
6214 |
] ifFalse:[ |
|
6215 |
|codePoint| |
|
6216 |
codePoint := eachChar codePoint. |
|
6217 |
(codePoint < 16r20 or:[codePoint >= 16r7F]) ifTrue:[ |
|
6218 |
aStream nextPutAll:'&#'. |
|
6219 |
codePoint printOn:aStream. |
|
6220 |
aStream nextPut:$;. |
|
6221 |
] ifFalse:[ |
|
6222 |
aStream nextPut:eachChar |
|
6223 |
]]]]]] |
|
15788 | 6224 |
]. |
6225 |
! |
|
6226 |
||
6227 |
printXmlTextQuotedOn:aStream |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
6228 |
"convert aString to a valid XML string |
15788 | 6229 |
that can be used for XML text. |
6230 |
Here line formatting characters are not escaped. |
|
6231 |
TODO: care for 16bit UNICODE string and escape chars ..." |
|
6232 |
||
6233 |
self do:[:eachChar | |
|
20874 | 6234 |
eachChar == $< ifTrue:[ |
6235 |
aStream nextPutAll:'<' "mapping needed for xml text" |
|
6236 |
] ifFalse:[ eachChar == $& ifTrue:[ |
|
6237 |
aStream nextPutAll:'&' "mapping needed for all" |
|
15788 | 6238 |
"/ ] ifFalse:[ eachChar == $> ifTrue:[ |
6239 |
"/ aStream nextPutAll:'>' "mapping needed for comments" |
|
6240 |
"/ ] ifFalse:[ eachChar == $' ifTrue:[ |
|
6241 |
"/ aStream nextPutAll:''' "mapping needed for attributes" |
|
6242 |
"/ ] ifFalse:[ eachChar == $" ifTrue:[ |
|
6243 |
"/ aStream nextPutAll:'"' "mapping needed for attributes" |
|
20874 | 6244 |
] ifFalse:[ |
6245 |
|codePoint| |
|
6246 |
codePoint := eachChar codePoint. |
|
6247 |
((codePoint < 16r20 and:[codePoint ~~ 9 and:[codePoint ~~ 10 and:[codePoint ~~ 13]]]) |
|
6248 |
or:[codePoint >= 16r7F]) ifTrue:[ |
|
6249 |
aStream nextPutAll:'&#'. |
|
6250 |
codePoint printOn:aStream. |
|
6251 |
aStream nextPut:$;. |
|
6252 |
] ifFalse:[ |
|
6253 |
aStream nextPut:eachChar |
|
6254 |
]]]"/]]] |
|
15788 | 6255 |
]. |
6256 |
! |
|
6257 |
||
6258 |
xmlQuotedPrintString |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
6259 |
"convert aString to a valid XML string |
15788 | 6260 |
that can be used for attributes, text, comments an PIs |
6261 |
TODO: care for 16bit UNICODE string and escape chars ..." |
|
6262 |
||
6263 |
^ String streamContents:[:s| |
|
20874 | 6264 |
self printXmlQuotedOn:s |
15788 | 6265 |
]. |
9192 | 6266 |
! ! |
6267 |
||
11955 | 6268 |
|
9192 | 6269 |
!CharacterArray methodsFor:'queries'! |
608 | 6270 |
|
4429 | 6271 |
bitsPerCharacter |
18662 | 6272 |
"return the underlying string's bitsPerCharacter |
4429 | 6273 |
(i.e. is it a regular String or a TwoByteString)" |
6274 |
||
8457 | 6275 |
|string max| |
6276 |
||
6277 |
(string := self string) ~~ self ifTrue:[ |
|
20874 | 6278 |
^ string bitsPerCharacter |
8457 | 6279 |
]. |
6280 |
||
6281 |
max := 8. |
|
6282 |
self do:[:eachCharacter | |
|
20874 | 6283 |
max := max max:(eachCharacter bitsPerCharacter) |
9192 | 6284 |
]. |
6285 |
^ max |
|
6286 |
||
6287 |
" |
|
6288 |
'hello' bitsPerCharacter |
|
6289 |
'hello' asText allBold bitsPerCharacter |
|
6290 |
" |
|
6291 |
! |
|
8457 | 6292 |
|
19757 | 6293 |
bytesPerCharacter |
6294 |
"return the underlying string's required |
|
6295 |
bytesPerCharacter |
|
6296 |
(i.e. is it a regular String or a TwoByteString)" |
|
6297 |
||
6298 |
|string max| |
|
6299 |
||
6300 |
(string := self string) ~~ self ifTrue:[ |
|
20874 | 6301 |
^ string bytesPerCharacter |
19757 | 6302 |
]. |
6303 |
||
6304 |
max := 1. |
|
6305 |
self do:[:eachCharacter | |
|
20874 | 6306 |
max := max max:(eachCharacter bytesPerCharacter) |
19757 | 6307 |
]. |
6308 |
^ max |
|
6309 |
||
6310 |
" |
|
6311 |
'hello' bytesPerCharacter |
|
6312 |
'hello' asUnicode16String bytesPerCharacter |
|
6313 |
'hello' asText allBold bytesPerCharacter |
|
6314 |
" |
|
6315 |
! |
|
6316 |
||
18967 | 6317 |
containsNon8BitElements |
6318 |
"return true, if the underlying string contains elements larger than a single byte" |
|
6319 |
||
21322 | 6320 |
|string sz "{ Class:SmallInteger }"| |
6321 |
||
6322 |
(string := self string) ~~ self ifTrue:[ |
|
6323 |
^ string containsNon8BitElements |
|
6324 |
]. |
|
18967 | 6325 |
sz := self size. |
6326 |
1 to:sz do:[:idx| |
|
20874 | 6327 |
(self at:idx) codePoint > 16rFF ifTrue:[ |
6328 |
^ true. |
|
6329 |
]. |
|
18967 | 6330 |
]. |
6331 |
^ false. |
|
6332 |
! |
|
6333 |
||
10903 | 6334 |
continuesWith:aString startingAt:startIndex |
6335 |
"return true, if the receiver beginning at startIndex |
|
22783 | 6336 |
contains the characters in aString. |
6337 |
Returns false if the search reaches the end of the receiver |
|
6338 |
before all characters have been compared." |
|
10903 | 6339 |
|
6340 |
|sz "{Class: SmallInteger }" |
|
6341 |
idx "{Class: SmallInteger }"| |
|
6342 |
||
6343 |
sz := aString size. |
|
22783 | 6344 |
(startIndex + sz - 1) > self size ifTrue:[^ false]. |
6345 |
||
10903 | 6346 |
idx := startIndex. |
22783 | 6347 |
|
10903 | 6348 |
1 to:sz do:[:i | |
20874 | 6349 |
(self at:idx) ~~ (aString at:i) ifTrue:[^ false]. |
6350 |
idx := idx + 1 |
|
10903 | 6351 |
]. |
6352 |
^ true |
|
6353 |
||
6354 |
" |
|
6355 |
'hello world' continuesWith:'world' startingAt:6 |
|
6356 |
'hello world' continuesWith:'world' startingAt:7 |
|
22783 | 6357 |
'hello' continuesWith:'llo' startingAt:3 |
6358 |
'hello' continuesWith:'llow' startingAt:3 |
|
10903 | 6359 |
" |
6360 |
||
6361 |
"Created: 12.5.1996 / 15:46:40 / cg" |
|
6362 |
"Modified: 26.7.1996 / 19:08:36 / cg" |
|
6363 |
! |
|
6364 |
||
6365 |
countWords |
|
6366 |
"return the number of words, which are separated by separators" |
|
6367 |
||
6368 |
|tally "{ Class: SmallInteger }" |
|
6369 |
start "{ Class: SmallInteger }" |
|
6370 |
mySize "{ Class: SmallInteger }" |
|
6371 |
stop ch| |
|
6372 |
||
6373 |
tally := 0. |
|
6374 |
start := 1. |
|
6375 |
mySize := self size. |
|
6376 |
[start <= mySize] whileTrue:[ |
|
20874 | 6377 |
ch := self at:start. |
6378 |
ch isSeparator ifTrue:[ |
|
6379 |
start := start + 1 |
|
6380 |
] ifFalse:[ |
|
6381 |
stop := self indexOfSeparatorStartingAt:start. |
|
6382 |
(stop == 0) ifTrue:[ |
|
6383 |
stop := mySize + 1 |
|
6384 |
]. |
|
6385 |
tally := tally + 1. |
|
6386 |
start := stop |
|
6387 |
] |
|
10903 | 6388 |
]. |
6389 |
^ tally |
|
6390 |
||
6391 |
" |
|
6392 |
'hello world isnt this nice' countWords' |
|
6393 |
" |
|
6394 |
! |
|
6395 |
||
11788 | 6396 |
defaultElement |
6397 |
^ Character space |
|
6398 |
! |
|
6399 |
||
608 | 6400 |
encoding |
1311 | 6401 |
"return the strings encoding, as a symbol. |
7982 | 6402 |
Here, by default, we assume unicode-encoding. |
20006 | 6403 |
Notice, that ISO-8859-1 is a true subset of unicode, |
8874 | 6404 |
and that singleByteStrings are therefore both unicode AND |
7982 | 6405 |
8859-1 encoded." |
6406 |
||
14168 | 6407 |
^ #unicode |
608 | 6408 |
! |
6409 |
||
1375
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
6410 |
hasChangeOfEmphasis |
14405
1d0d99a95bba
comment/format in:7 methods
Claus Gittinger <cg@exept.de>
parents:
14168
diff
changeset
|
6411 |
"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
|
6412 |
i.e. any non-normal (=emphasized) characters" |
1d0d99a95bba
comment/format in:7 methods
Claus Gittinger <cg@exept.de>
parents:
14168
diff
changeset
|
6413 |
|
1375
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
6414 |
^ false |
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
6415 |
|
1381 | 6416 |
"Created: 12.5.1996 / 12:31:39 / cg" |
1375
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
6417 |
! |
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
6418 |
|
7995
8ddbb403d67b
+hasIcon and hasImage for LabelAndIcon compatibility
Claus Gittinger <cg@exept.de>
parents:
7991
diff
changeset
|
6419 |
hasIcon |
9191 | 6420 |
"for LabelAndIcon compatibility" |
6421 |
||
9192 | 6422 |
^ false |
6423 |
! |
|
6424 |
||
6425 |
hasImage |
|
6426 |
"for LabelAndIcon compatibility" |
|
6427 |
||
6428 |
^ false |
|
6429 |
! |
|
7995
8ddbb403d67b
+hasIcon and hasImage for LabelAndIcon compatibility
Claus Gittinger <cg@exept.de>
parents:
7991
diff
changeset
|
6430 |
|
1388 | 6431 |
heightOn:aGC |
19464 | 6432 |
"return the size of the receiver in device units if displayed on aGC" |
1388 | 6433 |
|
20221 | 6434 |
^ aGC deviceFont heightOf:self "font is already a device font" |
1388 | 6435 |
|
6436 |
" |
|
8874 | 6437 |
'hello world' heightOn:(View new) |
1388 | 6438 |
" |
6439 |
||
6440 |
"Created: 12.5.1996 / 20:09:29 / cg" |
|
6441 |
"Modified: 12.5.1996 / 20:32:05 / cg" |
|
6442 |
! |
|
6443 |
||
11828 | 6444 |
isLowercaseFirst |
6445 |
"return true, if the first character is a lowercase character." |
|
6446 |
||
6447 |
^ self size ~~ 0 and:[ self first isLowercase ] |
|
6448 |
||
6449 |
" |
|
12177 | 6450 |
'helloWorld' isLowercaseFirst |
6451 |
'HelloWorld' isLowercaseFirst |
|
11828 | 6452 |
" |
6453 |
! |
|
6454 |
||
6455 |
isUppercaseFirst |
|
6456 |
"return true, if the first character is an uppercase character." |
|
6457 |
||
6458 |
^ self size ~~ 0 and:[ self first isUppercase ] |
|
6459 |
||
6460 |
" |
|
12177 | 6461 |
'helloWorld' isUppercaseFirst |
6462 |
'HelloWorld' isUppercaseFirst |
|
11828 | 6463 |
" |
6464 |
! |
|
6465 |
||
15773 | 6466 |
keywords |
6467 |
"assuming the receiver is a keyword message selector, |
|
6468 |
return the individual keywords (i.e. break it up at colons) |
|
6469 |
and return these as a collection. |
|
6470 |
For binary and unary selectors, the result may be nonsense (an array containing the receiver)." |
|
6471 |
||
6472 |
|coll s part| |
|
6473 |
||
6474 |
coll := OrderedCollection new. |
|
6475 |
s := ReadStream on:self. |
|
6476 |
[s atEnd] whileFalse:[ |
|
20874 | 6477 |
part := s through:$:. |
6478 |
coll add:part |
|
15773 | 6479 |
]. |
6480 |
^ coll asArray |
|
6481 |
||
6482 |
" |
|
6483 |
#at:put: keywords |
|
6484 |
#at: keywords |
|
6485 |
#+ keywords |
|
6486 |
#size keywords |
|
6487 |
" |
|
6488 |
||
6489 |
"Modified (Comment): / 30-06-2011 / 17:46:21 / cg" |
|
6490 |
! |
|
6491 |
||
9190 | 6492 |
knownAsSymbol |
6493 |
"for now, only single character strings are allowed as symbols. |
|
6494 |
This method is redefined in String." |
|
6495 |
||
6496 |
^ false |
|
6497 |
! |
|
6498 |
||
6499 |
leftIndent |
|
6500 |
"if the receiver starts with spaces, return the number of spaces |
|
6501 |
at the left - otherwise, return 0. |
|
19464 | 6502 |
If the receiver consists of spaces only, return the receiver's size." |
9192 | 6503 |
|
6504 |
|index "{Class: SmallInteger }" |
|
6505 |
end "{Class: SmallInteger }"| |
|
6506 |
||
6507 |
index := 1. |
|
6508 |
end := self size. |
|
6509 |
[index <= end] whileTrue:[ |
|
20874 | 6510 |
(self at:index) isSeparator ifFalse:[^ index - 1]. |
6511 |
index := index + 1 |
|
8350 | 6512 |
]. |
9192 | 6513 |
^ end |
6514 |
||
6515 |
" |
|
6516 |
' hello' leftIndent |
|
6517 |
'foo ' leftIndent |
|
9190 | 6518 |
' ' leftIndent |
6519 |
" |
|
6520 |
||
6521 |
"Modified: 20.4.1996 / 19:28:43 / cg" |
|
8350 | 6522 |
! |
6523 |
||
10437 | 6524 |
nameSpaceSelectorParts |
6525 |
"Answer the namespace and baseSelector parts of a namespace selector. |
|
6526 |
Namespace selectors are those generated by sends from a method in a different |
|
6527 |
namespace; they are prefixed by ':'<ns>'::'. |
|
6528 |
You cannot easily change this algorithm here, as it is also known by the VM's lookup function. |
|
6529 |
Experimental" |
|
6530 |
||
6531 |
|nsPart selPart idx| |
|
6532 |
||
6533 |
(self at:1) == $: ifFalse:[^ Array with:'' with:self]. |
|
6534 |
idx := self indexOf:$: startingAt:3. |
|
6535 |
idx == 0 ifTrue:[^ Array with:'' with:self]. |
|
6536 |
||
6537 |
(idx+2 > self size) ifTrue:[^ Array with:'' with:self]. |
|
6538 |
(self at:idx+1) == $: ifFalse:[^ Array with:'' with:self]. |
|
6539 |
nsPart := self copyFrom:2 to:idx-1. |
|
6540 |
selPart := self copyFrom:idx+2. |
|
6541 |
^ Array with:nsPart with:selPart |
|
6542 |
||
6543 |
"test: |
|
10703
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
6544 |
self assert:('+' nameSpaceSelectorParts) = #('' '+'). |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
6545 |
self assert:(':+' nameSpaceSelectorParts) = #('' ':+'). |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
6546 |
self assert:(':Foo:+' nameSpaceSelectorParts) = #('' ':Foo:+'). |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
6547 |
|
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
6548 |
self assert:(':Foo::+' nameSpaceSelectorParts) = #('Foo' '+'). |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
6549 |
self assert:(':Foo::bar:baz:' nameSpaceSelectorParts) = #('Foo' 'bar:baz:'). |
10437 | 6550 |
" |
6551 |
||
6552 |
"Created: / 05-03-2007 / 17:16:58 / cg" |
|
6553 |
"Modified: / 06-03-2007 / 11:51:15 / cg" |
|
6554 |
! |
|
6555 |
||
21283 | 6556 |
speciesForSubcollection |
6557 |
"answer the class, when splitting instances into subcollections" |
|
6558 |
||
6559 |
^ StringCollection |
|
6560 |
||
6561 |
"Created: / 24-01-2017 / 18:54:18 / stefan" |
|
6562 |
! |
|
6563 |
||
9190 | 6564 |
stringSpecies |
8874 | 6565 |
"return the underlying strings bitsPerCharacter |
8037
8cce25b1059c
care for wide chars in asXXXFirst
Claus Gittinger <cg@exept.de>
parents:
8032
diff
changeset
|
6566 |
(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
|
6567 |
|
8cce25b1059c
care for wide chars in asXXXFirst
Claus Gittinger <cg@exept.de>
parents:
8032
diff
changeset
|
6568 |
|string| |
8cce25b1059c
care for wide chars in asXXXFirst
Claus Gittinger <cg@exept.de>
parents:
8032
diff
changeset
|
6569 |
|
15523 | 6570 |
string := self string. |
6571 |
string == self ifTrue:[^ self species]. |
|
8037
8cce25b1059c
care for wide chars in asXXXFirst
Claus Gittinger <cg@exept.de>
parents:
8032
diff
changeset
|
6572 |
^ string stringSpecies |
8cce25b1059c
care for wide chars in asXXXFirst
Claus Gittinger <cg@exept.de>
parents:
8032
diff
changeset
|
6573 |
|
8cce25b1059c
care for wide chars in asXXXFirst
Claus Gittinger <cg@exept.de>
parents:
8032
diff
changeset
|
6574 |
" |
8874 | 6575 |
'hello' stringSpecies |
9192 | 6576 |
'hello' asText allBold stringSpecies |
6577 |
" |
|
6578 |
! |
|
8037
8cce25b1059c
care for wide chars in asXXXFirst
Claus Gittinger <cg@exept.de>
parents:
8032
diff
changeset
|
6579 |
|
6653 | 6580 |
widthFrom:startIndex to:endIndex on:aGC |
19464 | 6581 |
"return ths size of part of the receiver in device units if displayed on aGC" |
6653 | 6582 |
|
20221 | 6583 |
^ aGC deviceFont widthOf:self from:startIndex to:endIndex |
6653 | 6584 |
|
6585 |
" |
|
8874 | 6586 |
'hello world' widthFrom:1 to:5 on:(View new) |
6587 |
'hello' widthOn:(View new) |
|
6653 | 6588 |
" |
9192 | 6589 |
! |
6653 | 6590 |
|
1388 | 6591 |
widthOn:aGC |
19464 | 6592 |
"return ths size of the receiver in device units if displayed on aGC" |
1388 | 6593 |
|
20221 | 6594 |
^ aGC deviceFont widthOf:self "font is already a device font" |
1388 | 6595 |
|
6596 |
" |
|
8874 | 6597 |
'hello world' widthOn:(View new) |
1388 | 6598 |
" |
6599 |
||
6600 |
"Created: 12.5.1996 / 20:09:29 / cg" |
|
2554 | 6601 |
"Modified: 17.4.1997 / 12:50:23 / cg" |
608 | 6602 |
! ! |
6603 |
||
22924 | 6604 |
|
608 | 6605 |
!CharacterArray methodsFor:'special string converting'! |
6606 |
||
16759 | 6607 |
asUnixFilenameString |
6608 |
"return a new string consisting of receiver's characters |
|
6609 |
with all \-characters replaced by /-characters. |
|
6610 |
If there are no backslashes, return the original" |
|
6611 |
||
6612 |
^ self copyReplaceAll:$\ with:$/ ifNone:self |
|
6613 |
||
6614 |
" |
|
6615 |
'hello\world' asUnixFilenameString |
|
6616 |
" |
|
6617 |
||
6618 |
"Modified: / 18.7.1998 / 22:53:02 / cg" |
|
6619 |
! |
|
6620 |
||
14768 | 6621 |
expandPlaceholders:escapeCharacter with:argArrayOrDictionary |
6622 |
"this is the generic version of the old %-escaping method, allowing for an arbitrary |
|
6623 |
escape character to be used (typically $$ or $% are effectively used). |
|
6624 |
Return a copy of the receiver, where all %i escapes are |
|
6625 |
replaced by corresponding arguments' printStrings from the argArrayOrDictionary. |
|
6626 |
I.e. 'hello %1; how is %2' expandPlaceholdersWith:#('world' 'this') results |
|
6627 |
in the new string 'hello world; how is this'. |
|
6628 |
As an extension, the argument may also be a dictionary, providing |
|
6629 |
values for symbolic keys. |
|
6630 |
In this case, %a .. %z and %(...) are also allowed. |
|
6631 |
(%1..%9 require a numeric key in the dictionary, however) |
|
6632 |
To get a '%' character, use a '%%'-escape. |
|
6633 |
To get an integer-indexed placeHolder followed by another digit, |
|
6634 |
or an index > 9, you must use %(digit). |
|
6635 |
See also bindWith:... for VisualAge compatibility." |
|
6636 |
||
6637 |
|stream| |
|
6638 |
||
18269 | 6639 |
stream := (TextStream ? CharacterWriteStream) on:(self species uninitializedNew:self size + 20). |
23067 | 6640 |
self expandPlaceholders:escapeCharacter with:argArrayOrDictionary ignoreNumericEscapes:false on:stream. |
14768 | 6641 |
^ stream contents. |
6642 |
||
6643 |
||
6644 |
" |
|
6645 |
'hello %1' expandPlaceholdersWith:#('world') |
|
6646 |
'hello %1; how is %2' expandPlaceholdersWith:#('world' 'this') |
|
6647 |
'hello %2; how is %1' expandPlaceholdersWith:#('world' 'this') |
|
6648 |
'%1 plus %2 gives %3 ' expandPlaceholdersWith:#(4 5 9) |
|
6649 |
'%%(1)0 gives %(1)0' expandPlaceholdersWith:#(123) |
|
6650 |
'%%10 gives %10' expandPlaceholdersWith:#(123) |
|
6651 |
'%%(10) gives %(10)' expandPlaceholdersWith:#(123) |
|
6652 |
'%test gives %1' expandPlaceholdersWith:#(123) |
|
6653 |
'bla %1 bla' expandPlaceholdersWith:{ 'hello' allBold } |
|
6654 |
'bla %1 bla' expandPlaceholdersWith:{ 'hello' } |
|
6655 |
" |
|
6656 |
||
6657 |
" |
|
6658 |
|dict| |
|
6659 |
||
6660 |
dict := Dictionary new. |
|
6661 |
dict at:1 put:'one'. |
|
6662 |
dict at:$a put:'AAAAA'. |
|
6663 |
dict at:$b put:[ Time now ]. |
|
6664 |
dict at:'foo' put:[ Date today ]. |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
6665 |
'hello $1 %a $b %(foo) $foo ' expandPlaceholders:$$ with:dict. |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
6666 |
'hello $1 %a $b %(foo) $foo ' expandPlaceholders:$% with:dict. |
14768 | 6667 |
" |
6668 |
||
23067 | 6669 |
"Modified: / 01-07-1997 / 00:53:24 / cg" |
6670 |
"Modified: / 14-06-2018 / 11:46:18 / Claus Gittinger" |
|
6671 |
! |
|
6672 |
||
6673 |
expandPlaceholders:escapeCharacter with:argArrayOrDictionary ignoreNumericEscapes:ignoreNumericEscapes on:aStream |
|
14765 | 6674 |
"this is the generic version of the old %-escaping method, allowing for an arbitrary |
6675 |
escape character to be used (typically $$ or $% are effectively used). |
|
18425 | 6676 |
|
14765 | 6677 |
Write the receiver to aStream, where all %i escapes are |
23067 | 6678 |
replaced by corresponding arguments' printStrings from the argArray. |
14765 | 6679 |
I.e. 'hello %1; how is %2' expandPlaceholdersWith:#('world' 'this') results |
6680 |
in the new string 'hello world; how is this'. |
|
18425 | 6681 |
|
17192 | 6682 |
As an extension, the argument may also be a dictionary, providing values for symbolic keys. |
14765 | 6683 |
In this case, %a .. %z and %(...) are also allowed. |
6684 |
(%1..%9 require a numeric key in the dictionary, however) |
|
23067 | 6685 |
|
6686 |
Also, values in argArrayOrDictionary may be blocks. |
|
18425 | 6687 |
|
14765 | 6688 |
To get a '%' character, use a '%%'-escape. |
6689 |
To get an integer-indexed placeHolder followed by another digit, |
|
6690 |
or an index > 9, you must use %(digit). |
|
18425 | 6691 |
|
14765 | 6692 |
See also bindWith:... for VisualAge compatibility. |
23067 | 6693 |
Use %<cr> to insert a CR and %<tab> to insert a TAB. |
6694 |
||
6695 |
ignoreNumericEscapes controls if %<nr> escapes are expanded or not. |
|
6696 |
This is required for Windows batch-script expansion, where %<nr> should be left |
|
6697 |
unchanged." |
|
14765 | 6698 |
|
22929 | 6699 |
|next v key numericKey |
14765 | 6700 |
idx "{ SmallInteger }" |
6701 |
idx2 "{ SmallInteger }" |
|
6702 |
start "{ SmallInteger }" |
|
6703 |
stop "{ SmallInteger }"| |
|
6704 |
||
6705 |
stop := self size. |
|
6706 |
start := 1. |
|
6707 |
[start <= stop] whileTrue:[ |
|
20874 | 6708 |
idx := self indexOf:escapeCharacter startingAt:start. |
6709 |
(idx == 0 or:[idx == stop]) ifTrue:[ |
|
6710 |
aStream nextPutAll:self startingAt:start to:stop. |
|
6711 |
^ self. |
|
6712 |
]. |
|
6713 |
"found an escapeCharacter" |
|
6714 |
aStream nextPutAll:self startingAt:start to:(idx - 1). |
|
6715 |
next := self at:(idx + 1). |
|
6716 |
(next == escapeCharacter) ifTrue:[ |
|
6717 |
aStream nextPut:escapeCharacter. |
|
6718 |
] ifFalse:[ |
|
6719 |
next == $< ifTrue:[ |
|
6720 |
idx2 := self indexOf:$> startingAt:idx+2. |
|
6721 |
key := self copyFrom:idx+2 to:idx2-1. |
|
6722 |
idx := idx2 - 1. |
|
6723 |
key := key asSymbolIfInterned. |
|
6724 |
(#(cr tab nl return lf ff null) includesIdentical:key) ifTrue:[ |
|
6725 |
aStream nextPut:(Character perform:key). |
|
6726 |
]. |
|
6727 |
] ifFalse:[ |
|
23069 | 6728 |
(next isDigit and:[ignoreNumericEscapes not]) ifTrue:[ |
20874 | 6729 |
v := argArrayOrDictionary at:(next digitValue) ifAbsent:'' |
6730 |
] ifFalse:[ |
|
6731 |
next == $( ifTrue:[ |
|
6732 |
idx2 := self indexOf:$) startingAt:idx+2. |
|
6733 |
key := self copyFrom:idx+2 to:idx2-1. |
|
6734 |
idx := idx2 - 1. |
|
22929 | 6735 |
|
6736 |
(key isNumeric |
|
6737 |
and:[(numericKey := Integer readFrom:key onError:nil) notNil |
|
6738 |
and:[(argArrayOrDictionary includesKey:numericKey)]]) ifTrue:[ |
|
6739 |
v := argArrayOrDictionary at:numericKey ifAbsent:'' |
|
6740 |
] ifFalse:[ |
|
20874 | 6741 |
(argArrayOrDictionary includesKey:key) ifTrue:[ |
6742 |
v := argArrayOrDictionary at:key |
|
6743 |
] ifFalse:[ |
|
22929 | 6744 |
key := key asSymbolIfInternedOrSelf. |
6745 |
(argArrayOrDictionary includesKey:key) ifTrue:[ |
|
6746 |
v := argArrayOrDictionary at:key |
|
20874 | 6747 |
] ifFalse:[ |
22929 | 6748 |
(key size == 1 and:[ argArrayOrDictionary includesKey:(key at:1)]) ifTrue:[ |
6749 |
v := argArrayOrDictionary at:(key at:1) |
|
6750 |
] ifFalse:[ |
|
6751 |
v := '' |
|
6752 |
] |
|
6753 |
]. |
|
20874 | 6754 |
]. |
6755 |
]. |
|
6756 |
] ifFalse:[ |
|
6757 |
(next isLetter and:[argArrayOrDictionary isSequenceable not "is a Dictionary"]) ifTrue:[ |
|
6758 |
"so next is a non-numeric single character." |
|
6759 |
v := argArrayOrDictionary |
|
6760 |
at:next |
|
6761 |
ifAbsent:[ |
|
21126 | 6762 |
"try symbol or string instead of character" |
20874 | 6763 |
argArrayOrDictionary |
21128 | 6764 |
at:next asString asSymbolIfInternedOrSelf |
20874 | 6765 |
ifAbsent:[String with:escapeCharacter with:next]. |
6766 |
]. |
|
6767 |
] ifFalse:[ |
|
6768 |
v := String with:$% with:next. |
|
6769 |
]. |
|
6770 |
] |
|
6771 |
]. |
|
6772 |
"/ v notNil ifTrue:[ |
|
6773 |
v isBlock ifTrue:[ |
|
6774 |
v := v value |
|
6775 |
]. |
|
6776 |
||
6777 |
v printOn:aStream. |
|
6778 |
"/ ]. |
|
6779 |
] |
|
6780 |
]. |
|
6781 |
start := idx + 2 |
|
14765 | 6782 |
]. |
6783 |
||
6784 |
" |
|
6785 |
String streamContents:[:s| |
|
20874 | 6786 |
'hello %1' expandPlaceholders:$% with:#('world') on:s. |
6787 |
s cr. |
|
6788 |
'hello $1; how is $2' expandPlaceholders:$$ with:#('world' 'this') on:s. |
|
6789 |
s cr. |
|
6790 |
'hello %2; how is %1' expandPlaceholders:$% with:#('world' 'this') on:s. |
|
6791 |
s cr. |
|
6792 |
'%1 plus %2 gives %3 ' expandPlaceholders:$% with:#(4 5 9) on:s. |
|
6793 |
s cr. |
|
6794 |
'%%(1)0 gives %(1)0' expandPlaceholders:$% with:#(123) on:s. |
|
6795 |
s cr. |
|
6796 |
'%%10 gives %10' expandPlaceholders:$% with:#(123) on:s. |
|
6797 |
s cr. |
|
6798 |
'%%(10) gives %(10) %<cr>%<tab>next line' expandPlaceholders:$% with:#(123) on:s. |
|
6799 |
s cr. |
|
6800 |
'%%test gives %test' expandPlaceholders:$% with:#(123) on:s. |
|
6801 |
s cr. |
|
6802 |
'|%%<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
|
6803 |
] |
14765 | 6804 |
" |
6805 |
||
6806 |
" |
|
6807 |
|dict| |
|
6808 |
||
6809 |
dict := Dictionary new. |
|
6810 |
dict at:1 put:'one'. |
|
6811 |
dict at:$a put:'AAAAA'. |
|
6812 |
dict at:$b put:[ Time now ]. |
|
6813 |
String streamContents:[:s| |
|
20874 | 6814 |
'hello $1 $a $b' expandPlaceholders:$$ with:dict on:s. |
14765 | 6815 |
]. |
6816 |
" |
|
6817 |
||
18425 | 6818 |
"using blocks: |
6819 |
|dict| |
|
6820 |
||
6821 |
dict := Dictionary new. |
|
6822 |
dict at:'time' put:[Time now printString]. |
|
6823 |
dict at:'date' put:[Date today printString]. |
|
6824 |
String streamContents:[:s| |
|
20874 | 6825 |
'it is $(time) $(date)' expandPlaceholders:$$ with:dict on:s. |
18425 | 6826 |
]. |
6827 |
" |
|
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
6828 |
|
23067 | 6829 |
"Created: / 14-06-2018 / 11:44:08 / Claus Gittinger" |
6830 |
! |
|
6831 |
||
6832 |
expandPlaceholders:escapeCharacter with:argArrayOrDictionary on:aStream |
|
6833 |
"this is the generic version of the old %-escaping method, allowing for an arbitrary |
|
6834 |
escape character to be used (typically $$ or $% are effectively used). |
|
6835 |
||
6836 |
Write the receiver to aStream, where all %i escapes are |
|
6837 |
replaced by corresponding arguments' printStrings from the argArrayOrDictionary. |
|
6838 |
I.e. 'hello %1; how is %2' expandPlaceholdersWith:#('world' 'this') results |
|
6839 |
in the new string 'hello world; how is this'. |
|
6840 |
||
6841 |
As an extension, the argument may also be a dictionary, providing values for symbolic keys. |
|
6842 |
In this case, %a .. %z and %(...) are also allowed. |
|
6843 |
(%1..%9 require a numeric key in the dictionary, however) |
|
6844 |
Also, the values in argArrayOrDictionary may be blocks. |
|
6845 |
||
6846 |
To get a '%' character, use a '%%'-escape. |
|
6847 |
To get an integer-indexed placeHolder followed by another digit, |
|
6848 |
or an index > 9, you must use %(digit). |
|
6849 |
||
6850 |
See also bindWith:... for VisualAge compatibility. |
|
6851 |
Use %<cr> to insert a CR and %<tab> to insert a TAB." |
|
6852 |
||
6853 |
^ self |
|
6854 |
expandPlaceholders:escapeCharacter |
|
6855 |
with:argArrayOrDictionary |
|
6856 |
ignoreNumericEscapes:false |
|
6857 |
on:aStream |
|
6858 |
||
6859 |
" |
|
6860 |
String streamContents:[:s| |
|
6861 |
'hello %1' expandPlaceholders:$% with:#('world') on:s. |
|
6862 |
s cr. |
|
6863 |
'hello $1; how is $2' expandPlaceholders:$$ with:#('world' 'this') on:s. |
|
6864 |
s cr. |
|
6865 |
'hello %2; how is %1' expandPlaceholders:$% with:#('world' 'this') on:s. |
|
6866 |
s cr. |
|
6867 |
'%1 plus %2 gives %3 ' expandPlaceholders:$% with:#(4 5 9) on:s. |
|
6868 |
s cr. |
|
6869 |
'%%(1)0 gives %(1)0' expandPlaceholders:$% with:#(123) on:s. |
|
6870 |
s cr. |
|
6871 |
'%%10 gives %10' expandPlaceholders:$% with:#(123) on:s. |
|
6872 |
s cr. |
|
6873 |
'%%(10) gives %(10) %<cr>%<tab>next line' expandPlaceholders:$% with:#(123) on:s. |
|
6874 |
s cr. |
|
6875 |
'%%test gives %test' expandPlaceholders:$% with:#(123) on:s. |
|
6876 |
s cr. |
|
6877 |
'|%%<tab>|%%1|%%<cr>| gives |%<tab>|%1|%<cr>|' expandPlaceholders:$% with:#(foo) on:s. |
|
6878 |
] |
|
6879 |
" |
|
6880 |
||
6881 |
" |
|
6882 |
|dict| |
|
6883 |
||
6884 |
dict := Dictionary new. |
|
6885 |
dict at:1 put:'one'. |
|
6886 |
dict at:$a put:'AAAAA'. |
|
6887 |
dict at:$b put:[ Time now ]. |
|
6888 |
String streamContents:[:s| |
|
6889 |
'hello $1 $a $b' expandPlaceholders:$$ with:dict on:s. |
|
6890 |
]. |
|
6891 |
" |
|
6892 |
||
6893 |
"using blocks: |
|
6894 |
|dict| |
|
6895 |
||
6896 |
dict := Dictionary new. |
|
6897 |
dict at:'time' put:[Time now printString]. |
|
6898 |
dict at:'date' put:[Date today printString]. |
|
6899 |
String streamContents:[:s| |
|
6900 |
'it is $(time) $(date)' expandPlaceholders:$$ with:dict on:s. |
|
6901 |
]. |
|
6902 |
" |
|
6903 |
||
14765 | 6904 |
"Modified: / 18-11-2010 / 15:43:28 / cg" |
23067 | 6905 |
"Modified: / 14-06-2018 / 11:44:32 / Claus Gittinger" |
14765 | 6906 |
! |
6907 |
||
9192 | 6908 |
expandPlaceholdersWith:argArrayOrDictionary |
608 | 6909 |
"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
|
6910 |
replaced by corresponding arguments' printStrings from the argArrayOrDictionary. |
608 | 6911 |
I.e. 'hello %1; how is %2' expandPlaceholdersWith:#('world' 'this') results |
6912 |
in the new string 'hello world; how is this'. |
|
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
6913 |
|
4410
2bdc0c89a42e
enhanced #exapndPlaceHolders, to allow for a dictionary arg
Claus Gittinger <cg@exept.de>
parents:
4409
diff
changeset
|
6914 |
As an extension, the argument may also be a dictionary, providing |
7313 | 6915 |
values for symbolic keys. |
4842 | 6916 |
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
|
6917 |
(%1..%9 require a numeric key in the dictionary, however) |
18425 | 6918 |
Also, the values in argArrayOrDictionary may be blocks. |
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
6919 |
|
608 | 6920 |
To get a '%' character, use a '%%'-escape. |
7313 | 6921 |
To get an integer-indexed placeHolder followed by another digit, |
6922 |
or an index > 9, you must use %(digit). |
|
608 | 6923 |
See also bindWith:... for VisualAge compatibility." |
6924 |
||
10906
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6925 |
|stream| |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6926 |
|
18269 | 6927 |
stream := (TextStream ? CharacterWriteStream) on:(self species uninitializedNew:self size + 20). |
10906
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6928 |
self expandPlaceholdersWith:argArrayOrDictionary on:stream. |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6929 |
^ stream contents. |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6930 |
|
608 | 6931 |
" |
8874 | 6932 |
'hello %1' expandPlaceholdersWith:#('world') |
6933 |
'hello %1; how is %2' expandPlaceholdersWith:#('world' 'this') |
|
6934 |
'hello %2; how is %1' expandPlaceholdersWith:#('world' 'this') |
|
6935 |
'%1 plus %2 gives %3 ' expandPlaceholdersWith:#(4 5 9) |
|
6936 |
'%%(1)0 gives %(1)0' expandPlaceholdersWith:#(123) |
|
6937 |
'%%10 gives %10' expandPlaceholdersWith:#(123) |
|
6938 |
'%%(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
|
6939 |
'%test gives %1' expandPlaceholdersWith:#(123) |
11028
cb1ee3ddd91a
expandPlaceHolders: care fr Text with emphasis
Claus Gittinger <cg@exept.de>
parents:
11018
diff
changeset
|
6940 |
'bla %1 bla' expandPlaceholdersWith:{ 'hello' allBold } |
cb1ee3ddd91a
expandPlaceHolders: care fr Text with emphasis
Claus Gittinger <cg@exept.de>
parents:
11018
diff
changeset
|
6941 |
'bla %1 bla' expandPlaceholdersWith:{ 'hello' } |
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
6942 |
('bla %1 bla' withColor:Color red) |
20874 | 6943 |
expandPlaceholdersWith:{ 'hello' } |
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
6944 |
('bla %1 bla' withColor:Color red) |
20874 | 6945 |
expandPlaceholdersWith:{ 'hello' withColor:Color blue } |
608 | 6946 |
" |
1026
455c9f609f4d
care for 16bit strings in expandPlaceHolders
Claus Gittinger <cg@exept.de>
parents:
1025
diff
changeset
|
6947 |
|
4410
2bdc0c89a42e
enhanced #exapndPlaceHolders, to allow for a dictionary arg
Claus Gittinger <cg@exept.de>
parents:
4409
diff
changeset
|
6948 |
" |
2bdc0c89a42e
enhanced #exapndPlaceHolders, to allow for a dictionary arg
Claus Gittinger <cg@exept.de>
parents:
4409
diff
changeset
|
6949 |
|dict| |
2bdc0c89a42e
enhanced #exapndPlaceHolders, to allow for a dictionary arg
Claus Gittinger <cg@exept.de>
parents:
4409
diff
changeset
|
6950 |
|
2bdc0c89a42e
enhanced #exapndPlaceHolders, to allow for a dictionary arg
Claus Gittinger <cg@exept.de>
parents:
4409
diff
changeset
|
6951 |
dict := Dictionary new. |
2bdc0c89a42e
enhanced #exapndPlaceHolders, to allow for a dictionary arg
Claus Gittinger <cg@exept.de>
parents:
4409
diff
changeset
|
6952 |
dict at:1 put:'one'. |
2bdc0c89a42e
enhanced #exapndPlaceHolders, to allow for a dictionary arg
Claus Gittinger <cg@exept.de>
parents:
4409
diff
changeset
|
6953 |
dict at:$a put:'AAAAA'. |
8794
a5df33935142
nindWith supports blocks in the value dictionary
Claus Gittinger <cg@exept.de>
parents:
8710
diff
changeset
|
6954 |
dict at:$b put:[ Time now ]. |
12316
3e78905d70a9
comment/format in: #expandPlaceholdersWith:
Claus Gittinger <cg@exept.de>
parents:
12196
diff
changeset
|
6955 |
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
|
6956 |
'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
|
6957 |
" |
2bdc0c89a42e
enhanced #exapndPlaceHolders, to allow for a dictionary arg
Claus Gittinger <cg@exept.de>
parents:
4409
diff
changeset
|
6958 |
|
2737
9f29aecea39d
care for % at end in expandPlaceHolders.
Claus Gittinger <cg@exept.de>
parents:
2736
diff
changeset
|
6959 |
"Modified: 1.7.1997 / 00:53:24 / cg" |
9192 | 6960 |
! |
6961 |
||
10906
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6962 |
expandPlaceholdersWith:argArrayOrDictionary on:aStream |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6963 |
"write the receiver to aStream, where all %i escapes are |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6964 |
replaced by corresponding arguments' printStrings from the argArrayOrDictionary. |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6965 |
I.e. 'hello %1; how is %2' expandPlaceholdersWith:#('world' 'this') results |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6966 |
in the new string 'hello world; how is this'. |
18425 | 6967 |
|
10906
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6968 |
As an extension, the argument may also be a dictionary, providing |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6969 |
values for symbolic keys. |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6970 |
In this case, %a .. %z and %(...) are also allowed. |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6971 |
(%1..%9 require a numeric key in the dictionary, however) |
18425 | 6972 |
Also, the values in argArrayOrDictionary may be blocks. |
6973 |
||
10906
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6974 |
To get a '%' character, use a '%%'-escape. |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6975 |
To get an integer-indexed placeHolder followed by another digit, |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6976 |
or an index > 9, you must use %(digit). |
18425 | 6977 |
|
10935 | 6978 |
See also bindWith:... for VisualAge compatibility. |
10939
d0ca31c1d1e9
#expandPlaceHoldersWith: handle %<cr> %<tab> etc
Stefan Vogel <sv@exept.de>
parents:
10935
diff
changeset
|
6979 |
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
|
6980 |
|
23067 | 6981 |
^ self expandPlaceholders:$% with:argArrayOrDictionary ignoreNumericEscapes:false on:aStream |
10906
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6982 |
|
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6983 |
" |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
6984 |
String streamContents:[:s| |
20874 | 6985 |
'hello %1' expandPlaceholdersWith:#('world') on:s. |
6986 |
s cr. |
|
6987 |
'hello %1; how is %2' expandPlaceholdersWith:#('world' 'this') on:s. |
|
6988 |
s cr. |
|
6989 |
'hello %2; how is %1' expandPlaceholdersWith:#('world' 'this') on:s. |
|
6990 |
s cr. |
|
6991 |
'%1 plus %2 gives %3 ' expandPlaceholdersWith:#(4 5 9) on:s. |
|
6992 |
s cr. |
|
6993 |
'%%(1)0 gives %(1)0' expandPlaceholdersWith:#(123) on:s. |
|
6994 |
s cr. |
|
6995 |
'%%10 gives %10' expandPlaceholdersWith:#(123) on:s. |
|
6996 |
s cr. |
|
6997 |
'%%(10) gives %(10) %<cr>%<tab>next line' expandPlaceholdersWith:#(123) on:s. |
|
6998 |
s cr. |
|
6999 |
'%test gives %1' expandPlaceholdersWith:#(123) on:s. |
|
10906
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
7000 |
] |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
7001 |
" |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
7002 |
|
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
7003 |
" |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
7004 |
|dict| |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
7005 |
|
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
7006 |
dict := Dictionary new. |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
7007 |
dict at:1 put:'one'. |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
7008 |
dict at:$a put:'AAAAA'. |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
7009 |
dict at:$b put:[ Time now ]. |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
7010 |
String streamContents:[:s| |
20874 | 7011 |
'hello %1 %a %b' expandPlaceholdersWith:dict on:s. |
10906
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
7012 |
]. |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
7013 |
" |
13008
a27647c36b31
changed: #expandPlaceholdersWith:on:
Claus Gittinger <cg@exept.de>
parents:
12925
diff
changeset
|
7014 |
|
13138
d2eb0eba5907
changed: #expandPlaceholdersWith:on:
Claus Gittinger <cg@exept.de>
parents:
13137
diff
changeset
|
7015 |
"Modified: / 18-11-2010 / 15:43:28 / cg" |
23067 | 7016 |
"Modified: / 14-06-2018 / 11:45:36 / Claus Gittinger" |
10906
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
7017 |
! |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
7018 |
|
22929 | 7019 |
extractPlaceHolders:escapeCharacter |
7020 |
"returns a list of placeholder keys of the form %i, |
|
7021 |
where i is either a single digit (as in %1, %2) |
|
7022 |
or a single letter (as in %a, %z) |
|
7023 |
or a word-key, as in %(one), %(fooBar). |
|
7024 |
For numeric keys, the returned collection contains integers; |
|
7025 |
for non-numeric ones, it includes strings." |
|
7026 |
||
7027 |
|setOfKeys loggingDictionary| |
|
7028 |
||
7029 |
setOfKeys := Set new. |
|
7030 |
loggingDictionary := Plug new. |
|
7031 |
loggingDictionary respondTo:#at: with:[:key | setOfKeys add:key. key asString]. |
|
7032 |
loggingDictionary respondTo:#at:ifAbsent: with:[:key :def | setOfKeys add:key. key asString]. |
|
7033 |
loggingDictionary respondTo:#includesKey: with:[:key | setOfKeys add:key. true]. |
|
7034 |
loggingDictionary respondTo:#isSequenceable with:[false]. |
|
7035 |
self expandPlaceholders:escapeCharacter with:loggingDictionary. |
|
7036 |
^ setOfKeys |
|
7037 |
||
7038 |
" |
|
7039 |
self assert:('hello %1 and %2' extractPlaceHolders:$%) asSet = #(1 2) asSet |
|
7040 |
self assert:('hello %9 and %(10)' extractPlaceHolders:$%) asSet = #(9 10) asSet |
|
7041 |
self assert:('hello %a and %(foo) and %1' extractPlaceHolders:$%) asSet = #(1 'a' 'foo') asSet |
|
7042 |
self assert:('hello %a and %(foo) and %1' extractPlaceHolders:$$) asSet = #() asSet |
|
7043 |
" |
|
7044 |
! |
|
7045 |
||
9230 | 7046 |
firstLine |
14405
1d0d99a95bba
comment/format in:7 methods
Claus Gittinger <cg@exept.de>
parents:
14168
diff
changeset
|
7047 |
"return the first line of a multiline string" |
1d0d99a95bba
comment/format in:7 methods
Claus Gittinger <cg@exept.de>
parents:
14168
diff
changeset
|
7048 |
|
1d0d99a95bba
comment/format in:7 methods
Claus Gittinger <cg@exept.de>
parents:
14168
diff
changeset
|
7049 |
^ self asCollectionOfSubCollectionsSeparatedBy:(Character cr) do:[:line | ^ line]. |
9230 | 7050 |
|
7051 |
" |
|
10703
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
7052 |
'hello' firstLine |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
7053 |
'1\2\3' withCRs firstLine |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
7054 |
'\1\2\3' withCRs firstLine |
9230 | 7055 |
" |
7056 |
! |
|
7057 |
||
13987 | 7058 |
tokensBasedOn:aCharacter |
7059 |
"this is an ST-80 alias for the ST/X method |
|
20874 | 7060 |
asCollectionOfSubstringsSeparatedBy:" |
13987 | 7061 |
|
7062 |
^ self asCollectionOfSubstringsSeparatedBy:aCharacter |
|
7063 |
||
7064 |
" |
|
7065 |
'hello:world:isnt:this nice' tokensBasedOn:$: |
|
7066 |
'foo,bar,baz' tokensBasedOn:$, |
|
7067 |
'/etc/passwd' asFilename readStream nextLine tokensBasedOn:$: |
|
7068 |
" |
|
7069 |
! |
|
7070 |
||
7071 |
withCEscapes |
|
16863 | 7072 |
"return a new string consisting of receiver's characters |
13987 | 7073 |
with all special and unprintable characters replaced by \X-character escapes. |
7074 |
(similar to the way C-language literal Strings are represented). |
|
7075 |
The resulting string will contain only 7-bit ascii characters. |
|
7076 |
Emphasis is not supported. |
|
7077 |
The following escapes are generated: |
|
20874 | 7078 |
\' single quote character |
7079 |
\dQuote double quote character |
|
7080 |
\r return character |
|
7081 |
\r return character |
|
7082 |
\n newline character |
|
7083 |
\t tab character |
|
7084 |
\\ the \ character itself |
|
7085 |
\xnn two digit hex number defining the characters ascii value |
|
7086 |
\unnnn four digit hex number defining the characters ascii value |
|
7087 |
\Unnnnnnnn eight digit hex number defining the characters ascii value |
|
16863 | 7088 |
This is the opposite of withoutCEscapes. |
17000 | 7089 |
|
7090 |
Sigh: this is named completely wrong (opposite naming of withCRs/witoutCRs), |
|
20874 | 7091 |
but it cannot be changed easily, as these methods are already used heavily |
13987 | 7092 |
" |
7093 |
||
7094 |
|anyEscapeNeeded out seq| |
|
7095 |
||
7096 |
" |
|
7097 |
first, check if any escape is needed and return the receiver unchanged if not |
|
7098 |
" |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
7099 |
anyEscapeNeeded := self |
20874 | 7100 |
contains:[:ch | |
7101 |
((ch codePoint between:32 and:126) not |
|
7102 |
or:[ch == $' or:[ch == $"]]) |
|
7103 |
]. |
|
13987 | 7104 |
anyEscapeNeeded ifFalse:[ ^ self ]. |
7105 |
||
7106 |
self hasChangeOfEmphasis ifTrue:[ self error:'emphasis not supported' ]. |
|
7107 |
||
18269 | 7108 |
out := WriteStream on:(String uninitializedNew:self size-1). |
13987 | 7109 |
|
7110 |
self do:[:ch | |
|
20874 | 7111 |
|cp| |
7112 |
||
7113 |
(ch == $' or:[ch == $"]) ifTrue:[ |
|
7114 |
out nextPut:$\. |
|
7115 |
out nextPut:ch. |
|
7116 |
] ifFalse:[ |
|
7117 |
(ch codePoint between:32 and:126) ifTrue:[ |
|
7118 |
out nextPut:ch |
|
7119 |
] ifFalse:[ |
|
7120 |
ch == Character return ifTrue:[ |
|
7121 |
seq := '\r' |
|
7122 |
] ifFalse:[ ch == Character nl ifTrue:[ |
|
7123 |
seq := '\n' |
|
7124 |
] ifFalse:[ ch == Character tab ifTrue:[ |
|
7125 |
seq := '\t' |
|
7126 |
] ifFalse:[ ch == $\ ifTrue:[ |
|
7127 |
seq := '\\' |
|
7128 |
] ifFalse:[ |
|
7129 |
cp := ch codePoint. |
|
7130 |
cp <= 16rFF ifTrue:[ |
|
22344 | 7131 |
seq := '\x' , (cp printStringRadix:16 size:2 fill:$0) |
20874 | 7132 |
] ifFalse:[ |
7133 |
cp <= 16rFFFF ifTrue:[ |
|
22344 | 7134 |
seq := '\u' , (cp printStringRadix:16 size:4 fill:$0) |
20874 | 7135 |
] ifFalse:[ |
22344 | 7136 |
seq := '\U',(cp printStringRadix:16 size:8 fill:$0) |
20874 | 7137 |
] |
7138 |
] |
|
7139 |
]]]]. |
|
7140 |
out nextPutAll:seq |
|
7141 |
]. |
|
7142 |
]. |
|
13987 | 7143 |
]. |
7144 |
^ out contents |
|
7145 |
||
7146 |
" |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
7147 |
'hello\n\tworld' withoutCEscapes. |
17000 | 7148 |
'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
|
7149 |
('hello ',(Character value:16r1234),' world') withCEscapes |
13987 | 7150 |
" |
7151 |
||
7152 |
"Created: / 25-01-2012 / 11:08:16 / cg" |
|
22344 | 7153 |
"Modified: / 12-11-2017 / 12:39:57 / cg" |
13987 | 7154 |
! |
7155 |
||
9192 | 7156 |
withCRs |
17205 | 7157 |
"return a new string consisting of the receiver's characters |
16757 | 7158 |
with all \-characters replaced by cr-characters. |
7159 |
If there are no backslashes, return the original" |
|
7160 |
||
16759 | 7161 |
^ self copyReplaceAll:$\ with:(Character cr) ifNone:self |
9192 | 7162 |
|
7163 |
" |
|
7164 |
'hello\world' withCRs |
|
7165 |
" |
|
7166 |
||
7167 |
"Modified: / 18.7.1998 / 22:53:02 / cg" |
|
7168 |
! |
|
7169 |
||
7170 |
withEscapes |
|
15405 | 7171 |
<resource: #obsolete> |
13987 | 7172 |
"has been renamed; the name withEscapes is misleading" |
7173 |
||
7174 |
self obsoleteMethodWarning:'use withoutCEscapes'. |
|
7175 |
^ self withoutCEscapes. |
|
7176 |
||
7177 |
"Modified: / 25-01-2012 / 10:42:30 / cg" |
|
9192 | 7178 |
! |
7179 |
||
7180 |
withMatchEscapes |
|
4117 | 7181 |
"return a copy of the receiver with all match characters escaped |
7182 |
by $\ characters (to be usable as a match string). |
|
7183 |
Return the receiver, if there are none." |
|
7184 |
||
5342
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
7185 |
|in out c escape| |
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
7186 |
|
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
7187 |
escape := self class matchEscapeCharacter. |
4117 | 7188 |
|
7189 |
in := self readStream. |
|
7986 | 7190 |
out := WriteStream on:(self species new:self size). |
4117 | 7191 |
[in atEnd] whileFalse:[ |
20874 | 7192 |
c := in next. |
7193 |
(c == escape or:['*[#' includes:c]) ifTrue:[ |
|
7194 |
out nextPut:$\. |
|
7195 |
]. |
|
7196 |
out nextPut:c. |
|
4117 | 7197 |
]. |
7198 |
^ out contents. |
|
7199 |
||
7200 |
" |
|
8874 | 7201 |
'*foo' withMatchEscapes |
7202 |
'\*foo' withMatchEscapes |
|
7203 |
'*foo' withMatchEscapes |
|
7204 |
'\\*foo' withMatchEscapes |
|
7205 |
'foo*' withMatchEscapes |
|
7206 |
'foo\*' withMatchEscapes |
|
7207 |
'foo\' withMatchEscapes |
|
7208 |
'f*o*o' withMatchEscapes |
|
4117 | 7209 |
" |
7210 |
||
7211 |
"Modified: 2.4.1997 / 18:13:04 / cg" |
|
9192 | 7212 |
! |
7213 |
||
14992 | 7214 |
withSeparatorsCompacted |
7215 |
"return a new string with each sequence of whiteSpace replaced by a single space character. |
|
7216 |
Preserves a leading/trailing space." |
|
7217 |
||
7218 |
^ self species streamContents:[:s | |
|
20874 | 7219 |
|skipping| |
7220 |
||
7221 |
skipping := false. |
|
7222 |
1 to:self size do:[:idx | |
|
7223 |
|char| |
|
7224 |
||
7225 |
char := self at:idx. |
|
7226 |
char isSeparator ifFalse:[ |
|
7227 |
s nextPut:char. |
|
7228 |
skipping := false. |
|
7229 |
] ifTrue:[ |
|
7230 |
skipping ifFalse:[ |
|
7231 |
s nextPut:(Character space). |
|
7232 |
skipping := true |
|
7233 |
]. |
|
7234 |
] |
|
7235 |
] |
|
14992 | 7236 |
] |
7237 |
||
7238 |
" |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
7239 |
'hello wwww' withSeparatorsCompacted |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
7240 |
'hello wwww' withSeparatorsCompacted |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
7241 |
' hello wwww' withSeparatorsCompacted |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
7242 |
' hello wwww ' withSeparatorsCompacted |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
7243 |
' hello wwww ' withSeparatorsCompacted |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
7244 |
'hel lo www w' withSeparatorsCompacted |
14992 | 7245 |
" |
7246 |
! |
|
7247 |
||
18464 | 7248 |
withSeparatorsReplacedBy:replacementCharacter |
7249 |
"return a new string with each separator (whitespace) replaced by replacementCharacter. |
|
7250 |
Typically used with space as replacementCharacter" |
|
7251 |
||
7252 |
^ self species streamContents:[:s | |
|
20874 | 7253 |
self do:[:ch | |
7254 |
ch isSeparator ifTrue:[ |
|
7255 |
s nextPut:replacementCharacter |
|
7256 |
] ifFalse:[ |
|
7257 |
s nextPut:ch. |
|
7258 |
] |
|
7259 |
] |
|
18464 | 7260 |
] |
7261 |
||
7262 |
" |
|
7263 |
'hello wwww' withSeparatorsReplacedBy:$* |
|
7264 |
'hello ww ww' withSeparatorsReplacedBy:$* |
|
7265 |
' hello wwww' withSeparatorsReplacedBy:$* |
|
7266 |
'hel lo www w' withSeparatorsReplacedBy:$* |
|
7267 |
'hel |
|
7268 |
lo www |
|
7269 |
w' withSeparatorsReplacedBy:$* |
|
7270 |
" |
|
7271 |
! |
|
7272 |
||
9192 | 7273 |
withTabs |
19464 | 7274 |
"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
|
7275 |
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
|
7276 |
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
|
7277 |
otherwise a new string is returned. |
8874 | 7278 |
Limitation: only the very first spaces are replaced |
20874 | 7279 |
(i.e. if the receiver contains newLine characters, |
7280 |
no tabs are inserted after those lineBreaks)" |
|
8874 | 7281 |
|
7282 |
|idx "{ SmallInteger }" |
|
7283 |
nTabs "{ SmallInteger }" |
|
608 | 7284 |
newString| |
7285 |
||
7286 |
idx := self findFirst:[:c | (c ~~ Character space)]. |
|
7287 |
nTabs := (idx-1) // 8. |
|
5815
4acdd87a77d4
// can return negative numbers.
Stefan Vogel <sv@exept.de>
parents:
5795
diff
changeset
|
7288 |
nTabs <= 0 ifTrue:[^ self]. |
608 | 7289 |
|
7290 |
"any tabs" |
|
15523 | 7291 |
newString := self species new:(self size - (nTabs * 7)). |
608 | 7292 |
newString atAll:(1 to:nTabs) put:(Character tab). |
7293 |
newString replaceFrom:(nTabs + 1) with:self startingAt:(nTabs * 8 + 1). |
|
7294 |
^ newString |
|
7295 |
||
7296 |
" |
|
8874 | 7297 |
'12345678901234567890' withTabs |
7298 |
' 8901234567890' withTabs |
|
7299 |
' 901234567890' withTabs |
|
608 | 7300 |
' 67890' withTabs |
7301 |
' 7890' withTabs |
|
7302 |
' 890' withTabs |
|
7303 |
" |
|
9192 | 7304 |
! |
7305 |
||
7306 |
withTabsExpanded |
|
19464 | 7307 |
"return a string consisting of the receiver's characters, |
8874 | 7308 |
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
|
7309 |
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
|
7310 |
otherwise a new string is returned. |
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
678
diff
changeset
|
7311 |
This does handle multiline strings." |
608 | 7312 |
|
5741
3a6086774d38
added #withTabsExpanded: (i.e. arg to specify tab-column width)
Claus Gittinger <cg@exept.de>
parents:
5739
diff
changeset
|
7313 |
^ self withTabsExpanded:8 |
3a6086774d38
added #withTabsExpanded: (i.e. arg to specify tab-column width)
Claus Gittinger <cg@exept.de>
parents:
5739
diff
changeset
|
7314 |
|
3a6086774d38
added #withTabsExpanded: (i.e. arg to specify tab-column width)
Claus Gittinger <cg@exept.de>
parents:
5739
diff
changeset
|
7315 |
" |
8874 | 7316 |
('1' , Character tab asString , 'x') withTabsExpanded |
7317 |
('12345' , Character tab asString , 'x') withTabsExpanded |
|
7318 |
('123456' , Character tab asString , 'x') withTabsExpanded |
|
7319 |
('1234567' , Character tab asString , 'x') withTabsExpanded |
|
7320 |
('12345678' , Character tab asString , 'x') withTabsExpanded |
|
7321 |
('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
|
7322 |
|
3a6086774d38
added #withTabsExpanded: (i.e. arg to specify tab-column width)
Claus Gittinger <cg@exept.de>
parents:
5739
diff
changeset
|
7323 |
(String with:Character tab |
20874 | 7324 |
with:Character tab |
7325 |
with:$1) withTabsExpanded |
|
5741
3a6086774d38
added #withTabsExpanded: (i.e. arg to specify tab-column width)
Claus Gittinger <cg@exept.de>
parents:
5739
diff
changeset
|
7326 |
|
3a6086774d38
added #withTabsExpanded: (i.e. arg to specify tab-column width)
Claus Gittinger <cg@exept.de>
parents:
5739
diff
changeset
|
7327 |
(String with:Character tab |
20874 | 7328 |
with:$1 |
7329 |
with:Character tab |
|
7330 |
with:$2) withTabsExpanded |
|
5741
3a6086774d38
added #withTabsExpanded: (i.e. arg to specify tab-column width)
Claus Gittinger <cg@exept.de>
parents:
5739
diff
changeset
|
7331 |
|
3a6086774d38
added #withTabsExpanded: (i.e. arg to specify tab-column width)
Claus Gittinger <cg@exept.de>
parents:
5739
diff
changeset
|
7332 |
(String with:Character tab |
20874 | 7333 |
with:$1 |
7334 |
with:Character cr |
|
7335 |
with:Character tab |
|
7336 |
with:$2) withTabsExpanded |
|
5741
3a6086774d38
added #withTabsExpanded: (i.e. arg to specify tab-column width)
Claus Gittinger <cg@exept.de>
parents:
5739
diff
changeset
|
7337 |
" |
3a6086774d38
added #withTabsExpanded: (i.e. arg to specify tab-column width)
Claus Gittinger <cg@exept.de>
parents:
5739
diff
changeset
|
7338 |
|
3a6086774d38
added #withTabsExpanded: (i.e. arg to specify tab-column width)
Claus Gittinger <cg@exept.de>
parents:
5739
diff
changeset
|
7339 |
"Modified: 12.5.1996 / 13:05:10 / cg" |
9192 | 7340 |
! |
7341 |
||
7342 |
withTabsExpanded:numSpaces |
|
19464 | 7343 |
"return a string consisting of the receiver's characters, |
8874 | 7344 |
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
|
7345 |
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
|
7346 |
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
|
7347 |
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
|
7348 |
|
8874 | 7349 |
|col "{ SmallInteger }" |
1381 | 7350 |
str ch |
7351 |
dstIdx "{ SmallInteger }" |
|
7352 |
newSz "{ SmallInteger }" |
|
7353 |
sz "{ SmallInteger }" |
|
7354 |
hasEmphasis e| |
|
608 | 7355 |
|
7356 |
(self includes:(Character tab)) ifFalse:[^ self]. |
|
1381 | 7357 |
|
7358 |
sz := self size. |
|
7359 |
||
7360 |
"/ count the new size first, instead of |
|
7361 |
"/ multiple resizing (better for large strings) |
|
7362 |
||
7363 |
col := 1. newSz := 0. |
|
7364 |
1 to:sz do:[:srcIdx | |
|
20874 | 7365 |
ch := self at:srcIdx. |
7366 |
ch == Character tab ifFalse:[ |
|
7367 |
col := col + 1. |
|
7368 |
newSz := newSz + 1. |
|
7369 |
ch == Character cr ifTrue:[ |
|
7370 |
col := 1 |
|
7371 |
]. |
|
7372 |
] ifTrue:[ |
|
7373 |
(col \\ numSpaces) to:numSpaces do:[:ii | |
|
7374 |
newSz := newSz + 1. |
|
7375 |
col := col + 1 |
|
7376 |
]. |
|
7377 |
] |
|
608 | 7378 |
]. |
1381 | 7379 |
|
7380 |
str := self species new:newSz. |
|
7381 |
||
7382 |
hasEmphasis := self hasChangeOfEmphasis. |
|
7383 |
||
7384 |
col := 1. dstIdx := 1. |
|
7385 |
1 to:sz do:[:srcIdx | |
|
20874 | 7386 |
ch := self at:srcIdx. |
7387 |
||
7388 |
ch == Character tab ifFalse:[ |
|
7389 |
col := col + 1. |
|
7390 |
ch == Character cr ifTrue:[ |
|
7391 |
col := 1 |
|
7392 |
]. |
|
7393 |
hasEmphasis ifTrue:[ |
|
7394 |
e := self emphasisAt:srcIdx. |
|
7395 |
str emphasisAt:dstIdx put:e |
|
7396 |
]. |
|
7397 |
str at:dstIdx put:ch. |
|
7398 |
dstIdx := dstIdx + 1 |
|
7399 |
] ifTrue:[ |
|
7400 |
(col \\ numSpaces) to:numSpaces do:[:ii | |
|
7401 |
str at:dstIdx put:Character space. |
|
7402 |
dstIdx := dstIdx + 1. |
|
7403 |
col := col + 1 |
|
7404 |
]. |
|
7405 |
] |
|
1381 | 7406 |
]. |
7407 |
^ str |
|
608 | 7408 |
|
7409 |
" |
|
8874 | 7410 |
('1' , Character tab asString , 'x') withTabsExpanded |
7411 |
('1' , Character tab asString , 'x') withTabsExpanded:4 |
|
7412 |
('12345' , Character tab asString , 'x') withTabsExpanded |
|
7413 |
('123456' , Character tab asString , 'x') withTabsExpanded |
|
7414 |
('1234567' , Character tab asString , 'x') withTabsExpanded |
|
7415 |
('12345678' , Character tab asString , 'x') withTabsExpanded |
|
7416 |
('123456789' , Character tab asString , 'x') withTabsExpanded |
|
735
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
678
diff
changeset
|
7417 |
|
608 | 7418 |
(String with:Character tab |
20874 | 7419 |
with:Character tab |
7420 |
with:$1) withTabsExpanded |
|
608 | 7421 |
|
7422 |
(String with:Character tab |
|
20874 | 7423 |
with:$1 |
7424 |
with:Character tab |
|
7425 |
with:$2) withTabsExpanded |
|
735
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
678
diff
changeset
|
7426 |
|
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
678
diff
changeset
|
7427 |
(String with:Character tab |
20874 | 7428 |
with:$1 |
7429 |
with:Character cr |
|
7430 |
with:Character tab |
|
7431 |
with:$2) withTabsExpanded |
|
608 | 7432 |
" |
735
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
678
diff
changeset
|
7433 |
|
1381 | 7434 |
"Modified: 12.5.1996 / 13:05:10 / cg" |
9192 | 7435 |
! |
7436 |
||
13987 | 7437 |
withoutAllSpaces |
16855 | 7438 |
"return a copy of the receiver with all whitespace removed" |
7439 |
||
13987 | 7440 |
^ self asCollectionOfWords asStringWith:''. |
7441 |
"/ |col string| |
|
7442 |
"/ |
|
7443 |
"/ col := self asCollectionOfWords. |
|
7444 |
"/ string := String new. |
|
7445 |
"/ col do:[:el | |
|
7446 |
"/ string := string,el |
|
7447 |
"/ ]. |
|
7448 |
"/ ^string |
|
7449 |
||
7450 |
" |
|
7451 |
'hello wwww' withoutAllSpaces |
|
7452 |
'hel lo www w' withoutAllSpaces |
|
7453 |
" |
|
7454 |
||
7455 |
"Modified: / 18.7.1998 / 22:53:08 / cg" |
|
7456 |
! |
|
7457 |
||
7458 |
withoutCEscapes |
|
15628 | 7459 |
"return a new string consisting of receiver's characters |
22205 | 7460 |
with all \X-character escapes replaced by corresponding characters. |
13987 | 7461 |
(similar to the way C-language Strings are converted). |
7462 |
The following escapes are supported: |
|
20874 | 7463 |
\r return character |
7464 |
\n newline character |
|
7465 |
\b backspace character |
|
7466 |
\f formfeed character |
|
7467 |
\t tab character |
|
7468 |
\e escape character |
|
7469 |
\\ the \ character itself |
|
7470 |
\nnn three digit octal number defining the characters ascii value |
|
7471 |
\xnn two digit hex number defining the characters ascii value |
|
7472 |
\unnnn four digit hex number defining the characters unicode value |
|
7473 |
\Unnnnnnnn eight digit hex number defining the characters unicode value |
|
7474 |
\other other |
|
13987 | 7475 |
|
7476 |
Notice, that \' is NOT a valid escape, since the general syntax of |
|
7477 |
string constants is not affected by this method. |
|
7478 |
||
7479 |
Although easily implementable, this is NOT done automatically |
|
7480 |
by the compiler (due to a lack of a language standard for this). |
|
22205 | 7481 |
However, the compiler may detect sends of #withEscapes to string literals |
13987 | 7482 |
and place a modified string constant into the binary/byte-code. |
7483 |
Therefore, no runtime penalty will be payed for using these escapes. |
|
7484 |
(not in pre 2.11 versions) |
|
16863 | 7485 |
|
7486 |
This is the opposite of withCEscapes. |
|
17000 | 7487 |
|
7488 |
Sigh: this is named completely wrong (opposite naming of withCRs/witoutCRs), |
|
20874 | 7489 |
but it cannot be changed easily, as these methods are already used heavily |
13987 | 7490 |
" |
7491 |
||
7492 |
|val "{ SmallInteger }" |
|
7493 |
in out nextChar nDigits| |
|
7494 |
||
7495 |
" |
|
7496 |
first, see if there is any escape; if not, return the receiver unchanged |
|
7497 |
" |
|
7498 |
(self includes:$\) ifFalse:[^ self ]. |
|
7499 |
||
7500 |
self hasChangeOfEmphasis ifTrue:[ self error:'emphasis not supported' ]. |
|
7501 |
out := CharacterWriteStream on:(String new:self size - 1). |
|
7502 |
||
7503 |
in := ReadStream on:self. |
|
7504 |
[in atEnd] whileFalse:[ |
|
20874 | 7505 |
nextChar := in next. |
7506 |
nextChar == $\ ifTrue:[ |
|
7507 |
in atEnd ifTrue:[ |
|
7508 |
] ifFalse:[ |
|
7509 |
nextChar := in next. |
|
7510 |
nextChar == $r ifTrue:[ |
|
7511 |
nextChar := Character return |
|
7512 |
] ifFalse:[ nextChar == $n ifTrue:[ |
|
7513 |
nextChar := Character nl |
|
7514 |
] ifFalse:[ nextChar == $b ifTrue:[ |
|
7515 |
nextChar := Character backspace |
|
7516 |
] ifFalse:[ nextChar == $f ifTrue:[ |
|
7517 |
nextChar := Character newPage |
|
7518 |
] ifFalse:[ nextChar == $t ifTrue:[ |
|
7519 |
nextChar := Character tab |
|
7520 |
] ifFalse:[ nextChar == $e ifTrue:[ |
|
7521 |
nextChar := Character esc |
|
7522 |
] ifFalse:[ |
|
7523 |
nextChar == $0 ifTrue:[ |
|
7524 |
val := 0. |
|
7525 |
nextChar := in peek. |
|
7526 |
nDigits := 1. |
|
7527 |
[nextChar notNil and:[nextChar isDigit and:[nDigits <= 3]]] whileTrue:[ |
|
7528 |
val := (val * 8) + nextChar digitValue. |
|
7529 |
nextChar := in nextPeek. |
|
7530 |
nDigits := nDigits + 1. |
|
7531 |
]. |
|
7532 |
nextChar := Character value:val. |
|
7533 |
] ifFalse:[ |
|
7534 |
val := 0. |
|
7535 |
nextChar == $x ifTrue:[ |
|
7536 |
2 timesRepeat:[ |
|
7537 |
nextChar := in next. |
|
7538 |
val := (val * 16) + nextChar digitValue. |
|
7539 |
]. |
|
7540 |
nextChar := Character value:val. |
|
7541 |
] ifFalse:[ |
|
7542 |
nextChar == $u ifTrue:[ |
|
7543 |
4 timesRepeat:[ |
|
7544 |
nextChar := in next. |
|
7545 |
val := (val * 16) + nextChar digitValue. |
|
7546 |
]. |
|
7547 |
nextChar := Character value:val. |
|
7548 |
] ifFalse:[ |
|
7549 |
nextChar == $U ifTrue:[ |
|
7550 |
8 timesRepeat:[ |
|
7551 |
nextChar := in next. |
|
7552 |
val := (val * 16) + nextChar digitValue. |
|
7553 |
]. |
|
7554 |
nextChar := Character value:val. |
|
7555 |
] |
|
7556 |
] |
|
7557 |
] |
|
7558 |
] |
|
7559 |
]]]]]]. |
|
7560 |
]. |
|
7561 |
]. |
|
7562 |
out nextPut:nextChar. |
|
13987 | 7563 |
]. |
7564 |
^ out contents |
|
7565 |
||
7566 |
" |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
7567 |
'hello world' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
7568 |
'hello\world' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
7569 |
'hello\world\' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
7570 |
'hello world\' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
7571 |
'hello\tworld' withoutCEscapes |
13987 | 7572 |
'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
|
7573 |
'hello\tworld\n' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
7574 |
'hello\010world' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
7575 |
'hello\r\nworld' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
7576 |
'hello\r\n\x08world' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
7577 |
'0\x080' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
7578 |
'0\u12340' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
7579 |
'0\U123456780' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
7580 |
'0\0a' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
7581 |
'0\00a' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
7582 |
'0\000a' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
7583 |
'0\0000a' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
7584 |
'0\00000a' withoutCEscapes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
7585 |
'0\03770' withoutCEscapes |
13987 | 7586 |
" |
7587 |
||
7588 |
"Created: / 25-01-2012 / 10:41:44 / cg" |
|
22205 | 7589 |
"Modified (comment): / 23-08-2017 / 11:07:43 / mawalch" |
13987 | 7590 |
! |
7591 |
||
9192 | 7592 |
withoutCRs |
19464 | 7593 |
"return a new collection consisting of receiver's elements |
9192 | 7594 |
with all cr-characters replaced by \-characters. |
7595 |
This is the reverse operation of withCRs." |
|
7596 |
||
16759 | 7597 |
^ self copyReplaceAll:(Character cr) with:$\ ifNone:self |
12319
18b80c2ce1e3
oops #withoutSeparators #withoutSpaces were noops
Claus Gittinger <cg@exept.de>
parents:
12317
diff
changeset
|
7598 |
|
9192 | 7599 |
" |
7600 |
'hello |
|
7601 |
world' withoutCRs |
|
7602 |
" |
|
7603 |
||
7604 |
"Modified: / 18.7.1998 / 22:53:08 / cg" |
|
7605 |
! |
|
7606 |
||
7607 |
withoutLeadingSeparators |
|
608 | 7608 |
"return a copy of myself without leading separators. |
7609 |
Notice: this does remove tabs, newline or any other whitespace. |
|
7610 |
Returns an empty string, if the receiver consist only of whitespace." |
|
7611 |
||
7612 |
|index| |
|
7613 |
||
7614 |
index := self indexOfNonSeparatorStartingAt:1. |
|
7615 |
index ~~ 0 ifTrue:[ |
|
20874 | 7616 |
index == 1 ifTrue:[ |
7617 |
^ self |
|
7618 |
]. |
|
7619 |
^ self copyFrom:index |
|
608 | 7620 |
]. |
7621 |
^ '' |
|
7622 |
||
7623 |
" |
|
8874 | 7624 |
' foo ' withoutLeadingSeparators |
7625 |
'foo ' withoutLeadingSeparators |
|
7626 |
' foo' withoutLeadingSeparators |
|
7627 |
' ' withoutLeadingSeparators |
|
7628 |
'foo' withoutLeadingSeparators |
|
7629 |
(' ' , Character tab asString , ' foo ') withoutLeadingSeparators inspect |
|
608 | 7630 |
" |
9192 | 7631 |
! |
7632 |
||
7633 |
withoutMatchEscapes |
|
4117 | 7634 |
"return a copy of the receiver with all $\ removed or |
7635 |
the receiver, if there are none." |
|
7636 |
||
5342
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
7637 |
|in out c escape| |
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
7638 |
|
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
7639 |
escape := self class matchEscapeCharacter. |
4117 | 7640 |
|
7641 |
in := self readStream. |
|
8222
f2c454a9a038
replaced '' writeStream by String writeStream
Claus Gittinger <cg@exept.de>
parents:
8124
diff
changeset
|
7642 |
out := self species writeStream. |
4117 | 7643 |
[in atEnd] whileFalse:[ |
20874 | 7644 |
c := in next. |
7645 |
c == escape ifTrue:[ |
|
7646 |
in atEnd ifFalse:[ |
|
7647 |
c := in next. |
|
7648 |
] |
|
7649 |
]. |
|
7650 |
out nextPut:c. |
|
4117 | 7651 |
]. |
7652 |
^ out contents. |
|
7653 |
||
7654 |
" |
|
8874 | 7655 |
'*foo' withoutMatchEscapes |
7656 |
'\*foo' withoutMatchEscapes |
|
7657 |
'*foo' withoutMatchEscapes |
|
7658 |
'\\*foo' withoutMatchEscapes |
|
7659 |
'foo*' withoutMatchEscapes |
|
7660 |
'foo\*' withoutMatchEscapes |
|
7661 |
'foo\' withoutMatchEscapes |
|
7662 |
'f\*o\*o' withoutMatchEscapes |
|
4117 | 7663 |
" |
7664 |
||
7665 |
"Modified: 30.6.1997 / 13:40:23 / cg" |
|
9192 | 7666 |
! |
7667 |
||
7668 |
withoutPrefix:aString |
|
19677 | 7669 |
"if the receiver startsWith aString, return a copy without it. |
9192 | 7670 |
Otherwise return the receiver" |
7671 |
||
7672 |
(self startsWith:aString) ifTrue:[ |
|
20874 | 7673 |
^ self copyFrom:aString size+1 |
9192 | 7674 |
]. |
7675 |
^ self |
|
7676 |
||
7677 |
" |
|
7678 |
'helloworld' withoutPrefix:'hello' |
|
7679 |
'helloworld' withoutPrefix:'foo' |
|
7680 |
" |
|
19677 | 7681 |
|
7682 |
"Modified: / 30-04-2016 / 10:01:00 / cg" |
|
9192 | 7683 |
! |
7684 |
||
10671 | 7685 |
withoutQuotes |
17000 | 7686 |
"/ remove quotes ($" and $') from the front and end of myself (if matching)" |
7687 |
||
7688 |
|firstChar| |
|
7689 |
||
17024 | 7690 |
self isEmpty ifTrue:[^ self]. |
7691 |
||
17000 | 7692 |
firstChar := self first. |
7693 |
((firstChar == $") or:[firstChar == $']) ifFalse:[^ self]. |
|
7694 |
||
7695 |
self last == firstChar ifTrue:[ |
|
20874 | 7696 |
^ self copyFrom:2 to:(self size-1) |
10671 | 7697 |
]. |
17000 | 7698 |
^ self |
7699 |
||
7700 |
"/ |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
7701 |
"/ '"hello"' withoutQuotes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
7702 |
"/ '''hello''' withoutQuotes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
7703 |
"/ 'hello' withoutQuotes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
7704 |
"/ '"hello' withoutQuotes |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
7705 |
"/ 'hello"' withoutQuotes |
17000 | 7706 |
"/ |
10671 | 7707 |
! |
7708 |
||
9192 | 7709 |
withoutSeparators |
608 | 7710 |
"return a copy of myself without leading and trailing whitespace. |
4156 | 7711 |
(but whiteSpace in-between is preserved) |
608 | 7712 |
Whitespace is space, tab, newline, formfeed. |
7713 |
Use withoutSpaces, if you want to remove spaces only." |
|
7714 |
||
12319
18b80c2ce1e3
oops #withoutSeparators #withoutSpaces were noops
Claus Gittinger <cg@exept.de>
parents:
12317
diff
changeset
|
7715 |
^ self trimForWhich:[:ch | ch isSeparator] |
608 | 7716 |
|
7717 |
" |
|
8874 | 7718 |
' foo ' withoutSeparators |
7719 |
' foo' withoutSeparators |
|
7720 |
'foo ' withoutSeparators |
|
7721 |
' ' withoutSeparators |
|
7722 |
(' foo' , Character tab asString , ' ') withoutSeparators inspect |
|
12319
18b80c2ce1e3
oops #withoutSeparators #withoutSpaces were noops
Claus Gittinger <cg@exept.de>
parents:
12317
diff
changeset
|
7723 |
' foo ' asUnicode16String withoutSeparators |
608 | 7724 |
" |
9192 | 7725 |
! |
7726 |
||
7727 |
withoutSpaces |
|
608 | 7728 |
"return a copy of myself without leading and trailing spaces. |
4156 | 7729 |
(but spaces in-between are preserved) |
608 | 7730 |
Notice: this does NOT remove tabs, newline or any other whitespace. |
7731 |
Use withoutSeparators for this." |
|
7732 |
||
12319
18b80c2ce1e3
oops #withoutSeparators #withoutSpaces were noops
Claus Gittinger <cg@exept.de>
parents:
12317
diff
changeset
|
7733 |
^ self trimForWhich:[:ch | ch == Character space] |
608 | 7734 |
|
7735 |
" |
|
8874 | 7736 |
' foo ' withoutSpaces |
7737 |
'foo ' withoutSpaces |
|
7738 |
' foo' withoutSpaces |
|
7739 |
' ' withoutSpaces |
|
7740 |
'a b' withoutSpaces |
|
7741 |
(' foo' , Character tab asString , ' ') withoutSpaces inspect |
|
608 | 7742 |
" |
9192 | 7743 |
! |
7744 |
||
22357 | 7745 |
withoutSuffix:aString |
7746 |
"if the receiver endsWith aString, return a copy without it. |
|
7747 |
Otherwise return the receiver" |
|
7748 |
||
7749 |
(self endsWith:aString) ifTrue:[ |
|
7750 |
^ self copyButLast:aString size |
|
7751 |
]. |
|
7752 |
^ self |
|
7753 |
||
7754 |
" |
|
7755 |
'helloworld' withoutSuffix:'world' |
|
7756 |
'helloworld' withoutSuffix:'foo' |
|
7757 |
" |
|
7758 |
||
7759 |
"Created: / 23-10-2017 / 15:01:37 / cg" |
|
7760 |
! |
|
7761 |
||
9192 | 7762 |
withoutTrailingSeparators |
915
1ac1c7e942f4
added withoutTrailingSeparators
Claus Gittinger <cg@exept.de>
parents:
828
diff
changeset
|
7763 |
"return a copy of myself without trailing separators. |
1ac1c7e942f4
added withoutTrailingSeparators
Claus Gittinger <cg@exept.de>
parents:
828
diff
changeset
|
7764 |
Notice: this does remove tabs, newline or any other whitespace. |
1ac1c7e942f4
added withoutTrailingSeparators
Claus Gittinger <cg@exept.de>
parents:
828
diff
changeset
|
7765 |
Returns an empty string, if the receiver consist only of whitespace." |
1ac1c7e942f4
added withoutTrailingSeparators
Claus Gittinger <cg@exept.de>
parents:
828
diff
changeset
|
7766 |
|
12188 | 7767 |
^ self withoutTrailingForWhich:[:ch | ch isSeparator] |
915
1ac1c7e942f4
added withoutTrailingSeparators
Claus Gittinger <cg@exept.de>
parents:
828
diff
changeset
|
7768 |
|
1ac1c7e942f4
added withoutTrailingSeparators
Claus Gittinger <cg@exept.de>
parents:
828
diff
changeset
|
7769 |
" |
8874 | 7770 |
' foo ' withoutTrailingSeparators |
7771 |
'foo ' withoutTrailingSeparators |
|
7772 |
' foo' withoutTrailingSeparators |
|
7773 |
' ' withoutTrailingSeparators |
|
7774 |
'foo' withoutTrailingSeparators |
|
7775 |
(' ' , Character tab asString , ' foo ') withoutTrailingSeparators inspect |
|
7776 |
(' foo' , Character tab asString) withoutTrailingSeparators inspect |
|
915
1ac1c7e942f4
added withoutTrailingSeparators
Claus Gittinger <cg@exept.de>
parents:
828
diff
changeset
|
7777 |
" |
9192 | 7778 |
! ! |
7779 |
||
22924 | 7780 |
|
9192 | 7781 |
!CharacterArray methodsFor:'substring searching'! |
7782 |
||
15055 | 7783 |
findRangeOfString:subString |
7784 |
"find a substring. if found, return the start- and endIndex; |
|
7785 |
if not found, return an empty interval." |
|
7786 |
||
7787 |
^ self rangeOfSubCollection:subString startingAt:1 ifAbsent:[0 to:-1] caseSensitive:true |
|
7788 |
||
7789 |
" |
|
7790 |
'hello world' findRangeOfString:'llo' |
|
7791 |
'hello world' findRangeOfString:'ole' |
|
7792 |
" |
|
7793 |
! |
|
7794 |
||
9192 | 7795 |
findString:subString |
7796 |
"find a substring. if found, return the index; |
|
7797 |
if not found, return 0." |
|
7798 |
||
7799 |
^ self indexOfSubCollection:subString startingAt:1 ifAbsent:0 caseSensitive:true |
|
7800 |
||
7801 |
" |
|
7802 |
'hello world' findString:'llo' |
|
7803 |
'hello world' findString:'ole' |
|
7804 |
" |
|
7805 |
! |
|
608 | 7806 |
|
15734 | 7807 |
findString:subString caseSensitive:caseSensitive |
7808 |
"find a substring. if found, return the index; |
|
7809 |
if not found, return 0." |
|
7810 |
||
7811 |
^ self indexOfSubCollection:subString startingAt:1 ifAbsent:0 caseSensitive:caseSensitive |
|
7812 |
||
7813 |
" |
|
7814 |
'hello world' findString:'LLo' caseSensitive:true |
|
7815 |
'hello world' findString:'LLo' caseSensitive:false |
|
7816 |
" |
|
7817 |
! |
|
7818 |
||
608 | 7819 |
findString:subString ifAbsent:exceptionBlock |
7820 |
"find a substring. If found, return the index; |
|
7821 |
if not found, return the result of evaluating exceptionBlock." |
|
7822 |
||
9087
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
7823 |
^ self indexOfSubCollection:subString startingAt:1 ifAbsent:exceptionBlock caseSensitive:true |
608 | 7824 |
! |
7825 |
||
11018 | 7826 |
findString:subString ignoreCase:ignoreCase |
15734 | 7827 |
<resource: #obsolete> |
11018 | 7828 |
"find a substring. if found, return the index; |
7829 |
if not found, return 0." |
|
7830 |
||
7831 |
^ self indexOfSubCollection:subString startingAt:1 ifAbsent:0 caseSensitive:ignoreCase not |
|
7832 |
||
7833 |
" |
|
7834 |
'hello world' findString:'LLo' ignoreCase:false |
|
7835 |
'hello world' findString:'LLo' ignoreCase:true |
|
7836 |
" |
|
7837 |
! |
|
7838 |
||
608 | 7839 |
findString:subString startingAt:index |
7840 |
"find a substring, starting at index. if found, return the index; |
|
7841 |
if not found, return 0." |
|
7842 |
||
9087
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
7843 |
^ self indexOfSubCollection:subString startingAt:index ifAbsent:0 caseSensitive:true |
608 | 7844 |
|
7845 |
" |
|
9192 | 7846 |
'hello yello' findString:'llo' startingAt:1 |
7847 |
'hello yello' findString:'llo' startingAt:5 |
|
7848 |
'hello yello' findString:'llo' startingAt:15 |
|
7849 |
" |
|
7850 |
! |
|
608 | 7851 |
|
7852 |
findString:subString startingAt:index ifAbsent:exceptionBlock |
|
7853 |
"find a substring, starting at index. if found, return the index; |
|
7854 |
if not found, return the result of evaluating exceptionBlock." |
|
7855 |
||
9087
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
7856 |
^ self indexOfSubCollection:subString startingAt:index ifAbsent:exceptionBlock caseSensitive:true |
608 | 7857 |
! |
7858 |
||
12177 | 7859 |
findString:subString startingAt:index ifAbsent:exceptionBlock caseSensitive:caseSensitive |
10929 | 7860 |
"find a substring, starting at index. if found, return the index; |
7861 |
if not found, return the result of evaluating exceptionBlock." |
|
7862 |
||
7863 |
^ self indexOfSubCollection:subString startingAt:index ifAbsent:exceptionBlock caseSensitive:caseSensitive |
|
7864 |
! |
|
7865 |
||
608 | 7866 |
includesString:aString |
7867 |
"return true, if a substring is contained in the receiver" |
|
7868 |
||
9087
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
7869 |
^ (self indexOfSubCollection:aString startingAt:1 ifAbsent:0 caseSensitive:true) ~~ 0 |
608 | 7870 |
|
7871 |
" |
|
12177 | 7872 |
'hello world' includesString:'hel' |
8874 | 7873 |
'hello world' includesString:'rld' |
9192 | 7874 |
'hello world' includesString:'llo' |
7875 |
'hello world' includesString:'LLO' |
|
12177 | 7876 |
'hello world' includesString:'' |
9192 | 7877 |
" |
7878 |
! |
|
9191 | 7879 |
|
7880 |
includesString:aString caseSensitive:caseSensitive |
|
9087
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
7881 |
"return true, if a substring is contained in the receiver" |
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
7882 |
|
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
7883 |
^ (self indexOfSubCollection:aString startingAt:1 ifAbsent:0 caseSensitive:caseSensitive) ~~ 0 |
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
7884 |
|
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
7885 |
" |
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
7886 |
'hello world' includesString:'hel' caseSensitive:true |
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
7887 |
'hello world' includesString:'HEL' caseSensitive:true |
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
7888 |
'hello world' includesString:'HEL' caseSensitive:false |
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
7889 |
|
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
7890 |
'hello world' includesString:'wor' caseSensitive:true |
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
7891 |
'hello world' includesString:'WOR' caseSensitive:true |
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
7892 |
'hello world' includesString:'WOR' caseSensitive:false |
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
7893 |
" |
9192 | 7894 |
! |
7895 |
||
23060 | 7896 |
indexOfString:aString |
7897 |
"VSE and V'age compatibility" |
|
7898 |
"find a substring. If found, return the index; if not found, return 0." |
|
7899 |
||
7900 |
^ self indexOfSubCollection:aString startingAt:1 ifAbsent:[0] caseSensitive:true |
|
7901 |
||
7902 |
" |
|
7903 |
'hello world' indexOfString:'hello' -> 1 |
|
7904 |
'hello world' indexOfString:'world' -> 7 |
|
7905 |
'hello world' indexOfString:'World' -> 0 |
|
7906 |
'hello world' indexOfString:'World' caseSensitive:false -> 7 |
|
7907 |
" |
|
7908 |
||
7909 |
"Modified (format): / 08-06-2018 / 14:43:15 / Claus Gittinger" |
|
7910 |
! |
|
7911 |
||
7912 |
indexOfString:aString caseSensitive:caseSensitive |
|
7913 |
"VSE and V'age compatibility" |
|
7914 |
"find a substring. If found, return the index; if not found, return 0." |
|
7915 |
||
7916 |
^ self indexOfSubCollection:aString startingAt:1 ifAbsent:[0] caseSensitive:caseSensitive |
|
7917 |
||
7918 |
" |
|
7919 |
'hello world' indexOfString:'hello' -> 1 |
|
7920 |
'hello world' indexOfString:'world' -> 7 |
|
7921 |
'hello world' indexOfString:'World' -> 0 |
|
7922 |
'hello world' indexOfString:'World' caseSensitive:false -> 7 |
|
7923 |
" |
|
7924 |
||
7925 |
"Created: / 08-06-2018 / 14:39:18 / Claus Gittinger" |
|
7926 |
! |
|
7927 |
||
7928 |
indexOfString:aString caseSensitive:caseSensitive ifAbsent:exceptionValue |
|
7929 |
"VSE and V'age compatibility" |
|
7930 |
"find a substring. If found, return the index; if not found, return 0." |
|
7931 |
||
7932 |
^ self indexOfSubCollection:aString startingAt:1 ifAbsent:exceptionValue caseSensitive:caseSensitive |
|
7933 |
||
7934 |
" |
|
7935 |
'abcdefabcdef' indexOfString:'fab' ifAbsent:[999] -> 6 |
|
7936 |
'abcdefabcdef' indexOfString:'Fab' ifAbsent:[999] -> 999 |
|
7937 |
'abcdefabcdef' indexOfString:'Fab' caseSensitive:false ifAbsent:[999] -> 6 |
|
7938 |
'abcdefabcdef' indexOfString:'xxx' caseSensitive:false ifAbsent:[999] -> 999 |
|
7939 |
" |
|
7940 |
||
7941 |
"Created: / 08-06-2018 / 14:40:21 / Claus Gittinger" |
|
7942 |
! |
|
7943 |
||
7944 |
indexOfString:aString caseSensitive:caseSensitive startingAt:startIndex |
|
7945 |
"VSE and V'age compatibility" |
|
7946 |
"find a substring. If found, return the index; if not found, return 0." |
|
7947 |
||
7948 |
^ self indexOfSubCollection:aString startingAt:startIndex ifAbsent:[0] caseSensitive:caseSensitive |
|
7949 |
||
7950 |
"Created: / 08-06-2018 / 14:44:17 / Claus Gittinger" |
|
7951 |
! |
|
7952 |
||
7953 |
indexOfString:aString caseSensitive:caseSensitive startingAt:startIndex ifAbsent:exceptionalValue |
|
7954 |
"VSE and V'age compatibility" |
|
7955 |
"find a substring. |
|
7956 |
If found, return the index; if not found, the value from exceptionalValue." |
|
7957 |
||
7958 |
^ self indexOfSubCollection:aString startingAt:startIndex ifAbsent:exceptionalValue caseSensitive:caseSensitive |
|
7959 |
||
7960 |
"Created: / 08-06-2018 / 14:42:33 / Claus Gittinger" |
|
7961 |
! |
|
7962 |
||
7963 |
indexOfString:aString ifAbsent:exceptionValue |
|
7964 |
"VSE and V'age compatibility" |
|
7965 |
"find a substring. If found, return the index; if not found, return 0." |
|
7966 |
||
7967 |
^ self indexOfSubCollection:aString startingAt:1 ifAbsent:exceptionValue caseSensitive:true |
|
7968 |
||
7969 |
" |
|
7970 |
'abcdefabcdef' indexOfString:'fab' ifAbsent:[999] -> 6 |
|
7971 |
'abcdefabcdef' indexOfString:'Fab' ifAbsent:[999] -> 999 |
|
7972 |
'abcdefabcdef' indexOfString:'Fab' caseSensitieve:false ifAbsent:[999] -> 999 |
|
7973 |
'abcdefabcdef' indexOfString:'xxx' ifAbsent:[999] -> 999 |
|
7974 |
" |
|
7975 |
||
7976 |
"Modified: / 08-06-2018 / 14:43:44 / Claus Gittinger" |
|
7977 |
! |
|
7978 |
||
7979 |
indexOfString:aString startingAt:startIndex |
|
7980 |
"VSE and V'age compatibility" |
|
7981 |
"find a substring. If found, return the index; if not found, return 0." |
|
7982 |
||
7983 |
^ self indexOfSubCollection:aString startingAt:startIndex ifAbsent:[0] caseSensitive:true |
|
7984 |
||
7985 |
"Modified: / 08-06-2018 / 14:37:52 / Claus Gittinger" |
|
7986 |
! |
|
7987 |
||
7988 |
indexOfString:aString startingAt:startIndex ifAbsent:exceptionalValue |
|
7989 |
"VSE and V'age compatibility" |
|
7990 |
"find a substring. |
|
7991 |
If found, return the index; if not found, the value from exceptionalValue." |
|
7992 |
||
7993 |
^ self indexOfSubCollection:aString startingAt:startIndex ifAbsent:exceptionalValue caseSensitive:true |
|
7994 |
||
7995 |
"Modified: / 08-06-2018 / 14:38:12 / Claus Gittinger" |
|
7996 |
! |
|
7997 |
||
18671 | 7998 |
indexOfSubCollection:subString caseSensitive:caseSensitive |
7999 |
"find a substring, starting at index. if found, return the index; |
|
21675 | 8000 |
if not found, return the result of evaluating exceptionBlock." |
18671 | 8001 |
|
8002 |
^ self indexOfSubCollection:subString startingAt:1 ifAbsent:[0] caseSensitive:caseSensitive. |
|
21675 | 8003 |
|
8004 |
"Modified (comment): / 28-03-2017 / 15:53:30 / stefan" |
|
18671 | 8005 |
! |
8006 |
||
9192 | 8007 |
indexOfSubCollection:subString startingAt:index ifAbsent:exceptionBlock |
8008 |
"find a substring, starting at index. if found, return the index; |
|
21675 | 8009 |
if not found, return the result of evaluating exceptionBlock." |
9192 | 8010 |
|
8011 |
^ self indexOfSubCollection:subString startingAt:index ifAbsent:exceptionBlock caseSensitive:true. |
|
21675 | 8012 |
|
8013 |
"Modified (comment): / 28-03-2017 / 15:53:14 / stefan" |
|
9192 | 8014 |
! |
8015 |
||
8016 |
indexOfSubCollection:subString startingAt:index ifAbsent:exceptionBlock caseSensitive:caseSensitive |
|
9087
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
8017 |
"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
|
8018 |
if not found, return the result of evaluating exceptionBlock. |
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
8019 |
This is a q&d hack - not very efficient" |
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
8020 |
|
608 | 8021 |
|firstChar found |
8022 |
startIndex "{ Class: SmallInteger }" |
|
8023 |
subSize "{ Class: SmallInteger }" |
|
8024 |
mySize "{ Class: SmallInteger }" |
|
21675 | 8025 |
runIdx "{ Class: SmallInteger }"| |
608 | 8026 |
|
8027 |
subSize := subString size. |
|
12177 | 8028 |
subSize == 0 ifTrue:[ |
20874 | 8029 |
subString isString ifFalse:[ |
23001 | 8030 |
self proceedableError:'non string argument'. |
20874 | 8031 |
]. |
8032 |
"empty string does not match" |
|
8033 |
^ 0. |
|
8034 |
"empty string matches" |
|
15433 | 8035 |
"/ ^ index |
6122
a98958f1c85f
handle non-string arg to indexOfSubString:
Claus Gittinger <cg@exept.de>
parents:
6119
diff
changeset
|
8036 |
]. |
9087
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
8037 |
|
608 | 8038 |
mySize := self size. |
8039 |
firstChar := subString at:1. |
|
9087
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
8040 |
caseSensitive ifTrue:[ |
20874 | 8041 |
startIndex := self indexOf:firstChar startingAt:index. |
9087
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
8042 |
] ifFalse:[ |
20874 | 8043 |
startIndex := self findFirst:[:c | c sameAs:firstChar] startingAt:index. |
9087
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
8044 |
]. |
21672 | 8045 |
[startIndex ~~ 0] whileTrue:[ |
20874 | 8046 |
runIdx := startIndex. |
8047 |
found := true. |
|
8048 |
1 to:subSize do:[:i | |
|
8049 |
runIdx > mySize ifTrue:[ |
|
8050 |
found := false |
|
8051 |
] ifFalse:[ |
|
21675 | 8052 |
caseSensitive ifTrue:[ |
8053 |
(subString at:i) ~= (self at:runIdx) ifTrue:[found := false]. |
|
8054 |
] ifFalse:[ |
|
8055 |
((subString at:i) sameAs:(self at:runIdx)) ifFalse:[found := false]. |
|
8056 |
]. |
|
20874 | 8057 |
]. |
8058 |
runIdx := runIdx + 1 |
|
8059 |
]. |
|
8060 |
found ifTrue:[ |
|
8061 |
^ startIndex |
|
8062 |
]. |
|
8063 |
caseSensitive ifTrue:[ |
|
8064 |
startIndex := self indexOf:firstChar startingAt:(startIndex + 1) |
|
8065 |
] ifFalse:[ |
|
8066 |
startIndex := self findFirst:[:c | c sameAs:firstChar] startingAt:(startIndex + 1). |
|
8067 |
]. |
|
608 | 8068 |
]. |
8069 |
^ exceptionBlock value |
|
1005
4507487e0be6
more on encoding/decoding (still unfinished)
Claus Gittinger <cg@exept.de>
parents:
997
diff
changeset
|
8070 |
|
21675 | 8071 |
" |
8072 |
'АБВГДЕЖ' asLowercase indexOfSubCollection:'ВГДЕ' startingAt:1 ifAbsent:nil caseSensitive:false. |
|
8073 |
'АБВГДЕЖ' indexOfSubCollection:'ВГДЕ' startingAt:1 ifAbsent:nil caseSensitive:true. |
|
8074 |
" |
|
8075 |
||
21672 | 8076 |
"Modified: / 23-02-1996 / 15:35:15 / cg" |
21675 | 8077 |
"Modified (comment): / 28-03-2017 / 16:05:48 / stefan" |
23001 | 8078 |
"Modified: / 24-05-2018 / 14:55:38 / Claus Gittinger" |
9192 | 8079 |
! |
8080 |
||
17191 | 8081 |
indexOfSubCollection:subString startingAt:index ifAbsent:exceptionBlock caseSensitive:caseSensitive ignoreDiacritics:ignoreDiacritics |
8082 |
"find a substring, starting at index. if found, return the index; |
|
8083 |
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
|
8084 |
This is a q&d hack - not very efficient |
17191 | 8085 |
(see implementation in string, for a much faster algorithm)" |
8086 |
||
8087 |
|firstChar found |
|
8088 |
startIndex "{ Class: SmallInteger }" |
|
8089 |
subSize "{ Class: SmallInteger }" |
|
8090 |
mySize "{ Class: SmallInteger }" |
|
8091 |
runIdx "{ Class: SmallInteger }" |
|
8092 |
tester charMap firstCharMapped findNextIndex| |
|
8093 |
||
8094 |
subSize := subString size. |
|
8095 |
subSize == 0 ifTrue:[ |
|
20874 | 8096 |
subString isString ifFalse:[ |
23001 | 8097 |
self proceedableError:'non string argument'. |
20874 | 8098 |
]. |
8099 |
"empty string does not match" |
|
8100 |
^ 0. |
|
8101 |
"empty string matches" |
|
17191 | 8102 |
"/ ^ index |
8103 |
]. |
|
8104 |
||
8105 |
mySize := self size. |
|
8106 |
firstChar := subString at:1. |
|
8107 |
ignoreDiacritics ifTrue:[ |
|
20874 | 8108 |
caseSensitive ifTrue:[ |
8109 |
charMap := [:ch | ch asLowercase withoutDiacritics]. |
|
8110 |
] ifFalse:[ |
|
8111 |
charMap := [:ch | ch withoutDiacritics]. |
|
8112 |
]. |
|
8113 |
tester := [:c1 :c2 | (charMap value:c1) = (charMap value:c2) ]. |
|
8114 |
firstCharMapped := (charMap value:firstChar). |
|
8115 |
findNextIndex := [:index | self findFirst:[:ch | (charMap value:ch) = firstCharMapped] startingAt:index]. |
|
17191 | 8116 |
] ifFalse:[ |
20874 | 8117 |
caseSensitive ifTrue:[ |
8118 |
tester := [:c1 :c2 | c1 = c2 ]. |
|
8119 |
findNextIndex := [:index | self indexOf:firstChar startingAt:index]. |
|
8120 |
] ifFalse:[ |
|
8121 |
tester := [:c1 :c2 | c1 sameAs: c2 ]. |
|
8122 |
findNextIndex := [:index | self findFirst:[:c | c sameAs:firstChar] startingAt:index]. |
|
8123 |
]. |
|
17191 | 8124 |
]. |
8125 |
startIndex := findNextIndex value:index. |
|
8126 |
[startIndex == 0] whileFalse:[ |
|
20874 | 8127 |
runIdx := startIndex. |
8128 |
found := true. |
|
8129 |
1 to:subSize do:[:i | |
|
8130 |
runIdx > mySize ifTrue:[ |
|
8131 |
found := false |
|
8132 |
] ifFalse:[ |
|
8133 |
(tester value:(subString at:i) value:(self at:runIdx)) ifFalse:[ |
|
8134 |
found := false |
|
8135 |
] |
|
8136 |
]. |
|
8137 |
runIdx := runIdx + 1 |
|
8138 |
]. |
|
8139 |
found ifTrue:[ |
|
8140 |
^ startIndex |
|
8141 |
]. |
|
8142 |
startIndex := findNextIndex value:(startIndex + 1) |
|
17191 | 8143 |
]. |
8144 |
^ exceptionBlock value |
|
8145 |
||
8146 |
" |
|
17600 | 8147 |
'bla depot bla dépots' indexOfSubCollection:'dep' startingAt:1 ifAbsent:0 caseSensitive:false ignoreDiacritics:true. 5 |
8148 |
'bla depot bla dépots' indexOfSubCollection:'dep' startingAt:6 ifAbsent:0 caseSensitive:false ignoreDiacritics:true. 0 |
|
8149 |
||
8150 |
'bla depot bla dépots' indexOfSubCollection:'dep' startingAt:1 ifAbsent:0 caseSensitive:false ignoreDiacritics:false. 5 |
|
8151 |
'bla depot bla dépots' indexOfSubCollection:'dep' startingAt:6 ifAbsent:0 caseSensitive:false ignoreDiacritics:false. 0 |
|
17191 | 8152 |
" |
23001 | 8153 |
|
8154 |
"Modified: / 24-05-2018 / 14:55:43 / Claus Gittinger" |
|
17191 | 8155 |
! |
8156 |
||
18939 | 8157 |
lastIndexOfString:aString |
8158 |
"VSE and V'age compatibility" |
|
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
8159 |
"find the last occurrence of substring. |
18939 | 8160 |
If found, return the index; if not found, return 0." |
8161 |
||
8162 |
^ self lastIndexOfSubCollection:aString startingAt:(self size-aString size+1) ifAbsent:[0] |
|
8163 |
||
8164 |
" 123456789012 |
|
8165 |
'abcdefabcdef' lastIndexOfString:'abc' |
|
8166 |
'abcdefabcdef' lastIndexOfString:'abc' startingAt:6 |
|
8167 |
'abcdefabcdef' lastIndexOfString:'xxx' startingAt:6 |
|
8168 |
" |
|
8169 |
! |
|
8170 |
||
8171 |
lastIndexOfString:aString ifAbsent:exceptionValue |
|
8172 |
"VSE and V'age compatibility" |
|
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
8173 |
"find the last occurrence of substring. |
18939 | 8174 |
If found, return the index; if not found, return 0." |
8175 |
||
8176 |
^ self lastIndexOfSubCollection:aString startingAt:(self size-aString size+1) ifAbsent:exceptionValue |
|
8177 |
||
8178 |
" 123456789012 |
|
8179 |
'abcdefabcdef' lastIndexOfString:'abc' ifAbsent:[999] |
|
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
8180 |
'abcdefabcdef' lastIndexOfString:'xxx' ifAbsent:[999] |
18939 | 8181 |
" |
8182 |
! |
|
8183 |
||
8184 |
lastIndexOfString:aString startingAt:startIndex |
|
8185 |
"VSE and V'age compatibility" |
|
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
8186 |
"find the last occurrence of a substring. |
18939 | 8187 |
If found, return the index; if not found, return 0." |
8188 |
||
8189 |
^ self lastIndexOfSubCollection:aString startingAt:startIndex ifAbsent:[0] |
|
8190 |
||
8191 |
" 123456789012 |
|
8192 |
'abcdefabcdef' lastIndexOfString:'abc' |
|
8193 |
'abcdefabcdef' lastIndexOfString:'abc' startingAt:6 |
|
8194 |
" |
|
8195 |
! |
|
8196 |
||
8197 |
lastIndexOfString:aString startingAt:startIndex ifAbsent:exceptionValue |
|
8198 |
"VSE and V'age compatibility" |
|
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
8199 |
"find the last occurrence of a substring. |
18939 | 8200 |
If found, return the index; if not found, return 0." |
8201 |
||
8202 |
^ self lastIndexOfSubCollection:aString startingAt:startIndex ifAbsent:exceptionValue |
|
8203 |
||
8204 |
" 123456789012 |
|
8205 |
'abcdefabcdef' lastIndexOfString:'abc' |
|
8206 |
'abcdefabcdef' lastIndexOfString:'abc' startingAt:6 |
|
8207 |
'abcdefabcdef' lastIndexOfString:'xxx' startingAt:6 ifAbsent:999 |
|
8208 |
" |
|
8209 |
! |
|
8210 |
||
15055 | 8211 |
rangeOfSubCollection:subString startingAt:start ifAbsent:exceptionValue caseSensitive:caseSensitive |
8212 |
"find a substring. if found, return the start- and endIndex; |
|
8213 |
if not found, return the value of exceptionValue." |
|
8214 |
||
8215 |
|i| |
|
8216 |
||
8217 |
i := self indexOfSubCollection:subString startingAt:start ifAbsent:0 caseSensitive:caseSensitive. |
|
8218 |
i == 0 ifTrue:[ |
|
20874 | 8219 |
^ exceptionValue value |
15055 | 8220 |
]. |
8221 |
^ i to:(i + subString size - 1) |
|
8222 |
||
8223 |
" |
|
8224 |
'hello world' findRangeOfString:'llo' |
|
8225 |
'hello world' findRangeOfString:'ole' |
|
8226 |
" |
|
9192 | 8227 |
! ! |
8228 |
||
8229 |
!CharacterArray methodsFor:'testing'! |
|
8230 |
||
18463 | 8231 |
argumentCount |
8232 |
"treating the receiver as a message selector, return how many arguments would it take (ANSI)" |
|
8233 |
||
8234 |
|binopChars| |
|
8235 |
||
8236 |
(self size <= Method maxBinarySelectorSize) ifTrue:[ |
|
20874 | 8237 |
binopChars := Method binarySelectorCharacters. |
8238 |
(self conform:[:eachChar | (binopChars includes:eachChar)]) ifTrue:[ |
|
8239 |
^ 1 |
|
8240 |
]. |
|
18463 | 8241 |
]. |
8242 |
^ self occurrencesOf:$: |
|
8243 |
||
8244 |
" |
|
8245 |
'foo:bar:' argumentCount |
|
8246 |
#foo:bar: argumentCount |
|
8247 |
'hello' argumentCount |
|
8248 |
'+' argumentCount |
|
8249 |
'++' argumentCount |
|
8250 |
'+++' argumentCount |
|
8251 |
'|' argumentCount |
|
8252 |
'?' argumentCount |
|
8253 |
'_' argumentCount |
|
8254 |
'_:' argumentCount |
|
8255 |
'_:_:' argumentCount |
|
8256 |
'<->' argumentCount |
|
21352 | 8257 |
'<' argumentCount |
8258 |
#'<' argumentCount |
|
8259 |
" |
|
8260 |
||
8261 |
"Modified (comment): / 06-02-2017 / 13:48:57 / cg" |
|
18463 | 8262 |
! |
8263 |
||
18597 | 8264 |
characterSize |
8265 |
"answer the size in bits of my largest character (actually only 7, 8, 16 or 32)" |
|
8266 |
||
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
8267 |
|string max |
18597 | 8268 |
sz "{ Class:SmallInteger}" | |
15829 | 8269 |
|
8270 |
(string := self string) ~~ self ifTrue:[ |
|
20874 | 8271 |
^ string characterSize. |
18597 | 8272 |
]. |
8273 |
||
8274 |
sz := self size. |
|
8275 |
max := 7. |
|
8276 |
1 to:sz do:[:idx | |
|
20874 | 8277 |
|thisSize| |
8278 |
||
8279 |
thisSize := (self at:idx) characterSize. |
|
8280 |
thisSize > max ifTrue:[ |
|
8281 |
max := thisSize. |
|
8282 |
max == 32 ifTrue:[ |
|
8283 |
"shortcut: we know, that max size is 32" |
|
8284 |
^ 32. |
|
8285 |
]. |
|
8286 |
]. |
|
15829 | 8287 |
]. |
18597 | 8288 |
|
8289 |
^ max. |
|
8290 |
||
8291 |
" |
|
8292 |
'hello' characterSize |
|
8293 |
'hello' asUnicode32String characterSize |
|
8294 |
'helloü' asUnicode32String characterSize |
|
8295 |
'hello' asText allBold characterSize |
|
8296 |
" |
|
8297 |
! |
|
8298 |
||
8299 |
contains8BitCharacters |
|
8300 |
<resource: #obsolete> |
|
8301 |
||
8302 |
^ self containsNon7BitAscii |
|
8303 |
! |
|
8304 |
||
8305 |
containsNon7BitAscii |
|
8306 |
"return true, if the underlying string contains 8BitCharacters (or widers) |
|
8307 |
(i.e. if it is non-ascii)" |
|
8308 |
||
8309 |
|string |
|
8310 |
sz "{ Class:SmallInteger }"| |
|
8311 |
||
8312 |
(string := self string) ~~ self ifTrue:[ |
|
20874 | 8313 |
^ string containsNon7BitAscii |
18597 | 8314 |
]. |
8315 |
sz := self size. |
|
8316 |
1 to:sz do:[:idx| |
|
20874 | 8317 |
(self at:idx) codePoint > 16r7F ifTrue:[ |
8318 |
^ true. |
|
8319 |
]. |
|
18597 | 8320 |
]. |
8321 |
^ false. |
|
8322 |
||
8323 |
" |
|
8324 |
'hello' asUnicode32String containsNon7BitAscii |
|
8325 |
'hello üöä' asUnicode32String containsNon7BitAscii |
|
8326 |
'hello' asUnicode32String asText allBold containsNon7BitAscii |
|
8327 |
'hello üö' asUnicode32String asText allBold containsNon7BitAscii |
|
15829 | 8328 |
" |
8329 |
! |
|
8330 |
||
9192 | 8331 |
isAlphaNumeric |
8332 |
"return true, if the receiver is some alphanumeric word; |
|
8333 |
i.e. consists of a letter followed by letters or digits." |
|
8334 |
||
22126 | 8335 |
self isEmpty ifTrue:[ |
20874 | 8336 |
"mhmh what is this ?" |
8337 |
^ false |
|
9192 | 8338 |
]. |
8339 |
(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
|
8340 |
^ self conform:[:char | char isLetterOrDigit]. |
8cff5eca1c6f
code cleanup: use #contains/#conform instead of explicit loop
Claus Gittinger <cg@exept.de>
parents:
10088
diff
changeset
|
8341 |
|
8cff5eca1c6f
code cleanup: use #contains/#conform instead of explicit loop
Claus Gittinger <cg@exept.de>
parents:
10088
diff
changeset
|
8342 |
" |
10703
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
8343 |
'helloWorld' isAlphaNumeric |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
8344 |
'foo1234' isAlphaNumeric |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
8345 |
'f1234' isAlphaNumeric |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
8346 |
'1234' isAlphaNumeric |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
8347 |
'+' isAlphaNumeric |
10093
8cff5eca1c6f
code cleanup: use #contains/#conform instead of explicit loop
Claus Gittinger <cg@exept.de>
parents:
10088
diff
changeset
|
8348 |
" |
8cff5eca1c6f
code cleanup: use #contains/#conform instead of explicit loop
Claus Gittinger <cg@exept.de>
parents:
10088
diff
changeset
|
8349 |
|
8cff5eca1c6f
code cleanup: use #contains/#conform instead of explicit loop
Claus Gittinger <cg@exept.de>
parents:
10088
diff
changeset
|
8350 |
"Modified: / 13-10-2006 / 12:53:49 / cg" |
9192 | 8351 |
! |
9190 | 8352 |
|
8353 |
isBinarySelector |
|
21425 | 8354 |
"treating the receiver as a message selector, return true if it's a binary selector. |
10903 | 8355 |
Notice, that st/x does not have a size <= 2 limit for unaries" |
9190 | 8356 |
|
8357 |
|binopChars| |
|
8358 |
||
18463 | 8359 |
(self size > Method maxBinarySelectorSize) ifTrue:[^ false]. |
8360 |
||
8361 |
binopChars := Method binarySelectorCharacters. |
|
9190 | 8362 |
^ (self conform:[:char | (binopChars includes:char)]) |
8363 |
||
8364 |
" |
|
8365 |
'foo:bar:' isBinarySelector |
|
8366 |
#foo:bar: isBinarySelector |
|
8367 |
'hello' isBinarySelector |
|
8368 |
'+' isBinarySelector |
|
8369 |
'|' isBinarySelector |
|
8874 | 8370 |
'?' isBinarySelector |
8371 |
':' isBinarySelector |
|
8372 |
'a:' isBinarySelector |
|
8373 |
'->' isBinarySelector |
|
8374 |
'<->' isBinarySelector |
|
8375 |
'::' isBinarySelector |
|
7482 | 8376 |
" |
8377 |
||
21425 | 8378 |
"Modified: / 04-01-1997 / 14:16:14 / cg" |
8379 |
"Modified (comment): / 13-02-2017 / 19:57:29 / cg" |
|
9192 | 8380 |
! |
8381 |
||
8382 |
isBlank |
|
8383 |
"return true, if the receiver contains spaces only" |
|
8384 |
||
9230 | 8385 |
^ (self contains:[:char | char ~~ Character space]) not |
8386 |
||
8387 |
" |
|
10703
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
8388 |
'' isBlank |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
8389 |
' a ' isBlank |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
8390 |
' ' isBlank |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
8391 |
' ' asUnicode16String isBlank |
9230 | 8392 |
" |
9192 | 8393 |
! |
8394 |
||
21126 | 8395 |
isInfix |
8396 |
"return true, if the receiver is a binary message selector" |
|
8397 |
||
8398 |
^ self first isLetterOrDigit not |
|
8399 |
||
8400 |
" |
|
8401 |
#at:put: isInfix |
|
8402 |
#at: isInfix |
|
8403 |
#+ isInfix |
|
8404 |
#size isInfix |
|
8405 |
" |
|
8406 |
||
8407 |
"Created: / 1.11.1997 / 12:34:55 / cg" |
|
8408 |
"Modified: / 1.11.1997 / 12:36:37 / cg" |
|
8409 |
! |
|
8410 |
||
8411 |
isKeyword |
|
8412 |
"return true, if the receiver is a keyword message selector. |
|
8413 |
This is a quick check, which only looks at the last character. |
|
8414 |
Should only be used, if we already know that the receiver forms a valid selector. |
|
8415 |
To check an arbitrary string, use isKeywordSelector. |
|
8416 |
Bad naming, but compatibility is asking for it." |
|
8417 |
||
8418 |
^ self last == $: |
|
8419 |
||
8420 |
" |
|
8421 |
#at:put: isKeyword |
|
8422 |
#at: isKeyword |
|
8423 |
#+ isKeyword |
|
8424 |
#size isKeyword |
|
8425 |
" |
|
8426 |
||
8427 |
"Created: / 01-11-1997 / 12:34:55 / cg" |
|
8428 |
"Modified (comment): / 30-04-2016 / 18:19:11 / cg" |
|
8429 |
! |
|
8430 |
||
10903 | 8431 |
isKeywordSelector |
8432 |
"return true, iff there are only alphanumeric or underline characters separated by colons. |
|
19682 | 8433 |
Must end with a colon. |
8434 |
You can use this to check an arbitrary string for being valid as a keyword. |
|
8435 |
If you have a valid selector at hand, and need to know if it is a keyword or not, |
|
8436 |
use #isKeyword, which is much faster." |
|
10903 | 8437 |
|
8438 |
|state| |
|
8439 |
||
19682 | 8440 |
(self size == 0) ifTrue:[^ false]. |
8441 |
(self last == $:) ifFalse:[^ false]. |
|
8442 |
||
12177 | 8443 |
state := #initial. |
10903 | 8444 |
self do:[:char | |
20874 | 8445 |
(state == #initial or:[ state == #gotColon]) ifTrue:[ |
8446 |
(char isLetterOrUnderline) ifFalse:[^ false]. |
|
8447 |
state := #gotCharacter. |
|
8448 |
] ifFalse:[ |
|
8449 |
char == $: ifTrue:[ |
|
8450 |
state := #gotColon. |
|
8451 |
] ifFalse:[ |
|
8452 |
(char isLetterOrDigit or:[char == $_]) ifFalse:[^ false]. |
|
8453 |
]. |
|
8454 |
]. |
|
10903 | 8455 |
]. |
8456 |
^ state == #gotColon. |
|
8457 |
||
8458 |
" |
|
12177 | 8459 |
self assert:(':' isKeywordSelector not). |
10903 | 8460 |
self assert:(':a' isKeywordSelector not). |
8461 |
self assert:('1:' isKeywordSelector not). |
|
8462 |
self assert:('a:' isKeywordSelector). |
|
8463 |
self assert:('_:' isKeywordSelector). |
|
8464 |
self assert:('_a:' isKeywordSelector). |
|
8465 |
self assert:('_1:' isKeywordSelector). |
|
8466 |
self assert:('_1::' isKeywordSelector not). |
|
8467 |
self assert:('_:_:' isKeywordSelector). |
|
8468 |
self assert:('a:b:' isKeywordSelector). |
|
8469 |
self assert:('aa:bb:' isKeywordSelector). |
|
8470 |
self assert:('aa:bb:a' isKeywordSelector not). |
|
8471 |
self assert:('1:2:' isKeywordSelector not). |
|
8472 |
" |
|
19682 | 8473 |
|
8474 |
"Modified (comment): / 30-04-2016 / 18:20:14 / cg" |
|
10903 | 8475 |
! |
8476 |
||
10431 | 8477 |
isNameSpaceSelector |
8478 |
"Answer true if the receiver contains chars which form a nameSpace selector name. |
|
8479 |
These are of the form ':<ns>::<sel>', where ns is the NameSpace and sel is the regular selector. |
|
8480 |
For example, the #+ selector as seen by the Foo namespace would be actually #':Foo::+'. |
|
17511 | 8481 |
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
|
8482 |
is legal, and this can be checked quickly by just looking at the first character. |
10431 | 8483 |
You cannot easily change this algorithm here, as it is also known by the VM's lookup function." |
8484 |
||
8485 |
|i| |
|
8486 |
||
16733 | 8487 |
(self at:1) == $: ifFalse:[^ false]. |
17511 | 8488 |
|
10431 | 8489 |
i := self indexOf:$: startingAt:2. |
8490 |
i == 0 ifTrue:[^ false]. |
|
8491 |
self size <= (i+1) ifTrue:[^ false]. |
|
8492 |
(self at:i+1) == $: ifFalse:[^ false]. |
|
8493 |
(self at:i+2) == $: ifTrue:[^ false]. |
|
8494 |
"/ could check if the rest after the ns-prefix and colons is a valid selector... |
|
8495 |
^ true |
|
8496 |
||
8497 |
"test: |
|
10703
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
8498 |
self assert:('+' isNameSpaceSelector) not. |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
8499 |
self assert:(':+' isNameSpaceSelector) not. |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
8500 |
self assert:(':Foo:+' isNameSpaceSelector) not. |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
8501 |
|
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
8502 |
self assert:(':Foo::+' isNameSpaceSelector). |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
8503 |
self assert:(':Foo::bar:baz:' isNameSpaceSelector). |
10431 | 8504 |
" |
8505 |
||
8506 |
"Created: / 05-03-2007 / 11:35:31 / cg" |
|
8507 |
! |
|
8508 |
||
9192 | 8509 |
isNumeric |
8510 |
"return true, if the receiver is some numeric word; |
|
8511 |
i.e. consists only of digits." |
|
8512 |
||
8513 |
self size == 0 ifTrue:[ |
|
20874 | 8514 |
^ false |
9192 | 8515 |
]. |
10093
8cff5eca1c6f
code cleanup: use #contains/#conform instead of explicit loop
Claus Gittinger <cg@exept.de>
parents:
10088
diff
changeset
|
8516 |
^ self conform:[:char | char isDigit] |
9192 | 8517 |
|
8518 |
" |
|
8519 |
'helloWorld' isNumeric |
|
8520 |
'foo1234' isNumeric |
|
8521 |
'f1234' isNumeric |
|
8522 |
'1234' isNumeric |
|
8523 |
'+' isNumeric |
|
8524 |
" |
|
10093
8cff5eca1c6f
code cleanup: use #contains/#conform instead of explicit loop
Claus Gittinger <cg@exept.de>
parents:
10088
diff
changeset
|
8525 |
|
8cff5eca1c6f
code cleanup: use #contains/#conform instead of explicit loop
Claus Gittinger <cg@exept.de>
parents:
10088
diff
changeset
|
8526 |
"Modified: / 13-10-2006 / 12:54:12 / cg" |
9192 | 8527 |
! |
8528 |
||
18893 | 8529 |
isPlainString |
8530 |
"return true, if the receiver is a plain string (without attributes); |
|
8531 |
true is returned here - redefinition of Object>>isPlainString." |
|
8532 |
||
8533 |
^ true |
|
8534 |
! |
|
8535 |
||
9230 | 8536 |
isString |
8537 |
"return true, if the receiver is some kind of string; |
|
8538 |
true is returned here - redefinition of Object>>isString." |
|
8539 |
||
8540 |
^ true |
|
8541 |
! |
|
8542 |
||
21126 | 8543 |
isUnary |
8544 |
"Answer true if the receiver is a unary selector. |
|
8545 |
That is not a check for being a valid selector, but instead relies on |
|
8546 |
the selector being valid." |
|
8547 |
||
8548 |
^ self isUnarySelector |
|
8549 |
! |
|
8550 |
||
9230 | 8551 |
isUnarySelector |
8552 |
"Answer true if the receiver contains only chars in an ANSI unary method selector, false otherwise." |
|
8553 |
||
17442 | 8554 |
^ (self first isLetterOrUnderline) |
9845 | 8555 |
and:[ self conform: [ :chr | chr isLetterOrDigit or:[chr == $_] ]] |
8556 |
||
8557 |
"Modified: / 13-09-2006 / 11:35:15 / cg" |
|
9230 | 8558 |
! |
8559 |
||
14122 | 8560 |
isUnicode16String |
8561 |
^ false |
|
8562 |
! |
|
8563 |
||
8564 |
isUnicode32String |
|
19939 | 8565 |
"true if this is a 4-byte unicode string" |
8566 |
||
14122 | 8567 |
^ false |
8568 |
! |
|
8569 |
||
8570 |
isUnicodeString |
|
19938 | 8571 |
"true if this is a 2- or 4-byte unicode string |
8572 |
(i.e. not a single byte string). |
|
20194 | 8573 |
Notice, that the name is misleading: |
19938 | 8574 |
all strings are use unicode encoding" |
20194 | 8575 |
|
14122 | 8576 |
^ false |
8577 |
! |
|
8578 |
||
9230 | 8579 |
isValidSmalltalkIdentifier |
19464 | 8580 |
"return true, if the receiver's characters make up a valid smalltalk identifier" |
9230 | 8581 |
|
8582 |
|scanner tok| |
|
8583 |
||
8584 |
scanner := Compiler new. |
|
8585 |
scanner source:(self readStream). |
|
22953 | 8586 |
ParseError handle:[:ex | |
20874 | 8587 |
tok := nil. |
11630
6db5956f188f
isValidSmalltalkIdentifier: return true/false (catch parseError)
Claus Gittinger <cg@exept.de>
parents:
11565
diff
changeset
|
8588 |
] do:[ |
20874 | 8589 |
tok := scanner nextToken. |
11630
6db5956f188f
isValidSmalltalkIdentifier: return true/false (catch parseError)
Claus Gittinger <cg@exept.de>
parents:
11565
diff
changeset
|
8590 |
]. |
9230 | 8591 |
tok ~~ #Identifier ifTrue:[ |
20874 | 8592 |
^ false |
9230 | 8593 |
]. |
8594 |
scanner tokenPosition == 1 ifFalse:[^ false]. |
|
8595 |
^ scanner sourceStream atEnd. |
|
8596 |
||
8597 |
" |
|
8598 |
'foo' isValidSmalltalkIdentifier |
|
8599 |
'1foo' isValidSmalltalkIdentifier |
|
8600 |
'_foo' isValidSmalltalkIdentifier |
|
8601 |
'_foo_bar_' isValidSmalltalkIdentifier |
|
8602 |
'foo ' isValidSmalltalkIdentifier |
|
8603 |
' foo' isValidSmalltalkIdentifier |
|
8604 |
" |
|
8605 |
! |
|
8606 |
||
21619 | 8607 |
isWhitespace |
8608 |
"return true, if the receiver is empty or contains only whitespace." |
|
8609 |
||
8610 |
^ (self indexOfNonSeparatorStartingAt:1) == 0 |
|
8611 |
||
8612 |
" |
|
8613 |
'' isWhitespace |
|
8614 |
' ' isWhitespace |
|
8615 |
' \ \' withCRs isWhitespace |
|
8616 |
' a\ \' withCRs isWhitespace |
|
8617 |
' \ \a' withCRs isWhitespace |
|
8618 |
'a \ \a' withCRs isWhitespace |
|
8619 |
" |
|
8620 |
||
8621 |
"Created: / 01-03-2017 / 15:24:53 / cg" |
|
8622 |
! |
|
8623 |
||
14555 | 8624 |
isWideString |
19580 | 8625 |
"true if I require more than one byte per character" |
19775
cfffb072d9a3
withoutPrefix/withoutSuffix
Claus Gittinger <cg@exept.de>
parents:
19774
diff
changeset
|
8626 |
|
15829 | 8627 |
|string| |
8628 |
||
8629 |
(string := self string) ~~ self ifTrue:[ |
|
20874 | 8630 |
^ string isWideString. |
15829 | 8631 |
]. |
8632 |
^ self contains:[:aCharacter | aCharacter codePoint > 16rFF]. |
|
14555 | 8633 |
! |
8634 |
||
9192 | 8635 |
numArgs |
18463 | 8636 |
<resource: #obsolete> |
21122 | 8637 |
"treating the receiver as a message selector, return how many arguments would it take. |
8638 |
Please use argumentCount for ANSI compatibility." |
|
608 | 8639 |
|
18463 | 8640 |
^ self argumentCount |
9192 | 8641 |
! |
8642 |
||
8643 |
partsIfSelector |
|
14629 | 8644 |
"treat the receiver as a message selector, return a collection of parts. |
8645 |
Notice: this is more tolerant than Smalltalk's syntax would suggest; |
|
8646 |
especially it allows for empty keyword parts between colons. |
|
8647 |
This is not (and should not be checked here), to allow parsing of |
|
8648 |
degenerate selectors as appearing with objectiveC." |
|
608 | 8649 |
|
9192 | 8650 |
|idx1 "{ Class: SmallInteger }" |
8651 |
coll idx2 sz| |
|
8652 |
||
8653 |
coll := OrderedCollection new. |
|
8654 |
idx1 := 1. |
|
8655 |
sz := self size. |
|
16245 | 8656 |
[ |
20874 | 8657 |
idx2 := self indexOf:$: startingAt:idx1. |
8658 |
(idx2 == 0 or:[idx2 == sz]) ifTrue:[ |
|
8659 |
coll add:(self copyFrom:idx1). |
|
8660 |
^ coll |
|
8661 |
]. |
|
8662 |
coll add:(self copyFrom:idx1 to:idx2). |
|
8663 |
idx1 := idx2 + 1 |
|
16245 | 8664 |
] loop. |
9192 | 8665 |
|
8666 |
" |
|
18350
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
8667 |
'foo:' partsIfSelector |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
8668 |
'foo:bar:' partsIfSelector |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
8669 |
'foo::::' partsIfSelector |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
8670 |
#foo:bar: partsIfSelector |
8f0102bb84f5
a send avoided in endsWith:
Claus Gittinger <cg@exept.de>
parents:
18318
diff
changeset
|
8671 |
'hello' partsIfSelector |
9192 | 8672 |
'+' partsIfSelector |
8673 |
" |
|
64 | 8674 |
! ! |
8675 |
||
9192 | 8676 |
!CharacterArray methodsFor:'tracing'! |
8677 |
||
8678 |
traceInto:aRequestor level:level from:referrer |
|
8679 |
"double dispatch into tracer, passing my type implicitely in the selector" |
|
8680 |
||
8681 |
^ aRequestor traceCharacterArray:self level:level from:referrer |
|
8682 |
||
8683 |
||
8684 |
! ! |
|
8685 |
||
8686 |
!CharacterArray methodsFor:'visiting'! |
|
8395
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8376
diff
changeset
|
8687 |
|
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8376
diff
changeset
|
8688 |
acceptVisitor:aVisitor with:aParameter |
16733 | 8689 |
"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
|
8690 |
|
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8376
diff
changeset
|
8691 |
^ aVisitor visitString:self with:aParameter |
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8376
diff
changeset
|
8692 |
! ! |
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8376
diff
changeset
|
8693 |
|
22953 | 8694 |
|
1849
9f35f5934ec6
added msdos-latin1 en/decoder (code = #msdos850)
Claus Gittinger <cg@exept.de>
parents:
1669
diff
changeset
|
8695 |
!CharacterArray class methodsFor:'documentation'! |
629 | 8696 |
|
8697 |
version |
|
18476 | 8698 |
^ '$Header$' |
12058 | 8699 |
! |
8700 |
||
8701 |
version_CVS |
|
18476 | 8702 |
^ '$Header$' |
629 | 8703 |
! ! |
6809 | 8704 |
|
14606 | 8705 |
|
2728
7569e45a4d15
raise error signal if anything goes wrong while decoding.
Claus Gittinger <cg@exept.de>
parents:
2721
diff
changeset
|
8706 |
CharacterArray initialize! |