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 27, 2024
1 parent 0e3fc93 commit c56c6f7
Show file tree
Hide file tree
Showing 3 changed files with 63 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 @@ -194,16 +194,7 @@ public int read(char[] cbuf, int off, int len) throws IOException {
if (next >= length)
return -1;
int n = Math.min(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
39 changes: 39 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,43 @@ 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:
* <pre>{@code
* dstbegin + (srcEnd-srcBegin) - 1
* }</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}
* </ul>
*
* @implSpec
* The default implementation iterates over {@link #charAt(int)}.
*
* @since 24
*/
public default void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) {
Objects.checkFromToIndex(srcBegin, srcEnd, length());
Objects.checkFromIndexSize(dstBegin, srcEnd - srcBegin, dst.length);
while (srcBegin < srcEnd)
dst[dstBegin++] = charAt(srcBegin++);
}
}
23 changes: 23 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,27 @@ public abstract sealed class $Type$Buffer

#end[streamableType]

#if[char]
/**
* {@inheritDoc}
*
* @apiNote This method exists solely to implement
* {@link CharSequence#getChars(int,int,char[],int)}.
* It has no additional benefit over {@link #get(int, char[], int, int)}.
*
* @implSpec This method is equivalent to
* {@code get(srcBegin, dst, dstBegin, srcEnd - srcBegin)}.
*
* @implNote This method allows for superior performance over the default
* implementation in {@code CharSequence}. It performs a direct
* buffer-to-array copy of the complete specified region.
*
* @since 24
*/
@Override
public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) {
get(srcBegin, dst, dstBegin, srcEnd - srcBegin);
}
#end[char]

}

0 comments on commit c56c6f7

Please sign in to comment.