home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / adav313.zip / gnat-3_13p-os2-bin-20010916.zip / emx / gnatlib / a-chahan.adb < prev    next >
Text File  |  2000-07-19  |  19KB  |  587 lines

  1. ------------------------------------------------------------------------------
  2. --                                                                          --
  3. --                         GNAT RUN-TIME COMPONENTS                         --
  4. --                                                                          --
  5. --              A D A . C H A R A C T E R S . H A N D L I N G               --
  6. --                                                                          --
  7. --                                 B o d y                                  --
  8. --                                                                          --
  9. --                            $Revision: 1.17 $                             --
  10. --                                                                          --
  11. --          Copyright (C) 1992-1998 Free Software Foundation, Inc.          --
  12. --                                                                          --
  13. -- GNAT is free software;  you can  redistribute it  and/or modify it under --
  14. -- terms of the  GNU General Public License as published  by the Free Soft- --
  15. -- ware  Foundation;  either version 2,  or (at your option) any later ver- --
  16. -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
  17. -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
  18. -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
  19. -- for  more details.  You should have  received  a copy of the GNU General --
  20. -- Public License  distributed with GNAT;  see file COPYING.  If not, write --
  21. -- to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston, --
  22. -- MA 02111-1307, USA.                                                      --
  23. --                                                                          --
  24. -- As a special exception,  if other files  instantiate  generics from this --
  25. -- unit, or you link  this unit with other files  to produce an executable, --
  26. -- this  unit  does not  by itself cause  the resulting  executable  to  be --
  27. -- covered  by the  GNU  General  Public  License.  This exception does not --
  28. -- however invalidate  any other reasons why  the executable file  might be --
  29. -- covered by the  GNU Public License.                                      --
  30. --                                                                          --
  31. -- GNAT was originally developed  by the GNAT team at  New York University. --
  32. -- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
  33. --                                                                          --
  34. ------------------------------------------------------------------------------
  35.  
  36. with Ada.Characters.Latin_1;      use Ada.Characters.Latin_1;
  37. with Ada.Strings.Maps;            use Ada.Strings.Maps;
  38. with Ada.Strings.Maps.Constants;  use Ada.Strings.Maps.Constants;
  39.  
  40. package body Ada.Characters.Handling is
  41. pragma Preelaborate (Handling);
  42.  
  43.    ------------------------------------
  44.    -- Character Classification Table --
  45.    ------------------------------------
  46.  
  47.    type Character_Flags is mod 256;
  48.    for Character_Flags'Size use 8;
  49.  
  50.    Control    : constant Character_Flags := 1;
  51.    Lower      : constant Character_Flags := 2;
  52.    Upper      : constant Character_Flags := 4;
  53.    Basic      : constant Character_Flags := 8;
  54.    Hex_Digit  : constant Character_Flags := 16;
  55.    Digit      : constant Character_Flags := 32;
  56.    Special    : constant Character_Flags := 64;
  57.  
  58.    Letter     : constant Character_Flags := Lower or Upper;
  59.    Alphanum   : constant Character_Flags := Letter or Digit;
  60.    Graphic    : constant Character_Flags := Alphanum or Special;
  61.  
  62.    Char_Map : constant array (Character) of Character_Flags :=
  63.    (
  64.      NUL                         => Control,
  65.      SOH                         => Control,
  66.      STX                         => Control,
  67.      ETX                         => Control,
  68.      EOT                         => Control,
  69.      ENQ                         => Control,
  70.      ACK                         => Control,
  71.      BEL                         => Control,
  72.      BS                          => Control,
  73.      HT                          => Control,
  74.      LF                          => Control,
  75.      VT                          => Control,
  76.      FF                          => Control,
  77.      CR                          => Control,
  78.      SO                          => Control,
  79.      SI                          => Control,
  80.  
  81.      DLE                         => Control,
  82.      DC1                         => Control,
  83.      DC2                         => Control,
  84.      DC3                         => Control,
  85.      DC4                         => Control,
  86.      NAK                         => Control,
  87.      SYN                         => Control,
  88.      ETB                         => Control,
  89.      CAN                         => Control,
  90.      EM                          => Control,
  91.      SUB                         => Control,
  92.      ESC                         => Control,
  93.      FS                          => Control,
  94.      GS                          => Control,
  95.      RS                          => Control,
  96.      US                          => Control,
  97.  
  98.      Space                       => Special,
  99.      Exclamation                 => Special,
  100.      Quotation                   => Special,
  101.      Number_Sign                 => Special,
  102.      Dollar_Sign                 => Special,
  103.      Percent_Sign                => Special,
  104.      Ampersand                   => Special,
  105.      Apostrophe                  => Special,
  106.      Left_Parenthesis            => Special,
  107.      Right_Parenthesis           => Special,
  108.      Asterisk                    => Special,
  109.      Plus_Sign                   => Special,
  110.      Comma                       => Special,
  111.      Hyphen                      => Special,
  112.      Full_Stop                   => Special,
  113.      Solidus                     => Special,
  114.  
  115.      '0' .. '9'                  => Digit + Hex_Digit,
  116.  
  117.      Colon                       => Special,
  118.      Semicolon                   => Special,
  119.      Less_Than_Sign              => Special,
  120.      Equals_Sign                 => Special,
  121.      Greater_Than_Sign           => Special,
  122.      Question                    => Special,
  123.      Commercial_At               => Special,
  124.  
  125.      'A' .. 'F'                  => Upper + Basic + Hex_Digit,
  126.      'G' .. 'Z'                  => Upper + Basic,
  127.  
  128.      Left_Square_Bracket         => Special,
  129.      Reverse_Solidus             => Special,
  130.      Right_Square_Bracket        => Special,
  131.      Circumflex                  => Special,
  132.      Low_Line                    => Special,
  133.      Grave                       => Special,
  134.  
  135.      'a' .. 'f'                  => Lower + Basic + Hex_Digit,
  136.      'g' .. 'z'                  => Lower + Basic,
  137.  
  138.      Left_Curly_Bracket          => Special,
  139.      Vertical_Line               => Special,
  140.      Right_Curly_Bracket         => Special,
  141.      Tilde                       => Special,
  142.  
  143.      DEL                         => Control,
  144.      Reserved_128                => Control,
  145.      Reserved_129                => Control,
  146.      BPH                         => Control,
  147.      NBH                         => Control,
  148.      Reserved_132                => Control,
  149.      NEL                         => Control,
  150.      SSA                         => Control,
  151.      ESA                         => Control,
  152.      HTS                         => Control,
  153.      HTJ                         => Control,
  154.      VTS                         => Control,
  155.      PLD                         => Control,
  156.      PLU                         => Control,
  157.      RI                          => Control,
  158.      SS2                         => Control,
  159.      SS3                         => Control,
  160.  
  161.      DCS                         => Control,
  162.      PU1                         => Control,
  163.      PU2                         => Control,
  164.      STS                         => Control,
  165.      CCH                         => Control,
  166.      MW                          => Control,
  167.      SPA                         => Control,
  168.      EPA                         => Control,
  169.  
  170.      SOS                         => Control,
  171.      Reserved_153                => Control,
  172.      SCI                         => Control,
  173.      CSI                         => Control,
  174.      ST                          => Control,
  175.      OSC                         => Control,
  176.      PM                          => Control,
  177.      APC                         => Control,
  178.  
  179.      No_Break_Space              => Special,
  180.      Inverted_Exclamation        => Special,
  181.      Cent_Sign                   => Special,
  182.      Pound_Sign                  => Special,
  183.      Currency_Sign               => Special,
  184.      Yen_Sign                    => Special,
  185.      Broken_Bar                  => Special,
  186.      Section_Sign                => Special,
  187.      Diaeresis                   => Special,
  188.      Copyright_Sign              => Special,
  189.      Feminine_Ordinal_Indicator  => Special,
  190.      Left_Angle_Quotation        => Special,
  191.      Not_Sign                    => Special,
  192.      Soft_Hyphen                 => Special,
  193.      Registered_Trade_Mark_Sign  => Special,
  194.      Macron                      => Special,
  195.      Degree_Sign                 => Special,
  196.      Plus_Minus_Sign             => Special,
  197.      Superscript_Two             => Special,
  198.      Superscript_Three           => Special,
  199.      Acute                       => Special,
  200.      Micro_Sign                  => Special,
  201.      Pilcrow_Sign                => Special,
  202.      Middle_Dot                  => Special,
  203.      Cedilla                     => Special,
  204.      Superscript_One             => Special,
  205.      Masculine_Ordinal_Indicator => Special,
  206.      Right_Angle_Quotation       => Special,
  207.      Fraction_One_Quarter        => Special,
  208.      Fraction_One_Half           => Special,
  209.      Fraction_Three_Quarters     => Special,
  210.      Inverted_Question           => Special,
  211.  
  212.      UC_A_Grave                  => Upper,
  213.      UC_A_Acute                  => Upper,
  214.      UC_A_Circumflex             => Upper,
  215.      UC_A_Tilde                  => Upper,
  216.      UC_A_Diaeresis              => Upper,
  217.      UC_A_Ring                   => Upper,
  218.      UC_AE_Diphthong             => Upper + Basic,
  219.      UC_C_Cedilla                => Upper,
  220.      UC_E_Grave                  => Upper,
  221.      UC_E_Acute                  => Upper,
  222.      UC_E_Circumflex             => Upper,
  223.      UC_E_Diaeresis              => Upper,
  224.      UC_I_Grave                  => Upper,
  225.      UC_I_Acute                  => Upper,
  226.      UC_I_Circumflex             => Upper,
  227.      UC_I_Diaeresis              => Upper,
  228.      UC_Icelandic_Eth            => Upper + Basic,
  229.      UC_N_Tilde                  => Upper,
  230.      UC_O_Grave                  => Upper,
  231.      UC_O_Acute                  => Upper,
  232.      UC_O_Circumflex             => Upper,
  233.      UC_O_Tilde                  => Upper,
  234.      UC_O_Diaeresis              => Upper,
  235.  
  236.      Multiplication_Sign         => Special,
  237.  
  238.      UC_O_Oblique_Stroke         => Upper,
  239.      UC_U_Grave                  => Upper,
  240.      UC_U_Acute                  => Upper,
  241.      UC_U_Circumflex             => Upper,
  242.      UC_U_Diaeresis              => Upper,
  243.      UC_Y_Acute                  => Upper,
  244.      UC_Icelandic_Thorn          => Upper + Basic,
  245.  
  246.      LC_German_Sharp_S           => Lower + Basic,
  247.      LC_A_Grave                  => Lower,
  248.      LC_A_Acute                  => Lower,
  249.      LC_A_Circumflex             => Lower,
  250.      LC_A_Tilde                  => Lower,
  251.      LC_A_Diaeresis              => Lower,
  252.      LC_A_Ring                   => Lower,
  253.      LC_AE_Diphthong             => Lower + Basic,
  254.      LC_C_Cedilla                => Lower,
  255.      LC_E_Grave                  => Lower,
  256.      LC_E_Acute                  => Lower,
  257.      LC_E_Circumflex             => Lower,
  258.      LC_E_Diaeresis              => Lower,
  259.      LC_I_Grave                  => Lower,
  260.      LC_I_Acute                  => Lower,
  261.      LC_I_Circumflex             => Lower,
  262.      LC_I_Diaeresis              => Lower,
  263.      LC_Icelandic_Eth            => Lower + Basic,
  264.      LC_N_Tilde                  => Lower,
  265.      LC_O_Grave                  => Lower,
  266.      LC_O_Acute                  => Lower,
  267.      LC_O_Circumflex             => Lower,
  268.      LC_O_Tilde                  => Lower,
  269.      LC_O_Diaeresis              => Lower,
  270.  
  271.      Division_Sign               => Special,
  272.  
  273.      LC_O_Oblique_Stroke         => Lower,
  274.      LC_U_Grave                  => Lower,
  275.      LC_U_Acute                  => Lower,
  276.      LC_U_Circumflex             => Lower,
  277.      LC_U_Diaeresis              => Lower,
  278.      LC_Y_Acute                  => Lower,
  279.      LC_Icelandic_Thorn          => Lower + Basic,
  280.      LC_Y_Diaeresis              => Lower
  281.    );
  282.  
  283.    ---------------------
  284.    -- Is_Alphanumeric --
  285.    ---------------------
  286.  
  287.    function Is_Alphanumeric (Item : in Character) return Boolean is
  288.    begin
  289.       return (Char_Map (Item) and Alphanum) /= 0;
  290.    end Is_Alphanumeric;
  291.  
  292.    --------------
  293.    -- Is_Basic --
  294.    --------------
  295.  
  296.    function Is_Basic (Item : in Character) return Boolean is
  297.    begin
  298.       return (Char_Map (Item) and Basic) /= 0;
  299.    end Is_Basic;
  300.  
  301.    ------------------
  302.    -- Is_Character --
  303.    ------------------
  304.  
  305.    function Is_Character (Item : in Wide_Character) return Boolean is
  306.    begin
  307.       return Wide_Character'Pos (Item) < 256;
  308.    end Is_Character;
  309.  
  310.    ----------------
  311.    -- Is_Control --
  312.    ----------------
  313.  
  314.    function Is_Control (Item : in Character) return Boolean is
  315.    begin
  316.       return (Char_Map (Item) and Control) /= 0;
  317.    end Is_Control;
  318.  
  319.    --------------
  320.    -- Is_Digit --
  321.    --------------
  322.  
  323.    function Is_Digit (Item : in Character) return Boolean is
  324.    begin
  325.       return Item in '0' .. '9';
  326.    end Is_Digit;
  327.  
  328.    --------------------------
  329.    -- Is_Hexadecimal_Digit --
  330.    --------------------------
  331.  
  332.    function Is_Hexadecimal_Digit (Item : in Character) return Boolean is
  333.    begin
  334.       return (Char_Map (Item) and Hex_Digit) /= 0;
  335.    end Is_Hexadecimal_Digit;
  336.  
  337.    ----------------
  338.    -- Is_ISO_646 --
  339.    ----------------
  340.  
  341.    function Is_ISO_646 (Item : in Character) return Boolean is
  342.    begin
  343.       return Item in ISO_646;
  344.    end Is_ISO_646;
  345.  
  346.    --  Note: much more efficient coding of the following function is possible
  347.    --  by testing several 16#80# bits in a complete word in a single operation
  348.  
  349.    function Is_ISO_646 (Item : in String) return Boolean is
  350.    begin
  351.       for J in Item'Range loop
  352.          if Item (J) not in ISO_646 then
  353.             return False;
  354.          end if;
  355.       end loop;
  356.  
  357.       return True;
  358.    end Is_ISO_646;
  359.  
  360.    ----------------
  361.    -- Is_Graphic --
  362.    ----------------
  363.  
  364.    function Is_Graphic (Item : in Character) return Boolean is
  365.    begin
  366.       return (Char_Map (Item) and Graphic) /= 0;
  367.    end Is_Graphic;
  368.  
  369.    ---------------
  370.    -- Is_Letter --
  371.    ---------------
  372.  
  373.    function Is_Letter (Item : in Character) return Boolean is
  374.    begin
  375.       return (Char_Map (Item) and Letter) /= 0;
  376.    end Is_Letter;
  377.  
  378.    --------------
  379.    -- Is_Lower --
  380.    --------------
  381.  
  382.    function Is_Lower (Item : in Character) return Boolean is
  383.    begin
  384.       return (Char_Map (Item) and Lower) /= 0;
  385.    end Is_Lower;
  386.  
  387.    ----------------
  388.    -- Is_Special --
  389.    ----------------
  390.  
  391.    function Is_Special (Item : in Character) return Boolean is
  392.    begin
  393.       return (Char_Map (Item) and Special) /= 0;
  394.    end Is_Special;
  395.  
  396.    ---------------
  397.    -- Is_String --
  398.    ---------------
  399.  
  400.    function Is_String (Item : in Wide_String) return Boolean is
  401.    begin
  402.       for J in Item'Range loop
  403.          if Wide_Character'Pos (Item (J)) >= 256 then
  404.             return False;
  405.          end if;
  406.       end loop;
  407.  
  408.       return True;
  409.    end Is_String;
  410.  
  411.    --------------
  412.    -- Is_Upper --
  413.    --------------
  414.  
  415.    function Is_Upper (Item : in Character) return Boolean is
  416.    begin
  417.       return (Char_Map (Item) and Upper) /= 0;
  418.    end Is_Upper;
  419.  
  420.    --------------
  421.    -- To_Basic --
  422.    --------------
  423.  
  424.    function To_Basic (Item : in Character) return Character is
  425.    begin
  426.       return Value (Basic_Map, Item);
  427.    end To_Basic;
  428.  
  429.    function To_Basic (Item : in String) return String is
  430.       Result : String (1 .. Item'Length);
  431.  
  432.    begin
  433.       for J in Item'Range loop
  434.          Result (J - (Item'First - 1)) := Value (Basic_Map, Item (J));
  435.       end loop;
  436.  
  437.       return Result;
  438.    end To_Basic;
  439.  
  440.    ------------------
  441.    -- To_Character --
  442.    ------------------
  443.  
  444.    function To_Character
  445.      (Item       : in Wide_Character;
  446.       Substitute : in Character := ' ')
  447.       return       Character
  448.    is
  449.    begin
  450.       if Is_Character (Item) then
  451.          return Character'Val (Wide_Character'Pos (Item));
  452.       else
  453.          return Substitute;
  454.       end if;
  455.    end To_Character;
  456.  
  457.    ----------------
  458.    -- To_ISO_646 --
  459.    ----------------
  460.  
  461.    function To_ISO_646
  462.      (Item       : in Character;
  463.       Substitute : in ISO_646 := ' ')
  464.       return       ISO_646
  465.    is
  466.    begin
  467.       if Item in ISO_646 then
  468.          return Item;
  469.       else
  470.          return Substitute;
  471.       end if;
  472.    end To_ISO_646;
  473.  
  474.    function To_ISO_646
  475.      (Item       : in String;
  476.       Substitute : in ISO_646 := ' ')
  477.       return       String
  478.    is
  479.       Result : String (1 .. Item'Length);
  480.  
  481.    begin
  482.       for J in Item'Range loop
  483.          if Item (J) in ISO_646 then
  484.             Result (J - (Item'First - 1)) := Item (J);
  485.          else
  486.             Result (J - (Item'First - 1)) := Substitute;
  487.          end if;
  488.       end loop;
  489.  
  490.       return Result;
  491.    end To_ISO_646;
  492.  
  493.    --------------
  494.    -- To_Lower --
  495.    --------------
  496.  
  497.    function To_Lower (Item : in Character) return Character is
  498.    begin
  499.       return Value (Lower_Case_Map, Item);
  500.    end To_Lower;
  501.  
  502.    function To_Lower (Item : in String) return String is
  503.       Result : String (1 .. Item'Length);
  504.  
  505.    begin
  506.       for J in Item'Range loop
  507.          Result (J - (Item'First - 1)) := Value (Lower_Case_Map, Item (J));
  508.       end loop;
  509.  
  510.       return Result;
  511.    end To_Lower;
  512.  
  513.    ---------------
  514.    -- To_String --
  515.    ---------------
  516.  
  517.    function To_String
  518.      (Item       : in Wide_String;
  519.       Substitute : in Character := ' ')
  520.      return        String
  521.    is
  522.       Result : String (1 .. Item'Length);
  523.  
  524.    begin
  525.       for J in Item'Range loop
  526.          Result (J - (Item'First - 1)) := To_Character (Item (J), Substitute);
  527.       end loop;
  528.       return Result;
  529.    end To_String;
  530.  
  531.    --------------
  532.    -- To_Upper --
  533.    --------------
  534.  
  535.    function To_Upper
  536.      (Item : in Character)
  537.      return  Character
  538.    is
  539.    begin
  540.       return Value (Upper_Case_Map, Item);
  541.    end To_Upper;
  542.  
  543.    function To_Upper
  544.      (Item : in String)
  545.       return String
  546.    is
  547.       Result : String (1 .. Item'Length);
  548.  
  549.    begin
  550.       for J in Item'Range loop
  551.          Result (J - (Item'First - 1)) := Value (Upper_Case_Map, Item (J));
  552.       end loop;
  553.  
  554.       return Result;
  555.    end To_Upper;
  556.  
  557.    -----------------------
  558.    -- To_Wide_Character --
  559.    -----------------------
  560.  
  561.    function To_Wide_Character
  562.      (Item : in Character)
  563.       return Wide_Character
  564.    is
  565.    begin
  566.       return Wide_Character'Val (Character'Pos (Item));
  567.    end To_Wide_Character;
  568.  
  569.    --------------------
  570.    -- To_Wide_String --
  571.    --------------------
  572.  
  573.    function To_Wide_String
  574.      (Item : in String)
  575.       return Wide_String
  576.    is
  577.       Result : Wide_String (1 .. Item'Length);
  578.  
  579.    begin
  580.       for J in Item'Range loop
  581.          Result (J - (Item'First - 1)) := To_Wide_Character (Item (J));
  582.       end loop;
  583.  
  584.       return Result;
  585.    end To_Wide_String;
  586. end Ada.Characters.Handling;
  587.