changeset 14527:8c988dabbe8e

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.
author Rik <octave@nomad.inbox5.com>
date Wed, 04 Apr 2012 21:08:38 -0700
parents e12945668746
children c67822beb7a4
files doc/interpreter/strings.txi src/octave.cc src/ov.cc src/ov.h
diffstat 4 files changed, 58 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- 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
-@samp{\"} embeds a double quote character.  In single-quoted strings, backslash
-is not a special character.  Here is an example showing the difference:
+@samp{\"} 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.
+
+@DOCSTRING(single_quote_escape_processing)
+
 @node Character Arrays
 @section Character Arrays
 
--- 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");
--- 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\
+@deftypefn  {Built-in Function} {@var{val} =} single_quote_escape_processing ()\n\
+@deftypefnx {Built-in Function} {@var{old_val} =} single_quote_escape_processing (@var{new_val})\n\
+@deftypefnx {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\
+@table @code\n\
+@item printf\n\
+@itemx sprintf\n\
+@itemx fprintf\n\
+Process format specification string @var{template}.\n\
+\n\
+@item error\n\
+Process format specification string @var{template}.\n\
+\n\
+@item regexp\n\
+@itemx regexpi\n\
+Process pattern string @var{pat}.\n\
+\n\
+@item regexprep\n\
+Process pattern string @var{pat} and replacement string @var{repstr}.\n\
+@end 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\
+@seealso{printf, sprintf, fprintf, error, regexp, regexpi, regexprep}\n\
+@end deftypefn")
+{
+  return SET_INTERNAL_VARIABLE (single_quote_escape_processing);
+}
--- 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