Repeated string command

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Repeated string command

Post 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:

Code: Select all

a$=RepeatString("*",50)
So it would generate a string containing 50 asterisks. Currently it's done like this:

Code: Select all

For a=1 To 50 : a$+"*" : Next
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"
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
User avatar
Demivec
Addict
Addict
Posts: 4260
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Repeated string command

Post 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)
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

Currently it's done like this:
I never thought of doing it that way. :shock: 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.
AND51
Addict
Addict
Posts: 1040
Joined: Sun Oct 15, 2006 8:56 pm
Location: Germany
Contact:

Re: Repeated string command

Post 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.
PB 4.30

Code: Select all

onErrorGoto(?Fred)
User avatar
HeX0R
Addict
Addict
Posts: 1189
Joined: Mon Sep 20, 2004 7:12 am
Location: Hell

Post by HeX0R »

Why not like this?

Code: Select all

Debug RSet("*", 50, "*")
[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
Post Reply