org.luaj.vm2.Buffer Java Examples

The following examples show how to use org.luaj.vm2.Buffer. 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
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 #2
Source File: StringLib.java    From HtmlNative with 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 #3
Source File: StringLib.java    From VideoOS-Android-SDK with GNU General Public License v3.0 6 votes vote down vote up
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 #4
Source File: StringLib.java    From XPrivacyLua with GNU General Public License v3.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
Source File: StringLib.java    From XPrivacyLua with GNU General Public License v3.0 5 votes vote down vote up
public void add_value( Buffer lbuf, int soffset, int end, LuaValue repl ) {
	switch ( repl.type() ) {
	case LuaValue.TSTRING:
	case LuaValue.TNUMBER:
		add_s( lbuf, repl.strvalue(), soffset, end );
		return;
		
	case LuaValue.TFUNCTION:
		repl = repl.invoke( push_captures( true, soffset, end ) ).arg1();
		break;
		
	case LuaValue.TTABLE:
		// Need to call push_onecapture here for the error checking
		repl = repl.get( push_onecapture( 0, soffset, end ) );
		break;
		
	default:
		error( "bad argument: string/function/table expected" );
		return;
	}
	
	if ( !repl.toboolean() ) {
		repl = s.substring( soffset, end );
	} else if ( ! repl.isstring() ) {
		error( "invalid replacement value (a "+repl.typename()+")" );
	}
	lbuf.append( repl.strvalue() );
}
 
Example #6
Source File: StringLib.java    From luaj with MIT License 5 votes vote down vote up
public void add_value( Buffer lbuf, int soffset, int end, LuaValue repl ) {
	switch ( repl.type() ) {
	case LuaValue.TSTRING:
	case LuaValue.TNUMBER:
		add_s( lbuf, repl.strvalue(), soffset, end );
		return;
		
	case LuaValue.TFUNCTION:
		repl = repl.invoke( push_captures( true, soffset, end ) ).arg1();
		break;
		
	case LuaValue.TTABLE:
		// Need to call push_onecapture here for the error checking
		repl = repl.get( push_onecapture( 0, soffset, end ) );
		break;
		
	default:
		error( "bad argument: string/function/table expected" );
		return;
	}
	
	if ( !repl.toboolean() ) {
		repl = s.substring( soffset, end );
	} else if ( ! repl.isstring() ) {
		error( "invalid replacement value (a "+repl.typename()+")" );
	}
	lbuf.append( repl.strvalue() );
}
 
Example #7
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 #8
Source File: StringLib.java    From luaj with MIT License 5 votes vote down vote up
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 #9
Source File: StringLib.java    From HtmlNative with Apache License 2.0 5 votes vote down vote up
public void add_value( Buffer lbuf, int soffset, int end, LuaValue repl ) {
	switch ( repl.type() ) {
	case LuaValue.TSTRING:
	case LuaValue.TNUMBER:
		add_s( lbuf, repl.strvalue(), soffset, end );
		return;
		
	case LuaValue.TFUNCTION:
		repl = repl.invoke( push_captures( true, soffset, end ) ).arg1();
		break;
		
	case LuaValue.TTABLE:
		// Need to call push_onecapture here for the error checking
		repl = repl.get( push_onecapture( 0, soffset, end ) );
		break;
		
	default:
		error( "bad argument: string/function/table expected" );
		return;
	}
	
	if ( !repl.toboolean() ) {
		repl = s.substring( soffset, end );
	} else if ( ! repl.isstring() ) {
		error( "invalid replacement value (a "+repl.typename()+")" );
	}
	lbuf.append( repl.strvalue() );
}
 
