changeset 20215:89616a98b02e

regexptranslate.m: Fix 'escape' option list of special chars (bug #45084). Also speed up 'wildcard' option by 44% by using strrep rather than regexprep. * regexptranslate.m: Explicitly list all all special regexp chars in the regular expression which escapes them. Replace regexprep calls with strrep function calls in 'wildcard' option processing.
author Rik <rik@octave.org>
date Tue, 12 May 2015 08:46:53 -0700
parents 5212cda4ac0e
children b2532deba721
files scripts/strings/regexptranslate.m
diffstat 1 files changed, 5 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/strings/regexptranslate.m	Sun May 10 21:37:19 2015 -0700
+++ b/scripts/strings/regexptranslate.m	Tue May 12 08:46:53 2015 -0700
@@ -64,11 +64,11 @@
 
   op = tolower (op);
   if (strcmp ("wildcard", op))
-    y = regexprep (regexprep (regexprep (s, '\.', '\.'),
-                                            '\*', '.*'),
-                                            '\?', '.');
+    y = strrep (strrep (strrep (s, '.', '\.'),
+                                   '*', '.*'),
+                                   '?', '.');
   elseif (strcmp ("escape", op))
-    y = regexprep (s, '([^\w])', '\\$1');
+    y = regexprep (s, '([][(){}.*+?^$|\\])', '\\$1');
   else
     error ("regexptranslate: invalid operation OP");
   endif
@@ -77,7 +77,7 @@
 
 
 %!assert (regexptranslate ("wildcard", "/a*b?c."), "/a.*b.c\\.")
-%!assert (regexptranslate ("escape", '$.?[abc]'), '\$\.\?\[abc\]')
+%!assert (regexptranslate ("escape", '^.?[abc]$'), '\^\.\?\[abc\]\$')
 
 ## Test input validation
 %!error <Invalid call to regexptranslate> regexptranslate ()