Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8303821

Add method for counting leading non-negative bytes in byte arrays

XMLWordPrintable

    • Icon: CSR CSR
    • Resolution: Unresolved
    • Icon: P3 P3
    • None
    • core-libs
    • None
    • behavioral
    • minimal
    • Adding a new method to a non-extensible static utility class such as java.util.Arrays should have no compatibility risk.
    • Java API

      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 ByteBuffers 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)

            redestad Claes Redestad
            redestad Claes Redestad
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: