INSTR and CHR$(0)

Previous topic - Next topic

MrTAToad

With this code :

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


a% returns -1...

Kitty Hello

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?

MrTAToad

Hmmm - I think it would be best to allow CHR$(0) for INSTR and REPLACE mainly if data is used that contains it.

Kitty Hello

OK, I'll do my very best.

MrTAToad

That'll be great, thanks!

Slydog

#5
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.
My current project (WIP) :: TwistedMaze <<  [Updated: 2015-11-25]