ruby-hdf5 0.0.3 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/hdf5/attribute.rb +25 -10
- data/lib/hdf5/data_helpers.rb +153 -17
- data/lib/hdf5/dataset.rb +125 -64
- data/lib/hdf5/dtype.rb +22 -8
- data/lib/hdf5/ffi.rb +3 -3
- data/lib/hdf5/ffi_10.rb +0 -485
- data/lib/hdf5/ffi_14.rb +0 -479
- data/lib/hdf5/ffi_20.rb +0 -516
- data/lib/hdf5/file.rb +23 -5
- data/lib/hdf5/group.rb +23 -5
- data/lib/hdf5/hierarchy.rb +1 -1
- data/lib/hdf5/selection.rb +17 -1
- data/lib/hdf5/string_codec.rb +31 -18
- data/lib/hdf5/version.rb +1 -1
- metadata +1 -1
data/lib/hdf5/string_codec.rb
CHANGED
|
@@ -21,21 +21,25 @@ module HDF5
|
|
|
21
21
|
buffer_for_values([normalize(value)])
|
|
22
22
|
end
|
|
23
23
|
|
|
24
|
-
def buffer_for_values(values)
|
|
25
|
-
pointers = values.map { |value| ::FFI::MemoryPointer.from_string(normalize(value)) }
|
|
24
|
+
def buffer_for_values(values, encoding: Encoding::UTF_8)
|
|
25
|
+
pointers = values.map { |value| ::FFI::MemoryPointer.from_string(normalize(value, encoding:)) }
|
|
26
26
|
buffer = ::FFI::MemoryPointer.new(:pointer, pointers.length)
|
|
27
27
|
buffer.write_array_of_pointer(pointers)
|
|
28
28
|
[buffer, pointers]
|
|
29
29
|
end
|
|
30
30
|
|
|
31
|
-
def read(buffer)
|
|
32
|
-
|
|
33
|
-
pointer.null? ? nil : pointer.read_string.force_encoding(Encoding::UTF_8)
|
|
31
|
+
def read(buffer, encoding: Encoding::UTF_8)
|
|
32
|
+
read_values(buffer, 1, [], encoding:)
|
|
34
33
|
end
|
|
35
34
|
|
|
36
|
-
def read_values(buffer, count, shape)
|
|
35
|
+
def read_values(buffer, count, shape, encoding: Encoding::UTF_8)
|
|
37
36
|
values = buffer.read_array_of_pointer(count).map do |pointer|
|
|
38
|
-
|
|
37
|
+
next nil if pointer.null?
|
|
38
|
+
|
|
39
|
+
value = pointer.read_string.force_encoding(encoding)
|
|
40
|
+
raise HDF5::Error, "String data is not valid #{encoding.name}" unless value.valid_encoding?
|
|
41
|
+
|
|
42
|
+
value
|
|
39
43
|
end
|
|
40
44
|
return values.first if shape.empty?
|
|
41
45
|
|
|
@@ -55,35 +59,44 @@ module HDF5
|
|
|
55
59
|
!values.empty? && values.all? { |item| item.is_a?(String) }
|
|
56
60
|
end
|
|
57
61
|
|
|
58
|
-
def normalize_data(value)
|
|
59
|
-
return [[normalize(value)], []] if value.is_a?(String)
|
|
60
|
-
return [value.to_a.flatten.map { |item| normalize(item) }, value.shape] if value.is_a?(Numo::RObject)
|
|
62
|
+
def normalize_data(value, encoding: Encoding::UTF_8)
|
|
63
|
+
return [[normalize(value, encoding:)], []] if value.is_a?(String)
|
|
64
|
+
return [value.to_a.flatten.map { |item| normalize(item, encoding:) }, value.shape] if value.is_a?(Numo::RObject)
|
|
61
65
|
|
|
62
66
|
shape = array_shape(value)
|
|
63
|
-
[value.flatten.map { |item| normalize(item) }, shape]
|
|
67
|
+
[value.flatten.map { |item| normalize(item, encoding:) }, shape]
|
|
64
68
|
end
|
|
65
69
|
|
|
66
70
|
def array_shape(value)
|
|
67
71
|
return [] unless value.is_a?(Array)
|
|
68
72
|
raise HDF5::Error, 'String data must not be empty' if value.empty?
|
|
69
73
|
|
|
70
|
-
|
|
71
|
-
raise HDF5::ShapeError, 'String data must be rectangular' unless child_shapes.uniq.length == 1
|
|
72
|
-
|
|
73
|
-
[value.length, *child_shapes.first]
|
|
74
|
+
DataHelpers.array_shape(value)
|
|
74
75
|
end
|
|
75
76
|
|
|
76
77
|
def variable?(type_id)
|
|
77
78
|
HDF5::FFI.H5Tis_variable_str(type_id).positive?
|
|
78
79
|
end
|
|
79
80
|
|
|
80
|
-
def
|
|
81
|
+
def encoding_for(type_id)
|
|
82
|
+
case HDF5::FFI.H5Tget_cset(type_id)
|
|
83
|
+
when :H5T_CSET_ASCII then Encoding::US_ASCII
|
|
84
|
+
when :H5T_CSET_UTF8 then Encoding::UTF_8
|
|
85
|
+
else raise UnsupportedTypeError, 'Unsupported HDF5 string encoding'
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
def normalize(value, encoding: Encoding::UTF_8)
|
|
81
90
|
raise HDF5::Error, 'String dataset data must be a String' unless value.is_a?(String)
|
|
91
|
+
raise HDF5::Error, 'String data has an invalid encoding' unless value.valid_encoding?
|
|
82
92
|
raise HDF5::Error, 'Variable-length strings cannot contain NUL bytes' if value.include?("\0")
|
|
83
93
|
|
|
84
|
-
value.encode(
|
|
94
|
+
result = value.encode(encoding)
|
|
95
|
+
raise HDF5::Error, "String data is not valid #{encoding.name}" unless result.valid_encoding?
|
|
96
|
+
|
|
97
|
+
result
|
|
85
98
|
rescue EncodingError
|
|
86
|
-
raise HDF5::Error,
|
|
99
|
+
raise HDF5::Error, "String data must be representable as #{encoding.name}"
|
|
87
100
|
end
|
|
88
101
|
|
|
89
102
|
def check(status, operation)
|
data/lib/hdf5/version.rb
CHANGED