[go: up one dir, main page]

Menu

[897c37]: / mlib / lib / range.lpc  Maximize  Restore  History

Download this file

146 lines (131 with data), 3.1 kB

inherit "RO/Servers";

/*
 * Integer Ranges:
 *  effectivly set implementation
 */

pack_range(arr)
/* take an array of integers, and convert it to a string
 */
{
}

array split_low_high(string str, int lowlimit, int highlimit)
{
    int low, high;
    array args;

    if (index(str, "..") != -1)
    {
        args = STRINGS->split_first(str, "..");
        low = atoi(args[0]);
        if (low < lowlimit) low = lowlimit;
        if (sizeof(args) == 2) high = atoi(args[1]);
        if ((high == 0 || high > highlimit) && highlimit) high = highlimit;
        return [ low, high ];
    }
    else if (index(str, "-") != -1)
    {
        args = STRINGS->split_first(str, "-");
        low = atoi(args[0]);
        if (sizeof(args) == 2) high = atoi(args[1]);
        if ((high == 0 || high > highlimit) && highlimit) high = highlimit;
        return [ low, high ];
    }
    else return 0;
}

array unpack_range(string str,int  lowlimit,int  highlimit)
/* convert a string into a set of integers..
 * string format is:
 *   strng ::= strng +","+field | ""
 *   field ::= int | int + delimiter + int
 *   int   ::= int + digit | ""
 *   digit ::= "0"|"1"| ... |"8"|"9"
 *   delimiter ::= ".."|"-"
 */
{
    array t;
    array res;
    array args;
    int i;
    int low, high;
    int j;

	res = [ ];
	if (str[..0] == "[" && str[sizeof(str)-1..] =="]")
		str =str[1..sizeof(str)-2];
	t = explode(str, ",");
	for (i = 0; i < sizeof(t); i ++)
	{
	if (!t[i]) continue; /* dredd - error check */
    if (args = split_low_high(t[i], lowlimit, highlimit))
    {
		for (j = args[0]; j <= args[1]; j ++)
			res += [ j];
    }
	else if (j = atoi(t[i]))
		res += [ j ];
	}
	return res;
}

array range2bitstr(string str, lowlimit, highlimit)
/* convert a string into a bitstring set ..
 * string format is:
 *   strng ::= strng +","+field | ""
 *   field ::= int | int + delimiter + int
 *   int   ::= int + digit | ""
 *   digit ::= "0"|"1"| ... |"8"|"9"
 *   delimiter ::= ".."|"-"
 */
{
    array t;
    array args;
    array res = [ ];
    int i;
    int low, high;
    int j;

	if (str[..0] == "[" && str[sizeof(str)-1..] =="]")
		str =str[1..sizeof(str)-2];
	t = explode(str, ",");
	for (i = 0; i < sizeof(t); i ++)
	{
	    if (!t[i]) continue; /* dredd - error check */
        if (args = split_low_high(t[i], lowlimit, highlimit))
        {
		    for (j = args[0]; j <= args[1]; j ++)
			    res = BIT->set_bit(res, j, 1);
        }
	else if (j = atoi(t[i]))
		res = BIT->set_bit(res, j, 1);
	}
	return res;
}

string bitstr2range(string bitstr, int lo, int hi) {

	int	i, j, k, brace = 0;
	string	res = "";

	i = lo;

	if (hi)
		k = hi;
	else
		k = sizeof(bitstr) * 6;

	while (i <= k ) {
		if (BIT->test_bit(bitstr, i)) {
			if (BIT->test_bit(bitstr, i + 1)) {
				j = i + 1;
				while (BIT->test_bit(bitstr, j))
					j = j + 1;
				if (sizeof(res))
					res = res + "," + i + ".." + (j - 1);
				else
					res = res + i + ".." + (j - 1);
				i = j - 1;
				brace = 1;
			}
			else
				if (sizeof(res))
					res = res + "," + i;
				else
					res = res + "" + i;
		}
		i = i + 1;
	}
	if (brace)
		res = "[" + res + "]";
	return res;
}