Re: find options

new topic     » goto parent     » topic index » view thread      » older message » newer message

Let's say you have wrapped PCRE. When you call pcre:new("[A-Z]+") memory is allocated and returned into euphoria as an atom. Take this example:

include pcre.e as r 
 
procedure is_proper_name(sequence s) 
    r:regex upperRx = r:new("[A-Z][a-z]+ [A-Z][a-z]+") 
    return r:match(upperRx, s) 
end procedure 

So, upperRx was allocated memory. I am unsure of how much each regex structure takes in PCRE, but it could be of a sizable amount. In Euphoria, we have garbage collection and we do not have to free memory as in C, that's one reason we program in Euphoria and not C. However, in the above example, we have leaked memory. the contents of upperRx can never be accessed again, nor is it free'd. The current way of dealing with the above code is this:

include pcre.e as r 
 
procedure is_proper_name(sequence s) 
    r:regex upperRx = r:new("[A-Z][a-z]+ [A-Z][a-z]+") 
    integer result = r:match(upperRx, s) 
    r:free(upperRx) 
    return result 
end procedure 

Now, the code is not so bad, but we have introduced having to deal with memory management in Euphoria, which is not nice. Now, we currently have user defined types that we can have. The addition we are speaking of will include a type cleanup method. This is all in design right now, but take this for example:

public type regex(object r) 
    -- validate r 
end type 
 
public type_cleanup regex(object r) 
    if r <> 0 then 
        pcre_free(r) 
    end if 
end type_cleanup 

Now, the above syntax I have no idea about. We have not discussed it yet, really. But, you get the idea. Now, go back up to the very first example. In this case, upperRx was assigned to a type of regex. When upperRx goes out of scope, Euphoria will now know to call the type_cleanup function associated with the regex user defined type.

This can be used in a few places in the standard library already. For instance, regular expressions and maps. Both of those currently require you to either call free or call delete which is pretty anti-Euphoria.

Jeremy

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu