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