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

The following examples show how to use org.luaj.vm2.LuaString#luaByte() . 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 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 6
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 7
Source File: StringLib.java    From HtmlNative with Apache License 2.0 5 votes vote down vote up
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 8
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 9
Source File: StringLib.java    From XPrivacyLua with GNU General Public License v3.0 5 votes vote down vote up
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 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 11
Source File: StringLib.java    From VideoOS-Android-SDK with GNU General Public License v3.0 5 votes vote down vote up
/**
 * string.reverse (s)
 * <p>
 * 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);
}
 
Example 12
Source File: StringLib.java    From XPrivacyLua with GNU General Public License v3.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 13
Source File: StringLib.java    From XPrivacyLua 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 14
Source File: StringLib.java    From HtmlNative with 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 15
Source File: StringLib.java    From XPrivacyLua with GNU General Public License v3.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
Source File: StringLib.java    From luaj with MIT License 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;
	src = strfrmt.substring(start - 1, p).tojstring();
}
 
Example 17
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 18
Source File: StringLib.java    From HtmlNative with 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;
}
 
Example 19
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 20
Source File: StringLib.java    From VideoOS-Android-SDK with GNU General Public License v3.0 4 votes vote down vote up
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;
}