Skip to content

Commit a7d2bba

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

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

src/staticstring.jl

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ function Base.iterate(x::StaticString{N, UInt8}, i::Int = 1) where {N}
2929
reinterpret(
3030
Char,
3131
(UInt32(cs[i]) << 24) |
32-
(UInt32(cs[i + 1]) << 16) |
32+
(UInt32(cs[i + 1]) << 16) |
3333
(UInt32(cs[i + 2]) << 8),
3434
),
3535
i + 3,
@@ -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(s::AbstractString)
66+
T = codeunit(s)
67+
N = ncodeunits(s)
68+
return StaticString{N,T}(NTuple{N,T}(codeunits(s)))
69+
end

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)