///|
pub(all) enum TextEncoding {
Utf8
Latin1
} derive(Eq)
///|
pub extend TextEncoding with Eq::{equal, not_equal}
///|
pub struct TextData {
shape : Array[Int64]
width : Int32
values : Array[String]
}
///|
/// Interpret the last NC_CHAR axis as fixed-width strings. Width is in bytes,
/// including for UTF-8. Only trailing NUL bytes are removed by default; embedded
/// NULs and spaces are preserved. Latin-1 is an explicit alternative to UTF-8.
pub fn decode_text(
block : DataBlock,
encoding~ : TextEncoding = Utf8,
trim_spaces~ : Bool = false,
) -> TextData raise CdfError {
if block.data_type != Char8 ||
block.shape.is_empty() ||
block.shape.length() > 16 {
raise Invalid(
"text decoding needs an NC_CHAR block with a final width axis",
)
}
if shape_count(block.shape, 1000000L) != block.values.length().to_int64() {
raise Invalid("character block shape does not match its bytes")
}
let width = block.shape[block.shape.length() - 1]
if width < 1L || width > 1000000L {
raise Invalid("text width must be positive and within the value budget")
}
let shape = block.shape[:block.shape.length() - 1].to_owned()
let rows = shape_count(shape, 1000000L).to_int()
let width = width.to_int()
let strings = Array::makei(rows, row => {
let bytes = Bytes::makei(width, column => {
match block.values[row * width + column] {
Character(byte) => byte
_ => raise Invalid("NC_CHAR block contains a non-character value")
}
})
let mut end = width
while end > 0 && bytes[end - 1] == 0 {
end -= 1
}
if trim_spaces {
while end > 0 && bytes[end - 1] == 32 {
end -= 1
}
}
match encoding {
Utf8 =>
@utf8.decode(bytes[:end]) catch {
_ =>
raise Invalid(
"fixed-width text is not valid UTF-8; choose Latin1 or raw bytes",
)
}
Latin1 =>
String::from_iter(
bytes[:end].iter().map(byte => byte.to_int().unsafe_to_char()),
)
}
})
{ shape, width, values: strings, }
}
///|
/// Encode strings without truncation. shape describes string rows and excludes
/// the final byte width; the returned block includes that last axis.
pub fn encode_text(
shape : Array[Int64],
strings : Array[String],
width : Int32,
encoding~ : TextEncoding = Utf8,
) -> DataBlock raise CdfError {
if shape.length() >= 16 || width < 1 || width > 1000000 {
raise Invalid("text width or rank exceeds the supported range")
}
let rows = shape_count(shape, 1000000L)
if rows != strings.length().to_int64() {
raise Invalid("string count does not match its leading shape")
}
let count = checked_product(rows, width.to_int64(), 1000000L).to_int()
let values = Array::make(count, Character(0))
for row, text in strings {
let bytes = match encoding {
Utf8 => @utf8.encode(text)
Latin1 => {
let characters = text.to_array()
Bytes::makei(characters.length(), index => {
let code = characters[index].to_int()
if code > 255 {
raise Invalid("text contains a character outside Latin-1")
}
code.to_byte()
})
}
}
if bytes.length() > width {
raise Invalid(
"encoded string exceeds its fixed byte width; truncation is not allowed",
)
}
for column, byte in bytes {
values[row * width + column] = Character(byte)
}
}
let output_shape = shape.copy()
output_shape.push(width.to_int64())
{ data_type: Char8, shape: output_shape, values, }
}
///|
pub fn Dataset::text_section(
self : Dataset,
name : String,
start : Array[Int64],
count : Array[Int64],
stride~ : Array[Int64] = [],
encoding~ : TextEncoding = Utf8,
trim_spaces~ : Bool = false,
) -> TextData raise CdfError {
decode_text(
self.section(name, start, count, stride~),
encoding~,
trim_spaces~,
)
}