private static int captureToClose ( MatchState ms ) {
        int level = ms.level;
        for ( level --; level >= 0; level -- ) {
            if ( ms.capture[level].len == CAP_UNFINISHED ) {
                return level;
            }
        }
        throw new IllegalStateException("invalid pattern capture");
    }
Only works with non-negative numbers
main/src/main/java/cgeo/geocaching/wherigo/kahlua/stdlib/StringLib.java