MCPcopy Index your code
hub / github.com/endbasic/endbasic / char_index_to_byte_index

Function char_index_to_byte_index

std/src/strings.rs:77–79  ·  view source on GitHub ↗

Returns the byte offset of the given character position within `s`.

(s: &str, char_index: usize)

Source from the content-addressed store, hash-verified

75
76/// Returns the byte offset of the given character position within `s`.
77fn char_index_to_byte_index(s: &str, char_index: usize) -> usize {
78 s.char_indices().nth(char_index).map(|(i, _)| i).unwrap_or(s.len())
79}
80
81/// Returns the number of characters before the given byte offset within `s`.
82fn byte_index_to_char_index(s: &str, byte_index: usize) -> usize {

Callers 2

slice_by_char_rangeFunction · 0.85
execMethod · 0.85

Calls 1

lenMethod · 0.45

Tested by

no test coverage detected