bugGNU Octave - Bugs: bug #39774, Support octal and hexadecimal...

 
 

bug #39774: Support octal and hexadecimal sprintf escape sequences

Submitter:  Mike Miller <mtmiller>
Submitted:  Mon 12 Aug 2013 06:11:24 PM UTC
   
 
Category:  Octave Function Severity:  3 - Normal
Priority:  5 - Normal Item Group:  Matlab Compatibility
Status:  Fixed Assigned to:  None
Originator Name:  Open/Closed:  * Closed
Release:  * dev Operating System:  * Any
Fixed Release:  None Planned Release:  None
* Mandatory Fields

Add a New Comment Rich Markup
   

Thu 21 May 2015 12:19:44 AM UTC, comment #4: 

I modified do_string_escapes to add support for octal and hexadecimal sequences (http://hg.savannah.gnu.org/hgweb/octave/rev/29eb47fe8e8c).

Rik <rik5>
Group administrator
Thu 15 Aug 2013 03:53:14 AM UTC, comment #3: 

Yeah, after I reported this I traced it to do_string_escapes. This function is already called for the printf and scanf family of functions with single-quoted strings, but it's done in oct-stream.cc instead of file-io.cc.

Thanks for pointing out the similar functions in regexp.cc, though. Some similarity, the fix for this could be adapted to the FIXME in that file.

Mike Miller <mtmiller>
Group Member
Wed 14 Aug 2013 08:24:55 PM UTC, comment #2: 

We already do this for single-quoted strings in regular expressions.  Take a look at regexp.cc


  std::string pattern = args(1).string_value ();
  if (error_state)
    return retval;
  // Matlab compatibility.
  if (args(1).is_sq_string ())
    pattern = do_regexp_ptn_string_escapes (pattern);


The function do_regexp_ptn_string_escapes is special because the escape sequences are not qute the same as the ordinary *printf family.  However, we already have do_string_escapes in util.cc which does escape sequence processing.  You could code the same thing in file-io.cc for printf and check is_sq_string() before calling do_string_escapes.

do_string_escapes() would need to be modified to support hexadecimal and octal though.  Right now it just does the basics like "\n" -> NEWLINE.

Incidentally, I hate Matlab's single option for a character string delimiter.  It makes everything totally confusing in Octave because we do have non-interpreted single-quoted strings, except where they interact with a Matlab function in which case they are treated as double-quoted strings.

Rik <rik5>
Group administrator
Tue 13 Aug 2013 08:12:26 PM UTC, comment #1: 

What, you don't trust the Matlab documentation to accurately reflect Matlab's actual behavior?

If it is documented to behave this way, then I guess that's what we should do for Octave as well.

John W. Eaton <jwe>
Group administrator
Mon 12 Aug 2013 06:11:24 PM UTC, original submission:  

According to the Matlab documentation for fprintf and sprintf, the standard C escape sequences for octal and hexadecimal literal characters are supported.

http://www.mathworks.com/help/matlab/ref/sprintf.html

For Octave, note that this is about handling the escape sequences in the printf/fprintf/sprintf functions when the format string argument is given as a single-quoted string.

The following three sprintf commands should all return the same string. This is based on the Matlab documentation only, I would appreciate it if someone were able to confirm these in Matlab.


s = sprintf ('Octave')
s = sprintf ('\117\143\164\141\166\145')
s = sprintf ('\x4f\x63\x74\x61\x76\x65')


Instead, Octave does the following:


>> s = sprintf ('Octave')
s = Octave
>> s = sprintf ('\117\143\164\141\166\145')
warning: unrecognized escape sequence '\1' -- converting to '1'
warning: unrecognized escape sequence '\1' -- converting to '1'
warning: unrecognized escape sequence '\1' -- converting to '1'
warning: unrecognized escape sequence '\1' -- converting to '1'
warning: unrecognized escape sequence '\1' -- converting to '1'
warning: unrecognized escape sequence '\1' -- converting to '1'
s = 117143164141166145
>> s = sprintf ('\x4f\x63\x74\x61\x76\x65')
warning: unrecognized escape sequence '\x' -- converting to 'x'
warning: unrecognized escape sequence '\x' -- converting to 'x'
warning: unrecognized escape sequence '\x' -- converting to 'x'
warning: unrecognized escape sequence '\x' -- converting to 'x'
warning: unrecognized escape sequence '\x' -- converting to 'x'
warning: unrecognized escape sequence '\x' -- converting to 'x'
s = x4fx63x74x61x76x65


Mike Miller <mtmiller>
Group Member

 

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

Attach Files:
   
   
Comment:
   

No files currently attached

 

Depends on the following items: None found

Items that depend on this one: None found

 

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

    There are 0 votes so far. Votes easily highlight which items people would like to see resolved in priority, independently of the priority of the item set by tracker managers.

    Only group members can vote.

     

    Follow 2 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2015-05-21 rik5 StatusNone Fixed
        Open/ClosedOpen Closed

    Back to the top

    Powered by Savane 3.13-f8d8.
    Corresponding source code