Search in sources :

Example 26 with LuaString

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

the class LuaC method cachedLuaString.

public LuaString cachedLuaString(LuaString s) {
    LuaString c = (LuaString) strings.get(s);
    if (c != null)
        return c;
    strings.put(s, s);
    return s;
}
Also used : LuaString(org.luaj.vm2.LuaString)

Example 27 with LuaString

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

the class Str method quoteString.

public static LuaString quoteString(String image) {
    String s = image.substring(1, image.length() - 1);
    byte[] bytes = unquote(s);
    return LuaString.valueOf(bytes);
}
Also used : LuaString(org.luaj.vm2.LuaString)

Example 28 with LuaString

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

the class StringLib method byte_.

/**
 * string.byte (s [, i [, j]])
 *
 * Returns the internal numerical codes of the
 * characters s[i], s[i+1], ..., s[j]. The default value for i is 1; the
 * default value for j is i.
 *
 * Note that numerical codes are not necessarily portable across platforms.
 *
 * @param args the calling args
 */
static Varargs byte_(Varargs args) {
    LuaString s = args.checkstring(1);
    int l = s.m_length;
    int posi = posrelat(args.optint(2, 1), l);
    int pose = posrelat(args.optint(3, posi), l);
    int n, i;
    if (posi <= 0)
        posi = 1;
    if (pose > l)
        pose = l;
    if (posi > pose)
        return NONE;
    /* empty interval; return no values */
    n = (int) (pose - posi + 1);
    if (posi + n <= pose)
        /* overflow? */
        error("string slice too long");
    LuaValue[] v = new LuaValue[n];
    for (i = 0; i < n; i++) v[i] = valueOf(s.luaByte(posi + i - 1));
    return varargsOf(v);
}
Also used : LuaString(org.luaj.vm2.LuaString) LuaValue(org.luaj.vm2.LuaValue)

Example 29 with LuaString

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

the class StringLib method gsub.

/**
 * string.gsub (s, pattern, repl [, n])
 * Returns a copy of s in which all (or the first n, if given) occurrences of the
 * pattern have been replaced by a replacement string specified by repl, which
 * may be a string, a table, or a function. gsub also returns, as its second value,
 * the total number of matches that occurred.
 *
 * If repl is a string, then its value is used for replacement.
 * The character % works as an escape character: any sequence in repl of the form %n,
 * with n between 1 and 9, stands for the value of the n-th captured substring (see below).
 * The sequence %0 stands for the whole match. The sequence %% stands for a single %.
 *
 * If repl is a table, then the table is queried for every match, using the first capture
 * as the key; if the pattern specifies no captures, then the whole match is used as the key.
 *
 * If repl is a function, then this function is called every time a match occurs,
 * with all captured substrings passed as arguments, in order; if the pattern specifies
 * no captures, then the whole match is passed as a sole argument.
 *
 * If the value returned by the table query or by the function call is a string or a number,
 * then it is used as the replacement string; otherwise, if it is false or nil,
 * then there is no replacement (that is, the original match is kept in the string).
 *
 * Here are some examples:
 * 	     x = string.gsub("hello world", "(%w+)", "%1 %1")
 * 	     --> x="hello hello world world"
 *
 *	     x = string.gsub("hello world", "%w+", "%0 %0", 1)
 *	     --> x="hello hello world"
 *
 *	     x = string.gsub("hello world from Lua", "(%w+)%s*(%w+)", "%2 %1")
 *	     --> x="world hello Lua from"
 *
 *	     x = string.gsub("home = $HOME, user = $USER", "%$(%w+)", os.getenv)
 *	     --> x="home = /home/roberto, user = roberto"
 *
 *	     x = string.gsub("4+5 = $return 4+5$", "%$(.-)%$", function (s)
 *	           return loadstring(s)()
 *       end)
 *	     --> x="4+5 = 9"
 *
 *	     local t = {name="lua", version="5.1"}
 *	     x = string.gsub("$name-$version.tar.gz", "%$(%w+)", t)
 *	     --> x="lua-5.1.tar.gz"
 */
static Varargs gsub(Varargs args) {
    LuaString src = args.checkstring(1);
    final int srclen = src.length();
    LuaString p = args.checkstring(2);
    LuaValue repl = args.arg(3);
    int max_s = args.optint(4, srclen + 1);
    final boolean anchor = p.length() > 0 && p.charAt(0) == '^';
    Buffer lbuf = new Buffer(srclen);
    MatchState ms = new MatchState(args, src, p);
    int soffset = 0;
    int n = 0;
    while (n < max_s) {
        ms.reset();
        int res = ms.match(soffset, anchor ? 1 : 0);
        if (res != -1) {
            n++;
            ms.add_value(lbuf, soffset, res, repl);
        }
        if (res != -1 && res > soffset)
            soffset = res;
        else if (soffset < srclen)
            lbuf.append((byte) src.luaByte(soffset++));
        else
            break;
        if (anchor)
            break;
    }
    lbuf.append(src.substring(soffset, srclen));
    return varargsOf(lbuf.tostring(), valueOf(n));
}
Also used : Buffer(org.luaj.vm2.Buffer) LuaString(org.luaj.vm2.LuaString) LuaValue(org.luaj.vm2.LuaValue)

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