Split a byte slice into lines. Lines are split on `\n` characters. The newline is included in each line (except possibly the last line if the content doesn't end with a newline). # Arguments `content` - The raw bytes to split into lines # Returns A vector of `Line`s representing each line in the content. # Example ```rust use atomic_core::diff::Line; let content = b"line1\nline2\nline3"; l
(content: &'a [u8])
| 160 | /// assert_eq!(lines[2].content(), b"line3"); // No trailing newline |
| 161 | /// ``` |
| 162 | pub fn from_bytes(content: &'a [u8]) -> Vec<Self> { |
| 163 | if content.is_empty() { |
| 164 | return Vec::new(); |
| 165 | } |
| 166 | |
| 167 | let mut lines = Vec::new(); |
| 168 | let mut start = 0; |
| 169 | |
| 170 | for (i, &byte) in content.iter().enumerate() { |
| 171 | if byte == b'\n' { |
| 172 | lines.push(Self::new(&content[start..=i])); |
| 173 | start = i + 1; |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | // Handle final line without trailing newline |
| 178 | if start < content.len() { |
| 179 | lines.push(Self::new_last(&content[start..])); |
| 180 | } else if !lines.is_empty() { |
| 181 | // Mark the last line as last |
| 182 | if let Some(last) = lines.last_mut() { |
| 183 | last.is_last = true; |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | lines |
| 188 | } |
| 189 | |
| 190 | /// Split a string into lines. |
| 191 | /// |