From: Michael D. Lowis Date: Tue, 31 Oct 2017 01:25:22 +0000 (-0400) Subject: getc now returns newlines for windows line endings X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=7dc06f2bc65c4686541c957106e6d0fe3bba456b;p=archive%2Ftide-ocaml.git getc now returns newlines for windows line endings --- diff --git a/lib/rope.ml b/lib/rope.ml index ca39bdc..62221bb 100644 --- a/lib/rope.ml +++ b/lib/rope.ml @@ -58,7 +58,14 @@ let del rope i j = let rec getc rope i = check_index rope i; match rope with - | Leaf (s,off,_) -> (Char.code s.[off + i]) + | Leaf (s,off,_) -> + let c = (Char.code s.[off + i]) in + let len = (length rope) in + let next = (i + 1) in + if (c == 0x0D && next < len && (getc rope next) == 0x0A) then + 0x0A + else + c | Node (l,r,len) -> let left_len = (length l) in if i < left_len then diff --git a/tests/rope_tests.ml b/tests/rope_tests.ml index d539ea1..93303cf 100644 --- a/tests/rope_tests.ml +++ b/tests/rope_tests.ml @@ -73,6 +73,18 @@ let () = let rope = Node((Leaf("a", 0, 1)), (Leaf("b", 0, 1)), 2) in assert( (getc rope (1)) == Char.code 'b' ); ); + test "getc : return \\n for \\r\\n" (fun () -> + let rope = from_string "\r\n" in + assert( (getc rope (0)) == Char.code '\n' ); + ); + test "getc : return \\r for \\r at end of string" (fun () -> + let rope = from_string "\r" in + assert( (getc rope (0)) == Char.code '\r' ); + ); + test "getc : return \\r for \\r with no \\n" (fun () -> + let rope = from_string "\ra" in + assert( (getc rope (0)) == Char.code '\r' ); + ); (* puts() tests *) test "puts : insert at index 0" (fun () ->