Summary
Add a new method for counting leading non-negative bytes in a byte array, effectively exposing publicly an efficient, intrinsified method we use internally in String
-related code to perform quick ASCII tests.
Problem
The internal solution serves the current String
implementation well, including various built-in CharsetEncoder/-Decoder
implementations. There are however a number of optional decoders and encoders out there that are left out, as well as third-party code operating on ByteBuffer
s et.c.
Solution
Expose the internal java.lang.StringCoding::countPositives
by means of a new method java.util.Arrays::numberOfLeadingPositives
.
Specification
The new method takes a naming cue from Integer::numberOfLeadingZeros
, while aligning parameter naming and error handling with other methods in java.util.Arrays
:
/**
* Counts the number of consecutive bytes in the range, starting from
* {@code fromIndex}, with a value greater than or equal to zero.
*
* @implNote this implementation will attempt to optimize this count using
* platform-specific SIMD instructions, and was implemented to speed up
* various ASCII tests.
*
* @param a the byte array to count leading positive bytes in
* @param fromIndex the index of the first element, inclusive, to be counted
* @param toIndex the index of the last element, exclusive, to be counted
* @return the number of consecutive bytes in the range, starting from
* {@code fromIndex}, with a value greater than or equal to zero.
*
* @throws IllegalArgumentException if {@code fromIndex > toIndex}
* @throws ArrayIndexOutOfBoundsException
* if {@code fromIndex < 0} or {@code toIndex > a.length}
* @since 21
*/
public static int countLeadingNonNegatives(byte[] a, int fromIndex, int toIndex)
- csr of
-
JDK-8303644 Add method for counting leading non-negative bytes in byte arrays
-
- New
-