Is there a Repeat method for Strings?

Right, I did that and it was just as you said.

Thanks!

1 Like

Now that Xojo 2022r2 has been released, I can reveal that the “mystery method” takes advantage of the new, very fast MemoryBlock.CopyBytes function.

Private Function Repeat(s As String, repetitions As Integer) As String
  if s = "" or repetitions < 1 then
    return ""
  end if
  
  if repetitions = 1 then
    return s
  end if
  
  var currentBytes as integer = s.Bytes
  var targetBytes as integer = currentBytes * repetitions
  var halfTarget as integer = targetBytes \ 2
  
  var mb as new MemoryBlock( targetBytes )
  mb.StringValue( 0, currentBytes ) = s
  
  while currentBytes <= halfTarget
    mb.CopyBytes( mb, 0, currentBytes, currentBytes )
    currentBytes = currentBytes + currentBytes
  wend
  
  var diff as integer = targetBytes - currentBytes
  if diff <> 0 then
    mb.CopyBytes( mb, 0, diff, currentBytes )
  end if
  
  return mb.StringValue( 0, targetBytes, s.Encoding )
  
End Function
4 Likes