GLBasic forum

Main forum => Bug Reports => Topic started by: MrTAToad on 2010-Aug-17

Title: INSTR and CHR$(0)
Post by: MrTAToad on 2010-Aug-17
With this code :

Code (glbasic) Select
a$=CHR$(10)+CHR$(13)+CHR$(0)+"J"
a%=INSTR(a$,"J")
DEBUG a%+"\n"


a% returns -1...
Title: Re: INSTR and CHR$(0)
Post by: Kitty Hello on 2010-Aug-17
both, replace and instr use a highly optimized function to find a string in a string. I would have to change that to work with '\0' characters and fear performance impacts. Do you really need that?
Title: Re: INSTR and CHR$(0)
Post by: MrTAToad on 2010-Aug-17
Hmmm - I think it would be best to allow CHR$(0) for INSTR and REPLACE mainly if data is used that contains it.
Title: Re: INSTR and CHR$(0)
Post by: Kitty Hello on 2010-Aug-17
OK, I'll do my very best.
Title: Re: INSTR and CHR$(0)
Post by: MrTAToad on 2010-Aug-17
That'll be great, thanks!
Title: Re: INSTR and CHR$(0)
Post by: Slydog on 2010-Aug-17
If you don't need to preserve the Null character (and speed isn't that important, or you have small strings!), you could always strip your strings ahead of time using something like (replaces all ascii '0' with ascii '1'): (Not tested)

Code (glbasic) Select

FUNCTION String_ReplaceNulls$: string$
LOCAL ix%
FOR ix = 0 TO LEN(string$) - 1
IF ASC(MID$(string$, ix, 1)) = 0 THEN string$ = MID$(string$, 0, ix-1) + CHR$(1) + MID$(string$, ix+1)
NEXT
RETURN string$
ENDFUNCTION


Also, MID$ doesn't work in reverse like some BASIC languages?  Such as:
Code (glbasic) Select
IF . . . THEN MID$(string$, ix, 1) = CHR$(1)

If modifying INSTR$ and REPLACE$ to fix this slows these commands down, perhaps adding two new functions may be better?
(INSTRB$, REPLACEB$ or something (B for Bytes like other languages), although the non 'B' versions usually offer non-case sensitive options).

Just some ideas.