jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From thom...@apache.org
Subject svn commit: r1165320 - in /jackrabbit/sandbox/microkernel/src: main/java/org/apache/jackrabbit/mk/json/JsopBuilder.java main/java/org/apache/jackrabbit/mk/json/JsopTokenizer.java test/java/org/apache/jackrabbit/mk/json/JsopTest.java
Date Mon, 05 Sep 2011 15:12:52 GMT
Author: thomasm
Date: Mon Sep  5 15:12:52 2011
New Revision: 1165320

URL: http://svn.apache.org/viewvc?rev=1165320&view=rev
Log:
Slightly improved Jsop tokenizer and builder

Modified:
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/json/JsopBuilder.java
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/json/JsopTokenizer.java
    jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/json/JsopTest.java

Modified: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/json/JsopBuilder.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/json/JsopBuilder.java?rev=1165320&r1=1165319&r2=1165320&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/json/JsopBuilder.java
(original)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/json/JsopBuilder.java
Mon Sep  5 15:12:52 2011
@@ -216,10 +216,17 @@ public class JsopBuilder {
         if (length == 0) {
             return "\"\"";
         }
+        for (int i = 0; i < length; i++) {
+            char c = s.charAt(i);
+            if (c == '\"' || c == '\\' || c < ' ' || c >= 127) {
+                StringBuilder buff = new StringBuilder(length + 2 + length / 8);
+                buff.append('\"');
+                escape(s, length, buff);
+                return buff.append('\"').toString();
+            }
+        }
         StringBuilder buff = new StringBuilder(length + 2);
-        buff.append('\"');
-        escape(s, length, buff);
-        return buff.append('\"').toString();
+        return buff.append('\"').append(s).append('\"').toString();
     }
 
     /**
@@ -233,55 +240,54 @@ public class JsopBuilder {
         for (int i = 0; i < length; i++) {
             char c = s.charAt(i);
             switch (c) {
-                case '"':
-                    // quotation mark
-                    buff.append("\\\"");
-                    break;
-                case '\\':
-                    // backslash
-                    buff.append("\\\\");
-                    break;
-                case '\b':
-                    // backspace
-                    buff.append("\\b");
-                    break;
-                case '\f':
-                    // formfeed
-                    buff.append("\\f");
-                    break;
-                case '\n':
-                    // newline
-                    buff.append("\\n");
-                    break;
-                case '\r':
-                    // carriage return
-                    buff.append("\\r");
-                    break;
-                case '\t':
-                    // horizontal tab
-                    buff.append("\\t");
-                    break;
-                default:
-                    int ch = c;
-                    if (ch < ' ') {
-                        // guaranteed to be 1 or 2 hex digits only
-                        buff.append("\\u00");
-                        String hex = Integer.toHexString(c);
-                        if (hex.length() == 1) {
-                            buff.append('0');
-                        }
-                        buff.append(hex);
-                    } else if (ch >= 127) {
-                        // ascii only mode
-                        buff.append("\\u");
-                        String hex = Integer.toHexString(c);
-                        for (int len = hex.length(); len < 4; len++) {
-                            buff.append('0');
-                        }
-                        buff.append(hex);
-                    } else {
-                        buff.append(c);
+            case '"':
+                // quotation mark
+                buff.append("\\\"");
+                break;
+            case '\\':
+                // backslash
+                buff.append("\\\\");
+                break;
+            case '\b':
+                // backspace
+                buff.append("\\b");
+                break;
+            case '\f':
+                // formfeed
+                buff.append("\\f");
+                break;
+            case '\n':
+                // newline
+                buff.append("\\n");
+                break;
+            case '\r':
+                // carriage return
+                buff.append("\\r");
+                break;
+            case '\t':
+                // horizontal tab
+                buff.append("\\t");
+                break;
+            default:
+                if (c < ' ') {
+                    // guaranteed to be 1 or 2 hex digits only
+                    buff.append("\\u00");
+                    String hex = Integer.toHexString(c);
+                    if (hex.length() == 1) {
+                        buff.append('0');
+                    }
+                    buff.append(hex);
+                } else if (c >= 127) {
+                    // ascii only mode
+                    buff.append("\\u");
+                    String hex = Integer.toHexString(c);
+                    for (int len = hex.length(); len < 4; len++) {
+                        buff.append('0');
                     }
+                    buff.append(hex);
+                } else {
+                    buff.append(c);
+                }
             }
         }
     }

Modified: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/json/JsopTokenizer.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/json/JsopTokenizer.java?rev=1165320&r1=1165319&r2=1165320&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/json/JsopTokenizer.java
(original)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/json/JsopTokenizer.java
Mon Sep  5 15:12:52 2011
@@ -238,7 +238,7 @@ public class JsopTokenizer {
                 } else if (ch >= 'a' && ch <= 'z') {
                     while (pos < length) {
                         ch = jsop.charAt(pos);
-                        if ((ch < 'a' || ch > 'z') && (ch < '0' || ch >
'9')) {
+                        if ((ch < 'a' || ch > 'z') && ch != '_' &&
(ch < '0' || ch > '9')) {
                             break;
                         }
                         pos++;
@@ -372,6 +372,9 @@ public class JsopTokenizer {
      */
     public String readRawValue() {
         int start = lastPos;
+        while (jsop.charAt(start) <= ' ') {
+            start++;
+        }
         skipRawValue();
         return jsop.substring(start, lastPos);
     }

Modified: jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/json/JsopTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/json/JsopTest.java?rev=1165320&r1=1165319&r2=1165320&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/json/JsopTest.java
(original)
+++ jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/json/JsopTest.java
Mon Sep  5 15:12:52 2011
@@ -25,6 +25,22 @@ import java.io.IOException;
  */
 public class JsopTest extends TestCase {
 
+    // run the micro-benchmark
+    public static void main(String... args) {
+        for (int k = 0; k < 5; k++) {
+            // String s = "Hello World Hello World Hello World Hello World Hello World Hello
World ";
+            String s = "Hello \"World\" Hello \"World\" Hello \"World\" Hello \"World\" Hello
\"World\" Hello \"World\" ";
+            long t = System.currentTimeMillis();
+            int t2 = 0;
+            for (int i = 0; i < 10000000; i++) {
+                t2 += JsopBuilder.encode(s).length();
+            }
+            System.out.println(System.currentTimeMillis() - t);
+        }
+        // old: not escaped: 5691 ms; escaped: 10609 ms
+        // new: not escaped: 3931 ms; escaped: 11001 ms
+    }
+
     public void testDataType() {
         String dateString = new JsopBuilder().
                 key("string").value("/Date(0)/").
@@ -84,13 +100,13 @@ public class JsopTest extends TestCase {
         t.read('{');
         assertEquals("x", t.readString());
         t.read(':');
-        assertEquals(" [1]", t.readRawValue());
+        assertEquals("[1]", t.readRawValue());
         t.read(',');
-        assertEquals(" null", t.readRawValue());
+        assertEquals("null", t.readRawValue());
         t.read(',');
-        assertEquals(" true", t.readRawValue());
+        assertEquals("true", t.readRawValue());
         t.read(',');
-        assertEquals(" {\"y\": 1}", t.readRawValue());
+        assertEquals("{\"y\": 1}", t.readRawValue());
         try {
             t.readRawValue();
             fail();



Mime
View raw message