Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Writing fixed width strings is tricky #818

Open
david-macmahon opened this issue Feb 16, 2021 · 2 comments
Open

Writing fixed width strings is tricky #818

david-macmahon opened this issue Feb 16, 2021 · 2 comments

Comments

@david-macmahon
Copy link
Contributor

While exploring Parallel HDF5 via HDF5.jl I discovered that parallel HDF5 does not support variable length strings (maybe any variable type?). To store fixed length strings, I made my own Datatype for HDF5 strings of length N (68 in my case) and then converted my Vector{String} to SVector{68, UInt8} by doing something like:

julia> dataset[1:2] = @. rpad(["abc","def"], 68) |> collect |> SVector{68, UInt8};

julia> dataset[1:2]
2-element Array{String,1}:
 "abc                                                                 "
 "def                                                                 "

That works, but it is a bit tricky/cumbersome and introduces a dependency on StaticArrays. Is there an easier way to do this using existing HDF5.jl functionality? Maybe involving HDF5.FixedString???

@musm
Copy link
Member

musm commented Feb 16, 2021

Can you simply use?

c = "rannndomstring"
ntuple(i -> i <= ncodeunits(c) ? codeunit(c, i) : '\0', 68)

@david-macmahon
Copy link
Contributor Author

That's better as it removes the dependency on StaticArrays (though have to change '\0' to UInt8(0)), but I was hoping for an existing function that does this. I discovered that I could create the Datatype by calling datatype(" "^68) so that helps too. I guess this can be considered a feature request and/or documentation update request. It's certainly not a show stopper. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants