From ecb135d1e61d16acac808e5bcab7c623928bdcff Mon Sep 17 00:00:00 2001 From: Jan Midtgaard Date: Fri, 5 Jan 2024 17:59:09 +0100 Subject: [PATCH] Adjust output{_char,_string,_bytes,,_substring} to reflect nl translation on MinGW+Cygwin --- src/io/stm_tests.ml | 47 ++++++++++++++++++++++++++++++++------------- 1 file changed, 34 insertions(+), 13 deletions(-) diff --git a/src/io/stm_tests.ml b/src/io/stm_tests.ml index 2def6b7a..cfbe6b00 100644 --- a/src/io/stm_tests.ml +++ b/src/io/stm_tests.ml @@ -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; @@ -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; @@ -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;