Example #10
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 #11
Source File: StringLib.java    From HtmlNative with 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 #12
Source File: StringLib.java    From XPrivacyLua with GNU General Public License v3.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 #13
Source File: StringLib.java    From VideoOS-Android-SDK with GNU General Public License v3.0 5 votes vote down vote up
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 #14
Source File: StringLib.java    From VideoOS-Android-SDK with GNU General Public License v3.0 5 votes vote down vote up
public void add_value(Buffer lbuf, int soffset, int end, LuaValue repl) {
    switch (repl.type()) {
        case LuaValue.TSTRING:
        case LuaValue.TNUMBER:
            add_s(lbuf, repl.strvalue(), soffset, end);
            return;

        case LuaValue.TFUNCTION:
            repl = repl.invoke(push_captures(true, soffset, end)).arg1();
            break;

        case LuaValue.TTABLE:
            // Need to call push_onecapture here for the error checking
            repl = repl.get(push_onecapture(0, soffset, end));
            break;

        default:
            error("bad argument: string/function/table expected");
            return;
    }

    if (!repl.toboolean()) {
        repl = s.substring(soffset, end);
    } else if (!repl.isstring()) {
        error("invalid replacement value (a " + repl.typename() + ")");
    }
    lbuf.append(repl.strvalue());
}
 
Example #15
Source File: StringLib.java    From VideoOS-Android-SDK with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 小数位数不足的情况下末尾补充0
 *
 * @param buf
 * @param x
 * @param precision
 */
public void format(Buffer buf, double x, int precision) {
    String value = String.valueOf(x);
    int dotIndex = value.indexOf('.');
    int count = dotIndex == -1 ? precision : (precision - (value.length() - dotIndex)) + 1;
    buf.append(value);
    pad(buf, '0', count);
}
 
Example #16
Source File: StringLib.java    From XPrivacyLua with GNU General Public License v3.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 #17
Source File: StringLib.java    From HtmlNative with Apache License 2.0 4 votes vote down vote up
public void format(Buffer buf, LuaString s) {
	int nullindex = s.indexOf( (byte)'\0', 0 );
	if ( nullindex != -1 )
		s = s.substring( 0, nullindex );
	buf.append(s);
}
 
Example #18
Source File: StringLib.java    From VideoOS-Android-SDK with GNU General Public License v3.0 4 votes vote down vote up
public void format(Buffer buf, byte c) {
    // TODO: not clear that any of width, precision, or flags apply here.
    buf.append(c);
}
 
Example #19
Source File: StringLib.java    From VideoOS-Android-SDK with GNU General Public License v3.0 4 votes vote down vote up
public void format(Buffer buf, long number) {
    String digits;

    if (number == 0 && precision == 0) {
        digits = "";
    } else {
        int radix;
        switch (conversion) {
            case 'x':
            case 'X':
                radix = 16;
                break;
            case 'o':
                radix = 8;
                break;
            default:
                radix = 10;
                break;
        }
        digits = Long.toString(number, radix);
        if (conversion == 'X')
            digits = digits.toUpperCase();
    }

    int minwidth = digits.length();
    int ndigits = minwidth;
    int nzeros;

    if (number < 0) {
        ndigits--;
    } else if (explicitPlus || space) {
        minwidth++;
    }

    if (precision > ndigits)
        nzeros = precision - ndigits;
    else if (precision == -1 && zeroPad && width > minwidth)
        nzeros = width - minwidth;
    else
        nzeros = 0;

    minwidth += nzeros;
    int nspaces = width > minwidth ? width - minwidth : 0;

    if (!leftAdjust)
        pad(buf, ' ', nspaces);

    if (number < 0) {
        if (nzeros > 0) {
            buf.append((byte) '-');
            digits = digits.substring(1);
        }
    } else if (explicitPlus) {
        buf.append((byte) '+');
    } else if (space) {
        buf.append((byte) ' ');
    }

    if (nzeros > 0)
        pad(buf, '0', nzeros);

    buf.append(digits);

    if (leftAdjust)
        pad(buf, ' ', nspaces);
}
 
Example #20
Source File: StringLib.java    From luaj with MIT License 4 votes vote down vote up
public final void pad(Buffer buf, char c, int n) {
	byte b = (byte)c;
	while ( n-- > 0 )
		buf.append(b);
}
 
