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

Support constructing from bytes buffer alone #43

Merged
merged 1 commit into from
Oct 4, 2022
Merged

Conversation

nickrobinson251
Copy link
Collaborator

Was there a reason not to support this and instead require a user to give pos and len arguments explicitly?

I don't particularly have a use-case beyond having expected this to work having it tried it when developing locally...

Since someone can write

julia> String([0x61, 0x62, 0x63])
"abc"

I thought it might make sense to be able to write

julia> String3([0x61, 0x62, 0x63])
"abc"

I think length(buf) is probably the correct default, e.g. to get

julia> String3([0x61, 0x62, 0x63, 0x64])
ERROR: ArgumentError: string too large (4) to convert to String3
Stacktrace:
 [1] stringtoolong(T::Type, n::Int64)
   @ InlineStrings ~/repos/InlineStrings.jl/src/InlineStrings.jl:264
 [2] String3(buf::Vector{UInt8}, pos::Int64, len::Int64)
   @ InlineStrings ~/repos/InlineStrings.jl/src/InlineStrings.jl:201
 [3] String3(buf::Vector{UInt8})
   @ InlineStrings ~/repos/InlineStrings.jl/src/InlineStrings.jl:199
 [4] top-level scope
   @ REPL[39]:1

(rather than e.g. just consuming as much of the buffer as would fit in the requested type)

@mkitti
Copy link
Contributor

mkitti commented Oct 3, 2022

This is part of my motivation for StaticStrings.jl:

julia> using StaticStrings

julia> StaticString((0x61, 0x62, 0x63))
"abc"

julia> StaticString((0x61, 0x62, 0x63, 0x64))
"abcd"

julia> StaticString(Tuple((0x61, 0x62, 0x63)))
"abc"

julia> StaticString(Tuple((0x61, 0x62, 0x63, 0x64)))
"abcd"

Copy link
Member

@quinnj quinnj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent stuff

@quinnj quinnj merged commit 4372407 into main Oct 4, 2022
@quinnj quinnj deleted the npr/from-vec-uint8 branch October 4, 2022 05:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants