Unofficial LSL reference

User Tools

Login

You are currently not logged in! Enter your authentication credentials below to log in. You need to have cookies enabled to log in.

Login

Forgotten your password? Get a new one: Set new password

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

types:string [2015-01-13 17:56 SLT]
sei Add navbar & header
types:string [2015-09-22 19:45 SLT] (current)
sei Use another trick for raw
Line 2: Line 2:
 ===== String type ===== ===== String type =====
  
-The **string** type represents a sequence of characters. Character support varies between LSO and Mono: LSO supports any byte sequence that does not contain **NUL**, even though some of them are not representable in Unicode, while Mono only supports valid Unicode sequences (also not containing **NUL**).+The $ty[stringtype represents a sequence of characters. Character support varies between LSO and Mono: LSO supports any byte sequence that does not contain ​an ASCII **NUL** ​character, even though some of them are not representable in Unicode, while Mono only supports valid Unicode sequences (also not containing **NUL**). There is no limit to the length of a string or string literal, other than available memory.
  
 A string literal is written by enclosing it in double quotes. It may be empty, and does not need to end at the end of the line, but if there are newlines embedded in the literal, they will be part of it. Here are some examples: A string literal is written by enclosing it in double quotes. It may be empty, and does not need to end at the end of the line, but if there are newlines embedded in the literal, they will be part of it. Here are some examples:
Line 19: Line 19:
 These four examples are string literals representing the empty string, the string **This is a string literal**, the string **☑ Check!** and the string **A line** followed by a newline character and by **and another line**. These four examples are string literals representing the empty string, the string **This is a string literal**, the string **☑ Check!** and the string **A line** followed by a newline character and by **and another line**.
  
-Inside the double quotes, there are two special characters: the double quote itself **"**, which obviously closes the string, and the backslash character **\**. The double quote closes the string literal, unless preceded by a backslash. The backslash is used as a prefix for any character that is to be interpreted literally, including the backslash itself and the double quote. The exceptions are if the backslash is followed by a lower case **n** or **t**. A backslash followed by **n** is interpreted as a newline, and a backslash followed by a **t** is interpreted as four spaces. Examples:+Inside the double quotes, there are two special characters: the double quote itself **"**, which obviously closes the string, and the backslash character **\**. The double quote closes the string literal, unless preceded by a backslash. The backslash is used as a prefix for any character that is to be interpreted literally, including the backslash itself and the double quote. The exceptions are if the backslash is followed by a lower case **n** or **t**. A backslash followed by an **n** is interpreted as a newline, and a backslash followed by a **t** is interpreted as four spaces. Examples:
  
 <code lsl2> <code lsl2>
Line 34: Line 34:
 </​code>​ </​code>​
  
-The first string literal represents the string **Press "​OK"​ to confirm**; the second string literal represents a string that has four spaces followed by **This is intdented ​by 4 spaces**, and the third one represents the string **\o/** followed by a newline, then a **|**, another newline, and then the string **/ \**, which in hovering text may look like this:+The first string literal represents the string **Press "​OK"​ to confirm**; the second string literal represents a string that has four spaces followed by **This is indented ​by 4 spaces**, and the third one represents the string **\o/** followed by a newline, then a **|**, another newline, and then the string **/ \**, which in hovering text may look like this:
  
-  ​\o/ +<WRAP centeralign>​ 
-   ​+\o/\\ 
-  / \+`|\\ 
 +/ \</​WRAP>​
  
 The last two examples represent the same string, namely the letter **a** followed by a newline and the letter **b**. The last two examples represent the same string, namely the letter **a** followed by a newline and the letter **b**.
Line 57: Line 58:
 will represent the string literal **String split.** followed by a newline, //then twenty spaces//, then **Indented part.** will represent the string literal **String split.** followed by a newline, //then twenty spaces//, then **Indented part.**
  
-Finally, string literals may be immediately preceded by the upper case letter **L** (with no spaces in between). The intention seemed to be to ignore it, but due to a bug in the compiler, it has a funny side effect: the first double quote character is taken as part of the string. For example, the string literal ''​L"​String ​literal"''​ actually represents the string **"​String ​literal**.+Finally, string literals may be immediately preceded by the upper case letter **L** (with no spaces in between). The intention seemed to be to ignore it, but due to a bug in the compiler, it has a funny side effect: the first double quote character is taken as part of the string. For example, the string literal ''​L"​String"''​ actually represents the string **"​String**.
  
 === Type casting of strings === === Type casting of strings ===
  
-The **string** type is universal in the sense that it may be cast to and from every other type:+The $ty[stringtype is universal in the sense that it may be cast to and from every other type:
  
-  * ''​(string)integer_value''​ converts it to decimal. See [[integer#​Type casting an integer to/from other types]] for details. +  * ''​(string)integer_value''​ converts it to decimal. See $lty[integer#​Type casting an integer to/from other types] for details. 
-  * ''​(string)float_value''​ converts it to decimal with six significant digits. See [[float#Precision]] for details. +  * ''​(string)float_value''​ converts it to decimal with six significant digits. See $lty[float#Type casting a float to string] for details. 
-  * ''​(string)key_value''​ converts a [[key]] to string. The **key** type acts like a string (though it is intended for other uses), therefore the conversion is literal. +  * ''​(string)key_value''​ converts a $lty[key] to string. The $ty[keytype acts like a string (though it is intended for other uses), therefore the conversion is literal. 
-  * ''​(string)vector_value''​ converts the [[vector]] to a string with three comma-separated floats enclosed in angle brackets, but with five decimal places instead of six. See [[float#Precision]] for details. +  * ''​(string)vector_value''​ converts the $lty[vector] to a string with three comma-separated floats enclosed in angle brackets, but with five decimal places instead of six. See $lty[float#Type casting a float to string] for details. 
-  * ''​(string)rotation_value''​ does the same but with the four values of the [[rotation]] instead of three+  * ''​(string)rotation_value''​ does the same with the four values of the $lty[rotation]. 
-  * ''​(string)list_value''​ converts the [[list]] by casting every element to string and then concatenating them all together. That is, it does the same as ''​[[functions/​llDumpList2String]](list_var, ""​)''​.In this case, however, vectors and rotations are converted with six decimal places per float.+  * ''​(string)list_value''​ converts the $lty[list] by casting every element to string and then concatenating them all together. That is, it does the same as ''​llDumpList2String(list_var,​ ""​)'' ​(see $lfn[llDumpList2String] for more information). In this case, however, vectors and rotations are converted with six decimal places per float.
  
-  * ''​(integer)string_value''​ tries to parse the string as an [[integer]]. See [[integer#​Type casting an integer to/from other types]] for details. If the parsing fails, 0 is the result instead. +  * ''​(integer)string_value''​ tries to parse the string as an $lty[integer]. See $lty[integer#​Type casting an integer to/from other types] for details. If the parsing fails, 0 is the result instead. 
-  * ''​(float)string_value''​ tries to parse the string as a [[float]]. The expected format is that from the //glibc// function ''​strtod''​. See [[http://​www.gnu.org/​software/​libc/​manual/​html_node/​Parsing-of-Floats.html|glibc:​ Parsing of Floats]] for details. Hexadecimal floats, as specified there, //are// supported. If the parsing fails, 0.0 is the result instead. +  * ''​(float)string_value''​ tries to parse the string as a $lty[float]. The expected format is that from the //glibc// function ''​strtof''​. See [[http://​www.gnu.org/​software/​libc/​manual/​html_node/​Parsing-of-Floats.html|glibc:​ Parsing of Floats]] for details. Hexadecimal floats, as specified there, //are// supported, but subnormal a.k.a. denormal floats in hexadecimal notation are only supported in LSO. If the parsing fails for whatever reason, 0.0 is the result instead. 
-  * ''​(key)string_value''​ converts the string to [[key]]. The resulting value may not be a valid UUID, but the **key** type is still able to hold any string, so the conversion is lossless. +  * ''​(key)string_value''​ converts the string to $lty[key]. The resulting value may not be a valid UUID, but the $ty[keytype is still able to hold any string, so the conversion is lossless. 
-  * ''​(vector)string_value''​ tries to parse the string as a [[vector]]. It expects a **<** followed by three comma-separated floats (see above for the format of the expected float). If the parsing fails at any point, [[constants/ZERO_VECTOR]] is the result instead. For example, ''​(vector)"<​1,​ 2, 3, 4>"''​ gives the vector ''<​1.0,​ 2.0, 3.0>''​ (the parsing succeeds after it successfully parses the third complete number). +  * ''​(vector)string_value''​ tries to parse the string as a $lty[vector]. It expects a $op[<followed by three comma-separated floats (see above for the format of the expected float). If the parsing fails at any point, ​$lct[ZERO_VECTOR] is the result instead. For example, ''​(vector)"<​1,​ 2, 3, 4>"''​ gives the vector ''<​1.0,​ 2.0, 3.0>''​ (the parsing succeeds after it successfully parses the third complete number), but ''​(vector)"<​1 , 2, 3>"''​ gives $ct[ZERO_VECTOR] (because there'​s a space before a comma). The strings ''​inf'',​ ''​infinity'',​ and ''​nan''​ (case insensitive) are also valid values that can appear instead of the numeric part of each component (apart from the optional sign and the leading space). If the last element'​s numeric part is any of the case-insensitive strings ''​infi'',​ ''​infin'',​ ''​inifini''​ or ''​infinit'',​ the parsing will fail, despite having the valid substring ''​inf''​ at the beginning of the numeric part
-  * ''​(rotation)string_value''​ is similar to vector. If the parsing fails at any point, [[constants/ZERO_ROTATION]] is the result instead.+  * ''​(rotation)string_value''​ is similar to vector. If the parsing fails at any point, ​$lct[ZERO_ROTATION] is the result instead.
   * ''​(list)string_value''​ returns a list with one single element of type string.   * ''​(list)string_value''​ returns a list with one single element of type string.
  
 === Concatenation === === Concatenation ===
-Two or more strings can be concatenated,​ meaning to make a larger string which contains the first one followed by the second and so on. The operator for concatenation is the plus sign **+**. Concatenation always happens at run time; the LSL compiler does not optimize the strings into a single one as other compilers do.+Two or more strings can be concatenated,​ meaning to make a larger string which contains the first one followed by the second and so on. The operator for concatenation is the plus sign $op[+]. Concatenation always happens at run time; the LSL compiler does not optimize the strings into a single one as other compilers do.
  
 For example, ''"​s"​ + "​tick"''​ will produce the string **stick**. A somewhat more useful complete example: For example, ''"​s"​ + "​tick"''​ will produce the string **stick**. A somewhat more useful complete example:
  
-<code lsl2>+<file lsl2 string-concatenation-example.lsl>
 default default
 { {
     touch_start(integer n)     touch_start(integer n)
     {     {
-        llSay(0, "Hello, ​" + llDetectedName(0));​+        llSay(0, "Touched by " + llDetectedName(0));​
     }     }
 } }
-</code+</file
-If e.g. Philip Linden touches it, the object will say, **Hello, ​Philip Linden**.+If e.g. Philip Linden touches it, the object will say, **Touched by Philip Linden**.
  
-There is no automatic type conversion when concatenating strings. That means that if a plus sign is between a string and something of any other type (except ​[[list]]) in any order, a type mismatch error will be generated. For example: ''​[[functions/llOwnerSay]]("​Your key is " + [[functions/llGetOwner]]());''​ will result in a type mismatch, because keys and strings can't be concatenated. You need to add an explicit type cast, e.g. ''​[[functions/​llOwnerSay]]("Your key is " + ([[types/string]])[[functions/​llGetOwner]]());''​+There is no automatic type conversion when concatenating strings. That means that if a plus sign is between a string and something of any other type (except ​$lty[list]) in any order, a type mismatch error will be generated. For example: ''​$lfn[llOwnerSay|llOwnerSay]("​Your key is " + $lfn[llGetOwner|llGetOwner]());''​ will result in a type mismatch, because ​$lfn[llGetOwner] returns a key, and keys and strings can't be concatenated. You need to add an explicit type cast, e.g. ''​llOwnerSay("​Your key is " + (string)llGetOwner());''​
  
-If one of the elements is a list, the result is not a string, but a list. See [[types/list]] for details.+If one of the elements is a list, the result is not a string, but a list, and a type mismatch may result at a latter point if a string was expected. See $lty[list] for details.
  
 ===== See Also ===== ===== See Also =====
-  * [[functions/string/​|String ​functions]]+  * $lfn[String/functions