What's the best way to determine the native newline characters such as '\n' or '\r\n' in Haskell?
I see there is a "nativeNewline" function in GHC.IO:Handle, but assume that it is both a private API and most of all non-standard Haskell.
What's the best way to determine the native newline characters such as '\n' or '\r\n' in Haskell?
I see there is a "nativeNewline" function in GHC.IO:Handle, but assume that it is both a private API and most of all non-standard Haskell.
You should think of the newline representation as part of the encoding of a text file that is stored in the filesystem, just like UTF-8. A text file is normally decoded when you read it into your program, and encoded when written -- converting to and from the native newline representation is done as part of this encoding and decoding. Inside your Haskell program, just as characters are represented by their Unicode code points, the newline character is always \n
.
To tell the I/O system about the newline encoding you want to use, see the section on Newline Conversion in the documentation for System.IO.
System.IO.nativeNewline
is not private - you can access it
to find out what GHC considers the native "newline" to be
on the current platform.
Note that the type of this variable, System.IO.Newline
, does
not have a Show
instance as of GHC 6.12.3. So you can't
easily print its value. Instead, check to see if it is equal
to System.IO.LF
or System.IO.CRLF
.
However, as Simon pointed out, you shouldn't need to know about the native newline sequence with normal usage of the text-oriented IO functions in GHC.
This variable, together with the rest of the new Unicode-aware capabilities of the IO system, is not yet part of the Haskell standard. It was not included in the Haskell 2010 report. However, since it is already implemented in GHC, and there is quite a widespread consensus that it is important and useful, expect it to be included in one of the upcoming yearly revisions of the standard.