private int sub(LuaCallFrame callFrame, int nArguments) {
        String s = getStringArg(callFrame, 1, names[SUB]);
        double start = getDoubleArg(callFrame, 2, names[SUB]).doubleValue();
        double end = -1;
        if (nArguments >= 3) {
            end = getDoubleArg(callFrame, 3, names[SUB]).doubleValue();
        }
        String res;
        int istart = (int) start;
        int iend = (int) end;

        int len = s.length();
        if (istart < 0) {
            istart = Math.max(len + istart + 1, 1);
        } else if (istart == 0) {
            istart = 1;
        }


        if (iend < 0) {
            iend = Math.max(0, iend + len + 1);
        } else if (iend > len) {
            iend = len;
        }

        if (istart > iend) {
            return callFrame.push("");
        }
        res = s.substring(istart - 1, iend);

        return callFrame.push(res);
    }
Only works with non-negative numbers
main/src/main/java/cgeo/geocaching/wherigo/kahlua/stdlib/StringLib.java