note description: "Convertor to do string to integer/natural conversion" library: "Free implementation of ELKS library" status: "See notice at end of class." legal: "See notice at end of class." date: "$Date: 2017-03-28 12:36:24 +0000 (Tue, 28 Mar 2017) $" revision: "$Revision: 100064 $" frozen class STRING_TO_INTEGER_CONVERTOR inherit STRING_TO_NUMERIC_CONVERTOR create make feature {NONE} -- Initialization make -- Initialize. do reset (Type_no_limitation) create leading_separators.make_from_string (" ") create trailing_separators.make_from_string (" ") ensure leading_separators_set: leading_separators ~ " " trailing_separators_set: trailing_separators ~ " " leading_separators_not_acceptable: not leading_separators_acceptable trailing_separatorsnot_acceptable: not trailing_separators_acceptable end feature -- State machine setting reset (type: INTEGER_32) -- Reset current convertor to parse integer of type type. do last_state := 0 part1 := 0 part2 := 0 sign := 0 conversion_type := type internal_overflowed := False ensure then internal_overflowed_set: not internal_overflowed part1_set: part1 = 0 part2_set: part2 = 0 end feature -- Status report separators_valid (separators: STRING_8): BOOLEAN -- Are separators contained in separators valid? local i, nb: INTEGER_32 c: CHARACTER_8 done: BOOLEAN do from i := 1 nb := separators.count done := False Result := True until i > nb or done loop c := separators.item (i) if (c >= '0' and c <= '9') or c = '+' or c = '-' then done := True Result := False end i := i + 1 end end overflowed: BOOLEAN -- Is integer parsed so far overflowed? do Result := internal_overflowed and then sign = 0 end underflowed: BOOLEAN -- Is integer parsed so fa underflowed? do Result := internal_overflowed and then sign = 1 end parse_successful: BOOLEAN -- This only means we didn't enter an invalid state when parsing, -- it doesn't mean that we have got an valid integer. -- You need to check is_integral_integer or is_part_of_integer. do Result := last_state /= 4 and last_state /= 5 end conversion_type_valid (type: INTEGER_32): BOOLEAN -- If conversion type valid? do Result := integer_natural_type_valid (type) end is_part_of_integer: BOOLEAN -- Is character sequence that has been parsed so far a valid start part of an integer? do Result := ((last_state = 0) or (last_state = 1) or (last_state = 2) or (last_state = 3)) and (not internal_overflowed) end is_integral_integer: BOOLEAN -- Is character sequence that has been parsed so far a valid integral integer? do Result := ((last_state = 2) or (last_state = 3)) and (not internal_overflowed) end feature -- String parsing parse_string_with_type (s: READABLE_STRING_GENERAL; type: INTEGER_32) -- Parse string s as integer of type type. local i, nb: INTEGER_32 l_area8: SPECIAL [CHARACTER_8] l_area32: SPECIAL [CHARACTER_32] l_c: CHARACTER_32 l_code: NATURAL_32 do reset (type) i := 0 nb := s.count if attached {READABLE_STRING_8} s as l_str8 then from l_area8 := l_str8.area until i = nb or last_state >= 4 loop parse_character (l_area8.item (i)) i := i + 1 end elseif attached {READABLE_STRING_32} s as l_str32 then from l_area32 := l_str32.area until i = nb or last_state >= 4 loop l_c := l_area32.item (i) if l_c.is_character_8 then parse_character (l_c.to_character_8) else last_state := 4 end i := i + 1 end else from i := 1 nb := s.count until i > nb or last_state >= 4 loop l_code := s.code (i) if l_code.is_valid_character_8_code then parse_character (l_code.to_character_8) else last_state := 4 end i := i + 1 end end end parse_character (c: CHARACTER_8) -- Parse next character c. local temp_p1: like max_natural_type temp_p2: like max_natural_type l_state: like last_state do l_state := last_state if l_state <= 4 then inspect l_state when 0 then if c.is_digit then l_state := 2 part1 := 0 part2 := (c.code - 48).to_natural_64 elseif c = '-' or c = '+' then l_state := 1 if c = '-' then sign := 1 else sign := 0 end elseif leading_separators_acceptable and then leading_separators.has (c) then else l_state := 4 end when 1 then if c.is_digit then part1 := 0 part2 := (c.code - 48).to_natural_64 l_state := 2 if conversion_type /= Type_no_limitation and then Overflow_checker.will_overflow (part1, part2, conversion_type, sign) then internal_overflowed := True part1 := temp_p1 part2 := temp_p2 l_state := 5 end else l_state := 4 end when 2 then if c.is_digit then temp_p1 := part1 temp_p2 := part2 part1 := part1 * 10 + part2 part2 := (c.code - 48).to_natural_64 if conversion_type /= Type_no_limitation then internal_overflowed := Overflow_checker.will_overflow (part1, part2, conversion_type, sign) if overflowed then part1 := temp_p1 part2 := temp_p2 l_state := 5 end end elseif trailing_separators_acceptable and then trailing_separators.has (c) then l_state := 3 else l_state := 4 end when 3 then if trailing_separators_acceptable and then trailing_separators.has (c) then else l_state := 4 end end end last_state := l_state end feature -- Access parsed_integer_8: INTEGER_8 -- INTEGER_8 representation of parsed string do if sign = 1 then Result := - (part1 * 10 + part2).as_integer_8 else Result := (part1 * 10 + part2).as_integer_8 end end parsed_integer_16: INTEGER_16 -- INTEGER_16 representation of parsed string do if sign = 1 then Result := - (part1 * 10 + part2).as_integer_16 else Result := (part1 * 10 + part2).as_integer_16 end end parsed_integer_32: INTEGER_32 -- INTEGER representation of parsed string -- Was declared in STRING_TO_INTEGER_CONVERTOR as synonym of parsed_integer. do if sign = 1 then Result := - (part1 * 10 + part2).as_integer_32 else Result := (part1 * 10 + part2).as_integer_32 end end parsed_integer: INTEGER_32 -- INTEGER representation of parsed string -- Was declared in STRING_TO_INTEGER_CONVERTOR as synonym of parsed_integer_32. do if sign = 1 then Result := - (part1 * 10 + part2).as_integer_32 else Result := (part1 * 10 + part2).as_integer_32 end end parsed_integer_64: INTEGER_64 -- INTEGER_64 representation of parsed string do if sign = 1 then Result := - (part1 * 10 + part2).as_integer_64 else Result := (part1 * 10 + part2).as_integer_64 end end parsed_natural_8: NATURAL_8 -- NATURAL_8 representation of parsed string do Result := (part1 * 10 + part2).as_natural_8 end parsed_natural_16: NATURAL_16 -- NATURAL_16 representation of parsed string do Result := (part1 * 10 + part2).as_natural_16 end parsed_natural_32: NATURAL_32 -- NATURAL_32 representation of parsed string -- Was declared in STRING_TO_INTEGER_CONVERTOR as synonym of parsed_natural. do Result := (part1 * 10 + part2).as_natural_32 end parsed_natural: NATURAL_32 -- NATURAL_32 representation of parsed string -- Was declared in STRING_TO_INTEGER_CONVERTOR as synonym of parsed_natural_32. do Result := (part1 * 10 + part2).as_natural_32 end parsed_natural_64: NATURAL_64 -- NATURAL_64 representation of parsed string do Result := part1 * 10 + part2 end feature {NONE} -- Implementation Overflow_checker: INTEGER_OVERFLOW_CHECKER -- Overflow checker once create Result.make ensure overflow_checker_not_void: Result /= Void end part1: like max_natural_type -- Naturals used for conversion part2: like max_natural_type -- Naturals used for conversion internal_overflowed: BOOLEAN -- Internal overflow flag note copyright: "Copyright (c) 1984-2017, 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 -- class STRING_TO_INTEGER_CONVERTOR
Generated by ISE EiffelStudio