[−][src]Trait truncrate::TruncateToBoundary
Required methods
fn truncate_to_boundary(&self, chars: usize) -> &Self
fn truncate_to_byte_offset(&self, count: usize) -> &Self
fn slice_indices_at_boundary(&self, boundary: usize) -> (&Self, usize)
fn slice_indices_at_offset(&self, offset: usize) -> (&Self, usize)
Implementations on Foreign Types
impl TruncateToBoundary for str
[src]
fn truncate_to_boundary(&self, chars: usize) -> &Self
[src]
Truncates a given string to a set numerical boundary. If the boundary splits a grapheme (e.g., when a character is a resultant mix of more than 1 utf-8 character, like some emojis) the truncation will scale back to the previous character. If the truncation ends with white space - this will be trimmed. Should the truncation boundary exceed the string's size - the original string will return (including whitespace).
Examples:
use truncrate::*; let s = "🤚🏾a🤚🏾 "; assert_eq!(s.truncate_to_boundary(1), ""); assert_eq!(s.truncate_to_boundary(2), "🤚🏾"); assert_eq!(s.truncate_to_boundary(3), "🤚🏾a"); assert_eq!(s.truncate_to_boundary(4), "🤚🏾a"); assert_eq!(s.truncate_to_boundary(5), "🤚🏾a🤚🏾"); assert_eq!(s.truncate_to_boundary(10), s);
fn truncate_to_byte_offset(&self, boundary: usize) -> &Self
[src]
Truncates a given string based on the provided byte-offset. If the offset splits a grapheme the truncation will scale back to the previous character. If the truncation ends with white space - this will be trimmed. Should the offset exceed the strings size - the original string will return (including whitespace).
Examples:
use truncrate::*; let s = "🤚🏾a🤚 "; // where "🤚🏾" = 8 bytes assert_eq!(s.truncate_to_byte_offset(0), ""); assert_eq!(s.truncate_to_byte_offset(7), ""); assert_eq!(s.truncate_to_byte_offset(8), "🤚🏾"); assert_eq!(s.truncate_to_byte_offset(9), "🤚🏾a"); assert_eq!(s.truncate_to_byte_offset(10), "🤚🏾a"); assert_eq!(s.truncate_to_byte_offset(18), s);
fn slice_indices_at_boundary(&self, boundary: usize) -> (&Self, usize)
[src]
The same as 'truncate_to_boundary' but returns a tuple with the desired slice along with the byte-offset.
Examples:
use truncrate::*; let s = "🤚🏾a🤚🏾 "; assert_eq!(s.slice_indices_at_boundary(2), ("🤚🏾", 8));
fn slice_indices_at_offset(&self, boundary: usize) -> (&Self, usize)
[src]
The same as 'truncate_to_byte_offset' but returns a tuple with the desired slice along with the byte-offset. assert_eq!(s.truncate_to_byte_offset(8), "🤚🏾");
Examples:
use truncrate::*; let s = "🤚🏾🤚🏾 "; assert_eq!(s.slice_indices_at_offset(9), ("🤚🏾", 8));