home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 3 / AACD03.BIN / AACD / Programming / sofa / archive / SmallEiffel.lha / SmallEiffel / lib_std / character.e < prev    next >
Text File  |  1999-06-05  |  9KB  |  363 lines

  1. -- This file is  free  software, which  comes  along  with  SmallEiffel. This
  2. -- software  is  distributed  in the hope that it will be useful, but WITHOUT 
  3. -- ANY  WARRANTY;  without  even  the  implied warranty of MERCHANTABILITY or
  4. -- FITNESS  FOR A PARTICULAR PURPOSE. You can modify it as you want, provided
  5. -- this header is kept unaltered, and a notification of the changes is added.
  6. -- You  are  allowed  to  redistribute  it and sell it, alone or as a part of 
  7. -- another product.
  8. --          Copyright (C) 1994-98 LORIA - UHP - CRIN - INRIA - FRANCE
  9. --            Dominique COLNET and Suzanne COLLIN - colnet@loria.fr 
  10. --                       http://SmallEiffel.loria.fr
  11. --
  12. expanded class CHARACTER
  13. --
  14. -- Note: An Eiffel CHARACTER is mapped as a C char or as a Java Byte.
  15. --
  16.  
  17. inherit 
  18.    CHARACTER_REF
  19.       redefine
  20.          infix "<", infix "<=", infix ">", infix ">=", code, to_lower, 
  21.          to_upper, out_in_tagged_out_memory, fill_tagged_out_memory,
  22.          hash_code
  23.       end;
  24.  
  25. feature 
  26.  
  27.    to_integer: INTEGER is
  28.          -- Sign-extended conversion.
  29.       external "SmallEiffel" 
  30.       end;
  31.  
  32.    code: INTEGER is
  33.          -- ASCII code of Current.
  34.          -- No Sign-extended conversion.
  35.       external "SmallEiffel" 
  36.       ensure
  37.          Result >= 0
  38.       end;
  39.  
  40.    infix "<" (other: CHARACTER): BOOLEAN is
  41.          -- Comparison using `code'. 
  42.       do
  43.          Result := code < other.code;
  44.       end;
  45.    
  46.    infix "<=" (other: CHARACTER): BOOLEAN is
  47.          -- Comparison using `code'. 
  48.       do
  49.          Result := code <= other.code;
  50.       end;
  51.    
  52.    infix ">" (other: CHARACTER): BOOLEAN is
  53.          -- Comparison using `code'. 
  54.       do
  55.          Result := code > other.code;
  56.       end;
  57.    
  58.    infix ">=" (other: CHARACTER): BOOLEAN is
  59.          -- Comparison using `code'. 
  60.       do
  61.          Result := code >= other.code;
  62.       end;
  63.    
  64.    value, decimal_value: INTEGER is
  65.          -- Gives the value of a decimal digit.
  66.       require
  67.          is_digit
  68.       do
  69.          Result := code - 48;
  70.       ensure
  71.          Result.in_range(0,9)
  72.       end;
  73.  
  74.    binary_value: INTEGER is
  75.          -- Gives the value of a binary digit.
  76.       require
  77.          is_binary_digit
  78.       do
  79.          Result := code - 48;
  80.       ensure
  81.          Result.in_range(0,1)
  82.       end;
  83.  
  84.    octal_value: INTEGER is
  85.          -- Gives the value of an octal digit.
  86.       require
  87.          is_octal_digit
  88.       do
  89.          Result := code - 48;
  90.       ensure
  91.          Result.in_range(0,7)
  92.       end;
  93.  
  94.    hexadecimal_value: INTEGER is
  95.          -- Gives the value of an hexadecimal digit.
  96.       require
  97.          is_hexadecimal_digit
  98.       do
  99.          if code < ('A').code then
  100.             Result := code - 48;
  101.          elseif code < ('a').code then
  102.             Result := code - 55;
  103.          else
  104.             Result := code - 87;
  105.          end;
  106.       ensure
  107.          Result.in_range(0,15)
  108.       end;
  109.  
  110.    same_as(other: CHARACTER): BOOLEAN is
  111.          -- Case insensitive comparison.
  112.          -- No difference between upper/lower case letters.
  113.       do
  114.          if Current = other then
  115.             Result := true;
  116.          else
  117.             inspect
  118.                code
  119.             when 65 .. 90 then
  120.                Result := code = other.code - 32;
  121.             when 97 .. 122 then
  122.                Result := code = other.code + 32;
  123.             else
  124.             end;
  125.          end;
  126.       ensure
  127.          Result implies to_lower = other or to_upper = other;
  128.       end ;
  129.    
  130.    to_upper: CHARACTER is
  131.          -- Conversion to the corresponding upper case.
  132.       do
  133.          if code < 97 then
  134.             Result := Current;
  135.          elseif code > 122 then
  136.             Result := Current;
  137.          else
  138.             Result := (code - 32).to_character;
  139.          end;
  140.       end;
  141.    
  142.    to_lower: CHARACTER is
  143.          -- Conversion to the corresponding lower case.
  144.       do
  145.          if code < 65 then
  146.             Result := Current;
  147.          elseif code > 90 then
  148.             Result := Current;
  149.          else
  150.             Result := (code + 32).to_character;
  151.          end;
  152.       end;
  153.    
  154.    is_letter: BOOLEAN is
  155.          -- Is it a letter ?
  156.       do
  157.          if Current >= 'a' then
  158.             Result := Current <= 'z';
  159.          elseif Current >= 'A' then
  160.             Result := Current <= 'Z';
  161.          end;
  162.       ensure
  163.          Result = in_range('A','Z') or in_range('a','z')
  164.       end;
  165.    
  166.    is_digit, is_decimal_digit: BOOLEAN is
  167.          -- Belongs to '0'..'9'.
  168.       do
  169.          if Current >= '0' then
  170.             Result := Current <= '9';
  171.          end;
  172.       ensure
  173.         Result = in_range('0','9')
  174.       end;
  175.    
  176.    is_binary_digit: BOOLEAN is
  177.          -- Belongs to '0'..'1'.
  178.       do
  179.          if Current >= '0' then
  180.             Result := Current <= '1';
  181.          end;
  182.       ensure
  183.          Result = in_range('0','1')
  184.       end;
  185.    
  186.    is_octal_digit: BOOLEAN is
  187.          -- Belongs to '0'..'7'.
  188.       do
  189.          if Current >= '0' then
  190.             Result := Current <= '7';
  191.          end;
  192.       ensure
  193.          Result = in_range('0','7')
  194.       end;
  195.    
  196.    is_hexadecimal_digit: BOOLEAN is 
  197.          -- Is it one character of "0123456789abcdefABCDEF" ?
  198.       do  
  199.          if is_digit then
  200.             Result := true;
  201.          elseif Current >= 'a' then
  202.             Result := Current <= 'f';
  203.          elseif Current >= 'A' then
  204.             Result := Current <= 'F';
  205.          end;
  206.       ensure
  207.          Result =  ("0123456789abcdefABCDEF").has(Current)
  208.       end; 
  209.  
  210.    is_lower: BOOLEAN is 
  211.          -- Is `item' lowercase?
  212.       do  
  213.          inspect 
  214.             Current
  215.          when 'a'..'z' then 
  216.             Result := true;
  217.          else 
  218.          end; 
  219.       end; 
  220.    
  221.    is_upper: BOOLEAN is 
  222.          -- Is `item' uppercase?
  223.       do  
  224.          inspect 
  225.             Current
  226.          when 'A'..'Z' then 
  227.             Result := true;
  228.          else 
  229.          end; 
  230.       end; 
  231.    
  232.    is_separator: BOOLEAN is
  233.          -- True when character is a separator.
  234.       do
  235.          inspect
  236.             Current
  237.          when ' ','%T','%N','%R','%U' then
  238.             Result := true;
  239.          else
  240.          end;
  241.       end;
  242.    
  243.    is_letter_or_digit: BOOLEAN is 
  244.       -- Is character a letter or digit ?
  245.       do  
  246.          Result := is_letter or else is_digit;
  247.       ensure 
  248.          valid: Result = (is_letter or is_digit); 
  249.       end; 
  250.  
  251.    is_hex_digit: BOOLEAN is 
  252.       obsolete "This feature will be soon removed.%
  253.                %Since release -0.78, the new name for this feature %
  254.                %is `is_hexadecimal_digit'. Please, update your code."
  255.       do  
  256.          Result := is_hexadecimal_digit;
  257.       end; 
  258.  
  259.    is_ascii: BOOLEAN is 
  260.          -- Is character a 8-bit ASCII character?
  261.       do  
  262.          inspect 
  263.             Current
  264.          when '%U'..'%/255/' then 
  265.             Result := true;
  266.          else 
  267.          end; 
  268.       end; 
  269.  
  270.    is_bit: BOOLEAN is
  271.          -- True for `0' and `1'.
  272.       do
  273.          inspect
  274.             Current
  275.          when '0','1' then
  276.             Result := true;
  277.          else
  278.          end;
  279.       end;
  280.  
  281.    next: CHARACTER is
  282.          -- Give the next character (the following `code');
  283.       do
  284.          Result := (code + 1).to_character;
  285.       end;
  286.  
  287.    previous: CHARACTER is
  288.          -- Give the next character (the following `code');
  289.       require
  290.          code > 0
  291.       do
  292.          Result := (code - 1).to_character;
  293.       end;
  294.  
  295. feature -- Conversions :
  296.  
  297.    to_bit: BIT Character_bits is
  298.       external "SmallEiffel"
  299.       end;
  300.  
  301.    to_octal: INTEGER is
  302.       obsolete "This strange feature will be removed in release -0.77. %
  303.                %Please update your code."
  304.       do
  305.          Result := code.to_octal;
  306.       end;
  307.  
  308.    to_hexadecimal: STRING is
  309.          -- Create a new STRING giving the `code' in hexadecimal.
  310.          -- For example :
  311.          --    (255).to_character.to_hexadecimal gives "FF".
  312.          -- Note: see `to_hexadecimal_in' to save memory.
  313.       do
  314.          !!Result.make(2);
  315.          to_hexadecimal_in(Result);
  316.       ensure
  317.          Result.count = 2
  318.       end;
  319.  
  320.    to_hexadecimal_in(str: STRING) is
  321.          -- Append the equivalent of `to_hexadecimal' at the end of 
  322.          -- `str'. Thus you can save memory because no other
  323.          -- STRING is allocate for the job.
  324.       local
  325.          c: CHARACTER;
  326.       do
  327.          c := ((to_bit and 11110000B) @>> 4).to_character;
  328.          inspect
  329.             c.code
  330.          when 0 .. 9 then
  331.             str.extend((('0').code + c.code).to_character);
  332.          else
  333.             str.extend(((('A').code - 10) + c.code).to_character);
  334.          end;
  335.          c := (to_bit and 00001111B).to_character;
  336.          inspect
  337.             c.code
  338.          when 0 .. 9 then
  339.             str.extend((('0').code + c.code).to_character);
  340.          else
  341.             str.extend(((('A').code - 10) + c.code).to_character);
  342.          end;
  343.       ensure
  344.          str.count = 2 + old str.count
  345.       end;
  346.  
  347. feature -- Object Printing :
  348.  
  349.    out_in_tagged_out_memory, fill_tagged_out_memory is
  350.       do
  351.          tagged_out_memory.extend(Current);
  352.       end;
  353.    
  354. feature -- Hashing :
  355.    
  356.    hash_code: INTEGER is
  357.       do
  358.          Result := code;
  359.       end;
  360.  
  361. end -- CHARACTER
  362.  
  363.