Skip to content

Instantly share code, notes, and snippets.

@masak
Created June 11, 2010 13:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save masak/434483 to your computer and use it in GitHub Desktop.
Save masak/434483 to your computer and use it in GitHub Desktop.
$ cat src/core/Buf.pm
role Buf[::T = Int] does Stringy {
has T @.contents;
multi method new(@contents) {
self.bless(*, :contents(@contents.list));
}
multi method decode($encoding = 'UTF-8') {
my @contents = @.contents;
my $str = ~Q:PIR {
$P0 = find_lex '@contents'
.local pmc bb
.local string s
bb = new ['ByteBuffer']
bb[0] = 0xC3
bb[1] = 0x91
s = bb.'get_string_as'(utf8:unicode:"")
%r = s
# iterator = iter $P0
# .local pmc sb
# sb = new 'StringBuilder'
# sb = unicode:""
# loop:
# unless iterator goto done
# $P1 = shift iterator
# $I1 = $P1
# $S1 = chr $I1
# sb .= $S1
# goto loop
# done:
# %r = sb
};
return $str;
}
multi method elems() {
@.contents.elems;
}
}
our multi sub infix:<eqv>(Buf $a, Buf $b) {
return $a.contents ~~ $b.contents;
}
$ ./perl6 t/spec/S32-str/encode.rakudo 1..10
ok 1 - $str.encode returns a Buf
ok 2 - encoding to ASCII
ok 3 - right length of Buf
ok 4 - encoding to UTF-8
ok 5 - right length of Buf
ok 6 - # SKIP We do not handle NDF yet
Null PMC access in set_string_native()
in 'decode' at line 3972:CORE.setting
in main program body at line 21:t/spec/S32-str/encode.rakudo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment