Encapsulate regexes in a non-capturing group 35/68435/4
authorRobert Varga <robert.varga@pantheon.tech>
Tue, 20 Feb 2018 18:12:19 +0000 (19:12 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Wed, 21 Feb 2018 11:05:25 +0000 (12:05 +0100)
Regular expressions are pesky, nwo we are being bitten by branches.
Let's just encapsulate the internal pattern into a non-capturing
group and be done with it.

JIRA: YANGTOOLS-798
Change-Id: If3b3b2fb35ecd336bc23fd7acaf29efa48eb9b74
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
yang/yang-data-impl/src/test/java/org/opendaylight/yangtools/yang/data/impl/codecs/StringPatternCheckingCodecTest.java
yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/util/RegexUtils.java
yang/yang-model-util/src/test/java/org/opendaylight/yangtools/yang/model/util/Bug4079Test.java
yang/yang-model-util/src/test/java/org/opendaylight/yangtools/yang/model/util/Bug5410Test.java
yang/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/Bug5410Test.java
yang/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/Bug4623Test.java
yang/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/Bug5396Test.java
yang/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/Bug6180Test.java
yang/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/EffectiveStatementTypeTest.java
yang/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/TypesResolutionTest.java
yang/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/YangParserTest.java

index 643a7a5012876ad93964cebc9854d55d3974c6c3..4cf56becb503188523168ebf5c74595c5caede50 100644 (file)
@@ -57,7 +57,7 @@ public class StringPatternCheckingCodecTest {
             fail("Exception should have been thrown.");
         } catch (final IllegalArgumentException ex) {
             LOG.debug("IllegalArgumentException was thrown as expected", ex);
-            assertEquals("Supplied value does not match the regular expression ^[A-Z]+$.", ex.getMessage());
+            assertEquals("Supplied value does not match the regular expression ^(?:[A-Z]+)$.", ex.getMessage());
         }
     }
 }
