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