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)
| 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). |
| 108 | fn 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)] |