Example #21
Source File: StringLib.java    From luaj with MIT License 4 votes vote down vote up
public void format(Buffer buf, LuaString s) {
	int nullindex = s.indexOf( (byte)'\0', 0 );
	if ( nullindex != -1 )
		s = s.substring( 0, nullindex );
	buf.append(s);
}
 
Example #22
Source File: StringLib.java    From luaj with MIT License 4 votes vote down vote up
public void format(Buffer buf, double x) {
	buf.append( StringLib.this.format(src, x) );
}
 
Example #23
Source File: StringLib.java    From luaj with MIT License 4 votes vote down vote up
public void format(Buffer buf, long number) {
	String digits;
	
	if ( number == 0 && precision == 0 ) {
		digits = "";
	} else {
		int radix;
		switch ( conversion ) {
		case 'x':
		case 'X':
			radix = 16;
			break;
		case 'o':
			radix = 8;
			break;
		default:
			radix = 10;
			break;
		}
		digits = Long.toString( number, radix );
		if ( conversion == 'X' )
			digits = digits.toUpperCase();
	}
	
	int minwidth = digits.length();
	int ndigits = minwidth;
	int nzeros;
	
	if ( number < 0 ) {
		ndigits--;
	} else if ( explicitPlus || space ) {
		minwidth++;
	}
	
	if ( precision > ndigits )
		nzeros = precision - ndigits;
	else if ( precision == -1 && zeroPad && width > minwidth )
		nzeros = width - minwidth;
	else
		nzeros = 0;
	
	minwidth += nzeros;
	int nspaces = width > minwidth ? width - minwidth : 0;
	
	if ( !leftAdjust )
		pad( buf, ' ', nspaces );
	
	if ( number < 0 ) {
		if ( nzeros > 0 ) {
			buf.append( (byte)'-' );
			digits = digits.substring( 1 );
		}
	} else if ( explicitPlus ) {
		buf.append( (byte)'+' );
	} else if ( space ) {
		buf.append( (byte)' ' );
	}
	
	if ( nzeros > 0 )
		pad( buf, '0', nzeros );
	
	buf.append( digits );
	
	if ( leftAdjust )
		pad( buf, ' ', nspaces );
}
 
Example #24
Source File: StringLib.java    From luaj with MIT License 4 votes vote down vote up
public void format(Buffer buf, byte c) {
	// TODO: not clear that any of width, precision, or flags apply here.
	buf.append(c);
}
 
Example #25
Source File: StringLib.java    From VideoOS-Android-SDK with GNU General Public License v3.0 4 votes vote down vote up
public void format(Buffer buf, double x) {
    // TODO
    buf.append(String.valueOf(x));
}
 
Example #26
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 #27
Source File: StringLib.java    From VideoOS-Android-SDK with GNU General Public License v3.0 4 votes vote down vote up
public void format(Buffer buf, LuaString s) {
    int nullindex = s.indexOf((byte) '\0', 0);
    if (nullindex != -1)
        s = s.substring(0, nullindex);
    buf.append(s);
}
 
Example #28
Source File: StringLib.java    From VideoOS-Android-SDK with GNU General Public License v3.0 4 votes vote down vote up
public static  void pad(Buffer buf, char c, int n) {
    byte b = (byte) c;
    while (n-- > 0)
        buf.append(b);
}
 
Example #29
Source File: StringLib.java    From HtmlNative with Apache License 2.0 4 votes vote down vote up
public static final void pad(Buffer buf, char c, int n) {
	byte b = (byte)c;
	while ( n-- > 0 )
		buf.append(b);
}
 
Example #30
Source File: StringLib.java    From HtmlNative with Apache License 2.0 4 votes vote down vote up
public void format(Buffer buf, double x) {
	// TODO
	buf.append( String.valueOf( x ) );
}