(raw) string literal linebreaks on Windows

This came up in unit tests: if I have a line break in a string literal, is the result platform-dependent? Eg on Linux,

julia> raw"1
       2"
"1\n2"

julia> """
       1
       2
       """
"1\n2\n"

but I wonder if this would give me "\r\n" linebreaks on Windows. Can someone with a Windows machine please check this?

julia> Base.Sys.KERNEL
:NT

julia> raw"1
       2"
"1\n2"

julia> """
       1
       2
       """
"1\n2\n"

Doesn’t look like it.

I wonder if this is intended - writing e.g. log files on windows would potentially need special handling for newline characters, as println behaves the same.

1 Like

Thanks. Can you please check the same with println? Eg

julia> let io = IOBuffer()
       println(io, "foo")
       String(take!(io))
       end
"foo\n"

on Linux. But perhaps this outputs \r\n linebreaks on Windows?

It behaves the same. I noticed that behaviour a while back while writing a small tool which also logs stuff (the logs ended up not being used, ironically - but at least the capability is there).

julia> let io = IOBuffer()
              println(io, "foo")
                     String(take!(io))
                            end
"foo\n"

I guess it comes down to whether Base or each individual developer should make the decision on how to print this based on the platform. There’s good arguments either way. Personally, I lean more towards one single implementation to avoid as many differences between platforms as possible. Although I do like the concept of a true carriage return (EDIT: after some testing, carriage return works on either platform anyway).

EDIT: Well, there’s the culprit:

I have not formed an opinion on whether this is the desired way, I was just asking because I am curious how newlines are meant to be handled cross-platform.

Does the \n not cause a problem on Windows when using the terminal? Or is it somehow converted?

Seems to work just fine from a CMD.exe:

C:\Users\<snip>>julia -e println(\"test\ntest\")
test
test

I do seem to recall some change to ConsoleHost a while back where support for this was added - since WSL (Windos Subsystem for Linux, the Linux shell thing on Windows) came into existence, they’ve added a bunch of Unix support to Windows. Some programs like Notepad are still lagging behind a bit, but even they are getting updated with, I think, the coming september update.

1 Like

There’s also the possibility for this to mess up on Mac (if I remember correctly, just \r is used over there). I can’t test that though.

EDIT: Apparently, that’s just a legacy thing.