]> git.mdlowis.com Git - archive/tide-ocaml.git/commitdiff
getc now returns newlines for windows line endings
authorMichael D. Lowis <mike@mdlowis.com>
Tue, 31 Oct 2017 01:25:22 +0000 (21:25 -0400)
committerMichael D. Lowis <mike@mdlowis.com>
Tue, 31 Oct 2017 01:25:22 +0000 (21:25 -0400)
lib/rope.ml
tests/rope_tests.ml

index ca39bdc4f997274597c0042767d42716279bbd3c..62221bb28d35f3b8490a0ef84e2abe9c9f681b97 100644 (file)
@@ -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
index d539ea1c7ed4b2b24716abe91bd42acc315869f1..93303cf273c3cd8152bc87091b111541ed24d0bf 100644 (file)
@@ -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 () ->