RFR: 8333843: Provide methods on MemorySegment to read strings with known lengths

Maurizio Cimadamore mcimadamore at openjdk.org
Wed Aug 28 09:56:19 UTC 2024


On Tue, 27 Aug 2024 09:36:56 GMT, Per Minborg <pminborg at openjdk.org> wrote:

> This PR proposes to add a new overload to `MemorySegment::getString` whereby it is possible to pass in a known byte length of the content in a segment that should be converted to a String. This is useful in case one already knows the byte length and thereby does not need to scan for a null terminator.

src/java.base/share/classes/java/lang/foreign/MemorySegment.java line 1316:

> 1314: 
> 1315:     /**
> 1316:      * Reads a string using the given byte length from this segment at the given offset,

What happens is there's a null terminator before `length` ? Seems like we're just copying it? IMHO, this is the tension with this API.

src/java.base/share/classes/java/lang/foreign/MemorySegment.java line 1335:

> 1333:      *         largest string supported by the platform
> 1334:      * @throws IndexOutOfBoundsException if {@code offset < 0}
> 1335:      * @throws IndexOutOfBoundsException if {@code offset > byteSize() - (B + N)}, where:

I believe this is incorrect - e.g. all the B + N stuff is not needed, as we have a length

-------------

PR Review Comment: https://git.openjdk.org/jdk/pull/20725#discussion_r1734361085
PR Review Comment: https://git.openjdk.org/jdk/pull/20725#discussion_r1734360517


More information about the core-libs-dev mailing list