I can create a pointer to a String using pointer and if I have a pointer to a string what’s the way to “convert” it back? Basically create a String where the first char is pointed at by the pointer
s = "abc"
ptr = pointer(s)
# how do I convert the ptr back to a string? By pointing to it?
Update
Actually, I am looking for a way where xp and yp are the same, so as to conserve memory
x = "abc"
xp = pointer(x)
y = unsafe_string(xp)
yp = pointer(y)
yp == xp # false
My use case is string grouping. I have a Strings array but many of them point to the same underlying location. So instead of grouping strings by underlying value (slow!), I group them by their pointer (fast!), but then I still want the underlying strings-value again in the final step of the algorithm.
Anyway, I can just unsafe_string once for each groups I guess.
You generally don’t want to take pointers to strings like this. The main reason you’d want a pointer to a string is interacting with C for which you’ll want to use the Cstring type in the ccall signature, which checks that you don’t have embedded nulls and ensures that your string data is null terminated. See
Edit: if you just want a pointer and a length, then you’ll want to use Ptr{UInt8} for the pointer field and Csize_t for the length (according to the C API, of course).
Yeah, I will at some point make an optimisation for that which is basically pointer based sorting/grouping (btw which at this stage is 40% faster than R’s string radixsort already).