wp_err/
util.rs

1use unicode_segmentation::UnicodeSegmentation;
2/// Return the first 30 Unicode grapheme clusters of the given string.
3/// This is a safe, user-facing truncation (not a logical 'split').
4pub fn split_string(s: &str) -> String {
5    s.graphemes(true).take(30).collect()
6}
7
8#[cfg(test)]
9mod tests {
10    use super::*;
11
12    #[test]
13    fn test_split_string_short() {
14        let s = "hello";
15        assert_eq!(split_string(s), "hello");
16    }
17
18    #[test]
19    fn test_split_string_exact_30() {
20        let s = "123456789012345678901234567890"; // exactly 30 chars
21        assert_eq!(split_string(s), s);
22    }
23
24    #[test]
25    fn test_split_string_longer_than_30() {
26        let s = "1234567890123456789012345678901234567890"; // 40 chars
27        let result = split_string(s);
28        assert_eq!(result.len(), 30);
29        assert_eq!(result, "123456789012345678901234567890");
30    }
31
32    #[test]
33    fn test_split_string_empty() {
34        assert_eq!(split_string(""), "");
35    }
36
37    #[test]
38    fn test_split_string_unicode() {
39        // Chinese characters (each is one grapheme cluster)
40        let s = "中文测试字符串用于验证功能是否正确工作这里还需要更多字符来测试"; // more than 30 chars
41        let result = split_string(s);
42        assert_eq!(result.graphemes(true).count(), 30);
43    }
44
45    #[test]
46    fn test_split_string_emoji() {
47        // Each emoji is one grapheme cluster
48        let s = "😀😁😂🤣😃😄😅😆😉😊😋😎😍😘🥰😗😙😚☺🙂🤗🤩🤔🤨😐😑😶🙄😏😣😥";
49        let result = split_string(s);
50        assert_eq!(result.graphemes(true).count(), 30);
51    }
52
53    #[test]
54    fn test_split_string_mixed() {
55        let s = "Hello世界🌍Test";
56        let result = split_string(s);
57        assert_eq!(result, "Hello世界🌍Test");
58    }
59}