octave-maintainers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Escape sequences in *printf family of functions


From: Rik
Subject: Re: Escape sequences in *printf family of functions
Date: Thu, 05 Apr 2012 09:27:13 -0700

On 04/04/2012 10:50 PM, John W. Eaton wrote:
> On  4-Apr-2012, Rik wrote:
>
> | I'm almost done adding an internal variable to control escape sequence
> | processing in single quoted strings.
>
> I don't think we want to add any internal variables that control
> things like this.  Can you post your patch to show what you have in
> mind?
>
> In Matlab strings, '\n' is two characters, backslash and n.
>
> In Matlab, some functions, like *printf, handle pairs of characters
> like this specially.  So, in  x = sprintf ('\n'),  x is a single
> character, ASCII 10, LF (new line).
>
> For compatibility with Matlab, Octave also does this for single-quoted
> strings.
>
> But Octave also has double-quoted strings that behave like C strings,
> so "\n" is one character, LF (new line).  These don't need any special
> handling in *printf or any other functions, and for double-quoted
> strings, that actually do contain \ followed by some other character,
> we don't want any special treatment, so for example, x = sprintf ("\\n")
> should result in x containing two characters, backslash and n.
>
> This behavior is intentional, and I think it is the correct way to
> handle things.  You get compatibility if you use single-quoted strings
> (any code written for Matlab will have to use single quotes for
> character strings) and double-quoted strings work as they do in C.
I think the decision is whether to follow Matlab's kludge for the sake of
compatibility or to separate the behaviors.  One mode would be consistent
and specific to Octave.  In this mode, single quotes are used for verbatim
input and escape sequences are not processed; Double quotes are used to
indicate that escape processing is desired.  This is self-consistent and
the pattern that all UNIX shells follow.  I have a strong, but personal,
preference for coding in that manner because I can guarantee, without
reading the documentation for each function, that I will get out what I
program in.

The second mode would chase Matlab compatibility.  Once you decide to be
inconsistent with single and double quotes then you need another standard
to decide which functions should implement it and which should not.  Since
flipping a coin on a per function basis isn't a great standard, we might as
well follow the whimsy of Matlab and use the same list of functions they
do.  I don't have a complete list but I have found the following are
affected: the template strings of printf(), sprintf(), fprintf(), the
pattern strings of regexp(), regexpi(), the pattern and replacement strings
of regexprep(), the message strings of error(), warning().

If we do go whole hog, and just implement Matlab compatibility, then we
should update the documentation to clearly point out the surprises that can
happen and direct users to regexptranslate() for translating strings to
escaped versions.

Attached are two patches on the issue.  One was already committed as a
changeset, but can be backed out if it is unnecessary.

--Rik 

Attachment: single_quote_var.cset
Description: Text document

Attachment: single_quote_impl.patch
Description: Text Data


reply via email to

[Prev in Thread] Current Thread [Next in Thread]