private static StringPointer classEnd ( StringPointer pp ) {
        StringPointer p = pp.getClone();
        switch ( p.postIncrString ( 1 ) ) {
        case L_ESC: {
            BaseLib.luaAssert(p.getChar () != '\0', "malformed pattern (ends with '%')");
            p.postIncrString ( 1 );
            return p;
        }
        case '[': {
            if ( p.getChar () == '^' ) {
                p.postIncrString ( 1 );
            }
            do { // look for a `]'
                BaseLib.luaAssert(p.getChar () != '\0', "malformed pattern (missing ']')");

                if ( p.postIncrString ( 1 ) == L_ESC && p.getChar () != '\0' ) {
                    p.postIncrString ( 1 );  // skip escapes (e.g. `%]')
                }

            } while ( p.getChar () != ']' );

            p.postIncrString ( 1 );
            return p;
        }
        default: {
            return p;
        }
        }
    }
Only works with non-negative numbers
main/src/main/java/cgeo/geocaching/wherigo/kahlua/stdlib/StringLib.java