[][src]Trait truncrate::TruncateToBoundary

pub trait TruncateToBoundary {
    fn truncate_to_boundary(&self, chars: usize) -> &Self;
fn truncate_to_byte_offset(&self, count: usize) -> &Self; }

Required methods

fn truncate_to_boundary(&self, chars: usize) -> &Self

fn truncate_to_byte_offset(&self, count: usize) -> &Self

Loading content...

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);
Loading content...

Implementors

Loading content...