Binary strings: Difference between revisions

→‎{{header|Perl 6}}: various updates for 6.c
m (minor edit)
(→‎{{header|Perl 6}}: various updates for 6.c)
Line 1,737:
 
=={{header|Perl 6}}==
{{Works with|rakudo|2016.12}}
 
<lang perl6># Perl 6 is perfectly fine with NUL *characters* in strings:
 
my Str $s = 'nema' ~ 0.chr ~ 'problema!';
say $s;
Line 1,750 ⟶ 1,749:
say $buf;
 
# Strs can be encoded into BufsBlobs
my BufBlob $this = 'fooround-trip'.encode('ascii');
# … and BufsBlobs can be decoded into Strs …
my Str $that = $this.decode('ascii');
 
Line 1,763 ⟶ 1,762:
# straight-forward stuff directly to this attribute:
# (Note: "has byte @.bytes" would be nicer, but that is
# not yet implemented in rakudo or nieczaRakudo.)
has Int @.bytes handles(< Bool elems gist push >);
 
Line 1,783 ⟶ 1,782:
# A couple of operators for our new type:
multi infix:<cmp>(ByteStr $x, ByteStr $y) { $x.bytes cmp $y.bytes }
multi infix:<~> (ByteStr $x, ByteStr $y) { ByteStr.new(:bytes(|$x.bytes, |$y.bytes)) }
 
# create some byte strings (destruction not needed due to garbage collection)
my ByteStr $b0 = ByteStr.new;
my ByteStr $b1 = ByteStr.new(:bytes( |'foo'.ords, 0, 10, |'bar'.ords ));
 
# assignment ($b1 and $b2 contain the same ByteStr object afterwards):
Line 1,809 ⟶ 1,808:
# appending a byte:
$b1.push: 123;
say 'appended = ', $b1;
 
# extracting a substring:
my $sub = $b1.substr(2, 4);
say 'subsubstr = ', $sub;
 
# replacing a byte:
$b2.replace(102 => 103);
say 'replaced = ', $b2;
 
# joining:
Line 1,826:
<pre>nema␀problema!
Buf:0x<ff 00 01 02 03>
round-trip
b0 cmp b1 = IncreaseLess
b1 cmp b2 = Same
b1 = [102 0 0 0 10 98 97 114]
b2 = [102 0 0 0 10 98 97 114]
clone = [102 111 111 0 10 98 97 114]
b0 is empty
b1 is not empty
subappended = [102 0 0 0 10 98 97 114 123]
103substr 0= [0 0 10 98 97 114 123]
joinedreplaced = [103 0 0 0 10 98 97 114 123 0 0 10 98]
joined = [103 0 0 0 10 98 97 114 123 0 0 10 98]</pre>
</pre>
 
=={{header|Phix}}==
2,392

edits