index 0991750e6ca55ec6f282c582cf8196f762f439c5..7aab1eaf94658cab1e118d2a421788607d6558be 100644 (file)
@@ -246,7 +246,8 @@ public final class RegexUtils {
      * @return Java-compatible regex
      */
     public static String getJavaRegexFromXSD(final String xsdRegex) {
-        return "^" + fixUnicodeScriptPattern(escapeChars(xsdRegex)) + '$';
+        // Note: we are using a non-capturing group to deal with internal structure issues, like branches and similar.
+        return "^(?:" + fixUnicodeScriptPattern(escapeChars(xsdRegex)) + ")$";
     }
 
     /*
index 5333c9882b4eae80d6c1b9a03f19803264c4ee45..28db4a52b6b45d054e872f42d11dae9180c399e2 100644 (file)
@@ -8,8 +8,11 @@
 package org.opendaylight.yangtools.yang.model.util;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
 
+import java.util.function.Predicate;
 import java.util.regex.Pattern;
 import java.util.regex.PatternSyntaxException;
 import org.junit.Test;
@@ -19,96 +22,96 @@ public class Bug4079Test {
     @Test
     public void testValidPatternFix() {
         String fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\p{IsArrows})*+");
-        assertEquals("^(\\p{InArrows})*+$", fixedUnicodeScriptPattern);
+        assertEquals("^(?:(\\p{InArrows})*+)$", fixedUnicodeScriptPattern);
         assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
 
         fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\p{IsDingbats})++");
-        assertEquals("^(\\p{InDingbats})++$", fixedUnicodeScriptPattern);
+        assertEquals("^(?:(\\p{InDingbats})++)$", fixedUnicodeScriptPattern);
         assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
 
         fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\p{IsSpecials})?+");
-        assertEquals("^(\\p{InSpecials})?+$", fixedUnicodeScriptPattern);
+        assertEquals("^(?:(\\p{InSpecials})?+)$", fixedUnicodeScriptPattern);
         assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
 
         fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\p{IsBatak}){4}+");
-        assertEquals("^(\\p{IsBatak}){4}+$", fixedUnicodeScriptPattern);
+        assertEquals("^(?:(\\p{IsBatak}){4}+)$", fixedUnicodeScriptPattern);
         assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
 
         fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\p{IsLatin}){4,6}+");
-        assertEquals("^(\\p{IsLatin}){4,6}+$", fixedUnicodeScriptPattern);
+        assertEquals("^(?:(\\p{IsLatin}){4,6}+)$", fixedUnicodeScriptPattern);
         assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
 
         fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\p{IsTibetan}){4,}+");
-        assertEquals("^(\\p{IsTibetan}){4,}+$", fixedUnicodeScriptPattern);
+        assertEquals("^(?:(\\p{IsTibetan}){4,}+)$", fixedUnicodeScriptPattern);
         assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
 
         fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\p{IsAlphabetic}){4}?");
-        assertEquals("^(\\p{IsAlphabetic}){4}?$", fixedUnicodeScriptPattern);
+        assertEquals("^(?:(\\p{IsAlphabetic}){4}?)$", fixedUnicodeScriptPattern);
         assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
 
         fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\p{IsLowercase}){4,6}?");
-        assertEquals("^(\\p{IsLowercase}){4,6}?$", fixedUnicodeScriptPattern);
+        assertEquals("^(?:(\\p{IsLowercase}){4,6}?)$", fixedUnicodeScriptPattern);
         assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
 
         fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\p{IsUppercase}){4,}?");
-        assertEquals("^(\\p{IsUppercase}){4,}?$", fixedUnicodeScriptPattern);
+        assertEquals("^(?:(\\p{IsUppercase}){4,}?)$", fixedUnicodeScriptPattern);
         assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
 
         fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\p{IsBasicLatin}|\\p{IsLatin-1Supplement})*");
-        assertEquals("^(\\p{InBasicLatin}|\\p{InLatin-1Supplement})*$", fixedUnicodeScriptPattern);
+        assertEquals("^(?:(\\p{InBasicLatin}|\\p{InLatin-1Supplement})*)$", fixedUnicodeScriptPattern);
         assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
 
         fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\p{InBasicLatin}|\\p{InLatin-1Supplement})+");
-        assertEquals("^(\\p{InBasicLatin}|\\p{InLatin-1Supplement})+$", fixedUnicodeScriptPattern);
+        assertEquals("^(?:(\\p{InBasicLatin}|\\p{InLatin-1Supplement})+)$", fixedUnicodeScriptPattern);
         assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
 
         fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\p{IsBasicLatin}|\\p{InLatin-1Supplement})?");
-        assertEquals("^(\\p{InBasicLatin}|\\p{InLatin-1Supplement})?$", fixedUnicodeScriptPattern);
+        assertEquals("^(?:(\\p{InBasicLatin}|\\p{InLatin-1Supplement})?)$", fixedUnicodeScriptPattern);
         assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
 
         fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\p{InBasicLatin}|\\p{IsLatin-1Supplement}){4}");
-        assertEquals("^(\\p{InBasicLatin}|\\p{InLatin-1Supplement}){4}$", fixedUnicodeScriptPattern);
+        assertEquals("^(?:(\\p{InBasicLatin}|\\p{InLatin-1Supplement}){4})$", fixedUnicodeScriptPattern);
         assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
 
         fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\p{IsLatin}|\\p{IsArmenian}){2,4}");
-        assertEquals("^(\\p{IsLatin}|\\p{IsArmenian}){2,4}$", fixedUnicodeScriptPattern);
+        assertEquals("^(?:(\\p{IsLatin}|\\p{IsArmenian}){2,4})$", fixedUnicodeScriptPattern);
         assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
 
         fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\p{IsLatin}|\\p{IsBasicLatin}){2,}");
-        assertEquals("^(\\p{IsLatin}|\\p{InBasicLatin}){2,}$", fixedUnicodeScriptPattern);
+        assertEquals("^(?:(\\p{IsLatin}|\\p{InBasicLatin}){2,})$", fixedUnicodeScriptPattern);
         assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
 
         fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\p{IsBasicLatin}|\\p{IsLatin})*?");
-        assertEquals("^(\\p{InBasicLatin}|\\p{IsLatin})*?$", fixedUnicodeScriptPattern);
+        assertEquals("^(?:(\\p{InBasicLatin}|\\p{IsLatin})*?)$", fixedUnicodeScriptPattern);
         assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
 
         fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD(
                 "(\\p{IsBasicLatin}|\\p{IsLatin-1Supplement}|\\p{IsArrows})+?");
-        assertEquals("^(\\p{InBasicLatin}|\\p{InLatin-1Supplement}|\\p{InArrows})+?$", fixedUnicodeScriptPattern);
+        assertEquals("^(?:(\\p{InBasicLatin}|\\p{InLatin-1Supplement}|\\p{InArrows})+?)$", fixedUnicodeScriptPattern);
         assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
 
         fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD(
                 "(\\p{InBasicLatin}|\\p{IsLatin-1Supplement}|\\p{IsLatin})??");
-        assertEquals("^(\\p{InBasicLatin}|\\p{InLatin-1Supplement}|\\p{IsLatin})??$", fixedUnicodeScriptPattern);
+        assertEquals("^(?:(\\p{InBasicLatin}|\\p{InLatin-1Supplement}|\\p{IsLatin})??)$", fixedUnicodeScriptPattern);
         assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
 
         fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\\\\\p{IsBasicLatin})*+");
-        assertEquals("^(\\\\\\p{InBasicLatin})*+$", fixedUnicodeScriptPattern);
+        assertEquals("^(?:(\\\\\\p{InBasicLatin})*+)$", fixedUnicodeScriptPattern);
         assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
 
         fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\\\\\\\\\p{IsBasicLatin})*+");
-        assertEquals("^(\\\\\\\\\\p{InBasicLatin})*+$", fixedUnicodeScriptPattern);
+        assertEquals("^(?:(\\\\\\\\\\p{InBasicLatin})*+)$", fixedUnicodeScriptPattern);
         assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
 
         fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\\\\\\\\\\\\\p{IsBasicLatin})*+");
-        assertEquals("^(\\\\\\\\\\\\\\p{InBasicLatin})*+$", fixedUnicodeScriptPattern);
+        assertEquals("^(?:(\\\\\\\\\\\\\\p{InBasicLatin})*+)$", fixedUnicodeScriptPattern);
         assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
     }
 
     @Test(expected = PatternSyntaxException.class)
     public void testInvalidPattern() {
         String fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\\\p{IsBasicLatin})*+");
-        assertEquals("^(\\\\p{IsBasicLatin})*+$", fixedUnicodeScriptPattern);
+        assertEquals("^(?:(\\\\p{IsBasicLatin})*+)$", fixedUnicodeScriptPattern);
         // should throw exception
         Pattern.compile(fixedUnicodeScriptPattern);
     }
@@ -117,7 +120,7 @@ public class Bug4079Test {
     public void testInvalidPattern2() {
         String fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD(
             "(\\p{IsSpecials}|\\\\\\\\p{IsBasicLatin})*+");
-        assertEquals("^(\\p{InSpecials}|\\\\\\\\p{IsBasicLatin})*+$", fixedUnicodeScriptPattern);
+        assertEquals("^(?:(\\p{InSpecials}|\\\\\\\\p{IsBasicLatin})*+)$", fixedUnicodeScriptPattern);
         // should throw exception
         Pattern.compile(fixedUnicodeScriptPattern);
     }
@@ -126,8 +129,21 @@ public class Bug4079Test {
     public void testInvalidPattern3() {
         String fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD(
             "(\\\\\\\\\\\\p{IsBasicLatin}|\\p{IsTags})*+");
-        assertEquals("^(\\\\\\\\\\\\p{IsBasicLatin}|\\p{IsTags})*+$", fixedUnicodeScriptPattern);
+        assertEquals("^(?:(\\\\\\\\\\\\p{IsBasicLatin}|\\p{IsTags})*+)$", fixedUnicodeScriptPattern);
         // should throw exception
         Pattern.compile(fixedUnicodeScriptPattern);
     }
+
+    @Test
+    public void testCorrectBranches() {
+        String str = RegexUtils.getJavaRegexFromXSD("a|bb");
+        assertEquals("^(?:a|bb)$", str);
+        Predicate<String> pred = Pattern.compile(str).asPredicate();
+
+        assertTrue(pred.test("a"));
+        assertTrue(pred.test("bb"));
+        assertFalse(pred.test("ab"));
+        assertFalse(pred.test("abb"));
+        assertFalse(pred.test("ac"));
+    }
 }
index 41daafa856d05d4f579bfa9a6033e6ed6d2d7394..0e997093796c9b3716bdc1f4ff3020e0ef3b7b1b 100644 (file)
@@ -118,26 +118,29 @@ public class Bug5410Test {
 
     @Test
     public void testJavaRegexFromXSD() {
-        testPattern("^[^:]+$", "^\\^[^:]+\\$$", ImmutableList.of("^a$", "^abc$"),
+        testPattern("^[^:]+$", "^(?:\\^[^:]+\\$)$", ImmutableList.of("^a$", "^abc$"),
                 ImmutableList.of("abc$", "^abc", "^a:bc$"));
-        testPattern("^[$^]$", "^\\^[$^]\\$$", ImmutableList.of("^^$", "^$$"), ImmutableList.of("^^", "^$", "$^", "$$"));
-        testPattern("[$-%]+", "^[$-%]+$", ImmutableList.of("$", "%", "%$"), ImmutableList.of("$-", "$-%", "-", "^"));
-        testPattern("[$-&]+", "^[$-&]+$", ImmutableList.of("$", "%&", "%$", "$%&"), ImmutableList.of("#", "$-&", "'"));
-
-        testPattern("[a-z&&[^m-p]]+", "^[a-z&&[^m-p]]+$", ImmutableList.of("a", "z", "az"),
+        testPattern("^[$^]$", "^(?:\\^[$^]\\$)$", ImmutableList.of("^^$", "^$$"),
+            ImmutableList.of("^^", "^$", "$^", "$$"));
+        testPattern("[$-%]+", "^(?:[$-%]+)$", ImmutableList.of("$", "%", "%$"),
+            ImmutableList.of("$-", "$-%", "-", "^"));
+        testPattern("[$-&]+", "^(?:[$-&]+)$", ImmutableList.of("$", "%&", "%$", "$%&"),
+            ImmutableList.of("#", "$-&", "'"));
+
+        testPattern("[a-z&&[^m-p]]+", "^(?:[a-z&&[^m-p]]+)$", ImmutableList.of("a", "z", "az"),
                 ImmutableList.of("m", "anz", "o"));
-        testPattern("^[\\[-b&&[^^-a]]+$", "^\\^[\\[-b&&[^^-a]]+\\$$", ImmutableList.of("^[$", "^\\$", "^]$", "^b$"),
+        testPattern("^[\\[-b&&[^^-a]]+$", "^(?:\\^[\\[-b&&[^^-a]]+\\$)$", ImmutableList.of("^[$", "^\\$", "^]$", "^b$"),
                 ImmutableList.of("^a$", "^^$", "^_$"));
 
-        testPattern("[^^-~&&[^$-^]]", "^[^^-~&&[^$-^]]$", ImmutableList.of("!", "\"", "#"),
+        testPattern("[^^-~&&[^$-^]]", "^(?:[^^-~&&[^$-^]])$", ImmutableList.of("!", "\"", "#"),
                 ImmutableList.of("a", "A", "z", "Z", "$", "%", "^", "}"));
-        testPattern("\\\\\\[^[^^-~&&[^$-^]]", "^\\\\\\[\\^[^^-~&&[^$-^]]$",
+        testPattern("\\\\\\[^[^^-~&&[^$-^]]", "^(?:\\\\\\[\\^[^^-~&&[^$-^]])$",
                 ImmutableList.of("\\[^ ", "\\[^!", "\\[^\"", "\\[^#"),
                 ImmutableList.of("\\[^a", "\\[^A", "\\[^z", "\\[^Z", "\\[^$", "\\[^%", "\\[^^", "\\[^}"));
-        testPattern("^\\[^\\\\[^^-b&&[^\\[-\\]]]\\]^", "^\\^\\[\\^\\\\[^^-b&&[^\\[-\\]]]\\]\\^$",
+        testPattern("^\\[^\\\\[^^-b&&[^\\[-\\]]]\\]^", "^(?:\\^\\[\\^\\\\[^^-b&&[^\\[-\\]]]\\]\\^)$",
                 ImmutableList.of("^[^\\c]^", "^[^\\Z]^"),
                 ImmutableList.of("^[^\\[]^", "^[^\\\\]^", "^[^\\]]^", "^[^\\^]^", "^[^\\_]^", "^[^\\b]^"));
-        testPattern("[\\^]$", "^[\\^]\\$$", ImmutableList.of("^$"),
+        testPattern("[\\^]$", "^(?:[\\^]\\$)$", ImmutableList.of("^$"),
                 ImmutableList.of("^", "$", "$^", "\\", "\\^", "\\^\\", "\\^\\$"));
     }
 
@@ -160,7 +163,7 @@ public class Bug5410Test {
     }
 
     private static void testPattern(final String xsdRegex, final String unanchoredJavaRegex) {
-        testPattern(xsdRegex, '^' + unanchoredJavaRegex + '$', ImmutableList.of(), ImmutableList.of());
+        testPattern(xsdRegex, "^(?:" + unanchoredJavaRegex + ")$", ImmutableList.of(), ImmutableList.of());
     }
 
     private static void testPattern(final String javaRegex, final List<String> positiveMatches,
index e9a65eea4c8c6cbf20098ba797279a0518d84843..8e4903b041d3584f1185fe71f7eaf7999402184d 100644 (file)
@@ -40,9 +40,9 @@ public class Bug5410Test {
         assertEquals(expectedYangRegex, rawRegex);
 
         final String javaRegexFromYang = pattern.getJavaPatternString();
-        final String expectedJavaRegex = "^\\$0\\$.*|\\$1\\$[a-zA-Z0-9./]{1,8}\\$[a-zA-Z0-9./]{22}|\\$5\\$"
+        final String expectedJavaRegex = "^(?:\\$0\\$.*|\\$1\\$[a-zA-Z0-9./]{1,8}\\$[a-zA-Z0-9./]{22}|\\$5\\$"
                 + "(rounds=\\d+\\$)?[a-zA-Z0-9./]{1,16}\\$[a-zA-Z0-9./]{43}|\\$6\\$(rounds=\\d+\\$)?"
-                + "[a-zA-Z0-9./]{1,16}\\$[a-zA-Z0-9./]{86}$";
+                + "[a-zA-Z0-9./]{1,16}\\$[a-zA-Z0-9./]{86})$";
         assertEquals(expectedJavaRegex, javaRegexFromYang);
 
         final String value = "$6$AnrKGc0V$B/0/A.pWg4HrrA6YiEJOtFGibQ9Fmm5.4rI/"
index a5fb57d3dcc1705629c313912cb1d27e6f456611..8ec6becc39e561836e72b284a87bfdf40f64b560 100644 (file)
@@ -65,7 +65,7 @@ public class Bug4623Test {
         Assert.assertEquals(Integer.valueOf(10), span.upperEndpoint());
 
         final PatternConstraint patternConstraint = patternConstraints.get(0);
-        Assert.assertEquals(patternConstraint.getJavaPatternString(), "^[0-9a-fA-F]$");
+        Assert.assertEquals(patternConstraint.getRegularExpressionString(), "[0-9a-fA-F]");
     }
 
     @Test
@@ -107,7 +107,7 @@ public class Bug4623Test {
         Assert.assertEquals(Integer.valueOf(10), lengthConstraint.upperEndpoint());
 
         final PatternConstraint patternConstraint = patternConstraints.get(0);
-        Assert.assertEquals(patternConstraint.getJavaPatternString(), "^[0-9a-fA-F]$");
+        Assert.assertEquals(patternConstraint.getRegularExpressionString(), "[0-9a-fA-F]");
     }
 
     @Test
@@ -150,7 +150,7 @@ public class Bug4623Test {
         Assert.assertEquals(Integer.valueOf(10), lengthConstraint.upperEndpoint());
 
         final PatternConstraint patternConstraint = patternConstraints.get(0);
-        Assert.assertEquals(patternConstraint.getJavaPatternString(), "^[0-9a-fA-F]$");
+        Assert.assertEquals(patternConstraint.getRegularExpressionString(), "[0-9a-fA-F]");
     }
 
     private static Module typesModule(final SchemaContext context) {
index 0e287e19faf188bd0c886ff7c74c79cbd5a45605..caa5a82eb580cc5bbd6c03be4c3e9d16e7b162e2 100644 (file)
@@ -82,10 +82,10 @@ public class Bug5396Test {
         final PatternConstraint patternConstraint2 = patternConstraints2.get(0);
         final PatternConstraint patternConstraint3 = patternConstraints3.get(0);
 
-        assertEquals("^dp[0-9]+o[0-9]+(d[0-9]+)?$", patternConstraint0.getJavaPatternString());
-        assertEquals("^dp[0-9]+s[0-9]+(f[0-9]+)?(d[0-9]+)?$", patternConstraint1.getJavaPatternString());
-        assertEquals("^dp[0-9]+(P[0-9]+)?p[0-9]{1,3}s[0-9]{1,3}(f[0-9]+)?(d[0-9]+)?$",
+        assertEquals("^(?:dp[0-9]+o[0-9]+(d[0-9]+)?)$", patternConstraint0.getJavaPatternString());
+        assertEquals("^(?:dp[0-9]+s[0-9]+(f[0-9]+)?(d[0-9]+)?)$", patternConstraint1.getJavaPatternString());
+        assertEquals("^(?:dp[0-9]+(P[0-9]+)?p[0-9]{1,3}s[0-9]{1,3}(f[0-9]+)?(d[0-9]+)?)$",
                 patternConstraint2.getJavaPatternString());
-        assertEquals("^dp[0-9]+p[0-9]+p[0-9]+$", patternConstraint3.getJavaPatternString());
+        assertEquals("^(?:dp[0-9]+p[0-9]+p[0-9]+)$", patternConstraint3.getJavaPatternString());
     }
 }
index 2984100caf425fe99616baf411f1006504594385..60f5f3b8997282dd2bf2872c5215d8848f660f2d 100644 (file)
@@ -81,7 +81,7 @@ public class Bug6180Test {
         final List<PatternConstraint> patternConstraints = ((StringTypeDefinition) type).getPatternConstraints();
         assertEquals(1, patternConstraints.size());
         final PatternConstraint pattern = patternConstraints.iterator().next();
-        assertEquals("^\".*\"$", pattern.getJavaPatternString());
+        assertEquals("^(?:\".*\")$", pattern.getJavaPatternString());
         assertTrue(Pattern.compile(pattern.getJavaPatternString()).matcher("\"enclosed string in quotes\"").matches());
     }
 
@@ -99,7 +99,7 @@ public class Bug6180Test {
         final List<PatternConstraint> patternConstraints = ((StringTypeDefinition) type).getPatternConstraints();
         assertEquals(1, patternConstraints.size());
         final PatternConstraint pattern = patternConstraints.iterator().next();
-        assertEquals("^'.*'$", pattern.getJavaPatternString());
+        assertEquals("^(?:'.*')$", pattern.getJavaPatternString());
         assertTrue(Pattern.compile(pattern.getJavaPatternString()).matcher("'enclosed string in quotes'").matches());
     }
 }
\ No newline at end of file
index 0cdbc4b0e295d6253552efc2085f220d0efc8133..33211f566d81a49915e8cdb50d4a0d1455e00994 100644 (file)
@@ -434,10 +434,10 @@ public class EffectiveStatementTypeTest {
         final PatternConstraint patternConstraintSecond = ((StringTypeDefinition) currentLeaf.getType())
                 .getPatternConstraints().get(0);
 
-        assertEquals("^[0-9a-fA-F]*$", patternConstraint.getJavaPatternString());
+        assertEquals("^(?:[0-9a-fA-F]*)$", patternConstraint.getJavaPatternString());
         assertFalse(patternConstraint.getReference().isPresent());
         assertFalse(patternConstraint.getDescription().isPresent());
-        assertEquals(Optional.of("Supplied value does not match the regular expression ^[0-9a-fA-F]*$."),
+        assertEquals(Optional.of("Supplied value does not match the regular expression ^(?:[0-9a-fA-F]*)$."),
             patternConstraint.getErrorMessage());
         assertEquals(Optional.of("invalid-regular-expression"), patternConstraint.getErrorAppTag());
         assertNotNull(patternConstraint.toString());
index 5319b9dae0afaa0a04af2447f334fc346d0cd4a5..ccf51f155727fd808411da2365cdce3dcba25bbf 100644 (file)
@@ -133,20 +133,20 @@ public class TypesResolutionTest {
 
         StringTypeDefinition ipv4 = (StringTypeDefinition) unionTypes.get(0);
         assertNotNull(ipv4.getBaseType());
-        String expectedPattern = "^(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\\.){3}"
-                + "([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])" + "(%[\\p{N}\\p{L}]+)?$";
+        String expectedPattern = "^(?:(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\\.){3}"
+                + "([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])" + "(%[\\p{N}\\p{L}]+)?)$";
         assertEquals(expectedPattern, ipv4.getPatternConstraints().get(0).getJavaPatternString());
 
         StringTypeDefinition ipv6 = (StringTypeDefinition) unionTypes.get(1);
         assertNotNull(ipv6.getBaseType());
         List<PatternConstraint> ipv6Patterns = ipv6.getPatternConstraints();
-        expectedPattern = "^((:|[0-9a-fA-F]{0,4}):)([0-9a-fA-F]{0,4}:){0,5}"
+        expectedPattern = "^(?:((:|[0-9a-fA-F]{0,4}):)([0-9a-fA-F]{0,4}:){0,5}"
                 + "((([0-9a-fA-F]{0,4}:)?(:|[0-9a-fA-F]{0,4}))|" + "(((25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])\\.){3}"
-                + "(25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])))" + "(%[\\p{N}\\p{L}]+)?$";
+                + "(25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])))" + "(%[\\p{N}\\p{L}]+)?)$";
         assertEquals(expectedPattern, ipv6Patterns.get(0).getJavaPatternString());
 
-        expectedPattern = "^(([^:]+:){6}(([^:]+:[^:]+)|(.*\\..*)))|" + "((([^:]+:)*[^:]+)?::(([^:]+:)*[^:]+)?)"
-                + "(%.+)?$";
+        expectedPattern = "^(?:(([^:]+:){6}(([^:]+:[^:]+)|(.*\\..*)))|" + "((([^:]+:)*[^:]+)?::(([^:]+:)*[^:]+)?)"
+                + "(%.+)?)$";
         assertEquals(expectedPattern, ipv6Patterns.get(1).getJavaPatternString());
     }
 
@@ -158,8 +158,8 @@ public class TypesResolutionTest {
         assertNotNull(type.getBaseType());
         List<PatternConstraint> patterns = type.getPatternConstraints();
         assertEquals(1, patterns.size());
-        String expectedPattern = "^((([a-zA-Z0-9_]([a-zA-Z0-9\\-_]){0,61})?[a-zA-Z0-9]\\.)*"
-                + "([a-zA-Z0-9_]([a-zA-Z0-9\\-_]){0,61})?[a-zA-Z0-9]\\.?)" + "|\\.$";
+        String expectedPattern = "^(?:((([a-zA-Z0-9_]([a-zA-Z0-9\\-_]){0,61})?[a-zA-Z0-9]\\.)*"
+                + "([a-zA-Z0-9_]([a-zA-Z0-9\\-_]){0,61})?[a-zA-Z0-9]\\.?)" + "|\\.)$";
         assertEquals(expectedPattern, patterns.get(0).getJavaPatternString());
 
         LengthConstraint lengths = type.getLengthConstraint().get();
@@ -314,7 +314,7 @@ public class TypesResolutionTest {
         List<PatternConstraint> patterns = testedType.getPatternConstraints();
         assertEquals(1, patterns.size());
         PatternConstraint pattern = patterns.get(0);
-        assertEquals("^\\d*(\\.\\d*){1,127}$", pattern.getJavaPatternString());
+        assertEquals("^(?:\\d*(\\.\\d*){1,127})$", pattern.getJavaPatternString());
 
         QName testedTypeQName = testedType.getQName();
         assertEquals(URI.create("urn:ietf:params:xml:ns:yang:ietf-yang-types"), testedTypeQName.getNamespace());
@@ -326,7 +326,7 @@ public class TypesResolutionTest {
         assertEquals(1, patterns.size());
 
         pattern = patterns.get(0);
-        assertEquals("^(([0-1](\\.[1-3]?[0-9]))|(2\\.(0|([1-9]\\d*))))(\\.(0|([1-9]\\d*)))*$",
+        assertEquals("^(?:(([0-1](\\.[1-3]?[0-9]))|(2\\.(0|([1-9]\\d*))))(\\.(0|([1-9]\\d*)))*)$",
                 pattern.getJavaPatternString());
 
         QName testedTypeBaseQName = testedTypeBase.getQName();
index 0621280a83394a4eab4516343f0e89da2cd9e69e..6c16627b1ef8b369af45c5dac10285a1ad7d4cf6 100644 (file)
@@ -233,7 +233,7 @@ public class YangParserTest {
         List<PatternConstraint> patterns = type.getPatternConstraints();
         assertEquals(1, patterns.size());
         PatternConstraint pattern = patterns.iterator().next();
-        assertEquals("^[e-z]*$", pattern.getJavaPatternString());
+        assertEquals("^(?:[e-z]*)$", pattern.getJavaPatternString());
         assertEquals(1, type.getLengthConstraint().get().getAllowedRanges().asRanges().size());
 
         final StringTypeDefinition baseType1 = type.getBaseType();
@@ -245,7 +245,7 @@ public class YangParserTest {
         patterns = baseType1.getPatternConstraints();
         assertEquals(1, patterns.size());
         pattern = patterns.iterator().next();
-        assertEquals("^[b-u]*$", pattern.getJavaPatternString());
+        assertEquals("^(?:[b-u]*)$", pattern.getJavaPatternString());
         assertEquals(1, baseType1.getLengthConstraint().get().getAllowedRanges().asRanges().size());
 
         final StringTypeDefinition baseType2 = baseType1.getBaseType();
@@ -268,7 +268,7 @@ public class YangParserTest {
         patterns = baseType3.getPatternConstraints();
         assertEquals(1, patterns.size());
         pattern = patterns.iterator().next();
-        assertEquals("^[a-k]*$", pattern.getJavaPatternString());
+        assertEquals("^(?:[a-k]*)$", pattern.getJavaPatternString());
         final RangeSet<Integer> baseType3Lengths = baseType3.getLengthConstraint().get().getAllowedRanges();
         assertEquals(1, baseType3Lengths.asRanges().size());
         length = baseType3Lengths.span();
@@ -309,7 +309,7 @@ public class YangParserTest {
         assertTrue(!patterns.isEmpty());
         assertEquals(1, patterns.size());
         final PatternConstraint pattern = patterns.iterator().next();
-        assertEquals("^[e-z]*$", pattern.getJavaPatternString());
+        assertEquals("^(?:[e-z]*)$", pattern.getJavaPatternString());
         assertEquals(1, type.getLengthConstraint().get().getAllowedRanges().asRanges().size());
 
         final LeafSchemaNode multiplePatternDirectStringDefLeaf = (LeafSchemaNode) foo
@@ -325,9 +325,9 @@ public class YangParserTest {
         boolean isEZPattern = false;
         boolean isADPattern = false;
         for (final PatternConstraint patternConstraint : patterns) {
-            if (patternConstraint.getJavaPatternString().equals("^[e-z]*$")) {
+            if (patternConstraint.getJavaPatternString().equals("^(?:[e-z]*)$")) {
                 isEZPattern = true;
-            } else if (patternConstraint.getJavaPatternString().equals("^[a-d]*$")) {
+            } else if (patternConstraint.getJavaPatternString().equals("^(?:[a-d]*)$")) {
                 isADPattern = true;
             }
         }
@@ -369,7 +369,7 @@ public class YangParserTest {
         final List<PatternConstraint> patterns = baseType2.getPatternConstraints();
         assertEquals(1, patterns.size());
         final PatternConstraint pattern = patterns.iterator().next();
-        assertEquals("^[a-k]*$", pattern.getJavaPatternString());
+        assertEquals("^(?:[a-k]*)$", pattern.getJavaPatternString());
         final RangeSet<Integer> baseType3Lengths = baseType2.getLengthConstraint().get().getAllowedRanges();
         assertEquals(1, baseType3Lengths.asRanges().size());
         length = baseType3Lengths.span();