Commits

Lionel Flandrin committed 6b6a434

Added "spaced" printers

Comments (0)

Files changed (5)

   // Let's register the available printers
   this->printers[""]   = new PrintPrefix(10); // Default
   this->printers["h"]  = new PrintPrefix(16);
+  this->printers["d"]  = new PrintPrefix(10);
   this->printers["o"]  = new PrintPrefix(8);
   this->printers["b"]  = new PrintPrefix(2);
+  this->printers["hs"]  = new PrintPrefix(16, 2);
+  this->printers["ds"]  = new PrintPrefix(10, 3);
+  this->printers["os"]  = new PrintPrefix(8,  3);
+  this->printers["bs"]  = new PrintPrefix(2,  4);
   this->printers["v"]  = new PrintVerilog(16);
   this->printers["vh"] = new PrintVerilog(16);
   this->printers["vd"] = new PrintVerilog(10);
   void print_integer_base(std::ostream &o,
                           const Integer &i,
                           int base,
-                          const char *p)
+                          const char *p,
+                          unsigned spacing)
   {
     size_t len;
     char   *str;
       s++;
     }
 
-    o << p << s;
+    o << p;
+
+    if (spacing == 0)
+      o << s;
+    else {
+      size_t slen = std::strlen(s);
+      bool   first = true;
+
+      do {
+        if (!first) {
+          if ((slen % spacing) == 0)
+            o << '_';
+        } else
+          first = false;
+        o << *(s++);
+      } while (--slen);
+    }
 
     delete [] str;
   }
 }
 
 
-PrintPrefix::PrintPrefix(int b)
-  :base(b)
+PrintPrefix::PrintPrefix(int b, unsigned s)
+  :base(b), spacing(s)
 {
   switch(this->base) {
   case 16:
 
 void PrintPrefix::print_integer(std::ostream &o, const Integer &i)
 {
-  print_integer_base(o, i, this->base, this->prefix);
+  print_integer_base(o, i, this->base, this->prefix, this->spacing);
 }
 
 void PrintPrefix::print_float(std::ostream &o, const Float &f)
     o << 's';
   }
 
-  print_integer_base(o, v, this->base, this->prefix);
+  print_integer_base(o, v, this->base, this->prefix, 0);
 }
 
 
     else
       o << link;
 
-    print_integer_base(o, r, 10, "");
+    print_integer_base(o, r, 10, "", 0);
     o << '*' << prefixes[prefix] << this->unit;
 
     r *= mul;
   if (v > 0) {
     if (!first)
       o << link;
-    print_integer_base(o, v, 10, "");
+    print_integer_base(o, v, 10, "", 0);
     if (*this->unit)
       o << '*' << this->unit;
   }
 
   class PrintPrefix : public Printer {
     int         base;
+    unsigned    spacing;
     const char *prefix;
 
   public:
-    PrintPrefix(int);
+    PrintPrefix(int, unsigned = 0);
 
     virtual void print_integer(std::ostream &, const Integer &);
     virtual void print_float(std::ostream &, const Float &);

tests/expected-output.txt

 1*k + 1
 -3*GB - 253*MB - 262*kB - 913*B
 -3*G - 486*M - 784*k - 401
-21*GB + 866*MB + 680*kB + 441*B +0.0367928634484249784974
--23*G - 457*M - 341*k - 881 -0.0367928634484249784974
+21*GB + 866*MB + 680*kB + 441*B +0.0367928634484249785161
+-23*G - 457*M - 341*k - 881 -0.0367928634484249785161
+0b0
+0b1
+-0b1
+0b100
+-0b1010
+0b1111_1111
+0b1110_0110_1010_1001
+-0b1110_0110_1010_1001
+0
+1
+-1
+4
+-10
+255
+59_049
+-59_049
+00
+01
+-01
+04
+-012
+0377
+0163_251
+-0163_251
+0x0
+0x1
+-0x1
+0x4
+-0xa
+0xff
+0xe6_a9
+-0xe6_a9
 0*B
 0*B +0.01
 0 +0.00000050603
 3.3**20:s
 -3.3**20:si
 
+0:bs
+1:bs
+-1:bs
+4:bs
+-10:bs
+0xff:bs
+3**10:bs
+-3**10:bs
+
+0:ds
+1:ds
+-1:ds
+4:ds
+-10:ds
+0xff:ds
+3**10:ds
+-3**10:ds
+
+0:os
+1:os
+-1:os
+4:os
+-10:os
+0xff:os
+3**10:os
+-3**10:os
+
+0:hs
+1:hs
+-1:hs
+4:hs
+-10:hs
+0xff:hs
+3**10:hs
+-3**10:hs
+
 0:s
 0.010:s
 0.00000050603:si