note description: "Formatter for integral numbers" library: "Free implementation of ELKS library" status: "See notice at end of class." legal: "See notice at end of class." names: format_integer date: "$Date: 2013-03-20 23:43:02 +0000 (Wed, 20 Mar 2013) $" revision: "$Revision: 92323 $" class FORMAT_INTEGER create make feature -- Initialization make (w: INTEGER_32) require reasonable_field: w >= 1 do blank_fill width := w sign_normal sign_negative_only right_justify ensure blank_fill: fill_character = ' ' show_sign_negative: show_sign_negative no_separator: no_separator width_set: width = w right_justified: right_justified leading_sign: leading_sign end feature {NONE} -- Initialization default_create -- Process instances of classes with no creation clause. -- (Default: do nothing.) -- (from ANY) do end feature -- Access bracketted_negative: BOOLEAN -- Enclose negative numbers in brackets? fill_character: CHARACTER_8 -- Padding character. generating_type: TYPE [detachable FORMAT_INTEGER] -- Type of current object -- (type of which it is a direct instance) -- (from ANY) external "built_in" ensure -- from ANY generating_type_not_void: Result /= Void end generator: STRING_8 -- Name of current object's generating class -- (base class of the type of which it is a direct instance) -- (from ANY) external "built_in" ensure -- from ANY generator_not_void: Result /= Void generator_not_empty: not Result.is_empty end justification: INTEGER_32 -- Where in the field the format goes. separator: CHARACTER_8 -- Separator between 1000's of numbers. sign_format: INTEGER_32 -- How the sign is formatted. sign_string: STRING_8 -- Formatting details for the sign. trailing_sign: BOOLEAN -- Is the sign at the end? width: INTEGER_32 -- Width of the field. feature -- Comparison frozen deep_equal (a: detachable ANY; b: like arg #1): BOOLEAN -- Are a and b either both void -- or attached to isomorphic object structures? -- (from ANY) do if a = Void then Result := b = Void else Result := b /= Void and then a.is_deep_equal (b) end ensure -- from ANY instance_free: class shallow_implies_deep: standard_equal (a, b) implies Result both_or_none_void: (a = Void) implies (Result = (b = Void)) same_type: (Result and (a /= Void)) implies (b /= Void and then a.same_type (b)) symmetric: Result implies deep_equal (b, a) end frozen equal (a: detachable ANY; b: like arg #1): BOOLEAN -- Are a and b either both void or attached -- to objects considered equal? -- (from ANY) do if a = Void then Result := b = Void else Result := b /= Void and then a.is_equal (b) end ensure -- from ANY instance_free: class definition: Result = (a = Void and b = Void) or else ((a /= Void and b /= Void) and then a.is_equal (b)) end frozen is_deep_equal alias "≡≡≡" (other: FORMAT_INTEGER): BOOLEAN -- Are Current and other attached to isomorphic object structures? -- (from ANY) require -- from ANY other_not_void: other /= Void external "built_in" ensure -- from ANY shallow_implies_deep: standard_is_equal (other) implies Result same_type: Result implies same_type (other) symmetric: Result implies other.is_deep_equal (Current) end is_equal (other: FORMAT_INTEGER): BOOLEAN -- Is other attached to an object considered -- equal to current object? -- (from ANY) require -- from ANY other_not_void: other /= Void external "built_in" ensure -- from ANY symmetric: Result implies other ~ Current consistent: standard_is_equal (other) implies Result end frozen standard_equal (a: detachable ANY; b: like arg #1): BOOLEAN -- Are a and b either both void or attached to -- field-by-field identical objects of the same type? -- Always uses default object comparison criterion. -- (from ANY) do if a = Void then Result := b = Void else Result := b /= Void and then a.standard_is_equal (b) end ensure -- from ANY instance_free: class definition: Result = (a = Void and b = Void) or else ((a /= Void and b /= Void) and then a.standard_is_equal (b)) end frozen standard_is_equal alias "≜" (other: FORMAT_INTEGER): BOOLEAN -- Is other attached to an object of the same type -- as current object, and field-by-field identical to it? -- (from ANY) require -- from ANY other_not_void: other /= Void external "built_in" ensure -- from ANY same_type: Result implies same_type (other) symmetric: Result implies other.standard_is_equal (Current) end feature -- Status report centered: BOOLEAN -- Are numbers to be formatted centered? do Result := justification = Center_justification ensure Result = (justification = Center_justification) end conforms_to (other: ANY): BOOLEAN -- Does type of current object conform to type -- of other (as per Eiffel: The Language, chapter 13)? -- (from ANY) require -- from ANY other_not_void: other /= Void external "built_in" end ignore_sign: BOOLEAN -- Ignore the sign of a number? do Result := sign_format = Ignore_sign_value ensure Result = (sign_format = Ignore_sign_value) end leading_sign: BOOLEAN -- Is the sign leading? do Result := not trailing_sign ensure Result = not trailing_sign end left_justified: BOOLEAN -- Are numbers to be formatted with spaces on the right? do Result := justification = Left_justification ensure Result = (justification = Left_justification) end no_separator: BOOLEAN -- Is there a separator? do Result := separator = '%U' ensure Result = (separator = '%U') end not_justified: BOOLEAN -- Are numbers to be formatted in smallest string possible do Result := justification = No_justification ensure Result = (justification = No_justification) end right_justified: BOOLEAN -- Are numbers to be formatted with spaces on the left? do Result := justification = Right_justification ensure Result = (justification = Right_justification) end same_type (other: ANY): BOOLEAN -- Is type of current object identical to type of other? -- (from ANY) require -- from ANY other_not_void: other /= Void external "built_in" ensure -- from ANY definition: Result = (conforms_to (other) and other.conforms_to (Current)) end show_sign: BOOLEAN -- Are numbers to show sign whether positive or negative? do Result := sign_format = Show_sign_value ensure Result = (sign_format = Show_sign_value) end show_sign_negative: BOOLEAN -- Are numbers to show sign only when negative? do Result := sign_format = Sign_negative_value ensure Result = (sign_format = Sign_negative_value) end show_sign_positive: BOOLEAN -- Are numbers to show sign only when positive? do Result := sign_format = Sign_positive_value ensure Result = (sign_format = Sign_positive_value) end feature -- Status setting asterisk_fill -- Fill numbers with asterisks. do fill_character := '*' ensure fill_character = '*' end blank_fill -- Fill numbers with blanks. do fill_character := ' ' ensure fill_character = ' ' end bracket_negative -- Bracket negative numbers. do bracketted_negative := True ensure bracketted_negative end center_justify -- Put padding on right and left do justification := Center_justification ensure centered end comma_separate -- Set separator to comma. do separator := ',' ensure separator = ',' end dollar_fill -- Fill numbers with dollars. do fill_character := '$' ensure fill_character = '$' end dot_separate -- Set separator to period. do separator := '.' ensure separator = '.' end left_justify --Put padding on right do justification := Left_justification ensure left_justified end no_justify -- Always return the smallest string possible do justification := No_justification ensure not_justified end remove_separator -- Remove the separator. do separator := '%U' ensure separator = '%U' end right_justify -- Put padding on left do justification := Right_justification ensure right_justified end set_fill (c: CHARACTER_8) -- Fill numbers with c do fill_character := c ensure fill_character = c end set_separator (c: CHARACTER_8) -- Set the separator to c do separator := c ensure separator = c end set_sign (s: STRING_8) -- Set sign values for positive, zero, negative -- All values must be the same length. -- Stored as negative, zero, positive. require s /= Void s.count >= 3 s.count \\ 3 = 0 do sign_string := s.twin ensure sign_set: sign_string ~ s end set_width (w: INTEGER_32) -- Set width to w require wide_enough: w >= 1 do width := w ensure width = w end sign_cr_dr -- Set sign for CR/DR do sign_string := "CR DR" ensure sign_string ~ "CR DR" end sign_dr_cr -- Set sign for DR/CR do sign_string := "DR CR" ensure sign_string ~ "DR CR" end sign_floating_dollar -- Set sign for floating dollar. do sign_string := "$$$" sign_leading ensure sign_string ~ "$$$" end sign_floating_dollar_signed -- Set sign for floating dollar include sign. do sign_string := "-$ $+$" sign_leading ensure sign_string ~ "-$ $+$" end sign_ignore -- Do not show sign. do sign_format := Ignore_sign_value ensure ignore_sign end sign_leading -- Set the sign to lead do trailing_sign := False ensure leading_sign end sign_negative_only -- Show sign for negative numbers only. do sign_format := Sign_negative_value ensure show_sign_negative end sign_normal -- Set sign for - and +. do sign_string := "- +" ensure sign_string ~ "- +" end sign_positive_only -- Show sign for positive numbers only. do sign_format := Sign_positive_value ensure show_sign_positive end sign_show -- Show sign for all numbers. do sign_format := Show_sign_value ensure show_sign end sign_trailing -- Set the sign to trail do trailing_sign := True ensure trailing_sign end unbracket_negative -- Do not bracket negative numbers. do bracketted_negative := False ensure not bracketted_negative end underscore_separate -- Set separator to underscore. do separator := '_' ensure separator = '_' end zero_fill -- Fill numbers with zeros. do fill_character := '0' ensure fill_character = '0' end feature -- Conversion formatted (i: INTEGER_32): STRING_8 -- Format the integer. local sign: INTEGER_32 unsigned: INTEGER_32 do if i < 0 then sign := -1 unsigned := - i elseif i > 0 then sign := 1 unsigned := i end if not no_separator then Result := split (unsigned.out) else Result := unsigned.out end if not ignore_sign or bracketted_negative then Result := process_sign (Result, sign) end if justification /= No_justification and then Result.count < width then Result := justify (Result) end ensure exists: Result /= Void correct_length: not_justified or Result.count >= width end feature -- Duplication frozen clone (other: detachable ANY): like other obsolete "Use `twin' instead. [2017-05-31]" -- Void if other is void; otherwise new object -- equal to other -- -- For non-void other, clone calls copy; -- to change copying/cloning semantics, redefine copy. -- (from ANY) do if other /= Void then Result := other.twin end ensure -- from ANY instance_free: class equal: Result ~ other end copy (other: FORMAT_INTEGER) -- Update current object using fields of object attached -- to other, so as to yield equal objects. -- (from ANY) require -- from ANY other_not_void: other /= Void type_identity: same_type (other) external "built_in" ensure -- from ANY is_equal: Current ~ other end frozen deep_clone (other: detachable ANY): like other obsolete "Use `deep_twin' instead. [2017-05-31]" -- Void if other is void: otherwise, new object structure -- recursively duplicated from the one attached to other -- (from ANY) do if other /= Void then Result := other.deep_twin end ensure -- from ANY instance_free: class deep_equal: deep_equal (other, Result) end frozen deep_copy (other: FORMAT_INTEGER) -- Effect equivalent to that of: -- copy (other . deep_twin) -- (from ANY) require -- from ANY other_not_void: other /= Void do copy (other.deep_twin) ensure -- from ANY deep_equal: deep_equal (Current, other) end frozen deep_twin: FORMAT_INTEGER -- New object structure recursively duplicated from Current. -- (from ANY) external "built_in" ensure -- from ANY deep_twin_not_void: Result /= Void deep_equal: deep_equal (Current, Result) end frozen standard_clone (other: detachable ANY): like other obsolete "Use `standard_twin' instead. [2017-05-31]" -- Void if other is void; otherwise new object -- field-by-field identical to other. -- Always uses default copying semantics. -- (from ANY) do if other /= Void then Result := other.standard_twin end ensure -- from ANY instance_free: class equal: standard_equal (Result, other) end frozen standard_copy (other: FORMAT_INTEGER) -- Copy every field of other onto corresponding field -- of current object. -- (from ANY) require -- from ANY other_not_void: other /= Void type_identity: same_type (other) external "built_in" ensure -- from ANY is_standard_equal: standard_is_equal (other) end frozen standard_twin: FORMAT_INTEGER -- New object field-by-field identical to other. -- Always uses default copying semantics. -- (from ANY) external "built_in" ensure -- from ANY standard_twin_not_void: Result /= Void equal: standard_equal (Result, Current) end frozen twin: FORMAT_INTEGER -- New object equal to Current -- twin calls copy; to change copying/twinning semantics, redefine copy. -- (from ANY) external "built_in" ensure -- from ANY twin_not_void: Result /= Void is_equal: Result ~ Current end feature -- Basic operations frozen as_attached: attached FORMAT_INTEGER obsolete "Remove calls to this feature. [2017-05-31]" -- Attached version of Current. -- (Can be used during transitional period to convert -- non-void-safe classes to void-safe ones.) -- (from ANY) do Result := Current end frozen default: detachable FORMAT_INTEGER -- Default value of object's type -- (from ANY) do end frozen default_pointer: POINTER -- Default value of type POINTER -- (Avoid the need to write p.default for -- some p of type POINTER.) -- (from ANY) do ensure -- from ANY instance_free: class end default_rescue -- Process exception for routines with no Rescue clause. -- (Default: do nothing.) -- (from ANY) do end frozen do_nothing -- Execute a null action. -- (from ANY) do ensure -- from ANY instance_free: class end feature {NONE} -- Implementation Center_justification: INTEGER_32 = 2 Ignore_sign_value: INTEGER_32 = 1 justify (s: STRING_8): STRING_8 -- Justify s. require room: s.count < width justification: justification /= No_justification local l, r: STRING_8 i, t: INTEGER_32 do Result := s if not centered then create l.make (width - s.count) from i := 1 until i > l.capacity loop l.extend (fill_character) i := i + 1 end if left_justified then Result.append (l) else Result.prepend (l) end else t := (width - s.count) // 2 if 2 * t + s.count < width then create l.make (t + 1) else create l.make (t) end create r.make (t) from i := 1 until i > r.capacity loop l.extend (fill_character); r.extend (fill_character) i := i + 1 end if i = l.capacity then l.extend (fill_character) end; Result.prepend (l); Result.append (r) end end Left_justification: INTEGER_32 = 1 No_justification: INTEGER_32 = 0 process_sign (s: STRING_8; sn: INTEGER_32): STRING_8 -- Process sign related values. local sstring: detachable STRING_8 do Result := s if bracketted_negative and sn = -1 then Result.precede ('('); Result.extend (')') end if ((show_sign_negative or show_sign) and sn = -1) or else ((show_sign_positive or show_sign) and sn = 1) or else (show_sign and sn = 0) then sstring := sign_value (sn) end if sstring /= Void then if trailing_sign then Result.append (sstring) else Result.prepend (sstring) end end end Right_justification: INTEGER_32 = 3 Show_sign_value: INTEGER_32 = 0 Sign_negative_value: INTEGER_32 = 3 Sign_positive_value: INTEGER_32 = 2 sign_size: INTEGER_32 -- Size of the sign. do Result := sign_string.count // 3 ensure Result * 3 = sign_string.count end sign_value (i: INTEGER_32): STRING_8 -- Value of the sign. require correct_sign: -1 <= i and i <= 1 local t: INTEGER_32 do t := sign_size * (i + 1) + 1 Result := sign_string.substring (t, t + sign_size - 1) end split (s: STRING_8): STRING_8 -- Apply separators to an integer require efficiency: separator /= '%U' local count, sep_length: INTEGER_32 do from count := s.count create Result.make (width) until count <= 3 loop from sep_length := 0 until sep_length = 3 loop Result.precede (s.item (count)) count := count - 1 sep_length := sep_length + 1 end; Result.precede (separator) end from until count = 0 loop Result.precede (s.item (count)) count := count - 1 end end feature -- Output Io: STD_FILES -- Handle to standard file setup -- (from ANY) once create Result; Result.set_output_default ensure -- from ANY instance_free: class io_not_void: Result /= Void end out: STRING_8 -- New string containing terse printable representation -- of current object -- (from ANY) do Result := tagged_out ensure -- from ANY out_not_void: Result /= Void end print (o: detachable ANY) -- Write terse external representation of o -- on standard output. -- (from ANY) local s: READABLE_STRING_8 do if attached o then s := o.out if attached {READABLE_STRING_32} s as s32 then Io.put_string_32 (s32) elseif attached {READABLE_STRING_8} s as s8 then Io.put_string (s8) else Io.put_string_32 (s.as_string_32) end end ensure -- from ANY instance_free: class end frozen tagged_out: STRING_8 -- New string containing terse printable representation -- of current object -- (from ANY) external "built_in" ensure -- from ANY tagged_out_not_void: Result /= Void end feature -- Platform Operating_environment: OPERATING_ENVIRONMENT -- Objects available from the operating system -- (from ANY) once create Result ensure -- from ANY instance_free: class operating_environment_not_void: Result /= Void end feature {NONE} -- Retrieval frozen internal_correct_mismatch -- Called from runtime to perform a proper dynamic dispatch on correct_mismatch -- from MISMATCH_CORRECTOR. -- (from ANY) local l_msg: STRING_32 l_exc: EXCEPTIONS do if attached {MISMATCH_CORRECTOR} Current as l_corrector then l_corrector.correct_mismatch else create l_msg.make_from_string ("Mismatch: ".as_string_32) create l_exc; l_msg.append (generating_type.name_32); l_exc.raise_retrieval_exception (l_msg) end end invariant sign_string_attached: sign_string /= Void sign_string_count: sign_string.count >= 3 sign_string_equal_parts: sign_string.count \\ 3 = 0 wide_enough: width >= 1 No_justification <= justification and justification <= Right_justification -- from ANY reflexive_equality: standard_is_equal (Current) reflexive_conformance: conforms_to (Current) note copyright: "Copyright (c) 1984-2012, 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 FORMAT_INTEGER
Generated by ISE EiffelStudio