diff Objects/stringlib/formatter.h @ 52773:28096ed73153

Added ',' thousands grouping to int.__format__. See PEP 378. This is incomplete, but I want to get some version into the next alpha. I am still working on: Documentation. More tests. Implement for floats. In addition, there's an existing bug with 'n' formatting that carries forward to thousands grouping (issue 5515).
author Eric Smith <eric@trueblade.com>
date Fri, 03 Apr 2009 14:45:06 +0000
parents a98091066e01
children 9a912a57164f
line wrap: on
line diff
--- a/Objects/stringlib/formatter.h	Fri Apr 03 11:19:31 2009 +0000
+++ b/Objects/stringlib/formatter.h	Fri Apr 03 14:45:06 2009 +0000
@@ -120,6 +120,7 @@
     int alternate;
     STRINGLIB_CHAR sign;
     Py_ssize_t width;
+    int thousands_separators;
     Py_ssize_t precision;
     STRINGLIB_CHAR type;
 } InternalFormatSpec;
@@ -149,6 +150,7 @@
     format->alternate = 0;
     format->sign = '\0';
     format->width = -1;
+    format->thousands_separators = 0;
     format->precision = -1;
     format->type = default_type;
 
@@ -201,6 +203,12 @@
         format->width = -1;
     }
 
+    /* Comma signifies add thousands separators */
+    if (end-ptr && ptr[0] == ',') {
+        format->thousands_separators = 1;
+        ++ptr;
+    }
+
     /* Parse field precision */
     if (end-ptr && ptr[0] == '.') {
         ++ptr;
@@ -230,6 +238,11 @@
         ++ptr;
     }
 
+    if (format->type == 'n' && format->thousands_separators) {
+        PyErr_Format(PyExc_ValueError, "Cannot specify ',' with 'n'.");
+        return 0;
+    }
+
     return 1;
 }
 
@@ -630,8 +643,13 @@
     if (format->type == 'n')
             /* Compute how many additional chars we need to allocate
                to hold the thousands grouping. */
+            STRINGLIB_GROUPING_LOCALE(NULL, n_digits, n_digits,
+                               0, &n_grouping_chars, 0);
+    if (format->thousands_separators)
+            /* Compute how many additional chars we need to allocate
+               to hold the thousands grouping. */
             STRINGLIB_GROUPING(NULL, n_digits, n_digits,
-                               0, &n_grouping_chars, 0);
+                               0, &n_grouping_chars, 0, "\3", ",");
 
     /* Calculate the widths of the various leading and trailing parts */
     calc_number_widths(&spec, sign, n_prefix, n_digits + n_grouping_chars,
@@ -670,11 +688,22 @@
                reserved enough space. */
             STRINGLIB_CHAR *pstart = p + n_leading_chars;
 #ifndef NDEBUG
-            int r =
+            int r;
 #endif
-                STRINGLIB_GROUPING(pstart, n_digits, n_digits,
+            if (format->type == 'n')
+#ifndef NDEBUG
+                r = 
+#endif
+                    STRINGLIB_GROUPING_LOCALE(pstart, n_digits, n_digits,
                            spec.n_total+n_grouping_chars-n_leading_chars,
                            NULL, 0);
+            else
+#ifndef NDEBUG
+                r =
+                    STRINGLIB_GROUPING(pstart, n_digits, n_digits,
+                           spec.n_total+n_grouping_chars-n_leading_chars,
+                           NULL, 0, "\3", ",");
+#endif
             assert(r);
     }