///|
/// Owned, validated key and translation bytes. Plural originals and NUL-delimited translation slots retain their original encoding.
/// Example: `entry.plural_orig_bytes`.
#derive(Eq, @debug.Debug)
pub struct RawEntry {
key_bytes : Bytes
plural_orig_bytes : Bytes?
trans_bytes : Bytes
}
///|
/// Validated revision-0 header, strictly sorted entries and extracted metadata.
/// Example: `file.entries.length()`.
#derive(Eq, @debug.Debug)
pub struct MoFile {
header : MoHeader
entries : Array[RawEntry]
metadata : MoMetadata
}
///|
fn split_original(
bytes : BytesView,
index : Int32,
) -> (Bytes, Bytes?) raise MoError {
guard bytes.find(b"\x00") is Some(first) else {
return (bytes.to_owned(), None)
}
let plural = bytes[first + 1:]
let mut count = 1
for byte in plural {
if byte == 0 {
count += 1
}
}
if count > 1 {
raise MalformedPluralOriginal(index~, nul_count=count)
}
(bytes[:first].to_owned(), Some(plural.to_owned()))
}
///|
/// Read revision 0.0 in either byte order. Invalid headers, ranges, terminators, keys or encodings raise MoError.
/// Example: `@mo.parse_mo(bytes)`.
pub fn parse_mo(bytes : Bytes) -> MoFile raise MoError {
let header = read_header(bytes)
let count = header.num_strings.reinterpret_as_int()
let metadata = if count > 0 &&
read_string(bytes, header, Original, 0).is_empty() {
read_metadata(Some(read_string(bytes, header, Translation, 0)))
} else {
read_metadata(None)
}
let entries : Array[RawEntry] = []
for i in 0.. 0 {
let cmp = entries[i - 1].key_bytes.lexical_compare(key)
if cmp == 0 {
raise DuplicateOriginalKey(index=i)
}
if cmp > 0 {
raise UnsortedOriginalTable(index=i)
}
}
entries.push({
key_bytes: key,
plural_orig_bytes: plural,
trans_bytes: trans.to_owned(),
})
}
{ header, entries, metadata, }
}
///|
/// Write the diagnostic representation of RawEntry to a logger.
/// Example: `value.to_string()`.
pub impl Show for RawEntry with fn output(self, logger) {
logger.write_string(@debug.to_string(self))
}
///|
/// Write the diagnostic representation of MoFile to a logger.
/// Example: `value.to_string()`.
pub impl Show for MoFile with fn output(self, logger) {
logger.write_string(@debug.to_string(self))
}