Skip to content

Commit

Permalink
Draft: CharSequence.getChars
Browse files Browse the repository at this point in the history
  • Loading branch information
mkarg committed Oct 9, 2024
1 parent 04b1c35 commit 42a675f
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 10 deletions.
11 changes: 1 addition & 10 deletions src/java.base/share/classes/java/io/Reader.java
Original file line number Diff line number Diff line change
Expand Up @@ -200,16 +200,7 @@ public int read(char[] cbuf, int off, int len) throws IOException {
if (next >= cs.length())
return -1;
int n = Math.min(cs.length() - next, len);
switch (cs) {
case String s -> s.getChars(next, next + n, cbuf, off);
case StringBuilder sb -> sb.getChars(next, next + n, cbuf, off);
case StringBuffer sb -> sb.getChars(next, next + n, cbuf, off);
case CharBuffer cb -> cb.get(next, cbuf, off, n);
default -> {
for (int i = 0; i < n; i++)
cbuf[off + i] = cs.charAt(next + i);
}
}
cs.getChars(next, next + n, cbuf, off);
next += n;
return n;
}
Expand Down
33 changes: 33 additions & 0 deletions src/java.base/share/classes/java/lang/CharSequence.java
Original file line number Diff line number Diff line change
Expand Up @@ -302,4 +302,37 @@ public static int compare(CharSequence cs1, CharSequence cs2) {
return cs1.length() - cs2.length();
}

/**
* Characters are copied from this sequence into the
* destination character array {@code dst}. 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
* {@code dstbegin + (srcEnd-srcBegin) - 1}.
*
* @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}
* </ul>
*/
public default void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) {
Objects.checkFromToIndex(srcBegin, srcEnd, length());
int n = srcEnd - srcBegin;
Objects.checkFromToIndex(dstBegin, dstBegin + n, dst.length);
for (int i = 0; i < n; i++)
dst[dstBegin + i] = charAt(srcBegin + i);
}
}
11 changes: 11 additions & 0 deletions src/java.base/share/classes/java/nio/X-Buffer.java.template
Original file line number Diff line number Diff line change
Expand Up @@ -2333,4 +2333,15 @@ public abstract sealed class $Type$Buffer

#end[streamableType]

#if[char]
@Override
public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) {
Objects.checkFromToIndex(srcBegin, srcEnd, limit());
int size = srcEnd - srcBegin;
Objects.checkFromIndexSize(dstBegin, size, dst.length);

getArray(srcBegin, dst, dstBegin, size);
}
#end[char]

}

0 comments on commit 42a675f

Please sign in to comment.