Escape codes are not 100% portable

Jens Bauer via Digitalmars-d digitalmars-d at puremagic.com
Thu Apr 2 04:04:06 PDT 2015


Reading lexer.c, in order to find out what's causing me problems 
on my PowerMac, I came across this snippet, and I'd like to point 
out that it is not reliable:


             case '\r':
                 p++;
                 if (*p != '\n')                 // if CR stands 
by itself
                     endOfLine();
                 continue;                       // skip white 
space

             case '\n':
                 p++;
                 endOfLine();
                 continue;                       // skip white 
space


-The problem is that on some hosts,
\r gives you the character code 13, while \n gives you the 
character code 10,
on other hosts,
\r gives you the character code 10, while \n gives you the 
character code 13.

This is crazy, yes, but in order to be sure, that things will 
always work, I suggest always using hexadecimal numbers.

This is why picture formats, such as PNG and GIF do not specify 
their identifier as ASCII characters but in hex-codes.
PPM is an image format, that's supposed to be portable, but 
unfortunately, they did not know about this problem. This causes 
some platforms to write PPM formats, that can not be read on 
other platforms.

Usually, I prefer using enum or #define to create constants that 
translate to hexadecimal numbers.


More information about the Digitalmars-d mailing list