[Implement] PeekS additional argument

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

[Implement] PeekS additional argument

Post by BackupUser »

Restored from previous forum. Originally posted by Danilo.

Text$ = PeekS(*MemoryBuffer)

With varARGs available it would be nice
when this command could have an (optional)
additonal Parameter "Length":

Code: Select all

Text$ = PeekS(*MemoryBuffer, Length)
So it takes not a String until NULL is found
in memory -> it takes "Length" bytes only.

Very useful for conversion with other languages
and WinAPI.

If you have an ARRAY of BYTES or a fixed length
string (char), so you can convert it easily to
a String:

Code: Select all

myChars.b[10]
  [...some API function here...]
Text$ = PeekS(@myChar,10)
The same for PokeS() would be fine too, so
we can fill ARRAYs of BYTE easily:

Code: Select all

myChars.b[10]
myText.s = "123456789123456789"
PokeS(@myChar, myText, 10)
Thanks,
...Danilo

(registered PureBasic user)
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by PB.

> So it takes not a String until NULL is found in memory -> it takes "Length"
> bytes only.

Why not just use: Text$=Left(PeekS(*MemoryBuffer),Length) ?


PB - Registered PureBasic Coder

Edited by - PB on 06 May 2002 20:07:36
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by tinman.
> So it takes not a String until NULL is found in memory -> it takes "Length"
> bytes only.

Why not just use: Text$=Left(PeekS(*MemoryBuffer),Length) ?
Because your string in memory may not end with a NULL byte. I don't know how likely this is, but it could be possible.

Also, you would not want to work with huge strings every time you call the PeekS() command. For example, I use text files embedded into my exe for default languages and extract them using my own implementation of PeekS. However, I don't want PeekS to extract up to the end of the whole text file every time I call it as the text files could get quite big when the program is complete.

--
It's not minimalist - I'm increasing efficiency by reducing input effort.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by PB.

> > > So it takes not a String until NULL is found in memory -> it takes "Length"
> > > bytes only.
> >
> > Why not just use: Text$=Left(PeekS(*MemoryBuffer),Length) ?
>
> Because your string in memory may not end with a NULL byte.

I see... but I took Danilo's request to mean "read memory until X chars or
null string, whichever comes first". In that case, my tip would work. But
if that's not what he wants, then yes, you're right: my tip won't work.

PB - Registered PureBasic Coder
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by tinman.
I see... but I took Danilo's request to mean "read memory until X chars or
null string, whichever comes first". In that case, my tip would work. But
That's what I thought he meant too, but in that case you may still run into problems with the second point I made. And do PB strings still have a 64k limit? If they do, another argument would be you do not want to PeekS more than 64k chars :)

--
It's not minimalist - I'm increasing efficiency by reducing input effort.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Danilo.

>Why not just use: Text$=Left(PeekS(*MemoryBuffer),Length) ?

Because this are 2 commands for 1 = BLOATED EXE :wink:

Text$=PeekS(*MemoryBuffer,Length)
is faster to type, so i can write
at least 300 lines more per hour...

cya,
...Danilo
Every Byte counts. Same for seconds.

(registered PureBasic user)
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by fred.

I think it's for performance reason than I will make it. Imagine, you have a 1000 bytes length string. If you want only the first 10 bytes, PeekS() will read the whole 1000 bytes (until 0 is encounter) and then left will return the first 10 bytes. Not very good :)

Fred - AlphaSND
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Franco.
I think it's for performance reason than I will make it. Imagine, you have a 1000 bytes length string. If you want only the first 10 bytes, PeekS() will read the whole 1000 bytes (until 0 is encounter) and then left will return the first 10 bytes. Not very good :)
But what if you don't know how long the String is

Please If you change something accept the following:

PeekS(*MemoryBuffer,0)

for a null terminated string (or something like that...)
my 2c


Have a nice day...
Franco

Sometimes you have to go a lonely way to accomplish genius things.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by fred.

The length parameter will be optionnal.

Fred - AlphaSND
Post Reply