(lines: &[GitDiffLine])
| 1855 | } |
| 1856 | |
| 1857 | fn parse_git_diff_replacements(lines: &[GitDiffLine]) -> Option<Vec<GitReplacementBlock>> { |
| 1858 | let mut blocks = Vec::new(); |
| 1859 | let mut old_start: Option<usize> = None; |
| 1860 | let mut new_start: Option<usize> = None; |
| 1861 | let mut old_len = 0usize; |
| 1862 | let mut new_lines = Vec::new(); |
| 1863 | let mut old_cursor = 1usize; |
| 1864 | let mut new_cursor = 1usize; |
| 1865 | |
| 1866 | let flush = |blocks: &mut Vec<GitReplacementBlock>, |
| 1867 | old_start: &mut Option<usize>, |
| 1868 | new_start: &mut Option<usize>, |
| 1869 | old_len: &mut usize, |
| 1870 | new_lines: &mut Vec<Vec<u8>>| |
| 1871 | -> Option<()> { |
| 1872 | if *old_len > 0 || !new_lines.is_empty() { |
| 1873 | let old = old_start.take()?; |
| 1874 | let new = new_start.take().unwrap_or(old); |
| 1875 | blocks.push(GitReplacementBlock { |
| 1876 | old_start: old, |
| 1877 | old_len: *old_len, |
| 1878 | new_start: new, |
| 1879 | new_lines: std::mem::take(new_lines), |
| 1880 | }); |
| 1881 | *old_len = 0; |
| 1882 | } |
| 1883 | Some(()) |
| 1884 | }; |
| 1885 | |
| 1886 | for line in lines { |
| 1887 | match line.origin { |
| 1888 | ' ' => { |
| 1889 | flush( |
| 1890 | &mut blocks, |
| 1891 | &mut old_start, |
| 1892 | &mut new_start, |
| 1893 | &mut old_len, |
| 1894 | &mut new_lines, |
| 1895 | )?; |
| 1896 | old_cursor = line |
| 1897 | .old_lineno |
| 1898 | .map(|n| n as usize + 1) |
| 1899 | .unwrap_or(old_cursor + 1); |
| 1900 | new_cursor = line |
| 1901 | .new_lineno |
| 1902 | .map(|n| n as usize + 1) |
| 1903 | .unwrap_or(new_cursor + 1); |
| 1904 | } |
| 1905 | '-' => { |
| 1906 | if old_start.is_none() { |
| 1907 | old_start = Some(line.old_lineno.map(|n| n as usize).unwrap_or(old_cursor)); |
| 1908 | } |
| 1909 | old_cursor = line |
| 1910 | .old_lineno |
| 1911 | .map(|n| n as usize + 1) |
| 1912 | .unwrap_or(old_cursor + 1); |
| 1913 | old_len += 1; |
| 1914 | } |
no test coverage detected