| 1 |
------------------------------------------------------------------------------ |
| 2 |
-- -- |
| 3 |
-- GNAT RUN-TIME COMPONENTS -- |
| 4 |
-- -- |
| 5 |
-- S Y S T E M . W C H _ C N V -- |
| 6 |
-- -- |
| 7 |
-- S p e c -- |
| 8 |
-- -- |
| 9 |
-- Copyright (C) 1992-2025, Free Software Foundation, Inc. -- |
| 10 |
-- -- |
| 11 |
-- GNAT is free software; you can redistribute it and/or modify it under -- |
| 12 |
-- terms of the GNU General Public License as published by the Free Soft- -- |
| 13 |
-- ware Foundation; either version 3, or (at your option) any later ver- -- |
| 14 |
-- sion. GNAT is distributed in the hope that it will be useful, but WITH- -- |
| 15 |
-- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY -- |
| 16 |
-- or FITNESS FOR A PARTICULAR PURPOSE. -- |
| 17 |
-- -- |
| 18 |
-- As a special exception under Section 7 of GPL version 3, you are granted -- |
| 19 |
-- additional permissions described in the GCC Runtime Library Exception, -- |
| 20 |
-- version 3.1, as published by the Free Software Foundation. -- |
| 21 |
-- -- |
| 22 |
-- You should have received a copy of the GNU General Public License and -- |
| 23 |
-- a copy of the GCC Runtime Library Exception along with this program; -- |
| 24 |
-- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see -- |
| 25 |
-- <http://www.gnu.org/licenses/>. -- |
| 26 |
-- -- |
| 27 |
-- GNAT was originally developed by the GNAT team at New York University. -- |
| 28 |
-- Extensive contributions were provided by Ada Core Technologies Inc. -- |
| 29 |
-- -- |
| 30 |
------------------------------------------------------------------------------ |
| 31 |
|
| 32 |
-- This package contains generic subprograms used for converting between |
| 33 |
-- sequences of Character and Wide_Character. Wide_Wide_Character values |
| 34 |
-- are also handled, but represented using integer range types defined in |
| 35 |
-- this package, so that this package can be used from applications that |
| 36 |
-- are restricted to Ada 95 compatibility (such as the compiler itself). |
| 37 |
|
| 38 |
-- All the algorithms for encoding and decoding are isolated in this package |
| 39 |
-- and in System.WCh_JIS and should not be duplicated elsewhere. The only |
| 40 |
-- exception to this is that GNAT.Decode_String and GNAT.Encode_String have |
| 41 |
-- their own circuits for UTF-8 conversions, for improved efficiency. |
| 42 |
|
| 43 |
-- This unit may be used directly from an application program by providing |
| 44 |
-- an appropriate WITH, and the interface can be expected to remain stable. |
| 45 |
|
| 46 |
with System.WCh_Con; |
| 47 |
|
| 48 |
package System.WCh_Cnv is |
| 49 |
pragma Pure; |
| 50 |
|
| 51 |
type UTF_32_Code is range 0 .. 16#7FFF_FFFF#; |
| 52 |
for UTF_32_Code'Size use 32; |
| 53 |
-- Range of allowed UTF-32 encoding values |
| 54 |
|
| 55 |
type UTF_32_String is array (Positive range <>) of UTF_32_Code; |
| 56 |
|
| 57 |
generic |
| 58 |
with function In_Char return Character; |
| 59 |
function Char_Sequence_To_Wide_Char |
| 60 |
(C : Character; |
| 61 |
EM : System.WCh_Con.WC_Encoding_Method) return Wide_Character; |
| 62 |
-- C is the first character of a sequence of one or more characters which |
| 63 |
-- represent a wide character sequence. Calling the function In_Char for |
| 64 |
-- additional characters as required, Char_To_Wide_Char returns the |
| 65 |
-- corresponding wide character value. Constraint_Error is raised if the |
| 66 |
-- sequence of characters encountered is not a valid wide character |
| 67 |
-- sequence for the given encoding method. |
| 68 |
-- |
| 69 |
-- Note on the use of brackets encoding (WCEM_Brackets). The brackets |
| 70 |
-- encoding method is ambiguous in the context of this function, since |
| 71 |
-- there is no way to tell if ["1234"] is eight unencoded characters or |
| 72 |
-- one encoded character. In the context of Ada sources, any sequence |
| 73 |
-- starting [" must be the start of an encoding (since that sequence is |
| 74 |
-- not valid in Ada source otherwise). The routines in this package use |
| 75 |
-- the same approach. If the input string contains the sequence [" then |
| 76 |
-- this is assumed to be the start of a brackets encoding sequence, and |
| 77 |
-- if it does not match the syntax, an error is raised. |
| 78 |
|
| 79 |
generic |
| 80 |
with function In_Char return Character; |
| 81 |
function Char_Sequence_To_UTF_32 |
| 82 |
(C : Character; |
| 83 |
EM : System.WCh_Con.WC_Encoding_Method) return UTF_32_Code; |
| 84 |
-- This is similar to the above, but the function returns a code from |
| 85 |
-- the full UTF_32 code set, which covers the full range of possible |
| 86 |
-- values in Wide_Wide_Character. The result can be converted to |
| 87 |
-- Wide_Wide_Character form using Wide_Wide_Character'Val. |
| 88 |
|
| 89 |
generic |
| 90 |
with procedure Out_Char (C : Character); |
| 91 |
procedure Wide_Char_To_Char_Sequence |
| 92 |
(WC : Wide_Character; |
| 93 |
EM : System.WCh_Con.WC_Encoding_Method); |
| 94 |
-- Given a wide character, converts it into a sequence of one or |
| 95 |
-- more characters, calling the given Out_Char procedure for each. |
| 96 |
-- Constraint_Error is raised if the given wide character value is |
| 97 |
-- not a valid value for the given encoding method. |
| 98 |
-- |
| 99 |
-- Note on brackets encoding (WCEM_Brackets). For the input routines above, |
| 100 |
-- upper half characters can be represented as ["hh"] but this procedure |
| 101 |
-- will only use brackets encodings for codes higher than 16#FF#, so upper |
| 102 |
-- half characters will be output as single Character values. |
| 103 |
|
| 104 |
generic |
| 105 |
with procedure Out_Char (C : Character); |
| 106 |
procedure UTF_32_To_Char_Sequence |
| 107 |
(Val : UTF_32_Code; |
| 108 |
EM : System.WCh_Con.WC_Encoding_Method); |
| 109 |
-- This is similar to the above, but the input value is a code from the |
| 110 |
-- full UTF_32 code set, which covers the full range of possible values |
| 111 |
-- in Wide_Wide_Character. To convert a Wide_Wide_Character value, the |
| 112 |
-- caller can use Wide_Wide_Character'Pos in the call. |
| 113 |
|
| 114 |
end System.WCh_Cnv; |