MyString = ReadS

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
Tenaja
Addict
Addict
Posts: 1959
Joined: Tue Nov 09, 2010 10:15 pm

MyString = ReadS

Post by Tenaja »

I would like to request the Read functions (all var types) get a version with the same format as the Peek functions. Look how much cleaner this can be...

Code: Select all

Read.s s
Debug s
; vs:
Debug ReadS
...if you have to put parens in for consistency, that's fine.
Debug ReadS()

Thanks!
User avatar
skywalk
Addict
Addict
Posts: 4211
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: MyString = ReadS

Post by skywalk »

Ummm, we already have Peek() functions... :wink:

Code: Select all

Debug PeekS(?MyString)
DataSection
  MyString:
  Data.s "Hello"
EndDataSection
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
User avatar
Tenaja
Addict
Addict
Posts: 1959
Joined: Tue Nov 09, 2010 10:15 pm

Re: MyString = ReadS

Post by Tenaja »

skywalk wrote:Ummm, we already have Peek() functions... :wink:

Code: Select all

Debug PeekS(?MyString)
DataSection
  MyString:
  Data.s "Hello"
EndDataSection
And how do you read numerous strings of unknown length, using PeekS?
User avatar
Shield
Addict
Addict
Posts: 1021
Joined: Fri Jan 21, 2011 8:25 am
Location: 'stralia!
Contact:

Re: MyString = ReadS

Post by Shield »

Like this?

Code: Select all


Procedure.s ReadS()
	Protected string.s
	
	Read.s string
	ProcedureReturn string
EndProcedure

Restore StringStuff
Debug ReadS()
Debug ReadS()
Debug ReadS()
Debug ReadS()
Debug ReadS()

DataSection
	StringStuff:
	Data.s "Hello"
	Data.s "Wolrd"
	Data.s "How"
	Data.s "Are"
	Data.s "You?"
EndDataSection

