org.luaj.vm
Class LString

java.lang.Object
  extended by org.luaj.vm.LValue
      extended by org.luaj.vm.LString

public class LString
extends LValue

A String implementation for Lua using bytes instead of chars. This should have the following advantages: (1) We can use strings as byte buffers, as Lua does, and therefore avoid questions about how to adapt Lua APIs that use strings with binary data. (2) Half the memory usage when strings are primarily ASCII TODO: Decide if/when to copy the bytes to a new array to ensure memory does not "leak" in the form of unused portions of byte arrays. Currently, for efficiency, new LStrings and substrings never create copies.


Field Summary
static LString[] LTYPENAMES
           
 byte[] m_bytes
           
 int m_hash
           
 int m_length
           
 int m_offset
           
 
Fields inherited from class org.luaj.vm.LValue
TM_INDEX, TM_METATABLE, TM_MODE, TM_NEWINDEX
 
Constructor Summary
LString(byte[] bytes)
          Construct a string from the given byte array.
LString(byte[] bytes, int off, int len)
          Construct a string from the given byte array and range.
LString(java.lang.String string)
          Construct a Lua string from the given Java string.
 
Method Summary
 int charAt(int index)
           
 int compareTo(LString o)
           
 void copyInto(int strOffset, byte[] bytes, int arrayOffset, int len)
          Copy the bytes of the string into the given byte array.
static void encodeToUtf8(java.lang.String string, byte[] bytes, int startOffset)
          Encode the given Java string as UTF-8 bytes, writing the result to bytes starting at offset.
static boolean equals(byte[] a, int i, byte[] b, int j, int n)
           
static boolean equals(LString a, int i, LString b, int j, int n)
           
 boolean equals(java.lang.Object o)
           
static LTable getMetatable()
          Get the metatable for all string values.
 int hashCode()
           
 int indexOf(byte b, int start)
           
 int indexOf(LString s, int start)
           
 int indexOfAny(LString accept)
          Java version of strpbrk, which is a terribly named C function.
 boolean isNumber()
          Returns true if this is or can be made into a number
 boolean isString()
          Return true if this is a lua string, meaning it is either a LString or LNumber,since all numbers are convertible to strings in lua
 int lastIndexOf(LString s)
           
 int length()
           
static int lengthAsUtf8(java.lang.String string)
          Count the number of bytes required to encode the string as UTF-8.
 LString luaAsString()
          Get the value as a LString
 boolean luaBinCmpString(int opcode, LString rhs)
           
 boolean luaBinCmpUnknown(int opcode, LValue lhs)
           
 LValue luaBinOpDouble(int opcode, double m_value)
           
 LValue luaBinOpInteger(int opcode, int m_value)
           
 LValue luaBinOpUnknown(int opcode, LValue lhs)
           
 int luaByte(int index)
           
 void luaConcatTo(java.io.ByteArrayOutputStream baos)
          Concatenate this value to a ByteArrayOutputStream
 LTable luaGetMetatable()
          Valid for all types: get a metatable.
 int luaGetType()
          Valid for all types: return the int value identifying the type of this value
 int luaLength()
          Built-in opcode LEN, for Strings and Tables
 LValue luaToNumber()
          Convert to a number if possible, or nil otherwise
 LValue luaToNumber(int base)
           
 LValue luaUnaryMinus()
          Arithmetic negative
static LString newStringCopy(byte[] buf, int off, int len)
           
static LString newStringCopy(LString src)
           
static LString newStringNoCopy(byte[] buf, int off, int len)
           
 LString substring(int beginIndex, int endIndex)
           
 java.io.ByteArrayInputStream toInputStream()
          Produce an InputStream instance from which the bytes of this LString can be read.
 java.lang.String toJavaString()
          Convert to Java string using UTF-8 encoding
static LString valueOf(double d)
           
static LString valueOf(int x)
           
static LString valueOf(java.lang.String s)
           
 void write(java.io.OutputStream os)
           
 void write(java.io.OutputStream os, int offset, int len)
          Write the specified substring of this string to the given output stream.
 
Methods inherited from class org.luaj.vm.LValue
compareError, conversionError, id, isClosure, isFunction, isInteger, isNil, isTable, isUserData, isValidKey, luaBinCmpDouble, luaBinCmpInteger, luaGetEnv, luaGetTable, luaGetTypeName, luaSetEnv, luaSetMetatable, luaSetTable, luaStackCall, toJavaBoolean, toJavaBoxedBoolean, toJavaBoxedByte, toJavaBoxedCharacter, toJavaBoxedDouble, toJavaBoxedFloat, toJavaBoxedInteger, toJavaBoxedLong, toJavaBoxedShort, toJavaByte, toJavaChar, toJavaDouble, toJavaFloat, toJavaInstance, toJavaInt, toJavaLong, toJavaShort, toString, toStrongReference
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

m_bytes

public byte[] m_bytes

m_offset

public int m_offset

m_length

public final int m_length

m_hash

public final int m_hash

LTYPENAMES

public static final LString[] LTYPENAMES
Constructor Detail

LString

public LString(java.lang.String string)
Construct a Lua string from the given Java string. Characters are encoded using UTF-8.


LString

public LString(byte[] bytes)
Construct a string from the given byte array. new LString(b) is identical to new LString(b, 0, b.length)


LString

public LString(byte[] bytes,
               int off,
               int len)
