Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ LibDeflate = "9255714d-24a7-4b30-8ea3-d46a97f7e13b"
Mmap = "a63ad114-7e13-5084-954f-fe012c677804"
PrecompileTools = "aea7be01-6a6a-4083-8856-8a6e6704d82a"
ResumableFunctions = "c5292f4c-5179-55e1-98c5-05642aab7184"
StaticStrings = "4db0a0c5-418a-4e1d-8806-cb305fe13294"

[weakdeps]
CommonDataModel = "1fbeeb36-5f17-413c-809b-666fb144f157"
Expand All @@ -30,5 +29,4 @@ LibDeflate = "0.4.3"
Mmap = "1"
PrecompileTools = "1"
ResumableFunctions = "1"
StaticStrings = "0.2.6"
julia = "1.10"
2 changes: 1 addition & 1 deletion src/CommonDataFormat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ using Mmap
using Dictionaries: Dictionary
using ResumableFunctions
using DiskArrays
using StaticStrings: StaticString
using Base.Threads
using CodecZlib: GzipDecompressor, transcode
using LibDeflate
Expand All @@ -22,6 +21,7 @@ export is_record_varying
include("epochs.jl")
include("enums.jl")
include("types.jl")
include("staticstring.jl")
include("parsing.jl")
include("decompress.jl")
include("records/records.jl")
Expand Down
2 changes: 1 addition & 1 deletion src/enums.jl
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,5 @@ const type_map = Dict(

function julia_type(cdf_type, num_elems)
cdf_type = DataType(cdf_type)
return cdf_type in (CDF_CHAR, CDF_UCHAR) ? StaticString{Int(num_elems)} : type_map[cdf_type]
return cdf_type in (CDF_CHAR, CDF_UCHAR) ? StaticString{Int(num_elems), UInt8} : type_map[cdf_type]
end
2 changes: 2 additions & 0 deletions src/epochs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ function Base.show(io::IO, epoch::Epoch16)
end

Base.promote_rule(::Type{<:CDFDateTime}, ::Type{Dates.DateTime}) = Dates.DateTime
Base.promote_rule(::Type{T}, ::Type{Dates.Date}) where {T <: CDFDateTime} = T
# Comment out because of invalidation
Base.convert(::Type{Dates.DateTime}, x::CDFDateTime) = Dates.DateTime(x)
Base.bswap(x::Epoch) = Epoch(Base.bswap(x.instant))
Base.bswap(x::Epoch16) = Epoch16(Base.bswap(x.seconds), Base.bswap(x.picoseconds))
Expand Down
2 changes: 1 addition & 1 deletion src/parsing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ function validate_cdf_magic(magic_bytes)
end

_btye_swap!(data) = map!(ntoh, data, data)
_btye_swap!(data::AbstractArray{StaticString{N}}) where {N} = data
_btye_swap!(data::AbstractArray{<:StaticString{N}}) where {N} = data
# function _btye_swap!(data::AbstractArray{TT2000})
# rd = reinterpret(Int64, data)
# return map!(ntoh, rd, rd)
Expand Down
61 changes: 61 additions & 0 deletions src/staticstring.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# https://github.com/mkitti/StaticStrings.jl
# https://github.com/JuliaPy/PythonCall.jl/blob/main/src/Utils/Utils.jl

struct StaticString{N, T} <: AbstractString
codeunits::NTuple{N, T}
StaticString{N, T}(codeunits::NTuple{N, T}) where {N, T} = new{N, T}(codeunits)
end

function Base.iterate(x::StaticString{N, UInt8}, i::Int = 1) where {N}
i > N && return
cs = x.codeunits
c = @inbounds cs[i]
if all(iszero, (cs[j] for j in i:N))
return
elseif (c & 0x80) == 0x00
return (reinterpret(Char, UInt32(c) << 24), i + 1)
elseif (c & 0x40) == 0x00
nothing
elseif (c & 0x20) == 0x00
if @inbounds (i ≤ N - 1) && ((cs[i + 1] & 0xC0) == 0x80)
return (
reinterpret(Char, (UInt32(cs[i]) << 24) | (UInt32(cs[i + 1]) << 16)),
i + 2,
)
end
elseif (c & 0x10) == 0x00
if @inbounds (i ≤ N - 2) && ((cs[i + 1] & 0xC0) == 0x80) && ((cs[i + 2] & 0xC0) == 0x80)
return (
reinterpret(
Char,
(UInt32(cs[i]) << 24) |
(UInt32(cs[i + 1]) << 16) |
(UInt32(cs[i + 2]) << 8),
),
i + 3,
)
end
elseif (c & 0x08) == 0x00
if @inbounds (i ≤ N - 3) &&
((cs[i + 1] & 0xC0) == 0x80) &&
((cs[i + 2] & 0xC0) == 0x80) &&
((cs[i + 3] & 0xC0) == 0x80)
return (
reinterpret(
Char,
(UInt32(cs[i]) << 24) |
(UInt32(cs[i + 1]) << 16) |
(UInt32(cs[i + 2]) << 8) |
UInt32(cs[i + 3]),
),
i + 4,
)
end
end
throw(StringIndexError(x, i))
end

function Base.String(x::StaticString{N, T}) where {N, T}
b = Base.StringVector(N)
return String(b .= x.codeunits)
end
13 changes: 8 additions & 5 deletions test/comprehensive_test.jl
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
# Comprehensive test based on the Python CDFpp test.py
# Tests all variables in a_cdf.cdf for expected shapes, types, values, and attributes
using Test
using CommonDataFormat
import CommonDataFormat as CDF
using Dates

include("utils.jl")

"""
Comprehensive test based on the Python CDFpp test.py
Tests all variables in a_cdf.cdf for expected shapes, types, values, and attributes
"""

# Expected variable definitions (translated from Python test.py)
const EXPECTED_VARIABLES = Dict(
"epoch" => (
Expand Down Expand Up @@ -129,6 +126,12 @@ ds = CDFDataset(file)
@test Set(keys(ds)) == Set(keys(EXPECTED_VARIABLES))
end

@testset "StaticString" begin
using CommonDataFormat: StaticString
@test typeof(ds["var_string"][1]) == StaticString{16, UInt8}
@test String(ds["var_string"][1]) == "This is a string"
end

@testset "DateTime Conversions" begin
for var in ("epoch", "epoch16", "tt2000")
@testset "Variable: $var" begin
Expand Down
Loading