/*
Format specifiers
      %d signed decimal integers
      %x unsigned hexadecimal integers
      %s strings
      %% literal percent
*/

#ifndef _string_subst
#define _string_subst 1
#endif

subst (fmt, vec)
{
   local i, j, c1, c2, x;
   o = ""; i = j = 0;
   while (i < sizeof(fmt))
       if  ((c1 = fmt[i++]) != '%') o += c1;
       else
        switch (c2 = fmt[i++])
           {
             case '%' : o += c2; break;
             case 'd' : o += tostr_10(vec[j++]); break;
             case 'x' : o += hex(vec[j++]); break;
             case 's' : o += vec[j++]; break;
            };
    return (o);
}

tostr_10(n)
{
  local s, neg;
  if (neg = (n < 0)) n = -n;
  s = "";
  if (n == 0) s = "0";
  else while(n) { s = '0' + n%10 + s; n /= 10; }
  return (neg)?("-" + s):s;
}

hex(n)
{
  local s, c;
  s = "";
  if (n == 0) s = "0";
  else while(n) {
   c = n%16;
   s = (c < 10)?'0':'W' +  c + s; n /= 16; }
  return (s);
}