Skip to content

Commit

Permalink
feat: graphemes trim_count
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandervantrijffel committed Oct 19, 2024
1 parent 37e4e44 commit 0e08645
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ name = "licactlib"
path = "src/lib.rs"
doctest = false

[dependencies]
unicode-segmentation = "1"

[profile.release]
# debug = true # Enable debug symbols
strip = true # Strip symbols from binary
Expand Down
25 changes: 25 additions & 0 deletions src/graphemes.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#![allow(dead_code)]

use unicode_segmentation::UnicodeSegmentation;

trait Trim {
fn trim_count(&self, count: usize) -> String;
}

impl Trim for str {
fn trim_count(&self, count: usize) -> String {
UnicodeSegmentation::graphemes(self, true).take(count).collect()
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_graphemes() {
let s = "a̐éö̲\r\n";
let result = s.trim_count(2);
assert_eq!(result, "a̐é");
}
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod graphemes;
pub mod mutate_in_closure;
pub mod vec_any;

0 comments on commit 0e08645

Please sign in to comment.