author | Claus Gittinger <cg@exept.de> |
Wed, 08 Oct 2014 10:49:47 +0200 | |
changeset 16887 | 8e30e6a52a64 |
parent 16875 | 32e61bb3a742 |
child 16898 | 0663e6b0d177 |
permissions | -rw-r--r-- |
64 | 1 |
" |
2 |
COPYRIGHT (c) 1994 by Claus Gittinger |
|
16202 | 3 |
All Rights Reserved |
64 | 4 |
|
5 |
This software is furnished under a license and may be used |
|
6 |
only in accordance with the terms of that license and with the |
|
7 |
inclusion of the above copyright notice. This software may not |
|
8 |
be provided or otherwise made available to, or used by, any |
|
9 |
other person. No title to or ownership of the software is |
|
10 |
hereby transferred. |
|
11 |
" |
|
5342
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
12 |
"{ Package: 'stx:libbasic' }" |
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
13 |
|
14137
1eb1b4a3f364
Inherit from UninterpretedBytes instead of ByteArray
Stefan Vogel <sv@exept.de>
parents:
14122
diff
changeset
|
14 |
UninterpretedBytes variableByteSubclass:#CharacterArray |
16212 | 15 |
instanceVariableNames:'' |
16 |
classVariableNames:'PreviousMatch DecoderTables EncoderTables DecodingFailedSignal |
|
17 |
EncodingFailedSignal' |
|
18 |
poolDictionaries:'' |
|
19 |
category:'Collections-Text' |
|
64 | 20 |
! |
21 |
||
1849
9f35f5934ec6
added msdos-latin1 en/decoder (code = #msdos850)
Claus Gittinger <cg@exept.de>
parents:
1669
diff
changeset
|
22 |
!CharacterArray class methodsFor:'documentation'! |
64 | 23 |
|
82 | 24 |
copyright |
25 |
" |
|
26 |
COPYRIGHT (c) 1994 by Claus Gittinger |
|
16202 | 27 |
All Rights Reserved |
82 | 28 |
|
29 |
This software is furnished under a license and may be used |
|
30 |
only in accordance with the terms of that license and with the |
|
31 |
inclusion of the above copyright notice. This software may not |
|
32 |
be provided or otherwise made available to, or used by, any |
|
33 |
other person. No title to or ownership of the software is |
|
34 |
hereby transferred. |
|
35 |
" |
|
36 |
! |
|
37 |
||
64 | 38 |
documentation |
39 |
" |
|
255 | 40 |
CharacterArray is a superclass for all kinds of Strings (i.e. |
68 | 41 |
(singleByte-)Strings, TwoByteStrings and whatever comes in the future. |
154 | 42 |
|
8874 | 43 |
This class is abstract, meaning that there are no instances of it. |
2537 | 44 |
All this class does is provide common protocol for concrete subclasses. |
1290 | 45 |
|
46 |
[author:] |
|
16202 | 47 |
Claus Gittinger |
1290 | 48 |
|
49 |
[see also:] |
|
16202 | 50 |
String TwoByteString |
51 |
StringCollection |
|
64 | 52 |
" |
53 |
! ! |
|
54 |
||
2728
7569e45a4d15
raise error signal if anything goes wrong while decoding.
Claus Gittinger <cg@exept.de>
parents:
2721
diff
changeset
|
55 |
!CharacterArray class methodsFor:'initialization'! |
7569e45a4d15
raise error signal if anything goes wrong while decoding.
Claus Gittinger <cg@exept.de>
parents:
2721
diff
changeset
|
56 |
|
7569e45a4d15
raise error signal if anything goes wrong while decoding.
Claus Gittinger <cg@exept.de>
parents:
2721
diff
changeset
|
57 |
initialize |
8874 | 58 |
DecodingFailedSignal isNil ifTrue:[ |
16202 | 59 |
DecodingFailedSignal := DecodingError. |
60 |
DecodingFailedSignal notifierString:'error during decode'. |
|
61 |
||
62 |
EncodingFailedSignal :=EncodingError. |
|
63 |
EncodingFailedSignal notifierString:'error during encode'. |
|
5440 | 64 |
] |
2728
7569e45a4d15
raise error signal if anything goes wrong while decoding.
Claus Gittinger <cg@exept.de>
parents:
2721
diff
changeset
|
65 |
|
7569e45a4d15
raise error signal if anything goes wrong while decoding.
Claus Gittinger <cg@exept.de>
parents:
2721
diff
changeset
|
66 |
" |
7569e45a4d15
raise error signal if anything goes wrong while decoding.
Claus Gittinger <cg@exept.de>
parents:
2721
diff
changeset
|
67 |
CharacterArray initialize |
7569e45a4d15
raise error signal if anything goes wrong while decoding.
Claus Gittinger <cg@exept.de>
parents:
2721
diff
changeset
|
68 |
" |
7569e45a4d15
raise error signal if anything goes wrong while decoding.
Claus Gittinger <cg@exept.de>
parents:
2721
diff
changeset
|
69 |
|
2833 | 70 |
"Modified: 3.8.1997 / 18:15:59 / cg" |
2728
7569e45a4d15
raise error signal if anything goes wrong while decoding.
Claus Gittinger <cg@exept.de>
parents:
2721
diff
changeset
|
71 |
! ! |
7569e45a4d15
raise error signal if anything goes wrong while decoding.
Claus Gittinger <cg@exept.de>
parents:
2721
diff
changeset
|
72 |
|
1849
9f35f5934ec6
added msdos-latin1 en/decoder (code = #msdos850)
Claus Gittinger <cg@exept.de>
parents:
1669
diff
changeset
|
73 |
!CharacterArray class methodsFor:'instance creation'! |
64 | 74 |
|
75 |
basicNew |
|
76 |
"return a new empty string" |
|
77 |
||
78 |
^ self basicNew:0 |
|
79 |
! |
|
80 |
||
2734 | 81 |
fromBytes:aByteCollection |
82 |
"return an instance of the receiver class, |
|
83 |
taking untranslated bytes from the argument, aByteCollection. |
|
84 |
Only useful, when reading twoByteStrings from external sources." |
|
85 |
||
86 |
|mySize nBytes newString dstIdx| |
|
87 |
||
88 |
nBytes := aByteCollection size. |
|
89 |
mySize := self basicNew bitsPerCharacter. |
|
90 |
mySize == 16 ifTrue:[ |
|
16202 | 91 |
newString := self basicNew:(nBytes // 2). |
92 |
dstIdx := 1. |
|
93 |
aByteCollection pairWiseDo:[:hi :lo | |
|
94 |
newString at:dstIdx put:(Character value:(hi bitShift:8)+lo). |
|
95 |
dstIdx := dstIdx + 1 |
|
96 |
]. |
|
97 |
^ newString. |
|
2734 | 98 |
]. |
99 |
||
100 |
^ (self basicNew:nBytes) replaceFrom:1 with:aByteCollection |
|
101 |
||
102 |
" |
|
103 |
TwoByteString fromBytes:#[16r21 16r21] |
|
104 |
" |
|
105 |
||
106 |
"Modified: 30.6.1997 / 20:08:37 / cg" |
|
107 |
! |
|
108 |
||
64 | 109 |
fromString:aString |
110 |
"return a copy of the argument, aString" |
|
111 |
||
68 | 112 |
^ (self basicNew:(aString size)) replaceFrom:1 with:aString |
113 |
||
114 |
"TwoByteString fromString:'hello'" |
|
581 | 115 |
! |
116 |
||
117 |
fromStringCollection:aCollectionOfStrings |
|
118 |
"return new string formed by concatenating a copy of the argument, aString" |
|
119 |
||
120 |
^ self fromStringCollection:aCollectionOfStrings separatedBy:'' |
|
121 |
||
122 |
" |
|
123 |
String fromStringCollection:#('hello' 'world' 'how' 'about' 'this') |
|
124 |
" |
|
125 |
||
126 |
"Created: 20.11.1995 / 15:26:59 / cg" |
|
127 |
! |
|
128 |
||
129 |
fromStringCollection:aCollectionOfStrings separatedBy:aSeparatorString |
|
130 |
"return new string formed by concatenating a copy of the argument, aString" |
|
131 |
||
132 |
|newString first| |
|
133 |
||
134 |
newString := ''. |
|
135 |
first := true. |
|
8874 | 136 |
aCollectionOfStrings do:[:s | |
16202 | 137 |
first ifFalse:[ |
138 |
newString := newString , aSeparatorString |
|
139 |
] ifTrue:[ |
|
140 |
first := false |
|
141 |
]. |
|
142 |
newString := newString , s |
|
581 | 143 |
]. |
144 |
^ newString |
|
145 |
||
146 |
" |
|
147 |
String fromStringCollection:#('hello' 'world' 'how' 'about' 'this') separatedBy:' ' |
|
148 |
" |
|
149 |
||
150 |
"Created: 20.11.1995 / 15:32:17 / cg" |
|
608 | 151 |
! |
152 |
||
5757 | 153 |
fromUTF8Bytes:aByteCollection |
5758 | 154 |
"return a new string which represents the characters as decoded |
155 |
from the utf8 encoded bytes, aByteCollection. |
|
156 |
Returns either a normal String, or a TwoByteString instance. |
|
6810 | 157 |
Only useful, when reading twoByteStrings from external sources. |
158 |
This only handles up-to 16bit characters" |
|
5757 | 159 |
|
7876 | 160 |
^ self decodeFromUTF8:aByteCollection. |
5757 | 161 |
|
162 |
" |
|
8874 | 163 |
CharacterArray fromUTF8Bytes:#[ 16r41 16r42 ] |
164 |
CharacterArray fromUTF8Bytes:#[ 16rC1 16r02 ] |
|
165 |
CharacterArray fromUTF8Bytes:#[ 16rE0 16r81 16r02 ] |
|
6810 | 166 |
CharacterArray fromUTF8Bytes:#[ 16rEF 16rBF 16rBF ] |
167 |
||
168 |
rfc2279 examples: |
|
8874 | 169 |
CharacterArray fromUTF8Bytes:#[ 16r41 16rE2 16r89 16rA2 16rCE 16r91 16r2E ] |
170 |
CharacterArray fromUTF8Bytes:#[ 16rED 16r95 16r9C 16rEA 16rB5 16rAD 16rEC 16r96 16rB4 ] |
|
171 |
CharacterArray fromUTF8Bytes:#[ 16rE6 16r97 16rA5 16rE6 16r9C 16rAC 16rE8 16rAA 16r9E ] |
|
6810 | 172 |
|
173 |
invalid: |
|
8874 | 174 |
CharacterArray fromUTF8Bytes:#[ 16rC0 16r80 ] |
175 |
CharacterArray fromUTF8Bytes:#[ 16rE0 16r80 16r80 ] |
|
5757 | 176 |
" |
177 |
! |
|
178 |
||
608 | 179 |
new |
180 |
"return a new empty string" |
|
181 |
||
182 |
^ self basicNew:0 |
|
9425 | 183 |
! |
184 |
||
185 |
readSmalltalkStringFrom:aStreamOrString onError:exceptionBlock |
|
186 |
"read & return the next String from the (character-)stream aStream; |
|
187 |
skipping all whitespace first; return the value of exceptionBlock, |
|
188 |
if no string can be read. The sequence of characters as read from the |
|
189 |
stream must be one as stored via storeOn: or storeString." |
|
190 |
||
191 |
|str collected char| |
|
192 |
||
193 |
str := aStreamOrString readStream. |
|
194 |
||
195 |
"skip whiteSpace" |
|
196 |
str skipSeparators. |
|
11320 | 197 |
|
14452
ef3591302afa
changed: #readSmalltalkStringFrom:onError:
Stefan Vogel <sv@exept.de>
parents:
14451
diff
changeset
|
198 |
(str peekOrNil == $') ifTrue:[ |
ef3591302afa
changed: #readSmalltalkStringFrom:onError:
Stefan Vogel <sv@exept.de>
parents:
14451
diff
changeset
|
199 |
str next. |
ef3591302afa
changed: #readSmalltalkStringFrom:onError:
Stefan Vogel <sv@exept.de>
parents:
14451
diff
changeset
|
200 |
collected := self writeStream. |
ef3591302afa
changed: #readSmalltalkStringFrom:onError:
Stefan Vogel <sv@exept.de>
parents:
14451
diff
changeset
|
201 |
[str atEnd] whileFalse:[ |
ef3591302afa
changed: #readSmalltalkStringFrom:onError:
Stefan Vogel <sv@exept.de>
parents:
14451
diff
changeset
|
202 |
char := str next. |
ef3591302afa
changed: #readSmalltalkStringFrom:onError:
Stefan Vogel <sv@exept.de>
parents:
14451
diff
changeset
|
203 |
char == $' ifTrue:[ |
ef3591302afa
changed: #readSmalltalkStringFrom:onError:
Stefan Vogel <sv@exept.de>
parents:
14451
diff
changeset
|
204 |
"/ look for another quote |
ef3591302afa
changed: #readSmalltalkStringFrom:onError:
Stefan Vogel <sv@exept.de>
parents:
14451
diff
changeset
|
205 |
str peekOrNil ~~ $' ifTrue:[ |
ef3591302afa
changed: #readSmalltalkStringFrom:onError:
Stefan Vogel <sv@exept.de>
parents:
14451
diff
changeset
|
206 |
"end of string reached" |
ef3591302afa
changed: #readSmalltalkStringFrom:onError:
Stefan Vogel <sv@exept.de>
parents:
14451
diff
changeset
|
207 |
^ collected contents. |
ef3591302afa
changed: #readSmalltalkStringFrom:onError:
Stefan Vogel <sv@exept.de>
parents:
14451
diff
changeset
|
208 |
]. |
ef3591302afa
changed: #readSmalltalkStringFrom:onError:
Stefan Vogel <sv@exept.de>
parents:
14451
diff
changeset
|
209 |
"eat doubled quote" |
ef3591302afa
changed: #readSmalltalkStringFrom:onError:
Stefan Vogel <sv@exept.de>
parents:
14451
diff
changeset
|
210 |
str next. |
ef3591302afa
changed: #readSmalltalkStringFrom:onError:
Stefan Vogel <sv@exept.de>
parents:
14451
diff
changeset
|
211 |
]. |
ef3591302afa
changed: #readSmalltalkStringFrom:onError:
Stefan Vogel <sv@exept.de>
parents:
14451
diff
changeset
|
212 |
((char ~~ Character return) or:[str peekOrNil ~~ Character lf]) ifTrue:[ |
ef3591302afa
changed: #readSmalltalkStringFrom:onError:
Stefan Vogel <sv@exept.de>
parents:
14451
diff
changeset
|
213 |
"compress CRLF to LF, but keep a single CR" |
ef3591302afa
changed: #readSmalltalkStringFrom:onError:
Stefan Vogel <sv@exept.de>
parents:
14451
diff
changeset
|
214 |
collected nextPut:char. |
ef3591302afa
changed: #readSmalltalkStringFrom:onError:
Stefan Vogel <sv@exept.de>
parents:
14451
diff
changeset
|
215 |
]. |
ef3591302afa
changed: #readSmalltalkStringFrom:onError:
Stefan Vogel <sv@exept.de>
parents:
14451
diff
changeset
|
216 |
]. |
ef3591302afa
changed: #readSmalltalkStringFrom:onError:
Stefan Vogel <sv@exept.de>
parents:
14451
diff
changeset
|
217 |
"if we come here, we reached the end without finding a closing $'" |
9425 | 218 |
]. |
219 |
^ exceptionBlock value |
|
220 |
||
221 |
" |
|
222 |
String readFrom:('''hello world''' readStream) |
|
223 |
String readFrom:('''hello '''' world''' readStream) |
|
224 |
String readFrom:('1 ''hello'' ' readStream) |
|
225 |
String readFrom:('1 ''hello'' ' readStream) onError:['foobar'] |
|
226 |
" |
|
227 |
||
228 |
"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
|
229 |
"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
|
230 |
! |
4bf099f5bb56
CharacterArray class>>#writeStream - answer a CharacterWriteStrem
Stefan Vogel <sv@exept.de>
parents:
11229
diff
changeset
|
231 |
|
13201 | 232 |
writeStreamClass |
233 |
"the class used by writeStream" |
|
234 |
||
235 |
^ CharacterWriteStream |
|
11243
4bf099f5bb56
CharacterArray class>>#writeStream - answer a CharacterWriteStrem
Stefan Vogel <sv@exept.de>
parents:
11229
diff
changeset
|
236 |
|
4bf099f5bb56
CharacterArray class>>#writeStream - answer a CharacterWriteStrem
Stefan Vogel <sv@exept.de>
parents:
11229
diff
changeset
|
237 |
" |
4bf099f5bb56
CharacterArray class>>#writeStream - answer a CharacterWriteStrem
Stefan Vogel <sv@exept.de>
parents:
11229
diff
changeset
|
238 |
String writeStream |
4bf099f5bb56
CharacterArray class>>#writeStream - answer a CharacterWriteStrem
Stefan Vogel <sv@exept.de>
parents:
11229
diff
changeset
|
239 |
" |
13201 | 240 |
|
241 |
"Created: / 09-01-2011 / 10:37:57 / cg" |
|
64 | 242 |
! ! |
243 |
||
9265 | 244 |
!CharacterArray class methodsFor:'Compatibility-VW'! |
245 |
||
246 |
fromIntegerArray: anArray |
|
247 |
"Answer a new instance of the receiver that is created from the argument, anArray." |
|
248 |
||
249 |
| new | |
|
250 |
||
251 |
new := self new: anArray size. |
|
252 |
1 to: anArray size do:[:index | |
|
16202 | 253 |
new at: index put: (anArray at: index) asCharacter |
9265 | 254 |
]. |
255 |
^new |
|
256 |
||
257 |
" |
|
258 |
String fromIntegerArray: #[8 127] |
|
259 |
" |
|
260 |
! ! |
|
261 |
||
2728
7569e45a4d15
raise error signal if anything goes wrong while decoding.
Claus Gittinger <cg@exept.de>
parents:
2721
diff
changeset
|
262 |
!CharacterArray class methodsFor:'Signal constants'! |
7569e45a4d15
raise error signal if anything goes wrong while decoding.
Claus Gittinger <cg@exept.de>
parents:
2721
diff
changeset
|
263 |
|
7569e45a4d15
raise error signal if anything goes wrong while decoding.
Claus Gittinger <cg@exept.de>
parents:
2721
diff
changeset
|
264 |
decodingFailedSignal |
2833 | 265 |
"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
|
266 |
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
|
267 |
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
|
268 |
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
|
269 |
|
7569e45a4d15
raise error signal if anything goes wrong while decoding.
Claus Gittinger <cg@exept.de>
parents:
2721
diff
changeset
|
270 |
^ DecodingFailedSignal |
7569e45a4d15
raise error signal if anything goes wrong while decoding.
Claus Gittinger <cg@exept.de>
parents:
2721
diff
changeset
|
271 |
|
2833 | 272 |
"Created: 28.6.1997 / 20:09:55 / cg" |
273 |
"Modified: 3.8.1997 / 18:16:47 / cg" |
|
274 |
! |
|
275 |
||
276 |
encodingFailedSignal |
|
277 |
"return the (query-) signal, raised when encoding of a string is not possible |
|
278 |
due to invalid characters contained in the source." |
|
279 |
||
280 |
^ EncodingFailedSignal |
|
281 |
||
2728
7569e45a4d15
raise error signal if anything goes wrong while decoding.
Claus Gittinger <cg@exept.de>
parents:
2721
diff
changeset
|
282 |
"Modified: 28.6.1997 / 20:09:35 / cg" |
2833 | 283 |
"Created: 3.8.1997 / 18:16:40 / cg" |
2728
7569e45a4d15
raise error signal if anything goes wrong while decoding.
Claus Gittinger <cg@exept.de>
parents:
2721
diff
changeset
|
284 |
! ! |
7569e45a4d15
raise error signal if anything goes wrong while decoding.
Claus Gittinger <cg@exept.de>
parents:
2721
diff
changeset
|
285 |
|
1849
9f35f5934ec6
added msdos-latin1 en/decoder (code = #msdos850)
Claus Gittinger <cg@exept.de>
parents:
1669
diff
changeset
|
286 |
!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
|
287 |
|
f0b45a4b82d2
first attempt in encoding/decoding for different character representations (mac/msdos etc.)
Claus Gittinger <cg@exept.de>
parents:
986
diff
changeset
|
288 |
lowSpaceCleanup |
f0b45a4b82d2
first attempt in encoding/decoding for different character representations (mac/msdos etc.)
Claus Gittinger <cg@exept.de>
parents:
986
diff
changeset
|
289 |
"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
|
290 |
|
f0b45a4b82d2
first attempt in encoding/decoding for different character representations (mac/msdos etc.)
Claus Gittinger <cg@exept.de>
parents:
986
diff
changeset
|
291 |
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
|
292 |
|
f0b45a4b82d2
first attempt in encoding/decoding for different character representations (mac/msdos etc.)
Claus Gittinger <cg@exept.de>
parents:
986
diff
changeset
|
293 |
" |
f0b45a4b82d2
first attempt in encoding/decoding for different character representations (mac/msdos etc.)
Claus Gittinger <cg@exept.de>
parents:
986
diff
changeset
|
294 |
CharacterArray lowSpaceCleanup |
f0b45a4b82d2
first attempt in encoding/decoding for different character representations (mac/msdos etc.)
Claus Gittinger <cg@exept.de>
parents:
986
diff
changeset
|
295 |
" |
f0b45a4b82d2
first attempt in encoding/decoding for different character representations (mac/msdos etc.)
Claus Gittinger <cg@exept.de>
parents:
986
diff
changeset
|
296 |
|
f0b45a4b82d2
first attempt in encoding/decoding for different character representations (mac/msdos etc.)
Claus Gittinger <cg@exept.de>
parents:
986
diff
changeset
|
297 |
"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
|
298 |
"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
|
299 |
! ! |
f0b45a4b82d2
first attempt in encoding/decoding for different character representations (mac/msdos etc.)
Claus Gittinger <cg@exept.de>
parents:
986
diff
changeset
|
300 |
|
7421 | 301 |
!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
|
302 |
|
7876 | 303 |
decodeFromUTF8:aStringOrByteCollection |
304 |
"return a string which represents the characters as decoded |
|
305 |
from the utf8 encoded bytes, aByteCollection. |
|
306 |
Returns either a normal String, or a TwoByteString instance. |
|
307 |
Only useful, when reading twoByteStrings from external sources. |
|
308 |
This only handles up-to 16bit characters." |
|
309 |
||
8124
ca774158b317
code cleanup (removed old obsolete encoding stuff)
Claus Gittinger <cg@exept.de>
parents:
8102
diff
changeset
|
310 |
^ CharacterEncoderImplementations::ISO10646_to_UTF8 decodeString:aStringOrByteCollection |
7876 | 311 |
|
312 |
" |
|
8874 | 313 |
CharacterArray fromUTF8Bytes:#[ 16r41 16r42 ] |
314 |
CharacterArray fromUTF8Bytes:#[ 16rC1 16r02 ] |
|
315 |
CharacterArray fromUTF8Bytes:#[ 16rE0 16r81 16r02 ] |
|
7876 | 316 |
CharacterArray fromUTF8Bytes:#[ 16rEF 16rBF 16rBF ] |
317 |
||
318 |
rfc2279 examples: |
|
8874 | 319 |
CharacterArray fromUTF8Bytes:#[ 16r41 16rE2 16r89 16rA2 16rCE 16r91 16r2E ] |
320 |
CharacterArray fromUTF8Bytes:#[ 16rED 16r95 16r9C 16rEA 16rB5 16rAD 16rEC 16r96 16rB4 ] |
|
321 |
CharacterArray fromUTF8Bytes:#[ 16rE6 16r97 16rA5 16rE6 16r9C 16rAC 16rE8 16rAA 16r9E ] |
|
7876 | 322 |
|
323 |
invalid: |
|
8874 | 324 |
CharacterArray fromUTF8Bytes:#[ 16rC0 16r80 ] |
325 |
CharacterArray fromUTF8Bytes:#[ 16rE0 16r80 16r80 ] |
|
7876 | 326 |
" |
443 | 327 |
! ! |
328 |
||
1849
9f35f5934ec6
added msdos-latin1 en/decoder (code = #msdos850)
Claus Gittinger <cg@exept.de>
parents:
1669
diff
changeset
|
329 |
!CharacterArray class methodsFor:'pattern matching'! |
64 | 330 |
|
5342
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
331 |
matchEscapeCharacter |
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
332 |
"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
|
333 |
(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
|
334 |
|
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
335 |
^ $\ |
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
336 |
! |
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
337 |
|
15734 | 338 |
matchScan:matchScanArray from:matchStart to:matchStop with:aString from:start to:stop caseSensitive:caseSensitive |
327 | 339 |
"helper for match; return true if the characters from start to stop in |
340 |
aString are matching the scan in matchScan from matchStart to matchStop. |
|
328 | 341 |
The matchScan is as created by asMatchScanArray. |
327 | 342 |
|
8874 | 343 |
This algorithm is not at all the most efficient; |
344 |
for heavy duty pattern matching, an interface (primitive) to the regex |
|
328 | 345 |
pattern matching package should be added." |
327 | 346 |
|
8874 | 347 |
|matchEntry |
327 | 348 |
mStart "{ Class: SmallInteger }" |
349 |
mStop "{ Class: SmallInteger }" |
|
350 |
sStart "{ Class: SmallInteger }" |
|
351 |
sStop "{ Class: SmallInteger }" |
|
352 |
mSize "{ Class: SmallInteger }" |
|
353 |
sSize "{ Class: SmallInteger }" |
|
354 |
index "{ Class: SmallInteger }" |
|
2561 | 355 |
quickCheck matchLast nextMatchEntry |
986 | 356 |
checkChar included| |
327 | 357 |
|
358 |
mStart := matchStart. |
|
359 |
mStop := matchStop. |
|
360 |
sStart := start. |
|
361 |
sStop := stop. |
|
362 |
||
16245 | 363 |
[ |
8874 | 364 |
"/ Transcript showCR:('match: ''' , (aString copyFrom:sStart to:sStop) , |
3537 | 365 |
"/ ''' against:' , (matchScanArray copyFrom:mStart to:mStop) printString). |
366 |
||
15734 | 367 |
mSize := mStop - mStart + 1. |
368 |
sSize := sStop - sStart + 1. |
|
369 |
||
370 |
"empty strings match" |
|
371 |
(mSize == 0) ifTrue:[^ (sSize == 0)]. |
|
372 |
||
373 |
matchEntry := matchScanArray at:mStart. |
|
374 |
||
375 |
"/ the most common case first: |
|
376 |
(sSize ~~ 0 |
|
377 |
and:[(checkChar := (aString at:sStart)) = matchEntry]) ifTrue:[ |
|
378 |
"advance by one and continue" |
|
379 |
mStart := mStart + 1. |
|
380 |
sStart := sStart + 1 |
|
381 |
] ifFalse:[ |
|
382 |
(matchEntry == #any) ifTrue:[ |
|
383 |
"restString empty -> no match" |
|
384 |
(sSize == 0) ifTrue:[^ false]. |
|
385 |
"# matches single character" |
|
386 |
((sSize == 1) and:[mSize == 1]) ifTrue:[^ true]. |
|
387 |
"advance by one and continue" |
|
388 |
mStart := mStart + 1. |
|
389 |
sStart := sStart + 1 |
|
390 |
] ifFalse:[ |
|
391 |
(matchEntry == #anyString) ifTrue:[ |
|
392 |
"* alone matches anything" |
|
393 |
(mSize == 1) ifTrue:[^ true]. |
|
394 |
"restString empty & matchString not empty -> no match" |
|
395 |
(sSize == 0) ifTrue:[^ false]. |
|
396 |
||
397 |
" |
|
398 |
try to avoid some of the recursion by checking last |
|
399 |
character and continue with shortened strings if possible |
|
400 |
" |
|
401 |
quickCheck := false. |
|
402 |
(mStop >= mStart) ifTrue:[ |
|
403 |
matchLast := matchScanArray at:mStop. |
|
404 |
(matchLast ~~ #anyString) ifTrue:[ |
|
405 |
(matchLast == #any) ifTrue:[ |
|
406 |
quickCheck := true |
|
407 |
] ifFalse:[ |
|
408 |
matchLast == (aString at:sStop) ifTrue:[ |
|
409 |
quickCheck := true |
|
410 |
] ifFalse:[ |
|
411 |
matchLast isString ifTrue:[ |
|
412 |
quickCheck := matchLast includes:(aString at:sStop) |
|
413 |
] |
|
414 |
] |
|
415 |
] |
|
416 |
] |
|
417 |
]. |
|
418 |
quickCheck ifTrue:[ |
|
419 |
" |
|
420 |
quickCheck ok, advance from the right |
|
421 |
" |
|
422 |
mStop := mStop - 1. |
|
423 |
sStop := sStop - 1 |
|
424 |
] ifFalse:[ |
|
425 |
"/ no quick check; |
|
426 |
"/ look for the next character(s) |
|
427 |
"/ and try matching there |
|
428 |
"/ (to avoid recursion) |
|
429 |
||
430 |
mStart < mStop ifTrue:[ |
|
431 |
nextMatchEntry := matchScanArray at:mStart+1. |
|
432 |
nextMatchEntry isCharacter ifTrue:[ |
|
433 |
sStart <= sStop ifTrue:[ |
|
16245 | 434 |
[ |
15734 | 435 |
caseSensitive ifTrue:[ |
436 |
index := aString indexOf:nextMatchEntry startingAt:sStart |
|
437 |
] ifFalse:[ |
|
438 |
index := aString findFirst:[:c | c asLowercase = nextMatchEntry asLowercase] |
|
439 |
startingAt:sStart. |
|
440 |
]. |
|
441 |
(index == 0 or:[index > sStop]) ifTrue:[ |
|
442 |
^ false |
|
443 |
]. |
|
444 |
(self matchScan:matchScanArray |
|
445 |
from:(mStart + 1) |
|
446 |
to:mStop |
|
447 |
with:aString |
|
448 |
from:index |
|
449 |
to:sStop |
|
450 |
caseSensitive:caseSensitive |
|
451 |
) ifTrue:[ |
|
452 |
^ true |
|
453 |
]. |
|
454 |
sStart := index + 1. |
|
16245 | 455 |
] loop. |
15734 | 456 |
] |
457 |
] |
|
458 |
]. |
|
459 |
||
460 |
" |
|
461 |
no quick check possible; |
|
462 |
loop over all possible substrings |
|
463 |
" |
|
464 |
index := sStart. |
|
465 |
[index <= sStop] whileTrue:[ |
|
466 |
(self matchScan:matchScanArray |
|
467 |
from:(mStart + 1) |
|
468 |
to:mStop |
|
469 |
with:aString |
|
470 |
from:index |
|
471 |
to:sStop |
|
472 |
caseSensitive:caseSensitive |
|
473 |
) ifTrue:[ |
|
474 |
^ true |
|
475 |
]. |
|
476 |
index := index + 1 |
|
477 |
]. |
|
478 |
^ false |
|
479 |
]. |
|
480 |
] ifFalse:[ |
|
481 |
(matchEntry isString) ifTrue:[ |
|
482 |
"testString empty -> no match" |
|
483 |
(sSize == 0) ifTrue:[^ false]. |
|
484 |
||
485 |
included := false. |
|
486 |
"/ checkChar := aString at:sStart. |
|
487 |
included := matchEntry includes:checkChar. |
|
488 |
included ifFalse:[ |
|
489 |
caseSensitive ifFalse:[ |
|
490 |
checkChar isUppercase ifTrue:[ |
|
491 |
included := matchEntry includes:checkChar asLowercase. |
|
492 |
] ifFalse:[ |
|
493 |
included := matchEntry includes:checkChar asUppercase. |
|
494 |
] |
|
495 |
]. |
|
496 |
]. |
|
497 |
mStart := mStart + 1. |
|
498 |
mSize := mSize - 1. |
|
499 |
included ifFalse:[^ false]. |
|
500 |
||
501 |
((sSize == 1) and:[mSize == 0]) ifTrue:[^ true]. |
|
502 |
] ifFalse:[ |
|
503 |
"/ must be single character |
|
504 |
||
505 |
"testString empty ?" |
|
506 |
(sSize == 0) ifTrue:[^ false]. |
|
507 |
||
508 |
"first characters equal ?" |
|
509 |
"/ checkChar := aString at:sStart. |
|
510 |
caseSensitive ifTrue:[^ false]. |
|
511 |
(checkChar asUppercase ~= matchEntry asUppercase) ifTrue:[^ false]. |
|
512 |
||
513 |
"advance and continue" |
|
514 |
mStart := mStart + 1. |
|
515 |
]. |
|
516 |
"cut off 1st char and continue" |
|
517 |
sStart := sStart + 1 |
|
518 |
] |
|
519 |
] |
|
520 |
] |
|
16245 | 521 |
] loop. |
2519
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
522 |
|
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
523 |
" |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
524 |
|scanArray s| |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
525 |
|
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
526 |
scanArray := self matchScanArrayFrom:'*hello'. |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
527 |
s := 'foo bar hello world'. |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
528 |
CharacterArray |
15734 | 529 |
matchScan:scanArray |
530 |
from:1 |
|
531 |
to:scanArray size |
|
532 |
with:s |
|
533 |
from:1 |
|
534 |
to:s size |
|
535 |
caseSensitive:true |
|
2519
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
536 |
" |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
537 |
" |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
538 |
|scanArray s| |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
539 |
|
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
540 |
scanArray := self matchScanArrayFrom:'*hello*'. |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
541 |
s := 'foo bar hello world'. |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
542 |
CharacterArray |
15734 | 543 |
matchScan:scanArray |
544 |
from:1 |
|
545 |
to:scanArray size |
|
546 |
with:s |
|
547 |
from:1 |
|
548 |
to:s size |
|
549 |
caseSensitive:true |
|
550 |
" |
|
551 |
||
552 |
"Modified: / 24-07-2011 / 07:17:03 / cg" |
|
553 |
"Modified (comment): / 24-07-2011 / 08:55:14 / cg" |
|
554 |
! |
|
555 |
||
556 |
matchScan:matchScanArray from:matchStart to:matchStop with:aString from:start to:stop ignoreCase:ignoreCase |
|
557 |
<resource: #obsolete> |
|
558 |
"helper for match; return true if the characters from start to stop in |
|
559 |
aString are matching the scan in matchScan from matchStart to matchStop. |
|
560 |
The matchScan is as created by asMatchScanArray. |
|
561 |
||
562 |
This algorithm is not at all the most efficient; |
|
563 |
for heavy duty pattern matching, an interface (primitive) to the regex |
|
564 |
pattern matching package should be added." |
|
565 |
||
566 |
^ self |
|
567 |
matchScan:matchScanArray from:matchStart to:matchStop with:aString from:start to:stop caseSensitive:ignoreCase not |
|
568 |
||
569 |
" |
|
570 |
|scanArray s| |
|
571 |
||
572 |
scanArray := self matchScanArrayFrom:'*hello'. |
|
573 |
s := 'foo bar hello world'. |
|
574 |
CharacterArray |
|
575 |
matchScan:scanArray |
|
576 |
from:1 |
|
577 |
to:scanArray size |
|
578 |
with:s |
|
579 |
from:1 |
|
580 |
to:s size |
|
581 |
ignoreCase:false |
|
582 |
" |
|
583 |
" |
|
584 |
|scanArray s| |
|
585 |
||
586 |
scanArray := self matchScanArrayFrom:'*hello*'. |
|
587 |
s := 'foo bar hello world'. |
|
588 |
CharacterArray |
|
589 |
matchScan:scanArray |
|
590 |
from:1 |
|
591 |
to:scanArray size |
|
592 |
with:s |
|
593 |
from:1 |
|
594 |
to:s size |
|
595 |
ignoreCase:false |
|
13520 | 596 |
" |
597 |
||
598 |
"Modified: / 24-07-2011 / 07:17:03 / cg" |
|
599 |
"Modified (comment): / 24-07-2011 / 08:55:14 / cg" |
|
608 | 600 |
! |
601 |
||
602 |
matchScanArrayFrom:aString |
|
603 |
"scan a pattern string and decompose it into a scanArray. |
|
604 |
This is processed faster (especially with character ranges), and |
|
605 |
can also be reused later. (if the same pattern is to be searched again)" |
|
606 |
||
9282
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
607 |
^ self matchScanArrayFrom:aString escapeCharacter:(self matchEscapeCharacter) |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
608 |
|
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
609 |
" |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
610 |
String matchScanArrayFrom:'*ute*' |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
611 |
String matchScanArrayFrom:'**ute**' |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
612 |
String matchScanArrayFrom:'*uter' |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
613 |
String matchScanArrayFrom:'\*uter' |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
614 |
String matchScanArrayFrom:'[cC]#mpute[rR]' |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
615 |
String matchScanArrayFrom:'[abcd]*' |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
616 |
String matchScanArrayFrom:'[a-k]*' |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
617 |
String matchScanArrayFrom:'*some*compl*ern*' |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
618 |
String matchScanArrayFrom:'[a-' |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
619 |
String matchScanArrayFrom:'[a-zA-Z]' |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
620 |
String matchScanArrayFrom:'[a-z01234A-Z]' |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
621 |
" |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
622 |
|
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
623 |
"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
|
624 |
! |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
625 |
|
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
626 |
matchScanArrayFrom:aString escapeCharacter:escape |
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
627 |
"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
|
628 |
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
|
629 |
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
|
630 |
|
8874 | 631 |
|coll |
608 | 632 |
idx "{ Class: SmallInteger }" |
9282
f6d9d4129919
allow for different escape cahracter in matches
Claus Gittinger <cg@exept.de>
parents:
9275
diff
changeset
|
633 |
end c1 c2 matchSet previous| |
5342
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
634 |
|
5487 | 635 |
previous := nil. |
608 | 636 |
|
637 |
coll := OrderedCollection new. |
|
638 |
idx := 1. end := aString size. |
|
639 |
[idx <= end] whileTrue:[ |
|
16202 | 640 |
|char this| |
641 |
||
642 |
char := aString at:idx. |
|
643 |
char == $* ifTrue:[ |
|
644 |
previous ~~ #anyString ifTrue:[ |
|
645 |
this := #anyString |
|
646 |
] |
|
647 |
] ifFalse:[ |
|
648 |
char == $# ifTrue:[ |
|
649 |
previous ~~ #anyString ifTrue:[ |
|
650 |
this := #any |
|
651 |
] |
|
652 |
] ifFalse:[ |
|
653 |
char == $[ ifTrue:[ |
|
654 |
matchSet := IdentitySet new. |
|
655 |
idx := idx + 1. |
|
656 |
idx > end ifTrue:[^ nil]. |
|
657 |
char := aString at:idx. |
|
658 |
c1 := nil. |
|
659 |
[char ~~ $]] whileTrue:[ |
|
660 |
((char == $-) and:[c1 notNil]) ifTrue:[ |
|
661 |
idx := idx + 1. |
|
662 |
idx > end ifTrue:[^ nil]. |
|
663 |
c2 := aString at:idx. |
|
664 |
c1 to:c2 do:[:c | matchSet add:c]. |
|
665 |
c1 := nil. |
|
666 |
idx := idx + 1. |
|
667 |
] ifFalse:[ |
|
668 |
(char ~~ $]) ifTrue:[ |
|
669 |
matchSet add:char. |
|
670 |
c1 := char. |
|
671 |
idx := idx + 1 |
|
672 |
] |
|
673 |
]. |
|
674 |
idx > end ifTrue:[^ nil]. |
|
675 |
char := aString at:idx |
|
676 |
]. |
|
677 |
this := matchSet asString |
|
678 |
] ifFalse:[ |
|
679 |
char == escape ifTrue:[ |
|
680 |
idx := idx + 1. |
|
681 |
idx > end ifTrue:[ |
|
682 |
"/ mhmh - what should we do here ? |
|
683 |
this := char |
|
684 |
] ifFalse:[ |
|
685 |
this := aString at:idx. |
|
686 |
] |
|
687 |
] ifFalse:[ |
|
688 |
this := char |
|
689 |
] |
|
690 |
] |
|
691 |
] |
|
692 |
]. |
|
693 |
this notNil ifTrue:[coll add:this. previous := this]. |
|
694 |
idx := idx + 1 |
|
608 | 695 |
]. |
696 |
||
697 |
^ coll asArray |
|
698 |
||
699 |
" |
|
8874 | 700 |
String matchScanArrayFrom:'*ute*' |
701 |
String matchScanArrayFrom:'**ute**' |
|
702 |
String matchScanArrayFrom:'*uter' |
|
703 |
String matchScanArrayFrom:'\*uter' |
|
704 |
String matchScanArrayFrom:'[cC]#mpute[rR]' |
|
705 |
String matchScanArrayFrom:'[abcd]*' |
|
706 |
String matchScanArrayFrom:'[a-k]*' |
|
707 |
String matchScanArrayFrom:'*some*compl*ern*' |
|
708 |
String matchScanArrayFrom:'[a-' |
|
709 |
String matchScanArrayFrom:'[a-zA-Z]' |
|
710 |
String matchScanArrayFrom:'[a-z01234A-Z]' |
|
608 | 711 |
" |
2519
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
712 |
|
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
713 |
"Modified: 2.4.1997 / 16:20:29 / cg" |
9192 | 714 |
! ! |
715 |
||
716 |
!CharacterArray class methodsFor:'queries'! |
|
717 |
||
718 |
isAbstract |
|
11229 | 719 |
"Return if this class is an abstract class. |
720 |
True is returned for CharacterArray here; false for subclasses. |
|
721 |
Abstract subclasses must redefine again." |
|
722 |
||
9192 | 723 |
^ self == CharacterArray |
724 |
! ! |
|
725 |
||
726 |
!CharacterArray methodsFor:'Compatibility-ANSI'! |
|
727 |
||
728 |
addLineDelimiters |
|
729 |
"Ansi compatibility - same as withCRs" |
|
730 |
||
731 |
^ self withCRs |
|
732 |
||
733 |
"Modified: / 13.11.2001 / 19:16:25 / cg" |
|
734 |
! ! |
|
735 |
||
736 |
!CharacterArray methodsFor:'Compatibility-Dolphin'! |
|
737 |
||
738 |
argumentCount |
|
739 |
"same as #numArgs - return the number of arguments a message with myself |
|
740 |
as selector would take." |
|
741 |
||
742 |
^ self numArgs |
|
743 |
! |
|
744 |
||
745 |
copyExpanding:expandTable |
|
746 |
"return a copy of myself, with translations from the expandTable sliced in. |
|
747 |
The argument is supposed to map from characters to either characters or strings." |
|
748 |
||
749 |
|ds| |
|
750 |
||
751 |
ds := WriteStream on:(self species new). |
|
752 |
self do:[:eachChar | |
|
16202 | 753 |
|repl| |
754 |
||
755 |
repl := expandTable at:eachChar ifAbsent:nil. |
|
756 |
repl isNil ifTrue:[ |
|
757 |
ds nextPut:eachChar |
|
758 |
] ifFalse:[ |
|
759 |
repl size == 0 ifTrue:[ |
|
760 |
ds nextPut:repl |
|
761 |
] ifFalse:[ |
|
762 |
ds nextPutAll:repl |
|
763 |
] |
|
764 |
]. |
|
9192 | 765 |
]. |
6870 | 766 |
^ ds contents. |
767 |
! |
|
768 |
||
769 |
formatWith:aString |
|
11955 | 770 |
"Compatibility method - do not use in new code. |
771 |
same as #bindWith: for dolphin compatibility" |
|
6870 | 772 |
|
773 |
^ self bindWith:aString |
|
774 |
||
775 |
" |
|
776 |
'hello%1world' formatWith:'123' |
|
777 |
" |
|
778 |
! |
|
779 |
||
780 |
formatWith:arg1 with:arg2 |
|
11955 | 781 |
"Compatibility method - do not use in new code. |
782 |
same as #bindWith: for dolphin compatibility" |
|
6870 | 783 |
|
784 |
^ self bindWith:arg1 with:arg2 |
|
785 |
||
786 |
" |
|
787 |
'hello%1 %2world' formatWith:'123' with:234 |
|
788 |
" |
|
789 |
! |
|
790 |
||
791 |
formatWith:arg1 with:arg2 with:arg3 |
|
11955 | 792 |
"Compatibility method - do not use in new code. |
793 |
same as #bindWith: for dolphin compatibility" |
|
6870 | 794 |
|
795 |
^ self bindWith:arg1 with:arg2 with:arg3 |
|
796 |
||
797 |
" |
|
798 |
'hello%1 %2 %3world' formatWith:'123' with:234 with:345 |
|
799 |
" |
|
6391 | 800 |
! ! |
801 |
||
14681 | 802 |
!CharacterArray methodsFor:'Compatibility-GNU'! |
803 |
||
804 |
% anArrayOfOperands |
|
805 |
"return a copy of the receiver, where a '%i' escape |
|
806 |
is replaced by the coresponding string from the argument array. |
|
807 |
'i' may be between 1 and 9 (i.e. a maximum of 9 placeholders is allowed). |
|
808 |
Added for GNU-ST compatibility." |
|
809 |
||
810 |
^ self expandPlaceholdersWith:anArrayOfOperands |
|
811 |
||
812 |
" |
|
813 |
'do you prefer %1 or rather %2 (not talking about %3) ?' |
|
814 |
% #('smalltalk' 'c++' 'c') |
|
815 |
||
816 |
'do you %(what) ?' |
|
817 |
% (Dictionary new at:#'what' put:'understand'; yourself) |
|
818 |
" |
|
819 |
! ! |
|
820 |
||
7261 | 821 |
!CharacterArray methodsFor:'Compatibility-ST/V'! |
608 | 822 |
|
823 |
byteAt:index put:aByte |
|
824 |
"store a byte at given index. |
|
825 |
This is an ST/V compatibility method." |
|
826 |
||
1333
d3fdca956f0d
allow storing of 0-bytes (be careful with printing then ...)
Claus Gittinger <cg@exept.de>
parents:
1312
diff
changeset
|
827 |
"/ (aByte == 0) ifTrue:[ |
d3fdca956f0d
allow storing of 0-bytes (be careful with printing then ...)
Claus Gittinger <cg@exept.de>
parents:
1312
diff
changeset
|
828 |
"/ "store a space instead" |
d3fdca956f0d
allow storing of 0-bytes (be careful with printing then ...)
Claus Gittinger <cg@exept.de>
parents:
1312
diff
changeset
|
829 |
"/ ^ super basicAt:index put:(Character space) |
d3fdca956f0d
allow storing of 0-bytes (be careful with printing then ...)
Claus Gittinger <cg@exept.de>
parents:
1312
diff
changeset
|
830 |
"/ ]. |
11754
5132248cfd6e
common definition of #byteAt:put between CharacterArray and Symbol
Stefan Vogel <sv@exept.de>
parents:
11632
diff
changeset
|
831 |
^ self basicAt:index put:(Character value:aByte) |
9192 | 832 |
|
833 |
" |
|
834 |
'hello' copy at:1 put:$H asciiValue; yourself |
|
835 |
'hello' copy byteAt:1 put:72; yourself |
|
836 |
'hello' copy byteAt:1 put:0; yourself |
|
837 |
" |
|
838 |
||
839 |
"Modified: 6.5.1996 / 10:35:26 / cg" |
|
840 |
! |
|
608 | 841 |
|
842 |
replChar:oldChar with:newChar |
|
843 |
"return a copy of the receiver, with all oldChars replaced |
|
844 |
by newChar. |
|
845 |
This is an ST/V compatibility method." |
|
846 |
||
3656 | 847 |
^ self copyReplaceAll:oldChar with:newChar |
608 | 848 |
|
849 |
" |
|
8874 | 850 |
'12345678901234567890' replChar:$0 with:$* |
608 | 851 |
" |
3656 | 852 |
|
853 |
"Modified: / 18.7.1998 / 22:52:57 / cg" |
|
608 | 854 |
! |
855 |
||
856 |
replChar:oldChar withString:newString |
|
857 |
"return a copy of the receiver, with all oldChars replaced |
|
858 |
by newString (i.e. slice in the newString in place of the oldChar). |
|
859 |
This is an ST/V compatibility method." |
|
860 |
||
861 |
|tmpStream| |
|
862 |
||
15523 | 863 |
tmpStream := self species writeStream. |
608 | 864 |
self do:[:element | |
15523 | 865 |
element = oldChar ifTrue:[ |
866 |
tmpStream nextPutAll:newString |
|
867 |
] ifFalse:[ |
|
868 |
tmpStream nextPut:element |
|
869 |
]. |
|
608 | 870 |
]. |
871 |
^ tmpStream contents |
|
872 |
||
873 |
" |
|
8874 | 874 |
'12345678901234567890' replChar:$0 withString:'foo' |
875 |
'a string with spaces' replChar:$ withString:' foo ' |
|
608 | 876 |
" |
9192 | 877 |
! |
878 |
||
879 |
replString:subString withString:newString |
|
4244
5deefa3144fb
added #replString:withString:
Claus Gittinger <cg@exept.de>
parents:
4156
diff
changeset
|
880 |
"return a copy of the receiver, with all sequences of subString replaced |
5deefa3144fb
added #replString:withString:
Claus Gittinger <cg@exept.de>
parents:
4156
diff
changeset
|
881 |
by newString (i.e. slice in the newString in place of the oldString)." |
5deefa3144fb
added #replString:withString:
Claus Gittinger <cg@exept.de>
parents:
4156
diff
changeset
|
882 |
|
8347 | 883 |
^ self copyReplaceString:subString withString:newString |
4244
5deefa3144fb
added #replString:withString:
Claus Gittinger <cg@exept.de>
parents:
4156
diff
changeset
|
884 |
|
5deefa3144fb
added #replString:withString:
Claus Gittinger <cg@exept.de>
parents:
4156
diff
changeset
|
885 |
" |
8874 | 886 |
'12345678901234567890' replString:'123' withString:'OneTwoThree' |
887 |
'12345678901234567890' replString:'123' withString:'*' |
|
888 |
'12345678901234567890' replString:'234' withString:'foo' |
|
4244
5deefa3144fb
added #replString:withString:
Claus Gittinger <cg@exept.de>
parents:
4156
diff
changeset
|
889 |
|
5deefa3144fb
added #replString:withString:
Claus Gittinger <cg@exept.de>
parents:
4156
diff
changeset
|
890 |
('a string with spaces' replChar:$ withString:' foo ') |
16202 | 891 |
replString:'foo' withString:'bar' |
4244
5deefa3144fb
added #replString:withString:
Claus Gittinger <cg@exept.de>
parents:
4156
diff
changeset
|
892 |
" |
5deefa3144fb
added #replString:withString:
Claus Gittinger <cg@exept.de>
parents:
4156
diff
changeset
|
893 |
|
8347 | 894 |
"Modified: / 12-05-2004 / 12:00:27 / cg" |
9192 | 895 |
! |
896 |
||
9275 | 897 |
subString:start to:end |
898 |
^ self copyFrom:start to:end |
|
899 |
||
900 |
" |
|
901 |
'12345678901234567890' subString:3 to:8 |
|
902 |
" |
|
903 |
! |
|
904 |
||
9192 | 905 |
trimBlanks |
14992 | 906 |
"return a copy of the receiver without leading and trailing spaces. |
9192 | 907 |
This is an ST/V compatibility method." |
908 |
||
909 |
^ self withoutSpaces |
|
910 |
||
911 |
" |
|
912 |
' spaces at beginning' trimBlanks |
|
913 |
'spaces at end ' trimBlanks |
|
914 |
' spaces at beginning and end ' trimBlanks |
|
915 |
'no spaces' trimBlanks |
|
916 |
" |
|
917 |
! ! |
|
918 |
||
919 |
!CharacterArray methodsFor:'Compatibility-Squeak'! |
|
4409
86e049b21f8d
added #includesSubstring:caseSensitive:
Claus Gittinger <cg@exept.de>
parents:
4389
diff
changeset
|
920 |
|
6148 | 921 |
asDate |
922 |
"Many allowed forms, see Date.readFrom:" |
|
923 |
||
924 |
^ Date readFrom: (ReadStream on: self) |
|
925 |
||
926 |
" |
|
8874 | 927 |
'30 Apr 1999' asDate dayName capitalized |
6148 | 928 |
" |
929 |
! |
|
930 |
||
16051 | 931 |
asOneByteString |
932 |
"return the receiver converted to a 'normal' string. |
|
933 |
Same as asSingleByteString - for compatibility." |
|
934 |
||
935 |
^ self asSingleByteString |
|
936 |
! |
|
937 |
||
8971 | 938 |
asUrl |
939 |
^ self asURL |
|
940 |
! |
|
941 |
||
10840 | 942 |
asWideString |
14405
1d0d99a95bba
comment/format in:7 methods
Claus Gittinger <cg@exept.de>
parents:
14168
diff
changeset
|
943 |
"return a two-byte string containing the same characters as the receiver" |
1d0d99a95bba
comment/format in:7 methods
Claus Gittinger <cg@exept.de>
parents:
14168
diff
changeset
|
944 |
|
10840 | 945 |
^ self asTwoByteString |
946 |
||
947 |
" |
|
948 |
'abc' asWideString |
|
949 |
" |
|
950 |
! |
|
951 |
||
4989 | 952 |
capitalized |
14405
1d0d99a95bba
comment/format in:7 methods
Claus Gittinger <cg@exept.de>
parents:
14168
diff
changeset
|
953 |
"same as asUppercaseFirst for Squeak compatibility" |
1d0d99a95bba
comment/format in:7 methods
Claus Gittinger <cg@exept.de>
parents:
14168
diff
changeset
|
954 |
|
4989 | 955 |
^ self asUppercaseFirst |
956 |
||
957 |
" |
|
958 |
'hello' capitalized |
|
959 |
" |
|
960 |
! |
|
961 |
||
10841 | 962 |
caseInsensitiveLessOrEqual:aString |
963 |
^ (self compareCaselessWith:aString) <= 0 |
|
964 |
! |
|
965 |
||
966 |
caseSensitiveLessOrEqual:aString |
|
967 |
^ self <= aString |
|
968 |
! |
|
969 |
||
970 |
charactersExactlyMatching:aString |
|
14405
1d0d99a95bba
comment/format in:7 methods
Claus Gittinger <cg@exept.de>
parents:
14168
diff
changeset
|
971 |
"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
|
972 |
|
10841 | 973 |
|idx nMax| |
974 |
||
975 |
nMax :=(self size) min:(aString size). |
|
976 |
idx := 1. |
|
977 |
[idx <= nMax] whileTrue:[ |
|
14405
1d0d99a95bba
comment/format in:7 methods
Claus Gittinger <cg@exept.de>
parents:
14168
diff
changeset
|
978 |
(self at:idx) = (aString at:idx) ifFalse:[ |
1d0d99a95bba
comment/format in:7 methods
Claus Gittinger <cg@exept.de>
parents:
14168
diff
changeset
|
979 |
^ idx - 1 |
1d0d99a95bba
comment/format in:7 methods
Claus Gittinger <cg@exept.de>
parents:
14168
diff
changeset
|
980 |
]. |
1d0d99a95bba
comment/format in:7 methods
Claus Gittinger <cg@exept.de>
parents:
14168
diff
changeset
|
981 |
idx := idx + 1. |
10841 | 982 |
]. |
983 |
^ nMax |
|
984 |
||
985 |
" |
|
12177 | 986 |
'abc' charactersExactlyMatching:'abc' |
987 |
'abc' charactersExactlyMatching:'abcd' |
|
988 |
'abcd' charactersExactlyMatching:'abc' |
|
989 |
'abc' charactersExactlyMatching:'abd' |
|
990 |
'abc' charactersExactlyMatching:'xxx' |
|
10841 | 991 |
" |
992 |
! |
|
993 |
||
9192 | 994 |
endsWithDigit |
995 |
"Answer whether the receiver's final character represents a digit. 3/11/96 sw" |
|
996 |
||
997 |
^ self size > 0 and: [self last isDigit] |
|
998 |
! |
|
999 |
||
10703
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
1000 |
findDelimiters:delimiters startingAt:start |
9192 | 1001 |
"Answer the index of the character within the receiver, starting at start, that matches one of the delimiters. If the receiver does not contain any of the delimiters, answer size + 1." |
1002 |
||
1003 |
|idx| |
|
1004 |
||
1005 |
idx := self indexOfAny:delimiters startingAt:start. |
|
1006 |
idx == 0 ifTrue:[^ self size + 1]. |
|
1007 |
^ idx. |
|
1008 |
"/start to: self size do: [:i | |
|
1009 |
"/ |char| |
|
1010 |
"/ |
|
1011 |
"/ char := self at: i. |
|
1012 |
"/ delimiters do: [:delim | delim = char ifTrue: [^ i]] |
|
8971 | 1013 |
"/ ]. |
1014 |
"/ ^ self size + 1 |
|
1015 |
! |
|
1016 |
||
13624 | 1017 |
findTokens:delimiterOrDelimiters |
7270 | 1018 |
"cg: I am not sure, if this is really the squeak semantics (w.r.t. empty fields)" |
1019 |
||
13624 | 1020 |
delimiterOrDelimiters size == 0 ifTrue:[ |
16202 | 1021 |
^ self asCollectionOfSubstringsSeparatedBy:delimiterOrDelimiters |
13624 | 1022 |
] ifFalse:[ |
16202 | 1023 |
^ self asCollectionOfSubstringsSeparatedByAny:delimiterOrDelimiters |
13624 | 1024 |
]. |
7270 | 1025 |
|
1026 |
" |
|
8874 | 1027 |
'a|b#c||e' findTokens:#($# $|) |
13624 | 1028 |
'a|b#c||e' findTokens:$| |
1029 |
" |
|
1030 |
||
1031 |
"Modified: / 04-09-2011 / 20:31:21 / cg" |
|
1032 |
"Modified (comment): / 05-09-2011 / 04:25:28 / cg" |
|
4981 | 1033 |
! |
1034 |
||
4974 | 1035 |
includesSubString:aString |
1036 |
"return true, if a substring is contained in the receiver. |
|
1037 |
The compare is case sensitive." |
|
1038 |
||
1039 |
^ self includesString:aString |
|
1040 |
||
1041 |
" |
|
8874 | 1042 |
'hello world' includesSubString:'Hel' |
9192 | 1043 |
'hello world' includesSubString:'hel' |
1044 |
'hello world' includesSubString:'llo' |
|
1045 |
" |
|
1046 |
||
1047 |
||
1048 |
||
1049 |
! |
|
1050 |
||
11178 | 1051 |
includesSubString:aString caseSensitive:caseSensitive |
11179 | 1052 |
^ self includesSubstring:aString caseSensitive:caseSensitive |
11178 | 1053 |
! |
1054 |
||
16875
32e61bb3a742
class: CharacterArray>>#includesSubstring: (for Squeak compatibility)
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
16863
diff
changeset
|
1055 |
includesSubstring: aString |
32e61bb3a742
class: CharacterArray>>#includesSubstring: (for Squeak compatibility)
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
16863
diff
changeset
|
1056 |
^ self includesSubString: aString |
32e61bb3a742
class: CharacterArray>>#includesSubstring: (for Squeak compatibility)
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
16863
diff
changeset
|
1057 |
|
32e61bb3a742
class: CharacterArray>>#includesSubstring: (for Squeak compatibility)
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
16863
diff
changeset
|
1058 |
"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
|
1059 |
! |
32e61bb3a742
class: CharacterArray>>#includesSubstring: (for Squeak compatibility)
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
16863
diff
changeset
|
1060 |
|
9192 | 1061 |
includesSubstring:aString caseSensitive:caseSensitive |
9190 | 1062 |
"return true, if a substring is contained in the receiver. |
9192 | 1063 |
The argument, caseSensitive controls if case is ignored in the compare." |
1064 |
||
1065 |
"/ for now, a q&d hack ... |
|
1066 |
||
1067 |
caseSensitive ifFalse:[ |
|
16202 | 1068 |
^ self asLowercase includesString:aString asLowercase |
9192 | 1069 |
]. |
9190 | 1070 |
^ self includesString:aString |
1071 |
||
1072 |
" |
|
9192 | 1073 |
'hello world' includesSubstring:'Hel' caseSensitive:true |
1074 |
'hello world' includesSubstring:'Hel' caseSensitive:false |
|
1075 |
" |
|
1076 |
||
1077 |
||
1078 |
||
1079 |
! |
|
9190 | 1080 |
|
6979 | 1081 |
isAllDigits |
1082 |
"Answer whether the receiver's characters are all digits" |
|
1083 |
||
1084 |
^ self conform:[:eachChar | eachChar isDigit] |
|
1085 |
||
1086 |
" |
|
1087 |
'hello world' isAllDigits |
|
1088 |
'12344' isAllDigits |
|
1089 |
" |
|
1090 |
! |
|
1091 |
||
4890 | 1092 |
lastSpacePosition |
14405
1d0d99a95bba
comment/format in:7 methods
Claus Gittinger <cg@exept.de>
parents:
14168
diff
changeset
|
1093 |
"return the index of the last space character; 0 if there is none" |
1d0d99a95bba
comment/format in:7 methods
Claus Gittinger <cg@exept.de>
parents:
14168
diff
changeset
|
1094 |
|
4890 | 1095 |
^ self lastIndexOfSeparator |
1096 |
! |
|
1097 |
||
7022 | 1098 |
padded:leftOrRight to:paddedSize with:padCharacter |
14053
acbb97913c30
comment/format in: #padded:to:with:
Claus Gittinger <cg@exept.de>
parents:
14050
diff
changeset
|
1099 |
"pad left (leftOrRight==#left) or right" |
acbb97913c30
comment/format in: #padded:to:with:
Claus Gittinger <cg@exept.de>
parents:
14050
diff
changeset
|
1100 |
|
7022 | 1101 |
leftOrRight == #left ifTrue:[ |
14053
acbb97913c30
comment/format in: #padded:to:with:
Claus Gittinger <cg@exept.de>
parents:
14050
diff
changeset
|
1102 |
^ self leftPaddedTo:paddedSize with:padCharacter |
9190 | 1103 |
]. |
1104 |
^ self paddedTo:paddedSize with:padCharacter |
|
1105 |
||
1106 |
" |
|
8874 | 1107 |
'hello' padded:#right to:10 with:$. |
9192 | 1108 |
'hello' padded:#left to:10 with:$. |
1109 |
" |
|
14053
acbb97913c30
comment/format in: #padded:to:with:
Claus Gittinger <cg@exept.de>
parents:
14050
diff
changeset
|
1110 |
|
acbb97913c30
comment/format in: #padded:to:with:
Claus Gittinger <cg@exept.de>
parents:
14050
diff
changeset
|
1111 |
"Modified (comment): / 07-03-2012 / 16:31:33 / cg" |
9192 | 1112 |
! |
1113 |
||
1114 |
skipDelimiters:delimiters startingAt:start |
|
1115 |
"Answer the index of the character within the receiver, starting at start, |
|
1116 |
that does NOT match one of the delimiters. |
|
1117 |
If the receiver does not contain any of the delimiters, answer size + 1. |
|
1118 |
Assumes the delimiters to be a non-empty string." |
|
1119 |
||
1120 |
start to:self size do:[:i | |
|
16202 | 1121 |
delimiters detect:[:delim | delim = (self at:i) ] ifNone:[ ^ i ] |
9192 | 1122 |
]. |
1123 |
^ self size + 1 |
|
1124 |
||
1125 |
" |
|
1126 |
'123***7890' skipDelimiters:'*' startingAt:4 |
|
1127 |
'123***7890' skipDelimiters:'*' startingAt:3 |
|
9190 | 1128 |
'123***7890' skipDelimiters:'*' startingAt:10 |
1129 |
'123*******' skipDelimiters:'*' startingAt:10 |
|
8874 | 1130 |
" |
1131 |
! |
|
1132 |
||
9190 | 1133 |
substrings |
1134 |
^ self asCollectionOfWords |
|
1135 |
||
1136 |
" |
|
1137 |
'foo bar baz' substrings |
|
1138 |
" |
|
1139 |
! |
|
1140 |
||
12665
b027fc5aec4e
added: #substringsSeparatedBy:
Claus Gittinger <cg@exept.de>
parents:
12664
diff
changeset
|
1141 |
substringsSeparatedBy:separatorCharacter |
b027fc5aec4e
added: #substringsSeparatedBy:
Claus Gittinger <cg@exept.de>
parents:
12664
diff
changeset
|
1142 |
^ self asCollectionOfSubstringsSeparatedBy:separatorCharacter |
b027fc5aec4e
added: #substringsSeparatedBy:
Claus Gittinger <cg@exept.de>
parents:
12664
diff
changeset
|
1143 |
|
b027fc5aec4e
added: #substringsSeparatedBy:
Claus Gittinger <cg@exept.de>
parents:
12664
diff
changeset
|
1144 |
" |
b027fc5aec4e
added: #substringsSeparatedBy:
Claus Gittinger <cg@exept.de>
parents:
12664
diff
changeset
|
1145 |
'foo bar, baz' substringsSeparatedBy:$, |
b027fc5aec4e
added: #substringsSeparatedBy:
Claus Gittinger <cg@exept.de>
parents:
12664
diff
changeset
|
1146 |
" |
b027fc5aec4e
added: #substringsSeparatedBy:
Claus Gittinger <cg@exept.de>
parents:
12664
diff
changeset
|
1147 |
! |
b027fc5aec4e
added: #substringsSeparatedBy:
Claus Gittinger <cg@exept.de>
parents:
12664
diff
changeset
|
1148 |
|
9190 | 1149 |
truncateTo:smallSize |
9192 | 1150 |
"return myself or a copy shortened to smallSize. 1/18/96 sw" |
1151 |
||
1152 |
self size <= smallSize ifTrue:[^ self]. |
|
1153 |
^ self copyFrom: 1 to: smallSize |
|
1154 |
||
1155 |
" |
|
1156 |
'hello world' truncateTo:5 |
|
1157 |
'hello' truncateTo:10 |
|
1158 |
||
1159 |
'hello world' copyTo:5 |
|
1160 |
'hello' copyTo:10 |
|
1161 |
" |
|
1162 |
! |
|
5795 | 1163 |
|
4890 | 1164 |
withBlanksTrimmed |
1165 |
"Return a copy of the receiver from which leading and trailing blanks have been trimmed." |
|
1166 |
||
14865 | 1167 |
^ self withoutSeparators "withoutSpaces" "/ cg: it seems that squeak cares for any whitespace |
4890 | 1168 |
|
1169 |
" |
|
8874 | 1170 |
' hello world ' withBlanksTrimmed |
4890 | 1171 |
" |
1172 |
! |
|
1173 |
||
1174 |
withNoLineLongerThan: aNumber |
|
1175 |
"Answer a string with the same content as receiver, but rewrapped so that no line has more characters than the given number" |
|
1176 |
||
9192 | 1177 |
| listOfLines currentLast currentStart resultString putativeLast putativeLine crPosition | |
1178 |
||
14451
c06e7885b675
changed: #withNoLineLongerThan:
Claus Gittinger <cg@exept.de>
parents:
14420
diff
changeset
|
1179 |
(aNumber isNumber not or:[ aNumber < 1] ) ifTrue: [self error: 'too narrow']. |
9192 | 1180 |
listOfLines _ OrderedCollection new. |
1181 |
currentLast _ 0. |
|
1182 |
[currentLast < self size] whileTrue: |
|
14451
c06e7885b675
changed: #withNoLineLongerThan:
Claus Gittinger <cg@exept.de>
parents:
14420
diff
changeset
|
1183 |
[currentStart _ currentLast + 1. |
c06e7885b675
changed: #withNoLineLongerThan:
Claus Gittinger <cg@exept.de>
parents:
14420
diff
changeset
|
1184 |
putativeLast _ (currentStart + aNumber - 1) min: self size. |
c06e7885b675
changed: #withNoLineLongerThan:
Claus Gittinger <cg@exept.de>
parents:
14420
diff
changeset
|
1185 |
putativeLine _ self copyFrom: currentStart to: putativeLast. |
c06e7885b675
changed: #withNoLineLongerThan:
Claus Gittinger <cg@exept.de>
parents:
14420
diff
changeset
|
1186 |
(crPosition _ putativeLine indexOf: Character cr) > 0 ifTrue: |
c06e7885b675
changed: #withNoLineLongerThan:
Claus Gittinger <cg@exept.de>
parents:
14420
diff
changeset
|
1187 |
[putativeLast _ currentStart + crPosition - 1. |
c06e7885b675
changed: #withNoLineLongerThan:
Claus Gittinger <cg@exept.de>
parents:
14420
diff
changeset
|
1188 |
putativeLine _ self copyFrom: currentStart to: putativeLast]. |
c06e7885b675
changed: #withNoLineLongerThan:
Claus Gittinger <cg@exept.de>
parents:
14420
diff
changeset
|
1189 |
currentLast _ putativeLast == self size |
c06e7885b675
changed: #withNoLineLongerThan:
Claus Gittinger <cg@exept.de>
parents:
14420
diff
changeset
|
1190 |
ifTrue: |
c06e7885b675
changed: #withNoLineLongerThan:
Claus Gittinger <cg@exept.de>
parents:
14420
diff
changeset
|
1191 |
[putativeLast] |
c06e7885b675
changed: #withNoLineLongerThan:
Claus Gittinger <cg@exept.de>
parents:
14420
diff
changeset
|
1192 |
ifFalse: |
c06e7885b675
changed: #withNoLineLongerThan:
Claus Gittinger <cg@exept.de>
parents:
14420
diff
changeset
|
1193 |
[currentStart + putativeLine lastSpacePosition - 1]. |
c06e7885b675
changed: #withNoLineLongerThan:
Claus Gittinger <cg@exept.de>
parents:
14420
diff
changeset
|
1194 |
currentLast <= currentStart ifTrue: |
c06e7885b675
changed: #withNoLineLongerThan:
Claus Gittinger <cg@exept.de>
parents:
14420
diff
changeset
|
1195 |
["line has NO spaces; baleout!!" |
c06e7885b675
changed: #withNoLineLongerThan:
Claus Gittinger <cg@exept.de>
parents:
14420
diff
changeset
|
1196 |
currentLast _ putativeLast]. |
c06e7885b675
changed: #withNoLineLongerThan:
Claus Gittinger <cg@exept.de>
parents:
14420
diff
changeset
|
1197 |
listOfLines add: (self copyFrom: currentStart to: currentLast) withBlanksTrimmed]. |
9192 | 1198 |
|
1199 |
listOfLines size > 0 ifFalse: [^ '']. |
|
1200 |
resultString _ listOfLines first. |
|
1201 |
2 to: listOfLines size do: |
|
14451
c06e7885b675
changed: #withNoLineLongerThan:
Claus Gittinger <cg@exept.de>
parents:
14420
diff
changeset
|
1202 |
[:i | resultString _ resultString, Character cr asString, (listOfLines at: i)]. |
9192 | 1203 |
^ resultString |
1204 |
||
1205 |
" |
|
1206 |
#(5 7 20) collect: |
|
14451
c06e7885b675
changed: #withNoLineLongerThan:
Claus Gittinger <cg@exept.de>
parents:
14420
diff
changeset
|
1207 |
[:i | 'Fred the bear went down to the brook to read his book in silence' withNoLineLongerThan: i] |
c06e7885b675
changed: #withNoLineLongerThan:
Claus Gittinger <cg@exept.de>
parents:
14420
diff
changeset
|
1208 |
" |
10883 | 1209 |
! |
1210 |
||
12343
0a93eee37d65
added: #withSqueakLineEndings
Claus Gittinger <cg@exept.de>
parents:
12330
diff
changeset
|
1211 |
withSqueakLineEndings |
13882
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
1212 |
"assume the string is textual, and that CR, LF, and CRLF are all |
12343
0a93eee37d65
added: #withSqueakLineEndings
Claus Gittinger <cg@exept.de>
parents:
12330
diff
changeset
|
1213 |
valid line endings. Replace each occurence with a single CR" |
0a93eee37d65
added: #withSqueakLineEndings
Claus Gittinger <cg@exept.de>
parents:
12330
diff
changeset
|
1214 |
|
0a93eee37d65
added: #withSqueakLineEndings
Claus Gittinger <cg@exept.de>
parents:
12330
diff
changeset
|
1215 |
^ self asStringCollection asStringWith:Character cr. |
0a93eee37d65
added: #withSqueakLineEndings
Claus Gittinger <cg@exept.de>
parents:
12330
diff
changeset
|
1216 |
! |
0a93eee37d65
added: #withSqueakLineEndings
Claus Gittinger <cg@exept.de>
parents:
12330
diff
changeset
|
1217 |
|
12188 | 1218 |
withoutLeading: char |
1219 |
"return a copy of myself without leading a char. |
|
1220 |
Returns an empty string, if the receiver consist only of a char." |
|
1221 |
||
1222 |
^ self withoutLeadingForWhich:[:ch | ch = char] |
|
1223 |
||
1224 |
" |
|
13882
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
1225 |
'****foo****' withoutLeading: $* |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
1226 |
'foo****' withoutLeading: $* |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
1227 |
'*' withoutLeading: $* |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
1228 |
'' withoutLeading: $* |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
1229 |
'****foo' withoutLeading: $* |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
1230 |
'*******' withoutLeading: $* |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
1231 |
'foo' withoutLeading: $* |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
1232 |
'f***o***o' withoutLeading: $* |
12188 | 1233 |
('**' , Character tab asString , '*foo***') withoutLeading: $* inspect |
1234 |
" |
|
1235 |
! |
|
1236 |
||
11565 | 1237 |
withoutTrailing:char |
1238 |
"return a copy of myself without trailing char. |
|
1239 |
Returns an empty string, if the receiver consist only of char." |
|
1240 |
||
12188 | 1241 |
^ self withoutTrailingForWhich:[:ch | ch = char] |
1242 |
||
1243 |
" |
|
13882
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
1244 |
' foo....' withoutTrailing:$. |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
1245 |
'foo....' withoutTrailing:$. |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
1246 |
' foo' withoutTrailing:$. |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
1247 |
'.......' withoutTrailing:$. |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
1248 |
'foo' withoutTrailing:$. |
11565 | 1249 |
" |
9190 | 1250 |
! ! |
1251 |
||
1252 |
!CharacterArray methodsFor:'Compatibility-V''Age'! |
|
1253 |
||
1254 |
addLineDelimiter |
|
1255 |
"replace all '\'-characters by line delimiter (cr) - characters. |
|
9192 | 1256 |
This has been added for VisualAge compatibility." |
1257 |
||
1258 |
^ self withCRs |
|
1259 |
! |
|
1260 |
||
1261 |
bindWith:aString |
|
1262 |
"return a copy of the receiver, where a '%1' escape is |
|
1263 |
replaced by aString. |
|
1264 |
This has been added for VisualAge compatibility." |
|
1265 |
||
1266 |
^ self expandPlaceholdersWith:(Array with:aString) |
|
1267 |
||
1268 |
" |
|
1269 |
'do you like %1 ?' bindWith:'smalltalk' |
|
1270 |
" |
|
1271 |
! |
|
608 | 1272 |
|
1273 |
bindWith:string1 with:string2 |
|
1274 |
"return a copy of the receiver, where a '%1' escape is |
|
1275 |
replaced by string1 and '%2' is replaced by string2. |
|
1276 |
This has been added for VisualAge compatibility." |
|
1277 |
||
1278 |
^ self expandPlaceholdersWith:(Array with:string1 with:string2) |
|
1279 |
||
1280 |
" |
|
1281 |
'do you prefer %1 or rather %2 ?' |
|
16202 | 1282 |
bindWith:'smalltalk' with:'c++' |
9192 | 1283 |
" |
1284 |
! |
|
9190 | 1285 |
|
1286 |
bindWith:str1 with:str2 with:str3 |
|
608 | 1287 |
"return a copy of the receiver, where a '%1', '%2' and '%3' escapes |
1288 |
are replaced by str1, str2 and str3 respectively. |
|
1289 |
This has been added for VisualAge compatibility." |
|
1290 |
||
1291 |
^ self expandPlaceholdersWith:(Array with:str1 with:str2 with:str3) |
|
1292 |
||
1293 |
" |
|
9192 | 1294 |
'do you prefer %1 or rather %2 (not talking about %3) ?' |
16202 | 1295 |
bindWith:'smalltalk' with:'c++' with:'c' |
9192 | 1296 |
" |
1297 |
! |
|
608 | 1298 |
|
1299 |
bindWith:str1 with:str2 with:str3 with:str4 |
|
1300 |
"return a copy of the receiver, where a '%1', '%2', '%3' and '%4' escapes |
|
1301 |
are replaced by str1, str2, str3 and str4 respectively. |
|
1302 |
This has been added for VisualAge compatibility." |
|
1303 |
||
1304 |
^ self expandPlaceholdersWith:(Array with:str1 with:str2 with:str3 with:str4) |
|
1305 |
||
1306 |
" |
|
9192 | 1307 |
'do you prefer %1 or rather %2 (not talking about %3 or even %4) ?' |
16202 | 1308 |
bindWith:'smalltalk' with:'c++' with:'c' with:'assembler' |
9192 | 1309 |
" |
1310 |
! |
|
608 | 1311 |
|
2344 | 1312 |
bindWith:str1 with:str2 with:str3 with:str4 with:str5 |
1313 |
"return a copy of the receiver, where a '%1' .. '%5' escapes |
|
1314 |
are replaced by str1 .. str5 respectively. |
|
1315 |
This has been added for VisualAge compatibility." |
|
1316 |
||
1317 |
^ self expandPlaceholdersWith:(Array with:str1 with:str2 with:str3 with:str4 with:str5) |
|
1318 |
||
1319 |
"Created: 31.1.1997 / 16:25:42 / cg" |
|
1320 |
! |
|
1321 |
||
5343 | 1322 |
bindWith:str1 with:str2 with:str3 with:str4 with:str5 with:str6 |
1323 |
"return a copy of the receiver, where a '%1' .. '%6' escapes |
|
11524 | 1324 |
are replaced by str1 .. str6 respectively. |
5343 | 1325 |
This has been added for VisualAge compatibility." |
1326 |
||
8874 | 1327 |
^ self expandPlaceholdersWith:(Array with:str1 with:str2 |
16202 | 1328 |
with:str3 with:str4 |
1329 |
with:str5 with:str6) |
|
11524 | 1330 |
! |
1331 |
||
1332 |
bindWith:str1 with:str2 with:str3 with:str4 with:str5 with:str6 with:str7 |
|
1333 |
"return a copy of the receiver, where a '%1' .. '%7' escapes |
|
1334 |
are replaced by str1 .. str7 respectively. |
|
1335 |
This has been added for VisualAge compatibility." |
|
1336 |
||
1337 |
^ self expandPlaceholdersWith:(Array with:str1 with:str2 |
|
16202 | 1338 |
with:str3 with:str4 |
1339 |
with:str5 with:str6 |
|
1340 |
with:str7) |
|
5343 | 1341 |
! |
1342 |
||
13996
a86faa4f17b8
added: #bindWith:with:with:with:with:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
13987
diff
changeset
|
1343 |
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
|
1344 |
"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
|
1345 |
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
|
1346 |
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
|
1347 |
|
a86faa4f17b8
added: #bindWith:with:with:with:with:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
13987
diff
changeset
|
1348 |
^ self expandPlaceholdersWith:(Array with:str1 with:str2 |
a86faa4f17b8
added: #bindWith:with:with:with:with:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
13987
diff
changeset
|
1349 |
with:str3 with:str4 |
a86faa4f17b8
added: #bindWith:with:with:with:with:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
13987
diff
changeset
|
1350 |
with:str5 with:str6 |
a86faa4f17b8
added: #bindWith:with:with:with:with:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
13987
diff
changeset
|
1351 |
with:str7 with:str8) |
a86faa4f17b8
added: #bindWith:with:with:with:with:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
13987
diff
changeset
|
1352 |
|
a86faa4f17b8
added: #bindWith:with:with:with:with:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
13987
diff
changeset
|
1353 |
"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
|
1354 |
! |
a86faa4f17b8
added: #bindWith:with:with:with:with:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
13987
diff
changeset
|
1355 |
|
14014
3aff2d7c41c8
added: #bindWith:with:with:with:with:with:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
13996
diff
changeset
|
1356 |
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
|
1357 |
"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
|
1358 |
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
|
1359 |
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
|
1360 |
|
3aff2d7c41c8
added: #bindWith:with:with:with:with:with:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
13996
diff
changeset
|
1361 |
^ self expandPlaceholdersWith:(Array with:str1 with:str2 |
3aff2d7c41c8
added: #bindWith:with:with:with:with:with:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
13996
diff
changeset
|
1362 |
with:str3 with:str4 |
3aff2d7c41c8
added: #bindWith:with:with:with:with:with:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
13996
diff
changeset
|
1363 |
with:str5 with:str6 |
3aff2d7c41c8
added: #bindWith:with:with:with:with:with:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
13996
diff
changeset
|
1364 |
with:str7 with:str8 |
3aff2d7c41c8
added: #bindWith:with:with:with:with:with:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
13996
diff
changeset
|
1365 |
with:str9) |
3aff2d7c41c8
added: #bindWith:with:with:with:with:with:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
13996
diff
changeset
|
1366 |
|
3aff2d7c41c8
added: #bindWith:with:with:with:with:with:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
13996
diff
changeset
|
1367 |
"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
|
1368 |
! |
3aff2d7c41c8
added: #bindWith:with:with:with:with:with:with:with:with:
Claus Gittinger <cg@exept.de>
parents:
13996
diff
changeset
|
1369 |
|
608 | 1370 |
bindWithArguments:anArrayOfStrings |
1371 |
"return a copy of the receiver, where a '%i' escape |
|
1372 |
is replaced by the coresponding string from the argument array. |
|
1373 |
'i' may be between 1 and 9 (i.e. a maximum of 9 placeholders is allowed). |
|
1374 |
This has been added for VisualAge compatibility." |
|
1375 |
||
9192 | 1376 |
^ self expandPlaceholdersWith:anArrayOfStrings |
1377 |
||
1378 |
" |
|
1379 |
'do you prefer %1 or rather %2 (not talking about %3) ?' |
|
16202 | 1380 |
bindWithArguments:#('smalltalk' 'c++' 'c') |
12196 | 1381 |
|
1382 |
'do you %(what) ?' |
|
16202 | 1383 |
bindWithArguments:(Dictionary new at:#'what' put:'understand'; yourself) |
9192 | 1384 |
" |
1385 |
! |
|
608 | 1386 |
|
1387 |
subStrings |
|
1388 |
"return an array consisting of all words contained in the receiver. |
|
1389 |
Words are separated by whitespace. |
|
1390 |
This has been added for VisualAge compatibility." |
|
1391 |
||
1392 |
^ self asCollectionOfWords |
|
1393 |
||
1394 |
" |
|
1395 |
'hello world, this is smalltalk' subStrings |
|
1396 |
" |
|
1397 |
! |
|
1398 |
||
16181 | 1399 |
subStrings:separatorCharacterOrString |
608 | 1400 |
"return an array consisting of all words contained in the receiver. |
1401 |
Words are separated by separatorCharacter. |
|
1402 |
This has been added for VisualAge compatibility." |
|
1403 |
||
16181 | 1404 |
separatorCharacterOrString isCharacter ifTrue:[ |
1405 |
^ self asCollectionOfSubstringsSeparatedBy:separatorCharacterOrString |
|
1406 |
]. |
|
1407 |
^ self asCollectionOfSubstringsSeparatedByAny:separatorCharacterOrString |
|
1408 |
||
1409 |
" |
|
1410 |
'foo:bar:baz:smalltalk' subStrings:$: |
|
1411 |
'foo:bar:baz:smalltalk' subStrings:':' |
|
1412 |
'foo.bar,baz-smalltalk' subStrings:'.,-' |
|
9192 | 1413 |
" |
1414 |
! |
|
1415 |
||
1416 |
trimSeparators |
|
1417 |
"return a copy of the receiver without leading and trailing whiteSpace" |
|
1418 |
||
1419 |
^ self withoutSeparators |
|
1420 |
! ! |
|
1421 |
||
1422 |
!CharacterArray methodsFor:'Compatibility-VW'! |
|
5964 | 1423 |
|
16051 | 1424 |
asComposedText |
1425 |
"ST-80 compatibility |
|
1426 |
- ST/X does not (as today) support composedTexts." |
|
1427 |
||
1428 |
^ ComposedText fromString:self string |
|
1429 |
||
1430 |
"Modified: 27.4.1996 / 13:30:30 / cg" |
|
1431 |
! |
|
1432 |
||
6139 | 1433 |
asLogicalFileSpecification |
1434 |
^ self asFilename |
|
1435 |
||
1436 |
"Created: / 30.10.2001 / 17:29:53 / cg" |
|
1437 |
! |
|
1438 |
||
6119 | 1439 |
asQualifiedReference |
1440 |
^ BindingReference pathString:(self string) |
|
1441 |
||
1442 |
! |
|
1443 |
||
5964 | 1444 |
expandMacros |
1445 |
"ST80 compatibility - expand '<..>' macros with |
|
1446 |
argument strings. Similar to #bindWith:. |
|
1447 |
Read the comment in #expandMacrosWithArguments: about |
|
1448 |
limited compatibility issues." |
|
1449 |
||
1450 |
^ self expandMacrosWithArguments:#() |
|
1451 |
||
1452 |
" |
|
1453 |
'hello<n>foo' expandMacros |
|
1454 |
" |
|
1455 |
||
9192 | 1456 |
"Modified: / 18.6.1998 / 16:03:02 / cg" |
1457 |
! |
|
1458 |
||
1459 |
expandMacrosWith:arg |
|
5964 | 1460 |
"ST80 compatibility - expand '<..>' macros with |
1461 |
argument strings. Similar to #bindWith:. |
|
1462 |
Read the comment in #expandMacrosWithArguments: about |
|
1463 |
limited compatibility issues." |
|
1464 |
||
9192 | 1465 |
^ self expandMacrosWithArguments:(Array with:arg) |
1466 |
||
1467 |
"Created: / 1.11.1997 / 13:01:28 / cg" |
|
1468 |
"Modified: / 1.11.1997 / 13:30:50 / cg" |
|
1469 |
! |
|
5964 | 1470 |
|
1471 |
expandMacrosWith:arg1 with:arg2 |
|
1472 |
"ST80 compatibility - expand '<..>' macros with |
|
1473 |
argument strings. Similar to #bindWith:. |
|
1474 |
Read the comment in #expandMacrosWithArguments: about |
|
1475 |
limited compatibility issues." |
|
1476 |
||
1477 |
^ self expandMacrosWithArguments:(Array with:arg1 with:arg2) |
|
1478 |
||
1479 |
"Modified: / 6.7.1998 / 21:58:14 / cg" |
|
1480 |
! |
|
1481 |
||
1482 |
expandMacrosWith:arg1 with:arg2 with:arg3 |
|
1483 |
"ST80 compatibility - expand '<..>' macros with |
|
1484 |
argument strings. Similar to #bindWith:. |
|
1485 |
Read the comment in #expandMacrosWithArguments: about |
|
1486 |
limited compatibility issues." |
|
1487 |
||
1488 |
^ self expandMacrosWithArguments:(Array with:arg1 with:arg2 with:arg3) |
|
1489 |
! |
|
1490 |
||
10353
65fb7aa2c6c2
added #expandMacrosWith:with:with:with:
Stefan Vogel <sv@exept.de>
parents:
10134
diff
changeset
|
1491 |
expandMacrosWith:arg1 with:arg2 with:arg3 with:arg4 |
65fb7aa2c6c2
added #expandMacrosWith:with:with:with:
Stefan Vogel <sv@exept.de>
parents:
10134
diff
changeset
|
1492 |
"ST80 compatibility - expand '<..>' macros with |
65fb7aa2c6c2
added #expandMacrosWith:with:with:with:
Stefan Vogel <sv@exept.de>
parents:
10134
diff
changeset
|
1493 |
argument strings. Similar to #bindWith:. |
65fb7aa2c6c2
added #expandMacrosWith:with:with:with:
Stefan Vogel <sv@exept.de>
parents:
10134
diff
changeset
|
1494 |
Read the comment in #expandMacrosWithArguments: about |
65fb7aa2c6c2
added #expandMacrosWith:with:with:with:
Stefan Vogel <sv@exept.de>
parents:
10134
diff
changeset
|
1495 |
limited compatibility issues." |
65fb7aa2c6c2
added #expandMacrosWith:with:with:with:
Stefan Vogel <sv@exept.de>
parents:
10134
diff
changeset
|
1496 |
|
65fb7aa2c6c2
added #expandMacrosWith:with:with:with:
Stefan Vogel <sv@exept.de>
parents:
10134
diff
changeset
|
1497 |
^ 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
|
1498 |
! |
65fb7aa2c6c2
added #expandMacrosWith:with:with:with:
Stefan Vogel <sv@exept.de>
parents:
10134
diff
changeset
|
1499 |
|
9192 | 1500 |
expandMacrosWithArguments:argArray |
5964 | 1501 |
"ST80 compatibility - expand '<..>' macros with |
1502 |
argument strings. Similar to #bindWith:. |
|
1503 |
WARNING: possibly not all ST80 expansions are supported here." |
|
1504 |
||
1505 |
"/ supported expansions: |
|
1506 |
"/ |
|
1507 |
"/ <#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
|
1508 |
"/ <#s> # is arg Number; slice in the arg itself (must know asString) |
5964 | 1509 |
"/ <#?s1:s2> # is arg Number; slice in s1 if the arg is true, s2 otherwise |
7643 | 1510 |
"/ use first arg if # is not given (i.e. no number before s,p ...) |
5964 | 1511 |
"/ <n> replace by a newLine character |
1512 |
"/ <t> replace by a tab character |
|
6834
eba4b58b8692
fixed expandMacros (%-escape)
Claus Gittinger <cg@exept.de>
parents:
6810
diff
changeset
|
1513 |
"/ %X the X character itself |
5964 | 1514 |
|
8999
f3fda66ea51e
#expandMacrosWithArguments: - be mor friendly to xml strings.
Stefan Vogel <sv@exept.de>
parents:
8971
diff
changeset
|
1515 |
|in out c fmt nr arg s1 s2 peekc| |
5964 | 1516 |
|
1517 |
in := self readStream. |
|
10721
58e6950264cb
care for bitsPerCharacter when expanding macros
Claus Gittinger <cg@exept.de>
parents:
10703
diff
changeset
|
1518 |
out := CharacterWriteStream on:(self species uninitializedNew:self size). |
5964 | 1519 |
|
1520 |
[in atEnd] whileFalse:[ |
|
16202 | 1521 |
c := in next. |
1522 |
c == $% ifTrue:[ |
|
1523 |
c := in next. |
|
1524 |
out nextPut:c |
|
1525 |
] ifFalse:[c ~~ $< ifTrue:[ |
|
1526 |
out nextPut:c. |
|
1527 |
] ifFalse:[ |
|
1528 |
peekc := in peek. |
|
1529 |
[peekc == $<] whileTrue:[ |
|
1530 |
out nextPut:$<. |
|
1531 |
peekc := in nextPeek. |
|
1532 |
]. |
|
1533 |
peekc == $n ifTrue:[ |
|
1534 |
peekc := in nextPeek. |
|
1535 |
peekc == $> ifTrue:[ |
|
1536 |
in next. |
|
1537 |
out cr. |
|
1538 |
] ifFalse:[ |
|
1539 |
out nextPutAll:'<n'. |
|
1540 |
] |
|
1541 |
] ifFalse:[peekc == $t ifTrue:[ |
|
1542 |
peekc := in nextPeek. |
|
1543 |
peekc == $> ifTrue:[ |
|
1544 |
in next. |
|
1545 |
out tab. |
|
1546 |
] ifFalse:[ |
|
1547 |
out nextPutAll:'<t'. |
|
1548 |
] |
|
1549 |
] ifFalse:[ |
|
1550 |
peekc isDigit ifTrue:[ |
|
1551 |
"start an argument expansion ..." |
|
1552 |
nr := Integer readFrom:in onError:nil. |
|
1553 |
nr isNil ifTrue:[ |
|
1554 |
"this cannot happen (there is at least one digit)" |
|
1555 |
self error:'invalid format' mayProceed:true. |
|
1556 |
^ self |
|
1557 |
]. |
|
1558 |
fmt := in next. |
|
1559 |
(fmt ~~ $? and:[in peek ~~ $>]) ifTrue:[ |
|
1560 |
out nextPut:$<. |
|
1561 |
nr printOn:out. |
|
1562 |
out nextPut:fmt. |
|
1563 |
] ifFalse:[ |
|
1564 |
(nr between:1 and:argArray size) ifTrue:[ |
|
1565 |
arg := argArray at:nr. |
|
1566 |
] ifFalse:[ |
|
1567 |
arg := '' |
|
1568 |
]. |
|
1569 |
||
1570 |
fmt == $p ifTrue:[ |
|
1571 |
"expand with args printString" |
|
1572 |
arg printOn:out. |
|
1573 |
] ifFalse:[fmt == $s ifTrue:[ |
|
1574 |
"expand with arg itself" |
|
1575 |
arg isText ifTrue:[ |
|
1576 |
out contentsSpecies isText ifFalse:[ |
|
1577 |
out := (WriteStream on:Text new) nextPutAll:out contents; yourself. |
|
1578 |
]. |
|
1579 |
out nextPutAll:arg. |
|
1580 |
] ifFalse:[ |
|
16473 | 1581 |
out nextPutAll:arg asString string. "see method comment: arg must know #asString" |
16202 | 1582 |
] |
1583 |
] ifFalse:[fmt == $? ifTrue:[ |
|
1584 |
s1 := in upTo:$:. |
|
1585 |
s2 := in nextUpTo:$>. |
|
1586 |
arg == true ifTrue:[ |
|
1587 |
out nextPutAll:s1 |
|
1588 |
] ifFalse:[ |
|
1589 |
out nextPutAll:s2 |
|
1590 |
]. |
|
1591 |
] ifFalse:[ |
|
1592 |
"what does VW do here ?" |
|
1593 |
self error:'invalid format' mayProceed:true. |
|
1594 |
^ self |
|
1595 |
]]]. |
|
1596 |
c := in next. |
|
1597 |
c ~~ $> ifTrue:[ |
|
1598 |
"what does VW do here ?" |
|
1599 |
self error:'invalid format' mayProceed:true. |
|
1600 |
^ self |
|
1601 |
] |
|
1602 |
||
1603 |
]. |
|
1604 |
] ifFalse:[ |
|
1605 |
out nextPut:$<. |
|
1606 |
]. |
|
1607 |
]]. |
|
1608 |
]]. |
|
5964 | 1609 |
]. |
1610 |
^ out contents |
|
1611 |
||
1612 |
" |
|
8874 | 1613 |
'hello <1s> how are you' expandMacrosWith:(OperatingSystem getLoginName) |
1614 |
'one plus one is <1p>' expandMacrosWith:2 |
|
5964 | 1615 |
" |
1616 |
||
10721
58e6950264cb
care for bitsPerCharacter when expanding macros
Claus Gittinger <cg@exept.de>
parents:
10703
diff
changeset
|
1617 |
"Modified: / 18-09-2007 / 22:50:43 / cg" |
9192 | 1618 |
! |
1619 |
||
1620 |
isCharacters |
|
1621 |
"added for visual works compatibility" |
|
1622 |
^ true |
|
1623 |
! ! |
|
1624 |
||
16295 | 1625 |
!CharacterArray methodsFor:'JavaScript support'! |
1626 |
||
1627 |
unquote |
|
16693 | 1628 |
"removes double quotes from the receiver" |
16295 | 1629 |
|
1630 |
self size >= 2 ifTrue:[ |
|
1631 |
(self startsWith:$") ifTrue:[ |
|
1632 |
(self endsWith:$") ifTrue:[ |
|
1633 |
^ self copyFrom:2 to:self size-1 |
|
1634 |
]. |
|
1635 |
]. |
|
1636 |
]. |
|
1637 |
^ self |
|
1638 |
||
1639 |
" |
|
1640 |
JavaScriptParser |
|
1641 |
evaluate:'''hello''.quote.unquote' |
|
1642 |
" |
|
1643 |
! ! |
|
13510 | 1644 |
|
16175 | 1645 |
|
9192 | 1646 |
!CharacterArray methodsFor:'character searching'! |
1647 |
||
1648 |
includesMatchCharacters |
|
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
1649 |
"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
|
1650 |
for match operations; false if not. |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
1651 |
Here, do not care for $\ escapes" |
608 | 1652 |
|
1653 |
^ self includesAny:'*#[' |
|
2519
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
1654 |
|
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
1655 |
" |
8874 | 1656 |
'*foo' includesMatchCharacters |
1657 |
'\*foo' includesMatchCharacters |
|
1658 |
'\*foo' includesUnescapedMatchCharacters |
|
1659 |
'*foo' includesMatchCharacters |
|
2519
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
1660 |
'\\*foo' includesMatchCharacters |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
1661 |
'foo*' includesMatchCharacters |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
1662 |
'foo\*' includesMatchCharacters |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
1663 |
'foo\' includesMatchCharacters |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
1664 |
" |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
1665 |
|
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
1666 |
"Modified: 2.4.1997 / 18:12:34 / cg" |
9192 | 1667 |
! |
1668 |
||
1669 |
includesSeparator |
|
1670 |
"return true, if the receiver contains any whitespace characters" |
|
1671 |
||
1672 |
^ (self indexOfSeparator ~~ 0) |
|
1673 |
||
1674 |
" |
|
1675 |
'hello world' includesSeparator |
|
1676 |
'helloworld' includesSeparator |
|
1677 |
" |
|
1678 |
! |
|
1679 |
||
1680 |
includesUnescapedMatchCharacters |
|
8874 | 1681 |
"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
|
1682 |
for match operations; false if not. |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
1683 |
Here, care for $\ escapes" |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
1684 |
|
5342
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
1685 |
|idx sz specialChars escape| |
2519
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
1686 |
|
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
1687 |
idx := 1. |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
1688 |
sz := self size. |
5342
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
1689 |
specialChars := '*#[\'. |
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
1690 |
(escape := self class matchEscapeCharacter) ~~ $\ ifTrue:[ |
16202 | 1691 |
specialChars := specialChars copy. |
1692 |
specialChars at:specialChars size put:escape |
|
5342
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
1693 |
]. |
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
1694 |
|
16245 | 1695 |
[ |
16202 | 1696 |
idx := self indexOfAny:specialChars startingAt:idx. |
1697 |
idx == 0 ifTrue:[^ false]. |
|
1698 |
(self at:idx) == escape ifFalse:[^ true]. |
|
1699 |
idx := idx + 2. |
|
1700 |
idx > sz ifTrue:[^ false]. |
|
16245 | 1701 |
] loop. |
8874 | 1702 |
|
1703 |
" |
|
1704 |
'*foo' includesUnescapedMatchCharacters |
|
1705 |
'\*foo' includesUnescapedMatchCharacters |
|
1706 |
'\\foo' includesUnescapedMatchCharacters |
|
1707 |
'\\\$foo' includesUnescapedMatchCharacters |
|
2519
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
1708 |
'*foo' includesUnescapedMatchCharacters |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
1709 |
'\\*foo' includesUnescapedMatchCharacters |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
1710 |
'foo*' includesUnescapedMatchCharacters |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
1711 |
'foo\*' includesUnescapedMatchCharacters |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
1712 |
'foo\' includesUnescapedMatchCharacters |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
1713 |
" |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
1714 |
|
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
1715 |
"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
|
1716 |
"Created: 2.4.1997 / 17:23:26 / cg" |
9192 | 1717 |
! |
1718 |
||
1719 |
indexOfControlCharacterStartingAt:startIndex |
|
3668
76271941f044
added #indexOfControlCharacterStartingAt:
Claus Gittinger <cg@exept.de>
parents:
3656
diff
changeset
|
1720 |
"return the index of the next control character; |
76271941f044
added #indexOfControlCharacterStartingAt:
Claus Gittinger <cg@exept.de>
parents:
3656
diff
changeset
|
1721 |
that is a character with asciiValue < 32. |
15605 | 1722 |
Start the search at startIndex, searching forward. |
3668
76271941f044
added #indexOfControlCharacterStartingAt:
Claus Gittinger <cg@exept.de>
parents:
3656
diff
changeset
|
1723 |
Return 0 if none is found." |
76271941f044
added #indexOfControlCharacterStartingAt:
Claus Gittinger <cg@exept.de>
parents:
3656
diff
changeset
|
1724 |
|
76271941f044
added #indexOfControlCharacterStartingAt:
Claus Gittinger <cg@exept.de>
parents:
3656
diff
changeset
|
1725 |
|start "{ Class: SmallInteger }" |
9192 | 1726 |
mySize "{ Class: SmallInteger }"| |
1727 |
||
1728 |
start := startIndex. |
|
1729 |
mySize := self size. |
|
1730 |
||
1731 |
start to:mySize do:[:index | |
|
15605 | 1732 |
(self at:index) isControlCharacter ifTrue:[^ index] |
9192 | 1733 |
]. |
1734 |
^ 0 |
|
1735 |
||
1736 |
" |
|
1737 |
'hello world' asTwoByteString indexOfControlCharacterStartingAt:1 |
|
1738 |
'hello world\foo' withCRsasTwoByteString indexOfControlCharacterStartingAt:1 |
|
1739 |
" |
|
1740 |
||
1741 |
"Modified: / 21.7.1998 / 17:25:07 / cg" |
|
1742 |
! |
|
3668
76271941f044
added #indexOfControlCharacterStartingAt:
Claus Gittinger <cg@exept.de>
parents:
3656
diff
changeset
|
1743 |
|
8541 | 1744 |
indexOfNonSeparator |
1745 |
"return the index of the first non-whitespace character. |
|
1746 |
return 0 if no non-separator was found" |
|
1747 |
||
1748 |
^ self indexOfNonSeparatorStartingAt:1. |
|
1749 |
||
1750 |
" |
|
8874 | 1751 |
' hello world' indexOfNonSeparator |
1752 |
' ' indexOfNonSeparator |
|
1753 |
'a ' indexOfNonSeparator |
|
1754 |
'abc' indexOfNonSeparator |
|
1755 |
' ' indexOfNonSeparator |
|
9192 | 1756 |
'' indexOfNonSeparator |
1757 |
" |
|
1758 |
! |
|
1759 |
||
1760 |
indexOfNonSeparatorStartingAt:startIndex |
|
4117 | 1761 |
"return the index of the next non-whitespace character, |
1762 |
starting the search at startIndex, searching forward; |
|
1763 |
return 0 if no non-separator was found" |
|
608 | 1764 |
|
1765 |
|start "{ Class: SmallInteger }" |
|
1766 |
mySize "{ Class: SmallInteger }"| |
|
1767 |
||
1768 |
start := startIndex. |
|
1769 |
mySize := self size. |
|
1770 |
||
1771 |
start to:mySize do:[:index | |
|
16202 | 1772 |
(self at:index) isSeparator ifFalse:[^ index] |
608 | 1773 |
]. |
1774 |
^ 0 |
|
1775 |
||
1776 |
" |
|
8874 | 1777 |
' hello world' indexOfNonSeparatorStartingAt:1 |
1778 |
' ' indexOfNonSeparatorStartingAt:1 |
|
1779 |
'a ' indexOfNonSeparatorStartingAt:2 |
|
4117 | 1780 |
" |
1781 |
||
608 | 1782 |
" |
1783 |
|s index1 index2| |
|
1784 |
s := ' foo bar baz'. |
|
1785 |
index1 := s indexOfNonSeparatorStartingAt:1. |
|
1786 |
index2 := s indexOfSeparatorStartingAt:index1. |
|
1787 |
s copyFrom:index1 to:index2 - 1 |
|
1788 |
" |
|
9192 | 1789 |
! |
1790 |
||
1791 |
indexOfSeparator |
|
1792 |
"return the index of the first whitespace character; |
|
1793 |
starting the search at the beginning, searching forward; |
|
1794 |
return 0 if no separator was found" |
|
1795 |
||
1796 |
^ self indexOfSeparatorStartingAt:1 |
|
1797 |
||
1798 |
" |
|
1799 |
'hello world' indexOfSeparator |
|
1800 |
'helloworld' indexOfSeparator |
|
1801 |
'hello ' indexOfSeparator |
|
1802 |
' hello' indexOfSeparator |
|
1803 |
" |
|
1804 |
! |
|
1805 |
||
15912 | 1806 |
indexOfSeparatorOrEndStartingAt:startIndex |
1807 |
"return the index of the next whitespace character, |
|
1808 |
starting the search at startIndex, searching forward; |
|
1809 |
return the index of one behond the end of the receiver if no separator was found. |
|
1810 |
To extract the word, copy from startIndex to the returned index-1" |
|
1811 |
||
1812 |
|idx| |
|
1813 |
||
1814 |
idx := self indexOfSeparatorStartingAt:startIndex. |
|
1815 |
idx == 0 ifTrue:[^ self size + 1]. |
|
1816 |
^ idx. |
|
1817 |
||
1818 |
" |
|
1819 |
'hello world' indexOfSeparatorOrEndStartingAt:3 |
|
1820 |
' hello world' indexOfSeparatorOrEndStartingAt:3 |
|
1821 |
'hello world ' indexOfSeparatorOrEndStartingAt:3 |
|
1822 |
'hello world ' indexOfSeparatorOrEndStartingAt:6 |
|
1823 |
'hello world ' indexOfSeparatorOrEndStartingAt:7 |
|
1824 |
'helloworld ' indexOfSeparatorOrEndStartingAt:7 |
|
1825 |
'helloworld' indexOfSeparatorOrEndStartingAt:7 |
|
1826 |
" |
|
1827 |
! |
|
1828 |
||
9192 | 1829 |
indexOfSeparatorStartingAt:startIndex |
1830 |
"return the index of the next whitespace character, |
|
1831 |
starting the search at startIndex, searching forward; |
|
1832 |
return 0 if no separator was found" |
|
1833 |
||
1834 |
|start "{ Class: SmallInteger }" |
|
1835 |
mySize "{ Class: SmallInteger }"| |
|
1836 |
||
1837 |
start := startIndex. |
|
1838 |
mySize := self size. |
|
1839 |
||
1840 |
start to:mySize do:[:index | |
|
16202 | 1841 |
(self at:index) isSeparator ifTrue:[^ index] |
9192 | 1842 |
]. |
1843 |
^ 0 |
|
1844 |
||
1845 |
" |
|
1846 |
'hello world' indexOfSeparatorStartingAt:3 |
|
1847 |
' hello world' indexOfSeparatorStartingAt:3 |
|
8874 | 1848 |
'hello world ' indexOfSeparatorStartingAt:3 |
1849 |
'hello world ' indexOfSeparatorStartingAt:6 |
|
1850 |
'hello world ' indexOfSeparatorStartingAt:7 |
|
1851 |
'helloworld ' indexOfSeparatorStartingAt:7 |
|
1852 |
'helloworld' indexOfSeparatorStartingAt:7 |
|
4117 | 1853 |
" |
2519
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
1854 |
! |
07674cea76dd
allow escaping a * when doing a match
Claus Gittinger <cg@exept.de>
parents:
2452
diff
changeset
|
1855 |
|
4117 | 1856 |
lastIndexOfSeparator |
14150
e13160aa321a
comment/format in: #lastIndexOfSeparator
Claus Gittinger <cg@exept.de>
parents:
14141
diff
changeset
|
1857 |
"return the last index of a whitespace character (space or tab). |
4117 | 1858 |
(i.e. start the search at the end and search backwards); |
1859 |
Returns 0 if no separator is found." |
|
1860 |
||
1861 |
^ self lastIndexOfSeparatorStartingAt:(self size) |
|
1862 |
||
1863 |
" |
|
8874 | 1864 |
'hello world' lastIndexOfSeparator |
1865 |
'helloworld' lastIndexOfSeparator |
|
1866 |
'hel lo wor ld' lastIndexOfSeparator |
|
1867 |
'hel ' lastIndexOfSeparator 6 |
|
4117 | 1868 |
" |
14150
e13160aa321a
comment/format in: #lastIndexOfSeparator
Claus Gittinger <cg@exept.de>
parents:
14141
diff
changeset
|
1869 |
|
e13160aa321a
comment/format in: #lastIndexOfSeparator
Claus Gittinger <cg@exept.de>
parents:
14141
diff
changeset
|
1870 |
"Modified (comment): / 01-06-2012 / 13:10:30 / cg" |
4117 | 1871 |
! |
1872 |
||
1873 |
lastIndexOfSeparatorStartingAt:startIndex |
|
9192 | 1874 |
"return the index of the previous whitespace character, |
1875 |
starting the search at startIndex (and searching backwards); |
|
1876 |
returns 0 if no separator was found" |
|
1877 |
||
1878 |
|start "{ Class: SmallInteger }"| |
|
1879 |
||
1880 |
start := startIndex. |
|
1881 |
||
1882 |
start to:1 by:-1 do:[:index | |
|
16202 | 1883 |
(self at:index) isSeparator ifTrue:[^ index] |
9192 | 1884 |
]. |
1885 |
^ 0 |
|
1886 |
||
1887 |
" |
|
1888 |
'hello world' lastIndexOfSeparatorStartingAt:3 |
|
1889 |
'hello world' lastIndexOfSeparatorStartingAt:7 |
|
1890 |
'helloworld' lastIndexOfSeparatorStartingAt:7 |
|
8874 | 1891 |
' helloworld' lastIndexOfSeparatorStartingAt:7 |
4117 | 1892 |
" |
608 | 1893 |
! ! |
1894 |
||
1895 |
!CharacterArray methodsFor:'comparing'! |
|
1896 |
||
6742
292775fda600
avoid endless recursion when comparing int < string
Claus Gittinger <cg@exept.de>
parents:
6655
diff
changeset
|
1897 |
< aString |
608 | 1898 |
"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
|
1899 |
receiver is greater than the argument. Otherwise return false. |
8874 | 1900 |
This comparison is based on the elements ascii code - |
9192 | 1901 |
i.e. upper/lowercase & upper/lowercase & national characters are NOT treated specially." |
1902 |
||
1903 |
|mySize "{ Class: SmallInteger }" |
|
1904 |
otherSize "{ Class: SmallInteger }" |
|
1905 |
n "{ Class: SmallInteger }" |
|
1906 |
c1 c2| |
|
1907 |
||
1908 |
mySize := self size. |
|
1909 |
otherSize := aString string size. |
|
6742
292775fda600
avoid endless recursion when comparing int < string
Claus Gittinger <cg@exept.de>
parents:
6655
diff
changeset
|
1910 |
n := mySize min:otherSize. |
292775fda600
avoid endless recursion when comparing int < string
Claus Gittinger <cg@exept.de>
parents:
6655
diff
changeset
|
1911 |
|
292775fda600
avoid endless recursion when comparing int < string
Claus Gittinger <cg@exept.de>
parents:
6655
diff
changeset
|
1912 |
1 to:n do:[:index | |
16202 | 1913 |
c1 := self at:index. |
1914 |
c2 := aString at:index. |
|
1915 |
(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
|
1916 |
]. |
292775fda600
avoid endless recursion when comparing int < string
Claus Gittinger <cg@exept.de>
parents:
6655
diff
changeset
|
1917 |
^ mySize < otherSize |
608 | 1918 |
! |
1919 |
||
1920 |
= aString |
|
1921 |
"Compare the receiver with the argument and return true if the |
|
1922 |
receiver is equal to the argument. Otherwise return false. |
|
1252 | 1923 |
|
9191 | 1924 |
This compare does NOT ignore case differences, |
608 | 1925 |
therefore 'foo' = 'Foo' will return false. |
1926 |
Since this is incompatible to ST-80 (at least, V2.x) , this may change." |
|
1927 |
||
14137
1eb1b4a3f364
Inherit from UninterpretedBytes instead of ByteArray
Stefan Vogel <sv@exept.de>
parents:
14122
diff
changeset
|
1928 |
|mySize "{ Class: SmallInteger }"| |
608 | 1929 |
|
5963
23462e06dcff
compare with Texts fixed (should compare equal if strings are)
Claus Gittinger <cg@exept.de>
parents:
5946
diff
changeset
|
1930 |
(aString isString or:[aString species == self species]) ifFalse:[ |
14137
1eb1b4a3f364
Inherit from UninterpretedBytes instead of ByteArray
Stefan Vogel <sv@exept.de>
parents:
14122
diff
changeset
|
1931 |
^ false |
5963
23462e06dcff
compare with Texts fixed (should compare equal if strings are)
Claus Gittinger <cg@exept.de>
parents:
5946
diff
changeset
|
1932 |
]. |
608 | 1933 |
mySize := self size. |
14137
1eb1b4a3f364
Inherit from UninterpretedBytes instead of ByteArray
Stefan Vogel <sv@exept.de>
parents:
14122
diff
changeset
|
1934 |
mySize ~~ (aString size) ifTrue:[^ false]. |
608 | 1935 |
|
1936 |
1 to:mySize do:[:index | |
|
14137
1eb1b4a3f364
Inherit from UninterpretedBytes instead of ByteArray
Stefan Vogel <sv@exept.de>
parents:
14122
diff
changeset
|
1937 |
(self at:index) = (aString at:index) ifFalse:[^ false]. |
608 | 1938 |
]. |
1939 |
^ true |
|
1940 |
||
1941 |
" |
|
8874 | 1942 |
'foo' = 'Foo' |
1943 |
'foo' = 'bar' |
|
1944 |
'foo' = 'foo' |
|
1945 |
'foo' = 'foo' asText |
|
1946 |
'foo' asText = 'foo' |
|
1947 |
'foo' asText = 'foo' asText |
|
608 | 1948 |
" |
1252 | 1949 |
|
1950 |
"Modified: 22.4.1996 / 15:53:58 / cg" |
|
608 | 1951 |
! |
9192 | 1952 |
|
9191 | 1953 |
> aString |
608 | 1954 |
"Compare the receiver with the argument and return true if the |
1955 |
receiver is greater than the argument. Otherwise return false. |
|
8874 | 1956 |
This comparison is based on the elements ascii code - |
1252 | 1957 |
i.e. upper/lowercase & upper/lowercase & national characters are NOT treated specially." |
608 | 1958 |
|
1959 |
|mySize "{ Class: SmallInteger }" |
|
8874 | 1960 |
otherSize "{ Class: SmallInteger }" |
9192 | 1961 |
n "{ Class: SmallInteger }" |
1962 |
c1 c2| |
|
1963 |
||
1964 |
mySize := self size. |
|
5929 | 1965 |
otherSize := aString string size. |
1966 |
n := mySize min:otherSize. |
|
1967 |
||
1968 |
1 to:n do:[:index | |
|
16202 | 1969 |
c1 := self at:index. |
1970 |
c2 := aString at:index. |
|
1971 |
(c1 == c2 or:[c1 = c2]) ifFalse:[^ c1 > c2]. |
|
5929 | 1972 |
]. |
9192 | 1973 |
^ mySize > otherSize |
1974 |
||
1975 |
"Modified: 22.4.1996 / 15:55:00 / cg" |
|
1976 |
! |
|
1977 |
||
14660 | 1978 |
after:aString |
1979 |
"Compare the receiver with the argument and return true if the |
|
1980 |
receiver should come after the argument in a sorted list. |
|
1981 |
Otherwise return false. |
|
1982 |
NOTE: The comparison should be language specific, depending on the value of |
|
1983 |
LC_COLLATE, which is initialized from the environment. |
|
1984 |
||
1985 |
Currently it is for Strings, but not for UnicodeStrings... |
|
1986 |
||
1987 |
STUPID: |
|
1988 |
#after has a completely different meaning in SeqColl ..." |
|
1989 |
||
1990 |
^ (self compareCollatingWith:aString) > 0 |
|
1991 |
! |
|
1992 |
||
15814 | 1993 |
compareAsVersionNumberWith:aStringOrCollection |
1994 |
"Compare the receiver with the argument and return 1 if the receiver is |
|
1995 |
greater, 0 if equal and -1 if less than the argument in a sorted list. |
|
1996 |
Compare as version numbers in the form a.b.c... ." |
|
1997 |
||
1998 |
|rev1 rev2| |
|
1999 |
||
2000 |
rev1 := self asCollectionOfSubstringsSeparatedBy:$.. |
|
2001 |
aStringOrCollection isString ifTrue:[ |
|
2002 |
rev2 := aStringOrCollection asCollectionOfSubstringsSeparatedBy:$.. |
|
2003 |
]. |
|
2004 |
rev1 := rev1 collect:[:each| each asInteger]. |
|
2005 |
rev2 := rev2 collect:[:each| each asInteger]. |
|
2006 |
||
2007 |
^ rev1 compareWith:rev2 |
|
2008 |
||
2009 |
" |
|
2010 |
self assert:('1' compareAsVersionNumberWith:'2') < 0. |
|
2011 |
self assert:('2' compareAsVersionNumberWith:'1') > 0. |
|
2012 |
self assert:('1.1' compareAsVersionNumberWith:'2.1.2') < 0. |
|
2013 |
self assert:('2.1' compareAsVersionNumberWith:'1.2.3') > 0. |
|
2014 |
self assert:('1' compareAsVersionNumberWith:'1.1') < 0. |
|
2015 |
self assert:('1.1' compareAsVersionNumberWith:'1') > 0. |
|
2016 |
self assert:('1.1' compareAsVersionNumberWith:'1.2') < 0. |
|
2017 |
self assert:('1.10' compareAsVersionNumberWith:'1.2') > 0. |
|
2018 |
self assert:('1.2.3.4' compareAsVersionNumberWith:'1.2.3.5') < 0. |
|
2019 |
self assert:('1.2.3.4' compareAsVersionNumberWith:'1.2.3.3') > 0. |
|
2020 |
self assert:('1.2.3.4' compareAsVersionNumberWith:'1.2.3') > 0. |
|
2021 |
self assert:('1.2.3.4' compareAsVersionNumberWith:'1.2.3.4') = 0. |
|
2022 |
self assert:('1.2.3.4' compareAsVersionNumberWith:'01.002.03.004') = 0. |
|
2023 |
self assert:('1.2.3.4' compareAsVersionNumberWith:#(1 2 3 4)) = 0. |
|
2024 |
" |
|
2025 |
! |
|
2026 |
||
9192 | 2027 |
compareCaselessWith:aString |
9190 | 2028 |
"Compare the receiver against the argument, ignoreing case. |
2029 |
Return 1 if the receiver is greater, 0 if equal and -1 if less than the argument. |
|
2030 |
||
8874 | 2031 |
This comparison is based on the elements ascii code - |
9190 | 2032 |
i.e. national characters are NOT treated specially. |
2033 |
'foo' compareWith: 'Foo' will return 0" |
|
608 | 2034 |
|
2035 |
|mySize "{ Class: SmallInteger }" |
|
8874 | 2036 |
otherSize "{ Class: SmallInteger }" |
9192 | 2037 |
n "{ Class: SmallInteger }" |
608 | 2038 |
c1 c2| |
2039 |
||
2040 |
mySize := self size. |
|
5739
4fd3be7ccc75
oops some relational ops did accept a non-string arg
Claus Gittinger <cg@exept.de>
parents:
5732
diff
changeset
|
2041 |
otherSize := aString string size. |
608 | 2042 |
n := mySize min:otherSize. |
2043 |
||
2044 |
1 to:n do:[:index | |
|
16202 | 2045 |
c1 := (self at:index) asLowercase. |
2046 |
c2 := (aString at:index) asLowercase. |
|
2047 |
c1 > c2 ifTrue:[^ 1]. |
|
2048 |
c1 < c2 ifTrue:[^ -1]. |
|
608 | 2049 |
]. |
2050 |
mySize > otherSize ifTrue:[^ 1]. |
|
2051 |
mySize < otherSize ifTrue:[^ -1]. |
|
2052 |
^ 0 |
|
1252 | 2053 |
|
2054 |
"Modified: 22.4.1996 / 15:56:07 / cg" |
|
608 | 2055 |
! |
2056 |
||
14660 | 2057 |
compareCollatingWith:aString |
2058 |
"Compare the receiver with the argument and return 1 if the receiver is |
|
2059 |
greater, 0 if equal and -1 if less than the argument in a sorted list. |
|
2060 |
The comparison is language specific, depending on the value of |
|
2061 |
LC_COLLATE, which is in the shell environment." |
|
2062 |
||
2063 |
"TODO not yet defined for unicode" |
|
2064 |
<resource: #todo> |
|
2065 |
||
2066 |
|s| |
|
2067 |
||
2068 |
(s := self string) ~~ self ifTrue:[ |
|
2069 |
^ s compareCollatingWith:aString |
|
2070 |
]. |
|
2071 |
^ self compareWith:aString |
|
2072 |
! |
|
2073 |
||
9192 | 2074 |
compareWith:aString |
9190 | 2075 |
"Compare the receiver with the argument and return 1 if the receiver is |
2076 |
greater, 0 if equal and -1 if less than the argument. |
|
11861 | 2077 |
This comparison is based on the elements' codepoints - |
9190 | 2078 |
i.e. upper/lowercase & national characters are NOT treated specially. |
2079 |
'foo' compareWith: 'Foo' will return 1. |
|
2080 |
while 'foo' sameAs:'Foo' will return true" |
|
2081 |
||
15814 | 2082 |
|s| |
2083 |
||
2084 |
s := self string. |
|
2085 |
s ~~ self ifTrue:[ |
|
2086 |
^ s compareWith:aString string. |
|
9192 | 2087 |
]. |
15814 | 2088 |
^ super compareWith:aString string. |
9192 | 2089 |
! |
2090 |
||
11861 | 2091 |
endsWith:aStringOrCharacter |
16175 | 2092 |
"return true, if the receiver ends with something, aStringOrCharacter. |
2093 |
If aStringOrCharacter is empty, true is returned" |
|
11861 | 2094 |
|
2095 |
|s| |
|
2096 |
||
2097 |
(s := self string) ~~ self ifTrue:[ |
|
16175 | 2098 |
^ s endsWith:aStringOrCharacter |
11861 | 2099 |
]. |
12912 | 2100 |
(self notEmpty and:[aStringOrCharacter isCharacter]) ifTrue:[ |
16175 | 2101 |
^ self last = aStringOrCharacter |
11861 | 2102 |
]. |
2103 |
^ super endsWith:aStringOrCharacter |
|
2104 |
||
2105 |
" |
|
2106 |
'hello world' endsWith:'world' |
|
2107 |
'hello world' asText allBold endsWith:'world' |
|
16175 | 2108 |
'hello world' endsWith:'' |
2109 |
'hello world' asText allBold endsWith:'' |
|
11861 | 2110 |
" |
2111 |
||
2112 |
"Modified: 12.5.1996 / 15:49:18 / cg" |
|
2113 |
! |
|
2114 |
||
11838 | 2115 |
hammingDistanceTo:aString |
2116 |
"return the hamming distance (the number of characters which are different). |
|
12177 | 2117 |
In information theory, the Hamming distance between two strings of equal length |
2118 |
is the number of positions for which the corresponding symbols are different. |
|
2119 |
Put another way, it measures the minimum number of substitutions required to change |
|
11838 | 2120 |
one into the other, or the number of errors that transformed one string into the other." |
2121 |
||
2122 |
self assert:(aString size == self size). |
|
2123 |
^ 1 to:self size count:[:idx | (self at:idx) ~= (aString at:idx)] |
|
2124 |
||
2125 |
" |
|
2126 |
'roses' hammingDistanceTo:'toned' |
|
2127 |
" |
|
2128 |
! |
|
2129 |
||
9192 | 2130 |
hash |
2131 |
"return an integer useful as a hash-key" |
|
2132 |
||
13879 | 2133 |
"/ immediately after any change, execute (maybe in a debugger): |
2134 |
"/ Set allSubInstancesDo:[:s | s rehash] |
|
14641 | 2135 |
"/ ^ self hash_dragonBook |
14652 | 2136 |
|
2137 |
|h| |
|
2138 |
||
2139 |
"/ |
|
2140 |
"/ this is the sdbm algorithm |
|
2141 |
"/ |
|
2142 |
h := 0. |
|
2143 |
self do:[:char | |
|
2144 |
h := (65599 times:h) plus:char codePoint. |
|
2145 |
]. |
|
2146 |
^ h |
|
13879 | 2147 |
|
2148 |
" |
|
2149 |
'a' hash |
|
2150 |
'a' asUnicode16String hash |
|
2151 |
'aa' hash |
|
2152 |
'aa' asUnicode16String hash |
|
2153 |
'ab' hash |
|
2154 |
'ab' asUnicode16String hash |
|
2155 |
'ab' hash |
|
2156 |
'ab' asArray hash |
|
2157 |
" |
|
2158 |
||
14641 | 2159 |
" |
2160 |
|syms ms| |
|
2161 |
||
2162 |
syms := Symbol allInstances. |
|
2163 |
Transcript show:'syms: '; showCR:syms size. |
|
14652 | 2164 |
Transcript show:'sdbm hashes: '; showCR:(syms collect:[:s| s hash]) asSet size. |
14641 | 2165 |
Transcript show:'dragonBook hashes: '; showCR:(syms collect:[:s| s hash_dragonBook]) asSet size. |
2166 |
||
2167 |
ms := Time millisecondsToRun:[ |
|
2168 |
10 timesRepeat:[ |
|
14652 | 2169 |
syms do:[:each| each hash]. |
14641 | 2170 |
]. |
2171 |
]. |
|
2172 |
Transcript show:'sdbm hash: '; showCR:ms. |
|
2173 |
||
2174 |
ms := Time millisecondsToRun:[ |
|
2175 |
10 timesRepeat:[ |
|
2176 |
syms do:[:each| each hash_dragonBook]. |
|
2177 |
]. |
|
2178 |
]. |
|
2179 |
Transcript show:'dragonBook: '; showCR:ms. |
|
2180 |
||
2181 |
syms := syms collect:[:each| each asUnicode16String]. |
|
2182 |
ms := Time millisecondsToRun:[ |
|
2183 |
10 timesRepeat:[ |
|
14652 | 2184 |
syms do:[:each| each hash]. |
14641 | 2185 |
]. |
2186 |
]. |
|
2187 |
Transcript show:'unicode sdbm hash: '; showCR:ms. |
|
2188 |
||
2189 |
ms := Time millisecondsToRun:[ |
|
2190 |
10 timesRepeat:[ |
|
2191 |
syms do:[:each| each hash_dragonBook]. |
|
2192 |
]. |
|
2193 |
]. |
|
2194 |
Transcript show:'unicode dragonBook:'; showCR:ms. |
|
2195 |
" |
|
2196 |
||
13879 | 2197 |
"Modified: / 26-12-2011 / 14:09:07 / cg" |
2198 |
! |
|
2199 |
||
2200 |
hash_dragonBook |
|
2201 |
"return an integer useful as a hash-key" |
|
2202 |
||
10698
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2203 |
|h g| |
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2204 |
|
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2205 |
"/ |
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2206 |
"/ this is the dragon-book algorithm |
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2207 |
"/ |
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2208 |
h := 0. |
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2209 |
self reverseDo:[:char | |
10703
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
2210 |
"/ 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
|
2211 |
"/ h := (h bitShift:4) + char asciiValue. |
14137
1eb1b4a3f364
Inherit from UninterpretedBytes instead of ByteArray
Stefan Vogel <sv@exept.de>
parents:
14122
diff
changeset
|
2212 |
h := (h bitShift:4). |
14637 | 2213 |
h := h + char codePoint. |
14137
1eb1b4a3f364
Inherit from UninterpretedBytes instead of ByteArray
Stefan Vogel <sv@exept.de>
parents:
14122
diff
changeset
|
2214 |
h := h bitAnd:16rFFFFFFFF. |
1eb1b4a3f364
Inherit from UninterpretedBytes instead of ByteArray
Stefan Vogel <sv@exept.de>
parents:
14122
diff
changeset
|
2215 |
g := (h bitAnd: 16rF0000000). |
1eb1b4a3f364
Inherit from UninterpretedBytes instead of ByteArray
Stefan Vogel <sv@exept.de>
parents:
14122
diff
changeset
|
2216 |
g ~~ 0 ifTrue:[ |
1eb1b4a3f364
Inherit from UninterpretedBytes instead of ByteArray
Stefan Vogel <sv@exept.de>
parents:
14122
diff
changeset
|
2217 |
h := h bitXor:(g bitShift:-24). |
1eb1b4a3f364
Inherit from UninterpretedBytes instead of ByteArray
Stefan Vogel <sv@exept.de>
parents:
14122
diff
changeset
|
2218 |
h := h bitXor:g. |
1eb1b4a3f364
Inherit from UninterpretedBytes instead of ByteArray
Stefan Vogel <sv@exept.de>
parents:
14122
diff
changeset
|
2219 |
]. |
10698
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2220 |
]. |
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2221 |
"/ |
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2222 |
"/ multiply by large prime to spread values |
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2223 |
"/ This speeds up Set and Dictionary by a factor of 10!! |
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2224 |
"/ |
14137
1eb1b4a3f364
Inherit from UninterpretedBytes instead of ByteArray
Stefan Vogel <sv@exept.de>
parents:
14122
diff
changeset
|
2225 |
h := h times:31415821. |
10698
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2226 |
^ h |
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2227 |
|
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2228 |
" |
10703
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
2229 |
'a' hash |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
2230 |
'a' asUnicode16String hash |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
2231 |
'aa' hash |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
2232 |
'aa' asUnicode16String hash |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
2233 |
'ab' hash |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
2234 |
'ab' asUnicode16String hash |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
2235 |
'ab' hash |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
2236 |
'ab' asArray hash |
10698
08bfe7364c0f
oops: Unicode16String-hash returned different value from
sr
parents:
10671
diff
changeset
|
2237 |
" |
13879 | 2238 |
|
2239 |
"Created: / 26-12-2011 / 13:46:06 / cg" |
|
2240 |
! |
|
2241 |
||
16887 | 2242 |
hash_fnv1a |
2243 |
"return an integer useful as a hash-key. |
|
2244 |
This method uses the fnv-1a algorithm |
|
2245 |
(which is actually a very good one)" |
|
2246 |
||
2247 |
|h| |
|
2248 |
||
2249 |
h := 2166136261. |
|
2250 |
self do:[:eachChar | |
|
2251 |
h := h bitXor:(eachChar codePoint). |
|
2252 |
h := (h * 16777619) bitAnd:16rFFFFFFFF. |
|
2253 |
]. |
|
2254 |
^ h |
|
2255 |
||
2256 |
" |
|
2257 |
'abc' hash_fnv1a |
|
2258 |
'foofooHelloWorld' hash_fnv1a |
|
2259 |
'blablaHelloWorld' hash_fnv1a |
|
2260 |
" |
|
2261 |
! |
|
2262 |
||
2263 |
hash_java |
|
2264 |
"return an integer useful as a hash-key. |
|
2265 |
This method uses the same algorithm as used in |
|
2266 |
the java virtual machine |
|
2267 |
(which is actually not a very good one)." |
|
2268 |
||
2269 |
|h| |
|
2270 |
||
2271 |
h := 0. |
|
2272 |
self do:[:eachChar | |
|
2273 |
h := (h * 31) + (eachChar codePoint). |
|
2274 |
h := h bitAnd:16rFFFFFFFF. |
|
2275 |
]. |
|
2276 |
^ h |
|
2277 |
||
2278 |
" |
|
2279 |
'abc' hash_java |
|
2280 |
'foofooHelloWorld' hash_java |
|
2281 |
'blablaHelloWorld' hash_java |
|
2282 |
" |
|
2283 |
! |
|
2284 |
||
11861 | 2285 |
levenshteinTo:aString |
2286 |
"return the levenshtein distance to the argument, aString; |
|
2287 |
this value corresponds to the number of replacements that have to be |
|
2288 |
made to get aString from the receiver. |
|
2289 |
See IEEE transactions on Computers 1976 Pg 172 ff." |
|
2290 |
||
2291 |
" |
|
2292 |
in the following, we assume that ommiting a character |
|
2293 |
is less of an error than inserting an extra character. |
|
2294 |
Therefore the different insertion (i) and deletion (d) values. |
|
15750 | 2295 |
s: substitution weight (4) |
2296 |
k: keyboard weight (k) (typing a nearby key) - or nil (then use s) |
|
2297 |
c: case weight (4) - or nil (then use s) |
|
2298 |
e: exchange weight (8) - or nil (then use s*2) |
|
2299 |
i: insertion of extra character weight (2) |
|
2300 |
d: delete of a character weight (6) |
|
11861 | 2301 |
" |
2302 |
||
2303 |
^ StringUtilities |
|
15750 | 2304 |
levenshteinDistanceFrom:self |
2305 |
to:aString |
|
2306 |
s:4 k:4 c:4 e:nil i:2 d:6 |
|
12177 | 2307 |
|
2308 |
" |
|
2309 |
'computer' levenshteinTo:'computer' |
|
11861 | 2310 |
'cOmputer' levenshteinTo:'computer' |
12177 | 2311 |
'cOmpuTer' levenshteinTo:'computer' |
11861 | 2312 |
'cimputer' levenshteinTo:'computer' |
2313 |
'cumputer' levenshteinTo:'computer' |
|
2314 |
||
2315 |
'cmputer' levenshteinTo:'computer' |
|
2316 |
'coomputer' levenshteinTo:'computer' |
|
2317 |
||
2318 |
'ocmprt' levenshteinTo:'computer' |
|
2319 |
'computer' levenshteinTo:'computer' |
|
2320 |
'ocmputer' levenshteinTo:'computer' |
|
2321 |
'cmputer' levenshteinTo:'computer' |
|
2322 |
'computer' levenshteinTo:'cmputer' |
|
2323 |
'Computer' levenshteinTo:'computer' |
|
2324 |
||
12177 | 2325 |
'compiter' levenshteinTo:'computer' |
2326 |
'compoter' levenshteinTo:'computer' |
|
2327 |
||
2328 |
'comptuer' levenshteinTo:'computer' |
|
11861 | 2329 |
" |
2330 |
! |
|
2331 |
||
2332 |
levenshteinTo:aString s:substWeight k:kbdTypoWeight c:caseWeight i:insrtWeight d:deleteWeight |
|
2333 |
"parametrized levenshtein. |
|
2334 |
return the levenshtein distance to the argument, aString; |
|
2335 |
this value corrensponds to the number of replacements that have to be |
|
2336 |
made to get aString from the receiver. |
|
2337 |
The arguments are the costs for |
|
16202 | 2338 |
s:substitution, |
2339 |
k:keyboard type (substitution), |
|
2340 |
c:case-change, |
|
2341 |
i:insertion |
|
2342 |
d:deletion |
|
11861 | 2343 |
of a character. |
2344 |
See IEEE transactions on Computers 1976 Pg 172 ff" |
|
2345 |
||
2346 |
^ StringUtilities |
|
16202 | 2347 |
levenshteinDistanceFrom:self |
2348 |
to:aString |
|
2349 |
s:substWeight k:kbdTypoWeight c:caseWeight e:nil i:insrtWeight d:deleteWeight |
|
11861 | 2350 |
! |
2351 |
||
9190 | 2352 |
sameAs:aString |
2353 |
"Compare the receiver with the argument like =, but ignore case differences. |
|
2354 |
Return true or false." |
|
2355 |
||
2356 |
|mySize "{ Class: SmallInteger }" |
|
2357 |
otherSize c1 c2| |
|
2358 |
||
9192 | 2359 |
self == aString ifTrue:[^ true]. |
2360 |
||
2361 |
mySize := self size. |
|
2362 |
otherSize := aString string size. |
|
2363 |
mySize == otherSize ifFalse:[^ false]. |
|
2364 |
||
2365 |
1 to:mySize do:[:index | |
|
16202 | 2366 |
c1 := self at:index. |
2367 |
c2 := aString at:index. |
|
2368 |
c1 == c2 ifFalse:[ |
|
2369 |
(c1 sameAs:c2) ifFalse:[^ false]. |
|
2370 |
] |
|
9192 | 2371 |
]. |
2372 |
^ true |
|
2373 |
||
608 | 2374 |
" |
8874 | 2375 |
'foo' sameAs: 'Foo' |
2376 |
'foo' sameAs: 'bar' |
|
2377 |
'foo' sameAs: 'foo' |
|
9192 | 2378 |
" |
2379 |
||
2380 |
"Modified: 22.4.1996 / 15:56:17 / cg" |
|
2381 |
! |
|
608 | 2382 |
|
15734 | 2383 |
sameAs:aString caseSensitive:caseSensitive |
2384 |
"Compare the receiver with the argument. |
|
2385 |
If caseSensitive is false, this is the same as #sameAs:, |
|
2386 |
if false, this is the same as #=." |
|
2387 |
||
2388 |
caseSensitive ifFalse:[ |
|
2389 |
^ self sameAs:aString |
|
2390 |
]. |
|
2391 |
^ self = aString |
|
2392 |
||
2393 |
" |
|
2394 |
'foo' sameAs:'Foo' caseSensitive:false |
|
2395 |
'foo' sameAs:'foo' caseSensitive:true |
|
2396 |
" |
|
2397 |
! |
|
2398 |
||
5242 | 2399 |
sameAs:aString ignoreCase:ignoreCase |
15734 | 2400 |
<resource: #obsolete> |
5242 | 2401 |
"Compare the receiver with the argument. |
2402 |
If ignoreCase is true, this is the same as #sameAs:, |
|
2403 |
if false, this is the same as #=." |
|
2404 |
||
2405 |
ignoreCase ifTrue:[ |
|
16202 | 2406 |
^ self sameAs:aString |
5242 | 2407 |
]. |
2408 |
^ self = aString |
|
2409 |
||
2410 |
" |
|
8874 | 2411 |
'foo' sameAs:'Foo' ignoreCase:false |
2412 |
'foo' sameAs:'foo' ignoreCase:true |
|
5242 | 2413 |
" |
2414 |
||
2415 |
! |
|
2416 |
||
608 | 2417 |
sameCharacters:aString |
9192 | 2418 |
"count & return the number of characters which are the same |
2419 |
(ignoring case and emphasis) in the receiver and the argument, aString." |
|
2420 |
||
2421 |
|n "{ Class: SmallInteger }" |
|
2422 |
c1 c2 cnt| |
|
2423 |
||
2424 |
n := self size. |
|
2425 |
n := n min:(aString string size). |
|
2426 |
||
2427 |
cnt := 0. |
|
2428 |
1 to:n do:[:index | |
|
16202 | 2429 |
c1 := self at:index. |
2430 |
c2 := aString at:index. |
|
2431 |
((c1 == c2) |
|
2432 |
or:[c1 asLowercase = c2 asLowercase]) ifTrue:[ |
|
2433 |
cnt := cnt + 1 |
|
2434 |
] |
|
9192 | 2435 |
]. |
2436 |
^ cnt |
|
2437 |
||
2438 |
" |
|
2439 |
'foobarbaz' sameCharacters: 'foo' |
|
8874 | 2440 |
'foobarbaz' sameCharacters: 'Foo' |
2441 |
'foobarbaz' sameCharacters: 'baz' |
|
608 | 2442 |
" |
5510
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
2443 |
! |
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
2444 |
|
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
2445 |
sameEmphasisAs:aStringOrText |
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
2446 |
"compare the receivers and the arguments emphasis" |
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
2447 |
|
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
2448 |
^ self emphasis = aStringOrText emphasis |
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
2449 |
|
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
2450 |
" |
8874 | 2451 |
'hello' asText sameEmphasisAs: 'hello' |
2452 |
'hello' asText sameEmphasisAs: 'hello' asText |
|
2453 |
'hello' asText allBold sameEmphasisAs: 'hello' |
|
2454 |
'hello' asText allBold sameEmphasisAs: 'fooba' asText allBold |
|
9192 | 2455 |
'hello' asText allBold sameEmphasisAs: 'fooba' asText allItalic |
2456 |
" |
|
2457 |
||
2458 |
||
2459 |
! |
|
2460 |
||
9191 | 2461 |
sameStringAndEmphasisAs:aStringOrText |
5739
4fd3be7ccc75
oops some relational ops did accept a non-string arg
Claus Gittinger <cg@exept.de>
parents:
5732
diff
changeset
|
2462 |
"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
|
2463 |
|
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
2464 |
aStringOrText isString ifFalse:[^ false]. |
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
2465 |
(self string = aStringOrText string) ifFalse:[^ false]. |
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
2466 |
self hasChangeOfEmphasis = aStringOrText hasChangeOfEmphasis ifFalse:[^ false]. |
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
2467 |
^ self emphasis = aStringOrText emphasis |
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
2468 |
|
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
2469 |
" |
8874 | 2470 |
'hello' asText sameEmphasisAs: 'hello' |
2471 |
'hello' asText sameEmphasisAs: 'hello' asText |
|
2472 |
'hello' asText allBold sameEmphasisAs: 'hello' |
|
2473 |
'hello' asText allBold sameEmphasisAs: 'fooba' asText allBold |
|
2474 |
'hello' asText allBold sameEmphasisAs: 'fooba' asText allItalic |
|
2475 |
||
2476 |
'hello' sameEmphasisAs: 'hello' asText |
|
2477 |
'hello' sameEmphasisAs: 'hello' asText allBold |
|
2478 |
'hello' sameEmphasisAs: 'fooba' |
|
2479 |
'hello' sameEmphasisAs: 'fooba' asText |
|
2480 |
'hello' sameEmphasisAs: 'fooba' asText allBold |
|
2481 |
'hello' sameEmphasisAs: 'fooba' asText allItalic |
|
2482 |
||
2483 |
'hello' asText sameStringAndEmphasisAs: 'hello' |
|
2484 |
'hello' asText sameStringAndEmphasisAs: 'hello' asText |
|
2485 |
'hello' asText allBold sameStringAndEmphasisAs: 'hello' |
|
2486 |
'hello' asText allBold sameStringAndEmphasisAs: 'fooba' asText allBold |
|
2487 |
'hello' asText allBold sameStringAndEmphasisAs: 'fooba' asText allItalic |
|
2488 |
||
2489 |
'hello' sameStringAndEmphasisAs: 'hello' asText |
|
2490 |
'hello' sameStringAndEmphasisAs: 'hello' asText allBold |
|
2491 |
'hello' sameStringAndEmphasisAs: 'fooba' |
|
2492 |
'hello' sameStringAndEmphasisAs: 'fooba' asText |
|
2493 |
'hello' sameStringAndEmphasisAs: 'fooba' asText allBold |
|
2494 |
'hello' sameStringAndEmphasisAs: 'fooba' asText allItalic |
|
5510
a261b8f5d66f
moved #sameEmphasisAs: and #sameStringAndEmphasisAs:
Claus Gittinger <cg@exept.de>
parents:
5487
diff
changeset
|
2495 |
" |
11861 | 2496 |
! |
2497 |
||
2498 |
spellAgainst: aString |
|
2499 |
"return an integer between 0 and 100 indicating how similar |
|
2500 |
the argument is to the receiver. No case conversion is done. |
|
2501 |
This algorithm is much simpler (but also less exact) than the |
|
2502 |
levenshtein distance. Experiment which is better for your |
|
2503 |
application." |
|
2504 |
||
2505 |
| i1 "{ Class: SmallInteger }" |
|
2506 |
i2 "{ Class: SmallInteger }" |
|
2507 |
next1 "{ Class: SmallInteger }" |
|
2508 |
next2 "{ Class: SmallInteger }" |
|
2509 |
size1 "{ Class: SmallInteger }" |
|
2510 |
size2 "{ Class: SmallInteger }" |
|
2511 |
score "{ Class: SmallInteger }" |
|
2512 |
maxLen "{ Class: SmallInteger }" | |
|
2513 |
||
2514 |
size1 := self size. |
|
2515 |
size2 := aString size. |
|
2516 |
maxLen := size1 max:size2. |
|
2517 |
score := 0. |
|
2518 |
i1 := i2 := 1. |
|
2519 |
[i1 <= size1 and: [i2 <= size2]] whileTrue:[ |
|
16202 | 2520 |
next1 := i1 + 1. |
2521 |
next2 := i2 + 1. |
|
2522 |
(self at:i1) == (aString at:i2) ifTrue: [ |
|
2523 |
score := score+1. |
|
2524 |
i1 := next1. |
|
2525 |
i2 := next2 |
|
2526 |
] ifFalse: [ |
|
2527 |
(i2 < size2 and: [(self at:i1) == (aString at:next2)]) ifTrue: [ |
|
2528 |
i2 := next2 |
|
2529 |
] ifFalse: [ |
|
2530 |
(i1 < size1 and: [(self at:next1) == (aString at:i2)]) ifTrue: [ |
|
2531 |
i1 := next1 |
|
2532 |
] ifFalse: [ |
|
2533 |
i1 := next1. |
|
2534 |
i2 := next2 |
|
2535 |
] |
|
2536 |
] |
|
2537 |
] |
|
11861 | 2538 |
]. |
2539 |
||
2540 |
score == maxLen ifTrue: [^ 100]. |
|
2541 |
^ 100 * score // maxLen |
|
2542 |
||
2543 |
" |
|
2544 |
'Smalltalk' spellAgainst: 'Smalltlak' |
|
2545 |
'Smalltalk' spellAgainst: 'smalltlak' |
|
2546 |
'Smalltalk' spellAgainst: 'smalltalk' |
|
2547 |
'Smalltalk' spellAgainst: 'smalltlk' |
|
2548 |
'Smalltalk' spellAgainst: 'Smalltolk' |
|
2549 |
" |
|
2550 |
! |
|
2551 |
||
16553 | 2552 |
startsWith:aStringOrCharacter |
2553 |
"return true, if the receiver starts with something, aStringOrCharacter. |
|
11861 | 2554 |
If the argument is empty, true is returned." |
2555 |
||
2556 |
|s| |
|
2557 |
||
16553 | 2558 |
aStringOrCharacter isCharacter ifTrue:[ |
2559 |
^ (self size > 0) and:[ (self at:1) == aStringOrCharacter ] |
|
2560 |
]. |
|
11861 | 2561 |
(s := self string) ~~ self ifTrue:[ |
16553 | 2562 |
^ s startsWith:aStringOrCharacter |
11861 | 2563 |
]. |
16553 | 2564 |
^ super startsWith:aStringOrCharacter |
11861 | 2565 |
|
2566 |
" |
|
2567 |
'hello world' startsWith:'hello' |
|
2568 |
'hello world' asText allBold startsWith:'hello' |
|
2569 |
'hello world' asText allBold startsWith:'' |
|
2570 |
" |
|
2571 |
||
2572 |
"Created: 12.5.1996 / 15:46:40 / cg" |
|
2573 |
"Modified: 12.5.1996 / 15:49:24 / cg" |
|
9192 | 2574 |
! ! |
2575 |
||
2576 |
!CharacterArray methodsFor:'converting'! |
|
2577 |
||
2578 |
asArrayOfSubstrings |
|
2579 |
"return an array of substrings from the receiver, interpreting |
|
2580 |
separators (i.e. spaces & newlines) as word-delimiters. |
|
2581 |
This is a compatibility method - the actual work is done in |
|
2582 |
asCollectionOfWords." |
|
2583 |
||
2584 |
^ self asCollectionOfWords asArray |
|
2585 |
||
2586 |
" |
|
2587 |
'1 one two three four 5 five' asArrayOfSubstrings |
|
2588 |
'1 |
|
2589 |
one |
|
16202 | 2590 |
two three four 5 five' asArrayOfSubstrings |
9192 | 2591 |
" |
2592 |
! |
|
608 | 2593 |
|
11881 | 2594 |
asAsciiZ |
2595 |
"if the receiver does not end with a 0-valued character, return a copy of it, |
|
2596 |
with an additional 0-character. Otherwise return the receiver. This is sometimes |
|
2597 |
needed when a string has to be passed to C, which needs 0-terminated strings. |
|
2598 |
Notice, that all singleByte strings are already 0-terminated in ST/X, whereas wide |
|
2599 |
strings are not." |
|
2600 |
||
12658 | 2601 |
(self notEmpty and:[self last codePoint == 0]) ifTrue:[^ self ]. |
11881 | 2602 |
^ self copyWith:(Character value:0). |
2603 |
||
2604 |
" |
|
2605 |
'abc' asAsciiZ |
|
2606 |
'abc' asWideString asAsciiZ |
|
2607 |
" |
|
2608 |
! |
|
2609 |
||
13524 | 2610 |
asByteArray |
15936 | 2611 |
"depending on the size of the characters in the receiver, |
2612 |
return a byteArray containing single-, double- or even 4-bytewise values. |
|
2613 |
The size of the returned byteArray will be the strings size multiplied by the |
|
2614 |
size required for the largest character. |
|
2615 |
Caveat: better use utf8Encoded, to get reproducable results" |
|
2616 |
||
14555 | 2617 |
|bytes sz bytesPerCharacter idx str| |
2618 |
||
2619 |
str := self string. |
|
2620 |
str ~~ self ifTrue:[ |
|
2621 |
"/ for text and other wrappers |
|
2622 |
^ str asByteArray |
|
13611
ba8c7372218a
variable renamed in: #inspector2TabBytes
Claus Gittinger <cg@exept.de>
parents:
13524
diff
changeset
|
2623 |
]. |
ba8c7372218a
variable renamed in: #inspector2TabBytes
Claus Gittinger <cg@exept.de>
parents:
13524
diff
changeset
|
2624 |
|
ba8c7372218a
variable renamed in: #inspector2TabBytes
Claus Gittinger <cg@exept.de>
parents:
13524
diff
changeset
|
2625 |
"/ for real strings, a fallback |
13524 | 2626 |
sz := self size. |
2627 |
bytesPerCharacter := self bitsPerCharacter // 8. |
|
2628 |
bytes := ByteArray new:(sz * bytesPerCharacter). |
|
2629 |
idx := 1. |
|
2630 |
self do:[:char | |
|
14555 | 2631 |
|code| |
2632 |
||
2633 |
code := char codePoint. |
|
2634 |
bytesPerCharacter == 2 ifTrue:[ |
|
2635 |
bytes unsignedShortAt:idx put:code |
|
2636 |
] ifFalse:[ |
|
2637 |
bytesPerCharacter == 4 ifTrue:[ |
|
2638 |
bytes unsignedLongAt:idx put:code |
|
2639 |
] ifFalse:[ |
|
2640 |
bytes at:idx put:code |
|
2641 |
]. |
|
2642 |
]. |
|
2643 |
idx := idx + bytesPerCharacter. |
|
13524 | 2644 |
]. |
2645 |
^ bytes |
|
2646 |
||
2647 |
"Created: / 27-07-2011 / 00:56:17 / cg" |
|
2648 |
! |
|
2649 |
||
608 | 2650 |
asCollectionOfLines |
8874 | 2651 |
"return a collection containing the lines (separated by cr) |
608 | 2652 |
of the receiver. If multiple cr's occur in a row, the result will |
14456
d7c3e67647f1
comment/format in: #asCollectionOfLines
Claus Gittinger <cg@exept.de>
parents:
14455
diff
changeset
|
2653 |
contain empty strings. |
d7c3e67647f1
comment/format in: #asCollectionOfLines
Claus Gittinger <cg@exept.de>
parents:
14455
diff
changeset
|
2654 |
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
|
2655 |
See also #asCollectionOfLinesWithReturn |
d7c3e67647f1
comment/format in: #asCollectionOfLines
Claus Gittinger <cg@exept.de>
parents:
14455
diff
changeset
|
2656 |
(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
|
2657 |
this method and we did not want to risk any incompatibilities)" |
14455
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
2658 |
|
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
2659 |
^ self asCollectionOfSubstringsSeparatedBy:Character cr. |
608 | 2660 |
|
2661 |
" |
|
2662 |
'1 one\2 two\3 three\4 four\5 five' withCRs asCollectionOfLines |
|
8874 | 2663 |
'1 one\\\\2 two\3 three' withCRs asCollectionOfLines |
608 | 2664 |
" |
2665 |
! |
|
2666 |
||
14455
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
2667 |
asCollectionOfLinesWithReturn |
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
2668 |
"return a collection containing the lines (separated by cr) |
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
2669 |
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
|
2670 |
contain empty strings." |
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
2671 |
|
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
2672 |
|lines| |
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
2673 |
|
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
2674 |
lines := self asCollectionOfSubstringsSeparatedBy:Character cr. |
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
2675 |
lines last isEmpty ifTrue:[ |
15167
62b89f82633c
Changed usage of deprecated #copyWithoutLast: to #copyButLast:
Stefan Vogel <sv@exept.de>
parents:
15055
diff
changeset
|
2676 |
^ lines copyButLast:1 |
14455
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
2677 |
]. |
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
2678 |
^ lines |
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
2679 |
|
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
2680 |
" |
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
2681 |
'1\2\3' withCRs asCollectionOfLines |
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
2682 |
'1\2\3\' withCRs asCollectionOfLines |
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
2683 |
'1\2\3' withCRs asCollectionOfLinesWithReturn |
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
2684 |
'1\2\3\' withCRs asCollectionOfLinesWithReturn |
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
2685 |
" |
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
2686 |
! |
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
2687 |
|
608 | 2688 |
asCollectionOfSubstringsSeparatedBy:aCharacter |
15740 | 2689 |
"return a collection containing substrings (separated by aCharacter) |
2690 |
of the receiver. |
|
2691 |
If aCharacter occurs multiple times in a row, the result will contain empty strings. |
|
2692 |
If the receiver ends with aCharacter, an empty string with be the last result element." |
|
608 | 2693 |
|
9192 | 2694 |
^ self asCollectionOfSubCollectionsSeparatedBy:aCharacter |
2695 |
||
2696 |
" |
|
15740 | 2697 |
'1 one:2 two:3 three:4 four:5 five' asCollectionOfSubstringsSeparatedBy:$: |
2698 |
'1 one:2 two:3 three:4 four:5 five:' asCollectionOfSubstringsSeparatedBy:$: |
|
2699 |
'1 one 2 two 3 three 4 four 5 five' asCollectionOfSubstringsSeparatedBy:Character space |
|
9192 | 2700 |
" |
2701 |
! |
|
608 | 2702 |
|
11828 | 2703 |
asCollectionOfSubstringsSeparatedBy:aCharacter exceptIn:ch |
15740 | 2704 |
"return a collection containing the substrings (separated by aCharacter) |
12177 | 2705 |
of the receiver. If aCharacter occurs multiple times in a row, |
15740 | 2706 |
the result will contain empty strings. |
2707 |
The separation is not done, inside a matching pair of ch-substrings. |
|
2708 |
Can be used to tokenize csv-like strings, which may or may not be enclosed in quotes." |
|
11828 | 2709 |
|
2710 |
|lines myClass except i c |
|
2711 |
startIndex "{ Class:SmallInteger }" |
|
2712 |
stopIndex "{ Class:SmallInteger }" | |
|
2713 |
||
11905
cbc2191cfe2d
return stringCollections from asCollectionOfXXX
Claus Gittinger <cg@exept.de>
parents:
11904
diff
changeset
|
2714 |
lines := StringCollection new. |
11828 | 2715 |
myClass := self species. |
2716 |
||
2717 |
startIndex := 1. |
|
2718 |
except := false. |
|
2719 |
[ |
|
14455
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
2720 |
i := startIndex-1. |
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
2721 |
[ |
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
2722 |
i := i+1. |
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
2723 |
c := self at:i. |
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
2724 |
c = ch ifTrue:[ except := except not. ]. |
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
2725 |
i < self size and:[except or:[c ~= aCharacter]] |
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
2726 |
] whileTrue. |
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
2727 |
|
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
2728 |
c = aCharacter ifTrue:[ |
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
2729 |
stopIndex := i -1. |
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
2730 |
] ifFalse: [ |
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
2731 |
stopIndex := i. |
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
2732 |
]. |
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
2733 |
(stopIndex < startIndex) ifTrue: [ |
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
2734 |
lines add:(myClass new:0) |
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
2735 |
] ifFalse: [ |
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
2736 |
lines add:(self copyFrom:startIndex to:stopIndex) |
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
2737 |
]. |
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
2738 |
startIndex := stopIndex + 2. |
ee8809bade6b
added: #asCollectionOfLinesWithReturn
Claus Gittinger <cg@exept.de>
parents:
14452
diff
changeset
|
2739 |
startIndex <= self size |
11828 | 2740 |
] whileTrue. |
2741 |
^ lines |
|
2742 |
||
2743 |
" |
|
12177 | 2744 |
'asd''f;d''dd;s' asCollectionOfSubstringsSeparatedBy:$; exceptIn:$' |
2745 |
" |
|
2746 |
"/ 'asd "hello bla" foo "bla bla" bar' asCollectionOfSubstringsSeparatedBy:$ exceptIn:$" |
|
11828 | 2747 |
! |
2748 |
||
13344
ec6860c49990
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13343
diff
changeset
|
2749 |
asCollectionOfSubstringsSeparatedBy:aFieldSeparatorString textSeparator:aTextSeparatorOrNil |
13343
3f058ad0d2f1
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13332
diff
changeset
|
2750 |
"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
|
2751 |
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
|
2752 |
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
|
2753 |
|
13344
ec6860c49990
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13343
diff
changeset
|
2754 |
|aTextSeparatorChar items scanningWord inStream element lastIsFieldSeparator sz| |
ec6860c49990
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13343
diff
changeset
|
2755 |
|
ec6860c49990
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13343
diff
changeset
|
2756 |
aTextSeparatorOrNil isNil ifTrue:[ |
15431 | 2757 |
^ self asCollectionOfSubstringsSeparatedByAll: aFieldSeparatorString |
13327
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
2758 |
]. |
13344
ec6860c49990
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13343
diff
changeset
|
2759 |
sz := aTextSeparatorOrNil size. |
ec6860c49990
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13343
diff
changeset
|
2760 |
sz = 0 ifTrue:[ |
15431 | 2761 |
aTextSeparatorChar := aTextSeparatorOrNil |
13344
ec6860c49990
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13343
diff
changeset
|
2762 |
] ifFalse:[sz = 1 ifTrue:[ |
15431 | 2763 |
"this is a String. Fetch the first character - compatibility to older expecco libs" |
2764 |
aTextSeparatorChar := aTextSeparatorOrNil first. |
|
13344
ec6860c49990
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13343
diff
changeset
|
2765 |
] ifFalse:[ |
15431 | 2766 |
self error:'textSeparatoSize > 1'. |
13344
ec6860c49990
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13343
diff
changeset
|
2767 |
]]. |
13327
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
2768 |
|
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
2769 |
items := OrderedCollection new. |
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
2770 |
|
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
2771 |
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
|
2772 |
[ |
15431 | 2773 |
inStream skipSeparators. |
2774 |
inStream atEnd |
|
13327
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
2775 |
] whileFalse:[ |
15431 | 2776 |
lastIsFieldSeparator := false. |
2777 |
inStream peek == aTextSeparatorChar ifTrue:[ |
|
2778 |
inStream next. |
|
2779 |
element := ''. |
|
2780 |
scanningWord := true. |
|
2781 |
[ scanningWord and:[inStream atEnd not] ] whileTrue:[ |
|
2782 |
element := element , (inStream upTo:aTextSeparatorChar). |
|
2783 |
(inStream peek == aTextSeparatorChar) ifTrue:[ |
|
2784 |
element := element , aTextSeparatorChar . |
|
2785 |
inStream next. |
|
2786 |
] ifFalse:[ |
|
2787 |
scanningWord := false. |
|
2788 |
]. |
|
2789 |
]. |
|
16200 | 2790 |
inStream upToAll_positionBefore:aFieldSeparatorString. |
15431 | 2791 |
] ifFalse:[ |
16200 | 2792 |
element := inStream upToAll_positionBefore:aFieldSeparatorString |
15431 | 2793 |
]. |
2794 |
items add:element. |
|
2795 |
lastIsFieldSeparator := (inStream skipThroughAll:aFieldSeparatorString) notNil. |
|
13343
3f058ad0d2f1
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13332
diff
changeset
|
2796 |
]. |
3f058ad0d2f1
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13332
diff
changeset
|
2797 |
lastIsFieldSeparator ifTrue:[ |
15431 | 2798 |
"empty element at the end of the line" |
2799 |
items add:''. |
|
13327
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
2800 |
]. |
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
2801 |
|
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
2802 |
^ items |
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
2803 |
|
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
2804 |
" |
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
2805 |
self assert:(('#First#, #Second,SecondAdd#, #Third#' asCollectionOfSubstringsSeparatedBy:',' textSeparator: $#) |
15431 | 2806 |
sameContentsAs:#('First' 'Second,SecondAdd' 'Third')). |
13327
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
2807 |
self assert:(('#Fir##st#, #Second,SecondAdd#, #Third#' asCollectionOfSubstringsSeparatedBy:',' textSeparator: $#) |
15431 | 2808 |
sameContentsAs:#('Fir#st' 'Second,SecondAdd' 'Third')). |
13327
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
2809 |
self assert:(('#Fir##st#, Second,SecondAdd, #Third#' asCollectionOfSubstringsSeparatedBy:',' textSeparator: $#) |
15431 | 2810 |
sameContentsAs:#('Fir#st' 'Second' 'SecondAdd' 'Third')). |
13343
3f058ad0d2f1
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13332
diff
changeset
|
2811 |
self assert:(('First,Second,Third,,' asCollectionOfSubstringsSeparatedBy:',' textSeparator:nil) |
15431 | 2812 |
sameContentsAs:#('First' 'Second' 'Third' '' '')). |
13344
ec6860c49990
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Stefan Vogel <sv@exept.de>
parents:
13343
diff
changeset
|
2813 |
self assert:(('First,Second,Third,,' asCollectionOfSubstringsSeparatedBy:',' textSeparator:'#') |
15431 | 2814 |
sameContentsAs:#('First' 'Second' 'Third' '' '')). |
13327
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
2815 |
" |
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
2816 |
|
9bfd3ddd5110
changed: #asCollectionOfSubstringsSeparatedBy:textSeparator:
Claus Gittinger <cg@exept.de>
parents:
13310
diff
changeset
|
2817 |
"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
|
2818 |
! |
999589445aa7
add new method for splitting a csv string into csv elements
ab
parents:
10437
diff
changeset
|
2819 |
|
7339
20969b5770c9
asCollectionOfSubstringsSeparatedByAll:
Claus Gittinger <cg@exept.de>
parents:
7318
diff
changeset
|
2820 |
asCollectionOfSubstringsSeparatedByAll:aSeparatorString |
8874 | 2821 |
"return a collection containing the lines (separated by aSeparatorString) |
2822 |
of the receiver. If aSeparatorString occurs multiple times in a row, |
|
7339
20969b5770c9
asCollectionOfSubstringsSeparatedByAll:
Claus Gittinger <cg@exept.de>
parents:
7318
diff
changeset
|
2823 |
the result will contain empty strings." |
20969b5770c9
asCollectionOfSubstringsSeparatedByAll:
Claus Gittinger <cg@exept.de>
parents:
7318
diff
changeset
|
2824 |
|
8852
9f5984eff2c2
use asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8794
diff
changeset
|
2825 |
^ self asCollectionOfSubCollectionsSeparatedByAll:aSeparatorString |
9f5984eff2c2
use asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8794
diff
changeset
|
2826 |
|
9f5984eff2c2
use asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8794
diff
changeset
|
2827 |
" |
8874 | 2828 |
'1::2::3::4::5::' asCollectionOfSubstringsSeparatedByAll:'::' |
7339
20969b5770c9
asCollectionOfSubstringsSeparatedByAll:
Claus Gittinger <cg@exept.de>
parents:
7318
diff
changeset
|
2829 |
" |
20969b5770c9
asCollectionOfSubstringsSeparatedByAll:
Claus Gittinger <cg@exept.de>
parents:
7318
diff
changeset
|
2830 |
! |
20969b5770c9
asCollectionOfSubstringsSeparatedByAll:
Claus Gittinger <cg@exept.de>
parents:
7318
diff
changeset
|
2831 |
|
608 | 2832 |
asCollectionOfSubstringsSeparatedByAny:aCollectionOfSeparators |
2833 |
"return a collection containing the words (separated by any character |
|
2834 |
from aCollectionOfSeparators) of the receiver. |
|
2835 |
This allows breaking up strings using any character as separator." |
|
2836 |
||
8852
9f5984eff2c2
use asCollectionOfSubCollectionsSeparatedBy*
Claus Gittinger <cg@exept.de>
parents:
8794
diff
changeset
|
2837 |
^ self asCollectionOfSubCollectionsSeparatedByAny:aCollectionOfSeparators |
608 | 2838 |
|
2839 |
" |
|
2840 |
'hello:world:isnt:this nice' asCollectionOfSubstringsSeparatedByAny:#($:) |
|
8874 | 2841 |
'hello:world:isnt:this nice' asCollectionOfSubstringsSeparatedByAny:':' |
2842 |
'hello:world:isnt:this nice' asCollectionOfSubstringsSeparatedByAny:(Array with:$: with:Character space) |
|
2843 |
'hello:world:isnt:this nice' asCollectionOfSubstringsSeparatedByAny:': ' |
|
2844 |
'h1e2l3l4o' asCollectionOfSubstringsSeparatedByAny:($1 to: $9) |
|
608 | 2845 |
" |
2846 |
! |
|
2847 |
||
2848 |
asCollectionOfWords |
|
8874 | 2849 |
"return a collection containing the words (separated by whitespace) |
2850 |
of the receiver. Multiple occurrences of whitespace characters will |
|
608 | 2851 |
be treated like one - i.e. whitespace is skipped." |
2852 |
||
7797 | 2853 |
|words| |
608 | 2854 |
|
11905
cbc2191cfe2d
return stringCollections from asCollectionOfXXX
Claus Gittinger <cg@exept.de>
parents:
11904
diff
changeset
|
2855 |
words := StringCollection new. |
7797 | 2856 |
self asCollectionOfWordsDo:[:w | words add:w]. |
608 | 2857 |
^ words |
2858 |
||
2859 |
" |
|
2860 |
'hello world isnt this nice' asCollectionOfWords |
|
2861 |
' hello world isnt this nice ' asCollectionOfWords |
|
2862 |
'hello' asCollectionOfWords |
|
2863 |
'' asCollectionOfWords |
|
2864 |
' ' asCollectionOfWords |
|
10703
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
2865 |
' foo bar__baz__bla__ bar ' asCollectionOfWords |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
2866 |
' foo __bar__baz__bla__ bar ' asCollectionOfWords |
608 | 2867 |
" |
9192 | 2868 |
! |
2869 |
||
2870 |
asCollectionOfWordsDo:aBlock |
|
8874 | 2871 |
"evaluate aBlock for each word (separated by whitespace) of the receiver. |
2872 |
Multiple occurrences of whitespace characters will be treated like one |
|
7797 | 2873 |
- i.e. whitespace is skipped. |
2874 |
Returns the number of words (i.e. the number of invocations of aBlock)." |
|
2875 |
||
2876 |
|count "{ Class:SmallInteger }" |
|
8874 | 2877 |
start "{ Class:SmallInteger }" |
2878 |
stop "{ Class:SmallInteger }" |
|
7797 | 2879 |
mySize "{ Class:SmallInteger }"| |
2880 |
||
2881 |
count := 0. |
|
2882 |
start := 1. |
|
2883 |
mySize := self size. |
|
2884 |
[start <= mySize] whileTrue:[ |
|
16202 | 2885 |
start := self indexOfNonSeparatorStartingAt:start. |
2886 |
start == 0 ifTrue:[ |
|
2887 |
^ count |
|
2888 |
]. |
|
2889 |
stop := self indexOfSeparatorStartingAt:start. |
|
2890 |
stop == 0 ifTrue:[ |
|
2891 |
aBlock value:(self copyFrom:start to:mySize). |
|
2892 |
^ count + 1 |
|
2893 |
]. |
|
2894 |
aBlock value:(self copyFrom:start to:(stop - 1)). |
|
2895 |
start := stop. |
|
2896 |
count := count + 1 |
|
7797 | 2897 |
]. |
2898 |
^ count |
|
2899 |
||
2900 |
" |
|
2901 |
'hello world isnt this nice' asCollectionOfWordsDo:[:w | Transcript showCR:w] |
|
2902 |
' hello world isnt this nice ' asCollectionOfWordsDo:[:w | Transcript showCR:w] |
|
2903 |
'hello' asCollectionOfWordsDo:[:w | Transcript showCR:w] |
|
2904 |
'' asCollectionOfWordsDo:[:w | Transcript showCR:w] |
|
2905 |
' ' asCollectionOfWordsDo:[:w | Transcript showCR:w] |
|
2906 |
" |
|
9192 | 2907 |
! |
2908 |
||
2909 |
asFilename |
|
2910 |
"return a Filename with pathname taken from the receiver" |
|
2911 |
||
2912 |
^ Filename named:self "(self asSingleByteStringReplaceInvalidWith:$?)" |
|
2913 |
||
2914 |
"Modified: 20.5.1996 / 09:38:15 / cg" |
|
2915 |
! |
|
608 | 2916 |
|
3060 | 2917 |
asFixedPoint |
2918 |
"read a fixedPoint number from the receiver. |
|
8874 | 2919 |
Notice, that errors may occur during the read, |
7423 | 2920 |
so you better setup some signal handler when using this method." |
3060 | 2921 |
|
2922 |
^ FixedPoint readFromString:self |
|
2923 |
||
2924 |
" |
|
8874 | 2925 |
'0.123' asFixedPoint |
2926 |
'12345' asFixedPoint |
|
3060 | 2927 |
'(1/5)' asFixedPoint |
2928 |
'foo' asFixedPoint |
|
9192 | 2929 |
Object errorSignal handle:[:ex | ex return:0] do:['foo' asFixedPoint] |
2930 |
" |
|
2931 |
||
2932 |
"Modified: / 25.10.1997 / 15:19:00 / cg" |
|
2933 |
! |
|
2934 |
||
2935 |
asFixedPoint:scale |
|
3060 | 2936 |
"read a fixedPoint number with scale number of post-decimal digits |
2937 |
from the receiver. Scale controls the number of displayed digits, |
|
2938 |
not the number of actually valid digits. |
|
8874 | 2939 |
Notice, that errors may occur during the read, |
7423 | 2940 |
so you better setup some signal handler when using this method." |
3060 | 2941 |
|
2942 |
^ (FixedPoint readFromString:self) scale:scale |
|
2943 |
||
2944 |
" |
|
8874 | 2945 |
'0.123' asFixedPoint:2 |
2946 |
'123456' asFixedPoint:2 |
|
2947 |
('3.14157' asFixedPoint:1) asFixedPoint:5 |
|
2948 |
'3.14157' asFixedPoint:2 |
|
2949 |
'foo' asFixedPoint:2 |
|
3060 | 2950 |
" |
2951 |
||
2952 |
"Modified: / 25.10.1997 / 15:21:57 / cg" |
|
9192 | 2953 |
! |
2954 |
||
2955 |
asFloat |
|
608 | 2956 |
"read a float number from the receiver. |
8874 | 2957 |
Notice, that errors may occur during the read, |
7422 | 2958 |
so you better setup some exception handler when using this method." |
608 | 2959 |
|
2960 |
^ (Number readFromString:self) asFloat |
|
2961 |
||
2962 |
" |
|
8874 | 2963 |
'0.123' asFloat |
608 | 2964 |
'12345' asFloat |
2965 |
'(1/5)' asFloat |
|
8874 | 2966 |
Object errorSignal handle:[:ex | ex return:0] do:['foo' asFloat] |
608 | 2967 |
" |
2968 |
! |
|
2969 |
||
2970 |
asInteger |
|
9190 | 2971 |
"convert the receiver into an integer. |
8874 | 2972 |
Notice, that errors may occur during the read, |
9190 | 2973 |
so you better setup some exception handler when using this method. |
2974 |
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
|
2975 |
in other smalltalks. |
9190 | 2976 |
For less strict integer reading, use Integer readFrom:aString" |
608 | 2977 |
|
2978 |
^ Integer readFromString:self |
|
2979 |
||
2980 |
" |
|
2981 |
'12345678901234567890' asInteger |
|
2982 |
'-1234' asInteger |
|
9190 | 2983 |
|
2984 |
The following raises an error: |
|
16202 | 2985 |
'0.123' asInteger <- reader finds more after reading 0 |
9190 | 2986 |
|
2987 |
whereas the less strict readFrom does not: |
|
16202 | 2988 |
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
|
2989 |
|
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
2990 |
'0.123' asInteger |
608 | 2991 |
'0.123' asNumber <- returns what you expect |
8874 | 2992 |
Object errorSignal handle:[:ex | ex return:0] do:['foo' asInteger] |
9190 | 2993 |
|
2994 |
" |
|
9192 | 2995 |
! |
2996 |
||
2997 |
asLowercase |
|
608 | 2998 |
"return a copy of myself in lowercase letters" |
2999 |
||
8003
6e690f4b79f1
asLowercase/asUppercase and friends care for 16-bit chars
Claus Gittinger <cg@exept.de>
parents:
7995
diff
changeset
|
3000 |
|newStr c bitsPerCharacter |
9192 | 3001 |
mySize "{ Class: SmallInteger }" | |
3002 |
||
3003 |
mySize := self size. |
|
3004 |
mySize == 0 ifTrue:[^ self]. |
|
3005 |
newStr := self species new:mySize. |
|
3006 |
bitsPerCharacter := newStr bitsPerCharacter. |
|
3007 |
||
3008 |
1 to:mySize do:[:i | |
|
14555 | 3009 |
c := (self at:i) asLowercase. |
3010 |
(c bitsPerCharacter > bitsPerCharacter |
|
3011 |
and:[c stringSpecies ~= newStr stringSpecies]) ifTrue:[ |
|
3012 |
newStr := c stringSpecies fromString:newStr. |
|
3013 |
]. |
|
3014 |
newStr at:i put:c |
|
9192 | 3015 |
]. |
3016 |
^ newStr |
|
3017 |
||
3018 |
" |
|
3019 |
'HelloWorld' asLowercase |
|
3020 |
'HelloWorld' asLowercaseFirst |
|
3021 |
" |
|
3022 |
! |
|
3023 |
||
3024 |
asLowercaseFirst |
|
3025 |
"return a copy of myself where the first character is converted to lowercase." |
|
3026 |
||
3027 |
|newString firstChar firstCharAsLowercase| |
|
3028 |
||
11525 | 3029 |
self isEmpty ifTrue:[^ self]. |
9192 | 3030 |
firstChar := (self at:1). |
3031 |
firstCharAsLowercase := firstChar asLowercase. |
|
3032 |
firstChar == firstCharAsLowercase ifTrue:[ ^ self]. |
|
3033 |
||
3034 |
firstCharAsLowercase bitsPerCharacter > self bitsPerCharacter ifTrue:[ |
|
16202 | 3035 |
newString := firstCharAsLowercase stringSpecies fromString:self. |
9192 | 3036 |
] ifFalse:[ |
16202 | 3037 |
newString := self stringSpecies fromString:self. |
9192 | 3038 |
]. |
9190 | 3039 |
newString at:1 put:firstCharAsLowercase. |
3040 |
^ newString |
|
608 | 3041 |
|
3042 |
" |
|
8874 | 3043 |
'HelloWorld' asLowercase |
3044 |
'HelloWorld' asLowercaseFirst |
|
608 | 3045 |
" |
3046 |
! |
|
3047 |
||
11828 | 3048 |
asLowercaseLast |
3049 |
"return a copy of myself where the last character is |
|
3050 |
converted to lowercase." |
|
3051 |
||
3052 |
|newString sz| |
|
3053 |
||
3054 |
sz := self size. |
|
3055 |
newString := self copyFrom:1 to:sz. |
|
3056 |
sz > 0 ifTrue:[ |
|
16202 | 3057 |
newString at:sz put:(newString at:sz) asLowercase |
11828 | 3058 |
]. |
3059 |
^ newString |
|
3060 |
||
3061 |
" |
|
12177 | 3062 |
'HelloWorld' asLowercase |
3063 |
'HelloWorlD' asLowercaseLast |
|
11828 | 3064 |
" |
3065 |
! |
|
3066 |
||
9190 | 3067 |
asNumber |
3068 |
"read a number from the receiver. |
|
3069 |
Notice, that (in contrast to ST-80) errors may occur during the read, |
|
3070 |
so you better setup some signal handler when using this method. |
|
3071 |
Also notice, that this is meant to read end-user numbers from a string; |
|
9135 | 3072 |
it does not handle smalltalk numbers (i.e. radix). |
3073 |
To read a smalltalk number, use Number >> readSmalltalkFrom:. |
|
608 | 3074 |
This may change if ANSI specifies it." |
3075 |
||
3076 |
"/ ST-80 behavior: |
|
3077 |
"/ ^ Number readFromString:self onError:0 |
|
3078 |
||
3079 |
^ Number readFromString:self |
|
3080 |
||
3081 |
" |
|
3082 |
'123' asNumber |
|
3083 |
'123.567' asNumber |
|
3084 |
'(5/6)' asNumber |
|
3085 |
'foo' asNumber |
|
8874 | 3086 |
Object errorSignal handle:[:ex | ex returnWith:0] do:['foo' asNumber] |
608 | 3087 |
" |
9192 | 3088 |
! |
3089 |
||
3090 |
asNumberFromFormatString:ignored |
|
608 | 3091 |
"read a number from the receiver, ignoring any nonDigit characters. |
3092 |
This is typically used to convert from strings which include |
|
3093 |
dollar-signs or millenium digits. However, this method also ignores |
|
3094 |
the decimal point (if any) and therefore should be used with care." |
|
3095 |
||
3096 |
|tempString| |
|
3097 |
||
3098 |
tempString := self collect:[:char | char isDigit]. |
|
3099 |
^ Number readFromString:tempString onError:0 |
|
3100 |
||
3101 |
" |
|
3102 |
'USD 123' asNumberFromFormatString:'foo' |
|
3103 |
'DM 123' asNumberFromFormatString:'foo' |
|
3104 |
'123' asNumberFromFormatString:'foo' |
|
3105 |
'123.567' asNumberFromFormatString:'foo' |
|
3106 |
'(5/6)' asNumberFromFormatString:'foo' |
|
3107 |
'foo' asNumberFromFormatString:'foo' |
|
3108 |
" |
|
9192 | 3109 |
! |
3110 |
||
3111 |
asSingleByteString |
|
3112 |
"return the receiver converted to a 'normal' string. |
|
11904 | 3113 |
Raises an error if unrepresentable characters are encountered. |
3114 |
See also: asSingleByteStringIfPossible and asSingleByteStringReplaceInvalidWith:" |
|
9192 | 3115 |
|
3116 |
^ String fromString:self |
|
11904 | 3117 |
|
3118 |
" |
|
12177 | 3119 |
(Unicode16String with:(Character value:16rFF)) asSingleByteString |
11904 | 3120 |
(Unicode16String with:(Character value:16rFFFF)) asSingleByteString |
3121 |
" |
|
9192 | 3122 |
! |
608 | 3123 |
|
7977 | 3124 |
asSingleByteStringIfPossible |
3125 |
"if possible, return the receiver converted to a 'normal' string. |
|
3126 |
It is only possible, if there are no characters with codePoints above 255 in the receiver." |
|
3127 |
||
14555 | 3128 |
self isWideString ifFalse:[^ self]. |
8102
e0537422e2d3
Use the ANSI-blessed #codePoint instead of deprecated #asciiValue
Stefan Vogel <sv@exept.de>
parents:
8083
diff
changeset
|
3129 |
(self contains:[:char | char codePoint > 255]) ifFalse:[^ self asSingleByteString]. |
9192 | 3130 |
^ self |
3131 |
||
3132 |
" |
|
3133 |
'hello' asSingleByteStringIfPossible |
|
3134 |
'hello' asUnicodeString asSingleByteStringIfPossible |
|
3135 |
" |
|
3136 |
! |
|
3137 |
||
3138 |
asSingleByteStringReplaceInvalidWith:replacementCharacter |
|
3139 |
"return the receiver converted to a 'normal' string, |
|
3140 |
with invalid characters replaced by replacementCharacter. |
|
3141 |
Can be used to convert from 16-bit strings to 8-bit strings |
|
3142 |
and replace characters above code-255 with some replacement." |
|
3143 |
||
3144 |
|newString| |
|
3145 |
||
3146 |
newString := String new:(self size). |
|
3147 |
1 to:self size do:[:idx | |
|
16202 | 3148 |
|char| |
3149 |
||
3150 |
char := self at:idx. |
|
3151 |
char codePoint <= 16rFF ifTrue:[ |
|
3152 |
newString at:idx put:char |
|
3153 |
] ifFalse:[ |
|
3154 |
newString at:idx put:replacementCharacter |
|
3155 |
]. |
|
9190 | 3156 |
]. |
3157 |
^ newString |
|
3158 |
||
3159 |
"Created: 30.6.1997 / 13:02:14 / cg" |
|
7977 | 3160 |
! |
3161 |
||
9192 | 3162 |
asString |
3163 |
"return myself - I am a string" |
|
3164 |
||
3165 |
^ self |
|
3166 |
! |
|
3167 |
||
3168 |
asStringCollection |
|
3169 |
"return a collection of lines from myself." |
|
3170 |
||
3171 |
^ StringCollection fromString:self "string" |
|
3172 |
||
3173 |
"Modified: 13.5.1996 / 20:36:59 / cg" |
|
3174 |
! |
|
1375
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
3175 |
|
1434 | 3176 |
asSymbol |
9230 | 3177 |
"Return a unique symbol with the name taken from the receivers characters. |
8049
ccd0e8f26439
printing - migrating towards unicodeSupport in ST-code
Claus Gittinger <cg@exept.de>
parents:
8040
diff
changeset
|
3178 |
The receiver must be a singleByte-String. |
ccd0e8f26439
printing - migrating towards unicodeSupport in ST-code
Claus Gittinger <cg@exept.de>
parents:
8040
diff
changeset
|
3179 |
TwoByte- and FourByteSymbols are (currently ?) not allowed." |
ccd0e8f26439
printing - migrating towards unicodeSupport in ST-code
Claus Gittinger <cg@exept.de>
parents:
8040
diff
changeset
|
3180 |
|
ccd0e8f26439
printing - migrating towards unicodeSupport in ST-code
Claus Gittinger <cg@exept.de>
parents:
8040
diff
changeset
|
3181 |
|str| |
ccd0e8f26439
printing - migrating towards unicodeSupport in ST-code
Claus Gittinger <cg@exept.de>
parents:
8040
diff
changeset
|
3182 |
|
9192 | 3183 |
str := self string. |
3184 |
str ~~ self ifTrue:[ ^ str asSymbol ]. |
|
3185 |
^ self asSingleByteString asSymbol |
|
3186 |
! |
|
3187 |
||
3188 |
asSymbolIfInterned |
|
10703
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
3189 |
"If a symbol with the receivers characters is already known, return it. Otherwise, return nil. |
9230 | 3190 |
This can be used to query for an existing symbol and is the same as: |
16202 | 3191 |
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
|
3192 |
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
|
3193 |
The receiver must be a singleByte-String. |
ccd0e8f26439
printing - migrating towards unicodeSupport in ST-code
Claus Gittinger <cg@exept.de>
parents:
8040
diff
changeset
|
3194 |
TwoByte- and FourByteSymbols are (currently ?) not allowed." |
1435
3ee1fde2aa58
allow asSymbolIfInterned for Texts
Claus Gittinger <cg@exept.de>
parents:
1434
diff
changeset
|
3195 |
|
6642 | 3196 |
|s| |
3197 |
||
3198 |
||
3199 |
s := self string. |
|
3200 |
s ~~ self ifTrue:[ |
|
3201 |
^ s asSymbolIfInterned |
|
3202 |
]. |
|
3203 |
^ nil. |
|
1435
3ee1fde2aa58
allow asSymbolIfInterned for Texts
Claus Gittinger <cg@exept.de>
parents:
1434
diff
changeset
|
3204 |
|
3ee1fde2aa58
allow asSymbolIfInterned for Texts
Claus Gittinger <cg@exept.de>
parents:
1434
diff
changeset
|
3205 |
"Created: 22.5.1996 / 16:37:04 / cg" |
9192 | 3206 |
! |
3207 |
||
3208 |
asText |
|
9403 | 3209 |
"return a Text-object (string with emphasis) from myself." |
9192 | 3210 |
|
3211 |
"this test allows for small non-gui apps to be built without libbasic2 (where Text is)" |
|
3212 |
Text isNil ifTrue:[^ self]. |
|
3213 |
^ Text fromString:self |
|
3214 |
||
3215 |
"Created: 12.5.1996 / 10:41:14 / cg" |
|
3216 |
! |
|
3217 |
||
3218 |
asTitlecase |
|
8027 | 3219 |
"return a version of the receiver, where the first character is converted to titlecase, |
3220 |
and everything else to lowercase. |
|
3221 |
See the comment in Character on what titlecase is." |
|
3222 |
||
3223 |
|newStr c bitsPerCharacter |
|
3224 |
mySize "{ Class: SmallInteger }" | |
|
3225 |
||
3226 |
mySize := self size. |
|
3227 |
newStr := self species new:mySize. |
|
3228 |
bitsPerCharacter := newStr bitsPerCharacter. |
|
3229 |
||
3230 |
1 to:mySize do:[:i | |
|
16202 | 3231 |
i == 1 ifTrue:[ |
3232 |
c := (self at:i) asTitlecase. |
|
3233 |
] ifFalse:[ |
|
3234 |
c := (self at:i) asLowercase. |
|
3235 |
]. |
|
3236 |
c bitsPerCharacter > bitsPerCharacter ifTrue:[ |
|
3237 |
newStr := c stringSpecies fromString:newStr. |
|
3238 |
]. |
|
3239 |
newStr at:i put:c |
|
8027 | 3240 |
]. |
3241 |
^ newStr |
|
3242 |
||
3243 |
" |
|
8874 | 3244 |
'helloWorld' asTitlecase |
3245 |
'HelloWorld' asTitlecase |
|
3246 |
'HELLOWORLD' asTitlecase |
|
3247 |
'helloworld' asTitlecase |
|
8027 | 3248 |
" |
9192 | 3249 |
! |
3250 |
||
3251 |
asTitlecaseFirst |
|
8023 | 3252 |
"return a version of the receiver, where the first character is converted to titlecase. |
8024 | 3253 |
Titlecase is much like uppercase for most characters, with the exception of some combined |
3254 |
(2-character glyphs), which consist of an upper- and lower-case characters. |
|
3255 |
If the first character is already titlecase, or there is no titlecasepercase for it, return the |
|
8023 | 3256 |
receiver." |
3257 |
||
8026 | 3258 |
" |
8874 | 3259 |
For example, in Unicode, character U+01F3 is LATIN SMALL LETTER DZ. |
3260 |
(Let us write this compound character using ASCII as 'dz'.) |
|
3261 |
This character uppercases to character U+01F1, LATIN CAPITAL LETTER DZ. |
|
3262 |
(Which is basically 'DZ'.) |
|
3263 |
But it titlecases to to character U+01F2, LATIN CAPITAL LETTER D WITH SMALL LETTER Z. |
|
8026 | 3264 |
(Which we can write 'Dz'.) |
3265 |
||
3266 |
character uppercase titlecase |
|
3267 |
--------- --------- --------- |
|
3268 |
dz DZ Dz |
|
3269 |
" |
|
3270 |
||
8023 | 3271 |
|newString firstChar firstCharAsTitlecase| |
3272 |
||
3273 |
firstChar := (self at:1). |
|
8025 | 3274 |
firstCharAsTitlecase := firstChar asTitlecase. |
8023 | 3275 |
firstChar == firstCharAsTitlecase ifTrue:[ ^ self]. |
8037
8cce25b1059c
care for wide chars in asXXXFirst
Claus Gittinger <cg@exept.de>
parents:
8032
diff
changeset
|
3276 |
|
8cce25b1059c
care for wide chars in asXXXFirst
Claus Gittinger <cg@exept.de>
parents:
8032
diff
changeset
|
3277 |
firstCharAsTitlecase bitsPerCharacter > self bitsPerCharacter ifTrue:[ |
16202 | 3278 |
newString := firstCharAsTitlecase stringSpecies fromString:self. |
8037
8cce25b1059c
care for wide chars in asXXXFirst
Claus Gittinger <cg@exept.de>
parents:
8032
diff
changeset
|
3279 |
] ifFalse:[ |
16202 | 3280 |
newString := self stringSpecies fromString:self. |
8037
8cce25b1059c
care for wide chars in asXXXFirst
Claus Gittinger <cg@exept.de>
parents:
8032
diff
changeset
|
3281 |
]. |
8023 | 3282 |
newString at:1 put:firstCharAsTitlecase. |
3283 |
^ newString |
|
3284 |
||
3285 |
" |
|
8874 | 3286 |
'helloWorld' asTitlecaseFirst |
3287 |
'HelloWorld' asTitlecaseFirst |
|
8023 | 3288 |
" |
9192 | 3289 |
! |
3290 |
||
3291 |
asTwoByteString |
|
3292 |
"return the receiver converted to a two-byte string. |
|
16051 | 3293 |
Will be obsolete soon - use asUnicode16String." |
9192 | 3294 |
|
3295 |
^ TwoByteString fromString:self |
|
3296 |
! |
|
3297 |
||
3298 |
asURI |
|
3299 |
"return an URI with string taken from the receiver" |
|
3300 |
||
3301 |
^ URI fromString:self |
|
3302 |
! |
|
8710 | 3303 |
|
5776 | 3304 |
asURL |
3305 |
"return an URL-object from myself." |
|
3306 |
||
3307 |
^ URL fromString:self |
|
3308 |
||
3309 |
" |
|
8874 | 3310 |
'http://www.exept.de:80/index.html' asURL host |
3311 |
'http://www.exept.de:80/index.html' asURL port |
|
9192 | 3312 |
'http://www.exept.de:80/index.html' asURL method |
3313 |
'http://www.exept.de:80/index.html' asURL path |
|
3314 |
" |
|
3315 |
! |
|
3316 |
||
3317 |
asUnicode16String |
|
15035 | 3318 |
"return the receiver in a two-byte per character representation. |
3319 |
Normally, the internal ST/X representation should be transparent and not |
|
3320 |
of the programmer's concern; except when strings are passed to the outside world, |
|
3321 |
such as wide-string ffi calls or file contents." |
|
3322 |
||
16051 | 3323 |
^ self as:Unicode16String. |
9192 | 3324 |
! |
8295
8433d819226b
Define #asUnicode16String and #asUnicode32String in CharacterArray
Stefan Vogel <sv@exept.de>
parents:
8222
diff
changeset
|
3325 |
|
8433d819226b
Define #asUnicode16String and #asUnicode32String in CharacterArray
Stefan Vogel <sv@exept.de>
parents:
8222
diff
changeset
|
3326 |
asUnicode32String |
16051 | 3327 |
"return the receiver in a four-byte per character representation. |
3328 |
Normally, the internal ST/X representation should be transparent and not |
|
3329 |
of the programmer's concern; except when strings are passed to the outside world, |
|
3330 |
such as wide-string ffi calls or file contents." |
|
3331 |
||
3332 |
^ self as:Unicode32String |
|
8295
8433d819226b
Define #asUnicode16String and #asUnicode32String in CharacterArray
Stefan Vogel <sv@exept.de>
parents:
8222
diff
changeset
|
3333 |
! |
8433d819226b
Define #asUnicode16String and #asUnicode32String in CharacterArray
Stefan Vogel <sv@exept.de>
parents:
8222
diff
changeset
|
3334 |
|
8433d819226b
Define #asUnicode16String and #asUnicode32String in CharacterArray
Stefan Vogel <sv@exept.de>
parents:
8222
diff
changeset
|
3335 |
asUnicodeString |
16051 | 3336 |
"return the receiver in a two-byte per character representation. |
3337 |
Normally, the internal ST/X representation should be transparent and not |
|
3338 |
of the programmer's concern; except when strings are passed to the outside world, |
|
3339 |
such as wide-string ffi calls or file contents." |
|
3340 |
||
3341 |
^ self asUnicode16String |
|
3342 |
" |
|
3343 |
'Hello World' asUnicodeString |
|
14141
e86d245213b0
comment/format in: #asUnicodeString
Stefan Vogel <sv@exept.de>
parents:
14137
diff
changeset
|
3344 |
" |
8295
8433d819226b
Define #asUnicode16String and #asUnicode32String in CharacterArray
Stefan Vogel <sv@exept.de>
parents:
8222
diff
changeset
|
3345 |
! |
8433d819226b
Define #asUnicode16String and #asUnicode32String in CharacterArray
Stefan Vogel <sv@exept.de>
parents:
8222
diff
changeset
|
3346 |
|
13073 | 3347 |
asUnixFilename |
3348 |
"return a UnixFilename with pathname taken from the receiver" |
|
3349 |
||
3350 |
^ UnixFilename named:self |
|
3351 |
||
3352 |
"Created: / 20-09-2010 / 21:42:55 / cg" |
|
3353 |
! |
|
3354 |
||
608 | 3355 |
asUppercase |
3356 |
"return a copy of myself in uppercase letters" |
|
3357 |
||
8003
6e690f4b79f1
asLowercase/asUppercase and friends care for 16-bit chars
Claus Gittinger <cg@exept.de>
parents:
7995
diff
changeset
|
3358 |
|newStr c bitsPerCharacter |
608 | 3359 |
mySize "{ Class: SmallInteger }" | |
3360 |
||
3361 |
mySize := self size. |
|
8954
12e701d7b5ac
allow for receiver to be empty in asLowercase and asUppercase.
Claus Gittinger <cg@exept.de>
parents:
8938
diff
changeset
|
3362 |
mySize == 0 ifTrue:[^ self]. |
608 | 3363 |
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
|
3364 |
bitsPerCharacter := newStr bitsPerCharacter. |
6e690f4b79f1
asLowercase/asUppercase and friends care for 16-bit chars
Claus Gittinger <cg@exept.de>
parents:
7995
diff
changeset
|
3365 |
|
608 | 3366 |
1 to:mySize do:[:i | |
16202 | 3367 |
c := (self at:i) asUppercase. |
3368 |
c bitsPerCharacter > bitsPerCharacter ifTrue:[ |
|
3369 |
newStr := c stringSpecies fromString:newStr. |
|
3370 |
]. |
|
3371 |
newStr at:i put:c |
|
608 | 3372 |
]. |
3373 |
^ newStr |
|
3374 |
||
3375 |
" |
|
8874 | 3376 |
'helloWorld' asUppercase |
3377 |
'helloWorld' asUppercaseFirst |
|
9192 | 3378 |
(Character value:16rB5) asString asUppercase -- needs 16 bits !! |
3379 |
" |
|
3380 |
! |
|
3381 |
||
3382 |
asUppercaseFirst |
|
8003
6e690f4b79f1
asLowercase/asUppercase and friends care for 16-bit chars
Claus Gittinger <cg@exept.de>
parents:
7995
diff
changeset
|
3383 |
"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
|
3384 |
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
|
3385 |
receiver." |
6e690f4b79f1
asLowercase/asUppercase and friends care for 16-bit chars
Claus Gittinger <cg@exept.de>
parents:
7995
diff
changeset
|
3386 |
|
6e690f4b79f1
asLowercase/asUppercase and friends care for 16-bit chars
Claus Gittinger <cg@exept.de>
parents:
7995
diff
changeset
|
3387 |
|newString firstChar firstCharAsUppercase| |
6e690f4b79f1
asLowercase/asUppercase and friends care for 16-bit chars
Claus Gittinger <cg@exept.de>
parents:
7995
diff
changeset
|
3388 |
|
12679 | 3389 |
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
|
3390 |
firstChar := self at:1. |
8003
6e690f4b79f1
asLowercase/asUppercase and friends care for 16-bit chars
Claus Gittinger <cg@exept.de>
parents:
7995
diff
changeset
|
3391 |
firstCharAsUppercase := firstChar asUppercase. |
6e690f4b79f1
asLowercase/asUppercase and friends care for 16-bit chars
Claus Gittinger <cg@exept.de>
parents:
7995
diff
changeset
|
3392 |
firstChar == firstCharAsUppercase ifTrue:[ ^ self]. |
6e690f4b79f1
asLowercase/asUppercase and friends care for 16-bit chars
Claus Gittinger <cg@exept.de>
parents:
7995
diff
changeset
|
3393 |
|
12679 | 3394 |
"/ 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
|
3395 |
firstCharAsUppercase bitsPerCharacter > self bitsPerCharacter ifTrue:[ |
16202 | 3396 |
newString := firstCharAsUppercase stringSpecies fromString:self. |
8037
8cce25b1059c
care for wide chars in asXXXFirst
Claus Gittinger <cg@exept.de>
parents:
8032
diff
changeset
|
3397 |
] ifFalse:[ |
16202 | 3398 |
newString := self stringSpecies fromString:self. |
8037
8cce25b1059c
care for wide chars in asXXXFirst
Claus Gittinger <cg@exept.de>
parents:
8032
diff
changeset
|
3399 |
]. |
8003
6e690f4b79f1
asLowercase/asUppercase and friends care for 16-bit chars
Claus Gittinger <cg@exept.de>
parents:
7995
diff
changeset
|
3400 |
newString at:1 put:firstCharAsUppercase. |
608 | 3401 |
^ newString |
3402 |
||
3403 |
" |
|
8874 | 3404 |
'helloWorld' asUppercase |
3405 |
'helloWorld' asUppercaseFirst |
|
3406 |
'HelloWorld' asUppercaseFirst |
|
608 | 3407 |
" |
9192 | 3408 |
! |
3409 |
||
11828 | 3410 |
asUppercaseLast |
3411 |
"return a copy of myself where the last character is |
|
3412 |
converted to uppercase." |
|
3413 |
||
3414 |
|newString sz| |
|
3415 |
||
3416 |
sz := self size. |
|
3417 |
newString := self copyFrom:1 to:sz. |
|
3418 |
sz > 0 ifTrue:[ |
|
16202 | 3419 |
newString at:sz put:(newString at:sz) asUppercase |
11828 | 3420 |
]. |
3421 |
^ newString |
|
3422 |
||
3423 |
" |
|
12177 | 3424 |
'HelloWorld' asUppercase |
3425 |
'HelloWorld' asUppercaseLast |
|
11828 | 3426 |
" |
3427 |
! |
|
3428 |
||
11062 | 3429 |
literalArrayEncoding |
3430 |
"encode myself as an array literal, from which a copy of the receiver |
|
3431 |
can be reconstructed with #decodeAsLiteralArray." |
|
3432 |
||
3433 |
^ self |
|
3434 |
||
3435 |
"Modified: 1.9.1995 / 02:25:45 / claus" |
|
3436 |
"Modified: 22.4.1996 / 13:00:50 / cg" |
|
3437 |
! |
|
3438 |
||
9192 | 3439 |
string |
3440 |
"return the receiver - for ST-80 compatibility" |
|
3441 |
||
3442 |
^ self |
|
3443 |
||
3444 |
"Modified: 27.4.1996 / 13:29:30 / cg" |
|
3445 |
! ! |
|
3446 |
||
3447 |
!CharacterArray methodsFor:'copying'! |
|
3448 |
||
3449 |
, aStringOrCharacter |
|
2720
98c21d9a07c0
allow mixed strings to be concatenated using comma
Claus Gittinger <cg@exept.de>
parents:
2692
diff
changeset
|
3450 |
"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
|
3451 |
This is nonStandard, but convenient" |
2f14bbd792a9
, now accepts a character-argument
Claus Gittinger <cg@exept.de>
parents:
1028
diff
changeset
|
3452 |
|
2720
98c21d9a07c0
allow mixed strings to be concatenated using comma
Claus Gittinger <cg@exept.de>
parents:
2692
diff
changeset
|
3453 |
|myWidth otherWidth| |
98c21d9a07c0
allow mixed strings to be concatenated using comma
Claus Gittinger <cg@exept.de>
parents:
2692
diff
changeset
|
3454 |
|
1050
2f14bbd792a9
, now accepts a character-argument
Claus Gittinger <cg@exept.de>
parents:
1028
diff
changeset
|
3455 |
aStringOrCharacter isCharacter ifTrue:[ |
16202 | 3456 |
^ self copyWith:aStringOrCharacter |
1050
2f14bbd792a9
, now accepts a character-argument
Claus Gittinger <cg@exept.de>
parents:
1028
diff
changeset
|
3457 |
]. |
1396
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
3458 |
aStringOrCharacter isText ifTrue:[ |
16202 | 3459 |
^ aStringOrCharacter concatenateFromString:self |
1396
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
3460 |
]. |
2720
98c21d9a07c0
allow mixed strings to be concatenated using comma
Claus Gittinger <cg@exept.de>
parents:
2692
diff
changeset
|
3461 |
aStringOrCharacter isString ifTrue:[ |
16202 | 3462 |
(otherWidth := aStringOrCharacter bitsPerCharacter) ~~ (myWidth := self bitsPerCharacter) ifTrue:[ |
3463 |
otherWidth > myWidth ifTrue:[ |
|
3464 |
^ (aStringOrCharacter species fromString:self) , aStringOrCharacter |
|
3465 |
]. |
|
3466 |
^ self , (self species fromString:aStringOrCharacter) |
|
3467 |
]. |
|
2720
98c21d9a07c0
allow mixed strings to be concatenated using comma
Claus Gittinger <cg@exept.de>
parents:
2692
diff
changeset
|
3468 |
]. |
1050
2f14bbd792a9
, now accepts a character-argument
Claus Gittinger <cg@exept.de>
parents:
1028
diff
changeset
|
3469 |
^ super , aStringOrCharacter |
2f14bbd792a9
, now accepts a character-argument
Claus Gittinger <cg@exept.de>
parents:
1028
diff
changeset
|
3470 |
|
2f14bbd792a9
, now accepts a character-argument
Claus Gittinger <cg@exept.de>
parents:
1028
diff
changeset
|
3471 |
" |
8874 | 3472 |
'hello' , $1 |
3473 |
'hello' , '1' |
|
3474 |
'hello' , (' world' asText allBold) |
|
3475 |
'hello' , (JISEncodedString fromString:' world') |
|
2720
98c21d9a07c0
allow mixed strings to be concatenated using comma
Claus Gittinger <cg@exept.de>
parents:
2692
diff
changeset
|
3476 |
(JISEncodedString fromString:'hello') , ' world' |
1411 | 3477 |
|
1422 | 3478 |
Transcript showCR: |
16202 | 3479 |
(Text string:'hello' emphasis:#italic) , (Text string:' world' emphasis:#bold) |
1050
2f14bbd792a9
, now accepts a character-argument
Claus Gittinger <cg@exept.de>
parents:
1028
diff
changeset
|
3480 |
" |
2f14bbd792a9
, now accepts a character-argument
Claus Gittinger <cg@exept.de>
parents:
1028
diff
changeset
|
3481 |
|
2720
98c21d9a07c0
allow mixed strings to be concatenated using comma
Claus Gittinger <cg@exept.de>
parents:
2692
diff
changeset
|
3482 |
"Modified: 28.6.1997 / 00:13:17 / cg" |
9192 | 3483 |
! |
3484 |
||
3485 |
,, aString |
|
7020 | 3486 |
"concatenate with a newLine in between" |
6965 | 3487 |
|
3488 |
^ (self copyWith:Character cr) , aString |
|
3489 |
||
3490 |
" |
|
3491 |
hello ,, world |
|
7020 | 3492 |
'hello' ,, 'world' |
6965 | 3493 |
" |
3494 |
! |
|
3495 |
||
11329 | 3496 |
chopTo:maxLen |
3497 |
"if the receivers size is less or equal to maxLen, return it. |
|
3498 |
Otherwise, return a copy of the receiver, where some characters |
|
3499 |
in the middle have been removed for a total string length |
|
3500 |
of maxLen." |
|
3501 |
||
3502 |
|sz n1 n2| |
|
3503 |
||
3504 |
(sz := self size) <= maxLen ifTrue:[ ^ self ]. |
|
3505 |
||
3506 |
n1 := n2 := maxLen // 2. |
|
3507 |
maxLen odd ifTrue:[ |
|
16202 | 3508 |
n2 := n1 + 1 |
11329 | 3509 |
]. |
3510 |
^ (self copyFrom:1 to:n1) , (self copyFrom:sz - n2 + 1) |
|
3511 |
||
3512 |
" |
|
3513 |
'12345678901234' chopTo:15 |
|
3514 |
'123456789012345' chopTo:15 |
|
3515 |
'1234567890123456' chopTo:15 |
|
3516 |
'aShortString' chopTo:15 |
|
3517 |
'aVeryLongNameForAStringThatShouldBeShortened' chopTo:15 |
|
3518 |
" |
|
3519 |
||
3520 |
"Modified: / 24-10-2006 / 12:32:01 / cg" |
|
3521 |
! |
|
3522 |
||
608 | 3523 |
concatenate:string1 and:string2 |
3524 |
"return the concatenation of myself and the arguments, string1 and string2. |
|
3525 |
This is equivalent to self , string1 , string2 |
|
3526 |
- generated by compiler when such a construct is detected and the receiver |
|
3527 |
is known to be a string." |
|
3528 |
||
3529 |
^ self , string1 , string2 |
|
3530 |
! |
|
3531 |
||
3532 |
concatenate:string1 and:string2 and:string3 |
|
3533 |
"return the concatenation of myself and the string arguments. |
|
3534 |
This is equivalent to self , string1 , string2 , string3 |
|
3535 |
- generated by compiler when such a construct is detected and the receiver |
|
3536 |
is known to be a string." |
|
3537 |
||
3538 |
^ self , string1 , string2 , string3 |
|
8040
f10cca439066
copyWith for mixed-width string/character
Claus Gittinger <cg@exept.de>
parents:
8037
diff
changeset
|
3539 |
! |
f10cca439066
copyWith for mixed-width string/character
Claus Gittinger <cg@exept.de>
parents:
8037
diff
changeset
|
3540 |
|
11329 | 3541 |
contractAtBeginningTo:maxLen |
3542 |
"if the receivers size is less or equal to maxLen, return it. |
|
3543 |
Otherwise, return a copy of the receiver, where some characters |
|
3544 |
at the beginning have been replaced by '...' for a total string length |
|
3545 |
of maxLen. Can be used to abbreviate long entries in tables." |
|
3546 |
||
3547 |
|sz| |
|
3548 |
||
3549 |
(sz := self size) <= maxLen ifTrue:[ ^ self ]. |
|
3550 |
||
3551 |
^ '...' , (self copyFrom:(sz - (maxLen - 4))) |
|
3552 |
||
3553 |
" |
|
3554 |
'12345678901234' contractAtBeginningTo:15 |
|
3555 |
'123456789012345' contractAtBeginningTo:15 |
|
3556 |
'1234567890123456' contractAtBeginningTo:15 |
|
3557 |
'aShortString' contractAtBeginningTo:15 |
|
3558 |
'aVeryLongNameForAStringThatShouldBeShortened' contractAtBeginningTo:15 |
|
3559 |
" |
|
3560 |
||
3561 |
"Modified: / 24-10-2006 / 12:32:13 / cg" |
|
3562 |
! |
|
3563 |
||
3564 |
contractAtEndTo:maxLen |
|
3565 |
"if the receivers size is less or equal to maxLen, return it. |
|
3566 |
Otherwise, return a copy of the receiver, where some characters |
|
3567 |
at the end have been replaced by '...' for a total string length |
|
3568 |
of maxLen. Can be used to abbreviate long entries in tables." |
|
3569 |
||
3570 |
|sz| |
|
3571 |
||
3572 |
(sz := self size) <= maxLen ifTrue:[ ^ self ]. |
|
3573 |
||
3574 |
^ (self copyTo:maxLen-3),'...' |
|
3575 |
||
3576 |
" |
|
3577 |
'12345678901234' contractAtEndTo:15 |
|
3578 |
'123456789012345' contractAtEndTo:15 |
|
3579 |
'1234567890123456' contractAtEndTo:15 |
|
3580 |
'aShortString' contractAtEndTo:15 |
|
3581 |
'aVeryLongNameForAStringThatShouldBeShortened' contractAtEndTo:15 |
|
3582 |
" |
|
3583 |
||
3584 |
"Modified: / 24-10-2006 / 12:32:26 / cg" |
|
3585 |
! |
|
3586 |
||
3587 |
contractLeftTo:maxLen |
|
3588 |
"if the receivers size is less or equal to maxLen, return it. |
|
3589 |
Otherwise, return a copy of the receiver, where some characters |
|
3590 |
near the first quarter have been replaced by '...' for a total string length |
|
12177 | 3591 |
of maxLen. |
3592 |
Very similar to contractTo:, but better to abbreviate long filename entries, |
|
11329 | 3593 |
where the right part is of more use than the left." |
3594 |
||
3595 |
|sz "{ SmallInteger }" |
|
3596 |
halfSize quarterSize "{ SmallInteger }" |
|
3597 |
leftEnd rightEnd rightStart| |
|
3598 |
||
3599 |
(sz := self size) <= maxLen ifTrue:[ ^ self ]. |
|
3600 |
||
3601 |
halfSize := maxLen // 2. |
|
3602 |
quarterSize := maxLen // 4. |
|
3603 |
leftEnd := quarterSize-1. |
|
3604 |
rightEnd := maxLen - leftEnd - 3. |
|
3605 |
rightStart := sz - rightEnd + 1. |
|
3606 |
^ (self copyTo:leftEnd),'...',(self copyFrom:rightStart) |
|
3607 |
||
3608 |
" |
|
12177 | 3609 |
'12345678901234' contractLeftTo:15 |
3610 |
'123456789012345' contractLeftTo:15 |
|
3611 |
'1234567890123456' contractLeftTo:15 |
|
3612 |
'aShortString' contractLeftTo:15 |
|
3613 |
'aVeryLongNameForAStringThatShouldBeShortened' contractLeftTo:15 |
|
11329 | 3614 |
'C:\Dokumente und Einstellungen\cg\work\bosch\dapas\hw_schnittstellen\DAPAS__HpibDLL.st' contractLeftTo:40 |
3615 |
" |
|
3616 |
! |
|
3617 |
||
3618 |
contractTo:maxLen |
|
3619 |
"if the receivers size is less or equal to maxLen, return it. |
|
3620 |
Otherwise, return a copy of the receiver, where some characters |
|
3621 |
in the middle have been replaced by '...' for a total string length |
|
3622 |
of maxLen. Can be used to abbreviate long entries in tables." |
|
3623 |
||
13362 | 3624 |
|sz "{ SmallInteger }" leftSize rightSize| |
11329 | 3625 |
|
3626 |
(sz := self size) <= maxLen ifTrue:[ ^ self ]. |
|
3627 |
||
13362 | 3628 |
rightSize := maxLen // 2. |
3629 |
leftSize := maxLen - rightSize. |
|
13822 | 3630 |
leftSize := leftSize - 2. |
3631 |
rightSize := rightSize - 1. |
|
13362 | 3632 |
^ (self copyTo:leftSize),'...',(self copyFrom:(sz+1-rightSize)) |
3633 |
||
3634 |
" |
|
13882
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
3635 |
'12345678901234' contractTo:15 |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
3636 |
'123456789012345' contractTo:15 |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
3637 |
'1234567890123456' contractTo:15 |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
3638 |
'12345678901234567' contractTo:15 |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
3639 |
'123456789012345678' contractTo:15 |
187a02bd5c99
OUCH: back to old hash; crashes Certificate compare in expecco (WHY??????=
Claus Gittinger <cg@exept.de>
parents:
13879
diff
changeset
|
3640 |
'aShortString' contractTo:15 |
11329 | 3641 |
'aVeryLongNameForAStringThatShouldBeShortened' contractTo:15 |
3642 |
'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
|
3643 |
('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
|
3644 |
('1234567890123456789012345678901234567' contractTo:29) size |
13362 | 3645 |
" |
3646 |
||
13822 | 3647 |
"Modified (comment): / 24-11-2011 / 19:17:46 / cg" |
11329 | 3648 |
! |
3649 |
||
16202 | 3650 |
copyReplaceAll:oldElement with:newElement |
3651 |
"return a copy of the receiver as a string, where all elements equal to oldElement |
|
3652 |
have been replaced by newElement." |
|
3653 |
||
3654 |
"/ ANSI seems to allow a sequence to be replaced by another sequence, |
|
3655 |
"/ whereas the old ST80 meant replace all occurrences... - sigh. |
|
3656 |
oldElement isByteCollection ifTrue:[ |
|
3657 |
newElement isByteCollection ifTrue:[ |
|
3658 |
^ self copyReplaceString:oldElement withString:newElement. |
|
3659 |
]. |
|
3660 |
self halt:'check if this is legal'. |
|
3661 |
]. |
|
3662 |
newElement isByteCollection ifTrue:[ |
|
3663 |
self halt:'check if this is legal'. |
|
3664 |
]. |
|
16212 | 3665 |
^ super copyReplaceAll:oldElement with:newElement |
16202 | 3666 |
! |
3667 |
||
8347 | 3668 |
copyReplaceString:subString withString:newString |
3669 |
"return a copy of the receiver, with all sequences of subString replaced |
|
3670 |
by newString (i.e. slice in the newString in place of the oldString)." |
|
3671 |
||
3672 |
|tmpStream idx idx1| |
|
3673 |
||
15523 | 3674 |
tmpStream := self species writeStream. |
8347 | 3675 |
idx := 1. |
3676 |
[idx ~~ 0] whileTrue:[ |
|
15523 | 3677 |
idx1 := idx. |
3678 |
idx := self indexOfSubCollection:subString startingAt:idx. |
|
3679 |
idx ~~ 0 ifTrue:[ |
|
3680 |
tmpStream nextPutAll:(self copyFrom:idx1 to:idx-1). |
|
3681 |
tmpStream nextPutAll:newString. |
|
3682 |
idx := idx + subString size |
|
3683 |
] |
|
8347 | 3684 |
]. |
3685 |
tmpStream nextPutAll:(self copyFrom:idx1). |
|
3686 |
^ tmpStream contents |
|
3687 |
||
3688 |
" |
|
9306 | 3689 |
'12345678901234567890' copyReplaceString:'123' withString:'OneTwoThree' |
3690 |
'12345678901234567890' copyReplaceString:'123' withString:'*' |
|
3691 |
'12345678901234567890' copyReplaceString:'234' withString:'foo' |
|
3692 |
||
3693 |
('a string with spaces' copyReplaceAll:$ withAll:' foo ') |
|
15523 | 3694 |
copyReplaceString:'foo' withString:'bar' |
8347 | 3695 |
" |
3696 |
||
3697 |
"Modified: / 31-05-1999 / 12:33:59 / cg" |
|
3698 |
"Created: / 12-05-2004 / 12:00:00 / cg" |
|
3699 |
! |
|
3700 |
||
8040
f10cca439066
copyWith for mixed-width string/character
Claus Gittinger <cg@exept.de>
parents:
8037
diff
changeset
|
3701 |
copyWith:aCharacter |
f10cca439066
copyWith for mixed-width string/character
Claus Gittinger <cg@exept.de>
parents:
8037
diff
changeset
|
3702 |
"return a new string containing the receivers characters |
8874 | 3703 |
and the single new character, aCharacter. |
8040
f10cca439066
copyWith for mixed-width string/character
Claus Gittinger <cg@exept.de>
parents:
8037
diff
changeset
|
3704 |
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
|
3705 |
as argument, but equivalent to copy-and-addLast. |
8874 | 3706 |
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
|
3707 |
(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
|
3708 |
|
f10cca439066
copyWith for mixed-width string/character
Claus Gittinger <cg@exept.de>
parents:
8037
diff
changeset
|
3709 |
|sz newString| |
f10cca439066
copyWith for mixed-width string/character
Claus Gittinger <cg@exept.de>
parents:
8037
diff
changeset
|
3710 |
|
f10cca439066
copyWith for mixed-width string/character
Claus Gittinger <cg@exept.de>
parents:
8037
diff
changeset
|
3711 |
aCharacter bitsPerCharacter > self bitsPerCharacter ifTrue:[ |
16202 | 3712 |
sz := self size. |
3713 |
newString := aCharacter stringSpecies new:sz + 1. |
|
3714 |
newString replaceFrom:1 to:sz with:self startingAt:1. |
|
3715 |
newString at:sz+1 put:aCharacter. |
|
3716 |
^ newString. |
|
8040
f10cca439066
copyWith for mixed-width string/character
Claus Gittinger <cg@exept.de>
parents:
8037
diff
changeset
|
3717 |
]. |
f10cca439066
copyWith for mixed-width string/character
Claus Gittinger <cg@exept.de>
parents:
8037
diff
changeset
|
3718 |
^ super copyWith:aCharacter |
608 | 3719 |
! ! |
3720 |
||
3721 |
!CharacterArray methodsFor:'displaying'! |
|
3722 |
||
2762 | 3723 |
displayOn:aGC x:x y:y from:start to:stop |
3724 |
"display the receiver on a GC" |
|
3725 |
||
12171 | 3726 |
self displayOn:aGC x:x y:y from:start to:stop opaque:false |
3727 |
||
3728 |
"Modified: 12.5.1996 / 12:49:33 / cg" |
|
3729 |
! |
|
3730 |
||
3731 |
displayOn:aGC x:x y:y from:start to:stop opaque:opaque |
|
3732 |
"display the receiver on a GC" |
|
3733 |
||
2762 | 3734 |
"q&d hack" |
3735 |
||
12177 | 3736 |
(start == 1 and:[stop == self size]) ifTrue:[ |
16202 | 3737 |
self displayOn:aGC x:x y:y opaque:opaque. |
12177 | 3738 |
] ifFalse:[ |
16202 | 3739 |
(self copyFrom:start to:stop) displayOn:aGC x:x y:y opaque:opaque. |
12177 | 3740 |
]. |
2762 | 3741 |
! |
3742 |
||
7082 | 3743 |
displayOn:aGc x:x y:y opaque:opaque |
2762 | 3744 |
"display the receiver in a graphicsContext - this method allows |
3745 |
strings to be used like DisplayObjects." |
|
3746 |
||
3747 |
|s| |
|
3748 |
||
3749 |
s := self string. |
|
7082 | 3750 |
opaque ifTrue:[ |
16202 | 3751 |
aGc displayOpaqueString:s x:x y:y. |
2762 | 3752 |
] ifFalse:[ |
16202 | 3753 |
aGc displayString:s x:x y:y. |
2762 | 3754 |
]. |
1381 | 3755 |
|
3756 |
"Modified: 11.5.1996 / 14:42:48 / cg" |
|
3757 |
! |
|
3758 |
||
2762 | 3759 |
displayOpaqueOn:aGC x:x y:y from:start to:stop |
3760 |
"display the receiver on a GC" |
|
3761 |
||
12171 | 3762 |
self displayOn:aGC x:x y:y from:start to:stop opaque:true |
608 | 3763 |
! ! |
3764 |
||
1375
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
3765 |
!CharacterArray methodsFor:'emphasis'! |
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
3766 |
|
11632
b82e9f3cabd1
new: #actionForAll: #emphasisAtPoint:on:
Stefan Vogel <sv@exept.de>
parents:
11630
diff
changeset
|
3767 |
actionForAll:aBlock |
12326
c67f12213ddb
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
12319
diff
changeset
|
3768 |
"change the action block of all characters. |
c67f12213ddb
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
12319
diff
changeset
|
3769 |
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
|
3770 |
|
b82e9f3cabd1
new: #actionForAll: #emphasisAtPoint:on:
Stefan Vogel <sv@exept.de>
parents:
11630
diff
changeset
|
3771 |
^ self asText actionForAll:aBlock |
b82e9f3cabd1
new: #actionForAll: #emphasisAtPoint:on:
Stefan Vogel <sv@exept.de>
parents:
11630
diff
changeset
|
3772 |
! |
b82e9f3cabd1
new: #actionForAll: #emphasisAtPoint:on:
Stefan Vogel <sv@exept.de>
parents:
11630
diff
changeset
|
3773 |
|
5732 | 3774 |
allBold |
12326
c67f12213ddb
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
12319
diff
changeset
|
3775 |
"return a text object representing the receiver, but all boldified" |
5732 | 3776 |
|
8874 | 3777 |
"this test allows for small non-gui apps to be built without libbasic2 (where Text is)" |
6187 | 3778 |
Text isNil ifTrue:[^ self]. |
5732 | 3779 |
^ self asText allBold |
3780 |
||
3781 |
" |
|
3782 |
Transcript showCR:'hello' asText allBold |
|
3783 |
Transcript showCR:'hello' allBold |
|
3784 |
" |
|
3785 |
! |
|
3786 |
||
5898
7bc4f1593b4e
allItalic now understood by CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5848
diff
changeset
|
3787 |
allItalic |
12326
c67f12213ddb
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
12319
diff
changeset
|
3788 |
"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
|
3789 |
|
8874 | 3790 |
"this test allows for small non-gui apps to be built without libbasic2 (where Text is)" |
6187 | 3791 |
Text isNil ifTrue:[^ self]. |
5898
7bc4f1593b4e
allItalic now understood by CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5848
diff
changeset
|
3792 |
^ self asText allItalic |
7bc4f1593b4e
allItalic now understood by CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5848
diff
changeset
|
3793 |
|
7bc4f1593b4e
allItalic now understood by CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5848
diff
changeset
|
3794 |
" |
7bc4f1593b4e
allItalic now understood by CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5848
diff
changeset
|
3795 |
Transcript showCR:'hello' asText allItalic |
7bc4f1593b4e
allItalic now understood by CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5848
diff
changeset
|
3796 |
Transcript showCR:'hello' allItalic |
7bc4f1593b4e
allItalic now understood by CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5848
diff
changeset
|
3797 |
" |
7bc4f1593b4e
allItalic now understood by CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5848
diff
changeset
|
3798 |
! |
7bc4f1593b4e
allItalic now understood by CharacterArray
Claus Gittinger <cg@exept.de>
parents:
5848
diff
changeset
|
3799 |
|
11345 | 3800 |
allStrikedOut |
12326
c67f12213ddb
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
12319
diff
changeset
|
3801 |
"return a text object representing the receiver, but all in strikeout" |
11345 | 3802 |
|
3803 |
"this test allows for small non-gui apps to be built without libbasic2 (where Text is)" |
|
3804 |
Text isNil ifTrue:[^ self]. |
|
3805 |
^ self asText allStrikedOut |
|
3806 |
||
3807 |
" |
|
3808 |
Transcript showCR:'hello' asText allStrikedOut |
|
3809 |
Transcript showCR:'hello' allStrikedOut |
|
3810 |
" |
|
3811 |
! |
|
3812 |
||
10862 | 3813 |
allUnderlined |
12326
c67f12213ddb
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
12319
diff
changeset
|
3814 |
"return a text object representing the receiver, but all with underline" |
10862 | 3815 |
|
3816 |
"this test allows for small non-gui apps to be built without libbasic2 (where Text is)" |
|
3817 |
Text isNil ifTrue:[^ self]. |
|
3818 |
^ self asText allUnderlined |
|
3819 |
||
3820 |
" |
|
3821 |
Transcript showCR:'hello' asText allUnderlined |
|
3822 |
Transcript showCR:'hello' allUnderlined |
|
3823 |
" |
|
3824 |
! |
|
3825 |
||
6228 | 3826 |
colorizeAllWith:aColor |
12326
c67f12213ddb
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
12319
diff
changeset
|
3827 |
"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
|
3828 |
|
c67f12213ddb
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
12319
diff
changeset
|
3829 |
"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
|
3830 |
Text isNil ifTrue:[^ self]. |
6228 | 3831 |
^ self asText colorizeAllWith:aColor |
3832 |
||
3833 |
" |
|
3834 |
Transcript showCR:('hello' colorizeAllWith:Color red) |
|
3835 |
Transcript showCR:('world' colorizeAllWith:Color green darkened) |
|
3836 |
" |
|
3837 |
! |
|
3838 |
||
12330
f9b57b0b8a8b
added: #colorizeAllWith:on:
Claus Gittinger <cg@exept.de>
parents:
12326
diff
changeset
|
3839 |
colorizeAllWith:fgColor on:bgColor |
f9b57b0b8a8b
added: #colorizeAllWith:on:
Claus Gittinger <cg@exept.de>
parents:
12326
diff
changeset
|
3840 |
"return a text object representing the receiver, but all colorized with |
f9b57b0b8a8b
added: #colorizeAllWith:on:
Claus Gittinger <cg@exept.de>
parents:
12326
diff
changeset
|
3841 |
both fg and background color" |
f9b57b0b8a8b
added: #colorizeAllWith:on:
Claus Gittinger <cg@exept.de>
parents:
12326
diff
changeset
|
3842 |
|
f9b57b0b8a8b
added: #colorizeAllWith:on:
Claus Gittinger <cg@exept.de>
parents:
12326
diff
changeset
|
3843 |
"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
|
3844 |
Text isNil ifTrue:[^ self]. |
f9b57b0b8a8b
added: #colorizeAllWith:on:
Claus Gittinger <cg@exept.de>
parents:
12326
diff
changeset
|
3845 |
^ self asText colorizeAllWith:fgColor on:bgColor |
f9b57b0b8a8b
added: #colorizeAllWith:on:
Claus Gittinger <cg@exept.de>
parents:
12326
diff
changeset
|
3846 |
|
f9b57b0b8a8b
added: #colorizeAllWith:on:
Claus Gittinger <cg@exept.de>
parents:
12326
diff
changeset
|
3847 |
" |
f9b57b0b8a8b
added: #colorizeAllWith:on:
Claus Gittinger <cg@exept.de>
parents:
12326
diff
changeset
|
3848 |
Transcript showCR:('hello' colorizeAllWith:Color red on:Color yellow) |
f9b57b0b8a8b
added: #colorizeAllWith:on:
Claus Gittinger <cg@exept.de>
parents:
12326
diff
changeset
|
3849 |
Transcript showCR:('world' colorizeAllWith:Color red) |
f9b57b0b8a8b
added: #colorizeAllWith:on:
Claus Gittinger <cg@exept.de>
parents:
12326
diff
changeset
|
3850 |
" |
f9b57b0b8a8b
added: #colorizeAllWith:on:
Claus Gittinger <cg@exept.de>
parents:
12326
diff
changeset
|
3851 |
! |
f9b57b0b8a8b
added: #colorizeAllWith:on:
Claus Gittinger <cg@exept.de>
parents:
12326
diff
changeset
|
3852 |
|
1396
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
3853 |
emphasis |
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
3854 |
"return the emphasis. |
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
3855 |
Since characterArrays do not hold any emphasis information, |
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
3856 |
nil (no emphasis) is returned here." |
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
3857 |
|
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
3858 |
^ RunArray new:self size withAll:nil |
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
3859 |
|
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
3860 |
"Created: 14.5.1996 / 13:58:58 / cg" |
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
3861 |
! |
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
3862 |
|
6655 | 3863 |
emphasis:emphasisCollection |
3864 |
^ self asText emphasis:emphasisCollection |
|
3865 |
||
3866 |
" |
|
3867 |
Transcript showCR:('hello' emphasis:#(bold bold bold bold bold)) |
|
3868 |
" |
|
3869 |
! |
|
3870 |
||
1375
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
3871 |
emphasisAt:characterIndex |
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
3872 |
"return the emphasis at some index. |
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
3873 |
Since characterArrays do not hold any emphasis information, |
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
3874 |
nil (no emphasis) is returned here." |
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
3875 |
|
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
3876 |
^ nil |
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
3877 |
|
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
3878 |
"Created: 11.5.1996 / 14:13:27 / cg" |
1396
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
3879 |
! |
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
3880 |
|
11632
b82e9f3cabd1
new: #actionForAll: #emphasisAtPoint:on:
Stefan Vogel <sv@exept.de>
parents:
11630
diff
changeset
|
3881 |
emphasisAtPoint:aPoint on:aGC |
b82e9f3cabd1
new: #actionForAll: #emphasisAtPoint:on:
Stefan Vogel <sv@exept.de>
parents:
11630
diff
changeset
|
3882 |
"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
|
3883 |
|
b82e9f3cabd1
new: #actionForAll: #emphasisAtPoint:on:
Stefan Vogel <sv@exept.de>
parents:
11630
diff
changeset
|
3884 |
^ nil |
b82e9f3cabd1
new: #actionForAll: #emphasisAtPoint:on:
Stefan Vogel <sv@exept.de>
parents:
11630
diff
changeset
|
3885 |
! |
b82e9f3cabd1
new: #actionForAll: #emphasisAtPoint:on:
Stefan Vogel <sv@exept.de>
parents:
11630
diff
changeset
|
3886 |
|
1396
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
3887 |
emphasisCollection |
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
3888 |
"return the emphasis. |
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
3889 |
Since characterArrays do not hold any emphasis information, |
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
3890 |
nil (no emphasis) is returned here." |
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
3891 |
|
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
3892 |
^ RunArray new:(self size) |
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
3893 |
|
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
3894 |
"Created: 14.5.1996 / 13:58:58 / cg" |
5b5386311dbd
concatenating mixed texts & strings
Claus Gittinger <cg@exept.de>
parents:
1393
diff
changeset
|
3895 |
"Modified: 14.5.1996 / 15:02:29 / cg" |
3404 | 3896 |
! |
3897 |
||
11919 | 3898 |
emphasiseFrom:start to:stop with:newEmphasis |
3899 |
"set to the emphasis within some range. return the receiver" |
|
3900 |
||
3901 |
^ self asText emphasiseFrom:start to:stop with:newEmphasis |
|
3902 |
||
3903 |
" |
|
12177 | 3904 |
'hello' emphasiseFrom:2 with:#italic |
11919 | 3905 |
|
3906 |
Transcript showCR:('hello' emphasiseFrom:2 with:#italic) |
|
3907 |
" |
|
3908 |
! |
|
3909 |
||
3910 |
emphasiseFrom:start with:newEmphasis |
|
3911 |
"set to the emphasis within some range. return the receiver" |
|
3912 |
||
3913 |
^ self emphasiseFrom:start to:(self size) with:newEmphasis |
|
3914 |
||
3915 |
" |
|
12177 | 3916 |
'hello' emphasiseFrom:2 with:#italic |
11919 | 3917 |
|
3918 |
Transcript showCR:('hello' emphasiseFrom:2 with:#italic) |
|
3919 |
" |
|
3920 |
! |
|
3921 |
||
3404 | 3922 |
emphasizeAllWith:emphasis |
12326
c67f12213ddb
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
12319
diff
changeset
|
3923 |
"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
|
3924 |
|
c67f12213ddb
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
12319
diff
changeset
|
3925 |
"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
|
3926 |
Text isNil ifTrue:[^ self]. |
3404 | 3927 |
^ self asText emphasizeAllWith:emphasis |
3928 |
||
3929 |
" |
|
3930 |
Transcript showCR:('hello' emphasizeAllWith:#bold) |
|
3599 | 3931 |
Transcript showCR:('hello' emphasizeAllWith:(#color -> Color red)) |
3932 |
Transcript showCR:('hello' emphasizeAllWith:(#color -> Color red)) |
|
3933 |
" |
|
3934 |
||
3935 |
"Modified: / 17.6.1998 / 12:51:44 / cg" |
|
5097 | 3936 |
! |
3937 |
||
3938 |
makeSelectorBoldIn:aClass |
|
14050 | 3939 |
"the receiver represents some source code for a method in aClass. |
5097 | 3940 |
Change myself to boldify the selector. |
14050 | 3941 |
Not yet implemented (could easily use the syntaxHighlighter for this ...). |
3942 |
For protocol compatibility with other smalltalks" |
|
5097 | 3943 |
|
3944 |
^ self |
|
3945 |
||
14050 | 3946 |
"Created: / 13-12-1999 / 21:49:24 / cg" |
3947 |
! |
|
3948 |
||
16697 | 3949 |
withColor:aColor |
3950 |
"return a text object representing the receiver, but all colorized" |
|
3951 |
||
3952 |
^ self colorizeAllWith:aColor |
|
3953 |
||
3954 |
" |
|
3955 |
Transcript showCR:('hello' withColor:Color red) |
|
3956 |
Transcript showCR:('world' withColor:Color green darkened) |
|
3957 |
" |
|
3958 |
! |
|
3959 |
||
14050 | 3960 |
withoutAnyColorEmphasis |
3961 |
"for protocol compatibility with Text" |
|
3962 |
||
3963 |
^ self |
|
3964 |
||
3965 |
"Created: / 06-03-2012 / 18:15:38 / cg" |
|
6653 | 3966 |
! |
3967 |
||
12396 | 3968 |
withoutBackgroundColorEmphasis |
14050 | 3969 |
"for protocol compatibility with Text" |
3970 |
||
12396 | 3971 |
^ self |
14050 | 3972 |
|
3973 |
"Modified (comment): / 06-03-2012 / 18:14:27 / cg" |
|
12396 | 3974 |
! |
3975 |
||
6653 | 3976 |
withoutEmphasis:emphasisToRemove |
14050 | 3977 |
"for protocol compatibility with Text" |
3978 |
||
6653 | 3979 |
^ self |
14050 | 3980 |
|
3981 |
"Modified (comment): / 06-03-2012 / 18:14:29 / cg" |
|
12396 | 3982 |
! |
3983 |
||
3984 |
withoutForegroundColorEmphasis |
|
14050 | 3985 |
"for protocol compatibility with Text" |
3986 |
||
12396 | 3987 |
^ self |
14050 | 3988 |
|
3989 |
"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
|
3990 |
! ! |
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
3991 |
|
7265 | 3992 |
!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
|
3993 |
|
12767
17d2c7268065
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12679
diff
changeset
|
3994 |
decodeAsLiteralArray |
17d2c7268065
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12679
diff
changeset
|
3995 |
"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
|
3996 |
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
|
3997 |
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
|
3998 |
|
17d2c7268065
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12679
diff
changeset
|
3999 |
^ self |
17d2c7268065
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12679
diff
changeset
|
4000 |
! |
17d2c7268065
Prepare for changing superclass of CharacterArray from ByteArray to UninterpretedBytes
Stefan Vogel <sv@exept.de>
parents:
12679
diff
changeset
|
4001 |
|
991
f0b45a4b82d2
first attempt in encoding/decoding for different character representations (mac/msdos etc.)
Claus Gittinger <cg@exept.de>
parents:
986
diff
changeset
|
4002 |
decodeFrom:encodingSymbol |
8874 | 4003 |
"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
|
4004 |
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
|
4005 |
|
ca774158b317
code cleanup (removed old obsolete encoding stuff)
Claus Gittinger <cg@exept.de>
parents:
8102
diff
changeset
|
4006 |
|myEncoding encoder| |
7950 | 4007 |
|
4008 |
encodingSymbol isNil ifTrue:[^ self]. |
|
4009 |
myEncoding := self encoding. |
|
4010 |
encodingSymbol == myEncoding ifTrue:[^ self]. |
|
4011 |
||
4012 |
encoder := CharacterEncoder encoderToEncodeFrom:(self encoding) into:encodingSymbol. |
|
8124
ca774158b317
code cleanup (removed old obsolete encoding stuff)
Claus Gittinger <cg@exept.de>
parents:
8102
diff
changeset
|
4013 |
encoder isNil ifTrue:[^ self]. |
ca774158b317
code cleanup (removed old obsolete encoding stuff)
Claus Gittinger <cg@exept.de>
parents:
8102
diff
changeset
|
4014 |
^ encoder decodeString:self. |
991
f0b45a4b82d2
first attempt in encoding/decoding for different character representations (mac/msdos etc.)
Claus Gittinger <cg@exept.de>
parents:
986
diff
changeset
|
4015 |
! |
f0b45a4b82d2
first attempt in encoding/decoding for different character representations (mac/msdos etc.)
Claus Gittinger <cg@exept.de>
parents:
986
diff
changeset
|
4016 |
|
9192 | 4017 |
encodeFrom:oldEncoding into:newEncoding |
4018 |
^ CharacterEncoder encodeString:self from:oldEncoding into:newEncoding |
|
14405
1d0d99a95bba
comment/format in:7 methods
Claus Gittinger <cg@exept.de>
parents:
14168
diff
changeset
|
4019 |
|
1d0d99a95bba
comment/format in:7 methods
Claus Gittinger <cg@exept.de>
parents:
14168
diff
changeset
|
4020 |
" |
1d0d99a95bba
comment/format in:7 methods
Claus Gittinger <cg@exept.de>
parents:
14168
diff
changeset
|
4021 |
'äüö' encodeFrom:#iso8859 into:#utf8 |
1d0d99a95bba
comment/format in:7 methods
Claus Gittinger <cg@exept.de>
parents:
14168
diff
changeset
|
4022 |
('äüö' encodeFrom:#iso8859 into:#utf8) encodeFrom:#utf8 into:#unicode |
1d0d99a95bba
comment/format in:7 methods
Claus Gittinger <cg@exept.de>
parents:
14168
diff
changeset
|
4023 |
" |
9192 | 4024 |
! |
4025 |
||
4026 |
rot13 |
|
8874 | 4027 |
"Usenet: from `rotate alphabet 13 places'] |
6835
bdb2cb03200c
added utf8Decode with 8-bit chars only
Claus Gittinger <cg@exept.de>
parents:
6834
diff
changeset
|
4028 |
The simple Caesar-cypher encryption that replaces each English |
8874 | 4029 |
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
|
4030 |
so that 'The butler did it!!' becomes 'Gur ohgyre qvq vg!!' |
8874 | 4031 |
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
|
4032 |
It is used to enclose the text in a sealed wrapper that the reader must choose |
8874 | 4033 |
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
|
4034 |
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
|
4035 |
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
|
4036 |
|
11865 | 4037 |
^ self rot:13 |
8874 | 4038 |
|
4039 |
" |
|
4040 |
'hello world' rot13 |
|
4041 |
'hello world' rot13 rot13 |
|
6835
bdb2cb03200c
added utf8Decode with 8-bit chars only
Claus Gittinger <cg@exept.de>
parents:
6834
diff
changeset
|
4042 |
" |
bdb2cb03200c
added utf8Decode with 8-bit chars only
Claus Gittinger <cg@exept.de>
parents:
6834
diff
changeset
|
4043 |
! |
bdb2cb03200c
added utf8Decode with 8-bit chars only
Claus Gittinger <cg@exept.de>
parents:
6834
diff
changeset
|
4044 |
|
11865 | 4045 |
rot:n |
4046 |
"Usenet: from `rotate alphabet N places'] |
|
4047 |
The simple Caesar-cypher encryption that replaces each English |
|
4048 |
letter with the one N places forward or back along the alphabet, |
|
4049 |
so that 'The butler did it!!' becomes 'Gur ohgyre qvq vg!!' by rot 13 |
|
4050 |
Most Usenet news reading and posting programs include a rot13 feature. |
|
4051 |
It is used to enclose the text in a sealed wrapper that the reader must choose |
|
4052 |
to open -- e.g., for posting things that might offend some readers, or spoilers. |
|
4053 |
A major advantage of rot13 over rot(N) for other N is that it |
|
4054 |
is self-inverse, so the same code can be used for encoding and decoding." |
|
4055 |
||
4056 |
^ self species |
|
16202 | 4057 |
streamContents:[:aStream | |
4058 |
self do:[:char | |
|
4059 |
aStream nextPut:(char rot:n) ]] |
|
11865 | 4060 |
|
4061 |
" |
|
4062 |
'hello world' rot:13 |
|
4063 |
('hello world' rot:13) rot:13 |
|
4064 |
" |
|
4065 |
! |
|
4066 |
||
15656 | 4067 |
utf16Encoded |
4068 |
"Return my UTF-16 representation as a new TwoByteString" |
|
4069 |
||
4070 |
|s| |
|
4071 |
||
4072 |
s := WriteStream on:(TwoByteString uninitializedNew:self size). |
|
4073 |
s nextPutAllUtf16:self. |
|
4074 |
^ s contents |
|
4075 |
||
4076 |
" |
|
4077 |
'abcde1234' utf16Encoded |
|
4078 |
'abcdeäöüß' utf16Encoded |
|
4079 |
" |
|
4080 |
||
4081 |
"Modified: / 11-05-2010 / 19:12:37 / cg" |
|
4082 |
! |
|
4083 |
||
6835
bdb2cb03200c
added utf8Decode with 8-bit chars only
Claus Gittinger <cg@exept.de>
parents:
6834
diff
changeset
|
4084 |
utf8Decoded |
7950 | 4085 |
"Interpreting myself as an UTF-8 representation, decode and return the decoded string." |
6835
bdb2cb03200c
added utf8Decode with 8-bit chars only
Claus Gittinger <cg@exept.de>
parents:
6834
diff
changeset
|
4086 |
|
14592 | 4087 |
|in out| |
4088 |
||
14606 | 4089 |
(self isWideString or:[self contains8BitCharacters]) ifFalse:[ |
14592 | 4090 |
"speed up common case" |
4091 |
^ self. |
|
4092 |
]. |
|
11326
11ce7ae19315
changed #utf8Decoded - use CharacterWriteStream
Stefan Vogel <sv@exept.de>
parents:
11320
diff
changeset
|
4093 |
out := CharacterWriteStream on:(String uninitializedNew:self size). |
6835
bdb2cb03200c
added utf8Decode with 8-bit chars only
Claus Gittinger <cg@exept.de>
parents:
6834
diff
changeset
|
4094 |
in := self readStream. |
bdb2cb03200c
added utf8Decode with 8-bit chars only
Claus Gittinger <cg@exept.de>
parents:
6834
diff
changeset
|
4095 |
[in atEnd] whileFalse:[ |
14592 | 4096 |
out nextPut:(Character utf8DecodeFrom:in). |
8874 | 4097 |
]. |
6835
bdb2cb03200c
added utf8Decode with 8-bit chars only
Claus Gittinger <cg@exept.de>
parents:
6834
diff
changeset
|
4098 |
^ out contents |
bdb2cb03200c
added utf8Decode with 8-bit chars only
Claus Gittinger <cg@exept.de>
parents:
6834
diff
changeset
|
4099 |
|
bdb2cb03200c
added utf8Decode with 8-bit chars only
Claus Gittinger <cg@exept.de>
parents:
6834
diff
changeset
|
4100 |
" |
8874 | 4101 |
#[16rC8 16rA0] asString utf8Decoded |
4102 |
(Character value:16r220) utf8Encoded |
|
4103 |
(Character value:16r220) utf8Encoded utf8Decoded |
|
4104 |
||
4105 |
(Character value:16r800) utf8Encoded |
|
4106 |
(Character value:16r220) utf8Encoded utf8Decoded |
|
6835
bdb2cb03200c
added utf8Decode with 8-bit chars only
Claus Gittinger <cg@exept.de>
parents:
6834
diff
changeset
|
4107 |
" |
bdb2cb03200c
added utf8Decode with 8-bit chars only
Claus Gittinger <cg@exept.de>
parents:
6834
diff
changeset
|
4108 |
|
bdb2cb03200c
added utf8Decode with 8-bit chars only
Claus Gittinger <cg@exept.de>
parents:
6834
diff
changeset
|
4109 |
"test: |
bdb2cb03200c
added utf8Decode with 8-bit chars only
Claus Gittinger <cg@exept.de>
parents:
6834
diff
changeset
|
4110 |
|
bdb2cb03200c
added utf8Decode with 8-bit chars only
Claus Gittinger <cg@exept.de>
parents:
6834
diff
changeset
|
4111 |
|utf8Encoding original readBack| |
bdb2cb03200c
added utf8Decode with 8-bit chars only
Claus Gittinger <cg@exept.de>
parents:
6834
diff
changeset
|
4112 |
|
bdb2cb03200c
added utf8Decode with 8-bit chars only
Claus Gittinger <cg@exept.de>
parents:
6834
diff
changeset
|
4113 |
1 to:16rFFFF do:[:ascii | |
14592 | 4114 |
original := (Character value:ascii) asString. |
4115 |
utf8Encoding := original utf8Encoded. |
|
4116 |
readBack := utf8Encoding utf8Decoded. |
|
4117 |
readBack = original ifFalse:[ |
|
4118 |
self halt |
|
4119 |
] |
|
6835
bdb2cb03200c
added utf8Decode with 8-bit chars only
Claus Gittinger <cg@exept.de>
parents:
6834
diff
changeset
|
4120 |
] |
bdb2cb03200c
added utf8Decode with 8-bit chars only
Claus Gittinger <cg@exept.de>
parents:
6834
diff
changeset
|
4121 |
" |
9192 | 4122 |
! |
4123 |
||
4124 |
utf8DecodedWithTwoByteCharactersReplacedBy:replacementCharacter |
|
6835
bdb2cb03200c
added utf8Decode with 8-bit chars only
Claus Gittinger <cg@exept.de>
parents:
6834
diff
changeset
|
4125 |
"Interpreting myself as an UTF-8 representation, decode and return |
bdb2cb03200c
added utf8Decode with 8-bit chars only
Claus Gittinger <cg@exept.de>
parents:
6834
diff
changeset
|
4126 |
the decoded string. Suppress all 2-byte (above 16rFF) characters, |
bdb2cb03200c
added utf8Decode with 8-bit chars only
Claus Gittinger <cg@exept.de>
parents:
6834
diff
changeset
|
4127 |
and replace them with replacementCharacter" |
bdb2cb03200c
added utf8Decode with 8-bit chars only
Claus Gittinger <cg@exept.de>
parents:
6834
diff
changeset
|
4128 |
|
bdb2cb03200c
added utf8Decode with 8-bit chars only
Claus Gittinger <cg@exept.de>
parents:
6834
diff
changeset
|
4129 |
|in out c| |
bdb2cb03200c
added utf8Decode with 8-bit chars only
Claus Gittinger <cg@exept.de>
parents:
6834
diff
changeset
|
4130 |
|
8222
f2c454a9a038
replaced '' writeStream by String writeStream
Claus Gittinger <cg@exept.de>
parents:
8124
diff
changeset
|
4131 |
out := WriteStream on:(String uninitializedNew:self size). |
6835
bdb2cb03200c
added utf8Decode with 8-bit chars only
Claus Gittinger <cg@exept.de>
parents:
6834
diff
changeset
|
4132 |
in := self readStream. |
bdb2cb03200c
added utf8Decode with 8-bit chars only
Claus Gittinger <cg@exept.de>
parents:
6834
diff
changeset
|
4133 |
[in atEnd] whileFalse:[ |
16202 | 4134 |
c := Character utf8DecodeFrom:in. |
4135 |
c codePoint > 16rFF ifTrue:[ |
|
4136 |
c := replacementCharacter |
|
4137 |
]. |
|
4138 |
out nextPut:c. |
|
8874 | 4139 |
]. |
6835
bdb2cb03200c
added utf8Decode with 8-bit chars only
Claus Gittinger <cg@exept.de>
parents:
6834
diff
changeset
|
4140 |
^ out contents |
bdb2cb03200c
added utf8Decode with 8-bit chars only
Claus Gittinger <cg@exept.de>
parents:
6834
diff
changeset
|
4141 |
|
bdb2cb03200c
added utf8Decode with 8-bit chars only
Claus Gittinger <cg@exept.de>
parents:
6834
diff
changeset
|
4142 |
" |
8874 | 4143 |
(Character value:16r220) utf8Encoded |
16202 | 4144 |
utf8DecodedWithTwoByteCharactersReplacedBy:(Character space) |
9192 | 4145 |
" |
4146 |
! |
|
4147 |
||
4148 |
utf8Encoded |
|
4149 |
"Return my UTF-8 representation as a new String" |
|
4150 |
||
14592 | 4151 |
|s sz| |
4152 |
||
14606 | 4153 |
(self isWideString or:[self contains8BitCharacters]) ifFalse:[ |
14592 | 4154 |
"speed up common case" |
4155 |
^ self. |
|
4156 |
]. |
|
4157 |
"We already know, that we need more space, just a rough estimation" |
|
4158 |
sz := self size. |
|
4159 |
s := WriteStream on:(String uninitializedNew:(sz+2+(sz//3))). |
|
11320 | 4160 |
s nextPutAllUtf8:self. |
9192 | 4161 |
^ s contents |
11320 | 4162 |
|
4163 |
" |
|
14592 | 4164 |
'abcde1234' utf8Encoded |
12925
5c0a9a633ca1
comment/format in: #utf8Encoded
Claus Gittinger <cg@exept.de>
parents:
12912
diff
changeset
|
4165 |
'abcdeäöüß' utf8Encoded |
5c0a9a633ca1
comment/format in: #utf8Encoded
Claus Gittinger <cg@exept.de>
parents:
12912
diff
changeset
|
4166 |
" |
5c0a9a633ca1
comment/format in: #utf8Encoded
Claus Gittinger <cg@exept.de>
parents:
12912
diff
changeset
|
4167 |
|
5c0a9a633ca1
comment/format in: #utf8Encoded
Claus Gittinger <cg@exept.de>
parents:
12912
diff
changeset
|
4168 |
"Modified: / 11-05-2010 / 19:12:37 / cg" |
9192 | 4169 |
! ! |
4170 |
||
11345 | 4171 |
|
12111 | 4172 |
!CharacterArray methodsFor:'matching - glob expressions'! |
4173 |
||
4174 |
compoundMatch:aString |
|
4175 |
"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
|
4176 |
consisting of multiple simple GLOB patterns, separated by semicolons. |
12111 | 4177 |
This is usable with fileName pattern fields. |
4178 |
||
4179 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
15734 | 4180 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
4181 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 4182 |
NOTICE: the receiver is the match pattern" |
4183 |
||
15890 | 4184 |
^ self compoundMatch:aString caseSensitive:true withoutSeparators:false |
12111 | 4185 |
|
4186 |
" |
|
4187 |
'f*' match:'foo' |
|
4188 |
'b*' match:'foo' |
|
4189 |
'f*;b*' match:'foo' |
|
4190 |
'f*;b*' match:'bar' |
|
4191 |
'f*;b*' compoundMatch:'foo' |
|
4192 |
'f*;b*' compoundMatch:'bar' |
|
4193 |
" |
|
4194 |
||
4195 |
"Modified: / 30.1.1998 / 11:40:18 / stefan" |
|
4196 |
"Modified: / 16.12.1999 / 01:22:08 / cg" |
|
4197 |
! |
|
4198 |
||
15734 | 4199 |
compoundMatch:aString caseSensitive:caseSensitive |
12111 | 4200 |
"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
|
4201 |
consisting of multiple simple GLOB patterns, separated by semicolons. |
12111 | 4202 |
This is usable with fileName pattern fields. |
4203 |
||
4204 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
15734 | 4205 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
4206 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 4207 |
NOTICE: the receiver is the match pattern" |
4208 |
||
15890 | 4209 |
^self compoundMatch:aString caseSensitive:caseSensitive withoutSeparators:false |
4210 |
||
4211 |
" |
|
4212 |
'f*' match:'foo' |
|
4213 |
'b*' match:'foo' |
|
4214 |
'f*;b*' match:'foo' |
|
4215 |
'f*;b*' match:'bar' |
|
4216 |
'f*;b*' compoundMatch:'foo' |
|
4217 |
'f*;b*' compoundMatch:'bar' |
|
4218 |
'f*;b*' compoundMatch:'Foo' caseSensitive:false |
|
4219 |
'f*;b*' compoundMatch:'Bar' caseSensitive:false |
|
4220 |
'f*;b*' compoundMatch:'ccc' caseSensitive:false |
|
4221 |
" |
|
4222 |
||
4223 |
"Modified: / 15.4.1997 / 15:50:33 / cg" |
|
4224 |
"Modified: / 30.1.1998 / 11:40:18 / stefan" |
|
4225 |
"Created: / 16.12.1999 / 01:21:35 / cg" |
|
4226 |
! |
|
4227 |
||
4228 |
compoundMatch:aString caseSensitive:caseSensitive withoutSeparators:withoutSeparators |
|
4229 |
"like match, but the receiver may be a compound match pattern, |
|
4230 |
consisting of multiple simple GLOB patterns, separated by semicolons. |
|
4231 |
If withoutSeparators is true, spaces around individual patterns are stripped off. |
|
4232 |
This is usable with fileName pattern fields. |
|
4233 |
||
4234 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
4235 |
NOT the ST-80 meaning. |
|
4236 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
|
4237 |
NOTICE: the receiver is the match pattern" |
|
4238 |
||
12111 | 4239 |
|matchers| |
4240 |
||
4241 |
matchers := self asCollectionOfSubstringsSeparatedBy:$;. |
|
15890 | 4242 |
withoutSeparators ifTrue:[ matchers := matchers collect:[:each | each withoutSeparators] ]. |
15734 | 4243 |
^ matchers |
4244 |
contains:[:aPattern | |
|
4245 |
aPattern match:aString ignoreCase:caseSensitive not escapeCharacter:nil |
|
4246 |
]. |
|
4247 |
||
4248 |
" |
|
4249 |
'f*' match:'foo' |
|
4250 |
'b*' match:'foo' |
|
4251 |
'f*;b*' match:'foo' |
|
4252 |
'f*;b*' match:'bar' |
|
4253 |
'f*;b*' compoundMatch:'foo' |
|
4254 |
'f*;b*' compoundMatch:'bar' |
|
4255 |
'f*;b*' compoundMatch:'Foo' caseSensitive:false |
|
4256 |
'f*;b*' compoundMatch:'Bar' caseSensitive:false |
|
4257 |
'f*;b*' compoundMatch:'ccc' caseSensitive:false |
|
15890 | 4258 |
|
4259 |
'f* ; b*' compoundMatch:'foo' |
|
4260 |
'f* ; b*' compoundMatch:'foo' caseSensitive:true withoutSeparators:true |
|
15734 | 4261 |
" |
4262 |
||
4263 |
"Modified: / 15.4.1997 / 15:50:33 / cg" |
|
4264 |
"Modified: / 30.1.1998 / 11:40:18 / stefan" |
|
4265 |
"Created: / 16.12.1999 / 01:21:35 / cg" |
|
4266 |
! |
|
4267 |
||
4268 |
compoundMatch:aString ignoreCase:ignoreCase |
|
4269 |
<resource: #obsolete> |
|
4270 |
||
4271 |
"like match, but the receiver may be a compound match pattern, |
|
4272 |
consisting of multiple simple GLOB patterns, separated by semicolons. |
|
4273 |
This is usable with fileName pattern fields. |
|
4274 |
||
4275 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
4276 |
NOT the ST-80 meaning. |
|
4277 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
|
4278 |
NOTICE: the receiver is the match pattern" |
|
4279 |
||
4280 |
^ self compoundMatch:aString caseSensitive:ignoreCase not |
|
12111 | 4281 |
|
4282 |
" |
|
4283 |
'f*' match:'foo' |
|
4284 |
'b*' match:'foo' |
|
4285 |
'f*;b*' match:'foo' |
|
4286 |
'f*;b*' match:'bar' |
|
4287 |
'f*;b*' compoundMatch:'foo' |
|
4288 |
'f*;b*' compoundMatch:'bar' |
|
4289 |
'f*;b*' compoundMatch:'Foo' ignoreCase:true |
|
4290 |
'f*;b*' compoundMatch:'Bar' ignoreCase:true |
|
4291 |
'f*;b*' compoundMatch:'ccc' ignoreCase:true |
|
4292 |
" |
|
4293 |
||
4294 |
"Modified: / 15.4.1997 / 15:50:33 / cg" |
|
4295 |
"Modified: / 30.1.1998 / 11:40:18 / stefan" |
|
4296 |
"Created: / 16.12.1999 / 01:21:35 / cg" |
|
4297 |
! |
|
4298 |
||
4299 |
findMatchString:matchString |
|
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
4300 |
"like findString/indexOfSubCollection, but allowing GLOB match patterns. |
12111 | 4301 |
find matchstring; if found, return the index; |
4302 |
if not found, return 0. |
|
4303 |
||
4304 |
NOTICE: match-meta character interpretation is like in unix-matching, |
|
16202 | 4305 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
4306 |
NOTICE: this GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 4307 |
NOTICE: the argument is the match pattern" |
4308 |
||
4309 |
^ self findMatchString:matchString startingAt:1 ignoreCase:false ifAbsent:0 |
|
4310 |
! |
|
4311 |
||
4312 |
findMatchString:matchString startingAt:index |
|
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
4313 |
"like findString, but allowing GLOB match patterns. |
12111 | 4314 |
find matchstring, starting at index. if found, return the index; |
4315 |
if not found, return 0. |
|
4316 |
||
4317 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
16202 | 4318 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
4319 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 4320 |
NOTICE: the argument is the match pattern" |
4321 |
||
4322 |
^ self findMatchString:matchString startingAt:index ignoreCase:false ifAbsent:0 |
|
4323 |
! |
|
4324 |
||
15734 | 4325 |
findMatchString:matchString startingAt:index caseSensitive:caseSensitive ifAbsent:exceptionBlock |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
4326 |
"like findString, but allowing GLOB match patterns. |
12111 | 4327 |
find matchstring, starting at index. if found, return the index; |
4328 |
if not found, return the result of evaluating exceptionBlock. |
|
4329 |
This is a q&d hack - not very efficient. |
|
4330 |
||
4331 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
14420
b388916e6aed
comment/format in: #findMatchString:startingAt:ignoreCase:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
14405
diff
changeset
|
4332 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
4333 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 4334 |
NOTICE: the argument is the match pattern" |
4335 |
||
4336 |
|firstChar firstSet |
|
4337 |
startIndex "{ Class: SmallInteger }" |
|
4338 |
matchSize "{ Class: SmallInteger }" |
|
4339 |
mySize "{ Class: SmallInteger }" |
|
4340 |
realMatchString lcChar ucChar| |
|
4341 |
||
4342 |
matchSize := matchString size. |
|
4343 |
matchSize == 0 ifTrue:[^ index]. "empty string matches" |
|
4344 |
||
4345 |
realMatchString := matchString. |
|
4346 |
(realMatchString endsWith:$*) ifFalse:[ |
|
14420
b388916e6aed
comment/format in: #findMatchString:startingAt:ignoreCase:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
14405
diff
changeset
|
4347 |
realMatchString := realMatchString , '*'. |
b388916e6aed
comment/format in: #findMatchString:startingAt:ignoreCase:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
14405
diff
changeset
|
4348 |
matchSize := matchSize + 1 |
12111 | 4349 |
]. |
4350 |
||
4351 |
mySize := self size. |
|
4352 |
firstChar := realMatchString at:1. |
|
4353 |
firstChar == self class matchEscapeCharacter ifTrue:[ |
|
14420
b388916e6aed
comment/format in: #findMatchString:startingAt:ignoreCase:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
14405
diff
changeset
|
4354 |
firstChar := realMatchString at:2. |
12111 | 4355 |
]. |
4356 |
||
4357 |
firstChar asString includesMatchCharacters ifTrue:[ |
|
14420
b388916e6aed
comment/format in: #findMatchString:startingAt:ignoreCase:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
14405
diff
changeset
|
4358 |
index to:mySize do:[:col | |
15734 | 4359 |
(realMatchString match:self from:col to:mySize caseSensitive:caseSensitive) |
14420
b388916e6aed
comment/format in: #findMatchString:startingAt:ignoreCase:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
14405
diff
changeset
|
4360 |
ifTrue:[^ col] |
b388916e6aed
comment/format in: #findMatchString:startingAt:ignoreCase:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
14405
diff
changeset
|
4361 |
]. |
b388916e6aed
comment/format in: #findMatchString:startingAt:ignoreCase:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
14405
diff
changeset
|
4362 |
^ exceptionBlock value. |
12111 | 4363 |
]. |
4364 |
||
4365 |
lcChar := firstChar asLowercase. |
|
4366 |
ucChar := firstChar asUppercase. |
|
15734 | 4367 |
(caseSensitive not and:[ lcChar ~= ucChar ]) ifTrue:[ |
14420
b388916e6aed
comment/format in: #findMatchString:startingAt:ignoreCase:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
14405
diff
changeset
|
4368 |
firstSet := Array with:ucChar with:lcChar. |
b388916e6aed
comment/format in: #findMatchString:startingAt:ignoreCase:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
14405
diff
changeset
|
4369 |
startIndex := self indexOfAny:firstSet startingAt:index. |
12111 | 4370 |
] ifFalse:[ |
14420
b388916e6aed
comment/format in: #findMatchString:startingAt:ignoreCase:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
14405
diff
changeset
|
4371 |
startIndex := self indexOf:firstChar startingAt:index. |
12111 | 4372 |
]. |
4373 |
[startIndex == 0] whileFalse:[ |
|
15734 | 4374 |
(realMatchString match:self from:startIndex to:mySize caseSensitive:caseSensitive) |
14420
b388916e6aed
comment/format in: #findMatchString:startingAt:ignoreCase:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
14405
diff
changeset
|
4375 |
ifTrue:[^ startIndex]. |
b388916e6aed
comment/format in: #findMatchString:startingAt:ignoreCase:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
14405
diff
changeset
|
4376 |
|
b388916e6aed
comment/format in: #findMatchString:startingAt:ignoreCase:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
14405
diff
changeset
|
4377 |
firstSet notNil ifTrue:[ |
b388916e6aed
comment/format in: #findMatchString:startingAt:ignoreCase:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
14405
diff
changeset
|
4378 |
startIndex := self indexOfAny:firstSet startingAt:(startIndex + 1). |
b388916e6aed
comment/format in: #findMatchString:startingAt:ignoreCase:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
14405
diff
changeset
|
4379 |
] ifFalse:[ |
b388916e6aed
comment/format in: #findMatchString:startingAt:ignoreCase:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
14405
diff
changeset
|
4380 |
startIndex := self indexOf:firstChar startingAt:(startIndex + 1). |
b388916e6aed
comment/format in: #findMatchString:startingAt:ignoreCase:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
14405
diff
changeset
|
4381 |
]. |
12111 | 4382 |
]. |
4383 |
^ exceptionBlock value |
|
4384 |
||
4385 |
" |
|
4386 |
'one two three four' findMatchString:'o[nu]' |
|
4387 |
'one two three four' findMatchString:'o[nu]' startingAt:3 |
|
15734 | 4388 |
'one two three four one' findMatchString:'ONE' startingAt:3 caseSensitive:false ifAbsent:0 |
4389 |
'one two three four one' findMatchString:'ONE' startingAt:3 caseSensitive:true ifAbsent:0 |
|
4390 |
'one two three four ONE' findMatchString:'O#E' startingAt:1 caseSensitive:false ifAbsent:0 |
|
4391 |
'one two three four ONE' findMatchString:'O#E' startingAt:1 caseSensitive:true ifAbsent:0 |
|
4392 |
12345678901234567890 |
|
4393 |
" |
|
4394 |
||
4395 |
"Modified: 13.9.1997 / 06:31:22 / cg" |
|
4396 |
! |
|
4397 |
||
4398 |
findMatchString:matchString startingAt:index ignoreCase:ignoreCase ifAbsent:exceptionBlock |
|
4399 |
<resource: #obsolete> |
|
4400 |
"like findString, but allowing GLOB match patterns. |
|
4401 |
find matchstring, starting at index. if found, return the index; |
|
4402 |
if not found, return the result of evaluating exceptionBlock. |
|
4403 |
This is a q&d hack - not very efficient. |
|
4404 |
||
4405 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
4406 |
NOT the ST-80 meaning. |
|
4407 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
|
4408 |
NOTICE: the argument is the match pattern" |
|
4409 |
||
4410 |
^ self |
|
4411 |
findMatchString:matchString startingAt:index caseSensitive:ignoreCase not ifAbsent:exceptionBlock |
|
4412 |
||
4413 |
" |
|
4414 |
'one two three four' findMatchString:'o[nu]' |
|
4415 |
'one two three four' findMatchString:'o[nu]' startingAt:3 |
|
12111 | 4416 |
'one two three four one' findMatchString:'ONE' startingAt:3 ignoreCase:true ifAbsent:0 |
4417 |
" |
|
4418 |
||
4419 |
"Modified: 13.9.1997 / 06:31:22 / cg" |
|
4420 |
! |
|
4421 |
||
4422 |
includesMatchString:matchString |
|
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
4423 |
"like includesString, but allowing GLOB match patterns. |
12111 | 4424 |
find matchstring; if found, return true, otherwise return false. |
4425 |
||
4426 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
16202 | 4427 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
4428 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 4429 |
NOTICE: the argument is the match pattern" |
4430 |
||
4431 |
^ (self findMatchString:matchString) ~~ 0 |
|
4432 |
||
4433 |
" |
|
4434 |
'hello world' includesMatchString:'h*' |
|
4435 |
'hello world' includesMatchString:'h[aeiou]llo' |
|
4436 |
'hello world' includesMatchString:'wor*' |
|
4437 |
'hello world' includesMatchString:'woR*' |
|
4438 |
" |
|
4439 |
! |
|
4440 |
||
4441 |
includesMatchString:matchString caseSensitive:caseSensitive |
|
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
4442 |
"like includesString, but allowing GLOB match patterns. |
12111 | 4443 |
find matchstring; if found, return true, otherwise return false. |
4444 |
||
4445 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
16202 | 4446 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
4447 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 4448 |
NOTICE: the argument is the match pattern" |
4449 |
||
4450 |
^ (self findMatchString:matchString startingAt:1 ignoreCase:caseSensitive not ifAbsent:0) ~~ 0 |
|
4451 |
||
4452 |
" |
|
4453 |
'hello world' includesMatchString:'h*' caseSensitive:true |
|
4454 |
'hello world' includesMatchString:'h*' caseSensitive:false |
|
4455 |
'Hello world' includesMatchString:'h*' caseSensitive:true |
|
4456 |
'Hello world' includesMatchString:'h*' caseSensitive:false |
|
4457 |
||
4458 |
'hello world' includesMatchString:'h[aeiou]llo' caseSensitive:true |
|
4459 |
'hello world' includesMatchString:'h[aeiou]llo' caseSensitive:false |
|
4460 |
||
4461 |
'hello world' includesMatchString:'wor*' caseSensitive:true |
|
4462 |
'hello world' includesMatchString:'wor*' caseSensitive:false |
|
4463 |
||
4464 |
'hello world' includesMatchString:'woR*' caseSensitive:true |
|
4465 |
'hello world' includesMatchString:'woR*' caseSensitive:false |
|
4466 |
" |
|
4467 |
! |
|
4468 |
||
4469 |
match:aString |
|
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
4470 |
"return true if aString matches self, where self may contain GLOB meta-match |
12111 | 4471 |
characters $* (to match any string) or $# (to match any character). |
4472 |
or [...] to match a set of characters. |
|
4473 |
Lower/uppercase are considered different. |
|
4474 |
The escape character is the backQuote. |
|
4475 |
||
4476 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
15734 | 4477 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
4478 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 4479 |
NOTICE: the receiver is the match pattern" |
4480 |
||
15734 | 4481 |
^ self match:aString from:1 to:aString size caseSensitive:true |
12111 | 4482 |
|
4483 |
" |
|
4484 |
'\*f*' match:'f' |
|
4485 |
'\*f*' match:'*f' |
|
4486 |
'*\*f*' match:'*f' |
|
4487 |
'*f*' match:'*f' |
|
4488 |
'*ute*' match:'computer' |
|
4489 |
'*uter' match:'computer' |
|
4490 |
'uter*' match:'computer' |
|
4491 |
'*ute*' match:'' |
|
4492 |
'[abcd]*' match:'computer' |
|
4493 |
'[abcd]*' match:'komputer' |
|
4494 |
'*some*compl*ern*' match:'this is some more complicated pattern match' |
|
4495 |
'*some*compl*ern*' match:'this is another complicated pattern match' |
|
4496 |
'*-hh' match:'anton-h' |
|
4497 |
" |
|
4498 |
||
4499 |
"Modified: / 9.6.1998 / 18:50:00 / cg" |
|
4500 |
! |
|
4501 |
||
15734 | 4502 |
match:aString caseSensitive:caseSensitive |
4503 |
"return true if aString matches self, where self may contain GLOB meta-match |
|
4504 |
characters $* (to match any string) or $# (to match any character) |
|
4505 |
or [...] to match a set of characters. |
|
4506 |
If caseSensitive is false, lower/uppercase are considered the same. |
|
4507 |
The escape character is the backQuote. |
|
4508 |
||
4509 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
4510 |
NOT the ST-80 meaning. |
|
4511 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
|
4512 |
NOTICE: the receiver is the match pattern" |
|
4513 |
||
4514 |
^ self match:aString from:1 to:aString size caseSensitive:caseSensitive |
|
4515 |
||
4516 |
" |
|
4517 |
'*ute*' match:'COMPUTER' caseSensitive:false |
|
4518 |
'*uter' match:'COMPUTER' caseSensitive:true |
|
4519 |
'[abcd]*' match:'computer' caseSensitive:true |
|
4520 |
'[abcd]*' match:'Computer' caseSensitive:true |
|
4521 |
'[a-k]*' match:'komputer' caseSensitive:true |
|
4522 |
'[a-k]*' match:'zomputer' caseSensitive:true |
|
4523 |
'[a-k]*' match:'Komputer' caseSensitive:true |
|
4524 |
'[a-k]*' match:'Komputer' caseSensitive:false |
|
4525 |
'*some*compl*ern*' match:'this is some more complicated pattern match' caseSensitive:false |
|
4526 |
'*some*compl*ern*' match:'this is another complicated pattern match' caseSensitive:false |
|
4527 |
||
4528 |
Time millisecondsToRun:[ |
|
4529 |
Symbol allInstancesDo:[:sym | |
|
4530 |
'[ab]*' match:sym caseSensitive:true |
|
4531 |
] |
|
4532 |
]. |
|
4533 |
Time millisecondsToRun:[ |
|
4534 |
Symbol allInstancesDo:[:sym | |
|
4535 |
'*at:*' match:sym caseSensitive:true |
|
4536 |
] |
|
4537 |
]. |
|
4538 |
" |
|
4539 |
||
4540 |
"Modified: 2.4.1997 / 17:28:58 / cg" |
|
4541 |
! |
|
4542 |
||
4543 |
match:aString caseSensitive:caseSensitive escapeCharacter:escape |
|
4544 |
"return true if aString matches self, where self may contain GLOB meta-match |
|
4545 |
characters $* (to match any string) or $# (to match any character) |
|
4546 |
or [...] to match a set of characters. |
|
4547 |
If caseSensitive is false, lower/uppercase are considered the same. |
|
4548 |
||
4549 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
4550 |
NOT the ST-80 meaning. |
|
4551 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
|
4552 |
NOTICE: the receiver is the match pattern" |
|
4553 |
||
4554 |
^ self match:aString from:1 to:aString size caseSensitive:caseSensitive escapeCharacter:escape |
|
4555 |
||
4556 |
" |
|
4557 |
'*ute*' match:'COMPUTER' caseSensitive:false |
|
4558 |
'*uter' match:'COMPUTER' caseSensitive:true |
|
4559 |
'[abcd]*' match:'computer' caseSensitive:true |
|
4560 |
'[abcd]*' match:'Computer' caseSensitive:true |
|
4561 |
'[a-k]*' match:'komputer' caseSensitive:true |
|
4562 |
'[a-k]*' match:'zomputer' caseSensitive:true |
|
4563 |
'[a-k]*' match:'Komputer' caseSensitive:true |
|
4564 |
'[a-k]*' match:'Komputer' caseSensitive:false |
|
4565 |
'*some*compl*ern*' match:'this is some more complicated pattern match' caseSensitive:false |
|
4566 |
'*some*compl*ern*' match:'this is another complicated pattern match' caseSensitive:false |
|
4567 |
||
4568 |
Time millisecondsToRun:[ |
|
4569 |
Symbol allInstancesDo:[:sym | |
|
4570 |
'[ab]*' match:sym caseSensitive:true |
|
4571 |
] |
|
4572 |
]. |
|
4573 |
Time millisecondsToRun:[ |
|
4574 |
Symbol allInstancesDo:[:sym | |
|
4575 |
'*at:*' match:sym caseSensitive:true |
|
4576 |
] |
|
4577 |
]. |
|
4578 |
" |
|
4579 |
||
4580 |
"Modified: 2.4.1997 / 17:28:58 / cg" |
|
4581 |
! |
|
4582 |
||
12111 | 4583 |
match:aString escapeCharacter:escape |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
4584 |
"return true if aString matches self, where self may contain GLOB meta-match |
12111 | 4585 |
characters $* (to match any string) or $# (to match any character). |
4586 |
or [...] to match a set of characters. |
|
4587 |
Lower/uppercase are considered different. |
|
4588 |
||
4589 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
16202 | 4590 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
4591 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 4592 |
NOTICE: the receiver is the match pattern" |
4593 |
||
4594 |
^ self match:aString from:1 to:aString size ignoreCase:false escapeCharacter:escape |
|
4595 |
||
4596 |
" |
|
4597 |
'a\b\c\*' match:'a\b\c\d' |
|
4598 |
'a\b\c\*' match:'a\b\c\d' escapeCharacter:nil |
|
4599 |
" |
|
4600 |
! |
|
4601 |
||
15734 | 4602 |
match:aString from:start to:stop caseSensitive:caseSensitive |
4603 |
"return true if part of aString matches myself, |
|
4604 |
where self may contain GLOB meta-match |
|
4605 |
characters $* (to match any string) or $# (to match any character) |
|
4606 |
or [...] to match a set of characters. |
|
4607 |
If caseSensitive is false, lower/uppercase are considered the same. |
|
4608 |
The escape character is the backQuote. |
|
4609 |
||
4610 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
4611 |
NOT the ST-80 meaning. |
|
4612 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
|
4613 |
NOTICE: the receiver is the match pattern" |
|
4614 |
||
4615 |
^ self |
|
4616 |
match:aString from:start to:stop caseSensitive:caseSensitive |
|
4617 |
escapeCharacter:(self class matchEscapeCharacter) |
|
4618 |
||
4619 |
" |
|
4620 |
'*ute*' match:'12345COMPUTER' from:1 to:5 caseSensitive:false |
|
4621 |
'*ute*' match:'12345COMPUTER' from:6 to:13 caseSensitive:false |
|
4622 |
" |
|
4623 |
||
4624 |
"Modified: / 10.11.1998 / 21:43:46 / cg" |
|
4625 |
! |
|
4626 |
||
4627 |
match:aString from:start to:stop caseSensitive:caseSensitive escapeCharacter:escape |
|
4628 |
"return true if part of aString matches myself, |
|
4629 |
where self may contain GLOB meta-match |
|
4630 |
characters $* (to match any string) or $# (to match any character) |
|
4631 |
or [...] to match a set of characters. |
|
4632 |
If caseSensitive is false, lower/uppercase are considered the same. |
|
4633 |
||
4634 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
4635 |
NOT the ST-80 meaning. |
|
4636 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
|
4637 |
NOTICE: the receiver is the match pattern" |
|
4638 |
||
4639 |
|matchScanArray| |
|
4640 |
||
4641 |
" |
|
4642 |
keep the matchScanArray from the most recent match - |
|
4643 |
avoids parsing the pattern over-and over if multiple searches |
|
4644 |
are done with the same pattern. |
|
4645 |
" |
|
4646 |
(PreviousMatch notNil |
|
4647 |
and:[PreviousMatch key = self]) ifTrue:[ |
|
4648 |
matchScanArray := PreviousMatch value |
|
4649 |
] ifFalse:[ |
|
4650 |
matchScanArray := self class matchScanArrayFrom:self escapeCharacter:escape. |
|
4651 |
matchScanArray isNil ifTrue:[ |
|
4652 |
'CharacterArray [info]: invalid matchpattern:''' infoPrint. self infoPrint. ''' comparing for equality.' infoPrintCR. |
|
4653 |
^ self = aString |
|
4654 |
"/ ^ false |
|
4655 |
]. |
|
4656 |
PreviousMatch := self -> matchScanArray. |
|
4657 |
]. |
|
4658 |
||
4659 |
^ self class |
|
4660 |
matchScan:matchScanArray |
|
4661 |
from:1 to:matchScanArray size |
|
4662 |
with:aString |
|
4663 |
from:start to:stop |
|
4664 |
caseSensitive:caseSensitive |
|
4665 |
||
4666 |
" |
|
4667 |
'*ute*' match:'12345COMPUTER' from:1 to:5 caseSensitive:false |
|
4668 |
'*ute*' match:'12345COMPUTER' from:6 to:13 caseSensitive:false |
|
4669 |
" |
|
4670 |
||
4671 |
"Modified: / 10.11.1998 / 21:43:46 / cg" |
|
4672 |
! |
|
4673 |
||
12111 | 4674 |
match:aString from:start to:stop ignoreCase:ignoreCase |
15734 | 4675 |
<resource: #obsolete> |
12111 | 4676 |
"return true if part of aString matches myself, |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
4677 |
where self may contain GLOB meta-match |
12111 | 4678 |
characters $* (to match any string) or $# (to match any character) |
4679 |
or [...] to match a set of characters. |
|
4680 |
If ignoreCase is true, lower/uppercase are considered the same. |
|
4681 |
The escape character is the backQuote. |
|
4682 |
||
4683 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
16202 | 4684 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
4685 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 4686 |
NOTICE: the receiver is the match pattern" |
4687 |
||
4688 |
^ self |
|
16202 | 4689 |
match:aString from:start to:stop ignoreCase:ignoreCase |
4690 |
escapeCharacter:(self class matchEscapeCharacter) |
|
12111 | 4691 |
|
4692 |
" |
|
4693 |
'*ute*' match:'12345COMPUTER' from:1 to:5 ignoreCase:true |
|
4694 |
'*ute*' match:'12345COMPUTER' from:6 to:13 ignoreCase:true |
|
4695 |
" |
|
4696 |
||
4697 |
"Modified: / 10.11.1998 / 21:43:46 / cg" |
|
4698 |
! |
|
4699 |
||
4700 |
match:aString from:start to:stop ignoreCase:ignoreCase escapeCharacter:escape |
|
15734 | 4701 |
<resource: #obsolete> |
12111 | 4702 |
"return true if part of aString matches myself, |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
4703 |
where self may contain GLOB meta-match |
12111 | 4704 |
characters $* (to match any string) or $# (to match any character) |
4705 |
or [...] to match a set of characters. |
|
4706 |
If ignoreCase is true, lower/uppercase are considered the same. |
|
4707 |
||
4708 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
15734 | 4709 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
4710 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 4711 |
NOTICE: the receiver is the match pattern" |
4712 |
||
15734 | 4713 |
^ self |
4714 |
match:aString from:start to:stop |
|
4715 |
caseSensitive:ignoreCase not |
|
4716 |
escapeCharacter:escape |
|
12111 | 4717 |
|
4718 |
" |
|
4719 |
'*ute*' match:'12345COMPUTER' from:1 to:5 ignoreCase:true |
|
4720 |
'*ute*' match:'12345COMPUTER' from:6 to:13 ignoreCase:true |
|
4721 |
" |
|
4722 |
||
4723 |
"Modified: / 10.11.1998 / 21:43:46 / cg" |
|
4724 |
! |
|
4725 |
||
4726 |
match:aString ignoreCase:ignoreCase |
|
15734 | 4727 |
<resource: #obsolete> |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
4728 |
"return true if aString matches self, where self may contain GLOB meta-match |
12111 | 4729 |
characters $* (to match any string) or $# (to match any character) |
4730 |
or [...] to match a set of characters. |
|
4731 |
If ignoreCase is true, lower/uppercase are considered the same. |
|
4732 |
The escape character is the backQuote. |
|
4733 |
||
4734 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
15734 | 4735 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
4736 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 4737 |
NOTICE: the receiver is the match pattern" |
4738 |
||
15734 | 4739 |
"/ leave it this way, in case a customer has redefined it for performance (for a while) |
12111 | 4740 |
^ self match:aString from:1 to:aString size ignoreCase:ignoreCase |
4741 |
||
4742 |
" |
|
4743 |
'*ute*' match:'COMPUTER' ignoreCase:true |
|
4744 |
'*uter' match:'COMPUTER' ignoreCase:false |
|
4745 |
'[abcd]*' match:'computer' ignoreCase:false |
|
4746 |
'[abcd]*' match:'Computer' ignoreCase:false |
|
4747 |
'[a-k]*' match:'komputer' ignoreCase:false |
|
4748 |
'[a-k]*' match:'zomputer' ignoreCase:false |
|
4749 |
'[a-k]*' match:'Komputer' ignoreCase:false |
|
4750 |
'[a-k]*' match:'Komputer' ignoreCase:true |
|
4751 |
'*some*compl*ern*' match:'this is some more complicated pattern match' ignoreCase:true |
|
4752 |
'*some*compl*ern*' match:'this is another complicated pattern match' ignoreCase:true |
|
4753 |
||
4754 |
Time millisecondsToRun:[ |
|
15734 | 4755 |
Symbol allInstancesDo:[:sym | |
4756 |
'[ab]*' match:sym ignoreCase:false |
|
4757 |
] |
|
12111 | 4758 |
]. |
4759 |
Time millisecondsToRun:[ |
|
15734 | 4760 |
Symbol allInstancesDo:[:sym | |
4761 |
'*at:*' match:sym ignoreCase:false |
|
4762 |
] |
|
12111 | 4763 |
]. |
4764 |
" |
|
4765 |
||
4766 |
"Modified: 2.4.1997 / 17:28:58 / cg" |
|
4767 |
! |
|
4768 |
||
4769 |
match:aString ignoreCase:ignoreCase escapeCharacter:escape |
|
15734 | 4770 |
<resource: #obsolete> |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
4771 |
"return true if aString matches self, where self may contain GLOB meta-match |
12111 | 4772 |
characters $* (to match any string) or $# (to match any character) |
4773 |
or [...] to match a set of characters. |
|
4774 |
If ignoreCase is true, lower/uppercase are considered the same. |
|
4775 |
||
4776 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
16202 | 4777 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
4778 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 4779 |
NOTICE: the receiver is the match pattern" |
4780 |
||
4781 |
^ self match:aString from:1 to:aString size ignoreCase:ignoreCase escapeCharacter:escape |
|
4782 |
||
4783 |
" |
|
4784 |
'*ute*' match:'COMPUTER' ignoreCase:true |
|
4785 |
'*uter' match:'COMPUTER' ignoreCase:false |
|
4786 |
'[abcd]*' match:'computer' ignoreCase:false |
|
4787 |
'[abcd]*' match:'Computer' ignoreCase:false |
|
4788 |
'[a-k]*' match:'komputer' ignoreCase:false |
|
4789 |
'[a-k]*' match:'zomputer' ignoreCase:false |
|
4790 |
'[a-k]*' match:'Komputer' ignoreCase:false |
|
4791 |
'[a-k]*' match:'Komputer' ignoreCase:true |
|
4792 |
'*some*compl*ern*' match:'this is some more complicated pattern match' ignoreCase:true |
|
4793 |
'*some*compl*ern*' match:'this is another complicated pattern match' ignoreCase:true |
|
4794 |
||
4795 |
Time millisecondsToRun:[ |
|
16202 | 4796 |
Symbol allInstancesDo:[:sym | |
4797 |
'[ab]*' match:sym ignoreCase:false |
|
4798 |
] |
|
12111 | 4799 |
]. |
4800 |
Time millisecondsToRun:[ |
|
16202 | 4801 |
Symbol allInstancesDo:[:sym | |
4802 |
'*at:*' match:sym ignoreCase:false |
|
4803 |
] |
|
12111 | 4804 |
]. |
4805 |
" |
|
4806 |
||
4807 |
"Modified: 2.4.1997 / 17:28:58 / cg" |
|
4808 |
! |
|
4809 |
||
4810 |
matches:aPatternString |
|
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
4811 |
"return true if the receiver matches aString, where aPatternString may contain GLOB meta-match |
12111 | 4812 |
characters $* (to match any string) or $# (to match any character). |
4813 |
or [...] to match a set of characters. |
|
4814 |
Lower/uppercase are considered different. |
|
4815 |
||
4816 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
16202 | 4817 |
NOT the ST-80 meaning. |
12518
bfdcbf713b43
comment/format in:16 methods
Claus Gittinger <cg@exept.de>
parents:
12396
diff
changeset
|
4818 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
12111 | 4819 |
NOTICE: the receiver is the match pattern" |
4820 |
||
4821 |
^ aPatternString match:self |
|
14054
48d3fab77bd9
added: #matches:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
14053
diff
changeset
|
4822 |
! |
48d3fab77bd9
added: #matches:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
14053
diff
changeset
|
4823 |
|
15734 | 4824 |
matches:aPatternString caseSensitive:caseSensitive |
4825 |
"return true if the receiver matches aString, where aPatternString may contain GLOB meta-match |
|
4826 |
characters $* (to match any string) or $# (to match any character). |
|
4827 |
or [...] to match a set of characters. |
|
4828 |
||
4829 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
|
4830 |
NOT the ST-80 meaning. |
|
4831 |
NOTICE: this is GLOB, which is different from regex matching (see matchesRegex:) |
|
4832 |
NOTICE: the receiver is the match pattern" |
|
4833 |
||
4834 |
^ aPatternString match:self caseSensitive:caseSensitive |
|
4835 |
! |
|
4836 |
||
14054
48d3fab77bd9
added: #matches:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
14053
diff
changeset
|
4837 |
matches:aPatternString ignoreCase:ignoreCase |
15734 | 4838 |
<resource: #obsolete> |
14054
48d3fab77bd9
added: #matches:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
14053
diff
changeset
|
4839 |
"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
|
4840 |
characters $* (to match any string) or $# (to match any character). |
48d3fab77bd9
added: #matches:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
14053
diff
changeset
|
4841 |
or [...] to match a set of characters. |
48d3fab77bd9
added: #matches:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
14053
diff
changeset
|
4842 |
Lower/uppercase are considered different. |
48d3fab77bd9
added: #matches:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
14053
diff
changeset
|
4843 |
|
48d3fab77bd9
added: #matches:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
14053
diff
changeset
|
4844 |
NOTICE: match-meta character interpretation is like in unix-matching (glob), |
48d3fab77bd9
added: #matches:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
14053
diff
changeset
|
4845 |
NOT the ST-80 meaning. |
48d3fab77bd9
added: #matches:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
14053
diff
changeset
|
4846 |
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
|
4847 |
NOTICE: the receiver is the match pattern" |
48d3fab77bd9
added: #matches:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
14053
diff
changeset
|
4848 |
|
15734 | 4849 |
^ aPatternString match:self caseSensitive:ignoreCase not |
14054
48d3fab77bd9
added: #matches:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
14053
diff
changeset
|
4850 |
|
48d3fab77bd9
added: #matches:ignoreCase:
Claus Gittinger <cg@exept.de>
parents:
14053
diff
changeset
|
4851 |
"Created: / 08-03-2012 / 03:11:11 / cg" |
12111 | 4852 |
! ! |
4853 |
||
4854 |
||
9192 | 4855 |
!CharacterArray methodsFor:'padded copying'! |
4856 |
||
4857 |
centerPaddedTo:newSize |
|
4858 |
"return a new string consisting of the receivers characters, |
|
4859 |
plus spaces up to length and center the receivers characters in |
|
4860 |
the resulting string. |
|
4861 |
If the receivers size is equal or greater than the length argument, |
|
4862 |
the original receiver is returned unchanged." |
|
4863 |
||
4864 |
^ self centerPaddedTo:newSize with:(Character space) |
|
4865 |
||
4866 |
" |
|
4867 |
'foo' centerPaddedTo:10 |
|
4868 |
123 printString centerPaddedTo:10 |
|
4869 |
" |
|
4870 |
||
4871 |
"Created: 25.11.1995 / 10:53:57 / cg" |
|
4872 |
! |
|
9190 | 4873 |
|
4874 |
centerPaddedTo:size with:padCharacter |
|
4875 |
"return a new string of length size, which contains the receiver |
|
4876 |
centered (i.e. padded on both sides). |
|
4877 |
Characters are filled with padCharacter. |
|
8874 | 4878 |
If the receivers 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
|
4879 |
the original receiver is returned unchanged." |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
4880 |
|
9190 | 4881 |
|len s| |
4882 |
||
4883 |
len := self size. |
|
9192 | 4884 |
(len < size) ifTrue:[ |
16202 | 4885 |
s := self species new:size withAll:padCharacter. |
4886 |
s replaceFrom:(size - len) // 2 + 1 with:self. |
|
4887 |
^ s |
|
9192 | 4888 |
] |
4889 |
||
9190 | 4890 |
" |
4891 |
'foo' centerPaddedTo:11 with:$. |
|
4892 |
'fooBar' centerPaddedTo:5 with:$. |
|
4893 |
123 printString centerPaddedTo:10 with:$. |
|
4894 |
(' ' , 123 printString) centerPaddedTo:10 with:$. |
|
4895 |
(Float pi printString) centerPaddedTo:15 with:(Character space) |
|
4896 |
(Float pi printString) centerPaddedTo:15 with:$- |
|
4897 |
(' ' , Float pi class name) centerPaddedTo:15 with:$. |
|
4898 |
" |
|
815
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
4899 |
! |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
4900 |
|
9190 | 4901 |
decimalPaddedTo:size and:afterPeriod at:decimalCharacter |
815
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
4902 |
"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
|
4903 |
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
|
4904 |
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
|
4905 |
to be padded). |
8874 | 4906 |
If the receivers 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
|
4907 |
the original receiver is returned unchanged. |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
4908 |
(sounds complicated ? -> see examples below)." |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
4909 |
|
8874 | 4910 |
^ self |
16202 | 4911 |
decimalPaddedTo:size |
4912 |
and:afterPeriod |
|
4913 |
at:decimalCharacter |
|
4914 |
withLeft:(Character space) |
|
4915 |
right:$0 |
|
815
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
4916 |
|
8874 | 4917 |
" |
4918 |
'123' decimalPaddedTo:10 and:3 at:$. -> ' 123 ' |
|
4919 |
'123' decimalPaddedTo:10 and:3 at:$. -> ' 123.000' |
|
4920 |
'123.' decimalPaddedTo:10 and:3 at:$. -> ' 123.000' |
|
815
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
4921 |
'123.1' decimalPaddedTo:10 and:3 at:$. -> ' 123.100' |
8874 | 4922 |
'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
|
4923 |
'123.123' decimalPaddedTo:10 and:3 at:$. -> ' 123.123' |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
4924 |
" |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
4925 |
|
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
4926 |
"Created: 23.12.1995 / 13:11:52 / cg" |
9192 | 4927 |
! |
4928 |
||
4929 |
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
|
4930 |
"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
|
4931 |
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
|
4932 |
of the period. |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
4933 |
Characters on the left are filled with leftPadChar. |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
4934 |
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
|
4935 |
otherwise, if missing, a decimal point is added and right characters filled with this. |
8874 | 4936 |
If the receivers 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
|
4937 |
the original receiver is returned unchanged. |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
4938 |
(sounds complicated ? -> see examples below)." |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
4939 |
|
5230
655c21ffe6cc
care for negative size of rest-string when decimalPadding
Claus Gittinger <cg@exept.de>
parents:
5221
diff
changeset
|
4940 |
|s idx n rest| |
815
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
4941 |
|
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
4942 |
idx := self indexOf:decimalCharacter. |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
4943 |
idx == 0 ifTrue:[ |
16202 | 4944 |
"/ |
4945 |
"/ no decimal point found; adjust string to the left of the period column |
|
4946 |
"/ |
|
4947 |
rightPadChar isNil ifTrue:[ |
|
4948 |
s := self , (self species new:afterPeriod + 1 withAll:leftPadChar) |
|
4949 |
] ifFalse:[ |
|
4950 |
s:= self , decimalCharacter asString , (self species new:afterPeriod withAll:rightPadChar). |
|
4951 |
]. |
|
815
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
4952 |
] ifFalse:[ |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
4953 |
|
16202 | 4954 |
"/ the number of after-decimalPoint characters |
4955 |
n := self size - idx. |
|
4956 |
rest := afterPeriod - n. |
|
4957 |
rest > 0 ifTrue:[ |
|
4958 |
s := (self species new:rest withAll:(rightPadChar ? leftPadChar)). |
|
4959 |
] ifFalse:[ |
|
4960 |
s := '' |
|
4961 |
]. |
|
4962 |
s := self , s. |
|
815
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
4963 |
]. |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
4964 |
|
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
4965 |
^ s leftPaddedTo:size with:leftPadChar |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
4966 |
|
8874 | 4967 |
" |
4968 |
'123' decimalPaddedTo:10 and:3 at:$. withLeft:(Character space) right:nil -> ' 123 ' |
|
4969 |
'123' decimalPaddedTo:10 and:3 at:$. withLeft:(Character space) right:$0 -> ' 123.000' |
|
4970 |
'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
|
4971 |
'123.1' decimalPaddedTo:10 and:3 at:$. withLeft:(Character space) right:$0 -> ' 123.100' |
8874 | 4972 |
'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
|
4973 |
'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
|
4974 |
" |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
4975 |
|
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
4976 |
"Modified: 23.12.1995 / 13:08:18 / cg" |
9192 | 4977 |
! |
4978 |
||
4979 |
leftPaddedTo:size |
|
4980 |
"return a new string of length size, which contains the receiver |
|
4981 |
right-adjusted (i.e. padded on the left). |
|
4982 |
Characters on the left are filled with spaces. |
|
4983 |
If the receivers size is equal or greater than the length argument, |
|
4984 |
the original receiver is returned unchanged." |
|
4985 |
||
4986 |
^ self leftPaddedTo:size with:(Character space) |
|
4987 |
||
4988 |
" |
|
4989 |
'foo' leftPaddedTo:10 |
|
4990 |
'fooBar' leftPaddedTo:5 |
|
4991 |
123 printString leftPaddedTo:10 |
|
4992 |
" |
|
4993 |
! |
|
608 | 4994 |
|
4995 |
paddedTo:newSize |
|
4996 |
"return a new string consisting of the receivers characters, |
|
4997 |
plus spaces up to length. |
|
8874 | 4998 |
If the receivers size is equal or greater than the length argument, |
608 | 4999 |
the original receiver is returned unchanged." |
5000 |
||
5001 |
^ self paddedTo:newSize with:(Character space) |
|
5002 |
||
5003 |
" |
|
8874 | 5004 |
'foo' paddedTo:10 |
5005 |
123 printString paddedTo:10 |
|
15734 | 5006 |
'12345678901234' paddedTo:10 |
608 | 5007 |
" |
64 | 5008 |
! ! |
5009 |
||
9192 | 5010 |
!CharacterArray methodsFor:'printing & storing'! |
608 | 5011 |
|
5012 |
article |
|
5013 |
"return an article string for the receiver." |
|
5014 |
||
5015 |
|firstChar| |
|
5016 |
||
8874 | 5017 |
firstChar := (self at:1) asLowercase. |
5848 | 5018 |
((firstChar isVowel and:[firstChar ~~ $u]) or:[firstChar == $x]) ifTrue:[ |
16202 | 5019 |
^ 'an' |
608 | 5020 |
]. |
5021 |
^ 'a' |
|
5848 | 5022 |
|
5023 |
" |
|
16202 | 5024 |
'uboot' article. |
5025 |
'xmas' article. |
|
5026 |
'alarm' article. |
|
5027 |
'baby' article. |
|
5848 | 5028 |
" |
608 | 5029 |
! |
5030 |
||
8049
ccd0e8f26439
printing - migrating towards unicodeSupport in ST-code
Claus Gittinger <cg@exept.de>
parents:
8040
diff
changeset
|
5031 |
basicStoreString |
ccd0e8f26439
printing - migrating towards unicodeSupport in ST-code
Claus Gittinger <cg@exept.de>
parents:
8040
diff
changeset
|
5032 |
"return a String for storing myself" |
ccd0e8f26439
printing - migrating towards unicodeSupport in ST-code
Claus Gittinger <cg@exept.de>
parents:
8040
diff
changeset
|
5033 |
|
ccd0e8f26439
printing - migrating towards unicodeSupport in ST-code
Claus Gittinger <cg@exept.de>
parents:
8040
diff
changeset
|
5034 |
|s n index| |
ccd0e8f26439
printing - migrating towards unicodeSupport in ST-code
Claus Gittinger <cg@exept.de>
parents:
8040
diff
changeset
|
5035 |
|
ccd0e8f26439
printing - migrating towards unicodeSupport in ST-code
Claus Gittinger <cg@exept.de>
parents:
8040
diff
changeset
|
5036 |
n := self occurrencesOf:$'. |
11320 | 5037 |
n ~~ 0 ifTrue:[ |
15523 | 5038 |
s := self species new:(n + 2 + self size). |
5039 |
s at:1 put:$'. |
|
5040 |
index := 2. |
|
5041 |
self do:[:thisChar | |
|
5042 |
(thisChar == $') ifTrue:[ |
|
5043 |
s at:index put:thisChar. |
|
5044 |
index := index + 1. |
|
5045 |
]. |
|
5046 |
s at:index put:thisChar. |
|
5047 |
index := index + 1. |
|
5048 |
]. |
|
5049 |
s at:index put:$'. |
|
5050 |
^ s |
|
9192 | 5051 |
]. |
11320 | 5052 |
|
9192 | 5053 |
^ '''' , self , '''' |
15523 | 5054 |
|
5055 |
" |
|
15540 | 5056 |
'''immutable'' string' asImmutableString basicStoreString |
5057 |
'immutable string' asImmutableString basicStoreString |
|
15523 | 5058 |
" |
5059 |
||
5060 |
"Modified: / 14-07-2013 / 19:20:49 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
9192 | 5061 |
! |
8049
ccd0e8f26439
printing - migrating towards unicodeSupport in ST-code
Claus Gittinger <cg@exept.de>
parents:
8040
diff
changeset
|
5062 |
|
11774
6f6e2b71b607
displayOn (inspector's interface) was broken
Claus Gittinger <cg@exept.de>
parents:
11754
diff
changeset
|
5063 |
displayOn:aGCOrStream |
6f6e2b71b607
displayOn (inspector's interface) was broken
Claus Gittinger <cg@exept.de>
parents:
11754
diff
changeset
|
5064 |
"Compatibility |
6f6e2b71b607
displayOn (inspector's interface) was broken
Claus Gittinger <cg@exept.de>
parents:
11754
diff
changeset
|
5065 |
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
|
5066 |
OR: |
6f6e2b71b607
displayOn (inspector's interface) was broken
Claus Gittinger <cg@exept.de>
parents:
11754
diff
changeset
|
5067 |
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
|
5068 |
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
|
5069 |
(although the fallBack is to display its printString ...)" |
6f6e2b71b607
displayOn (inspector's interface) was broken
Claus Gittinger <cg@exept.de>
parents:
11754
diff
changeset
|
5070 |
|
6f6e2b71b607
displayOn (inspector's interface) was broken
Claus Gittinger <cg@exept.de>
parents:
11754
diff
changeset
|
5071 |
"/ what a kludge - Dolphin and Squeak mean: printOn: a stream; |
6f6e2b71b607
displayOn (inspector's interface) was broken
Claus Gittinger <cg@exept.de>
parents:
11754
diff
changeset
|
5072 |
"/ ST/X (and some old ST80's) mean: draw-yourself on a GC. |
16738 | 5073 |
(aGCOrStream isStream) ifTrue:[ |
16202 | 5074 |
self storeOn:aGCOrStream. |
5075 |
^ self |
|
11774
6f6e2b71b607
displayOn (inspector's interface) was broken
Claus Gittinger <cg@exept.de>
parents:
11754
diff
changeset
|
5076 |
]. |
6f6e2b71b607
displayOn (inspector's interface) was broken
Claus Gittinger <cg@exept.de>
parents:
11754
diff
changeset
|
5077 |
^ super displayOn:aGCOrStream |
6f6e2b71b607
displayOn (inspector's interface) was broken
Claus Gittinger <cg@exept.de>
parents:
11754
diff
changeset
|
5078 |
! |
6f6e2b71b607
displayOn (inspector's interface) was broken
Claus Gittinger <cg@exept.de>
parents:
11754
diff
changeset
|
5079 |
|
608 | 5080 |
printOn:aStream |
5081 |
"print the receiver on aStream" |
|
5082 |
||
5083 |
aStream nextPutAll:self |
|
5084 |
! |
|
5085 |
||
5086 |
printString |
|
13079
111e7e456648
Implement display with #displayOn: instead of #displayString
Stefan Vogel <sv@exept.de>
parents:
13073
diff
changeset
|
5087 |
"return a string for printing - that's myself" |
608 | 5088 |
|
5089 |
^ self |
|
3578 | 5090 |
! |
5091 |
||
5092 |
printWithQuotesDoubledOn:aStream |
|
5093 |
"put the raw storeString of myself on aStream" |
|
5094 |
||
5095 |
self do:[:thisChar | |
|
16202 | 5096 |
(thisChar == $') ifTrue:[aStream nextPut:thisChar]. |
5097 |
aStream nextPut:thisChar |
|
3578 | 5098 |
] |
5099 |
||
5100 |
"Modified: / 15.6.1998 / 17:21:17 / cg" |
|
5101 |
"Created: / 15.6.1998 / 17:22:13 / cg" |
|
12171 | 5102 |
! |
5103 |
||
15788 | 5104 |
printXmlQuotedOn:aStream |
5105 |
"convert aString to a valid XML string |
|
5106 |
that can be used for attributes, text, comments an PIs |
|
5107 |
TODO: care for 16bit UNICODE string and escape chars ..." |
|
5108 |
||
5109 |
self do:[:eachChar | |
|
5110 |
eachChar == $< ifTrue:[ |
|
5111 |
aStream nextPutAll:'<' "mapping needed for xml text" |
|
5112 |
] ifFalse:[ eachChar == $& ifTrue:[ |
|
5113 |
aStream nextPutAll:'&' "mapping needed for all" |
|
5114 |
] ifFalse:[ eachChar == $> ifTrue:[ |
|
5115 |
aStream nextPutAll:'>' "mapping needed for comments" |
|
5116 |
] ifFalse:[ eachChar == $' ifTrue:[ |
|
5117 |
aStream nextPutAll:''' "mapping needed for attributes" |
|
5118 |
] ifFalse:[ eachChar == $" ifTrue:[ |
|
5119 |
aStream nextPutAll:'"' "mapping needed for attributes" |
|
5120 |
] ifFalse:[ |
|
5121 |
|codePoint| |
|
5122 |
codePoint := eachChar codePoint. |
|
5123 |
(codePoint < 16r20 or:[codePoint >= 16r7F]) ifTrue:[ |
|
5124 |
aStream nextPutAll:'&#'. |
|
5125 |
codePoint printOn:aStream. |
|
5126 |
aStream nextPut:$;. |
|
5127 |
] ifFalse:[ |
|
5128 |
aStream nextPut:eachChar |
|
5129 |
]]]]]] |
|
5130 |
]. |
|
5131 |
! |
|
5132 |
||
5133 |
printXmlTextQuotedOn:aStream |
|
5134 |
"convert aString to a valid XML string |
|
5135 |
that can be used for XML text. |
|
5136 |
Here line formatting characters are not escaped. |
|
5137 |
TODO: care for 16bit UNICODE string and escape chars ..." |
|
5138 |
||
5139 |
self do:[:eachChar | |
|
5140 |
eachChar == $< ifTrue:[ |
|
5141 |
aStream nextPutAll:'<' "mapping needed for xml text" |
|
5142 |
] ifFalse:[ eachChar == $& ifTrue:[ |
|
5143 |
aStream nextPutAll:'&' "mapping needed for all" |
|
5144 |
"/ ] ifFalse:[ eachChar == $> ifTrue:[ |
|
5145 |
"/ aStream nextPutAll:'>' "mapping needed for comments" |
|
5146 |
"/ ] ifFalse:[ eachChar == $' ifTrue:[ |
|
5147 |
"/ aStream nextPutAll:''' "mapping needed for attributes" |
|
5148 |
"/ ] ifFalse:[ eachChar == $" ifTrue:[ |
|
5149 |
"/ aStream nextPutAll:'"' "mapping needed for attributes" |
|
5150 |
] ifFalse:[ |
|
5151 |
|codePoint| |
|
5152 |
codePoint := eachChar codePoint. |
|
5153 |
((codePoint < 16r20 and:[codePoint ~~ 9 and:[codePoint ~~ 10 and:[codePoint ~~ 13]]]) |
|
5154 |
or:[codePoint >= 16r7F]) ifTrue:[ |
|
5155 |
aStream nextPutAll:'&#'. |
|
5156 |
codePoint printOn:aStream. |
|
5157 |
aStream nextPut:$;. |
|
5158 |
] ifFalse:[ |
|
5159 |
aStream nextPut:eachChar |
|
5160 |
]]]"/]]] |
|
5161 |
]. |
|
5162 |
! |
|
5163 |
||
12171 | 5164 |
printfWith:arg1 with:arg2 with:arg3 with:arg4 with:arg5 |
12177 | 5165 |
"Format and print the receiver with <argI> formatted in C style, |
12171 | 5166 |
as specified in the Unix C-language manual page for printf(3). |
15712 | 5167 |
Return the resulting string (i.e actually, this is more like an sprintf)." |
12177 | 5168 |
|
12171 | 5169 |
^ self printf:(Array with:arg1 with:arg2 with:arg3 with:arg4 with:arg5) |
5170 |
||
5171 |
" |
|
12177 | 5172 |
Transcript showCR:('%d %05x %08o %b' printfWith:123 with:234 with:345 with:123) |
12171 | 5173 |
" |
15788 | 5174 |
! |
5175 |
||
5176 |
xmlQuotedPrintString |
|
5177 |
"convert aString to a valid XML string |
|
5178 |
that can be used for attributes, text, comments an PIs |
|
5179 |
TODO: care for 16bit UNICODE string and escape chars ..." |
|
5180 |
||
5181 |
^ String streamContents:[:s| |
|
5182 |
self printXmlQuotedOn:s |
|
5183 |
]. |
|
9192 | 5184 |
! ! |
5185 |
||
11955 | 5186 |
|
9192 | 5187 |
!CharacterArray methodsFor:'queries'! |
608 | 5188 |
|
4429 | 5189 |
bitsPerCharacter |
8874 | 5190 |
"return the underlying strings bitsPerCharacter |
4429 | 5191 |
(i.e. is it a regular String or a TwoByteString)" |
5192 |
||
8457 | 5193 |
|string max| |
5194 |
||
5195 |
(string := self string) ~~ self ifTrue:[ |
|
14555 | 5196 |
^ string bitsPerCharacter |
8457 | 5197 |
]. |
5198 |
||
5199 |
max := 8. |
|
5200 |
self do:[:eachCharacter | |
|
14555 | 5201 |
max := max max:(eachCharacter bitsPerCharacter) |
9192 | 5202 |
]. |
5203 |
^ max |
|
5204 |
||
5205 |
" |
|
5206 |
'hello' bitsPerCharacter |
|
5207 |
'hello' asText allBold bitsPerCharacter |
|
5208 |
" |
|
5209 |
! |
|
8457 | 5210 |
|
10903 | 5211 |
continuesWith:aString startingAt:startIndex |
5212 |
"return true, if the receiver beginning at startIndex |
|
5213 |
contains the characters in aString." |
|
5214 |
||
5215 |
|sz "{Class: SmallInteger }" |
|
5216 |
idx "{Class: SmallInteger }"| |
|
5217 |
||
5218 |
sz := aString size. |
|
5219 |
idx := startIndex. |
|
5220 |
||
5221 |
1 to:sz do:[:i | |
|
16202 | 5222 |
(self at:idx) ~~ (aString at:i) ifTrue:[^ false]. |
5223 |
idx := idx + 1 |
|
10903 | 5224 |
]. |
5225 |
^ true |
|
5226 |
||
5227 |
" |
|
5228 |
'hello world' continuesWith:'world' startingAt:6 |
|
5229 |
'hello world' continuesWith:'world' startingAt:7 |
|
5230 |
" |
|
5231 |
||
5232 |
"Created: 12.5.1996 / 15:46:40 / cg" |
|
5233 |
"Modified: 26.7.1996 / 19:08:36 / cg" |
|
5234 |
! |
|
5235 |
||
5236 |
countWords |
|
5237 |
"return the number of words, which are separated by separators" |
|
5238 |
||
5239 |
|tally "{ Class: SmallInteger }" |
|
5240 |
start "{ Class: SmallInteger }" |
|
5241 |
mySize "{ Class: SmallInteger }" |
|
5242 |
stop ch| |
|
5243 |
||
5244 |
tally := 0. |
|
5245 |
start := 1. |
|
5246 |
mySize := self size. |
|
5247 |
[start <= mySize] whileTrue:[ |
|
16202 | 5248 |
ch := self at:start. |
5249 |
ch isSeparator ifTrue:[ |
|
5250 |
start := start + 1 |
|
5251 |
] ifFalse:[ |
|
5252 |
stop := self indexOfSeparatorStartingAt:start. |
|
5253 |
(stop == 0) ifTrue:[ |
|
5254 |
stop := mySize + 1 |
|
5255 |
]. |
|
5256 |
tally := tally + 1. |
|
5257 |
start := stop |
|
5258 |
] |
|
10903 | 5259 |
]. |
5260 |
^ tally |
|
5261 |
||
5262 |
" |
|
5263 |
'hello world isnt this nice' countWords' |
|
5264 |
" |
|
5265 |
! |
|
5266 |
||
11788 | 5267 |
defaultElement |
5268 |
^ Character space |
|
5269 |
! |
|
5270 |
||
608 | 5271 |
encoding |
1311 | 5272 |
"return the strings encoding, as a symbol. |
7982 | 5273 |
Here, by default, we assume unicode-encoding. |
5274 |
Notice, that iso8859-1 is a true subset of unicode, |
|
8874 | 5275 |
and that singleByteStrings are therefore both unicode AND |
7982 | 5276 |
8859-1 encoded." |
5277 |
||
14168 | 5278 |
^ #unicode |
608 | 5279 |
! |
5280 |
||
1375
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
5281 |
hasChangeOfEmphasis |
14405
1d0d99a95bba
comment/format in:7 methods
Claus Gittinger <cg@exept.de>
parents:
14168
diff
changeset
|
5282 |
"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
|
5283 |
i.e. any non-normal (=emphasized) characters" |
1d0d99a95bba
comment/format in:7 methods
Claus Gittinger <cg@exept.de>
parents:
14168
diff
changeset
|
5284 |
|
1375
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
5285 |
^ false |
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
5286 |
|
1381 | 5287 |
"Created: 12.5.1996 / 12:31:39 / cg" |
1375
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
5288 |
! |
f035bb7468f5
additions for new Text class
Claus Gittinger <cg@exept.de>
parents:
1333
diff
changeset
|
5289 |
|
7995
8ddbb403d67b
+hasIcon and hasImage for LabelAndIcon compatibility
Claus Gittinger <cg@exept.de>
parents:
7991
diff
changeset
|
5290 |
hasIcon |
9191 | 5291 |
"for LabelAndIcon compatibility" |
5292 |
||
9192 | 5293 |
^ false |
5294 |
! |
|
5295 |
||
5296 |
hasImage |
|
5297 |
"for LabelAndIcon compatibility" |
|
5298 |
||
5299 |
^ false |
|
5300 |
! |
|
7995
8ddbb403d67b
+hasIcon and hasImage for LabelAndIcon compatibility
Claus Gittinger <cg@exept.de>
parents:
7991
diff
changeset
|
5301 |
|
1388 | 5302 |
heightOn:aGC |
5303 |
"return the size of the recevier in device units if displayed on aGC" |
|
5304 |
||
6528 | 5305 |
^ (aGC font onDevice:aGC device) heightOf:self |
1388 | 5306 |
|
5307 |
" |
|
8874 | 5308 |
'hello world' heightOn:(View new) |
1388 | 5309 |
" |
5310 |
||
5311 |
"Created: 12.5.1996 / 20:09:29 / cg" |
|
5312 |
"Modified: 12.5.1996 / 20:32:05 / cg" |
|
5313 |
! |
|
5314 |
||
15773 | 5315 |
isKeyword |
5316 |
"return true, if the receiver is a keyword message selector" |
|
5317 |
||
5318 |
^ self includes:$: |
|
5319 |
||
5320 |
" |
|
5321 |
#at:put: isKeyword |
|
5322 |
#at: isKeyword |
|
5323 |
#+ isKeyword |
|
5324 |
#size isKeyword |
|
5325 |
" |
|
5326 |
||
5327 |
"Created: / 1.11.1997 / 12:34:55 / cg" |
|
5328 |
"Modified: / 1.11.1997 / 12:36:37 / cg" |
|
5329 |
! |
|
5330 |
||
11828 | 5331 |
isLowercaseFirst |
5332 |
"return true, if the first character is a lowercase character." |
|
5333 |
||
5334 |
^ self size ~~ 0 and:[ self first isLowercase ] |
|
5335 |
||
5336 |
" |
|
12177 | 5337 |
'helloWorld' isLowercaseFirst |
5338 |
'HelloWorld' isLowercaseFirst |
|
11828 | 5339 |
" |
5340 |
! |
|
5341 |
||
5342 |
isUppercaseFirst |
|
5343 |
"return true, if the first character is an uppercase character." |
|
5344 |
||
5345 |
^ self size ~~ 0 and:[ self first isUppercase ] |
|
5346 |
||
5347 |
" |
|
12177 | 5348 |
'helloWorld' isUppercaseFirst |
5349 |
'HelloWorld' isUppercaseFirst |
|
11828 | 5350 |
" |
5351 |
! |
|
5352 |
||
15773 | 5353 |
keywords |
5354 |
"assuming the receiver is a keyword message selector, |
|
5355 |
return the individual keywords (i.e. break it up at colons) |
|
5356 |
and return these as a collection. |
|
5357 |
For binary and unary selectors, the result may be nonsense (an array containing the receiver)." |
|
5358 |
||
5359 |
|coll s part| |
|
5360 |
||
5361 |
coll := OrderedCollection new. |
|
5362 |
s := ReadStream on:self. |
|
5363 |
[s atEnd] whileFalse:[ |
|
16202 | 5364 |
part := s through:$:. |
5365 |
coll add:part |
|
15773 | 5366 |
]. |
5367 |
^ coll asArray |
|
5368 |
||
5369 |
" |
|
5370 |
#at:put: keywords |
|
5371 |
#at: keywords |
|
5372 |
#+ keywords |
|
5373 |
#size keywords |
|
5374 |
" |
|
5375 |
||
5376 |
"Modified (Comment): / 30-06-2011 / 17:46:21 / cg" |
|
5377 |
! |
|
5378 |
||
9190 | 5379 |
knownAsSymbol |
5380 |
"for now, only single character strings are allowed as symbols. |
|
5381 |
This method is redefined in String." |
|
5382 |
||
5383 |
^ false |
|
5384 |
! |
|
5385 |
||
5386 |
leftIndent |
|
5387 |
"if the receiver starts with spaces, return the number of spaces |
|
5388 |
at the left - otherwise, return 0. |
|
9192 | 5389 |
If the receiver consists of spaces only, return the receivers size." |
5390 |
||
5391 |
|index "{Class: SmallInteger }" |
|
5392 |
end "{Class: SmallInteger }"| |
|
5393 |
||
5394 |
index := 1. |
|
5395 |
end := self size. |
|
5396 |
[index <= end] whileTrue:[ |
|
16202 | 5397 |
(self at:index) isSeparator ifFalse:[^ index - 1]. |
5398 |
index := index + 1 |
|
8350 | 5399 |
]. |
9192 | 5400 |
^ end |
5401 |
||
5402 |
" |
|
5403 |
' hello' leftIndent |
|
5404 |
'foo ' leftIndent |
|
9190 | 5405 |
' ' leftIndent |
5406 |
" |
|
5407 |
||
5408 |
"Modified: 20.4.1996 / 19:28:43 / cg" |
|
8350 | 5409 |
! |
5410 |
||
10437 | 5411 |
nameSpaceSelectorParts |
5412 |
"Answer the namespace and baseSelector parts of a namespace selector. |
|
5413 |
Namespace selectors are those generated by sends from a method in a different |
|
5414 |
namespace; they are prefixed by ':'<ns>'::'. |
|
5415 |
You cannot easily change this algorithm here, as it is also known by the VM's lookup function. |
|
5416 |
Experimental" |
|
5417 |
||
5418 |
|nsPart selPart idx| |
|
5419 |
||
5420 |
(self at:1) == $: ifFalse:[^ Array with:'' with:self]. |
|
5421 |
idx := self indexOf:$: startingAt:3. |
|
5422 |
idx == 0 ifTrue:[^ Array with:'' with:self]. |
|
5423 |
||
5424 |
(idx+2 > self size) ifTrue:[^ Array with:'' with:self]. |
|
5425 |
(self at:idx+1) == $: ifFalse:[^ Array with:'' with:self]. |
|
5426 |
nsPart := self copyFrom:2 to:idx-1. |
|
5427 |
selPart := self copyFrom:idx+2. |
|
5428 |
^ Array with:nsPart with:selPart |
|
5429 |
||
5430 |
"test: |
|
10703
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
5431 |
self assert:('+' nameSpaceSelectorParts) = #('' '+'). |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
5432 |
self assert:(':+' nameSpaceSelectorParts) = #('' ':+'). |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
5433 |
self assert:(':Foo:+' nameSpaceSelectorParts) = #('' ':Foo:+'). |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
5434 |
|
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
5435 |
self assert:(':Foo::+' nameSpaceSelectorParts) = #('Foo' '+'). |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
5436 |
self assert:(':Foo::bar:baz:' nameSpaceSelectorParts) = #('Foo' 'bar:baz:'). |
10437 | 5437 |
" |
5438 |
||
5439 |
"Created: / 05-03-2007 / 17:16:58 / cg" |
|
5440 |
"Modified: / 06-03-2007 / 11:51:15 / cg" |
|
5441 |
! |
|
5442 |
||
9190 | 5443 |
stringSpecies |
8874 | 5444 |
"return the underlying strings bitsPerCharacter |
8037
8cce25b1059c
care for wide chars in asXXXFirst
Claus Gittinger <cg@exept.de>
parents:
8032
diff
changeset
|
5445 |
(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
|
5446 |
|
8cce25b1059c
care for wide chars in asXXXFirst
Claus Gittinger <cg@exept.de>
parents:
8032
diff
changeset
|
5447 |
|string| |
8cce25b1059c
care for wide chars in asXXXFirst
Claus Gittinger <cg@exept.de>
parents:
8032
diff
changeset
|
5448 |
|
15523 | 5449 |
string := self string. |
5450 |
string == self ifTrue:[^ self species]. |
|
8037
8cce25b1059c
care for wide chars in asXXXFirst
Claus Gittinger <cg@exept.de>
parents:
8032
diff
changeset
|
5451 |
^ string stringSpecies |
8cce25b1059c
care for wide chars in asXXXFirst
Claus Gittinger <cg@exept.de>
parents:
8032
diff
changeset
|
5452 |
|
8cce25b1059c
care for wide chars in asXXXFirst
Claus Gittinger <cg@exept.de>
parents:
8032
diff
changeset
|
5453 |
" |
8874 | 5454 |
'hello' stringSpecies |
9192 | 5455 |
'hello' asText allBold stringSpecies |
5456 |
" |
|
5457 |
! |
|
8037
8cce25b1059c
care for wide chars in asXXXFirst
Claus Gittinger <cg@exept.de>
parents:
8032
diff
changeset
|
5458 |
|
6653 | 5459 |
widthFrom:startIndex to:endIndex on:aGC |
5460 |
"return ths size of part of the recevier in device units if displayed on aGC" |
|
5461 |
||
5462 |
^ (aGC font onDevice:aGC device) widthOf:self from:startIndex to:endIndex |
|
5463 |
||
5464 |
" |
|
8874 | 5465 |
'hello world' widthFrom:1 to:5 on:(View new) |
5466 |
'hello' widthOn:(View new) |
|
6653 | 5467 |
" |
9192 | 5468 |
! |
6653 | 5469 |
|
1388 | 5470 |
widthOn:aGC |
5471 |
"return ths size of the recevier in device units if displayed on aGC" |
|
5472 |
||
6528 | 5473 |
^ (aGC font onDevice:aGC device) widthOf:self |
1388 | 5474 |
|
5475 |
" |
|
8874 | 5476 |
'hello world' widthOn:(View new) |
1388 | 5477 |
" |
5478 |
||
5479 |
"Created: 12.5.1996 / 20:09:29 / cg" |
|
2554 | 5480 |
"Modified: 17.4.1997 / 12:50:23 / cg" |
608 | 5481 |
! ! |
5482 |
||
5483 |
!CharacterArray methodsFor:'special string converting'! |
|
5484 |
||
16759 | 5485 |
asUnixFilenameString |
5486 |
"return a new string consisting of receiver's characters |
|
5487 |
with all \-characters replaced by /-characters. |
|
5488 |
If there are no backslashes, return the original" |
|
5489 |
||
5490 |
^ self copyReplaceAll:$\ with:$/ ifNone:self |
|
5491 |
||
5492 |
" |
|
5493 |
'hello\world' asUnixFilenameString |
|
5494 |
" |
|
5495 |
||
5496 |
"Modified: / 18.7.1998 / 22:53:02 / cg" |
|
5497 |
! |
|
5498 |
||
14768 | 5499 |
expandPlaceholders:escapeCharacter with:argArrayOrDictionary |
5500 |
"this is the generic version of the old %-escaping method, allowing for an arbitrary |
|
5501 |
escape character to be used (typically $$ or $% are effectively used). |
|
5502 |
Return a copy of the receiver, where all %i escapes are |
|
5503 |
replaced by corresponding arguments' printStrings from the argArrayOrDictionary. |
|
5504 |
I.e. 'hello %1; how is %2' expandPlaceholdersWith:#('world' 'this') results |
|
5505 |
in the new string 'hello world; how is this'. |
|
5506 |
As an extension, the argument may also be a dictionary, providing |
|
5507 |
values for symbolic keys. |
|
5508 |
In this case, %a .. %z and %(...) are also allowed. |
|
5509 |
(%1..%9 require a numeric key in the dictionary, however) |
|
5510 |
To get a '%' character, use a '%%'-escape. |
|
5511 |
To get an integer-indexed placeHolder followed by another digit, |
|
5512 |
or an index > 9, you must use %(digit). |
|
5513 |
See also bindWith:... for VisualAge compatibility." |
|
5514 |
||
5515 |
|stream| |
|
5516 |
||
5517 |
stream := (TextStream ? WriteStream) on:(self species new:self size + 20). |
|
5518 |
self expandPlaceholders:escapeCharacter with:argArrayOrDictionary on:stream. |
|
5519 |
^ stream contents. |
|
5520 |
||
5521 |
||
5522 |
" |
|
5523 |
'hello %1' expandPlaceholdersWith:#('world') |
|
5524 |
'hello %1; how is %2' expandPlaceholdersWith:#('world' 'this') |
|
5525 |
'hello %2; how is %1' expandPlaceholdersWith:#('world' 'this') |
|
5526 |
'%1 plus %2 gives %3 ' expandPlaceholdersWith:#(4 5 9) |
|
5527 |
'%%(1)0 gives %(1)0' expandPlaceholdersWith:#(123) |
|
5528 |
'%%10 gives %10' expandPlaceholdersWith:#(123) |
|
5529 |
'%%(10) gives %(10)' expandPlaceholdersWith:#(123) |
|
5530 |
'%test gives %1' expandPlaceholdersWith:#(123) |
|
5531 |
'bla %1 bla' expandPlaceholdersWith:{ 'hello' allBold } |
|
5532 |
'bla %1 bla' expandPlaceholdersWith:{ 'hello' } |
|
5533 |
" |
|
5534 |
||
5535 |
" |
|
5536 |
|dict| |
|
5537 |
||
5538 |
dict := Dictionary new. |
|
5539 |
dict at:1 put:'one'. |
|
5540 |
dict at:$a put:'AAAAA'. |
|
5541 |
dict at:$b put:[ Time now ]. |
|
5542 |
dict at:'foo' put:[ Date today ]. |
|
15196 | 5543 |
'hello $1 %a $b %(foo) $foo ' expandPlaceholders:$$ with:dict. |
5544 |
'hello $1 %a $b %(foo) $foo ' expandPlaceholders:$% with:dict. |
|
14768 | 5545 |
" |
5546 |
||
5547 |
"Modified: 1.7.1997 / 00:53:24 / cg" |
|
5548 |
! |
|
5549 |
||
14765 | 5550 |
expandPlaceholders:escapeCharacter with:argArrayOrDictionary on:aStream |
5551 |
"this is the generic version of the old %-escaping method, allowing for an arbitrary |
|
5552 |
escape character to be used (typically $$ or $% are effectively used). |
|
5553 |
Write the receiver to aStream, where all %i escapes are |
|
5554 |
replaced by corresponding arguments' printStrings from the argArrayOrDictionary. |
|
5555 |
I.e. 'hello %1; how is %2' expandPlaceholdersWith:#('world' 'this') results |
|
5556 |
in the new string 'hello world; how is this'. |
|
5557 |
As an extension, the argument may also be a dictionary, providing |
|
5558 |
values for symbolic keys. |
|
5559 |
In this case, %a .. %z and %(...) are also allowed. |
|
5560 |
(%1..%9 require a numeric key in the dictionary, however) |
|
5561 |
To get a '%' character, use a '%%'-escape. |
|
5562 |
To get an integer-indexed placeHolder followed by another digit, |
|
5563 |
or an index > 9, you must use %(digit). |
|
5564 |
See also bindWith:... for VisualAge compatibility. |
|
5565 |
Use %<cr> to insert a CR and %<tab> to insert a TAB." |
|
5566 |
||
5567 |
|next v key |
|
5568 |
idx "{ SmallInteger }" |
|
5569 |
idx2 "{ SmallInteger }" |
|
5570 |
start "{ SmallInteger }" |
|
5571 |
stop "{ SmallInteger }"| |
|
5572 |
||
5573 |
stop := self size. |
|
5574 |
start := 1. |
|
5575 |
[start <= stop] whileTrue:[ |
|
5576 |
idx := self indexOf:escapeCharacter startingAt:start. |
|
5577 |
(idx == 0 or:[idx == stop]) ifTrue:[ |
|
5578 |
aStream nextPutAll:self startingAt:start to:stop. |
|
5579 |
^ self. |
|
5580 |
]. |
|
5581 |
"found an escapeCharacter" |
|
5582 |
aStream nextPutAll:self startingAt:start to:(idx - 1). |
|
5583 |
next := self at:(idx + 1). |
|
5584 |
(next == escapeCharacter) ifTrue:[ |
|
5585 |
aStream nextPut:escapeCharacter. |
|
5586 |
] ifFalse:[ |
|
5587 |
next == $< ifTrue:[ |
|
5588 |
idx2 := self indexOf:$> startingAt:idx+2. |
|
5589 |
key := self copyFrom:idx+2 to:idx2-1. |
|
5590 |
idx := idx2 - 1. |
|
5591 |
key := key asSymbolIfInterned. |
|
5592 |
(#(cr tab nl return lf ff null) includesIdentical:key) ifTrue:[ |
|
5593 |
aStream nextPut:(Character perform:key). |
|
5594 |
]. |
|
5595 |
] ifFalse:[ |
|
5596 |
next isDigit ifTrue:[ |
|
5597 |
v := argArrayOrDictionary at:(next digitValue) ifAbsent:'' |
|
5598 |
] ifFalse:[ |
|
5599 |
next == $( ifTrue:[ |
|
5600 |
idx2 := self indexOf:$) startingAt:idx+2. |
|
5601 |
key := self copyFrom:idx+2 to:idx2-1. |
|
5602 |
idx := idx2 - 1. |
|
5603 |
(argArrayOrDictionary includesKey:key) ifTrue:[ |
|
5604 |
v := argArrayOrDictionary at:key |
|
5605 |
] ifFalse:[ |
|
5606 |
key := key asSymbolIfInterned ? key. |
|
5607 |
(argArrayOrDictionary includesKey:key) ifTrue:[ |
|
5608 |
v := argArrayOrDictionary at:key |
|
5609 |
] ifFalse:[ |
|
5610 |
(key size == 1 and:[ argArrayOrDictionary includesKey:(key at:1)]) ifTrue:[ |
|
5611 |
v := argArrayOrDictionary at:(key at:1) |
|
5612 |
] ifFalse:[ |
|
5613 |
key isNumeric ifTrue:[ |
|
5614 |
key := Integer readFrom:key onError:nil. |
|
5615 |
]. |
|
5616 |
v := argArrayOrDictionary at:key ifAbsent:'' |
|
5617 |
] |
|
5618 |
]. |
|
5619 |
]. |
|
5620 |
] ifFalse:[ |
|
5621 |
(next isLetter and:[argArrayOrDictionary isSequenceable not "is a Dictionary"]) ifTrue:[ |
|
5622 |
"so next is a non-numeric single character." |
|
5623 |
v := argArrayOrDictionary |
|
5624 |
at:next |
|
5625 |
ifAbsent:[ |
|
5626 |
"try symbol instead of character" |
|
5627 |
argArrayOrDictionary |
|
5628 |
at:next asSymbol |
|
5629 |
ifAbsent:[String with:escapeCharacter with:next]. |
|
5630 |
]. |
|
5631 |
] ifFalse:[ |
|
5632 |
v := String with:$% with:next. |
|
5633 |
]. |
|
5634 |
] |
|
5635 |
]. |
|
5636 |
"/ v notNil ifTrue:[ |
|
5637 |
v isBlock ifTrue:[ |
|
5638 |
v := v value |
|
5639 |
]. |
|
5640 |
||
5641 |
v printOn:aStream. |
|
5642 |
"/ ]. |
|
5643 |
] |
|
5644 |
]. |
|
5645 |
start := idx + 2 |
|
5646 |
]. |
|
5647 |
||
5648 |
" |
|
5649 |
String streamContents:[:s| |
|
5650 |
'hello %1' expandPlaceholders:$% with:#('world') on:s. |
|
5651 |
s cr. |
|
5652 |
'hello $1; how is $2' expandPlaceholders:$$ with:#('world' 'this') on:s. |
|
5653 |
s cr. |
|
5654 |
'hello %2; how is %1' expandPlaceholders:$% with:#('world' 'this') on:s. |
|
5655 |
s cr. |
|
5656 |
'%1 plus %2 gives %3 ' expandPlaceholders:$% with:#(4 5 9) on:s. |
|
5657 |
s cr. |
|
5658 |
'%%(1)0 gives %(1)0' expandPlaceholders:$% with:#(123) on:s. |
|
5659 |
s cr. |
|
5660 |
'%%10 gives %10' expandPlaceholders:$% with:#(123) on:s. |
|
5661 |
s cr. |
|
5662 |
'%%(10) gives %(10) %<cr>%<tab>next line' expandPlaceholders:$% with:#(123) on:s. |
|
5663 |
s cr. |
|
16452 | 5664 |
'%%test gives %test' expandPlaceholders:$% with:#(123) on:s. |
5665 |
s cr. |
|
5666 |
'|%%<tab>|%%1|%%<cr>| gives |%<tab>|%1|%<cr>|' expandPlaceholders:$% with:#(foo) on:s. |
|
14765 | 5667 |
] |
5668 |
" |
|
5669 |
||
5670 |
" |
|
5671 |
|dict| |
|
5672 |
||
5673 |
dict := Dictionary new. |
|
5674 |
dict at:1 put:'one'. |
|
5675 |
dict at:$a put:'AAAAA'. |
|
5676 |
dict at:$b put:[ Time now ]. |
|
5677 |
String streamContents:[:s| |
|
5678 |
'hello $1 $a $b' expandPlaceholders:$$ with:dict on:s. |
|
5679 |
]. |
|
5680 |
" |
|
5681 |
||
5682 |
"Modified: / 18-11-2010 / 15:43:28 / cg" |
|
5683 |
! |
|
5684 |
||
9192 | 5685 |
expandPlaceholdersWith:argArrayOrDictionary |
608 | 5686 |
"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
|
5687 |
replaced by corresponding arguments' printStrings from the argArrayOrDictionary. |
608 | 5688 |
I.e. 'hello %1; how is %2' expandPlaceholdersWith:#('world' 'this') results |
5689 |
in the new string 'hello world; how is this'. |
|
4410
2bdc0c89a42e
enhanced #exapndPlaceHolders, to allow for a dictionary arg
Claus Gittinger <cg@exept.de>
parents:
4409
diff
changeset
|
5690 |
As an extension, the argument may also be a dictionary, providing |
7313 | 5691 |
values for symbolic keys. |
4842 | 5692 |
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
|
5693 |
(%1..%9 require a numeric key in the dictionary, however) |
608 | 5694 |
To get a '%' character, use a '%%'-escape. |
7313 | 5695 |
To get an integer-indexed placeHolder followed by another digit, |
5696 |
or an index > 9, you must use %(digit). |
|
608 | 5697 |
See also bindWith:... for VisualAge compatibility." |
5698 |
||
10906
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
5699 |
|stream| |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
5700 |
|
12601
afe93f0d48c5
changed: #expandPlaceholdersWith:
Claus Gittinger <cg@exept.de>
parents:
12519
diff
changeset
|
5701 |
stream := (TextStream ? WriteStream) on:(self species new:self size + 20). |
10906
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
5702 |
self expandPlaceholdersWith:argArrayOrDictionary on:stream. |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
5703 |
^ stream contents. |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
5704 |
|
608 | 5705 |
|
5706 |
" |
|
8874 | 5707 |
'hello %1' expandPlaceholdersWith:#('world') |
5708 |
'hello %1; how is %2' expandPlaceholdersWith:#('world' 'this') |
|
5709 |
'hello %2; how is %1' expandPlaceholdersWith:#('world' 'this') |
|
5710 |
'%1 plus %2 gives %3 ' expandPlaceholdersWith:#(4 5 9) |
|
5711 |
'%%(1)0 gives %(1)0' expandPlaceholdersWith:#(123) |
|
5712 |
'%%10 gives %10' expandPlaceholdersWith:#(123) |
|
5713 |
'%%(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
|
5714 |
'%test gives %1' expandPlaceholdersWith:#(123) |
11028
cb1ee3ddd91a
expandPlaceHolders: care fr Text with emphasis
Claus Gittinger <cg@exept.de>
parents:
11018
diff
changeset
|
5715 |
'bla %1 bla' expandPlaceholdersWith:{ 'hello' allBold } |
cb1ee3ddd91a
expandPlaceHolders: care fr Text with emphasis
Claus Gittinger <cg@exept.de>
parents:
11018
diff
changeset
|
5716 |
'bla %1 bla' expandPlaceholdersWith:{ 'hello' } |
608 | 5717 |
" |
1026
455c9f609f4d
care for 16bit strings in expandPlaceHolders
Claus Gittinger <cg@exept.de>
parents:
1025
diff
changeset
|
5718 |
|
4410
2bdc0c89a42e
enhanced #exapndPlaceHolders, to allow for a dictionary arg
Claus Gittinger <cg@exept.de>
parents:
4409
diff
changeset
|
5719 |
" |
2bdc0c89a42e
enhanced #exapndPlaceHolders, to allow for a dictionary arg
Claus Gittinger <cg@exept.de>
parents:
4409
diff
changeset
|
5720 |
|dict| |
2bdc0c89a42e
enhanced #exapndPlaceHolders, to allow for a dictionary arg
Claus Gittinger <cg@exept.de>
parents:
4409
diff
changeset
|
5721 |
|
2bdc0c89a42e
enhanced #exapndPlaceHolders, to allow for a dictionary arg
Claus Gittinger <cg@exept.de>
parents:
4409
diff
changeset
|
5722 |
dict := Dictionary new. |
2bdc0c89a42e
enhanced #exapndPlaceHolders, to allow for a dictionary arg
Claus Gittinger <cg@exept.de>
parents:
4409
diff
changeset
|
5723 |
dict at:1 put:'one'. |
2bdc0c89a42e
enhanced #exapndPlaceHolders, to allow for a dictionary arg
Claus Gittinger <cg@exept.de>
parents:
4409
diff
changeset
|
5724 |
dict at:$a put:'AAAAA'. |
8794
a5df33935142
nindWith supports blocks in the value dictionary
Claus Gittinger <cg@exept.de>
parents:
8710
diff
changeset
|
5725 |
dict at:$b put:[ Time now ]. |
12316
3e78905d70a9
comment/format in: #expandPlaceholdersWith:
Claus Gittinger <cg@exept.de>
parents:
12196
diff
changeset
|
5726 |
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
|
5727 |
'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
|
5728 |
" |
2bdc0c89a42e
enhanced #exapndPlaceHolders, to allow for a dictionary arg
Claus Gittinger <cg@exept.de>
parents:
4409
diff
changeset
|
5729 |
|
2737
9f29aecea39d
care for % at end in expandPlaceHolders.
Claus Gittinger <cg@exept.de>
parents:
2736
diff
changeset
|
5730 |
"Modified: 1.7.1997 / 00:53:24 / cg" |
9192 | 5731 |
! |
5732 |
||
10906
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
5733 |
expandPlaceholdersWith:argArrayOrDictionary on:aStream |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
5734 |
"write the receiver to aStream, where all %i escapes are |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
5735 |
replaced by corresponding arguments' printStrings from the argArrayOrDictionary. |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
5736 |
I.e. 'hello %1; how is %2' expandPlaceholdersWith:#('world' 'this') results |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
5737 |
in the new string 'hello world; how is this'. |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
5738 |
As an extension, the argument may also be a dictionary, providing |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
5739 |
values for symbolic keys. |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
5740 |
In this case, %a .. %z and %(...) are also allowed. |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
5741 |
(%1..%9 require a numeric key in the dictionary, however) |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
5742 |
To get a '%' character, use a '%%'-escape. |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
5743 |
To get an integer-indexed placeHolder followed by another digit, |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
5744 |
or an index > 9, you must use %(digit). |
10935 | 5745 |
See also bindWith:... for VisualAge compatibility. |
10939
d0ca31c1d1e9
#expandPlaceHoldersWith: handle %<cr> %<tab> etc
Stefan Vogel <sv@exept.de>
parents:
10935
diff
changeset
|
5746 |
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
|
5747 |
|
14765 | 5748 |
^ self expandPlaceholders:$% with:argArrayOrDictionary on:aStream |
10906
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
5749 |
|
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
5750 |
" |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
5751 |
String streamContents:[:s| |
14765 | 5752 |
'hello %1' expandPlaceholdersWith:#('world') on:s. |
5753 |
s cr. |
|
5754 |
'hello %1; how is %2' expandPlaceholdersWith:#('world' 'this') on:s. |
|
5755 |
s cr. |
|
5756 |
'hello %2; how is %1' expandPlaceholdersWith:#('world' 'this') on:s. |
|
5757 |
s cr. |
|
5758 |
'%1 plus %2 gives %3 ' expandPlaceholdersWith:#(4 5 9) on:s. |
|
5759 |
s cr. |
|
5760 |
'%%(1)0 gives %(1)0' expandPlaceholdersWith:#(123) on:s. |
|
5761 |
s cr. |
|
5762 |
'%%10 gives %10' expandPlaceholdersWith:#(123) on:s. |
|
5763 |
s cr. |
|
5764 |
'%%(10) gives %(10) %<cr>%<tab>next line' expandPlaceholdersWith:#(123) on:s. |
|
5765 |
s cr. |
|
5766 |
'%test gives %1' expandPlaceholdersWith:#(123) on:s. |
|
10906
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
5767 |
] |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
5768 |
" |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
5769 |
|
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
5770 |
" |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
5771 |
|dict| |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
5772 |
|
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
5773 |
dict := Dictionary new. |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
5774 |
dict at:1 put:'one'. |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
5775 |
dict at:$a put:'AAAAA'. |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
5776 |
dict at:$b put:[ Time now ]. |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
5777 |
String streamContents:[:s| |
14765 | 5778 |
'hello %1 %a %b' expandPlaceholdersWith:dict on:s. |
10906
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
5779 |
]. |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
5780 |
" |
13008
a27647c36b31
changed: #expandPlaceholdersWith:on:
Claus Gittinger <cg@exept.de>
parents:
12925
diff
changeset
|
5781 |
|
13138
d2eb0eba5907
changed: #expandPlaceholdersWith:on:
Claus Gittinger <cg@exept.de>
parents:
13137
diff
changeset
|
5782 |
"Modified: / 18-11-2010 / 15:43:28 / cg" |
10906
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
5783 |
! |
6468fe197af0
CharacterArray>>#expandMacrosWith:on:
Stefan Vogel <sv@exept.de>
parents:
10903
diff
changeset
|
5784 |
|
9230 | 5785 |
firstLine |
14405
1d0d99a95bba
comment/format in:7 methods
Claus Gittinger <cg@exept.de>
parents:
14168
diff
changeset
|
5786 |
"return the first line of a multiline string" |
1d0d99a95bba
comment/format in:7 methods
Claus Gittinger <cg@exept.de>
parents:
14168
diff
changeset
|
5787 |
|
1d0d99a95bba
comment/format in:7 methods
Claus Gittinger <cg@exept.de>
parents:
14168
diff
changeset
|
5788 |
^ self asCollectionOfSubCollectionsSeparatedBy:(Character cr) do:[:line | ^ line]. |
9230 | 5789 |
|
5790 |
" |
|
10703
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
5791 |
'hello' firstLine |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
5792 |
'1\2\3' withCRs firstLine |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
5793 |
'\1\2\3' withCRs firstLine |
9230 | 5794 |
" |
5795 |
! |
|
5796 |
||
13987 | 5797 |
tokensBasedOn:aCharacter |
5798 |
"this is an ST-80 alias for the ST/X method |
|
16202 | 5799 |
asCollectionOfSubstringsSeparatedBy:" |
13987 | 5800 |
|
5801 |
^ self asCollectionOfSubstringsSeparatedBy:aCharacter |
|
5802 |
||
5803 |
" |
|
5804 |
'hello:world:isnt:this nice' tokensBasedOn:$: |
|
5805 |
'foo,bar,baz' tokensBasedOn:$, |
|
5806 |
'/etc/passwd' asFilename readStream nextLine tokensBasedOn:$: |
|
5807 |
" |
|
5808 |
! |
|
5809 |
||
5810 |
withCEscapes |
|
16863 | 5811 |
"return a new string consisting of receiver's characters |
13987 | 5812 |
with all special and unprintable characters replaced by \X-character escapes. |
5813 |
(similar to the way C-language literal Strings are represented). |
|
5814 |
The resulting string will contain only 7-bit ascii characters. |
|
5815 |
Emphasis is not supported. |
|
5816 |
The following escapes are generated: |
|
16785 | 5817 |
\' single quote character |
5818 |
\dQuote double quote character |
|
5819 |
\r return character |
|
13987 | 5820 |
\r return character |
5821 |
\n newline character |
|
5822 |
\t tab character |
|
5823 |
\\ the \ character itself |
|
5824 |
\xnn two digit hex number defining the characters ascii value |
|
5825 |
\unnnn four digit hex number defining the characters ascii value |
|
5826 |
\Unnnnnnnn eight digit hex number defining the characters ascii value |
|
16863 | 5827 |
This is the opposite of withoutCEscapes. |
13987 | 5828 |
" |
5829 |
||
5830 |
|anyEscapeNeeded out seq| |
|
5831 |
||
5832 |
" |
|
5833 |
first, check if any escape is needed and return the receiver unchanged if not |
|
5834 |
" |
|
16785 | 5835 |
anyEscapeNeeded := self |
5836 |
contains:[:ch | |
|
5837 |
((ch codePoint between:32 and:126) not |
|
5838 |
or:[ch == $' or:[ch == $"]]) |
|
5839 |
]. |
|
13987 | 5840 |
anyEscapeNeeded ifFalse:[ ^ self ]. |
5841 |
||
5842 |
self hasChangeOfEmphasis ifTrue:[ self error:'emphasis not supported' ]. |
|
5843 |
||
5844 |
out := WriteStream on:(String new:self size-1). |
|
5845 |
||
5846 |
self do:[:ch | |
|
5847 |
|cp| |
|
5848 |
||
16785 | 5849 |
(ch == $' or:[ch == $"]) ifTrue:[ |
5850 |
out nextPut:$\. |
|
5851 |
out nextPut:ch. |
|
13987 | 5852 |
] ifFalse:[ |
16785 | 5853 |
(ch codePoint between:32 and:126) ifTrue:[ |
5854 |
out nextPut:ch |
|
5855 |
] ifFalse:[ |
|
5856 |
ch == Character return ifTrue:[ |
|
5857 |
seq := '\r' |
|
5858 |
] ifFalse:[ ch == Character nl ifTrue:[ |
|
5859 |
seq := '\n' |
|
5860 |
] ifFalse:[ ch == Character tab ifTrue:[ |
|
5861 |
seq := '\t' |
|
5862 |
] ifFalse:[ ch == $\ ifTrue:[ |
|
5863 |
seq := '\\' |
|
5864 |
] ifFalse:[ |
|
5865 |
cp := ch codePoint. |
|
5866 |
cp <= 16rFF ifTrue:[ |
|
5867 |
seq := '\x' , (cp printStringRadix:16 padTo:2) |
|
13987 | 5868 |
] ifFalse:[ |
16785 | 5869 |
cp <= 16rFFFF ifTrue:[ |
5870 |
seq := '\u' , (cp printStringRadix:16 padTo:4) |
|
5871 |
] ifFalse:[ |
|
5872 |
seq := '\U',(cp printStringRadix:16 padTo:8) |
|
5873 |
] |
|
13987 | 5874 |
] |
16785 | 5875 |
]]]]. |
5876 |
out nextPutAll:seq |
|
5877 |
]. |
|
13987 | 5878 |
]. |
5879 |
]. |
|
5880 |
^ out contents |
|
5881 |
||
5882 |
" |
|
16785 | 5883 |
'hello\nworld\na\n\tnice\n\t\tstring' withoutCEscapes withCEscapes. |
13987 | 5884 |
" |
5885 |
||
5886 |
"Created: / 25-01-2012 / 11:08:16 / cg" |
|
5887 |
! |
|
5888 |
||
9192 | 5889 |
withCRs |
16757 | 5890 |
"return a new string consisting of receiver's characters |
5891 |
with all \-characters replaced by cr-characters. |
|
5892 |
If there are no backslashes, return the original" |
|
5893 |
||
16759 | 5894 |
^ self copyReplaceAll:$\ with:(Character cr) ifNone:self |
9192 | 5895 |
|
5896 |
" |
|
5897 |
'hello\world' withCRs |
|
5898 |
" |
|
5899 |
||
5900 |
"Modified: / 18.7.1998 / 22:53:02 / cg" |
|
5901 |
! |
|
5902 |
||
5903 |
withEscapes |
|
15405 | 5904 |
<resource: #obsolete> |
13987 | 5905 |
"has been renamed; the name withEscapes is misleading" |
5906 |
||
5907 |
self obsoleteMethodWarning:'use withoutCEscapes'. |
|
5908 |
^ self withoutCEscapes. |
|
5909 |
||
5910 |
"Modified: / 25-01-2012 / 10:42:30 / cg" |
|
9192 | 5911 |
! |
5912 |
||
5913 |
withMatchEscapes |
|
4117 | 5914 |
"return a copy of the receiver with all match characters escaped |
5915 |
by $\ characters (to be usable as a match string). |
|
5916 |
Return the receiver, if there are none." |
|
5917 |
||
5342
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
5918 |
|in out c escape| |
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
5919 |
|
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
5920 |
escape := self class matchEscapeCharacter. |
4117 | 5921 |
|
5922 |
in := self readStream. |
|
7986 | 5923 |
out := WriteStream on:(self species new:self size). |
4117 | 5924 |
[in atEnd] whileFalse:[ |
16202 | 5925 |
c := in next. |
5926 |
(c == escape or:['*[#' includes:c]) ifTrue:[ |
|
5927 |
out nextPut:$\. |
|
5928 |
]. |
|
5929 |
out nextPut:c. |
|
4117 | 5930 |
]. |
5931 |
^ out contents. |
|
5932 |
||
5933 |
" |
|
8874 | 5934 |
'*foo' withMatchEscapes |
5935 |
'\*foo' withMatchEscapes |
|
5936 |
'*foo' withMatchEscapes |
|
5937 |
'\\*foo' withMatchEscapes |
|
5938 |
'foo*' withMatchEscapes |
|
5939 |
'foo\*' withMatchEscapes |
|
5940 |
'foo\' withMatchEscapes |
|
5941 |
'f*o*o' withMatchEscapes |
|
4117 | 5942 |
" |
5943 |
||
5944 |
"Modified: 2.4.1997 / 18:13:04 / cg" |
|
9192 | 5945 |
! |
5946 |
||
14992 | 5947 |
withSeparatorsCompacted |
5948 |
"return a new string with each sequence of whiteSpace replaced by a single space character. |
|
5949 |
Preserves a leading/trailing space." |
|
5950 |
||
5951 |
^ self species streamContents:[:s | |
|
5952 |
|skipping| |
|
5953 |
||
5954 |
skipping := false. |
|
5955 |
1 to:self size do:[:idx | |
|
5956 |
|char| |
|
5957 |
||
5958 |
char := self at:idx. |
|
14993 | 5959 |
char isSeparator ifFalse:[ |
5960 |
s nextPut:char. |
|
5961 |
skipping := false. |
|
5962 |
] ifTrue:[ |
|
5963 |
skipping ifFalse:[ |
|
14992 | 5964 |
s nextPut:(Character space). |
5965 |
skipping := true |
|
5966 |
]. |
|
5967 |
] |
|
5968 |
] |
|
5969 |
] |
|
5970 |
||
5971 |
" |
|
5972 |
'hello wwww' withSeparatorsCompacted |
|
5973 |
'hello wwww' withSeparatorsCompacted |
|
5974 |
' hello wwww' withSeparatorsCompacted |
|
5975 |
' hello wwww ' withSeparatorsCompacted |
|
5976 |
' hello wwww ' withSeparatorsCompacted |
|
5977 |
'hel lo www w' withSeparatorsCompacted |
|
5978 |
" |
|
5979 |
! |
|
5980 |
||
9192 | 5981 |
withTabs |
735
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
678
diff
changeset
|
5982 |
"return a string consisting of the receivers characters |
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
678
diff
changeset
|
5983 |
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
|
5984 |
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
|
5985 |
otherwise a new string is returned. |
8874 | 5986 |
Limitation: only the very first spaces are replaced |
15523 | 5987 |
(i.e. if the receiver contains newLine characters, |
5988 |
no tabs are inserted after those lineBreaks)" |
|
8874 | 5989 |
|
5990 |
|idx "{ SmallInteger }" |
|
5991 |
nTabs "{ SmallInteger }" |
|
608 | 5992 |
newString| |
5993 |
||
5994 |
idx := self findFirst:[:c | (c ~~ Character space)]. |
|
5995 |
nTabs := (idx-1) // 8. |
|
5815
4acdd87a77d4
// can return negative numbers.
Stefan Vogel <sv@exept.de>
parents:
5795
diff
changeset
|
5996 |
nTabs <= 0 ifTrue:[^ self]. |
608 | 5997 |
|
5998 |
"any tabs" |
|
15523 | 5999 |
newString := self species new:(self size - (nTabs * 7)). |
608 | 6000 |
newString atAll:(1 to:nTabs) put:(Character tab). |
6001 |
newString replaceFrom:(nTabs + 1) with:self startingAt:(nTabs * 8 + 1). |
|
6002 |
^ newString |
|
6003 |
||
6004 |
" |
|
8874 | 6005 |
'12345678901234567890' withTabs |
6006 |
' 8901234567890' withTabs |
|
6007 |
' 901234567890' withTabs |
|
608 | 6008 |
' 67890' withTabs |
6009 |
' 7890' withTabs |
|
6010 |
' 890' withTabs |
|
6011 |
" |
|
9192 | 6012 |
! |
6013 |
||
6014 |
withTabsExpanded |
|
735
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
678
diff
changeset
|
6015 |
"return a string consisting of the receivers characters, |
8874 | 6016 |
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
|
6017 |
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
|
6018 |
otherwise a new string is returned. |
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
678
diff
changeset
|
6019 |
This does handle multiline strings." |
608 | 6020 |
|
5741
3a6086774d38
added #withTabsExpanded: (i.e. arg to specify tab-column width)
Claus Gittinger <cg@exept.de>
parents:
5739
diff
changeset
|
6021 |
^ self withTabsExpanded:8 |
3a6086774d38
added #withTabsExpanded: (i.e. arg to specify tab-column width)
Claus Gittinger <cg@exept.de>
parents:
5739
diff
changeset
|
6022 |
|
3a6086774d38
added #withTabsExpanded: (i.e. arg to specify tab-column width)
Claus Gittinger <cg@exept.de>
parents:
5739
diff
changeset
|
6023 |
" |
8874 | 6024 |
('1' , Character tab asString , 'x') withTabsExpanded |
6025 |
('12345' , Character tab asString , 'x') withTabsExpanded |
|
6026 |
('123456' , Character tab asString , 'x') withTabsExpanded |
|
6027 |
('1234567' , Character tab asString , 'x') withTabsExpanded |
|
6028 |
('12345678' , Character tab asString , 'x') withTabsExpanded |
|
6029 |
('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
|
6030 |
|
3a6086774d38
added #withTabsExpanded: (i.e. arg to specify tab-column width)
Claus Gittinger <cg@exept.de>
parents:
5739
diff
changeset
|
6031 |
(String with:Character tab |
16202 | 6032 |
with:Character tab |
6033 |
with:$1) withTabsExpanded |
|
5741
3a6086774d38
added #withTabsExpanded: (i.e. arg to specify tab-column width)
Claus Gittinger <cg@exept.de>
parents:
5739
diff
changeset
|
6034 |
|
3a6086774d38
added #withTabsExpanded: (i.e. arg to specify tab-column width)
Claus Gittinger <cg@exept.de>
parents:
5739
diff
changeset
|
6035 |
(String with:Character tab |
16202 | 6036 |
with:$1 |
6037 |
with:Character tab |
|
6038 |
with:$2) withTabsExpanded |
|
5741
3a6086774d38
added #withTabsExpanded: (i.e. arg to specify tab-column width)
Claus Gittinger <cg@exept.de>
parents:
5739
diff
changeset
|
6039 |
|
3a6086774d38
added #withTabsExpanded: (i.e. arg to specify tab-column width)
Claus Gittinger <cg@exept.de>
parents:
5739
diff
changeset
|
6040 |
(String with:Character tab |
16202 | 6041 |
with:$1 |
6042 |
with:Character cr |
|
6043 |
with:Character tab |
|
6044 |
with:$2) withTabsExpanded |
|
5741
3a6086774d38
added #withTabsExpanded: (i.e. arg to specify tab-column width)
Claus Gittinger <cg@exept.de>
parents:
5739
diff
changeset
|
6045 |
" |
3a6086774d38
added #withTabsExpanded: (i.e. arg to specify tab-column width)
Claus Gittinger <cg@exept.de>
parents:
5739
diff
changeset
|
6046 |
|
3a6086774d38
added #withTabsExpanded: (i.e. arg to specify tab-column width)
Claus Gittinger <cg@exept.de>
parents:
5739
diff
changeset
|
6047 |
"Modified: 12.5.1996 / 13:05:10 / cg" |
9192 | 6048 |
! |
6049 |
||
6050 |
withTabsExpanded:numSpaces |
|
5741
3a6086774d38
added #withTabsExpanded: (i.e. arg to specify tab-column width)
Claus Gittinger <cg@exept.de>
parents:
5739
diff
changeset
|
6051 |
"return a string consisting of the receivers characters, |
8874 | 6052 |
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
|
6053 |
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
|
6054 |
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
|
6055 |
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
|
6056 |
|
8874 | 6057 |
|col "{ SmallInteger }" |
1381 | 6058 |
str ch |
6059 |
dstIdx "{ SmallInteger }" |
|
6060 |
newSz "{ SmallInteger }" |
|
6061 |
sz "{ SmallInteger }" |
|
6062 |
hasEmphasis e| |
|
608 | 6063 |
|
6064 |
(self includes:(Character tab)) ifFalse:[^ self]. |
|
1381 | 6065 |
|
6066 |
sz := self size. |
|
6067 |
||
6068 |
"/ count the new size first, instead of |
|
6069 |
"/ multiple resizing (better for large strings) |
|
6070 |
||
6071 |
col := 1. newSz := 0. |
|
6072 |
1 to:sz do:[:srcIdx | |
|
16202 | 6073 |
ch := self at:srcIdx. |
6074 |
ch == Character tab ifFalse:[ |
|
6075 |
col := col + 1. |
|
6076 |
newSz := newSz + 1. |
|
6077 |
ch == Character cr ifTrue:[ |
|
6078 |
col := 1 |
|
6079 |
]. |
|
6080 |
] ifTrue:[ |
|
6081 |
(col \\ numSpaces) to:numSpaces do:[:ii | |
|
6082 |
newSz := newSz + 1. |
|
6083 |
col := col + 1 |
|
6084 |
]. |
|
6085 |
] |
|
608 | 6086 |
]. |
1381 | 6087 |
|
6088 |
str := self species new:newSz. |
|
6089 |
||
6090 |
hasEmphasis := self hasChangeOfEmphasis. |
|
6091 |
||
6092 |
col := 1. dstIdx := 1. |
|
6093 |
1 to:sz do:[:srcIdx | |
|
16202 | 6094 |
ch := self at:srcIdx. |
6095 |
||
6096 |
ch == Character tab ifFalse:[ |
|
6097 |
col := col + 1. |
|
6098 |
ch == Character cr ifTrue:[ |
|
6099 |
col := 1 |
|
6100 |
]. |
|
6101 |
hasEmphasis ifTrue:[ |
|
6102 |
e := self emphasisAt:srcIdx. |
|
6103 |
str emphasisAt:dstIdx put:e |
|
6104 |
]. |
|
6105 |
str at:dstIdx put:ch. |
|
6106 |
dstIdx := dstIdx + 1 |
|
6107 |
] ifTrue:[ |
|
6108 |
(col \\ numSpaces) to:numSpaces do:[:ii | |
|
6109 |
str at:dstIdx put:Character space. |
|
6110 |
dstIdx := dstIdx + 1. |
|
6111 |
col := col + 1 |
|
6112 |
]. |
|
6113 |
] |
|
1381 | 6114 |
]. |
6115 |
^ str |
|
608 | 6116 |
|
6117 |
" |
|
8874 | 6118 |
('1' , Character tab asString , 'x') withTabsExpanded |
6119 |
('1' , Character tab asString , 'x') withTabsExpanded:4 |
|
6120 |
('12345' , Character tab asString , 'x') withTabsExpanded |
|
6121 |
('123456' , Character tab asString , 'x') withTabsExpanded |
|
6122 |
('1234567' , Character tab asString , 'x') withTabsExpanded |
|
6123 |
('12345678' , Character tab asString , 'x') withTabsExpanded |
|
6124 |
('123456789' , Character tab asString , 'x') withTabsExpanded |
|
735
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
678
diff
changeset
|
6125 |
|
608 | 6126 |
(String with:Character tab |
16202 | 6127 |
with:Character tab |
6128 |
with:$1) withTabsExpanded |
|
608 | 6129 |
|
6130 |
(String with:Character tab |
|
16202 | 6131 |
with:$1 |
6132 |
with:Character tab |
|
6133 |
with:$2) withTabsExpanded |
|
735
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
678
diff
changeset
|
6134 |
|
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
678
diff
changeset
|
6135 |
(String with:Character tab |
16202 | 6136 |
with:$1 |
6137 |
with:Character cr |
|
6138 |
with:Character tab |
|
6139 |
with:$2) withTabsExpanded |
|
608 | 6140 |
" |
735
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
678
diff
changeset
|
6141 |
|
1381 | 6142 |
"Modified: 12.5.1996 / 13:05:10 / cg" |
9192 | 6143 |
! |
6144 |
||
13987 | 6145 |
withoutAllSpaces |
16855 | 6146 |
"return a copy of the receiver with all whitespace removed" |
6147 |
||
13987 | 6148 |
^ self asCollectionOfWords asStringWith:''. |
6149 |
"/ |col string| |
|
6150 |
"/ |
|
6151 |
"/ col := self asCollectionOfWords. |
|
6152 |
"/ string := String new. |
|
6153 |
"/ col do:[:el | |
|
6154 |
"/ string := string,el |
|
6155 |
"/ ]. |
|
6156 |
"/ ^string |
|
6157 |
||
6158 |
" |
|
6159 |
'hello wwww' withoutAllSpaces |
|
6160 |
'hel lo www w' withoutAllSpaces |
|
6161 |
" |
|
6162 |
||
6163 |
"Modified: / 18.7.1998 / 22:53:08 / cg" |
|
6164 |
! |
|
6165 |
||
6166 |
withoutCEscapes |
|
15628 | 6167 |
"return a new string consisting of receiver's characters |
13987 | 6168 |
with all \X-character escapes replaced by corresponding-characters. |
6169 |
(similar to the way C-language Strings are converted). |
|
6170 |
The following escapes are supported: |
|
6171 |
\r return character |
|
6172 |
\n newline character |
|
6173 |
\b backspace character |
|
6174 |
\f formfeed character |
|
6175 |
\t tab character |
|
6176 |
\e escape character |
|
6177 |
\\ the \ character itself |
|
6178 |
\nnn three digit octal number defining the characters ascii value |
|
6179 |
\xnn two digit hex number defining the characters ascii value |
|
6180 |
\unnnn four digit hex number defining the characters unicode value |
|
6181 |
\Unnnnnnnn eight digit hex number defining the characters unicode value |
|
6182 |
\other other |
|
6183 |
||
6184 |
Notice, that \' is NOT a valid escape, since the general syntax of |
|
6185 |
string constants is not affected by this method. |
|
6186 |
||
6187 |
Although easily implementable, this is NOT done automatically |
|
6188 |
by the compiler (due to a lack of a language standard for this). |
|
6189 |
However, the compiler may detect sends ot #withEscapes to string literals |
|
6190 |
and place a modified string constant into the binary/byte-code. |
|
6191 |
Therefore, no runtime penalty will be payed for using these escapes. |
|
6192 |
(not in pre 2.11 versions) |
|
16863 | 6193 |
|
6194 |
This is the opposite of withCEscapes. |
|
13987 | 6195 |
" |
6196 |
||
6197 |
|val "{ SmallInteger }" |
|
6198 |
in out nextChar nDigits| |
|
6199 |
||
6200 |
" |
|
6201 |
first, see if there is any escape; if not, return the receiver unchanged |
|
6202 |
" |
|
6203 |
(self includes:$\) ifFalse:[^ self ]. |
|
6204 |
||
6205 |
self hasChangeOfEmphasis ifTrue:[ self error:'emphasis not supported' ]. |
|
6206 |
out := CharacterWriteStream on:(String new:self size - 1). |
|
6207 |
||
6208 |
in := ReadStream on:self. |
|
6209 |
[in atEnd] whileFalse:[ |
|
6210 |
nextChar := in next. |
|
6211 |
nextChar == $\ ifTrue:[ |
|
6212 |
in atEnd ifTrue:[ |
|
6213 |
] ifFalse:[ |
|
6214 |
nextChar := in next. |
|
6215 |
nextChar == $r ifTrue:[ |
|
6216 |
nextChar := Character return |
|
6217 |
] ifFalse:[ nextChar == $n ifTrue:[ |
|
6218 |
nextChar := Character nl |
|
6219 |
] ifFalse:[ nextChar == $b ifTrue:[ |
|
6220 |
nextChar := Character backspace |
|
6221 |
] ifFalse:[ nextChar == $f ifTrue:[ |
|
6222 |
nextChar := Character newPage |
|
6223 |
] ifFalse:[ nextChar == $t ifTrue:[ |
|
6224 |
nextChar := Character tab |
|
6225 |
] ifFalse:[ nextChar == $e ifTrue:[ |
|
6226 |
nextChar := Character esc |
|
6227 |
] ifFalse:[ |
|
6228 |
nextChar == $0 ifTrue:[ |
|
6229 |
val := 0. |
|
6230 |
nextChar := in peek. |
|
6231 |
nDigits := 1. |
|
6232 |
[nextChar notNil and:[nextChar isDigit and:[nDigits <= 3]]] whileTrue:[ |
|
6233 |
val := (val * 8) + nextChar digitValue. |
|
6234 |
nextChar := in nextPeek. |
|
6235 |
nDigits := nDigits + 1. |
|
6236 |
]. |
|
6237 |
nextChar := Character value:val. |
|
6238 |
] ifFalse:[ |
|
6239 |
val := 0. |
|
6240 |
nextChar == $x ifTrue:[ |
|
6241 |
2 timesRepeat:[ |
|
6242 |
nextChar := in next. |
|
6243 |
val := (val * 16) + nextChar digitValue. |
|
6244 |
]. |
|
6245 |
nextChar := Character value:val. |
|
6246 |
] ifFalse:[ |
|
6247 |
nextChar == $u ifTrue:[ |
|
6248 |
4 timesRepeat:[ |
|
6249 |
nextChar := in next. |
|
6250 |
val := (val * 16) + nextChar digitValue. |
|
6251 |
]. |
|
6252 |
nextChar := Character value:val. |
|
6253 |
] ifFalse:[ |
|
6254 |
nextChar == $U ifTrue:[ |
|
6255 |
8 timesRepeat:[ |
|
6256 |
nextChar := in next. |
|
6257 |
val := (val * 16) + nextChar digitValue. |
|
6258 |
]. |
|
6259 |
nextChar := Character value:val. |
|
6260 |
] |
|
6261 |
] |
|
6262 |
] |
|
6263 |
] |
|
6264 |
]]]]]]. |
|
6265 |
]. |
|
6266 |
]. |
|
6267 |
out nextPut:nextChar. |
|
6268 |
]. |
|
6269 |
^ out contents |
|
6270 |
||
6271 |
" |
|
6272 |
'hello world' withoutCEscapes |
|
6273 |
'hello\world' withoutCEscapes |
|
6274 |
'hello\world\' withoutCEscapes |
|
6275 |
'hello world\' withoutCEscapes |
|
6276 |
'hello\tworld' withoutCEscapes |
|
6277 |
'hello\nworld\na\n\tnice\n\t\tstring' withoutCEscapes |
|
6278 |
'hello\tworld\n' withoutCEscapes |
|
6279 |
'hello\010world' withoutCEscapes |
|
6280 |
'hello\r\nworld' withoutCEscapes |
|
6281 |
'hello\r\n\x08world' withoutCEscapes |
|
6282 |
'0\x080' withoutCEscapes |
|
6283 |
'0\u12340' withoutCEscapes |
|
6284 |
'0\U123456780' withoutCEscapes |
|
6285 |
'0\0a' withoutCEscapes |
|
6286 |
'0\00a' withoutCEscapes |
|
6287 |
'0\000a' withoutCEscapes |
|
6288 |
'0\0000a' withoutCEscapes |
|
6289 |
'0\00000a' withoutCEscapes |
|
6290 |
'0\03770' withoutCEscapes |
|
6291 |
" |
|
6292 |
||
6293 |
"Created: / 25-01-2012 / 10:41:44 / cg" |
|
6294 |
! |
|
6295 |
||
9192 | 6296 |
withoutCRs |
6297 |
"return a new collection consisting of receivers elements |
|
6298 |
with all cr-characters replaced by \-characters. |
|
6299 |
This is the reverse operation of withCRs." |
|
6300 |
||
16759 | 6301 |
^ self copyReplaceAll:(Character cr) with:$\ ifNone:self |
12319
18b80c2ce1e3
oops #withoutSeparators #withoutSpaces were noops
Claus Gittinger <cg@exept.de>
parents:
12317
diff
changeset
|
6302 |
|
9192 | 6303 |
" |
6304 |
'hello |
|
6305 |
world' withoutCRs |
|
6306 |
" |
|
6307 |
||
6308 |
"Modified: / 18.7.1998 / 22:53:08 / cg" |
|
6309 |
! |
|
6310 |
||
6311 |
withoutLeadingSeparators |
|
608 | 6312 |
"return a copy of myself without leading separators. |
6313 |
Notice: this does remove tabs, newline or any other whitespace. |
|
6314 |
Returns an empty string, if the receiver consist only of whitespace." |
|
6315 |
||
6316 |
|index| |
|
6317 |
||
6318 |
index := self indexOfNonSeparatorStartingAt:1. |
|
6319 |
index ~~ 0 ifTrue:[ |
|
16202 | 6320 |
index == 1 ifTrue:[ |
6321 |
^ self |
|
6322 |
]. |
|
6323 |
^ self copyFrom:index |
|
608 | 6324 |
]. |
6325 |
^ '' |
|
6326 |
||
6327 |
" |
|
8874 | 6328 |
' foo ' withoutLeadingSeparators |
6329 |
'foo ' withoutLeadingSeparators |
|
6330 |
' foo' withoutLeadingSeparators |
|
6331 |
' ' withoutLeadingSeparators |
|
6332 |
'foo' withoutLeadingSeparators |
|
6333 |
(' ' , Character tab asString , ' foo ') withoutLeadingSeparators inspect |
|
608 | 6334 |
" |
9192 | 6335 |
! |
6336 |
||
6337 |
withoutMatchEscapes |
|
4117 | 6338 |
"return a copy of the receiver with all $\ removed or |
6339 |
the receiver, if there are none." |
|
6340 |
||
5342
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
6341 |
|in out c escape| |
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
6342 |
|
5010cea0ffd4
match-escape character definition in one place
Claus Gittinger <cg@exept.de>
parents:
5316
diff
changeset
|
6343 |
escape := self class matchEscapeCharacter. |
4117 | 6344 |
|
6345 |
in := self readStream. |
|
8222
f2c454a9a038
replaced '' writeStream by String writeStream
Claus Gittinger <cg@exept.de>
parents:
8124
diff
changeset
|
6346 |
out := self species writeStream. |
4117 | 6347 |
[in atEnd] whileFalse:[ |
16202 | 6348 |
c := in next. |
6349 |
c == escape ifTrue:[ |
|
6350 |
in atEnd ifFalse:[ |
|
6351 |
c := in next. |
|
6352 |
] |
|
6353 |
]. |
|
6354 |
out nextPut:c. |
|
4117 | 6355 |
]. |
6356 |
^ out contents. |
|
6357 |
||
6358 |
" |
|
8874 | 6359 |
'*foo' withoutMatchEscapes |
6360 |
'\*foo' withoutMatchEscapes |
|
6361 |
'*foo' withoutMatchEscapes |
|
6362 |
'\\*foo' withoutMatchEscapes |
|
6363 |
'foo*' withoutMatchEscapes |
|
6364 |
'foo\*' withoutMatchEscapes |
|
6365 |
'foo\' withoutMatchEscapes |
|
6366 |
'f\*o\*o' withoutMatchEscapes |
|
4117 | 6367 |
" |
6368 |
||
6369 |
"Modified: 30.6.1997 / 13:40:23 / cg" |
|
9192 | 6370 |
! |
6371 |
||
6372 |
withoutPrefix:aString |
|
6373 |
"if the receiver startsWith aPrefix, return a copy without it. |
|
6374 |
Otherwise return the receiver" |
|
6375 |
||
6376 |
(self startsWith:aString) ifTrue:[ |
|
16202 | 6377 |
^ self copyFrom:aString size + 1 |
9192 | 6378 |
]. |
6379 |
^ self |
|
6380 |
||
6381 |
" |
|
6382 |
'helloworld' withoutPrefix:'hello' |
|
6383 |
'helloworld' withoutPrefix:'foo' |
|
6384 |
" |
|
6385 |
! |
|
6386 |
||
10671 | 6387 |
withoutQuotes |
6388 |
"remove quotes ("" and ') from the front and the end of myself" |
|
6389 |
||
6390 |
|result quote| |
|
6391 |
||
6392 |
result := self. |
|
6393 |
((result startsWith:$") or:[(result startsWith:$')]) ifTrue:[ |
|
15167
62b89f82633c
Changed usage of deprecated #copyWithoutLast: to #copyButLast:
Stefan Vogel <sv@exept.de>
parents:
15055
diff
changeset
|
6394 |
quote := result at:1. |
62b89f82633c
Changed usage of deprecated #copyWithoutLast: to #copyButLast:
Stefan Vogel <sv@exept.de>
parents:
15055
diff
changeset
|
6395 |
result := result copyFrom:2. |
62b89f82633c
Changed usage of deprecated #copyWithoutLast: to #copyButLast:
Stefan Vogel <sv@exept.de>
parents:
15055
diff
changeset
|
6396 |
(result endsWith:quote) ifTrue:[ |
62b89f82633c
Changed usage of deprecated #copyWithoutLast: to #copyButLast:
Stefan Vogel <sv@exept.de>
parents:
15055
diff
changeset
|
6397 |
result := result copyButLast:1 |
62b89f82633c
Changed usage of deprecated #copyWithoutLast: to #copyButLast:
Stefan Vogel <sv@exept.de>
parents:
15055
diff
changeset
|
6398 |
]. |
10671 | 6399 |
]. |
6400 |
^ result |
|
6401 |
||
16693 | 6402 |
"/ '"hello"' withoutQuotes |
6403 |
"/ '''hello''' withoutQuotes |
|
6404 |
"/ 'hello' withoutQuotes |
|
10671 | 6405 |
! |
6406 |
||
9192 | 6407 |
withoutSeparators |
608 | 6408 |
"return a copy of myself without leading and trailing whitespace. |
4156 | 6409 |
(but whiteSpace in-between is preserved) |
608 | 6410 |
Whitespace is space, tab, newline, formfeed. |
6411 |
Use withoutSpaces, if you want to remove spaces only." |
|
6412 |
||
12319
18b80c2ce1e3
oops #withoutSeparators #withoutSpaces were noops
Claus Gittinger <cg@exept.de>
parents:
12317
diff
changeset
|
6413 |
^ self trimForWhich:[:ch | ch isSeparator] |
608 | 6414 |
|
6415 |
" |
|
8874 | 6416 |
' foo ' withoutSeparators |
6417 |
' foo' withoutSeparators |
|
6418 |
'foo ' withoutSeparators |
|
6419 |
' ' withoutSeparators |
|
6420 |
(' foo' , Character tab asString , ' ') withoutSeparators inspect |
|
12319
18b80c2ce1e3
oops #withoutSeparators #withoutSpaces were noops
Claus Gittinger <cg@exept.de>
parents:
12317
diff
changeset
|
6421 |
' foo ' asUnicode16String withoutSeparators |
608 | 6422 |
" |
9192 | 6423 |
! |
6424 |
||
6425 |
withoutSpaces |
|
608 | 6426 |
"return a copy of myself without leading and trailing spaces. |
4156 | 6427 |
(but spaces in-between are preserved) |
608 | 6428 |
Notice: this does NOT remove tabs, newline or any other whitespace. |
6429 |
Use withoutSeparators for this." |
|
6430 |
||
12319
18b80c2ce1e3
oops #withoutSeparators #withoutSpaces were noops
Claus Gittinger <cg@exept.de>
parents:
12317
diff
changeset
|
6431 |
^ self trimForWhich:[:ch | ch == Character space] |
608 | 6432 |
|
6433 |
" |
|
8874 | 6434 |
' foo ' withoutSpaces |
6435 |
'foo ' withoutSpaces |
|
6436 |
' foo' withoutSpaces |
|
6437 |
' ' withoutSpaces |
|
6438 |
'a b' withoutSpaces |
|
6439 |
(' foo' , Character tab asString , ' ') withoutSpaces inspect |
|
608 | 6440 |
" |
9192 | 6441 |
! |
6442 |
||
6443 |
withoutTrailingSeparators |
|
915
1ac1c7e942f4
added withoutTrailingSeparators
Claus Gittinger <cg@exept.de>
parents:
828
diff
changeset
|
6444 |
"return a copy of myself without trailing separators. |
1ac1c7e942f4
added withoutTrailingSeparators
Claus Gittinger <cg@exept.de>
parents:
828
diff
changeset
|
6445 |
Notice: this does remove tabs, newline or any other whitespace. |
1ac1c7e942f4
added withoutTrailingSeparators
Claus Gittinger <cg@exept.de>
parents:
828
diff
changeset
|
6446 |
Returns an empty string, if the receiver consist only of whitespace." |
1ac1c7e942f4
added withoutTrailingSeparators
Claus Gittinger <cg@exept.de>
parents:
828
diff
changeset
|
6447 |
|
12188 | 6448 |
^ self withoutTrailingForWhich:[:ch | ch isSeparator] |
915
1ac1c7e942f4
added withoutTrailingSeparators
Claus Gittinger <cg@exept.de>
parents:
828
diff
changeset
|
6449 |
|
1ac1c7e942f4
added withoutTrailingSeparators
Claus Gittinger <cg@exept.de>
parents:
828
diff
changeset
|
6450 |
" |
8874 | 6451 |
' foo ' withoutTrailingSeparators |
6452 |
'foo ' withoutTrailingSeparators |
|
6453 |
' foo' withoutTrailingSeparators |
|
6454 |
' ' withoutTrailingSeparators |
|
6455 |
'foo' withoutTrailingSeparators |
|
6456 |
(' ' , Character tab asString , ' foo ') withoutTrailingSeparators inspect |
|
6457 |
(' foo' , Character tab asString) withoutTrailingSeparators inspect |
|
915
1ac1c7e942f4
added withoutTrailingSeparators
Claus Gittinger <cg@exept.de>
parents:
828
diff
changeset
|
6458 |
" |
9192 | 6459 |
! ! |
6460 |
||
6461 |
!CharacterArray methodsFor:'substring searching'! |
|
6462 |
||
15055 | 6463 |
findRangeOfString:subString |
6464 |
"find a substring. if found, return the start- and endIndex; |
|
6465 |
if not found, return an empty interval." |
|
6466 |
||
6467 |
^ self rangeOfSubCollection:subString startingAt:1 ifAbsent:[0 to:-1] caseSensitive:true |
|
6468 |
||
6469 |
" |
|
6470 |
'hello world' findRangeOfString:'llo' |
|
6471 |
'hello world' findRangeOfString:'ole' |
|
6472 |
" |
|
6473 |
! |
|
6474 |
||
9192 | 6475 |
findString:subString |
6476 |
"find a substring. if found, return the index; |
|
6477 |
if not found, return 0." |
|
6478 |
||
6479 |
^ self indexOfSubCollection:subString startingAt:1 ifAbsent:0 caseSensitive:true |
|
6480 |
||
6481 |
" |
|
6482 |
'hello world' findString:'llo' |
|
6483 |
'hello world' findString:'ole' |
|
6484 |
" |
|
6485 |
! |
|
608 | 6486 |
|
15734 | 6487 |
findString:subString caseSensitive:caseSensitive |
6488 |
"find a substring. if found, return the index; |
|
6489 |
if not found, return 0." |
|
6490 |
||
6491 |
^ self indexOfSubCollection:subString startingAt:1 ifAbsent:0 caseSensitive:caseSensitive |
|
6492 |
||
6493 |
" |
|
6494 |
'hello world' findString:'LLo' caseSensitive:true |
|
6495 |
'hello world' findString:'LLo' caseSensitive:false |
|
6496 |
" |
|
6497 |
! |
|
6498 |
||
608 | 6499 |
findString:subString ifAbsent:exceptionBlock |
6500 |
"find a substring. If found, return the index; |
|
6501 |
if not found, return the result of evaluating exceptionBlock." |
|
6502 |
||
9087
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
6503 |
^ self indexOfSubCollection:subString startingAt:1 ifAbsent:exceptionBlock caseSensitive:true |
608 | 6504 |
! |
6505 |
||
11018 | 6506 |
findString:subString ignoreCase:ignoreCase |
15734 | 6507 |
<resource: #obsolete> |
11018 | 6508 |
"find a substring. if found, return the index; |
6509 |
if not found, return 0." |
|
6510 |
||
6511 |
^ self indexOfSubCollection:subString startingAt:1 ifAbsent:0 caseSensitive:ignoreCase not |
|
6512 |
||
6513 |
" |
|
6514 |
'hello world' findString:'LLo' ignoreCase:false |
|
6515 |
'hello world' findString:'LLo' ignoreCase:true |
|
6516 |
" |
|
6517 |
! |
|
6518 |
||
608 | 6519 |
findString:subString startingAt:index |
6520 |
"find a substring, starting at index. if found, return the index; |
|
6521 |
if not found, return 0." |
|
6522 |
||
9087
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
6523 |
^ self indexOfSubCollection:subString startingAt:index ifAbsent:0 caseSensitive:true |
608 | 6524 |
|
6525 |
" |
|
9192 | 6526 |
'hello yello' findString:'llo' startingAt:1 |
6527 |
'hello yello' findString:'llo' startingAt:5 |
|
6528 |
'hello yello' findString:'llo' startingAt:15 |
|
6529 |
" |
|
6530 |
! |
|
608 | 6531 |
|
6532 |
findString:subString startingAt:index ifAbsent:exceptionBlock |
|
6533 |
"find a substring, starting at index. if found, return the index; |
|
6534 |
if not found, return the result of evaluating exceptionBlock." |
|
6535 |
||
9087
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
6536 |
^ self indexOfSubCollection:subString startingAt:index ifAbsent:exceptionBlock caseSensitive:true |
608 | 6537 |
! |
6538 |
||
12177 | 6539 |
findString:subString startingAt:index ifAbsent:exceptionBlock caseSensitive:caseSensitive |
10929 | 6540 |
"find a substring, starting at index. if found, return the index; |
6541 |
if not found, return the result of evaluating exceptionBlock." |
|
6542 |
||
6543 |
^ self indexOfSubCollection:subString startingAt:index ifAbsent:exceptionBlock caseSensitive:caseSensitive |
|
6544 |
! |
|
6545 |
||
608 | 6546 |
includesString:aString |
6547 |
"return true, if a substring is contained in the receiver" |
|
6548 |
||
9087
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
6549 |
^ (self indexOfSubCollection:aString startingAt:1 ifAbsent:0 caseSensitive:true) ~~ 0 |
608 | 6550 |
|
6551 |
" |
|
12177 | 6552 |
'hello world' includesString:'hel' |
8874 | 6553 |
'hello world' includesString:'rld' |
9192 | 6554 |
'hello world' includesString:'llo' |
6555 |
'hello world' includesString:'LLO' |
|
12177 | 6556 |
'hello world' includesString:'' |
9192 | 6557 |
" |
6558 |
! |
|
9191 | 6559 |
|
6560 |
includesString:aString caseSensitive:caseSensitive |
|
9087
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
6561 |
"return true, if a substring is contained in the receiver" |
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
6562 |
|
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
6563 |
^ (self indexOfSubCollection:aString startingAt:1 ifAbsent:0 caseSensitive:caseSensitive) ~~ 0 |
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
6564 |
|
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
6565 |
" |
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
6566 |
'hello world' includesString:'hel' caseSensitive:true |
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
6567 |
'hello world' includesString:'HEL' caseSensitive:true |
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
6568 |
'hello world' includesString:'HEL' caseSensitive:false |
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
6569 |
|
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
6570 |
'hello world' includesString:'wor' caseSensitive:true |
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
6571 |
'hello world' includesString:'WOR' caseSensitive:true |
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
6572 |
'hello world' includesString:'WOR' caseSensitive:false |
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
6573 |
" |
9192 | 6574 |
! |
6575 |
||
6576 |
indexOfSubCollection:subString startingAt:index ifAbsent:exceptionBlock |
|
6577 |
"find a substring, starting at index. if found, return the index; |
|
6578 |
if not found, return the result of evaluating exceptionBlock. |
|
6579 |
This is a q&d hack - not very efficient" |
|
6580 |
||
6581 |
^ self indexOfSubCollection:subString startingAt:index ifAbsent:exceptionBlock caseSensitive:true. |
|
6582 |
! |
|
6583 |
||
6584 |
indexOfSubCollection:subString startingAt:index ifAbsent:exceptionBlock caseSensitive:caseSensitive |
|
9087
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
6585 |
"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
|
6586 |
if not found, return the result of evaluating exceptionBlock. |
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
6587 |
This is a q&d hack - not very efficient" |
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
6588 |
|
608 | 6589 |
|firstChar found |
6590 |
startIndex "{ Class: SmallInteger }" |
|
6591 |
subSize "{ Class: SmallInteger }" |
|
6592 |
mySize "{ Class: SmallInteger }" |
|
10703
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
6593 |
runIdx "{ Class: SmallInteger }" |
9087
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
6594 |
tester| |
608 | 6595 |
|
6596 |
subSize := subString size. |
|
12177 | 6597 |
subSize == 0 ifTrue:[ |
15433 | 6598 |
subString isString ifFalse:[ |
6599 |
self error:'non string argument' mayProceed:true. |
|
6600 |
]. |
|
6601 |
"empty string does not match" |
|
6602 |
^ 0. |
|
6603 |
"empty string matches" |
|
6604 |
"/ ^ index |
|
6122
a98958f1c85f
handle non-string arg to indexOfSubString:
Claus Gittinger <cg@exept.de>
parents:
6119
diff
changeset
|
6605 |
]. |
9087
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
6606 |
|
608 | 6607 |
mySize := self size. |
6608 |
firstChar := subString at:1. |
|
9087
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
6609 |
caseSensitive ifTrue:[ |
15433 | 6610 |
tester := [:c1 :c2 | c1 = c2 ]. |
6611 |
startIndex := self indexOf:firstChar startingAt:index. |
|
9087
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
6612 |
] ifFalse:[ |
15433 | 6613 |
tester := [:c1 :c2 | c1 sameAs: c2 ]. |
6614 |
startIndex := self findFirst:[:c | c sameAs:firstChar] startingAt:index. |
|
9087
9e28a6168468
case-insensitive search cleanup
Claus Gittinger <cg@exept.de>
parents:
9058
diff
changeset
|
6615 |
]. |
608 | 6616 |
[startIndex == 0] whileFalse:[ |
15433 | 6617 |
runIdx := startIndex. |
6618 |
found := true. |
|
6619 |
1 to:subSize do:[:i | |
|
6620 |
runIdx > mySize ifTrue:[ |
|
6621 |
found := false |
|
6622 |
] ifFalse:[ |
|
6623 |
(tester value:(subString at:i) value:(self at:runIdx)) ifFalse:[ |
|
6624 |
found := false |
|
6625 |
] |
|
6626 |
]. |
|
6627 |
runIdx := runIdx + 1 |
|
6628 |
]. |
|
6629 |
found ifTrue:[ |
|
6630 |
^ startIndex |
|
6631 |
]. |
|
6632 |
caseSensitive ifTrue:[ |
|
6633 |
startIndex := self indexOf:firstChar startingAt:(startIndex + 1) |
|
6634 |
] ifFalse:[ |
|
6635 |
startIndex := self findFirst:[:c | c sameAs:firstChar] startingAt:(startIndex + 1). |
|
6636 |
]. |
|
608 | 6637 |
]. |
6638 |
^ exceptionBlock value |
|
1005
4507487e0be6
more on encoding/decoding (still unfinished)
Claus Gittinger <cg@exept.de>
parents:
997
diff
changeset
|
6639 |
|
4507487e0be6
more on encoding/decoding (still unfinished)
Claus Gittinger <cg@exept.de>
parents:
997
diff
changeset
|
6640 |
"Modified: 23.2.1996 / 15:35:15 / cg" |
9192 | 6641 |
! |
6642 |
||
15055 | 6643 |
rangeOfSubCollection:subString startingAt:start ifAbsent:exceptionValue caseSensitive:caseSensitive |
6644 |
"find a substring. if found, return the start- and endIndex; |
|
6645 |
if not found, return the value of exceptionValue." |
|
6646 |
||
6647 |
|i| |
|
6648 |
||
6649 |
i := self indexOfSubCollection:subString startingAt:start ifAbsent:0 caseSensitive:caseSensitive. |
|
6650 |
i == 0 ifTrue:[ |
|
6651 |
^ exceptionValue value |
|
6652 |
]. |
|
6653 |
^ i to:(i + subString size - 1) |
|
6654 |
||
6655 |
" |
|
6656 |
'hello world' findRangeOfString:'llo' |
|
6657 |
'hello world' findRangeOfString:'ole' |
|
6658 |
" |
|
6659 |
! |
|
6660 |
||
9192 | 6661 |
restAfter:keyword withoutSeparators:strip |
651
2093b018dc3a
added restAfter:withoutSeparators:
Claus Gittinger <cg@exept.de>
parents:
650
diff
changeset
|
6662 |
"compare the left of the receiver with keyword, |
8874 | 6663 |
if it matches return the right. |
651
2093b018dc3a
added restAfter:withoutSeparators:
Claus Gittinger <cg@exept.de>
parents:
650
diff
changeset
|
6664 |
Finally, if strip is true, remove whiteSpace. |
2093b018dc3a
added restAfter:withoutSeparators:
Claus Gittinger <cg@exept.de>
parents:
650
diff
changeset
|
6665 |
This method is used to match and extract lines of the form: |
16202 | 6666 |
something: rest |
759
908363ce8a32
interest is written with one 'r' (shame on me)
Claus Gittinger <cg@exept.de>
parents:
735
diff
changeset
|
6667 |
where we are interested in rest, but only if the receiver string |
8874 | 6668 |
begins with something. |
651
2093b018dc3a
added restAfter:withoutSeparators:
Claus Gittinger <cg@exept.de>
parents:
650
diff
changeset
|
6669 |
|
2093b018dc3a
added restAfter:withoutSeparators:
Claus Gittinger <cg@exept.de>
parents:
650
diff
changeset
|
6670 |
You may wonder why such a specialized method exists here |
2093b018dc3a
added restAfter:withoutSeparators:
Claus Gittinger <cg@exept.de>
parents:
650
diff
changeset
|
6671 |
- this is so common when processing mailboxes, |
2093b018dc3a
added restAfter:withoutSeparators:
Claus Gittinger <cg@exept.de>
parents:
650
diff
changeset
|
6672 |
rcs files, nntp/pop3 responses, that is was considered worth |
2093b018dc3a
added restAfter:withoutSeparators:
Claus Gittinger <cg@exept.de>
parents:
650
diff
changeset
|
6673 |
a special method here to avoid having the code below a hundred |
2093b018dc3a
added restAfter:withoutSeparators:
Claus Gittinger <cg@exept.de>
parents:
650
diff
changeset
|
6674 |
times in variuos places." |
2093b018dc3a
added restAfter:withoutSeparators:
Claus Gittinger <cg@exept.de>
parents:
650
diff
changeset
|
6675 |
|
2093b018dc3a
added restAfter:withoutSeparators:
Claus Gittinger <cg@exept.de>
parents:
650
diff
changeset
|
6676 |
|rest| |
2093b018dc3a
added restAfter:withoutSeparators:
Claus Gittinger <cg@exept.de>
parents:
650
diff
changeset
|
6677 |
|
2093b018dc3a
added restAfter:withoutSeparators:
Claus Gittinger <cg@exept.de>
parents:
650
diff
changeset
|
6678 |
(self startsWith:keyword) ifTrue:[ |
16202 | 6679 |
rest := self copyFrom:(keyword size + 1). |
6680 |
strip ifTrue:[ |
|
6681 |
rest := rest withoutSeparators |
|
6682 |
]. |
|
6683 |
^ rest |
|
651
2093b018dc3a
added restAfter:withoutSeparators:
Claus Gittinger <cg@exept.de>
parents:
650
diff
changeset
|
6684 |
]. |
2093b018dc3a
added restAfter:withoutSeparators:
Claus Gittinger <cg@exept.de>
parents:
650
diff
changeset
|
6685 |
^ nil |
2093b018dc3a
added restAfter:withoutSeparators:
Claus Gittinger <cg@exept.de>
parents:
650
diff
changeset
|
6686 |
|
2093b018dc3a
added restAfter:withoutSeparators:
Claus Gittinger <cg@exept.de>
parents:
650
diff
changeset
|
6687 |
" |
8874 | 6688 |
'foo: hello world' restAfter:'foo:' withoutSeparators:true |
6689 |
'funny: something' restAfter:'foo:' withoutSeparators:true |
|
6690 |
||
6691 |
'foo: hello world ' restAfter:'foo:' withoutSeparators:true |
|
6692 |
'foo: hello world ' restAfter:'foo:' withoutSeparators:false |
|
651
2093b018dc3a
added restAfter:withoutSeparators:
Claus Gittinger <cg@exept.de>
parents:
650
diff
changeset
|
6693 |
" |
2093b018dc3a
added restAfter:withoutSeparators:
Claus Gittinger <cg@exept.de>
parents:
650
diff
changeset
|
6694 |
|
2093b018dc3a
added restAfter:withoutSeparators:
Claus Gittinger <cg@exept.de>
parents:
650
diff
changeset
|
6695 |
"Created: 25.11.1995 / 11:04:18 / cg" |
15213 | 6696 |
! |
6697 |
||
6698 |
splitAtString:subString withoutSeparators:strip |
|
6699 |
"If the receiver is of the form: |
|
6700 |
<left><subString><right> |
|
6701 |
return a collection containing left and right only. |
|
6702 |
If strip is true, remove whiteSpace in the returned substrings." |
|
6703 |
||
6704 |
|idx left right| |
|
6705 |
||
6706 |
(idx := self indexOfSubCollection:subString) ~~ 0 ifTrue:[ |
|
6707 |
left := self copyTo:(idx - 1). |
|
6708 |
right := self copyFrom:(idx + subString size). |
|
6709 |
strip ifTrue:[ |
|
6710 |
left := left withoutSeparators. |
|
6711 |
right := right withoutSeparators. |
|
6712 |
]. |
|
6713 |
^ StringCollection with:left with:right |
|
6714 |
]. |
|
6715 |
self error:'substring not present in receiver' mayProceed:true. |
|
6716 |
^ self |
|
6717 |
||
6718 |
" |
|
6719 |
'hello -> world' splitAtString:'->' withoutSeparators:false |
|
6720 |
'hello -> world' splitAtString:'->' withoutSeparators:true |
|
6721 |
'hello -> ' splitAtString:'->' withoutSeparators:true |
|
6722 |
'hello > error' splitAtString:'->' withoutSeparators:true |
|
6723 |
" |
|
6724 |
||
6725 |
"Created: 25.11.1995 / 11:04:18 / cg" |
|
9192 | 6726 |
! ! |
6727 |
||
6728 |
!CharacterArray methodsFor:'testing'! |
|
6729 |
||
15829 | 6730 |
contains8BitCharacters |
6731 |
"return true, if the underlying string contains 8BitCharacters (or widers) |
|
6732 |
(i.e. if it is non-ascii)" |
|
6733 |
||
6734 |
|string| |
|
6735 |
||
6736 |
(string := self string) ~~ self ifTrue:[ |
|
16202 | 6737 |
^ string contains8BitCharacters |
15829 | 6738 |
]. |
6739 |
^ self contains:[:aCharacter | aCharacter codePoint > 16r7F ]. |
|
6740 |
||
6741 |
" |
|
6742 |
'hello' contains8BitCharacters |
|
6743 |
'hello' asText allBold contains8BitCharacters |
|
6744 |
" |
|
6745 |
! |
|
6746 |
||
9192 | 6747 |
isAlphaNumeric |
6748 |
"return true, if the receiver is some alphanumeric word; |
|
6749 |
i.e. consists of a letter followed by letters or digits." |
|
6750 |
||
6751 |
self size == 0 ifTrue:[ |
|
16202 | 6752 |
"mhmh what is this ?" |
6753 |
^ false |
|
9192 | 6754 |
]. |
6755 |
(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
|
6756 |
^ self conform:[:char | char isLetterOrDigit]. |
8cff5eca1c6f
code cleanup: use #contains/#conform instead of explicit loop
Claus Gittinger <cg@exept.de>
parents:
10088
diff
changeset
|
6757 |
|
8cff5eca1c6f
code cleanup: use #contains/#conform instead of explicit loop
Claus Gittinger <cg@exept.de>
parents:
10088
diff
changeset
|
6758 |
" |
10703
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
6759 |
'helloWorld' isAlphaNumeric |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
6760 |
'foo1234' isAlphaNumeric |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
6761 |
'f1234' isAlphaNumeric |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
6762 |
'1234' isAlphaNumeric |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
6763 |
'+' isAlphaNumeric |
10093
8cff5eca1c6f
code cleanup: use #contains/#conform instead of explicit loop
Claus Gittinger <cg@exept.de>
parents:
10088
diff
changeset
|
6764 |
" |
8cff5eca1c6f
code cleanup: use #contains/#conform instead of explicit loop
Claus Gittinger <cg@exept.de>
parents:
10088
diff
changeset
|
6765 |
|
8cff5eca1c6f
code cleanup: use #contains/#conform instead of explicit loop
Claus Gittinger <cg@exept.de>
parents:
10088
diff
changeset
|
6766 |
"Modified: / 13-10-2006 / 12:53:49 / cg" |
9192 | 6767 |
! |
9190 | 6768 |
|
6769 |
isBinarySelector |
|
10903 | 6770 |
"treating the receiver as a message selector, return true if its a binary selector. |
6771 |
Notice, that st/x does not have a size <= 2 limit for unaries" |
|
9190 | 6772 |
|
6773 |
|binopChars| |
|
6774 |
||
13332 | 6775 |
(self size > Scanner maxBinarySelectorSize) ifTrue:[^ false]. |
9190 | 6776 |
|
6777 |
binopChars := Scanner binarySelectorCharacters. |
|
6778 |
^ (self conform:[:char | (binopChars includes:char)]) |
|
6779 |
||
6780 |
" |
|
6781 |
'foo:bar:' isBinarySelector |
|
6782 |
#foo:bar: isBinarySelector |
|
6783 |
'hello' isBinarySelector |
|
6784 |
'+' isBinarySelector |
|
6785 |
'|' isBinarySelector |
|
8874 | 6786 |
'?' isBinarySelector |
6787 |
':' isBinarySelector |
|
6788 |
'a:' isBinarySelector |
|
6789 |
'->' isBinarySelector |
|
6790 |
'<->' isBinarySelector |
|
6791 |
'::' isBinarySelector |
|
7482 | 6792 |
" |
6793 |
||
6794 |
"Modified: 4.1.1997 / 14:16:14 / cg" |
|
9192 | 6795 |
! |
6796 |
||
6797 |
isBlank |
|
6798 |
"return true, if the receiver contains spaces only" |
|
6799 |
||
9230 | 6800 |
^ (self contains:[:char | char ~~ Character space]) not |
6801 |
||
6802 |
" |
|
10703
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
6803 |
'' isBlank |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
6804 |
' a ' isBlank |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
6805 |
' ' isBlank |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
6806 |
' ' asUnicode16String isBlank |
9230 | 6807 |
" |
9192 | 6808 |
! |
6809 |
||
10903 | 6810 |
isKeywordSelector |
6811 |
"return true, iff there are only alphanumeric or underline characters separated by colons. |
|
6812 |
Must end with a colon." |
|
6813 |
||
6814 |
|state| |
|
6815 |
||
12177 | 6816 |
state := #initial. |
10903 | 6817 |
self do:[:char | |
16202 | 6818 |
(state == #initial or:[ state == #gotColon]) ifTrue:[ |
6819 |
(char isLetter or:[ char == $_ ]) ifFalse:[^ false]. |
|
6820 |
state := #gotCharacter. |
|
6821 |
] ifFalse:[ |
|
6822 |
char == $: ifTrue:[ |
|
6823 |
state := #gotColon. |
|
6824 |
] ifFalse:[ |
|
6825 |
(char isLetterOrDigit or:[char == $_]) ifFalse:[^ false]. |
|
6826 |
]. |
|
6827 |
]. |
|
10903 | 6828 |
]. |
6829 |
^ state == #gotColon. |
|
6830 |
||
6831 |
" |
|
12177 | 6832 |
self assert:(':' isKeywordSelector not). |
10903 | 6833 |
self assert:(':a' isKeywordSelector not). |
6834 |
self assert:('1:' isKeywordSelector not). |
|
6835 |
self assert:('a:' isKeywordSelector). |
|
6836 |
self assert:('_:' isKeywordSelector). |
|
6837 |
self assert:('_a:' isKeywordSelector). |
|
6838 |
self assert:('_1:' isKeywordSelector). |
|
6839 |
self assert:('_1::' isKeywordSelector not). |
|
6840 |
self assert:('_:_:' isKeywordSelector). |
|
6841 |
self assert:('a:b:' isKeywordSelector). |
|
6842 |
self assert:('aa:bb:' isKeywordSelector). |
|
6843 |
self assert:('aa:bb:a' isKeywordSelector not). |
|
6844 |
self assert:('1:2:' isKeywordSelector not). |
|
6845 |
" |
|
6846 |
! |
|
6847 |
||
10431 | 6848 |
isNameSpaceSelector |
6849 |
"Answer true if the receiver contains chars which form a nameSpace selector name. |
|
6850 |
These are of the form ':<ns>::<sel>', where ns is the NameSpace and sel is the regular selector. |
|
6851 |
For example, the #+ selector as seen by the Foo namespace would be actually #':Foo::+'. |
|
6852 |
You cannot easily change this algorithm here, as it is also known by the VM's lookup function." |
|
6853 |
||
6854 |
|i| |
|
6855 |
||
16733 | 6856 |
(self at:1) == $: ifFalse:[^ false]. |
10431 | 6857 |
i := self indexOf:$: startingAt:2. |
6858 |
i == 0 ifTrue:[^ false]. |
|
6859 |
self size <= (i+1) ifTrue:[^ false]. |
|
6860 |
(self at:i+1) == $: ifFalse:[^ false]. |
|
6861 |
(self at:i+2) == $: ifTrue:[^ false]. |
|
6862 |
"/ could check if the rest after the ns-prefix and colons is a valid selector... |
|
6863 |
^ true |
|
6864 |
||
6865 |
"test: |
|
10703
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
6866 |
self assert:('+' isNameSpaceSelector) not. |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
6867 |
self assert:(':+' isNameSpaceSelector) not. |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
6868 |
self assert:(':Foo:+' isNameSpaceSelector) not. |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
6869 |
|
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
6870 |
self assert:(':Foo::+' isNameSpaceSelector). |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
6871 |
self assert:(':Foo::bar:baz:' isNameSpaceSelector). |
10431 | 6872 |
" |
6873 |
||
6874 |
"Created: / 05-03-2007 / 11:35:31 / cg" |
|
6875 |
! |
|
6876 |
||
9192 | 6877 |
isNumeric |
6878 |
"return true, if the receiver is some numeric word; |
|
6879 |
i.e. consists only of digits." |
|
6880 |
||
6881 |
self size == 0 ifTrue:[ |
|
16202 | 6882 |
^ false |
9192 | 6883 |
]. |
10093
8cff5eca1c6f
code cleanup: use #contains/#conform instead of explicit loop
Claus Gittinger <cg@exept.de>
parents:
10088
diff
changeset
|
6884 |
^ self conform:[:char | char isDigit] |
9192 | 6885 |
|
6886 |
" |
|
6887 |
'helloWorld' isNumeric |
|
6888 |
'foo1234' isNumeric |
|
6889 |
'f1234' isNumeric |
|
6890 |
'1234' isNumeric |
|
6891 |
'+' isNumeric |
|
6892 |
" |
|
10093
8cff5eca1c6f
code cleanup: use #contains/#conform instead of explicit loop
Claus Gittinger <cg@exept.de>
parents:
10088
diff
changeset
|
6893 |
|
8cff5eca1c6f
code cleanup: use #contains/#conform instead of explicit loop
Claus Gittinger <cg@exept.de>
parents:
10088
diff
changeset
|
6894 |
"Modified: / 13-10-2006 / 12:54:12 / cg" |
9192 | 6895 |
! |
6896 |
||
9230 | 6897 |
isString |
6898 |
"return true, if the receiver is some kind of string; |
|
6899 |
true is returned here - redefinition of Object>>isString." |
|
6900 |
||
6901 |
^ true |
|
6902 |
! |
|
6903 |
||
6904 |
isUnarySelector |
|
6905 |
"Answer true if the receiver contains only chars in an ANSI unary method selector, false otherwise." |
|
6906 |
||
9471 | 6907 |
^ (self first isLetter or:[ self first == $_ ]) |
9845 | 6908 |
and:[ self conform: [ :chr | chr isLetterOrDigit or:[chr == $_] ]] |
6909 |
||
6910 |
"Modified: / 13-09-2006 / 11:35:15 / cg" |
|
9230 | 6911 |
! |
6912 |
||
14122 | 6913 |
isUnicode16String |
6914 |
^ false |
|
6915 |
! |
|
6916 |
||
6917 |
isUnicode32String |
|
6918 |
^ false |
|
6919 |
! |
|
6920 |
||
6921 |
isUnicodeString |
|
6922 |
^ false |
|
6923 |
! |
|
6924 |
||
9230 | 6925 |
isValidSmalltalkIdentifier |
6926 |
"return true, if the receivers characters make up a valid smalltalk identifier" |
|
6927 |
||
6928 |
|scanner tok| |
|
6929 |
||
6930 |
scanner := Compiler new. |
|
6931 |
scanner source:(self readStream). |
|
11630
6db5956f188f
isValidSmalltalkIdentifier: return true/false (catch parseError)
Claus Gittinger <cg@exept.de>
parents:
11565
diff
changeset
|
6932 |
Parser parseErrorSignal handle:[:ex | |
16202 | 6933 |
tok := nil. |
11630
6db5956f188f
isValidSmalltalkIdentifier: return true/false (catch parseError)
Claus Gittinger <cg@exept.de>
parents:
11565
diff
changeset
|
6934 |
] do:[ |
16202 | 6935 |
tok := scanner nextToken. |
11630
6db5956f188f
isValidSmalltalkIdentifier: return true/false (catch parseError)
Claus Gittinger <cg@exept.de>
parents:
11565
diff
changeset
|
6936 |
]. |
9230 | 6937 |
tok ~~ #Identifier ifTrue:[ |
16202 | 6938 |
^ false |
9230 | 6939 |
]. |
6940 |
scanner tokenPosition == 1 ifFalse:[^ false]. |
|
6941 |
^ scanner sourceStream atEnd. |
|
6942 |
||
6943 |
" |
|
6944 |
'foo' isValidSmalltalkIdentifier |
|
6945 |
'1foo' isValidSmalltalkIdentifier |
|
6946 |
'_foo' isValidSmalltalkIdentifier |
|
6947 |
'_foo_bar_' isValidSmalltalkIdentifier |
|
6948 |
'foo ' isValidSmalltalkIdentifier |
|
6949 |
' foo' isValidSmalltalkIdentifier |
|
6950 |
" |
|
6951 |
! |
|
6952 |
||
14555 | 6953 |
isWideString |
15829 | 6954 |
|string| |
6955 |
||
6956 |
(string := self string) ~~ self ifTrue:[ |
|
6957 |
^ string isWideString. |
|
6958 |
]. |
|
6959 |
^ self contains:[:aCharacter | aCharacter codePoint > 16rFF]. |
|
14555 | 6960 |
! |
6961 |
||
9192 | 6962 |
numArgs |
608 | 6963 |
"treating the receiver as a message selector, return how many arguments would it take" |
6964 |
||
9038
d75b89570ec0
numArgs fixed (binOps can now be longer)
Claus Gittinger <cg@exept.de>
parents:
9019
diff
changeset
|
6965 |
|binopChars| |
d75b89570ec0
numArgs fixed (binOps can now be longer)
Claus Gittinger <cg@exept.de>
parents:
9019
diff
changeset
|
6966 |
|
d75b89570ec0
numArgs fixed (binOps can now be longer)
Claus Gittinger <cg@exept.de>
parents:
9019
diff
changeset
|
6967 |
(self size <= Scanner maxBinarySelectorSize) ifTrue:[ |
16202 | 6968 |
binopChars := Scanner binarySelectorCharacters. |
6969 |
(self conform:[:eachChar | (binopChars includes:eachChar)]) ifTrue:[ |
|
6970 |
^ 1 |
|
6971 |
]. |
|
608 | 6972 |
]. |
6973 |
^ self occurrencesOf:$: |
|
6974 |
||
6975 |
" |
|
10703
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
6976 |
'foo:bar:' numArgs |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
6977 |
#foo:bar: numArgs |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
6978 |
'hello' numArgs |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
6979 |
'+' numArgs |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
6980 |
'++' numArgs |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
6981 |
'+++' numArgs |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
6982 |
'|' numArgs |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
6983 |
'?' numArgs |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
6984 |
'_' numArgs |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
6985 |
'_:' numArgs |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
6986 |
'_:_:' numArgs |
d1e981878c9c
Work around stx bug when compiling hash
Stefan Vogel <sv@exept.de>
parents:
10698
diff
changeset
|
6987 |
'<->' numArgs |
608 | 6988 |
" |
2051
08fd6a693ee3
ask Scanner for valid binarySelector characters in numArgs
Claus Gittinger <cg@exept.de>
parents:
1978
diff
changeset
|
6989 |
|
10088 | 6990 |
"Modified: / 13-10-2006 / 11:52:33 / cg" |
9192 | 6991 |
! |
6992 |
||
6993 |
partsIfSelector |
|
14629 | 6994 |
"treat the receiver as a message selector, return a collection of parts. |
6995 |
Notice: this is more tolerant than Smalltalk's syntax would suggest; |
|
6996 |
especially it allows for empty keyword parts between colons. |
|
6997 |
This is not (and should not be checked here), to allow parsing of |
|
6998 |
degenerate selectors as appearing with objectiveC." |
|
608 | 6999 |
|
9192 | 7000 |
|idx1 "{ Class: SmallInteger }" |
7001 |
coll idx2 sz| |
|
7002 |
||
7003 |
coll := OrderedCollection new. |
|
7004 |
idx1 := 1. |
|
7005 |
sz := self size. |
|
16245 | 7006 |
[ |
14629 | 7007 |
idx2 := self indexOf:$: startingAt:idx1. |
7008 |
(idx2 == 0 or:[idx2 == sz]) ifTrue:[ |
|
7009 |
coll add:(self copyFrom:idx1). |
|
7010 |
^ coll |
|
7011 |
]. |
|
7012 |
coll add:(self copyFrom:idx1 to:idx2). |
|
7013 |
idx1 := idx2 + 1 |
|
16245 | 7014 |
] loop. |
9192 | 7015 |
|
7016 |
" |
|
14631 | 7017 |
'foo:' partsIfSelector |
7018 |
'foo:bar:' partsIfSelector |
|
7019 |
'foo::::' partsIfSelector |
|
7020 |
#foo:bar: partsIfSelector |
|
7021 |
'hello' partsIfSelector |
|
9192 | 7022 |
'+' partsIfSelector |
7023 |
" |
|
64 | 7024 |
! ! |
7025 |
||
9192 | 7026 |
!CharacterArray methodsFor:'tracing'! |
7027 |
||
7028 |
traceInto:aRequestor level:level from:referrer |
|
7029 |
"double dispatch into tracer, passing my type implicitely in the selector" |
|
7030 |
||
7031 |
^ aRequestor traceCharacterArray:self level:level from:referrer |
|
7032 |
||
7033 |
||
7034 |
! ! |
|
7035 |
||
7036 |
!CharacterArray methodsFor:'visiting'! |
|
8395
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8376
diff
changeset
|
7037 |
|
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8376
diff
changeset
|
7038 |
acceptVisitor:aVisitor with:aParameter |
16733 | 7039 |
"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
|
7040 |
|
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8376
diff
changeset
|
7041 |
^ aVisitor visitString:self with:aParameter |
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8376
diff
changeset
|
7042 |
! ! |
6bd97113cb4c
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8376
diff
changeset
|
7043 |
|
1849
9f35f5934ec6
added msdos-latin1 en/decoder (code = #msdos850)
Claus Gittinger <cg@exept.de>
parents:
1669
diff
changeset
|
7044 |
!CharacterArray class methodsFor:'documentation'! |
629 | 7045 |
|
7046 |
version |
|
16887 | 7047 |
^ '$Header: /cvs/stx/stx/libbasic/CharacterArray.st,v 1.544 2014-10-08 08:49:47 cg Exp $' |
12058 | 7048 |
! |
7049 |
||
7050 |
version_CVS |
|
16887 | 7051 |
^ '$Header: /cvs/stx/stx/libbasic/CharacterArray.st,v 1.544 2014-10-08 08:49:47 cg Exp $' |
629 | 7052 |
! ! |
6809 | 7053 |
|
14606 | 7054 |
|
2728
7569e45a4d15
raise error signal if anything goes wrong while decoding.
Claus Gittinger <cg@exept.de>
parents:
2721
diff
changeset
|
7055 |
CharacterArray initialize! |