]> git.lizzy.rs Git - rust.git/commit
Modify `regex::Captures::{at,name}` to return `Option`
authorEric Kidd <git@randomhacks.net>
Sat, 13 Dec 2014 18:33:18 +0000 (13:33 -0500)
committerEric Kidd <git@randomhacks.net>
Sun, 14 Dec 2014 13:56:51 +0000 (08:56 -0500)
commitc2b0d7dd8818a0dca9b1fa7af6873375907f05ca
tree369f80679f73646e73ed5f58b2041eb8183aa242
parent444fa1b7cffcd99ca5b8abb51acf979f06a25899
Modify `regex::Captures::{at,name}` to return `Option`

Closes #14602.  As discussed in that issue, the existing `at` and `name`
functions represent two different results with the empty string:

1. Matched the empty string.
2. Did not match anything.

Consider the following example.  This regex has two named matched
groups, `key` and `value`. `value` is optional:

```rust
// Matches "foo", "foo;v=bar" and "foo;v=".
regex!(r"(?P<key>[a-z]+)(;v=(?P<value>[a-z]*))?");
```

We can access `value` using `caps.name("value")`, but there's no way for
us to distinguish between the `"foo"` and `"foo;v="` cases.

Early this year, @BurntSushi recommended modifying the existing `at` and
`name` functions to return `Option`, instead of adding new functions to
the API.

This is a [breaking-change], but the fix is easy:

- `refs.at(1)` becomes `refs.at(1).unwrap_or("")`.
- `refs.name(name)` becomes `refs.name(name).unwrap_or("")`.
src/compiletest/compiletest.rs
src/compiletest/errors.rs
src/grammar/verify.rs
src/libregex/lib.rs
src/libregex/re.rs