Code: Select all
Procedure.S RepStr(A$, N.I = 1)
Define R$, I
For I = 1 To N
R$ + A$
Next
ProcedureReturn R$
EndProcedure
Code: Select all
Debug RepStr("ha", 3)
; Displays "hahaha"
Code: Select all
Procedure.S RepStr(A$, N.I = 1)
Define R$, I
For I = 1 To N
R$ + A$
Next
ProcedureReturn R$
EndProcedure
Code: Select all
Debug RepStr("ha", 3)
; Displays "hahaha"
Code: Select all
Macro RepStr(text,num)
ReplaceString(Space(num)," ",text)
EndMacro
Debug RepStr("ha",3) ; Displays "hahaha"
Code: Select all
Debug "ha" * 3 ;outputs "hahaha"
Code: Select all
"ha" * "hi"
Code: Select all
>>> "ha" * "hi"
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can't multiply sequence by non-int of type 'str'
Nice simple one-linerYou can do this until the team decides whether to add your request. It's much faster than building a string with For/Next, and doesn't have the overhead of a procedure.
Code: Select all
Procedure.S RepStr(A$, N.I = 1)
Define R$, I
For I = 1 To N
R$ + A$
Next
ProcedureReturn R$
EndProcedure
Macro RepStr2(text,num)
ReplaceString(Space(num)," ",text)
EndMacro
Procedure.s RepStr3(A$, N.I = 1)
Protected R$
If A$ = ""
ElseIf (N > 1)
R$ = A$ + Space(Len(A$) * (N-1))
Copied = StringByteLength(A$)
Remain = Copied * (N-1)
While (#True)
If (Remain > Copied)
CopyMemory(@R$, @R$ + Copied, Copied)
Remain - Copied
Copied + Copied
Else
CopyMemory(@R$, @R$ + Copied, Remain)
Break
EndIf
Wend
ElseIf (N = 1)
R$ = A$
EndIf
ProcedureReturn R$
EndProcedure
CompilerIf #PB_Compiler_Debugger
CompilerError "Turn off Debugger for accurate speed comparison"
CompilerEndIf
OpenConsole()
TestStr.s = "ha"
NumRepeats.i = 5000
UseMD5Fingerprint()
PrintN("Method 1")
NumLoops.i = 10 ; limit the number of loops because this method is much slower!
Start = ElapsedMilliseconds()
For j = 1 To NumLoops
Test$ = RepStr(TestStr, NumRepeats)
Next j
Stop = ElapsedMilliseconds()
PrintN(StrF((Stop - Start)/NumLoops, 3) + " ms per loop")
PrintN("Hash: " + Fingerprint(@Test$, StringByteLength(Test$), #PB_Cipher_MD5))
PrintN("")
PrintN("Method 2")
NumLoops = 1000
Start = ElapsedMilliseconds()
For j = 1 To NumLoops
Test$ = RepStr2(TestStr, NumRepeats)
Next j
Stop = ElapsedMilliseconds()
PrintN(StrF((Stop - Start)/NumLoops, 3) + " ms per loop")
PrintN("Hash: " + Fingerprint(@Test$, StringByteLength(Test$), #PB_Cipher_MD5))
PrintN("")
PrintN("Method 3")
NumLoops = 1000
Start = ElapsedMilliseconds()
For j = 1 To NumLoops
Test$ = RepStr3(TestStr, NumRepeats)
Next j
Stop = ElapsedMilliseconds()
PrintN(StrF((Stop - Start)/NumLoops, 3) + " ms per loop")
PrintN("Hash: " + Fingerprint(@Test$, StringByteLength(Test$), #PB_Cipher_MD5))
PrintN("")
Input()
CloseConsole()
Code: Select all
Macro RepeatChar (_count_, _char_)
LSet("", _count_, _char_)
EndMacro
Optimize for big strings. First parameter allocate now enough space for the result stringBarryG wrote:You can do this until the team decides whether to add your request. It's much faster than building a string with For/Next, and doesn't have the overhead of a procedure.
Code: Select all
Macro RepStr(text,num) ReplaceString(Space(num)," ",text) EndMacro Debug RepStr("ha",3) ; Displays "hahaha"
Code: Select all
Macro RepBigStr(text,num)
ReplaceString(Space(num * Len(text)), Space(Len(text)), text)
EndMacro
r1.s = RepBigStr("No12345678",3) ; Displays "hahaha"
Debug r1
Code: Select all
Macro RepBigStr(text,num)
ReplaceString(Space(num)," ",text)
EndMacro
r1.s = RepBigStr("1234567890",1000)
Debug r1
Debug Len(r1) ; 10000, as expected.
Code: Select all
RepBigStr("1234567890",100000)