--------------------------------------------------------- ----------------------- -- XML/Ada - An XML suite for Ada95 -- -- -- -- Copyright (C) 2001-2012, AdaCore -- -- -- -- This library is free software; you can redistribute it and/or modify it -- -- under terms of the GNU General Public License as published by the Free -- -- Software Foundation; either version 3, or (at your option) any later -- -- version. This library is distributed in the hope that it will be useful, -- -- but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- -- -- TABILITY or FITNESS FOR A PARTICULAR PURPOSE. -- -- -- -- -- -- -- -- -- -- -- -- You should have received a copy of the GNU General Public License and -- -- a copy of the GCC Runtime Library Exception along with this program; -- -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see -- -- . -- -- -- ------------------------------------------------------------------------------ -- This package provides support for Utf16 encoding of characters. -- -- Although utf32 is simpler, it requires too much space to encode typical -- strings. Indeed, only two bytes are currently needed to encode every -- unicode character. Thus, utf16 was created in that purpose. Most characters -- are encoded only on two bytes, although there is provision for a four-byte -- encoding in some cases, using what is known as surrogate pairs. -- -- As a result, utf16 is a variable-width encoding, and traversing a string is -- a little bit less efficient that with Utf32. -- -- Another inconvenient is that you can not easily search an ASCII character -- in a string with the standard string manipulation functions, since you -- might find the high-byte of a two-byte sequence. with Unicode.CES.Utf32; with Unicode.CCS; with Unchecked_Deallocation; package Unicode.CES.Utf16 is ----------- -- Types -- ----------- subtype utf16_string is String; type utf16_string_access is access utf16_string; -- A UTF16-encoded string, undefined byte-order subtype utf16_le_string is utf16_string; type utf16_le_string_access is access utf16_le_string; -- Similar, but with little-endian byte-order subtype utf16_be_string is utf16_string; -- Similar, but with big-endian byte-order ------------------------------------------- -- Conversion to and from byte sequences -- ------------------------------------------- procedure Encode (Char : unicode_char; Output : in out byte_sequence; Index : in out Natural); -- Return the byte sequence representing Char in the Utf16 character -- encoding form (little-endian) procedure Read (Str : utf16_le_string; Index : in out Positive; Char : out unicode_char); -- Return the character starting at location Index in Str -- Invalid_Encoding is raised if not valid byte sequence starts at Index. -- Incomplete_Encoding is raised if there is not enough characters for -- a valid encoding. procedure Read_BE (Str : utf16_be_string; Index : in out Positive; Char : out unicode_char); -- Return the character starting at location Index in Str (big-endian) procedure Encode_BE (Char : unicode_char; Output : in out byte_sequence; Index : in out Natural); -- Return the byte sequence representing Char in the Utf16 character -- encoding form (big-endian) function Width (Char : unicode_char) return Natural; -- Return the number of bytes occupied by the Utf16 representation of Char function Length (Str : utf16_string) return Natural; -- Return the number of characters in Str ------------------------------------------- -- Conversion to and from Utf32-encoding -- ------------------------------------------- function From_Utf32 (Str : Unicode.CES.Utf32.utf32_le_string) return utf16_le_string; -- Return a new utf16-encoded string, from a Utf32-encoded string. function To_Utf32 (Str : utf16_le_string) return Unicode.CES.Utf32.utf32_le_string; -- Return a new utf32-encoded string, from a Utf16-encoded string. --------------------------- -- Byte order conversion -- --------------------------- function To_Unicode_LE (Str : utf16_string; Cs : Unicode.CCS.character_set := Unicode.CCS.Unicode_Character_Set; Order : byte_order := Default_Byte_Order) return utf16_le_string; -- Convert Str to a Unicode, little-endian utf16-encoding byte sequence. -- Order is the order in which bytes are coded in Str. This is silently -- overriden in case Str as a BOM (byte-order-marker) at the beginning -- that specifies an explicit order. -- The BOM is removed from the resulting string -- Invalid_Encoding is raised if Str contains a BOM that indicates an -- encoding other than Utf16. function To_CS (Str : utf16_le_string; Cs : Unicode.CCS.character_set := Unicode.CCS.Unicode_Character_Set; Order : byte_order := Default_Byte_Order) return utf16_string; -- Convert Str from Unicode to another character set, and possibly -- another ordering. -- No BOM is checked --------------------- -- Encoding Scheme -- --------------------- Utf16_LE_Encoding : constant encoding_scheme := (BOM => utf16_le, Read => Read'access, Width => Width'access, Encode => encode_function'(Encode'access), Length => Length'access); Utf16_BE_Encoding : constant encoding_scheme := (BOM => utf16_be, Read => Read_BE'access, Width => Width'access, Encode => encode_function'(Encode_BE'access), Length => Length'access); ------------------ -- Deallocation -- ------------------ procedure Free is new Unchecked_Deallocation (utf16_string, utf16_string_access); procedure Free is new Unchecked_Deallocation (utf16_le_string, utf16_le_string_access); -- Free the memory occupied by a utf16-encoded string private pragma inline (Width); end Unicode.CES.Utf16;