octave-maintainers
[Top][All Lists]

## backslash escapes in character strings

 From: John W. Eaton Subject: backslash escapes in character strings Date: Fri, 1 Apr 2005 14:56:32 -0500

I think we may have discussed this before, but I'm not sure what the
outcome was.

Currently, Octave converts things like

"foo\nbar"

to the sequence of characters

f o o LF b a r

at the time the string is parsed (LF is the ascii linefeed character,
0x0A).  In Matlab, there is no conversion, so the sequence of
characters is

f o o \ n b a r

The \n is only special in some contexts, like when it is passed
through fprintf (as far as I know, in Matlab, if you want to embed a
LF character in a string, you need to use something like

s = sprintf ('foo\nbar');

Should Octave continue to behave as it does, or should it try to be
compatible here as well?

reason for wanting this change in behavior is that it makes things
like

cd c:\foo\bar

work as Windows users would expect without making cd a special command
(and introducing likely inconsistencies with other commands).

One possibility that might break less code would be to handle escapes
differently inside strings created with single quotes vs. strings
created with double quotes.  For compatibility, single-quote strings
would not perform the conversions and double-quote strings would
continue to behave as they do now.

We would have to keep track of whether a string was "double quoted" or
'single quoted', to avoid trouble with things like

sprintf ("foo\\not")

which should return

"foo\not"

becuase the \\ is converted to the single character \ when the string
is parsed, but it is followed by an 'n'.  But we don't want this kind
of string to be processed specially by the *printf format string
interpreter.

For compatibility, we would have to treat strings created by setstr
as 'single quoted' so that things like

sprintf (setstr ([102, 111, 111, 92, 110, 98, 97, 114]))

will return a string with an embedded LF character.