--  Verada.Strings.Unicode: CPython's character classes and single-character case mappings
--  of all of Unicode, from the run time's tables (rt/unicode.c, generated from CPython's
--  own str methods), and the properties Ada.Wide_Wide_Characters.Handling classifies by
--  (Unicode 16.0.0). Each takes and gives a code point; a class is 1 or 0.
package Verada.Strings.Unicode is
   --  1 when CPython's `c.isdigit()` holds of the code point, else 0
   function Digit_Code (Code : Integer) return Integer
     with Import, Convention => Ada, External_Name => "verada_rt_digit_code",
          Global => null,
          Post => Digit_Code'Result in 0 .. 1;

   --  1 when CPython's `c.isalpha()` holds of the code point, else 0
   function Alpha_Code (Code : Integer) return Integer
     with Import, Convention => Ada, External_Name => "verada_rt_alpha_code",
          Global => null,
          Post => Alpha_Code'Result in 0 .. 1;

   --  1 when CPython's `c.isspace()` holds of the code point, else 0
   function Space_Code (Code : Integer) return Integer
     with Import, Convention => Ada, External_Name => "verada_rt_space_code",
          Global => null,
          Post => Space_Code'Result in 0 .. 1;

   --  the code point's upper case, CPython's where it is one character (else itself)
   function Upper_Code (Code : Integer) return Integer
     with Import, Convention => Ada, External_Name => "verada_rt_upper_of",
          Global => null,
          Pre => Code in 0 .. 16#10FFFF#,
          Post => Upper_Code'Result in 0 .. 16#10FFFF#;

   --  the code point's lower case, CPython's where it is one character (else itself)
   function Lower_Code (Code : Integer) return Integer
     with Import, Convention => Ada, External_Name => "verada_rt_lower_of",
          Global => null,
          Pre => Code in 0 .. 16#10FFFF#,
          Post => Lower_Code'Result in 0 .. 16#10FFFF#;

   --  ---- Unicode's properties, for Ada.Wide_Wide_Characters.Handling ----

   --  Unicode's general categories, by their short names
   type General_Category is
     (Lu, Ll, Lt, Lm, Lo, Mn, Mc, Me, Nd, Nl, No, Pc, Pd, Ps, Pe, Pi, Pf, Po,
      Sm, Sc, Sk, So, Zs, Zl, Zp, Cc, Cf, Cs, Co, Cn);

   --  the position in General_Category of the code point's category (a code point beyond
   --  Unicode's, Cn's)
   function Category_Code (Code : Natural) return Natural
     with Import, Convention => Ada, External_Name => "verada_rt_category_code",
          Global => null,
          Post => Category_Code'Result <= General_Category'Pos (General_Category'Last);

   --  1 when the code point has a compatibility decomposition (its decomposition is tagged,
   --  as <super> is), else 0
   function Compatibility_Code (Code : Natural) return Natural
     with Import, Convention => Ada, External_Name => "verada_rt_compatibility_code",
          Global => null,
          Post => Compatibility_Code'Result in 0 .. 1;

   --  1 when normalization form KC leaves the code point as it is, else 0
   function NFKC_Code (Code : Natural) return Natural
     with Import, Convention => Ada, External_Name => "verada_rt_nfkc_code",
          Global => null,
          Post => NFKC_Code'Result in 0 .. 1;

   --  Unicode's simple case mappings: the code point's upper (lower) case when that is one
   --  code point, else itself
   function Simple_Upper_Code (Code : Natural) return Natural
     with Import, Convention => Ada, External_Name => "verada_rt_simple_upper",
          Global => null,
          Post => Simple_Upper_Code'Result <= 16#10FFFF# or else Simple_Upper_Code'Result = Code;
   function Simple_Lower_Code (Code : Natural) return Natural
     with Import, Convention => Ada, External_Name => "verada_rt_simple_lower",
          Global => null,
          Post => Simple_Lower_Code'Result <= 16#10FFFF# or else Simple_Lower_Code'Result = Code;

   --  a letter's basic letter: the letter its canonical decomposition starts with, when the
   --  rest is combining marks (A for each of A's accented forms); else the code point itself
   function Basic_Code (Code : Natural) return Natural
     with Import, Convention => Ada, External_Name => "verada_rt_basic_of",
          Global => null,
          Post => Basic_Code'Result <= 16#10FFFF# or else Basic_Code'Result = Code;

   --  The same properties of every code point, Latin-1's written out so that proofs see them
   --  (the run time's tables give the same there).

   function Category (Code : Natural) return General_Category is
     (if Code > 255 then General_Category'Val (Category_Code (Code))
      else (case Code is
               when 0 .. 31 | 127 .. 159 => Cc,
               when 32 | 160 => Zs,
               when 33 .. 35 | 37 .. 39 | 42 | 44 | 46 .. 47 | 58 .. 59 | 63 .. 64 | 92 | 161 | 167
                    | 182 .. 183 | 191 => Po,
               when 36 | 162 .. 165 => Sc,
               when 40 | 91 | 123 => Ps,
               when 41 | 93 | 125 => Pe,
               when 43 | 60 .. 62 | 124 | 126 | 172 | 177 | 215 | 247 => Sm,
               when 45 => Pd,
               when 48 .. 57 => Nd,
               when 65 .. 90 | 192 .. 214 | 216 .. 222 => Lu,
               when 94 | 96 | 168 | 175 | 180 | 184 => Sk,
               when 95 => Pc,
               when 97 .. 122 | 181 | 223 .. 246 | 248 .. 255 => Ll,
               when 166 | 169 | 174 | 176 => So,
               when 170 | 186 => Lo,
               when 171 => Pi,
               when 173 => Cf,
               when 178 .. 179 | 185 | 188 .. 190 => No,
               when 187 => Pf,
               when others => Cn));

   function Has_Compatibility_Decomposition (Code : Natural) return Boolean is
     (if Code > 255 then Compatibility_Code (Code) = 1
      else Code in 160 | 168 | 170 | 175 | 178 .. 181 | 184 .. 186 | 188 .. 190);

   function Is_NFKC (Code : Natural) return Boolean is
     (if Code > 255 then NFKC_Code (Code) = 1
      else Code not in 160 | 168 | 170 | 175 | 178 .. 181 | 184 .. 186 | 188 .. 190);

   function Simple_Upper (Code : Natural) return Natural is
     (if Code > 255 then Simple_Upper_Code (Code)
      elsif Code in 97 .. 122 | 224 .. 246 | 248 .. 254 then Code - 32
      elsif Code = 181 then 924
      elsif Code = 255 then 376
      else Code);

   function Simple_Lower (Code : Natural) return Natural is
     (if Code > 255 then Simple_Lower_Code (Code)
      elsif Code in 65 .. 90 | 192 .. 214 | 216 .. 222 then Code + 32
      else Code);

   function Basic (Code : Natural) return Natural is
     (if Code > 255 then Basic_Code (Code)
      else (case Code is
               when 192 .. 197 => 65, when 199 => 67, when 200 .. 203 => 69, when 204 .. 207 => 73,
               when 209 => 78, when 210 .. 214 => 79, when 217 .. 220 => 85, when 221 => 89,
               when 224 .. 229 => 97, when 231 => 99, when 232 .. 235 => 101, when 236 .. 239 => 105,
               when 241 => 110, when 242 .. 246 => 111, when 249 .. 252 => 117, when 253 | 255 => 121,
               when others => Code));
end Verada.Strings.Unicode;
