FillString

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
NikitaOdnorob98
User
User
Posts: 74
Joined: Fri Jun 29, 2012 4:50 pm

FillString

Post by NikitaOdnorob98 »

Code: Select all

Procedure$ FillString(Symbol$, Lenght)
  Protected String$ = ""
  Protected i
  For i = 1 To Lenght
    String$ + Symbol$
  Next
  ProcedureReturn String$
EndProcedure

a$ = FillString("1", 10)
Debug a$
User avatar
luis
Addict
Addict
Posts: 3895
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: FillString

Post by luis »

Code: Select all

Debug LSet("",10, "1")
"Have you tried turning it off and on again ?"
A little PureBasic review
User avatar
Bisonte
Addict
Addict
Posts: 1305
Joined: Tue Oct 09, 2007 2:15 am

Re: FillString

Post by Bisonte »

Sorry.... but

Code: Select all

b$ = RSet("", 10, "1") ; or b$ = LSet("", 10, "1")
Debug b$
Edit : Arg to slow ;)
PureBasic 6.21 (Windows x64) | Windows 11 Pro | AsRock B850 Steel Legend Wifi | R7 9800x3D | 64GB RAM | RTX 5080 | ThermaltakeView 270 TG ARGB | build by vannicom​​
English is not my native language... (I often use DeepL.)
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: FillString

Post by PB »

And don't forget...

Code: Select all

a$=ReplaceString(Space(10)," ","1")
Debug a$
...which has the added avantage of using multiple chars:

Code: Select all

a$=ReplaceString(Space(10)," ","ho")
Debug a$ ; Santa laughing!
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
davido
Addict
Addict
Posts: 1890
Joined: Fri Nov 09, 2012 11:04 pm
Location: Uttoxeter, UK

Re: FillString

Post by davido »

Would something like this work:

Code: Select all

*B = AllocateMemory(10000001)

For M = 0 To 1000000
  PokeB(*B + M,192)
Next M
PokeB(*B + 1000000,0)
A$ = PeekS(*B)
I guess it could be improved by using integers.
DE AA EB
User avatar
luis
Addict
Addict
Posts: 3895
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: FillString

Post by luis »

davido wrote: I guess it could be improved by using integers.

I think this is better:

. works with unicode
. it is faster for a large buffer because it uses FillMemory()
. does not loop using Poke()

PS: you don't need to poke a NULL at the end of the buffer, AllocateMemory reset the area to zeros.

Code: Select all

len = 10 ; len of the string
size = len * SizeOf(Character)
*p = AllocateMemory(size + SizeOf(Character))
FillMemory(*p, size, Asc("?"), #PB_Character)


ShowMemoryViewer(*p, size + SizeOf(Character))
a$ = PeekS(*p)
Debug Len(a$)
Debug a$
But LSet() is good enough and easier, especially for small strings.. and does not need an additional PeekS() at the end to transfer the data to a PB string (if required).
"Have you tried turning it off and on again ?"
A little PureBasic review
davido
Addict
Addict
Posts: 1890
Joined: Fri Nov 09, 2012 11:04 pm
Location: Uttoxeter, UK

Re: FillString

Post by davido »

Thanks luis,

I did think there was a better way. Just didn't know how. :)
DE AA EB
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: FillString

Post by wilbert »

A fixed length string could also be an option

Code: Select all

MyString.s{10}
FillMemory(@MyString, 10 * SizeOf(Character), Asc("?"), #PB_Character)

Debug MyString
Windows (x64)
Raspberry Pi OS (Arm64)
User avatar
STARGÅTE
Addict
Addict
Posts: 2227
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: FillString

Post by STARGÅTE »

Here my Code:

Code: Select all

Define MyString.s{10}
FillMemory(@MyString, SizeOf(MyString), 'X', #PB_Character)

Debug MyString
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
davido
Addict
Addict
Posts: 1890
Joined: Fri Nov 09, 2012 11:04 pm
Location: Uttoxeter, UK

Re: FillString

Post by davido »

Hi STARGÅTE,

As expected: Concise and incredibly fast!
DE AA EB
User avatar
luis
Addict
Addict
Posts: 3895
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: FillString

Post by luis »

Note in the original code the length was a parameter, using a static string you have to specify a constant for its size, so it's not the same thing.
"Have you tried turning it off and on again ?"
A little PureBasic review
Post Reply