Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
yokurang committed Jul 25, 2024
1 parent 3ff6c9f commit 4adc84e
Showing 1 changed file with 35 additions and 38 deletions.
73 changes: 35 additions & 38 deletions lib/Block.ml
Original file line number Diff line number Diff line change
@@ -1,50 +1,47 @@
type 'a t = Common of 'a | Changed of { mine : 'a list; their : 'a list }

(* Helper function implementations *)
let rec group_lines acc current_mine current_their = function
| [] ->
if current_mine <> [] || current_their <> [] then
Changed { mine = current_mine; their = current_their } :: acc
else acc
| `Common line :: rest ->
let acc' =
let of_hunk (hunk : 'a Patch.hunk) : 'a t list =
let rec process_hunk acc current_mine current_their = function
| [] ->
if current_mine <> [] || current_their <> [] then
Changed { mine = current_mine; their = current_their } :: acc
Changed
{ mine = List.rev current_mine; their = List.rev current_their }
:: acc
else acc
in
group_lines (Common line :: acc') [] [] rest
| `Mine line :: rest ->
group_lines acc (line :: current_mine) current_their rest
| `Their line :: rest ->
group_lines acc current_mine (line :: current_their) rest
| `Common line :: rest ->
let acc' =
if current_mine <> [] || current_their <> [] then
Changed
{ mine = List.rev current_mine; their = List.rev current_their }
:: acc
else acc
in
process_hunk (Common line :: acc') [] [] rest
| `Mine line :: rest ->
process_hunk acc (line :: current_mine) current_their rest
| `Their line :: rest ->
process_hunk acc current_mine (line :: current_their) rest
in
List.rev (process_hunk [] [] [] hunk.lines)

let rec process_blocks lines mine_len their_len = function
| [] -> (lines, mine_len, their_len)
| Common line :: rest ->
process_blocks (`Common line :: lines) (mine_len + 1) (their_len + 1) rest
| Changed { mine; their } :: rest ->
let mine_lines = List.rev_map (fun line -> `Mine line) mine in
let their_lines = List.rev_map (fun line -> `Their line) their in
process_blocks
(List.rev_append mine_lines (List.rev_append their_lines lines))
(mine_len + List.length mine)
(their_len + List.length their)
rest

(* End of helper function implementations *)

(* Main block function implementations *)
let of_hunk (_hunk : 'a Patch.hunk) : 'a t list =
List.rev (group_lines [] [] [] _hunk.lines)

let to_hunk (_blocks : 'a t list) : 'a Patch.hunk =
let lines, mine_len, their_len = process_blocks [] 0 0 _blocks in
let to_hunk (blocks : 'a t list) : 'a Patch.hunk =
let lines, mine_len, their_len =
List.fold_left
(fun (lines, mine_len, their_len) block ->
match block with
| Common line -> (`Common line :: lines, mine_len + 1, their_len + 1)
| Changed { mine; their } ->
let mine_lines = List.map (fun line -> `Mine line) mine in
let their_lines = List.map (fun line -> `Their line) their in
( mine_lines @ their_lines @ lines,
mine_len + List.length mine,
their_len + List.length their ))
([], 0, 0) blocks
in
{
mine_start = 0;
mine_len;
their_start = 0;
their_len;
lines = List.rev lines;
}

(* End of main block function implementations *)

0 comments on commit 4adc84e

Please sign in to comment.