You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
For the `ucs4` and `ascii` string datatypes, [`materialized_eltype`](@ref) is a thin `AbstractString` view over the characters ([`UCS4String`](@ref) / [`AsciiString`](@ref); see [`stringify_data`](@ref)). For all other datatypes, `eltype(result) == Type(ndarray.datatype)` and additionally `sizeof(eltype) .* strides(result) == Tuple(reverse(ndarray.strides))`.
794
800
"""
795
801
function Base.getindex(ndarray::NDArray)
796
802
if ndarray.data !==nothing
@@ -831,6 +837,12 @@ function Base.getindex(ndarray::NDArray)
831
837
error("`data` has different stride from `ndarray.strides`")
832
838
end
833
839
840
+
# Present `ucs4`/`ascii` data as `UCS4String`/`AsciiString` rather than raw tuples of
841
+
# codepoints. Done after the layout checks above, which rely on the `isbitstype` tuple
842
+
# representation; the views are a zero-copy `reinterpret` sharing those bytes, so this is
843
+
# display/behavior only.
844
+
data =stringify_data(data, ndarray.datatype)
845
+
834
846
return data::AbstractArray
835
847
end
836
848
@@ -883,6 +895,113 @@ function correct_byteorder(data, ::Ucs4Datatype, byteorder::Byteorder)
883
895
return data
884
896
end
885
897
898
+
"""
899
+
UCS4String{N} <: AbstractString
900
+
901
+
A fixed-width UCS-4 string of `N` characters, as materialized from an [`Ucs4Datatype`](@ref)
902
+
array. It wraps the raw `NTuple{N, UInt32}` codepoints and is `isbitstype` with an identical
903
+
memory layout, so it reinterprets to and from the on-disk block bytes exactly like the tuple
904
+
would. The difference is purely in presentation: a `UCS4String` *displays* and *behaves* as a
905
+
Julia string rather than as raw `UInt32` codepoints.
906
+
907
+
```jldoctest
908
+
julia> s = ASDF.UCS4String((UInt32('h'), UInt32('i'), UInt32(0))) # null-padded
909
+
"hi"
910
+
911
+
julia> s == "hi"
912
+
true
913
+
```
914
+
915
+
As a fixed-width string, trailing null (`\\0`) padding is treated as unused and excluded from
916
+
the string's length and contents. The full `N` codepoints are retained internally so the
917
+
on-disk representation round-trips unchanged.
918
+
919
+
See also: [`AsciiString`](@ref).
920
+
"""
921
+
struct UCS4String{N} <:AbstractString
922
+
codes::NTuple{N, UInt32}
923
+
end
924
+
925
+
"""
926
+
AsciiString{N} <: AbstractString
927
+
928
+
A fixed-width ASCII string of `N` characters, as materialized from an [`AsciiDatatype`](@ref)
929
+
array. The single-byte analogue of [`UCS4String`](@ref): it wraps the raw `NTuple{N, UInt8}`
930
+
bytes and is `isbitstype` with an identical memory layout, reinterpreting to and from the
931
+
on-disk block bytes exactly like the tuple would, while *displaying* and *behaving* as a Julia
932
+
string rather than as raw `UInt8` bytes.
933
+
934
+
```jldoctest
935
+
julia> s = ASDF.AsciiString((UInt8('h'), UInt8('i'), UInt8(0))) # null-padded
936
+
"hi"
937
+
938
+
julia> s == "hi"
939
+
true
940
+
```
941
+
942
+
As with `UCS4String`, trailing null (`\\0`) padding is excluded from the string's length and
943
+
contents, while the full `N` bytes are retained internally so the on-disk representation
944
+
round-trips unchanged.
945
+
"""
946
+
struct AsciiString{N} <:AbstractString
947
+
codes::NTuple{N, UInt8}
948
+
end
949
+
950
+
# Both ASDF string views share the same fixed-width, null-padded, one-codeunit-per-character
951
+
# `AbstractString` interface; only the code-unit type differs.
952
+
const ASDFString = Union{UCS4String, AsciiString}
953
+
954
+
# Number of significant characters, i.e. `N` minus any trailing null padding.
0 commit comments