MCPcopy Index your code
hub / github.com/carllerche/assert-struct / byte_offset_of

Function byte_offset_of

assert-struct/src/error.rs:108–118  ·  view source on GitHub ↗

Compute the byte offset of a (line, col) position within a source string. `line` is 1-indexed; `col` is 0-indexed (as returned by proc_macro2's span locations). Returns 0 when `line` is 0 (synthetic span with no real source location).

(source: &str, line: u32, col: u32)

Source from the content-addressed store, hash-verified

106/// `line` is 1-indexed; `col` is 0-indexed (as returned by proc_macro2's span locations).
107/// Returns 0 when `line` is 0 (synthetic span with no real source location).
108fn byte_offset_of(source: &str, line: u32, col: u32) -> usize {
109 if line == 0 {
110 return 0;
111 }
112 let line_start: usize = source
113 .split('\n')
114 .take((line - 1) as usize)
115 .map(|l| l.len() + 1) // +1 for the '\n'
116 .sum();
117 (line_start + col as usize).min(source.len())
118}
119
120/// Context information for a failed assertion.
121#[derive(Debug, Clone)]

Callers 1

fmtMethod · 0.85

Calls 1

lenMethod · 0.80

Tested by

no test coverage detected