LANG-935: Possible performance improvement on string escape functions. Thanks to Fabian Lange and Thomas Neidhart.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@1668348 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
bf6ee5c56e
commit
7bb99bcb6b
|
@ -22,6 +22,7 @@
|
||||||
<body>
|
<body>
|
||||||
|
|
||||||
<release version="3.4" date="tba" description="tba">
|
<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-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="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>
|
<action issue="LANG-1098" type="update" dev="britter" due-to="Michał Kordas">Update maven-checkstyle-plugin to 2.14</action>
|
||||||
|
|
|
@ -19,6 +19,7 @@ package org.apache.commons.lang3.text.translate;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.Writer;
|
import java.io.Writer;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.HashSet;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Translates a value using a lookup table.
|
* Translates a value using a lookup table.
|
||||||
|
@ -29,6 +30,7 @@ import java.util.HashMap;
|
||||||
public class LookupTranslator extends CharSequenceTranslator {
|
public class LookupTranslator extends CharSequenceTranslator {
|
||||||
|
|
||||||
private final HashMap<String, CharSequence> lookupMap;
|
private final HashMap<String, CharSequence> lookupMap;
|
||||||
|
private final HashSet<Character> prefixSet;
|
||||||
private final int shortest;
|
private final int shortest;
|
||||||
private final int longest;
|
private final int longest;
|
||||||
|
|
||||||
|
@ -44,11 +46,13 @@ public class LookupTranslator extends CharSequenceTranslator {
|
||||||
*/
|
*/
|
||||||
public LookupTranslator(final CharSequence[]... lookup) {
|
public LookupTranslator(final CharSequence[]... lookup) {
|
||||||
lookupMap = new HashMap<String, CharSequence>();
|
lookupMap = new HashMap<String, CharSequence>();
|
||||||
|
prefixSet = new HashSet<Character>();
|
||||||
int _shortest = Integer.MAX_VALUE;
|
int _shortest = Integer.MAX_VALUE;
|
||||||
int _longest = 0;
|
int _longest = 0;
|
||||||
if (lookup != null) {
|
if (lookup != null) {
|
||||||
for (final CharSequence[] seq : lookup) {
|
for (final CharSequence[] seq : lookup) {
|
||||||
this.lookupMap.put(seq[0].toString(), seq[1]);
|
this.lookupMap.put(seq[0].toString(), seq[1]);
|
||||||
|
this.prefixSet.add(seq[0].charAt(0));
|
||||||
final int sz = seq[0].length();
|
final int sz = seq[0].length();
|
||||||
if (sz < _shortest) {
|
if (sz < _shortest) {
|
||||||
_shortest = sz;
|
_shortest = sz;
|
||||||
|
@ -71,10 +75,17 @@ public class LookupTranslator extends CharSequenceTranslator {
|
||||||
if (index + longest > input.length()) {
|
if (index + longest > input.length()) {
|
||||||
max = input.length() - index;
|
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
|
// descend so as to get a greedy algorithm
|
||||||
for (int i = max; i >= shortest; i--) {
|
for (int i = max; i >= shortest; i--) {
|
||||||
final CharSequence subSeq = input.subSequence(index, index + i);
|
final CharSequence subSeq = input.subSequence(index, index + i);
|
||||||
final CharSequence result = lookupMap.get(subSeq.toString());
|
final CharSequence result = lookupMap.get(subSeq.toString());
|
||||||
|
|
||||||
if (result != null) {
|
if (result != null) {
|
||||||
out.write(result.toString());
|
out.write(result.toString());
|
||||||
return i;
|
return i;
|
||||||
|
|
Loading…
Reference in New Issue