Class LuaString


public final class LuaString extends LuaValue
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 final 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 final int m_offset
      The offset into the byte array, 0 means start at the first byte
    • m_length

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

    • valueOf

      public static LuaString valueOf(String string)
      Get a LuaString instance whose bytes match the supplied Java String using the UTF8 encoding.
      Parameters:
      string - Java String containing characters to encode as UTF8
      Returns:
      LuaString with UTF8 bytes corresponding to the supplied String
    • valueOf

      public static LuaString valueOf(byte[] bytes, int off, int len)
      Construct a LuaString for a portion of a byte array.

      The array is first be used as the backing for this object, so clients must not change contents. If the supplied value for 'len' is more than half the length of the container, the supplied byte array will be used as the backing, otherwise the bytes will be copied to a new byte array, and cache lookup may be performed.

      Parameters:
      bytes - byte buffer
      off - offset into the byte buffer
      len - length of the byte buffer
      Returns:
      LuaString wrapping the byte buffer
    • valueUsing

      public static LuaString valueUsing(byte[] bytes, int off, int len)
      Construct a LuaString around, possibly using the the supplied byte array as the backing store.

      The caller must ensure that the array is not mutated after the call. However, if the string is short enough the short-string cache is checked for a match which may be used instead of the supplied byte array.

      Parameters:
      bytes - byte buffer
      Returns:
      LuaString wrapping the byte buffer, or an equivalent string.
    • valueOf

      public static LuaString valueOf(char[] bytes)
      Construct a LuaString using the supplied characters as byte values.

      Only the low-order 8-bits of each character are used, the remainder is ignored.

      This is most useful for constructing byte sequences that do not conform to UTF8.

      Parameters:
      bytes - array of char, whose values are truncated at 8-bits each and put into a byte array.
      Returns:
      LuaString wrapping a copy of the byte buffer
    • valueOf

      public static LuaString valueOf(char[] bytes, int off, int len)
      Construct a LuaString using the supplied characters as byte values.

      Only the low-order 8-bits of each character are used, the remainder is ignored.

      This is most useful for constructing byte sequences that do not conform to UTF8.

      Parameters:
      bytes - array of char, whose values are truncated at 8-bits each and put into a byte array.
      Returns:
      LuaString wrapping a copy of the byte buffer
    • valueOf

      public static LuaString valueOf(byte[] bytes)
      Construct a LuaString for all the bytes in a byte array.

      The LuaString returned will either be a new LuaString containing a copy of the bytes array, or be an existing LuaString used already having the same value.

      Parameters:
      bytes - byte buffer
      Returns:
      LuaString wrapping the byte buffer
    • valueUsing

      public static LuaString valueUsing(byte[] bytes)
      Construct a LuaString for all the bytes in a byte array, possibly using the supplied array as the backing store.

      The LuaString returned will either be a new LuaString containing the byte array, or be an existing LuaString used already having the same value.

      The caller must not mutate the contents of the byte array after this call, as it may be used elsewhere due to recent short string caching.

      Parameters:
      bytes - byte buffer
      Returns:
      LuaString wrapping the byte buffer
    • isstring

      public boolean isstring()
      Description copied from class: LuaValue
      Check if this is a string
      Overrides:
      isstring in class LuaValue
      Returns:
      true if this is a string, meaning derives from LuaString or LuaNumber, otherwise false
      See Also:
    • getmetatable

      public LuaValue getmetatable()
      Description copied from class: LuaValue
      Get the metatable for this LuaValue

      For LuaTable and LuaUserdata instances, the metatable returned is this instance metatable. For all other types, the class metatable value will be returned.

      Overrides:
      getmetatable in class LuaValue
      Returns:
      metatable, or null if it there is none
    • type

      public int type()
      Description copied from class: LuaValue
      Get the enumeration value for the type of this value.
      Specified by:
      type in class LuaValue
      Returns:
      value for this type, one of LuaValue.TNIL, LuaValue.TBOOLEAN, LuaValue.TNUMBER, LuaValue.TSTRING, LuaValue.TTABLE, LuaValue.TFUNCTION, LuaValue.TUSERDATA, LuaValue.TTHREAD
      See Also:
    • typename

      public String typename()
      Description copied from class: LuaValue
      Get the String name of the type of this value.

      Specified by:
      typename in class LuaValue
      Returns:
      name from type name list LuaValue.TYPE_NAMES corresponding to the type of this value: "nil", "boolean", "number", "string", "table", "function", "userdata", "thread"
      See Also:
    • tojstring

      public String tojstring()
      Description copied from class: LuaValue
      Convert to human readable String for any type.
      Overrides:
      tojstring in class LuaValue
      Returns:
      String for use by human readers based on type.
      See Also:
    • neg

      public LuaValue neg()
      Description copied from class: LuaValue
      Unary minus: return negative value (-this) as defined by lua unary minus operator
      Overrides:
      neg in class LuaValue
      Returns:
      boolean inverse as LuaBoolean if boolean or nil, numeric inverse as LuaNumber if numeric, or metatag processing result if LuaValue.UNM metatag is defined
    • add

      public LuaValue add(LuaValue rhs)
      Description copied from class: LuaValue
      Add: Perform numeric add operation with another value including metatag processing.

      Each operand must derive from LuaNumber or derive from LuaString and be convertible to a number

      Overrides:
      add in class LuaValue
      Parameters:
      rhs - The right-hand-side value to perform the add with
      Returns:
      value of (this + rhs) if both are numeric, or LuaValue if metatag processing occurs
      See Also:
      • LuaValue.arithmt(LuaValue, LuaValue)
    • add

      public LuaValue add(double rhs)
      Description copied from class: LuaValue
      Add: Perform numeric add operation with another value of double type with metatag processing

      this must derive from LuaNumber or derive from LuaString and be convertible to a number

      Overrides:
      add in class LuaValue
      Parameters:
      rhs - The right-hand-side value to perform the add with
      Returns:
      value of (this + rhs) if this is numeric
      See Also:
    • add

      public LuaValue add(int rhs)
      Description copied from class: LuaValue
      Add: Perform numeric add operation with another value of int type with metatag processing

      this must derive from LuaNumber or derive from LuaString and be convertible to a number

      Overrides:
      add in class LuaValue
      Parameters:
      rhs - The right-hand-side value to perform the add with
      Returns:
      value of (this + rhs) if this is numeric
      See Also:
    • sub

      public LuaValue sub(LuaValue rhs)
      Description copied from class: LuaValue
      Subtract: Perform numeric subtract operation with another value of unknown type, including metatag processing.

      Each operand must derive from LuaNumber or derive from LuaString and be convertible to a number

      Overrides:
      sub in class LuaValue
      Parameters:
      rhs - The right-hand-side value to perform the subtract with
      Returns:
      value of (this - rhs) if both are numeric, or LuaValue if metatag processing occurs
      See Also:
      • LuaValue.arithmt(LuaValue, LuaValue)
    • sub

      public LuaValue sub(double rhs)
      Description copied from class: LuaValue
      Subtract: Perform numeric subtract operation with another value of double type with metatag processing

      this must derive from LuaNumber or derive from LuaString and be convertible to a number

      Overrides:
      sub in class LuaValue
      Parameters:
      rhs - The right-hand-side value to perform the subtract with
      Returns:
      value of (this - rhs) if this is numeric
      See Also:
    • sub

      public LuaValue sub(int rhs)
      Description copied from class: LuaValue
      Subtract: Perform numeric subtract operation with another value of int type with metatag processing

      this must derive from LuaNumber or derive from LuaString and be convertible to a number

      Overrides:
      sub in class LuaValue
      Parameters:
      rhs - The right-hand-side value to perform the subtract with
      Returns:
      value of (this - rhs) if this is numeric
      See Also:
    • subFrom

      public LuaValue subFrom(double lhs)
      Description copied from class: LuaValue
      Reverse-subtract: Perform numeric subtract operation from an int value with metatag processing

      this must derive from LuaNumber or derive from LuaString and be convertible to a number

      Overrides:
      subFrom in class LuaValue
      Parameters:
      lhs - The left-hand-side value from which to perform the subtraction
      Returns:
      value of (lhs - this) if this is numeric
      See Also:
    • mul

      public LuaValue mul(LuaValue rhs)
      Description copied from class: LuaValue
      Multiply: Perform numeric multiply operation with another value of unknown type, including metatag processing.

      Each operand must derive from LuaNumber or derive from LuaString and be convertible to a number

      Overrides:
      mul in class LuaValue
      Parameters:
      rhs - The right-hand-side value to perform the multiply with
      Returns:
      value of (this * rhs) if both are numeric, or LuaValue if metatag processing occurs
      See Also:
      • LuaValue.arithmt(LuaValue, LuaValue)
    • mul

      public LuaValue mul(double rhs)
      Description copied from class: LuaValue
      Multiply: Perform numeric multiply operation with another value of double type with metatag processing

      this must derive from LuaNumber or derive from LuaString and be convertible to a number

      Overrides:
      mul in class LuaValue
      Parameters:
      rhs - The right-hand-side value to perform the multiply with
      Returns:
      value of (this * rhs) if this is numeric
    • mul

      public LuaValue mul(int rhs)
      Description copied from class: LuaValue
      Multiply: Perform numeric multiply operation with another value of int type with metatag processing

      this must derive from LuaNumber or derive from LuaString and be convertible to a number

      Overrides:
      mul in class LuaValue
      Parameters:
      rhs - The right-hand-side value to perform the multiply with
      Returns:
      value of (this * rhs) if this is numeric
    • pow

      public LuaValue pow(LuaValue rhs)
      Description copied from class: LuaValue
      Raise to power: Raise this value to a power including metatag processing.

      Each operand must derive from LuaNumber or derive from LuaString and be convertible to a number

      Overrides:
      pow in class LuaValue
      Parameters:
      rhs - The power to raise this value to
      Returns:
      value of (this ^ rhs) if both are numeric, or LuaValue if metatag processing occurs
      See Also:
      • LuaValue.arithmt(LuaValue, LuaValue)
    • pow

      public LuaValue pow(double rhs)
      Description copied from class: LuaValue
      Raise to power: Raise this value to a power of double type with metatag processing

      this must derive from LuaNumber or derive from LuaString and be convertible to a number

      Overrides:
      pow in class LuaValue
      Parameters:
      rhs - The power to raise this value to
      Returns:
      value of (this ^ rhs) if this is numeric
      See Also:
    • pow

      public LuaValue pow(int rhs)
      Description copied from class: LuaValue
      Raise to power: Raise this value to a power of int type with metatag processing

      this must derive from LuaNumber or derive from LuaString and be convertible to a number

      Overrides:
      pow in class LuaValue
      Parameters:
      rhs - The power to raise this value to
      Returns:
      value of (this ^ rhs) if this is numeric
      See Also:
    • powWith

      public LuaValue powWith(double lhs)
      Description copied from class: LuaValue
      Reverse-raise to power: Raise another value of double type to this power with metatag processing

      this must derive from LuaNumber or derive from LuaString and be convertible to a number

      Overrides:
      powWith in class LuaValue
      Parameters:
      lhs - The left-hand-side value which will be raised to this power
      Returns:
      value of (lhs ^ this) if this is numeric
      See Also:
    • powWith

      public LuaValue powWith(int lhs)
      Description copied from class: LuaValue
      Reverse-raise to power: Raise another value of double type to this power with metatag processing

      this must derive from LuaNumber or derive from LuaString and be convertible to a number

      Overrides:
      powWith in class LuaValue
      Parameters:
      lhs - The left-hand-side value which will be raised to this power
      Returns:
      value of (lhs ^ this) if this is numeric
      See Also:
    • div

      public LuaValue div(LuaValue rhs)
      Description copied from class: LuaValue
      Divide: Perform numeric divide operation by another value of unknown type, including metatag processing.

      Each operand must derive from LuaNumber or derive from LuaString and be convertible to a number

      Overrides:
      div in class LuaValue
      Parameters:
      rhs - The right-hand-side value to perform the divulo with
      Returns:
      value of (this / rhs) if both are numeric, or LuaValue if metatag processing occurs
      See Also:
      • LuaValue.arithmt(LuaValue, LuaValue)
    • div

      public LuaValue div(double rhs)
      Description copied from class: LuaValue
      Divide: Perform numeric divide operation by another value of double type without metatag processing

      this must derive from LuaNumber or derive from LuaString and be convertible to a number

      For metatag processing LuaValue.div(LuaValue) must be used

      Overrides:
      div in class LuaValue
      Parameters:
      rhs - The right-hand-side value to perform the divulo with
      Returns:
      value of (this / rhs) if this is numeric
      See Also:
    • div

      public LuaValue div(int rhs)
      Description copied from class: LuaValue
      Divide: Perform numeric divide operation by another value of int type without metatag processing

      this must derive from LuaNumber or derive from LuaString and be convertible to a number

      For metatag processing LuaValue.div(LuaValue) must be used

      Overrides:
      div in class LuaValue
      Parameters:
      rhs - The right-hand-side value to perform the divulo with
      Returns:
      value of (this / rhs) if this is numeric
      See Also:
    • divInto

      public LuaValue divInto(double lhs)
      Description copied from class: LuaValue
      Reverse-divide: Perform numeric divide operation into another value with metatag processing

      this must derive from LuaNumber or derive from LuaString and be convertible to a number

      Overrides:
      divInto in class LuaValue
      Parameters:
      lhs - The left-hand-side value which will be divided by this
      Returns:
      value of (lhs / this) if this is numeric
      See Also:
    • mod

      public LuaValue mod(LuaValue rhs)
      Description copied from class: LuaValue
      Modulo: Perform numeric modulo operation with another value of unknown type, including metatag processing.

      Each operand must derive from LuaNumber or derive from LuaString and be convertible to a number

      Overrides:
      mod in class LuaValue
      Parameters:
      rhs - The right-hand-side value to perform the modulo with
      Returns:
      value of (this % rhs) if both are numeric, or LuaValue if metatag processing occurs
      See Also:
      • LuaValue.arithmt(LuaValue, LuaValue)
    • mod

      public LuaValue mod(double rhs)
      Description copied from class: LuaValue
      Modulo: Perform numeric modulo operation with another value of double type without metatag processing

      this must derive from LuaNumber or derive from LuaString and be convertible to a number

      For metatag processing LuaValue.mod(LuaValue) must be used

      Overrides:
      mod in class LuaValue
      Parameters:
      rhs - The right-hand-side value to perform the modulo with
      Returns:
      value of (this % rhs) if this is numeric
      See Also:
    • mod

      public LuaValue mod(int rhs)
      Description copied from class: LuaValue
      Modulo: Perform numeric modulo operation with another value of int type without metatag processing

      this must derive from LuaNumber or derive from LuaString and be convertible to a number

      For metatag processing LuaValue.mod(LuaValue) must be used

      Overrides:
      mod in class LuaValue
      Parameters:
      rhs - The right-hand-side value to perform the modulo with
      Returns:
      value of (this % rhs) if this is numeric
      See Also:
    • modFrom

      public LuaValue modFrom(double lhs)
      Description copied from class: LuaValue
      Reverse-modulo: Perform numeric modulo operation from another value with metatag processing

      this must derive from LuaNumber or derive from LuaString and be convertible to a number

      Overrides:
      modFrom in class LuaValue
      Parameters:
      lhs - The left-hand-side value which will be modulo'ed by this
      Returns:
      value of (lhs % this) if this is numeric
      See Also:
    • lt

      public LuaValue lt(LuaValue rhs)
      Description copied from class: LuaValue
      Less than: Perform numeric or string comparison with another value of unknown type, including metatag processing, and returning LuaValue.

      To be comparable, both operands must derive from LuaString or both must derive from LuaNumber.

      Overrides:
      lt in class LuaValue
      Parameters:
      rhs - The right-hand-side value to perform the comparison with
      Returns:
      LuaValue.TRUE if (this < rhs), LuaValue.FALSE if not, or LuaValue if metatag processing occurs
      See Also:
    • lt_b

      public boolean lt_b(LuaValue rhs)
      Description copied from class: LuaValue
      Less than: Perform numeric or string comparison with another value of unknown type, including metatag processing, and returning java boolean.

      To be comparable, both operands must derive from LuaString or both must derive from LuaNumber.

      Overrides:
      lt_b in class LuaValue
      Parameters:
      rhs - The right-hand-side value to perform the comparison with
      Returns:
      true if (this < rhs), false if not, and boolean interpreation of result if metatag processing occurs.
      See Also:
    • lt_b

      public boolean lt_b(int rhs)
      Description copied from class: LuaValue
      Less than: Perform numeric comparison with another value of int type, including metatag processing, and returning java boolean.

      To be comparable, this must derive from LuaNumber.

      Overrides:
      lt_b in class LuaValue
      Parameters:
      rhs - The right-hand-side value to perform the comparison with
      Returns:
      true if (this < rhs), false if not, and boolean interpreation of result if metatag processing occurs.
      See Also:
    • lt_b

      public boolean lt_b(double rhs)
      Description copied from class: LuaValue
      Less than: Perform numeric or string comparison with another value of unknown type, including metatag processing, and returning java boolean.

      To be comparable, both operands must derive from LuaString or both must derive from LuaNumber.

      Overrides:
      lt_b in class LuaValue
      Parameters:
      rhs - The right-hand-side value to perform the comparison with
      Returns:
      true if (this < rhs), false if not, and boolean interpreation of result if metatag processing occurs.
      See Also:
    • lteq

      public LuaValue lteq(LuaValue rhs)
      Description copied from class: LuaValue
      Less than or equals: Perform numeric or string comparison with another value of unknown type, including metatag processing, and returning LuaValue.

      To be comparable, both operands must derive from LuaString or both must derive from LuaNumber.

      Overrides:
      lteq in class LuaValue
      Parameters:
      rhs - The right-hand-side value to perform the comparison with
      Returns:
      LuaValue.TRUE if (this <= rhs), LuaValue.FALSE if not, or LuaValue if metatag processing occurs
      See Also:
    • lteq_b

      public boolean lteq_b(LuaValue rhs)
      Description copied from class: LuaValue
      Less than or equals: Perform numeric or string comparison with another value of unknown type, including metatag processing, and returning java boolean.

      To be comparable, both operands must derive from LuaString or both must derive from LuaNumber.

      Overrides:
      lteq_b in class LuaValue
      Parameters:
      rhs - The right-hand-side value to perform the comparison with
      Returns:
      true if (this <= rhs), false if not, and boolean interpreation of result if metatag processing occurs.
      See Also:
    • lteq_b

      public boolean lteq_b(int rhs)
      Description copied from class: LuaValue
      Less than or equals: Perform numeric comparison with another value of int type, including metatag processing, and returning java boolean.

      To be comparable, this must derive from LuaNumber.

      Overrides:
      lteq_b in class LuaValue
      Parameters:
      rhs - The right-hand-side value to perform the comparison with
      Returns:
      true if (this <= rhs), false if not, and boolean interpreation of result if metatag processing occurs.
      See Also:
    • lteq_b

      public boolean lteq_b(double rhs)
      Description copied from class: LuaValue
      Less than or equals: Perform numeric comparison with another value of double type, including metatag processing, and returning java boolean.

      To be comparable, this must derive from LuaNumber.

      Overrides:
      lteq_b in class LuaValue
      Parameters:
      rhs - The right-hand-side value to perform the comparison with
      Returns:
      true if (this <= rhs), false if not, and boolean interpreation of result if metatag processing occurs.
      See Also:
    • gt

      public LuaValue gt(LuaValue rhs)
      Description copied from class: LuaValue
      Greater than: Perform numeric or string comparison with another value of unknown type, including metatag processing, and returning LuaValue.

      To be comparable, both operands must derive from LuaString or both must derive from LuaNumber.

      Overrides:
      gt in class LuaValue
      Parameters:
      rhs - The right-hand-side value to perform the comparison with
      Returns:
      LuaValue.TRUE if (this > rhs), LuaValue.FALSE if not, or LuaValue if metatag processing occurs
      See Also:
    • gt_b

      public boolean gt_b(LuaValue rhs)
      Description copied from class: LuaValue
      Greater than: Perform numeric or string comparison with another value of unknown type, including metatag processing, and returning java boolean.

      To be comparable, both operands must derive from LuaString or both must derive from LuaNumber.

      Overrides:
      gt_b in class LuaValue
      Parameters:
      rhs - The right-hand-side value to perform the comparison with
      Returns:
      true if (this > rhs), false if not, and boolean interpreation of result if metatag processing occurs.
      See Also:
    • gt_b

      public boolean gt_b(int rhs)
      Description copied from class: LuaValue
      Greater than: Perform numeric comparison with another value of int type, including metatag processing, and returning java boolean.

      To be comparable, this must derive from LuaNumber.

      Overrides:
      gt_b in class LuaValue
      Parameters:
      rhs - The right-hand-side value to perform the comparison with
      Returns:
      true if (this > rhs), false if not, and boolean interpreation of result if metatag processing occurs.
      See Also:
    • gt_b

      public boolean gt_b(double rhs)
      Description copied from class: LuaValue
      Greater than: Perform numeric or string comparison with another value of unknown type, including metatag processing, and returning java boolean.

      To be comparable, both operands must derive from LuaString or both must derive from LuaNumber.

      Overrides:
      gt_b in class LuaValue
      Parameters:
      rhs - The right-hand-side value to perform the comparison with
      Returns:
      true if (this > rhs), false if not, and boolean interpreation of result if metatag processing occurs.
      See Also:
    • gteq

      public LuaValue gteq(LuaValue rhs)
      Description copied from class: LuaValue
      Greater than or equals: Perform numeric or string comparison with another value of unknown type, including metatag processing, and returning LuaValue.

      To be comparable, both operands must derive from LuaString or both must derive from LuaNumber.

      Overrides:
      gteq in class LuaValue
      Parameters:
      rhs - The right-hand-side value to perform the comparison with
      Returns:
      LuaValue.TRUE if (this >= rhs), LuaValue.FALSE if not, or LuaValue if metatag processing occurs
      See Also:
    • gteq_b

      public boolean gteq_b(LuaValue rhs)
      Description copied from class: LuaValue
      Greater than or equals: Perform numeric or string comparison with another value of unknown type, including metatag processing, and returning java boolean.

      To be comparable, both operands must derive from LuaString or both must derive from LuaNumber.

      Overrides:
      gteq_b in class LuaValue
      Parameters:
      rhs - The right-hand-side value to perform the comparison with
      Returns:
      true if (this >= rhs), false if not, and boolean interpreation of result if metatag processing occurs.
      See Also:
    • gteq_b

      public boolean gteq_b(int rhs)
      Description copied from class: LuaValue
      Greater than or equals: Perform numeric comparison with another value of int type, including metatag processing, and returning java boolean.

      To be comparable, this must derive from LuaNumber.

      Overrides:
      gteq_b in class LuaValue
      Parameters:
      rhs - The right-hand-side value to perform the comparison with
      Returns:
      true if (this >= rhs), false if not, and boolean interpreation of result if metatag processing occurs.
      See Also:
    • gteq_b

      public boolean gteq_b(double rhs)
      Description copied from class: LuaValue
      Greater than or equals: Perform numeric comparison with another value of double type, including metatag processing, and returning java boolean.

      To be comparable, this must derive from LuaNumber.

      Overrides:
      gteq_b in class LuaValue
      Parameters:
      rhs - The right-hand-side value to perform the comparison with
      Returns:
      true if (this >= rhs), false if not, and boolean interpreation of result if metatag processing occurs.
      See Also:
    • concat

      public LuaValue concat(LuaValue rhs)
      Description copied from class: LuaValue
      Concatenate another value onto this value and return the result using rules of lua string concatenation including metatag processing.

      Only strings and numbers as represented can be concatenated, meaning each operand must derive from LuaString or LuaNumber.

      Overrides:
      concat in class LuaValue
      Parameters:
      rhs - The right-hand-side value to perform the operation with
      Returns:
      LuaValue resulting from concatenation of (this .. rhs)
    • concat

      public Buffer concat(Buffer rhs)
      Description copied from class: LuaValue
      Concatenate a Buffer onto this value and return the result using rules of lua string concatenation including metatag processing.

      Only strings and numbers as represented can be concatenated, meaning each operand must derive from LuaString or LuaNumber.

      Overrides:
      concat in class LuaValue
      Parameters:
      rhs - The right-hand-side Buffer to perform the operation with
      Returns:
      LuaString resulting from concatenation of (this .. rhs)
    • concatTo

      public LuaValue concatTo(LuaNumber lhs)
      Description copied from class: LuaValue
      Reverse-concatenation: concatenate this value onto another value known to be a LuaNumber and return the result using rules of lua string concatenation including metatag processing.

      Only strings and numbers as represented can be concatenated, meaning each operand must derive from LuaString or LuaNumber.

      Overrides:
      concatTo in class LuaValue
      Parameters:
      lhs - The left-hand-side value onto which this will be concatenated
      Returns:
      LuaValue resulting from concatenation of (lhs .. this)
      See Also:
    • concatTo

      public LuaValue concatTo(LuaString lhs)
      Description copied from class: LuaValue
      Reverse-concatenation: concatenate this value onto another value known to be a LuaString and return the result using rules of lua string concatenation including metatag processing.

      Only strings and numbers as represented can be concatenated, meaning each operand must derive from LuaString or LuaNumber.

      Overrides:
      concatTo in class LuaValue
      Parameters:
      lhs - The left-hand-side value onto which this will be concatenated
      Returns:
      LuaValue resulting from concatenation of (lhs .. this)
      See Also:
    • strcmp

      public int strcmp(LuaValue lhs)
      Description copied from class: LuaValue
      Perform string comparison with another value of any type using string comparison based on byte values.

      Only strings can be compared, meaning each operand must derive from LuaString.

      Overrides:
      strcmp in class LuaValue
      Parameters:
      lhs - The right-hand-side value to perform the comparison with
      Returns:
      int < 0 for (this &lt; rhs), int > 0 for (this &gt; rhs), or 0 when same string.
    • strcmp

      public int strcmp(LuaString rhs)
      Description copied from class: LuaValue
      Perform string comparison with another value known to be a LuaString using string comparison based on byte values.

      Only strings can be compared, meaning each operand must derive from LuaString.

      Overrides:
      strcmp in class LuaValue
      Parameters:
      rhs - The right-hand-side value to perform the comparison with
      Returns:
      int < 0 for (this &lt; rhs), int > 0 for (this &gt; rhs), or 0 when same string.
    • checkint

      public int checkint()
      Description copied from class: LuaValue
      Check that the value is numeric, and convert and cast value to int, or throw LuaError if not numeric

      Values that are LuaNumber will be cast to int and may lose precision. Values that are LuaString that can be converted to a number will be converted, then cast to int, so may also lose precision.

      Overrides:
      checkint in class LuaValue
      Returns:
      value cast to a int if numeric
      See Also:
    • checklong

      public long checklong()
      Description copied from class: LuaValue
      Check that the value is numeric, and convert and cast value to long, or throw LuaError if not numeric

      Values that are LuaNumber will be cast to long and may lose precision. Values that are LuaString that can be converted to a number will be converted, then cast to long, so may also lose precision.

      Overrides:
      checklong in class LuaValue
      Returns:
      value cast to a long if numeric
      See Also:
    • checkdouble

      public double checkdouble()
      Description copied from class: LuaValue
      Check that the value is numeric and return the value as a double, or throw LuaError if not numeric

      Values that are LuaNumber and values that are LuaString that can be converted to a number will be converted to double.

      Overrides:
      checkdouble in class LuaValue
      Returns:
      value cast to a double if numeric
      See Also:
    • checknumber

      public LuaNumber checknumber()
      Description copied from class: LuaValue
      Check that the value is numeric, and return as a LuaNumber if so, or throw LuaError

      Values that are LuaString that can be converted to a number will be converted and returned.

      Overrides:
      checknumber in class LuaValue
      Returns:
      value as a LuaNumber if numeric
      See Also:
    • checknumber

      public LuaNumber checknumber(String msg)
      Description copied from class: LuaValue
      Check that the value is numeric, and return as a LuaNumber if so, or throw LuaError

      Values that are LuaString that can be converted to a number will be converted and returned.

      Overrides:
      checknumber in class LuaValue
      Parameters:
      msg - String message to supply if conversion fails
      Returns:
      value as a LuaNumber if numeric
      See Also:
    • isnumber

      public boolean isnumber()
      Description copied from class: LuaValue
      Check if this is a number
      Overrides:
      isnumber in class LuaValue
      Returns:
      true if this is a number, meaning derives from LuaNumber or derives from LuaString and is convertible to a number, otherwise false
      See Also:
    • isint

      public boolean isint()
      Description copied from class: LuaValue
      Check if this is a number and is representable by java int without rounding or truncation
      Overrides:
      isint in class LuaValue
      Returns:
      true if this is a number meaning derives from LuaNumber or derives from LuaString and is convertible to a number, and can be represented by int, otherwise false
      See Also:
    • islong

      public boolean islong()
      Description copied from class: LuaValue
      Check if this is a number and is representable by java long without rounding or truncation
      Overrides:
      islong in class LuaValue
      Returns:
      true if this is a number meaning derives from LuaNumber or derives from LuaString and is convertible to a number, and can be represented by long, otherwise false
      See Also:
    • tobyte

      public byte tobyte()
      Description copied from class: LuaValue
      Convert to byte if numeric, or 0 if not.
      Overrides:
      tobyte in class LuaValue
      Returns:
      Value cast to byte if number or string convertible to number, otherwise 0
      See Also:
    • tochar

      public char tochar()
      Description copied from class: LuaValue
      Convert to char if numeric, or 0 if not.
      Overrides:
      tochar in class LuaValue
      Returns:
      Value cast to char if number or string convertible to number, otherwise 0
      See Also:
    • todouble

      public double todouble()
      Description copied from class: LuaValue
      Convert to double if numeric, or 0 if not.
      Overrides:
      todouble in class LuaValue
      Returns:
      Value cast to double if number or string convertible to number, otherwise 0
      See Also:
    • tofloat

      public float tofloat()
      Description copied from class: LuaValue
      Convert to float if numeric, or 0 if not.
      Overrides:
      tofloat in class LuaValue
      Returns:
      Value cast to float if number or string convertible to number, otherwise 0
      See Also:
    • toint

      public int toint()
      Description copied from class: LuaValue
      Convert to int if numeric, or 0 if not.
      Overrides:
      toint in class LuaValue
      Returns:
      Value cast to int if number or string convertible to number, otherwise 0
      See Also:
    • tolong

      public long tolong()
      Description copied from class: LuaValue
      Convert to long if numeric, or 0 if not.
      Overrides:
      tolong in class LuaValue
      Returns:
      Value cast to long if number or string convertible to number, otherwise 0
      See Also:
    • toshort

      public short toshort()
      Description copied from class: LuaValue
      Convert to short if numeric, or 0 if not.
      Overrides:
      toshort in class LuaValue
      Returns:
      Value cast to short if number or string convertible to number, otherwise 0
      See Also:
    • optdouble

      public double optdouble(double defval)
      Description copied from class: LuaValue
      Check that optional argument is a number or string convertible to number and return as double
      Overrides:
      optdouble in class LuaValue
      Parameters:
      defval - double to return if this is nil or none
      Returns:
      this cast to double if numeric, defval if nil or none, throws LuaError otherwise
      See Also:
    • optint

      public int optint(int defval)
      Description copied from class: LuaValue
      Check that optional argument is a number or string convertible to number and return as int
      Overrides:
      optint in class LuaValue
      Parameters:
      defval - int to return if this is nil or none
      Returns:
      this cast to int if numeric, defval if nil or none, throws LuaError otherwise
      See Also:
    • optlong

      public long optlong(long defval)
      Description copied from class: LuaValue
      Check that optional argument is a number or string convertible to number and return as long
      Overrides:
      optlong in class LuaValue
      Parameters:
      defval - long to return if this is nil or none
      Returns:
      this cast to long if numeric, defval if nil or none, throws LuaError otherwise
      See Also:
    • optnumber

      public LuaNumber optnumber(LuaNumber defval)
      Description copied from class: LuaValue
      Check that optional argument is a number or string convertible to number and return as LuaNumber
      Overrides:
      optnumber in class LuaValue
      Parameters:
      defval - LuaNumber to return if this is nil or none
      Returns:
      this cast to LuaNumber if numeric, defval if nil or none, throws LuaError otherwise
      See Also:
    • optstring

      public LuaString optstring(LuaString defval)
      Description copied from class: LuaValue
      Check that optional argument is a string or number and return as LuaString
      Overrides:
      optstring in class LuaValue
      Parameters:
      defval - LuaString to return if this is nil or none
      Returns:
      this converted to LuaString if a string or number, defval if nil or none, throws LuaError if some other type
      See Also:
    • tostring

      public LuaValue tostring()
      Description copied from class: LuaValue
      Conditionally convert to lua string without throwing errors.

      In lua all numbers are strings, so this function will return the LuaValue this if it is a string or number, and LuaValue.NIL for all other cases.

      This allows values to be tested for their "string-ness" without the penalty of throwing exceptions.

      Overrides:
      tostring in class LuaValue
      Returns:
      this if it is a LuaString or LuaNumber, otherwise LuaValue.NIL
      See Also:
    • optjstring

      public String optjstring(String defval)
      Description copied from class: LuaValue
      Check that optional argument is a string or number and return as Java String
      Overrides:
      optjstring in class LuaValue
      Parameters:
      defval - LuaString to return if this is nil or none
      Returns:
      this converted to String if a string or number, defval if nil or none, throws LuaError if some other type
      See Also:
    • strvalue

      public LuaString strvalue()
      Description copied from class: LuaValue
      Convert this value to a string if it is a LuaString or LuaNumber, or throw a LuaError if it is not
      Overrides:
      strvalue in class LuaValue
      Returns:
      LuaString corresponding to the value if a string or number
    • substring

      public LuaString substring(int beginIndex, int endIndex)
      Take a substring using Java zero-based indexes for begin and end or range.
      Parameters:
      beginIndex - The zero-based index of the first character to include.
      endIndex - The zero-based index of position after the last character.
      Returns:
      LuaString which is a substring whose first character is at offset beginIndex and extending for (endIndex - beginIndex ) characters.
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • hashCode

      public static int hashCode(byte[] bytes, int offset, int length)
      Compute the hash code of a sequence of bytes within a byte array using lua's rules for string hashes. For long strings, not all bytes are hashed.
      Parameters:
      bytes - byte array containing the bytes.
      offset - offset into the hash for the first byte.
      length - number of bytes starting with offset that are part of the string.
      Returns:
      hash for the string defined by bytes, offset, and length.
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class LuaValue
    • eq

      public LuaValue eq(LuaValue val)
      Description copied from class: LuaValue
      Equals: Perform equality comparison with another value including metatag processing using LuaValue.EQ.
      Overrides:
      eq in class LuaValue
      Parameters:
      val - The value to compare with.
      Returns:
      LuaValue.TRUE if values are comparable and (this == rhs), LuaValue.FALSE if comparable but not equal, LuaValue if metatag processing occurs.
      See Also:
    • eq_b

      public boolean eq_b(LuaValue val)
      Description copied from class: LuaValue
      Equals: Perform equality comparison with another value including metatag processing using LuaValue.EQ, and return java boolean
      Overrides:
      eq_b in class LuaValue
      Parameters:
      val - The value to compare with.
      Returns:
      true if values are comparable and (this == rhs), false if comparable but not equal, result converted to java boolean if metatag processing occurs.
      See Also:
    • raweq

      public boolean raweq(LuaValue val)
      Description copied from class: LuaValue
      Equals: Perform direct equality comparison with another value without metatag processing.
      Overrides:
      raweq in class LuaValue
      Parameters:
      val - The value to compare with.
      Returns:
      true if (this == rhs), false otherwise
      See Also:
    • raweq

      public boolean raweq(LuaString s)
      Description copied from class: LuaValue
      Equals: Perform direct equality comparison with a LuaString value without metatag processing.
      Overrides:
      raweq in class LuaValue
      Parameters:
      s - The LuaString to compare with.
      Returns:
      true if this is a LuaString and their byte sequences match, otherwise false
    • equals

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

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

      public void write(DataOutputStream writer, int i, int len) throws IOException
      Throws:
      IOException
    • len

      public LuaValue len()
      Description copied from class: LuaValue
      Length operator: return lua length of object (#this) including metatag processing as java int
      Overrides:
      len in class LuaValue
      Returns:
      length as defined by the lua # operator or metatag processing result
    • length

      public int length()
      Description copied from class: LuaValue
      Length operator: return lua length of object (#this) including metatag processing as java int
      Overrides:
      length in class LuaValue
      Returns:
      length as defined by the lua # operator or metatag processing result converted to java int using LuaValue.toint()
    • rawlen

      public int rawlen()
      Description copied from class: LuaValue
      Get raw length of table or string without metatag processing.
      Overrides:
      rawlen in class LuaValue
      Returns:
      the length of the table or string.
    • luaByte

      public int luaByte(int index)
    • charAt

      public int charAt(int index)
    • checkjstring

      public String checkjstring()
      Description copied from class: LuaValue
      Convert this value to a Java String.

      The string representations here will roughly match what is produced by the C lua distribution, however hash codes have no relationship, and there may be differences in number formatting.

      Overrides:
      checkjstring in class LuaValue
      Returns:
      String representation of the value
      See Also:
    • checkstring

      public LuaString checkstring()
      Description copied from class: LuaValue
      Check that this is a lua string, or throw LuaError if it is not.

      In lua all numbers are strings, so this will succeed for anything that derives from LuaString or LuaNumber. Numbers will be converted to LuaString.

      Overrides:
      checkstring in class LuaValue
      Returns:
      LuaString representation of the value if it is a LuaString or LuaNumber
      See Also:
    • toInputStream

      public InputStream toInputStream()
      Convert value to an input stream.
      Returns:
      InputStream whose data matches the bytes in this LuaString
    • copyInto

      public void copyInto(int strOffset, byte[] bytes, int arrayOffset, int len)
      Copy the bytes of the string into the given byte array.
      Parameters:
      strOffset - offset from which to copy
      bytes - destination byte array
      arrayOffset - offset in destination
      len - number of bytes to copy
    • indexOfAny

      public int indexOfAny(LuaString accept)
      Java version of strpbrk - find index of any byte that in an accept string.
      Parameters:
      accept - LuaString containing characters to look for.
      Returns:
      index of first match in the accept string, or -1 if not found.
    • indexOf

      public int indexOf(byte b, int start)
      Find the index of a byte starting at a point in this string
      Parameters:
      b - the byte to look for
      start - the first index in the string
      Returns:
      index of first match found, or -1 if not found.
    • indexOf

      public int indexOf(LuaString s, int start)
      Find the index of a string starting at a point in this string
      Parameters:
      s - the string to search for
      start - the first index in the string
      Returns:
      index of first match found, or -1 if not found.
    • lastIndexOf

      public int lastIndexOf(LuaString s)
      Find the last index of a string in this string
      Parameters:
      s - the string to search for
      Returns:
      index of last match found, or -1 if not found.
    • decodeAsUtf8

      public static String decodeAsUtf8(byte[] bytes, int offset, int length)
      Convert to Java String interpreting as utf8 characters.
      Parameters:
      bytes - byte array in UTF8 encoding to convert
      offset - starting index in byte array
      length - number of bytes to convert
      Returns:
      Java String corresponding to the value of bytes interpreted using UTF8
      See Also:
    • lengthAsUtf8

      public static int lengthAsUtf8(char[] chars)
      Count the number of bytes required to encode the string as UTF-8.
      Parameters:
      chars - Array of unicode characters to be encoded as UTF-8
      Returns:
      count of bytes needed to encode using UTF-8
      See Also:
    • encodeToUtf8

      public static int encodeToUtf8(char[] chars, int nchars, byte[] bytes, int off)
      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.

      Parameters:
      chars - Array of unicode characters to be encoded as UTF-8
      nchars - Number of characters in the array to convert.
      bytes - byte array to hold the result
      off - offset into the byte array to start writing
      Returns:
      number of bytes converted.
      See Also:
    • isValidUtf8

      public boolean isValidUtf8()
      Check that a byte sequence is valid UTF-8
      Returns:
      true if it is valid UTF-8, otherwise false
      See Also:
    • tonumber

      public LuaValue tonumber()
      convert to a number using baee 10 or base 16 if it starts with '0x', or NIL if it can't be converted
      Overrides:
      tonumber in class LuaValue
      Returns:
      IntValue, DoubleValue, or NIL depending on the content of the string.
      See Also:
    • tonumber

      public LuaValue tonumber(int base)
      convert to a number using a supplied base, or NIL if it can't be converted
      Parameters:
      base - the base to use, such as 10
      Returns:
      IntValue, DoubleValue, or NIL depending on the content of the string.
      See Also:
    • scannumber

      public double scannumber()
      Convert to a number in base 10, or base 16 if the string starts with '0x', or return Double.NaN if it cannot be converted to a number.
      Returns:
      double value if conversion is valid, or Double.NaN if not
    • scannumber

      public double scannumber(int base)
      Convert to a number in a base, or return Double.NaN if not a number.
      Parameters:
      base - the base to use between 2 and 36
      Returns:
      double value if conversion is valid, or Double.NaN if not
    • printToStream

      public void printToStream(PrintStream ps)
      Print the bytes of the LuaString to a PrintStream as if it were an ASCII string, quoting and escaping control characters.
      Parameters:
      ps - PrintStream to print to.