Java Code Examples for org.luaj.vm2.Varargs#checkstring()

The following examples show how to use org.luaj.vm2.Varargs#checkstring() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: StringLib.java    From luaj with MIT License 6 votes vote down vote up
public Varargs invoke(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;
	}
}
 
Example 2
Source File: PackageLib.java    From VideoOS-Android-SDK with GNU General Public License v3.0 6 votes vote down vote up
public Varargs invoke(Varargs args) {
            LuaString name = args.checkstring(1);
//            InputStream is = null;

            // get package path
            LuaValue path = package_.get(_PATH);
            if (!path.isstring())
                return valueOf("package.path is not a string");

            // get the searchpath function.
            Varargs v = package_.get(_SEARCHPATH).invoke(varargsOf(name, path));

            // Did we get a result?
            if (!v.isstring(1))
                return v.arg(2).tostring();
            LuaString filename = v.arg1().strvalue();

            // Try to load the file.
            v = globals.loadfile(filename.tojstring());
            if (v.arg1().isfunction())
                return LuaValue.varargsOf(v.arg1(), filename);

            // report error
            return varargsOf(NIL, valueOf("'" + filename + "': " + v.arg(2).tojstring()));
        }
 
Example 3
Source File: StringLib.java    From VideoOS-Android-SDK with GNU General Public License v3.0 6 votes vote down vote up
/**
 * string.byte (s [, i [, j]])
 * <p>
 * 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.
 * <p>
 * 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);
}
 
Example 4
Source File: StringLib.java    From XPrivacyLua with GNU General Public License v3.0 6 votes vote down vote up
public Varargs invoke(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;
	}
}
 
Example 5
Source File: StringLib.java    From luaj with MIT License 6 votes vote down vote up
public Varargs invoke(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);
}
 
Example 6
Source File: StringLib.java    From HtmlNative with Apache License 2.0 6 votes vote down vote up
public Varargs invoke(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;
	}
}
 
Example 7
Source File: PackageLib.java    From VideoOS-Android-SDK with GNU General Public License v3.0 5 votes vote down vote up
public Varargs invoke(Varargs args) {
    LuaString name = args.checkstring(1);
    LuaValue val = package_.get(_PRELOAD).get(name);
    return val.isnil() ?
            valueOf("\n\tno field package.preload['" + name + "']") :
            val;
}
 
Example 8
Source File: PackageLib.java    From luaj with MIT License 5 votes vote down vote up
public Varargs invoke(Varargs args) {
	LuaString name = args.checkstring(1);
	LuaValue val = package_.get(_PRELOAD).get(name);
	return val.isnil()?
		valueOf("\n\tno field package.preload['"+name+"']"):
		val;
}
 
Example 9
Source File: StringLib.java    From HtmlNative with Apache License 2.0 5 votes vote down vote up
public Varargs invoke(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));
}
 
Example 10
Source File: StringLib.java    From luaj with MIT License 5 votes vote down vote up
public Varargs invoke(Varargs args) {
	LuaString s = args.checkstring( 1 );
	int n = args.checkint( 2 );
	final byte[] bytes = new byte[ s.length() * n ];
	int len = s.length();
	for ( int offset = 0; offset < bytes.length; offset += len ) {
		s.copyInto( 0, bytes, offset, len );
	}
	return LuaString.valueUsing( bytes );
}
 
Example 11
Source File: StringLib.java    From HtmlNative with Apache License 2.0 5 votes vote down vote up
public Varargs invoke(Varargs args) {
	LuaString s = args.checkstring( 1 );
	int n = args.checkint( 2 );
	final byte[] bytes = new byte[ s.length() * n ];
	int len = s.length();
	for ( int offset = 0; offset < bytes.length; offset += len ) {
		s.copyInto( 0, bytes, offset, len );
	}
	return LuaString.valueUsing( bytes );
}
 
Example 12
Source File: PackageLib.java    From XPrivacyLua with GNU General Public License v3.0 5 votes vote down vote up
public Varargs invoke(Varargs args) {
	LuaString name = args.checkstring(1);
	LuaValue val = package_.get(_PRELOAD).get(name);
	return val.isnil()? 
		valueOf("\n\tno field package.preload['"+name+"']"):
		val;
}
 
Example 13
Source File: StringLib.java    From luaj with MIT License 5 votes vote down vote up
public Varargs invoke(Varargs args) {
	LuaString src = args.checkstring( 1 );
	final int srclen = src.length();
	LuaString p = args.checkstring( 2 );
	int lastmatch = -1; /* end of last match */
	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 && res != lastmatch ) {  /* match? */
			n++;
			ms.add_value( lbuf, soffset, res, repl );  /* add replacement to buffer */
			soffset = lastmatch = res;
		}
		else if ( soffset < srclen ) /* otherwise, skip one character */
			lbuf.append( (byte) src.luaByte( soffset++ ) );
		else break;   /* end of subject */
		if ( anchor ) break;
	}
	lbuf.append( src.substring( soffset, srclen ) );
	return varargsOf(lbuf.tostring(), valueOf(n));
}
 
