wiki:League/TextCodec

Text Codec

Text Codec helps to convert text from external encoding into internal representation (Universal_String) and back.

Supported encodings

Character encoding Decoder Encoder Aliases unofficial
UTF-8 + +
UTF-16 (LE/BE) + -
ISO-8859-1 + +
Shift JIS + -
IBM 437 + +
Windows-1250 + +
Windows-1251 + + cp1251
Windows-1252 + +
Windows-1253 - -
Windows-1254 - -
Windows-1255 - -
Windows-1256 - -
Windows-1257 - -
Windows-1258 - -
KOI8-R + +
KOI8-U + +

Examples

Encoding conversion to UTF-16

Following function converts peace of stream elements in IBM-437 encoding into Wide_String:

function From_IBM_437
 (Item : Ada.Streams.Stream_Element_Array) return Wide_String
is
   Codec : constant League.Text_Codecs.Text_Codec
     := League.Text_Codecs.Codec
         (League.Strings.To_Universal_String ("ibm437"));

begin
   return Codec.Decode (Item).To_UTF_16_Wide_String;
end From_IBM_437;

Locale character encoding

This example outputs string using standard Ada.Streams.Stream_IO package, but correctly converting data into the locale character encoding.

with Ada.Streams.Stream_IO;

with League.Strings;
with League.Text_Codecs;

procedure Example is
   File : Ada.Streams.Stream_IO.File_Type;

begin
   Ada.Streams.Stream_IO.Create
    (File, Ada.Streams.Stream_IO.Out_File, "test.txt");
   Ada.Streams.Stream_IO.Write
    (File,
     League.Text_Codecs.Codec_For_Application_Locale.Encode
      (League.Strings.To_Universal_String
        ("Здравствуй, мир!")).To_Stream_Element_Array);
   Ada.Streams.Stream_IO.Close (File);
end Example;
Last modified 7 years ago Last modified on Apr 8, 2013, 2:28:05 PM
Note: See TracWiki for help on using the wiki.