StrFind error in IDE v2.6.5?

Discussion about the ZBasic language including the System Library. If you're not sure where to post your message, do it here. However, do not make test posts here; that's the purpose of the Sandbox.
Post Reply
jay
Posts: 37
Joined: 08 July 2006, 13:58 PM
Location: Vermont, USA

StrFind error in IDE v2.6.5?

Post by jay »

Seems a problem with v2.6.5 of the compiler.. or more likely, I'm doing something stupid.

Code: Select all

' Examples from StrFind in ZBasicSysLib.pdf v 2.2.5

Sub main()
Dim idx as Byte

idx = StrFind("haystack", "needle") ' returns 0
Debug.Print "expected: 0, returns: " ; CStr(idx)
idx = StrFind("haystack with needle", "needle") ' returns 15
Debug.Print "expected: 15, returns: " ; CStr(idx)
idx = StrFind("foo bar foo", "foo", 2) ' returns 9
Debug.Print "expected: 9, returns: " ; CStr(idx)
idx = StrFind("foo bar foo", "", 2) ' returns 2
Debug.Print "expected: 2, returns: " ; CStr(idx)
idx = StrFind("foo bar FOO", "FOO") ' returns 9
Debug.Print "expected: 9, returns: " ; CStr(idx)
idx = StrFind("foo bar FOO", "FOO", 1, true) ' returns 1
Debug.Print "expected: 1, returns: " ; CStr(idx)
End Sub
Results:

Code: Select all

ZX1280 v2.6.3 00df,6c6a
Downloading file "C:zbasiclibstest.zxb":
...................
Download complete.
Verifying download:
...................
Verification complete.
expected: 0, returns: 0
expected: 15, returns: 0
expected: 9, returns: 0
expected: 2, returns: 1
expected: 9, returns: 0
expected: 1, returns: 1
Similar results when run on a ZX24 (v2.6.3 03ad,a3b1)
dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

Post by dkinzer »

We've been able to reproduce the issue here but don't yet know the cause.
- Don Kinzer
dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

Post by dkinzer »

dkinzer wrote:We've been able to reproduce the issue here but don't yet know the cause.
The problem is caused by an error in the compiler's optimization of the StrFind() invocation when it is passed constant parameters. You can demonstrate that this is so using the following similar test case which prevents the StrFind() calls from being optimized out.

Code: Select all

Sub Main()
  Call Find("haystack", "needle", 1, False, 0)
  Call Find("haystack with needle", "needle", 1, False, 15)
  Call Find("foo bar foo", "foo", 2, False, 9)
  Call Find("foo bar foo", "", 2, False, 2)
  Call Find("foo bar FOO", "FOO", 1, False, 9)
  Call Find("foo bar FOO", "FOO", 1, True, 1)
End Sub

Sub Find(ByVal srcStr as String, ByVal findStr as String, ByVal idx as Byte, _
    ByVal ignCase as Boolean, ByVal expect as Byte)
  idx = StrFind(srcStr, findStr, idx, ignCase)
  Debug.Print "expected: "; expect; ", returns: " ; idx
End Sub
This problem has been corrected internally. It will be in the next release.

In practice, this issue will rarely arise since the StrFind() call can be optimized away only if all of the parameters are compile-time constants.
- Don Kinzer
jay
Posts: 37
Joined: 08 July 2006, 13:58 PM
Location: Vermont, USA

Post by jay »

Thanks Your test code does indeed work, but
In practice, this issue will rarely arise since the StrFind() call can be optimized away only if all of the parameters are compile-time constants.
If you chage the original code to add:

Dim s1 as string, s2 as string, and use console.readline (stripping any eol) to assign data to s1 and s2. StrFind(s1,s2) still doesn't return the expected results.
It appears that the optimization problem seems to be a little more insidious than just compile-time constants.

..jay
Post Reply