< prev index next >

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

Print this page
rev 53050 : 8215493: String::indent inconsistency with blank lines
Reviewed-by: rriggs, smarks


2796      *           by faster search of new line terminators.
2797      *
2798      * @return  the stream of lines extracted from this string
2799      *
2800      * @since 11
2801      */
2802     public Stream<String> lines() {
2803         return lines(0, 0);
2804     }
2805 
2806     /**
2807      * Adjusts the indentation of each line of this string based on the value of
2808      * {@code n}, and normalizes line termination characters.
2809      * <p>
2810      * This string is conceptually separated into lines using
2811      * {@link String#lines()}. Each line is then adjusted as described below
2812      * and then suffixed with a line feed {@code "\n"} (U+000A). The resulting
2813      * lines are then concatenated and returned.
2814      * <p>
2815      * If {@code n > 0} then {@code n} spaces (U+0020) are inserted at the
2816      * beginning of each line. {@link String#isBlank() Blank lines} are
2817      * unaffected.
2818      * <p>
2819      * If {@code n < 0} then up to {@code n}
2820      * {@link Character#isWhitespace(int) white space characters} are removed
2821      * from the beginning of each line. If a given line does not contain
2822      * sufficient white space then all leading
2823      * {@link Character#isWhitespace(int) white space characters} are removed.
2824      * Each white space character is treated as a single character. In
2825      * particular, the tab character {@code "\t"} (U+0009) is considered a
2826      * single character; it is not expanded.
2827      * <p>
2828      * If {@code n == 0} then the line remains unchanged. However, line
2829      * terminators are still normalized.
2830      *
2831      * @param n  number of leading
2832      *           {@link Character#isWhitespace(int) white space characters}
2833      *           to add or remove
2834      *
2835      * @return string with indentation adjusted and line endings normalized
2836      *
2837      * @see String#lines()
2838      * @see String#isBlank()
2839      * @see Character#isWhitespace(int)
2840      *
2841      * @since 12
2842      */
2843     public String indent(int n) {
2844         return isEmpty() ? "" :  indent(n, false);
2845     }
2846 
2847     private String indent(int n, boolean removeBlanks) {
2848         Stream<String> stream = removeBlanks ? lines(Integer.MAX_VALUE, Integer.MAX_VALUE)
2849                                              : lines();
2850         if (n > 0) {
2851             final String spaces = " ".repeat(n);
2852             stream = stream.map(s -> s.isBlank() ? s : spaces + s);
2853         } else if (n == Integer.MIN_VALUE) {
2854             stream = stream.map(s -> s.stripLeading());
2855         } else if (n < 0) {
2856             stream = stream.map(s -> s.substring(Math.min(-n, s.indexOfNonWhitespace())));
2857         }
2858         return stream.collect(Collectors.joining("\n", "", "\n"));
2859     }
2860 
2861     private int indexOfNonWhitespace() {
2862         return isLatin1() ? StringLatin1.indexOfNonWhitespace(value)
2863                           : StringUTF16.indexOfNonWhitespace(value);
2864     }
2865 
2866     private int lastIndexOfNonWhitespace() {
2867         return isLatin1() ? StringLatin1.lastIndexOfNonWhitespace(value)
2868                           : StringUTF16.lastIndexOfNonWhitespace(value);
2869     }
2870 
2871     /**
2872      * Removes vertical and horizontal white space margins from around the




2796      *           by faster search of new line terminators.
2797      *
2798      * @return  the stream of lines extracted from this string
2799      *
2800      * @since 11
2801      */
2802     public Stream<String> lines() {
2803         return lines(0, 0);
2804     }
2805 
2806     /**
2807      * Adjusts the indentation of each line of this string based on the value of
2808      * {@code n}, and normalizes line termination characters.
2809      * <p>
2810      * This string is conceptually separated into lines using
2811      * {@link String#lines()}. Each line is then adjusted as described below
2812      * and then suffixed with a line feed {@code "\n"} (U+000A). The resulting
2813      * lines are then concatenated and returned.
2814      * <p>
2815      * If {@code n > 0} then {@code n} spaces (U+0020) are inserted at the
2816      * beginning of each line.

2817      * <p>
2818      * If {@code n < 0} then up to {@code n}
2819      * {@link Character#isWhitespace(int) white space characters} are removed
2820      * from the beginning of each line. If a given line does not contain
2821      * sufficient white space then all leading
2822      * {@link Character#isWhitespace(int) white space characters} are removed.
2823      * Each white space character is treated as a single character. In
2824      * particular, the tab character {@code "\t"} (U+0009) is considered a
2825      * single character; it is not expanded.
2826      * <p>
2827      * If {@code n == 0} then the line remains unchanged. However, line
2828      * terminators are still normalized.
2829      *
2830      * @param n  number of leading
2831      *           {@link Character#isWhitespace(int) white space characters}
2832      *           to add or remove
2833      *
2834      * @return string with indentation adjusted and line endings normalized
2835      *
2836      * @see String#lines()
2837      * @see String#isBlank()
2838      * @see Character#isWhitespace(int)
2839      *
2840      * @since 12
2841      */
2842     public String indent(int n) {
2843         return isEmpty() ? "" :  indent(n, false);
2844     }
2845 
2846     private String indent(int n, boolean removeBlanks) {
2847         Stream<String> stream = removeBlanks ? lines(Integer.MAX_VALUE, Integer.MAX_VALUE)
2848                                              : lines();
2849         if (n > 0) {
2850             final String spaces = " ".repeat(n);
2851             stream = stream.map(s -> spaces + s);
2852         } else if (n == Integer.MIN_VALUE) {
2853             stream = stream.map(s -> s.stripLeading());
2854         } else if (n < 0) {
2855             stream = stream.map(s -> s.substring(Math.min(-n, s.indexOfNonWhitespace())));
2856         }
2857         return stream.collect(Collectors.joining("\n", "", "\n"));
2858     }
2859 
2860     private int indexOfNonWhitespace() {
2861         return isLatin1() ? StringLatin1.indexOfNonWhitespace(value)
2862                           : StringUTF16.indexOfNonWhitespace(value);
2863     }
2864 
2865     private int lastIndexOfNonWhitespace() {
2866         return isLatin1() ? StringLatin1.lastIndexOfNonWhitespace(value)
2867                           : StringUTF16.lastIndexOfNonWhitespace(value);
2868     }
2869 
2870     /**
2871      * Removes vertical and horizontal white space margins from around the


< prev index next >