< prev index next >

src/java.base/share/classes/java/lang/String.java

Print this page
rev 52199 : 8212694: Using Raw String Literals with align() and Integer.MIN_VALUE causes out of memory error
Reviewed-by: smarks


2951      * </pre></blockquote>
2952      *
2953      * @param n  number of leading white space characters
2954      *           to add or remove
2955      *
2956      * @return string with margins removed, indentation adjusted and
2957      *         line terminators normalized
2958      *
2959      * @see String#align()
2960      *
2961      * @since 12
2962      */
2963     public String align(int n) {
2964         if (isEmpty()) {
2965             return "";
2966         }
2967         int outdent = lines().filter(not(String::isBlank))
2968                              .mapToInt(String::indexOfNonWhitespace)
2969                              .min()
2970                              .orElse(0);
2971         return indent(n - outdent, true);


2972     }
2973 
2974     /**
2975      * This object (which is already a string!) is itself returned.
2976      *
2977      * @return  the string itself.
2978      */
2979     public String toString() {
2980         return this;
2981     }
2982 
2983     /**
2984      * Returns a stream of {@code int} zero-extending the {@code char} values
2985      * from this sequence.  Any char which maps to a <a
2986      * href="{@docRoot}/java.base/java/lang/Character.html#unicode">surrogate code
2987      * point</a> is passed through uninterpreted.
2988      *
2989      * @return an IntStream of char values from this sequence
2990      * @since 9
2991      */




2951      * </pre></blockquote>
2952      *
2953      * @param n  number of leading white space characters
2954      *           to add or remove
2955      *
2956      * @return string with margins removed, indentation adjusted and
2957      *         line terminators normalized
2958      *
2959      * @see String#align()
2960      *
2961      * @since 12
2962      */
2963     public String align(int n) {
2964         if (isEmpty()) {
2965             return "";
2966         }
2967         int outdent = lines().filter(not(String::isBlank))
2968                              .mapToInt(String::indexOfNonWhitespace)
2969                              .min()
2970                              .orElse(0);
2971         // overflow-conscious code
2972         int indent = n - outdent;
2973         return indent(indent > n ? Integer.MIN_VALUE : indent, true);
2974     }
2975 
2976     /**
2977      * This object (which is already a string!) is itself returned.
2978      *
2979      * @return  the string itself.
2980      */
2981     public String toString() {
2982         return this;
2983     }
2984 
2985     /**
2986      * Returns a stream of {@code int} zero-extending the {@code char} values
2987      * from this sequence.  Any char which maps to a <a
2988      * href="{@docRoot}/java.base/java/lang/Character.html#unicode">surrogate code
2989      * point</a> is passed through uninterpreted.
2990      *
2991      * @return an IntStream of char values from this sequence
2992      * @since 9
2993      */


< prev index next >