MCPcopy Create free account
hub / github.com/ashvardanian/StringZilla / count_utf8

Function count_utf8

rust/stringzilla.rs:1877–1886  ·  view source on GitHub ↗

Counts the number of UTF-8 characters in the text. This function efficiently counts UTF-8 characters by identifying character start bytes (non-continuation bytes). Uses SIMD acceleration when available. # Arguments `text`: The UTF-8 encoded byte slice to count characters in. # Returns The number of UTF-8 characters (codepoints) in the text. # Examples ``` use stringzilla::stringzilla as sz;

(text: T)

Source from the content-addressed store, hash-verified

1875/// assert_eq!(sz::count_utf8(text_cjk), 4);
1876/// ```
1877pub fn count_utf8<T>(text: T) -> usize
1878where
1879 T: AsRef<[u8]>,
1880{
1881 let text_ref = text.as_ref();
1882 let text_pointer = text_ref.as_ptr() as *const c_void;
1883 let text_length = text_ref.len();
1884
1885 unsafe { sz_utf8_count(text_pointer, text_length) }
1886}
1887
1888/// Finds the byte offset of the Nth UTF-8 character (0-indexed).
1889///

Callers 1

lenMethod · 0.85

Calls 3

as_ptrMethod · 0.80
lenMethod · 0.80
sz_utf8_countFunction · 0.50

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…