Skip to content

Commit dbf9ecb

Browse files
committed
feat: add more methods for staticstring
1 parent 2bc89c7 commit dbf9ecb

File tree

3 files changed

+17
-0
lines changed

3 files changed

+17
-0
lines changed

src/staticstring.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,11 @@ function Base.String(x::StaticString{N, T}) where {N, T}
5959
b = Base.StringVector(N)
6060
return String(b .= x.codeunits)
6161
end
62+
63+
@inline Base.ncodeunits(::StaticString{N}) where N = N
64+
65+
function StaticString(cu::Base.CodeUnits{T}) where T
66+
N = length(cu)
67+
return StaticString{N, T}(NTuple{N, T}(cu))
68+
end
69+
StaticString(s::AbstractString) = StaticString(codeunits(s))

test/runtests.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ include("epochs_test.jl")
77
include("comprehensive_test.jl")
88
include("cdf2_test.jl")
99
include("CommonDataModelExt_test.jl")
10+
include("staticstring.jl")
1011

1112
@testset "Aqua" begin
1213
using Aqua

test/staticstring.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
using CommonDataFormat: StaticString
2+
@testset "StaticString" begin
3+
s = "Hello, World!"
4+
ss = StaticString(s)
5+
@test ss == s
6+
@test String(ss) == s
7+
@test !isempty(ss)
8+
end

0 commit comments

Comments
 (0)