You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by su...@apache.org on 2018/11/06 16:53:43 UTC

groovy git commit: Refine escaping of `GProperties`

Repository: groovy
Updated Branches:
  refs/heads/master 187617ea8 -> d98ef8381


Refine escaping of `GProperties`


Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/d98ef838
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/d98ef838
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/d98ef838

Branch: refs/heads/master
Commit: d98ef838152aa8d7f1974c2fa8131e95e4f5d538
Parents: 187617e
Author: danielsun1106 <re...@hotmail.com>
Authored: Wed Nov 7 00:51:17 2018 +0800
Committer: Daniel Sun <su...@apache.org>
Committed: Wed Nov 7 00:53:18 2018 +0800

----------------------------------------------------------------------
 src/main/groovy/groovy/util/GProperties.groovy  | 28 +++++++----
 .../groovy/util/gproperties.properties          |  7 +++
 src/test/groovy/util/GPropertiesTest.groovy     | 49 ++++++++++++++++++++
 3 files changed, 74 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/d98ef838/src/main/groovy/groovy/util/GProperties.groovy
----------------------------------------------------------------------
diff --git a/src/main/groovy/groovy/util/GProperties.groovy b/src/main/groovy/groovy/util/GProperties.groovy
index fde01dc..d3b08c1 100644
--- a/src/main/groovy/groovy/util/GProperties.groovy
+++ b/src/main/groovy/groovy/util/GProperties.groovy
@@ -85,8 +85,7 @@ import java.util.regex.Pattern
 class GProperties extends Properties {
     private static final long serialVersionUID = 6112578636029876860L
     public static final String IMPORT_PROPERTIES_KEY = 'import.properties'
-    private static final Pattern INTERPOLATE_PATTERN = Pattern.compile(/[{](.+?)[}]/)
-    private static final Pattern ESCAPE_PATTERN = Pattern.compile(/[{]([{][^{}]*?[}])[}]/)
+    private static final Pattern INTERPOLATE_PATTERN = Pattern.compile(/([{]+)(.*?)([}]+)/)
     private static final String LEFT_CURLY_BRACE = '{'
     private static final String RIGHT_CURLY_BRACE = '}'
     private static final String COMMA = ','
@@ -148,17 +147,26 @@ class GProperties extends Properties {
             return value
         }
 
-        value = value.replaceAll(INTERPOLATE_PATTERN) { String _0, String _1 ->
-            if (_1.startsWith(LEFT_CURLY_BRACE) && _1.endsWith(RIGHT_CURLY_BRACE)) {
-                return _0
+        interpolate(value)
+    }
+
+    private String interpolate(String value) {
+        value.replaceAll(INTERPOLATE_PATTERN) { String _0, String _1, String _2, String _3 ->
+            int leftCnt = _1.count(LEFT_CURLY_BRACE)
+            int rightCnt = _3.count(RIGHT_CURLY_BRACE)
+            int minCnt = Math.min(leftCnt, rightCnt)
+            int escapeCnt = (minCnt / 2) as int
+
+            String left = LEFT_CURLY_BRACE * (escapeCnt + (leftCnt - minCnt))
+            String right = RIGHT_CURLY_BRACE * (escapeCnt + (rightCnt - minCnt))
+
+            if (minCnt % 2 == 0) {
+                return "${left}${_2}${right}"
             }
 
-            def p = this.getProperty(_1.trim())
-            null == p ? _0 : p
-        }
+            def p = this.getProperty(_2.trim())
 
-        value.replaceAll(ESCAPE_PATTERN) { String _0, String _1 ->
-            _1
+            "${left}${null == p ? _0 : p}${right}"
         }
     }
 

http://git-wip-us.apache.org/repos/asf/groovy/blob/d98ef838/src/test-resources/groovy/util/gproperties.properties
----------------------------------------------------------------------
diff --git a/src/test-resources/groovy/util/gproperties.properties b/src/test-resources/groovy/util/gproperties.properties
index 3c68452..33c0a2a 100644
--- a/src/test-resources/groovy/util/gproperties.properties
+++ b/src/test-resources/groovy/util/gproperties.properties
@@ -23,6 +23,12 @@ groovy.greeting.with.smile={groovy.greeting} :)
 groovy.greeting.with.missing=Hello,{none} {0}
 groovy.greeting.with.escapes=Hello,{{some.name}}
 groovy.greeting.with.escapes2=Hello, curly brace {{}}
+groovy.greeting.with.escapes3=Hello,{{{some.name}}}
+groovy.greeting.with.escapes4=Hello,{{{some.name}}
+groovy.greeting.with.escapes5=Hello,{{some.name}}}
+groovy.greeting.with.escapes6=Hello,{{{{some.name}}}}
+groovy.greeting.with.escapes7=Hello,{{some.name}
+groovy.greeting.with.escapes8=Hello,{some.name}}
 property.character=a
 property.character.invalid=abc
 property.byte=1
@@ -34,3 +40,4 @@ property.double=18.1104d
 property.boolean=true
 property.biginteger=20181104
 property.bigdecimal=2018.1104
+property.empty=Hello,{}

http://git-wip-us.apache.org/repos/asf/groovy/blob/d98ef838/src/test/groovy/util/GPropertiesTest.groovy
----------------------------------------------------------------------
diff --git a/src/test/groovy/util/GPropertiesTest.groovy b/src/test/groovy/util/GPropertiesTest.groovy
index 0d8b944..805d015 100644
--- a/src/test/groovy/util/GPropertiesTest.groovy
+++ b/src/test/groovy/util/GPropertiesTest.groovy
@@ -72,6 +72,13 @@ class GPropertiesTest extends GroovyTestCase {
         assert '''Hello,Daniel''' == gp.getProperty('greeting.daniel')
     }
 
+    void testInterpolate6() {
+        def gp = new GProperties()
+        gp.load(GPropertiesTest.getResourceAsStream('/groovy/util/gproperties.properties'))
+
+        assert '''Hello,{}''' == gp.getProperty('property.empty')
+    }
+
     void testEscape() {
         def gp = new GProperties()
         gp.load(GPropertiesTest.getResourceAsStream('/groovy/util/gproperties.properties'))
@@ -86,6 +93,48 @@ class GPropertiesTest extends GroovyTestCase {
         assert 'Hello, curly brace {}' == gp.getProperty('groovy.greeting.with.escapes2')
     }
 
+    void testEscape3() {
+        def gp = new GProperties()
+        gp.load(GPropertiesTest.getResourceAsStream('/groovy/util/gproperties.properties'))
+
+        assert 'Hello,{Daniel}' == gp.getProperty('groovy.greeting.with.escapes3')
+    }
+
+    void testEscape4() {
+        def gp = new GProperties()
+        gp.load(GPropertiesTest.getResourceAsStream('/groovy/util/gproperties.properties'))
+
+        assert 'Hello,{{some.name}' == gp.getProperty('groovy.greeting.with.escapes4')
+    }
+
+    void testEscape5() {
+        def gp = new GProperties()
+        gp.load(GPropertiesTest.getResourceAsStream('/groovy/util/gproperties.properties'))
+
+        assert 'Hello,{some.name}}' == gp.getProperty('groovy.greeting.with.escapes5')
+    }
+
+    void testEscape6() {
+        def gp = new GProperties()
+        gp.load(GPropertiesTest.getResourceAsStream('/groovy/util/gproperties.properties'))
+
+        assert 'Hello,{{some.name}}' == gp.getProperty('groovy.greeting.with.escapes6')
+    }
+
+    void testEscape7() {
+        def gp = new GProperties()
+        gp.load(GPropertiesTest.getResourceAsStream('/groovy/util/gproperties.properties'))
+
+        assert 'Hello,{Daniel' == gp.getProperty('groovy.greeting.with.escapes7')
+    }
+
+    void testEscape8() {
+        def gp = new GProperties()
+        gp.load(GPropertiesTest.getResourceAsStream('/groovy/util/gproperties.properties'))
+
+        assert 'Hello,Daniel}' == gp.getProperty('groovy.greeting.with.escapes8')
+    }
+
     void testGetCharacter() {
         def gp = new GProperties()
         gp.load(GPropertiesTest.getResourceAsStream('/groovy/util/gproperties.properties'))