JuliaStrings / InlineStrings.jl

Fixed-width string types for Julia
Other
45 stars 13 forks source link

WIP Towards C-compat: Store the capacity minus length as the trailing byte #82

Open KristofferC opened 3 months ago

KristofferC commented 3 months ago

The point is that on master we have:

julia> reinterpret(UInt32, String3("a"))
0x61000001

julia> reinterpret(UInt32, String3("ab"))
0x61620002

julia> reinterpret(UInt32, String3("abc"))
0x61626303

Instead, with this PR we store the capacity minus the length as the last byte (and not the length itself) leading to:

julia> reinterpret(UInt32, String3("a"))
0x61000002

julia> reinterpret(UInt32, String3("ab"))
0x61620001

julia> reinterpret(UInt32, String3("abc"))
0x61626300

This means that in the case of the inline string being at full capacity the last byte double dips as a null terminator and showing that there is no capacity left (another way of saying that the length of the string is 3).

In order for "full C-compat" I think the byte order has to be swapped as well. I am not sure how worth this endeavour is tbh.