changeset 49691:bc38df004048 raw-string-literal

0000000: Change name of trimXXXX routines to stripXXXX
author jlaskey
date Tue, 20 Mar 2018 14:45:21 -0300
parents fc3f1ec7cd52
children f2ec59847c27
files src/java.base/share/classes/java/lang/String.java src/java.base/share/classes/java/lang/StringLatin1.java src/java.base/share/classes/java/lang/StringUTF16.java test/jdk/java/lang/String/RawStringLiteralLib.java
diffstat 4 files changed, 194 insertions(+), 35 deletions(-) [+]
line wrap: on
line diff
--- a/src/java.base/share/classes/java/lang/String.java	Mon Mar 12 13:39:06 2018 -0300
+++ b/src/java.base/share/classes/java/lang/String.java	Tue Mar 20 14:45:21 2018 -0300
@@ -2671,9 +2671,9 @@
      *          and trailing white space removed, or this string if it
      *          has no leading or trailing white space.
      */
-    public String trimWhitespace() {
-        String ret = isLatin1() ? StringLatin1.trim(value)
-                                : StringUTF16.trimWhitespace(value);
+    public String strip() {
+        String ret = isLatin1() ? StringLatin1.strip(value)
+                                : StringUTF16.strip(value);
         return ret == null ? this : ret;
     }
 
@@ -2709,9 +2709,9 @@
      *          space removed, or this string if it has no leading white space.
      * @since 11
      */
-    public String trimLeft() {
-        String ret = isLatin1() ? StringLatin1.trimLeft(value)
-                                : StringUTF16.trimLeft(value);
+    public String stripLeading() {
+        String ret = isLatin1() ? StringLatin1.stripLeading(value)
+                                : StringUTF16.stripLeading(value);
         return ret == null ? this : ret;
     }
 
@@ -2749,9 +2749,9 @@
      *          trailing white space.
      * @since 11
      */
-    public String trimRight() {
-        String ret = isLatin1() ? StringLatin1.trimRight(value)
-                                : StringUTF16.trimRight(value);
+    public String stripTrailing() {
+        String ret = isLatin1() ? StringLatin1.stripTrailing(value)
+                                : StringUTF16.stripTrailing(value);
         return ret == null ? this : ret;
     }
 
@@ -2808,36 +2808,36 @@
         if (isEmpty()) {
             return this;
         }
-        List<String> list = lines().map(s -> s.trimRight())
+        List<String> list = lines().map(s -> s.stripTrailing())
                                    .collect(Collectors.toList());
         int count = list.size();
         String first = list.get(0);
         String last = list.get(count - 1);
         int minimal = list.stream().skip(1)
                                    .mapToInt(s -> s.isEmpty() ? Integer.MAX_VALUE :
-                                                                s.indexOfNonSpace())
+                                                                s.indexOfNonWhitespace())
                                    .min()
                                    .orElse(Integer.MAX_VALUE);
         Stream<String> stream = list.stream().skip(first.isEmpty() ? 1 : 0)
                                              .limit(last.isEmpty() ? count - 1 : count);
-        final int trim = last.isEmpty() ? Integer.min(minimal, lastIndexOfNonSpace()) : minimal;
+        final int trim = last.isEmpty() ? Integer.min(minimal, lastIndexOfNonWhitespace()) : minimal;
         if (trim != 0 && trim != Integer.MAX_VALUE) {
-            stream = stream.map(s -> trim <= s.indexOfNonSpace() ? s.substring(trim) : s);
+            stream = stream.map(s -> trim <= s.indexOfNonWhitespace() ? s.substring(trim) : s);
         }
         return stream.collect(Collectors.joining("\n"));
     }
 
-    private int indexOfNonSpace() {
+    private int indexOfNonWhitespace() {
         if (isLatin1()) {
-            return StringLatin1.indexOfNonSpace(value);
+            return StringLatin1.indexOfNonWhitespace(value);
         } else {
             return StringUTF16.indexOfNonWhitespace(value) >> 1;
         }
     }
 
-    private int lastIndexOfNonSpace() {
+    private int lastIndexOfNonWhitespace() {
         if (isLatin1()) {
-            return StringLatin1.lastIndexOfNonSpace(value);
+            return StringLatin1.lastIndexOfNonWhitespace(value);
         } else {
             return StringUTF16.lastIndexOfNonWhitespace(value) >> 1;
         }
@@ -2870,7 +2870,7 @@
         }
         int leftLength = leftMarker.length();
         int rightLength = rightMarker.length();
-        List<java.lang.String> list = lines().map(s -> s.trimWhitespace())
+        List<java.lang.String> list = lines().map(s -> s.strip())
                                              .collect(Collectors.toList());
         int count = list.size();
         String first = list.get(0);
@@ -3217,6 +3217,55 @@
      */
     public native String intern();
 
+    /**
+     * Returns a string whose value is the concatenation of this
+     * string repeated {@code count} times.
+     * <p>
+     * If this string is empty or count is zero then the empty
+     * string is returned.
+     *
+     * @param   count number of times to repeat
+     *
+     * @return  A string composed of this string repeated
+     *          {@code count} times or the empty string if this
+     *          string is empty or count is zero
+     *
+     * @throws  IllegalArgumentException if the {@code count} is
+     *          negative.
+     *
+     * @since 11
+     */
+    public String repeat(int count) {
+        if (count < 0) {
+            throw new IllegalArgumentException("count is negative: " + count);
+        }
+        if (count == 1) {
+            return this;
+        }
+        final int len = value.length;
+        if (len == 0 || count == 0) {
+            return "";
+        }
+        if (len == 1) {
+            final byte[] single = new byte[count];
+            Arrays.fill(single, value[0]);
+            return new String(single, coder);
+        }
+        if (Integer.MAX_VALUE / count < len) {
+            throw new OutOfMemoryError("Repeating " + len + " bytes String " + count +
+                    " times will produce a String exceeding maximum size.");
+        }
+        final int limit = len * count;
+        final byte[] multiple = new byte[limit];
+        System.arraycopy(value, 0, multiple, 0, len);
+        int copied = len;
+        for (; copied < limit - copied; copied <<= 1) {
+            System.arraycopy(multiple, 0, multiple, copied, copied);
+        }
+        System.arraycopy(multiple, 0, multiple, copied, limit - copied);
+        return new String(multiple, coder);
+    }
+
     ////////////////////////////////////////////////////////////////
 
     /**
@@ -3586,4 +3635,28 @@
         }
     }
 
+
+    /**
+     * Returns the string representation of the {@code codePoint}
+     * argument.
+     *
+     * @param   codePoint a {@code codePoint}.
+     * @return  a string of length {@code 1} or {@code 2} containing
+     *          as its single character the argument {@code codePoint}.
+     * @throws IllegalArgumentException if the specified
+     *          {@code codePoint} is not a {@linkplain Character#isValidCodePoint
+     *          valid Unicode code point}.
+     */
+    static String valueOfCodePoint(int codePoint) {
+        if (COMPACT_STRINGS && StringLatin1.canEncode(codePoint)) {
+            return new String(StringLatin1.toBytes((char)codePoint), LATIN1);
+        } else if (Character.isBmpCodePoint(codePoint)) {
+            return new String(StringUTF16.toBytes((char)codePoint), UTF16);
+        } else if (Character.isSupplementaryCodePoint(codePoint)) {
+            return new String(StringUTF16.toBytesSupplementary(codePoint), UTF16);
+        }
+
+        throw new IllegalArgumentException(
+            format("Not a valid Unicode code point: 0x%X", codePoint));
+    }
 }
--- a/src/java.base/share/classes/java/lang/StringLatin1.java	Mon Mar 12 13:39:06 2018 -0300
+++ b/src/java.base/share/classes/java/lang/StringLatin1.java	Tue Mar 20 14:45:21 2018 -0300
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -109,6 +109,10 @@
     public static int compareTo(byte[] value, byte[] other) {
         int len1 = value.length;
         int len2 = other.length;
+        return compareTo(value, other, len1, len2);
+    }
+
+    public static int compareTo(byte[] value, byte[] other, int len1, int len2) {
         int lim = Math.min(len1, len2);
         for (int k = 0; k < lim; k++) {
             if (value[k] != other[k]) {
@@ -122,6 +126,20 @@
     public static int compareToUTF16(byte[] value, byte[] other) {
         int len1 = length(value);
         int len2 = StringUTF16.length(other);
+        return compareToUTF16Values(value, other, len1, len2);
+    }
+
+    /*
+     * Checks the boundary and then compares the byte arrays.
+     */
+    public static int compareToUTF16(byte[] value, byte[] other, int len1, int len2) {
+        checkOffset(len1, length(value));
+        checkOffset(len2, StringUTF16.length(other));
+
+        return compareToUTF16Values(value, other, len1, len2);
+    }
+
+    private static int compareToUTF16Values(byte[] value, byte[] other, int len1, int len2) {
         int lim = Math.min(len1, len2);
         for (int k = 0; k < lim; k++) {
             char c1 = getChar(value, k);
@@ -529,6 +547,32 @@
         return right;
     }
 
+    public static int indexOfNonWhitespace(byte[] value) {
+        int length = value.length;
+        int left = 0;
+        while (left < length) {
+            char ch = (char)(value[left] & 0xff);
+            if (ch != ' ' && ch != '\t' && !Character.isWhitespace(ch)) {
+                break;
+            }
+            left++;
+        }
+        return left;
+    }
+
+    public static int lastIndexOfNonWhitespace(byte[] value) {
+        int length = value.length;
+        int right = length;
+        while (0 < right) {
+            char ch = (char)(value[right - 1] & 0xff);
+            if (ch != ' ' && ch != '\t' && !Character.isWhitespace(ch)) {
+                break;
+            }
+            right--;
+        }
+        return right;
+    }
+
     public static String trim(byte[] value) {
         int left = indexOfNonSpace(value);
         if (left == value.length) {
@@ -539,8 +583,18 @@
                 newString(value, left, right - left) : null;
     }
 
-    public static String trimLeft(byte[] value) {
-        int left = indexOfNonSpace(value);
+    public static String strip(byte[] value) {
+        int left = indexOfNonWhitespace(value);
+        if (left == value.length) {
+            return "";
+        }
+        int right = lastIndexOfNonWhitespace(value);
+        return ((left > 0) || (right < value.length)) ?
+                newString(value, left, right - left) : null;
+    }
+
+    public static String stripLeading(byte[] value) {
+        int left = indexOfNonWhitespace(value);
         if (left == value.length) {
             return "";
         }
@@ -548,8 +602,8 @@
                 newString(value, left, value.length - left) : null;
     }
 
-    public static String trimRight(byte[] value) {
-        int right = lastIndexOfNonSpace(value);
+    public static String stripTrailing(byte[] value) {
+        int right = lastIndexOfNonWhitespace(value);
         if (right == 0) {
             return "";
         }
--- a/src/java.base/share/classes/java/lang/StringUTF16.java	Mon Mar 12 13:39:06 2018 -0300
+++ b/src/java.base/share/classes/java/lang/StringUTF16.java	Tue Mar 20 14:45:21 2018 -0300
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -241,6 +241,13 @@
         return result;
     }
 
+    static byte[] toBytesSupplementary(int cp) {
+        byte[] result = new byte[4];
+        putChar(result, 0, Character.highSurrogate(cp));
+        putChar(result, 1, Character.lowSurrogate(cp));
+        return result;
+    }
+
     @HotSpotIntrinsicCandidate
     public static void getChars(byte[] value, int srcBegin, int srcEnd, char dst[], int dstBegin) {
         // We need a range check here because 'getChar' has no checks
@@ -279,6 +286,20 @@
     public static int compareTo(byte[] value, byte[] other) {
         int len1 = length(value);
         int len2 = length(other);
+        return compareValues(value, other, len1, len2);
+    }
+
+    /*
+     * Checks the boundary and then compares the byte arrays.
+     */
+    public static int compareTo(byte[] value, byte[] other, int len1, int len2) {
+        checkOffset(len1, value);
+        checkOffset(len2, other);
+
+        return compareValues(value, other, len1, len2);
+    }
+
+    private static int compareValues(byte[] value, byte[] other, int len1, int len2) {
         int lim = Math.min(len1, len2);
         for (int k = 0; k < lim; k++) {
             char c1 = getChar(value, k);
@@ -295,6 +316,10 @@
         return -StringLatin1.compareToUTF16(other, value);
     }
 
+    public static int compareToLatin1(byte[] value, byte[] other, int len1, int len2) {
+        return -StringLatin1.compareToUTF16(other, value, len2, len1);
+    }
+
     public static int compareToCI(byte[] value, byte[] other) {
         int len1 = length(value);
         int len2 = length(other);
@@ -853,7 +878,7 @@
         int left = 0;
         while (left < length) {
             char ch = getChar(value, left);
-            if (ch > ' ' && !Character.isWhitespace(ch)) {
+            if (ch != ' ' && ch != '\t' && !Character.isWhitespace(ch)) {
                 break;
             }
             left++;
@@ -866,7 +891,7 @@
         int right = length;
         while (0 < right) {
             char ch = getChar(value, right - 1);
-            if (ch > ' ' && !Character.isWhitespace(ch)) {
+            if (ch != ' ' && ch != '\t' && !Character.isWhitespace(ch)) {
                 break;
             }
             right--;
@@ -885,7 +910,7 @@
                 new String(Arrays.copyOfRange(value, left << 1, right << 1), UTF16) : null;
     }
 
-    public static String trimWhitespace(byte[] value) {
+    public static String strip(byte[] value) {
         int length = value.length >> 1;
         int left = indexOfNonWhitespace(value);
         if (left == length) {
@@ -896,7 +921,7 @@
                 new String(Arrays.copyOfRange(value, left << 1, right << 1), UTF16) : null;
     }
 
-    public static String trimLeft(byte[] value) {
+    public static String stripLeading(byte[] value) {
         int length = value.length >> 1;
         int left = indexOfNonWhitespace(value);
         if (left == length) {
@@ -907,7 +932,7 @@
                 null;
     }
 
-    public static String trimRight(byte[] value) {
+    public static String stripTrailing(byte[] value) {
         int length = value.length >> 1;
         int right = lastIndexOfNonWhitespace(value);
         if (right == 0) {
--- a/test/jdk/java/lang/String/RawStringLiteralLib.java	Mon Mar 12 13:39:06 2018 -0300
+++ b/test/jdk/java/lang/String/RawStringLiteralLib.java	Tue Mar 20 14:45:21 2018 -0300
@@ -44,14 +44,21 @@
      */
     static void test1() {
         equal("   abc   ".trim(), "abc");
-        equal("   abc   ".trimLeft(), "abc   ");
-        equal("   abc   ".trimRight(), "   abc");
+        equal("   abc   ".strip(), "abc");
+        equal("   abc   ".stripLeading(), "abc   ");
+        equal("   abc   ".stripTrailing(), "   abc");
         equal("   abc\u2022   ".trim(), "abc\u2022");
-        equal("   abc\u2022   ".trimLeft(), "abc\u2022   ");
-        equal("   abc\u2022   ".trimRight(), "   abc\u2022");
+        equal("   abc\u2022   ".strip(), "abc\u2022");
+        equal("   abc\u2022   ".stripLeading(), "abc\u2022   ");
+        equal("   abc\u2022   ".stripTrailing(), "   abc\u2022");
         equal("".trim(), "");
-        equal("".trimLeft(), "");
-        equal("".trimRight(), "");
+        equal("".strip(), "");
+        equal("".stripLeading(), "");
+        equal("".stripTrailing(), "");
+        equal("\b".trim(), "");
+        equal("\b".strip(), "\b");
+        equal("\b".stripLeading(), "\b");
+        equal("\b".stripTrailing(), "\b");
 
         // trimIndent
         for (String prefix : List.of("", "\n", "   \n"))