1pub trait StringEncoding {
2 fn new() -> Self;
3
4 fn get_encoded_length(string: &str) -> usize;
5
6 fn encoded_length_to_byte_count(string: &str, length: usize) -> usize;
7}
8
9#[derive(Debug, PartialEq, Eq)]
10pub struct ByteWiseEncoding;
11
12impl StringEncoding for ByteWiseEncoding {
13 fn new() -> Self {
14 Self
15 }
16
17 fn get_encoded_length(string: &str) -> usize {
18 string.len()
19 }
20
21 fn encoded_length_to_byte_count(_string: &str, length: usize) -> usize {
22 length
23 }
24}
25
26#[derive(Debug, PartialEq, Eq)]
27pub struct Utf8;
28
29impl StringEncoding for Utf8 {
30 fn new() -> Self {
31 Self
32 }
33
34 fn get_encoded_length(string: &str) -> usize {
35 string.chars().count()
36 }
37
38 fn encoded_length_to_byte_count(string: &str, length: usize) -> usize {
39 string.chars().take(length).map(char::len_utf8).count()
40 }
41}