| 1 |
------------------------------------------------------------------------------ |
| 2 |
-- -- |
| 3 |
-- GNAT RUN-TIME COMPONENTS -- |
| 4 |
-- -- |
| 5 |
-- S Y S T E M . U T F _ 3 2 -- |
| 6 |
-- -- |
| 7 |
-- S p e c -- |
| 8 |
-- -- |
| 9 |
-- Copyright (C) 2005-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 is an internal package that provides basic character |
| 33 |
-- classification capabilities needed by the compiler for handling full |
| 34 |
-- 32-bit wide wide characters. We avoid the use of the actual type |
| 35 |
-- Wide_Wide_Character, since we want to use these routines in the compiler |
| 36 |
-- itself, and we want to be able to compile the compiler with old versions |
| 37 |
-- of GNAT that did not implement Wide_Wide_Character. |
| 38 |
|
| 39 |
-- System.UTF_32 should not be directly used from an application program, but |
| 40 |
-- an equivalent package GNAT.UTF_32 can be used directly and provides exactly |
| 41 |
-- the same services. The reason this package is in System is so that it can |
| 42 |
-- with'ed by other packages in the Ada and System hierarchies. |
| 43 |
|
| 44 |
-- Note: this unit is used during bootstrap, see ADA_GENERATED_FILES in |
| 45 |
-- gcc-interface/Make-lang.in for details on the constraints. |
| 46 |
|
| 47 |
package System.UTF_32 is |
| 48 |
pragma Pure; |
| 49 |
|
| 50 |
type UTF_32 is range 0 .. 16#7FFF_FFFF#; |
| 51 |
-- So far, the only defined character codes are in 0 .. 16#01_FFFF# |
| 52 |
|
| 53 |
-- The following type defines the categories from the unicode definitions. |
| 54 |
-- The one addition we make is Fe, which represents the characters FFFE |
| 55 |
-- and FFFF in any of the planes. |
| 56 |
|
| 57 |
type Category is ( |
| 58 |
Cc, -- Other, Control |
| 59 |
Cf, -- Other, Format |
| 60 |
Cn, -- Other, Not Assigned |
| 61 |
Co, -- Other, Private Use |
| 62 |
Cs, -- Other, Surrogate |
| 63 |
Ll, -- Letter, Lowercase |
| 64 |
Lm, -- Letter, Modifier |
| 65 |
Lo, -- Letter, Other |
| 66 |
Lt, -- Letter, Titlecase |
| 67 |
Lu, -- Letter, Uppercase |
| 68 |
Mc, -- Mark, Spacing Combining |
| 69 |
Me, -- Mark, Enclosing |
| 70 |
Mn, -- Mark, Nonspacing |
| 71 |
Nd, -- Number, Decimal Digit |
| 72 |
Nl, -- Number, Letter |
| 73 |
No, -- Number, Other |
| 74 |
Pc, -- Punctuation, Connector |
| 75 |
Pd, -- Punctuation, Dash |
| 76 |
Pe, -- Punctuation, Close |
| 77 |
Pf, -- Punctuation, Final quote |
| 78 |
Pi, -- Punctuation, Initial quote |
| 79 |
Po, -- Punctuation, Other |
| 80 |
Ps, -- Punctuation, Open |
| 81 |
Sc, -- Symbol, Currency |
| 82 |
Sk, -- Symbol, Modifier |
| 83 |
Sm, -- Symbol, Math |
| 84 |
So, -- Symbol, Other |
| 85 |
Zl, -- Separator, Line |
| 86 |
Zp, -- Separator, Paragraph |
| 87 |
Zs, -- Separator, Space |
| 88 |
Fe); -- relative position FFFE/FFFF in any plane |
| 89 |
|
| 90 |
function Get_Category (U : UTF_32) return Category; |
| 91 |
-- Given a UTF32 code, returns corresponding Category, or Cn if |
| 92 |
-- the code does not have an assigned unicode category. |
| 93 |
|
| 94 |
-- The following functions perform category tests corresponding to lexical |
| 95 |
-- classes defined in the Ada standard. There are two interfaces for each |
| 96 |
-- function. The second takes a Category (e.g. returned by Get_Category). |
| 97 |
-- The first takes a UTF_32 code. The form taking the UTF_32 code is |
| 98 |
-- typically more efficient than calling Get_Category, but if several |
| 99 |
-- different tests are to be performed on the same code, it is more |
| 100 |
-- efficient to use Get_Category to get the category, then test the |
| 101 |
-- resulting category. |
| 102 |
|
| 103 |
function Is_UTF_32_Letter (U : UTF_32) return Boolean; |
| 104 |
function Is_UTF_32_Letter (C : Category) return Boolean; |
| 105 |
pragma Inline (Is_UTF_32_Letter); |
| 106 |
-- Returns true iff U is a letter that can be used to start an identifier, |
| 107 |
-- or if C is one of the corresponding categories, which are the following: |
| 108 |
-- Letter, Uppercase (Lu) |
| 109 |
-- Letter, Lowercase (Ll) |
| 110 |
-- Letter, Titlecase (Lt) |
| 111 |
-- Letter, Modifier (Lm) |
| 112 |
-- Letter, Other (Lo) |
| 113 |
-- Number, Letter (Nl) |
| 114 |
|
| 115 |
function Is_UTF_32_Digit (U : UTF_32) return Boolean; |
| 116 |
function Is_UTF_32_Digit (C : Category) return Boolean; |
| 117 |
pragma Inline (Is_UTF_32_Digit); |
| 118 |
-- Returns true iff U is a digit that can be used to extend an identifier, |
| 119 |
-- or if C is one of the corresponding categories, which are the following: |
| 120 |
-- Number, Decimal_Digit (Nd) |
| 121 |
|
| 122 |
function Is_UTF_32_Line_Terminator (U : UTF_32) return Boolean; |
| 123 |
pragma Inline (Is_UTF_32_Line_Terminator); |
| 124 |
-- Returns true iff U is an allowed line terminator for source programs, |
| 125 |
-- if U is in the category Zp (Separator, Paragraph), or Zl (Separator, |
| 126 |
-- Line), or if U is a conventional line terminator (CR, LF, VT, FF). |
| 127 |
-- There is no category version for this function, since the set of |
| 128 |
-- characters does not correspond to a set of Unicode categories. |
| 129 |
|
| 130 |
function Is_UTF_32_Mark (U : UTF_32) return Boolean; |
| 131 |
function Is_UTF_32_Mark (C : Category) return Boolean; |
| 132 |
pragma Inline (Is_UTF_32_Mark); |
| 133 |
-- Returns true iff U is a mark character which can be used to extend an |
| 134 |
-- identifier, or if C is one of the corresponding categories, which are |
| 135 |
-- the following: |
| 136 |
-- Mark, Non-Spacing (Mn) |
| 137 |
-- Mark, Spacing Combining (Mc) |
| 138 |
|
| 139 |
function Is_UTF_32_Other (U : UTF_32) return Boolean; |
| 140 |
function Is_UTF_32_Other (C : Category) return Boolean; |
| 141 |
pragma Inline (Is_UTF_32_Other); |
| 142 |
-- Returns true iff U is an other format character, which means that it |
| 143 |
-- can be used to extend an identifier, but is ignored for the purposes of |
| 144 |
-- matching of identifiers, or if C is one of the corresponding categories, |
| 145 |
-- which are the following: |
| 146 |
-- Other, Format (Cf) |
| 147 |
|
| 148 |
function Is_UTF_32_Punctuation (U : UTF_32) return Boolean; |
| 149 |
function Is_UTF_32_Punctuation (C : Category) return Boolean; |
| 150 |
pragma Inline (Is_UTF_32_Punctuation); |
| 151 |
-- Returns true iff U is a punctuation character that can be used to |
| 152 |
-- separate pieces of an identifier, or if C is one of the corresponding |
| 153 |
-- categories, which are the following: |
| 154 |
-- Punctuation, Connector (Pc) |
| 155 |
|
| 156 |
function Is_UTF_32_Space (U : UTF_32) return Boolean; |
| 157 |
function Is_UTF_32_Space (C : Category) return Boolean; |
| 158 |
pragma Inline (Is_UTF_32_Space); |
| 159 |
-- Returns true iff U is considered a space to be ignored, or if C is one |
| 160 |
-- of the corresponding categories, which are the following: |
| 161 |
-- Separator, Space (Zs) |
| 162 |
|
| 163 |
function Is_UTF_32_Non_Graphic (U : UTF_32) return Boolean; |
| 164 |
function Is_UTF_32_Non_Graphic (C : Category) return Boolean; |
| 165 |
pragma Inline (Is_UTF_32_Non_Graphic); |
| 166 |
-- Returns true iff U is considered to be a non-graphic character, or if C |
| 167 |
-- is one of the corresponding categories, which are the following: |
| 168 |
-- Other, Control (Cc) |
| 169 |
-- Other, Private Use (Co) |
| 170 |
-- Other, Surrogate (Cs) |
| 171 |
-- Separator, Line (Zl) |
| 172 |
-- Separator, Paragraph (Zp) |
| 173 |
-- FFFE or FFFF positions in any plane (Fe) |
| 174 |
-- |
| 175 |
-- Note that the Ada category format effector is subsumed by the above |
| 176 |
-- list of Unicode categories. |
| 177 |
-- |
| 178 |
-- Note that Other, Unassigned (Cn) is quite deliberately not included |
| 179 |
-- in the list of categories above. This means that should any of these |
| 180 |
-- code positions be defined in future with graphic characters they will |
| 181 |
-- be allowed without a need to change implementations or the standard. |
| 182 |
-- |
| 183 |
-- Note that Other, Format (Cf) is also quite deliberately not included |
| 184 |
-- in the list of categories above. This means that these characters can |
| 185 |
-- be included in character and string literals. |
| 186 |
|
| 187 |
-- The following function is used to fold to upper case, as required by |
| 188 |
-- the Ada 2005 standard rules for identifier case folding. Two |
| 189 |
-- identifiers are equivalent if they are identical after folding all |
| 190 |
-- letters to upper case using this routine. A corresponding routine to |
| 191 |
-- fold to lower case is also provided. |
| 192 |
|
| 193 |
function Is_UTF_32_NFKC (U : UTF_32) return Boolean; |
| 194 |
pragma Inline (Is_UTF_32_NFKC); |
| 195 |
-- Return True if U could be present in a string normalized to |
| 196 |
-- Normalization Form KC (as defined by Clause 21 of ISO/IEC 10646:2017), |
| 197 |
-- otherwise returns False. |
| 198 |
|
| 199 |
function Is_UTF_32_Basic (U : UTF_32) return Boolean; |
| 200 |
pragma Inline (Is_UTF_32_Basic); |
| 201 |
-- Return True if U has no Decomposition Mapping in the code charts of |
| 202 |
-- ISO/IEC 10646:2017. |
| 203 |
|
| 204 |
function UTF_32_To_Lower_Case (U : UTF_32) return UTF_32; |
| 205 |
pragma Inline (UTF_32_To_Lower_Case); |
| 206 |
-- If U represents an upper case letter, returns the corresponding lower |
| 207 |
-- case letter, otherwise U is returned unchanged. The folding rule is |
| 208 |
-- simply that if the code corresponds to a 10646 entry whose name contains |
| 209 |
-- the string CAPITAL LETTER, and there is a corresponding entry whose name |
| 210 |
-- is the same but with CAPITAL LETTER replaced by SMALL LETTER, then the |
| 211 |
-- code is folded to this SMALL LETTER code. Otherwise the input code is |
| 212 |
-- returned unchanged. |
| 213 |
|
| 214 |
function UTF_32_To_Upper_Case (U : UTF_32) return UTF_32; |
| 215 |
pragma Inline (UTF_32_To_Upper_Case); |
| 216 |
-- If U represents a lower case letter, returns the corresponding lower |
| 217 |
-- case letter, otherwise U is returned unchanged. The folding rule is |
| 218 |
-- simply that if the code corresponds to a 10646 entry whose name contains |
| 219 |
-- the string SMALL LETTER, and there is a corresponding entry whose name |
| 220 |
-- is the same but with SMALL LETTER replaced by CAPITAL LETTER, then the |
| 221 |
-- code is folded to this CAPITAL LETTER code. Otherwise the input code is |
| 222 |
-- returned unchanged. |
| 223 |
|
| 224 |
function UTF_32_To_Basic (U : UTF_32) return UTF_32; |
| 225 |
pragma Inline (UTF_32_To_Basic); |
| 226 |
-- Returns the UTF_32 character whose code point is given by the first |
| 227 |
-- value of its Decomposition Mapping in the code charts of ISO/IEC |
| 228 |
-- 10646:2017 if any; returns Item otherwise. |
| 229 |
|
| 230 |
end System.UTF_32; |