note
	description: "String - Integer/Natural conversion overflow checker"
	library: "Free implementation of ELKS library"
	status: "See notice at end of class."
	legal: "See notice at end of class."
	date: "$Date: 2017-03-23 20:42:57 +0000 (Thu, 23 Mar 2017) $"
	revision: "$Revision: 100036 $"

class 
	INTEGER_OVERFLOW_CHECKER

inherit
	NUMERIC_INFORMATION

create 
	make

feature {NONE} -- Initialization

	make
			-- Initialize.
		do
			create integer_overflow_state1.make_empty (Type_count * 2 + 1)
			integer_overflow_state1.extend (0)
			create integer_overflow_state2.make_empty (Type_count * 2 + 1)
			integer_overflow_state2.extend (0)
			integer_overflow_state1.extend (({INTEGER_8}.max_value // 10).to_natural_64)
			integer_overflow_state2.extend (({INTEGER_8}.max_value \\ 10).to_natural_64)
			integer_overflow_state1.extend (({INTEGER_16}.max_value // 10).to_natural_64)
			integer_overflow_state2.extend (({INTEGER_16}.max_value \\ 10).to_natural_64)
			integer_overflow_state1.extend (({INTEGER_32}.max_value // 10).to_natural_64)
			integer_overflow_state2.extend (({INTEGER_32}.max_value \\ 10).to_natural_64)
			integer_overflow_state1.extend (({INTEGER_64}.max_value // 10).to_natural_64)
			integer_overflow_state2.extend (({INTEGER_64}.max_value \\ 10).to_natural_64)
			integer_overflow_state1.extend ((- ({INTEGER_8}.min_value // 10)).to_natural_64)
			integer_overflow_state2.extend ((- ({INTEGER_8}.min_value \\ 10)).to_natural_64)
			integer_overflow_state1.extend ((- ({INTEGER_16}.min_value // 10)).to_natural_64)
			integer_overflow_state2.extend ((- ({INTEGER_16}.min_value \\ 10)).to_natural_64)
			integer_overflow_state1.extend ((- ({INTEGER_32}.min_value // 10)).to_natural_64)
			integer_overflow_state2.extend ((- ({INTEGER_32}.min_value \\ 10)).to_natural_64)
			integer_overflow_state1.extend ((- ({INTEGER_64}.min_value // 10)).to_natural_64)
			integer_overflow_state2.extend ((- ({INTEGER_64}.min_value \\ 10)).to_natural_64)
			create natural_overflow_state1.make_empty (Type_count + 1)
			natural_overflow_state1.extend (0)
			create natural_overflow_state2.make_empty (Type_count + 1)
			natural_overflow_state2.extend (0)
			natural_overflow_state1.extend (({NATURAL_8}.max_value // 10).to_natural_64)
			natural_overflow_state2.extend (({NATURAL_8}.max_value \\ 10).to_natural_64)
			natural_overflow_state1.extend (({NATURAL_16}.max_value // 10).to_natural_64)
			natural_overflow_state2.extend (({NATURAL_16}.max_value \\ 10).to_natural_64)
			natural_overflow_state1.extend (({NATURAL_32}.max_value // 10).to_natural_64)
			natural_overflow_state2.extend (({NATURAL_32}.max_value \\ 10).to_natural_64)
			natural_overflow_state1.extend (({NATURAL_64}.max_value // 10).to_natural_64)
			natural_overflow_state2.extend (({NATURAL_64}.max_value \\ 10).to_natural_64)
		end
	
feature -- Overflow checking

	will_overflow (part1: like max_natural_type; part2: like max_natural_type; type: INTEGER_32; sign: INTEGER_32): BOOLEAN
			-- Will part1 * 10 + part2 with sign overflow
			-- if we convert it to an number of type?			
		require
			type_valid: integer_natural_type_valid (type)
		local
			l_index: INTEGER_32
		do
			if type /= Type_no_limitation then
				if type = Type_integer_8 or type = Type_integer_16 or type = Type_integer_32 or type = Type_integer_64 then
					l_index := sign * 4 + type
					Result := part1 > integer_overflow_state1.item (l_index) or (part1 = integer_overflow_state1.item (l_index) and part2 > integer_overflow_state2.item (l_index))
				elseif sign = 1 then
					Result := part1 > 0 or part2 > 0
				else
					l_index := type - Type_integer_natural_separator
					Result := part1 > natural_overflow_state1.item (l_index) or (part1 = natural_overflow_state1.item (l_index) and part2 > natural_overflow_state2.item (l_index))
				end
			end
		end
	
feature {NONE} -- Implementation

	integer_overflow_state1: SPECIAL [like max_natural_type]

	integer_overflow_state2: SPECIAL [like max_natural_type]

	natural_overflow_state1: SPECIAL [like max_natural_type]

	natural_overflow_state2: SPECIAL [like max_natural_type]
			-- Arrays to check conversion overflow.
	
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 INTEGER_OVERFLOW_CHECKER

Generated by ISE EiffelStudio