@@ -84,7 +84,7 @@ pub fn parse_unified_diff(patch: &str) -> PatchResult<Vec<FileChange>> {
8484 let line = lines[ i] ;
8585
8686 // Detect file header: --- path
87- if line. starts_with ( "--- " ) {
87+ if let Some ( old_str ) = line. strip_prefix ( "--- " ) {
8888 // Save previous file change if exists
8989 if let Some ( mut change) = current_change. take ( ) {
9090 if let Some ( hunk) = current_hunk. take ( ) {
@@ -95,18 +95,19 @@ pub fn parse_unified_diff(patch: &str) -> PatchResult<Vec<FileChange>> {
9595 }
9696 }
9797
98- let old_path = parse_file_path ( & line [ 4 .. ] ) ;
98+ let old_path = parse_file_path ( old_str ) ;
9999
100100 // Look for +++ line
101- if i + 1 < lines. len ( ) && lines[ i + 1 ] . starts_with ( "+++ " ) {
102- let new_path = parse_file_path ( & lines[ i + 1 ] [ 4 ..] ) ;
103- current_change = Some ( FileChange :: new ( old_path, new_path) ) ;
104- i += 2 ;
105- continue ;
106- } else {
107- // Malformed patch, but try to continue
108- current_change = Some ( FileChange :: new ( old_path, None ) ) ;
101+ if i + 1 < lines. len ( ) {
102+ if let Some ( new_str) = lines[ i + 1 ] . strip_prefix ( "+++ " ) {
103+ let new_path = parse_file_path ( new_str) ;
104+ current_change = Some ( FileChange :: new ( old_path, new_path) ) ;
105+ i += 2 ;
106+ continue ;
107+ }
109108 }
109+ // Malformed patch, but try to continue
110+ current_change = Some ( FileChange :: new ( old_path, None ) ) ;
110111 i += 1 ;
111112 continue ;
112113 }
@@ -201,37 +202,37 @@ pub fn parse_git_diff(patch: &str) -> PatchResult<Vec<FileChange>> {
201202
202203 // Handle git-specific metadata
203204 if let Some ( ref mut change) = current_change {
204- if line. starts_with ( "old mode " ) {
205- change. old_mode = Some ( line [ 9 .. ] . to_string ( ) ) ;
205+ if let Some ( mode ) = line. strip_prefix ( "old mode " ) {
206+ change. old_mode = Some ( mode . to_string ( ) ) ;
206207 i += 1 ;
207208 continue ;
208209 }
209- if line. starts_with ( "new mode " ) {
210- change. new_mode = Some ( line [ 9 .. ] . to_string ( ) ) ;
210+ if let Some ( mode ) = line. strip_prefix ( "new mode " ) {
211+ change. new_mode = Some ( mode . to_string ( ) ) ;
211212 i += 1 ;
212213 continue ;
213214 }
214- if line. starts_with ( "new file mode " ) {
215+ if let Some ( mode ) = line. strip_prefix ( "new file mode " ) {
215216 change. is_new_file = true ;
216- change. new_mode = Some ( line [ 14 .. ] . to_string ( ) ) ;
217+ change. new_mode = Some ( mode . to_string ( ) ) ;
217218 i += 1 ;
218219 continue ;
219220 }
220- if line. starts_with ( "deleted file mode " ) {
221+ if let Some ( mode ) = line. strip_prefix ( "deleted file mode " ) {
221222 change. is_deleted = true ;
222- change. old_mode = Some ( line [ 18 .. ] . to_string ( ) ) ;
223+ change. old_mode = Some ( mode . to_string ( ) ) ;
223224 i += 1 ;
224225 continue ;
225226 }
226- if line. starts_with ( "rename from " ) {
227+ if let Some ( path ) = line. strip_prefix ( "rename from " ) {
227228 change. is_rename = true ;
228- change. old_path = Some ( PathBuf :: from ( & line [ 12 .. ] ) ) ;
229+ change. old_path = Some ( PathBuf :: from ( path ) ) ;
229230 i += 1 ;
230231 continue ;
231232 }
232- if line. starts_with ( "rename to " ) {
233+ if let Some ( path ) = line. strip_prefix ( "rename to " ) {
233234 change. is_rename = true ;
234- change. new_path = Some ( PathBuf :: from ( & line [ 10 .. ] ) ) ;
235+ change. new_path = Some ( PathBuf :: from ( path ) ) ;
235236 i += 1 ;
236237 continue ;
237238 }
@@ -251,9 +252,9 @@ pub fn parse_git_diff(patch: &str) -> PatchResult<Vec<FileChange>> {
251252 }
252253
253254 // Standard unified diff parts
254- if line. starts_with ( "--- " ) {
255+ if let Some ( path_str ) = line. strip_prefix ( "--- " ) {
255256 if let Some ( ref mut change) = current_change {
256- let old_path = parse_file_path ( & line [ 4 .. ] ) ;
257+ let old_path = parse_file_path ( path_str ) ;
257258 if old_path
258259 . as_ref ( )
259260 . is_some_and ( |p| p. as_os_str ( ) == "/dev/null" )
@@ -268,9 +269,9 @@ pub fn parse_git_diff(patch: &str) -> PatchResult<Vec<FileChange>> {
268269 continue ;
269270 }
270271
271- if line. starts_with ( "+++ " ) {
272+ if let Some ( path_str ) = line. strip_prefix ( "+++ " ) {
272273 if let Some ( ref mut change) = current_change {
273- let new_path = parse_file_path ( & line [ 4 .. ] ) ;
274+ let new_path = parse_file_path ( path_str ) ;
274275 if new_path
275276 . as_ref ( )
276277 . is_some_and ( |p| p. as_os_str ( ) == "/dev/null" )
0 commit comments