binstr.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>  <--------------


class BINSTR < $STRING{OCTET,FBINSTR,BINSTR}, $IS_EQ, $IS_LT{BINSTR},$IMMUTABLE, $HASH

class BINSTR < $STRING{OCTET,FBINSTR,BINSTR}, $IS_EQ, $IS_LT{BINSTR},$IMMUTABLE, $HASH is -- This class is an immutable version of FBINSTR where every operation -- yielding a BINSTR creates a new buffer to contain it. -- Version 1.3 Apr 99. Copyright K Hopper, U of Waikato -- Development History -- ------------------- -- Date Who By Detail -- ---- ------ ------ -- 1 Dec 96 kh Original built round CHAR -- 3 Jan 97 kh Changed to OCTET from CHAR -- 10 Jan 97 kh Separated out common string parts. -- 14 Apr 99 kh Revised for V8 of text classes. include STRING{OCTET,FBINSTR} ; include STR_SEARCH{OCTET} ; include BINARY ; build(cursor : BIN_CURSOR) : SAME pre true post (result.size = cursor.remaining) is --This routine builds a binary string from the remainder of the indicated buffer. return cursor.get_remainder end ; create : SAME is --This routine yields void as an empty string. return void end ; create(size : CARD) : SAME is --This produces a binary string of the given size - empty! return aref_create(size) end ; create(oct : OCTET) : SAME pre true post (result.size = 1) and (result[0] = oct) is --This produces a binary string with only one element with the value oct me : SAME := new(1) ; me[0] := oct ; return me end ; create(arr : ARRAY{OCTET}) : SAME pre true post (result.size = arr.size) -- and contents are identical is --This produces a binary string with a copy of the contents of arr. me : SAME := create(arr.size) ; loop me.set!(arr.elt!) end ; return me end ; private is_eq_helper(bstr : SAME,cnt : CARD) : BOOL is -- This predicate returns true if and only if the binary string is the -- same as self. builtin STR_MEMCMP_STR_CARD end ; is_eq(other : SAME) : BOOL is --This predicate returns true if and only if self and other are the -- same. Either may be void. if void(self) then if void(other) then return true else return (other.asize = 0) end elsif void(other) then return (asize = 0) elsif asize /= other.asize then return false else return is_eq_helper(other,asize) end end ; is_lt(other : SAME) : BOOL is --This predicate returns true if and only if the bit pattern contained -- in the string is octet for octet less than the bit_pattern contained -- in other. if size = 0 then if other.size /= 0 then return true else return false end elsif other.size = 0 then return false else loop val_self : CARD := self.elt!.card ; val_other : CARD := other.elt!.card ; if val_self < val_other then return true elsif val_self > val_other then return false end end ; return false end end ; size : CARD is --This routine returns the number of elements in the string. if void(self) then return CARD::zero else return asize end end ; cursor : BIN_CURSOR pre true -- self may be void! post (void(self) and void(result)) or (~void(self) and ~void(result) and result.buffer = self) is -- This routine returns a cursor object corresponding to the contents of self. if void(self) then return void else return BIN_CURSOR::create(self) end end ; fbinstr : FBINSTR pre true post (void(self) and (result.size = 1)) or (result.binstr = self) is -- This routine converts self into a mutable version of a binary string -- by using its own creation routine. return FBINSTR::create(self) end ; binstr : BINSTR is -- This routine returns a copy of self. It is provided to meet the -- interface requirements of $BINARY. return copy end ; sized : SAME pre (size <= OCTET::Octet_Max) post (result.asize = self.asize + 1) and (result[0].card = self.asize) and (result.tail(asize) = self) is -- This routine is provided to 'match' the get_sized binary cursor -- feature. It returns a copy of self preceded by a size octet. res : SAME := create(OCTET::create(size)) ; if size > 0 then loc_res : SAME := create(size) ; loc_res.acopyn(self,size) ; res := res + loc_res end ; return res end ; plus(hex : HEXTET) : SAME pre true post (result.size = (self.asize + 2)) and (hex.create(result.tail(2)) = hex) and (result.head(result.asize - 2) = self) is -- This routine returns a new string consisting of the contents -- of self with hex appended to it. It is provided as a convenience in -- stringing together hextets/octets/etc. src : SAME := hex.binstr ; return append_destroy(src, true) end ; private store_index(elem_index : CARD) : CARD is -- This routine returns the store index corresponding to the given -- element_index for use where they may be different). In this class the two -- indices are identical. return elem_index end ; private null_tail(buff : SAME,tail : CARD) is --This private predicate is used solely in post-condition testing. -- It returns true if and only if all tail elements of buff are the null -- octet, otherwise false. loop index : CARD := (buff.size - tail).upto!(buff.size - 1) ; if buff[index] /= OCTET::create(0) then return false end end ; return true end ; private null_tail(str : SAME,from : CARD) : BOOL is --This private predicate returns true if and only if the tail (starting -- at from) is all null octets. loop if ~(str.elt!(from) = OCTET::null) then return false end end ; return true end ; pad(length : CARD) : SAME pre true post ((length <= self.asize) and (result = self)) or ((result.asize = length) and null_tail(result,self.asize)) is -- This routine returns self padded with null octets up to the given -- length unless self is equal or greater to the length when self is returned. if length <= size then return self end ; res : FBINSTR := FBINSTR::create(self) ; loop (length - size).times! ; res := res.push(OCTET::null) end ; return res.binstr end ; text_str(lib : LIBCHARS) : STR pre ~void(self) and ~void(lib) post (result.size = (self.size * 3 - 1)) -- right number of chars! is -- This routine returns a representation of the contents of self as -- a sequence of hexadecimal octet representations, using the given -- repertoire and encoding. res : STR := STR::create ; loc_sep : STR := lib.Space.char.str(lib) ; loop res := res + loc_sep.separate!(elt!.hex_str(lib)) end ; return res end ; text_str : STR pre ~void(self) post (result.size = (self.size * 3 - 1)) -- right number of chars! is -- This routine returns a representation of the contents of self as -- a sequence of hexadecimal octet representations, using the default -- repertoire and encoding. return text_str(LIBCHARS::default) end ; str(lib : LIBCHARS) : STR pre ~void(self) and ~void(lib) and (asize % lib.my_size = 0) post (result.size * lib.my_size = self.size) is -- This routine is a synonym for the following routine to conform -- to the signature of $STR. res : STR := STR::create(asize/lib.my_size,lib) ; res.acopy(self) ; return res end ; str : STR pre ~void(self) post (result.size * LIBCHARS::default.my_size = self.size) is -- This routine returns the binary string as though it were text in the -- current repertoire and encoding (which need not be known here!)! loc_lib : LIBCHARS := LIBCHARS::default ; res : STR := STR::create(asize/loc_lib.my_size,loc_lib) ; res.acopy(self) ; return res end ; end ; -- BINSTR