[Jython-checkins] jython: Fix imaginary padding problem in complex.__format__, and some javadoc.

jeff.allen jython-checkins at python.org
Mon May 19 00:49:33 CEST 2014


http://hg.python.org/jython/rev/805a6138224c
changeset:   7257:805a6138224c
parent:      7220:61ef07321554
user:        Jeff Allen <ja.py at farowl.co.uk>
date:        Sun May 04 16:56:16 2014 +0100
summary:
  Fix imaginary padding problem in complex.__format__, and some javadoc.
format(4j, "*>6") is '****4j' not '4j'. Cleaned up javadoc in stringlib.InternalFormat.

files:
  src/org/python/core/PyComplex.java                    |  24 +++--
  src/org/python/core/stringlib/InternalFormat.java     |  37 ++++-----
  src/org/python/core/stringlib/InternalFormatSpec.java |   6 +-
  3 files changed, 34 insertions(+), 33 deletions(-)


diff --git a/src/org/python/core/PyComplex.java b/src/org/python/core/PyComplex.java
--- a/src/org/python/core/PyComplex.java
+++ b/src/org/python/core/PyComplex.java
@@ -165,9 +165,10 @@
      * and <code>__repr__</code>, and none-format.
      * <p>
      * In general, the output is surrounded in parentheses, like <code>"(12.34+24.68j)"</code>.
-     * However, if the real part is zero, only the imaginary part is printed, and without
-     * parentheses like <code>"24.68j"</code>. The number format specification passed in is used
-     * without padding to width, for the real and imaginary parts individually.
+     * However, if the real part is zero (but not negative zero), only the imaginary part is
+     * printed, and without parentheses like <code>"24.68j"</code>. The number format specification
+     * passed in is used for the real and imaginary parts individually, with padding to width
+     * afterwards (if the specification requires it).
      *
      * @param spec parsed format specification string
      * @return formatted value
@@ -176,12 +177,13 @@
         FloatFormatter f = new FloatFormatter(spec, 2, 3); // Two elements + "(j)".length
         // Even in r-format, complex strips *all* the trailing zeros.
         f.setMinFracDigits(0);
-        if (real == 0.0) {
+        if (Double.doubleToLongBits(real) == 0L) {
+            // Real part is truly zero: show no real part.
             f.format(imag).append('j');
         } else {
-            f.append('(').format(real).format(imag, "+").append("j)").pad();
+            f.append('(').format(real).format(imag, "+").append("j)");
         }
-        return f.getResult();
+        return f.pad().getResult();
     }
 
     @Override