Example 14
Source File: StringLib.java    From luaj with MIT License 4 votes vote down vote up
public Varargs invoke(Varargs args) {
	LuaString fmt = args.checkstring( 1 );
	final int n = fmt.length();
	Buffer result = new Buffer(n);
	int arg = 1;
	int c;
	
	for ( int i = 0; i < n; ) {
		switch ( c = fmt.luaByte( i++ ) ) {
		case '\n':
			result.append( "\n" );
			break;
		default:
			result.append( (byte) c );
			break;
		case L_ESC:
			if ( i < n ) {
				if ( ( c = fmt.luaByte( i ) ) == L_ESC ) {
					++i;
					result.append( (byte)L_ESC );
				} else {
					arg++;
					FormatDesc fdsc = new FormatDesc(args, fmt, i );
					i += fdsc.length;
					switch ( fdsc.conversion ) {
					case 'c':
						fdsc.format( result, (byte)args.checkint( arg ) );
						break;
					case 'i':
					case 'd':
						fdsc.format( result, args.checklong( arg ) );
						break;
					case 'o':
					case 'u':
					case 'x':
					case 'X':
						fdsc.format( result, args.checklong( arg ) );
						break;
					case 'e':
					case 'E':
					case 'f':
					case 'g':
					case 'G':
						fdsc.format( result, args.checkdouble( arg ) );
						break;
					case 'q':
						addquoted( result, args.checkstring( arg ) );
						break;
					case 's': {
						LuaString s = args.checkstring( arg );
						if ( fdsc.precision == -1 && s.length() >= 100 ) {
							result.append( s );
						} else {
							fdsc.format( result, s );
						}
					}	break;
					default:
						error("invalid option '%"+(char)fdsc.conversion+"' to 'format'");
						break;
					}
				}
			}
		}
	}
	
	return result.tostring();
}
 
Example 15
Source File: PackageLib.java    From XPrivacyLua with GNU General Public License v3.0 4 votes vote down vote up
public Varargs loadlib( Varargs args ) {
	args.checkstring(1);
	return varargsOf(NIL, valueOf("dynamic libraries not enabled"), valueOf("absent"));
}
 
Example 16
Source File: StringLib.java    From VideoOS-Android-SDK with GNU General Public License v3.0 4 votes vote down vote up
/**
 * 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;
}
 
Example 17
Source File: StringLib.java    From luaj with MIT License 4 votes vote down vote up
/**
 * 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.length() > 0 && 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;
}
 
Example 18
Source File: StringLib.java    From HtmlNative with Apache License 2.0 4 votes vote down vote up
public Varargs invoke(Varargs args) {
	LuaString src = args.checkstring( 1 );
	LuaString pat = args.checkstring( 2 );
	return new GMatchAux(args, src, pat);
}
 
Example 19
Source File: StringLib.java    From VideoOS-Android-SDK with GNU General Public License v3.0 4 votes vote down vote up
/**
 * string.format (formatstring, ...)
 * <p>
 * Returns a formatted version of its variable number of arguments following
 * the description given in its first argument (which must be a string).
 * The format string follows the same rules as the printf family of standard C functions.
 * The only differences are that the options/modifiers *, l, L, n, p, and h are not supported
 * and that there is an extra option, q. The q option formats a string in a form suitable
 * to be safely read back by the Lua interpreter: the string is written between double quotes,
 * and all double quotes, newlines, embedded zeros, and backslashes in the string are correctly
 * escaped when written. For instance, the call
 * string.format('%q', 'a string with "quotes" and \n new line')
 * <p>
 * will produce the string:
 * "a string with \"quotes\" and \
 * new line"
 * <p>
 * The options c, d, E, e, f, g, G, i, o, u, X, and x all expect a number as argument,
 * whereas q and s expect a string.
 * <p>
 * This function does not accept string values containing embedded zeros,
 * except as arguments to the q option.
 */
static Varargs format(Varargs args) {
    LuaString fmt = args.checkstring(1);
     int n = fmt.length();
    Buffer result = new Buffer(n);
    int arg = 1;
    int c;

    for (int i = 0; i < n; ) {
        switch (c = fmt.luaByte(i++)) {
            case '\n':
                result.append("\n");
                break;
            default:
                result.append((byte) c);
                break;
            case L_ESC:
                if (i < n) {
                    if ((c = fmt.luaByte(i)) == L_ESC) {
                        ++i;
                        result.append((byte) L_ESC);
                    } else {
                        arg++;
                        FormatDesc fdsc = new FormatDesc(args, fmt, i);
                        i += fdsc.length;
                        switch (fdsc.conversion) {
                            case 'c':
                                fdsc.format(result, (byte) args.checkint(arg));
                                break;
                            case 'i':
                            case 'd':
                                fdsc.format(result, args.checkint(arg));
                                break;
                            case 'o':
                            case 'u':
                            case 'x':
                            case 'X':
                                fdsc.format(result, args.checklong(arg));
                                break;
                            case 'e':
                            case 'E':
                            case 'f':
                            case 'g':
                            case 'G':
                                fdsc.format(result, args.checkdouble(arg), fdsc.precision);
                                break;
                            case 'q':
                                addquoted(result, args.checkstring(arg));
                                break;
                            case 's': {
                                LuaString s = args.checkstring(arg);
                                if (fdsc.precision == -1 && s.length() >= 100) {
                                    result.append(s);
                                } else {
                                    fdsc.format(result, s);
                                }
                            }
                            break;
                            default:
                                error("invalid option '%" + (char) fdsc.conversion + "' to 'format'");
                                break;
                        }
                    }
                }
        }
    }

    return result.tostring();
}
 
Example 20
Source File: StringLib.java    From VideoOS-Android-SDK with GNU General Public License v3.0 2 votes vote down vote up
/**
 * string.gmatch (s, pattern)
 * <p>
 * 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.
 * <p>
 * As an example, the following loop
 * s = "hello world from Lua"
 * for w in string.gmatch(s, "%a+") do
 * print(w)
 * end
 * <p>
 * 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
 * <p>
 * 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);
}