Unexpected Mid$ behaviour

BlitzMax Forums/BlitzMax Programming/Unexpected Mid$ behaviour

Grey Alien(Posted 2007) [#1]
Local s$ = "1234"
Print "S="+Mid(s$,1,1)
Print "S="+Mid(s$,1,0)
Print "S="+Mid(s$,1,-1)
Print "S="+Mid(s$,2,1)
Print "S="+Mid(s$,2,0)
Print "S="+Mid(s$,2,-1)


result:


S=1
S=
S=1234
S=2
S=
S=234



I'd have expected a negative length to return the same as length=0 i.e. a null string!

like this does:

Function ccMid$(s$,start%,length%)
	If length<0 Then
		Return ""
	Else
		Return Mid(s$,start,length)
	EndIf
End Function



H&K(Posted 2007) [#2]
I'd have expected a negative length to return the same as length=0 i.e. a null string!
Hummm, I think I would expect that the Mid$ function to be as fast as it could be. All -1 really is, is 1111111111111111(etc) (ie 4294967295)

So whereas I can see exactly where you are coming from, I wouldnt call it as unexpected as it would seem to be. That is I concider Indexes to be NON-signed


Grey Alien(Posted 2007) [#3]
You are probably not using Mid thousands of times a frame in most cases so safety of parameters adds more flexibility of use. Blitz rarely uses unsigned Integer parameters so why use one on a classic BASIC command?


H&K(Posted 2007) [#4]
I agree totaly, my point was that, I wouldnt have been sure how it would have delt with -1, so the result to me isnt unexpected, because its one of the two ways I would have expected it to do it.

What I wanted to say tho, was that in this case at least, the safty of parameters can be done as a very simple extention of the command that exists (That is your ccMid$() function). And I for one would prefer the fastest implementaion to be the base one.

Having said that I would be all for
Function Mid$(s$,start%,length%)
?Debug
 If length<0 Then
  runtimeerror "Negative length in Mid$"
Endif
?
Normal Mid$ code
  Return Result
End Function



Azathoth(Posted 2007) [#5]
The help says 'size' defaults to -1 which means to the end of the string.


Grey Alien(Posted 2007) [#6]
H&K yeah that could be useful, unless as Azahoth points out you used -1 intentionally... (thx Azahoth)


MGE(Posted 2007) [#7]
Grey, correct me if I'm wrong, but if -1 means "end of string" then isn't your initial results 100% correct?


Grey Alien(Posted 2007) [#8]
yep. I just didn't read the help files :-) Like I said, for ME it was unexpected ;-)


MGE(Posted 2007) [#9]
There's help files? :) lol.. Seriously, I need a book, something, I'm flying blind in the land of Blitzmax coding. lol.. (Aren't we all??)