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 |
|
|
13 |
ByteArray subclass:#AbstractString
|
|
14 |
instanceVariableNames:''
|
|
15 |
classVariableNames:''
|
|
16 |
poolDictionaries:''
|
|
17 |
category:'Collections-Text'
|
|
18 |
!
|
|
19 |
|
|
20 |
AbstractString comment:'
|
|
21 |
COPYRIGHT (c) 1994 by Claus Gittinger
|
154
|
22 |
All Rights Reserved
|
92
|
23 |
|
163
|
24 |
$Header: /cvs/stx/stx/libbasic/CharacterArray.st,v 1.9 1994-10-28 01:15:39 claus Exp $
|
64
|
25 |
'!
|
|
26 |
|
|
27 |
!AbstractString class methodsFor:'documentation'!
|
|
28 |
|
82
|
29 |
copyright
|
|
30 |
"
|
|
31 |
COPYRIGHT (c) 1994 by Claus Gittinger
|
154
|
32 |
All Rights Reserved
|
82
|
33 |
|
|
34 |
This software is furnished under a license and may be used
|
|
35 |
only in accordance with the terms of that license and with the
|
|
36 |
inclusion of the above copyright notice. This software may not
|
|
37 |
be provided or otherwise made available to, or used by, any
|
|
38 |
other person. No title to or ownership of the software is
|
|
39 |
hereby transferred.
|
|
40 |
"
|
|
41 |
!
|
|
42 |
|
|
43 |
version
|
|
44 |
"
|
163
|
45 |
$Header: /cvs/stx/stx/libbasic/CharacterArray.st,v 1.9 1994-10-28 01:15:39 claus Exp $
|
82
|
46 |
"
|
|
47 |
!
|
|
48 |
|
64
|
49 |
documentation
|
|
50 |
"
|
68
|
51 |
AbstractString is a superclass for all kinds of Strings (i.e.
|
|
52 |
(singleByte-)Strings, TwoByteStrings and whatever comes in the future.
|
154
|
53 |
|
|
54 |
As the name already implies, this class is abstract, meaning that there are
|
|
55 |
no instances of it. All this class does is provide common protocol for
|
|
56 |
concrete subclasses.
|
64
|
57 |
"
|
|
58 |
! !
|
|
59 |
|
|
60 |
!AbstractString class methodsFor:'instance creation'!
|
|
61 |
|
|
62 |
basicNew
|
|
63 |
"return a new empty string"
|
|
64 |
|
|
65 |
^ self basicNew:0
|
|
66 |
!
|
|
67 |
|
|
68 |
new
|
|
69 |
"return a new empty string"
|
|
70 |
|
|
71 |
^ self basicNew:0
|
|
72 |
!
|
|
73 |
|
|
74 |
fromString:aString
|
|
75 |
"return a copy of the argument, aString"
|
|
76 |
|
68
|
77 |
^ (self basicNew:(aString size)) replaceFrom:1 with:aString
|
|
78 |
|
|
79 |
"TwoByteString fromString:'hello'"
|
64
|
80 |
! !
|
|
81 |
|
|
82 |
!AbstractString methodsFor:'converting'!
|
|
83 |
|
|
84 |
asUppercase
|
|
85 |
"return a copy of myself in uppercase letters"
|
|
86 |
|
|
87 |
|newStr
|
|
88 |
mySize "{ Class: SmallInteger }" |
|
|
89 |
|
|
90 |
mySize := self size.
|
|
91 |
newStr := self species new:mySize.
|
|
92 |
1 to:mySize do:[:i |
|
154
|
93 |
newStr at:i put:(self at:i) asUppercase
|
64
|
94 |
].
|
|
95 |
^newStr
|
|
96 |
!
|
|
97 |
|
|
98 |
asLowercase
|
|
99 |
"return a copy of myself in lowercase letters"
|
|
100 |
|
|
101 |
|newStr
|
|
102 |
mySize "{ Class: SmallInteger }" |
|
|
103 |
|
|
104 |
mySize := self size.
|
|
105 |
newStr := self species new:mySize.
|
|
106 |
1 to:mySize do:[:i |
|
154
|
107 |
newStr at:i put:(self at:i) asLowercase
|
64
|
108 |
].
|
|
109 |
^newStr
|
|
110 |
!
|
|
111 |
|
|
112 |
asString
|
|
113 |
"return myself - I am a string"
|
|
114 |
|
|
115 |
^ self
|
|
116 |
!
|
|
117 |
|
68
|
118 |
asTwoByteString
|
|
119 |
"return the receiver converted to a two-byte string"
|
|
120 |
|
|
121 |
^ TwoByteString fromString:self
|
|
122 |
!
|
|
123 |
|
|
124 |
asSingleByteString
|
|
125 |
"return the receiver converted to a 'normal' string"
|
|
126 |
|
|
127 |
^ String fromString:self
|
|
128 |
!
|
|
129 |
|
64
|
130 |
asText
|
68
|
131 |
"return a Text-object (collection of lines) from myself.
|
|
132 |
BIG warning: Text is totally misnamed here
|
154
|
133 |
- ST/X's Text has nothing to do with PP's Text.
|
|
134 |
Therefore it will be removed/renamed soon."
|
64
|
135 |
|
138
|
136 |
^ Text from:self
|
64
|
137 |
!
|
|
138 |
|
|
139 |
asNumber
|
154
|
140 |
"read a number from the receiver.
|
|
141 |
Notice, that errors may occur during the read, so you better
|
|
142 |
setup some signal handler when using this method."
|
64
|
143 |
|
|
144 |
^ Number readFromString:self
|
|
145 |
|
92
|
146 |
"
|
|
147 |
'123' asNumber
|
|
148 |
'123.567' asNumber
|
|
149 |
'(5/6)' asNumber
|
154
|
150 |
'foo' asNumber
|
|
151 |
Object errorSignal handle:[:ex | ex returnWith:0] do:['foo' asNumber]
|
92
|
152 |
"
|
|
153 |
!
|
|
154 |
|
|
155 |
asInteger
|
154
|
156 |
"read an integer from the receiver.
|
|
157 |
Notice, that errors may occur during the read, so you better
|
|
158 |
setup some signal handler when using this method."
|
92
|
159 |
|
|
160 |
^ Integer readFromString:self
|
|
161 |
|
|
162 |
"
|
|
163 |
'12345678901234567890' asInteger
|
|
164 |
'-1234' asInteger
|
|
165 |
'0.123' asInteger <- reader stops at ., returning 0 here
|
|
166 |
'0.123' asNumber <- returns what you expect
|
154
|
167 |
Object errorSignal handle:[:ex | ex returnWith:0] do:['foo' asInteger]
|
92
|
168 |
"
|
|
169 |
!
|
|
170 |
|
|
171 |
asFloat
|
154
|
172 |
"read a float number from the receiver.
|
|
173 |
Notice, that errors may occur during the read, so you better
|
|
174 |
setup some signal handler when using this method."
|
92
|
175 |
|
|
176 |
^ (Number readFromString:self) asFloat
|
|
177 |
|
|
178 |
"
|
|
179 |
'0.123' asFloat
|
|
180 |
'12345' asFloat
|
|
181 |
'(1/5)' asFloat
|
154
|
182 |
Object errorSignal handle:[:ex | ex returnWith:0] do:['foo' asFloat]
|
92
|
183 |
"
|
64
|
184 |
!
|
|
185 |
|
|
186 |
asFilename
|
|
187 |
"return a Filename with pathname taken from the receiver"
|
|
188 |
|
|
189 |
^ Filename named:self
|
|
190 |
!
|
|
191 |
|
|
192 |
asCollectionOfWords
|
|
193 |
"return a collection containing the words (separated by whitespace)
|
154
|
194 |
of the receiver. Multiple occurences of whitespace characters will
|
|
195 |
be treated like one - i.e. whitespace is skipped."
|
64
|
196 |
|
154
|
197 |
|words
|
68
|
198 |
start "{ Class:SmallInteger }"
|
|
199 |
stop "{ Class:SmallInteger }"
|
|
200 |
mySize "{ Class:SmallInteger }"|
|
64
|
201 |
|
|
202 |
words := OrderedCollection new.
|
|
203 |
start := 1.
|
|
204 |
mySize := self size.
|
|
205 |
[start <= mySize] whileTrue:[
|
154
|
206 |
start := self indexOfNonSeparatorStartingAt:start.
|
|
207 |
start == 0 ifTrue:[
|
|
208 |
^ words
|
|
209 |
].
|
|
210 |
stop := self indexOfSeparatorStartingAt:start.
|
|
211 |
stop == 0 ifTrue:[
|
|
212 |
words add:(self copyFrom:start to:mySize).
|
|
213 |
^ words
|
|
214 |
].
|
|
215 |
words add:(self copyFrom:start to:(stop - 1)).
|
|
216 |
start := stop
|
64
|
217 |
].
|
|
218 |
^ words
|
|
219 |
|
92
|
220 |
"
|
|
221 |
'hello world isnt this nice' asCollectionOfWords
|
|
222 |
' hello world isnt this nice ' asCollectionOfWords
|
|
223 |
'hello' asCollectionOfWords
|
|
224 |
'' asCollectionOfWords
|
|
225 |
' ' asCollectionOfWords
|
|
226 |
"
|
68
|
227 |
!
|
|
228 |
|
154
|
229 |
asCollectionOfSubstringsSeparatedBy:aCharacter
|
|
230 |
"return a collection containing the lines (separated by aCharacter)
|
|
231 |
of the receiver. If aCharacter occurs multiple times in a row,
|
|
232 |
the result will contain empty strings."
|
68
|
233 |
|
|
234 |
|lines myClass
|
|
235 |
numberOfLines "{ Class:SmallInteger }"
|
|
236 |
startIndex "{ Class:SmallInteger }"
|
|
237 |
stopIndex "{ Class:SmallInteger }" |
|
|
238 |
|
|
239 |
"count first, to avoid regrowing"
|
|
240 |
|
154
|
241 |
numberOfLines := (self occurrencesOf:aCharacter) + 1.
|
68
|
242 |
lines := OrderedCollection new:numberOfLines.
|
|
243 |
myClass := self species.
|
|
244 |
|
|
245 |
startIndex := 1.
|
|
246 |
1 to:numberOfLines do:[:lineNr |
|
154
|
247 |
stopIndex := self indexOf:aCharacter startingAt:startIndex.
|
|
248 |
stopIndex == 0 ifTrue:[
|
|
249 |
stopIndex := self size
|
|
250 |
] ifFalse: [
|
|
251 |
stopIndex := stopIndex - 1.
|
|
252 |
].
|
68
|
253 |
|
154
|
254 |
(stopIndex < startIndex) ifTrue: [
|
|
255 |
lines add:(myClass new:0)
|
|
256 |
] ifFalse: [
|
|
257 |
lines add:(self copyFrom:startIndex to:stopIndex)
|
|
258 |
].
|
|
259 |
startIndex := stopIndex + 2
|
68
|
260 |
].
|
|
261 |
^ lines
|
|
262 |
|
|
263 |
"
|
154
|
264 |
'1 one:2 two:3 three:4 four:5 five' withCRs asCollectionOfSubstringsSeparatedBy:$:
|
|
265 |
'1 one 2 two 3 three 4 four 5 five' withCRs asCollectionOfSubstringsSeparatedBy:Character space
|
|
266 |
"
|
|
267 |
!
|
|
268 |
|
|
269 |
asCollectionOfLines
|
|
270 |
"return a collection containing the lines (separated by cr)
|
|
271 |
of the receiver. If multiple cr's occur in a row, the result will
|
|
272 |
contain empty strings."
|
|
273 |
|
|
274 |
^ self asCollectionOfSubstringsSeparatedBy:Character cr
|
|
275 |
|
|
276 |
"
|
68
|
277 |
'1 one\2 two\3 three\4 four\5 five' withCRs asCollectionOfLines
|
154
|
278 |
'1 one\\\\2 two\3 three' withCRs asCollectionOfLines
|
68
|
279 |
"
|
92
|
280 |
!
|
|
281 |
|
|
282 |
asArrayOfSubstrings
|
|
283 |
"return an array of substrings from the receiver, interpreting
|
154
|
284 |
separators (i.e. spaces & newlines) as word-delimiters.
|
92
|
285 |
This is a compatibility method - the actual work is done in
|
|
286 |
asCollectionOfWords."
|
|
287 |
|
|
288 |
^ self asCollectionOfWords asArray
|
|
289 |
|
|
290 |
"
|
|
291 |
'1 one two three four 5 five' asArrayOfSubstrings
|
|
292 |
"
|
154
|
293 |
!
|
|
294 |
|
|
295 |
asCollectionOfSubstringsSeparatedByAny:aCollectionOfSeparators
|
|
296 |
"return a collection containing the words (separated by any character
|
|
297 |
from aCollectionOfSeparators) of the receiver.
|
|
298 |
This allows breaking up strings using any character as separator."
|
|
299 |
|
|
300 |
|words
|
|
301 |
start "{ Class:SmallInteger }"
|
|
302 |
stop "{ Class:SmallInteger }"
|
|
303 |
mySize "{ Class:SmallInteger }"|
|
|
304 |
|
|
305 |
words := OrderedCollection new.
|
|
306 |
start := 1.
|
|
307 |
mySize := self size.
|
|
308 |
[start <= mySize] whileTrue:[
|
|
309 |
"skip multiple separators"
|
|
310 |
[aCollectionOfSeparators includes:(self at:start)] whileTrue:[
|
|
311 |
start := start + 1 .
|
|
312 |
start > mySize ifTrue:[
|
|
313 |
^ words
|
|
314 |
].
|
|
315 |
].
|
|
316 |
|
|
317 |
stop := self indexOfAny:aCollectionOfSeparators startingAt:start.
|
|
318 |
stop == 0 ifTrue:[
|
|
319 |
words add:(self copyFrom:start to:mySize).
|
|
320 |
^ words
|
|
321 |
].
|
|
322 |
words add:(self copyFrom:start to:(stop - 1)).
|
|
323 |
start := stop
|
|
324 |
].
|
|
325 |
^ words
|
|
326 |
|
|
327 |
"
|
|
328 |
'hello:world:isnt:this nice' asCollectionOfSubstringsSeparatedByAny:#($:)
|
|
329 |
'hello:world:isnt:this nice' asCollectionOfSubstringsSeparatedByAny:(Array with:$: with:Character space)
|
|
330 |
'h1e2l3l4o' asCollectionOfSubstringsSeparatedByAny:($1 to: $9)
|
|
331 |
"
|
163
|
332 |
!
|
|
333 |
|
|
334 |
contractTo:maxLen
|
|
335 |
"if the receivers size is less or equal to maxLen, return it.
|
|
336 |
Otherwise, return a copy of the receiver, where some characters
|
|
337 |
in the middle have been replaced by '...' for a total string length
|
|
338 |
of maxLen. Can be used to abbreviate long entries in tables."
|
|
339 |
|
|
340 |
|sz halfSize|
|
|
341 |
|
|
342 |
(sz := self size) > maxLen ifTrue:[
|
|
343 |
halfSize := maxLen // 2.
|
|
344 |
^ self copyReplaceFrom:halfSize - 1
|
|
345 |
to:sz - maxLen + halfSize + 1
|
|
346 |
with:'...'
|
|
347 |
]
|
|
348 |
|
|
349 |
"
|
|
350 |
'12345678901234' contractTo:15
|
|
351 |
'123456789012345' contractTo:15
|
|
352 |
'1234567890123456' contractTo:15
|
|
353 |
'aShortString' contractTo:15
|
|
354 |
'aVeryLongNameForAStringThatShouldBeShortened' contractTo:15
|
|
355 |
"
|
|
356 |
!
|
|
357 |
|
|
358 |
contractAtEndTo:maxLen
|
|
359 |
"if the receivers size is less or equal to maxLen, return it.
|
|
360 |
Otherwise, return a copy of the receiver, where some characters
|
|
361 |
at the end have been replaced by '...' for a total string length
|
|
362 |
of maxLen. Can be used to abbreviate long entries in tables."
|
|
363 |
|
|
364 |
|sz|
|
|
365 |
|
|
366 |
(sz := self size) > maxLen ifTrue:[
|
|
367 |
^ self copyReplaceFrom:maxLen - 3
|
|
368 |
with:'...'
|
|
369 |
]
|
|
370 |
|
|
371 |
"
|
|
372 |
'12345678901234' contractAtEndTo:15
|
|
373 |
'123456789012345' contractAtEndTo:15
|
|
374 |
'1234567890123456' contractAtEndTo:15
|
|
375 |
'aShortString' contractAtEndTo:15
|
|
376 |
'aVeryLongNameForAStringThatShouldBeShortened' contractAtEndTo:15
|
|
377 |
"
|
|
378 |
!
|
|
379 |
|
|
380 |
contractAtBeginningTo:maxLen
|
|
381 |
"if the receivers size is less or equal to maxLen, return it.
|
|
382 |
Otherwise, return a copy of the receiver, where some characters
|
|
383 |
at the beginning have been replaced by '...' for a total string length
|
|
384 |
of maxLen. Can be used to abbreviate long entries in tables."
|
|
385 |
|
|
386 |
|sz|
|
|
387 |
|
|
388 |
(sz := self size) > maxLen ifTrue:[
|
|
389 |
^ '...' , (self copyFrom:(sz - (maxLen - 4)))
|
|
390 |
]
|
|
391 |
|
|
392 |
"
|
|
393 |
'12345678901234' contractAtBeginningTo:15
|
|
394 |
'123456789012345' contractAtBeginningTo:15
|
|
395 |
'1234567890123456' contractAtBeginningTo:15
|
|
396 |
'aShortString' contractAtBeginningTo:15
|
|
397 |
'aVeryLongNameForAStringThatShouldBeShortened' contractAtBeginningTo:15
|
|
398 |
"
|
92
|
399 |
! !
|
|
400 |
|
|
401 |
!AbstractString methodsFor:'ST/V compatibility'!
|
|
402 |
|
|
403 |
replChar:oldChar with:newChar
|
|
404 |
"return a copy of the receiver, with all oldChars replaced
|
154
|
405 |
by newChar.
|
|
406 |
This is an ST/V compatibility method."
|
92
|
407 |
|
|
408 |
^ self copy replaceAll:oldChar by:newChar
|
|
409 |
|
|
410 |
"
|
|
411 |
'12345678901234567890' replChar:$0 with:$*
|
|
412 |
"
|
|
413 |
!
|
|
414 |
|
|
415 |
replChar:oldChar withString:newString
|
|
416 |
"return a copy of the receiver, with all oldChars replaced
|
154
|
417 |
by newString (i.e. slice in the newString in place of the oldChar).
|
|
418 |
This is an ST/V compatibility method."
|
92
|
419 |
|
|
420 |
|tmpStream|
|
|
421 |
|
|
422 |
tmpStream := WriteStream on:(self class new).
|
|
423 |
self do:[:element |
|
154
|
424 |
element = oldChar ifTrue:[
|
|
425 |
tmpStream nextPutAll:newString
|
|
426 |
] ifFalse:[
|
|
427 |
tmpStream nextPut:element
|
|
428 |
].
|
92
|
429 |
].
|
|
430 |
^ tmpStream contents
|
|
431 |
|
|
432 |
"
|
|
433 |
'12345678901234567890' replChar:$0 withString:'foo'
|
|
434 |
'a string with spaces' replChar:$ withString:' foo '
|
|
435 |
"
|
|
436 |
!
|
|
437 |
|
|
438 |
trimBlanks
|
|
439 |
"return a copy of the receiver without leading
|
154
|
440 |
and trailing spaces.
|
|
441 |
This is an ST/V compatibility method."
|
92
|
442 |
|
|
443 |
^ self withoutSpaces
|
|
444 |
|
|
445 |
"
|
|
446 |
' spaces at beginning' trimBlanks
|
|
447 |
'spaces at end ' trimBlanks
|
|
448 |
' spaces at beginning and end ' trimBlanks
|
|
449 |
'no spaces' trimBlanks
|
|
450 |
"
|
|
451 |
!
|
|
452 |
|
|
453 |
byteAt:index put:aByte
|
154
|
454 |
"store a byte at given index.
|
|
455 |
This is an ST/V compatibility method."
|
92
|
456 |
|
|
457 |
(aByte == 0) ifTrue:[
|
154
|
458 |
"store a space instead"
|
|
459 |
^ super basicAt:index put:(Character space)
|
92
|
460 |
].
|
|
461 |
^ super at:index put:(Character value:aByte)
|
64
|
462 |
! !
|
|
463 |
|
|
464 |
!AbstractString methodsFor:'printing & storing'!
|
|
465 |
|
82
|
466 |
article
|
154
|
467 |
"return an article string for the receiver."
|
82
|
468 |
|
|
469 |
|firstChar|
|
|
470 |
|
|
471 |
firstChar := (self at:1) asLowercase.
|
|
472 |
(firstChar isVowel or:[firstChar == $x]) ifTrue:[
|
154
|
473 |
firstChar ~~ $u ifTrue:[
|
|
474 |
^ 'an'
|
|
475 |
]
|
82
|
476 |
].
|
|
477 |
^ 'a'
|
|
478 |
!
|
|
479 |
|
64
|
480 |
printOn:aStream
|
|
481 |
"print the receiver on aStream"
|
|
482 |
|
|
483 |
aStream nextPutAll:self
|
|
484 |
!
|
|
485 |
|
|
486 |
printString
|
|
487 |
"return a string for printing - thats myself"
|
|
488 |
|
|
489 |
^ self
|
|
490 |
!
|
|
491 |
|
|
492 |
displayString
|
|
493 |
"return a string to display the receiver - use storeString to have
|
154
|
494 |
quotes around."
|
64
|
495 |
|
|
496 |
^ self storeString
|
|
497 |
! !
|
|
498 |
|
|
499 |
!AbstractString methodsFor:'comparing'!
|
|
500 |
|
|
501 |
hash
|
|
502 |
"return an integer useful as a hash-key"
|
|
503 |
|
|
504 |
%{ /* NOCONTEXT */
|
|
505 |
|
|
506 |
REGISTER int g, val;
|
|
507 |
REGISTER unsigned char *cp, *cp0;
|
|
508 |
int l;
|
|
509 |
|
|
510 |
cp = _stringVal(self);
|
|
511 |
l = _stringSize(self);
|
|
512 |
if (_qClass(self) != String) {
|
154
|
513 |
int n = __OBJS2BYTES__(_intVal(_ClassInstPtr(_qClass(self))->c_ninstvars));
|
64
|
514 |
|
154
|
515 |
cp += n;
|
|
516 |
l -= n;
|
64
|
517 |
}
|
|
518 |
|
|
519 |
/*
|
|
520 |
* this is the dragon-book algorithm with a funny start
|
|
521 |
* value (to give short strings a number above 8192)
|
|
522 |
*/
|
|
523 |
val = 12345;
|
|
524 |
for (cp0 = cp, cp += l - 1; cp >= cp0; cp--) {
|
154
|
525 |
val = (val << 5) + (*cp & 0x1F);
|
|
526 |
if (g = (val & 0x3E000000))
|
|
527 |
val ^= g >> 25 /* 23 */ /* 25 */;
|
|
528 |
val &= 0x3FFFFFFF;
|
64
|
529 |
}
|
|
530 |
|
|
531 |
if (l) {
|
154
|
532 |
l |= 1;
|
|
533 |
val = (val * l) & 0x3FFFFFFF;
|
64
|
534 |
}
|
|
535 |
|
|
536 |
RETURN ( _MKSMALLINT(val) );
|
|
537 |
%}
|
|
538 |
!
|
|
539 |
|
|
540 |
<= something
|
|
541 |
"Compare the receiver with the argument and return true if the
|
|
542 |
receiver is less than or equal to the argument. Otherwise return false."
|
|
543 |
|
|
544 |
^ (self > something) not
|
|
545 |
!
|
|
546 |
|
|
547 |
< something
|
|
548 |
"Compare the receiver with the argument and return true if the
|
|
549 |
receiver is less than the argument. Otherwise return false."
|
|
550 |
|
|
551 |
^ (something > self)
|
|
552 |
!
|
|
553 |
|
|
554 |
>= something
|
|
555 |
"Compare the receiver with the argument and return true if the
|
|
556 |
receiver is greater than or equal to the argument.
|
|
557 |
Otherwise return false."
|
|
558 |
|
|
559 |
^ (something > self) not
|
|
560 |
!
|
|
561 |
|
|
562 |
> aString
|
|
563 |
"Compare the receiver with the argument and return true if the
|
|
564 |
receiver is greater than the argument. Otherwise return false.
|
|
565 |
In contrast to ST-80, case differences are NOT ignored, thus
|
|
566 |
'foo' > 'Foo' will return true.
|
|
567 |
This may change."
|
|
568 |
|
92
|
569 |
|mySize "{ Class: SmallInteger }"
|
|
570 |
otherSize "{ Class: SmallInteger }"
|
|
571 |
c1 c2|
|
64
|
572 |
|
|
573 |
mySize := self size.
|
|
574 |
otherSize := aString size.
|
|
575 |
|
|
576 |
1 to:(mySize min:otherSize) do:[:index |
|
154
|
577 |
c1 := self at:index.
|
|
578 |
c2 := aString at:index.
|
|
579 |
c1 > c2 ifTrue:[^ true].
|
|
580 |
c1 < c2 ifTrue:[^ false].
|
64
|
581 |
].
|
|
582 |
^ mySize > otherSize
|
|
583 |
!
|
|
584 |
|
|
585 |
= aString
|
|
586 |
"Compare the receiver with the argument and return true if the
|
92
|
587 |
receiver is equal to the argument. Otherwise return false.
|
|
588 |
This compare doe NOT ignore case differences,
|
|
589 |
therefore 'foo' = 'Foo' will return false."
|
64
|
590 |
|
92
|
591 |
|mySize "{ Class: SmallInteger }"
|
|
592 |
otherSize |
|
64
|
593 |
|
|
594 |
mySize := self size.
|
|
595 |
otherSize := aString size.
|
|
596 |
mySize == otherSize ifFalse:[^ false].
|
|
597 |
|
|
598 |
1 to:mySize do:[:index |
|
154
|
599 |
(self at:index) = (aString at:index) ifFalse:[^ false].
|
64
|
600 |
].
|
|
601 |
^ true
|
92
|
602 |
|
|
603 |
"
|
|
604 |
'foo' = 'Foo'
|
|
605 |
'foo' = 'bar'
|
|
606 |
'foo' = 'foo'
|
|
607 |
"
|
64
|
608 |
!
|
|
609 |
|
|
610 |
sameAs:aString
|
|
611 |
"Compare the receiver with the argument like =, but ignore
|
|
612 |
case differences. Return true or false"
|
|
613 |
|
92
|
614 |
|mySize "{ Class: SmallInteger }"
|
|
615 |
otherSize c1 c2|
|
64
|
616 |
|
|
617 |
mySize := self size.
|
|
618 |
otherSize := aString size.
|
|
619 |
mySize == otherSize ifFalse:[^ false].
|
|
620 |
|
|
621 |
1 to:mySize do:[:index |
|
154
|
622 |
c1 := self at:index.
|
|
623 |
c2 := aString at:index.
|
|
624 |
c1 == c2 ifFalse:[
|
|
625 |
c1 asLowercase = c2 asLowercase ifFalse:[^ false].
|
|
626 |
]
|
64
|
627 |
].
|
|
628 |
^ true
|
92
|
629 |
|
|
630 |
"
|
|
631 |
'foo' sameAs: 'Foo'
|
|
632 |
'foo' sameAs: 'bar'
|
|
633 |
'foo' sameAs: 'foo'
|
|
634 |
"
|
64
|
635 |
! !
|
|
636 |
|
|
637 |
!AbstractString methodsFor:'character searching'!
|
|
638 |
|
|
639 |
includesMatchCharacters
|
|
640 |
"return true if the receiver includes any meta characters (i.e. $* or $#)
|
|
641 |
for match operations; false if not"
|
|
642 |
|
|
643 |
^ self includesAny:'*#['
|
|
644 |
!
|
|
645 |
|
68
|
646 |
indexOfSeparator
|
|
647 |
"return the index of the first whitespace character"
|
|
648 |
|
|
649 |
^ self indexOfSeparatorStartingAt:1
|
|
650 |
|
|
651 |
"'hello world' indexOfSeparator"
|
|
652 |
!
|
|
653 |
|
92
|
654 |
indexOfSeparatorStartingAt:startIndex
|
68
|
655 |
"return the index of the next whitespace character"
|
64
|
656 |
|
92
|
657 |
|start "{ Class: SmallInteger }"
|
|
658 |
mySize "{ Class: SmallInteger }"|
|
|
659 |
|
|
660 |
start := startIndex.
|
|
661 |
mySize := self size.
|
|
662 |
|
|
663 |
start to:mySize do:[:index |
|
154
|
664 |
(self at:index) isSeparator ifTrue:[^ index]
|
64
|
665 |
].
|
|
666 |
^ 0
|
|
667 |
|
|
668 |
"'hello world' indexOfSeparatorStartingAt:3"
|
92
|
669 |
!
|
|
670 |
|
|
671 |
indexOfNonSeparatorStartingAt:startIndex
|
|
672 |
"return the index of the next non-whitespace character"
|
|
673 |
|
|
674 |
|start "{ Class: SmallInteger }"
|
|
675 |
mySize "{ Class: SmallInteger }"|
|
|
676 |
|
|
677 |
start := startIndex.
|
|
678 |
mySize := self size.
|
|
679 |
|
|
680 |
start to:mySize do:[:index |
|
154
|
681 |
(self at:index) isSeparator ifFalse:[^ index]
|
92
|
682 |
].
|
|
683 |
^ 0
|
|
684 |
|
|
685 |
"
|
|
686 |
' hello world' indexOfNonSeparatorStartingAt:1
|
|
687 |
"
|
|
688 |
"
|
|
689 |
|s index1 index2|
|
|
690 |
s := ' foo bar baz'.
|
|
691 |
index1 := s indexOfNonSeparatorStartingAt:1.
|
|
692 |
index2 := s indexOfSeparatorStartingAt:index1.
|
|
693 |
s copyFrom:index1 to:index2 - 1
|
|
694 |
"
|
64
|
695 |
! !
|
|
696 |
|
|
697 |
!AbstractString methodsFor:'substring searching'!
|
|
698 |
|
|
699 |
findString:subString
|
|
700 |
"find a substring. if found, return the index;
|
92
|
701 |
if not found, return 0."
|
64
|
702 |
|
|
703 |
^ self findString:subString startingAt:1 ifAbsent:[0]
|
|
704 |
|
92
|
705 |
"
|
|
706 |
'hello world' findString:'llo'
|
|
707 |
'hello world' findString:'ole'
|
|
708 |
"
|
64
|
709 |
!
|
|
710 |
|
|
711 |
findString:subString startingAt:index
|
|
712 |
"find a substring, starting at index. if found, return the index;
|
92
|
713 |
if not found, return 0."
|
64
|
714 |
|
|
715 |
^ self findString:subString startingAt:index ifAbsent:[0]
|
|
716 |
|
92
|
717 |
"
|
|
718 |
'hello yello' findString:'llo' startingAt:1
|
|
719 |
'hello yello' findString:'llo' startingAt:5
|
|
720 |
'hello yello' findString:'llo' startingAt:15
|
|
721 |
"
|
64
|
722 |
!
|
|
723 |
|
|
724 |
findString:subString startingAt:index ifAbsent:exceptionBlock
|
|
725 |
"find a substring, starting at index. if found, return the index;
|
|
726 |
if not found, return the result of evaluating exceptionBlock.
|
|
727 |
This is a q&d hack - not very efficient"
|
|
728 |
|
|
729 |
|firstChar found
|
|
730 |
startIndex "{ Class: SmallInteger }"
|
|
731 |
subSize "{ Class: SmallInteger }"
|
|
732 |
mySize "{ Class: SmallInteger }"
|
|
733 |
runIdx "{ Class: SmallInteger }" |
|
|
734 |
|
|
735 |
subSize := subString size.
|
|
736 |
subSize == 0 ifTrue:[^ index]. "empty string matches"
|
|
737 |
mySize := self size.
|
|
738 |
firstChar := subString at:1.
|
|
739 |
startIndex := self indexOf:firstChar startingAt:index.
|
|
740 |
[startIndex == 0] whileFalse:[
|
154
|
741 |
runIdx := startIndex.
|
|
742 |
found := true.
|
|
743 |
1 to:subSize do:[:i |
|
|
744 |
runIdx > mySize ifTrue:[
|
|
745 |
found := false
|
|
746 |
] ifFalse:[
|
|
747 |
(subString at:i) ~~ (self at:runIdx) ifTrue:[
|
|
748 |
found := false
|
|
749 |
]
|
|
750 |
].
|
|
751 |
runIdx := runIdx + 1
|
|
752 |
].
|
|
753 |
found ifTrue:[
|
|
754 |
^ startIndex
|
|
755 |
].
|
|
756 |
startIndex := self indexOf:firstChar startingAt:(startIndex + 1)
|
64
|
757 |
].
|
|
758 |
^ exceptionBlock value
|
|
759 |
! !
|
|
760 |
|
|
761 |
!AbstractString methodsFor:'pattern matching'!
|
|
762 |
|
|
763 |
from:matchStart to:matchStop match:aString from:start to:stop
|
|
764 |
"helper for match; return true if the characters from start to stop in
|
|
765 |
aString are matching the receivers characters from matchStart to matchStop.
|
|
766 |
The receiver may contain meta-match characters $* (to match any string)
|
92
|
767 |
or $# (to match any character) or [...] (to match from a set of characters).
|
|
768 |
This algorithm is not very efficient; for heavy duty pattern matching,
|
|
769 |
an interface (primitive) to the regex pattern matching package should be
|
|
770 |
added."
|
64
|
771 |
|
|
772 |
|matchChar
|
|
773 |
mStart "{ Class: SmallInteger }"
|
|
774 |
mStop "{ Class: SmallInteger }"
|
|
775 |
sStart "{ Class: SmallInteger }"
|
|
776 |
sStop "{ Class: SmallInteger }"
|
|
777 |
mSize "{ Class: SmallInteger }"
|
|
778 |
sSize "{ Class: SmallInteger }"
|
|
779 |
index cont matchLast
|
|
780 |
matchSet c1 c2|
|
|
781 |
|
|
782 |
mStart := matchStart.
|
|
783 |
mStop := matchStop.
|
|
784 |
sStart := start.
|
|
785 |
sStop := stop.
|
|
786 |
|
|
787 |
[true] whileTrue:[
|
154
|
788 |
mSize := mStop - mStart + 1.
|
|
789 |
sSize := sStop - sStart + 1.
|
64
|
790 |
|
154
|
791 |
"empty strings match"
|
|
792 |
(mSize == 0) ifTrue:[^ (sSize == 0)].
|
64
|
793 |
|
154
|
794 |
matchChar := self at:mStart.
|
64
|
795 |
|
154
|
796 |
(matchChar == $#) ifTrue:[
|
|
797 |
"testString empty -> no match"
|
|
798 |
(sSize == 0) ifTrue:[^ false].
|
|
799 |
"# matches single character"
|
|
800 |
((sSize == 1) and:[mSize == 1]) ifTrue:[^ true].
|
|
801 |
"cut off 1st chars and continue"
|
|
802 |
mStart := mStart + 1.
|
|
803 |
sStart := sStart + 1
|
|
804 |
] ifFalse:[
|
|
805 |
(matchChar == $[) ifTrue:[
|
|
806 |
"testString empty -> no match"
|
|
807 |
(sSize == 0) ifTrue:[^ false].
|
64
|
808 |
|
154
|
809 |
matchSet := Set new.
|
|
810 |
c1 := nil.
|
|
811 |
[matchChar == $]] whileFalse:[
|
|
812 |
mStart := mStart + 1.
|
|
813 |
mSize := mSize - 1.
|
|
814 |
matchChar := self at:mStart.
|
|
815 |
((matchChar == $-) and:[c1 notNil]) ifTrue:[
|
|
816 |
mStart := mStart + 1.
|
|
817 |
mSize := mSize - 1.
|
|
818 |
c2 := self at:mStart.
|
|
819 |
(c1 to:c2) do:[:c | matchSet add:c].
|
|
820 |
c1 := nil
|
|
821 |
] ifFalse:[
|
|
822 |
(matchChar == $]) ifFalse:[
|
|
823 |
matchSet add:matchChar.
|
|
824 |
c1 := matchChar.
|
|
825 |
].
|
|
826 |
].
|
|
827 |
].
|
|
828 |
mStart := mStart + 1.
|
|
829 |
mSize := mSize - 1.
|
|
830 |
(matchSet includes:(aString at:sStart)) ifFalse:[^ false].
|
|
831 |
((sSize == 1) and:[mSize == 0]) ifTrue:[^ true].
|
|
832 |
"cut off 1st char and continue"
|
|
833 |
sStart := sStart + 1
|
|
834 |
] ifFalse:[
|
|
835 |
(matchChar == $*) ifTrue:[
|
|
836 |
"* matches anything"
|
|
837 |
(mSize == 1) ifTrue:[^ true].
|
|
838 |
"testString empty -> matchString not we have no match"
|
|
839 |
(sSize == 0) ifTrue:[^ false].
|
64
|
840 |
|
154
|
841 |
"try to avoid some of the recursion by checking last
|
|
842 |
character and continue with shortened strings if possible"
|
|
843 |
cont := false.
|
|
844 |
(mStop >= mStart) ifTrue:[
|
|
845 |
matchLast := self at:mStop.
|
|
846 |
(matchLast ~~ $*) ifTrue:[
|
|
847 |
(matchLast == $#) ifTrue:[
|
|
848 |
cont := true
|
|
849 |
] ifFalse:[
|
|
850 |
(matchLast == (aString at:sStop)) ifTrue:[
|
|
851 |
cont := true
|
|
852 |
]
|
|
853 |
]
|
|
854 |
]
|
|
855 |
].
|
|
856 |
cont ifFalse:[
|
|
857 |
index := sStart.
|
|
858 |
[index <= sStop] whileTrue:[
|
|
859 |
(self from:(mStart + 1) to:mStop match:aString
|
|
860 |
from:index to:sStop) ifTrue:[
|
|
861 |
^ true
|
|
862 |
].
|
|
863 |
index := index + 1
|
|
864 |
].
|
|
865 |
^ false
|
|
866 |
].
|
|
867 |
mStop := mStop - 1.
|
|
868 |
sStop := sStop - 1
|
|
869 |
] ifFalse:[
|
64
|
870 |
|
154
|
871 |
"testString empty ?"
|
|
872 |
(sSize == 0) ifTrue:[^ false].
|
64
|
873 |
|
154
|
874 |
"first characters equal ?"
|
|
875 |
((aString at:sStart) ~~ matchChar) ifTrue:[^ false].
|
64
|
876 |
|
154
|
877 |
"avoid recursion if possible"
|
|
878 |
((sSize == mSize) and:[self = aString]) ifTrue:[^ true].
|
64
|
879 |
|
154
|
880 |
"cut off 1st chars and continue"
|
|
881 |
mStart := mStart + 1.
|
|
882 |
sStart := sStart + 1
|
|
883 |
]
|
|
884 |
]
|
|
885 |
]
|
64
|
886 |
]
|
|
887 |
!
|
|
888 |
|
|
889 |
match:aString
|
|
890 |
"return true if aString matches self, where self may contain meta-match
|
|
891 |
characters $* (to match any string) or $# (to match any character)."
|
|
892 |
|
|
893 |
^ self from:1 to:(self size) match:aString from:1 to:(aString size)
|
|
894 |
|
92
|
895 |
"
|
|
896 |
'*ute*' match:'computer'
|
|
897 |
'*uter' match:'computer'
|
|
898 |
'uter*' match:'computer'
|
|
899 |
'*ute*' match:''
|
|
900 |
'[abcd]*' match:'computer'
|
|
901 |
'[abcd]*' match:'komputer'
|
|
902 |
'*some*compl*ern*' match:'this is some more complicated pattern match'
|
|
903 |
'*some*compl*ern*' match:'this is another complicated pattern match'
|
|
904 |
"
|
64
|
905 |
! !
|
|
906 |
|
|
907 |
!AbstractString methodsFor:'testing'!
|
|
908 |
|
|
909 |
isBlank
|
|
910 |
"return true, if the receiver contains spaces only"
|
|
911 |
|
|
912 |
self do:[:char |
|
154
|
913 |
char ~~ Character space ifTrue:[^ false].
|
64
|
914 |
].
|
|
915 |
^ true
|
|
916 |
!
|
|
917 |
|
154
|
918 |
isAlphaNumeric
|
|
919 |
"return true, if the receiver is some alphanumeric word;
|
|
920 |
i.e. consists of a letter followed by letters or digits."
|
|
921 |
|
|
922 |
self size == 0 ifTrue:[
|
|
923 |
"mhmh what is this ?"
|
|
924 |
^ false
|
|
925 |
].
|
|
926 |
(self at:1) isLetter ifFalse:[^ false].
|
|
927 |
self do:[:char |
|
|
928 |
char isLetterOrDigit ifFalse:[^ false].
|
|
929 |
].
|
|
930 |
^ true
|
|
931 |
|
|
932 |
"
|
|
933 |
'helloWorld' isAlphaNumeric
|
|
934 |
'foo1234' isAlphaNumeric
|
|
935 |
'f1234' isAlphaNumeric
|
|
936 |
'1234' isAlphaNumeric
|
|
937 |
'+' isAlphaNumeric
|
|
938 |
"
|
|
939 |
!
|
|
940 |
|
64
|
941 |
countWords
|
|
942 |
"return the number of words, which are separated by separators"
|
|
943 |
|
92
|
944 |
|tally "{ Class: SmallInteger }"
|
|
945 |
start "{ Class: SmallInteger }"
|
|
946 |
mySize "{ Class: SmallInteger }"
|
|
947 |
stop ch|
|
64
|
948 |
|
|
949 |
tally := 0.
|
|
950 |
start := 1.
|
|
951 |
mySize := self size.
|
|
952 |
[start <= mySize] whileTrue:[
|
154
|
953 |
ch := self at:start.
|
|
954 |
ch isSeparator ifTrue:[
|
|
955 |
start := start + 1
|
|
956 |
] ifFalse:[
|
|
957 |
stop := self indexOfSeparatorStartingAt:start.
|
|
958 |
(stop == 0) ifTrue:[
|
|
959 |
stop := mySize + 1
|
|
960 |
].
|
|
961 |
tally := tally + 1.
|
|
962 |
start := stop
|
|
963 |
]
|
64
|
964 |
].
|
|
965 |
^ tally
|
|
966 |
|
92
|
967 |
"
|
|
968 |
'hello world isnt this nice' countWords'
|
|
969 |
"
|
64
|
970 |
!
|
|
971 |
|
154
|
972 |
numArgs
|
77
|
973 |
"treating the receiver as a message selector, return how many arguments would it take"
|
64
|
974 |
|
|
975 |
|binopChars|
|
|
976 |
|
|
977 |
(self size > 2) ifFalse:[
|
154
|
978 |
binopChars := '|&-+=*/\<>~@,'.
|
|
979 |
(self size == 1) ifTrue:[
|
|
980 |
((binopChars occurrencesOf:(self at:1)) == 0) ifTrue:[^ 0].
|
|
981 |
^ 1
|
|
982 |
].
|
|
983 |
((binopChars occurrencesOf:(self at:1)) == 0) ifFalse:[
|
|
984 |
((binopChars occurrencesOf:(self at:2)) == 0) ifFalse:[^ 1]
|
|
985 |
]
|
64
|
986 |
].
|
|
987 |
^ self occurrencesOf:$:
|
|
988 |
|
77
|
989 |
"
|
154
|
990 |
'foo:bar:' numArgs
|
|
991 |
#foo:bar: numArgs
|
|
992 |
'hello' numArgs
|
|
993 |
'+' numArgs
|
77
|
994 |
"
|
|
995 |
!
|
|
996 |
|
|
997 |
partsIfSelector
|
|
998 |
"treat the receiver as a message selector, return a collection of parts."
|
|
999 |
|
92
|
1000 |
|idx1 "{ Class: SmallInteger }"
|
|
1001 |
coll idx2 sz|
|
77
|
1002 |
|
|
1003 |
coll := OrderedCollection new.
|
|
1004 |
idx1 := 1.
|
|
1005 |
sz := self size.
|
|
1006 |
[true] whileTrue:[
|
154
|
1007 |
idx2 := self indexOf:$: startingAt:idx1 + 1.
|
|
1008 |
(idx2 == 0 or:[idx2 == sz]) ifTrue:[
|
|
1009 |
coll add:(self copyFrom:idx1).
|
|
1010 |
^ coll
|
|
1011 |
].
|
|
1012 |
coll add:(self copyFrom:idx1 to:idx2).
|
|
1013 |
idx1 := idx2 + 1
|
77
|
1014 |
].
|
|
1015 |
|
|
1016 |
"
|
|
1017 |
'foo:bar:' partsIfSelector
|
|
1018 |
#foo:bar: partsIfSelector
|
|
1019 |
'hello' partsIfSelector
|
|
1020 |
'+' partsIfSelector
|
|
1021 |
"
|
64
|
1022 |
!
|
|
1023 |
|
|
1024 |
levenshteinTo:aString
|
|
1025 |
"return the levenshtein distance to the argument, aString;
|
|
1026 |
this value corrensponds to the number of replacements that have to be
|
|
1027 |
made to get aString from the receiver.
|
|
1028 |
see IEEE transactions on Computers 1976 Pg 172 ff."
|
|
1029 |
|
|
1030 |
^ self levenshteinTo:aString s:4 c:1 i:2 d:6
|
154
|
1031 |
|
|
1032 |
"
|
|
1033 |
'ocmprt' levenshteinTo:'computer'
|
|
1034 |
'computer' levenshteinTo:'computer'
|
|
1035 |
'ocmputer' levenshteinTo:'computer'
|
|
1036 |
'cmputer' levenshteinTo:'computer'
|
|
1037 |
'Computer' levenshteinTo:'computer'
|
|
1038 |
"
|
64
|
1039 |
!
|
|
1040 |
|
|
1041 |
levenshteinTo:aString s:substWeight c:caseWeight i:insrtWeight d:deleteWeight
|
|
1042 |
"parametrized levenshtein. arguments are the costs for
|
|
1043 |
substitution, case-change, insertion and deletion of a character."
|
|
1044 |
|
|
1045 |
|d "delta matrix"
|
|
1046 |
len1 len2 dim prevRow row col dimPlus1
|
|
1047 |
min pp c1 c2|
|
|
1048 |
|
|
1049 |
len1 := self size.
|
|
1050 |
len2 := aString size.
|
|
1051 |
|
|
1052 |
"create the help-matrix"
|
|
1053 |
|
|
1054 |
dim := len1 max:len2.
|
|
1055 |
dimPlus1 := dim + 1.
|
|
1056 |
|
|
1057 |
d := Array new:dimPlus1.
|
|
1058 |
1 to:dimPlus1 do:[:i |
|
154
|
1059 |
d at:i put:(Array new:dimPlus1)
|
64
|
1060 |
].
|
|
1061 |
|
|
1062 |
"init help-matrix"
|
|
1063 |
|
|
1064 |
(d at:1) at:1 put:0.
|
|
1065 |
row := d at:1.
|
|
1066 |
1 to:dim do:[:j |
|
154
|
1067 |
row at:(j + 1) put:( (row at:j) + insrtWeight )
|
64
|
1068 |
].
|
|
1069 |
|
|
1070 |
1 to:dim do:[:i |
|
154
|
1071 |
(d at:(i + 1)) at:1 put:( ((d at:i) at:1) + deleteWeight )
|
64
|
1072 |
].
|
|
1073 |
|
|
1074 |
1 to:len1 do:[:i |
|
154
|
1075 |
c1 := self at:i.
|
|
1076 |
1 to:len2 do:[:j |
|
|
1077 |
c2 := aString at:j.
|
|
1078 |
(c1 == c2) ifTrue:[
|
|
1079 |
pp := 0
|
|
1080 |
] ifFalse:[
|
|
1081 |
(c1 asLowercase == c2 asLowercase) ifTrue:[
|
|
1082 |
pp := caseWeight
|
|
1083 |
] ifFalse:[
|
|
1084 |
pp := substWeight
|
|
1085 |
]
|
|
1086 |
].
|
|
1087 |
prevRow := d at:i.
|
|
1088 |
row := d at:(i + 1).
|
|
1089 |
col := j + 1.
|
|
1090 |
min := (prevRow at:j) + pp.
|
|
1091 |
min := min min:( (row at:j) + insrtWeight).
|
|
1092 |
min := min min:( (prevRow at:col) + deleteWeight).
|
|
1093 |
row at:col put: min
|
|
1094 |
]
|
64
|
1095 |
].
|
|
1096 |
|
|
1097 |
^ (d at:(len1 + 1)) at:(len2 + 1)
|
|
1098 |
!
|
|
1099 |
|
|
1100 |
spellAgainst: aString
|
|
1101 |
"return an integer between 0 and 100 indicating how similar
|
154
|
1102 |
the argument is to the receiver. No case conversion is done.
|
|
1103 |
This algorithm is much simpler (but also less exact) than the
|
|
1104 |
levenshtein distance. Experiment which is better for your
|
|
1105 |
application."
|
64
|
1106 |
|
|
1107 |
| i1 "{ Class: SmallInteger }"
|
|
1108 |
i2 "{ Class: SmallInteger }"
|
154
|
1109 |
next1 "{ Class: SmallInteger }"
|
|
1110 |
next2 "{ Class: SmallInteger }"
|
64
|
1111 |
size1 "{ Class: SmallInteger }"
|
|
1112 |
size2 "{ Class: SmallInteger }"
|
|
1113 |
score "{ Class: SmallInteger }"
|
|
1114 |
maxLen "{ Class: SmallInteger }" |
|
|
1115 |
|
|
1116 |
size1 := self size.
|
|
1117 |
size2 := aString size.
|
154
|
1118 |
maxLen := size1 max:size2.
|
64
|
1119 |
score := 0.
|
|
1120 |
i1 := i2 := 1.
|
|
1121 |
[i1 <= size1 and: [i2 <= size2]] whileTrue:[
|
154
|
1122 |
next1 := i1 + 1.
|
|
1123 |
next2 := i2 + 1.
|
|
1124 |
(self at:i1) == (aString at:i2) ifTrue: [
|
|
1125 |
score := score+1.
|
|
1126 |
i1 := next1.
|
|
1127 |
i2 := next2
|
|
1128 |
] ifFalse: [
|
|
1129 |
(i2 < size2 and: [(self at:i1) == (aString at:next2)]) ifTrue: [
|
|
1130 |
i2 := next2
|
|
1131 |
] ifFalse: [
|
|
1132 |
(i1 < size1 and: [(self at:next1) == (aString at:i2)]) ifTrue: [
|
|
1133 |
i1 := next1
|
|
1134 |
] ifFalse: [
|
|
1135 |
i1 := next1.
|
|
1136 |
i2 := next2
|
|
1137 |
]
|
|
1138 |
]
|
|
1139 |
]
|
64
|
1140 |
].
|
|
1141 |
|
154
|
1142 |
score = maxLen ifTrue: [^ 100].
|
|
1143 |
^ 100 * score // maxLen
|
64
|
1144 |
|
154
|
1145 |
"
|
|
1146 |
'Smalltalk' spellAgainst: 'Smalltlak'
|
|
1147 |
'Smalltalk' spellAgainst: 'smalltlak'
|
|
1148 |
'Smalltalk' spellAgainst: 'smalltalk'
|
|
1149 |
'Smalltalk' spellAgainst: 'smalltlk'
|
|
1150 |
'Smalltalk' spellAgainst: 'Smalltolk'
|
|
1151 |
"
|
64
|
1152 |
! !
|
|
1153 |
|
68
|
1154 |
!AbstractString methodsFor:'padded copying'!
|
|
1155 |
|
|
1156 |
paddedTo:newSize with:padCharacter
|
|
1157 |
"return a new string consisting of the receivers characters,
|
92
|
1158 |
plus pad characters up to length.
|
|
1159 |
If the receivers size is larger than the legth argument, it
|
|
1160 |
is returned unchanged."
|
68
|
1161 |
|
|
1162 |
|s len|
|
|
1163 |
|
|
1164 |
len := self size.
|
|
1165 |
len < newSize ifTrue:[
|
154
|
1166 |
s := self species new:newSize withAll:padCharacter.
|
|
1167 |
s replaceFrom:1 to:len with:self.
|
|
1168 |
^ s
|
68
|
1169 |
]
|
|
1170 |
|
92
|
1171 |
"
|
|
1172 |
'foo' paddedTo:10 with:$.
|
|
1173 |
123 printString paddedTo:10 with:$*
|
|
1174 |
(Float pi printString) paddedTo:15 with:(Character space)
|
|
1175 |
(Float pi printString) paddedTo:15 with:$-
|
|
1176 |
(Float pi class name , ' ') paddedTo:15 with:$.
|
|
1177 |
"
|
68
|
1178 |
!
|
|
1179 |
|
|
1180 |
leftPaddedTo:size with:padCharacter
|
|
1181 |
"return a new string of length size, which contains the receiver
|
|
1182 |
right-adjusted (i.e. padded on the left).
|
|
1183 |
characters on the left are filled with padCharacter.
|
|
1184 |
If the receivers size is larger than the legth argument, it
|
|
1185 |
is returned unchanged."
|
|
1186 |
|
|
1187 |
|len s|
|
|
1188 |
|
|
1189 |
len := self size.
|
|
1190 |
(len < size) ifTrue:[
|
154
|
1191 |
s := self species new:size withAll:padCharacter.
|
|
1192 |
s replaceFrom:(size - len + 1) with:self.
|
|
1193 |
^ s
|
68
|
1194 |
]
|
|
1195 |
|
92
|
1196 |
"
|
|
1197 |
'foo' leftPaddedTo:10 with:$.
|
|
1198 |
123 printString leftPaddedTo:10 with:$.
|
|
1199 |
(Float pi printString) leftPaddedTo:15 with:(Character space)
|
|
1200 |
(Float pi printString) leftPaddedTo:15 with:$-
|
|
1201 |
(' ' , Float pi class name) leftPaddedTo:15 with:$.
|
|
1202 |
"
|
68
|
1203 |
! !
|
|
1204 |
|
64
|
1205 |
!AbstractString methodsFor:'copying'!
|
|
1206 |
|
|
1207 |
concatenate:string1 and:string2
|
|
1208 |
"return the concatenation of myself and the arguments, string1 and string2.
|
|
1209 |
This is equivalent to self , string1 , string2
|
|
1210 |
- generated by compiler when such a construct is detected"
|
|
1211 |
|
|
1212 |
^ self , string1 , string2
|
|
1213 |
!
|
|
1214 |
|
|
1215 |
concatenate:string1 and:string2 and:string3
|
|
1216 |
"return the concatenation of myself and the string arguments.
|
|
1217 |
This is equivalent to self , string1 , string2 , string3
|
|
1218 |
- generated by compiler when such a construct is detected"
|
|
1219 |
|
|
1220 |
^ self , string1 , string2 , string3
|
|
1221 |
! !
|
|
1222 |
|
|
1223 |
!AbstractString methodsFor:'filling and replacing'!
|
|
1224 |
|
163
|
1225 |
withCRs
|
|
1226 |
"return a new string consisting of receivers characters
|
|
1227 |
with all \-characters replaced by cr-characters."
|
|
1228 |
|
|
1229 |
^ self copy replaceAll:$\ by:(Character cr)
|
|
1230 |
!
|
|
1231 |
|
|
1232 |
withoutCRs
|
|
1233 |
"return a new collection consisting of receivers elements
|
|
1234 |
with all cr-characters replaced by \-characters."
|
|
1235 |
|
|
1236 |
^ self copy replaceAll:(Character cr) by:$\
|
|
1237 |
!
|
|
1238 |
|
64
|
1239 |
withoutSpaces
|
92
|
1240 |
"return a copy of myself without leading and trailing spaces.
|
|
1241 |
Notice: this does NOT remove tabs, newline or any other whitespace.
|
|
1242 |
Use withoutSeparators for this."
|
64
|
1243 |
|
|
1244 |
|startIndex "{ Class: SmallInteger }"
|
|
1245 |
endIndex "{ Class: SmallInteger }"
|
|
1246 |
sz blank|
|
|
1247 |
|
|
1248 |
startIndex := 0.
|
|
1249 |
sz := self size.
|
|
1250 |
startIndex == 0 ifTrue:[
|
154
|
1251 |
startIndex := 1.
|
|
1252 |
endIndex := sz.
|
|
1253 |
blank := Character space.
|
|
1254 |
[(startIndex < endIndex) and:[(self at:startIndex) == blank]] whileTrue:[
|
|
1255 |
startIndex := startIndex + 1
|
|
1256 |
].
|
|
1257 |
[(endIndex > 1) and:[(self at:endIndex) == blank]] whileTrue:[
|
|
1258 |
endIndex := endIndex - 1
|
|
1259 |
]
|
64
|
1260 |
].
|
|
1261 |
startIndex > endIndex ifTrue:[
|
154
|
1262 |
^ ''
|
64
|
1263 |
].
|
|
1264 |
((startIndex == 1) and:[endIndex == sz]) ifTrue:[
|
154
|
1265 |
^ self
|
64
|
1266 |
].
|
|
1267 |
^ self copyFrom:startIndex to:endIndex
|
92
|
1268 |
|
|
1269 |
"
|
|
1270 |
' foo ' withoutSpaces
|
|
1271 |
(' foo' , Character tab asString , ' ') withoutSpaces inspect
|
|
1272 |
"
|
64
|
1273 |
!
|
|
1274 |
|
|
1275 |
withoutSeparators
|
92
|
1276 |
"return a copy of myself without leading and trailing whitespace.
|
|
1277 |
Whitespace is space, tab, newline, formfeed.
|
|
1278 |
Use withoutSpaces, if you want to remove spaces only."
|
64
|
1279 |
|
|
1280 |
|startIndex "{ Class: SmallInteger }"
|
|
1281 |
endIndex "{ Class: SmallInteger }"
|
|
1282 |
sz|
|
|
1283 |
|
|
1284 |
startIndex := 0.
|
|
1285 |
sz := self size.
|
|
1286 |
startIndex == 0 ifTrue:[
|
154
|
1287 |
startIndex := 1.
|
|
1288 |
endIndex := self size.
|
|
1289 |
[(startIndex < endIndex) and:[(self at:startIndex) isSeparator]] whileTrue:[
|
|
1290 |
startIndex := startIndex + 1
|
|
1291 |
].
|
|
1292 |
[(endIndex > 1) and:[(self at:endIndex) isSeparator]] whileTrue:[
|
|
1293 |
endIndex := endIndex - 1
|
|
1294 |
].
|
64
|
1295 |
].
|
|
1296 |
startIndex > endIndex ifTrue:[
|
154
|
1297 |
^ ''
|
64
|
1298 |
].
|
|
1299 |
((startIndex == 1) and:[endIndex == sz]) ifTrue:[
|
154
|
1300 |
^ self
|
64
|
1301 |
].
|
|
1302 |
^ self copyFrom:startIndex to:endIndex
|
92
|
1303 |
|
|
1304 |
"
|
|
1305 |
' foo ' withoutSeparators
|
|
1306 |
(' foo' , Character tab asString , ' ') withoutSeparators inspect
|
|
1307 |
"
|
154
|
1308 |
!
|
|
1309 |
|
|
1310 |
withTabs
|
|
1311 |
"return a copy of the receiver where leading spaces are
|
|
1312 |
replaced by tabulator characters (assuming 8-col tabs)"
|
|
1313 |
|
|
1314 |
|idx nTabs newString|
|
|
1315 |
|
|
1316 |
idx := self findFirst:[:c | (c ~~ Character space)].
|
|
1317 |
nTabs := (idx-1) // 8.
|
|
1318 |
nTabs == 0 ifTrue:[^ self].
|
|
1319 |
|
|
1320 |
"any tabs"
|
|
1321 |
newString := self class new:(self size - (nTabs * 7)).
|
|
1322 |
newString atAll:(1 to:nTabs) put:(Character tab).
|
|
1323 |
newString replaceFrom:(nTabs + 1) with:self startingAt:(nTabs * 8 + 1).
|
|
1324 |
^ newString
|
|
1325 |
|
|
1326 |
"
|
|
1327 |
'12345678901234567890' withTabs
|
|
1328 |
' 8901234567890' withTabs
|
|
1329 |
' 901234567890' withTabs
|
|
1330 |
' 67890' withTabs
|
|
1331 |
' 7890' withTabs
|
|
1332 |
' 890' withTabs
|
|
1333 |
"
|
64
|
1334 |
! !
|
154
|
1335 |
|
64
|
1336 |
!AbstractString methodsFor:'queries'!
|
|
1337 |
|
|
1338 |
isString
|
|
1339 |
"return true, if the receiver is some kind of string;
|
|
1340 |
true is returned here - redefinition of Object>>isString."
|
|
1341 |
|
|
1342 |
^ true
|
|
1343 |
!
|
|
1344 |
|
|
1345 |
encoding
|
|
1346 |
^ #unknown
|
|
1347 |
! !
|