PDA

View Full Version : Left$, Right$, Mid$ ... Would this work??



m.r.b.
09-03-2007, 06:47 PM
Hello All...

I am not in front of my 'development PC' right now.

I'm trying to work out Left$,Right$ and Mid$ functions

Will this work?? If its right, it might be useful to someone...

Or is there a faster/better way embedded into spin..

[code]

PUB LeftStr(SourcreStringPtr,DestStringPointer,RhsChop )
{{
· @DestStringPointer = LEFT$(@SourcreStringPtr,RhsChop)·················· ··
}}
· MidStr(SourcreStringPtr,DestStringPointer,0,RhsCho p)


PUB RightStr(SourcreStringPtr,DestStringPointer,LhsCho p)
{{
· @DestStringPointer = RIGHT$(@SourcreStringPtr,LhsChop)················· ···
}}
· MidStr(SourcreStringPtr,DestStringPointer,LhsChop, strsize(SourcreStringPtr))


PUB MidStr(SourcreStringPtr,DestStringPointer,LhsChop, RhsChop) | TempPointer
{{
· @DestStringPointer = MID$(@SourcreStringPtr,LhsChop,RhsChop)
· IE MID$("ABCDEFG",2,3) RETURNS "CD"····················
}}

· repeat TempPointer from (SourcreStringPtr+LhsChop) to (SourcreStringPtr+RhsChop)
···· byte[DestStringPointer++] := (byte[TempPointer])

· byte[DestStringPointer] := 0 'Add the zero terminator to the end of the string.

[code/]

Regards
M.R.B.

Mike Green
09-03-2007, 10:07 PM
It would minimally work (i.e. copy the bytes) although in the Basic version, the substring positions start at 1 and in this version, they start at zero (which is correct here).

I say minimally because the Basic versions have a lot of checking implied so that the copy stops at the end of either the source or the destination string and "illegal" start and end values are not allowed. In Spin, there's no maximum length associated with a byte array that can be referenced by such a function. You'd have to add it as an extra parameter or store it as part of the byte array, etc.

You might consider using the BYTEMOVE statement which would be faster. Have a look at it in the manual.