Skip to content

Commit

Permalink
Adjust output{_char,_string,_bytes,,_substring} to reflect nl transla…
Browse files Browse the repository at this point in the history
…tion on MinGW+Cygwin
  • Loading branch information
jmid committed Jan 5, 2024
1 parent 122ad7f commit ecb135d
Showing 1 changed file with 34 additions and 13 deletions.
47 changes: 34 additions & 13 deletions src/io/stm_tests.ml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ struct

let init_state = Closed 0L

let count_nls s =
String.fold_right (fun c count -> if c = '\n' then 1+count else count) s 0

let next_state c s = match c,s with
| Open_text, Closed _l ->
Open { position = 0L;
Expand Down Expand Up @@ -123,30 +126,45 @@ struct
| Set_buffered b, Open { position; length; buffered = _; binary_mode } ->
Open { position; length; buffered = b; binary_mode }
(* output on open Out_channel *)
| Output_char _, Open { position; length; buffered; binary_mode }
| Output_char c, Open { position; length; buffered; binary_mode } ->
let len = (* Windows text mode maps '\n' to "\r\n" *)
if (Sys.win32 || Sys.cygwin) && not binary_mode && c = '\n' then 2L else 1L in
Open { position = Int64.add position len;
length = Int64.add length len;
buffered;
binary_mode; }
| Output_byte _, Open { position; length; buffered; binary_mode } ->
Open { position = Int64.succ position;
length = Int64.succ length;
buffered;
binary_mode; }
| Output_string str, Open { position; length; buffered; binary_mode } ->
let len = Int64.of_int (String.length str) in
Open { position = Int64.add position len;
length = Int64.add length len;
buffered;
binary_mode; }
let len = (* Windows text mode maps '\n' to "\r\n" *)
if (Sys.win32 || Sys.cygwin) && not binary_mode
then Int64.of_int (String.length str + count_nls str)
else Int64.of_int (String.length str) in
Open { position = Int64.add position len;
length = Int64.add length len;
buffered;
binary_mode; }
| Output_bytes b, Open { position; length; buffered; binary_mode } ->
let len = Int64.of_int (Bytes.length b) in
Open { position = Int64.add position len;
length = Int64.add length len;
buffered;
binary_mode; }
let len = (* Windows text mode maps '\n' to "\r\n" *)
if (Sys.win32 || Sys.cygwin) && not binary_mode
then Int64.of_int (Bytes.length b + count_nls (String.of_bytes b))
else Int64.of_int (Bytes.length b) in
Open { position = Int64.add position len;
length = Int64.add length len;
buffered;
binary_mode; }
| Output (b,p,l), Open { position; length; buffered; binary_mode } ->
let bytes_len = Bytes.length b in
if p < 0 || p >= bytes_len || l < 0 || p+l > bytes_len
then s
else
let len = Int64.of_int l in
let len = (* Windows text mode maps '\n' to "\r\n" *)
if (Sys.win32 || Sys.cygwin) && not binary_mode
then Int64.of_int (l + count_nls String.(sub (of_bytes b) p l))
else Int64.of_int l in
Open { position = Int64.add position len;
length = Int64.add length len;
buffered;
Expand All @@ -156,7 +174,10 @@ struct
if p < 0 || p >= str_len || l < 0 || p+l > str_len
then s
else
let len = Int64.of_int l in
let len = (* Windows text mode maps '\n' to "\r\n" *)
if (Sys.win32 || Sys.cygwin) && not binary_mode
then Int64.of_int (l + count_nls (String.sub str p l))
else Int64.of_int l in
Open { position = Int64.add position len;
length = Int64.add length len;
buffered;
Expand Down

0 comments on commit ecb135d

Please sign in to comment.