rejetto forum

Software => HFS ~ HTTP File Server => Bug reports => Topic started by: Mars on December 06, 2007, 11:21:31 PM

Title: function cut() io non comprendere
Post by: Mars on December 06, 2007, 11:21:31 PM
 procedure cut();
  var
    from, upTo, l: integer;
    s: string;
  begin
(1)  s:=par(2);                               
(2)  l:=length(s);
(3)  from:=strToIntDef(par(0), 1);
(4)  upTo:=strToIntDef(par(1), l);
(5)  if from < 0 then from:=l+from+1;
(6)  if upTo >= 0 then upTo:=from+upTo-1
(7)  else upTo:=l+upTo;
(8)  result:=substr(s, from, upTo);
  end; // cut

(0) {{cut|-3|-4|ABCDEFGH}}
(1) s='ABCDEFGH'
(2) l=
(3)  from=-3
(4)  upTo:=-4
(5)  if from < 0 then from:=l+from+1;         --> from=8-3+1=6
(6)  if upTo >= 0 then upTo:=from+upTo-1
(7)  else upTo:=l+upTo;                            --> upto=8-4=4 
(8)  result:=substr(s, from, upTo)               ---> substr(s,6,4)=copy(s,6,4-6+1)=copy(s,6,-1)=????? 

quelle est dans ton idée l'usage de nombres negatifs dans la macro cut ??


non documentée dans le wiki:
if name = 'replace' then
  result:=replaceText(par(2), par(1), p);





Title: Re: function cut() io non comprendere
Post by: rejetto on December 07, 2007, 12:41:17 PM
quelle est dans ton idée l'usage de nombres negatifs dans la macro cut ??

the problem with your example is that it has no meaning.
-3 and -1 should work.

Quote
non documentée dans le wiki:
if name = 'replace' then
  result:=replaceText(par(2), par(1), p);

documented and fixed as
Code: [Select]
  result:=ansiReplaceText(par(2), p, par(1));
Title: Re: function cut() io non comprendere
Post by: bacter on December 07, 2007, 12:43:14 PM
As it's explained in the wiki, newgative values for A and B are used to pint from the tail.
Naturally your example {{cut|-3|-4|ABCDEFGH}} uses correct sintax and so it is possible to write it that way, what natually will produce in theory an unpredictable result, but suppose delphi will produce a runtime error.

In the programmers logic i think there would be only few cases where from and upto both would be negative. But to make execution sure, line 8 should be
( 8 )  if upto > from then result:=substr(s, from, upTo); else result:='error'