# HG changeset patch # User Rik # Date 1333598918 25200 # Node ID 8c988dabbe8e61bac8067cb251689b674e07b857 # Parent e12945668746d81a5e06e28e4b0df8b9a9dc3556 Add new internal variable single_quote_escape_processing * strings.txi: Add single_quote_escape_processing to documentation * octave.cc (maximum_braindamage): Turn variable on in --traditional mode. * ov.cc (Fsingle_quote_escape_processing): New function to query and set variable. * ov.h: Declare Vsingle_quote_escape_processing variable. diff -r e12945668746 -r 8c988dabbe8e doc/interpreter/strings.txi --- a/doc/interpreter/strings.txi Wed Apr 04 19:28:16 2012 -0700 +++ b/doc/interpreter/strings.txi Wed Apr 04 21:08:38 2012 -0700 @@ -69,8 +69,8 @@ In double-quoted strings, the backslash character is used to introduce @dfn{escape sequences} that represent other characters. For example, @samp{\n} embeds a newline character in a double-quoted string and address@hidden"} embeds a double quote character. In single-quoted strings, backslash -is not a special character. Here is an example showing the difference: address@hidden"} embeds a double quote character. In single-quoted strings, the +backslash is not a special character. An example showing the difference is @example @group @@ -81,7 +81,7 @@ @end group @end example -Here is a table of all the escape sequences used in Octave (within +Below is a table of all escape sequences recognized by Octave (within double quoted strings). They are the same as those used in the C programming language. @@ -147,13 +147,21 @@ @end group @end example -In scripts the two different string types can be distinguished if necessary +In scripts the two different string types can be distinguished, if necessary, by using @code{is_dq_string} and @code{is_sq_string}. @DOCSTRING(is_dq_string) @DOCSTRING(is_sq_string) +The use of double quotes for strings with escape sequences and single quotes +for verbatim input is quite straightforward. However, @sc{matlab} behavior +is inconsistent in this regard and in certain instances will do escape sequence +processing even within single quotes. When absolute compatibility is essential +Octave can perform the same processing. + address@hidden(single_quote_escape_processing) + @node Character Arrays @section Character Arrays diff -r e12945668746 -r 8c988dabbe8e src/octave.cc --- a/src/octave.cc Wed Apr 04 19:28:16 2012 -0700 +++ b/src/octave.cc Wed Apr 04 21:08:38 2012 -0700 @@ -615,6 +615,7 @@ "%%-- %D %I:%M %p --%%"); bind_internal_variable ("page_screen_output", false); bind_internal_variable ("print_empty_dimensions", false); + bind_internal_variable ("single_quote_escape_processing", true); disable_warning ("Octave:abbreviated-property-match"); disable_warning ("Octave:fopen-file-in-path"); diff -r e12945668746 -r 8c988dabbe8e src/ov.cc --- a/src/ov.cc Wed Apr 04 19:28:16 2012 -0700 +++ b/src/ov.cc Wed Apr 04 21:08:38 2012 -0700 @@ -87,6 +87,9 @@ #include "utils.h" #include "variables.h" +// Expand escape sequences even in single quoted strings (Matlab compatibility) +bool Vsingle_quote_escape_processing = false; + // We are likely to have a lot of octave_value objects to allocate, so // make the grow_size large. DEFINE_OCTAVE_ALLOCATOR2(octave_value, 1024); @@ -3044,3 +3047,42 @@ %!error is_dq_string () %!error is_dq_string ("foo", 2) */ + +DEFUN (single_quote_escape_processing, args, nargout, + "-*- texinfo -*-\n\ address@hidden {Built-in Function} address@hidden =} single_quote_escape_processing ()\n\ address@hidden {Built-in Function} address@hidden =} single_quote_escape_processing (@var{new_val})\n\ address@hidden {Built-in Function} {} single_quote_escape_processing (@var{new_val}, \"local\")\n\ +Query or set the internal variable that controls whether Octave processes\n\ +escape sequences within single quoted strings.\n\ +\n\ +Ordinarily, escape sequences such as \"\\n\" => newline are only processed\n\ +in double quoted strings. However, @sc{matlab} performs escape sequence\n\ +processing even for single quoted strings for certain functions. This\n\ +variable enables @sc{matlab} compatibility for these instances:\n\ +\n\ address@hidden @code\n\ address@hidden printf\n\ address@hidden sprintf\n\ address@hidden fprintf\n\ +Process format specification string @var{template}.\n\ +\n\ address@hidden error\n\ +Process format specification string @var{template}.\n\ +\n\ address@hidden regexp\n\ address@hidden regexpi\n\ +Process pattern string @var{pat}.\n\ +\n\ address@hidden regexprep\n\ +Process pattern string @var{pat} and replacement string @var{repstr}.\n\ address@hidden table\n\ +\n\ +When called from inside a function with the \"local\" option, the variable is\n\ +changed locally for the function and any subroutines it calls. The original\n\ +variable value is restored when exiting the function.\n\ address@hidden, sprintf, fprintf, error, regexp, regexpi, regexprep}\n\ address@hidden deftypefn") +{ + return SET_INTERNAL_VARIABLE (single_quote_escape_processing); +} diff -r e12945668746 -r 8c988dabbe8e src/ov.h --- a/src/ov.h Wed Apr 04 19:28:16 2012 -0700 +++ b/src/ov.h Wed Apr 04 21:08:38 2012 -0700 @@ -1387,4 +1387,7 @@ DEF_DUMMY_VALUE_EXTRACTOR (octave_value, octave_value ()) #undef DEF_DUMMY_VALUE_EXTRACTOR +// Expand escape sequences even in single quoted strings (Matlab compatibility) +extern bool Vsingle_quote_escape_processing; + #endif