// 
// Decompiled by Procyon v0.6.0
// 

package ch.randelshofer.fastdoubleparser.chr;

import java.util.Iterator;
import java.util.Set;

final class CharTrieOfFewIgnoreCase implements CharTrie
{
    private CharTrieNode root;
    
    public CharTrieOfFewIgnoreCase(final Set<String> set) {
        this.root = new CharTrieNode();
        for (final String str : set) {
            if (!str.isEmpty()) {
                this.add(str);
            }
        }
    }
    
    private void add(final String str) {
        CharTrieNode upperNode = this.root;
        CharTrieNode lowerNode = this.root;
        final String upperStr = str.toUpperCase();
        final String lowerStr = upperStr.toLowerCase();
        for (int i = 0; i < str.length(); ++i) {
            final char upper = upperStr.charAt(i);
            final char lower = lowerStr.charAt(i);
            upperNode = upperNode.insert(upper);
            lowerNode = lowerNode.insert(lower, upperNode);
        }
        upperNode.setEnd();
    }
    
    @Override
    public int match(final char[] str, final int startIndex, final int endIndex) {
        CharTrieNode node = this.root;
        int longestMatch = startIndex;
        for (int i = startIndex; i < endIndex; ++i) {
            node = node.get(str[i]);
            if (node == null) {
                break;
            }
            longestMatch = (node.isEnd() ? (i + 1) : longestMatch);
        }
        return longestMatch - startIndex;
    }
    
    @Override
    public int match(final CharSequence str, final int startIndex, final int endIndex) {
        CharTrieNode node = this.root;
        int longestMatch = startIndex;
        for (int i = startIndex; i < endIndex; ++i) {
            node = node.get(str.charAt(i));
            if (node == null) {
                break;
            }
            longestMatch = (node.isEnd() ? (i + 1) : longestMatch);
        }
        return longestMatch - startIndex;
    }
}
