| 1 |
------------------------------------------------------------------------------ |
| 2 |
-- -- |
| 3 |
-- GNAT RUN-TIME COMPONENTS -- |
| 4 |
-- -- |
| 5 |
-- S Y S T E M . W C H _ W T S -- |
| 6 |
-- -- |
| 7 |
-- B o d y -- |
| 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 |
with System.Case_Util; use System.Case_Util; |
| 33 |
with System.WCh_Con; use System.WCh_Con; |
| 34 |
with System.WCh_Cnv; use System.WCh_Cnv; |
| 35 |
|
| 36 |
package body System.WCh_WtS is |
| 37 |
|
| 38 |
----------------------- |
| 39 |
-- Local Subprograms -- |
| 40 |
----------------------- |
| 41 |
|
| 42 |
procedure Normalize_String |
| 43 |
(S : in out String; |
| 44 |
EM : WC_Encoding_Method); |
| 45 |
-- If S does not represent a character literal, then any lower case |
| 46 |
-- characters in S are changed to their upper case counterparts, while |
| 47 |
-- wide characters are unchanged. EM indicates their encoding method. |
| 48 |
-- This is the wide counterpart of System.Val_Util.Normalize_String. |
| 49 |
|
| 50 |
procedure Store_UTF_32_Character |
| 51 |
(U : UTF_32_Code; |
| 52 |
S : out String; |
| 53 |
P : in out Integer; |
| 54 |
EM : WC_Encoding_Method); |
| 55 |
-- Stores the string representation of the wide or wide wide character |
| 56 |
-- whose code is given as U, starting at S (P + 1). P is incremented to |
| 57 |
-- point to the last character stored. Raises CE if character cannot be |
| 58 |
-- stored using the given encoding method. |
| 59 |
|
| 60 |
---------------------- |
| 61 |
-- Normalize_String -- |
| 62 |
---------------------- |
| 63 |
|
| 64 |
procedure Normalize_String |
| 65 |
(S : in out String; |
| 66 |
EM : WC_Encoding_Method) |
| 67 |
is |
| 68 |
procedure Skip_Wide (S : String; P : in out Natural); |
| 69 |
-- On entry S (P) points to an ESC character for a wide character escape |
| 70 |
-- sequence or an upper half character if the encoding method uses the |
| 71 |
-- upper bit, or a left bracket if the brackets encoding method is in |
| 72 |
-- use. On exit, P is bumped past the wide character sequence. |
| 73 |
|
| 74 |
--------------- |
| 75 |
-- Skip_Wide -- |
| 76 |
--------------- |
| 77 |
|
| 78 |
procedure Skip_Wide (S : String; P : in out Natural) is |
| 79 |
function Skip_Char return Character; |
| 80 |
-- Function to skip one character of wide character escape sequence |
| 81 |
|
| 82 |
--------------- |
| 83 |
-- Skip_Char -- |
| 84 |
--------------- |
| 85 |
|
| 86 |
function Skip_Char return Character is |
| 87 |
begin |
| 88 |
P := P + 1; |
| 89 |
return S (P - 1); |
| 90 |
end Skip_Char; |
| 91 |
|
| 92 |
function WC_Skip is new Char_Sequence_To_UTF_32 (Skip_Char); |
| 93 |
|
| 94 |
Discard : UTF_32_Code; |
| 95 |
pragma Warnings (Off, Discard); |
| 96 |
|
| 97 |
-- Start of processing for Skip_Wide |
| 98 |
|
| 99 |
begin |
| 100 |
-- Capture invalid wide characters errors since we are going to |
| 101 |
-- discard the result anyway. We just want to move past it. |
| 102 |
|
| 103 |
begin |
| 104 |
Discard := WC_Skip (Skip_Char, EM); |
| 105 |
exception |
| 106 |
when Constraint_Error => |
| 107 |
null; |
| 108 |
end; |
| 109 |
end Skip_Wide; |
| 110 |
|
| 111 |
F, L, Ptr : Natural; |
| 112 |
|
| 113 |
begin |
| 114 |
F := S'First; |
| 115 |
L := S'Last; |
| 116 |
|
| 117 |
-- Case of empty string |
| 118 |
|
| 119 |
if F > L then |
| 120 |
return; |
| 121 |
end if; |
| 122 |
|
| 123 |
-- Scan for leading spaces |
| 124 |
|
| 125 |
while F < L and then S (F) = ' ' loop |
| 126 |
F := F + 1; |
| 127 |
end loop; |
| 128 |
|
| 129 |
-- Case of no nonspace characters found. Decrease L to ensure L < F |
| 130 |
-- without risking an overflow if F is Integer'Last. |
| 131 |
|
| 132 |
if S (F) = ' ' then |
| 133 |
L := L - 1; |
| 134 |
return; |
| 135 |
end if; |
| 136 |
|
| 137 |
-- Scan for trailing spaces |
| 138 |
|
| 139 |
while S (L) = ' ' loop |
| 140 |
L := L - 1; |
| 141 |
end loop; |
| 142 |
|
| 143 |
-- Convert to upper case if S is not a character literal |
| 144 |
|
| 145 |
if S (F) /= ''' then |
| 146 |
Ptr := F; |
| 147 |
|
| 148 |
while Ptr <= L loop |
| 149 |
-- This mimics the handling of wide characters in a call to |
| 150 |
-- Casing.Set_Casing (All_Upper_Case) in the compiler. |
| 151 |
|
| 152 |
if S (Ptr) = ASCII.ESC |
| 153 |
or else S (Ptr) = '[' |
| 154 |
or else (EM in WC_Upper_Half_Encoding_Method |
| 155 |
and then Character'Pos (S (Ptr)) >= 16#80#) |
| 156 |
then |
| 157 |
Skip_Wide (S, Ptr); |
| 158 |
|
| 159 |
else |
| 160 |
S (Ptr) := To_Upper (S (Ptr)); |
| 161 |
Ptr := Ptr + 1; |
| 162 |
end if; |
| 163 |
end loop; |
| 164 |
end if; |
| 165 |
end Normalize_String; |
| 166 |
|
| 167 |
---------------------------- |
| 168 |
-- Store_UTF_32_Character -- |
| 169 |
---------------------------- |
| 170 |
|
| 171 |
procedure Store_UTF_32_Character |
| 172 |
(U : UTF_32_Code; |
| 173 |
S : out String; |
| 174 |
P : in out Integer; |
| 175 |
EM : WC_Encoding_Method) |
| 176 |
is |
| 177 |
procedure Out_Char (C : Character); |
| 178 |
pragma Inline (Out_Char); |
| 179 |
-- Procedure to increment P and store C at S (P) |
| 180 |
|
| 181 |
procedure Store_Chars is new UTF_32_To_Char_Sequence (Out_Char); |
| 182 |
|
| 183 |
-------------- |
| 184 |
-- Out_Char -- |
| 185 |
-------------- |
| 186 |
|
| 187 |
procedure Out_Char (C : Character) is |
| 188 |
begin |
| 189 |
P := P + 1; |
| 190 |
S (P) := C; |
| 191 |
end Out_Char; |
| 192 |
|
| 193 |
begin |
| 194 |
Store_Chars (U, EM); |
| 195 |
end Store_UTF_32_Character; |
| 196 |
|
| 197 |
-------------------------------- |
| 198 |
-- Enum_Wide_String_To_String -- |
| 199 |
-------------------------------- |
| 200 |
|
| 201 |
function Enum_Wide_String_To_String |
| 202 |
(S : Wide_String; |
| 203 |
EM : WC_Encoding_Method) return String |
| 204 |
is |
| 205 |
Result : String := Wide_String_To_String (S, EM); |
| 206 |
|
| 207 |
begin |
| 208 |
Normalize_String (Result, EM); |
| 209 |
return Result; |
| 210 |
end Enum_Wide_String_To_String; |
| 211 |
|
| 212 |
------------------------------------- |
| 213 |
-- Enum_Wide_Wide_String_To_String -- |
| 214 |
------------------------------------- |
| 215 |
|
| 216 |
function Enum_Wide_Wide_String_To_String |
| 217 |
(S : Wide_Wide_String; |
| 218 |
EM : WC_Encoding_Method) return String |
| 219 |
is |
| 220 |
Result : String := Wide_Wide_String_To_String (S, EM); |
| 221 |
|
| 222 |
begin |
| 223 |
Normalize_String (Result, EM); |
| 224 |
return Result; |
| 225 |
end Enum_Wide_Wide_String_To_String; |
| 226 |
|
| 227 |
--------------------------- |
| 228 |
-- Wide_String_To_String -- |
| 229 |
--------------------------- |
| 230 |
|
| 231 |
function Wide_String_To_String |
| 232 |
(S : Wide_String; |
| 233 |
EM : WC_Encoding_Method) return String |
| 234 |
is |
| 235 |
Max_Chars : constant Natural := WC_Longest_Sequences (EM); |
| 236 |
|
| 237 |
Result : String (S'First .. S'First + Max_Chars * S'Length); |
| 238 |
Result_Idx : Natural; |
| 239 |
|
| 240 |
begin |
| 241 |
Result_Idx := Result'First - 1; |
| 242 |
|
| 243 |
for S_Idx in S'Range loop |
| 244 |
Store_UTF_32_Character |
| 245 |
(U => Wide_Character'Pos (S (S_Idx)), |
| 246 |
S => Result, |
| 247 |
P => Result_Idx, |
| 248 |
EM => EM); |
| 249 |
end loop; |
| 250 |
|
| 251 |
return Result (Result'First .. Result_Idx); |
| 252 |
end Wide_String_To_String; |
| 253 |
|
| 254 |
-------------------------------- |
| 255 |
-- Wide_Wide_String_To_String -- |
| 256 |
-------------------------------- |
| 257 |
|
| 258 |
function Wide_Wide_String_To_String |
| 259 |
(S : Wide_Wide_String; |
| 260 |
EM : WC_Encoding_Method) return String |
| 261 |
is |
| 262 |
Max_Chars : constant Natural := WC_Longest_Sequences (EM); |
| 263 |
|
| 264 |
Result : String (S'First .. S'First + Max_Chars * S'Length); |
| 265 |
Result_Idx : Natural; |
| 266 |
|
| 267 |
begin |
| 268 |
Result_Idx := Result'First - 1; |
| 269 |
|
| 270 |
for S_Idx in S'Range loop |
| 271 |
Store_UTF_32_Character |
| 272 |
(U => Wide_Wide_Character'Pos (S (S_Idx)), |
| 273 |
S => Result, |
| 274 |
P => Result_Idx, |
| 275 |
EM => EM); |
| 276 |
end loop; |
| 277 |
|
| 278 |
return Result (Result'First .. Result_Idx); |
| 279 |
end Wide_Wide_String_To_String; |
| 280 |
|
| 281 |
end System.WCh_WtS; |