home *** CD-ROM | disk | FTP | other *** search
Text File | 1997-04-13 | 4.6 KB | 229 lines | [TEXT/ttxt] |
- -- Part of SmallEiffel -- Read DISCLAIMER file -- Copyright (C)
- -- Dominique COLNET and Suzanne COLLIN -- colnet@loria.fr
- --
- expanded class CHARACTER
- --
- -- Note : corresponding C type is "char"
- --
- inherit
- CHARACTER_REF
- redefine
- infix "<", infix "<=", infix ">", infix ">=", compare,
- code, to_lower, to_upper, out_in_tagged_out_memory,
- fill_tagged_out_memory
- end;
-
- feature
-
- code: INTEGER is
- -- ASCII code of Current.
- external "CSE"
- end;
-
- infix "<" (other: CHARACTER): BOOLEAN is
- -- Comparison using `code'.
- do
- Result := code < other.code;
- end;
-
- infix "<="(other: CHARACTER): BOOLEAN is
- -- Comparison using `code'.
- do
- Result := code <= other.code;
- end;
-
- infix ">"(other: CHARACTER): BOOLEAN is
- -- Comparison using `code'.
- do
- Result := code > other.code;
- end;
-
- infix ">="(other: CHARACTER): BOOLEAN is
- -- Comparison using `code'.
- do
- Result := code >= other.code;
- end;
-
- compare (other: CHARACTER): INTEGER is
- -- Compare Current with `other'.
- -- '<' <==> Result < 0
- -- '>' <==> Result > 0
- -- Otherwise Result = 0
- do
- Result := code - other.code
- end;
-
- value: INTEGER is
- -- Gives the value of a digit.
- require
- is_digit
- do
- Result := code - 48;
- ensure
- 0 <= Result and Result <= 9;
- definition: Result = code - 48;
- end; -- value
-
- same_as(other: CHARACTER): BOOLEAN is
- -- No difference upper/lower.
- do
- if Current = other then
- Result := true;
- else
- inspect
- code
- when 65 .. 90 then
- Result := Current = other.to_upper;
- when 97 .. 122 then
- Result := Current = other.to_lower;
- else
- end;
- end;
- ensure
- Result implies to_lower = other or to_upper = other;
- end ;
-
- to_upper: CHARACTER is
- -- Conversion to the corresponding upper case.
- do
- if code < 97 then
- Result := Current;
- elseif code > 122 then
- Result := Current;
- else
- Result := (code - 32).to_character;
- end;
- end;
-
- to_lower: CHARACTER is
- -- Conversion to the corresponding lower case.
- do
- if code < 65 then
- Result := Current;
- elseif code > 90 then
- Result := Current;
- else
- Result := (code + 32).to_character;
- end;
- end;
-
- is_letter: BOOLEAN is
- do
- inspect
- Current
- when 'A'..'Z','a'..'z' then
- Result := true;
- else
- end;
- ensure
- Result = ('A' <= Current and Current <= 'Z') or
- ('a' <= Current and Current <= 'z');
- end;
-
- is_digit: BOOLEAN is
- -- Belongs to '0'..'9'.
- do
- inspect
- Current
- when '0'..'9' then
- Result := true;
- else
- end;
- ensure
- Result implies ('0' <= Current and Current <= '9');
- end;
-
- is_lower: BOOLEAN is
- -- Is `item' lowercase?
- do
- inspect
- Current
- when 'a'..'z' then
- Result := true;
- else
- end;
- end;
-
- is_upper: BOOLEAN is
- -- Is `item' uppercase?
- do
- inspect
- Current
- when 'A'..'Z' then
- Result := true;
- else
- end;
- end;
-
- is_separator: BOOLEAN is
- -- True when character is a separator.
- do
- inspect
- Current
- when ' ','%T','%N','%R','%U' then
- Result := true;
- else
- end;
- end;
-
- is_letter_or_digit: BOOLEAN is
- -- Is character a letter or digit?
- do
- Result := is_letter or is_digit;
- ensure
- valid: Result = (is_letter or is_digit);
- end;
-
- is_hex_digit: BOOLEAN is
- -- Is character one of 0123456789abcdefABCDEF ?
- do
- if is_digit then
- Result := true;
- elseif Current >= 'a' then
- Result := Current <= 'f';
- elseif Current >= 'A' then
- Result := Current <= 'F';
- end;
- end;
-
- is_ascii: BOOLEAN is
- -- Is character a 8-bit ASCII character?
- do
- inspect
- Current
- when '%U'..'%/255/' then
- Result := true;
- else
- end;
- end;
-
- next: CHARACTER is
- -- Give the next character (the following `code');
- do
- Result := (code + 1).to_character;
- end;
-
- previous: CHARACTER is
- -- Give the next character (the following `code');
- require
- code > 0
- do
- Result := (code - 1).to_character;
- end;
-
- feature -- Conversions :
-
- to_bit: BIT Character_bits is
- do
- c_inline_c("R=C;");
- end;
-
- feature -- Object Printing :
-
- out_in_tagged_out_memory, fill_tagged_out_memory is
- do
- tagged_out_memory.extend(Current);
- end;
-
- end -- CHARACTER
-