Page 1 of 1

Platform independent line terminators

PostPosted: Sun Sep 07, 2008 9:09 pm
by hopscc
If you're writing cobra code (output strings) for multiple platforms ( .Net and mono) say its painful ( not to mention ugly) to provide correct line terminators for the specific platform
( in fact its not just between platforms - this bites even within Windows - output to file and a Gui widget for e.g.)

i.e you have to do something like
Code: Select all
nl=Environment.newLine
...
string='\nline1...[nl]line2...[nl]....[nl]
...

rather than the more obvious
Code: Select all
string='\nline1...\nline2...\n....\n\n
# or
string='\r\nline1...\r\nline2...\r\n....\r\n\r\n


I propose adding another c'\?' metacharacter to the currently supported set ( '\n', \r', '\t', ...) for strings
that expands to Environment.newLine automatically
I'd suggest '\N'
Code: Select all
string='\Nline1...\Nline2...\N....\N\N


(in an attempt to belabour the point, the first block of example code above has a deliberate mistake in it)

Re: Platform independent line terminators

PostPosted: Thu Sep 11, 2008 1:18 am
by Charles
Regarding the intentional error, I think it would be just as easy to make the mistake like so:
string = '\nline1...\Nline2...\N....\N'

An interesting observation is that "\n" is almost always a bad idea given that "Environment.newLine" is the best practice. What if \n not preceded by \r was interpreted as Environment.newLine?

\n could be "environment new line"
\N could be "new line character"

The idea here is to get rid of having to remember anything special in the common case such as \N or [Environment.newLine]

Re: Platform independent line terminators

PostPosted: Thu Sep 11, 2008 5:58 am
by hopscc
yeeeees (maybe)

I'm a little uncertain ;
due the difference from (C/C++) historical practice ( \n always mapping to newline regardless of platform) and
for introducing whats essentially a conversion change ( vs existing use) and windows platform use (line termin '/r/n')
and the possible difficulty
of defending/explaining the '\n is Environment newline unless follows \r then just newline' variation

OTOH it is the most common case/use and is simplest (single char) and is unchanged against *nix platform practice .....
(and \N does look a bit weird and could be error prone)

If go this way I'd go for a non context dependent change ( let \r\n fall as it may)...
\n is always Environment newline
\N is just newline

Re: Platform independent line terminators

PostPosted: Wed Sep 17, 2008 9:59 pm
by hopscc
Added ticket:42