newbie question (different newbie)

Josh Flowers josh at i33.com
Mon Jul 12 21:06:46 UTC 1999


>2. You have found a bug, and want to fix it.  Beginners would be well advised
>to check with experienced Smalltalkers, such as can be found on the Squeak
>list, before doing this.
>

While not a bug, I did run across an inefficient implementation of String >>
lineCorrespondingToIndex.

lineCorrespondingToIndex: anIndex
 "Answer a string containing the line at the given character position. 
1/15/96 sw:  Inefficient first stab at this"

 | cr aChar answer |
 cr _ Character cr.
 answer _ ''.
 1 to: self size do:
  [:i | 
   aChar _ self at: i.
   aChar == cr
    ifTrue:
     [i > anIndex
      ifTrue:
       [^ answer]
      ifFalse:
       [answer _ '']]
    ifFalse:
     [answer _ answer copyWith: aChar]].
 ^ answer

I changed it to - 

lineCorrespondingToIndex: anIndex
 "Answer a string containing the line at the given character position. 
7/12/99 jf:  Hopefully more efficient second stab at this"

 
| start end cr |
 
 cr _ Character cr.
 end _ self indexOf: cr startingAt: anIndex
 end = 0
  ifTrue:[ end _ self size ].
 start _ anIndex.
 [ ( self at: start ) = cr or: [ start = 1 ] ]
  ifFalse: [ start _ start - 1 ]. "Is there a start--, or is this how it's
done?"
 ^ self copyFrom: start to: end.

(Note, this is from memory, the actual code is at home.  Errors might
exist.).
Is it worth posting things like this?  The method doesn't appear to be used
anywhere except my code, but unless I'm overlooking something it should be
faster.





More information about the Squeak-dev mailing list