string options for regular expression functions

For the Compleat Fan
Locked
zhanglong
Posts: 9
Joined: Thu Jun 27, 2013 3:58 pm

string options for regular expression functions

Post by zhanglong »

Hello, I write a function to support string as regex-options.

Example use "ip" to replce (| 0x10000 1), {u} than 2048

Code: Select all

CELL* parseRegexOptions(CELL* params, UINT * options, int evalFlag)
{
    CELL* cell;
    int i;
    char* buffer;

    if (evalFlag)
        cell = evaluateExpression(params);
    else
        cell = params;

    *options = 0;

    if (cell->type == CELL_STRING) {
        buffer = (char*)cell->contents;
        for (i = 0; i < cell->aux; i ++) {
            switch (buffer[i]) {
                    case 'i': *options |= 1; break;         /* PCRE_CASELESS  */
                    case 'm': *options |= 2; break;         /* PCRE_MULTILINE */
                    case 's': *options |= 4; break;         /* PCRE_DOTALL    */
                    case 'x': *options |= 8; break;         /* PCRE_EXTENDED  */
                    case 'u': *options |= 2048; break;      /* PCRE_UTF8      */
                    case 'p': *options |= 0x10000; break;   /* PRECOMPILED    */
            }
        }
    } else {
        getIntegerExt(cell, options, FALSE);
    }

    return (params->next);
}
First add CELL * parseRegexOptions(CELL * param, UINT * options, int evalFlag); to proto.h

For parse regex replace directory member find-all starts-with ends-with just change getInteger(..., &options) / getIntegerExt(..., &options, FALSE) to parseRegexOptions

search and find check options's type, we need one more step. Change
isNumber(params->type)
to
(isNumber(params->type) || params->type == CELL_STRING)

At last find has two syntax so two place to change.


It's useful for people come from PCRE world. And someone asked this feature months ago.

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Re: string options for regular expression functions

Post by Lutz »

Thanks Zhanglong, nice addition!

Added here: http://www.newlisp.org/downloads/develo ... nprogress/

zhanglong
Posts: 9
Joined: Thu Jun 27, 2013 3:58 pm

Re: string options for regular expression functions

Post by zhanglong »

Fogot regex-comp member, others look nice.

Debug should be easier if carp for other characters, maybe throw No.39 regular expression error.

zhanglong
Posts: 9
Joined: Thu Jun 27, 2013 3:58 pm

Re: string options for regular expression functions

Post by zhanglong »

i < cell->aux - 1

raise error like regexError("Unknown option at", i, buffer);

Locked