#replaceFrom:to:with:startingAt: vs. memmove()

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

#replaceFrom:to:with:startingAt: vs. memmove()

Lukas Renggli
As far as I see, the only obvious and efficient way to copy parts of
SequencableCollection's around is to use
#replaceFrom:to:with:startingAt:.

The problem is that for overlapping regions this does not work:

    " copying ahead works "
    buffer := String withAll: ($a to: $j).
    buffer replaceFrom: 1 to: 9 with: buffer startingAt: 2.
    self assert: buffer = 'bcdefghijj'.

    " copying backwards fails: the result is 'aaaaaaaaaa' "
    buffer := String withAll: ($a to: $j).
    buffer replaceFrom: 2 to: 10 with: buffer startingAt: 1.
    self assert: buffer = 'aabcdefghi'. "

When realizing this issue I felt put back far into the era of memcpy()
vs. memmove().

Fixing the fallback code in Smalltalk is obviously trivial, if
primitive 105 (that is flawed as well) was not involved. I wonder if
this never caused any troubles during the past 25 years? Why not fix
it? Afraid of the slowdown of an additional #== check?

Lukas

--
Lukas Renggli
http://www.lukas-renggli.ch