| 1 |
------------------------------------------------------------------------------ |
| 2 |
-- -- |
| 3 |
-- GNAT COMPILER COMPONENTS -- |
| 4 |
-- -- |
| 5 |
-- S Y S T E M . W C H _ C O N -- |
| 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 defines the codes used to identify the encoding method for |
| 33 |
-- wide characters in string and character constants. This is needed both |
| 34 |
-- at compile time and at runtime (for the wide character runtime routines) |
| 35 |
|
| 36 |
-- This unit may be used directly from an application program by providing |
| 37 |
-- an appropriate WITH, and the interface can be expected to remain stable. |
| 38 |
|
| 39 |
package System.WCh_Con is |
| 40 |
pragma Pure; |
| 41 |
|
| 42 |
------------------------------------- |
| 43 |
-- Wide_Character Encoding Methods -- |
| 44 |
------------------------------------- |
| 45 |
|
| 46 |
-- A wide character encoding method is a method for uniquely representing |
| 47 |
-- a Wide_Character or Wide_Wide_Character value using a one or more |
| 48 |
-- Character values. Three types of encoding method are supported by GNAT: |
| 49 |
|
| 50 |
-- An escape encoding method uses ESC as the first character of the |
| 51 |
-- sequence, and subsequent characters determine the wide character |
| 52 |
-- value that is represented. Any character other than ESC stands |
| 53 |
-- for itself as a single byte (i.e. any character in Latin-1, other |
| 54 |
-- than ESC itself, is represented as a single character: itself). |
| 55 |
|
| 56 |
-- An upper half encoding method uses a character in the upper half |
| 57 |
-- range (i.e. in the range 16#80# .. 16#FF#) as the first byte of |
| 58 |
-- a wide character encoding sequence. Subsequent characters are |
| 59 |
-- used to determine the wide character value that is represented. |
| 60 |
-- Any character in the lower half (16#00# .. 16#7F#) represents |
| 61 |
-- itself as a single character. |
| 62 |
|
| 63 |
-- The brackets notation, where a wide character is represented by the |
| 64 |
-- sequence ["xx"] or ["xxxx"] or ["xxxxxx"] where xx are hexadecimal |
| 65 |
-- characters. Note that currently this is the only encoding that |
| 66 |
-- supports the full UTF-32 range. |
| 67 |
|
| 68 |
-- Note that GNAT does not currently support escape-in, escape-out |
| 69 |
-- encoding methods, where an escape sequence is used to set a mode |
| 70 |
-- used to recognize subsequent characters. All encoding methods use |
| 71 |
-- individual character-by-character encodings, so that a sequence of |
| 72 |
-- wide characters is represented by a sequence of encodings. |
| 73 |
|
| 74 |
-- To add new encoding methods, the following steps are required: |
| 75 |
|
| 76 |
-- 1. Define a code for a new value of type WC_Encoding_Method |
| 77 |
-- 2. Adjust the definition of WC_Encoding_Method accordingly |
| 78 |
-- 3. Provide appropriate conversion routines in System.WCh_Cnv |
| 79 |
-- 4. Adjust definition of WC_Longest_Sequence if necessary |
| 80 |
-- 5. Add an entry in WC_Encoding_Letters for the new method |
| 81 |
-- 6. Add proper code to s-wchstw.adb, s-wchwts.adb, s-widwch.adb |
| 82 |
-- 7. Update documentation (remember section on form strings) |
| 83 |
|
| 84 |
-- Note that the WC_Encoding_Method values must be kept ordered so that |
| 85 |
-- the definitions of the subtypes WC_Upper_Half_Encoding_Method and |
| 86 |
-- WC_ESC_Encoding_Method are still correct. |
| 87 |
|
| 88 |
--------------------------------- |
| 89 |
-- Encoding Method Definitions -- |
| 90 |
--------------------------------- |
| 91 |
|
| 92 |
type WC_Encoding_Method is range 1 .. 6; |
| 93 |
-- Type covering the range of values used to represent wide character |
| 94 |
-- encoding methods. An enumeration type might be a little neater, but |
| 95 |
-- more trouble than it's worth, given the need to pass these values |
| 96 |
-- from the compiler to the backend, and to record them in the ALI file. |
| 97 |
|
| 98 |
WCEM_Hex : constant WC_Encoding_Method := 1; |
| 99 |
-- The wide character with code 16#abcd# is represented by the escape |
| 100 |
-- sequence ESC a b c d (five characters, where abcd are ASCII hex |
| 101 |
-- characters, using upper case for letters). This method is easy |
| 102 |
-- to deal with in external environments that do not support wide |
| 103 |
-- characters, and covers the whole 16-bit BMP. Codes larger than |
| 104 |
-- 16#FFFF# are not representable using this encoding method. |
| 105 |
|
| 106 |
WCEM_Upper : constant WC_Encoding_Method := 2; |
| 107 |
-- The wide character with encoding 16#abcd#, where the upper bit is on |
| 108 |
-- (i.e. a is in the range 8-F) is represented as two bytes 16#ab# and |
| 109 |
-- 16#cd#. The second byte may never be a format control character, but |
| 110 |
-- is not required to be in the upper half. This method can be also used |
| 111 |
-- for shift-JIS or EUC where the internal coding matches the external |
| 112 |
-- coding. Codes larger than 16#FFFF# are not representable using this |
| 113 |
-- encoding method. |
| 114 |
|
| 115 |
WCEM_Shift_JIS : constant WC_Encoding_Method := 3; |
| 116 |
-- A wide character is represented by a two character sequence 16#ab# |
| 117 |
-- and 16#cd#, with the restrictions described for upper half encoding |
| 118 |
-- as described above. The internal character code is the corresponding |
| 119 |
-- JIS character according to the standard algorithm for Shift-JIS |
| 120 |
-- conversion. See the body of package System.JIS_Conversions for |
| 121 |
-- further details. Codes larger than 16#FFFF are not representable |
| 122 |
-- using this encoding method. |
| 123 |
|
| 124 |
WCEM_EUC : constant WC_Encoding_Method := 4; |
| 125 |
-- A wide character is represented by a two character sequence 16#ab# and |
| 126 |
-- 16#cd#, with both characters being in the upper half set. The internal |
| 127 |
-- character code is the corresponding JIS character according to the EUC |
| 128 |
-- encoding algorithm. See the body of package System.JIS_Conversions for |
| 129 |
-- further details. Codes larger than 16#FFFF# are not representable using |
| 130 |
-- this encoding method. |
| 131 |
|
| 132 |
WCEM_UTF8 : constant WC_Encoding_Method := 5; |
| 133 |
-- An ISO 10646-1 BMP/Unicode wide character is represented in UCS |
| 134 |
-- Transformation Format 8 (UTF-8), as defined in Annex R of ISO |
| 135 |
-- 10646-1/Am.2. Depending on the character value, a Unicode character |
| 136 |
-- is represented as the one to six byte sequence. |
| 137 |
-- |
| 138 |
-- 16#0000_0000#-16#0000_007f#: 2#0xxxxxxx# |
| 139 |
-- 16#0000_0080#-16#0000_07ff#: 2#110xxxxx# 2#10xxxxxx# |
| 140 |
-- 16#0000_0800#-16#0000_ffff#: 2#1110xxxx# 2#10xxxxxx# 2#10xxxxxx# |
| 141 |
-- 16#0001_0000#-16#001F_FFFF#: 2#11110xxx# 2#10xxxxxx# 2#10xxxxxx# |
| 142 |
-- 2#10xxxxxx# |
| 143 |
-- 16#0020_0000#-16#03FF_FFFF#: 2#111110xx# 2#10xxxxxx# 2#10xxxxxx# |
| 144 |
-- 2#10xxxxxx# 2#10xxxxxx# |
| 145 |
-- 16#0400_0000#-16#7FFF_FFFF#: 2#1111110x# 2#10xxxxxx# 2#10xxxxxx# |
| 146 |
-- 2#10xxxxxx# 2#10xxxxxx# 2#10xxxxxx# |
| 147 |
-- |
| 148 |
-- where the xxx bits correspond to the left-padded bits of the |
| 149 |
-- 16-bit character value. Note that all lower half ASCII characters |
| 150 |
-- are represented as ASCII bytes and all upper half characters and |
| 151 |
-- other wide characters are represented as sequences of upper-half. This |
| 152 |
-- encoding method can represent the entire range of Wide_Wide_Character. |
| 153 |
|
| 154 |
WCEM_Brackets : constant WC_Encoding_Method := 6; |
| 155 |
-- A wide character is represented using one of the following sequences: |
| 156 |
-- |
| 157 |
-- ["xx"] |
| 158 |
-- ["xxxx"] |
| 159 |
-- ["xxxxxx"] |
| 160 |
-- ["xxxxxxxx"] |
| 161 |
-- |
| 162 |
-- where xx are hexadecimal digits representing the character code. This |
| 163 |
-- encoding method can represent the entire range of Wide_Wide_Character |
| 164 |
-- but in the general case results in ambiguous representations (there is |
| 165 |
-- no ambiguity in Ada sources, since the above sequences are illegal Ada). |
| 166 |
|
| 167 |
WC_Encoding_Letters : constant array (WC_Encoding_Method) of Character := |
| 168 |
[WCEM_Hex => 'h', |
| 169 |
WCEM_Upper => 'u', |
| 170 |
WCEM_Shift_JIS => 's', |
| 171 |
WCEM_EUC => 'e', |
| 172 |
WCEM_UTF8 => '8', |
| 173 |
WCEM_Brackets => 'b']; |
| 174 |
-- Letters used for selection of wide character encoding method in the |
| 175 |
-- compiler options (-gnatW? switch) and for Wide_Text_IO (WCEM parameter |
| 176 |
-- in the form string). |
| 177 |
|
| 178 |
subtype WC_ESC_Encoding_Method is |
| 179 |
WC_Encoding_Method range WCEM_Hex .. WCEM_Hex; |
| 180 |
-- Encoding methods using an ESC character at the start of the sequence |
| 181 |
|
| 182 |
subtype WC_Upper_Half_Encoding_Method is |
| 183 |
WC_Encoding_Method range WCEM_Upper .. WCEM_UTF8; |
| 184 |
-- Encoding methods using an upper half character (16#80#..16#FF) at |
| 185 |
-- the start of the sequence. |
| 186 |
|
| 187 |
WC_Longest_Sequence : constant := 12; |
| 188 |
-- The longest number of characters that can be used for a wide character |
| 189 |
-- or wide wide character sequence for any of the active encoding methods. |
| 190 |
|
| 191 |
WC_Longest_Sequences : constant array (WC_Encoding_Method) of Natural := |
| 192 |
[WCEM_Hex => 5, |
| 193 |
WCEM_Upper => 2, |
| 194 |
WCEM_Shift_JIS => 2, |
| 195 |
WCEM_EUC => 2, |
| 196 |
WCEM_UTF8 => 6, |
| 197 |
WCEM_Brackets => 12]; |
| 198 |
-- The longest number of characters that can be used for a wide character |
| 199 |
-- or wide wide character sequence using the given encoding method. |
| 200 |
|
| 201 |
function Get_WC_Encoding_Method (C : Character) return WC_Encoding_Method; |
| 202 |
-- Given a character C, returns corresponding encoding method (see array |
| 203 |
-- WC_Encoding_Letters above). Raises Constraint_Error if not in list. |
| 204 |
|
| 205 |
function Get_WC_Encoding_Method (S : String) return WC_Encoding_Method; |
| 206 |
-- Given a lower case string that is one of hex, upper, shift_jis, euc, |
| 207 |
-- utf8, brackets, return the corresponding encoding method. Raises |
| 208 |
-- Constraint_Error if not in list. |
| 209 |
|
| 210 |
function Is_Start_Of_Encoding |
| 211 |
(C : Character; |
| 212 |
EM : WC_Encoding_Method) return Boolean; |
| 213 |
pragma Inline (Is_Start_Of_Encoding); |
| 214 |
-- Returns True if the Character C is the start of a multi-character |
| 215 |
-- encoding sequence for the given encoding method EM. If EM is set to |
| 216 |
-- WCEM_Brackets, this function always returns False. |
| 217 |
|
| 218 |
end System.WCh_Con; |