rejetto forum

function cut() io non comprendere

Mars · 3 · 3483

0 Members and 1 Guest are viewing this topic.

Offline Mars

  • Operator
  • Tireless poster
  • *****
    • Posts: 2059
    • View Profile
 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);







Offline rejetto

  • Administrator
  • Tireless poster
  • *****
    • Posts: 13510
    • View Profile
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));


Offline bacter

  • Operator
  • Tireless poster
  • *****
    • Posts: 681
    • View Profile
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'
« Last Edit: December 07, 2007, 12:44:47 PM by bacter »
your computer has no brain - use your own !