Skip to content

Commit a8a85ed

Browse files
authored
feat: add more methods for staticstring (#35)
1 parent 2bc89c7 commit a8a85ed

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-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: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using CommonDataFormat: StaticString
2+
3+
@testset "StaticString" begin
4+
s = "Hello, World!"
5+
ss = StaticString(s)
6+
7+
StaticString(codeunits(s))
8+
@test ss == s
9+
@test String(ss) == s
10+
@test !isempty(ss)
11+
end

0 commit comments

Comments
 (0)