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