home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / lang / eiffel / smalleif.97 / se.t / SmallEiffel / lib_std / character.e < prev    next >
Encoding:
Text File  |  1996-05-02  |  2.8 KB  |  139 lines

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