1. class Encoding::Builtin does Encoding {
  2. has Str $.name;
  3. has $!alternative-names;
  4. method new() {
  5. X::Cannot::New.new(class => self.WHAT).throw
  6. }
  7. method SET-SELF(\name, \alternatives) {
  8. nqp::stmts(
  9. ($!name := name),
  10. ($!alternative-names := alternatives),
  11. self
  12. )
  13. }
  14. method alternative-names() { $!alternative-names }
  15. method decoder(:$replacement, :$translate-nl, :$strict --> Encoding::Decoder) {
  16. my $decoder = $replacement.DEFINITE && $replacement !=== False
  17. ?? Encoding::Decoder::Builtin.new($!name, :$strict, :$translate-nl, :replacement(self!rep-char($replacement)))
  18. !! Encoding::Decoder::Builtin.new($!name, :$strict, :$translate-nl);
  19. }
  20. my int $is-win = Rakudo::Internals.IS-WIN;
  21. method encoder(:$replacement, :$translate-nl, :$strict --> Encoding::Encoder) {
  22. my $encoder = $replacement.DEFINITE && $replacement !=== False
  23. ?? Encoding::Encoder::Builtin.new($!name, self!buf-type(), :$strict, :replacement(self!rep-char($replacement)))
  24. !! Encoding::Encoder::Builtin.new($!name, self!buf-type(), :$strict);
  25. $translate-nl && $is-win
  26. ?? Encoding::Encoder::TranslateNewlineWrapper.new($encoder)
  27. !! $encoder
  28. }
  29. my $enc_type := nqp::hash('utf8',utf8,'utf16',utf16,'utf32',utf32);
  30. method !buf-type() {
  31. nqp::ifnull(nqp::atkey($enc_type, $!name), blob8)
  32. }
  33. method !rep-char($replacement) {
  34. nqp::istype($replacement, Bool)
  35. ?? ($!name.starts-with('utf') ?? "\x[FFFD]" !! "?")
  36. !! $replacement.Str
  37. }
  38. }