#![no_std]
#![doc(html_root_url = "https://docs.rs/css-escape/0.1.0")]
#[cfg(doctest)]
#[doc = include_str!("../README.md")]
struct ReadmeDoctests;
extern crate alloc;
use alloc::string::String;
use alloc::vec::Vec;
use core::fmt::Write;
#[must_use]
pub fn escape(value: &str) -> String {
let chars: Vec<char> = value.chars().collect();
let len = chars.len();
let first = chars.first().copied();
let mut out = String::with_capacity(value.len());
for (i, &c) in chars.iter().enumerate() {
match c {
'\0' => out.push('\u{fffd}'),
'\u{1}'..='\u{1f}' | '\u{7f}' => push_hex(&mut out, c),
'0'..='9' if i == 0 => push_hex(&mut out, c),
'0'..='9' if i == 1 && first == Some('-') => push_hex(&mut out, c),
'-' if i == 0 && len == 1 => out.push_str("\\-"),
c if c >= '\u{80}' || c == '-' || c == '_' || c.is_ascii_alphanumeric() => out.push(c),
c => {
out.push('\\');
out.push(c);
}
}
}
out
}
fn push_hex(out: &mut String, c: char) {
let _ = write!(out, "\\{:x} ", c as u32);
}