1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
//---------------------------------------------------------------------------------------------------- Impl
impl<T: AsRef<str>> HeadTail for T {}
//---------------------------------------------------------------------------------------------------- Head
/// Head/Tail characters of a [`str`]
///
/// This trait provides some functionality for
/// cutting off a string either by the head, tail,
/// or both, with optional `...` after/before/in-between.
///
/// Anything that implements `AsRef<str>` can use this trait.
pub trait HeadTail: AsRef<str> {
/// Return the first `len` bytes of this [`str`].
///
/// This will return the full [`str`] if the `len` is
/// longer than the actual inner [`str`].
///
/// ## Note
/// Input is split by [`char`]'s, not bytes.
fn head(&self, len: usize) -> &str {
let s = self.as_ref();
if let Some((index, _)) = s.char_indices().nth(len) {
&s[..index]
} else {
s
}
}
/// Same as [`head()`] but the [`String`] ends with `...`
///
/// This will return the full string without `...` if
/// the `len` is longer than the actual inner [`str`].
///
/// ## Note
/// Input is split by [`char`]'s, not bytes.
fn head_dot(&self, len: usize) -> String {
let s = self.as_ref();
if let Some((index, _)) = s.char_indices().nth(len) {
format!("{}...", &s[..index])
} else {
String::from(s)
}
}
/// Return the last `len` bytes of this [`str`].
///
/// This will return the full [`str`] if the `len` is
/// longer than the actual inner [`str`].
///
/// ## Note
/// Input is split by [`char`]'s, not bytes.
fn tail(&self, len: usize) -> &str {
let s = self.as_ref();
let end = s.chars().count();
if len >= end {
return s;
}
if let Some((index, _)) = s.char_indices().nth(end - len) {
return &s[index..];
}
s
}
/// Same as [`tail()`] but returns a [`String`] starting with `...`
///
/// This will return the full string without `...` if
/// the `len` is longer than the actual inner [`str`].
///
/// ## Note
/// Input is split by [`char`]'s, not bytes.
fn tail_dot(&self, len: usize) -> String {
let s = self.as_ref();
let end = s.chars().count();
if len >= end {
format!("...{s}");
}
if let Some((index, _)) = s.char_indices().nth(end - len) {
return format!("...{}", &s[index..]);
}
format!("...{s}")
}
/// Return the first `head` bytes and last `tail`
/// bytes of this string separated with `...`.
///
/// ## Note
/// Input is split by [`char`]'s, not bytes.
fn head_tail(&self, head: usize, tail: usize) -> String {
let s = self.as_ref();
let end = s.chars().count();
if head >= end || tail >= end || head + tail >= end {
return String::from(s);
}
// Iterator is consumed, must create twice.
let head = s.char_indices().nth(head);
let tail = s.char_indices().nth(end - tail);
if let (Some((head, _)), Some((tail, _))) = (head, tail) {
return format!("{}...{}", &s[..head], &s[tail..]);
}
String::from(s)
}
}
//---------------------------------------------------------------------------------------------------- TESTS
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn _head() {
let four_bytes = "desu";
let six_bytes = "です";
assert!(four_bytes.len() == 4);
assert!(six_bytes.len() == 6);
assert!(four_bytes.head(2) == "de");
assert!(six_bytes.head(1) == "で");
}
#[test]
fn _head_dot() {
let four_bytes = "desu";
let six_bytes = "です";
assert!(four_bytes.len() == 4);
assert!(six_bytes.len() == 6);
assert!(four_bytes.head_dot(2) == "de...");
assert!(six_bytes.head_dot(1) == "で...");
}
#[test]
fn _tail() {
let four_bytes = "desu";
let six_bytes = "です";
assert!(four_bytes.len() == 4);
assert!(six_bytes.len() == 6);
assert!(four_bytes.tail(2) == "su");
assert!(six_bytes.tail(1) == "す");
}
#[test]
fn _tail_dot() {
let four_bytes = "desu";
let six_bytes = "です";
assert!(four_bytes.len() == 4);
assert!(six_bytes.len() == 6);
assert!(four_bytes.tail_dot(2) == "...su");
assert!(six_bytes.tail_dot(1) == "...す");
}
#[test]
fn _head_tail() {
let eight_bytes = "desukedo";
let twelve_bytes = "ですけど";
assert!(eight_bytes.len() == 8);
assert!(twelve_bytes.len() == 12);
assert!(eight_bytes.head_tail(2, 2) == "de...do");
assert!(twelve_bytes.head_tail(1, 1) == "で...ど");
assert!(eight_bytes.head_tail(4, 4) == "desukedo");
assert!(twelve_bytes.head_tail(2, 2) == "ですけど");
}
}