Construct a string from the given byte array and range. For efficiency, the byte array is not copied. Lua strings are immutable so the bytes must not be modified after the string is constructed.

Method Detail

toJavaString

public java.lang.String toJavaString()
Convert to Java string using UTF-8 encoding

Overrides:
toJavaString in class LValue

newStringCopy

public static LString newStringCopy(LString src)

newStringCopy

public static LString newStringCopy(byte[] buf,
                                    int off,
                                    int len)

newStringNoCopy

public static LString newStringNoCopy(byte[] buf,
                                      int off,
                                      int len)

lengthAsUtf8

public static int lengthAsUtf8(java.lang.String string)
Count the number of bytes required to encode the string as UTF-8.


encodeToUtf8

public static void encodeToUtf8(java.lang.String string,
                                byte[] bytes,
                                int startOffset)
Encode the given Java string as UTF-8 bytes, writing the result to bytes starting at offset. The string should be measured first with lengthAsUtf8 to make sure the given byte array is large enough.


isString

public boolean isString()
Description copied from class: LValue
Return true if this is a lua string, meaning it is either a LString or LNumber,since all numbers are convertible to strings in lua

Overrides:
isString in class LValue

equals

public boolean equals(java.lang.Object o)
Overrides:
equals in class java.lang.Object

compareTo

public int compareTo(LString o)

hashCode

public int hashCode()
Overrides:
hashCode in class java.lang.Object

length

public int length()

substring

public LString substring(int beginIndex,
                         int endIndex)

charAt

public int charAt(int index)

indexOfAny

public int indexOfAny(LString accept)
Java version of strpbrk, which is a terribly named C function.


indexOf

public int indexOf(byte b,
                   int start)

indexOf

public int indexOf(LString s,
                   int start)

lastIndexOf

public int lastIndexOf(LString s)

valueOf

public static LString valueOf(double d)

valueOf

public static LString valueOf(int x)

valueOf

public static LString valueOf(java.lang.String s)

write

public void write(java.io.OutputStream os,
                  int offset,
                  int len)
           throws java.io.IOException
Write the specified substring of this string to the given output stream.

Throws:
java.io.IOException

write

public void write(java.io.OutputStream os)
           throws java.io.IOException
Throws:
java.io.IOException

copyInto

public void copyInto(int strOffset,
                     byte[] bytes,
                     int arrayOffset,
                     int len)
Copy the bytes of the string into the given byte array.


toInputStream

public java.io.ByteArrayInputStream toInputStream()
Produce an InputStream instance from which the bytes of this LString can be read. Underlying byte array is not copied.


luaBinCmpUnknown

public boolean luaBinCmpUnknown(int opcode,
                                LValue lhs)
Overrides:
luaBinCmpUnknown in class LValue

luaBinCmpString

public boolean luaBinCmpString(int opcode,
                               LString rhs)
Overrides:
luaBinCmpString in class LValue

luaBinOpDouble

public LValue luaBinOpDouble(int opcode,
                             double m_value)
Overrides:
luaBinOpDouble in class LValue

luaBinOpInteger

public LValue luaBinOpInteger(int opcode,
                              int m_value)
Overrides:
luaBinOpInteger in class LValue

luaBinOpUnknown

public LValue luaBinOpUnknown(int opcode,
                              LValue lhs)
Overrides:
luaBinOpUnknown in class LValue

luaUnaryMinus

public LValue luaUnaryMinus()
Description copied from class: LValue
Arithmetic negative

Overrides:
luaUnaryMinus in class LValue

luaToNumber

public LValue luaToNumber()
Description copied from class: LValue
Convert to a number if possible, or nil otherwise

Overrides:
luaToNumber in class LValue

luaToNumber

public LValue luaToNumber(int base)

luaAsString

public LString luaAsString()
Description copied from class: LValue
Get the value as a LString

Overrides:
luaAsString in class LValue

luaLength

public int luaLength()
Built-in opcode LEN, for Strings and Tables

Overrides:
luaLength in class LValue

luaGetType

public int luaGetType()
Description copied from class: LValue
Valid for all types: return the int value identifying the type of this value

Specified by:
luaGetType in class LValue

luaGetMetatable

public LTable luaGetMetatable()
Description copied from class: LValue
Valid for all types: get a metatable. Only tables and userdata can have a different metatable per instance, though, other types are restricted to one metatable per type. Since metatables on non-tables can only be set through Java and not Lua, this function should be overridden for each value type as necessary.

Overrides:
luaGetMetatable in class LValue
Returns:
null if there is no meta-table

getMetatable

public static LTable getMetatable()
Get the metatable for all string values. Creates the table if it does not exist yet, and sets its __index entry to point to itself.

Returns:
metatable that will be used for all strings

equals

public static boolean equals(LString a,
                             int i,
                             LString b,
                             int j,
                             int n)

equals

public static boolean equals(byte[] a,
                             int i,
                             byte[] b,
                             int j,
                             int n)

luaByte

public int luaByte(int index)

luaConcatTo

public void luaConcatTo(java.io.ByteArrayOutputStream baos)
Description copied from class: LValue
Concatenate this value to a ByteArrayOutputStream

Overrides:
luaConcatTo in class LValue

isNumber

public boolean isNumber()
Returns true if this is or can be made into a number

Overrides:
isNumber in class LValue


Copyright © 2007-2013 Luaj.org. All Rights Reserved.