[metapost] Metapost: generisize() bug

Eddie Kohler kohler at cs.ucla.edu
Tue Mar 18 18:35:32 CET 2008


Taco Hoekwater wrote:
 > This may be a strange question, but: Do you know how to fix the
 > macro? Apparently it is broken now thanks to an earlier (wrong)
 > bugfix by me for a problem that happened with strings with "[["
 > in it (reported by Denis Roegel).

It's not a STRANGE question exactly :)

The following generisize() appears to work.


vardef generisize (expr ss) =
   save res, s, stest, l; string res, s, stest;
   res = "";             % result so far
   s = ss;               % left to process
   forever: exitif s="";
     l := str_prefix(s, (c<>"[") and ((c<"0") or (c>"9")));
     res := res & substring (0,l) of s;
     s := substring (l,infinity) of s;
     if s<>"":
       if (s>="[") and (length s > 1):
         stest := substring (1,2) of s;
         if stest="[":
           l := 2;
           res := res & "[[";
         else:
           l := 1 + str_prefix(s, c<>"]");
           res := res & "[]";
         fi
       else:
         res := res & "[]";
         l := str_prefix(s, (c=".") or ("0"<=c) and (c<="9"));
       fi
       s := substring(l,infinity) of s;
     fi
   endfor
   res
enddef;


I think the problem was the prior [[ logic had some off by one errors and 
looked at the wrong characters.

The following metapost input file now works.


input boxes;
prologues := 1;
beginfig(1)
   boxjoin(b.w - a.e = (20,0));
   boxit.a(btex A etex);
   boxit.[[(btex [[ etex);
   boxit.b0(btex B0 etex);
   boxit.c[1](btex C[1] etex);
   boxit.d.e(btex D.E etex);
   boxit.f[2].g(btex F[2].G etex);
   boxit.h3i(btex H3I etex);
   drawboxed([[, a, b0, c[1], d.e, f[2].g, h3i);
endfig;
end


More tests are definitely warranted
Eddie


More information about the metapost mailing list