-
Notifications
You must be signed in to change notification settings - Fork 6.1k
8343110: Add getChars(int, int, char[], int) to CharSequence and CharBuffer #21730
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
c56c6f7
e912ad8
5da2f4b
60a891b
a3c2add
d27d1bd
29e1521
a7f56f4
a5d26c5
d2ccf42
884e7dc
cb2a2ef
47fbca7
99fcefb
342bc84
62a6867
31537b7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| /* | ||
| * Copyright (c) 2000, 2023, Oracle and/or its affiliates. All rights reserved. | ||
| * Copyright (c) 2000, 2025, 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 | ||
|
|
@@ -302,4 +302,47 @@ public static int compare(CharSequence cs1, CharSequence cs2) { | |
| return cs1.length() - cs2.length(); | ||
| } | ||
|
|
||
| /** | ||
| * Copies characters from this sequence into the given destination array. | ||
| * The first character to be copied is at index {@code srcBegin}; the last | ||
| * character to be copied is at index {@code srcEnd-1}. The total number of | ||
| * characters to be copied is {@code srcEnd-srcBegin}. The | ||
| * characters are copied into the subarray of {@code dst} starting | ||
| * at index {@code dstBegin} and ending at index: | ||
| * <pre>{@code | ||
| * dstbegin + (srcEnd-srcBegin) - 1 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't it be dstBegin? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hello Andrey, what you note is right. This and the other change you have proposed to this text seems reasonable. Do you want to create a JBS issue and raise a PR proposing this change? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could also simply include it in the PR for https://bugs.openjdk.org/browse/JDK-8356679 to reduce organizational overhead. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hello Markus, it's OK to do this text change as part of JDK-8356679. Like Roger noted in the corresponding core-libs-dev mailing list, it would be good to do the 8356679 changes in smaller pieces and include this change in one of those. |
||
| * }</pre> | ||
| * | ||
| * @param srcBegin start copying at this offset. | ||
| * @param srcEnd stop copying at this offset. | ||
| * @param dst the array to copy the data into. | ||
| * @param dstBegin offset into {@code dst}. | ||
| * @throws IndexOutOfBoundsException if any of the following is true: | ||
| * <ul> | ||
| * <li>{@code srcBegin} is negative | ||
| * <li>{@code dstBegin} is negative | ||
| * <li>the {@code srcBegin} argument is greater than | ||
| * the {@code srcEnd} argument. | ||
| * <li>{@code srcEnd} is greater than | ||
| * {@code this.length()}. | ||
| * <li>{@code dstBegin+srcEnd-srcBegin} is greater than | ||
| * {@code dst.length} | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should have |
||
| * </ul> | ||
| * @throws NullPointerException if {@code dst} is {@code null} | ||
| * | ||
| * @implSpec | ||
| * The default implementation invokes {@link #charAt(int index)} in a loop | ||
| * iterating {@code index} from {@code srcBegin} to {@code srcEnd-1}. | ||
| * Concurrent truncation of this character sequence can throw | ||
| * {@code IndexOutOfBoundsException}. In this case, some characters, but not | ||
| * all, may be already transferred. | ||
mkarg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| * | ||
| * @since 25 | ||
| */ | ||
| public default void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) { | ||
mkarg marked this conversation as resolved.
Show resolved
Hide resolved
mkarg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Objects.checkFromToIndex(srcBegin, srcEnd, length()); | ||
| Objects.checkIndex(dstBegin, dst.length - (srcEnd - srcBegin) + 1); | ||
| while (srcBegin < srcEnd) | ||
| dst[dstBegin++] = charAt(srcBegin++); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| /* | ||
| * Copyright (c) 2025, 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 | ||
| * under the terms of the GNU General Public License version 2 only, as | ||
| * published by the Free Software Foundation. | ||
| * | ||
| * This code is distributed in the hope that it will be useful, but WITHOUT | ||
| * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
| * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||
| * version 2 for more details (a copy is included in the LICENSE file that | ||
| * accompanied this code). | ||
| * | ||
| * You should have received a copy of the GNU General Public License version | ||
| * 2 along with this work; if not, write to the Free Software Foundation, | ||
| * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | ||
| * | ||
| * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | ||
| * or visit www.oracle.com if you need additional information or have any | ||
| * questions. | ||
| */ | ||
|
|
||
| import org.testng.Assert; | ||
| import org.testng.annotations.Test; | ||
|
|
||
| /** | ||
| * @test | ||
| * @bug 8343110 | ||
| * @summary Check for expected behavior of default implementation of | ||
| * CharSequence.getChars(). | ||
| * @run testng GetChars | ||
| */ | ||
| public class GetChars { | ||
| private static CharSequence CS = new CharSequence() { | ||
| @Override | ||
| public int length() { | ||
| return 4; | ||
| } | ||
|
|
||
| @Override | ||
| public char charAt(int index) { | ||
| return "Test".charAt(index); | ||
| } | ||
|
|
||
| @Override | ||
| public CharSequence subSequence(int start, int end) { | ||
| throw new UnsupportedOperationException(); | ||
| } | ||
| }; | ||
|
|
||
| @Test | ||
| public void testExactCopy() { | ||
| var dst = new char[4]; | ||
| CS.getChars(0, 4, dst, 0); | ||
| Assert.assertEquals(dst, new char[] {'T', 'e', 's', 't'}); | ||
| } | ||
|
|
||
| @Test | ||
| public void testPartialCopy() { | ||
| var dst = new char[2]; | ||
| CS.getChars(1, 3, dst, 0); | ||
| Assert.assertEquals(dst, new char[] {'e', 's'}); | ||
| } | ||
|
|
||
| @Test | ||
| public void testPositionedCopy() { | ||
| var dst = new char[] {1, 2, 3, 4, 5, 6}; | ||
| CS.getChars(0, 4, dst, 1); | ||
| Assert.assertEquals(dst, new char[] {1, 'T', 'e', 's', 't', 6}); | ||
| } | ||
|
|
||
| @Test | ||
| public void testSrcBeginIsNegative() { | ||
| Assert.assertThrows(IndexOutOfBoundsException.class, | ||
| () -> CS.getChars(-1, 3, new char[4], 0)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testDstBeginIsNegative() { | ||
| Assert.assertThrows(IndexOutOfBoundsException.class, | ||
| () -> CS.getChars(0, 4, new char[4], -1)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testSrcBeginIsGreaterThanSrcEnd() { | ||
| Assert.assertThrows(IndexOutOfBoundsException.class, | ||
| () -> CS.getChars(4, 0, new char[4], 0)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testSrcEndIsGreaterThanSequenceLength() { | ||
| Assert.assertThrows(IndexOutOfBoundsException.class, | ||
| () -> CS.getChars(0, 5, new char[4], 0)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testRequestedLengthIsGreaterThanDstLength() { | ||
| Assert.assertThrows(IndexOutOfBoundsException.class, | ||
| () -> CS.getChars(0, 4, new char[3], 0)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testDstIsNull() { | ||
| Assert.assertThrows(NullPointerException.class, | ||
| () -> CS.getChars(0, 4, null, 0)); | ||
| } | ||
|
|
||
| } |
Uh oh!
There was an error while loading. Please reload this page.