Jump to content

? bug with MidStr() ?

Recommended Posts

I wrote some code using the intrinsic function MidStr()


Notice the following short function, first in pascal, then in javascript


procedure Split(var s : string; Delim : string; var PartA : string; var PartB : string); 
var p : integer; 
  PartA := ''; 
  PartB := ''; 
  p := Pos(Delim, s); 
  if p > 0 then begin 
    PartA := MidStr(s, 1, p-1); 
    PartB := MidStr(s, p+1, Length(s)); 


function Split$1(s$3, Delim, PartA, PartB) { 
var p$1 = 0; 
PartA.v = ""; 
PartB.v = ""; 
p$1 = (s$3.v.indexOf(Delim)+1); 
if (p$1>0) { 
PartA.v = MidStr(s$3.v,1,p$1-1); 
PartB.v = MidStr(s$3.v,p$1+1,s$3.v.length); 


Notice that the Pos() function is converted to .indexOf, but the MidStr function is left intact.  The problem is that MidStr is not included in the compile and therefore causes a runtime error.


I'll have to write my own.  Shouldn't be hard, but seems like that should be fixed.





Link to post
Share on other sites

Yes, that's a bug. MidStr() is actually just an alias for Copy(), but it isn't handled correctly by the codegen.


I'm fixing it for the next release, ad interim, you don't have top write your own, just use Copy() instead of MidStr(), the parameters are the same.


edit: also took it as an opportunity to add a peephole optimization for the MidStr/Copy calls of the form "Copy(stringVariable, whatever, Length(stringVariable))"

Link to post
Share on other sites

Thanks for the info.  But I already wrote one.  Not a big deal.


I did notice, however, that pascal strings start with 1st character at position 1, and javascript strings seem to start with first character at position 0.  So when I wrote my code, I had to adjust the positioning by 1.


What is the general philosophy of SMS regarding strings?  Does it try to simulate pascal behavior, or just expect the developers to use them as javascript strings?





Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...