Shop OBEX P1 Docs P2 Docs Learn Events
Left$, Right$, Mid$ ... Would this work?? — Parallax Forums

Left$, Right$, Mid$ ... Would this work??

m.r.b.m.r.b. Posts: 36
edited 2007-09-03 15:07 in Propeller 1
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..

[noparse][[/noparse]code]

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


PUB RightStr(SourcreStringPtr,DestStringPointer,LhsChop)
{{
· @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[noparse][[/noparse]DestStringPointer++] := (byte[noparse][[/noparse]TempPointer])

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

[noparse][[/noparse]code/]

Regards
M.R.B.

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2007-09-03 15:07
    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.
Sign In or Register to comment.