< prev index next >

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

Print this page
rev 51726 : 8210717: String::detab, String::entab
Reviewed-by: smarks, rriggs, sherman

@@ -2970,10 +2970,108 @@
                              .orElse(0);
         return indent(n - outdent, true);
     }
 
     /**
+     * Replaces tab (U+0009) characters with enough space
+     * (U+0020) characters to align to tab stops at
+     * intervals {@code n}.
+     *
+     * @param n  number of characters between tab stops
+     *
+     * @return this string with tabs replaced with spaces
+     *
+     * @throws IllegalArgumentException if n is less that equals to zero.
+     *
+     * @since 12
+     */
+    public String detab(int n) {
+        if (n <= 0) {
+            throw new IllegalArgumentException("n must be greater than zero: " + n);
+        }
+        int length = length();
+        int column = 0;
+        int spaces = 0;
+        final StringBuilder sb = new StringBuilder(length * 2);
+        for (int pos = 0; pos < length; pos++) {
+            char ch = charAt(pos);
+            if (ch == ' ') {
+                spaces++;
+            } else if (ch == '\t') {
+                spaces += n - (column + spaces) % n;
+            } else if (ch == '\n' || ch == '\r') {
+                sb.append(ch);
+                column = 0;
+                spaces = 0;
+            } else {
+                if (0 < spaces) {
+                    column += spaces;
+                    while (0 < spaces) {
+                        spaces--;
+                        sb.append(' ');
+                    }
+                }
+                sb.append(ch);
+                column++;
+            }
+        }
+        return sb.toString();
+    }
+
+    /**
+     * Replaces some space (U+0020) characters with tab
+     * (U+0009) characters if the spacing aligns to tab
+     * stops at  intervals {@code n}.
+     *
+     * @param n  number of characters between tab stops
+     *
+     * @return this string with some spaces replaced with tabs
+     *
+     * @throws IllegalArgumentException if n is less that equals to zero.
+     *
+     * @since 12
+     */
+    public String entab(int n) {
+        if (n <= 0) {
+            throw new IllegalArgumentException("n must be greater than zero: " + n);
+        }
+        int length = length();
+        int column = 0;
+        int spaces = 0;
+        final StringBuilder sb = new StringBuilder(length);
+        for (int i = 0; i < length; i++) {
+            char ch = charAt(i);
+            if (ch == ' ') {
+                spaces++;
+            } else if (ch == '\t') {
+                spaces += n - (column + spaces) % n;
+            } else if (ch == '\n' || ch == '\r') {
+                sb.append(ch);
+                column = 0;
+                spaces = 0;
+            } else {
+                if (0 < spaces) {
+                    int nexttab = n - column % n;
+                    column += spaces;
+                    while (nexttab <= spaces) {
+                        spaces -= nexttab;
+                        nexttab = n;
+                        sb.append('\t');
+                    }
+                    while (0 < spaces) {
+                        spaces--;
+                        sb.append(' ');
+                    }
+                }
+                sb.append(ch);
+                column++;
+            }
+        }
+        return sb.toString();
+    }
+
+    /**
      * This object (which is already a string!) is itself returned.
      *
      * @return  the string itself.
      */
     public String toString() {
< prev index next >