From eca04f401af2a2c21bfea285175ab267e5614e9c Mon Sep 17 00:00:00 2001 From: Greg Wilkins Date: Thu, 26 Mar 2015 10:41:41 +1100 Subject: [PATCH] TreeTrie getBest String implementation --- .../java/org/eclipse/jetty/util/TreeTrie.java | 40 +++++++++++++++++-- 1 file changed, 37 insertions(+), 3 deletions(-) diff --git a/jetty-util/src/main/java/org/eclipse/jetty/util/TreeTrie.java b/jetty-util/src/main/java/org/eclipse/jetty/util/TreeTrie.java index 9114f33ebc3..d6228940a14 100644 --- a/jetty-util/src/main/java/org/eclipse/jetty/util/TreeTrie.java +++ b/jetty-util/src/main/java/org/eclipse/jetty/util/TreeTrie.java @@ -224,9 +224,43 @@ public class TreeTrie extends AbstractTrie @Override public V getBest(String s, int offset, int len) { - // TODO inefficient - byte[] b=s.substring(offset,offset+len).getBytes(StandardCharsets.ISO_8859_1); - return getBest(b,0,b.length); + TreeTrie t = this; + for(int i=0; i < len; i++) + { + byte c=(byte)(0xff&s.charAt(offset+i)); + int index=c>=0&&c<0x7f?__lookup[c]:-1; + if (index>=0) + { + if (t._nextIndex[index] == null) + break; + t = t._nextIndex[index]; + } + else + { + TreeTrie n=null; + for (int j=t._nextOther.size();j-->0;) + { + n=t._nextOther.get(j); + if (n._c==c) + break; + n=null; + } + if (n==null) + break; + t=n; + } + + // Is the next Trie is a match + if (t._key!=null) + { + // Recurse so we can remember this possibility + V best=t.getBest(s,offset+i+1,len-i-1); + if (best!=null) + return best; + break; + } + } + return t._value; } @Override