Split text into lines (by '\n'), returning each line's text and the global char index where it starts.
(text: &str)
| 1433 | /// Split text into lines (by '\n'), returning each line's text |
| 1434 | /// and the global char index where it starts. |
| 1435 | pub fn split_lines(text: &str) -> Vec<(usize, &str)> { |
| 1436 | let mut result = Vec::new(); |
| 1437 | let mut char_start = 0; |
| 1438 | let mut byte_start = 0; |
| 1439 | for (byte_idx, ch) in text.char_indices() { |
| 1440 | if ch == '\n' { |
| 1441 | result.push((char_start, &text[byte_start..byte_idx])); |
| 1442 | char_start += text[byte_start..byte_idx].chars().count() + 1; // +1 for '\n' |
| 1443 | byte_start = byte_idx + 1; // '\n' is 1 byte |
| 1444 | } |
| 1445 | } |
| 1446 | // Last line (after final '\n' or entire text if no '\n') |
| 1447 | result.push((char_start, &text[byte_start..])); |
| 1448 | result |
| 1449 | } |
| 1450 | |
| 1451 | /// A single visual line after word-wrapping. |
| 1452 | #[derive(Debug, Clone)] |
no outgoing calls