home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Environments / Small Eiffel 0.4.8 / lib_std / character.e < prev    next >
Encoding:
Text File  |  1997-04-13  |  4.6 KB  |  229 lines  |  [TEXT/ttxt]

  1. -- Part of SmallEiffel -- Read DISCLAIMER file -- Copyright (C) 
  2. -- Dominique COLNET and Suzanne COLLIN -- colnet@loria.fr
  3. --
  4. expanded class CHARACTER
  5. --
  6. -- Note : corresponding C type is "char"
  7. --
  8. inherit 
  9.    CHARACTER_REF
  10.       redefine
  11.      infix "<", infix "<=", infix ">", infix ">=", compare, 
  12.      code, to_lower, to_upper, out_in_tagged_out_memory, 
  13.      fill_tagged_out_memory
  14.       end;
  15.  
  16. feature 
  17.  
  18.    code: INTEGER is
  19.      -- ASCII code of Current.
  20.       external "CSE" 
  21.       end;
  22.    
  23.    infix "<" (other: CHARACTER): BOOLEAN is
  24.      -- Comparison using `code'. 
  25.       do
  26.      Result := code < other.code;
  27.       end;
  28.    
  29.    infix "<="(other: CHARACTER): BOOLEAN is
  30.      -- Comparison using `code'. 
  31.       do
  32.      Result := code <= other.code;
  33.       end;
  34.    
  35.    infix ">"(other: CHARACTER): BOOLEAN is
  36.      -- Comparison using `code'. 
  37.       do
  38.      Result := code > other.code;
  39.       end;
  40.    
  41.    infix ">="(other: CHARACTER): BOOLEAN is
  42.      -- Comparison using `code'. 
  43.       do
  44.      Result := code >= other.code;
  45.       end;
  46.    
  47.    compare (other: CHARACTER): INTEGER is
  48.      -- Compare Current with `other'.
  49.      -- '<' <==> Result < 0
  50.      -- '>' <==> Result > 0
  51.      -- Otherwise Result = 0
  52.       do
  53.      Result := code - other.code
  54.       end;
  55.    
  56.    value: INTEGER is
  57.      -- Gives the value of a digit.
  58.       require
  59.      is_digit
  60.       do
  61.      Result := code - 48;
  62.       ensure
  63.      0 <= Result and Result <= 9;
  64.      definition: Result = code - 48;
  65.       end; -- value
  66.  
  67.    same_as(other: CHARACTER): BOOLEAN is
  68.      -- No difference upper/lower.
  69.       do
  70.      if Current = other then
  71.         Result := true;
  72.      else
  73.         inspect
  74.            code
  75.         when 65 .. 90 then
  76.            Result := Current = other.to_upper;
  77.         when 97 .. 122 then
  78.            Result := Current = other.to_lower;
  79.         else
  80.         end;
  81.      end;
  82.       ensure
  83.      Result implies to_lower = other or to_upper = other;
  84.       end ;
  85.    
  86.    to_upper: CHARACTER is
  87.      -- Conversion to the corresponding upper case.
  88.       do
  89.      if code < 97 then
  90.         Result := Current;
  91.      elseif code > 122 then
  92.         Result := Current;
  93.      else
  94.         Result := (code - 32).to_character;
  95.      end;
  96.       end;
  97.    
  98.    to_lower: CHARACTER is
  99.      -- Conversion to the corresponding lower case.
  100.       do
  101.      if code < 65 then
  102.         Result := Current;
  103.      elseif code > 90 then
  104.         Result := Current;
  105.      else
  106.         Result := (code + 32).to_character;
  107.      end;
  108.       end;
  109.    
  110.    is_letter: BOOLEAN is
  111.       do
  112.      inspect 
  113.         Current
  114.      when 'A'..'Z','a'..'z' then
  115.         Result := true;
  116.      else
  117.      end;
  118.       ensure
  119.      Result = ('A' <= Current and Current <= 'Z') or 
  120.         ('a' <= Current and Current <= 'z');
  121.       end;
  122.    
  123.    is_digit: BOOLEAN is
  124.      -- Belongs to '0'..'9'.
  125.       do
  126.      inspect 
  127.         Current
  128.      when '0'..'9' then
  129.         Result := true;
  130.      else
  131.      end;
  132.       ensure
  133.     Result implies ('0' <= Current and Current <= '9');
  134.       end;
  135.    
  136.    is_lower: BOOLEAN is 
  137.       -- Is `item' lowercase?
  138.       do  
  139.          inspect 
  140.             Current
  141.          when 'a'..'z' then 
  142.                Result := true;
  143.          else 
  144.          end; 
  145.       end; 
  146.    
  147.    is_upper: BOOLEAN is 
  148.       -- Is `item' uppercase?
  149.       do  
  150.          inspect 
  151.             Current
  152.          when 'A'..'Z' then 
  153.                Result := true;
  154.          else 
  155.          end; 
  156.       end; 
  157.    
  158.    is_separator: BOOLEAN is
  159.      -- True when character is a separator.
  160.       do
  161.      inspect
  162.         Current
  163.      when ' ','%T','%N','%R','%U' then
  164.         Result := true;
  165.      else
  166.      end;
  167.       end;
  168.    
  169.    is_letter_or_digit: BOOLEAN is 
  170.       -- Is character a letter or digit?
  171.       do  
  172.          Result := is_letter or is_digit;
  173.       ensure 
  174.          valid: Result = (is_letter or is_digit); 
  175.       end; 
  176.  
  177.    is_hex_digit: BOOLEAN is 
  178.      -- Is character one of 0123456789abcdefABCDEF ?
  179.       do  
  180.          if is_digit then
  181.         Result := true;
  182.      elseif Current >= 'a' then
  183.         Result := Current <= 'f';
  184.      elseif Current >= 'A' then
  185.         Result := Current <= 'F';
  186.      end;
  187.       end; 
  188.  
  189.    is_ascii: BOOLEAN is 
  190.      -- Is character a 8-bit ASCII character?
  191.       do  
  192.          inspect 
  193.             Current
  194.          when '%U'..'%/255/' then 
  195.         Result := true;
  196.          else 
  197.          end; 
  198.       end; 
  199.  
  200.    next: CHARACTER is
  201.       -- Give the next character (the following `code');
  202.       do
  203.      Result := (code + 1).to_character;
  204.       end;
  205.  
  206.    previous: CHARACTER is
  207.       -- Give the next character (the following `code');
  208.       require
  209.      code > 0
  210.       do
  211.      Result := (code - 1).to_character;
  212.       end;
  213.  
  214. feature -- Conversions :
  215.  
  216.    to_bit: BIT Character_bits is
  217.       do
  218.          c_inline_c("R=C;");
  219.       end;
  220.  
  221. feature -- Object Printing :
  222.  
  223.    out_in_tagged_out_memory, fill_tagged_out_memory is
  224.       do
  225.      tagged_out_memory.extend(Current);
  226.       end;
  227.    
  228. end -- CHARACTER
  229.