string options for regular expression functions

Started by zhanglong, August 15, 2014, 08:14:49 AM

Previous topic - Next topic

zhanglong

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



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


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

#1
Thanks Zhanglong, nice addition!



Added here: http://www.newlisp.org/downloads/development/inprogress/">http://www.newlisp.org/downloads/develo ... nprogress/">http://www.newlisp.org/downloads/development/inprogress/

zhanglong

#2
Fogot regex-comp member, others look nice.



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

zhanglong

#3
i < cell->aux - 1



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