You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@stdcxx.apache.org by se...@apache.org on 2006/06/20 02:56:32 UTC

svn commit: r415471 - in /incubator/stdcxx/trunk/tests: self/0.printf.cpp src/fmt_bits.cpp src/fmt_defs.h src/printf.cpp

Author: sebor
Date: Mon Jun 19 17:56:32 2006
New Revision: 415471

URL: http://svn.apache.org/viewvc?rev=415471&view=rev
Log:
2006-06-19  Martin Sebor  <se...@roguewave.com>

	* fmt_defs.h (_rw_fmtbits): Declared new helper function.
	* fmt_bits.cpp (_rw_fmtbits): Defined it.
	* src/printf.cpp (_rw_vasnprintf_ext): Called _rw_fmtbits in
	response to the %B and %b directives to format bitset values.
	* test/printf.cpp (test_bitset): Exercised the %b directive.
	(test_*): Declared all functions static.

Modified:
    incubator/stdcxx/trunk/tests/self/0.printf.cpp
    incubator/stdcxx/trunk/tests/src/fmt_bits.cpp
    incubator/stdcxx/trunk/tests/src/fmt_defs.h
    incubator/stdcxx/trunk/tests/src/printf.cpp

Modified: incubator/stdcxx/trunk/tests/self/0.printf.cpp
URL: http://svn.apache.org/viewvc/incubator/stdcxx/trunk/tests/self/0.printf.cpp?rev=415471&r1=415470&r2=415471&view=diff
==============================================================================
--- incubator/stdcxx/trunk/tests/self/0.printf.cpp (original)
+++ incubator/stdcxx/trunk/tests/self/0.printf.cpp Mon Jun 19 17:56:32 2006
@@ -28,6 +28,7 @@
 #include <rw_printf.h>
 #include <environ.h>   // for rw_putenv()
 
+#include <bitset>      // for bitset
 #include <ios>         // for ios::openmode, ios::seekdir
 #include <string>      // for string
 
@@ -190,7 +191,8 @@
 
 /***********************************************************************/
 
