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

package ch.randelshofer.fastdoubleparser.bte;

import java.nio.charset.StandardCharsets;
import java.util.Iterator;
import java.util.Set;

final class ByteTrieOfFew implements ByteTrie
{
    private ByteTrieNode root;
    
    public ByteTrieOfFew(final Set<String> set) {
        this.root = new ByteTrieNode();
        for (final String str : set) {
            if (!str.isEmpty()) {
                this.add(str);
            }
        }
    }
    
    private void add(final String str) {
        ByteTrieNode node = this.root;
        final byte[] strBytes = str.getBytes(StandardCharsets.UTF_8);
        for (int i = 0; i < strBytes.length; ++i) {
            node = node.insert(strBytes[i]);
        }
        node.setEnd();
    }
    
    @Override
    public int match(final byte[] str, final int startIndex, final int endIndex) {
        ByteTrieNode 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;
    }
}
