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