• Print

Author Topic: QB64 string function accepts negative values [by design]  (Read 513 times)

Clippy

  • Hero Member
  • *****
  • Posts: 16445
  • I LOVE π = 4 * ATN(1)    Use the QB64 WIKI >>>
    • Pete's Qbasic Site
    • Email
QB64 string function accepts negative values [by design]
« on: March 15, 2013, 11:36:02 AM »
Code: [Select]
PRINT LEFT$("xxx",-1)

Network54
« Last Edit: April 03, 2013, 09:31:58 PM by Galleon »
QB64 WIKI: Main Page
Download Q-Basics Code Demo: Q-Basics.zip
Download QB64 BAT, IconAdder and VBS shortcuts: QB64BAT.zip
Download QB64 DLL files in a ZIP: Program64.zip

mcalkins

  • Hero Member
  • *****
  • Posts: 1269
    • qbasicmichael.com
    • Email
Re: QB64 string function accepts negative values
« Reply #1 on: March 15, 2013, 11:45:03 AM »
My impression is that this is one of a number of things that QB64 allows that QBASIC didn't.

I suppose that this could be called a bug, especially if there is some QBASIC program out there that relies on this causing an error. However, I suspect that it is deliberate.

Regards,
Michael
The QBASIC Forum Community: http://www.network54.com/index/10167 Includes off-topic subforums.
QB64 Off-topic subforum: http://qb64offtopic.freeforums.org/

sjt1975

  • Newbie
  • *
  • Posts: 46
    • My Personal Web-Site
    • Email
Re: QB64 string function accepts negative values
« Reply #2 on: March 15, 2013, 12:01:06 PM »
What about RIGHT$ and MID$?

Clippy

  • Hero Member
  • *****
  • Posts: 16445
  • I LOVE π = 4 * ATN(1)    Use the QB64 WIKI >>>
    • Pete's Qbasic Site
    • Email
Re: QB64 string function accepts negative values
« Reply #3 on: March 15, 2013, 01:06:08 PM »
Code: [Select]
PRINT LEFT$("xxx", -1)
PRINT RIGHT$("xxx", -1)
PRINT MID$("xxx", -1)
PRINT MID$("xxx", -1, -2)
QB64 WIKI: Main Page
Download Q-Basics Code Demo: Q-Basics.zip
Download QB64 BAT, IconAdder and VBS shortcuts: QB64BAT.zip
Download QB64 DLL files in a ZIP: Program64.zip

sjt1975

  • Newbie
  • *
  • Posts: 46
    • My Personal Web-Site
    • Email
Re: QB64 string function accepts negative values
« Reply #4 on: March 15, 2013, 02:15:53 PM »
Quote from: Clippy on March 15, 2013, 01:06:08 PM
Code: [Select]
PRINT LEFT$("xxx", -1)
PRINT RIGHT$("xxx", -1)
PRINT MID$("xxx", -1)
PRINT MID$("xxx", -1, -2)

If any of the parameters to LEFT$/RIGHT$/MID$ is a negative number, this should be trapped by the IDE/Editor as an error.  I assume that currently this is not the case, but that these functions rather return an empty string.

The trouble is, again, this is the-thin-edge-of-the-wedge, i.e. how many errors does the IDE/Editor trap, given all of the commands/statements within the QB64 language?

DSMan195276

  • Hero Member
  • *****
  • Posts: 1988
  • Yes
    • Email
Re: QB64 string function accepts negative values
« Reply #5 on: March 15, 2013, 02:31:26 PM »
Honestly, it should just come down to what QBasic does in this case, for compatibility reasons. Does any body actually have QBasic running so they could test this? I could probably get one going (Or just run a virtualized DOS if it comes to that) if nobody else has it up and running.

Matt
"Cast your cares on the Lord and he will sustain you; he will never let the righteous be shaken" -- Psalm 55:22
QB64 Linux Installer

OlDosLover

  • Hero Member
  • *****
  • Posts: 3860
  • OlDosLover
    • Email
Re: QB64 string function accepts negative values
« Reply #6 on: March 15, 2013, 06:03:14 PM »
Hi all,
    Just tested the
Quote
PRINT LEFT$("xxx", -1)
PRINT RIGHT$("xxx", -1)
PRINT MID$("xxx", -1)
PRINT MID$("xxx", -1, -2)
on QB45 and each one does a "Illegal function call". I remmed the other 3 out and ran it 4 times for same result each time.
OlDosLover.

mcalkins

  • Hero Member
  • *****
  • Posts: 1269
    • qbasicmichael.com
    • Email
Re: QB64 string function accepts negative values
« Reply #7 on: March 15, 2013, 07:47:10 PM »
To be clear, this is not an error that would be caught by the compiler, as the parameter to LEFT$(), etc, might not be constant. This is something that would have to be caught at runtime, by the library.

Yes, this is an incompatibility with QBASIC. But so is the fact that you can do unsigned integer overflow...

I'm pretty sure that this behavior is well known, and very likely deliberate.

How many real world programs actually relied on this causing an error?

I'm not against the idea of changing it to cause an error. However, doing so will break some existing QB64 code. Command line switch, anyone?

Regards,
Michael
The QBASIC Forum Community: http://www.network54.com/index/10167 Includes off-topic subforums.
QB64 Off-topic subforum: http://qb64offtopic.freeforums.org/

DSMan195276

  • Hero Member
  • *****
  • Posts: 1988
  • Yes
    • Email
