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-strmap.adb < prev    next >
Text File  |  2000-07-19  |  10KB  |  342 lines

  1. ------------------------------------------------------------------------------
  2. --                                                                          --
  3. --                         GNAT RUNTIME COMPONENTS                          --
  4. --                                                                          --
  5. --                     A D A . S T R I N G S . M A P S                      --
  6. --                                                                          --
  7. --                                 B o d y                                  --
  8. --                                                                          --
  9. --                            $Revision: 1.17 $                             --
  10. --                                                                          --
  11. --   Copyright (C) 1992,1993,1994,1995,1996 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. --  Note: parts of this code are derived from the ADAR.CSH public domain
  37. --  Ada 83 versions of the Appendix C string handling packages. The main
  38. --  differences are that we avoid the use of the minimize function which
  39. --  is bit-by-bit or character-by-character and therefore rather slow.
  40. --  Generally for character sets we favor the full 32-byte representation.
  41.  
  42. package body Ada.Strings.Maps is
  43.  
  44.    use Ada.Characters.Latin_1;
  45.  
  46.    ---------
  47.    -- "=" --
  48.    ---------
  49.  
  50.    function "=" (Left, Right : in Character_Set) return Boolean is
  51.    begin
  52.       return Character_Set_Internal (Left) = Character_Set_Internal (Right);
  53.    end "=";
  54.  
  55.    -----------
  56.    -- "and" --
  57.    -----------
  58.  
  59.    function "and" (Left, Right : in Character_Set) return Character_Set is
  60.    begin
  61.       return Character_Set
  62.         (Character_Set_Internal (Left) and Character_Set_Internal (Right));
  63.    end "and";
  64.  
  65.    -----------
  66.    -- "not" --
  67.    -----------
  68.  
  69.    function "not" (Right : in Character_Set) return Character_Set is
  70.    begin
  71.       return Character_Set (not Character_Set_Internal (Right));
  72.    end "not";
  73.  
  74.    ----------
  75.    -- "or" --
  76.    ----------
  77.  
  78.    function "or" (Left, Right : in Character_Set) return Character_Set is
  79.    begin
  80.       return Character_Set
  81.         (Character_Set_Internal (Left) or Character_Set_Internal (Right));
  82.    end "or";
  83.  
  84.    -----------
  85.    -- "xor" --
  86.    -----------
  87.  
  88.    function "xor" (Left, Right : in Character_Set) return Character_Set is
  89.    begin
  90.       return Character_Set
  91.         (Character_Set_Internal (Left) xor Character_Set_Internal (Right));
  92.    end "xor";
  93.  
  94.    ------------
  95.    -- To_Set --
  96.    ------------
  97.  
  98.    function To_Set (Ranges : in Character_Ranges) return Character_Set is
  99.       Result : Character_Set;
  100.  
  101.    begin
  102.       for C in Result'Range loop
  103.          Result (C) := False;
  104.       end loop;
  105.  
  106.       for R in Ranges'Range loop
  107.          for C in Ranges (R).Low .. Ranges (R).High loop
  108.             Result (C) := True;
  109.          end loop;
  110.       end loop;
  111.  
  112.       return Result;
  113.    end To_Set;
  114.  
  115.    function To_Set (Span   : in Character_Range) return Character_Set is
  116.       Result : Character_Set;
  117.  
  118.    begin
  119.       for C in Result'Range loop
  120.          Result (C) := False;
  121.       end loop;
  122.  
  123.       for C in Span.Low .. Span.High loop
  124.          Result (C) := True;
  125.       end loop;
  126.  
  127.       return Result;
  128.    end To_Set;
  129.  
  130.    ---------------
  131.    -- To_Ranges --
  132.    ---------------
  133.  
  134.    function To_Ranges (Set : in Character_Set) return Character_Ranges is
  135.       Max_Ranges : Character_Ranges (1 .. Set'Length / 2 + 1);
  136.       Range_Num  : Natural;
  137.       C          : Character;
  138.  
  139.    begin
  140.       C := Character'First;
  141.       Range_Num := 0;
  142.  
  143.       loop
  144.          --  Skip gap between subsets.
  145.  
  146.          while not Set (C) loop
  147.             exit when C = Character'Last;
  148.             C := Character'Succ (C);
  149.          end loop;
  150.  
  151.          exit when not Set (C);
  152.  
  153.          Range_Num := Range_Num + 1;
  154.          Max_Ranges (Range_Num).Low := C;
  155.  
  156.          --  Span a subset.
  157.  
  158.          loop
  159.             exit when not Set (C) or else C = Character'Last;
  160.             C := Character' Succ (C);
  161.          end loop;
  162.  
  163.          if Set (C) then
  164.             Max_Ranges (Range_Num). High := C;
  165.             exit;
  166.          else
  167.             Max_Ranges (Range_Num). High := Character'Pred (C);
  168.          end if;
  169.       end loop;
  170.  
  171.       return Max_Ranges (1 .. Range_Num);
  172.    end To_Ranges;
  173.  
  174.    ---------
  175.    -- "-" --
  176.    ---------
  177.  
  178.    function "-" (Left, Right : Character_Set) return Character_Set is
  179.    begin
  180.       return Left and not Right;
  181.    end "-";
  182.  
  183.    -----------
  184.    -- Is_In --
  185.    -----------
  186.  
  187.    function Is_In
  188.      (Element : Character;
  189.       Set     : Character_Set)
  190.       return    Boolean
  191.    is
  192.    begin
  193.       return Set (Element);
  194.    end Is_In;
  195.  
  196.    ---------------
  197.    -- Is_Subset --
  198.    ---------------
  199.  
  200.    function Is_Subset
  201.      (Elements : Character_Set;
  202.       Set      : Character_Set)
  203.       return     Boolean
  204.    is
  205.    begin
  206.       return (Elements and Set) = Elements;
  207.    end Is_Subset;
  208.  
  209.    ----------------
  210.    -- To_Mapping --
  211.    ----------------
  212.  
  213.    function To_Mapping
  214.      (From, To : in Character_Sequence)
  215.       return     Character_Mapping
  216.    is
  217.       Result   : Character_Mapping;
  218.       Inserted : Character_Set := Null_Set;
  219.       From_Len : constant Natural := From'Length;
  220.       To_Len   : constant Natural := To'Length;
  221.  
  222.    begin
  223.       if From_Len /= To_Len then
  224.          raise Strings.Translation_Error;
  225.       end if;
  226.  
  227.       for Char in Character loop
  228.          Result (Char) := Char;
  229.       end loop;
  230.  
  231.       for J in From'Range loop
  232.          if Inserted (From (J)) then
  233.             raise Strings.Translation_Error;
  234.          end if;
  235.  
  236.          Result   (From (J)) := To (J - From'First + To'First);
  237.          Inserted (From (J)) := True;
  238.       end loop;
  239.  
  240.       return Result;
  241.    end To_Mapping;
  242.  
  243.    -----------------
  244.    -- To_Sequence --
  245.    -----------------
  246.  
  247.    function To_Sequence
  248.      (Set  : Character_Set)
  249.       return Character_Sequence
  250.    is
  251.       Result : String (1 .. Character'Pos (Character'Last));
  252.       Count  : Natural := 0;
  253.  
  254.    begin
  255.       for Char in Set'Range loop
  256.          if Set (Char) then
  257.             Count := Count + 1;
  258.             Result (Count) := Char;
  259.          end if;
  260.       end loop;
  261.  
  262.       return Result (1 .. Count);
  263.    end To_Sequence;
  264.  
  265.    ------------
  266.    -- To_Set --
  267.    ------------
  268.  
  269.    function To_Set (Sequence : Character_Sequence) return Character_Set is
  270.       Result : Character_Set := Null_Set;
  271.  
  272.    begin
  273.       for J in Sequence'Range loop
  274.          Result (Sequence (J)) := True;
  275.       end loop;
  276.  
  277.       return Result;
  278.    end To_Set;
  279.  
  280.    function To_Set (Singleton : Character) return Character_Set is
  281.       Result : Character_Set := Null_Set;
  282.  
  283.    begin
  284.       Result (Singleton) := True;
  285.       return Result;
  286.    end To_Set;
  287.  
  288.    -----------
  289.    -- Value --
  290.    -----------
  291.  
  292.    function Value (Map : in Character_Mapping; Element : in Character)
  293.       return Character is
  294.  
  295.    begin
  296.       return Map (Element);
  297.    end Value;
  298.  
  299.    ---------------
  300.    -- To_Domain --
  301.    ---------------
  302.  
  303.    function To_Domain (Map : in Character_Mapping) return Character_Sequence
  304.    is
  305.       Result : String (1 .. Map'Length);
  306.       J      : Natural;
  307.  
  308.    begin
  309.       J := 0;
  310.       for C in Map'Range loop
  311.          if Map (C) /= C then
  312.             J := J + 1;
  313.             Result (J) := C;
  314.          end if;
  315.       end loop;
  316.  
  317.       return Result (1 .. J);
  318.    end To_Domain;
  319.  
  320.    --------------
  321.    -- To_Range --
  322.    --------------
  323.  
  324.    function To_Range (Map : in Character_Mapping) return Character_Sequence
  325.    is
  326.       Result : String (1 .. Map'Length);
  327.       J      : Natural;
  328.  
  329.    begin
  330.       J := 0;
  331.       for C in Map'Range loop
  332.          if Map (C) /= C then
  333.             J := J + 1;
  334.             Result (J) := Map (C);
  335.          end if;
  336.       end loop;
  337.  
  338.       return Result (1 .. J);
  339.    end To_Range;
  340.  
  341. end Ada.Strings.Maps;
  342.