/// Simple but still over-engineered XML generator for a [`Formatter`], for generating /// Windows Manifest XML. This can easily generate invalid XML. /// /// When used correctly, this should generate the same output as MT’s `-canonicalize` /// option. pubstruct XmlFormatter<'a, 'f> {
f: &'f mut Formatter<'a>,
state: State,
depth: usize,
}
#[derive(Eq, PartialEq)] enum State {
Init,
StartTag,
Text,
}
/// Temporary wrapper for outputting a string with XML attribute encoding. /// This does not do anything with the control characters which are not /// valid in XML, encoded or not. struct Xml<'a>(&'a str);
impl<'a> Display for Xml<'a> { fn fmt(&self, f: &mut Formatter) -> fmt::Result { // Process the string in blocks separated by special characters, so that the parts that // don't need encoding can be written all at once, not character by character, and with // no checks for whether string slices are aligned on character boundaries. for s inself.0.split_inclusive(&['<', '&', '>', '"', '\r'][..]) { // Check whether the last character in the substring needs encoding. This will be // `None` at the end of the input string. letmut iter = s.chars(); let ch = match iter.next_back() {
Some('<') => Some("<"),
Some('&') => Some("&"),
Some('>') => Some(">"),
Some('"') => Some("""),
Some('\r') => Some("
"),
_ => None,
}; // Write the substring except the last character, then the encoded character; // or the entire substring if it is not terminated by a special character. match ch {
Some(enc) => {
f.write_str(iter.as_str())?;
f.write_str(enc)?;
}
None => f.write_str(s)?,
}
}
Ok(())
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.