Image
Blog: Why Does It Suck? (http://whydoesitsuck.com/)
"You can disagree with me as much as you want, but during this talk, by definition, anybody who disagrees is stupid and ugly."
- Linus Torvalds
User avatar
skywalk
Addict
Addict
Posts: 4211
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: MyString = ReadS

Post by skywalk »

Code: Select all

Macro NEXTDATA_Str(DataLabel)
  *DataLabel + (MemoryStringLength(*DataLabel) + 1) * SizeOf(Character)
EndMacro

Define.i *p = ?MyString
Define.s s$

Repeat
  s$ = PeekS(*p): NEXTDATA_Str(p)
  Debug s$
Until s$ = "Q"

DataSection
  MyString:
  Data.s "Hello"
  Data.s "Hello2", "Hello33"
  Data.s "Hello444"
  Data.s "Q"
EndDataSection
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
User avatar
Tenaja
Addict
Addict
Posts: 1959
Joined: Tue Nov 09, 2010 10:15 pm

Re: MyString = ReadS

Post by Tenaja »

I recognize those are both useable workarounds, but I was requesting new PB commands that operate at the speed of PB, not the speed of workarounds.
User avatar
skywalk
Addict
Addict
Posts: 4211
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: MyString = ReadS

Post by skywalk »

Using the existing commands: Read.s and PeekS() are NOT workarounds or slow. :lol:
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
User avatar
Tenaja
Addict
Addict
Posts: 1959
Joined: Tue Nov 09, 2010 10:15 pm

Re: MyString = ReadS

Post by Tenaja »

skywalk wrote:Using the existing commands: Read.s and PeekS() are NOT workarounds or slow. :lol:
No, read.s is not a workaround, but is not a "function" that allows assigning a var its return value. But is IS cumbersome coding because it requires a "temporary" variable.

PeekS IS a workaround, because it does not read data sequentially.

Using a handwritten ReadS() function works, but has a lot of unnecessary overhead. If Fred were to implement it as a native function, I'd bet 100% of that overhead would go away. It IS a workaround, and a reasonable workaround until we get a native command, if we ever do. This is the wishlist forum, and I am just expressing a simple wish. It can't be a time consuming implementation.
User avatar
Demivec
Addict
Addict
Posts: 4260
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: MyString = ReadS

Post by Demivec »

Tenaja wrote:No, read.s is not a workaround, but is not a "function" that allows assigning a var its return value. But is IS cumbersome coding because it requires a "temporary" variable.
I gather that you are requesting a function that modifies the data pointer and returns a type of data read from the data pointer. I agree that that would be a useful thing.


Contrary to your comment you currently can use 'Read' to place data directly in a variable without the need for a temporary variable, you just can't use it in an any kind of expression or as a literal (without being placed in a variable). You would just use: Read.s MyString.

As stated PeekS() and it's relatives are functions and return data of a specific type but do not update a data pointer. I suggest that instead of adding a group of new 'Read' functions that a 'Peek' type of function be implemented that updates a data pointer.

Perhaps something like IPeekS(*dataPtr), IPeekD(*dataPtr), and etc. The 'I' stands for increment. It would return the data from the memory location and update the pointer by the size of the data.

It would look something like:

Code: Select all

;here are two stand-in macros representing the functionality desired
Macro IPeekB(ptr)
  PeekB(ptr): ptr + SizeOf(Byte)
EndMacro

;a version that handles the specification of length or flags is desired but is not shown here
Macro IPeekS(ptr)
  PeekS(ptr): ptr + (MemoryStringLength(ptr) + 1) * SizeOf(Character)
EndMacro


DataSection
  stuff:
  Data.b 5
  Data.s "first", "second", "third", "fourth", "fifth"
EndDataSection

*dataPtr = ?stuff
count = IPeekB(*dataPtr) ;pointer will be incremented by 1 for the size of a byte

For i = 1 To count
  Debug IPeekS(*dataPtr) ;pointer will be incremented by the memory length of the string + Null
Next
I think such a think would be useful because it would complement the 'Read' functionality with direct access to memory. As a plus it would allow several simultaneous reads from different locations.

If implemented it would also would beg the question, "Neglecting the origins of BASIC, why would you need 'Read' for anything anymore?"


It should be noted that the current function of Data/Read does not allow the use of fixed length strings.


@Edit: corrected typo from "just can use" to "just can't use"
Last edited by Demivec on Fri May 11, 2012 7:36 pm, edited 1 time in total.
User avatar
Tenaja
Addict
Addict
Posts: 1959
Joined: Tue Nov 09, 2010 10:15 pm

Re: MyString = ReadS

Post by Tenaja »

Demivec wrote:Contrary to your comment you currently can use 'Read' to place data directly in a variable without the need for a temporary variable, you just can use it in an any kind of expression or as a literal (without being placed in a variable). You would just use: Read.s MyString.
This does NOT work as a function parameter for calling another function. Like I said, it requires a temporary variable.
User avatar
Tenaja
Addict
Addict
Posts: 1959
Joined: Tue Nov 09, 2010 10:15 pm

Re: MyString = ReadS

Post by Tenaja »

Demivec wrote:If implemented it would also would beg the question, "Neglecting the origins of BASIC, why would you need 'Read' for anything anymore?"
Conversely, if Read were implemented with full access, why would one need Peek/Poke??? Peek is more of an antiquated command that was implemented due to lack of pointers etc. No other language uses it, other than languages trying to workaround their lack of string and or data handling.

:mrgreen:
User avatar
Demivec
Addict
Addict
Posts: 4260
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: MyString = ReadS

Post by Demivec »

Tenaja wrote:
Demivec wrote:Contrary to your comment you currently can use 'Read' to place data directly in a variable without the need for a temporary variable, you just can use it in an any kind of expression or as a literal (without being placed in a variable). You would just use: Read.s MyString.
This does NOT work as a function parameter for calling another function. Like I said, it requires a temporary variable.
I agree, and I corrected the typo in the previous message.
Post Reply