[Top][All Lists]

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

Re: [Chicken-users] Using irregex safely & responsibly [Was: Re: dev-sna

From: Jim Ursetto
Subject: Re: [Chicken-users] Using irregex safely & responsibly [Was: Re: dev-snapshot 4.6.3]
Date: Thu, 7 Oct 2010 20:37:59 -0500

On Thu, Oct 7, 2010 at 15:53, Peter Bex <address@hidden> wrote:
> In your egg's file, where you would previously use this idiom:
> (require-library regex)      ; or (use regex) for the lazy & sloppy
> (import irregex)
> you can now replace it with this block (you can delete emulation of
> procedures you are sure you aren't using):
> (cond-expand
>  (total-irregex
>  (use irregex))
>  (else
>  (require-library regex)
>  (import (rename irregex
>                  (irregex-match-start irregex-match-start-index)
>                  (irregex-match-end irregex-match-end-index)))
>  (define irregex-num-submatches irregex-submatches)
>  (define irregex-match-num-submatches irregex-submatches)
>  (define (irregex-match-valid-index? m i)
>    (and (irregex-match-start-index m i) #t))
>  (define (maybe-string->sre obj)
>    (if (string? obj) (string->sre obj) obj))))

Does this mean for every egg that uses the irregex API directly, I
need to insert this blob of code?

There is some inconsistency in the docs:

irregex-match-num-submatches: Returns the number of numbered
submatches that are defined in the
irregex or match object.
irregex-match-valid-index?: Returns {{#t}} iff the {{index-or-name}}
named submatch or index is defined in the {{match}} object.

But below, *-valid-index? says undefined when *-num-submatches says defined:

#;1> (define m (irregex-search (irregex "(abc)|(def)|(ghi)") "ghi"))
#;2> (irregex-match-num-submatches m)
#;3> (irregex-match-valid-index? m 2)

The valid-index? predicate does not return a boolean #t value:

#;9> (irregex-match-valid-index? m 3)
#;9> (irregex-match-substring m 3)

Failure behavior for match-start-index and match-substring is
unspecified in the docs.  The former throws an error and the latter
returns #f:

#;3> (irregex-match-start-index m 2)
Error: (irregex-match-start-index) not a valid index
#<regexp-match (3 submatches)>

#;6> (irregex-match-substring m 2)

I prefer the old behavior for consistency because if irregex tells me
that 3 submatches exist, I expect to be able to access them without an
exception being thrown.

reply via email to

[Prev in Thread] Current Thread [Next in Thread]