Fast shift of a MemoryBlock


let’s assume we have a MemoryBlock of size 5, whose bytes are 12345. If now I increase the size to 7, it will have two extra zeros on the right 1234500. However, I want to end up with a MemoryBlock with the zeros on the left 0012345. Is there a more efficient way that looping on the whole MB like the code below? Consider that the MB could be very large so I want to avoid the loop as I am looking for performance.


  mb.Size = mb.Size + n

  for i As Integer = mb.Size-1 DownTo n
    mb.Byte(i) = mb.Byte(i-n)
  for i As Integer = 0 To n-1
    mb.Byte(i) = 0

Assuming this is a Classic MemoryBlock…

dim mbTemp as new MemoryBlock( newSize )
mb.Temp.StringValue( mbTemp.Size - mb.Size, mb.Size ) = mb.StringValue( 0, mb.Size )
mb = mbTemp

New framework:

Dim mbNew As New Xojo.Core.MutableMemoryBlock(2) mbNew.Append(mb)

thank you both for the answers. I will do some tests and report here.

After some tests I ended up implementing the solution from Kem. Thanks again.