Render one escaped `key: value` record per line. Empty strings are quoted so an explicitly empty argument stays distinct from an absent record. Quotes are escaped for the same reason.
(kv: &[(&str, String)])
| 235 | /// Empty strings are quoted so an explicitly empty argument stays distinct |
| 236 | /// from an absent record. Quotes are escaped for the same reason. |
| 237 | fn emit(kv: &[(&str, String)]) -> String { |
| 238 | use std::fmt::Write as _; |
| 239 | |
| 240 | let mut out = String::new(); |
| 241 | for (key, value) in kv { |
| 242 | out.push_str(key); |
| 243 | out.push_str(": "); |
| 244 | if value.is_empty() { |
| 245 | out.push_str("\"\"\n"); |
| 246 | continue; |
| 247 | } |
| 248 | for ch in value.chars() { |
| 249 | match ch { |
| 250 | '\\' => out.push_str("\\\\"), |
| 251 | '"' => out.push_str("\\\""), |
| 252 | '\n' => out.push_str("\\n"), |
| 253 | '\r' => out.push_str("\\r"), |
| 254 | '\t' => out.push_str("\\t"), |
| 255 | ch if ch.is_control() || is_invisible_format(ch) => { |
| 256 | let _ = write!(out, "\\u{{{:x}}}", ch as u32); |
| 257 | } |
| 258 | ch => out.push(ch), |
| 259 | } |
| 260 | } |
| 261 | out.push('\n'); |
| 262 | } |
| 263 | out |
| 264 | } |
| 265 | |
| 266 | /// Characters that can alter visual order or create invisible ambiguity while |
| 267 | /// remaining legal Unicode. Keep ordinary non-ASCII text readable. |