[go: up one dir, main page]

Menu

[897c37]: / modules / regexp.lpc  Maximize  Restore  History

Download this file

118 lines (94 with data), 2.2 kB

#!/u/geoff/swlpc/bin/lpc

/*
 *
 * LPC / C interface
 *
 * Regular expression matching / support.
 *
 */

int handle;

void reset()
{
    int x;

    handle = load_shared_object("modules/regexp.so");
}

int query_destruct()
{
    unload_shared_object(handle);
    return 0;
}

/*
 * Name: index
 * Purpose: return the first index of a pat in string if 
 *  there is a match, else -1
 *  Ignores the first 'ignore' number of matches 
 */

int index(string str, string pat, int ignore)
{
    return ext_c_call(handle, "lpc_reg_index", str, pat, ignore);
}

/*
 * Name: index
 * Purpose: return the last index of a pat in string if 
 *  there is a match, else -1
 *  Ignores the first 'ignore' number of matches 
 *  going backwards from the end of the string
 */

int rindex(string str, string pat, int ignore)
{
    return ext_c_call(handle, "lpc_reg_rindex", str, pat, ignore);
}

/*
 * Name: index_all
 * Purpose: return an array of all locations which matched
 *   the given pat in str
 */

array index_all(string str, string pat)
{
    return ext_c_call(handle, "lpc_index_all", str, pat);
}


/*
 * Name: split
 * Purpose: split a string by a regular expression
 * if max == 0 - do all possible in split & sub
 */

array split(string str, string pat, int max)
{
    return ext_c_call(handle, "lpc_reg_split", str, pat, max);
}

/*
 * Name: sub
 * Purpose: given a regular expression, substitute max
 *   matches with the given replacement, 0 == unlimited
 */

string sub(string str, string pat, string repl, int max)
{
    return ext_c_call(handle, "lpc_reg_sub", str, pat, repl, max);
}


/*
 * Name: match
 * Purpose: given an array of strings and a pattern
 *  return an array with strings
 *  which matched strings in the original array.
 */
string match(string pat, array arr)
{
    return ext_c_call(handle, "lpc_reg_match", pat, arr);
}

/*
 * Name: escape
 * Purpose: escape a string, all non-alphanumeric chars
 *   are backslashed.
 */

string escape(string str)
{
    return ext_c_call(handle, "lpc_reg_escape", str);
}


void doit()
{
string test="abc2efg8hi2g10l";
array  fud = split(test, "2", "X", 1);
out_to_term("fud[0]="+fud[0]+"\n");
// out_to_term("fud[0]="+fud[0]+"\n");
// out_to_term("fud[1]="+fud[1]+"\n");
}