@@ -823,7 +825,7 @@
         try {
             String specString = formatSpecStr.getString();
             Spec spec = InternalFormat.fromText(specString);
-            if (spec.type!=Spec.NONE && "efgEFGn%".indexOf(spec.type) < 0) {
+            if (spec.type != Spec.NONE && "efgEFGn%".indexOf(spec.type) < 0) {
                 throw FloatFormatter.unknownFormat(spec.type, "complex");
             } else if (spec.alternate) {
                 throw FloatFormatter.alternateFormNotAllowed("complex");
@@ -838,12 +840,12 @@
                     // And then we use the __str__ mechanism to get parentheses or real 0 elision.
                     result = formatComplex(spec);
                 } else {
-                    // In any other format, the defaults those commonly used for numeric formats.
+                    // In any other format, defaults are those commonly used for numeric formats.
                     spec = spec.withDefaults(Spec.NUMERIC);
                     FloatFormatter f = new FloatFormatter(spec, 2, 1);// 2 floats + "j"
-                    // Convert as both parts per specification
-                    f.format(real).format(imag, "+").append('j').pad();
-                    result = f.getResult();
+                    // Convert both parts as per specification
+                    f.format(real).format(imag, "+").append('j');
+                    result = f.pad().getResult();
                 }
             }
         } catch (IllegalArgumentException e) {
diff --git a/src/org/python/core/stringlib/InternalFormat.java b/src/org/python/core/stringlib/InternalFormat.java
--- a/src/org/python/core/stringlib/InternalFormat.java
+++ b/src/org/python/core/stringlib/InternalFormat.java
@@ -108,7 +108,7 @@
          * method shows a '|' character between each section when it prints out the buffer. Override
          * this when you define more lengths in the subclass.
          *
-         * @return
+         * @return the lengths of the successive sections
          */
         protected int[] sectionLengths() {
             return new int[] {lenSign, lenWhole};
@@ -265,7 +265,6 @@
          * result you probably don't want. It is up to the client to disallow this (which
          * <code>complex</code> does).
          *
-         * @param value to pad
          * @return this object
          */
         public Formatter pad() {
@@ -468,8 +467,8 @@
      * during the construction of a new <code>Spec</code>, for attributes that are unspecified in a
      * primary source.
      * <p>
-     * This structure is returned by factory method {@link #fromText(CharSequence)}, and having
-     * public final members is freely accessed by formatters such as {@link FloatBuilder}, and the
+     * This structure is returned by factory method {@link #fromText(String)}, and having public
+     * final members is freely accessed by formatters such as {@link FloatFormatter}, and the
      * __format__ methods of client object types.
      * <p>
      * The fields correspond to the elements of a format specification. The grammar of a format
@@ -482,28 +481,28 @@
      * A typical idiom is:
      *
      * <pre>
-     *     private static final InternalFormatSpec FLOAT_DEFAULT = InternalFormatSpec.from(">");
+     *     private static final InternalFormatSpec FLOAT_DEFAULTS = InternalFormatSpec.from(">");
      *     ...
-     *         InternalFormatSpec spec = InternalFormatSpec.from(specString, FLOAT_DEFAULT);
+     *         InternalFormat.Spec spec = InternalFormat.fromText(specString);
+     *         spec = spec.withDefaults(FLOAT_DEFAULTS);
      *         ... // Validation of spec.type, and other attributes, for this type.
-     *         FloatBuilder buf = new FloatBuilder(spec);
-     *         buf.format(value);
-     *         String result = buf.getResult();
+     *         FloatFormatter f = new FloatFormatter(spec);
+     *         String result = f.format(value).getResult();
      *
      * </pre>
      */
     public static class Spec {
 
-        /** The fill character specified, or '\uffff' if unspecified. */
+        /** The fill character specified, or U+FFFF if unspecified. */
         public final char fill;
         /**
-         * Alignment indicator is one of {<code>'<', '^', '>', '='</code>, or '\uffff' if
+         * Alignment indicator is one of {<code>'<', '^', '>', '='</code>, or U+FFFF if
          * unspecified.
          */
         public final char align;
         /**
          * Sign-handling flag, one of <code>'+'</code>, <code>'-'</code>, or <code>' '</code>, or
-         * '\uffff' if unspecified.
+         * U+FFFF if unspecified.
          */
         public final char sign;
         /** The alternative format flag '#' was given. */
@@ -514,7 +513,7 @@
         public final boolean grouping;
         /** Precision decoded from the format, or -1 if unspecified. */
         public final int precision;
-        /** Type key from the format, or '\uffff' if unspecified. */
+        /** Type key from the format, or U+FFFF if unspecified. */
         public final char type;
 
         /** Non-character code point used to represent "no value" in <code>char</code> attributes. */
@@ -604,17 +603,17 @@
         }
 
         /**
-         * Return a merged <code>Spec</code> object, in which any attribute of this object, that is
-         * specified (or <code>true</code>) has the same value in the result, and any attribute of
-         * this object that is unspecified (or <code>false</code>) has the value that attribute
-         * takes in the other object. This the second object supplies default values. (These
+         * Return a merged <code>Spec</code> object, in which any attribute of this object that is
+         * specified (or <code>true</code>), has the same value in the result, and any attribute of
+         * this object that is unspecified (or <code>false</code>), has the value that attribute
+         * takes in the other object. Thus the second object supplies default values. (These
          * defaults may also be unspecified.) The use of this method is to allow a <code>Spec</code>
          * constructed from text to record exactly, and only, what was in the textual specification,
          * while the __format__ method of a client object supplies its type-specific defaults. Thus
          * "20" means "<20s" to a <code>str</code>, ">20.12" to a <code>float</code> and ">20.12g"
          * to a <code>complex</code>.
          *
-         * @param defaults to merge where this object does not specify the attribute.
+         * @param other defaults to merge where this object does not specify the attribute.
          * @return a new Spec object.
          */
         public Spec withDefaults(Spec other) {
@@ -646,7 +645,7 @@
          * @param precision (e.g. decimal places)
          * @param type indicator character
          */
-        public Spec(int width, int precision, char type) {
+        public Spec(int precision, char type) {
             this(' ', '>', Spec.NONE, false, UNSPECIFIED, false, precision, type);
         }
 
diff --git a/src/org/python/core/stringlib/InternalFormatSpec.java b/src/org/python/core/stringlib/InternalFormatSpec.java
--- a/src/org/python/core/stringlib/InternalFormatSpec.java
+++ b/src/org/python/core/stringlib/InternalFormatSpec.java
@@ -7,8 +7,8 @@
  * to a string assumed to be the result of formatting to the given precision.
  * <p>
  * This structure is returned by {@link InternalFormatSpecParser#parse()} and having public members
- * is freely used by {@link InternalFormatSpecParser}, {@link Formatter} and the __format__ methods
- * of client object types.
+ * is freely used by {@link InternalFormatSpecParser}, and the __format__ methods of client object
+ * types.
  * <p>
  * The fields correspond to the elements of a format specification. The grammar of a format
  * specification is:
@@ -45,7 +45,7 @@
      * @param defaultAlign to use if <code>this.align</code>=0 (one of <code>'<'</code>,
      *            <code>'^'</code>, <code>'>'</code>, or <code>'='</code>).
      * @param leaveWidth to reduce effective <code>this.width</code> by
-     * @return
+     * @return padded value
      */
     public String pad(String value, char defaultAlign, int leaveWidth) {
 

-- 
Repository URL: http://hg.python.org/jython


More information about the Jython-checkins mailing list