note description: "[ Helper functions to provide property of a Unicode character of type CHARACTER_32. This includes finding out if a character is lower, upper, title case, or if it is a digit, a punctuation, a control or a space character. ]" generator: "$GENERATOR" unicode_version: "$UNICODE_VERSION" date: "$Date: $" revision: "$Revision: $" class CHARACTER_PROPERTY create make feature {NONE} -- Initialization make do end feature -- Status report is_alpha (a_char: CHARACTER_32): BOOLEAN -- Is `a_char' an alphabetic character? do if a_char.natural_32_code <= max_ascii_value then -- Fast implementation for ASCII character. if ('a' <= a_char and a_char <= 'z') or ('A' <= a_char and a_char <= 'Z') then Result := True end else Result := (property (a_char) & (is_lower_flag | is_upper_flag | is_title_flag)) /= 0 end ensure definition: Result = (is_lower (a_char) or is_upper (a_char)) end is_lower (a_char: CHARACTER_32): BOOLEAN -- Is `a_char' a lower character? do if a_char.natural_32_code <= max_ascii_value then -- Fast implementation for ASCII character. if 'a' <= a_char and a_char <= 'z' then Result := True end else Result := (property (a_char) & is_lower_flag) /= 0 end end is_upper (a_char: CHARACTER_32): BOOLEAN -- Is `a_char' an upper character? do if a_char.natural_32_code <= max_ascii_value then -- Fast implementation for ASCII character. if 'A' <= a_char and a_char <= 'Z' then Result := True end else Result := (property (a_char) & is_upper_flag) /= 0 end end is_title (a_char: CHARACTER_32): BOOLEAN -- Is `a_char' a title character? do if a_char.natural_32_code <= max_ascii_value then -- Fast implementation for ASCII character. if 'A' <= a_char and a_char <= 'Z' then Result := True end else Result := (property (a_char) & is_title_flag) /= 0 end end is_digit (a_char: CHARACTER_32): BOOLEAN -- Is `a_char' a digit character? do if a_char.natural_32_code <= max_ascii_value then -- Fast implementation for ASCII character. if '0' <= a_char and a_char <= '9' then Result := True end else Result := (property (a_char) & is_digit_flag) /= 0 end end is_punctuation (a_char: CHARACTER_32): BOOLEAN -- Is `a_char' a punctuation character? do if a_char.natural_32_code <= max_ascii_value then -- Fast implementation for ASCII character. Result := a_char.to_character_8.is_punctuation else Result := (property (a_char) & is_punctuation_flag) /= 0 end end is_control (a_char: CHARACTER_32): BOOLEAN -- Is `a_char' a control character? do if a_char.natural_32_code <= max_ascii_value then -- Fast implementation for ASCII character. Result := a_char.to_character_8.is_control else Result := (property (a_char) & is_control_flag) /= 0 end end is_hexa_digit (a_char: CHARACTER_32): BOOLEAN -- Is `a_char' an hexadecimal character? do if a_char.natural_32_code <= max_ascii_value then -- Fast implementation for ASCII character. Result := a_char.to_character_8.is_hexa_digit else Result := (property (a_char) & is_hexa_digit_flag) /= 0 end end is_space (a_char: CHARACTER_32): BOOLEAN -- Is `a_char' a space character? do if a_char.natural_32_code <= max_ascii_value then -- Fast implementation for ASCII character. Result := a_char.to_character_8.is_space else Result := (property (a_char) & is_space_flag) /= 0 end end feature -- Constants max_ascii_value: NATURAL_32 = 0x7F max_latin1_value: NATURAL_32 = 0xFF max_unicode_value: NATURAL_32 = 0x10FFFF -- Various maximal value for character range. feature -- Conversion to_lower (a_char: CHARACTER_32): CHARACTER_32 -- Convert `a_char' to lower case. local l_code: NATURAL_32 do -- Most of the time the character remains unchanged. Result := a_char -- Fast implementation for ASCII character. l_code := a_char.natural_32_code if l_code <= max_ascii_value then if 'A' <= a_char and a_char <= 'Z' then check valid_assumption: ('a').natural_32_code = ('A').natural_32_code + 32 end Result := a_char + 32 end else --$TO_LOWER_HELPER end end to_upper (a_char: CHARACTER_32): CHARACTER_32 -- Convert `a_char' to upper case. local l_code: NATURAL_32 do -- Most of the time the character remains unchanged. Result := a_char -- Fast implementation for ASCII character. l_code := a_char.natural_32_code if l_code <= max_ascii_value then if 'a' <= a_char and a_char <= 'z' then check valid_assumption: ('a').natural_32_code = ('A').natural_32_code + 32 end Result := a_char - 32 end else --$TO_UPPER_HELPER end end to_title (a_char: CHARACTER_32): CHARACTER_32 -- Convert `a_char' to title case. local l_code: NATURAL_32 do -- Most of the time the character remains unchanged. Result := a_char -- Fast implementation for ASCII character. l_code := a_char.natural_32_code if l_code <= max_ascii_value then if 'a' <= a_char and a_char <= 'z' then check valid_assumption: ('a').natural_32_code = ('A').natural_32_code + 32 end Result := a_char - 32 end else --$TO_TITLE_HELPER end end feature {NONE} -- Helper property (a_char: CHARACTER_32): NATURAL_8 -- Provides the various flags describing `a_char'. local l_code: NATURAL_32 do l_code := a_char.natural_32_code --$PROPERTY_HELPER end feature {NONE} -- Flags is_upper_flag: NATURAL_8 = 0x01 is_lower_flag: NATURAL_8 = 0x02 is_title_flag: NATURAL_8 = 0x4 is_digit_flag: NATURAL_8 = 0x08 is_punctuation_flag: NATURAL_8 = 0x10 is_control_flag: NATURAL_8 = 0x20 is_hexa_digit_flag: NATURAL_8 = 0x40 is_space_flag: NATURAL_8 = 0x80 -- Flags for character properties used in `property_table'. feature {NONE} -- Property tables --$TABLES note copyright: "Copyright (c) 1984-2018, Eiffel Software and others" license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)" source: "[ Eiffel Software 5949 Hollister Ave., Goleta, CA 93117 USA Telephone 805-685-1006, Fax 805-685-6869 Website http://www.eiffel.com Customer support http://support.eiffel.com ]" end