intistr.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 INTI_STR < $TEXT, $EXACT_FMT

partial class INTI_STR < $TEXT, $EXACT_FMT is -- This partial class provides numeric whole number conversion routines, -- to and from character strings. -- Version 1.1 Mar 99. Copyright K Hopper, U of Waikato -- Development History -- ------------------- -- Date Who By Detail -- ---- ------ ------ -- 15 Sep 98 kh Original from Modula-2 standard -- 30 Mar 99 kh Revised for V8 of text classes private const log10D : CARD := 4 ; private const Decimal : CARD := 10 ^ log10D ; -- decimal base; D <= B private const Default_Base : CARD := 10 ; -- for conversions! private opt_sign(cursor : STR_CURSOR) : BOOL pre ~void(cursor) and ~cursor.is_done post true is --This private routine checks to see if a minus or plus sign are in -- the indicated string next. If and only if any such sign is negative then -- true is returned! The sign (if present) has been skipped over on return. loc_ch : CHAR := cursor.item ; loc_lib : LIBCHARS := cursor.buffer.index_lib ; res : BOOL := (loc_ch = loc_lib.Minus_Sign.char) ; if (loc_ch = loc_lib.Minus_Sign.char) or (loc_ch = loc_lib.Plus_Sign.char) then cursor.advance end ; return res end ; private scan(cursor : STR_CURSOR,out negative : BOOL) : FLIST{CARD} pre ~void(cursor) and ~cursor.is_done post (void(result) and (initial(cursor.index) = cursor.index)) or (initial(cursor.index) < cursor.index) is --This routine is the integer number scanner which returns void with -- an unchanged string if there is a format problem, otherwise the digit string! start_index : CARD := cursor.index ; loc_lib : LIBCHARS := cursor.buffer.index_lib ; loc_format : NUMBER_FMT := cursor.buffer.index_lib.culture.numeric.format ; negative := opt_sign(cursor) ; if ~cursor.item.is_digit(loc_lib) then cursor.set_index(start_index) ; return void end ; return loc_format.digit_string(cursor,true,Default_Base) end ; is_inti(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 ; negative : BOOL ; -- not needed in this routine loc_cursor : STR_CURSOR := str.cursor ; start_index : CARD := 0 ; loc_dummy : FLIST{CARD} := scan(loc_cursor,out negative) ; if start_index = loc_cursor.index then -- bad or too large 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.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 integer number corresponding to the textual -- representation contained in the string indicated. If the string indicated -- does not contain an exact number as the next non-blank item in the string -- then zero is returned and the cursor has not been moved! start_index : CARD := loc_cursor.index ; negative : BOOL ; me : SAME := create(0) ; loc_digits : FLIST{CARD} := scan(loc_cursor,out negative) ; loc_res : CARD := 0 ; loc_index : CARD := loc_cursor.index ; loop loc_res := loc_res * 10 + loc_digits.elt! ; if loc_cursor.index - loc_index = log10D then me := u_times_plus(me, Decimal, loc_res) ; loc_res := 0 ; loc_index := loc_cursor.index end end ; if loc_cursor.index > loc_index then me := u_times_plus(me, 10^(loc_cursor.index - loc_index), loc_res) end ; if negative then me.len := -me.len end ; return me end ; create(str : STR,index : CARD) : SAME pre (str.size > index) post true is -- This routine creates an infinite integer from its decimal string -- representation starting at the given index in str. If no number is -- there then zero is returned. loc_cursor : STR_CURSOR := str.cursor ; loc_cursor.set_index(index) ; return build(loc_cursor) end ; create(str : STR) : SAME pre is_inti(str) = CONVERSION_RESULTS::All_Right post true is --This routine returns the whole number corresponding to -- the given string in the default repertoire and encoding. return build(str.cursor) end ; create(str : FSTR,index : CARD) : SAME pre ~void(str) and (index < str.size) post true is --This routine creates an infinite integer from its decimal string -- representation starting at the given index in str. If no number is -- there then zero is returned. loc_cursor : STR_CURSOR := str.str.cursor ; loc_cursor.set_index(index) ; return build(loc_cursor) end ; create(str : FSTR) : SAME pre ~void(str) and (str.size > 0) post true is --This routine creates an infinite integer out of the text string str, -- starting at the beginning of it. return build(str.str.cursor) end ; private to_digits(base : CARD) : FLIST{CARD} pre ~void(self) and (base > 0) post (result.size > 0) is -- This routine converts self into a list of digits with the least -- significant digit first. loc_val : SAME := self.abs ; -- the sign is not considered here res : FLIST{CARD} := FLIST{CARD}::create ; loc_div : SAME := create(base) ; -- numeric base loop loc_tmp : SAME := loc_val % loc_div ; res := res.push(loc_tmp.card) ; loc_val := loc_val / loc_div ; if loc_val = zero then break! end end ; return res end ; digits : FLIST{CARD} pre ~void(self) post result.size > 0 is --This public routine returns the number of decimal digits in the -- string representation of self.abs. return to_digits(10) end ; str(lib : LIBCHARS) : STR pre ~void(lib) post (result.size > 0) is -- This routine returns a string representation of self using the given -- repertoire and encoding. res : STR := STR::create(lib) ; if self < zero then res := res + lib.Minus_Sign.char end ; return res + lib.culture.numeric.format.fmt(to_digits(Default_Base),0,false,lib).tgt_str end ; decimal_str : STR post (result.size > 0) is -- This routine returns a string representation of self using the given -- repertoire and encoding. lib::=LIBCHARS::default; res : STR := ""; --STR::create(lib) ; if self < zero then res := res + lib.Minus_Sign.char end ; return res + lib.culture.numeric.format.fmt_no_sep(to_digits(Default_Base),0,false,lib).tgt_str end ; str : STR pre true post (result.size > 0) is --This routine returns a string representation of self using the given -- repertoire and encoding. return str(LIBCHARS::default) end ; fmt(format : EXACT_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. res : STR := str(lib) ; loc_fill : STR := STR::create(lib) + format.filler.char ; if res.size < format.whole_digits then -- needs a filler res := loc_fill.repeat(format.whole_digits - res.size) + res end ; return res end ; fmt(format : EXACT_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 ; end ; -- INTI_STR