## char-set.pkg
#
# Fast, read-only, character sets.
#
# These are meant to be used to construct
# predicates for the functions in Strings.
# Compiled by:
#
src/lib/std/standard.lib # Char_Set is from
src/lib/std/src/char-set.apipackage char_set
: Char_Set
{
package ba = rw_vector_of_one_byte_unts;
# An immutable set of characters
#
Char_Set
=
CHAR_SET ba::Rw_Vector;
# Make a character set consisting of
# the characters of the given string.
#
fun make_char_set s
=
{ ba = ba::make_rw_vector (256, 0u0);
fun ins i
=
{ ba::set (ba, string::get_byte (s, i), 0u1);
ins (i+1);
};
ins 0
except
_ = ();
CHAR_SET ba;
};
# Make a character set consisting
# of the characters whose ordinals are
# given by the list of integers.
#
fun make_char_set_from_list l
=
{ ba = ba::make_rw_vector (256, 0u0);
fun ins (c ! r)
=>
{ ba::set (ba, c, 0u1);
ins r;
};
ins []
=>
();
end;
ins l
except
_ = lib_base::failure { module=> "char_set", fn=>"make_char_set_from_list", msg=>"range error" };
CHAR_SET ba;
};
# Return a string representation of a character set
#
fun to_string (CHAR_SET ba)
=
f (255, [])
where
fun f (-1, l)
=>
implode l;
f (i, l)
=>
ba::get (ba, i) == 0u1
##
?? f (i - 1, (char::from_int i) ! l)
:: f (i - 1, l);
end;
end;
# Return TRUE if the character of
# the given ordinal is in the set:
#
fun is_in_set (CHAR_SET ba) i
=
ba::get (ba, i) == 0u1;
# (in_set c (s, i)) is equivalent to (inSetOrd c (ro_int8_vec_get (s, i)))
#
fun string_element_is_in_set (CHAR_SET ba) (s, i)
=
ba::get (ba, string::get_byte (s, i)) == 0u1;
}; # package char_set
## AUTHOR: John Reppy
## AT&T Bell Laboratories
## Murray Hill, NJ 07974
## jhr@research.att.com
## COPYRIGHT (c) 1993 by AT&T Bell Laboratories. See SMLNJ-COPYRIGHT file for details.
## Subsequent changes by Jeff Prothero Copyright (c) 2010-2015,
## released per terms of SMLNJ-COPYRIGHT.