Class LuaString

All Implemented Interfaces:
com.esotericsoftware.kryo.KryoSerializable

public final class LuaString extends LuaValue implements com.esotericsoftware.kryo.KryoSerializable
Subclass of LuaValue for representing lua strings.

Because lua string values are more nearly sequences of bytes than sequences of characters or unicode code points, the LuaString implementation holds the string value in an internal byte array.

LuaString values are not considered mutable once constructed, so multiple LuaString values can chare a single byte array.

Currently LuaStrings are pooled via a centrally managed weak table. To ensure that as many string values as possible take advantage of this, Constructors are not exposed directly. As with number, booleans, and nil, instance construction should be via LuaValue.valueOf(byte[]) or similar API.

Because of this pooling, users of LuaString must not directly alter the bytes in a LuaString, or undefined behavior will result.

When Java Strings are used to initialize LuaString data, the UTF8 encoding is assumed. The functions lengthAsUtf8(char[]), encodeToUtf8(char[], int, byte[], int), and decodeAsUtf8(byte[], int, int) are used to convert back and forth between UTF8 byte arrays and character arrays.

See Also:
  • Field Details

    • s_metatable

      public static LuaValue s_metatable
      The singleton instance for string metatables that forwards to the string functions. Typically, this is set to the string metatable as a side effect of loading the string library, and is read-write to provide flexible behavior by default. When used in a server environment where there may be roge scripts, this should be replaced with a read-only table since it is shared across all lua code in this Java VM.
    • m_bytes

      public byte[] m_bytes
      The bytes for the string. These must not be mutated directly because the backing may be shared by multiple LuaStrings, and the hash code is computed only at construction time. It is exposed only for performance and legacy reasons.
    • m_offset

      public int m_offset
      The offset into the byte array, 0 means start at the first byte
    • m_length

      public int m_length
      The number of bytes that comprise this string
  • Method Details