src/main/java/com/basistech/tclre/Dfa.java
int longest(int start, int stop, boolean[] hitstop) {
        int cp;
        int realstop = (stop == runtime.dataLength) ? stop : stop + 1;
        short co;
        StateSet css;
        int post;

    /* initialize */
        css = initialize(start);
        cp = start;
        if (hitstop != null) {
            hitstop[0] = false;
        }

    /* startup */
        if (cp == 0) {
            co = cnfa.bos[0 != (runtime.eflags & Flags.REG_NOTBOL) ? 0 : 1];
        } else {
            char theChar = runtime.data.charAt(cp - 1);
            if (Character.isLowSurrogate(theChar)) {
                // collect the other end of the surrogate.
                theChar = runtime.data.charAt(cp - 2);
                char high = theChar;
                int codepoint = Character.toCodePoint(high, theChar);
                co = cm.getcolor(codepoint); // and get a color for the pair.
            } else {
                co = cm.getcolor(theChar);
            }
        }
        css = miss(css, co, cp);
        if (css == null) {
            return -1;
        }

        css.setLastSeen(cp);

        StateSet ss;
    /* main loop */
        while (cp < realstop) {
            char theChar = runtime.data.charAt(cp);
            int increment = 1;
            if (Character.isHighSurrogate(theChar)) {
                int codepoint = Character.toCodePoint(theChar, runtime.data.charAt(cp + 1));
                co = cm.getcolor(codepoint);
                increment = 2;
            } else {
                co = cm.getcolor(theChar);
            }
            ss = css.outs[co];
            if (ss == null) {
                ss = miss(css, co, cp + increment);
                if (ss == null) {
                    break;  /* NOTE BREAK OUT */
                }
            }

            cp = cp + increment;
            ss.setLastSeen(cp);
            css = ss;
        }

    /* shutdown */
        if (cp == runtime.dataLength && stop == runtime.dataLength) {
            if (hitstop != null) {
                hitstop[0] = true;
            }
            co = cnfa.eos[0 != (runtime.eflags & Flags.REG_NOTEOL) ? 0 : 1];
            ss = miss(css, co, cp);
        /* special case:  match ended at eol? */
            if (ss != null && ss.poststate) {
                return cp;
            } else if (ss != null) {
                ss.setLastSeen(cp); /* to be tidy */
            }
        }

    /* find last match, if any */
        post = -1;
        for (StateSet thisSS : stateSets.values()) { //.object2ObjectEntrySet()) {
            if (thisSS.poststate && post != thisSS.getLastSeen()
                    && (post == -1 || post < thisSS.getLastSeen())) {
                post = thisSS.getLastSeen();
            }
        }
        if (post != -1) {       /* found one */
            /* Post points after the codepoint after the last one in the match (!) */
            /* So, if that is an SMP codepoint, we need to back up 2 to get to the beginning of it,
             * and thus be just after the last character of the match. */
            char postChar = runtime.data.charAt(post - 1);
            if (Character.isLowSurrogate(postChar)) {
                return post - 2;
            } else {
                return post - 1;
            }
        }
        return -1;
    }
int longest(int start, int stop, boolean[] hitstop) {
        int cp;
        int realstop = (stop == runtime.dataLength) ? stop : stop + 1;
        short co;
        StateSet css;
        int post;

    /* initialize */
        css = initialize(start);
        cp = start;
        if (hitstop != null) {
            hitstop[0] = false;
        }

    /* startup */
        if (cp == 0) {
            co = cnfa.bos[0 != (runtime.eflags & Flags.REG_NOTBOL) ? 0 : 1];
        } else {
            char theChar = runtime.data.charAt(cp - 1);
            if (Character.isLowSurrogate(theChar)) {
                // collect the other end of the surrogate.
                theChar = runtime.data.charAt(cp - 2);
                char high = theChar;
                int codepoint = Character.toCodePoint(high, theChar);
                co = cm.getcolor(codepoint); // and get a color for the pair.
            } else {
                co = cm.getcolor(theChar);
            }
        }
        css = miss(css, co, cp);
        if (css == null) {
            return -1;
        }

        css.setLastSeen(cp);

        StateSet ss;
    /* main loop */
        while (cp < realstop) {
            char theChar = runtime.data.charAt(cp);
            int increment = 1;
            if (Character.isHighSurrogate(theChar)) {
                int codepoint = Character.toCodePoint(theChar, runtime.data.charAt(cp + 1));
                co = cm.getcolor(codepoint);
                increment = 2;
            } else {
                co = cm.getcolor(theChar);
            }
            ss = css.outs[co];
            if (ss == null) {
                ss = miss(css, co, cp + increment);
                if (ss == null) {
                    break;  /* NOTE BREAK OUT */
                }
            }

            cp = cp + increment;
            ss.setLastSeen(cp);
            css = ss;
        }

    /* shutdown */
        if (cp == runtime.dataLength && stop == runtime.dataLength) {
            if (hitstop != null) {
                hitstop[0] = true;
            }
            co = cnfa.eos[0 != (runtime.eflags & Flags.REG_NOTEOL) ? 0 : 1];
            ss = miss(css, co, cp);
        /* special case:  match ended at eol? */
            if (ss != null && ss.poststate) {
                return cp;
            } else if (ss != null) {
                ss.setLastSeen(cp); /* to be tidy */
            }
        }

    /* find last match, if any */
        post = -1;
        for (StateSet thisSS : stateSets.values()) { //.object2ObjectEntrySet()) {
            if (thisSS.poststate && post != thisSS.getLastSeen()
                    && (post == -1 || post < thisSS.getLastSeen())) {
                post = thisSS.getLastSeen();
            }
        }
        if (post != -1) {       /* found one */
            /* Post points after the codepoint after the last one in the match (!) */
            /* So, if that is an SMP codepoint, we need to back up 2 to get to the beginning of it,
             * and thus be just after the last character of the match. */
            char postChar = runtime.data.charAt(post - 1);
            if (Character.isLowSurrogate(postChar)) {
                return post - 2;
            } else {
                return post - 1;
            }
        }
        return -1;
    }
longest - longest-preferred matching engine

@return endpoint or -1