agentmux_common/
tokens.rs1pub fn estimate_tokens(text: &str) -> u64 {
13 tokens_for_chars(text.chars().count() as u64)
14}
15
16pub fn tokens_for_chars(chars: u64) -> u64 {
18 chars.div_ceil(4)
19}
20
21pub fn count_lines(text: &str) -> u64 {
24 text.lines().count() as u64
25}
26
27#[cfg(test)]
28mod tests {
29 use super::*;
30
31 pub(crate) const VECTOR: &[(&str, u64)] = &[
33 ("", 0),
34 ("abcd", 1),
35 ("abcde", 2),
36 ("héllo", 2),
37 ("日本語テキスト", 2),
38 ("🙂🙂🙂🙂", 1),
39 ("e\u{301}e\u{301}", 1),
40 ("# Memory\n\nOne line.\n", 5),
41 ];
42
43 #[test]
44 fn the_estimate_is_scalar_values_over_four_rounded_up() {
45 for (text, tokens) in VECTOR {
46 assert_eq!(estimate_tokens(text), *tokens, "{text:?}");
47 }
48 }
49
50 #[test]
51 fn lines_count_like_str_lines() {
52 assert_eq!(count_lines(""), 0);
53 assert_eq!(count_lines("a"), 1);
54 assert_eq!(count_lines("a\n"), 1);
55 assert_eq!(count_lines("a\nb"), 2);
56 assert_eq!(count_lines("a\r\nb\n"), 2);
57 assert_eq!(count_lines("\n"), 1);
58 }
59}