}
pos = peek_pos;
- description = format!("expected `'}}'`, found `{maybe:?}`");
+ description = format!("expected `'}}'`, found `{:?}`", maybe);
} else {
description = "expected `'}'` but string was terminated".to_owned();
// point at closing `"`
// fill character
if let Some(&(idx, c)) = self.cur.peek() {
- if let Some((_, '>' | '<' | '^')) = self.cur.clone().nth(1) {
- spec.fill = Some(c);
- spec.fill_span = Some(self.span(idx, idx + 1));
- self.cur.next();
+ match self.cur.clone().nth(1) {
+ Some((_, '>')) | Some((_, '<')) | Some((_, '^')) => {
+ spec.fill = Some(c);
+ spec.fill_span = Some(self.span(idx, idx + 1));
+ self.cur.next();
+ }
+ _ => {}
}
}
+
// Alignment
if self.consume('<') {
spec.align = AlignLeft;
);
}
- found.then_some(cur)
+ if found {
+ Some(cur)
+ } else {
+ None
+ }
}
fn suggest_format(&mut self) {
// Alternatively, we could just count the trailing newlines and only trim one from the input if they don't match up.
let input_no_nl = input.trim_end_matches('\n');
let unescaped = match unescape_string(snippet) {
- Some(unescaped) => unescaped,
- _ => {
- return InputStringKind::NotALiteral;
- }
+ Some(u) => u,
+ None => return InputStringKind::NotALiteral,
};
let unescaped_no_nl = unescaped.trim_end_matches('\n');
width_mappings.push(InnerWidthMapping::new(pos, width, 0));
}
- ('\\', Some((_, 'n' | 't' | 'r' | '0' | '\\' | '\'' | '\"'))) => {
+ ('\\', Some((_, 'n')))
+ | ('\\', Some((_, 't')))
+ | ('\\', Some((_, 'r')))
+ | ('\\', Some((_, '0')))
+ | ('\\', Some((_, '\\')))
+ | ('\\', Some((_, '\'')))
+ | ('\\', Some((_, '\"'))) => {
width_mappings.push(InnerWidthMapping::new(pos, 2, 1));
let _ = s.next();
}
.as_str()
.get(..digits_len)
.and_then(|digits| u32::from_str_radix(digits, 16).ok())
- .and_then(char::from_u32)
+ .and_then(std::char::from_u32)
.map_or(1, char::len_utf8);
// Skip the digits, for chars that encode to more than 1 utf-8 byte
let buf = string::String::from(string);
let ok = true;
- ok.then_some(buf)
+ if ok {
+ Some(buf)
+ } else {
+ None
+ }
}
// Assert a reasonable size for `Piece`
use std::ffi::CStr;
-// Local replacement for 1.72.0+ method 'leak' for struct 'std::string::String',
-// <https://doc.rust-lang.org/1.72.0/src/alloc/string.rs.html#1853>
-fn leak_string<'a>(s: String) -> &'a mut str {
- let slice = s.into_bytes().leak();
- unsafe { std::str::from_utf8_unchecked_mut(slice) }
+trait StringLeakExt {
+ fn leak<'a>(self) -> &'a mut str;
+}
+
+impl StringLeakExt for String {
+ fn leak<'a>(self) -> &'a mut str {
+ Box::leak(self.into_boxed_str())
+ }
}
trait IntoFFI<T> {
let rust_string = RustString {
len: str.len(),
cap: str.capacity(),
- ptr: leak_string(str).as_ptr(),
+ ptr: str.leak().as_ptr(),
};
FormatArgsHandle(piece_slice, rust_string)
let cloned_s = s.clone();
// FIXME: Documentation
- leak_string(s);
+ s.leak();
let rust_string = RustString {
len: cloned_s.len(),
cap: cloned_s.capacity(),
- ptr: leak_string(cloned_s).as_ptr(),
+ ptr: cloned_s.leak().as_ptr(),
};
FormatArgsHandle(piece_slice, rust_string)