crate-cli 11.0.0

A command-line tool for managing Cargo package lifecycles: version bump, workspace dependency sync, members-ordered publish and code formatting.
Documentation
use super::*;

/// Normalizes CRLF and lone CR line endings to LF (`\n`) in a single pass.
///
/// # Arguments
///
/// - `&str` - Raw input text with arbitrary line endings.
///
/// # Returns
///
/// - `String` - Text with LF-only line endings.
#[inline]
pub fn normalize_line_endings(input: &str) -> String {
    let bytes: &[u8] = input.as_bytes();
    let Some(first_cr) = bytes.iter().position(|&b: &u8| b == b'\r') else {
        return input.to_string();
    };
    let mut out: String = String::with_capacity(input.len());
    out.push_str(&input[..first_cr]);
    let mut idx: usize = first_cr;
    let len: usize = bytes.len();
    while idx < len {
        if bytes[idx] == b'\r' {
            out.push(TEXT_LF);
            if idx + 1 < len && bytes[idx + 1] == b'\n' {
                idx += 2;
            } else {
                idx += 1;
            }
            let start: usize = idx;
            while idx < len && bytes[idx] != b'\r' {
                idx += 1;
            }
            if idx > start {
                out.push_str(&input[start..idx]);
            }
        } else {
            idx += 1;
        }
    }
    out
}

/// Formats generic plain text files with LF line endings, trimmed trailing whitespace, and a single trailing newline.
///
/// # Arguments
///
/// - `&str` - Raw file content.
/// - `bool` - When `true`, preserves leading tabs (for example in `Makefile`) while stripping trailing whitespace.
///
/// # Returns
///
/// - `String` - Normalized text content.
pub fn format_plain_text(input: &str, preserve_tabs: bool) -> String {
    if input.is_empty() {
        return String::new();
    }
    let bytes: &[u8] = input.as_bytes();
    let len: usize = bytes.len();
    let mut out: String = String::with_capacity(len + 1);
    let mut last_non_empty_end: usize = 0;
    let mut start: usize = 0;
    let mut idx: usize = 0;
    let mut line_idx: usize = 0;
    loop {
        while idx < len && bytes[idx] != b'\n' && bytes[idx] != b'\r' {
            idx += 1;
        }
        let raw_line: &str = &input[start..idx];
        let trimmed: &str = raw_line.trim_end_matches([TEXT_SPACE, TEXT_TAB]);
        if line_idx > 0 {
            out.push(TEXT_LF);
        }
        if !trimmed.is_empty() {
            if preserve_tabs || !trimmed.starts_with(TEXT_TAB) {
                out.push_str(trimmed);
            } else {
                let tab_count: usize = trimmed
                    .as_bytes()
                    .iter()
                    .take_while(|&&b: &&u8| b == b'\t')
                    .count();
                push_spaces(&mut out, tab_count * PRETTIER_TAB_WIDTH);
                out.push_str(&trimmed[tab_count..]);
            }
            last_non_empty_end = out.len();
        }
        if idx >= len {
            break;
        }
        if bytes[idx] == b'\r' && idx + 1 < len && bytes[idx + 1] == b'\n' {
            idx += 2;
        } else {
            idx += 1;
        }
        start = idx;
        line_idx += 1;
    }
    if last_non_empty_end == 0 {
        return String::new();
    }
    out.truncate(last_non_empty_end);
    out.push(TEXT_LF);
    out
}

/// Computes the terminal/Prettier column display width of a single Unicode scalar value.
///
/// # Arguments
///
/// - `char` - Unicode character to measure.
///
/// # Returns
///
/// - `usize` - Display width (`0` for combining marks, `2` for East Asian wide/fullwidth/emoji characters, `1` otherwise).
#[inline(always)]
pub fn char_display_width(ch: char) -> usize {
    let code: u32 = ch as u32;
    if (0x20..0x7F).contains(&code) {
        return 1;
    }
    if code < 0x20 || (0x7F..0xA0).contains(&code) {
        return 0;
    }
    if (0x0300..=0x036F).contains(&code)
        || (0x1AB0..=0x1AFF).contains(&code)
        || (0x1DC0..=0x1DFF).contains(&code)
        || (0x20D0..=0x20FF).contains(&code)
        || (0xFE00..=0xFE0F).contains(&code)
        || (0xFE20..=0xFE2F).contains(&code)
        || code == 0x200D
    {
        return 0;
    }
    if is_wide_codepoint(code) {
        return 2;
    }
    1
}

/// Computes the Prettier column display width of a string (CJK and emoji wide characters count as 2).
///
/// # Arguments
///
/// - `&str` - String slice to measure.
///
/// # Returns
///
/// - `usize` - Total column display width.
#[inline]
pub fn str_display_width(text: &str) -> usize {
    let bytes: &[u8] = text.as_bytes();
    if text.is_ascii() {
        let mut width: usize = 0;
        for &b in bytes {
            width += usize::from((0x20..0x7F).contains(&b));
        }
        return width;
    }
    let len: usize = bytes.len();
    let mut width: usize = 0;
    let mut idx: usize = 0;
    while idx < len {
        let b: u8 = bytes[idx];
        if b < 0x80 {
            width += usize::from((0x20..0x7F).contains(&b));
            idx += 1;
            continue;
        }
        let rest: &str = &text[idx..];
        if let Some(ch) = rest.chars().next() {
            width += char_display_width(ch);
            idx += ch.len_utf8();
        } else {
            break;
        }
    }
    width
}

/// Returns `true` when `code` falls inside East Asian Wide, Fullwidth, or wide Emoji Unicode ranges.
///
/// # Arguments
///
/// - `u32` - Unicode code point.
///
/// # Returns
///
/// - `bool` - Whether the character occupies two display columns.
#[inline(always)]
fn is_wide_codepoint(code: u32) -> bool {
    (0x1100..=0x115F).contains(&code)
        || (0x2329..=0x232A).contains(&code)
        || (0x2E80..=0x303E).contains(&code)
        || (0x3040..=0xA4CF).contains(&code)
        || (0xAC00..=0xD7A3).contains(&code)
        || (0xF900..=0xFAFF).contains(&code)
        || (0xFE10..=0xFE19).contains(&code)
        || (0xFE30..=0xFE6F).contains(&code)
        || (0xFF00..=0xFF60).contains(&code)
        || (0xFFE0..=0xFFE6).contains(&code)
        || (0x1F300..=0x1FAFF).contains(&code)
        || (0x20000..=0x2FFFD).contains(&code)
        || (0x30000..=0x3FFFD).contains(&code)
}

/// Repeats spaces `count` times into `out`.
///
/// # Arguments
///
/// - `&mut String` - Destination string buffer.
/// - `usize` - Number of ASCII space characters to append.
#[inline(always)]
pub(crate) fn push_spaces(out: &mut String, count: usize) {
    let mut remaining: usize = count;
    while remaining >= TEXT_SPACES_CHUNK.len() {
        out.push_str(TEXT_SPACES_CHUNK);
        remaining -= TEXT_SPACES_CHUNK.len();
    }
    if remaining > 0 {
        out.push_str(&TEXT_SPACES_CHUNK[..remaining]);
    }
}