hexstr.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 HEX_STR < $TEXT_STRING{CHAR,FSTR,HEX_STR}, $IMMUTABLE
class HEX_STR < $TEXT_STRING{CHAR,FSTR,HEX_STR}, $IMMUTABLE is
-- This class provides a string encoding of arbitrary codes of 32-bits.
-- It provides most of the operations which are provided in the class STR
-- in respect of characters which have a single encoding which is a single
-- octet only.
-- Version 1.0 Apr 99. Copyright K Hopper, U of Waikato
-- Development History
-- -------------------
-- Date Who By Detail
-- ---- ------ ------
-- 14 Apr 99 kh Original for V8 of text classes
include TEXT_STRING{CHAR,FSTR} ;
private const Width : CARD := 2 ;
private attr width : CARD ;
create(
sz : CARD,
lib : LIBCHARS
) : SAME
pre ~void(lib)
and (lib.my_size <= Width)
post ~void(result)
is
-- This routine is the 'general' creation one which is called by all of
-- the others in which the size is expected to be the number of character
-- codes!
me : SAME := new(sz * Width) ;
me.width := Width ;
me.priv_lib := REP_LIB_LIST::index(lib) ;
return me
end ;
create(
sz : CARD
) : SAME is
-- This routine is the version of creation which assumes the default
-- cultural repertoire and environment.
return create(sz,LIBCHARS::default)
end ;
create(
lib : LIBCHARS
) : SAME
pre ~void(lib)
and (lib.my_size <= Width)
post ~void(result)
is
-- This routine returns an empty character string.
return create(0,lib)
end ;
create : SAME is
-- This routine returns an empty character string, provided that the
-- default repertoire and encoding size is 1!.
if LIBCHARS::default.my_size = Width then
return create(0,LIBCHARS::default)
else
return void
end
end ;
create(
ch : CHAR,
lib : LIBCHARS
) : SAME
pre ~void(lib)
and (lib.my_size <= Width)
post true
is
-- This routine creates and then returns a single element character
-- string in the given repertoire and encoding.
me : SAME := create(1,lib) ;
me[0] := ch ;
return me
end ;
create(
ch : CHAR
) : SAME is
-- This routine creates and then returns a single element character
-- string.
return create(ch,LIBCHARS::default)
end ;
create(
ch_code : CHAR_CODE
) : SAME
pre ch_code.lib.my_size <= Width
post ~void(result)
is
-- This routine creates a new single character string from the argument.
return create(ch_code.char,ch_code.lib)
end ;
create(
str : STR
) : SAME
pre ~void(str)
and (str.index_lib.my_size <= Width)
post (result.size = str.size)
is
-- This routine attempts to create a new character string from the given
-- character string, using the same repertoire and encoding.
res : SAME := create(str.size,str.index_lib) ;
index : CARD := 0 ;
loop
res.aset(index,str.elt!) ;
index := index + 1
end ;
return res
end ;
from_fstr(
fstr : FSTR
) : SAME
pre ~void(fstr)
and (fstr.index_lib.my_size <= Width)
post (result.size = fstr.size)
is
-- This routine converts the given fast character string into a normal
-- character string.
res : SAME := create(fstr.size,fstr.index_lib) ;
res.acopyn(fstr,fstr.loc) ;
return res
end ;
private buffer_scan is
-- This is a do-nothing dummy to satisfy some of the common TEXT_STRING
-- requirements
end ;
size : CARD
pre true
post (void(self)
and (result = 0))
or (result = asize / Width)
is
-- This routine returns the number of characters in self, or zero if self
-- is void.
if void(self) then
return 0
else
return asize / Width
end
end ;
private store_index(
elem_index : CARD
) : CARD
pre (elem_index < size)
post (result = (elem_index * Width))
is
-- This routine returns the store index corresponding to the given
-- element_index for use where they may be different).
return elem_index * Width
end ;
aget(
index : CARD
) : CHAR
pre ~void(self)
and (index < size)
post true
is
-- This routine is the 'array' indexing facility for characters in a
-- string, returning the character indexed.
loc_res : BINSTR := BINSTR::create + oct_aget(index * Width) +
oct_aget(index * Width + 1) ;
return CHAR::create(loc_res)
end ;
aset(
index : CARD,
elem : CHAR
)
pre ~void(self)
and (index < size)
post true
is
-- This routine is the 'array' indexing facility for characters in
-- a string, setting the element specified to the given character coding.
loc_index : CARD := (index * Width) + 1 ;
oct_aset(loc_index,elem.aget(1)) ;
oct_aset(loc_index - 1,elem.aget(0))
end ;
char(
index : CARD
) : CHAR
pre (index < (asize - 1))
post result = [index]
is
-- This routine returns the value of the character at the given index.
return [index]
end ;
binstr : BINSTR
pre ~void(self)
post ~void(result)
is
-- This routine just returns a copy of self. It is provided for cases
-- where it is necessary to put a text string into a binary stream of some
-- kind.
res : BINSTR := BINSTR::create ;
loop
index : CARD := 0.upto!(size - 1) ;
loc_high : OCTET := oct_aget(index * Width + 1) ;
loc_low : OCTET := oct_aget(index * Width) ;
res := res + loc_high + loc_low
end ;
return res
end ;
convert(
lib : LIBCHARS
) : SAME
pre ~void(lib)
and ~lib.has_combining
and (lib.my_size = Width)
post true
is
-- This routine converts self to be in the given encoding and
-- repertoire. If any character has no corresponding code then void is
-- returned.
loc_res : RUNES := CODE_CONVERTER::runes(str) ;
return create(CODE_CONVERTER::str(lib,loc_res))
end ;
private do_replace(
old_ch,
new_ch : CHAR
) : SAME is
-- This routine returns a copy of self which has had every occurrence of
-- old_ch replaced by new_ch.
res : SAME := SAME::create(0,index_lib) ;
loop
ch : CHAR := elt! ;
if ch = old_ch then
ch := new_ch
end ;
res := res + ch
end ;
return res
end ;
replace(
set : SAME,
new_ch : CHAR
) : SAME
pre ~void(set)
and ~void(self)
post (result.size = self.size) -- and replacement done!
is
-- This routine returns a copy of self in which all occurrences of
-- characters in set are replaced by new_ch.
loc_res : CODE_STR := CODE_STR::create(index_lib) ;
loop
ch : CHAR := elt! ;
if set.contains(ch) then
ch := new_ch
end ;
loc_res := loc_res + ch.code
end ;
return create(loc_res.tgt_str)
end ;
escape(
esc : CHAR,
elist : SAME
) : SAME
pre ~void(self)
and ~void(elist)
and (elist.index_lib = index_lib)
post (result.contains(esc)
or (result = self))
is
-- This routine returns a copy of self in which all characters occurring
-- in elist and the character esc itself are preceded by the escape character.
-- This is done in situ using a fast string.
buf : SAME := SAME::create(index_lib) ;
loop
loc_ch : CHAR := elt! ;
if elist.contains(loc_ch)
or (loc_ch = esc) then
buf := buf + esc
end ;
buf := buf + loc_ch
end ;
return buf
end ;
split!(
once ch : CHAR
) : SAME
pre true
post (result.size >= 0) -- may be zero if two adjacent characters found
is
-- This iter yields successive substrings of self which are separated
-- by the single character given. The separating characters are omitted and
-- the string yielded starts after the previous separating character up to and
-- NOT containing the next (or the end of self if not found).
if void(self) then
quit
end ;
curr_loc : CARD := 0 ; -- Start of next string
loop
next_loc : CARD := search(ch,curr_loc) ;
if next_loc /= CARD::nil then -- The character was found
yield substring(curr_loc,next_loc - curr_loc) ;
curr_loc := next_loc + 1
else -- not found so use rest of string
yield substring(curr_loc,size - curr_loc) ;
quit
end
end
end ;
separate!(
str : SAME
) : SAME
pre ~void(str)
and ~void(self)
and (priv_lib = str.priv_lib)
post (result = str)
or (result = self + str)
is
-- On the first iteration just outputs rns, on successive iterations it
-- outputs self followed by rns. Useful for forming lists, eg
--
-- loop
-- #OUT + comma.separate!(a.elt!)
-- end ;
yield str ;
loop
yield self + str
end
end ;
str(
lib : LIBCHARS
) : STR
pre ~void(self)
post size = result.size
is
-- This routine returns the contents of self as a string in the given
-- repertoire and encoding.
res : STR := STR::create(lib) ;
loop
res := res + elt!
end ;
return res
end ;
str : STR
pre ~void(self)
post size = result.size
is
-- This routine returns the contents of self as a string in the same
-- repertoire and encoding.
res : STR := STR::create(index_lib) ;
loop
res := res + elt!
end ;
return res
end ;
end ; -- HEX_STR