Forums

Platform independent line terminators

General discussion about Cobra. Releases and general news will also be posted here.
Feel free to ask questions or just say "Hello".

Platform independent line terminators

Postby hopscc » Sun Sep 07, 2008 9:09 pm

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)
hopscc
 
Posts: 632
Location: New Plymouth, Taranaki, New Zealand

Re: Platform independent line terminators

Postby Charles » Thu Sep 11, 2008 1:18 am

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]
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: Platform independent line terminators

Postby hopscc » Thu Sep 11, 2008 5:58 am

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
hopscc
 
Posts: 632
Location: New Plymouth, Taranaki, New Zealand

Re: Platform independent line terminators

Postby hopscc » Wed Sep 17, 2008 9:59 pm

Added ticket:42
hopscc
 
Posts: 632
Location: New Plymouth, Taranaki, New Zealand


Return to Discussion

Who is online

Users browsing this forum: No registered users and 22 guests

cron