#![cfg_attr(feature = "alloc", doc = "\
- <code>[sanitise][](input: &str) -> String</code>: the simplest thing to call;
- <code>[sanitise_with_options][](input: &str, options: &Options<_>) -> String</code>:
when you want to tweak the nature of the sanitisation; and")]
#![cfg_attr(not(feature = "alloc"), doc = "\
- <s><code>[sanitise][](input: &str) -> String</code>: the simplest thing to call</s>
*(disabled in this build due to compiling without the `alloc` feature)*;
- <s><code>[sanitise_with_options][](input: &str, options: &Options<_>) -> String</code>:
when you want to tweak the nature of the sanitisation</s> *(disabled in this build due to
compiling without the `alloc` feature)*; and")]
#![cfg_attr(feature = "alloc", doc = " a `String`")]
#![cfg_attr(not(feature = "alloc"), doc = " a string")]
#![cfg_attr(feature = "tinyvec_string", doc = " or [`tinyvec_string::ArrayString`]")]
#![cfg_attr(all(docsrs, feature = "tinyvec_string"), doc = " (when enabled)")]
#![cfg_attr(feature = "tinyvec_string", doc = " or `ArrayString`")]
#![cfg_attr(feature = "std", doc = " <span class='stab portability'><code>std</code></span>")]
#![cfg_attr(feature = "alloc", doc = " <span class='stab portability'><code>alloc</code></span>")]
#![cfg_attr(feature = "tinyvec_string", doc = " <span class='stab portability'><code>tinyvec_string</code></span>")]
#![cfg_attr(feature = "const-fn-trait-bound", doc = " <span class='stab portability'><code>const-fn-trait-bound</code></span>")]
#![cfg_attr(
all(
not(feature = "std"),
not(feature = "alloc"),
not(feature = "tinyvec_string"),
not(feature = "const-fn-trait-bound"),
),
doc = " *(none of them)*")]
#![cfg_attr(not(feature = "std"), doc = " <span class='stab portability'><code>std</code></span>")]
#![cfg_attr(not(feature = "alloc"), doc = " <span class='stab portability'><code>alloc</code></span>")]
#![cfg_attr(not(feature = "tinyvec_string"), doc = " <span class='stab portability'><code>tinyvec_string</code></span>")]
#![cfg_attr(not(feature = "const-fn-trait-bound"), doc = " <span class='stab portability'><code>const-fn-trait-bound</code></span>")]
#![cfg_attr(
all(
feature = "std",
feature = "alloc",
feature = "tinyvec_string",
feature = "const-fn-trait-bound",
),
doc = " *(none of them)*")]
#![cfg_attr(not(feature = "std"), no_std)]
#![cfg_attr(feature = "const-fn-trait-bound", feature(const_fn_trait_bound))]
#![cfg_attr(docsrs, feature(doc_cfg))]
#![cfg_attr(not(feature = "alloc"), allow(rustdoc::broken_intra_doc_links))]
#[cfg(feature = "alloc")]
extern crate alloc;
#[cfg(feature = "alloc")]
use alloc::string::String;
use core::ops::{Deref, Index, Range, RangeFrom, RangeBounds};
#[derive(Debug)]
pub struct Options<R: Replace> {
pub length_limit: usize,
pub reserve_extra: usize,
pub extension_cleverness: bool,
pub most_fs_safe: bool,
pub windows_safe: bool,
pub url_safe: bool,
pub normalise_whitespace: bool,
pub trim_spaces_and_full_stops: bool,
pub trim_more_punctuation: bool,
pub remove_control_characters: bool,
pub remove_reordering_characters: bool,
pub replace_with: R,
pub collapse_replacements: bool,
pub six_measures_of_barley: &'static str,
}
impl Options<Option<char>> {
pub const DEFAULT: Self = Options {
length_limit: 255,
reserve_extra: 0,
extension_cleverness: true,
most_fs_safe: true,
windows_safe: true,
url_safe: false,
normalise_whitespace: true,
trim_spaces_and_full_stops: true,
trim_more_punctuation: true,
remove_control_characters: true,
remove_reordering_characters: true,
replace_with: Some('_'),
collapse_replacements: false,
six_measures_of_barley: "_",
};
}
impl Default for Options<Option<char>> {
fn default() -> Self {
Self::DEFAULT
}
}
impl<R: Replace> Options<R> {
pub fn with_replace_with<R2: Replace>(self, new_replace_with: R2) -> Options<R2> {
Options {
length_limit: self.length_limit,
reserve_extra: self.reserve_extra,
extension_cleverness: self.extension_cleverness,
most_fs_safe: self.most_fs_safe,
windows_safe: self.windows_safe,
url_safe: self.url_safe,
normalise_whitespace: self.normalise_whitespace,
trim_spaces_and_full_stops: self.trim_spaces_and_full_stops,
trim_more_punctuation: self.trim_more_punctuation,
remove_control_characters: self.remove_control_characters,
remove_reordering_characters: self.remove_reordering_characters,
replace_with: new_replace_with,
collapse_replacements: self.collapse_replacements,
six_measures_of_barley: self.six_measures_of_barley,
}
}
}
pub trait Replace {
fn replace(&self, char_being_removed: char) -> Option<char>;
}
impl Replace for Option<char> {
fn replace(&self, _: char) -> Option<char> {
*self
}
}
impl<F: Fn(char) -> Option<char>> Replace for F {
fn replace(&self, c: char) -> Option<char> {
self(c)
}
}
fn is_most_fs_safe_char(c: char) -> bool {
c != '/' && c != '\0'
}
fn is_url_safe_char(c: char) -> bool {
matches!(c,
'A'..='Z' | 'a'..='z' | '0'..='9' |
'!' | '$' | '\'' | '(' | ')' | '*' | '+' | ',' | '-' |
'.' | ':' | ';' | '=' |
'@' | '_' | '~' | '\u{a0}'..='\u{fdcf}' | '\u{fdf0}'..='\u{10fffd}')
&& (c as u32) & 0xfffe != 0xfffe
}
fn is_windows_safe_char(char: char) -> bool {
!matches!(char,
'<' | '>' | ':' | '"' | '/' | '\\' | '|' | '?' | '*' |
'\u{0}'..='\u{1f}' | '\u{7f}')
}
fn is_space_or_full_stop(c: char) -> bool {
matches!(c, ' ' | '.')
}
fn is_more_punctuation_character(c: char) -> bool {
matches!(c, '_' | '-' | ',' | ';')
}
fn is_reordering_character(c: char) -> bool {
matches!(c, '\u{202A}'..='\u{202E}' | '\u{2066}'..='\u{2069}')
}
fn is_reserved_windows_file_name(name: &str) -> bool {
matches!(name.as_bytes(),
| [b'C' | b'c', b'O' | b'o', b'N' | b'n']
| [b'P' | b'p', b'R' | b'r', b'N' | b'n']
| [b'A' | b'a', b'U' | b'u', b'X' | b'x']
| [b'N' | b'n', b'U' | b'u', b'L' | b'l']
| [b'C' | b'c', b'O' | b'o', b'M' | b'm', b'1'..=b'9']
| [b'L' | b'l', b'P' | b'p', b'T' | b't', b'1'..=b'9'])
}
fn split_extension(input: &str) -> Option<(&str, &str)> {
input
.as_bytes()
.iter()
.enumerate()
.rev()
.find(|(_, c)| **c == b'.')
.map(|(dot_index, _)| (&input[..dot_index], &input[dot_index + 1..]))
}
#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
pub fn sanitise(s: &str) -> String {
sanitise_with_options(s, &Options::DEFAULT)
}
pub fn sufficient_alloc_size<R: Replace>(input: &str, options: &Options<R>) -> usize {
if options.length_limit < 10 {
return 0;
}
if options.extension_cleverness {
if let Some((base_name, extension)) = split_extension(input) {
let extension_length_limit = options.length_limit - 6;
let might_add_underscore = |n| {
if (n == 3 || n == 4) && options.windows_safe {
n + '_'.len_utf8()
} else {
n
}
};
return (
might_add_underscore(base_name.len()).min(options.length_limit) +
'.'.len_utf8() +
extension.len().min(extension_length_limit)
).max(
might_add_underscore(input.len())
.min(options.length_limit)
.max(options.six_measures_of_barley.len()) +
options.reserve_extra
)
}
}
(input.len().min(options.length_limit) + if options.windows_safe { '_'.len_utf8() } else { 0 })
.max(options.six_measures_of_barley.len()) + options.reserve_extra
}
const fn max(a: usize, b: usize) -> usize { if a > b { a } else { b } }
macro_rules! max_alloc_size_body {
($options:ident) => {{
if $options.length_limit < 10 {
return 0;
}
let baseline = max($options.length_limit, $options.six_measures_of_barley.len())
+ $options.reserve_extra;
if $options.extension_cleverness {
let extension_length_limit = $options.length_limit - 6;
max($options.length_limit + '.'.len_utf8() + extension_length_limit, baseline)
} else {
baseline
}
}}
}
#[cfg(not(feature = "const-fn-trait-bound"))]
pub fn max_alloc_size<R: Replace>(options: &Options<R>) -> usize {
max_alloc_size_body!(options)
}
#[cfg(feature = "const-fn-trait-bound")]
pub const fn max_alloc_size<R: Replace>(options: &Options<R>) -> usize {
max_alloc_size_body!(options)
}
#[cfg_attr(feature = "const-fn-trait-bound", doc = "\n \
Since you compiled this crate with the `const-fn-trait-bound` feature, you don’t need this
method. Be cheerful and use `max_alloc_size` instead!")]
pub const fn max_alloc_size_const(options: &Options<Option<char>>) -> usize {
max_alloc_size_body!(options)
}
#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
pub fn sanitise_with_options<R: Replace>(input: &str, options: &Options<R>) -> String {
let mut out = String::with_capacity(sufficient_alloc_size(input, options));
#[cfg(test)]
let initial_capacity = out.capacity();
sanitise_to(input, options, &mut out);
#[cfg(test)]
if initial_capacity != out.capacity() {
panic!("Capacity changed from {initial_capacity} to {} (on {:?} → {:?})",
out.capacity(), input, out);
}
out
}
pub trait Stringy:
Index<Range<usize>, Output = str> +
Index<RangeFrom<usize>, Output = str> +
Deref<Target = str> +
Extend<char>
{
fn push(&mut self, ch: char);
fn push_str(&mut self, string: &str);
fn pop(&mut self) -> Option<char>;
fn truncate(&mut self, new_len: usize);
fn replace_range<R>(&mut self, range: R, replace_with: &str) where R: RangeBounds<usize>;
}
#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
impl Stringy for String {
#[inline] fn push(&mut self, ch: char) { self.push(ch) }
#[inline] fn push_str(&mut self, string: &str) { self.push_str(string) }
#[inline] fn pop(&mut self) -> Option<char> { self.pop() }
#[inline] fn truncate(&mut self, new_len: usize) { self.truncate(new_len) }
#[inline] fn replace_range<R>(&mut self, range: R, replace_with: &str)
where R: RangeBounds<usize>
{ self.replace_range(range, replace_with) }
}
#[cfg(feature = "tinyvec_string")]
#[cfg_attr(docsrs, doc(cfg(feature = "tinyvec_string")))]
impl<A: tinyvec_string::bytearray::ByteArray> Stringy for tinyvec_string::ArrayString<A> {
#[inline] fn push(&mut self, ch: char) { self.push(ch) }
#[inline] fn push_str(&mut self, string: &str) { self.push_str(string) }
#[inline] fn pop(&mut self) -> Option<char> { self.pop() }
#[inline] fn truncate(&mut self, new_len: usize) { self.truncate(new_len) }
#[inline] fn replace_range<R>(&mut self, range: R, replace_with: &str)
where R: RangeBounds<usize>
{ self.replace_range(range, replace_with) }
}
pub fn sanitise_to<R: Replace, S: Stringy>(input: &str, options: &Options<R>, out: &mut S) {
let protected = out.len();
if options.length_limit < 10 {
return;
}
#[allow(clippy::never_loop)]
loop { if options.extension_cleverness {
if let Some((base_name, extension)) = split_extension(input) {
let extension_length_limit = options.length_limit - 6;
sanitise_part(base_name, options, options.length_limit, false, out, protected);
let base_len = out.len() - protected;
out.push('.');
let extension_truncated = sanitise_part(
extension,
options,
extension_length_limit,
true,
out,
protected,
);
let mut total_len = out.len() - protected;
if total_len > options.length_limit {
if extension_truncated {
while total_len > options.length_limit {
match out.pop() {
Some(c) => total_len -= c.len_utf8(),
None => unreachable!(),
}
}
out.truncate(protected + trim_end(&out[protected..], options, true).len());
} else {
let base_name_end_index = base_len;
let mut base_chars = out[protected..protected + base_name_end_index].chars();
while total_len > options.length_limit {
match base_chars.next_back() {
Some(c) => {
total_len -= c.len_utf8();
}
None => unreachable!(),
}
}
let base_name = trim_end(
&out[protected..protected + base_chars.as_str().len()],
options,
false,
);
let range = protected + base_name.len()..protected + base_name_end_index;
out.replace_range(range, "");
}
}
break;
}
}
sanitise_part(input, options, options.length_limit, false, out, protected);
break;
}
if (options.url_safe && (&out[protected..] == "." || &out[protected..] == "..")) ||
(options.most_fs_safe && out[protected..].chars().all(|c| c == '.'))
{
out.truncate(protected);
}
if out[protected..].is_empty() {
out.push_str(options.six_measures_of_barley);
}
}
fn sanitise_part<R: Replace, S: Stringy>(
input: &str,
options: &Options<R>,
length_limit: usize,
is_extension: bool,
out: &mut S,
protected: usize,
) -> bool {
let mut len = 0;
let mut did_truncate = false;
let mut last_was_remove = false;
let mut last_was_whitespace = false;
out.extend(input.chars()
.map(|mut c| {
c = if options.normalise_whitespace && c.is_whitespace() { ' ' } else { c };
(c,
(options.most_fs_safe && !is_most_fs_safe_char(c)) ||
(options.windows_safe && !is_windows_safe_char(c)) ||
(options.url_safe && !is_url_safe_char(c)) ||
(options.remove_control_characters && c.is_control()) ||
(options.remove_reordering_characters && is_reordering_character(c))
)
})
.filter_map(|(c, remove)| {
if options.collapse_replacements {
if remove && last_was_remove {
return None;
}
last_was_remove = remove;
}
if remove { options.replace_with.replace(c) } else { Some(c) }
.filter(|&c| {
if options.normalise_whitespace {
let is_whitespace = c == ' ';
let drop = last_was_whitespace && is_whitespace;
last_was_whitespace = is_whitespace;
!drop
} else {
true
}
})
})
.skip_while(|&c| {
(options.trim_spaces_and_full_stops && is_space_or_full_stop(c)) ||
(options.trim_more_punctuation && is_more_punctuation_character(c))
})
.take_while(|&c| {
let new_len = len + c.len_utf8();
if new_len <= length_limit {
len = new_len;
true
} else {
did_truncate = true;
false
}
})
);
if len > 0 {
out.truncate(protected + trim_end(&out[protected..], options, is_extension).len());
}
if !is_extension && options.windows_safe && is_reserved_windows_file_name(&out[protected..]) {
out.push('_');
}
did_truncate
}
fn trim_end<'a, R: Replace>(out: &'a str, options: &Options<R>, is_extension: bool) -> &'a str {
let trim_space_or_full_stop = options.trim_spaces_and_full_stops ||
((is_extension || !options.extension_cleverness) && options.windows_safe);
out.trim_end_matches(|c| {
(trim_space_or_full_stop && is_space_or_full_stop(c)) ||
(options.trim_more_punctuation && is_more_punctuation_character(c))
})
}
#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
pub use sanitise as sanitize;
#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
pub use sanitise_with_options as sanitize_with_options;
pub use sanitise_to as sanitize_to;
#[cfg(feature = "alloc")]
#[test]
fn test_length_limit_things() {
let short = Options {
length_limit: 10,
..Options::DEFAULT
};
assert_eq!(sanitise_with_options("abcdef.ghij", &short), "abcde.ghij");
assert_eq!(sanitise_with_options("abcde.fghij", &short), "abcde.fghi");
assert_eq!(sanitise_with_options("AUX.abcdef", &short), "AUX_.abcd");
assert_eq!(sanitise_with_options("AUX.abcdef", &Options { windows_safe: false, ..short }),
"AUX.abcd");
assert_eq!(sanitise_with_options("lpT7.abcdef", &short), "lpT7_.abcd");
assert_eq!(sanitise_with_options("cOm6.abcdef", &Options { windows_safe: false, ..short }),
"cOm6.abcd");
assert_eq!(sanitise("CON"), "CON_");
assert_eq!(sanitise("aux.h"), "aux_.h");
assert_eq!(sanitise("Lpt1.exe"), "Lpt1_.exe");
assert_eq!(sanitise("xyz"), "xyz");
assert_eq!(sanitise(""), "_");
assert_eq!(sanitise("nül"), "nül");
assert_eq!(sanitise("COM1.jpg.png"), "COM1.jpg.png");
}
#[cfg(feature = "alloc")]
#[test]
fn matrix() {
#[cfg(not(feature = "std"))]
extern crate std;
use std::prelude::rust_2021::*;
use std::fmt::Write;
use std::{eprintln, println, format, vec};
fn case<
R: Replace,
#[cfg(feature = "tinyvec_string")]
A: tinyvec_string::bytearray::ByteArray,
>(
set_name: &'static str,
options_name: &'static str,
options: &Options<R>,
paths: &mut Vec<String>,
unsteady_state: &mut Vec<(&'static str, &'static str, String, String, String)>,
#[cfg(feature = "tinyvec_string")]
_: std::marker::PhantomData<A>,
) {
println!("Sanitising {set_name} with options {options_name}");
#[cfg(feature = "tinyvec_string")]
let mut array_string = tinyvec_string::ArrayString::<A>::new();
let mut sanitised = String::new();
let mut capacity = String::new();
let mut scratch = if options_name == "passthrough" {
String::new()
} else {
String::with_capacity(max_alloc_size(options) + 1)
};
let scratch_size = scratch.capacity();
for input in std::fs::read_to_string(format!("tests/{set_name}.txt")).unwrap().lines() {
let output = sanitise_with_options(input, options);
if output.len() > options.length_limit {
panic!(
"Input {input} sanitised to {output}, which at {len} is greater than the allowed {max}",
len = output.len(),
max = options.length_limit,
);
}
if options.windows_safe && is_reserved_windows_file_name(
options.extension_cleverness.then(|| &*output)
.and_then(split_extension)
.map(|(base, _)| base)
.unwrap_or(&output)
) {
panic!("Input {input} sanitised to {output}, which is a reserved Windows file name");
}
scratch.truncate(0);
scratch.push('.'); sanitise_to(input, options, &mut scratch);
assert_eq!(scratch.chars().next(), Some('.'));
assert_eq!(scratch[1..], output);
#[cfg(feature = "tinyvec_string")]
{
if array_string.capacity() > 0 {
array_string.truncate(0);
array_string.push(' '); sanitise_to(input, options, &mut array_string);
assert_eq!(array_string.chars().next(), Some(' '));
assert_eq!(array_string[1..], output);
}
}
sanitised.push_str(&output);
sanitised.push('\n');
let _ = writeln!(capacity, "{}", output.capacity());
if input != output {
if options_name == "passthrough" {
unsteady_state.push(
(set_name, options_name, input.to_owned(), output.clone(), output),
);
} else {
let repeated = sanitise_with_options(&output, options);
if repeated != output {
sanitised.push_str("⚠ Sanitisation did not reach a steady state. Next line shows the effect of resanitising the line above. ⚠\n");
sanitised.push_str(&repeated);
sanitised.push('\n');
unsteady_state.push(
(set_name, options_name, input.to_owned(), output, repeated),
);
}
}
}
}
let sanitised_name = format!("tests/{set_name}.{options_name}.sanitised");
let capacity_name = format!("tests/{set_name}.{options_name}.capacity");
std::fs::write(&sanitised_name, sanitised).unwrap();
std::fs::write(&capacity_name, capacity).unwrap();
paths.push(sanitised_name);
paths.push(capacity_name);
if options_name != "passthrough" {
assert_eq!(scratch_size, scratch.capacity(), "scratch buffer reallocated");
}
}
let mut unsteady_state = vec![];
let mut paths = vec![];
let d = Options::DEFAULT;
for name in ["blns", "misc"] {
macro_rules! case {
($array_size:literal, $options_name:expr, $options:expr) => {
let options = &$options;
let required_size = max_alloc_size(options).saturating_add(1);
assert!($array_size == 0 || required_size <= $array_size,
"Test case design error: array being given {} bytes, but {} are needed",
$array_size,
required_size
);
case(
name,
$options_name,
options,
&mut paths,
&mut unsteady_state,
#[cfg(feature = "tinyvec_string")]
std::marker::PhantomData::<[u8; $array_size]>,
);
}
}
case!(512, "default", d);
case!(512, "realistic-length_limit-reduction", Options { length_limit: Options::DEFAULT.length_limit - 4, ..d });
case!(512, "url_safe", Options { url_safe: true, ..d });
case!(512, "silly-replace_with", Options::DEFAULT.with_replace_with(|c| char::from_u32(c as u32 + 1)));
case!(512, "no-windows_safe", Options { windows_safe: false, ..d });
case!(256, "no-extension_cleverness", Options { extension_cleverness: false, ..d });
case!(11, "short-sans-extension_cleverness", Options { length_limit: 10, extension_cleverness: false, ..d });
case!(16, "short", Options { length_limit: 10, ..d });
case!(0, "passthrough", Options {
length_limit: usize::MAX,
reserve_extra: 0,
extension_cleverness: false,
most_fs_safe: false,
windows_safe: false,
url_safe: false,
normalise_whitespace: false,
trim_spaces_and_full_stops: false,
trim_more_punctuation: false,
remove_control_characters: false,
remove_reordering_characters: false,
replace_with: None,
collapse_replacements: false,
six_measures_of_barley: "",
});
macro_rules! case_only {
($option:ident) => {{
let mut options = Options {
most_fs_safe: false,
windows_safe: false,
url_safe: false,
normalise_whitespace: false,
trim_spaces_and_full_stops: false,
trim_more_punctuation: false,
remove_control_characters: false,
remove_reordering_characters: false,
..d
};
options.$option = true;
case!(512, concat!("just-", stringify!($option)), options);
}}
}
case_only!(most_fs_safe);
case_only!(windows_safe);
case_only!(url_safe);
case_only!(normalise_whitespace);
case_only!(trim_spaces_and_full_stops);
case_only!(trim_more_punctuation);
case_only!(remove_control_characters);
case_only!(remove_reordering_characters);
}
let mut complain_of_unsteady_states = false;
if !unsteady_state.is_empty() {
for (set, options, original, first, second) in &unsteady_state {
match (*set, *options, &**original, &**first, &**second) {
("blns", "short", "Dr. Herman I. Libshitz", "Dr. Herman", "Dr.Herm") |
("blns", "short", r#"{{ "".__class__.__mro__[2].__subclasses__()[40]("/etc/passwd").read() }}"#, "{{ __.__cl", "{{.cl") => {
},
(_, "silly-replace_with", _, _, _) => {
},
(_, "passthrough", _, _, _) => {
complain_of_unsteady_states = true;
eprintln!("Unsteady state in {set} with {options} options, diff tests/{set}.{options}.txt and tests/{set}.{options}.sanitised");
},
_ => {
complain_of_unsteady_states = true;
eprintln!("Unknown unsteady state in {set} with {options} options, look for the ⚠ symbol in tests/{set}.{options}.sanitised");
},
}
}
}
if !std::process::Command::new("git")
.arg("diff")
.arg("--exit-code")
.arg("--text")
.args(&paths)
.status()
.unwrap()
.success()
{
panic!("sanitisation produced different results than are known, review the diffs");
}
if complain_of_unsteady_states {
panic!("Some sanitisations unexpectedly failed to reach a steady state.");
}
}
#[cfg(feature = "tinyvec_string")]
#[test]
#[should_panic]
fn test_tinyvec_string_panic() {
sanitise_to(
"Watch me panic!",
&Options::DEFAULT,
&mut tinyvec_string::ArrayString::<[u8; 12]>::new(),
);
}
#[cfg(feature = "tinyvec_string")]
#[test]
fn test_tinyvec_string_max_alloc_size() {
use tinyvec_string::ArrayString;
let mut string: ArrayString<[u8; 505]> =
ArrayString::<[u8; max_alloc_size_const(&Options::DEFAULT)]>::new();
assert_eq!(string.capacity(), 505);
sanitise_to(
"Watch everything being hunky dory, even when I throw unreasonably long values at it all. \
Even when dots become extension separators; yes, even then. Then further: into the deep \
reaches of testing, where things start to get garbled, and having written at least 255 \
characters, I have to now write just as much of extension—horror. But that was a full \
stop so that this could now be the extension, and I can’t put a dot for the next while› \
¿Whatever will I do? I suspect things are getting out of hand here, but I can’t stop now; \
I’m running out of things to write, but it should be enough by now!",
&Options::DEFAULT,
&mut string,
);
assert_eq!(string, "Watch everything being hunky dory, even when I throw unreasonably long \
values at it all. Even when dots become extension separators; yes, even then. Then \
further_ into the deep reaches of testing, where things start to get garbled, and having \
written at l");
}