1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2023, Free Software Foundation, Inc. --
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. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 -- Note: this package uses the generic subprograms in System.WCh_Cnv, which
27 -- completely encapsulate the set of wide character encoding methods, so no
28 -- modifications are required when adding new encoding methods.
32 with System
.WCh_Cnv
; use System
.WCh_Cnv
;
33 with System
.WCh_Con
; use System
.WCh_Con
;
35 package body Widechar
is
37 ---------------------------
38 -- Is_Start_Of_Wide_Char --
39 ---------------------------
41 function Is_Start_Of_Wide_Char
42 (S
: Source_Buffer_Ptr
;
43 P
: Source_Ptr
) return Boolean
46 case Wide_Character_Encoding_Method
is
48 -- For Hex mode, just test for an ESC character. The ESC character
49 -- cannot appear in any other context in a legal Ada program.
52 return S
(P
) = ASCII
.ESC
;
54 -- For brackets, just test ["x where x is a hex character. This is
55 -- sufficient test, since this sequence cannot otherwise appear in a
59 return P
<= S
'Last - 2
61 and then S
(P
+ 1) = '"'
62 and then (S
(P
+ 2) in '0' .. '9'
64 S
(P
+ 2) in 'a' .. 'f'
66 S
(P
+ 2) in 'A' .. 'F');
68 -- All other encoding methods use the upper bit set in the first
69 -- character to uniquely represent a wide character.
76 return S
(P
) >= Character'Val (16#
80#
);
78 end Is_Start_Of_Wide_Char
;
84 function Length_Wide
return Nat
is
86 return WC_Longest_Sequence
;
94 (S
: Source_Buffer_Ptr
;
95 P
: in out Source_Ptr
;
99 P_Init
: constant Source_Ptr
:= P
;
102 function In_Char
return Character;
103 -- Function to obtain characters of wide character escape sequence
109 function In_Char
return Character is
115 function WC_In
is new Char_Sequence_To_UTF_32
(In_Char
);
117 -- Start of processing for Scan_Wide
122 -- Scan out the wide character. If the first character is a bracket,
123 -- we allow brackets encoding regardless of the standard encoding
124 -- method being used, but otherwise we use this standard method.
127 C
:= Char_Code
(WC_In
(Chr
, WCEM_Brackets
));
129 C
:= Char_Code
(WC_In
(Chr
, Wide_Character_Encoding_Method
));
133 Wide_Char_Byte_Count
:= Wide_Char_Byte_Count
+ Nat
(P
- P_Init
- 1);
136 when Constraint_Error
=>
151 procedure Out_Char
(C
: Character);
152 -- Procedure to store one character of wide character sequence
158 procedure Out_Char
(C
: Character) is
164 procedure WC_Out
is new UTF_32_To_Char_Sequence
(Out_Char
);
166 -- Start of processing for Set_Wide
169 WC_Out
(UTF_32_Code
(C
), Wide_Character_Encoding_Method
);
176 procedure Skip_Wide
(S
: String; P
: in out Natural) is
177 P_Init
: constant Natural := P
;
179 function Skip_Char
return Character;
180 -- Function to skip one character of wide character escape sequence
186 function Skip_Char
return Character is
192 function WC_Skip
is new Char_Sequence_To_UTF_32
(Skip_Char
);
194 Discard
: UTF_32_Code
;
195 pragma Warnings
(Off
, Discard
);
197 -- Start of processing for Skip_Wide
200 -- Capture invalid wide characters errors since we are going to discard
201 -- the result anyway. We just want to move past it.
204 Discard
:= WC_Skip
(Skip_Char
, Wide_Character_Encoding_Method
);
206 when Constraint_Error
=>
210 Wide_Char_Byte_Count
:= Wide_Char_Byte_Count
+ Nat
(P
- P_Init
- 1);
217 procedure Skip_Wide
(S
: Source_Buffer_Ptr
; P
: in out Source_Ptr
) is
218 P_Init
: constant Source_Ptr
:= P
;
220 function Skip_Char
return Character;
221 -- Function to skip one character of wide character escape sequence
227 function Skip_Char
return Character is
233 function WC_Skip
is new Char_Sequence_To_UTF_32
(Skip_Char
);
235 Discard
: UTF_32_Code
;
236 pragma Warnings
(Off
, Discard
);
238 -- Start of processing for Skip_Wide
241 -- Capture invalid wide characters errors since we are going to discard
242 -- the result anyway. We just want to move past it.
245 Discard
:= WC_Skip
(Skip_Char
, Wide_Character_Encoding_Method
);
247 when Constraint_Error
=>
251 Wide_Char_Byte_Count
:= Wide_Char_Byte_Count
+ Nat
(P
- P_Init
- 1);