Skip to content

Commit 3b31fef

Browse files
author
Christopher Doris
committed
more efficient convert to StaticString
1 parent d40eeff commit 3b31fef

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

src/utils.jl

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -180,12 +180,13 @@ module Utils
180180
end
181181

182182
function Base.convert(::Type{StaticString{T,N}}, x::AbstractString) where {T,N}
183-
cs = collect(transcode(T, convert(String, x)))
184-
length(cs) > N && throw(InexactError(:convert, StaticString{T,N}, x))
185-
while length(cs) < N
186-
push!(cs, 0)
187-
end
188-
StaticString{T,N}(NTuple{N,T}(cs))
183+
ts = transcode(T, convert(String, x))
184+
n = length(ts)
185+
n > N && throw(InexactError(:convert, StaticString{T,N}, x))
186+
n > 0 && iszero(ts[n]) && throw(InexactError(:convert, StaticString{T,N}, x))
187+
z = zero(T)
188+
cs = ntuple(i -> i > n ? z : @inbounds(ts[i]), N)
189+
StaticString{T,N}(cs)
189190
end
190191

191192
StaticString{T,N}(x::AbstractString) where {T,N} = convert(StaticString{T,N}, x)

0 commit comments

Comments
 (0)