------------------------------------------------------------------------------ -- 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 Utf8 encoding of characters. -- -- Characters whose code is less than 128 are encoded as is in the -- Utf8_String. As a result, such a string is compatible with a standard -- String whose characters are all standard ASCII (and contains no -- extended ASCII characters). -- In that, one of the beauties of UTF-8 (and UTF-16) is that there is no -- overlap, as opposed to what happens with other encodings. If you search -- for an ASCII character within a Utf8_String, using the standard string -- string or array manipulation functions, you will only find that character, -- and not part of a longer sequence that encodes another character. -- As a result, all the standard string-manipulation functions will work -- as is (note however that the 'Length attribute doesn't represent the -- number of characters in the string, but the number of bytes). -- -- However, since characters can be encoded on one to six bytes, this means -- that traversing a string is not as efficient as with other encodings. -- -- Also, this encoding is not subject to byte-ordering constraints, since this -- is only a sequence of bytes. It is self-synchronizing, in that you can -- start anywhere in the string and find a synchronization point easily. with Unicode.CES.Utf32; with Unicode.CCS; with Unchecked_Deallocation; package Unicode.CES.Utf8 is ----------- -- Types -- ----------- subtype utf8_string is String; type utf8_string_access is access all utf8_string; -- An UTF8-encoded string. ------------------------------------------- -- Conversion to and from byte sequences -- ------------------------------------------- procedure Encode (Char : unicode_char; Output : in out byte_sequence; Index : in out Natural); -- Set the byte sequence representing Char in the Utf8 character encoding. -- There must remain at least 6 characters in Output if you want to avoid -- Constraint_Errors. procedure Read (Str : utf8_string; Index : in out Positive; Char : out unicode_char); -- Return the character starting at location Index in Str, and move Index -- to the beginning of the next location -- 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. function Width (Char : unicode_char) return Natural; -- Return the number of bytes occupied by the Utf8 representation of Char function Length (Str : utf8_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 utf8_string; -- Return a new Utf8-encoded string, from a Utf32-encoded string. function To_Utf32 (Str : utf8_string) return Unicode.CES.Utf32.utf32_le_string; -- Return a new Utf32-encoded string, from a Utf8-encoded string. --------------------------- -- Byte order conversion -- --------------------------- function To_Unicode_LE (Str : utf8_string; Cs : Unicode.CCS.character_set := Unicode.CCS.Unicode_Character_Set; Order : byte_order := Default_Byte_Order) return utf8_string; -- Convert str (character set is CS) to a Unicode -- little-endian byte-sequence -- If Str contains a BOM that indicates an encoding other than Utf8, -- Invalid_Encoding is raised. -- Order is irrelevant for utf8, but is kept for interface compatibility -- with other similar functions. function To_CS (Str : utf8_string; Cs : Unicode.CCS.character_set := Unicode.CCS.Unicode_Character_Set; Order : byte_order := Default_Byte_Order) return utf8_string; -- Convert Str (Unicode) to another character set --------------------- -- Encoding Scheme -- --------------------- Utf8_Encoding : constant encoding_scheme := (BOM => utf8_all, Read => Read'access, Width => Width'access, Encode => encode_function'(Encode'access), Length => Length'access); ------------------ -- Deallocation -- ------------------ procedure Free is new Unchecked_Deallocation (utf8_string, utf8_string_access); -- Free the memory occupied by a utf8-encoded string private pragma inline (Width); end Unicode.CES.Utf8;