felix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gno...@apache.org
Subject svn commit: r1736018 - in /felix/trunk/gogo/runtime/src: main/java/org/apache/felix/gogo/runtime/Expander.java test/java/org/apache/felix/gogo/runtime/TestTokenizer.java
Date Mon, 21 Mar 2016 16:55:32 GMT
Author: gnodet
Date: Mon Mar 21 16:55:32 2016
New Revision: 1736018

URL: http://svn.apache.org/viewvc?rev=1736018&view=rev
Log:
Improve pattern matching

Modified:
    felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/Expander.java
    felix/trunk/gogo/runtime/src/test/java/org/apache/felix/gogo/runtime/TestTokenizer.java

Modified: felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/Expander.java
URL: http://svn.apache.org/viewvc/felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/Expander.java?rev=1736018&r1=1736017&r2=1736018&view=diff
==============================================================================
--- felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/Expander.java (original)
+++ felix/trunk/gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/Expander.java Mon
Mar 21 16:55:32 2016
@@ -181,7 +181,7 @@ public class Expander extends BaseTokeni
     protected List<? extends CharSequence> generateFileNames(CharSequence arg) throws
IOException {
         // Disable if currentDir is not set
         Path currentDir = evaluate.currentDir();
-        if (currentDir == null) {
+        if (currentDir == null || inQuote) {
             return Collections.singletonList(arg);
         }
         // Search for unquoted escapes
@@ -640,6 +640,7 @@ public class Expander extends BaseTokeni
             boolean flagC = false;
             boolean flagL = false;
             boolean flagU = false;
+            boolean flagG = false;
             boolean flagExpand = false;
             if (ch == '(') {
                 getch();
@@ -651,6 +652,9 @@ public class Expander extends BaseTokeni
                         case '@':
                             flagExpand = true;
                             break;
+                        case 'G':
+                            flagG = true;
+                            break;
                         case 'k':
                             flagk = true;
                             break;
@@ -703,7 +707,7 @@ public class Expander extends BaseTokeni
                 }
                 else {
                     int start = index - 1;
-                    while (ch != EOT && ch != '}' && ":-+=?#%/^|*?".indexOf(ch)
>= 0) {
+                    while (ch != EOT && ch != '}' && ":-+=?#%/".indexOf(ch)
>= 0) {
                         getch();
                     }
                     Token op = text.subSequence(start, index - 1);
@@ -741,23 +745,45 @@ public class Expander extends BaseTokeni
                             throw new IllegalArgumentException(name + ": parameter not set");
                         }
                     }
-                    else if (Token.eq("#", op) || Token.eq("##", op) || Token.eq("%", op)
|| Token.eq("%%", op)) {
+                    else if (Token.eq("#", op) || Token.eq("##", op)
+                            || Token.eq("%", op) || Token.eq("%%", op)
+                            || Token.eq("/", op) || Token.eq("//", op)) {
                         val1 = val1 instanceof Token ? evaluate.get(expand((Token) val1).toString())
: val1;
                         Object val2 = getValue();
                         if (val2 != null) {
                             String p = toRegexPattern(val2.toString(), op.length() == 1);
-                            String m = op.charAt(0) == '#' ? "^" + p : p + "$";
+                            String r;
+                            if (op.charAt(0) == '/') {
+                                if (ch == '/') {
+                                    getch();
+                                    r = getValue().toString();
+                                } else {
+                                    r = "";
+                                }
+                            } else {
+                                p = toRegexPattern(val2.toString(), op.length() == 1);
+                                r = "";
+                            }
+                            String m = op.charAt(0) == '#' ? "^" + p : op.charAt(0) == '%'
? p + "$" : p;
                             if (val1 instanceof Map) {
                                 val1 = toList((Map) val1, flagk, flagv);
                             }
                             if (val1 instanceof Collection) {
                                 List<String> l = new ArrayList<>();
                                 for (Object o : ((Collection) val1)) {
-                                    l.add(o.toString().replaceFirst(m, ""));
+                                    if (flagG) {
+                                        l.add(o.toString().replaceAll(m, r));
+                                    } else {
+                                        l.add(o.toString().replaceFirst(m, r));
+                                    }
                                 }
                                 val = l;
                             } else if (val1 != null) {
-                                val = val1.toString().replaceFirst(m, "");
+                                if (flagG) {
+                                    val = val1.toString().replaceAll(m, r);
+                                } else {
+                                    val = val1.toString().replaceFirst(m, r);
+                                }
                             }
                         } else {
                             val = val1;

Modified: felix/trunk/gogo/runtime/src/test/java/org/apache/felix/gogo/runtime/TestTokenizer.java
URL: http://svn.apache.org/viewvc/felix/trunk/gogo/runtime/src/test/java/org/apache/felix/gogo/runtime/TestTokenizer.java?rev=1736018&r1=1736017&r2=1736018&view=diff
==============================================================================
--- felix/trunk/gogo/runtime/src/test/java/org/apache/felix/gogo/runtime/TestTokenizer.java
(original)
+++ felix/trunk/gogo/runtime/src/test/java/org/apache/felix/gogo/runtime/TestTokenizer.java
Mon Mar 21 16:55:32 2016
@@ -195,6 +195,19 @@ public class TestTokenizer
     }
 
     @Test
+    public void testPatterns() throws Exception {
+        vars.clear();
+        vars.put("foo", "twinkle twinkle little star");
+        vars.put("sub", "t*e");
+        vars.put("rep", "spy");
+
+        assertEquals("spy twinkle little star", expand("${foo/${sub}/${rep}}"));
+        assertEquals("spy star", expand("${foo//${sub}/${rep}}"));
+        assertEquals("spy spy lispy star", expand("${(G)foo/${sub}/${rep}}"));
+        assertEquals("spy star", expand("${(G)foo//${sub}/${rep}}"));
+    }
+
+    @Test
     public void testExpand() throws Exception
     {
         final URI home = new URI("/home/derek");



Mime
View raw message