-void test_percent ()
+static void
+test_percent ()
 {
     //////////////////////////////////////////////////////////////////
     printf ("%s\n", "\"%\": percent sign");
@@ -213,7 +215,8 @@
 
 /***********************************************************************/
 
-void test_character ()
+static void
+test_character ()
 {
     //////////////////////////////////////////////////////////////////
     printf ("%s\n", "\"%c\": character formatting");
@@ -346,7 +349,8 @@
 
 /***********************************************************************/
 
-void test_string ()
+static void
+test_string ()
 {
     //////////////////////////////////////////////////////////////////
     printf ("%s\n", "\"%s\": character string");
@@ -497,7 +501,8 @@
 
 /***********************************************************************/
 
-void test_chararray ()
+static void
+test_chararray ()
 {
     //////////////////////////////////////////////////////////////////
     printf ("%s\n", "extension: \"%{Ac}\": quoted character array");
@@ -620,7 +625,8 @@
 
 /***********************************************************************/
 
-void test_basic_string ()
+static void
+test_basic_string ()
 {
     //////////////////////////////////////////////////////////////////
     printf ("%s\n", "extension: \"%{S}\": std::string");
@@ -721,7 +727,8 @@
 
 /***********************************************************************/
 
-void test_ios_bitmasks ()
+static void
+test_ios_bitmasks ()
 {
     //////////////////////////////////////////////////////////////////
     printf ("%s\n", "extension: \"%{Io}\": std::ios_base::opemode");
@@ -763,7 +770,75 @@
 
 /***********************************************************************/
 
-void test_dec (char spec)
+static const char*
+mkbitset (const char *str)
+{
+    static char bitset [32];
+
+    memset (bitset, 0, sizeof bitset);
+
+    char *pbyte = bitset;
+
+    for (const char *pc = str; *pc; ++pc) {
+
+        const size_t bitno = size_t (pc - str);
+
+        if ((bitno & 15) == 8)
+            ++pbyte;
+
+        const size_t binx = bitno & 7;
+
+        if ('0' == *pc)
+            *pbyte &= ~(1 << binx);
+        else if ('1' == *pc)
+            *pbyte |= 1 << binx;
+        else
+            RW_ASSERT (!"logic error: bit must be '0' or '1'");
+
+        RW_ASSERT (size_t (pbyte - bitset) < sizeof bitset);
+    }
+
+    return bitset;
+}
+
+
+static void
+test_bitset ()
+{
+    //////////////////////////////////////////////////////////////////
+    printf ("%s\n", "extension: \"%{b}\": bitset");
+
+#define BS(str)   mkbitset (str)
+
+    TEST ("%{b}",    0,                       0, 0, "(null)");
+    TEST ("%{.1b}",  BS ("1"),                0, 0, "1");
+    TEST ("%{.2b}",  BS ("1"),                0, 0, "10");
+    TEST ("%{.3b}",  BS ("1"),                0, 0, "100");
+    TEST ("%{.3b}",  BS ("101"),              0, 0, "101");
+    TEST ("%{.8b}",  BS ("11111111"),         0, 0, "11111111");
+    TEST ("%{.16b}", BS ("0000000000000000"), 0, 0, "0000000000000000");
+    TEST ("%{.16b}", BS ("0000000000000001"), 0, 0, "0000000000000001");
+    TEST ("%{.16b}", BS ("0000000000000010"), 0, 0, "0000000000000010");
+    TEST ("%{.16b}", BS ("0000000000000100"), 0, 0, "0000000000000100");
+    TEST ("%{.16b}", BS ("0000000000001000"), 0, 0, "0000000000001000");
+    TEST ("%{.16b}", BS ("0000000000010000"), 0, 0, "0000000000010000");
+    TEST ("%{.16b}", BS ("0000000000100000"), 0, 0, "0000000000100000");
+    TEST ("%{.16b}", BS ("0000000001000000"), 0, 0, "0000000001000000");
+    TEST ("%{.16b}", BS ("0000000010000000"), 0, 0, "0000000010000000");
+    TEST ("%{.16b}", BS ("0000000100000000"), 0, 0, "0000000100000000");
+    TEST ("%{.16b}", BS ("0000001000000000"), 0, 0, "0000001000000000");
+    TEST ("%{.16b}", BS ("0000010000000000"), 0, 0, "0000010000000000");
+    TEST ("%{.16b}", BS ("0000100000000000"), 0, 0, "0000100000000000");
+    TEST ("%{.16b}", BS ("0001000000000000"), 0, 0, "0001000000000000");
+    TEST ("%{.16b}", BS ("0010000000000000"), 0, 0, "0010000000000000");
+    TEST ("%{.16b}", BS ("0100000000000000"), 0, 0, "0100000000000000");
+    TEST ("%{.16b}", BS ("1000000000000000"), 0, 0, "1000000000000000");
+}
+
+/***********************************************************************/
+
+static void
+test_dec (char spec)
 {
     const bool sgn = 'u' != spec;
 
@@ -916,7 +991,8 @@
 
 /***********************************************************************/
 
-void test_oct ()
+static void
+test_oct ()
 {
     printf ("%s\n", "\"%o\": octal integer");
 
@@ -937,7 +1013,8 @@
 
 /***********************************************************************/
 
-void test_hex (char spec)
+static void
+test_hex (char spec)
 {
     printf ("\"%%%c\": hexadecimal integer\n", spec);
 
@@ -1153,7 +1230,8 @@
 
 /***********************************************************************/
 
-void test_bool ()
+static void
+test_bool ()
 {
     printf ("%s\n", "extension: \"%b\": bool");
 
@@ -1174,7 +1252,8 @@
 
 /***********************************************************************/
 
-void test_integer ()
+static void
+test_integer ()
 {
     test_dec ('d');
     test_dec ('i');
@@ -1246,7 +1325,8 @@
     return array;
 }
 
-void test_intarray ()
+static void
+test_intarray ()
 {
     //////////////////////////////////////////////////////////////////
     printf ("%s\n", "extension: \"%{Ao}\": array of octal integers");
@@ -1377,7 +1457,8 @@
 
 /***********************************************************************/
 
-void test_floating ()
+static void
+test_floating ()
 {
     //////////////////////////////////////////////////////////////////
     printf ("%s\n", "\"%e\": scientific floating point notation");
@@ -1449,7 +1530,8 @@
 
 /***********************************************************************/
 
-void test_pointer ()
+static void
+test_pointer ()
 {
     printf ("%s\n", "\"%p\": void pointer");
 
@@ -1486,7 +1568,8 @@
 }
 
 
-void test_funptr ()
+static void
+test_funptr ()
 {
     printf ("%s\n", "extension: \"%{f}\": function pointer");
 
@@ -1544,7 +1627,8 @@
 
 /***********************************************************************/
 
-void test_memptr ()
+static void
+test_memptr ()
 {
     printf ("%s\n", "extension: \"%{M}\": member pointer");
 
@@ -1676,7 +1760,8 @@
 
 /***********************************************************************/
 
-void test_width_specific_int ()
+static void
+test_width_specific_int ()
 {
     printf ("%s\n", "extension: \"%{I8d}\": 8-bit decimal integers");
 
@@ -1770,7 +1855,8 @@
 
 /***********************************************************************/
 
-void test_envvar ()
+static void
+test_envvar ()
 {
     printf ("%s\n", "extension: \"%{$string}\": environment variable");
 
@@ -1947,7 +2033,8 @@
 
 /***********************************************************************/
 
-void test_errno ()
+static void
+test_errno ()
 {
     //////////////////////////////////////////////////////////////////
     printf ("%s\n", "extension: \"%m\": strerror(errno)");
@@ -2017,7 +2104,8 @@
 
 /***********************************************************************/
 
-void test_signal ()
+static void
+test_signal ()
 {
     //////////////////////////////////////////////////////////////////
     printf ("%s\n", "extension: \"%K\": signal name");
@@ -2110,7 +2198,8 @@
 }
 
 
-void test_tm ()
+static void
+test_tm ()
 {
     //////////////////////////////////////////////////////////////////
     printf ("%s\n", "extension: \"%{t}\": struct tm");
@@ -2190,7 +2279,8 @@
 
 /***********************************************************************/
 
-void test_paramno ()
+static void
+test_paramno ()
 {
     //////////////////////////////////////////////////////////////////
     printf ("%s\n", "extension: \"%<paramno>$\": positional parameters");
@@ -2243,7 +2333,8 @@
 
 /***********************************************************************/
 
-void test_conditional ()
+static void
+test_conditional ()
 {
     //////////////////////////////////////////////////////////////////
     printf ("%s\n", "extension: \"%{?}\", \"%{:}\", \"%{;}\": conditional");
@@ -2481,7 +2572,8 @@
 }
 
 
-void test_user_defined_formatting ()
+static void
+test_user_defined_formatting ()
 {
     //////////////////////////////////////////////////////////////////
     printf ("%s\n", "extension: \"%{!}\" user-defined formatting function");
@@ -2524,7 +2616,8 @@
 
 /***********************************************************************/
 
-void test_bufsize ()
+static void
+test_bufsize ()
 {
     //////////////////////////////////////////////////////////////////
     printf ("%s\n", "extension: \"%{N}\" buffer size");
@@ -2545,7 +2638,8 @@
 
 /***********************************************************************/
 
-void test_malformed_directives ()
+static void
+test_malformed_directives ()
 {
     //////////////////////////////////////////////////////////////////
     printf ("%s\n", "malformed directives");
@@ -2581,6 +2675,8 @@
     test_basic_string ();
 
     test_ios_bitmasks ();
+
+    test_bitset ();
 
     test_tm ();
 

Modified: incubator/stdcxx/trunk/tests/src/fmt_bits.cpp
URL: http://svn.apache.org/viewvc/incubator/stdcxx/trunk/tests/src/fmt_bits.cpp?rev=415471&r1=415470&r2=415471&view=diff
==============================================================================
--- incubator/stdcxx/trunk/tests/src/fmt_bits.cpp (original)
+++ incubator/stdcxx/trunk/tests/src/fmt_bits.cpp Mon Jun 19 17:56:32 2006
@@ -288,7 +288,7 @@
 /********************************************************************/
 
 /* extern */ int
-_rw_fmtevent (const FmtSpec &spec, Buffer &buf, int event)
+_rw_fmtevent (const FmtSpec&, Buffer &buf, int event)
 {
     const char* const str =
           std::ios::copyfmt_event == event ? "copyfmt_event"
@@ -1088,4 +1088,66 @@
     newspec.fl_pound = 0;
 
     return _rw_fmtstr (newspec, buf, str, _RWSTD_SIZE_MAX);
+}
+
+/********************************************************************/
+
+/* extern */ int
+_rw_fmtbits (const FmtSpec &spec, Buffer &buf,
+             const void *pelems, size_t elemsize)
+{
+    const size_t nbits = spec.prec;
+
+    char bitbuf [256];
+    char *pbits = nbits < sizeof bitbuf ? bitbuf : new char [nbits + 1];
+
+    for (size_t bitno = 0; bitno != nbits; ++bitno) {
+
+        if (bitno && 0 == (bitno % (elemsize * _RWSTD_CHAR_BIT)))
+            pelems = _RWSTD_STATIC_CAST (const UChar*, pelems) + elemsize;
+
+        size_t bit = 1 << (bitno & (_RWSTD_CHAR_BIT * elemsize - 1));
+
+        switch (elemsize) {
+        case _RWSTD_CHAR_SIZE:
+            bit = *_RWSTD_STATIC_CAST (const UChar*, pelems) & bit;
+            break;
+
+#if _RWSTD_CHAR_SIZE < _RWSTD_SHRT_SIZE
+        case _RWSTD_SHRT_SIZE:
+            bit = *_RWSTD_STATIC_CAST (const UShrt*, pelems) & bit;
+            break;
+#endif   // sizeof (char) < sizeof (short)
+
+#if _RWSTD_SHRT_SIZE < _RWSTD_INT_SIZE
+        case _RWSTD_INT_SIZE:
+            bit = *_RWSTD_STATIC_CAST (const UInt*, pelems) & bit;
+            break;
+#endif   // sizeof (short) < sizeof (int)
+
+#if _RWSTD_INT_SIZE < _RWSTD_LONG_SIZE
+        case _RWSTD_LONG_SIZE:
+            bit = *_RWSTD_STATIC_CAST (const ULong*, pelems) & bit;
+            break;
+#endif   // sizeof (int) < sizeof (long)
+
+#if _RWSTD_LONG_SIZE < _RWSTD_LLONG_SIZE
+        case _RWSTD_LLONG_SIZE:
+            bit = *_RWSTD_STATIC_CAST (const ULLong*, pelems) & bit;
+            break;
+#endif   // sizeof (long) < sizeof (long long)
+
+        default:
+            RW_ASSERT ("logic error: bad element size");
+        }
+
+        pbits [bitno] = bit ? '1' : '0';
+    }
+
+    const int res = _rw_fmtstr (spec, buf, pbits, nbits);
+
+    if (pbits != bitbuf)
+        delete[] pbits;
+
+    return res;
 }

Modified: incubator/stdcxx/trunk/tests/src/fmt_defs.h
URL: http://svn.apache.org/viewvc/incubator/stdcxx/trunk/tests/src/fmt_defs.h?rev=415471&r1=415470&r2=415471&view=diff
==============================================================================
--- incubator/stdcxx/trunk/tests/src/fmt_defs.h (original)
+++ incubator/stdcxx/trunk/tests/src/fmt_defs.h Mon Jun 19 17:56:32 2006
@@ -221,5 +221,8 @@
 extern int
 _rw_fmtsignal (const FmtSpec&, Buffer&, int);
 
+// format a bitset of arbitrary size
+extern int
+_rw_fmtbits (const FmtSpec&, Buffer&, const void*, size_t);
 
 #endif   // RW_FMT_DEFS_H_INCLUDED

Modified: incubator/stdcxx/trunk/tests/src/printf.cpp
URL: http://svn.apache.org/viewvc/incubator/stdcxx/trunk/tests/src/printf.cpp?rev=415471&r1=415470&r2=415471&view=diff
==============================================================================
--- incubator/stdcxx/trunk/tests/src/printf.cpp (original)
+++ incubator/stdcxx/trunk/tests/src/printf.cpp Mon Jun 19 17:56:32 2006
@@ -2535,6 +2535,25 @@
         }
         break;
 
+    case 'B':   // %{B}
+        // std::bitset
+        spec.param.ptr_ = PARAM (ptr_);
+        if (   0 == spec.param.ptr_
+            || 0 > _RW::__rw_memattr (spec.param.ptr_, spec.prec, 0))
+            len = _rw_fmtbadaddr (spec, buf, spec.param.ptr_, sizeof (long));
+        else
+            len = _rw_fmtbits (spec, buf, spec.param.ptr_, sizeof (long));
+        break;
+
+    case 'b':   // %{b}
+        spec.param.ptr_ = PARAM (ptr_);
+        if (   0 == spec.param.ptr_
+            || 0 > _RW::__rw_memattr (spec.param.ptr_, spec.prec, 0))
+            len = _rw_fmtbadaddr (spec, buf, spec.param.ptr_);
+        else
+            len = _rw_fmtbits (spec, buf, spec.param.ptr_, 1);
+        break;
+
     case 'c':   // %{c}, %{Ac}, %{Lc}, %{lc}
         if (spec.mod == spec.mod_ext_A) {   // array of characters
             len = _rw_fmtarray (pspec, paramno, buf, pva);