Crate bytecount

Source
Expand description

count occurrences of a given byte, or the number of UTF-8 code points, in a byte slice, fast.

This crate has the count method to count byte occurrences (for example newlines) in a larger &[u8] slice.

For example:

assert_eq!(5, bytecount::count(b"Hello, this is the bytecount crate!", b' '));

Also there is a num_chars method to count the number of UTF8 characters in a slice. It will work the same as str::chars().count() for byte slices of correct UTF-8 character sequences. The result will likely be off for invalid sequences, although the result is guaranteed to be between 0 and [_]::len(), inclusive.

Example:

let sequence = "Wenn ich ein Vöglein wär, flög ich zu Dir!";
assert_eq!(sequence.chars().count(),
           bytecount::num_chars(sequence.as_bytes()));

For completeness and easy comparison, the “naive” versions of both count and num_chars are provided. Those are also faster if used on predominantly small strings. The naive_count_32 method can be faster still on small strings.

Functions§

  • Count occurrences of a byte in a slice of bytes, fast
  • Count occurrences of a byte in a slice of bytes, simple
  • Count up to (2^32)-1 occurrences of a byte in a slice of bytes, simple
  • Count the number of UTF-8 encoded Unicode codepoints in a slice of bytes, simple
  • Count the number of UTF-8 encoded Unicode codepoints in a slice of bytes, fast