artifactize-tools 0.27.1

Portable scoped tools and safe file access for artifactize.
Documentation
//! Decode native command output before interpreting terminal controls.

use std::borrow::Cow;

/// A program's captured output as text: UTF-8 or Windows UTF-16, its CRLF line endings as `\n`.
pub fn output(bytes: &[u8]) -> Cow<'_, str> {
    let text = decode(bytes);
    if text.contains("\r\n") {
        Cow::Owned(text.replace("\r\n", "\n"))
    } else {
        text
    }
}

/// The UTF-8 byte-order mark some Windows programs write before their output; it is no text.
const UTF8_BOM: &[u8] = b"\xef\xbb\xbf";

fn decode(bytes: &[u8]) -> Cow<'_, str> {
    #[cfg(windows)]
    if let Some(text) = utf16_output(bytes) {
        return Cow::Owned(text);
    }
    String::from_utf8_lossy(bytes.strip_prefix(UTF8_BOM).unwrap_or(bytes))
}

/// BOM-less UTF-16LE is recognized only when more than this share of its code units are ASCII
/// characters with a zero high byte: cmd's Unicode mode writes mostly ASCII text that way, and
/// a lower share would mistake ordinary bytes for UTF-16.
#[cfg(windows)]
const UTF16_ASCII_MAJORITY_DIVISOR: usize = 2;

/// One UTF-16 code unit is two bytes; output is read in such chunks.
#[cfg(windows)]
const UTF16_UNIT_BYTES: usize = 2;

/// The UTF-16 byte-order marks; the one a program writes selects how its output decodes.
#[cfg(windows)]
const UTF16_LE_BOM: &[u8] = b"\xff\xfe";
#[cfg(windows)]
const UTF16_BE_BOM: &[u8] = b"\xfe\xff";

#[cfg(windows)]
fn utf16_output(bytes: &[u8]) -> Option<String> {
    let (bytes, little_endian) = if let Some(bytes) = bytes.strip_prefix(UTF16_LE_BOM) {
        (bytes, true)
    } else if let Some(bytes) = bytes.strip_prefix(UTF16_BE_BOM) {
        (bytes, false)
    } else {
        // cmd's Unicode mode can emit BOM-less UTF-16LE. Recognize the
        // alternating NULs in its ASCII text without guessing on arbitrary bytes.
        let (units, _) = bytes.as_chunks::<UTF16_UNIT_BYTES>();
        let ascii = units
            .iter()
            .filter(|[low, high]| *low != 0 && low.is_ascii() && *high == 0)
            .count();
        if ascii <= units.len() / UTF16_ASCII_MAJORITY_DIVISOR || units.contains(&[0, 0]) {
            return None;
        }
        (bytes, true)
    };
    let (units, rest) = bytes.as_chunks::<UTF16_UNIT_BYTES>();
    let byte_order = if little_endian {
        u16::from_le_bytes
    } else {
        u16::from_be_bytes
    };
    Some(
        char::decode_utf16(units.iter().copied().map(byte_order))
            .map(|character| character.unwrap_or(char::REPLACEMENT_CHARACTER))
            .chain((!rest.is_empty()).then_some(char::REPLACEMENT_CHARACTER))
            .collect(),
    )
}

#[cfg(test)]
mod tests;