home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Environments / SmallEiffel 0.3.3 / SmallEiffel 68k / lib_std / character.e < prev    next >
Encoding:
Text File  |  1996-06-13  |  3.1 KB  |  156 lines  |  [TEXT/EDIT]

  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, fill_tagged_out_memory
  13.       end;
  14.  
  15. feature {ANY}
  16.  
  17.    code: INTEGER is
  18.      -- ASCII code of Current.
  19.       external "CSE" 
  20.       end;
  21.    
  22.    infix "<" (other: CHARACTER): BOOLEAN is
  23.      -- Comparison using `code'. 
  24.       do
  25.      Result := code < other.code;
  26.       end;
  27.    
  28.    infix "<="(other: CHARACTER): BOOLEAN is
  29.      -- Comparison using `code'. 
  30.       do
  31.      Result := code <= other.code;
  32.       end;
  33.    
  34.    infix ">"(other: CHARACTER): BOOLEAN is
  35.      -- Comparison using `code'. 
  36.       do
  37.      Result := code > other.code;
  38.       end;
  39.    
  40.    infix ">="(other: CHARACTER): BOOLEAN is
  41.      -- Comparison using `code'. 
  42.       do
  43.      Result := code >= other.code;
  44.       end;
  45.    
  46.    compare (other: CHARACTER): INTEGER is
  47.      -- Compare Current with `other'.
  48.      -- '<' <==> Result < 0
  49.      -- '>' <==> Result > 0
  50.      -- Otherwise Result = 0
  51.       do
  52.      Result := code - other.code
  53.       end;
  54.    
  55.    value: INTEGER is
  56.      -- Gives the value of a digit.
  57.       require
  58.      is_digit
  59.       do
  60.      Result := code - 48;
  61.       ensure
  62.      0 <= Result and Result <= 9;
  63.      definition: Result = code - 48;
  64.       end; -- value
  65.  
  66.    same_as(other: CHARACTER): BOOLEAN is
  67.      -- No difference upper/lower.
  68.       do
  69.      if Current = other then
  70.         Result := true;
  71.      else
  72.         inspect
  73.            code
  74.         when 65 .. 90 then
  75.            Result := Current = other.to_upper;
  76.         when 97 .. 122 then
  77.            Result := Current = other.to_lower;
  78.         else
  79.         end;
  80.      end;
  81.       ensure
  82.      Result implies to_lower = other or to_upper = other;
  83.       end ;
  84.    
  85.    to_upper: CHARACTER is
  86.      -- Conversion to the corresponding upper case.
  87.       do
  88.      if code < 97 then
  89.         Result := Current;
  90.      elseif code > 122 then
  91.         Result := Current;
  92.      else
  93.         Result := (code - 32).to_character;
  94.      end;
  95.       end;
  96.    
  97.    to_lower: CHARACTER is
  98.      -- Conversion to the corresponding lower case.
  99.       do
  100.      if code < 65 then
  101.         Result := Current;
  102.      elseif code > 90 then
  103.         Result := Current;
  104.      else
  105.         Result := (code + 32).to_character;
  106.      end;
  107.       end;
  108.    
  109.    is_letter: BOOLEAN is
  110.       do
  111.      inspect 
  112.         Current
  113.      when 'A'..'Z','a'..'z' then
  114.         Result := true;
  115.      else
  116.      end;
  117.       ensure
  118.      Result = ('A' <= Current and Current <= 'Z') or 
  119.         ('a' <= Current and Current <= 'z');
  120.       end;
  121.    
  122.    is_digit: BOOLEAN is
  123.      -- Belongs to '0'..'9'.
  124.       do
  125.      inspect 
  126.         Current
  127.      when '0'..'9' then
  128.         Result := true;
  129.      else
  130.      end;
  131.       ensure
  132.     Result implies ('0' <= Current and Current <= '9');
  133.       end;
  134.    
  135.    is_separator: BOOLEAN is
  136.      -- True when character is a separator.
  137.       do
  138.      inspect
  139.         Current
  140.      when ' ','%T','%N','%R','%U' then
  141.         Result := true;
  142.      else
  143.      end;
  144.       end;
  145.    
  146. feature -- Object Printing :
  147.  
  148.    fill_tagged_out_memory is
  149.       do
  150.      tagged_out_memory.extend('%'');
  151.      tagged_out_memory.extend(Current);
  152.      tagged_out_memory.extend('%'');
  153.       end;
  154.    
  155. end -- CHARACTER
  156.