rangestr.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 RANGE_STR{T} < $TEXT, $ANCHORED_FMT
partial class RANGE_STR{T} < $TEXT, $ANCHORED_FMT is
-- This partial class implements formatting services for range classes.
-- Version 1.0 Oct 98. Copyright K Hopper, U of Waikato
-- Development History
-- -------------------
-- Date Who By Detail
-- ---- ------ ------
-- 2 Oct 98 kh Original from RAT_STR class
private ellipsis(
ch : CHAR,
lib : LIBCHARS
) : BOOL
pre ~void(lib)
post result = (ch = lib.Fullstop.char)
is
-- This private routine returns true if ch is a full-stop (for an
-- ellipsis), otherwise false.
return ch = lib.Fullstop.char
end ;
private scan(
cursor : STR_CURSOR
) : SAME
pre ~void(cursor)
post (result.is_empty
and (initial(cursor.index) = cursor.index))
or (initial(cursor.index) < cursor.index)
is
-- This routine is the real number 'scanner' to obtain the real number
-- value expected to be next in the indicated string - or returning with
-- the string unchanged, returning void if there is a format error.
start_index : CARD := cursor.index ;
loc_lib : LIBCHARS := cursor.buffer.index_lib ;
loc_low : T := T::build(cursor) ;
if cursor.index = start_index then
return empty
else
loc_start : CARD := cursor.index ;
cursor.skip_space ;
if ~ellipsis(cursor.item,loc_lib) then -- not a range!
cursor.set_index(start_index) ;
return empty
else
dummy : STR := cursor.get_upto(bind(ellipsis(_,loc_lib)))
end
end ;
cursor.skip_space ;
loc_start := cursor.index ;
loc_high : T := T::build(cursor) + T::one ;
if cursor.index = loc_start then
cursor.set_index(start_index) ;
return empty
else
return low(loc_low).high(loc_high)
end
end ;
is_range(
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 rational 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)
post (result.is_empty
and (initial(loc_cursor.index) = loc_cursor.index))
or (initial(loc_cursor.index) < loc_cursor.index)
is
-- This routine creates the real 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 (is_range(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 ;
str(
lib : LIBCHARS
) : STR
pre ~void(lib)
post ~void(result)
is
-- This routine returns a string representation of self as a vulgar
-- fraction (unless the value is integral) using the given repertoire and
-- encoding.
if is_empty then
return STR::create + lib.Left_Bracket.char +
lib.Right_Bracket.char
else
return STR::create + lib.Left_Bracket.char +
low.str(lib) +
lib.Fullstop.char + lib.Fullstop.char +
lib.Fullstop.char +
(high - T::one).str(lib) +
lib.Right_Bracket.char
end
end ;
str : STR is
-- This routine returns a string representation of self as a vulgar
-- fraction using the default repertoire and encoding.
return str(LIBCHARS::default)
end ;
fmt(
format : ANCHORED_DESCR,
lib : LIBCHARS
) : STR
pre ~void(format)
and ~void(lib)
post ~void(result)
is
-- This routine returns the formatted string representation of self in
-- accordance with the string format. This provides special case treatment
-- where the leading component is exactly one - it reduces the value string
-- to the single leading character - which is therefore expected to differ
-- for the two values!.
res : STR := str(lib) ;
loc_fill : STR := STR::create(lib) + format.filler.char ;
if format.leading = 1 then
res := res.head(1)
elsif res.size < format.leading then -- needs a filler
res := loc_fill.repeat(format.leading - res.size) + res
end ;
return res + loc_fill.repeat(format.trailing)
end ;
fmt(
format : ANCHORED_DESCR
) : STR
pre ~void(format)
post ~void(result)
is
-- This routine returns the formatted string representation of self in
-- accordance with the string format.
return fmt(format,LIBCHARS::default)
end ;
end ; -- RANGE_STR{T}