More File Performance Q.?

Richard A. O'Keefe ok at cs.otago.ac.nz
Thu May 16 06:17:13 UTC 2002


Jimmie Houchin <jhouchin at texoma.net> wrote:
Python code:
	        for line in f1.readlines():
	            if line[:4].lower() == 'from':
	                if line.find('@') == -1:
	                    f2.write(' ' + line)
	                else: f2.write(line)
	            else: f2.write(line)
	
Smalltalk code:
	  (line asLowercase beginsWith: 'from') ifTrue:
	   [((line findString: '@') = 0) ifTrue: [line := ' ', line]].
	  file2 nextPutAll: line; cr].
	
1.  The Python code only lowercases the first four characters,
    the Smalltalk code lowercases every character.

2.  The best way to do this may be to use #match:
    'from*' match: line
    returns true iff line begins with "from" ignoring case.

3.  Don't construct new strings if you can avoid it.

So this code would be better as

	(('from*' match: line) and: [(line includes: $@) not])
	    ifTrue: [file2 nextPut: $ ].
	file2 nextPutAll: line; cr.

(I note that the traditional convention has been to stuff a ">" sign in
front of such lines, not a space, but that's the same for both languages.)

Is anyone else bothered by the fact that String>>beginsWith: will do a
futile search in the rest of the string if the receiver _doesn't_ begin
with prefix?




More information about the Squeak-dev mailing list