complexstr.sa
Generated by gen_html_sa_files from ICSI. Contact gomes@icsi.berkeley.edu for details
-------------------------> GNU Sather - sourcefile <-------------------------
-- Copyright (C) 2000 by K Hopper, University of Waikato, New Zealand --
-- This file is part of the GNU Sather library. It is free software; you may --
-- redistribute and/or modify it under the terms of the GNU Library General --
-- Public License (LGPL) as published by the Free Software Foundation; --
-- either version 2 of the license, or (at your option) any later version. --
-- This library is distributed in the hope that it will be useful, but --
-- WITHOUT ANY WARRANTY without even the implied warranty of MERCHANTABILITY --
-- or FITNESS FOR A PARTICULAR PURPOSE. See Doc/LGPL for more details. --
-- The license text is also available from: Free Software Foundation, Inc., --
-- 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA --
--------------> Please email comments to <bug-sather@gnu.org> <--------------
partial class COMPLEX_STR{T} < $TEXT, $FLT_FMT
partial class COMPLEX_STR{T} < $TEXT, $FLT_FMT is
-- This partial class provides numeric real number conversion routines,
-- to and from character strings.
-- The character representation of a complex number consists of two
-- floating point values separated by a space-surrounded sign and followed
-- by an imaginary component indicator as in "1.23 + 4.56j".
-- Version 1.1 Mar 99. Copyright K Hopper, U of Waikato
-- Development History
-- -------------------
-- Date Who By Detail
-- ---- ------ ------
-- 2 Oct 98 kh Original from RAT_STR/FLOAT_STR
-- 29 Mar 99 kh Revised for V8 of text classes
private const Imaginary_Component_Indicator : UNICODE :=
UNICODE::LATIN_SMALL_LETTER_J ;
private scan(
cursor : STR_CURSOR
) : SAME
pre ~void(cursor)
post true -- should involve 'void'
is
-- This routine is the complex number 'scanner' to obtain the value
-- expected to be next in the indicated string - or returning with the string
-- unchanged, returning void if there is a format error or NaN if there is
-- a range error.
start_index : CARD := cursor.index ;
loc_lib : LIBCHARS := cursor.buffer.index_lib ;
loc_start : CARD ;
loc_re : T := T::build(cursor) ;
if cursor.index = start_index then
return void
else
loc_start := cursor.index ;
cursor.skip_space ;
if cursor.item = loc_lib.Plus_Sign.char
or cursor.item = loc_lib.Minus_Sign.char then
cursor.skip_space
else
cursor.set_index(start_index) ;
return void
end ;
loc_start := cursor.index
end ;
loc_im : T := T::build(cursor) ;
loc_ICI : CHAR :=
CHAR_CODE::create(Imaginary_Component_Indicator.card,loc_lib).char ;
if cursor.index = loc_start then
cursor.set_index(start_index) ;
return void
elsif cursor.item = loc_ICI then
return re(loc_re).im(loc_im)
else
cursor.set_index(start_index) ;
return void
end
end ;
is_complex(
str : STR_CURSOR
) : CONVERSION_RESULTS
pre true
post (result = CONVERSION_RESULTS::All_Right)
or (result = CONVERSION_RESULTS::Out_of_Range)
or (result = CONVERSION_RESULTS::Bad_Format)
or (result = CONVERSION_RESULTS::Empty)
is
-- This routine checks that the format of of the leading characters of
-- the given string using the given repertoire and encoding corresponds to
-- that required for a complex number, returning the relevant result state.
if str.remaining = 0 then
return CONVERSION_RESULTS::Empty
end ;
start_index : CARD := 0 ;
loc_cursor : STR_CURSOR := str ;
loc_res : SAME := scan(loc_cursor) ;
if loc_cursor.index = start_index then
return CONVERSION_RESULTS::Bad_Format
else
loc_cursor.set_index(start_index) ;
return CONVERSION_RESULTS::All_Right
end
end ;
build(
loc_cursor : STR_CURSOR
) : SAME
pre ~void(loc_cursor)
and (((loc_cursor.remaining >= 3) -- as in x.y
and ((loc_cursor.item /=
loc_cursor.buffer.index_lib.Plus_Sign.char)
and (loc_cursor.item /=
loc_cursor.buffer.index_lib.Minus_Sign.char)))
or (loc_cursor.remaining >= 4))
and ~loc_cursor.is_done
post true -- should involve 'void'
is
-- This routine creates the complex number corresponding to the textual
-- representation contained in str which must have a digit both before and
-- after a mandatory decimal mark! If the string indicated does not
-- contain a real number as the next non-blank item in the string then zero is
-- returned or if out of range then quiet_NaN is returned -- and the cursor
-- has not been moved!
return scan(loc_cursor)
end ;
create(
str : STR
) : SAME
pre ~void(str.index_lib)
and (is_complex(str.cursor) = CONVERSION_RESULTS::All_Right)
post true
is
-- This routine creates the real number corresponding to the textual
-- representation contained in str.
return build(str.cursor)
end ;
num_chars : CARD is
-- This routine returns the number of digits in the result, excluding
-- any exponent.
loc_val : T := im.abs ;
return re.num_chars + loc_val.num_chars + 3
end ;
str(
lib : LIBCHARS
) : STR
pre ~void(lib)
-- and (re /= T::quiet_NaN(0.int))
-- and (im /= T::quiet_NaN(0.int))
post (result.size > 0)
is
-- This routine returns a string representation of self using the given
-- repertoire and encoding. The format will be either fixed point or
-- floating point dependent upon the value of self.
res : STR := re.str(lib) + lib.Space.char ;
loc_im : T := im ;
if loc_im < T::zero then
res := res + lib.Minus_Sign.char ;
loc_im := -loc_im
else
res := res + lib.Plus_Sign.char
end ;
return res + lib.Space.char + loc_im.str(lib) +
CHAR_CODE::create(Imaginary_Component_Indicator.card,lib).char
end ;
str : STR
pre true -- (re /= T::quiet_NaN(0.int))
-- and (im /= T::quiet_NaN(0.int))
post (result.size > 0)
is
-- This routine returns a string representation of self using the given
-- repertoire and encoding. The format will be either fixed point or
-- floating point dependent upon which of the two is the shorter string.
return str(LIBCHARS::default)
end ;
fmt(
format : FLT_DESCR,
lib : LIBCHARS
) : STR
pre ~void(format)
and ~void(lib)
post result.size > 0
is
-- This routine returns a formatted representation of self in the given
-- repertoire and encoding as dictated by the given format description.
--
-- NOTE This pair of routines produces a string in which the teo components
-- are formatted individually as indicated. Further layout may be done
-- by using the last formatting routine.
res : STR := re.fmt(format,lib) + lib.Space.char ;
loc_im : T := im ;
if loc_im < T::zero then
res := res + lib.Minus_Sign.char ;
loc_im := -loc_im
else
res := res + lib.Plus_Sign.char
end ;
return res + lib.Space.char + loc_im.fmt(format,lib) +
CHAR_CODE::create(Imaginary_Component_Indicator.card,lib).char
end ;
fmt(
format : FLT_DESCR
) : STR
pre ~void(format)
post result.size > 0
is
-- This routine returns a formatted representation of self in the default
-- repertoire and encoding as dictated by the given format description.
return fmt(format,LIBCHARS::default)
end ;
fmt(
format : FLT_DESCR,
final_fmt : ANCHORED_DESCR, -- anchored about central sign
lib : LIBCHARS
) : STR
pre ~void(format)
and ~void(lib)
post result.size > 0
is
-- This routine returns a formatted representation of self in the given
-- repertoire and encoding as dictated by the given format description.
--
-- NOTE This pair of routines produces a string in which the teo components
-- are formatted individually as indicated. Further layout may be done
-- by using the following pair of routines instead.
res : STR ;
tail : STR ;
res := re.fmt(format,lib) + lib.Space.char ;
if final_fmt.leading > res.size then
-- needs some leading fillers
res := STR::create(final_fmt.filler.card,lib).repeat(
final_fmt.leading - res.size) + res
end ;
loc_im : T := im ;
if loc_im < T::zero then
res := res + lib.Minus_Sign.char ;
loc_im := -loc_im
else
res := res + lib.Plus_Sign.char
end ;
tail := STR::create + lib.Space.char + loc_im.fmt(format,lib) +
Imaginary_Component_Indicator.code.char ;
if final_fmt.trailing > tail.size then
tail := tail + STR::create(final_fmt.filler.card,lib).repeat(
final_fmt.trailing - tail.size)
end ;
return res + tail
end ;
end ; -- COMPLEX_STR