Browse Source

优化parser

zhouhao 6 years ago
parent
commit
9a75ff7be4

+ 22 - 22
hsweb-commons/hsweb-commons-entity/src/main/java/org/hswebframework/web/commons/entity/param/TermExpressionParser.java

@@ -20,7 +20,7 @@ public class TermExpressionParser {
         NestConditional<?> nest = null;
 
         char[] buf = new char[128];
-        byte current = 0;
+        byte len = 0;
         byte spaceLen = 0;
 
         char[] currentColumn = null;
@@ -35,54 +35,54 @@ public class TermExpressionParser {
                 nest = (nest == null ?
                         (currentType.equals("or") ? conditional.orNest() : conditional.nest()) :
                         (currentType.equals("or") ? nest.orNest() : nest.nest()));
-                current = 0;
-
+                len = 0;
                 continue;
             } else if (c == ')') {
                 if (nest == null) {
                     continue;
                 }
                 if (null != currentColumn) {
-                    currentValue = Arrays.copyOf(buf, current);
+                    currentValue = Arrays.copyOf(buf, len);
                     nest.accept(new String(currentColumn), convertTermType(currentTermType), new String(currentValue));
                     currentColumn = null;
                     currentTermType = null;
                 }
                 Object end = nest.end();
                 nest = end instanceof NestConditional ? ((NestConditional) end) : null;
-                current = 0;
+                len = 0;
                 spaceLen++;
                 continue;
             } else if (c == '=' || c == '>' || c == '<') {
                 if (currentTermType != null) {
                     currentTermType += String.valueOf(c);
+                    //spaceLen--;
                 } else {
                     currentTermType = String.valueOf(c);
                 }
 
                 if (currentColumn == null) {
-                    currentColumn = Arrays.copyOf(buf, current);
+                    currentColumn = Arrays.copyOf(buf, len);
                 }
                 spaceLen++;
-                current = 0;
+                len = 0;
                 continue;
             } else if (c == ' ') {
-                if (current == 0) {
+                if (len == 0) {
                     continue;
                 }
                 spaceLen++;
                 if (currentColumn == null && (spaceLen == 1 || spaceLen % 5 == 0)) {
-                    currentColumn = Arrays.copyOf(buf, current);
-                    current = 0;
+                    currentColumn = Arrays.copyOf(buf, len);
+                    len = 0;
                     continue;
                 }
                 if (null != currentColumn) {
                     if (null == currentTermType) {
-                        currentTermType = new String(Arrays.copyOf(buf, current));
-                        current = 0;
+                        currentTermType = new String(Arrays.copyOf(buf, len));
+                        len = 0;
                         continue;
                     }
-                    currentValue = Arrays.copyOf(buf, current);
+                    currentValue = Arrays.copyOf(buf, len);
                     if (nest != null) {
                         nest.accept(new String(currentColumn), convertTermType(currentTermType), new String(currentValue));
                     } else {
@@ -90,10 +90,10 @@ public class TermExpressionParser {
                     }
                     currentColumn = null;
                     currentTermType = null;
-                    current = 0;
+                    len = 0;
                     continue;
-                } else if (current == 2 || current == 3) {
-                    String type = new String(Arrays.copyOf(buf, current));
+                } else if (len == 2 || len == 3) {
+                    String type = new String(Arrays.copyOf(buf, len));
                     if (type.equalsIgnoreCase("or")) {
                         currentType = "or";
                         if (nest != null) {
@@ -101,7 +101,7 @@ public class TermExpressionParser {
                         } else {
                             conditional.or();
                         }
-                        current = 0;
+                        len = 0;
                         continue;
                     } else if (type.equalsIgnoreCase("and")) {
                         currentType = "and";
@@ -110,21 +110,21 @@ public class TermExpressionParser {
                         } else {
                             conditional.and();
                         }
-                        current = 0;
+                        len = 0;
                         continue;
                     } else {
-                        currentColumn = Arrays.copyOf(buf, current);
-                        current = 0;
+                        currentColumn = Arrays.copyOf(buf, len);
+                        len = 0;
                         spaceLen++;
                     }
                 }
                 continue;
             }
 
-            buf[current++] = c;
+            buf[len++] = c;
         }
         if (null != currentColumn) {
-            currentValue = Arrays.copyOf(buf, current);
+            currentValue = Arrays.copyOf(buf, len);
             if (nest != null) {
                 nest.accept(new String(currentColumn), convertTermType(currentTermType), new String(currentValue));
             } else {

+ 1 - 1
hsweb-commons/hsweb-commons-entity/src/test/java/org/hswebframework/web/commons/entity/param/TermExpressionParserTest.java

@@ -29,7 +29,7 @@ public class TermExpressionParserTest {
 
     @Test
     public void testNest() {
-        String expression = "name = 测试 and (age > 10 or age <= 20) and test like test2 and (age gt age2 or age btw age3,age4)";
+        String expression = "name = 测试 and (age > 10 or age <= 20) and test like test2 and (age gt age2 or age btw age3,age4 or (age > 10 or age <= 20))";
         System.out.println(expression);
         List<Term> terms = TermExpressionParser.parse(expression);
         System.out.println(JSON.toJSONString(terms, SerializerFeature.PrettyFormat));