64
|
1 |
"
|
|
2 |
COPYRIGHT (c) 1994 by Claus Gittinger
|
|
3 |
All Rights Reserved
|
|
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
|
82
|
22 |
All Rights Reserved
|
64
|
23 |
'!
|
|
24 |
|
|
25 |
!AbstractString class methodsFor:'documentation'!
|
|
26 |
|
82
|
27 |
copyright
|
|
28 |
"
|
|
29 |
COPYRIGHT (c) 1994 by Claus Gittinger
|
|
30 |
All Rights Reserved
|
|
31 |
|
|
32 |
This software is furnished under a license and may be used
|
|
33 |
only in accordance with the terms of that license and with the
|
|
34 |
inclusion of the above copyright notice. This software may not
|
|
35 |
be provided or otherwise made available to, or used by, any
|
|
36 |
other person. No title to or ownership of the software is
|
|
37 |
hereby transferred.
|
|
38 |
"
|
|
39 |
!
|
|
40 |
|
|
41 |
version
|
|
42 |
"
|
|
43 |
$Header: /cvs/stx/stx/libbasic/Attic/CharArray.st,v 1.4 1994-06-02 11:18:12 claus Exp $
|
|
44 |
"
|
|
45 |
!
|
|
46 |
|
64
|
47 |
documentation
|
|
48 |
"
|
68
|
49 |
AbstractString is a superclass for all kinds of Strings (i.e.
|
|
50 |
(singleByte-)Strings, TwoByteStrings and whatever comes in the future.
|
64
|
51 |
"
|
|
52 |
! !
|
|
53 |
|
|
54 |
!AbstractString class methodsFor:'instance creation'!
|
|
55 |
|
|
56 |
basicNew
|
|
57 |
"return a new empty string"
|
|
58 |
|
|
59 |
^ self basicNew:0
|
|
60 |
!
|
|
61 |
|
|
62 |
new
|
|
63 |
"return a new empty string"
|
|
64 |
|
|
65 |
^ self basicNew:0
|
|
66 |
!
|
|
67 |
|
|
68 |
fromString:aString
|
|
69 |
"return a copy of the argument, aString"
|
|
70 |
|
68
|
71 |
^ (self basicNew:(aString size)) replaceFrom:1 with:aString
|
|
72 |
|
|
73 |
"TwoByteString fromString:'hello'"
|
64
|
74 |
! !
|
|
75 |
|
|
76 |
!AbstractString methodsFor:'converting'!
|
|
77 |
|
|
78 |
asUppercase
|
|
79 |
"return a copy of myself in uppercase letters"
|
|
80 |
|
|
81 |
|newStr
|
|
82 |
mySize "{ Class: SmallInteger }" |
|
|
83 |
|
|
84 |
mySize := self size.
|
|
85 |
newStr := self species new:mySize.
|
|
86 |
1 to:mySize do:[:i |
|
|
87 |
newStr at:i put:(self at:i) asUppercase
|
|
88 |
].
|
|
89 |
^newStr
|
|
90 |
!
|
|
91 |
|
|
92 |
asLowercase
|
|
93 |
"return a copy of myself in lowercase letters"
|
|
94 |
|
|
95 |
|newStr
|
|
96 |
mySize "{ Class: SmallInteger }" |
|
|
97 |
|
|
98 |
mySize := self size.
|
|
99 |
newStr := self species new:mySize.
|
|
100 |
1 to:mySize do:[:i |
|
|
101 |
newStr at:i put:(self at:i) asLowercase
|
|
102 |
].
|
|
103 |
^newStr
|
|
104 |
!
|
|
105 |
|
|
106 |
asString
|
|
107 |
"return myself - I am a string"
|
|
108 |
|
|
109 |
^ self
|
|
110 |
!
|
|
111 |
|
68
|
112 |
asTwoByteString
|
|
113 |
"return the receiver converted to a two-byte string"
|
|
114 |
|
|
115 |
^ TwoByteString fromString:self
|
|
116 |
!
|
|
117 |
|
|
118 |
asSingleByteString
|
|
119 |
"return the receiver converted to a 'normal' string"
|
|
120 |
|
|
121 |
^ String fromString:self
|
|
122 |
!
|
|
123 |
|
64
|
124 |
asText
|
68
|
125 |
"return a Text-object (collection of lines) from myself.
|
|
126 |
BIG warning: Text is totally misnamed here
|
|
127 |
- ST/X's Text has nothing to do with PP's Text.
|
82
|
128 |
Therefore it will be removed/renamed soon."
|
64
|
129 |
|
|
130 |
^ Text from:self
|
|
131 |
!
|
|
132 |
|
|
133 |
asNumber
|
|
134 |
"read a number from the receiver"
|
|
135 |
|
|
136 |
^ Number readFromString:self
|
|
137 |
|
|
138 |
"'123' asNumber"
|
|
139 |
"'123.567' asNumber"
|
|
140 |
"'(5/6)' asNumber"
|
|
141 |
!
|
|
142 |
|
|
143 |
asFilename
|
|
144 |
"return a Filename with pathname taken from the receiver"
|
|
145 |
|
|
146 |
^ Filename named:self
|
|
147 |
!
|
|
148 |
|
|
149 |
asCollectionOfWords
|
|
150 |
"return a collection containing the words (separated by whitespace)
|
|
151 |
of the receiver"
|
|
152 |
|
68
|
153 |
|words ch
|
|
154 |
start "{ Class:SmallInteger }"
|
|
155 |
stop "{ Class:SmallInteger }"
|
|
156 |
mySize "{ Class:SmallInteger }"|
|
64
|
157 |
|
|
158 |
words := OrderedCollection new.
|
|
159 |
start := 1.
|
|
160 |
mySize := self size.
|
|
161 |
[start <= mySize] whileTrue:[
|
|
162 |
ch := self at:start.
|
|
163 |
((ch == Character space) or:[ch isSeparator]) ifTrue:[
|
|
164 |
start := start + 1
|
|
165 |
] ifFalse:[
|
|
166 |
stop := self indexOfSeparatorStartingAt:start.
|
|
167 |
stop == 0 ifTrue:[
|
|
168 |
words add:(self copyFrom:start to:mySize).
|
|
169 |
^ words
|
|
170 |
].
|
|
171 |
words add:(self copyFrom:start to:(stop - 1)).
|
|
172 |
start := stop
|
|
173 |
]
|
|
174 |
].
|
|
175 |
^ words
|
|
176 |
|
|
177 |
"'hello world isnt this nice' asCollectionOfWords'"
|
68
|
178 |
!
|
|
179 |
|
|
180 |
asCollectionOfLines
|
|
181 |
"return a collection containing the lines (separated by cr)
|
|
182 |
of the receiver."
|
|
183 |
|
|
184 |
|lines myClass
|
|
185 |
numberOfLines "{ Class:SmallInteger }"
|
|
186 |
startIndex "{ Class:SmallInteger }"
|
|
187 |
stopIndex "{ Class:SmallInteger }" |
|
|
188 |
|
|
189 |
"count first, to avoid regrowing"
|
|
190 |
|
|
191 |
numberOfLines := (self occurrencesOf:Character cr) + 1.
|
|
192 |
lines := OrderedCollection new:numberOfLines.
|
|
193 |
myClass := self species.
|
|
194 |
|
|
195 |
startIndex := 1.
|
|
196 |
1 to:numberOfLines do:[:lineNr |
|
|
197 |
stopIndex := self indexOf:(Character cr) startingAt:startIndex.
|
|
198 |
stopIndex == 0 ifTrue:[
|
|
199 |
stopIndex := self size
|
|
200 |
] ifFalse: [
|
|
201 |
stopIndex := stopIndex - 1.
|
|
202 |
].
|
|
203 |
|
|
204 |
(stopIndex < startIndex) ifTrue: [
|
|
205 |
lines add:(myClass new:0)
|
|
206 |
] ifFalse: [
|
|
207 |
lines add:(self copyFrom:startIndex to:stopIndex)
|
|
208 |
].
|
|
209 |
startIndex := stopIndex + 2
|
|
210 |
].
|
|
211 |
^ lines
|
|
212 |
|
|
213 |
"
|
|
214 |
'1 one\2 two\3 three\4 four\5 five' withCRs asCollectionOfLines
|
|
215 |
'1 one\2 two\3 three\4 four\5 five' withCRs asCollectionOfWords
|
|
216 |
"
|
64
|
217 |
! !
|
|
218 |
|
|
219 |
!AbstractString methodsFor:'printing & storing'!
|
|
220 |
|
82
|
221 |
article
|
|
222 |
"return an article string"
|
|
223 |
|
|
224 |
|firstChar|
|
|
225 |
|
|
226 |
firstChar := (self at:1) asLowercase.
|
|
227 |
(firstChar isVowel or:[firstChar == $x]) ifTrue:[
|
|
228 |
^ 'an'
|
|
229 |
].
|
|
230 |
^ 'a'
|
|
231 |
!
|
|
232 |
|
64
|
233 |
printOn:aStream
|
|
234 |
"print the receiver on aStream"
|
|
235 |
|
|
236 |
aStream nextPutAll:self
|
|
237 |
!
|
|
238 |
|
|
239 |
printString
|
|
240 |
"return a string for printing - thats myself"
|
|
241 |
|
|
242 |
^ self
|
|
243 |
!
|
|
244 |
|
|
245 |
displayString
|
|
246 |
"return a string to display the receiver - use storeString to have
|
|
247 |
quotes around"
|
|
248 |
|
|
249 |
^ self storeString
|
|
250 |
! !
|
|
251 |
|
|
252 |
!AbstractString methodsFor:'comparing'!
|
|
253 |
|
|
254 |
hash
|
|
255 |
"return an integer useful as a hash-key"
|
|
256 |
|
|
257 |
%{ /* NOCONTEXT */
|
|
258 |
|
|
259 |
REGISTER int g, val;
|
|
260 |
REGISTER unsigned char *cp, *cp0;
|
|
261 |
int l;
|
|
262 |
|
|
263 |
cp = _stringVal(self);
|
|
264 |
l = _stringSize(self);
|
|
265 |
if (_qClass(self) != String) {
|
|
266 |
int n = _intVal(_ClassInstPtr(_qClass(self))->c_ninstvars) * sizeof(OBJ);
|
|
267 |
|
|
268 |
cp += n;
|
|
269 |
l -= n;
|
|
270 |
}
|
|
271 |
|
|
272 |
/*
|
|
273 |
* this is the dragon-book algorithm with a funny start
|
|
274 |
* value (to give short strings a number above 8192)
|
|
275 |
*/
|
|
276 |
val = 12345;
|
|
277 |
for (cp0 = cp, cp += l - 1; cp >= cp0; cp--) {
|
|
278 |
val = (val << 5) + (*cp & 0x1F);
|
|
279 |
if (g = (val & 0x3E000000))
|
|
280 |
val ^= g >> 25 /* 23 */ /* 25 */;
|
|
281 |
val &= 0x3FFFFFFF;
|
|
282 |
}
|
|
283 |
|
|
284 |
if (l) {
|
|
285 |
l |= 1;
|
|
286 |
val = (val * l) & 0x3FFFFFFF;
|
|
287 |
}
|
|
288 |
|
|
289 |
RETURN ( _MKSMALLINT(val) );
|
|
290 |
%}
|
|
291 |
!
|
|
292 |
|
|
293 |
<= something
|
|
294 |
"Compare the receiver with the argument and return true if the
|
|
295 |
receiver is less than or equal to the argument. Otherwise return false."
|
|
296 |
|
|
297 |
^ (self > something) not
|
|
298 |
!
|
|
299 |
|
|
300 |
< something
|
|
301 |
"Compare the receiver with the argument and return true if the
|
|
302 |
receiver is less than the argument. Otherwise return false."
|
|
303 |
|
|
304 |
^ (something > self)
|
|
305 |
!
|
|
306 |
|
|
307 |
>= something
|
|
308 |
"Compare the receiver with the argument and return true if the
|
|
309 |
receiver is greater than or equal to the argument.
|
|
310 |
Otherwise return false."
|
|
311 |
|
|
312 |
^ (something > self) not
|
|
313 |
!
|
|
314 |
|
|
315 |
> aString
|
|
316 |
"Compare the receiver with the argument and return true if the
|
|
317 |
receiver is greater than the argument. Otherwise return false.
|
|
318 |
In contrast to ST-80, case differences are NOT ignored, thus
|
|
319 |
'foo' > 'Foo' will return true.
|
|
320 |
This may change."
|
|
321 |
|
|
322 |
|mySize otherSize c1 c2|
|
|
323 |
|
|
324 |
mySize := self size.
|
|
325 |
otherSize := aString size.
|
|
326 |
|
|
327 |
1 to:(mySize min:otherSize) do:[:index |
|
|
328 |
c1 := self at:index.
|
|
329 |
c2 := aString at:index.
|
|
330 |
c1 > c2 ifTrue:[^ true].
|
|
331 |
c1 < c2 ifTrue:[^ false].
|
|
332 |
].
|
|
333 |
^ mySize > otherSize
|
|
334 |
!
|
|
335 |
|
|
336 |
= aString
|
|
337 |
"Compare the receiver with the argument and return true if the
|
|
338 |
receiver is equal to the argument. Otherwise return false."
|
|
339 |
|
|
340 |
|mySize otherSize|
|
|
341 |
|
|
342 |
mySize := self size.
|
|
343 |
otherSize := aString size.
|
|
344 |
mySize == otherSize ifFalse:[^ false].
|
|
345 |
|
|
346 |
1 to:mySize do:[:index |
|
|
347 |
(self at:index) = (aString at:index) ifFalse:[^ false].
|
|
348 |
].
|
|
349 |
^ true
|
|
350 |
!
|
|
351 |
|
|
352 |
sameAs:aString
|
|
353 |
"Compare the receiver with the argument like =, but ignore
|
|
354 |
case differences. Return true or false"
|
|
355 |
|
|
356 |
|mySize otherSize|
|
|
357 |
|
|
358 |
mySize := self size.
|
|
359 |
otherSize := aString size.
|
|
360 |
mySize == otherSize ifFalse:[^ false].
|
|
361 |
|
|
362 |
1 to:mySize do:[:index |
|
|
363 |
(self at:index) asLowercase = (aString at:index) asLowercase ifFalse:[^ false].
|
|
364 |
].
|
|
365 |
^ true
|
|
366 |
! !
|
|
367 |
|
|
368 |
!AbstractString methodsFor:'character searching'!
|
|
369 |
|
|
370 |
includesMatchCharacters
|
|
371 |
"return true if the receiver includes any meta characters (i.e. $* or $#)
|
|
372 |
for match operations; false if not"
|
|
373 |
|
|
374 |
^ self includesAny:'*#['
|
|
375 |
!
|
|
376 |
|
68
|
377 |
indexOfSeparator
|
|
378 |
"return the index of the first whitespace character"
|
|
379 |
|
|
380 |
^ self indexOfSeparatorStartingAt:1
|
|
381 |
|
|
382 |
"'hello world' indexOfSeparator"
|
|
383 |
!
|
|
384 |
|
64
|
385 |
indexOfSeparatorStartingAt:start
|
68
|
386 |
"return the index of the next whitespace character"
|
64
|
387 |
|
|
388 |
start to:self size do:[:index |
|
|
389 |
(self at:index) isSeparator ifTrue:[^ index]
|
|
390 |
].
|
|
391 |
^ 0
|
|
392 |
|
|
393 |
"'hello world' indexOfSeparatorStartingAt:3"
|
|
394 |
! !
|
|
395 |
|
|
396 |
!AbstractString methodsFor:'substring searching'!
|
|
397 |
|
|
398 |
findString:subString
|
|
399 |
"find a substring. if found, return the index;
|
|
400 |
if not found, return 0"
|
|
401 |
|
|
402 |
^ self findString:subString startingAt:1 ifAbsent:[0]
|
|
403 |
|
|
404 |
"'hello world' findString:'llo'"
|
|
405 |
"'hello world' findString:'ole'"
|
|
406 |
!
|
|
407 |
|
|
408 |
findString:subString startingAt:index
|
|
409 |
"find a substring, starting at index. if found, return the index;
|
|
410 |
if not found, return 0"
|
|
411 |
|
|
412 |
^ self findString:subString startingAt:index ifAbsent:[0]
|
|
413 |
|
|
414 |
"'hello yello' findString:'llo' startingAt:1"
|
|
415 |
"'hello yello' findString:'llo' startingAt:5"
|
|
416 |
"'hello yello' findString:'llo' startingAt:15"
|
|
417 |
!
|
|
418 |
|
|
419 |
findString:subString startingAt:index ifAbsent:exceptionBlock
|
|
420 |
"find a substring, starting at index. if found, return the index;
|
|
421 |
if not found, return the result of evaluating exceptionBlock.
|
|
422 |
This is a q&d hack - not very efficient"
|
|
423 |
|
|
424 |
|firstChar found
|
|
425 |
startIndex "{ Class: SmallInteger }"
|
|
426 |
subSize "{ Class: SmallInteger }"
|
|
427 |
mySize "{ Class: SmallInteger }"
|
|
428 |
runIdx "{ Class: SmallInteger }" |
|
|
429 |
|
|
430 |
subSize := subString size.
|
|
431 |
subSize == 0 ifTrue:[^ index]. "empty string matches"
|
|
432 |
mySize := self size.
|
|
433 |
firstChar := subString at:1.
|
|
434 |
startIndex := self indexOf:firstChar startingAt:index.
|
|
435 |
[startIndex == 0] whileFalse:[
|
|
436 |
runIdx := startIndex.
|
|
437 |
found := true.
|
|
438 |
1 to:subSize do:[:i |
|
|
439 |
runIdx > mySize ifTrue:[
|
|
440 |
found := false
|
|
441 |
] ifFalse:[
|
|
442 |
(subString at:i) ~~ (self at:runIdx) ifTrue:[
|
|
443 |
found := false
|
|
444 |
]
|
|
445 |
].
|
|
446 |
runIdx := runIdx + 1
|
|
447 |
].
|
|
448 |
found ifTrue:[
|
|
449 |
^ startIndex
|
|
450 |
].
|
|
451 |
startIndex := self indexOf:firstChar startingAt:(startIndex + 1)
|
|
452 |
].
|
|
453 |
^ exceptionBlock value
|
|
454 |
! !
|
|
455 |
|
|
456 |
!AbstractString methodsFor:'pattern matching'!
|
|
457 |
|
|
458 |
from:matchStart to:matchStop match:aString from:start to:stop
|
|
459 |
"helper for match; return true if the characters from start to stop in
|
|
460 |
aString are matching the receivers characters from matchStart to matchStop.
|
|
461 |
The receiver may contain meta-match characters $* (to match any string)
|
|
462 |
or $# (to match any character) or [...] (to match from a set of characters)."
|
|
463 |
|
|
464 |
|matchChar
|
|
465 |
mStart "{ Class: SmallInteger }"
|
|
466 |
mStop "{ Class: SmallInteger }"
|
|
467 |
sStart "{ Class: SmallInteger }"
|
|
468 |
sStop "{ Class: SmallInteger }"
|
|
469 |
mSize "{ Class: SmallInteger }"
|
|
470 |
sSize "{ Class: SmallInteger }"
|
|
471 |
index cont matchLast
|
|
472 |
matchSet c1 c2|
|
|
473 |
|
|
474 |
mStart := matchStart.
|
|
475 |
mStop := matchStop.
|
|
476 |
sStart := start.
|
|
477 |
sStop := stop.
|
|
478 |
|
|
479 |
[true] whileTrue:[
|
|
480 |
mSize := mStop - mStart + 1.
|
|
481 |
sSize := sStop - sStart + 1.
|
|
482 |
|
|
483 |
"empty strings match"
|
|
484 |
(mSize == 0) ifTrue:[^ (sSize == 0)].
|
|
485 |
|
|
486 |
matchChar := self at:mStart.
|
|
487 |
|
|
488 |
(matchChar == $#) ifTrue:[
|
|
489 |
"testString empty -> no match"
|
|
490 |
(sSize == 0) ifTrue:[^ false].
|
|
491 |
"# matches single character"
|
|
492 |
((sSize == 1) and:[mSize == 1]) ifTrue:[^ true].
|
|
493 |
"cut off 1st chars and continue"
|
|
494 |
mStart := mStart + 1.
|
|
495 |
sStart := sStart + 1
|
|
496 |
] ifFalse:[
|
|
497 |
(matchChar == $[) ifTrue:[
|
|
498 |
"testString empty -> no match"
|
|
499 |
(sSize == 0) ifTrue:[^ false].
|
|
500 |
|
|
501 |
matchSet := Set new.
|
|
502 |
c1 := nil.
|
|
503 |
[matchChar == $]] whileFalse:[
|
|
504 |
mStart := mStart + 1.
|
|
505 |
mSize := mSize - 1.
|
|
506 |
matchChar := self at:mStart.
|
|
507 |
((matchChar == $-) and:[c1 notNil]) ifTrue:[
|
|
508 |
mStart := mStart + 1.
|
|
509 |
mSize := mSize - 1.
|
|
510 |
c2 := self at:mStart.
|
|
511 |
(c1 to:c2) do:[:c | matchSet add:c].
|
|
512 |
c1 := nil
|
|
513 |
] ifFalse:[
|
|
514 |
(matchChar == $]) ifFalse:[
|
|
515 |
matchSet add:matchChar.
|
|
516 |
c1 := matchChar.
|
|
517 |
].
|
|
518 |
].
|
|
519 |
].
|
|
520 |
mStart := mStart + 1.
|
|
521 |
mSize := mSize - 1.
|
|
522 |
(matchSet includes:(aString at:sStart)) ifFalse:[^ false].
|
|
523 |
((sSize == 1) and:[mSize == 0]) ifTrue:[^ true].
|
|
524 |
"cut off 1st char and continue"
|
|
525 |
sStart := sStart + 1
|
|
526 |
] ifFalse:[
|
|
527 |
(matchChar == $*) ifTrue:[
|
|
528 |
"* matches anything"
|
|
529 |
(mSize == 1) ifTrue:[^ true].
|
|
530 |
"testString empty -> matchString not we have no match"
|
|
531 |
(sSize == 0) ifTrue:[^ false].
|
|
532 |
|
|
533 |
"try to avoid some of the recursion by checking last
|
|
534 |
character and continue with shortened strings if possible"
|
|
535 |
cont := false.
|
|
536 |
(mStop >= mStart) ifTrue:[
|
|
537 |
matchLast := self at:mStop.
|
|
538 |
(matchLast ~~ $*) ifTrue:[
|
|
539 |
(matchLast == $#) ifTrue:[
|
|
540 |
cont := true
|
|
541 |
] ifFalse:[
|
|
542 |
(matchLast == (aString at:sStop)) ifTrue:[
|
|
543 |
cont := true
|
|
544 |
]
|
|
545 |
]
|
|
546 |
]
|
|
547 |
].
|
|
548 |
cont ifFalse:[
|
|
549 |
index := sStart.
|
|
550 |
[index <= sStop] whileTrue:[
|
|
551 |
(self from:(mStart + 1) to:mStop match:aString
|
|
552 |
from:index to:sStop) ifTrue:[
|
|
553 |
^ true
|
|
554 |
].
|
|
555 |
index := index + 1
|
|
556 |
].
|
|
557 |
^ false
|
|
558 |
].
|
|
559 |
mStop := mStop - 1.
|
|
560 |
sStop := sStop - 1
|
|
561 |
] ifFalse:[
|
|
562 |
|
|
563 |
"testString empty ?"
|
|
564 |
(sSize == 0) ifTrue:[^ false].
|
|
565 |
|
|
566 |
"first characters equal ?"
|
|
567 |
((aString at:sStart) ~~ matchChar) ifTrue:[^ false].
|
|
568 |
|
|
569 |
"avoid recursion if possible"
|
|
570 |
((sSize == mSize) and:[self = aString]) ifTrue:[^ true].
|
|
571 |
|
|
572 |
"cut off 1st chars and continue"
|
|
573 |
mStart := mStart + 1.
|
|
574 |
sStart := sStart + 1
|
|
575 |
]
|
|
576 |
]
|
|
577 |
]
|
|
578 |
]
|
|
579 |
!
|
|
580 |
|
|
581 |
match:aString
|
|
582 |
"return true if aString matches self, where self may contain meta-match
|
|
583 |
characters $* (to match any string) or $# (to match any character)."
|
|
584 |
|
|
585 |
^ self from:1 to:(self size) match:aString from:1 to:(aString size)
|
|
586 |
|
|
587 |
" '*ute*' match:'computer' "
|
|
588 |
" '*uter' match:'computer' "
|
|
589 |
" 'uter*' match:'computer' "
|
|
590 |
" '*ute*' match:'' "
|
|
591 |
" '[abcd]*' match:'computer' "
|
|
592 |
" '[abcd]*' match:'komputer' "
|
|
593 |
! !
|
|
594 |
|
|
595 |
!AbstractString methodsFor:'testing'!
|
|
596 |
|
|
597 |
isBlank
|
|
598 |
"return true, if the receiver contains spaces only"
|
|
599 |
|
|
600 |
self do:[:char |
|
|
601 |
char ~~ Character space ifTrue:[^ false].
|
|
602 |
].
|
|
603 |
^ true
|
|
604 |
!
|
|
605 |
|
|
606 |
countWords
|
|
607 |
"return the number of words, which are separated by separators"
|
|
608 |
|
|
609 |
|tally start stop mySize ch|
|
|
610 |
|
|
611 |
tally := 0.
|
|
612 |
start := 1.
|
|
613 |
mySize := self size.
|
|
614 |
[start <= mySize] whileTrue:[
|
|
615 |
ch := self at:start.
|
|
616 |
ch isSeparator ifTrue:[
|
|
617 |
start := start + 1
|
|
618 |
] ifFalse:[
|
|
619 |
stop := self indexOfSeparatorStartingAt:start.
|
|
620 |
(stop == 0) ifTrue:[
|
|
621 |
stop := mySize + 1
|
|
622 |
].
|
|
623 |
tally := tally + 1.
|
|
624 |
start := stop
|
|
625 |
]
|
|
626 |
].
|
|
627 |
^ tally
|
|
628 |
|
|
629 |
"'hello world isnt this nice' countWords'"
|
|
630 |
!
|
|
631 |
|
|
632 |
nArgsIfSelector
|
77
|
633 |
"treating the receiver as a message selector, return how many arguments would it take"
|
64
|
634 |
|
|
635 |
|binopChars|
|
|
636 |
|
|
637 |
(self size > 2) ifFalse:[
|
|
638 |
binopChars := '|&-+=*/\<>~@,'.
|
|
639 |
(self size == 1) ifTrue:[
|
|
640 |
((binopChars occurrencesOf:(self at:1)) == 0) ifTrue:[^ 0].
|
|
641 |
^ 1
|
|
642 |
].
|
|
643 |
((binopChars occurrencesOf:(self at:1)) == 0) ifFalse:[
|
|
644 |
((binopChars occurrencesOf:(self at:2)) == 0) ifFalse:[^ 1]
|
|
645 |
]
|
|
646 |
].
|
|
647 |
^ self occurrencesOf:$:
|
|
648 |
|
77
|
649 |
"
|
|
650 |
'foo:bar:' nArgsIfSelector
|
|
651 |
#foo:bar: nArgsIfSelector
|
|
652 |
'hello' nArgsIfSelector
|
|
653 |
'+' nArgsIfSelector
|
|
654 |
"
|
|
655 |
!
|
|
656 |
|
|
657 |
partsIfSelector
|
|
658 |
"treat the receiver as a message selector, return a collection of parts."
|
|
659 |
|
|
660 |
|coll idx1 idx2 sz|
|
|
661 |
|
|
662 |
coll := OrderedCollection new.
|
|
663 |
idx1 := 1.
|
|
664 |
sz := self size.
|
|
665 |
[true] whileTrue:[
|
|
666 |
idx2 := self indexOf:$: startingAt:idx1 + 1.
|
|
667 |
(idx2 == 0 or:[idx2 == sz]) ifTrue:[
|
|
668 |
coll add:(self copyFrom:idx1).
|
|
669 |
^ coll
|
|
670 |
].
|
|
671 |
coll add:(self copyFrom:idx1 to:idx2).
|
|
672 |
idx1 := idx2 + 1
|
|
673 |
].
|
|
674 |
|
|
675 |
"
|
|
676 |
'foo:bar:' partsIfSelector
|
|
677 |
#foo:bar: partsIfSelector
|
|
678 |
'hello' partsIfSelector
|
|
679 |
'+' partsIfSelector
|
|
680 |
"
|
64
|
681 |
!
|
|
682 |
|
|
683 |
levenshteinTo:aString
|
|
684 |
"return the levenshtein distance to the argument, aString;
|
|
685 |
this value corrensponds to the number of replacements that have to be
|
|
686 |
made to get aString from the receiver.
|
|
687 |
see IEEE transactions on Computers 1976 Pg 172 ff."
|
|
688 |
|
|
689 |
^ self levenshteinTo:aString s:4 c:1 i:2 d:6
|
|
690 |
!
|
|
691 |
|
|
692 |
levenshteinTo:aString s:substWeight c:caseWeight i:insrtWeight d:deleteWeight
|
|
693 |
"parametrized levenshtein. arguments are the costs for
|
|
694 |
substitution, case-change, insertion and deletion of a character."
|
|
695 |
|
|
696 |
|d "delta matrix"
|
|
697 |
len1 len2 dim prevRow row col dimPlus1
|
|
698 |
min pp c1 c2|
|
|
699 |
|
|
700 |
len1 := self size.
|
|
701 |
len2 := aString size.
|
|
702 |
|
|
703 |
"create the help-matrix"
|
|
704 |
|
|
705 |
dim := len1 max:len2.
|
|
706 |
dimPlus1 := dim + 1.
|
|
707 |
|
|
708 |
d := Array new:dimPlus1.
|
|
709 |
1 to:dimPlus1 do:[:i |
|
|
710 |
d at:i put:(Array new:dimPlus1)
|
|
711 |
].
|
|
712 |
|
|
713 |
"init help-matrix"
|
|
714 |
|
|
715 |
(d at:1) at:1 put:0.
|
|
716 |
row := d at:1.
|
|
717 |
1 to:dim do:[:j |
|
|
718 |
row at:(j + 1) put:( (row at:j) + insrtWeight )
|
|
719 |
].
|
|
720 |
|
|
721 |
1 to:dim do:[:i |
|
|
722 |
(d at:(i + 1)) at:1 put:( ((d at:i) at:1) + deleteWeight )
|
|
723 |
].
|
|
724 |
|
|
725 |
1 to:len1 do:[:i |
|
|
726 |
c1 := self at:i.
|
|
727 |
1 to:len2 do:[:j |
|
|
728 |
c2 := aString at:j.
|
|
729 |
(c1 == c2) ifTrue:[
|
|
730 |
pp := 0
|
|
731 |
] ifFalse:[
|
|
732 |
(c1 asLowercase == c2 asLowercase) ifTrue:[
|
|
733 |
pp := caseWeight
|
|
734 |
] ifFalse:[
|
|
735 |
pp := substWeight
|
|
736 |
]
|
|
737 |
].
|
|
738 |
prevRow := d at:i.
|
|
739 |
row := d at:(i + 1).
|
|
740 |
col := j + 1.
|
|
741 |
min := (prevRow at:j) + pp.
|
|
742 |
min := min min:( (row at:j) + insrtWeight).
|
|
743 |
min := min min:( (prevRow at:col) + deleteWeight).
|
|
744 |
row at:col put: min
|
|
745 |
]
|
|
746 |
].
|
|
747 |
|
|
748 |
^ (d at:(len1 + 1)) at:(len2 + 1)
|
|
749 |
|
|
750 |
"'ocmprt' levenshteinTo:'computer'
|
|
751 |
'computer' levenshteinTo:'computer'
|
|
752 |
'ocmputer' levenshteinTo:'computer'
|
|
753 |
'cmputer' levenshteinTo:'computer'
|
|
754 |
'Computer' levenshteinTo:'computer'"
|
|
755 |
!
|
|
756 |
|
|
757 |
spellAgainst: aString
|
|
758 |
"return an integer between 0 and 100 indicating how similar
|
|
759 |
the argument is to the receiver. No case conversion is done."
|
|
760 |
|
|
761 |
| i1 "{ Class: SmallInteger }"
|
|
762 |
i2 "{ Class: SmallInteger }"
|
|
763 |
size1 "{ Class: SmallInteger }"
|
|
764 |
size2 "{ Class: SmallInteger }"
|
|
765 |
score "{ Class: SmallInteger }"
|
|
766 |
maxLen "{ Class: SmallInteger }" |
|
|
767 |
|
|
768 |
size1 := self size.
|
|
769 |
size2 := aString size.
|
|
770 |
maxLen := size1 max: size2.
|
|
771 |
score := 0.
|
|
772 |
i1 := i2 := 1.
|
|
773 |
[i1 <= size1 and: [i2 <= size2]] whileTrue:[
|
|
774 |
(self at: i1) == (aString at: i2) ifTrue: [
|
|
775 |
score := score+1.
|
|
776 |
i1 := i1+1.
|
|
777 |
i2 := i2+1
|
|
778 |
] ifFalse: [
|
|
779 |
(i2 < size2 and: [(self at: i1) == (aString at: i2+1)]) ifTrue: [
|
|
780 |
i2 := i2+1
|
|
781 |
] ifFalse: [
|
|
782 |
(i1 < size1 and: [(self at: i1+1) == (aString at: i2)]) ifTrue: [
|
|
783 |
i1 := i1+1
|
|
784 |
] ifFalse: [
|
|
785 |
i1 := i1+1.
|
|
786 |
i2 := i2+1
|
|
787 |
]
|
|
788 |
]
|
|
789 |
]
|
|
790 |
].
|
|
791 |
|
|
792 |
score = maxLen ifTrue: [^100].
|
|
793 |
^100*score//maxLen
|
|
794 |
|
|
795 |
" 'Smalltalk' spellAgainst: 'Smalltlak' "
|
|
796 |
" 'Smalltalk' spellAgainst: 'smalltlak' "
|
|
797 |
" 'Smalltalk' spellAgainst: 'smalltalk' "
|
|
798 |
" 'Smalltalk' spellAgainst: 'smalltlk' "
|
|
799 |
" 'Smalltalk' spellAgainst: 'Smalltolk' "
|
|
800 |
! !
|
|
801 |
|
68
|
802 |
!AbstractString methodsFor:'padded copying'!
|
|
803 |
|
|
804 |
paddedTo:newSize with:padCharacter
|
|
805 |
"return a new string consisting of the receivers characters,
|
|
806 |
plus pad characters up to length"
|
|
807 |
|
|
808 |
|s len|
|
|
809 |
|
|
810 |
len := self size.
|
|
811 |
len < newSize ifTrue:[
|
|
812 |
s := self species new:newSize withAll:padCharacter.
|
|
813 |
s replaceFrom:1 to:len with:self.
|
|
814 |
^ s
|
|
815 |
]
|
|
816 |
|
|
817 |
"'123' printStringPaddedTo:10 with:$."
|
|
818 |
"'123' printStringPaddedTo:10 with:$*"
|
|
819 |
"(Float pi class name , ' ') printStringPaddedTo:15 with:$."
|
|
820 |
!
|
|
821 |
|
|
822 |
leftPaddedTo:size with:padCharacter
|
|
823 |
"return a new string of length size, which contains the receiver
|
|
824 |
right-adjusted (i.e. padded on the left).
|
|
825 |
characters on the left are filled with padCharacter.
|
|
826 |
If the receivers size is larger than the legth argument, it
|
|
827 |
is returned unchanged."
|
|
828 |
|
|
829 |
|len s|
|
|
830 |
|
|
831 |
len := self size.
|
|
832 |
(len < size) ifTrue:[
|
|
833 |
s := self species new:size withAll:padCharacter.
|
|
834 |
s replaceFrom:(size - len + 1) with:self.
|
|
835 |
^ s
|
|
836 |
]
|
|
837 |
|
|
838 |
"'123' leftPaddedTo:10 with:$."
|
|
839 |
"'1' leftPaddedTo:10 with:$."
|
|
840 |
"'hello' leftPaddedTo:10 with:$."
|
|
841 |
! !
|
|
842 |
|
64
|
843 |
!AbstractString methodsFor:'copying'!
|
|
844 |
|
|
845 |
concatenate:string1 and:string2
|
|
846 |
"return the concatenation of myself and the arguments, string1 and string2.
|
|
847 |
This is equivalent to self , string1 , string2
|
|
848 |
- generated by compiler when such a construct is detected"
|
|
849 |
|
|
850 |
^ self , string1 , string2
|
|
851 |
!
|
|
852 |
|
|
853 |
concatenate:string1 and:string2 and:string3
|
|
854 |
"return the concatenation of myself and the string arguments.
|
|
855 |
This is equivalent to self , string1 , string2 , string3
|
|
856 |
- generated by compiler when such a construct is detected"
|
|
857 |
|
|
858 |
^ self , string1 , string2 , string3
|
|
859 |
! !
|
|
860 |
|
|
861 |
!AbstractString methodsFor:'filling and replacing'!
|
|
862 |
|
|
863 |
withoutSpaces
|
|
864 |
"return a copy of myself without leading and trailing spaces"
|
|
865 |
|
|
866 |
|startIndex "{ Class: SmallInteger }"
|
|
867 |
endIndex "{ Class: SmallInteger }"
|
|
868 |
sz blank|
|
|
869 |
|
|
870 |
startIndex := 0.
|
|
871 |
sz := self size.
|
|
872 |
startIndex == 0 ifTrue:[
|
|
873 |
startIndex := 1.
|
|
874 |
endIndex := sz.
|
|
875 |
blank := Character space.
|
|
876 |
[(startIndex < endIndex) and:[(self at:startIndex) == blank]] whileTrue:[
|
|
877 |
startIndex := startIndex + 1
|
|
878 |
].
|
|
879 |
[(endIndex > 1) and:[(self at:endIndex) == blank]] whileTrue:[
|
|
880 |
endIndex := endIndex - 1
|
|
881 |
]
|
|
882 |
].
|
|
883 |
startIndex > endIndex ifTrue:[
|
|
884 |
^ ''
|
|
885 |
].
|
|
886 |
((startIndex == 1) and:[endIndex == sz]) ifTrue:[
|
|
887 |
^ self
|
|
888 |
].
|
|
889 |
^ self copyFrom:startIndex to:endIndex
|
|
890 |
!
|
|
891 |
|
|
892 |
withoutSeparators
|
|
893 |
"return a copy of myself without leading and trailing whitespace"
|
|
894 |
|
|
895 |
|startIndex "{ Class: SmallInteger }"
|
|
896 |
endIndex "{ Class: SmallInteger }"
|
|
897 |
sz|
|
|
898 |
|
|
899 |
startIndex := 0.
|
|
900 |
sz := self size.
|
|
901 |
startIndex == 0 ifTrue:[
|
|
902 |
startIndex := 1.
|
|
903 |
endIndex := self size.
|
|
904 |
[(startIndex < endIndex) and:[(self at:startIndex) isSeparator]] whileTrue:[
|
|
905 |
startIndex := startIndex + 1
|
|
906 |
].
|
|
907 |
[(endIndex > 1) and:[(self at:endIndex) isSeparator]] whileTrue:[
|
|
908 |
endIndex := endIndex - 1
|
|
909 |
].
|
|
910 |
].
|
|
911 |
startIndex > endIndex ifTrue:[
|
|
912 |
^ ''
|
|
913 |
].
|
|
914 |
((startIndex == 1) and:[endIndex == sz]) ifTrue:[
|
|
915 |
^ self
|
|
916 |
].
|
|
917 |
^ self copyFrom:startIndex to:endIndex
|
|
918 |
! !
|
|
919 |
|
|
920 |
!AbstractString methodsFor:'queries'!
|
|
921 |
|
|
922 |
isString
|
|
923 |
"return true, if the receiver is some kind of string;
|
|
924 |
true is returned here - redefinition of Object>>isString."
|
|
925 |
|
|
926 |
^ true
|
|
927 |
!
|
|
928 |
|
|
929 |
encoding
|
|
930 |
^ #unknown
|
|
931 |
! !
|