bugGNU Octave - Bugs: bug #44745, single quoted string in sprintf is...

 
 

bug #44745: single quoted string in sprintf is interpreted

Submitted by:  ederag <ederag>
Submitted on:  Mon 06 Apr 2015 05:37:53 PM UTC  
 
Category: Octave FunctionSeverity: 3 - Normal
Priority: 5 - NormalItem Group: Documentation
Status: FixedAssigned to: None
Originator Name: Open/Closed: Closed
Release: 4.0.0Operating System: GNU/Linux

Add a New Comment(Rich Markup)
   

You are not logged in

Please log in, so followups can be emailed to you.

 

(Jump to the original submission Jump to the original submission)

Wed 08 Apr 2015 04:49:37 AM UTC, comment #10:

Fixed in this cset (http://hg.savannah.gnu.org/hgweb/octave/rev/dbf2418a46dd).

Rik <rik5>
Project Administrator
Tue 07 Apr 2015 02:52:23 PM UTC, comment #9:

I proposed

"Beware that these functions will interpret the backslashes in the template string, even if this is a single quote string"

after

"The following functions are available for formatted output. They are modeled after the C language functions of the same name, but they interpret the format template differently in order to improve the performance of printing vector and matrix values."

in
https://www.gnu.org/software/octave/doc/interpreter/Formatted-Output.html

ederag <ederag>
Tue 07 Apr 2015 02:40:05 PM UTC, comment #8:

Yes, it should probably be documented. Can you suggest where, and what wording to use?

John W. Eaton <jwe>
Project Administrator
Tue 07 Apr 2015 02:26:25 PM UTC, comment #7:

@jwe, agreed, and using %s is a great idea, very clean. Thanks !

But current beaviour can be surprising, shouldn't it be documented somewhere ?
This is why I chose "incorrect documentation" as item group,
and proposed a complement for the documentation,
at the end of the original submission.

ederag <ederag>
Tue 07 Apr 2015 11:48:32 AM UTC, comment #6:

OK, I think this came up before, and once I saw that Matlab produced a warning and completely dropped the escape sequence, I decided to not do anything about it.

You can write something like

if you want to be sure that an escape sequence won't be processed. This is generally what you will have to do anyway if you want to insert things like \theta because I don't see a way to guess whether you really want \theta or a tab followed by 'heta'.

John W. Eaton <jwe>
Project Administrator
Tue 07 Apr 2015 04:04:09 AM UTC, comment #5:

>> s = sprintf('\z')

Warning: Control Character '\z' is not valid. See 'doc sprintf' for control characters valid in the format string.

s =

Empty string: 1-by-0

>> version


ans =

8.5.0.197613 (R2015a)

Michael Godfrey <godfrey>
Project Member
Tue 07 Apr 2015 01:28:04 AM UTC, comment #4:

I agree it would be good to get confirmation on what Matlab's sprintf/fprintf does with invalid backslash escape sequences. What does Matlab produce with

I looked through the manual and the backslash string escapes are not mentioned anywhere in the section "C-Style I/O Functions". They are documented elsewhere as a feature of double-quoted strings only. So I can understand the confusion if someone is not familiar with the printf family of functions and that this is a normal expected feature.

Mike Miller <mtmiller>
Project Administrator
Mon 06 Apr 2015 08:09:13 PM UTC, comment #3:

@Rik, Thanks for your support. I did propose a doc change in the end of the original submission. Do you mean that a patch is required, or that it is not clear enough ?

@jwe, that would be nice, provided that '\nu' and '\theta' are handled correctly as well, when the sprintf result is passed to the tex interpreter, as in
title(sprintf('\nu = %.1f', 0.1))
and
title(sprintf('\theta = %.1f', 0.1))

ederag <ederag>
Mon 06 Apr 2015 06:50:38 PM UTC, comment #2:

When transforming escape sequences in single-quote strings that appaear in *printf formats, maybe we should just transform the escape sequences that Matlab recognizes and silently ignore the rest, perserving the backslash? If that's what Matlab does, then we should probably do the same.

John W. Eaton <jwe>
Project Administrator
Mon 06 Apr 2015 06:14:18 PM UTC, comment #1:

Coming from a UNIX shell programming environment I hate this as well. Matlab, however, only has the single quote character for all strings. So for Matlab compatability, Octave expands escape sequences like "\n" or "\t"--even within single quotes--if the function is sprintf, printf, regexp, regexprep, and probably scanf too.

Can you suggest a documentation change that would be clear about this?

Rik <rik5>
Project Administrator
Mon 06 Apr 2015 05:37:53 PM UTC, original submission:

yields

This is unexpected, since the documentation for strings states that
"In single-quoted strings, backslash is not a special character"

And the sprintf documentation does not appear to mention anything about that:
"This is like printf, except that the output is returned as a string. Unlike the C library function, which requires you to provide a suitably sized string as an argument, Octave’s sprintf function returns the string, automatically sized to hold all of the items converted."

Is it a bug, or is it done on purpose ?
I infer the latter, from a comment from Soren:
"deviate from this rule just as the '*printf' functions do."

Then, after

"The following functions are available for formatted output. They are modeled after the C language functions of the same name, but they interpret the format template differently in order to improve the performance of printing vector and matrix values."

something like

"Beware that these functions will interpret the backslashes in the template string, even if this is a single quote string"

could be helpful ?

Note this happens also under 3.8.2.

ederag <ederag>

 

(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attach File(s):
   
   
Comment:
   

No files currently attached

 

Depends on the following items: None found

Items that depend on this one: None found

 

Carbon-Copy List
  • -unavailable- added by godfrey (Posted a comment)
  • -unavailable- added by mtmiller (Posted a comment)
  • -unavailable- added by jwe (Posted a comment)
  • -unavailable- added by rik5 (Posted a comment)
  • -unavailable- added by ederag (Submitted the item)
  •  

    Do you think this task is very important?
    If so, you can click here to add your encouragement to it.
    This task has 0 encouragements so far.

    Only project members can vote.

     

    Please enter the title of George Orwell's famous dystopian book (it's a date):

     

     

    Follow 3 latest changes.

    Date Changed By Updated Field Previous Value => Replaced By
    Tue 02 Jun 2015 04:41:47 PM UTCjweRelease4.0.0-rc2=>4.0.0
    Wed 08 Apr 2015 04:49:37 AM UTCrik5StatusNone=>Fixed
      Open/ClosedOpen=>Closed

    Back to the top


    Powered by Savane 3.1-cleanup1