Re: QB64 string function accepts negative values
« Reply #8 on: March 15, 2013, 08:32:59 PM »
Personally I'd vote to not change the current behavior. It is slightly different then QBasic, but I don't think it's different enough to cause horrible issues. I think that virtually everybody who found this error wouldn't have trapped it anyway but avoided it. And if the QBasic code avoids the error all together then it won't have any trouble running on QB64. I think it's more likely it'll just make some QB64 code not work anymore for not much gain.

Matt
"Cast your cares on the Lord and he will sustain you; he will never let the righteous be shaken" -- Psalm 55:22
QB64 Linux Installer

Galleon

  • Administrator
  • Hero Member
  • *****
  • Posts: 4664
  • QB Forever
    • Email
Re: QB64 string function accepts negative values
« Reply #9 on: March 16, 2013, 04:22:56 AM »
The above behaviour is by design. I do a lot of text manipulation and sometimes using a negative offset, particularly in a MID$ statement makes a lot of sense. After all, if it can flow out of the rhs of a string why can't it flow off the lhs.

Consider the following code snippit:
Code: [Select]
FOR x = 80 TO -26 STEP -1
    a$ = SPACE$(80)
    MID$(a$, x) = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    LOCATE 1, 1
    PRINT a$;
    SLEEP
NEXT
It was a natural progression to allow for negative offsets in LEFT$/RIGHT$ which is a little more controversial. Still, its been part of the QB64 language for a while now and will not change in the future.
Something old... Something new... Something borrowed... Something blue...

sjt1975

  • Newbie
  • *
  • Posts: 46
    • My Personal Web-Site
    • Email
Re: QB64 string function accepts negative values
« Reply #10 on: March 16, 2013, 11:24:07 AM »
Fair enough, Galleon, I can understand your point - kind of 'string over-flow symmetry'!  Also, if the internals of QB64 require this behaviour in these functions, then those behaviours had better remain, just in case changing them (to make these functions behave strictly like QB45) would cause any knock-on effects elsewhere (requiring any re-coding of existing working code).

fluffrabbit

  • Sr. Member
  • ****
  • Posts: 395
Re: QB64 string function accepts negative values
« Reply #11 on: March 17, 2013, 10:49:39 AM »
$strictqb:on

For exact emulation of QBASIC's functionality and ONLY QBASIC's functionality.


Ehh, bad idea. FreeBASIC has something like this. It offered a few different dialects, and some were added and removed, with functionality being shaved away. It made coding very confusing. I think that complete compatibility with QBASIC should be maintained at all times. Little incompatibilities like how the line-drawing algorithm is different should be fixed.

This may or may not be an incompatibility.

sjt1975

  • Newbie
  • *
  • Posts: 46
    • My Personal Web-Site
    • Email
Re: QB64 string function accepts negative values
« Reply #12 on: March 17, 2013, 11:45:48 AM »
Quote from: fluffrabbit on March 17, 2013, 10:49:39 AM
I think that complete compatibility with QBASIC should be maintained at all times.

That would be the ideal answer.

Quote from: fluffrabbit on March 17, 2013, 10:49:39 AM
This may or may not be an incompatibility.

Strictly speaking, this is an incompatibility with QB45.  However, I consider it to be a harmless incompatibility, since it doesn't cause any errors (the functions just return an empty string) and the original behaviour (as per QB45) still works as intended.  A programmer would not normally pass negative parameters to the LEFT$/MID$/RIGHT$ functions, since they don't make logical sense.
« Last Edit: March 17, 2013, 11:52:40 AM by sjt1975 »

fluffrabbit

  • Sr. Member
  • ****
  • Posts: 395
Re: QB64 string function accepts negative values
« Reply #13 on: March 18, 2013, 08:41:13 AM »
Quote from: sjt1975 on March 17, 2013, 11:45:48 AM
Quote from: fluffrabbit on March 17, 2013, 10:49:39 AM
I think that complete compatibility with QBASIC should be maintained at all times.

That would be the ideal answer.

Quote from: fluffrabbit on March 17, 2013, 10:49:39 AM
This may or may not be an incompatibility.

Strictly speaking, this is an incompatibility with QB45.  However, I consider it to be a harmless incompatibility, since it doesn't cause any errors (the functions just return an empty string) and the original behaviour (as per QB45) still works as intended.  A programmer would not normally pass negative parameters to the LEFT$/MID$/RIGHT$ functions, since they don't make logical sense.

What if someone wrote a QBASIC program in the QB64 editor and then tried to port it to QB 4.5?

mcalkins

  • Hero Member
  • *****
  • Posts: 1269
    • qbasicmichael.com
    • Email
Re: QB64 string function accepts negative values
« Reply #14 on: March 18, 2013, 10:53:21 AM »
Quote
What if someone wrote a QBASIC program in the QB64 editor and then tried to port it to QB 4.5?

Which is basically what happened in the topic that Clippy linked to.

There's not a whole lot of incentive to run QB64 programs in QB45/QBASIC 1.1. Probably the single biggest reason would be to take advantage of debugging. However, if you're using _UNSIGNED or DECLARE LIBRARY, you can't easily do that anyway.

Hopefully, they would see the run time error, and correct for it.

Code: [Select]
FUNCTION ErrorLeft$ (t AS STRING, l AS LONG)
 IF l < 0 THEN ERROR 5
 ErrorLeft = LEFT$(t, l)
END FUNCTION

Code: [Select]
FUNCTION SilentLeft$ (t AS STRING, l AS LONG)
IF l >= 0 THEN SilentLeft = LEFT$(t, l)
END FUNCTION

Regards,
Michael
The QBASIC Forum Community: http://www.network54.com/index/10167 Includes off-topic subforums.
QB64 Off-topic subforum: http://qb64offtopic.freeforums.org/

  • Print