Binary strings: Difference between revisions

Content added Content deleted
(J: clarification)
Line 731: Line 731:


=={{header|J}}==
=={{header|J}}==
J's literal data type supports arbitrary binary data (strings are binary strings by default). J's semantics are pass by value (with garbage collection) with a minor exception (mapped files).
{{incorrect|J}}


* Example binary string creation
J's literal data type supports arbitrary binary data. J's semantics are pass by value (with garbage collection) with a minor exception (mapped files).
<lang j> name=: ''</lang>


* Example binary string deletion (removing all references to a string allows it to be deleted, in this case we give the name a numeric value to replace its prior string value):
* String creation and destruction is not needed
<lang j> FIXME: show mapped file example</lang>
<lang j> name=: 0</lang>


* String assignment
* Example binary string assignment
<lang j> name=: 'value'</lang>
<lang j> name=: 'value'</lang>


* String comparison
* Example binary string comparison
<lang j> name1 -: name2</lang>
<lang j> name1 -: name2</lang>


* String cloning and copying is not needed
* Example binary string cloning and copying
<lang j> FIXME: show mapped file example</lang>
<lang j> name1= 'example'
name2= name1</lang>


Though, technically, its the internal reference which is cloned, not the internal representation of the value. But operations which modify strings are copy on write, so this distinction is not visible without going outside the language.
* Check if a string is empty

* Example check if a binary string is empty
<lang j> 0=#string</lang>
<lang j> 0=#string</lang>


* Append a byte to a string
* Example apppend a byte to a binary string
<lang j> string,byte</lang>
<lang j> string=: 'example'
byte=: DEL
string=: string,byte</lang>


* Extract a substring from a string
* Extract a substring from a binary string
<lang j> 3{.5}.'The quick brown fox runs...'</lang>
<lang j> 3{.5}.'The quick brown fox runs...'</lang>


Line 762: Line 768:
* Join strings
* Join strings
<lang j> 'string1','string2'</lang>
<lang j> 'string1','string2'</lang>

Note also: given an integer n, the corresponding byte value may be obtained by indexing into <code>a.</code> which is the ordered array of all bytes.:
<lang j> n{a.</lang>

Thus, the binary string containing bytes with numeric values 1 0 255 can be obtained this way:
<lang j>1 0 255{a.</lang>


=={{header|Lua}}==
=={{header|Lua}}==