Java Code Examples for org.luaj.vm2.LuaString#length()

The following examples show how to use org.luaj.vm2.LuaString#length() . 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
/**
 * string.sub (s, i [, j])
 * <p>
 * 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) {
     LuaString s = args.checkstring(1);
     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
void add_s(Buffer lbuf, LuaString news, int soff, int e) {
    int l = news.length();
    for (int i = 0; i < l; ++i) {
        byte b = (byte) news.luaByte(i);
        if (b != L_ESC) {
            lbuf.append((byte) b);
        } else {
            ++i; // skip ESC
            b = (byte) news.luaByte(i);
            if (!Character.isDigit((char) b)) {
                lbuf.append(b);
            } else if (b == '0') {
                lbuf.append(s.substring(soff, e));
            } else {
                lbuf.append(push_onecapture(b - '1', soff, e).strvalue());
            }
        }
    }
}
 
Example 3
Source Project: luaj   File: StringLib.java    License: MIT License 6 votes vote down vote up
private void add_s( Buffer lbuf, LuaString news, int soff, int e ) {
	int l = news.length();
	for ( int i = 0; i < l; ++i ) {
		byte b = (byte) news.luaByte( i );
		if ( b != L_ESC ) {
			lbuf.append( (byte) b );
		} else {
			++i; // skip ESC
			b = (byte)(i < l ? news.luaByte( i ) : 0);
			if ( !Character.isDigit( (char) b ) ) {
				if (b != L_ESC) error( "invalid use of '" + (char)L_ESC +
					"' in replacement string: after '" + (char)L_ESC +
					"' must be '0'-'9' or '" + (char)L_ESC +
					"', but found " + (i < l ? "symbol '" + (char)b + "' with code " + b +
					" at pos " + (i + 1) :
					"end of string"));
				lbuf.append( b );
			} else if ( b == '0' ) {
				lbuf.append( s.substring( soff, e ) );
			} else {
				lbuf.append( push_onecapture( b - '1', soff, e ).strvalue() );
			}
		}
	}
}
 
Example 4
Source Project: HtmlNative   File: StringLib.java    License: Apache License 2.0 6 votes vote down vote up
private void add_s( Buffer lbuf, LuaString news, int soff, int e ) {
	int l = news.length();
	for ( int i = 0; i < l; ++i ) {
		byte b = (byte) news.luaByte( i );
		if ( b != L_ESC ) {
			lbuf.append( (byte) b );
		} else {
			++i; // skip ESC
			b = (byte) news.luaByte( i );
			if ( !Character.isDigit( (char) b ) ) {
				lbuf.append( b );
			} else if ( b == '0' ) {
				lbuf.append( s.substring( soff, e ) );
			} else {
				lbuf.append( push_onecapture( b - '1', soff, e ).strvalue() );
			}
		}
	}
}
 
Example 5
static void addquoted(Buffer buf, LuaString s) {
    int c;
    buf.append((byte) '"');
    for (int i = 0, n = s.length(); i < n; i++) {
        switch (c = s.luaByte(i)) {
            case '"':
            case '\\':
            case '\n':
                buf.append((byte) '\\');
                buf.append((byte) c);
                break;
            default:
                if (c <= 0x1F || c == 0x7F) {
                    buf.append((byte) '\\');
                    if (i + 1 == n || s.luaByte(i + 1) < '0' || s.luaByte(i + 1) > '9') {
                        buf.append(Integer.toString(c));
                    } else {
                        buf.append((byte) '0');
                        buf.append((byte) (char) ('0' + c / 10));
                        buf.append((byte) (char) ('0' + c % 10));
                    }
                } else {
                    buf.append((byte) c);
                }
                break;
        }
    }
    buf.append((byte) '"');
}
 
Example 6
Source Project: HtmlNative   File: StringLib.java    License: Apache License 2.0 5 votes vote down vote up
private static void addquoted(Buffer buf, LuaString s) {
	int c;
	buf.append( (byte) '"' );
	for ( int i = 0, n = s.length(); i < n; i++ ) {
		switch ( c = s.luaByte( i ) ) {
		case '"': case '\\':  case '\n':
			buf.append( (byte)'\\' );
			buf.append( (byte)c );
			break;
		default:
			if (c <= 0x1F || c == 0x7F) {
				buf.append( (byte) '\\' );
				if (i+1 == n || s.luaByte(i+1) < '0' || s.luaByte(i+1) > '9') {
					buf.append(Integer.toString(c));
				} else {
					buf.append( (byte) '0' );
					buf.append( (byte) (char) ('0' + c / 10) );
					buf.append( (byte) (char) ('0' + c % 10) );
				}
			} else {
				buf.append((byte) c);
			}
			break;
		}
	}
	buf.append( (byte) '"' );
}
 
Example 7
Source Project: HtmlNative   File: StringLib.java    License: 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 8
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 9
public LuaValue call(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.valueUsing( b );
}
 
Example 10
Source Project: HtmlNative   File: StringLib.java    License: 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 11
Source Project: luaj   File: StringLib.java    License: 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 12
/**
 * 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 13
public FormatDesc(Varargs args, LuaString strfrmt,  int start) {
    int p = start, n = strfrmt.length();
    int c = 0;

    boolean moreFlags = true;
    while (moreFlags) {
        switch (c = ((p < n) ? strfrmt.luaByte(p++) : 0)) {
            case '-':
                leftAdjust = true;
                break;
            case '+':
                explicitPlus = true;
                break;
            case ' ':
                space = true;
                break;
            case '#':
                alternateForm = true;
                break;
            case '0':
                zeroPad = true;
                break;
            default:
                moreFlags = false;
                break;
        }
    }
    if (p - start > MAX_FLAGS)
        error("invalid format (repeated flags)");

    width = -1;
    if (Character.isDigit((char) c)) {
        width = c - '0';
        c = ((p < n) ? strfrmt.luaByte(p++) : 0);
        if (Character.isDigit((char) c)) {
            width = width * 10 + (c - '0');
            c = ((p < n) ? strfrmt.luaByte(p++) : 0);
        }
    }

    precision = -1;
    if (c == '.') {
        c = ((p < n) ? strfrmt.luaByte(p++) : 0);
        if (Character.isDigit((char) c)) {
            precision = c - '0';
            c = ((p < n) ? strfrmt.luaByte(p++) : 0);
            if (Character.isDigit((char) c)) {
                precision = precision * 10 + (c - '0');
                c = ((p < n) ? strfrmt.luaByte(p++) : 0);
            }
        }
    }

    if (Character.isDigit((char) c))
        error("invalid format (width or precision too long)");

    zeroPad &= !leftAdjust; // '-' overrides '0'
    conversion = c;
    length = p - start;
}
 
Example 14
public GMatchAux(Varargs args, LuaString src, LuaString pat) {
    this.srclen = src.length();
    this.ms = new MatchState(args, src, pat);
    this.soffset = 0;
}
 
Example 15
Source Project: HtmlNative   File: StringLib.java    License: Apache License 2.0 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.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 ) );
						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 16
/**
 * 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 Project: HtmlNative   File: StringLib.java    License: Apache License 2.0 4 votes vote down vote up
public FormatDesc(Varargs args, LuaString strfrmt, final int start) {
	int p = start, n = strfrmt.length();
	int c = 0;
	
	boolean moreFlags = true;
	while ( moreFlags ) {
		switch ( c = ( (p < n) ? strfrmt.luaByte( p++ ) : 0 ) ) {
		case '-': leftAdjust = true; break;
		case '+': explicitPlus = true; break;
		case ' ': space = true; break;
		case '#': alternateForm = true; break;
		case '0': zeroPad = true; break;
		default: moreFlags = false; break;
		}
	}
	if ( p - start > MAX_FLAGS )
		error("invalid format (repeated flags)");
	
	width = -1;
	if ( Character.isDigit( (char)c ) ) {
		width = c - '0';
		c = ( (p < n) ? strfrmt.luaByte( p++ ) : 0 );
		if ( Character.isDigit( (char) c ) ) {
			width = width * 10 + (c - '0');
			c = ( (p < n) ? strfrmt.luaByte( p++ ) : 0 );
		}
	}
	
	precision = -1;
	if ( c == '.' ) {
		c = ( (p < n) ? strfrmt.luaByte( p++ ) : 0 );
		if ( Character.isDigit( (char) c ) ) {
			precision = c - '0';
			c = ( (p < n) ? strfrmt.luaByte( p++ ) : 0 );
			if ( Character.isDigit( (char) c ) ) {
				precision = precision * 10 + (c - '0');
				c = ( (p < n) ? strfrmt.luaByte( p++ ) : 0 );
			}
		}
	}
	
	if ( Character.isDigit( (char) c ) )
		error("invalid format (width or precision too long)");
	
	zeroPad &= !leftAdjust; // '-' overrides '0'
	conversion = c;
	length = p - start;
}
 
Example 18
public FormatDesc(Varargs args, LuaString strfrmt, final int start) {
	int p = start, n = strfrmt.length();
	int c = 0;
	
	boolean moreFlags = true;
	while ( moreFlags ) {
		switch ( c = ( (p < n) ? strfrmt.luaByte( p++ ) : 0 ) ) {
		case '-': leftAdjust = true; break;
		case '+': explicitPlus = true; break;
		case ' ': space = true; break;
		case '#': alternateForm = true; break;
		case '0': zeroPad = true; break;
		default: moreFlags = false; break;
		}
	}
	if ( p - start > MAX_FLAGS )
		error("invalid format (repeated flags)");
	
	width = -1;
	if ( Character.isDigit( (char)c ) ) {
		width = c - '0';
		c = ( (p < n) ? strfrmt.luaByte( p++ ) : 0 );
		if ( Character.isDigit( (char) c ) ) {
			width = width * 10 + (c - '0');
			c = ( (p < n) ? strfrmt.luaByte( p++ ) : 0 );
		}
	}
	
	precision = -1;
	if ( c == '.' ) {
		c = ( (p < n) ? strfrmt.luaByte( p++ ) : 0 );
		if ( Character.isDigit( (char) c ) ) {
			precision = c - '0';
			c = ( (p < n) ? strfrmt.luaByte( p++ ) : 0 );
			if ( Character.isDigit( (char) c ) ) {
				precision = precision * 10 + (c - '0');
				c = ( (p < n) ? strfrmt.luaByte( p++ ) : 0 );
			}
		}
	}
	
	if ( Character.isDigit( (char) c ) )
		error("invalid format (width or precision too long)");
	
	zeroPad &= !leftAdjust; // '-' overrides '0'
	conversion = c;
	length = p - start;
}
 
Example 19
public GMatchAux(Varargs args, LuaString src, LuaString pat) {
	this.srclen = src.length();
	this.ms = new MatchState(args, src, pat);
	this.soffset = 0;
}
 
Example 20
Source Project: HtmlNative   File: StringLib.java    License: Apache License 2.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;
}