]> granicus.if.org Git - re2c/commitdiff
Simplified pretty-printing individual characters.
authorUlya Trofimovich <skvadrik@gmail.com>
Tue, 11 Aug 2015 10:12:32 +0000 (11:12 +0100)
committerUlya Trofimovich <skvadrik@gmail.com>
Tue, 11 Aug 2015 10:12:32 +0000 (11:12 +0100)
re2c/src/codegen/go_emit.cc
re2c/src/codegen/print.cc
re2c/src/codegen/print.h

index c196e6460230d9416c0603bf860c7784c981b297..542108fd5f82af1337bf3d95e3c1573d2f7d51bd 100644 (file)
@@ -71,7 +71,7 @@ void Case::emit (OutputFile & o, uint32_t ind)
                        if (dFlag && encoding.is (Enc::EBCDIC))
                        {
                                const uint32_t c = encoding.decodeUnsafe (b);
-                               if (isprint (c))
+                               if (is_print (c))
                                        o << " /* " << static_cast<char> (c) << " */";
                        }
                        bool last_case = i == ranges.size () - 1 && b == ranges[i].second - 1;
index 2ab73d55858e0d9e925815247a556ddfcf3e1bf1..58d0634502d882a6a24ef91052410d6cca7ac2d9 100644 (file)
@@ -6,6 +6,27 @@
 namespace re2c
 {
 
+bool is_print (uint32_t c)
+{
+       return c >= 0x20 && c < 0x7F;
+}
+
+bool is_space (uint32_t c)
+{
+       switch (c)
+       {
+               case '\t':
+               case '\f':
+               case '\v':
+               case '\n':
+               case '\r':
+               case ' ':
+                       return true;
+               default:
+                       return false;
+       }
+}
+
 char hexCh(uint32_t c)
 {
        static const char * sHex = "0123456789ABCDEF";
@@ -14,7 +35,8 @@ char hexCh(uint32_t c)
 
 void prtChOrHex(std::ostream& o, uint32_t c)
 {
-       if (!encoding.is(Enc::EBCDIC) && (c < 256u) && (isprint(c) || isspace(c)))
+       if (!encoding.is (Enc::EBCDIC)
+               && (is_print (c) || is_space (c)))
        {
                o << '\'';
                prtCh(o, c);
@@ -28,34 +50,22 @@ void prtChOrHex(std::ostream& o, uint32_t c)
 
 void prtHex(std::ostream& o, uint32_t c)
 {
-       int oc = (int)(c);
-
-       if (encoding.szCodeUnit() == 4)
-       {
-               o << "0x"
-                 << hexCh(oc >> 28)
-                 << hexCh(oc >> 24)
-                 << hexCh(oc >> 20)
-                 << hexCh(oc >> 16)
-                 << hexCh(oc >> 12)
-                 << hexCh(oc >>  8)
-                 << hexCh(oc >>  4)
-                 << hexCh(oc);
-       }
-       else if (encoding.szCodeUnit() == 2)
+       o << "0x";
+       const uint32_t cunit_size = encoding.szCodeUnit ();
+       if (cunit_size >= 4)
        {
-               o << "0x"
-                 << hexCh(oc >> 12)
-                 << hexCh(oc >>  8)
-                 << hexCh(oc >>  4)
-                 << hexCh(oc);
+               o << hexCh (c >> 28u)
+                       << hexCh (c >> 24u)
+                       << hexCh (c >> 20u)
+                       << hexCh (c >> 16u);
        }
-       else
+       if (cunit_size >= 2)
        {
-               o << "0x"
-                 << hexCh(oc >>  4) 
-                 << hexCh(oc);
+               o << hexCh (c >> 12u)
+                       << hexCh (c >> 8u);
        }
+       o << hexCh (c >> 4u)
+               << hexCh (c);
 }
 
 void prtCh(std::ostream& o, uint32_t c)
@@ -110,7 +120,9 @@ void prtCh(std::ostream& o, uint32_t c)
 
 void prtChOrHexForSpan(std::ostream& o, uint32_t c)
 {
-       if (!encoding.is(Enc::EBCDIC) && (c < 256u) && isprint(c) && (c != ']'))
+       if (!encoding.is(Enc::EBCDIC)
+               && is_print (c)
+               && (c != ']'))
        {
                prtCh(o, c);
        }
index 59d97fdd2e99f5263b5e16d0456fde4ac2ecd156..045796d171e2030e0ace2c86e582989f2dc6168b 100644 (file)
@@ -8,6 +8,8 @@
 namespace re2c
 {
 
+bool is_print (uint32_t c);
+bool is_space (uint32_t c);
 char hexCh(uint32_t c);
 void prtCh(std::ostream&, uint32_t);
 void prtHex(std::ostream&, uint32_t);