Search in sources :

Example 1 with LuaString

use of org.luaj.vm2.LuaString in project LuaViewSDK by alibaba.

the class LuaPrint method buildState.

/**
 * Print the state of a {@link LuaClosure} that is being executed
 *
 * @param cl      the {@link LuaClosure}
 * @param pc      the program counter
 * @param stack   the stack of {@link LuaValue}
 * @param top     the top of the stack
 * @param varargs any {@link Varargs} value that may apply
 */
public LuaPrint buildState(LuaClosure cl, int pc, LuaValue[] stack, int top, Varargs varargs) {
    // print opcode into buffer
    StringBuffer previous = ps;
    ps = new StringBuffer();
    buildOpCode(cl.p, pc);
    LogUtil.i(ps);
    ps = previous;
    format(ps.toString(), 50);
    // print stack
    ps.append('[');
    for (int i = 0; i < stack.length; i++) {
        LuaValue v = stack[i];
        if (v == null)
            ps.append(STRING_FOR_NULL);
        else
            switch(v.type()) {
                case LuaValue.TSTRING:
                    LuaString s = v.checkstring();
                    ps.append(s.length() < 48 ? s.tojstring() : s.substring(0, 32).tojstring() + "...+" + (s.length() - 32) + "b");
                    break;
                case LuaValue.TFUNCTION:
                    ps.append(v.tojstring());
                    break;
                case LuaValue.TUSERDATA:
                    Object o = v.touserdata();
                    if (o != null) {
                        String n = o.getClass().getName();
                        n = n.substring(n.lastIndexOf('.') + 1);
                        ps.append(n + ": " + Integer.toHexString(o.hashCode()));
                    } else {
                        ps.append(v.toString());
                    }
                    break;
                default:
                    ps.append(v.tojstring());
            }
        if (i + 1 == top)
            ps.append(']');
        ps.append(" | ");
    }
    ps.append(varargs);
    ps.append("\n");
    return this;
}
Also used : LuaString(org.luaj.vm2.LuaString) LuaValue(org.luaj.vm2.LuaValue) LuaString(org.luaj.vm2.LuaString)

Example 2 with LuaString

use of org.luaj.vm2.LuaString in project LuaViewSDK by alibaba.

the class StringLib method sub.

/**
 * string.sub (s, i [, j])
 *
 * Returns the substring of s that starts at i and continues until j;
 * i and j may be negative. If j is absent, then it is assumed to be equal to -1
 * (which is the same as the string length). In particular, the call
 *    string.sub(s,1,j)
 * returns a prefix of s with length j, and
 *   string.sub(s, -i)
 * returns a suffix of s with length i.
 */
static Varargs sub(Varargs args) {
    final LuaString s = args.checkstring(1);
    final int l = s.length();
    int start = posrelat(args.checkint(2), l);
    int end = posrelat(args.optint(3, -1), l);
    if (start < 1)
        start = 1;
    if (end > l)
        end = l;
    if (start <= end) {
        return s.substring(start - 1, end);
    } else {
        return EMPTYSTRING;
    }
}
Also used : LuaString(org.luaj.vm2.LuaString)

Example 3 with LuaString

use of org.luaj.vm2.LuaString in project LuaViewSDK by alibaba.

the class StringLib method gmatch.

/**
 * string.gmatch (s, pattern)
 *
 * Returns an iterator function that, each time it is called, returns the next captures
 * from pattern over string s. If pattern specifies no captures, then the
 * whole match is produced in each call.
 *
 * As an example, the following loop
 *   s = "hello world from Lua"
 *   for w in string.gmatch(s, "%a+") do
 *      print(w)
 *   end
 *
 * will iterate over all the words from string s, printing one per line.
 * The next example collects all pairs key=value from the given string into a table:
 *   t = {}
 *   s = "from=world, to=Lua"
 *   for k, v in string.gmatch(s, "(%w+)=(%w+)") do
 *     t[k] = v
 *   end
 *
 * For this function, a '^' at the start of a pattern does not work as an anchor,
 * as this would prevent the iteration.
 */
static Varargs gmatch(Varargs args) {
    LuaString src = args.checkstring(1);
    LuaString pat = args.checkstring(2);
    return new GMatchAux(args, src, pat);
}
Also used : LuaString(org.luaj.vm2.LuaString)

Example 4 with LuaString

use of org.luaj.vm2.LuaString in project LuaViewSDK by alibaba.

the class StringLib method str_find_aux.

/**
 * This utility method implements both string.find and string.match.
 */
static Varargs str_find_aux(Varargs args, boolean find) {
    LuaString s = args.checkstring(1);
    LuaString pat = args.checkstring(2);
    int init = args.optint(3, 1);
    if (init > 0) {
        init = Math.min(init - 1, s.length());
    } else if (init < 0) {
        init = Math.max(0, s.length() + init);
    }
    boolean fastMatch = find && (args.arg(4).toboolean() || pat.indexOfAny(SPECIALS) == -1);
    if (fastMatch) {
        int result = s.indexOf(pat, init);
        if (result != -1) {
            return varargsOf(valueOf(result + 1), valueOf(result + pat.length()));
        }
    } else {
        MatchState ms = new MatchState(args, s, pat);
        boolean anchor = false;
        int poff = 0;
        if (pat.luaByte(0) == '^') {
            anchor = true;
            poff = 1;
        }
        int soff = init;
        do {
            int res;
            ms.reset();
            if ((res = ms.match(soff, poff)) != -1) {
                if (find) {
                    return varargsOf(valueOf(soff + 1), valueOf(res), ms.push_captures(false, soff, res));
                } else {
                    return ms.push_captures(true, soff, res);
                }
            }
        } while (soff++ < s.length() && !anchor);
    }
    return NIL;
}
Also used : LuaString(org.luaj.vm2.LuaString)

Example 5 with LuaString

use of org.luaj.vm2.LuaString in project LuaViewSDK by alibaba.

the class StringLib method reverse.

/**
 * string.reverse (s)
 *
 * Returns a string that is the string s reversed.
 */
static LuaValue reverse(LuaValue arg) {
    LuaString s = arg.checkstring();
    int n = s.length();
    byte[] b = new byte[n];
    for (int i = 0, j = n - 1; i < n; i++, j--) b[j] = (byte) s.luaByte(i);
    return LuaString.valueOf(b);
}
Also used : LuaString(org.luaj.vm2.LuaString)

Aggregations

LuaString (org.luaj.vm2.LuaString)25 LuaValue (org.luaj.vm2.LuaValue)5 Prototype (org.luaj.vm2.Prototype)3 Buffer (org.luaj.vm2.Buffer)2 LuaPrint (org.luaj.vm2.log.LuaPrint)2 LocVars (org.luaj.vm2.LocVars)1 Upvaldesc (org.luaj.vm2.Upvaldesc)1 BlockCnt (org.luaj.vm2.compiler.FuncState.BlockCnt)1