How can I get a list of encodings that Perl 6 understands?

323 Views Asked by At

I see in /src/core/Rakudo/Internals.pm a hash with a short list of encodings but no way to get to it outside of that. The NORMALIZE_ENCODING method is the only thing that uses it. Use something that is not in this list and you get an exception.

So, how can I know what's valid before I try it, especially since some important encodings are missing?

my $encodings := nqp::hash(
      # fast mapping for identicals
      'utf8',            'utf8',
      'utf16',           'utf16',
      'utf32',           'utf32',
      'ascii',           'ascii',
      'iso-8859-1',      'iso-8859-1',
      'windows-1252',    'windows-1252',
      # with dash
      'utf-8',           'utf8',
      'utf-16',          'utf16',
      'utf-32',          'utf32',
      # according to http://de.wikipedia.org/wiki/ISO-8859-1
      'iso_8859-1:1987', 'iso-8859-1',
      'iso_8859-1',      'iso-8859-1',
      'iso-ir-100',      'iso-8859-1',
      'latin1',          'iso-8859-1',
      'latin-1',         'iso-8859-1',
      'csisolatin1',     'iso-8859-1',
      'l1',              'iso-8859-1',
      'ibm819',          'iso-8859-1',
      'cp819',           'iso-8859-1',
    );
    method NORMALIZE_ENCODING(Str:D \encoding) {
        my str $key = nqp::unbox_s(encoding);
        if nqp::existskey($encodings,$key) {
            nqp::atkey($encodings,$key)
        }
        else {
            my str $lc = nqp::lc($key);
            nqp::existskey($encodings,$lc)
              ?? nqp::atkey($encodings,$lc)
              !! nqp::lc($key)
        }
    }
1

There are 1 best solutions below

0
On

Regarding your original question: there does not seem to be such a thing. However, the code for encodings has changed, and now you can register new encodings and check existing ones; those list also available encodings.

There's also a function, find, that returns an encoding object if it exists:

say Encoding::Registry.find('oh-no'); 
# OUTPUT: «(exit code 1) Unknown string encoding 'oh-no'␤»
say Encoding::Registry.find('latin1'); 
#OUTPUT: «Encoding::Builtin.new(name => "iso-8859-1")␤»

The corresponding class is now documented.