Crates.io | truncrate |
lib.rs | truncrate |
version | 0.1.3 |
source | src |
created_at | 2019-09-11 11:44:18.203391 |
updated_at | 2019-10-22 11:44:20.81989 |
description | truncate strings economically without splitting your characters, symbols, emojis etc. |
homepage | |
repository | https://github.com/yotamofek/trunc |
max_upload_size | |
id | 164124 |
size | 34,017 |
Rust library for intelligently truncating unicode strings!
An economical way to truncate a string to a given character count or byte-offset without splitting graphemes.
Depending on the encoding of your browser 'π€πΎ' will produce a dark-skinned hand. In most text editors it will look like two separate characters (π€ πΎ).
Notice how the truncation to 1 will not break the grapheme into a yellow hand:
use truncrate::*;
let s = "π€πΎaπ€ π€πΎ\t π€ ";
assert_eq!(s.truncate_to_boundary(1), "");
assert_eq!(s.truncate_to_boundary(2), "π€πΎ");
Should you set a numeric boundary which ends with a whitespace - truncation will trim the whitespace for you:
assert_eq!(s.truncate_to_boundary(4), "π€πΎaπ€");
assert_eq!(s.truncate_to_boundary(5), "π€πΎaπ€");
But if the truncation exceeds the strings size it will return the entire string:
assert_eq!(s.truncate_to_boundary(10), s);
You can also choose to truncate by byte-offset (i.e., byte-size boundary):
let s = "π€πΎaπ€ ";
// where "π€πΎ" = 8 bytes
assert_eq!(s.truncate_to_byte_offset(0), "");
assert_eq!(s.truncate_to_byte_offset(8), "π€πΎ");
Aside from truncation of a single string you can also split with unicode awareness:
let mut s = "π€πΎaπ€ ";
assert_eq!(s.split_all_to_boundary(1), vec!("a", "π€"));
assert_eq!(s.split_all_to_boundary(2), vec!("π€πΎ", "aπ€",));
If you wish to chain splitting patterns you can do it with the 'inplace' functions:
let mut s = vec!("π€πΎaπ€ ", "π€πΎπ€πΎπ€πΎ ");
// split different byte offsets
s.split_to_offset_inplace(9)
.split_to_offset_inplace(8)
.split_to_offset_inplace(10);
assert_eq!(s, vec!("π€πΎaπ€ ", "π€πΎ", "π€πΎ", "π€πΎ", " "));
You can also split all of your string to a boundary with the split_all_to_boundary method:
let s = "π€πΎaπ€ ";
assert_eq!(s.split_all_to_boundary(3), vec!("π€πΎa", "π€ "));
For the full functionality and further examples check out the documentation.