author | Claus Gittinger <cg@exept.de> |
Sat, 23 Dec 1995 17:33:30 +0100 | |
changeset 815 | 997a02c135e7 |
parent 759 | 908363ce8a32 |
child 828 | a1922f7414ff |
permissions | -rw-r--r-- |
64 | 1 |
" |
2 |
COPYRIGHT (c) 1994 by Claus Gittinger |
|
154 | 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 |
" |
|
12 |
||
255 | 13 |
ByteArray subclass:#CharacterArray |
735
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
678
diff
changeset
|
14 |
instanceVariableNames:'' |
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
678
diff
changeset
|
15 |
classVariableNames:'PreviousMatch' |
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
678
diff
changeset
|
16 |
poolDictionaries:'' |
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
678
diff
changeset
|
17 |
category:'Collections-Text' |
64 | 18 |
! |
19 |
||
255 | 20 |
!CharacterArray class methodsFor:'documentation'! |
64 | 21 |
|
82 | 22 |
copyright |
23 |
" |
|
24 |
COPYRIGHT (c) 1994 by Claus Gittinger |
|
154 | 25 |
All Rights Reserved |
82 | 26 |
|
27 |
This software is furnished under a license and may be used |
|
28 |
only in accordance with the terms of that license and with the |
|
29 |
inclusion of the above copyright notice. This software may not |
|
30 |
be provided or otherwise made available to, or used by, any |
|
31 |
other person. No title to or ownership of the software is |
|
32 |
hereby transferred. |
|
33 |
" |
|
34 |
! |
|
35 |
||
64 | 36 |
documentation |
37 |
" |
|
255 | 38 |
CharacterArray is a superclass for all kinds of Strings (i.e. |
68 | 39 |
(singleByte-)Strings, TwoByteStrings and whatever comes in the future. |
154 | 40 |
|
41 |
As the name already implies, this class is abstract, meaning that there are |
|
42 |
no instances of it. All this class does is provide common protocol for |
|
43 |
concrete subclasses. |
|
64 | 44 |
" |
45 |
! ! |
|
46 |
||
255 | 47 |
!CharacterArray class methodsFor:'instance creation'! |
64 | 48 |
|
49 |
basicNew |
|
50 |
"return a new empty string" |
|
51 |
||
52 |
^ self basicNew:0 |
|
53 |
! |
|
54 |
||
55 |
fromString:aString |
|
56 |
"return a copy of the argument, aString" |
|
57 |
||
68 | 58 |
^ (self basicNew:(aString size)) replaceFrom:1 with:aString |
59 |
||
60 |
"TwoByteString fromString:'hello'" |
|
581 | 61 |
! |
62 |
||
63 |
fromStringCollection:aCollectionOfStrings |
|
64 |
"return new string formed by concatenating a copy of the argument, aString" |
|
65 |
||
66 |
^ self fromStringCollection:aCollectionOfStrings separatedBy:'' |
|
67 |
||
68 |
" |
|
69 |
String fromStringCollection:#('hello' 'world' 'how' 'about' 'this') |
|
70 |
" |
|
71 |
||
72 |
"Created: 20.11.1995 / 15:26:59 / cg" |
|
73 |
! |
|
74 |
||
75 |
fromStringCollection:aCollectionOfStrings separatedBy:aSeparatorString |
|
76 |
"return new string formed by concatenating a copy of the argument, aString" |
|
77 |
||
78 |
|newString first| |
|
79 |
||
80 |
newString := ''. |
|
81 |
first := true. |
|
82 |
aCollectionOfStrings do:[:s | |
|
83 |
first ifFalse:[ |
|
84 |
newString := newString , aSeparatorString |
|
85 |
] ifTrue:[ |
|
86 |
first := false |
|
87 |
]. |
|
88 |
newString := newString , s |
|
89 |
]. |
|
90 |
^ newString |
|
91 |
||
92 |
" |
|
93 |
String fromStringCollection:#('hello' 'world' 'how' 'about' 'this') separatedBy:' ' |
|
94 |
" |
|
95 |
||
96 |
"Created: 20.11.1995 / 15:32:17 / cg" |
|
608 | 97 |
! |
98 |
||
99 |
new |
|
100 |
"return a new empty string" |
|
101 |
||
102 |
^ self basicNew:0 |
|
64 | 103 |
! ! |
104 |
||
443 | 105 |
!CharacterArray class methodsFor:'code tables'! |
106 |
||
107 |
decoderTableFor:encoding |
|
108 |
"this is an experimental interface - unfinished" |
|
109 |
||
110 |
|table| |
|
111 |
||
112 |
table := (0 to:255) asByteArray. |
|
113 |
encoding == #mac ifTrue:[ |
|
678
723357b38138
no 8bit chars in comments (realIX's lex is not 8bit clean)
Claus Gittinger <cg@exept.de>
parents:
657
diff
changeset
|
114 |
table at:8r232+1 put:246. "/ german umlaut o (mac: 154; 8859: 246) |
657 | 115 |
table at:8r212+1 put:228. "/ german umlaut a (mac: 138; 8859: 228) |
116 |
table at:8r237+1 put:252. "/ german umlaut u (mac: 159; 8859: 252) |
|
117 |
table at:8r206+1 put:220. "/ german umlaut U (mac: 134; 8859: 220) |
|
118 |
"/ more needed here - need info to do it .... |
|
119 |
]. |
|
120 |
encoding == #postscript ifTrue:[ |
|
678
723357b38138
no 8bit chars in comments (realIX's lex is not 8bit clean)
Claus Gittinger <cg@exept.de>
parents:
657
diff
changeset
|
121 |
table at:8r224+1 put:246. "/ german umlaut o (ps: 148; 8859: 246) |
657 | 122 |
table at:8r204+1 put:228. "/ german umlaut a (ps: 132; 8859: 228) |
123 |
table at:8r201+1 put:252. "/ german umlaut u (ps: 129; 8859: 252) |
|
124 |
table at:8r231+1 put:214. "/ german umlaut O (ps: 153; 8859: 214) |
|
125 |
table at:8r216+1 put:196. "/ german umlaut A (ps: 142; 8859: 196) |
|
126 |
table at:8r232+1 put:220. "/ german umlaut U (ps: 154; 8859: 220) |
|
443 | 127 |
"/ more needed here - need info to do it .... |
128 |
]. |
|
129 |
"/ more encodings needed here .... |
|
130 |
||
131 |
"Created: 20.10.1995 / 23:04:43 / cg" |
|
132 |
! ! |
|
133 |
||
328 | 134 |
!CharacterArray class methodsFor:'pattern matching'! |
64 | 135 |
|
327 | 136 |
matchScan:matchScanArray from:matchStart to:matchStop with:aString from:start to:stop ignoreCase:ignoreCase |
137 |
"helper for match; return true if the characters from start to stop in |
|
138 |
aString are matching the scan in matchScan from matchStart to matchStop. |
|
328 | 139 |
The matchScan is as created by asMatchScanArray. |
327 | 140 |
|
328 | 141 |
This algorithm is not at all the most efficient; |
142 |
for heavy duty pattern matching, an interface (primitive) to the regex |
|
143 |
pattern matching package should be added." |
|
327 | 144 |
|
145 |
|matchEntry |
|
146 |
mStart "{ Class: SmallInteger }" |
|
147 |
mStop "{ Class: SmallInteger }" |
|
148 |
sStart "{ Class: SmallInteger }" |
|
149 |
sStop "{ Class: SmallInteger }" |
|
150 |
mSize "{ Class: SmallInteger }" |
|
151 |
sSize "{ Class: SmallInteger }" |
|
152 |
index "{ Class: SmallInteger }" |
|
153 |
quickCheck matchLast |
|
154 |
matchSet checkChar included| |
|
155 |
||
156 |
mStart := matchStart. |
|
157 |
mStop := matchStop. |
|
158 |
sStart := start. |
|
159 |
sStop := stop. |
|
160 |
||
161 |
[true] whileTrue:[ |
|
162 |
mSize := mStop - mStart + 1. |
|
163 |
sSize := sStop - sStart + 1. |
|
164 |
||
165 |
"empty strings match" |
|
166 |
(mSize == 0) ifTrue:[^ (sSize == 0)]. |
|
167 |
||
168 |
matchEntry := matchScanArray at:mStart. |
|
169 |
||
170 |
"/ the most common case first: |
|
171 |
(sSize ~~ 0 |
|
172 |
and:[(checkChar := (aString at:sStart)) == matchEntry]) ifTrue:[ |
|
173 |
"advance by one and continue" |
|
174 |
mStart := mStart + 1. |
|
175 |
sStart := sStart + 1 |
|
176 |
] ifFalse:[ |
|
177 |
(matchEntry == #any) ifTrue:[ |
|
178 |
"restString empty -> no match" |
|
179 |
(sSize == 0) ifTrue:[^ false]. |
|
180 |
"# matches single character" |
|
181 |
((sSize == 1) and:[mSize == 1]) ifTrue:[^ true]. |
|
182 |
"advance by one and continue" |
|
183 |
mStart := mStart + 1. |
|
184 |
sStart := sStart + 1 |
|
185 |
] ifFalse:[ |
|
186 |
(matchEntry == #anyString) ifTrue:[ |
|
187 |
"* alone matches anything" |
|
188 |
(mSize == 1) ifTrue:[^ true]. |
|
189 |
"restString empty & matchString not empty -> no match" |
|
190 |
(sSize == 0) ifTrue:[^ false]. |
|
191 |
||
192 |
" |
|
193 |
try to avoid some of the recursion by checking last |
|
194 |
character and continue with shortened strings if possible |
|
195 |
" |
|
196 |
quickCheck := false. |
|
197 |
(mStop >= mStart) ifTrue:[ |
|
198 |
matchLast := matchScanArray at:mStop. |
|
199 |
(matchLast ~~ #anyString) ifTrue:[ |
|
200 |
(matchLast == #any) ifTrue:[ |
|
201 |
quickCheck := true |
|
202 |
] ifFalse:[ |
|
203 |
matchLast == (aString at:sStop) ifTrue:[ |
|
204 |
quickCheck := true |
|
205 |
] ifFalse:[ |
|
206 |
matchLast isString ifTrue:[ |
|
207 |
quickCheck := matchLast includes:(aString at:sStop) |
|
208 |
] |
|
209 |
] |
|
210 |
] |
|
211 |
] |
|
212 |
]. |
|
213 |
quickCheck ifFalse:[ |
|
214 |
" |
|
215 |
no quick check possible; |
|
216 |
loop over all possible substrings |
|
217 |
" |
|
218 |
index := sStart. |
|
219 |
[index <= sStop] whileTrue:[ |
|
220 |
(self matchScan:matchScanArray |
|
221 |
from:(mStart + 1) |
|
222 |
to:mStop |
|
223 |
with:aString |
|
224 |
from:index |
|
225 |
to:stop |
|
226 |
ignoreCase:ignoreCase) ifTrue:[ |
|
227 |
^ true |
|
228 |
]. |
|
229 |
index := index + 1 |
|
230 |
]. |
|
231 |
^ false |
|
232 |
]. |
|
233 |
" |
|
234 |
quickCheck ok, advance from the right |
|
235 |
" |
|
236 |
mStop := mStop - 1. |
|
237 |
sStop := sStop - 1 |
|
238 |
] ifFalse:[ |
|
239 |
(matchEntry isString) ifTrue:[ |
|
240 |
"testString empty -> no match" |
|
241 |
(sSize == 0) ifTrue:[^ false]. |
|
242 |
||
243 |
included := false. |
|
244 |
"/ checkChar := aString at:sStart. |
|
245 |
included := matchEntry includes:checkChar. |
|
246 |
included ifFalse:[ |
|
247 |
ignoreCase ifTrue:[ |
|
248 |
checkChar isUppercase ifTrue:[ |
|
249 |
included := matchEntry includes:checkChar asLowercase. |
|
250 |
] ifFalse:[ |
|
251 |
included := matchEntry includes:checkChar asUppercase. |
|
252 |
] |
|
253 |
]. |
|
254 |
]. |
|
255 |
mStart := mStart + 1. |
|
256 |
mSize := mSize - 1. |
|
257 |
included ifFalse:[^ false]. |
|
258 |
||
259 |
((sSize == 1) and:[mSize == 0]) ifTrue:[^ true]. |
|
260 |
"cut off 1st char and continue" |
|
261 |
sStart := sStart + 1 |
|
262 |
] ifFalse:[ |
|
263 |
"/ must be single character |
|
264 |
||
265 |
"testString empty ?" |
|
266 |
(sSize == 0) ifTrue:[^ false]. |
|
267 |
||
268 |
"first characters equal ?" |
|
269 |
"/ checkChar := aString at:sStart. |
|
270 |
ignoreCase ifFalse:[^ false]. |
|
271 |
(checkChar asUppercase ~~ matchEntry asUppercase) ifTrue:[^ false]. |
|
272 |
||
273 |
"advance and continue" |
|
274 |
mStart := mStart + 1. |
|
275 |
sStart := sStart + 1 |
|
276 |
] |
|
277 |
] |
|
278 |
] |
|
279 |
] |
|
280 |
] |
|
608 | 281 |
! |
282 |
||
283 |
matchScanArrayFrom:aString |
|
284 |
"scan a pattern string and decompose it into a scanArray. |
|
285 |
This is processed faster (especially with character ranges), and |
|
286 |
can also be reused later. (if the same pattern is to be searched again)" |
|
287 |
||
288 |
|coll |
|
289 |
idx "{ Class: SmallInteger }" |
|
290 |
end c1 c2 matchSet previous| |
|
291 |
||
292 |
coll := OrderedCollection new. |
|
293 |
idx := 1. end := aString size. |
|
294 |
[idx <= end] whileTrue:[ |
|
295 |
|char this| |
|
296 |
||
297 |
char := aString at:idx. |
|
298 |
char == $* ifTrue:[ |
|
299 |
previous ~~ #anyString ifTrue:[ |
|
300 |
this := #anyString |
|
301 |
] |
|
302 |
] ifFalse:[ |
|
303 |
char == $# ifTrue:[ |
|
304 |
previous ~~ #anyString ifTrue:[ |
|
305 |
this := #any |
|
306 |
] |
|
307 |
] ifFalse:[ |
|
308 |
char == $[ ifTrue:[ |
|
309 |
matchSet := IdentitySet new. |
|
310 |
idx := idx + 1. |
|
311 |
idx > end ifTrue:[^ nil]. |
|
312 |
char := aString at:idx. |
|
313 |
c1 := nil. |
|
314 |
[char ~~ $]] whileTrue:[ |
|
315 |
((char == $-) and:[c1 notNil]) ifTrue:[ |
|
316 |
idx := idx + 1. |
|
317 |
idx > end ifTrue:[^ nil]. |
|
318 |
c2 := aString at:idx. |
|
319 |
(c1 to:c2) do:[:c | matchSet add:c]. |
|
320 |
c1 := nil. |
|
321 |
idx := idx + 1. |
|
322 |
] ifFalse:[ |
|
323 |
(char ~~ $]) ifTrue:[ |
|
324 |
matchSet add:char. |
|
325 |
c1 := char. |
|
326 |
idx := idx + 1 |
|
327 |
] |
|
328 |
]. |
|
329 |
idx > end ifTrue:[^ nil]. |
|
330 |
char := aString at:idx |
|
331 |
]. |
|
332 |
this := matchSet asString |
|
333 |
] ifFalse:[ |
|
334 |
this := char |
|
335 |
] |
|
336 |
] |
|
337 |
]. |
|
338 |
this notNil ifTrue:[coll add:this. previous := this]. |
|
339 |
idx := idx + 1 |
|
340 |
]. |
|
341 |
||
342 |
^ coll asArray |
|
343 |
||
344 |
" |
|
345 |
String matchScanArrayFrom:'*ute*' |
|
346 |
String matchScanArrayFrom:'**ute**' |
|
347 |
String matchScanArrayFrom:'*uter' |
|
348 |
String matchScanArrayFrom:'[cC]#mpute[rR]' |
|
349 |
String matchScanArrayFrom:'[abcd]*' |
|
350 |
String matchScanArrayFrom:'[a-k]*' |
|
351 |
String matchScanArrayFrom:'*some*compl*ern*' |
|
352 |
String matchScanArrayFrom:'[a-' |
|
353 |
String matchScanArrayFrom:'[a-zA-Z]' |
|
354 |
String matchScanArrayFrom:'[a-z01234A-Z]' |
|
355 |
" |
|
356 |
! ! |
|
357 |
||
358 |
!CharacterArray methodsFor:'Compatibility - ST/V'! |
|
359 |
||
360 |
byteAt:index put:aByte |
|
361 |
"store a byte at given index. |
|
362 |
This is an ST/V compatibility method." |
|
363 |
||
364 |
(aByte == 0) ifTrue:[ |
|
365 |
"store a space instead" |
|
366 |
^ super basicAt:index put:(Character space) |
|
367 |
]. |
|
368 |
^ super at:index put:(Character value:aByte) |
|
369 |
! |
|
370 |
||
371 |
replChar:oldChar with:newChar |
|
372 |
"return a copy of the receiver, with all oldChars replaced |
|
373 |
by newChar. |
|
374 |
This is an ST/V compatibility method." |
|
375 |
||
376 |
^ self copy replaceAll:oldChar by:newChar |
|
377 |
||
378 |
" |
|
379 |
'12345678901234567890' replChar:$0 with:$* |
|
380 |
" |
|
381 |
! |
|
382 |
||
383 |
replChar:oldChar withString:newString |
|
384 |
"return a copy of the receiver, with all oldChars replaced |
|
385 |
by newString (i.e. slice in the newString in place of the oldChar). |
|
386 |
This is an ST/V compatibility method." |
|
387 |
||
388 |
|tmpStream| |
|
389 |
||
390 |
tmpStream := WriteStream on:(self class new). |
|
391 |
self do:[:element | |
|
392 |
element = oldChar ifTrue:[ |
|
393 |
tmpStream nextPutAll:newString |
|
394 |
] ifFalse:[ |
|
395 |
tmpStream nextPut:element |
|
396 |
]. |
|
397 |
]. |
|
398 |
^ tmpStream contents |
|
399 |
||
400 |
" |
|
401 |
'12345678901234567890' replChar:$0 withString:'foo' |
|
402 |
'a string with spaces' replChar:$ withString:' foo ' |
|
403 |
" |
|
404 |
! |
|
405 |
||
406 |
trimBlanks |
|
407 |
"return a copy of the receiver without leading |
|
408 |
and trailing spaces. |
|
409 |
This is an ST/V compatibility method." |
|
410 |
||
411 |
^ self withoutSpaces |
|
412 |
||
413 |
" |
|
414 |
' spaces at beginning' trimBlanks |
|
415 |
'spaces at end ' trimBlanks |
|
416 |
' spaces at beginning and end ' trimBlanks |
|
417 |
'no spaces' trimBlanks |
|
418 |
" |
|
419 |
! ! |
|
420 |
||
421 |
!CharacterArray methodsFor:'Compatibility - VisualAge'! |
|
422 |
||
423 |
addLineDelimiter |
|
424 |
"replace all '\'-characters by line delimiter (cr) - characters. |
|
425 |
This has been added for VisualAge compatibility." |
|
426 |
||
427 |
^ self withCRs |
|
428 |
! |
|
429 |
||
430 |
bindWith:aString |
|
431 |
"return a copy of the receiver, where a '%1' escape is |
|
432 |
replaced by aString. |
|
433 |
This has been added for VisualAge compatibility." |
|
434 |
||
435 |
^ self expandPlaceholdersWith:(Array with:aString) |
|
436 |
||
437 |
" |
|
438 |
'do you like %1 ?' bindWith:'smalltalk' |
|
439 |
" |
|
440 |
! |
|
441 |
||
442 |
bindWith:string1 with:string2 |
|
443 |
"return a copy of the receiver, where a '%1' escape is |
|
444 |
replaced by string1 and '%2' is replaced by string2. |
|
445 |
This has been added for VisualAge compatibility." |
|
446 |
||
447 |
^ self expandPlaceholdersWith:(Array with:string1 with:string2) |
|
448 |
||
449 |
" |
|
450 |
'do you prefer %1 or rather %2 ?' |
|
451 |
bindWith:'smalltalk' with:'c++' |
|
452 |
" |
|
453 |
! |
|
454 |
||
455 |
bindWith:str1 with:str2 with:str3 |
|
456 |
"return a copy of the receiver, where a '%1', '%2' and '%3' escapes |
|
457 |
are replaced by str1, str2 and str3 respectively. |
|
458 |
This has been added for VisualAge compatibility." |
|
459 |
||
460 |
^ self expandPlaceholdersWith:(Array with:str1 with:str2 with:str3) |
|
461 |
||
462 |
" |
|
463 |
'do you prefer %1 or rather %2 (not talking about %3) ?' |
|
464 |
bindWith:'smalltalk' with:'c++' with:'c' |
|
465 |
" |
|
466 |
! |
|
467 |
||
468 |
bindWith:str1 with:str2 with:str3 with:str4 |
|
469 |
"return a copy of the receiver, where a '%1', '%2', '%3' and '%4' escapes |
|
470 |
are replaced by str1, str2, str3 and str4 respectively. |
|
471 |
This has been added for VisualAge compatibility." |
|
472 |
||
473 |
^ self expandPlaceholdersWith:(Array with:str1 with:str2 with:str3 with:str4) |
|
474 |
||
475 |
" |
|
476 |
'do you prefer %1 or rather %2 (not talking about %3 or even %4) ?' |
|
477 |
bindWith:'smalltalk' with:'c++' with:'c' with:'assembler' |
|
478 |
" |
|
479 |
! |
|
480 |
||
481 |
bindWithArguments:anArrayOfStrings |
|
482 |
"return a copy of the receiver, where a '%i' escape |
|
483 |
is replaced by the coresponding string from the argument array. |
|
484 |
'i' may be between 1 and 9 (i.e. a maximum of 9 placeholders is allowed). |
|
485 |
This has been added for VisualAge compatibility." |
|
486 |
||
487 |
^ self expandPlaceholdersWith:anArrayOfStrings |
|
488 |
||
489 |
" |
|
490 |
'do you prefer %1 or rather %2 (not talking about %3) ?' |
|
491 |
bindWithArguments:#('smalltalk' 'c++' 'c') |
|
492 |
" |
|
493 |
! |
|
494 |
||
495 |
subStrings |
|
496 |
"return an array consisting of all words contained in the receiver. |
|
497 |
Words are separated by whitespace. |
|
498 |
This has been added for VisualAge compatibility." |
|
499 |
||
500 |
^ self asCollectionOfWords |
|
501 |
||
502 |
" |
|
503 |
'hello world, this is smalltalk' subStrings |
|
504 |
" |
|
505 |
! |
|
506 |
||
507 |
subStrings:separatorCharacter |
|
508 |
"return an array consisting of all words contained in the receiver. |
|
509 |
Words are separated by separatorCharacter. |
|
510 |
This has been added for VisualAge compatibility." |
|
511 |
||
512 |
^ self asCollectionOfSubstringsSeparatedBy:separatorCharacter |
|
513 |
||
514 |
" |
|
515 |
'foo:bar:baz:smalltalk' subStrings:$: |
|
516 |
" |
|
517 |
! |
|
518 |
||
519 |
trimSeparators |
|
520 |
"return a copy of the receiver without leading and trailing whiteSpace" |
|
521 |
||
522 |
^ self withoutSeparators |
|
523 |
! ! |
|
524 |
||
525 |
!CharacterArray methodsFor:'character searching'! |
|
526 |
||
527 |
includesMatchCharacters |
|
528 |
"return true if the receiver includes any meta characters (i.e. $* or $#) |
|
529 |
for match operations; false if not" |
|
530 |
||
531 |
^ self includesAny:'*#[' |
|
532 |
! |
|
533 |
||
534 |
indexOfNonSeparatorStartingAt:startIndex |
|
535 |
"return the index of the next non-whitespace character" |
|
536 |
||
537 |
|start "{ Class: SmallInteger }" |
|
538 |
mySize "{ Class: SmallInteger }"| |
|
539 |
||
540 |
start := startIndex. |
|
541 |
mySize := self size. |
|
542 |
||
543 |
start to:mySize do:[:index | |
|
544 |
(self at:index) isSeparator ifFalse:[^ index] |
|
545 |
]. |
|
546 |
^ 0 |
|
547 |
||
548 |
" |
|
549 |
' hello world' indexOfNonSeparatorStartingAt:1 |
|
550 |
" |
|
551 |
" |
|
552 |
|s index1 index2| |
|
553 |
s := ' foo bar baz'. |
|
554 |
index1 := s indexOfNonSeparatorStartingAt:1. |
|
555 |
index2 := s indexOfSeparatorStartingAt:index1. |
|
556 |
s copyFrom:index1 to:index2 - 1 |
|
557 |
" |
|
558 |
! |
|
559 |
||
560 |
indexOfSeparator |
|
561 |
"return the index of the first whitespace character" |
|
562 |
||
563 |
^ self indexOfSeparatorStartingAt:1 |
|
564 |
||
565 |
"'hello world' indexOfSeparator" |
|
566 |
! |
|
567 |
||
568 |
indexOfSeparatorStartingAt:startIndex |
|
569 |
"return the index of the next whitespace character" |
|
570 |
||
571 |
|start "{ Class: SmallInteger }" |
|
572 |
mySize "{ Class: SmallInteger }"| |
|
573 |
||
574 |
start := startIndex. |
|
575 |
mySize := self size. |
|
576 |
||
577 |
start to:mySize do:[:index | |
|
578 |
(self at:index) isSeparator ifTrue:[^ index] |
|
579 |
]. |
|
580 |
^ 0 |
|
581 |
||
582 |
"'hello world' indexOfSeparatorStartingAt:3" |
|
583 |
! ! |
|
584 |
||
585 |
!CharacterArray methodsFor:'comparing'! |
|
586 |
||
587 |
< something |
|
588 |
"Compare the receiver with the argument and return true if the |
|
589 |
receiver is less than the argument. Otherwise return false." |
|
590 |
||
591 |
^ (something > self) |
|
592 |
! |
|
593 |
||
594 |
<= something |
|
595 |
"Compare the receiver with the argument and return true if the |
|
596 |
receiver is less than or equal to the argument. Otherwise return false." |
|
597 |
||
598 |
^ (self > something) not |
|
599 |
! |
|
600 |
||
601 |
= aString |
|
602 |
"Compare the receiver with the argument and return true if the |
|
603 |
receiver is equal to the argument. Otherwise return false. |
|
604 |
This compare does NOT ignore case differences, |
|
605 |
therefore 'foo' = 'Foo' will return false. |
|
606 |
Since this is incompatible to ST-80 (at least, V2.x) , this may change." |
|
607 |
||
608 |
|mySize "{ Class: SmallInteger }" |
|
609 |
otherSize | |
|
610 |
||
611 |
aString species == self species ifFalse:[^ false]. |
|
612 |
mySize := self size. |
|
613 |
otherSize := aString size. |
|
614 |
mySize == otherSize ifFalse:[^ false]. |
|
615 |
||
616 |
1 to:mySize do:[:index | |
|
617 |
(self at:index) = (aString at:index) ifFalse:[^ false]. |
|
618 |
]. |
|
619 |
^ true |
|
620 |
||
621 |
" |
|
622 |
'foo' = 'Foo' |
|
623 |
'foo' = 'bar' |
|
624 |
'foo' = 'foo' |
|
625 |
" |
|
626 |
! |
|
627 |
||
628 |
> aString |
|
629 |
"Compare the receiver with the argument and return true if the |
|
630 |
receiver is greater than the argument. Otherwise return false. |
|
631 |
In contrast to ST-80, case differences are NOT ignored, thus |
|
632 |
'foo' > 'Foo' will return true; use #sameAs: to compare ignoring cases.. |
|
633 |
Since this is incompatible to ST-80, this may change." |
|
634 |
||
635 |
|mySize "{ Class: SmallInteger }" |
|
636 |
otherSize "{ Class: SmallInteger }" |
|
637 |
n "{ Class: SmallInteger }" |
|
638 |
c1 c2| |
|
639 |
||
640 |
mySize := self size. |
|
641 |
otherSize := aString size. |
|
642 |
n := mySize min:otherSize. |
|
643 |
||
644 |
1 to:n do:[:index | |
|
645 |
c1 := self at:index. |
|
646 |
c2 := aString at:index. |
|
647 |
c1 > c2 ifTrue:[^ true]. |
|
648 |
c1 < c2 ifTrue:[^ false]. |
|
649 |
]. |
|
650 |
^ mySize > otherSize |
|
651 |
! |
|
652 |
||
653 |
>= something |
|
654 |
"Compare the receiver with the argument and return true if the |
|
655 |
receiver is greater than or equal to the argument. |
|
656 |
Otherwise return false." |
|
657 |
||
658 |
^ (something > self) not |
|
659 |
! |
|
660 |
||
661 |
compareWith:aString |
|
662 |
"Compare the receiver with the argument and return 1 if the receiver is |
|
663 |
greater, 0 if equal and -1 if less than the argument. |
|
664 |
Case differences are NOT ignored, thus |
|
665 |
'foo' compareWith: 'Foo' will return 1." |
|
666 |
||
667 |
|mySize "{ Class: SmallInteger }" |
|
668 |
otherSize "{ Class: SmallInteger }" |
|
669 |
n "{ Class: SmallInteger }" |
|
670 |
c1 c2| |
|
671 |
||
672 |
mySize := self size. |
|
673 |
otherSize := aString size. |
|
674 |
n := mySize min:otherSize. |
|
675 |
||
676 |
1 to:n do:[:index | |
|
677 |
c1 := self at:index. |
|
678 |
c2 := aString at:index. |
|
679 |
c1 > c2 ifTrue:[^ 1]. |
|
680 |
c1 < c2 ifTrue:[^ -1]. |
|
681 |
]. |
|
682 |
mySize > otherSize ifTrue:[^ 1]. |
|
683 |
mySize < otherSize ifTrue:[^ -1]. |
|
684 |
^ 0 |
|
685 |
! |
|
686 |
||
687 |
hash |
|
688 |
"return an integer useful as a hash-key" |
|
689 |
||
690 |
%{ /* NOCONTEXT */ |
|
691 |
||
692 |
REGISTER int g, val; |
|
693 |
REGISTER unsigned char *cp, *cp0; |
|
694 |
int l; |
|
695 |
||
696 |
cp = _stringVal(self); |
|
697 |
l = _stringSize(self); |
|
698 |
if (__qClass(self) != String) { |
|
699 |
int n = __OBJS2BYTES__(_intVal(_ClassInstPtr(__qClass(self))->c_ninstvars)); |
|
700 |
||
701 |
cp += n; |
|
702 |
l -= n; |
|
703 |
} |
|
704 |
||
705 |
/* |
|
706 |
* this is the dragon-book algorithm with a funny start |
|
707 |
* value (to give short strings a number above 8192) |
|
708 |
*/ |
|
709 |
val = 12345; |
|
710 |
for (cp0 = cp, cp += l - 1; cp >= cp0; cp--) { |
|
711 |
val = (val << 5) + (*cp & 0x1F); |
|
712 |
if (g = (val & 0x3E000000)) |
|
713 |
val ^= g >> 25 /* 23 */ /* 25 */; |
|
714 |
val &= 0x3FFFFFFF; |
|
715 |
} |
|
716 |
||
717 |
if (l) { |
|
718 |
l |= 1; |
|
719 |
val = (val * l) & 0x3FFFFFFF; |
|
720 |
} |
|
721 |
||
722 |
RETURN ( _MKSMALLINT(val) ); |
|
723 |
%} |
|
724 |
! |
|
725 |
||
726 |
sameAs:aString |
|
727 |
"Compare the receiver with the argument like =, but ignore |
|
728 |
case differences. Return true or false." |
|
729 |
||
730 |
|mySize "{ Class: SmallInteger }" |
|
731 |
otherSize c1 c2| |
|
732 |
||
733 |
mySize := self size. |
|
734 |
otherSize := aString size. |
|
735 |
mySize == otherSize ifFalse:[^ false]. |
|
736 |
||
737 |
1 to:mySize do:[:index | |
|
738 |
c1 := self at:index. |
|
739 |
c2 := aString at:index. |
|
740 |
c1 == c2 ifFalse:[ |
|
741 |
c1 asLowercase = c2 asLowercase ifFalse:[^ false]. |
|
742 |
] |
|
743 |
]. |
|
744 |
^ true |
|
745 |
||
746 |
" |
|
747 |
'foo' sameAs: 'Foo' |
|
748 |
'foo' sameAs: 'bar' |
|
749 |
'foo' sameAs: 'foo' |
|
750 |
" |
|
751 |
! |
|
752 |
||
753 |
sameCharacters:aString |
|
754 |
"count & return the number of characters which are the same |
|
755 |
(ignoring case) in the receiver and the argument, aString." |
|
756 |
||
757 |
|n "{ Class: SmallInteger }" |
|
758 |
otherSize c1 c2 cnt| |
|
759 |
||
760 |
n := self size. |
|
761 |
n := n min:(aString size). |
|
762 |
||
763 |
cnt := 0. |
|
764 |
1 to:n do:[:index | |
|
765 |
c1 := self at:index. |
|
766 |
c2 := aString at:index. |
|
767 |
((c1 == c2) |
|
768 |
or:[c1 asLowercase = c2 asLowercase]) ifTrue:[ |
|
769 |
cnt := cnt + 1 |
|
770 |
] |
|
771 |
]. |
|
772 |
^ cnt |
|
773 |
||
774 |
" |
|
775 |
'foobarbaz' sameCharacters: 'foo' |
|
776 |
'foobarbaz' sameCharacters: 'Foo' |
|
777 |
'foobarbaz' sameCharacters: 'baz' |
|
778 |
" |
|
779 |
! ! |
|
780 |
||
781 |
!CharacterArray methodsFor:'converting'! |
|
782 |
||
783 |
asArrayOfSubstrings |
|
784 |
"return an array of substrings from the receiver, interpreting |
|
785 |
separators (i.e. spaces & newlines) as word-delimiters. |
|
786 |
This is a compatibility method - the actual work is done in |
|
787 |
asCollectionOfWords." |
|
788 |
||
789 |
^ self asCollectionOfWords asArray |
|
790 |
||
791 |
" |
|
792 |
'1 one two three four 5 five' asArrayOfSubstrings |
|
793 |
" |
|
794 |
! |
|
795 |
||
796 |
asCollectionOfLines |
|
797 |
"return a collection containing the lines (separated by cr) |
|
798 |
of the receiver. If multiple cr's occur in a row, the result will |
|
799 |
contain empty strings." |
|
800 |
||
801 |
^ self asCollectionOfSubstringsSeparatedBy:Character cr |
|
802 |
||
803 |
" |
|
804 |
'1 one\2 two\3 three\4 four\5 five' withCRs asCollectionOfLines |
|
805 |
'1 one\\\\2 two\3 three' withCRs asCollectionOfLines |
|
806 |
" |
|
807 |
! |
|
808 |
||
809 |
asCollectionOfSubstringsSeparatedBy:aCharacter |
|
810 |
"return a collection containing the lines (separated by aCharacter) |
|
811 |
of the receiver. If aCharacter occurs multiple times in a row, |
|
812 |
the result will contain empty strings." |
|
813 |
||
814 |
|lines myClass |
|
815 |
numberOfLines "{ Class:SmallInteger }" |
|
816 |
startIndex "{ Class:SmallInteger }" |
|
817 |
stopIndex "{ Class:SmallInteger }" | |
|
818 |
||
819 |
" |
|
820 |
count first, to avoid regrowing of the OC |
|
821 |
" |
|
822 |
numberOfLines := (self occurrencesOf:aCharacter) + 1. |
|
823 |
lines := OrderedCollection new:numberOfLines. |
|
824 |
myClass := self species. |
|
825 |
||
826 |
startIndex := 1. |
|
827 |
1 to:numberOfLines do:[:lineNr | |
|
828 |
stopIndex := self indexOf:aCharacter startingAt:startIndex. |
|
829 |
stopIndex == 0 ifTrue:[ |
|
830 |
stopIndex := self size |
|
831 |
] ifFalse: [ |
|
832 |
stopIndex := stopIndex - 1. |
|
833 |
]. |
|
834 |
||
835 |
(stopIndex < startIndex) ifTrue: [ |
|
836 |
lines add:(myClass new:0) |
|
837 |
] ifFalse: [ |
|
838 |
lines add:(self copyFrom:startIndex to:stopIndex) |
|
839 |
]. |
|
840 |
startIndex := stopIndex + 2 |
|
841 |
]. |
|
842 |
^ lines |
|
843 |
||
844 |
" |
|
845 |
'1 one:2 two:3 three:4 four:5 five' withCRs asCollectionOfSubstringsSeparatedBy:$: |
|
846 |
'1 one 2 two 3 three 4 four 5 five' withCRs asCollectionOfSubstringsSeparatedBy:Character space |
|
847 |
" |
|
848 |
! |
|
849 |
||
850 |
asCollectionOfSubstringsSeparatedByAny:aCollectionOfSeparators |
|
851 |
"return a collection containing the words (separated by any character |
|
852 |
from aCollectionOfSeparators) of the receiver. |
|
853 |
This allows breaking up strings using any character as separator." |
|
854 |
||
855 |
|words |
|
856 |
start "{ Class:SmallInteger }" |
|
857 |
stop "{ Class:SmallInteger }" |
|
858 |
mySize "{ Class:SmallInteger }"| |
|
859 |
||
860 |
words := OrderedCollection new. |
|
861 |
start := 1. |
|
862 |
mySize := self size. |
|
863 |
[start <= mySize] whileTrue:[ |
|
864 |
"skip multiple separators" |
|
865 |
[aCollectionOfSeparators includes:(self at:start)] whileTrue:[ |
|
866 |
start := start + 1 . |
|
867 |
start > mySize ifTrue:[ |
|
868 |
^ words |
|
869 |
]. |
|
870 |
]. |
|
871 |
||
872 |
stop := self indexOfAny:aCollectionOfSeparators startingAt:start. |
|
873 |
stop == 0 ifTrue:[ |
|
874 |
words add:(self copyFrom:start to:mySize). |
|
875 |
^ words |
|
876 |
]. |
|
877 |
words add:(self copyFrom:start to:(stop - 1)). |
|
878 |
start := stop |
|
879 |
]. |
|
880 |
^ words |
|
881 |
||
882 |
" |
|
883 |
'hello:world:isnt:this nice' asCollectionOfSubstringsSeparatedByAny:#($:) |
|
884 |
'hello:world:isnt:this nice' asCollectionOfSubstringsSeparatedByAny:':' |
|
885 |
'hello:world:isnt:this nice' asCollectionOfSubstringsSeparatedByAny:(Array with:$: with:Character space) |
|
886 |
'hello:world:isnt:this nice' asCollectionOfSubstringsSeparatedByAny:': ' |
|
887 |
'h1e2l3l4o' asCollectionOfSubstringsSeparatedByAny:($1 to: $9) |
|
888 |
" |
|
889 |
! |
|
890 |
||
891 |
asCollectionOfWords |
|
892 |
"return a collection containing the words (separated by whitespace) |
|
893 |
of the receiver. Multiple occurences of whitespace characters will |
|
894 |
be treated like one - i.e. whitespace is skipped." |
|
895 |
||
896 |
|words |
|
897 |
start "{ Class:SmallInteger }" |
|
898 |
stop "{ Class:SmallInteger }" |
|
899 |
mySize "{ Class:SmallInteger }"| |
|
900 |
||
901 |
words := OrderedCollection new. |
|
902 |
start := 1. |
|
903 |
mySize := self size. |
|
904 |
[start <= mySize] whileTrue:[ |
|
905 |
start := self indexOfNonSeparatorStartingAt:start. |
|
906 |
start == 0 ifTrue:[ |
|
907 |
^ words |
|
908 |
]. |
|
909 |
stop := self indexOfSeparatorStartingAt:start. |
|
910 |
stop == 0 ifTrue:[ |
|
911 |
words add:(self copyFrom:start to:mySize). |
|
912 |
^ words |
|
913 |
]. |
|
914 |
words add:(self copyFrom:start to:(stop - 1)). |
|
915 |
start := stop |
|
916 |
]. |
|
917 |
^ words |
|
918 |
||
919 |
" |
|
920 |
'hello world isnt this nice' asCollectionOfWords |
|
921 |
' hello world isnt this nice ' asCollectionOfWords |
|
922 |
'hello' asCollectionOfWords |
|
923 |
'' asCollectionOfWords |
|
924 |
' ' asCollectionOfWords |
|
925 |
" |
|
926 |
! |
|
927 |
||
928 |
asComposedText |
|
929 |
^ ComposedText fromString:self |
|
930 |
! |
|
931 |
||
932 |
asFilename |
|
933 |
"return a Filename with pathname taken from the receiver" |
|
934 |
||
935 |
^ Filename named:self |
|
936 |
! |
|
937 |
||
938 |
asFloat |
|
939 |
"read a float number from the receiver. |
|
940 |
Notice, that errors may occur during the read, so you better |
|
941 |
setup some signal handler when using this method." |
|
942 |
||
943 |
^ (Number readFromString:self) asFloat |
|
944 |
||
945 |
" |
|
946 |
'0.123' asFloat |
|
947 |
'12345' asFloat |
|
948 |
'(1/5)' asFloat |
|
949 |
Object errorSignal handle:[:ex | ex returnWith:0] do:['foo' asFloat] |
|
950 |
" |
|
951 |
! |
|
952 |
||
953 |
asInteger |
|
954 |
"read an integer from the receiver. |
|
955 |
Notice, that errors may occur during the read, so you better |
|
956 |
setup some signal handler when using this method." |
|
957 |
||
958 |
^ Integer readFromString:self |
|
959 |
||
960 |
" |
|
961 |
'12345678901234567890' asInteger |
|
962 |
'-1234' asInteger |
|
963 |
'0.123' asInteger <- reader stops at ., returning 0 here |
|
964 |
'0.123' asNumber <- returns what you expect |
|
965 |
Object errorSignal handle:[:ex | ex returnWith:0] do:['foo' asInteger] |
|
966 |
" |
|
967 |
! |
|
968 |
||
969 |
asLowercase |
|
970 |
"return a copy of myself in lowercase letters" |
|
971 |
||
972 |
|newStr |
|
973 |
mySize "{ Class: SmallInteger }" | |
|
974 |
||
975 |
mySize := self size. |
|
976 |
newStr := self species new:mySize. |
|
977 |
1 to:mySize do:[:i | |
|
978 |
newStr at:i put:(self at:i) asLowercase |
|
979 |
]. |
|
980 |
^ newStr |
|
981 |
||
982 |
" |
|
983 |
'HelloWorld' asLowercase |
|
984 |
'HelloWorld' asLowercaseFirst |
|
985 |
" |
|
986 |
! |
|
987 |
||
988 |
asLowercaseFirst |
|
989 |
"return a copy of myself where the first character is |
|
990 |
converted to lowercase." |
|
991 |
||
992 |
|newString sz| |
|
993 |
||
994 |
sz := self size. |
|
995 |
newString := self copyFrom:1 to:sz. |
|
996 |
sz > 0 ifTrue:[ |
|
997 |
newString at:1 put:(newString at:1) asLowercase |
|
998 |
]. |
|
999 |
^ newString |
|
1000 |
||
1001 |
" |
|
1002 |
'HelloWorld' asLowercase |
|
1003 |
'HelloWorld' asLowercaseFirst |
|
1004 |
" |
|
1005 |
! |
|
1006 |
||
1007 |
asNumber |
|
1008 |
"read a number from the receiver. |
|
1009 |
Notice, that (in contrast to ST-80) errors may occur during the read, |
|
1010 |
so you better setup some signal handler when using this method. |
|
1011 |
This may change if ANSI specifies it." |
|
1012 |
||
1013 |
"/ ST-80 behavior: |
|
1014 |
"/ ^ Number readFromString:self onError:0 |
|
1015 |
||
1016 |
^ Number readFromString:self |
|
1017 |
||
1018 |
" |
|
1019 |
'123' asNumber |
|
1020 |
'123.567' asNumber |
|
1021 |
'(5/6)' asNumber |
|
1022 |
'foo' asNumber |
|
1023 |
Object errorSignal handle:[:ex | ex returnWith:0] do:['foo' asNumber] |
|
1024 |
" |
|
1025 |
! |
|
1026 |
||
1027 |
asNumberFromFormatString:ignored |
|
1028 |
"read a number from the receiver, ignoring any nonDigit characters. |
|
1029 |
This is typically used to convert from strings which include |
|
1030 |
dollar-signs or millenium digits. However, this method also ignores |
|
1031 |
the decimal point (if any) and therefore should be used with care." |
|
1032 |
||
1033 |
|tempString| |
|
1034 |
||
1035 |
tempString := self collect:[:char | char isDigit]. |
|
1036 |
^ Number readFromString:tempString onError:0 |
|
1037 |
||
1038 |
" |
|
1039 |
'USD 123' asNumberFromFormatString:'foo' |
|
1040 |
'DM 123' asNumberFromFormatString:'foo' |
|
1041 |
'123' asNumberFromFormatString:'foo' |
|
1042 |
'123.567' asNumberFromFormatString:'foo' |
|
1043 |
'(5/6)' asNumberFromFormatString:'foo' |
|
1044 |
'foo' asNumberFromFormatString:'foo' |
|
1045 |
" |
|
1046 |
! |
|
1047 |
||
1048 |
asSingleByteString |
|
1049 |
"return the receiver converted to a 'normal' string" |
|
1050 |
||
1051 |
^ String fromString:self |
|
1052 |
! |
|
1053 |
||
1054 |
asString |
|
1055 |
"return myself - I am a string" |
|
1056 |
||
1057 |
^ self |
|
1058 |
! |
|
1059 |
||
1060 |
asStringCollection |
|
1061 |
"return a collection of lines from myself." |
|
1062 |
||
1063 |
^ StringCollection from:self |
|
1064 |
! |
|
1065 |
||
1066 |
asTwoByteString |
|
1067 |
"return the receiver converted to a two-byte string" |
|
1068 |
||
1069 |
^ TwoByteString fromString:self |
|
1070 |
! |
|
1071 |
||
1072 |
asUppercase |
|
1073 |
"return a copy of myself in uppercase letters" |
|
1074 |
||
1075 |
|newStr |
|
1076 |
mySize "{ Class: SmallInteger }" | |
|
1077 |
||
1078 |
mySize := self size. |
|
1079 |
newStr := self species new:mySize. |
|
1080 |
1 to:mySize do:[:i | |
|
1081 |
newStr at:i put:(self at:i) asUppercase |
|
1082 |
]. |
|
1083 |
^ newStr |
|
1084 |
||
1085 |
" |
|
1086 |
'helloWorld' asUppercase |
|
1087 |
'helloWorld' asUppercaseFirst |
|
1088 |
" |
|
1089 |
! |
|
1090 |
||
1091 |
asUppercaseFirst |
|
1092 |
"return a copy of myself where the first character is |
|
1093 |
converted to uppercase." |
|
1094 |
||
1095 |
|newString sz| |
|
1096 |
||
1097 |
sz := self size. |
|
1098 |
newString := self copyFrom:1 to:sz. |
|
1099 |
sz > 0 ifTrue:[ |
|
1100 |
newString at:1 put:(newString at:1) asUppercase |
|
1101 |
]. |
|
1102 |
^ newString |
|
1103 |
||
1104 |
" |
|
1105 |
'helloWorld' asUppercase |
|
1106 |
'helloWorld' asUppercaseFirst |
|
1107 |
'HelloWorld' asUppercaseFirst |
|
1108 |
" |
|
1109 |
! |
|
1110 |
||
1111 |
replaceFrom:aString decode:encoding |
|
1112 |
"this is an experimental interface - unfinished" |
|
1113 |
||
1114 |
|table| |
|
1115 |
||
1116 |
table := self class decoderTableFor:encoding. |
|
1117 |
table isNil ifTrue:[ |
|
1118 |
^ self replaceFrom:aString |
|
1119 |
]. |
|
1120 |
||
1121 |
1 to:self size do:[:index | |
|
1122 |
|char oldCode newCode| |
|
1123 |
||
1124 |
char := aString at:index. |
|
1125 |
oldCode := char asciiValue. |
|
1126 |
newCode := table at:(oldCode + 1). |
|
1127 |
newCode ~~ oldCode ifTrue:[ |
|
1128 |
self at:index put:(Character value:newCode) |
|
1129 |
] |
|
1130 |
]. |
|
1131 |
||
1132 |
"Created: 20.10.1995 / 23:00:09 / cg" |
|
1133 |
"Modified: 20.10.1995 / 23:08:16 / cg" |
|
1134 |
! |
|
1135 |
||
1136 |
string |
|
1137 |
^ self |
|
1138 |
! |
|
1139 |
||
1140 |
tokensBasedOn:aCharacter |
|
1141 |
"this is an ST-80 alias for the ST/X method |
|
1142 |
asCollectionOfSubstringsSeparatedBy:" |
|
1143 |
||
1144 |
^ self asCollectionOfSubstringsSeparatedBy:aCharacter |
|
1145 |
||
1146 |
" |
|
1147 |
'hello:world:isnt:this nice' tokensBasedOn:$: |
|
1148 |
'foo,bar,baz' tokensBasedOn:$, |
|
1149 |
'/etc/passwd' asFilename readStream nextLine tokensBasedOn:$: |
|
1150 |
" |
|
1151 |
! ! |
|
1152 |
||
1153 |
!CharacterArray methodsFor:'copying'! |
|
1154 |
||
1155 |
concatenate:string1 and:string2 |
|
1156 |
"return the concatenation of myself and the arguments, string1 and string2. |
|
1157 |
This is equivalent to self , string1 , string2 |
|
1158 |
- generated by compiler when such a construct is detected and the receiver |
|
1159 |
is known to be a string." |
|
1160 |
||
1161 |
^ self , string1 , string2 |
|
1162 |
! |
|
1163 |
||
1164 |
concatenate:string1 and:string2 and:string3 |
|
1165 |
"return the concatenation of myself and the string arguments. |
|
1166 |
This is equivalent to self , string1 , string2 , string3 |
|
1167 |
- generated by compiler when such a construct is detected and the receiver |
|
1168 |
is known to be a string." |
|
1169 |
||
1170 |
^ self , string1 , string2 , string3 |
|
1171 |
! ! |
|
1172 |
||
1173 |
!CharacterArray methodsFor:'displaying'! |
|
1174 |
||
1175 |
displayOn:aGc x:x y:y |
|
1176 |
"display the receiver in a graphicsContext - this method allows |
|
1177 |
strings to be used like DisplayObjects." |
|
1178 |
||
1179 |
^ aGc displayString:self x:x y:y. |
|
1180 |
! ! |
|
1181 |
||
1182 |
!CharacterArray methodsFor:'padded copying'! |
|
1183 |
||
815
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
1184 |
centerPaddedTo:newSize |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
1185 |
"return a new string consisting of the receivers characters, |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
1186 |
plus spaces up to length and center the receivers characters in |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
1187 |
the resulting string. |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
1188 |
If the receivers size is equal or greater than the length argument, |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
1189 |
the original receiver is returned unchanged." |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
1190 |
|
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
1191 |
^ self centerPaddedTo:newSize with:(Character space) |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
1192 |
|
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
1193 |
" |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
1194 |
'foo' centerPaddedTo:10 |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
1195 |
123 printString centerPaddedTo:10 |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
1196 |
" |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
1197 |
|
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
1198 |
"Created: 25.11.1995 / 10:53:57 / cg" |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
1199 |
! |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
1200 |
|
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
1201 |
centerPaddedTo:size with:padCharacter |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
1202 |
"return a new string of length size, which contains the receiver |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
1203 |
centered (i.e. padded on both sides). |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
1204 |
Characters are filled with padCharacter. |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
1205 |
If the receivers size is equal or greater than the length argument, |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
1206 |
the original receiver is returned unchanged." |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
1207 |
|
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
1208 |
|len s| |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
1209 |
|
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
1210 |
len := self size. |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
1211 |
(len < size) ifTrue:[ |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
1212 |
s := self species new:size withAll:padCharacter. |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
1213 |
s replaceFrom:(size - len) // 2 + 1 with:self. |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
1214 |
^ s |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
1215 |
] |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
1216 |
|
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
1217 |
" |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
1218 |
'foo' centerPaddedTo:11 with:$. |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
1219 |
'fooBar' centerPaddedTo:5 with:$. |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
1220 |
123 printString centerPaddedTo:10 with:$. |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
1221 |
(' ' , 123 printString) centerPaddedTo:10 with:$. |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
1222 |
(Float pi printString) centerPaddedTo:15 with:(Character space) |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
1223 |
(Float pi printString) centerPaddedTo:15 with:$- |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
1224 |
(' ' , Float pi class name) centerPaddedTo:15 with:$. |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
1225 |
" |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
1226 |
! |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
1227 |
|
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
1228 |
decimalPaddedTo:size and:afterPeriod at:decimalCharacter |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
1229 |
"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
|
1230 |
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
|
1231 |
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
|
1232 |
to be padded). |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
1233 |
If the receivers size is equal or greater than the length argument, |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
1234 |
the original receiver is returned unchanged. |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
1235 |
(sounds complicated ? -> see examples below)." |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
1236 |
|
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
1237 |
^ self |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
1238 |
decimalPaddedTo:size |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
1239 |
and:afterPeriod |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
1240 |
at:decimalCharacter |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
1241 |
withLeft:(Character space) |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
1242 |
right:$0 |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
1243 |
|
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
1244 |
" |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
1245 |
'123' decimalPaddedTo:10 and:3 at:$. -> ' 123 ' |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
1246 |
'123' decimalPaddedTo:10 and:3 at:$. -> ' 123.000' |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
1247 |
'123.' decimalPaddedTo:10 and:3 at:$. -> ' 123.000' |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
1248 |
'123.1' decimalPaddedTo:10 and:3 at:$. -> ' 123.100' |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
1249 |
'123.1' decimalPaddedTo:10 and:3 at:$. -> ' 123.1 ' |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
1250 |
'123.123' decimalPaddedTo:10 and:3 at:$. -> ' 123.123' |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
1251 |
" |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
1252 |
|
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
1253 |
"Created: 23.12.1995 / 13:11:52 / cg" |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
1254 |
! |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
1255 |
|
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
1256 |
decimalPaddedTo:size and:afterPeriod at:decimalCharacter withLeft:leftPadChar right:rightPadChar |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
1257 |
"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
|
1258 |
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
|
1259 |
of the period. |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
1260 |
Characters on the left are filled with leftPadChar. |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
1261 |
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
|
1262 |
otherwise, if missing, a decimal point is added and right characters filled with this. |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
1263 |
If the receivers size is equal or greater than the length argument, |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
1264 |
the original receiver is returned unchanged. |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
1265 |
(sounds complicated ? -> see examples below)." |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
1266 |
|
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
1267 |
|len s idx n| |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
1268 |
|
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
1269 |
idx := self indexOf:decimalCharacter. |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
1270 |
idx == 0 ifTrue:[ |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
1271 |
"/ |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
1272 |
"/ no decimal point found; adjust string to the left of the period column |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
1273 |
"/ |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
1274 |
rightPadChar isNil ifTrue:[ |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
1275 |
s := self , (self species new:afterPeriod + 1 withAll:leftPadChar) |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
1276 |
] ifFalse:[ |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
1277 |
s:= self , decimalCharacter asString , (self species new:afterPeriod withAll:rightPadChar). |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
1278 |
]. |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
1279 |
] ifFalse:[ |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
1280 |
|
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
1281 |
"/ the number of after-decimalPoint characters |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
1282 |
n := self size - idx. |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
1283 |
rightPadChar isNil ifTrue:[ |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
1284 |
s := self , (self species new:afterPeriod - n withAll:leftPadChar). |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
1285 |
] ifFalse:[ |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
1286 |
s := self , (self species new:afterPeriod - n withAll:rightPadChar). |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
1287 |
]. |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
1288 |
]. |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
1289 |
|
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
1290 |
^ s leftPaddedTo:size with:leftPadChar |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
1291 |
|
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
1292 |
" |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
1293 |
'123' decimalPaddedTo:10 and:3 at:$. withLeft:(Character space) right:nil -> ' 123 ' |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
1294 |
'123' decimalPaddedTo:10 and:3 at:$. withLeft:(Character space) right:$0 -> ' 123.000' |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
1295 |
'123.' decimalPaddedTo:10 and:3 at:$. withLeft:(Character space) right:$0 -> ' 123.000' |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
1296 |
'123.1' decimalPaddedTo:10 and:3 at:$. withLeft:(Character space) right:$0 -> ' 123.100' |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
1297 |
'123.1' decimalPaddedTo:10 and:3 at:$. withLeft:(Character space) right:nil -> ' 123.1 ' |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
1298 |
'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
|
1299 |
" |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
1300 |
|
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
1301 |
"Modified: 23.12.1995 / 13:08:18 / cg" |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
1302 |
! |
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
1303 |
|
608 | 1304 |
leftPaddedTo:size |
1305 |
"return a new string of length size, which contains the receiver |
|
1306 |
right-adjusted (i.e. padded on the left). |
|
1307 |
Characters on the left are filled with spaces. |
|
1308 |
If the receivers size is equal or greater than the length argument, |
|
1309 |
the original receiver is returned unchanged." |
|
1310 |
||
1311 |
^ self leftPaddedTo:size with:(Character space) |
|
1312 |
||
1313 |
" |
|
1314 |
'foo' leftPaddedTo:10 |
|
1315 |
'fooBar' leftPaddedTo:5 |
|
1316 |
123 printString leftPaddedTo:10 |
|
1317 |
" |
|
1318 |
! |
|
1319 |
||
1320 |
leftPaddedTo:size with:padCharacter |
|
1321 |
"return a new string of length size, which contains the receiver |
|
1322 |
right-adjusted (i.e. padded on the left). |
|
1323 |
Characters on the left are filled with padCharacter. |
|
1324 |
If the receivers size is equal or greater than the length argument, |
|
1325 |
the original receiver is returned unchanged." |
|
1326 |
||
1327 |
|len s| |
|
1328 |
||
1329 |
len := self size. |
|
1330 |
(len < size) ifTrue:[ |
|
1331 |
s := self species new:size withAll:padCharacter. |
|
1332 |
s replaceFrom:(size - len + 1) with:self. |
|
1333 |
^ s |
|
1334 |
] |
|
1335 |
||
1336 |
" |
|
1337 |
'foo' leftPaddedTo:10 with:$. |
|
1338 |
'fooBar' leftPaddedTo:5 with:$. |
|
1339 |
123 printString leftPaddedTo:10 with:$. |
|
1340 |
(' ' , 123 printString) leftPaddedTo:10 with:$. |
|
1341 |
(Float pi printString) leftPaddedTo:15 with:(Character space) |
|
1342 |
(Float pi printString) leftPaddedTo:15 with:$- |
|
1343 |
(' ' , Float pi class name) leftPaddedTo:15 with:$. |
|
1344 |
" |
|
1345 |
! |
|
1346 |
||
1347 |
paddedTo:newSize |
|
1348 |
"return a new string consisting of the receivers characters, |
|
1349 |
plus spaces up to length. |
|
1350 |
If the receivers size is equal or greater than the length argument, |
|
1351 |
the original receiver is returned unchanged." |
|
1352 |
||
1353 |
^ self paddedTo:newSize with:(Character space) |
|
1354 |
||
1355 |
" |
|
1356 |
'foo' paddedTo:10 |
|
1357 |
123 printString paddedTo:10 |
|
1358 |
" |
|
1359 |
! |
|
1360 |
||
1361 |
paddedTo:newSize with:padCharacter |
|
1362 |
"return a new string consisting of the receivers characters, |
|
1363 |
plus pad characters up to length. |
|
1364 |
If the receivers size is equal or greater than the length argument, |
|
1365 |
the original receiver is returned unchanged." |
|
1366 |
||
1367 |
|s len| |
|
1368 |
||
1369 |
len := self size. |
|
1370 |
len < newSize ifTrue:[ |
|
1371 |
s := self species new:newSize withAll:padCharacter. |
|
1372 |
s replaceFrom:1 to:len with:self. |
|
1373 |
^ s |
|
1374 |
] |
|
1375 |
||
1376 |
" |
|
1377 |
'foo' paddedTo:10 with:$. |
|
1378 |
123 printString paddedTo:10 with:$* |
|
1379 |
(Float pi printString) paddedTo:15 with:(Character space) |
|
1380 |
(Float pi printString) paddedTo:15 with:$- |
|
1381 |
(Float pi class name , ' ') paddedTo:15 with:$. |
|
1382 |
" |
|
64 | 1383 |
! ! |
1384 |
||
328 | 1385 |
!CharacterArray methodsFor:'pattern matching'! |
1386 |
||
1387 |
findMatchString:matchString |
|
368 | 1388 |
"like findString/indexOfSubCollection, but allowing match patterns. |
328 | 1389 |
find matchstring; if found, return the index; |
1390 |
if not found, return 0." |
|
1391 |
||
1392 |
^ self findMatchString:matchString startingAt:1 ignoreCase:false ifAbsent:[0] |
|
1393 |
! |
|
1394 |
||
1395 |
findMatchString:matchString startingAt:index |
|
1396 |
"like findString, but allowing match patterns. |
|
1397 |
find matchstring, starting at index. if found, return the index; |
|
1398 |
if not found, return 0." |
|
1399 |
||
1400 |
^ self findMatchString:matchString startingAt:index ignoreCase:false ifAbsent:[0] |
|
1401 |
! |
|
1402 |
||
1403 |
findMatchString:matchString startingAt:index ignoreCase:ignoreCase ifAbsent:exceptionBlock |
|
1404 |
"like findString, but allowing match patterns. |
|
1405 |
find matchstring, starting at index. if found, return the index; |
|
1406 |
if not found, return the result of evaluating exceptionBlock. |
|
1407 |
This is a q&d hack - not very efficient" |
|
1408 |
||
1409 |
|firstChar |
|
1410 |
startIndex "{ Class: SmallInteger }" |
|
1411 |
matchSize "{ Class: SmallInteger }" |
|
1412 |
mySize "{ Class: SmallInteger }" |
|
1413 |
realMatchString| |
|
1414 |
||
1415 |
matchSize := matchString size. |
|
1416 |
matchSize == 0 ifTrue:[^ index]. "empty string matches" |
|
1417 |
||
1418 |
realMatchString := matchString. |
|
1419 |
(realMatchString endsWith:$*) ifFalse:[ |
|
1420 |
realMatchString := realMatchString , '*'. |
|
1421 |
matchSize := matchSize + 1 |
|
1422 |
]. |
|
1423 |
||
1424 |
mySize := self size. |
|
1425 |
firstChar := realMatchString at:1. |
|
1426 |
||
1427 |
firstChar asString includesMatchCharacters ifTrue:[ |
|
1428 |
index to:mySize do:[:col | |
|
1429 |
(realMatchString match:self from:col to:mySize ignoreCase:ignoreCase) |
|
1430 |
ifTrue:[^ col] |
|
1431 |
]. |
|
1432 |
^ exceptionBlock value. |
|
1433 |
]. |
|
1434 |
startIndex := self indexOf:firstChar startingAt:index. |
|
1435 |
[startIndex == 0] whileFalse:[ |
|
1436 |
(realMatchString match:self from:startIndex to:mySize ignoreCase:ignoreCase) |
|
1437 |
ifTrue:[^ startIndex]. |
|
1438 |
startIndex := self indexOf:firstChar startingAt:(startIndex + 1) |
|
1439 |
]. |
|
1440 |
^ exceptionBlock value |
|
1441 |
||
1442 |
" |
|
1443 |
'one two three four' findMatchString:'o[nu]' |
|
1444 |
'one two three four' findMatchString:'o[nu]' startingAt:3 |
|
1445 |
" |
|
1446 |
! |
|
1447 |
||
608 | 1448 |
includesMatchString:matchString |
1449 |
"like includesString, but allowing match patterns. |
|
1450 |
find matchstring; if found, return true, otherwise return false" |
|
1451 |
||
1452 |
^ (self findMatchString:matchString) ~~ 0 |
|
1453 |
||
1454 |
" |
|
1455 |
'hello world' includesMatchString:'h*' |
|
1456 |
'hello world' includesMatchString:'h[aeiou]llo' |
|
1457 |
'hello world' includesMatchString:'wor*' |
|
1458 |
'hello world' includesMatchString:'woR*' |
|
1459 |
" |
|
1460 |
! |
|
1461 |
||
328 | 1462 |
match:aString |
1463 |
"return true if aString matches self, where self may contain meta-match |
|
1464 |
characters $* (to match any string) or $# (to match any character). |
|
1465 |
or [...] to match a set of characters. |
|
345 | 1466 |
Lower/uppercase are considered different. |
1467 |
NOTICE: match-meta character interpretation is like in unix-matching, |
|
1468 |
NOT the ST-80 meaning." |
|
328 | 1469 |
|
1470 |
^ self match:aString from:1 to:aString size ignoreCase:false |
|
1471 |
||
1472 |
" |
|
1473 |
'*ute*' match:'computer' |
|
1474 |
'*uter' match:'computer' |
|
1475 |
'uter*' match:'computer' |
|
1476 |
'*ute*' match:'' |
|
1477 |
'[abcd]*' match:'computer' |
|
1478 |
'[abcd]*' match:'komputer' |
|
1479 |
'*some*compl*ern*' match:'this is some more complicated pattern match' |
|
1480 |
'*some*compl*ern*' match:'this is another complicated pattern match' |
|
1481 |
" |
|
1482 |
! |
|
1483 |
||
1484 |
match:aString from:start to:stop ignoreCase:ignoreCase |
|
1485 |
"return true if part of aString matches myself, |
|
1486 |
where self may contain meta-match |
|
1487 |
characters $* (to match any string) or $# (to match any character) |
|
1488 |
or [...] to match a set of characters. |
|
345 | 1489 |
If ignoreCase is true, lower/uppercase are considered the same. |
1490 |
NOTICE: match-meta character interpretation is like in unix-matching, |
|
1491 |
NOT the ST-80 meaning." |
|
328 | 1492 |
|
1493 |
|matchScanArray| |
|
1494 |
||
1495 |
" |
|
1496 |
keep the matchScanArray from the most recent match - |
|
1497 |
avoids parsing the pattern over-and over if multiple searches |
|
1498 |
are done with the same pattern. |
|
1499 |
" |
|
1500 |
(PreviousMatch notNil |
|
1501 |
and:[PreviousMatch key = self]) ifTrue:[ |
|
1502 |
matchScanArray := PreviousMatch value |
|
1503 |
] ifFalse:[ |
|
1504 |
matchScanArray := self class matchScanArrayFrom:self. |
|
1505 |
matchScanArray isNil ifTrue:[ |
|
356 | 1506 |
'CHARARRAY: invalid matchpattern:' infoPrint. self infoPrintNL. |
328 | 1507 |
^ false |
1508 |
]. |
|
1509 |
PreviousMatch := self -> matchScanArray. |
|
1510 |
]. |
|
1511 |
||
1512 |
^ self class |
|
1513 |
matchScan:matchScanArray |
|
1514 |
from:1 to:matchScanArray size |
|
1515 |
with:aString |
|
1516 |
from:start to:stop |
|
1517 |
ignoreCase:ignoreCase |
|
1518 |
||
1519 |
" |
|
1520 |
'*ute*' match:'12345COMPUTER' from:1 to:5 ignoreCase:true |
|
1521 |
'*ute*' match:'12345COMPUTER' from:6 to:13 ignoreCase:true |
|
1522 |
" |
|
359 | 1523 |
! |
1524 |
||
608 | 1525 |
match:aString ignoreCase:ignoreCase |
1526 |
"return true if aString matches self, where self may contain meta-match |
|
1527 |
characters $* (to match any string) or $# (to match any character) |
|
1528 |
or [...] to match a set of characters. |
|
1529 |
If ignoreCase is true, lower/uppercase are considered the same. |
|
1530 |
NOTICE: match-meta character interpretation is like in unix-matching, |
|
1531 |
NOT the ST-80 meaning." |
|
1532 |
||
1533 |
^ self match:aString from:1 to:aString size ignoreCase:ignoreCase |
|
1534 |
||
1535 |
" |
|
1536 |
'*ute*' match:'COMPUTER' ignoreCase:true |
|
1537 |
'*uter' match:'COMPUTER' ignoreCase:false |
|
1538 |
'[abcd]*' match:'computer' ignoreCase:false |
|
1539 |
'[abcd]*' match:'Computer' ignoreCase:false |
|
1540 |
'[a-k]*' match:'komputer' ignoreCase:false |
|
1541 |
'[a-k]*' match:'zomputer' ignoreCase:false |
|
1542 |
'[a-k]*' match:'Komputer' ignoreCase:false |
|
1543 |
'[a-k]*' match:'Komputer' ignoreCase:true |
|
1544 |
'*some*compl*ern*' match:'this is some more complicated pattern match' ignoreCase:true |
|
1545 |
'*some*compl*ern*' match:'this is another complicated pattern match' ignoreCase:true |
|
1546 |
||
1547 |
Time millisecondsToRun:[ |
|
1548 |
Symbol allInstancesDo:[:sym | |
|
1549 |
'[ab]*' match:sym ignoreCase:false |
|
1550 |
] |
|
1551 |
]. |
|
1552 |
" |
|
1553 |
! ! |
|
1554 |
||
1555 |
!CharacterArray methodsFor:'printing & storing'! |
|
1556 |
||
1557 |
article |
|
1558 |
"return an article string for the receiver." |
|
1559 |
||
1560 |
|firstChar| |
|
1561 |
||
1562 |
firstChar := (self at:1) asLowercase. |
|
1563 |
(firstChar isVowel or:[firstChar == $x]) ifTrue:[ |
|
1564 |
firstChar ~~ $u ifTrue:[ |
|
1565 |
^ 'an' |
|
1566 |
] |
|
1567 |
]. |
|
1568 |
^ 'a' |
|
1569 |
! |
|
1570 |
||
1571 |
displayString |
|
1572 |
"return a string to display the receiver - use storeString to have |
|
1573 |
quotes around." |
|
1574 |
||
1575 |
^ self storeString |
|
1576 |
! |
|
1577 |
||
1578 |
printOn:aStream |
|
1579 |
"print the receiver on aStream" |
|
1580 |
||
1581 |
aStream nextPutAll:self |
|
1582 |
! |
|
1583 |
||
1584 |
printString |
|
1585 |
"return a string for printing - thats myself" |
|
1586 |
||
1587 |
^ self |
|
1588 |
! ! |
|
1589 |
||
1590 |
!CharacterArray methodsFor:'queries'! |
|
1591 |
||
1592 |
encoding |
|
1593 |
^ #unknown |
|
1594 |
! |
|
1595 |
||
1596 |
isString |
|
1597 |
"return true, if the receiver is some kind of string; |
|
1598 |
true is returned here - redefinition of Object>>isString." |
|
1599 |
||
1600 |
^ true |
|
1601 |
! ! |
|
1602 |
||
1603 |
!CharacterArray methodsFor:'special string converting'! |
|
1604 |
||
1605 |
chopTo:maxLen |
|
1606 |
"if the receivers size is less or equal to maxLen, return it. |
|
1607 |
Otherwise, return a copy of the receiver, where some characters |
|
1608 |
in the middle have been removed for a total string length |
|
1609 |
of maxLen." |
|
1610 |
||
1611 |
|sz n1 n2| |
|
1612 |
||
1613 |
(sz := self size) > maxLen ifTrue:[ |
|
1614 |
n1 := n2 := maxLen // 2. |
|
1615 |
maxLen odd ifTrue:[ |
|
1616 |
n2 := n1 + 1 |
|
1617 |
]. |
|
1618 |
^ (self copyFrom:1 to:n1) , (self copyFrom:sz - n2 + 1) |
|
1619 |
] |
|
1620 |
||
1621 |
" |
|
1622 |
'12345678901234' chopTo:15 |
|
1623 |
'123456789012345' chopTo:15 |
|
1624 |
'1234567890123456' chopTo:15 |
|
1625 |
'aShortString' chopTo:15 |
|
1626 |
'aVeryLongNameForAStringThatShouldBeShortened' chopTo:15 |
|
1627 |
" |
|
1628 |
! |
|
1629 |
||
1630 |
contractAtBeginningTo:maxLen |
|
1631 |
"if the receivers size is less or equal to maxLen, return it. |
|
1632 |
Otherwise, return a copy of the receiver, where some characters |
|
1633 |
at the beginning have been replaced by '...' for a total string length |
|
1634 |
of maxLen. Can be used to abbreviate long entries in tables." |
|
1635 |
||
1636 |
|sz| |
|
1637 |
||
1638 |
(sz := self size) > maxLen ifTrue:[ |
|
1639 |
^ '...' , (self copyFrom:(sz - (maxLen - 4))) |
|
1640 |
] |
|
1641 |
||
1642 |
" |
|
1643 |
'12345678901234' contractAtBeginningTo:15 |
|
1644 |
'123456789012345' contractAtBeginningTo:15 |
|
1645 |
'1234567890123456' contractAtBeginningTo:15 |
|
1646 |
'aShortString' contractAtBeginningTo:15 |
|
1647 |
'aVeryLongNameForAStringThatShouldBeShortened' contractAtBeginningTo:15 |
|
1648 |
" |
|
1649 |
! |
|
359 | 1650 |
|
608 | 1651 |
contractAtEndTo:maxLen |
1652 |
"if the receivers size is less or equal to maxLen, return it. |
|
1653 |
Otherwise, return a copy of the receiver, where some characters |
|
1654 |
at the end have been replaced by '...' for a total string length |
|
1655 |
of maxLen. Can be used to abbreviate long entries in tables." |
|
1656 |
||
1657 |
|sz| |
|
1658 |
||
1659 |
(sz := self size) > maxLen ifTrue:[ |
|
1660 |
^ self copyReplaceFrom:maxLen - 3 |
|
1661 |
with:'...' |
|
1662 |
] |
|
1663 |
||
1664 |
" |
|
1665 |
'12345678901234' contractAtEndTo:15 |
|
1666 |
'123456789012345' contractAtEndTo:15 |
|
1667 |
'1234567890123456' contractAtEndTo:15 |
|
1668 |
'aShortString' contractAtEndTo:15 |
|
1669 |
'aVeryLongNameForAStringThatShouldBeShortened' contractAtEndTo:15 |
|
1670 |
" |
|
1671 |
! |
|
1672 |
||
1673 |
contractTo:maxLen |
|
1674 |
"if the receivers size is less or equal to maxLen, return it. |
|
1675 |
Otherwise, return a copy of the receiver, where some characters |
|
1676 |
in the middle have been replaced by '...' for a total string length |
|
1677 |
of maxLen. Can be used to abbreviate long entries in tables." |
|
1678 |
||
1679 |
|sz "{ SmallInteger }" |
|
1680 |
halfSize "{ SmallInteger }"| |
|
1681 |
||
1682 |
(sz := self size) > maxLen ifTrue:[ |
|
1683 |
halfSize := maxLen // 2. |
|
1684 |
^ self copyReplaceFrom:halfSize - 1 |
|
1685 |
to:sz - maxLen + halfSize + 1 |
|
1686 |
with:'...' |
|
1687 |
] |
|
1688 |
||
1689 |
" |
|
1690 |
'12345678901234' contractTo:15 |
|
1691 |
'123456789012345' contractTo:15 |
|
1692 |
'1234567890123456' contractTo:15 |
|
1693 |
'aShortString' contractTo:15 |
|
1694 |
'aVeryLongNameForAStringThatShouldBeShortened' contractTo:15 |
|
1695 |
" |
|
1696 |
! |
|
1697 |
||
1698 |
expandPlaceholdersWith:argArray |
|
1699 |
"return a copy of the receiver, where all %i escapes are |
|
1700 |
replaced by corresponding arguments from the argArray. |
|
1701 |
I.e. 'hello %1; how is %2' expandPlaceholdersWith:#('world' 'this') results |
|
1702 |
in the new string 'hello world; how is this'. |
|
1703 |
To get a '%' character, use a '%%'-escape. |
|
1704 |
See also bindWith:... for VisualAge compatibility." |
|
1705 |
||
1706 |
|expandedString next |
|
1707 |
idx "{ SmallInteger }" |
|
1708 |
start "{ SmallInteger }" |
|
1709 |
stop "{ SmallInteger }"| |
|
1710 |
||
1711 |
expandedString := ''. |
|
1712 |
stop := self size. |
|
1713 |
start := 1. |
|
1714 |
[start <= stop] whileTrue:[ |
|
1715 |
idx := self indexOf:$% startingAt:start. |
|
1716 |
idx == 0 ifTrue:[ |
|
1717 |
^ expandedString , (self copyFrom:start to:stop) |
|
1718 |
]. |
|
1719 |
"found a %" |
|
1720 |
expandedString := expandedString , (self copyFrom:start to:(idx - 1)). |
|
1721 |
next := self at:(idx + 1). |
|
1722 |
(next == $%) ifTrue:[ |
|
1723 |
expandedString := expandedString , '%' |
|
1724 |
] ifFalse:[ |
|
1725 |
expandedString := expandedString , (argArray at:(next digitValue)) printString |
|
1726 |
]. |
|
1727 |
start := idx + 2 |
|
1728 |
]. |
|
1729 |
^ expandedString |
|
1730 |
||
1731 |
" |
|
1732 |
'hello %1' expandPlaceholdersWith:#('world') |
|
1733 |
'hello %1; how is %2' expandPlaceholdersWith:#('world' 'this') |
|
1734 |
'hello %2; how is %1' expandPlaceholdersWith:#('world' 'this') |
|
1735 |
" |
|
1736 |
! |
|
1737 |
||
1738 |
withCRs |
|
1739 |
"return a new string consisting of receivers characters |
|
1740 |
with all \-characters replaced by cr-characters." |
|
1741 |
||
1742 |
^ self copy replaceAll:$\ by:(Character cr) |
|
1743 |
||
1744 |
" |
|
1745 |
'hello\world' withCRs |
|
1746 |
" |
|
1747 |
! |
|
1748 |
||
1749 |
withEscapes |
|
1750 |
"return a new string consisting of receivers characters |
|
1751 |
with all \X-character escapes replaced by corresponding-characters. |
|
1752 |
(similar to the way C-language Strings are converted). |
|
1753 |
The following escapes are supported: |
|
1754 |
\r return character |
|
1755 |
\n newline character |
|
1756 |
\b backspace character |
|
1757 |
\f formfeed character |
|
1758 |
\t tab character |
|
1759 |
\\ the \ character itself |
|
1760 |
\nnn three digit octal number defining the characters ascii value |
|
1761 |
\other other |
|
1762 |
||
1763 |
Notice, that \' is NOT a valid escape, since the general syntax of |
|
1764 |
string constants is not affected by this method. |
|
1765 |
||
1766 |
Although easily implementable, this is NOT done automatically |
|
1767 |
by the compiler (due to a lack of a language standard for this). |
|
1768 |
However, the compiler may detect sends ot #withEscapes to string literals |
|
1769 |
and place a modified string constant into the binary/byte-code. |
|
1770 |
Therefore, no runtime penalty will be payed for using these escapes. |
|
1771 |
(not in pre 2.11 versions) |
|
1772 |
" |
|
1773 |
||
1774 |
|sz "{ SmallInteger }" |
|
1775 |
newSize "{ SmallInteger }" |
|
1776 |
srcIdx "{ SmallInteger }" |
|
1777 |
dstIdx "{ SmallInteger }" |
|
1778 |
val "{ SmallInteger }" |
|
1779 |
newString next start| |
|
359 | 1780 |
|
1781 |
" |
|
608 | 1782 |
first, count the number of escapes, to allow preallocation |
1783 |
of the new string ... |
|
1784 |
(it is faster to scan the string twice than to reallocate it multiple |
|
1785 |
times in a WriteStream) |
|
1786 |
" |
|
1787 |
sz := newSize := self size. |
|
1788 |
srcIdx := 1. |
|
1789 |
[(srcIdx := self indexOf:$\ startingAt:srcIdx) ~~ 0] whileTrue:[ |
|
1790 |
srcIdx == sz ifFalse:[ |
|
1791 |
newSize := newSize - 1. |
|
1792 |
srcIdx := srcIdx + 1. |
|
1793 |
next := self at:srcIdx. |
|
1794 |
next == $0 ifTrue:[ |
|
1795 |
[(self at:srcIdx) isDigit] whileTrue:[ |
|
1796 |
newSize := newSize - 1. srcIdx := srcIdx + 1. |
|
1797 |
] |
|
1798 |
]. |
|
1799 |
]. |
|
1800 |
srcIdx := srcIdx + 1. |
|
1801 |
]. |
|
1802 |
||
1803 |
newSize == sz ifTrue:[ |
|
1804 |
^ self |
|
1805 |
]. |
|
1806 |
||
1807 |
newString := self species new:newSize. |
|
1808 |
" |
|
1809 |
copy over, replace escapes |
|
1810 |
" |
|
1811 |
srcIdx := dstIdx := 1. |
|
1812 |
[srcIdx <= sz] whileTrue:[ |
|
1813 |
next := self at:srcIdx. |
|
1814 |
srcIdx := srcIdx + 1. |
|
1815 |
next == $\ ifTrue:[ |
|
1816 |
srcIdx <= sz ifTrue:[ |
|
1817 |
next := self at:srcIdx. |
|
1818 |
srcIdx := srcIdx + 1. |
|
1819 |
next == $r ifTrue:[ |
|
1820 |
next := Character return |
|
1821 |
] ifFalse:[ |
|
1822 |
next == $n ifTrue:[ |
|
1823 |
next := Character nl |
|
1824 |
] ifFalse:[ |
|
1825 |
next == $b ifTrue:[ |
|
1826 |
next := Character backspace |
|
1827 |
] ifFalse:[ |
|
1828 |
next == $f ifTrue:[ |
|
1829 |
next := Character newPage |
|
1830 |
] ifFalse:[ |
|
1831 |
next == $t ifTrue:[ |
|
1832 |
next := Character tab |
|
1833 |
] ifFalse:[ |
|
1834 |
next == $0 ifTrue:[ |
|
1835 |
val := 0. |
|
1836 |
[next isDigit] whileTrue:[ |
|
1837 |
val := val * 8 + next digitValue. |
|
1838 |
next := self at:srcIdx. |
|
1839 |
srcIdx := srcIdx + 1. |
|
1840 |
]. |
|
1841 |
next := Character value:val. |
|
1842 |
] |
|
1843 |
] |
|
1844 |
] |
|
1845 |
] |
|
1846 |
] |
|
1847 |
]. |
|
1848 |
]. |
|
1849 |
]. |
|
1850 |
newString at:dstIdx put:next. |
|
1851 |
dstIdx := dstIdx + 1. |
|
1852 |
]. |
|
1853 |
^ newString |
|
1854 |
||
1855 |
" |
|
1856 |
'hello world' withEscapes |
|
1857 |
'hello\world' withEscapes |
|
1858 |
'hello\world\' withEscapes |
|
1859 |
'hello world\' withEscapes |
|
1860 |
'hello\tworld' withEscapes |
|
1861 |
'hello\nworld\na\n\tnice\n\t\tstring' withEscapes |
|
1862 |
'hello\tworld\n' withEscapes |
|
1863 |
'hello\010world' withEscapes |
|
1864 |
'hello\r\nworld' withEscapes |
|
1865 |
" |
|
1866 |
! |
|
1867 |
||
1868 |
withTabs |
|
735
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
678
diff
changeset
|
1869 |
"return a string consisting of the receivers characters |
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
678
diff
changeset
|
1870 |
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
|
1871 |
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
|
1872 |
otherwise a new string is returned. |
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
678
diff
changeset
|
1873 |
Limitation: only the very first spaces are replaced |
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
678
diff
changeset
|
1874 |
(i.e. if the receiver contains newLine characters, |
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
678
diff
changeset
|
1875 |
no tabs are inserted after those lineBreaks)" |
608 | 1876 |
|
1877 |
|idx "{ SmallInteger }" |
|
1878 |
nTabs "{ SmallInteger }" |
|
1879 |
newString| |
|
1880 |
||
1881 |
idx := self findFirst:[:c | (c ~~ Character space)]. |
|
1882 |
nTabs := (idx-1) // 8. |
|
1883 |
nTabs == 0 ifTrue:[^ self]. |
|
1884 |
||
1885 |
"any tabs" |
|
1886 |
newString := self class new:(self size - (nTabs * 7)). |
|
1887 |
newString atAll:(1 to:nTabs) put:(Character tab). |
|
1888 |
newString replaceFrom:(nTabs + 1) with:self startingAt:(nTabs * 8 + 1). |
|
1889 |
^ newString |
|
1890 |
||
1891 |
" |
|
1892 |
'12345678901234567890' withTabs |
|
1893 |
' 8901234567890' withTabs |
|
1894 |
' 901234567890' withTabs |
|
1895 |
' 67890' withTabs |
|
1896 |
' 7890' withTabs |
|
1897 |
' 890' withTabs |
|
1898 |
" |
|
1899 |
! |
|
1900 |
||
1901 |
withTabsExpanded |
|
735
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
678
diff
changeset
|
1902 |
"return a string consisting of the receivers characters, |
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
678
diff
changeset
|
1903 |
where all tabulator characters are expanded into spaces (assuming 8-col tabs). |
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
678
diff
changeset
|
1904 |
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
|
1905 |
otherwise a new string is returned. |
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
678
diff
changeset
|
1906 |
This does handle multiline strings." |
608 | 1907 |
|
1908 |
|idx "{ SmallInteger }" str| |
|
1909 |
||
1910 |
(self includes:(Character tab)) ifFalse:[^ self]. |
|
1911 |
str := WriteStream on:String new. |
|
1912 |
||
1913 |
idx := 1. |
|
1914 |
self do:[:ch | |
|
1915 |
ch == Character tab ifFalse:[ |
|
735
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
678
diff
changeset
|
1916 |
idx := idx + 1. |
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
678
diff
changeset
|
1917 |
ch == Character cr ifTrue:[ |
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
678
diff
changeset
|
1918 |
idx := 1 |
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
678
diff
changeset
|
1919 |
]. |
608 | 1920 |
str nextPut:ch. |
1921 |
] ifTrue:[ |
|
1922 |
(idx \\ 8) to:8 do:[:ii | |
|
1923 |
str space. |
|
1924 |
idx := idx + 1 |
|
735
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
678
diff
changeset
|
1925 |
]. |
608 | 1926 |
] |
1927 |
]. |
|
1928 |
^ str contents |
|
1929 |
||
1930 |
" |
|
735
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
678
diff
changeset
|
1931 |
('1' , Character tab asString , 'x') withTabsExpanded |
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
678
diff
changeset
|
1932 |
('12345' , Character tab asString , 'x') withTabsExpanded |
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
678
diff
changeset
|
1933 |
('123456' , Character tab asString , 'x') withTabsExpanded |
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
678
diff
changeset
|
1934 |
('1234567' , Character tab asString , 'x') withTabsExpanded |
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
678
diff
changeset
|
1935 |
('12345678' , Character tab asString , 'x') withTabsExpanded |
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
678
diff
changeset
|
1936 |
('123456789' , Character tab asString , 'x') withTabsExpanded |
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
678
diff
changeset
|
1937 |
|
608 | 1938 |
(String with:Character tab |
1939 |
with:Character tab |
|
1940 |
with:$1) withTabsExpanded |
|
1941 |
||
1942 |
(String with:Character tab |
|
1943 |
with:$1 |
|
1944 |
with:Character tab |
|
1945 |
with:$2) withTabsExpanded |
|
735
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
678
diff
changeset
|
1946 |
|
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
678
diff
changeset
|
1947 |
(String with:Character tab |
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
678
diff
changeset
|
1948 |
with:$1 |
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
678
diff
changeset
|
1949 |
with:Character cr |
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
678
diff
changeset
|
1950 |
with:Character tab |
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
678
diff
changeset
|
1951 |
with:$2) withTabsExpanded |
608 | 1952 |
" |
735
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
678
diff
changeset
|
1953 |
|
362ce9e28d89
expandTabs now handles multiline strings
Claus Gittinger <cg@exept.de>
parents:
678
diff
changeset
|
1954 |
"Modified: 11.12.1995 / 15:27:50 / cg" |
608 | 1955 |
! |
1956 |
||
1957 |
withoutCRs |
|
1958 |
"return a new collection consisting of receivers elements |
|
1959 |
with all cr-characters replaced by \-characters. |
|
1960 |
This is the reverse operation of withCRs." |
|
1961 |
||
1962 |
^ self copy replaceAll:(Character cr) by:$\ |
|
1963 |
" |
|
1964 |
'hello |
|
1965 |
world' withoutCRs |
|
359 | 1966 |
" |
608 | 1967 |
! |
1968 |
||
1969 |
withoutLeadingSeparators |
|
1970 |
"return a copy of myself without leading separators. |
|
1971 |
Notice: this does remove tabs, newline or any other whitespace. |
|
1972 |
Returns an empty string, if the receiver consist only of whitespace." |
|
1973 |
||
1974 |
|index| |
|
1975 |
||
1976 |
index := self indexOfNonSeparatorStartingAt:1. |
|
1977 |
index ~~ 0 ifTrue:[ |
|
1978 |
index == 1 ifTrue:[ |
|
1979 |
^ self |
|
1980 |
]. |
|
1981 |
^ self copyFrom:index |
|
1982 |
]. |
|
1983 |
^ '' |
|
1984 |
||
1985 |
" |
|
1986 |
' foo ' withoutLeadingSeparators |
|
1987 |
'foo ' withoutLeadingSeparators |
|
1988 |
' foo' withoutLeadingSeparators |
|
1989 |
' ' withoutLeadingSeparators |
|
1990 |
'foo' withoutLeadingSeparators |
|
1991 |
(' ' , Character tab asString , ' foo ') withoutLeadingSeparators inspect |
|
1992 |
" |
|
1993 |
! |
|
1994 |
||
1995 |
withoutSeparators |
|
1996 |
"return a copy of myself without leading and trailing whitespace. |
|
1997 |
Whitespace is space, tab, newline, formfeed. |
|
1998 |
Use withoutSpaces, if you want to remove spaces only." |
|
1999 |
||
2000 |
|startIndex "{ Class: SmallInteger }" |
|
2001 |
endIndex "{ Class: SmallInteger }" |
|
2002 |
sz| |
|
2003 |
||
2004 |
sz := self size. |
|
2005 |
startIndex := 1. |
|
2006 |
endIndex := sz. |
|
2007 |
||
2008 |
[(startIndex < endIndex) and:[(self at:startIndex) isSeparator]] whileTrue:[ |
|
2009 |
startIndex := startIndex + 1 |
|
2010 |
]. |
|
2011 |
[(endIndex > 1) and:[(self at:endIndex) isSeparator]] whileTrue:[ |
|
2012 |
endIndex := endIndex - 1 |
|
2013 |
]. |
|
2014 |
startIndex > endIndex ifTrue:[ |
|
2015 |
^ '' |
|
2016 |
]. |
|
2017 |
((startIndex == 1) and:[endIndex == sz]) ifTrue:[ |
|
2018 |
^ self |
|
2019 |
]. |
|
2020 |
^ self copyFrom:startIndex to:endIndex |
|
2021 |
||
2022 |
" |
|
2023 |
' foo ' withoutSeparators |
|
2024 |
' foo' withoutSeparators |
|
2025 |
'foo ' withoutSeparators |
|
2026 |
' ' withoutSeparators |
|
2027 |
(' foo' , Character tab asString , ' ') withoutSeparators inspect |
|
2028 |
" |
|
2029 |
! |
|
2030 |
||
2031 |
withoutSpaces |
|
2032 |
"return a copy of myself without leading and trailing spaces. |
|
2033 |
Notice: this does NOT remove tabs, newline or any other whitespace. |
|
2034 |
Use withoutSeparators for this." |
|
2035 |
||
2036 |
|startIndex "{ Class: SmallInteger }" |
|
2037 |
endIndex "{ Class: SmallInteger }" |
|
2038 |
sz| |
|
2039 |
||
2040 |
sz := self size. |
|
2041 |
startIndex := 1. |
|
2042 |
endIndex := sz. |
|
2043 |
||
2044 |
[(startIndex < endIndex) and:[(self at:startIndex) == Character space]] whileTrue:[ |
|
2045 |
startIndex := startIndex + 1 |
|
2046 |
]. |
|
2047 |
[(endIndex > 1) and:[(self at:endIndex) == Character space]] whileTrue:[ |
|
2048 |
endIndex := endIndex - 1 |
|
2049 |
]. |
|
2050 |
startIndex > endIndex ifTrue:[ |
|
2051 |
^ '' |
|
2052 |
]. |
|
2053 |
((startIndex == 1) and:[endIndex == sz]) ifTrue:[ |
|
2054 |
^ self |
|
2055 |
]. |
|
2056 |
^ self copyFrom:startIndex to:endIndex |
|
2057 |
||
2058 |
" |
|
2059 |
' foo ' withoutSpaces |
|
2060 |
'foo ' withoutSpaces |
|
2061 |
' foo' withoutSpaces |
|
2062 |
' ' withoutSpaces |
|
2063 |
(' foo' , Character tab asString , ' ') withoutSpaces inspect |
|
2064 |
" |
|
2065 |
! ! |
|
2066 |
||
2067 |
!CharacterArray methodsFor:'substring searching'! |
|
2068 |
||
2069 |
findString:subString |
|
2070 |
"find a substring. if found, return the index; |
|
2071 |
if not found, return 0." |
|
2072 |
||
2073 |
^ self indexOfSubCollection:subString startingAt:1 ifAbsent:[0] |
|
2074 |
||
2075 |
" |
|
2076 |
'hello world' findString:'llo' |
|
2077 |
'hello world' findString:'ole' |
|
2078 |
" |
|
2079 |
! |
|
2080 |
||
2081 |
findString:subString ifAbsent:exceptionBlock |
|
2082 |
"find a substring. If found, return the index; |
|
2083 |
if not found, return the result of evaluating exceptionBlock." |
|
2084 |
||
2085 |
^ self indexOfSubCollection:subString startingAt:1 ifAbsent:exceptionBlock |
|
2086 |
! |
|
2087 |
||
2088 |
findString:subString startingAt:index |
|
2089 |
"find a substring, starting at index. if found, return the index; |
|
2090 |
if not found, return 0." |
|
2091 |
||
2092 |
^ self indexOfSubCollection:subString startingAt:index ifAbsent:[0] |
|
2093 |
||
2094 |
" |
|
2095 |
'hello yello' findString:'llo' startingAt:1 |
|
2096 |
'hello yello' findString:'llo' startingAt:5 |
|
2097 |
'hello yello' findString:'llo' startingAt:15 |
|
2098 |
" |
|
2099 |
! |
|
2100 |
||
2101 |
findString:subString startingAt:index ifAbsent:exceptionBlock |
|
2102 |
"find a substring, starting at index. if found, return the index; |
|
2103 |
if not found, return the result of evaluating exceptionBlock." |
|
2104 |
||
2105 |
^ self indexOfSubCollection:subString startingAt:index ifAbsent:exceptionBlock |
|
2106 |
! |
|
2107 |
||
2108 |
includesString:aString |
|
2109 |
"return true, if a substring is contained in the receiver" |
|
2110 |
||
2111 |
^ (self indexOfSubCollection:aString startingAt:1 ifAbsent:[0]) ~~ 0 |
|
2112 |
||
2113 |
" |
|
2114 |
'hello world' includesString:'hel' |
|
2115 |
'hello world' includesString:'rld' |
|
2116 |
'hello world' includesString:'llo' |
|
2117 |
'hello world' includesString:'LLO' |
|
2118 |
" |
|
2119 |
! |
|
2120 |
||
2121 |
indexOfSubCollection:subString startingAt:index ifAbsent:exceptionBlock |
|
2122 |
"find a substring, starting at index. if found, return the index; |
|
2123 |
if not found, return the result of evaluating exceptionBlock. |
|
2124 |
This is a q&d hack - not very efficient" |
|
2125 |
||
2126 |
|firstChar found |
|
2127 |
startIndex "{ Class: SmallInteger }" |
|
2128 |
subSize "{ Class: SmallInteger }" |
|
2129 |
mySize "{ Class: SmallInteger }" |
|
2130 |
runIdx "{ Class: SmallInteger }" | |
|
2131 |
||
2132 |
subSize := subString size. |
|
2133 |
subSize == 0 ifTrue:[^ index]. "empty string matches" |
|
2134 |
mySize := self size. |
|
2135 |
firstChar := subString at:1. |
|
2136 |
startIndex := self indexOf:firstChar startingAt:index. |
|
2137 |
[startIndex == 0] whileFalse:[ |
|
2138 |
runIdx := startIndex. |
|
2139 |
found := true. |
|
2140 |
1 to:subSize do:[:i | |
|
2141 |
runIdx > mySize ifTrue:[ |
|
2142 |
found := false |
|
2143 |
] ifFalse:[ |
|
2144 |
(subString at:i) ~~ (self at:runIdx) ifTrue:[ |
|
2145 |
found := false |
|
2146 |
] |
|
2147 |
]. |
|
2148 |
runIdx := runIdx + 1 |
|
2149 |
]. |
|
2150 |
found ifTrue:[ |
|
2151 |
^ startIndex |
|
2152 |
]. |
|
2153 |
startIndex := self indexOf:firstChar startingAt:(startIndex + 1) |
|
2154 |
]. |
|
2155 |
^ exceptionBlock value |
|
651
2093b018dc3a
added restAfter:withoutSeparators:
Claus Gittinger <cg@exept.de>
parents:
650
diff
changeset
|
2156 |
! |
2093b018dc3a
added restAfter:withoutSeparators:
Claus Gittinger <cg@exept.de>
parents:
650
diff
changeset
|
2157 |
|
652 | 2158 |
restAfter:keyword withoutSeparators:strip |
651
2093b018dc3a
added restAfter:withoutSeparators:
Claus Gittinger <cg@exept.de>
parents:
650
diff
changeset
|
2159 |
"compare the left of the receiver with keyword, |
2093b018dc3a
added restAfter:withoutSeparators:
Claus Gittinger <cg@exept.de>
parents:
650
diff
changeset
|
2160 |
if it matches return the right. |
2093b018dc3a
added restAfter:withoutSeparators:
Claus Gittinger <cg@exept.de>
parents:
650
diff
changeset
|
2161 |
Finally, if strip is true, remove whiteSpace. |
2093b018dc3a
added restAfter:withoutSeparators:
Claus Gittinger <cg@exept.de>
parents:
650
diff
changeset
|
2162 |
This method is used to match and extract lines of the form: |
2093b018dc3a
added restAfter:withoutSeparators:
Claus Gittinger <cg@exept.de>
parents:
650
diff
changeset
|
2163 |
something: rest |
759
908363ce8a32
interest is written with one 'r' (shame on me)
Claus Gittinger <cg@exept.de>
parents:
735
diff
changeset
|
2164 |
where we are interested in rest, but only if the receiver string |
651
2093b018dc3a
added restAfter:withoutSeparators:
Claus Gittinger <cg@exept.de>
parents:
650
diff
changeset
|
2165 |
begins with something. |
2093b018dc3a
added restAfter:withoutSeparators:
Claus Gittinger <cg@exept.de>
parents:
650
diff
changeset
|
2166 |
|
2093b018dc3a
added restAfter:withoutSeparators:
Claus Gittinger <cg@exept.de>
parents:
650
diff
changeset
|
2167 |
You may wonder why such a specialized method exists here |
2093b018dc3a
added restAfter:withoutSeparators:
Claus Gittinger <cg@exept.de>
parents:
650
diff
changeset
|
2168 |
- this is so common when processing mailboxes, |
2093b018dc3a
added restAfter:withoutSeparators:
Claus Gittinger <cg@exept.de>
parents:
650
diff
changeset
|
2169 |
rcs files, nntp/pop3 responses, that is was considered worth |
2093b018dc3a
added restAfter:withoutSeparators:
Claus Gittinger <cg@exept.de>
parents:
650
diff
changeset
|
2170 |
a special method here to avoid having the code below a hundred |
2093b018dc3a
added restAfter:withoutSeparators:
Claus Gittinger <cg@exept.de>
parents:
650
diff
changeset
|
2171 |
times in variuos places." |
2093b018dc3a
added restAfter:withoutSeparators:
Claus Gittinger <cg@exept.de>
parents:
650
diff
changeset
|
2172 |
|
2093b018dc3a
added restAfter:withoutSeparators:
Claus Gittinger <cg@exept.de>
parents:
650
diff
changeset
|
2173 |
|rest| |
2093b018dc3a
added restAfter:withoutSeparators:
Claus Gittinger <cg@exept.de>
parents:
650
diff
changeset
|
2174 |
|
2093b018dc3a
added restAfter:withoutSeparators:
Claus Gittinger <cg@exept.de>
parents:
650
diff
changeset
|
2175 |
(self startsWith:keyword) ifTrue:[ |
2093b018dc3a
added restAfter:withoutSeparators:
Claus Gittinger <cg@exept.de>
parents:
650
diff
changeset
|
2176 |
rest := self copyFrom:(keyword size + 1). |
2093b018dc3a
added restAfter:withoutSeparators:
Claus Gittinger <cg@exept.de>
parents:
650
diff
changeset
|
2177 |
strip ifTrue:[ |
2093b018dc3a
added restAfter:withoutSeparators:
Claus Gittinger <cg@exept.de>
parents:
650
diff
changeset
|
2178 |
rest := rest withoutSeparators |
2093b018dc3a
added restAfter:withoutSeparators:
Claus Gittinger <cg@exept.de>
parents:
650
diff
changeset
|
2179 |
]. |
2093b018dc3a
added restAfter:withoutSeparators:
Claus Gittinger <cg@exept.de>
parents:
650
diff
changeset
|
2180 |
^ rest |
2093b018dc3a
added restAfter:withoutSeparators:
Claus Gittinger <cg@exept.de>
parents:
650
diff
changeset
|
2181 |
]. |
2093b018dc3a
added restAfter:withoutSeparators:
Claus Gittinger <cg@exept.de>
parents:
650
diff
changeset
|
2182 |
^ nil |
2093b018dc3a
added restAfter:withoutSeparators:
Claus Gittinger <cg@exept.de>
parents:
650
diff
changeset
|
2183 |
|
2093b018dc3a
added restAfter:withoutSeparators:
Claus Gittinger <cg@exept.de>
parents:
650
diff
changeset
|
2184 |
" |
2093b018dc3a
added restAfter:withoutSeparators:
Claus Gittinger <cg@exept.de>
parents:
650
diff
changeset
|
2185 |
'foo: hello world' restAfter:'foo:' withoutSeparators:true |
2093b018dc3a
added restAfter:withoutSeparators:
Claus Gittinger <cg@exept.de>
parents:
650
diff
changeset
|
2186 |
'funny: something' restAfter:'foo:' withoutSeparators:true |
2093b018dc3a
added restAfter:withoutSeparators:
Claus Gittinger <cg@exept.de>
parents:
650
diff
changeset
|
2187 |
|
2093b018dc3a
added restAfter:withoutSeparators:
Claus Gittinger <cg@exept.de>
parents:
650
diff
changeset
|
2188 |
'foo: hello world ' restAfter:'foo:' withoutSeparators:true |
2093b018dc3a
added restAfter:withoutSeparators:
Claus Gittinger <cg@exept.de>
parents:
650
diff
changeset
|
2189 |
'foo: hello world ' restAfter:'foo:' withoutSeparators:false |
2093b018dc3a
added restAfter:withoutSeparators:
Claus Gittinger <cg@exept.de>
parents:
650
diff
changeset
|
2190 |
" |
2093b018dc3a
added restAfter:withoutSeparators:
Claus Gittinger <cg@exept.de>
parents:
650
diff
changeset
|
2191 |
|
2093b018dc3a
added restAfter:withoutSeparators:
Claus Gittinger <cg@exept.de>
parents:
650
diff
changeset
|
2192 |
"Created: 25.11.1995 / 11:04:18 / cg" |
328 | 2193 |
! ! |
2194 |
||
255 | 2195 |
!CharacterArray methodsFor:'testing'! |
64 | 2196 |
|
2197 |
countWords |
|
2198 |
"return the number of words, which are separated by separators" |
|
2199 |
||
92 | 2200 |
|tally "{ Class: SmallInteger }" |
2201 |
start "{ Class: SmallInteger }" |
|
2202 |
mySize "{ Class: SmallInteger }" |
|
2203 |
stop ch| |
|
64 | 2204 |
|
2205 |
tally := 0. |
|
2206 |
start := 1. |
|
2207 |
mySize := self size. |
|
2208 |
[start <= mySize] whileTrue:[ |
|
154 | 2209 |
ch := self at:start. |
2210 |
ch isSeparator ifTrue:[ |
|
2211 |
start := start + 1 |
|
2212 |
] ifFalse:[ |
|
2213 |
stop := self indexOfSeparatorStartingAt:start. |
|
2214 |
(stop == 0) ifTrue:[ |
|
2215 |
stop := mySize + 1 |
|
2216 |
]. |
|
2217 |
tally := tally + 1. |
|
2218 |
start := stop |
|
2219 |
] |
|
64 | 2220 |
]. |
2221 |
^ tally |
|
2222 |
||
92 | 2223 |
" |
2224 |
'hello world isnt this nice' countWords' |
|
2225 |
" |
|
64 | 2226 |
! |
2227 |
||
608 | 2228 |
isAlphaNumeric |
2229 |
"return true, if the receiver is some alphanumeric word; |
|
2230 |
i.e. consists of a letter followed by letters or digits." |
|
64 | 2231 |
|
608 | 2232 |
self size == 0 ifTrue:[ |
2233 |
"mhmh what is this ?" |
|
2234 |
^ false |
|
64 | 2235 |
]. |
608 | 2236 |
(self at:1) isLetter ifFalse:[^ false]. |
2237 |
self do:[:char | |
|
2238 |
char isLetterOrDigit ifFalse:[^ false]. |
|
2239 |
]. |
|
2240 |
^ true |
|
64 | 2241 |
|
77 | 2242 |
" |
608 | 2243 |
'helloWorld' isAlphaNumeric |
2244 |
'foo1234' isAlphaNumeric |
|
2245 |
'f1234' isAlphaNumeric |
|
2246 |
'1234' isAlphaNumeric |
|
2247 |
'+' isAlphaNumeric |
|
77 | 2248 |
" |
2249 |
! |
|
2250 |
||
608 | 2251 |
isBlank |
2252 |
"return true, if the receiver contains spaces only" |
|
77 | 2253 |
|
608 | 2254 |
self do:[:char | |
2255 |
char ~~ Character space ifTrue:[^ false]. |
|
77 | 2256 |
]. |
608 | 2257 |
^ true |
64 | 2258 |
! |
2259 |
||
2260 |
levenshteinTo:aString |
|
2261 |
"return the levenshtein distance to the argument, aString; |
|
2262 |
this value corrensponds to the number of replacements that have to be |
|
2263 |
made to get aString from the receiver. |
|
196 | 2264 |
See IEEE transactions on Computers 1976 Pg 172 ff." |
64 | 2265 |
|
327 | 2266 |
" |
2267 |
in the following, we assum that ommiting a character |
|
2268 |
is less of an error than inserting an extra character. |
|
2269 |
Therefore the different insertion (i) and deletion (d) |
|
2270 |
values. |
|
2271 |
" |
|
2272 |
||
64 | 2273 |
^ self levenshteinTo:aString s:4 c:1 i:2 d:6 |
154 | 2274 |
|
2275 |
" |
|
2276 |
'ocmprt' levenshteinTo:'computer' |
|
2277 |
'computer' levenshteinTo:'computer' |
|
2278 |
'ocmputer' levenshteinTo:'computer' |
|
2279 |
'cmputer' levenshteinTo:'computer' |
|
327 | 2280 |
'computer' levenshteinTo:'cmputer' |
154 | 2281 |
'Computer' levenshteinTo:'computer' |
2282 |
" |
|
64 | 2283 |
! |
2284 |
||
2285 |
levenshteinTo:aString s:substWeight c:caseWeight i:insrtWeight d:deleteWeight |
|
196 | 2286 |
"parametrized levenshtein. |
2287 |
return the levenshtein distance to the argument, aString; |
|
2288 |
this value corrensponds to the number of replacements that have to be |
|
2289 |
made to get aString from the receiver. |
|
2290 |
The arguments are the costs for substitution, case-change, insertion and |
|
2291 |
deletion of a character. |
|
2292 |
See IEEE transactions on Computers 1976 Pg 172 ff" |
|
64 | 2293 |
|
2294 |
|d "delta matrix" |
|
196 | 2295 |
len1 "{ Class: SmallInteger }" |
2296 |
len2 "{ Class: SmallInteger }" |
|
2297 |
dim "{ Class: SmallInteger }" |
|
2298 |
prevRow row col |
|
2299 |
dimPlus1 "{ Class: SmallInteger }" |
|
64 | 2300 |
min pp c1 c2| |
2301 |
||
2302 |
len1 := self size. |
|
2303 |
len2 := aString size. |
|
2304 |
||
2305 |
"create the help-matrix" |
|
2306 |
||
2307 |
dim := len1 max:len2. |
|
2308 |
dimPlus1 := dim + 1. |
|
2309 |
||
2310 |
d := Array new:dimPlus1. |
|
2311 |
1 to:dimPlus1 do:[:i | |
|
154 | 2312 |
d at:i put:(Array new:dimPlus1) |
64 | 2313 |
]. |
2314 |
||
2315 |
"init help-matrix" |
|
2316 |
||
2317 |
(d at:1) at:1 put:0. |
|
2318 |
row := d at:1. |
|
2319 |
1 to:dim do:[:j | |
|
154 | 2320 |
row at:(j + 1) put:( (row at:j) + insrtWeight ) |
64 | 2321 |
]. |
2322 |
||
2323 |
1 to:dim do:[:i | |
|
154 | 2324 |
(d at:(i + 1)) at:1 put:( ((d at:i) at:1) + deleteWeight ) |
64 | 2325 |
]. |
2326 |
||
2327 |
1 to:len1 do:[:i | |
|
154 | 2328 |
c1 := self at:i. |
2329 |
1 to:len2 do:[:j | |
|
2330 |
c2 := aString at:j. |
|
2331 |
(c1 == c2) ifTrue:[ |
|
2332 |
pp := 0 |
|
2333 |
] ifFalse:[ |
|
2334 |
(c1 asLowercase == c2 asLowercase) ifTrue:[ |
|
2335 |
pp := caseWeight |
|
2336 |
] ifFalse:[ |
|
2337 |
pp := substWeight |
|
2338 |
] |
|
2339 |
]. |
|
2340 |
prevRow := d at:i. |
|
2341 |
row := d at:(i + 1). |
|
2342 |
col := j + 1. |
|
2343 |
min := (prevRow at:j) + pp. |
|
2344 |
min := min min:( (row at:j) + insrtWeight). |
|
2345 |
min := min min:( (prevRow at:col) + deleteWeight). |
|
2346 |
row at:col put: min |
|
2347 |
] |
|
64 | 2348 |
]. |
2349 |
||
2350 |
^ (d at:(len1 + 1)) at:(len2 + 1) |
|
2351 |
! |
|
2352 |
||
608 | 2353 |
numArgs |
2354 |
"treating the receiver as a message selector, return how many arguments would it take" |
|
2355 |
||
2356 |
|binopChars| |
|
2357 |
||
2358 |
(self size > 2) ifFalse:[ |
|
2359 |
binopChars := '|&-+=*/\<>~@,'. |
|
2360 |
(self size == 1) ifTrue:[ |
|
2361 |
((binopChars occurrencesOf:(self at:1)) == 0) ifTrue:[^ 0]. |
|
2362 |
^ 1 |
|
2363 |
]. |
|
2364 |
((binopChars occurrencesOf:(self at:1)) == 0) ifFalse:[ |
|
2365 |
((binopChars occurrencesOf:(self at:2)) == 0) ifFalse:[^ 1] |
|
2366 |
] |
|
2367 |
]. |
|
2368 |
^ self occurrencesOf:$: |
|
2369 |
||
2370 |
" |
|
2371 |
'foo:bar:' numArgs |
|
2372 |
#foo:bar: numArgs |
|
2373 |
'hello' numArgs |
|
2374 |
'+' numArgs |
|
2375 |
" |
|
2376 |
! |
|
2377 |
||
2378 |
partsIfSelector |
|
2379 |
"treat the receiver as a message selector, return a collection of parts." |
|
2380 |
||
2381 |
|idx1 "{ Class: SmallInteger }" |
|
2382 |
coll idx2 sz| |
|
2383 |
||
2384 |
coll := OrderedCollection new. |
|
2385 |
idx1 := 1. |
|
2386 |
sz := self size. |
|
2387 |
[true] whileTrue:[ |
|
2388 |
idx2 := self indexOf:$: startingAt:idx1 + 1. |
|
2389 |
(idx2 == 0 or:[idx2 == sz]) ifTrue:[ |
|
2390 |
coll add:(self copyFrom:idx1). |
|
2391 |
^ coll |
|
2392 |
]. |
|
2393 |
coll add:(self copyFrom:idx1 to:idx2). |
|
2394 |
idx1 := idx2 + 1 |
|
2395 |
]. |
|
2396 |
||
2397 |
" |
|
2398 |
'foo:bar:' partsIfSelector |
|
2399 |
#foo:bar: partsIfSelector |
|
2400 |
'hello' partsIfSelector |
|
2401 |
'+' partsIfSelector |
|
2402 |
" |
|
2403 |
! |
|
2404 |
||
64 | 2405 |
spellAgainst: aString |
2406 |
"return an integer between 0 and 100 indicating how similar |
|
154 | 2407 |
the argument is to the receiver. No case conversion is done. |
2408 |
This algorithm is much simpler (but also less exact) than the |
|
2409 |
levenshtein distance. Experiment which is better for your |
|
2410 |
application." |
|
64 | 2411 |
|
2412 |
| i1 "{ Class: SmallInteger }" |
|
2413 |
i2 "{ Class: SmallInteger }" |
|
154 | 2414 |
next1 "{ Class: SmallInteger }" |
2415 |
next2 "{ Class: SmallInteger }" |
|
64 | 2416 |
size1 "{ Class: SmallInteger }" |
2417 |
size2 "{ Class: SmallInteger }" |
|
2418 |
score "{ Class: SmallInteger }" |
|
2419 |
maxLen "{ Class: SmallInteger }" | |
|
2420 |
||
2421 |
size1 := self size. |
|
2422 |
size2 := aString size. |
|
154 | 2423 |
maxLen := size1 max:size2. |
64 | 2424 |
score := 0. |
2425 |
i1 := i2 := 1. |
|
2426 |
[i1 <= size1 and: [i2 <= size2]] whileTrue:[ |
|
154 | 2427 |
next1 := i1 + 1. |
2428 |
next2 := i2 + 1. |
|
2429 |
(self at:i1) == (aString at:i2) ifTrue: [ |
|
2430 |
score := score+1. |
|
2431 |
i1 := next1. |
|
2432 |
i2 := next2 |
|
2433 |
] ifFalse: [ |
|
2434 |
(i2 < size2 and: [(self at:i1) == (aString at:next2)]) ifTrue: [ |
|
2435 |
i2 := next2 |
|
2436 |
] ifFalse: [ |
|
2437 |
(i1 < size1 and: [(self at:next1) == (aString at:i2)]) ifTrue: [ |
|
2438 |
i1 := next1 |
|
2439 |
] ifFalse: [ |
|
2440 |
i1 := next1. |
|
2441 |
i2 := next2 |
|
2442 |
] |
|
2443 |
] |
|
2444 |
] |
|
64 | 2445 |
]. |
2446 |
||
154 | 2447 |
score = maxLen ifTrue: [^ 100]. |
2448 |
^ 100 * score // maxLen |
|
64 | 2449 |
|
154 | 2450 |
" |
2451 |
'Smalltalk' spellAgainst: 'Smalltlak' |
|
2452 |
'Smalltalk' spellAgainst: 'smalltlak' |
|
2453 |
'Smalltalk' spellAgainst: 'smalltalk' |
|
2454 |
'Smalltalk' spellAgainst: 'smalltlk' |
|
2455 |
'Smalltalk' spellAgainst: 'Smalltolk' |
|
2456 |
" |
|
64 | 2457 |
! ! |
2458 |
||
629 | 2459 |
!CharacterArray class methodsFor:'documentation'! |
2460 |
||
2461 |
version |
|
815
997a02c135e7
support for decimal padding added
Claus Gittinger <cg@exept.de>
parents:
759
diff
changeset
|
2462 |
^ '$Header: /cvs/stx/stx/libbasic/CharacterArray.st,v 1.47 1995-12-23 16:33:30 cg Exp $' |
629 | 2463 |
! ! |