boolstr.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 BOOL_STR < $TEXT, $ANCHORED_FMT
partial class BOOL_STR < $TEXT, $ANCHORED_FMT is
-- This partial class provides for the conversion of logical quantities
-- to/from text strings.
-- Version 1.0 Sep 98. Copyright K Hopper, U of Waikato
-- Development History
-- -------------------
-- Date Who By Detail
-- ---- ------ ------
-- 28 Sep 98 kh Original from BOOL class
private shared Names : ARRAY{STR} ; -- for external representation!
private const Msg_Cnt : CARD := 2 ;
private shared lib : LIBCHARS ;
-- The index value corresponding to self.card when self is true shall be
-- the element of this array which contains a text representation of true,
-- the other element shall contain a text representation of false.
private check_names( loc_lib : LIBCHARS)
pre ~void(loc_lib)
post (lib = loc_lib) and (Names.asize = Msg_Cnt)
is
-- This auxiliary routine attempts to set the value of the Names array
-- if void or of a different culture to the required one. If this action
-- is not successful then an exception is raised.
if void(Names) or (lib /= loc_lib) then
lib := loc_lib ;
Names := lib.culture.resources.read(SYS::rune_name(self),Msg_Cnt)
end
end ;
private equals( first, second : STR ) : BOOL
pre ~void(first) and ~void(second)
post true
is
-- This predicate tests for string equality up to the length of
-- first! The test is case independent.
one : STR := first.lower ;
two : STR := second.lower ;
loop
if ~(one.elt! = two.elt!) then return false; end;
end ;
return true
end ;
private lookup( str : STR ) : SAME
pre ~void(str)
post true
is
-- This function returns the truth value specified in text
-- string form in str. Matching the string in the table is done
-- independently of case until the str is found to be unambiguous.
if equals(str,Names[0]) then
return false
elsif equals(str,Names[1]) then
return true
else -- Not a boolean string!
SYS_ERROR::create.error(self,SYS_EXCEPT::Bad_Value,str) ;
return void -- to keep compiler happy!
end
end ;
is_bool( str : STR ) : CONVERSION_RESULTS
pre true
post (result = CONVERSION_RESULTS::All_Right)
or (result = CONVERSION_RESULTS::Out_of_Range)
or (result = CONVERSION_RESULTS::Empty)
is
-- This routine checks that the format of of the leading characters of
-- the given string in the given repertoire and encoding corresponds to that
-- required for a real number, returning the relevant result state.
if str.size = 0 then
return CONVERSION_RESULTS::Empty
end ;
check_names(str.index_lib) ;
protect
dummy : BOOL := lookup(str) ;
return CONVERSION_RESULTS::All_Right
when STR then
return CONVERSION_RESULTS::Out_of_Range
when SAME then
return CONVERSION_RESULTS::Out_of_Range
end
end ;
build( loc_cursor : STR_CURSOR ) : SAME
pre ~void(loc_cursor) and ~loc_cursor.is_done
post (void(result) and (initial(loc_cursor.index) = loc_cursor.index))
or (initial(loc_cursor.index) < loc_cursor.index)
is
-- This routine creates the truth value corresponding to the textual
-- representation contained in the string indicated or the cursor has not
-- been moved!
start_index : CARD := loc_cursor.index ;
loc_lib : LIBCHARS := loc_cursor.buffer.index_lib ;
loc_cursor.skip_space ; -- skip padding
me : SAME ;
loc_str : STR := STR::create(loc_lib) + loc_cursor.get_item ;
-- in non-alphabetic scripts at least one character must be tested!
loc_str := loc_str +
loc_cursor.get_pred(bind(_.is_alpha(loc_str.index_lib))) ;
-- more up to a non-letter item!
if is_bool(loc_str) = CONVERSION_RESULTS::All_Right then
return lookup(loc_str)
else -- back to original position!
loc_cursor.set_index(start_index) ;
return void
end
end ;
create( str : STR ) : SAME is
-- This is the creation routine from a string which is any of the
-- abbreviations listed in the Name table. If the name table has not been
-- read in this is done first.
return build(str.cursor)
end ;
str( lib : LIBCHARS ) : STR
pre ~void(lib)
post (result = Names[card])
is
-- This routine provides a string representation of self in the default
-- character repertoire and encoding.
check_names(lib) ; return Names[card]
end ;
str : STR
pre true
-- post (result = Names[card])
is
-- This routine provides a string representation of self in the default
-- character repertoire and encoding.
if self then return "true"; else return "false"; end;
--return str(LIBCHARS::default)
end ;
fmt( format : ANCHORED_DESCR, lib : LIBCHARS ) : STR 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 is
-- This routine returns the formatted string representation of self in
-- accordance with the string format.
return fmt(format,LIBCHARS::default)
end ;
end ; -- BOOL_STR