Page 1 of 1
Repeated string command
Posted: Thu Dec 04, 2008 12:46 pm
by PB
I thought this had been requested before, but I can't find it now.
In some other Basics there's a command to create a repeated string.
In PureBasic, I would suggest it to be named "RepeatString", as in this:
So it would generate a string containing 50 asterisks. Currently it's done like this:
Which is ugly and probably way slower than a native ASM command to do it.
Oh, and the repeated string is NOT just one character, but can be any size:
Code: Select all
a$=RepeatString("he",3) ; Returns "hehehe"
Re: Repeated string command
Posted: Thu Dec 04, 2008 5:27 pm
by Demivec
Not to detract from the request, here's a faster implentation:
Code: Select all
Procedure.s RepeatString(pattern$,reps = 1)
Protected a.String
a\S = Space(reps * Len(pattern$))
Protected *stringPtr = @a\S, i
CopyMemoryString(pattern$,@*stringPtr)
For i = 2 To reps
CopyMemoryString(pattern$)
Next
ProcedureReturn a\S
EndProcedure
Debug RepeatString("*",50)
Debug RepeatString("he",3)
Posted: Thu Dec 04, 2008 7:19 pm
by Trond
Currently it's done like this:
I never thought of doing it that way.

I always used ReplaceString() and Space() like this:
Code: Select all
S.s = ReplaceString(Space(RCount), " ", "RepeatMe")
ReplaceString() can even use in-place replacing, but then it's not so elegant any more:
Code: Select all
Procedure.s RepeatString(RepeatCount, StringToRepeat.s)
Protected S.s = Space(RepeatCount*Len(StringToRepeat))
ReplaceString(S, Space(Len(StringToRepeat)), StringToRepeat, #PB_String_InPlace)
ProcedureReturn S
EndProcedure
But I agree that it would be nice to have this predefined, it's something I've had to create for myself several times.
Re: Repeated string command
Posted: Fri Dec 05, 2008 11:08 am
by AND51
PB wrote:Which is ugly and probably way slower than a native ASM command to do it.
I think that's the point why there are so many feature requests. With ASM, an algorithm can be done better than with the best PB-Knowledge and with the smallest PB code.
Same with RepeatString().
But the best example is Hex-Dec-Bin-Support for
Val(). The best coders of this forum cannot reach the performance of the native
Val(), if a hex-value has to be converted to a long.
All in all I would agree with you, PB.
Posted: Fri Dec 05, 2008 12:18 pm
by HeX0R
Why not like this?
[Edit]
>>Oh, and the repeated string is NOT just one character, but can be any size:
Well o.k., then my solution won't work