[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Sc-devel] primitive return value confusion



Here's the regexp code i have done so far. It's relatively straightforward. 
Boost also has support for subexpressions, etc.. I would even be willing to 
give implementing them a try, too. But first i need to sort out this issue:

"foobar".findRegexp("ba", 2)
[ 1, 2 ]

Ok, don't mind the debug output. The return value is a collection of ints, the 
first holding the start of the match and the second the length..

"foobaaaar".findRegexp("ba*")
[ 3, 5 ]

But if i don't match i get:

"foobar".findRegexp("foc")
[ 0, 0 ]

This is a little weird, since in this case i do:

SetNil(a);
return errNone;

and i would expect to get nil back.. I guess i must have messed something up 
with the GC.. Hints?

Thanks,
Flo


int prString_FindRegexp(struct VMGlobals *g, int numArgsPushed)
{
        int err;

        PyrSlot *a = g->sp - 2; // source string
        PyrSlot *b = g->sp - 1; // pattern
        PyrSlot *c = g->sp;     // offset

        std::cout << " num of args: " <<  g->numpop << std::endl;

        if (!isKindOfSlot(b, class_string)) return errWrongType;

        int offset = c->ui;

        char *string = (char*)malloc(a->uo->size + 1);
        err = slotStrVal(a, string, a->uo->size + 1);
        if (err) return err;
       
        char *pattern = (char*)malloc(b->uo->size + 1);
        err = slotStrVal(b, pattern, b->uo->size + 1);
        if (err) return err;


        std::cout << "input string: " << string << std::endl;
        std::cout << "     pattern: " << pattern << std::endl;

        std::cout << "      offset: " << offset << std::endl; 

        std::string stringstring(string);
        std::string::const_iterator start, end;

        start = stringstring.begin() + offset;
        end = stringstring.end();

        boost::match_results<std::string::const_iterator> what;
        boost::match_flag_type flags = boost::match_default;

        boost::regex expression(pattern);

        int match_start = 0;
        int match_length = 0;

        if (boost::regex_search(start, end, what, expression, flags))
        {
                if (what[0].matched == false)
                {
                        SetNil(a);
                        return errNone;
                }
                else
                {
                        std::cout << "match!!"  << std::endl;
                        match_start =  what[0].first - start;
                        match_length = what[0].second - what[0].first;
                        std::cout << match_start << std::endl;
                        std::cout << match_length << std::endl;
                }
        }

	// only here we touch the return values
        PyrObject *array = newPyrArray(g->gc, 2, 0, true);
        array->size = 2;


        SetInt(array->slots, match_start);
        SetInt(array->slots + 1, match_length);
        SetObject(a, array);

        g->gc->GCWrite(array, a);

        return errNone;
}

-- 
Palimm Palimm!
http://tapas.affenbande.org