Binary strings: Difference between revisions

Content added Content deleted
m (minor edit)
(→‎{{header|Perl 6}}: various updates for 6.c)
Line 1,737: Line 1,737:


=={{header|Perl 6}}==
=={{header|Perl 6}}==
{{Works with|rakudo|2016.12}}

<lang perl6># Perl 6 is perfectly fine with NUL *characters* in strings:
<lang perl6># Perl 6 is perfectly fine with NUL *characters* in strings:

my Str $s = 'nema' ~ 0.chr ~ 'problema!';
my Str $s = 'nema' ~ 0.chr ~ 'problema!';
say $s;
say $s;
Line 1,750: Line 1,749:
say $buf;
say $buf;


# Strs can be encoded into Bufs
# Strs can be encoded into Blobs
my Buf $this = 'foo'.encode('ascii');
my Blob $this = 'round-trip'.encode('ascii');
# … and Bufs can be decoded into Strs …
# … and Blobs can be decoded into Strs …
my Str $that = $this.decode('ascii');
my Str $that = $this.decode('ascii');


Line 1,763: Line 1,762:
# straight-forward stuff directly to this attribute:
# straight-forward stuff directly to this attribute:
# (Note: "has byte @.bytes" would be nicer, but that is
# (Note: "has byte @.bytes" would be nicer, but that is
# not yet implemented in rakudo or niecza.)
# not yet implemented in Rakudo.)
has Int @.bytes handles(< Bool elems gist push >);
has Int @.bytes handles(< Bool elems gist push >);


Line 1,783: Line 1,782:
# A couple of operators for our new type:
# A couple of operators for our new type:
multi infix:<cmp>(ByteStr $x, ByteStr $y) { $x.bytes cmp $y.bytes }
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)) }
multi infix:<~> (ByteStr $x, ByteStr $y) { ByteStr.new(:bytes(|$x.bytes, |$y.bytes)) }


# create some byte strings (destruction not needed due to garbage collection)
# create some byte strings (destruction not needed due to garbage collection)
my ByteStr $b0 = ByteStr.new;
my ByteStr $b0 = ByteStr.new;
my ByteStr $b1 = ByteStr.new(:bytes( 'foo'.ords, 0, 10, 'bar'.ords ));
my ByteStr $b1 = ByteStr.new(:bytes( |'foo'.ords, 0, 10, |'bar'.ords ));


# assignment ($b1 and $b2 contain the same ByteStr object afterwards):
# assignment ($b1 and $b2 contain the same ByteStr object afterwards):
Line 1,809: Line 1,808:
# appending a byte:
# appending a byte:
$b1.push: 123;
$b1.push: 123;
say 'appended = ', $b1;


# extracting a substring:
# extracting a substring:
my $sub = $b1.substr(2, 4);
my $sub = $b1.substr(2, 4);
say 'sub = ', $sub;
say 'substr = ', $sub;


# replacing a byte:
# replacing a byte:
$b2.replace(102 => 103);
$b2.replace(102 => 103);
say $b2;
say 'replaced = ', $b2;


# joining:
# joining:
Line 1,826: Line 1,826:
<pre>nema␀problema!
<pre>nema␀problema!
Buf:0x<ff 00 01 02 03>
Buf:0x<ff 00 01 02 03>
round-trip
b0 cmp b1 = Increase
b0 cmp b1 = Less
b1 cmp b2 = Same
b1 cmp b2 = Same
b1 = 102 0 0 0 10 98 97 114
b1 = [102 0 0 0 10 98 97 114]
b2 = 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
clone = [102 111 111 0 10 98 97 114]
b0 is empty
b0 is empty
b1 is not empty
b1 is not empty
sub = 0 0 10 98
appended = [102 0 0 0 10 98 97 114 123]
103 0 0 0 10 98 97 114 123
substr = [0 0 10 98]
joined = 103 0 0 0 10 98 97 114 123 0 0 10 98
replaced = [103 0 0 0 10 98 97 114 123]
joined = [103 0 0 0 10 98 97 114 123 0 0 10 98]</pre>
</pre>


=={{header|Phix}}==
=={{header|Phix}}==