Skip to content

Commit

Permalink
Implement Uni.encode (#5274)
Browse files Browse the repository at this point in the history
This allows one to write normalized unicode strings. It should be noted
that this is not an optimized implementation at all, written in nqp it
should be faster and even faster if it would be made a syscall.
  • Loading branch information
Leont committed Jun 13, 2023
1 parent 1657c0f commit e3a7474
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/core.c/Uni.pm6
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ my class NFKC is repr('VMArray') is array_type(uint32) { ... }
my class NFKD is repr('VMArray') is array_type(uint32) { ... }

my class X::InvalidCodepoint { ... }
my class Encoding::Registry { ... }

my class Uni does Positional[uint32] does Stringy is repr('VMArray') is array_type(uint32) {

Expand Down Expand Up @@ -148,6 +149,17 @@ my class Uni does Positional[uint32] does Stringy is repr('VMArray') is array_ty
'Uni.new(' ~ self.list.fmt('0x%04x', ', ') ~ ')' ~
(self.WHAT === Uni ?? '' !! '.' ~ self.^name);
}

method encode(Str:D $encoding = 'utf8',
:$replacement, Bool() :$translate-nl = False, :$strict --> Blob:D) {
my $encoder = Encoding::Registry.find($encoding)
.encoder(:$replacement, :$translate-nl, :$strict);
my $buf = Buf.new;
for self.list -> $character {
$buf.append($encoder.encode-chars($character.chr));
}
$buf;
}
}

my class NFD is Uni {
Expand Down

0 comments on commit e3a7474

Please sign in to comment.