Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Add encoding support to new encoder API.
This will be used by I/O handles for doing output, and may be used by Perl 6 language users directly also. It is designed to allow various decisions to be made up front (on newline translation, replacement chars, and output blob type), while `.encode` today has to make them for every single call.
- Loading branch information
Showing
5 changed files
with
84 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| my class Encoding::Encoder::Builtin does Encoding::Encoder { | ||
| has str $!encoding; | ||
| has Blob $!type; | ||
|
|
||
| method new(Str $encoding, Blob:U $type) { | ||
| nqp::create(self)!setup($encoding, $type) | ||
| } | ||
|
|
||
| method !setup($encoding, $type) { | ||
| $!encoding = $encoding; | ||
| $!type := nqp::can($type.HOW, 'pun') ?? $type.^pun !! $type.WHAT; | ||
| self | ||
| } | ||
|
|
||
| method encode-chars(Str:D $str --> Blob:D) { | ||
| nqp::encode($str, $!encoding, nqp::create($!type)) | ||
| } | ||
| } | ||
|
|
||
| my class Encoding::Encoder::Builtin::Replacement does Encoding::Encoder { | ||
| has str $!encoding; | ||
| has Blob $!type; | ||
| has str $!replacement; | ||
|
|
||
| method new(Str $encoding, Blob:U $type, Str $replacement) { | ||
| nqp::create(self)!setup($encoding, $type, $replacement) | ||
| } | ||
|
|
||
| method !setup($encoding, $type, $replacement) { | ||
| $!encoding = $encoding; | ||
| $!type := nqp::can($type.HOW, 'pun') ?? $type.^pun !! $type.WHAT; | ||
| $!replacement = $replacement; | ||
| self | ||
| } | ||
|
|
||
| method encode-chars(Str:D $str --> Blob:D) { | ||
| #?if moar | ||
| nqp::encoderep($str, $!encoding, $!replacement, nqp::create($!type)) | ||
| #?endif | ||
| #?if !moar | ||
| X::NYI.new(feature => 'encoding with replacement').throw | ||
| #?endif | ||
| } | ||
| } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| my class Encoding::Encoder::TranslateNewlineWrapper { | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
jnthn
Author
Member
|
||
| has Encoding::Encoder $!delegate; | ||
|
|
||
| method new(Encoding::Encoder $delegate) { | ||
| nqp::create(self)!setup($delegate) | ||
| } | ||
|
|
||
| method !setup(Encoding::Encoder $delegate) { | ||
| $!delegate := $delegate; | ||
| self | ||
| } | ||
|
|
||
| method encode-chars(Str:D $str --> Blob:D) { | ||
| $!delegate.encode-chars(Rakudo::Internals.TRANSPOSE($str, "\n", "\r\n")) | ||
| } | ||
| } | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Shouldn't
does Encoding::Encoderbe added toEncoding::Encoder::TranslateNewlineWrapper?It breaks the build on my Windows computer.
Submitted as PR #1105