commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From brit...@apache.org
Subject svn commit: r1668348 - in /commons/proper/lang/trunk/src: changes/changes.xml main/java/org/apache/commons/lang3/text/translate/LookupTranslator.java
Date Sun, 22 Mar 2015 09:52:26 GMT
Author: britter
Date: Sun Mar 22 09:52:25 2015
New Revision: 1668348

URL: http://svn.apache.org/r1668348
Log:
LANG-935: Possible performance improvement on string escape functions. Thanks to Fabian Lange
and Thomas Neidhart.

Modified:
    commons/proper/lang/trunk/src/changes/changes.xml
    commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/text/translate/LookupTranslator.java

Modified: commons/proper/lang/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/changes/changes.xml?rev=1668348&r1=1668347&r2=1668348&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/changes/changes.xml [utf-8] (original)
+++ commons/proper/lang/trunk/src/changes/changes.xml [utf-8] Sun Mar 22 09:52:25 2015
@@ -22,6 +22,7 @@
   <body>
 
   <release version="3.4" date="tba" description="tba">
+    <action issue="LANG-935" type="update" dev="britter" due-to="Fabian Lange, Thomas
Neidhart">Possible performance improvement on string escape functions</action>
     <action issue="LANG-948" type="fix" dev="britter" due-to="Andrey Khobnya">Exception
while using ExtendedMessageFormat and escaping braces</action>
     <action issue="LANG-1098" type="update" dev="britter" due-to="Mikhail Mazurskiy, Fabian
Lange">Avoid String allocation in StrBuilder.append(CharSequence)</action>
     <action issue="LANG-1098" type="update" dev="britter" due-to="MichaƂ Kordas">Update
maven-checkstyle-plugin to 2.14</action>

Modified: commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/text/translate/LookupTranslator.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/text/translate/LookupTranslator.java?rev=1668348&r1=1668347&r2=1668348&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/text/translate/LookupTranslator.java
(original)
+++ commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/text/translate/LookupTranslator.java
Sun Mar 22 09:52:25 2015
@@ -19,6 +19,7 @@ package org.apache.commons.lang3.text.tr
 import java.io.IOException;
 import java.io.Writer;
 import java.util.HashMap;
+import java.util.HashSet;
 
 /**
  * Translates a value using a lookup table.
@@ -29,6 +30,7 @@ import java.util.HashMap;
 public class LookupTranslator extends CharSequenceTranslator {
 
     private final HashMap<String, CharSequence> lookupMap;
+    private final HashSet<Character> prefixSet;
     private final int shortest;
     private final int longest;
 
@@ -44,11 +46,13 @@ public class LookupTranslator extends Ch
      */
     public LookupTranslator(final CharSequence[]... lookup) {
         lookupMap = new HashMap<String, CharSequence>();
+        prefixSet = new HashSet<Character>();
         int _shortest = Integer.MAX_VALUE;
         int _longest = 0;
         if (lookup != null) {
             for (final CharSequence[] seq : lookup) {
                 this.lookupMap.put(seq[0].toString(), seq[1]);
+                this.prefixSet.add(seq[0].charAt(0));
                 final int sz = seq[0].length();
                 if (sz < _shortest) {
                     _shortest = sz;
@@ -71,10 +75,17 @@ public class LookupTranslator extends Ch
         if (index + longest > input.length()) {
             max = input.length() - index;
         }
+
+        if (!prefixSet.contains(input.charAt(index))) {
+            // no translation exists for the input at position index
+            return 0;
+        }
+
         // descend so as to get a greedy algorithm
         for (int i = max; i >= shortest; i--) {
             final CharSequence subSeq = input.subSequence(index, index + i);
             final CharSequence result = lookupMap.get(subSeq.toString());
+
             if (result != null) {
                 out.write(result.toString());
                 return i;



Mime
View raw message